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