1 #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 2 3 #undef __FUNCT__ 4 #define __FUNCT__ "DMPlexLocatePoint_Simplex_2D_Internal" 5 static PetscErrorCode DMPlexLocatePoint_Simplex_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 6 { 7 const PetscInt embedDim = 2; 8 PetscReal x = PetscRealPart(point[0]); 9 PetscReal y = PetscRealPart(point[1]); 10 PetscReal v0[2], J[4], invJ[4], detJ; 11 PetscReal xi, eta; 12 PetscErrorCode ierr; 13 14 PetscFunctionBegin; 15 ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 16 xi = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]); 17 eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]); 18 19 if ((xi >= 0.0) && (eta >= 0.0) && (xi + eta <= 2.0)) *cell = c; 20 else *cell = -1; 21 PetscFunctionReturn(0); 22 } 23 24 #undef __FUNCT__ 25 #define __FUNCT__ "DMPlexLocatePoint_General_2D_Internal" 26 static PetscErrorCode DMPlexLocatePoint_General_2D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 27 { 28 PetscSection coordSection; 29 Vec coordsLocal; 30 PetscScalar *coords = NULL; 31 const PetscInt faces[8] = {0, 1, 1, 2, 2, 3, 3, 0}; 32 PetscReal x = PetscRealPart(point[0]); 33 PetscReal y = PetscRealPart(point[1]); 34 PetscInt crossings = 0, f; 35 PetscErrorCode ierr; 36 37 PetscFunctionBegin; 38 ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 39 ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 40 ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr); 41 for (f = 0; f < 4; ++f) { 42 PetscReal x_i = PetscRealPart(coords[faces[2*f+0]*2+0]); 43 PetscReal y_i = PetscRealPart(coords[faces[2*f+0]*2+1]); 44 PetscReal x_j = PetscRealPart(coords[faces[2*f+1]*2+0]); 45 PetscReal y_j = PetscRealPart(coords[faces[2*f+1]*2+1]); 46 PetscReal slope = (y_j - y_i) / (x_j - x_i); 47 PetscBool cond1 = (x_i <= x) && (x < x_j) ? PETSC_TRUE : PETSC_FALSE; 48 PetscBool cond2 = (x_j <= x) && (x < x_i) ? PETSC_TRUE : PETSC_FALSE; 49 PetscBool above = (y < slope * (x - x_i) + y_i) ? PETSC_TRUE : PETSC_FALSE; 50 if ((cond1 || cond2) && above) ++crossings; 51 } 52 if (crossings % 2) *cell = c; 53 else *cell = -1; 54 ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr); 55 PetscFunctionReturn(0); 56 } 57 58 #undef __FUNCT__ 59 #define __FUNCT__ "DMPlexLocatePoint_Simplex_3D_Internal" 60 static PetscErrorCode DMPlexLocatePoint_Simplex_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 61 { 62 const PetscInt embedDim = 3; 63 PetscReal v0[3], J[9], invJ[9], detJ; 64 PetscReal x = PetscRealPart(point[0]); 65 PetscReal y = PetscRealPart(point[1]); 66 PetscReal z = PetscRealPart(point[2]); 67 PetscReal xi, eta, zeta; 68 PetscErrorCode ierr; 69 70 PetscFunctionBegin; 71 ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 72 xi = invJ[0*embedDim+0]*(x - v0[0]) + invJ[0*embedDim+1]*(y - v0[1]) + invJ[0*embedDim+2]*(z - v0[2]); 73 eta = invJ[1*embedDim+0]*(x - v0[0]) + invJ[1*embedDim+1]*(y - v0[1]) + invJ[1*embedDim+2]*(z - v0[2]); 74 zeta = invJ[2*embedDim+0]*(x - v0[0]) + invJ[2*embedDim+1]*(y - v0[1]) + invJ[2*embedDim+2]*(z - v0[2]); 75 76 if ((xi >= 0.0) && (eta >= 0.0) && (zeta >= 0.0) && (xi + eta + zeta <= 2.0)) *cell = c; 77 else *cell = -1; 78 PetscFunctionReturn(0); 79 } 80 81 #undef __FUNCT__ 82 #define __FUNCT__ "DMPlexLocatePoint_General_3D_Internal" 83 static PetscErrorCode DMPlexLocatePoint_General_3D_Internal(DM dm, const PetscScalar point[], PetscInt c, PetscInt *cell) 84 { 85 PetscSection coordSection; 86 Vec coordsLocal; 87 PetscScalar *coords; 88 const PetscInt faces[24] = {0, 3, 2, 1, 5, 4, 7, 6, 3, 0, 4, 5, 89 1, 2, 6, 7, 3, 5, 6, 2, 0, 1, 7, 4}; 90 PetscBool found = PETSC_TRUE; 91 PetscInt f; 92 PetscErrorCode ierr; 93 94 PetscFunctionBegin; 95 ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 96 ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 97 ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr); 98 for (f = 0; f < 6; ++f) { 99 /* Check the point is under plane */ 100 /* Get face normal */ 101 PetscReal v_i[3]; 102 PetscReal v_j[3]; 103 PetscReal normal[3]; 104 PetscReal pp[3]; 105 PetscReal dot; 106 107 v_i[0] = PetscRealPart(coords[faces[f*4+3]*3+0]-coords[faces[f*4+0]*3+0]); 108 v_i[1] = PetscRealPart(coords[faces[f*4+3]*3+1]-coords[faces[f*4+0]*3+1]); 109 v_i[2] = PetscRealPart(coords[faces[f*4+3]*3+2]-coords[faces[f*4+0]*3+2]); 110 v_j[0] = PetscRealPart(coords[faces[f*4+1]*3+0]-coords[faces[f*4+0]*3+0]); 111 v_j[1] = PetscRealPart(coords[faces[f*4+1]*3+1]-coords[faces[f*4+0]*3+1]); 112 v_j[2] = PetscRealPart(coords[faces[f*4+1]*3+2]-coords[faces[f*4+0]*3+2]); 113 normal[0] = v_i[1]*v_j[2] - v_i[2]*v_j[1]; 114 normal[1] = v_i[2]*v_j[0] - v_i[0]*v_j[2]; 115 normal[2] = v_i[0]*v_j[1] - v_i[1]*v_j[0]; 116 pp[0] = PetscRealPart(coords[faces[f*4+0]*3+0] - point[0]); 117 pp[1] = PetscRealPart(coords[faces[f*4+0]*3+1] - point[1]); 118 pp[2] = PetscRealPart(coords[faces[f*4+0]*3+2] - point[2]); 119 dot = normal[0]*pp[0] + normal[1]*pp[1] + normal[2]*pp[2]; 120 121 /* Check that projected point is in face (2D location problem) */ 122 if (dot < 0.0) { 123 found = PETSC_FALSE; 124 break; 125 } 126 } 127 if (found) *cell = c; 128 else *cell = -1; 129 ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &coords);CHKERRQ(ierr); 130 PetscFunctionReturn(0); 131 } 132 133 #undef __FUNCT__ 134 #define __FUNCT__ "PetscGridHashInitialize_Internal" 135 static PetscErrorCode PetscGridHashInitialize_Internal(PetscGridHash box, PetscInt dim, const PetscScalar point[]) 136 { 137 PetscInt d; 138 139 PetscFunctionBegin; 140 box->dim = dim; 141 for (d = 0; d < dim; ++d) box->lower[d] = box->upper[d] = PetscRealPart(point[d]); 142 PetscFunctionReturn(0); 143 } 144 145 #undef __FUNCT__ 146 #define __FUNCT__ "PetscGridHashCreate" 147 PetscErrorCode PetscGridHashCreate(MPI_Comm comm, PetscInt dim, const PetscScalar point[], PetscGridHash *box) 148 { 149 PetscErrorCode ierr; 150 151 PetscFunctionBegin; 152 ierr = PetscMalloc1(1, box);CHKERRQ(ierr); 153 ierr = PetscGridHashInitialize_Internal(*box, dim, point);CHKERRQ(ierr); 154 PetscFunctionReturn(0); 155 } 156 157 #undef __FUNCT__ 158 #define __FUNCT__ "PetscGridHashEnlarge" 159 PetscErrorCode PetscGridHashEnlarge(PetscGridHash box, const PetscScalar point[]) 160 { 161 PetscInt d; 162 163 PetscFunctionBegin; 164 for (d = 0; d < box->dim; ++d) { 165 box->lower[d] = PetscMin(box->lower[d], PetscRealPart(point[d])); 166 box->upper[d] = PetscMax(box->upper[d], PetscRealPart(point[d])); 167 } 168 PetscFunctionReturn(0); 169 } 170 171 #undef __FUNCT__ 172 #define __FUNCT__ "PetscGridHashSetGrid" 173 PetscErrorCode PetscGridHashSetGrid(PetscGridHash box, const PetscInt n[], const PetscReal h[]) 174 { 175 PetscInt d; 176 177 PetscFunctionBegin; 178 for (d = 0; d < box->dim; ++d) { 179 box->extent[d] = box->upper[d] - box->lower[d]; 180 if (n[d] == PETSC_DETERMINE) { 181 box->h[d] = h[d]; 182 box->n[d] = PetscCeilReal(box->extent[d]/h[d]); 183 } else { 184 box->n[d] = n[d]; 185 box->h[d] = box->extent[d]/n[d]; 186 } 187 } 188 PetscFunctionReturn(0); 189 } 190 191 #undef __FUNCT__ 192 #define __FUNCT__ "PetscGridHashGetEnclosingBox" 193 PetscErrorCode PetscGridHashGetEnclosingBox(PetscGridHash box, PetscInt numPoints, const PetscScalar points[], PetscInt dboxes[], PetscInt boxes[]) 194 { 195 const PetscReal *lower = box->lower; 196 const PetscReal *upper = box->upper; 197 const PetscReal *h = box->h; 198 const PetscInt *n = box->n; 199 const PetscInt dim = box->dim; 200 PetscInt d, p; 201 202 PetscFunctionBegin; 203 for (p = 0; p < numPoints; ++p) { 204 for (d = 0; d < dim; ++d) { 205 PetscInt dbox = PetscFloorReal((PetscRealPart(points[p*dim+d]) - lower[d])/h[d]); 206 207 if (dbox == n[d] && PetscAbsReal(PetscRealPart(points[p*dim+d]) - upper[d]) < 1.0e-9) dbox = n[d]-1; 208 if (dbox < 0 || dbox >= n[d]) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input point %d (%g, %g, %g) is outside of our bounding box", 209 p, PetscRealPart(points[p*dim+0]), dim > 1 ? PetscRealPart(points[p*dim+1]) : 0.0, dim > 2 ? PetscRealPart(points[p*dim+2]) : 0.0); 210 dboxes[p*dim+d] = dbox; 211 } 212 if (boxes) for (d = 1, boxes[p] = dboxes[p*dim]; d < dim; ++d) boxes[p] += dboxes[p*dim+d]*n[d-1]; 213 } 214 PetscFunctionReturn(0); 215 } 216 217 #undef __FUNCT__ 218 #define __FUNCT__ "PetscGridHashDestroy" 219 PetscErrorCode PetscGridHashDestroy(PetscGridHash *box) 220 { 221 PetscErrorCode ierr; 222 223 PetscFunctionBegin; 224 if (*box) { 225 ierr = PetscSectionDestroy(&(*box)->cellSection);CHKERRQ(ierr); 226 ierr = ISDestroy(&(*box)->cells);CHKERRQ(ierr); 227 ierr = DMLabelDestroy(&(*box)->cellsSparse);CHKERRQ(ierr); 228 } 229 ierr = PetscFree(*box);CHKERRQ(ierr); 230 PetscFunctionReturn(0); 231 } 232 233 #undef __FUNCT__ 234 #define __FUNCT__ "DMPlexLocatePoint_Internal" 235 PetscErrorCode DMPlexLocatePoint_Internal(DM dm, PetscInt dim, const PetscScalar point[], PetscInt cellStart, PetscInt *cell) 236 { 237 PetscInt coneSize; 238 PetscErrorCode ierr; 239 240 PetscFunctionBegin; 241 switch (dim) { 242 case 2: 243 ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr); 244 switch (coneSize) { 245 case 3: 246 ierr = DMPlexLocatePoint_Simplex_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr); 247 break; 248 case 4: 249 ierr = DMPlexLocatePoint_General_2D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr); 250 break; 251 default: 252 SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize); 253 } 254 break; 255 case 3: 256 ierr = DMPlexGetConeSize(dm, cellStart, &coneSize);CHKERRQ(ierr); 257 switch (coneSize) { 258 case 4: 259 ierr = DMPlexLocatePoint_Simplex_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr); 260 break; 261 case 6: 262 ierr = DMPlexLocatePoint_General_3D_Internal(dm, point, cellStart, cell);CHKERRQ(ierr); 263 break; 264 default: 265 SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for cell with cone size %D", coneSize); 266 } 267 break; 268 default: 269 SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No point location for mesh dimension %D", dim); 270 } 271 PetscFunctionReturn(0); 272 } 273 274 #undef __FUNCT__ 275 #define __FUNCT__ "DMPlexComputeGridHash_Internal" 276 PetscErrorCode DMPlexComputeGridHash_Internal(DM dm, PetscGridHash *localBox) 277 { 278 MPI_Comm comm; 279 PetscGridHash lbox; 280 Vec coordinates; 281 PetscSection coordSection; 282 Vec coordsLocal; 283 const PetscScalar *coords; 284 PetscInt *dboxes; 285 PetscInt n[3] = {10, 10, 10}; 286 PetscInt dim, N, cStart, cEnd, c, i; 287 PetscErrorCode ierr; 288 289 PetscFunctionBegin; 290 ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr); 291 ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 292 ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr); 293 ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr); 294 ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr); 295 ierr = PetscGridHashCreate(comm, dim, coords, &lbox);CHKERRQ(ierr); 296 for (i = 0; i < N; i += dim) {ierr = PetscGridHashEnlarge(lbox, &coords[i]);CHKERRQ(ierr);} 297 ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr); 298 ierr = PetscGridHashSetGrid(lbox, n, NULL);CHKERRQ(ierr); 299 #if 0 300 /* Could define a custom reduction to merge these */ 301 ierr = MPI_Allreduce(lbox->lower, gbox->lower, 3, MPIU_REAL, MPI_MIN, comm);CHKERRQ(ierr); 302 ierr = MPI_Allreduce(lbox->upper, gbox->upper, 3, MPIU_REAL, MPI_MAX, comm);CHKERRQ(ierr); 303 #endif 304 /* Is there a reason to snap the local bounding box to a division of the global box? */ 305 /* Should we compute all overlaps of local boxes? We could do this with a rendevouz scheme partitioning the global box */ 306 /* Create label */ 307 ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 308 ierr = DMLabelCreate("cells", &lbox->cellsSparse);CHKERRQ(ierr); 309 ierr = DMLabelCreateIndex(lbox->cellsSparse, cStart, cEnd);CHKERRQ(ierr); 310 /* Compute boxes which overlap each cell */ 311 ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 312 ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 313 ierr = PetscCalloc1((dim+1) * dim, &dboxes);CHKERRQ(ierr); 314 for (c = cStart; c < cEnd; ++c) { 315 const PetscReal *h = lbox->h; 316 PetscScalar *ccoords = NULL; 317 PetscScalar point[3]; 318 PetscInt dlim[6], d, e, i, j, k; 319 320 /* Find boxes enclosing each vertex */ 321 ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords);CHKERRQ(ierr); 322 ierr = PetscGridHashGetEnclosingBox(lbox, dim+1, ccoords, dboxes, NULL);CHKERRQ(ierr); 323 ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &ccoords);CHKERRQ(ierr); 324 /* Get grid of boxes containing these */ 325 for (d = 0; d < dim; ++d) {dlim[d*2+0] = dlim[d*2+1] = dboxes[d];} 326 for (d = dim; d < 3; ++d) {dlim[d*2+0] = dlim[d*2+1] = 0;} 327 for (e = 1; e < dim+1; ++e) { 328 for (d = 0; d < dim; ++d) { 329 dlim[d*2+0] = PetscMin(dlim[d*2+0], dboxes[e*dim+d]); 330 dlim[d*2+1] = PetscMax(dlim[d*2+1], dboxes[e*dim+d]); 331 } 332 } 333 /* Check whether cell contains any vertex of these subboxes TODO vectorize this */ 334 for (k = dlim[2*2+0], point[2] = lbox->lower[2] + k*h[2]; k <= dlim[2*2+1]; ++k, point[2] += h[2]) { 335 for (j = dlim[1*2+0], point[1] = lbox->lower[1] + j*h[1]; j <= dlim[1*2+1]; ++j, point[1] += h[1]) { 336 for (i = dlim[0*2+0], point[0] = lbox->lower[0] + i*h[0]; i <= dlim[0*2+1]; ++i, point[0] += h[0]) { 337 const PetscInt box = (k*lbox->n[1] + j)*lbox->n[0] + i; 338 PetscScalar cpoint[3]; 339 PetscInt cell, ii, jj, kk; 340 341 for (kk = 0, cpoint[2] = point[2]; kk < (dim > 2 ? 2 : 1); ++kk, cpoint[2] += h[2]) { 342 for (jj = 0, cpoint[1] = point[1]; jj < (dim > 1 ? 2 : 1); ++jj, cpoint[1] += h[1]) { 343 for (ii = 0, cpoint[0] = point[0]; ii < 2; ++ii, cpoint[0] += h[0]) { 344 345 ierr = DMPlexLocatePoint_Internal(dm, dim, cpoint, c, &cell);CHKERRQ(ierr); 346 if (cell >= 0) {DMLabelSetValue(lbox->cellsSparse, c, box);CHKERRQ(ierr); ii = jj = kk = 2;} 347 } 348 } 349 } 350 } 351 } 352 } 353 } 354 ierr = PetscFree(dboxes);CHKERRQ(ierr); 355 ierr = DMLabelConvertToSection(lbox->cellsSparse, &lbox->cellSection, &lbox->cells);CHKERRQ(ierr); 356 ierr = DMLabelDestroy(&lbox->cellsSparse);CHKERRQ(ierr); 357 *localBox = lbox; 358 PetscFunctionReturn(0); 359 } 360 361 #undef __FUNCT__ 362 #define __FUNCT__ "DMLocatePoints_Plex" 363 /* 364 Need to implement using the guess 365 */ 366 PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, IS *cellIS) 367 { 368 DM_Plex *mesh = (DM_Plex *) dm->data; 369 PetscInt bs, numPoints, p; 370 PetscInt dim, cStart, cEnd, cMax, numCells, c; 371 const PetscInt *boxCells; 372 PetscInt *cells; 373 PetscScalar *a; 374 PetscErrorCode ierr; 375 376 PetscFunctionBegin; 377 if (!mesh->lbox) {ierr = DMPlexComputeGridHash_Internal(dm, &mesh->lbox);CHKERRQ(ierr);} 378 ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr); 379 ierr = VecGetBlockSize(v, &bs);CHKERRQ(ierr); 380 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); 381 ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 382 ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr); 383 if (cMax >= 0) cEnd = PetscMin(cEnd, cMax); 384 ierr = VecGetLocalSize(v, &numPoints);CHKERRQ(ierr); 385 ierr = VecGetArray(v, &a);CHKERRQ(ierr); 386 numPoints /= bs; 387 ierr = PetscMalloc1(numPoints, &cells);CHKERRQ(ierr); 388 /* Designate the local box for each point */ 389 /* Send points to correct process */ 390 /* Search cells that lie in each subbox */ 391 /* Should we bin points before doing search? */ 392 ierr = ISGetIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr); 393 for (p = 0; p < numPoints; ++p) { 394 const PetscScalar *point = &a[p*bs]; 395 PetscInt dbin[3], bin, cell, cellOffset; 396 397 ierr = PetscGridHashGetEnclosingBox(mesh->lbox, 1, point, dbin, &bin);CHKERRQ(ierr); 398 /* TODO Lay an interface over this so we can switch between Section (dense) and Label (sparse) */ 399 ierr = PetscSectionGetDof(mesh->lbox->cellSection, bin, &numCells);CHKERRQ(ierr); 400 ierr = PetscSectionGetOffset(mesh->lbox->cellSection, bin, &cellOffset);CHKERRQ(ierr); 401 for (c = cellOffset; c < cellOffset + numCells; ++c) { 402 ierr = DMPlexLocatePoint_Internal(dm, dim, point, boxCells[c], &cell);CHKERRQ(ierr); 403 if (cell >= 0) break; 404 } 405 if (cell < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Point %D not found in mesh", p); 406 cells[p] = cell; 407 } 408 ierr = ISRestoreIndices(mesh->lbox->cells, &boxCells);CHKERRQ(ierr); 409 /* Check for highest numbered proc that claims a point (do we care?) */ 410 ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 411 ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints, cells, PETSC_OWN_POINTER, cellIS);CHKERRQ(ierr); 412 PetscFunctionReturn(0); 413 } 414 415 #undef __FUNCT__ 416 #define __FUNCT__ "DMPlexComputeProjection2Dto1D_Internal" 417 /* 418 DMPlexComputeProjection2Dto1D_Internal - Rewrite coordinates to be the 1D projection of the 2D 419 */ 420 PetscErrorCode DMPlexComputeProjection2Dto1D_Internal(PetscScalar coords[], PetscReal R[]) 421 { 422 const PetscReal x = PetscRealPart(coords[2] - coords[0]); 423 const PetscReal y = PetscRealPart(coords[3] - coords[1]); 424 const PetscReal r = PetscSqrtReal(x*x + y*y), c = x/r, s = y/r; 425 426 PetscFunctionBegin; 427 R[0] = c; R[1] = -s; 428 R[2] = s; R[3] = c; 429 coords[0] = 0.0; 430 coords[1] = r; 431 PetscFunctionReturn(0); 432 } 433 434 #undef __FUNCT__ 435 #define __FUNCT__ "DMPlexComputeProjection3Dto1D_Internal" 436 /* 437 DMPlexComputeProjection3Dto1D_Internal - Rewrite coordinates to be the 1D projection of the 3D 438 439 This uses the basis completion described by Frisvad, 440 441 http://www.imm.dtu.dk/~jerf/papers/abstracts/onb.html 442 DOI:10.1080/2165347X.2012.689606 443 */ 444 PetscErrorCode DMPlexComputeProjection3Dto1D_Internal(PetscScalar coords[], PetscReal R[]) 445 { 446 PetscReal x = PetscRealPart(coords[3] - coords[0]); 447 PetscReal y = PetscRealPart(coords[4] - coords[1]); 448 PetscReal z = PetscRealPart(coords[5] - coords[2]); 449 PetscReal r = PetscSqrtReal(x*x + y*y + z*z); 450 PetscReal rinv = 1. / r; 451 PetscFunctionBegin; 452 453 x *= rinv; y *= rinv; z *= rinv; 454 if (x > 0.) { 455 PetscReal inv1pX = 1./ (1. + x); 456 457 R[0] = x; R[1] = -y; R[2] = -z; 458 R[3] = y; R[4] = 1. - y*y*inv1pX; R[5] = -y*z*inv1pX; 459 R[6] = z; R[7] = -y*z*inv1pX; R[8] = 1. - z*z*inv1pX; 460 } 461 else { 462 PetscReal inv1mX = 1./ (1. - x); 463 464 R[0] = x; R[1] = z; R[2] = y; 465 R[3] = y; R[4] = -y*z*inv1mX; R[5] = 1. - y*y*inv1mX; 466 R[6] = z; R[7] = 1. - z*z*inv1mX; R[8] = -y*z*inv1mX; 467 } 468 coords[0] = 0.0; 469 coords[1] = r; 470 PetscFunctionReturn(0); 471 } 472 473 #undef __FUNCT__ 474 #define __FUNCT__ "DMPlexComputeProjection3Dto2D_Internal" 475 /* 476 DMPlexComputeProjection3Dto2D_Internal - Rewrite coordinates to be the 2D projection of the 3D 477 */ 478 PetscErrorCode DMPlexComputeProjection3Dto2D_Internal(PetscInt coordSize, PetscScalar coords[], PetscReal R[]) 479 { 480 PetscReal x1[3], x2[3], n[3], norm; 481 PetscReal x1p[3], x2p[3], xnp[3]; 482 PetscReal sqrtz, alpha; 483 const PetscInt dim = 3; 484 PetscInt d, e, p; 485 486 PetscFunctionBegin; 487 /* 0) Calculate normal vector */ 488 for (d = 0; d < dim; ++d) { 489 x1[d] = PetscRealPart(coords[1*dim+d] - coords[0*dim+d]); 490 x2[d] = PetscRealPart(coords[2*dim+d] - coords[0*dim+d]); 491 } 492 n[0] = x1[1]*x2[2] - x1[2]*x2[1]; 493 n[1] = x1[2]*x2[0] - x1[0]*x2[2]; 494 n[2] = x1[0]*x2[1] - x1[1]*x2[0]; 495 norm = PetscSqrtReal(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]); 496 n[0] /= norm; 497 n[1] /= norm; 498 n[2] /= norm; 499 /* 1) Take the normal vector and rotate until it is \hat z 500 501 Let the normal vector be <nx, ny, nz> and alpha = 1/sqrt(1 - nz^2), then 502 503 R = / alpha nx nz alpha ny nz -1/alpha \ 504 | -alpha ny alpha nx 0 | 505 \ nx ny nz / 506 507 will rotate the normal vector to \hat z 508 */ 509 sqrtz = PetscSqrtReal(1.0 - n[2]*n[2]); 510 /* Check for n = z */ 511 if (sqrtz < 1.0e-10) { 512 if (n[2] < 0.0) { 513 if (coordSize > 9) { 514 coords[2] = PetscRealPart(coords[3*dim+0] - coords[0*dim+0]); 515 coords[3] = PetscRealPart(coords[3*dim+1] - coords[0*dim+1]); 516 coords[4] = x2[0]; 517 coords[5] = x2[1]; 518 coords[6] = x1[0]; 519 coords[7] = x1[1]; 520 } else { 521 coords[2] = x2[0]; 522 coords[3] = x2[1]; 523 coords[4] = x1[0]; 524 coords[5] = x1[1]; 525 } 526 R[0] = 1.0; R[1] = 0.0; R[2] = 0.0; 527 R[3] = 0.0; R[4] = 1.0; R[5] = 0.0; 528 R[6] = 0.0; R[7] = 0.0; R[8] = -1.0; 529 } else { 530 for (p = 3; p < coordSize/3; ++p) { 531 coords[p*2+0] = PetscRealPart(coords[p*dim+0] - coords[0*dim+0]); 532 coords[p*2+1] = PetscRealPart(coords[p*dim+1] - coords[0*dim+1]); 533 } 534 coords[2] = x1[0]; 535 coords[3] = x1[1]; 536 coords[4] = x2[0]; 537 coords[5] = x2[1]; 538 R[0] = 1.0; R[1] = 0.0; R[2] = 0.0; 539 R[3] = 0.0; R[4] = 1.0; R[5] = 0.0; 540 R[6] = 0.0; R[7] = 0.0; R[8] = 1.0; 541 } 542 coords[0] = 0.0; 543 coords[1] = 0.0; 544 PetscFunctionReturn(0); 545 } 546 alpha = 1.0/sqrtz; 547 R[0] = alpha*n[0]*n[2]; R[1] = alpha*n[1]*n[2]; R[2] = -sqrtz; 548 R[3] = -alpha*n[1]; R[4] = alpha*n[0]; R[5] = 0.0; 549 R[6] = n[0]; R[7] = n[1]; R[8] = n[2]; 550 for (d = 0; d < dim; ++d) { 551 x1p[d] = 0.0; 552 x2p[d] = 0.0; 553 for (e = 0; e < dim; ++e) { 554 x1p[d] += R[d*dim+e]*x1[e]; 555 x2p[d] += R[d*dim+e]*x2[e]; 556 } 557 } 558 if (PetscAbsReal(x1p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated"); 559 if (PetscAbsReal(x2p[2]) > 1.0e-9) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid rotation calculated"); 560 /* 2) Project to (x, y) */ 561 for (p = 3; p < coordSize/3; ++p) { 562 for (d = 0; d < dim; ++d) { 563 xnp[d] = 0.0; 564 for (e = 0; e < dim; ++e) { 565 xnp[d] += R[d*dim+e]*PetscRealPart(coords[p*dim+e] - coords[0*dim+e]); 566 } 567 if (d < dim-1) coords[p*2+d] = xnp[d]; 568 } 569 } 570 coords[0] = 0.0; 571 coords[1] = 0.0; 572 coords[2] = x1p[0]; 573 coords[3] = x1p[1]; 574 coords[4] = x2p[0]; 575 coords[5] = x2p[1]; 576 /* Output R^T which rotates \hat z to the input normal */ 577 for (d = 0; d < dim; ++d) { 578 for (e = d+1; e < dim; ++e) { 579 PetscReal tmp; 580 581 tmp = R[d*dim+e]; 582 R[d*dim+e] = R[e*dim+d]; 583 R[e*dim+d] = tmp; 584 } 585 } 586 PetscFunctionReturn(0); 587 } 588 589 #undef __FUNCT__ 590 #define __FUNCT__ "Volume_Triangle_Internal" 591 PETSC_UNUSED 592 PETSC_STATIC_INLINE void Volume_Triangle_Internal(PetscReal *vol, PetscReal coords[]) 593 { 594 /* Signed volume is 1/2 the determinant 595 596 | 1 1 1 | 597 | x0 x1 x2 | 598 | y0 y1 y2 | 599 600 but if x0,y0 is the origin, we have 601 602 | x1 x2 | 603 | y1 y2 | 604 */ 605 const PetscReal x1 = coords[2] - coords[0], y1 = coords[3] - coords[1]; 606 const PetscReal x2 = coords[4] - coords[0], y2 = coords[5] - coords[1]; 607 PetscReal M[4], detM; 608 M[0] = x1; M[1] = x2; 609 M[2] = y1; M[3] = y2; 610 DMPlex_Det2D_Internal(&detM, M); 611 *vol = 0.5*detM; 612 PetscLogFlops(5.0); 613 } 614 615 #undef __FUNCT__ 616 #define __FUNCT__ "Volume_Triangle_Origin_Internal" 617 PETSC_STATIC_INLINE void Volume_Triangle_Origin_Internal(PetscReal *vol, PetscReal coords[]) 618 { 619 DMPlex_Det2D_Internal(vol, coords); 620 *vol *= 0.5; 621 } 622 623 #undef __FUNCT__ 624 #define __FUNCT__ "Volume_Tetrahedron_Internal" 625 PETSC_UNUSED 626 PETSC_STATIC_INLINE void Volume_Tetrahedron_Internal(PetscReal *vol, PetscReal coords[]) 627 { 628 /* Signed volume is 1/6th of the determinant 629 630 | 1 1 1 1 | 631 | x0 x1 x2 x3 | 632 | y0 y1 y2 y3 | 633 | z0 z1 z2 z3 | 634 635 but if x0,y0,z0 is the origin, we have 636 637 | x1 x2 x3 | 638 | y1 y2 y3 | 639 | z1 z2 z3 | 640 */ 641 const PetscReal x1 = coords[3] - coords[0], y1 = coords[4] - coords[1], z1 = coords[5] - coords[2]; 642 const PetscReal x2 = coords[6] - coords[0], y2 = coords[7] - coords[1], z2 = coords[8] - coords[2]; 643 const PetscReal x3 = coords[9] - coords[0], y3 = coords[10] - coords[1], z3 = coords[11] - coords[2]; 644 PetscReal M[9], detM; 645 M[0] = x1; M[1] = x2; M[2] = x3; 646 M[3] = y1; M[4] = y2; M[5] = y3; 647 M[6] = z1; M[7] = z2; M[8] = z3; 648 DMPlex_Det3D_Internal(&detM, M); 649 *vol = -0.16666666666666666666666*detM; 650 PetscLogFlops(10.0); 651 } 652 653 #undef __FUNCT__ 654 #define __FUNCT__ "Volume_Tetrahedron_Origin_Internal" 655 PETSC_STATIC_INLINE void Volume_Tetrahedron_Origin_Internal(PetscReal *vol, PetscReal coords[]) 656 { 657 DMPlex_Det3D_Internal(vol, coords); 658 *vol *= -0.16666666666666666666666; 659 } 660 661 #undef __FUNCT__ 662 #define __FUNCT__ "DMPlexComputeLineGeometry_Internal" 663 static PetscErrorCode DMPlexComputeLineGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 664 { 665 PetscSection coordSection; 666 Vec coordinates; 667 PetscScalar *coords = NULL; 668 PetscInt numCoords, d; 669 PetscErrorCode ierr; 670 671 PetscFunctionBegin; 672 ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 673 ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 674 ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 675 *detJ = 0.0; 676 if (numCoords == 6) { 677 const PetscInt dim = 3; 678 PetscReal R[9], J0; 679 680 if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 681 ierr = DMPlexComputeProjection3Dto1D_Internal(coords, R);CHKERRQ(ierr); 682 if (J) { 683 J0 = 0.5*PetscRealPart(coords[1]); 684 J[0] = R[0]*J0; J[1] = R[1]; J[2] = R[2]; 685 J[3] = R[3]*J0; J[4] = R[4]; J[5] = R[5]; 686 J[6] = R[6]*J0; J[7] = R[7]; J[8] = R[8]; 687 DMPlex_Det3D_Internal(detJ, J); 688 } 689 if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 690 } else if (numCoords == 4) { 691 const PetscInt dim = 2; 692 PetscReal R[4], J0; 693 694 if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 695 ierr = DMPlexComputeProjection2Dto1D_Internal(coords, R);CHKERRQ(ierr); 696 if (J) { 697 J0 = 0.5*PetscRealPart(coords[1]); 698 J[0] = R[0]*J0; J[1] = R[1]; 699 J[2] = R[2]*J0; J[3] = R[3]; 700 DMPlex_Det2D_Internal(detJ, J); 701 } 702 if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 703 } else if (numCoords == 2) { 704 const PetscInt dim = 1; 705 706 if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 707 if (J) { 708 J[0] = 0.5*(PetscRealPart(coords[1]) - PetscRealPart(coords[0])); 709 *detJ = J[0]; 710 PetscLogFlops(2.0); 711 } 712 if (invJ) {invJ[0] = 1.0/J[0]; PetscLogFlops(1.0);} 713 } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this segment is %D != 2", numCoords); 714 ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 715 PetscFunctionReturn(0); 716 } 717 718 #undef __FUNCT__ 719 #define __FUNCT__ "DMPlexComputeTriangleGeometry_Internal" 720 static PetscErrorCode DMPlexComputeTriangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 721 { 722 PetscSection coordSection; 723 Vec coordinates; 724 PetscScalar *coords = NULL; 725 PetscInt numCoords, d, f, g; 726 PetscErrorCode ierr; 727 728 PetscFunctionBegin; 729 ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 730 ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 731 ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 732 *detJ = 0.0; 733 if (numCoords == 9) { 734 const PetscInt dim = 3; 735 PetscReal R[9], J0[9] = {1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0}; 736 737 if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 738 ierr = DMPlexComputeProjection3Dto2D_Internal(numCoords, coords, R);CHKERRQ(ierr); 739 if (J) { 740 const PetscInt pdim = 2; 741 742 for (d = 0; d < pdim; d++) { 743 for (f = 0; f < pdim; f++) { 744 J0[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*pdim+d]) - PetscRealPart(coords[0*pdim+d])); 745 } 746 } 747 PetscLogFlops(8.0); 748 DMPlex_Det3D_Internal(detJ, J0); 749 for (d = 0; d < dim; d++) { 750 for (f = 0; f < dim; f++) { 751 J[d*dim+f] = 0.0; 752 for (g = 0; g < dim; g++) { 753 J[d*dim+f] += R[d*dim+g]*J0[g*dim+f]; 754 } 755 } 756 } 757 PetscLogFlops(18.0); 758 } 759 if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 760 } else if (numCoords == 6) { 761 const PetscInt dim = 2; 762 763 if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 764 if (J) { 765 for (d = 0; d < dim; d++) { 766 for (f = 0; f < dim; f++) { 767 J[d*dim+f] = 0.5*(PetscRealPart(coords[(f+1)*dim+d]) - PetscRealPart(coords[0*dim+d])); 768 } 769 } 770 PetscLogFlops(8.0); 771 DMPlex_Det2D_Internal(detJ, J); 772 } 773 if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 774 } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this triangle is %D != 6 or 9", numCoords); 775 ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 776 PetscFunctionReturn(0); 777 } 778 779 #undef __FUNCT__ 780 #define __FUNCT__ "DMPlexComputeRectangleGeometry_Internal" 781 static PetscErrorCode DMPlexComputeRectangleGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 782 { 783 PetscSection coordSection; 784 Vec coordinates; 785 PetscScalar *coords = NULL; 786 PetscInt numCoords, d, f, g; 787 PetscErrorCode ierr; 788 789 PetscFunctionBegin; 790 ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 791 ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 792 ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 793 *detJ = 0.0; 794 if (numCoords == 12) { 795 const PetscInt dim = 3; 796 PetscReal R[9], J0[9] = {1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0}; 797 798 if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 799 ierr = DMPlexComputeProjection3Dto2D_Internal(numCoords, coords, R);CHKERRQ(ierr); 800 if (J) { 801 const PetscInt pdim = 2; 802 803 for (d = 0; d < pdim; d++) { 804 J0[d*dim+0] = 0.5*(PetscRealPart(coords[1*pdim+d]) - PetscRealPart(coords[0*pdim+d])); 805 J0[d*dim+1] = 0.5*(PetscRealPart(coords[3*pdim+d]) - PetscRealPart(coords[0*pdim+d])); 806 } 807 PetscLogFlops(8.0); 808 DMPlex_Det3D_Internal(detJ, J0); 809 for (d = 0; d < dim; d++) { 810 for (f = 0; f < dim; f++) { 811 J[d*dim+f] = 0.0; 812 for (g = 0; g < dim; g++) { 813 J[d*dim+f] += R[d*dim+g]*J0[g*dim+f]; 814 } 815 } 816 } 817 PetscLogFlops(18.0); 818 } 819 if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 820 } else if ((numCoords == 8) || (numCoords == 16)) { 821 const PetscInt dim = 2; 822 823 if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 824 if (J) { 825 for (d = 0; d < dim; d++) { 826 J[d*dim+0] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d])); 827 J[d*dim+1] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d])); 828 } 829 PetscLogFlops(8.0); 830 DMPlex_Det2D_Internal(detJ, J); 831 } 832 if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 833 } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of coordinates for this quadrilateral is %D != 8 or 12", numCoords); 834 ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, &numCoords, &coords);CHKERRQ(ierr); 835 PetscFunctionReturn(0); 836 } 837 838 #undef __FUNCT__ 839 #define __FUNCT__ "DMPlexComputeTetrahedronGeometry_Internal" 840 static PetscErrorCode DMPlexComputeTetrahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 841 { 842 PetscSection coordSection; 843 Vec coordinates; 844 PetscScalar *coords = NULL; 845 const PetscInt dim = 3; 846 PetscInt d; 847 PetscErrorCode ierr; 848 849 PetscFunctionBegin; 850 ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 851 ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 852 ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr); 853 *detJ = 0.0; 854 if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 855 if (J) { 856 for (d = 0; d < dim; d++) { 857 /* I orient with outward face normals */ 858 J[d*dim+0] = 0.5*(PetscRealPart(coords[2*dim+d]) - PetscRealPart(coords[0*dim+d])); 859 J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d])); 860 J[d*dim+2] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d])); 861 } 862 PetscLogFlops(18.0); 863 DMPlex_Det3D_Internal(detJ, J); 864 } 865 if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 866 ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr); 867 PetscFunctionReturn(0); 868 } 869 870 #undef __FUNCT__ 871 #define __FUNCT__ "DMPlexComputeHexahedronGeometry_Internal" 872 static PetscErrorCode DMPlexComputeHexahedronGeometry_Internal(DM dm, PetscInt e, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 873 { 874 PetscSection coordSection; 875 Vec coordinates; 876 PetscScalar *coords = NULL; 877 const PetscInt dim = 3; 878 PetscInt d; 879 PetscErrorCode ierr; 880 881 PetscFunctionBegin; 882 ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 883 ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 884 ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr); 885 *detJ = 0.0; 886 if (v0) {for (d = 0; d < dim; d++) v0[d] = PetscRealPart(coords[d]);} 887 if (J) { 888 for (d = 0; d < dim; d++) { 889 J[d*dim+0] = 0.5*(PetscRealPart(coords[3*dim+d]) - PetscRealPart(coords[0*dim+d])); 890 J[d*dim+1] = 0.5*(PetscRealPart(coords[1*dim+d]) - PetscRealPart(coords[0*dim+d])); 891 J[d*dim+2] = 0.5*(PetscRealPart(coords[4*dim+d]) - PetscRealPart(coords[0*dim+d])); 892 } 893 PetscLogFlops(18.0); 894 DMPlex_Det3D_Internal(detJ, J); 895 } 896 if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 897 ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, e, NULL, &coords);CHKERRQ(ierr); 898 PetscFunctionReturn(0); 899 } 900 901 #undef __FUNCT__ 902 #define __FUNCT__ "DMPlexComputeCellGeometryAffineFEM" 903 /*@C 904 DMPlexComputeCellGeometryAffineFEM - Assuming an affine map, compute the Jacobian, inverse Jacobian, and Jacobian determinant for a given cell 905 906 Collective on DM 907 908 Input Arguments: 909 + dm - the DM 910 - cell - the cell 911 912 Output Arguments: 913 + v0 - the translation part of this affine transform 914 . J - the Jacobian of the transform from the reference element 915 . invJ - the inverse of the Jacobian 916 - detJ - the Jacobian determinant 917 918 Level: advanced 919 920 Fortran Notes: 921 Since it returns arrays, this routine is only available in Fortran 90, and you must 922 include petsc.h90 in your code. 923 924 .seealso: DMPlexComputeCellGeometryFEM(), DMGetCoordinateSection(), DMGetCoordinateVec() 925 @*/ 926 PetscErrorCode DMPlexComputeCellGeometryAffineFEM(DM dm, PetscInt cell, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ) 927 { 928 PetscInt depth, dim, coneSize; 929 PetscErrorCode ierr; 930 931 PetscFunctionBegin; 932 ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 933 ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr); 934 if (depth == 1) { 935 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 936 } else { 937 DMLabel depth; 938 939 ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 940 ierr = DMLabelGetValue(depth, cell, &dim);CHKERRQ(ierr); 941 } 942 switch (dim) { 943 case 1: 944 ierr = DMPlexComputeLineGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 945 break; 946 case 2: 947 switch (coneSize) { 948 case 3: 949 ierr = DMPlexComputeTriangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 950 break; 951 case 4: 952 ierr = DMPlexComputeRectangleGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 953 break; 954 default: 955 SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell); 956 } 957 break; 958 case 3: 959 switch (coneSize) { 960 case 4: 961 ierr = DMPlexComputeTetrahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 962 break; 963 case 6: /* Faces */ 964 case 8: /* Vertices */ 965 ierr = DMPlexComputeHexahedronGeometry_Internal(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr); 966 break; 967 default: 968 SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported number of faces %D in cell %D for element geometry computation", coneSize, cell); 969 } 970 break; 971 default: 972 SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim); 973 } 974 PetscFunctionReturn(0); 975 } 976 977 #undef __FUNCT__ 978 #define __FUNCT__ "DMPlexComputeIsoparametricGeometry_Internal" 979 static PetscErrorCode DMPlexComputeIsoparametricGeometry_Internal(DM dm, PetscFE fe, PetscInt point, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal *detJ) 980 { 981 PetscQuadrature quad; 982 PetscSection coordSection; 983 Vec coordinates; 984 PetscScalar *coords = NULL; 985 const PetscReal *quadPoints; 986 PetscReal *basisDer; 987 PetscInt dim, cdim, pdim, qdim, Nq, numCoords, d, q; 988 PetscErrorCode ierr; 989 990 PetscFunctionBegin; 991 ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 992 ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 993 ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr); 994 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 995 ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr); 996 ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 997 ierr = PetscFEGetDimension(fe, &pdim);CHKERRQ(ierr); 998 ierr = PetscQuadratureGetData(quad, &qdim, &Nq, &quadPoints, NULL);CHKERRQ(ierr); 999 ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr); 1000 *detJ = 0.0; 1001 if (qdim != dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Point dimension %d != quadrature dimension %d", dim, qdim); 1002 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); 1003 if (v0) {for (d = 0; d < cdim; d++) v0[d] = PetscRealPart(coords[d]);} 1004 if (J) { 1005 for (q = 0; q < Nq; ++q) { 1006 PetscInt i, j, k, c, r; 1007 1008 /* J = dx_i/d\xi_j = sum[k=0,n-1] dN_k/d\xi_j * x_i(k) */ 1009 for (k = 0; k < pdim; ++k) 1010 for (j = 0; j < dim; ++j) 1011 for (i = 0; i < cdim; ++i) 1012 J[(q*cdim + i)*dim + j] += basisDer[(q*pdim + k)*dim + j] * PetscRealPart(coords[k*cdim + i]); 1013 PetscLogFlops(2.0*pdim*dim*cdim); 1014 if (cdim > dim) { 1015 for (c = dim; c < cdim; ++c) 1016 for (r = 0; r < cdim; ++r) 1017 J[r*cdim+c] = r == c ? 1.0 : 0.0; 1018 } 1019 switch (cdim) { 1020 case 3: 1021 DMPlex_Det3D_Internal(detJ, J); 1022 if (invJ) {DMPlex_Invert3D_Internal(invJ, J, *detJ);} 1023 break; 1024 case 2: 1025 DMPlex_Det2D_Internal(detJ, J); 1026 if (invJ) {DMPlex_Invert2D_Internal(invJ, J, *detJ);} 1027 break; 1028 case 1: 1029 *detJ = J[0]; 1030 if (invJ) invJ[0] = 1.0/J[0]; 1031 } 1032 } 1033 } 1034 ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, point, &numCoords, &coords);CHKERRQ(ierr); 1035 PetscFunctionReturn(0); 1036 } 1037 1038 #undef __FUNCT__ 1039 #define __FUNCT__ "DMPlexComputeCellGeometryFEM" 1040 /*@C 1041 DMPlexComputeCellGeometryFEM - Compute the Jacobian, inverse Jacobian, and Jacobian determinant at each quadrature point in the given cell 1042 1043 Collective on DM 1044 1045 Input Arguments: 1046 + dm - the DM 1047 . cell - the cell 1048 - fe - the finite element containing the quadrature 1049 1050 Output Arguments: 1051 + v0 - the translation part of this transform 1052 . J - the Jacobian of the transform from the reference element at each quadrature point 1053 . invJ - the inverse of the Jacobian at each quadrature point 1054 - detJ - the Jacobian determinant at each quadrature point 1055 1056 Level: advanced 1057 1058 Fortran Notes: 1059 Since it returns arrays, this routine is only available in Fortran 90, and you must 1060 include petsc.h90 in your code. 1061 1062 .seealso: DMGetCoordinateSection(), DMGetCoordinateVec() 1063 @*/ 1064 PetscErrorCode DMPlexComputeCellGeometryFEM(DM dm, PetscInt cell, PetscFE fe, PetscReal *v0, PetscReal *J, PetscReal *invJ, PetscReal *detJ) 1065 { 1066 PetscErrorCode ierr; 1067 1068 PetscFunctionBegin; 1069 if (!fe) {ierr = DMPlexComputeCellGeometryAffineFEM(dm, cell, v0, J, invJ, detJ);CHKERRQ(ierr);} 1070 else {ierr = DMPlexComputeIsoparametricGeometry_Internal(dm, fe, cell, v0, J, invJ, detJ);CHKERRQ(ierr);} 1071 PetscFunctionReturn(0); 1072 } 1073 1074 #undef __FUNCT__ 1075 #define __FUNCT__ "DMPlexComputeGeometryFVM_1D_Internal" 1076 static PetscErrorCode DMPlexComputeGeometryFVM_1D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 1077 { 1078 PetscSection coordSection; 1079 Vec coordinates; 1080 PetscScalar *coords = NULL; 1081 PetscScalar tmp[2]; 1082 PetscInt coordSize; 1083 PetscErrorCode ierr; 1084 1085 PetscFunctionBegin; 1086 ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 1087 ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1088 ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr); 1089 if (dim != 2) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "We only support 2D edges right now"); 1090 ierr = DMPlexLocalizeCoordinate_Internal(dm, dim, coords, &coords[dim], tmp);CHKERRQ(ierr); 1091 if (centroid) { 1092 centroid[0] = 0.5*PetscRealPart(coords[0] + tmp[0]); 1093 centroid[1] = 0.5*PetscRealPart(coords[1] + tmp[1]); 1094 } 1095 if (normal) { 1096 PetscReal norm; 1097 1098 normal[0] = -PetscRealPart(coords[1] - tmp[1]); 1099 normal[1] = PetscRealPart(coords[0] - tmp[0]); 1100 norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1]); 1101 normal[0] /= norm; 1102 normal[1] /= norm; 1103 } 1104 if (vol) { 1105 *vol = PetscSqrtReal(PetscSqr(PetscRealPart(coords[0] - tmp[0])) + PetscSqr(PetscRealPart(coords[1] - tmp[1]))); 1106 } 1107 ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr); 1108 PetscFunctionReturn(0); 1109 } 1110 1111 #undef __FUNCT__ 1112 #define __FUNCT__ "DMPlexComputeGeometryFVM_2D_Internal" 1113 /* Centroid_i = (\sum_n A_n Cn_i ) / A */ 1114 static PetscErrorCode DMPlexComputeGeometryFVM_2D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 1115 { 1116 PetscSection coordSection; 1117 Vec coordinates; 1118 PetscScalar *coords = NULL; 1119 PetscReal vsum = 0.0, csum[3] = {0.0, 0.0, 0.0}, vtmp, ctmp[4], v0[3], R[9]; 1120 PetscInt tdim = 2, coordSize, numCorners, p, d, e; 1121 PetscErrorCode ierr; 1122 1123 PetscFunctionBegin; 1124 ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 1125 ierr = DMPlexGetConeSize(dm, cell, &numCorners);CHKERRQ(ierr); 1126 ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1127 ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr); 1128 ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr); 1129 if (normal) { 1130 if (dim > 2) { 1131 const PetscReal x0 = PetscRealPart(coords[dim+0] - coords[0]), x1 = PetscRealPart(coords[dim*2+0] - coords[0]); 1132 const PetscReal y0 = PetscRealPart(coords[dim+1] - coords[1]), y1 = PetscRealPart(coords[dim*2+1] - coords[1]); 1133 const PetscReal z0 = PetscRealPart(coords[dim+2] - coords[2]), z1 = PetscRealPart(coords[dim*2+2] - coords[2]); 1134 PetscReal norm; 1135 1136 v0[0] = PetscRealPart(coords[0]); 1137 v0[1] = PetscRealPart(coords[1]); 1138 v0[2] = PetscRealPart(coords[2]); 1139 normal[0] = y0*z1 - z0*y1; 1140 normal[1] = z0*x1 - x0*z1; 1141 normal[2] = x0*y1 - y0*x1; 1142 norm = PetscSqrtReal(normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2]); 1143 normal[0] /= norm; 1144 normal[1] /= norm; 1145 normal[2] /= norm; 1146 } else { 1147 for (d = 0; d < dim; ++d) normal[d] = 0.0; 1148 } 1149 } 1150 if (dim == 3) {ierr = DMPlexComputeProjection3Dto2D_Internal(coordSize, coords, R);CHKERRQ(ierr);} 1151 for (p = 0; p < numCorners; ++p) { 1152 /* Need to do this copy to get types right */ 1153 for (d = 0; d < tdim; ++d) { 1154 ctmp[d] = PetscRealPart(coords[p*tdim+d]); 1155 ctmp[tdim+d] = PetscRealPart(coords[((p+1)%numCorners)*tdim+d]); 1156 } 1157 Volume_Triangle_Origin_Internal(&vtmp, ctmp); 1158 vsum += vtmp; 1159 for (d = 0; d < tdim; ++d) { 1160 csum[d] += (ctmp[d] + ctmp[tdim+d])*vtmp; 1161 } 1162 } 1163 for (d = 0; d < tdim; ++d) { 1164 csum[d] /= (tdim+1)*vsum; 1165 } 1166 ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, cell, &coordSize, &coords);CHKERRQ(ierr); 1167 if (vol) *vol = PetscAbsReal(vsum); 1168 if (centroid) { 1169 if (dim > 2) { 1170 for (d = 0; d < dim; ++d) { 1171 centroid[d] = v0[d]; 1172 for (e = 0; e < dim; ++e) { 1173 centroid[d] += R[d*dim+e]*csum[e]; 1174 } 1175 } 1176 } else for (d = 0; d < dim; ++d) centroid[d] = csum[d]; 1177 } 1178 PetscFunctionReturn(0); 1179 } 1180 1181 #undef __FUNCT__ 1182 #define __FUNCT__ "DMPlexComputeGeometryFVM_3D_Internal" 1183 /* Centroid_i = (\sum_n V_n Cn_i ) / V */ 1184 static PetscErrorCode DMPlexComputeGeometryFVM_3D_Internal(DM dm, PetscInt dim, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 1185 { 1186 PetscSection coordSection; 1187 Vec coordinates; 1188 PetscScalar *coords = NULL; 1189 PetscReal vsum = 0.0, vtmp, coordsTmp[3*3]; 1190 const PetscInt *faces, *facesO; 1191 PetscInt numFaces, f, coordSize, numCorners, p, d; 1192 PetscErrorCode ierr; 1193 1194 PetscFunctionBegin; 1195 if (PetscUnlikely(dim > 3)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No support for dim %D > 3",dim); 1196 ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 1197 ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1198 1199 if (centroid) for (d = 0; d < dim; ++d) centroid[d] = 0.0; 1200 ierr = DMPlexGetConeSize(dm, cell, &numFaces);CHKERRQ(ierr); 1201 ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr); 1202 ierr = DMPlexGetConeOrientation(dm, cell, &facesO);CHKERRQ(ierr); 1203 for (f = 0; f < numFaces; ++f) { 1204 ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr); 1205 numCorners = coordSize/dim; 1206 switch (numCorners) { 1207 case 3: 1208 for (d = 0; d < dim; ++d) { 1209 coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]); 1210 coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]); 1211 coordsTmp[2*dim+d] = PetscRealPart(coords[2*dim+d]); 1212 } 1213 Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 1214 if (facesO[f] < 0) vtmp = -vtmp; 1215 vsum += vtmp; 1216 if (centroid) { /* Centroid of OABC = (a+b+c)/4 */ 1217 for (d = 0; d < dim; ++d) { 1218 for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp; 1219 } 1220 } 1221 break; 1222 case 4: 1223 /* DO FOR PYRAMID */ 1224 /* First tet */ 1225 for (d = 0; d < dim; ++d) { 1226 coordsTmp[0*dim+d] = PetscRealPart(coords[0*dim+d]); 1227 coordsTmp[1*dim+d] = PetscRealPart(coords[1*dim+d]); 1228 coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]); 1229 } 1230 Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 1231 if (facesO[f] < 0) vtmp = -vtmp; 1232 vsum += vtmp; 1233 if (centroid) { 1234 for (d = 0; d < dim; ++d) { 1235 for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp; 1236 } 1237 } 1238 /* Second tet */ 1239 for (d = 0; d < dim; ++d) { 1240 coordsTmp[0*dim+d] = PetscRealPart(coords[1*dim+d]); 1241 coordsTmp[1*dim+d] = PetscRealPart(coords[2*dim+d]); 1242 coordsTmp[2*dim+d] = PetscRealPart(coords[3*dim+d]); 1243 } 1244 Volume_Tetrahedron_Origin_Internal(&vtmp, coordsTmp); 1245 if (facesO[f] < 0) vtmp = -vtmp; 1246 vsum += vtmp; 1247 if (centroid) { 1248 for (d = 0; d < dim; ++d) { 1249 for (p = 0; p < 3; ++p) centroid[d] += coordsTmp[p*dim+d]*vtmp; 1250 } 1251 } 1252 break; 1253 default: 1254 SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle faces with %D vertices", numCorners); 1255 } 1256 ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, faces[f], &coordSize, &coords);CHKERRQ(ierr); 1257 } 1258 if (vol) *vol = PetscAbsReal(vsum); 1259 if (normal) for (d = 0; d < dim; ++d) normal[d] = 0.0; 1260 if (centroid) for (d = 0; d < dim; ++d) centroid[d] /= (vsum*4); 1261 PetscFunctionReturn(0); 1262 } 1263 1264 #undef __FUNCT__ 1265 #define __FUNCT__ "DMPlexComputeCellGeometryFVM" 1266 /*@C 1267 DMPlexComputeCellGeometryFVM - Compute the volume for a given cell 1268 1269 Collective on DM 1270 1271 Input Arguments: 1272 + dm - the DM 1273 - cell - the cell 1274 1275 Output Arguments: 1276 + volume - the cell volume 1277 . centroid - the cell centroid 1278 - normal - the cell normal, if appropriate 1279 1280 Level: advanced 1281 1282 Fortran Notes: 1283 Since it returns arrays, this routine is only available in Fortran 90, and you must 1284 include petsc.h90 in your code. 1285 1286 .seealso: DMGetCoordinateSection(), DMGetCoordinateVec() 1287 @*/ 1288 PetscErrorCode DMPlexComputeCellGeometryFVM(DM dm, PetscInt cell, PetscReal *vol, PetscReal centroid[], PetscReal normal[]) 1289 { 1290 PetscInt depth, dim; 1291 PetscErrorCode ierr; 1292 1293 PetscFunctionBegin; 1294 ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 1295 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1296 if (depth != dim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh must be interpolated"); 1297 /* We need to keep a pointer to the depth label */ 1298 ierr = DMPlexGetLabelValue(dm, "depth", cell, &depth);CHKERRQ(ierr); 1299 /* Cone size is now the number of faces */ 1300 switch (depth) { 1301 case 1: 1302 ierr = DMPlexComputeGeometryFVM_1D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr); 1303 break; 1304 case 2: 1305 ierr = DMPlexComputeGeometryFVM_2D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr); 1306 break; 1307 case 3: 1308 ierr = DMPlexComputeGeometryFVM_3D_Internal(dm, dim, cell, vol, centroid, normal);CHKERRQ(ierr); 1309 break; 1310 default: 1311 SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension %D for element geometry computation", dim); 1312 } 1313 PetscFunctionReturn(0); 1314 } 1315 1316 #undef __FUNCT__ 1317 #define __FUNCT__ "DMPlexComputeGeometryFEM" 1318 /* This should also take a PetscFE argument I think */ 1319 PetscErrorCode DMPlexComputeGeometryFEM(DM dm, Vec *cellgeom) 1320 { 1321 DM dmCell; 1322 Vec coordinates; 1323 PetscSection coordSection, sectionCell; 1324 PetscScalar *cgeom; 1325 PetscInt cStart, cEnd, cMax, c; 1326 PetscErrorCode ierr; 1327 1328 PetscFunctionBegin; 1329 ierr = DMClone(dm, &dmCell);CHKERRQ(ierr); 1330 ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1331 ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 1332 ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr); 1333 ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr); 1334 ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionCell);CHKERRQ(ierr); 1335 ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1336 ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr); 1337 cEnd = cMax < 0 ? cEnd : cMax; 1338 ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr); 1339 /* TODO This needs to be multiplied by Nq for non-affine */ 1340 for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFECellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);} 1341 ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr); 1342 ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr); 1343 ierr = PetscSectionDestroy(§ionCell);CHKERRQ(ierr); 1344 ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr); 1345 ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr); 1346 for (c = cStart; c < cEnd; ++c) { 1347 PetscFECellGeom *cg; 1348 1349 ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr); 1350 ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr); 1351 ierr = DMPlexComputeCellGeometryFEM(dmCell, c, NULL, cg->v0, cg->J, cg->invJ, &cg->detJ);CHKERRQ(ierr); 1352 if (cg->detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", cg->detJ, c); 1353 } 1354 ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr); 1355 ierr = DMDestroy(&dmCell);CHKERRQ(ierr); 1356 PetscFunctionReturn(0); 1357 } 1358 1359 #undef __FUNCT__ 1360 #define __FUNCT__ "DMPlexComputeGeometryFVM" 1361 /*@ 1362 DMPlexComputeGeometryFVM - Computes the cell and face geometry for a finite volume method 1363 1364 Input Parameter: 1365 . dm - The DM 1366 1367 Output Parameters: 1368 + cellgeom - A Vec of PetscFVCellGeom data 1369 . facegeom - A Vec of PetscFVFaceGeom data 1370 1371 Level: developer 1372 1373 .seealso: PetscFVFaceGeom, PetscFVCellGeom, DMPlexComputeGeometryFEM() 1374 @*/ 1375 PetscErrorCode DMPlexComputeGeometryFVM(DM dm, Vec *cellgeom, Vec *facegeom) 1376 { 1377 DM dmFace, dmCell; 1378 DMLabel ghostLabel; 1379 PetscSection sectionFace, sectionCell; 1380 PetscSection coordSection; 1381 Vec coordinates; 1382 PetscScalar *fgeom, *cgeom; 1383 PetscReal minradius, gminradius; 1384 PetscInt dim, cStart, cEnd, cEndInterior, c, fStart, fEnd, f; 1385 PetscErrorCode ierr; 1386 1387 PetscFunctionBegin; 1388 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1389 ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 1390 ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 1391 /* Make cell centroids and volumes */ 1392 ierr = DMClone(dm, &dmCell);CHKERRQ(ierr); 1393 ierr = DMSetCoordinateSection(dmCell, PETSC_DETERMINE, coordSection);CHKERRQ(ierr); 1394 ierr = DMSetCoordinatesLocal(dmCell, coordinates);CHKERRQ(ierr); 1395 ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionCell);CHKERRQ(ierr); 1396 ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1397 ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 1398 ierr = PetscSectionSetChart(sectionCell, cStart, cEnd);CHKERRQ(ierr); 1399 for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionCell, c, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVCellGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);} 1400 ierr = PetscSectionSetUp(sectionCell);CHKERRQ(ierr); 1401 ierr = DMSetDefaultSection(dmCell, sectionCell);CHKERRQ(ierr); 1402 ierr = PetscSectionDestroy(§ionCell);CHKERRQ(ierr); 1403 ierr = DMCreateLocalVector(dmCell, cellgeom);CHKERRQ(ierr); 1404 ierr = VecGetArray(*cellgeom, &cgeom);CHKERRQ(ierr); 1405 for (c = cStart; c < cEndInterior; ++c) { 1406 PetscFVCellGeom *cg; 1407 1408 ierr = DMPlexPointLocalRef(dmCell, c, cgeom, &cg);CHKERRQ(ierr); 1409 ierr = PetscMemzero(cg, sizeof(*cg));CHKERRQ(ierr); 1410 ierr = DMPlexComputeCellGeometryFVM(dmCell, c, &cg->volume, cg->centroid, NULL);CHKERRQ(ierr); 1411 } 1412 /* Compute face normals and minimum cell radius */ 1413 ierr = DMClone(dm, &dmFace);CHKERRQ(ierr); 1414 ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionFace);CHKERRQ(ierr); 1415 ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 1416 ierr = PetscSectionSetChart(sectionFace, fStart, fEnd);CHKERRQ(ierr); 1417 for (f = fStart; f < fEnd; ++f) {ierr = PetscSectionSetDof(sectionFace, f, (PetscInt) PetscCeilReal(((PetscReal) sizeof(PetscFVFaceGeom))/sizeof(PetscScalar)));CHKERRQ(ierr);} 1418 ierr = PetscSectionSetUp(sectionFace);CHKERRQ(ierr); 1419 ierr = DMSetDefaultSection(dmFace, sectionFace);CHKERRQ(ierr); 1420 ierr = PetscSectionDestroy(§ionFace);CHKERRQ(ierr); 1421 ierr = DMCreateLocalVector(dmFace, facegeom);CHKERRQ(ierr); 1422 ierr = VecGetArray(*facegeom, &fgeom);CHKERRQ(ierr); 1423 ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 1424 minradius = PETSC_MAX_REAL; 1425 for (f = fStart; f < fEnd; ++f) { 1426 PetscFVFaceGeom *fg; 1427 PetscReal area; 1428 PetscInt ghost = -1, d; 1429 1430 if (ghostLabel) {ierr = DMLabelGetValue(ghostLabel, f, &ghost);CHKERRQ(ierr);} 1431 if (ghost >= 0) continue; 1432 ierr = DMPlexPointLocalRef(dmFace, f, fgeom, &fg);CHKERRQ(ierr); 1433 ierr = DMPlexComputeCellGeometryFVM(dm, f, &area, fg->centroid, fg->normal);CHKERRQ(ierr); 1434 for (d = 0; d < dim; ++d) fg->normal[d] *= area; 1435 /* Flip face orientation if necessary to match ordering in support, and Update minimum radius */ 1436 { 1437 PetscFVCellGeom *cL, *cR; 1438 const PetscInt *cells; 1439 PetscReal *lcentroid, *rcentroid; 1440 PetscReal l[3], r[3], v[3]; 1441 1442 ierr = DMPlexGetSupport(dm, f, &cells);CHKERRQ(ierr); 1443 ierr = DMPlexPointLocalRead(dmCell, cells[0], cgeom, &cL);CHKERRQ(ierr); 1444 ierr = DMPlexPointLocalRead(dmCell, cells[1], cgeom, &cR);CHKERRQ(ierr); 1445 lcentroid = cells[0] >= cEndInterior ? fg->centroid : cL->centroid; 1446 rcentroid = cells[1] >= cEndInterior ? fg->centroid : cR->centroid; 1447 ierr = DMPlexLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, lcentroid, l);CHKERRQ(ierr); 1448 ierr = DMPlexLocalizeCoordinateReal_Internal(dm, dim, fg->centroid, rcentroid, r);CHKERRQ(ierr); 1449 DMPlex_WaxpyD_Internal(dim, -1, l, r, v); 1450 if (DMPlex_DotRealD_Internal(dim, fg->normal, v) < 0) { 1451 for (d = 0; d < dim; ++d) fg->normal[d] = -fg->normal[d]; 1452 } 1453 if (DMPlex_DotRealD_Internal(dim, fg->normal, v) <= 0) { 1454 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]); 1455 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]); 1456 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Direction for face %d could not be fixed", f); 1457 } 1458 if (cells[0] < cEndInterior) { 1459 DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cL->centroid, v); 1460 minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v)); 1461 } 1462 if (cells[1] < cEndInterior) { 1463 DMPlex_WaxpyD_Internal(dim, -1, fg->centroid, cR->centroid, v); 1464 minradius = PetscMin(minradius, DMPlex_NormD_Internal(dim, v)); 1465 } 1466 } 1467 } 1468 ierr = MPI_Allreduce(&minradius, &gminradius, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 1469 ierr = DMPlexSetMinRadius(dm, gminradius);CHKERRQ(ierr); 1470 /* Compute centroids of ghost cells */ 1471 for (c = cEndInterior; c < cEnd; ++c) { 1472 PetscFVFaceGeom *fg; 1473 const PetscInt *cone, *support; 1474 PetscInt coneSize, supportSize, s; 1475 1476 ierr = DMPlexGetConeSize(dmCell, c, &coneSize);CHKERRQ(ierr); 1477 if (coneSize != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ghost cell %d has cone size %d != 1", c, coneSize); 1478 ierr = DMPlexGetCone(dmCell, c, &cone);CHKERRQ(ierr); 1479 ierr = DMPlexGetSupportSize(dmCell, cone[0], &supportSize);CHKERRQ(ierr); 1480 if (supportSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d has support size %d != 1", cone[0], supportSize); 1481 ierr = DMPlexGetSupport(dmCell, cone[0], &support);CHKERRQ(ierr); 1482 ierr = DMPlexPointLocalRef(dmFace, cone[0], fgeom, &fg);CHKERRQ(ierr); 1483 for (s = 0; s < 2; ++s) { 1484 /* Reflect ghost centroid across plane of face */ 1485 if (support[s] == c) { 1486 const PetscFVCellGeom *ci; 1487 PetscFVCellGeom *cg; 1488 PetscReal c2f[3], a; 1489 1490 ierr = DMPlexPointLocalRead(dmCell, support[(s+1)%2], cgeom, &ci);CHKERRQ(ierr); 1491 DMPlex_WaxpyD_Internal(dim, -1, ci->centroid, fg->centroid, c2f); /* cell to face centroid */ 1492 a = DMPlex_DotRealD_Internal(dim, c2f, fg->normal)/DMPlex_DotRealD_Internal(dim, fg->normal, fg->normal); 1493 ierr = DMPlexPointLocalRef(dmCell, support[s], cgeom, &cg);CHKERRQ(ierr); 1494 DMPlex_WaxpyD_Internal(dim, 2*a, fg->normal, ci->centroid, cg->centroid); 1495 cg->volume = ci->volume; 1496 } 1497 } 1498 } 1499 ierr = VecRestoreArray(*facegeom, &fgeom);CHKERRQ(ierr); 1500 ierr = VecRestoreArray(*cellgeom, &cgeom);CHKERRQ(ierr); 1501 ierr = DMDestroy(&dmCell);CHKERRQ(ierr); 1502 ierr = DMDestroy(&dmFace);CHKERRQ(ierr); 1503 PetscFunctionReturn(0); 1504 } 1505 1506 #undef __FUNCT__ 1507 #define __FUNCT__ "DMPlexGetMinRadius" 1508 /*@C 1509 DMPlexGetMinRadius - Returns the minimum distance from any cell centroid to a face 1510 1511 Not collective 1512 1513 Input Argument: 1514 . dm - the DM 1515 1516 Output Argument: 1517 . minradius - the minium cell radius 1518 1519 Level: developer 1520 1521 .seealso: DMGetCoordinates() 1522 @*/ 1523 PetscErrorCode DMPlexGetMinRadius(DM dm, PetscReal *minradius) 1524 { 1525 PetscFunctionBegin; 1526 PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1527 PetscValidPointer(minradius,2); 1528 *minradius = ((DM_Plex*) dm->data)->minradius; 1529 PetscFunctionReturn(0); 1530 } 1531 1532 #undef __FUNCT__ 1533 #define __FUNCT__ "DMPlexSetMinRadius" 1534 /*@C 1535 DMPlexSetMinRadius - Sets the minimum distance from the cell centroid to a face 1536 1537 Logically collective 1538 1539 Input Arguments: 1540 + dm - the DM 1541 - minradius - the minium cell radius 1542 1543 Level: developer 1544 1545 .seealso: DMSetCoordinates() 1546 @*/ 1547 PetscErrorCode DMPlexSetMinRadius(DM dm, PetscReal minradius) 1548 { 1549 PetscFunctionBegin; 1550 PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1551 ((DM_Plex*) dm->data)->minradius = minradius; 1552 PetscFunctionReturn(0); 1553 } 1554 1555 #undef __FUNCT__ 1556 #define __FUNCT__ "BuildGradientReconstruction_Internal" 1557 static PetscErrorCode BuildGradientReconstruction_Internal(DM dm, PetscFV fvm, DM dmFace, PetscScalar *fgeom, DM dmCell, PetscScalar *cgeom) 1558 { 1559 DMLabel ghostLabel; 1560 PetscScalar *dx, *grad, **gref; 1561 PetscInt dim, cStart, cEnd, c, cEndInterior, maxNumFaces; 1562 PetscErrorCode ierr; 1563 1564 PetscFunctionBegin; 1565 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1566 ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1567 ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 1568 ierr = DMPlexGetMaxSizes(dm, &maxNumFaces, NULL);CHKERRQ(ierr); 1569 ierr = PetscFVLeastSquaresSetMaxFaces(fvm, maxNumFaces);CHKERRQ(ierr); 1570 ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 1571 ierr = PetscMalloc3(maxNumFaces*dim, &dx, maxNumFaces*dim, &grad, maxNumFaces, &gref);CHKERRQ(ierr); 1572 for (c = cStart; c < cEndInterior; c++) { 1573 const PetscInt *faces; 1574 PetscInt numFaces, usedFaces, f, d; 1575 const PetscFVCellGeom *cg; 1576 PetscBool boundary; 1577 PetscInt ghost; 1578 1579 ierr = DMPlexPointLocalRead(dmCell, c, cgeom, &cg);CHKERRQ(ierr); 1580 ierr = DMPlexGetConeSize(dm, c, &numFaces);CHKERRQ(ierr); 1581 ierr = DMPlexGetCone(dm, c, &faces);CHKERRQ(ierr); 1582 if (numFaces < dim) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Cell %D has only %D faces, not enough for gradient reconstruction", c, numFaces); 1583 for (f = 0, usedFaces = 0; f < numFaces; ++f) { 1584 const PetscFVCellGeom *cg1; 1585 PetscFVFaceGeom *fg; 1586 const PetscInt *fcells; 1587 PetscInt ncell, side; 1588 1589 ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr); 1590 ierr = DMPlexIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr); 1591 if ((ghost >= 0) || boundary) continue; 1592 ierr = DMPlexGetSupport(dm, faces[f], &fcells);CHKERRQ(ierr); 1593 side = (c != fcells[0]); /* c is on left=0 or right=1 of face */ 1594 ncell = fcells[!side]; /* the neighbor */ 1595 ierr = DMPlexPointLocalRef(dmFace, faces[f], fgeom, &fg);CHKERRQ(ierr); 1596 ierr = DMPlexPointLocalRead(dmCell, ncell, cgeom, &cg1);CHKERRQ(ierr); 1597 for (d = 0; d < dim; ++d) dx[usedFaces*dim+d] = cg1->centroid[d] - cg->centroid[d]; 1598 gref[usedFaces++] = fg->grad[side]; /* Gradient reconstruction term will go here */ 1599 } 1600 if (!usedFaces) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Mesh contains isolated cell (no neighbors). Is it intentional?"); 1601 ierr = PetscFVComputeGradient(fvm, usedFaces, dx, grad);CHKERRQ(ierr); 1602 for (f = 0, usedFaces = 0; f < numFaces; ++f) { 1603 ierr = DMLabelGetValue(ghostLabel, faces[f], &ghost);CHKERRQ(ierr); 1604 ierr = DMPlexIsBoundaryPoint(dm, faces[f], &boundary);CHKERRQ(ierr); 1605 if ((ghost >= 0) || boundary) continue; 1606 for (d = 0; d < dim; ++d) gref[usedFaces][d] = grad[usedFaces*dim+d]; 1607 ++usedFaces; 1608 } 1609 } 1610 ierr = PetscFree3(dx, grad, gref);CHKERRQ(ierr); 1611 PetscFunctionReturn(0); 1612 } 1613 1614 #undef __FUNCT__ 1615 #define __FUNCT__ "DMPlexComputeGradientFVM" 1616 /*@ 1617 DMPlexComputeGradientFVM - Compute geometric factors for gradient reconstruction, which are stored in the geometry data, and compute layout for gradient data 1618 1619 Collective on DM 1620 1621 Input Arguments: 1622 + dm - The DM 1623 . fvm - The PetscFV 1624 . faceGeometry - The face geometry from DMPlexGetFaceGeometryFVM() 1625 - cellGeometry - The face geometry from DMPlexGetCellGeometryFVM() 1626 1627 Output Parameters: 1628 + faceGeometry - The geometric factors for gradient calculation are inserted 1629 - dmGrad - The DM describing the layout of gradient data 1630 1631 Level: developer 1632 1633 .seealso: DMPlexGetFaceGeometryFVM(), DMPlexGetCellGeometryFVM() 1634 @*/ 1635 PetscErrorCode DMPlexComputeGradientFVM(DM dm, PetscFV fvm, Vec faceGeometry, Vec cellGeometry, DM *dmGrad) 1636 { 1637 DM dmFace, dmCell; 1638 PetscScalar *fgeom, *cgeom; 1639 PetscSection sectionGrad; 1640 PetscInt dim, pdim, cStart, cEnd, cEndInterior, c; 1641 PetscErrorCode ierr; 1642 1643 PetscFunctionBegin; 1644 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1645 ierr = PetscFVGetNumComponents(fvm, &pdim);CHKERRQ(ierr); 1646 ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1647 ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 1648 /* Construct the interpolant corresponding to each face from the least-square solution over the cell neighborhood */ 1649 ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 1650 ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 1651 ierr = VecGetArray(faceGeometry, &fgeom);CHKERRQ(ierr); 1652 ierr = VecGetArray(cellGeometry, &cgeom);CHKERRQ(ierr); 1653 ierr = BuildGradientReconstruction_Internal(dm, fvm, dmFace, fgeom, dmCell, cgeom);CHKERRQ(ierr); 1654 ierr = VecRestoreArray(faceGeometry, &fgeom);CHKERRQ(ierr); 1655 ierr = VecRestoreArray(cellGeometry, &cgeom);CHKERRQ(ierr); 1656 /* Create storage for gradients */ 1657 ierr = DMClone(dm, dmGrad);CHKERRQ(ierr); 1658 ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), §ionGrad);CHKERRQ(ierr); 1659 ierr = PetscSectionSetChart(sectionGrad, cStart, cEnd);CHKERRQ(ierr); 1660 for (c = cStart; c < cEnd; ++c) {ierr = PetscSectionSetDof(sectionGrad, c, pdim*dim);CHKERRQ(ierr);} 1661 ierr = PetscSectionSetUp(sectionGrad);CHKERRQ(ierr); 1662 ierr = DMSetDefaultSection(*dmGrad, sectionGrad);CHKERRQ(ierr); 1663 ierr = PetscSectionDestroy(§ionGrad);CHKERRQ(ierr); 1664 PetscFunctionReturn(0); 1665 } 1666