xref: /petsc/src/dm/impls/plex/plexgeometry.c (revision 1318edbe5267f2dbae78d44d39239ffbc478ddd5)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2ccd2543fSMatthew G Knepley 
3ccd2543fSMatthew G Knepley #undef __FUNCT__
4ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_Simplex_2D_Internal"
5ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
6ccd2543fSMatthew G Knepley {
7ccd2543fSMatthew G Knepley   const PetscInt embedDim = 2;
8ccd2543fSMatthew G Knepley   PetscReal      x        = PetscRealPart(point[0]);
9ccd2543fSMatthew G Knepley   PetscReal      y        = PetscRealPart(point[1]);
10ccd2543fSMatthew G Knepley   PetscReal      v0[2], J[4], invJ[4], detJ;
11ccd2543fSMatthew G Knepley   PetscReal      xi, eta;
12ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
13ccd2543fSMatthew G Knepley 
14ccd2543fSMatthew G Knepley   PetscFunctionBegin;
158e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
16ccd2543fSMatthew G Knepley   xi  = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]);
17ccd2543fSMatthew G Knepley   eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]);
18ccd2543fSMatthew G Knepley 
19ccd2543fSMatthew G Knepley   if ((xi >= 0.0) && (eta >= 0.0) && (xi + eta <= 2.0)) *cell = c;
20ccd2543fSMatthew G Knepley   else *cell = -1;
21ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
22ccd2543fSMatthew G Knepley }
23ccd2543fSMatthew G Knepley 
24ccd2543fSMatthew G Knepley #undef __FUNCT__
25ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_General_2D_Internal"
26ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
27ccd2543fSMatthew G Knepley {
28ccd2543fSMatthew G Knepley   PetscSection       coordSection;
29ccd2543fSMatthew G Knepley   Vec             coordsLocal;
30a1e44745SMatthew G. Knepley   PetscScalar    *coords = NULL;
31ccd2543fSMatthew G Knepley   const PetscInt  faces[8]  = {0, 1, 1, 2, 2, 3, 3, 0};
32ccd2543fSMatthew G Knepley   PetscReal       x         = PetscRealPart(point[0]);
33ccd2543fSMatthew G Knepley   PetscReal       y         = PetscRealPart(point[1]);
34ccd2543fSMatthew G Knepley   PetscInt        crossings = 0, f;
35ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
36ccd2543fSMatthew G Knepley 
37ccd2543fSMatthew G Knepley   PetscFunctionBegin;
38ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
3969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
40ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
41ccd2543fSMatthew G Knepley   for (f = 0; f < 4; ++f) {
42ccd2543fSMatthew G Knepley     PetscReal x_i   = PetscRealPart(coords[faces[2*f+0]*2+0]);
43ccd2543fSMatthew G Knepley     PetscReal y_i   = PetscRealPart(coords[faces[2*f+0]*2+1]);
44ccd2543fSMatthew G Knepley     PetscReal x_j   = PetscRealPart(coords[faces[2*f+1]*2+0]);
45ccd2543fSMatthew G Knepley     PetscReal y_j   = PetscRealPart(coords[faces[2*f+1]*2+1]);
46ccd2543fSMatthew G Knepley     PetscReal slope = (y_j - y_i) / (x_j - x_i);
47ccd2543fSMatthew G Knepley     PetscBool cond1 = (x_i <= x) && (x < x_j) ? PETSC_TRUE : PETSC_FALSE;
48ccd2543fSMatthew G Knepley     PetscBool cond2 = (x_j <= x) && (x < x_i) ? PETSC_TRUE : PETSC_FALSE;
49ccd2543fSMatthew G Knepley     PetscBool above = (y < slope * (x - x_i) + y_i) ? PETSC_TRUE : PETSC_FALSE;
50ccd2543fSMatthew G Knepley     if ((cond1 || cond2)  && above) ++crossings;
51ccd2543fSMatthew G Knepley   }
52ccd2543fSMatthew G Knepley   if (crossings % 2) *cell = c;
53ccd2543fSMatthew G Knepley   else *cell = -1;
54ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
55ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
56ccd2543fSMatthew G Knepley }
57ccd2543fSMatthew G Knepley 
58ccd2543fSMatthew G Knepley #undef __FUNCT__
59ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_Simplex_3D_Internal"
60ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
61ccd2543fSMatthew G Knepley {
62ccd2543fSMatthew G Knepley   const PetscInt embedDim = 3;
63ccd2543fSMatthew G Knepley   PetscReal      v0[3], J[9], invJ[9], detJ;
64ccd2543fSMatthew G Knepley   PetscReal      x = PetscRealPart(point[0]);
65ccd2543fSMatthew G Knepley   PetscReal      y = PetscRealPart(point[1]);
66ccd2543fSMatthew G Knepley   PetscReal      z = PetscRealPart(point[2]);
67ccd2543fSMatthew G Knepley   PetscReal      xi, eta, zeta;
68ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
69ccd2543fSMatthew G Knepley 
70ccd2543fSMatthew G Knepley   PetscFunctionBegin;
718e0841e0SMatthew G. Knepley   ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
72ccd2543fSMatthew G Knepley   xi   = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]) + invJ[0*embedDim+2]*(z - v0[2]);
73ccd2543fSMatthew G Knepley   eta  = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]) + invJ[1*embedDim+2]*(z - v0[2]);
74ccd2543fSMatthew G Knepley   zeta = invJ[2*embedDim+0]*(x - v0[0]) + invJ[2*embedDim+1]*(y - v0[1]) + invJ[2*embedDim+2]*(z - v0[2]);
75ccd2543fSMatthew G Knepley 
76ccd2543fSMatthew G Knepley   if ((xi >= 0.0) && (eta >= 0.0) && (zeta >= 0.0) && (xi + eta + zeta <= 2.0)) *cell = c;
77ccd2543fSMatthew G Knepley   else *cell = -1;
78ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
79ccd2543fSMatthew G Knepley }
80ccd2543fSMatthew G Knepley 
81ccd2543fSMatthew G Knepley #undef __FUNCT__
82ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexLocatePoint_General_3D_Internal"
83ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell)
84ccd2543fSMatthew G Knepley {
85ccd2543fSMatthew G Knepley   PetscSection   coordSection;
86ccd2543fSMatthew G Knepley   Vec            coordsLocal;
877c1f9639SMatthew G Knepley   PetscScalar   *coords;
88fb150da6SMatthew G. Knepley   const PetscInt faces[24] = {0, 3, 2, 1,  5, 4, 7, 6,  3, 0, 4, 5,
89fb150da6SMatthew G. Knepley                               1, 2, 6, 7,  3, 5, 6, 2,  0, 1, 7, 4};
90ccd2543fSMatthew G Knepley   PetscBool      found = PETSC_TRUE;
91ccd2543fSMatthew G Knepley   PetscInt       f;
92ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
93ccd2543fSMatthew G Knepley 
94ccd2543fSMatthew G Knepley   PetscFunctionBegin;
95ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
9669d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
97ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
98ccd2543fSMatthew G Knepley   for (f = 0; f < 6; ++f) {
99ccd2543fSMatthew G Knepley     /* Check the point is under plane */
100ccd2543fSMatthew G Knepley     /*   Get face normal */
101ccd2543fSMatthew G Knepley     PetscReal v_i[3];
102ccd2543fSMatthew G Knepley     PetscReal v_j[3];
103ccd2543fSMatthew G Knepley     PetscReal normal[3];
104ccd2543fSMatthew G Knepley     PetscReal pp[3];
105ccd2543fSMatthew G Knepley     PetscReal dot;
106ccd2543fSMatthew G Knepley 
107ccd2543fSMatthew G Knepley     v_i[0]    = PetscRealPart(coords[faces[f*4+3]*3+0]-coords[faces[f*4+0]*3+0]);
108ccd2543fSMatthew G Knepley     v_i[1]    = PetscRealPart(coords[faces[f*4+3]*3+1]-coords[faces[f*4+0]*3+1]);
109ccd2543fSMatthew G Knepley     v_i[2]    = PetscRealPart(coords[faces[f*4+3]*3+2]-coords[faces[f*4+0]*3+2]);
110ccd2543fSMatthew G Knepley     v_j[0]    = PetscRealPart(coords[faces[f*4+1]*3+0]-coords[faces[f*4+0]*3+0]);
111ccd2543fSMatthew G Knepley     v_j[1]    = PetscRealPart(coords[faces[f*4+1]*3+1]-coords[faces[f*4+0]*3+1]);
112ccd2543fSMatthew G Knepley     v_j[2]    = PetscRealPart(coords[faces[f*4+1]*3+2]-coords[faces[f*4+0]*3+2]);
113ccd2543fSMatthew G Knepley     normal[0] = v_i[1]*v_j[2] - v_i[2]*v_j[1];
114ccd2543fSMatthew G Knepley     normal[1] = v_i[2]*v_j[0] - v_i[0]*v_j[2];
115ccd2543fSMatthew G Knepley     normal[2] = v_i[0]*v_j[1] - v_i[1]*v_j[0];
116ccd2543fSMatthew G Knepley     pp[0]     = PetscRealPart(coords[faces[f*4+0]*3+0] - point[0]);
117ccd2543fSMatthew G Knepley     pp[1]     = PetscRealPart(coords[faces[f*4+0]*3+1] - point[1]);
118ccd2543fSMatthew G Knepley     pp[2]     = PetscRealPart(coords[faces[f*4+0]*3+2] - point[2]);
119ccd2543fSMatthew G Knepley     dot       = normal[0]*pp[0] + normal[1]*pp[1] + normal[2]*pp[2];
120ccd2543fSMatthew G Knepley 
121ccd2543fSMatthew G Knepley     /* Check that projected point is in face (2D location problem) */
122ccd2543fSMatthew G Knepley     if (dot < 0.0) {
123ccd2543fSMatthew G Knepley       found = PETSC_FALSE;
124ccd2543fSMatthew G Knepley       break;
125ccd2543fSMatthew G Knepley     }
126ccd2543fSMatthew G Knepley   }
127ccd2543fSMatthew G Knepley   if (found) *cell = c;
128ccd2543fSMatthew G Knepley   else *cell = -1;
129ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr);
130ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
131ccd2543fSMatthew G Knepley }
132ccd2543fSMatthew G Knepley 
133ccd2543fSMatthew G Knepley #undef __FUNCT__
134c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashInitialize_Internal"
135c4eade1cSMatthew G. Knepley static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[])
136c4eade1cSMatthew G. Knepley {
137c4eade1cSMatthew G. Knepley   PetscInt d;
138c4eade1cSMatthew G. Knepley 
139c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
140c4eade1cSMatthew G. Knepley   box->dim = dim;
141c4eade1cSMatthew G. Knepley   for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]);
142c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
143c4eade1cSMatthew G. Knepley }
144c4eade1cSMatthew G. Knepley 
145c4eade1cSMatthew G. Knepley #undef __FUNCT__
146c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashCreate"
147c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box)
148c4eade1cSMatthew G. Knepley {
149c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
150c4eade1cSMatthew G. Knepley 
151c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
152c4eade1cSMatthew G. Knepley   ierr = PetscMalloc1(1, box);CHKERRQ(ierr);
153c4eade1cSMatthew G. Knepley   ierr = PetscGridHashInitialize_Internal(*box, dim, point);CHKERRQ(ierr);
154c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
155c4eade1cSMatthew G. Knepley }
156c4eade1cSMatthew G. Knepley 
157c4eade1cSMatthew G. Knepley #undef __FUNCT__
158c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashEnlarge"
159c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[])
160c4eade1cSMatthew G. Knepley {
161c4eade1cSMatthew G. Knepley   PetscInt d;
162c4eade1cSMatthew G. Knepley 
163c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
164c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
165c4eade1cSMatthew G. Knepley     box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d]));
166c4eade1cSMatthew G. Knepley     box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d]));
167c4eade1cSMatthew G. Knepley   }
168c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
169c4eade1cSMatthew G. Knepley }
170c4eade1cSMatthew G. Knepley 
171c4eade1cSMatthew G. Knepley #undef __FUNCT__
172c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashSetGrid"
173c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[])
174c4eade1cSMatthew G. Knepley {
175c4eade1cSMatthew G. Knepley   PetscInt d;
176c4eade1cSMatthew G. Knepley 
177c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
178c4eade1cSMatthew G. Knepley   for (d = 0; d < box->dim; ++d) {
179c4eade1cSMatthew G. Knepley     box->extent[d] = box->upper[d] - box->lower[d];
180c4eade1cSMatthew G. Knepley     if (n[d] == PETSC_DETERMINE) {
181c4eade1cSMatthew G. Knepley       box->h[d] = h[d];
182c4eade1cSMatthew G. Knepley       box->n[d] = PetscCeilReal(box->extent[d]/h[d]);
183c4eade1cSMatthew G. Knepley     } else {
184c4eade1cSMatthew G. Knepley       box->n[d] = n[d];
185c4eade1cSMatthew G. Knepley       box->h[d] = box->extent[d]/n[d];
186c4eade1cSMatthew G. Knepley     }
187c4eade1cSMatthew G. Knepley   }
188c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
189c4eade1cSMatthew G. Knepley }
190c4eade1cSMatthew G. Knepley 
191c4eade1cSMatthew G. Knepley #undef __FUNCT__
192c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashGetEnclosingBox"
1931c6dfc3eSMatthew G. Knepley PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[])
194c4eade1cSMatthew G. Knepley {
195c4eade1cSMatthew G. Knepley   const PetscReal *lower = box->lower;
196c4eade1cSMatthew G. Knepley   const PetscReal *upper = box->upper;
197c4eade1cSMatthew G. Knepley   const PetscReal *h     = box->h;
198c4eade1cSMatthew G. Knepley   const PetscInt  *n     = box->n;
199c4eade1cSMatthew G. Knepley   const PetscInt   dim   = box->dim;
200c4eade1cSMatthew G. Knepley   PetscInt         d, p;
201c4eade1cSMatthew G. Knepley 
202c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
203c4eade1cSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
204c4eade1cSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
2051c6dfc3eSMatthew G. Knepley       PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]);
206c4eade1cSMatthew G. Knepley 
2071c6dfc3eSMatthew G. Knepley       if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1;
208c4eade1cSMatthew G. Knepley       if (dbox < 0 || dbox >= n[d]) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input point %d (%g, %g, %g) is outside of our bounding box",
2091c6dfc3eSMatthew G. Knepley                                              p, PetscRealPart(points[p*dim+0]), dim > 1 ? PetscRealPart(points[p*dim+1]) : 0.0, dim > 2 ? PetscRealPart(points[p*dim+2]) : 0.0);
210c4eade1cSMatthew G. Knepley       dboxes[p*dim+d] = dbox;
211c4eade1cSMatthew G. Knepley     }
212c4eade1cSMatthew G. Knepley     if (boxes) for (d = 1, boxes[p] = dboxes[p*dim]; d < dim; ++d) boxes[p] += dboxes[p*dim+d]*n[d-1];
213c4eade1cSMatthew G. Knepley   }
214c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
215c4eade1cSMatthew G. Knepley }
216c4eade1cSMatthew G. Knepley 
217c4eade1cSMatthew G. Knepley #undef __FUNCT__
218c4eade1cSMatthew G. Knepley #define __FUNCT__ "PetscGridHashDestroy"
219c4eade1cSMatthew G. Knepley PetscErrorCode PetscGridHashDestroy(PetscGridHash *box)
220c4eade1cSMatthew G. Knepley {
221c4eade1cSMatthew G. Knepley   PetscErrorCode ierr;
222c4eade1cSMatthew G. Knepley 
223c4eade1cSMatthew G. Knepley   PetscFunctionBegin;
224c4eade1cSMatthew G. Knepley   if (*box) {
225c4eade1cSMatthew G. Knepley     ierr = PetscSectionDestroy(&(*box)->cellSection);CHKERRQ(ierr);
226c4eade1cSMatthew G. Knepley     ierr = ISDestroy(&(*box)->cells);CHKERRQ(ierr);
227c4eade1cSMatthew G. Knepley     ierr = DMLabelDestroy(&(*box)->cellsSparse);CHKERRQ(ierr);
228c4eade1cSMatthew G. Knepley   }
229c4eade1cSMatthew G. Knepley   ierr = PetscFree(*box);CHKERRQ(ierr);
230c4eade1cSMatthew G. Knepley   PetscFunctionReturn(0);
231c4eade1cSMatthew G. Knepley }
232c4eade1cSMatthew G. Knepley 
233cafe43deSMatthew G. Knepley #undef __FUNCT__
234cafe43deSMatthew G. Knepley #define __FUNCT__ "DMPlexLocatePoint_Internal"
235cafe43deSMatthew G. Knepley PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell)
236cafe43deSMatthew G. Knepley {
237cafe43deSMatthew G. Knepley   PetscInt       coneSize;
238cafe43deSMatthew G. Knepley   PetscErrorCode ierr;
239cafe43deSMatthew G. Knepley 
240cafe43deSMatthew G. Knepley   PetscFunctionBegin;
241cafe43deSMatthew G. Knepley   switch (dim) {
242cafe43deSMatthew G. Knepley   case 2:
243cafe43deSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr);
244cafe43deSMatthew G. Knepley     switch (coneSize) {
245cafe43deSMatthew G. Knepley     case 3:
246cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
247cafe43deSMatthew G. Knepley       break;
248cafe43deSMatthew G. Knepley     case 4:
249cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_General_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
250cafe43deSMatthew G. Knepley       break;
251cafe43deSMatthew G. Knepley     default:
252cafe43deSMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize);
253cafe43deSMatthew G. Knepley     }
254cafe43deSMatthew G. Knepley     break;
255cafe43deSMatthew G. Knepley   case 3:
256cafe43deSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr);
257cafe43deSMatthew G. Knepley     switch (coneSize) {
258cafe43deSMatthew G. Knepley     case 4:
259cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
260cafe43deSMatthew G. Knepley       break;
261cafe43deSMatthew G. Knepley     case 6:
262cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr);
263cafe43deSMatthew G. Knepley       break;
264cafe43deSMatthew G. Knepley     default:
265cafe43deSMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize);
266cafe43deSMatthew G. Knepley     }
267cafe43deSMatthew G. Knepley     break;
268cafe43deSMatthew G. Knepley   default:
269cafe43deSMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for mesh dimension %D", dim);
270cafe43deSMatthew G. Knepley   }
271cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
272cafe43deSMatthew G. Knepley }
273cafe43deSMatthew G. Knepley 
274cafe43deSMatthew G. Knepley #undef __FUNCT__
275cafe43deSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGridHash_Internal"
276cafe43deSMatthew G. Knepley PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox)
277cafe43deSMatthew G. Knepley {
278cafe43deSMatthew G. Knepley   MPI_Comm           comm;
279cafe43deSMatthew G. Knepley   PetscGridHash      lbox;
280cafe43deSMatthew G. Knepley   Vec                coordinates;
281cafe43deSMatthew G. Knepley   PetscSection       coordSection;
282cafe43deSMatthew G. Knepley   Vec                coordsLocal;
283cafe43deSMatthew G. Knepley   const PetscScalar *coords;
284cafe43deSMatthew G. Knepley   PetscInt          *dboxes;
285cafe43deSMatthew G. Knepley   PetscInt           n[3] = {10, 10, 10};
286cafe43deSMatthew G. Knepley   PetscInt           dim, N, cStart, cEnd, c, i;
287cafe43deSMatthew G. Knepley   PetscErrorCode     ierr;
288cafe43deSMatthew G. Knepley 
289cafe43deSMatthew G. Knepley   PetscFunctionBegin;
290cafe43deSMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
291cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
292cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
293cafe43deSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
294cafe43deSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
295cafe43deSMatthew G. Knepley   ierr = PetscGridHashCreate(comm, dim, coords, &lbox);CHKERRQ(ierr);
296cafe43deSMatthew G. Knepley   for (i = 0; i < N; i += dim) {ierr = PetscGridHashEnlarge(lbox, &coords[i]);CHKERRQ(ierr);}
297cafe43deSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
298cafe43deSMatthew G. Knepley   ierr = PetscGridHashSetGrid(lbox, n, NULL);CHKERRQ(ierr);
299cafe43deSMatthew G. Knepley #if 0
300cafe43deSMatthew G. Knepley   /* Could define a custom reduction to merge these */
301cafe43deSMatthew G. Knepley   ierr = MPI_Allreduce(lbox->lower, gbox->lower, 3, MPIU_REAL, MPI_MIN, comm);CHKERRQ(ierr);
302cafe43deSMatthew G. Knepley   ierr = MPI_Allreduce(lbox->upper, gbox->upper, 3, MPIU_REAL, MPI_MAX, comm);CHKERRQ(ierr);
303cafe43deSMatthew G. Knepley #endif
304cafe43deSMatthew G. Knepley   /* Is there a reason to snap the local bounding box to a division of the global box? */
305cafe43deSMatthew G. Knepley   /* Should we compute all overlaps of local boxes? We could do this with a rendevouz scheme partitioning the global box */
306cafe43deSMatthew G. Knepley   /* Create label */
307cafe43deSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
308cafe43deSMatthew G. Knepley   ierr = DMLabelCreate("cells", &lbox->cellsSparse);CHKERRQ(ierr);
309cafe43deSMatthew G. Knepley   ierr = DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd);CHKERRQ(ierr);
310cafe43deSMatthew G. Knepley   /* Compute boxes which overlap each cell */
311cafe43deSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
312cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
313cafe43deSMatthew G. Knepley   ierr = PetscCalloc1((dim+1) * dim, &dboxes);CHKERRQ(ierr);
314cafe43deSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
315cafe43deSMatthew G. Knepley     const PetscReal *h       = lbox->h;
316cafe43deSMatthew G. Knepley     PetscScalar     *ccoords = NULL;
317cafe43deSMatthew G. Knepley     PetscScalar      point[3];
318cafe43deSMatthew G. Knepley     PetscInt         dlim[6], d, e, i, j, k;
319cafe43deSMatthew G. Knepley 
320cafe43deSMatthew G. Knepley     /* Find boxes enclosing each vertex */
321cafe43deSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords);CHKERRQ(ierr);
322cafe43deSMatthew G. Knepley     ierr = PetscGridHashGetEnclosingBox(lbox, dim+1, ccoords, dboxes, NULL);CHKERRQ(ierr);
323cafe43deSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords);CHKERRQ(ierr);
324cafe43deSMatthew G. Knepley     /* Get grid of boxes containing these */
325cafe43deSMatthew G. Knepley     for (d = 0; d < dim; ++d) {dlim[d*2+0] = dlim[d*2+1] = dboxes[d];}
326cafe43deSMatthew G. Knepley     for (e = 1; e < dim+1; ++e) {
327cafe43deSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
328cafe43deSMatthew G. Knepley         dlim[d*2+0] = PetscMin(dlim[d*2+0], dboxes[e*dim+d]);
329cafe43deSMatthew G. Knepley         dlim[d*2+1] = PetscMax(dlim[d*2+1], dboxes[e*dim+d]);
330cafe43deSMatthew G. Knepley       }
331cafe43deSMatthew G. Knepley     }
332cafe43deSMatthew G. Knepley     /* Check whether cell contains any vertex of these subboxes TODO vectorize this */
333cafe43deSMatthew G. Knepley     for (k = dlim[2*2+0], point[2] = lbox->lower[2] + k*h[2]; k <= dlim[2*2+1]; ++k, point[2] += h[2]) {
334cafe43deSMatthew G. Knepley       for (j = dlim[1*2+0], point[1] = lbox->lower[1] + j*h[1]; j <= dlim[1*2+1]; ++j, point[1] += h[1]) {
335cafe43deSMatthew G. Knepley         for (i = dlim[0*2+0], point[0] = lbox->lower[0] + i*h[0]; i <= dlim[0*2+1]; ++i, point[0] += h[0]) {
336cafe43deSMatthew G. Knepley           const PetscInt box = (k*lbox->n[1] + j)*lbox->n[0] + i;
337cafe43deSMatthew G. Knepley           PetscScalar    cpoint[3];
338cafe43deSMatthew G. Knepley           PetscInt       cell, ii, jj, kk;
339cafe43deSMatthew G. Knepley 
340cafe43deSMatthew G. Knepley           for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) {
341cafe43deSMatthew G. Knepley             for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) {
342cafe43deSMatthew G. Knepley               for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) {
343cafe43deSMatthew G. Knepley 
344cafe43deSMatthew G. Knepley                 ierr = DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell);CHKERRQ(ierr);
345cafe43deSMatthew G. Knepley                 if (cell >= 0) {DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); ii = jj = kk = 2;}
346cafe43deSMatthew G. Knepley               }
347cafe43deSMatthew G. Knepley             }
348cafe43deSMatthew G. Knepley           }
349cafe43deSMatthew G. Knepley         }
350cafe43deSMatthew G. Knepley       }
351cafe43deSMatthew G. Knepley     }
352cafe43deSMatthew G. Knepley   }
353cafe43deSMatthew G. Knepley   ierr = PetscFree(dboxes);CHKERRQ(ierr);
354cafe43deSMatthew G. Knepley   ierr = DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells);CHKERRQ(ierr);
355cafe43deSMatthew G. Knepley   ierr = DMLabelDestroy(&lbox->cellsSparse);CHKERRQ(ierr);
356cafe43deSMatthew G. Knepley   *localBox = lbox;
357cafe43deSMatthew G. Knepley   PetscFunctionReturn(0);
358cafe43deSMatthew G. Knepley }
359cafe43deSMatthew G. Knepley 
360cafe43deSMatthew G. Knepley #undef __FUNCT__
361ccd2543fSMatthew G Knepley #define __FUNCT__ "DMLocatePoints_Plex"
362ccd2543fSMatthew G Knepley /*
363ccd2543fSMatthew G Knepley  Need to implement using the guess
364ccd2543fSMatthew G Knepley */
365ccd2543fSMatthew G Knepley PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, IS *cellIS)
366ccd2543fSMatthew G Knepley {
367cafe43deSMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
368ccd2543fSMatthew G Knepley   PetscInt        bs, numPoints, p;
369*1318edbeSMatthew G. Knepley   PetscInt        dim, cStart, cEnd, cMax, numCells, c;
370cafe43deSMatthew G. Knepley   const PetscInt *boxCells;
371ccd2543fSMatthew G Knepley   PetscInt       *cells;
372ccd2543fSMatthew G Knepley   PetscScalar    *a;
373ccd2543fSMatthew G Knepley   PetscErrorCode  ierr;
374ccd2543fSMatthew G Knepley 
375ccd2543fSMatthew G Knepley   PetscFunctionBegin;
376cafe43deSMatthew G. Knepley   if (!mesh->lbox) {ierr = DMPlexComputeGridHash_Internal(dm, &mesh->lbox);CHKERRQ(ierr);}
377cafe43deSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
378cafe43deSMatthew G. Knepley   ierr = VecGetBlockSize(v, &bs);CHKERRQ(ierr);
379cafe43deSMatthew G. Knepley   if (bs != dim) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Block size for point vector %D must be the mesh coordinate dimension %D", bs, dim);
380ccd2543fSMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
381ccd2543fSMatthew G Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
382ccd2543fSMatthew G Knepley   if (cMax >= 0) cEnd = PetscMin(cEnd, cMax);
383ccd2543fSMatthew G Knepley   ierr = VecGetLocalSize(v, &numPoints);CHKERRQ(ierr);
384ccd2543fSMatthew G Knepley   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
385ccd2543fSMatthew G Knepley   numPoints /= bs;
386785e854fSJed Brown   ierr       = PetscMalloc1(numPoints, &cells);CHKERRQ(ierr);
387cafe43deSMatthew G. Knepley   /* Designate the local box for each point */
388cafe43deSMatthew G. Knepley   /* Send points to correct process */
389cafe43deSMatthew G. Knepley   /* Search cells that lie in each subbox */
390cafe43deSMatthew G. Knepley   /*   Should we bin points before doing search? */
391cafe43deSMatthew G. Knepley   ierr = ISGetIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);
392ccd2543fSMatthew G Knepley   for (p = 0; p < numPoints; ++p) {
393ccd2543fSMatthew G Knepley     const PetscScalar *point = &a[p*bs];
394*1318edbeSMatthew G. Knepley     PetscInt           dbin[3], bin, cell, cellOffset;
395ccd2543fSMatthew G Knepley 
396cafe43deSMatthew G. Knepley     ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr);
397cafe43deSMatthew G. Knepley     /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */
398cafe43deSMatthew G. Knepley     ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr);
399cafe43deSMatthew G. Knepley     ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr);
400cafe43deSMatthew G. Knepley     for (c = cellOffset; c < cellOffset + numCells; ++c) {
401cafe43deSMatthew G. Knepley       ierr = DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell);CHKERRQ(ierr);
402ccd2543fSMatthew G Knepley       if (cell >= 0) break;
403ccd2543fSMatthew G Knepley     }
404ccd2543fSMatthew G Knepley     cells[p] = cell;
405ccd2543fSMatthew G Knepley   }
406cafe43deSMatthew G. Knepley   ierr = ISRestoreIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr);
407cafe43deSMatthew G. Knepley   /* Check for highest numbered proc that claims a point (do we care?) */
408ccd2543fSMatthew G Knepley   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
409ccd2543fSMatthew G Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints, cells, PETSC_OWN_POINTER, cellIS);CHKERRQ(ierr);
410ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
411ccd2543fSMatthew G Knepley }
412ccd2543fSMatthew G Knepley 
413ccd2543fSMatthew G Knepley #undef __FUNCT__
41417fe8556SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeProjection2Dto1D_Internal"
41517fe8556SMatthew G. Knepley /*
41617fe8556SMatthew G. Knepley   DMPlexComputeProjection2Dto1D_Internal - Rewrite coordinates to be the 1D projection of the 2D
41717fe8556SMatthew G. Knepley */
4183beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection2Dto1D_Internal(PetscScalar coords[], PetscReal R[])
41917fe8556SMatthew G. Knepley {
42017fe8556SMatthew G. Knepley   const PetscReal x = PetscRealPart(coords[2] - coords[0]);
42117fe8556SMatthew G. Knepley   const PetscReal y = PetscRealPart(coords[3] - coords[1]);
4228b49ba18SBarry Smith   const PetscReal r = PetscSqrtReal(x*x + y*y), c = x/r, s = y/r;
42317fe8556SMatthew G. Knepley 
42417fe8556SMatthew G. Knepley   PetscFunctionBegin;
4251c99cf0cSGeoffrey Irving   R[0] = c; R[1] = -s;
4261c99cf0cSGeoffrey Irving   R[2] = s; R[3] =  c;
42717fe8556SMatthew G. Knepley   coords[0] = 0.0;
4287f07f362SMatthew G. Knepley   coords[1] = r;
42917fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
43017fe8556SMatthew G. Knepley }
43117fe8556SMatthew G. Knepley 
43217fe8556SMatthew G. Knepley #undef __FUNCT__
43328dbe442SToby Isaac #define __FUNCT__ "DMPlexComputeProjection3Dto1D_Internal"
43428dbe442SToby Isaac /*
43528dbe442SToby Isaac   DMPlexComputeProjection3Dto1D_Internal - Rewrite coordinates to be the 1D projection of the 3D
43628dbe442SToby Isaac 
43728dbe442SToby Isaac   This uses the basis completion described by Frisvad,
43828dbe442SToby Isaac 
43928dbe442SToby Isaac   http://www.imm.dtu.dk/~jerf/papers/abstracts/onb.html
44028dbe442SToby Isaac   DOI:10.1080/2165347X.2012.689606
44128dbe442SToby Isaac */
4423beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto1D_Internal(PetscScalar coords[], PetscReal R[])
44328dbe442SToby Isaac {
44428dbe442SToby Isaac   PetscReal      x    = PetscRealPart(coords[3] - coords[0]);
44528dbe442SToby Isaac   PetscReal      y    = PetscRealPart(coords[4] - coords[1]);
44628dbe442SToby Isaac   PetscReal      z    = PetscRealPart(coords[5] - coords[2]);
44728dbe442SToby Isaac   PetscReal      r    = PetscSqrtReal(x*x + y*y + z*z);
44828dbe442SToby Isaac   PetscReal      rinv = 1. / r;
44928dbe442SToby Isaac   PetscFunctionBegin;
45028dbe442SToby Isaac 
45128dbe442SToby Isaac   x *= rinv; y *= rinv; z *= rinv;
45228dbe442SToby Isaac   if (x > 0.) {
45328dbe442SToby Isaac     PetscReal inv1pX   = 1./ (1. + x);
45428dbe442SToby Isaac 
45528dbe442SToby Isaac     R[0] = x; R[1] = -y;              R[2] = -z;
45628dbe442SToby Isaac     R[3] = y; R[4] = 1. - y*y*inv1pX; R[5] =     -y*z*inv1pX;
45728dbe442SToby Isaac     R[6] = z; R[7] =     -y*z*inv1pX; R[8] = 1. - z*z*inv1pX;
45828dbe442SToby Isaac   }
45928dbe442SToby Isaac   else {
46028dbe442SToby Isaac     PetscReal inv1mX   = 1./ (1. - x);
46128dbe442SToby Isaac 
46228dbe442SToby Isaac     R[0] = x; R[1] = z;               R[2] = y;
46328dbe442SToby Isaac     R[3] = y; R[4] =     -y*z*inv1mX; R[5] = 1. - y*y*inv1mX;
46428dbe442SToby Isaac     R[6] = z; R[7] = 1. - z*z*inv1mX; R[8] =     -y*z*inv1mX;
46528dbe442SToby Isaac   }
46628dbe442SToby Isaac   coords[0] = 0.0;
46728dbe442SToby Isaac   coords[1] = r;
46828dbe442SToby Isaac   PetscFunctionReturn(0);
46928dbe442SToby Isaac }
47028dbe442SToby Isaac 
47128dbe442SToby Isaac #undef __FUNCT__
472ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeProjection3Dto2D_Internal"
473ccd2543fSMatthew G Knepley /*
474ccd2543fSMatthew G Knepley   DMPlexComputeProjection3Dto2D_Internal - Rewrite coordinates to be the 2D projection of the 3D
475ccd2543fSMatthew G Knepley */
4763beb2758SMatthew G. Knepley PetscErrorCode DMPlexComputeProjection3Dto2D_Internal(PetscInt coordSize, PetscScalar coords[], PetscReal R[])
477ccd2543fSMatthew G Knepley {
4781ee9d5ecSMatthew G. Knepley   PetscReal      x1[3],  x2[3], n[3], norm;
47999dec3a6SMatthew G. Knepley   PetscReal      x1p[3], x2p[3], xnp[3];
4804a217a95SMatthew G. Knepley   PetscReal      sqrtz, alpha;
481ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
48299dec3a6SMatthew G. Knepley   PetscInt       d, e, p;
483ccd2543fSMatthew G Knepley 
484ccd2543fSMatthew G Knepley   PetscFunctionBegin;
485ccd2543fSMatthew G Knepley   /* 0) Calculate normal vector */
486ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
4871ee9d5ecSMatthew G. Knepley     x1[d] = PetscRealPart(coords[1*dim+d] - coords[0*dim+d]);
4881ee9d5ecSMatthew G. Knepley     x2[d] = PetscRealPart(coords[2*dim+d] - coords[0*dim+d]);
489ccd2543fSMatthew G Knepley   }
490ccd2543fSMatthew G Knepley   n[0] = x1[1]*x2[2] - x1[2]*x2[1];
491ccd2543fSMatthew G Knepley   n[1] = x1[2]*x2[0] - x1[0]*x2[2];
492ccd2543fSMatthew G Knepley   n[2] = x1[0]*x2[1] - x1[1]*x2[0];
4938b49ba18SBarry Smith   norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
494ccd2543fSMatthew G Knepley   n[0] /= norm;
495ccd2543fSMatthew G Knepley   n[1] /= norm;
496ccd2543fSMatthew G Knepley   n[2] /= norm;
497ccd2543fSMatthew G Knepley   /* 1) Take the normal vector and rotate until it is \hat z
498ccd2543fSMatthew G Knepley 
499ccd2543fSMatthew G Knepley     Let the normal vector be <nx, ny, nz> and alpha = 1/sqrt(1 - nz^2), then
500ccd2543fSMatthew G Knepley 
501ccd2543fSMatthew G Knepley     R = /  alpha nx nz  alpha ny nz -1/alpha \
502ccd2543fSMatthew G Knepley         | -alpha ny     alpha nx        0    |
503ccd2543fSMatthew G Knepley         \     nx            ny         nz    /
504ccd2543fSMatthew G Knepley 
505ccd2543fSMatthew G Knepley     will rotate the normal vector to \hat z
506ccd2543fSMatthew G Knepley   */
5078b49ba18SBarry Smith   sqrtz = PetscSqrtReal(1.0 - n[2]*n[2]);
50873868372SMatthew G. Knepley   /* Check for n = z */
50973868372SMatthew G. Knepley   if (sqrtz < 1.0e-10) {
5101ee9d5ecSMatthew G. Knepley     if (n[2] < 0.0) {
51199dec3a6SMatthew G. Knepley       if (coordSize > 9) {
51299dec3a6SMatthew G. Knepley         coords[2] = PetscRealPart(coords[3*dim+0] - coords[0*dim+0]);
51308b58242SMatthew G. Knepley         coords[3] = PetscRealPart(coords[3*dim+1] - coords[0*dim+1]);
51499dec3a6SMatthew G. Knepley         coords[4] = x2[0];
51599dec3a6SMatthew G. Knepley         coords[5] = x2[1];
51699dec3a6SMatthew G. Knepley         coords[6] = x1[0];
51799dec3a6SMatthew G. Knepley         coords[7] = x1[1];
51899dec3a6SMatthew G. Knepley       } else {
51973868372SMatthew G. Knepley         coords[2] = x2[0];
52073868372SMatthew G. Knepley         coords[3] = x2[1];
52173868372SMatthew G. Knepley         coords[4] = x1[0];
52273868372SMatthew G. Knepley         coords[5] = x1[1];
52399dec3a6SMatthew G. Knepley       }
524b7ad821dSMatthew G. Knepley       R[0] = 1.0; R[1] = 0.0; R[2] = 0.0;
525b7ad821dSMatthew G. Knepley       R[3] = 0.0; R[4] = 1.0; R[5] = 0.0;
526b7ad821dSMatthew G. Knepley       R[6] = 0.0; R[7] = 0.0; R[8] = -1.0;
52773868372SMatthew G. Knepley     } else {
52899dec3a6SMatthew G. Knepley       for (p = 3; p < coordSize/3; ++p) {
52999dec3a6SMatthew G. Knepley         coords[p*2+0] = PetscRealPart(coords[p*dim+0] - coords[0*dim+0]);
53099dec3a6SMatthew G. Knepley         coords[p*2+1] = PetscRealPart(coords[p*dim+1] - coords[0*dim+1]);
53199dec3a6SMatthew G. Knepley       }
53273868372SMatthew G. Knepley       coords[2] = x1[0];
53373868372SMatthew G. Knepley       coords[3] = x1[1];
53473868372SMatthew G. Knepley       coords[4] = x2[0];
53573868372SMatthew G. Knepley       coords[5] = x2[1];
536b7ad821dSMatthew G. Knepley       R[0] = 1.0; R[1] = 0.0; R[2] = 0.0;
537b7ad821dSMatthew G. Knepley       R[3] = 0.0; R[4] = 1.0; R[5] = 0.0;
538b7ad821dSMatthew G. Knepley       R[6] = 0.0; R[7] = 0.0; R[8] = 1.0;
53973868372SMatthew G. Knepley     }
54099dec3a6SMatthew G. Knepley     coords[0] = 0.0;
54199dec3a6SMatthew G. Knepley     coords[1] = 0.0;
54273868372SMatthew G. Knepley     PetscFunctionReturn(0);
54373868372SMatthew G. Knepley   }
544da18b5e6SMatthew G Knepley   alpha = 1.0/sqrtz;
545ccd2543fSMatthew G Knepley   R[0] =  alpha*n[0]*n[2]; R[1] = alpha*n[1]*n[2]; R[2] = -sqrtz;
546ccd2543fSMatthew G Knepley   R[3] = -alpha*n[1];      R[4] = alpha*n[0];      R[5] = 0.0;
547ccd2543fSMatthew G Knepley   R[6] =  n[0];            R[7] = n[1];            R[8] = n[2];
548ccd2543fSMatthew G Knepley   for (d = 0; d < dim; ++d) {
549ccd2543fSMatthew G Knepley     x1p[d] = 0.0;
550ccd2543fSMatthew G Knepley     x2p[d] = 0.0;
551ccd2543fSMatthew G Knepley     for (e = 0; e < dim; ++e) {
552ccd2543fSMatthew G Knepley       x1p[d] += R[d*dim+e]*x1[e];
553ccd2543fSMatthew G Knepley       x2p[d] += R[d*dim+e]*x2[e];
554ccd2543fSMatthew G Knepley     }
555ccd2543fSMatthew G Knepley   }
5568763be8eSMatthew G. Knepley   if (PetscAbsReal(x1p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
5578763be8eSMatthew G. Knepley   if (PetscAbsReal(x2p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated");
558ccd2543fSMatthew G Knepley   /* 2) Project to (x, y) */
55999dec3a6SMatthew G. Knepley   for (p = 3; p < coordSize/3; ++p) {
56099dec3a6SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
56199dec3a6SMatthew G. Knepley       xnp[d] = 0.0;
56299dec3a6SMatthew G. Knepley       for (e = 0; e < dim; ++e) {
56399dec3a6SMatthew G. Knepley         xnp[d] += R[d*dim+e]*PetscRealPart(coords[p*dim+e] - coords[0*dim+e]);
56499dec3a6SMatthew G. Knepley       }
56599dec3a6SMatthew G. Knepley       if (d < dim-1) coords[p*2+d] = xnp[d];
56699dec3a6SMatthew G. Knepley     }
56799dec3a6SMatthew G. Knepley   }
568ccd2543fSMatthew G Knepley   coords[0] = 0.0;
569ccd2543fSMatthew G Knepley   coords[1] = 0.0;
570ccd2543fSMatthew G Knepley   coords[2] = x1p[0];
571ccd2543fSMatthew G Knepley   coords[3] = x1p[1];
572ccd2543fSMatthew G Knepley   coords[4] = x2p[0];
573ccd2543fSMatthew G Knepley   coords[5] = x2p[1];
5747f07f362SMatthew G. Knepley   /* Output R^T which rotates \hat z to the input normal */
5757f07f362SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
5767f07f362SMatthew G. Knepley     for (e = d+1; e < dim; ++e) {
5777f07f362SMatthew G. Knepley       PetscReal tmp;
5787f07f362SMatthew G. Knepley 
5797f07f362SMatthew G. Knepley       tmp        = R[d*dim+e];
5807f07f362SMatthew G. Knepley       R[d*dim+e] = R[e*dim+d];
5817f07f362SMatthew G. Knepley       R[e*dim+d] = tmp;
5827f07f362SMatthew G. Knepley     }
5837f07f362SMatthew G. Knepley   }
584ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
585ccd2543fSMatthew G Knepley }
586ccd2543fSMatthew G Knepley 
587ccd2543fSMatthew G Knepley #undef __FUNCT__
588834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Internal"
5896322fe33SJed Brown PETSC_UNUSED
590834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[])
591834e62ceSMatthew G. Knepley {
592834e62ceSMatthew G. Knepley   /* Signed volume is 1/2 the determinant
593834e62ceSMatthew G. Knepley 
594834e62ceSMatthew G. Knepley    |  1  1  1 |
595834e62ceSMatthew G. Knepley    | x0 x1 x2 |
596834e62ceSMatthew G. Knepley    | y0 y1 y2 |
597834e62ceSMatthew G. Knepley 
598834e62ceSMatthew G. Knepley      but if x0,y0 is the origin, we have
599834e62ceSMatthew G. Knepley 
600834e62ceSMatthew G. Knepley    | x1 x2 |
601834e62ceSMatthew G. Knepley    | y1 y2 |
602834e62ceSMatthew G. Knepley   */
603834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1];
604834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1];
605834e62ceSMatthew G. Knepley   PetscReal       M[4], detM;
606834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2;
60786623015SMatthew G. Knepley   M[2] = y1; M[3] = y2;
608923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(&detM, M);
609834e62ceSMatthew G. Knepley   *vol = 0.5*detM;
610834e62ceSMatthew G. Knepley   PetscLogFlops(5.0);
611834e62ceSMatthew G. Knepley }
612834e62ceSMatthew G. Knepley 
613834e62ceSMatthew G. Knepley #undef __FUNCT__
614834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Triangle_Origin_Internal"
615834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Triangle_Origin_Internal(PetscReal *vol, PetscReal coords[])
616834e62ceSMatthew G. Knepley {
617923591dfSMatthew G. Knepley   DMPlex_Det2D_Internal(vol, coords);
618834e62ceSMatthew G. Knepley   *vol *= 0.5;
619834e62ceSMatthew G. Knepley }
620834e62ceSMatthew G. Knepley 
621834e62ceSMatthew G. Knepley #undef __FUNCT__
622834e62ceSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Internal"
6236322fe33SJed Brown PETSC_UNUSED
624834e62ceSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[])
625834e62ceSMatthew G. Knepley {
626834e62ceSMatthew G. Knepley   /* Signed volume is 1/6th of the determinant
627834e62ceSMatthew G. Knepley 
628834e62ceSMatthew G. Knepley    |  1  1  1  1 |
629834e62ceSMatthew G. Knepley    | x0 x1 x2 x3 |
630834e62ceSMatthew G. Knepley    | y0 y1 y2 y3 |
631834e62ceSMatthew G. Knepley    | z0 z1 z2 z3 |
632834e62ceSMatthew G. Knepley 
633834e62ceSMatthew G. Knepley      but if x0,y0,z0 is the origin, we have
634834e62ceSMatthew G. Knepley 
635834e62ceSMatthew G. Knepley    | x1 x2 x3 |
636834e62ceSMatthew G. Knepley    | y1 y2 y3 |
637834e62ceSMatthew G. Knepley    | z1 z2 z3 |
638834e62ceSMatthew G. Knepley   */
639834e62ceSMatthew G. Knepley   const PetscReal x1 = coords[3] - coords[0], y1 = coords[4]  - coords[1], z1 = coords[5]  - coords[2];
640834e62ceSMatthew G. Knepley   const PetscReal x2 = coords[6] - coords[0], y2 = coords[7]  - coords[1], z2 = coords[8]  - coords[2];
641834e62ceSMatthew G. Knepley   const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2];
642834e62ceSMatthew G. Knepley   PetscReal       M[9], detM;
643834e62ceSMatthew G. Knepley   M[0] = x1; M[1] = x2; M[2] = x3;
644834e62ceSMatthew G. Knepley   M[3] = y1; M[4] = y2; M[5] = y3;
645834e62ceSMatthew G. Knepley   M[6] = z1; M[7] = z2; M[8] = z3;
646923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(&detM, M);
647b7ad821dSMatthew G. Knepley   *vol = -0.16666666666666666666666*detM;
648834e62ceSMatthew G. Knepley   PetscLogFlops(10.0);
649834e62ceSMatthew G. Knepley }
650834e62ceSMatthew G. Knepley 
651834e62ceSMatthew G. Knepley #undef __FUNCT__
6520ec8681fSMatthew G. Knepley #define __FUNCT__ "Volume_Tetrahedron_Origin_Internal"
6530ec8681fSMatthew G. Knepley PETSC_STATIC_INLINE void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[])
6540ec8681fSMatthew G. Knepley {
655923591dfSMatthew G. Knepley   DMPlex_Det3D_Internal(vol, coords);
656b7ad821dSMatthew G. Knepley   *vol *= -0.16666666666666666666666;
6570ec8681fSMatthew G. Knepley }
6580ec8681fSMatthew G. Knepley 
6590ec8681fSMatthew G. Knepley #undef __FUNCT__
66017fe8556SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeLineGeometry_Internal"
66117fe8556SMatthew G. Knepley static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
66217fe8556SMatthew G. Knepley {
66317fe8556SMatthew G. Knepley   PetscSection   coordSection;
66417fe8556SMatthew G. Knepley   Vec            coordinates;
665a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
6667f07f362SMatthew G. Knepley   PetscInt       numCoords, d;
66717fe8556SMatthew G. Knepley   PetscErrorCode ierr;
66817fe8556SMatthew G. Knepley 
66917fe8556SMatthew G. Knepley   PetscFunctionBegin;
67017fe8556SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
67169d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
67217fe8556SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
6737f07f362SMatthew G. Knepley   *detJ = 0.0;
67428dbe442SToby Isaac   if (numCoords == 6) {
67528dbe442SToby Isaac     const PetscInt dim = 3;
67628dbe442SToby Isaac     PetscReal      R[9], J0;
67728dbe442SToby Isaac 
67828dbe442SToby Isaac     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
67928dbe442SToby Isaac     ierr = DMPlexComputeProjection3Dto1D_Internal(coords, R);CHKERRQ(ierr);
68028dbe442SToby Isaac     if (J)    {
68128dbe442SToby Isaac       J0   = 0.5*PetscRealPart(coords[1]);
68228dbe442SToby Isaac       J[0] = R[0]*J0; J[1] = R[1]; J[2] = R[2];
68328dbe442SToby Isaac       J[3] = R[3]*J0; J[4] = R[4]; J[5] = R[5];
68428dbe442SToby Isaac       J[6] = R[6]*J0; J[7] = R[7]; J[8] = R[8];
68528dbe442SToby Isaac       DMPlex_Det3D_Internal(detJ, J);
68628dbe442SToby Isaac     }
68728dbe442SToby Isaac     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
68828dbe442SToby Isaac   } else if (numCoords == 4) {
6897f07f362SMatthew G. Knepley     const PetscInt dim = 2;
6907f07f362SMatthew G. Knepley     PetscReal      R[4], J0;
6917f07f362SMatthew G. Knepley 
6927f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
6937f07f362SMatthew G. Knepley     ierr = DMPlexComputeProjection2Dto1D_Internal(coords, R);CHKERRQ(ierr);
69417fe8556SMatthew G. Knepley     if (J)    {
6957f07f362SMatthew G. Knepley       J0   = 0.5*PetscRealPart(coords[1]);
6967f07f362SMatthew G. Knepley       J[0] = R[0]*J0; J[1] = R[1];
6977f07f362SMatthew G. Knepley       J[2] = R[2]*J0; J[3] = R[3];
698923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
69917fe8556SMatthew G. Knepley     }
700923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
7017f07f362SMatthew G. Knepley   } else if (numCoords == 2) {
7027f07f362SMatthew G. Knepley     const PetscInt dim = 1;
7037f07f362SMatthew G. Knepley 
7047f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
7057f07f362SMatthew G. Knepley     if (J)    {
7067f07f362SMatthew G. Knepley       J[0]  = 0.5*(PetscRealPart(coords[1]) - PetscRealPart(coords[0]));
70717fe8556SMatthew G. Knepley       *detJ = J[0];
70817fe8556SMatthew G. Knepley       PetscLogFlops(2.0);
70917fe8556SMatthew G. Knepley     }
7107f07f362SMatthew G. Knepley     if (invJ) {invJ[0] = 1.0/J[0]; PetscLogFlops(1.0);}
711796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this segment is %D != 2", numCoords);
71217fe8556SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
71317fe8556SMatthew G. Knepley   PetscFunctionReturn(0);
71417fe8556SMatthew G. Knepley }
71517fe8556SMatthew G. Knepley 
71617fe8556SMatthew G. Knepley #undef __FUNCT__
717ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTriangleGeometry_Internal"
718ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
719ccd2543fSMatthew G Knepley {
720ccd2543fSMatthew G Knepley   PetscSection   coordSection;
721ccd2543fSMatthew G Knepley   Vec            coordinates;
722a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
7237f07f362SMatthew G. Knepley   PetscInt       numCoords, d, f, g;
724ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
725ccd2543fSMatthew G Knepley 
726ccd2543fSMatthew G Knepley   PetscFunctionBegin;
727ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
72869d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
729ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
7307f07f362SMatthew G. Knepley   *detJ = 0.0;
731ccd2543fSMatthew G Knepley   if (numCoords == 9) {
7327f07f362SMatthew G. Knepley     const PetscInt dim = 3;
7337f07f362SMatthew 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};
7347f07f362SMatthew G. Knepley 
7357f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
73699dec3a6SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D_Internal(numCoords, coords, R);CHKERRQ(ierr);
7377f07f362SMatthew G. Knepley     if (J)    {
738b7ad821dSMatthew G. Knepley       const PetscInt pdim = 2;
739b7ad821dSMatthew G. Knepley 
740b7ad821dSMatthew G. Knepley       for (d = 0; d < pdim; d++) {
741b7ad821dSMatthew G. Knepley         for (f = 0; f < pdim; f++) {
742b7ad821dSMatthew G. Knepley           J0[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
743ccd2543fSMatthew G Knepley         }
7447f07f362SMatthew G. Knepley       }
7457f07f362SMatthew G. Knepley       PetscLogFlops(8.0);
746923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
7477f07f362SMatthew G. Knepley       for (d = 0; d < dim; d++) {
7487f07f362SMatthew G. Knepley         for (f = 0; f < dim; f++) {
7497f07f362SMatthew G. Knepley           J[d*dim+f] = 0.0;
7507f07f362SMatthew G. Knepley           for (g = 0; g < dim; g++) {
7517f07f362SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
7527f07f362SMatthew G. Knepley           }
7537f07f362SMatthew G. Knepley         }
7547f07f362SMatthew G. Knepley       }
7557f07f362SMatthew G. Knepley       PetscLogFlops(18.0);
7567f07f362SMatthew G. Knepley     }
757923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
7587f07f362SMatthew G. Knepley   } else if (numCoords == 6) {
7597f07f362SMatthew G. Knepley     const PetscInt dim = 2;
7607f07f362SMatthew G. Knepley 
7617f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
762ccd2543fSMatthew G Knepley     if (J)    {
763ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
764ccd2543fSMatthew G Knepley         for (f = 0; f < dim; f++) {
765ccd2543fSMatthew G Knepley           J[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*dim+d]) - PetscRealPart(coords[0*dim+d]));
766ccd2543fSMatthew G Knepley         }
767ccd2543fSMatthew G Knepley       }
7687f07f362SMatthew G. Knepley       PetscLogFlops(8.0);
769923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
770ccd2543fSMatthew G Knepley     }
771923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
772796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %D != 6 or 9", numCoords);
773ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
774ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
775ccd2543fSMatthew G Knepley }
776ccd2543fSMatthew G Knepley 
777ccd2543fSMatthew G Knepley #undef __FUNCT__
778ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeRectangleGeometry_Internal"
779ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
780ccd2543fSMatthew G Knepley {
781ccd2543fSMatthew G Knepley   PetscSection   coordSection;
782ccd2543fSMatthew G Knepley   Vec            coordinates;
783a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
78499dec3a6SMatthew G. Knepley   PetscInt       numCoords, d, f, g;
785ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
786ccd2543fSMatthew G Knepley 
787ccd2543fSMatthew G Knepley   PetscFunctionBegin;
788ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
78969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
79099dec3a6SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
7917f07f362SMatthew G. Knepley   *detJ = 0.0;
79299dec3a6SMatthew G. Knepley   if (numCoords == 12) {
79399dec3a6SMatthew G. Knepley     const PetscInt dim = 3;
79499dec3a6SMatthew 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};
79599dec3a6SMatthew G. Knepley 
79699dec3a6SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
79799dec3a6SMatthew G. Knepley     ierr = DMPlexComputeProjection3Dto2D_Internal(numCoords, coords, R);CHKERRQ(ierr);
79899dec3a6SMatthew G. Knepley     if (J)    {
79999dec3a6SMatthew G. Knepley       const PetscInt pdim = 2;
80099dec3a6SMatthew G. Knepley 
80199dec3a6SMatthew G. Knepley       for (d = 0; d < pdim; d++) {
80299dec3a6SMatthew G. Knepley         J0[d*dim+0] = 0.5*(PetscRealPart(coords[1*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
80399dec3a6SMatthew G. Knepley         J0[d*dim+1] = 0.5*(PetscRealPart(coords[3*pdim+d]) - PetscRealPart(coords[0*pdim+d]));
80499dec3a6SMatthew G. Knepley       }
80599dec3a6SMatthew G. Knepley       PetscLogFlops(8.0);
806923591dfSMatthew G. Knepley       DMPlex_Det3D_Internal(detJ, J0);
80799dec3a6SMatthew G. Knepley       for (d = 0; d < dim; d++) {
80899dec3a6SMatthew G. Knepley         for (f = 0; f < dim; f++) {
80999dec3a6SMatthew G. Knepley           J[d*dim+f] = 0.0;
81099dec3a6SMatthew G. Knepley           for (g = 0; g < dim; g++) {
81199dec3a6SMatthew G. Knepley             J[d*dim+f] += R[d*dim+g]*J0[g*dim+f];
81299dec3a6SMatthew G. Knepley           }
81399dec3a6SMatthew G. Knepley         }
81499dec3a6SMatthew G. Knepley       }
81599dec3a6SMatthew G. Knepley       PetscLogFlops(18.0);
81699dec3a6SMatthew G. Knepley     }
817923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
81897f1a218SMatthew G. Knepley   } else if ((numCoords == 8) || (numCoords == 16)) {
81999dec3a6SMatthew G. Knepley     const PetscInt dim = 2;
82099dec3a6SMatthew G. Knepley 
8217f07f362SMatthew G. Knepley     if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
822ccd2543fSMatthew G Knepley     if (J)    {
823ccd2543fSMatthew G Knepley       for (d = 0; d < dim; d++) {
82499dec3a6SMatthew G. Knepley         J[d*dim+0] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
82599dec3a6SMatthew G. Knepley         J[d*dim+1] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
826ccd2543fSMatthew G Knepley       }
8277f07f362SMatthew G. Knepley       PetscLogFlops(8.0);
828923591dfSMatthew G. Knepley       DMPlex_Det2D_Internal(detJ, J);
829ccd2543fSMatthew G Knepley     }
830923591dfSMatthew G. Knepley     if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
831796f034aSJed Brown   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords);
83299dec3a6SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr);
833ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
834ccd2543fSMatthew G Knepley }
835ccd2543fSMatthew G Knepley 
836ccd2543fSMatthew G Knepley #undef __FUNCT__
837ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeTetrahedronGeometry_Internal"
838ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
839ccd2543fSMatthew G Knepley {
840ccd2543fSMatthew G Knepley   PetscSection   coordSection;
841ccd2543fSMatthew G Knepley   Vec            coordinates;
842a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
843ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
84499dec3a6SMatthew G. Knepley   PetscInt       d;
845ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
846ccd2543fSMatthew G Knepley 
847ccd2543fSMatthew G Knepley   PetscFunctionBegin;
848ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
84969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
850ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
8517f07f362SMatthew G. Knepley   *detJ = 0.0;
8527f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
853ccd2543fSMatthew G Knepley   if (J)    {
854ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
855f0df753eSMatthew G. Knepley       /* I orient with outward face normals */
856f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d]));
857f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
858f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
859ccd2543fSMatthew G Knepley     }
8607f07f362SMatthew G. Knepley     PetscLogFlops(18.0);
861923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
862ccd2543fSMatthew G Knepley   }
863923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
864ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
865ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
866ccd2543fSMatthew G Knepley }
867ccd2543fSMatthew G Knepley 
868ccd2543fSMatthew G Knepley #undef __FUNCT__
869ccd2543fSMatthew G Knepley #define __FUNCT__ "DMPlexComputeHexahedronGeometry_Internal"
870ccd2543fSMatthew G Knepley static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
871ccd2543fSMatthew G Knepley {
872ccd2543fSMatthew G Knepley   PetscSection   coordSection;
873ccd2543fSMatthew G Knepley   Vec            coordinates;
874a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
875ccd2543fSMatthew G Knepley   const PetscInt dim = 3;
876ccd2543fSMatthew G Knepley   PetscInt       d;
877ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
878ccd2543fSMatthew G Knepley 
879ccd2543fSMatthew G Knepley   PetscFunctionBegin;
880ccd2543fSMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
88169d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
882ccd2543fSMatthew G Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
8837f07f362SMatthew G. Knepley   *detJ = 0.0;
8847f07f362SMatthew G. Knepley   if (v0)   {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);}
885ccd2543fSMatthew G Knepley   if (J)    {
886ccd2543fSMatthew G Knepley     for (d = 0; d < dim; d++) {
887f0df753eSMatthew G. Knepley       J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d]));
888f0df753eSMatthew G. Knepley       J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d]));
889f0df753eSMatthew G. Knepley       J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d]));
890ccd2543fSMatthew G Knepley     }
8917f07f362SMatthew G. Knepley     PetscLogFlops(18.0);
892923591dfSMatthew G. Knepley     DMPlex_Det3D_Internal(detJ, J);
893ccd2543fSMatthew G Knepley   }
894923591dfSMatthew G. Knepley   if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
895ccd2543fSMatthew G Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr);
896ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
897ccd2543fSMatthew G Knepley }
898ccd2543fSMatthew G Knepley 
899ccd2543fSMatthew G Knepley #undef __FUNCT__
9008e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryAffineFEM"
901ccd2543fSMatthew G Knepley /*@C
9028e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell
903ccd2543fSMatthew G Knepley 
904ccd2543fSMatthew G Knepley   Collective on DM
905ccd2543fSMatthew G Knepley 
906ccd2543fSMatthew G Knepley   Input Arguments:
907ccd2543fSMatthew G Knepley + dm   - the DM
908ccd2543fSMatthew G Knepley - cell - the cell
909ccd2543fSMatthew G Knepley 
910ccd2543fSMatthew G Knepley   Output Arguments:
911ccd2543fSMatthew G Knepley + v0   - the translation part of this affine transform
912ccd2543fSMatthew G Knepley . J    - the Jacobian of the transform from the reference element
913ccd2543fSMatthew G Knepley . invJ - the inverse of the Jacobian
914ccd2543fSMatthew G Knepley - detJ - the Jacobian determinant
915ccd2543fSMatthew G Knepley 
916ccd2543fSMatthew G Knepley   Level: advanced
917ccd2543fSMatthew G Knepley 
918ccd2543fSMatthew G Knepley   Fortran Notes:
919ccd2543fSMatthew G Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
920ccd2543fSMatthew G Knepley   include petsc.h90 in your code.
921ccd2543fSMatthew G Knepley 
9228e0841e0SMatthew G. Knepley .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinateVec()
923ccd2543fSMatthew G Knepley @*/
9248e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
925ccd2543fSMatthew G Knepley {
92649dc4407SMatthew G. Knepley   PetscInt       depth, dim, coneSize;
927ccd2543fSMatthew G Knepley   PetscErrorCode ierr;
928ccd2543fSMatthew G Knepley 
929ccd2543fSMatthew G Knepley   PetscFunctionBegin;
930139a35ccSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
931ccd2543fSMatthew G Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
93249dc4407SMatthew G. Knepley   if (depth == 1) {
9338e0841e0SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
9348e0841e0SMatthew G. Knepley   } else {
9358e0841e0SMatthew G. Knepley     DMLabel depth;
9368e0841e0SMatthew G. Knepley 
9378e0841e0SMatthew G. Knepley     ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
9388e0841e0SMatthew G. Knepley     ierr = DMLabelGetValue(depth, cell, &dim);CHKERRQ(ierr);
9398e0841e0SMatthew G. Knepley   }
940ccd2543fSMatthew G Knepley   switch (dim) {
94117fe8556SMatthew G. Knepley   case 1:
94217fe8556SMatthew G. Knepley     ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
94317fe8556SMatthew G. Knepley     break;
944ccd2543fSMatthew G Knepley   case 2:
945ccd2543fSMatthew G Knepley     switch (coneSize) {
946ccd2543fSMatthew G Knepley     case 3:
947ccd2543fSMatthew G Knepley       ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
948ccd2543fSMatthew G Knepley       break;
949ccd2543fSMatthew G Knepley     case 4:
950ccd2543fSMatthew G Knepley       ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
951ccd2543fSMatthew G Knepley       break;
952ccd2543fSMatthew G Knepley     default:
9538e0841e0SMatthew G. Knepley       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
954ccd2543fSMatthew G Knepley     }
955ccd2543fSMatthew G Knepley     break;
956ccd2543fSMatthew G Knepley   case 3:
957ccd2543fSMatthew G Knepley     switch (coneSize) {
958ccd2543fSMatthew G Knepley     case 4:
959ccd2543fSMatthew G Knepley       ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
960ccd2543fSMatthew G Knepley       break;
9618e0841e0SMatthew G. Knepley     case 6: /* Faces */
9628e0841e0SMatthew G. Knepley     case 8: /* Vertices */
963ccd2543fSMatthew G Knepley       ierr = DMPlexComputeHexahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);
964ccd2543fSMatthew G Knepley       break;
965ccd2543fSMatthew G Knepley     default:
9668e0841e0SMatthew G. Knepley         SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell);
967ccd2543fSMatthew G Knepley     }
968ccd2543fSMatthew G Knepley       break;
969ccd2543fSMatthew G Knepley   default:
970ccd2543fSMatthew G Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
971ccd2543fSMatthew G Knepley   }
9728e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
9738e0841e0SMatthew G. Knepley }
9748e0841e0SMatthew G. Knepley 
9758e0841e0SMatthew G. Knepley #undef __FUNCT__
9768e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIsoparametricGeometry_Internal"
9778e0841e0SMatthew G. Knepley static PetscErrorCode DMPlexComputeIsoparametricGeometry_Internal(DM dm, PetscFE fe, PetscInt point, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
9788e0841e0SMatthew G. Knepley {
9798e0841e0SMatthew G. Knepley   PetscQuadrature  quad;
9808e0841e0SMatthew G. Knepley   PetscSection     coordSection;
9818e0841e0SMatthew G. Knepley   Vec              coordinates;
9828e0841e0SMatthew G. Knepley   PetscScalar     *coords = NULL;
9838e0841e0SMatthew G. Knepley   const PetscReal *quadPoints;
9848e0841e0SMatthew G. Knepley   PetscReal       *basisDer;
9858e0841e0SMatthew G. Knepley   PetscInt         dim, cdim, pdim, qdim, Nq, numCoords, d, q;
9868e0841e0SMatthew G. Knepley   PetscErrorCode   ierr;
9878e0841e0SMatthew G. Knepley 
9888e0841e0SMatthew G. Knepley   PetscFunctionBegin;
9898e0841e0SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
9908e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
9918e0841e0SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
9928e0841e0SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
9938e0841e0SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
9948e0841e0SMatthew G. Knepley   ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
995954b1791SMatthew G. Knepley   ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr);
9968e0841e0SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, &qdim, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
9978e0841e0SMatthew G. Knepley   ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr);
9988e0841e0SMatthew G. Knepley   *detJ = 0.0;
9998e0841e0SMatthew G. Knepley   if (qdim != dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim);
10008e0841e0SMatthew 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);
10018e0841e0SMatthew G. Knepley   if (v0) {for (d = 0; d < cdim; d++) v0[d] = PetscRealPart(coords[d]);}
10028e0841e0SMatthew G. Knepley   if (J) {
10038e0841e0SMatthew G. Knepley     for (q = 0; q < Nq; ++q) {
10048e0841e0SMatthew G. Knepley       PetscInt i, j, k, c, r;
10058e0841e0SMatthew G. Knepley 
10068e0841e0SMatthew G. Knepley       /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */
10078e0841e0SMatthew G. Knepley       for (k = 0; k < pdim; ++k)
10088e0841e0SMatthew G. Knepley         for (j = 0; j < dim; ++j)
10098e0841e0SMatthew G. Knepley           for (i = 0; i < cdim; ++i)
101071d6e60fSMatthew G. Knepley             J[(q*cdim + i)*dim + j] += basisDer[(q*pdim + k)*dim + j] * PetscRealPart(coords[k*cdim + i]);
10118e0841e0SMatthew G. Knepley       PetscLogFlops(2.0*pdim*dim*cdim);
10128e0841e0SMatthew G. Knepley       if (cdim > dim) {
10138e0841e0SMatthew G. Knepley         for (c = dim; c < cdim; ++c)
10148e0841e0SMatthew G. Knepley           for (r = 0; r < cdim; ++r)
10158e0841e0SMatthew G. Knepley             J[r*cdim+c] = r == c ? 1.0 : 0.0;
10168e0841e0SMatthew G. Knepley       }
10178e0841e0SMatthew G. Knepley       switch (cdim) {
10188e0841e0SMatthew G. Knepley       case 3:
10198e0841e0SMatthew G. Knepley         DMPlex_Det3D_Internal(detJ, J);
10208e0841e0SMatthew G. Knepley         if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);}
102117fe8556SMatthew G. Knepley         break;
102249dc4407SMatthew G. Knepley       case 2:
10238e0841e0SMatthew G. Knepley         DMPlex_Det2D_Internal(detJ, J);
10248e0841e0SMatthew G. Knepley         if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);}
102549dc4407SMatthew G. Knepley         break;
10268e0841e0SMatthew G. Knepley       case 1:
10278e0841e0SMatthew G. Knepley         *detJ = J[0];
10288e0841e0SMatthew G. Knepley         if (invJ) invJ[0] = 1.0/J[0];
102949dc4407SMatthew G. Knepley       }
103049dc4407SMatthew G. Knepley     }
10318e0841e0SMatthew G. Knepley   }
10328e0841e0SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr);
10338e0841e0SMatthew G. Knepley   PetscFunctionReturn(0);
10348e0841e0SMatthew G. Knepley }
10358e0841e0SMatthew G. Knepley 
10368e0841e0SMatthew G. Knepley #undef __FUNCT__
10378e0841e0SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFEM"
10388e0841e0SMatthew G. Knepley /*@C
10398e0841e0SMatthew G. Knepley   DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell
10408e0841e0SMatthew G. Knepley 
10418e0841e0SMatthew G. Knepley   Collective on DM
10428e0841e0SMatthew G. Knepley 
10438e0841e0SMatthew G. Knepley   Input Arguments:
10448e0841e0SMatthew G. Knepley + dm   - the DM
10458e0841e0SMatthew G. Knepley . cell - the cell
10468e0841e0SMatthew G. Knepley - fe   - the finite element containing the quadrature
10478e0841e0SMatthew G. Knepley 
10488e0841e0SMatthew G. Knepley   Output Arguments:
10498e0841e0SMatthew G. Knepley + v0   - the translation part of this transform
10508e0841e0SMatthew G. Knepley . J    - the Jacobian of the transform from the reference element at each quadrature point
10518e0841e0SMatthew G. Knepley . invJ - the inverse of the Jacobian at each quadrature point
10528e0841e0SMatthew G. Knepley - detJ - the Jacobian determinant at each quadrature point
10538e0841e0SMatthew G. Knepley 
10548e0841e0SMatthew G. Knepley   Level: advanced
10558e0841e0SMatthew G. Knepley 
10568e0841e0SMatthew G. Knepley   Fortran Notes:
10578e0841e0SMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
10588e0841e0SMatthew G. Knepley   include petsc.h90 in your code.
10598e0841e0SMatthew G. Knepley 
10608e0841e0SMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec()
10618e0841e0SMatthew G. Knepley @*/
10628e0841e0SMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscFE fe, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ)
10638e0841e0SMatthew G. Knepley {
10648e0841e0SMatthew G. Knepley   PetscErrorCode ierr;
10658e0841e0SMatthew G. Knepley 
10668e0841e0SMatthew G. Knepley   PetscFunctionBegin;
10678e0841e0SMatthew G. Knepley   if (!fe) {ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);}
10688e0841e0SMatthew G. Knepley   else     {ierr = DMPlexComputeIsoparametricGeometry_Internal(dm, fe, cell, v0, J, invJ, detJ);CHKERRQ(ierr);}
1069ccd2543fSMatthew G Knepley   PetscFunctionReturn(0);
1070ccd2543fSMatthew G Knepley }
1071834e62ceSMatthew G. Knepley 
1072834e62ceSMatthew G. Knepley #undef __FUNCT__
1073cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_1D_Internal"
1074011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1075cc08537eSMatthew G. Knepley {
1076cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1077cc08537eSMatthew G. Knepley   Vec            coordinates;
1078a1e44745SMatthew G. Knepley   PetscScalar   *coords = NULL;
107906e2781eSMatthew G. Knepley   PetscScalar    tmp[2];
1080cc08537eSMatthew G. Knepley   PetscInt       coordSize;
1081cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1082cc08537eSMatthew G. Knepley 
1083cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1084cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
108569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1086cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1087011ea5d8SMatthew G. Knepley   if (dim != 2) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "We only support 2D edges right now");
108806e2781eSMatthew G. Knepley   ierr = DMPlexLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp);CHKERRQ(ierr);
1089cc08537eSMatthew G. Knepley   if (centroid) {
109006e2781eSMatthew G. Knepley     centroid[0] = 0.5*PetscRealPart(coords[0] + tmp[0]);
109106e2781eSMatthew G. Knepley     centroid[1] = 0.5*PetscRealPart(coords[1] + tmp[1]);
1092cc08537eSMatthew G. Knepley   }
1093cc08537eSMatthew G. Knepley   if (normal) {
1094a60a936bSMatthew G. Knepley     PetscReal norm;
1095a60a936bSMatthew G. Knepley 
109606e2781eSMatthew G. Knepley     normal[0]  = -PetscRealPart(coords[1] - tmp[1]);
109706e2781eSMatthew G. Knepley     normal[1]  =  PetscRealPart(coords[0] - tmp[0]);
1098a60a936bSMatthew G. Knepley     norm       = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1]);
1099a60a936bSMatthew G. Knepley     normal[0] /= norm;
1100a60a936bSMatthew G. Knepley     normal[1] /= norm;
1101cc08537eSMatthew G. Knepley   }
1102cc08537eSMatthew G. Knepley   if (vol) {
110306e2781eSMatthew G. Knepley     *vol = PetscSqrtReal(PetscSqr(PetscRealPart(coords[0] - tmp[0])) + PetscSqr(PetscRealPart(coords[1] - tmp[1])));
1104cc08537eSMatthew G. Knepley   }
1105cc08537eSMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1106cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1107cc08537eSMatthew G. Knepley }
1108cc08537eSMatthew G. Knepley 
1109cc08537eSMatthew G. Knepley #undef __FUNCT__
1110cc08537eSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_2D_Internal"
1111cc08537eSMatthew G. Knepley /* Centroid_i = (\sum_n A_n Cn_i ) / A */
1112011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1113cc08537eSMatthew G. Knepley {
1114cc08537eSMatthew G. Knepley   PetscSection   coordSection;
1115cc08537eSMatthew G. Knepley   Vec            coordinates;
1116cc08537eSMatthew G. Knepley   PetscScalar   *coords = NULL;
11170a1d6728SMatthew G. Knepley   PetscReal      vsum = 0.0, csum[3] = {0.0, 0.0, 0.0}, vtmp, ctmp[4], v0[3], R[9];
11180a1d6728SMatthew G. Knepley   PetscInt       tdim = 2, coordSize, numCorners, p, d, e;
1119cc08537eSMatthew G. Knepley   PetscErrorCode ierr;
1120cc08537eSMatthew G. Knepley 
1121cc08537eSMatthew G. Knepley   PetscFunctionBegin;
1122cc08537eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
11230a1d6728SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numCorners);CHKERRQ(ierr);
112469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1125cc08537eSMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
11260bce18caSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1127011ea5d8SMatthew G. Knepley   if (normal) {
1128011ea5d8SMatthew G. Knepley     if (dim > 2) {
11291ee9d5ecSMatthew G. Knepley       const PetscReal x0 = PetscRealPart(coords[dim+0] - coords[0]), x1 = PetscRealPart(coords[dim*2+0] - coords[0]);
11301ee9d5ecSMatthew G. Knepley       const PetscReal y0 = PetscRealPart(coords[dim+1] - coords[1]), y1 = PetscRealPart(coords[dim*2+1] - coords[1]);
11311ee9d5ecSMatthew G. Knepley       const PetscReal z0 = PetscRealPart(coords[dim+2] - coords[2]), z1 = PetscRealPart(coords[dim*2+2] - coords[2]);
11320a1d6728SMatthew G. Knepley       PetscReal       norm;
11330a1d6728SMatthew G. Knepley 
11341ee9d5ecSMatthew G. Knepley       v0[0]     = PetscRealPart(coords[0]);
11351ee9d5ecSMatthew G. Knepley       v0[1]     = PetscRealPart(coords[1]);
11361ee9d5ecSMatthew G. Knepley       v0[2]     = PetscRealPart(coords[2]);
11370a1d6728SMatthew G. Knepley       normal[0] = y0*z1 - z0*y1;
11380a1d6728SMatthew G. Knepley       normal[1] = z0*x1 - x0*z1;
11390a1d6728SMatthew G. Knepley       normal[2] = x0*y1 - y0*x1;
11408b49ba18SBarry Smith       norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2]);
11410a1d6728SMatthew G. Knepley       normal[0] /= norm;
11420a1d6728SMatthew G. Knepley       normal[1] /= norm;
11430a1d6728SMatthew G. Knepley       normal[2] /= norm;
1144011ea5d8SMatthew G. Knepley     } else {
1145011ea5d8SMatthew G. Knepley       for (d = 0; d < dim; ++d) normal[d] = 0.0;
1146011ea5d8SMatthew G. Knepley     }
1147011ea5d8SMatthew G. Knepley   }
114899dec3a6SMatthew G. Knepley   if (dim == 3) {ierr = DMPlexComputeProjection3Dto2D_Internal(coordSize, coords, R);CHKERRQ(ierr);}
11490a1d6728SMatthew G. Knepley   for (p = 0; p < numCorners; ++p) {
11500a1d6728SMatthew G. Knepley     /* Need to do this copy to get types right */
11510a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
11521ee9d5ecSMatthew G. Knepley       ctmp[d]      = PetscRealPart(coords[p*tdim+d]);
11531ee9d5ecSMatthew G. Knepley       ctmp[tdim+d] = PetscRealPart(coords[((p+1)%numCorners)*tdim+d]);
11540a1d6728SMatthew G. Knepley     }
11550a1d6728SMatthew G. Knepley     Volume_Triangle_Origin_Internal(&vtmp, ctmp);
11560a1d6728SMatthew G. Knepley     vsum += vtmp;
11570a1d6728SMatthew G. Knepley     for (d = 0; d < tdim; ++d) {
11580a1d6728SMatthew G. Knepley       csum[d] += (ctmp[d] + ctmp[tdim+d])*vtmp;
11590a1d6728SMatthew G. Knepley     }
11600a1d6728SMatthew G. Knepley   }
11610a1d6728SMatthew G. Knepley   for (d = 0; d < tdim; ++d) {
11620a1d6728SMatthew G. Knepley     csum[d] /= (tdim+1)*vsum;
11630a1d6728SMatthew G. Knepley   }
11640a1d6728SMatthew G. Knepley   ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr);
1165ee6bbdb2SSatish Balay   if (vol) *vol = PetscAbsReal(vsum);
11660a1d6728SMatthew G. Knepley   if (centroid) {
11670a1d6728SMatthew G. Knepley     if (dim > 2) {
11680a1d6728SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
11690a1d6728SMatthew G. Knepley         centroid[d] = v0[d];
11700a1d6728SMatthew G. Knepley         for (e = 0; e < dim; ++e) {
11710a1d6728SMatthew G. Knepley           centroid[d] += R[d*dim+e]*csum[e];
11720a1d6728SMatthew G. Knepley         }
11730a1d6728SMatthew G. Knepley       }
11740a1d6728SMatthew G. Knepley     } else for (d = 0; d < dim; ++d) centroid[d] = csum[d];
11750a1d6728SMatthew G. Knepley   }
1176cc08537eSMatthew G. Knepley   PetscFunctionReturn(0);
1177cc08537eSMatthew G. Knepley }
1178cc08537eSMatthew G. Knepley 
1179cc08537eSMatthew G. Knepley #undef __FUNCT__
11800ec8681fSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM_3D_Internal"
11810ec8681fSMatthew G. Knepley /* Centroid_i = (\sum_n V_n Cn_i ) / V */
1182011ea5d8SMatthew G. Knepley static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
11830ec8681fSMatthew G. Knepley {
11840ec8681fSMatthew G. Knepley   PetscSection    coordSection;
11850ec8681fSMatthew G. Knepley   Vec             coordinates;
11860ec8681fSMatthew G. Knepley   PetscScalar    *coords = NULL;
118786623015SMatthew G. Knepley   PetscReal       vsum = 0.0, vtmp, coordsTmp[3*3];
1188a7df9edeSMatthew G. Knepley   const PetscInt *faces, *facesO;
11890ec8681fSMatthew G. Knepley   PetscInt        numFaces, f, coordSize, numCorners, p, d;
11900ec8681fSMatthew G. Knepley   PetscErrorCode  ierr;
11910ec8681fSMatthew G. Knepley 
11920ec8681fSMatthew G. Knepley   PetscFunctionBegin;
1193f6dae198SJed Brown   if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim);
11940ec8681fSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
119569d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
11960ec8681fSMatthew G. Knepley 
1197d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0;
11980ec8681fSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &numFaces);CHKERRQ(ierr);
11990ec8681fSMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr);
1200a7df9edeSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &facesO);CHKERRQ(ierr);
12010ec8681fSMatthew G. Knepley   for (f = 0; f < numFaces; ++f) {
1202011ea5d8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
12030ec8681fSMatthew G. Knepley     numCorners = coordSize/dim;
12040ec8681fSMatthew G. Knepley     switch (numCorners) {
12050ec8681fSMatthew G. Knepley     case 3:
12060ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
12071ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
12081ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
12091ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]);
12100ec8681fSMatthew G. Knepley       }
12110ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1212a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
12130ec8681fSMatthew G. Knepley       vsum += vtmp;
12144f25033aSJed Brown       if (centroid) {           /* Centroid of OABC = (a+b+c)/4 */
12150ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
12161ee9d5ecSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
12170ec8681fSMatthew G. Knepley         }
12180ec8681fSMatthew G. Knepley       }
12190ec8681fSMatthew G. Knepley       break;
12200ec8681fSMatthew G. Knepley     case 4:
12210ec8681fSMatthew G. Knepley       /* DO FOR PYRAMID */
12220ec8681fSMatthew G. Knepley       /* First tet */
12230ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
12241ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]);
12251ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]);
12261ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
12270ec8681fSMatthew G. Knepley       }
12280ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1229a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
12300ec8681fSMatthew G. Knepley       vsum += vtmp;
12310ec8681fSMatthew G. Knepley       if (centroid) {
12320ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
12330ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
12340ec8681fSMatthew G. Knepley         }
12350ec8681fSMatthew G. Knepley       }
12360ec8681fSMatthew G. Knepley       /* Second tet */
12370ec8681fSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
12381ee9d5ecSMatthew G. Knepley         coordsTmp[0*dim+d] = PetscRealPart(coords[1*dim+d]);
12391ee9d5ecSMatthew G. Knepley         coordsTmp[1*dim+d] = PetscRealPart(coords[2*dim+d]);
12401ee9d5ecSMatthew G. Knepley         coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]);
12410ec8681fSMatthew G. Knepley       }
12420ec8681fSMatthew G. Knepley       Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp);
1243a7df9edeSMatthew G. Knepley       if (facesO[f] < 0) vtmp = -vtmp;
12440ec8681fSMatthew G. Knepley       vsum += vtmp;
12450ec8681fSMatthew G. Knepley       if (centroid) {
12460ec8681fSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
12470ec8681fSMatthew G. Knepley           for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp;
12480ec8681fSMatthew G. Knepley         }
12490ec8681fSMatthew G. Knepley       }
12500ec8681fSMatthew G. Knepley       break;
12510ec8681fSMatthew G. Knepley     default:
1252796f034aSJed Brown       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle faces with %D vertices", numCorners);
12530ec8681fSMatthew G. Knepley     }
12544f25033aSJed Brown     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr);
12550ec8681fSMatthew G. Knepley   }
12568763be8eSMatthew G. Knepley   if (vol)     *vol = PetscAbsReal(vsum);
12570ec8681fSMatthew G. Knepley   if (normal)   for (d = 0; d < dim; ++d) normal[d]    = 0.0;
1258d9a81ebdSMatthew G. Knepley   if (centroid) for (d = 0; d < dim; ++d) centroid[d] /= (vsum*4);
12590ec8681fSMatthew G. Knepley   PetscFunctionReturn(0);
12600ec8681fSMatthew G. Knepley }
12610ec8681fSMatthew G. Knepley 
12620ec8681fSMatthew G. Knepley #undef __FUNCT__
1263834e62ceSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeCellGeometryFVM"
1264834e62ceSMatthew G. Knepley /*@C
1265834e62ceSMatthew G. Knepley   DMPlexComputeCellGeometryFVM - Compute the volume for a given cell
1266834e62ceSMatthew G. Knepley 
1267834e62ceSMatthew G. Knepley   Collective on DM
1268834e62ceSMatthew G. Knepley 
1269834e62ceSMatthew G. Knepley   Input Arguments:
1270834e62ceSMatthew G. Knepley + dm   - the DM
1271834e62ceSMatthew G. Knepley - cell - the cell
1272834e62ceSMatthew G. Knepley 
1273834e62ceSMatthew G. Knepley   Output Arguments:
1274834e62ceSMatthew G. Knepley + volume   - the cell volume
1275cc08537eSMatthew G. Knepley . centroid - the cell centroid
1276cc08537eSMatthew G. Knepley - normal - the cell normal, if appropriate
1277834e62ceSMatthew G. Knepley 
1278834e62ceSMatthew G. Knepley   Level: advanced
1279834e62ceSMatthew G. Knepley 
1280834e62ceSMatthew G. Knepley   Fortran Notes:
1281834e62ceSMatthew G. Knepley   Since it returns arrays, this routine is only available in Fortran 90, and you must
1282834e62ceSMatthew G. Knepley   include petsc.h90 in your code.
1283834e62ceSMatthew G. Knepley 
128469d8a9ceSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetCoordinateVec()
1285834e62ceSMatthew G. Knepley @*/
1286cc08537eSMatthew G. Knepley PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[])
1287834e62ceSMatthew G. Knepley {
12880ec8681fSMatthew G. Knepley   PetscInt       depth, dim;
1289834e62ceSMatthew G. Knepley   PetscErrorCode ierr;
1290834e62ceSMatthew G. Knepley 
1291834e62ceSMatthew G. Knepley   PetscFunctionBegin;
1292834e62ceSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1293c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1294834e62ceSMatthew G. Knepley   if (depth != dim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated");
1295834e62ceSMatthew G. Knepley   /* We need to keep a pointer to the depth label */
1296011ea5d8SMatthew G. Knepley   ierr = DMPlexGetLabelValue(dm, "depth", cell, &depth);CHKERRQ(ierr);
1297834e62ceSMatthew G. Knepley   /* Cone size is now the number of faces */
1298011ea5d8SMatthew G. Knepley   switch (depth) {
1299cc08537eSMatthew G. Knepley   case 1:
1300011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1301cc08537eSMatthew G. Knepley     break;
1302834e62ceSMatthew G. Knepley   case 2:
1303011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1304834e62ceSMatthew G. Knepley     break;
1305834e62ceSMatthew G. Knepley   case 3:
1306011ea5d8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr);
1307834e62ceSMatthew G. Knepley     break;
1308834e62ceSMatthew G. Knepley   default:
1309834e62ceSMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim);
1310834e62ceSMatthew G. Knepley   }
1311834e62ceSMatthew G. Knepley   PetscFunctionReturn(0);
1312834e62ceSMatthew G. Knepley }
1313113c68e6SMatthew G. Knepley 
1314113c68e6SMatthew G. Knepley #undef __FUNCT__
1315c0d900a5SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFEM"
1316c0d900a5SMatthew G. Knepley /* This should also take a PetscFE argument I think */
1317c0d900a5SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom)
1318c0d900a5SMatthew G. Knepley {
1319c0d900a5SMatthew G. Knepley   DM             dmCell;
1320c0d900a5SMatthew G. Knepley   Vec            coordinates;
1321c0d900a5SMatthew G. Knepley   PetscSection   coordSection, sectionCell;
1322c0d900a5SMatthew G. Knepley   PetscScalar   *cgeom;
1323c0d900a5SMatthew G. Knepley   PetscInt       cStart, cEnd, cMax, c;
1324c0d900a5SMatthew G. Knepley   PetscErrorCode ierr;
1325c0d900a5SMatthew G. Knepley 
1326c0d900a5SMatthew G. Knepley   PetscFunctionBegin;
1327c0d900a5SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1328c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1329c0d900a5SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1330c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1331c0d900a5SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1332c0d900a5SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1333c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1334c0d900a5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
1335c0d900a5SMatthew G. Knepley   cEnd = cMax < 0 ? cEnd : cMax;
1336c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
1337c0d900a5SMatthew G. Knepley   /* TODO This needs to be multiplied by Nq for non-affine */
13389e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFECellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1339c0d900a5SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1340c0d900a5SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1341c0d900a5SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1342c0d900a5SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
1343c0d900a5SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1344c0d900a5SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1345c0d900a5SMatthew G. Knepley     PetscFECellGeom *cg;
1346c0d900a5SMatthew G. Knepley 
1347c0d900a5SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1348c0d900a5SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1349c0d900a5SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v0, cg->J, cg->invJ, &cg->detJ);CHKERRQ(ierr);
1350c0d900a5SMatthew G. Knepley     if (cg->detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", cg->detJ, c);
1351c0d900a5SMatthew G. Knepley   }
1352c0d900a5SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1353c0d900a5SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
1354c0d900a5SMatthew G. Knepley   PetscFunctionReturn(0);
1355c0d900a5SMatthew G. Knepley }
1356c0d900a5SMatthew G. Knepley 
1357c0d900a5SMatthew G. Knepley #undef __FUNCT__
1358113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGeometryFVM"
1359891a9168SMatthew G. Knepley /*@
1360891a9168SMatthew G. Knepley   DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method
1361891a9168SMatthew G. Knepley 
1362891a9168SMatthew G. Knepley   Input Parameter:
1363891a9168SMatthew G. Knepley . dm - The DM
1364891a9168SMatthew G. Knepley 
1365891a9168SMatthew G. Knepley   Output Parameters:
1366891a9168SMatthew G. Knepley + cellgeom - A Vec of PetscFVCellGeom data
1367891a9168SMatthew G. Knepley . facegeom - A Vec of PetscFVFaceGeom data
1368891a9168SMatthew G. Knepley 
1369891a9168SMatthew G. Knepley   Level: developer
1370891a9168SMatthew G. Knepley 
1371891a9168SMatthew G. Knepley .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM()
1372891a9168SMatthew G. Knepley @*/
1373113c68e6SMatthew G. Knepley PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom)
1374113c68e6SMatthew G. Knepley {
1375113c68e6SMatthew G. Knepley   DM             dmFace, dmCell;
1376113c68e6SMatthew G. Knepley   DMLabel        ghostLabel;
1377113c68e6SMatthew G. Knepley   PetscSection   sectionFace, sectionCell;
1378113c68e6SMatthew G. Knepley   PetscSection   coordSection;
1379113c68e6SMatthew G. Knepley   Vec            coordinates;
1380113c68e6SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
1381113c68e6SMatthew G. Knepley   PetscReal      minradius, gminradius;
1382113c68e6SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f;
1383113c68e6SMatthew G. Knepley   PetscErrorCode ierr;
1384113c68e6SMatthew G. Knepley 
1385113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1386113c68e6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1387113c68e6SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1388113c68e6SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1389113c68e6SMatthew G. Knepley   /* Make cell centroids and volumes */
1390113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmCell);CHKERRQ(ierr);
1391113c68e6SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr);
1392113c68e6SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr);
1393113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionCell);CHKERRQ(ierr);
1394113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1395113c68e6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1396113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr);
13979e5edeeeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1398113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr);
1399113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr);
1400113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionCell);CHKERRQ(ierr);
1401113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr);
1402113c68e6SMatthew G. Knepley   ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1403113c68e6SMatthew G. Knepley   for (c = cStart; c < cEndInterior; ++c) {
1404113c68e6SMatthew G. Knepley     PetscFVCellGeom *cg;
1405113c68e6SMatthew G. Knepley 
1406113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1407113c68e6SMatthew G. Knepley     ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr);
1408113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr);
1409113c68e6SMatthew G. Knepley   }
1410113c68e6SMatthew G. Knepley   /* Compute face normals and minimum cell radius */
1411113c68e6SMatthew G. Knepley   ierr = DMClone(dm, &dmFace);CHKERRQ(ierr);
1412113c68e6SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionFace);CHKERRQ(ierr);
1413113c68e6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1414113c68e6SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr);
14159e5edeeeSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);}
1416113c68e6SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr);
1417113c68e6SMatthew G. Knepley   ierr = DMSetDefaultSection(dmFace, sectionFace);CHKERRQ(ierr);
1418113c68e6SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionFace);CHKERRQ(ierr);
1419113c68e6SMatthew G. Knepley   ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr);
1420113c68e6SMatthew G. Knepley   ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr);
1421113c68e6SMatthew G. Knepley   ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1422113c68e6SMatthew G. Knepley   minradius = PETSC_MAX_REAL;
1423113c68e6SMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
1424113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
1425113c68e6SMatthew G. Knepley     PetscReal        area;
14269ac3fadcSMatthew G. Knepley     PetscInt         ghost = -1, d;
1427113c68e6SMatthew G. Knepley 
14289ac3fadcSMatthew G. Knepley     if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);}
1429113c68e6SMatthew G. Knepley     if (ghost >= 0) continue;
1430113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr);
1431113c68e6SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr);
1432113c68e6SMatthew G. Knepley     for (d = 0; d < dim; ++d) fg->normal[d] *= area;
1433113c68e6SMatthew G. Knepley     /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */
1434113c68e6SMatthew G. Knepley     {
1435113c68e6SMatthew G. Knepley       PetscFVCellGeom *cL, *cR;
1436113c68e6SMatthew G. Knepley       const PetscInt  *cells;
1437113c68e6SMatthew G. Knepley       PetscReal       *lcentroid, *rcentroid;
14380453c0cdSMatthew G. Knepley       PetscReal        l[3], r[3], v[3];
1439113c68e6SMatthew G. Knepley 
1440113c68e6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr);
1441113c68e6SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr);
1442113c68e6SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr);
1443113c68e6SMatthew G. Knepley       lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid;
1444113c68e6SMatthew G. Knepley       rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid;
1445f170fd80SMatthew G. Knepley       ierr = DMPlexLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr);
1446f170fd80SMatthew G. Knepley       ierr = DMPlexLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr);
14470453c0cdSMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, l, r, v);
1448113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) {
1449113c68e6SMatthew G. Knepley         for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d];
1450113c68e6SMatthew G. Knepley       }
1451113c68e6SMatthew G. Knepley       if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) {
1452113c68e6SMatthew 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]);
1453113c68e6SMatthew 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]);
1454113c68e6SMatthew G. Knepley         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f);
1455113c68e6SMatthew G. Knepley       }
1456113c68e6SMatthew G. Knepley       if (cells[0] < cEndInterior) {
1457113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v);
1458113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
1459113c68e6SMatthew G. Knepley       }
1460113c68e6SMatthew G. Knepley       if (cells[1] < cEndInterior) {
1461113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v);
1462113c68e6SMatthew G. Knepley         minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v));
1463113c68e6SMatthew G. Knepley       }
1464113c68e6SMatthew G. Knepley     }
1465113c68e6SMatthew G. Knepley   }
1466a9b180a6SBarry Smith   ierr = MPI_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1467113c68e6SMatthew G. Knepley   ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr);
1468113c68e6SMatthew G. Knepley   /* Compute centroids of ghost cells */
1469113c68e6SMatthew G. Knepley   for (c = cEndInterior; c < cEnd; ++c) {
1470113c68e6SMatthew G. Knepley     PetscFVFaceGeom *fg;
1471113c68e6SMatthew G. Knepley     const PetscInt  *cone,    *support;
1472113c68e6SMatthew G. Knepley     PetscInt         coneSize, supportSize, s;
1473113c68e6SMatthew G. Knepley 
1474113c68e6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr);
1475113c68e6SMatthew G. Knepley     if (coneSize != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize);
1476113c68e6SMatthew G. Knepley     ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr);
1477113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr);
1478113c68e6SMatthew G. Knepley     if (supportSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 1", cone[0], supportSize);
1479113c68e6SMatthew G. Knepley     ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr);
1480113c68e6SMatthew G. Knepley     ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr);
1481113c68e6SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
1482113c68e6SMatthew G. Knepley       /* Reflect ghost centroid across plane of face */
1483113c68e6SMatthew G. Knepley       if (support[s] == c) {
1484113c68e6SMatthew G. Knepley         const PetscFVCellGeom *ci;
1485113c68e6SMatthew G. Knepley         PetscFVCellGeom       *cg;
1486113c68e6SMatthew G. Knepley         PetscReal              c2f[3], a;
1487113c68e6SMatthew G. Knepley 
1488113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr);
1489113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */
1490113c68e6SMatthew G. Knepley         a    = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal);
1491113c68e6SMatthew G. Knepley         ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr);
1492113c68e6SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid);
1493113c68e6SMatthew G. Knepley         cg->volume = ci->volume;
1494113c68e6SMatthew G. Knepley       }
1495113c68e6SMatthew G. Knepley     }
1496113c68e6SMatthew G. Knepley   }
1497113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr);
1498113c68e6SMatthew G. Knepley   ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr);
1499113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmCell);CHKERRQ(ierr);
1500113c68e6SMatthew G. Knepley   ierr = DMDestroy(&dmFace);CHKERRQ(ierr);
1501113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1502113c68e6SMatthew G. Knepley }
1503113c68e6SMatthew G. Knepley 
1504113c68e6SMatthew G. Knepley #undef __FUNCT__
1505113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexGetMinRadius"
1506113c68e6SMatthew G. Knepley /*@C
1507113c68e6SMatthew G. Knepley   DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face
1508113c68e6SMatthew G. Knepley 
1509113c68e6SMatthew G. Knepley   Not collective
1510113c68e6SMatthew G. Knepley 
1511113c68e6SMatthew G. Knepley   Input Argument:
1512113c68e6SMatthew G. Knepley . dm - the DM
1513113c68e6SMatthew G. Knepley 
1514113c68e6SMatthew G. Knepley   Output Argument:
1515113c68e6SMatthew G. Knepley . minradius - the minium cell radius
1516113c68e6SMatthew G. Knepley 
1517113c68e6SMatthew G. Knepley   Level: developer
1518113c68e6SMatthew G. Knepley 
1519113c68e6SMatthew G. Knepley .seealso: DMGetCoordinates()
1520113c68e6SMatthew G. Knepley @*/
1521113c68e6SMatthew G. Knepley PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius)
1522113c68e6SMatthew G. Knepley {
1523113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1524113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1525113c68e6SMatthew G. Knepley   PetscValidPointer(minradius,2);
1526113c68e6SMatthew G. Knepley   *minradius = ((DM_Plex*) dm->data)->minradius;
1527113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1528113c68e6SMatthew G. Knepley }
1529113c68e6SMatthew G. Knepley 
1530113c68e6SMatthew G. Knepley #undef __FUNCT__
1531113c68e6SMatthew G. Knepley #define __FUNCT__ "DMPlexSetMinRadius"
1532113c68e6SMatthew G. Knepley /*@C
1533113c68e6SMatthew G. Knepley   DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face
1534113c68e6SMatthew G. Knepley 
1535113c68e6SMatthew G. Knepley   Logically collective
1536113c68e6SMatthew G. Knepley 
1537113c68e6SMatthew G. Knepley   Input Arguments:
1538113c68e6SMatthew G. Knepley + dm - the DM
1539113c68e6SMatthew G. Knepley - minradius - the minium cell radius
1540113c68e6SMatthew G. Knepley 
1541113c68e6SMatthew G. Knepley   Level: developer
1542113c68e6SMatthew G. Knepley 
1543113c68e6SMatthew G. Knepley .seealso: DMSetCoordinates()
1544113c68e6SMatthew G. Knepley @*/
1545113c68e6SMatthew G. Knepley PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius)
1546113c68e6SMatthew G. Knepley {
1547113c68e6SMatthew G. Knepley   PetscFunctionBegin;
1548113c68e6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1549113c68e6SMatthew G. Knepley   ((DM_Plex*) dm->data)->minradius = minradius;
1550113c68e6SMatthew G. Knepley   PetscFunctionReturn(0);
1551113c68e6SMatthew G. Knepley }
1552856ac710SMatthew G. Knepley 
1553856ac710SMatthew G. Knepley #undef __FUNCT__
1554856ac710SMatthew G. Knepley #define __FUNCT__ "BuildGradientReconstruction_Internal"
1555856ac710SMatthew G. Knepley static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom)
1556856ac710SMatthew G. Knepley {
1557856ac710SMatthew G. Knepley   DMLabel        ghostLabel;
1558856ac710SMatthew G. Knepley   PetscScalar   *dx, *grad, **gref;
1559856ac710SMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c, cEndInterior, maxNumFaces;
1560856ac710SMatthew G. Knepley   PetscErrorCode ierr;
1561856ac710SMatthew G. Knepley 
1562856ac710SMatthew G. Knepley   PetscFunctionBegin;
1563856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1564856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1565856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1566856ac710SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr);
1567856ac710SMatthew G. Knepley   ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr);
1568856ac710SMatthew G. Knepley   ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1569856ac710SMatthew G. Knepley   ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr);
1570856ac710SMatthew G. Knepley   for (c = cStart; c < cEndInterior; c++) {
1571856ac710SMatthew G. Knepley     const PetscInt        *faces;
1572856ac710SMatthew G. Knepley     PetscInt               numFaces, usedFaces, f, d;
1573856ac710SMatthew G. Knepley     const PetscFVCellGeom *cg;
1574856ac710SMatthew G. Knepley     PetscBool              boundary;
1575856ac710SMatthew G. Knepley     PetscInt               ghost;
1576856ac710SMatthew G. Knepley 
1577856ac710SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr);
1578856ac710SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr);
1579856ac710SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr);
1580856ac710SMatthew 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);
1581856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
1582856ac710SMatthew G. Knepley       const PetscFVCellGeom *cg1;
1583856ac710SMatthew G. Knepley       PetscFVFaceGeom       *fg;
1584856ac710SMatthew G. Knepley       const PetscInt        *fcells;
1585856ac710SMatthew G. Knepley       PetscInt               ncell, side;
1586856ac710SMatthew G. Knepley 
1587856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
1588856ac710SMatthew G. Knepley       ierr = DMPlexIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
1589856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
1590856ac710SMatthew G. Knepley       ierr  = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr);
1591856ac710SMatthew G. Knepley       side  = (c != fcells[0]); /* c is on left=0 or right=1 of face */
1592856ac710SMatthew G. Knepley       ncell = fcells[!side];    /* the neighbor */
1593856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr);
1594856ac710SMatthew G. Knepley       ierr  = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr);
1595856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d];
1596856ac710SMatthew G. Knepley       gref[usedFaces++] = fg->grad[side];  /* Gradient reconstruction term will go here */
1597856ac710SMatthew G. Knepley     }
1598856ac710SMatthew G. Knepley     if (!usedFaces) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?");
1599856ac710SMatthew G. Knepley     ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr);
1600856ac710SMatthew G. Knepley     for (f = 0, usedFaces = 0; f < numFaces; ++f) {
1601856ac710SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr);
1602856ac710SMatthew G. Knepley       ierr = DMPlexIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr);
1603856ac710SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
1604856ac710SMatthew G. Knepley       for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d];
1605856ac710SMatthew G. Knepley       ++usedFaces;
1606856ac710SMatthew G. Knepley     }
1607856ac710SMatthew G. Knepley   }
1608856ac710SMatthew G. Knepley   ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr);
1609856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
1610856ac710SMatthew G. Knepley }
1611856ac710SMatthew G. Knepley 
1612856ac710SMatthew G. Knepley #undef __FUNCT__
1613856ac710SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeGradientFVM"
1614856ac710SMatthew G. Knepley /*@
1615856ac710SMatthew G. Knepley   DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data
1616856ac710SMatthew G. Knepley 
1617856ac710SMatthew G. Knepley   Collective on DM
1618856ac710SMatthew G. Knepley 
1619856ac710SMatthew G. Knepley   Input Arguments:
1620856ac710SMatthew G. Knepley + dm  - The DM
1621856ac710SMatthew G. Knepley . fvm - The PetscFV
1622856ac710SMatthew G. Knepley . faceGeometry - The face geometry from DMPlexGetFaceGeometryFVM()
1623856ac710SMatthew G. Knepley - cellGeometry - The face geometry from DMPlexGetCellGeometryFVM()
1624856ac710SMatthew G. Knepley 
1625856ac710SMatthew G. Knepley   Output Parameters:
1626856ac710SMatthew G. Knepley + faceGeometry - The geometric factors for gradient calculation are inserted
1627856ac710SMatthew G. Knepley - dmGrad - The DM describing the layout of gradient data
1628856ac710SMatthew G. Knepley 
1629856ac710SMatthew G. Knepley   Level: developer
1630856ac710SMatthew G. Knepley 
1631856ac710SMatthew G. Knepley .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM()
1632856ac710SMatthew G. Knepley @*/
1633856ac710SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad)
1634856ac710SMatthew G. Knepley {
1635856ac710SMatthew G. Knepley   DM             dmFace, dmCell;
1636856ac710SMatthew G. Knepley   PetscScalar   *fgeom, *cgeom;
1637856ac710SMatthew G. Knepley   PetscSection   sectionGrad;
1638856ac710SMatthew G. Knepley   PetscInt       dim, pdim, cStart, cEnd, cEndInterior, c;
1639856ac710SMatthew G. Knepley   PetscErrorCode ierr;
1640856ac710SMatthew G. Knepley 
1641856ac710SMatthew G. Knepley   PetscFunctionBegin;
1642856ac710SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1643856ac710SMatthew G. Knepley   ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr);
1644856ac710SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1645856ac710SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1646856ac710SMatthew G. Knepley   /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */
1647856ac710SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
1648856ac710SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
1649856ac710SMatthew G. Knepley   ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr);
1650856ac710SMatthew G. Knepley   ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr);
1651856ac710SMatthew G. Knepley   ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr);
1652856ac710SMatthew G. Knepley   ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr);
1653856ac710SMatthew G. Knepley   ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr);
1654856ac710SMatthew G. Knepley   /* Create storage for gradients */
1655856ac710SMatthew G. Knepley   ierr = DMClone(dm, dmGrad);CHKERRQ(ierr);
1656856ac710SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &sectionGrad);CHKERRQ(ierr);
1657856ac710SMatthew G. Knepley   ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr);
1658856ac710SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);}
1659856ac710SMatthew G. Knepley   ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr);
1660856ac710SMatthew G. Knepley   ierr = DMSetDefaultSection(*dmGrad, sectionGrad);CHKERRQ(ierr);
1661856ac710SMatthew G. Knepley   ierr = PetscSectionDestroy(&sectionGrad);CHKERRQ(ierr);
1662856ac710SMatthew G. Knepley   PetscFunctionReturn(0);
1663856ac710SMatthew G. Knepley }
1664