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 PCPatchSetComputeOperator - Set the callback used to compute patch matrices 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 + usercomputeop(pc, point, x, mat, cellIS, n, u, ctx) 606 + pc - The PC 607 + point - The point 608 + x - The input solution (not used in linear problems) 609 + mat - The patch matrix 610 + cellIS - An array of the cell 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: PCPatchGetComputeOperator(), PCPatchSetComputeFunction(), PCPatchSetDiscretisationInfo() 617 @*/ 618 PetscErrorCode PCPatchSetComputeOperator(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Mat, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx) 619 { 620 PC_PATCH *patch = (PC_PATCH *) pc->data; 621 622 PetscFunctionBegin; 623 patch->usercomputeop = func; 624 patch->usercomputeopctx = ctx; 625 PetscFunctionReturn(0); 626 } 627 628 /* On entry, ht contains the topological entities whose dofs we are responsible for solving for; 629 on exit, cht contains all the topological entities we need to compute their residuals. 630 In full generality this should incorporate knowledge of the sparsity pattern of the matrix; 631 here we assume a standard FE sparsity pattern.*/ 632 /* TODO: Use DMPlexGetAdjacency() */ 633 static PetscErrorCode PCPatchCompleteCellPatch(PC pc, PetscHSetI ht, PetscHSetI cht) 634 { 635 DM dm; 636 PetscHashIter hi; 637 PetscInt point; 638 PetscInt *star = NULL, *closure = NULL; 639 PetscInt ignoredim, iStart = 0, iEnd = -1, starSize, closureSize, si, ci; 640 PetscErrorCode ierr; 641 642 PetscFunctionBegin; 643 ierr = PCGetDM(pc, &dm);CHKERRQ(ierr); 644 ierr = PCPatchGetIgnoreDim(pc, &ignoredim);CHKERRQ(ierr); 645 if (ignoredim >= 0) {ierr = DMPlexGetDepthStratum(dm, ignoredim, &iStart, &iEnd);CHKERRQ(ierr);} 646 ierr = PetscHSetIClear(cht);CHKERRQ(ierr); 647 PetscHashIterBegin(ht, hi); 648 while (!PetscHashIterAtEnd(ht, hi)) { 649 650 PetscHashIterGetKey(ht, hi, point); 651 PetscHashIterNext(ht, hi); 652 653 /* Loop over all the cells that this point connects to */ 654 ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr); 655 for (si = 0; si < starSize*2; si += 2) { 656 const PetscInt ownedpoint = star[si]; 657 /* TODO Check for point in cht before running through closure again */ 658 /* now loop over all entities in the closure of that cell */ 659 ierr = DMPlexGetTransitiveClosure(dm, ownedpoint, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 660 for (ci = 0; ci < closureSize*2; ci += 2) { 661 const PetscInt seenpoint = closure[ci]; 662 if (ignoredim >= 0 && seenpoint >= iStart && seenpoint < iEnd) continue; 663 ierr = PetscHSetIAdd(cht, seenpoint);CHKERRQ(ierr); 664 } 665 ierr = DMPlexRestoreTransitiveClosure(dm, ownedpoint, PETSC_TRUE, NULL, &closure);CHKERRQ(ierr); 666 } 667 ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, NULL, &star);CHKERRQ(ierr); 668 } 669 PetscFunctionReturn(0); 670 } 671 672 static PetscErrorCode PCPatchGetGlobalDofs(PC pc, PetscSection dofSection[], PetscInt f, PetscBool combined, PetscInt p, PetscInt *dof, PetscInt *off) 673 { 674 PetscErrorCode ierr; 675 676 PetscFunctionBegin; 677 if (combined) { 678 if (f < 0) { 679 if (dof) {ierr = PetscSectionGetDof(dofSection[0], p, dof);CHKERRQ(ierr);} 680 if (off) {ierr = PetscSectionGetOffset(dofSection[0], p, off);CHKERRQ(ierr);} 681 } else { 682 if (dof) {ierr = PetscSectionGetFieldDof(dofSection[0], p, f, dof);CHKERRQ(ierr);} 683 if (off) {ierr = PetscSectionGetFieldOffset(dofSection[0], p, f, off);CHKERRQ(ierr);} 684 } 685 } else { 686 if (f < 0) { 687 PC_PATCH *patch = (PC_PATCH *) pc->data; 688 PetscInt fdof, g; 689 690 if (dof) { 691 *dof = 0; 692 for (g = 0; g < patch->nsubspaces; ++g) { 693 ierr = PetscSectionGetDof(dofSection[g], p, &fdof);CHKERRQ(ierr); 694 *dof += fdof; 695 } 696 } 697 if (off) { 698 *off = 0; 699 for (g = 0; g < patch->nsubspaces; ++g) { 700 ierr = PetscSectionGetOffset(dofSection[g], p, &fdof);CHKERRQ(ierr); 701 *off += fdof; 702 } 703 } 704 } else { 705 if (dof) {ierr = PetscSectionGetDof(dofSection[f], p, dof);CHKERRQ(ierr);} 706 if (off) {ierr = PetscSectionGetOffset(dofSection[f], p, off);CHKERRQ(ierr);} 707 } 708 } 709 PetscFunctionReturn(0); 710 } 711 712 /* Given a hash table with a set of topological entities (pts), compute the degrees of 713 freedom in global concatenated numbering on those entities. 714 For Vanka smoothing, this needs to do something special: ignore dofs of the 715 constraint subspace on entities that aren't the base entity we're building the patch 716 around. */ 717 static PetscErrorCode PCPatchGetPointDofs(PC pc, PetscHSetI pts, PetscHSetI dofs, PetscInt base, PetscHSetI* subspaces_to_exclude) 718 { 719 PC_PATCH *patch = (PC_PATCH *) pc->data; 720 PetscHashIter hi; 721 PetscInt ldof, loff; 722 PetscInt k, p; 723 PetscErrorCode ierr; 724 725 PetscFunctionBegin; 726 ierr = PetscHSetIClear(dofs);CHKERRQ(ierr); 727 for (k = 0; k < patch->nsubspaces; ++k) { 728 PetscInt subspaceOffset = patch->subspaceOffsets[k]; 729 PetscInt bs = patch->bs[k]; 730 PetscInt j, l; 731 732 if (subspaces_to_exclude != NULL) { 733 PetscBool should_exclude_k = PETSC_FALSE; 734 PetscHSetIHas(*subspaces_to_exclude, k, &should_exclude_k); 735 if (should_exclude_k) { 736 /* only get this subspace dofs at the base entity, not any others */ 737 ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, base, &ldof, &loff);CHKERRQ(ierr); 738 if (0 == ldof) continue; 739 for (j = loff; j < ldof + loff; ++j) { 740 for (l = 0; l < bs; ++l) { 741 PetscInt dof = bs*j + l + subspaceOffset; 742 ierr = PetscHSetIAdd(dofs, dof);CHKERRQ(ierr); 743 } 744 } 745 continue; /* skip the other dofs of this subspace */ 746 } 747 } 748 749 PetscHashIterBegin(pts, hi); 750 while (!PetscHashIterAtEnd(pts, hi)) { 751 PetscHashIterGetKey(pts, hi, p); 752 PetscHashIterNext(pts, hi); 753 ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, p, &ldof, &loff);CHKERRQ(ierr); 754 if (0 == ldof) continue; 755 for (j = loff; j < ldof + loff; ++j) { 756 for (l = 0; l < bs; ++l) { 757 PetscInt dof = bs*j + l + subspaceOffset; 758 ierr = PetscHSetIAdd(dofs, dof);CHKERRQ(ierr); 759 } 760 } 761 } 762 } 763 PetscFunctionReturn(0); 764 } 765 766 /* Given two hash tables A and B, compute the keys in B that are not in A, and put them in C */ 767 static PetscErrorCode PCPatchComputeSetDifference_Private(PetscHSetI A, PetscHSetI B, PetscHSetI C) 768 { 769 PetscHashIter hi; 770 PetscInt key; 771 PetscBool flg; 772 PetscErrorCode ierr; 773 774 PetscFunctionBegin; 775 ierr = PetscHSetIClear(C);CHKERRQ(ierr); 776 PetscHashIterBegin(B, hi); 777 while (!PetscHashIterAtEnd(B, hi)) { 778 PetscHashIterGetKey(B, hi, key); 779 PetscHashIterNext(B, hi); 780 ierr = PetscHSetIHas(A, key, &flg);CHKERRQ(ierr); 781 if (!flg) {ierr = PetscHSetIAdd(C, key);CHKERRQ(ierr);} 782 } 783 PetscFunctionReturn(0); 784 } 785 786 /* 787 * PCPatchCreateCellPatches - create patches. 788 * 789 * Input Parameters: 790 * + dm - The DMPlex object defining the mesh 791 * 792 * Output Parameters: 793 * + cellCounts - Section with counts of cells around each vertex 794 * . cells - IS of the cell point indices of cells in each patch 795 * . pointCounts - Section with counts of cells around each vertex 796 * - point - IS of the cell point indices of cells in each patch 797 */ 798 static PetscErrorCode PCPatchCreateCellPatches(PC pc) 799 { 800 PC_PATCH *patch = (PC_PATCH *) pc->data; 801 DMLabel ghost = NULL; 802 DM dm, plex; 803 PetscHSetI ht, cht; 804 PetscSection cellCounts, pointCounts; 805 PetscInt *cellsArray, *pointsArray; 806 PetscInt numCells, numPoints; 807 const PetscInt *leaves; 808 PetscInt nleaves, pStart, pEnd, cStart, cEnd, vStart, vEnd, v; 809 PetscBool isFiredrake; 810 PetscErrorCode ierr; 811 812 PetscFunctionBegin; 813 /* Used to keep track of the cells in the patch. */ 814 ierr = PetscHSetICreate(&ht);CHKERRQ(ierr); 815 ierr = PetscHSetICreate(&cht);CHKERRQ(ierr); 816 817 ierr = PCGetDM(pc, &dm);CHKERRQ(ierr); 818 if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONGSTATE, "DM not yet set on patch PC\n"); 819 ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr); 820 ierr = DMPlexGetChart(plex, &pStart, &pEnd);CHKERRQ(ierr); 821 ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr); 822 823 if (patch->user_patches) { 824 ierr = patch->userpatchconstructionop(pc, &patch->npatch, &patch->userIS, &patch->iterationSet, patch->userpatchconstructctx);CHKERRQ(ierr); 825 vStart = 0; vEnd = patch->npatch; 826 } else if (patch->ctype == PC_PATCH_PARDECOMP) { 827 vStart = 0; vEnd = 1; 828 } else if (patch->codim < 0) { 829 if (patch->dim < 0) {ierr = DMPlexGetDepthStratum(plex, 0, &vStart, &vEnd);CHKERRQ(ierr);} 830 else {ierr = DMPlexGetDepthStratum(plex, patch->dim, &vStart, &vEnd);CHKERRQ(ierr);} 831 } else {ierr = DMPlexGetHeightStratum(plex, patch->codim, &vStart, &vEnd);CHKERRQ(ierr);} 832 patch->npatch = vEnd - vStart; 833 834 /* These labels mark the owned points. We only create patches around points that this process owns. */ 835 ierr = DMHasLabel(dm, "pyop2_ghost", &isFiredrake);CHKERRQ(ierr); 836 if (isFiredrake) { 837 ierr = DMGetLabel(dm, "pyop2_ghost", &ghost);CHKERRQ(ierr); 838 ierr = DMLabelCreateIndex(ghost, pStart, pEnd);CHKERRQ(ierr); 839 } else { 840 PetscSF sf; 841 842 ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr); 843 ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr); 844 nleaves = PetscMax(nleaves, 0); 845 } 846 847 ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->cellCounts);CHKERRQ(ierr); 848 ierr = PetscObjectSetName((PetscObject) patch->cellCounts, "Patch Cell Layout");CHKERRQ(ierr); 849 cellCounts = patch->cellCounts; 850 ierr = PetscSectionSetChart(cellCounts, vStart, vEnd);CHKERRQ(ierr); 851 ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->pointCounts);CHKERRQ(ierr); 852 ierr = PetscObjectSetName((PetscObject) patch->pointCounts, "Patch Point Layout");CHKERRQ(ierr); 853 pointCounts = patch->pointCounts; 854 ierr = PetscSectionSetChart(pointCounts, vStart, vEnd);CHKERRQ(ierr); 855 /* Count cells and points in the patch surrounding each entity */ 856 for (v = vStart; v < vEnd; ++v) { 857 PetscHashIter hi; 858 PetscInt chtSize, loc = -1; 859 PetscBool flg; 860 861 if (!patch->user_patches && patch->ctype != PC_PATCH_PARDECOMP) { 862 if (ghost) {ierr = DMLabelHasPoint(ghost, v, &flg);CHKERRQ(ierr);} 863 else {ierr = PetscFindInt(v, nleaves, leaves, &loc);CHKERRQ(ierr); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;} 864 /* Not an owned entity, don't make a cell patch. */ 865 if (flg) continue; 866 } 867 868 ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr); 869 ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr); 870 ierr = PetscHSetIGetSize(cht, &chtSize);CHKERRQ(ierr); 871 /* empty patch, continue */ 872 if (chtSize == 0) continue; 873 874 /* safe because size(cht) > 0 from above */ 875 PetscHashIterBegin(cht, hi); 876 while (!PetscHashIterAtEnd(cht, hi)) { 877 PetscInt point, pdof; 878 879 PetscHashIterGetKey(cht, hi, point); 880 ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr); 881 if (pdof) {ierr = PetscSectionAddDof(pointCounts, v, 1);CHKERRQ(ierr);} 882 if (point >= cStart && point < cEnd) {ierr = PetscSectionAddDof(cellCounts, v, 1);CHKERRQ(ierr);} 883 PetscHashIterNext(cht, hi); 884 } 885 } 886 if (isFiredrake) {ierr = DMLabelDestroyIndex(ghost);CHKERRQ(ierr);} 887 888 ierr = PetscSectionSetUp(cellCounts);CHKERRQ(ierr); 889 ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr); 890 ierr = PetscMalloc1(numCells, &cellsArray);CHKERRQ(ierr); 891 ierr = PetscSectionSetUp(pointCounts);CHKERRQ(ierr); 892 ierr = PetscSectionGetStorageSize(pointCounts, &numPoints);CHKERRQ(ierr); 893 ierr = PetscMalloc1(numPoints, &pointsArray);CHKERRQ(ierr); 894 895 /* Now that we know how much space we need, run through again and actually remember the cells. */ 896 for (v = vStart; v < vEnd; v++ ) { 897 PetscHashIter hi; 898 PetscInt dof, off, cdof, coff, pdof, n = 0, cn = 0; 899 900 ierr = PetscSectionGetDof(pointCounts, v, &dof);CHKERRQ(ierr); 901 ierr = PetscSectionGetOffset(pointCounts, v, &off);CHKERRQ(ierr); 902 ierr = PetscSectionGetDof(cellCounts, v, &cdof);CHKERRQ(ierr); 903 ierr = PetscSectionGetOffset(cellCounts, v, &coff);CHKERRQ(ierr); 904 if (dof <= 0) continue; 905 ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr); 906 ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr); 907 PetscHashIterBegin(cht, hi); 908 while (!PetscHashIterAtEnd(cht, hi)) { 909 PetscInt point; 910 911 PetscHashIterGetKey(cht, hi, point); 912 ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr); 913 if (pdof) {pointsArray[off + n++] = point;} 914 if (point >= cStart && point < cEnd) {cellsArray[coff + cn++] = point;} 915 PetscHashIterNext(cht, hi); 916 } 917 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); 918 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); 919 } 920 ierr = PetscHSetIDestroy(&ht);CHKERRQ(ierr); 921 ierr = PetscHSetIDestroy(&cht);CHKERRQ(ierr); 922 ierr = DMDestroy(&plex);CHKERRQ(ierr); 923 924 ierr = ISCreateGeneral(PETSC_COMM_SELF, numCells, cellsArray, PETSC_OWN_POINTER, &patch->cells);CHKERRQ(ierr); 925 ierr = PetscObjectSetName((PetscObject) patch->cells, "Patch Cells");CHKERRQ(ierr); 926 if (patch->viewCells) { 927 ierr = ObjectView((PetscObject) patch->cellCounts, patch->viewerCells, patch->formatCells);CHKERRQ(ierr); 928 ierr = ObjectView((PetscObject) patch->cells, patch->viewerCells, patch->formatCells);CHKERRQ(ierr); 929 } 930 ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints, pointsArray, PETSC_OWN_POINTER, &patch->points);CHKERRQ(ierr); 931 ierr = PetscObjectSetName((PetscObject) patch->points, "Patch Points");CHKERRQ(ierr); 932 if (patch->viewPoints) { 933 ierr = ObjectView((PetscObject) patch->pointCounts, patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr); 934 ierr = ObjectView((PetscObject) patch->points, patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr); 935 } 936 PetscFunctionReturn(0); 937 } 938 939 /* 940 * PCPatchCreateCellPatchDiscretisationInfo - Build the dof maps for cell patches 941 * 942 * Input Parameters: 943 * + dm - The DMPlex object defining the mesh 944 * . cellCounts - Section with counts of cells around each vertex 945 * . cells - IS of the cell point indices of cells in each patch 946 * . cellNumbering - Section mapping plex cell points to Firedrake cell indices. 947 * . nodesPerCell - number of nodes per cell. 948 * - cellNodeMap - map from cells to node indices (nodesPerCell * numCells) 949 * 950 * Output Parameters: 951 * + dofs - IS of local dof numbers of each cell in the patch, where local is a patch local numbering 952 * . gtolCounts - Section with counts of dofs per cell patch 953 * - gtol - IS mapping from global dofs to local dofs for each patch. 954 */ 955 static PetscErrorCode PCPatchCreateCellPatchDiscretisationInfo(PC pc) 956 { 957 PC_PATCH *patch = (PC_PATCH *) pc->data; 958 PetscSection cellCounts = patch->cellCounts; 959 PetscSection pointCounts = patch->pointCounts; 960 PetscSection gtolCounts, gtolCountsWithArtificial = NULL, gtolCountsWithAll = NULL; 961 IS cells = patch->cells; 962 IS points = patch->points; 963 PetscSection cellNumbering = patch->cellNumbering; 964 PetscInt Nf = patch->nsubspaces; 965 PetscInt numCells, numPoints; 966 PetscInt numDofs; 967 PetscInt numGlobalDofs, numGlobalDofsWithArtificial, numGlobalDofsWithAll; 968 PetscInt totalDofsPerCell = patch->totalDofsPerCell; 969 PetscInt vStart, vEnd, v; 970 const PetscInt *cellsArray, *pointsArray; 971 PetscInt *newCellsArray = NULL; 972 PetscInt *dofsArray = NULL; 973 PetscInt *dofsArrayWithArtificial = NULL; 974 PetscInt *dofsArrayWithAll = NULL; 975 PetscInt *offsArray = NULL; 976 PetscInt *offsArrayWithArtificial = NULL; 977 PetscInt *offsArrayWithAll = NULL; 978 PetscInt *asmArray = NULL; 979 PetscInt *asmArrayWithArtificial = NULL; 980 PetscInt *asmArrayWithAll = NULL; 981 PetscInt *globalDofsArray = NULL; 982 PetscInt *globalDofsArrayWithArtificial = NULL; 983 PetscInt *globalDofsArrayWithAll = NULL; 984 PetscInt globalIndex = 0; 985 PetscInt key = 0; 986 PetscInt asmKey = 0; 987 DM dm = NULL; 988 const PetscInt *bcNodes = NULL; 989 PetscHMapI ht; 990 PetscHMapI htWithArtificial; 991 PetscHMapI htWithAll; 992 PetscHSetI globalBcs; 993 PetscInt numBcs; 994 PetscHSetI ownedpts, seenpts, owneddofs, seendofs, artificialbcs; 995 PetscInt pStart, pEnd, p, i; 996 char option[PETSC_MAX_PATH_LEN]; 997 PetscBool isNonlinear; 998 PetscErrorCode ierr; 999 1000 PetscFunctionBegin; 1001 1002 ierr = PCGetDM(pc, &dm); CHKERRQ(ierr); 1003 /* dofcounts section is cellcounts section * dofPerCell */ 1004 ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr); 1005 ierr = PetscSectionGetStorageSize(patch->pointCounts, &numPoints);CHKERRQ(ierr); 1006 numDofs = numCells * totalDofsPerCell; 1007 ierr = PetscMalloc1(numDofs, &dofsArray);CHKERRQ(ierr); 1008 ierr = PetscMalloc1(numPoints*Nf, &offsArray);CHKERRQ(ierr); 1009 ierr = PetscMalloc1(numDofs, &asmArray);CHKERRQ(ierr); 1010 ierr = PetscMalloc1(numCells, &newCellsArray);CHKERRQ(ierr); 1011 ierr = PetscSectionGetChart(cellCounts, &vStart, &vEnd);CHKERRQ(ierr); 1012 ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCounts);CHKERRQ(ierr); 1013 gtolCounts = patch->gtolCounts; 1014 ierr = PetscSectionSetChart(gtolCounts, vStart, vEnd);CHKERRQ(ierr); 1015 ierr = PetscObjectSetName((PetscObject) patch->gtolCounts, "Patch Global Index Section");CHKERRQ(ierr); 1016 1017 if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) 1018 { 1019 ierr = PetscMalloc1(numPoints*Nf, &offsArrayWithArtificial);CHKERRQ(ierr); 1020 ierr = PetscMalloc1(numDofs, &asmArrayWithArtificial);CHKERRQ(ierr); 1021 ierr = PetscMalloc1(numDofs, &dofsArrayWithArtificial);CHKERRQ(ierr); 1022 ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithArtificial);CHKERRQ(ierr); 1023 gtolCountsWithArtificial = patch->gtolCountsWithArtificial; 1024 ierr = PetscSectionSetChart(gtolCountsWithArtificial, vStart, vEnd);CHKERRQ(ierr); 1025 ierr = PetscObjectSetName((PetscObject) patch->gtolCountsWithArtificial, "Patch Global Index Section Including Artificial BCs");CHKERRQ(ierr); 1026 } 1027 1028 isNonlinear = patch->isNonlinear; 1029 if(isNonlinear) 1030 { 1031 ierr = PetscMalloc1(numPoints*Nf, &offsArrayWithAll);CHKERRQ(ierr); 1032 ierr = PetscMalloc1(numDofs, &asmArrayWithAll);CHKERRQ(ierr); 1033 ierr = PetscMalloc1(numDofs, &dofsArrayWithAll);CHKERRQ(ierr); 1034 ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithAll);CHKERRQ(ierr); 1035 gtolCountsWithAll = patch->gtolCountsWithAll; 1036 ierr = PetscSectionSetChart(gtolCountsWithAll, vStart, vEnd);CHKERRQ(ierr); 1037 ierr = PetscObjectSetName((PetscObject) patch->gtolCountsWithAll, "Patch Global Index Section Including All BCs");CHKERRQ(ierr); 1038 } 1039 1040 /* Outside the patch loop, get the dofs that are globally-enforced Dirichlet 1041 conditions */ 1042 ierr = PetscHSetICreate(&globalBcs);CHKERRQ(ierr); 1043 ierr = ISGetIndices(patch->ghostBcNodes, &bcNodes); CHKERRQ(ierr); 1044 ierr = ISGetSize(patch->ghostBcNodes, &numBcs); CHKERRQ(ierr); 1045 for (i = 0; i < numBcs; ++i) { 1046 ierr = PetscHSetIAdd(globalBcs, bcNodes[i]);CHKERRQ(ierr); /* these are already in concatenated numbering */ 1047 } 1048 ierr = ISRestoreIndices(patch->ghostBcNodes, &bcNodes); CHKERRQ(ierr); 1049 ierr = ISDestroy(&patch->ghostBcNodes); CHKERRQ(ierr); /* memory optimisation */ 1050 1051 /* Hash tables for artificial BC construction */ 1052 ierr = PetscHSetICreate(&ownedpts);CHKERRQ(ierr); 1053 ierr = PetscHSetICreate(&seenpts);CHKERRQ(ierr); 1054 ierr = PetscHSetICreate(&owneddofs);CHKERRQ(ierr); 1055 ierr = PetscHSetICreate(&seendofs);CHKERRQ(ierr); 1056 ierr = PetscHSetICreate(&artificialbcs);CHKERRQ(ierr); 1057 1058 ierr = ISGetIndices(cells, &cellsArray);CHKERRQ(ierr); 1059 ierr = ISGetIndices(points, &pointsArray);CHKERRQ(ierr); 1060 ierr = PetscHMapICreate(&ht);CHKERRQ(ierr); 1061 ierr = PetscHMapICreate(&htWithArtificial);CHKERRQ(ierr); 1062 ierr = PetscHMapICreate(&htWithAll);CHKERRQ(ierr); 1063 for (v = vStart; v < vEnd; ++v) { 1064 PetscInt localIndex = 0; 1065 PetscInt localIndexWithArtificial = 0; 1066 PetscInt localIndexWithAll = 0; 1067 PetscInt dof, off, i, j, k, l; 1068 1069 ierr = PetscHMapIClear(ht);CHKERRQ(ierr); 1070 ierr = PetscHMapIClear(htWithArtificial);CHKERRQ(ierr); 1071 ierr = PetscHMapIClear(htWithAll);CHKERRQ(ierr); 1072 ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr); 1073 ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr); 1074 if (dof <= 0) continue; 1075 1076 /* Calculate the global numbers of the artificial BC dofs here first */ 1077 ierr = patch->patchconstructop((void*)patch, dm, v, ownedpts); CHKERRQ(ierr); 1078 ierr = PCPatchCompleteCellPatch(pc, ownedpts, seenpts); CHKERRQ(ierr); 1079 ierr = PCPatchGetPointDofs(pc, ownedpts, owneddofs, v, &patch->subspaces_to_exclude); CHKERRQ(ierr); 1080 ierr = PCPatchGetPointDofs(pc, seenpts, seendofs, v, NULL); CHKERRQ(ierr); 1081 ierr = PCPatchComputeSetDifference_Private(owneddofs, seendofs, artificialbcs); CHKERRQ(ierr); 1082 if (patch->viewPatches) { 1083 PetscHSetI globalbcdofs; 1084 PetscHashIter hi; 1085 MPI_Comm comm = PetscObjectComm((PetscObject)pc); 1086 1087 ierr = PetscHSetICreate(&globalbcdofs);CHKERRQ(ierr); 1088 ierr = PetscSynchronizedPrintf(comm, "Patch %d: owned dofs:\n", v); CHKERRQ(ierr); 1089 PetscHashIterBegin(owneddofs, hi); 1090 while (!PetscHashIterAtEnd(owneddofs, hi)) { 1091 PetscInt globalDof; 1092 1093 PetscHashIterGetKey(owneddofs, hi, globalDof); 1094 PetscHashIterNext(owneddofs, hi); 1095 ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr); 1096 } 1097 ierr = PetscSynchronizedPrintf(comm, "\n"); CHKERRQ(ierr); 1098 ierr = PetscSynchronizedPrintf(comm, "Patch %d: seen dofs:\n", v); CHKERRQ(ierr); 1099 PetscHashIterBegin(seendofs, hi); 1100 while (!PetscHashIterAtEnd(seendofs, hi)) { 1101 PetscInt globalDof; 1102 PetscBool flg; 1103 1104 PetscHashIterGetKey(seendofs, hi, globalDof); 1105 PetscHashIterNext(seendofs, hi); 1106 ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr); 1107 1108 ierr = PetscHSetIHas(globalBcs, globalDof, &flg);CHKERRQ(ierr); 1109 if (flg) {ierr = PetscHSetIAdd(globalbcdofs, globalDof);CHKERRQ(ierr);} 1110 } 1111 ierr = PetscSynchronizedPrintf(comm, "\n"); CHKERRQ(ierr); 1112 ierr = PetscSynchronizedPrintf(comm, "Patch %d: global BCs:\n", v); CHKERRQ(ierr); 1113 ierr = PetscHSetIGetSize(globalbcdofs, &numBcs);CHKERRQ(ierr); 1114 if (numBcs > 0) { 1115 PetscHashIterBegin(globalbcdofs, hi); 1116 while (!PetscHashIterAtEnd(globalbcdofs, hi)) { 1117 PetscInt globalDof; 1118 PetscHashIterGetKey(globalbcdofs, hi, globalDof); 1119 PetscHashIterNext(globalbcdofs, hi); 1120 ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof);CHKERRQ(ierr); 1121 } 1122 } 1123 ierr = PetscSynchronizedPrintf(comm, "\n");CHKERRQ(ierr); 1124 ierr = PetscSynchronizedPrintf(comm, "Patch %d: artificial BCs:\n", v);CHKERRQ(ierr); 1125 ierr = PetscHSetIGetSize(artificialbcs, &numBcs);CHKERRQ(ierr); 1126 if (numBcs > 0) { 1127 PetscHashIterBegin(artificialbcs, hi); 1128 while (!PetscHashIterAtEnd(artificialbcs, hi)) { 1129 PetscInt globalDof; 1130 PetscHashIterGetKey(artificialbcs, hi, globalDof); 1131 PetscHashIterNext(artificialbcs, hi); 1132 ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr); 1133 } 1134 } 1135 ierr = PetscSynchronizedPrintf(comm, "\n\n"); CHKERRQ(ierr); 1136 ierr = PetscHSetIDestroy(&globalbcdofs);CHKERRQ(ierr); 1137 } 1138 for (k = 0; k < patch->nsubspaces; ++k) { 1139 const PetscInt *cellNodeMap = patch->cellNodeMap[k]; 1140 PetscInt nodesPerCell = patch->nodesPerCell[k]; 1141 PetscInt subspaceOffset = patch->subspaceOffsets[k]; 1142 PetscInt bs = patch->bs[k]; 1143 1144 for (i = off; i < off + dof; ++i) { 1145 /* Walk over the cells in this patch. */ 1146 const PetscInt c = cellsArray[i]; 1147 PetscInt cell = c; 1148 1149 /* TODO Change this to an IS */ 1150 if (cellNumbering) { 1151 ierr = PetscSectionGetDof(cellNumbering, c, &cell);CHKERRQ(ierr); 1152 if (cell <= 0) SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_OUTOFRANGE, "Cell %D doesn't appear in cell numbering map", c); 1153 ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr); 1154 } 1155 newCellsArray[i] = cell; 1156 for (j = 0; j < nodesPerCell; ++j) { 1157 /* For each global dof, map it into contiguous local storage. */ 1158 const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + subspaceOffset; 1159 /* finally, loop over block size */ 1160 for (l = 0; l < bs; ++l) { 1161 PetscInt localDof; 1162 PetscBool isGlobalBcDof, isArtificialBcDof; 1163 1164 /* first, check if this is either a globally enforced or locally enforced BC dof */ 1165 ierr = PetscHSetIHas(globalBcs, globalDof + l, &isGlobalBcDof);CHKERRQ(ierr); 1166 ierr = PetscHSetIHas(artificialbcs, globalDof + l, &isArtificialBcDof);CHKERRQ(ierr); 1167 1168 /* if it's either, don't ever give it a local dof number */ 1169 if (isGlobalBcDof || isArtificialBcDof) { 1170 dofsArray[globalIndex] = -1; /* don't use this in assembly in this patch */ 1171 } else { 1172 ierr = PetscHMapIGet(ht, globalDof + l, &localDof);CHKERRQ(ierr); 1173 if (localDof == -1) { 1174 localDof = localIndex++; 1175 ierr = PetscHMapISet(ht, globalDof + l, localDof);CHKERRQ(ierr); 1176 } 1177 if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs); 1178 /* And store. */ 1179 dofsArray[globalIndex] = localDof; 1180 } 1181 1182 if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1183 if (isGlobalBcDof) { 1184 dofsArrayWithArtificial[globalIndex] = -1; /* don't use this in assembly in this patch */ 1185 } else { 1186 ierr = PetscHMapIGet(htWithArtificial, globalDof + l, &localDof);CHKERRQ(ierr); 1187 if (localDof == -1) { 1188 localDof = localIndexWithArtificial++; 1189 ierr = PetscHMapISet(htWithArtificial, globalDof + l, localDof);CHKERRQ(ierr); 1190 } 1191 if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs); 1192 /* And store.*/ 1193 dofsArrayWithArtificial[globalIndex] = localDof; 1194 } 1195 } 1196 1197 if(isNonlinear) { 1198 /* Build the dofmap for the function space with _all_ dofs, 1199 including those in any kind of boundary condition */ 1200 ierr = PetscHMapIGet(htWithAll, globalDof + l, &localDof);CHKERRQ(ierr); 1201 if (localDof == -1) { 1202 localDof = localIndexWithAll++; 1203 ierr = PetscHMapISet(htWithAll, globalDof + l, localDof);CHKERRQ(ierr); 1204 } 1205 if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs); 1206 /* And store.*/ 1207 dofsArrayWithAll[globalIndex] = localDof; 1208 } 1209 globalIndex++; 1210 } 1211 } 1212 } 1213 } 1214 /*How many local dofs in this patch? */ 1215 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1216 ierr = PetscHMapIGetSize(htWithArtificial, &dof);CHKERRQ(ierr); 1217 ierr = PetscSectionSetDof(gtolCountsWithArtificial, v, dof);CHKERRQ(ierr); 1218 } 1219 if (isNonlinear) { 1220 ierr = PetscHMapIGetSize(htWithAll, &dof);CHKERRQ(ierr); 1221 ierr = PetscSectionSetDof(gtolCountsWithAll, v, dof);CHKERRQ(ierr); 1222 } 1223 ierr = PetscHMapIGetSize(ht, &dof);CHKERRQ(ierr); 1224 ierr = PetscSectionSetDof(gtolCounts, v, dof);CHKERRQ(ierr); 1225 } 1226 if (globalIndex != numDofs) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Expected number of dofs (%d) doesn't match found number (%d)", numDofs, globalIndex); 1227 ierr = PetscSectionSetUp(gtolCounts);CHKERRQ(ierr); 1228 ierr = PetscSectionGetStorageSize(gtolCounts, &numGlobalDofs);CHKERRQ(ierr); 1229 ierr = PetscMalloc1(numGlobalDofs, &globalDofsArray);CHKERRQ(ierr); 1230 1231 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1232 ierr = PetscSectionSetUp(gtolCountsWithArtificial);CHKERRQ(ierr); 1233 ierr = PetscSectionGetStorageSize(gtolCountsWithArtificial, &numGlobalDofsWithArtificial);CHKERRQ(ierr); 1234 ierr = PetscMalloc1(numGlobalDofsWithArtificial, &globalDofsArrayWithArtificial);CHKERRQ(ierr); 1235 } 1236 if (isNonlinear) { 1237 ierr = PetscSectionSetUp(gtolCountsWithAll);CHKERRQ(ierr); 1238 ierr = PetscSectionGetStorageSize(gtolCountsWithAll, &numGlobalDofsWithAll);CHKERRQ(ierr); 1239 ierr = PetscMalloc1(numGlobalDofsWithAll, &globalDofsArrayWithAll);CHKERRQ(ierr); 1240 } 1241 /* Now populate the global to local map. This could be merged into the above loop if we were willing to deal with reallocs. */ 1242 for (v = vStart; v < vEnd; ++v) { 1243 PetscHashIter hi; 1244 PetscInt dof, off, Np, ooff, i, j, k, l; 1245 1246 ierr = PetscHMapIClear(ht);CHKERRQ(ierr); 1247 ierr = PetscHMapIClear(htWithArtificial);CHKERRQ(ierr); 1248 ierr = PetscHMapIClear(htWithAll);CHKERRQ(ierr); 1249 ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr); 1250 ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr); 1251 ierr = PetscSectionGetDof(pointCounts, v, &Np);CHKERRQ(ierr); 1252 ierr = PetscSectionGetOffset(pointCounts, v, &ooff);CHKERRQ(ierr); 1253 if (dof <= 0) continue; 1254 1255 for (k = 0; k < patch->nsubspaces; ++k) { 1256 const PetscInt *cellNodeMap = patch->cellNodeMap[k]; 1257 PetscInt nodesPerCell = patch->nodesPerCell[k]; 1258 PetscInt subspaceOffset = patch->subspaceOffsets[k]; 1259 PetscInt bs = patch->bs[k]; 1260 PetscInt goff; 1261 1262 for (i = off; i < off + dof; ++i) { 1263 /* Reconstruct mapping of global-to-local on this patch. */ 1264 const PetscInt c = cellsArray[i]; 1265 PetscInt cell = c; 1266 1267 if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);} 1268 for (j = 0; j < nodesPerCell; ++j) { 1269 for (l = 0; l < bs; ++l) { 1270 const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset; 1271 const PetscInt localDof = dofsArray[key]; 1272 if (localDof >= 0) {ierr = PetscHMapISet(ht, globalDof, localDof);CHKERRQ(ierr);} 1273 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1274 const PetscInt localDofWithArtificial = dofsArrayWithArtificial[key]; 1275 if (localDofWithArtificial >= 0) { 1276 ierr = PetscHMapISet(htWithArtificial, globalDof, localDofWithArtificial);CHKERRQ(ierr); 1277 } 1278 } 1279 if (isNonlinear) { 1280 const PetscInt localDofWithAll = dofsArrayWithAll[key]; 1281 if (localDofWithAll >= 0) { 1282 ierr = PetscHMapISet(htWithAll, globalDof, localDofWithAll);CHKERRQ(ierr); 1283 } 1284 } 1285 key++; 1286 } 1287 } 1288 } 1289 1290 /* Shove it in the output data structure. */ 1291 ierr = PetscSectionGetOffset(gtolCounts, v, &goff);CHKERRQ(ierr); 1292 PetscHashIterBegin(ht, hi); 1293 while (!PetscHashIterAtEnd(ht, hi)) { 1294 PetscInt globalDof, localDof; 1295 1296 PetscHashIterGetKey(ht, hi, globalDof); 1297 PetscHashIterGetVal(ht, hi, localDof); 1298 if (globalDof >= 0) globalDofsArray[goff + localDof] = globalDof; 1299 PetscHashIterNext(ht, hi); 1300 } 1301 1302 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1303 ierr = PetscSectionGetOffset(gtolCountsWithArtificial, v, &goff);CHKERRQ(ierr); 1304 PetscHashIterBegin(htWithArtificial, hi); 1305 while (!PetscHashIterAtEnd(htWithArtificial, hi)) { 1306 PetscInt globalDof, localDof; 1307 PetscHashIterGetKey(htWithArtificial, hi, globalDof); 1308 PetscHashIterGetVal(htWithArtificial, hi, localDof); 1309 if (globalDof >= 0) globalDofsArrayWithArtificial[goff + localDof] = globalDof; 1310 PetscHashIterNext(htWithArtificial, hi); 1311 } 1312 } 1313 if (isNonlinear) { 1314 ierr = PetscSectionGetOffset(gtolCountsWithAll, v, &goff);CHKERRQ(ierr); 1315 PetscHashIterBegin(htWithAll, hi); 1316 while (!PetscHashIterAtEnd(htWithAll, hi)) { 1317 PetscInt globalDof, localDof; 1318 PetscHashIterGetKey(htWithAll, hi, globalDof); 1319 PetscHashIterGetVal(htWithAll, hi, localDof); 1320 if (globalDof >= 0) globalDofsArrayWithAll[goff + localDof] = globalDof; 1321 PetscHashIterNext(htWithAll, hi); 1322 } 1323 } 1324 1325 for (p = 0; p < Np; ++p) { 1326 const PetscInt point = pointsArray[ooff + p]; 1327 PetscInt globalDof, localDof; 1328 1329 ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, point, NULL, &globalDof);CHKERRQ(ierr); 1330 ierr = PetscHMapIGet(ht, globalDof, &localDof);CHKERRQ(ierr); 1331 offsArray[(ooff + p)*Nf + k] = localDof; 1332 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1333 ierr = PetscHMapIGet(htWithArtificial, globalDof, &localDof);CHKERRQ(ierr); 1334 offsArrayWithArtificial[(ooff + p)*Nf + k] = localDof; 1335 } 1336 if (isNonlinear) { 1337 ierr = PetscHMapIGet(htWithAll, globalDof, &localDof);CHKERRQ(ierr); 1338 offsArrayWithAll[(ooff + p)*Nf + k] = localDof; 1339 } 1340 } 1341 } 1342 1343 ierr = PetscHSetIDestroy(&globalBcs);CHKERRQ(ierr); 1344 ierr = PetscHSetIDestroy(&ownedpts);CHKERRQ(ierr); 1345 ierr = PetscHSetIDestroy(&seenpts);CHKERRQ(ierr); 1346 ierr = PetscHSetIDestroy(&owneddofs);CHKERRQ(ierr); 1347 ierr = PetscHSetIDestroy(&seendofs);CHKERRQ(ierr); 1348 ierr = PetscHSetIDestroy(&artificialbcs);CHKERRQ(ierr); 1349 1350 /* At this point, we have a hash table ht built that maps globalDof -> localDof. 1351 We need to create the dof table laid out cellwise first, then by subspace, 1352 as the assembler assembles cell-wise and we need to stuff the different 1353 contributions of the different function spaces to the right places. So we loop 1354 over cells, then over subspaces. */ 1355 if (patch->nsubspaces > 1) { /* for nsubspaces = 1, data we need is already in dofsArray */ 1356 for (i = off; i < off + dof; ++i) { 1357 const PetscInt c = cellsArray[i]; 1358 PetscInt cell = c; 1359 1360 if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);} 1361 for (k = 0; k < patch->nsubspaces; ++k) { 1362 const PetscInt *cellNodeMap = patch->cellNodeMap[k]; 1363 PetscInt nodesPerCell = patch->nodesPerCell[k]; 1364 PetscInt subspaceOffset = patch->subspaceOffsets[k]; 1365 PetscInt bs = patch->bs[k]; 1366 1367 for (j = 0; j < nodesPerCell; ++j) { 1368 for (l = 0; l < bs; ++l) { 1369 const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset; 1370 PetscInt localDof; 1371 1372 ierr = PetscHMapIGet(ht, globalDof, &localDof);CHKERRQ(ierr); 1373 /* If it's not in the hash table, i.e. is a BC dof, 1374 then the PetscHSetIMap above gives -1, which matches 1375 exactly the convention for PETSc's matrix assembly to 1376 ignore the dof. So we don't need to do anything here */ 1377 asmArray[asmKey] = localDof; 1378 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1379 ierr = PetscHMapIGet(htWithArtificial, globalDof, &localDof);CHKERRQ(ierr); 1380 asmArrayWithArtificial[asmKey] = localDof; 1381 } 1382 if (isNonlinear) { 1383 ierr = PetscHMapIGet(htWithAll, globalDof, &localDof);CHKERRQ(ierr); 1384 asmArrayWithAll[asmKey] = localDof; 1385 } 1386 asmKey++; 1387 } 1388 } 1389 } 1390 } 1391 } 1392 } 1393 if (1 == patch->nsubspaces) { 1394 ierr = PetscMemcpy(asmArray, dofsArray, numDofs * sizeof(PetscInt));CHKERRQ(ierr); 1395 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1396 ierr = PetscMemcpy(asmArrayWithArtificial, dofsArrayWithArtificial, numDofs * sizeof(PetscInt));CHKERRQ(ierr); 1397 } 1398 if (isNonlinear) { 1399 ierr = PetscMemcpy(asmArrayWithAll, dofsArrayWithAll, numDofs * sizeof(PetscInt));CHKERRQ(ierr); 1400 } 1401 } 1402 1403 ierr = PetscHMapIDestroy(&ht);CHKERRQ(ierr); 1404 ierr = PetscHMapIDestroy(&htWithArtificial);CHKERRQ(ierr); 1405 ierr = PetscHMapIDestroy(&htWithAll);CHKERRQ(ierr); 1406 ierr = ISRestoreIndices(cells, &cellsArray);CHKERRQ(ierr); 1407 ierr = ISRestoreIndices(points, &pointsArray);CHKERRQ(ierr); 1408 ierr = PetscFree(dofsArray);CHKERRQ(ierr); 1409 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1410 ierr = PetscFree(dofsArrayWithArtificial);CHKERRQ(ierr); 1411 } 1412 if (isNonlinear) { 1413 ierr = PetscFree(dofsArrayWithAll);CHKERRQ(ierr); 1414 } 1415 /* Create placeholder section for map from points to patch dofs */ 1416 ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->patchSection);CHKERRQ(ierr); 1417 ierr = PetscSectionSetNumFields(patch->patchSection, patch->nsubspaces);CHKERRQ(ierr); 1418 if (patch->combined) { 1419 PetscInt numFields; 1420 ierr = PetscSectionGetNumFields(patch->dofSection[0], &numFields);CHKERRQ(ierr); 1421 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); 1422 ierr = PetscSectionGetChart(patch->dofSection[0], &pStart, &pEnd);CHKERRQ(ierr); 1423 ierr = PetscSectionSetChart(patch->patchSection, pStart, pEnd);CHKERRQ(ierr); 1424 for (p = pStart; p < pEnd; ++p) { 1425 PetscInt dof, fdof, f; 1426 1427 ierr = PetscSectionGetDof(patch->dofSection[0], p, &dof);CHKERRQ(ierr); 1428 ierr = PetscSectionSetDof(patch->patchSection, p, dof);CHKERRQ(ierr); 1429 for (f = 0; f < patch->nsubspaces; ++f) { 1430 ierr = PetscSectionGetFieldDof(patch->dofSection[0], p, f, &fdof);CHKERRQ(ierr); 1431 ierr = PetscSectionSetFieldDof(patch->patchSection, p, f, fdof);CHKERRQ(ierr); 1432 } 1433 } 1434 } else { 1435 PetscInt pStartf, pEndf, f; 1436 pStart = PETSC_MAX_INT; 1437 pEnd = PETSC_MIN_INT; 1438 for (f = 0; f < patch->nsubspaces; ++f) { 1439 ierr = PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf);CHKERRQ(ierr); 1440 pStart = PetscMin(pStart, pStartf); 1441 pEnd = PetscMax(pEnd, pEndf); 1442 } 1443 ierr = PetscSectionSetChart(patch->patchSection, pStart, pEnd);CHKERRQ(ierr); 1444 for (f = 0; f < patch->nsubspaces; ++f) { 1445 ierr = PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf);CHKERRQ(ierr); 1446 for (p = pStartf; p < pEndf; ++p) { 1447 PetscInt fdof; 1448 ierr = PetscSectionGetDof(patch->dofSection[f], p, &fdof);CHKERRQ(ierr); 1449 ierr = PetscSectionAddDof(patch->patchSection, p, fdof);CHKERRQ(ierr); 1450 ierr = PetscSectionSetFieldDof(patch->patchSection, p, f, fdof);CHKERRQ(ierr); 1451 } 1452 } 1453 } 1454 ierr = PetscSectionSetUp(patch->patchSection);CHKERRQ(ierr); 1455 ierr = PetscSectionSetUseFieldOffsets(patch->patchSection, PETSC_TRUE);CHKERRQ(ierr); 1456 /* Replace cell indices with firedrake-numbered ones. */ 1457 ierr = ISGeneralSetIndices(cells, numCells, (const PetscInt *) newCellsArray, PETSC_OWN_POINTER);CHKERRQ(ierr); 1458 ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofs, globalDofsArray, PETSC_OWN_POINTER, &patch->gtol);CHKERRQ(ierr); 1459 ierr = PetscObjectSetName((PetscObject) patch->gtol, "Global Indices");CHKERRQ(ierr); 1460 ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_g2l_view", patch->classname);CHKERRQ(ierr); 1461 ierr = PetscSectionViewFromOptions(patch->gtolCounts, (PetscObject) pc, option);CHKERRQ(ierr); 1462 ierr = ISViewFromOptions(patch->gtol, (PetscObject) pc, option);CHKERRQ(ierr); 1463 ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArray, PETSC_OWN_POINTER, &patch->dofs);CHKERRQ(ierr); 1464 ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArray, PETSC_OWN_POINTER, &patch->offs);CHKERRQ(ierr); 1465 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1466 ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithArtificial, globalDofsArrayWithArtificial, PETSC_OWN_POINTER, &patch->gtolWithArtificial);CHKERRQ(ierr); 1467 ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithArtificial, PETSC_OWN_POINTER, &patch->dofsWithArtificial);CHKERRQ(ierr); 1468 ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithArtificial, PETSC_OWN_POINTER, &patch->offsWithArtificial);CHKERRQ(ierr); 1469 } 1470 if (isNonlinear) { 1471 ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithAll, globalDofsArrayWithAll, PETSC_OWN_POINTER, &patch->gtolWithAll);CHKERRQ(ierr); 1472 ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithAll, PETSC_OWN_POINTER, &patch->dofsWithAll);CHKERRQ(ierr); 1473 ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithAll, PETSC_OWN_POINTER, &patch->offsWithAll);CHKERRQ(ierr); 1474 } 1475 PetscFunctionReturn(0); 1476 } 1477 1478 static PetscErrorCode PCPatchZeroFillMatrix_Private(Mat mat, const PetscInt ncell, const PetscInt ndof, const PetscInt *dof) 1479 { 1480 PetscScalar *values = NULL; 1481 PetscInt rows, c, i; 1482 PetscErrorCode ierr; 1483 1484 PetscFunctionBegin; 1485 ierr = PetscCalloc1(ndof*ndof, &values);CHKERRQ(ierr); 1486 for (c = 0; c < ncell; ++c) { 1487 const PetscInt *idx = &dof[ndof*c]; 1488 ierr = MatSetValues(mat, ndof, idx, ndof, idx, values, INSERT_VALUES);CHKERRQ(ierr); 1489 } 1490 ierr = MatGetLocalSize(mat, &rows, NULL);CHKERRQ(ierr); 1491 for (i = 0; i < rows; ++i) { 1492 ierr = MatSetValues(mat, 1, &i, 1, &i, values, INSERT_VALUES);CHKERRQ(ierr); 1493 } 1494 ierr = MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1495 ierr = MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1496 ierr = PetscFree(values);CHKERRQ(ierr); 1497 PetscFunctionReturn(0); 1498 } 1499 1500 static PetscErrorCode PCPatchCreateMatrix_Private(PC pc, PetscInt point, Mat *mat, PetscBool withArtificial) 1501 { 1502 PC_PATCH *patch = (PC_PATCH *) pc->data; 1503 Vec x, y; 1504 PetscBool flg; 1505 PetscInt csize, rsize; 1506 const char *prefix = NULL; 1507 PetscErrorCode ierr; 1508 1509 PetscFunctionBegin; 1510 if(withArtificial) { 1511 /* would be nice if we could create a rectangular matrix of size numDofsWithArtificial x numDofs here */ 1512 x = patch->patchRHSWithArtificial[point]; 1513 y = patch->patchRHSWithArtificial[point]; 1514 } else { 1515 x = patch->patchRHS[point]; 1516 y = patch->patchUpdate[point]; 1517 } 1518 1519 ierr = VecGetSize(x, &csize);CHKERRQ(ierr); 1520 ierr = VecGetSize(y, &rsize);CHKERRQ(ierr); 1521 ierr = MatCreate(PETSC_COMM_SELF, mat);CHKERRQ(ierr); 1522 ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr); 1523 ierr = MatSetOptionsPrefix(*mat, prefix);CHKERRQ(ierr); 1524 ierr = MatAppendOptionsPrefix(*mat, "pc_patch_sub_");CHKERRQ(ierr); 1525 if (patch->sub_mat_type) {ierr = MatSetType(*mat, patch->sub_mat_type);CHKERRQ(ierr);} 1526 else if (!patch->sub_mat_type) {ierr = MatSetType(*mat, MATDENSE);CHKERRQ(ierr);} 1527 ierr = MatSetSizes(*mat, rsize, csize, rsize, csize);CHKERRQ(ierr); 1528 ierr = PetscObjectTypeCompare((PetscObject) *mat, MATDENSE, &flg);CHKERRQ(ierr); 1529 if (!flg) {ierr = PetscObjectTypeCompare((PetscObject)*mat, MATSEQDENSE, &flg);CHKERRQ(ierr);} 1530 /* Sparse patch matrices */ 1531 if (!flg) { 1532 PetscBT bt; 1533 PetscInt *dnnz = NULL; 1534 const PetscInt *dofsArray = NULL; 1535 PetscInt pStart, pEnd, ncell, offset, c, i, j; 1536 1537 if(withArtificial) { 1538 ierr = ISGetIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr); 1539 } else { 1540 ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr); 1541 } 1542 ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr); 1543 point += pStart; 1544 if (point >= pEnd) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %D not in [%D, %D)\n", point, pStart, pEnd);CHKERRQ(ierr); 1545 ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr); 1546 ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr); 1547 ierr = PetscLogEventBegin(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr); 1548 /* A PetscBT uses N^2 bits to store the sparsity pattern on a 1549 * patch. This is probably OK if the patches are not too big, 1550 * but uses too much memory. We therefore switch based on rsize. */ 1551 if (rsize < 3000) { /* FIXME: I picked this switch value out of my hat */ 1552 ierr = PetscCalloc1(rsize, &dnnz);CHKERRQ(ierr); 1553 ierr = PetscBTCreate(rsize*rsize, &bt);CHKERRQ(ierr); 1554 for (c = 0; c < ncell; ++c) { 1555 const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell; 1556 for (i = 0; i < patch->totalDofsPerCell; ++i) { 1557 const PetscInt row = idx[i]; 1558 if (row < 0) continue; 1559 for (j = 0; j < patch->totalDofsPerCell; ++j) { 1560 const PetscInt col = idx[j]; 1561 const PetscInt key = row*rsize + col; 1562 if (col < 0) continue; 1563 if (!PetscBTLookupSet(bt, key)) ++dnnz[row]; 1564 } 1565 } 1566 } 1567 ierr = PetscBTDestroy(&bt);CHKERRQ(ierr); 1568 ierr = MatXAIJSetPreallocation(*mat, 1, dnnz, NULL, NULL, NULL);CHKERRQ(ierr); 1569 ierr = PetscFree(dnnz);CHKERRQ(ierr); 1570 ierr = PCPatchZeroFillMatrix_Private(*mat, ncell, patch->totalDofsPerCell, &dofsArray[offset*patch->totalDofsPerCell]);CHKERRQ(ierr); 1571 } else { /* rsize too big, use MATPREALLOCATOR */ 1572 Mat preallocator; 1573 PetscScalar* vals; 1574 1575 ierr = PetscCalloc1(patch->totalDofsPerCell*patch->totalDofsPerCell, &vals);CHKERRQ(ierr); 1576 ierr = MatCreate(PETSC_COMM_SELF, &preallocator);CHKERRQ(ierr); 1577 ierr = MatSetType(preallocator, MATPREALLOCATOR);CHKERRQ(ierr); 1578 ierr = MatSetSizes(preallocator, rsize, rsize, rsize, rsize);CHKERRQ(ierr); 1579 ierr = MatSetUp(preallocator);CHKERRQ(ierr); 1580 for (c = 0; c < ncell; ++c) { 1581 const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell; 1582 ierr = MatSetValues(preallocator, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, vals, INSERT_VALUES);CHKERRQ(ierr); 1583 } 1584 ierr = PetscFree(vals);CHKERRQ(ierr); 1585 ierr = MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1586 ierr = MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1587 ierr = MatPreallocatorPreallocate(preallocator, PETSC_TRUE, *mat);CHKERRQ(ierr); 1588 ierr = MatDestroy(&preallocator);CHKERRQ(ierr); 1589 } 1590 ierr = PetscLogEventEnd(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr); 1591 if(withArtificial) { 1592 ierr = ISRestoreIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr); 1593 } else { 1594 ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr); 1595 } 1596 } 1597 ierr = MatSetUp(*mat);CHKERRQ(ierr); 1598 PetscFunctionReturn(0); 1599 } 1600 1601 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) 1602 { 1603 PC_PATCH *patch = (PC_PATCH *) pc->data; 1604 DM dm; 1605 PetscSection s; 1606 const PetscInt *parray, *oarray; 1607 PetscInt Nf = patch->nsubspaces, Np, poff, p, f; 1608 PetscErrorCode ierr; 1609 1610 PetscFunctionBegin; 1611 ierr = PCGetDM(pc, &dm);CHKERRQ(ierr); 1612 ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr); 1613 /* Set offset into patch */ 1614 ierr = PetscSectionGetDof(patch->pointCounts, patchNum, &Np);CHKERRQ(ierr); 1615 ierr = PetscSectionGetOffset(patch->pointCounts, patchNum, &poff);CHKERRQ(ierr); 1616 ierr = ISGetIndices(patch->points, &parray);CHKERRQ(ierr); 1617 ierr = ISGetIndices(patch->offs, &oarray);CHKERRQ(ierr); 1618 for (f = 0; f < Nf; ++f) { 1619 for (p = 0; p < Np; ++p) { 1620 const PetscInt point = parray[poff+p]; 1621 PetscInt dof; 1622 1623 ierr = PetscSectionGetFieldDof(patch->patchSection, point, f, &dof);CHKERRQ(ierr); 1624 ierr = PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr); 1625 if (patch->nsubspaces == 1) {ierr = PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);} 1626 else {ierr = PetscSectionSetOffset(patch->patchSection, point, -1);CHKERRQ(ierr);} 1627 } 1628 } 1629 ierr = ISRestoreIndices(patch->points, &parray);CHKERRQ(ierr); 1630 ierr = ISRestoreIndices(patch->offs, &oarray);CHKERRQ(ierr); 1631 if (patch->viewSection) {ierr = ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection);CHKERRQ(ierr);} 1632 ierr = DMPlexComputeResidual_Patch_Internal(pc->dm, patch->patchSection, cellIS, 0.0, x, NULL, F, ctx);CHKERRQ(ierr); 1633 PetscFunctionReturn(0); 1634 } 1635 1636 PetscErrorCode PCPatchComputeFunction_Internal(PC pc, Vec x, Vec F, PetscInt point) 1637 { 1638 PC_PATCH *patch = (PC_PATCH *) pc->data; 1639 const PetscInt *dofsArray; 1640 const PetscInt *dofsArrayWithAll; 1641 const PetscInt *cellsArray; 1642 PetscInt ncell, offset, pStart, pEnd; 1643 PetscErrorCode ierr; 1644 1645 PetscFunctionBegin; 1646 ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr); 1647 if (!patch->usercomputeop) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback\n"); 1648 ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr); 1649 ierr = ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr); 1650 ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr); 1651 ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr); 1652 1653 point += pStart; 1654 if (point >= pEnd) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %D not in [%D, %D)\n", point, pStart, pEnd);CHKERRQ(ierr); 1655 1656 ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr); 1657 ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr); 1658 if (ncell <= 0) { 1659 ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr); 1660 PetscFunctionReturn(0); 1661 } 1662 PetscStackPush("PCPatch user callback"); 1663 /* Cannot reuse the same IS because the geometry info is being cached in it */ 1664 ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS);CHKERRQ(ierr); 1665 ierr = patch->usercomputef(pc, point, x, F, patch->cellIS, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell, 1666 dofsArrayWithAll + offset*patch->totalDofsPerCell, 1667 patch->usercomputefctx);CHKERRQ(ierr); 1668 PetscStackPop; 1669 ierr = ISDestroy(&patch->cellIS);CHKERRQ(ierr); 1670 ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr); 1671 ierr = ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr); 1672 ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr); 1673 if (patch->viewMatrix) { 1674 char name[PETSC_MAX_PATH_LEN]; 1675 1676 ierr = PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch vector for Point %D", point);CHKERRQ(ierr); 1677 ierr = PetscObjectSetName((PetscObject) F, name);CHKERRQ(ierr); 1678 ierr = ObjectView((PetscObject) F, patch->viewerMatrix, patch->formatMatrix);CHKERRQ(ierr); 1679 } 1680 ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr); 1681 PetscFunctionReturn(0); 1682 } 1683 1684 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) 1685 { 1686 PC_PATCH *patch = (PC_PATCH *) pc->data; 1687 DM dm; 1688 PetscSection s; 1689 const PetscInt *parray, *oarray; 1690 PetscInt Nf = patch->nsubspaces, Np, poff, p, f; 1691 PetscErrorCode ierr; 1692 1693 PetscFunctionBegin; 1694 ierr = PCGetDM(pc, &dm);CHKERRQ(ierr); 1695 ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr); 1696 /* Set offset into patch */ 1697 ierr = PetscSectionGetDof(patch->pointCounts, patchNum, &Np);CHKERRQ(ierr); 1698 ierr = PetscSectionGetOffset(patch->pointCounts, patchNum, &poff);CHKERRQ(ierr); 1699 ierr = ISGetIndices(patch->points, &parray);CHKERRQ(ierr); 1700 ierr = ISGetIndices(patch->offs, &oarray);CHKERRQ(ierr); 1701 for (f = 0; f < Nf; ++f) { 1702 for (p = 0; p < Np; ++p) { 1703 const PetscInt point = parray[poff+p]; 1704 PetscInt dof; 1705 1706 ierr = PetscSectionGetFieldDof(patch->patchSection, point, f, &dof);CHKERRQ(ierr); 1707 ierr = PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr); 1708 if (patch->nsubspaces == 1) {ierr = PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);} 1709 else {ierr = PetscSectionSetOffset(patch->patchSection, point, -1);CHKERRQ(ierr);} 1710 } 1711 } 1712 ierr = ISRestoreIndices(patch->points, &parray);CHKERRQ(ierr); 1713 ierr = ISRestoreIndices(patch->offs, &oarray);CHKERRQ(ierr); 1714 if (patch->viewSection) {ierr = ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection);CHKERRQ(ierr);} 1715 /* TODO Shut off MatViewFromOptions() in MatAssemblyEnd() here */ 1716 ierr = DMPlexComputeJacobian_Patch_Internal(pc->dm, patch->patchSection, patch->patchSection, cellIS, 0.0, 0.0, x, NULL, J, J, ctx);CHKERRQ(ierr); 1717 PetscFunctionReturn(0); 1718 } 1719 1720 PetscErrorCode PCPatchComputeOperator_Internal(PC pc, Vec x, Mat mat, PetscInt point, PetscBool withArtificial) 1721 { 1722 PC_PATCH *patch = (PC_PATCH *) pc->data; 1723 const PetscInt *dofsArray; 1724 const PetscInt *dofsArrayWithArtificial = NULL; 1725 const PetscInt *dofsArrayWithAll = NULL; 1726 const PetscInt *cellsArray; 1727 PetscInt ncell, offset, pStart, pEnd; 1728 PetscBool isNonlinear; 1729 PetscErrorCode ierr; 1730 1731 PetscFunctionBegin; 1732 ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr); 1733 isNonlinear = patch->isNonlinear; 1734 if (!patch->usercomputeop) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback\n"); 1735 if(withArtificial) { 1736 ierr = ISGetIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr); 1737 } else { 1738 ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr); 1739 } 1740 if (isNonlinear) { 1741 ierr = ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr); 1742 } 1743 ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr); 1744 ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr); 1745 1746 point += pStart; 1747 if (point >= pEnd) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %D not in [%D, %D)\n", point, pStart, pEnd);CHKERRQ(ierr); 1748 1749 ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr); 1750 ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr); 1751 if (ncell <= 0) { 1752 ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr); 1753 PetscFunctionReturn(0); 1754 } 1755 PetscStackPush("PCPatch user callback"); 1756 /* Cannot reuse the same IS because the geometry info is being cached in it */ 1757 ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS);CHKERRQ(ierr); 1758 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); 1759 PetscStackPop; 1760 ierr = ISDestroy(&patch->cellIS);CHKERRQ(ierr); 1761 if(withArtificial) { 1762 ierr = ISRestoreIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr); 1763 } else { 1764 ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr); 1765 } 1766 if (isNonlinear) { 1767 ierr = ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr); 1768 } 1769 ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr); 1770 if (patch->viewMatrix) { 1771 char name[PETSC_MAX_PATH_LEN]; 1772 1773 ierr = PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch matrix for Point %D", point);CHKERRQ(ierr); 1774 ierr = PetscObjectSetName((PetscObject) mat, name);CHKERRQ(ierr); 1775 ierr = ObjectView((PetscObject) mat, patch->viewerMatrix, patch->formatMatrix);CHKERRQ(ierr); 1776 } 1777 ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr); 1778 PetscFunctionReturn(0); 1779 } 1780 1781 PetscErrorCode PCPatch_ScatterLocal_Private(PC pc, PetscInt p, Vec x, Vec y, InsertMode mode, ScatterMode scat, PatchScatterType scattertype) 1782 { 1783 PC_PATCH *patch = (PC_PATCH *) pc->data; 1784 const PetscScalar *xArray = NULL; 1785 PetscScalar *yArray = NULL; 1786 const PetscInt *gtolArray = NULL; 1787 PetscInt dof, offset, lidx; 1788 PetscErrorCode ierr; 1789 1790 PetscFunctionBeginHot; 1791 ierr = PetscLogEventBegin(PC_Patch_Scatter, pc, 0, 0, 0);CHKERRQ(ierr); 1792 ierr = VecGetArrayRead(x, &xArray);CHKERRQ(ierr); 1793 ierr = VecGetArray(y, &yArray);CHKERRQ(ierr); 1794 if (scattertype == SCATTER_WITHARTIFICIAL) { 1795 ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof);CHKERRQ(ierr); 1796 ierr = PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offset);CHKERRQ(ierr); 1797 ierr = ISGetIndices(patch->gtolWithArtificial, >olArray);CHKERRQ(ierr); 1798 } else if (scattertype == SCATTER_WITHALL) { 1799 ierr = PetscSectionGetDof(patch->gtolCountsWithAll, p, &dof);CHKERRQ(ierr); 1800 ierr = PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offset);CHKERRQ(ierr); 1801 ierr = ISGetIndices(patch->gtolWithAll, >olArray);CHKERRQ(ierr); 1802 } else { 1803 ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr); 1804 ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr); 1805 ierr = ISGetIndices(patch->gtol, >olArray);CHKERRQ(ierr); 1806 } 1807 if (mode == INSERT_VALUES && scat != SCATTER_FORWARD) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't insert if not scattering forward\n"); 1808 if (mode == ADD_VALUES && scat != SCATTER_REVERSE) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't add if not scattering reverse\n"); 1809 for (lidx = 0; lidx < dof; ++lidx) { 1810 const PetscInt gidx = gtolArray[offset+lidx]; 1811 1812 if (mode == INSERT_VALUES) yArray[lidx] = xArray[gidx]; /* Forward */ 1813 else yArray[gidx] += xArray[lidx]; /* Reverse */ 1814 } 1815 if (scattertype == SCATTER_WITHARTIFICIAL) { 1816 ierr = ISRestoreIndices(patch->gtolWithArtificial, >olArray);CHKERRQ(ierr); 1817 } else if (scattertype == SCATTER_WITHALL) { 1818 ierr = ISRestoreIndices(patch->gtolWithAll, >olArray);CHKERRQ(ierr); 1819 } else { 1820 ierr = ISRestoreIndices(patch->gtol, >olArray);CHKERRQ(ierr); 1821 } 1822 ierr = VecRestoreArrayRead(x, &xArray);CHKERRQ(ierr); 1823 ierr = VecRestoreArray(y, &yArray);CHKERRQ(ierr); 1824 ierr = PetscLogEventEnd(PC_Patch_Scatter, pc, 0, 0, 0);CHKERRQ(ierr); 1825 PetscFunctionReturn(0); 1826 } 1827 1828 static PetscErrorCode PCSetUp_PATCH_Linear(PC pc) 1829 { 1830 PC_PATCH *patch = (PC_PATCH *) pc->data; 1831 const char *prefix; 1832 PetscInt i; 1833 PetscErrorCode ierr; 1834 1835 PetscFunctionBegin; 1836 if (!pc->setupcalled) { 1837 ierr = PetscMalloc1(patch->npatch, &patch->solver);CHKERRQ(ierr); 1838 ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr); 1839 for (i = 0; i < patch->npatch; ++i) { 1840 KSP ksp; 1841 PC subpc; 1842 1843 ierr = KSPCreate(PETSC_COMM_SELF, &ksp);CHKERRQ(ierr); 1844 ierr = KSPSetErrorIfNotConverged(ksp, pc->erroriffailure);CHKERRQ(ierr); 1845 ierr = KSPSetOptionsPrefix(ksp, prefix);CHKERRQ(ierr); 1846 ierr = KSPAppendOptionsPrefix(ksp, "sub_");CHKERRQ(ierr); 1847 ierr = PetscObjectIncrementTabLevel((PetscObject) ksp, (PetscObject) pc, 1);CHKERRQ(ierr); 1848 ierr = KSPGetPC(ksp, &subpc);CHKERRQ(ierr); 1849 ierr = PetscObjectIncrementTabLevel((PetscObject) subpc, (PetscObject) pc, 1);CHKERRQ(ierr); 1850 ierr = PetscLogObjectParent((PetscObject) pc, (PetscObject) ksp);CHKERRQ(ierr); 1851 patch->solver[i] = (PetscObject) ksp; 1852 } 1853 } 1854 if (patch->save_operators) { 1855 for (i = 0; i < patch->npatch; ++i) { 1856 ierr = MatZeroEntries(patch->mat[i]);CHKERRQ(ierr); 1857 ierr = PCPatchComputeOperator_Internal(pc, NULL, patch->mat[i], i, PETSC_FALSE);CHKERRQ(ierr); 1858 ierr = KSPSetOperators((KSP) patch->solver[i], patch->mat[i], patch->mat[i]);CHKERRQ(ierr); 1859 } 1860 } 1861 if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1862 for (i = 0; i < patch->npatch; ++i) { 1863 /* Instead of padding patch->patchUpdate with zeros to get */ 1864 /* patch->patchUpdateWithArtificial and then multiplying with the matrix, */ 1865 /* just get rid of the columns that correspond to the dofs with */ 1866 /* artificial bcs. That's of course fairly inefficient, hopefully we */ 1867 /* can just assemble the rectangular matrix in the first place. */ 1868 Mat matSquare; 1869 IS rowis; 1870 PetscInt dof; 1871 1872 ierr = MatGetSize(patch->mat[i], &dof, NULL);CHKERRQ(ierr); 1873 if (dof == 0) { 1874 patch->matWithArtificial[i] = NULL; 1875 continue; 1876 } 1877 1878 ierr = PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE);CHKERRQ(ierr); 1879 ierr = MatZeroEntries(matSquare);CHKERRQ(ierr); 1880 ierr = PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE);CHKERRQ(ierr); 1881 1882 ierr = MatGetSize(matSquare, &dof, NULL);CHKERRQ(ierr); 1883 ierr = ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis); CHKERRQ(ierr); 1884 if(pc->setupcalled) { 1885 ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_REUSE_MATRIX, &patch->matWithArtificial[i]); CHKERRQ(ierr); 1886 } else { 1887 ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &patch->matWithArtificial[i]); CHKERRQ(ierr); 1888 } 1889 ierr = ISDestroy(&rowis); CHKERRQ(ierr); 1890 ierr = MatDestroy(&matSquare);CHKERRQ(ierr); 1891 } 1892 } 1893 PetscFunctionReturn(0); 1894 } 1895 1896 static PetscErrorCode PCSetUp_PATCH(PC pc) 1897 { 1898 PC_PATCH *patch = (PC_PATCH *) pc->data; 1899 PetscInt i; 1900 PetscBool isNonlinear; 1901 PetscErrorCode ierr; 1902 1903 PetscFunctionBegin; 1904 if (!pc->setupcalled) { 1905 PetscInt pStart, pEnd, p; 1906 PetscInt localSize; 1907 1908 ierr = PetscLogEventBegin(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr); 1909 1910 isNonlinear = patch->isNonlinear; 1911 if (!patch->nsubspaces) { 1912 DM dm; 1913 PetscDS prob; 1914 PetscSection s; 1915 PetscInt cStart, cEnd, c, Nf, f, numGlobalBcs = 0, *globalBcs, *Nb, totNb = 0, **cellDofs; 1916 1917 ierr = PCGetDM(pc, &dm);CHKERRQ(ierr); 1918 if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONG, "Must set DM for PCPATCH or call PCPatchSetDiscretisationInfo()"); 1919 ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr); 1920 ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr); 1921 ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr); 1922 for (p = pStart; p < pEnd; ++p) { 1923 PetscInt cdof; 1924 ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr); 1925 numGlobalBcs += cdof; 1926 } 1927 ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1928 ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1929 ierr = PetscMalloc3(Nf, &Nb, Nf, &cellDofs, numGlobalBcs, &globalBcs);CHKERRQ(ierr); 1930 for (f = 0; f < Nf; ++f) { 1931 PetscFE fe; 1932 PetscDualSpace sp; 1933 PetscInt cdoff = 0; 1934 1935 ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 1936 /* ierr = PetscFEGetNumComponents(fe, &Nc[f]);CHKERRQ(ierr); */ 1937 ierr = PetscFEGetDualSpace(fe, &sp);CHKERRQ(ierr); 1938 ierr = PetscDualSpaceGetDimension(sp, &Nb[f]);CHKERRQ(ierr); 1939 totNb += Nb[f]; 1940 1941 ierr = PetscMalloc1((cEnd-cStart)*Nb[f], &cellDofs[f]);CHKERRQ(ierr); 1942 for (c = cStart; c < cEnd; ++c) { 1943 PetscInt *closure = NULL; 1944 PetscInt clSize = 0, cl; 1945 1946 ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr); 1947 for (cl = 0; cl < clSize*2; cl += 2) { 1948 const PetscInt p = closure[cl]; 1949 PetscInt fdof, d, foff; 1950 1951 ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr); 1952 ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr); 1953 for (d = 0; d < fdof; ++d, ++cdoff) cellDofs[f][cdoff] = foff + d; 1954 } 1955 ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr); 1956 } 1957 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]); 1958 } 1959 numGlobalBcs = 0; 1960 for (p = pStart; p < pEnd; ++p) { 1961 const PetscInt *ind; 1962 PetscInt off, cdof, d; 1963 1964 ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr); 1965 ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr); 1966 ierr = PetscSectionGetConstraintIndices(s, p, &ind);CHKERRQ(ierr); 1967 for (d = 0; d < cdof; ++d) globalBcs[numGlobalBcs++] = off + ind[d]; 1968 } 1969 1970 ierr = PCPatchSetDiscretisationInfoCombined(pc, dm, Nb, (const PetscInt **) cellDofs, numGlobalBcs, globalBcs, numGlobalBcs, globalBcs);CHKERRQ(ierr); 1971 for (f = 0; f < Nf; ++f) { 1972 ierr = PetscFree(cellDofs[f]);CHKERRQ(ierr); 1973 } 1974 ierr = PetscFree3(Nb, cellDofs, globalBcs);CHKERRQ(ierr); 1975 ierr = PCPatchSetComputeFunction(pc, PCPatchComputeFunction_DMPlex_Private, NULL);CHKERRQ(ierr); 1976 ierr = PCPatchSetComputeOperator(pc, PCPatchComputeOperator_DMPlex_Private, NULL);CHKERRQ(ierr); 1977 } 1978 1979 localSize = patch->subspaceOffsets[patch->nsubspaces]; 1980 ierr = VecCreateSeq(PETSC_COMM_SELF, localSize, &patch->localRHS);CHKERRQ(ierr); 1981 ierr = VecSetUp(patch->localRHS);CHKERRQ(ierr); 1982 ierr = VecDuplicate(patch->localRHS, &patch->localUpdate);CHKERRQ(ierr); 1983 ierr = PCPatchCreateCellPatches(pc);CHKERRQ(ierr); 1984 ierr = PCPatchCreateCellPatchDiscretisationInfo(pc);CHKERRQ(ierr); 1985 1986 /* OK, now build the work vectors */ 1987 ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, &pEnd);CHKERRQ(ierr); 1988 ierr = PetscMalloc1(patch->npatch, &patch->patchRHS);CHKERRQ(ierr); 1989 ierr = PetscMalloc1(patch->npatch, &patch->patchUpdate);CHKERRQ(ierr); 1990 1991 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1992 ierr = PetscMalloc1(patch->npatch, &patch->patchRHSWithArtificial);CHKERRQ(ierr); 1993 ierr = PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithArtificial);CHKERRQ(ierr); 1994 } 1995 if (isNonlinear) { 1996 ierr = PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithAll);CHKERRQ(ierr); 1997 } 1998 for (p = pStart; p < pEnd; ++p) { 1999 PetscInt dof; 2000 2001 ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr); 2002 ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchRHS[p-pStart]);CHKERRQ(ierr); 2003 ierr = VecSetUp(patch->patchRHS[p-pStart]);CHKERRQ(ierr); 2004 ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchUpdate[p-pStart]);CHKERRQ(ierr); 2005 ierr = VecSetUp(patch->patchUpdate[p-pStart]);CHKERRQ(ierr); 2006 if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 2007 const PetscInt *gtolArray, *gtolArrayWithArtificial = NULL; 2008 PetscInt numPatchDofs, offset; 2009 PetscInt numPatchDofsWithArtificial, offsetWithArtificial; 2010 PetscInt dofWithoutArtificialCounter = 0; 2011 PetscInt *patchWithoutArtificialToWithArtificialArray; 2012 2013 ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof);CHKERRQ(ierr); 2014 ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchRHSWithArtificial[p-pStart]);CHKERRQ(ierr); 2015 ierr = VecSetUp(patch->patchRHSWithArtificial[p-pStart]);CHKERRQ(ierr); 2016 2017 /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */ 2018 /* the index in the patch with all dofs */ 2019 ierr = ISGetIndices(patch->gtol, >olArray);CHKERRQ(ierr); 2020 2021 ierr = PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs);CHKERRQ(ierr); 2022 if (numPatchDofs == 0) { 2023 patch->dofMappingWithoutToWithArtificial[p-pStart] = NULL; 2024 continue; 2025 } 2026 2027 ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr); 2028 ierr = ISGetIndices(patch->gtolWithArtificial, >olArrayWithArtificial);CHKERRQ(ierr); 2029 ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &numPatchDofsWithArtificial);CHKERRQ(ierr); 2030 ierr = PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offsetWithArtificial);CHKERRQ(ierr); 2031 2032 ierr = PetscMalloc1(numPatchDofs, &patchWithoutArtificialToWithArtificialArray);CHKERRQ(ierr); 2033 for (i=0; i<numPatchDofsWithArtificial; i++) { 2034 if (gtolArrayWithArtificial[i+offsetWithArtificial] == gtolArray[offset+dofWithoutArtificialCounter]) { 2035 patchWithoutArtificialToWithArtificialArray[dofWithoutArtificialCounter] = i; 2036 dofWithoutArtificialCounter++; 2037 if (dofWithoutArtificialCounter == numPatchDofs) 2038 break; 2039 } 2040 } 2041 ierr = ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutArtificialToWithArtificialArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithArtificial[p-pStart]);CHKERRQ(ierr); 2042 ierr = ISRestoreIndices(patch->gtol, >olArray);CHKERRQ(ierr); 2043 ierr = ISRestoreIndices(patch->gtolWithArtificial, >olArrayWithArtificial);CHKERRQ(ierr); 2044 } 2045 if (isNonlinear) { 2046 const PetscInt *gtolArray, *gtolArrayWithAll = NULL; 2047 PetscInt numPatchDofs, offset; 2048 PetscInt numPatchDofsWithAll, offsetWithAll; 2049 PetscInt dofWithoutAllCounter = 0; 2050 PetscInt *patchWithoutAllToWithAllArray; 2051 2052 /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */ 2053 /* the index in the patch with all dofs */ 2054 ierr = ISGetIndices(patch->gtol, >olArray);CHKERRQ(ierr); 2055 2056 ierr = PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs);CHKERRQ(ierr); 2057 if (numPatchDofs == 0) { 2058 patch->dofMappingWithoutToWithAll[p-pStart] = NULL; 2059 continue; 2060 } 2061 2062 ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr); 2063 ierr = ISGetIndices(patch->gtolWithAll, >olArrayWithAll);CHKERRQ(ierr); 2064 ierr = PetscSectionGetDof(patch->gtolCountsWithAll, p, &numPatchDofsWithAll);CHKERRQ(ierr); 2065 ierr = PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offsetWithAll);CHKERRQ(ierr); 2066 2067 ierr = PetscMalloc1(numPatchDofs, &patchWithoutAllToWithAllArray);CHKERRQ(ierr); 2068 2069 for (i=0; i<numPatchDofsWithAll; i++) { 2070 if (gtolArrayWithAll[i+offsetWithAll] == gtolArray[offset+dofWithoutAllCounter]) { 2071 patchWithoutAllToWithAllArray[dofWithoutAllCounter] = i; 2072 dofWithoutAllCounter++; 2073 if (dofWithoutAllCounter == numPatchDofs) 2074 break; 2075 } 2076 } 2077 ierr = ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutAllToWithAllArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithAll[p-pStart]);CHKERRQ(ierr); 2078 ierr = ISRestoreIndices(patch->gtol, >olArray);CHKERRQ(ierr); 2079 ierr = ISRestoreIndices(patch->gtolWithAll, >olArrayWithAll);CHKERRQ(ierr); 2080 } 2081 } 2082 if (patch->save_operators) { 2083 ierr = PetscMalloc1(patch->npatch, &patch->mat);CHKERRQ(ierr); 2084 for (i = 0; i < patch->npatch; ++i) { 2085 ierr = PCPatchCreateMatrix_Private(pc, i, &patch->mat[i], PETSC_FALSE);CHKERRQ(ierr); 2086 } 2087 } 2088 ierr = PetscLogEventEnd(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr); 2089 2090 /* If desired, calculate weights for dof multiplicity */ 2091 if (patch->partition_of_unity) { 2092 PetscScalar *input = NULL; 2093 PetscScalar *output = NULL; 2094 Vec global; 2095 2096 ierr = VecDuplicate(patch->localRHS, &patch->dof_weights);CHKERRQ(ierr); 2097 if(patch->local_composition_type == PC_COMPOSITE_ADDITIVE) { 2098 for (i = 0; i < patch->npatch; ++i) { 2099 PetscInt dof; 2100 2101 ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &dof);CHKERRQ(ierr); 2102 if (dof <= 0) continue; 2103 ierr = VecSet(patch->patchRHS[i], 1.0);CHKERRQ(ierr); 2104 ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchRHS[i], patch->dof_weights, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR);CHKERRQ(ierr); 2105 } 2106 } else { 2107 /* multiplicative is actually only locally multiplicative and globally additive. need the pou where the mesh decomposition overlaps */ 2108 ierr = VecSet(patch->dof_weights, 1.0);CHKERRQ(ierr); 2109 } 2110 2111 VecDuplicate(patch->dof_weights, &global); 2112 VecSet(global, 0.); 2113 2114 ierr = VecGetArray(patch->dof_weights, &input);CHKERRQ(ierr); 2115 ierr = VecGetArray(global, &output);CHKERRQ(ierr); 2116 ierr = PetscSFReduceBegin(patch->defaultSF, MPIU_SCALAR, input, output, MPI_SUM);CHKERRQ(ierr); 2117 ierr = PetscSFReduceEnd(patch->defaultSF, MPIU_SCALAR, input, output, MPI_SUM);CHKERRQ(ierr); 2118 ierr = VecRestoreArray(patch->dof_weights, &input);CHKERRQ(ierr); 2119 ierr = VecRestoreArray(global, &output);CHKERRQ(ierr); 2120 2121 ierr = VecReciprocal(global);CHKERRQ(ierr); 2122 2123 ierr = VecGetArray(patch->dof_weights, &output);CHKERRQ(ierr); 2124 ierr = VecGetArray(global, &input);CHKERRQ(ierr); 2125 ierr = PetscSFBcastBegin(patch->defaultSF, MPIU_SCALAR, input, output);CHKERRQ(ierr); 2126 ierr = PetscSFBcastEnd(patch->defaultSF, MPIU_SCALAR, input, output);CHKERRQ(ierr); 2127 ierr = VecRestoreArray(patch->dof_weights, &output);CHKERRQ(ierr); 2128 ierr = VecRestoreArray(global, &input);CHKERRQ(ierr); 2129 ierr = VecDestroy(&global);CHKERRQ(ierr); 2130 } 2131 if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE && patch->save_operators) { 2132 ierr = PetscMalloc1(patch->npatch, &patch->matWithArtificial);CHKERRQ(ierr); 2133 } 2134 } 2135 ierr = (*patch->setupsolver)(pc);CHKERRQ(ierr); 2136 PetscFunctionReturn(0); 2137 } 2138 2139 static PetscErrorCode PCApply_PATCH_Linear(PC pc, PetscInt i, Vec x, Vec y) 2140 { 2141 PC_PATCH *patch = (PC_PATCH *) pc->data; 2142 KSP ksp = (KSP) patch->solver[i]; 2143 PetscErrorCode ierr; 2144 2145 PetscFunctionBegin; 2146 if (!patch->save_operators) { 2147 Mat mat; 2148 2149 ierr = PCPatchCreateMatrix_Private(pc, i, &mat, PETSC_FALSE);CHKERRQ(ierr); 2150 /* Populate operator here. */ 2151 ierr = PCPatchComputeOperator_Internal(pc, NULL, mat, i, PETSC_FALSE);CHKERRQ(ierr); 2152 ierr = KSPSetOperators(ksp, mat, mat);CHKERRQ(ierr); 2153 /* Drop reference so the KSPSetOperators below will blow it away. */ 2154 ierr = MatDestroy(&mat);CHKERRQ(ierr); 2155 } 2156 ierr = PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr); 2157 if (!ksp->setfromoptionscalled) { 2158 ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr); 2159 } 2160 ierr = KSPSolve(ksp, x, y);CHKERRQ(ierr); 2161 ierr = KSPCheckSolve(ksp, pc, y);CHKERRQ(ierr); 2162 ierr = PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr); 2163 if (!patch->save_operators) { 2164 PC pc; 2165 ierr = KSPSetOperators(ksp, NULL, NULL);CHKERRQ(ierr); 2166 ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr); 2167 /* Destroy PC context too, otherwise the factored matrix hangs around. */ 2168 ierr = PCReset(pc);CHKERRQ(ierr); 2169 } 2170 PetscFunctionReturn(0); 2171 } 2172 2173 static PetscErrorCode PCUpdateMultiplicative_PATCH_Linear(PC pc, PetscInt i, PetscInt pStart) 2174 { 2175 PC_PATCH *patch = (PC_PATCH *) pc->data; 2176 Mat multMat; 2177 PetscErrorCode ierr; 2178 2179 PetscFunctionBegin; 2180 2181 if (patch->save_operators) { 2182 multMat = patch->matWithArtificial[i]; 2183 } else { 2184 /*Very inefficient, hopefully we can just assemble the rectangular matrix in the first place.*/ 2185 Mat matSquare; 2186 PetscInt dof; 2187 IS rowis; 2188 ierr = PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE);CHKERRQ(ierr); 2189 ierr = MatZeroEntries(matSquare);CHKERRQ(ierr); 2190 ierr = PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE);CHKERRQ(ierr); 2191 ierr = MatGetSize(matSquare, &dof, NULL);CHKERRQ(ierr); 2192 ierr = ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis); CHKERRQ(ierr); 2193 ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &multMat); CHKERRQ(ierr); 2194 ierr = MatDestroy(&matSquare);CHKERRQ(ierr); 2195 ierr = ISDestroy(&rowis); CHKERRQ(ierr); 2196 } 2197 ierr = MatMult(multMat, patch->patchUpdate[i], patch->patchRHSWithArtificial[i]); CHKERRQ(ierr); 2198 ierr = VecScale(patch->patchRHSWithArtificial[i], -1.0); CHKERRQ(ierr); 2199 ierr = PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHSWithArtificial[i], patch->localRHS, ADD_VALUES, SCATTER_REVERSE, SCATTER_WITHARTIFICIAL); CHKERRQ(ierr); 2200 if (!patch->save_operators) { 2201 ierr = MatDestroy(&multMat); CHKERRQ(ierr); 2202 } 2203 PetscFunctionReturn(0); 2204 } 2205 2206 static PetscErrorCode PCApply_PATCH(PC pc, Vec x, Vec y) 2207 { 2208 PC_PATCH *patch = (PC_PATCH *) pc->data; 2209 const PetscScalar *globalRHS = NULL; 2210 PetscScalar *localRHS = NULL; 2211 PetscScalar *globalUpdate = NULL; 2212 const PetscInt *bcNodes = NULL; 2213 PetscInt nsweep = patch->symmetrise_sweep ? 2 : 1; 2214 PetscInt start[2] = {0, 0}; 2215 PetscInt end[2] = {-1, -1}; 2216 const PetscInt inc[2] = {1, -1}; 2217 const PetscScalar *localUpdate; 2218 const PetscInt *iterationSet; 2219 PetscInt pStart, numBcs, n, sweep, bc, j; 2220 PetscErrorCode ierr; 2221 2222 PetscFunctionBegin; 2223 ierr = PetscLogEventBegin(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr); 2224 ierr = PetscOptionsPushGetViewerOff(PETSC_TRUE);CHKERRQ(ierr); 2225 /* start, end, inc have 2 entries to manage a second backward sweep if we symmetrize */ 2226 end[0] = patch->npatch; 2227 start[1] = patch->npatch-1; 2228 if (patch->user_patches) { 2229 ierr = ISGetLocalSize(patch->iterationSet, &end[0]);CHKERRQ(ierr); 2230 start[1] = end[0] - 1; 2231 ierr = ISGetIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr); 2232 } 2233 /* Scatter from global space into overlapped local spaces */ 2234 ierr = VecGetArrayRead(x, &globalRHS);CHKERRQ(ierr); 2235 ierr = VecGetArray(patch->localRHS, &localRHS);CHKERRQ(ierr); 2236 ierr = PetscSFBcastBegin(patch->defaultSF, MPIU_SCALAR, globalRHS, localRHS);CHKERRQ(ierr); 2237 ierr = PetscSFBcastEnd(patch->defaultSF, MPIU_SCALAR, globalRHS, localRHS);CHKERRQ(ierr); 2238 ierr = VecRestoreArrayRead(x, &globalRHS);CHKERRQ(ierr); 2239 ierr = VecRestoreArray(patch->localRHS, &localRHS);CHKERRQ(ierr); 2240 2241 ierr = VecSet(patch->localUpdate, 0.0);CHKERRQ(ierr); 2242 ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, NULL);CHKERRQ(ierr); 2243 for (sweep = 0; sweep < nsweep; sweep++) { 2244 for (j = start[sweep]; j*inc[sweep] < end[sweep]*inc[sweep]; j += inc[sweep]) { 2245 PetscInt i = patch->user_patches ? iterationSet[j] : j; 2246 PetscInt start, len; 2247 2248 ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &len);CHKERRQ(ierr); 2249 ierr = PetscSectionGetOffset(patch->gtolCounts, i+pStart, &start);CHKERRQ(ierr); 2250 /* TODO: Squash out these guys in the setup as well. */ 2251 if (len <= 0) continue; 2252 /* TODO: Do we need different scatters for X and Y? */ 2253 ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->localRHS, patch->patchRHS[i], INSERT_VALUES, SCATTER_FORWARD, SCATTER_INTERIOR);CHKERRQ(ierr); 2254 ierr = (*patch->applysolver)(pc, i, patch->patchRHS[i], patch->patchUpdate[i]);CHKERRQ(ierr); 2255 ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchUpdate[i], patch->localUpdate, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR);CHKERRQ(ierr); 2256 if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 2257 ierr = (*patch->updatemultiplicative)(pc, i, pStart);CHKERRQ(ierr); 2258 } 2259 } 2260 } 2261 if (patch->user_patches) {ierr = ISRestoreIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr);} 2262 /* XXX: should we do this on the global vector? */ 2263 if (patch->partition_of_unity) { 2264 ierr = VecPointwiseMult(patch->localUpdate, patch->localUpdate, patch->dof_weights);CHKERRQ(ierr); 2265 } 2266 /* Now patch->localUpdate contains the solution of the patch solves, so we need to combine them all. */ 2267 ierr = VecSet(y, 0.0);CHKERRQ(ierr); 2268 ierr = VecGetArray(y, &globalUpdate);CHKERRQ(ierr); 2269 ierr = VecGetArrayRead(patch->localUpdate, &localUpdate);CHKERRQ(ierr); 2270 ierr = PetscSFReduceBegin(patch->defaultSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM);CHKERRQ(ierr); 2271 ierr = PetscSFReduceEnd(patch->defaultSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM);CHKERRQ(ierr); 2272 ierr = VecRestoreArrayRead(patch->localUpdate, &localUpdate);CHKERRQ(ierr); 2273 2274 /* Now we need to send the global BC values through */ 2275 ierr = VecGetArrayRead(x, &globalRHS);CHKERRQ(ierr); 2276 ierr = ISGetSize(patch->globalBcNodes, &numBcs);CHKERRQ(ierr); 2277 ierr = ISGetIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr); 2278 ierr = VecGetLocalSize(x, &n);CHKERRQ(ierr); 2279 for (bc = 0; bc < numBcs; ++bc) { 2280 const PetscInt idx = bcNodes[bc]; 2281 if (idx < n) globalUpdate[idx] = globalRHS[idx]; 2282 } 2283 2284 ierr = ISRestoreIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr); 2285 ierr = VecRestoreArrayRead(x, &globalRHS);CHKERRQ(ierr); 2286 ierr = VecRestoreArray(y, &globalUpdate);CHKERRQ(ierr); 2287 2288 ierr = PetscOptionsPopGetViewerOff();CHKERRQ(ierr); 2289 ierr = PetscLogEventEnd(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr); 2290 PetscFunctionReturn(0); 2291 } 2292 2293 static PetscErrorCode PCReset_PATCH_Linear(PC pc) 2294 { 2295 PC_PATCH *patch = (PC_PATCH *) pc->data; 2296 PetscInt i; 2297 PetscErrorCode ierr; 2298 2299 PetscFunctionBegin; 2300 if (patch->solver) { 2301 for (i = 0; i < patch->npatch; ++i) {ierr = KSPReset((KSP) patch->solver[i]);CHKERRQ(ierr);} 2302 } 2303 PetscFunctionReturn(0); 2304 } 2305 2306 static PetscErrorCode PCReset_PATCH(PC pc) 2307 { 2308 PC_PATCH *patch = (PC_PATCH *) pc->data; 2309 PetscInt i; 2310 PetscErrorCode ierr; 2311 2312 PetscFunctionBegin; 2313 /* TODO: Get rid of all these ifs */ 2314 ierr = PetscSFDestroy(&patch->defaultSF);CHKERRQ(ierr); 2315 ierr = PetscSectionDestroy(&patch->cellCounts);CHKERRQ(ierr); 2316 ierr = PetscSectionDestroy(&patch->pointCounts);CHKERRQ(ierr); 2317 ierr = PetscSectionDestroy(&patch->cellNumbering);CHKERRQ(ierr); 2318 ierr = PetscSectionDestroy(&patch->gtolCounts);CHKERRQ(ierr); 2319 ierr = ISDestroy(&patch->gtol);CHKERRQ(ierr); 2320 ierr = ISDestroy(&patch->cells);CHKERRQ(ierr); 2321 ierr = ISDestroy(&patch->points);CHKERRQ(ierr); 2322 ierr = ISDestroy(&patch->dofs);CHKERRQ(ierr); 2323 ierr = ISDestroy(&patch->offs);CHKERRQ(ierr); 2324 ierr = PetscSectionDestroy(&patch->patchSection);CHKERRQ(ierr); 2325 ierr = ISDestroy(&patch->ghostBcNodes);CHKERRQ(ierr); 2326 ierr = ISDestroy(&patch->globalBcNodes);CHKERRQ(ierr); 2327 ierr = PetscSectionDestroy(&patch->gtolCountsWithArtificial);CHKERRQ(ierr); 2328 ierr = ISDestroy(&patch->gtolWithArtificial);CHKERRQ(ierr); 2329 ierr = ISDestroy(&patch->dofsWithArtificial);CHKERRQ(ierr); 2330 ierr = ISDestroy(&patch->offsWithArtificial);CHKERRQ(ierr); 2331 ierr = PetscSectionDestroy(&patch->gtolCountsWithAll);CHKERRQ(ierr); 2332 ierr = ISDestroy(&patch->gtolWithAll);CHKERRQ(ierr); 2333 ierr = ISDestroy(&patch->dofsWithAll);CHKERRQ(ierr); 2334 ierr = ISDestroy(&patch->offsWithAll);CHKERRQ(ierr); 2335 2336 2337 if (patch->dofSection) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscSectionDestroy(&patch->dofSection[i]);CHKERRQ(ierr);} 2338 ierr = PetscFree(patch->dofSection);CHKERRQ(ierr); 2339 ierr = PetscFree(patch->bs);CHKERRQ(ierr); 2340 ierr = PetscFree(patch->nodesPerCell);CHKERRQ(ierr); 2341 if (patch->cellNodeMap) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscFree(patch->cellNodeMap[i]);CHKERRQ(ierr);} 2342 ierr = PetscFree(patch->cellNodeMap);CHKERRQ(ierr); 2343 ierr = PetscFree(patch->subspaceOffsets);CHKERRQ(ierr); 2344 2345 ierr = (*patch->resetsolver)(pc);CHKERRQ(ierr); 2346 2347 if (patch->subspaces_to_exclude) { 2348 PetscHSetIDestroy(&patch->subspaces_to_exclude); 2349 } 2350 2351 ierr = VecDestroy(&patch->localRHS);CHKERRQ(ierr); 2352 ierr = VecDestroy(&patch->localUpdate);CHKERRQ(ierr); 2353 if (patch->patchRHS) { 2354 for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchRHS[i]);CHKERRQ(ierr);} 2355 ierr = PetscFree(patch->patchRHS);CHKERRQ(ierr); 2356 } 2357 if (patch->patchUpdate) { 2358 for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchUpdate[i]);CHKERRQ(ierr);} 2359 ierr = PetscFree(patch->patchUpdate);CHKERRQ(ierr); 2360 } 2361 ierr = VecDestroy(&patch->dof_weights);CHKERRQ(ierr); 2362 if (patch->patch_dof_weights) { 2363 for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patch_dof_weights[i]);CHKERRQ(ierr);} 2364 ierr = PetscFree(patch->patch_dof_weights);CHKERRQ(ierr); 2365 } 2366 if (patch->mat) { 2367 for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->mat[i]);CHKERRQ(ierr);} 2368 ierr = PetscFree(patch->mat);CHKERRQ(ierr); 2369 } 2370 if (patch->matWithArtificial) { 2371 for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->matWithArtificial[i]);CHKERRQ(ierr);} 2372 ierr = PetscFree(patch->matWithArtificial);CHKERRQ(ierr); 2373 } 2374 if (patch->patchRHSWithArtificial) { 2375 for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchRHSWithArtificial[i]);CHKERRQ(ierr);} 2376 ierr = PetscFree(patch->patchRHSWithArtificial);CHKERRQ(ierr); 2377 } 2378 if(patch->dofMappingWithoutToWithArtificial) { 2379 for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->dofMappingWithoutToWithArtificial[i]);CHKERRQ(ierr);} 2380 ierr = PetscFree(patch->dofMappingWithoutToWithArtificial);CHKERRQ(ierr); 2381 2382 } 2383 if(patch->dofMappingWithoutToWithAll) { 2384 for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->dofMappingWithoutToWithAll[i]);CHKERRQ(ierr);} 2385 ierr = PetscFree(patch->dofMappingWithoutToWithAll);CHKERRQ(ierr); 2386 2387 } 2388 ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr); 2389 if (patch->userIS) { 2390 for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->userIS[i]);CHKERRQ(ierr);} 2391 ierr = PetscFree(patch->userIS);CHKERRQ(ierr); 2392 } 2393 patch->bs = 0; 2394 patch->cellNodeMap = NULL; 2395 patch->nsubspaces = 0; 2396 ierr = ISDestroy(&patch->iterationSet);CHKERRQ(ierr); 2397 2398 ierr = PetscViewerDestroy(&patch->viewerSection);CHKERRQ(ierr); 2399 PetscFunctionReturn(0); 2400 } 2401 2402 static PetscErrorCode PCDestroy_PATCH_Linear(PC pc) 2403 { 2404 PC_PATCH *patch = (PC_PATCH *) pc->data; 2405 PetscInt i; 2406 PetscErrorCode ierr; 2407 2408 PetscFunctionBegin; 2409 if (patch->solver) { 2410 for (i = 0; i < patch->npatch; ++i) {ierr = KSPDestroy((KSP *) &patch->solver[i]);CHKERRQ(ierr);} 2411 ierr = PetscFree(patch->solver);CHKERRQ(ierr); 2412 } 2413 PetscFunctionReturn(0); 2414 } 2415 2416 static PetscErrorCode PCDestroy_PATCH(PC pc) 2417 { 2418 PC_PATCH *patch = (PC_PATCH *) pc->data; 2419 PetscErrorCode ierr; 2420 2421 PetscFunctionBegin; 2422 ierr = PCReset_PATCH(pc);CHKERRQ(ierr); 2423 ierr = (*patch->destroysolver)(pc);CHKERRQ(ierr); 2424 ierr = PetscFree(pc->data);CHKERRQ(ierr); 2425 PetscFunctionReturn(0); 2426 } 2427 2428 static PetscErrorCode PCSetFromOptions_PATCH(PetscOptionItems *PetscOptionsObject, PC pc) 2429 { 2430 PC_PATCH *patch = (PC_PATCH *) pc->data; 2431 PCPatchConstructType patchConstructionType = PC_PATCH_STAR; 2432 char sub_mat_type[PETSC_MAX_PATH_LEN]; 2433 char option[PETSC_MAX_PATH_LEN]; 2434 const char *prefix; 2435 PetscBool flg, dimflg, codimflg; 2436 MPI_Comm comm; 2437 PetscInt *ifields, nfields, k; 2438 PetscErrorCode ierr; 2439 PCCompositeType loctype = PC_COMPOSITE_ADDITIVE; 2440 2441 PetscFunctionBegin; 2442 ierr = PetscObjectGetComm((PetscObject) pc, &comm);CHKERRQ(ierr); 2443 ierr = PetscObjectGetOptionsPrefix((PetscObject) pc, &prefix);CHKERRQ(ierr); 2444 ierr = PetscOptionsHead(PetscOptionsObject, "Patch solver options");CHKERRQ(ierr); 2445 2446 ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_save_operators", patch->classname); 2447 ierr = PetscOptionsBool(option, "Store all patch operators for lifetime of object?", "PCPatchSetSaveOperators", patch->save_operators, &patch->save_operators, &flg);CHKERRQ(ierr); 2448 2449 ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_partition_of_unity", patch->classname); 2450 ierr = PetscOptionsBool(option, "Weight contributions by dof multiplicity?", "PCPatchSetPartitionOfUnity", patch->partition_of_unity, &patch->partition_of_unity, &flg);CHKERRQ(ierr); 2451 2452 ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_local_type", patch->classname); 2453 ierr = PetscOptionsEnum(option,"Type of local solver composition (additive or multiplicative)","PCPatchSetLocalComposition",PCCompositeTypes,(PetscEnum)loctype,(PetscEnum*)&loctype,&flg);CHKERRQ(ierr); 2454 if(flg) { ierr = PCPatchSetLocalComposition(pc, loctype);CHKERRQ(ierr);} 2455 2456 ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_dim", patch->classname); 2457 ierr = PetscOptionsInt(option, "What dimension of mesh point to construct patches by? (0 = vertices)", "PCPATCH", patch->dim, &patch->dim, &dimflg);CHKERRQ(ierr); 2458 ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_codim", patch->classname); 2459 ierr = PetscOptionsInt(option, "What co-dimension of mesh point to construct patches by? (0 = cells)", "PCPATCH", patch->codim, &patch->codim, &codimflg);CHKERRQ(ierr); 2460 if (dimflg && codimflg) {SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Can only set one of dimension or co-dimension");CHKERRQ(ierr);} 2461 2462 ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_type", patch->classname); 2463 ierr = PetscOptionsEnum(option, "How should the patches be constructed?", "PCPatchSetConstructType", PCPatchConstructTypes, (PetscEnum) patchConstructionType, (PetscEnum *) &patchConstructionType, &flg);CHKERRQ(ierr); 2464 if (flg) {ierr = PCPatchSetConstructType(pc, patchConstructionType, NULL, NULL);CHKERRQ(ierr);} 2465 2466 ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_vanka_dim", patch->classname); 2467 ierr = PetscOptionsInt(option, "Topological dimension of entities for Vanka to ignore", "PCPATCH", patch->vankadim, &patch->vankadim, &flg);CHKERRQ(ierr); 2468 2469 ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_ignore_dim", patch->classname); 2470 ierr = PetscOptionsInt(option, "Topological dimension of entities for completion to ignore", "PCPATCH", patch->ignoredim, &patch->ignoredim, &flg);CHKERRQ(ierr); 2471 2472 ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_pardecomp_overlap", patch->classname); 2473 ierr = PetscOptionsInt(option, "What overlap should we use in construct type pardecomp?", "PCPATCH", patch->pardecomp_overlap, &patch->pardecomp_overlap, &flg);CHKERRQ(ierr); 2474 2475 ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_sub_mat_type", patch->classname); 2476 ierr = PetscOptionsFList(option, "Matrix type for patch solves", "PCPatchSetSubMatType", MatList, NULL, sub_mat_type, PETSC_MAX_PATH_LEN, &flg);CHKERRQ(ierr); 2477 if (flg) {ierr = PCPatchSetSubMatType(pc, sub_mat_type);CHKERRQ(ierr);} 2478 2479 ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_symmetrise_sweep", patch->classname); 2480 ierr = PetscOptionsBool(option, "Go start->end, end->start?", "PCPATCH", patch->symmetrise_sweep, &patch->symmetrise_sweep, &flg);CHKERRQ(ierr); 2481 2482 /* If the user has set the number of subspaces, use that for the buffer size, 2483 otherwise use a large number */ 2484 if (patch->nsubspaces <= 0) { 2485 nfields = 128; 2486 } else { 2487 nfields = patch->nsubspaces; 2488 } 2489 ierr = PetscMalloc1(nfields, &ifields);CHKERRQ(ierr); 2490 ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exclude_subspaces", patch->classname); 2491 ierr = PetscOptionsGetIntArray(((PetscObject)pc)->options,((PetscObject)pc)->prefix,option,ifields,&nfields,&flg);CHKERRQ(ierr); 2492 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"); 2493 if (flg) { 2494 PetscHSetIClear(patch->subspaces_to_exclude); 2495 for (k = 0; k < nfields; k++) { 2496 PetscHSetIAdd(patch->subspaces_to_exclude, ifields[k]); 2497 } 2498 } 2499 ierr = PetscFree(ifields);CHKERRQ(ierr); 2500 2501 ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_patches_view", patch->classname); 2502 ierr = PetscOptionsBool(option, "Print out information during patch construction", "PCPATCH", patch->viewPatches, &patch->viewPatches, &flg);CHKERRQ(ierr); 2503 2504 ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_cells_view", patch->classname); 2505 ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options,prefix, option, &patch->viewerCells, &patch->formatCells, &patch->viewCells);CHKERRQ(ierr); 2506 ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_points_view", patch->classname); 2507 ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options,prefix, option, &patch->viewerPoints, &patch->formatPoints, &patch->viewPoints);CHKERRQ(ierr); 2508 ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_section_view", patch->classname); 2509 ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options,prefix, option, &patch->viewerSection, &patch->formatSection, &patch->viewSection);CHKERRQ(ierr); 2510 ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_mat_view", patch->classname); 2511 ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options,prefix, option, &patch->viewerMatrix, &patch->formatMatrix, &patch->viewMatrix);CHKERRQ(ierr); 2512 ierr = PetscOptionsTail();CHKERRQ(ierr); 2513 patch->optionsSet = PETSC_TRUE; 2514 PetscFunctionReturn(0); 2515 } 2516 2517 static PetscErrorCode PCSetUpOnBlocks_PATCH(PC pc) 2518 { 2519 PC_PATCH *patch = (PC_PATCH*) pc->data; 2520 KSPConvergedReason reason; 2521 PetscInt i; 2522 PetscErrorCode ierr; 2523 2524 PetscFunctionBegin; 2525 if (!patch->save_operators) { 2526 /* Can't do this here because the sub KSPs don't have an operator attached yet. */ 2527 PetscFunctionReturn(0); 2528 } 2529 for (i = 0; i < patch->npatch; ++i) { 2530 if (!((KSP) patch->solver[i])->setfromoptionscalled) { 2531 ierr = KSPSetFromOptions((KSP) patch->solver[i]);CHKERRQ(ierr); 2532 } 2533 ierr = KSPSetUp((KSP) patch->solver[i]);CHKERRQ(ierr); 2534 ierr = KSPGetConvergedReason((KSP) patch->solver[i], &reason);CHKERRQ(ierr); 2535 if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR; 2536 } 2537 PetscFunctionReturn(0); 2538 } 2539 2540 static PetscErrorCode PCView_PATCH(PC pc, PetscViewer viewer) 2541 { 2542 PC_PATCH *patch = (PC_PATCH *) pc->data; 2543 PetscViewer sviewer; 2544 PetscBool isascii; 2545 PetscMPIInt rank; 2546 PetscErrorCode ierr; 2547 2548 PetscFunctionBegin; 2549 /* TODO Redo tabbing with set tbas in new style */ 2550 ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii);CHKERRQ(ierr); 2551 if (!isascii) PetscFunctionReturn(0); 2552 ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) pc), &rank);CHKERRQ(ierr); 2553 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 2554 ierr = PetscViewerASCIIPrintf(viewer, "Subspace Correction preconditioner with %d patches\n", patch->npatch);CHKERRQ(ierr); 2555 if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 2556 ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: multiplicative\n");CHKERRQ(ierr); 2557 } else { 2558 ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: additive\n");CHKERRQ(ierr); 2559 } 2560 if (patch->partition_of_unity) {ierr = PetscViewerASCIIPrintf(viewer, "Weighting by partition of unity\n");CHKERRQ(ierr);} 2561 else {ierr = PetscViewerASCIIPrintf(viewer, "Not weighting by partition of unity\n");CHKERRQ(ierr);} 2562 if (patch->symmetrise_sweep) {ierr = PetscViewerASCIIPrintf(viewer, "Symmetrising sweep (start->end, then end->start)\n");CHKERRQ(ierr);} 2563 else {ierr = PetscViewerASCIIPrintf(viewer, "Not symmetrising sweep\n");CHKERRQ(ierr);} 2564 if (!patch->save_operators) {ierr = PetscViewerASCIIPrintf(viewer, "Not saving patch operators (rebuilt every PCApply)\n");CHKERRQ(ierr);} 2565 else {ierr = PetscViewerASCIIPrintf(viewer, "Saving patch operators (rebuilt every PCSetUp)\n");CHKERRQ(ierr);} 2566 if (patch->patchconstructop == PCPatchConstruct_Star) {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: star\n");CHKERRQ(ierr);} 2567 else if (patch->patchconstructop == PCPatchConstruct_Vanka) {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: Vanka\n");CHKERRQ(ierr);} 2568 else if (patch->patchconstructop == PCPatchConstruct_User) {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: user-specified\n");CHKERRQ(ierr);} 2569 else {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: unknown\n");CHKERRQ(ierr);} 2570 2571 if (patch->isNonlinear) { 2572 ierr = PetscViewerASCIIPrintf(viewer, "SNES on patches (all same):\n");CHKERRQ(ierr); 2573 } else { 2574 ierr = PetscViewerASCIIPrintf(viewer, "KSP on patches (all same):\n");CHKERRQ(ierr); 2575 } 2576 if (patch->solver) { 2577 ierr = PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr); 2578 if (!rank) { 2579 ierr = PetscViewerASCIIPushTab(sviewer);CHKERRQ(ierr); 2580 ierr = PetscObjectView(patch->solver[0], sviewer);CHKERRQ(ierr); 2581 ierr = PetscViewerASCIIPopTab(sviewer);CHKERRQ(ierr); 2582 } 2583 ierr = PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr); 2584 } else { 2585 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 2586 ierr = PetscViewerASCIIPrintf(viewer, "Solver not yet set.\n");CHKERRQ(ierr); 2587 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 2588 } 2589 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 2590 PetscFunctionReturn(0); 2591 } 2592 2593 /*MC 2594 PCPATCH - A PC object that encapsulates flexible definition of blocks for overlapping and non-overlapping 2595 small block additive preconditioners. Block definition is based on topology from 2596 a DM and equation numbering from a PetscSection. 2597 2598 Options Database Keys: 2599 + -pc_patch_cells_view - Views the process local cell numbers for each patch 2600 . -pc_patch_points_view - Views the process local mesh point numbers for each patch 2601 . -pc_patch_g2l_view - Views the map between global dofs and patch local dofs for each patch 2602 . -pc_patch_patches_view - Views the global dofs associated with each patch and its boundary 2603 - -pc_patch_sub_mat_view - Views the matrix associated with each patch 2604 2605 Level: intermediate 2606 2607 .seealso: PCType, PCCreate(), PCSetType() 2608 M*/ 2609 PETSC_EXTERN PetscErrorCode PCCreate_Patch(PC pc) 2610 { 2611 PC_PATCH *patch; 2612 PetscErrorCode ierr; 2613 2614 PetscFunctionBegin; 2615 ierr = PetscNewLog(pc, &patch);CHKERRQ(ierr); 2616 2617 if (patch->subspaces_to_exclude) { 2618 PetscHSetIDestroy(&patch->subspaces_to_exclude); 2619 } 2620 PetscHSetICreate(&patch->subspaces_to_exclude); 2621 2622 patch->classname = "pc"; 2623 patch->isNonlinear = PETSC_FALSE; 2624 2625 /* Set some defaults */ 2626 patch->combined = PETSC_FALSE; 2627 patch->save_operators = PETSC_TRUE; 2628 patch->local_composition_type = PC_COMPOSITE_ADDITIVE; 2629 patch->partition_of_unity = PETSC_FALSE; 2630 patch->codim = -1; 2631 patch->dim = -1; 2632 patch->vankadim = -1; 2633 patch->ignoredim = -1; 2634 patch->pardecomp_overlap = 0; 2635 patch->patchconstructop = PCPatchConstruct_Star; 2636 patch->symmetrise_sweep = PETSC_FALSE; 2637 patch->npatch = 0; 2638 patch->userIS = NULL; 2639 patch->optionsSet = PETSC_FALSE; 2640 patch->iterationSet = NULL; 2641 patch->user_patches = PETSC_FALSE; 2642 ierr = PetscStrallocpy(MATDENSE, (char **) &patch->sub_mat_type);CHKERRQ(ierr); 2643 patch->viewPatches = PETSC_FALSE; 2644 patch->viewCells = PETSC_FALSE; 2645 patch->viewPoints = PETSC_FALSE; 2646 patch->viewSection = PETSC_FALSE; 2647 patch->viewMatrix = PETSC_FALSE; 2648 patch->setupsolver = PCSetUp_PATCH_Linear; 2649 patch->applysolver = PCApply_PATCH_Linear; 2650 patch->resetsolver = PCReset_PATCH_Linear; 2651 patch->destroysolver = PCDestroy_PATCH_Linear; 2652 patch->updatemultiplicative = PCUpdateMultiplicative_PATCH_Linear; 2653 patch->dofMappingWithoutToWithArtificial = NULL; 2654 patch->dofMappingWithoutToWithAll = NULL; 2655 2656 pc->data = (void *) patch; 2657 pc->ops->apply = PCApply_PATCH; 2658 pc->ops->applytranspose = 0; /* PCApplyTranspose_PATCH; */ 2659 pc->ops->setup = PCSetUp_PATCH; 2660 pc->ops->reset = PCReset_PATCH; 2661 pc->ops->destroy = PCDestroy_PATCH; 2662 pc->ops->setfromoptions = PCSetFromOptions_PATCH; 2663 pc->ops->setuponblocks = PCSetUpOnBlocks_PATCH; 2664 pc->ops->view = PCView_PATCH; 2665 pc->ops->applyrichardson = 0; 2666 2667 PetscFunctionReturn(0); 2668 } 2669