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