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