xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision 0a3da2c2b989bc22c55b3303fde7c852e85937bb)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
29d150b73SToby Isaac #include <petsc/private/petscfeimpl.h>  /*I      "petscfe.h"       I*/
39d150b73SToby Isaac #include <petscblaslapack.h>
4ccd2543fSMatthew G Knepley 
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 
39ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
40ccd2543fSMatthew G Knepley {
41ccd2543fSMatthew G Knepley   const PetscInt  embedDim = 2;
42f5ebc837SMatthew G. Knepley   const PetscReal eps      = PETSC_SQRT_MACHINE_EPSILON;
43ccd2543fSMatthew G Knepley   PetscReal       x        = PetscRealPart(point[0]);
44ccd2543fSMatthew G Knepley   PetscReal       y        = PetscRealPart(point[1]);
45ccd2543fSMatthew G Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
46ccd2543fSMatthew G Knepley   PetscReal       xi, eta;
47ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
48ccd2543fSMatthew G Knepley 
49ccd2543fSMatthew G Knepley   PetscFunctionBegin;
508e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
51ccd2543fSMatthew G Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
52ccd2543fSMatthew G Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
53ccd2543fSMatthew G Knepley 
54f5ebc837SMatthew G. Knepley   if ((xi >= -eps) && (eta >= -eps) && (xi + eta <= 2.0+eps)) *cell = c;
55c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
56ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
57ccd2543fSMatthew G Knepley }
58ccd2543fSMatthew G Knepley 
5962a38674SMatthew G. Knepley static PetscErrorCode DMPlexClosestPoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscReal cpoint[])
6062a38674SMatthew G. Knepley {
6162a38674SMatthew G. Knepley   const PetscInt  embedDim = 2;
6262a38674SMatthew G. Knepley   PetscReal       x        = PetscRealPart(point[0]);
6362a38674SMatthew G. Knepley   PetscReal       y        = PetscRealPart(point[1]);
6462a38674SMatthew G. Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
6562a38674SMatthew G. Knepley   PetscReal       xi, eta, r;
6662a38674SMatthew G. Knepley   PetscErrorCode  ierr;
6762a38674SMatthew G. Knepley 
6862a38674SMatthew G. Knepley   PetscFunctionBegin;
6962a38674SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
7062a38674SMatthew G. Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
7162a38674SMatthew G. Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
7262a38674SMatthew G. Knepley 
7362a38674SMatthew G. Knepley   xi  = PetscMax(xi,  0.0);
7462a38674SMatthew G. Knepley   eta = PetscMax(eta, 0.0);
7562a38674SMatthew G. Knepley   r   = (xi + eta)/2.0;
7662a38674SMatthew G. Knepley   if (xi + eta > 2.0) {
7762a38674SMatthew G. Knepley     r    = (xi + eta)/2.0;
7862a38674SMatthew G. Knepley     xi  /= r;
7962a38674SMatthew G. Knepley     eta /= r;
8062a38674SMatthew G. Knepley   }
8162a38674SMatthew G. Knepley   cpoint[0] = J[0*embedDim+0]*xi + J[0*embedDim+1]*eta + v0[0];
8262a38674SMatthew G. Knepley   cpoint[1] = J[1*embedDim+0]*xi + J[1*embedDim+1]*eta + v0[1];
8362a38674SMatthew G. Knepley   PetscFunctionReturn(0);
8462a38674SMatthew G. Knepley }
8562a38674SMatthew G. Knepley 
86ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
87ccd2543fSMatthew G Knepley {
88ccd2543fSMatthew G Knepley   PetscSection       coordSection;
89ccd2543fSMatthew G Knepley   Vec             coordsLocal;
90a1e44745SMatthew G. Knepley   PetscScalar    *coords = NULL;
91ccd2543fSMatthew G Knepley   const PetscInt  faces[8]  = {0, 1, 1, 2, 2, 3, 3, 0};
92ccd2543fSMatthew G Knepley   PetscReal       x         = PetscRealPart(point[0]);
93ccd2543fSMatthew G Knepley   PetscReal       y         = PetscRealPart(point[1]);
94ccd2543fSMatthew G Knepley   PetscInt        crossings = 0, f;
95ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
96ccd2543fSMatthew G Knepley 
97ccd2543fSMatthew G Knepley   PetscFunctionBegin;
98ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
9969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
100ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
101ccd2543fSMatthew G Knepley   for (f = 0; f < 4; ++f) {
102ccd2543fSMatthew G Knepley     PetscReal x_i   = PetscRealPart(coords[faces[2*f+0]*2+0]);
103ccd2543fSMatthew G Knepley     PetscReal y_i   = PetscRealPart(coords[faces[2*f+0]*2+1]);
104ccd2543fSMatthew G Knepley     PetscReal x_j   = PetscRealPart(coords[faces[2*f+1]*2+0]);
105ccd2543fSMatthew G Knepley     PetscReal y_j   = PetscRealPart(coords[faces[2*f+1]*2+1]);
106ccd2543fSMatthew G Knepley     PetscReal slope = (y_j - y_i) / (x_j - x_i);
107ccd2543fSMatthew G Knepley     PetscBool cond1 = (x_i <= x) && (x < x_j) ? PETSC_TRUE : PETSC_FALSE;
108ccd2543fSMatthew G Knepley     PetscBool cond2 = (x_j <= x) && (x < x_i) ? PETSC_TRUE : PETSC_FALSE;
109ccd2543fSMatthew G Knepley     PetscBool above = (y < slope * (x - x_i) + y_i) ? PETSC_TRUE : PETSC_FALSE;
110ccd2543fSMatthew G Knepley     if ((cond1 || cond2)  && above) ++crossings;
111ccd2543fSMatthew G Knepley   }
112ccd2543fSMatthew G Knepley   if (crossings % 2) *cell = c;
113c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
114ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
115ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
116ccd2543fSMatthew G Knepley }
117ccd2543fSMatthew G Knepley 
118ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
119ccd2543fSMatthew G Knepley {
120ccd2543fSMatthew G Knepley   const PetscInt embedDim = 3;
121ccd2543fSMatthew G Knepley   PetscReal      v0[3], J[9], invJ[9], detJ;
122ccd2543fSMatthew G Knepley   PetscReal      x = PetscRealPart(point[0]);
123ccd2543fSMatthew G Knepley   PetscReal      y = PetscRealPart(point[1]);
124ccd2543fSMatthew G Knepley   PetscReal      z = PetscRealPart(point[2]);
125ccd2543fSMatthew G Knepley   PetscReal      xi, eta, zeta;
126ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
127ccd2543fSMatthew G Knepley 
128ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1298e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
130ccd2543fSMatthew G Knepley   xi   = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]) + invJ[0*embedDim+2]*(z - v0[2]);
131ccd2543fSMatthew G Knepley   eta  = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]) + invJ[1*embedDim+2]*(z - v0[2]);
132ccd2543fSMatthew G Knepley   zeta = invJ[2*embedDim+0]*(x - v0[0]) + invJ[2*embedDim+1]*(y - v0[1]) + invJ[2*embedDim+2]*(z - v0[2]);
133ccd2543fSMatthew G Knepley 
134ccd2543fSMatthew G Knepley   if ((xi >= 0.0) && (eta >= 0.0) && (zeta >= 0.0) && (xi + eta + zeta <= 2.0)) *cell = c;
135c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
136ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
137ccd2543fSMatthew G Knepley }
138ccd2543fSMatthew G Knepley 
139ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
140ccd2543fSMatthew G Knepley {
141ccd2543fSMatthew G Knepley   PetscSection   coordSection;
142ccd2543fSMatthew G Knepley   Vec            coordsLocal;
1437c1f9639SMatthew G Knepley   PetscScalar   *coords;
144fb150da6SMatthew G. Knepley   const PetscInt faces[24] = {0, 3, 2, 1,  5, 4, 7, 6,  3, 0, 4, 5,
145fb150da6SMatthew G. Knepley                               1, 2, 6, 7,  3, 5, 6, 2,  0, 1, 7, 4};
146ccd2543fSMatthew G Knepley   PetscBool      found = PETSC_TRUE;
147ccd2543fSMatthew G Knepley   PetscInt       f;
148ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
149ccd2543fSMatthew G Knepley 
150ccd2543fSMatthew G Knepley   PetscFunctionBegin;
151ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
15269d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
153ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
154ccd2543fSMatthew G Knepley   for (f = 0; f < 6; ++f) {
155ccd2543fSMatthew G Knepley     /* Check the point is under plane */
156ccd2543fSMatthew G Knepley     /*   Get face normal */
157ccd2543fSMatthew G Knepley     PetscReal v_i[3];
158ccd2543fSMatthew G Knepley     PetscReal v_j[3];
159ccd2543fSMatthew G Knepley     PetscReal normal[3];
160ccd2543fSMatthew G Knepley     PetscReal pp[3];
161ccd2543fSMatthew G Knepley     PetscReal dot;
162ccd2543fSMatthew G Knepley 
163ccd2543fSMatthew G Knepley     v_i[0]    = PetscRealPart(coords[faces[f*4+3]*3+0]-coords[faces[f*4+0]*3+0]);
164ccd2543fSMatthew G Knepley     v_i[1]    = PetscRealPart(coords[faces[f*4+3]*3+1]-coords[faces[f*4+0]*3+1]);
165ccd2543fSMatthew G Knepley     v_i[2]    = PetscRealPart(coords[faces[f*4+3]*3+2]-coords[faces[f*4+0]*3+2]);
166ccd2543fSMatthew G Knepley     v_j[0]    = PetscRealPart(coords[faces[f*4+1]*3+0]-coords[faces[f*4+0]*3+0]);
167ccd2543fSMatthew G Knepley     v_j[1]    = PetscRealPart(coords[faces[f*4+1]*3+1]-coords[faces[f*4+0]*3+1]);
168ccd2543fSMatthew G Knepley     v_j[2]    = PetscRealPart(coords[faces[f*4+1]*3+2]-coords[faces[f*4+0]*3+2]);
169ccd2543fSMatthew G Knepley     normal[0] = v_i[1]*v_j[2] - v_i[2]*v_j[1];
170ccd2543fSMatthew G Knepley     normal[1] = v_i[2]*v_j[0] - v_i[0]*v_j[2];
171ccd2543fSMatthew G Knepley     normal[2] = v_i[0]*v_j[1] - v_i[1]*v_j[0];
172ccd2543fSMatthew G Knepley     pp[0]     = PetscRealPart(coords[faces[f*4+0]*3+0] - point[0]);
173ccd2543fSMatthew G Knepley     pp[1]     = PetscRealPart(coords[faces[f*4+0]*3+1] - point[1]);
174ccd2543fSMatthew G Knepley     pp[2]     = PetscRealPart(coords[faces[f*4+0]*3+2] - point[2]);
175ccd2543fSMatthew G Knepley     dot       = normal[0]*pp[0] + normal[1]*pp[1] + normal[2]*pp[2];
176ccd2543fSMatthew G Knepley 
177ccd2543fSMatthew G Knepley     /* Check that projected point is in face (2D location problem) */
178ccd2543fSMatthew G Knepley     if (dot < 0.0) {
179ccd2543fSMatthew G Knepley       found = PETSC_FALSE;
180ccd2543fSMatthew G Knepley       break;
181ccd2543fSMatthew G Knepley     }
182ccd2543fSMatthew G Knepley   }
183ccd2543fSMatthew G Knepley   if (found) *cell = c;
184c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
185ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
186ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
187ccd2543fSMatthew G Knepley }
188ccd2543fSMatthew G Knepley 
189c4eade1cSMatthew G. Knepley static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[])
190c4eade1cSMatthew G. Knepley {
191c4eade1cSMatthew G. Knepley   PetscInt d;
192c4eade1cSMatthew G. Knepley 
193c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
194c4eade1cSMatthew G. Knepley   box->dim = dim;
195c4eade1cSMatthew G. Knepley   for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]);
196c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
197c4eade1cSMatthew G. Knepley }
198c4eade1cSMatthew G. Knepley 
199c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box)
200c4eade1cSMatthew G. Knepley {
201c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
202c4eade1cSMatthew G. Knepley 
203c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
204c4eade1cSMatthew G. Knepley   ierr = PetscMalloc1(1, box);CHKERRQ(ierr);
205c4eade1cSMatthew G. Knepley   ierr = PetscGridHashInitialize_Internal(*box, dim, point);CHKERRQ(ierr);
206c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
207c4eade1cSMatthew G. Knepley }
208c4eade1cSMatthew G. Knepley 
209c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[])
210c4eade1cSMatthew G. Knepley {
211c4eade1cSMatthew G. Knepley   PetscInt d;
212c4eade1cSMatthew G. Knepley 
213c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
214c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
215c4eade1cSMatthew G. Knepley     box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d]));
216c4eade1cSMatthew G. Knepley     box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d]));
217c4eade1cSMatthew G. Knepley   }
218c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
219c4eade1cSMatthew G. Knepley }
220c4eade1cSMatthew G. Knepley 
22162a38674SMatthew G. Knepley /*
22262a38674SMatthew G. Knepley   PetscGridHashSetGrid - Divide the grid into boxes
22362a38674SMatthew G. Knepley 
22462a38674SMatthew G. Knepley   Not collective
22562a38674SMatthew G. Knepley 
22662a38674SMatthew G. Knepley   Input Parameters:
22762a38674SMatthew G. Knepley + box - The grid hash object
22862a38674SMatthew G. Knepley . n   - The number of boxes in each dimension, or PETSC_DETERMINE
22962a38674SMatthew G. Knepley - h   - The box size in each dimension, only used if n[d] == PETSC_DETERMINE
23062a38674SMatthew G. Knepley 
23162a38674SMatthew G. Knepley   Level: developer
23262a38674SMatthew G. Knepley 
23362a38674SMatthew G. Knepley .seealso: PetscGridHashCreate()
23462a38674SMatthew G. Knepley */
235c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[])
236c4eade1cSMatthew G. Knepley {
237c4eade1cSMatthew G. Knepley   PetscInt d;
238c4eade1cSMatthew G. Knepley 
239c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
240c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
241c4eade1cSMatthew G. Knepley     box->extent[d] = box->upper[d] - box->lower[d];
242c4eade1cSMatthew G. Knepley     if (n[d] == PETSC_DETERMINE) {
243c4eade1cSMatthew G. Knepley       box->h[d] = h[d];
244c4eade1cSMatthew G. Knepley       box->n[d] = PetscCeilReal(box->extent[d]/h[d]);
245c4eade1cSMatthew G. Knepley     } else {
246c4eade1cSMatthew G. Knepley       box->n[d] = n[d];
247c4eade1cSMatthew G. Knepley       box->h[d] = box->extent[d]/n[d];
248c4eade1cSMatthew G. Knepley     }
249c4eade1cSMatthew G. Knepley   }
250c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
251c4eade1cSMatthew G. Knepley }
252c4eade1cSMatthew G. Knepley 
25362a38674SMatthew G. Knepley /*
25462a38674SMatthew G. Knepley   PetscGridHashGetEnclosingBox - Find the grid boxes containing each input point
25562a38674SMatthew G. Knepley 
25662a38674SMatthew G. Knepley   Not collective
25762a38674SMatthew G. Knepley 
25862a38674SMatthew G. Knepley   Input Parameters:
25962a38674SMatthew G. Knepley + box       - The grid hash object
26062a38674SMatthew G. Knepley . numPoints - The number of input points
26162a38674SMatthew G. Knepley - points    - The input point coordinates
26262a38674SMatthew G. Knepley 
26362a38674SMatthew G. Knepley   Output Parameters:
26462a38674SMatthew G. Knepley + dboxes    - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
26562a38674SMatthew G. Knepley - boxes     - An array of numPoints integers expressing the enclosing box as single number, or NULL
26662a38674SMatthew G. Knepley 
26762a38674SMatthew G. Knepley   Level: developer
26862a38674SMatthew G. Knepley 
26962a38674SMatthew G. Knepley .seealso: PetscGridHashCreate()
27062a38674SMatthew G. Knepley */
2711c6dfc3eSMatthew G. Knepley PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[])
272c4eade1cSMatthew G. Knepley {
273c4eade1cSMatthew G. Knepley   const PetscReal *lower = box->lower;
274c4eade1cSMatthew G. Knepley   const PetscReal *upper = box->upper;
275c4eade1cSMatthew G. Knepley   const PetscReal *h     = box->h;
276c4eade1cSMatthew G. Knepley   const PetscInt  *n     = box->n;
277c4eade1cSMatthew G. Knepley   const PetscInt   dim   = box->dim;
278c4eade1cSMatthew G. Knepley   PetscInt         d, p;
279c4eade1cSMatthew G. Knepley 
280c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
281c4eade1cSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
282c4eade1cSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
2831c6dfc3eSMatthew G. Knepley       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]);
284c4eade1cSMatthew G. Knepley 
2851c6dfc3eSMatthew G. Knepley       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1;
2862a705cacSMatthew G. Knepley       if (dbox == -1   && PetscAbsReal(PetscRealPart(points[p*dim+d]) - lower[d]) < 1.0e-9) dbox = 0;
287c4eade1cSMatthew 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",
2881c6dfc3eSMatthew 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);
289c4eade1cSMatthew G. Knepley       dboxes[p*dim+d] = dbox;
290c4eade1cSMatthew G. Knepley     }
291c4eade1cSMatthew G. Knepley     if (boxes) for (d = 1, boxes[p] = dboxes[p*dim]; d < dim; ++d) boxes[p] += dboxes[p*dim+d]*n[d-1];
292c4eade1cSMatthew G. Knepley   }
293c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
294c4eade1cSMatthew G. Knepley }
295c4eade1cSMatthew G. Knepley 
296c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashDestroy(PetscGridHash *box)
297c4eade1cSMatthew G. Knepley {
298c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
299c4eade1cSMatthew G. Knepley 
300c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
301c4eade1cSMatthew G. Knepley   if (*box) {
302c4eade1cSMatthew G. Knepley     ierr = PetscSectionDestroy(&(*box)->cellSection);CHKERRQ(ierr);
303c4eade1cSMatthew G. Knepley     ierr = ISDestroy(&(*box)->cells);CHKERRQ(ierr);
304c4eade1cSMatthew G. Knepley     ierr = DMLabelDestroy(&(*box)->cellsSparse);CHKERRQ(ierr);
305c4eade1cSMatthew G. Knepley   }
306c4eade1cSMatthew G. Knepley   ierr = PetscFree(*box);CHKERRQ(ierr);
307c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
308c4eade1cSMatthew G. Knepley }
309c4eade1cSMatthew G. Knepley 
310cafe43deSMatthew G. Knepley PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell)
311cafe43deSMatthew G. Knepley {
312cafe43deSMatthew G. Knepley   PetscInt       coneSize;
313cafe43deSMatthew G. Knepley   PetscErrorCode ierr;
314cafe43deSMatthew G. Knepley 
315cafe43deSMatthew G. Knepley   PetscFunctionBegin;
316cafe43deSMatthew G. Knepley   switch (dim) {
317cafe43deSMatthew G. Knepley   case 2:
318cafe43deSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr);
319cafe43deSMatthew G. Knepley     switch (coneSize) {
320cafe43deSMatthew G. Knepley     case 3:
321cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
322cafe43deSMatthew G. Knepley       break;
323cafe43deSMatthew G. Knepley     case 4:
324cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_General_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
325cafe43deSMatthew G. Knepley       break;
326cafe43deSMatthew G. Knepley     default:
327cafe43deSMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize);
328cafe43deSMatthew G. Knepley     }
329cafe43deSMatthew G. Knepley     break;
330cafe43deSMatthew G. Knepley   case 3:
331cafe43deSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr);
332cafe43deSMatthew G. Knepley     switch (coneSize) {
333cafe43deSMatthew G. Knepley     case 4:
334cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
335cafe43deSMatthew G. Knepley       break;
336cafe43deSMatthew G. Knepley     case 6:
337cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
338cafe43deSMatthew G. Knepley       break;
339cafe43deSMatthew G. Knepley     default:
340cafe43deSMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize);
341cafe43deSMatthew G. Knepley     }
342cafe43deSMatthew G. Knepley     break;
343cafe43deSMatthew G. Knepley   default:
344cafe43deSMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for mesh dimension %D", dim);
345cafe43deSMatthew G. Knepley   }
346cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
347cafe43deSMatthew G. Knepley }
348cafe43deSMatthew G. Knepley 
34962a38674SMatthew G. Knepley /*
35062a38674SMatthew G. Knepley   DMPlexClosestPoint_Internal - Returns the closest point in the cell to the given point
35162a38674SMatthew G. Knepley */
35262a38674SMatthew G. Knepley PetscErrorCode DMPlexClosestPoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cell, PetscReal cpoint[])
35362a38674SMatthew G. Knepley {
35462a38674SMatthew G. Knepley   PetscInt       coneSize;
35562a38674SMatthew G. Knepley   PetscErrorCode ierr;
35662a38674SMatthew G. Knepley 
35762a38674SMatthew G. Knepley   PetscFunctionBegin;
35862a38674SMatthew G. Knepley   switch (dim) {
35962a38674SMatthew G. Knepley   case 2:
36062a38674SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
36162a38674SMatthew G. Knepley     switch (coneSize) {
36262a38674SMatthew G. Knepley     case 3:
36362a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_Simplex_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
36462a38674SMatthew G. Knepley       break;
36562a38674SMatthew G. Knepley #if 0
36662a38674SMatthew G. Knepley     case 4:
36762a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_General_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
36862a38674SMatthew G. Knepley       break;
36962a38674SMatthew G. Knepley #endif
37062a38674SMatthew G. Knepley     default:
37162a38674SMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell with cone size %D", coneSize);
37262a38674SMatthew G. Knepley     }
37362a38674SMatthew G. Knepley     break;
37462a38674SMatthew G. Knepley #if 0
37562a38674SMatthew G. Knepley   case 3:
37662a38674SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
37762a38674SMatthew G. Knepley     switch (coneSize) {
37862a38674SMatthew G. Knepley     case 4:
37962a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_Simplex_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
38062a38674SMatthew G. Knepley       break;
38162a38674SMatthew G. Knepley     case 6:
38262a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_General_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
38362a38674SMatthew G. Knepley       break;
38462a38674SMatthew G. Knepley     default:
38562a38674SMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell with cone size %D", coneSize);
38662a38674SMatthew G. Knepley     }
38762a38674SMatthew G. Knepley     break;
38862a38674SMatthew G. Knepley #endif
38962a38674SMatthew G. Knepley   default:
39062a38674SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for mesh dimension %D", dim);
39162a38674SMatthew G. Knepley   }
39262a38674SMatthew G. Knepley   PetscFunctionReturn(0);
39362a38674SMatthew G. Knepley }
39462a38674SMatthew G. Knepley 
39562a38674SMatthew G. Knepley /*
39662a38674SMatthew G. Knepley   DMPlexComputeGridHash_Internal - Create a grid hash structure covering the Plex
39762a38674SMatthew G. Knepley 
39862a38674SMatthew G. Knepley   Collective on DM
39962a38674SMatthew G. Knepley 
40062a38674SMatthew G. Knepley   Input Parameter:
40162a38674SMatthew G. Knepley . dm - The Plex
40262a38674SMatthew G. Knepley 
40362a38674SMatthew G. Knepley   Output Parameter:
40462a38674SMatthew G. Knepley . localBox - The grid hash object
40562a38674SMatthew G. Knepley 
40662a38674SMatthew G. Knepley   Level: developer
40762a38674SMatthew G. Knepley 
40862a38674SMatthew G. Knepley .seealso: PetscGridHashCreate(), PetscGridHashGetEnclosingBox()
40962a38674SMatthew G. Knepley */
410cafe43deSMatthew G. Knepley PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox)
411cafe43deSMatthew G. Knepley {
412cafe43deSMatthew G. Knepley   MPI_Comm           comm;
413cafe43deSMatthew G. Knepley   PetscGridHash      lbox;
414cafe43deSMatthew G. Knepley   Vec                coordinates;
415cafe43deSMatthew G. Knepley   PetscSection       coordSection;
416cafe43deSMatthew G. Knepley   Vec                coordsLocal;
417cafe43deSMatthew G. Knepley   const PetscScalar *coords;
418722d0f5cSMatthew G. Knepley   PetscInt          *dboxes, *boxes;
419cafe43deSMatthew G. Knepley   PetscInt           n[3] = {10, 10, 10};
4201d0c6c94SMatthew G. Knepley   PetscInt           dim, N, cStart, cEnd, cMax, c, i;
421cafe43deSMatthew G. Knepley   PetscErrorCode     ierr;
422cafe43deSMatthew G. Knepley 
423cafe43deSMatthew G. Knepley   PetscFunctionBegin;
424cafe43deSMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
425cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
426cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
4275b3353d8SMatthew G. Knepley   if (dim != 2) SETERRQ(comm, PETSC_ERR_SUP, "I have only coded this for 2D");
428cafe43deSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
429cafe43deSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
430cafe43deSMatthew G. Knepley   ierr = PetscGridHashCreate(comm, dim, coords, &lbox);CHKERRQ(ierr);
431cafe43deSMatthew G. Knepley   for (i = 0; i < N; i += dim) {ierr = PetscGridHashEnlarge(lbox, &coords[i]);CHKERRQ(ierr);}
432cafe43deSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
433cafe43deSMatthew G. Knepley   ierr = PetscGridHashSetGrid(lbox, n, NULL);CHKERRQ(ierr);
434cafe43deSMatthew G. Knepley #if 0
435cafe43deSMatthew G. Knepley   /* Could define a custom reduction to merge these */
436b2566f29SBarry Smith   ierr = MPIU_Allreduce(lbox->lower, gbox->lower, 3, MPIU_REAL, MPI_MIN, comm);CHKERRQ(ierr);
437b2566f29SBarry Smith   ierr = MPIU_Allreduce(lbox->upper, gbox->upper, 3, MPIU_REAL, MPI_MAX, comm);CHKERRQ(ierr);
438cafe43deSMatthew G. Knepley #endif
439cafe43deSMatthew G. Knepley   /* Is there a reason to snap the local bounding box to a division of the global box? */
440cafe43deSMatthew G. Knepley   /* Should we compute all overlaps of local boxes? We could do this with a rendevouz scheme partitioning the global box */
441cafe43deSMatthew G. Knepley   /* Create label */
442cafe43deSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
4431d0c6c94SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
4441d0c6c94SMatthew G. Knepley   if (cMax >= 0) cEnd = PetscMin(cEnd, cMax);
445cafe43deSMatthew G. Knepley   ierr = DMLabelCreate("cells", &lbox->cellsSparse);CHKERRQ(ierr);
446cafe43deSMatthew G. Knepley   ierr = DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd);CHKERRQ(ierr);
447722d0f5cSMatthew G. Knepley   /* Compute boxes which overlap each cell: http://stackoverflow.com/questions/13790208/triangle-square-intersection-test-in-2d */
448cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
449cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
45038353de4SMatthew G. Knepley   ierr = PetscCalloc2(16 * dim, &dboxes, 16, &boxes);CHKERRQ(ierr);
451cafe43deSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
452cafe43deSMatthew G. Knepley     const PetscReal *h       = lbox->h;
453cafe43deSMatthew G. Knepley     PetscScalar     *ccoords = NULL;
45438353de4SMatthew G. Knepley     PetscInt         csize   = 0;
455cafe43deSMatthew G. Knepley     PetscScalar      point[3];
456cafe43deSMatthew G. Knepley     PetscInt         dlim[6], d, e, i, j, k;
457cafe43deSMatthew G. Knepley 
458cafe43deSMatthew G. Knepley     /* Find boxes enclosing each vertex */
45938353de4SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &csize, &ccoords);CHKERRQ(ierr);
46038353de4SMatthew G. Knepley     ierr = PetscGridHashGetEnclosingBox(lbox, csize/dim, ccoords, dboxes, boxes);CHKERRQ(ierr);
461722d0f5cSMatthew G. Knepley     /* Mark cells containing the vertices */
46238353de4SMatthew G. Knepley     for (e = 0; e < csize/dim; ++e) {ierr = DMLabelSetValue(lbox->cellsSparse, c, boxes[e]);CHKERRQ(ierr);}
463cafe43deSMatthew G. Knepley     /* Get grid of boxes containing these */
464cafe43deSMatthew G. Knepley     for (d = 0;   d < dim; ++d) {dlim[d*2+0] = dlim[d*2+1] = dboxes[d];}
4652291669eSMatthew G. Knepley     for (d = dim; d < 3;   ++d) {dlim[d*2+0] = dlim[d*2+1] = 0;}
466cafe43deSMatthew G. Knepley     for (e = 1; e < dim+1; ++e) {
467cafe43deSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
468cafe43deSMatthew G. Knepley         dlim[d*2+0] = PetscMin(dlim[d*2+0], dboxes[e*dim+d]);
469cafe43deSMatthew G. Knepley         dlim[d*2+1] = PetscMax(dlim[d*2+1], dboxes[e*dim+d]);
470cafe43deSMatthew G. Knepley       }
471cafe43deSMatthew G. Knepley     }
472fea14342SMatthew G. Knepley     /* Check for intersection of box with cell */
473cafe43deSMatthew 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]) {
474cafe43deSMatthew 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]) {
475cafe43deSMatthew 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]) {
476cafe43deSMatthew G. Knepley           const PetscInt box = (k*lbox->n[1] + j)*lbox->n[0] + i;
477cafe43deSMatthew G. Knepley           PetscScalar    cpoint[3];
478fea14342SMatthew G. Knepley           PetscInt       cell, edge, ii, jj, kk;
479cafe43deSMatthew G. Knepley 
480fea14342SMatthew G. Knepley           /* Check whether cell contains any vertex of these subboxes TODO vectorize this */
481cafe43deSMatthew G. Knepley           for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) {
482cafe43deSMatthew G. Knepley             for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) {
483cafe43deSMatthew G. Knepley               for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) {
484cafe43deSMatthew G. Knepley 
485cafe43deSMatthew G. Knepley                 ierr = DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell);CHKERRQ(ierr);
486cafe43deSMatthew G. Knepley                 if (cell >= 0) {DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); ii = jj = kk = 2;}
487cafe43deSMatthew G. Knepley               }
488cafe43deSMatthew G. Knepley             }
489cafe43deSMatthew G. Knepley           }
490fea14342SMatthew G. Knepley           /* Check whether cell edge intersects any edge of these subboxes TODO vectorize this */
491fea14342SMatthew G. Knepley           for (edge = 0; edge < dim+1; ++edge) {
492fea14342SMatthew G. Knepley             PetscReal segA[6], segB[6];
493fea14342SMatthew G. Knepley 
494fea14342SMatthew 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]);}
495fea14342SMatthew G. Knepley             for (kk = 0; kk < (dim > 2 ? 2 : 1); ++kk) {
4969a128ed2SMatthew G. Knepley               if (dim > 2) {segB[2]     = PetscRealPart(point[2]);
4979a128ed2SMatthew G. Knepley                             segB[dim+2] = PetscRealPart(point[2]) + kk*h[2];}
498fea14342SMatthew G. Knepley               for (jj = 0; jj < (dim > 1 ? 2 : 1); ++jj) {
4999a128ed2SMatthew G. Knepley                 if (dim > 1) {segB[1]     = PetscRealPart(point[1]);
5009a128ed2SMatthew G. Knepley                               segB[dim+1] = PetscRealPart(point[1]) + jj*h[1];}
501fea14342SMatthew G. Knepley                 for (ii = 0; ii < 2; ++ii) {
502fea14342SMatthew G. Knepley                   PetscBool intersects;
503fea14342SMatthew G. Knepley 
5049a128ed2SMatthew G. Knepley                   segB[0]     = PetscRealPart(point[0]);
5059a128ed2SMatthew G. Knepley                   segB[dim+0] = PetscRealPart(point[0]) + ii*h[0];
506fea14342SMatthew G. Knepley                   ierr = DMPlexGetLineIntersection_2D_Internal(segA, segB, NULL, &intersects);CHKERRQ(ierr);
507fea14342SMatthew G. Knepley                   if (intersects) {DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); edge = ii = jj = kk = dim+1;}
508cafe43deSMatthew G. Knepley                 }
509cafe43deSMatthew G. Knepley               }
510cafe43deSMatthew G. Knepley             }
511cafe43deSMatthew G. Knepley           }
512fea14342SMatthew G. Knepley         }
513fea14342SMatthew G. Knepley       }
514fea14342SMatthew G. Knepley     }
515fea14342SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords);CHKERRQ(ierr);
516fea14342SMatthew G. Knepley   }
517722d0f5cSMatthew G. Knepley   ierr = PetscFree2(dboxes, boxes);CHKERRQ(ierr);
518cafe43deSMatthew G. Knepley   ierr = DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells);CHKERRQ(ierr);
519cafe43deSMatthew G. Knepley   ierr = DMLabelDestroy(&lbox->cellsSparse);CHKERRQ(ierr);
520cafe43deSMatthew G. Knepley   *localBox = lbox;
521cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
522cafe43deSMatthew G. Knepley }
523cafe43deSMatthew G. Knepley 
52462a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, DMPointLocationType ltype, PetscSF cellSF)
525ccd2543fSMatthew G Knepley {
526cafe43deSMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
527953fc75cSMatthew G. Knepley   PetscBool       hash = mesh->useHashLocation;
5283a93e3b7SToby Isaac   PetscInt        bs, numPoints, p, numFound, *found = NULL;
5291318edbeSMatthew G. Knepley   PetscInt        dim, cStart, cEnd, cMax, numCells, c;
530cafe43deSMatthew G. Knepley   const PetscInt *boxCells;
5313a93e3b7SToby Isaac   PetscSFNode    *cells;
532ccd2543fSMatthew G Knepley   PetscScalar    *a;
5333a93e3b7SToby Isaac   PetscMPIInt     result;
534ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
535ccd2543fSMatthew G Knepley 
536ccd2543fSMatthew G Knepley   PetscFunctionBegin;
537080342d1SMatthew 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.");
538cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
539cafe43deSMatthew G. Knepley   ierr = VecGetBlockSize(v, &bs);CHKERRQ(ierr);
5403a93e3b7SToby Isaac   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)cellSF),PETSC_COMM_SELF,&result);CHKERRQ(ierr);
5413a93e3b7SToby Isaac   if (result != MPI_IDENT && result != MPI_CONGRUENT) SETERRQ(PetscObjectComm((PetscObject)cellSF),PETSC_ERR_SUP, "Trying parallel point location: only local point location supported");
542cafe43deSMatthew 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);
543ccd2543fSMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
544ccd2543fSMatthew G Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
545ccd2543fSMatthew G Knepley   if (cMax >= 0) cEnd = PetscMin(cEnd, cMax);
546ccd2543fSMatthew G Knepley   ierr = VecGetLocalSize(v, &numPoints);CHKERRQ(ierr);
547ccd2543fSMatthew G Knepley   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
548ccd2543fSMatthew G Knepley   numPoints /= bs;
549785e854fSJed Brown   ierr = PetscMalloc1(numPoints, &cells);CHKERRQ(ierr);
550953fc75cSMatthew G. Knepley   if (hash) {
551ac6ec2abSMatthew G. Knepley     if (!mesh->lbox) {ierr = PetscInfo(dm, "Initializing grid hashing");CHKERRQ(ierr);ierr = DMPlexComputeGridHash_Internal(dm, &mesh->lbox);CHKERRQ(ierr);}
552cafe43deSMatthew G. Knepley     /* Designate the local box for each point */
553cafe43deSMatthew G. Knepley     /* Send points to correct process */
554cafe43deSMatthew G. Knepley     /* Search cells that lie in each subbox */
555cafe43deSMatthew G. Knepley     /*   Should we bin points before doing search? */
556cafe43deSMatthew G. Knepley     ierr = ISGetIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);
557953fc75cSMatthew G. Knepley   }
5583a93e3b7SToby Isaac   for (p = 0, numFound = 0; p < numPoints; ++p) {
559ccd2543fSMatthew G Knepley     const PetscScalar *point = &a[p*bs];
560953fc75cSMatthew G. Knepley     PetscInt           dbin[3], bin, cell = -1, cellOffset;
561ccd2543fSMatthew G Knepley 
562e9b685f5SMatthew G. Knepley     cells[p].rank  = 0;
563e9b685f5SMatthew G. Knepley     cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
564953fc75cSMatthew G. Knepley     if (hash) {
565cafe43deSMatthew G. Knepley       ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr);
566cafe43deSMatthew G. Knepley       /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */
567cafe43deSMatthew G. Knepley       ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
568cafe43deSMatthew G. Knepley       ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
569cafe43deSMatthew G. Knepley       for (c = cellOffset; c < cellOffset + numCells; ++c) {
570cafe43deSMatthew G. Knepley         ierr = DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell);CHKERRQ(ierr);
5713a93e3b7SToby Isaac         if (cell >= 0) {
5723a93e3b7SToby Isaac           cells[p].rank = 0;
5733a93e3b7SToby Isaac           cells[p].index = cell;
5743a93e3b7SToby Isaac           numFound++;
5753a93e3b7SToby Isaac           break;
576ccd2543fSMatthew G Knepley         }
5773a93e3b7SToby Isaac       }
578953fc75cSMatthew G. Knepley     } else {
579953fc75cSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
580953fc75cSMatthew G. Knepley         ierr = DMPlexLocatePoint_Internal(dm, dim, point, c, &cell);CHKERRQ(ierr);
5813a93e3b7SToby Isaac         if (cell >= 0) {
5823a93e3b7SToby Isaac           cells[p].rank = 0;
5833a93e3b7SToby Isaac           cells[p].index = cell;
5843a93e3b7SToby Isaac           numFound++;
5853a93e3b7SToby Isaac           break;
586953fc75cSMatthew G. Knepley         }
587953fc75cSMatthew G. Knepley       }
5883a93e3b7SToby Isaac     }
589ccd2543fSMatthew G Knepley   }
590953fc75cSMatthew G. Knepley   if (hash) {ierr = ISRestoreIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);}
59162a38674SMatthew G. Knepley   if (ltype == DM_POINTLOCATION_NEAREST && hash && numFound < numPoints) {
59262a38674SMatthew G. Knepley     for (p = 0; p < numPoints; p++) {
59362a38674SMatthew G. Knepley       const PetscScalar *point = &a[p*bs];
59462a38674SMatthew G. Knepley       PetscReal          cpoint[3], diff[3], dist, distMax = PETSC_MAX_REAL;
595b716b415SMatthew G. Knepley       PetscInt           dbin[3], bin, cellOffset, d;
59662a38674SMatthew G. Knepley 
597e9b685f5SMatthew G. Knepley       if (cells[p].index < 0) {
59862a38674SMatthew G. Knepley         ++numFound;
59962a38674SMatthew G. Knepley         ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr);
60062a38674SMatthew G. Knepley         ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
60162a38674SMatthew G. Knepley         ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
60262a38674SMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
60362a38674SMatthew G. Knepley           ierr = DMPlexClosestPoint_Internal(dm, dim, point, boxCells[c], cpoint);CHKERRQ(ierr);
604b716b415SMatthew G. Knepley           for (d = 0; d < dim; ++d) diff[d] = cpoint[d] - PetscRealPart(point[d]);
60562a38674SMatthew G. Knepley           dist = DMPlex_NormD_Internal(dim, diff);
60662a38674SMatthew G. Knepley           if (dist < distMax) {
60762a38674SMatthew G. Knepley             for (d = 0; d < dim; ++d) a[p*bs+d] = cpoint[d];
60862a38674SMatthew G. Knepley             cells[p].rank  = 0;
60962a38674SMatthew G. Knepley             cells[p].index = boxCells[c];
61062a38674SMatthew G. Knepley             distMax = dist;
61162a38674SMatthew G. Knepley             break;
61262a38674SMatthew G. Knepley           }
61362a38674SMatthew G. Knepley         }
61462a38674SMatthew G. Knepley       }
61562a38674SMatthew G. Knepley     }
61662a38674SMatthew G. Knepley   }
61762a38674SMatthew G. Knepley   /* This code is only be relevant when interfaced to parallel point location */
618cafe43deSMatthew G. Knepley   /* Check for highest numbered proc that claims a point (do we care?) */
6192d1fa6caSMatthew G. Knepley   if (ltype == DM_POINTLOCATION_REMOVE && numFound < numPoints) {
6203a93e3b7SToby Isaac     ierr = PetscMalloc1(numFound,&found);CHKERRQ(ierr);
6213a93e3b7SToby Isaac     for (p = 0, numFound = 0; p < numPoints; p++) {
6223a93e3b7SToby Isaac       if (cells[p].rank >= 0 && cells[p].index >= 0) {
6233a93e3b7SToby Isaac         if (numFound < p) {
6243a93e3b7SToby Isaac           cells[numFound] = cells[p];
6253a93e3b7SToby Isaac         }
6263a93e3b7SToby Isaac         found[numFound++] = p;
6273a93e3b7SToby Isaac       }
6283a93e3b7SToby Isaac     }
6293a93e3b7SToby Isaac   }
63062a38674SMatthew G. Knepley   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
6313a93e3b7SToby Isaac   ierr = PetscSFSetGraph(cellSF, cEnd - cStart, numFound, found, PETSC_OWN_POINTER, cells, PETSC_OWN_POINTER);CHKERRQ(ierr);
632ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
633ccd2543fSMatthew G Knepley }
634ccd2543fSMatthew G Knepley 
635741bfc07SMatthew G. Knepley /*@C
636741bfc07SMatthew G. Knepley   DMPlexComputeProjection2Dto1D - Rewrite coordinates to be the 1D projection of the 2D coordinates
637741bfc07SMatthew G. Knepley 
638741bfc07SMatthew G. Knepley   Not collective
639741bfc07SMatthew G. Knepley 
640741bfc07SMatthew G. Knepley   Input Parameter:
641741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
642741bfc07SMatthew G. Knepley 
643741bfc07SMatthew G. Knepley   Output Parameters:
644741bfc07SMatthew G. Knepley + coords - The new y-coordinate, and 0 for x
645741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
646741bfc07SMatthew G. Knepley 
647741bfc07SMatthew G. Knepley   Level: developer
648741bfc07SMatthew G. Knepley 
649741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection3Dto1D(), DMPlexComputeProjection3Dto2D()
650741bfc07SMatthew G. Knepley @*/
651741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection2Dto1D(PetscScalar coords[], PetscReal R[])
65217fe8556SMatthew G. Knepley {
65317fe8556SMatthew G. Knepley   const PetscReal x = PetscRealPart(coords[2] - coords[0]);
65417fe8556SMatthew G. Knepley   const PetscReal y = PetscRealPart(coords[3] - coords[1]);
6558b49ba18SBarry Smith   const PetscReal r = PetscSqrtReal(x*x + y*y), c = x/r, s = y/r;
65617fe8556SMatthew G. Knepley 
65717fe8556SMatthew G. Knepley   PetscFunctionBegin;
6581c99cf0cSGeoffrey Irving   R[0] = c; R[1] = -s;
6591c99cf0cSGeoffrey Irving   R[2] = s; R[3] =  c;
66017fe8556SMatthew G. Knepley   coords[0] = 0.0;
6617f07f362SMatthew G. Knepley   coords[1] = r;
66217fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
66317fe8556SMatthew G. Knepley }
66417fe8556SMatthew G. Knepley 
665741bfc07SMatthew G. Knepley /*@C
666741bfc07SMatthew G. Knepley   DMPlexComputeProjection3Dto1D - Rewrite coordinates to be the 1D projection of the 3D coordinates
66728dbe442SToby Isaac 
668741bfc07SMatthew G. Knepley   Not collective
66928dbe442SToby Isaac 
670741bfc07SMatthew G. Knepley   Input Parameter:
671741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
672741bfc07SMatthew G. Knepley 
673741bfc07SMatthew G. Knepley   Output Parameters:
674741bfc07SMatthew G. Knepley + coords - The new y-coordinate, and 0 for x and z
675741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
676741bfc07SMatthew G. Knepley 
677741bfc07SMatthew 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
678741bfc07SMatthew G. Knepley 
679741bfc07SMatthew G. Knepley   Level: developer
680741bfc07SMatthew G. Knepley 
681741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto2D()
682741bfc07SMatthew G. Knepley @*/
683741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto1D(PetscScalar coords[], PetscReal R[])
68428dbe442SToby Isaac {
68528dbe442SToby Isaac   PetscReal      x    = PetscRealPart(coords[3] - coords[0]);
68628dbe442SToby Isaac   PetscReal      y    = PetscRealPart(coords[4] - coords[1]);
68728dbe442SToby Isaac   PetscReal      z    = PetscRealPart(coords[5] - coords[2]);
68828dbe442SToby Isaac   PetscReal      r    = PetscSqrtReal(x*x + y*y + z*z);
68928dbe442SToby Isaac   PetscReal      rinv = 1. / r;
69028dbe442SToby Isaac   PetscFunctionBegin;
69128dbe442SToby Isaac 
69228dbe442SToby Isaac   x *= rinv; y *= rinv; z *= rinv;
69328dbe442SToby Isaac   if (x > 0.) {
69428dbe442SToby Isaac     PetscReal inv1pX   = 1./ (1. + x);
69528dbe442SToby Isaac 
69628dbe442SToby Isaac     R[0] = x; R[1] = -y;              R[2] = -z;
69728dbe442SToby Isaac     R[3] = y; R[4] = 1. - y*y*inv1pX; R[5] =     -y*z*inv1pX;
69828dbe442SToby Isaac     R[6] = z; R[7] =     -y*z*inv1pX; R[8] = 1. - z*z*inv1pX;
69928dbe442SToby Isaac   }
70028dbe442SToby Isaac   else {
70128dbe442SToby Isaac     PetscReal inv1mX   = 1./ (1. - x);
70228dbe442SToby Isaac 
70328dbe442SToby Isaac     R[0] = x; R[1] = z;               R[2] = y;
70428dbe442SToby Isaac     R[3] = y; R[4] =     -y*z*inv1mX; R[5] = 1. - y*y*inv1mX;
70528dbe442SToby Isaac     R[6] = z; R[7] = 1. - z*z*inv1mX; R[8] =     -y*z*inv1mX;
70628dbe442SToby Isaac   }
70728dbe442SToby Isaac   coords[0] = 0.0;
70828dbe442SToby Isaac   coords[1] = r;
70928dbe442SToby Isaac   PetscFunctionReturn(0);
71028dbe442SToby Isaac }
71128dbe442SToby Isaac 
712741bfc07SMatthew G. Knepley /*@
713741bfc07SMatthew G. Knepley   DMPlexComputeProjection3Dto2D - Rewrite coordinates to be the 2D projection of the 3D coordinates
714741bfc07SMatthew G. Knepley 
715741bfc07SMatthew G. Knepley   Not collective
716741bfc07SMatthew G. Knepley 
717741bfc07SMatthew G. Knepley   Input Parameter:
718741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
719741bfc07SMatthew G. Knepley 
720741bfc07SMatthew G. Knepley   Output Parameters:
721741bfc07SMatthew G. Knepley + coords - The new y- and z-coordinates, and 0 for x
722741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
723741bfc07SMatthew G. Knepley 
724741bfc07SMatthew G. Knepley   Level: developer
725741bfc07SMatthew G. Knepley 
726741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto1D()
727741bfc07SMatthew G. Knepley @*/
728741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto2D(PetscInt coordSize, PetscScalar coords[], PetscReal R[])
729ccd2543fSMatthew G Knepley {
7301ee9d5ecSMatthew G. Knepley   PetscReal      x1[3],  x2[3], n[3], norm;
73199dec3a6SMatthew G. Knepley   PetscReal      x1p[3], x2p[3], xnp[3];
7324a217a95SMatthew G. Knepley   PetscReal      sqrtz, alpha;
733ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
73499dec3a6SMatthew G. Knepley   PetscInt       d, e, p;
735ccd2543fSMatthew G Knepley 
736ccd2543fSMatthew G Knepley   PetscFunctionBegin;
737ccd2543fSMatthew G Knepley   /* 0) Calculate normal vector */
738ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
7391ee9d5ecSMatthew G. Knepley     x1[d] = PetscRealPart(coords[1*dim+d] - coords[0*dim+d]);
7401ee9d5ecSMatthew G. Knepley     x2[d] = PetscRealPart(coords[2*dim+d] - coords[0*dim+d]);
741ccd2543fSMatthew G Knepley   }
742ccd2543fSMatthew G Knepley   n[0] = x1[1]*x2[2] - x1[2]*x2[1];
743ccd2543fSMatthew G Knepley   n[1] = x1[2]*x2[0] - x1[0]*x2[2];
744ccd2543fSMatthew G Knepley   n[2] = x1[0]*x2[1] - x1[1]*x2[0];
7458b49ba18SBarry Smith   norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
746ccd2543fSMatthew G Knepley   n[0] /= norm;
747ccd2543fSMatthew G Knepley   n[1] /= norm;
748ccd2543fSMatthew G Knepley   n[2] /= norm;
749ccd2543fSMatthew G Knepley   /* 1) Take the normal vector and rotate until it is \hat z
750ccd2543fSMatthew G Knepley 
751ccd2543fSMatthew G Knepley     Let the normal vector be <nx, ny, nz> and alpha = 1/sqrt(1 - nz^2), then
752ccd2543fSMatthew G Knepley 
753ccd2543fSMatthew G Knepley     R = /  alpha nx nz  alpha ny nz -1/alpha \
754ccd2543fSMatthew G Knepley         | -alpha ny     alpha nx        0    |
755ccd2543fSMatthew G Knepley         \     nx            ny         nz    /
756ccd2543fSMatthew G Knepley 
757ccd2543fSMatthew G Knepley     will rotate the normal vector to \hat z
758ccd2543fSMatthew G Knepley   */
7598b49ba18SBarry Smith   sqrtz = PetscSqrtReal(1.0 - n[2]*n[2]);
76073868372SMatthew G. Knepley   /* Check for n = z */
76173868372SMatthew G. Knepley   if (sqrtz < 1.0e-10) {
7627df32b8bSSanderA     const PetscInt s = PetscSign(n[2]);
7637df32b8bSSanderA     /* If nz < 0, rotate 180 degrees around x-axis */
76499dec3a6SMatthew G. Knepley     for (p = 3; p < coordSize/3; ++p) {
76599dec3a6SMatthew G. Knepley       coords[p*2+0] = PetscRealPart(coords[p*dim+0] - coords[0*dim+0]);
7667df32b8bSSanderA       coords[p*2+1] = (PetscRealPart(coords[p*dim+1] - coords[0*dim+1])) * s;
76773868372SMatthew G. Knepley     }
76899dec3a6SMatthew G. Knepley     coords[0] = 0.0;
76999dec3a6SMatthew G. Knepley     coords[1] = 0.0;
7707df32b8bSSanderA     coords[2] = x1[0];
7717df32b8bSSanderA     coords[3] = x1[1] * s;
7727df32b8bSSanderA     coords[4] = x2[0];
7737df32b8bSSanderA     coords[5] = x2[1] * s;
7747df32b8bSSanderA     R[0] = 1.0;     R[1] = 0.0;     R[2] = 0.0;
7757df32b8bSSanderA     R[3] = 0.0;     R[4] = 1.0 * s; R[5] = 0.0;
7767df32b8bSSanderA     R[6] = 0.0;     R[7] = 0.0;     R[8] = 1.0 * s;
77773868372SMatthew G. Knepley     PetscFunctionReturn(0);
77873868372SMatthew G. Knepley   }
779da18b5e6SMatthew G Knepley   alpha = 1.0/sqrtz;
780ccd2543fSMatthew G Knepley   R[0] =  alpha*n[0]*n[2]; R[1] = alpha*n[1]*n[2]; R[2] = -sqrtz;
781ccd2543fSMatthew G Knepley   R[3] = -alpha*n[1];      R[4] = alpha*n[0];      R[5] = 0.0;
782ccd2543fSMatthew G Knepley   R[6] =  n[0];            R[7] = n[1];            R[8] = n[2];
783ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
784ccd2543fSMatthew G Knepley     x1p[d] = 0.0;
785ccd2543fSMatthew G Knepley     x2p[d] = 0.0;
786ccd2543fSMatthew G Knepley     for (e = 0; e < dim; ++e) {
787ccd2543fSMatthew G Knepley       x1p[d] += R[d*dim+e]*x1[e];
788ccd2543fSMatthew G Knepley       x2p[d] += R[d*dim+e]*x2[e];
789ccd2543fSMatthew G Knepley     }
790ccd2543fSMatthew G Knepley   }
79148120919SToby Isaac   if (PetscAbsReal(x1p[2]) > 10. * PETSC_SMALL) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
79248120919SToby Isaac   if (PetscAbsReal(x2p[2]) > 10. * PETSC_SMALL) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
793ccd2543fSMatthew G Knepley   /* 2) Project to (x, y) */
79499dec3a6SMatthew G. Knepley   for (p = 3; p < coordSize/3; ++p) {
79599dec3a6SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
79699dec3a6SMatthew G. Knepley       xnp[d] = 0.0;
79799dec3a6SMatthew G. Knepley       for (e = 0; e < dim; ++e) {
79899dec3a6SMatthew G. Knepley         xnp[d] += R[d*dim+e]*PetscRealPart(coords[p*dim+e] - coords[0*dim+e]);
79999dec3a6SMatthew G. Knepley       }
80099dec3a6SMatthew G. Knepley       if (d < dim-1) coords[p*2+d] = xnp[d];
80199dec3a6SMatthew G. Knepley     }
80299dec3a6SMatthew G. Knepley   }
803ccd2543fSMatthew G Knepley   coords[0] = 0.0;
804ccd2543fSMatthew G Knepley   coords[1] = 0.0;
805ccd2543fSMatthew G Knepley   coords[2] = x1p[0];
806ccd2543fSMatthew G Knepley   coords[3] = x1p[1];
807ccd2543fSMatthew G Knepley   coords[4] = x2p[0];
808ccd2543fSMatthew G Knepley   coords[5] = x2p[1];
8097f07f362SMatthew G. Knepley   /* Output R^T which rotates \hat z to the input normal */
8107f07f362SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
8117f07f362SMatthew G. Knepley     for (e = d+1; e < dim; ++e) {
8127f07f362SMatthew G. Knepley       PetscReal tmp;
8137f07f362SMatthew G. Knepley 
8147f07f362SMatthew G. Knepley       tmp        = R[d*dim+e];
8157f07f362SMatthew G. Knepley       R[d*dim+e] = R[e*dim+d];
8167f07f362SMatthew G. Knepley       R[e*dim+d] = tmp;
8177f07f362SMatthew G. Knepley     }
8187f07f362SMatthew G. Knepley   }
819ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
820ccd2543fSMatthew G Knepley }
821ccd2543fSMatthew G Knepley 
8226322fe33SJed Brown PETSC_UNUSED
823834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[])
824834e62ceSMatthew G. Knepley {
825834e62ceSMatthew G. Knepley   /* Signed volume is 1/2 the determinant
826834e62ceSMatthew G. Knepley 
827834e62ceSMatthew G. Knepley    |  1  1  1 |
828834e62ceSMatthew G. Knepley    | x0 x1 x2 |
829834e62ceSMatthew G. Knepley    | y0 y1 y2 |
830834e62ceSMatthew G. Knepley 
831834e62ceSMatthew G. Knepley      but if x0,y0 is the origin, we have
832834e62ceSMatthew G. Knepley 
833834e62ceSMatthew G. Knepley    | x1 x2 |
834834e62ceSMatthew G. Knepley    | y1 y2 |
835834e62ceSMatthew G. Knepley   */
836834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1];
837834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1];
838834e62ceSMatthew G. Knepley   PetscReal       M[4], detM;
839834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2;
84086623015SMatthew G. Knepley   M[2] = y1; M[3] = y2;
841923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(&detM, M);
842834e62ceSMatthew G. Knepley   *vol = 0.5*detM;
8433bc0b13bSBarry Smith   (void)PetscLogFlops(5.0);
844834e62ceSMatthew G. Knepley }
845834e62ceSMatthew G. Knepley 
846834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Origin_Internal(PetscReal *vol, PetscReal coords[])
847834e62ceSMatthew G. Knepley {
848923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(vol, coords);
849834e62ceSMatthew G. Knepley   *vol *= 0.5;
850834e62ceSMatthew G. Knepley }
851834e62ceSMatthew G. Knepley 
8526322fe33SJed Brown PETSC_UNUSED
853834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[])
854834e62ceSMatthew G. Knepley {
855834e62ceSMatthew G. Knepley   /* Signed volume is 1/6th of the determinant
856834e62ceSMatthew G. Knepley 
857834e62ceSMatthew G. Knepley    |  1  1  1  1 |
858834e62ceSMatthew G. Knepley    | x0 x1 x2 x3 |
859834e62ceSMatthew G. Knepley    | y0 y1 y2 y3 |
860834e62ceSMatthew G. Knepley    | z0 z1 z2 z3 |
861834e62ceSMatthew G. Knepley 
862834e62ceSMatthew G. Knepley      but if x0,y0,z0 is the origin, we have
863834e62ceSMatthew G. Knepley 
864834e62ceSMatthew G. Knepley    | x1 x2 x3 |
865834e62ceSMatthew G. Knepley    | y1 y2 y3 |
866834e62ceSMatthew G. Knepley    | z1 z2 z3 |
867834e62ceSMatthew G. Knepley   */
868834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[3] - coords[0], y1 = coords[4]  - coords[1], z1 = coords[5]  - coords[2];
869834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[6] - coords[0], y2 = coords[7]  - coords[1], z2 = coords[8]  - coords[2];
870834e62ceSMatthew G. Knepley   const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2];
871*0a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1./(PetscReal)6.);
872834e62ceSMatthew G. Knepley   PetscReal       M[9], detM;
873834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2; M[2] = x3;
874834e62ceSMatthew G. Knepley   M[3] = y1; M[4] = y2; M[5] = y3;
875834e62ceSMatthew G. Knepley   M[6] = z1; M[7] = z2; M[8] = z3;
876923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(&detM, M);
877*0a3da2c2SToby Isaac   *vol = -onesixth*detM;
8783bc0b13bSBarry Smith   (void)PetscLogFlops(10.0);
879834e62ceSMatthew G. Knepley }
880834e62ceSMatthew G. Knepley 
8810ec8681fSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[])
8820ec8681fSMatthew G. Knepley {
883*0a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1./(PetscReal)6.);
884923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(vol, coords);
885*0a3da2c2SToby Isaac   *vol *= -onesixth;
8860ec8681fSMatthew G. Knepley }
8870ec8681fSMatthew G. Knepley 
888cb92db44SToby Isaac static PetscErrorCode DMPlexComputePointGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
889cb92db44SToby Isaac {
890cb92db44SToby Isaac   PetscSection   coordSection;
891cb92db44SToby Isaac   Vec            coordinates;
892cb92db44SToby Isaac   const PetscScalar *coords;
893cb92db44SToby Isaac   PetscInt       dim, d, off;
894cb92db44SToby Isaac   PetscErrorCode ierr;
895cb92db44SToby Isaac 
896cb92db44SToby Isaac   PetscFunctionBegin;
897cb92db44SToby Isaac   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
898cb92db44SToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
899cb92db44SToby Isaac   ierr = PetscSectionGetDof(coordSection,e,&dim);CHKERRQ(ierr);
900cb92db44SToby Isaac   if (!dim) PetscFunctionReturn(0);
901cb92db44SToby Isaac   ierr = PetscSectionGetOffset(coordSection,e,&off);CHKERRQ(ierr);
902cb92db44SToby Isaac   ierr = VecGetArrayRead(coordinates,&coords);CHKERRQ(ierr);
903cb92db44SToby Isaac   if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[off + d]);}
904cb92db44SToby Isaac   ierr = VecRestoreArrayRead(coordinates,&coords);CHKERRQ(ierr);
905cb92db44SToby Isaac   *detJ = 1.;
906cb92db44SToby Isaac   if (J) {
907cb92db44SToby Isaac     for (d = 0; d < dim * dim; d++) J[d] = 0.;
908cb92db44SToby Isaac     for (d = 0; d < dim; d++) J[d * dim + d] = 1.;
909cb92db44SToby Isaac     if (invJ) {
910cb92db44SToby Isaac       for (d = 0; d < dim * dim; d++) invJ[d] = 0.;
911cb92db44SToby Isaac       for (d = 0; d < dim; d++) invJ[d * dim + d] = 1.;
912cb92db44SToby Isaac     }
913cb92db44SToby Isaac   }
914cb92db44SToby Isaac   PetscFunctionReturn(0);
915cb92db44SToby Isaac }
916cb92db44SToby Isaac 
91717fe8556SMatthew G. Knepley static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
91817fe8556SMatthew G. Knepley {
91917fe8556SMatthew G. Knepley   PetscSection   coordSection;
92017fe8556SMatthew G. Knepley   Vec            coordinates;
921a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
9228bf5c034SToby Isaac   PetscInt       numCoords, d, pStart, pEnd, numSelfCoords = 0;
92317fe8556SMatthew G. Knepley   PetscErrorCode ierr;
92417fe8556SMatthew G. Knepley 
92517fe8556SMatthew G. Knepley   PetscFunctionBegin;
92617fe8556SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
92769d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
9288bf5c034SToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
9298bf5c034SToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
93017fe8556SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
9318bf5c034SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
932adac9986SMatthew G. Knepley   if (invJ && !J) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
9337f07f362SMatthew G. Knepley   *detJ = 0.0;
93428dbe442SToby Isaac   if (numCoords == 6) {
93528dbe442SToby Isaac     const PetscInt dim = 3;
93628dbe442SToby Isaac     PetscReal      R[9], J0;
93728dbe442SToby Isaac 
93828dbe442SToby Isaac     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
939741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto1D(coords, R);CHKERRQ(ierr);
94028dbe442SToby Isaac     if (J)    {
94128dbe442SToby Isaac       J0   = 0.5*PetscRealPart(coords[1]);
94228dbe442SToby Isaac       J[0] = R[0]*J0; J[1] = R[1]; J[2] = R[2];
94328dbe442SToby Isaac       J[3] = R[3]*J0; J[4] = R[4]; J[5] = R[5];
94428dbe442SToby Isaac       J[6] = R[6]*J0; J[7] = R[7]; J[8] = R[8];
94528dbe442SToby Isaac       DMPlex_Det3D_Internal(detJ, J);
94628dbe442SToby Isaac       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
947adac9986SMatthew G. Knepley     }
94828dbe442SToby Isaac   } else if (numCoords == 4) {
9497f07f362SMatthew G. Knepley     const PetscInt dim = 2;
9507f07f362SMatthew G. Knepley     PetscReal      R[4], J0;
9517f07f362SMatthew G. Knepley 
9527f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
953741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection2Dto1D(coords, R);CHKERRQ(ierr);
95417fe8556SMatthew G. Knepley     if (J)    {
9557f07f362SMatthew G. Knepley       J0   = 0.5*PetscRealPart(coords[1]);
9567f07f362SMatthew G. Knepley       J[0] = R[0]*J0; J[1] = R[1];
9577f07f362SMatthew G. Knepley       J[2] = R[2]*J0; J[3] = R[3];
958923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
959923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
960adac9986SMatthew G. Knepley     }
9617f07f362SMatthew G. Knepley   } else if (numCoords == 2) {
9627f07f362SMatthew G. Knepley     const PetscInt dim = 1;
9637f07f362SMatthew G. Knepley 
9647f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
9657f07f362SMatthew G. Knepley     if (J)    {
9667f07f362SMatthew G. Knepley       J[0]  = 0.5*(PetscRealPart(coords[1]) - PetscRealPart(coords[0]));
96717fe8556SMatthew G. Knepley       *detJ = J[0];
9683bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0);CHKERRQ(ierr);
9693bc0b13bSBarry Smith       if (invJ) {invJ[0] = 1.0/J[0]; ierr = PetscLogFlops(1.0);CHKERRQ(ierr);}
970adac9986SMatthew G. Knepley     }
971796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this segment is %D != 2", numCoords);
97217fe8556SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
97317fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
97417fe8556SMatthew G. Knepley }
97517fe8556SMatthew G. Knepley 
976ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
977ccd2543fSMatthew G Knepley {
978ccd2543fSMatthew G Knepley   PetscSection   coordSection;
979ccd2543fSMatthew G Knepley   Vec            coordinates;
980a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
9817f07f362SMatthew G. Knepley   PetscInt       numCoords, d, f, g;
982ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
983ccd2543fSMatthew G Knepley 
984ccd2543fSMatthew G Knepley   PetscFunctionBegin;
985ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
98669d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
987ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
9887f07f362SMatthew G. Knepley   *detJ = 0.0;
989ccd2543fSMatthew G Knepley   if (numCoords == 9) {
9907f07f362SMatthew G. Knepley     const PetscInt dim = 3;
9917f07f362SMatthew 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};
9927f07f362SMatthew G. Knepley 
9937f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
994741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
9957f07f362SMatthew G. Knepley     if (J)    {
996b7ad821dSMatthew G. Knepley       const PetscInt pdim = 2;
997b7ad821dSMatthew G. Knepley 
998b7ad821dSMatthew G. Knepley       for (d = 0; d < pdim; d++) {
999b7ad821dSMatthew G. Knepley         for (f = 0; f < pdim; f++) {
1000b7ad821dSMatthew G. Knepley           J0[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
1001ccd2543fSMatthew G Knepley         }
10027f07f362SMatthew G. Knepley       }
10033bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1004923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
10057f07f362SMatthew G. Knepley       for (d = 0; d < dim; d++) {
10067f07f362SMatthew G. Knepley         for (f = 0; f < dim; f++) {
10077f07f362SMatthew G. Knepley           J[d*dim+f] = 0.0;
10087f07f362SMatthew G. Knepley           for (g = 0; g < dim; g++) {
10097f07f362SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
10107f07f362SMatthew G. Knepley           }
10117f07f362SMatthew G. Knepley         }
10127f07f362SMatthew G. Knepley       }
10133bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
10147f07f362SMatthew G. Knepley     }
1015923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
10167f07f362SMatthew G. Knepley   } else if (numCoords == 6) {
10177f07f362SMatthew G. Knepley     const PetscInt dim = 2;
10187f07f362SMatthew G. Knepley 
10197f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1020ccd2543fSMatthew G Knepley     if (J)    {
1021ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1022ccd2543fSMatthew G Knepley         for (f = 0; f < dim; f++) {
1023ccd2543fSMatthew G Knepley           J[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*dim+d]) - PetscRealPart(coords[0*dim+d]));
1024ccd2543fSMatthew G Knepley         }
1025ccd2543fSMatthew G Knepley       }
10263bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1027923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1028ccd2543fSMatthew G Knepley     }
1029923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1030796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %D != 6 or 9", numCoords);
1031ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1032ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1033ccd2543fSMatthew G Knepley }
1034ccd2543fSMatthew G Knepley 
1035dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1036ccd2543fSMatthew G Knepley {
1037ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1038ccd2543fSMatthew G Knepley   Vec            coordinates;
1039a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
10400d29256aSToby Isaac   PetscInt       numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd;
1041ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1042ccd2543fSMatthew G Knepley 
1043ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1044ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
104569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
10460d29256aSToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
10470d29256aSToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
104899dec3a6SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
104971f58de1SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
1050dfccc68fSToby Isaac   if (!Nq) {
10517f07f362SMatthew G. Knepley     *detJ = 0.0;
105299dec3a6SMatthew G. Knepley     if (numCoords == 12) {
105399dec3a6SMatthew G. Knepley       const PetscInt dim = 3;
105499dec3a6SMatthew 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};
105599dec3a6SMatthew G. Knepley 
1056dfccc68fSToby Isaac       if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1057741bfc07SMatthew G. Knepley       ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
105899dec3a6SMatthew G. Knepley       if (J)    {
105999dec3a6SMatthew G. Knepley         const PetscInt pdim = 2;
106099dec3a6SMatthew G. Knepley 
106199dec3a6SMatthew G. Knepley         for (d = 0; d < pdim; d++) {
106299dec3a6SMatthew G. Knepley           J0[d*dim+0] = 0.5*(PetscRealPart(coords[1*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
106399dec3a6SMatthew G. Knepley           J0[d*dim+1] = 0.5*(PetscRealPart(coords[3*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
106499dec3a6SMatthew G. Knepley         }
10653bc0b13bSBarry Smith         ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1066923591dfSMatthew G. Knepley         DMPlex_Det3D_Internal(detJ, J0);
106799dec3a6SMatthew G. Knepley         for (d = 0; d < dim; d++) {
106899dec3a6SMatthew G. Knepley           for (f = 0; f < dim; f++) {
106999dec3a6SMatthew G. Knepley             J[d*dim+f] = 0.0;
107099dec3a6SMatthew G. Knepley             for (g = 0; g < dim; g++) {
107199dec3a6SMatthew G. Knepley               J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
107299dec3a6SMatthew G. Knepley             }
107399dec3a6SMatthew G. Knepley           }
107499dec3a6SMatthew G. Knepley         }
10753bc0b13bSBarry Smith         ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
107699dec3a6SMatthew G. Knepley       }
1077923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
107871f58de1SToby Isaac     } else if (numCoords == 8) {
107999dec3a6SMatthew G. Knepley       const PetscInt dim = 2;
108099dec3a6SMatthew G. Knepley 
1081dfccc68fSToby Isaac       if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1082ccd2543fSMatthew G Knepley       if (J)    {
1083ccd2543fSMatthew G Knepley         for (d = 0; d < dim; d++) {
108499dec3a6SMatthew G. Knepley           J[d*dim+0] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
108599dec3a6SMatthew G. Knepley           J[d*dim+1] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1086ccd2543fSMatthew G Knepley         }
10873bc0b13bSBarry Smith         ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1088923591dfSMatthew G. Knepley         DMPlex_Det2D_Internal(detJ, J);
1089ccd2543fSMatthew G Knepley       }
1090923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1091796f034aSJed Brown     } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
1092dfccc68fSToby Isaac   } else {
1093dfccc68fSToby Isaac     const PetscInt Nv = 4;
1094dfccc68fSToby Isaac     const PetscInt dimR = 2;
1095dfccc68fSToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
1096dfccc68fSToby Isaac     PetscReal zOrder[12];
1097dfccc68fSToby Isaac     PetscReal zCoeff[12];
1098dfccc68fSToby Isaac     PetscInt  i, j, k, l, dim;
1099dfccc68fSToby Isaac 
1100dfccc68fSToby Isaac     if (numCoords == 12) {
1101dfccc68fSToby Isaac       dim = 3;
1102dfccc68fSToby Isaac     } else if (numCoords == 8) {
1103dfccc68fSToby Isaac       dim = 2;
1104dfccc68fSToby Isaac     } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
1105dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1106dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1107dfccc68fSToby Isaac 
1108dfccc68fSToby Isaac       for (j = 0; j < dim; j++) {
1109dfccc68fSToby Isaac         zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1110dfccc68fSToby Isaac       }
1111dfccc68fSToby Isaac     }
1112dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1113dfccc68fSToby Isaac       zCoeff[dim * 0 + j] = 0.25 * (  zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1114dfccc68fSToby Isaac       zCoeff[dim * 1 + j] = 0.25 * (- zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1115dfccc68fSToby Isaac       zCoeff[dim * 2 + j] = 0.25 * (- zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1116dfccc68fSToby Isaac       zCoeff[dim * 3 + j] = 0.25 * (  zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1117dfccc68fSToby Isaac     }
1118dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1119dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1];
1120dfccc68fSToby Isaac 
1121dfccc68fSToby Isaac       if (v) {
1122dfccc68fSToby Isaac         PetscReal extPoint[4];
1123dfccc68fSToby Isaac 
1124dfccc68fSToby Isaac         extPoint[0] = 1.;
1125dfccc68fSToby Isaac         extPoint[1] = xi;
1126dfccc68fSToby Isaac         extPoint[2] = eta;
1127dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1128dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1129dfccc68fSToby Isaac           PetscReal val = 0.;
1130dfccc68fSToby Isaac 
1131dfccc68fSToby Isaac           for (k = 0; k < Nv; k++) {
1132dfccc68fSToby Isaac             val += extPoint[k] * zCoeff[dim * k + j];
1133dfccc68fSToby Isaac           }
1134dfccc68fSToby Isaac           v[i * dim + j] = val;
1135dfccc68fSToby Isaac         }
1136dfccc68fSToby Isaac       }
1137dfccc68fSToby Isaac       if (J) {
1138dfccc68fSToby Isaac         PetscReal extJ[8];
1139dfccc68fSToby Isaac 
1140dfccc68fSToby Isaac         extJ[0] = 0.;
1141dfccc68fSToby Isaac         extJ[1] = 0.;
1142dfccc68fSToby Isaac         extJ[2] = 1.;
1143dfccc68fSToby Isaac         extJ[3] = 0.;
1144dfccc68fSToby Isaac         extJ[4] = 0.;
1145dfccc68fSToby Isaac         extJ[5] = 1.;
1146dfccc68fSToby Isaac         extJ[6] = eta;
1147dfccc68fSToby Isaac         extJ[7] = xi;
1148dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1149dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1150dfccc68fSToby Isaac             PetscReal val = 0.;
1151dfccc68fSToby Isaac 
1152dfccc68fSToby Isaac             for (l = 0; l < Nv; l++) {
1153dfccc68fSToby Isaac               val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1154dfccc68fSToby Isaac             }
1155dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1156dfccc68fSToby Isaac           }
1157dfccc68fSToby Isaac         }
1158dfccc68fSToby Isaac         if (dim == 3) { /* put the cross product in the third component of the Jacobian */
1159dfccc68fSToby Isaac           PetscReal x, y, z;
1160dfccc68fSToby Isaac           PetscReal *iJ = &J[i * dim * dim];
1161dfccc68fSToby Isaac           PetscReal norm;
1162dfccc68fSToby Isaac 
1163dfccc68fSToby Isaac           x = iJ[1 * dim + 0] * iJ[2 * dim + 1] - iJ[1 * dim + 1] * iJ[2 * dim + 0];
1164dfccc68fSToby Isaac           y = iJ[0 * dim + 1] * iJ[2 * dim + 0] - iJ[0 * dim + 0] * iJ[2 * dim + 1];
1165dfccc68fSToby Isaac           z = iJ[0 * dim + 0] * iJ[1 * dim + 1] - iJ[0 * dim + 1] * iJ[1 * dim + 0];
1166dfccc68fSToby Isaac           norm = PetscSqrtReal(x * x + y * y + z * z);
1167dfccc68fSToby Isaac           iJ[2] = x / norm;
1168dfccc68fSToby Isaac           iJ[5] = y / norm;
1169dfccc68fSToby Isaac           iJ[8] = z / norm;
1170dfccc68fSToby Isaac           DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1171dfccc68fSToby Isaac           if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1172dfccc68fSToby Isaac         } else {
1173dfccc68fSToby Isaac           DMPlex_Det2D_Internal(&detJ[i], &J[i * dim * dim]);
1174dfccc68fSToby Isaac           if (invJ) {DMPlex_Invert2D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1175dfccc68fSToby Isaac         }
1176dfccc68fSToby Isaac       }
1177dfccc68fSToby Isaac     }
1178dfccc68fSToby Isaac   }
117999dec3a6SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1180ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1181ccd2543fSMatthew G Knepley }
1182ccd2543fSMatthew G Knepley 
1183ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1184ccd2543fSMatthew G Knepley {
1185ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1186ccd2543fSMatthew G Knepley   Vec            coordinates;
1187a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1188ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
118999dec3a6SMatthew G. Knepley   PetscInt       d;
1190ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1191ccd2543fSMatthew G Knepley 
1192ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1193ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
119469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1195ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
11967f07f362SMatthew G. Knepley   *detJ = 0.0;
11977f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1198ccd2543fSMatthew G Knepley   if (J)    {
1199ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1200f0df753eSMatthew G. Knepley       /* I orient with outward face normals */
1201f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d]));
1202f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1203f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1204ccd2543fSMatthew G Knepley     }
12053bc0b13bSBarry Smith     ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1206923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1207ccd2543fSMatthew G Knepley   }
1208923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1209ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1210ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1211ccd2543fSMatthew G Knepley }
1212ccd2543fSMatthew G Knepley 
1213dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1214ccd2543fSMatthew G Knepley {
1215ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1216ccd2543fSMatthew G Knepley   Vec            coordinates;
1217a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1218ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
1219ccd2543fSMatthew G Knepley   PetscInt       d;
1220ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1221ccd2543fSMatthew G Knepley 
1222ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1223ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
122469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1225ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1226dfccc68fSToby Isaac   if (!Nq) {
12277f07f362SMatthew G. Knepley     *detJ = 0.0;
1228dfccc68fSToby Isaac     if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1229ccd2543fSMatthew G Knepley     if (J)    {
1230ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1231f0df753eSMatthew G. Knepley         J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1232f0df753eSMatthew G. Knepley         J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1233f0df753eSMatthew G. Knepley         J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d]));
1234ccd2543fSMatthew G Knepley       }
12353bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1236923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J);
1237ccd2543fSMatthew G Knepley     }
1238923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1239dfccc68fSToby Isaac   } else {
1240dfccc68fSToby Isaac     const PetscInt Nv = 8;
1241dfccc68fSToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
1242dfccc68fSToby Isaac     const PetscInt dim = 3;
1243dfccc68fSToby Isaac     const PetscInt dimR = 3;
1244dfccc68fSToby Isaac     PetscReal zOrder[24];
1245dfccc68fSToby Isaac     PetscReal zCoeff[24];
1246dfccc68fSToby Isaac     PetscInt  i, j, k, l;
1247dfccc68fSToby Isaac 
1248dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1249dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1250dfccc68fSToby Isaac 
1251dfccc68fSToby Isaac       for (j = 0; j < dim; j++) {
1252dfccc68fSToby Isaac         zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1253dfccc68fSToby Isaac       }
1254dfccc68fSToby Isaac     }
1255dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1256dfccc68fSToby Isaac       zCoeff[dim * 0 + j] = 0.125 * (  zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j] + zOrder[dim * 4 + j] + zOrder[dim * 5 + j] + zOrder[dim * 6 + j] + zOrder[dim * 7 + j]);
1257dfccc68fSToby Isaac       zCoeff[dim * 1 + j] = 0.125 * (- zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j] - zOrder[dim * 4 + j] + zOrder[dim * 5 + j] - zOrder[dim * 6 + j] + zOrder[dim * 7 + j]);
1258dfccc68fSToby Isaac       zCoeff[dim * 2 + j] = 0.125 * (- zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j] - zOrder[dim * 4 + j] - zOrder[dim * 5 + j] + zOrder[dim * 6 + j] + zOrder[dim * 7 + j]);
1259dfccc68fSToby Isaac       zCoeff[dim * 3 + j] = 0.125 * (  zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j] + zOrder[dim * 4 + j] - zOrder[dim * 5 + j] - zOrder[dim * 6 + j] + zOrder[dim * 7 + j]);
1260dfccc68fSToby Isaac       zCoeff[dim * 4 + j] = 0.125 * (- zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] - zOrder[dim * 3 + j] + zOrder[dim * 4 + j] + zOrder[dim * 5 + j] + zOrder[dim * 6 + j] + zOrder[dim * 7 + j]);
1261dfccc68fSToby Isaac       zCoeff[dim * 5 + j] = 0.125 * (+ zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] - zOrder[dim * 3 + j] - zOrder[dim * 4 + j] + zOrder[dim * 5 + j] - zOrder[dim * 6 + j] + zOrder[dim * 7 + j]);
1262dfccc68fSToby Isaac       zCoeff[dim * 6 + j] = 0.125 * (+ zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] - zOrder[dim * 3 + j] - zOrder[dim * 4 + j] - zOrder[dim * 5 + j] + zOrder[dim * 6 + j] + zOrder[dim * 7 + j]);
1263dfccc68fSToby Isaac       zCoeff[dim * 7 + j] = 0.125 * (- zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] - zOrder[dim * 3 + j] + zOrder[dim * 4 + j] - zOrder[dim * 5 + j] - zOrder[dim * 6 + j] + zOrder[dim * 7 + j]);
1264dfccc68fSToby Isaac     }
1265dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1266dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], theta = points[dimR * i + 2];
1267dfccc68fSToby Isaac 
1268dfccc68fSToby Isaac       if (v) {
126991d2b7ceSToby Isaac         PetscReal extPoint[8];
1270dfccc68fSToby Isaac 
1271dfccc68fSToby Isaac         extPoint[0] = 1.;
1272dfccc68fSToby Isaac         extPoint[1] = xi;
1273dfccc68fSToby Isaac         extPoint[2] = eta;
1274dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1275dfccc68fSToby Isaac         extPoint[4] = theta;
1276dfccc68fSToby Isaac         extPoint[5] = theta * xi;
1277dfccc68fSToby Isaac         extPoint[6] = theta * eta;
1278dfccc68fSToby Isaac         extPoint[7] = theta * eta * xi;
1279dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1280dfccc68fSToby Isaac           PetscReal val = 0.;
1281dfccc68fSToby Isaac 
1282dfccc68fSToby Isaac           for (k = 0; k < Nv; k++) {
1283dfccc68fSToby Isaac             val += extPoint[k] * zCoeff[dim * k + j];
1284dfccc68fSToby Isaac           }
1285dfccc68fSToby Isaac           v[i * dim + j] = val;
1286dfccc68fSToby Isaac         }
1287dfccc68fSToby Isaac       }
1288dfccc68fSToby Isaac       if (J) {
1289dfccc68fSToby Isaac         PetscReal extJ[24];
1290dfccc68fSToby Isaac 
1291dfccc68fSToby Isaac         extJ[0]  = 0.         ; extJ[1]  = 0.        ; extJ[2]  = 0.      ;
1292dfccc68fSToby Isaac         extJ[3]  = 1.         ; extJ[4]  = 0.        ; extJ[5]  = 0.      ;
1293dfccc68fSToby Isaac         extJ[6]  = 0.         ; extJ[7]  = 1.        ; extJ[8]  = 0.      ;
1294dfccc68fSToby Isaac         extJ[9]  = eta        ; extJ[10] = xi        ; extJ[11] = 0.      ;
1295dfccc68fSToby Isaac         extJ[12] = 0.         ; extJ[13] = 0.        ; extJ[14] = 1.      ;
1296dfccc68fSToby Isaac         extJ[15] = theta      ; extJ[16] = 0.        ; extJ[17] = xi      ;
1297dfccc68fSToby Isaac         extJ[18] = 0.         ; extJ[19] = theta     ; extJ[20] = eta     ;
1298dfccc68fSToby Isaac         extJ[21] = theta * eta; extJ[22] = theta * xi; extJ[23] = eta * xi;
1299dfccc68fSToby Isaac 
1300dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1301dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1302dfccc68fSToby Isaac             PetscReal val = 0.;
1303dfccc68fSToby Isaac 
1304dfccc68fSToby Isaac             for (l = 0; l < Nv; l++) {
1305dfccc68fSToby Isaac               val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1306dfccc68fSToby Isaac             }
1307dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1308dfccc68fSToby Isaac           }
1309dfccc68fSToby Isaac         }
1310dfccc68fSToby Isaac         DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1311dfccc68fSToby Isaac         if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1312dfccc68fSToby Isaac       }
1313dfccc68fSToby Isaac     }
1314dfccc68fSToby Isaac   }
1315ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1316ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1317ccd2543fSMatthew G Knepley }
1318ccd2543fSMatthew G Knepley 
1319dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeCellGeometryFEM_Implicit(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1320dfccc68fSToby Isaac {
1321dfccc68fSToby Isaac   PetscInt        depth, dim, coordDim, coneSize, i;
1322dfccc68fSToby Isaac   PetscInt        Nq = 0;
1323dfccc68fSToby Isaac   const PetscReal *points = NULL;
1324dfccc68fSToby Isaac   DMLabel         depthLabel;
13257318780aSToby Isaac   PetscReal       v0[3] = {-1.}, J0[9] = {-1.}, detJ0 = -1.;
1326dfccc68fSToby Isaac   PetscBool       isAffine = PETSC_TRUE;
1327dfccc68fSToby Isaac   PetscErrorCode  ierr;
1328dfccc68fSToby Isaac 
1329dfccc68fSToby Isaac   PetscFunctionBegin;
1330dfccc68fSToby Isaac   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1331dfccc68fSToby Isaac   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
1332dfccc68fSToby Isaac   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1333dfccc68fSToby Isaac   ierr = DMLabelGetValue(depthLabel, cell, &dim);CHKERRQ(ierr);
1334dfccc68fSToby Isaac   if (depth == 1 && dim == 1) {
1335dfccc68fSToby Isaac     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1336dfccc68fSToby Isaac   }
1337dfccc68fSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
1338dfccc68fSToby Isaac   if (coordDim > 3) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported coordinate dimension %D > 3", coordDim);
13399c3cf19fSMatthew G. Knepley   if (quad) {ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, &points, NULL);CHKERRQ(ierr);}
1340dfccc68fSToby Isaac   switch (dim) {
1341dfccc68fSToby Isaac   case 0:
1342dfccc68fSToby Isaac     ierr = DMPlexComputePointGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
1343dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1344dfccc68fSToby Isaac     break;
1345dfccc68fSToby Isaac   case 1:
13467318780aSToby Isaac     if (Nq) {
13477318780aSToby Isaac       ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);
13487318780aSToby Isaac     } else {
13497318780aSToby Isaac       ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
13507318780aSToby Isaac     }
1351dfccc68fSToby Isaac     break;
1352dfccc68fSToby Isaac   case 2:
1353dfccc68fSToby Isaac     switch (coneSize) {
1354dfccc68fSToby Isaac     case 3:
13557318780aSToby Isaac       if (Nq) {
13567318780aSToby Isaac         ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);
13577318780aSToby Isaac       } else {
13587318780aSToby Isaac         ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
13597318780aSToby Isaac       }
1360dfccc68fSToby Isaac       break;
1361dfccc68fSToby Isaac     case 4:
1362dfccc68fSToby Isaac       ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1363dfccc68fSToby Isaac       isAffine = PETSC_FALSE;
1364dfccc68fSToby Isaac       break;
1365dfccc68fSToby Isaac     default:
1366dfccc68fSToby Isaac       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
1367dfccc68fSToby Isaac     }
1368dfccc68fSToby Isaac     break;
1369dfccc68fSToby Isaac   case 3:
1370dfccc68fSToby Isaac     switch (coneSize) {
1371dfccc68fSToby Isaac     case 4:
13727318780aSToby Isaac       if (Nq) {
13737318780aSToby Isaac         ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);
13747318780aSToby Isaac       } else {
13757318780aSToby Isaac         ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
13767318780aSToby Isaac       }
1377dfccc68fSToby Isaac       break;
1378dfccc68fSToby Isaac     case 6: /* Faces */
1379dfccc68fSToby Isaac     case 8: /* Vertices */
1380dfccc68fSToby Isaac       ierr = DMPlexComputeHexahedronGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1381dfccc68fSToby Isaac       isAffine = PETSC_FALSE;
1382dfccc68fSToby Isaac       break;
1383dfccc68fSToby Isaac     default:
1384dfccc68fSToby Isaac       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
1385dfccc68fSToby Isaac     }
1386dfccc68fSToby Isaac     break;
1387dfccc68fSToby Isaac   default:
1388dfccc68fSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
1389dfccc68fSToby Isaac   }
13907318780aSToby Isaac   if (isAffine && Nq) {
1391dfccc68fSToby Isaac     if (v) {
1392dfccc68fSToby Isaac       for (i = 0; i < Nq; i++) {
13937318780aSToby Isaac         CoordinatesRefToReal(coordDim,dim,v0, J0, &points[dim * i], &v[coordDim * i]);
1394dfccc68fSToby Isaac       }
1395dfccc68fSToby Isaac     }
13967318780aSToby Isaac     if (detJ) {
13977318780aSToby Isaac       for (i = 0; i < Nq; i++) {
13987318780aSToby Isaac         detJ[i] = detJ0;
1399dfccc68fSToby Isaac       }
14007318780aSToby Isaac     }
14017318780aSToby Isaac     if (J) {
14027318780aSToby Isaac       PetscInt k;
14037318780aSToby Isaac 
14047318780aSToby Isaac       for (i = 0, k = 0; i < Nq; i++) {
1405dfccc68fSToby Isaac         PetscInt j;
1406dfccc68fSToby Isaac 
14077318780aSToby Isaac         for (j = 0; j < coordDim * coordDim; j++, k++) {
14087318780aSToby Isaac           J[k] = J0[j];
14097318780aSToby Isaac         }
14107318780aSToby Isaac       }
14117318780aSToby Isaac     }
14127318780aSToby Isaac     if (invJ) {
14137318780aSToby Isaac       PetscInt k;
14147318780aSToby Isaac       switch (coordDim) {
14157318780aSToby Isaac       case 0:
14167318780aSToby Isaac         break;
14177318780aSToby Isaac       case 1:
14187318780aSToby Isaac         invJ[0] = 1./J0[0];
14197318780aSToby Isaac         break;
14207318780aSToby Isaac       case 2:
14217318780aSToby Isaac         DMPlex_Invert2D_Internal(invJ, J0, detJ0);
14227318780aSToby Isaac         break;
14237318780aSToby Isaac       case 3:
14247318780aSToby Isaac         DMPlex_Invert3D_Internal(invJ, J0, detJ0);
14257318780aSToby Isaac         break;
14267318780aSToby Isaac       }
14277318780aSToby Isaac       for (i = 1, k = coordDim * coordDim; i < Nq; i++) {
14287318780aSToby Isaac         PetscInt j;
14297318780aSToby Isaac 
14307318780aSToby Isaac         for (j = 0; j < coordDim * coordDim; j++, k++) {
14317318780aSToby Isaac           invJ[k] = invJ[j];
14327318780aSToby Isaac         }
1433dfccc68fSToby Isaac       }
1434dfccc68fSToby Isaac     }
1435dfccc68fSToby Isaac   }
1436dfccc68fSToby Isaac   PetscFunctionReturn(0);
1437dfccc68fSToby Isaac }
1438dfccc68fSToby Isaac 
1439ccd2543fSMatthew G Knepley /*@C
14408e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell
1441ccd2543fSMatthew G Knepley 
1442ccd2543fSMatthew G Knepley   Collective on DM
1443ccd2543fSMatthew G Knepley 
1444ccd2543fSMatthew G Knepley   Input Arguments:
1445ccd2543fSMatthew G Knepley + dm   - the DM
1446ccd2543fSMatthew G Knepley - cell - the cell
1447ccd2543fSMatthew G Knepley 
1448ccd2543fSMatthew G Knepley   Output Arguments:
1449ccd2543fSMatthew G Knepley + v0   - the translation part of this affine transform
1450ccd2543fSMatthew G Knepley . J    - the Jacobian of the transform from the reference element
1451ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian
1452ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant
1453ccd2543fSMatthew G Knepley 
1454ccd2543fSMatthew G Knepley   Level: advanced
1455ccd2543fSMatthew G Knepley 
1456ccd2543fSMatthew G Knepley   Fortran Notes:
1457ccd2543fSMatthew G Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1458ccd2543fSMatthew G Knepley   include petsc.h90 in your code.
1459ccd2543fSMatthew G Knepley 
14608e0841e0SMatthew G. Knepley .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinateVec()
1461ccd2543fSMatthew G Knepley @*/
14628e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1463ccd2543fSMatthew G Knepley {
1464ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1465ccd2543fSMatthew G Knepley 
1466ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1467dfccc68fSToby Isaac   ierr = DMPlexComputeCellGeometryFEM_Implicit(dm,cell,NULL,v0,J,invJ,detJ);CHKERRQ(ierr);
14688e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
14698e0841e0SMatthew G. Knepley }
14708e0841e0SMatthew G. Knepley 
1471dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeCellGeometryFEM_FE(DM dm, PetscFE fe, PetscInt point, PetscQuadrature quad, PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
14728e0841e0SMatthew G. Knepley {
1473dfccc68fSToby Isaac   PetscQuadrature  feQuad;
14748e0841e0SMatthew G. Knepley   PetscSection     coordSection;
14758e0841e0SMatthew G. Knepley   Vec              coordinates;
14768e0841e0SMatthew G. Knepley   PetscScalar     *coords = NULL;
14778e0841e0SMatthew G. Knepley   const PetscReal *quadPoints;
1478f960e424SToby Isaac   PetscReal       *basisDer, *basis, detJt;
1479f960e424SToby Isaac   PetscInt         dim, cdim, pdim, qdim, Nq, numCoords, q;
14808e0841e0SMatthew G. Knepley   PetscErrorCode   ierr;
14818e0841e0SMatthew G. Knepley 
14828e0841e0SMatthew G. Knepley   PetscFunctionBegin;
14838e0841e0SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
14848e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
14858e0841e0SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
14868e0841e0SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
14878e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
1488dfccc68fSToby Isaac   if (!quad) { /* use the first point of the first functional of the dual space */
1489dfccc68fSToby Isaac     PetscDualSpace dsp;
1490dfccc68fSToby Isaac 
1491dfccc68fSToby Isaac     ierr = PetscFEGetDualSpace(fe, &dsp);CHKERRQ(ierr);
1492dfccc68fSToby Isaac     ierr = PetscDualSpaceGetFunctional(dsp, 0, &quad);CHKERRQ(ierr);
14939c3cf19fSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
1494dfccc68fSToby Isaac     Nq = 1;
1495dfccc68fSToby Isaac   } else {
14969c3cf19fSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
1497dfccc68fSToby Isaac   }
149891d2b7ceSToby Isaac   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
1499dfccc68fSToby Isaac   ierr = PetscFEGetQuadrature(fe, &feQuad);CHKERRQ(ierr);
1500dfccc68fSToby Isaac   if (feQuad == quad) {
1501f960e424SToby Isaac     ierr = PetscFEGetDefaultTabulation(fe, &basis, &basisDer, NULL);CHKERRQ(ierr);
15028e0841e0SMatthew 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);
1503dfccc68fSToby Isaac   } else {
1504dfccc68fSToby Isaac     ierr = PetscFEGetTabulation(fe, Nq, quadPoints, &basis, &basisDer, NULL);CHKERRQ(ierr);
1505dfccc68fSToby Isaac   }
1506dfccc68fSToby Isaac   if (qdim != dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim);
1507dfccc68fSToby Isaac   if (v) {
1508dfccc68fSToby Isaac     ierr = PetscMemzero(v, Nq*cdim*sizeof(PetscReal));CHKERRQ(ierr);
1509f960e424SToby Isaac     for (q = 0; q < Nq; ++q) {
1510f960e424SToby Isaac       PetscInt i, k;
1511f960e424SToby Isaac 
1512f960e424SToby Isaac       for (k = 0; k < pdim; ++k)
1513f960e424SToby Isaac         for (i = 0; i < cdim; ++i)
1514dfccc68fSToby Isaac           v[q*cdim + i] += basis[q*pdim + k] * PetscRealPart(coords[k*cdim + i]);
1515f960e424SToby Isaac       ierr = PetscLogFlops(2.0*pdim*cdim);CHKERRQ(ierr);
1516f960e424SToby Isaac     }
1517f960e424SToby Isaac   }
15188e0841e0SMatthew G. Knepley   if (J) {
1519dfccc68fSToby Isaac     ierr = PetscMemzero(J, Nq*cdim*cdim*sizeof(PetscReal));CHKERRQ(ierr);
15208e0841e0SMatthew G. Knepley     for (q = 0; q < Nq; ++q) {
15218e0841e0SMatthew G. Knepley       PetscInt i, j, k, c, r;
15228e0841e0SMatthew G. Knepley 
15238e0841e0SMatthew G. Knepley       /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */
15248e0841e0SMatthew G. Knepley       for (k = 0; k < pdim; ++k)
15258e0841e0SMatthew G. Knepley         for (j = 0; j < dim; ++j)
15268e0841e0SMatthew G. Knepley           for (i = 0; i < cdim; ++i)
1527dfccc68fSToby Isaac             J[(q*cdim + i)*cdim + j] += basisDer[(q*pdim + k)*dim + j] * PetscRealPart(coords[k*cdim + i]);
15283bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0*pdim*dim*cdim);CHKERRQ(ierr);
15298e0841e0SMatthew G. Knepley       if (cdim > dim) {
15308e0841e0SMatthew G. Knepley         for (c = dim; c < cdim; ++c)
15318e0841e0SMatthew G. Knepley           for (r = 0; r < cdim; ++r)
15328e0841e0SMatthew G. Knepley             J[r*cdim+c] = r == c ? 1.0 : 0.0;
15338e0841e0SMatthew G. Knepley       }
1534f960e424SToby Isaac       if (!detJ && !invJ) continue;
1535a63b72c6SToby Isaac       detJt = 0.;
15368e0841e0SMatthew G. Knepley       switch (cdim) {
15378e0841e0SMatthew G. Knepley       case 3:
1538037dc194SToby Isaac         DMPlex_Det3D_Internal(&detJt, &J[q*cdim*dim]);
1539037dc194SToby Isaac         if (invJ) {DMPlex_Invert3D_Internal(&invJ[q*cdim*dim], &J[q*cdim*dim], detJt);}
154017fe8556SMatthew G. Knepley         break;
154149dc4407SMatthew G. Knepley       case 2:
15429f328543SToby Isaac         DMPlex_Det2D_Internal(&detJt, &J[q*cdim*dim]);
1543037dc194SToby Isaac         if (invJ) {DMPlex_Invert2D_Internal(&invJ[q*cdim*dim], &J[q*cdim*dim], detJt);}
154449dc4407SMatthew G. Knepley         break;
15458e0841e0SMatthew G. Knepley       case 1:
1546037dc194SToby Isaac         detJt = J[q*cdim*dim];
1547037dc194SToby Isaac         if (invJ) invJ[q*cdim*dim] = 1.0/detJt;
154849dc4407SMatthew G. Knepley       }
1549f960e424SToby Isaac       if (detJ) detJ[q] = detJt;
155049dc4407SMatthew G. Knepley     }
155149dc4407SMatthew G. Knepley   }
1552037dc194SToby Isaac   else if (detJ || invJ) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Need J to compute invJ or detJ");
1553dfccc68fSToby Isaac   if (feQuad != quad) {
1554dfccc68fSToby Isaac     ierr = PetscFERestoreTabulation(fe, Nq, quadPoints, &basis, &basisDer, NULL);CHKERRQ(ierr);
1555dfccc68fSToby Isaac   }
15568e0841e0SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
15578e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
15588e0841e0SMatthew G. Knepley }
15598e0841e0SMatthew G. Knepley 
15608e0841e0SMatthew G. Knepley /*@C
15618e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell
15628e0841e0SMatthew G. Knepley 
15638e0841e0SMatthew G. Knepley   Collective on DM
15648e0841e0SMatthew G. Knepley 
15658e0841e0SMatthew G. Knepley   Input Arguments:
15668e0841e0SMatthew G. Knepley + dm   - the DM
15678e0841e0SMatthew G. Knepley . cell - the cell
1568dfccc68fSToby Isaac - quad - the quadrature containing the points in the reference element where the geometry will be evaluated.  If quad == NULL, geometry will be
1569dfccc68fSToby Isaac          evaluated at the first vertex of the reference element
15708e0841e0SMatthew G. Knepley 
15718e0841e0SMatthew G. Knepley   Output Arguments:
1572dfccc68fSToby Isaac + v    - the image of the transformed quadrature points, otherwise the image of the first vertex in the closure of the reference element
15738e0841e0SMatthew G. Knepley . J    - the Jacobian of the transform from the reference element at each quadrature point
15748e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point
15758e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point
15768e0841e0SMatthew G. Knepley 
15778e0841e0SMatthew G. Knepley   Level: advanced
15788e0841e0SMatthew G. Knepley 
15798e0841e0SMatthew G. Knepley   Fortran Notes:
15808e0841e0SMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
15818e0841e0SMatthew G. Knepley   include petsc.h90 in your code.
15828e0841e0SMatthew G. Knepley 
15838e0841e0SMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec()
15848e0841e0SMatthew G. Knepley @*/
1585dfccc68fSToby Isaac PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
15868e0841e0SMatthew G. Knepley {
1587dfccc68fSToby Isaac   PetscFE        fe = NULL;
15888e0841e0SMatthew G. Knepley   PetscErrorCode ierr;
15898e0841e0SMatthew G. Knepley 
15908e0841e0SMatthew G. Knepley   PetscFunctionBegin;
1591dfccc68fSToby Isaac   if (dm->coordinateDM) {
1592dfccc68fSToby Isaac     PetscClassId id;
1593dfccc68fSToby Isaac     PetscInt     numFields;
1594dfccc68fSToby Isaac     PetscDS      prob = dm->coordinateDM->prob;
1595dfccc68fSToby Isaac     PetscObject  disc;
1596dfccc68fSToby Isaac 
1597dfccc68fSToby Isaac     ierr = PetscDSGetNumFields(prob, &numFields);CHKERRQ(ierr);
1598dfccc68fSToby Isaac     if (numFields) {
1599dfccc68fSToby Isaac       ierr = PetscDSGetDiscretization(prob,0,&disc);CHKERRQ(ierr);
1600dfccc68fSToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
1601dfccc68fSToby Isaac       if (id == PETSCFE_CLASSID) {
1602dfccc68fSToby Isaac         fe = (PetscFE) disc;
1603dfccc68fSToby Isaac       }
1604dfccc68fSToby Isaac     }
1605dfccc68fSToby Isaac   }
1606dfccc68fSToby Isaac   if (!fe) {ierr = DMPlexComputeCellGeometryFEM_Implicit(dm, cell, quad, v, J, invJ, detJ);CHKERRQ(ierr);}
1607dfccc68fSToby Isaac   else     {ierr = DMPlexComputeCellGeometryFEM_FE(dm, fe, cell, quad, v, J, invJ, detJ);CHKERRQ(ierr);}
1608ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1609ccd2543fSMatthew G Knepley }
1610834e62ceSMatthew G. Knepley 
1611011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1612cc08537eSMatthew G. Knepley {
1613cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1614cc08537eSMatthew G. Knepley   Vec            coordinates;
1615a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
161606e2781eSMatthew G. Knepley   PetscScalar    tmp[2];
1617cc08537eSMatthew G. Knepley   PetscInt       coordSize;
1618cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1619cc08537eSMatthew G. Knepley 
1620cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1621cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
162269d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1623cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1624011ea5d8SMatthew G. Knepley   if (dim != 2) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "We only support 2D edges right now");
16252e17dfb7SMatthew G. Knepley   ierr = DMLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp);CHKERRQ(ierr);
1626cc08537eSMatthew G. Knepley   if (centroid) {
162706e2781eSMatthew G. Knepley     centroid[0] = 0.5*PetscRealPart(coords[0] + tmp[0]);
162806e2781eSMatthew G. Knepley     centroid[1] = 0.5*PetscRealPart(coords[1] + tmp[1]);
1629cc08537eSMatthew G. Knepley   }
1630cc08537eSMatthew G. Knepley   if (normal) {
1631a60a936bSMatthew G. Knepley     PetscReal norm;
1632a60a936bSMatthew G. Knepley 
163306e2781eSMatthew G. Knepley     normal[0]  = -PetscRealPart(coords[1] - tmp[1]);
163406e2781eSMatthew G. Knepley     normal[1]  =  PetscRealPart(coords[0] - tmp[0]);
1635a60a936bSMatthew G. Knepley     norm       = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1]);
1636a60a936bSMatthew G. Knepley     normal[0] /= norm;
1637a60a936bSMatthew G. Knepley     normal[1] /= norm;
1638cc08537eSMatthew G. Knepley   }
1639cc08537eSMatthew G. Knepley   if (vol) {
164006e2781eSMatthew G. Knepley     *vol = PetscSqrtReal(PetscSqr(PetscRealPart(coords[0] - tmp[0])) + PetscSqr(PetscRealPart(coords[1] - tmp[1])));
1641cc08537eSMatthew G. Knepley   }
1642cc08537eSMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1643cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1644cc08537eSMatthew G. Knepley }
1645cc08537eSMatthew G. Knepley 
1646cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i ) / A */
1647011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1648cc08537eSMatthew G. Knepley {
1649cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1650cc08537eSMatthew G. Knepley   Vec            coordinates;
1651cc08537eSMatthew G. Knepley   PetscScalar   *coords = NULL;
16520a1d6728SMatthew G. Knepley   PetscReal      vsum = 0.0, csum[3] = {0.0, 0.0, 0.0}, vtmp, ctmp[4], v0[3], R[9];
16530a1d6728SMatthew G. Knepley   PetscInt       tdim = 2, coordSize, numCorners, p, d, e;
1654cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1655cc08537eSMatthew G. Knepley 
1656cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1657cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
16580a1d6728SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numCorners);CHKERRQ(ierr);
165969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1660cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
16610bce18caSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1662ceee4971SMatthew G. Knepley   if (dim > 2 && centroid) {
1663ceee4971SMatthew G. Knepley     v0[0] = PetscRealPart(coords[0]);
1664ceee4971SMatthew G. Knepley     v0[1] = PetscRealPart(coords[1]);
1665ceee4971SMatthew G. Knepley     v0[2] = PetscRealPart(coords[2]);
1666ceee4971SMatthew G. Knepley   }
1667011ea5d8SMatthew G. Knepley   if (normal) {
1668011ea5d8SMatthew G. Knepley     if (dim > 2) {
16691ee9d5ecSMatthew G. Knepley       const PetscReal x0 = PetscRealPart(coords[dim+0] - coords[0]), x1 = PetscRealPart(coords[dim*2+0] - coords[0]);
16701ee9d5ecSMatthew G. Knepley       const PetscReal y0 = PetscRealPart(coords[dim+1] - coords[1]), y1 = PetscRealPart(coords[dim*2+1] - coords[1]);
16711ee9d5ecSMatthew G. Knepley       const PetscReal z0 = PetscRealPart(coords[dim+2] - coords[2]), z1 = PetscRealPart(coords[dim*2+2] - coords[2]);
16720a1d6728SMatthew G. Knepley       PetscReal       norm;
16730a1d6728SMatthew G. Knepley 
16740a1d6728SMatthew G. Knepley       normal[0] = y0*z1 - z0*y1;
16750a1d6728SMatthew G. Knepley       normal[1] = z0*x1 - x0*z1;
16760a1d6728SMatthew G. Knepley       normal[2] = x0*y1 - y0*x1;
16778b49ba18SBarry Smith       norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2]);
16780a1d6728SMatthew G. Knepley       normal[0] /= norm;
16790a1d6728SMatthew G. Knepley       normal[1] /= norm;
16800a1d6728SMatthew G. Knepley       normal[2] /= norm;
1681011ea5d8SMatthew G. Knepley     } else {
1682011ea5d8SMatthew G. Knepley       for (d = 0; d < dim; ++d) normal[d] = 0.0;
1683011ea5d8SMatthew G. Knepley     }
1684011ea5d8SMatthew G. Knepley   }
1685741bfc07SMatthew G. Knepley   if (dim == 3) {ierr = DMPlexComputeProjection3Dto2D(coordSize, coords, R);CHKERRQ(ierr);}
16860a1d6728SMatthew G. Knepley   for (p = 0; p < numCorners; ++p) {
16870a1d6728SMatthew G. Knepley     /* Need to do this copy to get types right */
16880a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
16891ee9d5ecSMatthew G. Knepley       ctmp[d]      = PetscRealPart(coords[p*tdim+d]);
16901ee9d5ecSMatthew G. Knepley       ctmp[tdim+d] = PetscRealPart(coords[((p+1)%numCorners)*tdim+d]);
16910a1d6728SMatthew G. Knepley     }
16920a1d6728SMatthew G. Knepley     Volume_Triangle_Origin_Internal(&vtmp, ctmp);
16930a1d6728SMatthew G. Knepley     vsum += vtmp;
16940a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
16950a1d6728SMatthew G. Knepley       csum[d] += (ctmp[d] + ctmp[tdim+d])*vtmp;
16960a1d6728SMatthew G. Knepley     }
16970a1d6728SMatthew G. Knepley   }
16980a1d6728SMatthew G. Knepley   for (d = 0; d < tdim; ++d) {
16990a1d6728SMatthew G. Knepley     csum[d] /= (tdim+1)*vsum;
17000a1d6728SMatthew G. Knepley   }
17010a1d6728SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1702ee6bbdb2SSatish Balay   if (vol) *vol = PetscAbsReal(vsum);
17030a1d6728SMatthew G. Knepley   if (centroid) {
17040a1d6728SMatthew G. Knepley     if (dim > 2) {
17050a1d6728SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
17060a1d6728SMatthew G. Knepley         centroid[d] = v0[d];
17070a1d6728SMatthew G. Knepley         for (e = 0; e < dim; ++e) {
17080a1d6728SMatthew G. Knepley           centroid[d] += R[d*dim+e]*csum[e];
17090a1d6728SMatthew G. Knepley         }
17100a1d6728SMatthew G. Knepley       }
17110a1d6728SMatthew G. Knepley     } else for (d = 0; d < dim; ++d) centroid[d] = csum[d];
17120a1d6728SMatthew G. Knepley   }
1713cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1714cc08537eSMatthew G. Knepley }
1715cc08537eSMatthew G. Knepley 
17160ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i ) / V */
1717011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
17180ec8681fSMatthew G. Knepley {
17190ec8681fSMatthew G. Knepley   PetscSection    coordSection;
17200ec8681fSMatthew G. Knepley   Vec             coordinates;
17210ec8681fSMatthew G. Knepley   PetscScalar    *coords = NULL;
172286623015SMatthew G. Knepley   PetscReal       vsum = 0.0, vtmp, coordsTmp[3*3];
1723a7df9edeSMatthew G. Knepley   const PetscInt *faces, *facesO;
17240ec8681fSMatthew G. Knepley   PetscInt        numFaces, f, coordSize, numCorners, p, d;
17250ec8681fSMatthew G. Knepley   PetscErrorCode  ierr;
17260ec8681fSMatthew G. Knepley 
17270ec8681fSMatthew G. Knepley   PetscFunctionBegin;
1728f6dae198SJed Brown   if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim);
17290ec8681fSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
173069d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
17310ec8681fSMatthew G. Knepley 
1732d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0;
17330ec8681fSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numFaces);CHKERRQ(ierr);
17340ec8681fSMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr);
1735a7df9edeSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &facesO);CHKERRQ(ierr);
17360ec8681fSMatthew G. Knepley   for (f = 0; f < numFaces; ++f) {
1737011ea5d8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
17380ec8681fSMatthew G. Knepley     numCorners = coordSize/dim;
17390ec8681fSMatthew G. Knepley     switch (numCorners) {
17400ec8681fSMatthew G. Knepley     case 3:
17410ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
17421ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
17431ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
17441ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]);
17450ec8681fSMatthew G. Knepley       }
17460ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1747a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
17480ec8681fSMatthew G. Knepley       vsum += vtmp;
17494f25033aSJed Brown       if (centroid) {           /* Centroid of OABC = (a+b+c)/4 */
17500ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
17511ee9d5ecSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
17520ec8681fSMatthew G. Knepley         }
17530ec8681fSMatthew G. Knepley       }
17540ec8681fSMatthew G. Knepley       break;
17550ec8681fSMatthew G. Knepley     case 4:
17560ec8681fSMatthew G. Knepley       /* DO FOR PYRAMID */
17570ec8681fSMatthew G. Knepley       /* First tet */
17580ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
17591ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
17601ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
17611ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
17620ec8681fSMatthew G. Knepley       }
17630ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1764a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
17650ec8681fSMatthew G. Knepley       vsum += vtmp;
17660ec8681fSMatthew G. Knepley       if (centroid) {
17670ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
17680ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
17690ec8681fSMatthew G. Knepley         }
17700ec8681fSMatthew G. Knepley       }
17710ec8681fSMatthew G. Knepley       /* Second tet */
17720ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
17731ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[1*dim+d]);
17741ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[2*dim+d]);
17751ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
17760ec8681fSMatthew G. Knepley       }
17770ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1778a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
17790ec8681fSMatthew G. Knepley       vsum += vtmp;
17800ec8681fSMatthew G. Knepley       if (centroid) {
17810ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
17820ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
17830ec8681fSMatthew G. Knepley         }
17840ec8681fSMatthew G. Knepley       }
17850ec8681fSMatthew G. Knepley       break;
17860ec8681fSMatthew G. Knepley     default:
1787796f034aSJed Brown       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle faces with %D vertices", numCorners);
17880ec8681fSMatthew G. Knepley     }
17894f25033aSJed Brown     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
17900ec8681fSMatthew G. Knepley   }
17918763be8eSMatthew G. Knepley   if (vol)     *vol = PetscAbsReal(vsum);
17920ec8681fSMatthew G. Knepley   if (normal)   for (d = 0; d < dim; ++d) normal[d]    = 0.0;
1793d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] /= (vsum*4);
17940ec8681fSMatthew G. Knepley   PetscFunctionReturn(0);
17950ec8681fSMatthew G. Knepley }
17960ec8681fSMatthew G. Knepley 
1797834e62ceSMatthew G. Knepley /*@C
1798834e62ceSMatthew G. Knepley   DMPlexComputeCellGeometryFVM - Compute the volume for a given cell
1799834e62ceSMatthew G. Knepley 
1800834e62ceSMatthew G. Knepley   Collective on DM
1801834e62ceSMatthew G. Knepley 
1802834e62ceSMatthew G. Knepley   Input Arguments:
1803834e62ceSMatthew G. Knepley + dm   - the DM
1804834e62ceSMatthew G. Knepley - cell - the cell
1805834e62ceSMatthew G. Knepley 
1806834e62ceSMatthew G. Knepley   Output Arguments:
1807834e62ceSMatthew G. Knepley + volume   - the cell volume
1808cc08537eSMatthew G. Knepley . centroid - the cell centroid
1809cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate
1810834e62ceSMatthew G. Knepley 
1811834e62ceSMatthew G. Knepley   Level: advanced
1812834e62ceSMatthew G. Knepley 
1813834e62ceSMatthew G. Knepley   Fortran Notes:
1814834e62ceSMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1815834e62ceSMatthew G. Knepley   include petsc.h90 in your code.
1816834e62ceSMatthew G. Knepley 
181769d8a9ceSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec()
1818834e62ceSMatthew G. Knepley @*/
1819cc08537eSMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1820834e62ceSMatthew G. Knepley {
18210ec8681fSMatthew G. Knepley   PetscInt       depth, dim;
1822834e62ceSMatthew G. Knepley   PetscErrorCode ierr;
1823834e62ceSMatthew G. Knepley 
1824834e62ceSMatthew G. Knepley   PetscFunctionBegin;
1825834e62ceSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1826c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1827834e62ceSMatthew G. Knepley   if (depth != dim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");
1828834e62ceSMatthew G. Knepley   /* We need to keep a pointer to the depth label */
1829c58f1c22SToby Isaac   ierr = DMGetLabelValue(dm, "depth", cell, &depth);CHKERRQ(ierr);
1830834e62ceSMatthew G. Knepley   /* Cone size is now the number of faces */
1831011ea5d8SMatthew G. Knepley   switch (depth) {
1832cc08537eSMatthew G. Knepley   case 1:
1833011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1834cc08537eSMatthew G. Knepley     break;
1835834e62ceSMatthew G. Knepley   case 2:
1836011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1837834e62ceSMatthew G. Knepley     break;
1838834e62ceSMatthew G. Knepley   case 3:
1839011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1840834e62ceSMatthew G. Knepley     break;
1841834e62ceSMatthew G. Knepley   default:
1842834e62ceSMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
1843834e62ceSMatthew G. Knepley   }
1844834e62ceSMatthew G. Knepley   PetscFunctionReturn(0);
1845834e62ceSMatthew G. Knepley }
1846113c68e6SMatthew G. Knepley 
1847c501906fSMatthew G. Knepley /*@
1848c501906fSMatthew G. Knepley   DMPlexComputeGeometryFEM - Precompute cell geometry for the entire mesh
1849c501906fSMatthew G. Knepley 
1850c501906fSMatthew G. Knepley   Collective on dm
1851c501906fSMatthew G. Knepley 
1852c501906fSMatthew G. Knepley   Input Parameter:
1853c501906fSMatthew G. Knepley . dm - The DMPlex
1854c501906fSMatthew G. Knepley 
1855c501906fSMatthew G. Knepley   Output Parameter:
1856c501906fSMatthew G. Knepley . cellgeom - A vector with the cell geometry data for each cell
1857c501906fSMatthew G. Knepley 
1858c501906fSMatthew G. Knepley   Level: beginner
1859c501906fSMatthew G. Knepley 
1860c501906fSMatthew G. Knepley .keywords: DMPlexComputeCellGeometryFEM()
1861c501906fSMatthew G. Knepley @*/
1862c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom)
1863c0d900a5SMatthew G. Knepley {
1864c0d900a5SMatthew G. Knepley   DM             dmCell;
1865c0d900a5SMatthew G. Knepley   Vec            coordinates;
1866c0d900a5SMatthew G. Knepley   PetscSection   coordSection, sectionCell;
1867c0d900a5SMatthew G. Knepley   PetscScalar   *cgeom;
1868c0d900a5SMatthew G. Knepley   PetscInt       cStart, cEnd, cMax, c;
1869c0d900a5SMatthew G. Knepley   PetscErrorCode ierr;
1870c0d900a5SMatthew G. Knepley 
1871c0d900a5SMatthew G. Knepley   PetscFunctionBegin;
1872c0d900a5SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1873c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1874c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1875c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1876c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1877c0d900a5SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1878c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1879c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
1880c0d900a5SMatthew G. Knepley   cEnd = cMax < 0 ? cEnd : cMax;
1881c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
1882c0d900a5SMatthew G. Knepley   /* TODO This needs to be multiplied by Nq for non-affine */
18839e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFECellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1884c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1885c0d900a5SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1886c0d900a5SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1887c0d900a5SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
1888c0d900a5SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1889c0d900a5SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1890c0d900a5SMatthew G. Knepley     PetscFECellGeom *cg;
1891c0d900a5SMatthew G. Knepley 
1892c0d900a5SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1893c0d900a5SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1894c0d900a5SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v0, cg->J, cg->invJ, &cg->detJ);CHKERRQ(ierr);
1895c0d900a5SMatthew G. Knepley     if (cg->detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", cg->detJ, c);
1896c0d900a5SMatthew G. Knepley   }
1897c0d900a5SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1898c0d900a5SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
1899c0d900a5SMatthew G. Knepley   PetscFunctionReturn(0);
1900c0d900a5SMatthew G. Knepley }
1901c0d900a5SMatthew G. Knepley 
1902891a9168SMatthew G. Knepley /*@
1903891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
1904891a9168SMatthew G. Knepley 
1905891a9168SMatthew G. Knepley   Input Parameter:
1906891a9168SMatthew G. Knepley . dm - The DM
1907891a9168SMatthew G. Knepley 
1908891a9168SMatthew G. Knepley   Output Parameters:
1909891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data
1910891a9168SMatthew G. Knepley . facegeom - A Vec of PetscFVFaceGeom data
1911891a9168SMatthew G. Knepley 
1912891a9168SMatthew G. Knepley   Level: developer
1913891a9168SMatthew G. Knepley 
1914891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM()
1915891a9168SMatthew G. Knepley @*/
1916113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom)
1917113c68e6SMatthew G. Knepley {
1918113c68e6SMatthew G. Knepley   DM             dmFace, dmCell;
1919113c68e6SMatthew G. Knepley   DMLabel        ghostLabel;
1920113c68e6SMatthew G. Knepley   PetscSection   sectionFace, sectionCell;
1921113c68e6SMatthew G. Knepley   PetscSection   coordSection;
1922113c68e6SMatthew G. Knepley   Vec            coordinates;
1923113c68e6SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
1924113c68e6SMatthew G. Knepley   PetscReal      minradius, gminradius;
1925113c68e6SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
1926113c68e6SMatthew G. Knepley   PetscErrorCode ierr;
1927113c68e6SMatthew G. Knepley 
1928113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1929113c68e6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1930113c68e6SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1931113c68e6SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1932113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
1933113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1934113c68e6SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1935113c68e6SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1936113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1937113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1938113c68e6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1939113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
19409e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1941113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1942113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1943113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1944113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
194506348e87SToby Isaac   if (cEndInterior < 0) {
194606348e87SToby Isaac     cEndInterior = cEnd;
194706348e87SToby Isaac   }
1948113c68e6SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1949113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
1950113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
1951113c68e6SMatthew G. Knepley 
1952113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1953113c68e6SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1954113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr);
1955113c68e6SMatthew G. Knepley   }
1956113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
1957113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmFace);CHKERRQ(ierr);
1958113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionFace);CHKERRQ(ierr);
1959113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1960113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr);
19619e5edeeeSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1962113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr);
1963113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmFace, sectionFace);CHKERRQ(ierr);
1964113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionFace);CHKERRQ(ierr);
1965113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr);
1966113c68e6SMatthew G. Knepley   ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr);
1967c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1968113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
1969113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
1970113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
1971113c68e6SMatthew G. Knepley     PetscReal        area;
197250d63984SToby Isaac     PetscInt         ghost = -1, d, numChildren;
1973113c68e6SMatthew G. Knepley 
19749ac3fadcSMatthew G. Knepley     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
197550d63984SToby Isaac     ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
197650d63984SToby Isaac     if (ghost >= 0 || numChildren) continue;
1977113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr);
1978113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr);
1979113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
1980113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
1981113c68e6SMatthew G. Knepley     {
1982113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
198306348e87SToby Isaac       PetscInt         ncells;
1984113c68e6SMatthew G. Knepley       const PetscInt  *cells;
1985113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
19860453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
1987113c68e6SMatthew G. Knepley 
1988113c68e6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr);
198906348e87SToby Isaac       ierr = DMPlexGetSupportSize(dm, f, &ncells);CHKERRQ(ierr);
1990113c68e6SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr);
1991113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
199206348e87SToby Isaac       if (ncells > 1) {
199306348e87SToby Isaac         ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr);
1994113c68e6SMatthew G. Knepley         rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
199506348e87SToby Isaac       }
199606348e87SToby Isaac       else {
199706348e87SToby Isaac         rcentroid = fg->centroid;
199806348e87SToby Isaac       }
19992e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr);
20002e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr);
20010453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
2002113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
2003113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
2004113c68e6SMatthew G. Knepley       }
2005113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
2006113c68e6SMatthew 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]);
2007113c68e6SMatthew 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]);
2008113c68e6SMatthew G. Knepley         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f);
2009113c68e6SMatthew G. Knepley       }
2010113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
2011113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
2012113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2013113c68e6SMatthew G. Knepley       }
201406348e87SToby Isaac       if (ncells > 1 && cells[1] < cEndInterior) {
2015113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
2016113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2017113c68e6SMatthew G. Knepley       }
2018113c68e6SMatthew G. Knepley     }
2019113c68e6SMatthew G. Knepley   }
2020b2566f29SBarry Smith   ierr = MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2021113c68e6SMatthew G. Knepley   ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr);
2022113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
2023113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
2024113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2025113c68e6SMatthew G. Knepley     const PetscInt  *cone,    *support;
2026113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
2027113c68e6SMatthew G. Knepley 
2028113c68e6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr);
2029113c68e6SMatthew G. Knepley     if (coneSize != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize);
2030113c68e6SMatthew G. Knepley     ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr);
2031113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr);
203250d63984SToby Isaac     if (supportSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 2", cone[0], supportSize);
2033113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr);
2034113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr);
2035113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
2036113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
2037113c68e6SMatthew G. Knepley       if (support[s] == c) {
2038640bce14SSatish Balay         PetscFVCellGeom       *ci;
2039113c68e6SMatthew G. Knepley         PetscFVCellGeom       *cg;
2040113c68e6SMatthew G. Knepley         PetscReal              c2f[3], a;
2041113c68e6SMatthew G. Knepley 
2042113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr);
2043113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
2044113c68e6SMatthew G. Knepley         a    = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
2045113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr);
2046113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid);
2047113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
2048113c68e6SMatthew G. Knepley       }
2049113c68e6SMatthew G. Knepley     }
2050113c68e6SMatthew G. Knepley   }
2051113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr);
2052113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2053113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
2054113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmFace);CHKERRQ(ierr);
2055113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2056113c68e6SMatthew G. Knepley }
2057113c68e6SMatthew G. Knepley 
2058113c68e6SMatthew G. Knepley /*@C
2059113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
2060113c68e6SMatthew G. Knepley 
2061113c68e6SMatthew G. Knepley   Not collective
2062113c68e6SMatthew G. Knepley 
2063113c68e6SMatthew G. Knepley   Input Argument:
2064113c68e6SMatthew G. Knepley . dm - the DM
2065113c68e6SMatthew G. Knepley 
2066113c68e6SMatthew G. Knepley   Output Argument:
2067113c68e6SMatthew G. Knepley . minradius - the minium cell radius
2068113c68e6SMatthew G. Knepley 
2069113c68e6SMatthew G. Knepley   Level: developer
2070113c68e6SMatthew G. Knepley 
2071113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates()
2072113c68e6SMatthew G. Knepley @*/
2073113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius)
2074113c68e6SMatthew G. Knepley {
2075113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2076113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2077113c68e6SMatthew G. Knepley   PetscValidPointer(minradius,2);
2078113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex*) dm->data)->minradius;
2079113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2080113c68e6SMatthew G. Knepley }
2081113c68e6SMatthew G. Knepley 
2082113c68e6SMatthew G. Knepley /*@C
2083113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
2084113c68e6SMatthew G. Knepley 
2085113c68e6SMatthew G. Knepley   Logically collective
2086113c68e6SMatthew G. Knepley 
2087113c68e6SMatthew G. Knepley   Input Arguments:
2088113c68e6SMatthew G. Knepley + dm - the DM
2089113c68e6SMatthew G. Knepley - minradius - the minium cell radius
2090113c68e6SMatthew G. Knepley 
2091113c68e6SMatthew G. Knepley   Level: developer
2092113c68e6SMatthew G. Knepley 
2093113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates()
2094113c68e6SMatthew G. Knepley @*/
2095113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius)
2096113c68e6SMatthew G. Knepley {
2097113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2098113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2099113c68e6SMatthew G. Knepley   ((DM_Plex*) dm->data)->minradius = minradius;
2100113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2101113c68e6SMatthew G. Knepley }
2102856ac710SMatthew G. Knepley 
2103856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2104856ac710SMatthew G. Knepley {
2105856ac710SMatthew G. Knepley   DMLabel        ghostLabel;
2106856ac710SMatthew G. Knepley   PetscScalar   *dx, *grad, **gref;
2107856ac710SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
2108856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2109856ac710SMatthew G. Knepley 
2110856ac710SMatthew G. Knepley   PetscFunctionBegin;
2111856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2112856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2113856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2114856ac710SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr);
2115856ac710SMatthew G. Knepley   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
2116c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2117856ac710SMatthew G. Knepley   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
2118856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
2119856ac710SMatthew G. Knepley     const PetscInt        *faces;
2120856ac710SMatthew G. Knepley     PetscInt               numFaces, usedFaces, f, d;
2121640bce14SSatish Balay     PetscFVCellGeom        *cg;
2122856ac710SMatthew G. Knepley     PetscBool              boundary;
2123856ac710SMatthew G. Knepley     PetscInt               ghost;
2124856ac710SMatthew G. Knepley 
2125856ac710SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2126856ac710SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr);
2127856ac710SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr);
2128856ac710SMatthew 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);
2129856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
2130640bce14SSatish Balay       PetscFVCellGeom       *cg1;
2131856ac710SMatthew G. Knepley       PetscFVFaceGeom       *fg;
2132856ac710SMatthew G. Knepley       const PetscInt        *fcells;
2133856ac710SMatthew G. Knepley       PetscInt               ncell, side;
2134856ac710SMatthew G. Knepley 
2135856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
2136a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
2137856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
2138856ac710SMatthew G. Knepley       ierr  = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr);
2139856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
2140856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
2141856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr);
2142856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
2143856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d];
2144856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side];  /* Gradient reconstruction term will go here */
2145856ac710SMatthew G. Knepley     }
2146856ac710SMatthew G. Knepley     if (!usedFaces) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
2147856ac710SMatthew G. Knepley     ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr);
2148856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
2149856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
2150a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
2151856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
2152856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d];
2153856ac710SMatthew G. Knepley       ++usedFaces;
2154856ac710SMatthew G. Knepley     }
2155856ac710SMatthew G. Knepley   }
2156856ac710SMatthew G. Knepley   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
2157856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2158856ac710SMatthew G. Knepley }
2159856ac710SMatthew G. Knepley 
2160b81db932SToby Isaac static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2161b81db932SToby Isaac {
2162b81db932SToby Isaac   DMLabel        ghostLabel;
2163b81db932SToby Isaac   PetscScalar   *dx, *grad, **gref;
2164b81db932SToby Isaac   PetscInt       dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0;
2165b81db932SToby Isaac   PetscSection   neighSec;
2166b81db932SToby Isaac   PetscInt     (*neighbors)[2];
2167b81db932SToby Isaac   PetscInt      *counter;
2168b81db932SToby Isaac   PetscErrorCode ierr;
2169b81db932SToby Isaac 
2170b81db932SToby Isaac   PetscFunctionBegin;
2171b81db932SToby Isaac   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2172b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2173b81db932SToby Isaac   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
21745bc680faSToby Isaac   if (cEndInterior < 0) {
21755bc680faSToby Isaac     cEndInterior = cEnd;
21765bc680faSToby Isaac   }
2177b81db932SToby Isaac   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm),&neighSec);CHKERRQ(ierr);
2178b81db932SToby Isaac   ierr = PetscSectionSetChart(neighSec,cStart,cEndInterior);CHKERRQ(ierr);
2179b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
2180c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2181b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2182b81db932SToby Isaac     const PetscInt        *fcells;
2183b81db932SToby Isaac     PetscBool              boundary;
21845bc680faSToby Isaac     PetscInt               ghost = -1;
2185b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
2186b81db932SToby Isaac 
218706348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
2188a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
2189b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
2190b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
2191b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
219206348e87SToby Isaac     if (numCells == 2) {
2193b81db932SToby Isaac       ierr = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
2194b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2195b81db932SToby Isaac         PetscInt cell = fcells[c];
2196b81db932SToby Isaac 
2197e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
2198b81db932SToby Isaac           ierr = PetscSectionAddDof(neighSec,cell,1);CHKERRQ(ierr);
2199b81db932SToby Isaac         }
2200b81db932SToby Isaac       }
2201b81db932SToby Isaac     }
220206348e87SToby Isaac   }
2203b81db932SToby Isaac   ierr = PetscSectionSetUp(neighSec);CHKERRQ(ierr);
2204b81db932SToby Isaac   ierr = PetscSectionGetMaxDof(neighSec,&maxNumFaces);CHKERRQ(ierr);
2205b81db932SToby Isaac   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
2206b81db932SToby Isaac   nStart = 0;
2207b81db932SToby Isaac   ierr = PetscSectionGetStorageSize(neighSec,&nEnd);CHKERRQ(ierr);
2208b81db932SToby Isaac   ierr = PetscMalloc1((nEnd-nStart),&neighbors);CHKERRQ(ierr);
2209b81db932SToby Isaac   ierr = PetscCalloc1((cEndInterior-cStart),&counter);CHKERRQ(ierr);
2210b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2211b81db932SToby Isaac     const PetscInt        *fcells;
2212b81db932SToby Isaac     PetscBool              boundary;
22135bc680faSToby Isaac     PetscInt               ghost = -1;
2214b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
2215b81db932SToby Isaac 
221606348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
2217a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
2218b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
2219b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
2220b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
222106348e87SToby Isaac     if (numCells == 2) {
2222b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
2223b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2224b81db932SToby Isaac         PetscInt cell = fcells[c], off;
2225b81db932SToby Isaac 
2226e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
2227b81db932SToby Isaac           ierr = PetscSectionGetOffset(neighSec,cell,&off);CHKERRQ(ierr);
2228b81db932SToby Isaac           off += counter[cell - cStart]++;
2229b81db932SToby Isaac           neighbors[off][0] = f;
2230b81db932SToby Isaac           neighbors[off][1] = fcells[1 - c];
2231b81db932SToby Isaac         }
2232b81db932SToby Isaac       }
2233b81db932SToby Isaac     }
223406348e87SToby Isaac   }
2235b81db932SToby Isaac   ierr = PetscFree(counter);CHKERRQ(ierr);
2236b81db932SToby Isaac   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
2237b81db932SToby Isaac   for (c = cStart; c < cEndInterior; c++) {
2238317218b9SToby Isaac     PetscInt               numFaces, f, d, off, ghost = -1;
2239640bce14SSatish Balay     PetscFVCellGeom        *cg;
2240b81db932SToby Isaac 
2241b81db932SToby Isaac     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2242b81db932SToby Isaac     ierr = PetscSectionGetDof(neighSec, c, &numFaces);CHKERRQ(ierr);
2243b81db932SToby Isaac     ierr = PetscSectionGetOffset(neighSec, c, &off);CHKERRQ(ierr);
2244317218b9SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, c, &ghost);CHKERRQ(ierr);}
2245317218b9SToby 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);
2246b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2247640bce14SSatish Balay       PetscFVCellGeom       *cg1;
2248b81db932SToby Isaac       PetscFVFaceGeom       *fg;
2249b81db932SToby Isaac       const PetscInt        *fcells;
2250b81db932SToby Isaac       PetscInt               ncell, side, nface;
2251b81db932SToby Isaac 
2252b81db932SToby Isaac       nface = neighbors[off + f][0];
2253b81db932SToby Isaac       ncell = neighbors[off + f][1];
2254b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm,nface,&fcells);CHKERRQ(ierr);
2255b81db932SToby Isaac       side  = (c != fcells[0]);
2256b81db932SToby Isaac       ierr  = DMPlexPointLocalRef(dmFace, nface, fgeom, &fg);CHKERRQ(ierr);
2257b81db932SToby Isaac       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
2258b81db932SToby Isaac       for (d = 0; d < dim; ++d) dx[f*dim+d] = cg1->centroid[d] - cg->centroid[d];
2259b81db932SToby Isaac       gref[f] = fg->grad[side];  /* Gradient reconstruction term will go here */
2260b81db932SToby Isaac     }
2261b81db932SToby Isaac     ierr = PetscFVComputeGradient(fvm, numFaces, dx, grad);CHKERRQ(ierr);
2262b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2263b81db932SToby Isaac       for (d = 0; d < dim; ++d) gref[f][d] = grad[f*dim+d];
2264b81db932SToby Isaac     }
2265b81db932SToby Isaac   }
2266b81db932SToby Isaac   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
22675fe94518SToby Isaac   ierr = PetscSectionDestroy(&neighSec);CHKERRQ(ierr);
2268b81db932SToby Isaac   ierr = PetscFree(neighbors);CHKERRQ(ierr);
2269b81db932SToby Isaac   PetscFunctionReturn(0);
2270b81db932SToby Isaac }
2271b81db932SToby Isaac 
2272856ac710SMatthew G. Knepley /*@
2273856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
2274856ac710SMatthew G. Knepley 
2275856ac710SMatthew G. Knepley   Collective on DM
2276856ac710SMatthew G. Knepley 
2277856ac710SMatthew G. Knepley   Input Arguments:
2278856ac710SMatthew G. Knepley + dm  - The DM
2279856ac710SMatthew G. Knepley . fvm - The PetscFV
22808f9f38e3SMatthew G. Knepley . faceGeometry - The face geometry from DMPlexComputeFaceGeometryFVM()
22818f9f38e3SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexComputeCellGeometryFVM()
2282856ac710SMatthew G. Knepley 
2283856ac710SMatthew G. Knepley   Output Parameters:
2284856ac710SMatthew G. Knepley + faceGeometry - The geometric factors for gradient calculation are inserted
2285856ac710SMatthew G. Knepley - dmGrad - The DM describing the layout of gradient data
2286856ac710SMatthew G. Knepley 
2287856ac710SMatthew G. Knepley   Level: developer
2288856ac710SMatthew G. Knepley 
2289856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM()
2290856ac710SMatthew G. Knepley @*/
2291856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad)
2292856ac710SMatthew G. Knepley {
2293856ac710SMatthew G. Knepley   DM             dmFace, dmCell;
2294856ac710SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
2295b81db932SToby Isaac   PetscSection   sectionGrad, parentSection;
2296856ac710SMatthew G. Knepley   PetscInt       dim, pdim, cStart, cEnd, cEndInterior, c;
2297856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2298856ac710SMatthew G. Knepley 
2299856ac710SMatthew G. Knepley   PetscFunctionBegin;
2300856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2301856ac710SMatthew G. Knepley   ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr);
2302856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2303856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2304856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
2305856ac710SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
2306856ac710SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
2307856ac710SMatthew G. Knepley   ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2308856ac710SMatthew G. Knepley   ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2309b81db932SToby Isaac   ierr = DMPlexGetTree(dm,&parentSection,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
2310b81db932SToby Isaac   if (!parentSection) {
2311856ac710SMatthew G. Knepley     ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2312b5a3613cSMatthew G. Knepley   } else {
2313b81db932SToby Isaac     ierr = BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2314b81db932SToby Isaac   }
2315856ac710SMatthew G. Knepley   ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2316856ac710SMatthew G. Knepley   ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2317856ac710SMatthew G. Knepley   /* Create storage for gradients */
2318856ac710SMatthew G. Knepley   ierr = DMClone(dm, dmGrad);CHKERRQ(ierr);
2319856ac710SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionGrad);CHKERRQ(ierr);
2320856ac710SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr);
2321856ac710SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);}
2322856ac710SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr);
2323856ac710SMatthew G. Knepley   ierr = DMSetDefaultSection(*dmGrad, sectionGrad);CHKERRQ(ierr);
2324856ac710SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionGrad);CHKERRQ(ierr);
2325856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2326856ac710SMatthew G. Knepley }
2327b27d5b9eSToby Isaac 
2328c501906fSMatthew G. Knepley /*@
2329c501906fSMatthew G. Knepley   DMPlexGetDataFVM - Retrieve precomputed cell geometry
2330c501906fSMatthew G. Knepley 
2331c501906fSMatthew G. Knepley   Collective on DM
2332c501906fSMatthew G. Knepley 
2333c501906fSMatthew G. Knepley   Input Arguments:
2334c501906fSMatthew G. Knepley + dm  - The DM
2335c501906fSMatthew G. Knepley - fvm - The PetscFV
2336c501906fSMatthew G. Knepley 
2337c501906fSMatthew G. Knepley   Output Parameters:
2338c501906fSMatthew G. Knepley + cellGeometry - The cell geometry
2339c501906fSMatthew G. Knepley . faceGeometry - The face geometry
2340c501906fSMatthew G. Knepley - dmGrad       - The gradient matrices
2341c501906fSMatthew G. Knepley 
2342c501906fSMatthew G. Knepley   Level: developer
2343c501906fSMatthew G. Knepley 
2344c501906fSMatthew G. Knepley .seealso: DMPlexComputeGeometryFVM()
2345c501906fSMatthew G. Knepley @*/
2346b27d5b9eSToby Isaac PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM)
2347b27d5b9eSToby Isaac {
2348b27d5b9eSToby Isaac   PetscObject    cellgeomobj, facegeomobj;
2349b27d5b9eSToby Isaac   PetscErrorCode ierr;
2350b27d5b9eSToby Isaac 
2351b27d5b9eSToby Isaac   PetscFunctionBegin;
2352b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2353b27d5b9eSToby Isaac   if (!cellgeomobj) {
2354b27d5b9eSToby Isaac     Vec cellgeomInt, facegeomInt;
2355b27d5b9eSToby Isaac 
2356b27d5b9eSToby Isaac     ierr = DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt);CHKERRQ(ierr);
2357b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_cellgeom_fvm",(PetscObject)cellgeomInt);CHKERRQ(ierr);
2358b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_facegeom_fvm",(PetscObject)facegeomInt);CHKERRQ(ierr);
2359b27d5b9eSToby Isaac     ierr = VecDestroy(&cellgeomInt);CHKERRQ(ierr);
2360b27d5b9eSToby Isaac     ierr = VecDestroy(&facegeomInt);CHKERRQ(ierr);
2361b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2362b27d5b9eSToby Isaac   }
2363b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_facegeom_fvm", &facegeomobj);CHKERRQ(ierr);
2364b27d5b9eSToby Isaac   if (cellgeom) *cellgeom = (Vec) cellgeomobj;
2365b27d5b9eSToby Isaac   if (facegeom) *facegeom = (Vec) facegeomobj;
2366b27d5b9eSToby Isaac   if (gradDM) {
2367b27d5b9eSToby Isaac     PetscObject gradobj;
2368b27d5b9eSToby Isaac     PetscBool   computeGradients;
2369b27d5b9eSToby Isaac 
2370b27d5b9eSToby Isaac     ierr = PetscFVGetComputeGradients(fv,&computeGradients);CHKERRQ(ierr);
2371b27d5b9eSToby Isaac     if (!computeGradients) {
2372b27d5b9eSToby Isaac       *gradDM = NULL;
2373b27d5b9eSToby Isaac       PetscFunctionReturn(0);
2374b27d5b9eSToby Isaac     }
2375b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2376b27d5b9eSToby Isaac     if (!gradobj) {
2377b27d5b9eSToby Isaac       DM dmGradInt;
2378b27d5b9eSToby Isaac 
2379b27d5b9eSToby Isaac       ierr = DMPlexComputeGradientFVM(dm,fv,(Vec) facegeomobj,(Vec) cellgeomobj,&dmGradInt);CHKERRQ(ierr);
2380b27d5b9eSToby Isaac       ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt);CHKERRQ(ierr);
2381b27d5b9eSToby Isaac       ierr = DMDestroy(&dmGradInt);CHKERRQ(ierr);
2382b27d5b9eSToby Isaac       ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2383b27d5b9eSToby Isaac     }
2384b27d5b9eSToby Isaac     *gradDM = (DM) gradobj;
2385b27d5b9eSToby Isaac   }
2386b27d5b9eSToby Isaac   PetscFunctionReturn(0);
2387b27d5b9eSToby Isaac }
2388d6143a4eSToby Isaac 
23899d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_NewtonUpdate(PetscInt dimC, PetscInt dimR, PetscScalar *J, PetscScalar *invJ, PetscScalar *work,  PetscReal *resNeg, PetscReal *guess)
23909d150b73SToby Isaac {
23919d150b73SToby Isaac   PetscInt l, m;
23929d150b73SToby Isaac 
2393cd345991SToby Isaac   PetscFunctionBeginHot;
23949d150b73SToby Isaac   if (dimC == dimR && dimR <= 3) {
23959d150b73SToby Isaac     /* invert Jacobian, multiply */
23969d150b73SToby Isaac     PetscScalar det, idet;
23979d150b73SToby Isaac 
23989d150b73SToby Isaac     switch (dimR) {
23999d150b73SToby Isaac     case 1:
24009d150b73SToby Isaac       invJ[0] = 1./ J[0];
24019d150b73SToby Isaac       break;
24029d150b73SToby Isaac     case 2:
24039d150b73SToby Isaac       det = J[0] * J[3] - J[1] * J[2];
24049d150b73SToby Isaac       idet = 1./det;
24059d150b73SToby Isaac       invJ[0] =  J[3] * idet;
24069d150b73SToby Isaac       invJ[1] = -J[1] * idet;
24079d150b73SToby Isaac       invJ[2] = -J[2] * idet;
24089d150b73SToby Isaac       invJ[3] =  J[0] * idet;
24099d150b73SToby Isaac       break;
24109d150b73SToby Isaac     case 3:
24119d150b73SToby Isaac       {
24129d150b73SToby Isaac         invJ[0] = J[4] * J[8] - J[5] * J[7];
24139d150b73SToby Isaac         invJ[1] = J[2] * J[7] - J[1] * J[8];
24149d150b73SToby Isaac         invJ[2] = J[1] * J[5] - J[2] * J[4];
24159d150b73SToby Isaac         det = invJ[0] * J[0] + invJ[1] * J[3] + invJ[2] * J[6];
24169d150b73SToby Isaac         idet = 1./det;
24179d150b73SToby Isaac         invJ[0] *= idet;
24189d150b73SToby Isaac         invJ[1] *= idet;
24199d150b73SToby Isaac         invJ[2] *= idet;
24209d150b73SToby Isaac         invJ[3]  = idet * (J[5] * J[6] - J[3] * J[8]);
24219d150b73SToby Isaac         invJ[4]  = idet * (J[0] * J[8] - J[2] * J[6]);
24229d150b73SToby Isaac         invJ[5]  = idet * (J[2] * J[3] - J[0] * J[5]);
24239d150b73SToby Isaac         invJ[6]  = idet * (J[3] * J[7] - J[4] * J[6]);
24249d150b73SToby Isaac         invJ[7]  = idet * (J[1] * J[6] - J[0] * J[7]);
24259d150b73SToby Isaac         invJ[8]  = idet * (J[0] * J[4] - J[1] * J[3]);
24269d150b73SToby Isaac       }
24279d150b73SToby Isaac       break;
24289d150b73SToby Isaac     }
24299d150b73SToby Isaac     for (l = 0; l < dimR; l++) {
24309d150b73SToby Isaac       for (m = 0; m < dimC; m++) {
2431c6e120d1SToby Isaac         guess[l] += PetscRealPart(invJ[l * dimC + m]) * resNeg[m];
24329d150b73SToby Isaac       }
24339d150b73SToby Isaac     }
24349d150b73SToby Isaac   } else {
24359d150b73SToby Isaac #if defined(PETSC_USE_COMPLEX)
24369d150b73SToby Isaac     char transpose = 'C';
24379d150b73SToby Isaac #else
24389d150b73SToby Isaac     char transpose = 'T';
24399d150b73SToby Isaac #endif
24409d150b73SToby Isaac     PetscBLASInt m = dimR;
24419d150b73SToby Isaac     PetscBLASInt n = dimC;
24429d150b73SToby Isaac     PetscBLASInt one = 1;
24439d150b73SToby Isaac     PetscBLASInt worksize = dimR * dimC, info;
24449d150b73SToby Isaac 
24459d150b73SToby Isaac     for (l = 0; l < dimC; l++) {invJ[l] = resNeg[l];}
24469d150b73SToby Isaac 
24479d150b73SToby Isaac     PetscStackCallBLAS("LAPACKgels",LAPACKgels_(&transpose,&m,&n,&one,J,&m,invJ,&n,work,&worksize, &info));
24489d150b73SToby Isaac     if (info != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad argument to GELS");
24499d150b73SToby Isaac 
2450c6e120d1SToby Isaac     for (l = 0; l < dimR; l++) {guess[l] += PetscRealPart(invJ[l]);}
24519d150b73SToby Isaac   }
24529d150b73SToby Isaac   PetscFunctionReturn(0);
24539d150b73SToby Isaac }
24549d150b73SToby Isaac 
24559d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
24569d150b73SToby Isaac {
2457c0cbe899SToby Isaac   PetscInt       coordSize, i, j, k, l, m, maxIts = 7, numV = (1 << dimR);
24589d150b73SToby Isaac   PetscScalar    *coordsScalar = NULL;
24599d150b73SToby Isaac   PetscReal      *cellData, *cellCoords, *cellCoeffs, *extJ, *resNeg;
24609d150b73SToby Isaac   PetscScalar    *J, *invJ, *work;
24619d150b73SToby Isaac   PetscErrorCode ierr;
24629d150b73SToby Isaac 
24639d150b73SToby Isaac   PetscFunctionBegin;
24649d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24659d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
24669d150b73SToby Isaac   if (coordSize < dimC * numV) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expecting at least %D coordinates, got %D",dimC * (1 << dimR), coordSize);CHKERRQ(ierr);
24679d150b73SToby Isaac   ierr = DMGetWorkArray(dm, 2 * coordSize + dimR + dimC, PETSC_REAL, &cellData);CHKERRQ(ierr);
24689d150b73SToby Isaac   ierr = DMGetWorkArray(dm, 3 * dimR * dimC, PETSC_SCALAR, &J);CHKERRQ(ierr);
24699d150b73SToby Isaac   cellCoords = &cellData[0];
24709d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
24719d150b73SToby Isaac   extJ       = &cellData[2 * coordSize];
24729d150b73SToby Isaac   resNeg     = &cellData[2 * coordSize + dimR];
24739d150b73SToby Isaac   invJ       = &J[dimR * dimC];
24749d150b73SToby Isaac   work       = &J[2 * dimR * dimC];
24759d150b73SToby Isaac   if (dimR == 2) {
24769d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
24779d150b73SToby Isaac 
24789d150b73SToby Isaac     for (i = 0; i < 4; i++) {
24799d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
24809d150b73SToby Isaac 
24819d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
24829d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
24839d150b73SToby Isaac       }
24849d150b73SToby Isaac     }
24859d150b73SToby Isaac   } else if (dimR == 3) {
24869d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
24879d150b73SToby Isaac 
24889d150b73SToby Isaac     for (i = 0; i < 8; i++) {
24899d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
24909d150b73SToby Isaac 
24919d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
24929d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
24939d150b73SToby Isaac       }
24949d150b73SToby Isaac     }
24959d150b73SToby Isaac   } else {
24969d150b73SToby Isaac     for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);}
24979d150b73SToby Isaac   }
24989d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
24999d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
25009d150b73SToby Isaac     PetscReal *swap;
25019d150b73SToby Isaac 
25029d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
25039d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
25049d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
25059d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
25069d150b73SToby Isaac       }
25079d150b73SToby Isaac     }
25089d150b73SToby Isaac 
25099d150b73SToby Isaac     if (i < dimR - 1) {
25109d150b73SToby Isaac       swap = cellCoeffs;
25119d150b73SToby Isaac       cellCoeffs = cellCoords;
25129d150b73SToby Isaac       cellCoords = swap;
25139d150b73SToby Isaac     }
25149d150b73SToby Isaac   }
25159d150b73SToby Isaac   ierr = PetscMemzero(refCoords,numPoints * dimR * sizeof (PetscReal));CHKERRQ(ierr);
25169d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
25179d150b73SToby Isaac     for (i = 0; i < maxIts; i++) {
25189d150b73SToby Isaac       PetscReal *guess = &refCoords[dimR * j];
25199d150b73SToby Isaac 
25209d150b73SToby Isaac       /* compute -residual and Jacobian */
25219d150b73SToby Isaac       for (k = 0; k < dimC; k++) {resNeg[k] = realCoords[dimC * j + k];}
25229d150b73SToby Isaac       for (k = 0; k < dimC * dimR; k++) {J[k] = 0.;}
25239d150b73SToby Isaac       for (k = 0; k < numV; k++) {
25249d150b73SToby Isaac         PetscReal extCoord = 1.;
25259d150b73SToby Isaac         for (l = 0; l < dimR; l++) {
25269d150b73SToby Isaac           PetscReal coord = guess[l];
25279d150b73SToby Isaac           PetscInt  dep   = (k & (1 << l)) >> l;
25289d150b73SToby Isaac 
25299d150b73SToby Isaac           extCoord *= dep * coord + !dep;
25309d150b73SToby Isaac           extJ[l] = dep;
25319d150b73SToby Isaac 
25329d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
25339d150b73SToby Isaac             PetscReal coord = guess[m];
25349d150b73SToby Isaac             PetscInt  dep   = ((k & (1 << m)) >> m) && (m != l);
25359d150b73SToby Isaac             PetscReal mult  = dep * coord + !dep;
25369d150b73SToby Isaac 
25379d150b73SToby Isaac             extJ[l] *= mult;
25389d150b73SToby Isaac           }
25399d150b73SToby Isaac         }
25409d150b73SToby Isaac         for (l = 0; l < dimC; l++) {
25419d150b73SToby Isaac           PetscReal coeff = cellCoeffs[dimC * k + l];
25429d150b73SToby Isaac 
25439d150b73SToby Isaac           resNeg[l] -= coeff * extCoord;
25449d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
25459d150b73SToby Isaac             J[dimR * l + m] += coeff * extJ[m];
25469d150b73SToby Isaac           }
25479d150b73SToby Isaac         }
25489d150b73SToby Isaac       }
25490611203eSToby Isaac #if 0 && defined(PETSC_USE_DEBUG)
25500611203eSToby Isaac       {
25510611203eSToby Isaac         PetscReal maxAbs = 0.;
25520611203eSToby Isaac 
25530611203eSToby Isaac         for (l = 0; l < dimC; l++) {
25540611203eSToby Isaac           maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l]));
25550611203eSToby Isaac         }
25560611203eSToby Isaac         ierr = PetscInfo4(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,maxAbs);CHKERRQ(ierr);
25570611203eSToby Isaac       }
25580611203eSToby Isaac #endif
25599d150b73SToby Isaac 
25609d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_NewtonUpdate(dimC,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr);
25619d150b73SToby Isaac     }
25629d150b73SToby Isaac   }
25639d150b73SToby Isaac   ierr = DMRestoreWorkArray(dm, 3 * dimR * dimC, PETSC_SCALAR, &J);CHKERRQ(ierr);
25649d150b73SToby Isaac   ierr = DMRestoreWorkArray(dm, 2 * coordSize + dimR + dimC, PETSC_REAL, &cellData);CHKERRQ(ierr);
25659d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
25669d150b73SToby Isaac   PetscFunctionReturn(0);
25679d150b73SToby Isaac }
25689d150b73SToby Isaac 
25699d150b73SToby Isaac static PetscErrorCode DMPlexReferenceToCoordinates_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
25709d150b73SToby Isaac {
25719d150b73SToby Isaac   PetscInt       coordSize, i, j, k, l, numV = (1 << dimR);
25729d150b73SToby Isaac   PetscScalar    *coordsScalar = NULL;
25739d150b73SToby Isaac   PetscReal      *cellData, *cellCoords, *cellCoeffs;
25749d150b73SToby Isaac   PetscErrorCode ierr;
25759d150b73SToby Isaac 
25769d150b73SToby Isaac   PetscFunctionBegin;
25779d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25789d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
25799d150b73SToby Isaac   if (coordSize < dimC * numV) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expecting at least %D coordinates, got %D",dimC * (1 << dimR), coordSize);CHKERRQ(ierr);
25809d150b73SToby Isaac   ierr = DMGetWorkArray(dm, 2 * coordSize, PETSC_REAL, &cellData);CHKERRQ(ierr);
25819d150b73SToby Isaac   cellCoords = &cellData[0];
25829d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
25839d150b73SToby Isaac   if (dimR == 2) {
25849d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
25859d150b73SToby Isaac 
25869d150b73SToby Isaac     for (i = 0; i < 4; i++) {
25879d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
25889d150b73SToby Isaac 
25899d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
25909d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
25919d150b73SToby Isaac       }
25929d150b73SToby Isaac     }
25939d150b73SToby Isaac   } else if (dimR == 3) {
25949d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
25959d150b73SToby Isaac 
25969d150b73SToby Isaac     for (i = 0; i < 8; i++) {
25979d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
25989d150b73SToby Isaac 
25999d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
26009d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
26019d150b73SToby Isaac       }
26029d150b73SToby Isaac     }
26039d150b73SToby Isaac   } else {
26049d150b73SToby Isaac     for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);}
26059d150b73SToby Isaac   }
26069d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
26079d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
26089d150b73SToby Isaac     PetscReal *swap;
26099d150b73SToby Isaac 
26109d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
26119d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
26129d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
26139d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
26149d150b73SToby Isaac       }
26159d150b73SToby Isaac     }
26169d150b73SToby Isaac 
26179d150b73SToby Isaac     if (i < dimR - 1) {
26189d150b73SToby Isaac       swap = cellCoeffs;
26199d150b73SToby Isaac       cellCoeffs = cellCoords;
26209d150b73SToby Isaac       cellCoords = swap;
26219d150b73SToby Isaac     }
26229d150b73SToby Isaac   }
26239d150b73SToby Isaac   ierr = PetscMemzero(realCoords,numPoints * dimC * sizeof (PetscReal));CHKERRQ(ierr);
26249d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
26259d150b73SToby Isaac     const PetscReal *guess  = &refCoords[dimR * j];
26269d150b73SToby Isaac     PetscReal       *mapped = &realCoords[dimC * j];
26279d150b73SToby Isaac 
26289d150b73SToby Isaac     for (k = 0; k < numV; k++) {
26299d150b73SToby Isaac       PetscReal extCoord = 1.;
26309d150b73SToby Isaac       for (l = 0; l < dimR; l++) {
26319d150b73SToby Isaac         PetscReal coord = guess[l];
26329d150b73SToby Isaac         PetscInt  dep   = (k & (1 << l)) >> l;
26339d150b73SToby Isaac 
26349d150b73SToby Isaac         extCoord *= dep * coord + !dep;
26359d150b73SToby Isaac       }
26369d150b73SToby Isaac       for (l = 0; l < dimC; l++) {
26379d150b73SToby Isaac         PetscReal coeff = cellCoeffs[dimC * k + l];
26389d150b73SToby Isaac 
26399d150b73SToby Isaac         mapped[l] += coeff * extCoord;
26409d150b73SToby Isaac       }
26419d150b73SToby Isaac     }
26429d150b73SToby Isaac   }
26439d150b73SToby Isaac   ierr = DMRestoreWorkArray(dm, 2 * coordSize, PETSC_REAL, &cellData);CHKERRQ(ierr);
26449d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
26459d150b73SToby Isaac   PetscFunctionReturn(0);
26469d150b73SToby Isaac }
26479d150b73SToby Isaac 
26489c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
26499c3cf19fSMatthew G. Knepley static PetscErrorCode DMPlexCoordinatesToReference_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt Nc, PetscInt dimR)
26509d150b73SToby Isaac {
26519c3cf19fSMatthew G. Knepley   PetscInt       numComp, pdim, i, j, k, l, m, maxIter = 7, coordSize;
2652c6e120d1SToby Isaac   PetscScalar    *nodes = NULL;
2653c6e120d1SToby Isaac   PetscReal      *invV, *modes;
2654c6e120d1SToby Isaac   PetscReal      *B, *D, *resNeg;
2655c6e120d1SToby Isaac   PetscScalar    *J, *invJ, *work;
26569d150b73SToby Isaac   PetscErrorCode ierr;
26579d150b73SToby Isaac 
26589d150b73SToby Isaac   PetscFunctionBegin;
26599c3cf19fSMatthew G. Knepley   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
26609d150b73SToby Isaac   ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
26619c3cf19fSMatthew G. Knepley   if (numComp != Nc) SETERRQ2(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"coordinate discretization must have as many components (%D) as embedding dimension (!= %D)",numComp,Nc);
26629d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
26639d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
2664012b7cc6SMatthew G. Knepley   ierr = DMGetWorkArray(dm,pdim,PETSC_REAL,&modes);CHKERRQ(ierr);
26659d150b73SToby Isaac   invV = fe->invV;
2666012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
2667012b7cc6SMatthew G. Knepley     modes[i] = 0.;
2668012b7cc6SMatthew G. Knepley     for (j = 0; j < pdim; ++j) {
2669012b7cc6SMatthew G. Knepley       modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]);
26709d150b73SToby Isaac     }
26719d150b73SToby Isaac   }
26729c3cf19fSMatthew G. Knepley   ierr   = DMGetWorkArray(dm,pdim * Nc + pdim * Nc * dimR + Nc,PETSC_REAL,&B);CHKERRQ(ierr);
26739c3cf19fSMatthew G. Knepley   D      = &B[pdim*Nc];
26749c3cf19fSMatthew G. Knepley   resNeg = &D[pdim*Nc * dimR];
26759c3cf19fSMatthew G. Knepley   ierr = DMGetWorkArray(dm,3 * Nc * dimR,PETSC_SCALAR,&J);CHKERRQ(ierr);
26769c3cf19fSMatthew G. Knepley   invJ = &J[Nc * dimR];
26779c3cf19fSMatthew G. Knepley   work = &invJ[Nc * dimR];
26789d150b73SToby Isaac   for (i = 0; i < numPoints * dimR; i++) {refCoords[i] = 0.;}
26799d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
26809b1f03cbSToby Isaac       for (i = 0; i < maxIter; i++) { /* we could batch this so that we're not making big B and D arrays all the time */
26819d150b73SToby Isaac       PetscReal *guess = &refCoords[j * dimR];
26829d150b73SToby Isaac       ierr = PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, D, NULL);CHKERRQ(ierr);
26839c3cf19fSMatthew G. Knepley       for (k = 0; k < Nc; k++) {resNeg[k] = realCoords[j * Nc + k];}
26849c3cf19fSMatthew G. Knepley       for (k = 0; k < Nc * dimR; k++) {J[k] = 0.;}
26859c3cf19fSMatthew G. Knepley       for (k = 0; k < pdim; k++) {
26869c3cf19fSMatthew G. Knepley         for (l = 0; l < Nc; l++) {
2687012b7cc6SMatthew G. Knepley           resNeg[l] -= modes[k] * B[k * Nc + l];
26889d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
2689012b7cc6SMatthew G. Knepley             J[l * dimR + m] += modes[k] * D[(k * Nc + l) * dimR + m];
26909d150b73SToby Isaac           }
26919d150b73SToby Isaac         }
26929d150b73SToby Isaac       }
26930611203eSToby Isaac #if 0 && defined(PETSC_USE_DEBUG)
26940611203eSToby Isaac       {
26950611203eSToby Isaac         PetscReal maxAbs = 0.;
26960611203eSToby Isaac 
26979c3cf19fSMatthew G. Knepley         for (l = 0; l < Nc; l++) {
26980611203eSToby Isaac           maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l]));
26990611203eSToby Isaac         }
27000611203eSToby Isaac         ierr = PetscInfo4(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,maxAbs);CHKERRQ(ierr);
27010611203eSToby Isaac       }
27020611203eSToby Isaac #endif
27039c3cf19fSMatthew G. Knepley       ierr = DMPlexCoordinatesToReference_NewtonUpdate(Nc,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr);
27049d150b73SToby Isaac     }
27059d150b73SToby Isaac   }
27069c3cf19fSMatthew G. Knepley   ierr = DMRestoreWorkArray(dm,3 * Nc * dimR,PETSC_SCALAR,&J);CHKERRQ(ierr);
2707012b7cc6SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm,pdim * Nc + pdim * Nc * dimR + Nc,PETSC_REAL,&B);CHKERRQ(ierr);
2708012b7cc6SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm,pdim,PETSC_REAL,&modes);CHKERRQ(ierr);
27099d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
27109d150b73SToby Isaac   PetscFunctionReturn(0);
27119d150b73SToby Isaac }
27129d150b73SToby Isaac 
27139c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
27149c3cf19fSMatthew G. Knepley static PetscErrorCode DMPlexReferenceToCoordinates_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt Nc, PetscInt dimR)
27159d150b73SToby Isaac {
27169c3cf19fSMatthew G. Knepley   PetscInt       numComp, pdim, i, j, k, l, coordSize;
2717c6e120d1SToby Isaac   PetscScalar    *nodes = NULL;
2718c6e120d1SToby Isaac   PetscReal      *invV, *modes;
27199d150b73SToby Isaac   PetscReal      *B;
27209d150b73SToby Isaac   PetscErrorCode ierr;
27219d150b73SToby Isaac 
27229d150b73SToby Isaac   PetscFunctionBegin;
27239c3cf19fSMatthew G. Knepley   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
27249d150b73SToby Isaac   ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
27259c3cf19fSMatthew G. Knepley   if (numComp != Nc) SETERRQ2(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"coordinate discretization must have as many components (%D) as embedding dimension (!= %D)",numComp,Nc);
27269d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
27279d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
2728012b7cc6SMatthew G. Knepley   ierr = DMGetWorkArray(dm,pdim,PETSC_REAL,&modes);CHKERRQ(ierr);
27299d150b73SToby Isaac   invV = fe->invV;
2730012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
2731012b7cc6SMatthew G. Knepley     modes[i] = 0.;
2732012b7cc6SMatthew G. Knepley     for (j = 0; j < pdim; ++j) {
2733012b7cc6SMatthew G. Knepley       modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]);
27349d150b73SToby Isaac     }
27359d150b73SToby Isaac   }
2736012b7cc6SMatthew G. Knepley   ierr = DMGetWorkArray(dm,numPoints * pdim * Nc,PETSC_REAL,&B);CHKERRQ(ierr);
2737012b7cc6SMatthew G. Knepley   ierr = PetscSpaceEvaluate(fe->basisSpace, numPoints, refCoords, B, NULL, NULL);CHKERRQ(ierr);
27389c3cf19fSMatthew G. Knepley   for (i = 0; i < numPoints * Nc; i++) {realCoords[i] = 0.;}
27399d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
27409c3cf19fSMatthew G. Knepley     PetscReal *mapped = &realCoords[j * Nc];
27419d150b73SToby Isaac 
27429c3cf19fSMatthew G. Knepley     for (k = 0; k < pdim; k++) {
27439c3cf19fSMatthew G. Knepley       for (l = 0; l < Nc; l++) {
274440cf36b3SToby Isaac         mapped[l] += modes[k] * B[(j * pdim + k) * Nc + l];
27459d150b73SToby Isaac       }
27469d150b73SToby Isaac     }
27479d150b73SToby Isaac   }
2748012b7cc6SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm,numPoints * pdim * Nc,PETSC_REAL,&B);CHKERRQ(ierr);
2749012b7cc6SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm,pdim,PETSC_REAL,&modes);CHKERRQ(ierr);
27509d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
27519d150b73SToby Isaac   PetscFunctionReturn(0);
27529d150b73SToby Isaac }
27539d150b73SToby Isaac 
2754d6143a4eSToby Isaac /*@
2755d6143a4eSToby Isaac   DMPlexCoordinatesToReference - Pull coordinates back from the mesh to the reference element using a single element
2756d6143a4eSToby Isaac   map.  This inversion will be accurate inside the reference element, but may be inaccurate for mappings that do not
2757d6143a4eSToby Isaac   extend uniquely outside the reference cell (e.g, most non-affine maps)
2758d6143a4eSToby Isaac 
2759d6143a4eSToby Isaac   Not collective
2760d6143a4eSToby Isaac 
2761d6143a4eSToby Isaac   Input Parameters:
2762d6143a4eSToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
2763d6143a4eSToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
2764d6143a4eSToby Isaac                as a multilinear map for tensor-product elements
2765d6143a4eSToby Isaac . cell       - the cell whose map is used.
2766d6143a4eSToby Isaac . numPoints  - the number of points to locate
27671b266c99SBarry Smith - realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
2768d6143a4eSToby Isaac 
2769d6143a4eSToby Isaac   Output Parameters:
2770d6143a4eSToby Isaac . refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
27711b266c99SBarry Smith 
27721b266c99SBarry Smith   Level: intermediate
2773d6143a4eSToby Isaac @*/
2774d6143a4eSToby Isaac PetscErrorCode DMPlexCoordinatesToReference(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[])
2775d6143a4eSToby Isaac {
27769d150b73SToby Isaac   PetscInt       dimC, dimR, depth, cStart, cEnd, cEndInterior, i;
27779d150b73SToby Isaac   DM             coordDM = NULL;
27789d150b73SToby Isaac   Vec            coords;
27799d150b73SToby Isaac   PetscFE        fe = NULL;
27809d150b73SToby Isaac   PetscErrorCode ierr;
27819d150b73SToby Isaac 
2782d6143a4eSToby Isaac   PetscFunctionBegin;
27839d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27849d150b73SToby Isaac   ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr);
27859d150b73SToby Isaac   ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr);
27869d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
27879d150b73SToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
27889d150b73SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
27899d150b73SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr);
27909d150b73SToby Isaac   if (coordDM) {
27919d150b73SToby Isaac     PetscInt coordFields;
27929d150b73SToby Isaac 
27939d150b73SToby Isaac     ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr);
27949d150b73SToby Isaac     if (coordFields) {
27959d150b73SToby Isaac       PetscClassId id;
27969d150b73SToby Isaac       PetscObject  disc;
27979d150b73SToby Isaac 
27989d150b73SToby Isaac       ierr = DMGetField(coordDM,0,&disc);CHKERRQ(ierr);
27999d150b73SToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
28009d150b73SToby Isaac       if (id == PETSCFE_CLASSID) {
28019d150b73SToby Isaac         fe = (PetscFE) disc;
28029d150b73SToby Isaac       }
28039d150b73SToby Isaac     }
28049d150b73SToby Isaac   }
28059d150b73SToby Isaac   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
28069d150b73SToby Isaac   ierr = DMPlexGetHybridBounds(dm,&cEndInterior,NULL,NULL,NULL);CHKERRQ(ierr);
28079d150b73SToby Isaac   cEnd = cEndInterior > 0 ? cEndInterior : cEnd;
28089d150b73SToby Isaac   if (cell < cStart || cell >= cEnd) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"point %D not in cell range [%D,%D)",cell,cStart,cEnd);CHKERRQ(ierr);
28099d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
28109d150b73SToby Isaac     PetscInt  coneSize;
28119d150b73SToby Isaac     PetscBool isSimplex, isTensor;
28129d150b73SToby Isaac 
28139d150b73SToby Isaac     ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
28149d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
28159d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
28169d150b73SToby Isaac     if (isSimplex) {
28179d150b73SToby Isaac       PetscReal detJ, *v0, *J, *invJ;
28189d150b73SToby Isaac 
28199d150b73SToby Isaac       ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr);
28209d150b73SToby Isaac       J    = &v0[dimC];
28219d150b73SToby Isaac       invJ = &J[dimC * dimC];
28229d150b73SToby Isaac       ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, &detJ);CHKERRQ(ierr);
28239d150b73SToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */
28249d150b73SToby Isaac         CoordinatesRealToRef(dimC, dimR, v0, invJ, &realCoords[dimC * i], &refCoords[dimR * i]);
28259d150b73SToby Isaac       }
28269d150b73SToby Isaac       ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr);
28279d150b73SToby Isaac     } else if (isTensor) {
28289d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_Tensor(coordDM, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr);
28299d150b73SToby Isaac     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize);
28309d150b73SToby Isaac   } else {
28319d150b73SToby Isaac     ierr = DMPlexCoordinatesToReference_FE(coordDM, fe, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr);
28329d150b73SToby Isaac   }
28339d150b73SToby Isaac   PetscFunctionReturn(0);
28349d150b73SToby Isaac }
28359d150b73SToby Isaac 
28369d150b73SToby Isaac /*@
28379d150b73SToby Isaac   DMPlexReferenceToCoordinates - Map references coordinates to coordinates in the the mesh for a single element map.
28389d150b73SToby Isaac 
28399d150b73SToby Isaac   Not collective
28409d150b73SToby Isaac 
28419d150b73SToby Isaac   Input Parameters:
28429d150b73SToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
28439d150b73SToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
28449d150b73SToby Isaac                as a multilinear map for tensor-product elements
28459d150b73SToby Isaac . cell       - the cell whose map is used.
28469d150b73SToby Isaac . numPoints  - the number of points to locate
28479d150b73SToby Isaac + refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
28489d150b73SToby Isaac 
28499d150b73SToby Isaac   Output Parameters:
28509d150b73SToby Isaac . realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
28511b266c99SBarry Smith 
28521b266c99SBarry Smith    Level: intermediate
28539d150b73SToby Isaac @*/
28549d150b73SToby Isaac PetscErrorCode DMPlexReferenceToCoordinates(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[])
28559d150b73SToby Isaac {
28569d150b73SToby Isaac   PetscInt       dimC, dimR, depth, cStart, cEnd, cEndInterior, i;
28579d150b73SToby Isaac   DM             coordDM = NULL;
28589d150b73SToby Isaac   Vec            coords;
28599d150b73SToby Isaac   PetscFE        fe = NULL;
28609d150b73SToby Isaac   PetscErrorCode ierr;
28619d150b73SToby Isaac 
28629d150b73SToby Isaac   PetscFunctionBegin;
28639d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28649d150b73SToby Isaac   ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr);
28659d150b73SToby Isaac   ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr);
28669d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
28679d150b73SToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
28689d150b73SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
28699d150b73SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr);
28709d150b73SToby Isaac   if (coordDM) {
28719d150b73SToby Isaac     PetscInt coordFields;
28729d150b73SToby Isaac 
28739d150b73SToby Isaac     ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr);
28749d150b73SToby Isaac     if (coordFields) {
28759d150b73SToby Isaac       PetscClassId id;
28769d150b73SToby Isaac       PetscObject  disc;
28779d150b73SToby Isaac 
28789d150b73SToby Isaac       ierr = DMGetField(coordDM,0,&disc);CHKERRQ(ierr);
28799d150b73SToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
28809d150b73SToby Isaac       if (id == PETSCFE_CLASSID) {
28819d150b73SToby Isaac         fe = (PetscFE) disc;
28829d150b73SToby Isaac       }
28839d150b73SToby Isaac     }
28849d150b73SToby Isaac   }
28859d150b73SToby Isaac   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
28869d150b73SToby Isaac   ierr = DMPlexGetHybridBounds(dm,&cEndInterior,NULL,NULL,NULL);CHKERRQ(ierr);
28879d150b73SToby Isaac   cEnd = cEndInterior > 0 ? cEndInterior : cEnd;
28889d150b73SToby Isaac   if (cell < cStart || cell >= cEnd) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"point %D not in cell range [%D,%D)",cell,cStart,cEnd);CHKERRQ(ierr);
28899d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
28909d150b73SToby Isaac     PetscInt  coneSize;
28919d150b73SToby Isaac     PetscBool isSimplex, isTensor;
28929d150b73SToby Isaac 
28939d150b73SToby Isaac     ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
28949d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
28959d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
28969d150b73SToby Isaac     if (isSimplex) {
28979d150b73SToby Isaac       PetscReal detJ, *v0, *J;
28989d150b73SToby Isaac 
28999d150b73SToby Isaac       ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr);
29009d150b73SToby Isaac       J    = &v0[dimC];
29019d150b73SToby Isaac       ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, NULL, &detJ);CHKERRQ(ierr);
29029d150b73SToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */
29039d150b73SToby Isaac         CoordinatesRefToReal(dimC, dimR, v0, J, &refCoords[dimR * i], &realCoords[dimC * i]);
29049d150b73SToby Isaac       }
29059d150b73SToby Isaac       ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr);
29069d150b73SToby Isaac     } else if (isTensor) {
29079d150b73SToby Isaac       ierr = DMPlexReferenceToCoordinates_Tensor(coordDM, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr);
29089d150b73SToby Isaac     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize);
29099d150b73SToby Isaac   } else {
29109d150b73SToby Isaac     ierr = DMPlexReferenceToCoordinates_FE(coordDM, fe, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr);
29119d150b73SToby Isaac   }
2912d6143a4eSToby Isaac   PetscFunctionReturn(0);
2913d6143a4eSToby Isaac }
2914