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