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 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 /* Cannot reuse the same IS because the geometry info is being cached in it */ 1982 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS)); 1983 PetscCallBack("PCPatch callback",patch->usercomputef(pc, point, x, F, patch->cellIS, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell,dofsArrayWithAll + offset*patch->totalDofsPerCell,patch->usercomputefctx)); 1984 PetscCall(ISDestroy(&patch->cellIS)); 1985 PetscCall(ISRestoreIndices(patch->dofs, &dofsArray)); 1986 PetscCall(ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll)); 1987 PetscCall(ISRestoreIndices(patch->cells, &cellsArray)); 1988 if (patch->viewMatrix) { 1989 char name[PETSC_MAX_PATH_LEN]; 1990 1991 PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch vector for Point %" PetscInt_FMT, point)); 1992 PetscCall(PetscObjectSetName((PetscObject) F, name)); 1993 PetscCall(ObjectView((PetscObject) F, patch->viewerMatrix, patch->formatMatrix)); 1994 } 1995 PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 1996 PetscFunctionReturn(0); 1997 } 1998 1999 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) 2000 { 2001 PC_PATCH *patch = (PC_PATCH *) pc->data; 2002 DM dm, plex; 2003 PetscSection s; 2004 const PetscInt *parray, *oarray; 2005 PetscInt Nf = patch->nsubspaces, Np, poff, p, f; 2006 2007 PetscFunctionBegin; 2008 PetscCall(PCGetDM(pc, &dm)); 2009 PetscCall(DMConvert(dm, DMPLEX, &plex)); 2010 dm = plex; 2011 PetscCall(DMGetLocalSection(dm, &s)); 2012 /* Set offset into patch */ 2013 PetscCall(PetscSectionGetDof(patch->pointCounts, patchNum, &Np)); 2014 PetscCall(PetscSectionGetOffset(patch->pointCounts, patchNum, &poff)); 2015 PetscCall(ISGetIndices(patch->points, &parray)); 2016 PetscCall(ISGetIndices(patch->offs, &oarray)); 2017 for (f = 0; f < Nf; ++f) { 2018 for (p = 0; p < Np; ++p) { 2019 const PetscInt point = parray[poff+p]; 2020 PetscInt dof; 2021 2022 PetscCall(PetscSectionGetFieldDof(patch->patchSection, point, f, &dof)); 2023 PetscCall(PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f])); 2024 if (patch->nsubspaces == 1) PetscCall(PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f])); 2025 else PetscCall(PetscSectionSetOffset(patch->patchSection, point, -1)); 2026 } 2027 } 2028 PetscCall(ISRestoreIndices(patch->points, &parray)); 2029 PetscCall(ISRestoreIndices(patch->offs, &oarray)); 2030 if (patch->viewSection) PetscCall(ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection)); 2031 /* TODO Shut off MatViewFromOptions() in MatAssemblyEnd() here */ 2032 PetscCall(DMPlexComputeJacobian_Patch_Internal(dm, patch->patchSection, patch->patchSection, cellIS, 0.0, 0.0, x, NULL, J, J, ctx)); 2033 PetscCall(DMDestroy(&dm)); 2034 PetscFunctionReturn(0); 2035 } 2036 2037 /* This function zeros mat on entry */ 2038 PetscErrorCode PCPatchComputeOperator_Internal(PC pc, Vec x, Mat mat, PetscInt point, PetscBool withArtificial) 2039 { 2040 PC_PATCH *patch = (PC_PATCH *) pc->data; 2041 const PetscInt *dofsArray; 2042 const PetscInt *dofsArrayWithAll = NULL; 2043 const PetscInt *cellsArray; 2044 PetscInt ncell, offset, pStart, pEnd, numIntFacets, intFacetOffset; 2045 PetscBool isNonlinear; 2046 2047 PetscFunctionBegin; 2048 PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0)); 2049 isNonlinear = patch->isNonlinear; 2050 PetscCheck(patch->usercomputeop,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set callback"); 2051 if (withArtificial) { 2052 PetscCall(ISGetIndices(patch->dofsWithArtificial, &dofsArray)); 2053 } else { 2054 PetscCall(ISGetIndices(patch->dofs, &dofsArray)); 2055 } 2056 if (isNonlinear) { 2057 PetscCall(ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll)); 2058 } 2059 PetscCall(ISGetIndices(patch->cells, &cellsArray)); 2060 PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd)); 2061 2062 point += pStart; 2063 PetscCheck(point < pEnd,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %" PetscInt_FMT " not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, pStart, pEnd); 2064 2065 PetscCall(PetscSectionGetDof(patch->cellCounts, point, &ncell)); 2066 PetscCall(PetscSectionGetOffset(patch->cellCounts, point, &offset)); 2067 if (ncell <= 0) { 2068 PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 2069 PetscFunctionReturn(0); 2070 } 2071 PetscCall(MatZeroEntries(mat)); 2072 if (patch->precomputeElementTensors) { 2073 PetscInt i; 2074 PetscInt ndof = patch->totalDofsPerCell; 2075 const PetscScalar *elementTensors; 2076 2077 PetscCall(VecGetArrayRead(patch->cellMats, &elementTensors)); 2078 for (i = 0; i < ncell; i++) { 2079 const PetscInt cell = cellsArray[i + offset]; 2080 const PetscInt *idx = dofsArray + (offset + i)*ndof; 2081 const PetscScalar *v = elementTensors + patch->precomputedTensorLocations[cell]*ndof*ndof; 2082 PetscCall(MatSetValues(mat, ndof, idx, ndof, idx, v, ADD_VALUES)); 2083 } 2084 PetscCall(VecRestoreArrayRead(patch->cellMats, &elementTensors)); 2085 PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY)); 2086 PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY)); 2087 } else { 2088 /* Cannot reuse the same IS because the geometry info is being cached in it */ 2089 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS)); 2090 PetscCallBack("PCPatch callback",patch->usercomputeop(pc, point, x, mat, patch->cellIS, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell, dofsArrayWithAll ? dofsArrayWithAll + offset*patch->totalDofsPerCell : NULL, patch->usercomputeopctx)); 2091 } 2092 if (patch->usercomputeopintfacet) { 2093 PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets)); 2094 PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset)); 2095 if (numIntFacets > 0) { 2096 /* For each interior facet, grab the two cells (in local numbering, and concatenate dof numberings for those cells) */ 2097 PetscInt *facetDofs = NULL, *facetDofsWithAll = NULL; 2098 const PetscInt *intFacetsArray = NULL; 2099 PetscInt idx = 0; 2100 PetscInt i, c, d; 2101 PetscInt fStart; 2102 DM dm, plex; 2103 IS facetIS = NULL; 2104 const PetscInt *facetCells = NULL; 2105 2106 PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells)); 2107 PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray)); 2108 PetscCall(PCGetDM(pc, &dm)); 2109 PetscCall(DMConvert(dm, DMPLEX, &plex)); 2110 dm = plex; 2111 PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, NULL)); 2112 /* FIXME: Pull this malloc out. */ 2113 PetscCall(PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofs)); 2114 if (dofsArrayWithAll) { 2115 PetscCall(PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofsWithAll)); 2116 } 2117 if (patch->precomputeElementTensors) { 2118 PetscInt nFacetDof = 2*patch->totalDofsPerCell; 2119 const PetscScalar *elementTensors; 2120 2121 PetscCall(VecGetArrayRead(patch->intFacetMats, &elementTensors)); 2122 2123 for (i = 0; i < numIntFacets; i++) { 2124 const PetscInt facet = intFacetsArray[i + intFacetOffset]; 2125 const PetscScalar *v = elementTensors + patch->precomputedIntFacetTensorLocations[facet - fStart]*nFacetDof*nFacetDof; 2126 idx = 0; 2127 /* 2128 * 0--1 2129 * |\-| 2130 * |+\| 2131 * 2--3 2132 * [0, 2, 3, 0, 1, 3] 2133 */ 2134 for (c = 0; c < 2; c++) { 2135 const PetscInt cell = facetCells[2*(intFacetOffset + i) + c]; 2136 for (d = 0; d < patch->totalDofsPerCell; d++) { 2137 facetDofs[idx] = dofsArray[(offset + cell)*patch->totalDofsPerCell + d]; 2138 idx++; 2139 } 2140 } 2141 PetscCall(MatSetValues(mat, nFacetDof, facetDofs, nFacetDof, facetDofs, v, ADD_VALUES)); 2142 } 2143 PetscCall(VecRestoreArrayRead(patch->intFacetMats, &elementTensors)); 2144 } else { 2145 /* 2146 * 0--1 2147 * |\-| 2148 * |+\| 2149 * 2--3 2150 * [0, 2, 3, 0, 1, 3] 2151 */ 2152 for (i = 0; i < numIntFacets; i++) { 2153 for (c = 0; c < 2; c++) { 2154 const PetscInt cell = facetCells[2*(intFacetOffset + i) + c]; 2155 for (d = 0; d < patch->totalDofsPerCell; d++) { 2156 facetDofs[idx] = dofsArray[(offset + cell)*patch->totalDofsPerCell + d]; 2157 if (dofsArrayWithAll) { 2158 facetDofsWithAll[idx] = dofsArrayWithAll[(offset + cell)*patch->totalDofsPerCell + d]; 2159 } 2160 idx++; 2161 } 2162 } 2163 } 2164 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray + intFacetOffset, PETSC_USE_POINTER, &facetIS)); 2165 PetscCall(patch->usercomputeopintfacet(pc, point, x, mat, facetIS, 2*numIntFacets*patch->totalDofsPerCell, facetDofs, facetDofsWithAll, patch->usercomputeopintfacetctx)); 2166 PetscCall(ISDestroy(&facetIS)); 2167 } 2168 PetscCall(ISRestoreIndices(patch->intFacetsToPatchCell, &facetCells)); 2169 PetscCall(ISRestoreIndices(patch->intFacets, &intFacetsArray)); 2170 PetscCall(PetscFree(facetDofs)); 2171 PetscCall(PetscFree(facetDofsWithAll)); 2172 PetscCall(DMDestroy(&dm)); 2173 } 2174 } 2175 2176 PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY)); 2177 PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY)); 2178 2179 if (!(withArtificial || isNonlinear) && patch->denseinverse) { 2180 MatFactorInfo info; 2181 PetscBool flg; 2182 PetscCall(PetscObjectTypeCompare((PetscObject)mat, MATSEQDENSE, &flg)); 2183 PetscCheck(flg,PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Invalid Mat type for dense inverse"); 2184 PetscCall(MatFactorInfoInitialize(&info)); 2185 PetscCall(MatLUFactor(mat, NULL, NULL, &info)); 2186 PetscCall(MatSeqDenseInvertFactors_Private(mat)); 2187 } 2188 PetscCall(ISDestroy(&patch->cellIS)); 2189 if (withArtificial) { 2190 PetscCall(ISRestoreIndices(patch->dofsWithArtificial, &dofsArray)); 2191 } else { 2192 PetscCall(ISRestoreIndices(patch->dofs, &dofsArray)); 2193 } 2194 if (isNonlinear) { 2195 PetscCall(ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll)); 2196 } 2197 PetscCall(ISRestoreIndices(patch->cells, &cellsArray)); 2198 if (patch->viewMatrix) { 2199 char name[PETSC_MAX_PATH_LEN]; 2200 2201 PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch matrix for Point %" PetscInt_FMT, point)); 2202 PetscCall(PetscObjectSetName((PetscObject) mat, name)); 2203 PetscCall(ObjectView((PetscObject) mat, patch->viewerMatrix, patch->formatMatrix)); 2204 } 2205 PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 2206 PetscFunctionReturn(0); 2207 } 2208 2209 static PetscErrorCode MatSetValues_PCPatch_Private(Mat mat, PetscInt m, const PetscInt idxm[], 2210 PetscInt n, const PetscInt idxn[], const PetscScalar *v, InsertMode addv) 2211 { 2212 Vec data; 2213 PetscScalar *array; 2214 PetscInt bs, nz, i, j, cell; 2215 2216 PetscCall(MatShellGetContext(mat, &data)); 2217 PetscCall(VecGetBlockSize(data, &bs)); 2218 PetscCall(VecGetSize(data, &nz)); 2219 PetscCall(VecGetArray(data, &array)); 2220 PetscCheck(m == n,PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Only for square insertion"); 2221 cell = (PetscInt)(idxm[0]/bs); /* use the fact that this is called once per cell */ 2222 for (i = 0; i < m; i++) { 2223 PetscCheck(idxm[i] == idxn[i],PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Row and column indices must match!"); 2224 for (j = 0; j < n; j++) { 2225 const PetscScalar v_ = v[i*bs + j]; 2226 /* Indexing is special to the data structure we have! */ 2227 if (addv == INSERT_VALUES) { 2228 array[cell*bs*bs + i*bs + j] = v_; 2229 } else { 2230 array[cell*bs*bs + i*bs + j] += v_; 2231 } 2232 } 2233 } 2234 PetscCall(VecRestoreArray(data, &array)); 2235 PetscFunctionReturn(0); 2236 } 2237 2238 static PetscErrorCode PCPatchPrecomputePatchTensors_Private(PC pc) 2239 { 2240 PC_PATCH *patch = (PC_PATCH *)pc->data; 2241 const PetscInt *cellsArray; 2242 PetscInt ncell, offset; 2243 const PetscInt *dofMapArray; 2244 PetscInt i, j; 2245 IS dofMap; 2246 IS cellIS; 2247 const PetscInt ndof = patch->totalDofsPerCell; 2248 Mat vecMat; 2249 PetscInt cStart, cEnd; 2250 DM dm, plex; 2251 2252 PetscCall(ISGetSize(patch->cells, &ncell)); 2253 if (!ncell) { /* No cells to assemble over -> skip */ 2254 PetscFunctionReturn(0); 2255 } 2256 2257 PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0)); 2258 2259 PetscCall(PCGetDM(pc, &dm)); 2260 PetscCall(DMConvert(dm, DMPLEX, &plex)); 2261 dm = plex; 2262 if (!patch->allCells) { 2263 PetscHSetI cells; 2264 PetscHashIter hi; 2265 PetscInt pStart, pEnd; 2266 PetscInt *allCells = NULL; 2267 PetscCall(PetscHSetICreate(&cells)); 2268 PetscCall(ISGetIndices(patch->cells, &cellsArray)); 2269 PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd)); 2270 for (i = pStart; i < pEnd; i++) { 2271 PetscCall(PetscSectionGetDof(patch->cellCounts, i, &ncell)); 2272 PetscCall(PetscSectionGetOffset(patch->cellCounts, i, &offset)); 2273 if (ncell <= 0) continue; 2274 for (j = 0; j < ncell; j++) { 2275 PetscCall(PetscHSetIAdd(cells, cellsArray[offset + j])); 2276 } 2277 } 2278 PetscCall(ISRestoreIndices(patch->cells, &cellsArray)); 2279 PetscCall(PetscHSetIGetSize(cells, &ncell)); 2280 PetscCall(PetscMalloc1(ncell, &allCells)); 2281 PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 2282 PetscCall(PetscMalloc1(cEnd-cStart, &patch->precomputedTensorLocations)); 2283 i = 0; 2284 PetscHashIterBegin(cells, hi); 2285 while (!PetscHashIterAtEnd(cells, hi)) { 2286 PetscHashIterGetKey(cells, hi, allCells[i]); 2287 patch->precomputedTensorLocations[allCells[i]] = i; 2288 PetscHashIterNext(cells, hi); 2289 i++; 2290 } 2291 PetscCall(PetscHSetIDestroy(&cells)); 2292 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, allCells, PETSC_OWN_POINTER, &patch->allCells)); 2293 } 2294 PetscCall(ISGetSize(patch->allCells, &ncell)); 2295 if (!patch->cellMats) { 2296 PetscCall(VecCreateSeq(PETSC_COMM_SELF, ncell*ndof*ndof, &patch->cellMats)); 2297 PetscCall(VecSetBlockSize(patch->cellMats, ndof)); 2298 } 2299 PetscCall(VecSet(patch->cellMats, 0)); 2300 2301 PetscCall(MatCreateShell(PETSC_COMM_SELF, ncell*ndof, ncell*ndof, ncell*ndof, ncell*ndof,(void*)patch->cellMats, &vecMat)); 2302 PetscCall(MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void(*)(void))&MatSetValues_PCPatch_Private)); 2303 PetscCall(ISGetSize(patch->allCells, &ncell)); 2304 PetscCall(ISCreateStride(PETSC_COMM_SELF, ndof*ncell, 0, 1, &dofMap)); 2305 PetscCall(ISGetIndices(dofMap, &dofMapArray)); 2306 PetscCall(ISGetIndices(patch->allCells, &cellsArray)); 2307 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray, PETSC_USE_POINTER, &cellIS)); 2308 /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */ 2309 PetscCallBack("PCPatch callback",patch->usercomputeop(pc, -1, NULL, vecMat, cellIS, ndof*ncell, dofMapArray, NULL, patch->usercomputeopctx)); 2310 PetscCall(ISDestroy(&cellIS)); 2311 PetscCall(MatDestroy(&vecMat)); 2312 PetscCall(ISRestoreIndices(patch->allCells, &cellsArray)); 2313 PetscCall(ISRestoreIndices(dofMap, &dofMapArray)); 2314 PetscCall(ISDestroy(&dofMap)); 2315 2316 if (patch->usercomputeopintfacet) { 2317 PetscInt nIntFacets; 2318 IS intFacetsIS; 2319 const PetscInt *intFacetsArray = NULL; 2320 if (!patch->allIntFacets) { 2321 PetscHSetI facets; 2322 PetscHashIter hi; 2323 PetscInt pStart, pEnd, fStart, fEnd; 2324 PetscInt *allIntFacets = NULL; 2325 PetscCall(PetscHSetICreate(&facets)); 2326 PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray)); 2327 PetscCall(PetscSectionGetChart(patch->intFacetCounts, &pStart, &pEnd)); 2328 PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd)); 2329 for (i = pStart; i < pEnd; i++) { 2330 PetscCall(PetscSectionGetDof(patch->intFacetCounts, i, &nIntFacets)); 2331 PetscCall(PetscSectionGetOffset(patch->intFacetCounts, i, &offset)); 2332 if (nIntFacets <= 0) continue; 2333 for (j = 0; j < nIntFacets; j++) { 2334 PetscCall(PetscHSetIAdd(facets, intFacetsArray[offset + j])); 2335 } 2336 } 2337 PetscCall(ISRestoreIndices(patch->intFacets, &intFacetsArray)); 2338 PetscCall(PetscHSetIGetSize(facets, &nIntFacets)); 2339 PetscCall(PetscMalloc1(nIntFacets, &allIntFacets)); 2340 PetscCall(PetscMalloc1(fEnd-fStart, &patch->precomputedIntFacetTensorLocations)); 2341 i = 0; 2342 PetscHashIterBegin(facets, hi); 2343 while (!PetscHashIterAtEnd(facets, hi)) { 2344 PetscHashIterGetKey(facets, hi, allIntFacets[i]); 2345 patch->precomputedIntFacetTensorLocations[allIntFacets[i] - fStart] = i; 2346 PetscHashIterNext(facets, hi); 2347 i++; 2348 } 2349 PetscCall(PetscHSetIDestroy(&facets)); 2350 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, allIntFacets, PETSC_OWN_POINTER, &patch->allIntFacets)); 2351 } 2352 PetscCall(ISGetSize(patch->allIntFacets, &nIntFacets)); 2353 if (!patch->intFacetMats) { 2354 PetscCall(VecCreateSeq(PETSC_COMM_SELF, nIntFacets*ndof*ndof*4, &patch->intFacetMats)); 2355 PetscCall(VecSetBlockSize(patch->intFacetMats, ndof*2)); 2356 } 2357 PetscCall(VecSet(patch->intFacetMats, 0)); 2358 2359 PetscCall(MatCreateShell(PETSC_COMM_SELF, nIntFacets*ndof*2, nIntFacets*ndof*2, nIntFacets*ndof*2, nIntFacets*ndof*2,(void*)patch->intFacetMats, &vecMat)); 2360 PetscCall(MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void(*)(void))&MatSetValues_PCPatch_Private)); 2361 PetscCall(ISCreateStride(PETSC_COMM_SELF, 2*ndof*nIntFacets, 0, 1, &dofMap)); 2362 PetscCall(ISGetIndices(dofMap, &dofMapArray)); 2363 PetscCall(ISGetIndices(patch->allIntFacets, &intFacetsArray)); 2364 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, intFacetsArray, PETSC_USE_POINTER, &intFacetsIS)); 2365 /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */ 2366 PetscCallBack("PCPatch callback (interior facets)",patch->usercomputeopintfacet(pc, -1, NULL, vecMat, intFacetsIS, 2*ndof*nIntFacets, dofMapArray, NULL, patch->usercomputeopintfacetctx)); 2367 PetscCall(ISDestroy(&intFacetsIS)); 2368 PetscCall(MatDestroy(&vecMat)); 2369 PetscCall(ISRestoreIndices(patch->allIntFacets, &intFacetsArray)); 2370 PetscCall(ISRestoreIndices(dofMap, &dofMapArray)); 2371 PetscCall(ISDestroy(&dofMap)); 2372 } 2373 PetscCall(DMDestroy(&dm)); 2374 PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 2375 2376 PetscFunctionReturn(0); 2377 } 2378 2379 PetscErrorCode PCPatch_ScatterLocal_Private(PC pc, PetscInt p, Vec x, Vec y, InsertMode mode, ScatterMode scat, PatchScatterType scattertype) 2380 { 2381 PC_PATCH *patch = (PC_PATCH *) pc->data; 2382 const PetscScalar *xArray = NULL; 2383 PetscScalar *yArray = NULL; 2384 const PetscInt *gtolArray = NULL; 2385 PetscInt dof, offset, lidx; 2386 2387 PetscFunctionBeginHot; 2388 PetscCall(VecGetArrayRead(x, &xArray)); 2389 PetscCall(VecGetArray(y, &yArray)); 2390 if (scattertype == SCATTER_WITHARTIFICIAL) { 2391 PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof)); 2392 PetscCall(PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offset)); 2393 PetscCall(ISGetIndices(patch->gtolWithArtificial, >olArray)); 2394 } else if (scattertype == SCATTER_WITHALL) { 2395 PetscCall(PetscSectionGetDof(patch->gtolCountsWithAll, p, &dof)); 2396 PetscCall(PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offset)); 2397 PetscCall(ISGetIndices(patch->gtolWithAll, >olArray)); 2398 } else { 2399 PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &dof)); 2400 PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset)); 2401 PetscCall(ISGetIndices(patch->gtol, >olArray)); 2402 } 2403 PetscCheck(mode != INSERT_VALUES || scat == SCATTER_FORWARD,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't insert if not scattering forward"); 2404 PetscCheck(mode != ADD_VALUES || scat == SCATTER_REVERSE,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't add if not scattering reverse"); 2405 for (lidx = 0; lidx < dof; ++lidx) { 2406 const PetscInt gidx = gtolArray[offset+lidx]; 2407 2408 if (mode == INSERT_VALUES) yArray[lidx] = xArray[gidx]; /* Forward */ 2409 else yArray[gidx] += xArray[lidx]; /* Reverse */ 2410 } 2411 if (scattertype == SCATTER_WITHARTIFICIAL) { 2412 PetscCall(ISRestoreIndices(patch->gtolWithArtificial, >olArray)); 2413 } else if (scattertype == SCATTER_WITHALL) { 2414 PetscCall(ISRestoreIndices(patch->gtolWithAll, >olArray)); 2415 } else { 2416 PetscCall(ISRestoreIndices(patch->gtol, >olArray)); 2417 } 2418 PetscCall(VecRestoreArrayRead(x, &xArray)); 2419 PetscCall(VecRestoreArray(y, &yArray)); 2420 PetscFunctionReturn(0); 2421 } 2422 2423 static PetscErrorCode PCSetUp_PATCH_Linear(PC pc) 2424 { 2425 PC_PATCH *patch = (PC_PATCH *) pc->data; 2426 const char *prefix; 2427 PetscInt i; 2428 2429 PetscFunctionBegin; 2430 if (!pc->setupcalled) { 2431 PetscCheck(patch->save_operators || !patch->denseinverse,PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Can't have dense inverse without save operators"); 2432 if (!patch->denseinverse) { 2433 PetscCall(PetscMalloc1(patch->npatch, &patch->solver)); 2434 PetscCall(PCGetOptionsPrefix(pc, &prefix)); 2435 for (i = 0; i < patch->npatch; ++i) { 2436 KSP ksp; 2437 PC subpc; 2438 2439 PetscCall(KSPCreate(PETSC_COMM_SELF, &ksp)); 2440 PetscCall(KSPSetErrorIfNotConverged(ksp, pc->erroriffailure)); 2441 PetscCall(KSPSetOptionsPrefix(ksp, prefix)); 2442 PetscCall(KSPAppendOptionsPrefix(ksp, "sub_")); 2443 PetscCall(PetscObjectIncrementTabLevel((PetscObject) ksp, (PetscObject) pc, 1)); 2444 PetscCall(KSPGetPC(ksp, &subpc)); 2445 PetscCall(PetscObjectIncrementTabLevel((PetscObject) subpc, (PetscObject) pc, 1)); 2446 PetscCall(PetscLogObjectParent((PetscObject) pc, (PetscObject) ksp)); 2447 patch->solver[i] = (PetscObject) ksp; 2448 } 2449 } 2450 } 2451 if (patch->save_operators) { 2452 if (patch->precomputeElementTensors) PetscCall(PCPatchPrecomputePatchTensors_Private(pc)); 2453 for (i = 0; i < patch->npatch; ++i) { 2454 PetscCall(PCPatchComputeOperator_Internal(pc, NULL, patch->mat[i], i, PETSC_FALSE)); 2455 if (!patch->denseinverse) { 2456 PetscCall(KSPSetOperators((KSP) patch->solver[i], patch->mat[i], patch->mat[i])); 2457 } else if (patch->mat[i] && !patch->densesolve) { 2458 /* Setup matmult callback */ 2459 PetscCall(MatGetOperation(patch->mat[i], MATOP_MULT, (void (**)(void))&patch->densesolve)); 2460 } 2461 } 2462 } 2463 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 2464 for (i = 0; i < patch->npatch; ++i) { 2465 /* Instead of padding patch->patchUpdate with zeros to get */ 2466 /* patch->patchUpdateWithArtificial and then multiplying with the matrix, */ 2467 /* just get rid of the columns that correspond to the dofs with */ 2468 /* artificial bcs. That's of course fairly inefficient, hopefully we */ 2469 /* can just assemble the rectangular matrix in the first place. */ 2470 Mat matSquare; 2471 IS rowis; 2472 PetscInt dof; 2473 2474 PetscCall(MatGetSize(patch->mat[i], &dof, NULL)); 2475 if (dof == 0) { 2476 patch->matWithArtificial[i] = NULL; 2477 continue; 2478 } 2479 2480 PetscCall(PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE)); 2481 PetscCall(PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE)); 2482 2483 PetscCall(MatGetSize(matSquare, &dof, NULL)); 2484 PetscCall(ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis)); 2485 if (pc->setupcalled) { 2486 PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_REUSE_MATRIX, &patch->matWithArtificial[i])); 2487 } else { 2488 PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &patch->matWithArtificial[i])); 2489 } 2490 PetscCall(ISDestroy(&rowis)); 2491 PetscCall(MatDestroy(&matSquare)); 2492 } 2493 } 2494 PetscFunctionReturn(0); 2495 } 2496 2497 static PetscErrorCode PCSetUp_PATCH(PC pc) 2498 { 2499 PC_PATCH *patch = (PC_PATCH *) pc->data; 2500 PetscInt i; 2501 PetscBool isNonlinear; 2502 PetscInt maxDof = -1, maxDofWithArtificial = -1; 2503 2504 PetscFunctionBegin; 2505 if (!pc->setupcalled) { 2506 PetscInt pStart, pEnd, p; 2507 PetscInt localSize; 2508 2509 PetscCall(PetscLogEventBegin(PC_Patch_CreatePatches, pc, 0, 0, 0)); 2510 2511 isNonlinear = patch->isNonlinear; 2512 if (!patch->nsubspaces) { 2513 DM dm, plex; 2514 PetscSection s; 2515 PetscInt cStart, cEnd, c, Nf, f, numGlobalBcs = 0, *globalBcs, *Nb, **cellDofs; 2516 2517 PetscCall(PCGetDM(pc, &dm)); 2518 PetscCheck(dm,PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONG, "Must set DM for PCPATCH or call PCPatchSetDiscretisationInfo()"); 2519 PetscCall(DMConvert(dm, DMPLEX, &plex)); 2520 dm = plex; 2521 PetscCall(DMGetLocalSection(dm, &s)); 2522 PetscCall(PetscSectionGetNumFields(s, &Nf)); 2523 PetscCall(PetscSectionGetChart(s, &pStart, &pEnd)); 2524 for (p = pStart; p < pEnd; ++p) { 2525 PetscInt cdof; 2526 PetscCall(PetscSectionGetConstraintDof(s, p, &cdof)); 2527 numGlobalBcs += cdof; 2528 } 2529 PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 2530 PetscCall(PetscMalloc3(Nf, &Nb, Nf, &cellDofs, numGlobalBcs, &globalBcs)); 2531 for (f = 0; f < Nf; ++f) { 2532 PetscFE fe; 2533 PetscDualSpace sp; 2534 PetscInt cdoff = 0; 2535 2536 PetscCall(DMGetField(dm, f, NULL, (PetscObject *) &fe)); 2537 /* PetscCall(PetscFEGetNumComponents(fe, &Nc[f])); */ 2538 PetscCall(PetscFEGetDualSpace(fe, &sp)); 2539 PetscCall(PetscDualSpaceGetDimension(sp, &Nb[f])); 2540 2541 PetscCall(PetscMalloc1((cEnd-cStart)*Nb[f], &cellDofs[f])); 2542 for (c = cStart; c < cEnd; ++c) { 2543 PetscInt *closure = NULL; 2544 PetscInt clSize = 0, cl; 2545 2546 PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure)); 2547 for (cl = 0; cl < clSize*2; cl += 2) { 2548 const PetscInt p = closure[cl]; 2549 PetscInt fdof, d, foff; 2550 2551 PetscCall(PetscSectionGetFieldDof(s, p, f, &fdof)); 2552 PetscCall(PetscSectionGetFieldOffset(s, p, f, &foff)); 2553 for (d = 0; d < fdof; ++d, ++cdoff) cellDofs[f][cdoff] = foff + d; 2554 } 2555 PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure)); 2556 } 2557 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]); 2558 } 2559 numGlobalBcs = 0; 2560 for (p = pStart; p < pEnd; ++p) { 2561 const PetscInt *ind; 2562 PetscInt off, cdof, d; 2563 2564 PetscCall(PetscSectionGetOffset(s, p, &off)); 2565 PetscCall(PetscSectionGetConstraintDof(s, p, &cdof)); 2566 PetscCall(PetscSectionGetConstraintIndices(s, p, &ind)); 2567 for (d = 0; d < cdof; ++d) globalBcs[numGlobalBcs++] = off + ind[d]; 2568 } 2569 2570 PetscCall(PCPatchSetDiscretisationInfoCombined(pc, dm, Nb, (const PetscInt **) cellDofs, numGlobalBcs, globalBcs, numGlobalBcs, globalBcs)); 2571 for (f = 0; f < Nf; ++f) { 2572 PetscCall(PetscFree(cellDofs[f])); 2573 } 2574 PetscCall(PetscFree3(Nb, cellDofs, globalBcs)); 2575 PetscCall(PCPatchSetComputeFunction(pc, PCPatchComputeFunction_DMPlex_Private, NULL)); 2576 PetscCall(PCPatchSetComputeOperator(pc, PCPatchComputeOperator_DMPlex_Private, NULL)); 2577 PetscCall(DMDestroy(&dm)); 2578 } 2579 2580 localSize = patch->subspaceOffsets[patch->nsubspaces]; 2581 PetscCall(VecCreateSeq(PETSC_COMM_SELF, localSize, &patch->localRHS)); 2582 PetscCall(VecSetUp(patch->localRHS)); 2583 PetscCall(VecDuplicate(patch->localRHS, &patch->localUpdate)); 2584 PetscCall(PCPatchCreateCellPatches(pc)); 2585 PetscCall(PCPatchCreateCellPatchDiscretisationInfo(pc)); 2586 2587 /* OK, now build the work vectors */ 2588 PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, &pEnd)); 2589 2590 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 2591 PetscCall(PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithArtificial)); 2592 } 2593 if (isNonlinear) { 2594 PetscCall(PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithAll)); 2595 } 2596 for (p = pStart; p < pEnd; ++p) { 2597 PetscInt dof; 2598 2599 PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &dof)); 2600 maxDof = PetscMax(maxDof, dof); 2601 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 2602 const PetscInt *gtolArray, *gtolArrayWithArtificial = NULL; 2603 PetscInt numPatchDofs, offset; 2604 PetscInt numPatchDofsWithArtificial, offsetWithArtificial; 2605 PetscInt dofWithoutArtificialCounter = 0; 2606 PetscInt *patchWithoutArtificialToWithArtificialArray; 2607 2608 PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof)); 2609 maxDofWithArtificial = PetscMax(maxDofWithArtificial, dof); 2610 2611 /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */ 2612 /* the index in the patch with all dofs */ 2613 PetscCall(ISGetIndices(patch->gtol, >olArray)); 2614 2615 PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs)); 2616 if (numPatchDofs == 0) { 2617 patch->dofMappingWithoutToWithArtificial[p-pStart] = NULL; 2618 continue; 2619 } 2620 2621 PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset)); 2622 PetscCall(ISGetIndices(patch->gtolWithArtificial, >olArrayWithArtificial)); 2623 PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &numPatchDofsWithArtificial)); 2624 PetscCall(PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offsetWithArtificial)); 2625 2626 PetscCall(PetscMalloc1(numPatchDofs, &patchWithoutArtificialToWithArtificialArray)); 2627 for (i=0; i<numPatchDofsWithArtificial; i++) { 2628 if (gtolArrayWithArtificial[i+offsetWithArtificial] == gtolArray[offset+dofWithoutArtificialCounter]) { 2629 patchWithoutArtificialToWithArtificialArray[dofWithoutArtificialCounter] = i; 2630 dofWithoutArtificialCounter++; 2631 if (dofWithoutArtificialCounter == numPatchDofs) 2632 break; 2633 } 2634 } 2635 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutArtificialToWithArtificialArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithArtificial[p-pStart])); 2636 PetscCall(ISRestoreIndices(patch->gtol, >olArray)); 2637 PetscCall(ISRestoreIndices(patch->gtolWithArtificial, >olArrayWithArtificial)); 2638 } 2639 if (isNonlinear) { 2640 const PetscInt *gtolArray, *gtolArrayWithAll = NULL; 2641 PetscInt numPatchDofs, offset; 2642 PetscInt numPatchDofsWithAll, offsetWithAll; 2643 PetscInt dofWithoutAllCounter = 0; 2644 PetscInt *patchWithoutAllToWithAllArray; 2645 2646 /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */ 2647 /* the index in the patch with all dofs */ 2648 PetscCall(ISGetIndices(patch->gtol, >olArray)); 2649 2650 PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs)); 2651 if (numPatchDofs == 0) { 2652 patch->dofMappingWithoutToWithAll[p-pStart] = NULL; 2653 continue; 2654 } 2655 2656 PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset)); 2657 PetscCall(ISGetIndices(patch->gtolWithAll, >olArrayWithAll)); 2658 PetscCall(PetscSectionGetDof(patch->gtolCountsWithAll, p, &numPatchDofsWithAll)); 2659 PetscCall(PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offsetWithAll)); 2660 2661 PetscCall(PetscMalloc1(numPatchDofs, &patchWithoutAllToWithAllArray)); 2662 2663 for (i=0; i<numPatchDofsWithAll; i++) { 2664 if (gtolArrayWithAll[i+offsetWithAll] == gtolArray[offset+dofWithoutAllCounter]) { 2665 patchWithoutAllToWithAllArray[dofWithoutAllCounter] = i; 2666 dofWithoutAllCounter++; 2667 if (dofWithoutAllCounter == numPatchDofs) 2668 break; 2669 } 2670 } 2671 PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutAllToWithAllArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithAll[p-pStart])); 2672 PetscCall(ISRestoreIndices(patch->gtol, >olArray)); 2673 PetscCall(ISRestoreIndices(patch->gtolWithAll, >olArrayWithAll)); 2674 } 2675 } 2676 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 2677 PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDofWithArtificial, &patch->patchRHSWithArtificial)); 2678 PetscCall(VecSetUp(patch->patchRHSWithArtificial)); 2679 } 2680 PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDof, &patch->patchRHS)); 2681 PetscCall(VecSetUp(patch->patchRHS)); 2682 PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDof, &patch->patchUpdate)); 2683 PetscCall(VecSetUp(patch->patchUpdate)); 2684 if (patch->save_operators) { 2685 PetscCall(PetscMalloc1(patch->npatch, &patch->mat)); 2686 for (i = 0; i < patch->npatch; ++i) { 2687 PetscCall(PCPatchCreateMatrix_Private(pc, i, &patch->mat[i], PETSC_FALSE)); 2688 } 2689 } 2690 PetscCall(PetscLogEventEnd(PC_Patch_CreatePatches, pc, 0, 0, 0)); 2691 2692 /* If desired, calculate weights for dof multiplicity */ 2693 if (patch->partition_of_unity) { 2694 PetscScalar *input = NULL; 2695 PetscScalar *output = NULL; 2696 Vec global; 2697 2698 PetscCall(VecDuplicate(patch->localRHS, &patch->dof_weights)); 2699 if (patch->local_composition_type == PC_COMPOSITE_ADDITIVE) { 2700 for (i = 0; i < patch->npatch; ++i) { 2701 PetscInt dof; 2702 2703 PetscCall(PetscSectionGetDof(patch->gtolCounts, i+pStart, &dof)); 2704 if (dof <= 0) continue; 2705 PetscCall(VecSet(patch->patchRHS, 1.0)); 2706 PetscCall(PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchRHS, patch->dof_weights, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR)); 2707 } 2708 } else { 2709 /* multiplicative is actually only locally multiplicative and globally additive. need the pou where the mesh decomposition overlaps */ 2710 PetscCall(VecSet(patch->dof_weights, 1.0)); 2711 } 2712 2713 VecDuplicate(patch->dof_weights, &global); 2714 VecSet(global, 0.); 2715 2716 PetscCall(VecGetArray(patch->dof_weights, &input)); 2717 PetscCall(VecGetArray(global, &output)); 2718 PetscCall(PetscSFReduceBegin(patch->sectionSF, MPIU_SCALAR, input, output, MPI_SUM)); 2719 PetscCall(PetscSFReduceEnd(patch->sectionSF, MPIU_SCALAR, input, output, MPI_SUM)); 2720 PetscCall(VecRestoreArray(patch->dof_weights, &input)); 2721 PetscCall(VecRestoreArray(global, &output)); 2722 2723 PetscCall(VecReciprocal(global)); 2724 2725 PetscCall(VecGetArray(patch->dof_weights, &output)); 2726 PetscCall(VecGetArray(global, &input)); 2727 PetscCall(PetscSFBcastBegin(patch->sectionSF, MPIU_SCALAR, input, output,MPI_REPLACE)); 2728 PetscCall(PetscSFBcastEnd(patch->sectionSF, MPIU_SCALAR, input, output,MPI_REPLACE)); 2729 PetscCall(VecRestoreArray(patch->dof_weights, &output)); 2730 PetscCall(VecRestoreArray(global, &input)); 2731 PetscCall(VecDestroy(&global)); 2732 } 2733 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE && patch->save_operators) { 2734 PetscCall(PetscMalloc1(patch->npatch, &patch->matWithArtificial)); 2735 } 2736 } 2737 PetscCall((*patch->setupsolver)(pc)); 2738 PetscFunctionReturn(0); 2739 } 2740 2741 static PetscErrorCode PCApply_PATCH_Linear(PC pc, PetscInt i, Vec x, Vec y) 2742 { 2743 PC_PATCH *patch = (PC_PATCH *) pc->data; 2744 KSP ksp; 2745 Mat op; 2746 PetscInt m, n; 2747 2748 PetscFunctionBegin; 2749 if (patch->denseinverse) { 2750 PetscCall((*patch->densesolve)(patch->mat[i], x, y)); 2751 PetscFunctionReturn(0); 2752 } 2753 ksp = (KSP) patch->solver[i]; 2754 if (!patch->save_operators) { 2755 Mat mat; 2756 2757 PetscCall(PCPatchCreateMatrix_Private(pc, i, &mat, PETSC_FALSE)); 2758 /* Populate operator here. */ 2759 PetscCall(PCPatchComputeOperator_Internal(pc, NULL, mat, i, PETSC_FALSE)); 2760 PetscCall(KSPSetOperators(ksp, mat, mat)); 2761 /* Drop reference so the KSPSetOperators below will blow it away. */ 2762 PetscCall(MatDestroy(&mat)); 2763 } 2764 PetscCall(PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0)); 2765 if (!ksp->setfromoptionscalled) { 2766 PetscCall(KSPSetFromOptions(ksp)); 2767 } 2768 /* Disgusting trick to reuse work vectors */ 2769 PetscCall(KSPGetOperators(ksp, &op, NULL)); 2770 PetscCall(MatGetLocalSize(op, &m, &n)); 2771 x->map->n = m; 2772 y->map->n = n; 2773 x->map->N = m; 2774 y->map->N = n; 2775 PetscCall(KSPSolve(ksp, x, y)); 2776 PetscCall(KSPCheckSolve(ksp, pc, y)); 2777 PetscCall(PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0)); 2778 if (!patch->save_operators) { 2779 PC pc; 2780 PetscCall(KSPSetOperators(ksp, NULL, NULL)); 2781 PetscCall(KSPGetPC(ksp, &pc)); 2782 /* Destroy PC context too, otherwise the factored matrix hangs around. */ 2783 PetscCall(PCReset(pc)); 2784 } 2785 PetscFunctionReturn(0); 2786 } 2787 2788 static PetscErrorCode PCUpdateMultiplicative_PATCH_Linear(PC pc, PetscInt i, PetscInt pStart) 2789 { 2790 PC_PATCH *patch = (PC_PATCH *) pc->data; 2791 Mat multMat; 2792 PetscInt n, m; 2793 2794 PetscFunctionBegin; 2795 2796 if (patch->save_operators) { 2797 multMat = patch->matWithArtificial[i]; 2798 } else { 2799 /*Very inefficient, hopefully we can just assemble the rectangular matrix in the first place.*/ 2800 Mat matSquare; 2801 PetscInt dof; 2802 IS rowis; 2803 PetscCall(PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE)); 2804 PetscCall(PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE)); 2805 PetscCall(MatGetSize(matSquare, &dof, NULL)); 2806 PetscCall(ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis)); 2807 PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &multMat)); 2808 PetscCall(MatDestroy(&matSquare)); 2809 PetscCall(ISDestroy(&rowis)); 2810 } 2811 /* Disgusting trick to reuse work vectors */ 2812 PetscCall(MatGetLocalSize(multMat, &m, &n)); 2813 patch->patchUpdate->map->n = n; 2814 patch->patchRHSWithArtificial->map->n = m; 2815 patch->patchUpdate->map->N = n; 2816 patch->patchRHSWithArtificial->map->N = m; 2817 PetscCall(MatMult(multMat, patch->patchUpdate, patch->patchRHSWithArtificial)); 2818 PetscCall(VecScale(patch->patchRHSWithArtificial, -1.0)); 2819 PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHSWithArtificial, patch->localRHS, ADD_VALUES, SCATTER_REVERSE, SCATTER_WITHARTIFICIAL)); 2820 if (!patch->save_operators) { 2821 PetscCall(MatDestroy(&multMat)); 2822 } 2823 PetscFunctionReturn(0); 2824 } 2825 2826 static PetscErrorCode PCApply_PATCH(PC pc, Vec x, Vec y) 2827 { 2828 PC_PATCH *patch = (PC_PATCH *) pc->data; 2829 const PetscScalar *globalRHS = NULL; 2830 PetscScalar *localRHS = NULL; 2831 PetscScalar *globalUpdate = NULL; 2832 const PetscInt *bcNodes = NULL; 2833 PetscInt nsweep = patch->symmetrise_sweep ? 2 : 1; 2834 PetscInt start[2] = {0, 0}; 2835 PetscInt end[2] = {-1, -1}; 2836 const PetscInt inc[2] = {1, -1}; 2837 const PetscScalar *localUpdate; 2838 const PetscInt *iterationSet; 2839 PetscInt pStart, numBcs, n, sweep, bc, j; 2840 2841 PetscFunctionBegin; 2842 PetscCall(PetscLogEventBegin(PC_Patch_Apply, pc, 0, 0, 0)); 2843 PetscCall(PetscOptionsPushGetViewerOff(PETSC_TRUE)); 2844 /* start, end, inc have 2 entries to manage a second backward sweep if we symmetrize */ 2845 end[0] = patch->npatch; 2846 start[1] = patch->npatch-1; 2847 if (patch->user_patches) { 2848 PetscCall(ISGetLocalSize(patch->iterationSet, &end[0])); 2849 start[1] = end[0] - 1; 2850 PetscCall(ISGetIndices(patch->iterationSet, &iterationSet)); 2851 } 2852 /* Scatter from global space into overlapped local spaces */ 2853 PetscCall(VecGetArrayRead(x, &globalRHS)); 2854 PetscCall(VecGetArray(patch->localRHS, &localRHS)); 2855 PetscCall(PetscSFBcastBegin(patch->sectionSF, MPIU_SCALAR, globalRHS, localRHS,MPI_REPLACE)); 2856 PetscCall(PetscSFBcastEnd(patch->sectionSF, MPIU_SCALAR, globalRHS, localRHS,MPI_REPLACE)); 2857 PetscCall(VecRestoreArrayRead(x, &globalRHS)); 2858 PetscCall(VecRestoreArray(patch->localRHS, &localRHS)); 2859 2860 PetscCall(VecSet(patch->localUpdate, 0.0)); 2861 PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, NULL)); 2862 PetscCall(PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0)); 2863 for (sweep = 0; sweep < nsweep; sweep++) { 2864 for (j = start[sweep]; j*inc[sweep] < end[sweep]*inc[sweep]; j += inc[sweep]) { 2865 PetscInt i = patch->user_patches ? iterationSet[j] : j; 2866 PetscInt start, len; 2867 2868 PetscCall(PetscSectionGetDof(patch->gtolCounts, i+pStart, &len)); 2869 PetscCall(PetscSectionGetOffset(patch->gtolCounts, i+pStart, &start)); 2870 /* TODO: Squash out these guys in the setup as well. */ 2871 if (len <= 0) continue; 2872 /* TODO: Do we need different scatters for X and Y? */ 2873 PetscCall(PCPatch_ScatterLocal_Private(pc, i+pStart, patch->localRHS, patch->patchRHS, INSERT_VALUES, SCATTER_FORWARD, SCATTER_INTERIOR)); 2874 PetscCall((*patch->applysolver)(pc, i, patch->patchRHS, patch->patchUpdate)); 2875 PetscCall(PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchUpdate, patch->localUpdate, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR)); 2876 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 2877 PetscCall((*patch->updatemultiplicative)(pc, i, pStart)); 2878 } 2879 } 2880 } 2881 PetscCall(PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0)); 2882 if (patch->user_patches) PetscCall(ISRestoreIndices(patch->iterationSet, &iterationSet)); 2883 /* XXX: should we do this on the global vector? */ 2884 if (patch->partition_of_unity) PetscCall(VecPointwiseMult(patch->localUpdate, patch->localUpdate, patch->dof_weights)); 2885 /* Now patch->localUpdate contains the solution of the patch solves, so we need to combine them all. */ 2886 PetscCall(VecSet(y, 0.0)); 2887 PetscCall(VecGetArray(y, &globalUpdate)); 2888 PetscCall(VecGetArrayRead(patch->localUpdate, &localUpdate)); 2889 PetscCall(PetscSFReduceBegin(patch->sectionSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM)); 2890 PetscCall(PetscSFReduceEnd(patch->sectionSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM)); 2891 PetscCall(VecRestoreArrayRead(patch->localUpdate, &localUpdate)); 2892 2893 /* Now we need to send the global BC values through */ 2894 PetscCall(VecGetArrayRead(x, &globalRHS)); 2895 PetscCall(ISGetSize(patch->globalBcNodes, &numBcs)); 2896 PetscCall(ISGetIndices(patch->globalBcNodes, &bcNodes)); 2897 PetscCall(VecGetLocalSize(x, &n)); 2898 for (bc = 0; bc < numBcs; ++bc) { 2899 const PetscInt idx = bcNodes[bc]; 2900 if (idx < n) globalUpdate[idx] = globalRHS[idx]; 2901 } 2902 2903 PetscCall(ISRestoreIndices(patch->globalBcNodes, &bcNodes)); 2904 PetscCall(VecRestoreArrayRead(x, &globalRHS)); 2905 PetscCall(VecRestoreArray(y, &globalUpdate)); 2906 2907 PetscCall(PetscOptionsPopGetViewerOff()); 2908 PetscCall(PetscLogEventEnd(PC_Patch_Apply, pc, 0, 0, 0)); 2909 PetscFunctionReturn(0); 2910 } 2911 2912 static PetscErrorCode PCReset_PATCH_Linear(PC pc) 2913 { 2914 PC_PATCH *patch = (PC_PATCH *) pc->data; 2915 PetscInt i; 2916 2917 PetscFunctionBegin; 2918 if (patch->solver) { 2919 for (i = 0; i < patch->npatch; ++i) PetscCall(KSPReset((KSP) patch->solver[i])); 2920 } 2921 PetscFunctionReturn(0); 2922 } 2923 2924 static PetscErrorCode PCReset_PATCH(PC pc) 2925 { 2926 PC_PATCH *patch = (PC_PATCH *) pc->data; 2927 PetscInt i; 2928 2929 PetscFunctionBegin; 2930 2931 PetscCall(PetscSFDestroy(&patch->sectionSF)); 2932 PetscCall(PetscSectionDestroy(&patch->cellCounts)); 2933 PetscCall(PetscSectionDestroy(&patch->pointCounts)); 2934 PetscCall(PetscSectionDestroy(&patch->cellNumbering)); 2935 PetscCall(PetscSectionDestroy(&patch->gtolCounts)); 2936 PetscCall(ISDestroy(&patch->gtol)); 2937 PetscCall(ISDestroy(&patch->cells)); 2938 PetscCall(ISDestroy(&patch->points)); 2939 PetscCall(ISDestroy(&patch->dofs)); 2940 PetscCall(ISDestroy(&patch->offs)); 2941 PetscCall(PetscSectionDestroy(&patch->patchSection)); 2942 PetscCall(ISDestroy(&patch->ghostBcNodes)); 2943 PetscCall(ISDestroy(&patch->globalBcNodes)); 2944 PetscCall(PetscSectionDestroy(&patch->gtolCountsWithArtificial)); 2945 PetscCall(ISDestroy(&patch->gtolWithArtificial)); 2946 PetscCall(ISDestroy(&patch->dofsWithArtificial)); 2947 PetscCall(ISDestroy(&patch->offsWithArtificial)); 2948 PetscCall(PetscSectionDestroy(&patch->gtolCountsWithAll)); 2949 PetscCall(ISDestroy(&patch->gtolWithAll)); 2950 PetscCall(ISDestroy(&patch->dofsWithAll)); 2951 PetscCall(ISDestroy(&patch->offsWithAll)); 2952 PetscCall(VecDestroy(&patch->cellMats)); 2953 PetscCall(VecDestroy(&patch->intFacetMats)); 2954 PetscCall(ISDestroy(&patch->allCells)); 2955 PetscCall(ISDestroy(&patch->intFacets)); 2956 PetscCall(ISDestroy(&patch->extFacets)); 2957 PetscCall(ISDestroy(&patch->intFacetsToPatchCell)); 2958 PetscCall(PetscSectionDestroy(&patch->intFacetCounts)); 2959 PetscCall(PetscSectionDestroy(&patch->extFacetCounts)); 2960 2961 if (patch->dofSection) for (i = 0; i < patch->nsubspaces; i++) PetscCall(PetscSectionDestroy(&patch->dofSection[i])); 2962 PetscCall(PetscFree(patch->dofSection)); 2963 PetscCall(PetscFree(patch->bs)); 2964 PetscCall(PetscFree(patch->nodesPerCell)); 2965 if (patch->cellNodeMap) for (i = 0; i < patch->nsubspaces; i++) PetscCall(PetscFree(patch->cellNodeMap[i])); 2966 PetscCall(PetscFree(patch->cellNodeMap)); 2967 PetscCall(PetscFree(patch->subspaceOffsets)); 2968 2969 PetscCall((*patch->resetsolver)(pc)); 2970 2971 if (patch->subspaces_to_exclude) { 2972 PetscCall(PetscHSetIDestroy(&patch->subspaces_to_exclude)); 2973 } 2974 2975 PetscCall(VecDestroy(&patch->localRHS)); 2976 PetscCall(VecDestroy(&patch->localUpdate)); 2977 PetscCall(VecDestroy(&patch->patchRHS)); 2978 PetscCall(VecDestroy(&patch->patchUpdate)); 2979 PetscCall(VecDestroy(&patch->dof_weights)); 2980 if (patch->patch_dof_weights) { 2981 for (i = 0; i < patch->npatch; ++i) PetscCall(VecDestroy(&patch->patch_dof_weights[i])); 2982 PetscCall(PetscFree(patch->patch_dof_weights)); 2983 } 2984 if (patch->mat) { 2985 for (i = 0; i < patch->npatch; ++i) PetscCall(MatDestroy(&patch->mat[i])); 2986 PetscCall(PetscFree(patch->mat)); 2987 } 2988 if (patch->matWithArtificial) { 2989 for (i = 0; i < patch->npatch; ++i) PetscCall(MatDestroy(&patch->matWithArtificial[i])); 2990 PetscCall(PetscFree(patch->matWithArtificial)); 2991 } 2992 PetscCall(VecDestroy(&patch->patchRHSWithArtificial)); 2993 if (patch->dofMappingWithoutToWithArtificial) { 2994 for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->dofMappingWithoutToWithArtificial[i])); 2995 PetscCall(PetscFree(patch->dofMappingWithoutToWithArtificial)); 2996 2997 } 2998 if (patch->dofMappingWithoutToWithAll) { 2999 for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->dofMappingWithoutToWithAll[i])); 3000 PetscCall(PetscFree(patch->dofMappingWithoutToWithAll)); 3001 3002 } 3003 PetscCall(PetscFree(patch->sub_mat_type)); 3004 if (patch->userIS) { 3005 for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->userIS[i])); 3006 PetscCall(PetscFree(patch->userIS)); 3007 } 3008 PetscCall(PetscFree(patch->precomputedTensorLocations)); 3009 PetscCall(PetscFree(patch->precomputedIntFacetTensorLocations)); 3010 3011 patch->bs = NULL; 3012 patch->cellNodeMap = NULL; 3013 patch->nsubspaces = 0; 3014 PetscCall(ISDestroy(&patch->iterationSet)); 3015 3016 PetscCall(PetscViewerDestroy(&patch->viewerSection)); 3017 PetscFunctionReturn(0); 3018 } 3019 3020 static PetscErrorCode PCDestroy_PATCH_Linear(PC pc) 3021 { 3022 PC_PATCH *patch = (PC_PATCH *) pc->data; 3023 PetscInt i; 3024 3025 PetscFunctionBegin; 3026 if (patch->solver) { 3027 for (i = 0; i < patch->npatch; ++i) PetscCall(KSPDestroy((KSP *) &patch->solver[i])); 3028 PetscCall(PetscFree(patch->solver)); 3029 } 3030 PetscFunctionReturn(0); 3031 } 3032 3033 static PetscErrorCode PCDestroy_PATCH(PC pc) 3034 { 3035 PC_PATCH *patch = (PC_PATCH *) pc->data; 3036 3037 PetscFunctionBegin; 3038 PetscCall(PCReset_PATCH(pc)); 3039 PetscCall((*patch->destroysolver)(pc)); 3040 PetscCall(PetscFree(pc->data)); 3041 PetscFunctionReturn(0); 3042 } 3043 3044 static PetscErrorCode PCSetFromOptions_PATCH(PetscOptionItems *PetscOptionsObject, PC pc) 3045 { 3046 PC_PATCH *patch = (PC_PATCH *) pc->data; 3047 PCPatchConstructType patchConstructionType = PC_PATCH_STAR; 3048 char sub_mat_type[PETSC_MAX_PATH_LEN]; 3049 char option[PETSC_MAX_PATH_LEN]; 3050 const char *prefix; 3051 PetscBool flg, dimflg, codimflg; 3052 MPI_Comm comm; 3053 PetscInt *ifields, nfields, k; 3054 PCCompositeType loctype = PC_COMPOSITE_ADDITIVE; 3055 3056 PetscFunctionBegin; 3057 PetscCall(PetscObjectGetComm((PetscObject) pc, &comm)); 3058 PetscCall(PetscObjectGetOptionsPrefix((PetscObject) pc, &prefix)); 3059 PetscOptionsHeadBegin(PetscOptionsObject, "Patch solver options"); 3060 3061 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_save_operators", patch->classname)); 3062 PetscCall(PetscOptionsBool(option, "Store all patch operators for lifetime of object?", "PCPatchSetSaveOperators", patch->save_operators, &patch->save_operators, &flg)); 3063 3064 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_precompute_element_tensors", patch->classname)); 3065 PetscCall(PetscOptionsBool(option, "Compute each element tensor only once?", "PCPatchSetPrecomputeElementTensors", patch->precomputeElementTensors, &patch->precomputeElementTensors, &flg)); 3066 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_partition_of_unity", patch->classname)); 3067 PetscCall(PetscOptionsBool(option, "Weight contributions by dof multiplicity?", "PCPatchSetPartitionOfUnity", patch->partition_of_unity, &patch->partition_of_unity, &flg)); 3068 3069 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_local_type", patch->classname)); 3070 PetscCall(PetscOptionsEnum(option,"Type of local solver composition (additive or multiplicative)","PCPatchSetLocalComposition",PCCompositeTypes,(PetscEnum)loctype,(PetscEnum*)&loctype,&flg)); 3071 if (flg) PetscCall(PCPatchSetLocalComposition(pc, loctype)); 3072 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_dense_inverse", patch->classname)); 3073 PetscCall(PetscOptionsBool(option, "Compute inverses of patch matrices and apply directly? Ignores KSP/PC settings on patch.", "PCPatchSetDenseInverse", patch->denseinverse, &patch->denseinverse, &flg)); 3074 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_dim", patch->classname)); 3075 PetscCall(PetscOptionsInt(option, "What dimension of mesh point to construct patches by? (0 = vertices)", "PCPATCH", patch->dim, &patch->dim, &dimflg)); 3076 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_codim", patch->classname)); 3077 PetscCall(PetscOptionsInt(option, "What co-dimension of mesh point to construct patches by? (0 = cells)", "PCPATCH", patch->codim, &patch->codim, &codimflg)); 3078 PetscCheck(!dimflg || !codimflg,comm, PETSC_ERR_ARG_WRONG, "Can only set one of dimension or co-dimension"); 3079 3080 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_type", patch->classname)); 3081 PetscCall(PetscOptionsEnum(option, "How should the patches be constructed?", "PCPatchSetConstructType", PCPatchConstructTypes, (PetscEnum) patchConstructionType, (PetscEnum *) &patchConstructionType, &flg)); 3082 if (flg) PetscCall(PCPatchSetConstructType(pc, patchConstructionType, NULL, NULL)); 3083 3084 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_vanka_dim", patch->classname)); 3085 PetscCall(PetscOptionsInt(option, "Topological dimension of entities for Vanka to ignore", "PCPATCH", patch->vankadim, &patch->vankadim, &flg)); 3086 3087 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_ignore_dim", patch->classname)); 3088 PetscCall(PetscOptionsInt(option, "Topological dimension of entities for completion to ignore", "PCPATCH", patch->ignoredim, &patch->ignoredim, &flg)); 3089 3090 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_pardecomp_overlap", patch->classname)); 3091 PetscCall(PetscOptionsInt(option, "What overlap should we use in construct type pardecomp?", "PCPATCH", patch->pardecomp_overlap, &patch->pardecomp_overlap, &flg)); 3092 3093 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_sub_mat_type", patch->classname)); 3094 PetscCall(PetscOptionsFList(option, "Matrix type for patch solves", "PCPatchSetSubMatType", MatList, NULL, sub_mat_type, PETSC_MAX_PATH_LEN, &flg)); 3095 if (flg) PetscCall(PCPatchSetSubMatType(pc, sub_mat_type)); 3096 3097 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_symmetrise_sweep", patch->classname)); 3098 PetscCall(PetscOptionsBool(option, "Go start->end, end->start?", "PCPATCH", patch->symmetrise_sweep, &patch->symmetrise_sweep, &flg)); 3099 3100 /* If the user has set the number of subspaces, use that for the buffer size, 3101 otherwise use a large number */ 3102 if (patch->nsubspaces <= 0) { 3103 nfields = 128; 3104 } else { 3105 nfields = patch->nsubspaces; 3106 } 3107 PetscCall(PetscMalloc1(nfields, &ifields)); 3108 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exclude_subspaces", patch->classname)); 3109 PetscCall(PetscOptionsGetIntArray(((PetscObject)pc)->options,((PetscObject)pc)->prefix,option,ifields,&nfields,&flg)); 3110 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"); 3111 if (flg) { 3112 PetscCall(PetscHSetIClear(patch->subspaces_to_exclude)); 3113 for (k = 0; k < nfields; k++) { 3114 PetscCall(PetscHSetIAdd(patch->subspaces_to_exclude, ifields[k])); 3115 } 3116 } 3117 PetscCall(PetscFree(ifields)); 3118 3119 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_patches_view", patch->classname)); 3120 PetscCall(PetscOptionsBool(option, "Print out information during patch construction", "PCPATCH", patch->viewPatches, &patch->viewPatches, &flg)); 3121 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_cells_view", patch->classname)); 3122 PetscCall(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerCells, &patch->formatCells, &patch->viewCells)); 3123 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_interior_facets_view", patch->classname)); 3124 PetscCall(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerIntFacets, &patch->formatIntFacets, &patch->viewIntFacets)); 3125 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exterior_facets_view", patch->classname)); 3126 PetscCall(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerExtFacets, &patch->formatExtFacets, &patch->viewExtFacets)); 3127 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_points_view", patch->classname)); 3128 PetscCall(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerPoints, &patch->formatPoints, &patch->viewPoints)); 3129 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_section_view", patch->classname)); 3130 PetscCall(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerSection, &patch->formatSection, &patch->viewSection)); 3131 PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_mat_view", patch->classname)); 3132 PetscCall(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerMatrix, &patch->formatMatrix, &patch->viewMatrix)); 3133 PetscOptionsHeadEnd(); 3134 patch->optionsSet = PETSC_TRUE; 3135 PetscFunctionReturn(0); 3136 } 3137 3138 static PetscErrorCode PCSetUpOnBlocks_PATCH(PC pc) 3139 { 3140 PC_PATCH *patch = (PC_PATCH*) pc->data; 3141 KSPConvergedReason reason; 3142 PetscInt i; 3143 3144 PetscFunctionBegin; 3145 if (!patch->save_operators) { 3146 /* Can't do this here because the sub KSPs don't have an operator attached yet. */ 3147 PetscFunctionReturn(0); 3148 } 3149 if (patch->denseinverse) { 3150 /* No solvers */ 3151 PetscFunctionReturn(0); 3152 } 3153 for (i = 0; i < patch->npatch; ++i) { 3154 if (!((KSP) patch->solver[i])->setfromoptionscalled) { 3155 PetscCall(KSPSetFromOptions((KSP) patch->solver[i])); 3156 } 3157 PetscCall(KSPSetUp((KSP) patch->solver[i])); 3158 PetscCall(KSPGetConvergedReason((KSP) patch->solver[i], &reason)); 3159 if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR; 3160 } 3161 PetscFunctionReturn(0); 3162 } 3163 3164 static PetscErrorCode PCView_PATCH(PC pc, PetscViewer viewer) 3165 { 3166 PC_PATCH *patch = (PC_PATCH *) pc->data; 3167 PetscViewer sviewer; 3168 PetscBool isascii; 3169 PetscMPIInt rank; 3170 3171 PetscFunctionBegin; 3172 /* TODO Redo tabbing with set tbas in new style */ 3173 PetscCall(PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii)); 3174 if (!isascii) PetscFunctionReturn(0); 3175 PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) pc), &rank)); 3176 PetscCall(PetscViewerASCIIPushTab(viewer)); 3177 PetscCall(PetscViewerASCIIPrintf(viewer, "Subspace Correction preconditioner with %" PetscInt_FMT " patches\n", patch->npatch)); 3178 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 3179 PetscCall(PetscViewerASCIIPrintf(viewer, "Schwarz type: multiplicative\n")); 3180 } else { 3181 PetscCall(PetscViewerASCIIPrintf(viewer, "Schwarz type: additive\n")); 3182 } 3183 if (patch->partition_of_unity) PetscCall(PetscViewerASCIIPrintf(viewer, "Weighting by partition of unity\n")); 3184 else PetscCall(PetscViewerASCIIPrintf(viewer, "Not weighting by partition of unity\n")); 3185 if (patch->symmetrise_sweep) PetscCall(PetscViewerASCIIPrintf(viewer, "Symmetrising sweep (start->end, then end->start)\n")); 3186 else PetscCall(PetscViewerASCIIPrintf(viewer, "Not symmetrising sweep\n")); 3187 if (!patch->precomputeElementTensors) PetscCall(PetscViewerASCIIPrintf(viewer, "Not precomputing element tensors (overlapping cells rebuilt in every patch assembly)\n")); 3188 else PetscCall(PetscViewerASCIIPrintf(viewer, "Precomputing element tensors (each cell assembled only once)\n")); 3189 if (!patch->save_operators) PetscCall(PetscViewerASCIIPrintf(viewer, "Not saving patch operators (rebuilt every PCApply)\n")); 3190 else PetscCall(PetscViewerASCIIPrintf(viewer, "Saving patch operators (rebuilt every PCSetUp)\n")); 3191 if (patch->patchconstructop == PCPatchConstruct_Star) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: star\n")); 3192 else if (patch->patchconstructop == PCPatchConstruct_Vanka) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: Vanka\n")); 3193 else if (patch->patchconstructop == PCPatchConstruct_User) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: user-specified\n")); 3194 else PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: unknown\n")); 3195 3196 if (patch->denseinverse) { 3197 PetscCall(PetscViewerASCIIPrintf(viewer, "Explicitly forming dense inverse and applying patch solver via MatMult.\n")); 3198 } else { 3199 if (patch->isNonlinear) { 3200 PetscCall(PetscViewerASCIIPrintf(viewer, "SNES on patches (all same):\n")); 3201 } else { 3202 PetscCall(PetscViewerASCIIPrintf(viewer, "KSP on patches (all same):\n")); 3203 } 3204 if (patch->solver) { 3205 PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer)); 3206 if (rank == 0) { 3207 PetscCall(PetscViewerASCIIPushTab(sviewer)); 3208 PetscCall(PetscObjectView(patch->solver[0], sviewer)); 3209 PetscCall(PetscViewerASCIIPopTab(sviewer)); 3210 } 3211 PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer)); 3212 } else { 3213 PetscCall(PetscViewerASCIIPushTab(viewer)); 3214 PetscCall(PetscViewerASCIIPrintf(viewer, "Solver not yet set.\n")); 3215 PetscCall(PetscViewerASCIIPopTab(viewer)); 3216 } 3217 } 3218 PetscCall(PetscViewerASCIIPopTab(viewer)); 3219 PetscFunctionReturn(0); 3220 } 3221 3222 /*MC 3223 PCPATCH - A PC object that encapsulates flexible definition of blocks for overlapping and non-overlapping 3224 small block additive preconditioners. Block definition is based on topology from 3225 a DM and equation numbering from a PetscSection. 3226 3227 Options Database Keys: 3228 + -pc_patch_cells_view - Views the process local cell numbers for each patch 3229 . -pc_patch_points_view - Views the process local mesh point numbers for each patch 3230 . -pc_patch_g2l_view - Views the map between global dofs and patch local dofs for each patch 3231 . -pc_patch_patches_view - Views the global dofs associated with each patch and its boundary 3232 - -pc_patch_sub_mat_view - Views the matrix associated with each patch 3233 3234 Level: intermediate 3235 3236 .seealso: `PCType`, `PCCreate()`, `PCSetType()` 3237 M*/ 3238 PETSC_EXTERN PetscErrorCode PCCreate_Patch(PC pc) 3239 { 3240 PC_PATCH *patch; 3241 3242 PetscFunctionBegin; 3243 PetscCall(PetscNewLog(pc, &patch)); 3244 3245 if (patch->subspaces_to_exclude) { 3246 PetscCall(PetscHSetIDestroy(&patch->subspaces_to_exclude)); 3247 } 3248 PetscCall(PetscHSetICreate(&patch->subspaces_to_exclude)); 3249 3250 patch->classname = "pc"; 3251 patch->isNonlinear = PETSC_FALSE; 3252 3253 /* Set some defaults */ 3254 patch->combined = PETSC_FALSE; 3255 patch->save_operators = PETSC_TRUE; 3256 patch->local_composition_type = PC_COMPOSITE_ADDITIVE; 3257 patch->precomputeElementTensors = PETSC_FALSE; 3258 patch->partition_of_unity = PETSC_FALSE; 3259 patch->codim = -1; 3260 patch->dim = -1; 3261 patch->vankadim = -1; 3262 patch->ignoredim = -1; 3263 patch->pardecomp_overlap = 0; 3264 patch->patchconstructop = PCPatchConstruct_Star; 3265 patch->symmetrise_sweep = PETSC_FALSE; 3266 patch->npatch = 0; 3267 patch->userIS = NULL; 3268 patch->optionsSet = PETSC_FALSE; 3269 patch->iterationSet = NULL; 3270 patch->user_patches = PETSC_FALSE; 3271 PetscCall(PetscStrallocpy(MATDENSE, (char **) &patch->sub_mat_type)); 3272 patch->viewPatches = PETSC_FALSE; 3273 patch->viewCells = PETSC_FALSE; 3274 patch->viewPoints = PETSC_FALSE; 3275 patch->viewSection = PETSC_FALSE; 3276 patch->viewMatrix = PETSC_FALSE; 3277 patch->densesolve = NULL; 3278 patch->setupsolver = PCSetUp_PATCH_Linear; 3279 patch->applysolver = PCApply_PATCH_Linear; 3280 patch->resetsolver = PCReset_PATCH_Linear; 3281 patch->destroysolver = PCDestroy_PATCH_Linear; 3282 patch->updatemultiplicative = PCUpdateMultiplicative_PATCH_Linear; 3283 patch->dofMappingWithoutToWithArtificial = NULL; 3284 patch->dofMappingWithoutToWithAll = NULL; 3285 3286 pc->data = (void *) patch; 3287 pc->ops->apply = PCApply_PATCH; 3288 pc->ops->applytranspose = NULL; /* PCApplyTranspose_PATCH; */ 3289 pc->ops->setup = PCSetUp_PATCH; 3290 pc->ops->reset = PCReset_PATCH; 3291 pc->ops->destroy = PCDestroy_PATCH; 3292 pc->ops->setfromoptions = PCSetFromOptions_PATCH; 3293 pc->ops->setuponblocks = PCSetUpOnBlocks_PATCH; 3294 pc->ops->view = PCView_PATCH; 3295 pc->ops->applyrichardson = NULL; 3296 3297 PetscFunctionReturn(0); 3298 } 3299