xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision 2a705cacaa405366b3f06c44ba555e114b913fbc)
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;
286*2a705cacSMatthew 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];
871834e62ceSMatthew G. Knepley   PetscReal       M[9], detM;
872834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2; M[2] = x3;
873834e62ceSMatthew G. Knepley   M[3] = y1; M[4] = y2; M[5] = y3;
874834e62ceSMatthew G. Knepley   M[6] = z1; M[7] = z2; M[8] = z3;
875923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(&detM, M);
876b7ad821dSMatthew G. Knepley   *vol = -0.16666666666666666666666*detM;
8773bc0b13bSBarry Smith   (void)PetscLogFlops(10.0);
878834e62ceSMatthew G. Knepley }
879834e62ceSMatthew G. Knepley 
8800ec8681fSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[])
8810ec8681fSMatthew G. Knepley {
882923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(vol, coords);
883b7ad821dSMatthew G. Knepley   *vol *= -0.16666666666666666666666;
8840ec8681fSMatthew G. Knepley }
8850ec8681fSMatthew G. Knepley 
886cb92db44SToby Isaac static PetscErrorCode DMPlexComputePointGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
887cb92db44SToby Isaac {
888cb92db44SToby Isaac   PetscSection   coordSection;
889cb92db44SToby Isaac   Vec            coordinates;
890cb92db44SToby Isaac   const PetscScalar *coords;
891cb92db44SToby Isaac   PetscInt       dim, d, off;
892cb92db44SToby Isaac   PetscErrorCode ierr;
893cb92db44SToby Isaac 
894cb92db44SToby Isaac   PetscFunctionBegin;
895cb92db44SToby Isaac   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
896cb92db44SToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
897cb92db44SToby Isaac   ierr = PetscSectionGetDof(coordSection,e,&dim);CHKERRQ(ierr);
898cb92db44SToby Isaac   if (!dim) PetscFunctionReturn(0);
899cb92db44SToby Isaac   ierr = PetscSectionGetOffset(coordSection,e,&off);CHKERRQ(ierr);
900cb92db44SToby Isaac   ierr = VecGetArrayRead(coordinates,&coords);CHKERRQ(ierr);
901cb92db44SToby Isaac   if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[off + d]);}
902cb92db44SToby Isaac   ierr = VecRestoreArrayRead(coordinates,&coords);CHKERRQ(ierr);
903cb92db44SToby Isaac   *detJ = 1.;
904cb92db44SToby Isaac   if (J) {
905cb92db44SToby Isaac     for (d = 0; d < dim * dim; d++) J[d] = 0.;
906cb92db44SToby Isaac     for (d = 0; d < dim; d++) J[d * dim + d] = 1.;
907cb92db44SToby Isaac     if (invJ) {
908cb92db44SToby Isaac       for (d = 0; d < dim * dim; d++) invJ[d] = 0.;
909cb92db44SToby Isaac       for (d = 0; d < dim; d++) invJ[d * dim + d] = 1.;
910cb92db44SToby Isaac     }
911cb92db44SToby Isaac   }
912cb92db44SToby Isaac   PetscFunctionReturn(0);
913cb92db44SToby Isaac }
914cb92db44SToby Isaac 
91517fe8556SMatthew G. Knepley static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
91617fe8556SMatthew G. Knepley {
91717fe8556SMatthew G. Knepley   PetscSection   coordSection;
91817fe8556SMatthew G. Knepley   Vec            coordinates;
919a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
9208bf5c034SToby Isaac   PetscInt       numCoords, d, pStart, pEnd, numSelfCoords = 0;
92117fe8556SMatthew G. Knepley   PetscErrorCode ierr;
92217fe8556SMatthew G. Knepley 
92317fe8556SMatthew G. Knepley   PetscFunctionBegin;
92417fe8556SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
92569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
9268bf5c034SToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
9278bf5c034SToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
92817fe8556SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
9298bf5c034SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
930adac9986SMatthew G. Knepley   if (invJ && !J) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
9317f07f362SMatthew G. Knepley   *detJ = 0.0;
93228dbe442SToby Isaac   if (numCoords == 6) {
93328dbe442SToby Isaac     const PetscInt dim = 3;
93428dbe442SToby Isaac     PetscReal      R[9], J0;
93528dbe442SToby Isaac 
93628dbe442SToby Isaac     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
937741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto1D(coords, R);CHKERRQ(ierr);
93828dbe442SToby Isaac     if (J)    {
93928dbe442SToby Isaac       J0   = 0.5*PetscRealPart(coords[1]);
94028dbe442SToby Isaac       J[0] = R[0]*J0; J[1] = R[1]; J[2] = R[2];
94128dbe442SToby Isaac       J[3] = R[3]*J0; J[4] = R[4]; J[5] = R[5];
94228dbe442SToby Isaac       J[6] = R[6]*J0; J[7] = R[7]; J[8] = R[8];
94328dbe442SToby Isaac       DMPlex_Det3D_Internal(detJ, J);
94428dbe442SToby Isaac       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
945adac9986SMatthew G. Knepley     }
94628dbe442SToby Isaac   } else if (numCoords == 4) {
9477f07f362SMatthew G. Knepley     const PetscInt dim = 2;
9487f07f362SMatthew G. Knepley     PetscReal      R[4], J0;
9497f07f362SMatthew G. Knepley 
9507f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
951741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection2Dto1D(coords, R);CHKERRQ(ierr);
95217fe8556SMatthew G. Knepley     if (J)    {
9537f07f362SMatthew G. Knepley       J0   = 0.5*PetscRealPart(coords[1]);
9547f07f362SMatthew G. Knepley       J[0] = R[0]*J0; J[1] = R[1];
9557f07f362SMatthew G. Knepley       J[2] = R[2]*J0; J[3] = R[3];
956923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
957923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
958adac9986SMatthew G. Knepley     }
9597f07f362SMatthew G. Knepley   } else if (numCoords == 2) {
9607f07f362SMatthew G. Knepley     const PetscInt dim = 1;
9617f07f362SMatthew G. Knepley 
9627f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
9637f07f362SMatthew G. Knepley     if (J)    {
9647f07f362SMatthew G. Knepley       J[0]  = 0.5*(PetscRealPart(coords[1]) - PetscRealPart(coords[0]));
96517fe8556SMatthew G. Knepley       *detJ = J[0];
9663bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0);CHKERRQ(ierr);
9673bc0b13bSBarry Smith       if (invJ) {invJ[0] = 1.0/J[0]; ierr = PetscLogFlops(1.0);CHKERRQ(ierr);}
968adac9986SMatthew G. Knepley     }
969796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this segment is %D != 2", numCoords);
97017fe8556SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
97117fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
97217fe8556SMatthew G. Knepley }
97317fe8556SMatthew G. Knepley 
974ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
975ccd2543fSMatthew G Knepley {
976ccd2543fSMatthew G Knepley   PetscSection   coordSection;
977ccd2543fSMatthew G Knepley   Vec            coordinates;
978a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
9797f07f362SMatthew G. Knepley   PetscInt       numCoords, d, f, g;
980ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
981ccd2543fSMatthew G Knepley 
982ccd2543fSMatthew G Knepley   PetscFunctionBegin;
983ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
98469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
985ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
9867f07f362SMatthew G. Knepley   *detJ = 0.0;
987ccd2543fSMatthew G Knepley   if (numCoords == 9) {
9887f07f362SMatthew G. Knepley     const PetscInt dim = 3;
9897f07f362SMatthew 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};
9907f07f362SMatthew G. Knepley 
9917f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
992741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
9937f07f362SMatthew G. Knepley     if (J)    {
994b7ad821dSMatthew G. Knepley       const PetscInt pdim = 2;
995b7ad821dSMatthew G. Knepley 
996b7ad821dSMatthew G. Knepley       for (d = 0; d < pdim; d++) {
997b7ad821dSMatthew G. Knepley         for (f = 0; f < pdim; f++) {
998b7ad821dSMatthew G. Knepley           J0[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
999ccd2543fSMatthew G Knepley         }
10007f07f362SMatthew G. Knepley       }
10013bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1002923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
10037f07f362SMatthew G. Knepley       for (d = 0; d < dim; d++) {
10047f07f362SMatthew G. Knepley         for (f = 0; f < dim; f++) {
10057f07f362SMatthew G. Knepley           J[d*dim+f] = 0.0;
10067f07f362SMatthew G. Knepley           for (g = 0; g < dim; g++) {
10077f07f362SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
10087f07f362SMatthew G. Knepley           }
10097f07f362SMatthew G. Knepley         }
10107f07f362SMatthew G. Knepley       }
10113bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
10127f07f362SMatthew G. Knepley     }
1013923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
10147f07f362SMatthew G. Knepley   } else if (numCoords == 6) {
10157f07f362SMatthew G. Knepley     const PetscInt dim = 2;
10167f07f362SMatthew G. Knepley 
10177f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1018ccd2543fSMatthew G Knepley     if (J)    {
1019ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1020ccd2543fSMatthew G Knepley         for (f = 0; f < dim; f++) {
1021ccd2543fSMatthew G Knepley           J[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*dim+d]) - PetscRealPart(coords[0*dim+d]));
1022ccd2543fSMatthew G Knepley         }
1023ccd2543fSMatthew G Knepley       }
10243bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1025923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1026ccd2543fSMatthew G Knepley     }
1027923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1028796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %D != 6 or 9", numCoords);
1029ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1030ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1031ccd2543fSMatthew G Knepley }
1032ccd2543fSMatthew G Knepley 
1033dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1034ccd2543fSMatthew G Knepley {
1035ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1036ccd2543fSMatthew G Knepley   Vec            coordinates;
1037a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
10380d29256aSToby Isaac   PetscInt       numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd;
1039ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1040ccd2543fSMatthew G Knepley 
1041ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1042ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
104369d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
10440d29256aSToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
10450d29256aSToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
104699dec3a6SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
104771f58de1SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
1048dfccc68fSToby Isaac   if (!Nq) {
10497f07f362SMatthew G. Knepley     *detJ = 0.0;
105099dec3a6SMatthew G. Knepley     if (numCoords == 12) {
105199dec3a6SMatthew G. Knepley       const PetscInt dim = 3;
105299dec3a6SMatthew 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};
105399dec3a6SMatthew G. Knepley 
1054dfccc68fSToby Isaac       if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1055741bfc07SMatthew G. Knepley       ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
105699dec3a6SMatthew G. Knepley       if (J)    {
105799dec3a6SMatthew G. Knepley         const PetscInt pdim = 2;
105899dec3a6SMatthew G. Knepley 
105999dec3a6SMatthew G. Knepley         for (d = 0; d < pdim; d++) {
106099dec3a6SMatthew G. Knepley           J0[d*dim+0] = 0.5*(PetscRealPart(coords[1*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
106199dec3a6SMatthew G. Knepley           J0[d*dim+1] = 0.5*(PetscRealPart(coords[3*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
106299dec3a6SMatthew G. Knepley         }
10633bc0b13bSBarry Smith         ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1064923591dfSMatthew G. Knepley         DMPlex_Det3D_Internal(detJ, J0);
106599dec3a6SMatthew G. Knepley         for (d = 0; d < dim; d++) {
106699dec3a6SMatthew G. Knepley           for (f = 0; f < dim; f++) {
106799dec3a6SMatthew G. Knepley             J[d*dim+f] = 0.0;
106899dec3a6SMatthew G. Knepley             for (g = 0; g < dim; g++) {
106999dec3a6SMatthew G. Knepley               J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
107099dec3a6SMatthew G. Knepley             }
107199dec3a6SMatthew G. Knepley           }
107299dec3a6SMatthew G. Knepley         }
10733bc0b13bSBarry Smith         ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
107499dec3a6SMatthew G. Knepley       }
1075923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
107671f58de1SToby Isaac     } else if (numCoords == 8) {
107799dec3a6SMatthew G. Knepley       const PetscInt dim = 2;
107899dec3a6SMatthew G. Knepley 
1079dfccc68fSToby Isaac       if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1080ccd2543fSMatthew G Knepley       if (J)    {
1081ccd2543fSMatthew G Knepley         for (d = 0; d < dim; d++) {
108299dec3a6SMatthew G. Knepley           J[d*dim+0] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
108399dec3a6SMatthew G. Knepley           J[d*dim+1] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1084ccd2543fSMatthew G Knepley         }
10853bc0b13bSBarry Smith         ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1086923591dfSMatthew G. Knepley         DMPlex_Det2D_Internal(detJ, J);
1087ccd2543fSMatthew G Knepley       }
1088923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1089796f034aSJed Brown     } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
1090dfccc68fSToby Isaac   } else {
1091dfccc68fSToby Isaac     const PetscInt Nv = 4;
1092dfccc68fSToby Isaac     const PetscInt dimR = 2;
1093dfccc68fSToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
1094dfccc68fSToby Isaac     PetscReal zOrder[12];
1095dfccc68fSToby Isaac     PetscReal zCoeff[12];
1096dfccc68fSToby Isaac     PetscInt  i, j, k, l, dim;
1097dfccc68fSToby Isaac 
1098dfccc68fSToby Isaac     if (numCoords == 12) {
1099dfccc68fSToby Isaac       dim = 3;
1100dfccc68fSToby Isaac     } else if (numCoords == 8) {
1101dfccc68fSToby Isaac       dim = 2;
1102dfccc68fSToby Isaac     } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
1103dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1104dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1105dfccc68fSToby Isaac 
1106dfccc68fSToby Isaac       for (j = 0; j < dim; j++) {
1107dfccc68fSToby Isaac         zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1108dfccc68fSToby Isaac       }
1109dfccc68fSToby Isaac     }
1110dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1111dfccc68fSToby Isaac       zCoeff[dim * 0 + j] = 0.25 * (  zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1112dfccc68fSToby Isaac       zCoeff[dim * 1 + j] = 0.25 * (- zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1113dfccc68fSToby Isaac       zCoeff[dim * 2 + j] = 0.25 * (- zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1114dfccc68fSToby Isaac       zCoeff[dim * 3 + j] = 0.25 * (  zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1115dfccc68fSToby Isaac     }
1116dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1117dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1];
1118dfccc68fSToby Isaac 
1119dfccc68fSToby Isaac       if (v) {
1120dfccc68fSToby Isaac         PetscReal extPoint[4];
1121dfccc68fSToby Isaac 
1122dfccc68fSToby Isaac         extPoint[0] = 1.;
1123dfccc68fSToby Isaac         extPoint[1] = xi;
1124dfccc68fSToby Isaac         extPoint[2] = eta;
1125dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1126dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1127dfccc68fSToby Isaac           PetscReal val = 0.;
1128dfccc68fSToby Isaac 
1129dfccc68fSToby Isaac           for (k = 0; k < Nv; k++) {
1130dfccc68fSToby Isaac             val += extPoint[k] * zCoeff[dim * k + j];
1131dfccc68fSToby Isaac           }
1132dfccc68fSToby Isaac           v[i * dim + j] = val;
1133dfccc68fSToby Isaac         }
1134dfccc68fSToby Isaac       }
1135dfccc68fSToby Isaac       if (J) {
1136dfccc68fSToby Isaac         PetscReal extJ[8];
1137dfccc68fSToby Isaac 
1138dfccc68fSToby Isaac         extJ[0] = 0.;
1139dfccc68fSToby Isaac         extJ[1] = 0.;
1140dfccc68fSToby Isaac         extJ[2] = 1.;
1141dfccc68fSToby Isaac         extJ[3] = 0.;
1142dfccc68fSToby Isaac         extJ[4] = 0.;
1143dfccc68fSToby Isaac         extJ[5] = 1.;
1144dfccc68fSToby Isaac         extJ[6] = eta;
1145dfccc68fSToby Isaac         extJ[7] = xi;
1146dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1147dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1148dfccc68fSToby Isaac             PetscReal val = 0.;
1149dfccc68fSToby Isaac 
1150dfccc68fSToby Isaac             for (l = 0; l < Nv; l++) {
1151dfccc68fSToby Isaac               val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1152dfccc68fSToby Isaac             }
1153dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1154dfccc68fSToby Isaac           }
1155dfccc68fSToby Isaac         }
1156dfccc68fSToby Isaac         if (dim == 3) { /* put the cross product in the third component of the Jacobian */
1157dfccc68fSToby Isaac           PetscReal x, y, z;
1158dfccc68fSToby Isaac           PetscReal *iJ = &J[i * dim * dim];
1159dfccc68fSToby Isaac           PetscReal norm;
1160dfccc68fSToby Isaac 
1161dfccc68fSToby Isaac           x = iJ[1 * dim + 0] * iJ[2 * dim + 1] - iJ[1 * dim + 1] * iJ[2 * dim + 0];
1162dfccc68fSToby Isaac           y = iJ[0 * dim + 1] * iJ[2 * dim + 0] - iJ[0 * dim + 0] * iJ[2 * dim + 1];
1163dfccc68fSToby Isaac           z = iJ[0 * dim + 0] * iJ[1 * dim + 1] - iJ[0 * dim + 1] * iJ[1 * dim + 0];
1164dfccc68fSToby Isaac           norm = PetscSqrtReal(x * x + y * y + z * z);
1165dfccc68fSToby Isaac           iJ[2] = x / norm;
1166dfccc68fSToby Isaac           iJ[5] = y / norm;
1167dfccc68fSToby Isaac           iJ[8] = z / norm;
1168dfccc68fSToby Isaac           DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1169dfccc68fSToby Isaac           if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1170dfccc68fSToby Isaac         } else {
1171dfccc68fSToby Isaac           DMPlex_Det2D_Internal(&detJ[i], &J[i * dim * dim]);
1172dfccc68fSToby Isaac           if (invJ) {DMPlex_Invert2D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1173dfccc68fSToby Isaac         }
1174dfccc68fSToby Isaac       }
1175dfccc68fSToby Isaac     }
1176dfccc68fSToby Isaac   }
117799dec3a6SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1178ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1179ccd2543fSMatthew G Knepley }
1180ccd2543fSMatthew G Knepley 
1181ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1182ccd2543fSMatthew G Knepley {
1183ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1184ccd2543fSMatthew G Knepley   Vec            coordinates;
1185a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1186ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
118799dec3a6SMatthew G. Knepley   PetscInt       d;
1188ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1189ccd2543fSMatthew G Knepley 
1190ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1191ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
119269d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1193ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
11947f07f362SMatthew G. Knepley   *detJ = 0.0;
11957f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1196ccd2543fSMatthew G Knepley   if (J)    {
1197ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1198f0df753eSMatthew G. Knepley       /* I orient with outward face normals */
1199f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d]));
1200f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1201f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1202ccd2543fSMatthew G Knepley     }
12033bc0b13bSBarry Smith     ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1204923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1205ccd2543fSMatthew G Knepley   }
1206923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1207ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1208ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1209ccd2543fSMatthew G Knepley }
1210ccd2543fSMatthew G Knepley 
1211dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1212ccd2543fSMatthew G Knepley {
1213ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1214ccd2543fSMatthew G Knepley   Vec            coordinates;
1215a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1216ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
1217ccd2543fSMatthew G Knepley   PetscInt       d;
1218ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1219ccd2543fSMatthew G Knepley 
1220ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1221ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
122269d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1223ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1224dfccc68fSToby Isaac   if (!Nq) {
12257f07f362SMatthew G. Knepley     *detJ = 0.0;
1226dfccc68fSToby Isaac     if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1227ccd2543fSMatthew G Knepley     if (J)    {
1228ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1229f0df753eSMatthew G. Knepley         J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1230f0df753eSMatthew G. Knepley         J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1231f0df753eSMatthew G. Knepley         J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d]));
1232ccd2543fSMatthew G Knepley       }
12333bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1234923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J);
1235ccd2543fSMatthew G Knepley     }
1236923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1237dfccc68fSToby Isaac   } else {
1238dfccc68fSToby Isaac     const PetscInt Nv = 8;
1239dfccc68fSToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
1240dfccc68fSToby Isaac     const PetscInt dim = 3;
1241dfccc68fSToby Isaac     const PetscInt dimR = 3;
1242dfccc68fSToby Isaac     PetscReal zOrder[24];
1243dfccc68fSToby Isaac     PetscReal zCoeff[24];
1244dfccc68fSToby Isaac     PetscInt  i, j, k, l;
1245dfccc68fSToby Isaac 
1246dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1247dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1248dfccc68fSToby Isaac 
1249dfccc68fSToby Isaac       for (j = 0; j < dim; j++) {
1250dfccc68fSToby Isaac         zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1251dfccc68fSToby Isaac       }
1252dfccc68fSToby Isaac     }
1253dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1254dfccc68fSToby 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]);
1255dfccc68fSToby 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]);
1256dfccc68fSToby 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]);
1257dfccc68fSToby 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]);
1258dfccc68fSToby 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]);
1259dfccc68fSToby 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]);
1260dfccc68fSToby 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]);
1261dfccc68fSToby 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]);
1262dfccc68fSToby Isaac     }
1263dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1264dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], theta = points[dimR * i + 2];
1265dfccc68fSToby Isaac 
1266dfccc68fSToby Isaac       if (v) {
126791d2b7ceSToby Isaac         PetscReal extPoint[8];
1268dfccc68fSToby Isaac 
1269dfccc68fSToby Isaac         extPoint[0] = 1.;
1270dfccc68fSToby Isaac         extPoint[1] = xi;
1271dfccc68fSToby Isaac         extPoint[2] = eta;
1272dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1273dfccc68fSToby Isaac         extPoint[4] = theta;
1274dfccc68fSToby Isaac         extPoint[5] = theta * xi;
1275dfccc68fSToby Isaac         extPoint[6] = theta * eta;
1276dfccc68fSToby Isaac         extPoint[7] = theta * eta * xi;
1277dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1278dfccc68fSToby Isaac           PetscReal val = 0.;
1279dfccc68fSToby Isaac 
1280dfccc68fSToby Isaac           for (k = 0; k < Nv; k++) {
1281dfccc68fSToby Isaac             val += extPoint[k] * zCoeff[dim * k + j];
1282dfccc68fSToby Isaac           }
1283dfccc68fSToby Isaac           v[i * dim + j] = val;
1284dfccc68fSToby Isaac         }
1285dfccc68fSToby Isaac       }
1286dfccc68fSToby Isaac       if (J) {
1287dfccc68fSToby Isaac         PetscReal extJ[24];
1288dfccc68fSToby Isaac 
1289dfccc68fSToby Isaac         extJ[0]  = 0.         ; extJ[1]  = 0.        ; extJ[2]  = 0.      ;
1290dfccc68fSToby Isaac         extJ[3]  = 1.         ; extJ[4]  = 0.        ; extJ[5]  = 0.      ;
1291dfccc68fSToby Isaac         extJ[6]  = 0.         ; extJ[7]  = 1.        ; extJ[8]  = 0.      ;
1292dfccc68fSToby Isaac         extJ[9]  = eta        ; extJ[10] = xi        ; extJ[11] = 0.      ;
1293dfccc68fSToby Isaac         extJ[12] = 0.         ; extJ[13] = 0.        ; extJ[14] = 1.      ;
1294dfccc68fSToby Isaac         extJ[15] = theta      ; extJ[16] = 0.        ; extJ[17] = xi      ;
1295dfccc68fSToby Isaac         extJ[18] = 0.         ; extJ[19] = theta     ; extJ[20] = eta     ;
1296dfccc68fSToby Isaac         extJ[21] = theta * eta; extJ[22] = theta * xi; extJ[23] = eta * xi;
1297dfccc68fSToby Isaac 
1298dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1299dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1300dfccc68fSToby Isaac             PetscReal val = 0.;
1301dfccc68fSToby Isaac 
1302dfccc68fSToby Isaac             for (l = 0; l < Nv; l++) {
1303dfccc68fSToby Isaac               val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1304dfccc68fSToby Isaac             }
1305dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1306dfccc68fSToby Isaac           }
1307dfccc68fSToby Isaac         }
1308dfccc68fSToby Isaac         DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1309dfccc68fSToby Isaac         if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1310dfccc68fSToby Isaac       }
1311dfccc68fSToby Isaac     }
1312dfccc68fSToby Isaac   }
1313ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1314ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1315ccd2543fSMatthew G Knepley }
1316ccd2543fSMatthew G Knepley 
1317dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeCellGeometryFEM_Implicit(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1318dfccc68fSToby Isaac {
1319dfccc68fSToby Isaac   PetscInt        depth, dim, coordDim, coneSize, i;
1320dfccc68fSToby Isaac   PetscInt        Nq = 0;
1321dfccc68fSToby Isaac   const PetscReal *points = NULL;
1322dfccc68fSToby Isaac   DMLabel         depthLabel;
13237318780aSToby Isaac   PetscReal       v0[3] = {-1.}, J0[9] = {-1.}, detJ0 = -1.;
1324dfccc68fSToby Isaac   PetscBool       isAffine = PETSC_TRUE;
1325dfccc68fSToby Isaac   PetscErrorCode  ierr;
1326dfccc68fSToby Isaac 
1327dfccc68fSToby Isaac   PetscFunctionBegin;
1328dfccc68fSToby Isaac   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1329dfccc68fSToby Isaac   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
1330dfccc68fSToby Isaac   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1331dfccc68fSToby Isaac   ierr = DMLabelGetValue(depthLabel, cell, &dim);CHKERRQ(ierr);
1332dfccc68fSToby Isaac   if (depth == 1 && dim == 1) {
1333dfccc68fSToby Isaac     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1334dfccc68fSToby Isaac   }
1335dfccc68fSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
1336dfccc68fSToby Isaac   if (coordDim > 3) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported coordinate dimension %D > 3", coordDim);
1337dfccc68fSToby Isaac   if (quad) {ierr = PetscQuadratureGetData(quad, NULL, &Nq, &points, NULL);CHKERRQ(ierr);}
1338dfccc68fSToby Isaac   switch (dim) {
1339dfccc68fSToby Isaac   case 0:
1340dfccc68fSToby Isaac     ierr = DMPlexComputePointGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
1341dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1342dfccc68fSToby Isaac     break;
1343dfccc68fSToby Isaac   case 1:
13447318780aSToby Isaac     if (Nq) {
13457318780aSToby Isaac       ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);
13467318780aSToby Isaac     } else {
13477318780aSToby Isaac       ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
13487318780aSToby Isaac     }
1349dfccc68fSToby Isaac     break;
1350dfccc68fSToby Isaac   case 2:
1351dfccc68fSToby Isaac     switch (coneSize) {
1352dfccc68fSToby Isaac     case 3:
13537318780aSToby Isaac       if (Nq) {
13547318780aSToby Isaac         ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);
13557318780aSToby Isaac       } else {
13567318780aSToby Isaac         ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
13577318780aSToby Isaac       }
1358dfccc68fSToby Isaac       break;
1359dfccc68fSToby Isaac     case 4:
1360dfccc68fSToby Isaac       ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1361dfccc68fSToby Isaac       isAffine = PETSC_FALSE;
1362dfccc68fSToby Isaac       break;
1363dfccc68fSToby Isaac     default:
1364dfccc68fSToby Isaac       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
1365dfccc68fSToby Isaac     }
1366dfccc68fSToby Isaac     break;
1367dfccc68fSToby Isaac   case 3:
1368dfccc68fSToby Isaac     switch (coneSize) {
1369dfccc68fSToby Isaac     case 4:
13707318780aSToby Isaac       if (Nq) {
13717318780aSToby Isaac         ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);
13727318780aSToby Isaac       } else {
13737318780aSToby Isaac         ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
13747318780aSToby Isaac       }
1375dfccc68fSToby Isaac       break;
1376dfccc68fSToby Isaac     case 6: /* Faces */
1377dfccc68fSToby Isaac     case 8: /* Vertices */
1378dfccc68fSToby Isaac       ierr = DMPlexComputeHexahedronGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1379dfccc68fSToby Isaac       isAffine = PETSC_FALSE;
1380dfccc68fSToby Isaac       break;
1381dfccc68fSToby Isaac     default:
1382dfccc68fSToby Isaac       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
1383dfccc68fSToby Isaac     }
1384dfccc68fSToby Isaac     break;
1385dfccc68fSToby Isaac   default:
1386dfccc68fSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
1387dfccc68fSToby Isaac   }
13887318780aSToby Isaac   if (isAffine && Nq) {
1389dfccc68fSToby Isaac     if (v) {
1390dfccc68fSToby Isaac       for (i = 0; i < Nq; i++) {
13917318780aSToby Isaac         CoordinatesRefToReal(coordDim,dim,v0, J0, &points[dim * i], &v[coordDim * i]);
1392dfccc68fSToby Isaac       }
1393dfccc68fSToby Isaac     }
13947318780aSToby Isaac     if (detJ) {
13957318780aSToby Isaac       for (i = 0; i < Nq; i++) {
13967318780aSToby Isaac         detJ[i] = detJ0;
1397dfccc68fSToby Isaac       }
13987318780aSToby Isaac     }
13997318780aSToby Isaac     if (J) {
14007318780aSToby Isaac       PetscInt k;
14017318780aSToby Isaac 
14027318780aSToby Isaac       for (i = 0, k = 0; i < Nq; i++) {
1403dfccc68fSToby Isaac         PetscInt j;
1404dfccc68fSToby Isaac 
14057318780aSToby Isaac         for (j = 0; j < coordDim * coordDim; j++, k++) {
14067318780aSToby Isaac           J[k] = J0[j];
14077318780aSToby Isaac         }
14087318780aSToby Isaac       }
14097318780aSToby Isaac     }
14107318780aSToby Isaac     if (invJ) {
14117318780aSToby Isaac       PetscInt k;
14127318780aSToby Isaac       switch (coordDim) {
14137318780aSToby Isaac       case 0:
14147318780aSToby Isaac         break;
14157318780aSToby Isaac       case 1:
14167318780aSToby Isaac         invJ[0] = 1./J0[0];
14177318780aSToby Isaac         break;
14187318780aSToby Isaac       case 2:
14197318780aSToby Isaac         DMPlex_Invert2D_Internal(invJ, J0, detJ0);
14207318780aSToby Isaac         break;
14217318780aSToby Isaac       case 3:
14227318780aSToby Isaac         DMPlex_Invert3D_Internal(invJ, J0, detJ0);
14237318780aSToby Isaac         break;
14247318780aSToby Isaac       }
14257318780aSToby Isaac       for (i = 1, k = coordDim * coordDim; i < Nq; i++) {
14267318780aSToby Isaac         PetscInt j;
14277318780aSToby Isaac 
14287318780aSToby Isaac         for (j = 0; j < coordDim * coordDim; j++, k++) {
14297318780aSToby Isaac           invJ[k] = invJ[j];
14307318780aSToby Isaac         }
1431dfccc68fSToby Isaac       }
1432dfccc68fSToby Isaac     }
1433dfccc68fSToby Isaac   }
1434dfccc68fSToby Isaac   PetscFunctionReturn(0);
1435dfccc68fSToby Isaac }
1436dfccc68fSToby Isaac 
1437ccd2543fSMatthew G Knepley /*@C
14388e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell
1439ccd2543fSMatthew G Knepley 
1440ccd2543fSMatthew G Knepley   Collective on DM
1441ccd2543fSMatthew G Knepley 
1442ccd2543fSMatthew G Knepley   Input Arguments:
1443ccd2543fSMatthew G Knepley + dm   - the DM
1444ccd2543fSMatthew G Knepley - cell - the cell
1445ccd2543fSMatthew G Knepley 
1446ccd2543fSMatthew G Knepley   Output Arguments:
1447ccd2543fSMatthew G Knepley + v0   - the translation part of this affine transform
1448ccd2543fSMatthew G Knepley . J    - the Jacobian of the transform from the reference element
1449ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian
1450ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant
1451ccd2543fSMatthew G Knepley 
1452ccd2543fSMatthew G Knepley   Level: advanced
1453ccd2543fSMatthew G Knepley 
1454ccd2543fSMatthew G Knepley   Fortran Notes:
1455ccd2543fSMatthew G Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1456ccd2543fSMatthew G Knepley   include petsc.h90 in your code.
1457ccd2543fSMatthew G Knepley 
14588e0841e0SMatthew G. Knepley .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinateVec()
1459ccd2543fSMatthew G Knepley @*/
14608e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1461ccd2543fSMatthew G Knepley {
1462ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1463ccd2543fSMatthew G Knepley 
1464ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1465dfccc68fSToby Isaac   ierr = DMPlexComputeCellGeometryFEM_Implicit(dm,cell,NULL,v0,J,invJ,detJ);CHKERRQ(ierr);
14668e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
14678e0841e0SMatthew G. Knepley }
14688e0841e0SMatthew G. Knepley 
14698e0841e0SMatthew G. Knepley #undef __FUNCT__
1470dfccc68fSToby Isaac #define __FUNCT__ "DMPlexComputeCellGeometryFEM_FE"
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);
14938e0841e0SMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, &qdim, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
1494dfccc68fSToby Isaac     Nq = 1;
1495dfccc68fSToby Isaac   } else {
1496dfccc68fSToby Isaac     ierr = PetscQuadratureGetData(quad, &qdim, &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 
1847c0d900a5SMatthew G. Knepley /* This should also take a PetscFE argument I think */
1848c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom)
1849c0d900a5SMatthew G. Knepley {
1850c0d900a5SMatthew G. Knepley   DM             dmCell;
1851c0d900a5SMatthew G. Knepley   Vec            coordinates;
1852c0d900a5SMatthew G. Knepley   PetscSection   coordSection, sectionCell;
1853c0d900a5SMatthew G. Knepley   PetscScalar   *cgeom;
1854c0d900a5SMatthew G. Knepley   PetscInt       cStart, cEnd, cMax, c;
1855c0d900a5SMatthew G. Knepley   PetscErrorCode ierr;
1856c0d900a5SMatthew G. Knepley 
1857c0d900a5SMatthew G. Knepley   PetscFunctionBegin;
1858c0d900a5SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1859c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1860c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1861c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1862c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1863c0d900a5SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1864c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1865c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
1866c0d900a5SMatthew G. Knepley   cEnd = cMax < 0 ? cEnd : cMax;
1867c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
1868c0d900a5SMatthew G. Knepley   /* TODO This needs to be multiplied by Nq for non-affine */
18699e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFECellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1870c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1871c0d900a5SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1872c0d900a5SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1873c0d900a5SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
1874c0d900a5SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1875c0d900a5SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1876c0d900a5SMatthew G. Knepley     PetscFECellGeom *cg;
1877c0d900a5SMatthew G. Knepley 
1878c0d900a5SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1879c0d900a5SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1880c0d900a5SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v0, cg->J, cg->invJ, &cg->detJ);CHKERRQ(ierr);
1881c0d900a5SMatthew G. Knepley     if (cg->detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", cg->detJ, c);
1882c0d900a5SMatthew G. Knepley   }
1883c0d900a5SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1884c0d900a5SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
1885c0d900a5SMatthew G. Knepley   PetscFunctionReturn(0);
1886c0d900a5SMatthew G. Knepley }
1887c0d900a5SMatthew G. Knepley 
1888891a9168SMatthew G. Knepley /*@
1889891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
1890891a9168SMatthew G. Knepley 
1891891a9168SMatthew G. Knepley   Input Parameter:
1892891a9168SMatthew G. Knepley . dm - The DM
1893891a9168SMatthew G. Knepley 
1894891a9168SMatthew G. Knepley   Output Parameters:
1895891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data
1896891a9168SMatthew G. Knepley . facegeom - A Vec of PetscFVFaceGeom data
1897891a9168SMatthew G. Knepley 
1898891a9168SMatthew G. Knepley   Level: developer
1899891a9168SMatthew G. Knepley 
1900891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM()
1901891a9168SMatthew G. Knepley @*/
1902113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom)
1903113c68e6SMatthew G. Knepley {
1904113c68e6SMatthew G. Knepley   DM             dmFace, dmCell;
1905113c68e6SMatthew G. Knepley   DMLabel        ghostLabel;
1906113c68e6SMatthew G. Knepley   PetscSection   sectionFace, sectionCell;
1907113c68e6SMatthew G. Knepley   PetscSection   coordSection;
1908113c68e6SMatthew G. Knepley   Vec            coordinates;
1909113c68e6SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
1910113c68e6SMatthew G. Knepley   PetscReal      minradius, gminradius;
1911113c68e6SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
1912113c68e6SMatthew G. Knepley   PetscErrorCode ierr;
1913113c68e6SMatthew G. Knepley 
1914113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1915113c68e6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1916113c68e6SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1917113c68e6SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1918113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
1919113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1920113c68e6SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1921113c68e6SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1922113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1923113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1924113c68e6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1925113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
19269e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1927113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1928113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1929113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1930113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
193106348e87SToby Isaac   if (cEndInterior < 0) {
193206348e87SToby Isaac     cEndInterior = cEnd;
193306348e87SToby Isaac   }
1934113c68e6SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1935113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
1936113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
1937113c68e6SMatthew G. Knepley 
1938113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1939113c68e6SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1940113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr);
1941113c68e6SMatthew G. Knepley   }
1942113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
1943113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmFace);CHKERRQ(ierr);
1944113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionFace);CHKERRQ(ierr);
1945113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1946113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr);
19479e5edeeeSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1948113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr);
1949113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmFace, sectionFace);CHKERRQ(ierr);
1950113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionFace);CHKERRQ(ierr);
1951113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr);
1952113c68e6SMatthew G. Knepley   ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr);
1953c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1954113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
1955113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
1956113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
1957113c68e6SMatthew G. Knepley     PetscReal        area;
195850d63984SToby Isaac     PetscInt         ghost = -1, d, numChildren;
1959113c68e6SMatthew G. Knepley 
19609ac3fadcSMatthew G. Knepley     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
196150d63984SToby Isaac     ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
196250d63984SToby Isaac     if (ghost >= 0 || numChildren) continue;
1963113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr);
1964113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr);
1965113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
1966113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
1967113c68e6SMatthew G. Knepley     {
1968113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
196906348e87SToby Isaac       PetscInt         ncells;
1970113c68e6SMatthew G. Knepley       const PetscInt  *cells;
1971113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
19720453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
1973113c68e6SMatthew G. Knepley 
1974113c68e6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr);
197506348e87SToby Isaac       ierr = DMPlexGetSupportSize(dm, f, &ncells);CHKERRQ(ierr);
1976113c68e6SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr);
1977113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
197806348e87SToby Isaac       if (ncells > 1) {
197906348e87SToby Isaac         ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr);
1980113c68e6SMatthew G. Knepley         rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
198106348e87SToby Isaac       }
198206348e87SToby Isaac       else {
198306348e87SToby Isaac         rcentroid = fg->centroid;
198406348e87SToby Isaac       }
19852e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr);
19862e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr);
19870453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
1988113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
1989113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
1990113c68e6SMatthew G. Knepley       }
1991113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
1992113c68e6SMatthew 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]);
1993113c68e6SMatthew 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]);
1994113c68e6SMatthew G. Knepley         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f);
1995113c68e6SMatthew G. Knepley       }
1996113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
1997113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
1998113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
1999113c68e6SMatthew G. Knepley       }
200006348e87SToby Isaac       if (ncells > 1 && cells[1] < cEndInterior) {
2001113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
2002113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2003113c68e6SMatthew G. Knepley       }
2004113c68e6SMatthew G. Knepley     }
2005113c68e6SMatthew G. Knepley   }
2006b2566f29SBarry Smith   ierr = MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2007113c68e6SMatthew G. Knepley   ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr);
2008113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
2009113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
2010113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2011113c68e6SMatthew G. Knepley     const PetscInt  *cone,    *support;
2012113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
2013113c68e6SMatthew G. Knepley 
2014113c68e6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr);
2015113c68e6SMatthew G. Knepley     if (coneSize != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize);
2016113c68e6SMatthew G. Knepley     ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr);
2017113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr);
201850d63984SToby Isaac     if (supportSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 2", cone[0], supportSize);
2019113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr);
2020113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr);
2021113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
2022113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
2023113c68e6SMatthew G. Knepley       if (support[s] == c) {
2024640bce14SSatish Balay         PetscFVCellGeom       *ci;
2025113c68e6SMatthew G. Knepley         PetscFVCellGeom       *cg;
2026113c68e6SMatthew G. Knepley         PetscReal              c2f[3], a;
2027113c68e6SMatthew G. Knepley 
2028113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr);
2029113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
2030113c68e6SMatthew G. Knepley         a    = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
2031113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr);
2032113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid);
2033113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
2034113c68e6SMatthew G. Knepley       }
2035113c68e6SMatthew G. Knepley     }
2036113c68e6SMatthew G. Knepley   }
2037113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr);
2038113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2039113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
2040113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmFace);CHKERRQ(ierr);
2041113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2042113c68e6SMatthew G. Knepley }
2043113c68e6SMatthew G. Knepley 
2044113c68e6SMatthew G. Knepley /*@C
2045113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
2046113c68e6SMatthew G. Knepley 
2047113c68e6SMatthew G. Knepley   Not collective
2048113c68e6SMatthew G. Knepley 
2049113c68e6SMatthew G. Knepley   Input Argument:
2050113c68e6SMatthew G. Knepley . dm - the DM
2051113c68e6SMatthew G. Knepley 
2052113c68e6SMatthew G. Knepley   Output Argument:
2053113c68e6SMatthew G. Knepley . minradius - the minium cell radius
2054113c68e6SMatthew G. Knepley 
2055113c68e6SMatthew G. Knepley   Level: developer
2056113c68e6SMatthew G. Knepley 
2057113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates()
2058113c68e6SMatthew G. Knepley @*/
2059113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius)
2060113c68e6SMatthew G. Knepley {
2061113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2062113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2063113c68e6SMatthew G. Knepley   PetscValidPointer(minradius,2);
2064113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex*) dm->data)->minradius;
2065113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2066113c68e6SMatthew G. Knepley }
2067113c68e6SMatthew G. Knepley 
2068113c68e6SMatthew G. Knepley /*@C
2069113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
2070113c68e6SMatthew G. Knepley 
2071113c68e6SMatthew G. Knepley   Logically collective
2072113c68e6SMatthew G. Knepley 
2073113c68e6SMatthew G. Knepley   Input Arguments:
2074113c68e6SMatthew G. Knepley + dm - the DM
2075113c68e6SMatthew G. Knepley - minradius - the minium cell radius
2076113c68e6SMatthew G. Knepley 
2077113c68e6SMatthew G. Knepley   Level: developer
2078113c68e6SMatthew G. Knepley 
2079113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates()
2080113c68e6SMatthew G. Knepley @*/
2081113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius)
2082113c68e6SMatthew G. Knepley {
2083113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2084113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2085113c68e6SMatthew G. Knepley   ((DM_Plex*) dm->data)->minradius = minradius;
2086113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2087113c68e6SMatthew G. Knepley }
2088856ac710SMatthew G. Knepley 
2089856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2090856ac710SMatthew G. Knepley {
2091856ac710SMatthew G. Knepley   DMLabel        ghostLabel;
2092856ac710SMatthew G. Knepley   PetscScalar   *dx, *grad, **gref;
2093856ac710SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
2094856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2095856ac710SMatthew G. Knepley 
2096856ac710SMatthew G. Knepley   PetscFunctionBegin;
2097856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2098856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2099856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2100856ac710SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr);
2101856ac710SMatthew G. Knepley   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
2102c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2103856ac710SMatthew G. Knepley   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
2104856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
2105856ac710SMatthew G. Knepley     const PetscInt        *faces;
2106856ac710SMatthew G. Knepley     PetscInt               numFaces, usedFaces, f, d;
2107640bce14SSatish Balay     PetscFVCellGeom        *cg;
2108856ac710SMatthew G. Knepley     PetscBool              boundary;
2109856ac710SMatthew G. Knepley     PetscInt               ghost;
2110856ac710SMatthew G. Knepley 
2111856ac710SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2112856ac710SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr);
2113856ac710SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr);
2114856ac710SMatthew 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);
2115856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
2116640bce14SSatish Balay       PetscFVCellGeom       *cg1;
2117856ac710SMatthew G. Knepley       PetscFVFaceGeom       *fg;
2118856ac710SMatthew G. Knepley       const PetscInt        *fcells;
2119856ac710SMatthew G. Knepley       PetscInt               ncell, side;
2120856ac710SMatthew G. Knepley 
2121856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
2122a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
2123856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
2124856ac710SMatthew G. Knepley       ierr  = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr);
2125856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
2126856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
2127856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr);
2128856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
2129856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d];
2130856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side];  /* Gradient reconstruction term will go here */
2131856ac710SMatthew G. Knepley     }
2132856ac710SMatthew G. Knepley     if (!usedFaces) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
2133856ac710SMatthew G. Knepley     ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr);
2134856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
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       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d];
2139856ac710SMatthew G. Knepley       ++usedFaces;
2140856ac710SMatthew G. Knepley     }
2141856ac710SMatthew G. Knepley   }
2142856ac710SMatthew G. Knepley   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
2143856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2144856ac710SMatthew G. Knepley }
2145856ac710SMatthew G. Knepley 
2146b81db932SToby Isaac static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2147b81db932SToby Isaac {
2148b81db932SToby Isaac   DMLabel        ghostLabel;
2149b81db932SToby Isaac   PetscScalar   *dx, *grad, **gref;
2150b81db932SToby Isaac   PetscInt       dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0;
2151b81db932SToby Isaac   PetscSection   neighSec;
2152b81db932SToby Isaac   PetscInt     (*neighbors)[2];
2153b81db932SToby Isaac   PetscInt      *counter;
2154b81db932SToby Isaac   PetscErrorCode ierr;
2155b81db932SToby Isaac 
2156b81db932SToby Isaac   PetscFunctionBegin;
2157b81db932SToby Isaac   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2158b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2159b81db932SToby Isaac   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
21605bc680faSToby Isaac   if (cEndInterior < 0) {
21615bc680faSToby Isaac     cEndInterior = cEnd;
21625bc680faSToby Isaac   }
2163b81db932SToby Isaac   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm),&neighSec);CHKERRQ(ierr);
2164b81db932SToby Isaac   ierr = PetscSectionSetChart(neighSec,cStart,cEndInterior);CHKERRQ(ierr);
2165b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
2166c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2167b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2168b81db932SToby Isaac     const PetscInt        *fcells;
2169b81db932SToby Isaac     PetscBool              boundary;
21705bc680faSToby Isaac     PetscInt               ghost = -1;
2171b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
2172b81db932SToby Isaac 
217306348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
2174a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
2175b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
2176b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
2177b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
217806348e87SToby Isaac     if (numCells == 2) {
2179b81db932SToby Isaac       ierr = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
2180b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2181b81db932SToby Isaac         PetscInt cell = fcells[c];
2182b81db932SToby Isaac 
2183e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
2184b81db932SToby Isaac           ierr = PetscSectionAddDof(neighSec,cell,1);CHKERRQ(ierr);
2185b81db932SToby Isaac         }
2186b81db932SToby Isaac       }
2187b81db932SToby Isaac     }
218806348e87SToby Isaac   }
2189b81db932SToby Isaac   ierr = PetscSectionSetUp(neighSec);CHKERRQ(ierr);
2190b81db932SToby Isaac   ierr = PetscSectionGetMaxDof(neighSec,&maxNumFaces);CHKERRQ(ierr);
2191b81db932SToby Isaac   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
2192b81db932SToby Isaac   nStart = 0;
2193b81db932SToby Isaac   ierr = PetscSectionGetStorageSize(neighSec,&nEnd);CHKERRQ(ierr);
2194b81db932SToby Isaac   ierr = PetscMalloc1((nEnd-nStart),&neighbors);CHKERRQ(ierr);
2195b81db932SToby Isaac   ierr = PetscCalloc1((cEndInterior-cStart),&counter);CHKERRQ(ierr);
2196b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2197b81db932SToby Isaac     const PetscInt        *fcells;
2198b81db932SToby Isaac     PetscBool              boundary;
21995bc680faSToby Isaac     PetscInt               ghost = -1;
2200b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
2201b81db932SToby Isaac 
220206348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
2203a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
2204b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
2205b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
2206b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
220706348e87SToby Isaac     if (numCells == 2) {
2208b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
2209b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2210b81db932SToby Isaac         PetscInt cell = fcells[c], off;
2211b81db932SToby Isaac 
2212e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
2213b81db932SToby Isaac           ierr = PetscSectionGetOffset(neighSec,cell,&off);CHKERRQ(ierr);
2214b81db932SToby Isaac           off += counter[cell - cStart]++;
2215b81db932SToby Isaac           neighbors[off][0] = f;
2216b81db932SToby Isaac           neighbors[off][1] = fcells[1 - c];
2217b81db932SToby Isaac         }
2218b81db932SToby Isaac       }
2219b81db932SToby Isaac     }
222006348e87SToby Isaac   }
2221b81db932SToby Isaac   ierr = PetscFree(counter);CHKERRQ(ierr);
2222b81db932SToby Isaac   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
2223b81db932SToby Isaac   for (c = cStart; c < cEndInterior; c++) {
2224317218b9SToby Isaac     PetscInt               numFaces, f, d, off, ghost = -1;
2225640bce14SSatish Balay     PetscFVCellGeom        *cg;
2226b81db932SToby Isaac 
2227b81db932SToby Isaac     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2228b81db932SToby Isaac     ierr = PetscSectionGetDof(neighSec, c, &numFaces);CHKERRQ(ierr);
2229b81db932SToby Isaac     ierr = PetscSectionGetOffset(neighSec, c, &off);CHKERRQ(ierr);
2230317218b9SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, c, &ghost);CHKERRQ(ierr);}
2231317218b9SToby 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);
2232b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2233640bce14SSatish Balay       PetscFVCellGeom       *cg1;
2234b81db932SToby Isaac       PetscFVFaceGeom       *fg;
2235b81db932SToby Isaac       const PetscInt        *fcells;
2236b81db932SToby Isaac       PetscInt               ncell, side, nface;
2237b81db932SToby Isaac 
2238b81db932SToby Isaac       nface = neighbors[off + f][0];
2239b81db932SToby Isaac       ncell = neighbors[off + f][1];
2240b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm,nface,&fcells);CHKERRQ(ierr);
2241b81db932SToby Isaac       side  = (c != fcells[0]);
2242b81db932SToby Isaac       ierr  = DMPlexPointLocalRef(dmFace, nface, fgeom, &fg);CHKERRQ(ierr);
2243b81db932SToby Isaac       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
2244b81db932SToby Isaac       for (d = 0; d < dim; ++d) dx[f*dim+d] = cg1->centroid[d] - cg->centroid[d];
2245b81db932SToby Isaac       gref[f] = fg->grad[side];  /* Gradient reconstruction term will go here */
2246b81db932SToby Isaac     }
2247b81db932SToby Isaac     ierr = PetscFVComputeGradient(fvm, numFaces, dx, grad);CHKERRQ(ierr);
2248b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2249b81db932SToby Isaac       for (d = 0; d < dim; ++d) gref[f][d] = grad[f*dim+d];
2250b81db932SToby Isaac     }
2251b81db932SToby Isaac   }
2252b81db932SToby Isaac   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
22535fe94518SToby Isaac   ierr = PetscSectionDestroy(&neighSec);CHKERRQ(ierr);
2254b81db932SToby Isaac   ierr = PetscFree(neighbors);CHKERRQ(ierr);
2255b81db932SToby Isaac   PetscFunctionReturn(0);
2256b81db932SToby Isaac }
2257b81db932SToby Isaac 
2258856ac710SMatthew G. Knepley /*@
2259856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
2260856ac710SMatthew G. Knepley 
2261856ac710SMatthew G. Knepley   Collective on DM
2262856ac710SMatthew G. Knepley 
2263856ac710SMatthew G. Knepley   Input Arguments:
2264856ac710SMatthew G. Knepley + dm  - The DM
2265856ac710SMatthew G. Knepley . fvm - The PetscFV
22668f9f38e3SMatthew G. Knepley . faceGeometry - The face geometry from DMPlexComputeFaceGeometryFVM()
22678f9f38e3SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexComputeCellGeometryFVM()
2268856ac710SMatthew G. Knepley 
2269856ac710SMatthew G. Knepley   Output Parameters:
2270856ac710SMatthew G. Knepley + faceGeometry - The geometric factors for gradient calculation are inserted
2271856ac710SMatthew G. Knepley - dmGrad - The DM describing the layout of gradient data
2272856ac710SMatthew G. Knepley 
2273856ac710SMatthew G. Knepley   Level: developer
2274856ac710SMatthew G. Knepley 
2275856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM()
2276856ac710SMatthew G. Knepley @*/
2277856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad)
2278856ac710SMatthew G. Knepley {
2279856ac710SMatthew G. Knepley   DM             dmFace, dmCell;
2280856ac710SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
2281b81db932SToby Isaac   PetscSection   sectionGrad, parentSection;
2282856ac710SMatthew G. Knepley   PetscInt       dim, pdim, cStart, cEnd, cEndInterior, c;
2283856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2284856ac710SMatthew G. Knepley 
2285856ac710SMatthew G. Knepley   PetscFunctionBegin;
2286856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2287856ac710SMatthew G. Knepley   ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr);
2288856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2289856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2290856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
2291856ac710SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
2292856ac710SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
2293856ac710SMatthew G. Knepley   ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2294856ac710SMatthew G. Knepley   ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2295b81db932SToby Isaac   ierr = DMPlexGetTree(dm,&parentSection,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
2296b81db932SToby Isaac   if (!parentSection) {
2297856ac710SMatthew G. Knepley     ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2298b5a3613cSMatthew G. Knepley   } else {
2299b81db932SToby Isaac     ierr = BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2300b81db932SToby Isaac   }
2301856ac710SMatthew G. Knepley   ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2302856ac710SMatthew G. Knepley   ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2303856ac710SMatthew G. Knepley   /* Create storage for gradients */
2304856ac710SMatthew G. Knepley   ierr = DMClone(dm, dmGrad);CHKERRQ(ierr);
2305856ac710SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionGrad);CHKERRQ(ierr);
2306856ac710SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr);
2307856ac710SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);}
2308856ac710SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr);
2309856ac710SMatthew G. Knepley   ierr = DMSetDefaultSection(*dmGrad, sectionGrad);CHKERRQ(ierr);
2310856ac710SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionGrad);CHKERRQ(ierr);
2311856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2312856ac710SMatthew G. Knepley }
2313b27d5b9eSToby Isaac 
2314b27d5b9eSToby Isaac PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM)
2315b27d5b9eSToby Isaac {
2316b27d5b9eSToby Isaac   PetscObject    cellgeomobj, facegeomobj;
2317b27d5b9eSToby Isaac   PetscErrorCode ierr;
2318b27d5b9eSToby Isaac 
2319b27d5b9eSToby Isaac   PetscFunctionBegin;
2320b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2321b27d5b9eSToby Isaac   if (!cellgeomobj) {
2322b27d5b9eSToby Isaac     Vec cellgeomInt, facegeomInt;
2323b27d5b9eSToby Isaac 
2324b27d5b9eSToby Isaac     ierr = DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt);CHKERRQ(ierr);
2325b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_cellgeom_fvm",(PetscObject)cellgeomInt);CHKERRQ(ierr);
2326b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_facegeom_fvm",(PetscObject)facegeomInt);CHKERRQ(ierr);
2327b27d5b9eSToby Isaac     ierr = VecDestroy(&cellgeomInt);CHKERRQ(ierr);
2328b27d5b9eSToby Isaac     ierr = VecDestroy(&facegeomInt);CHKERRQ(ierr);
2329b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2330b27d5b9eSToby Isaac   }
2331b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_facegeom_fvm", &facegeomobj);CHKERRQ(ierr);
2332b27d5b9eSToby Isaac   if (cellgeom) *cellgeom = (Vec) cellgeomobj;
2333b27d5b9eSToby Isaac   if (facegeom) *facegeom = (Vec) facegeomobj;
2334b27d5b9eSToby Isaac   if (gradDM) {
2335b27d5b9eSToby Isaac     PetscObject gradobj;
2336b27d5b9eSToby Isaac     PetscBool   computeGradients;
2337b27d5b9eSToby Isaac 
2338b27d5b9eSToby Isaac     ierr = PetscFVGetComputeGradients(fv,&computeGradients);CHKERRQ(ierr);
2339b27d5b9eSToby Isaac     if (!computeGradients) {
2340b27d5b9eSToby Isaac       *gradDM = NULL;
2341b27d5b9eSToby Isaac       PetscFunctionReturn(0);
2342b27d5b9eSToby Isaac     }
2343b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2344b27d5b9eSToby Isaac     if (!gradobj) {
2345b27d5b9eSToby Isaac       DM dmGradInt;
2346b27d5b9eSToby Isaac 
2347b27d5b9eSToby Isaac       ierr = DMPlexComputeGradientFVM(dm,fv,(Vec) facegeomobj,(Vec) cellgeomobj,&dmGradInt);CHKERRQ(ierr);
2348b27d5b9eSToby Isaac       ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt);CHKERRQ(ierr);
2349b27d5b9eSToby Isaac       ierr = DMDestroy(&dmGradInt);CHKERRQ(ierr);
2350b27d5b9eSToby Isaac       ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2351b27d5b9eSToby Isaac     }
2352b27d5b9eSToby Isaac     *gradDM = (DM) gradobj;
2353b27d5b9eSToby Isaac   }
2354b27d5b9eSToby Isaac   PetscFunctionReturn(0);
2355b27d5b9eSToby Isaac }
2356d6143a4eSToby Isaac 
23579d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_NewtonUpdate(PetscInt dimC, PetscInt dimR, PetscScalar *J, PetscScalar *invJ, PetscScalar *work,  PetscReal *resNeg, PetscReal *guess)
23589d150b73SToby Isaac {
23599d150b73SToby Isaac   PetscInt l, m;
23609d150b73SToby Isaac 
2361cd345991SToby Isaac   PetscFunctionBeginHot;
23629d150b73SToby Isaac   if (dimC == dimR && dimR <= 3) {
23639d150b73SToby Isaac     /* invert Jacobian, multiply */
23649d150b73SToby Isaac     PetscScalar det, idet;
23659d150b73SToby Isaac 
23669d150b73SToby Isaac     switch (dimR) {
23679d150b73SToby Isaac     case 1:
23689d150b73SToby Isaac       invJ[0] = 1./ J[0];
23699d150b73SToby Isaac       break;
23709d150b73SToby Isaac     case 2:
23719d150b73SToby Isaac       det = J[0] * J[3] - J[1] * J[2];
23729d150b73SToby Isaac       idet = 1./det;
23739d150b73SToby Isaac       invJ[0] =  J[3] * idet;
23749d150b73SToby Isaac       invJ[1] = -J[1] * idet;
23759d150b73SToby Isaac       invJ[2] = -J[2] * idet;
23769d150b73SToby Isaac       invJ[3] =  J[0] * idet;
23779d150b73SToby Isaac       break;
23789d150b73SToby Isaac     case 3:
23799d150b73SToby Isaac       {
23809d150b73SToby Isaac         invJ[0] = J[4] * J[8] - J[5] * J[7];
23819d150b73SToby Isaac         invJ[1] = J[2] * J[7] - J[1] * J[8];
23829d150b73SToby Isaac         invJ[2] = J[1] * J[5] - J[2] * J[4];
23839d150b73SToby Isaac         det = invJ[0] * J[0] + invJ[1] * J[3] + invJ[2] * J[6];
23849d150b73SToby Isaac         idet = 1./det;
23859d150b73SToby Isaac         invJ[0] *= idet;
23869d150b73SToby Isaac         invJ[1] *= idet;
23879d150b73SToby Isaac         invJ[2] *= idet;
23889d150b73SToby Isaac         invJ[3]  = idet * (J[5] * J[6] - J[3] * J[8]);
23899d150b73SToby Isaac         invJ[4]  = idet * (J[0] * J[8] - J[2] * J[6]);
23909d150b73SToby Isaac         invJ[5]  = idet * (J[2] * J[3] - J[0] * J[5]);
23919d150b73SToby Isaac         invJ[6]  = idet * (J[3] * J[7] - J[4] * J[6]);
23929d150b73SToby Isaac         invJ[7]  = idet * (J[1] * J[6] - J[0] * J[7]);
23939d150b73SToby Isaac         invJ[8]  = idet * (J[0] * J[4] - J[1] * J[3]);
23949d150b73SToby Isaac       }
23959d150b73SToby Isaac       break;
23969d150b73SToby Isaac     }
23979d150b73SToby Isaac     for (l = 0; l < dimR; l++) {
23989d150b73SToby Isaac       for (m = 0; m < dimC; m++) {
2399c6e120d1SToby Isaac         guess[l] += PetscRealPart(invJ[l * dimC + m]) * resNeg[m];
24009d150b73SToby Isaac       }
24019d150b73SToby Isaac     }
24029d150b73SToby Isaac   } else {
24039d150b73SToby Isaac #if defined(PETSC_USE_COMPLEX)
24049d150b73SToby Isaac     char transpose = 'C';
24059d150b73SToby Isaac #else
24069d150b73SToby Isaac     char transpose = 'T';
24079d150b73SToby Isaac #endif
24089d150b73SToby Isaac     PetscBLASInt m = dimR;
24099d150b73SToby Isaac     PetscBLASInt n = dimC;
24109d150b73SToby Isaac     PetscBLASInt one = 1;
24119d150b73SToby Isaac     PetscBLASInt worksize = dimR * dimC, info;
24129d150b73SToby Isaac 
24139d150b73SToby Isaac     for (l = 0; l < dimC; l++) {invJ[l] = resNeg[l];}
24149d150b73SToby Isaac 
24159d150b73SToby Isaac     PetscStackCallBLAS("LAPACKgels",LAPACKgels_(&transpose,&m,&n,&one,J,&m,invJ,&n,work,&worksize, &info));
24169d150b73SToby Isaac     if (info != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad argument to GELS");
24179d150b73SToby Isaac 
2418c6e120d1SToby Isaac     for (l = 0; l < dimR; l++) {guess[l] += PetscRealPart(invJ[l]);}
24199d150b73SToby Isaac   }
24209d150b73SToby Isaac   PetscFunctionReturn(0);
24219d150b73SToby Isaac }
24229d150b73SToby Isaac 
24239d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
24249d150b73SToby Isaac {
2425c0cbe899SToby Isaac   PetscInt       coordSize, i, j, k, l, m, maxIts = 7, numV = (1 << dimR);
24269d150b73SToby Isaac   PetscScalar    *coordsScalar = NULL;
24279d150b73SToby Isaac   PetscReal      *cellData, *cellCoords, *cellCoeffs, *extJ, *resNeg;
24289d150b73SToby Isaac   PetscScalar    *J, *invJ, *work;
24299d150b73SToby Isaac   PetscErrorCode ierr;
24309d150b73SToby Isaac 
24319d150b73SToby Isaac   PetscFunctionBegin;
24329d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24339d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
24349d150b73SToby 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);
24359d150b73SToby Isaac   ierr = DMGetWorkArray(dm, 2 * coordSize + dimR + dimC, PETSC_REAL, &cellData);CHKERRQ(ierr);
24369d150b73SToby Isaac   ierr = DMGetWorkArray(dm, 3 * dimR * dimC, PETSC_SCALAR, &J);CHKERRQ(ierr);
24379d150b73SToby Isaac   cellCoords = &cellData[0];
24389d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
24399d150b73SToby Isaac   extJ       = &cellData[2 * coordSize];
24409d150b73SToby Isaac   resNeg     = &cellData[2 * coordSize + dimR];
24419d150b73SToby Isaac   invJ       = &J[dimR * dimC];
24429d150b73SToby Isaac   work       = &J[2 * dimR * dimC];
24439d150b73SToby Isaac   if (dimR == 2) {
24449d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
24459d150b73SToby Isaac 
24469d150b73SToby Isaac     for (i = 0; i < 4; i++) {
24479d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
24489d150b73SToby Isaac 
24499d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
24509d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
24519d150b73SToby Isaac       }
24529d150b73SToby Isaac     }
24539d150b73SToby Isaac   } else if (dimR == 3) {
24549d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
24559d150b73SToby Isaac 
24569d150b73SToby Isaac     for (i = 0; i < 8; i++) {
24579d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
24589d150b73SToby Isaac 
24599d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
24609d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
24619d150b73SToby Isaac       }
24629d150b73SToby Isaac     }
24639d150b73SToby Isaac   } else {
24649d150b73SToby Isaac     for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);}
24659d150b73SToby Isaac   }
24669d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
24679d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
24689d150b73SToby Isaac     PetscReal *swap;
24699d150b73SToby Isaac 
24709d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
24719d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
24729d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
24739d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
24749d150b73SToby Isaac       }
24759d150b73SToby Isaac     }
24769d150b73SToby Isaac 
24779d150b73SToby Isaac     if (i < dimR - 1) {
24789d150b73SToby Isaac       swap = cellCoeffs;
24799d150b73SToby Isaac       cellCoeffs = cellCoords;
24809d150b73SToby Isaac       cellCoords = swap;
24819d150b73SToby Isaac     }
24829d150b73SToby Isaac   }
24839d150b73SToby Isaac   ierr = PetscMemzero(refCoords,numPoints * dimR * sizeof (PetscReal));CHKERRQ(ierr);
24849d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
24859d150b73SToby Isaac     for (i = 0; i < maxIts; i++) {
24869d150b73SToby Isaac       PetscReal *guess = &refCoords[dimR * j];
24879d150b73SToby Isaac 
24889d150b73SToby Isaac       /* compute -residual and Jacobian */
24899d150b73SToby Isaac       for (k = 0; k < dimC; k++) {resNeg[k] = realCoords[dimC * j + k];}
24909d150b73SToby Isaac       for (k = 0; k < dimC * dimR; k++) {J[k] = 0.;}
24919d150b73SToby Isaac       for (k = 0; k < numV; k++) {
24929d150b73SToby Isaac         PetscReal extCoord = 1.;
24939d150b73SToby Isaac         for (l = 0; l < dimR; l++) {
24949d150b73SToby Isaac           PetscReal coord = guess[l];
24959d150b73SToby Isaac           PetscInt  dep   = (k & (1 << l)) >> l;
24969d150b73SToby Isaac 
24979d150b73SToby Isaac           extCoord *= dep * coord + !dep;
24989d150b73SToby Isaac           extJ[l] = dep;
24999d150b73SToby Isaac 
25009d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
25019d150b73SToby Isaac             PetscReal coord = guess[m];
25029d150b73SToby Isaac             PetscInt  dep   = ((k & (1 << m)) >> m) && (m != l);
25039d150b73SToby Isaac             PetscReal mult  = dep * coord + !dep;
25049d150b73SToby Isaac 
25059d150b73SToby Isaac             extJ[l] *= mult;
25069d150b73SToby Isaac           }
25079d150b73SToby Isaac         }
25089d150b73SToby Isaac         for (l = 0; l < dimC; l++) {
25099d150b73SToby Isaac           PetscReal coeff = cellCoeffs[dimC * k + l];
25109d150b73SToby Isaac 
25119d150b73SToby Isaac           resNeg[l] -= coeff * extCoord;
25129d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
25139d150b73SToby Isaac             J[dimR * l + m] += coeff * extJ[m];
25149d150b73SToby Isaac           }
25159d150b73SToby Isaac         }
25169d150b73SToby Isaac       }
25170611203eSToby Isaac #if 0 && defined(PETSC_USE_DEBUG)
25180611203eSToby Isaac       {
25190611203eSToby Isaac         PetscReal maxAbs = 0.;
25200611203eSToby Isaac 
25210611203eSToby Isaac         for (l = 0; l < dimC; l++) {
25220611203eSToby Isaac           maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l]));
25230611203eSToby Isaac         }
25240611203eSToby Isaac         ierr = PetscInfo4(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,maxAbs);CHKERRQ(ierr);
25250611203eSToby Isaac       }
25260611203eSToby Isaac #endif
25279d150b73SToby Isaac 
25289d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_NewtonUpdate(dimC,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr);
25299d150b73SToby Isaac     }
25309d150b73SToby Isaac   }
25319d150b73SToby Isaac   ierr = DMRestoreWorkArray(dm, 3 * dimR * dimC, PETSC_SCALAR, &J);CHKERRQ(ierr);
25329d150b73SToby Isaac   ierr = DMRestoreWorkArray(dm, 2 * coordSize + dimR + dimC, PETSC_REAL, &cellData);CHKERRQ(ierr);
25339d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
25349d150b73SToby Isaac   PetscFunctionReturn(0);
25359d150b73SToby Isaac }
25369d150b73SToby Isaac 
25379d150b73SToby Isaac static PetscErrorCode DMPlexReferenceToCoordinates_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
25389d150b73SToby Isaac {
25399d150b73SToby Isaac   PetscInt       coordSize, i, j, k, l, numV = (1 << dimR);
25409d150b73SToby Isaac   PetscScalar    *coordsScalar = NULL;
25419d150b73SToby Isaac   PetscReal      *cellData, *cellCoords, *cellCoeffs;
25429d150b73SToby Isaac   PetscErrorCode ierr;
25439d150b73SToby Isaac 
25449d150b73SToby Isaac   PetscFunctionBegin;
25459d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25469d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
25479d150b73SToby 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);
25489d150b73SToby Isaac   ierr = DMGetWorkArray(dm, 2 * coordSize, PETSC_REAL, &cellData);CHKERRQ(ierr);
25499d150b73SToby Isaac   cellCoords = &cellData[0];
25509d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
25519d150b73SToby Isaac   if (dimR == 2) {
25529d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
25539d150b73SToby Isaac 
25549d150b73SToby Isaac     for (i = 0; i < 4; i++) {
25559d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
25569d150b73SToby Isaac 
25579d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
25589d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
25599d150b73SToby Isaac       }
25609d150b73SToby Isaac     }
25619d150b73SToby Isaac   } else if (dimR == 3) {
25629d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
25639d150b73SToby Isaac 
25649d150b73SToby Isaac     for (i = 0; i < 8; i++) {
25659d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
25669d150b73SToby Isaac 
25679d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
25689d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
25699d150b73SToby Isaac       }
25709d150b73SToby Isaac     }
25719d150b73SToby Isaac   } else {
25729d150b73SToby Isaac     for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);}
25739d150b73SToby Isaac   }
25749d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
25759d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
25769d150b73SToby Isaac     PetscReal *swap;
25779d150b73SToby Isaac 
25789d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
25799d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
25809d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
25819d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
25829d150b73SToby Isaac       }
25839d150b73SToby Isaac     }
25849d150b73SToby Isaac 
25859d150b73SToby Isaac     if (i < dimR - 1) {
25869d150b73SToby Isaac       swap = cellCoeffs;
25879d150b73SToby Isaac       cellCoeffs = cellCoords;
25889d150b73SToby Isaac       cellCoords = swap;
25899d150b73SToby Isaac     }
25909d150b73SToby Isaac   }
25919d150b73SToby Isaac   ierr = PetscMemzero(realCoords,numPoints * dimC * sizeof (PetscReal));CHKERRQ(ierr);
25929d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
25939d150b73SToby Isaac     const PetscReal *guess  = &refCoords[dimR * j];
25949d150b73SToby Isaac     PetscReal       *mapped = &realCoords[dimC * j];
25959d150b73SToby Isaac 
25969d150b73SToby Isaac     for (k = 0; k < numV; k++) {
25979d150b73SToby Isaac       PetscReal extCoord = 1.;
25989d150b73SToby Isaac       for (l = 0; l < dimR; l++) {
25999d150b73SToby Isaac         PetscReal coord = guess[l];
26009d150b73SToby Isaac         PetscInt  dep   = (k & (1 << l)) >> l;
26019d150b73SToby Isaac 
26029d150b73SToby Isaac         extCoord *= dep * coord + !dep;
26039d150b73SToby Isaac       }
26049d150b73SToby Isaac       for (l = 0; l < dimC; l++) {
26059d150b73SToby Isaac         PetscReal coeff = cellCoeffs[dimC * k + l];
26069d150b73SToby Isaac 
26079d150b73SToby Isaac         mapped[l] += coeff * extCoord;
26089d150b73SToby Isaac       }
26099d150b73SToby Isaac     }
26109d150b73SToby Isaac   }
26119d150b73SToby Isaac   ierr = DMRestoreWorkArray(dm, 2 * coordSize, PETSC_REAL, &cellData);CHKERRQ(ierr);
26129d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
26139d150b73SToby Isaac   PetscFunctionReturn(0);
26149d150b73SToby Isaac }
26159d150b73SToby Isaac 
26169d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
26179d150b73SToby Isaac {
2618c0cbe899SToby Isaac   PetscInt       numComp, numDof, i, j, k, l, m, maxIter = 7, coordSize;
2619c6e120d1SToby Isaac   PetscScalar    *nodes = NULL;
2620c6e120d1SToby Isaac   PetscReal      *invV, *modes;
2621c6e120d1SToby Isaac   PetscReal      *B, *D, *resNeg;
2622c6e120d1SToby Isaac   PetscScalar    *J, *invJ, *work;
26239d150b73SToby Isaac   PetscErrorCode ierr;
26249d150b73SToby Isaac 
26259d150b73SToby Isaac   PetscFunctionBegin;
26269d150b73SToby Isaac   ierr = PetscFEGetDimension(fe, &numDof);CHKERRQ(ierr);
26279d150b73SToby Isaac   ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
26289d150b73SToby Isaac   if (numComp != dimC) SETERRQ2(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"coordinate discretization must have as many components (%D) as embedding dimension (!= %D)",numComp,dimC);
26299d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
26309d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
2631c6e120d1SToby Isaac   ierr = DMGetWorkArray(dm,dimC * numDof,PETSC_REAL,&modes);CHKERRQ(ierr);
26329d150b73SToby Isaac   invV = fe->invV;
26339d150b73SToby Isaac   for (i = 0; i < numDof; i++) {
26349d150b73SToby Isaac     for (j = 0; j < dimC; j++) {
26359d150b73SToby Isaac       modes[i * dimC + j] = 0.;
26369d150b73SToby Isaac       for (k = 0; k < numDof; k++) {
2637c6e120d1SToby Isaac         modes[i * dimC + j] += invV[i * numDof + k] * PetscRealPart(nodes[k * dimC + j]);
26389d150b73SToby Isaac       }
26399d150b73SToby Isaac     }
26409d150b73SToby Isaac   }
2641c6e120d1SToby Isaac   ierr   = DMGetWorkArray(dm,numDof + numDof * dimR + dimC,PETSC_REAL,&B);CHKERRQ(ierr);
2642c6e120d1SToby Isaac   D      = &B[numDof];
2643c6e120d1SToby Isaac   resNeg = &D[numDof * dimR];
2644c6e120d1SToby Isaac   ierr = DMGetWorkArray(dm,3 * dimC * dimR,PETSC_SCALAR,&J);CHKERRQ(ierr);
26459d150b73SToby Isaac   invJ = &J[dimC * dimR];
26469d150b73SToby Isaac   work = &invJ[dimC * dimR];
26479d150b73SToby Isaac   for (i = 0; i < numPoints * dimR; i++) {refCoords[i] = 0.;}
26489d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
26499b1f03cbSToby 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 */
26509d150b73SToby Isaac       PetscReal *guess = &refCoords[j * dimR];
26519d150b73SToby Isaac       ierr = PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, D, NULL);CHKERRQ(ierr);
26529d150b73SToby Isaac       for (k = 0; k < dimC; k++) {resNeg[k] = realCoords[j * dimC + k];}
26539d150b73SToby Isaac       for (k = 0; k < dimC * dimR; k++) {J[k] = 0.;}
26549d150b73SToby Isaac       for (k = 0; k < numDof; k++) {
26559d150b73SToby Isaac         for (l = 0; l < dimC; l++) {
26569d150b73SToby Isaac           resNeg[l] -= modes[k * dimC + l] * B[k];
26579d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
26589d150b73SToby Isaac             J[l * dimR + m] += modes[k * dimC + l] * D[k * dimR + m];
26599d150b73SToby Isaac           }
26609d150b73SToby Isaac         }
26619d150b73SToby Isaac       }
26620611203eSToby Isaac #if 0 && defined(PETSC_USE_DEBUG)
26630611203eSToby Isaac       {
26640611203eSToby Isaac         PetscReal maxAbs = 0.;
26650611203eSToby Isaac 
26660611203eSToby Isaac         for (l = 0; l < dimC; l++) {
26670611203eSToby Isaac           maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l]));
26680611203eSToby Isaac         }
26690611203eSToby Isaac         ierr = PetscInfo4(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,maxAbs);CHKERRQ(ierr);
26700611203eSToby Isaac       }
26710611203eSToby Isaac #endif
26729d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_NewtonUpdate(dimC,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr);
26739d150b73SToby Isaac     }
26749d150b73SToby Isaac   }
2675c6e120d1SToby Isaac   ierr = DMRestoreWorkArray(dm,3 * dimC * dimR,PETSC_SCALAR,&J);CHKERRQ(ierr);
2676c6e120d1SToby Isaac   ierr = DMRestoreWorkArray(dm,numDof + numDof * dimR + dimC,PETSC_REAL,&B);CHKERRQ(ierr);
2677c6e120d1SToby Isaac   ierr = DMRestoreWorkArray(dm,dimC * numDof,PETSC_REAL,&modes);CHKERRQ(ierr);
26789d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
26799d150b73SToby Isaac   PetscFunctionReturn(0);
26809d150b73SToby Isaac }
26819d150b73SToby Isaac 
26829d150b73SToby Isaac static PetscErrorCode DMPlexReferenceToCoordinates_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
26839d150b73SToby Isaac {
26849d150b73SToby Isaac   PetscInt       numComp, numDof, i, j, k, l, coordSize;
2685c6e120d1SToby Isaac   PetscScalar    *nodes = NULL;
2686c6e120d1SToby Isaac   PetscReal      *invV, *modes;
26879d150b73SToby Isaac   PetscReal      *B;
26889d150b73SToby Isaac   PetscErrorCode ierr;
26899d150b73SToby Isaac 
26909d150b73SToby Isaac   PetscFunctionBegin;
26919d150b73SToby Isaac   ierr = PetscFEGetDimension(fe, &numDof);CHKERRQ(ierr);
26929d150b73SToby Isaac   ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
26939d150b73SToby Isaac   if (numComp != dimC) SETERRQ2(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"coordinate discretization must have as many components (%D) as embedding dimension (!= %D)",numComp,dimC);
26949d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
26959d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
2696c6e120d1SToby Isaac   ierr = DMGetWorkArray(dm,dimC * numDof,PETSC_REAL,&modes);CHKERRQ(ierr);
26979d150b73SToby Isaac   invV = fe->invV;
26989d150b73SToby Isaac   for (i = 0; i < numDof; i++) {
26999d150b73SToby Isaac     for (j = 0; j < dimC; j++) {
27009d150b73SToby Isaac       modes[i * dimC + j] = 0.;
27019d150b73SToby Isaac       for (k = 0; k < numDof; k++) {
2702c6e120d1SToby Isaac         modes[i * dimC + j] += invV[i * numDof + k] * PetscRealPart(nodes[k * dimC + j]);
27039d150b73SToby Isaac       }
27049d150b73SToby Isaac     }
27059d150b73SToby Isaac   }
27069b1f03cbSToby Isaac   ierr = DMGetWorkArray(dm,numDof,PETSC_REAL,&B);CHKERRQ(ierr);
27079d150b73SToby Isaac   for (i = 0; i < numPoints * dimC; i++) {realCoords[i] = 0.;}
27089d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
27099d150b73SToby Isaac     const PetscReal *guess  = &refCoords[j * dimR];
27109d150b73SToby Isaac     PetscReal       *mapped = &realCoords[j * dimC];
27119d150b73SToby Isaac 
27129d150b73SToby Isaac     ierr = PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, NULL, NULL);CHKERRQ(ierr);
27139d150b73SToby Isaac     for (k = 0; k < numDof; k++) {
27149d150b73SToby Isaac       for (l = 0; l < dimC; l++) {
27159d150b73SToby Isaac         mapped[l] += modes[k * dimC + l] * B[k];
27169d150b73SToby Isaac       }
27179d150b73SToby Isaac     }
27189d150b73SToby Isaac   }
27199b1f03cbSToby Isaac   ierr = DMRestoreWorkArray(dm,numDof,PETSC_REAL,&B);CHKERRQ(ierr);
2720c6e120d1SToby Isaac   ierr = DMRestoreWorkArray(dm,dimC * numDof,PETSC_REAL,&modes);CHKERRQ(ierr);
27219d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
27229d150b73SToby Isaac   PetscFunctionReturn(0);
27239d150b73SToby Isaac }
27249d150b73SToby Isaac 
2725d6143a4eSToby Isaac /*@
2726d6143a4eSToby Isaac   DMPlexCoordinatesToReference - Pull coordinates back from the mesh to the reference element using a single element
2727d6143a4eSToby Isaac   map.  This inversion will be accurate inside the reference element, but may be inaccurate for mappings that do not
2728d6143a4eSToby Isaac   extend uniquely outside the reference cell (e.g, most non-affine maps)
2729d6143a4eSToby Isaac 
2730d6143a4eSToby Isaac   Not collective
2731d6143a4eSToby Isaac 
2732d6143a4eSToby Isaac   Input Parameters:
2733d6143a4eSToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
2734d6143a4eSToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
2735d6143a4eSToby Isaac                as a multilinear map for tensor-product elements
2736d6143a4eSToby Isaac . cell       - the cell whose map is used.
2737d6143a4eSToby Isaac . numPoints  - the number of points to locate
27381b266c99SBarry Smith - realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
2739d6143a4eSToby Isaac 
2740d6143a4eSToby Isaac   Output Parameters:
2741d6143a4eSToby Isaac . refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
27421b266c99SBarry Smith 
27431b266c99SBarry Smith   Level: intermediate
2744d6143a4eSToby Isaac @*/
2745d6143a4eSToby Isaac PetscErrorCode DMPlexCoordinatesToReference(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[])
2746d6143a4eSToby Isaac {
27479d150b73SToby Isaac   PetscInt       dimC, dimR, depth, cStart, cEnd, cEndInterior, i;
27489d150b73SToby Isaac   DM             coordDM = NULL;
27499d150b73SToby Isaac   Vec            coords;
27509d150b73SToby Isaac   PetscFE        fe = NULL;
27519d150b73SToby Isaac   PetscErrorCode ierr;
27529d150b73SToby Isaac 
2753d6143a4eSToby Isaac   PetscFunctionBegin;
27549d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27559d150b73SToby Isaac   ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr);
27569d150b73SToby Isaac   ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr);
27579d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
27589d150b73SToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
27599d150b73SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
27609d150b73SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr);
27619d150b73SToby Isaac   if (coordDM) {
27629d150b73SToby Isaac     PetscInt coordFields;
27639d150b73SToby Isaac 
27649d150b73SToby Isaac     ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr);
27659d150b73SToby Isaac     if (coordFields) {
27669d150b73SToby Isaac       PetscClassId id;
27679d150b73SToby Isaac       PetscObject  disc;
27689d150b73SToby Isaac 
27699d150b73SToby Isaac       ierr = DMGetField(coordDM,0,&disc);CHKERRQ(ierr);
27709d150b73SToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
27719d150b73SToby Isaac       if (id == PETSCFE_CLASSID) {
27729d150b73SToby Isaac         fe = (PetscFE) disc;
27739d150b73SToby Isaac       }
27749d150b73SToby Isaac     }
27759d150b73SToby Isaac   }
27769d150b73SToby Isaac   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
27779d150b73SToby Isaac   ierr = DMPlexGetHybridBounds(dm,&cEndInterior,NULL,NULL,NULL);CHKERRQ(ierr);
27789d150b73SToby Isaac   cEnd = cEndInterior > 0 ? cEndInterior : cEnd;
27799d150b73SToby 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);
27809d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
27819d150b73SToby Isaac     PetscInt  coneSize;
27829d150b73SToby Isaac     PetscBool isSimplex, isTensor;
27839d150b73SToby Isaac 
27849d150b73SToby Isaac     ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
27859d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
27869d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
27879d150b73SToby Isaac     if (isSimplex) {
27889d150b73SToby Isaac       PetscReal detJ, *v0, *J, *invJ;
27899d150b73SToby Isaac 
27909d150b73SToby Isaac       ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr);
27919d150b73SToby Isaac       J    = &v0[dimC];
27929d150b73SToby Isaac       invJ = &J[dimC * dimC];
27939d150b73SToby Isaac       ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, &detJ);CHKERRQ(ierr);
27949d150b73SToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */
27959d150b73SToby Isaac         CoordinatesRealToRef(dimC, dimR, v0, invJ, &realCoords[dimC * i], &refCoords[dimR * i]);
27969d150b73SToby Isaac       }
27979d150b73SToby Isaac       ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr);
27989d150b73SToby Isaac     } else if (isTensor) {
27999d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_Tensor(coordDM, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr);
28009d150b73SToby Isaac     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize);
28019d150b73SToby Isaac   } else {
28029d150b73SToby Isaac     ierr = DMPlexCoordinatesToReference_FE(coordDM, fe, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr);
28039d150b73SToby Isaac   }
28049d150b73SToby Isaac   PetscFunctionReturn(0);
28059d150b73SToby Isaac }
28069d150b73SToby Isaac 
28079d150b73SToby Isaac /*@
28089d150b73SToby Isaac   DMPlexReferenceToCoordinates - Map references coordinates to coordinates in the the mesh for a single element map.
28099d150b73SToby Isaac 
28109d150b73SToby Isaac   Not collective
28119d150b73SToby Isaac 
28129d150b73SToby Isaac   Input Parameters:
28139d150b73SToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
28149d150b73SToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
28159d150b73SToby Isaac                as a multilinear map for tensor-product elements
28169d150b73SToby Isaac . cell       - the cell whose map is used.
28179d150b73SToby Isaac . numPoints  - the number of points to locate
28189d150b73SToby Isaac + refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
28199d150b73SToby Isaac 
28209d150b73SToby Isaac   Output Parameters:
28219d150b73SToby Isaac . realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
28221b266c99SBarry Smith 
28231b266c99SBarry Smith    Level: intermediate
28249d150b73SToby Isaac @*/
28259d150b73SToby Isaac PetscErrorCode DMPlexReferenceToCoordinates(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[])
28269d150b73SToby Isaac {
28279d150b73SToby Isaac   PetscInt       dimC, dimR, depth, cStart, cEnd, cEndInterior, i;
28289d150b73SToby Isaac   DM             coordDM = NULL;
28299d150b73SToby Isaac   Vec            coords;
28309d150b73SToby Isaac   PetscFE        fe = NULL;
28319d150b73SToby Isaac   PetscErrorCode ierr;
28329d150b73SToby Isaac 
28339d150b73SToby Isaac   PetscFunctionBegin;
28349d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28359d150b73SToby Isaac   ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr);
28369d150b73SToby Isaac   ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr);
28379d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
28389d150b73SToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
28399d150b73SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
28409d150b73SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr);
28419d150b73SToby Isaac   if (coordDM) {
28429d150b73SToby Isaac     PetscInt coordFields;
28439d150b73SToby Isaac 
28449d150b73SToby Isaac     ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr);
28459d150b73SToby Isaac     if (coordFields) {
28469d150b73SToby Isaac       PetscClassId id;
28479d150b73SToby Isaac       PetscObject  disc;
28489d150b73SToby Isaac 
28499d150b73SToby Isaac       ierr = DMGetField(coordDM,0,&disc);CHKERRQ(ierr);
28509d150b73SToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
28519d150b73SToby Isaac       if (id == PETSCFE_CLASSID) {
28529d150b73SToby Isaac         fe = (PetscFE) disc;
28539d150b73SToby Isaac       }
28549d150b73SToby Isaac     }
28559d150b73SToby Isaac   }
28569d150b73SToby Isaac   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
28579d150b73SToby Isaac   ierr = DMPlexGetHybridBounds(dm,&cEndInterior,NULL,NULL,NULL);CHKERRQ(ierr);
28589d150b73SToby Isaac   cEnd = cEndInterior > 0 ? cEndInterior : cEnd;
28599d150b73SToby 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);
28609d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
28619d150b73SToby Isaac     PetscInt  coneSize;
28629d150b73SToby Isaac     PetscBool isSimplex, isTensor;
28639d150b73SToby Isaac 
28649d150b73SToby Isaac     ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
28659d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
28669d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
28679d150b73SToby Isaac     if (isSimplex) {
28689d150b73SToby Isaac       PetscReal detJ, *v0, *J;
28699d150b73SToby Isaac 
28709d150b73SToby Isaac       ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr);
28719d150b73SToby Isaac       J    = &v0[dimC];
28729d150b73SToby Isaac       ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, NULL, &detJ);CHKERRQ(ierr);
28739d150b73SToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */
28749d150b73SToby Isaac         CoordinatesRefToReal(dimC, dimR, v0, J, &refCoords[dimR * i], &realCoords[dimC * i]);
28759d150b73SToby Isaac       }
28769d150b73SToby Isaac       ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, PETSC_REAL, &v0);CHKERRQ(ierr);
28779d150b73SToby Isaac     } else if (isTensor) {
28789d150b73SToby Isaac       ierr = DMPlexReferenceToCoordinates_Tensor(coordDM, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr);
28799d150b73SToby Isaac     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize);
28809d150b73SToby Isaac   } else {
28819d150b73SToby Isaac     ierr = DMPlexReferenceToCoordinates_FE(coordDM, fe, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr);
28829d150b73SToby Isaac   }
2883d6143a4eSToby Isaac   PetscFunctionReturn(0);
2884d6143a4eSToby Isaac }
2885