1 #include <petsc-private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 2 3 #undef __FUNCT__ 4 #define __FUNCT__ "DMPlexGetScale" 5 PetscErrorCode DMPlexGetScale(DM dm, PetscUnit unit, PetscReal *scale) 6 { 7 DM_Plex *mesh = (DM_Plex*) dm->data; 8 9 PetscFunctionBegin; 10 PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 11 PetscValidPointer(scale, 3); 12 *scale = mesh->scale[unit]; 13 PetscFunctionReturn(0); 14 } 15 16 #undef __FUNCT__ 17 #define __FUNCT__ "DMPlexSetScale" 18 PetscErrorCode DMPlexSetScale(DM dm, PetscUnit unit, PetscReal scale) 19 { 20 DM_Plex *mesh = (DM_Plex*) dm->data; 21 22 PetscFunctionBegin; 23 PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 24 mesh->scale[unit] = scale; 25 PetscFunctionReturn(0); 26 } 27 28 PETSC_STATIC_INLINE PetscInt epsilon(PetscInt i, PetscInt j, PetscInt k) 29 { 30 switch (i) { 31 case 0: 32 switch (j) { 33 case 0: return 0; 34 case 1: 35 switch (k) { 36 case 0: return 0; 37 case 1: return 0; 38 case 2: return 1; 39 } 40 case 2: 41 switch (k) { 42 case 0: return 0; 43 case 1: return -1; 44 case 2: return 0; 45 } 46 } 47 case 1: 48 switch (j) { 49 case 0: 50 switch (k) { 51 case 0: return 0; 52 case 1: return 0; 53 case 2: return -1; 54 } 55 case 1: return 0; 56 case 2: 57 switch (k) { 58 case 0: return 1; 59 case 1: return 0; 60 case 2: return 0; 61 } 62 } 63 case 2: 64 switch (j) { 65 case 0: 66 switch (k) { 67 case 0: return 0; 68 case 1: return 1; 69 case 2: return 0; 70 } 71 case 1: 72 switch (k) { 73 case 0: return -1; 74 case 1: return 0; 75 case 2: return 0; 76 } 77 case 2: return 0; 78 } 79 } 80 return 0; 81 } 82 83 #undef __FUNCT__ 84 #define __FUNCT__ "DMPlexCreateRigidBody" 85 /*@C 86 DMPlexCreateRigidBody - create rigid body modes from coordinates 87 88 Collective on DM 89 90 Input Arguments: 91 + dm - the DM 92 . section - the local section associated with the rigid field, or NULL for the default section 93 - globalSection - the global section associated with the rigid field, or NULL for the default section 94 95 Output Argument: 96 . sp - the null space 97 98 Note: This is necessary to take account of Dirichlet conditions on the displacements 99 100 Level: advanced 101 102 .seealso: MatNullSpaceCreate() 103 @*/ 104 PetscErrorCode DMPlexCreateRigidBody(DM dm, PetscSection section, PetscSection globalSection, MatNullSpace *sp) 105 { 106 MPI_Comm comm; 107 Vec coordinates, localMode, mode[6]; 108 PetscSection coordSection; 109 PetscScalar *coords; 110 PetscInt dim, vStart, vEnd, v, n, m, d, i, j; 111 PetscErrorCode ierr; 112 113 PetscFunctionBegin; 114 ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 115 ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 116 if (dim == 1) { 117 ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr); 118 PetscFunctionReturn(0); 119 } 120 if (!section) {ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr);} 121 if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);} 122 ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr); 123 ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr); 124 ierr = DMPlexGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 125 ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 126 m = (dim*(dim+1))/2; 127 ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr); 128 ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr); 129 ierr = VecSetUp(mode[0]);CHKERRQ(ierr); 130 for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);} 131 /* Assume P1 */ 132 ierr = DMGetLocalVector(dm, &localMode);CHKERRQ(ierr); 133 for (d = 0; d < dim; ++d) { 134 PetscScalar values[3] = {0.0, 0.0, 0.0}; 135 136 values[d] = 1.0; 137 ierr = VecSet(localMode, 0.0);CHKERRQ(ierr); 138 for (v = vStart; v < vEnd; ++v) { 139 ierr = DMPlexVecSetClosure(dm, section, localMode, v, values, INSERT_VALUES);CHKERRQ(ierr); 140 } 141 ierr = DMLocalToGlobalBegin(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 142 ierr = DMLocalToGlobalEnd(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 143 } 144 ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr); 145 for (d = dim; d < dim*(dim+1)/2; ++d) { 146 PetscInt i, j, k = dim > 2 ? d - dim : d; 147 148 ierr = VecSet(localMode, 0.0);CHKERRQ(ierr); 149 for (v = vStart; v < vEnd; ++v) { 150 PetscScalar values[3] = {0.0, 0.0, 0.0}; 151 PetscInt off; 152 153 ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr); 154 for (i = 0; i < dim; ++i) { 155 for (j = 0; j < dim; ++j) { 156 values[j] += epsilon(i, j, k)*PetscRealPart(coords[off+i]); 157 } 158 } 159 ierr = DMPlexVecSetClosure(dm, section, localMode, v, values, INSERT_VALUES);CHKERRQ(ierr); 160 } 161 ierr = DMLocalToGlobalBegin(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 162 ierr = DMLocalToGlobalEnd(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 163 } 164 ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr); 165 ierr = DMRestoreLocalVector(dm, &localMode);CHKERRQ(ierr); 166 for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);} 167 /* Orthonormalize system */ 168 for (i = dim; i < m; ++i) { 169 PetscScalar dots[6]; 170 171 ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr); 172 for (j = 0; j < i; ++j) dots[j] *= -1.0; 173 ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr); 174 ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr); 175 } 176 ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr); 177 for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);} 178 PetscFunctionReturn(0); 179 } 180 /******************************************************************************* 181 This should be in a separate Discretization object, but I am not sure how to lay 182 it out yet, so I am stuffing things here while I experiment. 183 *******************************************************************************/ 184 #undef __FUNCT__ 185 #define __FUNCT__ "DMPlexSetFEMIntegration" 186 PetscErrorCode DMPlexSetFEMIntegration(DM dm, 187 PetscErrorCode (*integrateResidualFEM)(PetscInt, PetscInt, PetscInt, PetscQuadrature[], const PetscScalar[], 188 const PetscReal[], const PetscReal[], const PetscReal[], const PetscReal[], 189 void (*)(const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]), 190 void (*)(const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]), PetscScalar[]), 191 PetscErrorCode (*integrateBdResidualFEM)(PetscInt, PetscInt, PetscInt, PetscQuadrature[], const PetscScalar[], 192 const PetscReal[], const PetscReal[], const PetscReal[], const PetscReal[], const PetscReal[], 193 void (*)(const PetscScalar[], const PetscScalar[], const PetscReal[], const PetscReal[], PetscScalar[]), 194 void (*)(const PetscScalar[], const PetscScalar[], const PetscReal[], const PetscReal[], PetscScalar[]), PetscScalar[]), 195 PetscErrorCode (*integrateJacobianActionFEM)(PetscInt, PetscInt, PetscInt, PetscQuadrature[], const PetscScalar[], const PetscScalar[], 196 const PetscReal[], const PetscReal[], const PetscReal[], const PetscReal[], 197 void (**)(const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]), 198 void (**)(const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]), 199 void (**)(const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]), 200 void (**)(const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]), PetscScalar[]), 201 PetscErrorCode (*integrateJacobianFEM)(PetscInt, PetscInt, PetscInt, PetscInt, PetscQuadrature[], const PetscScalar[], 202 const PetscReal[], const PetscReal[], const PetscReal[], const PetscReal[], 203 void (*)(const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]), 204 void (*)(const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]), 205 void (*)(const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]), 206 void (*)(const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]), PetscScalar[])) 207 { 208 DM_Plex *mesh = (DM_Plex*) dm->data; 209 210 PetscFunctionBegin; 211 PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 212 mesh->integrateResidualFEM = integrateResidualFEM; 213 mesh->integrateBdResidualFEM = integrateBdResidualFEM; 214 mesh->integrateJacobianActionFEM = integrateJacobianActionFEM; 215 mesh->integrateJacobianFEM = integrateJacobianFEM; 216 PetscFunctionReturn(0); 217 } 218 219 #undef __FUNCT__ 220 #define __FUNCT__ "DMPlexProjectFunctionLocal" 221 PetscErrorCode DMPlexProjectFunctionLocal(DM dm, PetscInt numComp, PetscScalar (**funcs)(const PetscReal []), InsertMode mode, Vec localX) 222 { 223 Vec coordinates; 224 PetscSection section, cSection; 225 PetscInt dim, vStart, vEnd, v, c, d; 226 PetscScalar *values, *cArray; 227 PetscReal *coords; 228 PetscErrorCode ierr; 229 230 PetscFunctionBegin; 231 ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr); 232 ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 233 ierr = DMPlexGetCoordinateSection(dm, &cSection);CHKERRQ(ierr); 234 ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 235 ierr = PetscMalloc(numComp * sizeof(PetscScalar), &values);CHKERRQ(ierr); 236 ierr = VecGetArray(coordinates, &cArray);CHKERRQ(ierr); 237 ierr = PetscSectionGetDof(cSection, vStart, &dim);CHKERRQ(ierr); 238 ierr = PetscMalloc(dim * sizeof(PetscReal),&coords);CHKERRQ(ierr); 239 for (v = vStart; v < vEnd; ++v) { 240 PetscInt dof, off; 241 242 ierr = PetscSectionGetDof(cSection, v, &dof);CHKERRQ(ierr); 243 ierr = PetscSectionGetOffset(cSection, v, &off);CHKERRQ(ierr); 244 if (dof > dim) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Cannot have more coordinates %d then dimensions %d", dof, dim); 245 for (d = 0; d < dof; ++d) coords[d] = PetscRealPart(cArray[off+d]); 246 for (c = 0; c < numComp; ++c) values[c] = (*funcs[c])(coords); 247 ierr = VecSetValuesSection(localX, section, v, values, mode);CHKERRQ(ierr); 248 } 249 ierr = VecRestoreArray(coordinates, &cArray);CHKERRQ(ierr); 250 /* Temporary, must be replaced by a projection on the finite element basis */ 251 { 252 PetscInt eStart = 0, eEnd = 0, e, depth; 253 254 ierr = DMPlexGetLabelSize(dm, "depth", &depth);CHKERRQ(ierr); 255 --depth; 256 if (depth > 1) {ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);} 257 for (e = eStart; e < eEnd; ++e) { 258 const PetscInt *cone = NULL; 259 PetscInt coneSize, d; 260 PetscScalar *coordsA, *coordsB; 261 262 ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr); 263 ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr); 264 if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "Cone size %d for point %d should be 2", coneSize, e); 265 ierr = VecGetValuesSection(coordinates, cSection, cone[0], &coordsA);CHKERRQ(ierr); 266 ierr = VecGetValuesSection(coordinates, cSection, cone[1], &coordsB);CHKERRQ(ierr); 267 for (d = 0; d < dim; ++d) { 268 coords[d] = 0.5*(PetscRealPart(coordsA[d]) + PetscRealPart(coordsB[d])); 269 } 270 for (c = 0; c < numComp; ++c) values[c] = (*funcs[c])(coords); 271 ierr = VecSetValuesSection(localX, section, e, values, mode);CHKERRQ(ierr); 272 } 273 } 274 275 ierr = PetscFree(coords);CHKERRQ(ierr); 276 ierr = PetscFree(values);CHKERRQ(ierr); 277 #if 0 278 const PetscInt localDof = this->_mesh->sizeWithBC(s, *cells->begin()); 279 PetscReal detJ; 280 281 ierr = PetscMalloc(localDof * sizeof(PetscScalar), &values);CHKERRQ(ierr); 282 ierr = PetscMalloc2(dim,PetscReal,&v0,dim*dim,PetscReal,&J);CHKERRQ(ierr); 283 ALE::ISieveVisitor::PointRetriever<PETSC_MESH_TYPE::sieve_type> pV(PetscPowInt(this->_mesh->getSieve()->getMaxConeSize(),dim+1), true); 284 285 for (PetscInt c = cStart; c < cEnd; ++c) { 286 ALE::ISieveTraversal<PETSC_MESH_TYPE::sieve_type>::orientedClosure(*this->_mesh->getSieve(), c, pV); 287 const PETSC_MESH_TYPE::point_type *oPoints = pV.getPoints(); 288 const int oSize = pV.getSize(); 289 int v = 0; 290 291 ierr = DMPlexComputeCellGeometry(dm, c, v0, J, NULL, &detJ);CHKERRQ(ierr); 292 for (PetscInt cl = 0; cl < oSize; ++cl) { 293 const PetscInt fDim; 294 295 ierr = PetscSectionGetDof(oPoints[cl], &fDim);CHKERRQ(ierr); 296 if (pointDim) { 297 for (PetscInt d = 0; d < fDim; ++d, ++v) { 298 values[v] = (*this->_options.integrate)(v0, J, v, initFunc); 299 } 300 } 301 } 302 ierr = DMPlexVecSetClosure(dm, NULL, localX, c, values);CHKERRQ(ierr); 303 pV.clear(); 304 } 305 ierr = PetscFree2(v0,J);CHKERRQ(ierr); 306 ierr = PetscFree(values);CHKERRQ(ierr); 307 #endif 308 PetscFunctionReturn(0); 309 } 310 311 #undef __FUNCT__ 312 #define __FUNCT__ "DMPlexProjectFunction" 313 /*@C 314 DMPlexProjectFunction - This projects the given function into the function space provided. 315 316 Input Parameters: 317 + dm - The DM 318 . numComp - The number of components (functions) 319 . funcs - The coordinate functions to evaluate 320 - mode - The insertion mode for values 321 322 Output Parameter: 323 . X - vector 324 325 Level: developer 326 327 Note: 328 This currently just calls the function with the coordinates of each vertex and edge midpoint, and stores the result in a vector. 329 We will eventually fix it. 330 331 .seealso: DMPlexComputeL2Diff() 332 @*/ 333 PetscErrorCode DMPlexProjectFunction(DM dm, PetscInt numComp, PetscScalar (**funcs)(const PetscReal []), InsertMode mode, Vec X) 334 { 335 Vec localX; 336 PetscErrorCode ierr; 337 338 PetscFunctionBegin; 339 ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 340 ierr = DMPlexProjectFunctionLocal(dm, numComp, funcs, mode, localX);CHKERRQ(ierr); 341 ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr); 342 ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr); 343 ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 344 PetscFunctionReturn(0); 345 } 346 347 #undef __FUNCT__ 348 #define __FUNCT__ "DMPlexComputeL2Diff" 349 /*@C 350 DMPlexComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h. 351 352 Input Parameters: 353 + dm - The DM 354 . quad - The PetscQuadrature object for each field 355 . funcs - The functions to evaluate for each field component 356 - X - The coefficient vector u_h 357 358 Output Parameter: 359 . diff - The diff ||u - u_h||_2 360 361 Level: developer 362 363 .seealso: DMPlexProjectFunction() 364 @*/ 365 PetscErrorCode DMPlexComputeL2Diff(DM dm, PetscQuadrature quad[], PetscScalar (**funcs)(const PetscReal []), Vec X, PetscReal *diff) 366 { 367 const PetscInt debug = 0; 368 PetscSection section; 369 Vec localX; 370 PetscReal *coords, *v0, *J, *invJ, detJ; 371 PetscReal localDiff = 0.0; 372 PetscInt dim, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset, comp; 373 PetscErrorCode ierr; 374 375 PetscFunctionBegin; 376 ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 377 ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 378 ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 379 ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 380 ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 381 ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 382 for (field = 0; field < numFields; ++field) { 383 numComponents += quad[field].numComponents; 384 } 385 ierr = DMPlexProjectFunctionLocal(dm, numComponents, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 386 ierr = PetscMalloc4(dim,PetscReal,&coords,dim,PetscReal,&v0,dim*dim,PetscReal,&J,dim*dim,PetscReal,&invJ);CHKERRQ(ierr); 387 ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 388 for (c = cStart; c < cEnd; ++c) { 389 PetscScalar *x; 390 PetscReal elemDiff = 0.0; 391 392 ierr = DMPlexComputeCellGeometry(dm, c, v0, J, invJ, &detJ);CHKERRQ(ierr); 393 if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 394 ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 395 396 for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) { 397 const PetscInt numQuadPoints = quad[field].numQuadPoints; 398 const PetscReal *quadPoints = quad[field].quadPoints; 399 const PetscReal *quadWeights = quad[field].quadWeights; 400 const PetscInt numBasisFuncs = quad[field].numBasisFuncs; 401 const PetscInt numBasisComps = quad[field].numComponents; 402 const PetscReal *basis = quad[field].basis; 403 PetscInt q, d, e, fc, f; 404 405 if (debug) { 406 char title[1024]; 407 ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 408 ierr = DMPrintCellVector(c, title, numBasisFuncs*numBasisComps, &x[fieldOffset]);CHKERRQ(ierr); 409 } 410 for (q = 0; q < numQuadPoints; ++q) { 411 for (d = 0; d < dim; d++) { 412 coords[d] = v0[d]; 413 for (e = 0; e < dim; e++) { 414 coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0); 415 } 416 } 417 for (fc = 0; fc < numBasisComps; ++fc) { 418 const PetscReal funcVal = PetscRealPart((*funcs[comp+fc])(coords)); 419 PetscReal interpolant = 0.0; 420 for (f = 0; f < numBasisFuncs; ++f) { 421 const PetscInt fidx = f*numBasisComps+fc; 422 interpolant += PetscRealPart(x[fieldOffset+fidx])*basis[q*numBasisFuncs*numBasisComps+fidx]; 423 } 424 if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d field %d diff %g\n", c, field, PetscSqr(interpolant - funcVal)*quadWeights[q]*detJ);CHKERRQ(ierr);} 425 elemDiff += PetscSqr(interpolant - funcVal)*quadWeights[q]*detJ; 426 } 427 } 428 comp += numBasisComps; 429 fieldOffset += numBasisFuncs*numBasisComps; 430 } 431 ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 432 if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);} 433 localDiff += elemDiff; 434 } 435 ierr = PetscFree4(coords,v0,J,invJ);CHKERRQ(ierr); 436 ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 437 ierr = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPI_SUM, PETSC_COMM_WORLD);CHKERRQ(ierr); 438 *diff = PetscSqrtReal(*diff); 439 PetscFunctionReturn(0); 440 } 441 442 #undef __FUNCT__ 443 #define __FUNCT__ "DMPlexComputeResidualFEM" 444 /*@ 445 DMPlexComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user 446 447 Input Parameters: 448 + dm - The mesh 449 . X - Local input vector 450 - user - The user context 451 452 Output Parameter: 453 . F - Local output vector 454 455 Note: 456 The second member of the user context must be an FEMContext. 457 458 We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 459 like a GPU, or vectorize on a multicore machine. 460 461 Level: developer 462 463 .seealso: DMPlexComputeJacobianActionFEM() 464 @*/ 465 PetscErrorCode DMPlexComputeResidualFEM(DM dm, Vec X, Vec F, void *user) 466 { 467 DM_Plex *mesh = (DM_Plex*) dm->data; 468 PetscFEM *fem = (PetscFEM*) &((DM*) user)[1]; 469 PetscQuadrature *quad = fem->quad; 470 PetscQuadrature *quadBd = fem->quadBd; 471 PetscSection section; 472 PetscReal *v0, *n, *J, *invJ, *detJ; 473 PetscScalar *elemVec, *u; 474 PetscInt dim, numFields, field, numBatchesTmp = 1, numCells, cStart, cEnd, c; 475 PetscInt cellDof, numComponents; 476 PetscBool has; 477 PetscErrorCode ierr; 478 479 PetscFunctionBegin; 480 /* ierr = PetscLogEventBegin(ResidualFEMEvent,0,0,0,0);CHKERRQ(ierr); */ 481 ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 482 ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 483 ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 484 ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 485 numCells = cEnd - cStart; 486 for (field = 0, cellDof = 0, numComponents = 0; field < numFields; ++field) { 487 cellDof += quad[field].numBasisFuncs*quad[field].numComponents; 488 numComponents += quad[field].numComponents; 489 } 490 ierr = DMPlexProjectFunctionLocal(dm, numComponents, fem->bcFuncs, INSERT_BC_VALUES, X);CHKERRQ(ierr); 491 ierr = VecSet(F, 0.0);CHKERRQ(ierr); 492 ierr = PetscMalloc6(numCells*cellDof,PetscScalar,&u,numCells*dim,PetscReal,&v0,numCells*dim*dim,PetscReal,&J,numCells*dim*dim,PetscReal,&invJ,numCells,PetscReal,&detJ,numCells*cellDof,PetscScalar,&elemVec);CHKERRQ(ierr); 493 for (c = cStart; c < cEnd; ++c) { 494 PetscScalar *x; 495 PetscInt i; 496 497 ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr); 498 if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c); 499 ierr = DMPlexVecGetClosure(dm, NULL, X, c, NULL, &x);CHKERRQ(ierr); 500 501 for (i = 0; i < cellDof; ++i) u[c*cellDof+i] = x[i]; 502 ierr = DMPlexVecRestoreClosure(dm, NULL, X, c, NULL, &x);CHKERRQ(ierr); 503 } 504 for (field = 0; field < numFields; ++field) { 505 const PetscInt numQuadPoints = quad[field].numQuadPoints; 506 const PetscInt numBasisFuncs = quad[field].numBasisFuncs; 507 void (*f0)(const PetscScalar u[], const PetscScalar gradU[], const PetscReal x[], PetscScalar f0[]) = fem->f0Funcs[field]; 508 void (*f1)(const PetscScalar u[], const PetscScalar gradU[], const PetscReal x[], PetscScalar f1[]) = fem->f1Funcs[field]; 509 /* Conforming batches */ 510 PetscInt blockSize = numBasisFuncs*numQuadPoints; 511 PetscInt numBlocks = 1; 512 PetscInt batchSize = numBlocks * blockSize; 513 PetscInt numBatches = numBatchesTmp; 514 PetscInt numChunks = numCells / (numBatches*batchSize); 515 /* Remainder */ 516 PetscInt numRemainder = numCells % (numBatches * batchSize); 517 PetscInt offset = numCells - numRemainder; 518 519 ierr = (*mesh->integrateResidualFEM)(numChunks*numBatches*batchSize, numFields, field, quad, u, v0, J, invJ, detJ, f0, f1, elemVec);CHKERRQ(ierr); 520 ierr = (*mesh->integrateResidualFEM)(numRemainder, numFields, field, quad, &u[offset*cellDof], &v0[offset*dim], &J[offset*dim*dim], &invJ[offset*dim*dim], &detJ[offset], 521 f0, f1, &elemVec[offset*cellDof]);CHKERRQ(ierr); 522 } 523 for (c = cStart; c < cEnd; ++c) { 524 if (mesh->printFEM > 1) {ierr = DMPrintCellVector(c, "Residual", cellDof, &elemVec[c*cellDof]);CHKERRQ(ierr);} 525 ierr = DMPlexVecSetClosure(dm, NULL, F, c, &elemVec[c*cellDof], ADD_VALUES);CHKERRQ(ierr); 526 } 527 ierr = PetscFree6(u,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr); 528 /* Integration over the boundary: 529 - This can probably be generalized to integration over a set of labels, however 530 the idea here is to do integration where we need the cell normal 531 - We can replace hardcoding with a registration process, and this is how we hook 532 up the system to something like FEniCS 533 */ 534 ierr = DMPlexHasLabel(dm, "boundary", &has);CHKERRQ(ierr); 535 if (has && quadBd) { 536 DMLabel label; 537 IS pointIS; 538 const PetscInt *points; 539 PetscInt numPoints, p; 540 541 ierr = DMPlexGetLabel(dm, "boundary", &label);CHKERRQ(ierr); 542 ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr); 543 ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr); 544 ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 545 ierr = DMPlexGetLabelValue(dm, "depth", points[0], &dim);CHKERRQ(ierr); 546 for (field = 0, cellDof = 0, numComponents = 0; field < numFields; ++field) { 547 cellDof += quadBd[field].numBasisFuncs*quadBd[field].numComponents; 548 numComponents += quadBd[field].numComponents; 549 } 550 ierr = PetscMalloc7(numPoints*cellDof,PetscScalar,&u,numPoints*dim,PetscReal,&v0,numPoints*dim,PetscReal,&n,numPoints*dim*dim,PetscReal,&J,numPoints*dim*dim,PetscReal,&invJ,numPoints,PetscReal,&detJ,numPoints*cellDof,PetscScalar,&elemVec);CHKERRQ(ierr); 551 for (p = 0; p < numPoints; ++p) { 552 const PetscInt point = points[p]; 553 PetscScalar *x; 554 PetscInt i; 555 556 /* TODO: Add normal determination here */ 557 ierr = DMPlexComputeCellGeometry(dm, point, &v0[p*dim], &J[p*dim*dim], &invJ[p*dim*dim], &detJ[p]);CHKERRQ(ierr); 558 if (detJ[p] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[p], p); 559 ierr = DMPlexVecGetClosure(dm, NULL, X, point, NULL, &x);CHKERRQ(ierr); 560 561 for (i = 0; i < cellDof; ++i) u[p*cellDof+i] = x[i]; 562 ierr = DMPlexVecRestoreClosure(dm, NULL, X, point, NULL, &x);CHKERRQ(ierr); 563 } 564 for (field = 0; field < numFields; ++field) { 565 const PetscInt numQuadPoints = quadBd[field].numQuadPoints; 566 const PetscInt numBasisFuncs = quadBd[field].numBasisFuncs; 567 void (*f0)(const PetscScalar u[], const PetscScalar gradU[], const PetscReal x[], const PetscReal n[], PetscScalar f0[]) = fem->f0BdFuncs[field]; 568 void (*f1)(const PetscScalar u[], const PetscScalar gradU[], const PetscReal x[], const PetscReal n[], PetscScalar f1[]) = fem->f1BdFuncs[field]; 569 /* Conforming batches */ 570 PetscInt blockSize = numBasisFuncs*numQuadPoints; 571 PetscInt numBlocks = 1; 572 PetscInt batchSize = numBlocks * blockSize; 573 PetscInt numBatches = numBatchesTmp; 574 PetscInt numChunks = numPoints / (numBatches*batchSize); 575 /* Remainder */ 576 PetscInt numRemainder = numPoints % (numBatches * batchSize); 577 PetscInt offset = numPoints - numRemainder; 578 579 ierr = (*mesh->integrateBdResidualFEM)(numChunks*numBatches*batchSize, numFields, field, quadBd, u, v0, n, J, invJ, detJ, f0, f1, elemVec);CHKERRQ(ierr); 580 ierr = (*mesh->integrateBdResidualFEM)(numRemainder, numFields, field, quadBd, &u[offset*cellDof], &v0[offset*dim], &n[offset*dim], &J[offset*dim*dim], &invJ[offset*dim*dim], &detJ[offset], 581 f0, f1, &elemVec[offset*cellDof]);CHKERRQ(ierr); 582 } 583 for (p = 0; p < numPoints; ++p) { 584 const PetscInt point = points[p]; 585 586 if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "Residual", cellDof, &elemVec[p*cellDof]);CHKERRQ(ierr);} 587 ierr = DMPlexVecSetClosure(dm, NULL, F, point, &elemVec[p*cellDof], ADD_VALUES);CHKERRQ(ierr); 588 } 589 ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 590 ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 591 ierr = PetscFree7(u,v0,n,J,invJ,detJ,elemVec);CHKERRQ(ierr); 592 } 593 if (mesh->printFEM) { 594 PetscMPIInt rank, numProcs; 595 PetscInt p; 596 597 ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr); 598 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &numProcs);CHKERRQ(ierr); 599 ierr = PetscPrintf(PETSC_COMM_WORLD, "Residual:\n");CHKERRQ(ierr); 600 for (p = 0; p < numProcs; ++p) { 601 if (p == rank) { 602 Vec f; 603 604 ierr = VecDuplicate(F, &f);CHKERRQ(ierr); 605 ierr = VecCopy(F, f);CHKERRQ(ierr); 606 ierr = VecChop(f, 1.0e-10);CHKERRQ(ierr); 607 ierr = VecView(f, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr); 608 ierr = VecDestroy(&f);CHKERRQ(ierr); 609 ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr); 610 } 611 ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr); 612 } 613 } 614 /* ierr = PetscLogEventEnd(ResidualFEMEvent,0,0,0,0);CHKERRQ(ierr); */ 615 PetscFunctionReturn(0); 616 } 617 618 #undef __FUNCT__ 619 #define __FUNCT__ "DMPlexComputeJacobianActionFEM" 620 /*@C 621 DMPlexComputeJacobianActionFEM - Form the local action of Jacobian J(u) on the local input X using pointwise functions specified by the user 622 623 Input Parameters: 624 + dm - The mesh 625 . J - The Jacobian shell matrix 626 . X - Local input vector 627 - user - The user context 628 629 Output Parameter: 630 . F - Local output vector 631 632 Note: 633 The second member of the user context must be an FEMContext. 634 635 We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 636 like a GPU, or vectorize on a multicore machine. 637 638 Level: developer 639 640 .seealso: DMPlexComputeResidualFEM() 641 @*/ 642 PetscErrorCode DMPlexComputeJacobianActionFEM(DM dm, Mat Jac, Vec X, Vec F, void *user) 643 { 644 DM_Plex *mesh = (DM_Plex*) dm->data; 645 PetscFEM *fem = (PetscFEM*) &((DM*) user)[1]; 646 PetscQuadrature *quad = fem->quad; 647 PetscSection section; 648 JacActionCtx *jctx; 649 PetscReal *v0, *J, *invJ, *detJ; 650 PetscScalar *elemVec, *u, *a; 651 PetscInt dim, numFields, field, numBatchesTmp = 1, numCells, cStart, cEnd, c; 652 PetscInt cellDof = 0; 653 PetscErrorCode ierr; 654 655 PetscFunctionBegin; 656 /* ierr = PetscLogEventBegin(JacobianActionFEMEvent,0,0,0,0);CHKERRQ(ierr); */ 657 ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr); 658 ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 659 ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 660 ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 661 ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 662 numCells = cEnd - cStart; 663 for (field = 0; field < numFields; ++field) { 664 cellDof += quad[field].numBasisFuncs*quad[field].numComponents; 665 } 666 ierr = VecSet(F, 0.0);CHKERRQ(ierr); 667 ierr = PetscMalloc7(numCells*cellDof,PetscScalar,&u,numCells*cellDof,PetscScalar,&a,numCells*dim,PetscReal,&v0,numCells*dim*dim,PetscReal,&J,numCells*dim*dim,PetscReal,&invJ,numCells,PetscReal,&detJ,numCells*cellDof,PetscScalar,&elemVec);CHKERRQ(ierr); 668 for (c = cStart; c < cEnd; ++c) { 669 PetscScalar *x; 670 PetscInt i; 671 672 ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr); 673 if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c); 674 ierr = DMPlexVecGetClosure(dm, NULL, jctx->u, c, NULL, &x);CHKERRQ(ierr); 675 for (i = 0; i < cellDof; ++i) u[c*cellDof+i] = x[i]; 676 ierr = DMPlexVecRestoreClosure(dm, NULL, jctx->u, c, NULL, &x);CHKERRQ(ierr); 677 ierr = DMPlexVecGetClosure(dm, NULL, X, c, NULL, &x);CHKERRQ(ierr); 678 for (i = 0; i < cellDof; ++i) a[c*cellDof+i] = x[i]; 679 ierr = DMPlexVecRestoreClosure(dm, NULL, X, c, NULL, &x);CHKERRQ(ierr); 680 } 681 for (field = 0; field < numFields; ++field) { 682 const PetscInt numQuadPoints = quad[field].numQuadPoints; 683 const PetscInt numBasisFuncs = quad[field].numBasisFuncs; 684 /* Conforming batches */ 685 PetscInt blockSize = numBasisFuncs*numQuadPoints; 686 PetscInt numBlocks = 1; 687 PetscInt batchSize = numBlocks * blockSize; 688 PetscInt numBatches = numBatchesTmp; 689 PetscInt numChunks = numCells / (numBatches*batchSize); 690 /* Remainder */ 691 PetscInt numRemainder = numCells % (numBatches * batchSize); 692 PetscInt offset = numCells - numRemainder; 693 694 ierr = (*mesh->integrateJacobianActionFEM)(numChunks*numBatches*batchSize, numFields, field, quad, u, a, v0, J, invJ, detJ, fem->g0Funcs, fem->g1Funcs, fem->g2Funcs, fem->g3Funcs, elemVec);CHKERRQ(ierr); 695 ierr = (*mesh->integrateJacobianActionFEM)(numRemainder, numFields, field, quad, &u[offset*cellDof], &a[offset*cellDof], &v0[offset*dim], &J[offset*dim*dim], &invJ[offset*dim*dim], &detJ[offset], 696 fem->g0Funcs, fem->g1Funcs, fem->g2Funcs, fem->g3Funcs, &elemVec[offset*cellDof]);CHKERRQ(ierr); 697 } 698 for (c = cStart; c < cEnd; ++c) { 699 if (mesh->printFEM > 1) {ierr = DMPrintCellVector(c, "Jacobian Action", cellDof, &elemVec[c*cellDof]);CHKERRQ(ierr);} 700 ierr = DMPlexVecSetClosure(dm, NULL, F, c, &elemVec[c*cellDof], ADD_VALUES);CHKERRQ(ierr); 701 } 702 ierr = PetscFree7(u,a,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr); 703 if (mesh->printFEM) { 704 PetscMPIInt rank, numProcs; 705 PetscInt p; 706 707 ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr); 708 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &numProcs);CHKERRQ(ierr); 709 ierr = PetscPrintf(PETSC_COMM_WORLD, "Jacobian Action:\n");CHKERRQ(ierr); 710 for (p = 0; p < numProcs; ++p) { 711 if (p == rank) {ierr = VecView(F, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);} 712 ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr); 713 } 714 } 715 /* ierr = PetscLogEventEnd(JacobianActionFEMEvent,0,0,0,0);CHKERRQ(ierr); */ 716 PetscFunctionReturn(0); 717 } 718 719 #undef __FUNCT__ 720 #define __FUNCT__ "DMPlexComputeJacobianFEM" 721 /*@ 722 DMPlexComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user. 723 724 Input Parameters: 725 + dm - The mesh 726 . X - Local input vector 727 - user - The user context 728 729 Output Parameter: 730 . Jac - Jacobian matrix 731 732 Note: 733 The second member of the user context must be an FEMContext. 734 735 We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 736 like a GPU, or vectorize on a multicore machine. 737 738 Level: developer 739 740 .seealso: FormFunctionLocal() 741 @*/ 742 PetscErrorCode DMPlexComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP, MatStructure *str,void *user) 743 { 744 DM_Plex *mesh = (DM_Plex*) dm->data; 745 PetscFEM *fem = (PetscFEM*) &((DM*) user)[1]; 746 PetscQuadrature *quad = fem->quad; 747 PetscSection section; 748 PetscReal *v0, *J, *invJ, *detJ; 749 PetscScalar *elemMat, *u; 750 PetscInt dim, numFields, field, fieldI, numBatchesTmp = 1, numCells, cStart, cEnd, c; 751 PetscInt cellDof = 0, numComponents = 0; 752 PetscBool isShell; 753 PetscErrorCode ierr; 754 755 PetscFunctionBegin; 756 /* ierr = PetscLogEventBegin(JacobianFEMEvent,0,0,0,0);CHKERRQ(ierr); */ 757 ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 758 ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 759 ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 760 ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 761 numCells = cEnd - cStart; 762 for (field = 0; field < numFields; ++field) { 763 cellDof += quad[field].numBasisFuncs*quad[field].numComponents; 764 numComponents += quad[field].numComponents; 765 } 766 ierr = DMPlexProjectFunctionLocal(dm, numComponents, fem->bcFuncs, INSERT_BC_VALUES, X);CHKERRQ(ierr); 767 ierr = MatZeroEntries(JacP);CHKERRQ(ierr); 768 ierr = PetscMalloc6(numCells*cellDof,PetscScalar,&u,numCells*dim,PetscReal,&v0,numCells*dim*dim,PetscReal,&J,numCells*dim*dim,PetscReal,&invJ,numCells,PetscReal,&detJ,numCells*cellDof*cellDof,PetscScalar,&elemMat);CHKERRQ(ierr); 769 for (c = cStart; c < cEnd; ++c) { 770 PetscScalar *x; 771 PetscInt i; 772 773 ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr); 774 if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c); 775 ierr = DMPlexVecGetClosure(dm, NULL, X, c, NULL, &x);CHKERRQ(ierr); 776 777 for (i = 0; i < cellDof; ++i) u[c*cellDof+i] = x[i]; 778 ierr = DMPlexVecRestoreClosure(dm, NULL, X, c, NULL, &x);CHKERRQ(ierr); 779 } 780 ierr = PetscMemzero(elemMat, numCells*cellDof*cellDof * sizeof(PetscScalar));CHKERRQ(ierr); 781 for (fieldI = 0; fieldI < numFields; ++fieldI) { 782 const PetscInt numQuadPoints = quad[fieldI].numQuadPoints; 783 const PetscInt numBasisFuncs = quad[fieldI].numBasisFuncs; 784 PetscInt fieldJ; 785 786 for (fieldJ = 0; fieldJ < numFields; ++fieldJ) { 787 void (*g0)(const PetscScalar u[], const PetscScalar gradU[], const PetscReal x[], PetscScalar g0[]) = fem->g0Funcs[fieldI*numFields+fieldJ]; 788 void (*g1)(const PetscScalar u[], const PetscScalar gradU[], const PetscReal x[], PetscScalar g1[]) = fem->g1Funcs[fieldI*numFields+fieldJ]; 789 void (*g2)(const PetscScalar u[], const PetscScalar gradU[], const PetscReal x[], PetscScalar g2[]) = fem->g2Funcs[fieldI*numFields+fieldJ]; 790 void (*g3)(const PetscScalar u[], const PetscScalar gradU[], const PetscReal x[], PetscScalar g3[]) = fem->g3Funcs[fieldI*numFields+fieldJ]; 791 /* Conforming batches */ 792 PetscInt blockSize = numBasisFuncs*numQuadPoints; 793 PetscInt numBlocks = 1; 794 PetscInt batchSize = numBlocks * blockSize; 795 PetscInt numBatches = numBatchesTmp; 796 PetscInt numChunks = numCells / (numBatches*batchSize); 797 /* Remainder */ 798 PetscInt numRemainder = numCells % (numBatches * batchSize); 799 PetscInt offset = numCells - numRemainder; 800 801 ierr = (*mesh->integrateJacobianFEM)(numChunks*numBatches*batchSize, numFields, fieldI, fieldJ, quad, u, v0, J, invJ, detJ, g0, g1, g2, g3, elemMat);CHKERRQ(ierr); 802 ierr = (*mesh->integrateJacobianFEM)(numRemainder, numFields, fieldI, fieldJ, quad, &u[offset*cellDof], &v0[offset*dim], &J[offset*dim*dim], &invJ[offset*dim*dim], &detJ[offset], 803 g0, g1, g2, g3, &elemMat[offset*cellDof*cellDof]);CHKERRQ(ierr); 804 } 805 } 806 for (c = cStart; c < cEnd; ++c) { 807 if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, "Jacobian", cellDof, cellDof, &elemMat[c*cellDof*cellDof]);CHKERRQ(ierr);} 808 ierr = DMPlexMatSetClosure(dm, NULL, NULL, JacP, c, &elemMat[c*cellDof*cellDof], ADD_VALUES);CHKERRQ(ierr); 809 } 810 ierr = PetscFree6(u,v0,J,invJ,detJ,elemMat);CHKERRQ(ierr); 811 812 /* Assemble matrix, using the 2-step process: 813 MatAssemblyBegin(), MatAssemblyEnd(). */ 814 ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 815 ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 816 817 if (mesh->printFEM) { 818 ierr = PetscPrintf(PETSC_COMM_WORLD, "Jacobian:\n");CHKERRQ(ierr); 819 ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr); 820 ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 821 } 822 /* ierr = PetscLogEventEnd(JacobianFEMEvent,0,0,0,0);CHKERRQ(ierr); */ 823 ierr = PetscObjectTypeCompare((PetscObject)Jac, MATSHELL, &isShell);CHKERRQ(ierr); 824 if (isShell) { 825 JacActionCtx *jctx; 826 827 ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr); 828 ierr = VecCopy(X, jctx->u);CHKERRQ(ierr); 829 } 830 *str = SAME_NONZERO_PATTERN; 831 PetscFunctionReturn(0); 832 } 833