14bbf5ea8SMatthew G. Knepley #include <petsc/private/pcpatchimpl.h> /*I "petscpc.h" I*/ 2*5f824522SMatthew G. Knepley #include <petsc/private/dmpleximpl.h> /* For DMPlexComputeJacobian_Patch_Internal() */ 34bbf5ea8SMatthew G. Knepley #include <petscsf.h> 44bbf5ea8SMatthew G. Knepley #include <petscbt.h> 5*5f824522SMatthew G. Knepley #include <petscds.h> 64bbf5ea8SMatthew G. Knepley 74bbf5ea8SMatthew G. Knepley PetscLogEvent PC_Patch_CreatePatches, PC_Patch_ComputeOp, PC_Patch_Solve, PC_Patch_Scatter, PC_Patch_Apply, PC_Patch_Prealloc; 84bbf5ea8SMatthew G. Knepley 9*5f824522SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode ObjectView(PetscObject obj, PetscViewer viewer, PetscViewerFormat format) 10*5f824522SMatthew G. Knepley { 11*5f824522SMatthew G. Knepley PetscErrorCode ierr; 12*5f824522SMatthew G. Knepley 13*5f824522SMatthew G. Knepley PetscFunctionBeginHot; 14*5f824522SMatthew G. Knepley ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr); 15*5f824522SMatthew G. Knepley ierr = PetscObjectView(obj, viewer);CHKERRQ(ierr); 16*5f824522SMatthew G. Knepley ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 17*5f824522SMatthew G. Knepley PetscFunctionReturn(0); 18*5f824522SMatthew G. Knepley } 19*5f824522SMatthew G. Knepley 204bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_Star(void *vpatch, DM dm, PetscInt point, PetscHashI ht) 214bbf5ea8SMatthew G. Knepley { 224bbf5ea8SMatthew G. Knepley PetscInt starSize; 234bbf5ea8SMatthew G. Knepley PetscInt *star = NULL, si; 244bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 254bbf5ea8SMatthew G. Knepley 264bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 274bbf5ea8SMatthew G. Knepley PetscHashIClear(ht); 284bbf5ea8SMatthew G. Knepley /* To start with, add the point we care about */ 294bbf5ea8SMatthew G. Knepley PetscHashIAdd(ht, point, 0); 304bbf5ea8SMatthew G. Knepley /* Loop over all the points that this point connects to */ 314bbf5ea8SMatthew G. Knepley ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr); 32*5f824522SMatthew G. Knepley for (si = 0; si < starSize*2; si += 2) {PetscHashIAdd(ht, star[si], 0);} 334bbf5ea8SMatthew G. Knepley ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr); 344bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 354bbf5ea8SMatthew G. Knepley } 364bbf5ea8SMatthew G. Knepley 374bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_Vanka(void *vpatch, DM dm, PetscInt point, PetscHashI ht) 384bbf5ea8SMatthew G. Knepley { 394bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) vpatch; 404bbf5ea8SMatthew G. Knepley PetscInt starSize; 414bbf5ea8SMatthew G. Knepley PetscInt *star = NULL; 424bbf5ea8SMatthew G. Knepley PetscBool shouldIgnore = PETSC_FALSE; 434bbf5ea8SMatthew G. Knepley PetscInt cStart, cEnd, iStart, iEnd, si; 444bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 454bbf5ea8SMatthew G. Knepley 464bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 474bbf5ea8SMatthew G. Knepley PetscHashIClear(ht); 484bbf5ea8SMatthew G. Knepley /* To start with, add the point we care about */ 494bbf5ea8SMatthew G. Knepley PetscHashIAdd(ht, point, 0); 504bbf5ea8SMatthew G. Knepley /* Should we ignore any points of a certain dimension? */ 514bbf5ea8SMatthew G. Knepley if (patch->vankadim >= 0) { 524bbf5ea8SMatthew G. Knepley shouldIgnore = PETSC_TRUE; 534bbf5ea8SMatthew G. Knepley ierr = DMPlexGetDepthStratum(dm, patch->vankadim, &iStart, &iEnd);CHKERRQ(ierr); 544bbf5ea8SMatthew G. Knepley } 554bbf5ea8SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 564bbf5ea8SMatthew G. Knepley /* Loop over all the cells that this point connects to */ 574bbf5ea8SMatthew G. Knepley ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr); 58*5f824522SMatthew G. Knepley for (si = 0; si < starSize*2; si += 2) { 594bbf5ea8SMatthew G. Knepley const PetscInt cell = star[si]; 604bbf5ea8SMatthew G. Knepley PetscInt closureSize; 614bbf5ea8SMatthew G. Knepley PetscInt *closure = NULL, ci; 624bbf5ea8SMatthew G. Knepley 634bbf5ea8SMatthew G. Knepley if (cell < cStart || cell >= cEnd) continue; 644bbf5ea8SMatthew G. Knepley /* now loop over all entities in the closure of that cell */ 654bbf5ea8SMatthew G. Knepley ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 66*5f824522SMatthew G. Knepley for (ci = 0; ci < closureSize*2; ci += 2) { 674bbf5ea8SMatthew G. Knepley const PetscInt newpoint = closure[ci]; 684bbf5ea8SMatthew G. Knepley 694bbf5ea8SMatthew G. Knepley /* We've been told to ignore entities of this type.*/ 704bbf5ea8SMatthew G. Knepley if (shouldIgnore && newpoint >= iStart && newpoint < iEnd) continue; 714bbf5ea8SMatthew G. Knepley PetscHashIAdd(ht, newpoint, 0); 724bbf5ea8SMatthew G. Knepley } 734bbf5ea8SMatthew G. Knepley ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 744bbf5ea8SMatthew G. Knepley } 754bbf5ea8SMatthew G. Knepley ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr); 764bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 774bbf5ea8SMatthew G. Knepley } 784bbf5ea8SMatthew G. Knepley 794bbf5ea8SMatthew G. Knepley /* The user's already set the patches in patch->userIS. Build the hash tables */ 804bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_User(void *vpatch, DM dm, PetscInt point, PetscHashI ht) 814bbf5ea8SMatthew G. Knepley { 824bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) vpatch; 834bbf5ea8SMatthew G. Knepley IS patchis = patch->userIS[point]; 844bbf5ea8SMatthew G. Knepley PetscInt n; 854bbf5ea8SMatthew G. Knepley const PetscInt *patchdata; 864bbf5ea8SMatthew G. Knepley PetscInt pStart, pEnd, i; 874bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 884bbf5ea8SMatthew G. Knepley 894bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 904bbf5ea8SMatthew G. Knepley PetscHashIClear(ht); 914bbf5ea8SMatthew G. Knepley ierr = DMPlexGetChart(dm, &pStart, &pEnd); 924bbf5ea8SMatthew G. Knepley ierr = ISGetLocalSize(patchis, &n);CHKERRQ(ierr); 934bbf5ea8SMatthew G. Knepley ierr = ISGetIndices(patchis, &patchdata);CHKERRQ(ierr); 944bbf5ea8SMatthew G. Knepley for (i = 0; i < n; ++i) { 954bbf5ea8SMatthew G. Knepley const PetscInt ownedpoint = patchdata[i]; 964bbf5ea8SMatthew G. Knepley 974bbf5ea8SMatthew G. Knepley if (ownedpoint < pStart || ownedpoint >= pEnd) { 984bbf5ea8SMatthew G. Knepley SETERRQ3(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D was not in [%D, %D)", ownedpoint, pStart, pEnd); 994bbf5ea8SMatthew G. Knepley } 1004bbf5ea8SMatthew G. Knepley PetscHashIAdd(ht, ownedpoint, 0); 1014bbf5ea8SMatthew G. Knepley } 1024bbf5ea8SMatthew G. Knepley ierr = ISRestoreIndices(patchis, &patchdata);CHKERRQ(ierr); 1034bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 1044bbf5ea8SMatthew G. Knepley } 1054bbf5ea8SMatthew G. Knepley 1064bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateDefaultSF_Private(PC pc, PetscInt n, const PetscSF *sf, const PetscInt *bs) 1074bbf5ea8SMatthew G. Knepley { 1084bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 1094bbf5ea8SMatthew G. Knepley PetscInt i; 1104bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 1114bbf5ea8SMatthew G. Knepley 1124bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 1134bbf5ea8SMatthew G. Knepley if (n == 1 && bs[0] == 1) { 1144bbf5ea8SMatthew G. Knepley patch->defaultSF = sf[0]; 1154bbf5ea8SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) patch->defaultSF);CHKERRQ(ierr); 1164bbf5ea8SMatthew G. Knepley } else { 1174bbf5ea8SMatthew G. Knepley PetscInt allRoots = 0, allLeaves = 0; 1184bbf5ea8SMatthew G. Knepley PetscInt leafOffset = 0; 1194bbf5ea8SMatthew G. Knepley PetscInt *ilocal = NULL; 1204bbf5ea8SMatthew G. Knepley PetscSFNode *iremote = NULL; 1214bbf5ea8SMatthew G. Knepley PetscInt *remoteOffsets = NULL; 1224bbf5ea8SMatthew G. Knepley PetscInt index = 0; 1234bbf5ea8SMatthew G. Knepley PetscHashI rankToIndex; 1244bbf5ea8SMatthew G. Knepley PetscInt numRanks = 0; 1254bbf5ea8SMatthew G. Knepley PetscSFNode *remote = NULL; 1264bbf5ea8SMatthew G. Knepley PetscSF rankSF; 1274bbf5ea8SMatthew G. Knepley PetscInt *ranks = NULL; 1284bbf5ea8SMatthew G. Knepley PetscInt *offsets = NULL; 1294bbf5ea8SMatthew G. Knepley MPI_Datatype contig; 1304bbf5ea8SMatthew G. Knepley PetscHashI ranksUniq; 1314bbf5ea8SMatthew G. Knepley 1324bbf5ea8SMatthew G. Knepley /* First figure out how many dofs there are in the concatenated numbering. 1334bbf5ea8SMatthew G. Knepley * allRoots: number of owned global dofs; 1344bbf5ea8SMatthew G. Knepley * allLeaves: number of visible dofs (global + ghosted). 1354bbf5ea8SMatthew G. Knepley */ 1364bbf5ea8SMatthew G. Knepley for (i = 0; i < n; ++i) { 1374bbf5ea8SMatthew G. Knepley PetscInt nroots, nleaves; 1384bbf5ea8SMatthew G. Knepley 1394bbf5ea8SMatthew G. Knepley ierr = PetscSFGetGraph(sf[i], &nroots, &nleaves, NULL, NULL);CHKERRQ(ierr); 1404bbf5ea8SMatthew G. Knepley allRoots += nroots * bs[i]; 1414bbf5ea8SMatthew G. Knepley allLeaves += nleaves * bs[i]; 1424bbf5ea8SMatthew G. Knepley } 1434bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(allLeaves, &ilocal);CHKERRQ(ierr); 1444bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(allLeaves, &iremote);CHKERRQ(ierr); 1454bbf5ea8SMatthew G. Knepley /* Now build an SF that just contains process connectivity. */ 1464bbf5ea8SMatthew G. Knepley PetscHashICreate(ranksUniq); 1474bbf5ea8SMatthew G. Knepley for (i = 0; i < n; ++i) { 1484bbf5ea8SMatthew G. Knepley const PetscMPIInt *ranks = NULL; 1494bbf5ea8SMatthew G. Knepley PetscInt nranks, j; 1504bbf5ea8SMatthew G. Knepley 1514bbf5ea8SMatthew G. Knepley ierr = PetscSFSetUp(sf[i]);CHKERRQ(ierr); 1524bbf5ea8SMatthew G. Knepley ierr = PetscSFGetRanks(sf[i], &nranks, &ranks, NULL, NULL, NULL);CHKERRQ(ierr); 1534bbf5ea8SMatthew G. Knepley /* These are all the ranks who communicate with me. */ 1544bbf5ea8SMatthew G. Knepley for (j = 0; j < nranks; ++j) { 1554bbf5ea8SMatthew G. Knepley PetscHashIAdd(ranksUniq, (PetscInt) ranks[j], 0); 1564bbf5ea8SMatthew G. Knepley } 1574bbf5ea8SMatthew G. Knepley } 1584bbf5ea8SMatthew G. Knepley PetscHashISize(ranksUniq, numRanks); 1594bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(numRanks, &remote);CHKERRQ(ierr); 1604bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(numRanks, &ranks);CHKERRQ(ierr); 1614bbf5ea8SMatthew G. Knepley PetscHashIGetKeys(ranksUniq, &index, ranks); 1624bbf5ea8SMatthew G. Knepley 1634bbf5ea8SMatthew G. Knepley PetscHashICreate(rankToIndex); 1644bbf5ea8SMatthew G. Knepley for (i = 0; i < numRanks; ++i) { 1654bbf5ea8SMatthew G. Knepley remote[i].rank = ranks[i]; 1664bbf5ea8SMatthew G. Knepley remote[i].index = 0; 1674bbf5ea8SMatthew G. Knepley PetscHashIAdd(rankToIndex, ranks[i], i); 1684bbf5ea8SMatthew G. Knepley } 1694bbf5ea8SMatthew G. Knepley ierr = PetscFree(ranks);CHKERRQ(ierr); 1704bbf5ea8SMatthew G. Knepley PetscHashIDestroy(ranksUniq); 1714bbf5ea8SMatthew G. Knepley ierr = PetscSFCreate(PetscObjectComm((PetscObject) pc), &rankSF);CHKERRQ(ierr); 1724bbf5ea8SMatthew G. Knepley ierr = PetscSFSetGraph(rankSF, 1, numRanks, NULL, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr); 1734bbf5ea8SMatthew G. Knepley ierr = PetscSFSetUp(rankSF);CHKERRQ(ierr); 1744bbf5ea8SMatthew G. Knepley /* OK, use it to communicate the root offset on the remote 1754bbf5ea8SMatthew G. Knepley * processes for each subspace. */ 1764bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(n, &offsets);CHKERRQ(ierr); 1774bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(n*numRanks, &remoteOffsets);CHKERRQ(ierr); 1784bbf5ea8SMatthew G. Knepley 1794bbf5ea8SMatthew G. Knepley offsets[0] = 0; 1804bbf5ea8SMatthew G. Knepley for (i = 1; i < n; ++i) { 1814bbf5ea8SMatthew G. Knepley PetscInt nroots; 1824bbf5ea8SMatthew G. Knepley 1834bbf5ea8SMatthew G. Knepley ierr = PetscSFGetGraph(sf[i-1], &nroots, NULL, NULL, NULL);CHKERRQ(ierr); 1844bbf5ea8SMatthew G. Knepley offsets[i] = offsets[i-1] + nroots*bs[i-1]; 1854bbf5ea8SMatthew G. Knepley } 1864bbf5ea8SMatthew G. Knepley /* Offsets are the offsets on the current process of the 1874bbf5ea8SMatthew G. Knepley * global dof numbering for the subspaces. */ 1884bbf5ea8SMatthew G. Knepley ierr = MPI_Type_contiguous(n, MPIU_INT, &contig);CHKERRQ(ierr); 1894bbf5ea8SMatthew G. Knepley ierr = MPI_Type_commit(&contig);CHKERRQ(ierr); 1904bbf5ea8SMatthew G. Knepley 1914bbf5ea8SMatthew G. Knepley ierr = PetscSFBcastBegin(rankSF, contig, offsets, remoteOffsets);CHKERRQ(ierr); 1924bbf5ea8SMatthew G. Knepley ierr = PetscSFBcastEnd(rankSF, contig, offsets, remoteOffsets);CHKERRQ(ierr); 1934bbf5ea8SMatthew G. Knepley ierr = MPI_Type_free(&contig);CHKERRQ(ierr); 1944bbf5ea8SMatthew G. Knepley ierr = PetscFree(offsets);CHKERRQ(ierr); 1954bbf5ea8SMatthew G. Knepley ierr = PetscSFDestroy(&rankSF);CHKERRQ(ierr); 1964bbf5ea8SMatthew G. Knepley /* Now remoteOffsets contains the offsets on the remote 1974bbf5ea8SMatthew G. Knepley * processes who communicate with me. So now we can 1984bbf5ea8SMatthew G. Knepley * concatenate the list of SFs into a single one. */ 1994bbf5ea8SMatthew G. Knepley index = 0; 2004bbf5ea8SMatthew G. Knepley for (i = 0; i < n; ++i) { 2014bbf5ea8SMatthew G. Knepley const PetscSFNode *remote = NULL; 2024bbf5ea8SMatthew G. Knepley const PetscInt *local = NULL; 2034bbf5ea8SMatthew G. Knepley PetscInt nroots, nleaves, j; 2044bbf5ea8SMatthew G. Knepley 2054bbf5ea8SMatthew G. Knepley ierr = PetscSFGetGraph(sf[i], &nroots, &nleaves, &local, &remote);CHKERRQ(ierr); 2064bbf5ea8SMatthew G. Knepley for (j = 0; j < nleaves; ++j) { 2074bbf5ea8SMatthew G. Knepley PetscInt rank = remote[j].rank; 2084bbf5ea8SMatthew G. Knepley PetscInt idx, rootOffset, k; 2094bbf5ea8SMatthew G. Knepley 2104bbf5ea8SMatthew G. Knepley PetscHashIMap(rankToIndex, rank, idx); 2114bbf5ea8SMatthew G. Knepley if (idx == -1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Didn't find rank, huh?"); 2124bbf5ea8SMatthew G. Knepley /* Offset on given rank for ith subspace */ 2134bbf5ea8SMatthew G. Knepley rootOffset = remoteOffsets[n*idx + i]; 2144bbf5ea8SMatthew G. Knepley for (k = 0; k < bs[i]; ++k) { 2154bbf5ea8SMatthew G. Knepley ilocal[index] = local[j]*bs[i] + k + leafOffset; 2164bbf5ea8SMatthew G. Knepley iremote[index].rank = remote[j].rank; 2174bbf5ea8SMatthew G. Knepley iremote[index].index = remote[j].index*bs[i] + k + rootOffset; 2184bbf5ea8SMatthew G. Knepley ++index; 2194bbf5ea8SMatthew G. Knepley } 2204bbf5ea8SMatthew G. Knepley } 2214bbf5ea8SMatthew G. Knepley leafOffset += nleaves * bs[i]; 2224bbf5ea8SMatthew G. Knepley } 2234bbf5ea8SMatthew G. Knepley PetscHashIDestroy(rankToIndex); 2244bbf5ea8SMatthew G. Knepley ierr = PetscFree(remoteOffsets);CHKERRQ(ierr); 2254bbf5ea8SMatthew G. Knepley ierr = PetscSFCreate(PetscObjectComm((PetscObject)pc), &patch->defaultSF);CHKERRQ(ierr); 2264bbf5ea8SMatthew G. Knepley ierr = PetscSFSetGraph(patch->defaultSF, allRoots, allLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER);CHKERRQ(ierr); 2274bbf5ea8SMatthew G. Knepley } 2284bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 2294bbf5ea8SMatthew G. Knepley } 2304bbf5ea8SMatthew G. Knepley 2314bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 232*5f824522SMatthew G. Knepley PetscErrorCode PCPatchSetIgnoreDim(PC pc, PetscInt dim) 233*5f824522SMatthew G. Knepley { 234*5f824522SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 235*5f824522SMatthew G. Knepley PetscFunctionBegin; 236*5f824522SMatthew G. Knepley patch->ignoredim = dim; 237*5f824522SMatthew G. Knepley PetscFunctionReturn(0); 238*5f824522SMatthew G. Knepley } 239*5f824522SMatthew G. Knepley 240*5f824522SMatthew G. Knepley /* TODO: Docs */ 241*5f824522SMatthew G. Knepley PetscErrorCode PCPatchGetIgnoreDim(PC pc, PetscInt *dim) 242*5f824522SMatthew G. Knepley { 243*5f824522SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 244*5f824522SMatthew G. Knepley PetscFunctionBegin; 245*5f824522SMatthew G. Knepley *dim = patch->ignoredim; 246*5f824522SMatthew G. Knepley PetscFunctionReturn(0); 247*5f824522SMatthew G. Knepley } 248*5f824522SMatthew G. Knepley 249*5f824522SMatthew G. Knepley /* TODO: Docs */ 2504bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetSaveOperators(PC pc, PetscBool flg) 2514bbf5ea8SMatthew G. Knepley { 2524bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 2534bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 2544bbf5ea8SMatthew G. Knepley patch->save_operators = flg; 2554bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 2564bbf5ea8SMatthew G. Knepley } 2574bbf5ea8SMatthew G. Knepley 2584bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 2594bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetSaveOperators(PC pc, PetscBool *flg) 2604bbf5ea8SMatthew G. Knepley { 2614bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 2624bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 2634bbf5ea8SMatthew G. Knepley *flg = patch->save_operators; 2644bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 2654bbf5ea8SMatthew G. Knepley } 2664bbf5ea8SMatthew G. Knepley 2674bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 2684bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetPartitionOfUnity(PC pc, PetscBool flg) 2694bbf5ea8SMatthew G. Knepley { 2704bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 2714bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 2724bbf5ea8SMatthew G. Knepley patch->partition_of_unity = flg; 2734bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 2744bbf5ea8SMatthew G. Knepley } 2754bbf5ea8SMatthew G. Knepley 2764bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 2774bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetPartitionOfUnity(PC pc, PetscBool *flg) 2784bbf5ea8SMatthew G. Knepley { 2794bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 2804bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 2814bbf5ea8SMatthew G. Knepley *flg = patch->partition_of_unity; 2824bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 2834bbf5ea8SMatthew G. Knepley } 2844bbf5ea8SMatthew G. Knepley 2854bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 2864bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetMultiplicative(PC pc, PetscBool flg) 2874bbf5ea8SMatthew G. Knepley { 2884bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 2894bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 2904bbf5ea8SMatthew G. Knepley patch->multiplicative = flg; 2914bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 2924bbf5ea8SMatthew G. Knepley } 2934bbf5ea8SMatthew G. Knepley 2944bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 2954bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetMultiplicative(PC pc, PetscBool *flg) 2964bbf5ea8SMatthew G. Knepley { 2974bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 2984bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 2994bbf5ea8SMatthew G. Knepley *flg = patch->multiplicative; 3004bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 3014bbf5ea8SMatthew G. Knepley } 3024bbf5ea8SMatthew G. Knepley 3034bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 3044bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetSubMatType(PC pc, MatType sub_mat_type) 3054bbf5ea8SMatthew G. Knepley { 3064bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 3074bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 3084bbf5ea8SMatthew G. Knepley 3094bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 3104bbf5ea8SMatthew G. Knepley if (patch->sub_mat_type) {ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr);} 3114bbf5ea8SMatthew G. Knepley ierr = PetscStrallocpy(sub_mat_type, (char **) &patch->sub_mat_type);CHKERRQ(ierr); 3124bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 3134bbf5ea8SMatthew G. Knepley } 3144bbf5ea8SMatthew G. Knepley 3154bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 3164bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetSubMatType(PC pc, MatType *sub_mat_type) 3174bbf5ea8SMatthew G. Knepley { 3184bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 3194bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 3204bbf5ea8SMatthew G. Knepley *sub_mat_type = patch->sub_mat_type; 3214bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 3224bbf5ea8SMatthew G. Knepley } 3234bbf5ea8SMatthew G. Knepley 3244bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 3254bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetCellNumbering(PC pc, PetscSection cellNumbering) 3264bbf5ea8SMatthew G. Knepley { 3274bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 3284bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 3294bbf5ea8SMatthew G. Knepley 3304bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 3314bbf5ea8SMatthew G. Knepley patch->cellNumbering = cellNumbering; 3324bbf5ea8SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) cellNumbering);CHKERRQ(ierr); 3334bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 3344bbf5ea8SMatthew G. Knepley } 3354bbf5ea8SMatthew G. Knepley 3364bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 3374bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetCellNumbering(PC pc, PetscSection *cellNumbering) 3384bbf5ea8SMatthew G. Knepley { 3394bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 3404bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 3414bbf5ea8SMatthew G. Knepley *cellNumbering = patch->cellNumbering; 3424bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 3434bbf5ea8SMatthew G. Knepley } 3444bbf5ea8SMatthew G. Knepley 3454bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 3464bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetConstructType(PC pc, PCPatchConstructType ctype, PetscErrorCode (*func)(PC, PetscInt *, IS **, IS *, void *), void *ctx) 3474bbf5ea8SMatthew G. Knepley { 3484bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 3494bbf5ea8SMatthew G. Knepley 3504bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 3514bbf5ea8SMatthew G. Knepley patch->ctype = ctype; 3524bbf5ea8SMatthew G. Knepley switch (ctype) { 3534bbf5ea8SMatthew G. Knepley case PC_PATCH_STAR: 3544bbf5ea8SMatthew G. Knepley patch->patchconstructop = PCPatchConstruct_Star; 3554bbf5ea8SMatthew G. Knepley break; 3564bbf5ea8SMatthew G. Knepley case PC_PATCH_VANKA: 3574bbf5ea8SMatthew G. Knepley patch->patchconstructop = PCPatchConstruct_Vanka; 3584bbf5ea8SMatthew G. Knepley break; 3594bbf5ea8SMatthew G. Knepley case PC_PATCH_USER: 3604bbf5ea8SMatthew G. Knepley case PC_PATCH_PYTHON: 3614bbf5ea8SMatthew G. Knepley patch->user_patches = PETSC_TRUE; 3624bbf5ea8SMatthew G. Knepley patch->patchconstructop = PCPatchConstruct_User; 3634bbf5ea8SMatthew G. Knepley patch->userpatchconstructionop = func; 3644bbf5ea8SMatthew G. Knepley patch->userpatchconstructctx = ctx; 3654bbf5ea8SMatthew G. Knepley break; 3664bbf5ea8SMatthew G. Knepley default: 3674bbf5ea8SMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype); 3684bbf5ea8SMatthew G. Knepley } 3694bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 3704bbf5ea8SMatthew G. Knepley } 3714bbf5ea8SMatthew G. Knepley 3724bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 3734bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetConstructType(PC pc, PCPatchConstructType *ctype, PetscErrorCode (**func)(PC, PetscInt *, IS **, IS *, void *), void **ctx) 3744bbf5ea8SMatthew G. Knepley { 3754bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 3764bbf5ea8SMatthew G. Knepley 3774bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 3784bbf5ea8SMatthew G. Knepley *ctype = patch->ctype; 3794bbf5ea8SMatthew G. Knepley switch (patch->ctype) { 3804bbf5ea8SMatthew G. Knepley case PC_PATCH_STAR: 3814bbf5ea8SMatthew G. Knepley case PC_PATCH_VANKA: 3824bbf5ea8SMatthew G. Knepley break; 3834bbf5ea8SMatthew G. Knepley case PC_PATCH_USER: 3844bbf5ea8SMatthew G. Knepley case PC_PATCH_PYTHON: 3854bbf5ea8SMatthew G. Knepley *func = patch->userpatchconstructionop; 3864bbf5ea8SMatthew G. Knepley *ctx = patch->userpatchconstructctx; 3874bbf5ea8SMatthew G. Knepley break; 3884bbf5ea8SMatthew G. Knepley default: 3894bbf5ea8SMatthew G. Knepley SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype); 3904bbf5ea8SMatthew G. Knepley } 3914bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 3924bbf5ea8SMatthew G. Knepley } 3934bbf5ea8SMatthew G. Knepley 3944bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 3954bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfo(PC pc, PetscInt nsubspaces, DM *dms, PetscInt *bs, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, 3964bbf5ea8SMatthew G. Knepley const PetscInt *subspaceOffsets, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes) 3974bbf5ea8SMatthew G. Knepley { 3984bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 399*5f824522SMatthew G. Knepley DM dm; 4004bbf5ea8SMatthew G. Knepley PetscSF *sfs; 401*5f824522SMatthew G. Knepley PetscInt cStart, cEnd, i, j; 4024bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 4034bbf5ea8SMatthew G. Knepley 4044bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 405*5f824522SMatthew G. Knepley ierr = PCGetDM(pc, &dm);CHKERRQ(ierr); 406*5f824522SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 4074bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(nsubspaces, &sfs);CHKERRQ(ierr); 4084bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(nsubspaces, &patch->dofSection);CHKERRQ(ierr); 4094bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(nsubspaces, &patch->bs);CHKERRQ(ierr); 4104bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(nsubspaces, &patch->nodesPerCell);CHKERRQ(ierr); 4114bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(nsubspaces, &patch->cellNodeMap);CHKERRQ(ierr); 4124bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(nsubspaces+1, &patch->subspaceOffsets);CHKERRQ(ierr); 4134bbf5ea8SMatthew G. Knepley 4144bbf5ea8SMatthew G. Knepley patch->nsubspaces = nsubspaces; 4154bbf5ea8SMatthew G. Knepley patch->totalDofsPerCell = 0; 4164bbf5ea8SMatthew G. Knepley for (i = 0; i < nsubspaces; ++i) { 4174bbf5ea8SMatthew G. Knepley ierr = DMGetDefaultSection(dms[i], &patch->dofSection[i]);CHKERRQ(ierr); 4184bbf5ea8SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) patch->dofSection[i]);CHKERRQ(ierr); 4194bbf5ea8SMatthew G. Knepley ierr = DMGetDefaultSF(dms[i], &sfs[i]);CHKERRQ(ierr); 4204bbf5ea8SMatthew G. Knepley patch->bs[i] = bs[i]; 4214bbf5ea8SMatthew G. Knepley patch->nodesPerCell[i] = nodesPerCell[i]; 4224bbf5ea8SMatthew G. Knepley patch->totalDofsPerCell += nodesPerCell[i]*bs[i]; 423*5f824522SMatthew G. Knepley ierr = PetscMalloc1((cEnd-cStart)*nodesPerCell[i]*bs[i], &patch->cellNodeMap[i]);CHKERRQ(ierr); 424*5f824522SMatthew G. Knepley for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]*bs[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j]; 4254bbf5ea8SMatthew G. Knepley patch->subspaceOffsets[i] = subspaceOffsets[i]; 4264bbf5ea8SMatthew G. Knepley } 4274bbf5ea8SMatthew G. Knepley ierr = PCPatchCreateDefaultSF_Private(pc, nsubspaces, sfs, patch->bs);CHKERRQ(ierr); 4284bbf5ea8SMatthew G. Knepley ierr = PetscFree(sfs);CHKERRQ(ierr); 4294bbf5ea8SMatthew G. Knepley 4304bbf5ea8SMatthew G. Knepley patch->subspaceOffsets[nsubspaces] = subspaceOffsets[nsubspaces]; 4314bbf5ea8SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes);CHKERRQ(ierr); 4324bbf5ea8SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes);CHKERRQ(ierr); 4334bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 4344bbf5ea8SMatthew G. Knepley } 4354bbf5ea8SMatthew G. Knepley 4364bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 437*5f824522SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfoCombined(PC pc, DM dm, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes) 438*5f824522SMatthew G. Knepley { 439*5f824522SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 440*5f824522SMatthew G. Knepley PetscInt cStart, cEnd, i, j; 441*5f824522SMatthew G. Knepley PetscErrorCode ierr; 442*5f824522SMatthew G. Knepley 443*5f824522SMatthew G. Knepley PetscFunctionBegin; 444*5f824522SMatthew G. Knepley patch->combined = PETSC_TRUE; 445*5f824522SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 446*5f824522SMatthew G. Knepley ierr = DMGetNumFields(dm, &patch->nsubspaces);CHKERRQ(ierr); 447*5f824522SMatthew G. Knepley ierr = PetscCalloc1(patch->nsubspaces, &patch->dofSection);CHKERRQ(ierr); 448*5f824522SMatthew G. Knepley ierr = PetscMalloc1(patch->nsubspaces, &patch->bs);CHKERRQ(ierr); 449*5f824522SMatthew G. Knepley ierr = PetscMalloc1(patch->nsubspaces, &patch->nodesPerCell);CHKERRQ(ierr); 450*5f824522SMatthew G. Knepley ierr = PetscMalloc1(patch->nsubspaces, &patch->cellNodeMap);CHKERRQ(ierr); 451*5f824522SMatthew G. Knepley ierr = PetscCalloc1(patch->nsubspaces+1, &patch->subspaceOffsets);CHKERRQ(ierr); 452*5f824522SMatthew G. Knepley ierr = DMGetDefaultSection(dm, &patch->dofSection[0]);CHKERRQ(ierr); 453*5f824522SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) patch->dofSection[0]);CHKERRQ(ierr); 454*5f824522SMatthew G. Knepley ierr = PetscSectionGetStorageSize(patch->dofSection[0], &patch->subspaceOffsets[patch->nsubspaces]);CHKERRQ(ierr); 455*5f824522SMatthew G. Knepley patch->totalDofsPerCell = 0; 456*5f824522SMatthew G. Knepley for (i = 0; i < patch->nsubspaces; ++i) { 457*5f824522SMatthew G. Knepley patch->bs[i] = 1; 458*5f824522SMatthew G. Knepley patch->nodesPerCell[i] = nodesPerCell[i]; 459*5f824522SMatthew G. Knepley patch->totalDofsPerCell += nodesPerCell[i]; 460*5f824522SMatthew G. Knepley ierr = PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i]);CHKERRQ(ierr); 461*5f824522SMatthew G. Knepley for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j]; 462*5f824522SMatthew G. Knepley } 463*5f824522SMatthew G. Knepley ierr = DMGetDefaultSF(dm, &patch->defaultSF);CHKERRQ(ierr); 464*5f824522SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) patch->defaultSF);CHKERRQ(ierr); 465*5f824522SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes);CHKERRQ(ierr); 466*5f824522SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes);CHKERRQ(ierr); 467*5f824522SMatthew G. Knepley PetscFunctionReturn(0); 468*5f824522SMatthew G. Knepley } 469*5f824522SMatthew G. Knepley 470*5f824522SMatthew G. Knepley /*@C 471*5f824522SMatthew G. Knepley 472*5f824522SMatthew G. Knepley PCPatchSetComputeOperator - Set the callback used to compute patch matrices 473*5f824522SMatthew G. Knepley 474*5f824522SMatthew G. Knepley Input Parameters: 475*5f824522SMatthew G. Knepley + pc - The PC 476*5f824522SMatthew G. Knepley . func - The callback 477*5f824522SMatthew G. Knepley - ctx - The user context 478*5f824522SMatthew G. Knepley 479*5f824522SMatthew G. Knepley Level: advanced 480*5f824522SMatthew G. Knepley 481*5f824522SMatthew G. Knepley Note: 482*5f824522SMatthew G. Knepley The callback has signature: 483*5f824522SMatthew G. Knepley + usercomputeop(pc, mat, ncell, cells, n, u, ctx) 484*5f824522SMatthew G. Knepley + pc - The PC 485*5f824522SMatthew G. Knepley + mat - The patch matrix 486*5f824522SMatthew G. Knepley + ncell - The number of cells to integrate over 487*5f824522SMatthew G. Knepley + cells - An array of the cell numbers 488*5f824522SMatthew G. Knepley + n - The size of g2l 489*5f824522SMatthew G. Knepley + g2l - The global to local dof translation table 490*5f824522SMatthew G. Knepley + ctx - The user context 491*5f824522SMatthew G. Knepley and can assume that the matrix entries have been set to zero before the call. 492*5f824522SMatthew G. Knepley 493*5f824522SMatthew G. Knepley .seealso: PCPatchGetComputeOperator(), PCPatchSetDiscretisationInfo() 494*5f824522SMatthew G. Knepley @*/ 495*5f824522SMatthew G. Knepley PetscErrorCode PCPatchSetComputeOperator(PC pc, PetscErrorCode (*func)(PC, PetscInt, Mat, PetscInt, const PetscInt [], PetscInt, const PetscInt *, void *), void *ctx) 4964bbf5ea8SMatthew G. Knepley { 4974bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 4984bbf5ea8SMatthew G. Knepley 4994bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 5004bbf5ea8SMatthew G. Knepley patch->usercomputeop = func; 5014bbf5ea8SMatthew G. Knepley patch->usercomputectx = ctx; 5024bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 5034bbf5ea8SMatthew G. Knepley } 5044bbf5ea8SMatthew G. Knepley 5054bbf5ea8SMatthew G. Knepley /* On entry, ht contains the topological entities whose dofs we are responsible for solving for; 5064bbf5ea8SMatthew G. Knepley on exit, cht contains all the topological entities we need to compute their residuals. 5074bbf5ea8SMatthew G. Knepley In full generality this should incorporate knowledge of the sparsity pattern of the matrix; 5084bbf5ea8SMatthew G. Knepley here we assume a standard FE sparsity pattern.*/ 5094bbf5ea8SMatthew G. Knepley /* TODO: Use DMPlexGetAdjacency() */ 5104bbf5ea8SMatthew G. Knepley /* TODO: Look at temp buffer management for GetClosure() */ 511*5f824522SMatthew G. Knepley static PetscErrorCode PCPatchCompleteCellPatch(PC pc, PetscHashI ht, PetscHashI cht) 5124bbf5ea8SMatthew G. Knepley { 513*5f824522SMatthew G. Knepley DM dm; 5144bbf5ea8SMatthew G. Knepley PetscHashIIter hi; 5154bbf5ea8SMatthew G. Knepley PetscInt point; 5164bbf5ea8SMatthew G. Knepley PetscInt *star = NULL, *closure = NULL; 517*5f824522SMatthew G. Knepley PetscInt ignoredim, iStart, iEnd, starSize, closureSize, si, ci; 5184bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 5194bbf5ea8SMatthew G. Knepley 5204bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 521*5f824522SMatthew G. Knepley ierr = PCGetDM(pc, &dm);CHKERRQ(ierr); 522*5f824522SMatthew G. Knepley ierr = PCPatchGetIgnoreDim(pc, &ignoredim);CHKERRQ(ierr); 523*5f824522SMatthew G. Knepley if (ignoredim >= 0) {ierr = DMPlexGetDepthStratum(dm, ignoredim, &iStart, &iEnd);CHKERRQ(ierr);} 5244bbf5ea8SMatthew G. Knepley PetscHashIClear(cht); 5254bbf5ea8SMatthew G. Knepley PetscHashIIterBegin(ht, hi); 5264bbf5ea8SMatthew G. Knepley while (!PetscHashIIterAtEnd(ht, hi)) { 5274bbf5ea8SMatthew G. Knepley PetscHashIIterGetKey(ht, hi, point); 5284bbf5ea8SMatthew G. Knepley PetscHashIIterNext(ht, hi); 5294bbf5ea8SMatthew G. Knepley 5304bbf5ea8SMatthew G. Knepley /* Loop over all the cells that this point connects to */ 5314bbf5ea8SMatthew G. Knepley ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr); 532*5f824522SMatthew G. Knepley for (si = 0; si < starSize*2; si += 2) { 5334bbf5ea8SMatthew G. Knepley PetscInt ownedpoint = star[si]; 534*5f824522SMatthew G. Knepley /* TODO Check for point in cht before running through closure again */ 5354bbf5ea8SMatthew G. Knepley /* now loop over all entities in the closure of that cell */ 5364bbf5ea8SMatthew G. Knepley ierr = DMPlexGetTransitiveClosure(dm, ownedpoint, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 537*5f824522SMatthew G. Knepley for (ci = 0; ci < closureSize*2; ci += 2) { 5384bbf5ea8SMatthew G. Knepley PetscInt seenpoint = closure[ci]; 539*5f824522SMatthew G. Knepley if (ignoredim >= 0 && seenpoint >= iStart && seenpoint < iEnd) continue; 5404bbf5ea8SMatthew G. Knepley PetscHashIAdd(cht, seenpoint, 0); 5414bbf5ea8SMatthew G. Knepley } 5424bbf5ea8SMatthew G. Knepley } 543*5f824522SMatthew G. Knepley ierr = DMPlexRestoreTransitiveClosure(dm, 0, PETSC_TRUE, NULL, &closure);CHKERRQ(ierr); 5444bbf5ea8SMatthew G. Knepley } 545*5f824522SMatthew G. Knepley ierr = DMPlexRestoreTransitiveClosure(dm, 0, PETSC_FALSE, NULL, &star);CHKERRQ(ierr); 546*5f824522SMatthew G. Knepley PetscFunctionReturn(0); 547*5f824522SMatthew G. Knepley } 548*5f824522SMatthew G. Knepley 549*5f824522SMatthew G. Knepley static PetscErrorCode PCPatchGetGlobalDofs(PC pc, PetscSection dofSection[], PetscInt f, PetscBool combined, PetscInt p, PetscInt *dof, PetscInt *off) 550*5f824522SMatthew G. Knepley { 551*5f824522SMatthew G. Knepley PetscErrorCode ierr; 552*5f824522SMatthew G. Knepley 553*5f824522SMatthew G. Knepley PetscFunctionBegin; 554*5f824522SMatthew G. Knepley if (combined) { 555*5f824522SMatthew G. Knepley if (f < 0) { 556*5f824522SMatthew G. Knepley if (dof) {ierr = PetscSectionGetDof(dofSection[0], p, dof);CHKERRQ(ierr);} 557*5f824522SMatthew G. Knepley if (off) {ierr = PetscSectionGetOffset(dofSection[0], p, off);CHKERRQ(ierr);} 558*5f824522SMatthew G. Knepley } else { 559*5f824522SMatthew G. Knepley if (dof) {ierr = PetscSectionGetFieldDof(dofSection[0], p, f, dof);CHKERRQ(ierr);} 560*5f824522SMatthew G. Knepley if (off) {ierr = PetscSectionGetFieldOffset(dofSection[0], p, f, off);CHKERRQ(ierr);} 561*5f824522SMatthew G. Knepley } 562*5f824522SMatthew G. Knepley } else { 563*5f824522SMatthew G. Knepley if (f < 0) { 564*5f824522SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 565*5f824522SMatthew G. Knepley PetscInt fdof, g; 566*5f824522SMatthew G. Knepley 567*5f824522SMatthew G. Knepley if (dof) { 568*5f824522SMatthew G. Knepley *dof = 0; 569*5f824522SMatthew G. Knepley for (g = 0; g < patch->nsubspaces; ++g) { 570*5f824522SMatthew G. Knepley ierr = PetscSectionGetDof(dofSection[g], p, &fdof);CHKERRQ(ierr); 571*5f824522SMatthew G. Knepley *dof += fdof; 572*5f824522SMatthew G. Knepley } 573*5f824522SMatthew G. Knepley } 574*5f824522SMatthew G. Knepley if (off) {ierr = PetscSectionGetOffset(dofSection[0], p, off);CHKERRQ(ierr);} 575*5f824522SMatthew G. Knepley } else { 576*5f824522SMatthew G. Knepley if (dof) {ierr = PetscSectionGetDof(dofSection[f], p, dof);CHKERRQ(ierr);} 577*5f824522SMatthew G. Knepley if (off) {ierr = PetscSectionGetOffset(dofSection[f], p, off);CHKERRQ(ierr);} 578*5f824522SMatthew G. Knepley } 579*5f824522SMatthew G. Knepley } 5804bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 5814bbf5ea8SMatthew G. Knepley } 5824bbf5ea8SMatthew G. Knepley 5834bbf5ea8SMatthew G. Knepley /* Given a hash table with a set of topological entities (pts), compute the degrees of 5844bbf5ea8SMatthew G. Knepley freedom in global concatenated numbering on those entities. 5854bbf5ea8SMatthew G. Knepley For Vanka smoothing, this needs to do something special: ignore dofs of the 5864bbf5ea8SMatthew G. Knepley constraint subspace on entities that aren't the base entity we're building the patch 5874bbf5ea8SMatthew G. Knepley around. */ 588*5f824522SMatthew G. Knepley static PetscErrorCode PCPatchGetPointDofs(PC pc, PetscHashI pts, PetscHashI dofs, PetscInt base, PetscInt exclude_subspace) 5894bbf5ea8SMatthew G. Knepley { 590*5f824522SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 5914bbf5ea8SMatthew G. Knepley PetscHashIIter hi; 5924bbf5ea8SMatthew G. Knepley PetscInt ldof, loff; 5934bbf5ea8SMatthew G. Knepley PetscInt k, p; 5944bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 5954bbf5ea8SMatthew G. Knepley 5964bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 5974bbf5ea8SMatthew G. Knepley PetscHashIClear(dofs); 5984bbf5ea8SMatthew G. Knepley for (k = 0; k < patch->nsubspaces; ++k) { 5994bbf5ea8SMatthew G. Knepley PetscInt subspaceOffset = patch->subspaceOffsets[k]; 6004bbf5ea8SMatthew G. Knepley PetscInt bs = patch->bs[k]; 6014bbf5ea8SMatthew G. Knepley PetscInt j, l; 6024bbf5ea8SMatthew G. Knepley 6034bbf5ea8SMatthew G. Knepley if (k == exclude_subspace) { 6044bbf5ea8SMatthew G. Knepley /* only get this subspace dofs at the base entity, not any others */ 605*5f824522SMatthew G. Knepley ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, base, &ldof, &loff);CHKERRQ(ierr); 6064bbf5ea8SMatthew G. Knepley if (0 == ldof) continue; 6074bbf5ea8SMatthew G. Knepley for (j = loff; j < ldof + loff; ++j) { 6084bbf5ea8SMatthew G. Knepley for (l = 0; l < bs; ++l) { 6094bbf5ea8SMatthew G. Knepley PetscInt dof = bs*j + l + subspaceOffset; 6104bbf5ea8SMatthew G. Knepley PetscHashIAdd(dofs, dof, 0); 6114bbf5ea8SMatthew G. Knepley } 6124bbf5ea8SMatthew G. Knepley } 6134bbf5ea8SMatthew G. Knepley continue; /* skip the other dofs of this subspace */ 6144bbf5ea8SMatthew G. Knepley } 6154bbf5ea8SMatthew G. Knepley 6164bbf5ea8SMatthew G. Knepley PetscHashIIterBegin(pts, hi); 6174bbf5ea8SMatthew G. Knepley while (!PetscHashIIterAtEnd(pts, hi)) { 6184bbf5ea8SMatthew G. Knepley PetscHashIIterGetKey(pts, hi, p); 6194bbf5ea8SMatthew G. Knepley PetscHashIIterNext(pts, hi); 620*5f824522SMatthew G. Knepley ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, p, &ldof, &loff);CHKERRQ(ierr); 6214bbf5ea8SMatthew G. Knepley if (0 == ldof) continue; 6224bbf5ea8SMatthew G. Knepley for (j = loff; j < ldof + loff; ++j) { 6234bbf5ea8SMatthew G. Knepley for (l = 0; l < bs; ++l) { 6244bbf5ea8SMatthew G. Knepley PetscInt dof = bs*j + l + subspaceOffset; 6254bbf5ea8SMatthew G. Knepley PetscHashIAdd(dofs, dof, 0); 6264bbf5ea8SMatthew G. Knepley } 6274bbf5ea8SMatthew G. Knepley } 6284bbf5ea8SMatthew G. Knepley } 6294bbf5ea8SMatthew G. Knepley } 6304bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 6314bbf5ea8SMatthew G. Knepley } 6324bbf5ea8SMatthew G. Knepley 6334bbf5ea8SMatthew G. Knepley /* Given two hash tables A and B, compute the keys in B that are not in A, and put them in C */ 6344bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchComputeSetDifference_Private(PetscHashI A, PetscHashI B, PetscHashI C) 6354bbf5ea8SMatthew G. Knepley { 6364bbf5ea8SMatthew G. Knepley PetscHashIIter hi; 6374bbf5ea8SMatthew G. Knepley PetscInt key, val; 6384bbf5ea8SMatthew G. Knepley PetscBool flg; 6394bbf5ea8SMatthew G. Knepley 6404bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 6414bbf5ea8SMatthew G. Knepley PetscHashIClear(C); 6424bbf5ea8SMatthew G. Knepley PetscHashIIterBegin(B, hi); 6434bbf5ea8SMatthew G. Knepley while (!PetscHashIIterAtEnd(B, hi)) { 6444bbf5ea8SMatthew G. Knepley PetscHashIIterGetKeyVal(B, hi, key, val); 6454bbf5ea8SMatthew G. Knepley PetscHashIIterNext(B, hi); 6464bbf5ea8SMatthew G. Knepley PetscHashIHasKey(A, key, flg); 6474bbf5ea8SMatthew G. Knepley if (!flg) {PetscHashIAdd(C, key, val);} 6484bbf5ea8SMatthew G. Knepley } 6494bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 6504bbf5ea8SMatthew G. Knepley } 6514bbf5ea8SMatthew G. Knepley 6524bbf5ea8SMatthew G. Knepley /* 6534bbf5ea8SMatthew G. Knepley * PCPatchCreateCellPatches - create patches. 6544bbf5ea8SMatthew G. Knepley * 6554bbf5ea8SMatthew G. Knepley * Input Parameters: 6564bbf5ea8SMatthew G. Knepley * + dm - The DMPlex object defining the mesh 6574bbf5ea8SMatthew G. Knepley * 6584bbf5ea8SMatthew G. Knepley * Output Parameters: 6594bbf5ea8SMatthew G. Knepley * + cellCounts - Section with counts of cells around each vertex 660*5f824522SMatthew G. Knepley * . cells - IS of the cell point indices of cells in each patch 661*5f824522SMatthew G. Knepley * . pointCounts - Section with counts of cells around each vertex 662*5f824522SMatthew G. Knepley * - point - IS of the cell point indices of cells in each patch 6634bbf5ea8SMatthew G. Knepley */ 6644bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatches(PC pc) 6654bbf5ea8SMatthew G. Knepley { 6664bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 667*5f824522SMatthew G. Knepley DMLabel ghost = NULL; 6684bbf5ea8SMatthew G. Knepley DM dm, plex; 6694bbf5ea8SMatthew G. Knepley PetscHashI ht, cht; 670*5f824522SMatthew G. Knepley PetscSection cellCounts, pointCounts; 671*5f824522SMatthew G. Knepley PetscInt *cellsArray, *pointsArray; 672*5f824522SMatthew G. Knepley PetscInt numCells, numPoints; 673*5f824522SMatthew G. Knepley const PetscInt *leaves; 674*5f824522SMatthew G. Knepley PetscInt nleaves, pStart, pEnd, cStart, cEnd, vStart, vEnd, v; 675*5f824522SMatthew G. Knepley PetscBool isFiredrake; 6764bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 6774bbf5ea8SMatthew G. Knepley 6784bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 6794bbf5ea8SMatthew G. Knepley /* Used to keep track of the cells in the patch. */ 6804bbf5ea8SMatthew G. Knepley PetscHashICreate(ht); 6814bbf5ea8SMatthew G. Knepley PetscHashICreate(cht); 6824bbf5ea8SMatthew G. Knepley 6834bbf5ea8SMatthew G. Knepley ierr = PCGetDM(pc, &dm);CHKERRQ(ierr); 6844bbf5ea8SMatthew G. Knepley if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONGSTATE, "DM not yet set on patch PC\n"); 6854bbf5ea8SMatthew G. Knepley ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr); 6864bbf5ea8SMatthew G. Knepley ierr = DMPlexGetChart(plex, &pStart, &pEnd);CHKERRQ(ierr); 6874bbf5ea8SMatthew G. Knepley ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr); 6884bbf5ea8SMatthew G. Knepley 6894bbf5ea8SMatthew G. Knepley if (patch->user_patches) { 690*5f824522SMatthew G. Knepley ierr = patch->userpatchconstructionop(pc, &patch->npatch, &patch->userIS, &patch->iterationSet, patch->userpatchconstructctx);CHKERRQ(ierr); 691*5f824522SMatthew G. Knepley vStart = 0; vEnd = patch->npatch; 692*5f824522SMatthew G. Knepley } else if (patch->codim < 0) { 693*5f824522SMatthew G. Knepley if (patch->dim < 0) {ierr = DMPlexGetDepthStratum(plex, 0, &vStart, &vEnd);CHKERRQ(ierr);} 694*5f824522SMatthew G. Knepley else {ierr = DMPlexGetDepthStratum(plex, patch->dim, &vStart, &vEnd);CHKERRQ(ierr);} 695*5f824522SMatthew G. Knepley } else {ierr = DMPlexGetHeightStratum(plex, patch->codim, &vStart, &vEnd);CHKERRQ(ierr);} 696*5f824522SMatthew G. Knepley patch->npatch = vEnd - vStart; 6974bbf5ea8SMatthew G. Knepley 6984bbf5ea8SMatthew G. Knepley /* These labels mark the owned points. We only create patches around points that this process owns. */ 699*5f824522SMatthew G. Knepley ierr = DMHasLabel(dm, "pyop2_ghost", &isFiredrake);CHKERRQ(ierr); 700*5f824522SMatthew G. Knepley if (isFiredrake) { 7014bbf5ea8SMatthew G. Knepley ierr = DMGetLabel(dm, "pyop2_ghost", &ghost);CHKERRQ(ierr); 7024bbf5ea8SMatthew G. Knepley ierr = DMLabelCreateIndex(ghost, pStart, pEnd);CHKERRQ(ierr); 703*5f824522SMatthew G. Knepley } else { 704*5f824522SMatthew G. Knepley PetscSF sf; 705*5f824522SMatthew G. Knepley 706*5f824522SMatthew G. Knepley ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr); 707*5f824522SMatthew G. Knepley ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr); 708*5f824522SMatthew G. Knepley nleaves = PetscMax(nleaves, 0); 709*5f824522SMatthew G. Knepley } 7104bbf5ea8SMatthew G. Knepley 7114bbf5ea8SMatthew G. Knepley ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->cellCounts);CHKERRQ(ierr); 712*5f824522SMatthew G. Knepley ierr = PetscObjectSetName((PetscObject) patch->cellCounts, "Patch Cell Layout");CHKERRQ(ierr); 7134bbf5ea8SMatthew G. Knepley cellCounts = patch->cellCounts; 7144bbf5ea8SMatthew G. Knepley ierr = PetscSectionSetChart(cellCounts, vStart, vEnd);CHKERRQ(ierr); 715*5f824522SMatthew G. Knepley ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->pointCounts);CHKERRQ(ierr); 716*5f824522SMatthew G. Knepley ierr = PetscObjectSetName((PetscObject) patch->pointCounts, "Patch Point Layout");CHKERRQ(ierr); 717*5f824522SMatthew G. Knepley pointCounts = patch->pointCounts; 718*5f824522SMatthew G. Knepley ierr = PetscSectionSetChart(pointCounts, vStart, vEnd);CHKERRQ(ierr); 719*5f824522SMatthew G. Knepley /* Count cells and points in the patch surrounding each entity */ 7204bbf5ea8SMatthew G. Knepley for (v = vStart; v < vEnd; ++v) { 7214bbf5ea8SMatthew G. Knepley PetscHashIIter hi; 722*5f824522SMatthew G. Knepley PetscInt chtSize, loc = -1; 723*5f824522SMatthew G. Knepley PetscBool flg; 7244bbf5ea8SMatthew G. Knepley 7254bbf5ea8SMatthew G. Knepley if (!patch->user_patches) { 726*5f824522SMatthew G. Knepley if (ghost) {ierr = DMLabelHasPoint(ghost, v, &flg);CHKERRQ(ierr);} 727*5f824522SMatthew G. Knepley else {ierr = PetscFindInt(v, nleaves, leaves, &loc); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;} 7284bbf5ea8SMatthew G. Knepley /* Not an owned entity, don't make a cell patch. */ 7294bbf5ea8SMatthew G. Knepley if (flg) continue; 7304bbf5ea8SMatthew G. Knepley } 7314bbf5ea8SMatthew G. Knepley 7324bbf5ea8SMatthew G. Knepley ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr); 733*5f824522SMatthew G. Knepley ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr); 7344bbf5ea8SMatthew G. Knepley PetscHashISize(cht, chtSize); 7354bbf5ea8SMatthew G. Knepley /* empty patch, continue */ 7364bbf5ea8SMatthew G. Knepley if (chtSize == 0) continue; 7374bbf5ea8SMatthew G. Knepley 7384bbf5ea8SMatthew G. Knepley /* safe because size(cht) > 0 from above */ 7394bbf5ea8SMatthew G. Knepley PetscHashIIterBegin(cht, hi); 7404bbf5ea8SMatthew G. Knepley while (!PetscHashIIterAtEnd(cht, hi)) { 741*5f824522SMatthew G. Knepley PetscInt point, pdof; 7424bbf5ea8SMatthew G. Knepley 7434bbf5ea8SMatthew G. Knepley PetscHashIIterGetKey(cht, hi, point); 744*5f824522SMatthew G. Knepley ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr); 745*5f824522SMatthew G. Knepley if (pdof) {ierr = PetscSectionAddDof(pointCounts, v, 1);CHKERRQ(ierr);} 746*5f824522SMatthew G. Knepley if (point >= cStart && point < cEnd) {ierr = PetscSectionAddDof(cellCounts, v, 1);CHKERRQ(ierr);} 7474bbf5ea8SMatthew G. Knepley PetscHashIIterNext(cht, hi); 7484bbf5ea8SMatthew G. Knepley } 7494bbf5ea8SMatthew G. Knepley } 750*5f824522SMatthew G. Knepley if (isFiredrake) {ierr = DMLabelDestroyIndex(ghost);CHKERRQ(ierr);} 7514bbf5ea8SMatthew G. Knepley 7524bbf5ea8SMatthew G. Knepley ierr = PetscSectionSetUp(cellCounts);CHKERRQ(ierr); 7534bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr); 7544bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(numCells, &cellsArray);CHKERRQ(ierr); 755*5f824522SMatthew G. Knepley ierr = PetscSectionSetUp(pointCounts);CHKERRQ(ierr); 756*5f824522SMatthew G. Knepley ierr = PetscSectionGetStorageSize(pointCounts, &numPoints);CHKERRQ(ierr); 757*5f824522SMatthew G. Knepley ierr = PetscMalloc1(numPoints, &pointsArray);CHKERRQ(ierr); 7584bbf5ea8SMatthew G. Knepley 7594bbf5ea8SMatthew G. Knepley /* Now that we know how much space we need, run through again and actually remember the cells. */ 7604bbf5ea8SMatthew G. Knepley for (v = vStart; v < vEnd; v++ ) { 7614bbf5ea8SMatthew G. Knepley PetscHashIIter hi; 762*5f824522SMatthew G. Knepley PetscInt dof, off, cdof, coff, pdof, n = 0, cn = 0; 7634bbf5ea8SMatthew G. Knepley 764*5f824522SMatthew G. Knepley ierr = PetscSectionGetDof(pointCounts, v, &dof);CHKERRQ(ierr); 765*5f824522SMatthew G. Knepley ierr = PetscSectionGetOffset(pointCounts, v, &off);CHKERRQ(ierr); 766*5f824522SMatthew G. Knepley ierr = PetscSectionGetDof(cellCounts, v, &cdof);CHKERRQ(ierr); 767*5f824522SMatthew G. Knepley ierr = PetscSectionGetOffset(cellCounts, v, &coff);CHKERRQ(ierr); 768*5f824522SMatthew G. Knepley if (dof <= 0) continue; 7694bbf5ea8SMatthew G. Knepley ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr); 770*5f824522SMatthew G. Knepley ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr); 7714bbf5ea8SMatthew G. Knepley PetscHashIIterBegin(cht, hi); 7724bbf5ea8SMatthew G. Knepley while (!PetscHashIIterAtEnd(cht, hi)) { 7734bbf5ea8SMatthew G. Knepley PetscInt point; 7744bbf5ea8SMatthew G. Knepley 7754bbf5ea8SMatthew G. Knepley PetscHashIIterGetKey(cht, hi, point); 776*5f824522SMatthew G. Knepley ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr); 777*5f824522SMatthew G. Knepley if (pdof) {pointsArray[off + n++] = point;} 778*5f824522SMatthew G. Knepley if (point >= cStart && point < cEnd) {cellsArray[coff + cn++] = point;} 7794bbf5ea8SMatthew G. Knepley PetscHashIIterNext(cht, hi); 7804bbf5ea8SMatthew G. Knepley } 781*5f824522SMatthew G. Knepley 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); 782*5f824522SMatthew G. Knepley 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); 7834bbf5ea8SMatthew G. Knepley } 7844bbf5ea8SMatthew G. Knepley PetscHashIDestroy(ht); 7854bbf5ea8SMatthew G. Knepley PetscHashIDestroy(cht); 7864bbf5ea8SMatthew G. Knepley ierr = DMDestroy(&plex);CHKERRQ(ierr); 787*5f824522SMatthew G. Knepley 788*5f824522SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, numCells, cellsArray, PETSC_OWN_POINTER, &patch->cells);CHKERRQ(ierr); 789*5f824522SMatthew G. Knepley ierr = PetscObjectSetName((PetscObject) patch->cells, "Patch Cells");CHKERRQ(ierr); 790*5f824522SMatthew G. Knepley if (patch->viewCells) { 791*5f824522SMatthew G. Knepley ierr = ObjectView((PetscObject) patch->cellCounts, patch->viewerCells, patch->formatCells);CHKERRQ(ierr); 792*5f824522SMatthew G. Knepley ierr = ObjectView((PetscObject) patch->cells, patch->viewerCells, patch->formatCells);CHKERRQ(ierr); 793*5f824522SMatthew G. Knepley } 794*5f824522SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints, pointsArray, PETSC_OWN_POINTER, &patch->points);CHKERRQ(ierr); 795*5f824522SMatthew G. Knepley ierr = PetscObjectSetName((PetscObject) patch->points, "Patch Points");CHKERRQ(ierr); 796*5f824522SMatthew G. Knepley if (patch->viewPoints) { 797*5f824522SMatthew G. Knepley ierr = ObjectView((PetscObject) patch->pointCounts, patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr); 798*5f824522SMatthew G. Knepley ierr = ObjectView((PetscObject) patch->points, patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr); 799*5f824522SMatthew G. Knepley } 8004bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 8014bbf5ea8SMatthew G. Knepley } 8024bbf5ea8SMatthew G. Knepley 8034bbf5ea8SMatthew G. Knepley /* 8044bbf5ea8SMatthew G. Knepley * PCPatchCreateCellPatchDiscretisationInfo - Build the dof maps for cell patches 8054bbf5ea8SMatthew G. Knepley * 8064bbf5ea8SMatthew G. Knepley * Input Parameters: 8074bbf5ea8SMatthew G. Knepley * + dm - The DMPlex object defining the mesh 8084bbf5ea8SMatthew G. Knepley * . cellCounts - Section with counts of cells around each vertex 8094bbf5ea8SMatthew G. Knepley * . cells - IS of the cell point indices of cells in each patch 8104bbf5ea8SMatthew G. Knepley * . cellNumbering - Section mapping plex cell points to Firedrake cell indices. 8114bbf5ea8SMatthew G. Knepley * . nodesPerCell - number of nodes per cell. 8124bbf5ea8SMatthew G. Knepley * - cellNodeMap - map from cells to node indices (nodesPerCell * numCells) 8134bbf5ea8SMatthew G. Knepley * 8144bbf5ea8SMatthew G. Knepley * Output Parameters: 815*5f824522SMatthew G. Knepley * + dofs - IS of local dof numbers of each cell in the patch, where local is a patch local numbering 8164bbf5ea8SMatthew G. Knepley * . gtolCounts - Section with counts of dofs per cell patch 8174bbf5ea8SMatthew G. Knepley * - gtol - IS mapping from global dofs to local dofs for each patch. 8184bbf5ea8SMatthew G. Knepley */ 8194bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatchDiscretisationInfo(PC pc) 8204bbf5ea8SMatthew G. Knepley { 8214bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 8224bbf5ea8SMatthew G. Knepley PetscSection cellCounts = patch->cellCounts; 823*5f824522SMatthew G. Knepley PetscSection pointCounts = patch->pointCounts; 8244bbf5ea8SMatthew G. Knepley PetscSection gtolCounts; 8254bbf5ea8SMatthew G. Knepley IS cells = patch->cells; 826*5f824522SMatthew G. Knepley IS points = patch->points; 8274bbf5ea8SMatthew G. Knepley PetscSection cellNumbering = patch->cellNumbering; 828*5f824522SMatthew G. Knepley PetscInt Nf = patch->nsubspaces; 829*5f824522SMatthew G. Knepley PetscInt numCells, numPoints; 8304bbf5ea8SMatthew G. Knepley PetscInt numDofs; 8314bbf5ea8SMatthew G. Knepley PetscInt numGlobalDofs; 8324bbf5ea8SMatthew G. Knepley PetscInt totalDofsPerCell = patch->totalDofsPerCell; 8334bbf5ea8SMatthew G. Knepley PetscInt vStart, vEnd, v; 834*5f824522SMatthew G. Knepley const PetscInt *cellsArray, *pointsArray; 8354bbf5ea8SMatthew G. Knepley PetscInt *newCellsArray = NULL; 8364bbf5ea8SMatthew G. Knepley PetscInt *dofsArray = NULL; 837*5f824522SMatthew G. Knepley PetscInt *offsArray = NULL; 8384bbf5ea8SMatthew G. Knepley PetscInt *asmArray = NULL; 8394bbf5ea8SMatthew G. Knepley PetscInt *globalDofsArray = NULL; 8404bbf5ea8SMatthew G. Knepley PetscInt globalIndex = 0; 8414bbf5ea8SMatthew G. Knepley PetscInt key = 0; 8424bbf5ea8SMatthew G. Knepley PetscInt asmKey = 0; 8434bbf5ea8SMatthew G. Knepley PetscHashI ht; 844*5f824522SMatthew G. Knepley PetscInt pStart, pEnd, p; 8454bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 8464bbf5ea8SMatthew G. Knepley 8474bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 8484bbf5ea8SMatthew G. Knepley /* dofcounts section is cellcounts section * dofPerCell */ 8494bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr); 850*5f824522SMatthew G. Knepley ierr = PetscSectionGetStorageSize(patch->pointCounts, &numPoints);CHKERRQ(ierr); 8514bbf5ea8SMatthew G. Knepley numDofs = numCells * totalDofsPerCell; 8524bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(numDofs, &dofsArray);CHKERRQ(ierr); 853*5f824522SMatthew G. Knepley ierr = PetscMalloc1(numPoints*Nf, &offsArray);CHKERRQ(ierr); 8544bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(numDofs, &asmArray);CHKERRQ(ierr); 8554bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(numCells, &newCellsArray);CHKERRQ(ierr); 8564bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetChart(cellCounts, &vStart, &vEnd);CHKERRQ(ierr); 8574bbf5ea8SMatthew G. Knepley ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCounts);CHKERRQ(ierr); 8584bbf5ea8SMatthew G. Knepley gtolCounts = patch->gtolCounts; 8594bbf5ea8SMatthew G. Knepley ierr = PetscSectionSetChart(gtolCounts, vStart, vEnd);CHKERRQ(ierr); 860*5f824522SMatthew G. Knepley ierr = PetscObjectSetName((PetscObject) patch->gtolCounts, "Patch Global Index Section");CHKERRQ(ierr); 8614bbf5ea8SMatthew G. Knepley 8624bbf5ea8SMatthew G. Knepley ierr = ISGetIndices(cells, &cellsArray);CHKERRQ(ierr); 863*5f824522SMatthew G. Knepley ierr = ISGetIndices(points, &pointsArray);CHKERRQ(ierr); 8644bbf5ea8SMatthew G. Knepley PetscHashICreate(ht); 8654bbf5ea8SMatthew G. Knepley for (v = vStart; v < vEnd; ++v) { 8664bbf5ea8SMatthew G. Knepley PetscInt localIndex = 0; 8674bbf5ea8SMatthew G. Knepley PetscInt dof, off, i, j, k, l; 8684bbf5ea8SMatthew G. Knepley 8694bbf5ea8SMatthew G. Knepley PetscHashIClear(ht); 8704bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr); 8714bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr); 8724bbf5ea8SMatthew G. Knepley if (dof <= 0) continue; 8734bbf5ea8SMatthew G. Knepley 8744bbf5ea8SMatthew G. Knepley for (k = 0; k < patch->nsubspaces; ++k) { 8754bbf5ea8SMatthew G. Knepley const PetscInt *cellNodeMap = patch->cellNodeMap[k]; 8764bbf5ea8SMatthew G. Knepley PetscInt nodesPerCell = patch->nodesPerCell[k]; 8774bbf5ea8SMatthew G. Knepley PetscInt subspaceOffset = patch->subspaceOffsets[k]; 8784bbf5ea8SMatthew G. Knepley PetscInt bs = patch->bs[k]; 8794bbf5ea8SMatthew G. Knepley 8804bbf5ea8SMatthew G. Knepley for (i = off; i < off + dof; ++i) { 8814bbf5ea8SMatthew G. Knepley /* Walk over the cells in this patch. */ 8824bbf5ea8SMatthew G. Knepley const PetscInt c = cellsArray[i]; 883*5f824522SMatthew G. Knepley PetscInt cell = c; 8844bbf5ea8SMatthew G. Knepley 885*5f824522SMatthew G. Knepley /* TODO Change this to an IS */ 886*5f824522SMatthew G. Knepley if (cellNumbering) { 8874bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetDof(cellNumbering, c, &cell);CHKERRQ(ierr); 8884bbf5ea8SMatthew G. Knepley if (cell <= 0) SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_OUTOFRANGE, "Cell %D doesn't appear in cell numbering map", c); 8894bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr); 890*5f824522SMatthew G. Knepley } 8914bbf5ea8SMatthew G. Knepley newCellsArray[i] = cell; 8924bbf5ea8SMatthew G. Knepley for (j = 0; j < nodesPerCell; ++j) { 8934bbf5ea8SMatthew G. Knepley /* For each global dof, map it into contiguous local storage. */ 8944bbf5ea8SMatthew G. Knepley const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + subspaceOffset; 8954bbf5ea8SMatthew G. Knepley /* finally, loop over block size */ 8964bbf5ea8SMatthew G. Knepley for (l = 0; l < bs; ++l) { 8974bbf5ea8SMatthew G. Knepley PetscInt localDof; 8984bbf5ea8SMatthew G. Knepley 8994bbf5ea8SMatthew G. Knepley PetscHashIMap(ht, globalDof + l, localDof); 9004bbf5ea8SMatthew G. Knepley if (localDof == -1) { 9014bbf5ea8SMatthew G. Knepley localDof = localIndex++; 9024bbf5ea8SMatthew G. Knepley PetscHashIAdd(ht, globalDof + l, localDof); 9034bbf5ea8SMatthew G. Knepley } 9044bbf5ea8SMatthew G. Knepley if (globalIndex >= numDofs) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs); 9054bbf5ea8SMatthew G. Knepley /* And store. */ 9064bbf5ea8SMatthew G. Knepley dofsArray[globalIndex++] = localDof; 9074bbf5ea8SMatthew G. Knepley } 9084bbf5ea8SMatthew G. Knepley } 9094bbf5ea8SMatthew G. Knepley } 9104bbf5ea8SMatthew G. Knepley } 9114bbf5ea8SMatthew G. Knepley /* How many local dofs in this patch? */ 9124bbf5ea8SMatthew G. Knepley PetscHashISize(ht, dof); 9134bbf5ea8SMatthew G. Knepley ierr = PetscSectionSetDof(gtolCounts, v, dof);CHKERRQ(ierr); 9144bbf5ea8SMatthew G. Knepley } 9154bbf5ea8SMatthew G. Knepley if (globalIndex != numDofs) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Expected number of dofs (%d) doesn't match found number (%d)", numDofs, globalIndex); 9164bbf5ea8SMatthew G. Knepley ierr = PetscSectionSetUp(gtolCounts);CHKERRQ(ierr); 9174bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetStorageSize(gtolCounts, &numGlobalDofs);CHKERRQ(ierr); 9184bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(numGlobalDofs, &globalDofsArray);CHKERRQ(ierr); 9194bbf5ea8SMatthew G. Knepley 9204bbf5ea8SMatthew G. Knepley /* Now populate the global to local map. This could be merged into the above loop if we were willing to deal with reallocs. */ 9214bbf5ea8SMatthew G. Knepley for (v = vStart; v < vEnd; ++v) { 9224bbf5ea8SMatthew G. Knepley PetscHashIIter hi; 923*5f824522SMatthew G. Knepley PetscInt dof, off, Np, ooff, i, j, k, l; 9244bbf5ea8SMatthew G. Knepley 9254bbf5ea8SMatthew G. Knepley PetscHashIClear(ht); 9264bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr); 9274bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr); 928*5f824522SMatthew G. Knepley ierr = PetscSectionGetDof(pointCounts, v, &Np);CHKERRQ(ierr); 929*5f824522SMatthew G. Knepley ierr = PetscSectionGetOffset(pointCounts, v, &ooff);CHKERRQ(ierr); 9304bbf5ea8SMatthew G. Knepley if (dof <= 0) continue; 9314bbf5ea8SMatthew G. Knepley 9324bbf5ea8SMatthew G. Knepley for (k = 0; k < patch->nsubspaces; ++k) { 9334bbf5ea8SMatthew G. Knepley const PetscInt *cellNodeMap = patch->cellNodeMap[k]; 9344bbf5ea8SMatthew G. Knepley PetscInt nodesPerCell = patch->nodesPerCell[k]; 9354bbf5ea8SMatthew G. Knepley PetscInt subspaceOffset = patch->subspaceOffsets[k]; 9364bbf5ea8SMatthew G. Knepley PetscInt bs = patch->bs[k]; 9374bbf5ea8SMatthew G. Knepley 9384bbf5ea8SMatthew G. Knepley for (i = off; i < off + dof; ++i) { 9394bbf5ea8SMatthew G. Knepley /* Reconstruct mapping of global-to-local on this patch. */ 9404bbf5ea8SMatthew G. Knepley const PetscInt c = cellsArray[i]; 941*5f824522SMatthew G. Knepley PetscInt cell = c; 9424bbf5ea8SMatthew G. Knepley 943*5f824522SMatthew G. Knepley if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);} 9444bbf5ea8SMatthew G. Knepley for (j = 0; j < nodesPerCell; ++j) { 9454bbf5ea8SMatthew G. Knepley for (l = 0; l < bs; ++l) { 946*5f824522SMatthew G. Knepley const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset; 9474bbf5ea8SMatthew G. Knepley const PetscInt localDof = dofsArray[key]; 9484bbf5ea8SMatthew G. Knepley 9494bbf5ea8SMatthew G. Knepley key += 1; 9504bbf5ea8SMatthew G. Knepley PetscHashIAdd(ht, globalDof, localDof); 9514bbf5ea8SMatthew G. Knepley } 9524bbf5ea8SMatthew G. Knepley } 9534bbf5ea8SMatthew G. Knepley } 9544bbf5ea8SMatthew G. Knepley if (dof > 0) { 9554bbf5ea8SMatthew G. Knepley /* Shove it in the output data structure. */ 9564bbf5ea8SMatthew G. Knepley PetscInt goff; 9574bbf5ea8SMatthew G. Knepley 9584bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetOffset(gtolCounts, v, &goff);CHKERRQ(ierr); 9594bbf5ea8SMatthew G. Knepley PetscHashIIterBegin(ht, hi); 9604bbf5ea8SMatthew G. Knepley while (!PetscHashIIterAtEnd(ht, hi)) { 9614bbf5ea8SMatthew G. Knepley PetscInt globalDof, localDof; 9624bbf5ea8SMatthew G. Knepley 9634bbf5ea8SMatthew G. Knepley PetscHashIIterGetKeyVal(ht, hi, globalDof, localDof); 9644bbf5ea8SMatthew G. Knepley if (globalDof >= 0) globalDofsArray[goff + localDof] = globalDof; 9654bbf5ea8SMatthew G. Knepley PetscHashIIterNext(ht, hi); 9664bbf5ea8SMatthew G. Knepley } 9674bbf5ea8SMatthew G. Knepley } 968*5f824522SMatthew G. Knepley 969*5f824522SMatthew G. Knepley for (p = 0; p < Np; ++p) { 970*5f824522SMatthew G. Knepley const PetscInt point = pointsArray[ooff + p]; 971*5f824522SMatthew G. Knepley PetscInt globalDof, localDof; 972*5f824522SMatthew G. Knepley 973*5f824522SMatthew G. Knepley ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, point, NULL, &globalDof);CHKERRQ(ierr); 974*5f824522SMatthew G. Knepley PetscHashIMap(ht, globalDof, localDof); 975*5f824522SMatthew G. Knepley offsArray[(ooff + p)*Nf + k] = localDof; 976*5f824522SMatthew G. Knepley } 9774bbf5ea8SMatthew G. Knepley } 9784bbf5ea8SMatthew G. Knepley 9794bbf5ea8SMatthew G. Knepley /* At this point, we have a hash table ht built that maps globalDof -> localDof. 9804bbf5ea8SMatthew G. Knepley We need to create the dof table laid out cellwise first, then by subspace, 9814bbf5ea8SMatthew G. Knepley as the assembler assembles cell-wise and we need to stuff the different 9824bbf5ea8SMatthew G. Knepley contributions of the different function spaces to the right places. So we loop 9834bbf5ea8SMatthew G. Knepley over cells, then over subspaces. */ 9844bbf5ea8SMatthew G. Knepley if (patch->nsubspaces > 1) { /* for nsubspaces = 1, data we need is already in dofsArray */ 9854bbf5ea8SMatthew G. Knepley for (i = off; i < off + dof; ++i) { 9864bbf5ea8SMatthew G. Knepley const PetscInt c = cellsArray[i]; 987*5f824522SMatthew G. Knepley PetscInt cell = c; 9884bbf5ea8SMatthew G. Knepley 989*5f824522SMatthew G. Knepley if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);} 9904bbf5ea8SMatthew G. Knepley for (k = 0; k < patch->nsubspaces; ++k) { 9914bbf5ea8SMatthew G. Knepley const PetscInt *cellNodeMap = patch->cellNodeMap[k]; 9924bbf5ea8SMatthew G. Knepley PetscInt nodesPerCell = patch->nodesPerCell[k]; 9934bbf5ea8SMatthew G. Knepley PetscInt subspaceOffset = patch->subspaceOffsets[k]; 9944bbf5ea8SMatthew G. Knepley PetscInt bs = patch->bs[k]; 9954bbf5ea8SMatthew G. Knepley 9964bbf5ea8SMatthew G. Knepley for (j = 0; j < nodesPerCell; ++j) { 9974bbf5ea8SMatthew G. Knepley for (l = 0; l < bs; ++l) { 998*5f824522SMatthew G. Knepley const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset; 9994bbf5ea8SMatthew G. Knepley PetscInt localDof; 10004bbf5ea8SMatthew G. Knepley 10014bbf5ea8SMatthew G. Knepley PetscHashIMap(ht, globalDof, localDof); 10024bbf5ea8SMatthew G. Knepley asmArray[asmKey++] = localDof; 10034bbf5ea8SMatthew G. Knepley } 10044bbf5ea8SMatthew G. Knepley } 10054bbf5ea8SMatthew G. Knepley } 10064bbf5ea8SMatthew G. Knepley } 10074bbf5ea8SMatthew G. Knepley } 10084bbf5ea8SMatthew G. Knepley } 10094bbf5ea8SMatthew G. Knepley if (1 == patch->nsubspaces) {ierr = PetscMemcpy(asmArray, dofsArray, numDofs * sizeof(PetscInt));CHKERRQ(ierr);} 10104bbf5ea8SMatthew G. Knepley 10114bbf5ea8SMatthew G. Knepley PetscHashIDestroy(ht); 10124bbf5ea8SMatthew G. Knepley ierr = ISRestoreIndices(cells, &cellsArray);CHKERRQ(ierr); 1013*5f824522SMatthew G. Knepley ierr = ISRestoreIndices(points, &pointsArray);CHKERRQ(ierr); 10144bbf5ea8SMatthew G. Knepley ierr = PetscFree(dofsArray);CHKERRQ(ierr); 1015*5f824522SMatthew G. Knepley /* Create placeholder section for map from points to patch dofs */ 1016*5f824522SMatthew G. Knepley ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->patchSection);CHKERRQ(ierr); 1017*5f824522SMatthew G. Knepley ierr = PetscSectionSetNumFields(patch->patchSection, patch->nsubspaces);CHKERRQ(ierr); 1018*5f824522SMatthew G. Knepley ierr = PetscSectionGetChart(patch->dofSection[0], &pStart, &pEnd);CHKERRQ(ierr); 1019*5f824522SMatthew G. Knepley ierr = PetscSectionSetChart(patch->patchSection, pStart, pEnd);CHKERRQ(ierr); 1020*5f824522SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 1021*5f824522SMatthew G. Knepley PetscInt dof, fdof, f; 1022*5f824522SMatthew G. Knepley 1023*5f824522SMatthew G. Knepley ierr = PetscSectionGetDof(patch->dofSection[0], p, &dof);CHKERRQ(ierr); 1024*5f824522SMatthew G. Knepley ierr = PetscSectionSetDof(patch->patchSection, p, dof);CHKERRQ(ierr); 1025*5f824522SMatthew G. Knepley for (f = 0; f < patch->nsubspaces; ++f) { 1026*5f824522SMatthew G. Knepley ierr = PetscSectionGetFieldDof(patch->dofSection[0], p, f, &fdof);CHKERRQ(ierr); 1027*5f824522SMatthew G. Knepley ierr = PetscSectionSetFieldDof(patch->patchSection, p, f, fdof);CHKERRQ(ierr); 1028*5f824522SMatthew G. Knepley } 1029*5f824522SMatthew G. Knepley } 1030*5f824522SMatthew G. Knepley ierr = PetscSectionSetUp(patch->patchSection);CHKERRQ(ierr); 1031*5f824522SMatthew G. Knepley ierr = PetscSectionSetUseFieldOffsets(patch->patchSection, PETSC_TRUE);CHKERRQ(ierr); 10324bbf5ea8SMatthew G. Knepley /* Replace cell indices with firedrake-numbered ones. */ 10334bbf5ea8SMatthew G. Knepley ierr = ISGeneralSetIndices(cells, numCells, (const PetscInt *) newCellsArray, PETSC_OWN_POINTER);CHKERRQ(ierr); 10344bbf5ea8SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofs, globalDofsArray, PETSC_OWN_POINTER, &patch->gtol);CHKERRQ(ierr); 1035*5f824522SMatthew G. Knepley ierr = PetscObjectSetName((PetscObject) patch->gtol, "Global Indices");CHKERRQ(ierr); 1036*5f824522SMatthew G. Knepley ierr = PetscSectionViewFromOptions(patch->gtolCounts, (PetscObject) pc, "-pc_patch_g2l_view");CHKERRQ(ierr); 1037*5f824522SMatthew G. Knepley ierr = ISViewFromOptions(patch->gtol, (PetscObject) pc, "-pc_patch_g2l_view");CHKERRQ(ierr); 10384bbf5ea8SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArray, PETSC_OWN_POINTER, &patch->dofs);CHKERRQ(ierr); 1039*5f824522SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArray, PETSC_OWN_POINTER, &patch->offs);CHKERRQ(ierr); 10404bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 10414bbf5ea8SMatthew G. Knepley } 10424bbf5ea8SMatthew G. Knepley 10434bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatchBCs(PC pc) 10444bbf5ea8SMatthew G. Knepley { 10454bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 10464bbf5ea8SMatthew G. Knepley const PetscInt *bcNodes = NULL; 10474bbf5ea8SMatthew G. Knepley PetscSection gtolCounts = patch->gtolCounts; 10484bbf5ea8SMatthew G. Knepley IS gtol = patch->gtol; 10494bbf5ea8SMatthew G. Knepley DM dm; 10504bbf5ea8SMatthew G. Knepley PetscInt numBcs; 10514bbf5ea8SMatthew G. Knepley PetscSection bcCounts; 10524bbf5ea8SMatthew G. Knepley PetscHashI globalBcs, localBcs, patchDofs; 10534bbf5ea8SMatthew G. Knepley PetscHashI ownedpts, seenpts, owneddofs, seendofs, artificialbcs; 10544bbf5ea8SMatthew G. Knepley PetscHashIIter hi; 10554bbf5ea8SMatthew G. Knepley PetscInt *bcsArray = NULL; 10564bbf5ea8SMatthew G. Knepley PetscInt *multBcsArray = NULL; 10574bbf5ea8SMatthew G. Knepley const PetscInt *gtolArray; 10584bbf5ea8SMatthew G. Knepley PetscInt vStart, vEnd, v, i; 10594bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 10604bbf5ea8SMatthew G. Knepley 10614bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 10624bbf5ea8SMatthew G. Knepley ierr = PCGetDM(pc, &dm);CHKERRQ(ierr); 10634bbf5ea8SMatthew G. Knepley PetscHashICreate(globalBcs); 10644bbf5ea8SMatthew G. Knepley ierr = ISGetIndices(patch->ghostBcNodes, &bcNodes);CHKERRQ(ierr); 10654bbf5ea8SMatthew G. Knepley ierr = ISGetSize(patch->ghostBcNodes, &numBcs);CHKERRQ(ierr); 10664bbf5ea8SMatthew G. Knepley for (i = 0; i < numBcs; ++i) { 10674bbf5ea8SMatthew G. Knepley /* these are already in concatenated numbering */ 10684bbf5ea8SMatthew G. Knepley PetscHashIAdd(globalBcs, bcNodes[i], 0); 10694bbf5ea8SMatthew G. Knepley } 10704bbf5ea8SMatthew G. Knepley ierr = ISRestoreIndices(patch->ghostBcNodes, &bcNodes);CHKERRQ(ierr); 10714bbf5ea8SMatthew G. Knepley PetscHashICreate(patchDofs); 10724bbf5ea8SMatthew G. Knepley PetscHashICreate(localBcs); 10734bbf5ea8SMatthew G. Knepley PetscHashICreate(ownedpts); 10744bbf5ea8SMatthew G. Knepley PetscHashICreate(seenpts); 10754bbf5ea8SMatthew G. Knepley PetscHashICreate(owneddofs); 10764bbf5ea8SMatthew G. Knepley PetscHashICreate(seendofs); 10774bbf5ea8SMatthew G. Knepley PetscHashICreate(artificialbcs); 10784bbf5ea8SMatthew G. Knepley 10794bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetChart(patch->cellCounts, &vStart, &vEnd);CHKERRQ(ierr); 10804bbf5ea8SMatthew G. Knepley ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->bcCounts);CHKERRQ(ierr); 10814bbf5ea8SMatthew G. Knepley bcCounts = patch->bcCounts; 10824bbf5ea8SMatthew G. Knepley ierr = PetscSectionSetChart(bcCounts, vStart, vEnd);CHKERRQ(ierr); 10834bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(vEnd - vStart, &patch->bcs);CHKERRQ(ierr); 10844bbf5ea8SMatthew G. Knepley if (patch->multiplicative) {ierr = PetscMalloc1(vEnd - vStart, &patch->multBcs);CHKERRQ(ierr);} 10854bbf5ea8SMatthew G. Knepley 10864bbf5ea8SMatthew G. Knepley ierr = ISGetIndices(gtol, >olArray);CHKERRQ(ierr); 10874bbf5ea8SMatthew G. Knepley for (v = vStart; v < vEnd; ++v) { 10884bbf5ea8SMatthew G. Knepley PetscInt bcIndex = 0; 10894bbf5ea8SMatthew G. Knepley PetscInt multBcIndex = 0; 10904bbf5ea8SMatthew G. Knepley PetscInt numBcs, dof, off; 10914bbf5ea8SMatthew G. Knepley 10924bbf5ea8SMatthew G. Knepley PetscHashIClear(patchDofs); 10934bbf5ea8SMatthew G. Knepley PetscHashIClear(localBcs); 10944bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetDof(gtolCounts, v, &dof);CHKERRQ(ierr); 10954bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetOffset(gtolCounts, v, &off);CHKERRQ(ierr); 10964bbf5ea8SMatthew G. Knepley 10974bbf5ea8SMatthew G. Knepley if (dof <= 0) { 10984bbf5ea8SMatthew G. Knepley patch->bcs[v-vStart] = NULL; 10994bbf5ea8SMatthew G. Knepley if (patch->multiplicative) patch->multBcs[v-vStart] = NULL; 11004bbf5ea8SMatthew G. Knepley continue; 11014bbf5ea8SMatthew G. Knepley } 11024bbf5ea8SMatthew G. Knepley 11034bbf5ea8SMatthew G. Knepley for (i = off; i < off + dof; ++i) { 11044bbf5ea8SMatthew G. Knepley const PetscInt globalDof = gtolArray[i]; 11054bbf5ea8SMatthew G. Knepley const PetscInt localDof = i-off; 11064bbf5ea8SMatthew G. Knepley PetscBool flg; 11074bbf5ea8SMatthew G. Knepley 11084bbf5ea8SMatthew G. Knepley PetscHashIAdd(patchDofs, globalDof, localDof); 11094bbf5ea8SMatthew G. Knepley PetscHashIHasKey(globalBcs, globalDof, flg); 11104bbf5ea8SMatthew G. Knepley if (flg) {PetscHashIAdd(localBcs, localDof, 0);} 11114bbf5ea8SMatthew G. Knepley } 11124bbf5ea8SMatthew G. Knepley 11134bbf5ea8SMatthew G. Knepley /* If we're doing multiplicative, make the BC data structures now corresponding solely to actual globally imposed Dirichlet BCs */ 11144bbf5ea8SMatthew G. Knepley if (patch->multiplicative) { 11154bbf5ea8SMatthew G. Knepley PetscHashISize(localBcs, numBcs); 11164bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(numBcs, &multBcsArray);CHKERRQ(ierr); 11174bbf5ea8SMatthew G. Knepley PetscHashIGetKeys(localBcs, &multBcIndex, multBcsArray); 11184bbf5ea8SMatthew G. Knepley ierr = PetscSortInt(numBcs, multBcsArray);CHKERRQ(ierr); 11194bbf5ea8SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, numBcs, multBcsArray, PETSC_OWN_POINTER, &(patch->multBcs[v-vStart]));CHKERRQ(ierr); 11204bbf5ea8SMatthew G. Knepley } 11214bbf5ea8SMatthew G. Knepley 11224bbf5ea8SMatthew G. Knepley /* Now figure out the artificial BCs: the set difference of {dofs on entities I see on the patch}\{dofs I am responsible for updating} */ 11234bbf5ea8SMatthew G. Knepley ierr = patch->patchconstructop((void*) patch, dm, v, ownedpts);CHKERRQ(ierr); 1124*5f824522SMatthew G. Knepley ierr = PCPatchCompleteCellPatch(pc, ownedpts, seenpts);CHKERRQ(ierr); 1125*5f824522SMatthew G. Knepley ierr = PCPatchGetPointDofs(pc, ownedpts, owneddofs, v, patch->exclude_subspace);CHKERRQ(ierr); 1126*5f824522SMatthew G. Knepley ierr = PCPatchGetPointDofs(pc, seenpts, seendofs, v, -1);CHKERRQ(ierr); 11274bbf5ea8SMatthew G. Knepley ierr = PCPatchComputeSetDifference_Private(owneddofs, seendofs, artificialbcs);CHKERRQ(ierr); 11284bbf5ea8SMatthew G. Knepley 1129*5f824522SMatthew G. Knepley if (patch->viewPatches) { 11304bbf5ea8SMatthew G. Knepley PetscHashI globalbcdofs; 11314bbf5ea8SMatthew G. Knepley MPI_Comm comm; 11324bbf5ea8SMatthew G. Knepley 11334bbf5ea8SMatthew G. Knepley PetscHashICreate(globalbcdofs); 11344bbf5ea8SMatthew G. Knepley 11354bbf5ea8SMatthew G. Knepley ierr = PetscObjectGetComm((PetscObject) pc, &comm);CHKERRQ(ierr); 11364bbf5ea8SMatthew G. Knepley ierr = PetscSynchronizedPrintf(comm, "Patch %d: owned dofs:\n", v);CHKERRQ(ierr); 11374bbf5ea8SMatthew G. Knepley PetscHashIIterBegin(owneddofs, hi); 11384bbf5ea8SMatthew G. Knepley while (!PetscHashIIterAtEnd(owneddofs, hi)) { 11394bbf5ea8SMatthew G. Knepley PetscInt globalDof; 11404bbf5ea8SMatthew G. Knepley 11414bbf5ea8SMatthew G. Knepley PetscHashIIterGetKey(owneddofs, hi, globalDof); 11424bbf5ea8SMatthew G. Knepley PetscHashIIterNext(owneddofs, hi); 11434bbf5ea8SMatthew G. Knepley ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof);CHKERRQ(ierr); 11444bbf5ea8SMatthew G. Knepley } 11454bbf5ea8SMatthew G. Knepley ierr = PetscSynchronizedPrintf(comm, "\n");CHKERRQ(ierr); 11464bbf5ea8SMatthew G. Knepley ierr = PetscSynchronizedPrintf(comm, "Patch %d: seen dofs:\n", v);CHKERRQ(ierr); 11474bbf5ea8SMatthew G. Knepley PetscHashIIterBegin(seendofs, hi); 11484bbf5ea8SMatthew G. Knepley while (!PetscHashIIterAtEnd(seendofs, hi)) { 11494bbf5ea8SMatthew G. Knepley PetscInt globalDof; 11504bbf5ea8SMatthew G. Knepley PetscBool flg; 11514bbf5ea8SMatthew G. Knepley 11524bbf5ea8SMatthew G. Knepley PetscHashIIterGetKey(seendofs, hi, globalDof); 11534bbf5ea8SMatthew G. Knepley PetscHashIIterNext(seendofs, hi); 11544bbf5ea8SMatthew G. Knepley ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof);CHKERRQ(ierr); 11554bbf5ea8SMatthew G. Knepley PetscHashIHasKey(globalBcs, globalDof, flg); 11564bbf5ea8SMatthew G. Knepley if (flg) {PetscHashIAdd(globalbcdofs, globalDof, 0);} 11574bbf5ea8SMatthew G. Knepley } 11584bbf5ea8SMatthew G. Knepley ierr = PetscSynchronizedPrintf(comm, "\n");CHKERRQ(ierr); 11594bbf5ea8SMatthew G. Knepley ierr = PetscSynchronizedPrintf(comm, "Patch %d: global BCs:\n", v);CHKERRQ(ierr); 11604bbf5ea8SMatthew G. Knepley PetscHashISize(globalbcdofs, numBcs); 11614bbf5ea8SMatthew G. Knepley if (numBcs > 0) { 11624bbf5ea8SMatthew G. Knepley PetscHashIIterBegin(globalbcdofs, hi); 11634bbf5ea8SMatthew G. Knepley while (!PetscHashIIterAtEnd(globalbcdofs, hi)) { 11644bbf5ea8SMatthew G. Knepley PetscInt globalDof; 11654bbf5ea8SMatthew G. Knepley PetscHashIIterGetKey(globalbcdofs, hi, globalDof); 11664bbf5ea8SMatthew G. Knepley PetscHashIIterNext(globalbcdofs, hi); 11674bbf5ea8SMatthew G. Knepley ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof);CHKERRQ(ierr); 11684bbf5ea8SMatthew G. Knepley } 11694bbf5ea8SMatthew G. Knepley } 11704bbf5ea8SMatthew G. Knepley ierr = PetscSynchronizedPrintf(comm, "\n");CHKERRQ(ierr); 11714bbf5ea8SMatthew G. Knepley ierr = PetscSynchronizedPrintf(comm, "Patch %d: artificial BCs:\n", v);CHKERRQ(ierr); 11724bbf5ea8SMatthew G. Knepley PetscHashISize(artificialbcs, numBcs); 11734bbf5ea8SMatthew G. Knepley if (numBcs > 0) { 11744bbf5ea8SMatthew G. Knepley PetscHashIIterBegin(artificialbcs, hi); 11754bbf5ea8SMatthew G. Knepley while (!PetscHashIIterAtEnd(artificialbcs, hi)) { 11764bbf5ea8SMatthew G. Knepley PetscInt globalDof; 11774bbf5ea8SMatthew G. Knepley PetscHashIIterGetKey(artificialbcs, hi, globalDof); 11784bbf5ea8SMatthew G. Knepley PetscHashIIterNext(artificialbcs, hi); 11794bbf5ea8SMatthew G. Knepley ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof);CHKERRQ(ierr); 11804bbf5ea8SMatthew G. Knepley } 11814bbf5ea8SMatthew G. Knepley } 11824bbf5ea8SMatthew G. Knepley ierr = PetscSynchronizedPrintf(comm, "\n\n");CHKERRQ(ierr); 11834bbf5ea8SMatthew G. Knepley ierr = PetscSynchronizedFlush(comm, PETSC_STDOUT);CHKERRQ(ierr); 11844bbf5ea8SMatthew G. Knepley PetscHashIDestroy(globalbcdofs); 11854bbf5ea8SMatthew G. Knepley } 11864bbf5ea8SMatthew G. Knepley 11874bbf5ea8SMatthew G. Knepley PetscHashISize(artificialbcs, numBcs); 11884bbf5ea8SMatthew G. Knepley if (numBcs > 0) { 11894bbf5ea8SMatthew G. Knepley PetscHashIIterBegin(artificialbcs, hi); 11904bbf5ea8SMatthew G. Knepley while (!PetscHashIIterAtEnd(artificialbcs, hi)) { 11914bbf5ea8SMatthew G. Knepley PetscInt globalDof, localDof; 11924bbf5ea8SMatthew G. Knepley PetscHashIIterGetKey(artificialbcs, hi, globalDof); 11934bbf5ea8SMatthew G. Knepley PetscHashIIterNext(artificialbcs, hi); 11944bbf5ea8SMatthew G. Knepley PetscHashIMap(patchDofs, globalDof, localDof); 1195*5f824522SMatthew G. Knepley if (localDof == -1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Patch %d Didn't find dof %d in patch\n", v, globalDof); 11964bbf5ea8SMatthew G. Knepley PetscHashIAdd(localBcs, localDof, 0); 11974bbf5ea8SMatthew G. Knepley } 11984bbf5ea8SMatthew G. Knepley } 11994bbf5ea8SMatthew G. Knepley 12004bbf5ea8SMatthew G. Knepley /* OK, now we have a hash table with all the bcs indicated by the artificial and global bcs */ 12014bbf5ea8SMatthew G. Knepley PetscHashISize(localBcs, numBcs); 12024bbf5ea8SMatthew G. Knepley ierr = PetscSectionSetDof(bcCounts, v, numBcs);CHKERRQ(ierr); 12034bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(numBcs, &bcsArray);CHKERRQ(ierr); 12044bbf5ea8SMatthew G. Knepley PetscHashIGetKeys(localBcs, &bcIndex, bcsArray); 12054bbf5ea8SMatthew G. Knepley ierr = PetscSortInt(numBcs, bcsArray);CHKERRQ(ierr); 12064bbf5ea8SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, numBcs, bcsArray, PETSC_OWN_POINTER, &(patch->bcs[v - vStart]));CHKERRQ(ierr); 12074bbf5ea8SMatthew G. Knepley } 12084bbf5ea8SMatthew G. Knepley ierr = ISRestoreIndices(gtol, >olArray);CHKERRQ(ierr); 12094bbf5ea8SMatthew G. Knepley PetscHashIDestroy(artificialbcs); 12104bbf5ea8SMatthew G. Knepley PetscHashIDestroy(seendofs); 12114bbf5ea8SMatthew G. Knepley PetscHashIDestroy(owneddofs); 12124bbf5ea8SMatthew G. Knepley PetscHashIDestroy(seenpts); 12134bbf5ea8SMatthew G. Knepley PetscHashIDestroy(ownedpts); 12144bbf5ea8SMatthew G. Knepley PetscHashIDestroy(localBcs); 12154bbf5ea8SMatthew G. Knepley PetscHashIDestroy(patchDofs); 12164bbf5ea8SMatthew G. Knepley PetscHashIDestroy(globalBcs); 12174bbf5ea8SMatthew G. Knepley ierr = ISDestroy(&patch->ghostBcNodes);CHKERRQ(ierr); 12184bbf5ea8SMatthew G. Knepley ierr = PetscSectionSetUp(bcCounts);CHKERRQ(ierr); 12194bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 12204bbf5ea8SMatthew G. Knepley } 12214bbf5ea8SMatthew G. Knepley 12224bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchZeroFillMatrix_Private(Mat mat, const PetscInt ncell, const PetscInt ndof, const PetscInt *dof) 12234bbf5ea8SMatthew G. Knepley { 12244bbf5ea8SMatthew G. Knepley const PetscScalar *values = NULL; 12254bbf5ea8SMatthew G. Knepley PetscInt rows, c, i; 12264bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 12274bbf5ea8SMatthew G. Knepley 12284bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 12294bbf5ea8SMatthew G. Knepley ierr = PetscCalloc1(ndof*ndof, &values);CHKERRQ(ierr); 12304bbf5ea8SMatthew G. Knepley for (c = 0; c < ncell; ++c) { 12314bbf5ea8SMatthew G. Knepley const PetscInt *idx = &dof[ndof*c]; 12324bbf5ea8SMatthew G. Knepley ierr = MatSetValues(mat, ndof, idx, ndof, idx, values, INSERT_VALUES);CHKERRQ(ierr); 12334bbf5ea8SMatthew G. Knepley } 12344bbf5ea8SMatthew G. Knepley ierr = MatGetLocalSize(mat, &rows, NULL);CHKERRQ(ierr); 12354bbf5ea8SMatthew G. Knepley for (i = 0; i < rows; ++i) { 12364bbf5ea8SMatthew G. Knepley ierr = MatSetValues(mat, 1, &i, 1, &i, values, INSERT_VALUES);CHKERRQ(ierr); 12374bbf5ea8SMatthew G. Knepley } 12384bbf5ea8SMatthew G. Knepley ierr = MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 12394bbf5ea8SMatthew G. Knepley ierr = MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 12404bbf5ea8SMatthew G. Knepley ierr = PetscFree(values);CHKERRQ(ierr); 12414bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 12424bbf5ea8SMatthew G. Knepley } 12434bbf5ea8SMatthew G. Knepley 12444bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateMatrix_Private(PC pc, PetscInt point, Mat *mat) 12454bbf5ea8SMatthew G. Knepley { 12464bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 12474bbf5ea8SMatthew G. Knepley Vec x, y; 12484bbf5ea8SMatthew G. Knepley PetscBool flg; 12494bbf5ea8SMatthew G. Knepley PetscInt csize, rsize; 12504bbf5ea8SMatthew G. Knepley const char *prefix = NULL; 12514bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 12524bbf5ea8SMatthew G. Knepley 12534bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 12544bbf5ea8SMatthew G. Knepley x = patch->patchX[point]; 12554bbf5ea8SMatthew G. Knepley y = patch->patchY[point]; 12564bbf5ea8SMatthew G. Knepley ierr = VecGetSize(x, &csize);CHKERRQ(ierr); 12574bbf5ea8SMatthew G. Knepley ierr = VecGetSize(y, &rsize);CHKERRQ(ierr); 12584bbf5ea8SMatthew G. Knepley ierr = MatCreate(PETSC_COMM_SELF, mat);CHKERRQ(ierr); 12594bbf5ea8SMatthew G. Knepley ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr); 12604bbf5ea8SMatthew G. Knepley ierr = MatSetOptionsPrefix(*mat, prefix);CHKERRQ(ierr); 1261*5f824522SMatthew G. Knepley ierr = MatAppendOptionsPrefix(*mat, "pc_patch_sub_");CHKERRQ(ierr); 12624bbf5ea8SMatthew G. Knepley if (patch->sub_mat_type) {ierr = MatSetType(*mat, patch->sub_mat_type);CHKERRQ(ierr);} 12634bbf5ea8SMatthew G. Knepley ierr = MatSetSizes(*mat, rsize, csize, rsize, csize);CHKERRQ(ierr); 12644bbf5ea8SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) *mat, MATDENSE, &flg);CHKERRQ(ierr); 12654bbf5ea8SMatthew G. Knepley if (!flg) {ierr = PetscObjectTypeCompare((PetscObject)*mat, MATSEQDENSE, &flg);CHKERRQ(ierr);} 12664bbf5ea8SMatthew G. Knepley /* Sparse patch matrices */ 12674bbf5ea8SMatthew G. Knepley if (!flg) { 12684bbf5ea8SMatthew G. Knepley PetscBT bt; 12694bbf5ea8SMatthew G. Knepley PetscInt *dnnz = NULL; 12704bbf5ea8SMatthew G. Knepley const PetscInt *dofsArray = NULL; 12714bbf5ea8SMatthew G. Knepley PetscInt pStart, pEnd, ncell, offset, c, i, j; 12724bbf5ea8SMatthew G. Knepley 12734bbf5ea8SMatthew G. Knepley ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr); 12744bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr); 12754bbf5ea8SMatthew G. Knepley point += pStart; 12764bbf5ea8SMatthew G. Knepley if (point >= pEnd) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %D not in [%D, %D)\n", point, pStart, pEnd);CHKERRQ(ierr); 12774bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr); 12784bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr); 12794bbf5ea8SMatthew G. Knepley ierr = PetscCalloc1(rsize, &dnnz);CHKERRQ(ierr); 12804bbf5ea8SMatthew G. Knepley ierr = PetscLogEventBegin(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr); 12814bbf5ea8SMatthew G. Knepley /* XXX: This uses N^2 bits to store the sparsity pattern on a 12824bbf5ea8SMatthew G. Knepley * patch. This is probably OK if the patches are not too big, 12834bbf5ea8SMatthew G. Knepley * but could use quite a bit of memory for planes in 3D. 12844bbf5ea8SMatthew G. Knepley * Should we switch based on the value of rsize to a 12854bbf5ea8SMatthew G. Knepley * hash-table (slower, but more memory efficient) approach? */ 12864bbf5ea8SMatthew G. Knepley ierr = PetscBTCreate(rsize*rsize, &bt);CHKERRQ(ierr); 12874bbf5ea8SMatthew G. Knepley for (c = 0; c < ncell; ++c) { 12884bbf5ea8SMatthew G. Knepley const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell; 12894bbf5ea8SMatthew G. Knepley for (i = 0; i < patch->totalDofsPerCell; ++i) { 12904bbf5ea8SMatthew G. Knepley const PetscInt row = idx[i]; 12914bbf5ea8SMatthew G. Knepley for (j = 0; j < patch->totalDofsPerCell; ++j) { 12924bbf5ea8SMatthew G. Knepley const PetscInt col = idx[j]; 12934bbf5ea8SMatthew G. Knepley const PetscInt key = row*rsize + col; 12944bbf5ea8SMatthew G. Knepley if (!PetscBTLookupSet(bt, key)) ++dnnz[row]; 12954bbf5ea8SMatthew G. Knepley } 12964bbf5ea8SMatthew G. Knepley } 12974bbf5ea8SMatthew G. Knepley } 12984bbf5ea8SMatthew G. Knepley ierr = PetscBTDestroy(&bt);CHKERRQ(ierr); 12994bbf5ea8SMatthew G. Knepley ierr = MatXAIJSetPreallocation(*mat, 1, dnnz, NULL, NULL, NULL);CHKERRQ(ierr); 13004bbf5ea8SMatthew G. Knepley ierr = PetscFree(dnnz);CHKERRQ(ierr); 13014bbf5ea8SMatthew G. Knepley ierr = PCPatchZeroFillMatrix_Private(*mat, ncell, patch->totalDofsPerCell, &dofsArray[offset*patch->totalDofsPerCell]);CHKERRQ(ierr); 13024bbf5ea8SMatthew G. Knepley ierr = PetscLogEventEnd(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr); 13034bbf5ea8SMatthew G. Knepley ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr); 13044bbf5ea8SMatthew G. Knepley } 13054bbf5ea8SMatthew G. Knepley ierr = MatSetUp(*mat);CHKERRQ(ierr); 13064bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 13074bbf5ea8SMatthew G. Knepley } 13084bbf5ea8SMatthew G. Knepley 1309*5f824522SMatthew G. Knepley static PetscErrorCode PCPatchComputeOperator_DMPlex_Private(PC pc, PetscInt patchNum, Mat J, PetscInt ncell, const PetscInt cells[], PetscInt n, const PetscInt *l2p, void *ctx) 1310*5f824522SMatthew G. Knepley { 1311*5f824522SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 1312*5f824522SMatthew G. Knepley DM dm; 1313*5f824522SMatthew G. Knepley PetscSection s; 1314*5f824522SMatthew G. Knepley const PetscInt *parray, *oarray; 1315*5f824522SMatthew G. Knepley PetscInt Nf = patch->nsubspaces, Np, poff, p, f; 1316*5f824522SMatthew G. Knepley PetscErrorCode ierr; 1317*5f824522SMatthew G. Knepley 1318*5f824522SMatthew G. Knepley PetscFunctionBegin; 1319*5f824522SMatthew G. Knepley ierr = PCGetDM(pc, &dm);CHKERRQ(ierr); 1320*5f824522SMatthew G. Knepley ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr); 1321*5f824522SMatthew G. Knepley /* Set offset into patch */ 1322*5f824522SMatthew G. Knepley ierr = PetscSectionGetDof(patch->pointCounts, patchNum, &Np);CHKERRQ(ierr); 1323*5f824522SMatthew G. Knepley ierr = PetscSectionGetOffset(patch->pointCounts, patchNum, &poff);CHKERRQ(ierr); 1324*5f824522SMatthew G. Knepley ierr = ISGetIndices(patch->points, &parray);CHKERRQ(ierr); 1325*5f824522SMatthew G. Knepley ierr = ISGetIndices(patch->offs, &oarray);CHKERRQ(ierr); 1326*5f824522SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1327*5f824522SMatthew G. Knepley for (p = 0; p < Np; ++p) { 1328*5f824522SMatthew G. Knepley const PetscInt point = parray[poff+p]; 1329*5f824522SMatthew G. Knepley PetscInt dof; 1330*5f824522SMatthew G. Knepley 1331*5f824522SMatthew G. Knepley ierr = PetscSectionGetFieldDof(patch->patchSection, point, f, &dof);CHKERRQ(ierr); 1332*5f824522SMatthew G. Knepley ierr = PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr); 1333*5f824522SMatthew G. Knepley if (patch->nsubspaces == 1) {ierr = PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);} 1334*5f824522SMatthew G. Knepley else {ierr = PetscSectionSetOffset(patch->patchSection, point, -1);CHKERRQ(ierr);} 1335*5f824522SMatthew G. Knepley } 1336*5f824522SMatthew G. Knepley } 1337*5f824522SMatthew G. Knepley ierr = ISRestoreIndices(patch->points, &parray);CHKERRQ(ierr); 1338*5f824522SMatthew G. Knepley ierr = ISRestoreIndices(patch->offs, &oarray);CHKERRQ(ierr); 1339*5f824522SMatthew G. Knepley if (patch->viewSection) {ierr = ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection);CHKERRQ(ierr);} 1340*5f824522SMatthew G. Knepley /* TODO Shut off MatViewFromOptions() in MatAssemblyEnd() here */ 1341*5f824522SMatthew G. Knepley ierr = DMPlexComputeJacobian_Patch_Internal(pc->dm, patch->patchSection, patch->patchSection, 0, ncell, cells, 0.0, 0.0, NULL, NULL, J, J, ctx);CHKERRQ(ierr); 1342*5f824522SMatthew G. Knepley PetscFunctionReturn(0); 1343*5f824522SMatthew G. Knepley } 1344*5f824522SMatthew G. Knepley 13454bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchComputeOperator_Private(PC pc, Mat mat, Mat multMat, PetscInt point) 13464bbf5ea8SMatthew G. Knepley { 13474bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 13484bbf5ea8SMatthew G. Knepley const PetscInt *dofsArray; 13494bbf5ea8SMatthew G. Knepley const PetscInt *cellsArray; 13504bbf5ea8SMatthew G. Knepley PetscInt ncell, offset, pStart, pEnd; 13514bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 13524bbf5ea8SMatthew G. Knepley 13534bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 13544bbf5ea8SMatthew G. Knepley ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr); 13554bbf5ea8SMatthew G. Knepley if (!patch->usercomputeop) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback\n"); 13564bbf5ea8SMatthew G. Knepley ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr); 13574bbf5ea8SMatthew G. Knepley ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr); 13584bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr); 13594bbf5ea8SMatthew G. Knepley 13604bbf5ea8SMatthew G. Knepley point += pStart; 13614bbf5ea8SMatthew G. Knepley if (point >= pEnd) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %D not in [%D, %D)\n", point, pStart, pEnd);CHKERRQ(ierr); 13624bbf5ea8SMatthew G. Knepley 13634bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr); 13644bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr); 13654bbf5ea8SMatthew G. Knepley if (ncell <= 0) { 13664bbf5ea8SMatthew G. Knepley ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr); 13674bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 13684bbf5ea8SMatthew G. Knepley } 13694bbf5ea8SMatthew G. Knepley PetscStackPush("PCPatch user callback"); 1370*5f824522SMatthew G. Knepley ierr = patch->usercomputeop(pc, point, mat, ncell, cellsArray + offset, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell, patch->usercomputectx);CHKERRQ(ierr); 13714bbf5ea8SMatthew G. Knepley PetscStackPop; 13724bbf5ea8SMatthew G. Knepley ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr); 13734bbf5ea8SMatthew G. Knepley ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr); 1374*5f824522SMatthew G. Knepley /* TODO Can apply these BCs through the patchSection (would need to changes sizes of patchX/Y etc) */ 13754bbf5ea8SMatthew G. Knepley /* Apply boundary conditions. Could also do this through the local_to_patch guy. */ 13764bbf5ea8SMatthew G. Knepley if (patch->multiplicative) { 13774bbf5ea8SMatthew G. Knepley ierr = MatCopy(mat, multMat, SAME_NONZERO_PATTERN);CHKERRQ(ierr); 13784bbf5ea8SMatthew G. Knepley ierr = MatZeroRowsColumnsIS(multMat, patch->multBcs[point-pStart], (PetscScalar) 1.0, NULL, NULL);CHKERRQ(ierr); 13794bbf5ea8SMatthew G. Knepley } 13804bbf5ea8SMatthew G. Knepley ierr = MatZeroRowsColumnsIS(mat, patch->bcs[point-pStart], (PetscScalar) 1.0, NULL, NULL);CHKERRQ(ierr); 1381*5f824522SMatthew G. Knepley if (patch->viewMatrix) {ierr = ObjectView((PetscObject) mat, patch->viewerMatrix, patch->formatMatrix);CHKERRQ(ierr);} 13824bbf5ea8SMatthew G. Knepley ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr); 13834bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 13844bbf5ea8SMatthew G. Knepley } 13854bbf5ea8SMatthew G. Knepley 13864bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatch_ScatterLocal_Private(PC pc, PetscInt p, Vec x, Vec y, InsertMode mode, ScatterMode scat) 13874bbf5ea8SMatthew G. Knepley { 13884bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 13894bbf5ea8SMatthew G. Knepley const PetscScalar *xArray = NULL; 13904bbf5ea8SMatthew G. Knepley PetscScalar *yArray = NULL; 13914bbf5ea8SMatthew G. Knepley const PetscInt *gtolArray = NULL; 13924bbf5ea8SMatthew G. Knepley PetscInt dof, offset, lidx; 13934bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 13944bbf5ea8SMatthew G. Knepley 13954bbf5ea8SMatthew G. Knepley PetscFunctionBeginHot; 13964bbf5ea8SMatthew G. Knepley ierr = PetscLogEventBegin(PC_Patch_Scatter, pc, 0, 0, 0);CHKERRQ(ierr); 13974bbf5ea8SMatthew G. Knepley ierr = VecGetArrayRead(x, &xArray);CHKERRQ(ierr); 13984bbf5ea8SMatthew G. Knepley ierr = VecGetArray(y, &yArray);CHKERRQ(ierr); 13994bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr); 14004bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr); 14014bbf5ea8SMatthew G. Knepley ierr = ISGetIndices(patch->gtol, >olArray);CHKERRQ(ierr); 14024bbf5ea8SMatthew G. Knepley if (mode == INSERT_VALUES && scat != SCATTER_FORWARD) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't insert if not scattering forward\n"); 14034bbf5ea8SMatthew G. Knepley if (mode == ADD_VALUES && scat != SCATTER_REVERSE) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't add if not scattering reverse\n"); 14044bbf5ea8SMatthew G. Knepley for (lidx = 0; lidx < dof; ++lidx) { 14054bbf5ea8SMatthew G. Knepley const PetscInt gidx = gtolArray[offset+lidx]; 14064bbf5ea8SMatthew G. Knepley 14074bbf5ea8SMatthew G. Knepley if (mode == INSERT_VALUES) yArray[lidx] = xArray[gidx]; /* Forward */ 14084bbf5ea8SMatthew G. Knepley else yArray[gidx] += xArray[lidx]; /* Reverse */ 14094bbf5ea8SMatthew G. Knepley } 14104bbf5ea8SMatthew G. Knepley ierr = ISRestoreIndices(patch->gtol, >olArray);CHKERRQ(ierr); 14114bbf5ea8SMatthew G. Knepley ierr = VecRestoreArrayRead(x, &xArray);CHKERRQ(ierr); 14124bbf5ea8SMatthew G. Knepley ierr = VecRestoreArray(y, &yArray);CHKERRQ(ierr); 14134bbf5ea8SMatthew G. Knepley ierr = PetscLogEventEnd(PC_Patch_Scatter, pc, 0, 0, 0);CHKERRQ(ierr); 14144bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 14154bbf5ea8SMatthew G. Knepley } 14164bbf5ea8SMatthew G. Knepley 14174bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUp_PATCH(PC pc) 14184bbf5ea8SMatthew G. Knepley { 14194bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 14204bbf5ea8SMatthew G. Knepley PetscScalar *patchX = NULL; 14214bbf5ea8SMatthew G. Knepley const PetscInt *bcNodes = NULL; 14224bbf5ea8SMatthew G. Knepley PetscInt numBcs, i, j; 14234bbf5ea8SMatthew G. Knepley const char *prefix; 14244bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 14254bbf5ea8SMatthew G. Knepley 14264bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 14274bbf5ea8SMatthew G. Knepley if (!pc->setupcalled) { 14284bbf5ea8SMatthew G. Knepley PetscInt pStart, pEnd, p; 14294bbf5ea8SMatthew G. Knepley PetscInt localSize; 14304bbf5ea8SMatthew G. Knepley 14314bbf5ea8SMatthew G. Knepley ierr = PetscLogEventBegin(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr); 14324bbf5ea8SMatthew G. Knepley 1433*5f824522SMatthew G. Knepley if (!patch->nsubspaces) { 1434*5f824522SMatthew G. Knepley DM dm; 1435*5f824522SMatthew G. Knepley PetscDS prob; 1436*5f824522SMatthew G. Knepley PetscSection s; 1437*5f824522SMatthew G. Knepley PetscInt cStart, cEnd, c, Nf, f, fsize, numGlobalBcs = 0, *globalBcs, *Nb, totNb = 0, **cellDofs; 1438*5f824522SMatthew G. Knepley 1439*5f824522SMatthew G. Knepley ierr = PCGetDM(pc, &dm);CHKERRQ(ierr); 1440*5f824522SMatthew G. Knepley if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONG, "Must set DM for PCPATCH or call PCPatchSetDiscretisationInfo()"); 1441*5f824522SMatthew G. Knepley ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr); 1442*5f824522SMatthew G. Knepley ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr); 1443*5f824522SMatthew G. Knepley ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr); 1444*5f824522SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 1445*5f824522SMatthew G. Knepley PetscInt cdof; 1446*5f824522SMatthew G. Knepley ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr); 1447*5f824522SMatthew G. Knepley numGlobalBcs += cdof; 1448*5f824522SMatthew G. Knepley } 1449*5f824522SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1450*5f824522SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1451*5f824522SMatthew G. Knepley ierr = PetscMalloc3(Nf, &Nb, Nf, &cellDofs, numGlobalBcs, &globalBcs);CHKERRQ(ierr); 1452*5f824522SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1453*5f824522SMatthew G. Knepley PetscFE fe; 1454*5f824522SMatthew G. Knepley PetscDualSpace sp; 1455*5f824522SMatthew G. Knepley PetscSection fs; 1456*5f824522SMatthew G. Knepley PetscInt cdoff = 0; 1457*5f824522SMatthew G. Knepley 1458*5f824522SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 1459*5f824522SMatthew G. Knepley /* ierr = PetscFEGetNumComponents(fe, &Nc[f]);CHKERRQ(ierr); */ 1460*5f824522SMatthew G. Knepley ierr = PetscFEGetDualSpace(fe, &sp);CHKERRQ(ierr); 1461*5f824522SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp, &Nb[f]);CHKERRQ(ierr); 1462*5f824522SMatthew G. Knepley totNb += Nb[f]; 1463*5f824522SMatthew G. Knepley 1464*5f824522SMatthew G. Knepley ierr = PetscSectionGetField(s, f, &fs);CHKERRQ(ierr); 1465*5f824522SMatthew G. Knepley ierr = PetscSectionGetStorageSize(fs, &fsize);CHKERRQ(ierr); 1466*5f824522SMatthew G. Knepley ierr = PetscMalloc1((cEnd-cStart)*Nb[f], &cellDofs[f]);CHKERRQ(ierr); 1467*5f824522SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 1468*5f824522SMatthew G. Knepley PetscInt *closure = NULL; 1469*5f824522SMatthew G. Knepley PetscInt clSize = 0, cl; 1470*5f824522SMatthew G. Knepley 1471*5f824522SMatthew G. Knepley ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr); 1472*5f824522SMatthew G. Knepley for (cl = 0; cl < clSize*2; cl += 2) { 1473*5f824522SMatthew G. Knepley const PetscInt p = closure[cl]; 1474*5f824522SMatthew G. Knepley PetscInt fdof, d, foff; 1475*5f824522SMatthew G. Knepley 1476*5f824522SMatthew G. Knepley ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr); 1477*5f824522SMatthew G. Knepley ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr); 1478*5f824522SMatthew G. Knepley for (d = 0; d < fdof; ++d, ++cdoff) cellDofs[f][cdoff] = foff + d; 1479*5f824522SMatthew G. Knepley } 1480*5f824522SMatthew G. Knepley ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr); 1481*5f824522SMatthew G. Knepley } 1482*5f824522SMatthew G. Knepley 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]); 1483*5f824522SMatthew G. Knepley } 1484*5f824522SMatthew G. Knepley numGlobalBcs = 0; 1485*5f824522SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 1486*5f824522SMatthew G. Knepley const PetscInt *ind; 1487*5f824522SMatthew G. Knepley PetscInt off, cdof, d; 1488*5f824522SMatthew G. Knepley 1489*5f824522SMatthew G. Knepley ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr); 1490*5f824522SMatthew G. Knepley ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr); 1491*5f824522SMatthew G. Knepley ierr = PetscSectionGetConstraintIndices(s, p, &ind);CHKERRQ(ierr); 1492*5f824522SMatthew G. Knepley for (d = 0; d < cdof; ++d) globalBcs[numGlobalBcs++] = off + ind[d]; 1493*5f824522SMatthew G. Knepley } 1494*5f824522SMatthew G. Knepley 1495*5f824522SMatthew G. Knepley ierr = PCPatchSetDiscretisationInfoCombined(pc, dm, Nb, (const PetscInt **) cellDofs, numGlobalBcs, globalBcs, numGlobalBcs, globalBcs);CHKERRQ(ierr); 1496*5f824522SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1497*5f824522SMatthew G. Knepley ierr = PetscFree(cellDofs[f]);CHKERRQ(ierr); 1498*5f824522SMatthew G. Knepley } 1499*5f824522SMatthew G. Knepley ierr = PetscFree3(Nb, cellDofs, globalBcs);CHKERRQ(ierr); 1500*5f824522SMatthew G. Knepley ierr = PCPatchSetComputeOperator(pc, PCPatchComputeOperator_DMPlex_Private, NULL);CHKERRQ(ierr); 1501*5f824522SMatthew G. Knepley } 1502*5f824522SMatthew G. Knepley 15034bbf5ea8SMatthew G. Knepley localSize = patch->subspaceOffsets[patch->nsubspaces]; 15044bbf5ea8SMatthew G. Knepley ierr = VecCreateSeq(PETSC_COMM_SELF, localSize, &patch->localX);CHKERRQ(ierr); 15054bbf5ea8SMatthew G. Knepley ierr = VecSetUp(patch->localX);CHKERRQ(ierr); 15064bbf5ea8SMatthew G. Knepley ierr = VecDuplicate(patch->localX, &patch->localY);CHKERRQ(ierr); 15074bbf5ea8SMatthew G. Knepley ierr = PCPatchCreateCellPatches(pc);CHKERRQ(ierr); 15084bbf5ea8SMatthew G. Knepley ierr = PCPatchCreateCellPatchDiscretisationInfo(pc);CHKERRQ(ierr); 15094bbf5ea8SMatthew G. Knepley ierr = PCPatchCreateCellPatchBCs(pc);CHKERRQ(ierr); 15104bbf5ea8SMatthew G. Knepley 15114bbf5ea8SMatthew G. Knepley /* OK, now build the work vectors */ 15124bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, &pEnd);CHKERRQ(ierr); 15134bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(patch->npatch, &patch->patchX);CHKERRQ(ierr); 15144bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(patch->npatch, &patch->patchY);CHKERRQ(ierr); 15154bbf5ea8SMatthew G. Knepley if (patch->partition_of_unity && patch->multiplicative) {ierr = PetscMalloc1(patch->npatch, &patch->patch_dof_weights);CHKERRQ(ierr);} 15164bbf5ea8SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 15174bbf5ea8SMatthew G. Knepley PetscInt dof; 15184bbf5ea8SMatthew G. Knepley 15194bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr); 15204bbf5ea8SMatthew G. Knepley ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchX[p-pStart]);CHKERRQ(ierr); 15214bbf5ea8SMatthew G. Knepley ierr = VecSetUp(patch->patchX[p-pStart]);CHKERRQ(ierr); 15224bbf5ea8SMatthew G. Knepley ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchY[p-pStart]);CHKERRQ(ierr); 15234bbf5ea8SMatthew G. Knepley ierr = VecSetUp(patch->patchY[p-pStart]);CHKERRQ(ierr); 15244bbf5ea8SMatthew G. Knepley if (patch->partition_of_unity && patch->multiplicative) { 15254bbf5ea8SMatthew G. Knepley ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patch_dof_weights[p-pStart]);CHKERRQ(ierr); 15264bbf5ea8SMatthew G. Knepley ierr = VecSetUp(patch->patch_dof_weights[p-pStart]);CHKERRQ(ierr); 15274bbf5ea8SMatthew G. Knepley } 15284bbf5ea8SMatthew G. Knepley } 15294bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(patch->npatch, &patch->ksp);CHKERRQ(ierr); 15304bbf5ea8SMatthew G. Knepley ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr); 15314bbf5ea8SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) { 15324bbf5ea8SMatthew G. Knepley ierr = KSPCreate(PETSC_COMM_SELF, &patch->ksp[i]);CHKERRQ(ierr); 15334bbf5ea8SMatthew G. Knepley ierr = KSPSetOptionsPrefix(patch->ksp[i], prefix);CHKERRQ(ierr); 15344bbf5ea8SMatthew G. Knepley ierr = KSPAppendOptionsPrefix(patch->ksp[i], "sub_");CHKERRQ(ierr); 15354bbf5ea8SMatthew G. Knepley } 15364bbf5ea8SMatthew G. Knepley if (patch->save_operators) { 15374bbf5ea8SMatthew G. Knepley ierr = PetscMalloc1(patch->npatch, &patch->mat);CHKERRQ(ierr); 15384bbf5ea8SMatthew G. Knepley if (patch->multiplicative) {ierr = PetscMalloc1(patch->npatch, &patch->multMat);CHKERRQ(ierr);} 15394bbf5ea8SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) { 15404bbf5ea8SMatthew G. Knepley ierr = PCPatchCreateMatrix_Private(pc, i, &patch->mat[i]);CHKERRQ(ierr); 15414bbf5ea8SMatthew G. Knepley if (patch->multiplicative) {ierr = MatDuplicate(patch->mat[i], MAT_SHARE_NONZERO_PATTERN, &patch->multMat[i]);CHKERRQ(ierr);} 15424bbf5ea8SMatthew G. Knepley } 15434bbf5ea8SMatthew G. Knepley } 15444bbf5ea8SMatthew G. Knepley ierr = PetscLogEventEnd(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr); 15454bbf5ea8SMatthew G. Knepley 15464bbf5ea8SMatthew G. Knepley /* If desired, calculate weights for dof multiplicity */ 15474bbf5ea8SMatthew G. Knepley if (patch->partition_of_unity) { 15484bbf5ea8SMatthew G. Knepley ierr = VecDuplicate(patch->localX, &patch->dof_weights);CHKERRQ(ierr); 15494bbf5ea8SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) { 15504bbf5ea8SMatthew G. Knepley PetscInt dof; 15514bbf5ea8SMatthew G. Knepley 15524bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &dof);CHKERRQ(ierr); 15534bbf5ea8SMatthew G. Knepley if (dof <= 0) continue; 15544bbf5ea8SMatthew G. Knepley ierr = VecSet(patch->patchX[i], 1.0);CHKERRQ(ierr); 15554bbf5ea8SMatthew G. Knepley /* TODO: Do we need different scatters for X and Y? */ 15564bbf5ea8SMatthew G. Knepley ierr = VecGetArray(patch->patchX[i], &patchX);CHKERRQ(ierr); 15574bbf5ea8SMatthew G. Knepley /* Apply bcs to patchX (zero entries) */ 15584bbf5ea8SMatthew G. Knepley ierr = ISGetLocalSize(patch->bcs[i], &numBcs);CHKERRQ(ierr); 15594bbf5ea8SMatthew G. Knepley ierr = ISGetIndices(patch->bcs[i], &bcNodes);CHKERRQ(ierr); 15604bbf5ea8SMatthew G. Knepley for (j = 0; j < numBcs; ++j) patchX[bcNodes[j]] = 0; 15614bbf5ea8SMatthew G. Knepley ierr = ISRestoreIndices(patch->bcs[i], &bcNodes);CHKERRQ(ierr); 15624bbf5ea8SMatthew G. Knepley ierr = VecRestoreArray(patch->patchX[i], &patchX);CHKERRQ(ierr); 15634bbf5ea8SMatthew G. Knepley 15644bbf5ea8SMatthew G. Knepley ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchX[i], patch->dof_weights, ADD_VALUES, SCATTER_REVERSE);CHKERRQ(ierr); 15654bbf5ea8SMatthew G. Knepley } 15664bbf5ea8SMatthew G. Knepley ierr = VecReciprocal(patch->dof_weights);CHKERRQ(ierr); 15674bbf5ea8SMatthew G. Knepley if (patch->partition_of_unity && patch->multiplicative) { 15684bbf5ea8SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) { 15694bbf5ea8SMatthew G. Knepley ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->dof_weights, patch->patch_dof_weights[i], INSERT_VALUES, SCATTER_FORWARD);CHKERRQ(ierr); 15704bbf5ea8SMatthew G. Knepley } 15714bbf5ea8SMatthew G. Knepley } 15724bbf5ea8SMatthew G. Knepley } 15734bbf5ea8SMatthew G. Knepley } 15744bbf5ea8SMatthew G. Knepley if (patch->save_operators) { 15754bbf5ea8SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) { 15764bbf5ea8SMatthew G. Knepley ierr = MatZeroEntries(patch->mat[i]);CHKERRQ(ierr); 15774bbf5ea8SMatthew G. Knepley if (patch->multiplicative) {ierr = PCPatchComputeOperator_Private(pc, patch->mat[i], patch->multMat[i], i);CHKERRQ(ierr);} 15784bbf5ea8SMatthew G. Knepley else {ierr = PCPatchComputeOperator_Private(pc, patch->mat[i], NULL, i);CHKERRQ(ierr);} 15794bbf5ea8SMatthew G. Knepley ierr = KSPSetOperators(patch->ksp[i], patch->mat[i], patch->mat[i]);CHKERRQ(ierr); 15804bbf5ea8SMatthew G. Knepley } 15814bbf5ea8SMatthew G. Knepley } 1582*5f824522SMatthew G. Knepley if (!pc->setupcalled && patch->optionsSet) for (i = 0; i < patch->npatch; ++i) {ierr = KSPSetFromOptions(patch->ksp[i]);CHKERRQ(ierr);} 15834bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 15844bbf5ea8SMatthew G. Knepley } 15854bbf5ea8SMatthew G. Knepley 15864bbf5ea8SMatthew G. Knepley static PetscErrorCode PCApply_PATCH(PC pc, Vec x, Vec y) 15874bbf5ea8SMatthew G. Knepley { 15884bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 15894bbf5ea8SMatthew G. Knepley const PetscScalar *globalX = NULL; 15904bbf5ea8SMatthew G. Knepley PetscScalar *localX = NULL; 15914bbf5ea8SMatthew G. Knepley PetscScalar *globalY = NULL; 15924bbf5ea8SMatthew G. Knepley PetscScalar *patchX = NULL; 15934bbf5ea8SMatthew G. Knepley const PetscInt *bcNodes = NULL; 15944bbf5ea8SMatthew G. Knepley PetscInt nsweep = patch->symmetrise_sweep ? 2 : 1; 15954bbf5ea8SMatthew G. Knepley PetscInt start[2] = {0, 0}; 15964bbf5ea8SMatthew G. Knepley PetscInt end[2] = {-1, -1}; 15974bbf5ea8SMatthew G. Knepley const PetscInt inc[2] = {1, -1}; 15984bbf5ea8SMatthew G. Knepley const PetscScalar *localY; 15994bbf5ea8SMatthew G. Knepley const PetscInt *iterationSet; 16004bbf5ea8SMatthew G. Knepley PetscInt pStart, numBcs, n, sweep, bc, j; 16014bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 16024bbf5ea8SMatthew G. Knepley 16034bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 16044bbf5ea8SMatthew G. Knepley ierr = PetscLogEventBegin(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr); 16054bbf5ea8SMatthew G. Knepley ierr = PetscOptionsPushGetViewerOff(PETSC_TRUE);CHKERRQ(ierr); 16064bbf5ea8SMatthew G. Knepley end[0] = patch->npatch; 16074bbf5ea8SMatthew G. Knepley start[1] = patch->npatch-1; 16084bbf5ea8SMatthew G. Knepley if (patch->user_patches) { 16094bbf5ea8SMatthew G. Knepley ierr = ISGetLocalSize(patch->iterationSet, &end[0]);CHKERRQ(ierr); 16104bbf5ea8SMatthew G. Knepley start[1] = end[0] - 1; 16114bbf5ea8SMatthew G. Knepley ierr = ISGetIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr); 16124bbf5ea8SMatthew G. Knepley } 16134bbf5ea8SMatthew G. Knepley /* Scatter from global space into overlapped local spaces */ 16144bbf5ea8SMatthew G. Knepley ierr = VecGetArrayRead(x, &globalX);CHKERRQ(ierr); 16154bbf5ea8SMatthew G. Knepley ierr = VecGetArray(patch->localX, &localX);CHKERRQ(ierr); 16164bbf5ea8SMatthew G. Knepley ierr = PetscSFBcastBegin(patch->defaultSF, MPIU_SCALAR, globalX, localX);CHKERRQ(ierr); 16174bbf5ea8SMatthew G. Knepley ierr = PetscSFBcastEnd(patch->defaultSF, MPIU_SCALAR, globalX, localX);CHKERRQ(ierr); 16184bbf5ea8SMatthew G. Knepley ierr = VecRestoreArrayRead(x, &globalX);CHKERRQ(ierr); 16194bbf5ea8SMatthew G. Knepley ierr = VecRestoreArray(patch->localX, &localX);CHKERRQ(ierr); 16204bbf5ea8SMatthew G. Knepley 16214bbf5ea8SMatthew G. Knepley ierr = VecSet(patch->localY, 0.0);CHKERRQ(ierr); 16224bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, NULL);CHKERRQ(ierr); 16234bbf5ea8SMatthew G. Knepley for (sweep = 0; sweep < nsweep; sweep++) { 16244bbf5ea8SMatthew G. Knepley for (j = start[sweep]; j*inc[sweep] < end[sweep]*inc[sweep]; j += inc[sweep]) { 16254bbf5ea8SMatthew G. Knepley Mat multMat = NULL; 16264bbf5ea8SMatthew G. Knepley PetscInt i = patch->user_patches ? iterationSet[j] : j; 16274bbf5ea8SMatthew G. Knepley PetscInt start, len; 16284bbf5ea8SMatthew G. Knepley 16294bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &len);CHKERRQ(ierr); 16304bbf5ea8SMatthew G. Knepley ierr = PetscSectionGetOffset(patch->gtolCounts, i+pStart, &start);CHKERRQ(ierr); 16314bbf5ea8SMatthew G. Knepley /* TODO: Squash out these guys in the setup as well. */ 16324bbf5ea8SMatthew G. Knepley if (len <= 0) continue; 16334bbf5ea8SMatthew G. Knepley /* TODO: Do we need different scatters for X and Y? */ 16344bbf5ea8SMatthew G. Knepley ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->localX, patch->patchX[i], INSERT_VALUES, SCATTER_FORWARD);CHKERRQ(ierr); 16354bbf5ea8SMatthew G. Knepley /* Apply bcs to patchX (zero entries) */ 16364bbf5ea8SMatthew G. Knepley ierr = VecGetArray(patch->patchX[i], &patchX);CHKERRQ(ierr); 16374bbf5ea8SMatthew G. Knepley ierr = ISGetLocalSize(patch->bcs[i], &numBcs);CHKERRQ(ierr); 16384bbf5ea8SMatthew G. Knepley ierr = ISGetIndices(patch->bcs[i], &bcNodes);CHKERRQ(ierr); 16394bbf5ea8SMatthew G. Knepley for (bc = 0; bc < numBcs; ++bc) patchX[bcNodes[bc]] = 0; 16404bbf5ea8SMatthew G. Knepley ierr = ISRestoreIndices(patch->bcs[i], &bcNodes);CHKERRQ(ierr); 16414bbf5ea8SMatthew G. Knepley ierr = VecRestoreArray(patch->patchX[i], &patchX);CHKERRQ(ierr); 16424bbf5ea8SMatthew G. Knepley if (!patch->save_operators) { 16434bbf5ea8SMatthew G. Knepley Mat mat; 16444bbf5ea8SMatthew G. Knepley 16454bbf5ea8SMatthew G. Knepley ierr = PCPatchCreateMatrix_Private(pc, i, &mat);CHKERRQ(ierr); 16464bbf5ea8SMatthew G. Knepley if (patch->multiplicative) {ierr = MatDuplicate(mat, MAT_SHARE_NONZERO_PATTERN, &multMat);CHKERRQ(ierr);} 16474bbf5ea8SMatthew G. Knepley /* Populate operator here. */ 16484bbf5ea8SMatthew G. Knepley ierr = PCPatchComputeOperator_Private(pc, mat, multMat, i);CHKERRQ(ierr); 16494bbf5ea8SMatthew G. Knepley ierr = KSPSetOperators(patch->ksp[i], mat, mat); 16504bbf5ea8SMatthew G. Knepley /* Drop reference so the KSPSetOperators below will blow it away. */ 16514bbf5ea8SMatthew G. Knepley ierr = MatDestroy(&mat);CHKERRQ(ierr); 16524bbf5ea8SMatthew G. Knepley } else if (patch->multiplicative) { 16534bbf5ea8SMatthew G. Knepley multMat = patch->multMat[i]; 16544bbf5ea8SMatthew G. Knepley } 16554bbf5ea8SMatthew G. Knepley 16564bbf5ea8SMatthew G. Knepley ierr = PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr); 16574bbf5ea8SMatthew G. Knepley ierr = KSPSolve(patch->ksp[i], patch->patchX[i], patch->patchY[i]);CHKERRQ(ierr); 16584bbf5ea8SMatthew G. Knepley ierr = PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr); 16594bbf5ea8SMatthew G. Knepley 16604bbf5ea8SMatthew G. Knepley if (!patch->save_operators) { 16614bbf5ea8SMatthew G. Knepley PC pc; 16624bbf5ea8SMatthew G. Knepley ierr = KSPSetOperators(patch->ksp[i], NULL, NULL);CHKERRQ(ierr); 16634bbf5ea8SMatthew G. Knepley ierr = KSPGetPC(patch->ksp[i], &pc);CHKERRQ(ierr); 16644bbf5ea8SMatthew G. Knepley /* Destroy PC context too, otherwise the factored matrix hangs around. */ 16654bbf5ea8SMatthew G. Knepley ierr = PCReset(pc);CHKERRQ(ierr); 16664bbf5ea8SMatthew G. Knepley } 16674bbf5ea8SMatthew G. Knepley 16684bbf5ea8SMatthew G. Knepley if (patch->partition_of_unity && patch->multiplicative) { 16694bbf5ea8SMatthew G. Knepley ierr = VecPointwiseMult(patch->patchY[i], patch->patchY[i], patch->patch_dof_weights[i]);CHKERRQ(ierr); 16704bbf5ea8SMatthew G. Knepley } 16714bbf5ea8SMatthew G. Knepley ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchY[i], patch->localY, ADD_VALUES, SCATTER_REVERSE);CHKERRQ(ierr); 16724bbf5ea8SMatthew G. Knepley 16734bbf5ea8SMatthew G. Knepley /* Get the matrix on the patch but with only global bcs applied. 16744bbf5ea8SMatthew G. Knepley * This matrix is then multiplied with the result from the previous solve 16754bbf5ea8SMatthew G. Knepley * to obtain by how much the residual changes. */ 16764bbf5ea8SMatthew G. Knepley if (patch->multiplicative) { 16774bbf5ea8SMatthew G. Knepley ierr = MatMult(multMat, patch->patchY[i], patch->patchX[i]);CHKERRQ(ierr); 16784bbf5ea8SMatthew G. Knepley ierr = VecScale(patch->patchX[i], -1.0);CHKERRQ(ierr); 16794bbf5ea8SMatthew G. Knepley ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchX[i], patch->localX, ADD_VALUES, SCATTER_REVERSE);CHKERRQ(ierr); 16804bbf5ea8SMatthew G. Knepley if (!patch->save_operators) {ierr = MatDestroy(&multMat);CHKERRQ(ierr);} 16814bbf5ea8SMatthew G. Knepley } 16824bbf5ea8SMatthew G. Knepley } 16834bbf5ea8SMatthew G. Knepley } 16844bbf5ea8SMatthew G. Knepley if (patch->user_patches) {ierr = ISRestoreIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr);} 16854bbf5ea8SMatthew G. Knepley /* XXX: should we do this on the global vector? */ 16864bbf5ea8SMatthew G. Knepley if (patch->partition_of_unity && !patch->multiplicative) { 16874bbf5ea8SMatthew G. Knepley ierr = VecPointwiseMult(patch->localY, patch->localY, patch->dof_weights);CHKERRQ(ierr); 16884bbf5ea8SMatthew G. Knepley } 16894bbf5ea8SMatthew G. Knepley /* Now patch->localY contains the solution of the patch solves, so we need to combine them all. */ 16904bbf5ea8SMatthew G. Knepley ierr = VecSet(y, 0.0);CHKERRQ(ierr); 16914bbf5ea8SMatthew G. Knepley ierr = VecGetArray(y, &globalY);CHKERRQ(ierr); 16924bbf5ea8SMatthew G. Knepley ierr = VecGetArrayRead(patch->localY, &localY);CHKERRQ(ierr); 16934bbf5ea8SMatthew G. Knepley ierr = PetscSFReduceBegin(patch->defaultSF, MPIU_SCALAR, localY, globalY, MPI_SUM);CHKERRQ(ierr); 16944bbf5ea8SMatthew G. Knepley ierr = PetscSFReduceEnd(patch->defaultSF, MPIU_SCALAR, localY, globalY, MPI_SUM);CHKERRQ(ierr); 16954bbf5ea8SMatthew G. Knepley ierr = VecRestoreArrayRead(patch->localY, &localY);CHKERRQ(ierr); 16964bbf5ea8SMatthew G. Knepley 16974bbf5ea8SMatthew G. Knepley /* Now we need to send the global BC values through */ 16984bbf5ea8SMatthew G. Knepley ierr = VecGetArrayRead(x, &globalX);CHKERRQ(ierr); 16994bbf5ea8SMatthew G. Knepley ierr = ISGetSize(patch->globalBcNodes, &numBcs);CHKERRQ(ierr); 17004bbf5ea8SMatthew G. Knepley ierr = ISGetIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr); 17014bbf5ea8SMatthew G. Knepley ierr = VecGetLocalSize(x, &n);CHKERRQ(ierr); 17024bbf5ea8SMatthew G. Knepley for (bc = 0; bc < numBcs; ++bc) { 17034bbf5ea8SMatthew G. Knepley const PetscInt idx = bcNodes[bc]; 17044bbf5ea8SMatthew G. Knepley if (idx < n) globalY[idx] = globalX[idx]; 17054bbf5ea8SMatthew G. Knepley } 17064bbf5ea8SMatthew G. Knepley 17074bbf5ea8SMatthew G. Knepley ierr = ISRestoreIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr); 17084bbf5ea8SMatthew G. Knepley ierr = VecRestoreArrayRead(x, &globalX);CHKERRQ(ierr); 17094bbf5ea8SMatthew G. Knepley ierr = VecRestoreArray(y, &globalY);CHKERRQ(ierr); 17104bbf5ea8SMatthew G. Knepley 17114bbf5ea8SMatthew G. Knepley ierr = PetscOptionsPopGetViewerOff();CHKERRQ(ierr); 17124bbf5ea8SMatthew G. Knepley ierr = PetscLogEventEnd(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr); 17134bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 17144bbf5ea8SMatthew G. Knepley } 17154bbf5ea8SMatthew G. Knepley 17164bbf5ea8SMatthew G. Knepley static PetscErrorCode PCReset_PATCH(PC pc) 17174bbf5ea8SMatthew G. Knepley { 17184bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 17194bbf5ea8SMatthew G. Knepley PetscInt i; 17204bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 17214bbf5ea8SMatthew G. Knepley 17224bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 17234bbf5ea8SMatthew G. Knepley /* TODO: Get rid of all these ifs */ 17244bbf5ea8SMatthew G. Knepley ierr = PetscSFDestroy(&patch->defaultSF);CHKERRQ(ierr); 17254bbf5ea8SMatthew G. Knepley ierr = PetscSectionDestroy(&patch->cellCounts);CHKERRQ(ierr); 1726*5f824522SMatthew G. Knepley ierr = PetscSectionDestroy(&patch->pointCounts);CHKERRQ(ierr); 17274bbf5ea8SMatthew G. Knepley ierr = PetscSectionDestroy(&patch->cellNumbering);CHKERRQ(ierr); 17284bbf5ea8SMatthew G. Knepley ierr = PetscSectionDestroy(&patch->gtolCounts);CHKERRQ(ierr); 17294bbf5ea8SMatthew G. Knepley ierr = PetscSectionDestroy(&patch->bcCounts);CHKERRQ(ierr); 17304bbf5ea8SMatthew G. Knepley ierr = ISDestroy(&patch->gtol);CHKERRQ(ierr); 17314bbf5ea8SMatthew G. Knepley ierr = ISDestroy(&patch->cells);CHKERRQ(ierr); 1732*5f824522SMatthew G. Knepley ierr = ISDestroy(&patch->points);CHKERRQ(ierr); 17334bbf5ea8SMatthew G. Knepley ierr = ISDestroy(&patch->dofs);CHKERRQ(ierr); 1734*5f824522SMatthew G. Knepley ierr = ISDestroy(&patch->offs);CHKERRQ(ierr); 1735*5f824522SMatthew G. Knepley ierr = PetscSectionDestroy(&patch->patchSection);CHKERRQ(ierr); 17364bbf5ea8SMatthew G. Knepley ierr = ISDestroy(&patch->ghostBcNodes);CHKERRQ(ierr); 17374bbf5ea8SMatthew G. Knepley ierr = ISDestroy(&patch->globalBcNodes);CHKERRQ(ierr); 17384bbf5ea8SMatthew G. Knepley 1739*5f824522SMatthew G. Knepley if (patch->dofSection) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscSectionDestroy(&patch->dofSection[i]);CHKERRQ(ierr);} 17404bbf5ea8SMatthew G. Knepley ierr = PetscFree(patch->dofSection);CHKERRQ(ierr); 17414bbf5ea8SMatthew G. Knepley ierr = PetscFree(patch->bs);CHKERRQ(ierr); 17424bbf5ea8SMatthew G. Knepley ierr = PetscFree(patch->nodesPerCell);CHKERRQ(ierr); 1743*5f824522SMatthew G. Knepley if (patch->cellNodeMap) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscFree(patch->cellNodeMap[i]);CHKERRQ(ierr);} 17444bbf5ea8SMatthew G. Knepley ierr = PetscFree(patch->cellNodeMap);CHKERRQ(ierr); 17454bbf5ea8SMatthew G. Knepley ierr = PetscFree(patch->subspaceOffsets);CHKERRQ(ierr); 17464bbf5ea8SMatthew G. Knepley 17474bbf5ea8SMatthew G. Knepley if (patch->bcs) { 1748*5f824522SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->bcs[i]);CHKERRQ(ierr);} 17494bbf5ea8SMatthew G. Knepley ierr = PetscFree(patch->bcs);CHKERRQ(ierr); 17504bbf5ea8SMatthew G. Knepley } 17514bbf5ea8SMatthew G. Knepley if (patch->multBcs) { 1752*5f824522SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->multBcs[i]);CHKERRQ(ierr);} 17534bbf5ea8SMatthew G. Knepley ierr = PetscFree(patch->multBcs);CHKERRQ(ierr); 17544bbf5ea8SMatthew G. Knepley } 17554bbf5ea8SMatthew G. Knepley if (patch->ksp) { 1756*5f824522SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) {ierr = KSPReset(patch->ksp[i]);CHKERRQ(ierr);} 17574bbf5ea8SMatthew G. Knepley } 17584bbf5ea8SMatthew G. Knepley 17594bbf5ea8SMatthew G. Knepley ierr = VecDestroy(&patch->localX);CHKERRQ(ierr); 17604bbf5ea8SMatthew G. Knepley ierr = VecDestroy(&patch->localY);CHKERRQ(ierr); 17614bbf5ea8SMatthew G. Knepley if (patch->patchX) { 1762*5f824522SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchX[i]);CHKERRQ(ierr);} 17634bbf5ea8SMatthew G. Knepley ierr = PetscFree(patch->patchX);CHKERRQ(ierr); 17644bbf5ea8SMatthew G. Knepley } 17654bbf5ea8SMatthew G. Knepley if (patch->patchY) { 1766*5f824522SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchY[i]);CHKERRQ(ierr);} 17674bbf5ea8SMatthew G. Knepley ierr = PetscFree(patch->patchY);CHKERRQ(ierr); 17684bbf5ea8SMatthew G. Knepley } 17694bbf5ea8SMatthew G. Knepley ierr = VecDestroy(&patch->dof_weights);CHKERRQ(ierr); 17704bbf5ea8SMatthew G. Knepley if (patch->patch_dof_weights) { 1771*5f824522SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patch_dof_weights[i]);CHKERRQ(ierr);} 17724bbf5ea8SMatthew G. Knepley ierr = PetscFree(patch->patch_dof_weights);CHKERRQ(ierr); 17734bbf5ea8SMatthew G. Knepley } 17744bbf5ea8SMatthew G. Knepley if (patch->mat) { 1775*5f824522SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->mat[i]);CHKERRQ(ierr);} 17764bbf5ea8SMatthew G. Knepley ierr = PetscFree(patch->mat);CHKERRQ(ierr); 1777*5f824522SMatthew G. Knepley } 1778*5f824522SMatthew G. Knepley if (patch->multMat) { 1779*5f824522SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->multMat[i]);CHKERRQ(ierr);} 17804bbf5ea8SMatthew G. Knepley ierr = PetscFree(patch->multMat);CHKERRQ(ierr); 17814bbf5ea8SMatthew G. Knepley } 17824bbf5ea8SMatthew G. Knepley ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr); 1783*5f824522SMatthew G. Knepley if (patch->userIS) { 1784*5f824522SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->userIS[i]);CHKERRQ(ierr);} 1785*5f824522SMatthew G. Knepley ierr = PetscFree(patch->userIS);CHKERRQ(ierr); 1786*5f824522SMatthew G. Knepley } 17874bbf5ea8SMatthew G. Knepley patch->bs = 0; 17884bbf5ea8SMatthew G. Knepley patch->cellNodeMap = NULL; 17894bbf5ea8SMatthew G. Knepley ierr = ISDestroy(&patch->iterationSet);CHKERRQ(ierr); 1790*5f824522SMatthew G. Knepley 1791*5f824522SMatthew G. Knepley ierr = PetscViewerDestroy(&patch->viewerSection);CHKERRQ(ierr); 17924bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 17934bbf5ea8SMatthew G. Knepley } 17944bbf5ea8SMatthew G. Knepley 17954bbf5ea8SMatthew G. Knepley static PetscErrorCode PCDestroy_PATCH(PC pc) 17964bbf5ea8SMatthew G. Knepley { 17974bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 17984bbf5ea8SMatthew G. Knepley PetscInt i; 17994bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 18004bbf5ea8SMatthew G. Knepley 18014bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 18024bbf5ea8SMatthew G. Knepley ierr = PCReset_PATCH(pc);CHKERRQ(ierr); 18034bbf5ea8SMatthew G. Knepley if (patch->ksp) { 18044bbf5ea8SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) {ierr = KSPDestroy(&patch->ksp[i]);CHKERRQ(ierr);} 18054bbf5ea8SMatthew G. Knepley ierr = PetscFree(patch->ksp);CHKERRQ(ierr); 18064bbf5ea8SMatthew G. Knepley } 18074bbf5ea8SMatthew G. Knepley ierr = PetscFree(pc->data);CHKERRQ(ierr); 18084bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 18094bbf5ea8SMatthew G. Knepley } 18104bbf5ea8SMatthew G. Knepley 18114bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetFromOptions_PATCH(PetscOptionItems *PetscOptionsObject, PC pc) 18124bbf5ea8SMatthew G. Knepley { 18134bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 18144bbf5ea8SMatthew G. Knepley PCPatchConstructType patchConstructionType = PC_PATCH_STAR; 1815*5f824522SMatthew G. Knepley char sub_mat_type[PETSC_MAX_PATH_LEN]; 1816*5f824522SMatthew G. Knepley const char *prefix; 18174bbf5ea8SMatthew G. Knepley PetscBool flg, dimflg, codimflg; 1818*5f824522SMatthew G. Knepley MPI_Comm comm; 18194bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 18204bbf5ea8SMatthew G. Knepley 18214bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 1822*5f824522SMatthew G. Knepley ierr = PetscObjectGetComm((PetscObject) pc, &comm);CHKERRQ(ierr); 1823*5f824522SMatthew G. Knepley ierr = PetscObjectGetOptionsPrefix((PetscObject) pc, &prefix);CHKERRQ(ierr); 18244bbf5ea8SMatthew G. Knepley ierr = PetscOptionsHead(PetscOptionsObject, "Vertex-patch Additive Schwarz options");CHKERRQ(ierr); 18254bbf5ea8SMatthew G. Knepley ierr = PetscOptionsBool("-pc_patch_save_operators", "Store all patch operators for lifetime of PC?", "PCPatchSetSaveOperators", patch->save_operators, &patch->save_operators, &flg);CHKERRQ(ierr); 18264bbf5ea8SMatthew G. Knepley ierr = PetscOptionsBool("-pc_patch_partition_of_unity", "Weight contributions by dof multiplicity?", "PCPatchSetPartitionOfUnity", patch->partition_of_unity, &patch->partition_of_unity, &flg);CHKERRQ(ierr); 18274bbf5ea8SMatthew G. Knepley ierr = PetscOptionsBool("-pc_patch_multiplicative", "Gauss-Seidel instead of Jacobi?", "PCPatchSetMultiplicative", patch->multiplicative, &patch->multiplicative, &flg);CHKERRQ(ierr); 1828*5f824522SMatthew G. Knepley ierr = PetscOptionsInt("-pc_patch_construct_dim", "What dimension of mesh point to construct patches by? (0 = vertices)", "PCPATCH", patch->dim, &patch->dim, &dimflg);CHKERRQ(ierr); 1829*5f824522SMatthew G. Knepley ierr = PetscOptionsInt("-pc_patch_construct_codim", "What co-dimension of mesh point to construct patches by? (0 = cells)", "PCPATCH", patch->codim, &patch->codim, &codimflg);CHKERRQ(ierr); 1830*5f824522SMatthew G. Knepley if (dimflg && codimflg) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Can only set one of dimension or co-dimension");CHKERRQ(ierr); 18314bbf5ea8SMatthew G. Knepley ierr = PetscOptionsEnum("-pc_patch_construct_type", "How should the patches be constructed?", "PCPatchSetConstructType", PCPatchConstructTypes, (PetscEnum) patchConstructionType, (PetscEnum *) &patchConstructionType, &flg);CHKERRQ(ierr); 18324bbf5ea8SMatthew G. Knepley if (flg) {ierr = PCPatchSetConstructType(pc, patchConstructionType, NULL, NULL);CHKERRQ(ierr);} 1833*5f824522SMatthew G. Knepley ierr = PetscOptionsInt("-pc_patch_vanka_dim", "Topological dimension of entities for Vanka to ignore", "PCPATCH", patch->vankadim, &patch->vankadim, &flg);CHKERRQ(ierr); 1834*5f824522SMatthew G. Knepley ierr = PetscOptionsInt("-pc_patch_ignore_dim", "Topological dimension of entities for completion to ignore", "PCPATCH", patch->ignoredim, &patch->ignoredim, &flg);CHKERRQ(ierr); 1835*5f824522SMatthew G. Knepley ierr = PetscOptionsFList("-pc_patch_sub_mat_type", "Matrix type for patch solves", "PCPatchSetSubMatType", MatList, NULL, sub_mat_type, PETSC_MAX_PATH_LEN, &flg);CHKERRQ(ierr); 18364bbf5ea8SMatthew G. Knepley if (flg) {ierr = PCPatchSetSubMatType(pc, sub_mat_type);CHKERRQ(ierr);} 1837*5f824522SMatthew G. Knepley ierr = PetscOptionsBool("-pc_patch_symmetrise_sweep", "Go start->end, end->start?", "PCPATCH", patch->symmetrise_sweep, &patch->symmetrise_sweep, &flg);CHKERRQ(ierr); 1838*5f824522SMatthew G. Knepley ierr = PetscOptionsInt("-pc_patch_exclude_subspace", "What subspace (if any) to exclude in construction?", "PCPATCH", patch->exclude_subspace, &patch->exclude_subspace, &flg);CHKERRQ(ierr); 1839*5f824522SMatthew G. Knepley 1840*5f824522SMatthew G. Knepley ierr = PetscOptionsBool("-pc_patch_patches_view", "Print out information during patch construction", "PCPATCH", patch->viewPatches, &patch->viewPatches, &flg);CHKERRQ(ierr); 1841*5f824522SMatthew G. Knepley ierr = PetscOptionsGetViewer(comm, prefix, "-pc_patch_cells_view", &patch->viewerCells, &patch->formatCells, &patch->viewCells);CHKERRQ(ierr); 1842*5f824522SMatthew G. Knepley ierr = PetscOptionsGetViewer(comm, prefix, "-pc_patch_points_view", &patch->viewerPoints, &patch->formatPoints, &patch->viewPoints);CHKERRQ(ierr); 1843*5f824522SMatthew G. Knepley ierr = PetscOptionsGetViewer(comm, prefix, "-pc_patch_section_view", &patch->viewerSection, &patch->formatSection, &patch->viewSection);CHKERRQ(ierr); 1844*5f824522SMatthew G. Knepley ierr = PetscOptionsGetViewer(comm, prefix, "-pc_patch_sub_mat_view", &patch->viewerMatrix, &patch->formatMatrix, &patch->viewMatrix);CHKERRQ(ierr); 18454bbf5ea8SMatthew G. Knepley ierr = PetscOptionsTail();CHKERRQ(ierr); 1846*5f824522SMatthew G. Knepley patch->optionsSet = PETSC_TRUE; 18474bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 18484bbf5ea8SMatthew G. Knepley } 18494bbf5ea8SMatthew G. Knepley 18504bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUpOnBlocks_PATCH(PC pc) 18514bbf5ea8SMatthew G. Knepley { 18524bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH*) pc->data; 18534bbf5ea8SMatthew G. Knepley KSPConvergedReason reason; 18544bbf5ea8SMatthew G. Knepley PetscInt i; 18554bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 18564bbf5ea8SMatthew G. Knepley 18574bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 18584bbf5ea8SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) { 18594bbf5ea8SMatthew G. Knepley ierr = KSPSetUp(patch->ksp[i]);CHKERRQ(ierr); 18604bbf5ea8SMatthew G. Knepley ierr = KSPGetConvergedReason(patch->ksp[i], &reason);CHKERRQ(ierr); 18614bbf5ea8SMatthew G. Knepley if (reason == KSP_DIVERGED_PCSETUP_FAILED) pc->failedreason = PC_SUBPC_ERROR; 18624bbf5ea8SMatthew G. Knepley } 18634bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 18644bbf5ea8SMatthew G. Knepley } 18654bbf5ea8SMatthew G. Knepley 18664bbf5ea8SMatthew G. Knepley static PetscErrorCode PCView_PATCH(PC pc, PetscViewer viewer) 18674bbf5ea8SMatthew G. Knepley { 18684bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 18694bbf5ea8SMatthew G. Knepley PetscViewer sviewer; 18704bbf5ea8SMatthew G. Knepley PetscBool isascii; 18714bbf5ea8SMatthew G. Knepley PetscMPIInt rank; 18724bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 18734bbf5ea8SMatthew G. Knepley 18744bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 18754bbf5ea8SMatthew G. Knepley /* TODO Redo tabbing with set tbas in new style */ 18764bbf5ea8SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii);CHKERRQ(ierr); 18774bbf5ea8SMatthew G. Knepley if (!isascii) PetscFunctionReturn(0); 18784bbf5ea8SMatthew G. Knepley ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) pc), &rank);CHKERRQ(ierr); 18794bbf5ea8SMatthew G. Knepley ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 18804bbf5ea8SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "Subspace Correction preconditioner with %d patches\n", patch->npatch);CHKERRQ(ierr); 18814bbf5ea8SMatthew G. Knepley if (patch->multiplicative) {ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: multiplicative\n");CHKERRQ(ierr);} 18824bbf5ea8SMatthew G. Knepley else {ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: additive\n");CHKERRQ(ierr);} 18834bbf5ea8SMatthew G. Knepley if (patch->partition_of_unity) {ierr = PetscViewerASCIIPrintf(viewer, "Weighting by partition of unity\n");CHKERRQ(ierr);} 18844bbf5ea8SMatthew G. Knepley else {ierr = PetscViewerASCIIPrintf(viewer, "Not weighting by partition of unity\n");CHKERRQ(ierr);} 18854bbf5ea8SMatthew G. Knepley if (patch->symmetrise_sweep) {ierr = PetscViewerASCIIPrintf(viewer, "Symmetrising sweep (start->end, then end->start)\n");CHKERRQ(ierr);} 18864bbf5ea8SMatthew G. Knepley else {ierr = PetscViewerASCIIPrintf(viewer, "Not symmetrising sweep\n");CHKERRQ(ierr);} 18874bbf5ea8SMatthew G. Knepley if (!patch->save_operators) {ierr = PetscViewerASCIIPrintf(viewer, "Not saving patch operators (rebuilt every PCApply)\n");CHKERRQ(ierr);} 18884bbf5ea8SMatthew G. Knepley else {ierr = PetscViewerASCIIPrintf(viewer, "Saving patch operators (rebuilt every PCSetUp)\n");CHKERRQ(ierr);} 18894bbf5ea8SMatthew G. Knepley if (patch->patchconstructop == PCPatchConstruct_Star) {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: star\n");CHKERRQ(ierr);} 18904bbf5ea8SMatthew G. Knepley else if (patch->patchconstructop == PCPatchConstruct_Vanka) {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: Vanka\n");CHKERRQ(ierr);} 18914bbf5ea8SMatthew G. Knepley else if (patch->patchconstructop == PCPatchConstruct_User) {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: user-specified\n");CHKERRQ(ierr);} 18924bbf5ea8SMatthew G. Knepley else {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: unknown\n");CHKERRQ(ierr);} 18934bbf5ea8SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "KSP on patches (all same):\n");CHKERRQ(ierr); 18944bbf5ea8SMatthew G. Knepley if (patch->ksp) { 18954bbf5ea8SMatthew G. Knepley ierr = PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr); 18964bbf5ea8SMatthew G. Knepley if (!rank) { 18974bbf5ea8SMatthew G. Knepley ierr = PetscViewerASCIIPushTab(sviewer);CHKERRQ(ierr); 18984bbf5ea8SMatthew G. Knepley ierr = KSPView(patch->ksp[0], sviewer);CHKERRQ(ierr); 18994bbf5ea8SMatthew G. Knepley ierr = PetscViewerASCIIPopTab(sviewer);CHKERRQ(ierr); 19004bbf5ea8SMatthew G. Knepley } 19014bbf5ea8SMatthew G. Knepley ierr = PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr); 19024bbf5ea8SMatthew G. Knepley } else { 19034bbf5ea8SMatthew G. Knepley ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 19044bbf5ea8SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "KSP not yet set.\n");CHKERRQ(ierr); 19054bbf5ea8SMatthew G. Knepley ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 19064bbf5ea8SMatthew G. Knepley } 19074bbf5ea8SMatthew G. Knepley ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 19084bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 19094bbf5ea8SMatthew G. Knepley } 19104bbf5ea8SMatthew G. Knepley 1911e5893cccSMatthew G. Knepley /*MC 1912e5893cccSMatthew G. Knepley PCPATCH = "patch" - A PC object that encapsulates flexible definition of blocks for overlapping and non-overlapping 1913e5893cccSMatthew G. Knepley small block additive and multiplicative preconditioners. Block definition is based on topology from 1914e5893cccSMatthew G. Knepley a DM and equation numbering from a PetscSection. 1915e5893cccSMatthew G. Knepley 1916e5893cccSMatthew G. Knepley Options Database Keys: 1917e5893cccSMatthew G. Knepley + -pc_patch_cells_view - Views the process local cell numbers for each patch 1918e5893cccSMatthew G. Knepley . -pc_patch_points_view - Views the process local mesh point numbers for each patch 1919e5893cccSMatthew G. Knepley . -pc_patch_g2l_view - Views the map between global dofs and patch local dofs for each patch 1920e5893cccSMatthew G. Knepley . -pc_patch_patches_view - Views the global dofs associated with each patch and its boundary 1921e5893cccSMatthew G. Knepley - -pc_patch_sub_mat_view - Views the matrix associated with each patch 1922e5893cccSMatthew G. Knepley 1923e5893cccSMatthew G. Knepley Level: intermediate 1924e5893cccSMatthew G. Knepley 1925e5893cccSMatthew G. Knepley .seealso: PCType, PCCreate(), PCSetType() 1926e5893cccSMatthew G. Knepley M*/ 1927642283e9SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PCCreate_Patch(PC pc) 19284bbf5ea8SMatthew G. Knepley { 19294bbf5ea8SMatthew G. Knepley PC_PATCH *patch; 19304bbf5ea8SMatthew G. Knepley PetscErrorCode ierr; 19314bbf5ea8SMatthew G. Knepley 19324bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 19334bbf5ea8SMatthew G. Knepley ierr = PetscNewLog(pc, &patch);CHKERRQ(ierr); 19344bbf5ea8SMatthew G. Knepley 19354bbf5ea8SMatthew G. Knepley /* Set some defaults */ 1936*5f824522SMatthew G. Knepley patch->combined = PETSC_FALSE; 19374bbf5ea8SMatthew G. Knepley patch->save_operators = PETSC_TRUE; 19384bbf5ea8SMatthew G. Knepley patch->partition_of_unity = PETSC_FALSE; 19394bbf5ea8SMatthew G. Knepley patch->multiplicative = PETSC_FALSE; 19404bbf5ea8SMatthew G. Knepley patch->codim = -1; 19414bbf5ea8SMatthew G. Knepley patch->dim = -1; 19424bbf5ea8SMatthew G. Knepley patch->exclude_subspace = -1; 19434bbf5ea8SMatthew G. Knepley patch->vankadim = -1; 1944*5f824522SMatthew G. Knepley patch->ignoredim = -1; 19454bbf5ea8SMatthew G. Knepley patch->patchconstructop = PCPatchConstruct_Star; 19464bbf5ea8SMatthew G. Knepley patch->symmetrise_sweep = PETSC_FALSE; 1947*5f824522SMatthew G. Knepley patch->npatch = 0; 19484bbf5ea8SMatthew G. Knepley patch->userIS = NULL; 1949*5f824522SMatthew G. Knepley patch->optionsSet = PETSC_FALSE; 19504bbf5ea8SMatthew G. Knepley patch->iterationSet = NULL; 19514bbf5ea8SMatthew G. Knepley patch->user_patches = PETSC_FALSE; 1952*5f824522SMatthew G. Knepley ierr = PetscStrallocpy(MATDENSE, (char **) &patch->sub_mat_type);CHKERRQ(ierr); 1953*5f824522SMatthew G. Knepley patch->viewPatches = PETSC_FALSE; 1954*5f824522SMatthew G. Knepley patch->viewCells = PETSC_FALSE; 1955*5f824522SMatthew G. Knepley patch->viewPoints = PETSC_FALSE; 1956*5f824522SMatthew G. Knepley patch->viewSection = PETSC_FALSE; 1957*5f824522SMatthew G. Knepley patch->viewMatrix = PETSC_FALSE; 19584bbf5ea8SMatthew G. Knepley 19594bbf5ea8SMatthew G. Knepley pc->data = (void *) patch; 19604bbf5ea8SMatthew G. Knepley pc->ops->apply = PCApply_PATCH; 19614bbf5ea8SMatthew G. Knepley pc->ops->applytranspose = 0; /* PCApplyTranspose_PATCH; */ 19624bbf5ea8SMatthew G. Knepley pc->ops->setup = PCSetUp_PATCH; 19634bbf5ea8SMatthew G. Knepley pc->ops->reset = PCReset_PATCH; 19644bbf5ea8SMatthew G. Knepley pc->ops->destroy = PCDestroy_PATCH; 19654bbf5ea8SMatthew G. Knepley pc->ops->setfromoptions = PCSetFromOptions_PATCH; 19664bbf5ea8SMatthew G. Knepley pc->ops->setuponblocks = PCSetUpOnBlocks_PATCH; 19674bbf5ea8SMatthew G. Knepley pc->ops->view = PCView_PATCH; 19684bbf5ea8SMatthew G. Knepley pc->ops->applyrichardson = 0; 19694bbf5ea8SMatthew G. Knepley 19704bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 19714bbf5ea8SMatthew G. Knepley } 1972