xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision e8964c0ada2b153d4dbdedf543d8be8c7c550756)
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>
4af74b616SDave May #include <petsctime.h>
5ccd2543fSMatthew G Knepley 
6fea14342SMatthew G. Knepley static PetscErrorCode DMPlexGetLineIntersection_2D_Internal(const PetscReal segmentA[], const PetscReal segmentB[], PetscReal intersection[], PetscBool *hasIntersection)
7fea14342SMatthew G. Knepley {
8fea14342SMatthew G. Knepley   const PetscReal p0_x  = segmentA[0*2+0];
9fea14342SMatthew G. Knepley   const PetscReal p0_y  = segmentA[0*2+1];
10fea14342SMatthew G. Knepley   const PetscReal p1_x  = segmentA[1*2+0];
11fea14342SMatthew G. Knepley   const PetscReal p1_y  = segmentA[1*2+1];
12fea14342SMatthew G. Knepley   const PetscReal p2_x  = segmentB[0*2+0];
13fea14342SMatthew G. Knepley   const PetscReal p2_y  = segmentB[0*2+1];
14fea14342SMatthew G. Knepley   const PetscReal p3_x  = segmentB[1*2+0];
15fea14342SMatthew G. Knepley   const PetscReal p3_y  = segmentB[1*2+1];
16fea14342SMatthew G. Knepley   const PetscReal s1_x  = p1_x - p0_x;
17fea14342SMatthew G. Knepley   const PetscReal s1_y  = p1_y - p0_y;
18fea14342SMatthew G. Knepley   const PetscReal s2_x  = p3_x - p2_x;
19fea14342SMatthew G. Knepley   const PetscReal s2_y  = p3_y - p2_y;
20fea14342SMatthew G. Knepley   const PetscReal denom = (-s2_x * s1_y + s1_x * s2_y);
21fea14342SMatthew G. Knepley 
22fea14342SMatthew G. Knepley   PetscFunctionBegin;
23fea14342SMatthew G. Knepley   *hasIntersection = PETSC_FALSE;
24fea14342SMatthew G. Knepley   /* Non-parallel lines */
25fea14342SMatthew G. Knepley   if (denom != 0.0) {
26fea14342SMatthew G. Knepley     const PetscReal s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / denom;
27fea14342SMatthew G. Knepley     const PetscReal t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / denom;
28fea14342SMatthew G. Knepley 
29fea14342SMatthew G. Knepley     if (s >= 0 && s <= 1 && t >= 0 && t <= 1) {
30fea14342SMatthew G. Knepley       *hasIntersection = PETSC_TRUE;
31fea14342SMatthew G. Knepley       if (intersection) {
32fea14342SMatthew G. Knepley         intersection[0] = p0_x + (t * s1_x);
33fea14342SMatthew G. Knepley         intersection[1] = p0_y + (t * s1_y);
34fea14342SMatthew G. Knepley       }
35fea14342SMatthew G. Knepley     }
36fea14342SMatthew G. Knepley   }
37fea14342SMatthew G. Knepley   PetscFunctionReturn(0);
38fea14342SMatthew G. Knepley }
39fea14342SMatthew G. Knepley 
40ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
41ccd2543fSMatthew G Knepley {
42ccd2543fSMatthew G Knepley   const PetscInt  embedDim = 2;
43f5ebc837SMatthew G. Knepley   const PetscReal eps      = PETSC_SQRT_MACHINE_EPSILON;
44ccd2543fSMatthew G Knepley   PetscReal       x        = PetscRealPart(point[0]);
45ccd2543fSMatthew G Knepley   PetscReal       y        = PetscRealPart(point[1]);
46ccd2543fSMatthew G Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
47ccd2543fSMatthew G Knepley   PetscReal       xi, eta;
48ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
49ccd2543fSMatthew G Knepley 
50ccd2543fSMatthew G Knepley   PetscFunctionBegin;
518e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
52ccd2543fSMatthew G Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
53ccd2543fSMatthew G Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
54ccd2543fSMatthew G Knepley 
55f5ebc837SMatthew G. Knepley   if ((xi >= -eps) && (eta >= -eps) && (xi + eta <= 2.0+eps)) *cell = c;
56c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
57ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
58ccd2543fSMatthew G Knepley }
59ccd2543fSMatthew G Knepley 
6062a38674SMatthew G. Knepley static PetscErrorCode DMPlexClosestPoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscReal cpoint[])
6162a38674SMatthew G. Knepley {
6262a38674SMatthew G. Knepley   const PetscInt  embedDim = 2;
6362a38674SMatthew G. Knepley   PetscReal       x        = PetscRealPart(point[0]);
6462a38674SMatthew G. Knepley   PetscReal       y        = PetscRealPart(point[1]);
6562a38674SMatthew G. Knepley   PetscReal       v0[2], J[4], invJ[4], detJ;
6662a38674SMatthew G. Knepley   PetscReal       xi, eta, r;
6762a38674SMatthew G. Knepley   PetscErrorCode  ierr;
6862a38674SMatthew G. Knepley 
6962a38674SMatthew G. Knepley   PetscFunctionBegin;
7062a38674SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
7162a38674SMatthew G. Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
7262a38674SMatthew G. Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
7362a38674SMatthew G. Knepley 
7462a38674SMatthew G. Knepley   xi  = PetscMax(xi,  0.0);
7562a38674SMatthew G. Knepley   eta = PetscMax(eta, 0.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;
143872a9804SMatthew G. Knepley   PetscScalar   *coords = NULL;
144fb150da6SMatthew G. Knepley   const PetscInt faces[24] = {0, 3, 2, 1,  5, 4, 7, 6,  3, 0, 4, 5,
145fb150da6SMatthew G. Knepley                               1, 2, 6, 7,  3, 5, 6, 2,  0, 1, 7, 4};
146ccd2543fSMatthew G Knepley   PetscBool      found = PETSC_TRUE;
147ccd2543fSMatthew G Knepley   PetscInt       f;
148ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
149ccd2543fSMatthew G Knepley 
150ccd2543fSMatthew G Knepley   PetscFunctionBegin;
151ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
15269d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
153ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
154ccd2543fSMatthew G Knepley   for (f = 0; f < 6; ++f) {
155ccd2543fSMatthew G Knepley     /* Check the point is under plane */
156ccd2543fSMatthew G Knepley     /*   Get face normal */
157ccd2543fSMatthew G Knepley     PetscReal v_i[3];
158ccd2543fSMatthew G Knepley     PetscReal v_j[3];
159ccd2543fSMatthew G Knepley     PetscReal normal[3];
160ccd2543fSMatthew G Knepley     PetscReal pp[3];
161ccd2543fSMatthew G Knepley     PetscReal dot;
162ccd2543fSMatthew G Knepley 
163ccd2543fSMatthew G Knepley     v_i[0]    = PetscRealPart(coords[faces[f*4+3]*3+0]-coords[faces[f*4+0]*3+0]);
164ccd2543fSMatthew G Knepley     v_i[1]    = PetscRealPart(coords[faces[f*4+3]*3+1]-coords[faces[f*4+0]*3+1]);
165ccd2543fSMatthew G Knepley     v_i[2]    = PetscRealPart(coords[faces[f*4+3]*3+2]-coords[faces[f*4+0]*3+2]);
166ccd2543fSMatthew G Knepley     v_j[0]    = PetscRealPart(coords[faces[f*4+1]*3+0]-coords[faces[f*4+0]*3+0]);
167ccd2543fSMatthew G Knepley     v_j[1]    = PetscRealPart(coords[faces[f*4+1]*3+1]-coords[faces[f*4+0]*3+1]);
168ccd2543fSMatthew G Knepley     v_j[2]    = PetscRealPart(coords[faces[f*4+1]*3+2]-coords[faces[f*4+0]*3+2]);
169ccd2543fSMatthew G Knepley     normal[0] = v_i[1]*v_j[2] - v_i[2]*v_j[1];
170ccd2543fSMatthew G Knepley     normal[1] = v_i[2]*v_j[0] - v_i[0]*v_j[2];
171ccd2543fSMatthew G Knepley     normal[2] = v_i[0]*v_j[1] - v_i[1]*v_j[0];
172ccd2543fSMatthew G Knepley     pp[0]     = PetscRealPart(coords[faces[f*4+0]*3+0] - point[0]);
173ccd2543fSMatthew G Knepley     pp[1]     = PetscRealPart(coords[faces[f*4+0]*3+1] - point[1]);
174ccd2543fSMatthew G Knepley     pp[2]     = PetscRealPart(coords[faces[f*4+0]*3+2] - point[2]);
175ccd2543fSMatthew G Knepley     dot       = normal[0]*pp[0] + normal[1]*pp[1] + normal[2]*pp[2];
176ccd2543fSMatthew G Knepley 
177ccd2543fSMatthew G Knepley     /* Check that projected point is in face (2D location problem) */
178ccd2543fSMatthew G Knepley     if (dot < 0.0) {
179ccd2543fSMatthew G Knepley       found = PETSC_FALSE;
180ccd2543fSMatthew G Knepley       break;
181ccd2543fSMatthew G Knepley     }
182ccd2543fSMatthew G Knepley   }
183ccd2543fSMatthew G Knepley   if (found) *cell = c;
184c1496c66SMatthew G. Knepley   else *cell = DMLOCATEPOINT_POINT_NOT_FOUND;
185ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
186ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
187ccd2543fSMatthew G Knepley }
188ccd2543fSMatthew G Knepley 
189c4eade1cSMatthew G. Knepley static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[])
190c4eade1cSMatthew G. Knepley {
191c4eade1cSMatthew G. Knepley   PetscInt d;
192c4eade1cSMatthew G. Knepley 
193c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
194c4eade1cSMatthew G. Knepley   box->dim = dim;
195c4eade1cSMatthew G. Knepley   for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]);
196c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
197c4eade1cSMatthew G. Knepley }
198c4eade1cSMatthew G. Knepley 
199c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box)
200c4eade1cSMatthew G. Knepley {
201c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
202c4eade1cSMatthew G. Knepley 
203c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
204c4eade1cSMatthew G. Knepley   ierr = PetscMalloc1(1, box);CHKERRQ(ierr);
205c4eade1cSMatthew G. Knepley   ierr = PetscGridHashInitialize_Internal(*box, dim, point);CHKERRQ(ierr);
206c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
207c4eade1cSMatthew G. Knepley }
208c4eade1cSMatthew G. Knepley 
209c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[])
210c4eade1cSMatthew G. Knepley {
211c4eade1cSMatthew G. Knepley   PetscInt d;
212c4eade1cSMatthew G. Knepley 
213c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
214c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
215c4eade1cSMatthew G. Knepley     box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d]));
216c4eade1cSMatthew G. Knepley     box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d]));
217c4eade1cSMatthew G. Knepley   }
218c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
219c4eade1cSMatthew G. Knepley }
220c4eade1cSMatthew G. Knepley 
22162a38674SMatthew G. Knepley /*
22262a38674SMatthew G. Knepley   PetscGridHashSetGrid - Divide the grid into boxes
22362a38674SMatthew G. Knepley 
22462a38674SMatthew G. Knepley   Not collective
22562a38674SMatthew G. Knepley 
22662a38674SMatthew G. Knepley   Input Parameters:
22762a38674SMatthew G. Knepley + box - The grid hash object
22862a38674SMatthew G. Knepley . n   - The number of boxes in each dimension, or PETSC_DETERMINE
22962a38674SMatthew G. Knepley - h   - The box size in each dimension, only used if n[d] == PETSC_DETERMINE
23062a38674SMatthew G. Knepley 
23162a38674SMatthew G. Knepley   Level: developer
23262a38674SMatthew G. Knepley 
23362a38674SMatthew G. Knepley .seealso: PetscGridHashCreate()
23462a38674SMatthew G. Knepley */
235c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[])
236c4eade1cSMatthew G. Knepley {
237c4eade1cSMatthew G. Knepley   PetscInt d;
238c4eade1cSMatthew G. Knepley 
239c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
240c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
241c4eade1cSMatthew G. Knepley     box->extent[d] = box->upper[d] - box->lower[d];
242c4eade1cSMatthew G. Knepley     if (n[d] == PETSC_DETERMINE) {
243c4eade1cSMatthew G. Knepley       box->h[d] = h[d];
244c4eade1cSMatthew G. Knepley       box->n[d] = PetscCeilReal(box->extent[d]/h[d]);
245c4eade1cSMatthew G. Knepley     } else {
246c4eade1cSMatthew G. Knepley       box->n[d] = n[d];
247c4eade1cSMatthew G. Knepley       box->h[d] = box->extent[d]/n[d];
248c4eade1cSMatthew G. Knepley     }
249c4eade1cSMatthew G. Knepley   }
250c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
251c4eade1cSMatthew G. Knepley }
252c4eade1cSMatthew G. Knepley 
25362a38674SMatthew G. Knepley /*
25462a38674SMatthew G. Knepley   PetscGridHashGetEnclosingBox - Find the grid boxes containing each input point
25562a38674SMatthew G. Knepley 
25662a38674SMatthew G. Knepley   Not collective
25762a38674SMatthew G. Knepley 
25862a38674SMatthew G. Knepley   Input Parameters:
25962a38674SMatthew G. Knepley + box       - The grid hash object
26062a38674SMatthew G. Knepley . numPoints - The number of input points
26162a38674SMatthew G. Knepley - points    - The input point coordinates
26262a38674SMatthew G. Knepley 
26362a38674SMatthew G. Knepley   Output Parameters:
26462a38674SMatthew G. Knepley + dboxes    - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
26562a38674SMatthew G. Knepley - boxes     - An array of numPoints integers expressing the enclosing box as single number, or NULL
26662a38674SMatthew G. Knepley 
26762a38674SMatthew G. Knepley   Level: developer
26862a38674SMatthew G. Knepley 
26962a38674SMatthew G. Knepley .seealso: PetscGridHashCreate()
27062a38674SMatthew G. Knepley */
2711c6dfc3eSMatthew G. Knepley PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[])
272c4eade1cSMatthew G. Knepley {
273c4eade1cSMatthew G. Knepley   const PetscReal *lower = box->lower;
274c4eade1cSMatthew G. Knepley   const PetscReal *upper = box->upper;
275c4eade1cSMatthew G. Knepley   const PetscReal *h     = box->h;
276c4eade1cSMatthew G. Knepley   const PetscInt  *n     = box->n;
277c4eade1cSMatthew G. Knepley   const PetscInt   dim   = box->dim;
278c4eade1cSMatthew G. Knepley   PetscInt         d, p;
279c4eade1cSMatthew G. Knepley 
280c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
281c4eade1cSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
282c4eade1cSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
2831c6dfc3eSMatthew G. Knepley       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]);
284c4eade1cSMatthew G. Knepley 
2851c6dfc3eSMatthew G. Knepley       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1;
2862a705cacSMatthew G. Knepley       if (dbox == -1   && PetscAbsReal(PetscRealPart(points[p*dim+d]) - lower[d]) < 1.0e-9) dbox = 0;
287c4eade1cSMatthew G. Knepley       if (dbox < 0 || dbox >= n[d]) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input point %d (%g, %g, %g) is outside of our bounding box",
2881c6dfc3eSMatthew G. Knepley                                              p, PetscRealPart(points[p*dim+0]), dim > 1 ? PetscRealPart(points[p*dim+1]) : 0.0, dim > 2 ? PetscRealPart(points[p*dim+2]) : 0.0);
289c4eade1cSMatthew G. Knepley       dboxes[p*dim+d] = dbox;
290c4eade1cSMatthew G. Knepley     }
291c4eade1cSMatthew G. Knepley     if (boxes) for (d = 1, boxes[p] = dboxes[p*dim]; d < dim; ++d) boxes[p] += dboxes[p*dim+d]*n[d-1];
292c4eade1cSMatthew G. Knepley   }
293c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
294c4eade1cSMatthew G. Knepley }
295c4eade1cSMatthew G. Knepley 
296af74b616SDave May /*
297af74b616SDave May  PetscGridHashGetEnclosingBoxQuery - Find the grid boxes containing each input point
298af74b616SDave May 
299af74b616SDave May  Not collective
300af74b616SDave May 
301af74b616SDave May   Input Parameters:
302af74b616SDave May + box       - The grid hash object
303af74b616SDave May . numPoints - The number of input points
304af74b616SDave May - points    - The input point coordinates
305af74b616SDave May 
306af74b616SDave May   Output Parameters:
307af74b616SDave May + dboxes    - An array of numPoints*dim integers expressing the enclosing box as (i_0, i_1, ..., i_dim)
308af74b616SDave May . boxes     - An array of numPoints integers expressing the enclosing box as single number, or NULL
309af74b616SDave May - found     - Flag indicating if point was located within a box
310af74b616SDave May 
311af74b616SDave May   Level: developer
312af74b616SDave May 
313af74b616SDave May .seealso: PetscGridHashGetEnclosingBox()
314af74b616SDave May */
315af74b616SDave May PetscErrorCode PetscGridHashGetEnclosingBoxQuery(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[],PetscBool *found)
316af74b616SDave May {
317af74b616SDave May   const PetscReal *lower = box->lower;
318af74b616SDave May   const PetscReal *upper = box->upper;
319af74b616SDave May   const PetscReal *h     = box->h;
320af74b616SDave May   const PetscInt  *n     = box->n;
321af74b616SDave May   const PetscInt   dim   = box->dim;
322af74b616SDave May   PetscInt         d, p;
323af74b616SDave May 
324af74b616SDave May   PetscFunctionBegin;
325af74b616SDave May   *found = PETSC_FALSE;
326af74b616SDave May   for (p = 0; p < numPoints; ++p) {
327af74b616SDave May     for (d = 0; d < dim; ++d) {
328af74b616SDave May       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]);
329af74b616SDave May 
330af74b616SDave May       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1;
331af74b616SDave May       if (dbox < 0 || dbox >= n[d]) {
332af74b616SDave May         PetscFunctionReturn(0);
333af74b616SDave May       }
334af74b616SDave May       dboxes[p*dim+d] = dbox;
335af74b616SDave May     }
336af74b616SDave May     if (boxes) for (d = 1, boxes[p] = dboxes[p*dim]; d < dim; ++d) boxes[p] += dboxes[p*dim+d]*n[d-1];
337af74b616SDave May   }
338af74b616SDave May   *found = PETSC_TRUE;
339af74b616SDave May   PetscFunctionReturn(0);
340af74b616SDave May }
341af74b616SDave May 
342c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashDestroy(PetscGridHash *box)
343c4eade1cSMatthew G. Knepley {
344c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
345c4eade1cSMatthew G. Knepley 
346c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
347c4eade1cSMatthew G. Knepley   if (*box) {
348c4eade1cSMatthew G. Knepley     ierr = PetscSectionDestroy(&(*box)->cellSection);CHKERRQ(ierr);
349c4eade1cSMatthew G. Knepley     ierr = ISDestroy(&(*box)->cells);CHKERRQ(ierr);
350c4eade1cSMatthew G. Knepley     ierr = DMLabelDestroy(&(*box)->cellsSparse);CHKERRQ(ierr);
351c4eade1cSMatthew G. Knepley   }
352c4eade1cSMatthew G. Knepley   ierr = PetscFree(*box);CHKERRQ(ierr);
353c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
354c4eade1cSMatthew G. Knepley }
355c4eade1cSMatthew G. Knepley 
356cafe43deSMatthew G. Knepley PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell)
357cafe43deSMatthew G. Knepley {
358cafe43deSMatthew G. Knepley   PetscInt       coneSize;
359cafe43deSMatthew G. Knepley   PetscErrorCode ierr;
360cafe43deSMatthew G. Knepley 
361cafe43deSMatthew G. Knepley   PetscFunctionBegin;
362cafe43deSMatthew G. Knepley   switch (dim) {
363cafe43deSMatthew G. Knepley   case 2:
364cafe43deSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr);
365cafe43deSMatthew G. Knepley     switch (coneSize) {
366cafe43deSMatthew G. Knepley     case 3:
367cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
368cafe43deSMatthew G. Knepley       break;
369cafe43deSMatthew G. Knepley     case 4:
370cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_General_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
371cafe43deSMatthew G. Knepley       break;
372cafe43deSMatthew G. Knepley     default:
373cafe43deSMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize);
374cafe43deSMatthew G. Knepley     }
375cafe43deSMatthew G. Knepley     break;
376cafe43deSMatthew G. Knepley   case 3:
377cafe43deSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr);
378cafe43deSMatthew G. Knepley     switch (coneSize) {
379cafe43deSMatthew G. Knepley     case 4:
380cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
381cafe43deSMatthew G. Knepley       break;
382cafe43deSMatthew G. Knepley     case 6:
383cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
384cafe43deSMatthew G. Knepley       break;
385cafe43deSMatthew G. Knepley     default:
386cafe43deSMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize);
387cafe43deSMatthew G. Knepley     }
388cafe43deSMatthew G. Knepley     break;
389cafe43deSMatthew G. Knepley   default:
390cafe43deSMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for mesh dimension %D", dim);
391cafe43deSMatthew G. Knepley   }
392cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
393cafe43deSMatthew G. Knepley }
394cafe43deSMatthew G. Knepley 
39562a38674SMatthew G. Knepley /*
39662a38674SMatthew G. Knepley   DMPlexClosestPoint_Internal - Returns the closest point in the cell to the given point
39762a38674SMatthew G. Knepley */
39862a38674SMatthew G. Knepley PetscErrorCode DMPlexClosestPoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cell, PetscReal cpoint[])
39962a38674SMatthew G. Knepley {
40062a38674SMatthew G. Knepley   PetscInt       coneSize;
40162a38674SMatthew G. Knepley   PetscErrorCode ierr;
40262a38674SMatthew G. Knepley 
40362a38674SMatthew G. Knepley   PetscFunctionBegin;
40462a38674SMatthew G. Knepley   switch (dim) {
40562a38674SMatthew G. Knepley   case 2:
40662a38674SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
40762a38674SMatthew G. Knepley     switch (coneSize) {
40862a38674SMatthew G. Knepley     case 3:
40962a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_Simplex_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
41062a38674SMatthew G. Knepley       break;
41162a38674SMatthew G. Knepley #if 0
41262a38674SMatthew G. Knepley     case 4:
41362a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_General_2D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
41462a38674SMatthew G. Knepley       break;
41562a38674SMatthew G. Knepley #endif
41662a38674SMatthew G. Knepley     default:
41762a38674SMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell with cone size %D", coneSize);
41862a38674SMatthew G. Knepley     }
41962a38674SMatthew G. Knepley     break;
42062a38674SMatthew G. Knepley #if 0
42162a38674SMatthew G. Knepley   case 3:
42262a38674SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
42362a38674SMatthew G. Knepley     switch (coneSize) {
42462a38674SMatthew G. Knepley     case 4:
42562a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_Simplex_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
42662a38674SMatthew G. Knepley       break;
42762a38674SMatthew G. Knepley     case 6:
42862a38674SMatthew G. Knepley       ierr = DMPlexClosestPoint_General_3D_Internal(dm, point, cell, cpoint);CHKERRQ(ierr);
42962a38674SMatthew G. Knepley       break;
43062a38674SMatthew G. Knepley     default:
43162a38674SMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for cell with cone size %D", coneSize);
43262a38674SMatthew G. Knepley     }
43362a38674SMatthew G. Knepley     break;
43462a38674SMatthew G. Knepley #endif
43562a38674SMatthew G. Knepley   default:
43662a38674SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No closest point location for mesh dimension %D", dim);
43762a38674SMatthew G. Knepley   }
43862a38674SMatthew G. Knepley   PetscFunctionReturn(0);
43962a38674SMatthew G. Knepley }
44062a38674SMatthew G. Knepley 
44162a38674SMatthew G. Knepley /*
44262a38674SMatthew G. Knepley   DMPlexComputeGridHash_Internal - Create a grid hash structure covering the Plex
44362a38674SMatthew G. Knepley 
44462a38674SMatthew G. Knepley   Collective on DM
44562a38674SMatthew G. Knepley 
44662a38674SMatthew G. Knepley   Input Parameter:
44762a38674SMatthew G. Knepley . dm - The Plex
44862a38674SMatthew G. Knepley 
44962a38674SMatthew G. Knepley   Output Parameter:
45062a38674SMatthew G. Knepley . localBox - The grid hash object
45162a38674SMatthew G. Knepley 
45262a38674SMatthew G. Knepley   Level: developer
45362a38674SMatthew G. Knepley 
45462a38674SMatthew G. Knepley .seealso: PetscGridHashCreate(), PetscGridHashGetEnclosingBox()
45562a38674SMatthew G. Knepley */
456cafe43deSMatthew G. Knepley PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox)
457cafe43deSMatthew G. Knepley {
458cafe43deSMatthew G. Knepley   MPI_Comm           comm;
459cafe43deSMatthew G. Knepley   PetscGridHash      lbox;
460cafe43deSMatthew G. Knepley   Vec                coordinates;
461cafe43deSMatthew G. Knepley   PetscSection       coordSection;
462cafe43deSMatthew G. Knepley   Vec                coordsLocal;
463cafe43deSMatthew G. Knepley   const PetscScalar *coords;
464722d0f5cSMatthew G. Knepley   PetscInt          *dboxes, *boxes;
465cafe43deSMatthew G. Knepley   PetscInt           n[3] = {10, 10, 10};
4661d0c6c94SMatthew G. Knepley   PetscInt           dim, N, cStart, cEnd, cMax, c, i;
467cafe43deSMatthew G. Knepley   PetscErrorCode     ierr;
468cafe43deSMatthew G. Knepley 
469cafe43deSMatthew G. Knepley   PetscFunctionBegin;
470cafe43deSMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
471cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
472cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
4735b3353d8SMatthew G. Knepley   if (dim != 2) SETERRQ(comm, PETSC_ERR_SUP, "I have only coded this for 2D");
474cafe43deSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
475cafe43deSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
476cafe43deSMatthew G. Knepley   ierr = PetscGridHashCreate(comm, dim, coords, &lbox);CHKERRQ(ierr);
477cafe43deSMatthew G. Knepley   for (i = 0; i < N; i += dim) {ierr = PetscGridHashEnlarge(lbox, &coords[i]);CHKERRQ(ierr);}
478cafe43deSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
4799cb35068SDave May   ierr = PetscOptionsGetInt(NULL,NULL,"-dm_plex_hash_box_nijk",&n[0],NULL);CHKERRQ(ierr);
4809cb35068SDave May   n[1] = n[0];
4819cb35068SDave May   n[2] = n[0];
482cafe43deSMatthew G. Knepley   ierr = PetscGridHashSetGrid(lbox, n, NULL);CHKERRQ(ierr);
483cafe43deSMatthew G. Knepley #if 0
484cafe43deSMatthew G. Knepley   /* Could define a custom reduction to merge these */
485b2566f29SBarry Smith   ierr = MPIU_Allreduce(lbox->lower, gbox->lower, 3, MPIU_REAL, MPI_MIN, comm);CHKERRQ(ierr);
486b2566f29SBarry Smith   ierr = MPIU_Allreduce(lbox->upper, gbox->upper, 3, MPIU_REAL, MPI_MAX, comm);CHKERRQ(ierr);
487cafe43deSMatthew G. Knepley #endif
488cafe43deSMatthew G. Knepley   /* Is there a reason to snap the local bounding box to a division of the global box? */
489cafe43deSMatthew G. Knepley   /* Should we compute all overlaps of local boxes? We could do this with a rendevouz scheme partitioning the global box */
490cafe43deSMatthew G. Knepley   /* Create label */
491cafe43deSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
4921d0c6c94SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
4931d0c6c94SMatthew G. Knepley   if (cMax >= 0) cEnd = PetscMin(cEnd, cMax);
494cafe43deSMatthew G. Knepley   ierr = DMLabelCreate("cells", &lbox->cellsSparse);CHKERRQ(ierr);
495cafe43deSMatthew G. Knepley   ierr = DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd);CHKERRQ(ierr);
496722d0f5cSMatthew G. Knepley   /* Compute boxes which overlap each cell: http://stackoverflow.com/questions/13790208/triangle-square-intersection-test-in-2d */
497cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
498cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
49938353de4SMatthew G. Knepley   ierr = PetscCalloc2(16 * dim, &dboxes, 16, &boxes);CHKERRQ(ierr);
500cafe43deSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
501cafe43deSMatthew G. Knepley     const PetscReal *h       = lbox->h;
502cafe43deSMatthew G. Knepley     PetscScalar     *ccoords = NULL;
50338353de4SMatthew G. Knepley     PetscInt         csize   = 0;
504cafe43deSMatthew G. Knepley     PetscScalar      point[3];
505cafe43deSMatthew G. Knepley     PetscInt         dlim[6], d, e, i, j, k;
506cafe43deSMatthew G. Knepley 
507cafe43deSMatthew G. Knepley     /* Find boxes enclosing each vertex */
50838353de4SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &csize, &ccoords);CHKERRQ(ierr);
50938353de4SMatthew G. Knepley     ierr = PetscGridHashGetEnclosingBox(lbox, csize/dim, ccoords, dboxes, boxes);CHKERRQ(ierr);
510722d0f5cSMatthew G. Knepley     /* Mark cells containing the vertices */
51138353de4SMatthew G. Knepley     for (e = 0; e < csize/dim; ++e) {ierr = DMLabelSetValue(lbox->cellsSparse, c, boxes[e]);CHKERRQ(ierr);}
512cafe43deSMatthew G. Knepley     /* Get grid of boxes containing these */
513cafe43deSMatthew G. Knepley     for (d = 0;   d < dim; ++d) {dlim[d*2+0] = dlim[d*2+1] = dboxes[d];}
5142291669eSMatthew G. Knepley     for (d = dim; d < 3;   ++d) {dlim[d*2+0] = dlim[d*2+1] = 0;}
515cafe43deSMatthew G. Knepley     for (e = 1; e < dim+1; ++e) {
516cafe43deSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
517cafe43deSMatthew G. Knepley         dlim[d*2+0] = PetscMin(dlim[d*2+0], dboxes[e*dim+d]);
518cafe43deSMatthew G. Knepley         dlim[d*2+1] = PetscMax(dlim[d*2+1], dboxes[e*dim+d]);
519cafe43deSMatthew G. Knepley       }
520cafe43deSMatthew G. Knepley     }
521fea14342SMatthew G. Knepley     /* Check for intersection of box with cell */
522cafe43deSMatthew 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]) {
523cafe43deSMatthew 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]) {
524cafe43deSMatthew 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]) {
525cafe43deSMatthew G. Knepley           const PetscInt box = (k*lbox->n[1] + j)*lbox->n[0] + i;
526cafe43deSMatthew G. Knepley           PetscScalar    cpoint[3];
527fea14342SMatthew G. Knepley           PetscInt       cell, edge, ii, jj, kk;
528cafe43deSMatthew G. Knepley 
529fea14342SMatthew G. Knepley           /* Check whether cell contains any vertex of these subboxes TODO vectorize this */
530cafe43deSMatthew G. Knepley           for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) {
531cafe43deSMatthew G. Knepley             for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) {
532cafe43deSMatthew G. Knepley               for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) {
533cafe43deSMatthew G. Knepley 
534cafe43deSMatthew G. Knepley                 ierr = DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell);CHKERRQ(ierr);
535cafe43deSMatthew G. Knepley                 if (cell >= 0) {DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); ii = jj = kk = 2;}
536cafe43deSMatthew G. Knepley               }
537cafe43deSMatthew G. Knepley             }
538cafe43deSMatthew G. Knepley           }
539fea14342SMatthew G. Knepley           /* Check whether cell edge intersects any edge of these subboxes TODO vectorize this */
540fea14342SMatthew G. Knepley           for (edge = 0; edge < dim+1; ++edge) {
541fea14342SMatthew G. Knepley             PetscReal segA[6], segB[6];
542fea14342SMatthew G. Knepley 
543aab5bcd8SJed Brown             if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected dim %d > 3",dim);
544fea14342SMatthew 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]);}
545fea14342SMatthew G. Knepley             for (kk = 0; kk < (dim > 2 ? 2 : 1); ++kk) {
5469a128ed2SMatthew G. Knepley               if (dim > 2) {segB[2]     = PetscRealPart(point[2]);
5479a128ed2SMatthew G. Knepley                             segB[dim+2] = PetscRealPart(point[2]) + kk*h[2];}
548fea14342SMatthew G. Knepley               for (jj = 0; jj < (dim > 1 ? 2 : 1); ++jj) {
5499a128ed2SMatthew G. Knepley                 if (dim > 1) {segB[1]     = PetscRealPart(point[1]);
5509a128ed2SMatthew G. Knepley                               segB[dim+1] = PetscRealPart(point[1]) + jj*h[1];}
551fea14342SMatthew G. Knepley                 for (ii = 0; ii < 2; ++ii) {
552fea14342SMatthew G. Knepley                   PetscBool intersects;
553fea14342SMatthew G. Knepley 
5549a128ed2SMatthew G. Knepley                   segB[0]     = PetscRealPart(point[0]);
5559a128ed2SMatthew G. Knepley                   segB[dim+0] = PetscRealPart(point[0]) + ii*h[0];
556fea14342SMatthew G. Knepley                   ierr = DMPlexGetLineIntersection_2D_Internal(segA, segB, NULL, &intersects);CHKERRQ(ierr);
557fea14342SMatthew G. Knepley                   if (intersects) {DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); edge = ii = jj = kk = dim+1;}
558cafe43deSMatthew G. Knepley                 }
559cafe43deSMatthew G. Knepley               }
560cafe43deSMatthew G. Knepley             }
561cafe43deSMatthew G. Knepley           }
562fea14342SMatthew G. Knepley         }
563fea14342SMatthew G. Knepley       }
564fea14342SMatthew G. Knepley     }
565fea14342SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords);CHKERRQ(ierr);
566fea14342SMatthew G. Knepley   }
567722d0f5cSMatthew G. Knepley   ierr = PetscFree2(dboxes, boxes);CHKERRQ(ierr);
568cafe43deSMatthew G. Knepley   ierr = DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells);CHKERRQ(ierr);
569cafe43deSMatthew G. Knepley   ierr = DMLabelDestroy(&lbox->cellsSparse);CHKERRQ(ierr);
570cafe43deSMatthew G. Knepley   *localBox = lbox;
571cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
572cafe43deSMatthew G. Knepley }
573cafe43deSMatthew G. Knepley 
57462a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, DMPointLocationType ltype, PetscSF cellSF)
575ccd2543fSMatthew G Knepley {
576cafe43deSMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
577af74b616SDave May   PetscBool       hash = mesh->useHashLocation, reuse = PETSC_FALSE;
5783a93e3b7SToby Isaac   PetscInt        bs, numPoints, p, numFound, *found = NULL;
5799cb35068SDave May   PetscInt        dim, cStart, cEnd, cMax, numCells, c, d;
580cafe43deSMatthew G. Knepley   const PetscInt *boxCells;
5813a93e3b7SToby Isaac   PetscSFNode    *cells;
582ccd2543fSMatthew G Knepley   PetscScalar    *a;
5833a93e3b7SToby Isaac   PetscMPIInt     result;
584af74b616SDave May   PetscLogDouble  t0,t1;
5859cb35068SDave May   PetscReal       gmin[3],gmax[3];
5869cb35068SDave May   PetscInt        terminating_query_type[] = { 0, 0, 0 };
587ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
588ccd2543fSMatthew G Knepley 
589ccd2543fSMatthew G Knepley   PetscFunctionBegin;
590af74b616SDave May   ierr = PetscTime(&t0);CHKERRQ(ierr);
591080342d1SMatthew 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.");
592cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
593cafe43deSMatthew G. Knepley   ierr = VecGetBlockSize(v, &bs);CHKERRQ(ierr);
5943a93e3b7SToby Isaac   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)cellSF),PETSC_COMM_SELF,&result);CHKERRQ(ierr);
5953a93e3b7SToby Isaac   if (result != MPI_IDENT && result != MPI_CONGRUENT) SETERRQ(PetscObjectComm((PetscObject)cellSF),PETSC_ERR_SUP, "Trying parallel point location: only local point location supported");
596cafe43deSMatthew 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);
597ccd2543fSMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
598ccd2543fSMatthew G Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
599ccd2543fSMatthew G Knepley   if (cMax >= 0) cEnd = PetscMin(cEnd, cMax);
600ccd2543fSMatthew G Knepley   ierr = VecGetLocalSize(v, &numPoints);CHKERRQ(ierr);
601ccd2543fSMatthew G Knepley   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
602ccd2543fSMatthew G Knepley   numPoints /= bs;
603af74b616SDave May   {
604af74b616SDave May     const PetscSFNode *sf_cells;
605af74b616SDave May 
606af74b616SDave May     ierr = PetscSFGetGraph(cellSF,NULL,NULL,NULL,&sf_cells);CHKERRQ(ierr);
607af74b616SDave May     if (sf_cells) {
608af74b616SDave May       ierr = PetscInfo(dm,"[DMLocatePoints_Plex] Re-using existing StarForest node list\n");CHKERRQ(ierr);
609af74b616SDave May       cells = (PetscSFNode*)sf_cells;
610af74b616SDave May       reuse = PETSC_TRUE;
611af74b616SDave May     } else {
612af74b616SDave May       ierr = PetscInfo(dm,"[DMLocatePoints_Plex] Creating and initializing new StarForest node list\n");CHKERRQ(ierr);
613785e854fSJed Brown       ierr = PetscMalloc1(numPoints, &cells);CHKERRQ(ierr);
614af74b616SDave May       /* initialize cells if created */
615af74b616SDave May       for (p=0; p<numPoints; p++) {
616af74b616SDave May         cells[p].rank  = 0;
617af74b616SDave May         cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
618af74b616SDave May       }
619af74b616SDave May     }
620af74b616SDave May   }
6219cb35068SDave May   /* define domain bounding box */
6229cb35068SDave May   {
6239cb35068SDave May     Vec coorglobal;
6249cb35068SDave May 
6259cb35068SDave May     ierr = DMGetCoordinates(dm,&coorglobal);CHKERRQ(ierr);
6269cb35068SDave May     ierr = VecStrideMaxAll(coorglobal,NULL,gmax);CHKERRQ(ierr);
6279cb35068SDave May     ierr = VecStrideMinAll(coorglobal,NULL,gmin);CHKERRQ(ierr);
6289cb35068SDave May   }
629953fc75cSMatthew G. Knepley   if (hash) {
630ac6ec2abSMatthew G. Knepley     if (!mesh->lbox) {ierr = PetscInfo(dm, "Initializing grid hashing");CHKERRQ(ierr);ierr = DMPlexComputeGridHash_Internal(dm, &mesh->lbox);CHKERRQ(ierr);}
631cafe43deSMatthew G. Knepley     /* Designate the local box for each point */
632cafe43deSMatthew G. Knepley     /* Send points to correct process */
633cafe43deSMatthew G. Knepley     /* Search cells that lie in each subbox */
634cafe43deSMatthew G. Knepley     /*   Should we bin points before doing search? */
635cafe43deSMatthew G. Knepley     ierr = ISGetIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);
636953fc75cSMatthew G. Knepley   }
6373a93e3b7SToby Isaac   for (p = 0, numFound = 0; p < numPoints; ++p) {
638ccd2543fSMatthew G Knepley     const PetscScalar *point = &a[p*bs];
639e56f9228SJed Brown     PetscInt           dbin[3] = {-1,-1,-1}, bin, cell = -1, cellOffset;
6409cb35068SDave May     PetscBool          point_outside_domain = PETSC_FALSE;
641ccd2543fSMatthew G Knepley 
6429cb35068SDave May     /* check bounding box of domain */
6439cb35068SDave May     for (d=0; d<dim; d++) {
644a5f152d1SDave May       if (PetscRealPart(point[d]) < gmin[d]) { point_outside_domain = PETSC_TRUE; break; }
645a5f152d1SDave May       if (PetscRealPart(point[d]) > gmax[d]) { point_outside_domain = PETSC_TRUE; break; }
6469cb35068SDave May     }
6479cb35068SDave May     if (point_outside_domain) {
648e9b685f5SMatthew G. Knepley       cells[p].rank = 0;
649e9b685f5SMatthew G. Knepley       cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
6509cb35068SDave May       terminating_query_type[0]++;
6519cb35068SDave May       continue;
6529cb35068SDave May     }
653ccd2543fSMatthew G Knepley 
654af74b616SDave May     /* check initial values in cells[].index - abort early if found */
655af74b616SDave May     if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) {
656af74b616SDave May       c = cells[p].index;
6573a93e3b7SToby Isaac       cells[p].index = DMLOCATEPOINT_POINT_NOT_FOUND;
658af74b616SDave May       ierr = DMPlexLocatePoint_Internal(dm, dim, point, c, &cell);CHKERRQ(ierr);
659af74b616SDave May       if (cell >= 0) {
660af74b616SDave May         cells[p].rank = 0;
661af74b616SDave May         cells[p].index = cell;
662af74b616SDave May         numFound++;
663af74b616SDave May       }
664af74b616SDave May     }
6659cb35068SDave May     if (cells[p].index != DMLOCATEPOINT_POINT_NOT_FOUND) {
6669cb35068SDave May       terminating_query_type[1]++;
6679cb35068SDave May       continue;
6689cb35068SDave May     }
669af74b616SDave May 
670953fc75cSMatthew G. Knepley     if (hash) {
671af74b616SDave May       PetscBool found_box;
672af74b616SDave May 
673af74b616SDave May       /* allow for case that point is outside box - abort early */
674af74b616SDave May       ierr = PetscGridHashGetEnclosingBoxQuery(mesh->lbox, 1, point, dbin, &bin,&found_box);CHKERRQ(ierr);
675af74b616SDave May       if (found_box) {
676cafe43deSMatthew G. Knepley         /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */
677cafe43deSMatthew G. Knepley         ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
678cafe43deSMatthew G. Knepley         ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
679cafe43deSMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
680cafe43deSMatthew G. Knepley           ierr = DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell);CHKERRQ(ierr);
6813a93e3b7SToby Isaac           if (cell >= 0) {
6823a93e3b7SToby Isaac             cells[p].rank = 0;
6833a93e3b7SToby Isaac             cells[p].index = cell;
6843a93e3b7SToby Isaac             numFound++;
6859cb35068SDave May             terminating_query_type[2]++;
6863a93e3b7SToby Isaac             break;
687ccd2543fSMatthew G Knepley           }
6883a93e3b7SToby Isaac         }
689af74b616SDave May       }
690953fc75cSMatthew G. Knepley     } else {
691953fc75cSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
692953fc75cSMatthew G. Knepley         ierr = DMPlexLocatePoint_Internal(dm, dim, point, c, &cell);CHKERRQ(ierr);
6933a93e3b7SToby Isaac         if (cell >= 0) {
6943a93e3b7SToby Isaac           cells[p].rank = 0;
6953a93e3b7SToby Isaac           cells[p].index = cell;
6963a93e3b7SToby Isaac           numFound++;
6979cb35068SDave May           terminating_query_type[2]++;
6983a93e3b7SToby Isaac           break;
699953fc75cSMatthew G. Knepley         }
700953fc75cSMatthew G. Knepley       }
7013a93e3b7SToby Isaac     }
702ccd2543fSMatthew G Knepley   }
703953fc75cSMatthew G. Knepley   if (hash) {ierr = ISRestoreIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);}
70462a38674SMatthew G. Knepley   if (ltype == DM_POINTLOCATION_NEAREST && hash && numFound < numPoints) {
70562a38674SMatthew G. Knepley     for (p = 0; p < numPoints; p++) {
70662a38674SMatthew G. Knepley       const PetscScalar *point = &a[p*bs];
70762a38674SMatthew G. Knepley       PetscReal          cpoint[3], diff[3], dist, distMax = PETSC_MAX_REAL;
708e56f9228SJed Brown       PetscInt           dbin[3] = {-1,-1,-1}, bin, cellOffset, d;
70962a38674SMatthew G. Knepley 
710e9b685f5SMatthew G. Knepley       if (cells[p].index < 0) {
71162a38674SMatthew G. Knepley         ++numFound;
71262a38674SMatthew G. Knepley         ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr);
71362a38674SMatthew G. Knepley         ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
71462a38674SMatthew G. Knepley         ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
71562a38674SMatthew G. Knepley         for (c = cellOffset; c < cellOffset + numCells; ++c) {
71662a38674SMatthew G. Knepley           ierr = DMPlexClosestPoint_Internal(dm, dim, point, boxCells[c], cpoint);CHKERRQ(ierr);
717b716b415SMatthew G. Knepley           for (d = 0; d < dim; ++d) diff[d] = cpoint[d] - PetscRealPart(point[d]);
71862a38674SMatthew G. Knepley           dist = DMPlex_NormD_Internal(dim, diff);
71962a38674SMatthew G. Knepley           if (dist < distMax) {
72062a38674SMatthew G. Knepley             for (d = 0; d < dim; ++d) a[p*bs+d] = cpoint[d];
72162a38674SMatthew G. Knepley             cells[p].rank  = 0;
72262a38674SMatthew G. Knepley             cells[p].index = boxCells[c];
72362a38674SMatthew G. Knepley             distMax = dist;
72462a38674SMatthew G. Knepley             break;
72562a38674SMatthew G. Knepley           }
72662a38674SMatthew G. Knepley         }
72762a38674SMatthew G. Knepley       }
72862a38674SMatthew G. Knepley     }
72962a38674SMatthew G. Knepley   }
73062a38674SMatthew G. Knepley   /* This code is only be relevant when interfaced to parallel point location */
731cafe43deSMatthew G. Knepley   /* Check for highest numbered proc that claims a point (do we care?) */
7322d1fa6caSMatthew G. Knepley   if (ltype == DM_POINTLOCATION_REMOVE && numFound < numPoints) {
7333a93e3b7SToby Isaac     ierr = PetscMalloc1(numFound,&found);CHKERRQ(ierr);
7343a93e3b7SToby Isaac     for (p = 0, numFound = 0; p < numPoints; p++) {
7353a93e3b7SToby Isaac       if (cells[p].rank >= 0 && cells[p].index >= 0) {
7363a93e3b7SToby Isaac         if (numFound < p) {
7373a93e3b7SToby Isaac           cells[numFound] = cells[p];
7383a93e3b7SToby Isaac         }
7393a93e3b7SToby Isaac         found[numFound++] = p;
7403a93e3b7SToby Isaac       }
7413a93e3b7SToby Isaac     }
7423a93e3b7SToby Isaac   }
74362a38674SMatthew G. Knepley   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
744af74b616SDave May   if (!reuse) {
7453a93e3b7SToby Isaac     ierr = PetscSFSetGraph(cellSF, cEnd - cStart, numFound, found, PETSC_OWN_POINTER, cells, PETSC_OWN_POINTER);CHKERRQ(ierr);
746af74b616SDave May   }
747af74b616SDave May   ierr = PetscTime(&t1);CHKERRQ(ierr);
7489cb35068SDave May   if (hash) {
749d6e0b8ebSSatish Balay     ierr = PetscInfo3(dm,"[DMLocatePoints_Plex] terminating_query_type : %D [outside domain] : %D [inside intial cell] : %D [hash]\n",terminating_query_type[0],terminating_query_type[1],terminating_query_type[2]);CHKERRQ(ierr);
7509cb35068SDave May   } else {
751d6e0b8ebSSatish Balay     ierr = PetscInfo3(dm,"[DMLocatePoints_Plex] terminating_query_type : %D [outside domain] : %D [inside intial cell] : %D [brute-force]\n",terminating_query_type[0],terminating_query_type[1],terminating_query_type[2]);CHKERRQ(ierr);
7529cb35068SDave May   }
753af74b616SDave May   ierr = PetscInfo3(dm,"[DMLocatePoints_Plex] npoints %D : time(rank0) %1.2e (sec): points/sec %1.4e\n",numPoints,t1-t0,(double)((double)numPoints/(t1-t0)));CHKERRQ(ierr);
754ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
755ccd2543fSMatthew G Knepley }
756ccd2543fSMatthew G Knepley 
757741bfc07SMatthew G. Knepley /*@C
758741bfc07SMatthew G. Knepley   DMPlexComputeProjection2Dto1D - Rewrite coordinates to be the 1D projection of the 2D coordinates
759741bfc07SMatthew G. Knepley 
760741bfc07SMatthew G. Knepley   Not collective
761741bfc07SMatthew G. Knepley 
762741bfc07SMatthew G. Knepley   Input Parameter:
763741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
764741bfc07SMatthew G. Knepley 
765741bfc07SMatthew G. Knepley   Output Parameters:
766741bfc07SMatthew G. Knepley + coords - The new y-coordinate, and 0 for x
767741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
768741bfc07SMatthew G. Knepley 
769741bfc07SMatthew G. Knepley   Level: developer
770741bfc07SMatthew G. Knepley 
771741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection3Dto1D(), DMPlexComputeProjection3Dto2D()
772741bfc07SMatthew G. Knepley @*/
773741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection2Dto1D(PetscScalar coords[], PetscReal R[])
77417fe8556SMatthew G. Knepley {
77517fe8556SMatthew G. Knepley   const PetscReal x = PetscRealPart(coords[2] - coords[0]);
77617fe8556SMatthew G. Knepley   const PetscReal y = PetscRealPart(coords[3] - coords[1]);
7778b49ba18SBarry Smith   const PetscReal r = PetscSqrtReal(x*x + y*y), c = x/r, s = y/r;
77817fe8556SMatthew G. Knepley 
77917fe8556SMatthew G. Knepley   PetscFunctionBegin;
7801c99cf0cSGeoffrey Irving   R[0] = c; R[1] = -s;
7811c99cf0cSGeoffrey Irving   R[2] = s; R[3] =  c;
78217fe8556SMatthew G. Knepley   coords[0] = 0.0;
7837f07f362SMatthew G. Knepley   coords[1] = r;
78417fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
78517fe8556SMatthew G. Knepley }
78617fe8556SMatthew G. Knepley 
787741bfc07SMatthew G. Knepley /*@C
788741bfc07SMatthew G. Knepley   DMPlexComputeProjection3Dto1D - Rewrite coordinates to be the 1D projection of the 3D coordinates
78928dbe442SToby Isaac 
790741bfc07SMatthew G. Knepley   Not collective
79128dbe442SToby Isaac 
792741bfc07SMatthew G. Knepley   Input Parameter:
793741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
794741bfc07SMatthew G. Knepley 
795741bfc07SMatthew G. Knepley   Output Parameters:
796741bfc07SMatthew G. Knepley + coords - The new y-coordinate, and 0 for x and z
797741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
798741bfc07SMatthew G. Knepley 
799741bfc07SMatthew 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
800741bfc07SMatthew G. Knepley 
801741bfc07SMatthew G. Knepley   Level: developer
802741bfc07SMatthew G. Knepley 
803741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto2D()
804741bfc07SMatthew G. Knepley @*/
805741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto1D(PetscScalar coords[], PetscReal R[])
80628dbe442SToby Isaac {
80728dbe442SToby Isaac   PetscReal      x    = PetscRealPart(coords[3] - coords[0]);
80828dbe442SToby Isaac   PetscReal      y    = PetscRealPart(coords[4] - coords[1]);
80928dbe442SToby Isaac   PetscReal      z    = PetscRealPart(coords[5] - coords[2]);
81028dbe442SToby Isaac   PetscReal      r    = PetscSqrtReal(x*x + y*y + z*z);
81128dbe442SToby Isaac   PetscReal      rinv = 1. / r;
81228dbe442SToby Isaac   PetscFunctionBegin;
81328dbe442SToby Isaac 
81428dbe442SToby Isaac   x *= rinv; y *= rinv; z *= rinv;
81528dbe442SToby Isaac   if (x > 0.) {
81628dbe442SToby Isaac     PetscReal inv1pX   = 1./ (1. + x);
81728dbe442SToby Isaac 
81828dbe442SToby Isaac     R[0] = x; R[1] = -y;              R[2] = -z;
81928dbe442SToby Isaac     R[3] = y; R[4] = 1. - y*y*inv1pX; R[5] =     -y*z*inv1pX;
82028dbe442SToby Isaac     R[6] = z; R[7] =     -y*z*inv1pX; R[8] = 1. - z*z*inv1pX;
82128dbe442SToby Isaac   }
82228dbe442SToby Isaac   else {
82328dbe442SToby Isaac     PetscReal inv1mX   = 1./ (1. - x);
82428dbe442SToby Isaac 
82528dbe442SToby Isaac     R[0] = x; R[1] = z;               R[2] = y;
82628dbe442SToby Isaac     R[3] = y; R[4] =     -y*z*inv1mX; R[5] = 1. - y*y*inv1mX;
82728dbe442SToby Isaac     R[6] = z; R[7] = 1. - z*z*inv1mX; R[8] =     -y*z*inv1mX;
82828dbe442SToby Isaac   }
82928dbe442SToby Isaac   coords[0] = 0.0;
83028dbe442SToby Isaac   coords[1] = r;
83128dbe442SToby Isaac   PetscFunctionReturn(0);
83228dbe442SToby Isaac }
83328dbe442SToby Isaac 
834741bfc07SMatthew G. Knepley /*@
835741bfc07SMatthew G. Knepley   DMPlexComputeProjection3Dto2D - Rewrite coordinates to be the 2D projection of the 3D coordinates
836741bfc07SMatthew G. Knepley 
837741bfc07SMatthew G. Knepley   Not collective
838741bfc07SMatthew G. Knepley 
839741bfc07SMatthew G. Knepley   Input Parameter:
840741bfc07SMatthew G. Knepley . coords - The coordinates of a segment
841741bfc07SMatthew G. Knepley 
842741bfc07SMatthew G. Knepley   Output Parameters:
843741bfc07SMatthew G. Knepley + coords - The new y- and z-coordinates, and 0 for x
844741bfc07SMatthew G. Knepley - R - The rotation which accomplishes the projection
845741bfc07SMatthew G. Knepley 
846741bfc07SMatthew G. Knepley   Level: developer
847741bfc07SMatthew G. Knepley 
848741bfc07SMatthew G. Knepley .seealso: DMPlexComputeProjection2Dto1D(), DMPlexComputeProjection3Dto1D()
849741bfc07SMatthew G. Knepley @*/
850741bfc07SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto2D(PetscInt coordSize, PetscScalar coords[], PetscReal R[])
851ccd2543fSMatthew G Knepley {
8521ee9d5ecSMatthew G. Knepley   PetscReal      x1[3],  x2[3], n[3], norm;
85399dec3a6SMatthew G. Knepley   PetscReal      x1p[3], x2p[3], xnp[3];
8544a217a95SMatthew G. Knepley   PetscReal      sqrtz, alpha;
855ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
85699dec3a6SMatthew G. Knepley   PetscInt       d, e, p;
857ccd2543fSMatthew G Knepley 
858ccd2543fSMatthew G Knepley   PetscFunctionBegin;
859ccd2543fSMatthew G Knepley   /* 0) Calculate normal vector */
860ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
8611ee9d5ecSMatthew G. Knepley     x1[d] = PetscRealPart(coords[1*dim+d] - coords[0*dim+d]);
8621ee9d5ecSMatthew G. Knepley     x2[d] = PetscRealPart(coords[2*dim+d] - coords[0*dim+d]);
863ccd2543fSMatthew G Knepley   }
864ccd2543fSMatthew G Knepley   n[0] = x1[1]*x2[2] - x1[2]*x2[1];
865ccd2543fSMatthew G Knepley   n[1] = x1[2]*x2[0] - x1[0]*x2[2];
866ccd2543fSMatthew G Knepley   n[2] = x1[0]*x2[1] - x1[1]*x2[0];
8678b49ba18SBarry Smith   norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
868ccd2543fSMatthew G Knepley   n[0] /= norm;
869ccd2543fSMatthew G Knepley   n[1] /= norm;
870ccd2543fSMatthew G Knepley   n[2] /= norm;
871ccd2543fSMatthew G Knepley   /* 1) Take the normal vector and rotate until it is \hat z
872ccd2543fSMatthew G Knepley 
873ccd2543fSMatthew G Knepley     Let the normal vector be <nx, ny, nz> and alpha = 1/sqrt(1 - nz^2), then
874ccd2543fSMatthew G Knepley 
875ccd2543fSMatthew G Knepley     R = /  alpha nx nz  alpha ny nz -1/alpha \
876ccd2543fSMatthew G Knepley         | -alpha ny     alpha nx        0    |
877ccd2543fSMatthew G Knepley         \     nx            ny         nz    /
878ccd2543fSMatthew G Knepley 
879ccd2543fSMatthew G Knepley     will rotate the normal vector to \hat z
880ccd2543fSMatthew G Knepley   */
8818b49ba18SBarry Smith   sqrtz = PetscSqrtReal(1.0 - n[2]*n[2]);
88273868372SMatthew G. Knepley   /* Check for n = z */
88373868372SMatthew G. Knepley   if (sqrtz < 1.0e-10) {
8847df32b8bSSanderA     const PetscInt s = PetscSign(n[2]);
8857df32b8bSSanderA     /* If nz < 0, rotate 180 degrees around x-axis */
88699dec3a6SMatthew G. Knepley     for (p = 3; p < coordSize/3; ++p) {
88799dec3a6SMatthew G. Knepley       coords[p*2+0] = PetscRealPart(coords[p*dim+0] - coords[0*dim+0]);
8887df32b8bSSanderA       coords[p*2+1] = (PetscRealPart(coords[p*dim+1] - coords[0*dim+1])) * s;
88973868372SMatthew G. Knepley     }
89099dec3a6SMatthew G. Knepley     coords[0] = 0.0;
89199dec3a6SMatthew G. Knepley     coords[1] = 0.0;
8927df32b8bSSanderA     coords[2] = x1[0];
8937df32b8bSSanderA     coords[3] = x1[1] * s;
8947df32b8bSSanderA     coords[4] = x2[0];
8957df32b8bSSanderA     coords[5] = x2[1] * s;
8967df32b8bSSanderA     R[0] = 1.0;     R[1] = 0.0;     R[2] = 0.0;
8977df32b8bSSanderA     R[3] = 0.0;     R[4] = 1.0 * s; R[5] = 0.0;
8987df32b8bSSanderA     R[6] = 0.0;     R[7] = 0.0;     R[8] = 1.0 * s;
89973868372SMatthew G. Knepley     PetscFunctionReturn(0);
90073868372SMatthew G. Knepley   }
901da18b5e6SMatthew G Knepley   alpha = 1.0/sqrtz;
902ccd2543fSMatthew G Knepley   R[0] =  alpha*n[0]*n[2]; R[1] = alpha*n[1]*n[2]; R[2] = -sqrtz;
903ccd2543fSMatthew G Knepley   R[3] = -alpha*n[1];      R[4] = alpha*n[0];      R[5] = 0.0;
904ccd2543fSMatthew G Knepley   R[6] =  n[0];            R[7] = n[1];            R[8] = n[2];
905ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
906ccd2543fSMatthew G Knepley     x1p[d] = 0.0;
907ccd2543fSMatthew G Knepley     x2p[d] = 0.0;
908ccd2543fSMatthew G Knepley     for (e = 0; e < dim; ++e) {
909ccd2543fSMatthew G Knepley       x1p[d] += R[d*dim+e]*x1[e];
910ccd2543fSMatthew G Knepley       x2p[d] += R[d*dim+e]*x2[e];
911ccd2543fSMatthew G Knepley     }
912ccd2543fSMatthew G Knepley   }
91348120919SToby Isaac   if (PetscAbsReal(x1p[2]) > 10. * PETSC_SMALL) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
91448120919SToby Isaac   if (PetscAbsReal(x2p[2]) > 10. * PETSC_SMALL) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
915ccd2543fSMatthew G Knepley   /* 2) Project to (x, y) */
91699dec3a6SMatthew G. Knepley   for (p = 3; p < coordSize/3; ++p) {
91799dec3a6SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
91899dec3a6SMatthew G. Knepley       xnp[d] = 0.0;
91999dec3a6SMatthew G. Knepley       for (e = 0; e < dim; ++e) {
92099dec3a6SMatthew G. Knepley         xnp[d] += R[d*dim+e]*PetscRealPart(coords[p*dim+e] - coords[0*dim+e]);
92199dec3a6SMatthew G. Knepley       }
92299dec3a6SMatthew G. Knepley       if (d < dim-1) coords[p*2+d] = xnp[d];
92399dec3a6SMatthew G. Knepley     }
92499dec3a6SMatthew G. Knepley   }
925ccd2543fSMatthew G Knepley   coords[0] = 0.0;
926ccd2543fSMatthew G Knepley   coords[1] = 0.0;
927ccd2543fSMatthew G Knepley   coords[2] = x1p[0];
928ccd2543fSMatthew G Knepley   coords[3] = x1p[1];
929ccd2543fSMatthew G Knepley   coords[4] = x2p[0];
930ccd2543fSMatthew G Knepley   coords[5] = x2p[1];
9317f07f362SMatthew G. Knepley   /* Output R^T which rotates \hat z to the input normal */
9327f07f362SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
9337f07f362SMatthew G. Knepley     for (e = d+1; e < dim; ++e) {
9347f07f362SMatthew G. Knepley       PetscReal tmp;
9357f07f362SMatthew G. Knepley 
9367f07f362SMatthew G. Knepley       tmp        = R[d*dim+e];
9377f07f362SMatthew G. Knepley       R[d*dim+e] = R[e*dim+d];
9387f07f362SMatthew G. Knepley       R[e*dim+d] = tmp;
9397f07f362SMatthew G. Knepley     }
9407f07f362SMatthew G. Knepley   }
941ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
942ccd2543fSMatthew G Knepley }
943ccd2543fSMatthew G Knepley 
9446322fe33SJed Brown PETSC_UNUSED
945834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[])
946834e62ceSMatthew G. Knepley {
947834e62ceSMatthew G. Knepley   /* Signed volume is 1/2 the determinant
948834e62ceSMatthew G. Knepley 
949834e62ceSMatthew G. Knepley    |  1  1  1 |
950834e62ceSMatthew G. Knepley    | x0 x1 x2 |
951834e62ceSMatthew G. Knepley    | y0 y1 y2 |
952834e62ceSMatthew G. Knepley 
953834e62ceSMatthew G. Knepley      but if x0,y0 is the origin, we have
954834e62ceSMatthew G. Knepley 
955834e62ceSMatthew G. Knepley    | x1 x2 |
956834e62ceSMatthew G. Knepley    | y1 y2 |
957834e62ceSMatthew G. Knepley   */
958834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1];
959834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1];
960834e62ceSMatthew G. Knepley   PetscReal       M[4], detM;
961834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2;
96286623015SMatthew G. Knepley   M[2] = y1; M[3] = y2;
963923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(&detM, M);
964834e62ceSMatthew G. Knepley   *vol = 0.5*detM;
9653bc0b13bSBarry Smith   (void)PetscLogFlops(5.0);
966834e62ceSMatthew G. Knepley }
967834e62ceSMatthew G. Knepley 
968834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Origin_Internal(PetscReal *vol, PetscReal coords[])
969834e62ceSMatthew G. Knepley {
970923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(vol, coords);
971834e62ceSMatthew G. Knepley   *vol *= 0.5;
972834e62ceSMatthew G. Knepley }
973834e62ceSMatthew G. Knepley 
9746322fe33SJed Brown PETSC_UNUSED
975834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[])
976834e62ceSMatthew G. Knepley {
977834e62ceSMatthew G. Knepley   /* Signed volume is 1/6th of the determinant
978834e62ceSMatthew G. Knepley 
979834e62ceSMatthew G. Knepley    |  1  1  1  1 |
980834e62ceSMatthew G. Knepley    | x0 x1 x2 x3 |
981834e62ceSMatthew G. Knepley    | y0 y1 y2 y3 |
982834e62ceSMatthew G. Knepley    | z0 z1 z2 z3 |
983834e62ceSMatthew G. Knepley 
984834e62ceSMatthew G. Knepley      but if x0,y0,z0 is the origin, we have
985834e62ceSMatthew G. Knepley 
986834e62ceSMatthew G. Knepley    | x1 x2 x3 |
987834e62ceSMatthew G. Knepley    | y1 y2 y3 |
988834e62ceSMatthew G. Knepley    | z1 z2 z3 |
989834e62ceSMatthew G. Knepley   */
990834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[3] - coords[0], y1 = coords[4]  - coords[1], z1 = coords[5]  - coords[2];
991834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[6] - coords[0], y2 = coords[7]  - coords[1], z2 = coords[8]  - coords[2];
992834e62ceSMatthew G. Knepley   const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2];
9930a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1./(PetscReal)6.);
994834e62ceSMatthew G. Knepley   PetscReal       M[9], detM;
995834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2; M[2] = x3;
996834e62ceSMatthew G. Knepley   M[3] = y1; M[4] = y2; M[5] = y3;
997834e62ceSMatthew G. Knepley   M[6] = z1; M[7] = z2; M[8] = z3;
998923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(&detM, M);
9990a3da2c2SToby Isaac   *vol = -onesixth*detM;
10003bc0b13bSBarry Smith   (void)PetscLogFlops(10.0);
1001834e62ceSMatthew G. Knepley }
1002834e62ceSMatthew G. Knepley 
10030ec8681fSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[])
10040ec8681fSMatthew G. Knepley {
10050a3da2c2SToby Isaac   const PetscReal onesixth = ((PetscReal)1./(PetscReal)6.);
1006923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(vol, coords);
10070a3da2c2SToby Isaac   *vol *= -onesixth;
10080ec8681fSMatthew G. Knepley }
10090ec8681fSMatthew G. Knepley 
1010cb92db44SToby Isaac static PetscErrorCode DMPlexComputePointGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1011cb92db44SToby Isaac {
1012cb92db44SToby Isaac   PetscSection   coordSection;
1013cb92db44SToby Isaac   Vec            coordinates;
1014cb92db44SToby Isaac   const PetscScalar *coords;
1015cb92db44SToby Isaac   PetscInt       dim, d, off;
1016cb92db44SToby Isaac   PetscErrorCode ierr;
1017cb92db44SToby Isaac 
1018cb92db44SToby Isaac   PetscFunctionBegin;
1019cb92db44SToby Isaac   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1020cb92db44SToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1021cb92db44SToby Isaac   ierr = PetscSectionGetDof(coordSection,e,&dim);CHKERRQ(ierr);
1022cb92db44SToby Isaac   if (!dim) PetscFunctionReturn(0);
1023cb92db44SToby Isaac   ierr = PetscSectionGetOffset(coordSection,e,&off);CHKERRQ(ierr);
1024cb92db44SToby Isaac   ierr = VecGetArrayRead(coordinates,&coords);CHKERRQ(ierr);
1025cb92db44SToby Isaac   if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[off + d]);}
1026cb92db44SToby Isaac   ierr = VecRestoreArrayRead(coordinates,&coords);CHKERRQ(ierr);
1027cb92db44SToby Isaac   *detJ = 1.;
1028cb92db44SToby Isaac   if (J) {
1029cb92db44SToby Isaac     for (d = 0; d < dim * dim; d++) J[d] = 0.;
1030cb92db44SToby Isaac     for (d = 0; d < dim; d++) J[d * dim + d] = 1.;
1031cb92db44SToby Isaac     if (invJ) {
1032cb92db44SToby Isaac       for (d = 0; d < dim * dim; d++) invJ[d] = 0.;
1033cb92db44SToby Isaac       for (d = 0; d < dim; d++) invJ[d * dim + d] = 1.;
1034cb92db44SToby Isaac     }
1035cb92db44SToby Isaac   }
1036cb92db44SToby Isaac   PetscFunctionReturn(0);
1037cb92db44SToby Isaac }
1038cb92db44SToby Isaac 
103917fe8556SMatthew G. Knepley static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
104017fe8556SMatthew G. Knepley {
104117fe8556SMatthew G. Knepley   PetscSection   coordSection;
104217fe8556SMatthew G. Knepley   Vec            coordinates;
1043a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
10448bf5c034SToby Isaac   PetscInt       numCoords, d, pStart, pEnd, numSelfCoords = 0;
104517fe8556SMatthew G. Knepley   PetscErrorCode ierr;
104617fe8556SMatthew G. Knepley 
104717fe8556SMatthew G. Knepley   PetscFunctionBegin;
104817fe8556SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
104969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
10508bf5c034SToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
10518bf5c034SToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
105217fe8556SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
10538bf5c034SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
1054adac9986SMatthew G. Knepley   if (invJ && !J) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "In order to compute invJ, J must not be NULL");
10557f07f362SMatthew G. Knepley   *detJ = 0.0;
105628dbe442SToby Isaac   if (numCoords == 6) {
105728dbe442SToby Isaac     const PetscInt dim = 3;
105828dbe442SToby Isaac     PetscReal      R[9], J0;
105928dbe442SToby Isaac 
106028dbe442SToby Isaac     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1061741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto1D(coords, R);CHKERRQ(ierr);
106228dbe442SToby Isaac     if (J)    {
106328dbe442SToby Isaac       J0   = 0.5*PetscRealPart(coords[1]);
106428dbe442SToby Isaac       J[0] = R[0]*J0; J[1] = R[1]; J[2] = R[2];
106528dbe442SToby Isaac       J[3] = R[3]*J0; J[4] = R[4]; J[5] = R[5];
106628dbe442SToby Isaac       J[6] = R[6]*J0; J[7] = R[7]; J[8] = R[8];
106728dbe442SToby Isaac       DMPlex_Det3D_Internal(detJ, J);
106828dbe442SToby Isaac       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1069adac9986SMatthew G. Knepley     }
107028dbe442SToby Isaac   } else if (numCoords == 4) {
10717f07f362SMatthew G. Knepley     const PetscInt dim = 2;
10727f07f362SMatthew G. Knepley     PetscReal      R[4], J0;
10737f07f362SMatthew G. Knepley 
10747f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1075741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection2Dto1D(coords, R);CHKERRQ(ierr);
107617fe8556SMatthew G. Knepley     if (J)    {
10777f07f362SMatthew G. Knepley       J0   = 0.5*PetscRealPart(coords[1]);
10787f07f362SMatthew G. Knepley       J[0] = R[0]*J0; J[1] = R[1];
10797f07f362SMatthew G. Knepley       J[2] = R[2]*J0; J[3] = R[3];
1080923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1081923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1082adac9986SMatthew G. Knepley     }
10837f07f362SMatthew G. Knepley   } else if (numCoords == 2) {
10847f07f362SMatthew G. Knepley     const PetscInt dim = 1;
10857f07f362SMatthew G. Knepley 
10867f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
10877f07f362SMatthew G. Knepley     if (J)    {
10887f07f362SMatthew G. Knepley       J[0]  = 0.5*(PetscRealPart(coords[1]) - PetscRealPart(coords[0]));
108917fe8556SMatthew G. Knepley       *detJ = J[0];
10903bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0);CHKERRQ(ierr);
10913bc0b13bSBarry Smith       if (invJ) {invJ[0] = 1.0/J[0]; ierr = PetscLogFlops(1.0);CHKERRQ(ierr);}
1092adac9986SMatthew G. Knepley     }
1093796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this segment is %D != 2", numCoords);
109417fe8556SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
109517fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
109617fe8556SMatthew G. Knepley }
109717fe8556SMatthew G. Knepley 
1098ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1099ccd2543fSMatthew G Knepley {
1100ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1101ccd2543fSMatthew G Knepley   Vec            coordinates;
1102a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
110303d7ed2eSStefano Zampini   PetscInt       numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd;
1104ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1105ccd2543fSMatthew G Knepley 
1106ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1107ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
110869d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
110903d7ed2eSStefano Zampini   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
111003d7ed2eSStefano Zampini   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
1111ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
111203d7ed2eSStefano Zampini   numCoords = numSelfCoords ? numSelfCoords : numCoords;
11137f07f362SMatthew G. Knepley   *detJ = 0.0;
1114ccd2543fSMatthew G Knepley   if (numCoords == 9) {
11157f07f362SMatthew G. Knepley     const PetscInt dim = 3;
11167f07f362SMatthew 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};
11177f07f362SMatthew G. Knepley 
11187f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1119741bfc07SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
11207f07f362SMatthew G. Knepley     if (J)    {
1121b7ad821dSMatthew G. Knepley       const PetscInt pdim = 2;
1122b7ad821dSMatthew G. Knepley 
1123b7ad821dSMatthew G. Knepley       for (d = 0; d < pdim; d++) {
1124b7ad821dSMatthew G. Knepley         for (f = 0; f < pdim; f++) {
1125b7ad821dSMatthew G. Knepley           J0[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
1126ccd2543fSMatthew G Knepley         }
11277f07f362SMatthew G. Knepley       }
11283bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1129923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
11307f07f362SMatthew G. Knepley       for (d = 0; d < dim; d++) {
11317f07f362SMatthew G. Knepley         for (f = 0; f < dim; f++) {
11327f07f362SMatthew G. Knepley           J[d*dim+f] = 0.0;
11337f07f362SMatthew G. Knepley           for (g = 0; g < dim; g++) {
11347f07f362SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
11357f07f362SMatthew G. Knepley           }
11367f07f362SMatthew G. Knepley         }
11377f07f362SMatthew G. Knepley       }
11383bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
11397f07f362SMatthew G. Knepley     }
1140923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
11417f07f362SMatthew G. Knepley   } else if (numCoords == 6) {
11427f07f362SMatthew G. Knepley     const PetscInt dim = 2;
11437f07f362SMatthew G. Knepley 
11447f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1145ccd2543fSMatthew G Knepley     if (J)    {
1146ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1147ccd2543fSMatthew G Knepley         for (f = 0; f < dim; f++) {
1148ccd2543fSMatthew G Knepley           J[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*dim+d]) - PetscRealPart(coords[0*dim+d]));
1149ccd2543fSMatthew G Knepley         }
1150ccd2543fSMatthew G Knepley       }
11513bc0b13bSBarry Smith       ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1152923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
1153ccd2543fSMatthew G Knepley     }
1154923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1155796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %D != 6 or 9", numCoords);
1156ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1157ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1158ccd2543fSMatthew G Knepley }
1159ccd2543fSMatthew G Knepley 
1160dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1161ccd2543fSMatthew G Knepley {
1162ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1163ccd2543fSMatthew G Knepley   Vec            coordinates;
1164a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
11650d29256aSToby Isaac   PetscInt       numCoords, numSelfCoords = 0, d, f, g, pStart, pEnd;
1166ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1167ccd2543fSMatthew G Knepley 
1168ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1169ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
117069d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
11710d29256aSToby Isaac   ierr = PetscSectionGetChart(coordSection,&pStart,&pEnd);CHKERRQ(ierr);
11720d29256aSToby Isaac   if (e >= pStart && e < pEnd) {ierr = PetscSectionGetDof(coordSection,e,&numSelfCoords);CHKERRQ(ierr);}
117399dec3a6SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
117471f58de1SToby Isaac   numCoords = numSelfCoords ? numSelfCoords : numCoords;
1175dfccc68fSToby Isaac   if (!Nq) {
11767f07f362SMatthew G. Knepley     *detJ = 0.0;
117799dec3a6SMatthew G. Knepley     if (numCoords == 12) {
117899dec3a6SMatthew G. Knepley       const PetscInt dim = 3;
117999dec3a6SMatthew 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};
118099dec3a6SMatthew G. Knepley 
1181dfccc68fSToby Isaac       if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1182741bfc07SMatthew G. Knepley       ierr = DMPlexComputeProjection3Dto2D(numCoords, coords, R);CHKERRQ(ierr);
118399dec3a6SMatthew G. Knepley       if (J)    {
118499dec3a6SMatthew G. Knepley         const PetscInt pdim = 2;
118599dec3a6SMatthew G. Knepley 
118699dec3a6SMatthew G. Knepley         for (d = 0; d < pdim; d++) {
118799dec3a6SMatthew G. Knepley           J0[d*dim+0] = 0.5*(PetscRealPart(coords[1*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
118899dec3a6SMatthew G. Knepley           J0[d*dim+1] = 0.5*(PetscRealPart(coords[3*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
118999dec3a6SMatthew G. Knepley         }
11903bc0b13bSBarry Smith         ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1191923591dfSMatthew G. Knepley         DMPlex_Det3D_Internal(detJ, J0);
119299dec3a6SMatthew G. Knepley         for (d = 0; d < dim; d++) {
119399dec3a6SMatthew G. Knepley           for (f = 0; f < dim; f++) {
119499dec3a6SMatthew G. Knepley             J[d*dim+f] = 0.0;
119599dec3a6SMatthew G. Knepley             for (g = 0; g < dim; g++) {
119699dec3a6SMatthew G. Knepley               J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
119799dec3a6SMatthew G. Knepley             }
119899dec3a6SMatthew G. Knepley           }
119999dec3a6SMatthew G. Knepley         }
12003bc0b13bSBarry Smith         ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
120199dec3a6SMatthew G. Knepley       }
1202923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
120371f58de1SToby Isaac     } else if (numCoords == 8) {
120499dec3a6SMatthew G. Knepley       const PetscInt dim = 2;
120599dec3a6SMatthew G. Knepley 
1206dfccc68fSToby Isaac       if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1207ccd2543fSMatthew G Knepley       if (J)    {
1208ccd2543fSMatthew G Knepley         for (d = 0; d < dim; d++) {
120999dec3a6SMatthew G. Knepley           J[d*dim+0] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
121099dec3a6SMatthew G. Knepley           J[d*dim+1] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1211ccd2543fSMatthew G Knepley         }
12123bc0b13bSBarry Smith         ierr = PetscLogFlops(8.0);CHKERRQ(ierr);
1213923591dfSMatthew G. Knepley         DMPlex_Det2D_Internal(detJ, J);
1214ccd2543fSMatthew G Knepley       }
1215923591dfSMatthew G. Knepley       if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
1216796f034aSJed Brown     } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
1217dfccc68fSToby Isaac   } else {
1218dfccc68fSToby Isaac     const PetscInt Nv = 4;
1219dfccc68fSToby Isaac     const PetscInt dimR = 2;
1220dfccc68fSToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
1221dfccc68fSToby Isaac     PetscReal zOrder[12];
1222dfccc68fSToby Isaac     PetscReal zCoeff[12];
1223dfccc68fSToby Isaac     PetscInt  i, j, k, l, dim;
1224dfccc68fSToby Isaac 
1225dfccc68fSToby Isaac     if (numCoords == 12) {
1226dfccc68fSToby Isaac       dim = 3;
1227dfccc68fSToby Isaac     } else if (numCoords == 8) {
1228dfccc68fSToby Isaac       dim = 2;
1229dfccc68fSToby Isaac     } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
1230dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1231dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1232dfccc68fSToby Isaac 
1233dfccc68fSToby Isaac       for (j = 0; j < dim; j++) {
1234dfccc68fSToby Isaac         zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1235dfccc68fSToby Isaac       }
1236dfccc68fSToby Isaac     }
1237dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1238dfccc68fSToby Isaac       zCoeff[dim * 0 + j] = 0.25 * (  zOrder[dim * 0 + j] + zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1239dfccc68fSToby Isaac       zCoeff[dim * 1 + j] = 0.25 * (- zOrder[dim * 0 + j] + zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1240dfccc68fSToby Isaac       zCoeff[dim * 2 + j] = 0.25 * (- zOrder[dim * 0 + j] - zOrder[dim * 1 + j] + zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1241dfccc68fSToby Isaac       zCoeff[dim * 3 + j] = 0.25 * (  zOrder[dim * 0 + j] - zOrder[dim * 1 + j] - zOrder[dim * 2 + j] + zOrder[dim * 3 + j]);
1242dfccc68fSToby Isaac     }
1243dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1244dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1];
1245dfccc68fSToby Isaac 
1246dfccc68fSToby Isaac       if (v) {
1247dfccc68fSToby Isaac         PetscReal extPoint[4];
1248dfccc68fSToby Isaac 
1249dfccc68fSToby Isaac         extPoint[0] = 1.;
1250dfccc68fSToby Isaac         extPoint[1] = xi;
1251dfccc68fSToby Isaac         extPoint[2] = eta;
1252dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1253dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1254dfccc68fSToby Isaac           PetscReal val = 0.;
1255dfccc68fSToby Isaac 
1256dfccc68fSToby Isaac           for (k = 0; k < Nv; k++) {
1257dfccc68fSToby Isaac             val += extPoint[k] * zCoeff[dim * k + j];
1258dfccc68fSToby Isaac           }
1259dfccc68fSToby Isaac           v[i * dim + j] = val;
1260dfccc68fSToby Isaac         }
1261dfccc68fSToby Isaac       }
1262dfccc68fSToby Isaac       if (J) {
1263dfccc68fSToby Isaac         PetscReal extJ[8];
1264dfccc68fSToby Isaac 
1265dfccc68fSToby Isaac         extJ[0] = 0.;
1266dfccc68fSToby Isaac         extJ[1] = 0.;
1267dfccc68fSToby Isaac         extJ[2] = 1.;
1268dfccc68fSToby Isaac         extJ[3] = 0.;
1269dfccc68fSToby Isaac         extJ[4] = 0.;
1270dfccc68fSToby Isaac         extJ[5] = 1.;
1271dfccc68fSToby Isaac         extJ[6] = eta;
1272dfccc68fSToby Isaac         extJ[7] = xi;
1273dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1274dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1275dfccc68fSToby Isaac             PetscReal val = 0.;
1276dfccc68fSToby Isaac 
1277dfccc68fSToby Isaac             for (l = 0; l < Nv; l++) {
1278dfccc68fSToby Isaac               val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1279dfccc68fSToby Isaac             }
1280dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1281dfccc68fSToby Isaac           }
1282dfccc68fSToby Isaac         }
1283dfccc68fSToby Isaac         if (dim == 3) { /* put the cross product in the third component of the Jacobian */
1284dfccc68fSToby Isaac           PetscReal x, y, z;
1285dfccc68fSToby Isaac           PetscReal *iJ = &J[i * dim * dim];
1286dfccc68fSToby Isaac           PetscReal norm;
1287dfccc68fSToby Isaac 
1288dfccc68fSToby Isaac           x = iJ[1 * dim + 0] * iJ[2 * dim + 1] - iJ[1 * dim + 1] * iJ[2 * dim + 0];
1289dfccc68fSToby Isaac           y = iJ[0 * dim + 1] * iJ[2 * dim + 0] - iJ[0 * dim + 0] * iJ[2 * dim + 1];
1290dfccc68fSToby Isaac           z = iJ[0 * dim + 0] * iJ[1 * dim + 1] - iJ[0 * dim + 1] * iJ[1 * dim + 0];
1291dfccc68fSToby Isaac           norm = PetscSqrtReal(x * x + y * y + z * z);
1292dfccc68fSToby Isaac           iJ[2] = x / norm;
1293dfccc68fSToby Isaac           iJ[5] = y / norm;
1294dfccc68fSToby Isaac           iJ[8] = z / norm;
1295dfccc68fSToby Isaac           DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1296dfccc68fSToby Isaac           if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1297dfccc68fSToby Isaac         } else {
1298dfccc68fSToby Isaac           DMPlex_Det2D_Internal(&detJ[i], &J[i * dim * dim]);
1299dfccc68fSToby Isaac           if (invJ) {DMPlex_Invert2D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1300dfccc68fSToby Isaac         }
1301dfccc68fSToby Isaac       }
1302dfccc68fSToby Isaac     }
1303dfccc68fSToby Isaac   }
130499dec3a6SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
1305ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1306ccd2543fSMatthew G Knepley }
1307ccd2543fSMatthew G Knepley 
1308ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1309ccd2543fSMatthew G Knepley {
1310ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1311ccd2543fSMatthew G Knepley   Vec            coordinates;
1312a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1313ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
131499dec3a6SMatthew G. Knepley   PetscInt       d;
1315ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1316ccd2543fSMatthew G Knepley 
1317ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1318ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
131969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1320ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
13217f07f362SMatthew G. Knepley   *detJ = 0.0;
13227f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
1323ccd2543fSMatthew G Knepley   if (J)    {
1324ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
1325f0df753eSMatthew G. Knepley       /* I orient with outward face normals */
1326f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d]));
1327f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1328f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1329ccd2543fSMatthew G Knepley     }
13303bc0b13bSBarry Smith     ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1331923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
1332ccd2543fSMatthew G Knepley   }
1333923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1334ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1335ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1336ccd2543fSMatthew G Knepley }
1337ccd2543fSMatthew G Knepley 
1338dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscInt Nq, const PetscReal points[], PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
1339ccd2543fSMatthew G Knepley {
1340ccd2543fSMatthew G Knepley   PetscSection   coordSection;
1341ccd2543fSMatthew G Knepley   Vec            coordinates;
1342a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
1343ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
1344ccd2543fSMatthew G Knepley   PetscInt       d;
1345ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1346ccd2543fSMatthew G Knepley 
1347ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1348ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
134969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1350ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1351dfccc68fSToby Isaac   if (!Nq) {
13527f07f362SMatthew G. Knepley     *detJ = 0.0;
1353dfccc68fSToby Isaac     if (v)   {for (d = 0; d < dim; d++) v[d] = PetscRealPart(coords[d]);}
1354ccd2543fSMatthew G Knepley     if (J)    {
1355ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
1356f0df753eSMatthew G. Knepley         J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
1357f0df753eSMatthew G. Knepley         J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
1358f0df753eSMatthew G. Knepley         J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d]));
1359ccd2543fSMatthew G Knepley       }
13603bc0b13bSBarry Smith       ierr = PetscLogFlops(18.0);CHKERRQ(ierr);
1361923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J);
1362ccd2543fSMatthew G Knepley     }
1363923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
1364dfccc68fSToby Isaac   } else {
1365dfccc68fSToby Isaac     const PetscInt Nv = 8;
1366dfccc68fSToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
1367dfccc68fSToby Isaac     const PetscInt dim = 3;
1368dfccc68fSToby Isaac     const PetscInt dimR = 3;
1369dfccc68fSToby Isaac     PetscReal zOrder[24];
1370dfccc68fSToby Isaac     PetscReal zCoeff[24];
1371dfccc68fSToby Isaac     PetscInt  i, j, k, l;
1372dfccc68fSToby Isaac 
1373dfccc68fSToby Isaac     for (i = 0; i < Nv; i++) {
1374dfccc68fSToby Isaac       PetscInt zi = zToPlex[i];
1375dfccc68fSToby Isaac 
1376dfccc68fSToby Isaac       for (j = 0; j < dim; j++) {
1377dfccc68fSToby Isaac         zOrder[dim * i + j] = PetscRealPart(coords[dim * zi + j]);
1378dfccc68fSToby Isaac       }
1379dfccc68fSToby Isaac     }
1380dfccc68fSToby Isaac     for (j = 0; j < dim; j++) {
1381dfccc68fSToby 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]);
1382dfccc68fSToby 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]);
1383dfccc68fSToby 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]);
1384dfccc68fSToby 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]);
1385dfccc68fSToby 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]);
1386dfccc68fSToby 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]);
1387dfccc68fSToby 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]);
1388dfccc68fSToby 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]);
1389dfccc68fSToby Isaac     }
1390dfccc68fSToby Isaac     for (i = 0; i < Nq; i++) {
1391dfccc68fSToby Isaac       PetscReal xi = points[dimR * i], eta = points[dimR * i + 1], theta = points[dimR * i + 2];
1392dfccc68fSToby Isaac 
1393dfccc68fSToby Isaac       if (v) {
139491d2b7ceSToby Isaac         PetscReal extPoint[8];
1395dfccc68fSToby Isaac 
1396dfccc68fSToby Isaac         extPoint[0] = 1.;
1397dfccc68fSToby Isaac         extPoint[1] = xi;
1398dfccc68fSToby Isaac         extPoint[2] = eta;
1399dfccc68fSToby Isaac         extPoint[3] = xi * eta;
1400dfccc68fSToby Isaac         extPoint[4] = theta;
1401dfccc68fSToby Isaac         extPoint[5] = theta * xi;
1402dfccc68fSToby Isaac         extPoint[6] = theta * eta;
1403dfccc68fSToby Isaac         extPoint[7] = theta * eta * xi;
1404dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1405dfccc68fSToby Isaac           PetscReal val = 0.;
1406dfccc68fSToby Isaac 
1407dfccc68fSToby Isaac           for (k = 0; k < Nv; k++) {
1408dfccc68fSToby Isaac             val += extPoint[k] * zCoeff[dim * k + j];
1409dfccc68fSToby Isaac           }
1410dfccc68fSToby Isaac           v[i * dim + j] = val;
1411dfccc68fSToby Isaac         }
1412dfccc68fSToby Isaac       }
1413dfccc68fSToby Isaac       if (J) {
1414dfccc68fSToby Isaac         PetscReal extJ[24];
1415dfccc68fSToby Isaac 
1416dfccc68fSToby Isaac         extJ[0]  = 0.         ; extJ[1]  = 0.        ; extJ[2]  = 0.      ;
1417dfccc68fSToby Isaac         extJ[3]  = 1.         ; extJ[4]  = 0.        ; extJ[5]  = 0.      ;
1418dfccc68fSToby Isaac         extJ[6]  = 0.         ; extJ[7]  = 1.        ; extJ[8]  = 0.      ;
1419dfccc68fSToby Isaac         extJ[9]  = eta        ; extJ[10] = xi        ; extJ[11] = 0.      ;
1420dfccc68fSToby Isaac         extJ[12] = 0.         ; extJ[13] = 0.        ; extJ[14] = 1.      ;
1421dfccc68fSToby Isaac         extJ[15] = theta      ; extJ[16] = 0.        ; extJ[17] = xi      ;
1422dfccc68fSToby Isaac         extJ[18] = 0.         ; extJ[19] = theta     ; extJ[20] = eta     ;
1423dfccc68fSToby Isaac         extJ[21] = theta * eta; extJ[22] = theta * xi; extJ[23] = eta * xi;
1424dfccc68fSToby Isaac 
1425dfccc68fSToby Isaac         for (j = 0; j < dim; j++) {
1426dfccc68fSToby Isaac           for (k = 0; k < dimR; k++) {
1427dfccc68fSToby Isaac             PetscReal val = 0.;
1428dfccc68fSToby Isaac 
1429dfccc68fSToby Isaac             for (l = 0; l < Nv; l++) {
1430dfccc68fSToby Isaac               val += zCoeff[dim * l + j] * extJ[dimR * l + k];
1431dfccc68fSToby Isaac             }
1432dfccc68fSToby Isaac             J[i * dim * dim + dim * j + k] = val;
1433dfccc68fSToby Isaac           }
1434dfccc68fSToby Isaac         }
1435dfccc68fSToby Isaac         DMPlex_Det3D_Internal(&detJ[i], &J[i * dim * dim]);
1436dfccc68fSToby Isaac         if (invJ) {DMPlex_Invert3D_Internal(&invJ[i * dim * dim], &J[i * dim * dim], detJ[i]);}
1437dfccc68fSToby Isaac       }
1438dfccc68fSToby Isaac     }
1439dfccc68fSToby Isaac   }
1440ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
1441ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1442ccd2543fSMatthew G Knepley }
1443ccd2543fSMatthew G Knepley 
1444dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeCellGeometryFEM_Implicit(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1445dfccc68fSToby Isaac {
1446dfccc68fSToby Isaac   PetscInt        depth, dim, coordDim, coneSize, i;
1447dfccc68fSToby Isaac   PetscInt        Nq = 0;
1448dfccc68fSToby Isaac   const PetscReal *points = NULL;
1449dfccc68fSToby Isaac   DMLabel         depthLabel;
14507318780aSToby Isaac   PetscReal       v0[3] = {-1.}, J0[9] = {-1.}, detJ0 = -1.;
1451dfccc68fSToby Isaac   PetscBool       isAffine = PETSC_TRUE;
1452dfccc68fSToby Isaac   PetscErrorCode  ierr;
1453dfccc68fSToby Isaac 
1454dfccc68fSToby Isaac   PetscFunctionBegin;
1455dfccc68fSToby Isaac   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1456dfccc68fSToby Isaac   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
1457dfccc68fSToby Isaac   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1458dfccc68fSToby Isaac   ierr = DMLabelGetValue(depthLabel, cell, &dim);CHKERRQ(ierr);
1459dfccc68fSToby Isaac   if (depth == 1 && dim == 1) {
1460dfccc68fSToby Isaac     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1461dfccc68fSToby Isaac   }
1462dfccc68fSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
1463dfccc68fSToby Isaac   if (coordDim > 3) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported coordinate dimension %D > 3", coordDim);
14649c3cf19fSMatthew G. Knepley   if (quad) {ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, &points, NULL);CHKERRQ(ierr);}
1465dfccc68fSToby Isaac   switch (dim) {
1466dfccc68fSToby Isaac   case 0:
1467dfccc68fSToby Isaac     ierr = DMPlexComputePointGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
1468dfccc68fSToby Isaac     isAffine = PETSC_FALSE;
1469dfccc68fSToby Isaac     break;
1470dfccc68fSToby Isaac   case 1:
14717318780aSToby Isaac     if (Nq) {
14727318780aSToby Isaac       ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);
14737318780aSToby Isaac     } else {
14747318780aSToby Isaac       ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
14757318780aSToby Isaac     }
1476dfccc68fSToby Isaac     break;
1477dfccc68fSToby Isaac   case 2:
1478dfccc68fSToby Isaac     switch (coneSize) {
1479dfccc68fSToby Isaac     case 3:
14807318780aSToby Isaac       if (Nq) {
14817318780aSToby Isaac         ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);
14827318780aSToby Isaac       } else {
14837318780aSToby Isaac         ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
14847318780aSToby Isaac       }
1485dfccc68fSToby Isaac       break;
1486dfccc68fSToby Isaac     case 4:
1487dfccc68fSToby Isaac       ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1488dfccc68fSToby Isaac       isAffine = PETSC_FALSE;
1489dfccc68fSToby Isaac       break;
1490dfccc68fSToby Isaac     default:
1491dfccc68fSToby Isaac       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
1492dfccc68fSToby Isaac     }
1493dfccc68fSToby Isaac     break;
1494dfccc68fSToby Isaac   case 3:
1495dfccc68fSToby Isaac     switch (coneSize) {
1496dfccc68fSToby Isaac     case 4:
14977318780aSToby Isaac       if (Nq) {
14987318780aSToby Isaac         ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J0, NULL, &detJ0);CHKERRQ(ierr);
14997318780aSToby Isaac       } else {
15007318780aSToby Isaac         ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v, J, invJ, detJ);CHKERRQ(ierr);
15017318780aSToby Isaac       }
1502dfccc68fSToby Isaac       break;
1503dfccc68fSToby Isaac     case 6: /* Faces */
1504dfccc68fSToby Isaac     case 8: /* Vertices */
1505dfccc68fSToby Isaac       ierr = DMPlexComputeHexahedronGeometry_Internal(dm, cell, Nq, points, v, J, invJ, detJ);CHKERRQ(ierr);
1506dfccc68fSToby Isaac       isAffine = PETSC_FALSE;
1507dfccc68fSToby Isaac       break;
1508dfccc68fSToby Isaac     default:
1509dfccc68fSToby Isaac       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
1510dfccc68fSToby Isaac     }
1511dfccc68fSToby Isaac     break;
1512dfccc68fSToby Isaac   default:
1513dfccc68fSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
1514dfccc68fSToby Isaac   }
15157318780aSToby Isaac   if (isAffine && Nq) {
1516dfccc68fSToby Isaac     if (v) {
1517dfccc68fSToby Isaac       for (i = 0; i < Nq; i++) {
15187318780aSToby Isaac         CoordinatesRefToReal(coordDim,dim,v0, J0, &points[dim * i], &v[coordDim * i]);
1519dfccc68fSToby Isaac       }
1520dfccc68fSToby Isaac     }
15217318780aSToby Isaac     if (detJ) {
15227318780aSToby Isaac       for (i = 0; i < Nq; i++) {
15237318780aSToby Isaac         detJ[i] = detJ0;
1524dfccc68fSToby Isaac       }
15257318780aSToby Isaac     }
15267318780aSToby Isaac     if (J) {
15277318780aSToby Isaac       PetscInt k;
15287318780aSToby Isaac 
15297318780aSToby Isaac       for (i = 0, k = 0; i < Nq; i++) {
1530dfccc68fSToby Isaac         PetscInt j;
1531dfccc68fSToby Isaac 
15327318780aSToby Isaac         for (j = 0; j < coordDim * coordDim; j++, k++) {
15337318780aSToby Isaac           J[k] = J0[j];
15347318780aSToby Isaac         }
15357318780aSToby Isaac       }
15367318780aSToby Isaac     }
15377318780aSToby Isaac     if (invJ) {
15387318780aSToby Isaac       PetscInt k;
15397318780aSToby Isaac       switch (coordDim) {
15407318780aSToby Isaac       case 0:
15417318780aSToby Isaac         break;
15427318780aSToby Isaac       case 1:
15437318780aSToby Isaac         invJ[0] = 1./J0[0];
15447318780aSToby Isaac         break;
15457318780aSToby Isaac       case 2:
15467318780aSToby Isaac         DMPlex_Invert2D_Internal(invJ, J0, detJ0);
15477318780aSToby Isaac         break;
15487318780aSToby Isaac       case 3:
15497318780aSToby Isaac         DMPlex_Invert3D_Internal(invJ, J0, detJ0);
15507318780aSToby Isaac         break;
15517318780aSToby Isaac       }
15527318780aSToby Isaac       for (i = 1, k = coordDim * coordDim; i < Nq; i++) {
15537318780aSToby Isaac         PetscInt j;
15547318780aSToby Isaac 
15557318780aSToby Isaac         for (j = 0; j < coordDim * coordDim; j++, k++) {
15567318780aSToby Isaac           invJ[k] = invJ[j];
15577318780aSToby Isaac         }
1558dfccc68fSToby Isaac       }
1559dfccc68fSToby Isaac     }
1560dfccc68fSToby Isaac   }
1561dfccc68fSToby Isaac   PetscFunctionReturn(0);
1562dfccc68fSToby Isaac }
1563dfccc68fSToby Isaac 
1564ccd2543fSMatthew G Knepley /*@C
15658e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell
1566ccd2543fSMatthew G Knepley 
1567ccd2543fSMatthew G Knepley   Collective on DM
1568ccd2543fSMatthew G Knepley 
1569ccd2543fSMatthew G Knepley   Input Arguments:
1570ccd2543fSMatthew G Knepley + dm   - the DM
1571ccd2543fSMatthew G Knepley - cell - the cell
1572ccd2543fSMatthew G Knepley 
1573ccd2543fSMatthew G Knepley   Output Arguments:
1574ccd2543fSMatthew G Knepley + v0   - the translation part of this affine transform
1575ccd2543fSMatthew G Knepley . J    - the Jacobian of the transform from the reference element
1576ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian
1577ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant
1578ccd2543fSMatthew G Knepley 
1579ccd2543fSMatthew G Knepley   Level: advanced
1580ccd2543fSMatthew G Knepley 
1581ccd2543fSMatthew G Knepley   Fortran Notes:
1582ccd2543fSMatthew G Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1583ccd2543fSMatthew G Knepley   include petsc.h90 in your code.
1584ccd2543fSMatthew G Knepley 
1585*e8964c0aSStefano Zampini .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinates()
1586ccd2543fSMatthew G Knepley @*/
15878e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
1588ccd2543fSMatthew G Knepley {
1589ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
1590ccd2543fSMatthew G Knepley 
1591ccd2543fSMatthew G Knepley   PetscFunctionBegin;
1592dfccc68fSToby Isaac   ierr = DMPlexComputeCellGeometryFEM_Implicit(dm,cell,NULL,v0,J,invJ,detJ);CHKERRQ(ierr);
15938e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
15948e0841e0SMatthew G. Knepley }
15958e0841e0SMatthew G. Knepley 
1596dfccc68fSToby Isaac static PetscErrorCode DMPlexComputeCellGeometryFEM_FE(DM dm, PetscFE fe, PetscInt point, PetscQuadrature quad, PetscReal v[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
15978e0841e0SMatthew G. Knepley {
1598dfccc68fSToby Isaac   PetscQuadrature  feQuad;
15998e0841e0SMatthew G. Knepley   PetscSection     coordSection;
16008e0841e0SMatthew G. Knepley   Vec              coordinates;
16018e0841e0SMatthew G. Knepley   PetscScalar     *coords = NULL;
16028e0841e0SMatthew G. Knepley   const PetscReal *quadPoints;
1603f960e424SToby Isaac   PetscReal       *basisDer, *basis, detJt;
1604f960e424SToby Isaac   PetscInt         dim, cdim, pdim, qdim, Nq, numCoords, q;
16058e0841e0SMatthew G. Knepley   PetscErrorCode   ierr;
16068e0841e0SMatthew G. Knepley 
16078e0841e0SMatthew G. Knepley   PetscFunctionBegin;
16088e0841e0SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
16098e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
16108e0841e0SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
16118e0841e0SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
16128e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
1613dfccc68fSToby Isaac   if (!quad) { /* use the first point of the first functional of the dual space */
1614dfccc68fSToby Isaac     PetscDualSpace dsp;
1615dfccc68fSToby Isaac 
1616dfccc68fSToby Isaac     ierr = PetscFEGetDualSpace(fe, &dsp);CHKERRQ(ierr);
1617dfccc68fSToby Isaac     ierr = PetscDualSpaceGetFunctional(dsp, 0, &quad);CHKERRQ(ierr);
16189c3cf19fSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
1619dfccc68fSToby Isaac     Nq = 1;
1620dfccc68fSToby Isaac   } else {
16219c3cf19fSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
1622dfccc68fSToby Isaac   }
162391d2b7ceSToby Isaac   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
1624dfccc68fSToby Isaac   ierr = PetscFEGetQuadrature(fe, &feQuad);CHKERRQ(ierr);
1625dfccc68fSToby Isaac   if (feQuad == quad) {
16268135c375SStefano Zampini     ierr = PetscFEGetDefaultTabulation(fe, &basis, J ? &basisDer : NULL, NULL);CHKERRQ(ierr);
16278e0841e0SMatthew 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);
1628dfccc68fSToby Isaac   } else {
16298135c375SStefano Zampini     ierr = PetscFEGetTabulation(fe, Nq, quadPoints, &basis, J ? &basisDer : NULL, NULL);CHKERRQ(ierr);
1630dfccc68fSToby Isaac   }
1631dfccc68fSToby Isaac   if (qdim != dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim);
1632dfccc68fSToby Isaac   if (v) {
1633dfccc68fSToby Isaac     ierr = PetscMemzero(v, Nq*cdim*sizeof(PetscReal));CHKERRQ(ierr);
1634f960e424SToby Isaac     for (q = 0; q < Nq; ++q) {
1635f960e424SToby Isaac       PetscInt i, k;
1636f960e424SToby Isaac 
1637f960e424SToby Isaac       for (k = 0; k < pdim; ++k)
1638f960e424SToby Isaac         for (i = 0; i < cdim; ++i)
1639dfccc68fSToby Isaac           v[q*cdim + i] += basis[q*pdim + k] * PetscRealPart(coords[k*cdim + i]);
1640f960e424SToby Isaac       ierr = PetscLogFlops(2.0*pdim*cdim);CHKERRQ(ierr);
1641f960e424SToby Isaac     }
1642f960e424SToby Isaac   }
16438e0841e0SMatthew G. Knepley   if (J) {
1644dfccc68fSToby Isaac     ierr = PetscMemzero(J, Nq*cdim*cdim*sizeof(PetscReal));CHKERRQ(ierr);
16458e0841e0SMatthew G. Knepley     for (q = 0; q < Nq; ++q) {
16468e0841e0SMatthew G. Knepley       PetscInt i, j, k, c, r;
16478e0841e0SMatthew G. Knepley 
16488e0841e0SMatthew G. Knepley       /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */
16498e0841e0SMatthew G. Knepley       for (k = 0; k < pdim; ++k)
16508e0841e0SMatthew G. Knepley         for (j = 0; j < dim; ++j)
16518e0841e0SMatthew G. Knepley           for (i = 0; i < cdim; ++i)
1652dfccc68fSToby Isaac             J[(q*cdim + i)*cdim + j] += basisDer[(q*pdim + k)*dim + j] * PetscRealPart(coords[k*cdim + i]);
16533bc0b13bSBarry Smith       ierr = PetscLogFlops(2.0*pdim*dim*cdim);CHKERRQ(ierr);
16548e0841e0SMatthew G. Knepley       if (cdim > dim) {
16558e0841e0SMatthew G. Knepley         for (c = dim; c < cdim; ++c)
16568e0841e0SMatthew G. Knepley           for (r = 0; r < cdim; ++r)
16578e0841e0SMatthew G. Knepley             J[r*cdim+c] = r == c ? 1.0 : 0.0;
16588e0841e0SMatthew G. Knepley       }
1659f960e424SToby Isaac       if (!detJ && !invJ) continue;
1660a63b72c6SToby Isaac       detJt = 0.;
16618e0841e0SMatthew G. Knepley       switch (cdim) {
16628e0841e0SMatthew G. Knepley       case 3:
1663037dc194SToby Isaac         DMPlex_Det3D_Internal(&detJt, &J[q*cdim*dim]);
1664037dc194SToby Isaac         if (invJ) {DMPlex_Invert3D_Internal(&invJ[q*cdim*dim], &J[q*cdim*dim], detJt);}
166517fe8556SMatthew G. Knepley         break;
166649dc4407SMatthew G. Knepley       case 2:
16679f328543SToby Isaac         DMPlex_Det2D_Internal(&detJt, &J[q*cdim*dim]);
1668037dc194SToby Isaac         if (invJ) {DMPlex_Invert2D_Internal(&invJ[q*cdim*dim], &J[q*cdim*dim], detJt);}
166949dc4407SMatthew G. Knepley         break;
16708e0841e0SMatthew G. Knepley       case 1:
1671037dc194SToby Isaac         detJt = J[q*cdim*dim];
1672037dc194SToby Isaac         if (invJ) invJ[q*cdim*dim] = 1.0/detJt;
167349dc4407SMatthew G. Knepley       }
1674f960e424SToby Isaac       if (detJ) detJ[q] = detJt;
167549dc4407SMatthew G. Knepley     }
167649dc4407SMatthew G. Knepley   }
1677037dc194SToby Isaac   else if (detJ || invJ) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Need J to compute invJ or detJ");
1678dfccc68fSToby Isaac   if (feQuad != quad) {
16798135c375SStefano Zampini     ierr = PetscFERestoreTabulation(fe, Nq, quadPoints, &basis, J ? &basisDer : NULL, NULL);CHKERRQ(ierr);
1680dfccc68fSToby Isaac   }
16818e0841e0SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
16828e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
16838e0841e0SMatthew G. Knepley }
16848e0841e0SMatthew G. Knepley 
16858e0841e0SMatthew G. Knepley /*@C
16868e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell
16878e0841e0SMatthew G. Knepley 
16888e0841e0SMatthew G. Knepley   Collective on DM
16898e0841e0SMatthew G. Knepley 
16908e0841e0SMatthew G. Knepley   Input Arguments:
16918e0841e0SMatthew G. Knepley + dm   - the DM
16928e0841e0SMatthew G. Knepley . cell - the cell
1693dfccc68fSToby Isaac - quad - the quadrature containing the points in the reference element where the geometry will be evaluated.  If quad == NULL, geometry will be
1694dfccc68fSToby Isaac          evaluated at the first vertex of the reference element
16958e0841e0SMatthew G. Knepley 
16968e0841e0SMatthew G. Knepley   Output Arguments:
1697dfccc68fSToby Isaac + v    - the image of the transformed quadrature points, otherwise the image of the first vertex in the closure of the reference element
16988e0841e0SMatthew G. Knepley . J    - the Jacobian of the transform from the reference element at each quadrature point
16998e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point
17008e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point
17018e0841e0SMatthew G. Knepley 
17028e0841e0SMatthew G. Knepley   Level: advanced
17038e0841e0SMatthew G. Knepley 
17048e0841e0SMatthew G. Knepley   Fortran Notes:
17058e0841e0SMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
17068e0841e0SMatthew G. Knepley   include petsc.h90 in your code.
17078e0841e0SMatthew G. Knepley 
1708*e8964c0aSStefano Zampini .seealso: DMGetCoordinateSection(), DMGetCoordinates()
17098e0841e0SMatthew G. Knepley @*/
1710dfccc68fSToby Isaac PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal *v, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
17118e0841e0SMatthew G. Knepley {
1712dfccc68fSToby Isaac   PetscFE        fe = NULL;
17138e0841e0SMatthew G. Knepley   PetscErrorCode ierr;
17148e0841e0SMatthew G. Knepley 
17158e0841e0SMatthew G. Knepley   PetscFunctionBegin;
1716dfccc68fSToby Isaac   if (dm->coordinateDM) {
1717dfccc68fSToby Isaac     PetscClassId id;
1718dfccc68fSToby Isaac     PetscInt     numFields;
1719dfccc68fSToby Isaac     PetscDS      prob = dm->coordinateDM->prob;
1720dfccc68fSToby Isaac     PetscObject  disc;
1721dfccc68fSToby Isaac 
1722dfccc68fSToby Isaac     ierr = PetscDSGetNumFields(prob, &numFields);CHKERRQ(ierr);
1723dfccc68fSToby Isaac     if (numFields) {
1724dfccc68fSToby Isaac       ierr = PetscDSGetDiscretization(prob,0,&disc);CHKERRQ(ierr);
1725dfccc68fSToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
1726dfccc68fSToby Isaac       if (id == PETSCFE_CLASSID) {
1727dfccc68fSToby Isaac         fe = (PetscFE) disc;
1728dfccc68fSToby Isaac       }
1729dfccc68fSToby Isaac     }
1730dfccc68fSToby Isaac   }
1731dfccc68fSToby Isaac   if (!fe) {ierr = DMPlexComputeCellGeometryFEM_Implicit(dm, cell, quad, v, J, invJ, detJ);CHKERRQ(ierr);}
1732dfccc68fSToby Isaac   else     {ierr = DMPlexComputeCellGeometryFEM_FE(dm, fe, cell, quad, v, J, invJ, detJ);CHKERRQ(ierr);}
1733ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1734ccd2543fSMatthew G Knepley }
1735834e62ceSMatthew G. Knepley 
1736011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1737cc08537eSMatthew G. Knepley {
1738cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1739cc08537eSMatthew G. Knepley   Vec            coordinates;
1740a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
174106e2781eSMatthew G. Knepley   PetscScalar    tmp[2];
1742cc08537eSMatthew G. Knepley   PetscInt       coordSize;
1743cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1744cc08537eSMatthew G. Knepley 
1745cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1746cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
174769d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1748cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1749011ea5d8SMatthew G. Knepley   if (dim != 2) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "We only support 2D edges right now");
17502e17dfb7SMatthew G. Knepley   ierr = DMLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp);CHKERRQ(ierr);
1751cc08537eSMatthew G. Knepley   if (centroid) {
175206e2781eSMatthew G. Knepley     centroid[0] = 0.5*PetscRealPart(coords[0] + tmp[0]);
175306e2781eSMatthew G. Knepley     centroid[1] = 0.5*PetscRealPart(coords[1] + tmp[1]);
1754cc08537eSMatthew G. Knepley   }
1755cc08537eSMatthew G. Knepley   if (normal) {
1756a60a936bSMatthew G. Knepley     PetscReal norm;
1757a60a936bSMatthew G. Knepley 
175806e2781eSMatthew G. Knepley     normal[0]  = -PetscRealPart(coords[1] - tmp[1]);
175906e2781eSMatthew G. Knepley     normal[1]  =  PetscRealPart(coords[0] - tmp[0]);
1760a60a936bSMatthew G. Knepley     norm       = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1]);
1761a60a936bSMatthew G. Knepley     normal[0] /= norm;
1762a60a936bSMatthew G. Knepley     normal[1] /= norm;
1763cc08537eSMatthew G. Knepley   }
1764cc08537eSMatthew G. Knepley   if (vol) {
176506e2781eSMatthew G. Knepley     *vol = PetscSqrtReal(PetscSqr(PetscRealPart(coords[0] - tmp[0])) + PetscSqr(PetscRealPart(coords[1] - tmp[1])));
1766cc08537eSMatthew G. Knepley   }
1767cc08537eSMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1768cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1769cc08537eSMatthew G. Knepley }
1770cc08537eSMatthew G. Knepley 
1771cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i ) / A */
1772011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1773cc08537eSMatthew G. Knepley {
1774cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1775cc08537eSMatthew G. Knepley   Vec            coordinates;
1776cc08537eSMatthew G. Knepley   PetscScalar   *coords = NULL;
17770a1d6728SMatthew G. Knepley   PetscReal      vsum = 0.0, csum[3] = {0.0, 0.0, 0.0}, vtmp, ctmp[4], v0[3], R[9];
17780a1d6728SMatthew G. Knepley   PetscInt       tdim = 2, coordSize, numCorners, p, d, e;
1779cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1780cc08537eSMatthew G. Knepley 
1781cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1782cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
17830a1d6728SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numCorners);CHKERRQ(ierr);
178469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1785cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
17860bce18caSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1787ceee4971SMatthew G. Knepley   if (dim > 2 && centroid) {
1788ceee4971SMatthew G. Knepley     v0[0] = PetscRealPart(coords[0]);
1789ceee4971SMatthew G. Knepley     v0[1] = PetscRealPart(coords[1]);
1790ceee4971SMatthew G. Knepley     v0[2] = PetscRealPart(coords[2]);
1791ceee4971SMatthew G. Knepley   }
1792011ea5d8SMatthew G. Knepley   if (normal) {
1793011ea5d8SMatthew G. Knepley     if (dim > 2) {
17941ee9d5ecSMatthew G. Knepley       const PetscReal x0 = PetscRealPart(coords[dim+0] - coords[0]), x1 = PetscRealPart(coords[dim*2+0] - coords[0]);
17951ee9d5ecSMatthew G. Knepley       const PetscReal y0 = PetscRealPart(coords[dim+1] - coords[1]), y1 = PetscRealPart(coords[dim*2+1] - coords[1]);
17961ee9d5ecSMatthew G. Knepley       const PetscReal z0 = PetscRealPart(coords[dim+2] - coords[2]), z1 = PetscRealPart(coords[dim*2+2] - coords[2]);
17970a1d6728SMatthew G. Knepley       PetscReal       norm;
17980a1d6728SMatthew G. Knepley 
17990a1d6728SMatthew G. Knepley       normal[0] = y0*z1 - z0*y1;
18000a1d6728SMatthew G. Knepley       normal[1] = z0*x1 - x0*z1;
18010a1d6728SMatthew G. Knepley       normal[2] = x0*y1 - y0*x1;
18028b49ba18SBarry Smith       norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2]);
18030a1d6728SMatthew G. Knepley       normal[0] /= norm;
18040a1d6728SMatthew G. Knepley       normal[1] /= norm;
18050a1d6728SMatthew G. Knepley       normal[2] /= norm;
1806011ea5d8SMatthew G. Knepley     } else {
1807011ea5d8SMatthew G. Knepley       for (d = 0; d < dim; ++d) normal[d] = 0.0;
1808011ea5d8SMatthew G. Knepley     }
1809011ea5d8SMatthew G. Knepley   }
1810741bfc07SMatthew G. Knepley   if (dim == 3) {ierr = DMPlexComputeProjection3Dto2D(coordSize, coords, R);CHKERRQ(ierr);}
18110a1d6728SMatthew G. Knepley   for (p = 0; p < numCorners; ++p) {
18120a1d6728SMatthew G. Knepley     /* Need to do this copy to get types right */
18130a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
18141ee9d5ecSMatthew G. Knepley       ctmp[d]      = PetscRealPart(coords[p*tdim+d]);
18151ee9d5ecSMatthew G. Knepley       ctmp[tdim+d] = PetscRealPart(coords[((p+1)%numCorners)*tdim+d]);
18160a1d6728SMatthew G. Knepley     }
18170a1d6728SMatthew G. Knepley     Volume_Triangle_Origin_Internal(&vtmp, ctmp);
18180a1d6728SMatthew G. Knepley     vsum += vtmp;
18190a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
18200a1d6728SMatthew G. Knepley       csum[d] += (ctmp[d] + ctmp[tdim+d])*vtmp;
18210a1d6728SMatthew G. Knepley     }
18220a1d6728SMatthew G. Knepley   }
18230a1d6728SMatthew G. Knepley   for (d = 0; d < tdim; ++d) {
18240a1d6728SMatthew G. Knepley     csum[d] /= (tdim+1)*vsum;
18250a1d6728SMatthew G. Knepley   }
18260a1d6728SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1827ee6bbdb2SSatish Balay   if (vol) *vol = PetscAbsReal(vsum);
18280a1d6728SMatthew G. Knepley   if (centroid) {
18290a1d6728SMatthew G. Knepley     if (dim > 2) {
18300a1d6728SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
18310a1d6728SMatthew G. Knepley         centroid[d] = v0[d];
18320a1d6728SMatthew G. Knepley         for (e = 0; e < dim; ++e) {
18330a1d6728SMatthew G. Knepley           centroid[d] += R[d*dim+e]*csum[e];
18340a1d6728SMatthew G. Knepley         }
18350a1d6728SMatthew G. Knepley       }
18360a1d6728SMatthew G. Knepley     } else for (d = 0; d < dim; ++d) centroid[d] = csum[d];
18370a1d6728SMatthew G. Knepley   }
1838cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1839cc08537eSMatthew G. Knepley }
1840cc08537eSMatthew G. Knepley 
18410ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i ) / V */
1842011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
18430ec8681fSMatthew G. Knepley {
18440ec8681fSMatthew G. Knepley   PetscSection    coordSection;
18450ec8681fSMatthew G. Knepley   Vec             coordinates;
18460ec8681fSMatthew G. Knepley   PetscScalar    *coords = NULL;
184786623015SMatthew G. Knepley   PetscReal       vsum = 0.0, vtmp, coordsTmp[3*3];
1848a7df9edeSMatthew G. Knepley   const PetscInt *faces, *facesO;
18490ec8681fSMatthew G. Knepley   PetscInt        numFaces, f, coordSize, numCorners, p, d;
18500ec8681fSMatthew G. Knepley   PetscErrorCode  ierr;
18510ec8681fSMatthew G. Knepley 
18520ec8681fSMatthew G. Knepley   PetscFunctionBegin;
1853f6dae198SJed Brown   if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim);
18540ec8681fSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
185569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
18560ec8681fSMatthew G. Knepley 
1857d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0;
18580ec8681fSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numFaces);CHKERRQ(ierr);
18590ec8681fSMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr);
1860a7df9edeSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &facesO);CHKERRQ(ierr);
18610ec8681fSMatthew G. Knepley   for (f = 0; f < numFaces; ++f) {
1862011ea5d8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
18630ec8681fSMatthew G. Knepley     numCorners = coordSize/dim;
18640ec8681fSMatthew G. Knepley     switch (numCorners) {
18650ec8681fSMatthew G. Knepley     case 3:
18660ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
18671ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
18681ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
18691ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]);
18700ec8681fSMatthew G. Knepley       }
18710ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1872a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
18730ec8681fSMatthew G. Knepley       vsum += vtmp;
18744f25033aSJed Brown       if (centroid) {           /* Centroid of OABC = (a+b+c)/4 */
18750ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
18761ee9d5ecSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
18770ec8681fSMatthew G. Knepley         }
18780ec8681fSMatthew G. Knepley       }
18790ec8681fSMatthew G. Knepley       break;
18800ec8681fSMatthew G. Knepley     case 4:
18810ec8681fSMatthew G. Knepley       /* DO FOR PYRAMID */
18820ec8681fSMatthew G. Knepley       /* First tet */
18830ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
18841ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
18851ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
18861ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
18870ec8681fSMatthew G. Knepley       }
18880ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1889a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
18900ec8681fSMatthew G. Knepley       vsum += vtmp;
18910ec8681fSMatthew G. Knepley       if (centroid) {
18920ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
18930ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
18940ec8681fSMatthew G. Knepley         }
18950ec8681fSMatthew G. Knepley       }
18960ec8681fSMatthew G. Knepley       /* Second tet */
18970ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
18981ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[1*dim+d]);
18991ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[2*dim+d]);
19001ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
19010ec8681fSMatthew G. Knepley       }
19020ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1903a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
19040ec8681fSMatthew G. Knepley       vsum += vtmp;
19050ec8681fSMatthew G. Knepley       if (centroid) {
19060ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
19070ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
19080ec8681fSMatthew G. Knepley         }
19090ec8681fSMatthew G. Knepley       }
19100ec8681fSMatthew G. Knepley       break;
19110ec8681fSMatthew G. Knepley     default:
1912796f034aSJed Brown       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle faces with %D vertices", numCorners);
19130ec8681fSMatthew G. Knepley     }
19144f25033aSJed Brown     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
19150ec8681fSMatthew G. Knepley   }
19168763be8eSMatthew G. Knepley   if (vol)     *vol = PetscAbsReal(vsum);
19170ec8681fSMatthew G. Knepley   if (normal)   for (d = 0; d < dim; ++d) normal[d]    = 0.0;
1918d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] /= (vsum*4);
19190ec8681fSMatthew G. Knepley   PetscFunctionReturn(0);
19200ec8681fSMatthew G. Knepley }
19210ec8681fSMatthew G. Knepley 
1922834e62ceSMatthew G. Knepley /*@C
1923834e62ceSMatthew G. Knepley   DMPlexComputeCellGeometryFVM - Compute the volume for a given cell
1924834e62ceSMatthew G. Knepley 
1925834e62ceSMatthew G. Knepley   Collective on DM
1926834e62ceSMatthew G. Knepley 
1927834e62ceSMatthew G. Knepley   Input Arguments:
1928834e62ceSMatthew G. Knepley + dm   - the DM
1929834e62ceSMatthew G. Knepley - cell - the cell
1930834e62ceSMatthew G. Knepley 
1931834e62ceSMatthew G. Knepley   Output Arguments:
1932834e62ceSMatthew G. Knepley + volume   - the cell volume
1933cc08537eSMatthew G. Knepley . centroid - the cell centroid
1934cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate
1935834e62ceSMatthew G. Knepley 
1936834e62ceSMatthew G. Knepley   Level: advanced
1937834e62ceSMatthew G. Knepley 
1938834e62ceSMatthew G. Knepley   Fortran Notes:
1939834e62ceSMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1940834e62ceSMatthew G. Knepley   include petsc.h90 in your code.
1941834e62ceSMatthew G. Knepley 
1942*e8964c0aSStefano Zampini .seealso: DMGetCoordinateSection(), DMGetCoordinates()
1943834e62ceSMatthew G. Knepley @*/
1944cc08537eSMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1945834e62ceSMatthew G. Knepley {
19460ec8681fSMatthew G. Knepley   PetscInt       depth, dim;
1947834e62ceSMatthew G. Knepley   PetscErrorCode ierr;
1948834e62ceSMatthew G. Knepley 
1949834e62ceSMatthew G. Knepley   PetscFunctionBegin;
1950834e62ceSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1951c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1952834e62ceSMatthew G. Knepley   if (depth != dim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");
1953834e62ceSMatthew G. Knepley   /* We need to keep a pointer to the depth label */
1954c58f1c22SToby Isaac   ierr = DMGetLabelValue(dm, "depth", cell, &depth);CHKERRQ(ierr);
1955834e62ceSMatthew G. Knepley   /* Cone size is now the number of faces */
1956011ea5d8SMatthew G. Knepley   switch (depth) {
1957cc08537eSMatthew G. Knepley   case 1:
1958011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1959cc08537eSMatthew G. Knepley     break;
1960834e62ceSMatthew G. Knepley   case 2:
1961011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1962834e62ceSMatthew G. Knepley     break;
1963834e62ceSMatthew G. Knepley   case 3:
1964011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1965834e62ceSMatthew G. Knepley     break;
1966834e62ceSMatthew G. Knepley   default:
1967834e62ceSMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
1968834e62ceSMatthew G. Knepley   }
1969834e62ceSMatthew G. Knepley   PetscFunctionReturn(0);
1970834e62ceSMatthew G. Knepley }
1971113c68e6SMatthew G. Knepley 
1972c501906fSMatthew G. Knepley /*@
1973c501906fSMatthew G. Knepley   DMPlexComputeGeometryFEM - Precompute cell geometry for the entire mesh
1974c501906fSMatthew G. Knepley 
1975c501906fSMatthew G. Knepley   Collective on dm
1976c501906fSMatthew G. Knepley 
1977c501906fSMatthew G. Knepley   Input Parameter:
1978c501906fSMatthew G. Knepley . dm - The DMPlex
1979c501906fSMatthew G. Knepley 
1980c501906fSMatthew G. Knepley   Output Parameter:
1981c501906fSMatthew G. Knepley . cellgeom - A vector with the cell geometry data for each cell
1982c501906fSMatthew G. Knepley 
1983c501906fSMatthew G. Knepley   Level: beginner
1984c501906fSMatthew G. Knepley 
1985c501906fSMatthew G. Knepley .keywords: DMPlexComputeCellGeometryFEM()
1986c501906fSMatthew G. Knepley @*/
1987c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom)
1988c0d900a5SMatthew G. Knepley {
1989c0d900a5SMatthew G. Knepley   DM             dmCell;
1990c0d900a5SMatthew G. Knepley   Vec            coordinates;
1991c0d900a5SMatthew G. Knepley   PetscSection   coordSection, sectionCell;
1992c0d900a5SMatthew G. Knepley   PetscScalar   *cgeom;
1993c0d900a5SMatthew G. Knepley   PetscInt       cStart, cEnd, cMax, c;
1994c0d900a5SMatthew G. Knepley   PetscErrorCode ierr;
1995c0d900a5SMatthew G. Knepley 
1996c0d900a5SMatthew G. Knepley   PetscFunctionBegin;
1997c0d900a5SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1998c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1999c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
2000c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
2001c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
2002c0d900a5SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
2003c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2004c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
2005c0d900a5SMatthew G. Knepley   cEnd = cMax < 0 ? cEnd : cMax;
2006c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
2007c0d900a5SMatthew G. Knepley   /* TODO This needs to be multiplied by Nq for non-affine */
20089e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFECellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
2009c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
2010c0d900a5SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
2011c0d900a5SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
2012c0d900a5SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
2013c0d900a5SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2014c0d900a5SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2015c0d900a5SMatthew G. Knepley     PetscFECellGeom *cg;
2016c0d900a5SMatthew G. Knepley 
2017c0d900a5SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2018c0d900a5SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
2019c0d900a5SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v0, cg->J, cg->invJ, &cg->detJ);CHKERRQ(ierr);
2020c0d900a5SMatthew G. Knepley     if (cg->detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", cg->detJ, c);
2021c0d900a5SMatthew G. Knepley   }
2022c0d900a5SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2023c0d900a5SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
2024c0d900a5SMatthew G. Knepley   PetscFunctionReturn(0);
2025c0d900a5SMatthew G. Knepley }
2026c0d900a5SMatthew G. Knepley 
2027891a9168SMatthew G. Knepley /*@
2028891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
2029891a9168SMatthew G. Knepley 
2030891a9168SMatthew G. Knepley   Input Parameter:
2031891a9168SMatthew G. Knepley . dm - The DM
2032891a9168SMatthew G. Knepley 
2033891a9168SMatthew G. Knepley   Output Parameters:
2034891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data
2035891a9168SMatthew G. Knepley . facegeom - A Vec of PetscFVFaceGeom data
2036891a9168SMatthew G. Knepley 
2037891a9168SMatthew G. Knepley   Level: developer
2038891a9168SMatthew G. Knepley 
2039891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM()
2040891a9168SMatthew G. Knepley @*/
2041113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom)
2042113c68e6SMatthew G. Knepley {
2043113c68e6SMatthew G. Knepley   DM             dmFace, dmCell;
2044113c68e6SMatthew G. Knepley   DMLabel        ghostLabel;
2045113c68e6SMatthew G. Knepley   PetscSection   sectionFace, sectionCell;
2046113c68e6SMatthew G. Knepley   PetscSection   coordSection;
2047113c68e6SMatthew G. Knepley   Vec            coordinates;
2048113c68e6SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
2049113c68e6SMatthew G. Knepley   PetscReal      minradius, gminradius;
2050113c68e6SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
2051113c68e6SMatthew G. Knepley   PetscErrorCode ierr;
2052113c68e6SMatthew G. Knepley 
2053113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2054113c68e6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2055113c68e6SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2056113c68e6SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
2057113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
2058113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
2059113c68e6SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
2060113c68e6SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
2061113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
2062113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2063113c68e6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2064113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
20659e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
2066113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
2067113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
2068113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
2069113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
207006348e87SToby Isaac   if (cEndInterior < 0) {
207106348e87SToby Isaac     cEndInterior = cEnd;
207206348e87SToby Isaac   }
2073113c68e6SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2074113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
2075113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
2076113c68e6SMatthew G. Knepley 
2077113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2078113c68e6SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
2079113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr);
2080113c68e6SMatthew G. Knepley   }
2081113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
2082113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmFace);CHKERRQ(ierr);
2083113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionFace);CHKERRQ(ierr);
2084113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
2085113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr);
20869e5edeeeSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
2087113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr);
2088113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmFace, sectionFace);CHKERRQ(ierr);
2089113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionFace);CHKERRQ(ierr);
2090113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr);
2091113c68e6SMatthew G. Knepley   ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr);
2092c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2093113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
2094113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
2095113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2096113c68e6SMatthew G. Knepley     PetscReal        area;
209750d63984SToby Isaac     PetscInt         ghost = -1, d, numChildren;
2098113c68e6SMatthew G. Knepley 
20999ac3fadcSMatthew G. Knepley     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
210050d63984SToby Isaac     ierr = DMPlexGetTreeChildren(dm,f,&numChildren,NULL);CHKERRQ(ierr);
210150d63984SToby Isaac     if (ghost >= 0 || numChildren) continue;
2102113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr);
2103113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr);
2104113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
2105113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
2106113c68e6SMatthew G. Knepley     {
2107113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
210806348e87SToby Isaac       PetscInt         ncells;
2109113c68e6SMatthew G. Knepley       const PetscInt  *cells;
2110113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
21110453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
2112113c68e6SMatthew G. Knepley 
2113113c68e6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr);
211406348e87SToby Isaac       ierr = DMPlexGetSupportSize(dm, f, &ncells);CHKERRQ(ierr);
2115113c68e6SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr);
2116113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
211706348e87SToby Isaac       if (ncells > 1) {
211806348e87SToby Isaac         ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr);
2119113c68e6SMatthew G. Knepley         rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
212006348e87SToby Isaac       }
212106348e87SToby Isaac       else {
212206348e87SToby Isaac         rcentroid = fg->centroid;
212306348e87SToby Isaac       }
21242e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr);
21252e17dfb7SMatthew G. Knepley       ierr = DMLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr);
21260453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
2127113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
2128113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
2129113c68e6SMatthew G. Knepley       }
2130113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
2131113c68e6SMatthew 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]);
2132113c68e6SMatthew 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]);
2133113c68e6SMatthew G. Knepley         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f);
2134113c68e6SMatthew G. Knepley       }
2135113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
2136113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
2137113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2138113c68e6SMatthew G. Knepley       }
213906348e87SToby Isaac       if (ncells > 1 && cells[1] < cEndInterior) {
2140113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
2141113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
2142113c68e6SMatthew G. Knepley       }
2143113c68e6SMatthew G. Knepley     }
2144113c68e6SMatthew G. Knepley   }
2145b2566f29SBarry Smith   ierr = MPIU_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2146113c68e6SMatthew G. Knepley   ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr);
2147113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
2148113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
2149113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
2150113c68e6SMatthew G. Knepley     const PetscInt  *cone,    *support;
2151113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
2152113c68e6SMatthew G. Knepley 
2153113c68e6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr);
2154113c68e6SMatthew G. Knepley     if (coneSize != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize);
2155113c68e6SMatthew G. Knepley     ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr);
2156113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr);
215750d63984SToby Isaac     if (supportSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 2", cone[0], supportSize);
2158113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr);
2159113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr);
2160113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
2161113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
2162113c68e6SMatthew G. Knepley       if (support[s] == c) {
2163640bce14SSatish Balay         PetscFVCellGeom       *ci;
2164113c68e6SMatthew G. Knepley         PetscFVCellGeom       *cg;
2165113c68e6SMatthew G. Knepley         PetscReal              c2f[3], a;
2166113c68e6SMatthew G. Knepley 
2167113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr);
2168113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
2169113c68e6SMatthew G. Knepley         a    = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
2170113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr);
2171113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid);
2172113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
2173113c68e6SMatthew G. Knepley       }
2174113c68e6SMatthew G. Knepley     }
2175113c68e6SMatthew G. Knepley   }
2176113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr);
2177113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
2178113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
2179113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmFace);CHKERRQ(ierr);
2180113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2181113c68e6SMatthew G. Knepley }
2182113c68e6SMatthew G. Knepley 
2183113c68e6SMatthew G. Knepley /*@C
2184113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
2185113c68e6SMatthew G. Knepley 
2186113c68e6SMatthew G. Knepley   Not collective
2187113c68e6SMatthew G. Knepley 
2188113c68e6SMatthew G. Knepley   Input Argument:
2189113c68e6SMatthew G. Knepley . dm - the DM
2190113c68e6SMatthew G. Knepley 
2191113c68e6SMatthew G. Knepley   Output Argument:
2192113c68e6SMatthew G. Knepley . minradius - the minium cell radius
2193113c68e6SMatthew G. Knepley 
2194113c68e6SMatthew G. Knepley   Level: developer
2195113c68e6SMatthew G. Knepley 
2196113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates()
2197113c68e6SMatthew G. Knepley @*/
2198113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius)
2199113c68e6SMatthew G. Knepley {
2200113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2201113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2202113c68e6SMatthew G. Knepley   PetscValidPointer(minradius,2);
2203113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex*) dm->data)->minradius;
2204113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2205113c68e6SMatthew G. Knepley }
2206113c68e6SMatthew G. Knepley 
2207113c68e6SMatthew G. Knepley /*@C
2208113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
2209113c68e6SMatthew G. Knepley 
2210113c68e6SMatthew G. Knepley   Logically collective
2211113c68e6SMatthew G. Knepley 
2212113c68e6SMatthew G. Knepley   Input Arguments:
2213113c68e6SMatthew G. Knepley + dm - the DM
2214113c68e6SMatthew G. Knepley - minradius - the minium cell radius
2215113c68e6SMatthew G. Knepley 
2216113c68e6SMatthew G. Knepley   Level: developer
2217113c68e6SMatthew G. Knepley 
2218113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates()
2219113c68e6SMatthew G. Knepley @*/
2220113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius)
2221113c68e6SMatthew G. Knepley {
2222113c68e6SMatthew G. Knepley   PetscFunctionBegin;
2223113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2224113c68e6SMatthew G. Knepley   ((DM_Plex*) dm->data)->minradius = minradius;
2225113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
2226113c68e6SMatthew G. Knepley }
2227856ac710SMatthew G. Knepley 
2228856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2229856ac710SMatthew G. Knepley {
2230856ac710SMatthew G. Knepley   DMLabel        ghostLabel;
2231856ac710SMatthew G. Knepley   PetscScalar   *dx, *grad, **gref;
2232856ac710SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
2233856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2234856ac710SMatthew G. Knepley 
2235856ac710SMatthew G. Knepley   PetscFunctionBegin;
2236856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2237856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2238856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2239856ac710SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr);
2240856ac710SMatthew G. Knepley   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
2241c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2242856ac710SMatthew G. Knepley   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
2243856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
2244856ac710SMatthew G. Knepley     const PetscInt        *faces;
2245856ac710SMatthew G. Knepley     PetscInt               numFaces, usedFaces, f, d;
2246640bce14SSatish Balay     PetscFVCellGeom        *cg;
2247856ac710SMatthew G. Knepley     PetscBool              boundary;
2248856ac710SMatthew G. Knepley     PetscInt               ghost;
2249856ac710SMatthew G. Knepley 
2250856ac710SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2251856ac710SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr);
2252856ac710SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr);
2253856ac710SMatthew 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);
2254856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
2255640bce14SSatish Balay       PetscFVCellGeom       *cg1;
2256856ac710SMatthew G. Knepley       PetscFVFaceGeom       *fg;
2257856ac710SMatthew G. Knepley       const PetscInt        *fcells;
2258856ac710SMatthew G. Knepley       PetscInt               ncell, side;
2259856ac710SMatthew G. Knepley 
2260856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
2261a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
2262856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
2263856ac710SMatthew G. Knepley       ierr  = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr);
2264856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
2265856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
2266856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr);
2267856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
2268856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d];
2269856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side];  /* Gradient reconstruction term will go here */
2270856ac710SMatthew G. Knepley     }
2271856ac710SMatthew G. Knepley     if (!usedFaces) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
2272856ac710SMatthew G. Knepley     ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr);
2273856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
2274856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
2275a6ba4734SToby Isaac       ierr = DMIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
2276856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
2277856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d];
2278856ac710SMatthew G. Knepley       ++usedFaces;
2279856ac710SMatthew G. Knepley     }
2280856ac710SMatthew G. Knepley   }
2281856ac710SMatthew G. Knepley   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
2282856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2283856ac710SMatthew G. Knepley }
2284856ac710SMatthew G. Knepley 
2285b81db932SToby Isaac static PetscErrorCode BuildGradientReconstruction_Internal_Tree(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
2286b81db932SToby Isaac {
2287b81db932SToby Isaac   DMLabel        ghostLabel;
2288b81db932SToby Isaac   PetscScalar   *dx, *grad, **gref;
2289b81db932SToby Isaac   PetscInt       dim, cStart, cEnd, c, cEndInterior, fStart, fEnd, f, nStart, nEnd, maxNumFaces = 0;
2290b81db932SToby Isaac   PetscSection   neighSec;
2291b81db932SToby Isaac   PetscInt     (*neighbors)[2];
2292b81db932SToby Isaac   PetscInt      *counter;
2293b81db932SToby Isaac   PetscErrorCode ierr;
2294b81db932SToby Isaac 
2295b81db932SToby Isaac   PetscFunctionBegin;
2296b81db932SToby Isaac   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2297b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2298b81db932SToby Isaac   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
22995bc680faSToby Isaac   if (cEndInterior < 0) {
23005bc680faSToby Isaac     cEndInterior = cEnd;
23015bc680faSToby Isaac   }
2302b81db932SToby Isaac   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm),&neighSec);CHKERRQ(ierr);
2303b81db932SToby Isaac   ierr = PetscSectionSetChart(neighSec,cStart,cEndInterior);CHKERRQ(ierr);
2304b81db932SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
2305c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2306b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2307b81db932SToby Isaac     const PetscInt        *fcells;
2308b81db932SToby Isaac     PetscBool              boundary;
23095bc680faSToby Isaac     PetscInt               ghost = -1;
2310b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
2311b81db932SToby Isaac 
231206348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
2313a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
2314b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
2315b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
2316b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
231706348e87SToby Isaac     if (numCells == 2) {
2318b81db932SToby Isaac       ierr = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
2319b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2320b81db932SToby Isaac         PetscInt cell = fcells[c];
2321b81db932SToby Isaac 
2322e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
2323b81db932SToby Isaac           ierr = PetscSectionAddDof(neighSec,cell,1);CHKERRQ(ierr);
2324b81db932SToby Isaac         }
2325b81db932SToby Isaac       }
2326b81db932SToby Isaac     }
232706348e87SToby Isaac   }
2328b81db932SToby Isaac   ierr = PetscSectionSetUp(neighSec);CHKERRQ(ierr);
2329b81db932SToby Isaac   ierr = PetscSectionGetMaxDof(neighSec,&maxNumFaces);CHKERRQ(ierr);
2330b81db932SToby Isaac   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
2331b81db932SToby Isaac   nStart = 0;
2332b81db932SToby Isaac   ierr = PetscSectionGetStorageSize(neighSec,&nEnd);CHKERRQ(ierr);
2333b81db932SToby Isaac   ierr = PetscMalloc1((nEnd-nStart),&neighbors);CHKERRQ(ierr);
2334b81db932SToby Isaac   ierr = PetscCalloc1((cEndInterior-cStart),&counter);CHKERRQ(ierr);
2335b81db932SToby Isaac   for (f = fStart; f < fEnd; f++) {
2336b81db932SToby Isaac     const PetscInt        *fcells;
2337b81db932SToby Isaac     PetscBool              boundary;
23385bc680faSToby Isaac     PetscInt               ghost = -1;
2339b81db932SToby Isaac     PetscInt               numChildren, numCells, c;
2340b81db932SToby Isaac 
234106348e87SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
2342a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, f, &boundary);CHKERRQ(ierr);
2343b81db932SToby Isaac     ierr = DMPlexGetTreeChildren(dm, f, &numChildren, NULL);CHKERRQ(ierr);
2344b81db932SToby Isaac     if ((ghost >= 0) || boundary || numChildren) continue;
2345b81db932SToby Isaac     ierr = DMPlexGetSupportSize(dm, f, &numCells);CHKERRQ(ierr);
234606348e87SToby Isaac     if (numCells == 2) {
2347b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm, f, &fcells);CHKERRQ(ierr);
2348b81db932SToby Isaac       for (c = 0; c < 2; c++) {
2349b81db932SToby Isaac         PetscInt cell = fcells[c], off;
2350b81db932SToby Isaac 
2351e6885bbbSToby Isaac         if (cell >= cStart && cell < cEndInterior) {
2352b81db932SToby Isaac           ierr = PetscSectionGetOffset(neighSec,cell,&off);CHKERRQ(ierr);
2353b81db932SToby Isaac           off += counter[cell - cStart]++;
2354b81db932SToby Isaac           neighbors[off][0] = f;
2355b81db932SToby Isaac           neighbors[off][1] = fcells[1 - c];
2356b81db932SToby Isaac         }
2357b81db932SToby Isaac       }
2358b81db932SToby Isaac     }
235906348e87SToby Isaac   }
2360b81db932SToby Isaac   ierr = PetscFree(counter);CHKERRQ(ierr);
2361b81db932SToby Isaac   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
2362b81db932SToby Isaac   for (c = cStart; c < cEndInterior; c++) {
2363317218b9SToby Isaac     PetscInt               numFaces, f, d, off, ghost = -1;
2364640bce14SSatish Balay     PetscFVCellGeom        *cg;
2365b81db932SToby Isaac 
2366b81db932SToby Isaac     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
2367b81db932SToby Isaac     ierr = PetscSectionGetDof(neighSec, c, &numFaces);CHKERRQ(ierr);
2368b81db932SToby Isaac     ierr = PetscSectionGetOffset(neighSec, c, &off);CHKERRQ(ierr);
2369317218b9SToby Isaac     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, c, &ghost);CHKERRQ(ierr);}
2370317218b9SToby 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);
2371b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2372640bce14SSatish Balay       PetscFVCellGeom       *cg1;
2373b81db932SToby Isaac       PetscFVFaceGeom       *fg;
2374b81db932SToby Isaac       const PetscInt        *fcells;
2375b81db932SToby Isaac       PetscInt               ncell, side, nface;
2376b81db932SToby Isaac 
2377b81db932SToby Isaac       nface = neighbors[off + f][0];
2378b81db932SToby Isaac       ncell = neighbors[off + f][1];
2379b81db932SToby Isaac       ierr  = DMPlexGetSupport(dm,nface,&fcells);CHKERRQ(ierr);
2380b81db932SToby Isaac       side  = (c != fcells[0]);
2381b81db932SToby Isaac       ierr  = DMPlexPointLocalRef(dmFace, nface, fgeom, &fg);CHKERRQ(ierr);
2382b81db932SToby Isaac       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
2383b81db932SToby Isaac       for (d = 0; d < dim; ++d) dx[f*dim+d] = cg1->centroid[d] - cg->centroid[d];
2384b81db932SToby Isaac       gref[f] = fg->grad[side];  /* Gradient reconstruction term will go here */
2385b81db932SToby Isaac     }
2386b81db932SToby Isaac     ierr = PetscFVComputeGradient(fvm, numFaces, dx, grad);CHKERRQ(ierr);
2387b81db932SToby Isaac     for (f = 0; f < numFaces; ++f) {
2388b81db932SToby Isaac       for (d = 0; d < dim; ++d) gref[f][d] = grad[f*dim+d];
2389b81db932SToby Isaac     }
2390b81db932SToby Isaac   }
2391b81db932SToby Isaac   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
23925fe94518SToby Isaac   ierr = PetscSectionDestroy(&neighSec);CHKERRQ(ierr);
2393b81db932SToby Isaac   ierr = PetscFree(neighbors);CHKERRQ(ierr);
2394b81db932SToby Isaac   PetscFunctionReturn(0);
2395b81db932SToby Isaac }
2396b81db932SToby Isaac 
2397856ac710SMatthew G. Knepley /*@
2398856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
2399856ac710SMatthew G. Knepley 
2400856ac710SMatthew G. Knepley   Collective on DM
2401856ac710SMatthew G. Knepley 
2402856ac710SMatthew G. Knepley   Input Arguments:
2403856ac710SMatthew G. Knepley + dm  - The DM
2404856ac710SMatthew G. Knepley . fvm - The PetscFV
24058f9f38e3SMatthew G. Knepley . faceGeometry - The face geometry from DMPlexComputeFaceGeometryFVM()
24068f9f38e3SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexComputeCellGeometryFVM()
2407856ac710SMatthew G. Knepley 
2408856ac710SMatthew G. Knepley   Output Parameters:
2409856ac710SMatthew G. Knepley + faceGeometry - The geometric factors for gradient calculation are inserted
2410856ac710SMatthew G. Knepley - dmGrad - The DM describing the layout of gradient data
2411856ac710SMatthew G. Knepley 
2412856ac710SMatthew G. Knepley   Level: developer
2413856ac710SMatthew G. Knepley 
2414856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM()
2415856ac710SMatthew G. Knepley @*/
2416856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad)
2417856ac710SMatthew G. Knepley {
2418856ac710SMatthew G. Knepley   DM             dmFace, dmCell;
2419856ac710SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
2420b81db932SToby Isaac   PetscSection   sectionGrad, parentSection;
2421856ac710SMatthew G. Knepley   PetscInt       dim, pdim, cStart, cEnd, cEndInterior, c;
2422856ac710SMatthew G. Knepley   PetscErrorCode ierr;
2423856ac710SMatthew G. Knepley 
2424856ac710SMatthew G. Knepley   PetscFunctionBegin;
2425856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2426856ac710SMatthew G. Knepley   ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr);
2427856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2428856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2429856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
2430856ac710SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
2431856ac710SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
2432856ac710SMatthew G. Knepley   ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2433856ac710SMatthew G. Knepley   ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2434b81db932SToby Isaac   ierr = DMPlexGetTree(dm,&parentSection,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
2435b81db932SToby Isaac   if (!parentSection) {
2436856ac710SMatthew G. Knepley     ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2437b5a3613cSMatthew G. Knepley   } else {
2438b81db932SToby Isaac     ierr = BuildGradientReconstruction_Internal_Tree(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
2439b81db932SToby Isaac   }
2440856ac710SMatthew G. Knepley   ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr);
2441856ac710SMatthew G. Knepley   ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr);
2442856ac710SMatthew G. Knepley   /* Create storage for gradients */
2443856ac710SMatthew G. Knepley   ierr = DMClone(dm, dmGrad);CHKERRQ(ierr);
2444856ac710SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionGrad);CHKERRQ(ierr);
2445856ac710SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr);
2446856ac710SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);}
2447856ac710SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr);
2448856ac710SMatthew G. Knepley   ierr = DMSetDefaultSection(*dmGrad, sectionGrad);CHKERRQ(ierr);
2449856ac710SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionGrad);CHKERRQ(ierr);
2450856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
2451856ac710SMatthew G. Knepley }
2452b27d5b9eSToby Isaac 
2453c501906fSMatthew G. Knepley /*@
2454c501906fSMatthew G. Knepley   DMPlexGetDataFVM - Retrieve precomputed cell geometry
2455c501906fSMatthew G. Knepley 
2456c501906fSMatthew G. Knepley   Collective on DM
2457c501906fSMatthew G. Knepley 
2458c501906fSMatthew G. Knepley   Input Arguments:
2459c501906fSMatthew G. Knepley + dm  - The DM
2460c501906fSMatthew G. Knepley - fvm - The PetscFV
2461c501906fSMatthew G. Knepley 
2462c501906fSMatthew G. Knepley   Output Parameters:
2463c501906fSMatthew G. Knepley + cellGeometry - The cell geometry
2464c501906fSMatthew G. Knepley . faceGeometry - The face geometry
2465c501906fSMatthew G. Knepley - dmGrad       - The gradient matrices
2466c501906fSMatthew G. Knepley 
2467c501906fSMatthew G. Knepley   Level: developer
2468c501906fSMatthew G. Knepley 
2469c501906fSMatthew G. Knepley .seealso: DMPlexComputeGeometryFVM()
2470c501906fSMatthew G. Knepley @*/
2471b27d5b9eSToby Isaac PetscErrorCode DMPlexGetDataFVM(DM dm, PetscFV fv, Vec *cellgeom, Vec *facegeom, DM *gradDM)
2472b27d5b9eSToby Isaac {
2473b27d5b9eSToby Isaac   PetscObject    cellgeomobj, facegeomobj;
2474b27d5b9eSToby Isaac   PetscErrorCode ierr;
2475b27d5b9eSToby Isaac 
2476b27d5b9eSToby Isaac   PetscFunctionBegin;
2477b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2478b27d5b9eSToby Isaac   if (!cellgeomobj) {
2479b27d5b9eSToby Isaac     Vec cellgeomInt, facegeomInt;
2480b27d5b9eSToby Isaac 
2481b27d5b9eSToby Isaac     ierr = DMPlexComputeGeometryFVM(dm, &cellgeomInt, &facegeomInt);CHKERRQ(ierr);
2482b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_cellgeom_fvm",(PetscObject)cellgeomInt);CHKERRQ(ierr);
2483b27d5b9eSToby Isaac     ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_facegeom_fvm",(PetscObject)facegeomInt);CHKERRQ(ierr);
2484b27d5b9eSToby Isaac     ierr = VecDestroy(&cellgeomInt);CHKERRQ(ierr);
2485b27d5b9eSToby Isaac     ierr = VecDestroy(&facegeomInt);CHKERRQ(ierr);
2486b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_cellgeom_fvm", &cellgeomobj);CHKERRQ(ierr);
2487b27d5b9eSToby Isaac   }
2488b27d5b9eSToby Isaac   ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_facegeom_fvm", &facegeomobj);CHKERRQ(ierr);
2489b27d5b9eSToby Isaac   if (cellgeom) *cellgeom = (Vec) cellgeomobj;
2490b27d5b9eSToby Isaac   if (facegeom) *facegeom = (Vec) facegeomobj;
2491b27d5b9eSToby Isaac   if (gradDM) {
2492b27d5b9eSToby Isaac     PetscObject gradobj;
2493b27d5b9eSToby Isaac     PetscBool   computeGradients;
2494b27d5b9eSToby Isaac 
2495b27d5b9eSToby Isaac     ierr = PetscFVGetComputeGradients(fv,&computeGradients);CHKERRQ(ierr);
2496b27d5b9eSToby Isaac     if (!computeGradients) {
2497b27d5b9eSToby Isaac       *gradDM = NULL;
2498b27d5b9eSToby Isaac       PetscFunctionReturn(0);
2499b27d5b9eSToby Isaac     }
2500b27d5b9eSToby Isaac     ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2501b27d5b9eSToby Isaac     if (!gradobj) {
2502b27d5b9eSToby Isaac       DM dmGradInt;
2503b27d5b9eSToby Isaac 
2504b27d5b9eSToby Isaac       ierr = DMPlexComputeGradientFVM(dm,fv,(Vec) facegeomobj,(Vec) cellgeomobj,&dmGradInt);CHKERRQ(ierr);
2505b27d5b9eSToby Isaac       ierr = PetscObjectCompose((PetscObject) dm, "DMPlex_dmgrad_fvm", (PetscObject)dmGradInt);CHKERRQ(ierr);
2506b27d5b9eSToby Isaac       ierr = DMDestroy(&dmGradInt);CHKERRQ(ierr);
2507b27d5b9eSToby Isaac       ierr = PetscObjectQuery((PetscObject) dm, "DMPlex_dmgrad_fvm", &gradobj);CHKERRQ(ierr);
2508b27d5b9eSToby Isaac     }
2509b27d5b9eSToby Isaac     *gradDM = (DM) gradobj;
2510b27d5b9eSToby Isaac   }
2511b27d5b9eSToby Isaac   PetscFunctionReturn(0);
2512b27d5b9eSToby Isaac }
2513d6143a4eSToby Isaac 
25149d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_NewtonUpdate(PetscInt dimC, PetscInt dimR, PetscScalar *J, PetscScalar *invJ, PetscScalar *work,  PetscReal *resNeg, PetscReal *guess)
25159d150b73SToby Isaac {
25169d150b73SToby Isaac   PetscInt l, m;
25179d150b73SToby Isaac 
2518cd345991SToby Isaac   PetscFunctionBeginHot;
25199d150b73SToby Isaac   if (dimC == dimR && dimR <= 3) {
25209d150b73SToby Isaac     /* invert Jacobian, multiply */
25219d150b73SToby Isaac     PetscScalar det, idet;
25229d150b73SToby Isaac 
25239d150b73SToby Isaac     switch (dimR) {
25249d150b73SToby Isaac     case 1:
25259d150b73SToby Isaac       invJ[0] = 1./ J[0];
25269d150b73SToby Isaac       break;
25279d150b73SToby Isaac     case 2:
25289d150b73SToby Isaac       det = J[0] * J[3] - J[1] * J[2];
25299d150b73SToby Isaac       idet = 1./det;
25309d150b73SToby Isaac       invJ[0] =  J[3] * idet;
25319d150b73SToby Isaac       invJ[1] = -J[1] * idet;
25329d150b73SToby Isaac       invJ[2] = -J[2] * idet;
25339d150b73SToby Isaac       invJ[3] =  J[0] * idet;
25349d150b73SToby Isaac       break;
25359d150b73SToby Isaac     case 3:
25369d150b73SToby Isaac       {
25379d150b73SToby Isaac         invJ[0] = J[4] * J[8] - J[5] * J[7];
25389d150b73SToby Isaac         invJ[1] = J[2] * J[7] - J[1] * J[8];
25399d150b73SToby Isaac         invJ[2] = J[1] * J[5] - J[2] * J[4];
25409d150b73SToby Isaac         det = invJ[0] * J[0] + invJ[1] * J[3] + invJ[2] * J[6];
25419d150b73SToby Isaac         idet = 1./det;
25429d150b73SToby Isaac         invJ[0] *= idet;
25439d150b73SToby Isaac         invJ[1] *= idet;
25449d150b73SToby Isaac         invJ[2] *= idet;
25459d150b73SToby Isaac         invJ[3]  = idet * (J[5] * J[6] - J[3] * J[8]);
25469d150b73SToby Isaac         invJ[4]  = idet * (J[0] * J[8] - J[2] * J[6]);
25479d150b73SToby Isaac         invJ[5]  = idet * (J[2] * J[3] - J[0] * J[5]);
25489d150b73SToby Isaac         invJ[6]  = idet * (J[3] * J[7] - J[4] * J[6]);
25499d150b73SToby Isaac         invJ[7]  = idet * (J[1] * J[6] - J[0] * J[7]);
25509d150b73SToby Isaac         invJ[8]  = idet * (J[0] * J[4] - J[1] * J[3]);
25519d150b73SToby Isaac       }
25529d150b73SToby Isaac       break;
25539d150b73SToby Isaac     }
25549d150b73SToby Isaac     for (l = 0; l < dimR; l++) {
25559d150b73SToby Isaac       for (m = 0; m < dimC; m++) {
2556c6e120d1SToby Isaac         guess[l] += PetscRealPart(invJ[l * dimC + m]) * resNeg[m];
25579d150b73SToby Isaac       }
25589d150b73SToby Isaac     }
25599d150b73SToby Isaac   } else {
25609d150b73SToby Isaac #if defined(PETSC_USE_COMPLEX)
25619d150b73SToby Isaac     char transpose = 'C';
25629d150b73SToby Isaac #else
25639d150b73SToby Isaac     char transpose = 'T';
25649d150b73SToby Isaac #endif
25659d150b73SToby Isaac     PetscBLASInt m = dimR;
25669d150b73SToby Isaac     PetscBLASInt n = dimC;
25679d150b73SToby Isaac     PetscBLASInt one = 1;
25689d150b73SToby Isaac     PetscBLASInt worksize = dimR * dimC, info;
25699d150b73SToby Isaac 
25709d150b73SToby Isaac     for (l = 0; l < dimC; l++) {invJ[l] = resNeg[l];}
25719d150b73SToby Isaac 
25729d150b73SToby Isaac     PetscStackCallBLAS("LAPACKgels",LAPACKgels_(&transpose,&m,&n,&one,J,&m,invJ,&n,work,&worksize, &info));
25739d150b73SToby Isaac     if (info != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad argument to GELS");
25749d150b73SToby Isaac 
2575c6e120d1SToby Isaac     for (l = 0; l < dimR; l++) {guess[l] += PetscRealPart(invJ[l]);}
25769d150b73SToby Isaac   }
25779d150b73SToby Isaac   PetscFunctionReturn(0);
25789d150b73SToby Isaac }
25799d150b73SToby Isaac 
25809d150b73SToby Isaac static PetscErrorCode DMPlexCoordinatesToReference_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
25819d150b73SToby Isaac {
2582c0cbe899SToby Isaac   PetscInt       coordSize, i, j, k, l, m, maxIts = 7, numV = (1 << dimR);
25839d150b73SToby Isaac   PetscScalar    *coordsScalar = NULL;
25849d150b73SToby Isaac   PetscReal      *cellData, *cellCoords, *cellCoeffs, *extJ, *resNeg;
25859d150b73SToby Isaac   PetscScalar    *J, *invJ, *work;
25869d150b73SToby Isaac   PetscErrorCode ierr;
25879d150b73SToby Isaac 
25889d150b73SToby Isaac   PetscFunctionBegin;
25899d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25909d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
259113903a91SSatish Balay   if (coordSize < dimC * numV) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expecting at least %D coordinates, got %D",dimC * (1 << dimR), coordSize);
259269291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData);CHKERRQ(ierr);
259369291d52SBarry Smith   ierr = DMGetWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J);CHKERRQ(ierr);
25949d150b73SToby Isaac   cellCoords = &cellData[0];
25959d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
25969d150b73SToby Isaac   extJ       = &cellData[2 * coordSize];
25979d150b73SToby Isaac   resNeg     = &cellData[2 * coordSize + dimR];
25989d150b73SToby Isaac   invJ       = &J[dimR * dimC];
25999d150b73SToby Isaac   work       = &J[2 * dimR * dimC];
26009d150b73SToby Isaac   if (dimR == 2) {
26019d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
26029d150b73SToby Isaac 
26039d150b73SToby Isaac     for (i = 0; i < 4; i++) {
26049d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
26059d150b73SToby Isaac 
26069d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
26079d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
26089d150b73SToby Isaac       }
26099d150b73SToby Isaac     }
26109d150b73SToby Isaac   } else if (dimR == 3) {
26119d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
26129d150b73SToby Isaac 
26139d150b73SToby Isaac     for (i = 0; i < 8; i++) {
26149d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
26159d150b73SToby Isaac 
26169d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
26179d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
26189d150b73SToby Isaac       }
26199d150b73SToby Isaac     }
26209d150b73SToby Isaac   } else {
26219d150b73SToby Isaac     for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);}
26229d150b73SToby Isaac   }
26239d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
26249d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
26259d150b73SToby Isaac     PetscReal *swap;
26269d150b73SToby Isaac 
26279d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
26289d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
26299d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
26309d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
26319d150b73SToby Isaac       }
26329d150b73SToby Isaac     }
26339d150b73SToby Isaac 
26349d150b73SToby Isaac     if (i < dimR - 1) {
26359d150b73SToby Isaac       swap = cellCoeffs;
26369d150b73SToby Isaac       cellCoeffs = cellCoords;
26379d150b73SToby Isaac       cellCoords = swap;
26389d150b73SToby Isaac     }
26399d150b73SToby Isaac   }
26409d150b73SToby Isaac   ierr = PetscMemzero(refCoords,numPoints * dimR * sizeof (PetscReal));CHKERRQ(ierr);
26419d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
26429d150b73SToby Isaac     for (i = 0; i < maxIts; i++) {
26439d150b73SToby Isaac       PetscReal *guess = &refCoords[dimR * j];
26449d150b73SToby Isaac 
26459d150b73SToby Isaac       /* compute -residual and Jacobian */
26469d150b73SToby Isaac       for (k = 0; k < dimC; k++) {resNeg[k] = realCoords[dimC * j + k];}
26479d150b73SToby Isaac       for (k = 0; k < dimC * dimR; k++) {J[k] = 0.;}
26489d150b73SToby Isaac       for (k = 0; k < numV; k++) {
26499d150b73SToby Isaac         PetscReal extCoord = 1.;
26509d150b73SToby Isaac         for (l = 0; l < dimR; l++) {
26519d150b73SToby Isaac           PetscReal coord = guess[l];
26529d150b73SToby Isaac           PetscInt  dep   = (k & (1 << l)) >> l;
26539d150b73SToby Isaac 
26549d150b73SToby Isaac           extCoord *= dep * coord + !dep;
26559d150b73SToby Isaac           extJ[l] = dep;
26569d150b73SToby Isaac 
26579d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
26589d150b73SToby Isaac             PetscReal coord = guess[m];
26599d150b73SToby Isaac             PetscInt  dep   = ((k & (1 << m)) >> m) && (m != l);
26609d150b73SToby Isaac             PetscReal mult  = dep * coord + !dep;
26619d150b73SToby Isaac 
26629d150b73SToby Isaac             extJ[l] *= mult;
26639d150b73SToby Isaac           }
26649d150b73SToby Isaac         }
26659d150b73SToby Isaac         for (l = 0; l < dimC; l++) {
26669d150b73SToby Isaac           PetscReal coeff = cellCoeffs[dimC * k + l];
26679d150b73SToby Isaac 
26689d150b73SToby Isaac           resNeg[l] -= coeff * extCoord;
26699d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
26709d150b73SToby Isaac             J[dimR * l + m] += coeff * extJ[m];
26719d150b73SToby Isaac           }
26729d150b73SToby Isaac         }
26739d150b73SToby Isaac       }
26740611203eSToby Isaac #if 0 && defined(PETSC_USE_DEBUG)
26750611203eSToby Isaac       {
26760611203eSToby Isaac         PetscReal maxAbs = 0.;
26770611203eSToby Isaac 
26780611203eSToby Isaac         for (l = 0; l < dimC; l++) {
26790611203eSToby Isaac           maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l]));
26800611203eSToby Isaac         }
26810611203eSToby Isaac         ierr = PetscInfo4(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,maxAbs);CHKERRQ(ierr);
26820611203eSToby Isaac       }
26830611203eSToby Isaac #endif
26849d150b73SToby Isaac 
26859d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_NewtonUpdate(dimC,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr);
26869d150b73SToby Isaac     }
26879d150b73SToby Isaac   }
268869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 3 * dimR * dimC, MPIU_SCALAR, &J);CHKERRQ(ierr);
268969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * coordSize + dimR + dimC, MPIU_REAL, &cellData);CHKERRQ(ierr);
26909d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
26919d150b73SToby Isaac   PetscFunctionReturn(0);
26929d150b73SToby Isaac }
26939d150b73SToby Isaac 
26949d150b73SToby Isaac static PetscErrorCode DMPlexReferenceToCoordinates_Tensor(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt dimC, PetscInt dimR)
26959d150b73SToby Isaac {
26969d150b73SToby Isaac   PetscInt       coordSize, i, j, k, l, numV = (1 << dimR);
26979d150b73SToby Isaac   PetscScalar    *coordsScalar = NULL;
26989d150b73SToby Isaac   PetscReal      *cellData, *cellCoords, *cellCoeffs;
26999d150b73SToby Isaac   PetscErrorCode ierr;
27009d150b73SToby Isaac 
27019d150b73SToby Isaac   PetscFunctionBegin;
27029d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27039d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
270413903a91SSatish Balay   if (coordSize < dimC * numV) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expecting at least %D coordinates, got %D",dimC * (1 << dimR), coordSize);
270569291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData);CHKERRQ(ierr);
27069d150b73SToby Isaac   cellCoords = &cellData[0];
27079d150b73SToby Isaac   cellCoeffs = &cellData[coordSize];
27089d150b73SToby Isaac   if (dimR == 2) {
27099d150b73SToby Isaac     const PetscInt zToPlex[4] = {0, 1, 3, 2};
27109d150b73SToby Isaac 
27119d150b73SToby Isaac     for (i = 0; i < 4; i++) {
27129d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
27139d150b73SToby Isaac 
27149d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
27159d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
27169d150b73SToby Isaac       }
27179d150b73SToby Isaac     }
27189d150b73SToby Isaac   } else if (dimR == 3) {
27199d150b73SToby Isaac     const PetscInt zToPlex[8] = {0, 3, 1, 2, 4, 5, 7, 6};
27209d150b73SToby Isaac 
27219d150b73SToby Isaac     for (i = 0; i < 8; i++) {
27229d150b73SToby Isaac       PetscInt plexI = zToPlex[i];
27239d150b73SToby Isaac 
27249d150b73SToby Isaac       for (j = 0; j < dimC; j++) {
27259d150b73SToby Isaac         cellCoords[dimC * i + j] = PetscRealPart(coordsScalar[dimC * plexI + j]);
27269d150b73SToby Isaac       }
27279d150b73SToby Isaac     }
27289d150b73SToby Isaac   } else {
27299d150b73SToby Isaac     for (i = 0; i < coordSize; i++) {cellCoords[i] = PetscRealPart(coordsScalar[i]);}
27309d150b73SToby Isaac   }
27319d150b73SToby Isaac   /* Perform the shuffling transform that converts values at the corners of [-1,1]^d to coefficients */
27329d150b73SToby Isaac   for (i = 0; i < dimR; i++) {
27339d150b73SToby Isaac     PetscReal *swap;
27349d150b73SToby Isaac 
27359d150b73SToby Isaac     for (j = 0; j < (numV / 2); j++) {
27369d150b73SToby Isaac       for (k = 0; k < dimC; k++) {
27379d150b73SToby Isaac         cellCoeffs[dimC * j + k]                = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] + cellCoords[dimC * 2 * j + k]);
27389d150b73SToby Isaac         cellCoeffs[dimC * (j + (numV / 2)) + k] = 0.5 * (cellCoords[dimC * (2 * j + 1) + k] - cellCoords[dimC * 2 * j + k]);
27399d150b73SToby Isaac       }
27409d150b73SToby Isaac     }
27419d150b73SToby Isaac 
27429d150b73SToby Isaac     if (i < dimR - 1) {
27439d150b73SToby Isaac       swap = cellCoeffs;
27449d150b73SToby Isaac       cellCoeffs = cellCoords;
27459d150b73SToby Isaac       cellCoords = swap;
27469d150b73SToby Isaac     }
27479d150b73SToby Isaac   }
27489d150b73SToby Isaac   ierr = PetscMemzero(realCoords,numPoints * dimC * sizeof (PetscReal));CHKERRQ(ierr);
27499d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
27509d150b73SToby Isaac     const PetscReal *guess  = &refCoords[dimR * j];
27519d150b73SToby Isaac     PetscReal       *mapped = &realCoords[dimC * j];
27529d150b73SToby Isaac 
27539d150b73SToby Isaac     for (k = 0; k < numV; k++) {
27549d150b73SToby Isaac       PetscReal extCoord = 1.;
27559d150b73SToby Isaac       for (l = 0; l < dimR; l++) {
27569d150b73SToby Isaac         PetscReal coord = guess[l];
27579d150b73SToby Isaac         PetscInt  dep   = (k & (1 << l)) >> l;
27589d150b73SToby Isaac 
27599d150b73SToby Isaac         extCoord *= dep * coord + !dep;
27609d150b73SToby Isaac       }
27619d150b73SToby Isaac       for (l = 0; l < dimC; l++) {
27629d150b73SToby Isaac         PetscReal coeff = cellCoeffs[dimC * k + l];
27639d150b73SToby Isaac 
27649d150b73SToby Isaac         mapped[l] += coeff * extCoord;
27659d150b73SToby Isaac       }
27669d150b73SToby Isaac     }
27679d150b73SToby Isaac   }
276869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * coordSize, MPIU_REAL, &cellData);CHKERRQ(ierr);
27699d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &coordsScalar);CHKERRQ(ierr);
27709d150b73SToby Isaac   PetscFunctionReturn(0);
27719d150b73SToby Isaac }
27729d150b73SToby Isaac 
27739c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
27749c3cf19fSMatthew G. Knepley static PetscErrorCode DMPlexCoordinatesToReference_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[], Vec coords, PetscInt Nc, PetscInt dimR)
27759d150b73SToby Isaac {
27769c3cf19fSMatthew G. Knepley   PetscInt       numComp, pdim, i, j, k, l, m, maxIter = 7, coordSize;
2777c6e120d1SToby Isaac   PetscScalar    *nodes = NULL;
2778c6e120d1SToby Isaac   PetscReal      *invV, *modes;
2779c6e120d1SToby Isaac   PetscReal      *B, *D, *resNeg;
2780c6e120d1SToby Isaac   PetscScalar    *J, *invJ, *work;
27819d150b73SToby Isaac   PetscErrorCode ierr;
27829d150b73SToby Isaac 
27839d150b73SToby Isaac   PetscFunctionBegin;
27849c3cf19fSMatthew G. Knepley   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
27859d150b73SToby Isaac   ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
27869c3cf19fSMatthew G. Knepley   if (numComp != Nc) SETERRQ2(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"coordinate discretization must have as many components (%D) as embedding dimension (!= %D)",numComp,Nc);
27879d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
27889d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
278969291d52SBarry Smith   ierr = DMGetWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
27909d150b73SToby Isaac   invV = fe->invV;
2791012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
2792012b7cc6SMatthew G. Knepley     modes[i] = 0.;
2793012b7cc6SMatthew G. Knepley     for (j = 0; j < pdim; ++j) {
2794012b7cc6SMatthew G. Knepley       modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]);
27959d150b73SToby Isaac     }
27969d150b73SToby Isaac   }
279769291d52SBarry Smith   ierr   = DMGetWorkArray(dm,pdim * Nc + pdim * Nc * dimR + Nc,MPIU_REAL,&B);CHKERRQ(ierr);
27989c3cf19fSMatthew G. Knepley   D      = &B[pdim*Nc];
27999c3cf19fSMatthew G. Knepley   resNeg = &D[pdim*Nc * dimR];
280069291d52SBarry Smith   ierr = DMGetWorkArray(dm,3 * Nc * dimR,MPIU_SCALAR,&J);CHKERRQ(ierr);
28019c3cf19fSMatthew G. Knepley   invJ = &J[Nc * dimR];
28029c3cf19fSMatthew G. Knepley   work = &invJ[Nc * dimR];
28039d150b73SToby Isaac   for (i = 0; i < numPoints * dimR; i++) {refCoords[i] = 0.;}
28049d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
28059b1f03cbSToby 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 */
28069d150b73SToby Isaac       PetscReal *guess = &refCoords[j * dimR];
28079d150b73SToby Isaac       ierr = PetscSpaceEvaluate(fe->basisSpace, 1, guess, B, D, NULL);CHKERRQ(ierr);
28089c3cf19fSMatthew G. Knepley       for (k = 0; k < Nc; k++) {resNeg[k] = realCoords[j * Nc + k];}
28099c3cf19fSMatthew G. Knepley       for (k = 0; k < Nc * dimR; k++) {J[k] = 0.;}
28109c3cf19fSMatthew G. Knepley       for (k = 0; k < pdim; k++) {
28119c3cf19fSMatthew G. Knepley         for (l = 0; l < Nc; l++) {
2812012b7cc6SMatthew G. Knepley           resNeg[l] -= modes[k] * B[k * Nc + l];
28139d150b73SToby Isaac           for (m = 0; m < dimR; m++) {
2814012b7cc6SMatthew G. Knepley             J[l * dimR + m] += modes[k] * D[(k * Nc + l) * dimR + m];
28159d150b73SToby Isaac           }
28169d150b73SToby Isaac         }
28179d150b73SToby Isaac       }
28180611203eSToby Isaac #if 0 && defined(PETSC_USE_DEBUG)
28190611203eSToby Isaac       {
28200611203eSToby Isaac         PetscReal maxAbs = 0.;
28210611203eSToby Isaac 
28229c3cf19fSMatthew G. Knepley         for (l = 0; l < Nc; l++) {
28230611203eSToby Isaac           maxAbs = PetscMax(maxAbs,PetscAbsReal(resNeg[l]));
28240611203eSToby Isaac         }
28250611203eSToby Isaac         ierr = PetscInfo4(dm,"cell %D, point %D, iter %D: res %g\n",cell,j,i,maxAbs);CHKERRQ(ierr);
28260611203eSToby Isaac       }
28270611203eSToby Isaac #endif
28289c3cf19fSMatthew G. Knepley       ierr = DMPlexCoordinatesToReference_NewtonUpdate(Nc,dimR,J,invJ,work,resNeg,guess);CHKERRQ(ierr);
28299d150b73SToby Isaac     }
28309d150b73SToby Isaac   }
283169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,3 * Nc * dimR,MPIU_SCALAR,&J);CHKERRQ(ierr);
283269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,pdim * Nc + pdim * Nc * dimR + Nc,MPIU_REAL,&B);CHKERRQ(ierr);
283369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
28349d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
28359d150b73SToby Isaac   PetscFunctionReturn(0);
28369d150b73SToby Isaac }
28379d150b73SToby Isaac 
28389c3cf19fSMatthew G. Knepley /* TODO: TOBY please fix this for Nc > 1 */
28399c3cf19fSMatthew G. Knepley static PetscErrorCode DMPlexReferenceToCoordinates_FE(DM dm, PetscFE fe, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[], Vec coords, PetscInt Nc, PetscInt dimR)
28409d150b73SToby Isaac {
28419c3cf19fSMatthew G. Knepley   PetscInt       numComp, pdim, i, j, k, l, coordSize;
2842c6e120d1SToby Isaac   PetscScalar    *nodes = NULL;
2843c6e120d1SToby Isaac   PetscReal      *invV, *modes;
28449d150b73SToby Isaac   PetscReal      *B;
28459d150b73SToby Isaac   PetscErrorCode ierr;
28469d150b73SToby Isaac 
28479d150b73SToby Isaac   PetscFunctionBegin;
28489c3cf19fSMatthew G. Knepley   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
28499d150b73SToby Isaac   ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
28509c3cf19fSMatthew G. Knepley   if (numComp != Nc) SETERRQ2(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"coordinate discretization must have as many components (%D) as embedding dimension (!= %D)",numComp,Nc);
28519d150b73SToby Isaac   ierr = DMPlexVecGetClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
28529d150b73SToby Isaac   /* convert nodes to values in the stable evaluation basis */
285369291d52SBarry Smith   ierr = DMGetWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
28549d150b73SToby Isaac   invV = fe->invV;
2855012b7cc6SMatthew G. Knepley   for (i = 0; i < pdim; ++i) {
2856012b7cc6SMatthew G. Knepley     modes[i] = 0.;
2857012b7cc6SMatthew G. Knepley     for (j = 0; j < pdim; ++j) {
2858012b7cc6SMatthew G. Knepley       modes[i] += invV[i * pdim + j] * PetscRealPart(nodes[j]);
28599d150b73SToby Isaac     }
28609d150b73SToby Isaac   }
286169291d52SBarry Smith   ierr = DMGetWorkArray(dm,numPoints * pdim * Nc,MPIU_REAL,&B);CHKERRQ(ierr);
2862012b7cc6SMatthew G. Knepley   ierr = PetscSpaceEvaluate(fe->basisSpace, numPoints, refCoords, B, NULL, NULL);CHKERRQ(ierr);
28639c3cf19fSMatthew G. Knepley   for (i = 0; i < numPoints * Nc; i++) {realCoords[i] = 0.;}
28649d150b73SToby Isaac   for (j = 0; j < numPoints; j++) {
28659c3cf19fSMatthew G. Knepley     PetscReal *mapped = &realCoords[j * Nc];
28669d150b73SToby Isaac 
28679c3cf19fSMatthew G. Knepley     for (k = 0; k < pdim; k++) {
28689c3cf19fSMatthew G. Knepley       for (l = 0; l < Nc; l++) {
286940cf36b3SToby Isaac         mapped[l] += modes[k] * B[(j * pdim + k) * Nc + l];
28709d150b73SToby Isaac       }
28719d150b73SToby Isaac     }
28729d150b73SToby Isaac   }
287369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,numPoints * pdim * Nc,MPIU_REAL,&B);CHKERRQ(ierr);
287469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,pdim,MPIU_REAL,&modes);CHKERRQ(ierr);
28759d150b73SToby Isaac   ierr = DMPlexVecRestoreClosure(dm, NULL, coords, cell, &coordSize, &nodes);CHKERRQ(ierr);
28769d150b73SToby Isaac   PetscFunctionReturn(0);
28779d150b73SToby Isaac }
28789d150b73SToby Isaac 
2879d6143a4eSToby Isaac /*@
2880d6143a4eSToby Isaac   DMPlexCoordinatesToReference - Pull coordinates back from the mesh to the reference element using a single element
2881d6143a4eSToby Isaac   map.  This inversion will be accurate inside the reference element, but may be inaccurate for mappings that do not
2882d6143a4eSToby Isaac   extend uniquely outside the reference cell (e.g, most non-affine maps)
2883d6143a4eSToby Isaac 
2884d6143a4eSToby Isaac   Not collective
2885d6143a4eSToby Isaac 
2886d6143a4eSToby Isaac   Input Parameters:
2887d6143a4eSToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
2888d6143a4eSToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
2889d6143a4eSToby Isaac                as a multilinear map for tensor-product elements
2890d6143a4eSToby Isaac . cell       - the cell whose map is used.
2891d6143a4eSToby Isaac . numPoints  - the number of points to locate
28921b266c99SBarry Smith - realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
2893d6143a4eSToby Isaac 
2894d6143a4eSToby Isaac   Output Parameters:
2895d6143a4eSToby Isaac . refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
28961b266c99SBarry Smith 
28971b266c99SBarry Smith   Level: intermediate
2898d6143a4eSToby Isaac @*/
2899d6143a4eSToby Isaac PetscErrorCode DMPlexCoordinatesToReference(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal realCoords[], PetscReal refCoords[])
2900d6143a4eSToby Isaac {
29019d150b73SToby Isaac   PetscInt       dimC, dimR, depth, cStart, cEnd, cEndInterior, i;
29029d150b73SToby Isaac   DM             coordDM = NULL;
29039d150b73SToby Isaac   Vec            coords;
29049d150b73SToby Isaac   PetscFE        fe = NULL;
29059d150b73SToby Isaac   PetscErrorCode ierr;
29069d150b73SToby Isaac 
2907d6143a4eSToby Isaac   PetscFunctionBegin;
29089d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29099d150b73SToby Isaac   ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr);
29109d150b73SToby Isaac   ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr);
29119d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
29129d150b73SToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
29139d150b73SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
29149d150b73SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr);
29159d150b73SToby Isaac   if (coordDM) {
29169d150b73SToby Isaac     PetscInt coordFields;
29179d150b73SToby Isaac 
29189d150b73SToby Isaac     ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr);
29199d150b73SToby Isaac     if (coordFields) {
29209d150b73SToby Isaac       PetscClassId id;
29219d150b73SToby Isaac       PetscObject  disc;
29229d150b73SToby Isaac 
29239d150b73SToby Isaac       ierr = DMGetField(coordDM,0,&disc);CHKERRQ(ierr);
29249d150b73SToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
29259d150b73SToby Isaac       if (id == PETSCFE_CLASSID) {
29269d150b73SToby Isaac         fe = (PetscFE) disc;
29279d150b73SToby Isaac       }
29289d150b73SToby Isaac     }
29299d150b73SToby Isaac   }
29309d150b73SToby Isaac   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
29319d150b73SToby Isaac   ierr = DMPlexGetHybridBounds(dm,&cEndInterior,NULL,NULL,NULL);CHKERRQ(ierr);
29329d150b73SToby Isaac   cEnd = cEndInterior > 0 ? cEndInterior : cEnd;
293313903a91SSatish Balay   if (cell < cStart || cell >= cEnd) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"point %D not in cell range [%D,%D)",cell,cStart,cEnd);
29349d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
29359d150b73SToby Isaac     PetscInt  coneSize;
29369d150b73SToby Isaac     PetscBool isSimplex, isTensor;
29379d150b73SToby Isaac 
29389d150b73SToby Isaac     ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
29399d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
29409d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
29419d150b73SToby Isaac     if (isSimplex) {
29429d150b73SToby Isaac       PetscReal detJ, *v0, *J, *invJ;
29439d150b73SToby Isaac 
294469291d52SBarry Smith       ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
29459d150b73SToby Isaac       J    = &v0[dimC];
29469d150b73SToby Isaac       invJ = &J[dimC * dimC];
29479d150b73SToby Isaac       ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, &detJ);CHKERRQ(ierr);
29489d150b73SToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */
29499d150b73SToby Isaac         CoordinatesRealToRef(dimC, dimR, v0, invJ, &realCoords[dimC * i], &refCoords[dimR * i]);
29509d150b73SToby Isaac       }
295169291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
29529d150b73SToby Isaac     } else if (isTensor) {
29539d150b73SToby Isaac       ierr = DMPlexCoordinatesToReference_Tensor(coordDM, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr);
29549d150b73SToby Isaac     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize);
29559d150b73SToby Isaac   } else {
29569d150b73SToby Isaac     ierr = DMPlexCoordinatesToReference_FE(coordDM, fe, cell, numPoints, realCoords, refCoords, coords, dimC, dimR);CHKERRQ(ierr);
29579d150b73SToby Isaac   }
29589d150b73SToby Isaac   PetscFunctionReturn(0);
29599d150b73SToby Isaac }
29609d150b73SToby Isaac 
29619d150b73SToby Isaac /*@
29629d150b73SToby Isaac   DMPlexReferenceToCoordinates - Map references coordinates to coordinates in the the mesh for a single element map.
29639d150b73SToby Isaac 
29649d150b73SToby Isaac   Not collective
29659d150b73SToby Isaac 
29669d150b73SToby Isaac   Input Parameters:
29679d150b73SToby Isaac + dm         - The mesh, with coordinate maps defined either by a PetscDS for the coordinate DM (see DMGetCoordinateDM()) or
29689d150b73SToby Isaac                implicitly by the coordinates of the corner vertices of the cell: as an affine map for simplicial elements, or
29699d150b73SToby Isaac                as a multilinear map for tensor-product elements
29709d150b73SToby Isaac . cell       - the cell whose map is used.
29719d150b73SToby Isaac . numPoints  - the number of points to locate
29729d150b73SToby Isaac + refCoords  - (numPoints x dimension) array of reference coordinates (see DMGetDimension())
29739d150b73SToby Isaac 
29749d150b73SToby Isaac   Output Parameters:
29759d150b73SToby Isaac . realCoords - (numPoints x coordinate dimension) array of coordinates (see DMGetCoordinateDim())
29761b266c99SBarry Smith 
29771b266c99SBarry Smith    Level: intermediate
29789d150b73SToby Isaac @*/
29799d150b73SToby Isaac PetscErrorCode DMPlexReferenceToCoordinates(DM dm, PetscInt cell, PetscInt numPoints, const PetscReal refCoords[], PetscReal realCoords[])
29809d150b73SToby Isaac {
29819d150b73SToby Isaac   PetscInt       dimC, dimR, depth, cStart, cEnd, cEndInterior, i;
29829d150b73SToby Isaac   DM             coordDM = NULL;
29839d150b73SToby Isaac   Vec            coords;
29849d150b73SToby Isaac   PetscFE        fe = NULL;
29859d150b73SToby Isaac   PetscErrorCode ierr;
29869d150b73SToby Isaac 
29879d150b73SToby Isaac   PetscFunctionBegin;
29889d150b73SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29899d150b73SToby Isaac   ierr = DMGetDimension(dm,&dimR);CHKERRQ(ierr);
29909d150b73SToby Isaac   ierr = DMGetCoordinateDim(dm,&dimC);CHKERRQ(ierr);
29919d150b73SToby Isaac   if (dimR <= 0 || dimC <= 0 || numPoints <= 0) PetscFunctionReturn(0);
29929d150b73SToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
29939d150b73SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
29949d150b73SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordDM);CHKERRQ(ierr);
29959d150b73SToby Isaac   if (coordDM) {
29969d150b73SToby Isaac     PetscInt coordFields;
29979d150b73SToby Isaac 
29989d150b73SToby Isaac     ierr = DMGetNumFields(coordDM,&coordFields);CHKERRQ(ierr);
29999d150b73SToby Isaac     if (coordFields) {
30009d150b73SToby Isaac       PetscClassId id;
30019d150b73SToby Isaac       PetscObject  disc;
30029d150b73SToby Isaac 
30039d150b73SToby Isaac       ierr = DMGetField(coordDM,0,&disc);CHKERRQ(ierr);
30049d150b73SToby Isaac       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
30059d150b73SToby Isaac       if (id == PETSCFE_CLASSID) {
30069d150b73SToby Isaac         fe = (PetscFE) disc;
30079d150b73SToby Isaac       }
30089d150b73SToby Isaac     }
30099d150b73SToby Isaac   }
30109d150b73SToby Isaac   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
30119d150b73SToby Isaac   ierr = DMPlexGetHybridBounds(dm,&cEndInterior,NULL,NULL,NULL);CHKERRQ(ierr);
30129d150b73SToby Isaac   cEnd = cEndInterior > 0 ? cEndInterior : cEnd;
301313903a91SSatish Balay   if (cell < cStart || cell >= cEnd) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"point %D not in cell range [%D,%D)",cell,cStart,cEnd);
30149d150b73SToby Isaac   if (!fe) { /* implicit discretization: affine or multilinear */
30159d150b73SToby Isaac     PetscInt  coneSize;
30169d150b73SToby Isaac     PetscBool isSimplex, isTensor;
30179d150b73SToby Isaac 
30189d150b73SToby Isaac     ierr = DMPlexGetConeSize(dm,cell,&coneSize);CHKERRQ(ierr);
30199d150b73SToby Isaac     isSimplex = (coneSize == (dimR + 1)) ? PETSC_TRUE : PETSC_FALSE;
30209d150b73SToby Isaac     isTensor  = (coneSize == ((depth == 1) ? (1 << dimR) : (2 * dimR))) ? PETSC_TRUE : PETSC_FALSE;
30219d150b73SToby Isaac     if (isSimplex) {
30229d150b73SToby Isaac       PetscReal detJ, *v0, *J;
30239d150b73SToby Isaac 
302469291d52SBarry Smith       ierr = DMGetWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
30259d150b73SToby Isaac       J    = &v0[dimC];
30269d150b73SToby Isaac       ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, NULL, &detJ);CHKERRQ(ierr);
30279d150b73SToby Isaac       for (i = 0; i < numPoints; i++) { /* Apply the inverse affine transformation for each point */
30289d150b73SToby Isaac         CoordinatesRefToReal(dimC, dimR, v0, J, &refCoords[dimR * i], &realCoords[dimC * i]);
30299d150b73SToby Isaac       }
303069291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,dimC + 2 * dimC * dimC, MPIU_REAL, &v0);CHKERRQ(ierr);
30319d150b73SToby Isaac     } else if (isTensor) {
30329d150b73SToby Isaac       ierr = DMPlexReferenceToCoordinates_Tensor(coordDM, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr);
30339d150b73SToby Isaac     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unrecognized cone size %D",coneSize);
30349d150b73SToby Isaac   } else {
30359d150b73SToby Isaac     ierr = DMPlexReferenceToCoordinates_FE(coordDM, fe, cell, numPoints, refCoords, realCoords, coords, dimC, dimR);CHKERRQ(ierr);
30369d150b73SToby Isaac   }
3037d6143a4eSToby Isaac   PetscFunctionReturn(0);
3038d6143a4eSToby Isaac }
3039