1 #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 2 #include <petsc/private/snesimpl.h> /*I "petscsnes.h" I*/ 3 #include <petscds.h> 4 #include <petsc/private/petscimpl.h> 5 #include <petsc/private/petscfeimpl.h> 6 7 /************************** Interpolation *******************************/ 8 9 #undef __FUNCT__ 10 #define __FUNCT__ "DMSNESConvertPlex" 11 static PetscErrorCode DMSNESConvertPlex(DM dm, DM *plex, PetscBool copy) 12 { 13 PetscBool isPlex; 14 PetscErrorCode ierr; 15 16 PetscFunctionBegin; 17 ierr = PetscObjectTypeCompare((PetscObject) dm, DMPLEX, &isPlex);CHKERRQ(ierr); 18 if (isPlex) { 19 *plex = dm; 20 ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr); 21 } else { 22 ierr = PetscObjectQuery((PetscObject) dm, "dm_plex", (PetscObject *) plex);CHKERRQ(ierr); 23 if (!*plex) { 24 ierr = DMConvert(dm,DMPLEX,plex);CHKERRQ(ierr); 25 ierr = PetscObjectCompose((PetscObject) dm, "dm_plex", (PetscObject) *plex);CHKERRQ(ierr); 26 if (copy) { 27 PetscInt i; 28 PetscObject obj; 29 const char *comps[3] = {"A","dmAux","dmCh"}; 30 31 ierr = DMCopyDMSNES(dm, *plex);CHKERRQ(ierr); 32 for (i = 0; i < 3; i++) { 33 ierr = PetscObjectQuery((PetscObject) dm, comps[i], &obj);CHKERRQ(ierr); 34 ierr = PetscObjectCompose((PetscObject) *plex, comps[i], obj);CHKERRQ(ierr); 35 } 36 } 37 } else { 38 ierr = PetscObjectReference((PetscObject) *plex);CHKERRQ(ierr); 39 } 40 } 41 PetscFunctionReturn(0); 42 } 43 44 #undef __FUNCT__ 45 #define __FUNCT__ "DMInterpolationCreate" 46 PetscErrorCode DMInterpolationCreate(MPI_Comm comm, DMInterpolationInfo *ctx) 47 { 48 PetscErrorCode ierr; 49 50 PetscFunctionBegin; 51 PetscValidPointer(ctx, 2); 52 ierr = PetscMalloc(sizeof(struct _DMInterpolationInfo), ctx);CHKERRQ(ierr); 53 54 (*ctx)->comm = comm; 55 (*ctx)->dim = -1; 56 (*ctx)->nInput = 0; 57 (*ctx)->points = NULL; 58 (*ctx)->cells = NULL; 59 (*ctx)->n = -1; 60 (*ctx)->coords = NULL; 61 PetscFunctionReturn(0); 62 } 63 64 #undef __FUNCT__ 65 #define __FUNCT__ "DMInterpolationSetDim" 66 PetscErrorCode DMInterpolationSetDim(DMInterpolationInfo ctx, PetscInt dim) 67 { 68 PetscFunctionBegin; 69 if ((dim < 1) || (dim > 3)) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension for points: %d", dim); 70 ctx->dim = dim; 71 PetscFunctionReturn(0); 72 } 73 74 #undef __FUNCT__ 75 #define __FUNCT__ "DMInterpolationGetDim" 76 PetscErrorCode DMInterpolationGetDim(DMInterpolationInfo ctx, PetscInt *dim) 77 { 78 PetscFunctionBegin; 79 PetscValidIntPointer(dim, 2); 80 *dim = ctx->dim; 81 PetscFunctionReturn(0); 82 } 83 84 #undef __FUNCT__ 85 #define __FUNCT__ "DMInterpolationSetDof" 86 PetscErrorCode DMInterpolationSetDof(DMInterpolationInfo ctx, PetscInt dof) 87 { 88 PetscFunctionBegin; 89 if (dof < 1) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of components: %d", dof); 90 ctx->dof = dof; 91 PetscFunctionReturn(0); 92 } 93 94 #undef __FUNCT__ 95 #define __FUNCT__ "DMInterpolationGetDof" 96 PetscErrorCode DMInterpolationGetDof(DMInterpolationInfo ctx, PetscInt *dof) 97 { 98 PetscFunctionBegin; 99 PetscValidIntPointer(dof, 2); 100 *dof = ctx->dof; 101 PetscFunctionReturn(0); 102 } 103 104 #undef __FUNCT__ 105 #define __FUNCT__ "DMInterpolationAddPoints" 106 PetscErrorCode DMInterpolationAddPoints(DMInterpolationInfo ctx, PetscInt n, PetscReal points[]) 107 { 108 PetscErrorCode ierr; 109 110 PetscFunctionBegin; 111 if (ctx->dim < 0) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set"); 112 if (ctx->points) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "Cannot add points multiple times yet"); 113 ctx->nInput = n; 114 115 ierr = PetscMalloc1(n*ctx->dim, &ctx->points);CHKERRQ(ierr); 116 ierr = PetscMemcpy(ctx->points, points, n*ctx->dim * sizeof(PetscReal));CHKERRQ(ierr); 117 PetscFunctionReturn(0); 118 } 119 120 #undef __FUNCT__ 121 #define __FUNCT__ "DMInterpolationSetUp" 122 PetscErrorCode DMInterpolationSetUp(DMInterpolationInfo ctx, DM dm, PetscBool redundantPoints) 123 { 124 MPI_Comm comm = ctx->comm; 125 PetscScalar *a; 126 PetscInt p, q, i; 127 PetscMPIInt rank, size; 128 PetscErrorCode ierr; 129 Vec pointVec; 130 PetscSF cellSF; 131 PetscLayout layout; 132 PetscReal *globalPoints; 133 PetscScalar *globalPointsScalar; 134 const PetscInt *ranges; 135 PetscMPIInt *counts, *displs; 136 const PetscSFNode *foundCells; 137 const PetscInt *foundPoints; 138 PetscMPIInt *foundProcs, *globalProcs; 139 PetscInt n, N, numFound; 140 141 PetscFunctionBegin; 142 PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 143 ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); 144 ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 145 if (ctx->dim < 0) SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set"); 146 /* Locate points */ 147 n = ctx->nInput; 148 if (!redundantPoints) { 149 ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr); 150 ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr); 151 ierr = PetscLayoutSetLocalSize(layout, n);CHKERRQ(ierr); 152 ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr); 153 ierr = PetscLayoutGetSize(layout, &N);CHKERRQ(ierr); 154 /* Communicate all points to all processes */ 155 ierr = PetscMalloc3(N*ctx->dim,&globalPoints,size,&counts,size,&displs);CHKERRQ(ierr); 156 ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr); 157 for (p = 0; p < size; ++p) { 158 counts[p] = (ranges[p+1] - ranges[p])*ctx->dim; 159 displs[p] = ranges[p]*ctx->dim; 160 } 161 ierr = MPI_Allgatherv(ctx->points, n*ctx->dim, MPIU_REAL, globalPoints, counts, displs, MPIU_REAL, comm);CHKERRQ(ierr); 162 } else { 163 N = n; 164 globalPoints = ctx->points; 165 counts = displs = NULL; 166 layout = NULL; 167 } 168 #if 0 169 ierr = PetscMalloc3(N,&foundCells,N,&foundProcs,N,&globalProcs);CHKERRQ(ierr); 170 /* foundCells[p] = m->locatePoint(&globalPoints[p*ctx->dim]); */ 171 #else 172 #if defined(PETSC_USE_COMPLEX) 173 ierr = PetscMalloc1(N,&globalPointsScalar);CHKERRQ(ierr); 174 for (i=0; i<N; i++) globalPointsScalar[i] = globalPoints[i]; 175 #else 176 globalPointsScalar = globalPoints; 177 #endif 178 ierr = VecCreateSeqWithArray(PETSC_COMM_SELF, ctx->dim, N*ctx->dim, globalPointsScalar, &pointVec);CHKERRQ(ierr); 179 ierr = PetscMalloc2(N,&foundProcs,N,&globalProcs);CHKERRQ(ierr); 180 cellSF = NULL; 181 ierr = DMLocatePoints(dm, pointVec, &cellSF);CHKERRQ(ierr); 182 ierr = PetscSFGetGraph(cellSF,NULL,&numFound,&foundPoints,&foundCells);CHKERRQ(ierr); 183 #endif 184 for (p = 0; p < numFound; ++p) { 185 if (foundCells[p].index >= 0) foundProcs[foundPoints ? foundPoints[p] : p] = rank; 186 else foundProcs[foundPoints ? foundPoints[p] : p] = size; 187 } 188 /* Let the lowest rank process own each point */ 189 ierr = MPIU_Allreduce(foundProcs, globalProcs, N, MPI_INT, MPI_MIN, comm);CHKERRQ(ierr); 190 ctx->n = 0; 191 for (p = 0; p < N; ++p) { 192 if (globalProcs[p] == size) SETERRQ4(comm, PETSC_ERR_PLIB, "Point %d: %g %g %g not located in mesh", p, globalPoints[p*ctx->dim+0], ctx->dim > 1 ? globalPoints[p*ctx->dim+1] : 0.0, ctx->dim > 2 ? globalPoints[p*ctx->dim+2] : 0.0); 193 else if (globalProcs[p] == rank) ctx->n++; 194 } 195 /* Create coordinates vector and array of owned cells */ 196 ierr = PetscMalloc1(ctx->n, &ctx->cells);CHKERRQ(ierr); 197 ierr = VecCreate(comm, &ctx->coords);CHKERRQ(ierr); 198 ierr = VecSetSizes(ctx->coords, ctx->n*ctx->dim, PETSC_DECIDE);CHKERRQ(ierr); 199 ierr = VecSetBlockSize(ctx->coords, ctx->dim);CHKERRQ(ierr); 200 ierr = VecSetType(ctx->coords,VECSTANDARD);CHKERRQ(ierr); 201 ierr = VecGetArray(ctx->coords, &a);CHKERRQ(ierr); 202 for (p = 0, q = 0, i = 0; p < N; ++p) { 203 if (globalProcs[p] == rank) { 204 PetscInt d; 205 206 for (d = 0; d < ctx->dim; ++d, ++i) a[i] = globalPoints[p*ctx->dim+d]; 207 ctx->cells[q++] = foundCells[p].index; 208 } 209 } 210 ierr = VecRestoreArray(ctx->coords, &a);CHKERRQ(ierr); 211 #if 0 212 ierr = PetscFree3(foundCells,foundProcs,globalProcs);CHKERRQ(ierr); 213 #else 214 ierr = PetscFree2(foundProcs,globalProcs);CHKERRQ(ierr); 215 ierr = PetscSFDestroy(&cellSF);CHKERRQ(ierr); 216 ierr = VecDestroy(&pointVec);CHKERRQ(ierr); 217 #endif 218 if ((void*)globalPointsScalar != (void*)globalPoints) {ierr = PetscFree(globalPointsScalar);CHKERRQ(ierr);} 219 if (!redundantPoints) {ierr = PetscFree3(globalPoints,counts,displs);CHKERRQ(ierr);} 220 ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr); 221 PetscFunctionReturn(0); 222 } 223 224 #undef __FUNCT__ 225 #define __FUNCT__ "DMInterpolationGetCoordinates" 226 PetscErrorCode DMInterpolationGetCoordinates(DMInterpolationInfo ctx, Vec *coordinates) 227 { 228 PetscFunctionBegin; 229 PetscValidPointer(coordinates, 2); 230 if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup."); 231 *coordinates = ctx->coords; 232 PetscFunctionReturn(0); 233 } 234 235 #undef __FUNCT__ 236 #define __FUNCT__ "DMInterpolationGetVector" 237 PetscErrorCode DMInterpolationGetVector(DMInterpolationInfo ctx, Vec *v) 238 { 239 PetscErrorCode ierr; 240 241 PetscFunctionBegin; 242 PetscValidPointer(v, 2); 243 if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup."); 244 ierr = VecCreate(ctx->comm, v);CHKERRQ(ierr); 245 ierr = VecSetSizes(*v, ctx->n*ctx->dof, PETSC_DECIDE);CHKERRQ(ierr); 246 ierr = VecSetBlockSize(*v, ctx->dof);CHKERRQ(ierr); 247 ierr = VecSetType(*v,VECSTANDARD);CHKERRQ(ierr); 248 PetscFunctionReturn(0); 249 } 250 251 #undef __FUNCT__ 252 #define __FUNCT__ "DMInterpolationRestoreVector" 253 PetscErrorCode DMInterpolationRestoreVector(DMInterpolationInfo ctx, Vec *v) 254 { 255 PetscErrorCode ierr; 256 257 PetscFunctionBegin; 258 PetscValidPointer(v, 2); 259 if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup."); 260 ierr = VecDestroy(v);CHKERRQ(ierr); 261 PetscFunctionReturn(0); 262 } 263 264 #undef __FUNCT__ 265 #define __FUNCT__ "DMInterpolate_Triangle_Private" 266 PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Triangle_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 267 { 268 PetscReal *v0, *J, *invJ, detJ; 269 const PetscScalar *coords; 270 PetscScalar *a; 271 PetscInt p; 272 PetscErrorCode ierr; 273 274 PetscFunctionBegin; 275 ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr); 276 ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 277 ierr = VecGetArray(v, &a);CHKERRQ(ierr); 278 for (p = 0; p < ctx->n; ++p) { 279 PetscInt c = ctx->cells[p]; 280 PetscScalar *x = NULL; 281 PetscReal xi[4]; 282 PetscInt d, f, comp; 283 284 ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 285 if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 286 ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 287 for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp]; 288 289 for (d = 0; d < ctx->dim; ++d) { 290 xi[d] = 0.0; 291 for (f = 0; f < ctx->dim; ++f) xi[d] += invJ[d*ctx->dim+f]*0.5*PetscRealPart(coords[p*ctx->dim+f] - v0[f]); 292 for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] += PetscRealPart(x[(d+1)*ctx->dof+comp] - x[0*ctx->dof+comp])*xi[d]; 293 } 294 ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 295 } 296 ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 297 ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 298 ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr); 299 PetscFunctionReturn(0); 300 } 301 302 #undef __FUNCT__ 303 #define __FUNCT__ "DMInterpolate_Tetrahedron_Private" 304 PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Tetrahedron_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 305 { 306 PetscReal *v0, *J, *invJ, detJ; 307 const PetscScalar *coords; 308 PetscScalar *a; 309 PetscInt p; 310 PetscErrorCode ierr; 311 312 PetscFunctionBegin; 313 ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr); 314 ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 315 ierr = VecGetArray(v, &a);CHKERRQ(ierr); 316 for (p = 0; p < ctx->n; ++p) { 317 PetscInt c = ctx->cells[p]; 318 const PetscInt order[3] = {2, 1, 3}; 319 PetscScalar *x = NULL; 320 PetscReal xi[4]; 321 PetscInt d, f, comp; 322 323 ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 324 if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 325 ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 326 for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp]; 327 328 for (d = 0; d < ctx->dim; ++d) { 329 xi[d] = 0.0; 330 for (f = 0; f < ctx->dim; ++f) xi[d] += invJ[d*ctx->dim+f]*0.5*PetscRealPart(coords[p*ctx->dim+f] - v0[f]); 331 for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] += PetscRealPart(x[order[d]*ctx->dof+comp] - x[0*ctx->dof+comp])*xi[d]; 332 } 333 ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 334 } 335 ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 336 ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 337 ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr); 338 PetscFunctionReturn(0); 339 } 340 341 #undef __FUNCT__ 342 #define __FUNCT__ "QuadMap_Private" 343 PETSC_STATIC_INLINE PetscErrorCode QuadMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx) 344 { 345 const PetscScalar *vertices = (const PetscScalar*) ctx; 346 const PetscScalar x0 = vertices[0]; 347 const PetscScalar y0 = vertices[1]; 348 const PetscScalar x1 = vertices[2]; 349 const PetscScalar y1 = vertices[3]; 350 const PetscScalar x2 = vertices[4]; 351 const PetscScalar y2 = vertices[5]; 352 const PetscScalar x3 = vertices[6]; 353 const PetscScalar y3 = vertices[7]; 354 const PetscScalar f_1 = x1 - x0; 355 const PetscScalar g_1 = y1 - y0; 356 const PetscScalar f_3 = x3 - x0; 357 const PetscScalar g_3 = y3 - y0; 358 const PetscScalar f_01 = x2 - x1 - x3 + x0; 359 const PetscScalar g_01 = y2 - y1 - y3 + y0; 360 const PetscScalar *ref; 361 PetscScalar *real; 362 PetscErrorCode ierr; 363 364 PetscFunctionBegin; 365 ierr = VecGetArrayRead(Xref, &ref);CHKERRQ(ierr); 366 ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr); 367 { 368 const PetscScalar p0 = ref[0]; 369 const PetscScalar p1 = ref[1]; 370 371 real[0] = x0 + f_1 * p0 + f_3 * p1 + f_01 * p0 * p1; 372 real[1] = y0 + g_1 * p0 + g_3 * p1 + g_01 * p0 * p1; 373 } 374 ierr = PetscLogFlops(28);CHKERRQ(ierr); 375 ierr = VecRestoreArrayRead(Xref, &ref);CHKERRQ(ierr); 376 ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr); 377 PetscFunctionReturn(0); 378 } 379 380 #include <petsc/private/dmimpl.h> 381 #undef __FUNCT__ 382 #define __FUNCT__ "QuadJacobian_Private" 383 PETSC_STATIC_INLINE PetscErrorCode QuadJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx) 384 { 385 const PetscScalar *vertices = (const PetscScalar*) ctx; 386 const PetscScalar x0 = vertices[0]; 387 const PetscScalar y0 = vertices[1]; 388 const PetscScalar x1 = vertices[2]; 389 const PetscScalar y1 = vertices[3]; 390 const PetscScalar x2 = vertices[4]; 391 const PetscScalar y2 = vertices[5]; 392 const PetscScalar x3 = vertices[6]; 393 const PetscScalar y3 = vertices[7]; 394 const PetscScalar f_01 = x2 - x1 - x3 + x0; 395 const PetscScalar g_01 = y2 - y1 - y3 + y0; 396 const PetscScalar *ref; 397 PetscErrorCode ierr; 398 399 PetscFunctionBegin; 400 ierr = VecGetArrayRead(Xref, &ref);CHKERRQ(ierr); 401 { 402 const PetscScalar x = ref[0]; 403 const PetscScalar y = ref[1]; 404 const PetscInt rows[2] = {0, 1}; 405 PetscScalar values[4]; 406 407 values[0] = (x1 - x0 + f_01*y) * 0.5; values[1] = (x3 - x0 + f_01*x) * 0.5; 408 values[2] = (y1 - y0 + g_01*y) * 0.5; values[3] = (y3 - y0 + g_01*x) * 0.5; 409 ierr = MatSetValues(J, 2, rows, 2, rows, values, INSERT_VALUES);CHKERRQ(ierr); 410 } 411 ierr = PetscLogFlops(30);CHKERRQ(ierr); 412 ierr = VecRestoreArrayRead(Xref, &ref);CHKERRQ(ierr); 413 ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 414 ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 415 PetscFunctionReturn(0); 416 } 417 418 #undef __FUNCT__ 419 #define __FUNCT__ "DMInterpolate_Quad_Private" 420 PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Quad_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 421 { 422 DM dmCoord; 423 SNES snes; 424 KSP ksp; 425 PC pc; 426 Vec coordsLocal, r, ref, real; 427 Mat J; 428 const PetscScalar *coords; 429 PetscScalar *a; 430 PetscInt p; 431 PetscErrorCode ierr; 432 433 PetscFunctionBegin; 434 ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 435 ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr); 436 ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr); 437 ierr = SNESSetOptionsPrefix(snes, "quad_interp_");CHKERRQ(ierr); 438 ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr); 439 ierr = VecSetSizes(r, 2, 2);CHKERRQ(ierr); 440 ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr); 441 ierr = VecDuplicate(r, &ref);CHKERRQ(ierr); 442 ierr = VecDuplicate(r, &real);CHKERRQ(ierr); 443 ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr); 444 ierr = MatSetSizes(J, 2, 2, 2, 2);CHKERRQ(ierr); 445 ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr); 446 ierr = MatSetUp(J);CHKERRQ(ierr); 447 ierr = SNESSetFunction(snes, r, QuadMap_Private, NULL);CHKERRQ(ierr); 448 ierr = SNESSetJacobian(snes, J, J, QuadJacobian_Private, NULL);CHKERRQ(ierr); 449 ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr); 450 ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr); 451 ierr = PCSetType(pc, PCLU);CHKERRQ(ierr); 452 ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 453 454 ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 455 ierr = VecGetArray(v, &a);CHKERRQ(ierr); 456 for (p = 0; p < ctx->n; ++p) { 457 PetscScalar *x = NULL, *vertices = NULL; 458 PetscScalar *xi; 459 PetscReal xir[2]; 460 PetscInt c = ctx->cells[p], comp, coordSize, xSize; 461 462 /* Can make this do all points at once */ 463 ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 464 if (4*2 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 4*2); 465 ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 466 if (4*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 4*ctx->dof); 467 ierr = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 468 ierr = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 469 ierr = VecGetArray(real, &xi);CHKERRQ(ierr); 470 xi[0] = coords[p*ctx->dim+0]; 471 xi[1] = coords[p*ctx->dim+1]; 472 ierr = VecRestoreArray(real, &xi);CHKERRQ(ierr); 473 ierr = SNESSolve(snes, real, ref);CHKERRQ(ierr); 474 ierr = VecGetArray(ref, &xi);CHKERRQ(ierr); 475 xir[0] = PetscRealPart(xi[0]); 476 xir[1] = PetscRealPart(xi[1]); 477 for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp]*(1 - xir[0])*(1 - xir[1]) + x[1*ctx->dof+comp]*xir[0]*(1 - xir[1]) + x[2*ctx->dof+comp]*xir[0]*xir[1] + x[3*ctx->dof+comp]*(1 - xir[0])*xir[1]; 478 479 ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr); 480 ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 481 ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 482 } 483 ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 484 ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 485 486 ierr = SNESDestroy(&snes);CHKERRQ(ierr); 487 ierr = VecDestroy(&r);CHKERRQ(ierr); 488 ierr = VecDestroy(&ref);CHKERRQ(ierr); 489 ierr = VecDestroy(&real);CHKERRQ(ierr); 490 ierr = MatDestroy(&J);CHKERRQ(ierr); 491 PetscFunctionReturn(0); 492 } 493 494 #undef __FUNCT__ 495 #define __FUNCT__ "HexMap_Private" 496 PETSC_STATIC_INLINE PetscErrorCode HexMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx) 497 { 498 const PetscScalar *vertices = (const PetscScalar*) ctx; 499 const PetscScalar x0 = vertices[0]; 500 const PetscScalar y0 = vertices[1]; 501 const PetscScalar z0 = vertices[2]; 502 const PetscScalar x1 = vertices[9]; 503 const PetscScalar y1 = vertices[10]; 504 const PetscScalar z1 = vertices[11]; 505 const PetscScalar x2 = vertices[6]; 506 const PetscScalar y2 = vertices[7]; 507 const PetscScalar z2 = vertices[8]; 508 const PetscScalar x3 = vertices[3]; 509 const PetscScalar y3 = vertices[4]; 510 const PetscScalar z3 = vertices[5]; 511 const PetscScalar x4 = vertices[12]; 512 const PetscScalar y4 = vertices[13]; 513 const PetscScalar z4 = vertices[14]; 514 const PetscScalar x5 = vertices[15]; 515 const PetscScalar y5 = vertices[16]; 516 const PetscScalar z5 = vertices[17]; 517 const PetscScalar x6 = vertices[18]; 518 const PetscScalar y6 = vertices[19]; 519 const PetscScalar z6 = vertices[20]; 520 const PetscScalar x7 = vertices[21]; 521 const PetscScalar y7 = vertices[22]; 522 const PetscScalar z7 = vertices[23]; 523 const PetscScalar f_1 = x1 - x0; 524 const PetscScalar g_1 = y1 - y0; 525 const PetscScalar h_1 = z1 - z0; 526 const PetscScalar f_3 = x3 - x0; 527 const PetscScalar g_3 = y3 - y0; 528 const PetscScalar h_3 = z3 - z0; 529 const PetscScalar f_4 = x4 - x0; 530 const PetscScalar g_4 = y4 - y0; 531 const PetscScalar h_4 = z4 - z0; 532 const PetscScalar f_01 = x2 - x1 - x3 + x0; 533 const PetscScalar g_01 = y2 - y1 - y3 + y0; 534 const PetscScalar h_01 = z2 - z1 - z3 + z0; 535 const PetscScalar f_12 = x7 - x3 - x4 + x0; 536 const PetscScalar g_12 = y7 - y3 - y4 + y0; 537 const PetscScalar h_12 = z7 - z3 - z4 + z0; 538 const PetscScalar f_02 = x5 - x1 - x4 + x0; 539 const PetscScalar g_02 = y5 - y1 - y4 + y0; 540 const PetscScalar h_02 = z5 - z1 - z4 + z0; 541 const PetscScalar f_012 = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7; 542 const PetscScalar g_012 = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7; 543 const PetscScalar h_012 = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7; 544 const PetscScalar *ref; 545 PetscScalar *real; 546 PetscErrorCode ierr; 547 548 PetscFunctionBegin; 549 ierr = VecGetArrayRead(Xref, &ref);CHKERRQ(ierr); 550 ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr); 551 { 552 const PetscScalar p0 = ref[0]; 553 const PetscScalar p1 = ref[1]; 554 const PetscScalar p2 = ref[2]; 555 556 real[0] = x0 + f_1*p0 + f_3*p1 + f_4*p2 + f_01*p0*p1 + f_12*p1*p2 + f_02*p0*p2 + f_012*p0*p1*p2; 557 real[1] = y0 + g_1*p0 + g_3*p1 + g_4*p2 + g_01*p0*p1 + g_01*p0*p1 + g_12*p1*p2 + g_02*p0*p2 + g_012*p0*p1*p2; 558 real[2] = z0 + h_1*p0 + h_3*p1 + h_4*p2 + h_01*p0*p1 + h_01*p0*p1 + h_12*p1*p2 + h_02*p0*p2 + h_012*p0*p1*p2; 559 } 560 ierr = PetscLogFlops(114);CHKERRQ(ierr); 561 ierr = VecRestoreArrayRead(Xref, &ref);CHKERRQ(ierr); 562 ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr); 563 PetscFunctionReturn(0); 564 } 565 566 #undef __FUNCT__ 567 #define __FUNCT__ "HexJacobian_Private" 568 PETSC_STATIC_INLINE PetscErrorCode HexJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx) 569 { 570 const PetscScalar *vertices = (const PetscScalar*) ctx; 571 const PetscScalar x0 = vertices[0]; 572 const PetscScalar y0 = vertices[1]; 573 const PetscScalar z0 = vertices[2]; 574 const PetscScalar x1 = vertices[9]; 575 const PetscScalar y1 = vertices[10]; 576 const PetscScalar z1 = vertices[11]; 577 const PetscScalar x2 = vertices[6]; 578 const PetscScalar y2 = vertices[7]; 579 const PetscScalar z2 = vertices[8]; 580 const PetscScalar x3 = vertices[3]; 581 const PetscScalar y3 = vertices[4]; 582 const PetscScalar z3 = vertices[5]; 583 const PetscScalar x4 = vertices[12]; 584 const PetscScalar y4 = vertices[13]; 585 const PetscScalar z4 = vertices[14]; 586 const PetscScalar x5 = vertices[15]; 587 const PetscScalar y5 = vertices[16]; 588 const PetscScalar z5 = vertices[17]; 589 const PetscScalar x6 = vertices[18]; 590 const PetscScalar y6 = vertices[19]; 591 const PetscScalar z6 = vertices[20]; 592 const PetscScalar x7 = vertices[21]; 593 const PetscScalar y7 = vertices[22]; 594 const PetscScalar z7 = vertices[23]; 595 const PetscScalar f_xy = x2 - x1 - x3 + x0; 596 const PetscScalar g_xy = y2 - y1 - y3 + y0; 597 const PetscScalar h_xy = z2 - z1 - z3 + z0; 598 const PetscScalar f_yz = x7 - x3 - x4 + x0; 599 const PetscScalar g_yz = y7 - y3 - y4 + y0; 600 const PetscScalar h_yz = z7 - z3 - z4 + z0; 601 const PetscScalar f_xz = x5 - x1 - x4 + x0; 602 const PetscScalar g_xz = y5 - y1 - y4 + y0; 603 const PetscScalar h_xz = z5 - z1 - z4 + z0; 604 const PetscScalar f_xyz = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7; 605 const PetscScalar g_xyz = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7; 606 const PetscScalar h_xyz = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7; 607 const PetscScalar *ref; 608 PetscErrorCode ierr; 609 610 PetscFunctionBegin; 611 ierr = VecGetArrayRead(Xref, &ref);CHKERRQ(ierr); 612 { 613 const PetscScalar x = ref[0]; 614 const PetscScalar y = ref[1]; 615 const PetscScalar z = ref[2]; 616 const PetscInt rows[3] = {0, 1, 2}; 617 PetscScalar values[9]; 618 619 values[0] = (x1 - x0 + f_xy*y + f_xz*z + f_xyz*y*z) / 2.0; 620 values[1] = (x3 - x0 + f_xy*x + f_yz*z + f_xyz*x*z) / 2.0; 621 values[2] = (x4 - x0 + f_yz*y + f_xz*x + f_xyz*x*y) / 2.0; 622 values[3] = (y1 - y0 + g_xy*y + g_xz*z + g_xyz*y*z) / 2.0; 623 values[4] = (y3 - y0 + g_xy*x + g_yz*z + g_xyz*x*z) / 2.0; 624 values[5] = (y4 - y0 + g_yz*y + g_xz*x + g_xyz*x*y) / 2.0; 625 values[6] = (z1 - z0 + h_xy*y + h_xz*z + h_xyz*y*z) / 2.0; 626 values[7] = (z3 - z0 + h_xy*x + h_yz*z + h_xyz*x*z) / 2.0; 627 values[8] = (z4 - z0 + h_yz*y + h_xz*x + h_xyz*x*y) / 2.0; 628 629 ierr = MatSetValues(J, 3, rows, 3, rows, values, INSERT_VALUES);CHKERRQ(ierr); 630 } 631 ierr = PetscLogFlops(152);CHKERRQ(ierr); 632 ierr = VecRestoreArrayRead(Xref, &ref);CHKERRQ(ierr); 633 ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 634 ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 635 PetscFunctionReturn(0); 636 } 637 638 #undef __FUNCT__ 639 #define __FUNCT__ "DMInterpolate_Hex_Private" 640 PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Hex_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 641 { 642 DM dmCoord; 643 SNES snes; 644 KSP ksp; 645 PC pc; 646 Vec coordsLocal, r, ref, real; 647 Mat J; 648 const PetscScalar *coords; 649 PetscScalar *a; 650 PetscInt p; 651 PetscErrorCode ierr; 652 653 PetscFunctionBegin; 654 ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 655 ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr); 656 ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr); 657 ierr = SNESSetOptionsPrefix(snes, "hex_interp_");CHKERRQ(ierr); 658 ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr); 659 ierr = VecSetSizes(r, 3, 3);CHKERRQ(ierr); 660 ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr); 661 ierr = VecDuplicate(r, &ref);CHKERRQ(ierr); 662 ierr = VecDuplicate(r, &real);CHKERRQ(ierr); 663 ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr); 664 ierr = MatSetSizes(J, 3, 3, 3, 3);CHKERRQ(ierr); 665 ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr); 666 ierr = MatSetUp(J);CHKERRQ(ierr); 667 ierr = SNESSetFunction(snes, r, HexMap_Private, NULL);CHKERRQ(ierr); 668 ierr = SNESSetJacobian(snes, J, J, HexJacobian_Private, NULL);CHKERRQ(ierr); 669 ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr); 670 ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr); 671 ierr = PCSetType(pc, PCLU);CHKERRQ(ierr); 672 ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 673 674 ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 675 ierr = VecGetArray(v, &a);CHKERRQ(ierr); 676 for (p = 0; p < ctx->n; ++p) { 677 PetscScalar *x = NULL, *vertices = NULL; 678 PetscScalar *xi; 679 PetscReal xir[3]; 680 PetscInt c = ctx->cells[p], comp, coordSize, xSize; 681 682 /* Can make this do all points at once */ 683 ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 684 if (8*3 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 8*3); 685 ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 686 if (8*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 8*ctx->dof); 687 ierr = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 688 ierr = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 689 ierr = VecGetArray(real, &xi);CHKERRQ(ierr); 690 xi[0] = coords[p*ctx->dim+0]; 691 xi[1] = coords[p*ctx->dim+1]; 692 xi[2] = coords[p*ctx->dim+2]; 693 ierr = VecRestoreArray(real, &xi);CHKERRQ(ierr); 694 ierr = SNESSolve(snes, real, ref);CHKERRQ(ierr); 695 ierr = VecGetArray(ref, &xi);CHKERRQ(ierr); 696 xir[0] = PetscRealPart(xi[0]); 697 xir[1] = PetscRealPart(xi[1]); 698 xir[2] = PetscRealPart(xi[2]); 699 for (comp = 0; comp < ctx->dof; ++comp) { 700 a[p*ctx->dof+comp] = 701 x[0*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*(1-xir[2]) + 702 x[3*ctx->dof+comp]* xir[0]*(1-xir[1])*(1-xir[2]) + 703 x[2*ctx->dof+comp]* xir[0]* xir[1]*(1-xir[2]) + 704 x[1*ctx->dof+comp]*(1-xir[0])* xir[1]*(1-xir[2]) + 705 x[4*ctx->dof+comp]*(1-xir[0])*(1-xir[1])* xir[2] + 706 x[5*ctx->dof+comp]* xir[0]*(1-xir[1])* xir[2] + 707 x[6*ctx->dof+comp]* xir[0]* xir[1]* xir[2] + 708 x[7*ctx->dof+comp]*(1-xir[0])* xir[1]* xir[2]; 709 } 710 ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr); 711 ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 712 ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 713 } 714 ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 715 ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 716 717 ierr = SNESDestroy(&snes);CHKERRQ(ierr); 718 ierr = VecDestroy(&r);CHKERRQ(ierr); 719 ierr = VecDestroy(&ref);CHKERRQ(ierr); 720 ierr = VecDestroy(&real);CHKERRQ(ierr); 721 ierr = MatDestroy(&J);CHKERRQ(ierr); 722 PetscFunctionReturn(0); 723 } 724 725 #undef __FUNCT__ 726 #define __FUNCT__ "DMInterpolationEvaluate" 727 /* 728 Input Parameters: 729 + ctx - The DMInterpolationInfo context 730 . dm - The DM 731 - x - The local vector containing the field to be interpolated 732 733 Output Parameters: 734 . v - The vector containing the interpolated values 735 */ 736 PetscErrorCode DMInterpolationEvaluate(DMInterpolationInfo ctx, DM dm, Vec x, Vec v) 737 { 738 PetscInt dim, coneSize, n; 739 PetscErrorCode ierr; 740 741 PetscFunctionBegin; 742 PetscValidHeaderSpecific(dm, DM_CLASSID, 2); 743 PetscValidHeaderSpecific(x, VEC_CLASSID, 3); 744 PetscValidHeaderSpecific(v, VEC_CLASSID, 4); 745 ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr); 746 if (n != ctx->n*ctx->dof) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid input vector size %d should be %d", n, ctx->n*ctx->dof); 747 if (n) { 748 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 749 ierr = DMPlexGetConeSize(dm, ctx->cells[0], &coneSize);CHKERRQ(ierr); 750 if (dim == 2) { 751 if (coneSize == 3) { 752 ierr = DMInterpolate_Triangle_Private(ctx, dm, x, v);CHKERRQ(ierr); 753 } else if (coneSize == 4) { 754 ierr = DMInterpolate_Quad_Private(ctx, dm, x, v);CHKERRQ(ierr); 755 } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim); 756 } else if (dim == 3) { 757 if (coneSize == 4) { 758 ierr = DMInterpolate_Tetrahedron_Private(ctx, dm, x, v);CHKERRQ(ierr); 759 } else { 760 ierr = DMInterpolate_Hex_Private(ctx, dm, x, v);CHKERRQ(ierr); 761 } 762 } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim); 763 } 764 PetscFunctionReturn(0); 765 } 766 767 #undef __FUNCT__ 768 #define __FUNCT__ "DMInterpolationDestroy" 769 PetscErrorCode DMInterpolationDestroy(DMInterpolationInfo *ctx) 770 { 771 PetscErrorCode ierr; 772 773 PetscFunctionBegin; 774 PetscValidPointer(ctx, 2); 775 ierr = VecDestroy(&(*ctx)->coords);CHKERRQ(ierr); 776 ierr = PetscFree((*ctx)->points);CHKERRQ(ierr); 777 ierr = PetscFree((*ctx)->cells);CHKERRQ(ierr); 778 ierr = PetscFree(*ctx);CHKERRQ(ierr); 779 *ctx = NULL; 780 PetscFunctionReturn(0); 781 } 782 783 #undef __FUNCT__ 784 #define __FUNCT__ "SNESMonitorFields" 785 /*@C 786 SNESMonitorFields - Monitors the residual for each field separately 787 788 Collective on SNES 789 790 Input Parameters: 791 + snes - the SNES context 792 . its - iteration number 793 . fgnorm - 2-norm of residual 794 - dummy - PetscViewer of type ASCII 795 796 Notes: 797 This routine prints the residual norm at each iteration. 798 799 Level: intermediate 800 801 .keywords: SNES, nonlinear, default, monitor, norm 802 .seealso: SNESMonitorSet(), SNESMonitorDefault() 803 @*/ 804 PetscErrorCode SNESMonitorFields(SNES snes, PetscInt its, PetscReal fgnorm, void *dummy) 805 { 806 PetscViewer viewer = (PetscViewer) dummy; 807 Vec res; 808 DM dm; 809 PetscSection s; 810 const PetscScalar *r; 811 PetscReal *lnorms, *norms; 812 PetscInt numFields, f, pStart, pEnd, p; 813 PetscErrorCode ierr; 814 815 PetscFunctionBegin; 816 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 817 ierr = SNESGetFunction(snes, &res, 0, 0);CHKERRQ(ierr); 818 ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr); 819 ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr); 820 ierr = PetscSectionGetNumFields(s, &numFields);CHKERRQ(ierr); 821 ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr); 822 ierr = PetscCalloc2(numFields, &lnorms, numFields, &norms);CHKERRQ(ierr); 823 ierr = VecGetArrayRead(res, &r);CHKERRQ(ierr); 824 for (p = pStart; p < pEnd; ++p) { 825 for (f = 0; f < numFields; ++f) { 826 PetscInt fdof, foff, d; 827 828 ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr); 829 ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr); 830 for (d = 0; d < fdof; ++d) lnorms[f] += PetscRealPart(PetscSqr(r[foff+d])); 831 } 832 } 833 ierr = VecRestoreArrayRead(res, &r);CHKERRQ(ierr); 834 ierr = MPIU_Allreduce(lnorms, norms, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr); 835 ierr = PetscViewerASCIIAddTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr); 836 ierr = PetscViewerASCIIPrintf(viewer, "%3D SNES Function norm %14.12e [", its, (double) fgnorm);CHKERRQ(ierr); 837 for (f = 0; f < numFields; ++f) { 838 if (f > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);} 839 ierr = PetscViewerASCIIPrintf(viewer, "%14.12e", (double) PetscSqrtReal(norms[f]));CHKERRQ(ierr); 840 } 841 ierr = PetscViewerASCIIPrintf(viewer, "]\n");CHKERRQ(ierr); 842 ierr = PetscViewerASCIISubtractTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr); 843 ierr = PetscFree2(lnorms, norms);CHKERRQ(ierr); 844 PetscFunctionReturn(0); 845 } 846 847 /********************* Residual Computation **************************/ 848 849 #undef __FUNCT__ 850 #define __FUNCT__ "DMPlexSNESGetGeometryFEM" 851 /*@ 852 DMPlexSNESGetGeometryFEM - Return precomputed geometric data 853 854 Input Parameter: 855 . dm - The DM 856 857 Output Parameters: 858 . cellgeom - The values precomputed from cell geometry 859 860 Level: developer 861 862 .seealso: DMPlexSNESSetFunctionLocal() 863 @*/ 864 PetscErrorCode DMPlexSNESGetGeometryFEM(DM dm, Vec *cellgeom) 865 { 866 DMSNES dmsnes; 867 PetscObject obj; 868 PetscErrorCode ierr; 869 870 PetscFunctionBegin; 871 PetscValidHeaderSpecific(dm,DM_CLASSID,1); 872 ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr); 873 ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", &obj);CHKERRQ(ierr); 874 if (!obj) { 875 Vec cellgeom; 876 877 ierr = DMPlexComputeGeometryFEM(dm, &cellgeom);CHKERRQ(ierr); 878 ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", (PetscObject) cellgeom);CHKERRQ(ierr); 879 ierr = VecDestroy(&cellgeom);CHKERRQ(ierr); 880 } 881 if (cellgeom) {PetscValidPointer(cellgeom, 3); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", (PetscObject *) cellgeom);CHKERRQ(ierr);} 882 PetscFunctionReturn(0); 883 } 884 885 #undef __FUNCT__ 886 #define __FUNCT__ "DMPlexSNESGetGeometryFVM" 887 /*@ 888 DMPlexSNESGetGeometryFVM - Return precomputed geometric data 889 890 Input Parameter: 891 . dm - The DM 892 893 Output Parameters: 894 + facegeom - The values precomputed from face geometry 895 . cellgeom - The values precomputed from cell geometry 896 - minRadius - The minimum radius over the mesh of an inscribed sphere in a cell 897 898 Level: developer 899 900 .seealso: DMPlexTSSetRHSFunctionLocal() 901 @*/ 902 PetscErrorCode DMPlexSNESGetGeometryFVM(DM dm, Vec *facegeom, Vec *cellgeom, PetscReal *minRadius) 903 { 904 DMSNES dmsnes; 905 PetscObject obj; 906 PetscErrorCode ierr; 907 908 PetscFunctionBegin; 909 PetscValidHeaderSpecific(dm,DM_CLASSID,1); 910 ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr); 911 ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", &obj);CHKERRQ(ierr); 912 if (!obj) { 913 Vec cellgeom, facegeom; 914 915 ierr = DMPlexComputeGeometryFVM(dm, &cellgeom, &facegeom);CHKERRQ(ierr); 916 ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", (PetscObject) facegeom);CHKERRQ(ierr); 917 ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fvm", (PetscObject) cellgeom);CHKERRQ(ierr); 918 ierr = VecDestroy(&facegeom);CHKERRQ(ierr); 919 ierr = VecDestroy(&cellgeom);CHKERRQ(ierr); 920 } 921 if (facegeom) {PetscValidPointer(facegeom, 2); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", (PetscObject *) facegeom);CHKERRQ(ierr);} 922 if (cellgeom) {PetscValidPointer(cellgeom, 3); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fvm", (PetscObject *) cellgeom);CHKERRQ(ierr);} 923 if (minRadius) {ierr = DMPlexGetMinRadius(dm, minRadius);CHKERRQ(ierr);} 924 PetscFunctionReturn(0); 925 } 926 927 #undef __FUNCT__ 928 #define __FUNCT__ "DMPlexSNESGetGradientDM" 929 /*@ 930 DMPlexSNESGetGradientDM - Return gradient data layout 931 932 Input Parameters: 933 + dm - The DM 934 - fv - The PetscFV 935 936 Output Parameter: 937 . dmGrad - The layout for gradient values 938 939 Level: developer 940 941 .seealso: DMPlexSNESGetGeometryFVM() 942 @*/ 943 PetscErrorCode DMPlexSNESGetGradientDM(DM dm, PetscFV fv, DM *dmGrad) 944 { 945 DMSNES dmsnes; 946 PetscObject obj; 947 PetscBool computeGradients; 948 PetscErrorCode ierr; 949 950 PetscFunctionBegin; 951 PetscValidHeaderSpecific(dm,DM_CLASSID,1); 952 PetscValidHeaderSpecific(fv,PETSCFV_CLASSID,2); 953 PetscValidPointer(dmGrad,3); 954 ierr = PetscFVGetComputeGradients(fv, &computeGradients);CHKERRQ(ierr); 955 if (!computeGradients) {*dmGrad = NULL; PetscFunctionReturn(0);} 956 ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr); 957 ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", &obj);CHKERRQ(ierr); 958 if (!obj) { 959 DM dmGrad; 960 Vec faceGeometry, cellGeometry; 961 962 ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometry, &cellGeometry, NULL);CHKERRQ(ierr); 963 ierr = DMPlexComputeGradientFVM(dm, fv, faceGeometry, cellGeometry, &dmGrad);CHKERRQ(ierr); 964 ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", (PetscObject) dmGrad);CHKERRQ(ierr); 965 ierr = DMDestroy(&dmGrad);CHKERRQ(ierr); 966 } 967 ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", (PetscObject *) dmGrad);CHKERRQ(ierr); 968 PetscFunctionReturn(0); 969 } 970 971 #undef __FUNCT__ 972 #define __FUNCT__ "DMPlexGetCellFields" 973 /*@C 974 DMPlexGetCellFields - Retrieve the field values values for a chunk of cells 975 976 Input Parameters: 977 + dm - The DM 978 . cStart - The first cell to include 979 . cEnd - The first cell to exclude 980 . locX - A local vector with the solution fields 981 . locX_t - A local vector with solution field time derivatives, or NULL 982 - locA - A local vector with auxiliary fields, or NULL 983 984 Output Parameters: 985 + u - The field coefficients 986 . u_t - The fields derivative coefficients 987 - a - The auxiliary field coefficients 988 989 Level: developer 990 991 .seealso: DMPlexGetFaceFields() 992 @*/ 993 PetscErrorCode DMPlexGetCellFields(DM dm, PetscInt cStart, PetscInt cEnd, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a) 994 { 995 DM dmAux; 996 PetscSection section, sectionAux; 997 PetscDS prob; 998 PetscInt numCells = cEnd - cStart, totDim, totDimAux, c; 999 PetscErrorCode ierr; 1000 1001 PetscFunctionBegin; 1002 PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1003 PetscValidHeaderSpecific(locX, VEC_CLASSID, 4); 1004 if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);} 1005 if (locA) {PetscValidHeaderSpecific(locA, VEC_CLASSID, 6);} 1006 PetscValidPointer(u, 7); 1007 PetscValidPointer(u_t, 8); 1008 PetscValidPointer(a, 9); 1009 ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1010 ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1011 ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 1012 if (locA) { 1013 PetscDS probAux; 1014 1015 ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 1016 ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 1017 ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 1018 ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 1019 } 1020 ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, u);CHKERRQ(ierr); 1021 if (locX_t) {ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, u_t);CHKERRQ(ierr);} else {*u_t = NULL;} 1022 if (locA) {ierr = DMGetWorkArray(dm, numCells*totDimAux, PETSC_SCALAR, a);CHKERRQ(ierr);} else {*a = NULL;} 1023 for (c = cStart; c < cEnd; ++c) { 1024 PetscScalar *x = NULL, *x_t = NULL, *ul = *u, *ul_t = *u_t, *al = *a; 1025 PetscInt i; 1026 1027 ierr = DMPlexVecGetClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr); 1028 for (i = 0; i < totDim; ++i) ul[(c-cStart)*totDim+i] = x[i]; 1029 ierr = DMPlexVecRestoreClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr); 1030 if (locX_t) { 1031 ierr = DMPlexVecGetClosure(dm, section, locX_t, c, NULL, &x_t);CHKERRQ(ierr); 1032 for (i = 0; i < totDim; ++i) ul_t[(c-cStart)*totDim+i] = x_t[i]; 1033 ierr = DMPlexVecRestoreClosure(dm, section, locX_t, c, NULL, &x_t);CHKERRQ(ierr); 1034 } 1035 if (locA) { 1036 DM dmAuxPlex; 1037 1038 ierr = DMSNESConvertPlex(dmAux, &dmAuxPlex, PETSC_FALSE);CHKERRQ(ierr); 1039 ierr = DMPlexVecGetClosure(dmAuxPlex, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr); 1040 for (i = 0; i < totDimAux; ++i) al[(c-cStart)*totDimAux+i] = x[i]; 1041 ierr = DMPlexVecRestoreClosure(dmAuxPlex, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr); 1042 ierr = DMDestroy(&dmAuxPlex);CHKERRQ(ierr); 1043 } 1044 } 1045 PetscFunctionReturn(0); 1046 } 1047 1048 #undef __FUNCT__ 1049 #define __FUNCT__ "DMPlexRestoreCellFields" 1050 /*@C 1051 DMPlexRestoreCellFields - Restore the field values values for a chunk of cells 1052 1053 Input Parameters: 1054 + dm - The DM 1055 . cStart - The first cell to include 1056 . cEnd - The first cell to exclude 1057 . locX - A local vector with the solution fields 1058 . locX_t - A local vector with solution field time derivatives, or NULL 1059 - locA - A local vector with auxiliary fields, or NULL 1060 1061 Output Parameters: 1062 + u - The field coefficients 1063 . u_t - The fields derivative coefficients 1064 - a - The auxiliary field coefficients 1065 1066 Level: developer 1067 1068 .seealso: DMPlexGetFaceFields() 1069 @*/ 1070 PetscErrorCode DMPlexRestoreCellFields(DM dm, PetscInt cStart, PetscInt cEnd, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a) 1071 { 1072 PetscErrorCode ierr; 1073 1074 PetscFunctionBegin; 1075 ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, u);CHKERRQ(ierr); 1076 if (*u_t) {ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, u_t);CHKERRQ(ierr);} 1077 if (*a) {ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, a);CHKERRQ(ierr);} 1078 PetscFunctionReturn(0); 1079 } 1080 1081 #undef __FUNCT__ 1082 #define __FUNCT__ "DMPlexGetFaceFields" 1083 /*@C 1084 DMPlexGetFaceFields - Retrieve the field values values for a chunk of faces 1085 1086 Input Parameters: 1087 + dm - The DM 1088 . fStart - The first face to include 1089 . fEnd - The first face to exclude 1090 . locX - A local vector with the solution fields 1091 . locX_t - A local vector with solution field time derivatives, or NULL 1092 . faceGeometry - A local vector with face geometry 1093 . cellGeometry - A local vector with cell geometry 1094 - locaGrad - A local vector with field gradients, or NULL 1095 1096 Output Parameters: 1097 + uL - The field values at the left side of the face 1098 - uR - The field values at the right side of the face 1099 1100 Level: developer 1101 1102 .seealso: DMPlexGetCellFields() 1103 @*/ 1104 PetscErrorCode DMPlexGetFaceFields(DM dm, PetscInt fStart, PetscInt fEnd, Vec locX, Vec locX_t, Vec faceGeometry, Vec cellGeometry, Vec locGrad, PetscScalar **uL, PetscScalar **uR) 1105 { 1106 DM dmFace, dmCell, dmGrad = NULL; 1107 PetscSection section; 1108 PetscDS prob; 1109 DMLabel ghostLabel; 1110 const PetscScalar *facegeom, *cellgeom, *x, *lgrad; 1111 PetscBool *isFE; 1112 PetscInt dim, Nf, f, Nc, numFaces = fEnd - fStart, iface, face; 1113 PetscErrorCode ierr; 1114 1115 PetscFunctionBegin; 1116 PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1117 PetscValidHeaderSpecific(locX, VEC_CLASSID, 4); 1118 if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);} 1119 PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 6); 1120 PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 7); 1121 if (locGrad) {PetscValidHeaderSpecific(locGrad, VEC_CLASSID, 8);} 1122 PetscValidPointer(uL, 9); 1123 PetscValidPointer(uR, 10); 1124 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1125 ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1126 ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1127 ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 1128 ierr = PetscDSGetTotalComponents(prob, &Nc);CHKERRQ(ierr); 1129 ierr = PetscMalloc1(Nf, &isFE);CHKERRQ(ierr); 1130 for (f = 0; f < Nf; ++f) { 1131 PetscObject obj; 1132 PetscClassId id; 1133 1134 ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr); 1135 ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1136 if (id == PETSCFE_CLASSID) {isFE[f] = PETSC_TRUE;} 1137 else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;} 1138 else {isFE[f] = PETSC_FALSE;} 1139 } 1140 ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 1141 ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr); 1142 ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 1143 ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 1144 ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 1145 ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 1146 if (locGrad) { 1147 ierr = VecGetDM(locGrad, &dmGrad);CHKERRQ(ierr); 1148 ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr); 1149 } 1150 ierr = DMGetWorkArray(dm, numFaces*Nc, PETSC_SCALAR, uL);CHKERRQ(ierr); 1151 ierr = DMGetWorkArray(dm, numFaces*Nc, PETSC_SCALAR, uR);CHKERRQ(ierr); 1152 /* Right now just eat the extra work for FE (could make a cell loop) */ 1153 for (face = fStart, iface = 0; face < fEnd; ++face) { 1154 const PetscInt *cells; 1155 PetscFVFaceGeom *fg; 1156 PetscFVCellGeom *cgL, *cgR; 1157 PetscScalar *xL, *xR, *gL, *gR; 1158 PetscScalar *uLl = *uL, *uRl = *uR; 1159 PetscInt ghost, nsupp; 1160 1161 ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 1162 ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr); 1163 if (ghost >= 0) continue; 1164 ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 1165 ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 1166 ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr); 1167 ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr); 1168 for (f = 0; f < Nf; ++f) { 1169 PetscInt off; 1170 1171 ierr = PetscDSGetComponentOffset(prob, f, &off);CHKERRQ(ierr); 1172 if (isFE[f]) { 1173 const PetscInt *cone; 1174 PetscInt comp, coneSize, faceLocL, faceLocR, ldof, rdof, d; 1175 1176 xL = xR = NULL; 1177 ierr = DMPlexVecGetClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr); 1178 ierr = DMPlexVecGetClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr); 1179 ierr = DMPlexGetCone(dm, cells[0], &cone);CHKERRQ(ierr); 1180 ierr = DMPlexGetConeSize(dm, cells[0], &coneSize);CHKERRQ(ierr); 1181 for (faceLocL = 0; faceLocL < coneSize; ++faceLocL) if (cone[faceLocL] == face) break; 1182 if (faceLocL == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %d in cone of cell %d", face, cells[0]); 1183 ierr = DMPlexGetCone(dm, cells[1], &cone);CHKERRQ(ierr); 1184 ierr = DMPlexGetConeSize(dm, cells[1], &coneSize);CHKERRQ(ierr); 1185 for (faceLocR = 0; faceLocR < coneSize; ++faceLocR) if (cone[faceLocR] == face) break; 1186 if (faceLocR == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %d in cone of cell %d", face, cells[1]); 1187 /* Check that FEM field has values in the right cell (sometimes its an FV ghost cell) */ 1188 ierr = EvaluateFaceFields(prob, f, faceLocL, xL, &uLl[iface*Nc+off]);CHKERRQ(ierr); 1189 if (rdof == ldof) {ierr = EvaluateFaceFields(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);} 1190 else {ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr); for(d = 0; d < comp; ++d) uRl[iface*Nc+off+d] = uLl[iface*Nc+off+d];} 1191 ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr); 1192 ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr); 1193 } else { 1194 PetscFV fv; 1195 PetscInt numComp, c; 1196 1197 ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fv);CHKERRQ(ierr); 1198 ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr); 1199 if (nsupp > 2) { 1200 for (f = 0; f < Nf; ++f) { 1201 PetscInt off; 1202 1203 ierr = PetscDSGetComponentOffset(prob, f, &off);CHKERRQ(ierr); 1204 ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr); 1205 for (c = 0; c < numComp; ++c) { 1206 uLl[iface*Nc+off+c] = 0.; 1207 uRl[iface*Nc+off+c] = 0.; 1208 } 1209 } 1210 continue; 1211 } 1212 ierr = DMPlexPointLocalFieldRead(dm, cells[0], f, x, &xL);CHKERRQ(ierr); 1213 ierr = DMPlexPointLocalFieldRead(dm, cells[1], f, x, &xR);CHKERRQ(ierr); 1214 if (dmGrad) { 1215 PetscReal dxL[3], dxR[3]; 1216 1217 ierr = DMPlexPointLocalRead(dmGrad, cells[0], lgrad, &gL);CHKERRQ(ierr); 1218 ierr = DMPlexPointLocalRead(dmGrad, cells[1], lgrad, &gR);CHKERRQ(ierr); 1219 DMPlex_WaxpyD_Internal(dim, -1, cgL->centroid, fg->centroid, dxL); 1220 DMPlex_WaxpyD_Internal(dim, -1, cgR->centroid, fg->centroid, dxR); 1221 for (c = 0; c < numComp; ++c) { 1222 uLl[iface*Nc+off+c] = xL[c] + DMPlex_DotD_Internal(dim, &gL[c*dim], dxL); 1223 uRl[iface*Nc+off+c] = xR[c] + DMPlex_DotD_Internal(dim, &gR[c*dim], dxR); 1224 } 1225 } else { 1226 for (c = 0; c < numComp; ++c) { 1227 uLl[iface*Nc+off+c] = xL[c]; 1228 uRl[iface*Nc+off+c] = xR[c]; 1229 } 1230 } 1231 } 1232 } 1233 ++iface; 1234 } 1235 ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr); 1236 ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 1237 ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 1238 if (locGrad) { 1239 ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr); 1240 } 1241 ierr = PetscFree(isFE);CHKERRQ(ierr); 1242 PetscFunctionReturn(0); 1243 } 1244 1245 #undef __FUNCT__ 1246 #define __FUNCT__ "DMPlexRestoreFaceFields" 1247 /*@C 1248 DMPlexRestoreFaceFields - Restore the field values values for a chunk of faces 1249 1250 Input Parameters: 1251 + dm - The DM 1252 . fStart - The first face to include 1253 . fEnd - The first face to exclude 1254 . locX - A local vector with the solution fields 1255 . locX_t - A local vector with solution field time derivatives, or NULL 1256 . faceGeometry - A local vector with face geometry 1257 . cellGeometry - A local vector with cell geometry 1258 - locaGrad - A local vector with field gradients, or NULL 1259 1260 Output Parameters: 1261 + uL - The field values at the left side of the face 1262 - uR - The field values at the right side of the face 1263 1264 Level: developer 1265 1266 .seealso: DMPlexGetFaceFields() 1267 @*/ 1268 PetscErrorCode DMPlexRestoreFaceFields(DM dm, PetscInt fStart, PetscInt fEnd, Vec locX, Vec locX_t, Vec faceGeometry, Vec cellGeometry, Vec locGrad, PetscScalar **uL, PetscScalar **uR) 1269 { 1270 PetscErrorCode ierr; 1271 1272 PetscFunctionBegin; 1273 ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, uL);CHKERRQ(ierr); 1274 ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, uR);CHKERRQ(ierr); 1275 PetscFunctionReturn(0); 1276 } 1277 1278 #undef __FUNCT__ 1279 #define __FUNCT__ "DMPlexGetFaceGeometry" 1280 /*@C 1281 DMPlexGetFaceGeometry - Retrieve the geometric values for a chunk of faces 1282 1283 Input Parameters: 1284 + dm - The DM 1285 . fStart - The first face to include 1286 . fEnd - The first face to exclude 1287 . faceGeometry - A local vector with face geometry 1288 - cellGeometry - A local vector with cell geometry 1289 1290 Output Parameters: 1291 + fgeom - The extract the face centroid and normal 1292 - vol - The cell volume 1293 1294 Level: developer 1295 1296 .seealso: DMPlexGetCellFields() 1297 @*/ 1298 PetscErrorCode DMPlexGetFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscFVFaceGeom **fgeom, PetscReal **vol) 1299 { 1300 DM dmFace, dmCell; 1301 DMLabel ghostLabel; 1302 const PetscScalar *facegeom, *cellgeom; 1303 PetscInt dim, numFaces = fEnd - fStart, iface, face; 1304 PetscErrorCode ierr; 1305 1306 PetscFunctionBegin; 1307 PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1308 PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 4); 1309 PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 5); 1310 PetscValidPointer(fgeom, 6); 1311 PetscValidPointer(vol, 7); 1312 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1313 ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 1314 ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 1315 ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 1316 ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 1317 ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 1318 ierr = PetscMalloc1(numFaces, fgeom);CHKERRQ(ierr); 1319 ierr = DMGetWorkArray(dm, numFaces*2, PETSC_SCALAR, vol);CHKERRQ(ierr); 1320 for (face = fStart, iface = 0; face < fEnd; ++face) { 1321 const PetscInt *cells; 1322 PetscFVFaceGeom *fg; 1323 PetscFVCellGeom *cgL, *cgR; 1324 PetscFVFaceGeom *fgeoml = *fgeom; 1325 PetscReal *voll = *vol; 1326 PetscInt ghost, d; 1327 1328 ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 1329 if (ghost >= 0) continue; 1330 ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 1331 ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 1332 ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr); 1333 ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr); 1334 for (d = 0; d < dim; ++d) { 1335 fgeoml[iface].centroid[d] = fg->centroid[d]; 1336 fgeoml[iface].normal[d] = fg->normal[d]; 1337 } 1338 voll[iface*2+0] = cgL->volume; 1339 voll[iface*2+1] = cgR->volume; 1340 ++iface; 1341 } 1342 ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 1343 ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 1344 PetscFunctionReturn(0); 1345 } 1346 1347 #undef __FUNCT__ 1348 #define __FUNCT__ "DMPlexRestoreFaceGeometry" 1349 /*@C 1350 DMPlexRestoreFaceGeometry - Restore the field values values for a chunk of faces 1351 1352 Input Parameters: 1353 + dm - The DM 1354 . fStart - The first face to include 1355 . fEnd - The first face to exclude 1356 . faceGeometry - A local vector with face geometry 1357 - cellGeometry - A local vector with cell geometry 1358 1359 Output Parameters: 1360 + fgeom - The extract the face centroid and normal 1361 - vol - The cell volume 1362 1363 Level: developer 1364 1365 .seealso: DMPlexGetFaceFields() 1366 @*/ 1367 PetscErrorCode DMPlexRestoreFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscFVFaceGeom **fgeom, PetscReal **vol) 1368 { 1369 PetscErrorCode ierr; 1370 1371 PetscFunctionBegin; 1372 ierr = PetscFree(*fgeom);CHKERRQ(ierr); 1373 ierr = DMRestoreWorkArray(dm, 0, PETSC_REAL, vol);CHKERRQ(ierr); 1374 PetscFunctionReturn(0); 1375 } 1376 1377 #undef __FUNCT__ 1378 #define __FUNCT__ "DMPlexApplyLimiter_Internal" 1379 static PetscErrorCode DMPlexApplyLimiter_Internal (DM dm, DM dmCell, PetscLimiter lim, PetscInt dim, PetscInt totDim, PetscInt cell, PetscInt face, PetscInt fStart, PetscInt fEnd, PetscReal *cellPhi, const PetscScalar *x, 1380 const PetscScalar *cellgeom, const PetscFVCellGeom *cg, const PetscScalar *cx, const PetscScalar *cgrad) 1381 { 1382 const PetscInt *children; 1383 PetscInt numChildren; 1384 PetscErrorCode ierr; 1385 1386 PetscFunctionBegin; 1387 ierr = DMPlexGetTreeChildren(dm,face,&numChildren,&children);CHKERRQ(ierr); 1388 if (numChildren) { 1389 PetscInt c; 1390 1391 for (c = 0; c < numChildren; c++) { 1392 PetscInt childFace = children[c]; 1393 1394 if (childFace >= fStart && childFace < fEnd) { 1395 ierr = DMPlexApplyLimiter_Internal(dm,dmCell,lim,dim,totDim,cell,childFace,fStart,fEnd,cellPhi,x,cellgeom,cg,cx,cgrad);CHKERRQ(ierr); 1396 } 1397 } 1398 } 1399 else { 1400 PetscScalar *ncx; 1401 PetscFVCellGeom *ncg; 1402 const PetscInt *fcells; 1403 PetscInt ncell, d; 1404 PetscReal v[3]; 1405 1406 ierr = DMPlexGetSupport(dm, face, &fcells);CHKERRQ(ierr); 1407 ncell = cell == fcells[0] ? fcells[1] : fcells[0]; 1408 ierr = DMPlexPointLocalRead(dm, ncell, x, &ncx);CHKERRQ(ierr); 1409 ierr = DMPlexPointLocalRead(dmCell, ncell, cellgeom, &ncg);CHKERRQ(ierr); 1410 DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, ncg->centroid, v); 1411 for (d = 0; d < totDim; ++d) { 1412 /* We use the symmetric slope limited form of Berger, Aftosmis, and Murman 2005 */ 1413 PetscReal phi, flim = 0.5 * PetscRealPart(ncx[d] - cx[d]) / DMPlex_DotD_Internal(dim, &cgrad[d*dim], v); 1414 1415 ierr = PetscLimiterLimit(lim, flim, &phi);CHKERRQ(ierr); 1416 cellPhi[d] = PetscMin(cellPhi[d], phi); 1417 } 1418 } 1419 PetscFunctionReturn(0); 1420 } 1421 1422 #undef __FUNCT__ 1423 #define __FUNCT__ "DMPlexReconstructGradients_Internal" 1424 PetscErrorCode DMPlexReconstructGradients_Internal(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, Vec locX, Vec grad) 1425 { 1426 DM dmFace, dmCell, dmGrad; 1427 DMLabel ghostLabel; 1428 PetscDS prob; 1429 PetscFV fvm; 1430 PetscLimiter lim; 1431 const PetscScalar *facegeom, *cellgeom, *x; 1432 PetscScalar *gr; 1433 PetscReal *cellPhi; 1434 PetscInt dim, face, cell, totDim, cStart, cEnd, cEndInterior; 1435 PetscErrorCode ierr; 1436 1437 PetscFunctionBegin; 1438 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1439 ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1440 ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 1441 ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 1442 ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fvm);CHKERRQ(ierr); 1443 ierr = PetscFVGetLimiter(fvm, &lim);CHKERRQ(ierr); 1444 ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 1445 ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 1446 ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 1447 ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 1448 ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr); 1449 ierr = VecGetDM(grad, &dmGrad);CHKERRQ(ierr); 1450 ierr = VecZeroEntries(grad);CHKERRQ(ierr); 1451 ierr = VecGetArray(grad, &gr);CHKERRQ(ierr); 1452 /* Reconstruct gradients */ 1453 for (face = fStart; face < fEnd; ++face) { 1454 const PetscInt *cells; 1455 PetscFVFaceGeom *fg; 1456 PetscScalar *cx[2]; 1457 PetscScalar *cgrad[2]; 1458 PetscBool boundary; 1459 PetscInt ghost, c, pd, d, numChildren, numCells; 1460 1461 ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 1462 ierr = DMIsBoundaryPoint(dm, face, &boundary);CHKERRQ(ierr); 1463 ierr = DMPlexGetTreeChildren(dm, face, &numChildren, NULL);CHKERRQ(ierr); 1464 if (ghost >= 0 || boundary || numChildren) continue; 1465 ierr = DMPlexGetSupportSize(dm, face, &numCells);CHKERRQ(ierr); 1466 if (numCells != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "facet %d has %d support points: expected 2",face,numCells); 1467 ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 1468 ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 1469 for (c = 0; c < 2; ++c) { 1470 ierr = DMPlexPointLocalRead(dm, cells[c], x, &cx[c]);CHKERRQ(ierr); 1471 ierr = DMPlexPointGlobalRef(dmGrad, cells[c], gr, &cgrad[c]);CHKERRQ(ierr); 1472 } 1473 for (pd = 0; pd < totDim; ++pd) { 1474 PetscScalar delta = cx[1][pd] - cx[0][pd]; 1475 1476 for (d = 0; d < dim; ++d) { 1477 if (cgrad[0]) cgrad[0][pd*dim+d] += fg->grad[0][d] * delta; 1478 if (cgrad[1]) cgrad[1][pd*dim+d] -= fg->grad[1][d] * delta; 1479 } 1480 } 1481 } 1482 /* Limit interior gradients (using cell-based loop because it generalizes better to vector limiters) */ 1483 ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1484 ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 1485 cEndInterior = cEndInterior < 0 ? cEnd : cEndInterior; 1486 ierr = DMGetWorkArray(dm, totDim, PETSC_REAL, &cellPhi);CHKERRQ(ierr); 1487 for (cell = dmGrad && lim ? cStart : cEnd; cell < cEndInterior; ++cell) { 1488 const PetscInt *faces; 1489 PetscScalar *cx; 1490 PetscFVCellGeom *cg; 1491 PetscScalar *cgrad; 1492 PetscInt coneSize, f, pd, d; 1493 1494 ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr); 1495 ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr); 1496 ierr = DMPlexPointLocalRead(dm, cell, x, &cx);CHKERRQ(ierr); 1497 ierr = DMPlexPointLocalRead(dmCell, cell, cellgeom, &cg);CHKERRQ(ierr); 1498 ierr = DMPlexPointGlobalRef(dmGrad, cell, gr, &cgrad);CHKERRQ(ierr); 1499 if (!cgrad) continue; /* Unowned overlap cell, we do not compute */ 1500 /* Limiter will be minimum value over all neighbors */ 1501 for (d = 0; d < totDim; ++d) cellPhi[d] = PETSC_MAX_REAL; 1502 for (f = 0; f < coneSize; ++f) { 1503 ierr = DMPlexApplyLimiter_Internal(dm,dmCell,lim,dim,totDim,cell,faces[f],fStart,fEnd,cellPhi,x,cellgeom,cg,cx,cgrad);CHKERRQ(ierr); 1504 } 1505 /* Apply limiter to gradient */ 1506 for (pd = 0; pd < totDim; ++pd) 1507 /* Scalar limiter applied to each component separately */ 1508 for (d = 0; d < dim; ++d) cgrad[pd*dim+d] *= cellPhi[pd]; 1509 } 1510 ierr = DMRestoreWorkArray(dm, totDim, PETSC_REAL, &cellPhi);CHKERRQ(ierr); 1511 ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 1512 ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 1513 ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr); 1514 ierr = VecRestoreArray(grad, &gr);CHKERRQ(ierr); 1515 PetscFunctionReturn(0); 1516 } 1517 1518 #undef __FUNCT__ 1519 #define __FUNCT__ "DMPlexComputeBdResidual_Internal" 1520 PetscErrorCode DMPlexComputeBdResidual_Internal(DM dm, Vec locX, Vec locX_t, Vec locF, void *user) 1521 { 1522 DM_Plex *mesh = (DM_Plex *) dm->data; 1523 PetscSection section; 1524 PetscDS prob; 1525 DMLabel depth; 1526 PetscFECellGeom *cgeom; 1527 PetscScalar *u = NULL, *u_t = NULL, *elemVec = NULL; 1528 PetscInt dim, Nf, f, totDimBd, numBd, bd; 1529 PetscErrorCode ierr; 1530 1531 PetscFunctionBegin; 1532 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1533 ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1534 ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1535 ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 1536 ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr); 1537 ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 1538 ierr = DMGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 1539 for (bd = 0; bd < numBd; ++bd) { 1540 const char *bdLabel; 1541 DMLabel label; 1542 IS pointIS; 1543 const PetscInt *points; 1544 const PetscInt *values; 1545 PetscInt field, numValues, numPoints, p, dep, numFaces; 1546 PetscBool isEssential; 1547 PetscObject obj; 1548 PetscClassId id; 1549 1550 ierr = DMGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr); 1551 ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 1552 ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1553 if ((id != PETSCFE_CLASSID) || isEssential) continue; 1554 if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this"); 1555 ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr); 1556 ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr); 1557 ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr); 1558 ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 1559 for (p = 0, numFaces = 0; p < numPoints; ++p) { 1560 ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 1561 if (dep == dim-1) ++numFaces; 1562 } 1563 ierr = PetscMalloc3(numFaces*totDimBd,&u,numFaces,&cgeom,numFaces*totDimBd,&elemVec);CHKERRQ(ierr); 1564 if (locX_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);} 1565 for (p = 0, f = 0; p < numPoints; ++p) { 1566 const PetscInt point = points[p]; 1567 PetscScalar *x = NULL; 1568 PetscInt i; 1569 1570 ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 1571 if (dep != dim-1) continue; 1572 ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, cgeom[f].v0, cgeom[f].J, cgeom[f].invJ, &cgeom[f].detJ);CHKERRQ(ierr); 1573 ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, cgeom[f].n);CHKERRQ(ierr); 1574 if (cgeom[f].detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", cgeom[f].detJ, point); 1575 /* TODO: Matt, this is wrong if feBd does not match fe: i.e., if the order differs. */ 1576 ierr = DMPlexVecGetClosure(dm, section, locX, point, NULL, &x);CHKERRQ(ierr); 1577 for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i]; 1578 ierr = DMPlexVecRestoreClosure(dm, section, locX, point, NULL, &x);CHKERRQ(ierr); 1579 if (locX_t) { 1580 ierr = DMPlexVecGetClosure(dm, section, locX_t, point, NULL, &x);CHKERRQ(ierr); 1581 for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i]; 1582 ierr = DMPlexVecRestoreClosure(dm, section, locX_t, point, NULL, &x);CHKERRQ(ierr); 1583 } 1584 ++f; 1585 } 1586 for (f = 0; f < Nf; ++f) { 1587 PetscFE fe; 1588 PetscQuadrature q; 1589 PetscInt numQuadPoints, Nb; 1590 /* Conforming batches */ 1591 PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 1592 /* Remainder */ 1593 PetscInt Nr, offset; 1594 1595 ierr = PetscDSGetBdDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 1596 ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 1597 ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 1598 ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 1599 ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 1600 blockSize = Nb*numQuadPoints; 1601 batchSize = numBlocks * blockSize; 1602 ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 1603 numChunks = numFaces / (numBatches*batchSize); 1604 Ne = numChunks*numBatches*batchSize; 1605 Nr = numFaces % (numBatches*batchSize); 1606 offset = numFaces - Nr; 1607 ierr = PetscFEIntegrateBdResidual(fe, prob, f, Ne, cgeom, u, u_t, NULL, NULL, elemVec);CHKERRQ(ierr); 1608 ierr = PetscFEIntegrateBdResidual(fe, prob, f, Nr, &cgeom[offset], &u[offset*totDimBd], u_t ? &u_t[offset*totDimBd] : NULL, NULL, NULL, &elemVec[offset*totDimBd]);CHKERRQ(ierr); 1609 } 1610 for (p = 0, f = 0; p < numPoints; ++p) { 1611 const PetscInt point = points[p]; 1612 1613 ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr); 1614 if (dep != dim-1) continue; 1615 if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDimBd, &elemVec[f*totDimBd]);CHKERRQ(ierr);} 1616 ierr = DMPlexVecSetClosure(dm, NULL, locF, point, &elemVec[f*totDimBd], ADD_ALL_VALUES);CHKERRQ(ierr); 1617 ++f; 1618 } 1619 ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 1620 ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 1621 ierr = PetscFree3(u,cgeom,elemVec);CHKERRQ(ierr); 1622 if (locX_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);} 1623 } 1624 PetscFunctionReturn(0); 1625 } 1626 1627 #undef __FUNCT__ 1628 #define __FUNCT__ "DMPlexReconstructGradientsFVM" 1629 /*@ 1630 DMPlexReconstructGradientsFVM - reconstruct the gradient of a vector using a finite volume method. 1631 1632 Input Parameters: 1633 + dm - the mesh 1634 - locX - the local representation of the vector 1635 1636 Output Parameter: 1637 . grad - the global representation of the gradient 1638 1639 Level: developer 1640 1641 .seealso: DMPlexSNESGetGradientDM() 1642 @*/ 1643 PetscErrorCode DMPlexReconstructGradientsFVM(DM dm, Vec locX, Vec grad) 1644 { 1645 PetscDS prob; 1646 PetscInt Nf, f, fStart, fEnd; 1647 PetscBool useFVM; 1648 PetscFV fvm = NULL; 1649 Vec faceGeometryFVM, cellGeometryFVM; 1650 PetscFVCellGeom *cgeomFVM = NULL; 1651 PetscFVFaceGeom *fgeomFVM = NULL; 1652 DM dmGrad = NULL; 1653 PetscErrorCode ierr; 1654 1655 PetscFunctionBegin; 1656 ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1657 ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 1658 for (f = 0; f < Nf; ++f) { 1659 PetscObject obj; 1660 PetscClassId id; 1661 1662 ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1663 ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1664 if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;} 1665 } 1666 if (!useFVM) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This dm does not have a finite volume discretization"); 1667 ierr = DMPlexSNESGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr); 1668 if (!dmGrad) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This dm's finite volume discretization does not reconstruct gradients"); 1669 ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr); 1670 ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr); 1671 ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr); 1672 ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 1673 ierr = DMPlexReconstructGradients_Internal(dm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr); 1674 PetscFunctionReturn(0); 1675 } 1676 1677 #undef __FUNCT__ 1678 #define __FUNCT__ "DMPlexComputeResidual_Internal" 1679 PetscErrorCode DMPlexComputeResidual_Internal(DM dm, PetscInt cStart, PetscInt cEnd, PetscReal time, Vec locX, Vec locX_t, Vec locF, void *user) 1680 { 1681 DM_Plex *mesh = (DM_Plex *) dm->data; 1682 const char *name = "Residual"; 1683 DM dmAux = NULL; 1684 DM dmGrad = NULL; 1685 DMLabel ghostLabel = NULL; 1686 PetscDS prob = NULL; 1687 PetscDS probAux = NULL; 1688 PetscSection section = NULL; 1689 PetscBool useFEM = PETSC_FALSE; 1690 PetscBool useFVM = PETSC_FALSE; 1691 PetscBool isImplicit = (locX_t || time == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE; 1692 PetscFV fvm = NULL; 1693 PetscFECellGeom *cgeomFEM = NULL; 1694 PetscScalar *cgeomScal; 1695 PetscFVCellGeom *cgeomFVM = NULL; 1696 PetscFVFaceGeom *fgeomFVM = NULL; 1697 Vec locA, cellGeometryFEM = NULL, cellGeometryFVM = NULL, faceGeometryFVM = NULL, grad, locGrad = NULL; 1698 PetscScalar *u, *u_t, *a, *uL, *uR; 1699 PetscInt Nf, f, totDim, totDimAux, numChunks, cellChunkSize, faceChunkSize, chunk, fStart, fEnd; 1700 PetscErrorCode ierr; 1701 1702 PetscFunctionBegin; 1703 ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 1704 /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */ 1705 /* TODO The FVM geometry is over-manipulated. Make the precalc functions return exactly what we need */ 1706 /* FEM+FVM */ 1707 /* 1: Get sizes from dm and dmAux */ 1708 ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1709 ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 1710 ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1711 ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 1712 ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 1713 ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 1714 if (locA) { 1715 ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 1716 ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 1717 ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 1718 } 1719 /* 2: Get geometric data */ 1720 for (f = 0; f < Nf; ++f) { 1721 PetscObject obj; 1722 PetscClassId id; 1723 PetscBool fimp; 1724 1725 ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 1726 if (isImplicit != fimp) continue; 1727 ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1728 ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1729 if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;} 1730 if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;} 1731 } 1732 if (useFEM) { 1733 ierr = DMPlexSNESGetGeometryFEM(dm, &cellGeometryFEM);CHKERRQ(ierr); 1734 ierr = VecGetArray(cellGeometryFEM, &cgeomScal);CHKERRQ(ierr); 1735 if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 1736 DM dmCell; 1737 PetscInt c; 1738 1739 ierr = VecGetDM(cellGeometryFEM,&dmCell);CHKERRQ(ierr); 1740 ierr = PetscMalloc1(cEnd-cStart,&cgeomFEM);CHKERRQ(ierr); 1741 for (c = 0; c < cEnd - cStart; c++) { 1742 PetscScalar *thisgeom; 1743 1744 ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr); 1745 cgeomFEM[c] = *((PetscFECellGeom *) thisgeom); 1746 } 1747 } 1748 else { 1749 cgeomFEM = (PetscFECellGeom *) cgeomScal; 1750 } 1751 } 1752 if (useFVM) { 1753 ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr); 1754 ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr); 1755 ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr); 1756 /* Reconstruct and limit cell gradients */ 1757 ierr = DMPlexSNESGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr); 1758 if (dmGrad) { 1759 ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 1760 ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 1761 ierr = DMPlexReconstructGradients_Internal(dm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr); 1762 /* Communicate gradient values */ 1763 ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr); 1764 ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 1765 ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 1766 ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 1767 } 1768 /* Handle non-essential (e.g. outflow) boundary values */ 1769 ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, time, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr); 1770 } 1771 /* Loop over chunks */ 1772 ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 1773 numChunks = 1; 1774 cellChunkSize = (cEnd - cStart)/numChunks; 1775 faceChunkSize = (fEnd - fStart)/numChunks; 1776 for (chunk = 0; chunk < numChunks; ++chunk) { 1777 PetscScalar *elemVec, *fluxL, *fluxR; 1778 PetscReal *vol; 1779 PetscFVFaceGeom *fgeom; 1780 PetscInt cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, cell; 1781 PetscInt fS = fStart+chunk*faceChunkSize, fE = PetscMin(fS+faceChunkSize, fEnd), numFaces = fE - fS, face; 1782 1783 /* Extract field coefficients */ 1784 if (useFEM) { 1785 ierr = DMPlexGetCellFields(dm, cS, cE, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 1786 ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, &elemVec);CHKERRQ(ierr); 1787 ierr = PetscMemzero(elemVec, numCells*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 1788 } 1789 if (useFVM) { 1790 ierr = DMPlexGetFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &uL, &uR);CHKERRQ(ierr); 1791 ierr = DMPlexGetFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &fgeom, &vol);CHKERRQ(ierr); 1792 ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxL);CHKERRQ(ierr); 1793 ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxR);CHKERRQ(ierr); 1794 ierr = PetscMemzero(fluxL, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 1795 ierr = PetscMemzero(fluxR, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 1796 } 1797 /* TODO We will interlace both our field coefficients (u, u_t, uL, uR, etc.) and our output (elemVec, fL, fR). I think this works */ 1798 /* Loop over fields */ 1799 for (f = 0; f < Nf; ++f) { 1800 PetscObject obj; 1801 PetscClassId id; 1802 PetscBool fimp; 1803 PetscInt numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset; 1804 1805 ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 1806 if (isImplicit != fimp) continue; 1807 ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1808 ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1809 if (id == PETSCFE_CLASSID) { 1810 PetscFE fe = (PetscFE) obj; 1811 PetscQuadrature q; 1812 PetscInt Nq, Nb; 1813 1814 ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 1815 1816 ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 1817 ierr = PetscQuadratureGetData(q, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 1818 ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 1819 blockSize = Nb*Nq; 1820 batchSize = numBlocks * blockSize; 1821 ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 1822 numChunks = numCells / (numBatches*batchSize); 1823 Ne = numChunks*numBatches*batchSize; 1824 Nr = numCells % (numBatches*batchSize); 1825 offset = numCells - Nr; 1826 /* Integrate FE residual to get elemVec (need fields at quadrature points) */ 1827 /* For FV, I think we use a P0 basis and the cell coefficients (for subdivided cells, we can tweak the basis tabulation to be the indicator function) */ 1828 ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, cgeomFEM, u, u_t, probAux, a, elemVec);CHKERRQ(ierr); 1829 ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, &cgeomFEM[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVec[offset*totDim]);CHKERRQ(ierr); 1830 } else if (id == PETSCFV_CLASSID) { 1831 PetscFV fv = (PetscFV) obj; 1832 1833 Ne = numFaces; 1834 Nr = 0; 1835 /* Riemann solve over faces (need fields at face centroids) */ 1836 /* We need to evaluate FE fields at those coordinates */ 1837 ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr); 1838 } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f); 1839 } 1840 if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 1841 ierr = PetscFree(cgeomFEM);CHKERRQ(ierr); 1842 } 1843 else { 1844 cgeomFEM = NULL; 1845 } 1846 if (cellGeometryFEM) {ierr = VecRestoreArray(cellGeometryFEM, &cgeomScal);CHKERRQ(ierr);} 1847 /* Loop over domain */ 1848 if (useFEM) { 1849 /* Add elemVec to locX */ 1850 for (cell = cS; cell < cE; ++cell) { 1851 if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cell*totDim]);CHKERRQ(ierr);} 1852 ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cell*totDim], ADD_ALL_VALUES);CHKERRQ(ierr); 1853 } 1854 } 1855 if (useFVM) { 1856 PetscScalar *fa; 1857 PetscInt iface; 1858 1859 ierr = VecGetArray(locF, &fa);CHKERRQ(ierr); 1860 for (f = 0; f < Nf; ++f) { 1861 PetscFV fv; 1862 PetscObject obj; 1863 PetscClassId id; 1864 PetscInt foff, pdim; 1865 1866 ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1867 ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr); 1868 ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1869 if (id != PETSCFV_CLASSID) continue; 1870 fv = (PetscFV) obj; 1871 ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 1872 /* Accumulate fluxes to cells */ 1873 for (face = fS, iface = 0; face < fE; ++face) { 1874 const PetscInt *cells; 1875 PetscScalar *fL, *fR; 1876 PetscInt ghost, d, nsupp; 1877 1878 ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 1879 ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr); 1880 if (ghost >= 0) continue; 1881 if (nsupp > 2) { /* noop */ 1882 ++iface; 1883 continue; 1884 } 1885 ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 1886 ierr = DMPlexPointGlobalFieldRef(dm, cells[0], f, fa, &fL);CHKERRQ(ierr); 1887 ierr = DMPlexPointGlobalFieldRef(dm, cells[1], f, fa, &fR);CHKERRQ(ierr); 1888 for (d = 0; d < pdim; ++d) { 1889 if (fL) fL[d] -= fluxL[iface*totDim+foff+d]; 1890 if (fR) fR[d] += fluxR[iface*totDim+foff+d]; 1891 } 1892 ++iface; 1893 } 1894 } 1895 ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr); 1896 } 1897 /* Handle time derivative */ 1898 if (locX_t) { 1899 PetscScalar *x_t, *fa; 1900 1901 ierr = VecGetArray(locF, &fa);CHKERRQ(ierr); 1902 ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr); 1903 for (f = 0; f < Nf; ++f) { 1904 PetscFV fv; 1905 PetscObject obj; 1906 PetscClassId id; 1907 PetscInt pdim, d; 1908 1909 ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1910 ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1911 if (id != PETSCFV_CLASSID) continue; 1912 fv = (PetscFV) obj; 1913 ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 1914 for (cell = cS; cell < cE; ++cell) { 1915 PetscScalar *u_t, *r; 1916 1917 ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr); 1918 ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr); 1919 for (d = 0; d < pdim; ++d) r[d] += u_t[d]; 1920 } 1921 } 1922 ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr); 1923 ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr); 1924 } 1925 if (useFEM) { 1926 ierr = DMPlexRestoreCellFields(dm, cS, cE, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 1927 ierr = DMRestoreWorkArray(dm, numCells*totDim, PETSC_SCALAR, &elemVec);CHKERRQ(ierr); 1928 } 1929 if (useFVM) { 1930 ierr = DMPlexRestoreFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &uL, &uR);CHKERRQ(ierr); 1931 ierr = DMPlexRestoreFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &fgeom, &vol);CHKERRQ(ierr); 1932 ierr = DMRestoreWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxL);CHKERRQ(ierr); 1933 ierr = DMRestoreWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxR);CHKERRQ(ierr); 1934 if (dmGrad) {ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);} 1935 } 1936 } 1937 1938 if (useFEM) {ierr = DMPlexComputeBdResidual_Internal(dm, locX, locX_t, locF, user);CHKERRQ(ierr);} 1939 1940 /* FEM */ 1941 /* 1: Get sizes from dm and dmAux */ 1942 /* 2: Get geometric data */ 1943 /* 3: Handle boundary values */ 1944 /* 4: Loop over domain */ 1945 /* Extract coefficients */ 1946 /* Loop over fields */ 1947 /* Set tiling for FE*/ 1948 /* Integrate FE residual to get elemVec */ 1949 /* Loop over subdomain */ 1950 /* Loop over quad points */ 1951 /* Transform coords to real space */ 1952 /* Evaluate field and aux fields at point */ 1953 /* Evaluate residual at point */ 1954 /* Transform residual to real space */ 1955 /* Add residual to elemVec */ 1956 /* Loop over domain */ 1957 /* Add elemVec to locX */ 1958 1959 /* FVM */ 1960 /* Get geometric data */ 1961 /* If using gradients */ 1962 /* Compute gradient data */ 1963 /* Loop over domain faces */ 1964 /* Count computational faces */ 1965 /* Reconstruct cell gradient */ 1966 /* Loop over domain cells */ 1967 /* Limit cell gradients */ 1968 /* Handle boundary values */ 1969 /* Loop over domain faces */ 1970 /* Read out field, centroid, normal, volume for each side of face */ 1971 /* Riemann solve over faces */ 1972 /* Loop over domain faces */ 1973 /* Accumulate fluxes to cells */ 1974 /* TODO Change printFEM to printDisc here */ 1975 if (mesh->printFEM) { 1976 Vec locFbc; 1977 PetscInt pStart, pEnd, p, maxDof; 1978 PetscScalar *zeroes; 1979 1980 ierr = VecDuplicate(locF,&locFbc);CHKERRQ(ierr); 1981 ierr = VecCopy(locF,locFbc);CHKERRQ(ierr); 1982 ierr = PetscSectionGetChart(section,&pStart,&pEnd);CHKERRQ(ierr); 1983 ierr = PetscSectionGetMaxDof(section,&maxDof);CHKERRQ(ierr); 1984 ierr = PetscCalloc1(maxDof,&zeroes);CHKERRQ(ierr); 1985 for (p = pStart; p < pEnd; p++) { 1986 ierr = VecSetValuesSection(locFbc,section,p,zeroes,INSERT_BC_VALUES);CHKERRQ(ierr); 1987 } 1988 ierr = PetscFree(zeroes);CHKERRQ(ierr); 1989 ierr = DMPrintLocalVec(dm, name, mesh->printTol, locFbc);CHKERRQ(ierr); 1990 ierr = VecDestroy(&locFbc);CHKERRQ(ierr); 1991 } 1992 ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 1993 PetscFunctionReturn(0); 1994 } 1995 1996 #undef __FUNCT__ 1997 #define __FUNCT__ "DMPlexComputeResidualFEM_Check_Internal" 1998 static PetscErrorCode DMPlexComputeResidualFEM_Check_Internal(DM dm, Vec X, Vec X_t, Vec F, void *user) 1999 { 2000 DM dmCh, dmAux; 2001 Vec A, cellgeom; 2002 PetscDS prob, probCh, probAux = NULL; 2003 PetscQuadrature q; 2004 PetscSection section, sectionAux; 2005 PetscFECellGeom *cgeom = NULL; 2006 PetscScalar *cgeomScal; 2007 PetscScalar *elemVec, *elemVecCh, *u, *u_t, *a = NULL; 2008 PetscInt dim, Nf, f, numCells, cStart, cEnd, c; 2009 PetscInt totDim, totDimAux, diffCell = 0; 2010 PetscErrorCode ierr; 2011 2012 PetscFunctionBegin; 2013 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 2014 ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 2015 ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 2016 ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 2017 ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 2018 ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 2019 numCells = cEnd - cStart; 2020 ierr = PetscObjectQuery((PetscObject) dm, "dmCh", (PetscObject *) &dmCh);CHKERRQ(ierr); 2021 ierr = DMGetDS(dmCh, &probCh);CHKERRQ(ierr); 2022 ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 2023 ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 2024 if (dmAux) { 2025 ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 2026 ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 2027 ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 2028 } 2029 ierr = VecSet(F, 0.0);CHKERRQ(ierr); 2030 ierr = PetscMalloc3(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim,&elemVec);CHKERRQ(ierr); 2031 ierr = PetscMalloc1(numCells*totDim,&elemVecCh);CHKERRQ(ierr); 2032 if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 2033 ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr); 2034 ierr = VecGetArray(cellgeom, &cgeomScal);CHKERRQ(ierr); 2035 if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 2036 DM dmCell; 2037 2038 ierr = VecGetDM(cellgeom,&dmCell);CHKERRQ(ierr); 2039 ierr = PetscMalloc1(cEnd-cStart,&cgeom);CHKERRQ(ierr); 2040 for (c = 0; c < cEnd - cStart; c++) { 2041 PetscScalar *thisgeom; 2042 2043 ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr); 2044 cgeom[c] = *((PetscFECellGeom *) thisgeom); 2045 } 2046 } 2047 else { 2048 cgeom = (PetscFECellGeom *) cgeomScal; 2049 } 2050 for (c = cStart; c < cEnd; ++c) { 2051 PetscScalar *x = NULL, *x_t = NULL; 2052 PetscInt i; 2053 2054 ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 2055 for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 2056 ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 2057 if (X_t) { 2058 ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 2059 for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i]; 2060 ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 2061 } 2062 if (dmAux) { 2063 DM dmAuxPlex; 2064 2065 ierr = DMSNESConvertPlex(dmAux,&dmAuxPlex, PETSC_FALSE);CHKERRQ(ierr); 2066 ierr = DMPlexVecGetClosure(dmAuxPlex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 2067 for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 2068 ierr = DMPlexVecRestoreClosure(dmAuxPlex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 2069 ierr = DMDestroy(&dmAuxPlex);CHKERRQ(ierr); 2070 } 2071 } 2072 for (f = 0; f < Nf; ++f) { 2073 PetscFE fe, feCh; 2074 PetscInt numQuadPoints, Nb; 2075 /* Conforming batches */ 2076 PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 2077 /* Remainder */ 2078 PetscInt Nr, offset; 2079 2080 ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 2081 ierr = PetscDSGetDiscretization(probCh, f, (PetscObject *) &feCh);CHKERRQ(ierr); 2082 ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 2083 ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 2084 ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 2085 ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 2086 blockSize = Nb*numQuadPoints; 2087 batchSize = numBlocks * blockSize; 2088 ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 2089 numChunks = numCells / (numBatches*batchSize); 2090 Ne = numChunks*numBatches*batchSize; 2091 Nr = numCells % (numBatches*batchSize); 2092 offset = numCells - Nr; 2093 ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, cgeom, u, u_t, probAux, a, elemVec);CHKERRQ(ierr); 2094 ierr = PetscFEIntegrateResidual(feCh, prob, f, Ne, cgeom, u, u_t, probAux, a, elemVecCh);CHKERRQ(ierr); 2095 ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVec[offset*totDim]);CHKERRQ(ierr); 2096 ierr = PetscFEIntegrateResidual(feCh, prob, f, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVecCh[offset*totDim]);CHKERRQ(ierr); 2097 } 2098 for (c = cStart; c < cEnd; ++c) { 2099 PetscBool diff = PETSC_FALSE; 2100 PetscInt d; 2101 2102 for (d = 0; d < totDim; ++d) if (PetscAbsScalar(elemVec[c*totDim+d] - elemVecCh[c*totDim+d]) > 1.0e-7) {diff = PETSC_TRUE;break;} 2103 if (diff) { 2104 ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Different cell %d\n", c);CHKERRQ(ierr); 2105 ierr = DMPrintCellVector(c, "Residual", totDim, &elemVec[c*totDim]);CHKERRQ(ierr); 2106 ierr = DMPrintCellVector(c, "Check Residual", totDim, &elemVecCh[c*totDim]);CHKERRQ(ierr); 2107 ++diffCell; 2108 } 2109 if (diffCell > 9) break; 2110 ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_ALL_VALUES);CHKERRQ(ierr); 2111 } 2112 if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 2113 ierr = PetscFree(cgeom);CHKERRQ(ierr); 2114 } 2115 else { 2116 cgeom = NULL; 2117 } 2118 ierr = VecRestoreArray(cellgeom, &cgeomScal);CHKERRQ(ierr); 2119 ierr = PetscFree3(u,u_t,elemVec);CHKERRQ(ierr); 2120 ierr = PetscFree(elemVecCh);CHKERRQ(ierr); 2121 if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 2122 PetscFunctionReturn(0); 2123 } 2124 2125 #undef __FUNCT__ 2126 #define __FUNCT__ "DMPlexSNESComputeResidualFEM" 2127 /*@ 2128 DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user 2129 2130 Input Parameters: 2131 + dm - The mesh 2132 . X - Local solution 2133 - user - The user context 2134 2135 Output Parameter: 2136 . F - Local output vector 2137 2138 Level: developer 2139 2140 .seealso: DMPlexComputeJacobianActionFEM() 2141 @*/ 2142 PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user) 2143 { 2144 PetscObject check; 2145 PetscInt cStart, cEnd, cEndInterior; 2146 DM plex; 2147 PetscErrorCode ierr; 2148 2149 PetscFunctionBegin; 2150 ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 2151 ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr); 2152 ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 2153 cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 2154 /* The dmCh is used to check two mathematically equivalent discretizations for computational equivalence */ 2155 ierr = PetscObjectQuery((PetscObject) plex, "dmCh", &check);CHKERRQ(ierr); 2156 if (check) {ierr = DMPlexComputeResidualFEM_Check_Internal(plex, X, NULL, F, user);CHKERRQ(ierr);} 2157 else {ierr = DMPlexComputeResidual_Internal(plex, cStart, cEnd, PETSC_MIN_REAL, X, NULL, F, user);CHKERRQ(ierr);} 2158 ierr = DMDestroy(&plex);CHKERRQ(ierr); 2159 PetscFunctionReturn(0); 2160 } 2161 2162 #undef __FUNCT__ 2163 #define __FUNCT__ "DMPlexSNESComputeBoundaryFEM" 2164 /*@ 2165 DMPlexSNESComputeBoundaryFEM - Form the boundary values for the local input X 2166 2167 Input Parameters: 2168 + dm - The mesh 2169 - user - The user context 2170 2171 Output Parameter: 2172 . X - Local solution 2173 2174 Level: developer 2175 2176 .seealso: DMPlexComputeJacobianActionFEM() 2177 @*/ 2178 PetscErrorCode DMPlexSNESComputeBoundaryFEM(DM dm, Vec X, void *user) 2179 { 2180 DM plex; 2181 PetscErrorCode ierr; 2182 2183 PetscFunctionBegin; 2184 ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 2185 ierr = DMPlexInsertBoundaryValues(plex, PETSC_TRUE, X, PETSC_MIN_REAL, NULL, NULL, NULL);CHKERRQ(ierr); 2186 ierr = DMDestroy(&plex);CHKERRQ(ierr); 2187 PetscFunctionReturn(0); 2188 } 2189 2190 #undef __FUNCT__ 2191 #define __FUNCT__ "DMPlexComputeJacobian_Internal" 2192 PetscErrorCode DMPlexComputeJacobian_Internal(DM dm, PetscInt cStart, PetscInt cEnd, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user) 2193 { 2194 DM_Plex *mesh = (DM_Plex *) dm->data; 2195 const char *name = "Jacobian"; 2196 DM dmAux, plex; 2197 DMLabel depth; 2198 Vec A, cellgeom; 2199 PetscDS prob, probAux = NULL; 2200 PetscQuadrature quad; 2201 PetscSection section, globalSection, sectionAux; 2202 PetscFECellGeom *cgeom = NULL; 2203 PetscScalar *cgeomScal; 2204 PetscScalar *elemMat, *elemMatP, *elemMatD, *u, *u_t, *a = NULL; 2205 PetscInt dim, Nf, f, fieldI, fieldJ, numCells, c; 2206 PetscInt totDim, totDimBd, totDimAux, numBd, bd; 2207 PetscBool isShell, hasPrec, hasDyn; 2208 PetscErrorCode ierr; 2209 2210 PetscFunctionBegin; 2211 ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 2212 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 2213 ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 2214 ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr); 2215 ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 2216 ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 2217 ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr); 2218 ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr); 2219 ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr); 2220 hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE; 2221 ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 2222 ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 2223 numCells = cEnd - cStart; 2224 ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 2225 ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 2226 if (dmAux) { 2227 ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr); 2228 ierr = DMGetDefaultSection(plex, §ionAux);CHKERRQ(ierr); 2229 ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 2230 ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 2231 } 2232 ierr = MatZeroEntries(JacP);CHKERRQ(ierr); 2233 ierr = PetscMalloc5(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim*totDim,&elemMat,hasPrec ? numCells*totDim*totDim : 0, &elemMatP,hasDyn ? numCells*totDim*totDim : 0, &elemMatD);CHKERRQ(ierr); 2234 if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 2235 ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr); 2236 ierr = VecGetArray(cellgeom, &cgeomScal);CHKERRQ(ierr); 2237 if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 2238 DM dmCell; 2239 2240 ierr = VecGetDM(cellgeom,&dmCell);CHKERRQ(ierr); 2241 ierr = PetscMalloc1(cEnd-cStart,&cgeom);CHKERRQ(ierr); 2242 for (c = 0; c < cEnd - cStart; c++) { 2243 PetscScalar *thisgeom; 2244 2245 ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr); 2246 cgeom[c] = *((PetscFECellGeom *) thisgeom); 2247 } 2248 } 2249 else { 2250 cgeom = (PetscFECellGeom *) cgeomScal; 2251 } 2252 for (c = cStart; c < cEnd; ++c) { 2253 PetscScalar *x = NULL, *x_t = NULL; 2254 PetscInt i; 2255 2256 ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 2257 for (i = 0; i < totDim; ++i) u[(c-cStart)*totDim+i] = x[i]; 2258 ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 2259 if (X_t) { 2260 ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 2261 for (i = 0; i < totDim; ++i) u_t[(c-cStart)*totDim+i] = x_t[i]; 2262 ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 2263 } 2264 if (dmAux) { 2265 ierr = DMPlexVecGetClosure(plex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 2266 for (i = 0; i < totDimAux; ++i) a[(c-cStart)*totDimAux+i] = x[i]; 2267 ierr = DMPlexVecRestoreClosure(plex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 2268 } 2269 } 2270 ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 2271 if (hasPrec) {ierr = PetscMemzero(elemMatP, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 2272 if (hasDyn) {ierr = PetscMemzero(elemMatD, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 2273 for (fieldI = 0; fieldI < Nf; ++fieldI) { 2274 PetscFE fe; 2275 PetscInt numQuadPoints, Nb; 2276 /* Conforming batches */ 2277 PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 2278 /* Remainder */ 2279 PetscInt Nr, offset; 2280 2281 ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 2282 ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 2283 ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 2284 ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 2285 ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 2286 blockSize = Nb*numQuadPoints; 2287 batchSize = numBlocks * blockSize; 2288 ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 2289 numChunks = numCells / (numBatches*batchSize); 2290 Ne = numChunks*numBatches*batchSize; 2291 Nr = numCells % (numBatches*batchSize); 2292 offset = numCells - Nr; 2293 for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 2294 ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, elemMat);CHKERRQ(ierr); 2295 ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemMat[offset*totDim*totDim]);CHKERRQ(ierr); 2296 if (hasPrec) { 2297 ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, elemMatP);CHKERRQ(ierr); 2298 ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemMatP[offset*totDim*totDim]);CHKERRQ(ierr); 2299 } 2300 if (hasDyn) { 2301 ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, elemMatD);CHKERRQ(ierr); 2302 ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemMatD[offset*totDim*totDim]);CHKERRQ(ierr); 2303 } 2304 } 2305 } 2306 if (hasDyn) { 2307 for (c = 0; c < (cEnd - cStart)*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c]; 2308 } 2309 for (c = cStart; c < cEnd; ++c) { 2310 if (hasPrec) { 2311 if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);} 2312 ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, c, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2313 if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMatP[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);} 2314 ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMatP[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2315 } else { 2316 if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);} 2317 ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2318 } 2319 } 2320 if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 2321 ierr = PetscFree(cgeom);CHKERRQ(ierr); 2322 } 2323 else { 2324 cgeom = NULL; 2325 } 2326 ierr = VecRestoreArray(cellgeom, &cgeomScal);CHKERRQ(ierr); 2327 ierr = PetscFree5(u,u_t,elemMat,elemMatP,elemMatD);CHKERRQ(ierr); 2328 if (dmAux) { 2329 ierr = PetscFree(a);CHKERRQ(ierr); 2330 ierr = DMDestroy(&plex);CHKERRQ(ierr); 2331 } 2332 ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 2333 ierr = DMGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 2334 ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 2335 ierr = DMGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 2336 for (bd = 0; bd < numBd; ++bd) { 2337 const char *bdLabel; 2338 DMLabel label; 2339 IS pointIS; 2340 const PetscInt *points; 2341 const PetscInt *values; 2342 PetscInt field, numValues, numPoints, p, dep, numFaces; 2343 PetscBool isEssential; 2344 PetscObject obj; 2345 PetscClassId id; 2346 2347 ierr = DMGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr); 2348 ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 2349 ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 2350 if ((id != PETSCFE_CLASSID) || isEssential) continue; 2351 if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this"); 2352 ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr); 2353 ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr); 2354 ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr); 2355 ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 2356 for (p = 0, numFaces = 0; p < numPoints; ++p) { 2357 ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 2358 if (dep == dim-1) ++numFaces; 2359 } 2360 ierr = PetscMalloc3(numFaces*totDimBd,&u,numFaces,&cgeom,numFaces*totDimBd*totDimBd,&elemMat);CHKERRQ(ierr); 2361 if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);} 2362 for (p = 0, f = 0; p < numPoints; ++p) { 2363 const PetscInt point = points[p]; 2364 PetscScalar *x = NULL; 2365 PetscInt i; 2366 2367 ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 2368 if (dep != dim-1) continue; 2369 ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, cgeom[f].v0, cgeom[f].J, cgeom[f].invJ, &cgeom[f].detJ);CHKERRQ(ierr); 2370 ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, cgeom[f].n);CHKERRQ(ierr); 2371 if (cgeom[f].detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", cgeom[f].detJ, point); 2372 ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr); 2373 for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i]; 2374 ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr); 2375 if (X_t) { 2376 ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr); 2377 for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i]; 2378 ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr); 2379 } 2380 ++f; 2381 } 2382 ierr = PetscMemzero(elemMat, numFaces*totDimBd*totDimBd * sizeof(PetscScalar));CHKERRQ(ierr); 2383 for (fieldI = 0; fieldI < Nf; ++fieldI) { 2384 PetscFE fe; 2385 PetscInt numQuadPoints, Nb; 2386 /* Conforming batches */ 2387 PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 2388 /* Remainder */ 2389 PetscInt Nr, offset; 2390 2391 ierr = PetscDSGetBdDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 2392 ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 2393 ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 2394 ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 2395 ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 2396 blockSize = Nb*numQuadPoints; 2397 batchSize = numBlocks * blockSize; 2398 ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 2399 numChunks = numFaces / (numBatches*batchSize); 2400 Ne = numChunks*numBatches*batchSize; 2401 Nr = numFaces % (numBatches*batchSize); 2402 offset = numFaces - Nr; 2403 for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 2404 ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Ne, cgeom, u, u_t, NULL, NULL, elemMat);CHKERRQ(ierr); 2405 ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Nr, &cgeom[offset], &u[offset*totDimBd], u_t ? &u_t[offset*totDimBd] : NULL, NULL, NULL, &elemMat[offset*totDimBd*totDimBd]);CHKERRQ(ierr); 2406 } 2407 } 2408 for (p = 0, f = 0; p < numPoints; ++p) { 2409 const PetscInt point = points[p]; 2410 2411 ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr); 2412 if (dep != dim-1) continue; 2413 if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDimBd, totDimBd, &elemMat[f*totDimBd*totDimBd]);CHKERRQ(ierr);} 2414 ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, point, &elemMat[f*totDimBd*totDimBd], ADD_VALUES);CHKERRQ(ierr); 2415 ++f; 2416 } 2417 ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 2418 ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 2419 ierr = PetscFree3(u,cgeom,elemMat);CHKERRQ(ierr); 2420 if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);} 2421 } 2422 if (hasPrec) { 2423 ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2424 ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2425 } 2426 ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2427 ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2428 if (mesh->printFEM) { 2429 ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr); 2430 ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr); 2431 ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 2432 } 2433 ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 2434 ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr); 2435 if (isShell) { 2436 JacActionCtx *jctx; 2437 2438 ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr); 2439 ierr = VecCopy(X, jctx->u);CHKERRQ(ierr); 2440 } 2441 PetscFunctionReturn(0); 2442 } 2443 2444 #undef __FUNCT__ 2445 #define __FUNCT__ "DMPlexSNESComputeJacobianFEM" 2446 /*@ 2447 DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user. 2448 2449 Input Parameters: 2450 + dm - The mesh 2451 . X - Local input vector 2452 - user - The user context 2453 2454 Output Parameter: 2455 . Jac - Jacobian matrix 2456 2457 Note: 2458 The first member of the user context must be an FEMContext. 2459 2460 We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 2461 like a GPU, or vectorize on a multicore machine. 2462 2463 Level: developer 2464 2465 .seealso: FormFunctionLocal() 2466 @*/ 2467 PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user) 2468 { 2469 PetscInt cStart, cEnd, cEndInterior; 2470 DM plex; 2471 PetscErrorCode ierr; 2472 2473 PetscFunctionBegin; 2474 ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 2475 ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr); 2476 ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 2477 cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 2478 ierr = DMPlexComputeJacobian_Internal(plex, cStart, cEnd, 0.0, 0.0, X, NULL, Jac, JacP, user);CHKERRQ(ierr); 2479 ierr = DMDestroy(&plex);CHKERRQ(ierr); 2480 PetscFunctionReturn(0); 2481 } 2482 2483 #undef __FUNCT__ 2484 #define __FUNCT__ "DMPlexSetSNESLocalFEM" 2485 /*@ 2486 DMPlexSetSNESLocalFEM - Use DMPlex's internal FEM routines to compute SNES boundary values, residual, and Jacobian. 2487 2488 Input Parameters: 2489 + dm - The DM object 2490 . boundaryctx - the user context that will be passed to pointwise evaluation of boundary values (see DMAddBoundary()) 2491 . residualctx - the user context that will be passed to pointwise evaluation of finite element residual computations (see PetscDSSetResidual()) 2492 - jacobianctx - the user context that will be passed to pointwise evaluation of finite element Jacobian construction (see PetscDSSetJacobian()) 2493 @*/ 2494 PetscErrorCode DMPlexSetSNESLocalFEM(DM dm, void *boundaryctx, void *residualctx, void *jacobianctx) 2495 { 2496 PetscErrorCode ierr; 2497 2498 PetscFunctionBegin; 2499 ierr = DMSNESSetBoundaryLocal(dm,DMPlexSNESComputeBoundaryFEM,boundaryctx);CHKERRQ(ierr); 2500 ierr = DMSNESSetFunctionLocal(dm,DMPlexSNESComputeResidualFEM,residualctx);CHKERRQ(ierr); 2501 ierr = DMSNESSetJacobianLocal(dm,DMPlexSNESComputeJacobianFEM,jacobianctx);CHKERRQ(ierr); 2502 PetscFunctionReturn(0); 2503 } 2504 2505 #undef __FUNCT__ 2506 #define __FUNCT__ "DMSNESCheckFromOptions_Internal" 2507 PetscErrorCode DMSNESCheckFromOptions_Internal(SNES snes, DM dm, Vec u, Vec sol, PetscErrorCode (**exactFuncs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx), void **ctxs) 2508 { 2509 Mat J, M; 2510 Vec r, b; 2511 MatNullSpace nullSpace; 2512 PetscReal *error, res = 0.0; 2513 PetscInt numFields; 2514 PetscErrorCode ierr; 2515 2516 PetscFunctionBegin; 2517 ierr = VecDuplicate(u, &r);CHKERRQ(ierr); 2518 ierr = DMCreateMatrix(dm, &J);CHKERRQ(ierr); 2519 M = J; 2520 /* TODO Null space for J */ 2521 /* Check discretization error */ 2522 ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr); 2523 ierr = PetscMalloc1(PetscMax(1, numFields), &error);CHKERRQ(ierr); 2524 if (numFields > 1) { 2525 PetscInt f; 2526 2527 ierr = DMComputeL2FieldDiff(dm, 0.0, exactFuncs, ctxs, u, error);CHKERRQ(ierr); 2528 ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: [");CHKERRQ(ierr); 2529 for (f = 0; f < numFields; ++f) { 2530 if (f) {ierr = PetscPrintf(PETSC_COMM_WORLD, ", ");CHKERRQ(ierr);} 2531 if (error[f] >= 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "%g", error[f]);CHKERRQ(ierr);} 2532 else {ierr = PetscPrintf(PETSC_COMM_WORLD, "< 1.0e-11");CHKERRQ(ierr);} 2533 } 2534 ierr = PetscPrintf(PETSC_COMM_WORLD, "]\n");CHKERRQ(ierr); 2535 } else { 2536 ierr = DMComputeL2Diff(dm, 0.0, exactFuncs, ctxs, u, &error[0]);CHKERRQ(ierr); 2537 if (error[0] >= 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: %g\n", error[0]);CHKERRQ(ierr);} 2538 else {ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: < 1.0e-11\n");CHKERRQ(ierr);} 2539 } 2540 ierr = PetscFree(error);CHKERRQ(ierr); 2541 /* Check residual */ 2542 ierr = SNESComputeFunction(snes, u, r);CHKERRQ(ierr); 2543 ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr); 2544 ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Residual: %g\n", res);CHKERRQ(ierr); 2545 ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr); 2546 ierr = PetscObjectSetName((PetscObject) r, "Initial Residual");CHKERRQ(ierr); 2547 ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"res_");CHKERRQ(ierr); 2548 ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr); 2549 /* Check Jacobian */ 2550 ierr = SNESComputeJacobian(snes, u, M, M);CHKERRQ(ierr); 2551 ierr = MatGetNullSpace(J, &nullSpace);CHKERRQ(ierr); 2552 if (nullSpace) { 2553 PetscBool isNull; 2554 ierr = MatNullSpaceTest(nullSpace, J, &isNull);CHKERRQ(ierr); 2555 if (!isNull) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "The null space calculated for the system operator is invalid."); 2556 } 2557 ierr = VecDuplicate(u, &b);CHKERRQ(ierr); 2558 ierr = VecSet(r, 0.0);CHKERRQ(ierr); 2559 ierr = SNESComputeFunction(snes, r, b);CHKERRQ(ierr); 2560 ierr = MatMult(M, u, r);CHKERRQ(ierr); 2561 ierr = VecAXPY(r, 1.0, b);CHKERRQ(ierr); 2562 ierr = VecDestroy(&b);CHKERRQ(ierr); 2563 ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr); 2564 ierr = PetscPrintf(PETSC_COMM_WORLD, "Linear L_2 Residual: %g\n", res);CHKERRQ(ierr); 2565 ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr); 2566 ierr = PetscObjectSetName((PetscObject) r, "Au - b = Au + F(0)");CHKERRQ(ierr); 2567 ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"linear_res_");CHKERRQ(ierr); 2568 ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr); 2569 ierr = VecDestroy(&r);CHKERRQ(ierr); 2570 ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr); 2571 ierr = MatDestroy(&J);CHKERRQ(ierr); 2572 PetscFunctionReturn(0); 2573 } 2574 2575 #undef __FUNCT__ 2576 #define __FUNCT__ "DMSNESCheckFromOptions" 2577 PetscErrorCode DMSNESCheckFromOptions(SNES snes, Vec u, PetscErrorCode (**exactFuncs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar *u, void *ctx), void **ctxs) 2578 { 2579 DM dm; 2580 Vec sol; 2581 PetscBool check; 2582 PetscErrorCode ierr; 2583 2584 PetscFunctionBegin; 2585 ierr = PetscOptionsHasName(((PetscObject)snes)->options,((PetscObject)snes)->prefix, "-dmsnes_check", &check);CHKERRQ(ierr); 2586 if (!check) PetscFunctionReturn(0); 2587 ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr); 2588 ierr = VecDuplicate(u, &sol);CHKERRQ(ierr); 2589 ierr = SNESSetSolution(snes, sol);CHKERRQ(ierr); 2590 ierr = DMSNESCheckFromOptions_Internal(snes, dm, u, sol, exactFuncs, ctxs);CHKERRQ(ierr); 2591 ierr = VecDestroy(&sol);CHKERRQ(ierr); 2592 PetscFunctionReturn(0); 2593 } 2594