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