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