xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision c4eade1cd939ab6fb630e74021eb049ea53490a5)
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__
134*c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashInitialize_Internal"
135*c4eade1cSMatthew G. Knepley static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[])
136*c4eade1cSMatthew G. Knepley {
137*c4eade1cSMatthew G. Knepley   PetscInt d;
138*c4eade1cSMatthew G. Knepley 
139*c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
140*c4eade1cSMatthew G. Knepley   box->dim = dim;
141*c4eade1cSMatthew G. Knepley   for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]);
142*c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
143*c4eade1cSMatthew G. Knepley }
144*c4eade1cSMatthew G. Knepley 
145*c4eade1cSMatthew G. Knepley #undef __FUNCT__
146*c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashCreate"
147*c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box)
148*c4eade1cSMatthew G. Knepley {
149*c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
150*c4eade1cSMatthew G. Knepley 
151*c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
152*c4eade1cSMatthew G. Knepley   ierr = PetscMalloc1(1, box);CHKERRQ(ierr);
153*c4eade1cSMatthew G. Knepley   ierr = PetscGridHashInitialize_Internal(*box, dim, point);CHKERRQ(ierr);
154*c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
155*c4eade1cSMatthew G. Knepley }
156*c4eade1cSMatthew G. Knepley 
157*c4eade1cSMatthew G. Knepley #undef __FUNCT__
158*c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashEnlarge"
159*c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[])
160*c4eade1cSMatthew G. Knepley {
161*c4eade1cSMatthew G. Knepley   PetscInt d;
162*c4eade1cSMatthew G. Knepley 
163*c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
164*c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
165*c4eade1cSMatthew G. Knepley     box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d]));
166*c4eade1cSMatthew G. Knepley     box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d]));
167*c4eade1cSMatthew G. Knepley   }
168*c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
169*c4eade1cSMatthew G. Knepley }
170*c4eade1cSMatthew G. Knepley 
171*c4eade1cSMatthew G. Knepley #undef __FUNCT__
172*c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashSetGrid"
173*c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[])
174*c4eade1cSMatthew G. Knepley {
175*c4eade1cSMatthew G. Knepley   PetscInt d;
176*c4eade1cSMatthew G. Knepley 
177*c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
178*c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
179*c4eade1cSMatthew G. Knepley     box->extent[d] = box->upper[d] - box->lower[d];
180*c4eade1cSMatthew G. Knepley     if (n[d] == PETSC_DETERMINE) {
181*c4eade1cSMatthew G. Knepley       box->h[d] = h[d];
182*c4eade1cSMatthew G. Knepley       box->n[d] = PetscCeilReal(box->extent[d]/h[d]);
183*c4eade1cSMatthew G. Knepley     } else {
184*c4eade1cSMatthew G. Knepley       box->n[d] = n[d];
185*c4eade1cSMatthew G. Knepley       box->h[d] = box->extent[d]/n[d];
186*c4eade1cSMatthew G. Knepley     }
187*c4eade1cSMatthew G. Knepley   }
188*c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
189*c4eade1cSMatthew G. Knepley }
190*c4eade1cSMatthew G. Knepley 
191*c4eade1cSMatthew G. Knepley #undef __FUNCT__
192*c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashGetEnclosingBox"
193*c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscReal points[], PetscInt dboxes[], PetscInt boxes[])
194*c4eade1cSMatthew G. Knepley {
195*c4eade1cSMatthew G. Knepley   const PetscReal *lower = box->lower;
196*c4eade1cSMatthew G. Knepley   const PetscReal *upper = box->upper;
197*c4eade1cSMatthew G. Knepley   const PetscReal *h     = box->h;
198*c4eade1cSMatthew G. Knepley   const PetscInt  *n     = box->n;
199*c4eade1cSMatthew G. Knepley   const PetscInt   dim   = box->dim;
200*c4eade1cSMatthew G. Knepley   PetscInt         d, p;
201*c4eade1cSMatthew G. Knepley 
202*c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
203*c4eade1cSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
204*c4eade1cSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
205*c4eade1cSMatthew G. Knepley       PetscInt dbox = PetscFloorReal((points[p*dim+d] - lower[d])/h[d]);
206*c4eade1cSMatthew G. Knepley 
207*c4eade1cSMatthew G. Knepley       if (dbox == n[d] && PetscAbsReal(points[p*dim+d] - upper[d]) < 1.0e-9) dbox = n[d]-1;
208*c4eade1cSMatthew 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",
209*c4eade1cSMatthew G. Knepley                                              p, points[p*dim+0], dim > 1 ? points[p*dim+1] : 0.0, dim > 2 ? points[p*dim+2] : 0.0);
210*c4eade1cSMatthew G. Knepley       dboxes[p*dim+d] = dbox;
211*c4eade1cSMatthew G. Knepley     }
212*c4eade1cSMatthew G. Knepley     if (boxes) for (d = 1, boxes[p] = dboxes[p*dim]; d < dim; ++d) boxes[p] += dboxes[p*dim+d]*n[d-1];
213*c4eade1cSMatthew G. Knepley   }
214*c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
215*c4eade1cSMatthew G. Knepley }
216*c4eade1cSMatthew G. Knepley 
217*c4eade1cSMatthew G. Knepley #undef __FUNCT__
218*c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashDestroy"
219*c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashDestroy(PetscGridHash *box)
220*c4eade1cSMatthew G. Knepley {
221*c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
222*c4eade1cSMatthew G. Knepley 
223*c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
224*c4eade1cSMatthew G. Knepley   if (*box) {
225*c4eade1cSMatthew G. Knepley     ierr = PetscSectionDestroy(&(*box)->cellSection);CHKERRQ(ierr);
226*c4eade1cSMatthew G. Knepley     ierr = ISDestroy(&(*box)->cells);CHKERRQ(ierr);
227*c4eade1cSMatthew G. Knepley     ierr = DMLabelDestroy(&(*box)->cellsSparse);CHKERRQ(ierr);
228*c4eade1cSMatthew G. Knepley   }
229*c4eade1cSMatthew G. Knepley   ierr = PetscFree(*box);CHKERRQ(ierr);
230*c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
231*c4eade1cSMatthew G. Knepley }
232*c4eade1cSMatthew G. Knepley 
233ccd2543fSMatthew G Knepley #define __FUNCT__ "DMLocatePoints_Plex"
234ccd2543fSMatthew G Knepley /*
235ccd2543fSMatthew G Knepley  Need to implement using the guess
236ccd2543fSMatthew G Knepley */
237ccd2543fSMatthew G Knepley PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, IS *cellIS)
238ccd2543fSMatthew G Knepley {
239ccd2543fSMatthew G Knepley   PetscInt       cell = -1 /*, guess = -1*/;
240ccd2543fSMatthew G Knepley   PetscInt       bs, numPoints, p;
241ccd2543fSMatthew G Knepley   PetscInt       dim, cStart, cEnd, cMax, c, coneSize;
242ccd2543fSMatthew G Knepley   PetscInt      *cells;
243ccd2543fSMatthew G Knepley   PetscScalar   *a;
244ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
245ccd2543fSMatthew G Knepley 
246ccd2543fSMatthew G Knepley   PetscFunctionBegin;
247c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
248ccd2543fSMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
249ccd2543fSMatthew G Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
250ccd2543fSMatthew G Knepley   if (cMax >= 0) cEnd = PetscMin(cEnd, cMax);
251ccd2543fSMatthew G Knepley   ierr = VecGetLocalSize(v, &numPoints);CHKERRQ(ierr);
252ccd2543fSMatthew G Knepley   ierr = VecGetBlockSize(v, &bs);CHKERRQ(ierr);
253ccd2543fSMatthew G Knepley   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
254796f034aSJed Brown   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);
255ccd2543fSMatthew G Knepley   numPoints /= bs;
256785e854fSJed Brown   ierr       = PetscMalloc1(numPoints, &cells);CHKERRQ(ierr);
257ccd2543fSMatthew G Knepley   for (p = 0; p < numPoints; ++p) {
258ccd2543fSMatthew G Knepley     const PetscScalar *point = &a[p*bs];
259ccd2543fSMatthew G Knepley 
260ccd2543fSMatthew G Knepley     switch (dim) {
261ccd2543fSMatthew G Knepley     case 2:
262ccd2543fSMatthew G Knepley       for (c = cStart; c < cEnd; ++c) {
263ccd2543fSMatthew G Knepley         ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
264ccd2543fSMatthew G Knepley         switch (coneSize) {
265ccd2543fSMatthew G Knepley         case 3:
266ccd2543fSMatthew G Knepley           ierr = DMPlexLocatePoint_Simplex_2D_Internal(dm, point, c, &cell);CHKERRQ(ierr);
267ccd2543fSMatthew G Knepley           break;
268ccd2543fSMatthew G Knepley         case 4:
269ccd2543fSMatthew G Knepley           ierr = DMPlexLocatePoint_General_2D_Internal(dm, point, c, &cell);CHKERRQ(ierr);
270ccd2543fSMatthew G Knepley           break;
271ccd2543fSMatthew G Knepley         default:
272796f034aSJed Brown           SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize);
273ccd2543fSMatthew G Knepley         }
274ccd2543fSMatthew G Knepley         if (cell >= 0) break;
275ccd2543fSMatthew G Knepley       }
276ccd2543fSMatthew G Knepley       break;
277ccd2543fSMatthew G Knepley     case 3:
278ccd2543fSMatthew G Knepley       for (c = cStart; c < cEnd; ++c) {
279ccd2543fSMatthew G Knepley         ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
280ccd2543fSMatthew G Knepley         switch (coneSize) {
281ccd2543fSMatthew G Knepley         case 4:
282ccd2543fSMatthew G Knepley           ierr = DMPlexLocatePoint_Simplex_3D_Internal(dm, point, c, &cell);CHKERRQ(ierr);
283ccd2543fSMatthew G Knepley           break;
28435953a02SMatthew G. Knepley         case 6:
285ccd2543fSMatthew G Knepley           ierr = DMPlexLocatePoint_General_3D_Internal(dm, point, c, &cell);CHKERRQ(ierr);
286ccd2543fSMatthew G Knepley           break;
287ccd2543fSMatthew G Knepley         default:
288796f034aSJed Brown           SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize);
289ccd2543fSMatthew G Knepley         }
290ccd2543fSMatthew G Knepley         if (cell >= 0) break;
291ccd2543fSMatthew G Knepley       }
292ccd2543fSMatthew G Knepley       break;
293ccd2543fSMatthew G Knepley     default:
294796f034aSJed Brown       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for mesh dimension %D", dim);
295ccd2543fSMatthew G Knepley     }
296ccd2543fSMatthew G Knepley     cells[p] = cell;
297ccd2543fSMatthew G Knepley   }
298ccd2543fSMatthew G Knepley   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
299ccd2543fSMatthew G Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints, cells, PETSC_OWN_POINTER, cellIS);CHKERRQ(ierr);
300ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
301ccd2543fSMatthew G Knepley }
302ccd2543fSMatthew G Knepley 
303ccd2543fSMatthew G Knepley #undef __FUNCT__
30417fe8556SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection2Dto1D_Internal"
30517fe8556SMatthew G. Knepley /*
30617fe8556SMatthew G. Knepley   DMPlexComputeProjection2Dto1D_Internal - Rewrite coordinates to be the 1D projection of the 2D
30717fe8556SMatthew G. Knepley */
3083beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection2Dto1D_Internal(PetscScalar coords[], PetscReal R[])
30917fe8556SMatthew G. Knepley {
31017fe8556SMatthew G. Knepley   const PetscReal x = PetscRealPart(coords[2] - coords[0]);
31117fe8556SMatthew G. Knepley   const PetscReal y = PetscRealPart(coords[3] - coords[1]);
3128b49ba18SBarry Smith   const PetscReal r = PetscSqrtReal(x*x + y*y), c = x/r, s = y/r;
31317fe8556SMatthew G. Knepley 
31417fe8556SMatthew G. Knepley   PetscFunctionBegin;
3151c99cf0cSGeoffrey Irving   R[0] = c; R[1] = -s;
3161c99cf0cSGeoffrey Irving   R[2] = s; R[3] =  c;
31717fe8556SMatthew G. Knepley   coords[0] = 0.0;
3187f07f362SMatthew G. Knepley   coords[1] = r;
31917fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
32017fe8556SMatthew G. Knepley }
32117fe8556SMatthew G. Knepley 
32217fe8556SMatthew G. Knepley #undef __FUNCT__
32328dbe442SToby Isaac #define __FUNCT__ "DMPlexComputeProjection3Dto1D_Internal"
32428dbe442SToby Isaac /*
32528dbe442SToby Isaac   DMPlexComputeProjection3Dto1D_Internal - Rewrite coordinates to be the 1D projection of the 3D
32628dbe442SToby Isaac 
32728dbe442SToby Isaac   This uses the basis completion described by Frisvad,
32828dbe442SToby Isaac 
32928dbe442SToby Isaac   http://www.imm.dtu.dk/~jerf/papers/abstracts/onb.html
33028dbe442SToby Isaac   DOI:10.1080/2165347X.2012.689606
33128dbe442SToby Isaac */
3323beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto1D_Internal(PetscScalar coords[], PetscReal R[])
33328dbe442SToby Isaac {
33428dbe442SToby Isaac   PetscReal      x    = PetscRealPart(coords[3] - coords[0]);
33528dbe442SToby Isaac   PetscReal      y    = PetscRealPart(coords[4] - coords[1]);
33628dbe442SToby Isaac   PetscReal      z    = PetscRealPart(coords[5] - coords[2]);
33728dbe442SToby Isaac   PetscReal      r    = PetscSqrtReal(x*x + y*y + z*z);
33828dbe442SToby Isaac   PetscReal      rinv = 1. / r;
33928dbe442SToby Isaac   PetscFunctionBegin;
34028dbe442SToby Isaac 
34128dbe442SToby Isaac   x *= rinv; y *= rinv; z *= rinv;
34228dbe442SToby Isaac   if (x > 0.) {
34328dbe442SToby Isaac     PetscReal inv1pX   = 1./ (1. + x);
34428dbe442SToby Isaac 
34528dbe442SToby Isaac     R[0] = x; R[1] = -y;              R[2] = -z;
34628dbe442SToby Isaac     R[3] = y; R[4] = 1. - y*y*inv1pX; R[5] =     -y*z*inv1pX;
34728dbe442SToby Isaac     R[6] = z; R[7] =     -y*z*inv1pX; R[8] = 1. - z*z*inv1pX;
34828dbe442SToby Isaac   }
34928dbe442SToby Isaac   else {
35028dbe442SToby Isaac     PetscReal inv1mX   = 1./ (1. - x);
35128dbe442SToby Isaac 
35228dbe442SToby Isaac     R[0] = x; R[1] = z;               R[2] = y;
35328dbe442SToby Isaac     R[3] = y; R[4] =     -y*z*inv1mX; R[5] = 1. - y*y*inv1mX;
35428dbe442SToby Isaac     R[6] = z; R[7] = 1. - z*z*inv1mX; R[8] =     -y*z*inv1mX;
35528dbe442SToby Isaac   }
35628dbe442SToby Isaac   coords[0] = 0.0;
35728dbe442SToby Isaac   coords[1] = r;
35828dbe442SToby Isaac   PetscFunctionReturn(0);
35928dbe442SToby Isaac }
36028dbe442SToby Isaac 
36128dbe442SToby Isaac #undef __FUNCT__
362ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeProjection3Dto2D_Internal"
363ccd2543fSMatthew G Knepley /*
364ccd2543fSMatthew G Knepley   DMPlexComputeProjection3Dto2D_Internal - Rewrite coordinates to be the 2D projection of the 3D
365ccd2543fSMatthew G Knepley */
3663beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto2D_Internal(PetscInt coordSize, PetscScalar coords[], PetscReal R[])
367ccd2543fSMatthew G Knepley {
3681ee9d5ecSMatthew G. Knepley   PetscReal      x1[3],  x2[3], n[3], norm;
36999dec3a6SMatthew G. Knepley   PetscReal      x1p[3], x2p[3], xnp[3];
3704a217a95SMatthew G. Knepley   PetscReal      sqrtz, alpha;
371ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
37299dec3a6SMatthew G. Knepley   PetscInt       d, e, p;
373ccd2543fSMatthew G Knepley 
374ccd2543fSMatthew G Knepley   PetscFunctionBegin;
375ccd2543fSMatthew G Knepley   /* 0) Calculate normal vector */
376ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
3771ee9d5ecSMatthew G. Knepley     x1[d] = PetscRealPart(coords[1*dim+d] - coords[0*dim+d]);
3781ee9d5ecSMatthew G. Knepley     x2[d] = PetscRealPart(coords[2*dim+d] - coords[0*dim+d]);
379ccd2543fSMatthew G Knepley   }
380ccd2543fSMatthew G Knepley   n[0] = x1[1]*x2[2] - x1[2]*x2[1];
381ccd2543fSMatthew G Knepley   n[1] = x1[2]*x2[0] - x1[0]*x2[2];
382ccd2543fSMatthew G Knepley   n[2] = x1[0]*x2[1] - x1[1]*x2[0];
3838b49ba18SBarry Smith   norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
384ccd2543fSMatthew G Knepley   n[0] /= norm;
385ccd2543fSMatthew G Knepley   n[1] /= norm;
386ccd2543fSMatthew G Knepley   n[2] /= norm;
387ccd2543fSMatthew G Knepley   /* 1) Take the normal vector and rotate until it is \hat z
388ccd2543fSMatthew G Knepley 
389ccd2543fSMatthew G Knepley     Let the normal vector be <nx, ny, nz> and alpha = 1/sqrt(1 - nz^2), then
390ccd2543fSMatthew G Knepley 
391ccd2543fSMatthew G Knepley     R = /  alpha nx nz  alpha ny nz -1/alpha \
392ccd2543fSMatthew G Knepley         | -alpha ny     alpha nx        0    |
393ccd2543fSMatthew G Knepley         \     nx            ny         nz    /
394ccd2543fSMatthew G Knepley 
395ccd2543fSMatthew G Knepley     will rotate the normal vector to \hat z
396ccd2543fSMatthew G Knepley   */
3978b49ba18SBarry Smith   sqrtz = PetscSqrtReal(1.0 - n[2]*n[2]);
39873868372SMatthew G. Knepley   /* Check for n = z */
39973868372SMatthew G. Knepley   if (sqrtz < 1.0e-10) {
4001ee9d5ecSMatthew G. Knepley     if (n[2] < 0.0) {
40199dec3a6SMatthew G. Knepley       if (coordSize > 9) {
40299dec3a6SMatthew G. Knepley         coords[2] = PetscRealPart(coords[3*dim+0] - coords[0*dim+0]);
40308b58242SMatthew G. Knepley         coords[3] = PetscRealPart(coords[3*dim+1] - coords[0*dim+1]);
40499dec3a6SMatthew G. Knepley         coords[4] = x2[0];
40599dec3a6SMatthew G. Knepley         coords[5] = x2[1];
40699dec3a6SMatthew G. Knepley         coords[6] = x1[0];
40799dec3a6SMatthew G. Knepley         coords[7] = x1[1];
40899dec3a6SMatthew G. Knepley       } else {
40973868372SMatthew G. Knepley         coords[2] = x2[0];
41073868372SMatthew G. Knepley         coords[3] = x2[1];
41173868372SMatthew G. Knepley         coords[4] = x1[0];
41273868372SMatthew G. Knepley         coords[5] = x1[1];
41399dec3a6SMatthew G. Knepley       }
414b7ad821dSMatthew G. Knepley       R[0] = 1.0; R[1] = 0.0; R[2] = 0.0;
415b7ad821dSMatthew G. Knepley       R[3] = 0.0; R[4] = 1.0; R[5] = 0.0;
416b7ad821dSMatthew G. Knepley       R[6] = 0.0; R[7] = 0.0; R[8] = -1.0;
41773868372SMatthew G. Knepley     } else {
41899dec3a6SMatthew G. Knepley       for (p = 3; p < coordSize/3; ++p) {
41999dec3a6SMatthew G. Knepley         coords[p*2+0] = PetscRealPart(coords[p*dim+0] - coords[0*dim+0]);
42099dec3a6SMatthew G. Knepley         coords[p*2+1] = PetscRealPart(coords[p*dim+1] - coords[0*dim+1]);
42199dec3a6SMatthew G. Knepley       }
42273868372SMatthew G. Knepley       coords[2] = x1[0];
42373868372SMatthew G. Knepley       coords[3] = x1[1];
42473868372SMatthew G. Knepley       coords[4] = x2[0];
42573868372SMatthew G. Knepley       coords[5] = x2[1];
426b7ad821dSMatthew G. Knepley       R[0] = 1.0; R[1] = 0.0; R[2] = 0.0;
427b7ad821dSMatthew G. Knepley       R[3] = 0.0; R[4] = 1.0; R[5] = 0.0;
428b7ad821dSMatthew G. Knepley       R[6] = 0.0; R[7] = 0.0; R[8] = 1.0;
42973868372SMatthew G. Knepley     }
43099dec3a6SMatthew G. Knepley     coords[0] = 0.0;
43199dec3a6SMatthew G. Knepley     coords[1] = 0.0;
43273868372SMatthew G. Knepley     PetscFunctionReturn(0);
43373868372SMatthew G. Knepley   }
434da18b5e6SMatthew G Knepley   alpha = 1.0/sqrtz;
435ccd2543fSMatthew G Knepley   R[0] =  alpha*n[0]*n[2]; R[1] = alpha*n[1]*n[2]; R[2] = -sqrtz;
436ccd2543fSMatthew G Knepley   R[3] = -alpha*n[1];      R[4] = alpha*n[0];      R[5] = 0.0;
437ccd2543fSMatthew G Knepley   R[6] =  n[0];            R[7] = n[1];            R[8] = n[2];
438ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
439ccd2543fSMatthew G Knepley     x1p[d] = 0.0;
440ccd2543fSMatthew G Knepley     x2p[d] = 0.0;
441ccd2543fSMatthew G Knepley     for (e = 0; e < dim; ++e) {
442ccd2543fSMatthew G Knepley       x1p[d] += R[d*dim+e]*x1[e];
443ccd2543fSMatthew G Knepley       x2p[d] += R[d*dim+e]*x2[e];
444ccd2543fSMatthew G Knepley     }
445ccd2543fSMatthew G Knepley   }
4468763be8eSMatthew G. Knepley   if (PetscAbsReal(x1p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
4478763be8eSMatthew G. Knepley   if (PetscAbsReal(x2p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
448ccd2543fSMatthew G Knepley   /* 2) Project to (x, y) */
44999dec3a6SMatthew G. Knepley   for (p = 3; p < coordSize/3; ++p) {
45099dec3a6SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
45199dec3a6SMatthew G. Knepley       xnp[d] = 0.0;
45299dec3a6SMatthew G. Knepley       for (e = 0; e < dim; ++e) {
45399dec3a6SMatthew G. Knepley         xnp[d] += R[d*dim+e]*PetscRealPart(coords[p*dim+e] - coords[0*dim+e]);
45499dec3a6SMatthew G. Knepley       }
45599dec3a6SMatthew G. Knepley       if (d < dim-1) coords[p*2+d] = xnp[d];
45699dec3a6SMatthew G. Knepley     }
45799dec3a6SMatthew G. Knepley   }
458ccd2543fSMatthew G Knepley   coords[0] = 0.0;
459ccd2543fSMatthew G Knepley   coords[1] = 0.0;
460ccd2543fSMatthew G Knepley   coords[2] = x1p[0];
461ccd2543fSMatthew G Knepley   coords[3] = x1p[1];
462ccd2543fSMatthew G Knepley   coords[4] = x2p[0];
463ccd2543fSMatthew G Knepley   coords[5] = x2p[1];
4647f07f362SMatthew G. Knepley   /* Output R^T which rotates \hat z to the input normal */
4657f07f362SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
4667f07f362SMatthew G. Knepley     for (e = d+1; e < dim; ++e) {
4677f07f362SMatthew G. Knepley       PetscReal tmp;
4687f07f362SMatthew G. Knepley 
4697f07f362SMatthew G. Knepley       tmp        = R[d*dim+e];
4707f07f362SMatthew G. Knepley       R[d*dim+e] = R[e*dim+d];
4717f07f362SMatthew G. Knepley       R[e*dim+d] = tmp;
4727f07f362SMatthew G. Knepley     }
4737f07f362SMatthew G. Knepley   }
474ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
475ccd2543fSMatthew G Knepley }
476ccd2543fSMatthew G Knepley 
477ccd2543fSMatthew G Knepley #undef __FUNCT__
478834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Internal"
4796322fe33SJed Brown PETSC_UNUSED
480834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[])
481834e62ceSMatthew G. Knepley {
482834e62ceSMatthew G. Knepley   /* Signed volume is 1/2 the determinant
483834e62ceSMatthew G. Knepley 
484834e62ceSMatthew G. Knepley    |  1  1  1 |
485834e62ceSMatthew G. Knepley    | x0 x1 x2 |
486834e62ceSMatthew G. Knepley    | y0 y1 y2 |
487834e62ceSMatthew G. Knepley 
488834e62ceSMatthew G. Knepley      but if x0,y0 is the origin, we have
489834e62ceSMatthew G. Knepley 
490834e62ceSMatthew G. Knepley    | x1 x2 |
491834e62ceSMatthew G. Knepley    | y1 y2 |
492834e62ceSMatthew G. Knepley   */
493834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1];
494834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1];
495834e62ceSMatthew G. Knepley   PetscReal       M[4], detM;
496834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2;
49786623015SMatthew G. Knepley   M[2] = y1; M[3] = y2;
498923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(&detM, M);
499834e62ceSMatthew G. Knepley   *vol = 0.5*detM;
500834e62ceSMatthew G. Knepley   PetscLogFlops(5.0);
501834e62ceSMatthew G. Knepley }
502834e62ceSMatthew G. Knepley 
503834e62ceSMatthew G. Knepley #undef __FUNCT__
504834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Origin_Internal"
505834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Origin_Internal(PetscReal *vol, PetscReal coords[])
506834e62ceSMatthew G. Knepley {
507923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(vol, coords);
508834e62ceSMatthew G. Knepley   *vol *= 0.5;
509834e62ceSMatthew G. Knepley }
510834e62ceSMatthew G. Knepley 
511834e62ceSMatthew G. Knepley #undef __FUNCT__
512834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Internal"
5136322fe33SJed Brown PETSC_UNUSED
514834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[])
515834e62ceSMatthew G. Knepley {
516834e62ceSMatthew G. Knepley   /* Signed volume is 1/6th of the determinant
517834e62ceSMatthew G. Knepley 
518834e62ceSMatthew G. Knepley    |  1  1  1  1 |
519834e62ceSMatthew G. Knepley    | x0 x1 x2 x3 |
520834e62ceSMatthew G. Knepley    | y0 y1 y2 y3 |
521834e62ceSMatthew G. Knepley    | z0 z1 z2 z3 |
522834e62ceSMatthew G. Knepley 
523834e62ceSMatthew G. Knepley      but if x0,y0,z0 is the origin, we have
524834e62ceSMatthew G. Knepley 
525834e62ceSMatthew G. Knepley    | x1 x2 x3 |
526834e62ceSMatthew G. Knepley    | y1 y2 y3 |
527834e62ceSMatthew G. Knepley    | z1 z2 z3 |
528834e62ceSMatthew G. Knepley   */
529834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[3] - coords[0], y1 = coords[4]  - coords[1], z1 = coords[5]  - coords[2];
530834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[6] - coords[0], y2 = coords[7]  - coords[1], z2 = coords[8]  - coords[2];
531834e62ceSMatthew G. Knepley   const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2];
532834e62ceSMatthew G. Knepley   PetscReal       M[9], detM;
533834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2; M[2] = x3;
534834e62ceSMatthew G. Knepley   M[3] = y1; M[4] = y2; M[5] = y3;
535834e62ceSMatthew G. Knepley   M[6] = z1; M[7] = z2; M[8] = z3;
536923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(&detM, M);
537b7ad821dSMatthew G. Knepley   *vol = -0.16666666666666666666666*detM;
538834e62ceSMatthew G. Knepley   PetscLogFlops(10.0);
539834e62ceSMatthew G. Knepley }
540834e62ceSMatthew G. Knepley 
541834e62ceSMatthew G. Knepley #undef __FUNCT__
5420ec8681fSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Origin_Internal"
5430ec8681fSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[])
5440ec8681fSMatthew G. Knepley {
545923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(vol, coords);
546b7ad821dSMatthew G. Knepley   *vol *= -0.16666666666666666666666;
5470ec8681fSMatthew G. Knepley }
5480ec8681fSMatthew G. Knepley 
5490ec8681fSMatthew G. Knepley #undef __FUNCT__
55017fe8556SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeLineGeometry_Internal"
55117fe8556SMatthew G. Knepley static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
55217fe8556SMatthew G. Knepley {
55317fe8556SMatthew G. Knepley   PetscSection   coordSection;
55417fe8556SMatthew G. Knepley   Vec            coordinates;
555a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
5567f07f362SMatthew G. Knepley   PetscInt       numCoords, d;
55717fe8556SMatthew G. Knepley   PetscErrorCode ierr;
55817fe8556SMatthew G. Knepley 
55917fe8556SMatthew G. Knepley   PetscFunctionBegin;
56017fe8556SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
56169d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
56217fe8556SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
5637f07f362SMatthew G. Knepley   *detJ = 0.0;
56428dbe442SToby Isaac   if (numCoords == 6) {
56528dbe442SToby Isaac     const PetscInt dim = 3;
56628dbe442SToby Isaac     PetscReal      R[9], J0;
56728dbe442SToby Isaac 
56828dbe442SToby Isaac     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
56928dbe442SToby Isaac     ierr = DMPlexComputeProjection3Dto1D_Internal(coords, R);CHKERRQ(ierr);
57028dbe442SToby Isaac     if (J)    {
57128dbe442SToby Isaac       J0   = 0.5*PetscRealPart(coords[1]);
57228dbe442SToby Isaac       J[0] = R[0]*J0; J[1] = R[1]; J[2] = R[2];
57328dbe442SToby Isaac       J[3] = R[3]*J0; J[4] = R[4]; J[5] = R[5];
57428dbe442SToby Isaac       J[6] = R[6]*J0; J[7] = R[7]; J[8] = R[8];
57528dbe442SToby Isaac       DMPlex_Det3D_Internal(detJ, J);
57628dbe442SToby Isaac     }
57728dbe442SToby Isaac     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
57828dbe442SToby Isaac   } else if (numCoords == 4) {
5797f07f362SMatthew G. Knepley     const PetscInt dim = 2;
5807f07f362SMatthew G. Knepley     PetscReal      R[4], J0;
5817f07f362SMatthew G. Knepley 
5827f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
5837f07f362SMatthew G. Knepley     ierr = DMPlexComputeProjection2Dto1D_Internal(coords, R);CHKERRQ(ierr);
58417fe8556SMatthew G. Knepley     if (J)    {
5857f07f362SMatthew G. Knepley       J0   = 0.5*PetscRealPart(coords[1]);
5867f07f362SMatthew G. Knepley       J[0] = R[0]*J0; J[1] = R[1];
5877f07f362SMatthew G. Knepley       J[2] = R[2]*J0; J[3] = R[3];
588923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
58917fe8556SMatthew G. Knepley     }
590923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
5917f07f362SMatthew G. Knepley   } else if (numCoords == 2) {
5927f07f362SMatthew G. Knepley     const PetscInt dim = 1;
5937f07f362SMatthew G. Knepley 
5947f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
5957f07f362SMatthew G. Knepley     if (J)    {
5967f07f362SMatthew G. Knepley       J[0]  = 0.5*(PetscRealPart(coords[1]) - PetscRealPart(coords[0]));
59717fe8556SMatthew G. Knepley       *detJ = J[0];
59817fe8556SMatthew G. Knepley       PetscLogFlops(2.0);
59917fe8556SMatthew G. Knepley     }
6007f07f362SMatthew G. Knepley     if (invJ) {invJ[0] = 1.0/J[0]; PetscLogFlops(1.0);}
601796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this segment is %D != 2", numCoords);
60217fe8556SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
60317fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
60417fe8556SMatthew G. Knepley }
60517fe8556SMatthew G. Knepley 
60617fe8556SMatthew G. Knepley #undef __FUNCT__
607ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTriangleGeometry_Internal"
608ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
609ccd2543fSMatthew G Knepley {
610ccd2543fSMatthew G Knepley   PetscSection   coordSection;
611ccd2543fSMatthew G Knepley   Vec            coordinates;
612a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
6137f07f362SMatthew G. Knepley   PetscInt       numCoords, d, f, g;
614ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
615ccd2543fSMatthew G Knepley 
616ccd2543fSMatthew G Knepley   PetscFunctionBegin;
617ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
61869d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
619ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
6207f07f362SMatthew G. Knepley   *detJ = 0.0;
621ccd2543fSMatthew G Knepley   if (numCoords == 9) {
6227f07f362SMatthew G. Knepley     const PetscInt dim = 3;
6237f07f362SMatthew 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};
6247f07f362SMatthew G. Knepley 
6257f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
62699dec3a6SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D_Internal(numCoords, coords, R);CHKERRQ(ierr);
6277f07f362SMatthew G. Knepley     if (J)    {
628b7ad821dSMatthew G. Knepley       const PetscInt pdim = 2;
629b7ad821dSMatthew G. Knepley 
630b7ad821dSMatthew G. Knepley       for (d = 0; d < pdim; d++) {
631b7ad821dSMatthew G. Knepley         for (f = 0; f < pdim; f++) {
632b7ad821dSMatthew G. Knepley           J0[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
633ccd2543fSMatthew G Knepley         }
6347f07f362SMatthew G. Knepley       }
6357f07f362SMatthew G. Knepley       PetscLogFlops(8.0);
636923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
6377f07f362SMatthew G. Knepley       for (d = 0; d < dim; d++) {
6387f07f362SMatthew G. Knepley         for (f = 0; f < dim; f++) {
6397f07f362SMatthew G. Knepley           J[d*dim+f] = 0.0;
6407f07f362SMatthew G. Knepley           for (g = 0; g < dim; g++) {
6417f07f362SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
6427f07f362SMatthew G. Knepley           }
6437f07f362SMatthew G. Knepley         }
6447f07f362SMatthew G. Knepley       }
6457f07f362SMatthew G. Knepley       PetscLogFlops(18.0);
6467f07f362SMatthew G. Knepley     }
647923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
6487f07f362SMatthew G. Knepley   } else if (numCoords == 6) {
6497f07f362SMatthew G. Knepley     const PetscInt dim = 2;
6507f07f362SMatthew G. Knepley 
6517f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
652ccd2543fSMatthew G Knepley     if (J)    {
653ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
654ccd2543fSMatthew G Knepley         for (f = 0; f < dim; f++) {
655ccd2543fSMatthew G Knepley           J[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*dim+d]) - PetscRealPart(coords[0*dim+d]));
656ccd2543fSMatthew G Knepley         }
657ccd2543fSMatthew G Knepley       }
6587f07f362SMatthew G. Knepley       PetscLogFlops(8.0);
659923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
660ccd2543fSMatthew G Knepley     }
661923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
662796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %D != 6 or 9", numCoords);
663ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
664ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
665ccd2543fSMatthew G Knepley }
666ccd2543fSMatthew G Knepley 
667ccd2543fSMatthew G Knepley #undef __FUNCT__
668ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeRectangleGeometry_Internal"
669ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
670ccd2543fSMatthew G Knepley {
671ccd2543fSMatthew G Knepley   PetscSection   coordSection;
672ccd2543fSMatthew G Knepley   Vec            coordinates;
673a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
67499dec3a6SMatthew G. Knepley   PetscInt       numCoords, d, f, g;
675ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
676ccd2543fSMatthew G Knepley 
677ccd2543fSMatthew G Knepley   PetscFunctionBegin;
678ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
67969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
68099dec3a6SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
6817f07f362SMatthew G. Knepley   *detJ = 0.0;
68299dec3a6SMatthew G. Knepley   if (numCoords == 12) {
68399dec3a6SMatthew G. Knepley     const PetscInt dim = 3;
68499dec3a6SMatthew 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};
68599dec3a6SMatthew G. Knepley 
68699dec3a6SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
68799dec3a6SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D_Internal(numCoords, coords, R);CHKERRQ(ierr);
68899dec3a6SMatthew G. Knepley     if (J)    {
68999dec3a6SMatthew G. Knepley       const PetscInt pdim = 2;
69099dec3a6SMatthew G. Knepley 
69199dec3a6SMatthew G. Knepley       for (d = 0; d < pdim; d++) {
69299dec3a6SMatthew G. Knepley         J0[d*dim+0] = 0.5*(PetscRealPart(coords[1*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
69399dec3a6SMatthew G. Knepley         J0[d*dim+1] = 0.5*(PetscRealPart(coords[3*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
69499dec3a6SMatthew G. Knepley       }
69599dec3a6SMatthew G. Knepley       PetscLogFlops(8.0);
696923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
69799dec3a6SMatthew G. Knepley       for (d = 0; d < dim; d++) {
69899dec3a6SMatthew G. Knepley         for (f = 0; f < dim; f++) {
69999dec3a6SMatthew G. Knepley           J[d*dim+f] = 0.0;
70099dec3a6SMatthew G. Knepley           for (g = 0; g < dim; g++) {
70199dec3a6SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
70299dec3a6SMatthew G. Knepley           }
70399dec3a6SMatthew G. Knepley         }
70499dec3a6SMatthew G. Knepley       }
70599dec3a6SMatthew G. Knepley       PetscLogFlops(18.0);
70699dec3a6SMatthew G. Knepley     }
707923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
70897f1a218SMatthew G. Knepley   } else if ((numCoords == 8) || (numCoords == 16)) {
70999dec3a6SMatthew G. Knepley     const PetscInt dim = 2;
71099dec3a6SMatthew G. Knepley 
7117f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
712ccd2543fSMatthew G Knepley     if (J)    {
713ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
71499dec3a6SMatthew G. Knepley         J[d*dim+0] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
71599dec3a6SMatthew G. Knepley         J[d*dim+1] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
716ccd2543fSMatthew G Knepley       }
7177f07f362SMatthew G. Knepley       PetscLogFlops(8.0);
718923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
719ccd2543fSMatthew G Knepley     }
720923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
721796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
72299dec3a6SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
723ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
724ccd2543fSMatthew G Knepley }
725ccd2543fSMatthew G Knepley 
726ccd2543fSMatthew G Knepley #undef __FUNCT__
727ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTetrahedronGeometry_Internal"
728ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
729ccd2543fSMatthew G Knepley {
730ccd2543fSMatthew G Knepley   PetscSection   coordSection;
731ccd2543fSMatthew G Knepley   Vec            coordinates;
732a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
733ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
73499dec3a6SMatthew G. Knepley   PetscInt       d;
735ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
736ccd2543fSMatthew G Knepley 
737ccd2543fSMatthew G Knepley   PetscFunctionBegin;
738ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
73969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
740ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
7417f07f362SMatthew G. Knepley   *detJ = 0.0;
7427f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
743ccd2543fSMatthew G Knepley   if (J)    {
744ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
745f0df753eSMatthew G. Knepley       /* I orient with outward face normals */
746f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d]));
747f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
748f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
749ccd2543fSMatthew G Knepley     }
7507f07f362SMatthew G. Knepley     PetscLogFlops(18.0);
751923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
752ccd2543fSMatthew G Knepley   }
753923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
754ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
755ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
756ccd2543fSMatthew G Knepley }
757ccd2543fSMatthew G Knepley 
758ccd2543fSMatthew G Knepley #undef __FUNCT__
759ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeHexahedronGeometry_Internal"
760ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
761ccd2543fSMatthew G Knepley {
762ccd2543fSMatthew G Knepley   PetscSection   coordSection;
763ccd2543fSMatthew G Knepley   Vec            coordinates;
764a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
765ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
766ccd2543fSMatthew G Knepley   PetscInt       d;
767ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
768ccd2543fSMatthew G Knepley 
769ccd2543fSMatthew G Knepley   PetscFunctionBegin;
770ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
77169d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
772ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
7737f07f362SMatthew G. Knepley   *detJ = 0.0;
7747f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
775ccd2543fSMatthew G Knepley   if (J)    {
776ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
777f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
778f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
779f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d]));
780ccd2543fSMatthew G Knepley     }
7817f07f362SMatthew G. Knepley     PetscLogFlops(18.0);
782923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
783ccd2543fSMatthew G Knepley   }
784923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
785ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
786ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
787ccd2543fSMatthew G Knepley }
788ccd2543fSMatthew G Knepley 
789ccd2543fSMatthew G Knepley #undef __FUNCT__
7908e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryAffineFEM"
791ccd2543fSMatthew G Knepley /*@C
7928e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell
793ccd2543fSMatthew G Knepley 
794ccd2543fSMatthew G Knepley   Collective on DM
795ccd2543fSMatthew G Knepley 
796ccd2543fSMatthew G Knepley   Input Arguments:
797ccd2543fSMatthew G Knepley + dm   - the DM
798ccd2543fSMatthew G Knepley - cell - the cell
799ccd2543fSMatthew G Knepley 
800ccd2543fSMatthew G Knepley   Output Arguments:
801ccd2543fSMatthew G Knepley + v0   - the translation part of this affine transform
802ccd2543fSMatthew G Knepley . J    - the Jacobian of the transform from the reference element
803ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian
804ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant
805ccd2543fSMatthew G Knepley 
806ccd2543fSMatthew G Knepley   Level: advanced
807ccd2543fSMatthew G Knepley 
808ccd2543fSMatthew G Knepley   Fortran Notes:
809ccd2543fSMatthew G Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
810ccd2543fSMatthew G Knepley   include petsc.h90 in your code.
811ccd2543fSMatthew G Knepley 
8128e0841e0SMatthew G. Knepley .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinateVec()
813ccd2543fSMatthew G Knepley @*/
8148e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
815ccd2543fSMatthew G Knepley {
81649dc4407SMatthew G. Knepley   PetscInt       depth, dim, coneSize;
817ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
818ccd2543fSMatthew G Knepley 
819ccd2543fSMatthew G Knepley   PetscFunctionBegin;
820139a35ccSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
821ccd2543fSMatthew G Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
82249dc4407SMatthew G. Knepley   if (depth == 1) {
8238e0841e0SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
8248e0841e0SMatthew G. Knepley   } else {
8258e0841e0SMatthew G. Knepley     DMLabel depth;
8268e0841e0SMatthew G. Knepley 
8278e0841e0SMatthew G. Knepley     ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
8288e0841e0SMatthew G. Knepley     ierr = DMLabelGetValue(depth, cell, &dim);CHKERRQ(ierr);
8298e0841e0SMatthew G. Knepley   }
830ccd2543fSMatthew G Knepley   switch (dim) {
83117fe8556SMatthew G. Knepley   case 1:
83217fe8556SMatthew G. Knepley     ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
83317fe8556SMatthew G. Knepley     break;
834ccd2543fSMatthew G Knepley   case 2:
835ccd2543fSMatthew G Knepley     switch (coneSize) {
836ccd2543fSMatthew G Knepley     case 3:
837ccd2543fSMatthew G Knepley       ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
838ccd2543fSMatthew G Knepley       break;
839ccd2543fSMatthew G Knepley     case 4:
840ccd2543fSMatthew G Knepley       ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
841ccd2543fSMatthew G Knepley       break;
842ccd2543fSMatthew G Knepley     default:
8438e0841e0SMatthew G. Knepley       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
844ccd2543fSMatthew G Knepley     }
845ccd2543fSMatthew G Knepley     break;
846ccd2543fSMatthew G Knepley   case 3:
847ccd2543fSMatthew G Knepley     switch (coneSize) {
848ccd2543fSMatthew G Knepley     case 4:
849ccd2543fSMatthew G Knepley       ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
850ccd2543fSMatthew G Knepley       break;
8518e0841e0SMatthew G. Knepley     case 6: /* Faces */
8528e0841e0SMatthew G. Knepley     case 8: /* Vertices */
853ccd2543fSMatthew G Knepley       ierr = DMPlexComputeHexahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
854ccd2543fSMatthew G Knepley       break;
855ccd2543fSMatthew G Knepley     default:
8568e0841e0SMatthew G. Knepley         SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
857ccd2543fSMatthew G Knepley     }
858ccd2543fSMatthew G Knepley       break;
859ccd2543fSMatthew G Knepley   default:
860ccd2543fSMatthew G Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
861ccd2543fSMatthew G Knepley   }
8628e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
8638e0841e0SMatthew G. Knepley }
8648e0841e0SMatthew G. Knepley 
8658e0841e0SMatthew G. Knepley #undef __FUNCT__
8668e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIsoparametricGeometry_Internal"
8678e0841e0SMatthew G. Knepley static PetscErrorCode DMPlexComputeIsoparametricGeometry_Internal(DM dm, PetscFE fe, PetscInt point, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
8688e0841e0SMatthew G. Knepley {
8698e0841e0SMatthew G. Knepley   PetscQuadrature  quad;
8708e0841e0SMatthew G. Knepley   PetscSection     coordSection;
8718e0841e0SMatthew G. Knepley   Vec              coordinates;
8728e0841e0SMatthew G. Knepley   PetscScalar     *coords = NULL;
8738e0841e0SMatthew G. Knepley   const PetscReal *quadPoints;
8748e0841e0SMatthew G. Knepley   PetscReal       *basisDer;
8758e0841e0SMatthew G. Knepley   PetscInt         dim, cdim, pdim, qdim, Nq, numCoords, d, q;
8768e0841e0SMatthew G. Knepley   PetscErrorCode   ierr;
8778e0841e0SMatthew G. Knepley 
8788e0841e0SMatthew G. Knepley   PetscFunctionBegin;
8798e0841e0SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
8808e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
8818e0841e0SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
8828e0841e0SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
8838e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
8848e0841e0SMatthew G. Knepley   ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
885954b1791SMatthew G. Knepley   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
8868e0841e0SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, &qdim, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
8878e0841e0SMatthew G. Knepley   ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr);
8888e0841e0SMatthew G. Knepley   *detJ = 0.0;
8898e0841e0SMatthew G. Knepley   if (qdim != dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim);
8908e0841e0SMatthew 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);
8918e0841e0SMatthew G. Knepley   if (v0) {for (d = 0; d < cdim; d++) v0[d] = PetscRealPart(coords[d]);}
8928e0841e0SMatthew G. Knepley   if (J) {
8938e0841e0SMatthew G. Knepley     for (q = 0; q < Nq; ++q) {
8948e0841e0SMatthew G. Knepley       PetscInt i, j, k, c, r;
8958e0841e0SMatthew G. Knepley 
8968e0841e0SMatthew G. Knepley       /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */
8978e0841e0SMatthew G. Knepley       for (k = 0; k < pdim; ++k)
8988e0841e0SMatthew G. Knepley         for (j = 0; j < dim; ++j)
8998e0841e0SMatthew G. Knepley           for (i = 0; i < cdim; ++i)
90071d6e60fSMatthew G. Knepley             J[(q*cdim + i)*dim + j] += basisDer[(q*pdim + k)*dim + j] * PetscRealPart(coords[k*cdim + i]);
9018e0841e0SMatthew G. Knepley       PetscLogFlops(2.0*pdim*dim*cdim);
9028e0841e0SMatthew G. Knepley       if (cdim > dim) {
9038e0841e0SMatthew G. Knepley         for (c = dim; c < cdim; ++c)
9048e0841e0SMatthew G. Knepley           for (r = 0; r < cdim; ++r)
9058e0841e0SMatthew G. Knepley             J[r*cdim+c] = r == c ? 1.0 : 0.0;
9068e0841e0SMatthew G. Knepley       }
9078e0841e0SMatthew G. Knepley       switch (cdim) {
9088e0841e0SMatthew G. Knepley       case 3:
9098e0841e0SMatthew G. Knepley         DMPlex_Det3D_Internal(detJ, J);
9108e0841e0SMatthew G. Knepley         if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
91117fe8556SMatthew G. Knepley         break;
91249dc4407SMatthew G. Knepley       case 2:
9138e0841e0SMatthew G. Knepley         DMPlex_Det2D_Internal(detJ, J);
9148e0841e0SMatthew G. Knepley         if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
91549dc4407SMatthew G. Knepley         break;
9168e0841e0SMatthew G. Knepley       case 1:
9178e0841e0SMatthew G. Knepley         *detJ = J[0];
9188e0841e0SMatthew G. Knepley         if (invJ) invJ[0] = 1.0/J[0];
91949dc4407SMatthew G. Knepley       }
92049dc4407SMatthew G. Knepley     }
9218e0841e0SMatthew G. Knepley   }
9228e0841e0SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
9238e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
9248e0841e0SMatthew G. Knepley }
9258e0841e0SMatthew G. Knepley 
9268e0841e0SMatthew G. Knepley #undef __FUNCT__
9278e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFEM"
9288e0841e0SMatthew G. Knepley /*@C
9298e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell
9308e0841e0SMatthew G. Knepley 
9318e0841e0SMatthew G. Knepley   Collective on DM
9328e0841e0SMatthew G. Knepley 
9338e0841e0SMatthew G. Knepley   Input Arguments:
9348e0841e0SMatthew G. Knepley + dm   - the DM
9358e0841e0SMatthew G. Knepley . cell - the cell
9368e0841e0SMatthew G. Knepley - fe   - the finite element containing the quadrature
9378e0841e0SMatthew G. Knepley 
9388e0841e0SMatthew G. Knepley   Output Arguments:
9398e0841e0SMatthew G. Knepley + v0   - the translation part of this transform
9408e0841e0SMatthew G. Knepley . J    - the Jacobian of the transform from the reference element at each quadrature point
9418e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point
9428e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point
9438e0841e0SMatthew G. Knepley 
9448e0841e0SMatthew G. Knepley   Level: advanced
9458e0841e0SMatthew G. Knepley 
9468e0841e0SMatthew G. Knepley   Fortran Notes:
9478e0841e0SMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
9488e0841e0SMatthew G. Knepley   include petsc.h90 in your code.
9498e0841e0SMatthew G. Knepley 
9508e0841e0SMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec()
9518e0841e0SMatthew G. Knepley @*/
9528e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscFE fe, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
9538e0841e0SMatthew G. Knepley {
9548e0841e0SMatthew G. Knepley   PetscErrorCode ierr;
9558e0841e0SMatthew G. Knepley 
9568e0841e0SMatthew G. Knepley   PetscFunctionBegin;
9578e0841e0SMatthew G. Knepley   if (!fe) {ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);}
9588e0841e0SMatthew G. Knepley   else     {ierr = DMPlexComputeIsoparametricGeometry_Internal(dm, fe, cell, v0, J, invJ, detJ);CHKERRQ(ierr);}
959ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
960ccd2543fSMatthew G Knepley }
961834e62ceSMatthew G. Knepley 
962834e62ceSMatthew G. Knepley #undef __FUNCT__
963cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_1D_Internal"
964011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
965cc08537eSMatthew G. Knepley {
966cc08537eSMatthew G. Knepley   PetscSection   coordSection;
967cc08537eSMatthew G. Knepley   Vec            coordinates;
968a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
96906e2781eSMatthew G. Knepley   PetscScalar    tmp[2];
970cc08537eSMatthew G. Knepley   PetscInt       coordSize;
971cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
972cc08537eSMatthew G. Knepley 
973cc08537eSMatthew G. Knepley   PetscFunctionBegin;
974cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
97569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
976cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
977011ea5d8SMatthew G. Knepley   if (dim != 2) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "We only support 2D edges right now");
97806e2781eSMatthew G. Knepley   ierr = DMPlexLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp);CHKERRQ(ierr);
979cc08537eSMatthew G. Knepley   if (centroid) {
98006e2781eSMatthew G. Knepley     centroid[0] = 0.5*PetscRealPart(coords[0] + tmp[0]);
98106e2781eSMatthew G. Knepley     centroid[1] = 0.5*PetscRealPart(coords[1] + tmp[1]);
982cc08537eSMatthew G. Knepley   }
983cc08537eSMatthew G. Knepley   if (normal) {
984a60a936bSMatthew G. Knepley     PetscReal norm;
985a60a936bSMatthew G. Knepley 
98606e2781eSMatthew G. Knepley     normal[0]  = -PetscRealPart(coords[1] - tmp[1]);
98706e2781eSMatthew G. Knepley     normal[1]  =  PetscRealPart(coords[0] - tmp[0]);
988a60a936bSMatthew G. Knepley     norm       = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1]);
989a60a936bSMatthew G. Knepley     normal[0] /= norm;
990a60a936bSMatthew G. Knepley     normal[1] /= norm;
991cc08537eSMatthew G. Knepley   }
992cc08537eSMatthew G. Knepley   if (vol) {
99306e2781eSMatthew G. Knepley     *vol = PetscSqrtReal(PetscSqr(PetscRealPart(coords[0] - tmp[0])) + PetscSqr(PetscRealPart(coords[1] - tmp[1])));
994cc08537eSMatthew G. Knepley   }
995cc08537eSMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
996cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
997cc08537eSMatthew G. Knepley }
998cc08537eSMatthew G. Knepley 
999cc08537eSMatthew G. Knepley #undef __FUNCT__
1000cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_2D_Internal"
1001cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i ) / A */
1002011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1003cc08537eSMatthew G. Knepley {
1004cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1005cc08537eSMatthew G. Knepley   Vec            coordinates;
1006cc08537eSMatthew G. Knepley   PetscScalar   *coords = NULL;
10070a1d6728SMatthew G. Knepley   PetscReal      vsum = 0.0, csum[3] = {0.0, 0.0, 0.0}, vtmp, ctmp[4], v0[3], R[9];
10080a1d6728SMatthew G. Knepley   PetscInt       tdim = 2, coordSize, numCorners, p, d, e;
1009cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1010cc08537eSMatthew G. Knepley 
1011cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1012cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
10130a1d6728SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numCorners);CHKERRQ(ierr);
101469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1015cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
10160bce18caSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1017011ea5d8SMatthew G. Knepley   if (normal) {
1018011ea5d8SMatthew G. Knepley     if (dim > 2) {
10191ee9d5ecSMatthew G. Knepley       const PetscReal x0 = PetscRealPart(coords[dim+0] - coords[0]), x1 = PetscRealPart(coords[dim*2+0] - coords[0]);
10201ee9d5ecSMatthew G. Knepley       const PetscReal y0 = PetscRealPart(coords[dim+1] - coords[1]), y1 = PetscRealPart(coords[dim*2+1] - coords[1]);
10211ee9d5ecSMatthew G. Knepley       const PetscReal z0 = PetscRealPart(coords[dim+2] - coords[2]), z1 = PetscRealPart(coords[dim*2+2] - coords[2]);
10220a1d6728SMatthew G. Knepley       PetscReal       norm;
10230a1d6728SMatthew G. Knepley 
10241ee9d5ecSMatthew G. Knepley       v0[0]     = PetscRealPart(coords[0]);
10251ee9d5ecSMatthew G. Knepley       v0[1]     = PetscRealPart(coords[1]);
10261ee9d5ecSMatthew G. Knepley       v0[2]     = PetscRealPart(coords[2]);
10270a1d6728SMatthew G. Knepley       normal[0] = y0*z1 - z0*y1;
10280a1d6728SMatthew G. Knepley       normal[1] = z0*x1 - x0*z1;
10290a1d6728SMatthew G. Knepley       normal[2] = x0*y1 - y0*x1;
10308b49ba18SBarry Smith       norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2]);
10310a1d6728SMatthew G. Knepley       normal[0] /= norm;
10320a1d6728SMatthew G. Knepley       normal[1] /= norm;
10330a1d6728SMatthew G. Knepley       normal[2] /= norm;
1034011ea5d8SMatthew G. Knepley     } else {
1035011ea5d8SMatthew G. Knepley       for (d = 0; d < dim; ++d) normal[d] = 0.0;
1036011ea5d8SMatthew G. Knepley     }
1037011ea5d8SMatthew G. Knepley   }
103899dec3a6SMatthew G. Knepley   if (dim == 3) {ierr = DMPlexComputeProjection3Dto2D_Internal(coordSize, coords, R);CHKERRQ(ierr);}
10390a1d6728SMatthew G. Knepley   for (p = 0; p < numCorners; ++p) {
10400a1d6728SMatthew G. Knepley     /* Need to do this copy to get types right */
10410a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
10421ee9d5ecSMatthew G. Knepley       ctmp[d]      = PetscRealPart(coords[p*tdim+d]);
10431ee9d5ecSMatthew G. Knepley       ctmp[tdim+d] = PetscRealPart(coords[((p+1)%numCorners)*tdim+d]);
10440a1d6728SMatthew G. Knepley     }
10450a1d6728SMatthew G. Knepley     Volume_Triangle_Origin_Internal(&vtmp, ctmp);
10460a1d6728SMatthew G. Knepley     vsum += vtmp;
10470a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
10480a1d6728SMatthew G. Knepley       csum[d] += (ctmp[d] + ctmp[tdim+d])*vtmp;
10490a1d6728SMatthew G. Knepley     }
10500a1d6728SMatthew G. Knepley   }
10510a1d6728SMatthew G. Knepley   for (d = 0; d < tdim; ++d) {
10520a1d6728SMatthew G. Knepley     csum[d] /= (tdim+1)*vsum;
10530a1d6728SMatthew G. Knepley   }
10540a1d6728SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1055ee6bbdb2SSatish Balay   if (vol) *vol = PetscAbsReal(vsum);
10560a1d6728SMatthew G. Knepley   if (centroid) {
10570a1d6728SMatthew G. Knepley     if (dim > 2) {
10580a1d6728SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
10590a1d6728SMatthew G. Knepley         centroid[d] = v0[d];
10600a1d6728SMatthew G. Knepley         for (e = 0; e < dim; ++e) {
10610a1d6728SMatthew G. Knepley           centroid[d] += R[d*dim+e]*csum[e];
10620a1d6728SMatthew G. Knepley         }
10630a1d6728SMatthew G. Knepley       }
10640a1d6728SMatthew G. Knepley     } else for (d = 0; d < dim; ++d) centroid[d] = csum[d];
10650a1d6728SMatthew G. Knepley   }
1066cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1067cc08537eSMatthew G. Knepley }
1068cc08537eSMatthew G. Knepley 
1069cc08537eSMatthew G. Knepley #undef __FUNCT__
10700ec8681fSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_3D_Internal"
10710ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i ) / V */
1072011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
10730ec8681fSMatthew G. Knepley {
10740ec8681fSMatthew G. Knepley   PetscSection    coordSection;
10750ec8681fSMatthew G. Knepley   Vec             coordinates;
10760ec8681fSMatthew G. Knepley   PetscScalar    *coords = NULL;
107786623015SMatthew G. Knepley   PetscReal       vsum = 0.0, vtmp, coordsTmp[3*3];
1078a7df9edeSMatthew G. Knepley   const PetscInt *faces, *facesO;
10790ec8681fSMatthew G. Knepley   PetscInt        numFaces, f, coordSize, numCorners, p, d;
10800ec8681fSMatthew G. Knepley   PetscErrorCode  ierr;
10810ec8681fSMatthew G. Knepley 
10820ec8681fSMatthew G. Knepley   PetscFunctionBegin;
1083f6dae198SJed Brown   if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim);
10840ec8681fSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
108569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
10860ec8681fSMatthew G. Knepley 
1087d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0;
10880ec8681fSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numFaces);CHKERRQ(ierr);
10890ec8681fSMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr);
1090a7df9edeSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &facesO);CHKERRQ(ierr);
10910ec8681fSMatthew G. Knepley   for (f = 0; f < numFaces; ++f) {
1092011ea5d8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
10930ec8681fSMatthew G. Knepley     numCorners = coordSize/dim;
10940ec8681fSMatthew G. Knepley     switch (numCorners) {
10950ec8681fSMatthew G. Knepley     case 3:
10960ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
10971ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
10981ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
10991ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]);
11000ec8681fSMatthew G. Knepley       }
11010ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1102a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
11030ec8681fSMatthew G. Knepley       vsum += vtmp;
11044f25033aSJed Brown       if (centroid) {           /* Centroid of OABC = (a+b+c)/4 */
11050ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
11061ee9d5ecSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
11070ec8681fSMatthew G. Knepley         }
11080ec8681fSMatthew G. Knepley       }
11090ec8681fSMatthew G. Knepley       break;
11100ec8681fSMatthew G. Knepley     case 4:
11110ec8681fSMatthew G. Knepley       /* DO FOR PYRAMID */
11120ec8681fSMatthew G. Knepley       /* First tet */
11130ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
11141ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
11151ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
11161ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
11170ec8681fSMatthew G. Knepley       }
11180ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1119a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
11200ec8681fSMatthew G. Knepley       vsum += vtmp;
11210ec8681fSMatthew G. Knepley       if (centroid) {
11220ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
11230ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
11240ec8681fSMatthew G. Knepley         }
11250ec8681fSMatthew G. Knepley       }
11260ec8681fSMatthew G. Knepley       /* Second tet */
11270ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
11281ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[1*dim+d]);
11291ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[2*dim+d]);
11301ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
11310ec8681fSMatthew G. Knepley       }
11320ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1133a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
11340ec8681fSMatthew G. Knepley       vsum += vtmp;
11350ec8681fSMatthew G. Knepley       if (centroid) {
11360ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
11370ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
11380ec8681fSMatthew G. Knepley         }
11390ec8681fSMatthew G. Knepley       }
11400ec8681fSMatthew G. Knepley       break;
11410ec8681fSMatthew G. Knepley     default:
1142796f034aSJed Brown       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle faces with %D vertices", numCorners);
11430ec8681fSMatthew G. Knepley     }
11444f25033aSJed Brown     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
11450ec8681fSMatthew G. Knepley   }
11468763be8eSMatthew G. Knepley   if (vol)     *vol = PetscAbsReal(vsum);
11470ec8681fSMatthew G. Knepley   if (normal)   for (d = 0; d < dim; ++d) normal[d]    = 0.0;
1148d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] /= (vsum*4);
11490ec8681fSMatthew G. Knepley   PetscFunctionReturn(0);
11500ec8681fSMatthew G. Knepley }
11510ec8681fSMatthew G. Knepley 
11520ec8681fSMatthew G. Knepley #undef __FUNCT__
1153834e62ceSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFVM"
1154834e62ceSMatthew G. Knepley /*@C
1155834e62ceSMatthew G. Knepley   DMPlexComputeCellGeometryFVM - Compute the volume for a given cell
1156834e62ceSMatthew G. Knepley 
1157834e62ceSMatthew G. Knepley   Collective on DM
1158834e62ceSMatthew G. Knepley 
1159834e62ceSMatthew G. Knepley   Input Arguments:
1160834e62ceSMatthew G. Knepley + dm   - the DM
1161834e62ceSMatthew G. Knepley - cell - the cell
1162834e62ceSMatthew G. Knepley 
1163834e62ceSMatthew G. Knepley   Output Arguments:
1164834e62ceSMatthew G. Knepley + volume   - the cell volume
1165cc08537eSMatthew G. Knepley . centroid - the cell centroid
1166cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate
1167834e62ceSMatthew G. Knepley 
1168834e62ceSMatthew G. Knepley   Level: advanced
1169834e62ceSMatthew G. Knepley 
1170834e62ceSMatthew G. Knepley   Fortran Notes:
1171834e62ceSMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1172834e62ceSMatthew G. Knepley   include petsc.h90 in your code.
1173834e62ceSMatthew G. Knepley 
117469d8a9ceSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec()
1175834e62ceSMatthew G. Knepley @*/
1176cc08537eSMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1177834e62ceSMatthew G. Knepley {
11780ec8681fSMatthew G. Knepley   PetscInt       depth, dim;
1179834e62ceSMatthew G. Knepley   PetscErrorCode ierr;
1180834e62ceSMatthew G. Knepley 
1181834e62ceSMatthew G. Knepley   PetscFunctionBegin;
1182834e62ceSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1183c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1184834e62ceSMatthew G. Knepley   if (depth != dim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");
1185834e62ceSMatthew G. Knepley   /* We need to keep a pointer to the depth label */
1186011ea5d8SMatthew G. Knepley   ierr = DMPlexGetLabelValue(dm, "depth", cell, &depth);CHKERRQ(ierr);
1187834e62ceSMatthew G. Knepley   /* Cone size is now the number of faces */
1188011ea5d8SMatthew G. Knepley   switch (depth) {
1189cc08537eSMatthew G. Knepley   case 1:
1190011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1191cc08537eSMatthew G. Knepley     break;
1192834e62ceSMatthew G. Knepley   case 2:
1193011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1194834e62ceSMatthew G. Knepley     break;
1195834e62ceSMatthew G. Knepley   case 3:
1196011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1197834e62ceSMatthew G. Knepley     break;
1198834e62ceSMatthew G. Knepley   default:
1199834e62ceSMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
1200834e62ceSMatthew G. Knepley   }
1201834e62ceSMatthew G. Knepley   PetscFunctionReturn(0);
1202834e62ceSMatthew G. Knepley }
1203113c68e6SMatthew G. Knepley 
1204113c68e6SMatthew G. Knepley #undef __FUNCT__
1205c0d900a5SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFEM"
1206c0d900a5SMatthew G. Knepley /* This should also take a PetscFE argument I think */
1207c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom)
1208c0d900a5SMatthew G. Knepley {
1209c0d900a5SMatthew G. Knepley   DM             dmCell;
1210c0d900a5SMatthew G. Knepley   Vec            coordinates;
1211c0d900a5SMatthew G. Knepley   PetscSection   coordSection, sectionCell;
1212c0d900a5SMatthew G. Knepley   PetscScalar   *cgeom;
1213c0d900a5SMatthew G. Knepley   PetscInt       cStart, cEnd, cMax, c;
1214c0d900a5SMatthew G. Knepley   PetscErrorCode ierr;
1215c0d900a5SMatthew G. Knepley 
1216c0d900a5SMatthew G. Knepley   PetscFunctionBegin;
1217c0d900a5SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1218c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1219c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1220c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1221c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1222c0d900a5SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1223c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1224c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
1225c0d900a5SMatthew G. Knepley   cEnd = cMax < 0 ? cEnd : cMax;
1226c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
1227c0d900a5SMatthew G. Knepley   /* TODO This needs to be multiplied by Nq for non-affine */
12289e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFECellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1229c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1230c0d900a5SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1231c0d900a5SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1232c0d900a5SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
1233c0d900a5SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1234c0d900a5SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1235c0d900a5SMatthew G. Knepley     PetscFECellGeom *cg;
1236c0d900a5SMatthew G. Knepley 
1237c0d900a5SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1238c0d900a5SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1239c0d900a5SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v0, cg->J, cg->invJ, &cg->detJ);CHKERRQ(ierr);
1240c0d900a5SMatthew G. Knepley     if (cg->detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", cg->detJ, c);
1241c0d900a5SMatthew G. Knepley   }
1242c0d900a5SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1243c0d900a5SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
1244c0d900a5SMatthew G. Knepley   PetscFunctionReturn(0);
1245c0d900a5SMatthew G. Knepley }
1246c0d900a5SMatthew G. Knepley 
1247c0d900a5SMatthew G. Knepley #undef __FUNCT__
1248113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM"
1249891a9168SMatthew G. Knepley /*@
1250891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
1251891a9168SMatthew G. Knepley 
1252891a9168SMatthew G. Knepley   Input Parameter:
1253891a9168SMatthew G. Knepley . dm - The DM
1254891a9168SMatthew G. Knepley 
1255891a9168SMatthew G. Knepley   Output Parameters:
1256891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data
1257891a9168SMatthew G. Knepley . facegeom - A Vec of PetscFVFaceGeom data
1258891a9168SMatthew G. Knepley 
1259891a9168SMatthew G. Knepley   Level: developer
1260891a9168SMatthew G. Knepley 
1261891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM()
1262891a9168SMatthew G. Knepley @*/
1263113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom)
1264113c68e6SMatthew G. Knepley {
1265113c68e6SMatthew G. Knepley   DM             dmFace, dmCell;
1266113c68e6SMatthew G. Knepley   DMLabel        ghostLabel;
1267113c68e6SMatthew G. Knepley   PetscSection   sectionFace, sectionCell;
1268113c68e6SMatthew G. Knepley   PetscSection   coordSection;
1269113c68e6SMatthew G. Knepley   Vec            coordinates;
1270113c68e6SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
1271113c68e6SMatthew G. Knepley   PetscReal      minradius, gminradius;
1272113c68e6SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
1273113c68e6SMatthew G. Knepley   PetscErrorCode ierr;
1274113c68e6SMatthew G. Knepley 
1275113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1276113c68e6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1277113c68e6SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1278113c68e6SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1279113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
1280113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1281113c68e6SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1282113c68e6SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1283113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1284113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1285113c68e6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1286113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
12879e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1288113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1289113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1290113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1291113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
1292113c68e6SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1293113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
1294113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
1295113c68e6SMatthew G. Knepley 
1296113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1297113c68e6SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1298113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr);
1299113c68e6SMatthew G. Knepley   }
1300113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
1301113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmFace);CHKERRQ(ierr);
1302113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionFace);CHKERRQ(ierr);
1303113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1304113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr);
13059e5edeeeSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1306113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr);
1307113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmFace, sectionFace);CHKERRQ(ierr);
1308113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionFace);CHKERRQ(ierr);
1309113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr);
1310113c68e6SMatthew G. Knepley   ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr);
1311113c68e6SMatthew G. Knepley   ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1312113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
1313113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
1314113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
1315113c68e6SMatthew G. Knepley     PetscReal        area;
13169ac3fadcSMatthew G. Knepley     PetscInt         ghost = -1, d;
1317113c68e6SMatthew G. Knepley 
13189ac3fadcSMatthew G. Knepley     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
1319113c68e6SMatthew G. Knepley     if (ghost >= 0) continue;
1320113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr);
1321113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr);
1322113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
1323113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
1324113c68e6SMatthew G. Knepley     {
1325113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
1326113c68e6SMatthew G. Knepley       const PetscInt  *cells;
1327113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
13280453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
1329113c68e6SMatthew G. Knepley 
1330113c68e6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr);
1331113c68e6SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr);
1332113c68e6SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr);
1333113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
1334113c68e6SMatthew G. Knepley       rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
1335f170fd80SMatthew G. Knepley       ierr = DMPlexLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr);
1336f170fd80SMatthew G. Knepley       ierr = DMPlexLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr);
13370453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
1338113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
1339113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
1340113c68e6SMatthew G. Knepley       }
1341113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
1342113c68e6SMatthew 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]);
1343113c68e6SMatthew 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]);
1344113c68e6SMatthew G. Knepley         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f);
1345113c68e6SMatthew G. Knepley       }
1346113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
1347113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
1348113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
1349113c68e6SMatthew G. Knepley       }
1350113c68e6SMatthew G. Knepley       if (cells[1] < cEndInterior) {
1351113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
1352113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
1353113c68e6SMatthew G. Knepley       }
1354113c68e6SMatthew G. Knepley     }
1355113c68e6SMatthew G. Knepley   }
1356a9b180a6SBarry Smith   ierr = MPI_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1357113c68e6SMatthew G. Knepley   ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr);
1358113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
1359113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
1360113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
1361113c68e6SMatthew G. Knepley     const PetscInt  *cone,    *support;
1362113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
1363113c68e6SMatthew G. Knepley 
1364113c68e6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr);
1365113c68e6SMatthew G. Knepley     if (coneSize != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize);
1366113c68e6SMatthew G. Knepley     ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr);
1367113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr);
1368113c68e6SMatthew G. Knepley     if (supportSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 1", cone[0], supportSize);
1369113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr);
1370113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr);
1371113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
1372113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
1373113c68e6SMatthew G. Knepley       if (support[s] == c) {
1374113c68e6SMatthew G. Knepley         const PetscFVCellGeom *ci;
1375113c68e6SMatthew G. Knepley         PetscFVCellGeom       *cg;
1376113c68e6SMatthew G. Knepley         PetscReal              c2f[3], a;
1377113c68e6SMatthew G. Knepley 
1378113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr);
1379113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
1380113c68e6SMatthew G. Knepley         a    = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
1381113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr);
1382113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid);
1383113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
1384113c68e6SMatthew G. Knepley       }
1385113c68e6SMatthew G. Knepley     }
1386113c68e6SMatthew G. Knepley   }
1387113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr);
1388113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1389113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
1390113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmFace);CHKERRQ(ierr);
1391113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1392113c68e6SMatthew G. Knepley }
1393113c68e6SMatthew G. Knepley 
1394113c68e6SMatthew G. Knepley #undef __FUNCT__
1395113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexGetMinRadius"
1396113c68e6SMatthew G. Knepley /*@C
1397113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
1398113c68e6SMatthew G. Knepley 
1399113c68e6SMatthew G. Knepley   Not collective
1400113c68e6SMatthew G. Knepley 
1401113c68e6SMatthew G. Knepley   Input Argument:
1402113c68e6SMatthew G. Knepley . dm - the DM
1403113c68e6SMatthew G. Knepley 
1404113c68e6SMatthew G. Knepley   Output Argument:
1405113c68e6SMatthew G. Knepley . minradius - the minium cell radius
1406113c68e6SMatthew G. Knepley 
1407113c68e6SMatthew G. Knepley   Level: developer
1408113c68e6SMatthew G. Knepley 
1409113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates()
1410113c68e6SMatthew G. Knepley @*/
1411113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius)
1412113c68e6SMatthew G. Knepley {
1413113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1414113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1415113c68e6SMatthew G. Knepley   PetscValidPointer(minradius,2);
1416113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex*) dm->data)->minradius;
1417113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1418113c68e6SMatthew G. Knepley }
1419113c68e6SMatthew G. Knepley 
1420113c68e6SMatthew G. Knepley #undef __FUNCT__
1421113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexSetMinRadius"
1422113c68e6SMatthew G. Knepley /*@C
1423113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
1424113c68e6SMatthew G. Knepley 
1425113c68e6SMatthew G. Knepley   Logically collective
1426113c68e6SMatthew G. Knepley 
1427113c68e6SMatthew G. Knepley   Input Arguments:
1428113c68e6SMatthew G. Knepley + dm - the DM
1429113c68e6SMatthew G. Knepley - minradius - the minium cell radius
1430113c68e6SMatthew G. Knepley 
1431113c68e6SMatthew G. Knepley   Level: developer
1432113c68e6SMatthew G. Knepley 
1433113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates()
1434113c68e6SMatthew G. Knepley @*/
1435113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius)
1436113c68e6SMatthew G. Knepley {
1437113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1438113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1439113c68e6SMatthew G. Knepley   ((DM_Plex*) dm->data)->minradius = minradius;
1440113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1441113c68e6SMatthew G. Knepley }
1442856ac710SMatthew G. Knepley 
1443856ac710SMatthew G. Knepley #undef __FUNCT__
1444856ac710SMatthew G. Knepley #define __FUNCT__ "BuildGradientReconstruction_Internal"
1445856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
1446856ac710SMatthew G. Knepley {
1447856ac710SMatthew G. Knepley   DMLabel        ghostLabel;
1448856ac710SMatthew G. Knepley   PetscScalar   *dx, *grad, **gref;
1449856ac710SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
1450856ac710SMatthew G. Knepley   PetscErrorCode ierr;
1451856ac710SMatthew G. Knepley 
1452856ac710SMatthew G. Knepley   PetscFunctionBegin;
1453856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1454856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1455856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1456856ac710SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr);
1457856ac710SMatthew G. Knepley   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
1458856ac710SMatthew G. Knepley   ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1459856ac710SMatthew G. Knepley   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
1460856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
1461856ac710SMatthew G. Knepley     const PetscInt        *faces;
1462856ac710SMatthew G. Knepley     PetscInt               numFaces, usedFaces, f, d;
1463856ac710SMatthew G. Knepley     const PetscFVCellGeom *cg;
1464856ac710SMatthew G. Knepley     PetscBool              boundary;
1465856ac710SMatthew G. Knepley     PetscInt               ghost;
1466856ac710SMatthew G. Knepley 
1467856ac710SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1468856ac710SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr);
1469856ac710SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr);
1470856ac710SMatthew 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);
1471856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
1472856ac710SMatthew G. Knepley       const PetscFVCellGeom *cg1;
1473856ac710SMatthew G. Knepley       PetscFVFaceGeom       *fg;
1474856ac710SMatthew G. Knepley       const PetscInt        *fcells;
1475856ac710SMatthew G. Knepley       PetscInt               ncell, side;
1476856ac710SMatthew G. Knepley 
1477856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
1478856ac710SMatthew G. Knepley       ierr = DMPlexIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
1479856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
1480856ac710SMatthew G. Knepley       ierr  = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr);
1481856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
1482856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
1483856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr);
1484856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
1485856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d];
1486856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side];  /* Gradient reconstruction term will go here */
1487856ac710SMatthew G. Knepley     }
1488856ac710SMatthew G. Knepley     if (!usedFaces) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
1489856ac710SMatthew G. Knepley     ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr);
1490856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
1491856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
1492856ac710SMatthew G. Knepley       ierr = DMPlexIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
1493856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
1494856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d];
1495856ac710SMatthew G. Knepley       ++usedFaces;
1496856ac710SMatthew G. Knepley     }
1497856ac710SMatthew G. Knepley   }
1498856ac710SMatthew G. Knepley   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
1499856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
1500856ac710SMatthew G. Knepley }
1501856ac710SMatthew G. Knepley 
1502856ac710SMatthew G. Knepley #undef __FUNCT__
1503856ac710SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGradientFVM"
1504856ac710SMatthew G. Knepley /*@
1505856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
1506856ac710SMatthew G. Knepley 
1507856ac710SMatthew G. Knepley   Collective on DM
1508856ac710SMatthew G. Knepley 
1509856ac710SMatthew G. Knepley   Input Arguments:
1510856ac710SMatthew G. Knepley + dm  - The DM
1511856ac710SMatthew G. Knepley . fvm - The PetscFV
1512856ac710SMatthew G. Knepley . faceGeometry - The face geometry from DMPlexGetFaceGeometryFVM()
1513856ac710SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexGetCellGeometryFVM()
1514856ac710SMatthew G. Knepley 
1515856ac710SMatthew G. Knepley   Output Parameters:
1516856ac710SMatthew G. Knepley + faceGeometry - The geometric factors for gradient calculation are inserted
1517856ac710SMatthew G. Knepley - dmGrad - The DM describing the layout of gradient data
1518856ac710SMatthew G. Knepley 
1519856ac710SMatthew G. Knepley   Level: developer
1520856ac710SMatthew G. Knepley 
1521856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM()
1522856ac710SMatthew G. Knepley @*/
1523856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad)
1524856ac710SMatthew G. Knepley {
1525856ac710SMatthew G. Knepley   DM             dmFace, dmCell;
1526856ac710SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
1527856ac710SMatthew G. Knepley   PetscSection   sectionGrad;
1528856ac710SMatthew G. Knepley   PetscInt       dim, pdim, cStart, cEnd, cEndInterior, c;
1529856ac710SMatthew G. Knepley   PetscErrorCode ierr;
1530856ac710SMatthew G. Knepley 
1531856ac710SMatthew G. Knepley   PetscFunctionBegin;
1532856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1533856ac710SMatthew G. Knepley   ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr);
1534856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1535856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1536856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
1537856ac710SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
1538856ac710SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
1539856ac710SMatthew G. Knepley   ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr);
1540856ac710SMatthew G. Knepley   ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr);
1541856ac710SMatthew G. Knepley   ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
1542856ac710SMatthew G. Knepley   ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr);
1543856ac710SMatthew G. Knepley   ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr);
1544856ac710SMatthew G. Knepley   /* Create storage for gradients */
1545856ac710SMatthew G. Knepley   ierr = DMClone(dm, dmGrad);CHKERRQ(ierr);
1546856ac710SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionGrad);CHKERRQ(ierr);
1547856ac710SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr);
1548856ac710SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);}
1549856ac710SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr);
1550856ac710SMatthew G. Knepley   ierr = DMSetDefaultSection(*dmGrad, sectionGrad);CHKERRQ(ierr);
1551856ac710SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionGrad);CHKERRQ(ierr);
1552856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
1553856ac710SMatthew G. Knepley }
1554