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 CHKERRQ(PetscViewerPushFormat(viewer, format)); 16 CHKERRQ(PetscObjectView(obj, viewer)); 17 CHKERRQ(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 CHKERRQ(PetscHSetIClear(ht)); 28 /* To start with, add the point we care about */ 29 CHKERRQ(PetscHSetIAdd(ht, point)); 30 /* Loop over all the points that this point connects to */ 31 CHKERRQ(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star)); 32 for (si = 0; si < starSize*2; si += 2) CHKERRQ(PetscHSetIAdd(ht, star[si])); 33 CHKERRQ(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 CHKERRQ(PetscHSetIClear(ht)); 47 /* To start with, add the point we care about */ 48 CHKERRQ(PetscHSetIAdd(ht, point)); 49 /* Should we ignore any points of a certain dimension? */ 50 if (patch->vankadim >= 0) { 51 shouldIgnore = PETSC_TRUE; 52 CHKERRQ(DMPlexGetDepthStratum(dm, patch->vankadim, &iStart, &iEnd)); 53 } 54 CHKERRQ(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 55 /* Loop over all the cells that this point connects to */ 56 CHKERRQ(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 CHKERRQ(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 CHKERRQ(PetscHSetIAdd(ht, newpoint)); 71 } 72 CHKERRQ(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure)); 73 } 74 CHKERRQ(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 CHKERRQ(PetscHSetIClear(ht)); 92 93 CHKERRQ(DMPlexGetChart(dm, &pStart, &pEnd)); 94 95 CHKERRQ(DMHasLabel(dm, "pyop2_ghost", &isFiredrake)); 96 if (isFiredrake) { 97 CHKERRQ(DMGetLabel(dm, "pyop2_ghost", &ghost)); 98 CHKERRQ(DMLabelCreateIndex(ghost, pStart, pEnd)); 99 } else { 100 PetscSF sf; 101 CHKERRQ(DMGetPointSF(dm, &sf)); 102 CHKERRQ(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL)); 103 nleaves = PetscMax(nleaves, 0); 104 } 105 106 for (opoint = pStart; opoint < pEnd; ++opoint) { 107 if (ghost) CHKERRQ(DMLabelHasPoint(ghost, opoint, &flg)); 108 else {CHKERRQ(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 CHKERRQ(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 CHKERRQ(PetscHSetIGetSize(ht, &htsize)); 122 CHKERRQ(PetscMalloc1(htsize, &htpoints)); 123 CHKERRQ(PetscHSetIGetElems(ht, &index, htpoints)); 124 125 for (i = 0; i < htsize; ++i) { 126 PetscInt hpoint = htpoints[i]; 127 PetscInt si; 128 129 CHKERRQ(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 CHKERRQ(DMPlexGetTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure)); 137 for (ci = 0; ci < closureSize*2; ci += 2) { 138 const PetscInt closstarp = closure[ci]; 139 CHKERRQ(PetscHSetIAdd(ht, closstarp)); 140 } 141 CHKERRQ(DMPlexRestoreTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure)); 142 } 143 CHKERRQ(DMPlexRestoreTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star)); 144 } 145 CHKERRQ(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 CHKERRQ(PetscHSetIClear(ht)); 162 CHKERRQ(DMPlexGetChart(dm, &pStart, &pEnd)); 163 CHKERRQ(ISGetLocalSize(patchis, &n)); 164 CHKERRQ(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 %D was not in [%D, %D)", ownedpoint, pStart, pEnd); 170 } 171 CHKERRQ(PetscHSetIAdd(ht, ownedpoint)); 172 } 173 CHKERRQ(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 CHKERRQ(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 CHKERRQ(PetscSFGetGraph(sf[i], &nroots, &nleaves, NULL, NULL)); 210 allRoots += nroots * bs[i]; 211 allLeaves += nleaves * bs[i]; 212 } 213 CHKERRQ(PetscMalloc1(allLeaves, &ilocal)); 214 CHKERRQ(PetscMalloc1(allLeaves, &iremote)); 215 /* Now build an SF that just contains process connectivity. */ 216 CHKERRQ(PetscHSetICreate(&ranksUniq)); 217 for (i = 0; i < n; ++i) { 218 const PetscMPIInt *ranks = NULL; 219 PetscInt nranks, j; 220 221 CHKERRQ(PetscSFSetUp(sf[i])); 222 CHKERRQ(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 CHKERRQ(PetscHSetIAdd(ranksUniq, (PetscInt) ranks[j])); 226 } 227 } 228 CHKERRQ(PetscHSetIGetSize(ranksUniq, &numRanks)); 229 CHKERRQ(PetscMalloc1(numRanks, &remote)); 230 CHKERRQ(PetscMalloc1(numRanks, &ranks)); 231 CHKERRQ(PetscHSetIGetElems(ranksUniq, &index, ranks)); 232 233 CHKERRQ(PetscHMapICreate(&rankToIndex)); 234 for (i = 0; i < numRanks; ++i) { 235 remote[i].rank = ranks[i]; 236 remote[i].index = 0; 237 CHKERRQ(PetscHMapISet(rankToIndex, ranks[i], i)); 238 } 239 CHKERRQ(PetscFree(ranks)); 240 CHKERRQ(PetscHSetIDestroy(&ranksUniq)); 241 CHKERRQ(PetscSFCreate(PetscObjectComm((PetscObject) pc), &rankSF)); 242 CHKERRQ(PetscSFSetGraph(rankSF, 1, numRanks, NULL, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER)); 243 CHKERRQ(PetscSFSetUp(rankSF)); 244 /* OK, use it to communicate the root offset on the remote 245 * processes for each subspace. */ 246 CHKERRQ(PetscMalloc1(n, &offsets)); 247 CHKERRQ(PetscMalloc1(n*numRanks, &remoteOffsets)); 248 249 offsets[0] = 0; 250 for (i = 1; i < n; ++i) { 251 PetscInt nroots; 252 253 CHKERRQ(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 CHKERRMPI(MPI_Type_contiguous(n, MPIU_INT, &contig)); 259 CHKERRMPI(MPI_Type_commit(&contig)); 260 261 CHKERRQ(PetscSFBcastBegin(rankSF, contig, offsets, remoteOffsets,MPI_REPLACE)); 262 CHKERRQ(PetscSFBcastEnd(rankSF, contig, offsets, remoteOffsets,MPI_REPLACE)); 263 CHKERRMPI(MPI_Type_free(&contig)); 264 CHKERRQ(PetscFree(offsets)); 265 CHKERRQ(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 CHKERRQ(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 CHKERRQ(PetscHMapIGet(rankToIndex, rank, &idx)); 281 PetscCheckFalse(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 CHKERRQ(PetscHMapIDestroy(&rankToIndex)); 294 CHKERRQ(PetscFree(remoteOffsets)); 295 CHKERRQ(PetscSFCreate(PetscObjectComm((PetscObject)pc), &patch->sectionSF)); 296 CHKERRQ(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 PetscCheckFalse(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) CHKERRQ(PetscFree(patch->sub_mat_type)); 415 CHKERRQ(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 CHKERRQ(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 %D", (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 %D", (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 CHKERRQ(PCGetDM(pc, &dm)); 517 CHKERRQ(DMConvert(dm, DMPLEX, &plex)); 518 dm = plex; 519 CHKERRQ(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 520 CHKERRQ(PetscMalloc1(nsubspaces, &sfs)); 521 CHKERRQ(PetscMalloc1(nsubspaces, &patch->dofSection)); 522 CHKERRQ(PetscMalloc1(nsubspaces, &patch->bs)); 523 CHKERRQ(PetscMalloc1(nsubspaces, &patch->nodesPerCell)); 524 CHKERRQ(PetscMalloc1(nsubspaces, &patch->cellNodeMap)); 525 CHKERRQ(PetscMalloc1(nsubspaces+1, &patch->subspaceOffsets)); 526 527 patch->nsubspaces = nsubspaces; 528 patch->totalDofsPerCell = 0; 529 for (i = 0; i < nsubspaces; ++i) { 530 CHKERRQ(DMGetLocalSection(dms[i], &patch->dofSection[i])); 531 CHKERRQ(PetscObjectReference((PetscObject) patch->dofSection[i])); 532 CHKERRQ(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 CHKERRQ(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 CHKERRQ(PCPatchCreateDefaultSF_Private(pc, nsubspaces, sfs, patch->bs)); 541 CHKERRQ(PetscFree(sfs)); 542 543 patch->subspaceOffsets[nsubspaces] = subspaceOffsets[nsubspaces]; 544 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes)); 545 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes)); 546 CHKERRQ(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 CHKERRQ(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 559 CHKERRQ(DMGetNumFields(dm, &patch->nsubspaces)); 560 CHKERRQ(PetscCalloc1(patch->nsubspaces, &patch->dofSection)); 561 CHKERRQ(PetscMalloc1(patch->nsubspaces, &patch->bs)); 562 CHKERRQ(PetscMalloc1(patch->nsubspaces, &patch->nodesPerCell)); 563 CHKERRQ(PetscMalloc1(patch->nsubspaces, &patch->cellNodeMap)); 564 CHKERRQ(PetscCalloc1(patch->nsubspaces+1, &patch->subspaceOffsets)); 565 CHKERRQ(DMGetLocalSection(dm, &patch->dofSection[0])); 566 CHKERRQ(PetscObjectReference((PetscObject) patch->dofSection[0])); 567 CHKERRQ(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 CHKERRQ(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 CHKERRQ(DMGetSectionSF(dm, &patch->sectionSF)); 577 CHKERRQ(PetscObjectReference((PetscObject) patch->sectionSF)); 578 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes)); 579 CHKERRQ(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 CHKERRQ(PCGetDM(pc, &dm)); 765 CHKERRQ(DMConvert(dm, DMPLEX, &plex)); 766 dm = plex; 767 CHKERRQ(DMPlexGetHeightStratum(dm, 1, &fBegin, &fEnd)); 768 CHKERRQ(PCPatchGetIgnoreDim(pc, &ignoredim)); 769 if (ignoredim >= 0) CHKERRQ(DMPlexGetDepthStratum(dm, ignoredim, &iStart, &iEnd)); 770 CHKERRQ(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 CHKERRQ(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 CHKERRQ(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 CHKERRQ(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 CHKERRQ(DMPlexGetTransitiveClosure(dm, seenpoint, PETSC_FALSE, &fStarSize, &fStar)); 794 for (fsi = 0; fsi < fStarSize*2; fsi += 2) { 795 CHKERRQ(DMPlexGetTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, &fClosureSize, &fClosure)); 796 for (fci = 0; fci < fClosureSize*2; fci += 2) { 797 CHKERRQ(PetscHSetIAdd(cht, fClosure[fci])); 798 } 799 CHKERRQ(DMPlexRestoreTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, NULL, &fClosure)); 800 } 801 CHKERRQ(DMPlexRestoreTransitiveClosure(dm, seenpoint, PETSC_FALSE, NULL, &fStar)); 802 } 803 } 804 } 805 CHKERRQ(DMPlexRestoreTransitiveClosure(dm, ownedpoint, PETSC_TRUE, NULL, &closure)); 806 } 807 CHKERRQ(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, NULL, &star)); 808 } 809 CHKERRQ(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) CHKERRQ(PetscSectionGetDof(dofSection[0], p, dof)); 819 if (off) CHKERRQ(PetscSectionGetOffset(dofSection[0], p, off)); 820 } else { 821 if (dof) CHKERRQ(PetscSectionGetFieldDof(dofSection[0], p, f, dof)); 822 if (off) CHKERRQ(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 CHKERRQ(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 CHKERRQ(PetscSectionGetOffset(dofSection[g], p, &fdof)); 840 *off += fdof; 841 } 842 } 843 } else { 844 if (dof) CHKERRQ(PetscSectionGetDof(dofSection[f], p, dof)); 845 if (off) CHKERRQ(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 CHKERRQ(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 CHKERRQ(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 CHKERRQ(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 CHKERRQ(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 CHKERRQ(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 CHKERRQ(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 CHKERRQ(PetscHSetIClear(C)); 913 PetscHashIterBegin(B, hi); 914 while (!PetscHashIterAtEnd(B, hi)) { 915 PetscHashIterGetKey(B, hi, key); 916 PetscHashIterNext(B, hi); 917 CHKERRQ(PetscHSetIHas(A, key, &flg)); 918 if (!flg) CHKERRQ(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, cht; 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 CHKERRQ(PetscHSetICreate(&ht)); 951 CHKERRQ(PetscHSetICreate(&cht)); 952 953 CHKERRQ(PCGetDM(pc, &dm)); 954 PetscCheck(dm,PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONGSTATE, "DM not yet set on patch PC"); 955 CHKERRQ(DMConvert(dm, DMPLEX, &plex)); 956 dm = plex; 957 CHKERRQ(DMPlexGetChart(dm, &pStart, &pEnd)); 958 CHKERRQ(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 959 960 if (patch->user_patches) { 961 CHKERRQ(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) CHKERRQ(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd)); 967 else CHKERRQ(DMPlexGetDepthStratum(dm, patch->dim, &vStart, &vEnd)); 968 } else CHKERRQ(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 CHKERRQ(DMHasLabel(dm, "pyop2_ghost", &isFiredrake)); 973 if (isFiredrake) { 974 CHKERRQ(DMGetLabel(dm, "pyop2_ghost", &ghost)); 975 CHKERRQ(DMLabelCreateIndex(ghost, pStart, pEnd)); 976 } else { 977 PetscSF sf; 978 979 CHKERRQ(DMGetPointSF(dm, &sf)); 980 CHKERRQ(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL)); 981 nleaves = PetscMax(nleaves, 0); 982 } 983 984 CHKERRQ(PetscSectionCreate(PETSC_COMM_SELF, &patch->cellCounts)); 985 CHKERRQ(PetscObjectSetName((PetscObject) patch->cellCounts, "Patch Cell Layout")); 986 cellCounts = patch->cellCounts; 987 CHKERRQ(PetscSectionSetChart(cellCounts, vStart, vEnd)); 988 CHKERRQ(PetscSectionCreate(PETSC_COMM_SELF, &patch->pointCounts)); 989 CHKERRQ(PetscObjectSetName((PetscObject) patch->pointCounts, "Patch Point Layout")); 990 pointCounts = patch->pointCounts; 991 CHKERRQ(PetscSectionSetChart(pointCounts, vStart, vEnd)); 992 CHKERRQ(PetscSectionCreate(PETSC_COMM_SELF, &patch->extFacetCounts)); 993 CHKERRQ(PetscObjectSetName((PetscObject) patch->extFacetCounts, "Patch Exterior Facet Layout")); 994 extFacetCounts = patch->extFacetCounts; 995 CHKERRQ(PetscSectionSetChart(extFacetCounts, vStart, vEnd)); 996 CHKERRQ(PetscSectionCreate(PETSC_COMM_SELF, &patch->intFacetCounts)); 997 CHKERRQ(PetscObjectSetName((PetscObject) patch->intFacetCounts, "Patch Interior Facet Layout")); 998 intFacetCounts = patch->intFacetCounts; 999 CHKERRQ(PetscSectionSetChart(intFacetCounts, vStart, vEnd)); 1000 /* Count cells and points in the patch surrounding each entity */ 1001 CHKERRQ(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) CHKERRQ(DMLabelHasPoint(ghost, v, &flg)); 1009 else {CHKERRQ(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 CHKERRQ(patch->patchconstructop((void *) patch, dm, v, ht)); 1015 CHKERRQ(PCPatchCompleteCellPatch(pc, ht, cht)); 1016 CHKERRQ(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 CHKERRQ(DMPlexGetSupport(dm, point, &support)); 1031 CHKERRQ(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 CHKERRQ(PetscHSetIHas(cht, support[p], &found)); 1039 if (!found) { 1040 interior = PETSC_FALSE; 1041 break; 1042 } 1043 } 1044 } 1045 if (interior) { 1046 CHKERRQ(PetscSectionAddDof(intFacetCounts, v, 1)); 1047 } else { 1048 CHKERRQ(PetscSectionAddDof(extFacetCounts, v, 1)); 1049 } 1050 } 1051 CHKERRQ(PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL)); 1052 if (pdof) CHKERRQ(PetscSectionAddDof(pointCounts, v, 1)); 1053 if (point >= cStart && point < cEnd) CHKERRQ(PetscSectionAddDof(cellCounts, v, 1)); 1054 PetscHashIterNext(cht, hi); 1055 } 1056 } 1057 if (isFiredrake) CHKERRQ(DMLabelDestroyIndex(ghost)); 1058 1059 CHKERRQ(PetscSectionSetUp(cellCounts)); 1060 CHKERRQ(PetscSectionGetStorageSize(cellCounts, &numCells)); 1061 CHKERRQ(PetscMalloc1(numCells, &cellsArray)); 1062 CHKERRQ(PetscSectionSetUp(pointCounts)); 1063 CHKERRQ(PetscSectionGetStorageSize(pointCounts, &numPoints)); 1064 CHKERRQ(PetscMalloc1(numPoints, &pointsArray)); 1065 1066 CHKERRQ(PetscSectionSetUp(intFacetCounts)); 1067 CHKERRQ(PetscSectionSetUp(extFacetCounts)); 1068 CHKERRQ(PetscSectionGetStorageSize(intFacetCounts, &numIntFacets)); 1069 CHKERRQ(PetscSectionGetStorageSize(extFacetCounts, &numExtFacets)); 1070 CHKERRQ(PetscMalloc1(numIntFacets, &intFacetsArray)); 1071 CHKERRQ(PetscMalloc1(numIntFacets*2, &intFacetsToPatchCell)); 1072 CHKERRQ(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 CHKERRQ(PetscSectionGetDof(pointCounts, v, &dof)); 1080 CHKERRQ(PetscSectionGetOffset(pointCounts, v, &off)); 1081 CHKERRQ(PetscSectionGetDof(cellCounts, v, &cdof)); 1082 CHKERRQ(PetscSectionGetOffset(cellCounts, v, &coff)); 1083 CHKERRQ(PetscSectionGetDof(intFacetCounts, v, &ifdof)); 1084 CHKERRQ(PetscSectionGetOffset(intFacetCounts, v, &ifoff)); 1085 CHKERRQ(PetscSectionGetDof(extFacetCounts, v, &efdof)); 1086 CHKERRQ(PetscSectionGetOffset(extFacetCounts, v, &efoff)); 1087 if (dof <= 0) continue; 1088 CHKERRQ(patch->patchconstructop((void *) patch, dm, v, ht)); 1089 CHKERRQ(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 CHKERRQ(DMPlexGetSupport(dm, point, &support)); 1100 CHKERRQ(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 CHKERRQ(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 CHKERRQ(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 PetscCheckFalse(ifn != ifdof,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of interior facets in patch %D is %D, but should be %D", v, ifn, ifdof); 1128 PetscCheckFalse(efn != efdof,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of exterior facets in patch %D is %D, but should be %D", v, efn, efdof); 1129 PetscCheckFalse(cn != cdof,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of cells in patch %D is %D, but should be %D", v, cn, cdof); 1130 PetscCheckFalse(n != dof,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of points in patch %D is %D, but should be %D", 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 PetscCheckFalse(!(found0 && found1),PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Didn't manage to find local point numbers for facet support"); 1148 } 1149 } 1150 CHKERRQ(PetscHSetIDestroy(&ht)); 1151 CHKERRQ(PetscHSetIDestroy(&cht)); 1152 1153 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numCells, cellsArray, PETSC_OWN_POINTER, &patch->cells)); 1154 CHKERRQ(PetscObjectSetName((PetscObject) patch->cells, "Patch Cells")); 1155 if (patch->viewCells) { 1156 CHKERRQ(ObjectView((PetscObject) patch->cellCounts, patch->viewerCells, patch->formatCells)); 1157 CHKERRQ(ObjectView((PetscObject) patch->cells, patch->viewerCells, patch->formatCells)); 1158 } 1159 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray, PETSC_OWN_POINTER, &patch->intFacets)); 1160 CHKERRQ(PetscObjectSetName((PetscObject) patch->intFacets, "Patch Interior Facets")); 1161 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, 2*numIntFacets, intFacetsToPatchCell, PETSC_OWN_POINTER, &patch->intFacetsToPatchCell)); 1162 CHKERRQ(PetscObjectSetName((PetscObject) patch->intFacetsToPatchCell, "Patch Interior Facets local support")); 1163 if (patch->viewIntFacets) { 1164 CHKERRQ(ObjectView((PetscObject) patch->intFacetCounts, patch->viewerIntFacets, patch->formatIntFacets)); 1165 CHKERRQ(ObjectView((PetscObject) patch->intFacets, patch->viewerIntFacets, patch->formatIntFacets)); 1166 CHKERRQ(ObjectView((PetscObject) patch->intFacetsToPatchCell, patch->viewerIntFacets, patch->formatIntFacets)); 1167 } 1168 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numExtFacets, extFacetsArray, PETSC_OWN_POINTER, &patch->extFacets)); 1169 CHKERRQ(PetscObjectSetName((PetscObject) patch->extFacets, "Patch Exterior Facets")); 1170 if (patch->viewExtFacets) { 1171 CHKERRQ(ObjectView((PetscObject) patch->extFacetCounts, patch->viewerExtFacets, patch->formatExtFacets)); 1172 CHKERRQ(ObjectView((PetscObject) patch->extFacets, patch->viewerExtFacets, patch->formatExtFacets)); 1173 } 1174 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numPoints, pointsArray, PETSC_OWN_POINTER, &patch->points)); 1175 CHKERRQ(PetscObjectSetName((PetscObject) patch->points, "Patch Points")); 1176 if (patch->viewPoints) { 1177 CHKERRQ(ObjectView((PetscObject) patch->pointCounts, patch->viewerPoints, patch->formatPoints)); 1178 CHKERRQ(ObjectView((PetscObject) patch->points, patch->viewerPoints, patch->formatPoints)); 1179 } 1180 CHKERRQ(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 CHKERRQ(PCGetDM(pc, &dm)); 1247 CHKERRQ(DMConvert(dm, DMPLEX, &plex)); 1248 dm = plex; 1249 /* dofcounts section is cellcounts section * dofPerCell */ 1250 CHKERRQ(PetscSectionGetStorageSize(cellCounts, &numCells)); 1251 CHKERRQ(PetscSectionGetStorageSize(patch->pointCounts, &numPoints)); 1252 numDofs = numCells * totalDofsPerCell; 1253 CHKERRQ(PetscMalloc1(numDofs, &dofsArray)); 1254 CHKERRQ(PetscMalloc1(numPoints*Nf, &offsArray)); 1255 CHKERRQ(PetscMalloc1(numDofs, &asmArray)); 1256 CHKERRQ(PetscMalloc1(numCells, &newCellsArray)); 1257 CHKERRQ(PetscSectionGetChart(cellCounts, &vStart, &vEnd)); 1258 CHKERRQ(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCounts)); 1259 gtolCounts = patch->gtolCounts; 1260 CHKERRQ(PetscSectionSetChart(gtolCounts, vStart, vEnd)); 1261 CHKERRQ(PetscObjectSetName((PetscObject) patch->gtolCounts, "Patch Global Index Section")); 1262 1263 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1264 CHKERRQ(PetscMalloc1(numPoints*Nf, &offsArrayWithArtificial)); 1265 CHKERRQ(PetscMalloc1(numDofs, &asmArrayWithArtificial)); 1266 CHKERRQ(PetscMalloc1(numDofs, &dofsArrayWithArtificial)); 1267 CHKERRQ(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithArtificial)); 1268 gtolCountsWithArtificial = patch->gtolCountsWithArtificial; 1269 CHKERRQ(PetscSectionSetChart(gtolCountsWithArtificial, vStart, vEnd)); 1270 CHKERRQ(PetscObjectSetName((PetscObject) patch->gtolCountsWithArtificial, "Patch Global Index Section Including Artificial BCs")); 1271 } 1272 1273 isNonlinear = patch->isNonlinear; 1274 if (isNonlinear) { 1275 CHKERRQ(PetscMalloc1(numPoints*Nf, &offsArrayWithAll)); 1276 CHKERRQ(PetscMalloc1(numDofs, &asmArrayWithAll)); 1277 CHKERRQ(PetscMalloc1(numDofs, &dofsArrayWithAll)); 1278 CHKERRQ(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithAll)); 1279 gtolCountsWithAll = patch->gtolCountsWithAll; 1280 CHKERRQ(PetscSectionSetChart(gtolCountsWithAll, vStart, vEnd)); 1281 CHKERRQ(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 CHKERRQ(PetscHSetICreate(&globalBcs)); 1287 CHKERRQ(ISGetIndices(patch->ghostBcNodes, &bcNodes)); 1288 CHKERRQ(ISGetSize(patch->ghostBcNodes, &numBcs)); 1289 for (i = 0; i < numBcs; ++i) { 1290 CHKERRQ(PetscHSetIAdd(globalBcs, bcNodes[i])); /* these are already in concatenated numbering */ 1291 } 1292 CHKERRQ(ISRestoreIndices(patch->ghostBcNodes, &bcNodes)); 1293 CHKERRQ(ISDestroy(&patch->ghostBcNodes)); /* memory optimisation */ 1294 1295 /* Hash tables for artificial BC construction */ 1296 CHKERRQ(PetscHSetICreate(&ownedpts)); 1297 CHKERRQ(PetscHSetICreate(&seenpts)); 1298 CHKERRQ(PetscHSetICreate(&owneddofs)); 1299 CHKERRQ(PetscHSetICreate(&seendofs)); 1300 CHKERRQ(PetscHSetICreate(&artificialbcs)); 1301 1302 CHKERRQ(ISGetIndices(cells, &cellsArray)); 1303 CHKERRQ(ISGetIndices(points, &pointsArray)); 1304 CHKERRQ(PetscHMapICreate(&ht)); 1305 CHKERRQ(PetscHMapICreate(&htWithArtificial)); 1306 CHKERRQ(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 CHKERRQ(PetscHMapIClear(ht)); 1314 CHKERRQ(PetscHMapIClear(htWithArtificial)); 1315 CHKERRQ(PetscHMapIClear(htWithAll)); 1316 CHKERRQ(PetscSectionGetDof(cellCounts, v, &dof)); 1317 CHKERRQ(PetscSectionGetOffset(cellCounts, v, &off)); 1318 if (dof <= 0) continue; 1319 1320 /* Calculate the global numbers of the artificial BC dofs here first */ 1321 CHKERRQ(patch->patchconstructop((void*)patch, dm, v, ownedpts)); 1322 CHKERRQ(PCPatchCompleteCellPatch(pc, ownedpts, seenpts)); 1323 CHKERRQ(PCPatchGetPointDofs(pc, ownedpts, owneddofs, v, &patch->subspaces_to_exclude)); 1324 CHKERRQ(PCPatchGetPointDofs(pc, seenpts, seendofs, v, NULL)); 1325 CHKERRQ(PCPatchComputeSetDifference_Private(owneddofs, seendofs, artificialbcs)); 1326 if (patch->viewPatches) { 1327 PetscHSetI globalbcdofs; 1328 PetscHashIter hi; 1329 MPI_Comm comm = PetscObjectComm((PetscObject)pc); 1330 1331 CHKERRQ(PetscHSetICreate(&globalbcdofs)); 1332 CHKERRQ(PetscSynchronizedPrintf(comm, "Patch %d: 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 CHKERRQ(PetscSynchronizedPrintf(comm, "%d ", globalDof)); 1340 } 1341 CHKERRQ(PetscSynchronizedPrintf(comm, "\n")); 1342 CHKERRQ(PetscSynchronizedPrintf(comm, "Patch %d: 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 CHKERRQ(PetscSynchronizedPrintf(comm, "%d ", globalDof)); 1351 1352 CHKERRQ(PetscHSetIHas(globalBcs, globalDof, &flg)); 1353 if (flg) CHKERRQ(PetscHSetIAdd(globalbcdofs, globalDof)); 1354 } 1355 CHKERRQ(PetscSynchronizedPrintf(comm, "\n")); 1356 CHKERRQ(PetscSynchronizedPrintf(comm, "Patch %d: global BCs:\n", v)); 1357 CHKERRQ(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 CHKERRQ(PetscSynchronizedPrintf(comm, "%d ", globalDof)); 1365 } 1366 } 1367 CHKERRQ(PetscSynchronizedPrintf(comm, "\n")); 1368 CHKERRQ(PetscSynchronizedPrintf(comm, "Patch %d: artificial BCs:\n", v)); 1369 CHKERRQ(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 CHKERRQ(PetscSynchronizedPrintf(comm, "%d ", globalDof)); 1377 } 1378 } 1379 CHKERRQ(PetscSynchronizedPrintf(comm, "\n\n")); 1380 CHKERRQ(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 CHKERRQ(PetscSectionGetDof(cellNumbering, c, &cell)); 1396 PetscCheckFalse(cell <= 0,PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_OUTOFRANGE, "Cell %D doesn't appear in cell numbering map", c); 1397 CHKERRQ(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 CHKERRQ(PetscHSetIHas(globalBcs, globalDof + l, &isGlobalBcDof)); 1410 CHKERRQ(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 CHKERRQ(PetscHMapIGet(ht, globalDof + l, &localDof)); 1417 if (localDof == -1) { 1418 localDof = localIndex++; 1419 CHKERRQ(PetscHMapISet(ht, globalDof + l, localDof)); 1420 } 1421 PetscCheckFalse(globalIndex >= numDofs,PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", 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 CHKERRQ(PetscHMapIGet(htWithArtificial, globalDof + l, &localDof)); 1431 if (localDof == -1) { 1432 localDof = localIndexWithArtificial++; 1433 CHKERRQ(PetscHMapISet(htWithArtificial, globalDof + l, localDof)); 1434 } 1435 PetscCheckFalse(globalIndex >= numDofs,PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", 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 CHKERRQ(PetscHMapIGet(htWithAll, globalDof + l, &localDof)); 1445 if (localDof == -1) { 1446 localDof = localIndexWithAll++; 1447 CHKERRQ(PetscHMapISet(htWithAll, globalDof + l, localDof)); 1448 } 1449 PetscCheckFalse(globalIndex >= numDofs,PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", 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 CHKERRQ(PetscHMapIGetSize(htWithArtificial, &dof)); 1461 CHKERRQ(PetscSectionSetDof(gtolCountsWithArtificial, v, dof)); 1462 } 1463 if (isNonlinear) { 1464 CHKERRQ(PetscHMapIGetSize(htWithAll, &dof)); 1465 CHKERRQ(PetscSectionSetDof(gtolCountsWithAll, v, dof)); 1466 } 1467 CHKERRQ(PetscHMapIGetSize(ht, &dof)); 1468 CHKERRQ(PetscSectionSetDof(gtolCounts, v, dof)); 1469 } 1470 1471 CHKERRQ(DMDestroy(&dm)); 1472 PetscCheckFalse(globalIndex != numDofs,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Expected number of dofs (%d) doesn't match found number (%d)", numDofs, globalIndex); 1473 CHKERRQ(PetscSectionSetUp(gtolCounts)); 1474 CHKERRQ(PetscSectionGetStorageSize(gtolCounts, &numGlobalDofs)); 1475 CHKERRQ(PetscMalloc1(numGlobalDofs, &globalDofsArray)); 1476 1477 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1478 CHKERRQ(PetscSectionSetUp(gtolCountsWithArtificial)); 1479 CHKERRQ(PetscSectionGetStorageSize(gtolCountsWithArtificial, &numGlobalDofsWithArtificial)); 1480 CHKERRQ(PetscMalloc1(numGlobalDofsWithArtificial, &globalDofsArrayWithArtificial)); 1481 } 1482 if (isNonlinear) { 1483 CHKERRQ(PetscSectionSetUp(gtolCountsWithAll)); 1484 CHKERRQ(PetscSectionGetStorageSize(gtolCountsWithAll, &numGlobalDofsWithAll)); 1485 CHKERRQ(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 CHKERRQ(PetscHMapIClear(ht)); 1493 CHKERRQ(PetscHMapIClear(htWithArtificial)); 1494 CHKERRQ(PetscHMapIClear(htWithAll)); 1495 CHKERRQ(PetscSectionGetDof(cellCounts, v, &dof)); 1496 CHKERRQ(PetscSectionGetOffset(cellCounts, v, &off)); 1497 CHKERRQ(PetscSectionGetDof(pointCounts, v, &Np)); 1498 CHKERRQ(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) CHKERRQ(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) CHKERRQ(PetscHMapISet(ht, globalDof, localDof)); 1519 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1520 const PetscInt localDofWithArtificial = dofsArrayWithArtificial[key]; 1521 if (localDofWithArtificial >= 0) { 1522 CHKERRQ(PetscHMapISet(htWithArtificial, globalDof, localDofWithArtificial)); 1523 } 1524 } 1525 if (isNonlinear) { 1526 const PetscInt localDofWithAll = dofsArrayWithAll[key]; 1527 if (localDofWithAll >= 0) { 1528 CHKERRQ(PetscHMapISet(htWithAll, globalDof, localDofWithAll)); 1529 } 1530 } 1531 key++; 1532 } 1533 } 1534 } 1535 1536 /* Shove it in the output data structure. */ 1537 CHKERRQ(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 CHKERRQ(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 CHKERRQ(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 CHKERRQ(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, point, NULL, &globalDof)); 1576 CHKERRQ(PetscHMapIGet(ht, globalDof, &localDof)); 1577 offsArray[(ooff + p)*Nf + k] = localDof; 1578 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1579 CHKERRQ(PetscHMapIGet(htWithArtificial, globalDof, &localDof)); 1580 offsArrayWithArtificial[(ooff + p)*Nf + k] = localDof; 1581 } 1582 if (isNonlinear) { 1583 CHKERRQ(PetscHMapIGet(htWithAll, globalDof, &localDof)); 1584 offsArrayWithAll[(ooff + p)*Nf + k] = localDof; 1585 } 1586 } 1587 } 1588 1589 CHKERRQ(PetscHSetIDestroy(&globalBcs)); 1590 CHKERRQ(PetscHSetIDestroy(&ownedpts)); 1591 CHKERRQ(PetscHSetIDestroy(&seenpts)); 1592 CHKERRQ(PetscHSetIDestroy(&owneddofs)); 1593 CHKERRQ(PetscHSetIDestroy(&seendofs)); 1594 CHKERRQ(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) CHKERRQ(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 CHKERRQ(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 CHKERRQ(PetscHMapIGet(htWithArtificial, globalDof, &localDof)); 1626 asmArrayWithArtificial[asmKey] = localDof; 1627 } 1628 if (isNonlinear) { 1629 CHKERRQ(PetscHMapIGet(htWithAll, globalDof, &localDof)); 1630 asmArrayWithAll[asmKey] = localDof; 1631 } 1632 asmKey++; 1633 } 1634 } 1635 } 1636 } 1637 } 1638 } 1639 if (1 == patch->nsubspaces) { 1640 CHKERRQ(PetscArraycpy(asmArray, dofsArray, numDofs)); 1641 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1642 CHKERRQ(PetscArraycpy(asmArrayWithArtificial, dofsArrayWithArtificial, numDofs)); 1643 } 1644 if (isNonlinear) { 1645 CHKERRQ(PetscArraycpy(asmArrayWithAll, dofsArrayWithAll, numDofs)); 1646 } 1647 } 1648 1649 CHKERRQ(PetscHMapIDestroy(&ht)); 1650 CHKERRQ(PetscHMapIDestroy(&htWithArtificial)); 1651 CHKERRQ(PetscHMapIDestroy(&htWithAll)); 1652 CHKERRQ(ISRestoreIndices(cells, &cellsArray)); 1653 CHKERRQ(ISRestoreIndices(points, &pointsArray)); 1654 CHKERRQ(PetscFree(dofsArray)); 1655 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1656 CHKERRQ(PetscFree(dofsArrayWithArtificial)); 1657 } 1658 if (isNonlinear) { 1659 CHKERRQ(PetscFree(dofsArrayWithAll)); 1660 } 1661 /* Create placeholder section for map from points to patch dofs */ 1662 CHKERRQ(PetscSectionCreate(PETSC_COMM_SELF, &patch->patchSection)); 1663 CHKERRQ(PetscSectionSetNumFields(patch->patchSection, patch->nsubspaces)); 1664 if (patch->combined) { 1665 PetscInt numFields; 1666 CHKERRQ(PetscSectionGetNumFields(patch->dofSection[0], &numFields)); 1667 PetscCheckFalse(numFields != patch->nsubspaces,PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Mismatch between number of section fields %D and number of subspaces %D", numFields, patch->nsubspaces); 1668 CHKERRQ(PetscSectionGetChart(patch->dofSection[0], &pStart, &pEnd)); 1669 CHKERRQ(PetscSectionSetChart(patch->patchSection, pStart, pEnd)); 1670 for (p = pStart; p < pEnd; ++p) { 1671 PetscInt dof, fdof, f; 1672 1673 CHKERRQ(PetscSectionGetDof(patch->dofSection[0], p, &dof)); 1674 CHKERRQ(PetscSectionSetDof(patch->patchSection, p, dof)); 1675 for (f = 0; f < patch->nsubspaces; ++f) { 1676 CHKERRQ(PetscSectionGetFieldDof(patch->dofSection[0], p, f, &fdof)); 1677 CHKERRQ(PetscSectionSetFieldDof(patch->patchSection, p, f, fdof)); 1678 } 1679 } 1680 } else { 1681 PetscInt pStartf, pEndf, f; 1682 pStart = PETSC_MAX_INT; 1683 pEnd = PETSC_MIN_INT; 1684 for (f = 0; f < patch->nsubspaces; ++f) { 1685 CHKERRQ(PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf)); 1686 pStart = PetscMin(pStart, pStartf); 1687 pEnd = PetscMax(pEnd, pEndf); 1688 } 1689 CHKERRQ(PetscSectionSetChart(patch->patchSection, pStart, pEnd)); 1690 for (f = 0; f < patch->nsubspaces; ++f) { 1691 CHKERRQ(PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf)); 1692 for (p = pStartf; p < pEndf; ++p) { 1693 PetscInt fdof; 1694 CHKERRQ(PetscSectionGetDof(patch->dofSection[f], p, &fdof)); 1695 CHKERRQ(PetscSectionAddDof(patch->patchSection, p, fdof)); 1696 CHKERRQ(PetscSectionSetFieldDof(patch->patchSection, p, f, fdof)); 1697 } 1698 } 1699 } 1700 CHKERRQ(PetscSectionSetUp(patch->patchSection)); 1701 CHKERRQ(PetscSectionSetUseFieldOffsets(patch->patchSection, PETSC_TRUE)); 1702 /* Replace cell indices with firedrake-numbered ones. */ 1703 CHKERRQ(ISGeneralSetIndices(cells, numCells, (const PetscInt *) newCellsArray, PETSC_OWN_POINTER)); 1704 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofs, globalDofsArray, PETSC_OWN_POINTER, &patch->gtol)); 1705 CHKERRQ(PetscObjectSetName((PetscObject) patch->gtol, "Global Indices")); 1706 CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_g2l_view", patch->classname)); 1707 CHKERRQ(PetscSectionViewFromOptions(patch->gtolCounts, (PetscObject) pc, option)); 1708 CHKERRQ(ISViewFromOptions(patch->gtol, (PetscObject) pc, option)); 1709 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArray, PETSC_OWN_POINTER, &patch->dofs)); 1710 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArray, PETSC_OWN_POINTER, &patch->offs)); 1711 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1712 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithArtificial, globalDofsArrayWithArtificial, PETSC_OWN_POINTER, &patch->gtolWithArtificial)); 1713 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithArtificial, PETSC_OWN_POINTER, &patch->dofsWithArtificial)); 1714 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithArtificial, PETSC_OWN_POINTER, &patch->offsWithArtificial)); 1715 } 1716 if (isNonlinear) { 1717 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithAll, globalDofsArrayWithAll, PETSC_OWN_POINTER, &patch->gtolWithAll)); 1718 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithAll, PETSC_OWN_POINTER, &patch->dofsWithAll)); 1719 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithAll, PETSC_OWN_POINTER, &patch->offsWithAll)); 1720 } 1721 PetscFunctionReturn(0); 1722 } 1723 1724 static PetscErrorCode PCPatchCreateMatrix_Private(PC pc, PetscInt point, Mat *mat, PetscBool withArtificial) 1725 { 1726 PC_PATCH *patch = (PC_PATCH *) pc->data; 1727 PetscBool flg; 1728 PetscInt csize, rsize; 1729 const char *prefix = NULL; 1730 1731 PetscFunctionBegin; 1732 if (withArtificial) { 1733 /* would be nice if we could create a rectangular matrix of size numDofsWithArtificial x numDofs here */ 1734 PetscInt pStart; 1735 CHKERRQ(PetscSectionGetChart(patch->gtolCountsWithArtificial, &pStart, NULL)); 1736 CHKERRQ(PetscSectionGetDof(patch->gtolCountsWithArtificial, point + pStart, &rsize)); 1737 csize = rsize; 1738 } else { 1739 PetscInt pStart; 1740 CHKERRQ(PetscSectionGetChart(patch->gtolCounts, &pStart, NULL)); 1741 CHKERRQ(PetscSectionGetDof(patch->gtolCounts, point + pStart, &rsize)); 1742 csize = rsize; 1743 } 1744 1745 CHKERRQ(MatCreate(PETSC_COMM_SELF, mat)); 1746 CHKERRQ(PCGetOptionsPrefix(pc, &prefix)); 1747 CHKERRQ(MatSetOptionsPrefix(*mat, prefix)); 1748 CHKERRQ(MatAppendOptionsPrefix(*mat, "pc_patch_sub_")); 1749 if (patch->sub_mat_type) CHKERRQ(MatSetType(*mat, patch->sub_mat_type)); 1750 else if (!patch->sub_mat_type) CHKERRQ(MatSetType(*mat, MATDENSE)); 1751 CHKERRQ(MatSetSizes(*mat, rsize, csize, rsize, csize)); 1752 CHKERRQ(PetscObjectTypeCompare((PetscObject) *mat, MATDENSE, &flg)); 1753 if (!flg) CHKERRQ(PetscObjectTypeCompare((PetscObject)*mat, MATSEQDENSE, &flg)); 1754 /* Sparse patch matrices */ 1755 if (!flg) { 1756 PetscBT bt; 1757 PetscInt *dnnz = NULL; 1758 const PetscInt *dofsArray = NULL; 1759 PetscInt pStart, pEnd, ncell, offset, c, i, j; 1760 1761 if (withArtificial) { 1762 CHKERRQ(ISGetIndices(patch->dofsWithArtificial, &dofsArray)); 1763 } else { 1764 CHKERRQ(ISGetIndices(patch->dofs, &dofsArray)); 1765 } 1766 CHKERRQ(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd)); 1767 point += pStart; 1768 PetscCheckFalse(point >= pEnd,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %D not in [%D, %D)", point, pStart, pEnd); 1769 CHKERRQ(PetscSectionGetDof(patch->cellCounts, point, &ncell)); 1770 CHKERRQ(PetscSectionGetOffset(patch->cellCounts, point, &offset)); 1771 CHKERRQ(PetscLogEventBegin(PC_Patch_Prealloc, pc, 0, 0, 0)); 1772 /* A PetscBT uses N^2 bits to store the sparsity pattern on a 1773 * patch. This is probably OK if the patches are not too big, 1774 * but uses too much memory. We therefore switch based on rsize. */ 1775 if (rsize < 3000) { /* FIXME: I picked this switch value out of my hat */ 1776 PetscScalar *zeroes; 1777 PetscInt rows; 1778 1779 CHKERRQ(PetscCalloc1(rsize, &dnnz)); 1780 CHKERRQ(PetscBTCreate(rsize*rsize, &bt)); 1781 for (c = 0; c < ncell; ++c) { 1782 const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell; 1783 for (i = 0; i < patch->totalDofsPerCell; ++i) { 1784 const PetscInt row = idx[i]; 1785 if (row < 0) continue; 1786 for (j = 0; j < patch->totalDofsPerCell; ++j) { 1787 const PetscInt col = idx[j]; 1788 const PetscInt key = row*rsize + col; 1789 if (col < 0) continue; 1790 if (!PetscBTLookupSet(bt, key)) ++dnnz[row]; 1791 } 1792 } 1793 } 1794 1795 if (patch->usercomputeopintfacet) { 1796 const PetscInt *intFacetsArray = NULL; 1797 PetscInt i, numIntFacets, intFacetOffset; 1798 const PetscInt *facetCells = NULL; 1799 1800 CHKERRQ(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets)); 1801 CHKERRQ(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset)); 1802 CHKERRQ(ISGetIndices(patch->intFacetsToPatchCell, &facetCells)); 1803 CHKERRQ(ISGetIndices(patch->intFacets, &intFacetsArray)); 1804 for (i = 0; i < numIntFacets; i++) { 1805 const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0]; 1806 const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1]; 1807 PetscInt celli, cellj; 1808 1809 for (celli = 0; celli < patch->totalDofsPerCell; celli++) { 1810 const PetscInt row = dofsArray[(offset + cell0)*patch->totalDofsPerCell + celli]; 1811 if (row < 0) continue; 1812 for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) { 1813 const PetscInt col = dofsArray[(offset + cell1)*patch->totalDofsPerCell + cellj]; 1814 const PetscInt key = row*rsize + col; 1815 if (col < 0) continue; 1816 if (!PetscBTLookupSet(bt, key)) ++dnnz[row]; 1817 } 1818 } 1819 1820 for (celli = 0; celli < patch->totalDofsPerCell; celli++) { 1821 const PetscInt row = dofsArray[(offset + cell1)*patch->totalDofsPerCell + celli]; 1822 if (row < 0) continue; 1823 for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) { 1824 const PetscInt col = dofsArray[(offset + cell0)*patch->totalDofsPerCell + cellj]; 1825 const PetscInt key = row*rsize + col; 1826 if (col < 0) continue; 1827 if (!PetscBTLookupSet(bt, key)) ++dnnz[row]; 1828 } 1829 } 1830 } 1831 } 1832 CHKERRQ(PetscBTDestroy(&bt)); 1833 CHKERRQ(MatXAIJSetPreallocation(*mat, 1, dnnz, NULL, NULL, NULL)); 1834 CHKERRQ(PetscFree(dnnz)); 1835 1836 CHKERRQ(PetscCalloc1(patch->totalDofsPerCell*patch->totalDofsPerCell, &zeroes)); 1837 for (c = 0; c < ncell; ++c) { 1838 const PetscInt *idx = &dofsArray[(offset + c)*patch->totalDofsPerCell]; 1839 CHKERRQ(MatSetValues(*mat, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, zeroes, INSERT_VALUES)); 1840 } 1841 CHKERRQ(MatGetLocalSize(*mat, &rows, NULL)); 1842 for (i = 0; i < rows; ++i) { 1843 CHKERRQ(MatSetValues(*mat, 1, &i, 1, &i, zeroes, INSERT_VALUES)); 1844 } 1845 1846 if (patch->usercomputeopintfacet) { 1847 const PetscInt *intFacetsArray = NULL; 1848 PetscInt i, numIntFacets, intFacetOffset; 1849 const PetscInt *facetCells = NULL; 1850 1851 CHKERRQ(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets)); 1852 CHKERRQ(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset)); 1853 CHKERRQ(ISGetIndices(patch->intFacetsToPatchCell, &facetCells)); 1854 CHKERRQ(ISGetIndices(patch->intFacets, &intFacetsArray)); 1855 for (i = 0; i < numIntFacets; i++) { 1856 const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0]; 1857 const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1]; 1858 const PetscInt *cell0idx = &dofsArray[(offset + cell0)*patch->totalDofsPerCell]; 1859 const PetscInt *cell1idx = &dofsArray[(offset + cell1)*patch->totalDofsPerCell]; 1860 CHKERRQ(MatSetValues(*mat, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, zeroes, INSERT_VALUES)); 1861 CHKERRQ(MatSetValues(*mat, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, zeroes, INSERT_VALUES)); 1862 } 1863 } 1864 1865 CHKERRQ(MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY)); 1866 CHKERRQ(MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY)); 1867 1868 CHKERRQ(PetscFree(zeroes)); 1869 1870 } else { /* rsize too big, use MATPREALLOCATOR */ 1871 Mat preallocator; 1872 PetscScalar* vals; 1873 1874 CHKERRQ(PetscCalloc1(patch->totalDofsPerCell*patch->totalDofsPerCell, &vals)); 1875 CHKERRQ(MatCreate(PETSC_COMM_SELF, &preallocator)); 1876 CHKERRQ(MatSetType(preallocator, MATPREALLOCATOR)); 1877 CHKERRQ(MatSetSizes(preallocator, rsize, rsize, rsize, rsize)); 1878 CHKERRQ(MatSetUp(preallocator)); 1879 1880 for (c = 0; c < ncell; ++c) { 1881 const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell; 1882 CHKERRQ(MatSetValues(preallocator, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, vals, INSERT_VALUES)); 1883 } 1884 1885 if (patch->usercomputeopintfacet) { 1886 const PetscInt *intFacetsArray = NULL; 1887 PetscInt i, numIntFacets, intFacetOffset; 1888 const PetscInt *facetCells = NULL; 1889 1890 CHKERRQ(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets)); 1891 CHKERRQ(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset)); 1892 CHKERRQ(ISGetIndices(patch->intFacetsToPatchCell, &facetCells)); 1893 CHKERRQ(ISGetIndices(patch->intFacets, &intFacetsArray)); 1894 for (i = 0; i < numIntFacets; i++) { 1895 const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0]; 1896 const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1]; 1897 const PetscInt *cell0idx = &dofsArray[(offset + cell0)*patch->totalDofsPerCell]; 1898 const PetscInt *cell1idx = &dofsArray[(offset + cell1)*patch->totalDofsPerCell]; 1899 CHKERRQ(MatSetValues(preallocator, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, vals, INSERT_VALUES)); 1900 CHKERRQ(MatSetValues(preallocator, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, vals, INSERT_VALUES)); 1901 } 1902 } 1903 1904 CHKERRQ(PetscFree(vals)); 1905 CHKERRQ(MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY)); 1906 CHKERRQ(MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY)); 1907 CHKERRQ(MatPreallocatorPreallocate(preallocator, PETSC_TRUE, *mat)); 1908 CHKERRQ(MatDestroy(&preallocator)); 1909 } 1910 CHKERRQ(PetscLogEventEnd(PC_Patch_Prealloc, pc, 0, 0, 0)); 1911 if (withArtificial) { 1912 CHKERRQ(ISRestoreIndices(patch->dofsWithArtificial, &dofsArray)); 1913 } else { 1914 CHKERRQ(ISRestoreIndices(patch->dofs, &dofsArray)); 1915 } 1916 } 1917 CHKERRQ(MatSetUp(*mat)); 1918 PetscFunctionReturn(0); 1919 } 1920 1921 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) 1922 { 1923 PC_PATCH *patch = (PC_PATCH *) pc->data; 1924 DM dm, plex; 1925 PetscSection s; 1926 const PetscInt *parray, *oarray; 1927 PetscInt Nf = patch->nsubspaces, Np, poff, p, f; 1928 1929 PetscFunctionBegin; 1930 PetscCheck(!patch->precomputeElementTensors,PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Precomputing element tensors not implemented with DMPlex compute operator"); 1931 CHKERRQ(PCGetDM(pc, &dm)); 1932 CHKERRQ(DMConvert(dm, DMPLEX, &plex)); 1933 dm = plex; 1934 CHKERRQ(DMGetLocalSection(dm, &s)); 1935 /* Set offset into patch */ 1936 CHKERRQ(PetscSectionGetDof(patch->pointCounts, patchNum, &Np)); 1937 CHKERRQ(PetscSectionGetOffset(patch->pointCounts, patchNum, &poff)); 1938 CHKERRQ(ISGetIndices(patch->points, &parray)); 1939 CHKERRQ(ISGetIndices(patch->offs, &oarray)); 1940 for (f = 0; f < Nf; ++f) { 1941 for (p = 0; p < Np; ++p) { 1942 const PetscInt point = parray[poff+p]; 1943 PetscInt dof; 1944 1945 CHKERRQ(PetscSectionGetFieldDof(patch->patchSection, point, f, &dof)); 1946 CHKERRQ(PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f])); 1947 if (patch->nsubspaces == 1) CHKERRQ(PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f])); 1948 else CHKERRQ(PetscSectionSetOffset(patch->patchSection, point, -1)); 1949 } 1950 } 1951 CHKERRQ(ISRestoreIndices(patch->points, &parray)); 1952 CHKERRQ(ISRestoreIndices(patch->offs, &oarray)); 1953 if (patch->viewSection) CHKERRQ(ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection)); 1954 CHKERRQ(DMPlexComputeResidual_Patch_Internal(dm, patch->patchSection, cellIS, 0.0, x, NULL, F, ctx)); 1955 CHKERRQ(DMDestroy(&dm)); 1956 PetscFunctionReturn(0); 1957 } 1958 1959 PetscErrorCode PCPatchComputeFunction_Internal(PC pc, Vec x, Vec F, PetscInt point) 1960 { 1961 PC_PATCH *patch = (PC_PATCH *) pc->data; 1962 const PetscInt *dofsArray; 1963 const PetscInt *dofsArrayWithAll; 1964 const PetscInt *cellsArray; 1965 PetscInt ncell, offset, pStart, pEnd; 1966 PetscErrorCode ierr; 1967 1968 PetscFunctionBegin; 1969 CHKERRQ(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0)); 1970 PetscCheck(patch->usercomputeop,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback"); 1971 CHKERRQ(ISGetIndices(patch->dofs, &dofsArray)); 1972 CHKERRQ(ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll)); 1973 CHKERRQ(ISGetIndices(patch->cells, &cellsArray)); 1974 CHKERRQ(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd)); 1975 1976 point += pStart; 1977 PetscCheckFalse(point >= pEnd,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %D not in [%D, %D)", point, pStart, pEnd); 1978 1979 CHKERRQ(PetscSectionGetDof(patch->cellCounts, point, &ncell)); 1980 CHKERRQ(PetscSectionGetOffset(patch->cellCounts, point, &offset)); 1981 if (ncell <= 0) { 1982 CHKERRQ(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 1983 PetscFunctionReturn(0); 1984 } 1985 CHKERRQ(VecSet(F, 0.0)); 1986 PetscStackPush("PCPatch user callback"); 1987 /* Cannot reuse the same IS because the geometry info is being cached in it */ 1988 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS)); 1989 ierr = patch->usercomputef(pc, point, x, F, patch->cellIS, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell, 1990 dofsArrayWithAll + offset*patch->totalDofsPerCell, 1991 patch->usercomputefctx);CHKERRQ(ierr); 1992 PetscStackPop; 1993 CHKERRQ(ISDestroy(&patch->cellIS)); 1994 CHKERRQ(ISRestoreIndices(patch->dofs, &dofsArray)); 1995 CHKERRQ(ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll)); 1996 CHKERRQ(ISRestoreIndices(patch->cells, &cellsArray)); 1997 if (patch->viewMatrix) { 1998 char name[PETSC_MAX_PATH_LEN]; 1999 2000 CHKERRQ(PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch vector for Point %D", point)); 2001 CHKERRQ(PetscObjectSetName((PetscObject) F, name)); 2002 CHKERRQ(ObjectView((PetscObject) F, patch->viewerMatrix, patch->formatMatrix)); 2003 } 2004 CHKERRQ(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 2005 PetscFunctionReturn(0); 2006 } 2007 2008 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) 2009 { 2010 PC_PATCH *patch = (PC_PATCH *) pc->data; 2011 DM dm, plex; 2012 PetscSection s; 2013 const PetscInt *parray, *oarray; 2014 PetscInt Nf = patch->nsubspaces, Np, poff, p, f; 2015 2016 PetscFunctionBegin; 2017 CHKERRQ(PCGetDM(pc, &dm)); 2018 CHKERRQ(DMConvert(dm, DMPLEX, &plex)); 2019 dm = plex; 2020 CHKERRQ(DMGetLocalSection(dm, &s)); 2021 /* Set offset into patch */ 2022 CHKERRQ(PetscSectionGetDof(patch->pointCounts, patchNum, &Np)); 2023 CHKERRQ(PetscSectionGetOffset(patch->pointCounts, patchNum, &poff)); 2024 CHKERRQ(ISGetIndices(patch->points, &parray)); 2025 CHKERRQ(ISGetIndices(patch->offs, &oarray)); 2026 for (f = 0; f < Nf; ++f) { 2027 for (p = 0; p < Np; ++p) { 2028 const PetscInt point = parray[poff+p]; 2029 PetscInt dof; 2030 2031 CHKERRQ(PetscSectionGetFieldDof(patch->patchSection, point, f, &dof)); 2032 CHKERRQ(PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f])); 2033 if (patch->nsubspaces == 1) CHKERRQ(PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f])); 2034 else CHKERRQ(PetscSectionSetOffset(patch->patchSection, point, -1)); 2035 } 2036 } 2037 CHKERRQ(ISRestoreIndices(patch->points, &parray)); 2038 CHKERRQ(ISRestoreIndices(patch->offs, &oarray)); 2039 if (patch->viewSection) CHKERRQ(ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection)); 2040 /* TODO Shut off MatViewFromOptions() in MatAssemblyEnd() here */ 2041 CHKERRQ(DMPlexComputeJacobian_Patch_Internal(dm, patch->patchSection, patch->patchSection, cellIS, 0.0, 0.0, x, NULL, J, J, ctx)); 2042 CHKERRQ(DMDestroy(&dm)); 2043 PetscFunctionReturn(0); 2044 } 2045 2046 /* This function zeros mat on entry */ 2047 PetscErrorCode PCPatchComputeOperator_Internal(PC pc, Vec x, Mat mat, PetscInt point, PetscBool withArtificial) 2048 { 2049 PC_PATCH *patch = (PC_PATCH *) pc->data; 2050 const PetscInt *dofsArray; 2051 const PetscInt *dofsArrayWithAll = NULL; 2052 const PetscInt *cellsArray; 2053 PetscInt ncell, offset, pStart, pEnd, numIntFacets, intFacetOffset; 2054 PetscBool isNonlinear; 2055 2056 PetscFunctionBegin; 2057 CHKERRQ(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0)); 2058 isNonlinear = patch->isNonlinear; 2059 PetscCheck(patch->usercomputeop,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback"); 2060 if (withArtificial) { 2061 CHKERRQ(ISGetIndices(patch->dofsWithArtificial, &dofsArray)); 2062 } else { 2063 CHKERRQ(ISGetIndices(patch->dofs, &dofsArray)); 2064 } 2065 if (isNonlinear) { 2066 CHKERRQ(ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll)); 2067 } 2068 CHKERRQ(ISGetIndices(patch->cells, &cellsArray)); 2069 CHKERRQ(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd)); 2070 2071 point += pStart; 2072 PetscCheckFalse(point >= pEnd,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %D not in [%D, %D)", point, pStart, pEnd); 2073 2074 CHKERRQ(PetscSectionGetDof(patch->cellCounts, point, &ncell)); 2075 CHKERRQ(PetscSectionGetOffset(patch->cellCounts, point, &offset)); 2076 if (ncell <= 0) { 2077 CHKERRQ(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 2078 PetscFunctionReturn(0); 2079 } 2080 CHKERRQ(MatZeroEntries(mat)); 2081 if (patch->precomputeElementTensors) { 2082 PetscInt i; 2083 PetscInt ndof = patch->totalDofsPerCell; 2084 const PetscScalar *elementTensors; 2085 2086 CHKERRQ(VecGetArrayRead(patch->cellMats, &elementTensors)); 2087 for (i = 0; i < ncell; i++) { 2088 const PetscInt cell = cellsArray[i + offset]; 2089 const PetscInt *idx = dofsArray + (offset + i)*ndof; 2090 const PetscScalar *v = elementTensors + patch->precomputedTensorLocations[cell]*ndof*ndof; 2091 CHKERRQ(MatSetValues(mat, ndof, idx, ndof, idx, v, ADD_VALUES)); 2092 } 2093 CHKERRQ(VecRestoreArrayRead(patch->cellMats, &elementTensors)); 2094 CHKERRQ(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY)); 2095 CHKERRQ(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY)); 2096 } else { 2097 PetscStackPush("PCPatch user callback"); 2098 /* Cannot reuse the same IS because the geometry info is being cached in it */ 2099 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS)); 2100 CHKERRQ(patch->usercomputeop(pc, point, x, mat, patch->cellIS, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell, dofsArrayWithAll ? dofsArrayWithAll + offset*patch->totalDofsPerCell : NULL, patch->usercomputeopctx)); 2101 } 2102 if (patch->usercomputeopintfacet) { 2103 CHKERRQ(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets)); 2104 CHKERRQ(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset)); 2105 if (numIntFacets > 0) { 2106 /* For each interior facet, grab the two cells (in local numbering, and concatenate dof numberings for those cells) */ 2107 PetscInt *facetDofs = NULL, *facetDofsWithAll = NULL; 2108 const PetscInt *intFacetsArray = NULL; 2109 PetscInt idx = 0; 2110 PetscInt i, c, d; 2111 PetscInt fStart; 2112 DM dm, plex; 2113 IS facetIS = NULL; 2114 const PetscInt *facetCells = NULL; 2115 2116 CHKERRQ(ISGetIndices(patch->intFacetsToPatchCell, &facetCells)); 2117 CHKERRQ(ISGetIndices(patch->intFacets, &intFacetsArray)); 2118 CHKERRQ(PCGetDM(pc, &dm)); 2119 CHKERRQ(DMConvert(dm, DMPLEX, &plex)); 2120 dm = plex; 2121 CHKERRQ(DMPlexGetHeightStratum(dm, 1, &fStart, NULL)); 2122 /* FIXME: Pull this malloc out. */ 2123 CHKERRQ(PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofs)); 2124 if (dofsArrayWithAll) { 2125 CHKERRQ(PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofsWithAll)); 2126 } 2127 if (patch->precomputeElementTensors) { 2128 PetscInt nFacetDof = 2*patch->totalDofsPerCell; 2129 const PetscScalar *elementTensors; 2130 2131 CHKERRQ(VecGetArrayRead(patch->intFacetMats, &elementTensors)); 2132 2133 for (i = 0; i < numIntFacets; i++) { 2134 const PetscInt facet = intFacetsArray[i + intFacetOffset]; 2135 const PetscScalar *v = elementTensors + patch->precomputedIntFacetTensorLocations[facet - fStart]*nFacetDof*nFacetDof; 2136 idx = 0; 2137 /* 2138 * 0--1 2139 * |\-| 2140 * |+\| 2141 * 2--3 2142 * [0, 2, 3, 0, 1, 3] 2143 */ 2144 for (c = 0; c < 2; c++) { 2145 const PetscInt cell = facetCells[2*(intFacetOffset + i) + c]; 2146 for (d = 0; d < patch->totalDofsPerCell; d++) { 2147 facetDofs[idx] = dofsArray[(offset + cell)*patch->totalDofsPerCell + d]; 2148 idx++; 2149 } 2150 } 2151 CHKERRQ(MatSetValues(mat, nFacetDof, facetDofs, nFacetDof, facetDofs, v, ADD_VALUES)); 2152 } 2153 CHKERRQ(VecRestoreArrayRead(patch->intFacetMats, &elementTensors)); 2154 } else { 2155 /* 2156 * 0--1 2157 * |\-| 2158 * |+\| 2159 * 2--3 2160 * [0, 2, 3, 0, 1, 3] 2161 */ 2162 for (i = 0; i < numIntFacets; i++) { 2163 for (c = 0; c < 2; c++) { 2164 const PetscInt cell = facetCells[2*(intFacetOffset + i) + c]; 2165 for (d = 0; d < patch->totalDofsPerCell; d++) { 2166 facetDofs[idx] = dofsArray[(offset + cell)*patch->totalDofsPerCell + d]; 2167 if (dofsArrayWithAll) { 2168 facetDofsWithAll[idx] = dofsArrayWithAll[(offset + cell)*patch->totalDofsPerCell + d]; 2169 } 2170 idx++; 2171 } 2172 } 2173 } 2174 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray + intFacetOffset, PETSC_USE_POINTER, &facetIS)); 2175 CHKERRQ(patch->usercomputeopintfacet(pc, point, x, mat, facetIS, 2*numIntFacets*patch->totalDofsPerCell, facetDofs, facetDofsWithAll, patch->usercomputeopintfacetctx)); 2176 CHKERRQ(ISDestroy(&facetIS)); 2177 } 2178 CHKERRQ(ISRestoreIndices(patch->intFacetsToPatchCell, &facetCells)); 2179 CHKERRQ(ISRestoreIndices(patch->intFacets, &intFacetsArray)); 2180 CHKERRQ(PetscFree(facetDofs)); 2181 CHKERRQ(PetscFree(facetDofsWithAll)); 2182 CHKERRQ(DMDestroy(&dm)); 2183 } 2184 } 2185 2186 CHKERRQ(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY)); 2187 CHKERRQ(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY)); 2188 2189 if (!(withArtificial || isNonlinear) && patch->denseinverse) { 2190 MatFactorInfo info; 2191 PetscBool flg; 2192 CHKERRQ(PetscObjectTypeCompare((PetscObject)mat, MATSEQDENSE, &flg)); 2193 PetscCheck(flg,PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Invalid Mat type for dense inverse"); 2194 CHKERRQ(MatFactorInfoInitialize(&info)); 2195 CHKERRQ(MatLUFactor(mat, NULL, NULL, &info)); 2196 CHKERRQ(MatSeqDenseInvertFactors_Private(mat)); 2197 } 2198 PetscStackPop; 2199 CHKERRQ(ISDestroy(&patch->cellIS)); 2200 if (withArtificial) { 2201 CHKERRQ(ISRestoreIndices(patch->dofsWithArtificial, &dofsArray)); 2202 } else { 2203 CHKERRQ(ISRestoreIndices(patch->dofs, &dofsArray)); 2204 } 2205 if (isNonlinear) { 2206 CHKERRQ(ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll)); 2207 } 2208 CHKERRQ(ISRestoreIndices(patch->cells, &cellsArray)); 2209 if (patch->viewMatrix) { 2210 char name[PETSC_MAX_PATH_LEN]; 2211 2212 CHKERRQ(PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch matrix for Point %D", point)); 2213 CHKERRQ(PetscObjectSetName((PetscObject) mat, name)); 2214 CHKERRQ(ObjectView((PetscObject) mat, patch->viewerMatrix, patch->formatMatrix)); 2215 } 2216 CHKERRQ(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 2217 PetscFunctionReturn(0); 2218 } 2219 2220 static PetscErrorCode MatSetValues_PCPatch_Private(Mat mat, PetscInt m, const PetscInt idxm[], 2221 PetscInt n, const PetscInt idxn[], const PetscScalar *v, InsertMode addv) 2222 { 2223 Vec data; 2224 PetscScalar *array; 2225 PetscInt bs, nz, i, j, cell; 2226 2227 CHKERRQ(MatShellGetContext(mat, &data)); 2228 CHKERRQ(VecGetBlockSize(data, &bs)); 2229 CHKERRQ(VecGetSize(data, &nz)); 2230 CHKERRQ(VecGetArray(data, &array)); 2231 PetscCheckFalse(m != n,PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Only for square insertion"); 2232 cell = (PetscInt)(idxm[0]/bs); /* use the fact that this is called once per cell */ 2233 for (i = 0; i < m; i++) { 2234 PetscCheckFalse(idxm[i] != idxn[i],PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Row and column indices must match!"); 2235 for (j = 0; j < n; j++) { 2236 const PetscScalar v_ = v[i*bs + j]; 2237 /* Indexing is special to the data structure we have! */ 2238 if (addv == INSERT_VALUES) { 2239 array[cell*bs*bs + i*bs + j] = v_; 2240 } else { 2241 array[cell*bs*bs + i*bs + j] += v_; 2242 } 2243 } 2244 } 2245 CHKERRQ(VecRestoreArray(data, &array)); 2246 PetscFunctionReturn(0); 2247 } 2248 2249 static PetscErrorCode PCPatchPrecomputePatchTensors_Private(PC pc) 2250 { 2251 PC_PATCH *patch = (PC_PATCH *)pc->data; 2252 const PetscInt *cellsArray; 2253 PetscInt ncell, offset; 2254 const PetscInt *dofMapArray; 2255 PetscInt i, j; 2256 IS dofMap; 2257 IS cellIS; 2258 const PetscInt ndof = patch->totalDofsPerCell; 2259 PetscErrorCode ierr; 2260 Mat vecMat; 2261 PetscInt cStart, cEnd; 2262 DM dm, plex; 2263 2264 CHKERRQ(ISGetSize(patch->cells, &ncell)); 2265 if (!ncell) { /* No cells to assemble over -> skip */ 2266 PetscFunctionReturn(0); 2267 } 2268 2269 CHKERRQ(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0)); 2270 2271 CHKERRQ(PCGetDM(pc, &dm)); 2272 CHKERRQ(DMConvert(dm, DMPLEX, &plex)); 2273 dm = plex; 2274 if (!patch->allCells) { 2275 PetscHSetI cells; 2276 PetscHashIter hi; 2277 PetscInt pStart, pEnd; 2278 PetscInt *allCells = NULL; 2279 CHKERRQ(PetscHSetICreate(&cells)); 2280 CHKERRQ(ISGetIndices(patch->cells, &cellsArray)); 2281 CHKERRQ(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd)); 2282 for (i = pStart; i < pEnd; i++) { 2283 CHKERRQ(PetscSectionGetDof(patch->cellCounts, i, &ncell)); 2284 CHKERRQ(PetscSectionGetOffset(patch->cellCounts, i, &offset)); 2285 if (ncell <= 0) continue; 2286 for (j = 0; j < ncell; j++) { 2287 CHKERRQ(PetscHSetIAdd(cells, cellsArray[offset + j])); 2288 } 2289 } 2290 CHKERRQ(ISRestoreIndices(patch->cells, &cellsArray)); 2291 CHKERRQ(PetscHSetIGetSize(cells, &ncell)); 2292 CHKERRQ(PetscMalloc1(ncell, &allCells)); 2293 CHKERRQ(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 2294 CHKERRQ(PetscMalloc1(cEnd-cStart, &patch->precomputedTensorLocations)); 2295 i = 0; 2296 PetscHashIterBegin(cells, hi); 2297 while (!PetscHashIterAtEnd(cells, hi)) { 2298 PetscHashIterGetKey(cells, hi, allCells[i]); 2299 patch->precomputedTensorLocations[allCells[i]] = i; 2300 PetscHashIterNext(cells, hi); 2301 i++; 2302 } 2303 CHKERRQ(PetscHSetIDestroy(&cells)); 2304 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, ncell, allCells, PETSC_OWN_POINTER, &patch->allCells)); 2305 } 2306 CHKERRQ(ISGetSize(patch->allCells, &ncell)); 2307 if (!patch->cellMats) { 2308 CHKERRQ(VecCreateSeq(PETSC_COMM_SELF, ncell*ndof*ndof, &patch->cellMats)); 2309 CHKERRQ(VecSetBlockSize(patch->cellMats, ndof)); 2310 } 2311 CHKERRQ(VecSet(patch->cellMats, 0)); 2312 2313 ierr = MatCreateShell(PETSC_COMM_SELF, ncell*ndof, ncell*ndof, ncell*ndof, ncell*ndof, 2314 (void*)patch->cellMats, &vecMat);CHKERRQ(ierr); 2315 CHKERRQ(MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void(*)(void))&MatSetValues_PCPatch_Private)); 2316 CHKERRQ(ISGetSize(patch->allCells, &ncell)); 2317 CHKERRQ(ISCreateStride(PETSC_COMM_SELF, ndof*ncell, 0, 1, &dofMap)); 2318 CHKERRQ(ISGetIndices(dofMap, &dofMapArray)); 2319 CHKERRQ(ISGetIndices(patch->allCells, &cellsArray)); 2320 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray, PETSC_USE_POINTER, &cellIS)); 2321 PetscStackPush("PCPatch user callback"); 2322 /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */ 2323 CHKERRQ(patch->usercomputeop(pc, -1, NULL, vecMat, cellIS, ndof*ncell, dofMapArray, NULL, patch->usercomputeopctx)); 2324 PetscStackPop; 2325 CHKERRQ(ISDestroy(&cellIS)); 2326 CHKERRQ(MatDestroy(&vecMat)); 2327 CHKERRQ(ISRestoreIndices(patch->allCells, &cellsArray)); 2328 CHKERRQ(ISRestoreIndices(dofMap, &dofMapArray)); 2329 CHKERRQ(ISDestroy(&dofMap)); 2330 2331 if (patch->usercomputeopintfacet) { 2332 PetscInt nIntFacets; 2333 IS intFacetsIS; 2334 const PetscInt *intFacetsArray = NULL; 2335 if (!patch->allIntFacets) { 2336 PetscHSetI facets; 2337 PetscHashIter hi; 2338 PetscInt pStart, pEnd, fStart, fEnd; 2339 PetscInt *allIntFacets = NULL; 2340 CHKERRQ(PetscHSetICreate(&facets)); 2341 CHKERRQ(ISGetIndices(patch->intFacets, &intFacetsArray)); 2342 CHKERRQ(PetscSectionGetChart(patch->intFacetCounts, &pStart, &pEnd)); 2343 CHKERRQ(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd)); 2344 for (i = pStart; i < pEnd; i++) { 2345 CHKERRQ(PetscSectionGetDof(patch->intFacetCounts, i, &nIntFacets)); 2346 CHKERRQ(PetscSectionGetOffset(patch->intFacetCounts, i, &offset)); 2347 if (nIntFacets <= 0) continue; 2348 for (j = 0; j < nIntFacets; j++) { 2349 CHKERRQ(PetscHSetIAdd(facets, intFacetsArray[offset + j])); 2350 } 2351 } 2352 CHKERRQ(ISRestoreIndices(patch->intFacets, &intFacetsArray)); 2353 CHKERRQ(PetscHSetIGetSize(facets, &nIntFacets)); 2354 CHKERRQ(PetscMalloc1(nIntFacets, &allIntFacets)); 2355 CHKERRQ(PetscMalloc1(fEnd-fStart, &patch->precomputedIntFacetTensorLocations)); 2356 i = 0; 2357 PetscHashIterBegin(facets, hi); 2358 while (!PetscHashIterAtEnd(facets, hi)) { 2359 PetscHashIterGetKey(facets, hi, allIntFacets[i]); 2360 patch->precomputedIntFacetTensorLocations[allIntFacets[i] - fStart] = i; 2361 PetscHashIterNext(facets, hi); 2362 i++; 2363 } 2364 CHKERRQ(PetscHSetIDestroy(&facets)); 2365 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, allIntFacets, PETSC_OWN_POINTER, &patch->allIntFacets)); 2366 } 2367 CHKERRQ(ISGetSize(patch->allIntFacets, &nIntFacets)); 2368 if (!patch->intFacetMats) { 2369 CHKERRQ(VecCreateSeq(PETSC_COMM_SELF, nIntFacets*ndof*ndof*4, &patch->intFacetMats)); 2370 CHKERRQ(VecSetBlockSize(patch->intFacetMats, ndof*2)); 2371 } 2372 CHKERRQ(VecSet(patch->intFacetMats, 0)); 2373 2374 ierr = MatCreateShell(PETSC_COMM_SELF, nIntFacets*ndof*2, nIntFacets*ndof*2, nIntFacets*ndof*2, nIntFacets*ndof*2, 2375 (void*)patch->intFacetMats, &vecMat);CHKERRQ(ierr); 2376 CHKERRQ(MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void(*)(void))&MatSetValues_PCPatch_Private)); 2377 CHKERRQ(ISCreateStride(PETSC_COMM_SELF, 2*ndof*nIntFacets, 0, 1, &dofMap)); 2378 CHKERRQ(ISGetIndices(dofMap, &dofMapArray)); 2379 CHKERRQ(ISGetIndices(patch->allIntFacets, &intFacetsArray)); 2380 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, intFacetsArray, PETSC_USE_POINTER, &intFacetsIS)); 2381 PetscStackPush("PCPatch user callback (interior facets)"); 2382 /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */ 2383 CHKERRQ(patch->usercomputeopintfacet(pc, -1, NULL, vecMat, intFacetsIS, 2*ndof*nIntFacets, dofMapArray, NULL, patch->usercomputeopintfacetctx)); 2384 PetscStackPop; 2385 CHKERRQ(ISDestroy(&intFacetsIS)); 2386 CHKERRQ(MatDestroy(&vecMat)); 2387 CHKERRQ(ISRestoreIndices(patch->allIntFacets, &intFacetsArray)); 2388 CHKERRQ(ISRestoreIndices(dofMap, &dofMapArray)); 2389 CHKERRQ(ISDestroy(&dofMap)); 2390 } 2391 CHKERRQ(DMDestroy(&dm)); 2392 CHKERRQ(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 2393 2394 PetscFunctionReturn(0); 2395 } 2396 2397 PetscErrorCode PCPatch_ScatterLocal_Private(PC pc, PetscInt p, Vec x, Vec y, InsertMode mode, ScatterMode scat, PatchScatterType scattertype) 2398 { 2399 PC_PATCH *patch = (PC_PATCH *) pc->data; 2400 const PetscScalar *xArray = NULL; 2401 PetscScalar *yArray = NULL; 2402 const PetscInt *gtolArray = NULL; 2403 PetscInt dof, offset, lidx; 2404 2405 PetscFunctionBeginHot; 2406 CHKERRQ(VecGetArrayRead(x, &xArray)); 2407 CHKERRQ(VecGetArray(y, &yArray)); 2408 if (scattertype == SCATTER_WITHARTIFICIAL) { 2409 CHKERRQ(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof)); 2410 CHKERRQ(PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offset)); 2411 CHKERRQ(ISGetIndices(patch->gtolWithArtificial, >olArray)); 2412 } else if (scattertype == SCATTER_WITHALL) { 2413 CHKERRQ(PetscSectionGetDof(patch->gtolCountsWithAll, p, &dof)); 2414 CHKERRQ(PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offset)); 2415 CHKERRQ(ISGetIndices(patch->gtolWithAll, >olArray)); 2416 } else { 2417 CHKERRQ(PetscSectionGetDof(patch->gtolCounts, p, &dof)); 2418 CHKERRQ(PetscSectionGetOffset(patch->gtolCounts, p, &offset)); 2419 CHKERRQ(ISGetIndices(patch->gtol, >olArray)); 2420 } 2421 PetscCheckFalse(mode == INSERT_VALUES && scat != SCATTER_FORWARD,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't insert if not scattering forward"); 2422 PetscCheckFalse(mode == ADD_VALUES && scat != SCATTER_REVERSE,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't add if not scattering reverse"); 2423 for (lidx = 0; lidx < dof; ++lidx) { 2424 const PetscInt gidx = gtolArray[offset+lidx]; 2425 2426 if (mode == INSERT_VALUES) yArray[lidx] = xArray[gidx]; /* Forward */ 2427 else yArray[gidx] += xArray[lidx]; /* Reverse */ 2428 } 2429 if (scattertype == SCATTER_WITHARTIFICIAL) { 2430 CHKERRQ(ISRestoreIndices(patch->gtolWithArtificial, >olArray)); 2431 } else if (scattertype == SCATTER_WITHALL) { 2432 CHKERRQ(ISRestoreIndices(patch->gtolWithAll, >olArray)); 2433 } else { 2434 CHKERRQ(ISRestoreIndices(patch->gtol, >olArray)); 2435 } 2436 CHKERRQ(VecRestoreArrayRead(x, &xArray)); 2437 CHKERRQ(VecRestoreArray(y, &yArray)); 2438 PetscFunctionReturn(0); 2439 } 2440 2441 static PetscErrorCode PCSetUp_PATCH_Linear(PC pc) 2442 { 2443 PC_PATCH *patch = (PC_PATCH *) pc->data; 2444 const char *prefix; 2445 PetscInt i; 2446 2447 PetscFunctionBegin; 2448 if (!pc->setupcalled) { 2449 PetscCheckFalse(!patch->save_operators && patch->denseinverse,PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Can't have dense inverse without save operators"); 2450 if (!patch->denseinverse) { 2451 CHKERRQ(PetscMalloc1(patch->npatch, &patch->solver)); 2452 CHKERRQ(PCGetOptionsPrefix(pc, &prefix)); 2453 for (i = 0; i < patch->npatch; ++i) { 2454 KSP ksp; 2455 PC subpc; 2456 2457 CHKERRQ(KSPCreate(PETSC_COMM_SELF, &ksp)); 2458 CHKERRQ(KSPSetErrorIfNotConverged(ksp, pc->erroriffailure)); 2459 CHKERRQ(KSPSetOptionsPrefix(ksp, prefix)); 2460 CHKERRQ(KSPAppendOptionsPrefix(ksp, "sub_")); 2461 CHKERRQ(PetscObjectIncrementTabLevel((PetscObject) ksp, (PetscObject) pc, 1)); 2462 CHKERRQ(KSPGetPC(ksp, &subpc)); 2463 CHKERRQ(PetscObjectIncrementTabLevel((PetscObject) subpc, (PetscObject) pc, 1)); 2464 CHKERRQ(PetscLogObjectParent((PetscObject) pc, (PetscObject) ksp)); 2465 patch->solver[i] = (PetscObject) ksp; 2466 } 2467 } 2468 } 2469 if (patch->save_operators) { 2470 if (patch->precomputeElementTensors) { 2471 CHKERRQ(PCPatchPrecomputePatchTensors_Private(pc)); 2472 } 2473 for (i = 0; i < patch->npatch; ++i) { 2474 CHKERRQ(PCPatchComputeOperator_Internal(pc, NULL, patch->mat[i], i, PETSC_FALSE)); 2475 if (!patch->denseinverse) { 2476 CHKERRQ(KSPSetOperators((KSP) patch->solver[i], patch->mat[i], patch->mat[i])); 2477 } else if (patch->mat[i] && !patch->densesolve) { 2478 /* Setup matmult callback */ 2479 CHKERRQ(MatGetOperation(patch->mat[i], MATOP_MULT, (void (**)(void))&patch->densesolve)); 2480 } 2481 } 2482 } 2483 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 2484 for (i = 0; i < patch->npatch; ++i) { 2485 /* Instead of padding patch->patchUpdate with zeros to get */ 2486 /* patch->patchUpdateWithArtificial and then multiplying with the matrix, */ 2487 /* just get rid of the columns that correspond to the dofs with */ 2488 /* artificial bcs. That's of course fairly inefficient, hopefully we */ 2489 /* can just assemble the rectangular matrix in the first place. */ 2490 Mat matSquare; 2491 IS rowis; 2492 PetscInt dof; 2493 2494 CHKERRQ(MatGetSize(patch->mat[i], &dof, NULL)); 2495 if (dof == 0) { 2496 patch->matWithArtificial[i] = NULL; 2497 continue; 2498 } 2499 2500 CHKERRQ(PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE)); 2501 CHKERRQ(PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE)); 2502 2503 CHKERRQ(MatGetSize(matSquare, &dof, NULL)); 2504 CHKERRQ(ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis)); 2505 if (pc->setupcalled) { 2506 CHKERRQ(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_REUSE_MATRIX, &patch->matWithArtificial[i])); 2507 } else { 2508 CHKERRQ(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &patch->matWithArtificial[i])); 2509 } 2510 CHKERRQ(ISDestroy(&rowis)); 2511 CHKERRQ(MatDestroy(&matSquare)); 2512 } 2513 } 2514 PetscFunctionReturn(0); 2515 } 2516 2517 static PetscErrorCode PCSetUp_PATCH(PC pc) 2518 { 2519 PC_PATCH *patch = (PC_PATCH *) pc->data; 2520 PetscInt i; 2521 PetscBool isNonlinear; 2522 PetscInt maxDof = -1, maxDofWithArtificial = -1; 2523 2524 PetscFunctionBegin; 2525 if (!pc->setupcalled) { 2526 PetscInt pStart, pEnd, p; 2527 PetscInt localSize; 2528 2529 CHKERRQ(PetscLogEventBegin(PC_Patch_CreatePatches, pc, 0, 0, 0)); 2530 2531 isNonlinear = patch->isNonlinear; 2532 if (!patch->nsubspaces) { 2533 DM dm, plex; 2534 PetscSection s; 2535 PetscInt cStart, cEnd, c, Nf, f, numGlobalBcs = 0, *globalBcs, *Nb, **cellDofs; 2536 2537 CHKERRQ(PCGetDM(pc, &dm)); 2538 PetscCheck(dm,PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONG, "Must set DM for PCPATCH or call PCPatchSetDiscretisationInfo()"); 2539 CHKERRQ(DMConvert(dm, DMPLEX, &plex)); 2540 dm = plex; 2541 CHKERRQ(DMGetLocalSection(dm, &s)); 2542 CHKERRQ(PetscSectionGetNumFields(s, &Nf)); 2543 CHKERRQ(PetscSectionGetChart(s, &pStart, &pEnd)); 2544 for (p = pStart; p < pEnd; ++p) { 2545 PetscInt cdof; 2546 CHKERRQ(PetscSectionGetConstraintDof(s, p, &cdof)); 2547 numGlobalBcs += cdof; 2548 } 2549 CHKERRQ(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 2550 CHKERRQ(PetscMalloc3(Nf, &Nb, Nf, &cellDofs, numGlobalBcs, &globalBcs)); 2551 for (f = 0; f < Nf; ++f) { 2552 PetscFE fe; 2553 PetscDualSpace sp; 2554 PetscInt cdoff = 0; 2555 2556 CHKERRQ(DMGetField(dm, f, NULL, (PetscObject *) &fe)); 2557 /* CHKERRQ(PetscFEGetNumComponents(fe, &Nc[f])); */ 2558 CHKERRQ(PetscFEGetDualSpace(fe, &sp)); 2559 CHKERRQ(PetscDualSpaceGetDimension(sp, &Nb[f])); 2560 2561 CHKERRQ(PetscMalloc1((cEnd-cStart)*Nb[f], &cellDofs[f])); 2562 for (c = cStart; c < cEnd; ++c) { 2563 PetscInt *closure = NULL; 2564 PetscInt clSize = 0, cl; 2565 2566 CHKERRQ(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure)); 2567 for (cl = 0; cl < clSize*2; cl += 2) { 2568 const PetscInt p = closure[cl]; 2569 PetscInt fdof, d, foff; 2570 2571 CHKERRQ(PetscSectionGetFieldDof(s, p, f, &fdof)); 2572 CHKERRQ(PetscSectionGetFieldOffset(s, p, f, &foff)); 2573 for (d = 0; d < fdof; ++d, ++cdoff) cellDofs[f][cdoff] = foff + d; 2574 } 2575 CHKERRQ(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure)); 2576 } 2577 PetscCheckFalse(cdoff != (cEnd-cStart)*Nb[f],PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_SIZ, "Total number of cellDofs %D for field %D should be Nc (%D) * cellDof (%D)", cdoff, f, cEnd-cStart, Nb[f]); 2578 } 2579 numGlobalBcs = 0; 2580 for (p = pStart; p < pEnd; ++p) { 2581 const PetscInt *ind; 2582 PetscInt off, cdof, d; 2583 2584 CHKERRQ(PetscSectionGetOffset(s, p, &off)); 2585 CHKERRQ(PetscSectionGetConstraintDof(s, p, &cdof)); 2586 CHKERRQ(PetscSectionGetConstraintIndices(s, p, &ind)); 2587 for (d = 0; d < cdof; ++d) globalBcs[numGlobalBcs++] = off + ind[d]; 2588 } 2589 2590 CHKERRQ(PCPatchSetDiscretisationInfoCombined(pc, dm, Nb, (const PetscInt **) cellDofs, numGlobalBcs, globalBcs, numGlobalBcs, globalBcs)); 2591 for (f = 0; f < Nf; ++f) { 2592 CHKERRQ(PetscFree(cellDofs[f])); 2593 } 2594 CHKERRQ(PetscFree3(Nb, cellDofs, globalBcs)); 2595 CHKERRQ(PCPatchSetComputeFunction(pc, PCPatchComputeFunction_DMPlex_Private, NULL)); 2596 CHKERRQ(PCPatchSetComputeOperator(pc, PCPatchComputeOperator_DMPlex_Private, NULL)); 2597 CHKERRQ(DMDestroy(&dm)); 2598 } 2599 2600 localSize = patch->subspaceOffsets[patch->nsubspaces]; 2601 CHKERRQ(VecCreateSeq(PETSC_COMM_SELF, localSize, &patch->localRHS)); 2602 CHKERRQ(VecSetUp(patch->localRHS)); 2603 CHKERRQ(VecDuplicate(patch->localRHS, &patch->localUpdate)); 2604 CHKERRQ(PCPatchCreateCellPatches(pc)); 2605 CHKERRQ(PCPatchCreateCellPatchDiscretisationInfo(pc)); 2606 2607 /* OK, now build the work vectors */ 2608 CHKERRQ(PetscSectionGetChart(patch->gtolCounts, &pStart, &pEnd)); 2609 2610 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 2611 CHKERRQ(PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithArtificial)); 2612 } 2613 if (isNonlinear) { 2614 CHKERRQ(PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithAll)); 2615 } 2616 for (p = pStart; p < pEnd; ++p) { 2617 PetscInt dof; 2618 2619 CHKERRQ(PetscSectionGetDof(patch->gtolCounts, p, &dof)); 2620 maxDof = PetscMax(maxDof, dof); 2621 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 2622 const PetscInt *gtolArray, *gtolArrayWithArtificial = NULL; 2623 PetscInt numPatchDofs, offset; 2624 PetscInt numPatchDofsWithArtificial, offsetWithArtificial; 2625 PetscInt dofWithoutArtificialCounter = 0; 2626 PetscInt *patchWithoutArtificialToWithArtificialArray; 2627 2628 CHKERRQ(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof)); 2629 maxDofWithArtificial = PetscMax(maxDofWithArtificial, dof); 2630 2631 /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */ 2632 /* the index in the patch with all dofs */ 2633 CHKERRQ(ISGetIndices(patch->gtol, >olArray)); 2634 2635 CHKERRQ(PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs)); 2636 if (numPatchDofs == 0) { 2637 patch->dofMappingWithoutToWithArtificial[p-pStart] = NULL; 2638 continue; 2639 } 2640 2641 CHKERRQ(PetscSectionGetOffset(patch->gtolCounts, p, &offset)); 2642 CHKERRQ(ISGetIndices(patch->gtolWithArtificial, >olArrayWithArtificial)); 2643 CHKERRQ(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &numPatchDofsWithArtificial)); 2644 CHKERRQ(PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offsetWithArtificial)); 2645 2646 CHKERRQ(PetscMalloc1(numPatchDofs, &patchWithoutArtificialToWithArtificialArray)); 2647 for (i=0; i<numPatchDofsWithArtificial; i++) { 2648 if (gtolArrayWithArtificial[i+offsetWithArtificial] == gtolArray[offset+dofWithoutArtificialCounter]) { 2649 patchWithoutArtificialToWithArtificialArray[dofWithoutArtificialCounter] = i; 2650 dofWithoutArtificialCounter++; 2651 if (dofWithoutArtificialCounter == numPatchDofs) 2652 break; 2653 } 2654 } 2655 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutArtificialToWithArtificialArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithArtificial[p-pStart])); 2656 CHKERRQ(ISRestoreIndices(patch->gtol, >olArray)); 2657 CHKERRQ(ISRestoreIndices(patch->gtolWithArtificial, >olArrayWithArtificial)); 2658 } 2659 if (isNonlinear) { 2660 const PetscInt *gtolArray, *gtolArrayWithAll = NULL; 2661 PetscInt numPatchDofs, offset; 2662 PetscInt numPatchDofsWithAll, offsetWithAll; 2663 PetscInt dofWithoutAllCounter = 0; 2664 PetscInt *patchWithoutAllToWithAllArray; 2665 2666 /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */ 2667 /* the index in the patch with all dofs */ 2668 CHKERRQ(ISGetIndices(patch->gtol, >olArray)); 2669 2670 CHKERRQ(PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs)); 2671 if (numPatchDofs == 0) { 2672 patch->dofMappingWithoutToWithAll[p-pStart] = NULL; 2673 continue; 2674 } 2675 2676 CHKERRQ(PetscSectionGetOffset(patch->gtolCounts, p, &offset)); 2677 CHKERRQ(ISGetIndices(patch->gtolWithAll, >olArrayWithAll)); 2678 CHKERRQ(PetscSectionGetDof(patch->gtolCountsWithAll, p, &numPatchDofsWithAll)); 2679 CHKERRQ(PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offsetWithAll)); 2680 2681 CHKERRQ(PetscMalloc1(numPatchDofs, &patchWithoutAllToWithAllArray)); 2682 2683 for (i=0; i<numPatchDofsWithAll; i++) { 2684 if (gtolArrayWithAll[i+offsetWithAll] == gtolArray[offset+dofWithoutAllCounter]) { 2685 patchWithoutAllToWithAllArray[dofWithoutAllCounter] = i; 2686 dofWithoutAllCounter++; 2687 if (dofWithoutAllCounter == numPatchDofs) 2688 break; 2689 } 2690 } 2691 CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutAllToWithAllArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithAll[p-pStart])); 2692 CHKERRQ(ISRestoreIndices(patch->gtol, >olArray)); 2693 CHKERRQ(ISRestoreIndices(patch->gtolWithAll, >olArrayWithAll)); 2694 } 2695 } 2696 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 2697 CHKERRQ(VecCreateSeq(PETSC_COMM_SELF, maxDofWithArtificial, &patch->patchRHSWithArtificial)); 2698 CHKERRQ(VecSetUp(patch->patchRHSWithArtificial)); 2699 } 2700 CHKERRQ(VecCreateSeq(PETSC_COMM_SELF, maxDof, &patch->patchRHS)); 2701 CHKERRQ(VecSetUp(patch->patchRHS)); 2702 CHKERRQ(VecCreateSeq(PETSC_COMM_SELF, maxDof, &patch->patchUpdate)); 2703 CHKERRQ(VecSetUp(patch->patchUpdate)); 2704 if (patch->save_operators) { 2705 CHKERRQ(PetscMalloc1(patch->npatch, &patch->mat)); 2706 for (i = 0; i < patch->npatch; ++i) { 2707 CHKERRQ(PCPatchCreateMatrix_Private(pc, i, &patch->mat[i], PETSC_FALSE)); 2708 } 2709 } 2710 CHKERRQ(PetscLogEventEnd(PC_Patch_CreatePatches, pc, 0, 0, 0)); 2711 2712 /* If desired, calculate weights for dof multiplicity */ 2713 if (patch->partition_of_unity) { 2714 PetscScalar *input = NULL; 2715 PetscScalar *output = NULL; 2716 Vec global; 2717 2718 CHKERRQ(VecDuplicate(patch->localRHS, &patch->dof_weights)); 2719 if (patch->local_composition_type == PC_COMPOSITE_ADDITIVE) { 2720 for (i = 0; i < patch->npatch; ++i) { 2721 PetscInt dof; 2722 2723 CHKERRQ(PetscSectionGetDof(patch->gtolCounts, i+pStart, &dof)); 2724 if (dof <= 0) continue; 2725 CHKERRQ(VecSet(patch->patchRHS, 1.0)); 2726 CHKERRQ(PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchRHS, patch->dof_weights, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR)); 2727 } 2728 } else { 2729 /* multiplicative is actually only locally multiplicative and globally additive. need the pou where the mesh decomposition overlaps */ 2730 CHKERRQ(VecSet(patch->dof_weights, 1.0)); 2731 } 2732 2733 VecDuplicate(patch->dof_weights, &global); 2734 VecSet(global, 0.); 2735 2736 CHKERRQ(VecGetArray(patch->dof_weights, &input)); 2737 CHKERRQ(VecGetArray(global, &output)); 2738 CHKERRQ(PetscSFReduceBegin(patch->sectionSF, MPIU_SCALAR, input, output, MPI_SUM)); 2739 CHKERRQ(PetscSFReduceEnd(patch->sectionSF, MPIU_SCALAR, input, output, MPI_SUM)); 2740 CHKERRQ(VecRestoreArray(patch->dof_weights, &input)); 2741 CHKERRQ(VecRestoreArray(global, &output)); 2742 2743 CHKERRQ(VecReciprocal(global)); 2744 2745 CHKERRQ(VecGetArray(patch->dof_weights, &output)); 2746 CHKERRQ(VecGetArray(global, &input)); 2747 CHKERRQ(PetscSFBcastBegin(patch->sectionSF, MPIU_SCALAR, input, output,MPI_REPLACE)); 2748 CHKERRQ(PetscSFBcastEnd(patch->sectionSF, MPIU_SCALAR, input, output,MPI_REPLACE)); 2749 CHKERRQ(VecRestoreArray(patch->dof_weights, &output)); 2750 CHKERRQ(VecRestoreArray(global, &input)); 2751 CHKERRQ(VecDestroy(&global)); 2752 } 2753 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE && patch->save_operators) { 2754 CHKERRQ(PetscMalloc1(patch->npatch, &patch->matWithArtificial)); 2755 } 2756 } 2757 CHKERRQ((*patch->setupsolver)(pc)); 2758 PetscFunctionReturn(0); 2759 } 2760 2761 static PetscErrorCode PCApply_PATCH_Linear(PC pc, PetscInt i, Vec x, Vec y) 2762 { 2763 PC_PATCH *patch = (PC_PATCH *) pc->data; 2764 KSP ksp; 2765 Mat op; 2766 PetscInt m, n; 2767 2768 PetscFunctionBegin; 2769 if (patch->denseinverse) { 2770 CHKERRQ((*patch->densesolve)(patch->mat[i], x, y)); 2771 PetscFunctionReturn(0); 2772 } 2773 ksp = (KSP) patch->solver[i]; 2774 if (!patch->save_operators) { 2775 Mat mat; 2776 2777 CHKERRQ(PCPatchCreateMatrix_Private(pc, i, &mat, PETSC_FALSE)); 2778 /* Populate operator here. */ 2779 CHKERRQ(PCPatchComputeOperator_Internal(pc, NULL, mat, i, PETSC_FALSE)); 2780 CHKERRQ(KSPSetOperators(ksp, mat, mat)); 2781 /* Drop reference so the KSPSetOperators below will blow it away. */ 2782 CHKERRQ(MatDestroy(&mat)); 2783 } 2784 CHKERRQ(PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0)); 2785 if (!ksp->setfromoptionscalled) { 2786 CHKERRQ(KSPSetFromOptions(ksp)); 2787 } 2788 /* Disgusting trick to reuse work vectors */ 2789 CHKERRQ(KSPGetOperators(ksp, &op, NULL)); 2790 CHKERRQ(MatGetLocalSize(op, &m, &n)); 2791 x->map->n = m; 2792 y->map->n = n; 2793 x->map->N = m; 2794 y->map->N = n; 2795 CHKERRQ(KSPSolve(ksp, x, y)); 2796 CHKERRQ(KSPCheckSolve(ksp, pc, y)); 2797 CHKERRQ(PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0)); 2798 if (!patch->save_operators) { 2799 PC pc; 2800 CHKERRQ(KSPSetOperators(ksp, NULL, NULL)); 2801 CHKERRQ(KSPGetPC(ksp, &pc)); 2802 /* Destroy PC context too, otherwise the factored matrix hangs around. */ 2803 CHKERRQ(PCReset(pc)); 2804 } 2805 PetscFunctionReturn(0); 2806 } 2807 2808 static PetscErrorCode PCUpdateMultiplicative_PATCH_Linear(PC pc, PetscInt i, PetscInt pStart) 2809 { 2810 PC_PATCH *patch = (PC_PATCH *) pc->data; 2811 Mat multMat; 2812 PetscInt n, m; 2813 2814 PetscFunctionBegin; 2815 2816 if (patch->save_operators) { 2817 multMat = patch->matWithArtificial[i]; 2818 } else { 2819 /*Very inefficient, hopefully we can just assemble the rectangular matrix in the first place.*/ 2820 Mat matSquare; 2821 PetscInt dof; 2822 IS rowis; 2823 CHKERRQ(PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE)); 2824 CHKERRQ(PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE)); 2825 CHKERRQ(MatGetSize(matSquare, &dof, NULL)); 2826 CHKERRQ(ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis)); 2827 CHKERRQ(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &multMat)); 2828 CHKERRQ(MatDestroy(&matSquare)); 2829 CHKERRQ(ISDestroy(&rowis)); 2830 } 2831 /* Disgusting trick to reuse work vectors */ 2832 CHKERRQ(MatGetLocalSize(multMat, &m, &n)); 2833 patch->patchUpdate->map->n = n; 2834 patch->patchRHSWithArtificial->map->n = m; 2835 patch->patchUpdate->map->N = n; 2836 patch->patchRHSWithArtificial->map->N = m; 2837 CHKERRQ(MatMult(multMat, patch->patchUpdate, patch->patchRHSWithArtificial)); 2838 CHKERRQ(VecScale(patch->patchRHSWithArtificial, -1.0)); 2839 CHKERRQ(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHSWithArtificial, patch->localRHS, ADD_VALUES, SCATTER_REVERSE, SCATTER_WITHARTIFICIAL)); 2840 if (!patch->save_operators) { 2841 CHKERRQ(MatDestroy(&multMat)); 2842 } 2843 PetscFunctionReturn(0); 2844 } 2845 2846 static PetscErrorCode PCApply_PATCH(PC pc, Vec x, Vec y) 2847 { 2848 PC_PATCH *patch = (PC_PATCH *) pc->data; 2849 const PetscScalar *globalRHS = NULL; 2850 PetscScalar *localRHS = NULL; 2851 PetscScalar *globalUpdate = NULL; 2852 const PetscInt *bcNodes = NULL; 2853 PetscInt nsweep = patch->symmetrise_sweep ? 2 : 1; 2854 PetscInt start[2] = {0, 0}; 2855 PetscInt end[2] = {-1, -1}; 2856 const PetscInt inc[2] = {1, -1}; 2857 const PetscScalar *localUpdate; 2858 const PetscInt *iterationSet; 2859 PetscInt pStart, numBcs, n, sweep, bc, j; 2860 2861 PetscFunctionBegin; 2862 CHKERRQ(PetscLogEventBegin(PC_Patch_Apply, pc, 0, 0, 0)); 2863 CHKERRQ(PetscOptionsPushGetViewerOff(PETSC_TRUE)); 2864 /* start, end, inc have 2 entries to manage a second backward sweep if we symmetrize */ 2865 end[0] = patch->npatch; 2866 start[1] = patch->npatch-1; 2867 if (patch->user_patches) { 2868 CHKERRQ(ISGetLocalSize(patch->iterationSet, &end[0])); 2869 start[1] = end[0] - 1; 2870 CHKERRQ(ISGetIndices(patch->iterationSet, &iterationSet)); 2871 } 2872 /* Scatter from global space into overlapped local spaces */ 2873 CHKERRQ(VecGetArrayRead(x, &globalRHS)); 2874 CHKERRQ(VecGetArray(patch->localRHS, &localRHS)); 2875 CHKERRQ(PetscSFBcastBegin(patch->sectionSF, MPIU_SCALAR, globalRHS, localRHS,MPI_REPLACE)); 2876 CHKERRQ(PetscSFBcastEnd(patch->sectionSF, MPIU_SCALAR, globalRHS, localRHS,MPI_REPLACE)); 2877 CHKERRQ(VecRestoreArrayRead(x, &globalRHS)); 2878 CHKERRQ(VecRestoreArray(patch->localRHS, &localRHS)); 2879 2880 CHKERRQ(VecSet(patch->localUpdate, 0.0)); 2881 CHKERRQ(PetscSectionGetChart(patch->gtolCounts, &pStart, NULL)); 2882 CHKERRQ(PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0)); 2883 for (sweep = 0; sweep < nsweep; sweep++) { 2884 for (j = start[sweep]; j*inc[sweep] < end[sweep]*inc[sweep]; j += inc[sweep]) { 2885 PetscInt i = patch->user_patches ? iterationSet[j] : j; 2886 PetscInt start, len; 2887 2888 CHKERRQ(PetscSectionGetDof(patch->gtolCounts, i+pStart, &len)); 2889 CHKERRQ(PetscSectionGetOffset(patch->gtolCounts, i+pStart, &start)); 2890 /* TODO: Squash out these guys in the setup as well. */ 2891 if (len <= 0) continue; 2892 /* TODO: Do we need different scatters for X and Y? */ 2893 CHKERRQ(PCPatch_ScatterLocal_Private(pc, i+pStart, patch->localRHS, patch->patchRHS, INSERT_VALUES, SCATTER_FORWARD, SCATTER_INTERIOR)); 2894 CHKERRQ((*patch->applysolver)(pc, i, patch->patchRHS, patch->patchUpdate)); 2895 CHKERRQ(PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchUpdate, patch->localUpdate, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR)); 2896 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 2897 CHKERRQ((*patch->updatemultiplicative)(pc, i, pStart)); 2898 } 2899 } 2900 } 2901 CHKERRQ(PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0)); 2902 if (patch->user_patches) CHKERRQ(ISRestoreIndices(patch->iterationSet, &iterationSet)); 2903 /* XXX: should we do this on the global vector? */ 2904 if (patch->partition_of_unity) { 2905 CHKERRQ(VecPointwiseMult(patch->localUpdate, patch->localUpdate, patch->dof_weights)); 2906 } 2907 /* Now patch->localUpdate contains the solution of the patch solves, so we need to combine them all. */ 2908 CHKERRQ(VecSet(y, 0.0)); 2909 CHKERRQ(VecGetArray(y, &globalUpdate)); 2910 CHKERRQ(VecGetArrayRead(patch->localUpdate, &localUpdate)); 2911 CHKERRQ(PetscSFReduceBegin(patch->sectionSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM)); 2912 CHKERRQ(PetscSFReduceEnd(patch->sectionSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM)); 2913 CHKERRQ(VecRestoreArrayRead(patch->localUpdate, &localUpdate)); 2914 2915 /* Now we need to send the global BC values through */ 2916 CHKERRQ(VecGetArrayRead(x, &globalRHS)); 2917 CHKERRQ(ISGetSize(patch->globalBcNodes, &numBcs)); 2918 CHKERRQ(ISGetIndices(patch->globalBcNodes, &bcNodes)); 2919 CHKERRQ(VecGetLocalSize(x, &n)); 2920 for (bc = 0; bc < numBcs; ++bc) { 2921 const PetscInt idx = bcNodes[bc]; 2922 if (idx < n) globalUpdate[idx] = globalRHS[idx]; 2923 } 2924 2925 CHKERRQ(ISRestoreIndices(patch->globalBcNodes, &bcNodes)); 2926 CHKERRQ(VecRestoreArrayRead(x, &globalRHS)); 2927 CHKERRQ(VecRestoreArray(y, &globalUpdate)); 2928 2929 CHKERRQ(PetscOptionsPopGetViewerOff()); 2930 CHKERRQ(PetscLogEventEnd(PC_Patch_Apply, pc, 0, 0, 0)); 2931 PetscFunctionReturn(0); 2932 } 2933 2934 static PetscErrorCode PCReset_PATCH_Linear(PC pc) 2935 { 2936 PC_PATCH *patch = (PC_PATCH *) pc->data; 2937 PetscInt i; 2938 2939 PetscFunctionBegin; 2940 if (patch->solver) { 2941 for (i = 0; i < patch->npatch; ++i) CHKERRQ(KSPReset((KSP) patch->solver[i])); 2942 } 2943 PetscFunctionReturn(0); 2944 } 2945 2946 static PetscErrorCode PCReset_PATCH(PC pc) 2947 { 2948 PC_PATCH *patch = (PC_PATCH *) pc->data; 2949 PetscInt i; 2950 2951 PetscFunctionBegin; 2952 2953 CHKERRQ(PetscSFDestroy(&patch->sectionSF)); 2954 CHKERRQ(PetscSectionDestroy(&patch->cellCounts)); 2955 CHKERRQ(PetscSectionDestroy(&patch->pointCounts)); 2956 CHKERRQ(PetscSectionDestroy(&patch->cellNumbering)); 2957 CHKERRQ(PetscSectionDestroy(&patch->gtolCounts)); 2958 CHKERRQ(ISDestroy(&patch->gtol)); 2959 CHKERRQ(ISDestroy(&patch->cells)); 2960 CHKERRQ(ISDestroy(&patch->points)); 2961 CHKERRQ(ISDestroy(&patch->dofs)); 2962 CHKERRQ(ISDestroy(&patch->offs)); 2963 CHKERRQ(PetscSectionDestroy(&patch->patchSection)); 2964 CHKERRQ(ISDestroy(&patch->ghostBcNodes)); 2965 CHKERRQ(ISDestroy(&patch->globalBcNodes)); 2966 CHKERRQ(PetscSectionDestroy(&patch->gtolCountsWithArtificial)); 2967 CHKERRQ(ISDestroy(&patch->gtolWithArtificial)); 2968 CHKERRQ(ISDestroy(&patch->dofsWithArtificial)); 2969 CHKERRQ(ISDestroy(&patch->offsWithArtificial)); 2970 CHKERRQ(PetscSectionDestroy(&patch->gtolCountsWithAll)); 2971 CHKERRQ(ISDestroy(&patch->gtolWithAll)); 2972 CHKERRQ(ISDestroy(&patch->dofsWithAll)); 2973 CHKERRQ(ISDestroy(&patch->offsWithAll)); 2974 CHKERRQ(VecDestroy(&patch->cellMats)); 2975 CHKERRQ(VecDestroy(&patch->intFacetMats)); 2976 CHKERRQ(ISDestroy(&patch->allCells)); 2977 CHKERRQ(ISDestroy(&patch->intFacets)); 2978 CHKERRQ(ISDestroy(&patch->extFacets)); 2979 CHKERRQ(ISDestroy(&patch->intFacetsToPatchCell)); 2980 CHKERRQ(PetscSectionDestroy(&patch->intFacetCounts)); 2981 CHKERRQ(PetscSectionDestroy(&patch->extFacetCounts)); 2982 2983 if (patch->dofSection) for (i = 0; i < patch->nsubspaces; i++) CHKERRQ(PetscSectionDestroy(&patch->dofSection[i])); 2984 CHKERRQ(PetscFree(patch->dofSection)); 2985 CHKERRQ(PetscFree(patch->bs)); 2986 CHKERRQ(PetscFree(patch->nodesPerCell)); 2987 if (patch->cellNodeMap) for (i = 0; i < patch->nsubspaces; i++) CHKERRQ(PetscFree(patch->cellNodeMap[i])); 2988 CHKERRQ(PetscFree(patch->cellNodeMap)); 2989 CHKERRQ(PetscFree(patch->subspaceOffsets)); 2990 2991 CHKERRQ((*patch->resetsolver)(pc)); 2992 2993 if (patch->subspaces_to_exclude) { 2994 CHKERRQ(PetscHSetIDestroy(&patch->subspaces_to_exclude)); 2995 } 2996 2997 CHKERRQ(VecDestroy(&patch->localRHS)); 2998 CHKERRQ(VecDestroy(&patch->localUpdate)); 2999 CHKERRQ(VecDestroy(&patch->patchRHS)); 3000 CHKERRQ(VecDestroy(&patch->patchUpdate)); 3001 CHKERRQ(VecDestroy(&patch->dof_weights)); 3002 if (patch->patch_dof_weights) { 3003 for (i = 0; i < patch->npatch; ++i) CHKERRQ(VecDestroy(&patch->patch_dof_weights[i])); 3004 CHKERRQ(PetscFree(patch->patch_dof_weights)); 3005 } 3006 if (patch->mat) { 3007 for (i = 0; i < patch->npatch; ++i) CHKERRQ(MatDestroy(&patch->mat[i])); 3008 CHKERRQ(PetscFree(patch->mat)); 3009 } 3010 if (patch->matWithArtificial) { 3011 for (i = 0; i < patch->npatch; ++i) CHKERRQ(MatDestroy(&patch->matWithArtificial[i])); 3012 CHKERRQ(PetscFree(patch->matWithArtificial)); 3013 } 3014 CHKERRQ(VecDestroy(&patch->patchRHSWithArtificial)); 3015 if (patch->dofMappingWithoutToWithArtificial) { 3016 for (i = 0; i < patch->npatch; ++i) CHKERRQ(ISDestroy(&patch->dofMappingWithoutToWithArtificial[i])); 3017 CHKERRQ(PetscFree(patch->dofMappingWithoutToWithArtificial)); 3018 3019 } 3020 if (patch->dofMappingWithoutToWithAll) { 3021 for (i = 0; i < patch->npatch; ++i) CHKERRQ(ISDestroy(&patch->dofMappingWithoutToWithAll[i])); 3022 CHKERRQ(PetscFree(patch->dofMappingWithoutToWithAll)); 3023 3024 } 3025 CHKERRQ(PetscFree(patch->sub_mat_type)); 3026 if (patch->userIS) { 3027 for (i = 0; i < patch->npatch; ++i) CHKERRQ(ISDestroy(&patch->userIS[i])); 3028 CHKERRQ(PetscFree(patch->userIS)); 3029 } 3030 CHKERRQ(PetscFree(patch->precomputedTensorLocations)); 3031 CHKERRQ(PetscFree(patch->precomputedIntFacetTensorLocations)); 3032 3033 patch->bs = NULL; 3034 patch->cellNodeMap = NULL; 3035 patch->nsubspaces = 0; 3036 CHKERRQ(ISDestroy(&patch->iterationSet)); 3037 3038 CHKERRQ(PetscViewerDestroy(&patch->viewerSection)); 3039 PetscFunctionReturn(0); 3040 } 3041 3042 static PetscErrorCode PCDestroy_PATCH_Linear(PC pc) 3043 { 3044 PC_PATCH *patch = (PC_PATCH *) pc->data; 3045 PetscInt i; 3046 3047 PetscFunctionBegin; 3048 if (patch->solver) { 3049 for (i = 0; i < patch->npatch; ++i) CHKERRQ(KSPDestroy((KSP *) &patch->solver[i])); 3050 CHKERRQ(PetscFree(patch->solver)); 3051 } 3052 PetscFunctionReturn(0); 3053 } 3054 3055 static PetscErrorCode PCDestroy_PATCH(PC pc) 3056 { 3057 PC_PATCH *patch = (PC_PATCH *) pc->data; 3058 3059 PetscFunctionBegin; 3060 CHKERRQ(PCReset_PATCH(pc)); 3061 CHKERRQ((*patch->destroysolver)(pc)); 3062 CHKERRQ(PetscFree(pc->data)); 3063 PetscFunctionReturn(0); 3064 } 3065 3066 static PetscErrorCode PCSetFromOptions_PATCH(PetscOptionItems *PetscOptionsObject, PC pc) 3067 { 3068 PC_PATCH *patch = (PC_PATCH *) pc->data; 3069 PCPatchConstructType patchConstructionType = PC_PATCH_STAR; 3070 char sub_mat_type[PETSC_MAX_PATH_LEN]; 3071 char option[PETSC_MAX_PATH_LEN]; 3072 const char *prefix; 3073 PetscBool flg, dimflg, codimflg; 3074 MPI_Comm comm; 3075 PetscInt *ifields, nfields, k; 3076 PCCompositeType loctype = PC_COMPOSITE_ADDITIVE; 3077 3078 PetscFunctionBegin; 3079 CHKERRQ(PetscObjectGetComm((PetscObject) pc, &comm)); 3080 CHKERRQ(PetscObjectGetOptionsPrefix((PetscObject) pc, &prefix)); 3081 CHKERRQ(PetscOptionsHead(PetscOptionsObject, "Patch solver options")); 3082 3083 CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_save_operators", patch->classname)); 3084 CHKERRQ(PetscOptionsBool(option, "Store all patch operators for lifetime of object?", "PCPatchSetSaveOperators", patch->save_operators, &patch->save_operators, &flg)); 3085 3086 CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_precompute_element_tensors", patch->classname)); 3087 CHKERRQ(PetscOptionsBool(option, "Compute each element tensor only once?", "PCPatchSetPrecomputeElementTensors", patch->precomputeElementTensors, &patch->precomputeElementTensors, &flg)); 3088 CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_partition_of_unity", patch->classname)); 3089 CHKERRQ(PetscOptionsBool(option, "Weight contributions by dof multiplicity?", "PCPatchSetPartitionOfUnity", patch->partition_of_unity, &patch->partition_of_unity, &flg)); 3090 3091 CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_local_type", patch->classname)); 3092 CHKERRQ(PetscOptionsEnum(option,"Type of local solver composition (additive or multiplicative)","PCPatchSetLocalComposition",PCCompositeTypes,(PetscEnum)loctype,(PetscEnum*)&loctype,&flg)); 3093 if (flg) CHKERRQ(PCPatchSetLocalComposition(pc, loctype)); 3094 CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_dense_inverse", patch->classname)); 3095 CHKERRQ(PetscOptionsBool(option, "Compute inverses of patch matrices and apply directly? Ignores KSP/PC settings on patch.", "PCPatchSetDenseInverse", patch->denseinverse, &patch->denseinverse, &flg)); 3096 CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_dim", patch->classname)); 3097 CHKERRQ(PetscOptionsInt(option, "What dimension of mesh point to construct patches by? (0 = vertices)", "PCPATCH", patch->dim, &patch->dim, &dimflg)); 3098 CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_codim", patch->classname)); 3099 CHKERRQ(PetscOptionsInt(option, "What co-dimension of mesh point to construct patches by? (0 = cells)", "PCPATCH", patch->codim, &patch->codim, &codimflg)); 3100 PetscCheckFalse(dimflg && codimflg,comm, PETSC_ERR_ARG_WRONG, "Can only set one of dimension or co-dimension"); 3101 3102 CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_type", patch->classname)); 3103 CHKERRQ(PetscOptionsEnum(option, "How should the patches be constructed?", "PCPatchSetConstructType", PCPatchConstructTypes, (PetscEnum) patchConstructionType, (PetscEnum *) &patchConstructionType, &flg)); 3104 if (flg) CHKERRQ(PCPatchSetConstructType(pc, patchConstructionType, NULL, NULL)); 3105 3106 CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_vanka_dim", patch->classname)); 3107 CHKERRQ(PetscOptionsInt(option, "Topological dimension of entities for Vanka to ignore", "PCPATCH", patch->vankadim, &patch->vankadim, &flg)); 3108 3109 CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_ignore_dim", patch->classname)); 3110 CHKERRQ(PetscOptionsInt(option, "Topological dimension of entities for completion to ignore", "PCPATCH", patch->ignoredim, &patch->ignoredim, &flg)); 3111 3112 CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_pardecomp_overlap", patch->classname)); 3113 CHKERRQ(PetscOptionsInt(option, "What overlap should we use in construct type pardecomp?", "PCPATCH", patch->pardecomp_overlap, &patch->pardecomp_overlap, &flg)); 3114 3115 CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_sub_mat_type", patch->classname)); 3116 CHKERRQ(PetscOptionsFList(option, "Matrix type for patch solves", "PCPatchSetSubMatType", MatList, NULL, sub_mat_type, PETSC_MAX_PATH_LEN, &flg)); 3117 if (flg) CHKERRQ(PCPatchSetSubMatType(pc, sub_mat_type)); 3118 3119 CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_symmetrise_sweep", patch->classname)); 3120 CHKERRQ(PetscOptionsBool(option, "Go start->end, end->start?", "PCPATCH", patch->symmetrise_sweep, &patch->symmetrise_sweep, &flg)); 3121 3122 /* If the user has set the number of subspaces, use that for the buffer size, 3123 otherwise use a large number */ 3124 if (patch->nsubspaces <= 0) { 3125 nfields = 128; 3126 } else { 3127 nfields = patch->nsubspaces; 3128 } 3129 CHKERRQ(PetscMalloc1(nfields, &ifields)); 3130 CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exclude_subspaces", patch->classname)); 3131 CHKERRQ(PetscOptionsGetIntArray(((PetscObject)pc)->options,((PetscObject)pc)->prefix,option,ifields,&nfields,&flg)); 3132 PetscCheckFalse(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"); 3133 if (flg) { 3134 CHKERRQ(PetscHSetIClear(patch->subspaces_to_exclude)); 3135 for (k = 0; k < nfields; k++) { 3136 CHKERRQ(PetscHSetIAdd(patch->subspaces_to_exclude, ifields[k])); 3137 } 3138 } 3139 CHKERRQ(PetscFree(ifields)); 3140 3141 CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_patches_view", patch->classname)); 3142 CHKERRQ(PetscOptionsBool(option, "Print out information during patch construction", "PCPATCH", patch->viewPatches, &patch->viewPatches, &flg)); 3143 CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_cells_view", patch->classname)); 3144 CHKERRQ(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerCells, &patch->formatCells, &patch->viewCells)); 3145 CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_interior_facets_view", patch->classname)); 3146 CHKERRQ(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerIntFacets, &patch->formatIntFacets, &patch->viewIntFacets)); 3147 CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exterior_facets_view", patch->classname)); 3148 CHKERRQ(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerExtFacets, &patch->formatExtFacets, &patch->viewExtFacets)); 3149 CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_points_view", patch->classname)); 3150 CHKERRQ(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerPoints, &patch->formatPoints, &patch->viewPoints)); 3151 CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_section_view", patch->classname)); 3152 CHKERRQ(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerSection, &patch->formatSection, &patch->viewSection)); 3153 CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_mat_view", patch->classname)); 3154 CHKERRQ(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerMatrix, &patch->formatMatrix, &patch->viewMatrix)); 3155 CHKERRQ(PetscOptionsTail()); 3156 patch->optionsSet = PETSC_TRUE; 3157 PetscFunctionReturn(0); 3158 } 3159 3160 static PetscErrorCode PCSetUpOnBlocks_PATCH(PC pc) 3161 { 3162 PC_PATCH *patch = (PC_PATCH*) pc->data; 3163 KSPConvergedReason reason; 3164 PetscInt i; 3165 3166 PetscFunctionBegin; 3167 if (!patch->save_operators) { 3168 /* Can't do this here because the sub KSPs don't have an operator attached yet. */ 3169 PetscFunctionReturn(0); 3170 } 3171 if (patch->denseinverse) { 3172 /* No solvers */ 3173 PetscFunctionReturn(0); 3174 } 3175 for (i = 0; i < patch->npatch; ++i) { 3176 if (!((KSP) patch->solver[i])->setfromoptionscalled) { 3177 CHKERRQ(KSPSetFromOptions((KSP) patch->solver[i])); 3178 } 3179 CHKERRQ(KSPSetUp((KSP) patch->solver[i])); 3180 CHKERRQ(KSPGetConvergedReason((KSP) patch->solver[i], &reason)); 3181 if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR; 3182 } 3183 PetscFunctionReturn(0); 3184 } 3185 3186 static PetscErrorCode PCView_PATCH(PC pc, PetscViewer viewer) 3187 { 3188 PC_PATCH *patch = (PC_PATCH *) pc->data; 3189 PetscViewer sviewer; 3190 PetscBool isascii; 3191 PetscMPIInt rank; 3192 3193 PetscFunctionBegin; 3194 /* TODO Redo tabbing with set tbas in new style */ 3195 CHKERRQ(PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii)); 3196 if (!isascii) PetscFunctionReturn(0); 3197 CHKERRMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) pc), &rank)); 3198 CHKERRQ(PetscViewerASCIIPushTab(viewer)); 3199 CHKERRQ(PetscViewerASCIIPrintf(viewer, "Subspace Correction preconditioner with %d patches\n", patch->npatch)); 3200 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 3201 CHKERRQ(PetscViewerASCIIPrintf(viewer, "Schwarz type: multiplicative\n")); 3202 } else { 3203 CHKERRQ(PetscViewerASCIIPrintf(viewer, "Schwarz type: additive\n")); 3204 } 3205 if (patch->partition_of_unity) CHKERRQ(PetscViewerASCIIPrintf(viewer, "Weighting by partition of unity\n")); 3206 else CHKERRQ(PetscViewerASCIIPrintf(viewer, "Not weighting by partition of unity\n")); 3207 if (patch->symmetrise_sweep) CHKERRQ(PetscViewerASCIIPrintf(viewer, "Symmetrising sweep (start->end, then end->start)\n")); 3208 else CHKERRQ(PetscViewerASCIIPrintf(viewer, "Not symmetrising sweep\n")); 3209 if (!patch->precomputeElementTensors) CHKERRQ(PetscViewerASCIIPrintf(viewer, "Not precomputing element tensors (overlapping cells rebuilt in every patch assembly)\n")); 3210 else CHKERRQ(PetscViewerASCIIPrintf(viewer, "Precomputing element tensors (each cell assembled only once)\n")); 3211 if (!patch->save_operators) CHKERRQ(PetscViewerASCIIPrintf(viewer, "Not saving patch operators (rebuilt every PCApply)\n")); 3212 else CHKERRQ(PetscViewerASCIIPrintf(viewer, "Saving patch operators (rebuilt every PCSetUp)\n")); 3213 if (patch->patchconstructop == PCPatchConstruct_Star) CHKERRQ(PetscViewerASCIIPrintf(viewer, "Patch construction operator: star\n")); 3214 else if (patch->patchconstructop == PCPatchConstruct_Vanka) CHKERRQ(PetscViewerASCIIPrintf(viewer, "Patch construction operator: Vanka\n")); 3215 else if (patch->patchconstructop == PCPatchConstruct_User) CHKERRQ(PetscViewerASCIIPrintf(viewer, "Patch construction operator: user-specified\n")); 3216 else CHKERRQ(PetscViewerASCIIPrintf(viewer, "Patch construction operator: unknown\n")); 3217 3218 if (patch->denseinverse) { 3219 CHKERRQ(PetscViewerASCIIPrintf(viewer, "Explicitly forming dense inverse and applying patch solver via MatMult.\n")); 3220 } else { 3221 if (patch->isNonlinear) { 3222 CHKERRQ(PetscViewerASCIIPrintf(viewer, "SNES on patches (all same):\n")); 3223 } else { 3224 CHKERRQ(PetscViewerASCIIPrintf(viewer, "KSP on patches (all same):\n")); 3225 } 3226 if (patch->solver) { 3227 CHKERRQ(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer)); 3228 if (rank == 0) { 3229 CHKERRQ(PetscViewerASCIIPushTab(sviewer)); 3230 CHKERRQ(PetscObjectView(patch->solver[0], sviewer)); 3231 CHKERRQ(PetscViewerASCIIPopTab(sviewer)); 3232 } 3233 CHKERRQ(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer)); 3234 } else { 3235 CHKERRQ(PetscViewerASCIIPushTab(viewer)); 3236 CHKERRQ(PetscViewerASCIIPrintf(viewer, "Solver not yet set.\n")); 3237 CHKERRQ(PetscViewerASCIIPopTab(viewer)); 3238 } 3239 } 3240 CHKERRQ(PetscViewerASCIIPopTab(viewer)); 3241 PetscFunctionReturn(0); 3242 } 3243 3244 /*MC 3245 PCPATCH - A PC object that encapsulates flexible definition of blocks for overlapping and non-overlapping 3246 small block additive preconditioners. Block definition is based on topology from 3247 a DM and equation numbering from a PetscSection. 3248 3249 Options Database Keys: 3250 + -pc_patch_cells_view - Views the process local cell numbers for each patch 3251 . -pc_patch_points_view - Views the process local mesh point numbers for each patch 3252 . -pc_patch_g2l_view - Views the map between global dofs and patch local dofs for each patch 3253 . -pc_patch_patches_view - Views the global dofs associated with each patch and its boundary 3254 - -pc_patch_sub_mat_view - Views the matrix associated with each patch 3255 3256 Level: intermediate 3257 3258 .seealso: PCType, PCCreate(), PCSetType() 3259 M*/ 3260 PETSC_EXTERN PetscErrorCode PCCreate_Patch(PC pc) 3261 { 3262 PC_PATCH *patch; 3263 3264 PetscFunctionBegin; 3265 CHKERRQ(PetscNewLog(pc, &patch)); 3266 3267 if (patch->subspaces_to_exclude) { 3268 CHKERRQ(PetscHSetIDestroy(&patch->subspaces_to_exclude)); 3269 } 3270 CHKERRQ(PetscHSetICreate(&patch->subspaces_to_exclude)); 3271 3272 patch->classname = "pc"; 3273 patch->isNonlinear = PETSC_FALSE; 3274 3275 /* Set some defaults */ 3276 patch->combined = PETSC_FALSE; 3277 patch->save_operators = PETSC_TRUE; 3278 patch->local_composition_type = PC_COMPOSITE_ADDITIVE; 3279 patch->precomputeElementTensors = PETSC_FALSE; 3280 patch->partition_of_unity = PETSC_FALSE; 3281 patch->codim = -1; 3282 patch->dim = -1; 3283 patch->vankadim = -1; 3284 patch->ignoredim = -1; 3285 patch->pardecomp_overlap = 0; 3286 patch->patchconstructop = PCPatchConstruct_Star; 3287 patch->symmetrise_sweep = PETSC_FALSE; 3288 patch->npatch = 0; 3289 patch->userIS = NULL; 3290 patch->optionsSet = PETSC_FALSE; 3291 patch->iterationSet = NULL; 3292 patch->user_patches = PETSC_FALSE; 3293 CHKERRQ(PetscStrallocpy(MATDENSE, (char **) &patch->sub_mat_type)); 3294 patch->viewPatches = PETSC_FALSE; 3295 patch->viewCells = PETSC_FALSE; 3296 patch->viewPoints = PETSC_FALSE; 3297 patch->viewSection = PETSC_FALSE; 3298 patch->viewMatrix = PETSC_FALSE; 3299 patch->densesolve = NULL; 3300 patch->setupsolver = PCSetUp_PATCH_Linear; 3301 patch->applysolver = PCApply_PATCH_Linear; 3302 patch->resetsolver = PCReset_PATCH_Linear; 3303 patch->destroysolver = PCDestroy_PATCH_Linear; 3304 patch->updatemultiplicative = PCUpdateMultiplicative_PATCH_Linear; 3305 patch->dofMappingWithoutToWithArtificial = NULL; 3306 patch->dofMappingWithoutToWithAll = NULL; 3307 3308 pc->data = (void *) patch; 3309 pc->ops->apply = PCApply_PATCH; 3310 pc->ops->applytranspose = NULL; /* PCApplyTranspose_PATCH; */ 3311 pc->ops->setup = PCSetUp_PATCH; 3312 pc->ops->reset = PCReset_PATCH; 3313 pc->ops->destroy = PCDestroy_PATCH; 3314 pc->ops->setfromoptions = PCSetFromOptions_PATCH; 3315 pc->ops->setuponblocks = PCSetUpOnBlocks_PATCH; 3316 pc->ops->view = PCView_PATCH; 3317 pc->ops->applyrichardson = NULL; 3318 3319 PetscFunctionReturn(0); 3320 } 3321