1 #include <petsc/private/pcpatchimpl.h> /*I "petscpc.h" I*/ 2 #include <petsc/private/kspimpl.h> /* For ksp->setfromoptionscalled */ 3 #include <petsc/private/vecimpl.h> /* For vec->map */ 4 #include <petsc/private/dmpleximpl.h> /* For DMPlexComputeJacobian_Patch_Internal() */ 5 #include <petscsf.h> 6 #include <petscbt.h> 7 #include <petscds.h> 8 #include <../src/mat/impls/dense/seq/dense.h> /*I "petscmat.h" I*/ 9 10 PetscLogEvent PC_Patch_CreatePatches, PC_Patch_ComputeOp, PC_Patch_Solve, PC_Patch_Apply, PC_Patch_Prealloc; 11 12 static inline PetscErrorCode ObjectView(PetscObject obj, PetscViewer viewer, PetscViewerFormat format) 13 { 14 15 PetscCall(PetscViewerPushFormat(viewer, format)); 16 PetscCall(PetscObjectView(obj, viewer)); 17 PetscCall(PetscViewerPopFormat(viewer)); 18 return(0); 19 } 20 21 static PetscErrorCode PCPatchConstruct_Star(void *vpatch, DM dm, PetscInt point, PetscHSetI ht) 22 { 23 PetscInt starSize; 24 PetscInt *star = NULL, si; 25 26 PetscFunctionBegin; 27 PetscCall(PetscHSetIClear(ht)); 28 /* To start with, add the point we care about */ 29 PetscCall(PetscHSetIAdd(ht, point)); 30 /* Loop over all the points that this point connects to */ 31 PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star)); 32 for (si = 0; si < starSize*2; si += 2) PetscCall(PetscHSetIAdd(ht, star[si])); 33 PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star)); 34 PetscFunctionReturn(0); 35 } 36 37 static PetscErrorCode PCPatchConstruct_Vanka(void *vpatch, DM dm, PetscInt point, PetscHSetI ht) 38 { 39 PC_PATCH *patch = (PC_PATCH *) vpatch; 40 PetscInt starSize; 41 PetscInt *star = NULL; 42 PetscBool shouldIgnore = PETSC_FALSE; 43 PetscInt cStart, cEnd, iStart, iEnd, si; 44 45 PetscFunctionBegin; 46 PetscCall(PetscHSetIClear(ht)); 47 /* To start with, add the point we care about */ 48 PetscCall(PetscHSetIAdd(ht, point)); 49 /* Should we ignore any points of a certain dimension? */ 50 if (patch->vankadim >= 0) { 51 shouldIgnore = PETSC_TRUE; 52 PetscCall(DMPlexGetDepthStratum(dm, patch->vankadim, &iStart, &iEnd)); 53 } 54 PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 55 /* Loop over all the cells that this point connects to */ 56 PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star)); 57 for (si = 0; si < starSize*2; si += 2) { 58 const PetscInt cell = star[si]; 59 PetscInt closureSize; 60 PetscInt *closure = NULL, ci; 61 62 if (cell < cStart || cell >= cEnd) continue; 63 /* now loop over all entities in the closure of that cell */ 64 PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure)); 65 for (ci = 0; ci < closureSize*2; ci += 2) { 66 const PetscInt newpoint = closure[ci]; 67 68 /* We've been told to ignore entities of this type.*/ 69 if (shouldIgnore && newpoint >= iStart && newpoint < iEnd) continue; 70 PetscCall(PetscHSetIAdd(ht, newpoint)); 71 } 72 PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure)); 73 } 74 PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star)); 75 PetscFunctionReturn(0); 76 } 77 78 static PetscErrorCode PCPatchConstruct_Pardecomp(void *vpatch, DM dm, PetscInt point, PetscHSetI ht) 79 { 80 PC_PATCH *patch = (PC_PATCH *) vpatch; 81 DMLabel ghost = NULL; 82 const PetscInt *leaves; 83 PetscInt nleaves, pStart, pEnd, loc; 84 PetscBool isFiredrake; 85 PetscBool flg; 86 PetscInt starSize; 87 PetscInt *star = NULL; 88 PetscInt opoint, overlapi; 89 90 PetscFunctionBegin; 91 PetscCall(PetscHSetIClear(ht)); 92 93 PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 94 95 PetscCall(DMHasLabel(dm, "pyop2_ghost", &isFiredrake)); 96 if (isFiredrake) { 97 PetscCall(DMGetLabel(dm, "pyop2_ghost", &ghost)); 98 PetscCall(DMLabelCreateIndex(ghost, pStart, pEnd)); 99 } else { 100 PetscSF sf; 101 PetscCall(DMGetPointSF(dm, &sf)); 102 PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL)); 103 nleaves = PetscMax(nleaves, 0); 104 } 105 106 for (opoint = pStart; opoint < pEnd; ++opoint) { 107 if (ghost) PetscCall(DMLabelHasPoint(ghost, opoint, &flg)); 108 else {PetscCall(PetscFindInt(opoint, nleaves, leaves, &loc)); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;} 109 /* Not an owned entity, don't make a cell patch. */ 110 if (flg) continue; 111 PetscCall(PetscHSetIAdd(ht, opoint)); 112 } 113 114 /* Now build the overlap for the patch */ 115 for (overlapi = 0; overlapi < patch->pardecomp_overlap; ++overlapi) { 116 PetscInt index = 0; 117 PetscInt *htpoints = NULL; 118 PetscInt htsize; 119 PetscInt i; 120 121 PetscCall(PetscHSetIGetSize(ht, &htsize)); 122 PetscCall(PetscMalloc1(htsize, &htpoints)); 123 PetscCall(PetscHSetIGetElems(ht, &index, htpoints)); 124 125 for (i = 0; i < htsize; ++i) { 126 PetscInt hpoint = htpoints[i]; 127 PetscInt si; 128 129 PetscCall(DMPlexGetTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star)); 130 for (si = 0; si < starSize*2; si += 2) { 131 const PetscInt starp = star[si]; 132 PetscInt closureSize; 133 PetscInt *closure = NULL, ci; 134 135 /* now loop over all entities in the closure of starp */ 136 PetscCall(DMPlexGetTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure)); 137 for (ci = 0; ci < closureSize*2; ci += 2) { 138 const PetscInt closstarp = closure[ci]; 139 PetscCall(PetscHSetIAdd(ht, closstarp)); 140 } 141 PetscCall(DMPlexRestoreTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure)); 142 } 143 PetscCall(DMPlexRestoreTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star)); 144 } 145 PetscCall(PetscFree(htpoints)); 146 } 147 148 PetscFunctionReturn(0); 149 } 150 151 /* The user's already set the patches in patch->userIS. Build the hash tables */ 152 static PetscErrorCode PCPatchConstruct_User(void *vpatch, DM dm, PetscInt point, PetscHSetI ht) 153 { 154 PC_PATCH *patch = (PC_PATCH *) vpatch; 155 IS patchis = patch->userIS[point]; 156 PetscInt n; 157 const PetscInt *patchdata; 158 PetscInt pStart, pEnd, i; 159 160 PetscFunctionBegin; 161 PetscCall(PetscHSetIClear(ht)); 162 PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 163 PetscCall(ISGetLocalSize(patchis, &n)); 164 PetscCall(ISGetIndices(patchis, &patchdata)); 165 for (i = 0; i < n; ++i) { 166 const PetscInt ownedpoint = patchdata[i]; 167 168 if (ownedpoint < pStart || ownedpoint >= pEnd) { 169 SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %" PetscInt_FMT " was not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", ownedpoint, pStart, pEnd); 170 } 171 PetscCall(PetscHSetIAdd(ht, ownedpoint)); 172 } 173 PetscCall(ISRestoreIndices(patchis, &patchdata)); 174 PetscFunctionReturn(0); 175 } 176 177 static PetscErrorCode PCPatchCreateDefaultSF_Private(PC pc, PetscInt n, const PetscSF *sf, const PetscInt *bs) 178 { 179 PC_PATCH *patch = (PC_PATCH *) pc->data; 180 PetscInt i; 181 182 PetscFunctionBegin; 183 if (n == 1 && bs[0] == 1) { 184 patch->sectionSF = sf[0]; 185 PetscCall(PetscObjectReference((PetscObject) patch->sectionSF)); 186 } else { 187 PetscInt allRoots = 0, allLeaves = 0; 188 PetscInt leafOffset = 0; 189 PetscInt *ilocal = NULL; 190 PetscSFNode *iremote = NULL; 191 PetscInt *remoteOffsets = NULL; 192 PetscInt index = 0; 193 PetscHMapI rankToIndex; 194 PetscInt numRanks = 0; 195 PetscSFNode *remote = NULL; 196 PetscSF rankSF; 197 PetscInt *ranks = NULL; 198 PetscInt *offsets = NULL; 199 MPI_Datatype contig; 200 PetscHSetI ranksUniq; 201 202 /* First figure out how many dofs there are in the concatenated numbering. 203 * allRoots: number of owned global dofs; 204 * allLeaves: number of visible dofs (global + ghosted). 205 */ 206 for (i = 0; i < n; ++i) { 207 PetscInt nroots, nleaves; 208 209 PetscCall(PetscSFGetGraph(sf[i], &nroots, &nleaves, NULL, NULL)); 210 allRoots += nroots * bs[i]; 211 allLeaves += nleaves * bs[i]; 212 } 213 PetscCall(PetscMalloc1(allLeaves, &ilocal)); 214 PetscCall(PetscMalloc1(allLeaves, &iremote)); 215 /* Now build an SF that just contains process connectivity. */ 216 PetscCall(PetscHSetICreate(&ranksUniq)); 217 for (i = 0; i < n; ++i) { 218 const PetscMPIInt *ranks = NULL; 219 PetscInt nranks, j; 220 221 PetscCall(PetscSFSetUp(sf[i])); 222 PetscCall(PetscSFGetRootRanks(sf[i], &nranks, &ranks, NULL, NULL, NULL)); 223 /* These are all the ranks who communicate with me. */ 224 for (j = 0; j < nranks; ++j) { 225 PetscCall(PetscHSetIAdd(ranksUniq, (PetscInt) ranks[j])); 226 } 227 } 228 PetscCall(PetscHSetIGetSize(ranksUniq, &numRanks)); 229 PetscCall(PetscMalloc1(numRanks, &remote)); 230 PetscCall(PetscMalloc1(numRanks, &ranks)); 231 PetscCall(PetscHSetIGetElems(ranksUniq, &index, ranks)); 232 233 PetscCall(PetscHMapICreate(&rankToIndex)); 234 for (i = 0; i < numRanks; ++i) { 235 remote[i].rank = ranks[i]; 236 remote[i].index = 0; 237 PetscCall(PetscHMapISet(rankToIndex, ranks[i], i)); 238 } 239 PetscCall(PetscFree(ranks)); 240 PetscCall(PetscHSetIDestroy(&ranksUniq)); 241 PetscCall(PetscSFCreate(PetscObjectComm((PetscObject) pc), &rankSF)); 242 PetscCall(PetscSFSetGraph(rankSF, 1, numRanks, NULL, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER)); 243 PetscCall(PetscSFSetUp(rankSF)); 244 /* OK, use it to communicate the root offset on the remote 245 * processes for each subspace. */ 246 PetscCall(PetscMalloc1(n, &offsets)); 247 PetscCall(PetscMalloc1(n*numRanks, &remoteOffsets)); 248 249 offsets[0] = 0; 250 for (i = 1; i < n; ++i) { 251 PetscInt nroots; 252 253 PetscCall(PetscSFGetGraph(sf[i-1], &nroots, NULL, NULL, NULL)); 254 offsets[i] = offsets[i-1] + nroots*bs[i-1]; 255 } 256 /* Offsets are the offsets on the current process of the 257 * global dof numbering for the subspaces. */ 258 PetscCallMPI(MPI_Type_contiguous(n, MPIU_INT, &contig)); 259 PetscCallMPI(MPI_Type_commit(&contig)); 260 261 PetscCall(PetscSFBcastBegin(rankSF, contig, offsets, remoteOffsets,MPI_REPLACE)); 262 PetscCall(PetscSFBcastEnd(rankSF, contig, offsets, remoteOffsets,MPI_REPLACE)); 263 PetscCallMPI(MPI_Type_free(&contig)); 264 PetscCall(PetscFree(offsets)); 265 PetscCall(PetscSFDestroy(&rankSF)); 266 /* Now remoteOffsets contains the offsets on the remote 267 * processes who communicate with me. So now we can 268 * concatenate the list of SFs into a single one. */ 269 index = 0; 270 for (i = 0; i < n; ++i) { 271 const PetscSFNode *remote = NULL; 272 const PetscInt *local = NULL; 273 PetscInt nroots, nleaves, j; 274 275 PetscCall(PetscSFGetGraph(sf[i], &nroots, &nleaves, &local, &remote)); 276 for (j = 0; j < nleaves; ++j) { 277 PetscInt rank = remote[j].rank; 278 PetscInt idx, rootOffset, k; 279 280 PetscCall(PetscHMapIGet(rankToIndex, rank, &idx)); 281 PetscCheck(idx != -1,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Didn't find rank, huh?"); 282 /* Offset on given rank for ith subspace */ 283 rootOffset = remoteOffsets[n*idx + i]; 284 for (k = 0; k < bs[i]; ++k) { 285 ilocal[index] = (local ? local[j] : j)*bs[i] + k + leafOffset; 286 iremote[index].rank = remote[j].rank; 287 iremote[index].index = remote[j].index*bs[i] + k + rootOffset; 288 ++index; 289 } 290 } 291 leafOffset += nleaves * bs[i]; 292 } 293 PetscCall(PetscHMapIDestroy(&rankToIndex)); 294 PetscCall(PetscFree(remoteOffsets)); 295 PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)pc), &patch->sectionSF)); 296 PetscCall(PetscSFSetGraph(patch->sectionSF, allRoots, allLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER)); 297 } 298 PetscFunctionReturn(0); 299 } 300 301 PetscErrorCode PCPatchSetDenseInverse(PC pc, PetscBool flg) 302 { 303 PC_PATCH *patch = (PC_PATCH *) pc->data; 304 PetscFunctionBegin; 305 patch->denseinverse = flg; 306 PetscFunctionReturn(0); 307 } 308 309 PetscErrorCode PCPatchGetDenseInverse(PC pc, PetscBool *flg) 310 { 311 PC_PATCH *patch = (PC_PATCH *) pc->data; 312 PetscFunctionBegin; 313 *flg = patch->denseinverse; 314 PetscFunctionReturn(0); 315 } 316 317 /* TODO: Docs */ 318 PetscErrorCode PCPatchSetIgnoreDim(PC pc, PetscInt dim) 319 { 320 PC_PATCH *patch = (PC_PATCH *) pc->data; 321 PetscFunctionBegin; 322 patch->ignoredim = dim; 323 PetscFunctionReturn(0); 324 } 325 326 /* TODO: Docs */ 327 PetscErrorCode PCPatchGetIgnoreDim(PC pc, PetscInt *dim) 328 { 329 PC_PATCH *patch = (PC_PATCH *) pc->data; 330 PetscFunctionBegin; 331 *dim = patch->ignoredim; 332 PetscFunctionReturn(0); 333 } 334 335 /* TODO: Docs */ 336 PetscErrorCode PCPatchSetSaveOperators(PC pc, PetscBool flg) 337 { 338 PC_PATCH *patch = (PC_PATCH *) pc->data; 339 PetscFunctionBegin; 340 patch->save_operators = flg; 341 PetscFunctionReturn(0); 342 } 343 344 /* TODO: Docs */ 345 PetscErrorCode PCPatchGetSaveOperators(PC pc, PetscBool *flg) 346 { 347 PC_PATCH *patch = (PC_PATCH *) pc->data; 348 PetscFunctionBegin; 349 *flg = patch->save_operators; 350 PetscFunctionReturn(0); 351 } 352 353 /* TODO: Docs */ 354 PetscErrorCode PCPatchSetPrecomputeElementTensors(PC pc, PetscBool flg) 355 { 356 PC_PATCH *patch = (PC_PATCH *) pc->data; 357 PetscFunctionBegin; 358 patch->precomputeElementTensors = flg; 359 PetscFunctionReturn(0); 360 } 361 362 /* TODO: Docs */ 363 PetscErrorCode PCPatchGetPrecomputeElementTensors(PC pc, PetscBool *flg) 364 { 365 PC_PATCH *patch = (PC_PATCH *) pc->data; 366 PetscFunctionBegin; 367 *flg = patch->precomputeElementTensors; 368 PetscFunctionReturn(0); 369 } 370 371 /* TODO: Docs */ 372 PetscErrorCode PCPatchSetPartitionOfUnity(PC pc, PetscBool flg) 373 { 374 PC_PATCH *patch = (PC_PATCH *) pc->data; 375 PetscFunctionBegin; 376 patch->partition_of_unity = flg; 377 PetscFunctionReturn(0); 378 } 379 380 /* TODO: Docs */ 381 PetscErrorCode PCPatchGetPartitionOfUnity(PC pc, PetscBool *flg) 382 { 383 PC_PATCH *patch = (PC_PATCH *) pc->data; 384 PetscFunctionBegin; 385 *flg = patch->partition_of_unity; 386 PetscFunctionReturn(0); 387 } 388 389 /* TODO: Docs */ 390 PetscErrorCode PCPatchSetLocalComposition(PC pc, PCCompositeType type) 391 { 392 PC_PATCH *patch = (PC_PATCH *) pc->data; 393 PetscFunctionBegin; 394 PetscCheck(type == PC_COMPOSITE_ADDITIVE || type == PC_COMPOSITE_MULTIPLICATIVE,PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Only supports additive or multiplicative as the local type"); 395 patch->local_composition_type = type; 396 PetscFunctionReturn(0); 397 } 398 399 /* TODO: Docs */ 400 PetscErrorCode PCPatchGetLocalComposition(PC pc, PCCompositeType *type) 401 { 402 PC_PATCH *patch = (PC_PATCH *) pc->data; 403 PetscFunctionBegin; 404 *type = patch->local_composition_type; 405 PetscFunctionReturn(0); 406 } 407 408 /* TODO: Docs */ 409 PetscErrorCode PCPatchSetSubMatType(PC pc, MatType sub_mat_type) 410 { 411 PC_PATCH *patch = (PC_PATCH *) pc->data; 412 413 PetscFunctionBegin; 414 if (patch->sub_mat_type) PetscCall(PetscFree(patch->sub_mat_type)); 415 PetscCall(PetscStrallocpy(sub_mat_type, (char **) &patch->sub_mat_type)); 416 PetscFunctionReturn(0); 417 } 418 419 /* TODO: Docs */ 420 PetscErrorCode PCPatchGetSubMatType(PC pc, MatType *sub_mat_type) 421 { 422 PC_PATCH *patch = (PC_PATCH *) pc->data; 423 PetscFunctionBegin; 424 *sub_mat_type = patch->sub_mat_type; 425 PetscFunctionReturn(0); 426 } 427 428 /* TODO: Docs */ 429 PetscErrorCode PCPatchSetCellNumbering(PC pc, PetscSection cellNumbering) 430 { 431 PC_PATCH *patch = (PC_PATCH *) pc->data; 432 433 PetscFunctionBegin; 434 patch->cellNumbering = cellNumbering; 435 PetscCall(PetscObjectReference((PetscObject) cellNumbering)); 436 PetscFunctionReturn(0); 437 } 438 439 /* TODO: Docs */ 440 PetscErrorCode PCPatchGetCellNumbering(PC pc, PetscSection *cellNumbering) 441 { 442 PC_PATCH *patch = (PC_PATCH *) pc->data; 443 PetscFunctionBegin; 444 *cellNumbering = patch->cellNumbering; 445 PetscFunctionReturn(0); 446 } 447 448 /* TODO: Docs */ 449 PetscErrorCode PCPatchSetConstructType(PC pc, PCPatchConstructType ctype, PetscErrorCode (*func)(PC, PetscInt *, IS **, IS *, void *), void *ctx) 450 { 451 PC_PATCH *patch = (PC_PATCH *) pc->data; 452 453 PetscFunctionBegin; 454 patch->ctype = ctype; 455 switch (ctype) { 456 case PC_PATCH_STAR: 457 patch->user_patches = PETSC_FALSE; 458 patch->patchconstructop = PCPatchConstruct_Star; 459 break; 460 case PC_PATCH_VANKA: 461 patch->user_patches = PETSC_FALSE; 462 patch->patchconstructop = PCPatchConstruct_Vanka; 463 break; 464 case PC_PATCH_PARDECOMP: 465 patch->user_patches = PETSC_FALSE; 466 patch->patchconstructop = PCPatchConstruct_Pardecomp; 467 break; 468 case PC_PATCH_USER: 469 case PC_PATCH_PYTHON: 470 patch->user_patches = PETSC_TRUE; 471 patch->patchconstructop = PCPatchConstruct_User; 472 if (func) { 473 patch->userpatchconstructionop = func; 474 patch->userpatchconstructctx = ctx; 475 } 476 break; 477 default: 478 SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %" PetscInt_FMT, (PetscInt) patch->ctype); 479 } 480 PetscFunctionReturn(0); 481 } 482 483 /* TODO: Docs */ 484 PetscErrorCode PCPatchGetConstructType(PC pc, PCPatchConstructType *ctype, PetscErrorCode (**func)(PC, PetscInt *, IS **, IS *, void *), void **ctx) 485 { 486 PC_PATCH *patch = (PC_PATCH *) pc->data; 487 488 PetscFunctionBegin; 489 *ctype = patch->ctype; 490 switch (patch->ctype) { 491 case PC_PATCH_STAR: 492 case PC_PATCH_VANKA: 493 case PC_PATCH_PARDECOMP: 494 break; 495 case PC_PATCH_USER: 496 case PC_PATCH_PYTHON: 497 *func = patch->userpatchconstructionop; 498 *ctx = patch->userpatchconstructctx; 499 break; 500 default: 501 SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %" PetscInt_FMT, (PetscInt) patch->ctype); 502 } 503 PetscFunctionReturn(0); 504 } 505 506 /* TODO: Docs */ 507 PetscErrorCode PCPatchSetDiscretisationInfo(PC pc, PetscInt nsubspaces, DM *dms, PetscInt *bs, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, 508 const PetscInt *subspaceOffsets, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes) 509 { 510 PC_PATCH *patch = (PC_PATCH *) pc->data; 511 DM dm, plex; 512 PetscSF *sfs; 513 PetscInt cStart, cEnd, i, j; 514 515 PetscFunctionBegin; 516 PetscCall(PCGetDM(pc, &dm)); 517 PetscCall(DMConvert(dm, DMPLEX, &plex)); 518 dm = plex; 519 PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 520 PetscCall(PetscMalloc1(nsubspaces, &sfs)); 521 PetscCall(PetscMalloc1(nsubspaces, &patch->dofSection)); 522 PetscCall(PetscMalloc1(nsubspaces, &patch->bs)); 523 PetscCall(PetscMalloc1(nsubspaces, &patch->nodesPerCell)); 524 PetscCall(PetscMalloc1(nsubspaces, &patch->cellNodeMap)); 525 PetscCall(PetscMalloc1(nsubspaces+1, &patch->subspaceOffsets)); 526 527 patch->nsubspaces = nsubspaces; 528 patch->totalDofsPerCell = 0; 529 for (i = 0; i < nsubspaces; ++i) { 530 PetscCall(DMGetLocalSection(dms[i], &patch->dofSection[i])); 531 PetscCall(PetscObjectReference((PetscObject) patch->dofSection[i])); 532 PetscCall(DMGetSectionSF(dms[i], &sfs[i])); 533 patch->bs[i] = bs[i]; 534 patch->nodesPerCell[i] = nodesPerCell[i]; 535 patch->totalDofsPerCell += nodesPerCell[i]*bs[i]; 536 PetscCall(PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i])); 537 for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j]; 538 patch->subspaceOffsets[i] = subspaceOffsets[i]; 539 } 540 PetscCall(PCPatchCreateDefaultSF_Private(pc, nsubspaces, sfs, patch->bs)); 541 PetscCall(PetscFree(sfs)); 542 543 patch->subspaceOffsets[nsubspaces] = subspaceOffsets[nsubspaces]; 544 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes)); 545 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes)); 546 PetscCall(DMDestroy(&dm)); 547 PetscFunctionReturn(0); 548 } 549 550 /* TODO: Docs */ 551 PetscErrorCode PCPatchSetDiscretisationInfoCombined(PC pc, DM dm, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes) 552 { 553 PC_PATCH *patch = (PC_PATCH *) pc->data; 554 PetscInt cStart, cEnd, i, j; 555 556 PetscFunctionBegin; 557 patch->combined = PETSC_TRUE; 558 PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 559 PetscCall(DMGetNumFields(dm, &patch->nsubspaces)); 560 PetscCall(PetscCalloc1(patch->nsubspaces, &patch->dofSection)); 561 PetscCall(PetscMalloc1(patch->nsubspaces, &patch->bs)); 562 PetscCall(PetscMalloc1(patch->nsubspaces, &patch->nodesPerCell)); 563 PetscCall(PetscMalloc1(patch->nsubspaces, &patch->cellNodeMap)); 564 PetscCall(PetscCalloc1(patch->nsubspaces+1, &patch->subspaceOffsets)); 565 PetscCall(DMGetLocalSection(dm, &patch->dofSection[0])); 566 PetscCall(PetscObjectReference((PetscObject) patch->dofSection[0])); 567 PetscCall(PetscSectionGetStorageSize(patch->dofSection[0], &patch->subspaceOffsets[patch->nsubspaces])); 568 patch->totalDofsPerCell = 0; 569 for (i = 0; i < patch->nsubspaces; ++i) { 570 patch->bs[i] = 1; 571 patch->nodesPerCell[i] = nodesPerCell[i]; 572 patch->totalDofsPerCell += nodesPerCell[i]; 573 PetscCall(PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i])); 574 for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j]; 575 } 576 PetscCall(DMGetSectionSF(dm, &patch->sectionSF)); 577 PetscCall(PetscObjectReference((PetscObject) patch->sectionSF)); 578 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes)); 579 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes)); 580 PetscFunctionReturn(0); 581 } 582 583 /*@C 584 585 PCPatchSetComputeFunction - Set the callback used to compute patch residuals 586 587 Logically collective on PC 588 589 Input Parameters: 590 + pc - The PC 591 . func - The callback 592 - ctx - The user context 593 594 Calling sequence of func: 595 $ func (PC pc,PetscInt point,Vec x,Vec f,IS cellIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx) 596 597 + pc - The PC 598 . point - The point 599 . x - The input solution (not used in linear problems) 600 . f - The patch residual vector 601 . cellIS - An array of the cell numbers 602 . n - The size of dofsArray 603 . dofsArray - The dofmap for the dofs to be solved for 604 . dofsArrayWithAll - The dofmap for all dofs on the patch 605 - ctx - The user context 606 607 Level: advanced 608 609 Notes: 610 The entries of F (the output residual vector) have been set to zero before the call. 611 612 .seealso: `PCPatchSetComputeOperator()`, `PCPatchGetComputeOperator()`, `PCPatchSetDiscretisationInfo()`, `PCPatchSetComputeFunctionInteriorFacets()` 613 @*/ 614 PetscErrorCode PCPatchSetComputeFunction(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Vec, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx) 615 { 616 PC_PATCH *patch = (PC_PATCH *) pc->data; 617 618 PetscFunctionBegin; 619 patch->usercomputef = func; 620 patch->usercomputefctx = ctx; 621 PetscFunctionReturn(0); 622 } 623 624 /*@C 625 626 PCPatchSetComputeFunctionInteriorFacets - Set the callback used to compute facet integrals for patch residuals 627 628 Logically collective on PC 629 630 Input Parameters: 631 + pc - The PC 632 . func - The callback 633 - ctx - The user context 634 635 Calling sequence of func: 636 $ func (PC pc,PetscInt point,Vec x,Vec f,IS facetIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx) 637 638 + pc - The PC 639 . point - The point 640 . x - The input solution (not used in linear problems) 641 . f - The patch residual vector 642 . facetIS - An array of the facet numbers 643 . n - The size of dofsArray 644 . dofsArray - The dofmap for the dofs to be solved for 645 . dofsArrayWithAll - The dofmap for all dofs on the patch 646 - ctx - The user context 647 648 Level: advanced 649 650 Notes: 651 The entries of F (the output residual vector) have been set to zero before the call. 652 653 .seealso: `PCPatchSetComputeOperator()`, `PCPatchGetComputeOperator()`, `PCPatchSetDiscretisationInfo()`, `PCPatchSetComputeFunction()` 654 @*/ 655 PetscErrorCode PCPatchSetComputeFunctionInteriorFacets(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Vec, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx) 656 { 657 PC_PATCH *patch = (PC_PATCH *) pc->data; 658 659 PetscFunctionBegin; 660 patch->usercomputefintfacet = func; 661 patch->usercomputefintfacetctx = ctx; 662 PetscFunctionReturn(0); 663 } 664 665 /*@C 666 667 PCPatchSetComputeOperator - Set the callback used to compute patch matrices 668 669 Logically collective on PC 670 671 Input Parameters: 672 + pc - The PC 673 . func - The callback 674 - ctx - The user context 675 676 Calling sequence of func: 677 $ func (PC pc,PetscInt point,Vec x,Mat mat,IS facetIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx) 678 679 + pc - The PC 680 . point - The point 681 . x - The input solution (not used in linear problems) 682 . mat - The patch matrix 683 . cellIS - An array of the cell numbers 684 . n - The size of dofsArray 685 . dofsArray - The dofmap for the dofs to be solved for 686 . dofsArrayWithAll - The dofmap for all dofs on the patch 687 - ctx - The user context 688 689 Level: advanced 690 691 Notes: 692 The matrix entries have been set to zero before the call. 693 694 .seealso: `PCPatchGetComputeOperator()`, `PCPatchSetComputeFunction()`, `PCPatchSetDiscretisationInfo()` 695 @*/ 696 PetscErrorCode PCPatchSetComputeOperator(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Mat, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx) 697 { 698 PC_PATCH *patch = (PC_PATCH *) pc->data; 699 700 PetscFunctionBegin; 701 patch->usercomputeop = func; 702 patch->usercomputeopctx = ctx; 703 PetscFunctionReturn(0); 704 } 705 706 /*@C 707 708 PCPatchSetComputeOperatorInteriorFacets - Set the callback used to compute facet integrals for patch matrices 709 710 Logically collective on PC 711 712 Input Parameters: 713 + pc - The PC 714 . func - The callback 715 - ctx - The user context 716 717 Calling sequence of func: 718 $ func (PC pc,PetscInt point,Vec x,Mat mat,IS facetIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx) 719 720 + pc - The PC 721 . point - The point 722 . x - The input solution (not used in linear problems) 723 . mat - The patch matrix 724 . facetIS - An array of the facet numbers 725 . n - The size of dofsArray 726 . dofsArray - The dofmap for the dofs to be solved for 727 . dofsArrayWithAll - The dofmap for all dofs on the patch 728 - ctx - The user context 729 730 Level: advanced 731 732 Notes: 733 The matrix entries have been set to zero before the call. 734 735 .seealso: `PCPatchGetComputeOperator()`, `PCPatchSetComputeFunction()`, `PCPatchSetDiscretisationInfo()` 736 @*/ 737 PetscErrorCode PCPatchSetComputeOperatorInteriorFacets(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Mat, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx) 738 { 739 PC_PATCH *patch = (PC_PATCH *) pc->data; 740 741 PetscFunctionBegin; 742 patch->usercomputeopintfacet = func; 743 patch->usercomputeopintfacetctx = ctx; 744 PetscFunctionReturn(0); 745 } 746 747 /* On entry, ht contains the topological entities whose dofs we are responsible for solving for; 748 on exit, cht contains all the topological entities we need to compute their residuals. 749 In full generality this should incorporate knowledge of the sparsity pattern of the matrix; 750 here we assume a standard FE sparsity pattern.*/ 751 /* TODO: Use DMPlexGetAdjacency() */ 752 static PetscErrorCode PCPatchCompleteCellPatch(PC pc, PetscHSetI ht, PetscHSetI cht) 753 { 754 DM dm, plex; 755 PC_PATCH *patch = (PC_PATCH *) pc->data; 756 PetscHashIter hi; 757 PetscInt point; 758 PetscInt *star = NULL, *closure = NULL; 759 PetscInt ignoredim, iStart = 0, iEnd = -1, starSize, closureSize, si, ci; 760 PetscInt *fStar = NULL, *fClosure = NULL; 761 PetscInt fBegin, fEnd, fsi, fci, fStarSize, fClosureSize; 762 763 PetscFunctionBegin; 764 PetscCall(PCGetDM(pc, &dm)); 765 PetscCall(DMConvert(dm, DMPLEX, &plex)); 766 dm = plex; 767 PetscCall(DMPlexGetHeightStratum(dm, 1, &fBegin, &fEnd)); 768 PetscCall(PCPatchGetIgnoreDim(pc, &ignoredim)); 769 if (ignoredim >= 0) PetscCall(DMPlexGetDepthStratum(dm, ignoredim, &iStart, &iEnd)); 770 PetscCall(PetscHSetIClear(cht)); 771 PetscHashIterBegin(ht, hi); 772 while (!PetscHashIterAtEnd(ht, hi)) { 773 774 PetscHashIterGetKey(ht, hi, point); 775 PetscHashIterNext(ht, hi); 776 777 /* Loop over all the cells that this point connects to */ 778 PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star)); 779 for (si = 0; si < starSize*2; si += 2) { 780 const PetscInt ownedpoint = star[si]; 781 /* TODO Check for point in cht before running through closure again */ 782 /* now loop over all entities in the closure of that cell */ 783 PetscCall(DMPlexGetTransitiveClosure(dm, ownedpoint, PETSC_TRUE, &closureSize, &closure)); 784 for (ci = 0; ci < closureSize*2; ci += 2) { 785 const PetscInt seenpoint = closure[ci]; 786 if (ignoredim >= 0 && seenpoint >= iStart && seenpoint < iEnd) continue; 787 PetscCall(PetscHSetIAdd(cht, seenpoint)); 788 /* Facet integrals couple dofs across facets, so in that case for each of 789 * the facets we need to add all dofs on the other side of the facet to 790 * the seen dofs. */ 791 if (patch->usercomputeopintfacet) { 792 if (fBegin <= seenpoint && seenpoint < fEnd) { 793 PetscCall(DMPlexGetTransitiveClosure(dm, seenpoint, PETSC_FALSE, &fStarSize, &fStar)); 794 for (fsi = 0; fsi < fStarSize*2; fsi += 2) { 795 PetscCall(DMPlexGetTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, &fClosureSize, &fClosure)); 796 for (fci = 0; fci < fClosureSize*2; fci += 2) { 797 PetscCall(PetscHSetIAdd(cht, fClosure[fci])); 798 } 799 PetscCall(DMPlexRestoreTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, NULL, &fClosure)); 800 } 801 PetscCall(DMPlexRestoreTransitiveClosure(dm, seenpoint, PETSC_FALSE, NULL, &fStar)); 802 } 803 } 804 } 805 PetscCall(DMPlexRestoreTransitiveClosure(dm, ownedpoint, PETSC_TRUE, NULL, &closure)); 806 } 807 PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, NULL, &star)); 808 } 809 PetscCall(DMDestroy(&dm)); 810 PetscFunctionReturn(0); 811 } 812 813 static PetscErrorCode PCPatchGetGlobalDofs(PC pc, PetscSection dofSection[], PetscInt f, PetscBool combined, PetscInt p, PetscInt *dof, PetscInt *off) 814 { 815 PetscFunctionBegin; 816 if (combined) { 817 if (f < 0) { 818 if (dof) PetscCall(PetscSectionGetDof(dofSection[0], p, dof)); 819 if (off) PetscCall(PetscSectionGetOffset(dofSection[0], p, off)); 820 } else { 821 if (dof) PetscCall(PetscSectionGetFieldDof(dofSection[0], p, f, dof)); 822 if (off) PetscCall(PetscSectionGetFieldOffset(dofSection[0], p, f, off)); 823 } 824 } else { 825 if (f < 0) { 826 PC_PATCH *patch = (PC_PATCH *) pc->data; 827 PetscInt fdof, g; 828 829 if (dof) { 830 *dof = 0; 831 for (g = 0; g < patch->nsubspaces; ++g) { 832 PetscCall(PetscSectionGetDof(dofSection[g], p, &fdof)); 833 *dof += fdof; 834 } 835 } 836 if (off) { 837 *off = 0; 838 for (g = 0; g < patch->nsubspaces; ++g) { 839 PetscCall(PetscSectionGetOffset(dofSection[g], p, &fdof)); 840 *off += fdof; 841 } 842 } 843 } else { 844 if (dof) PetscCall(PetscSectionGetDof(dofSection[f], p, dof)); 845 if (off) PetscCall(PetscSectionGetOffset(dofSection[f], p, off)); 846 } 847 } 848 PetscFunctionReturn(0); 849 } 850 851 /* Given a hash table with a set of topological entities (pts), compute the degrees of 852 freedom in global concatenated numbering on those entities. 853 For Vanka smoothing, this needs to do something special: ignore dofs of the 854 constraint subspace on entities that aren't the base entity we're building the patch 855 around. */ 856 static PetscErrorCode PCPatchGetPointDofs(PC pc, PetscHSetI pts, PetscHSetI dofs, PetscInt base, PetscHSetI* subspaces_to_exclude) 857 { 858 PC_PATCH *patch = (PC_PATCH *) pc->data; 859 PetscHashIter hi; 860 PetscInt ldof, loff; 861 PetscInt k, p; 862 863 PetscFunctionBegin; 864 PetscCall(PetscHSetIClear(dofs)); 865 for (k = 0; k < patch->nsubspaces; ++k) { 866 PetscInt subspaceOffset = patch->subspaceOffsets[k]; 867 PetscInt bs = patch->bs[k]; 868 PetscInt j, l; 869 870 if (subspaces_to_exclude != NULL) { 871 PetscBool should_exclude_k = PETSC_FALSE; 872 PetscCall(PetscHSetIHas(*subspaces_to_exclude, k, &should_exclude_k)); 873 if (should_exclude_k) { 874 /* only get this subspace dofs at the base entity, not any others */ 875 PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, base, &ldof, &loff)); 876 if (0 == ldof) continue; 877 for (j = loff; j < ldof + loff; ++j) { 878 for (l = 0; l < bs; ++l) { 879 PetscInt dof = bs*j + l + subspaceOffset; 880 PetscCall(PetscHSetIAdd(dofs, dof)); 881 } 882 } 883 continue; /* skip the other dofs of this subspace */ 884 } 885 } 886 887 PetscHashIterBegin(pts, hi); 888 while (!PetscHashIterAtEnd(pts, hi)) { 889 PetscHashIterGetKey(pts, hi, p); 890 PetscHashIterNext(pts, hi); 891 PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, p, &ldof, &loff)); 892 if (0 == ldof) continue; 893 for (j = loff; j < ldof + loff; ++j) { 894 for (l = 0; l < bs; ++l) { 895 PetscInt dof = bs*j + l + subspaceOffset; 896 PetscCall(PetscHSetIAdd(dofs, dof)); 897 } 898 } 899 } 900 } 901 PetscFunctionReturn(0); 902 } 903 904 /* Given two hash tables A and B, compute the keys in B that are not in A, and put them in C */ 905 static PetscErrorCode PCPatchComputeSetDifference_Private(PetscHSetI A, PetscHSetI B, PetscHSetI C) 906 { 907 PetscHashIter hi; 908 PetscInt key; 909 PetscBool flg; 910 911 PetscFunctionBegin; 912 PetscCall(PetscHSetIClear(C)); 913 PetscHashIterBegin(B, hi); 914 while (!PetscHashIterAtEnd(B, hi)) { 915 PetscHashIterGetKey(B, hi, key); 916 PetscHashIterNext(B, hi); 917 PetscCall(PetscHSetIHas(A, key, &flg)); 918 if (!flg) PetscCall(PetscHSetIAdd(C, key)); 919 } 920 PetscFunctionReturn(0); 921 } 922 923 /* 924 * PCPatchCreateCellPatches - create patches. 925 * 926 * Input Parameters: 927 * + dm - The DMPlex object defining the mesh 928 * 929 * Output Parameters: 930 * + cellCounts - Section with counts of cells around each vertex 931 * . cells - IS of the cell point indices of cells in each patch 932 * . pointCounts - Section with counts of cells around each vertex 933 * - point - IS of the cell point indices of cells in each patch 934 */ 935 static PetscErrorCode PCPatchCreateCellPatches(PC pc) 936 { 937 PC_PATCH *patch = (PC_PATCH *) pc->data; 938 DMLabel ghost = NULL; 939 DM dm, plex; 940 PetscHSetI ht=NULL, cht=NULL; 941 PetscSection cellCounts, pointCounts, intFacetCounts, extFacetCounts; 942 PetscInt *cellsArray, *pointsArray, *intFacetsArray, *extFacetsArray, *intFacetsToPatchCell; 943 PetscInt numCells, numPoints, numIntFacets, numExtFacets; 944 const PetscInt *leaves; 945 PetscInt nleaves, pStart, pEnd, cStart, cEnd, vStart, vEnd, fStart, fEnd, v; 946 PetscBool isFiredrake; 947 948 PetscFunctionBegin; 949 /* Used to keep track of the cells in the patch. */ 950 PetscCall(PetscHSetICreate(&ht)); 951 PetscCall(PetscHSetICreate(&cht)); 952 953 PetscCall(PCGetDM(pc, &dm)); 954 PetscCheck(dm,PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONGSTATE, "DM not yet set on patch PC"); 955 PetscCall(DMConvert(dm, DMPLEX, &plex)); 956 dm = plex; 957 PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 958 PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 959 960 if (patch->user_patches) { 961 PetscCall(patch->userpatchconstructionop(pc, &patch->npatch, &patch->userIS, &patch->iterationSet, patch->userpatchconstructctx)); 962 vStart = 0; vEnd = patch->npatch; 963 } else if (patch->ctype == PC_PATCH_PARDECOMP) { 964 vStart = 0; vEnd = 1; 965 } else if (patch->codim < 0) { 966 if (patch->dim < 0) PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd)); 967 else PetscCall(DMPlexGetDepthStratum(dm, patch->dim, &vStart, &vEnd)); 968 } else PetscCall(DMPlexGetHeightStratum(dm, patch->codim, &vStart, &vEnd)); 969 patch->npatch = vEnd - vStart; 970 971 /* These labels mark the owned points. We only create patches around points that this process owns. */ 972 PetscCall(DMHasLabel(dm, "pyop2_ghost", &isFiredrake)); 973 if (isFiredrake) { 974 PetscCall(DMGetLabel(dm, "pyop2_ghost", &ghost)); 975 PetscCall(DMLabelCreateIndex(ghost, pStart, pEnd)); 976 } else { 977 PetscSF sf; 978 979 PetscCall(DMGetPointSF(dm, &sf)); 980 PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL)); 981 nleaves = PetscMax(nleaves, 0); 982 } 983 984 PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->cellCounts)); 985 PetscCall(PetscObjectSetName((PetscObject) patch->cellCounts, "Patch Cell Layout")); 986 cellCounts = patch->cellCounts; 987 PetscCall(PetscSectionSetChart(cellCounts, vStart, vEnd)); 988 PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->pointCounts)); 989 PetscCall(PetscObjectSetName((PetscObject) patch->pointCounts, "Patch Point Layout")); 990 pointCounts = patch->pointCounts; 991 PetscCall(PetscSectionSetChart(pointCounts, vStart, vEnd)); 992 PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->extFacetCounts)); 993 PetscCall(PetscObjectSetName((PetscObject) patch->extFacetCounts, "Patch Exterior Facet Layout")); 994 extFacetCounts = patch->extFacetCounts; 995 PetscCall(PetscSectionSetChart(extFacetCounts, vStart, vEnd)); 996 PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->intFacetCounts)); 997 PetscCall(PetscObjectSetName((PetscObject) patch->intFacetCounts, "Patch Interior Facet Layout")); 998 intFacetCounts = patch->intFacetCounts; 999 PetscCall(PetscSectionSetChart(intFacetCounts, vStart, vEnd)); 1000 /* Count cells and points in the patch surrounding each entity */ 1001 PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd)); 1002 for (v = vStart; v < vEnd; ++v) { 1003 PetscHashIter hi; 1004 PetscInt chtSize, loc = -1; 1005 PetscBool flg; 1006 1007 if (!patch->user_patches && patch->ctype != PC_PATCH_PARDECOMP) { 1008 if (ghost) PetscCall(DMLabelHasPoint(ghost, v, &flg)); 1009 else {PetscCall(PetscFindInt(v, nleaves, leaves, &loc)); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;} 1010 /* Not an owned entity, don't make a cell patch. */ 1011 if (flg) continue; 1012 } 1013 1014 PetscCall(patch->patchconstructop((void *) patch, dm, v, ht)); 1015 PetscCall(PCPatchCompleteCellPatch(pc, ht, cht)); 1016 PetscCall(PetscHSetIGetSize(cht, &chtSize)); 1017 /* empty patch, continue */ 1018 if (chtSize == 0) continue; 1019 1020 /* safe because size(cht) > 0 from above */ 1021 PetscHashIterBegin(cht, hi); 1022 while (!PetscHashIterAtEnd(cht, hi)) { 1023 PetscInt point, pdof; 1024 1025 PetscHashIterGetKey(cht, hi, point); 1026 if (fStart <= point && point < fEnd) { 1027 const PetscInt *support; 1028 PetscInt supportSize, p; 1029 PetscBool interior = PETSC_TRUE; 1030 PetscCall(DMPlexGetSupport(dm, point, &support)); 1031 PetscCall(DMPlexGetSupportSize(dm, point, &supportSize)); 1032 if (supportSize == 1) { 1033 interior = PETSC_FALSE; 1034 } else { 1035 for (p = 0; p < supportSize; p++) { 1036 PetscBool found; 1037 /* FIXME: can I do this while iterating over cht? */ 1038 PetscCall(PetscHSetIHas(cht, support[p], &found)); 1039 if (!found) { 1040 interior = PETSC_FALSE; 1041 break; 1042 } 1043 } 1044 } 1045 if (interior) { 1046 PetscCall(PetscSectionAddDof(intFacetCounts, v, 1)); 1047 } else { 1048 PetscCall(PetscSectionAddDof(extFacetCounts, v, 1)); 1049 } 1050 } 1051 PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL)); 1052 if (pdof) PetscCall(PetscSectionAddDof(pointCounts, v, 1)); 1053 if (point >= cStart && point < cEnd) PetscCall(PetscSectionAddDof(cellCounts, v, 1)); 1054 PetscHashIterNext(cht, hi); 1055 } 1056 } 1057 if (isFiredrake) PetscCall(DMLabelDestroyIndex(ghost)); 1058 1059 PetscCall(PetscSectionSetUp(cellCounts)); 1060 PetscCall(PetscSectionGetStorageSize(cellCounts, &numCells)); 1061 PetscCall(PetscMalloc1(numCells, &cellsArray)); 1062 PetscCall(PetscSectionSetUp(pointCounts)); 1063 PetscCall(PetscSectionGetStorageSize(pointCounts, &numPoints)); 1064 PetscCall(PetscMalloc1(numPoints, &pointsArray)); 1065 1066 PetscCall(PetscSectionSetUp(intFacetCounts)); 1067 PetscCall(PetscSectionSetUp(extFacetCounts)); 1068 PetscCall(PetscSectionGetStorageSize(intFacetCounts, &numIntFacets)); 1069 PetscCall(PetscSectionGetStorageSize(extFacetCounts, &numExtFacets)); 1070 PetscCall(PetscMalloc1(numIntFacets, &intFacetsArray)); 1071 PetscCall(PetscMalloc1(numIntFacets*2, &intFacetsToPatchCell)); 1072 PetscCall(PetscMalloc1(numExtFacets, &extFacetsArray)); 1073 1074 /* Now that we know how much space we need, run through again and actually remember the cells. */ 1075 for (v = vStart; v < vEnd; v++) { 1076 PetscHashIter hi; 1077 PetscInt dof, off, cdof, coff, efdof, efoff, ifdof, ifoff, pdof, n = 0, cn = 0, ifn = 0, efn = 0; 1078 1079 PetscCall(PetscSectionGetDof(pointCounts, v, &dof)); 1080 PetscCall(PetscSectionGetOffset(pointCounts, v, &off)); 1081 PetscCall(PetscSectionGetDof(cellCounts, v, &cdof)); 1082 PetscCall(PetscSectionGetOffset(cellCounts, v, &coff)); 1083 PetscCall(PetscSectionGetDof(intFacetCounts, v, &ifdof)); 1084 PetscCall(PetscSectionGetOffset(intFacetCounts, v, &ifoff)); 1085 PetscCall(PetscSectionGetDof(extFacetCounts, v, &efdof)); 1086 PetscCall(PetscSectionGetOffset(extFacetCounts, v, &efoff)); 1087 if (dof <= 0) continue; 1088 PetscCall(patch->patchconstructop((void *) patch, dm, v, ht)); 1089 PetscCall(PCPatchCompleteCellPatch(pc, ht, cht)); 1090 PetscHashIterBegin(cht, hi); 1091 while (!PetscHashIterAtEnd(cht, hi)) { 1092 PetscInt point; 1093 1094 PetscHashIterGetKey(cht, hi, point); 1095 if (fStart <= point && point < fEnd) { 1096 const PetscInt *support; 1097 PetscInt supportSize, p; 1098 PetscBool interior = PETSC_TRUE; 1099 PetscCall(DMPlexGetSupport(dm, point, &support)); 1100 PetscCall(DMPlexGetSupportSize(dm, point, &supportSize)); 1101 if (supportSize == 1) { 1102 interior = PETSC_FALSE; 1103 } else { 1104 for (p = 0; p < supportSize; p++) { 1105 PetscBool found; 1106 /* FIXME: can I do this while iterating over cht? */ 1107 PetscCall(PetscHSetIHas(cht, support[p], &found)); 1108 if (!found) { 1109 interior = PETSC_FALSE; 1110 break; 1111 } 1112 } 1113 } 1114 if (interior) { 1115 intFacetsToPatchCell[2*(ifoff + ifn)] = support[0]; 1116 intFacetsToPatchCell[2*(ifoff + ifn) + 1] = support[1]; 1117 intFacetsArray[ifoff + ifn++] = point; 1118 } else { 1119 extFacetsArray[efoff + efn++] = point; 1120 } 1121 } 1122 PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL)); 1123 if (pdof) {pointsArray[off + n++] = point;} 1124 if (point >= cStart && point < cEnd) {cellsArray[coff + cn++] = point;} 1125 PetscHashIterNext(cht, hi); 1126 } 1127 PetscCheck(ifn == ifdof,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of interior facets in patch %" PetscInt_FMT " is %" PetscInt_FMT ", but should be %" PetscInt_FMT, v, ifn, ifdof); 1128 PetscCheck(efn == efdof,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of exterior facets in patch %" PetscInt_FMT " is %" PetscInt_FMT ", but should be %" PetscInt_FMT, v, efn, efdof); 1129 PetscCheck(cn == cdof,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of cells in patch %" PetscInt_FMT " is %" PetscInt_FMT ", but should be %" PetscInt_FMT, v, cn, cdof); 1130 PetscCheck(n == dof,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of points in patch %" PetscInt_FMT " is %" PetscInt_FMT ", but should be %" PetscInt_FMT, v, n, dof); 1131 1132 for (ifn = 0; ifn < ifdof; ifn++) { 1133 PetscInt cell0 = intFacetsToPatchCell[2*(ifoff + ifn)]; 1134 PetscInt cell1 = intFacetsToPatchCell[2*(ifoff + ifn) + 1]; 1135 PetscBool found0 = PETSC_FALSE, found1 = PETSC_FALSE; 1136 for (n = 0; n < cdof; n++) { 1137 if (!found0 && cell0 == cellsArray[coff + n]) { 1138 intFacetsToPatchCell[2*(ifoff + ifn)] = n; 1139 found0 = PETSC_TRUE; 1140 } 1141 if (!found1 && cell1 == cellsArray[coff + n]) { 1142 intFacetsToPatchCell[2*(ifoff + ifn) + 1] = n; 1143 found1 = PETSC_TRUE; 1144 } 1145 if (found0 && found1) break; 1146 } 1147 PetscCheck(found0 && found1,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Didn't manage to find local point numbers for facet support"); 1148 } 1149 } 1150 PetscCall(PetscHSetIDestroy(&ht)); 1151 PetscCall(PetscHSetIDestroy(&cht)); 1152 1153 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numCells, cellsArray, PETSC_OWN_POINTER, &patch->cells)); 1154 PetscCall(PetscObjectSetName((PetscObject) patch->cells, "Patch Cells")); 1155 if (patch->viewCells) { 1156 PetscCall(ObjectView((PetscObject) patch->cellCounts, patch->viewerCells, patch->formatCells)); 1157 PetscCall(ObjectView((PetscObject) patch->cells, patch->viewerCells, patch->formatCells)); 1158 } 1159 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray, PETSC_OWN_POINTER, &patch->intFacets)); 1160 PetscCall(PetscObjectSetName((PetscObject) patch->intFacets, "Patch Interior Facets")); 1161 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 2*numIntFacets, intFacetsToPatchCell, PETSC_OWN_POINTER, &patch->intFacetsToPatchCell)); 1162 PetscCall(PetscObjectSetName((PetscObject) patch->intFacetsToPatchCell, "Patch Interior Facets local support")); 1163 if (patch->viewIntFacets) { 1164 PetscCall(ObjectView((PetscObject) patch->intFacetCounts, patch->viewerIntFacets, patch->formatIntFacets)); 1165 PetscCall(ObjectView((PetscObject) patch->intFacets, patch->viewerIntFacets, patch->formatIntFacets)); 1166 PetscCall(ObjectView((PetscObject) patch->intFacetsToPatchCell, patch->viewerIntFacets, patch->formatIntFacets)); 1167 } 1168 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numExtFacets, extFacetsArray, PETSC_OWN_POINTER, &patch->extFacets)); 1169 PetscCall(PetscObjectSetName((PetscObject) patch->extFacets, "Patch Exterior Facets")); 1170 if (patch->viewExtFacets) { 1171 PetscCall(ObjectView((PetscObject) patch->extFacetCounts, patch->viewerExtFacets, patch->formatExtFacets)); 1172 PetscCall(ObjectView((PetscObject) patch->extFacets, patch->viewerExtFacets, patch->formatExtFacets)); 1173 } 1174 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints, pointsArray, PETSC_OWN_POINTER, &patch->points)); 1175 PetscCall(PetscObjectSetName((PetscObject) patch->points, "Patch Points")); 1176 if (patch->viewPoints) { 1177 PetscCall(ObjectView((PetscObject) patch->pointCounts, patch->viewerPoints, patch->formatPoints)); 1178 PetscCall(ObjectView((PetscObject) patch->points, patch->viewerPoints, patch->formatPoints)); 1179 } 1180 PetscCall(DMDestroy(&dm)); 1181 PetscFunctionReturn(0); 1182 } 1183 1184 /* 1185 * PCPatchCreateCellPatchDiscretisationInfo - Build the dof maps for cell patches 1186 * 1187 * Input Parameters: 1188 * + dm - The DMPlex object defining the mesh 1189 * . cellCounts - Section with counts of cells around each vertex 1190 * . cells - IS of the cell point indices of cells in each patch 1191 * . cellNumbering - Section mapping plex cell points to Firedrake cell indices. 1192 * . nodesPerCell - number of nodes per cell. 1193 * - cellNodeMap - map from cells to node indices (nodesPerCell * numCells) 1194 * 1195 * Output Parameters: 1196 * + dofs - IS of local dof numbers of each cell in the patch, where local is a patch local numbering 1197 * . gtolCounts - Section with counts of dofs per cell patch 1198 * - gtol - IS mapping from global dofs to local dofs for each patch. 1199 */ 1200 static PetscErrorCode PCPatchCreateCellPatchDiscretisationInfo(PC pc) 1201 { 1202 PC_PATCH *patch = (PC_PATCH *) pc->data; 1203 PetscSection cellCounts = patch->cellCounts; 1204 PetscSection pointCounts = patch->pointCounts; 1205 PetscSection gtolCounts, gtolCountsWithArtificial = NULL, gtolCountsWithAll = NULL; 1206 IS cells = patch->cells; 1207 IS points = patch->points; 1208 PetscSection cellNumbering = patch->cellNumbering; 1209 PetscInt Nf = patch->nsubspaces; 1210 PetscInt numCells, numPoints; 1211 PetscInt numDofs; 1212 PetscInt numGlobalDofs, numGlobalDofsWithArtificial, numGlobalDofsWithAll; 1213 PetscInt totalDofsPerCell = patch->totalDofsPerCell; 1214 PetscInt vStart, vEnd, v; 1215 const PetscInt *cellsArray, *pointsArray; 1216 PetscInt *newCellsArray = NULL; 1217 PetscInt *dofsArray = NULL; 1218 PetscInt *dofsArrayWithArtificial = NULL; 1219 PetscInt *dofsArrayWithAll = NULL; 1220 PetscInt *offsArray = NULL; 1221 PetscInt *offsArrayWithArtificial = NULL; 1222 PetscInt *offsArrayWithAll = NULL; 1223 PetscInt *asmArray = NULL; 1224 PetscInt *asmArrayWithArtificial = NULL; 1225 PetscInt *asmArrayWithAll = NULL; 1226 PetscInt *globalDofsArray = NULL; 1227 PetscInt *globalDofsArrayWithArtificial = NULL; 1228 PetscInt *globalDofsArrayWithAll = NULL; 1229 PetscInt globalIndex = 0; 1230 PetscInt key = 0; 1231 PetscInt asmKey = 0; 1232 DM dm = NULL, plex; 1233 const PetscInt *bcNodes = NULL; 1234 PetscHMapI ht; 1235 PetscHMapI htWithArtificial; 1236 PetscHMapI htWithAll; 1237 PetscHSetI globalBcs; 1238 PetscInt numBcs; 1239 PetscHSetI ownedpts, seenpts, owneddofs, seendofs, artificialbcs; 1240 PetscInt pStart, pEnd, p, i; 1241 char option[PETSC_MAX_PATH_LEN]; 1242 PetscBool isNonlinear; 1243 1244 PetscFunctionBegin; 1245 1246 PetscCall(PCGetDM(pc, &dm)); 1247 PetscCall(DMConvert(dm, DMPLEX, &plex)); 1248 dm = plex; 1249 /* dofcounts section is cellcounts section * dofPerCell */ 1250 PetscCall(PetscSectionGetStorageSize(cellCounts, &numCells)); 1251 PetscCall(PetscSectionGetStorageSize(patch->pointCounts, &numPoints)); 1252 numDofs = numCells * totalDofsPerCell; 1253 PetscCall(PetscMalloc1(numDofs, &dofsArray)); 1254 PetscCall(PetscMalloc1(numPoints*Nf, &offsArray)); 1255 PetscCall(PetscMalloc1(numDofs, &asmArray)); 1256 PetscCall(PetscMalloc1(numCells, &newCellsArray)); 1257 PetscCall(PetscSectionGetChart(cellCounts, &vStart, &vEnd)); 1258 PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCounts)); 1259 gtolCounts = patch->gtolCounts; 1260 PetscCall(PetscSectionSetChart(gtolCounts, vStart, vEnd)); 1261 PetscCall(PetscObjectSetName((PetscObject) patch->gtolCounts, "Patch Global Index Section")); 1262 1263 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1264 PetscCall(PetscMalloc1(numPoints*Nf, &offsArrayWithArtificial)); 1265 PetscCall(PetscMalloc1(numDofs, &asmArrayWithArtificial)); 1266 PetscCall(PetscMalloc1(numDofs, &dofsArrayWithArtificial)); 1267 PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithArtificial)); 1268 gtolCountsWithArtificial = patch->gtolCountsWithArtificial; 1269 PetscCall(PetscSectionSetChart(gtolCountsWithArtificial, vStart, vEnd)); 1270 PetscCall(PetscObjectSetName((PetscObject) patch->gtolCountsWithArtificial, "Patch Global Index Section Including Artificial BCs")); 1271 } 1272 1273 isNonlinear = patch->isNonlinear; 1274 if (isNonlinear) { 1275 PetscCall(PetscMalloc1(numPoints*Nf, &offsArrayWithAll)); 1276 PetscCall(PetscMalloc1(numDofs, &asmArrayWithAll)); 1277 PetscCall(PetscMalloc1(numDofs, &dofsArrayWithAll)); 1278 PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithAll)); 1279 gtolCountsWithAll = patch->gtolCountsWithAll; 1280 PetscCall(PetscSectionSetChart(gtolCountsWithAll, vStart, vEnd)); 1281 PetscCall(PetscObjectSetName((PetscObject) patch->gtolCountsWithAll, "Patch Global Index Section Including All BCs")); 1282 } 1283 1284 /* Outside the patch loop, get the dofs that are globally-enforced Dirichlet 1285 conditions */ 1286 PetscCall(PetscHSetICreate(&globalBcs)); 1287 PetscCall(ISGetIndices(patch->ghostBcNodes, &bcNodes)); 1288 PetscCall(ISGetSize(patch->ghostBcNodes, &numBcs)); 1289 for (i = 0; i < numBcs; ++i) { 1290 PetscCall(PetscHSetIAdd(globalBcs, bcNodes[i])); /* these are already in concatenated numbering */ 1291 } 1292 PetscCall(ISRestoreIndices(patch->ghostBcNodes, &bcNodes)); 1293 PetscCall(ISDestroy(&patch->ghostBcNodes)); /* memory optimisation */ 1294 1295 /* Hash tables for artificial BC construction */ 1296 PetscCall(PetscHSetICreate(&ownedpts)); 1297 PetscCall(PetscHSetICreate(&seenpts)); 1298 PetscCall(PetscHSetICreate(&owneddofs)); 1299 PetscCall(PetscHSetICreate(&seendofs)); 1300 PetscCall(PetscHSetICreate(&artificialbcs)); 1301 1302 PetscCall(ISGetIndices(cells, &cellsArray)); 1303 PetscCall(ISGetIndices(points, &pointsArray)); 1304 PetscCall(PetscHMapICreate(&ht)); 1305 PetscCall(PetscHMapICreate(&htWithArtificial)); 1306 PetscCall(PetscHMapICreate(&htWithAll)); 1307 for (v = vStart; v < vEnd; ++v) { 1308 PetscInt localIndex = 0; 1309 PetscInt localIndexWithArtificial = 0; 1310 PetscInt localIndexWithAll = 0; 1311 PetscInt dof, off, i, j, k, l; 1312 1313 PetscCall(PetscHMapIClear(ht)); 1314 PetscCall(PetscHMapIClear(htWithArtificial)); 1315 PetscCall(PetscHMapIClear(htWithAll)); 1316 PetscCall(PetscSectionGetDof(cellCounts, v, &dof)); 1317 PetscCall(PetscSectionGetOffset(cellCounts, v, &off)); 1318 if (dof <= 0) continue; 1319 1320 /* Calculate the global numbers of the artificial BC dofs here first */ 1321 PetscCall(patch->patchconstructop((void*)patch, dm, v, ownedpts)); 1322 PetscCall(PCPatchCompleteCellPatch(pc, ownedpts, seenpts)); 1323 PetscCall(PCPatchGetPointDofs(pc, ownedpts, owneddofs, v, &patch->subspaces_to_exclude)); 1324 PetscCall(PCPatchGetPointDofs(pc, seenpts, seendofs, v, NULL)); 1325 PetscCall(PCPatchComputeSetDifference_Private(owneddofs, seendofs, artificialbcs)); 1326 if (patch->viewPatches) { 1327 PetscHSetI globalbcdofs; 1328 PetscHashIter hi; 1329 MPI_Comm comm = PetscObjectComm((PetscObject)pc); 1330 1331 PetscCall(PetscHSetICreate(&globalbcdofs)); 1332 PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": owned dofs:\n", v)); 1333 PetscHashIterBegin(owneddofs, hi); 1334 while (!PetscHashIterAtEnd(owneddofs, hi)) { 1335 PetscInt globalDof; 1336 1337 PetscHashIterGetKey(owneddofs, hi, globalDof); 1338 PetscHashIterNext(owneddofs, hi); 1339 PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof)); 1340 } 1341 PetscCall(PetscSynchronizedPrintf(comm, "\n")); 1342 PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": seen dofs:\n", v)); 1343 PetscHashIterBegin(seendofs, hi); 1344 while (!PetscHashIterAtEnd(seendofs, hi)) { 1345 PetscInt globalDof; 1346 PetscBool flg; 1347 1348 PetscHashIterGetKey(seendofs, hi, globalDof); 1349 PetscHashIterNext(seendofs, hi); 1350 PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof)); 1351 1352 PetscCall(PetscHSetIHas(globalBcs, globalDof, &flg)); 1353 if (flg) PetscCall(PetscHSetIAdd(globalbcdofs, globalDof)); 1354 } 1355 PetscCall(PetscSynchronizedPrintf(comm, "\n")); 1356 PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": global BCs:\n", v)); 1357 PetscCall(PetscHSetIGetSize(globalbcdofs, &numBcs)); 1358 if (numBcs > 0) { 1359 PetscHashIterBegin(globalbcdofs, hi); 1360 while (!PetscHashIterAtEnd(globalbcdofs, hi)) { 1361 PetscInt globalDof; 1362 PetscHashIterGetKey(globalbcdofs, hi, globalDof); 1363 PetscHashIterNext(globalbcdofs, hi); 1364 PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof)); 1365 } 1366 } 1367 PetscCall(PetscSynchronizedPrintf(comm, "\n")); 1368 PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": artificial BCs:\n", v)); 1369 PetscCall(PetscHSetIGetSize(artificialbcs, &numBcs)); 1370 if (numBcs > 0) { 1371 PetscHashIterBegin(artificialbcs, hi); 1372 while (!PetscHashIterAtEnd(artificialbcs, hi)) { 1373 PetscInt globalDof; 1374 PetscHashIterGetKey(artificialbcs, hi, globalDof); 1375 PetscHashIterNext(artificialbcs, hi); 1376 PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof)); 1377 } 1378 } 1379 PetscCall(PetscSynchronizedPrintf(comm, "\n\n")); 1380 PetscCall(PetscHSetIDestroy(&globalbcdofs)); 1381 } 1382 for (k = 0; k < patch->nsubspaces; ++k) { 1383 const PetscInt *cellNodeMap = patch->cellNodeMap[k]; 1384 PetscInt nodesPerCell = patch->nodesPerCell[k]; 1385 PetscInt subspaceOffset = patch->subspaceOffsets[k]; 1386 PetscInt bs = patch->bs[k]; 1387 1388 for (i = off; i < off + dof; ++i) { 1389 /* Walk over the cells in this patch. */ 1390 const PetscInt c = cellsArray[i]; 1391 PetscInt cell = c; 1392 1393 /* TODO Change this to an IS */ 1394 if (cellNumbering) { 1395 PetscCall(PetscSectionGetDof(cellNumbering, c, &cell)); 1396 PetscCheck(cell > 0,PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_OUTOFRANGE, "Cell %" PetscInt_FMT " doesn't appear in cell numbering map", c); 1397 PetscCall(PetscSectionGetOffset(cellNumbering, c, &cell)); 1398 } 1399 newCellsArray[i] = cell; 1400 for (j = 0; j < nodesPerCell; ++j) { 1401 /* For each global dof, map it into contiguous local storage. */ 1402 const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + subspaceOffset; 1403 /* finally, loop over block size */ 1404 for (l = 0; l < bs; ++l) { 1405 PetscInt localDof; 1406 PetscBool isGlobalBcDof, isArtificialBcDof; 1407 1408 /* first, check if this is either a globally enforced or locally enforced BC dof */ 1409 PetscCall(PetscHSetIHas(globalBcs, globalDof + l, &isGlobalBcDof)); 1410 PetscCall(PetscHSetIHas(artificialbcs, globalDof + l, &isArtificialBcDof)); 1411 1412 /* if it's either, don't ever give it a local dof number */ 1413 if (isGlobalBcDof || isArtificialBcDof) { 1414 dofsArray[globalIndex] = -1; /* don't use this in assembly in this patch */ 1415 } else { 1416 PetscCall(PetscHMapIGet(ht, globalDof + l, &localDof)); 1417 if (localDof == -1) { 1418 localDof = localIndex++; 1419 PetscCall(PetscHMapISet(ht, globalDof + l, localDof)); 1420 } 1421 PetscCheck(globalIndex < numDofs,PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %" PetscInt_FMT " than expected %" PetscInt_FMT, globalIndex+1, numDofs); 1422 /* And store. */ 1423 dofsArray[globalIndex] = localDof; 1424 } 1425 1426 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1427 if (isGlobalBcDof) { 1428 dofsArrayWithArtificial[globalIndex] = -1; /* don't use this in assembly in this patch */ 1429 } else { 1430 PetscCall(PetscHMapIGet(htWithArtificial, globalDof + l, &localDof)); 1431 if (localDof == -1) { 1432 localDof = localIndexWithArtificial++; 1433 PetscCall(PetscHMapISet(htWithArtificial, globalDof + l, localDof)); 1434 } 1435 PetscCheck(globalIndex < numDofs,PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %" PetscInt_FMT " than expected %" PetscInt_FMT, globalIndex+1, numDofs); 1436 /* And store.*/ 1437 dofsArrayWithArtificial[globalIndex] = localDof; 1438 } 1439 } 1440 1441 if (isNonlinear) { 1442 /* Build the dofmap for the function space with _all_ dofs, 1443 including those in any kind of boundary condition */ 1444 PetscCall(PetscHMapIGet(htWithAll, globalDof + l, &localDof)); 1445 if (localDof == -1) { 1446 localDof = localIndexWithAll++; 1447 PetscCall(PetscHMapISet(htWithAll, globalDof + l, localDof)); 1448 } 1449 PetscCheck(globalIndex < numDofs,PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %" PetscInt_FMT " than expected %" PetscInt_FMT, globalIndex+1, numDofs); 1450 /* And store.*/ 1451 dofsArrayWithAll[globalIndex] = localDof; 1452 } 1453 globalIndex++; 1454 } 1455 } 1456 } 1457 } 1458 /*How many local dofs in this patch? */ 1459 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1460 PetscCall(PetscHMapIGetSize(htWithArtificial, &dof)); 1461 PetscCall(PetscSectionSetDof(gtolCountsWithArtificial, v, dof)); 1462 } 1463 if (isNonlinear) { 1464 PetscCall(PetscHMapIGetSize(htWithAll, &dof)); 1465 PetscCall(PetscSectionSetDof(gtolCountsWithAll, v, dof)); 1466 } 1467 PetscCall(PetscHMapIGetSize(ht, &dof)); 1468 PetscCall(PetscSectionSetDof(gtolCounts, v, dof)); 1469 } 1470 1471 PetscCall(DMDestroy(&dm)); 1472 PetscCheck(globalIndex == numDofs,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Expected number of dofs (%" PetscInt_FMT ") doesn't match found number (%" PetscInt_FMT ")", numDofs, globalIndex); 1473 PetscCall(PetscSectionSetUp(gtolCounts)); 1474 PetscCall(PetscSectionGetStorageSize(gtolCounts, &numGlobalDofs)); 1475 PetscCall(PetscMalloc1(numGlobalDofs, &globalDofsArray)); 1476 1477 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1478 PetscCall(PetscSectionSetUp(gtolCountsWithArtificial)); 1479 PetscCall(PetscSectionGetStorageSize(gtolCountsWithArtificial, &numGlobalDofsWithArtificial)); 1480 PetscCall(PetscMalloc1(numGlobalDofsWithArtificial, &globalDofsArrayWithArtificial)); 1481 } 1482 if (isNonlinear) { 1483 PetscCall(PetscSectionSetUp(gtolCountsWithAll)); 1484 PetscCall(PetscSectionGetStorageSize(gtolCountsWithAll, &numGlobalDofsWithAll)); 1485 PetscCall(PetscMalloc1(numGlobalDofsWithAll, &globalDofsArrayWithAll)); 1486 } 1487 /* Now populate the global to local map. This could be merged into the above loop if we were willing to deal with reallocs. */ 1488 for (v = vStart; v < vEnd; ++v) { 1489 PetscHashIter hi; 1490 PetscInt dof, off, Np, ooff, i, j, k, l; 1491 1492 PetscCall(PetscHMapIClear(ht)); 1493 PetscCall(PetscHMapIClear(htWithArtificial)); 1494 PetscCall(PetscHMapIClear(htWithAll)); 1495 PetscCall(PetscSectionGetDof(cellCounts, v, &dof)); 1496 PetscCall(PetscSectionGetOffset(cellCounts, v, &off)); 1497 PetscCall(PetscSectionGetDof(pointCounts, v, &Np)); 1498 PetscCall(PetscSectionGetOffset(pointCounts, v, &ooff)); 1499 if (dof <= 0) continue; 1500 1501 for (k = 0; k < patch->nsubspaces; ++k) { 1502 const PetscInt *cellNodeMap = patch->cellNodeMap[k]; 1503 PetscInt nodesPerCell = patch->nodesPerCell[k]; 1504 PetscInt subspaceOffset = patch->subspaceOffsets[k]; 1505 PetscInt bs = patch->bs[k]; 1506 PetscInt goff; 1507 1508 for (i = off; i < off + dof; ++i) { 1509 /* Reconstruct mapping of global-to-local on this patch. */ 1510 const PetscInt c = cellsArray[i]; 1511 PetscInt cell = c; 1512 1513 if (cellNumbering) PetscCall(PetscSectionGetOffset(cellNumbering, c, &cell)); 1514 for (j = 0; j < nodesPerCell; ++j) { 1515 for (l = 0; l < bs; ++l) { 1516 const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset; 1517 const PetscInt localDof = dofsArray[key]; 1518 if (localDof >= 0) PetscCall(PetscHMapISet(ht, globalDof, localDof)); 1519 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1520 const PetscInt localDofWithArtificial = dofsArrayWithArtificial[key]; 1521 if (localDofWithArtificial >= 0) { 1522 PetscCall(PetscHMapISet(htWithArtificial, globalDof, localDofWithArtificial)); 1523 } 1524 } 1525 if (isNonlinear) { 1526 const PetscInt localDofWithAll = dofsArrayWithAll[key]; 1527 if (localDofWithAll >= 0) { 1528 PetscCall(PetscHMapISet(htWithAll, globalDof, localDofWithAll)); 1529 } 1530 } 1531 key++; 1532 } 1533 } 1534 } 1535 1536 /* Shove it in the output data structure. */ 1537 PetscCall(PetscSectionGetOffset(gtolCounts, v, &goff)); 1538 PetscHashIterBegin(ht, hi); 1539 while (!PetscHashIterAtEnd(ht, hi)) { 1540 PetscInt globalDof, localDof; 1541 1542 PetscHashIterGetKey(ht, hi, globalDof); 1543 PetscHashIterGetVal(ht, hi, localDof); 1544 if (globalDof >= 0) globalDofsArray[goff + localDof] = globalDof; 1545 PetscHashIterNext(ht, hi); 1546 } 1547 1548 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1549 PetscCall(PetscSectionGetOffset(gtolCountsWithArtificial, v, &goff)); 1550 PetscHashIterBegin(htWithArtificial, hi); 1551 while (!PetscHashIterAtEnd(htWithArtificial, hi)) { 1552 PetscInt globalDof, localDof; 1553 PetscHashIterGetKey(htWithArtificial, hi, globalDof); 1554 PetscHashIterGetVal(htWithArtificial, hi, localDof); 1555 if (globalDof >= 0) globalDofsArrayWithArtificial[goff + localDof] = globalDof; 1556 PetscHashIterNext(htWithArtificial, hi); 1557 } 1558 } 1559 if (isNonlinear) { 1560 PetscCall(PetscSectionGetOffset(gtolCountsWithAll, v, &goff)); 1561 PetscHashIterBegin(htWithAll, hi); 1562 while (!PetscHashIterAtEnd(htWithAll, hi)) { 1563 PetscInt globalDof, localDof; 1564 PetscHashIterGetKey(htWithAll, hi, globalDof); 1565 PetscHashIterGetVal(htWithAll, hi, localDof); 1566 if (globalDof >= 0) globalDofsArrayWithAll[goff + localDof] = globalDof; 1567 PetscHashIterNext(htWithAll, hi); 1568 } 1569 } 1570 1571 for (p = 0; p < Np; ++p) { 1572 const PetscInt point = pointsArray[ooff + p]; 1573 PetscInt globalDof, localDof; 1574 1575 PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, point, NULL, &globalDof)); 1576 PetscCall(PetscHMapIGet(ht, globalDof, &localDof)); 1577 offsArray[(ooff + p)*Nf + k] = localDof; 1578 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1579 PetscCall(PetscHMapIGet(htWithArtificial, globalDof, &localDof)); 1580 offsArrayWithArtificial[(ooff + p)*Nf + k] = localDof; 1581 } 1582 if (isNonlinear) { 1583 PetscCall(PetscHMapIGet(htWithAll, globalDof, &localDof)); 1584 offsArrayWithAll[(ooff + p)*Nf + k] = localDof; 1585 } 1586 } 1587 } 1588 1589 PetscCall(PetscHSetIDestroy(&globalBcs)); 1590 PetscCall(PetscHSetIDestroy(&ownedpts)); 1591 PetscCall(PetscHSetIDestroy(&seenpts)); 1592 PetscCall(PetscHSetIDestroy(&owneddofs)); 1593 PetscCall(PetscHSetIDestroy(&seendofs)); 1594 PetscCall(PetscHSetIDestroy(&artificialbcs)); 1595 1596 /* At this point, we have a hash table ht built that maps globalDof -> localDof. 1597 We need to create the dof table laid out cellwise first, then by subspace, 1598 as the assembler assembles cell-wise and we need to stuff the different 1599 contributions of the different function spaces to the right places. So we loop 1600 over cells, then over subspaces. */ 1601 if (patch->nsubspaces > 1) { /* for nsubspaces = 1, data we need is already in dofsArray */ 1602 for (i = off; i < off + dof; ++i) { 1603 const PetscInt c = cellsArray[i]; 1604 PetscInt cell = c; 1605 1606 if (cellNumbering) PetscCall(PetscSectionGetOffset(cellNumbering, c, &cell)); 1607 for (k = 0; k < patch->nsubspaces; ++k) { 1608 const PetscInt *cellNodeMap = patch->cellNodeMap[k]; 1609 PetscInt nodesPerCell = patch->nodesPerCell[k]; 1610 PetscInt subspaceOffset = patch->subspaceOffsets[k]; 1611 PetscInt bs = patch->bs[k]; 1612 1613 for (j = 0; j < nodesPerCell; ++j) { 1614 for (l = 0; l < bs; ++l) { 1615 const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset; 1616 PetscInt localDof; 1617 1618 PetscCall(PetscHMapIGet(ht, globalDof, &localDof)); 1619 /* If it's not in the hash table, i.e. is a BC dof, 1620 then the PetscHSetIMap above gives -1, which matches 1621 exactly the convention for PETSc's matrix assembly to 1622 ignore the dof. So we don't need to do anything here */ 1623 asmArray[asmKey] = localDof; 1624 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1625 PetscCall(PetscHMapIGet(htWithArtificial, globalDof, &localDof)); 1626 asmArrayWithArtificial[asmKey] = localDof; 1627 } 1628 if (isNonlinear) { 1629 PetscCall(PetscHMapIGet(htWithAll, globalDof, &localDof)); 1630 asmArrayWithAll[asmKey] = localDof; 1631 } 1632 asmKey++; 1633 } 1634 } 1635 } 1636 } 1637 } 1638 } 1639 if (1 == patch->nsubspaces) { 1640 PetscCall(PetscArraycpy(asmArray, dofsArray, numDofs)); 1641 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1642 PetscCall(PetscArraycpy(asmArrayWithArtificial, dofsArrayWithArtificial, numDofs)); 1643 } 1644 if (isNonlinear) PetscCall(PetscArraycpy(asmArrayWithAll, dofsArrayWithAll, numDofs)); 1645 } 1646 1647 PetscCall(PetscHMapIDestroy(&ht)); 1648 PetscCall(PetscHMapIDestroy(&htWithArtificial)); 1649 PetscCall(PetscHMapIDestroy(&htWithAll)); 1650 PetscCall(ISRestoreIndices(cells, &cellsArray)); 1651 PetscCall(ISRestoreIndices(points, &pointsArray)); 1652 PetscCall(PetscFree(dofsArray)); 1653 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1654 PetscCall(PetscFree(dofsArrayWithArtificial)); 1655 } 1656 if (isNonlinear) PetscCall(PetscFree(dofsArrayWithAll)); 1657 /* Create placeholder section for map from points to patch dofs */ 1658 PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->patchSection)); 1659 PetscCall(PetscSectionSetNumFields(patch->patchSection, patch->nsubspaces)); 1660 if (patch->combined) { 1661 PetscInt numFields; 1662 PetscCall(PetscSectionGetNumFields(patch->dofSection[0], &numFields)); 1663 PetscCheck(numFields == patch->nsubspaces,PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Mismatch between number of section fields %" PetscInt_FMT " and number of subspaces %" PetscInt_FMT, numFields, patch->nsubspaces); 1664 PetscCall(PetscSectionGetChart(patch->dofSection[0], &pStart, &pEnd)); 1665 PetscCall(PetscSectionSetChart(patch->patchSection, pStart, pEnd)); 1666 for (p = pStart; p < pEnd; ++p) { 1667 PetscInt dof, fdof, f; 1668 1669 PetscCall(PetscSectionGetDof(patch->dofSection[0], p, &dof)); 1670 PetscCall(PetscSectionSetDof(patch->patchSection, p, dof)); 1671 for (f = 0; f < patch->nsubspaces; ++f) { 1672 PetscCall(PetscSectionGetFieldDof(patch->dofSection[0], p, f, &fdof)); 1673 PetscCall(PetscSectionSetFieldDof(patch->patchSection, p, f, fdof)); 1674 } 1675 } 1676 } else { 1677 PetscInt pStartf, pEndf, f; 1678 pStart = PETSC_MAX_INT; 1679 pEnd = PETSC_MIN_INT; 1680 for (f = 0; f < patch->nsubspaces; ++f) { 1681 PetscCall(PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf)); 1682 pStart = PetscMin(pStart, pStartf); 1683 pEnd = PetscMax(pEnd, pEndf); 1684 } 1685 PetscCall(PetscSectionSetChart(patch->patchSection, pStart, pEnd)); 1686 for (f = 0; f < patch->nsubspaces; ++f) { 1687 PetscCall(PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf)); 1688 for (p = pStartf; p < pEndf; ++p) { 1689 PetscInt fdof; 1690 PetscCall(PetscSectionGetDof(patch->dofSection[f], p, &fdof)); 1691 PetscCall(PetscSectionAddDof(patch->patchSection, p, fdof)); 1692 PetscCall(PetscSectionSetFieldDof(patch->patchSection, p, f, fdof)); 1693 } 1694 } 1695 } 1696 PetscCall(PetscSectionSetUp(patch->patchSection)); 1697 PetscCall(PetscSectionSetUseFieldOffsets(patch->patchSection, PETSC_TRUE)); 1698 /* Replace cell indices with firedrake-numbered ones. */ 1699 PetscCall(ISGeneralSetIndices(cells, numCells, (const PetscInt *) newCellsArray, PETSC_OWN_POINTER)); 1700 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofs, globalDofsArray, PETSC_OWN_POINTER, &patch->gtol)); 1701 PetscCall(PetscObjectSetName((PetscObject) patch->gtol, "Global Indices")); 1702 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_g2l_view", patch->classname)); 1703 PetscCall(PetscSectionViewFromOptions(patch->gtolCounts, (PetscObject) pc, option)); 1704 PetscCall(ISViewFromOptions(patch->gtol, (PetscObject) pc, option)); 1705 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArray, PETSC_OWN_POINTER, &patch->dofs)); 1706 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArray, PETSC_OWN_POINTER, &patch->offs)); 1707 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1708 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithArtificial, globalDofsArrayWithArtificial, PETSC_OWN_POINTER, &patch->gtolWithArtificial)); 1709 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithArtificial, PETSC_OWN_POINTER, &patch->dofsWithArtificial)); 1710 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithArtificial, PETSC_OWN_POINTER, &patch->offsWithArtificial)); 1711 } 1712 if (isNonlinear) { 1713 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithAll, globalDofsArrayWithAll, PETSC_OWN_POINTER, &patch->gtolWithAll)); 1714 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithAll, PETSC_OWN_POINTER, &patch->dofsWithAll)); 1715 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithAll, PETSC_OWN_POINTER, &patch->offsWithAll)); 1716 } 1717 PetscFunctionReturn(0); 1718 } 1719 1720 static PetscErrorCode PCPatchCreateMatrix_Private(PC pc, PetscInt point, Mat *mat, PetscBool withArtificial) 1721 { 1722 PC_PATCH *patch = (PC_PATCH *) pc->data; 1723 PetscBool flg; 1724 PetscInt csize, rsize; 1725 const char *prefix = NULL; 1726 1727 PetscFunctionBegin; 1728 if (withArtificial) { 1729 /* would be nice if we could create a rectangular matrix of size numDofsWithArtificial x numDofs here */ 1730 PetscInt pStart; 1731 PetscCall(PetscSectionGetChart(patch->gtolCountsWithArtificial, &pStart, NULL)); 1732 PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, point + pStart, &rsize)); 1733 csize = rsize; 1734 } else { 1735 PetscInt pStart; 1736 PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, NULL)); 1737 PetscCall(PetscSectionGetDof(patch->gtolCounts, point + pStart, &rsize)); 1738 csize = rsize; 1739 } 1740 1741 PetscCall(MatCreate(PETSC_COMM_SELF, mat)); 1742 PetscCall(PCGetOptionsPrefix(pc, &prefix)); 1743 PetscCall(MatSetOptionsPrefix(*mat, prefix)); 1744 PetscCall(MatAppendOptionsPrefix(*mat, "pc_patch_sub_")); 1745 if (patch->sub_mat_type) PetscCall(MatSetType(*mat, patch->sub_mat_type)); 1746 else if (!patch->sub_mat_type) PetscCall(MatSetType(*mat, MATDENSE)); 1747 PetscCall(MatSetSizes(*mat, rsize, csize, rsize, csize)); 1748 PetscCall(PetscObjectTypeCompare((PetscObject) *mat, MATDENSE, &flg)); 1749 if (!flg) PetscCall(PetscObjectTypeCompare((PetscObject)*mat, MATSEQDENSE, &flg)); 1750 /* Sparse patch matrices */ 1751 if (!flg) { 1752 PetscBT bt; 1753 PetscInt *dnnz = NULL; 1754 const PetscInt *dofsArray = NULL; 1755 PetscInt pStart, pEnd, ncell, offset, c, i, j; 1756 1757 if (withArtificial) { 1758 PetscCall(ISGetIndices(patch->dofsWithArtificial, &dofsArray)); 1759 } else { 1760 PetscCall(ISGetIndices(patch->dofs, &dofsArray)); 1761 } 1762 PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd)); 1763 point += pStart; 1764 PetscCheck(point < pEnd,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %" PetscInt_FMT " not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, pStart, pEnd); 1765 PetscCall(PetscSectionGetDof(patch->cellCounts, point, &ncell)); 1766 PetscCall(PetscSectionGetOffset(patch->cellCounts, point, &offset)); 1767 PetscCall(PetscLogEventBegin(PC_Patch_Prealloc, pc, 0, 0, 0)); 1768 /* A PetscBT uses N^2 bits to store the sparsity pattern on a 1769 * patch. This is probably OK if the patches are not too big, 1770 * but uses too much memory. We therefore switch based on rsize. */ 1771 if (rsize < 3000) { /* FIXME: I picked this switch value out of my hat */ 1772 PetscScalar *zeroes; 1773 PetscInt rows; 1774 1775 PetscCall(PetscCalloc1(rsize, &dnnz)); 1776 PetscCall(PetscBTCreate(rsize*rsize, &bt)); 1777 for (c = 0; c < ncell; ++c) { 1778 const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell; 1779 for (i = 0; i < patch->totalDofsPerCell; ++i) { 1780 const PetscInt row = idx[i]; 1781 if (row < 0) continue; 1782 for (j = 0; j < patch->totalDofsPerCell; ++j) { 1783 const PetscInt col = idx[j]; 1784 const PetscInt key = row*rsize + col; 1785 if (col < 0) continue; 1786 if (!PetscBTLookupSet(bt, key)) ++dnnz[row]; 1787 } 1788 } 1789 } 1790 1791 if (patch->usercomputeopintfacet) { 1792 const PetscInt *intFacetsArray = NULL; 1793 PetscInt i, numIntFacets, intFacetOffset; 1794 const PetscInt *facetCells = NULL; 1795 1796 PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets)); 1797 PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset)); 1798 PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells)); 1799 PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray)); 1800 for (i = 0; i < numIntFacets; i++) { 1801 const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0]; 1802 const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1]; 1803 PetscInt celli, cellj; 1804 1805 for (celli = 0; celli < patch->totalDofsPerCell; celli++) { 1806 const PetscInt row = dofsArray[(offset + cell0)*patch->totalDofsPerCell + celli]; 1807 if (row < 0) continue; 1808 for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) { 1809 const PetscInt col = dofsArray[(offset + cell1)*patch->totalDofsPerCell + cellj]; 1810 const PetscInt key = row*rsize + col; 1811 if (col < 0) continue; 1812 if (!PetscBTLookupSet(bt, key)) ++dnnz[row]; 1813 } 1814 } 1815 1816 for (celli = 0; celli < patch->totalDofsPerCell; celli++) { 1817 const PetscInt row = dofsArray[(offset + cell1)*patch->totalDofsPerCell + celli]; 1818 if (row < 0) continue; 1819 for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) { 1820 const PetscInt col = dofsArray[(offset + cell0)*patch->totalDofsPerCell + cellj]; 1821 const PetscInt key = row*rsize + col; 1822 if (col < 0) continue; 1823 if (!PetscBTLookupSet(bt, key)) ++dnnz[row]; 1824 } 1825 } 1826 } 1827 } 1828 PetscCall(PetscBTDestroy(&bt)); 1829 PetscCall(MatXAIJSetPreallocation(*mat, 1, dnnz, NULL, NULL, NULL)); 1830 PetscCall(PetscFree(dnnz)); 1831 1832 PetscCall(PetscCalloc1(patch->totalDofsPerCell*patch->totalDofsPerCell, &zeroes)); 1833 for (c = 0; c < ncell; ++c) { 1834 const PetscInt *idx = &dofsArray[(offset + c)*patch->totalDofsPerCell]; 1835 PetscCall(MatSetValues(*mat, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, zeroes, INSERT_VALUES)); 1836 } 1837 PetscCall(MatGetLocalSize(*mat, &rows, NULL)); 1838 for (i = 0; i < rows; ++i) { 1839 PetscCall(MatSetValues(*mat, 1, &i, 1, &i, zeroes, INSERT_VALUES)); 1840 } 1841 1842 if (patch->usercomputeopintfacet) { 1843 const PetscInt *intFacetsArray = NULL; 1844 PetscInt i, numIntFacets, intFacetOffset; 1845 const PetscInt *facetCells = NULL; 1846 1847 PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets)); 1848 PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset)); 1849 PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells)); 1850 PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray)); 1851 for (i = 0; i < numIntFacets; i++) { 1852 const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0]; 1853 const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1]; 1854 const PetscInt *cell0idx = &dofsArray[(offset + cell0)*patch->totalDofsPerCell]; 1855 const PetscInt *cell1idx = &dofsArray[(offset + cell1)*patch->totalDofsPerCell]; 1856 PetscCall(MatSetValues(*mat, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, zeroes, INSERT_VALUES)); 1857 PetscCall(MatSetValues(*mat, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, zeroes, INSERT_VALUES)); 1858 } 1859 } 1860 1861 PetscCall(MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY)); 1862 PetscCall(MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY)); 1863 1864 PetscCall(PetscFree(zeroes)); 1865 1866 } else { /* rsize too big, use MATPREALLOCATOR */ 1867 Mat preallocator; 1868 PetscScalar* vals; 1869 1870 PetscCall(PetscCalloc1(patch->totalDofsPerCell*patch->totalDofsPerCell, &vals)); 1871 PetscCall(MatCreate(PETSC_COMM_SELF, &preallocator)); 1872 PetscCall(MatSetType(preallocator, MATPREALLOCATOR)); 1873 PetscCall(MatSetSizes(preallocator, rsize, rsize, rsize, rsize)); 1874 PetscCall(MatSetUp(preallocator)); 1875 1876 for (c = 0; c < ncell; ++c) { 1877 const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell; 1878 PetscCall(MatSetValues(preallocator, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, vals, INSERT_VALUES)); 1879 } 1880 1881 if (patch->usercomputeopintfacet) { 1882 const PetscInt *intFacetsArray = NULL; 1883 PetscInt i, numIntFacets, intFacetOffset; 1884 const PetscInt *facetCells = NULL; 1885 1886 PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets)); 1887 PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset)); 1888 PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells)); 1889 PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray)); 1890 for (i = 0; i < numIntFacets; i++) { 1891 const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0]; 1892 const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1]; 1893 const PetscInt *cell0idx = &dofsArray[(offset + cell0)*patch->totalDofsPerCell]; 1894 const PetscInt *cell1idx = &dofsArray[(offset + cell1)*patch->totalDofsPerCell]; 1895 PetscCall(MatSetValues(preallocator, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, vals, INSERT_VALUES)); 1896 PetscCall(MatSetValues(preallocator, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, vals, INSERT_VALUES)); 1897 } 1898 } 1899 1900 PetscCall(PetscFree(vals)); 1901 PetscCall(MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY)); 1902 PetscCall(MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY)); 1903 PetscCall(MatPreallocatorPreallocate(preallocator, PETSC_TRUE, *mat)); 1904 PetscCall(MatDestroy(&preallocator)); 1905 } 1906 PetscCall(PetscLogEventEnd(PC_Patch_Prealloc, pc, 0, 0, 0)); 1907 if (withArtificial) { 1908 PetscCall(ISRestoreIndices(patch->dofsWithArtificial, &dofsArray)); 1909 } else { 1910 PetscCall(ISRestoreIndices(patch->dofs, &dofsArray)); 1911 } 1912 } 1913 PetscCall(MatSetUp(*mat)); 1914 PetscFunctionReturn(0); 1915 } 1916 1917 static PetscErrorCode PCPatchComputeFunction_DMPlex_Private(PC pc, PetscInt patchNum, Vec x, Vec F, IS cellIS, PetscInt n, const PetscInt *l2p, const PetscInt *l2pWithAll, void *ctx) 1918 { 1919 PC_PATCH *patch = (PC_PATCH *) pc->data; 1920 DM dm, plex; 1921 PetscSection s; 1922 const PetscInt *parray, *oarray; 1923 PetscInt Nf = patch->nsubspaces, Np, poff, p, f; 1924 1925 PetscFunctionBegin; 1926 PetscCheck(!patch->precomputeElementTensors,PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Precomputing element tensors not implemented with DMPlex compute operator"); 1927 PetscCall(PCGetDM(pc, &dm)); 1928 PetscCall(DMConvert(dm, DMPLEX, &plex)); 1929 dm = plex; 1930 PetscCall(DMGetLocalSection(dm, &s)); 1931 /* Set offset into patch */ 1932 PetscCall(PetscSectionGetDof(patch->pointCounts, patchNum, &Np)); 1933 PetscCall(PetscSectionGetOffset(patch->pointCounts, patchNum, &poff)); 1934 PetscCall(ISGetIndices(patch->points, &parray)); 1935 PetscCall(ISGetIndices(patch->offs, &oarray)); 1936 for (f = 0; f < Nf; ++f) { 1937 for (p = 0; p < Np; ++p) { 1938 const PetscInt point = parray[poff+p]; 1939 PetscInt dof; 1940 1941 PetscCall(PetscSectionGetFieldDof(patch->patchSection, point, f, &dof)); 1942 PetscCall(PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f])); 1943 if (patch->nsubspaces == 1) PetscCall(PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f])); 1944 else PetscCall(PetscSectionSetOffset(patch->patchSection, point, -1)); 1945 } 1946 } 1947 PetscCall(ISRestoreIndices(patch->points, &parray)); 1948 PetscCall(ISRestoreIndices(patch->offs, &oarray)); 1949 if (patch->viewSection) PetscCall(ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection)); 1950 PetscCall(DMPlexComputeResidual_Patch_Internal(dm, patch->patchSection, cellIS, 0.0, x, NULL, F, ctx)); 1951 PetscCall(DMDestroy(&dm)); 1952 PetscFunctionReturn(0); 1953 } 1954 1955 PetscErrorCode PCPatchComputeFunction_Internal(PC pc, Vec x, Vec F, PetscInt point) 1956 { 1957 PC_PATCH *patch = (PC_PATCH *) pc->data; 1958 const PetscInt *dofsArray; 1959 const PetscInt *dofsArrayWithAll; 1960 const PetscInt *cellsArray; 1961 PetscInt ncell, offset, pStart, pEnd; 1962 1963 PetscFunctionBegin; 1964 PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0)); 1965 PetscCheck(patch->usercomputeop,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback"); 1966 PetscCall(ISGetIndices(patch->dofs, &dofsArray)); 1967 PetscCall(ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll)); 1968 PetscCall(ISGetIndices(patch->cells, &cellsArray)); 1969 PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd)); 1970 1971 point += pStart; 1972 PetscCheck(point < pEnd,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %" PetscInt_FMT " not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, pStart, pEnd); 1973 1974 PetscCall(PetscSectionGetDof(patch->cellCounts, point, &ncell)); 1975 PetscCall(PetscSectionGetOffset(patch->cellCounts, point, &offset)); 1976 if (ncell <= 0) { 1977 PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 1978 PetscFunctionReturn(0); 1979 } 1980 PetscCall(VecSet(F, 0.0)); 1981 PetscStackPush("PCPatch user callback"); 1982 /* Cannot reuse the same IS because the geometry info is being cached in it */ 1983 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS)); 1984 PetscCall(patch->usercomputef(pc, point, x, F, patch->cellIS, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell,dofsArrayWithAll + offset*patch->totalDofsPerCell,patch->usercomputefctx)); 1985 PetscStackPop; 1986 PetscCall(ISDestroy(&patch->cellIS)); 1987 PetscCall(ISRestoreIndices(patch->dofs, &dofsArray)); 1988 PetscCall(ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll)); 1989 PetscCall(ISRestoreIndices(patch->cells, &cellsArray)); 1990 if (patch->viewMatrix) { 1991 char name[PETSC_MAX_PATH_LEN]; 1992 1993 PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch vector for Point %" PetscInt_FMT, point)); 1994 PetscCall(PetscObjectSetName((PetscObject) F, name)); 1995 PetscCall(ObjectView((PetscObject) F, patch->viewerMatrix, patch->formatMatrix)); 1996 } 1997 PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 1998 PetscFunctionReturn(0); 1999 } 2000 2001 static PetscErrorCode PCPatchComputeOperator_DMPlex_Private(PC pc, PetscInt patchNum, Vec x, Mat J, IS cellIS, PetscInt n, const PetscInt *l2p, const PetscInt *l2pWithAll, void *ctx) 2002 { 2003 PC_PATCH *patch = (PC_PATCH *) pc->data; 2004 DM dm, plex; 2005 PetscSection s; 2006 const PetscInt *parray, *oarray; 2007 PetscInt Nf = patch->nsubspaces, Np, poff, p, f; 2008 2009 PetscFunctionBegin; 2010 PetscCall(PCGetDM(pc, &dm)); 2011 PetscCall(DMConvert(dm, DMPLEX, &plex)); 2012 dm = plex; 2013 PetscCall(DMGetLocalSection(dm, &s)); 2014 /* Set offset into patch */ 2015 PetscCall(PetscSectionGetDof(patch->pointCounts, patchNum, &Np)); 2016 PetscCall(PetscSectionGetOffset(patch->pointCounts, patchNum, &poff)); 2017 PetscCall(ISGetIndices(patch->points, &parray)); 2018 PetscCall(ISGetIndices(patch->offs, &oarray)); 2019 for (f = 0; f < Nf; ++f) { 2020 for (p = 0; p < Np; ++p) { 2021 const PetscInt point = parray[poff+p]; 2022 PetscInt dof; 2023 2024 PetscCall(PetscSectionGetFieldDof(patch->patchSection, point, f, &dof)); 2025 PetscCall(PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f])); 2026 if (patch->nsubspaces == 1) PetscCall(PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f])); 2027 else PetscCall(PetscSectionSetOffset(patch->patchSection, point, -1)); 2028 } 2029 } 2030 PetscCall(ISRestoreIndices(patch->points, &parray)); 2031 PetscCall(ISRestoreIndices(patch->offs, &oarray)); 2032 if (patch->viewSection) PetscCall(ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection)); 2033 /* TODO Shut off MatViewFromOptions() in MatAssemblyEnd() here */ 2034 PetscCall(DMPlexComputeJacobian_Patch_Internal(dm, patch->patchSection, patch->patchSection, cellIS, 0.0, 0.0, x, NULL, J, J, ctx)); 2035 PetscCall(DMDestroy(&dm)); 2036 PetscFunctionReturn(0); 2037 } 2038 2039 /* This function zeros mat on entry */ 2040 PetscErrorCode PCPatchComputeOperator_Internal(PC pc, Vec x, Mat mat, PetscInt point, PetscBool withArtificial) 2041 { 2042 PC_PATCH *patch = (PC_PATCH *) pc->data; 2043 const PetscInt *dofsArray; 2044 const PetscInt *dofsArrayWithAll = NULL; 2045 const PetscInt *cellsArray; 2046 PetscInt ncell, offset, pStart, pEnd, numIntFacets, intFacetOffset; 2047 PetscBool isNonlinear; 2048 2049 PetscFunctionBegin; 2050 PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0)); 2051 isNonlinear = patch->isNonlinear; 2052 PetscCheck(patch->usercomputeop,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback"); 2053 if (withArtificial) { 2054 PetscCall(ISGetIndices(patch->dofsWithArtificial, &dofsArray)); 2055 } else { 2056 PetscCall(ISGetIndices(patch->dofs, &dofsArray)); 2057 } 2058 if (isNonlinear) { 2059 PetscCall(ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll)); 2060 } 2061 PetscCall(ISGetIndices(patch->cells, &cellsArray)); 2062 PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd)); 2063 2064 point += pStart; 2065 PetscCheck(point < pEnd,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %" PetscInt_FMT " not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, pStart, pEnd); 2066 2067 PetscCall(PetscSectionGetDof(patch->cellCounts, point, &ncell)); 2068 PetscCall(PetscSectionGetOffset(patch->cellCounts, point, &offset)); 2069 if (ncell <= 0) { 2070 PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 2071 PetscFunctionReturn(0); 2072 } 2073 PetscCall(MatZeroEntries(mat)); 2074 if (patch->precomputeElementTensors) { 2075 PetscInt i; 2076 PetscInt ndof = patch->totalDofsPerCell; 2077 const PetscScalar *elementTensors; 2078 2079 PetscCall(VecGetArrayRead(patch->cellMats, &elementTensors)); 2080 for (i = 0; i < ncell; i++) { 2081 const PetscInt cell = cellsArray[i + offset]; 2082 const PetscInt *idx = dofsArray + (offset + i)*ndof; 2083 const PetscScalar *v = elementTensors + patch->precomputedTensorLocations[cell]*ndof*ndof; 2084 PetscCall(MatSetValues(mat, ndof, idx, ndof, idx, v, ADD_VALUES)); 2085 } 2086 PetscCall(VecRestoreArrayRead(patch->cellMats, &elementTensors)); 2087 PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY)); 2088 PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY)); 2089 } else { 2090 PetscStackPush("PCPatch user callback"); 2091 /* Cannot reuse the same IS because the geometry info is being cached in it */ 2092 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS)); 2093 PetscCall(patch->usercomputeop(pc, point, x, mat, patch->cellIS, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell, dofsArrayWithAll ? dofsArrayWithAll + offset*patch->totalDofsPerCell : NULL, patch->usercomputeopctx)); 2094 } 2095 if (patch->usercomputeopintfacet) { 2096 PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets)); 2097 PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset)); 2098 if (numIntFacets > 0) { 2099 /* For each interior facet, grab the two cells (in local numbering, and concatenate dof numberings for those cells) */ 2100 PetscInt *facetDofs = NULL, *facetDofsWithAll = NULL; 2101 const PetscInt *intFacetsArray = NULL; 2102 PetscInt idx = 0; 2103 PetscInt i, c, d; 2104 PetscInt fStart; 2105 DM dm, plex; 2106 IS facetIS = NULL; 2107 const PetscInt *facetCells = NULL; 2108 2109 PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells)); 2110 PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray)); 2111 PetscCall(PCGetDM(pc, &dm)); 2112 PetscCall(DMConvert(dm, DMPLEX, &plex)); 2113 dm = plex; 2114 PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, NULL)); 2115 /* FIXME: Pull this malloc out. */ 2116 PetscCall(PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofs)); 2117 if (dofsArrayWithAll) { 2118 PetscCall(PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofsWithAll)); 2119 } 2120 if (patch->precomputeElementTensors) { 2121 PetscInt nFacetDof = 2*patch->totalDofsPerCell; 2122 const PetscScalar *elementTensors; 2123 2124 PetscCall(VecGetArrayRead(patch->intFacetMats, &elementTensors)); 2125 2126 for (i = 0; i < numIntFacets; i++) { 2127 const PetscInt facet = intFacetsArray[i + intFacetOffset]; 2128 const PetscScalar *v = elementTensors + patch->precomputedIntFacetTensorLocations[facet - fStart]*nFacetDof*nFacetDof; 2129 idx = 0; 2130 /* 2131 * 0--1 2132 * |\-| 2133 * |+\| 2134 * 2--3 2135 * [0, 2, 3, 0, 1, 3] 2136 */ 2137 for (c = 0; c < 2; c++) { 2138 const PetscInt cell = facetCells[2*(intFacetOffset + i) + c]; 2139 for (d = 0; d < patch->totalDofsPerCell; d++) { 2140 facetDofs[idx] = dofsArray[(offset + cell)*patch->totalDofsPerCell + d]; 2141 idx++; 2142 } 2143 } 2144 PetscCall(MatSetValues(mat, nFacetDof, facetDofs, nFacetDof, facetDofs, v, ADD_VALUES)); 2145 } 2146 PetscCall(VecRestoreArrayRead(patch->intFacetMats, &elementTensors)); 2147 } else { 2148 /* 2149 * 0--1 2150 * |\-| 2151 * |+\| 2152 * 2--3 2153 * [0, 2, 3, 0, 1, 3] 2154 */ 2155 for (i = 0; i < numIntFacets; i++) { 2156 for (c = 0; c < 2; c++) { 2157 const PetscInt cell = facetCells[2*(intFacetOffset + i) + c]; 2158 for (d = 0; d < patch->totalDofsPerCell; d++) { 2159 facetDofs[idx] = dofsArray[(offset + cell)*patch->totalDofsPerCell + d]; 2160 if (dofsArrayWithAll) { 2161 facetDofsWithAll[idx] = dofsArrayWithAll[(offset + cell)*patch->totalDofsPerCell + d]; 2162 } 2163 idx++; 2164 } 2165 } 2166 } 2167 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray + intFacetOffset, PETSC_USE_POINTER, &facetIS)); 2168 PetscCall(patch->usercomputeopintfacet(pc, point, x, mat, facetIS, 2*numIntFacets*patch->totalDofsPerCell, facetDofs, facetDofsWithAll, patch->usercomputeopintfacetctx)); 2169 PetscCall(ISDestroy(&facetIS)); 2170 } 2171 PetscCall(ISRestoreIndices(patch->intFacetsToPatchCell, &facetCells)); 2172 PetscCall(ISRestoreIndices(patch->intFacets, &intFacetsArray)); 2173 PetscCall(PetscFree(facetDofs)); 2174 PetscCall(PetscFree(facetDofsWithAll)); 2175 PetscCall(DMDestroy(&dm)); 2176 } 2177 } 2178 2179 PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY)); 2180 PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY)); 2181 2182 if (!(withArtificial || isNonlinear) && patch->denseinverse) { 2183 MatFactorInfo info; 2184 PetscBool flg; 2185 PetscCall(PetscObjectTypeCompare((PetscObject)mat, MATSEQDENSE, &flg)); 2186 PetscCheck(flg,PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Invalid Mat type for dense inverse"); 2187 PetscCall(MatFactorInfoInitialize(&info)); 2188 PetscCall(MatLUFactor(mat, NULL, NULL, &info)); 2189 PetscCall(MatSeqDenseInvertFactors_Private(mat)); 2190 } 2191 PetscStackPop; 2192 PetscCall(ISDestroy(&patch->cellIS)); 2193 if (withArtificial) { 2194 PetscCall(ISRestoreIndices(patch->dofsWithArtificial, &dofsArray)); 2195 } else { 2196 PetscCall(ISRestoreIndices(patch->dofs, &dofsArray)); 2197 } 2198 if (isNonlinear) { 2199 PetscCall(ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll)); 2200 } 2201 PetscCall(ISRestoreIndices(patch->cells, &cellsArray)); 2202 if (patch->viewMatrix) { 2203 char name[PETSC_MAX_PATH_LEN]; 2204 2205 PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch matrix for Point %" PetscInt_FMT, point)); 2206 PetscCall(PetscObjectSetName((PetscObject) mat, name)); 2207 PetscCall(ObjectView((PetscObject) mat, patch->viewerMatrix, patch->formatMatrix)); 2208 } 2209 PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 2210 PetscFunctionReturn(0); 2211 } 2212 2213 static PetscErrorCode MatSetValues_PCPatch_Private(Mat mat, PetscInt m, const PetscInt idxm[], 2214 PetscInt n, const PetscInt idxn[], const PetscScalar *v, InsertMode addv) 2215 { 2216 Vec data; 2217 PetscScalar *array; 2218 PetscInt bs, nz, i, j, cell; 2219 2220 PetscCall(MatShellGetContext(mat, &data)); 2221 PetscCall(VecGetBlockSize(data, &bs)); 2222 PetscCall(VecGetSize(data, &nz)); 2223 PetscCall(VecGetArray(data, &array)); 2224 PetscCheck(m == n,PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Only for square insertion"); 2225 cell = (PetscInt)(idxm[0]/bs); /* use the fact that this is called once per cell */ 2226 for (i = 0; i < m; i++) { 2227 PetscCheck(idxm[i] == idxn[i],PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Row and column indices must match!"); 2228 for (j = 0; j < n; j++) { 2229 const PetscScalar v_ = v[i*bs + j]; 2230 /* Indexing is special to the data structure we have! */ 2231 if (addv == INSERT_VALUES) { 2232 array[cell*bs*bs + i*bs + j] = v_; 2233 } else { 2234 array[cell*bs*bs + i*bs + j] += v_; 2235 } 2236 } 2237 } 2238 PetscCall(VecRestoreArray(data, &array)); 2239 PetscFunctionReturn(0); 2240 } 2241 2242 static PetscErrorCode PCPatchPrecomputePatchTensors_Private(PC pc) 2243 { 2244 PC_PATCH *patch = (PC_PATCH *)pc->data; 2245 const PetscInt *cellsArray; 2246 PetscInt ncell, offset; 2247 const PetscInt *dofMapArray; 2248 PetscInt i, j; 2249 IS dofMap; 2250 IS cellIS; 2251 const PetscInt ndof = patch->totalDofsPerCell; 2252 Mat vecMat; 2253 PetscInt cStart, cEnd; 2254 DM dm, plex; 2255 2256 PetscCall(ISGetSize(patch->cells, &ncell)); 2257 if (!ncell) { /* No cells to assemble over -> skip */ 2258 PetscFunctionReturn(0); 2259 } 2260 2261 PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0)); 2262 2263 PetscCall(PCGetDM(pc, &dm)); 2264 PetscCall(DMConvert(dm, DMPLEX, &plex)); 2265 dm = plex; 2266 if (!patch->allCells) { 2267 PetscHSetI cells; 2268 PetscHashIter hi; 2269 PetscInt pStart, pEnd; 2270 PetscInt *allCells = NULL; 2271 PetscCall(PetscHSetICreate(&cells)); 2272 PetscCall(ISGetIndices(patch->cells, &cellsArray)); 2273 PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd)); 2274 for (i = pStart; i < pEnd; i++) { 2275 PetscCall(PetscSectionGetDof(patch->cellCounts, i, &ncell)); 2276 PetscCall(PetscSectionGetOffset(patch->cellCounts, i, &offset)); 2277 if (ncell <= 0) continue; 2278 for (j = 0; j < ncell; j++) { 2279 PetscCall(PetscHSetIAdd(cells, cellsArray[offset + j])); 2280 } 2281 } 2282 PetscCall(ISRestoreIndices(patch->cells, &cellsArray)); 2283 PetscCall(PetscHSetIGetSize(cells, &ncell)); 2284 PetscCall(PetscMalloc1(ncell, &allCells)); 2285 PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 2286 PetscCall(PetscMalloc1(cEnd-cStart, &patch->precomputedTensorLocations)); 2287 i = 0; 2288 PetscHashIterBegin(cells, hi); 2289 while (!PetscHashIterAtEnd(cells, hi)) { 2290 PetscHashIterGetKey(cells, hi, allCells[i]); 2291 patch->precomputedTensorLocations[allCells[i]] = i; 2292 PetscHashIterNext(cells, hi); 2293 i++; 2294 } 2295 PetscCall(PetscHSetIDestroy(&cells)); 2296 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, allCells, PETSC_OWN_POINTER, &patch->allCells)); 2297 } 2298 PetscCall(ISGetSize(patch->allCells, &ncell)); 2299 if (!patch->cellMats) { 2300 PetscCall(VecCreateSeq(PETSC_COMM_SELF, ncell*ndof*ndof, &patch->cellMats)); 2301 PetscCall(VecSetBlockSize(patch->cellMats, ndof)); 2302 } 2303 PetscCall(VecSet(patch->cellMats, 0)); 2304 2305 PetscCall(MatCreateShell(PETSC_COMM_SELF, ncell*ndof, ncell*ndof, ncell*ndof, ncell*ndof,(void*)patch->cellMats, &vecMat)); 2306 PetscCall(MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void(*)(void))&MatSetValues_PCPatch_Private)); 2307 PetscCall(ISGetSize(patch->allCells, &ncell)); 2308 PetscCall(ISCreateStride(PETSC_COMM_SELF, ndof*ncell, 0, 1, &dofMap)); 2309 PetscCall(ISGetIndices(dofMap, &dofMapArray)); 2310 PetscCall(ISGetIndices(patch->allCells, &cellsArray)); 2311 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray, PETSC_USE_POINTER, &cellIS)); 2312 PetscStackPush("PCPatch user callback"); 2313 /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */ 2314 PetscCall(patch->usercomputeop(pc, -1, NULL, vecMat, cellIS, ndof*ncell, dofMapArray, NULL, patch->usercomputeopctx)); 2315 PetscStackPop; 2316 PetscCall(ISDestroy(&cellIS)); 2317 PetscCall(MatDestroy(&vecMat)); 2318 PetscCall(ISRestoreIndices(patch->allCells, &cellsArray)); 2319 PetscCall(ISRestoreIndices(dofMap, &dofMapArray)); 2320 PetscCall(ISDestroy(&dofMap)); 2321 2322 if (patch->usercomputeopintfacet) { 2323 PetscInt nIntFacets; 2324 IS intFacetsIS; 2325 const PetscInt *intFacetsArray = NULL; 2326 if (!patch->allIntFacets) { 2327 PetscHSetI facets; 2328 PetscHashIter hi; 2329 PetscInt pStart, pEnd, fStart, fEnd; 2330 PetscInt *allIntFacets = NULL; 2331 PetscCall(PetscHSetICreate(&facets)); 2332 PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray)); 2333 PetscCall(PetscSectionGetChart(patch->intFacetCounts, &pStart, &pEnd)); 2334 PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd)); 2335 for (i = pStart; i < pEnd; i++) { 2336 PetscCall(PetscSectionGetDof(patch->intFacetCounts, i, &nIntFacets)); 2337 PetscCall(PetscSectionGetOffset(patch->intFacetCounts, i, &offset)); 2338 if (nIntFacets <= 0) continue; 2339 for (j = 0; j < nIntFacets; j++) { 2340 PetscCall(PetscHSetIAdd(facets, intFacetsArray[offset + j])); 2341 } 2342 } 2343 PetscCall(ISRestoreIndices(patch->intFacets, &intFacetsArray)); 2344 PetscCall(PetscHSetIGetSize(facets, &nIntFacets)); 2345 PetscCall(PetscMalloc1(nIntFacets, &allIntFacets)); 2346 PetscCall(PetscMalloc1(fEnd-fStart, &patch->precomputedIntFacetTensorLocations)); 2347 i = 0; 2348 PetscHashIterBegin(facets, hi); 2349 while (!PetscHashIterAtEnd(facets, hi)) { 2350 PetscHashIterGetKey(facets, hi, allIntFacets[i]); 2351 patch->precomputedIntFacetTensorLocations[allIntFacets[i] - fStart] = i; 2352 PetscHashIterNext(facets, hi); 2353 i++; 2354 } 2355 PetscCall(PetscHSetIDestroy(&facets)); 2356 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, allIntFacets, PETSC_OWN_POINTER, &patch->allIntFacets)); 2357 } 2358 PetscCall(ISGetSize(patch->allIntFacets, &nIntFacets)); 2359 if (!patch->intFacetMats) { 2360 PetscCall(VecCreateSeq(PETSC_COMM_SELF, nIntFacets*ndof*ndof*4, &patch->intFacetMats)); 2361 PetscCall(VecSetBlockSize(patch->intFacetMats, ndof*2)); 2362 } 2363 PetscCall(VecSet(patch->intFacetMats, 0)); 2364 2365 PetscCall(MatCreateShell(PETSC_COMM_SELF, nIntFacets*ndof*2, nIntFacets*ndof*2, nIntFacets*ndof*2, nIntFacets*ndof*2,(void*)patch->intFacetMats, &vecMat)); 2366 PetscCall(MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void(*)(void))&MatSetValues_PCPatch_Private)); 2367 PetscCall(ISCreateStride(PETSC_COMM_SELF, 2*ndof*nIntFacets, 0, 1, &dofMap)); 2368 PetscCall(ISGetIndices(dofMap, &dofMapArray)); 2369 PetscCall(ISGetIndices(patch->allIntFacets, &intFacetsArray)); 2370 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, intFacetsArray, PETSC_USE_POINTER, &intFacetsIS)); 2371 PetscStackPush("PCPatch user callback (interior facets)"); 2372 /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */ 2373 PetscCall(patch->usercomputeopintfacet(pc, -1, NULL, vecMat, intFacetsIS, 2*ndof*nIntFacets, dofMapArray, NULL, patch->usercomputeopintfacetctx)); 2374 PetscStackPop; 2375 PetscCall(ISDestroy(&intFacetsIS)); 2376 PetscCall(MatDestroy(&vecMat)); 2377 PetscCall(ISRestoreIndices(patch->allIntFacets, &intFacetsArray)); 2378 PetscCall(ISRestoreIndices(dofMap, &dofMapArray)); 2379 PetscCall(ISDestroy(&dofMap)); 2380 } 2381 PetscCall(DMDestroy(&dm)); 2382 PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 2383 2384 PetscFunctionReturn(0); 2385 } 2386 2387 PetscErrorCode PCPatch_ScatterLocal_Private(PC pc, PetscInt p, Vec x, Vec y, InsertMode mode, ScatterMode scat, PatchScatterType scattertype) 2388 { 2389 PC_PATCH *patch = (PC_PATCH *) pc->data; 2390 const PetscScalar *xArray = NULL; 2391 PetscScalar *yArray = NULL; 2392 const PetscInt *gtolArray = NULL; 2393 PetscInt dof, offset, lidx; 2394 2395 PetscFunctionBeginHot; 2396 PetscCall(VecGetArrayRead(x, &xArray)); 2397 PetscCall(VecGetArray(y, &yArray)); 2398 if (scattertype == SCATTER_WITHARTIFICIAL) { 2399 PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof)); 2400 PetscCall(PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offset)); 2401 PetscCall(ISGetIndices(patch->gtolWithArtificial, >olArray)); 2402 } else if (scattertype == SCATTER_WITHALL) { 2403 PetscCall(PetscSectionGetDof(patch->gtolCountsWithAll, p, &dof)); 2404 PetscCall(PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offset)); 2405 PetscCall(ISGetIndices(patch->gtolWithAll, >olArray)); 2406 } else { 2407 PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &dof)); 2408 PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset)); 2409 PetscCall(ISGetIndices(patch->gtol, >olArray)); 2410 } 2411 PetscCheck(mode != INSERT_VALUES || scat == SCATTER_FORWARD,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't insert if not scattering forward"); 2412 PetscCheck(mode != ADD_VALUES || scat == SCATTER_REVERSE,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't add if not scattering reverse"); 2413 for (lidx = 0; lidx < dof; ++lidx) { 2414 const PetscInt gidx = gtolArray[offset+lidx]; 2415 2416 if (mode == INSERT_VALUES) yArray[lidx] = xArray[gidx]; /* Forward */ 2417 else yArray[gidx] += xArray[lidx]; /* Reverse */ 2418 } 2419 if (scattertype == SCATTER_WITHARTIFICIAL) { 2420 PetscCall(ISRestoreIndices(patch->gtolWithArtificial, >olArray)); 2421 } else if (scattertype == SCATTER_WITHALL) { 2422 PetscCall(ISRestoreIndices(patch->gtolWithAll, >olArray)); 2423 } else { 2424 PetscCall(ISRestoreIndices(patch->gtol, >olArray)); 2425 } 2426 PetscCall(VecRestoreArrayRead(x, &xArray)); 2427 PetscCall(VecRestoreArray(y, &yArray)); 2428 PetscFunctionReturn(0); 2429 } 2430 2431 static PetscErrorCode PCSetUp_PATCH_Linear(PC pc) 2432 { 2433 PC_PATCH *patch = (PC_PATCH *) pc->data; 2434 const char *prefix; 2435 PetscInt i; 2436 2437 PetscFunctionBegin; 2438 if (!pc->setupcalled) { 2439 PetscCheck(patch->save_operators || !patch->denseinverse,PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Can't have dense inverse without save operators"); 2440 if (!patch->denseinverse) { 2441 PetscCall(PetscMalloc1(patch->npatch, &patch->solver)); 2442 PetscCall(PCGetOptionsPrefix(pc, &prefix)); 2443 for (i = 0; i < patch->npatch; ++i) { 2444 KSP ksp; 2445 PC subpc; 2446 2447 PetscCall(KSPCreate(PETSC_COMM_SELF, &ksp)); 2448 PetscCall(KSPSetErrorIfNotConverged(ksp, pc->erroriffailure)); 2449 PetscCall(KSPSetOptionsPrefix(ksp, prefix)); 2450 PetscCall(KSPAppendOptionsPrefix(ksp, "sub_")); 2451 PetscCall(PetscObjectIncrementTabLevel((PetscObject) ksp, (PetscObject) pc, 1)); 2452 PetscCall(KSPGetPC(ksp, &subpc)); 2453 PetscCall(PetscObjectIncrementTabLevel((PetscObject) subpc, (PetscObject) pc, 1)); 2454 PetscCall(PetscLogObjectParent((PetscObject) pc, (PetscObject) ksp)); 2455 patch->solver[i] = (PetscObject) ksp; 2456 } 2457 } 2458 } 2459 if (patch->save_operators) { 2460 if (patch->precomputeElementTensors) PetscCall(PCPatchPrecomputePatchTensors_Private(pc)); 2461 for (i = 0; i < patch->npatch; ++i) { 2462 PetscCall(PCPatchComputeOperator_Internal(pc, NULL, patch->mat[i], i, PETSC_FALSE)); 2463 if (!patch->denseinverse) { 2464 PetscCall(KSPSetOperators((KSP) patch->solver[i], patch->mat[i], patch->mat[i])); 2465 } else if (patch->mat[i] && !patch->densesolve) { 2466 /* Setup matmult callback */ 2467 PetscCall(MatGetOperation(patch->mat[i], MATOP_MULT, (void (**)(void))&patch->densesolve)); 2468 } 2469 } 2470 } 2471 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 2472 for (i = 0; i < patch->npatch; ++i) { 2473 /* Instead of padding patch->patchUpdate with zeros to get */ 2474 /* patch->patchUpdateWithArtificial and then multiplying with the matrix, */ 2475 /* just get rid of the columns that correspond to the dofs with */ 2476 /* artificial bcs. That's of course fairly inefficient, hopefully we */ 2477 /* can just assemble the rectangular matrix in the first place. */ 2478 Mat matSquare; 2479 IS rowis; 2480 PetscInt dof; 2481 2482 PetscCall(MatGetSize(patch->mat[i], &dof, NULL)); 2483 if (dof == 0) { 2484 patch->matWithArtificial[i] = NULL; 2485 continue; 2486 } 2487 2488 PetscCall(PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE)); 2489 PetscCall(PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE)); 2490 2491 PetscCall(MatGetSize(matSquare, &dof, NULL)); 2492 PetscCall(ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis)); 2493 if (pc->setupcalled) { 2494 PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_REUSE_MATRIX, &patch->matWithArtificial[i])); 2495 } else { 2496 PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &patch->matWithArtificial[i])); 2497 } 2498 PetscCall(ISDestroy(&rowis)); 2499 PetscCall(MatDestroy(&matSquare)); 2500 } 2501 } 2502 PetscFunctionReturn(0); 2503 } 2504 2505 static PetscErrorCode PCSetUp_PATCH(PC pc) 2506 { 2507 PC_PATCH *patch = (PC_PATCH *) pc->data; 2508 PetscInt i; 2509 PetscBool isNonlinear; 2510 PetscInt maxDof = -1, maxDofWithArtificial = -1; 2511 2512 PetscFunctionBegin; 2513 if (!pc->setupcalled) { 2514 PetscInt pStart, pEnd, p; 2515 PetscInt localSize; 2516 2517 PetscCall(PetscLogEventBegin(PC_Patch_CreatePatches, pc, 0, 0, 0)); 2518 2519 isNonlinear = patch->isNonlinear; 2520 if (!patch->nsubspaces) { 2521 DM dm, plex; 2522 PetscSection s; 2523 PetscInt cStart, cEnd, c, Nf, f, numGlobalBcs = 0, *globalBcs, *Nb, **cellDofs; 2524 2525 PetscCall(PCGetDM(pc, &dm)); 2526 PetscCheck(dm,PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONG, "Must set DM for PCPATCH or call PCPatchSetDiscretisationInfo()"); 2527 PetscCall(DMConvert(dm, DMPLEX, &plex)); 2528 dm = plex; 2529 PetscCall(DMGetLocalSection(dm, &s)); 2530 PetscCall(PetscSectionGetNumFields(s, &Nf)); 2531 PetscCall(PetscSectionGetChart(s, &pStart, &pEnd)); 2532 for (p = pStart; p < pEnd; ++p) { 2533 PetscInt cdof; 2534 PetscCall(PetscSectionGetConstraintDof(s, p, &cdof)); 2535 numGlobalBcs += cdof; 2536 } 2537 PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 2538 PetscCall(PetscMalloc3(Nf, &Nb, Nf, &cellDofs, numGlobalBcs, &globalBcs)); 2539 for (f = 0; f < Nf; ++f) { 2540 PetscFE fe; 2541 PetscDualSpace sp; 2542 PetscInt cdoff = 0; 2543 2544 PetscCall(DMGetField(dm, f, NULL, (PetscObject *) &fe)); 2545 /* PetscCall(PetscFEGetNumComponents(fe, &Nc[f])); */ 2546 PetscCall(PetscFEGetDualSpace(fe, &sp)); 2547 PetscCall(PetscDualSpaceGetDimension(sp, &Nb[f])); 2548 2549 PetscCall(PetscMalloc1((cEnd-cStart)*Nb[f], &cellDofs[f])); 2550 for (c = cStart; c < cEnd; ++c) { 2551 PetscInt *closure = NULL; 2552 PetscInt clSize = 0, cl; 2553 2554 PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure)); 2555 for (cl = 0; cl < clSize*2; cl += 2) { 2556 const PetscInt p = closure[cl]; 2557 PetscInt fdof, d, foff; 2558 2559 PetscCall(PetscSectionGetFieldDof(s, p, f, &fdof)); 2560 PetscCall(PetscSectionGetFieldOffset(s, p, f, &foff)); 2561 for (d = 0; d < fdof; ++d, ++cdoff) cellDofs[f][cdoff] = foff + d; 2562 } 2563 PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure)); 2564 } 2565 PetscCheck(cdoff == (cEnd-cStart)*Nb[f],PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_SIZ, "Total number of cellDofs %" PetscInt_FMT " for field %" PetscInt_FMT " should be Nc (%" PetscInt_FMT ") * cellDof (%" PetscInt_FMT ")", cdoff, f, cEnd-cStart, Nb[f]); 2566 } 2567 numGlobalBcs = 0; 2568 for (p = pStart; p < pEnd; ++p) { 2569 const PetscInt *ind; 2570 PetscInt off, cdof, d; 2571 2572 PetscCall(PetscSectionGetOffset(s, p, &off)); 2573 PetscCall(PetscSectionGetConstraintDof(s, p, &cdof)); 2574 PetscCall(PetscSectionGetConstraintIndices(s, p, &ind)); 2575 for (d = 0; d < cdof; ++d) globalBcs[numGlobalBcs++] = off + ind[d]; 2576 } 2577 2578 PetscCall(PCPatchSetDiscretisationInfoCombined(pc, dm, Nb, (const PetscInt **) cellDofs, numGlobalBcs, globalBcs, numGlobalBcs, globalBcs)); 2579 for (f = 0; f < Nf; ++f) { 2580 PetscCall(PetscFree(cellDofs[f])); 2581 } 2582 PetscCall(PetscFree3(Nb, cellDofs, globalBcs)); 2583 PetscCall(PCPatchSetComputeFunction(pc, PCPatchComputeFunction_DMPlex_Private, NULL)); 2584 PetscCall(PCPatchSetComputeOperator(pc, PCPatchComputeOperator_DMPlex_Private, NULL)); 2585 PetscCall(DMDestroy(&dm)); 2586 } 2587 2588 localSize = patch->subspaceOffsets[patch->nsubspaces]; 2589 PetscCall(VecCreateSeq(PETSC_COMM_SELF, localSize, &patch->localRHS)); 2590 PetscCall(VecSetUp(patch->localRHS)); 2591 PetscCall(VecDuplicate(patch->localRHS, &patch->localUpdate)); 2592 PetscCall(PCPatchCreateCellPatches(pc)); 2593 PetscCall(PCPatchCreateCellPatchDiscretisationInfo(pc)); 2594 2595 /* OK, now build the work vectors */ 2596 PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, &pEnd)); 2597 2598 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 2599 PetscCall(PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithArtificial)); 2600 } 2601 if (isNonlinear) { 2602 PetscCall(PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithAll)); 2603 } 2604 for (p = pStart; p < pEnd; ++p) { 2605 PetscInt dof; 2606 2607 PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &dof)); 2608 maxDof = PetscMax(maxDof, dof); 2609 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 2610 const PetscInt *gtolArray, *gtolArrayWithArtificial = NULL; 2611 PetscInt numPatchDofs, offset; 2612 PetscInt numPatchDofsWithArtificial, offsetWithArtificial; 2613 PetscInt dofWithoutArtificialCounter = 0; 2614 PetscInt *patchWithoutArtificialToWithArtificialArray; 2615 2616 PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof)); 2617 maxDofWithArtificial = PetscMax(maxDofWithArtificial, dof); 2618 2619 /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */ 2620 /* the index in the patch with all dofs */ 2621 PetscCall(ISGetIndices(patch->gtol, >olArray)); 2622 2623 PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs)); 2624 if (numPatchDofs == 0) { 2625 patch->dofMappingWithoutToWithArtificial[p-pStart] = NULL; 2626 continue; 2627 } 2628 2629 PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset)); 2630 PetscCall(ISGetIndices(patch->gtolWithArtificial, >olArrayWithArtificial)); 2631 PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &numPatchDofsWithArtificial)); 2632 PetscCall(PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offsetWithArtificial)); 2633 2634 PetscCall(PetscMalloc1(numPatchDofs, &patchWithoutArtificialToWithArtificialArray)); 2635 for (i=0; i<numPatchDofsWithArtificial; i++) { 2636 if (gtolArrayWithArtificial[i+offsetWithArtificial] == gtolArray[offset+dofWithoutArtificialCounter]) { 2637 patchWithoutArtificialToWithArtificialArray[dofWithoutArtificialCounter] = i; 2638 dofWithoutArtificialCounter++; 2639 if (dofWithoutArtificialCounter == numPatchDofs) 2640 break; 2641 } 2642 } 2643 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutArtificialToWithArtificialArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithArtificial[p-pStart])); 2644 PetscCall(ISRestoreIndices(patch->gtol, >olArray)); 2645 PetscCall(ISRestoreIndices(patch->gtolWithArtificial, >olArrayWithArtificial)); 2646 } 2647 if (isNonlinear) { 2648 const PetscInt *gtolArray, *gtolArrayWithAll = NULL; 2649 PetscInt numPatchDofs, offset; 2650 PetscInt numPatchDofsWithAll, offsetWithAll; 2651 PetscInt dofWithoutAllCounter = 0; 2652 PetscInt *patchWithoutAllToWithAllArray; 2653 2654 /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */ 2655 /* the index in the patch with all dofs */ 2656 PetscCall(ISGetIndices(patch->gtol, >olArray)); 2657 2658 PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs)); 2659 if (numPatchDofs == 0) { 2660 patch->dofMappingWithoutToWithAll[p-pStart] = NULL; 2661 continue; 2662 } 2663 2664 PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset)); 2665 PetscCall(ISGetIndices(patch->gtolWithAll, >olArrayWithAll)); 2666 PetscCall(PetscSectionGetDof(patch->gtolCountsWithAll, p, &numPatchDofsWithAll)); 2667 PetscCall(PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offsetWithAll)); 2668 2669 PetscCall(PetscMalloc1(numPatchDofs, &patchWithoutAllToWithAllArray)); 2670 2671 for (i=0; i<numPatchDofsWithAll; i++) { 2672 if (gtolArrayWithAll[i+offsetWithAll] == gtolArray[offset+dofWithoutAllCounter]) { 2673 patchWithoutAllToWithAllArray[dofWithoutAllCounter] = i; 2674 dofWithoutAllCounter++; 2675 if (dofWithoutAllCounter == numPatchDofs) 2676 break; 2677 } 2678 } 2679 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutAllToWithAllArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithAll[p-pStart])); 2680 PetscCall(ISRestoreIndices(patch->gtol, >olArray)); 2681 PetscCall(ISRestoreIndices(patch->gtolWithAll, >olArrayWithAll)); 2682 } 2683 } 2684 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 2685 PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDofWithArtificial, &patch->patchRHSWithArtificial)); 2686 PetscCall(VecSetUp(patch->patchRHSWithArtificial)); 2687 } 2688 PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDof, &patch->patchRHS)); 2689 PetscCall(VecSetUp(patch->patchRHS)); 2690 PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDof, &patch->patchUpdate)); 2691 PetscCall(VecSetUp(patch->patchUpdate)); 2692 if (patch->save_operators) { 2693 PetscCall(PetscMalloc1(patch->npatch, &patch->mat)); 2694 for (i = 0; i < patch->npatch; ++i) { 2695 PetscCall(PCPatchCreateMatrix_Private(pc, i, &patch->mat[i], PETSC_FALSE)); 2696 } 2697 } 2698 PetscCall(PetscLogEventEnd(PC_Patch_CreatePatches, pc, 0, 0, 0)); 2699 2700 /* If desired, calculate weights for dof multiplicity */ 2701 if (patch->partition_of_unity) { 2702 PetscScalar *input = NULL; 2703 PetscScalar *output = NULL; 2704 Vec global; 2705 2706 PetscCall(VecDuplicate(patch->localRHS, &patch->dof_weights)); 2707 if (patch->local_composition_type == PC_COMPOSITE_ADDITIVE) { 2708 for (i = 0; i < patch->npatch; ++i) { 2709 PetscInt dof; 2710 2711 PetscCall(PetscSectionGetDof(patch->gtolCounts, i+pStart, &dof)); 2712 if (dof <= 0) continue; 2713 PetscCall(VecSet(patch->patchRHS, 1.0)); 2714 PetscCall(PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchRHS, patch->dof_weights, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR)); 2715 } 2716 } else { 2717 /* multiplicative is actually only locally multiplicative and globally additive. need the pou where the mesh decomposition overlaps */ 2718 PetscCall(VecSet(patch->dof_weights, 1.0)); 2719 } 2720 2721 VecDuplicate(patch->dof_weights, &global); 2722 VecSet(global, 0.); 2723 2724 PetscCall(VecGetArray(patch->dof_weights, &input)); 2725 PetscCall(VecGetArray(global, &output)); 2726 PetscCall(PetscSFReduceBegin(patch->sectionSF, MPIU_SCALAR, input, output, MPI_SUM)); 2727 PetscCall(PetscSFReduceEnd(patch->sectionSF, MPIU_SCALAR, input, output, MPI_SUM)); 2728 PetscCall(VecRestoreArray(patch->dof_weights, &input)); 2729 PetscCall(VecRestoreArray(global, &output)); 2730 2731 PetscCall(VecReciprocal(global)); 2732 2733 PetscCall(VecGetArray(patch->dof_weights, &output)); 2734 PetscCall(VecGetArray(global, &input)); 2735 PetscCall(PetscSFBcastBegin(patch->sectionSF, MPIU_SCALAR, input, output,MPI_REPLACE)); 2736 PetscCall(PetscSFBcastEnd(patch->sectionSF, MPIU_SCALAR, input, output,MPI_REPLACE)); 2737 PetscCall(VecRestoreArray(patch->dof_weights, &output)); 2738 PetscCall(VecRestoreArray(global, &input)); 2739 PetscCall(VecDestroy(&global)); 2740 } 2741 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE && patch->save_operators) { 2742 PetscCall(PetscMalloc1(patch->npatch, &patch->matWithArtificial)); 2743 } 2744 } 2745 PetscCall((*patch->setupsolver)(pc)); 2746 PetscFunctionReturn(0); 2747 } 2748 2749 static PetscErrorCode PCApply_PATCH_Linear(PC pc, PetscInt i, Vec x, Vec y) 2750 { 2751 PC_PATCH *patch = (PC_PATCH *) pc->data; 2752 KSP ksp; 2753 Mat op; 2754 PetscInt m, n; 2755 2756 PetscFunctionBegin; 2757 if (patch->denseinverse) { 2758 PetscCall((*patch->densesolve)(patch->mat[i], x, y)); 2759 PetscFunctionReturn(0); 2760 } 2761 ksp = (KSP) patch->solver[i]; 2762 if (!patch->save_operators) { 2763 Mat mat; 2764 2765 PetscCall(PCPatchCreateMatrix_Private(pc, i, &mat, PETSC_FALSE)); 2766 /* Populate operator here. */ 2767 PetscCall(PCPatchComputeOperator_Internal(pc, NULL, mat, i, PETSC_FALSE)); 2768 PetscCall(KSPSetOperators(ksp, mat, mat)); 2769 /* Drop reference so the KSPSetOperators below will blow it away. */ 2770 PetscCall(MatDestroy(&mat)); 2771 } 2772 PetscCall(PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0)); 2773 if (!ksp->setfromoptionscalled) { 2774 PetscCall(KSPSetFromOptions(ksp)); 2775 } 2776 /* Disgusting trick to reuse work vectors */ 2777 PetscCall(KSPGetOperators(ksp, &op, NULL)); 2778 PetscCall(MatGetLocalSize(op, &m, &n)); 2779 x->map->n = m; 2780 y->map->n = n; 2781 x->map->N = m; 2782 y->map->N = n; 2783 PetscCall(KSPSolve(ksp, x, y)); 2784 PetscCall(KSPCheckSolve(ksp, pc, y)); 2785 PetscCall(PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0)); 2786 if (!patch->save_operators) { 2787 PC pc; 2788 PetscCall(KSPSetOperators(ksp, NULL, NULL)); 2789 PetscCall(KSPGetPC(ksp, &pc)); 2790 /* Destroy PC context too, otherwise the factored matrix hangs around. */ 2791 PetscCall(PCReset(pc)); 2792 } 2793 PetscFunctionReturn(0); 2794 } 2795 2796 static PetscErrorCode PCUpdateMultiplicative_PATCH_Linear(PC pc, PetscInt i, PetscInt pStart) 2797 { 2798 PC_PATCH *patch = (PC_PATCH *) pc->data; 2799 Mat multMat; 2800 PetscInt n, m; 2801 2802 PetscFunctionBegin; 2803 2804 if (patch->save_operators) { 2805 multMat = patch->matWithArtificial[i]; 2806 } else { 2807 /*Very inefficient, hopefully we can just assemble the rectangular matrix in the first place.*/ 2808 Mat matSquare; 2809 PetscInt dof; 2810 IS rowis; 2811 PetscCall(PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE)); 2812 PetscCall(PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE)); 2813 PetscCall(MatGetSize(matSquare, &dof, NULL)); 2814 PetscCall(ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis)); 2815 PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &multMat)); 2816 PetscCall(MatDestroy(&matSquare)); 2817 PetscCall(ISDestroy(&rowis)); 2818 } 2819 /* Disgusting trick to reuse work vectors */ 2820 PetscCall(MatGetLocalSize(multMat, &m, &n)); 2821 patch->patchUpdate->map->n = n; 2822 patch->patchRHSWithArtificial->map->n = m; 2823 patch->patchUpdate->map->N = n; 2824 patch->patchRHSWithArtificial->map->N = m; 2825 PetscCall(MatMult(multMat, patch->patchUpdate, patch->patchRHSWithArtificial)); 2826 PetscCall(VecScale(patch->patchRHSWithArtificial, -1.0)); 2827 PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHSWithArtificial, patch->localRHS, ADD_VALUES, SCATTER_REVERSE, SCATTER_WITHARTIFICIAL)); 2828 if (!patch->save_operators) { 2829 PetscCall(MatDestroy(&multMat)); 2830 } 2831 PetscFunctionReturn(0); 2832 } 2833 2834 static PetscErrorCode PCApply_PATCH(PC pc, Vec x, Vec y) 2835 { 2836 PC_PATCH *patch = (PC_PATCH *) pc->data; 2837 const PetscScalar *globalRHS = NULL; 2838 PetscScalar *localRHS = NULL; 2839 PetscScalar *globalUpdate = NULL; 2840 const PetscInt *bcNodes = NULL; 2841 PetscInt nsweep = patch->symmetrise_sweep ? 2 : 1; 2842 PetscInt start[2] = {0, 0}; 2843 PetscInt end[2] = {-1, -1}; 2844 const PetscInt inc[2] = {1, -1}; 2845 const PetscScalar *localUpdate; 2846 const PetscInt *iterationSet; 2847 PetscInt pStart, numBcs, n, sweep, bc, j; 2848 2849 PetscFunctionBegin; 2850 PetscCall(PetscLogEventBegin(PC_Patch_Apply, pc, 0, 0, 0)); 2851 PetscCall(PetscOptionsPushGetViewerOff(PETSC_TRUE)); 2852 /* start, end, inc have 2 entries to manage a second backward sweep if we symmetrize */ 2853 end[0] = patch->npatch; 2854 start[1] = patch->npatch-1; 2855 if (patch->user_patches) { 2856 PetscCall(ISGetLocalSize(patch->iterationSet, &end[0])); 2857 start[1] = end[0] - 1; 2858 PetscCall(ISGetIndices(patch->iterationSet, &iterationSet)); 2859 } 2860 /* Scatter from global space into overlapped local spaces */ 2861 PetscCall(VecGetArrayRead(x, &globalRHS)); 2862 PetscCall(VecGetArray(patch->localRHS, &localRHS)); 2863 PetscCall(PetscSFBcastBegin(patch->sectionSF, MPIU_SCALAR, globalRHS, localRHS,MPI_REPLACE)); 2864 PetscCall(PetscSFBcastEnd(patch->sectionSF, MPIU_SCALAR, globalRHS, localRHS,MPI_REPLACE)); 2865 PetscCall(VecRestoreArrayRead(x, &globalRHS)); 2866 PetscCall(VecRestoreArray(patch->localRHS, &localRHS)); 2867 2868 PetscCall(VecSet(patch->localUpdate, 0.0)); 2869 PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, NULL)); 2870 PetscCall(PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0)); 2871 for (sweep = 0; sweep < nsweep; sweep++) { 2872 for (j = start[sweep]; j*inc[sweep] < end[sweep]*inc[sweep]; j += inc[sweep]) { 2873 PetscInt i = patch->user_patches ? iterationSet[j] : j; 2874 PetscInt start, len; 2875 2876 PetscCall(PetscSectionGetDof(patch->gtolCounts, i+pStart, &len)); 2877 PetscCall(PetscSectionGetOffset(patch->gtolCounts, i+pStart, &start)); 2878 /* TODO: Squash out these guys in the setup as well. */ 2879 if (len <= 0) continue; 2880 /* TODO: Do we need different scatters for X and Y? */ 2881 PetscCall(PCPatch_ScatterLocal_Private(pc, i+pStart, patch->localRHS, patch->patchRHS, INSERT_VALUES, SCATTER_FORWARD, SCATTER_INTERIOR)); 2882 PetscCall((*patch->applysolver)(pc, i, patch->patchRHS, patch->patchUpdate)); 2883 PetscCall(PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchUpdate, patch->localUpdate, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR)); 2884 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 2885 PetscCall((*patch->updatemultiplicative)(pc, i, pStart)); 2886 } 2887 } 2888 } 2889 PetscCall(PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0)); 2890 if (patch->user_patches) PetscCall(ISRestoreIndices(patch->iterationSet, &iterationSet)); 2891 /* XXX: should we do this on the global vector? */ 2892 if (patch->partition_of_unity) PetscCall(VecPointwiseMult(patch->localUpdate, patch->localUpdate, patch->dof_weights)); 2893 /* Now patch->localUpdate contains the solution of the patch solves, so we need to combine them all. */ 2894 PetscCall(VecSet(y, 0.0)); 2895 PetscCall(VecGetArray(y, &globalUpdate)); 2896 PetscCall(VecGetArrayRead(patch->localUpdate, &localUpdate)); 2897 PetscCall(PetscSFReduceBegin(patch->sectionSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM)); 2898 PetscCall(PetscSFReduceEnd(patch->sectionSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM)); 2899 PetscCall(VecRestoreArrayRead(patch->localUpdate, &localUpdate)); 2900 2901 /* Now we need to send the global BC values through */ 2902 PetscCall(VecGetArrayRead(x, &globalRHS)); 2903 PetscCall(ISGetSize(patch->globalBcNodes, &numBcs)); 2904 PetscCall(ISGetIndices(patch->globalBcNodes, &bcNodes)); 2905 PetscCall(VecGetLocalSize(x, &n)); 2906 for (bc = 0; bc < numBcs; ++bc) { 2907 const PetscInt idx = bcNodes[bc]; 2908 if (idx < n) globalUpdate[idx] = globalRHS[idx]; 2909 } 2910 2911 PetscCall(ISRestoreIndices(patch->globalBcNodes, &bcNodes)); 2912 PetscCall(VecRestoreArrayRead(x, &globalRHS)); 2913 PetscCall(VecRestoreArray(y, &globalUpdate)); 2914 2915 PetscCall(PetscOptionsPopGetViewerOff()); 2916 PetscCall(PetscLogEventEnd(PC_Patch_Apply, pc, 0, 0, 0)); 2917 PetscFunctionReturn(0); 2918 } 2919 2920 static PetscErrorCode PCReset_PATCH_Linear(PC pc) 2921 { 2922 PC_PATCH *patch = (PC_PATCH *) pc->data; 2923 PetscInt i; 2924 2925 PetscFunctionBegin; 2926 if (patch->solver) { 2927 for (i = 0; i < patch->npatch; ++i) PetscCall(KSPReset((KSP) patch->solver[i])); 2928 } 2929 PetscFunctionReturn(0); 2930 } 2931 2932 static PetscErrorCode PCReset_PATCH(PC pc) 2933 { 2934 PC_PATCH *patch = (PC_PATCH *) pc->data; 2935 PetscInt i; 2936 2937 PetscFunctionBegin; 2938 2939 PetscCall(PetscSFDestroy(&patch->sectionSF)); 2940 PetscCall(PetscSectionDestroy(&patch->cellCounts)); 2941 PetscCall(PetscSectionDestroy(&patch->pointCounts)); 2942 PetscCall(PetscSectionDestroy(&patch->cellNumbering)); 2943 PetscCall(PetscSectionDestroy(&patch->gtolCounts)); 2944 PetscCall(ISDestroy(&patch->gtol)); 2945 PetscCall(ISDestroy(&patch->cells)); 2946 PetscCall(ISDestroy(&patch->points)); 2947 PetscCall(ISDestroy(&patch->dofs)); 2948 PetscCall(ISDestroy(&patch->offs)); 2949 PetscCall(PetscSectionDestroy(&patch->patchSection)); 2950 PetscCall(ISDestroy(&patch->ghostBcNodes)); 2951 PetscCall(ISDestroy(&patch->globalBcNodes)); 2952 PetscCall(PetscSectionDestroy(&patch->gtolCountsWithArtificial)); 2953 PetscCall(ISDestroy(&patch->gtolWithArtificial)); 2954 PetscCall(ISDestroy(&patch->dofsWithArtificial)); 2955 PetscCall(ISDestroy(&patch->offsWithArtificial)); 2956 PetscCall(PetscSectionDestroy(&patch->gtolCountsWithAll)); 2957 PetscCall(ISDestroy(&patch->gtolWithAll)); 2958 PetscCall(ISDestroy(&patch->dofsWithAll)); 2959 PetscCall(ISDestroy(&patch->offsWithAll)); 2960 PetscCall(VecDestroy(&patch->cellMats)); 2961 PetscCall(VecDestroy(&patch->intFacetMats)); 2962 PetscCall(ISDestroy(&patch->allCells)); 2963 PetscCall(ISDestroy(&patch->intFacets)); 2964 PetscCall(ISDestroy(&patch->extFacets)); 2965 PetscCall(ISDestroy(&patch->intFacetsToPatchCell)); 2966 PetscCall(PetscSectionDestroy(&patch->intFacetCounts)); 2967 PetscCall(PetscSectionDestroy(&patch->extFacetCounts)); 2968 2969 if (patch->dofSection) for (i = 0; i < patch->nsubspaces; i++) PetscCall(PetscSectionDestroy(&patch->dofSection[i])); 2970 PetscCall(PetscFree(patch->dofSection)); 2971 PetscCall(PetscFree(patch->bs)); 2972 PetscCall(PetscFree(patch->nodesPerCell)); 2973 if (patch->cellNodeMap) for (i = 0; i < patch->nsubspaces; i++) PetscCall(PetscFree(patch->cellNodeMap[i])); 2974 PetscCall(PetscFree(patch->cellNodeMap)); 2975 PetscCall(PetscFree(patch->subspaceOffsets)); 2976 2977 PetscCall((*patch->resetsolver)(pc)); 2978 2979 if (patch->subspaces_to_exclude) { 2980 PetscCall(PetscHSetIDestroy(&patch->subspaces_to_exclude)); 2981 } 2982 2983 PetscCall(VecDestroy(&patch->localRHS)); 2984 PetscCall(VecDestroy(&patch->localUpdate)); 2985 PetscCall(VecDestroy(&patch->patchRHS)); 2986 PetscCall(VecDestroy(&patch->patchUpdate)); 2987 PetscCall(VecDestroy(&patch->dof_weights)); 2988 if (patch->patch_dof_weights) { 2989 for (i = 0; i < patch->npatch; ++i) PetscCall(VecDestroy(&patch->patch_dof_weights[i])); 2990 PetscCall(PetscFree(patch->patch_dof_weights)); 2991 } 2992 if (patch->mat) { 2993 for (i = 0; i < patch->npatch; ++i) PetscCall(MatDestroy(&patch->mat[i])); 2994 PetscCall(PetscFree(patch->mat)); 2995 } 2996 if (patch->matWithArtificial) { 2997 for (i = 0; i < patch->npatch; ++i) PetscCall(MatDestroy(&patch->matWithArtificial[i])); 2998 PetscCall(PetscFree(patch->matWithArtificial)); 2999 } 3000 PetscCall(VecDestroy(&patch->patchRHSWithArtificial)); 3001 if (patch->dofMappingWithoutToWithArtificial) { 3002 for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->dofMappingWithoutToWithArtificial[i])); 3003 PetscCall(PetscFree(patch->dofMappingWithoutToWithArtificial)); 3004 3005 } 3006 if (patch->dofMappingWithoutToWithAll) { 3007 for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->dofMappingWithoutToWithAll[i])); 3008 PetscCall(PetscFree(patch->dofMappingWithoutToWithAll)); 3009 3010 } 3011 PetscCall(PetscFree(patch->sub_mat_type)); 3012 if (patch->userIS) { 3013 for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->userIS[i])); 3014 PetscCall(PetscFree(patch->userIS)); 3015 } 3016 PetscCall(PetscFree(patch->precomputedTensorLocations)); 3017 PetscCall(PetscFree(patch->precomputedIntFacetTensorLocations)); 3018 3019 patch->bs = NULL; 3020 patch->cellNodeMap = NULL; 3021 patch->nsubspaces = 0; 3022 PetscCall(ISDestroy(&patch->iterationSet)); 3023 3024 PetscCall(PetscViewerDestroy(&patch->viewerSection)); 3025 PetscFunctionReturn(0); 3026 } 3027 3028 static PetscErrorCode PCDestroy_PATCH_Linear(PC pc) 3029 { 3030 PC_PATCH *patch = (PC_PATCH *) pc->data; 3031 PetscInt i; 3032 3033 PetscFunctionBegin; 3034 if (patch->solver) { 3035 for (i = 0; i < patch->npatch; ++i) PetscCall(KSPDestroy((KSP *) &patch->solver[i])); 3036 PetscCall(PetscFree(patch->solver)); 3037 } 3038 PetscFunctionReturn(0); 3039 } 3040 3041 static PetscErrorCode PCDestroy_PATCH(PC pc) 3042 { 3043 PC_PATCH *patch = (PC_PATCH *) pc->data; 3044 3045 PetscFunctionBegin; 3046 PetscCall(PCReset_PATCH(pc)); 3047 PetscCall((*patch->destroysolver)(pc)); 3048 PetscCall(PetscFree(pc->data)); 3049 PetscFunctionReturn(0); 3050 } 3051 3052 static PetscErrorCode PCSetFromOptions_PATCH(PetscOptionItems *PetscOptionsObject, PC pc) 3053 { 3054 PC_PATCH *patch = (PC_PATCH *) pc->data; 3055 PCPatchConstructType patchConstructionType = PC_PATCH_STAR; 3056 char sub_mat_type[PETSC_MAX_PATH_LEN]; 3057 char option[PETSC_MAX_PATH_LEN]; 3058 const char *prefix; 3059 PetscBool flg, dimflg, codimflg; 3060 MPI_Comm comm; 3061 PetscInt *ifields, nfields, k; 3062 PCCompositeType loctype = PC_COMPOSITE_ADDITIVE; 3063 3064 PetscFunctionBegin; 3065 PetscCall(PetscObjectGetComm((PetscObject) pc, &comm)); 3066 PetscCall(PetscObjectGetOptionsPrefix((PetscObject) pc, &prefix)); 3067 PetscOptionsHeadBegin(PetscOptionsObject, "Patch solver options"); 3068 3069 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_save_operators", patch->classname)); 3070 PetscCall(PetscOptionsBool(option, "Store all patch operators for lifetime of object?", "PCPatchSetSaveOperators", patch->save_operators, &patch->save_operators, &flg)); 3071 3072 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_precompute_element_tensors", patch->classname)); 3073 PetscCall(PetscOptionsBool(option, "Compute each element tensor only once?", "PCPatchSetPrecomputeElementTensors", patch->precomputeElementTensors, &patch->precomputeElementTensors, &flg)); 3074 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_partition_of_unity", patch->classname)); 3075 PetscCall(PetscOptionsBool(option, "Weight contributions by dof multiplicity?", "PCPatchSetPartitionOfUnity", patch->partition_of_unity, &patch->partition_of_unity, &flg)); 3076 3077 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_local_type", patch->classname)); 3078 PetscCall(PetscOptionsEnum(option,"Type of local solver composition (additive or multiplicative)","PCPatchSetLocalComposition",PCCompositeTypes,(PetscEnum)loctype,(PetscEnum*)&loctype,&flg)); 3079 if (flg) PetscCall(PCPatchSetLocalComposition(pc, loctype)); 3080 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_dense_inverse", patch->classname)); 3081 PetscCall(PetscOptionsBool(option, "Compute inverses of patch matrices and apply directly? Ignores KSP/PC settings on patch.", "PCPatchSetDenseInverse", patch->denseinverse, &patch->denseinverse, &flg)); 3082 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_dim", patch->classname)); 3083 PetscCall(PetscOptionsInt(option, "What dimension of mesh point to construct patches by? (0 = vertices)", "PCPATCH", patch->dim, &patch->dim, &dimflg)); 3084 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_codim", patch->classname)); 3085 PetscCall(PetscOptionsInt(option, "What co-dimension of mesh point to construct patches by? (0 = cells)", "PCPATCH", patch->codim, &patch->codim, &codimflg)); 3086 PetscCheck(!dimflg || !codimflg,comm, PETSC_ERR_ARG_WRONG, "Can only set one of dimension or co-dimension"); 3087 3088 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_type", patch->classname)); 3089 PetscCall(PetscOptionsEnum(option, "How should the patches be constructed?", "PCPatchSetConstructType", PCPatchConstructTypes, (PetscEnum) patchConstructionType, (PetscEnum *) &patchConstructionType, &flg)); 3090 if (flg) PetscCall(PCPatchSetConstructType(pc, patchConstructionType, NULL, NULL)); 3091 3092 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_vanka_dim", patch->classname)); 3093 PetscCall(PetscOptionsInt(option, "Topological dimension of entities for Vanka to ignore", "PCPATCH", patch->vankadim, &patch->vankadim, &flg)); 3094 3095 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_ignore_dim", patch->classname)); 3096 PetscCall(PetscOptionsInt(option, "Topological dimension of entities for completion to ignore", "PCPATCH", patch->ignoredim, &patch->ignoredim, &flg)); 3097 3098 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_pardecomp_overlap", patch->classname)); 3099 PetscCall(PetscOptionsInt(option, "What overlap should we use in construct type pardecomp?", "PCPATCH", patch->pardecomp_overlap, &patch->pardecomp_overlap, &flg)); 3100 3101 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_sub_mat_type", patch->classname)); 3102 PetscCall(PetscOptionsFList(option, "Matrix type for patch solves", "PCPatchSetSubMatType", MatList, NULL, sub_mat_type, PETSC_MAX_PATH_LEN, &flg)); 3103 if (flg) PetscCall(PCPatchSetSubMatType(pc, sub_mat_type)); 3104 3105 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_symmetrise_sweep", patch->classname)); 3106 PetscCall(PetscOptionsBool(option, "Go start->end, end->start?", "PCPATCH", patch->symmetrise_sweep, &patch->symmetrise_sweep, &flg)); 3107 3108 /* If the user has set the number of subspaces, use that for the buffer size, 3109 otherwise use a large number */ 3110 if (patch->nsubspaces <= 0) { 3111 nfields = 128; 3112 } else { 3113 nfields = patch->nsubspaces; 3114 } 3115 PetscCall(PetscMalloc1(nfields, &ifields)); 3116 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exclude_subspaces", patch->classname)); 3117 PetscCall(PetscOptionsGetIntArray(((PetscObject)pc)->options,((PetscObject)pc)->prefix,option,ifields,&nfields,&flg)); 3118 PetscCheck(!flg || !(patchConstructionType == PC_PATCH_USER),comm, PETSC_ERR_ARG_INCOMP, "We cannot support excluding a subspace with user patches because we do not index patches with a mesh point"); 3119 if (flg) { 3120 PetscCall(PetscHSetIClear(patch->subspaces_to_exclude)); 3121 for (k = 0; k < nfields; k++) { 3122 PetscCall(PetscHSetIAdd(patch->subspaces_to_exclude, ifields[k])); 3123 } 3124 } 3125 PetscCall(PetscFree(ifields)); 3126 3127 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_patches_view", patch->classname)); 3128 PetscCall(PetscOptionsBool(option, "Print out information during patch construction", "PCPATCH", patch->viewPatches, &patch->viewPatches, &flg)); 3129 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_cells_view", patch->classname)); 3130 PetscCall(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerCells, &patch->formatCells, &patch->viewCells)); 3131 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_interior_facets_view", patch->classname)); 3132 PetscCall(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerIntFacets, &patch->formatIntFacets, &patch->viewIntFacets)); 3133 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exterior_facets_view", patch->classname)); 3134 PetscCall(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerExtFacets, &patch->formatExtFacets, &patch->viewExtFacets)); 3135 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_points_view", patch->classname)); 3136 PetscCall(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerPoints, &patch->formatPoints, &patch->viewPoints)); 3137 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_section_view", patch->classname)); 3138 PetscCall(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerSection, &patch->formatSection, &patch->viewSection)); 3139 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_mat_view", patch->classname)); 3140 PetscCall(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerMatrix, &patch->formatMatrix, &patch->viewMatrix)); 3141 PetscOptionsHeadEnd(); 3142 patch->optionsSet = PETSC_TRUE; 3143 PetscFunctionReturn(0); 3144 } 3145 3146 static PetscErrorCode PCSetUpOnBlocks_PATCH(PC pc) 3147 { 3148 PC_PATCH *patch = (PC_PATCH*) pc->data; 3149 KSPConvergedReason reason; 3150 PetscInt i; 3151 3152 PetscFunctionBegin; 3153 if (!patch->save_operators) { 3154 /* Can't do this here because the sub KSPs don't have an operator attached yet. */ 3155 PetscFunctionReturn(0); 3156 } 3157 if (patch->denseinverse) { 3158 /* No solvers */ 3159 PetscFunctionReturn(0); 3160 } 3161 for (i = 0; i < patch->npatch; ++i) { 3162 if (!((KSP) patch->solver[i])->setfromoptionscalled) { 3163 PetscCall(KSPSetFromOptions((KSP) patch->solver[i])); 3164 } 3165 PetscCall(KSPSetUp((KSP) patch->solver[i])); 3166 PetscCall(KSPGetConvergedReason((KSP) patch->solver[i], &reason)); 3167 if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR; 3168 } 3169 PetscFunctionReturn(0); 3170 } 3171 3172 static PetscErrorCode PCView_PATCH(PC pc, PetscViewer viewer) 3173 { 3174 PC_PATCH *patch = (PC_PATCH *) pc->data; 3175 PetscViewer sviewer; 3176 PetscBool isascii; 3177 PetscMPIInt rank; 3178 3179 PetscFunctionBegin; 3180 /* TODO Redo tabbing with set tbas in new style */ 3181 PetscCall(PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii)); 3182 if (!isascii) PetscFunctionReturn(0); 3183 PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) pc), &rank)); 3184 PetscCall(PetscViewerASCIIPushTab(viewer)); 3185 PetscCall(PetscViewerASCIIPrintf(viewer, "Subspace Correction preconditioner with %" PetscInt_FMT " patches\n", patch->npatch)); 3186 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 3187 PetscCall(PetscViewerASCIIPrintf(viewer, "Schwarz type: multiplicative\n")); 3188 } else { 3189 PetscCall(PetscViewerASCIIPrintf(viewer, "Schwarz type: additive\n")); 3190 } 3191 if (patch->partition_of_unity) PetscCall(PetscViewerASCIIPrintf(viewer, "Weighting by partition of unity\n")); 3192 else PetscCall(PetscViewerASCIIPrintf(viewer, "Not weighting by partition of unity\n")); 3193 if (patch->symmetrise_sweep) PetscCall(PetscViewerASCIIPrintf(viewer, "Symmetrising sweep (start->end, then end->start)\n")); 3194 else PetscCall(PetscViewerASCIIPrintf(viewer, "Not symmetrising sweep\n")); 3195 if (!patch->precomputeElementTensors) PetscCall(PetscViewerASCIIPrintf(viewer, "Not precomputing element tensors (overlapping cells rebuilt in every patch assembly)\n")); 3196 else PetscCall(PetscViewerASCIIPrintf(viewer, "Precomputing element tensors (each cell assembled only once)\n")); 3197 if (!patch->save_operators) PetscCall(PetscViewerASCIIPrintf(viewer, "Not saving patch operators (rebuilt every PCApply)\n")); 3198 else PetscCall(PetscViewerASCIIPrintf(viewer, "Saving patch operators (rebuilt every PCSetUp)\n")); 3199 if (patch->patchconstructop == PCPatchConstruct_Star) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: star\n")); 3200 else if (patch->patchconstructop == PCPatchConstruct_Vanka) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: Vanka\n")); 3201 else if (patch->patchconstructop == PCPatchConstruct_User) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: user-specified\n")); 3202 else PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: unknown\n")); 3203 3204 if (patch->denseinverse) { 3205 PetscCall(PetscViewerASCIIPrintf(viewer, "Explicitly forming dense inverse and applying patch solver via MatMult.\n")); 3206 } else { 3207 if (patch->isNonlinear) { 3208 PetscCall(PetscViewerASCIIPrintf(viewer, "SNES on patches (all same):\n")); 3209 } else { 3210 PetscCall(PetscViewerASCIIPrintf(viewer, "KSP on patches (all same):\n")); 3211 } 3212 if (patch->solver) { 3213 PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer)); 3214 if (rank == 0) { 3215 PetscCall(PetscViewerASCIIPushTab(sviewer)); 3216 PetscCall(PetscObjectView(patch->solver[0], sviewer)); 3217 PetscCall(PetscViewerASCIIPopTab(sviewer)); 3218 } 3219 PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer)); 3220 } else { 3221 PetscCall(PetscViewerASCIIPushTab(viewer)); 3222 PetscCall(PetscViewerASCIIPrintf(viewer, "Solver not yet set.\n")); 3223 PetscCall(PetscViewerASCIIPopTab(viewer)); 3224 } 3225 } 3226 PetscCall(PetscViewerASCIIPopTab(viewer)); 3227 PetscFunctionReturn(0); 3228 } 3229 3230 /*MC 3231 PCPATCH - A PC object that encapsulates flexible definition of blocks for overlapping and non-overlapping 3232 small block additive preconditioners. Block definition is based on topology from 3233 a DM and equation numbering from a PetscSection. 3234 3235 Options Database Keys: 3236 + -pc_patch_cells_view - Views the process local cell numbers for each patch 3237 . -pc_patch_points_view - Views the process local mesh point numbers for each patch 3238 . -pc_patch_g2l_view - Views the map between global dofs and patch local dofs for each patch 3239 . -pc_patch_patches_view - Views the global dofs associated with each patch and its boundary 3240 - -pc_patch_sub_mat_view - Views the matrix associated with each patch 3241 3242 Level: intermediate 3243 3244 .seealso: `PCType`, `PCCreate()`, `PCSetType()` 3245 M*/ 3246 PETSC_EXTERN PetscErrorCode PCCreate_Patch(PC pc) 3247 { 3248 PC_PATCH *patch; 3249 3250 PetscFunctionBegin; 3251 PetscCall(PetscNewLog(pc, &patch)); 3252 3253 if (patch->subspaces_to_exclude) { 3254 PetscCall(PetscHSetIDestroy(&patch->subspaces_to_exclude)); 3255 } 3256 PetscCall(PetscHSetICreate(&patch->subspaces_to_exclude)); 3257 3258 patch->classname = "pc"; 3259 patch->isNonlinear = PETSC_FALSE; 3260 3261 /* Set some defaults */ 3262 patch->combined = PETSC_FALSE; 3263 patch->save_operators = PETSC_TRUE; 3264 patch->local_composition_type = PC_COMPOSITE_ADDITIVE; 3265 patch->precomputeElementTensors = PETSC_FALSE; 3266 patch->partition_of_unity = PETSC_FALSE; 3267 patch->codim = -1; 3268 patch->dim = -1; 3269 patch->vankadim = -1; 3270 patch->ignoredim = -1; 3271 patch->pardecomp_overlap = 0; 3272 patch->patchconstructop = PCPatchConstruct_Star; 3273 patch->symmetrise_sweep = PETSC_FALSE; 3274 patch->npatch = 0; 3275 patch->userIS = NULL; 3276 patch->optionsSet = PETSC_FALSE; 3277 patch->iterationSet = NULL; 3278 patch->user_patches = PETSC_FALSE; 3279 PetscCall(PetscStrallocpy(MATDENSE, (char **) &patch->sub_mat_type)); 3280 patch->viewPatches = PETSC_FALSE; 3281 patch->viewCells = PETSC_FALSE; 3282 patch->viewPoints = PETSC_FALSE; 3283 patch->viewSection = PETSC_FALSE; 3284 patch->viewMatrix = PETSC_FALSE; 3285 patch->densesolve = NULL; 3286 patch->setupsolver = PCSetUp_PATCH_Linear; 3287 patch->applysolver = PCApply_PATCH_Linear; 3288 patch->resetsolver = PCReset_PATCH_Linear; 3289 patch->destroysolver = PCDestroy_PATCH_Linear; 3290 patch->updatemultiplicative = PCUpdateMultiplicative_PATCH_Linear; 3291 patch->dofMappingWithoutToWithArtificial = NULL; 3292 patch->dofMappingWithoutToWithAll = NULL; 3293 3294 pc->data = (void *) patch; 3295 pc->ops->apply = PCApply_PATCH; 3296 pc->ops->applytranspose = NULL; /* PCApplyTranspose_PATCH; */ 3297 pc->ops->setup = PCSetUp_PATCH; 3298 pc->ops->reset = PCReset_PATCH; 3299 pc->ops->destroy = PCDestroy_PATCH; 3300 pc->ops->setfromoptions = PCSetFromOptions_PATCH; 3301 pc->ops->setuponblocks = PCSetUpOnBlocks_PATCH; 3302 pc->ops->view = PCView_PATCH; 3303 pc->ops->applyrichardson = NULL; 3304 3305 PetscFunctionReturn(0); 3306 } 3307