xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision 2291669eed302bc459cb19ab2daeb3b21ac5cb8f)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2ccd2543fSMatthew G Knepley 
3ccd2543fSMatthew G Knepley #undef __FUNCT__
4ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_Simplex_2D_Internal"
5ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
6ccd2543fSMatthew G Knepley {
7ccd2543fSMatthew G Knepley   const PetscInt embedDim = 2;
8ccd2543fSMatthew G Knepley   PetscReal      x        = PetscRealPart(point[0]);
9ccd2543fSMatthew G Knepley   PetscReal      y        = PetscRealPart(point[1]);
10ccd2543fSMatthew G Knepley   PetscReal      v0[2], J[4], invJ[4], detJ;
11ccd2543fSMatthew G Knepley   PetscReal      xi, eta;
12ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
13ccd2543fSMatthew G Knepley 
14ccd2543fSMatthew G Knepley   PetscFunctionBegin;
158e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
16ccd2543fSMatthew G Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
17ccd2543fSMatthew G Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
18ccd2543fSMatthew G Knepley 
19ccd2543fSMatthew G Knepley   if ((xi >= 0.0) && (eta >= 0.0) && (xi + eta <= 2.0)) *cell = c;
20ccd2543fSMatthew G Knepley   else *cell = -1;
21ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
22ccd2543fSMatthew G Knepley }
23ccd2543fSMatthew G Knepley 
24ccd2543fSMatthew G Knepley #undef __FUNCT__
25ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_General_2D_Internal"
26ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
27ccd2543fSMatthew G Knepley {
28ccd2543fSMatthew G Knepley   PetscSection       coordSection;
29ccd2543fSMatthew G Knepley   Vec             coordsLocal;
30a1e44745SMatthew G. Knepley   PetscScalar    *coords = NULL;
31ccd2543fSMatthew G Knepley   const PetscInt  faces[8]  = {0, 1, 1, 2, 2, 3, 3, 0};
32ccd2543fSMatthew G Knepley   PetscReal       x         = PetscRealPart(point[0]);
33ccd2543fSMatthew G Knepley   PetscReal       y         = PetscRealPart(point[1]);
34ccd2543fSMatthew G Knepley   PetscInt        crossings = 0, f;
35ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
36ccd2543fSMatthew G Knepley 
37ccd2543fSMatthew G Knepley   PetscFunctionBegin;
38ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
3969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
40ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
41ccd2543fSMatthew G Knepley   for (f = 0; f < 4; ++f) {
42ccd2543fSMatthew G Knepley     PetscReal x_i   = PetscRealPart(coords[faces[2*f+0]*2+0]);
43ccd2543fSMatthew G Knepley     PetscReal y_i   = PetscRealPart(coords[faces[2*f+0]*2+1]);
44ccd2543fSMatthew G Knepley     PetscReal x_j   = PetscRealPart(coords[faces[2*f+1]*2+0]);
45ccd2543fSMatthew G Knepley     PetscReal y_j   = PetscRealPart(coords[faces[2*f+1]*2+1]);
46ccd2543fSMatthew G Knepley     PetscReal slope = (y_j - y_i) / (x_j - x_i);
47ccd2543fSMatthew G Knepley     PetscBool cond1 = (x_i <= x) && (x < x_j) ? PETSC_TRUE : PETSC_FALSE;
48ccd2543fSMatthew G Knepley     PetscBool cond2 = (x_j <= x) && (x < x_i) ? PETSC_TRUE : PETSC_FALSE;
49ccd2543fSMatthew G Knepley     PetscBool above = (y < slope * (x - x_i) + y_i) ? PETSC_TRUE : PETSC_FALSE;
50ccd2543fSMatthew G Knepley     if ((cond1 || cond2)  && above) ++crossings;
51ccd2543fSMatthew G Knepley   }
52ccd2543fSMatthew G Knepley   if (crossings % 2) *cell = c;
53ccd2543fSMatthew G Knepley   else *cell = -1;
54ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
55ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
56ccd2543fSMatthew G Knepley }
57ccd2543fSMatthew G Knepley 
58ccd2543fSMatthew G Knepley #undef __FUNCT__
59ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_Simplex_3D_Internal"
60ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
61ccd2543fSMatthew G Knepley {
62ccd2543fSMatthew G Knepley   const PetscInt embedDim = 3;
63ccd2543fSMatthew G Knepley   PetscReal      v0[3], J[9], invJ[9], detJ;
64ccd2543fSMatthew G Knepley   PetscReal      x = PetscRealPart(point[0]);
65ccd2543fSMatthew G Knepley   PetscReal      y = PetscRealPart(point[1]);
66ccd2543fSMatthew G Knepley   PetscReal      z = PetscRealPart(point[2]);
67ccd2543fSMatthew G Knepley   PetscReal      xi, eta, zeta;
68ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
69ccd2543fSMatthew G Knepley 
70ccd2543fSMatthew G Knepley   PetscFunctionBegin;
718e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
72ccd2543fSMatthew G Knepley   xi   = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]) + invJ[0*embedDim+2]*(z - v0[2]);
73ccd2543fSMatthew G Knepley   eta  = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]) + invJ[1*embedDim+2]*(z - v0[2]);
74ccd2543fSMatthew G Knepley   zeta = invJ[2*embedDim+0]*(x - v0[0]) + invJ[2*embedDim+1]*(y - v0[1]) + invJ[2*embedDim+2]*(z - v0[2]);
75ccd2543fSMatthew G Knepley 
76ccd2543fSMatthew G Knepley   if ((xi >= 0.0) && (eta >= 0.0) && (zeta >= 0.0) && (xi + eta + zeta <= 2.0)) *cell = c;
77ccd2543fSMatthew G Knepley   else *cell = -1;
78ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
79ccd2543fSMatthew G Knepley }
80ccd2543fSMatthew G Knepley 
81ccd2543fSMatthew G Knepley #undef __FUNCT__
82ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_General_3D_Internal"
83ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
84ccd2543fSMatthew G Knepley {
85ccd2543fSMatthew G Knepley   PetscSection   coordSection;
86ccd2543fSMatthew G Knepley   Vec            coordsLocal;
877c1f9639SMatthew G Knepley   PetscScalar   *coords;
88fb150da6SMatthew G. Knepley   const PetscInt faces[24] = {0, 3, 2, 1,  5, 4, 7, 6,  3, 0, 4, 5,
89fb150da6SMatthew G. Knepley                               1, 2, 6, 7,  3, 5, 6, 2,  0, 1, 7, 4};
90ccd2543fSMatthew G Knepley   PetscBool      found = PETSC_TRUE;
91ccd2543fSMatthew G Knepley   PetscInt       f;
92ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
93ccd2543fSMatthew G Knepley 
94ccd2543fSMatthew G Knepley   PetscFunctionBegin;
95ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
9669d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
97ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
98ccd2543fSMatthew G Knepley   for (f = 0; f < 6; ++f) {
99ccd2543fSMatthew G Knepley     /* Check the point is under plane */
100ccd2543fSMatthew G Knepley     /*   Get face normal */
101ccd2543fSMatthew G Knepley     PetscReal v_i[3];
102ccd2543fSMatthew G Knepley     PetscReal v_j[3];
103ccd2543fSMatthew G Knepley     PetscReal normal[3];
104ccd2543fSMatthew G Knepley     PetscReal pp[3];
105ccd2543fSMatthew G Knepley     PetscReal dot;
106ccd2543fSMatthew G Knepley 
107ccd2543fSMatthew G Knepley     v_i[0]    = PetscRealPart(coords[faces[f*4+3]*3+0]-coords[faces[f*4+0]*3+0]);
108ccd2543fSMatthew G Knepley     v_i[1]    = PetscRealPart(coords[faces[f*4+3]*3+1]-coords[faces[f*4+0]*3+1]);
109ccd2543fSMatthew G Knepley     v_i[2]    = PetscRealPart(coords[faces[f*4+3]*3+2]-coords[faces[f*4+0]*3+2]);
110ccd2543fSMatthew G Knepley     v_j[0]    = PetscRealPart(coords[faces[f*4+1]*3+0]-coords[faces[f*4+0]*3+0]);
111ccd2543fSMatthew G Knepley     v_j[1]    = PetscRealPart(coords[faces[f*4+1]*3+1]-coords[faces[f*4+0]*3+1]);
112ccd2543fSMatthew G Knepley     v_j[2]    = PetscRealPart(coords[faces[f*4+1]*3+2]-coords[faces[f*4+0]*3+2]);
113ccd2543fSMatthew G Knepley     normal[0] = v_i[1]*v_j[2] - v_i[2]*v_j[1];
114ccd2543fSMatthew G Knepley     normal[1] = v_i[2]*v_j[0] - v_i[0]*v_j[2];
115ccd2543fSMatthew G Knepley     normal[2] = v_i[0]*v_j[1] - v_i[1]*v_j[0];
116ccd2543fSMatthew G Knepley     pp[0]     = PetscRealPart(coords[faces[f*4+0]*3+0] - point[0]);
117ccd2543fSMatthew G Knepley     pp[1]     = PetscRealPart(coords[faces[f*4+0]*3+1] - point[1]);
118ccd2543fSMatthew G Knepley     pp[2]     = PetscRealPart(coords[faces[f*4+0]*3+2] - point[2]);
119ccd2543fSMatthew G Knepley     dot       = normal[0]*pp[0] + normal[1]*pp[1] + normal[2]*pp[2];
120ccd2543fSMatthew G Knepley 
121ccd2543fSMatthew G Knepley     /* Check that projected point is in face (2D location problem) */
122ccd2543fSMatthew G Knepley     if (dot < 0.0) {
123ccd2543fSMatthew G Knepley       found = PETSC_FALSE;
124ccd2543fSMatthew G Knepley       break;
125ccd2543fSMatthew G Knepley     }
126ccd2543fSMatthew G Knepley   }
127ccd2543fSMatthew G Knepley   if (found) *cell = c;
128ccd2543fSMatthew G Knepley   else *cell = -1;
129ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
130ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
131ccd2543fSMatthew G Knepley }
132ccd2543fSMatthew G Knepley 
133ccd2543fSMatthew G Knepley #undef __FUNCT__
134c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashInitialize_Internal"
135c4eade1cSMatthew G. Knepley static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[])
136c4eade1cSMatthew G. Knepley {
137c4eade1cSMatthew G. Knepley   PetscInt d;
138c4eade1cSMatthew G. Knepley 
139c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
140c4eade1cSMatthew G. Knepley   box->dim = dim;
141c4eade1cSMatthew G. Knepley   for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]);
142c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
143c4eade1cSMatthew G. Knepley }
144c4eade1cSMatthew G. Knepley 
145c4eade1cSMatthew G. Knepley #undef __FUNCT__
146c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashCreate"
147c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box)
148c4eade1cSMatthew G. Knepley {
149c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
150c4eade1cSMatthew G. Knepley 
151c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
152c4eade1cSMatthew G. Knepley   ierr = PetscMalloc1(1, box);CHKERRQ(ierr);
153c4eade1cSMatthew G. Knepley   ierr = PetscGridHashInitialize_Internal(*box, dim, point);CHKERRQ(ierr);
154c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
155c4eade1cSMatthew G. Knepley }
156c4eade1cSMatthew G. Knepley 
157c4eade1cSMatthew G. Knepley #undef __FUNCT__
158c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashEnlarge"
159c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[])
160c4eade1cSMatthew G. Knepley {
161c4eade1cSMatthew G. Knepley   PetscInt d;
162c4eade1cSMatthew G. Knepley 
163c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
164c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
165c4eade1cSMatthew G. Knepley     box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d]));
166c4eade1cSMatthew G. Knepley     box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d]));
167c4eade1cSMatthew G. Knepley   }
168c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
169c4eade1cSMatthew G. Knepley }
170c4eade1cSMatthew G. Knepley 
171c4eade1cSMatthew G. Knepley #undef __FUNCT__
172c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashSetGrid"
173c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[])
174c4eade1cSMatthew G. Knepley {
175c4eade1cSMatthew G. Knepley   PetscInt d;
176c4eade1cSMatthew G. Knepley 
177c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
178c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
179c4eade1cSMatthew G. Knepley     box->extent[d] = box->upper[d] - box->lower[d];
180c4eade1cSMatthew G. Knepley     if (n[d] == PETSC_DETERMINE) {
181c4eade1cSMatthew G. Knepley       box->h[d] = h[d];
182c4eade1cSMatthew G. Knepley       box->n[d] = PetscCeilReal(box->extent[d]/h[d]);
183c4eade1cSMatthew G. Knepley     } else {
184c4eade1cSMatthew G. Knepley       box->n[d] = n[d];
185c4eade1cSMatthew G. Knepley       box->h[d] = box->extent[d]/n[d];
186c4eade1cSMatthew G. Knepley     }
187c4eade1cSMatthew G. Knepley   }
188c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
189c4eade1cSMatthew G. Knepley }
190c4eade1cSMatthew G. Knepley 
191c4eade1cSMatthew G. Knepley #undef __FUNCT__
192c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashGetEnclosingBox"
1931c6dfc3eSMatthew G. Knepley PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[])
194c4eade1cSMatthew G. Knepley {
195c4eade1cSMatthew G. Knepley   const PetscReal *lower = box->lower;
196c4eade1cSMatthew G. Knepley   const PetscReal *upper = box->upper;
197c4eade1cSMatthew G. Knepley   const PetscReal *h     = box->h;
198c4eade1cSMatthew G. Knepley   const PetscInt  *n     = box->n;
199c4eade1cSMatthew G. Knepley   const PetscInt   dim   = box->dim;
200c4eade1cSMatthew G. Knepley   PetscInt         d, p;
201c4eade1cSMatthew G. Knepley 
202c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
203c4eade1cSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
204c4eade1cSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
2051c6dfc3eSMatthew G. Knepley       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]);
206c4eade1cSMatthew G. Knepley 
2071c6dfc3eSMatthew G. Knepley       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1;
208c4eade1cSMatthew 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",
2091c6dfc3eSMatthew 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);
210c4eade1cSMatthew G. Knepley       dboxes[p*dim+d] = dbox;
211c4eade1cSMatthew G. Knepley     }
212c4eade1cSMatthew G. Knepley     if (boxes) for (d = 1, boxes[p] = dboxes[p*dim]; d < dim; ++d) boxes[p] += dboxes[p*dim+d]*n[d-1];
213c4eade1cSMatthew G. Knepley   }
214c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
215c4eade1cSMatthew G. Knepley }
216c4eade1cSMatthew G. Knepley 
217c4eade1cSMatthew G. Knepley #undef __FUNCT__
218c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashDestroy"
219c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashDestroy(PetscGridHash *box)
220c4eade1cSMatthew G. Knepley {
221c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
222c4eade1cSMatthew G. Knepley 
223c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
224c4eade1cSMatthew G. Knepley   if (*box) {
225c4eade1cSMatthew G. Knepley     ierr = PetscSectionDestroy(&(*box)->cellSection);CHKERRQ(ierr);
226c4eade1cSMatthew G. Knepley     ierr = ISDestroy(&(*box)->cells);CHKERRQ(ierr);
227c4eade1cSMatthew G. Knepley     ierr = DMLabelDestroy(&(*box)->cellsSparse);CHKERRQ(ierr);
228c4eade1cSMatthew G. Knepley   }
229c4eade1cSMatthew G. Knepley   ierr = PetscFree(*box);CHKERRQ(ierr);
230c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
231c4eade1cSMatthew G. Knepley }
232c4eade1cSMatthew G. Knepley 
233cafe43deSMatthew G. Knepley #undef __FUNCT__
234cafe43deSMatthew G. Knepley #define __FUNCT__ "DMPlexLocatePoint_Internal"
235cafe43deSMatthew G. Knepley PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell)
236cafe43deSMatthew G. Knepley {
237cafe43deSMatthew G. Knepley   PetscInt       coneSize;
238cafe43deSMatthew G. Knepley   PetscErrorCode ierr;
239cafe43deSMatthew G. Knepley 
240cafe43deSMatthew G. Knepley   PetscFunctionBegin;
241cafe43deSMatthew G. Knepley   switch (dim) {
242cafe43deSMatthew G. Knepley   case 2:
243cafe43deSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr);
244cafe43deSMatthew G. Knepley     switch (coneSize) {
245cafe43deSMatthew G. Knepley     case 3:
246cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
247cafe43deSMatthew G. Knepley       break;
248cafe43deSMatthew G. Knepley     case 4:
249cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_General_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
250cafe43deSMatthew G. Knepley       break;
251cafe43deSMatthew G. Knepley     default:
252cafe43deSMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize);
253cafe43deSMatthew G. Knepley     }
254cafe43deSMatthew G. Knepley     break;
255cafe43deSMatthew G. Knepley   case 3:
256cafe43deSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr);
257cafe43deSMatthew G. Knepley     switch (coneSize) {
258cafe43deSMatthew G. Knepley     case 4:
259cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
260cafe43deSMatthew G. Knepley       break;
261cafe43deSMatthew G. Knepley     case 6:
262cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
263cafe43deSMatthew G. Knepley       break;
264cafe43deSMatthew G. Knepley     default:
265cafe43deSMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize);
266cafe43deSMatthew G. Knepley     }
267cafe43deSMatthew G. Knepley     break;
268cafe43deSMatthew G. Knepley   default:
269cafe43deSMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for mesh dimension %D", dim);
270cafe43deSMatthew G. Knepley   }
271cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
272cafe43deSMatthew G. Knepley }
273cafe43deSMatthew G. Knepley 
274cafe43deSMatthew G. Knepley #undef __FUNCT__
275cafe43deSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGridHash_Internal"
276cafe43deSMatthew G. Knepley PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox)
277cafe43deSMatthew G. Knepley {
278cafe43deSMatthew G. Knepley   MPI_Comm           comm;
279cafe43deSMatthew G. Knepley   PetscGridHash      lbox;
280cafe43deSMatthew G. Knepley   Vec                coordinates;
281cafe43deSMatthew G. Knepley   PetscSection       coordSection;
282cafe43deSMatthew G. Knepley   Vec                coordsLocal;
283cafe43deSMatthew G. Knepley   const PetscScalar *coords;
284cafe43deSMatthew G. Knepley   PetscInt          *dboxes;
285cafe43deSMatthew G. Knepley   PetscInt           n[3] = {10, 10, 10};
286cafe43deSMatthew G. Knepley   PetscInt           dim, N, cStart, cEnd, c, i;
287cafe43deSMatthew G. Knepley   PetscErrorCode     ierr;
288cafe43deSMatthew G. Knepley 
289cafe43deSMatthew G. Knepley   PetscFunctionBegin;
290cafe43deSMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
291cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
292cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
293cafe43deSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
294cafe43deSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
295cafe43deSMatthew G. Knepley   ierr = PetscGridHashCreate(comm, dim, coords, &lbox);CHKERRQ(ierr);
296cafe43deSMatthew G. Knepley   for (i = 0; i < N; i += dim) {ierr = PetscGridHashEnlarge(lbox, &coords[i]);CHKERRQ(ierr);}
297cafe43deSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
298cafe43deSMatthew G. Knepley   ierr = PetscGridHashSetGrid(lbox, n, NULL);CHKERRQ(ierr);
299cafe43deSMatthew G. Knepley #if 0
300cafe43deSMatthew G. Knepley   /* Could define a custom reduction to merge these */
301cafe43deSMatthew G. Knepley   ierr = MPI_Allreduce(lbox->lower, gbox->lower, 3, MPIU_REAL, MPI_MIN, comm);CHKERRQ(ierr);
302cafe43deSMatthew G. Knepley   ierr = MPI_Allreduce(lbox->upper, gbox->upper, 3, MPIU_REAL, MPI_MAX, comm);CHKERRQ(ierr);
303cafe43deSMatthew G. Knepley #endif
304cafe43deSMatthew G. Knepley   /* Is there a reason to snap the local bounding box to a division of the global box? */
305cafe43deSMatthew G. Knepley   /* Should we compute all overlaps of local boxes? We could do this with a rendevouz scheme partitioning the global box */
306cafe43deSMatthew G. Knepley   /* Create label */
307cafe43deSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
308cafe43deSMatthew G. Knepley   ierr = DMLabelCreate("cells", &lbox->cellsSparse);CHKERRQ(ierr);
309cafe43deSMatthew G. Knepley   ierr = DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd);CHKERRQ(ierr);
310cafe43deSMatthew G. Knepley   /* Compute boxes which overlap each cell */
311cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
312cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
313cafe43deSMatthew G. Knepley   ierr = PetscCalloc1((dim+1) * dim, &dboxes);CHKERRQ(ierr);
314cafe43deSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
315cafe43deSMatthew G. Knepley     const PetscReal *h       = lbox->h;
316cafe43deSMatthew G. Knepley     PetscScalar     *ccoords = NULL;
317cafe43deSMatthew G. Knepley     PetscScalar      point[3];
318cafe43deSMatthew G. Knepley     PetscInt         dlim[6], d, e, i, j, k;
319cafe43deSMatthew G. Knepley 
320cafe43deSMatthew G. Knepley     /* Find boxes enclosing each vertex */
321cafe43deSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords);CHKERRQ(ierr);
322cafe43deSMatthew G. Knepley     ierr = PetscGridHashGetEnclosingBox(lbox, dim+1, ccoords, dboxes, NULL);CHKERRQ(ierr);
323cafe43deSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords);CHKERRQ(ierr);
324cafe43deSMatthew G. Knepley     /* Get grid of boxes containing these */
325cafe43deSMatthew G. Knepley     for (d = 0;   d < dim; ++d) {dlim[d*2+0] = dlim[d*2+1] = dboxes[d];}
326*2291669eSMatthew G. Knepley     for (d = dim; d < 3;   ++d) {dlim[d*2+0] = dlim[d*2+1] = 0;}
327cafe43deSMatthew G. Knepley     for (e = 1; e < dim+1; ++e) {
328cafe43deSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
329cafe43deSMatthew G. Knepley         dlim[d*2+0] = PetscMin(dlim[d*2+0], dboxes[e*dim+d]);
330cafe43deSMatthew G. Knepley         dlim[d*2+1] = PetscMax(dlim[d*2+1], dboxes[e*dim+d]);
331cafe43deSMatthew G. Knepley       }
332cafe43deSMatthew G. Knepley     }
333cafe43deSMatthew G. Knepley     /* Check whether cell contains any vertex of these subboxes TODO vectorize this */
334cafe43deSMatthew 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]) {
335cafe43deSMatthew 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]) {
336cafe43deSMatthew 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]) {
337cafe43deSMatthew G. Knepley           const PetscInt box = (k*lbox->n[1] + j)*lbox->n[0] + i;
338cafe43deSMatthew G. Knepley           PetscScalar    cpoint[3];
339cafe43deSMatthew G. Knepley           PetscInt       cell, ii, jj, kk;
340cafe43deSMatthew G. Knepley 
341cafe43deSMatthew G. Knepley           for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) {
342cafe43deSMatthew G. Knepley             for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) {
343cafe43deSMatthew G. Knepley               for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) {
344cafe43deSMatthew G. Knepley 
345cafe43deSMatthew G. Knepley                 ierr = DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell);CHKERRQ(ierr);
346cafe43deSMatthew G. Knepley                 if (cell >= 0) {DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); ii = jj = kk = 2;}
347cafe43deSMatthew G. Knepley               }
348cafe43deSMatthew G. Knepley             }
349cafe43deSMatthew G. Knepley           }
350cafe43deSMatthew G. Knepley         }
351cafe43deSMatthew G. Knepley       }
352cafe43deSMatthew G. Knepley     }
353cafe43deSMatthew G. Knepley   }
354cafe43deSMatthew G. Knepley   ierr = PetscFree(dboxes);CHKERRQ(ierr);
355cafe43deSMatthew G. Knepley   ierr = DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells);CHKERRQ(ierr);
356cafe43deSMatthew G. Knepley   ierr = DMLabelDestroy(&lbox->cellsSparse);CHKERRQ(ierr);
357cafe43deSMatthew G. Knepley   *localBox = lbox;
358cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
359cafe43deSMatthew G. Knepley }
360cafe43deSMatthew G. Knepley 
361cafe43deSMatthew G. Knepley #undef __FUNCT__
362ccd2543fSMatthew G Knepley #define __FUNCT__ "DMLocatePoints_Plex"
363ccd2543fSMatthew G Knepley /*
364ccd2543fSMatthew G Knepley  Need to implement using the guess
365ccd2543fSMatthew G Knepley */
366ccd2543fSMatthew G Knepley PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, IS *cellIS)
367ccd2543fSMatthew G Knepley {
368cafe43deSMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
369ccd2543fSMatthew G Knepley   PetscInt        bs, numPoints, p;
3701318edbeSMatthew G. Knepley   PetscInt        dim, cStart, cEnd, cMax, numCells, c;
371cafe43deSMatthew G. Knepley   const PetscInt *boxCells;
372ccd2543fSMatthew G Knepley   PetscInt       *cells;
373ccd2543fSMatthew G Knepley   PetscScalar    *a;
374ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
375ccd2543fSMatthew G Knepley 
376ccd2543fSMatthew G Knepley   PetscFunctionBegin;
377cafe43deSMatthew G. Knepley   if (!mesh->lbox) {ierr = DMPlexComputeGridHash_Internal(dm, &mesh->lbox);CHKERRQ(ierr);}
378cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
379cafe43deSMatthew G. Knepley   ierr = VecGetBlockSize(v, &bs);CHKERRQ(ierr);
380cafe43deSMatthew 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);
381ccd2543fSMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
382ccd2543fSMatthew G Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
383ccd2543fSMatthew G Knepley   if (cMax >= 0) cEnd = PetscMin(cEnd, cMax);
384ccd2543fSMatthew G Knepley   ierr = VecGetLocalSize(v, &numPoints);CHKERRQ(ierr);
385ccd2543fSMatthew G Knepley   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
386ccd2543fSMatthew G Knepley   numPoints /= bs;
387785e854fSJed Brown   ierr       = PetscMalloc1(numPoints, &cells);CHKERRQ(ierr);
388cafe43deSMatthew G. Knepley   /* Designate the local box for each point */
389cafe43deSMatthew G. Knepley   /* Send points to correct process */
390cafe43deSMatthew G. Knepley   /* Search cells that lie in each subbox */
391cafe43deSMatthew G. Knepley   /*   Should we bin points before doing search? */
392cafe43deSMatthew G. Knepley   ierr = ISGetIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);
393ccd2543fSMatthew G Knepley   for (p = 0; p < numPoints; ++p) {
394ccd2543fSMatthew G Knepley     const PetscScalar *point = &a[p*bs];
3951318edbeSMatthew G. Knepley     PetscInt           dbin[3], bin, cell, cellOffset;
396ccd2543fSMatthew G Knepley 
397cafe43deSMatthew G. Knepley     ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr);
398cafe43deSMatthew G. Knepley     /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */
399cafe43deSMatthew G. Knepley     ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
400cafe43deSMatthew G. Knepley     ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
401cafe43deSMatthew G. Knepley     for (c = cellOffset; c < cellOffset + numCells; ++c) {
402cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell);CHKERRQ(ierr);
403ccd2543fSMatthew G Knepley       if (cell >= 0) break;
404ccd2543fSMatthew G Knepley     }
405ccd2543fSMatthew G Knepley     cells[p] = cell;
406ccd2543fSMatthew G Knepley   }
407cafe43deSMatthew G. Knepley   ierr = ISRestoreIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);
408cafe43deSMatthew G. Knepley   /* Check for highest numbered proc that claims a point (do we care?) */
409ccd2543fSMatthew G Knepley   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
410ccd2543fSMatthew G Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints, cells, PETSC_OWN_POINTER, cellIS);CHKERRQ(ierr);
411ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
412ccd2543fSMatthew G Knepley }
413ccd2543fSMatthew G Knepley 
414ccd2543fSMatthew G Knepley #undef __FUNCT__
41517fe8556SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection2Dto1D_Internal"
41617fe8556SMatthew G. Knepley /*
41717fe8556SMatthew G. Knepley   DMPlexComputeProjection2Dto1D_Internal - Rewrite coordinates to be the 1D projection of the 2D
41817fe8556SMatthew G. Knepley */
4193beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection2Dto1D_Internal(PetscScalar coords[], PetscReal R[])
42017fe8556SMatthew G. Knepley {
42117fe8556SMatthew G. Knepley   const PetscReal x = PetscRealPart(coords[2] - coords[0]);
42217fe8556SMatthew G. Knepley   const PetscReal y = PetscRealPart(coords[3] - coords[1]);
4238b49ba18SBarry Smith   const PetscReal r = PetscSqrtReal(x*x + y*y), c = x/r, s = y/r;
42417fe8556SMatthew G. Knepley 
42517fe8556SMatthew G. Knepley   PetscFunctionBegin;
4261c99cf0cSGeoffrey Irving   R[0] = c; R[1] = -s;
4271c99cf0cSGeoffrey Irving   R[2] = s; R[3] =  c;
42817fe8556SMatthew G. Knepley   coords[0] = 0.0;
4297f07f362SMatthew G. Knepley   coords[1] = r;
43017fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
43117fe8556SMatthew G. Knepley }
43217fe8556SMatthew G. Knepley 
43317fe8556SMatthew G. Knepley #undef __FUNCT__
43428dbe442SToby Isaac #define __FUNCT__ "DMPlexComputeProjection3Dto1D_Internal"
43528dbe442SToby Isaac /*
43628dbe442SToby Isaac   DMPlexComputeProjection3Dto1D_Internal - Rewrite coordinates to be the 1D projection of the 3D
43728dbe442SToby Isaac 
43828dbe442SToby Isaac   This uses the basis completion described by Frisvad,
43928dbe442SToby Isaac 
44028dbe442SToby Isaac   http://www.imm.dtu.dk/~jerf/papers/abstracts/onb.html
44128dbe442SToby Isaac   DOI:10.1080/2165347X.2012.689606
44228dbe442SToby Isaac */
4433beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto1D_Internal(PetscScalar coords[], PetscReal R[])
44428dbe442SToby Isaac {
44528dbe442SToby Isaac   PetscReal      x    = PetscRealPart(coords[3] - coords[0]);
44628dbe442SToby Isaac   PetscReal      y    = PetscRealPart(coords[4] - coords[1]);
44728dbe442SToby Isaac   PetscReal      z    = PetscRealPart(coords[5] - coords[2]);
44828dbe442SToby Isaac   PetscReal      r    = PetscSqrtReal(x*x + y*y + z*z);
44928dbe442SToby Isaac   PetscReal      rinv = 1. / r;
45028dbe442SToby Isaac   PetscFunctionBegin;
45128dbe442SToby Isaac 
45228dbe442SToby Isaac   x *= rinv; y *= rinv; z *= rinv;
45328dbe442SToby Isaac   if (x > 0.) {
45428dbe442SToby Isaac     PetscReal inv1pX   = 1./ (1. + x);
45528dbe442SToby Isaac 
45628dbe442SToby Isaac     R[0] = x; R[1] = -y;              R[2] = -z;
45728dbe442SToby Isaac     R[3] = y; R[4] = 1. - y*y*inv1pX; R[5] =     -y*z*inv1pX;
45828dbe442SToby Isaac     R[6] = z; R[7] =     -y*z*inv1pX; R[8] = 1. - z*z*inv1pX;
45928dbe442SToby Isaac   }
46028dbe442SToby Isaac   else {
46128dbe442SToby Isaac     PetscReal inv1mX   = 1./ (1. - x);
46228dbe442SToby Isaac 
46328dbe442SToby Isaac     R[0] = x; R[1] = z;               R[2] = y;
46428dbe442SToby Isaac     R[3] = y; R[4] =     -y*z*inv1mX; R[5] = 1. - y*y*inv1mX;
46528dbe442SToby Isaac     R[6] = z; R[7] = 1. - z*z*inv1mX; R[8] =     -y*z*inv1mX;
46628dbe442SToby Isaac   }
46728dbe442SToby Isaac   coords[0] = 0.0;
46828dbe442SToby Isaac   coords[1] = r;
46928dbe442SToby Isaac   PetscFunctionReturn(0);
47028dbe442SToby Isaac }
47128dbe442SToby Isaac 
47228dbe442SToby Isaac #undef __FUNCT__
473ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeProjection3Dto2D_Internal"
474ccd2543fSMatthew G Knepley /*
475ccd2543fSMatthew G Knepley   DMPlexComputeProjection3Dto2D_Internal - Rewrite coordinates to be the 2D projection of the 3D
476ccd2543fSMatthew G Knepley */
4773beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto2D_Internal(PetscInt coordSize, PetscScalar coords[], PetscReal R[])
478ccd2543fSMatthew G Knepley {
4791ee9d5ecSMatthew G. Knepley   PetscReal      x1[3],  x2[3], n[3], norm;
48099dec3a6SMatthew G. Knepley   PetscReal      x1p[3], x2p[3], xnp[3];
4814a217a95SMatthew G. Knepley   PetscReal      sqrtz, alpha;
482ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
48399dec3a6SMatthew G. Knepley   PetscInt       d, e, p;
484ccd2543fSMatthew G Knepley 
485ccd2543fSMatthew G Knepley   PetscFunctionBegin;
486ccd2543fSMatthew G Knepley   /* 0) Calculate normal vector */
487ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
4881ee9d5ecSMatthew G. Knepley     x1[d] = PetscRealPart(coords[1*dim+d] - coords[0*dim+d]);
4891ee9d5ecSMatthew G. Knepley     x2[d] = PetscRealPart(coords[2*dim+d] - coords[0*dim+d]);
490ccd2543fSMatthew G Knepley   }
491ccd2543fSMatthew G Knepley   n[0] = x1[1]*x2[2] - x1[2]*x2[1];
492ccd2543fSMatthew G Knepley   n[1] = x1[2]*x2[0] - x1[0]*x2[2];
493ccd2543fSMatthew G Knepley   n[2] = x1[0]*x2[1] - x1[1]*x2[0];
4948b49ba18SBarry Smith   norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
495ccd2543fSMatthew G Knepley   n[0] /= norm;
496ccd2543fSMatthew G Knepley   n[1] /= norm;
497ccd2543fSMatthew G Knepley   n[2] /= norm;
498ccd2543fSMatthew G Knepley   /* 1) Take the normal vector and rotate until it is \hat z
499ccd2543fSMatthew G Knepley 
500ccd2543fSMatthew G Knepley     Let the normal vector be <nx, ny, nz> and alpha = 1/sqrt(1 - nz^2), then
501ccd2543fSMatthew G Knepley 
502ccd2543fSMatthew G Knepley     R = /  alpha nx nz  alpha ny nz -1/alpha \
503ccd2543fSMatthew G Knepley         | -alpha ny     alpha nx        0    |
504ccd2543fSMatthew G Knepley         \     nx            ny         nz    /
505ccd2543fSMatthew G Knepley 
506ccd2543fSMatthew G Knepley     will rotate the normal vector to \hat z
507ccd2543fSMatthew G Knepley   */
5088b49ba18SBarry Smith   sqrtz = PetscSqrtReal(1.0 - n[2]*n[2]);
50973868372SMatthew G. Knepley   /* Check for n = z */
51073868372SMatthew G. Knepley   if (sqrtz < 1.0e-10) {
5111ee9d5ecSMatthew G. Knepley     if (n[2] < 0.0) {
51299dec3a6SMatthew G. Knepley       if (coordSize > 9) {
51399dec3a6SMatthew G. Knepley         coords[2] = PetscRealPart(coords[3*dim+0] - coords[0*dim+0]);
51408b58242SMatthew G. Knepley         coords[3] = PetscRealPart(coords[3*dim+1] - coords[0*dim+1]);
51599dec3a6SMatthew G. Knepley         coords[4] = x2[0];
51699dec3a6SMatthew G. Knepley         coords[5] = x2[1];
51799dec3a6SMatthew G. Knepley         coords[6] = x1[0];
51899dec3a6SMatthew G. Knepley         coords[7] = x1[1];
51999dec3a6SMatthew G. Knepley       } else {
52073868372SMatthew G. Knepley         coords[2] = x2[0];
52173868372SMatthew G. Knepley         coords[3] = x2[1];
52273868372SMatthew G. Knepley         coords[4] = x1[0];
52373868372SMatthew G. Knepley         coords[5] = x1[1];
52499dec3a6SMatthew G. Knepley       }
525b7ad821dSMatthew G. Knepley       R[0] = 1.0; R[1] = 0.0; R[2] = 0.0;
526b7ad821dSMatthew G. Knepley       R[3] = 0.0; R[4] = 1.0; R[5] = 0.0;
527b7ad821dSMatthew G. Knepley       R[6] = 0.0; R[7] = 0.0; R[8] = -1.0;
52873868372SMatthew G. Knepley     } else {
52999dec3a6SMatthew G. Knepley       for (p = 3; p < coordSize/3; ++p) {
53099dec3a6SMatthew G. Knepley         coords[p*2+0] = PetscRealPart(coords[p*dim+0] - coords[0*dim+0]);
53199dec3a6SMatthew G. Knepley         coords[p*2+1] = PetscRealPart(coords[p*dim+1] - coords[0*dim+1]);
53299dec3a6SMatthew G. Knepley       }
53373868372SMatthew G. Knepley       coords[2] = x1[0];
53473868372SMatthew G. Knepley       coords[3] = x1[1];
53573868372SMatthew G. Knepley       coords[4] = x2[0];
53673868372SMatthew G. Knepley       coords[5] = x2[1];
537b7ad821dSMatthew G. Knepley       R[0] = 1.0; R[1] = 0.0; R[2] = 0.0;
538b7ad821dSMatthew G. Knepley       R[3] = 0.0; R[4] = 1.0; R[5] = 0.0;
539b7ad821dSMatthew G. Knepley       R[6] = 0.0; R[7] = 0.0; R[8] = 1.0;
54073868372SMatthew G. Knepley     }
54199dec3a6SMatthew G. Knepley     coords[0] = 0.0;
54299dec3a6SMatthew G. Knepley     coords[1] = 0.0;
54373868372SMatthew G. Knepley     PetscFunctionReturn(0);
54473868372SMatthew G. Knepley   }
545da18b5e6SMatthew G Knepley   alpha = 1.0/sqrtz;
546ccd2543fSMatthew G Knepley   R[0] =  alpha*n[0]*n[2]; R[1] = alpha*n[1]*n[2]; R[2] = -sqrtz;
547ccd2543fSMatthew G Knepley   R[3] = -alpha*n[1];      R[4] = alpha*n[0];      R[5] = 0.0;
548ccd2543fSMatthew G Knepley   R[6] =  n[0];            R[7] = n[1];            R[8] = n[2];
549ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
550ccd2543fSMatthew G Knepley     x1p[d] = 0.0;
551ccd2543fSMatthew G Knepley     x2p[d] = 0.0;
552ccd2543fSMatthew G Knepley     for (e = 0; e < dim; ++e) {
553ccd2543fSMatthew G Knepley       x1p[d] += R[d*dim+e]*x1[e];
554ccd2543fSMatthew G Knepley       x2p[d] += R[d*dim+e]*x2[e];
555ccd2543fSMatthew G Knepley     }
556ccd2543fSMatthew G Knepley   }
5578763be8eSMatthew G. Knepley   if (PetscAbsReal(x1p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
5588763be8eSMatthew G. Knepley   if (PetscAbsReal(x2p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
559ccd2543fSMatthew G Knepley   /* 2) Project to (x, y) */
56099dec3a6SMatthew G. Knepley   for (p = 3; p < coordSize/3; ++p) {
56199dec3a6SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
56299dec3a6SMatthew G. Knepley       xnp[d] = 0.0;
56399dec3a6SMatthew G. Knepley       for (e = 0; e < dim; ++e) {
56499dec3a6SMatthew G. Knepley         xnp[d] += R[d*dim+e]*PetscRealPart(coords[p*dim+e] - coords[0*dim+e]);
56599dec3a6SMatthew G. Knepley       }
56699dec3a6SMatthew G. Knepley       if (d < dim-1) coords[p*2+d] = xnp[d];
56799dec3a6SMatthew G. Knepley     }
56899dec3a6SMatthew G. Knepley   }
569ccd2543fSMatthew G Knepley   coords[0] = 0.0;
570ccd2543fSMatthew G Knepley   coords[1] = 0.0;
571ccd2543fSMatthew G Knepley   coords[2] = x1p[0];
572ccd2543fSMatthew G Knepley   coords[3] = x1p[1];
573ccd2543fSMatthew G Knepley   coords[4] = x2p[0];
574ccd2543fSMatthew G Knepley   coords[5] = x2p[1];
5757f07f362SMatthew G. Knepley   /* Output R^T which rotates \hat z to the input normal */
5767f07f362SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
5777f07f362SMatthew G. Knepley     for (e = d+1; e < dim; ++e) {
5787f07f362SMatthew G. Knepley       PetscReal tmp;
5797f07f362SMatthew G. Knepley 
5807f07f362SMatthew G. Knepley       tmp        = R[d*dim+e];
5817f07f362SMatthew G. Knepley       R[d*dim+e] = R[e*dim+d];
5827f07f362SMatthew G. Knepley       R[e*dim+d] = tmp;
5837f07f362SMatthew G. Knepley     }
5847f07f362SMatthew G. Knepley   }
585ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
586ccd2543fSMatthew G Knepley }
587ccd2543fSMatthew G Knepley 
588ccd2543fSMatthew G Knepley #undef __FUNCT__
589834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Internal"
5906322fe33SJed Brown PETSC_UNUSED
591834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[])
592834e62ceSMatthew G. Knepley {
593834e62ceSMatthew G. Knepley   /* Signed volume is 1/2 the determinant
594834e62ceSMatthew G. Knepley 
595834e62ceSMatthew G. Knepley    |  1  1  1 |
596834e62ceSMatthew G. Knepley    | x0 x1 x2 |
597834e62ceSMatthew G. Knepley    | y0 y1 y2 |
598834e62ceSMatthew G. Knepley 
599834e62ceSMatthew G. Knepley      but if x0,y0 is the origin, we have
600834e62ceSMatthew G. Knepley 
601834e62ceSMatthew G. Knepley    | x1 x2 |
602834e62ceSMatthew G. Knepley    | y1 y2 |
603834e62ceSMatthew G. Knepley   */
604834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1];
605834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1];
606834e62ceSMatthew G. Knepley   PetscReal       M[4], detM;
607834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2;
60886623015SMatthew G. Knepley   M[2] = y1; M[3] = y2;
609923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(&detM, M);
610834e62ceSMatthew G. Knepley   *vol = 0.5*detM;
611834e62ceSMatthew G. Knepley   PetscLogFlops(5.0);
612834e62ceSMatthew G. Knepley }
613834e62ceSMatthew G. Knepley 
614834e62ceSMatthew G. Knepley #undef __FUNCT__
615834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Origin_Internal"
616834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Origin_Internal(PetscReal *vol, PetscReal coords[])
617834e62ceSMatthew G. Knepley {
618923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(vol, coords);
619834e62ceSMatthew G. Knepley   *vol *= 0.5;
620834e62ceSMatthew G. Knepley }
621834e62ceSMatthew G. Knepley 
622834e62ceSMatthew G. Knepley #undef __FUNCT__
623834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Internal"
6246322fe33SJed Brown PETSC_UNUSED
625834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[])
626834e62ceSMatthew G. Knepley {
627834e62ceSMatthew G. Knepley   /* Signed volume is 1/6th of the determinant
628834e62ceSMatthew G. Knepley 
629834e62ceSMatthew G. Knepley    |  1  1  1  1 |
630834e62ceSMatthew G. Knepley    | x0 x1 x2 x3 |
631834e62ceSMatthew G. Knepley    | y0 y1 y2 y3 |
632834e62ceSMatthew G. Knepley    | z0 z1 z2 z3 |
633834e62ceSMatthew G. Knepley 
634834e62ceSMatthew G. Knepley      but if x0,y0,z0 is the origin, we have
635834e62ceSMatthew G. Knepley 
636834e62ceSMatthew G. Knepley    | x1 x2 x3 |
637834e62ceSMatthew G. Knepley    | y1 y2 y3 |
638834e62ceSMatthew G. Knepley    | z1 z2 z3 |
639834e62ceSMatthew G. Knepley   */
640834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[3] - coords[0], y1 = coords[4]  - coords[1], z1 = coords[5]  - coords[2];
641834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[6] - coords[0], y2 = coords[7]  - coords[1], z2 = coords[8]  - coords[2];
642834e62ceSMatthew G. Knepley   const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2];
643834e62ceSMatthew G. Knepley   PetscReal       M[9], detM;
644834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2; M[2] = x3;
645834e62ceSMatthew G. Knepley   M[3] = y1; M[4] = y2; M[5] = y3;
646834e62ceSMatthew G. Knepley   M[6] = z1; M[7] = z2; M[8] = z3;
647923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(&detM, M);
648b7ad821dSMatthew G. Knepley   *vol = -0.16666666666666666666666*detM;
649834e62ceSMatthew G. Knepley   PetscLogFlops(10.0);
650834e62ceSMatthew G. Knepley }
651834e62ceSMatthew G. Knepley 
652834e62ceSMatthew G. Knepley #undef __FUNCT__
6530ec8681fSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Origin_Internal"
6540ec8681fSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[])
6550ec8681fSMatthew G. Knepley {
656923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(vol, coords);
657b7ad821dSMatthew G. Knepley   *vol *= -0.16666666666666666666666;
6580ec8681fSMatthew G. Knepley }
6590ec8681fSMatthew G. Knepley 
6600ec8681fSMatthew G. Knepley #undef __FUNCT__
66117fe8556SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeLineGeometry_Internal"
66217fe8556SMatthew G. Knepley static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
66317fe8556SMatthew G. Knepley {
66417fe8556SMatthew G. Knepley   PetscSection   coordSection;
66517fe8556SMatthew G. Knepley   Vec            coordinates;
666a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
6677f07f362SMatthew G. Knepley   PetscInt       numCoords, d;
66817fe8556SMatthew G. Knepley   PetscErrorCode ierr;
66917fe8556SMatthew G. Knepley 
67017fe8556SMatthew G. Knepley   PetscFunctionBegin;
67117fe8556SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
67269d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
67317fe8556SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
6747f07f362SMatthew G. Knepley   *detJ = 0.0;
67528dbe442SToby Isaac   if (numCoords == 6) {
67628dbe442SToby Isaac     const PetscInt dim = 3;
67728dbe442SToby Isaac     PetscReal      R[9], J0;
67828dbe442SToby Isaac 
67928dbe442SToby Isaac     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
68028dbe442SToby Isaac     ierr = DMPlexComputeProjection3Dto1D_Internal(coords, R);CHKERRQ(ierr);
68128dbe442SToby Isaac     if (J)    {
68228dbe442SToby Isaac       J0   = 0.5*PetscRealPart(coords[1]);
68328dbe442SToby Isaac       J[0] = R[0]*J0; J[1] = R[1]; J[2] = R[2];
68428dbe442SToby Isaac       J[3] = R[3]*J0; J[4] = R[4]; J[5] = R[5];
68528dbe442SToby Isaac       J[6] = R[6]*J0; J[7] = R[7]; J[8] = R[8];
68628dbe442SToby Isaac       DMPlex_Det3D_Internal(detJ, J);
68728dbe442SToby Isaac     }
68828dbe442SToby Isaac     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
68928dbe442SToby Isaac   } else if (numCoords == 4) {
6907f07f362SMatthew G. Knepley     const PetscInt dim = 2;
6917f07f362SMatthew G. Knepley     PetscReal      R[4], J0;
6927f07f362SMatthew G. Knepley 
6937f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
6947f07f362SMatthew G. Knepley     ierr = DMPlexComputeProjection2Dto1D_Internal(coords, R);CHKERRQ(ierr);
69517fe8556SMatthew G. Knepley     if (J)    {
6967f07f362SMatthew G. Knepley       J0   = 0.5*PetscRealPart(coords[1]);
6977f07f362SMatthew G. Knepley       J[0] = R[0]*J0; J[1] = R[1];
6987f07f362SMatthew G. Knepley       J[2] = R[2]*J0; J[3] = R[3];
699923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
70017fe8556SMatthew G. Knepley     }
701923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
7027f07f362SMatthew G. Knepley   } else if (numCoords == 2) {
7037f07f362SMatthew G. Knepley     const PetscInt dim = 1;
7047f07f362SMatthew G. Knepley 
7057f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
7067f07f362SMatthew G. Knepley     if (J)    {
7077f07f362SMatthew G. Knepley       J[0]  = 0.5*(PetscRealPart(coords[1]) - PetscRealPart(coords[0]));
70817fe8556SMatthew G. Knepley       *detJ = J[0];
70917fe8556SMatthew G. Knepley       PetscLogFlops(2.0);
71017fe8556SMatthew G. Knepley     }
7117f07f362SMatthew G. Knepley     if (invJ) {invJ[0] = 1.0/J[0]; PetscLogFlops(1.0);}
712796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this segment is %D != 2", numCoords);
71317fe8556SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
71417fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
71517fe8556SMatthew G. Knepley }
71617fe8556SMatthew G. Knepley 
71717fe8556SMatthew G. Knepley #undef __FUNCT__
718ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTriangleGeometry_Internal"
719ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
720ccd2543fSMatthew G Knepley {
721ccd2543fSMatthew G Knepley   PetscSection   coordSection;
722ccd2543fSMatthew G Knepley   Vec            coordinates;
723a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
7247f07f362SMatthew G. Knepley   PetscInt       numCoords, d, f, g;
725ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
726ccd2543fSMatthew G Knepley 
727ccd2543fSMatthew G Knepley   PetscFunctionBegin;
728ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
72969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
730ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
7317f07f362SMatthew G. Knepley   *detJ = 0.0;
732ccd2543fSMatthew G Knepley   if (numCoords == 9) {
7337f07f362SMatthew G. Knepley     const PetscInt dim = 3;
7347f07f362SMatthew 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};
7357f07f362SMatthew G. Knepley 
7367f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
73799dec3a6SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D_Internal(numCoords, coords, R);CHKERRQ(ierr);
7387f07f362SMatthew G. Knepley     if (J)    {
739b7ad821dSMatthew G. Knepley       const PetscInt pdim = 2;
740b7ad821dSMatthew G. Knepley 
741b7ad821dSMatthew G. Knepley       for (d = 0; d < pdim; d++) {
742b7ad821dSMatthew G. Knepley         for (f = 0; f < pdim; f++) {
743b7ad821dSMatthew G. Knepley           J0[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
744ccd2543fSMatthew G Knepley         }
7457f07f362SMatthew G. Knepley       }
7467f07f362SMatthew G. Knepley       PetscLogFlops(8.0);
747923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
7487f07f362SMatthew G. Knepley       for (d = 0; d < dim; d++) {
7497f07f362SMatthew G. Knepley         for (f = 0; f < dim; f++) {
7507f07f362SMatthew G. Knepley           J[d*dim+f] = 0.0;
7517f07f362SMatthew G. Knepley           for (g = 0; g < dim; g++) {
7527f07f362SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
7537f07f362SMatthew G. Knepley           }
7547f07f362SMatthew G. Knepley         }
7557f07f362SMatthew G. Knepley       }
7567f07f362SMatthew G. Knepley       PetscLogFlops(18.0);
7577f07f362SMatthew G. Knepley     }
758923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
7597f07f362SMatthew G. Knepley   } else if (numCoords == 6) {
7607f07f362SMatthew G. Knepley     const PetscInt dim = 2;
7617f07f362SMatthew G. Knepley 
7627f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
763ccd2543fSMatthew G Knepley     if (J)    {
764ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
765ccd2543fSMatthew G Knepley         for (f = 0; f < dim; f++) {
766ccd2543fSMatthew G Knepley           J[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*dim+d]) - PetscRealPart(coords[0*dim+d]));
767ccd2543fSMatthew G Knepley         }
768ccd2543fSMatthew G Knepley       }
7697f07f362SMatthew G. Knepley       PetscLogFlops(8.0);
770923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
771ccd2543fSMatthew G Knepley     }
772923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
773796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %D != 6 or 9", numCoords);
774ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
775ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
776ccd2543fSMatthew G Knepley }
777ccd2543fSMatthew G Knepley 
778ccd2543fSMatthew G Knepley #undef __FUNCT__
779ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeRectangleGeometry_Internal"
780ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
781ccd2543fSMatthew G Knepley {
782ccd2543fSMatthew G Knepley   PetscSection   coordSection;
783ccd2543fSMatthew G Knepley   Vec            coordinates;
784a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
78599dec3a6SMatthew G. Knepley   PetscInt       numCoords, d, f, g;
786ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
787ccd2543fSMatthew G Knepley 
788ccd2543fSMatthew G Knepley   PetscFunctionBegin;
789ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
79069d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
79199dec3a6SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
7927f07f362SMatthew G. Knepley   *detJ = 0.0;
79399dec3a6SMatthew G. Knepley   if (numCoords == 12) {
79499dec3a6SMatthew G. Knepley     const PetscInt dim = 3;
79599dec3a6SMatthew 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};
79699dec3a6SMatthew G. Knepley 
79799dec3a6SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
79899dec3a6SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D_Internal(numCoords, coords, R);CHKERRQ(ierr);
79999dec3a6SMatthew G. Knepley     if (J)    {
80099dec3a6SMatthew G. Knepley       const PetscInt pdim = 2;
80199dec3a6SMatthew G. Knepley 
80299dec3a6SMatthew G. Knepley       for (d = 0; d < pdim; d++) {
80399dec3a6SMatthew G. Knepley         J0[d*dim+0] = 0.5*(PetscRealPart(coords[1*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
80499dec3a6SMatthew G. Knepley         J0[d*dim+1] = 0.5*(PetscRealPart(coords[3*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
80599dec3a6SMatthew G. Knepley       }
80699dec3a6SMatthew G. Knepley       PetscLogFlops(8.0);
807923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
80899dec3a6SMatthew G. Knepley       for (d = 0; d < dim; d++) {
80999dec3a6SMatthew G. Knepley         for (f = 0; f < dim; f++) {
81099dec3a6SMatthew G. Knepley           J[d*dim+f] = 0.0;
81199dec3a6SMatthew G. Knepley           for (g = 0; g < dim; g++) {
81299dec3a6SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
81399dec3a6SMatthew G. Knepley           }
81499dec3a6SMatthew G. Knepley         }
81599dec3a6SMatthew G. Knepley       }
81699dec3a6SMatthew G. Knepley       PetscLogFlops(18.0);
81799dec3a6SMatthew G. Knepley     }
818923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
81997f1a218SMatthew G. Knepley   } else if ((numCoords == 8) || (numCoords == 16)) {
82099dec3a6SMatthew G. Knepley     const PetscInt dim = 2;
82199dec3a6SMatthew G. Knepley 
8227f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
823ccd2543fSMatthew G Knepley     if (J)    {
824ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
82599dec3a6SMatthew G. Knepley         J[d*dim+0] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
82699dec3a6SMatthew G. Knepley         J[d*dim+1] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
827ccd2543fSMatthew G Knepley       }
8287f07f362SMatthew G. Knepley       PetscLogFlops(8.0);
829923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
830ccd2543fSMatthew G Knepley     }
831923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
832796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
83399dec3a6SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
834ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
835ccd2543fSMatthew G Knepley }
836ccd2543fSMatthew G Knepley 
837ccd2543fSMatthew G Knepley #undef __FUNCT__
838ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTetrahedronGeometry_Internal"
839ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
840ccd2543fSMatthew G Knepley {
841ccd2543fSMatthew G Knepley   PetscSection   coordSection;
842ccd2543fSMatthew G Knepley   Vec            coordinates;
843a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
844ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
84599dec3a6SMatthew G. Knepley   PetscInt       d;
846ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
847ccd2543fSMatthew G Knepley 
848ccd2543fSMatthew G Knepley   PetscFunctionBegin;
849ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
85069d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
851ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
8527f07f362SMatthew G. Knepley   *detJ = 0.0;
8537f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
854ccd2543fSMatthew G Knepley   if (J)    {
855ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
856f0df753eSMatthew G. Knepley       /* I orient with outward face normals */
857f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d]));
858f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
859f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
860ccd2543fSMatthew G Knepley     }
8617f07f362SMatthew G. Knepley     PetscLogFlops(18.0);
862923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
863ccd2543fSMatthew G Knepley   }
864923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
865ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
866ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
867ccd2543fSMatthew G Knepley }
868ccd2543fSMatthew G Knepley 
869ccd2543fSMatthew G Knepley #undef __FUNCT__
870ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeHexahedronGeometry_Internal"
871ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
872ccd2543fSMatthew G Knepley {
873ccd2543fSMatthew G Knepley   PetscSection   coordSection;
874ccd2543fSMatthew G Knepley   Vec            coordinates;
875a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
876ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
877ccd2543fSMatthew G Knepley   PetscInt       d;
878ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
879ccd2543fSMatthew G Knepley 
880ccd2543fSMatthew G Knepley   PetscFunctionBegin;
881ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
88269d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
883ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
8847f07f362SMatthew G. Knepley   *detJ = 0.0;
8857f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
886ccd2543fSMatthew G Knepley   if (J)    {
887ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
888f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
889f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
890f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d]));
891ccd2543fSMatthew G Knepley     }
8927f07f362SMatthew G. Knepley     PetscLogFlops(18.0);
893923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
894ccd2543fSMatthew G Knepley   }
895923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
896ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
897ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
898ccd2543fSMatthew G Knepley }
899ccd2543fSMatthew G Knepley 
900ccd2543fSMatthew G Knepley #undef __FUNCT__
9018e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryAffineFEM"
902ccd2543fSMatthew G Knepley /*@C
9038e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell
904ccd2543fSMatthew G Knepley 
905ccd2543fSMatthew G Knepley   Collective on DM
906ccd2543fSMatthew G Knepley 
907ccd2543fSMatthew G Knepley   Input Arguments:
908ccd2543fSMatthew G Knepley + dm   - the DM
909ccd2543fSMatthew G Knepley - cell - the cell
910ccd2543fSMatthew G Knepley 
911ccd2543fSMatthew G Knepley   Output Arguments:
912ccd2543fSMatthew G Knepley + v0   - the translation part of this affine transform
913ccd2543fSMatthew G Knepley . J    - the Jacobian of the transform from the reference element
914ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian
915ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant
916ccd2543fSMatthew G Knepley 
917ccd2543fSMatthew G Knepley   Level: advanced
918ccd2543fSMatthew G Knepley 
919ccd2543fSMatthew G Knepley   Fortran Notes:
920ccd2543fSMatthew G Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
921ccd2543fSMatthew G Knepley   include petsc.h90 in your code.
922ccd2543fSMatthew G Knepley 
9238e0841e0SMatthew G. Knepley .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinateVec()
924ccd2543fSMatthew G Knepley @*/
9258e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
926ccd2543fSMatthew G Knepley {
92749dc4407SMatthew G. Knepley   PetscInt       depth, dim, coneSize;
928ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
929ccd2543fSMatthew G Knepley 
930ccd2543fSMatthew G Knepley   PetscFunctionBegin;
931139a35ccSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
932ccd2543fSMatthew G Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
93349dc4407SMatthew G. Knepley   if (depth == 1) {
9348e0841e0SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
9358e0841e0SMatthew G. Knepley   } else {
9368e0841e0SMatthew G. Knepley     DMLabel depth;
9378e0841e0SMatthew G. Knepley 
9388e0841e0SMatthew G. Knepley     ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
9398e0841e0SMatthew G. Knepley     ierr = DMLabelGetValue(depth, cell, &dim);CHKERRQ(ierr);
9408e0841e0SMatthew G. Knepley   }
941ccd2543fSMatthew G Knepley   switch (dim) {
94217fe8556SMatthew G. Knepley   case 1:
94317fe8556SMatthew G. Knepley     ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
94417fe8556SMatthew G. Knepley     break;
945ccd2543fSMatthew G Knepley   case 2:
946ccd2543fSMatthew G Knepley     switch (coneSize) {
947ccd2543fSMatthew G Knepley     case 3:
948ccd2543fSMatthew G Knepley       ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
949ccd2543fSMatthew G Knepley       break;
950ccd2543fSMatthew G Knepley     case 4:
951ccd2543fSMatthew G Knepley       ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
952ccd2543fSMatthew G Knepley       break;
953ccd2543fSMatthew G Knepley     default:
9548e0841e0SMatthew G. Knepley       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
955ccd2543fSMatthew G Knepley     }
956ccd2543fSMatthew G Knepley     break;
957ccd2543fSMatthew G Knepley   case 3:
958ccd2543fSMatthew G Knepley     switch (coneSize) {
959ccd2543fSMatthew G Knepley     case 4:
960ccd2543fSMatthew G Knepley       ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
961ccd2543fSMatthew G Knepley       break;
9628e0841e0SMatthew G. Knepley     case 6: /* Faces */
9638e0841e0SMatthew G. Knepley     case 8: /* Vertices */
964ccd2543fSMatthew G Knepley       ierr = DMPlexComputeHexahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
965ccd2543fSMatthew G Knepley       break;
966ccd2543fSMatthew G Knepley     default:
9678e0841e0SMatthew G. Knepley         SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
968ccd2543fSMatthew G Knepley     }
969ccd2543fSMatthew G Knepley       break;
970ccd2543fSMatthew G Knepley   default:
971ccd2543fSMatthew G Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
972ccd2543fSMatthew G Knepley   }
9738e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
9748e0841e0SMatthew G. Knepley }
9758e0841e0SMatthew G. Knepley 
9768e0841e0SMatthew G. Knepley #undef __FUNCT__
9778e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIsoparametricGeometry_Internal"
9788e0841e0SMatthew G. Knepley static PetscErrorCode DMPlexComputeIsoparametricGeometry_Internal(DM dm, PetscFE fe, PetscInt point, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
9798e0841e0SMatthew G. Knepley {
9808e0841e0SMatthew G. Knepley   PetscQuadrature  quad;
9818e0841e0SMatthew G. Knepley   PetscSection     coordSection;
9828e0841e0SMatthew G. Knepley   Vec              coordinates;
9838e0841e0SMatthew G. Knepley   PetscScalar     *coords = NULL;
9848e0841e0SMatthew G. Knepley   const PetscReal *quadPoints;
9858e0841e0SMatthew G. Knepley   PetscReal       *basisDer;
9868e0841e0SMatthew G. Knepley   PetscInt         dim, cdim, pdim, qdim, Nq, numCoords, d, q;
9878e0841e0SMatthew G. Knepley   PetscErrorCode   ierr;
9888e0841e0SMatthew G. Knepley 
9898e0841e0SMatthew G. Knepley   PetscFunctionBegin;
9908e0841e0SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
9918e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
9928e0841e0SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
9938e0841e0SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
9948e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
9958e0841e0SMatthew G. Knepley   ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
996954b1791SMatthew G. Knepley   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
9978e0841e0SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, &qdim, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
9988e0841e0SMatthew G. Knepley   ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr);
9998e0841e0SMatthew G. Knepley   *detJ = 0.0;
10008e0841e0SMatthew G. Knepley   if (qdim != dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim);
10018e0841e0SMatthew 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);
10028e0841e0SMatthew G. Knepley   if (v0) {for (d = 0; d < cdim; d++) v0[d] = PetscRealPart(coords[d]);}
10038e0841e0SMatthew G. Knepley   if (J) {
10048e0841e0SMatthew G. Knepley     for (q = 0; q < Nq; ++q) {
10058e0841e0SMatthew G. Knepley       PetscInt i, j, k, c, r;
10068e0841e0SMatthew G. Knepley 
10078e0841e0SMatthew G. Knepley       /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */
10088e0841e0SMatthew G. Knepley       for (k = 0; k < pdim; ++k)
10098e0841e0SMatthew G. Knepley         for (j = 0; j < dim; ++j)
10108e0841e0SMatthew G. Knepley           for (i = 0; i < cdim; ++i)
101171d6e60fSMatthew G. Knepley             J[(q*cdim + i)*dim + j] += basisDer[(q*pdim + k)*dim + j] * PetscRealPart(coords[k*cdim + i]);
10128e0841e0SMatthew G. Knepley       PetscLogFlops(2.0*pdim*dim*cdim);
10138e0841e0SMatthew G. Knepley       if (cdim > dim) {
10148e0841e0SMatthew G. Knepley         for (c = dim; c < cdim; ++c)
10158e0841e0SMatthew G. Knepley           for (r = 0; r < cdim; ++r)
10168e0841e0SMatthew G. Knepley             J[r*cdim+c] = r == c ? 1.0 : 0.0;
10178e0841e0SMatthew G. Knepley       }
10188e0841e0SMatthew G. Knepley       switch (cdim) {
10198e0841e0SMatthew G. Knepley       case 3:
10208e0841e0SMatthew G. Knepley         DMPlex_Det3D_Internal(detJ, J);
10218e0841e0SMatthew G. Knepley         if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
102217fe8556SMatthew G. Knepley         break;
102349dc4407SMatthew G. Knepley       case 2:
10248e0841e0SMatthew G. Knepley         DMPlex_Det2D_Internal(detJ, J);
10258e0841e0SMatthew G. Knepley         if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
102649dc4407SMatthew G. Knepley         break;
10278e0841e0SMatthew G. Knepley       case 1:
10288e0841e0SMatthew G. Knepley         *detJ = J[0];
10298e0841e0SMatthew G. Knepley         if (invJ) invJ[0] = 1.0/J[0];
103049dc4407SMatthew G. Knepley       }
103149dc4407SMatthew G. Knepley     }
10328e0841e0SMatthew G. Knepley   }
10338e0841e0SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
10348e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
10358e0841e0SMatthew G. Knepley }
10368e0841e0SMatthew G. Knepley 
10378e0841e0SMatthew G. Knepley #undef __FUNCT__
10388e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFEM"
10398e0841e0SMatthew G. Knepley /*@C
10408e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell
10418e0841e0SMatthew G. Knepley 
10428e0841e0SMatthew G. Knepley   Collective on DM
10438e0841e0SMatthew G. Knepley 
10448e0841e0SMatthew G. Knepley   Input Arguments:
10458e0841e0SMatthew G. Knepley + dm   - the DM
10468e0841e0SMatthew G. Knepley . cell - the cell
10478e0841e0SMatthew G. Knepley - fe   - the finite element containing the quadrature
10488e0841e0SMatthew G. Knepley 
10498e0841e0SMatthew G. Knepley   Output Arguments:
10508e0841e0SMatthew G. Knepley + v0   - the translation part of this transform
10518e0841e0SMatthew G. Knepley . J    - the Jacobian of the transform from the reference element at each quadrature point
10528e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point
10538e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point
10548e0841e0SMatthew G. Knepley 
10558e0841e0SMatthew G. Knepley   Level: advanced
10568e0841e0SMatthew G. Knepley 
10578e0841e0SMatthew G. Knepley   Fortran Notes:
10588e0841e0SMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
10598e0841e0SMatthew G. Knepley   include petsc.h90 in your code.
10608e0841e0SMatthew G. Knepley 
10618e0841e0SMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec()
10628e0841e0SMatthew G. Knepley @*/
10638e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscFE fe, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
10648e0841e0SMatthew G. Knepley {
10658e0841e0SMatthew G. Knepley   PetscErrorCode ierr;
10668e0841e0SMatthew G. Knepley 
10678e0841e0SMatthew G. Knepley   PetscFunctionBegin;
10688e0841e0SMatthew G. Knepley   if (!fe) {ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);}
10698e0841e0SMatthew G. Knepley   else     {ierr = DMPlexComputeIsoparametricGeometry_Internal(dm, fe, cell, v0, J, invJ, detJ);CHKERRQ(ierr);}
1070ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1071ccd2543fSMatthew G Knepley }
1072834e62ceSMatthew G. Knepley 
1073834e62ceSMatthew G. Knepley #undef __FUNCT__
1074cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_1D_Internal"
1075011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1076cc08537eSMatthew G. Knepley {
1077cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1078cc08537eSMatthew G. Knepley   Vec            coordinates;
1079a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
108006e2781eSMatthew G. Knepley   PetscScalar    tmp[2];
1081cc08537eSMatthew G. Knepley   PetscInt       coordSize;
1082cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1083cc08537eSMatthew G. Knepley 
1084cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1085cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
108669d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1087cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1088011ea5d8SMatthew G. Knepley   if (dim != 2) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "We only support 2D edges right now");
108906e2781eSMatthew G. Knepley   ierr = DMPlexLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp);CHKERRQ(ierr);
1090cc08537eSMatthew G. Knepley   if (centroid) {
109106e2781eSMatthew G. Knepley     centroid[0] = 0.5*PetscRealPart(coords[0] + tmp[0]);
109206e2781eSMatthew G. Knepley     centroid[1] = 0.5*PetscRealPart(coords[1] + tmp[1]);
1093cc08537eSMatthew G. Knepley   }
1094cc08537eSMatthew G. Knepley   if (normal) {
1095a60a936bSMatthew G. Knepley     PetscReal norm;
1096a60a936bSMatthew G. Knepley 
109706e2781eSMatthew G. Knepley     normal[0]  = -PetscRealPart(coords[1] - tmp[1]);
109806e2781eSMatthew G. Knepley     normal[1]  =  PetscRealPart(coords[0] - tmp[0]);
1099a60a936bSMatthew G. Knepley     norm       = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1]);
1100a60a936bSMatthew G. Knepley     normal[0] /= norm;
1101a60a936bSMatthew G. Knepley     normal[1] /= norm;
1102cc08537eSMatthew G. Knepley   }
1103cc08537eSMatthew G. Knepley   if (vol) {
110406e2781eSMatthew G. Knepley     *vol = PetscSqrtReal(PetscSqr(PetscRealPart(coords[0] - tmp[0])) + PetscSqr(PetscRealPart(coords[1] - tmp[1])));
1105cc08537eSMatthew G. Knepley   }
1106cc08537eSMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1107cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1108cc08537eSMatthew G. Knepley }
1109cc08537eSMatthew G. Knepley 
1110cc08537eSMatthew G. Knepley #undef __FUNCT__
1111cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_2D_Internal"
1112cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i ) / A */
1113011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1114cc08537eSMatthew G. Knepley {
1115cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1116cc08537eSMatthew G. Knepley   Vec            coordinates;
1117cc08537eSMatthew G. Knepley   PetscScalar   *coords = NULL;
11180a1d6728SMatthew G. Knepley   PetscReal      vsum = 0.0, csum[3] = {0.0, 0.0, 0.0}, vtmp, ctmp[4], v0[3], R[9];
11190a1d6728SMatthew G. Knepley   PetscInt       tdim = 2, coordSize, numCorners, p, d, e;
1120cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1121cc08537eSMatthew G. Knepley 
1122cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1123cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
11240a1d6728SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numCorners);CHKERRQ(ierr);
112569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1126cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
11270bce18caSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1128011ea5d8SMatthew G. Knepley   if (normal) {
1129011ea5d8SMatthew G. Knepley     if (dim > 2) {
11301ee9d5ecSMatthew G. Knepley       const PetscReal x0 = PetscRealPart(coords[dim+0] - coords[0]), x1 = PetscRealPart(coords[dim*2+0] - coords[0]);
11311ee9d5ecSMatthew G. Knepley       const PetscReal y0 = PetscRealPart(coords[dim+1] - coords[1]), y1 = PetscRealPart(coords[dim*2+1] - coords[1]);
11321ee9d5ecSMatthew G. Knepley       const PetscReal z0 = PetscRealPart(coords[dim+2] - coords[2]), z1 = PetscRealPart(coords[dim*2+2] - coords[2]);
11330a1d6728SMatthew G. Knepley       PetscReal       norm;
11340a1d6728SMatthew G. Knepley 
11351ee9d5ecSMatthew G. Knepley       v0[0]     = PetscRealPart(coords[0]);
11361ee9d5ecSMatthew G. Knepley       v0[1]     = PetscRealPart(coords[1]);
11371ee9d5ecSMatthew G. Knepley       v0[2]     = PetscRealPart(coords[2]);
11380a1d6728SMatthew G. Knepley       normal[0] = y0*z1 - z0*y1;
11390a1d6728SMatthew G. Knepley       normal[1] = z0*x1 - x0*z1;
11400a1d6728SMatthew G. Knepley       normal[2] = x0*y1 - y0*x1;
11418b49ba18SBarry Smith       norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2]);
11420a1d6728SMatthew G. Knepley       normal[0] /= norm;
11430a1d6728SMatthew G. Knepley       normal[1] /= norm;
11440a1d6728SMatthew G. Knepley       normal[2] /= norm;
1145011ea5d8SMatthew G. Knepley     } else {
1146011ea5d8SMatthew G. Knepley       for (d = 0; d < dim; ++d) normal[d] = 0.0;
1147011ea5d8SMatthew G. Knepley     }
1148011ea5d8SMatthew G. Knepley   }
114999dec3a6SMatthew G. Knepley   if (dim == 3) {ierr = DMPlexComputeProjection3Dto2D_Internal(coordSize, coords, R);CHKERRQ(ierr);}
11500a1d6728SMatthew G. Knepley   for (p = 0; p < numCorners; ++p) {
11510a1d6728SMatthew G. Knepley     /* Need to do this copy to get types right */
11520a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
11531ee9d5ecSMatthew G. Knepley       ctmp[d]      = PetscRealPart(coords[p*tdim+d]);
11541ee9d5ecSMatthew G. Knepley       ctmp[tdim+d] = PetscRealPart(coords[((p+1)%numCorners)*tdim+d]);
11550a1d6728SMatthew G. Knepley     }
11560a1d6728SMatthew G. Knepley     Volume_Triangle_Origin_Internal(&vtmp, ctmp);
11570a1d6728SMatthew G. Knepley     vsum += vtmp;
11580a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
11590a1d6728SMatthew G. Knepley       csum[d] += (ctmp[d] + ctmp[tdim+d])*vtmp;
11600a1d6728SMatthew G. Knepley     }
11610a1d6728SMatthew G. Knepley   }
11620a1d6728SMatthew G. Knepley   for (d = 0; d < tdim; ++d) {
11630a1d6728SMatthew G. Knepley     csum[d] /= (tdim+1)*vsum;
11640a1d6728SMatthew G. Knepley   }
11650a1d6728SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1166ee6bbdb2SSatish Balay   if (vol) *vol = PetscAbsReal(vsum);
11670a1d6728SMatthew G. Knepley   if (centroid) {
11680a1d6728SMatthew G. Knepley     if (dim > 2) {
11690a1d6728SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
11700a1d6728SMatthew G. Knepley         centroid[d] = v0[d];
11710a1d6728SMatthew G. Knepley         for (e = 0; e < dim; ++e) {
11720a1d6728SMatthew G. Knepley           centroid[d] += R[d*dim+e]*csum[e];
11730a1d6728SMatthew G. Knepley         }
11740a1d6728SMatthew G. Knepley       }
11750a1d6728SMatthew G. Knepley     } else for (d = 0; d < dim; ++d) centroid[d] = csum[d];
11760a1d6728SMatthew G. Knepley   }
1177cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1178cc08537eSMatthew G. Knepley }
1179cc08537eSMatthew G. Knepley 
1180cc08537eSMatthew G. Knepley #undef __FUNCT__
11810ec8681fSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_3D_Internal"
11820ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i ) / V */
1183011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
11840ec8681fSMatthew G. Knepley {
11850ec8681fSMatthew G. Knepley   PetscSection    coordSection;
11860ec8681fSMatthew G. Knepley   Vec             coordinates;
11870ec8681fSMatthew G. Knepley   PetscScalar    *coords = NULL;
118886623015SMatthew G. Knepley   PetscReal       vsum = 0.0, vtmp, coordsTmp[3*3];
1189a7df9edeSMatthew G. Knepley   const PetscInt *faces, *facesO;
11900ec8681fSMatthew G. Knepley   PetscInt        numFaces, f, coordSize, numCorners, p, d;
11910ec8681fSMatthew G. Knepley   PetscErrorCode  ierr;
11920ec8681fSMatthew G. Knepley 
11930ec8681fSMatthew G. Knepley   PetscFunctionBegin;
1194f6dae198SJed Brown   if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim);
11950ec8681fSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
119669d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
11970ec8681fSMatthew G. Knepley 
1198d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0;
11990ec8681fSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numFaces);CHKERRQ(ierr);
12000ec8681fSMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr);
1201a7df9edeSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &facesO);CHKERRQ(ierr);
12020ec8681fSMatthew G. Knepley   for (f = 0; f < numFaces; ++f) {
1203011ea5d8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
12040ec8681fSMatthew G. Knepley     numCorners = coordSize/dim;
12050ec8681fSMatthew G. Knepley     switch (numCorners) {
12060ec8681fSMatthew G. Knepley     case 3:
12070ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
12081ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
12091ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
12101ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]);
12110ec8681fSMatthew G. Knepley       }
12120ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1213a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
12140ec8681fSMatthew G. Knepley       vsum += vtmp;
12154f25033aSJed Brown       if (centroid) {           /* Centroid of OABC = (a+b+c)/4 */
12160ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
12171ee9d5ecSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
12180ec8681fSMatthew G. Knepley         }
12190ec8681fSMatthew G. Knepley       }
12200ec8681fSMatthew G. Knepley       break;
12210ec8681fSMatthew G. Knepley     case 4:
12220ec8681fSMatthew G. Knepley       /* DO FOR PYRAMID */
12230ec8681fSMatthew G. Knepley       /* First tet */
12240ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
12251ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
12261ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
12271ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
12280ec8681fSMatthew G. Knepley       }
12290ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1230a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
12310ec8681fSMatthew G. Knepley       vsum += vtmp;
12320ec8681fSMatthew G. Knepley       if (centroid) {
12330ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
12340ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
12350ec8681fSMatthew G. Knepley         }
12360ec8681fSMatthew G. Knepley       }
12370ec8681fSMatthew G. Knepley       /* Second tet */
12380ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
12391ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[1*dim+d]);
12401ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[2*dim+d]);
12411ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
12420ec8681fSMatthew G. Knepley       }
12430ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1244a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
12450ec8681fSMatthew G. Knepley       vsum += vtmp;
12460ec8681fSMatthew G. Knepley       if (centroid) {
12470ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
12480ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
12490ec8681fSMatthew G. Knepley         }
12500ec8681fSMatthew G. Knepley       }
12510ec8681fSMatthew G. Knepley       break;
12520ec8681fSMatthew G. Knepley     default:
1253796f034aSJed Brown       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle faces with %D vertices", numCorners);
12540ec8681fSMatthew G. Knepley     }
12554f25033aSJed Brown     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
12560ec8681fSMatthew G. Knepley   }
12578763be8eSMatthew G. Knepley   if (vol)     *vol = PetscAbsReal(vsum);
12580ec8681fSMatthew G. Knepley   if (normal)   for (d = 0; d < dim; ++d) normal[d]    = 0.0;
1259d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] /= (vsum*4);
12600ec8681fSMatthew G. Knepley   PetscFunctionReturn(0);
12610ec8681fSMatthew G. Knepley }
12620ec8681fSMatthew G. Knepley 
12630ec8681fSMatthew G. Knepley #undef __FUNCT__
1264834e62ceSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFVM"
1265834e62ceSMatthew G. Knepley /*@C
1266834e62ceSMatthew G. Knepley   DMPlexComputeCellGeometryFVM - Compute the volume for a given cell
1267834e62ceSMatthew G. Knepley 
1268834e62ceSMatthew G. Knepley   Collective on DM
1269834e62ceSMatthew G. Knepley 
1270834e62ceSMatthew G. Knepley   Input Arguments:
1271834e62ceSMatthew G. Knepley + dm   - the DM
1272834e62ceSMatthew G. Knepley - cell - the cell
1273834e62ceSMatthew G. Knepley 
1274834e62ceSMatthew G. Knepley   Output Arguments:
1275834e62ceSMatthew G. Knepley + volume   - the cell volume
1276cc08537eSMatthew G. Knepley . centroid - the cell centroid
1277cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate
1278834e62ceSMatthew G. Knepley 
1279834e62ceSMatthew G. Knepley   Level: advanced
1280834e62ceSMatthew G. Knepley 
1281834e62ceSMatthew G. Knepley   Fortran Notes:
1282834e62ceSMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1283834e62ceSMatthew G. Knepley   include petsc.h90 in your code.
1284834e62ceSMatthew G. Knepley 
128569d8a9ceSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec()
1286834e62ceSMatthew G. Knepley @*/
1287cc08537eSMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1288834e62ceSMatthew G. Knepley {
12890ec8681fSMatthew G. Knepley   PetscInt       depth, dim;
1290834e62ceSMatthew G. Knepley   PetscErrorCode ierr;
1291834e62ceSMatthew G. Knepley 
1292834e62ceSMatthew G. Knepley   PetscFunctionBegin;
1293834e62ceSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1294c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1295834e62ceSMatthew G. Knepley   if (depth != dim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");
1296834e62ceSMatthew G. Knepley   /* We need to keep a pointer to the depth label */
1297011ea5d8SMatthew G. Knepley   ierr = DMPlexGetLabelValue(dm, "depth", cell, &depth);CHKERRQ(ierr);
1298834e62ceSMatthew G. Knepley   /* Cone size is now the number of faces */
1299011ea5d8SMatthew G. Knepley   switch (depth) {
1300cc08537eSMatthew G. Knepley   case 1:
1301011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1302cc08537eSMatthew G. Knepley     break;
1303834e62ceSMatthew G. Knepley   case 2:
1304011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1305834e62ceSMatthew G. Knepley     break;
1306834e62ceSMatthew G. Knepley   case 3:
1307011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1308834e62ceSMatthew G. Knepley     break;
1309834e62ceSMatthew G. Knepley   default:
1310834e62ceSMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
1311834e62ceSMatthew G. Knepley   }
1312834e62ceSMatthew G. Knepley   PetscFunctionReturn(0);
1313834e62ceSMatthew G. Knepley }
1314113c68e6SMatthew G. Knepley 
1315113c68e6SMatthew G. Knepley #undef __FUNCT__
1316c0d900a5SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFEM"
1317c0d900a5SMatthew G. Knepley /* This should also take a PetscFE argument I think */
1318c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom)
1319c0d900a5SMatthew G. Knepley {
1320c0d900a5SMatthew G. Knepley   DM             dmCell;
1321c0d900a5SMatthew G. Knepley   Vec            coordinates;
1322c0d900a5SMatthew G. Knepley   PetscSection   coordSection, sectionCell;
1323c0d900a5SMatthew G. Knepley   PetscScalar   *cgeom;
1324c0d900a5SMatthew G. Knepley   PetscInt       cStart, cEnd, cMax, c;
1325c0d900a5SMatthew G. Knepley   PetscErrorCode ierr;
1326c0d900a5SMatthew G. Knepley 
1327c0d900a5SMatthew G. Knepley   PetscFunctionBegin;
1328c0d900a5SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1329c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1330c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1331c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1332c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1333c0d900a5SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1334c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1335c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
1336c0d900a5SMatthew G. Knepley   cEnd = cMax < 0 ? cEnd : cMax;
1337c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
1338c0d900a5SMatthew G. Knepley   /* TODO This needs to be multiplied by Nq for non-affine */
13399e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFECellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1340c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1341c0d900a5SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1342c0d900a5SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1343c0d900a5SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
1344c0d900a5SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1345c0d900a5SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1346c0d900a5SMatthew G. Knepley     PetscFECellGeom *cg;
1347c0d900a5SMatthew G. Knepley 
1348c0d900a5SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1349c0d900a5SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1350c0d900a5SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v0, cg->J, cg->invJ, &cg->detJ);CHKERRQ(ierr);
1351c0d900a5SMatthew G. Knepley     if (cg->detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", cg->detJ, c);
1352c0d900a5SMatthew G. Knepley   }
1353c0d900a5SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1354c0d900a5SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
1355c0d900a5SMatthew G. Knepley   PetscFunctionReturn(0);
1356c0d900a5SMatthew G. Knepley }
1357c0d900a5SMatthew G. Knepley 
1358c0d900a5SMatthew G. Knepley #undef __FUNCT__
1359113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM"
1360891a9168SMatthew G. Knepley /*@
1361891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
1362891a9168SMatthew G. Knepley 
1363891a9168SMatthew G. Knepley   Input Parameter:
1364891a9168SMatthew G. Knepley . dm - The DM
1365891a9168SMatthew G. Knepley 
1366891a9168SMatthew G. Knepley   Output Parameters:
1367891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data
1368891a9168SMatthew G. Knepley . facegeom - A Vec of PetscFVFaceGeom data
1369891a9168SMatthew G. Knepley 
1370891a9168SMatthew G. Knepley   Level: developer
1371891a9168SMatthew G. Knepley 
1372891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM()
1373891a9168SMatthew G. Knepley @*/
1374113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom)
1375113c68e6SMatthew G. Knepley {
1376113c68e6SMatthew G. Knepley   DM             dmFace, dmCell;
1377113c68e6SMatthew G. Knepley   DMLabel        ghostLabel;
1378113c68e6SMatthew G. Knepley   PetscSection   sectionFace, sectionCell;
1379113c68e6SMatthew G. Knepley   PetscSection   coordSection;
1380113c68e6SMatthew G. Knepley   Vec            coordinates;
1381113c68e6SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
1382113c68e6SMatthew G. Knepley   PetscReal      minradius, gminradius;
1383113c68e6SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
1384113c68e6SMatthew G. Knepley   PetscErrorCode ierr;
1385113c68e6SMatthew G. Knepley 
1386113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1387113c68e6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1388113c68e6SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1389113c68e6SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1390113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
1391113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1392113c68e6SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1393113c68e6SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1394113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1395113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1396113c68e6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1397113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
13989e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1399113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1400113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1401113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1402113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
1403113c68e6SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1404113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
1405113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
1406113c68e6SMatthew G. Knepley 
1407113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1408113c68e6SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1409113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr);
1410113c68e6SMatthew G. Knepley   }
1411113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
1412113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmFace);CHKERRQ(ierr);
1413113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionFace);CHKERRQ(ierr);
1414113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1415113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr);
14169e5edeeeSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1417113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr);
1418113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmFace, sectionFace);CHKERRQ(ierr);
1419113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionFace);CHKERRQ(ierr);
1420113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr);
1421113c68e6SMatthew G. Knepley   ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr);
1422113c68e6SMatthew G. Knepley   ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1423113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
1424113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
1425113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
1426113c68e6SMatthew G. Knepley     PetscReal        area;
14279ac3fadcSMatthew G. Knepley     PetscInt         ghost = -1, d;
1428113c68e6SMatthew G. Knepley 
14299ac3fadcSMatthew G. Knepley     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
1430113c68e6SMatthew G. Knepley     if (ghost >= 0) continue;
1431113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr);
1432113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr);
1433113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
1434113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
1435113c68e6SMatthew G. Knepley     {
1436113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
1437113c68e6SMatthew G. Knepley       const PetscInt  *cells;
1438113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
14390453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
1440113c68e6SMatthew G. Knepley 
1441113c68e6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr);
1442113c68e6SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr);
1443113c68e6SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr);
1444113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
1445113c68e6SMatthew G. Knepley       rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
1446f170fd80SMatthew G. Knepley       ierr = DMPlexLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr);
1447f170fd80SMatthew G. Knepley       ierr = DMPlexLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr);
14480453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
1449113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
1450113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
1451113c68e6SMatthew G. Knepley       }
1452113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
1453113c68e6SMatthew 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]);
1454113c68e6SMatthew 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]);
1455113c68e6SMatthew G. Knepley         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f);
1456113c68e6SMatthew G. Knepley       }
1457113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
1458113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
1459113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
1460113c68e6SMatthew G. Knepley       }
1461113c68e6SMatthew G. Knepley       if (cells[1] < cEndInterior) {
1462113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
1463113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
1464113c68e6SMatthew G. Knepley       }
1465113c68e6SMatthew G. Knepley     }
1466113c68e6SMatthew G. Knepley   }
1467a9b180a6SBarry Smith   ierr = MPI_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1468113c68e6SMatthew G. Knepley   ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr);
1469113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
1470113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
1471113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
1472113c68e6SMatthew G. Knepley     const PetscInt  *cone,    *support;
1473113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
1474113c68e6SMatthew G. Knepley 
1475113c68e6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr);
1476113c68e6SMatthew G. Knepley     if (coneSize != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize);
1477113c68e6SMatthew G. Knepley     ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr);
1478113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr);
1479113c68e6SMatthew G. Knepley     if (supportSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 1", cone[0], supportSize);
1480113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr);
1481113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr);
1482113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
1483113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
1484113c68e6SMatthew G. Knepley       if (support[s] == c) {
1485113c68e6SMatthew G. Knepley         const PetscFVCellGeom *ci;
1486113c68e6SMatthew G. Knepley         PetscFVCellGeom       *cg;
1487113c68e6SMatthew G. Knepley         PetscReal              c2f[3], a;
1488113c68e6SMatthew G. Knepley 
1489113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr);
1490113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
1491113c68e6SMatthew G. Knepley         a    = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
1492113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr);
1493113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid);
1494113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
1495113c68e6SMatthew G. Knepley       }
1496113c68e6SMatthew G. Knepley     }
1497113c68e6SMatthew G. Knepley   }
1498113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr);
1499113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1500113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
1501113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmFace);CHKERRQ(ierr);
1502113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1503113c68e6SMatthew G. Knepley }
1504113c68e6SMatthew G. Knepley 
1505113c68e6SMatthew G. Knepley #undef __FUNCT__
1506113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexGetMinRadius"
1507113c68e6SMatthew G. Knepley /*@C
1508113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
1509113c68e6SMatthew G. Knepley 
1510113c68e6SMatthew G. Knepley   Not collective
1511113c68e6SMatthew G. Knepley 
1512113c68e6SMatthew G. Knepley   Input Argument:
1513113c68e6SMatthew G. Knepley . dm - the DM
1514113c68e6SMatthew G. Knepley 
1515113c68e6SMatthew G. Knepley   Output Argument:
1516113c68e6SMatthew G. Knepley . minradius - the minium cell radius
1517113c68e6SMatthew G. Knepley 
1518113c68e6SMatthew G. Knepley   Level: developer
1519113c68e6SMatthew G. Knepley 
1520113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates()
1521113c68e6SMatthew G. Knepley @*/
1522113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius)
1523113c68e6SMatthew G. Knepley {
1524113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1525113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1526113c68e6SMatthew G. Knepley   PetscValidPointer(minradius,2);
1527113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex*) dm->data)->minradius;
1528113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1529113c68e6SMatthew G. Knepley }
1530113c68e6SMatthew G. Knepley 
1531113c68e6SMatthew G. Knepley #undef __FUNCT__
1532113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexSetMinRadius"
1533113c68e6SMatthew G. Knepley /*@C
1534113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
1535113c68e6SMatthew G. Knepley 
1536113c68e6SMatthew G. Knepley   Logically collective
1537113c68e6SMatthew G. Knepley 
1538113c68e6SMatthew G. Knepley   Input Arguments:
1539113c68e6SMatthew G. Knepley + dm - the DM
1540113c68e6SMatthew G. Knepley - minradius - the minium cell radius
1541113c68e6SMatthew G. Knepley 
1542113c68e6SMatthew G. Knepley   Level: developer
1543113c68e6SMatthew G. Knepley 
1544113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates()
1545113c68e6SMatthew G. Knepley @*/
1546113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius)
1547113c68e6SMatthew G. Knepley {
1548113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1549113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1550113c68e6SMatthew G. Knepley   ((DM_Plex*) dm->data)->minradius = minradius;
1551113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1552113c68e6SMatthew G. Knepley }
1553856ac710SMatthew G. Knepley 
1554856ac710SMatthew G. Knepley #undef __FUNCT__
1555856ac710SMatthew G. Knepley #define __FUNCT__ "BuildGradientReconstruction_Internal"
1556856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
1557856ac710SMatthew G. Knepley {
1558856ac710SMatthew G. Knepley   DMLabel        ghostLabel;
1559856ac710SMatthew G. Knepley   PetscScalar   *dx, *grad, **gref;
1560856ac710SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
1561856ac710SMatthew G. Knepley   PetscErrorCode ierr;
1562856ac710SMatthew G. Knepley 
1563856ac710SMatthew G. Knepley   PetscFunctionBegin;
1564856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1565856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1566856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1567856ac710SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr);
1568856ac710SMatthew G. Knepley   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
1569856ac710SMatthew G. Knepley   ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1570856ac710SMatthew G. Knepley   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
1571856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
1572856ac710SMatthew G. Knepley     const PetscInt        *faces;
1573856ac710SMatthew G. Knepley     PetscInt               numFaces, usedFaces, f, d;
1574856ac710SMatthew G. Knepley     const PetscFVCellGeom *cg;
1575856ac710SMatthew G. Knepley     PetscBool              boundary;
1576856ac710SMatthew G. Knepley     PetscInt               ghost;
1577856ac710SMatthew G. Knepley 
1578856ac710SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1579856ac710SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr);
1580856ac710SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr);
1581856ac710SMatthew 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);
1582856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
1583856ac710SMatthew G. Knepley       const PetscFVCellGeom *cg1;
1584856ac710SMatthew G. Knepley       PetscFVFaceGeom       *fg;
1585856ac710SMatthew G. Knepley       const PetscInt        *fcells;
1586856ac710SMatthew G. Knepley       PetscInt               ncell, side;
1587856ac710SMatthew G. Knepley 
1588856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
1589856ac710SMatthew G. Knepley       ierr = DMPlexIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
1590856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
1591856ac710SMatthew G. Knepley       ierr  = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr);
1592856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
1593856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
1594856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr);
1595856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
1596856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d];
1597856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side];  /* Gradient reconstruction term will go here */
1598856ac710SMatthew G. Knepley     }
1599856ac710SMatthew G. Knepley     if (!usedFaces) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
1600856ac710SMatthew G. Knepley     ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr);
1601856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
1602856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
1603856ac710SMatthew G. Knepley       ierr = DMPlexIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
1604856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
1605856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d];
1606856ac710SMatthew G. Knepley       ++usedFaces;
1607856ac710SMatthew G. Knepley     }
1608856ac710SMatthew G. Knepley   }
1609856ac710SMatthew G. Knepley   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
1610856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
1611856ac710SMatthew G. Knepley }
1612856ac710SMatthew G. Knepley 
1613856ac710SMatthew G. Knepley #undef __FUNCT__
1614856ac710SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGradientFVM"
1615856ac710SMatthew G. Knepley /*@
1616856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
1617856ac710SMatthew G. Knepley 
1618856ac710SMatthew G. Knepley   Collective on DM
1619856ac710SMatthew G. Knepley 
1620856ac710SMatthew G. Knepley   Input Arguments:
1621856ac710SMatthew G. Knepley + dm  - The DM
1622856ac710SMatthew G. Knepley . fvm - The PetscFV
1623856ac710SMatthew G. Knepley . faceGeometry - The face geometry from DMPlexGetFaceGeometryFVM()
1624856ac710SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexGetCellGeometryFVM()
1625856ac710SMatthew G. Knepley 
1626856ac710SMatthew G. Knepley   Output Parameters:
1627856ac710SMatthew G. Knepley + faceGeometry - The geometric factors for gradient calculation are inserted
1628856ac710SMatthew G. Knepley - dmGrad - The DM describing the layout of gradient data
1629856ac710SMatthew G. Knepley 
1630856ac710SMatthew G. Knepley   Level: developer
1631856ac710SMatthew G. Knepley 
1632856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM()
1633856ac710SMatthew G. Knepley @*/
1634856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad)
1635856ac710SMatthew G. Knepley {
1636856ac710SMatthew G. Knepley   DM             dmFace, dmCell;
1637856ac710SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
1638856ac710SMatthew G. Knepley   PetscSection   sectionGrad;
1639856ac710SMatthew G. Knepley   PetscInt       dim, pdim, cStart, cEnd, cEndInterior, c;
1640856ac710SMatthew G. Knepley   PetscErrorCode ierr;
1641856ac710SMatthew G. Knepley 
1642856ac710SMatthew G. Knepley   PetscFunctionBegin;
1643856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1644856ac710SMatthew G. Knepley   ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr);
1645856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1646856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1647856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
1648856ac710SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
1649856ac710SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
1650856ac710SMatthew G. Knepley   ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr);
1651856ac710SMatthew G. Knepley   ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr);
1652856ac710SMatthew G. Knepley   ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
1653856ac710SMatthew G. Knepley   ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr);
1654856ac710SMatthew G. Knepley   ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr);
1655856ac710SMatthew G. Knepley   /* Create storage for gradients */
1656856ac710SMatthew G. Knepley   ierr = DMClone(dm, dmGrad);CHKERRQ(ierr);
1657856ac710SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionGrad);CHKERRQ(ierr);
1658856ac710SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr);
1659856ac710SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);}
1660856ac710SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr);
1661856ac710SMatthew G. Knepley   ierr = DMSetDefaultSection(*dmGrad, sectionGrad);CHKERRQ(ierr);
1662856ac710SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionGrad);CHKERRQ(ierr);
1663856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
1664856ac710SMatthew G. Knepley }
1665