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 = DMGetDefaultSection(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 = DMGetDefaultSection(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 = DMGetDefaultSection(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 = DMGetDefaultSection(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 ISIntersect_Caching(IS is1, IS is2, IS *isect) 1378 { 1379 PetscErrorCode ierr; 1380 1381 PetscFunctionBegin; 1382 *isect = NULL; 1383 if (is2 && is1) { 1384 char composeStr[33] = {0}; 1385 PetscObjectId is2id; 1386 1387 ierr = PetscObjectGetId((PetscObject)is2,&is2id);CHKERRQ(ierr); 1388 ierr = PetscSNPrintf(composeStr,32,"ISIntersect_Caching_%x",is2id);CHKERRQ(ierr); 1389 ierr = PetscObjectQuery((PetscObject) is1, composeStr, (PetscObject *) isect);CHKERRQ(ierr); 1390 if (*isect == NULL) { 1391 ierr = ISIntersect(is1, is2, isect);CHKERRQ(ierr); 1392 ierr = PetscObjectCompose((PetscObject) is1, composeStr, (PetscObject) *isect);CHKERRQ(ierr); 1393 } else { 1394 ierr = PetscObjectReference((PetscObject) *isect);CHKERRQ(ierr); 1395 } 1396 } 1397 PetscFunctionReturn(0); 1398 } 1399 1400 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) 1401 { 1402 DM_Plex *mesh = (DM_Plex *) dm->data; 1403 DM plex = NULL, plexA = NULL; 1404 PetscDS prob, probAux = NULL; 1405 PetscSection section, sectionAux = NULL; 1406 Vec locA = NULL; 1407 PetscFEGeom *fgeom; 1408 PetscScalar *u = NULL, *u_t = NULL, *a = NULL, *elemVec = NULL; 1409 PetscInt v; 1410 PetscInt totDim, totDimAux = 0; 1411 PetscErrorCode ierr; 1412 1413 PetscFunctionBegin; 1414 ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr); 1415 ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1416 ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1417 ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 1418 ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 1419 if (locA) { 1420 DM dmAux; 1421 1422 ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 1423 ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr); 1424 ierr = DMGetDS(plexA, &probAux);CHKERRQ(ierr); 1425 ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 1426 ierr = DMGetDefaultSection(plexA, §ionAux);CHKERRQ(ierr); 1427 } 1428 for (v = 0; v < numValues; ++v) { 1429 PetscBool isAffine; 1430 PetscQuadrature qGeom = NULL; 1431 IS pointIS; 1432 const PetscInt *points; 1433 PetscInt numFaces, face, Nq; 1434 1435 ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr); 1436 if (!pointIS) continue; /* No points with that id on this process */ 1437 { 1438 IS isectIS; 1439 1440 /* TODO: Special cases of ISIntersect where it is quick to check a priori if one is a superset of the other */ 1441 ierr = ISIntersect_Caching(facetIS,pointIS,&isectIS);CHKERRQ(ierr); 1442 ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 1443 pointIS = isectIS; 1444 } 1445 ierr = ISGetLocalSize(pointIS,&numFaces);CHKERRQ(ierr); 1446 ierr = ISGetIndices(pointIS,&points);CHKERRQ(ierr); 1447 ierr = PetscMalloc4(numFaces*totDim, &u, locX_t ? numFaces*totDim : 0, &u_t, numFaces*totDim, &elemVec, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr); 1448 ierr = DMFieldGetFEInvariance(coordField,pointIS,NULL,&isAffine,NULL);CHKERRQ(ierr); 1449 if (isAffine) { 1450 ierr = DMFieldCreateDefaultQuadrature(coordField,pointIS,&qGeom);CHKERRQ(ierr); 1451 } 1452 if (!qGeom) { 1453 PetscFE fe; 1454 1455 ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr); 1456 ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr); 1457 ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr); 1458 } 1459 ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 1460 ierr = DMSNESGetFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr); 1461 for (face = 0; face < numFaces; ++face) { 1462 const PetscInt point = points[face], *support, *cone; 1463 PetscScalar *x = NULL; 1464 PetscInt i, coneSize, faceLoc; 1465 1466 ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr); 1467 ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr); 1468 ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr); 1469 for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break; 1470 if (faceLoc == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %D in cone of support[0] %D", point, support[0]); 1471 fgeom->face[face][0] = faceLoc; 1472 ierr = DMPlexVecGetClosure(dm, section, locX, support[0], NULL, &x);CHKERRQ(ierr); 1473 for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i]; 1474 ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr); 1475 if (locX_t) { 1476 ierr = DMPlexVecGetClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr); 1477 for (i = 0; i < totDim; ++i) u_t[face*totDim+i] = x[i]; 1478 ierr = DMPlexVecRestoreClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr); 1479 } 1480 if (locA) { 1481 PetscInt subp; 1482 ierr = DMPlexGetSubpoint(plexA, support[0], &subp);CHKERRQ(ierr); 1483 ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr); 1484 for (i = 0; i < totDimAux; ++i) a[face*totDimAux+i] = x[i]; 1485 ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr); 1486 } 1487 } 1488 ierr = PetscMemzero(elemVec, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 1489 { 1490 PetscFE fe; 1491 PetscInt Nb; 1492 PetscFEGeom *chunkGeom = NULL; 1493 /* Conforming batches */ 1494 PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 1495 /* Remainder */ 1496 PetscInt Nr, offset; 1497 1498 ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr); 1499 ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 1500 ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 1501 /* TODO: documentation is unclear about what is going on with these numbers: how should Nb / Nq factor in ? */ 1502 blockSize = Nb; 1503 batchSize = numBlocks * blockSize; 1504 ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 1505 numChunks = numFaces / (numBatches*batchSize); 1506 Ne = numChunks*numBatches*batchSize; 1507 Nr = numFaces % (numBatches*batchSize); 1508 offset = numFaces - Nr; 1509 ierr = PetscFEGeomGetChunk(fgeom,0,offset,&chunkGeom);CHKERRQ(ierr); 1510 ierr = PetscFEIntegrateBdResidual(fe, prob, field, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr); 1511 ierr = PetscFEGeomGetChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr); 1512 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); 1513 ierr = PetscFEGeomRestoreChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr); 1514 } 1515 for (face = 0; face < numFaces; ++face) { 1516 const PetscInt point = points[face], *support; 1517 1518 if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDim, &elemVec[face*totDim]);CHKERRQ(ierr);} 1519 ierr = DMPlexGetSupport(plex, point, &support);CHKERRQ(ierr); 1520 ierr = DMPlexVecSetClosure(plex, NULL, locF, support[0], &elemVec[face*totDim], ADD_ALL_VALUES);CHKERRQ(ierr); 1521 } 1522 ierr = DMSNESRestoreFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr); 1523 ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr); 1524 ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 1525 ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 1526 ierr = PetscFree4(u, u_t, elemVec, a);CHKERRQ(ierr); 1527 } 1528 if (plex) {ierr = DMDestroy(&plex);CHKERRQ(ierr);} 1529 if (plexA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);} 1530 PetscFunctionReturn(0); 1531 } 1532 1533 PetscErrorCode DMPlexComputeBdResidual_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user) 1534 { 1535 PetscDS prob; 1536 PetscInt dim, numBd, bd; 1537 DMLabel depthLabel; 1538 DMField coordField = NULL; 1539 IS facetIS; 1540 PetscErrorCode ierr; 1541 1542 PetscFunctionBegin; 1543 ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1544 ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 1545 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1546 ierr = DMLabelGetStratumIS(depthLabel,dim - 1,&facetIS);CHKERRQ(ierr); 1547 ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr); 1548 ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 1549 for (bd = 0; bd < numBd; ++bd) { 1550 DMBoundaryConditionType type; 1551 const char *bdLabel; 1552 DMLabel label; 1553 const PetscInt *values; 1554 PetscInt field, numValues; 1555 PetscObject obj; 1556 PetscClassId id; 1557 1558 ierr = PetscDSGetBoundary(prob, bd, &type, NULL, &bdLabel, &field, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr); 1559 ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr); 1560 ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1561 if ((id != PETSCFE_CLASSID) || (type & DM_BC_ESSENTIAL)) continue; 1562 ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr); 1563 ierr = DMPlexComputeBdResidual_Single_Internal(dm, t, label, numValues, values, field, locX, locX_t, locF, coordField, facetIS);CHKERRQ(ierr); 1564 } 1565 ierr = ISDestroy(&facetIS);CHKERRQ(ierr); 1566 PetscFunctionReturn(0); 1567 } 1568 1569 PetscErrorCode DMPlexComputeResidual_Internal(DM dm, IS cellIS, PetscReal time, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user) 1570 { 1571 DM_Plex *mesh = (DM_Plex *) dm->data; 1572 const char *name = "Residual"; 1573 DM dmAux = NULL; 1574 DM dmGrad = NULL; 1575 DMLabel ghostLabel = NULL; 1576 PetscDS prob = NULL; 1577 PetscDS probAux = NULL; 1578 PetscSection section = NULL; 1579 PetscBool useFEM = PETSC_FALSE; 1580 PetscBool useFVM = PETSC_FALSE; 1581 PetscBool isImplicit = (locX_t || time == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE; 1582 PetscFV fvm = NULL; 1583 PetscFVCellGeom *cgeomFVM = NULL; 1584 PetscFVFaceGeom *fgeomFVM = NULL; 1585 DMField coordField = NULL; 1586 Vec locA, cellGeometryFVM = NULL, faceGeometryFVM = NULL, grad, locGrad = NULL; 1587 PetscScalar *u = NULL, *u_t, *a, *uL, *uR; 1588 IS chunkIS; 1589 const PetscInt *cells; 1590 PetscInt cStart, cEnd, numCells; 1591 PetscInt Nf, f, totDim, totDimAux, numChunks, cellChunkSize, faceChunkSize, chunk, fStart, fEnd; 1592 PetscBool isAffine = PETSC_FALSE; 1593 PetscQuadrature affineQuad = NULL, *quads = NULL; 1594 PetscFEGeom *affineGeom = NULL, **geoms = NULL; 1595 PetscErrorCode ierr; 1596 1597 PetscFunctionBegin; 1598 ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 1599 /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */ 1600 /* TODO The FVM geometry is over-manipulated. Make the precalc functions return exactly what we need */ 1601 /* FEM+FVM */ 1602 /* 1: Get sizes from dm and dmAux */ 1603 ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1604 ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 1605 ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1606 ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 1607 ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 1608 ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 1609 if (locA) { 1610 ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 1611 ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 1612 ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 1613 } 1614 /* 2: Get geometric data */ 1615 for (f = 0; f < Nf; ++f) { 1616 PetscObject obj; 1617 PetscClassId id; 1618 PetscBool fimp; 1619 1620 ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 1621 if (isImplicit != fimp) continue; 1622 ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1623 ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1624 if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;} 1625 if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;} 1626 } 1627 if (useFEM) { 1628 ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 1629 ierr = DMFieldGetFEInvariance(coordField,cellIS,NULL,&isAffine,NULL);CHKERRQ(ierr); 1630 if (isAffine) { 1631 ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr); 1632 if (affineQuad) { 1633 ierr = DMSNESGetFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr); 1634 } 1635 } else { 1636 ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr); 1637 for (f = 0; f < Nf; ++f) { 1638 PetscObject obj; 1639 PetscClassId id; 1640 PetscBool fimp; 1641 1642 ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 1643 if (isImplicit != fimp) continue; 1644 ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1645 ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1646 if (id == PETSCFE_CLASSID) { 1647 PetscFE fe = (PetscFE) obj; 1648 1649 ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr); 1650 ierr = PetscObjectReference((PetscObject)quads[f]);CHKERRQ(ierr); 1651 ierr = DMSNESGetFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr); 1652 } 1653 } 1654 } 1655 } 1656 if (useFVM) { 1657 ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr); 1658 ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr); 1659 ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr); 1660 /* Reconstruct and limit cell gradients */ 1661 ierr = DMPlexSNESGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr); 1662 if (dmGrad) { 1663 ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 1664 ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 1665 ierr = DMPlexReconstructGradients_Internal(dm, fvm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr); 1666 /* Communicate gradient values */ 1667 ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr); 1668 ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 1669 ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 1670 ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 1671 } 1672 /* Handle non-essential (e.g. outflow) boundary values */ 1673 ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, time, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr); 1674 } 1675 /* Loop over chunks */ 1676 ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 1677 ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 1678 if (useFEM) {ierr = ISCreate(PETSC_COMM_SELF, &chunkIS);CHKERRQ(ierr);} 1679 numCells = cEnd - cStart; 1680 numChunks = 1; 1681 cellChunkSize = numCells/numChunks; 1682 faceChunkSize = (fEnd - fStart)/numChunks; 1683 numChunks = PetscMin(1,numCells); 1684 for (chunk = 0; chunk < numChunks; ++chunk) { 1685 PetscScalar *elemVec, *fluxL, *fluxR; 1686 PetscReal *vol; 1687 PetscFVFaceGeom *fgeom; 1688 PetscInt cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c; 1689 PetscInt fS = fStart+chunk*faceChunkSize, fE = PetscMin(fS+faceChunkSize, fEnd), numFaces = 0, face; 1690 1691 /* Extract field coefficients */ 1692 if (useFEM) { 1693 ierr = ISGetPointSubrange(chunkIS, cS, cE, cells);CHKERRQ(ierr); 1694 ierr = DMPlexGetCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 1695 ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr); 1696 ierr = PetscMemzero(elemVec, numCells*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 1697 } 1698 if (useFVM) { 1699 ierr = DMPlexGetFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);CHKERRQ(ierr); 1700 ierr = DMPlexGetFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);CHKERRQ(ierr); 1701 ierr = DMGetWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxL);CHKERRQ(ierr); 1702 ierr = DMGetWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxR);CHKERRQ(ierr); 1703 ierr = PetscMemzero(fluxL, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 1704 ierr = PetscMemzero(fluxR, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 1705 } 1706 /* TODO We will interlace both our field coefficients (u, u_t, uL, uR, etc.) and our output (elemVec, fL, fR). I think this works */ 1707 /* Loop over fields */ 1708 for (f = 0; f < Nf; ++f) { 1709 PetscObject obj; 1710 PetscClassId id; 1711 PetscBool fimp; 1712 PetscInt numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset; 1713 1714 ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 1715 if (isImplicit != fimp) continue; 1716 ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1717 ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1718 if (id == PETSCFE_CLASSID) { 1719 PetscFE fe = (PetscFE) obj; 1720 PetscFEGeom *geom = affineGeom ? affineGeom : geoms[f]; 1721 PetscFEGeom *chunkGeom = NULL; 1722 PetscQuadrature quad = affineQuad ? affineQuad : quads[f]; 1723 PetscInt Nq, Nb; 1724 1725 ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 1726 ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 1727 ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 1728 blockSize = Nb; 1729 batchSize = numBlocks * blockSize; 1730 ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 1731 numChunks = numCells / (numBatches*batchSize); 1732 Ne = numChunks*numBatches*batchSize; 1733 Nr = numCells % (numBatches*batchSize); 1734 offset = numCells - Nr; 1735 /* Integrate FE residual to get elemVec (need fields at quadrature points) */ 1736 /* 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) */ 1737 ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr); 1738 ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr); 1739 ierr = PetscFEGeomGetChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr); 1740 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); 1741 ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr); 1742 } else if (id == PETSCFV_CLASSID) { 1743 PetscFV fv = (PetscFV) obj; 1744 1745 Ne = numFaces; 1746 /* Riemann solve over faces (need fields at face centroids) */ 1747 /* We need to evaluate FE fields at those coordinates */ 1748 ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr); 1749 } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f); 1750 } 1751 /* Loop over domain */ 1752 if (useFEM) { 1753 /* Add elemVec to locX */ 1754 for (c = cS; c < cE; ++c) { 1755 const PetscInt cell = cells ? cells[c] : c; 1756 const PetscInt cind = c - cStart; 1757 1758 if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);CHKERRQ(ierr);} 1759 if (ghostLabel) { 1760 PetscInt ghostVal; 1761 1762 ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr); 1763 if (ghostVal > 0) continue; 1764 } 1765 ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);CHKERRQ(ierr); 1766 } 1767 } 1768 if (useFVM) { 1769 PetscScalar *fa; 1770 PetscInt iface; 1771 1772 ierr = VecGetArray(locF, &fa);CHKERRQ(ierr); 1773 for (f = 0; f < Nf; ++f) { 1774 PetscFV fv; 1775 PetscObject obj; 1776 PetscClassId id; 1777 PetscInt foff, pdim; 1778 1779 ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1780 ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr); 1781 ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1782 if (id != PETSCFV_CLASSID) continue; 1783 fv = (PetscFV) obj; 1784 ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 1785 /* Accumulate fluxes to cells */ 1786 for (face = fS, iface = 0; face < fE; ++face) { 1787 const PetscInt *scells; 1788 PetscScalar *fL = NULL, *fR = NULL; 1789 PetscInt ghost, d, nsupp, nchild; 1790 1791 ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 1792 ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr); 1793 ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr); 1794 if (ghost >= 0 || nsupp > 2 || nchild > 0) continue; 1795 ierr = DMPlexGetSupport(dm, face, &scells);CHKERRQ(ierr); 1796 ierr = DMLabelGetValue(ghostLabel,scells[0],&ghost);CHKERRQ(ierr); 1797 if (ghost <= 0) {ierr = DMPlexPointLocalFieldRef(dm, scells[0], f, fa, &fL);CHKERRQ(ierr);} 1798 ierr = DMLabelGetValue(ghostLabel,scells[1],&ghost);CHKERRQ(ierr); 1799 if (ghost <= 0) {ierr = DMPlexPointLocalFieldRef(dm, scells[1], f, fa, &fR);CHKERRQ(ierr);} 1800 for (d = 0; d < pdim; ++d) { 1801 if (fL) fL[d] -= fluxL[iface*totDim+foff+d]; 1802 if (fR) fR[d] += fluxR[iface*totDim+foff+d]; 1803 } 1804 ++iface; 1805 } 1806 } 1807 ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr); 1808 } 1809 /* Handle time derivative */ 1810 if (locX_t) { 1811 PetscScalar *x_t, *fa; 1812 1813 ierr = VecGetArray(locF, &fa);CHKERRQ(ierr); 1814 ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr); 1815 for (f = 0; f < Nf; ++f) { 1816 PetscFV fv; 1817 PetscObject obj; 1818 PetscClassId id; 1819 PetscInt pdim, d; 1820 1821 ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1822 ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1823 if (id != PETSCFV_CLASSID) continue; 1824 fv = (PetscFV) obj; 1825 ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 1826 for (c = cS; c < cE; ++c) { 1827 const PetscInt cell = cells ? cells[c] : c; 1828 PetscScalar *u_t, *r; 1829 1830 if (ghostLabel) { 1831 PetscInt ghostVal; 1832 1833 ierr = DMLabelGetValue(ghostLabel, cell, &ghostVal);CHKERRQ(ierr); 1834 if (ghostVal > 0) continue; 1835 } 1836 ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr); 1837 ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr); 1838 for (d = 0; d < pdim; ++d) r[d] += u_t[d]; 1839 } 1840 } 1841 ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr); 1842 ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr); 1843 } 1844 if (useFEM) { 1845 ierr = DMPlexRestoreCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 1846 ierr = DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr); 1847 } 1848 if (useFVM) { 1849 ierr = DMPlexRestoreFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);CHKERRQ(ierr); 1850 ierr = DMPlexRestoreFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);CHKERRQ(ierr); 1851 ierr = DMRestoreWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxL);CHKERRQ(ierr); 1852 ierr = DMRestoreWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxR);CHKERRQ(ierr); 1853 if (dmGrad) {ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);} 1854 } 1855 } 1856 if (useFEM) {ierr = ISDestroy(&chunkIS);CHKERRQ(ierr);} 1857 ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 1858 1859 if (useFEM) { 1860 ierr = DMPlexComputeBdResidual_Internal(dm, locX, locX_t, t, locF, user);CHKERRQ(ierr); 1861 1862 if (isAffine) { 1863 ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr); 1864 ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr); 1865 } else { 1866 for (f = 0; f < Nf; ++f) { 1867 ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr); 1868 ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr); 1869 } 1870 ierr = PetscFree2(quads,geoms);CHKERRQ(ierr); 1871 } 1872 } 1873 1874 /* FEM */ 1875 /* 1: Get sizes from dm and dmAux */ 1876 /* 2: Get geometric data */ 1877 /* 3: Handle boundary values */ 1878 /* 4: Loop over domain */ 1879 /* Extract coefficients */ 1880 /* Loop over fields */ 1881 /* Set tiling for FE*/ 1882 /* Integrate FE residual to get elemVec */ 1883 /* Loop over subdomain */ 1884 /* Loop over quad points */ 1885 /* Transform coords to real space */ 1886 /* Evaluate field and aux fields at point */ 1887 /* Evaluate residual at point */ 1888 /* Transform residual to real space */ 1889 /* Add residual to elemVec */ 1890 /* Loop over domain */ 1891 /* Add elemVec to locX */ 1892 1893 /* FVM */ 1894 /* Get geometric data */ 1895 /* If using gradients */ 1896 /* Compute gradient data */ 1897 /* Loop over domain faces */ 1898 /* Count computational faces */ 1899 /* Reconstruct cell gradient */ 1900 /* Loop over domain cells */ 1901 /* Limit cell gradients */ 1902 /* Handle boundary values */ 1903 /* Loop over domain faces */ 1904 /* Read out field, centroid, normal, volume for each side of face */ 1905 /* Riemann solve over faces */ 1906 /* Loop over domain faces */ 1907 /* Accumulate fluxes to cells */ 1908 /* TODO Change printFEM to printDisc here */ 1909 if (mesh->printFEM) { 1910 Vec locFbc; 1911 PetscInt pStart, pEnd, p, maxDof; 1912 PetscScalar *zeroes; 1913 1914 ierr = VecDuplicate(locF,&locFbc);CHKERRQ(ierr); 1915 ierr = VecCopy(locF,locFbc);CHKERRQ(ierr); 1916 ierr = PetscSectionGetChart(section,&pStart,&pEnd);CHKERRQ(ierr); 1917 ierr = PetscSectionGetMaxDof(section,&maxDof);CHKERRQ(ierr); 1918 ierr = PetscCalloc1(maxDof,&zeroes);CHKERRQ(ierr); 1919 for (p = pStart; p < pEnd; p++) { 1920 ierr = VecSetValuesSection(locFbc,section,p,zeroes,INSERT_BC_VALUES);CHKERRQ(ierr); 1921 } 1922 ierr = PetscFree(zeroes);CHKERRQ(ierr); 1923 ierr = DMPrintLocalVec(dm, name, mesh->printTol, locFbc);CHKERRQ(ierr); 1924 ierr = VecDestroy(&locFbc);CHKERRQ(ierr); 1925 } 1926 ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 1927 PetscFunctionReturn(0); 1928 } 1929 1930 static PetscErrorCode DMPlexComputeResidualFEM_Check_Internal(DM dm, Vec X, Vec X_t, PetscReal t, Vec F, void *user) 1931 { 1932 DM dmCh, dmAux; 1933 Vec A; 1934 DMField coordField = NULL; 1935 PetscDS prob, probCh, probAux = NULL; 1936 PetscSection section, sectionAux; 1937 PetscScalar *elemVec, *elemVecCh, *u, *u_t, *a = NULL; 1938 PetscInt Nf, f, numCells, cStart, cEnd, c; 1939 PetscInt totDim, totDimAux = 0, diffCell = 0; 1940 PetscInt depth; 1941 PetscBool isAffine; 1942 IS cellIS; 1943 DMLabel depthLabel; 1944 PetscErrorCode ierr; 1945 1946 PetscFunctionBegin; 1947 ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1948 ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1949 ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 1950 ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 1951 ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1952 numCells = cEnd - cStart; 1953 ierr = PetscObjectQuery((PetscObject) dm, "dmCh", (PetscObject *) &dmCh);CHKERRQ(ierr); 1954 ierr = DMGetDS(dmCh, &probCh);CHKERRQ(ierr); 1955 ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 1956 ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 1957 if (dmAux) { 1958 ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 1959 ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 1960 ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 1961 } 1962 ierr = VecSet(F, 0.0);CHKERRQ(ierr); 1963 ierr = PetscMalloc3(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim,&elemVec);CHKERRQ(ierr); 1964 ierr = PetscMalloc1(numCells*totDim,&elemVecCh);CHKERRQ(ierr); 1965 if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 1966 ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 1967 ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr); 1968 ierr = DMLabelGetStratumIS(depthLabel,depth,&cellIS);CHKERRQ(ierr); 1969 ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 1970 for (c = cStart; c < cEnd; ++c) { 1971 PetscScalar *x = NULL, *x_t = NULL; 1972 PetscInt i; 1973 1974 ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 1975 for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 1976 ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 1977 if (X_t) { 1978 ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 1979 for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i]; 1980 ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 1981 } 1982 if (dmAux) { 1983 DM dmAuxPlex; 1984 1985 ierr = DMSNESConvertPlex(dmAux,&dmAuxPlex, PETSC_FALSE);CHKERRQ(ierr); 1986 ierr = DMPlexVecGetClosure(dmAuxPlex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 1987 for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 1988 ierr = DMPlexVecRestoreClosure(dmAuxPlex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 1989 ierr = DMDestroy(&dmAuxPlex);CHKERRQ(ierr); 1990 } 1991 } 1992 for (f = 0; f < Nf; ++f) { 1993 PetscFE fe, feCh; 1994 PetscInt Nq, Nb; 1995 /* Conforming batches */ 1996 PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 1997 /* Remainder */ 1998 PetscInt Nr, offset; 1999 PetscQuadrature qGeom = NULL; 2000 PetscFEGeom *cgeomFEM, *chunkGeom = NULL; 2001 2002 ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 2003 ierr = PetscDSGetDiscretization(probCh, f, (PetscObject *) &feCh);CHKERRQ(ierr); 2004 ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 2005 ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 2006 ierr = DMFieldGetFEInvariance(coordField,cellIS,NULL,&isAffine,NULL);CHKERRQ(ierr); 2007 if (isAffine) { 2008 ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&qGeom);CHKERRQ(ierr); 2009 } 2010 if (!qGeom) { 2011 ierr = PetscFEGetQuadrature(fe, &qGeom);CHKERRQ(ierr); 2012 ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr); 2013 } 2014 ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 2015 ierr = DMSNESGetFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 2016 blockSize = Nb; 2017 batchSize = numBlocks * blockSize; 2018 ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 2019 numChunks = numCells / (numBatches*batchSize); 2020 Ne = numChunks*numBatches*batchSize; 2021 Nr = numCells % (numBatches*batchSize); 2022 offset = numCells - Nr; 2023 ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr); 2024 ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr); 2025 ierr = PetscFEIntegrateResidual(feCh, prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVecCh);CHKERRQ(ierr); 2026 ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr); 2027 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); 2028 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); 2029 ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr); 2030 ierr = DMSNESRestoreFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 2031 ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr); 2032 } 2033 ierr = ISDestroy(&cellIS);CHKERRQ(ierr); 2034 for (c = cStart; c < cEnd; ++c) { 2035 PetscBool diff = PETSC_FALSE; 2036 PetscInt d; 2037 2038 for (d = 0; d < totDim; ++d) if (PetscAbsScalar(elemVec[c*totDim+d] - elemVecCh[c*totDim+d]) > 1.0e-7) {diff = PETSC_TRUE;break;} 2039 if (diff) { 2040 ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Different cell %d\n", c);CHKERRQ(ierr); 2041 ierr = DMPrintCellVector(c, "Residual", totDim, &elemVec[c*totDim]);CHKERRQ(ierr); 2042 ierr = DMPrintCellVector(c, "Check Residual", totDim, &elemVecCh[c*totDim]);CHKERRQ(ierr); 2043 ++diffCell; 2044 } 2045 if (diffCell > 9) break; 2046 ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_ALL_VALUES);CHKERRQ(ierr); 2047 } 2048 ierr = PetscFree3(u,u_t,elemVec);CHKERRQ(ierr); 2049 ierr = PetscFree(elemVecCh);CHKERRQ(ierr); 2050 if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 2051 PetscFunctionReturn(0); 2052 } 2053 2054 /*@ 2055 DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user 2056 2057 Input Parameters: 2058 + dm - The mesh 2059 . X - Local solution 2060 - user - The user context 2061 2062 Output Parameter: 2063 . F - Local output vector 2064 2065 Level: developer 2066 2067 .seealso: DMPlexComputeJacobianActionFEM() 2068 @*/ 2069 PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user) 2070 { 2071 PetscObject check; 2072 DM plex; 2073 IS cellIS; 2074 PetscInt depth; 2075 PetscErrorCode ierr; 2076 2077 PetscFunctionBegin; 2078 ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 2079 ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr); 2080 ierr = DMGetStratumIS(plex, "dim", depth, &cellIS);CHKERRQ(ierr); 2081 if (!cellIS) { 2082 ierr = DMGetStratumIS(plex, "depth", depth, &cellIS);CHKERRQ(ierr); 2083 } 2084 /* The dmCh is used to check two mathematically equivalent discretizations for computational equivalence */ 2085 ierr = PetscObjectQuery((PetscObject) plex, "dmCh", &check);CHKERRQ(ierr); 2086 if (check) {ierr = DMPlexComputeResidualFEM_Check_Internal(plex, X, NULL, 0.0, F, user);CHKERRQ(ierr);} 2087 else {ierr = DMPlexComputeResidual_Internal(plex, cellIS, PETSC_MIN_REAL, X, NULL, 0.0, F, user);CHKERRQ(ierr);} 2088 ierr = ISDestroy(&cellIS);CHKERRQ(ierr); 2089 ierr = DMDestroy(&plex);CHKERRQ(ierr); 2090 PetscFunctionReturn(0); 2091 } 2092 2093 /*@ 2094 DMPlexSNESComputeBoundaryFEM - Form the boundary values for the local input X 2095 2096 Input Parameters: 2097 + dm - The mesh 2098 - user - The user context 2099 2100 Output Parameter: 2101 . X - Local solution 2102 2103 Level: developer 2104 2105 .seealso: DMPlexComputeJacobianActionFEM() 2106 @*/ 2107 PetscErrorCode DMPlexSNESComputeBoundaryFEM(DM dm, Vec X, void *user) 2108 { 2109 DM plex; 2110 PetscErrorCode ierr; 2111 2112 PetscFunctionBegin; 2113 ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 2114 ierr = DMPlexInsertBoundaryValues(plex, PETSC_TRUE, X, PETSC_MIN_REAL, NULL, NULL, NULL);CHKERRQ(ierr); 2115 ierr = DMDestroy(&plex);CHKERRQ(ierr); 2116 PetscFunctionReturn(0); 2117 } 2118 2119 PetscErrorCode DMPlexComputeBdJacobian_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, PetscReal X_tShift, Mat Jac, Mat JacP, void *user) 2120 { 2121 DM_Plex *mesh = (DM_Plex *) dm->data; 2122 DM dmAux = NULL, plex = NULL; 2123 DMField coordField = NULL; 2124 PetscSection section, globalSection, subSection, sectionAux = NULL; 2125 PetscDS prob, probAux = NULL; 2126 DMLabel depth; 2127 Vec locA = NULL; 2128 PetscScalar *u = NULL, *u_t = NULL, *a = NULL, *elemMat = NULL; 2129 PetscInt dim, totDim, totDimAux, numBd, bd, Nf; 2130 PetscBool isMatISP; 2131 IS facetIS; 2132 PetscErrorCode ierr; 2133 2134 PetscFunctionBegin; 2135 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 2136 ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 2137 ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr); 2138 ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr); 2139 if (isMatISP) { 2140 ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr); 2141 } 2142 ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 2143 ierr = DMLabelGetStratumIS(depth,dim-1,&facetIS);CHKERRQ(ierr); 2144 ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 2145 ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 2146 ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 2147 ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr); 2148 ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 2149 if (locA) { 2150 ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 2151 ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr); 2152 ierr = DMGetDefaultSection(plex, §ionAux);CHKERRQ(ierr); 2153 ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 2154 ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 2155 } 2156 ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 2157 for (bd = 0; bd < numBd; ++bd) { 2158 DMBoundaryConditionType type; 2159 const char *bdLabel; 2160 DMLabel label; 2161 IS pointIS; 2162 const PetscInt *points; 2163 const PetscInt *values; 2164 PetscInt fieldI, fieldJ, numValues, v, numFaces, face, Nq; 2165 PetscObject obj; 2166 PetscBool isAffine; 2167 PetscFE fe; 2168 PetscClassId id; 2169 2170 ierr = PetscDSGetBoundary(prob, bd, &type, NULL, &bdLabel, &fieldI, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr); 2171 ierr = PetscDSGetDiscretization(prob, fieldI, &obj);CHKERRQ(ierr); 2172 ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 2173 if ((id != PETSCFE_CLASSID) || (type & DM_BC_ESSENTIAL)) continue; 2174 fe = (PetscFE) obj; 2175 ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr); 2176 for (v = 0; v < numValues; ++v) { 2177 PetscQuadrature qGeom = NULL; 2178 PetscFEGeom *fgeom; 2179 2180 ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr); 2181 if (!pointIS) continue; /* No points with that id on this process */ 2182 { 2183 IS isectIS; 2184 2185 /* TODO: Special cases of ISIntersect where it is quick to check a prior if one is a superset of the other */ 2186 ierr = ISIntersect_Caching(facetIS,pointIS,&isectIS);CHKERRQ(ierr); 2187 ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 2188 pointIS = isectIS; 2189 } 2190 ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr); 2191 ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 2192 ierr = PetscMalloc3(numFaces*totDim,&u,locX_t ? numFaces*totDim : 0,&u_t,numFaces*totDim*totDim,&elemMat);CHKERRQ(ierr); 2193 if (locA) {ierr = PetscMalloc1(numFaces*totDimAux,&a);CHKERRQ(ierr);} 2194 ierr = DMFieldGetFEInvariance(coordField,pointIS,NULL,&isAffine,NULL);CHKERRQ(ierr); 2195 if (isAffine) { 2196 ierr = DMFieldCreateDefaultQuadrature(coordField,pointIS,&qGeom);CHKERRQ(ierr); 2197 } 2198 if (!qGeom) { 2199 ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr); 2200 ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr); 2201 } 2202 ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 2203 ierr = DMSNESGetFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr); 2204 for (face = 0; face < numFaces; ++face) { 2205 const PetscInt point = points[face], *support, *cone; 2206 PetscScalar *x = NULL; 2207 PetscInt i, coneSize, faceLoc; 2208 2209 ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr); 2210 ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr); 2211 ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr); 2212 for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break; 2213 if (faceLoc == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %d in cone of support[0] %d", point, support[0]); 2214 fgeom->face[face][0] = faceLoc; 2215 ierr = DMPlexVecGetClosure(dm, section, locX, support[0], NULL, &x);CHKERRQ(ierr); 2216 for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i]; 2217 ierr = DMPlexVecRestoreClosure(dm, section, locX, support[0], NULL, &x);CHKERRQ(ierr); 2218 if (locX_t) { 2219 ierr = DMPlexVecGetClosure(dm, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr); 2220 for (i = 0; i < totDim; ++i) u_t[face*totDim+i] = x[i]; 2221 ierr = DMPlexVecRestoreClosure(dm, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr); 2222 } 2223 if (locA) { 2224 ierr = DMPlexVecGetClosure(plex, sectionAux, locA, support[0], NULL, &x);CHKERRQ(ierr); 2225 for (i = 0; i < totDimAux; ++i) a[face*totDimAux+i] = x[i]; 2226 ierr = DMPlexVecRestoreClosure(plex, sectionAux, locA, support[0], NULL, &x);CHKERRQ(ierr); 2227 } 2228 } 2229 ierr = PetscMemzero(elemMat, numFaces*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 2230 { 2231 PetscFE fe; 2232 PetscInt Nb; 2233 /* Conforming batches */ 2234 PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 2235 /* Remainder */ 2236 PetscFEGeom *chunkGeom = NULL; 2237 PetscInt Nr, offset; 2238 2239 ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 2240 ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 2241 ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 2242 blockSize = Nb; 2243 batchSize = numBlocks * blockSize; 2244 ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 2245 numChunks = numFaces / (numBatches*batchSize); 2246 Ne = numChunks*numBatches*batchSize; 2247 Nr = numFaces % (numBatches*batchSize); 2248 offset = numFaces - Nr; 2249 ierr = PetscFEGeomGetChunk(fgeom,0,offset,&chunkGeom);CHKERRQ(ierr); 2250 for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 2251 ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr); 2252 } 2253 ierr = PetscFEGeomGetChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr); 2254 for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 2255 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); 2256 } 2257 ierr = PetscFEGeomRestoreChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr); 2258 } 2259 for (face = 0; face < numFaces; ++face) { 2260 const PetscInt point = points[face], *support; 2261 2262 if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDim, totDim, &elemMat[face*totDim*totDim]);CHKERRQ(ierr);} 2263 ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr); 2264 if (!isMatISP) { 2265 ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, support[0], &elemMat[face*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2266 } else { 2267 Mat lJ; 2268 2269 ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr); 2270 ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, support[0], &elemMat[face*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2271 } 2272 } 2273 ierr = DMSNESRestoreFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr); 2274 ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr); 2275 ierr = PetscFree3(u,u_t,elemMat);CHKERRQ(ierr); 2276 ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 2277 ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 2278 if (locA) {ierr = PetscFree(a);CHKERRQ(ierr);} 2279 } 2280 } 2281 ierr = ISDestroy(&facetIS);CHKERRQ(ierr); 2282 if (plex) {ierr = DMDestroy(&plex);CHKERRQ(ierr);} 2283 PetscFunctionReturn(0); 2284 } 2285 2286 PetscErrorCode DMPlexComputeJacobian_Internal(DM dm, IS cellIS, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user) 2287 { 2288 DM_Plex *mesh = (DM_Plex *) dm->data; 2289 const char *name = "Jacobian"; 2290 DM dmAux, plex; 2291 Vec A; 2292 DMField coordField; 2293 PetscDS prob, probAux = NULL; 2294 PetscSection section, globalSection, subSection, sectionAux; 2295 PetscScalar *elemMat, *elemMatP, *elemMatD, *u, *u_t, *a = NULL; 2296 const PetscInt *cells; 2297 PetscInt Nf, fieldI, fieldJ; 2298 PetscInt totDim, totDimAux, cStart, cEnd, numCells, c; 2299 PetscBool isMatIS, isMatISP, isShell, hasJac, hasPrec, hasDyn, hasFV = PETSC_FALSE; 2300 PetscErrorCode ierr; 2301 2302 PetscFunctionBegin; 2303 ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 2304 ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 2305 ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr); 2306 ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr); 2307 if (isMatISP) { 2308 ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr); 2309 } 2310 ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 2311 ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 2312 ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr); 2313 ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr); 2314 ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr); 2315 hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE; 2316 ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 2317 ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr); 2318 ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 2319 ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 2320 ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 2321 if (dmAux) { 2322 ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr); 2323 ierr = DMGetDefaultSection(plex, §ionAux);CHKERRQ(ierr); 2324 ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 2325 ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 2326 } 2327 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); 2328 if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 2329 ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 2330 for (c = cStart; c < cEnd; ++c) { 2331 const PetscInt cell = cells ? cells[c] : c; 2332 const PetscInt cind = c - cStart; 2333 PetscScalar *x = NULL, *x_t = NULL; 2334 PetscInt i; 2335 2336 ierr = DMPlexVecGetClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr); 2337 for (i = 0; i < totDim; ++i) u[cind*totDim+i] = x[i]; 2338 ierr = DMPlexVecRestoreClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr); 2339 if (X_t) { 2340 ierr = DMPlexVecGetClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr); 2341 for (i = 0; i < totDim; ++i) u_t[cind*totDim+i] = x_t[i]; 2342 ierr = DMPlexVecRestoreClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr); 2343 } 2344 if (dmAux) { 2345 ierr = DMPlexVecGetClosure(plex, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr); 2346 for (i = 0; i < totDimAux; ++i) a[cind*totDimAux+i] = x[i]; 2347 ierr = DMPlexVecRestoreClosure(plex, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr); 2348 } 2349 } 2350 if (hasJac) {ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 2351 if (hasPrec) {ierr = PetscMemzero(elemMatP, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 2352 if (hasDyn) {ierr = PetscMemzero(elemMatD, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 2353 for (fieldI = 0; fieldI < Nf; ++fieldI) { 2354 PetscClassId id; 2355 PetscFE fe; 2356 PetscQuadrature qGeom = NULL; 2357 PetscInt Nb; 2358 /* Conforming batches */ 2359 PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 2360 /* Remainder */ 2361 PetscInt Nr, offset, Nq; 2362 PetscBool isAffine; 2363 PetscFEGeom *cgeomFEM, *chunkGeom = NULL, *remGeom = NULL; 2364 2365 ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 2366 ierr = PetscObjectGetClassId((PetscObject) fe, &id);CHKERRQ(ierr); 2367 if (id == PETSCFV_CLASSID) {hasFV = PETSC_TRUE; continue;} 2368 ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 2369 ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 2370 ierr = DMFieldGetFEInvariance(coordField,cellIS,NULL,&isAffine,NULL);CHKERRQ(ierr); 2371 if (isAffine) { 2372 ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&qGeom);CHKERRQ(ierr); 2373 } 2374 if (!qGeom) { 2375 ierr = PetscFEGetQuadrature(fe,&qGeom);CHKERRQ(ierr); 2376 ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr); 2377 } 2378 ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 2379 ierr = DMSNESGetFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 2380 blockSize = Nb; 2381 batchSize = numBlocks * blockSize; 2382 ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 2383 numChunks = numCells / (numBatches*batchSize); 2384 Ne = numChunks*numBatches*batchSize; 2385 Nr = numCells % (numBatches*batchSize); 2386 offset = numCells - Nr; 2387 ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr); 2388 ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr); 2389 for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 2390 if (hasJac) { 2391 ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr); 2392 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); 2393 } 2394 if (hasPrec) { 2395 ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatP);CHKERRQ(ierr); 2396 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); 2397 } 2398 if (hasDyn) { 2399 ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr); 2400 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); 2401 } 2402 } 2403 ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr); 2404 ierr = PetscFEGeomRestoreChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr); 2405 ierr = DMSNESRestoreFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 2406 ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr); 2407 } 2408 if (hasDyn) {for (c = 0; c < numCells*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];} 2409 if (hasFV) { 2410 PetscClassId id; 2411 PetscFV fv; 2412 PetscInt offsetI, NcI, NbI = 1, fc, f; 2413 2414 for (fieldI = 0; fieldI < Nf; ++fieldI) { 2415 ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fv);CHKERRQ(ierr); 2416 ierr = PetscDSGetFieldOffset(prob, fieldI, &offsetI);CHKERRQ(ierr); 2417 ierr = PetscObjectGetClassId((PetscObject) fv, &id);CHKERRQ(ierr); 2418 if (id != PETSCFV_CLASSID) continue; 2419 /* Put in the identity */ 2420 ierr = PetscFVGetNumComponents(fv, &NcI);CHKERRQ(ierr); 2421 for (c = cStart; c < cEnd; ++c) { 2422 const PetscInt cind = c - cStart; 2423 const PetscInt eOffset = cind*totDim*totDim; 2424 for (fc = 0; fc < NcI; ++fc) { 2425 for (f = 0; f < NbI; ++f) { 2426 const PetscInt i = offsetI + f*NcI+fc; 2427 if (hasPrec) { 2428 if (hasJac) {elemMat[eOffset+i*totDim+i] = 1.0;} 2429 elemMatP[eOffset+i*totDim+i] = 1.0; 2430 } else {elemMat[eOffset+i*totDim+i] = 1.0;} 2431 } 2432 } 2433 } 2434 } 2435 /* No allocated space for FV stuff, so ignore the zero entries */ 2436 ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);CHKERRQ(ierr); 2437 } 2438 isMatIS = PETSC_FALSE; 2439 if (hasPrec && hasJac) { 2440 ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatIS);CHKERRQ(ierr); 2441 } 2442 if (isMatIS && !subSection) { 2443 ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr); 2444 } 2445 for (c = cStart; c < cEnd; ++c) { 2446 const PetscInt cell = cells ? cells[c] : c; 2447 const PetscInt cind = c - cStart; 2448 2449 if (hasPrec) { 2450 if (hasJac) { 2451 if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);} 2452 if (!isMatIS) { 2453 ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2454 } else { 2455 Mat lJ; 2456 2457 ierr = MatISGetLocalMat(Jac,&lJ);CHKERRQ(ierr); 2458 ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2459 } 2460 } 2461 if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMatP[cind*totDim*totDim]);CHKERRQ(ierr);} 2462 if (!isMatISP) { 2463 ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2464 } else { 2465 Mat lJ; 2466 2467 ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr); 2468 ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2469 } 2470 } else { 2471 if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);} 2472 if (!isMatISP) { 2473 ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2474 } else { 2475 Mat lJ; 2476 2477 ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr); 2478 ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2479 } 2480 } 2481 } 2482 ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 2483 if (hasFV) {ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);} 2484 ierr = PetscFree5(u,u_t,elemMat,elemMatP,elemMatD);CHKERRQ(ierr); 2485 if (dmAux) { 2486 ierr = PetscFree(a);CHKERRQ(ierr); 2487 ierr = DMDestroy(&plex);CHKERRQ(ierr); 2488 } 2489 ierr = DMPlexComputeBdJacobian_Internal(dm, X, X_t, t, X_tShift, Jac, JacP, user);CHKERRQ(ierr); 2490 if (hasJac && hasPrec) { 2491 ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2492 ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2493 } 2494 ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2495 ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2496 if (mesh->printFEM) { 2497 ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr); 2498 ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr); 2499 ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 2500 } 2501 ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 2502 ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr); 2503 if (isShell) { 2504 JacActionCtx *jctx; 2505 2506 ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr); 2507 ierr = VecCopy(X, jctx->u);CHKERRQ(ierr); 2508 } 2509 PetscFunctionReturn(0); 2510 } 2511 2512 PetscErrorCode DMPlexComputeJacobianAction_Internal(DM dm, IS cellIS, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Vec Y, Vec Z, void *user) 2513 { 2514 DM_Plex *mesh = (DM_Plex *) dm->data; 2515 const char *name = "Jacobian"; 2516 DM dmAux, plex; 2517 Vec A; 2518 PetscDS prob, probAux = NULL; 2519 PetscQuadrature quad; 2520 PetscSection section, globalSection, sectionAux; 2521 PetscScalar *elemMat, *elemMatD, *u, *u_t, *a = NULL, *y, *z; 2522 PetscInt Nf, fieldI, fieldJ; 2523 PetscInt totDim, totDimAux = 0; 2524 const PetscInt *cells; 2525 PetscInt cStart, cEnd, numCells, c; 2526 PetscBool hasDyn; 2527 DMField coordField; 2528 PetscErrorCode ierr; 2529 2530 PetscFunctionBegin; 2531 ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 2532 ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 2533 ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr); 2534 ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 2535 ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 2536 ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr); 2537 hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE; 2538 ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 2539 ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr); 2540 ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 2541 ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 2542 ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 2543 if (dmAux) { 2544 ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr); 2545 ierr = DMGetDefaultSection(plex, §ionAux);CHKERRQ(ierr); 2546 ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 2547 ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 2548 } 2549 ierr = VecSet(Z, 0.0);CHKERRQ(ierr); 2550 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); 2551 if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 2552 ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 2553 for (c = cStart; c < cEnd; ++c) { 2554 const PetscInt cell = cells ? cells[c] : c; 2555 const PetscInt cind = c - cStart; 2556 PetscScalar *x = NULL, *x_t = NULL; 2557 PetscInt i; 2558 2559 ierr = DMPlexVecGetClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr); 2560 for (i = 0; i < totDim; ++i) u[cind*totDim+i] = x[i]; 2561 ierr = DMPlexVecRestoreClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr); 2562 if (X_t) { 2563 ierr = DMPlexVecGetClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr); 2564 for (i = 0; i < totDim; ++i) u_t[cind*totDim+i] = x_t[i]; 2565 ierr = DMPlexVecRestoreClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr); 2566 } 2567 if (dmAux) { 2568 ierr = DMPlexVecGetClosure(plex, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr); 2569 for (i = 0; i < totDimAux; ++i) a[cind*totDimAux+i] = x[i]; 2570 ierr = DMPlexVecRestoreClosure(plex, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr); 2571 } 2572 ierr = DMPlexVecGetClosure(dm, section, Y, cell, NULL, &x);CHKERRQ(ierr); 2573 for (i = 0; i < totDim; ++i) y[cind*totDim+i] = x[i]; 2574 ierr = DMPlexVecRestoreClosure(dm, section, Y, cell, NULL, &x);CHKERRQ(ierr); 2575 } 2576 ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 2577 if (hasDyn) {ierr = PetscMemzero(elemMatD, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 2578 for (fieldI = 0; fieldI < Nf; ++fieldI) { 2579 PetscFE fe; 2580 PetscInt Nb; 2581 /* Conforming batches */ 2582 PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 2583 /* Remainder */ 2584 PetscInt Nr, offset, Nq; 2585 PetscQuadrature qGeom = NULL; 2586 PetscBool isAffine; 2587 PetscFEGeom *cgeomFEM, *chunkGeom = NULL, *remGeom = NULL; 2588 2589 ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 2590 ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 2591 ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 2592 ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 2593 ierr = DMFieldGetFEInvariance(coordField,cellIS,NULL,&isAffine,NULL);CHKERRQ(ierr); 2594 if (isAffine) { 2595 ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&qGeom);CHKERRQ(ierr); 2596 } 2597 if (!qGeom) { 2598 ierr = PetscFEGetQuadrature(fe,&qGeom);CHKERRQ(ierr); 2599 ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr); 2600 } 2601 ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 2602 ierr = DMSNESGetFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 2603 blockSize = Nb; 2604 batchSize = numBlocks * blockSize; 2605 ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 2606 numChunks = numCells / (numBatches*batchSize); 2607 Ne = numChunks*numBatches*batchSize; 2608 Nr = numCells % (numBatches*batchSize); 2609 offset = numCells - Nr; 2610 ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr); 2611 ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr); 2612 for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 2613 ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr); 2614 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); 2615 if (hasDyn) { 2616 ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr); 2617 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); 2618 } 2619 } 2620 ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr); 2621 ierr = PetscFEGeomRestoreChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr); 2622 ierr = DMSNESRestoreFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 2623 ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr); 2624 } 2625 if (hasDyn) { 2626 for (c = 0; c < numCells*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c]; 2627 } 2628 for (c = cStart; c < cEnd; ++c) { 2629 const PetscInt cell = cells ? cells[c] : c; 2630 const PetscInt cind = c - cStart; 2631 const PetscBLASInt M = totDim, one = 1; 2632 const PetscScalar a = 1.0, b = 0.0; 2633 2634 PetscStackCallBLAS("BLASgemv", BLASgemv_("N", &M, &M, &a, &elemMat[cind*totDim*totDim], &M, &y[cind*totDim], &one, &b, z, &one)); 2635 if (mesh->printFEM > 1) { 2636 ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr); 2637 ierr = DMPrintCellVector(c, "Y", totDim, &y[cind*totDim]);CHKERRQ(ierr); 2638 ierr = DMPrintCellVector(c, "Z", totDim, z);CHKERRQ(ierr); 2639 } 2640 ierr = DMPlexVecSetClosure(dm, section, Z, cell, z, ADD_VALUES);CHKERRQ(ierr); 2641 } 2642 ierr = PetscFree6(u,u_t,elemMat,elemMatD,y,z);CHKERRQ(ierr); 2643 if (dmAux) { 2644 ierr = PetscFree(a);CHKERRQ(ierr); 2645 ierr = DMDestroy(&plex);CHKERRQ(ierr); 2646 } 2647 if (mesh->printFEM) { 2648 ierr = PetscPrintf(PETSC_COMM_WORLD, "Z:\n");CHKERRQ(ierr); 2649 ierr = VecView(Z, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 2650 } 2651 ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 2652 PetscFunctionReturn(0); 2653 } 2654 2655 /*@ 2656 DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user. 2657 2658 Input Parameters: 2659 + dm - The mesh 2660 . X - Local input vector 2661 - user - The user context 2662 2663 Output Parameter: 2664 . Jac - Jacobian matrix 2665 2666 Note: 2667 We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 2668 like a GPU, or vectorize on a multicore machine. 2669 2670 Level: developer 2671 2672 .seealso: FormFunctionLocal() 2673 @*/ 2674 PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user) 2675 { 2676 DM plex; 2677 PetscDS prob; 2678 IS cellIS; 2679 PetscBool hasJac, hasPrec; 2680 PetscInt depth; 2681 PetscErrorCode ierr; 2682 2683 PetscFunctionBegin; 2684 ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 2685 ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr); 2686 ierr = DMGetStratumIS(plex, "dim", depth, &cellIS);CHKERRQ(ierr); 2687 if (!cellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &cellIS);CHKERRQ(ierr);} 2688 ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 2689 ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr); 2690 ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr); 2691 if (hasJac && hasPrec) {ierr = MatZeroEntries(Jac);CHKERRQ(ierr);} 2692 ierr = MatZeroEntries(JacP);CHKERRQ(ierr); 2693 ierr = DMPlexComputeJacobian_Internal(plex, cellIS, 0.0, 0.0, X, NULL, Jac, JacP, user);CHKERRQ(ierr); 2694 ierr = ISDestroy(&cellIS);CHKERRQ(ierr); 2695 ierr = DMDestroy(&plex);CHKERRQ(ierr); 2696 PetscFunctionReturn(0); 2697 } 2698 2699 /*@ 2700 DMPlexSNESComputeJacobianActionFEM - Form the local portion of the Jacobian action Z = J(X) Y at the local solution X using pointwise functions specified by the user. 2701 2702 Input Parameters: 2703 + dm - The mesh 2704 . X - Local solution vector 2705 . Y - Local input vector 2706 - user - The user context 2707 2708 Output Parameter: 2709 . Z - Local output vector 2710 2711 Note: 2712 We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 2713 like a GPU, or vectorize on a multicore machine. 2714 2715 Level: developer 2716 2717 .seealso: FormFunctionLocal() 2718 @*/ 2719 PetscErrorCode DMPlexSNESComputeJacobianActionFEM(DM dm, Vec X, Vec Y, Vec Z, void *user) 2720 { 2721 DM plex; 2722 IS cellIS; 2723 PetscInt depth; 2724 PetscErrorCode ierr; 2725 2726 PetscFunctionBegin; 2727 ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 2728 ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr); 2729 ierr = DMGetStratumIS(plex, "dim", depth, &cellIS);CHKERRQ(ierr); 2730 if (!cellIS) { 2731 ierr = DMGetStratumIS(plex, "depth", depth, &cellIS);CHKERRQ(ierr); 2732 } 2733 ierr = DMPlexComputeJacobianAction_Internal(plex, cellIS, 0.0, 0.0, X, NULL, Y, Z, user);CHKERRQ(ierr); 2734 ierr = ISDestroy(&cellIS);CHKERRQ(ierr); 2735 ierr = DMDestroy(&plex);CHKERRQ(ierr); 2736 PetscFunctionReturn(0); 2737 } 2738 2739 /*@ 2740 DMPlexSetSNESLocalFEM - Use DMPlex's internal FEM routines to compute SNES boundary values, residual, and Jacobian. 2741 2742 Input Parameters: 2743 + dm - The DM object 2744 . boundaryctx - the user context that will be passed to pointwise evaluation of boundary values (see PetscDSAddBoundary()) 2745 . residualctx - the user context that will be passed to pointwise evaluation of finite element residual computations (see PetscDSSetResidual()) 2746 - jacobianctx - the user context that will be passed to pointwise evaluation of finite element Jacobian construction (see PetscDSSetJacobian()) 2747 2748 Level: developer 2749 @*/ 2750 PetscErrorCode DMPlexSetSNESLocalFEM(DM dm, void *boundaryctx, void *residualctx, void *jacobianctx) 2751 { 2752 PetscErrorCode ierr; 2753 2754 PetscFunctionBegin; 2755 ierr = DMSNESSetBoundaryLocal(dm,DMPlexSNESComputeBoundaryFEM,boundaryctx);CHKERRQ(ierr); 2756 ierr = DMSNESSetFunctionLocal(dm,DMPlexSNESComputeResidualFEM,residualctx);CHKERRQ(ierr); 2757 ierr = DMSNESSetJacobianLocal(dm,DMPlexSNESComputeJacobianFEM,jacobianctx);CHKERRQ(ierr); 2758 PetscFunctionReturn(0); 2759 } 2760 2761 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) 2762 { 2763 PetscDS prob; 2764 Mat J, M; 2765 Vec r, b; 2766 MatNullSpace nullSpace; 2767 PetscReal *error, res = 0.0; 2768 PetscInt numFields; 2769 PetscBool hasJac, hasPrec; 2770 PetscErrorCode ierr; 2771 2772 PetscFunctionBegin; 2773 ierr = VecDuplicate(u, &r);CHKERRQ(ierr); 2774 ierr = DMCreateMatrix(dm, &J);CHKERRQ(ierr); 2775 /* TODO Null space for J */ 2776 /* Check discretization error */ 2777 ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr); 2778 ierr = PetscMalloc1(PetscMax(1, numFields), &error);CHKERRQ(ierr); 2779 if (numFields > 1) { 2780 PetscInt f; 2781 2782 ierr = DMComputeL2FieldDiff(dm, 0.0, exactFuncs, ctxs, u, error);CHKERRQ(ierr); 2783 ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: [");CHKERRQ(ierr); 2784 for (f = 0; f < numFields; ++f) { 2785 if (f) {ierr = PetscPrintf(PETSC_COMM_WORLD, ", ");CHKERRQ(ierr);} 2786 if (error[f] >= 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "%g", (double)error[f]);CHKERRQ(ierr);} 2787 else {ierr = PetscPrintf(PETSC_COMM_WORLD, "< 1.0e-11");CHKERRQ(ierr);} 2788 } 2789 ierr = PetscPrintf(PETSC_COMM_WORLD, "]\n");CHKERRQ(ierr); 2790 } else { 2791 ierr = DMComputeL2Diff(dm, 0.0, exactFuncs, ctxs, u, &error[0]);CHKERRQ(ierr); 2792 if (error[0] >= 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: %g\n", (double)error[0]);CHKERRQ(ierr);} 2793 else {ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: < 1.0e-11\n");CHKERRQ(ierr);} 2794 } 2795 ierr = PetscFree(error);CHKERRQ(ierr); 2796 /* Check residual */ 2797 ierr = SNESComputeFunction(snes, u, r);CHKERRQ(ierr); 2798 ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr); 2799 ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Residual: %g\n", (double)res);CHKERRQ(ierr); 2800 ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr); 2801 ierr = PetscObjectSetName((PetscObject) r, "Initial Residual");CHKERRQ(ierr); 2802 ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"res_");CHKERRQ(ierr); 2803 ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr); 2804 /* Check Jacobian */ 2805 ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 2806 ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr); 2807 ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr); 2808 if (hasJac && hasPrec) { 2809 ierr = DMCreateMatrix(dm, &M);CHKERRQ(ierr); 2810 ierr = SNESComputeJacobian(snes, u, J, M);CHKERRQ(ierr); 2811 ierr = PetscObjectSetOptionsPrefix((PetscObject) M, "jacpre_");CHKERRQ(ierr); 2812 ierr = MatViewFromOptions(M, NULL, "-mat_view");CHKERRQ(ierr); 2813 ierr = MatDestroy(&M);CHKERRQ(ierr); 2814 } else { 2815 ierr = SNESComputeJacobian(snes, u, J, J);CHKERRQ(ierr); 2816 } 2817 ierr = PetscObjectSetOptionsPrefix((PetscObject) J, "jac_");CHKERRQ(ierr); 2818 ierr = MatViewFromOptions(J, NULL, "-mat_view");CHKERRQ(ierr); 2819 ierr = MatGetNullSpace(J, &nullSpace);CHKERRQ(ierr); 2820 if (nullSpace) { 2821 PetscBool isNull; 2822 ierr = MatNullSpaceTest(nullSpace, J, &isNull);CHKERRQ(ierr); 2823 if (!isNull) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "The null space calculated for the system operator is invalid."); 2824 } 2825 ierr = VecDuplicate(u, &b);CHKERRQ(ierr); 2826 ierr = VecSet(r, 0.0);CHKERRQ(ierr); 2827 ierr = SNESComputeFunction(snes, r, b);CHKERRQ(ierr); 2828 ierr = MatMult(J, u, r);CHKERRQ(ierr); 2829 ierr = VecAXPY(r, 1.0, b);CHKERRQ(ierr); 2830 ierr = VecDestroy(&b);CHKERRQ(ierr); 2831 ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr); 2832 ierr = PetscPrintf(PETSC_COMM_WORLD, "Linear L_2 Residual: %g\n", (double)res);CHKERRQ(ierr); 2833 ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr); 2834 ierr = PetscObjectSetName((PetscObject) r, "Au - b = Au + F(0)");CHKERRQ(ierr); 2835 ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"linear_res_");CHKERRQ(ierr); 2836 ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr); 2837 ierr = VecDestroy(&r);CHKERRQ(ierr); 2838 ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr); 2839 ierr = MatDestroy(&J);CHKERRQ(ierr); 2840 PetscFunctionReturn(0); 2841 } 2842 2843 PetscErrorCode DMSNESCheckFromOptions(SNES snes, Vec u, PetscErrorCode (**exactFuncs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar *u, void *ctx), void **ctxs) 2844 { 2845 DM dm; 2846 Vec sol; 2847 PetscBool check; 2848 PetscErrorCode ierr; 2849 2850 PetscFunctionBegin; 2851 ierr = PetscOptionsHasName(((PetscObject)snes)->options,((PetscObject)snes)->prefix, "-dmsnes_check", &check);CHKERRQ(ierr); 2852 if (!check) PetscFunctionReturn(0); 2853 ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr); 2854 ierr = VecDuplicate(u, &sol);CHKERRQ(ierr); 2855 ierr = SNESSetSolution(snes, sol);CHKERRQ(ierr); 2856 ierr = DMSNESCheckFromOptions_Internal(snes, dm, u, sol, exactFuncs, ctxs);CHKERRQ(ierr); 2857 ierr = VecDestroy(&sol);CHKERRQ(ierr); 2858 PetscFunctionReturn(0); 2859 } 2860