14bbf5ea8SMatthew G. Knepley #include <petsc/private/pcpatchimpl.h> /*I "petscpc.h" I*/ 254ab768cSLawrence Mitchell #include <petsc/private/kspimpl.h> /* For ksp->setfromoptionscalled */ 39d4fc724SLawrence Mitchell #include <petsc/private/vecimpl.h> /* For vec->map */ 45f824522SMatthew G. Knepley #include <petsc/private/dmpleximpl.h> /* For DMPlexComputeJacobian_Patch_Internal() */ 54bbf5ea8SMatthew G. Knepley #include <petscsf.h> 64bbf5ea8SMatthew G. Knepley #include <petscbt.h> 75f824522SMatthew G. Knepley #include <petscds.h> 8c73d2cf6SLawrence Mitchell #include <../src/mat/impls/dense/seq/dense.h> /*I "petscmat.h" I*/ 94bbf5ea8SMatthew G. Knepley 10f39ec787SMatthew G. Knepley PetscBool PCPatchcite = PETSC_FALSE; 11f39ec787SMatthew G. Knepley const char PCPatchCitation[] = "@article{FarrellKnepleyWechsungMitchell2020,\n" 12f39ec787SMatthew G. Knepley "title = {{PCPATCH}: software for the topological construction of multigrid relaxation methods},\n" 13f39ec787SMatthew G. Knepley "author = {Patrick E Farrell and Matthew G Knepley and Lawrence Mitchell and Florian Wechsung},\n" 14f39ec787SMatthew G. Knepley "journal = {ACM Transaction on Mathematical Software},\n" 15f39ec787SMatthew G. Knepley "eprint = {http://arxiv.org/abs/1912.08516},\n" 16f39ec787SMatthew G. Knepley "volume = {47},\n" 17f39ec787SMatthew G. Knepley "number = {3},\n" 18f39ec787SMatthew G. Knepley "pages = {1--22},\n" 19f39ec787SMatthew G. Knepley "year = {2021},\n" 20f39ec787SMatthew G. Knepley "petsc_uses={KSP,DMPlex}\n}\n"; 21f39ec787SMatthew G. Knepley 229d4fc724SLawrence Mitchell PetscLogEvent PC_Patch_CreatePatches, PC_Patch_ComputeOp, PC_Patch_Solve, PC_Patch_Apply, PC_Patch_Prealloc; 234bbf5ea8SMatthew G. Knepley 24d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode ObjectView(PetscObject obj, PetscViewer viewer, PetscViewerFormat format) 25d71ae5a4SJacob Faibussowitsch { 269566063dSJacob Faibussowitsch PetscCall(PetscViewerPushFormat(viewer, format)); 279566063dSJacob Faibussowitsch PetscCall(PetscObjectView(obj, viewer)); 289566063dSJacob Faibussowitsch PetscCall(PetscViewerPopFormat(viewer)); 293ba16761SJacob Faibussowitsch return PETSC_SUCCESS; 305f824522SMatthew G. Knepley } 315f824522SMatthew G. Knepley 32d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchConstruct_Star(void *vpatch, DM dm, PetscInt point, PetscHSetI ht) 33d71ae5a4SJacob Faibussowitsch { 344bbf5ea8SMatthew G. Knepley PetscInt starSize; 354bbf5ea8SMatthew G. Knepley PetscInt *star = NULL, si; 364bbf5ea8SMatthew G. Knepley 374bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 389566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(ht)); 394bbf5ea8SMatthew G. Knepley /* To start with, add the point we care about */ 409566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(ht, point)); 414bbf5ea8SMatthew G. Knepley /* Loop over all the points that this point connects to */ 429566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star)); 439566063dSJacob Faibussowitsch for (si = 0; si < starSize * 2; si += 2) PetscCall(PetscHSetIAdd(ht, star[si])); 449566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star)); 453ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 464bbf5ea8SMatthew G. Knepley } 474bbf5ea8SMatthew G. Knepley 48d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchConstruct_Vanka(void *vpatch, DM dm, PetscInt point, PetscHSetI ht) 49d71ae5a4SJacob Faibussowitsch { 504bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)vpatch; 514bbf5ea8SMatthew G. Knepley PetscInt starSize; 524bbf5ea8SMatthew G. Knepley PetscInt *star = NULL; 534bbf5ea8SMatthew G. Knepley PetscBool shouldIgnore = PETSC_FALSE; 544bbf5ea8SMatthew G. Knepley PetscInt cStart, cEnd, iStart, iEnd, si; 554bbf5ea8SMatthew G. Knepley 564bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 579566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(ht)); 584bbf5ea8SMatthew G. Knepley /* To start with, add the point we care about */ 599566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(ht, point)); 604bbf5ea8SMatthew G. Knepley /* Should we ignore any points of a certain dimension? */ 614bbf5ea8SMatthew G. Knepley if (patch->vankadim >= 0) { 624bbf5ea8SMatthew G. Knepley shouldIgnore = PETSC_TRUE; 639566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, patch->vankadim, &iStart, &iEnd)); 644bbf5ea8SMatthew G. Knepley } 659566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 664bbf5ea8SMatthew G. Knepley /* Loop over all the cells that this point connects to */ 679566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star)); 685f824522SMatthew G. Knepley for (si = 0; si < starSize * 2; si += 2) { 694bbf5ea8SMatthew G. Knepley const PetscInt cell = star[si]; 704bbf5ea8SMatthew G. Knepley PetscInt closureSize; 714bbf5ea8SMatthew G. Knepley PetscInt *closure = NULL, ci; 724bbf5ea8SMatthew G. Knepley 734bbf5ea8SMatthew G. Knepley if (cell < cStart || cell >= cEnd) continue; 744bbf5ea8SMatthew G. Knepley /* now loop over all entities in the closure of that cell */ 759566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure)); 765f824522SMatthew G. Knepley for (ci = 0; ci < closureSize * 2; ci += 2) { 774bbf5ea8SMatthew G. Knepley const PetscInt newpoint = closure[ci]; 784bbf5ea8SMatthew G. Knepley 794bbf5ea8SMatthew G. Knepley /* We've been told to ignore entities of this type.*/ 804bbf5ea8SMatthew G. Knepley if (shouldIgnore && newpoint >= iStart && newpoint < iEnd) continue; 819566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(ht, newpoint)); 824bbf5ea8SMatthew G. Knepley } 839566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure)); 844bbf5ea8SMatthew G. Knepley } 859566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star)); 863ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 874bbf5ea8SMatthew G. Knepley } 884bbf5ea8SMatthew G. Knepley 89d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchConstruct_Pardecomp(void *vpatch, DM dm, PetscInt point, PetscHSetI ht) 90d71ae5a4SJacob Faibussowitsch { 91b525f888SPatrick Farrell PC_PATCH *patch = (PC_PATCH *)vpatch; 920a390943SPatrick Farrell DMLabel ghost = NULL; 93eae3dc7dSJacob Faibussowitsch const PetscInt *leaves = NULL; 94eae3dc7dSJacob Faibussowitsch PetscInt nleaves = 0, pStart, pEnd, loc; 950a390943SPatrick Farrell PetscBool isFiredrake; 960a390943SPatrick Farrell PetscBool flg; 97b525f888SPatrick Farrell PetscInt starSize; 98b525f888SPatrick Farrell PetscInt *star = NULL; 9925fd193aSPatrick Farrell PetscInt opoint, overlapi; 1000a390943SPatrick Farrell 1010a390943SPatrick Farrell PetscFunctionBegin; 1029566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(ht)); 1030a390943SPatrick Farrell 1049566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 1050a390943SPatrick Farrell 1069566063dSJacob Faibussowitsch PetscCall(DMHasLabel(dm, "pyop2_ghost", &isFiredrake)); 1070a390943SPatrick Farrell if (isFiredrake) { 1089566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, "pyop2_ghost", &ghost)); 1099566063dSJacob Faibussowitsch PetscCall(DMLabelCreateIndex(ghost, pStart, pEnd)); 1100a390943SPatrick Farrell } else { 1110a390943SPatrick Farrell PetscSF sf; 1129566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm, &sf)); 1139566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL)); 1140a390943SPatrick Farrell nleaves = PetscMax(nleaves, 0); 1150a390943SPatrick Farrell } 1160a390943SPatrick Farrell 11725fd193aSPatrick Farrell for (opoint = pStart; opoint < pEnd; ++opoint) { 1189566063dSJacob Faibussowitsch if (ghost) PetscCall(DMLabelHasPoint(ghost, opoint, &flg)); 1199371c9d4SSatish Balay else { 1209371c9d4SSatish Balay PetscCall(PetscFindInt(opoint, nleaves, leaves, &loc)); 1219371c9d4SSatish Balay flg = loc >= 0 ? PETSC_TRUE : PETSC_FALSE; 1229371c9d4SSatish Balay } 1230a390943SPatrick Farrell /* Not an owned entity, don't make a cell patch. */ 1240a390943SPatrick Farrell if (flg) continue; 1259566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(ht, opoint)); 1260a390943SPatrick Farrell } 1270a390943SPatrick Farrell 128b525f888SPatrick Farrell /* Now build the overlap for the patch */ 12925fd193aSPatrick Farrell for (overlapi = 0; overlapi < patch->pardecomp_overlap; ++overlapi) { 130b525f888SPatrick Farrell PetscInt index = 0; 131b525f888SPatrick Farrell PetscInt *htpoints = NULL; 132b525f888SPatrick Farrell PetscInt htsize; 13325fd193aSPatrick Farrell PetscInt i; 134b525f888SPatrick Farrell 1359566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(ht, &htsize)); 1369566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(htsize, &htpoints)); 1379566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetElems(ht, &index, htpoints)); 138b525f888SPatrick Farrell 13925fd193aSPatrick Farrell for (i = 0; i < htsize; ++i) { 14025fd193aSPatrick Farrell PetscInt hpoint = htpoints[i]; 14125fd193aSPatrick Farrell PetscInt si; 142b525f888SPatrick Farrell 1439566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star)); 14425fd193aSPatrick Farrell for (si = 0; si < starSize * 2; si += 2) { 145b525f888SPatrick Farrell const PetscInt starp = star[si]; 146b525f888SPatrick Farrell PetscInt closureSize; 147b525f888SPatrick Farrell PetscInt *closure = NULL, ci; 148b525f888SPatrick Farrell 149b525f888SPatrick Farrell /* now loop over all entities in the closure of starp */ 1509566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure)); 15125fd193aSPatrick Farrell for (ci = 0; ci < closureSize * 2; ci += 2) { 152b525f888SPatrick Farrell const PetscInt closstarp = closure[ci]; 1539566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(ht, closstarp)); 154b525f888SPatrick Farrell } 1559566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure)); 156b525f888SPatrick Farrell } 1579566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star)); 158b525f888SPatrick Farrell } 1599566063dSJacob Faibussowitsch PetscCall(PetscFree(htpoints)); 160b525f888SPatrick Farrell } 161b525f888SPatrick Farrell 1623ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1630a390943SPatrick Farrell } 1640a390943SPatrick Farrell 1654bbf5ea8SMatthew G. Knepley /* The user's already set the patches in patch->userIS. Build the hash tables */ 166d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchConstruct_User(void *vpatch, DM dm, PetscInt point, PetscHSetI ht) 167d71ae5a4SJacob Faibussowitsch { 1684bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)vpatch; 1694bbf5ea8SMatthew G. Knepley IS patchis = patch->userIS[point]; 1704bbf5ea8SMatthew G. Knepley PetscInt n; 1714bbf5ea8SMatthew G. Knepley const PetscInt *patchdata; 1724bbf5ea8SMatthew G. Knepley PetscInt pStart, pEnd, i; 1734bbf5ea8SMatthew G. Knepley 1744bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 1759566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(ht)); 1769566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 1779566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(patchis, &n)); 1789566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patchis, &patchdata)); 1794bbf5ea8SMatthew G. Knepley for (i = 0; i < n; ++i) { 1804bbf5ea8SMatthew G. Knepley const PetscInt ownedpoint = patchdata[i]; 1814bbf5ea8SMatthew G. Knepley 1827a46b595SBarry Smith PetscCheck(ownedpoint >= pStart && ownedpoint < pEnd, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %" PetscInt_FMT " was not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", ownedpoint, pStart, pEnd); 1839566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(ht, ownedpoint)); 1844bbf5ea8SMatthew G. Knepley } 1859566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patchis, &patchdata)); 1863ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1874bbf5ea8SMatthew G. Knepley } 1884bbf5ea8SMatthew G. Knepley 189d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCreateDefaultSF_Private(PC pc, PetscInt n, const PetscSF *sf, const PetscInt *bs) 190d71ae5a4SJacob Faibussowitsch { 1914bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 1924bbf5ea8SMatthew G. Knepley PetscInt i; 1934bbf5ea8SMatthew G. Knepley 1944bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 1954bbf5ea8SMatthew G. Knepley if (n == 1 && bs[0] == 1) { 1961bb6d2a8SBarry Smith patch->sectionSF = sf[0]; 1979566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)patch->sectionSF)); 1984bbf5ea8SMatthew G. Knepley } else { 1994bbf5ea8SMatthew G. Knepley PetscInt allRoots = 0, allLeaves = 0; 2004bbf5ea8SMatthew G. Knepley PetscInt leafOffset = 0; 2014bbf5ea8SMatthew G. Knepley PetscInt *ilocal = NULL; 2024bbf5ea8SMatthew G. Knepley PetscSFNode *iremote = NULL; 2034bbf5ea8SMatthew G. Knepley PetscInt *remoteOffsets = NULL; 2044bbf5ea8SMatthew G. Knepley PetscInt index = 0; 2051b68eb51SMatthew G. Knepley PetscHMapI rankToIndex; 2064bbf5ea8SMatthew G. Knepley PetscInt numRanks = 0; 2074bbf5ea8SMatthew G. Knepley PetscSFNode *remote = NULL; 2084bbf5ea8SMatthew G. Knepley PetscSF rankSF; 2094bbf5ea8SMatthew G. Knepley PetscInt *ranks = NULL; 2104bbf5ea8SMatthew G. Knepley PetscInt *offsets = NULL; 2114bbf5ea8SMatthew G. Knepley MPI_Datatype contig; 2121b68eb51SMatthew G. Knepley PetscHSetI ranksUniq; 2134bbf5ea8SMatthew G. Knepley 2144bbf5ea8SMatthew G. Knepley /* First figure out how many dofs there are in the concatenated numbering. 215f1580f4eSBarry Smith allRoots: number of owned global dofs; 216f1580f4eSBarry Smith allLeaves: number of visible dofs (global + ghosted). 2174bbf5ea8SMatthew G. Knepley */ 2184bbf5ea8SMatthew G. Knepley for (i = 0; i < n; ++i) { 2194bbf5ea8SMatthew G. Knepley PetscInt nroots, nleaves; 2204bbf5ea8SMatthew G. Knepley 2219566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sf[i], &nroots, &nleaves, NULL, NULL)); 2224bbf5ea8SMatthew G. Knepley allRoots += nroots * bs[i]; 2234bbf5ea8SMatthew G. Knepley allLeaves += nleaves * bs[i]; 2244bbf5ea8SMatthew G. Knepley } 2259566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(allLeaves, &ilocal)); 2269566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(allLeaves, &iremote)); 2274bbf5ea8SMatthew G. Knepley /* Now build an SF that just contains process connectivity. */ 2289566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&ranksUniq)); 2294bbf5ea8SMatthew G. Knepley for (i = 0; i < n; ++i) { 2304bbf5ea8SMatthew G. Knepley const PetscMPIInt *ranks = NULL; 2314bbf5ea8SMatthew G. Knepley PetscInt nranks, j; 2324bbf5ea8SMatthew G. Knepley 2339566063dSJacob Faibussowitsch PetscCall(PetscSFSetUp(sf[i])); 2349566063dSJacob Faibussowitsch PetscCall(PetscSFGetRootRanks(sf[i], &nranks, &ranks, NULL, NULL, NULL)); 2354bbf5ea8SMatthew G. Knepley /* These are all the ranks who communicate with me. */ 23648a46eb9SPierre Jolivet for (j = 0; j < nranks; ++j) PetscCall(PetscHSetIAdd(ranksUniq, (PetscInt)ranks[j])); 2374bbf5ea8SMatthew G. Knepley } 2389566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(ranksUniq, &numRanks)); 2399566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numRanks, &remote)); 2409566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numRanks, &ranks)); 2419566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetElems(ranksUniq, &index, ranks)); 2424bbf5ea8SMatthew G. Knepley 2439566063dSJacob Faibussowitsch PetscCall(PetscHMapICreate(&rankToIndex)); 2444bbf5ea8SMatthew G. Knepley for (i = 0; i < numRanks; ++i) { 2454bbf5ea8SMatthew G. Knepley remote[i].rank = ranks[i]; 2464bbf5ea8SMatthew G. Knepley remote[i].index = 0; 2479566063dSJacob Faibussowitsch PetscCall(PetscHMapISet(rankToIndex, ranks[i], i)); 2484bbf5ea8SMatthew G. Knepley } 2499566063dSJacob Faibussowitsch PetscCall(PetscFree(ranks)); 2509566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&ranksUniq)); 2519566063dSJacob Faibussowitsch PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)pc), &rankSF)); 2529566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(rankSF, 1, numRanks, NULL, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER)); 2539566063dSJacob Faibussowitsch PetscCall(PetscSFSetUp(rankSF)); 254f1580f4eSBarry Smith /* OK, use it to communicate the root offset on the remote processes for each subspace. */ 2559566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(n, &offsets)); 2569566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(n * numRanks, &remoteOffsets)); 2574bbf5ea8SMatthew G. Knepley 2584bbf5ea8SMatthew G. Knepley offsets[0] = 0; 2594bbf5ea8SMatthew G. Knepley for (i = 1; i < n; ++i) { 2604bbf5ea8SMatthew G. Knepley PetscInt nroots; 2614bbf5ea8SMatthew G. Knepley 2629566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sf[i - 1], &nroots, NULL, NULL, NULL)); 2634bbf5ea8SMatthew G. Knepley offsets[i] = offsets[i - 1] + nroots * bs[i - 1]; 2644bbf5ea8SMatthew G. Knepley } 265f1580f4eSBarry Smith /* Offsets are the offsets on the current process of the global dof numbering for the subspaces. */ 2669566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_contiguous(n, MPIU_INT, &contig)); 2679566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_commit(&contig)); 2684bbf5ea8SMatthew G. Knepley 2699566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(rankSF, contig, offsets, remoteOffsets, MPI_REPLACE)); 2709566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(rankSF, contig, offsets, remoteOffsets, MPI_REPLACE)); 2719566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_free(&contig)); 2729566063dSJacob Faibussowitsch PetscCall(PetscFree(offsets)); 2739566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&rankSF)); 2744bbf5ea8SMatthew G. Knepley /* Now remoteOffsets contains the offsets on the remote 275f1580f4eSBarry Smith processes who communicate with me. So now we can 276f1580f4eSBarry Smith concatenate the list of SFs into a single one. */ 2774bbf5ea8SMatthew G. Knepley index = 0; 2784bbf5ea8SMatthew G. Knepley for (i = 0; i < n; ++i) { 2794bbf5ea8SMatthew G. Knepley const PetscSFNode *remote = NULL; 2804bbf5ea8SMatthew G. Knepley const PetscInt *local = NULL; 2814bbf5ea8SMatthew G. Knepley PetscInt nroots, nleaves, j; 2824bbf5ea8SMatthew G. Knepley 2839566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sf[i], &nroots, &nleaves, &local, &remote)); 2844bbf5ea8SMatthew G. Knepley for (j = 0; j < nleaves; ++j) { 2854bbf5ea8SMatthew G. Knepley PetscInt rank = remote[j].rank; 2864bbf5ea8SMatthew G. Knepley PetscInt idx, rootOffset, k; 2874bbf5ea8SMatthew G. Knepley 2889566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(rankToIndex, rank, &idx)); 28908401ef6SPierre Jolivet PetscCheck(idx != -1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Didn't find rank, huh?"); 2904bbf5ea8SMatthew G. Knepley /* Offset on given rank for ith subspace */ 2914bbf5ea8SMatthew G. Knepley rootOffset = remoteOffsets[n * idx + i]; 2924bbf5ea8SMatthew G. Knepley for (k = 0; k < bs[i]; ++k) { 29373ec7555SLawrence Mitchell ilocal[index] = (local ? local[j] : j) * bs[i] + k + leafOffset; 2944bbf5ea8SMatthew G. Knepley iremote[index].rank = remote[j].rank; 2954bbf5ea8SMatthew G. Knepley iremote[index].index = remote[j].index * bs[i] + k + rootOffset; 2964bbf5ea8SMatthew G. Knepley ++index; 2974bbf5ea8SMatthew G. Knepley } 2984bbf5ea8SMatthew G. Knepley } 2994bbf5ea8SMatthew G. Knepley leafOffset += nleaves * bs[i]; 3004bbf5ea8SMatthew G. Knepley } 3019566063dSJacob Faibussowitsch PetscCall(PetscHMapIDestroy(&rankToIndex)); 3029566063dSJacob Faibussowitsch PetscCall(PetscFree(remoteOffsets)); 3039566063dSJacob Faibussowitsch PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)pc), &patch->sectionSF)); 3049566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(patch->sectionSF, allRoots, allLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER)); 3054bbf5ea8SMatthew G. Knepley } 3063ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3074bbf5ea8SMatthew G. Knepley } 3084bbf5ea8SMatthew G. Knepley 3094bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 310ba38deedSJacob Faibussowitsch static PetscErrorCode PCPatchGetIgnoreDim(PC pc, PetscInt *dim) 311d71ae5a4SJacob Faibussowitsch { 3125f824522SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 3135f824522SMatthew G. Knepley PetscFunctionBegin; 3145f824522SMatthew G. Knepley *dim = patch->ignoredim; 3153ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3165f824522SMatthew G. Knepley } 3175f824522SMatthew G. Knepley 3185f824522SMatthew G. Knepley /* TODO: Docs */ 319d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetSaveOperators(PC pc, PetscBool flg) 320d71ae5a4SJacob Faibussowitsch { 3214bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 3224bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 3234bbf5ea8SMatthew G. Knepley patch->save_operators = flg; 3243ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3254bbf5ea8SMatthew G. Knepley } 3264bbf5ea8SMatthew G. Knepley 3274bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 328d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetSaveOperators(PC pc, PetscBool *flg) 329d71ae5a4SJacob Faibussowitsch { 3304bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 3314bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 3324bbf5ea8SMatthew G. Knepley *flg = patch->save_operators; 3333ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3344bbf5ea8SMatthew G. Knepley } 3354bbf5ea8SMatthew G. Knepley 3364bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 337d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetPrecomputeElementTensors(PC pc, PetscBool flg) 338d71ae5a4SJacob Faibussowitsch { 339fa84ea4cSLawrence Mitchell PC_PATCH *patch = (PC_PATCH *)pc->data; 340fa84ea4cSLawrence Mitchell PetscFunctionBegin; 341fa84ea4cSLawrence Mitchell patch->precomputeElementTensors = flg; 3423ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 343fa84ea4cSLawrence Mitchell } 344fa84ea4cSLawrence Mitchell 345fa84ea4cSLawrence Mitchell /* TODO: Docs */ 346d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetPrecomputeElementTensors(PC pc, PetscBool *flg) 347d71ae5a4SJacob Faibussowitsch { 348fa84ea4cSLawrence Mitchell PC_PATCH *patch = (PC_PATCH *)pc->data; 349fa84ea4cSLawrence Mitchell PetscFunctionBegin; 350fa84ea4cSLawrence Mitchell *flg = patch->precomputeElementTensors; 3513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 352fa84ea4cSLawrence Mitchell } 353fa84ea4cSLawrence Mitchell 354fa84ea4cSLawrence Mitchell /* TODO: Docs */ 355d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetPartitionOfUnity(PC pc, PetscBool flg) 356d71ae5a4SJacob Faibussowitsch { 3574bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 3584bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 3594bbf5ea8SMatthew G. Knepley patch->partition_of_unity = flg; 3603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3614bbf5ea8SMatthew G. Knepley } 3624bbf5ea8SMatthew G. Knepley 3634bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 364d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetPartitionOfUnity(PC pc, PetscBool *flg) 365d71ae5a4SJacob Faibussowitsch { 3664bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 3674bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 3684bbf5ea8SMatthew G. Knepley *flg = patch->partition_of_unity; 3693ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3704bbf5ea8SMatthew G. Knepley } 3714bbf5ea8SMatthew G. Knepley 3724bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 373ba38deedSJacob Faibussowitsch static PetscErrorCode PCPatchSetLocalComposition(PC pc, PCCompositeType type) 374d71ae5a4SJacob Faibussowitsch { 375c2e6f3c0SFlorian Wechsung PC_PATCH *patch = (PC_PATCH *)pc->data; 376c2e6f3c0SFlorian Wechsung PetscFunctionBegin; 3772472a847SBarry Smith PetscCheck(type == PC_COMPOSITE_ADDITIVE || type == PC_COMPOSITE_MULTIPLICATIVE, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Only supports additive or multiplicative as the local type"); 37861c4b389SFlorian Wechsung patch->local_composition_type = type; 3793ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 380c2e6f3c0SFlorian Wechsung } 381c2e6f3c0SFlorian Wechsung 382c2e6f3c0SFlorian Wechsung /* TODO: Docs */ 383d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetSubMatType(PC pc, MatType sub_mat_type) 384d71ae5a4SJacob Faibussowitsch { 3854bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 3864bbf5ea8SMatthew G. Knepley 3874bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 3889566063dSJacob Faibussowitsch if (patch->sub_mat_type) PetscCall(PetscFree(patch->sub_mat_type)); 3899566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(sub_mat_type, (char **)&patch->sub_mat_type)); 3903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3914bbf5ea8SMatthew G. Knepley } 3924bbf5ea8SMatthew G. Knepley 3934bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 394d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetSubMatType(PC pc, MatType *sub_mat_type) 395d71ae5a4SJacob Faibussowitsch { 3964bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 3974bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 3984bbf5ea8SMatthew G. Knepley *sub_mat_type = patch->sub_mat_type; 3993ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4004bbf5ea8SMatthew G. Knepley } 4014bbf5ea8SMatthew G. Knepley 4024bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 403d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetCellNumbering(PC pc, PetscSection cellNumbering) 404d71ae5a4SJacob Faibussowitsch { 4054bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 4064bbf5ea8SMatthew G. Knepley 4074bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 4084bbf5ea8SMatthew G. Knepley patch->cellNumbering = cellNumbering; 4099566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)cellNumbering)); 4103ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4114bbf5ea8SMatthew G. Knepley } 4124bbf5ea8SMatthew G. Knepley 4134bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 414d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetCellNumbering(PC pc, PetscSection *cellNumbering) 415d71ae5a4SJacob Faibussowitsch { 4164bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 4174bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 4184bbf5ea8SMatthew G. Knepley *cellNumbering = patch->cellNumbering; 4193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4204bbf5ea8SMatthew G. Knepley } 4214bbf5ea8SMatthew G. Knepley 4224bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 423d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetConstructType(PC pc, PCPatchConstructType ctype, PetscErrorCode (*func)(PC, PetscInt *, IS **, IS *, void *), void *ctx) 424d71ae5a4SJacob Faibussowitsch { 4254bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 4264bbf5ea8SMatthew G. Knepley 4274bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 4284bbf5ea8SMatthew G. Knepley patch->ctype = ctype; 4294bbf5ea8SMatthew G. Knepley switch (ctype) { 4304bbf5ea8SMatthew G. Knepley case PC_PATCH_STAR: 43140c17a03SPatrick Farrell patch->user_patches = PETSC_FALSE; 4324bbf5ea8SMatthew G. Knepley patch->patchconstructop = PCPatchConstruct_Star; 4334bbf5ea8SMatthew G. Knepley break; 4344bbf5ea8SMatthew G. Knepley case PC_PATCH_VANKA: 43540c17a03SPatrick Farrell patch->user_patches = PETSC_FALSE; 4364bbf5ea8SMatthew G. Knepley patch->patchconstructop = PCPatchConstruct_Vanka; 4374bbf5ea8SMatthew G. Knepley break; 438e5b9877fSPatrick Farrell case PC_PATCH_PARDECOMP: 4390a390943SPatrick Farrell patch->user_patches = PETSC_FALSE; 440e5b9877fSPatrick Farrell patch->patchconstructop = PCPatchConstruct_Pardecomp; 4410a390943SPatrick Farrell break; 4424bbf5ea8SMatthew G. Knepley case PC_PATCH_USER: 4434bbf5ea8SMatthew G. Knepley case PC_PATCH_PYTHON: 4444bbf5ea8SMatthew G. Knepley patch->user_patches = PETSC_TRUE; 4454bbf5ea8SMatthew G. Knepley patch->patchconstructop = PCPatchConstruct_User; 446bdd9e0cdSPatrick Farrell if (func) { 4474bbf5ea8SMatthew G. Knepley patch->userpatchconstructionop = func; 4484bbf5ea8SMatthew G. Knepley patch->userpatchconstructctx = ctx; 449bdd9e0cdSPatrick Farrell } 4504bbf5ea8SMatthew G. Knepley break; 451d71ae5a4SJacob Faibussowitsch default: 452d71ae5a4SJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_USER, "Unknown patch construction type %" PetscInt_FMT, (PetscInt)patch->ctype); 4534bbf5ea8SMatthew G. Knepley } 4543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4554bbf5ea8SMatthew G. Knepley } 4564bbf5ea8SMatthew G. Knepley 4574bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 458d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetConstructType(PC pc, PCPatchConstructType *ctype, PetscErrorCode (**func)(PC, PetscInt *, IS **, IS *, void *), void **ctx) 459d71ae5a4SJacob Faibussowitsch { 4604bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 4614bbf5ea8SMatthew G. Knepley 4624bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 4634bbf5ea8SMatthew G. Knepley *ctype = patch->ctype; 4644bbf5ea8SMatthew G. Knepley switch (patch->ctype) { 4654bbf5ea8SMatthew G. Knepley case PC_PATCH_STAR: 4664bbf5ea8SMatthew G. Knepley case PC_PATCH_VANKA: 467d71ae5a4SJacob Faibussowitsch case PC_PATCH_PARDECOMP: 468d71ae5a4SJacob Faibussowitsch break; 4694bbf5ea8SMatthew G. Knepley case PC_PATCH_USER: 4704bbf5ea8SMatthew G. Knepley case PC_PATCH_PYTHON: 4714bbf5ea8SMatthew G. Knepley *func = patch->userpatchconstructionop; 4724bbf5ea8SMatthew G. Knepley *ctx = patch->userpatchconstructctx; 4734bbf5ea8SMatthew G. Knepley break; 474d71ae5a4SJacob Faibussowitsch default: 475d71ae5a4SJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_USER, "Unknown patch construction type %" PetscInt_FMT, (PetscInt)patch->ctype); 4764bbf5ea8SMatthew G. Knepley } 4773ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4784bbf5ea8SMatthew G. Knepley } 4794bbf5ea8SMatthew G. Knepley 4804bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 481d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetDiscretisationInfo(PC pc, PetscInt nsubspaces, DM *dms, PetscInt *bs, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, const PetscInt *subspaceOffsets, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes) 482d71ae5a4SJacob Faibussowitsch { 4834bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 484b6bb21d1SLawrence Mitchell DM dm, plex; 4854bbf5ea8SMatthew G. Knepley PetscSF *sfs; 4865f824522SMatthew G. Knepley PetscInt cStart, cEnd, i, j; 4874bbf5ea8SMatthew G. Knepley 4884bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 4899566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm)); 4909566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex)); 491b6bb21d1SLawrence Mitchell dm = plex; 4929566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 4939566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nsubspaces, &sfs)); 4949566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nsubspaces, &patch->dofSection)); 4959566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nsubspaces, &patch->bs)); 4969566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nsubspaces, &patch->nodesPerCell)); 4979566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nsubspaces, &patch->cellNodeMap)); 4989566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nsubspaces + 1, &patch->subspaceOffsets)); 4994bbf5ea8SMatthew G. Knepley 5004bbf5ea8SMatthew G. Knepley patch->nsubspaces = nsubspaces; 5014bbf5ea8SMatthew G. Knepley patch->totalDofsPerCell = 0; 5024bbf5ea8SMatthew G. Knepley for (i = 0; i < nsubspaces; ++i) { 5039566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dms[i], &patch->dofSection[i])); 5049566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)patch->dofSection[i])); 5059566063dSJacob Faibussowitsch PetscCall(DMGetSectionSF(dms[i], &sfs[i])); 5064bbf5ea8SMatthew G. Knepley patch->bs[i] = bs[i]; 5074bbf5ea8SMatthew G. Knepley patch->nodesPerCell[i] = nodesPerCell[i]; 5084bbf5ea8SMatthew G. Knepley patch->totalDofsPerCell += nodesPerCell[i] * bs[i]; 5099566063dSJacob Faibussowitsch PetscCall(PetscMalloc1((cEnd - cStart) * nodesPerCell[i], &patch->cellNodeMap[i])); 51080e8a965SFlorian Wechsung for (j = 0; j < (cEnd - cStart) * nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j]; 5114bbf5ea8SMatthew G. Knepley patch->subspaceOffsets[i] = subspaceOffsets[i]; 5124bbf5ea8SMatthew G. Knepley } 5139566063dSJacob Faibussowitsch PetscCall(PCPatchCreateDefaultSF_Private(pc, nsubspaces, sfs, patch->bs)); 5149566063dSJacob Faibussowitsch PetscCall(PetscFree(sfs)); 5154bbf5ea8SMatthew G. Knepley 5164bbf5ea8SMatthew G. Knepley patch->subspaceOffsets[nsubspaces] = subspaceOffsets[nsubspaces]; 5179566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes)); 5189566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes)); 5199566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm)); 5203ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5214bbf5ea8SMatthew G. Knepley } 5224bbf5ea8SMatthew G. Knepley 5234bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 524ba38deedSJacob Faibussowitsch static PetscErrorCode PCPatchSetDiscretisationInfoCombined(PC pc, DM dm, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes) 525d71ae5a4SJacob Faibussowitsch { 5265f824522SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 5275f824522SMatthew G. Knepley PetscInt cStart, cEnd, i, j; 5285f824522SMatthew G. Knepley 5295f824522SMatthew G. Knepley PetscFunctionBegin; 5305f824522SMatthew G. Knepley patch->combined = PETSC_TRUE; 5319566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 5329566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &patch->nsubspaces)); 5339566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(patch->nsubspaces, &patch->dofSection)); 5349566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(patch->nsubspaces, &patch->bs)); 5359566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(patch->nsubspaces, &patch->nodesPerCell)); 5369566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(patch->nsubspaces, &patch->cellNodeMap)); 5379566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(patch->nsubspaces + 1, &patch->subspaceOffsets)); 5389566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, &patch->dofSection[0])); 5399566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)patch->dofSection[0])); 5409566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(patch->dofSection[0], &patch->subspaceOffsets[patch->nsubspaces])); 5415f824522SMatthew G. Knepley patch->totalDofsPerCell = 0; 5425f824522SMatthew G. Knepley for (i = 0; i < patch->nsubspaces; ++i) { 5435f824522SMatthew G. Knepley patch->bs[i] = 1; 5445f824522SMatthew G. Knepley patch->nodesPerCell[i] = nodesPerCell[i]; 5455f824522SMatthew G. Knepley patch->totalDofsPerCell += nodesPerCell[i]; 5469566063dSJacob Faibussowitsch PetscCall(PetscMalloc1((cEnd - cStart) * nodesPerCell[i], &patch->cellNodeMap[i])); 5475f824522SMatthew G. Knepley for (j = 0; j < (cEnd - cStart) * nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j]; 5485f824522SMatthew G. Knepley } 5499566063dSJacob Faibussowitsch PetscCall(DMGetSectionSF(dm, &patch->sectionSF)); 5509566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)patch->sectionSF)); 5519566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes)); 5529566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes)); 5533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5545f824522SMatthew G. Knepley } 5555f824522SMatthew G. Knepley 5565f824522SMatthew G. Knepley /*@C 55704c3f3b8SBarry Smith PCPatchSetComputeFunction - Set the callback function used to compute patch residuals 55892d50984SMatthew G. Knepley 55920f4b53cSBarry Smith Logically Collective 56099b7e5c6SPatrick Farrell 56192d50984SMatthew G. Knepley Input Parameters: 56220f4b53cSBarry Smith + pc - The `PC` 56304c3f3b8SBarry Smith . func - The callback function 56492d50984SMatthew G. Knepley - ctx - The user context 56592d50984SMatthew G. Knepley 56620f4b53cSBarry Smith Calling sequence of `func`: 56720f4b53cSBarry Smith + pc - The `PC` 5687a50e09dSPatrick Farrell . point - The point 5697a50e09dSPatrick Farrell . x - The input solution (not used in linear problems) 5707a50e09dSPatrick Farrell . f - The patch residual vector 5717a50e09dSPatrick Farrell . cellIS - An array of the cell numbers 57220f4b53cSBarry Smith . n - The size of `dofsArray` 5737a50e09dSPatrick Farrell . dofsArray - The dofmap for the dofs to be solved for 5747a50e09dSPatrick Farrell . dofsArrayWithAll - The dofmap for all dofs on the patch 5757a50e09dSPatrick Farrell - ctx - The user context 5767a50e09dSPatrick Farrell 57792d50984SMatthew G. Knepley Level: advanced 57892d50984SMatthew G. Knepley 579f1580f4eSBarry Smith Note: 58004c3f3b8SBarry Smith The entries of `f` (the output residual vector) have been set to zero before the call. 58192d50984SMatthew G. Knepley 582*562efe2eSBarry Smith .seealso: [](ch_ksp), `PCPatchSetComputeOperator()`, `PCPatchGetComputeOperator()`, `PCPatchSetDiscretisationInfo()`, `PCPatchSetComputeFunctionInteriorFacets()` 58392d50984SMatthew G. Knepley @*/ 58404c3f3b8SBarry Smith PetscErrorCode PCPatchSetComputeFunction(PC pc, PetscErrorCode (*func)(PC pc, PetscInt point, Vec x, Vec f, IS cellIS, PetscInt n, const PetscInt *dofsArray, const PetscInt *dofsArrayWithAll, void *ctx), void *ctx) 585d71ae5a4SJacob Faibussowitsch { 58692d50984SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 58792d50984SMatthew G. Knepley 58892d50984SMatthew G. Knepley PetscFunctionBegin; 58992d50984SMatthew G. Knepley patch->usercomputef = func; 59092d50984SMatthew G. Knepley patch->usercomputefctx = ctx; 5913ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 59292d50984SMatthew G. Knepley } 59392d50984SMatthew G. Knepley 59492d50984SMatthew G. Knepley /*@C 59504c3f3b8SBarry Smith PCPatchSetComputeFunctionInteriorFacets - Set the callback function used to compute facet integrals for patch residuals 59659109abcSLawrence Mitchell 59720f4b53cSBarry Smith Logically Collective 5987a50e09dSPatrick Farrell 59959109abcSLawrence Mitchell Input Parameters: 60020f4b53cSBarry Smith + pc - The `PC` 60104c3f3b8SBarry Smith . func - The callback function 60259109abcSLawrence Mitchell - ctx - The user context 60359109abcSLawrence Mitchell 60420f4b53cSBarry Smith Calling sequence of `func`: 60520f4b53cSBarry Smith + pc - The `PC` 6067a50e09dSPatrick Farrell . point - The point 6077a50e09dSPatrick Farrell . x - The input solution (not used in linear problems) 6087a50e09dSPatrick Farrell . f - The patch residual vector 6097a50e09dSPatrick Farrell . facetIS - An array of the facet numbers 61020f4b53cSBarry Smith . n - The size of `dofsArray` 6117a50e09dSPatrick Farrell . dofsArray - The dofmap for the dofs to be solved for 6127a50e09dSPatrick Farrell . dofsArrayWithAll - The dofmap for all dofs on the patch 6137a50e09dSPatrick Farrell - ctx - The user context 6147a50e09dSPatrick Farrell 61559109abcSLawrence Mitchell Level: advanced 61659109abcSLawrence Mitchell 617f1580f4eSBarry Smith Note: 61804c3f3b8SBarry Smith The entries of `f` (the output residual vector) have been set to zero before the call. 61959109abcSLawrence Mitchell 620*562efe2eSBarry Smith .seealso: [](ch_ksp), `PCPatchSetComputeOperator()`, `PCPatchGetComputeOperator()`, `PCPatchSetDiscretisationInfo()`, `PCPatchSetComputeFunction()` 62159109abcSLawrence Mitchell @*/ 62204c3f3b8SBarry Smith PetscErrorCode PCPatchSetComputeFunctionInteriorFacets(PC pc, PetscErrorCode (*func)(PC pc, PetscInt point, Vec x, Vec f, IS facetIS, PetscInt n, const PetscInt *dofsArray, const PetscInt *dofsArrayWithAll, void *ctx), void *ctx) 623d71ae5a4SJacob Faibussowitsch { 62459109abcSLawrence Mitchell PC_PATCH *patch = (PC_PATCH *)pc->data; 62559109abcSLawrence Mitchell 62659109abcSLawrence Mitchell PetscFunctionBegin; 62759109abcSLawrence Mitchell patch->usercomputefintfacet = func; 62859109abcSLawrence Mitchell patch->usercomputefintfacetctx = ctx; 6293ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 63059109abcSLawrence Mitchell } 63159109abcSLawrence Mitchell 63259109abcSLawrence Mitchell /*@C 63304c3f3b8SBarry Smith PCPatchSetComputeOperator - Set the callback function used to compute patch matrices 6345f824522SMatthew G. Knepley 63520f4b53cSBarry Smith Logically Collective 6367a50e09dSPatrick Farrell 6375f824522SMatthew G. Knepley Input Parameters: 63820f4b53cSBarry Smith + pc - The `PC` 63904c3f3b8SBarry Smith . func - The callback function 6405f824522SMatthew G. Knepley - ctx - The user context 6415f824522SMatthew G. Knepley 64220f4b53cSBarry Smith Calling sequence of `func`: 64320f4b53cSBarry Smith + pc - The `PC` 6447a50e09dSPatrick Farrell . point - The point 6457a50e09dSPatrick Farrell . x - The input solution (not used in linear problems) 6467a50e09dSPatrick Farrell . mat - The patch matrix 64704c3f3b8SBarry Smith . facetIS - An array of the cell numbers 64820f4b53cSBarry Smith . n - The size of `dofsArray` 6497a50e09dSPatrick Farrell . dofsArray - The dofmap for the dofs to be solved for 6507a50e09dSPatrick Farrell . dofsArrayWithAll - The dofmap for all dofs on the patch 6517a50e09dSPatrick Farrell - ctx - The user context 6527a50e09dSPatrick Farrell 6535f824522SMatthew G. Knepley Level: advanced 6545f824522SMatthew G. Knepley 655f1580f4eSBarry Smith Note: 6567a50e09dSPatrick Farrell The matrix entries have been set to zero before the call. 6575f824522SMatthew G. Knepley 658*562efe2eSBarry Smith .seealso: [](ch_ksp), `PCPatchGetComputeOperator()`, `PCPatchSetComputeFunction()`, `PCPatchSetDiscretisationInfo()` 6595f824522SMatthew G. Knepley @*/ 66004c3f3b8SBarry Smith PetscErrorCode PCPatchSetComputeOperator(PC pc, PetscErrorCode (*func)(PC pc, PetscInt point, Vec x, Mat mat, IS facetIS, PetscInt n, const PetscInt *dofsArray, const PetscInt *dofsArrayWithAll, void *ctx), void *ctx) 661d71ae5a4SJacob Faibussowitsch { 6624bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 6634bbf5ea8SMatthew G. Knepley 6644bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 6654bbf5ea8SMatthew G. Knepley patch->usercomputeop = func; 666723f9013SMatthew G. Knepley patch->usercomputeopctx = ctx; 6673ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6684bbf5ea8SMatthew G. Knepley } 6694bbf5ea8SMatthew G. Knepley 67059109abcSLawrence Mitchell /*@C 67159109abcSLawrence Mitchell 67204c3f3b8SBarry Smith PCPatchSetComputeOperatorInteriorFacets - Set the callback function used to compute facet integrals for patch matrices 67359109abcSLawrence Mitchell 67420f4b53cSBarry Smith Logically Collective 67599b7e5c6SPatrick Farrell 67659109abcSLawrence Mitchell Input Parameters: 67720f4b53cSBarry Smith + pc - The `PC` 67804c3f3b8SBarry Smith . func - The callback function 67959109abcSLawrence Mitchell - ctx - The user context 68059109abcSLawrence Mitchell 68120f4b53cSBarry Smith Calling sequence of `func`: 68220f4b53cSBarry Smith + pc - The `PC` 6837a50e09dSPatrick Farrell . point - The point 6847a50e09dSPatrick Farrell . x - The input solution (not used in linear problems) 6857a50e09dSPatrick Farrell . mat - The patch matrix 6867a50e09dSPatrick Farrell . facetIS - An array of the facet numbers 68720f4b53cSBarry Smith . n - The size of `dofsArray` 6887a50e09dSPatrick Farrell . dofsArray - The dofmap for the dofs to be solved for 6897a50e09dSPatrick Farrell . dofsArrayWithAll - The dofmap for all dofs on the patch 6907a50e09dSPatrick Farrell - ctx - The user context 6917a50e09dSPatrick Farrell 69259109abcSLawrence Mitchell Level: advanced 69359109abcSLawrence Mitchell 694f1580f4eSBarry Smith Note: 6957a50e09dSPatrick Farrell The matrix entries have been set to zero before the call. 69659109abcSLawrence Mitchell 697*562efe2eSBarry Smith .seealso: [](ch_ksp), `PCPatchGetComputeOperator()`, `PCPatchSetComputeFunction()`, `PCPatchSetDiscretisationInfo()` 69859109abcSLawrence Mitchell @*/ 69904c3f3b8SBarry Smith PetscErrorCode PCPatchSetComputeOperatorInteriorFacets(PC pc, PetscErrorCode (*func)(PC pc, PetscInt point, Vec x, Mat mat, IS facetIS, PetscInt n, const PetscInt *dofsArray, const PetscInt *dofsArrayWithAll, void *ctx), void *ctx) 700d71ae5a4SJacob Faibussowitsch { 70159109abcSLawrence Mitchell PC_PATCH *patch = (PC_PATCH *)pc->data; 70259109abcSLawrence Mitchell 70359109abcSLawrence Mitchell PetscFunctionBegin; 70459109abcSLawrence Mitchell patch->usercomputeopintfacet = func; 70559109abcSLawrence Mitchell patch->usercomputeopintfacetctx = ctx; 7063ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 70759109abcSLawrence Mitchell } 70859109abcSLawrence Mitchell 7094bbf5ea8SMatthew G. Knepley /* On entry, ht contains the topological entities whose dofs we are responsible for solving for; 7104bbf5ea8SMatthew G. Knepley on exit, cht contains all the topological entities we need to compute their residuals. 7114bbf5ea8SMatthew G. Knepley In full generality this should incorporate knowledge of the sparsity pattern of the matrix; 7124bbf5ea8SMatthew G. Knepley here we assume a standard FE sparsity pattern.*/ 7134bbf5ea8SMatthew G. Knepley /* TODO: Use DMPlexGetAdjacency() */ 714d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCompleteCellPatch(PC pc, PetscHSetI ht, PetscHSetI cht) 715d71ae5a4SJacob Faibussowitsch { 716b6bb21d1SLawrence Mitchell DM dm, plex; 717bc7fa33aSFlorian Wechsung PC_PATCH *patch = (PC_PATCH *)pc->data; 7181b68eb51SMatthew G. Knepley PetscHashIter hi; 7194bbf5ea8SMatthew G. Knepley PetscInt point; 7204bbf5ea8SMatthew G. Knepley PetscInt *star = NULL, *closure = NULL; 7214c954380SMatthew G. Knepley PetscInt ignoredim, iStart = 0, iEnd = -1, starSize, closureSize, si, ci; 722bc7fa33aSFlorian Wechsung PetscInt *fStar = NULL, *fClosure = NULL; 723bc7fa33aSFlorian Wechsung PetscInt fBegin, fEnd, fsi, fci, fStarSize, fClosureSize; 7244bbf5ea8SMatthew G. Knepley 7254bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 7269566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm)); 7279566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex)); 728b6bb21d1SLawrence Mitchell dm = plex; 7299566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fBegin, &fEnd)); 7309566063dSJacob Faibussowitsch PetscCall(PCPatchGetIgnoreDim(pc, &ignoredim)); 7319566063dSJacob Faibussowitsch if (ignoredim >= 0) PetscCall(DMPlexGetDepthStratum(dm, ignoredim, &iStart, &iEnd)); 7329566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(cht)); 7331b68eb51SMatthew G. Knepley PetscHashIterBegin(ht, hi); 7341b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(ht, hi)) { 7351b68eb51SMatthew G. Knepley PetscHashIterGetKey(ht, hi, point); 7361b68eb51SMatthew G. Knepley PetscHashIterNext(ht, hi); 7374bbf5ea8SMatthew G. Knepley 7384bbf5ea8SMatthew G. Knepley /* Loop over all the cells that this point connects to */ 7399566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star)); 7405f824522SMatthew G. Knepley for (si = 0; si < starSize * 2; si += 2) { 7414c954380SMatthew G. Knepley const PetscInt ownedpoint = star[si]; 7425f824522SMatthew G. Knepley /* TODO Check for point in cht before running through closure again */ 7434bbf5ea8SMatthew G. Knepley /* now loop over all entities in the closure of that cell */ 7449566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, ownedpoint, PETSC_TRUE, &closureSize, &closure)); 7455f824522SMatthew G. Knepley for (ci = 0; ci < closureSize * 2; ci += 2) { 7464c954380SMatthew G. Knepley const PetscInt seenpoint = closure[ci]; 7475f824522SMatthew G. Knepley if (ignoredim >= 0 && seenpoint >= iStart && seenpoint < iEnd) continue; 7489566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(cht, seenpoint)); 749bc7fa33aSFlorian Wechsung /* Facet integrals couple dofs across facets, so in that case for each of 750f1580f4eSBarry Smith the facets we need to add all dofs on the other side of the facet to 751f1580f4eSBarry Smith the seen dofs. */ 752bc7fa33aSFlorian Wechsung if (patch->usercomputeopintfacet) { 753bc7fa33aSFlorian Wechsung if (fBegin <= seenpoint && seenpoint < fEnd) { 7549566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, seenpoint, PETSC_FALSE, &fStarSize, &fStar)); 755bc7fa33aSFlorian Wechsung for (fsi = 0; fsi < fStarSize * 2; fsi += 2) { 7569566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, &fClosureSize, &fClosure)); 75748a46eb9SPierre Jolivet for (fci = 0; fci < fClosureSize * 2; fci += 2) PetscCall(PetscHSetIAdd(cht, fClosure[fci])); 7589566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, NULL, &fClosure)); 759bc7fa33aSFlorian Wechsung } 7609566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, seenpoint, PETSC_FALSE, NULL, &fStar)); 761bc7fa33aSFlorian Wechsung } 762bc7fa33aSFlorian Wechsung } 7634bbf5ea8SMatthew G. Knepley } 7649566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, ownedpoint, PETSC_TRUE, NULL, &closure)); 7654bbf5ea8SMatthew G. Knepley } 7669566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, NULL, &star)); 7674bbf5ea8SMatthew G. Knepley } 7689566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm)); 7693ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7705f824522SMatthew G. Knepley } 7715f824522SMatthew G. Knepley 772d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchGetGlobalDofs(PC pc, PetscSection dofSection[], PetscInt f, PetscBool combined, PetscInt p, PetscInt *dof, PetscInt *off) 773d71ae5a4SJacob Faibussowitsch { 7745f824522SMatthew G. Knepley PetscFunctionBegin; 7755f824522SMatthew G. Knepley if (combined) { 7765f824522SMatthew G. Knepley if (f < 0) { 7779566063dSJacob Faibussowitsch if (dof) PetscCall(PetscSectionGetDof(dofSection[0], p, dof)); 7789566063dSJacob Faibussowitsch if (off) PetscCall(PetscSectionGetOffset(dofSection[0], p, off)); 7795f824522SMatthew G. Knepley } else { 7809566063dSJacob Faibussowitsch if (dof) PetscCall(PetscSectionGetFieldDof(dofSection[0], p, f, dof)); 7819566063dSJacob Faibussowitsch if (off) PetscCall(PetscSectionGetFieldOffset(dofSection[0], p, f, off)); 7825f824522SMatthew G. Knepley } 7835f824522SMatthew G. Knepley } else { 7845f824522SMatthew G. Knepley if (f < 0) { 7855f824522SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 7865f824522SMatthew G. Knepley PetscInt fdof, g; 7875f824522SMatthew G. Knepley 7885f824522SMatthew G. Knepley if (dof) { 7895f824522SMatthew G. Knepley *dof = 0; 7905f824522SMatthew G. Knepley for (g = 0; g < patch->nsubspaces; ++g) { 7919566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(dofSection[g], p, &fdof)); 7925f824522SMatthew G. Knepley *dof += fdof; 7935f824522SMatthew G. Knepley } 7945f824522SMatthew G. Knepley } 795624e31c3SLawrence Mitchell if (off) { 796624e31c3SLawrence Mitchell *off = 0; 797624e31c3SLawrence Mitchell for (g = 0; g < patch->nsubspaces; ++g) { 7989566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(dofSection[g], p, &fdof)); 799624e31c3SLawrence Mitchell *off += fdof; 800624e31c3SLawrence Mitchell } 801624e31c3SLawrence Mitchell } 8025f824522SMatthew G. Knepley } else { 8039566063dSJacob Faibussowitsch if (dof) PetscCall(PetscSectionGetDof(dofSection[f], p, dof)); 8049566063dSJacob Faibussowitsch if (off) PetscCall(PetscSectionGetOffset(dofSection[f], p, off)); 8055f824522SMatthew G. Knepley } 8065f824522SMatthew G. Knepley } 8073ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 8084bbf5ea8SMatthew G. Knepley } 8094bbf5ea8SMatthew G. Knepley 8104bbf5ea8SMatthew G. Knepley /* Given a hash table with a set of topological entities (pts), compute the degrees of 8114bbf5ea8SMatthew G. Knepley freedom in global concatenated numbering on those entities. 8124bbf5ea8SMatthew G. Knepley For Vanka smoothing, this needs to do something special: ignore dofs of the 8134bbf5ea8SMatthew G. Knepley constraint subspace on entities that aren't the base entity we're building the patch 8144bbf5ea8SMatthew G. Knepley around. */ 815d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchGetPointDofs(PC pc, PetscHSetI pts, PetscHSetI dofs, PetscInt base, PetscHSetI *subspaces_to_exclude) 816d71ae5a4SJacob Faibussowitsch { 8175f824522SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 8181b68eb51SMatthew G. Knepley PetscHashIter hi; 8194bbf5ea8SMatthew G. Knepley PetscInt ldof, loff; 8204bbf5ea8SMatthew G. Knepley PetscInt k, p; 8214bbf5ea8SMatthew G. Knepley 8224bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 8239566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(dofs)); 8244bbf5ea8SMatthew G. Knepley for (k = 0; k < patch->nsubspaces; ++k) { 8254bbf5ea8SMatthew G. Knepley PetscInt subspaceOffset = patch->subspaceOffsets[k]; 8264bbf5ea8SMatthew G. Knepley PetscInt bs = patch->bs[k]; 8274bbf5ea8SMatthew G. Knepley PetscInt j, l; 8284bbf5ea8SMatthew G. Knepley 829e4c66b91SPatrick Farrell if (subspaces_to_exclude != NULL) { 830e4c66b91SPatrick Farrell PetscBool should_exclude_k = PETSC_FALSE; 8319566063dSJacob Faibussowitsch PetscCall(PetscHSetIHas(*subspaces_to_exclude, k, &should_exclude_k)); 832e4c66b91SPatrick Farrell if (should_exclude_k) { 8334bbf5ea8SMatthew G. Knepley /* only get this subspace dofs at the base entity, not any others */ 8349566063dSJacob Faibussowitsch PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, base, &ldof, &loff)); 8354bbf5ea8SMatthew G. Knepley if (0 == ldof) continue; 8364bbf5ea8SMatthew G. Knepley for (j = loff; j < ldof + loff; ++j) { 8374bbf5ea8SMatthew G. Knepley for (l = 0; l < bs; ++l) { 8384bbf5ea8SMatthew G. Knepley PetscInt dof = bs * j + l + subspaceOffset; 8399566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(dofs, dof)); 8404bbf5ea8SMatthew G. Knepley } 8414bbf5ea8SMatthew G. Knepley } 8424bbf5ea8SMatthew G. Knepley continue; /* skip the other dofs of this subspace */ 8434bbf5ea8SMatthew G. Knepley } 844e4c66b91SPatrick Farrell } 8454bbf5ea8SMatthew G. Knepley 8461b68eb51SMatthew G. Knepley PetscHashIterBegin(pts, hi); 8471b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(pts, hi)) { 8481b68eb51SMatthew G. Knepley PetscHashIterGetKey(pts, hi, p); 8491b68eb51SMatthew G. Knepley PetscHashIterNext(pts, hi); 8509566063dSJacob Faibussowitsch PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, p, &ldof, &loff)); 8514bbf5ea8SMatthew G. Knepley if (0 == ldof) continue; 8524bbf5ea8SMatthew G. Knepley for (j = loff; j < ldof + loff; ++j) { 8534bbf5ea8SMatthew G. Knepley for (l = 0; l < bs; ++l) { 8544bbf5ea8SMatthew G. Knepley PetscInt dof = bs * j + l + subspaceOffset; 8559566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(dofs, dof)); 8564bbf5ea8SMatthew G. Knepley } 8574bbf5ea8SMatthew G. Knepley } 8584bbf5ea8SMatthew G. Knepley } 8594bbf5ea8SMatthew G. Knepley } 8603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 8614bbf5ea8SMatthew G. Knepley } 8624bbf5ea8SMatthew G. Knepley 8634bbf5ea8SMatthew G. Knepley /* Given two hash tables A and B, compute the keys in B that are not in A, and put them in C */ 864d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchComputeSetDifference_Private(PetscHSetI A, PetscHSetI B, PetscHSetI C) 865d71ae5a4SJacob Faibussowitsch { 8661b68eb51SMatthew G. Knepley PetscHashIter hi; 8671b68eb51SMatthew G. Knepley PetscInt key; 8684bbf5ea8SMatthew G. Knepley PetscBool flg; 8694bbf5ea8SMatthew G. Knepley 8704bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 8719566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(C)); 8721b68eb51SMatthew G. Knepley PetscHashIterBegin(B, hi); 8731b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(B, hi)) { 8741b68eb51SMatthew G. Knepley PetscHashIterGetKey(B, hi, key); 8751b68eb51SMatthew G. Knepley PetscHashIterNext(B, hi); 8769566063dSJacob Faibussowitsch PetscCall(PetscHSetIHas(A, key, &flg)); 8779566063dSJacob Faibussowitsch if (!flg) PetscCall(PetscHSetIAdd(C, key)); 8784bbf5ea8SMatthew G. Knepley } 8793ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 8804bbf5ea8SMatthew G. Knepley } 8814bbf5ea8SMatthew G. Knepley 88204c3f3b8SBarry Smith // PetscClangLinter pragma disable: -fdoc-sowing-chars 8834bbf5ea8SMatthew G. Knepley /* 884f1580f4eSBarry Smith PCPatchCreateCellPatches - create patches. 885f1580f4eSBarry Smith 886f1580f4eSBarry Smith Input Parameter: 887f1580f4eSBarry Smith . dm - The DMPlex object defining the mesh 888f1580f4eSBarry Smith 889f1580f4eSBarry Smith Output Parameters: 890f1580f4eSBarry Smith + cellCounts - Section with counts of cells around each vertex 891f1580f4eSBarry Smith . cells - IS of the cell point indices of cells in each patch 892f1580f4eSBarry Smith . pointCounts - Section with counts of cells around each vertex 893f1580f4eSBarry Smith - point - IS of the cell point indices of cells in each patch 8944bbf5ea8SMatthew G. Knepley */ 895d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCreateCellPatches(PC pc) 896d71ae5a4SJacob Faibussowitsch { 8974bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 8985f824522SMatthew G. Knepley DMLabel ghost = NULL; 8994bbf5ea8SMatthew G. Knepley DM dm, plex; 90076ce8f1aSJose E. Roman PetscHSetI ht = NULL, cht = NULL; 9010e126c0bSLawrence Mitchell PetscSection cellCounts, pointCounts, intFacetCounts, extFacetCounts; 902eb62eeaaSLawrence Mitchell PetscInt *cellsArray, *pointsArray, *intFacetsArray, *extFacetsArray, *intFacetsToPatchCell; 9030e126c0bSLawrence Mitchell PetscInt numCells, numPoints, numIntFacets, numExtFacets; 9045f824522SMatthew G. Knepley const PetscInt *leaves; 90533cbca70SPatrick Farrell PetscInt nleaves, pStart, pEnd, cStart, cEnd, vStart, vEnd, fStart, fEnd, v; 9065f824522SMatthew G. Knepley PetscBool isFiredrake; 9074bbf5ea8SMatthew G. Knepley 9084bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 9094bbf5ea8SMatthew G. Knepley /* Used to keep track of the cells in the patch. */ 9109566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&ht)); 9119566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&cht)); 9124bbf5ea8SMatthew G. Knepley 9139566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm)); 91428b400f6SJacob Faibussowitsch PetscCheck(dm, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "DM not yet set on patch PC"); 9159566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex)); 916b6bb21d1SLawrence Mitchell dm = plex; 9179566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 9189566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 9194bbf5ea8SMatthew G. Knepley 9204bbf5ea8SMatthew G. Knepley if (patch->user_patches) { 9219566063dSJacob Faibussowitsch PetscCall(patch->userpatchconstructionop(pc, &patch->npatch, &patch->userIS, &patch->iterationSet, patch->userpatchconstructctx)); 9229371c9d4SSatish Balay vStart = 0; 9239371c9d4SSatish Balay vEnd = patch->npatch; 924e5b9877fSPatrick Farrell } else if (patch->ctype == PC_PATCH_PARDECOMP) { 9259371c9d4SSatish Balay vStart = 0; 9269371c9d4SSatish Balay vEnd = 1; 9275f824522SMatthew G. Knepley } else if (patch->codim < 0) { 9289566063dSJacob Faibussowitsch if (patch->dim < 0) PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd)); 9299566063dSJacob Faibussowitsch else PetscCall(DMPlexGetDepthStratum(dm, patch->dim, &vStart, &vEnd)); 9309566063dSJacob Faibussowitsch } else PetscCall(DMPlexGetHeightStratum(dm, patch->codim, &vStart, &vEnd)); 9315f824522SMatthew G. Knepley patch->npatch = vEnd - vStart; 9324bbf5ea8SMatthew G. Knepley 9334bbf5ea8SMatthew G. Knepley /* These labels mark the owned points. We only create patches around points that this process owns. */ 9349566063dSJacob Faibussowitsch PetscCall(DMHasLabel(dm, "pyop2_ghost", &isFiredrake)); 9355f824522SMatthew G. Knepley if (isFiredrake) { 9369566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, "pyop2_ghost", &ghost)); 9379566063dSJacob Faibussowitsch PetscCall(DMLabelCreateIndex(ghost, pStart, pEnd)); 9385f824522SMatthew G. Knepley } else { 9395f824522SMatthew G. Knepley PetscSF sf; 9405f824522SMatthew G. Knepley 9419566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm, &sf)); 9429566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL)); 9435f824522SMatthew G. Knepley nleaves = PetscMax(nleaves, 0); 9445f824522SMatthew G. Knepley } 9454bbf5ea8SMatthew G. Knepley 9469566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->cellCounts)); 9479566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->cellCounts, "Patch Cell Layout")); 9484bbf5ea8SMatthew G. Knepley cellCounts = patch->cellCounts; 9499566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(cellCounts, vStart, vEnd)); 9509566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->pointCounts)); 9519566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->pointCounts, "Patch Point Layout")); 9525f824522SMatthew G. Knepley pointCounts = patch->pointCounts; 9539566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(pointCounts, vStart, vEnd)); 9549566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->extFacetCounts)); 9559566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->extFacetCounts, "Patch Exterior Facet Layout")); 9560e126c0bSLawrence Mitchell extFacetCounts = patch->extFacetCounts; 9579566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(extFacetCounts, vStart, vEnd)); 9589566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->intFacetCounts)); 9599566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->intFacetCounts, "Patch Interior Facet Layout")); 9600e126c0bSLawrence Mitchell intFacetCounts = patch->intFacetCounts; 9619566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(intFacetCounts, vStart, vEnd)); 9625f824522SMatthew G. Knepley /* Count cells and points in the patch surrounding each entity */ 9639566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd)); 9644bbf5ea8SMatthew G. Knepley for (v = vStart; v < vEnd; ++v) { 9651b68eb51SMatthew G. Knepley PetscHashIter hi; 9665f824522SMatthew G. Knepley PetscInt chtSize, loc = -1; 9675f824522SMatthew G. Knepley PetscBool flg; 9684bbf5ea8SMatthew G. Knepley 969b525f888SPatrick Farrell if (!patch->user_patches && patch->ctype != PC_PATCH_PARDECOMP) { 9709566063dSJacob Faibussowitsch if (ghost) PetscCall(DMLabelHasPoint(ghost, v, &flg)); 9719371c9d4SSatish Balay else { 9729371c9d4SSatish Balay PetscCall(PetscFindInt(v, nleaves, leaves, &loc)); 9739371c9d4SSatish Balay flg = loc >= 0 ? PETSC_TRUE : PETSC_FALSE; 9749371c9d4SSatish Balay } 9754bbf5ea8SMatthew G. Knepley /* Not an owned entity, don't make a cell patch. */ 9764bbf5ea8SMatthew G. Knepley if (flg) continue; 9774bbf5ea8SMatthew G. Knepley } 9784bbf5ea8SMatthew G. Knepley 9799566063dSJacob Faibussowitsch PetscCall(patch->patchconstructop((void *)patch, dm, v, ht)); 9809566063dSJacob Faibussowitsch PetscCall(PCPatchCompleteCellPatch(pc, ht, cht)); 9819566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(cht, &chtSize)); 9824bbf5ea8SMatthew G. Knepley /* empty patch, continue */ 9834bbf5ea8SMatthew G. Knepley if (chtSize == 0) continue; 9844bbf5ea8SMatthew G. Knepley 9854bbf5ea8SMatthew G. Knepley /* safe because size(cht) > 0 from above */ 9861b68eb51SMatthew G. Knepley PetscHashIterBegin(cht, hi); 9871b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(cht, hi)) { 9885f824522SMatthew G. Knepley PetscInt point, pdof; 9894bbf5ea8SMatthew G. Knepley 9901b68eb51SMatthew G. Knepley PetscHashIterGetKey(cht, hi, point); 9910e126c0bSLawrence Mitchell if (fStart <= point && point < fEnd) { 9920e126c0bSLawrence Mitchell const PetscInt *support; 9930e126c0bSLawrence Mitchell PetscInt supportSize, p; 9940e126c0bSLawrence Mitchell PetscBool interior = PETSC_TRUE; 9959566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, point, &support)); 9969566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, point, &supportSize)); 9970e126c0bSLawrence Mitchell if (supportSize == 1) { 9980e126c0bSLawrence Mitchell interior = PETSC_FALSE; 9990e126c0bSLawrence Mitchell } else { 10000e126c0bSLawrence Mitchell for (p = 0; p < supportSize; p++) { 10010e126c0bSLawrence Mitchell PetscBool found; 10020e126c0bSLawrence Mitchell /* FIXME: can I do this while iterating over cht? */ 10039566063dSJacob Faibussowitsch PetscCall(PetscHSetIHas(cht, support[p], &found)); 10040e126c0bSLawrence Mitchell if (!found) { 10050e126c0bSLawrence Mitchell interior = PETSC_FALSE; 10060e126c0bSLawrence Mitchell break; 10070e126c0bSLawrence Mitchell } 10080e126c0bSLawrence Mitchell } 10090e126c0bSLawrence Mitchell } 10100e126c0bSLawrence Mitchell if (interior) { 10119566063dSJacob Faibussowitsch PetscCall(PetscSectionAddDof(intFacetCounts, v, 1)); 10120e126c0bSLawrence Mitchell } else { 10139566063dSJacob Faibussowitsch PetscCall(PetscSectionAddDof(extFacetCounts, v, 1)); 10140e126c0bSLawrence Mitchell } 10150e126c0bSLawrence Mitchell } 10169566063dSJacob Faibussowitsch PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL)); 10179566063dSJacob Faibussowitsch if (pdof) PetscCall(PetscSectionAddDof(pointCounts, v, 1)); 10189566063dSJacob Faibussowitsch if (point >= cStart && point < cEnd) PetscCall(PetscSectionAddDof(cellCounts, v, 1)); 10191b68eb51SMatthew G. Knepley PetscHashIterNext(cht, hi); 10204bbf5ea8SMatthew G. Knepley } 10214bbf5ea8SMatthew G. Knepley } 10229566063dSJacob Faibussowitsch if (isFiredrake) PetscCall(DMLabelDestroyIndex(ghost)); 10234bbf5ea8SMatthew G. Knepley 10249566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(cellCounts)); 10259566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(cellCounts, &numCells)); 10269566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numCells, &cellsArray)); 10279566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(pointCounts)); 10289566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(pointCounts, &numPoints)); 10299566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPoints, &pointsArray)); 10304bbf5ea8SMatthew G. Knepley 10319566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(intFacetCounts)); 10329566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(extFacetCounts)); 10339566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(intFacetCounts, &numIntFacets)); 10349566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(extFacetCounts, &numExtFacets)); 10359566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numIntFacets, &intFacetsArray)); 10369566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numIntFacets * 2, &intFacetsToPatchCell)); 10379566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numExtFacets, &extFacetsArray)); 10380e126c0bSLawrence Mitchell 10394bbf5ea8SMatthew G. Knepley /* Now that we know how much space we need, run through again and actually remember the cells. */ 10404bbf5ea8SMatthew G. Knepley for (v = vStart; v < vEnd; v++) { 10411b68eb51SMatthew G. Knepley PetscHashIter hi; 10420e126c0bSLawrence Mitchell PetscInt dof, off, cdof, coff, efdof, efoff, ifdof, ifoff, pdof, n = 0, cn = 0, ifn = 0, efn = 0; 10434bbf5ea8SMatthew G. Knepley 10449566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(pointCounts, v, &dof)); 10459566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(pointCounts, v, &off)); 10469566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(cellCounts, v, &cdof)); 10479566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(cellCounts, v, &coff)); 10489566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(intFacetCounts, v, &ifdof)); 10499566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(intFacetCounts, v, &ifoff)); 10509566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(extFacetCounts, v, &efdof)); 10519566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(extFacetCounts, v, &efoff)); 10525f824522SMatthew G. Knepley if (dof <= 0) continue; 10539566063dSJacob Faibussowitsch PetscCall(patch->patchconstructop((void *)patch, dm, v, ht)); 10549566063dSJacob Faibussowitsch PetscCall(PCPatchCompleteCellPatch(pc, ht, cht)); 10551b68eb51SMatthew G. Knepley PetscHashIterBegin(cht, hi); 10561b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(cht, hi)) { 10574bbf5ea8SMatthew G. Knepley PetscInt point; 10584bbf5ea8SMatthew G. Knepley 10591b68eb51SMatthew G. Knepley PetscHashIterGetKey(cht, hi, point); 10600e126c0bSLawrence Mitchell if (fStart <= point && point < fEnd) { 10610e126c0bSLawrence Mitchell const PetscInt *support; 10620e126c0bSLawrence Mitchell PetscInt supportSize, p; 10630e126c0bSLawrence Mitchell PetscBool interior = PETSC_TRUE; 10649566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, point, &support)); 10659566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, point, &supportSize)); 10660e126c0bSLawrence Mitchell if (supportSize == 1) { 10670e126c0bSLawrence Mitchell interior = PETSC_FALSE; 10680e126c0bSLawrence Mitchell } else { 10690e126c0bSLawrence Mitchell for (p = 0; p < supportSize; p++) { 10700e126c0bSLawrence Mitchell PetscBool found; 10710e126c0bSLawrence Mitchell /* FIXME: can I do this while iterating over cht? */ 10729566063dSJacob Faibussowitsch PetscCall(PetscHSetIHas(cht, support[p], &found)); 10730e126c0bSLawrence Mitchell if (!found) { 10740e126c0bSLawrence Mitchell interior = PETSC_FALSE; 10750e126c0bSLawrence Mitchell break; 10760e126c0bSLawrence Mitchell } 10770e126c0bSLawrence Mitchell } 10780e126c0bSLawrence Mitchell } 10790e126c0bSLawrence Mitchell if (interior) { 108044b625f7SLawrence Mitchell intFacetsToPatchCell[2 * (ifoff + ifn)] = support[0]; 108144b625f7SLawrence Mitchell intFacetsToPatchCell[2 * (ifoff + ifn) + 1] = support[1]; 10820e126c0bSLawrence Mitchell intFacetsArray[ifoff + ifn++] = point; 10830e126c0bSLawrence Mitchell } else { 10840e126c0bSLawrence Mitchell extFacetsArray[efoff + efn++] = point; 10850e126c0bSLawrence Mitchell } 10860e126c0bSLawrence Mitchell } 10879566063dSJacob Faibussowitsch PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL)); 1088ad540459SPierre Jolivet if (pdof) pointsArray[off + n++] = point; 1089ad540459SPierre Jolivet if (point >= cStart && point < cEnd) cellsArray[coff + cn++] = point; 10901b68eb51SMatthew G. Knepley PetscHashIterNext(cht, hi); 10914bbf5ea8SMatthew G. Knepley } 109263a3b9bcSJacob Faibussowitsch PetscCheck(ifn == ifdof, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of interior facets in patch %" PetscInt_FMT " is %" PetscInt_FMT ", but should be %" PetscInt_FMT, v, ifn, ifdof); 109363a3b9bcSJacob Faibussowitsch PetscCheck(efn == efdof, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of exterior facets in patch %" PetscInt_FMT " is %" PetscInt_FMT ", but should be %" PetscInt_FMT, v, efn, efdof); 109463a3b9bcSJacob Faibussowitsch PetscCheck(cn == cdof, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of cells in patch %" PetscInt_FMT " is %" PetscInt_FMT ", but should be %" PetscInt_FMT, v, cn, cdof); 109563a3b9bcSJacob Faibussowitsch PetscCheck(n == dof, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of points in patch %" PetscInt_FMT " is %" PetscInt_FMT ", but should be %" PetscInt_FMT, v, n, dof); 1096eb62eeaaSLawrence Mitchell 1097eb62eeaaSLawrence Mitchell for (ifn = 0; ifn < ifdof; ifn++) { 109844b625f7SLawrence Mitchell PetscInt cell0 = intFacetsToPatchCell[2 * (ifoff + ifn)]; 109944b625f7SLawrence Mitchell PetscInt cell1 = intFacetsToPatchCell[2 * (ifoff + ifn) + 1]; 1100eb62eeaaSLawrence Mitchell PetscBool found0 = PETSC_FALSE, found1 = PETSC_FALSE; 1101eb62eeaaSLawrence Mitchell for (n = 0; n < cdof; n++) { 11027c54fef0SLawrence Mitchell if (!found0 && cell0 == cellsArray[coff + n]) { 1103c3faab33SLawrence Mitchell intFacetsToPatchCell[2 * (ifoff + ifn)] = n; 1104eb62eeaaSLawrence Mitchell found0 = PETSC_TRUE; 1105eb62eeaaSLawrence Mitchell } 11067c54fef0SLawrence Mitchell if (!found1 && cell1 == cellsArray[coff + n]) { 1107c3faab33SLawrence Mitchell intFacetsToPatchCell[2 * (ifoff + ifn) + 1] = n; 110880fc4459SLawrence Mitchell found1 = PETSC_TRUE; 1109eb62eeaaSLawrence Mitchell } 1110eb62eeaaSLawrence Mitchell if (found0 && found1) break; 1111eb62eeaaSLawrence Mitchell } 11127827d75bSBarry Smith PetscCheck(found0 && found1, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Didn't manage to find local point numbers for facet support"); 1113eb62eeaaSLawrence Mitchell } 11144bbf5ea8SMatthew G. Knepley } 11159566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&ht)); 11169566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&cht)); 11175f824522SMatthew G. Knepley 11189566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numCells, cellsArray, PETSC_OWN_POINTER, &patch->cells)); 11199566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->cells, "Patch Cells")); 11205f824522SMatthew G. Knepley if (patch->viewCells) { 11219566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject)patch->cellCounts, patch->viewerCells, patch->formatCells)); 11229566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject)patch->cells, patch->viewerCells, patch->formatCells)); 11235f824522SMatthew G. Knepley } 11249566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray, PETSC_OWN_POINTER, &patch->intFacets)); 11259566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->intFacets, "Patch Interior Facets")); 11269566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 2 * numIntFacets, intFacetsToPatchCell, PETSC_OWN_POINTER, &patch->intFacetsToPatchCell)); 11279566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->intFacetsToPatchCell, "Patch Interior Facets local support")); 11280e126c0bSLawrence Mitchell if (patch->viewIntFacets) { 11299566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject)patch->intFacetCounts, patch->viewerIntFacets, patch->formatIntFacets)); 11309566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject)patch->intFacets, patch->viewerIntFacets, patch->formatIntFacets)); 11319566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject)patch->intFacetsToPatchCell, patch->viewerIntFacets, patch->formatIntFacets)); 11320e126c0bSLawrence Mitchell } 11339566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numExtFacets, extFacetsArray, PETSC_OWN_POINTER, &patch->extFacets)); 11349566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->extFacets, "Patch Exterior Facets")); 11350e126c0bSLawrence Mitchell if (patch->viewExtFacets) { 11369566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject)patch->extFacetCounts, patch->viewerExtFacets, patch->formatExtFacets)); 11379566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject)patch->extFacets, patch->viewerExtFacets, patch->formatExtFacets)); 11380e126c0bSLawrence Mitchell } 11399566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints, pointsArray, PETSC_OWN_POINTER, &patch->points)); 11409566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->points, "Patch Points")); 11415f824522SMatthew G. Knepley if (patch->viewPoints) { 11429566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject)patch->pointCounts, patch->viewerPoints, patch->formatPoints)); 11439566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject)patch->points, patch->viewerPoints, patch->formatPoints)); 11445f824522SMatthew G. Knepley } 11459566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm)); 11463ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 11474bbf5ea8SMatthew G. Knepley } 11484bbf5ea8SMatthew G. Knepley 11494bbf5ea8SMatthew G. Knepley /* 1150f1580f4eSBarry Smith PCPatchCreateCellPatchDiscretisationInfo - Build the dof maps for cell patches 1151f1580f4eSBarry Smith 1152f1580f4eSBarry Smith Input Parameters: 1153f1580f4eSBarry Smith + dm - The DMPlex object defining the mesh 1154f1580f4eSBarry Smith . cellCounts - Section with counts of cells around each vertex 1155f1580f4eSBarry Smith . cells - IS of the cell point indices of cells in each patch 1156f1580f4eSBarry Smith . cellNumbering - Section mapping plex cell points to Firedrake cell indices. 1157f1580f4eSBarry Smith . nodesPerCell - number of nodes per cell. 1158f1580f4eSBarry Smith - cellNodeMap - map from cells to node indices (nodesPerCell * numCells) 1159f1580f4eSBarry Smith 1160f1580f4eSBarry Smith Output Parameters: 1161f1580f4eSBarry Smith + dofs - IS of local dof numbers of each cell in the patch, where local is a patch local numbering 1162f1580f4eSBarry Smith . gtolCounts - Section with counts of dofs per cell patch 1163f1580f4eSBarry Smith - gtol - IS mapping from global dofs to local dofs for each patch. 11644bbf5ea8SMatthew G. Knepley */ 1165d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCreateCellPatchDiscretisationInfo(PC pc) 1166d71ae5a4SJacob Faibussowitsch { 11674bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 11684bbf5ea8SMatthew G. Knepley PetscSection cellCounts = patch->cellCounts; 11695f824522SMatthew G. Knepley PetscSection pointCounts = patch->pointCounts; 11700904074fSPatrick Farrell PetscSection gtolCounts, gtolCountsWithArtificial = NULL, gtolCountsWithAll = NULL; 11714bbf5ea8SMatthew G. Knepley IS cells = patch->cells; 11725f824522SMatthew G. Knepley IS points = patch->points; 11734bbf5ea8SMatthew G. Knepley PetscSection cellNumbering = patch->cellNumbering; 11745f824522SMatthew G. Knepley PetscInt Nf = patch->nsubspaces; 11755f824522SMatthew G. Knepley PetscInt numCells, numPoints; 11764bbf5ea8SMatthew G. Knepley PetscInt numDofs; 11770904074fSPatrick Farrell PetscInt numGlobalDofs, numGlobalDofsWithArtificial, numGlobalDofsWithAll; 11784bbf5ea8SMatthew G. Knepley PetscInt totalDofsPerCell = patch->totalDofsPerCell; 11794bbf5ea8SMatthew G. Knepley PetscInt vStart, vEnd, v; 11805f824522SMatthew G. Knepley const PetscInt *cellsArray, *pointsArray; 11814bbf5ea8SMatthew G. Knepley PetscInt *newCellsArray = NULL; 11824bbf5ea8SMatthew G. Knepley PetscInt *dofsArray = NULL; 1183c2e6f3c0SFlorian Wechsung PetscInt *dofsArrayWithArtificial = NULL; 11840904074fSPatrick Farrell PetscInt *dofsArrayWithAll = NULL; 11855f824522SMatthew G. Knepley PetscInt *offsArray = NULL; 1186c2e6f3c0SFlorian Wechsung PetscInt *offsArrayWithArtificial = NULL; 11870904074fSPatrick Farrell PetscInt *offsArrayWithAll = NULL; 11884bbf5ea8SMatthew G. Knepley PetscInt *asmArray = NULL; 1189c2e6f3c0SFlorian Wechsung PetscInt *asmArrayWithArtificial = NULL; 11900904074fSPatrick Farrell PetscInt *asmArrayWithAll = NULL; 11914bbf5ea8SMatthew G. Knepley PetscInt *globalDofsArray = NULL; 1192c2e6f3c0SFlorian Wechsung PetscInt *globalDofsArrayWithArtificial = NULL; 11930904074fSPatrick Farrell PetscInt *globalDofsArrayWithAll = NULL; 11944bbf5ea8SMatthew G. Knepley PetscInt globalIndex = 0; 11954bbf5ea8SMatthew G. Knepley PetscInt key = 0; 11964bbf5ea8SMatthew G. Knepley PetscInt asmKey = 0; 1197b6bb21d1SLawrence Mitchell DM dm = NULL, plex; 1198557beb66SLawrence Mitchell const PetscInt *bcNodes = NULL; 11991b68eb51SMatthew G. Knepley PetscHMapI ht; 1200c2e6f3c0SFlorian Wechsung PetscHMapI htWithArtificial; 12010904074fSPatrick Farrell PetscHMapI htWithAll; 12021b68eb51SMatthew G. Knepley PetscHSetI globalBcs; 1203557beb66SLawrence Mitchell PetscInt numBcs; 12041b68eb51SMatthew G. Knepley PetscHSetI ownedpts, seenpts, owneddofs, seendofs, artificialbcs; 1205cda239d9SMatthew G. Knepley PetscInt pStart, pEnd, p, i; 120610534d48SPatrick Farrell char option[PETSC_MAX_PATH_LEN]; 120739fd2e8aSPatrick Farrell PetscBool isNonlinear; 12084bbf5ea8SMatthew G. Knepley 12094bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 1210557beb66SLawrence Mitchell 12119566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm)); 12129566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex)); 1213b6bb21d1SLawrence Mitchell dm = plex; 12144bbf5ea8SMatthew G. Knepley /* dofcounts section is cellcounts section * dofPerCell */ 12159566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(cellCounts, &numCells)); 12169566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(patch->pointCounts, &numPoints)); 12174bbf5ea8SMatthew G. Knepley numDofs = numCells * totalDofsPerCell; 12189566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numDofs, &dofsArray)); 12199566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPoints * Nf, &offsArray)); 12209566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numDofs, &asmArray)); 12219566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numCells, &newCellsArray)); 12229566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(cellCounts, &vStart, &vEnd)); 12239566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCounts)); 12244bbf5ea8SMatthew G. Knepley gtolCounts = patch->gtolCounts; 12259566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(gtolCounts, vStart, vEnd)); 12269566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->gtolCounts, "Patch Global Index Section")); 12274bbf5ea8SMatthew G. Knepley 1228b6bb21d1SLawrence Mitchell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 12299566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPoints * Nf, &offsArrayWithArtificial)); 12309566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numDofs, &asmArrayWithArtificial)); 12319566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numDofs, &dofsArrayWithArtificial)); 12329566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithArtificial)); 1233c2e6f3c0SFlorian Wechsung gtolCountsWithArtificial = patch->gtolCountsWithArtificial; 12349566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(gtolCountsWithArtificial, vStart, vEnd)); 12359566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->gtolCountsWithArtificial, "Patch Global Index Section Including Artificial BCs")); 1236c2e6f3c0SFlorian Wechsung } 1237c2e6f3c0SFlorian Wechsung 12380904074fSPatrick Farrell isNonlinear = patch->isNonlinear; 1239b6bb21d1SLawrence Mitchell if (isNonlinear) { 12409566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPoints * Nf, &offsArrayWithAll)); 12419566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numDofs, &asmArrayWithAll)); 12429566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numDofs, &dofsArrayWithAll)); 12439566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithAll)); 12440904074fSPatrick Farrell gtolCountsWithAll = patch->gtolCountsWithAll; 12459566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(gtolCountsWithAll, vStart, vEnd)); 12469566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->gtolCountsWithAll, "Patch Global Index Section Including All BCs")); 12470904074fSPatrick Farrell } 12480904074fSPatrick Farrell 1249557beb66SLawrence Mitchell /* Outside the patch loop, get the dofs that are globally-enforced Dirichlet 1250557beb66SLawrence Mitchell conditions */ 12519566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&globalBcs)); 12529566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->ghostBcNodes, &bcNodes)); 12539566063dSJacob Faibussowitsch PetscCall(ISGetSize(patch->ghostBcNodes, &numBcs)); 12549371c9d4SSatish Balay for (i = 0; i < numBcs; ++i) { PetscCall(PetscHSetIAdd(globalBcs, bcNodes[i])); /* these are already in concatenated numbering */ } 12559566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->ghostBcNodes, &bcNodes)); 12569566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->ghostBcNodes)); /* memory optimisation */ 1257557beb66SLawrence Mitchell 1258557beb66SLawrence Mitchell /* Hash tables for artificial BC construction */ 12599566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&ownedpts)); 12609566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&seenpts)); 12619566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&owneddofs)); 12629566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&seendofs)); 12639566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&artificialbcs)); 1264557beb66SLawrence Mitchell 12659566063dSJacob Faibussowitsch PetscCall(ISGetIndices(cells, &cellsArray)); 12669566063dSJacob Faibussowitsch PetscCall(ISGetIndices(points, &pointsArray)); 12679566063dSJacob Faibussowitsch PetscCall(PetscHMapICreate(&ht)); 12689566063dSJacob Faibussowitsch PetscCall(PetscHMapICreate(&htWithArtificial)); 12699566063dSJacob Faibussowitsch PetscCall(PetscHMapICreate(&htWithAll)); 12704bbf5ea8SMatthew G. Knepley for (v = vStart; v < vEnd; ++v) { 12714bbf5ea8SMatthew G. Knepley PetscInt localIndex = 0; 1272c2e6f3c0SFlorian Wechsung PetscInt localIndexWithArtificial = 0; 12730904074fSPatrick Farrell PetscInt localIndexWithAll = 0; 12744bbf5ea8SMatthew G. Knepley PetscInt dof, off, i, j, k, l; 12754bbf5ea8SMatthew G. Knepley 12769566063dSJacob Faibussowitsch PetscCall(PetscHMapIClear(ht)); 12779566063dSJacob Faibussowitsch PetscCall(PetscHMapIClear(htWithArtificial)); 12789566063dSJacob Faibussowitsch PetscCall(PetscHMapIClear(htWithAll)); 12799566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(cellCounts, v, &dof)); 12809566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(cellCounts, v, &off)); 12814bbf5ea8SMatthew G. Knepley if (dof <= 0) continue; 12824bbf5ea8SMatthew G. Knepley 1283557beb66SLawrence Mitchell /* Calculate the global numbers of the artificial BC dofs here first */ 12849566063dSJacob Faibussowitsch PetscCall(patch->patchconstructop((void *)patch, dm, v, ownedpts)); 12859566063dSJacob Faibussowitsch PetscCall(PCPatchCompleteCellPatch(pc, ownedpts, seenpts)); 12869566063dSJacob Faibussowitsch PetscCall(PCPatchGetPointDofs(pc, ownedpts, owneddofs, v, &patch->subspaces_to_exclude)); 12879566063dSJacob Faibussowitsch PetscCall(PCPatchGetPointDofs(pc, seenpts, seendofs, v, NULL)); 12889566063dSJacob Faibussowitsch PetscCall(PCPatchComputeSetDifference_Private(owneddofs, seendofs, artificialbcs)); 12898135ed82SLawrence Mitchell if (patch->viewPatches) { 12901b68eb51SMatthew G. Knepley PetscHSetI globalbcdofs; 12911b68eb51SMatthew G. Knepley PetscHashIter hi; 12928135ed82SLawrence Mitchell MPI_Comm comm = PetscObjectComm((PetscObject)pc); 12931b68eb51SMatthew G. Knepley 12949566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&globalbcdofs)); 129563a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": owned dofs:\n", v)); 12961b68eb51SMatthew G. Knepley PetscHashIterBegin(owneddofs, hi); 12971b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(owneddofs, hi)) { 12988135ed82SLawrence Mitchell PetscInt globalDof; 12998135ed82SLawrence Mitchell 13001b68eb51SMatthew G. Knepley PetscHashIterGetKey(owneddofs, hi, globalDof); 13011b68eb51SMatthew G. Knepley PetscHashIterNext(owneddofs, hi); 130263a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof)); 13038135ed82SLawrence Mitchell } 13049566063dSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "\n")); 130563a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": seen dofs:\n", v)); 13061b68eb51SMatthew G. Knepley PetscHashIterBegin(seendofs, hi); 13071b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(seendofs, hi)) { 13088135ed82SLawrence Mitchell PetscInt globalDof; 13098135ed82SLawrence Mitchell PetscBool flg; 13108135ed82SLawrence Mitchell 13111b68eb51SMatthew G. Knepley PetscHashIterGetKey(seendofs, hi, globalDof); 13121b68eb51SMatthew G. Knepley PetscHashIterNext(seendofs, hi); 131363a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof)); 13148135ed82SLawrence Mitchell 13159566063dSJacob Faibussowitsch PetscCall(PetscHSetIHas(globalBcs, globalDof, &flg)); 13169566063dSJacob Faibussowitsch if (flg) PetscCall(PetscHSetIAdd(globalbcdofs, globalDof)); 13178135ed82SLawrence Mitchell } 13189566063dSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "\n")); 131963a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": global BCs:\n", v)); 13209566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(globalbcdofs, &numBcs)); 13218135ed82SLawrence Mitchell if (numBcs > 0) { 13221b68eb51SMatthew G. Knepley PetscHashIterBegin(globalbcdofs, hi); 13231b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(globalbcdofs, hi)) { 13248135ed82SLawrence Mitchell PetscInt globalDof; 13251b68eb51SMatthew G. Knepley PetscHashIterGetKey(globalbcdofs, hi, globalDof); 13261b68eb51SMatthew G. Knepley PetscHashIterNext(globalbcdofs, hi); 132763a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof)); 13288135ed82SLawrence Mitchell } 13298135ed82SLawrence Mitchell } 13309566063dSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "\n")); 133163a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": artificial BCs:\n", v)); 13329566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(artificialbcs, &numBcs)); 13338135ed82SLawrence Mitchell if (numBcs > 0) { 13341b68eb51SMatthew G. Knepley PetscHashIterBegin(artificialbcs, hi); 13351b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(artificialbcs, hi)) { 13368135ed82SLawrence Mitchell PetscInt globalDof; 13371b68eb51SMatthew G. Knepley PetscHashIterGetKey(artificialbcs, hi, globalDof); 13381b68eb51SMatthew G. Knepley PetscHashIterNext(artificialbcs, hi); 133963a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof)); 13408135ed82SLawrence Mitchell } 13418135ed82SLawrence Mitchell } 13429566063dSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "\n\n")); 13439566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&globalbcdofs)); 13448135ed82SLawrence Mitchell } 13454bbf5ea8SMatthew G. Knepley for (k = 0; k < patch->nsubspaces; ++k) { 13464bbf5ea8SMatthew G. Knepley const PetscInt *cellNodeMap = patch->cellNodeMap[k]; 13474bbf5ea8SMatthew G. Knepley PetscInt nodesPerCell = patch->nodesPerCell[k]; 13484bbf5ea8SMatthew G. Knepley PetscInt subspaceOffset = patch->subspaceOffsets[k]; 13494bbf5ea8SMatthew G. Knepley PetscInt bs = patch->bs[k]; 13504bbf5ea8SMatthew G. Knepley 13514bbf5ea8SMatthew G. Knepley for (i = off; i < off + dof; ++i) { 13524bbf5ea8SMatthew G. Knepley /* Walk over the cells in this patch. */ 13534bbf5ea8SMatthew G. Knepley const PetscInt c = cellsArray[i]; 13545f824522SMatthew G. Knepley PetscInt cell = c; 13554bbf5ea8SMatthew G. Knepley 13565f824522SMatthew G. Knepley /* TODO Change this to an IS */ 13575f824522SMatthew G. Knepley if (cellNumbering) { 13589566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(cellNumbering, c, &cell)); 135963a3b9bcSJacob Faibussowitsch PetscCheck(cell > 0, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Cell %" PetscInt_FMT " doesn't appear in cell numbering map", c); 13609566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(cellNumbering, c, &cell)); 13615f824522SMatthew G. Knepley } 13624bbf5ea8SMatthew G. Knepley newCellsArray[i] = cell; 13634bbf5ea8SMatthew G. Knepley for (j = 0; j < nodesPerCell; ++j) { 13644bbf5ea8SMatthew G. Knepley /* For each global dof, map it into contiguous local storage. */ 13654bbf5ea8SMatthew G. Knepley const PetscInt globalDof = cellNodeMap[cell * nodesPerCell + j] * bs + subspaceOffset; 13664bbf5ea8SMatthew G. Knepley /* finally, loop over block size */ 13674bbf5ea8SMatthew G. Knepley for (l = 0; l < bs; ++l) { 13681b68eb51SMatthew G. Knepley PetscInt localDof; 13691b68eb51SMatthew G. Knepley PetscBool isGlobalBcDof, isArtificialBcDof; 13704bbf5ea8SMatthew G. Knepley 1371557beb66SLawrence Mitchell /* first, check if this is either a globally enforced or locally enforced BC dof */ 13729566063dSJacob Faibussowitsch PetscCall(PetscHSetIHas(globalBcs, globalDof + l, &isGlobalBcDof)); 13739566063dSJacob Faibussowitsch PetscCall(PetscHSetIHas(artificialbcs, globalDof + l, &isArtificialBcDof)); 1374557beb66SLawrence Mitchell 1375557beb66SLawrence Mitchell /* if it's either, don't ever give it a local dof number */ 13761b68eb51SMatthew G. Knepley if (isGlobalBcDof || isArtificialBcDof) { 1377c2e6f3c0SFlorian Wechsung dofsArray[globalIndex] = -1; /* don't use this in assembly in this patch */ 1378557beb66SLawrence Mitchell } else { 13799566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(ht, globalDof + l, &localDof)); 13804bbf5ea8SMatthew G. Knepley if (localDof == -1) { 13814bbf5ea8SMatthew G. Knepley localDof = localIndex++; 13829566063dSJacob Faibussowitsch PetscCall(PetscHMapISet(ht, globalDof + l, localDof)); 13834bbf5ea8SMatthew G. Knepley } 138463a3b9bcSJacob Faibussowitsch PetscCheck(globalIndex < numDofs, PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %" PetscInt_FMT " than expected %" PetscInt_FMT, globalIndex + 1, numDofs); 13854bbf5ea8SMatthew G. Knepley /* And store. */ 1386c2e6f3c0SFlorian Wechsung dofsArray[globalIndex] = localDof; 13874bbf5ea8SMatthew G. Knepley } 1388c2e6f3c0SFlorian Wechsung 13890904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1390c2e6f3c0SFlorian Wechsung if (isGlobalBcDof) { 1391e047a90bSFlorian Wechsung dofsArrayWithArtificial[globalIndex] = -1; /* don't use this in assembly in this patch */ 1392c2e6f3c0SFlorian Wechsung } else { 13939566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(htWithArtificial, globalDof + l, &localDof)); 1394c2e6f3c0SFlorian Wechsung if (localDof == -1) { 1395c2e6f3c0SFlorian Wechsung localDof = localIndexWithArtificial++; 13969566063dSJacob Faibussowitsch PetscCall(PetscHMapISet(htWithArtificial, globalDof + l, localDof)); 1397c2e6f3c0SFlorian Wechsung } 139863a3b9bcSJacob Faibussowitsch PetscCheck(globalIndex < numDofs, PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %" PetscInt_FMT " than expected %" PetscInt_FMT, globalIndex + 1, numDofs); 1399c2e6f3c0SFlorian Wechsung /* And store.*/ 1400c2e6f3c0SFlorian Wechsung dofsArrayWithArtificial[globalIndex] = localDof; 1401c2e6f3c0SFlorian Wechsung } 1402c2e6f3c0SFlorian Wechsung } 14030904074fSPatrick Farrell 14040904074fSPatrick Farrell if (isNonlinear) { 14050904074fSPatrick Farrell /* Build the dofmap for the function space with _all_ dofs, 14060904074fSPatrick Farrell including those in any kind of boundary condition */ 14079566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(htWithAll, globalDof + l, &localDof)); 14080904074fSPatrick Farrell if (localDof == -1) { 14090904074fSPatrick Farrell localDof = localIndexWithAll++; 14109566063dSJacob Faibussowitsch PetscCall(PetscHMapISet(htWithAll, globalDof + l, localDof)); 14110904074fSPatrick Farrell } 141263a3b9bcSJacob Faibussowitsch PetscCheck(globalIndex < numDofs, PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %" PetscInt_FMT " than expected %" PetscInt_FMT, globalIndex + 1, numDofs); 14130904074fSPatrick Farrell /* And store.*/ 14140904074fSPatrick Farrell dofsArrayWithAll[globalIndex] = localDof; 14150904074fSPatrick Farrell } 1416c2e6f3c0SFlorian Wechsung globalIndex++; 14174bbf5ea8SMatthew G. Knepley } 14184bbf5ea8SMatthew G. Knepley } 14194bbf5ea8SMatthew G. Knepley } 1420557beb66SLawrence Mitchell } 14214bbf5ea8SMatthew G. Knepley /*How many local dofs in this patch? */ 14220904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 14239566063dSJacob Faibussowitsch PetscCall(PetscHMapIGetSize(htWithArtificial, &dof)); 14249566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(gtolCountsWithArtificial, v, dof)); 1425c2e6f3c0SFlorian Wechsung } 14260904074fSPatrick Farrell if (isNonlinear) { 14279566063dSJacob Faibussowitsch PetscCall(PetscHMapIGetSize(htWithAll, &dof)); 14289566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(gtolCountsWithAll, v, dof)); 14290904074fSPatrick Farrell } 14309566063dSJacob Faibussowitsch PetscCall(PetscHMapIGetSize(ht, &dof)); 14319566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(gtolCounts, v, dof)); 14324bbf5ea8SMatthew G. Knepley } 1433b6bb21d1SLawrence Mitchell 14349566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm)); 143563a3b9bcSJacob Faibussowitsch PetscCheck(globalIndex == numDofs, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Expected number of dofs (%" PetscInt_FMT ") doesn't match found number (%" PetscInt_FMT ")", numDofs, globalIndex); 14369566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(gtolCounts)); 14379566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(gtolCounts, &numGlobalDofs)); 14389566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numGlobalDofs, &globalDofsArray)); 14394bbf5ea8SMatthew G. Knepley 14400904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 14419566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(gtolCountsWithArtificial)); 14429566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(gtolCountsWithArtificial, &numGlobalDofsWithArtificial)); 14439566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numGlobalDofsWithArtificial, &globalDofsArrayWithArtificial)); 1444c2e6f3c0SFlorian Wechsung } 14450904074fSPatrick Farrell if (isNonlinear) { 14469566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(gtolCountsWithAll)); 14479566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(gtolCountsWithAll, &numGlobalDofsWithAll)); 14489566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numGlobalDofsWithAll, &globalDofsArrayWithAll)); 14490904074fSPatrick Farrell } 14504bbf5ea8SMatthew 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. */ 14514bbf5ea8SMatthew G. Knepley for (v = vStart; v < vEnd; ++v) { 14521b68eb51SMatthew G. Knepley PetscHashIter hi; 14535f824522SMatthew G. Knepley PetscInt dof, off, Np, ooff, i, j, k, l; 14544bbf5ea8SMatthew G. Knepley 14559566063dSJacob Faibussowitsch PetscCall(PetscHMapIClear(ht)); 14569566063dSJacob Faibussowitsch PetscCall(PetscHMapIClear(htWithArtificial)); 14579566063dSJacob Faibussowitsch PetscCall(PetscHMapIClear(htWithAll)); 14589566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(cellCounts, v, &dof)); 14599566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(cellCounts, v, &off)); 14609566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(pointCounts, v, &Np)); 14619566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(pointCounts, v, &ooff)); 14624bbf5ea8SMatthew G. Knepley if (dof <= 0) continue; 14634bbf5ea8SMatthew G. Knepley 14644bbf5ea8SMatthew G. Knepley for (k = 0; k < patch->nsubspaces; ++k) { 14654bbf5ea8SMatthew G. Knepley const PetscInt *cellNodeMap = patch->cellNodeMap[k]; 14664bbf5ea8SMatthew G. Knepley PetscInt nodesPerCell = patch->nodesPerCell[k]; 14674bbf5ea8SMatthew G. Knepley PetscInt subspaceOffset = patch->subspaceOffsets[k]; 14684bbf5ea8SMatthew G. Knepley PetscInt bs = patch->bs[k]; 1469d490bb3dSLawrence Mitchell PetscInt goff; 14704bbf5ea8SMatthew G. Knepley 14714bbf5ea8SMatthew G. Knepley for (i = off; i < off + dof; ++i) { 14724bbf5ea8SMatthew G. Knepley /* Reconstruct mapping of global-to-local on this patch. */ 14734bbf5ea8SMatthew G. Knepley const PetscInt c = cellsArray[i]; 14745f824522SMatthew G. Knepley PetscInt cell = c; 14754bbf5ea8SMatthew G. Knepley 14769566063dSJacob Faibussowitsch if (cellNumbering) PetscCall(PetscSectionGetOffset(cellNumbering, c, &cell)); 14774bbf5ea8SMatthew G. Knepley for (j = 0; j < nodesPerCell; ++j) { 14784bbf5ea8SMatthew G. Knepley for (l = 0; l < bs; ++l) { 14795f824522SMatthew G. Knepley const PetscInt globalDof = cellNodeMap[cell * nodesPerCell + j] * bs + l + subspaceOffset; 1480c2e6f3c0SFlorian Wechsung const PetscInt localDof = dofsArray[key]; 14819566063dSJacob Faibussowitsch if (localDof >= 0) PetscCall(PetscHMapISet(ht, globalDof, localDof)); 14820904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1483c2e6f3c0SFlorian Wechsung const PetscInt localDofWithArtificial = dofsArrayWithArtificial[key]; 148448a46eb9SPierre Jolivet if (localDofWithArtificial >= 0) PetscCall(PetscHMapISet(htWithArtificial, globalDof, localDofWithArtificial)); 1485c2e6f3c0SFlorian Wechsung } 14860904074fSPatrick Farrell if (isNonlinear) { 14870904074fSPatrick Farrell const PetscInt localDofWithAll = dofsArrayWithAll[key]; 148848a46eb9SPierre Jolivet if (localDofWithAll >= 0) PetscCall(PetscHMapISet(htWithAll, globalDof, localDofWithAll)); 14890904074fSPatrick Farrell } 1490c2e6f3c0SFlorian Wechsung key++; 14914bbf5ea8SMatthew G. Knepley } 14924bbf5ea8SMatthew G. Knepley } 14934bbf5ea8SMatthew G. Knepley } 1494557beb66SLawrence Mitchell 14954bbf5ea8SMatthew G. Knepley /* Shove it in the output data structure. */ 14969566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(gtolCounts, v, &goff)); 14971b68eb51SMatthew G. Knepley PetscHashIterBegin(ht, hi); 14981b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(ht, hi)) { 14994bbf5ea8SMatthew G. Knepley PetscInt globalDof, localDof; 15004bbf5ea8SMatthew G. Knepley 15011b68eb51SMatthew G. Knepley PetscHashIterGetKey(ht, hi, globalDof); 15021b68eb51SMatthew G. Knepley PetscHashIterGetVal(ht, hi, localDof); 15034bbf5ea8SMatthew G. Knepley if (globalDof >= 0) globalDofsArray[goff + localDof] = globalDof; 15041b68eb51SMatthew G. Knepley PetscHashIterNext(ht, hi); 15054bbf5ea8SMatthew G. Knepley } 15065f824522SMatthew G. Knepley 15070904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 15089566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(gtolCountsWithArtificial, v, &goff)); 1509c2e6f3c0SFlorian Wechsung PetscHashIterBegin(htWithArtificial, hi); 1510c2e6f3c0SFlorian Wechsung while (!PetscHashIterAtEnd(htWithArtificial, hi)) { 1511c2e6f3c0SFlorian Wechsung PetscInt globalDof, localDof; 1512c2e6f3c0SFlorian Wechsung PetscHashIterGetKey(htWithArtificial, hi, globalDof); 1513c2e6f3c0SFlorian Wechsung PetscHashIterGetVal(htWithArtificial, hi, localDof); 1514c2e6f3c0SFlorian Wechsung if (globalDof >= 0) globalDofsArrayWithArtificial[goff + localDof] = globalDof; 1515c2e6f3c0SFlorian Wechsung PetscHashIterNext(htWithArtificial, hi); 1516c2e6f3c0SFlorian Wechsung } 1517c2e6f3c0SFlorian Wechsung } 15180904074fSPatrick Farrell if (isNonlinear) { 15199566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(gtolCountsWithAll, v, &goff)); 15200904074fSPatrick Farrell PetscHashIterBegin(htWithAll, hi); 15210904074fSPatrick Farrell while (!PetscHashIterAtEnd(htWithAll, hi)) { 15220904074fSPatrick Farrell PetscInt globalDof, localDof; 15230904074fSPatrick Farrell PetscHashIterGetKey(htWithAll, hi, globalDof); 15240904074fSPatrick Farrell PetscHashIterGetVal(htWithAll, hi, localDof); 15250904074fSPatrick Farrell if (globalDof >= 0) globalDofsArrayWithAll[goff + localDof] = globalDof; 15260904074fSPatrick Farrell PetscHashIterNext(htWithAll, hi); 15270904074fSPatrick Farrell } 15280904074fSPatrick Farrell } 1529c2e6f3c0SFlorian Wechsung 15305f824522SMatthew G. Knepley for (p = 0; p < Np; ++p) { 15315f824522SMatthew G. Knepley const PetscInt point = pointsArray[ooff + p]; 15325f824522SMatthew G. Knepley PetscInt globalDof, localDof; 15335f824522SMatthew G. Knepley 15349566063dSJacob Faibussowitsch PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, point, NULL, &globalDof)); 15359566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(ht, globalDof, &localDof)); 15365f824522SMatthew G. Knepley offsArray[(ooff + p) * Nf + k] = localDof; 15370904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 15389566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(htWithArtificial, globalDof, &localDof)); 1539c2e6f3c0SFlorian Wechsung offsArrayWithArtificial[(ooff + p) * Nf + k] = localDof; 1540c2e6f3c0SFlorian Wechsung } 15410904074fSPatrick Farrell if (isNonlinear) { 15429566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(htWithAll, globalDof, &localDof)); 15430904074fSPatrick Farrell offsArrayWithAll[(ooff + p) * Nf + k] = localDof; 15440904074fSPatrick Farrell } 15455f824522SMatthew G. Knepley } 15464bbf5ea8SMatthew G. Knepley } 15474bbf5ea8SMatthew G. Knepley 15489566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&globalBcs)); 15499566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&ownedpts)); 15509566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&seenpts)); 15519566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&owneddofs)); 15529566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&seendofs)); 15539566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&artificialbcs)); 1554557beb66SLawrence Mitchell 15554bbf5ea8SMatthew G. Knepley /* At this point, we have a hash table ht built that maps globalDof -> localDof. 15564bbf5ea8SMatthew G. Knepley We need to create the dof table laid out cellwise first, then by subspace, 15574bbf5ea8SMatthew G. Knepley as the assembler assembles cell-wise and we need to stuff the different 15584bbf5ea8SMatthew G. Knepley contributions of the different function spaces to the right places. So we loop 15594bbf5ea8SMatthew G. Knepley over cells, then over subspaces. */ 15604bbf5ea8SMatthew G. Knepley if (patch->nsubspaces > 1) { /* for nsubspaces = 1, data we need is already in dofsArray */ 15614bbf5ea8SMatthew G. Knepley for (i = off; i < off + dof; ++i) { 15624bbf5ea8SMatthew G. Knepley const PetscInt c = cellsArray[i]; 15635f824522SMatthew G. Knepley PetscInt cell = c; 15644bbf5ea8SMatthew G. Knepley 15659566063dSJacob Faibussowitsch if (cellNumbering) PetscCall(PetscSectionGetOffset(cellNumbering, c, &cell)); 15664bbf5ea8SMatthew G. Knepley for (k = 0; k < patch->nsubspaces; ++k) { 15674bbf5ea8SMatthew G. Knepley const PetscInt *cellNodeMap = patch->cellNodeMap[k]; 15684bbf5ea8SMatthew G. Knepley PetscInt nodesPerCell = patch->nodesPerCell[k]; 15694bbf5ea8SMatthew G. Knepley PetscInt subspaceOffset = patch->subspaceOffsets[k]; 15704bbf5ea8SMatthew G. Knepley PetscInt bs = patch->bs[k]; 15714bbf5ea8SMatthew G. Knepley 15724bbf5ea8SMatthew G. Knepley for (j = 0; j < nodesPerCell; ++j) { 15734bbf5ea8SMatthew G. Knepley for (l = 0; l < bs; ++l) { 15745f824522SMatthew G. Knepley const PetscInt globalDof = cellNodeMap[cell * nodesPerCell + j] * bs + l + subspaceOffset; 15754bbf5ea8SMatthew G. Knepley PetscInt localDof; 15764bbf5ea8SMatthew G. Knepley 15779566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(ht, globalDof, &localDof)); 1578557beb66SLawrence Mitchell /* If it's not in the hash table, i.e. is a BC dof, 15791b68eb51SMatthew G. Knepley then the PetscHSetIMap above gives -1, which matches 1580557beb66SLawrence Mitchell exactly the convention for PETSc's matrix assembly to 1581557beb66SLawrence Mitchell ignore the dof. So we don't need to do anything here */ 1582c2e6f3c0SFlorian Wechsung asmArray[asmKey] = localDof; 15830904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 15849566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(htWithArtificial, globalDof, &localDof)); 1585c2e6f3c0SFlorian Wechsung asmArrayWithArtificial[asmKey] = localDof; 1586c2e6f3c0SFlorian Wechsung } 15870904074fSPatrick Farrell if (isNonlinear) { 15889566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(htWithAll, globalDof, &localDof)); 15890904074fSPatrick Farrell asmArrayWithAll[asmKey] = localDof; 15900904074fSPatrick Farrell } 1591c2e6f3c0SFlorian Wechsung asmKey++; 15924bbf5ea8SMatthew G. Knepley } 15934bbf5ea8SMatthew G. Knepley } 15944bbf5ea8SMatthew G. Knepley } 15954bbf5ea8SMatthew G. Knepley } 15964bbf5ea8SMatthew G. Knepley } 15974bbf5ea8SMatthew G. Knepley } 1598c2e6f3c0SFlorian Wechsung if (1 == patch->nsubspaces) { 15999566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(asmArray, dofsArray, numDofs)); 160048a46eb9SPierre Jolivet if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) PetscCall(PetscArraycpy(asmArrayWithArtificial, dofsArrayWithArtificial, numDofs)); 16011baa6e33SBarry Smith if (isNonlinear) PetscCall(PetscArraycpy(asmArrayWithAll, dofsArrayWithAll, numDofs)); 1602c2e6f3c0SFlorian Wechsung } 16034bbf5ea8SMatthew G. Knepley 16049566063dSJacob Faibussowitsch PetscCall(PetscHMapIDestroy(&ht)); 16059566063dSJacob Faibussowitsch PetscCall(PetscHMapIDestroy(&htWithArtificial)); 16069566063dSJacob Faibussowitsch PetscCall(PetscHMapIDestroy(&htWithAll)); 16079566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(cells, &cellsArray)); 16089566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(points, &pointsArray)); 16099566063dSJacob Faibussowitsch PetscCall(PetscFree(dofsArray)); 161048a46eb9SPierre Jolivet if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) PetscCall(PetscFree(dofsArrayWithArtificial)); 16111baa6e33SBarry Smith if (isNonlinear) PetscCall(PetscFree(dofsArrayWithAll)); 16125f824522SMatthew G. Knepley /* Create placeholder section for map from points to patch dofs */ 16139566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->patchSection)); 16149566063dSJacob Faibussowitsch PetscCall(PetscSectionSetNumFields(patch->patchSection, patch->nsubspaces)); 16151e5fa6bbSLawrence Mitchell if (patch->combined) { 16161e5fa6bbSLawrence Mitchell PetscInt numFields; 16179566063dSJacob Faibussowitsch PetscCall(PetscSectionGetNumFields(patch->dofSection[0], &numFields)); 161863a3b9bcSJacob Faibussowitsch PetscCheck(numFields == patch->nsubspaces, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Mismatch between number of section fields %" PetscInt_FMT " and number of subspaces %" PetscInt_FMT, numFields, patch->nsubspaces); 16199566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->dofSection[0], &pStart, &pEnd)); 16209566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(patch->patchSection, pStart, pEnd)); 16215f824522SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 16225f824522SMatthew G. Knepley PetscInt dof, fdof, f; 16235f824522SMatthew G. Knepley 16249566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->dofSection[0], p, &dof)); 16259566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(patch->patchSection, p, dof)); 16265f824522SMatthew G. Knepley for (f = 0; f < patch->nsubspaces; ++f) { 16279566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldDof(patch->dofSection[0], p, f, &fdof)); 16289566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(patch->patchSection, p, f, fdof)); 16295f824522SMatthew G. Knepley } 16301e5fa6bbSLawrence Mitchell } 16311e5fa6bbSLawrence Mitchell } else { 16321e5fa6bbSLawrence Mitchell PetscInt pStartf, pEndf, f; 16331e5fa6bbSLawrence Mitchell pStart = PETSC_MAX_INT; 16341e5fa6bbSLawrence Mitchell pEnd = PETSC_MIN_INT; 16351e5fa6bbSLawrence Mitchell for (f = 0; f < patch->nsubspaces; ++f) { 16369566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf)); 16371e5fa6bbSLawrence Mitchell pStart = PetscMin(pStart, pStartf); 16381e5fa6bbSLawrence Mitchell pEnd = PetscMax(pEnd, pEndf); 16391e5fa6bbSLawrence Mitchell } 16409566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(patch->patchSection, pStart, pEnd)); 16411e5fa6bbSLawrence Mitchell for (f = 0; f < patch->nsubspaces; ++f) { 16429566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf)); 16431e5fa6bbSLawrence Mitchell for (p = pStartf; p < pEndf; ++p) { 16441e5fa6bbSLawrence Mitchell PetscInt fdof; 16459566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->dofSection[f], p, &fdof)); 16469566063dSJacob Faibussowitsch PetscCall(PetscSectionAddDof(patch->patchSection, p, fdof)); 16479566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(patch->patchSection, p, f, fdof)); 1648bdd9e0cdSPatrick Farrell } 1649bdd9e0cdSPatrick Farrell } 16505f824522SMatthew G. Knepley } 16519566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(patch->patchSection)); 16529566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUseFieldOffsets(patch->patchSection, PETSC_TRUE)); 16534bbf5ea8SMatthew G. Knepley /* Replace cell indices with firedrake-numbered ones. */ 16549566063dSJacob Faibussowitsch PetscCall(ISGeneralSetIndices(cells, numCells, (const PetscInt *)newCellsArray, PETSC_OWN_POINTER)); 16559566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofs, globalDofsArray, PETSC_OWN_POINTER, &patch->gtol)); 16569566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->gtol, "Global Indices")); 16579566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_g2l_view", patch->classname)); 16589566063dSJacob Faibussowitsch PetscCall(PetscSectionViewFromOptions(patch->gtolCounts, (PetscObject)pc, option)); 16599566063dSJacob Faibussowitsch PetscCall(ISViewFromOptions(patch->gtol, (PetscObject)pc, option)); 16609566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArray, PETSC_OWN_POINTER, &patch->dofs)); 16619566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints * Nf, offsArray, PETSC_OWN_POINTER, &patch->offs)); 16620904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 16639566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithArtificial, globalDofsArrayWithArtificial, PETSC_OWN_POINTER, &patch->gtolWithArtificial)); 16649566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithArtificial, PETSC_OWN_POINTER, &patch->dofsWithArtificial)); 16659566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints * Nf, offsArrayWithArtificial, PETSC_OWN_POINTER, &patch->offsWithArtificial)); 1666c2e6f3c0SFlorian Wechsung } 16670904074fSPatrick Farrell if (isNonlinear) { 16689566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithAll, globalDofsArrayWithAll, PETSC_OWN_POINTER, &patch->gtolWithAll)); 16699566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithAll, PETSC_OWN_POINTER, &patch->dofsWithAll)); 16709566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints * Nf, offsArrayWithAll, PETSC_OWN_POINTER, &patch->offsWithAll)); 16710904074fSPatrick Farrell } 16723ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 16734bbf5ea8SMatthew G. Knepley } 16744bbf5ea8SMatthew G. Knepley 1675d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCreateMatrix_Private(PC pc, PetscInt point, Mat *mat, PetscBool withArtificial) 1676d71ae5a4SJacob Faibussowitsch { 16774bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 16784bbf5ea8SMatthew G. Knepley PetscBool flg; 16794bbf5ea8SMatthew G. Knepley PetscInt csize, rsize; 16804bbf5ea8SMatthew G. Knepley const char *prefix = NULL; 16814bbf5ea8SMatthew G. Knepley 16824bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 1683c2e6f3c0SFlorian Wechsung if (withArtificial) { 1684e047a90bSFlorian Wechsung /* would be nice if we could create a rectangular matrix of size numDofsWithArtificial x numDofs here */ 16859d4fc724SLawrence Mitchell PetscInt pStart; 16869566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->gtolCountsWithArtificial, &pStart, NULL)); 16879566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, point + pStart, &rsize)); 16889d4fc724SLawrence Mitchell csize = rsize; 1689ff201f6aSFlorian Wechsung } else { 16909d4fc724SLawrence Mitchell PetscInt pStart; 16919566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, NULL)); 16929566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCounts, point + pStart, &rsize)); 16939d4fc724SLawrence Mitchell csize = rsize; 1694c2e6f3c0SFlorian Wechsung } 1695c2e6f3c0SFlorian Wechsung 16969566063dSJacob Faibussowitsch PetscCall(MatCreate(PETSC_COMM_SELF, mat)); 16979566063dSJacob Faibussowitsch PetscCall(PCGetOptionsPrefix(pc, &prefix)); 16989566063dSJacob Faibussowitsch PetscCall(MatSetOptionsPrefix(*mat, prefix)); 16999566063dSJacob Faibussowitsch PetscCall(MatAppendOptionsPrefix(*mat, "pc_patch_sub_")); 17009566063dSJacob Faibussowitsch if (patch->sub_mat_type) PetscCall(MatSetType(*mat, patch->sub_mat_type)); 17019566063dSJacob Faibussowitsch else if (!patch->sub_mat_type) PetscCall(MatSetType(*mat, MATDENSE)); 17029566063dSJacob Faibussowitsch PetscCall(MatSetSizes(*mat, rsize, csize, rsize, csize)); 17039566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)*mat, MATDENSE, &flg)); 17049566063dSJacob Faibussowitsch if (!flg) PetscCall(PetscObjectTypeCompare((PetscObject)*mat, MATSEQDENSE, &flg)); 17054bbf5ea8SMatthew G. Knepley /* Sparse patch matrices */ 17064bbf5ea8SMatthew G. Knepley if (!flg) { 17074bbf5ea8SMatthew G. Knepley PetscBT bt; 17084bbf5ea8SMatthew G. Knepley PetscInt *dnnz = NULL; 17094bbf5ea8SMatthew G. Knepley const PetscInt *dofsArray = NULL; 17104bbf5ea8SMatthew G. Knepley PetscInt pStart, pEnd, ncell, offset, c, i, j; 17114bbf5ea8SMatthew G. Knepley 1712c2e6f3c0SFlorian Wechsung if (withArtificial) { 17139566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->dofsWithArtificial, &dofsArray)); 1714ff201f6aSFlorian Wechsung } else { 17159566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->dofs, &dofsArray)); 1716c2e6f3c0SFlorian Wechsung } 17179566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd)); 17184bbf5ea8SMatthew G. Knepley point += pStart; 171963a3b9bcSJacob Faibussowitsch PetscCheck(point < pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %" PetscInt_FMT " not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, pStart, pEnd); 17209566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->cellCounts, point, &ncell)); 17219566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->cellCounts, point, &offset)); 17229566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_Prealloc, pc, 0, 0, 0)); 1723b2866507SPatrick Farrell /* A PetscBT uses N^2 bits to store the sparsity pattern on a 17244bbf5ea8SMatthew G. Knepley * patch. This is probably OK if the patches are not too big, 1725b2866507SPatrick Farrell * but uses too much memory. We therefore switch based on rsize. */ 1726b2866507SPatrick Farrell if (rsize < 3000) { /* FIXME: I picked this switch value out of my hat */ 1727d63cebbaSPatrick Farrell PetscScalar *zeroes; 1728d63cebbaSPatrick Farrell PetscInt rows; 1729d63cebbaSPatrick Farrell 17309566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(rsize, &dnnz)); 17319566063dSJacob Faibussowitsch PetscCall(PetscBTCreate(rsize * rsize, &bt)); 17324bbf5ea8SMatthew G. Knepley for (c = 0; c < ncell; ++c) { 17334bbf5ea8SMatthew G. Knepley const PetscInt *idx = dofsArray + (offset + c) * patch->totalDofsPerCell; 17344bbf5ea8SMatthew G. Knepley for (i = 0; i < patch->totalDofsPerCell; ++i) { 17354bbf5ea8SMatthew G. Knepley const PetscInt row = idx[i]; 1736557beb66SLawrence Mitchell if (row < 0) continue; 17374bbf5ea8SMatthew G. Knepley for (j = 0; j < patch->totalDofsPerCell; ++j) { 17384bbf5ea8SMatthew G. Knepley const PetscInt col = idx[j]; 17394bbf5ea8SMatthew G. Knepley const PetscInt key = row * rsize + col; 1740557beb66SLawrence Mitchell if (col < 0) continue; 17414bbf5ea8SMatthew G. Knepley if (!PetscBTLookupSet(bt, key)) ++dnnz[row]; 17424bbf5ea8SMatthew G. Knepley } 17434bbf5ea8SMatthew G. Knepley } 17444bbf5ea8SMatthew G. Knepley } 1745d63cebbaSPatrick Farrell 1746d63cebbaSPatrick Farrell if (patch->usercomputeopintfacet) { 1747d63cebbaSPatrick Farrell const PetscInt *intFacetsArray = NULL; 1748d63cebbaSPatrick Farrell PetscInt i, numIntFacets, intFacetOffset; 1749d63cebbaSPatrick Farrell const PetscInt *facetCells = NULL; 1750d63cebbaSPatrick Farrell 17519566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets)); 17529566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset)); 17539566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells)); 17549566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray)); 1755d63cebbaSPatrick Farrell for (i = 0; i < numIntFacets; i++) { 1756d63cebbaSPatrick Farrell const PetscInt cell0 = facetCells[2 * (intFacetOffset + i) + 0]; 1757d63cebbaSPatrick Farrell const PetscInt cell1 = facetCells[2 * (intFacetOffset + i) + 1]; 1758d63cebbaSPatrick Farrell PetscInt celli, cellj; 1759d63cebbaSPatrick Farrell 1760d63cebbaSPatrick Farrell for (celli = 0; celli < patch->totalDofsPerCell; celli++) { 1761d63cebbaSPatrick Farrell const PetscInt row = dofsArray[(offset + cell0) * patch->totalDofsPerCell + celli]; 1762b5c64f08SPatrick Farrell if (row < 0) continue; 1763d63cebbaSPatrick Farrell for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) { 1764d63cebbaSPatrick Farrell const PetscInt col = dofsArray[(offset + cell1) * patch->totalDofsPerCell + cellj]; 1765d63cebbaSPatrick Farrell const PetscInt key = row * rsize + col; 1766d63cebbaSPatrick Farrell if (col < 0) continue; 1767d63cebbaSPatrick Farrell if (!PetscBTLookupSet(bt, key)) ++dnnz[row]; 1768d63cebbaSPatrick Farrell } 1769d63cebbaSPatrick Farrell } 1770d63cebbaSPatrick Farrell 1771d63cebbaSPatrick Farrell for (celli = 0; celli < patch->totalDofsPerCell; celli++) { 1772d63cebbaSPatrick Farrell const PetscInt row = dofsArray[(offset + cell1) * patch->totalDofsPerCell + celli]; 1773b5c64f08SPatrick Farrell if (row < 0) continue; 1774d63cebbaSPatrick Farrell for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) { 1775d63cebbaSPatrick Farrell const PetscInt col = dofsArray[(offset + cell0) * patch->totalDofsPerCell + cellj]; 1776d63cebbaSPatrick Farrell const PetscInt key = row * rsize + col; 1777d63cebbaSPatrick Farrell if (col < 0) continue; 1778d63cebbaSPatrick Farrell if (!PetscBTLookupSet(bt, key)) ++dnnz[row]; 1779d63cebbaSPatrick Farrell } 1780d63cebbaSPatrick Farrell } 1781d63cebbaSPatrick Farrell } 1782d63cebbaSPatrick Farrell } 17839566063dSJacob Faibussowitsch PetscCall(PetscBTDestroy(&bt)); 17849566063dSJacob Faibussowitsch PetscCall(MatXAIJSetPreallocation(*mat, 1, dnnz, NULL, NULL, NULL)); 17859566063dSJacob Faibussowitsch PetscCall(PetscFree(dnnz)); 1786d63cebbaSPatrick Farrell 17879566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(patch->totalDofsPerCell * patch->totalDofsPerCell, &zeroes)); 1788d63cebbaSPatrick Farrell for (c = 0; c < ncell; ++c) { 1789d63cebbaSPatrick Farrell const PetscInt *idx = &dofsArray[(offset + c) * patch->totalDofsPerCell]; 17909566063dSJacob Faibussowitsch PetscCall(MatSetValues(*mat, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, zeroes, INSERT_VALUES)); 1791d63cebbaSPatrick Farrell } 17929566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(*mat, &rows, NULL)); 179348a46eb9SPierre Jolivet for (i = 0; i < rows; ++i) PetscCall(MatSetValues(*mat, 1, &i, 1, &i, zeroes, INSERT_VALUES)); 1794d63cebbaSPatrick Farrell 1795d63cebbaSPatrick Farrell if (patch->usercomputeopintfacet) { 1796d63cebbaSPatrick Farrell const PetscInt *intFacetsArray = NULL; 1797d63cebbaSPatrick Farrell PetscInt i, numIntFacets, intFacetOffset; 1798d63cebbaSPatrick Farrell const PetscInt *facetCells = NULL; 1799d63cebbaSPatrick Farrell 18009566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets)); 18019566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset)); 18029566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells)); 18039566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray)); 1804d63cebbaSPatrick Farrell for (i = 0; i < numIntFacets; i++) { 1805d63cebbaSPatrick Farrell const PetscInt cell0 = facetCells[2 * (intFacetOffset + i) + 0]; 1806d63cebbaSPatrick Farrell const PetscInt cell1 = facetCells[2 * (intFacetOffset + i) + 1]; 1807d63cebbaSPatrick Farrell const PetscInt *cell0idx = &dofsArray[(offset + cell0) * patch->totalDofsPerCell]; 1808d63cebbaSPatrick Farrell const PetscInt *cell1idx = &dofsArray[(offset + cell1) * patch->totalDofsPerCell]; 18099566063dSJacob Faibussowitsch PetscCall(MatSetValues(*mat, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, zeroes, INSERT_VALUES)); 18109566063dSJacob Faibussowitsch PetscCall(MatSetValues(*mat, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, zeroes, INSERT_VALUES)); 1811d63cebbaSPatrick Farrell } 1812d63cebbaSPatrick Farrell } 1813d63cebbaSPatrick Farrell 18149566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY)); 18159566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY)); 1816d63cebbaSPatrick Farrell 18179566063dSJacob Faibussowitsch PetscCall(PetscFree(zeroes)); 1818d63cebbaSPatrick Farrell 1819b2866507SPatrick Farrell } else { /* rsize too big, use MATPREALLOCATOR */ 1820b2866507SPatrick Farrell Mat preallocator; 1821b2866507SPatrick Farrell PetscScalar *vals; 1822b2866507SPatrick Farrell 18239566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(patch->totalDofsPerCell * patch->totalDofsPerCell, &vals)); 18249566063dSJacob Faibussowitsch PetscCall(MatCreate(PETSC_COMM_SELF, &preallocator)); 18259566063dSJacob Faibussowitsch PetscCall(MatSetType(preallocator, MATPREALLOCATOR)); 18269566063dSJacob Faibussowitsch PetscCall(MatSetSizes(preallocator, rsize, rsize, rsize, rsize)); 18279566063dSJacob Faibussowitsch PetscCall(MatSetUp(preallocator)); 182811bcd083SPatrick Farrell 1829b2866507SPatrick Farrell for (c = 0; c < ncell; ++c) { 1830b2866507SPatrick Farrell const PetscInt *idx = dofsArray + (offset + c) * patch->totalDofsPerCell; 18319566063dSJacob Faibussowitsch PetscCall(MatSetValues(preallocator, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, vals, INSERT_VALUES)); 1832b2866507SPatrick Farrell } 183311bcd083SPatrick Farrell 183411bcd083SPatrick Farrell if (patch->usercomputeopintfacet) { 183511bcd083SPatrick Farrell const PetscInt *intFacetsArray = NULL; 183611bcd083SPatrick Farrell PetscInt i, numIntFacets, intFacetOffset; 183711bcd083SPatrick Farrell const PetscInt *facetCells = NULL; 183811bcd083SPatrick Farrell 18399566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets)); 18409566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset)); 18419566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells)); 18429566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray)); 184311bcd083SPatrick Farrell for (i = 0; i < numIntFacets; i++) { 184411bcd083SPatrick Farrell const PetscInt cell0 = facetCells[2 * (intFacetOffset + i) + 0]; 184511bcd083SPatrick Farrell const PetscInt cell1 = facetCells[2 * (intFacetOffset + i) + 1]; 184611bcd083SPatrick Farrell const PetscInt *cell0idx = &dofsArray[(offset + cell0) * patch->totalDofsPerCell]; 184711bcd083SPatrick Farrell const PetscInt *cell1idx = &dofsArray[(offset + cell1) * patch->totalDofsPerCell]; 18489566063dSJacob Faibussowitsch PetscCall(MatSetValues(preallocator, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, vals, INSERT_VALUES)); 18499566063dSJacob Faibussowitsch PetscCall(MatSetValues(preallocator, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, vals, INSERT_VALUES)); 185011bcd083SPatrick Farrell } 185111bcd083SPatrick Farrell } 185211bcd083SPatrick Farrell 18539566063dSJacob Faibussowitsch PetscCall(PetscFree(vals)); 18549566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY)); 18559566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY)); 18569566063dSJacob Faibussowitsch PetscCall(MatPreallocatorPreallocate(preallocator, PETSC_TRUE, *mat)); 18579566063dSJacob Faibussowitsch PetscCall(MatDestroy(&preallocator)); 1858b2866507SPatrick Farrell } 18599566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_Prealloc, pc, 0, 0, 0)); 1860fe117d09SFlorian Wechsung if (withArtificial) { 18619566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->dofsWithArtificial, &dofsArray)); 1862fe117d09SFlorian Wechsung } else { 18639566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->dofs, &dofsArray)); 18644bbf5ea8SMatthew G. Knepley } 1865fe117d09SFlorian Wechsung } 18669566063dSJacob Faibussowitsch PetscCall(MatSetUp(*mat)); 18673ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 18684bbf5ea8SMatthew G. Knepley } 18694bbf5ea8SMatthew G. Knepley 1870d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchComputeFunction_DMPlex_Private(PC pc, PetscInt patchNum, Vec x, Vec F, IS cellIS, PetscInt n, const PetscInt *l2p, const PetscInt *l2pWithAll, void *ctx) 1871d71ae5a4SJacob Faibussowitsch { 187292d50984SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 1873b6bb21d1SLawrence Mitchell DM dm, plex; 187492d50984SMatthew G. Knepley PetscSection s; 187592d50984SMatthew G. Knepley const PetscInt *parray, *oarray; 187692d50984SMatthew G. Knepley PetscInt Nf = patch->nsubspaces, Np, poff, p, f; 187792d50984SMatthew G. Knepley 187892d50984SMatthew G. Knepley PetscFunctionBegin; 187928b400f6SJacob Faibussowitsch PetscCheck(!patch->precomputeElementTensors, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Precomputing element tensors not implemented with DMPlex compute operator"); 18809566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm)); 18819566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex)); 1882b6bb21d1SLawrence Mitchell dm = plex; 18839566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, &s)); 188492d50984SMatthew G. Knepley /* Set offset into patch */ 18859566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->pointCounts, patchNum, &Np)); 18869566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->pointCounts, patchNum, &poff)); 18879566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->points, &parray)); 18889566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->offs, &oarray)); 188992d50984SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 189092d50984SMatthew G. Knepley for (p = 0; p < Np; ++p) { 189192d50984SMatthew G. Knepley const PetscInt point = parray[poff + p]; 189292d50984SMatthew G. Knepley PetscInt dof; 189392d50984SMatthew G. Knepley 18949566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldDof(patch->patchSection, point, f, &dof)); 18959566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff + p) * Nf + f])); 18969566063dSJacob Faibussowitsch if (patch->nsubspaces == 1) PetscCall(PetscSectionSetOffset(patch->patchSection, point, oarray[(poff + p) * Nf + f])); 18979566063dSJacob Faibussowitsch else PetscCall(PetscSectionSetOffset(patch->patchSection, point, -1)); 189892d50984SMatthew G. Knepley } 189992d50984SMatthew G. Knepley } 19009566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->points, &parray)); 19019566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->offs, &oarray)); 19029566063dSJacob Faibussowitsch if (patch->viewSection) PetscCall(ObjectView((PetscObject)patch->patchSection, patch->viewerSection, patch->formatSection)); 19039566063dSJacob Faibussowitsch PetscCall(DMPlexComputeResidual_Patch_Internal(dm, patch->patchSection, cellIS, 0.0, x, NULL, F, ctx)); 19049566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm)); 19053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 190692d50984SMatthew G. Knepley } 190792d50984SMatthew G. Knepley 1908d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchComputeFunction_Internal(PC pc, Vec x, Vec F, PetscInt point) 1909d71ae5a4SJacob Faibussowitsch { 191092d50984SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 191192d50984SMatthew G. Knepley const PetscInt *dofsArray; 19120904074fSPatrick Farrell const PetscInt *dofsArrayWithAll; 191392d50984SMatthew G. Knepley const PetscInt *cellsArray; 191492d50984SMatthew G. Knepley PetscInt ncell, offset, pStart, pEnd; 191592d50984SMatthew G. Knepley 191692d50984SMatthew G. Knepley PetscFunctionBegin; 19179566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0)); 1918ef1023bdSBarry Smith PetscCheck(patch->usercomputeop, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set callback"); 19199566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->dofs, &dofsArray)); 19209566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll)); 19219566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->cells, &cellsArray)); 19229566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd)); 192392d50984SMatthew G. Knepley 192492d50984SMatthew G. Knepley point += pStart; 192563a3b9bcSJacob Faibussowitsch PetscCheck(point < pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %" PetscInt_FMT " not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, pStart, pEnd); 192692d50984SMatthew G. Knepley 19279566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->cellCounts, point, &ncell)); 19289566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->cellCounts, point, &offset)); 192992d50984SMatthew G. Knepley if (ncell <= 0) { 19309566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 19313ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 193292d50984SMatthew G. Knepley } 19339566063dSJacob Faibussowitsch PetscCall(VecSet(F, 0.0)); 193492d50984SMatthew G. Knepley /* Cannot reuse the same IS because the geometry info is being cached in it */ 19359566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS)); 1936792fecdfSBarry Smith PetscCallBack("PCPatch callback", patch->usercomputef(pc, point, x, F, patch->cellIS, ncell * patch->totalDofsPerCell, dofsArray + offset * patch->totalDofsPerCell, dofsArrayWithAll + offset * patch->totalDofsPerCell, patch->usercomputefctx)); 19379566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->cellIS)); 19389566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->dofs, &dofsArray)); 19399566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll)); 19409566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->cells, &cellsArray)); 194192d50984SMatthew G. Knepley if (patch->viewMatrix) { 194292d50984SMatthew G. Knepley char name[PETSC_MAX_PATH_LEN]; 194392d50984SMatthew G. Knepley 194463a3b9bcSJacob Faibussowitsch PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN - 1, "Patch vector for Point %" PetscInt_FMT, point)); 19459566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)F, name)); 19469566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject)F, patch->viewerMatrix, patch->formatMatrix)); 194792d50984SMatthew G. Knepley } 19489566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 19493ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 195092d50984SMatthew G. Knepley } 195192d50984SMatthew G. Knepley 1952d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchComputeOperator_DMPlex_Private(PC pc, PetscInt patchNum, Vec x, Mat J, IS cellIS, PetscInt n, const PetscInt *l2p, const PetscInt *l2pWithAll, void *ctx) 1953d71ae5a4SJacob Faibussowitsch { 19545f824522SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 1955b6bb21d1SLawrence Mitchell DM dm, plex; 19565f824522SMatthew G. Knepley PetscSection s; 19575f824522SMatthew G. Knepley const PetscInt *parray, *oarray; 19585f824522SMatthew G. Knepley PetscInt Nf = patch->nsubspaces, Np, poff, p, f; 19595f824522SMatthew G. Knepley 19605f824522SMatthew G. Knepley PetscFunctionBegin; 19619566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm)); 19629566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex)); 1963b6bb21d1SLawrence Mitchell dm = plex; 19649566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, &s)); 19655f824522SMatthew G. Knepley /* Set offset into patch */ 19669566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->pointCounts, patchNum, &Np)); 19679566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->pointCounts, patchNum, &poff)); 19689566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->points, &parray)); 19699566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->offs, &oarray)); 19705f824522SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 19715f824522SMatthew G. Knepley for (p = 0; p < Np; ++p) { 19725f824522SMatthew G. Knepley const PetscInt point = parray[poff + p]; 19735f824522SMatthew G. Knepley PetscInt dof; 19745f824522SMatthew G. Knepley 19759566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldDof(patch->patchSection, point, f, &dof)); 19769566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff + p) * Nf + f])); 19779566063dSJacob Faibussowitsch if (patch->nsubspaces == 1) PetscCall(PetscSectionSetOffset(patch->patchSection, point, oarray[(poff + p) * Nf + f])); 19789566063dSJacob Faibussowitsch else PetscCall(PetscSectionSetOffset(patch->patchSection, point, -1)); 19795f824522SMatthew G. Knepley } 19805f824522SMatthew G. Knepley } 19819566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->points, &parray)); 19829566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->offs, &oarray)); 19839566063dSJacob Faibussowitsch if (patch->viewSection) PetscCall(ObjectView((PetscObject)patch->patchSection, patch->viewerSection, patch->formatSection)); 19845f824522SMatthew G. Knepley /* TODO Shut off MatViewFromOptions() in MatAssemblyEnd() here */ 19859566063dSJacob Faibussowitsch PetscCall(DMPlexComputeJacobian_Patch_Internal(dm, patch->patchSection, patch->patchSection, cellIS, 0.0, 0.0, x, NULL, J, J, ctx)); 19869566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm)); 19873ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 19885f824522SMatthew G. Knepley } 19895f824522SMatthew G. Knepley 1990a685ae26SLawrence Mitchell /* This function zeros mat on entry */ 1991d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchComputeOperator_Internal(PC pc, Vec x, Mat mat, PetscInt point, PetscBool withArtificial) 1992d71ae5a4SJacob Faibussowitsch { 19934bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 19944bbf5ea8SMatthew G. Knepley const PetscInt *dofsArray; 19950904074fSPatrick Farrell const PetscInt *dofsArrayWithAll = NULL; 19964bbf5ea8SMatthew G. Knepley const PetscInt *cellsArray; 1997eb62eeaaSLawrence Mitchell PetscInt ncell, offset, pStart, pEnd, numIntFacets, intFacetOffset; 19984d04e9f1SPatrick Farrell PetscBool isNonlinear; 19994bbf5ea8SMatthew G. Knepley 20004bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 20019566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0)); 2002debbdec3SPatrick Farrell isNonlinear = patch->isNonlinear; 2003ef1023bdSBarry Smith PetscCheck(patch->usercomputeop, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set callback"); 2004c2e6f3c0SFlorian Wechsung if (withArtificial) { 20059566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->dofsWithArtificial, &dofsArray)); 2006c2e6f3c0SFlorian Wechsung } else { 20079566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->dofs, &dofsArray)); 2008c2e6f3c0SFlorian Wechsung } 200948a46eb9SPierre Jolivet if (isNonlinear) PetscCall(ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll)); 20109566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->cells, &cellsArray)); 20119566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd)); 20124bbf5ea8SMatthew G. Knepley 20134bbf5ea8SMatthew G. Knepley point += pStart; 201463a3b9bcSJacob Faibussowitsch PetscCheck(point < pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %" PetscInt_FMT " not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, pStart, pEnd); 20154bbf5ea8SMatthew G. Knepley 20169566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->cellCounts, point, &ncell)); 20179566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->cellCounts, point, &offset)); 20184bbf5ea8SMatthew G. Knepley if (ncell <= 0) { 20199566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 20203ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 20214bbf5ea8SMatthew G. Knepley } 20229566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(mat)); 2023fa84ea4cSLawrence Mitchell if (patch->precomputeElementTensors) { 2024fa84ea4cSLawrence Mitchell PetscInt i; 2025fa84ea4cSLawrence Mitchell PetscInt ndof = patch->totalDofsPerCell; 2026fa84ea4cSLawrence Mitchell const PetscScalar *elementTensors; 2027fa84ea4cSLawrence Mitchell 20289566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(patch->cellMats, &elementTensors)); 2029fa84ea4cSLawrence Mitchell for (i = 0; i < ncell; i++) { 2030fa84ea4cSLawrence Mitchell const PetscInt cell = cellsArray[i + offset]; 2031fa84ea4cSLawrence Mitchell const PetscInt *idx = dofsArray + (offset + i) * ndof; 2032fe988be2SFlorian Wechsung const PetscScalar *v = elementTensors + patch->precomputedTensorLocations[cell] * ndof * ndof; 20339566063dSJacob Faibussowitsch PetscCall(MatSetValues(mat, ndof, idx, ndof, idx, v, ADD_VALUES)); 2034fa84ea4cSLawrence Mitchell } 20359566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(patch->cellMats, &elementTensors)); 20369566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY)); 20379566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY)); 2038fa84ea4cSLawrence Mitchell } else { 20392aa6f319SMatthew G. Knepley /* Cannot reuse the same IS because the geometry info is being cached in it */ 20409566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS)); 20419371c9d4SSatish Balay PetscCallBack("PCPatch callback", 20429371c9d4SSatish Balay patch->usercomputeop(pc, point, x, mat, patch->cellIS, ncell * patch->totalDofsPerCell, dofsArray + offset * patch->totalDofsPerCell, dofsArrayWithAll ? dofsArrayWithAll + offset * patch->totalDofsPerCell : NULL, patch->usercomputeopctx)); 2043fa84ea4cSLawrence Mitchell } 204459109abcSLawrence Mitchell if (patch->usercomputeopintfacet) { 20459566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets)); 20469566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset)); 2047eb62eeaaSLawrence Mitchell if (numIntFacets > 0) { 2048eb62eeaaSLawrence Mitchell /* For each interior facet, grab the two cells (in local numbering, and concatenate dof numberings for those cells) */ 2049eb62eeaaSLawrence Mitchell PetscInt *facetDofs = NULL, *facetDofsWithAll = NULL; 2050eb62eeaaSLawrence Mitchell const PetscInt *intFacetsArray = NULL; 2051eb62eeaaSLawrence Mitchell PetscInt idx = 0; 2052eb62eeaaSLawrence Mitchell PetscInt i, c, d; 2053de2d1767SPatrick Farrell PetscInt fStart; 2054b6bb21d1SLawrence Mitchell DM dm, plex; 2055eb62eeaaSLawrence Mitchell IS facetIS = NULL; 2056eb62eeaaSLawrence Mitchell const PetscInt *facetCells = NULL; 20577a50e09dSPatrick Farrell 20589566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells)); 20599566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray)); 20609566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm)); 20619566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex)); 2062b6bb21d1SLawrence Mitchell dm = plex; 20639566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, NULL)); 2064eb62eeaaSLawrence Mitchell /* FIXME: Pull this malloc out. */ 20659566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofs)); 206648a46eb9SPierre Jolivet if (dofsArrayWithAll) PetscCall(PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofsWithAll)); 2067f98464cbSLawrence Mitchell if (patch->precomputeElementTensors) { 2068f98464cbSLawrence Mitchell PetscInt nFacetDof = 2 * patch->totalDofsPerCell; 2069f98464cbSLawrence Mitchell const PetscScalar *elementTensors; 2070f98464cbSLawrence Mitchell 20719566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(patch->intFacetMats, &elementTensors)); 2072f98464cbSLawrence Mitchell 2073f98464cbSLawrence Mitchell for (i = 0; i < numIntFacets; i++) { 2074f98464cbSLawrence Mitchell const PetscInt facet = intFacetsArray[i + intFacetOffset]; 2075de2d1767SPatrick Farrell const PetscScalar *v = elementTensors + patch->precomputedIntFacetTensorLocations[facet - fStart] * nFacetDof * nFacetDof; 2076f98464cbSLawrence Mitchell idx = 0; 2077f98464cbSLawrence Mitchell /* 2078f1580f4eSBarry Smith 0--1 2079f1580f4eSBarry Smith |\-| 2080f1580f4eSBarry Smith |+\| 2081f1580f4eSBarry Smith 2--3 2082f1580f4eSBarry Smith [0, 2, 3, 0, 1, 3] 2083f98464cbSLawrence Mitchell */ 2084f98464cbSLawrence Mitchell for (c = 0; c < 2; c++) { 2085f98464cbSLawrence Mitchell const PetscInt cell = facetCells[2 * (intFacetOffset + i) + c]; 2086f98464cbSLawrence Mitchell for (d = 0; d < patch->totalDofsPerCell; d++) { 2087f98464cbSLawrence Mitchell facetDofs[idx] = dofsArray[(offset + cell) * patch->totalDofsPerCell + d]; 2088f98464cbSLawrence Mitchell idx++; 2089f98464cbSLawrence Mitchell } 2090f98464cbSLawrence Mitchell } 20919566063dSJacob Faibussowitsch PetscCall(MatSetValues(mat, nFacetDof, facetDofs, nFacetDof, facetDofs, v, ADD_VALUES)); 2092f98464cbSLawrence Mitchell } 20939566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(patch->intFacetMats, &elementTensors)); 2094f98464cbSLawrence Mitchell } else { 2095eb62eeaaSLawrence Mitchell /* 2096f1580f4eSBarry Smith 0--1 2097f1580f4eSBarry Smith |\-| 2098f1580f4eSBarry Smith |+\| 2099f1580f4eSBarry Smith 2--3 2100f1580f4eSBarry Smith [0, 2, 3, 0, 1, 3] 2101eb62eeaaSLawrence Mitchell */ 2102eb62eeaaSLawrence Mitchell for (i = 0; i < numIntFacets; i++) { 2103eb62eeaaSLawrence Mitchell for (c = 0; c < 2; c++) { 2104eb62eeaaSLawrence Mitchell const PetscInt cell = facetCells[2 * (intFacetOffset + i) + c]; 2105eb62eeaaSLawrence Mitchell for (d = 0; d < patch->totalDofsPerCell; d++) { 2106eb62eeaaSLawrence Mitchell facetDofs[idx] = dofsArray[(offset + cell) * patch->totalDofsPerCell + d]; 2107ad540459SPierre Jolivet if (dofsArrayWithAll) facetDofsWithAll[idx] = dofsArrayWithAll[(offset + cell) * patch->totalDofsPerCell + d]; 2108eb62eeaaSLawrence Mitchell idx++; 2109eb62eeaaSLawrence Mitchell } 2110eb62eeaaSLawrence Mitchell } 2111eb62eeaaSLawrence Mitchell } 21129566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray + intFacetOffset, PETSC_USE_POINTER, &facetIS)); 21139566063dSJacob Faibussowitsch PetscCall(patch->usercomputeopintfacet(pc, point, x, mat, facetIS, 2 * numIntFacets * patch->totalDofsPerCell, facetDofs, facetDofsWithAll, patch->usercomputeopintfacetctx)); 21149566063dSJacob Faibussowitsch PetscCall(ISDestroy(&facetIS)); 2115f98464cbSLawrence Mitchell } 21169566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->intFacetsToPatchCell, &facetCells)); 21179566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->intFacets, &intFacetsArray)); 21189566063dSJacob Faibussowitsch PetscCall(PetscFree(facetDofs)); 21199566063dSJacob Faibussowitsch PetscCall(PetscFree(facetDofsWithAll)); 21209566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm)); 2121eb62eeaaSLawrence Mitchell } 212259109abcSLawrence Mitchell } 21236710cc29SPatrick Farrell 21249566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY)); 21259566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY)); 21266710cc29SPatrick Farrell 2127c73d2cf6SLawrence Mitchell if (!(withArtificial || isNonlinear) && patch->denseinverse) { 2128c73d2cf6SLawrence Mitchell MatFactorInfo info; 2129c73d2cf6SLawrence Mitchell PetscBool flg; 21309566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)mat, MATSEQDENSE, &flg)); 213128b400f6SJacob Faibussowitsch PetscCheck(flg, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Invalid Mat type for dense inverse"); 21329566063dSJacob Faibussowitsch PetscCall(MatFactorInfoInitialize(&info)); 21339566063dSJacob Faibussowitsch PetscCall(MatLUFactor(mat, NULL, NULL, &info)); 21349566063dSJacob Faibussowitsch PetscCall(MatSeqDenseInvertFactors_Private(mat)); 2135c73d2cf6SLawrence Mitchell } 21369566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->cellIS)); 21374d04e9f1SPatrick Farrell if (withArtificial) { 21389566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->dofsWithArtificial, &dofsArray)); 2139c2e6f3c0SFlorian Wechsung } else { 21409566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->dofs, &dofsArray)); 2141c2e6f3c0SFlorian Wechsung } 214248a46eb9SPierre Jolivet if (isNonlinear) PetscCall(ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll)); 21439566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->cells, &cellsArray)); 21442aa6f319SMatthew G. Knepley if (patch->viewMatrix) { 21452aa6f319SMatthew G. Knepley char name[PETSC_MAX_PATH_LEN]; 21462aa6f319SMatthew G. Knepley 214763a3b9bcSJacob Faibussowitsch PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN - 1, "Patch matrix for Point %" PetscInt_FMT, point)); 21489566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)mat, name)); 21499566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject)mat, patch->viewerMatrix, patch->formatMatrix)); 21502aa6f319SMatthew G. Knepley } 21519566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 21523ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 21534bbf5ea8SMatthew G. Knepley } 21544bbf5ea8SMatthew G. Knepley 2155d71ae5a4SJacob Faibussowitsch static PetscErrorCode MatSetValues_PCPatch_Private(Mat mat, PetscInt m, const PetscInt idxm[], PetscInt n, const PetscInt idxn[], const PetscScalar *v, InsertMode addv) 2156d71ae5a4SJacob Faibussowitsch { 2157fa84ea4cSLawrence Mitchell Vec data; 2158fa84ea4cSLawrence Mitchell PetscScalar *array; 2159fe988be2SFlorian Wechsung PetscInt bs, nz, i, j, cell; 2160fa84ea4cSLawrence Mitchell 21619566063dSJacob Faibussowitsch PetscCall(MatShellGetContext(mat, &data)); 21629566063dSJacob Faibussowitsch PetscCall(VecGetBlockSize(data, &bs)); 21639566063dSJacob Faibussowitsch PetscCall(VecGetSize(data, &nz)); 21649566063dSJacob Faibussowitsch PetscCall(VecGetArray(data, &array)); 216508401ef6SPierre Jolivet PetscCheck(m == n, PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Only for square insertion"); 216633cbca70SPatrick Farrell cell = (PetscInt)(idxm[0] / bs); /* use the fact that this is called once per cell */ 2167fa84ea4cSLawrence Mitchell for (i = 0; i < m; i++) { 216808401ef6SPierre Jolivet PetscCheck(idxm[i] == idxn[i], PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Row and column indices must match!"); 2169fa84ea4cSLawrence Mitchell for (j = 0; j < n; j++) { 2170fa84ea4cSLawrence Mitchell const PetscScalar v_ = v[i * bs + j]; 2171fa84ea4cSLawrence Mitchell /* Indexing is special to the data structure we have! */ 2172fa84ea4cSLawrence Mitchell if (addv == INSERT_VALUES) { 2173fe988be2SFlorian Wechsung array[cell * bs * bs + i * bs + j] = v_; 2174fa84ea4cSLawrence Mitchell } else { 2175fe988be2SFlorian Wechsung array[cell * bs * bs + i * bs + j] += v_; 2176fa84ea4cSLawrence Mitchell } 2177fa84ea4cSLawrence Mitchell } 2178fa84ea4cSLawrence Mitchell } 21799566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(data, &array)); 21803ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2181fa84ea4cSLawrence Mitchell } 2182fa84ea4cSLawrence Mitchell 2183d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchPrecomputePatchTensors_Private(PC pc) 2184d71ae5a4SJacob Faibussowitsch { 2185fa84ea4cSLawrence Mitchell PC_PATCH *patch = (PC_PATCH *)pc->data; 2186fa84ea4cSLawrence Mitchell const PetscInt *cellsArray; 2187fa84ea4cSLawrence Mitchell PetscInt ncell, offset; 2188fa84ea4cSLawrence Mitchell const PetscInt *dofMapArray; 2189fa84ea4cSLawrence Mitchell PetscInt i, j; 2190fa84ea4cSLawrence Mitchell IS dofMap; 2191fa84ea4cSLawrence Mitchell IS cellIS; 2192fa84ea4cSLawrence Mitchell const PetscInt ndof = patch->totalDofsPerCell; 2193fa84ea4cSLawrence Mitchell Mat vecMat; 2194fe988be2SFlorian Wechsung PetscInt cStart, cEnd; 2195fe988be2SFlorian Wechsung DM dm, plex; 2196fe988be2SFlorian Wechsung 21979566063dSJacob Faibussowitsch PetscCall(ISGetSize(patch->cells, &ncell)); 2198e9c2c94bSFlorian Wechsung if (!ncell) { /* No cells to assemble over -> skip */ 21993ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2200e9c2c94bSFlorian Wechsung } 2201e9c2c94bSFlorian Wechsung 22029566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0)); 2203fa84ea4cSLawrence Mitchell 22049566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm)); 22059566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex)); 2206b6bb21d1SLawrence Mitchell dm = plex; 2207fa84ea4cSLawrence Mitchell if (!patch->allCells) { 2208fa84ea4cSLawrence Mitchell PetscHSetI cells; 2209fa84ea4cSLawrence Mitchell PetscHashIter hi; 2210fa84ea4cSLawrence Mitchell PetscInt pStart, pEnd; 2211fa84ea4cSLawrence Mitchell PetscInt *allCells = NULL; 22129566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&cells)); 22139566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->cells, &cellsArray)); 22149566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd)); 2215fa84ea4cSLawrence Mitchell for (i = pStart; i < pEnd; i++) { 22169566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->cellCounts, i, &ncell)); 22179566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->cellCounts, i, &offset)); 2218fa84ea4cSLawrence Mitchell if (ncell <= 0) continue; 221948a46eb9SPierre Jolivet for (j = 0; j < ncell; j++) PetscCall(PetscHSetIAdd(cells, cellsArray[offset + j])); 2220fa84ea4cSLawrence Mitchell } 22219566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->cells, &cellsArray)); 22229566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(cells, &ncell)); 22239566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(ncell, &allCells)); 22249566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 22259566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(cEnd - cStart, &patch->precomputedTensorLocations)); 2226fa84ea4cSLawrence Mitchell i = 0; 2227fa84ea4cSLawrence Mitchell PetscHashIterBegin(cells, hi); 2228fa84ea4cSLawrence Mitchell while (!PetscHashIterAtEnd(cells, hi)) { 2229fe988be2SFlorian Wechsung PetscHashIterGetKey(cells, hi, allCells[i]); 2230fe988be2SFlorian Wechsung patch->precomputedTensorLocations[allCells[i]] = i; 2231fa84ea4cSLawrence Mitchell PetscHashIterNext(cells, hi); 2232fe988be2SFlorian Wechsung i++; 2233fa84ea4cSLawrence Mitchell } 22349566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&cells)); 22359566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, allCells, PETSC_OWN_POINTER, &patch->allCells)); 2236fa84ea4cSLawrence Mitchell } 22379566063dSJacob Faibussowitsch PetscCall(ISGetSize(patch->allCells, &ncell)); 2238fa84ea4cSLawrence Mitchell if (!patch->cellMats) { 22399566063dSJacob Faibussowitsch PetscCall(VecCreateSeq(PETSC_COMM_SELF, ncell * ndof * ndof, &patch->cellMats)); 22409566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(patch->cellMats, ndof)); 2241fa84ea4cSLawrence Mitchell } 22429566063dSJacob Faibussowitsch PetscCall(VecSet(patch->cellMats, 0)); 2243fa84ea4cSLawrence Mitchell 2244d0609cedSBarry Smith PetscCall(MatCreateShell(PETSC_COMM_SELF, ncell * ndof, ncell * ndof, ncell * ndof, ncell * ndof, (void *)patch->cellMats, &vecMat)); 22459566063dSJacob Faibussowitsch PetscCall(MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void (*)(void)) & MatSetValues_PCPatch_Private)); 22469566063dSJacob Faibussowitsch PetscCall(ISGetSize(patch->allCells, &ncell)); 22479566063dSJacob Faibussowitsch PetscCall(ISCreateStride(PETSC_COMM_SELF, ndof * ncell, 0, 1, &dofMap)); 22489566063dSJacob Faibussowitsch PetscCall(ISGetIndices(dofMap, &dofMapArray)); 22499566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->allCells, &cellsArray)); 22509566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray, PETSC_USE_POINTER, &cellIS)); 2251fa84ea4cSLawrence Mitchell /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */ 2252792fecdfSBarry Smith PetscCallBack("PCPatch callback", patch->usercomputeop(pc, -1, NULL, vecMat, cellIS, ndof * ncell, dofMapArray, NULL, patch->usercomputeopctx)); 22539566063dSJacob Faibussowitsch PetscCall(ISDestroy(&cellIS)); 22549566063dSJacob Faibussowitsch PetscCall(MatDestroy(&vecMat)); 22559566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->allCells, &cellsArray)); 22569566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(dofMap, &dofMapArray)); 22579566063dSJacob Faibussowitsch PetscCall(ISDestroy(&dofMap)); 2258f98464cbSLawrence Mitchell 2259f98464cbSLawrence Mitchell if (patch->usercomputeopintfacet) { 2260f98464cbSLawrence Mitchell PetscInt nIntFacets; 2261f98464cbSLawrence Mitchell IS intFacetsIS; 2262f98464cbSLawrence Mitchell const PetscInt *intFacetsArray = NULL; 2263f98464cbSLawrence Mitchell if (!patch->allIntFacets) { 2264f98464cbSLawrence Mitchell PetscHSetI facets; 2265f98464cbSLawrence Mitchell PetscHashIter hi; 2266f98464cbSLawrence Mitchell PetscInt pStart, pEnd, fStart, fEnd; 2267f98464cbSLawrence Mitchell PetscInt *allIntFacets = NULL; 22689566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&facets)); 22699566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray)); 22709566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->intFacetCounts, &pStart, &pEnd)); 22719566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd)); 2272f98464cbSLawrence Mitchell for (i = pStart; i < pEnd; i++) { 22739566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->intFacetCounts, i, &nIntFacets)); 22749566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->intFacetCounts, i, &offset)); 2275f98464cbSLawrence Mitchell if (nIntFacets <= 0) continue; 227648a46eb9SPierre Jolivet for (j = 0; j < nIntFacets; j++) PetscCall(PetscHSetIAdd(facets, intFacetsArray[offset + j])); 2277f98464cbSLawrence Mitchell } 22789566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->intFacets, &intFacetsArray)); 22799566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(facets, &nIntFacets)); 22809566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nIntFacets, &allIntFacets)); 22819566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(fEnd - fStart, &patch->precomputedIntFacetTensorLocations)); 2282f98464cbSLawrence Mitchell i = 0; 2283f98464cbSLawrence Mitchell PetscHashIterBegin(facets, hi); 2284f98464cbSLawrence Mitchell while (!PetscHashIterAtEnd(facets, hi)) { 2285f98464cbSLawrence Mitchell PetscHashIterGetKey(facets, hi, allIntFacets[i]); 2286de2d1767SPatrick Farrell patch->precomputedIntFacetTensorLocations[allIntFacets[i] - fStart] = i; 2287f98464cbSLawrence Mitchell PetscHashIterNext(facets, hi); 2288f98464cbSLawrence Mitchell i++; 2289f98464cbSLawrence Mitchell } 22909566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&facets)); 22919566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, allIntFacets, PETSC_OWN_POINTER, &patch->allIntFacets)); 2292f98464cbSLawrence Mitchell } 22939566063dSJacob Faibussowitsch PetscCall(ISGetSize(patch->allIntFacets, &nIntFacets)); 2294f98464cbSLawrence Mitchell if (!patch->intFacetMats) { 22959566063dSJacob Faibussowitsch PetscCall(VecCreateSeq(PETSC_COMM_SELF, nIntFacets * ndof * ndof * 4, &patch->intFacetMats)); 22969566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(patch->intFacetMats, ndof * 2)); 2297f98464cbSLawrence Mitchell } 22989566063dSJacob Faibussowitsch PetscCall(VecSet(patch->intFacetMats, 0)); 2299f98464cbSLawrence Mitchell 2300d0609cedSBarry Smith PetscCall(MatCreateShell(PETSC_COMM_SELF, nIntFacets * ndof * 2, nIntFacets * ndof * 2, nIntFacets * ndof * 2, nIntFacets * ndof * 2, (void *)patch->intFacetMats, &vecMat)); 23019566063dSJacob Faibussowitsch PetscCall(MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void (*)(void)) & MatSetValues_PCPatch_Private)); 23029566063dSJacob Faibussowitsch PetscCall(ISCreateStride(PETSC_COMM_SELF, 2 * ndof * nIntFacets, 0, 1, &dofMap)); 23039566063dSJacob Faibussowitsch PetscCall(ISGetIndices(dofMap, &dofMapArray)); 23049566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->allIntFacets, &intFacetsArray)); 23059566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, intFacetsArray, PETSC_USE_POINTER, &intFacetsIS)); 2306f98464cbSLawrence Mitchell /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */ 2307792fecdfSBarry Smith PetscCallBack("PCPatch callback (interior facets)", patch->usercomputeopintfacet(pc, -1, NULL, vecMat, intFacetsIS, 2 * ndof * nIntFacets, dofMapArray, NULL, patch->usercomputeopintfacetctx)); 23089566063dSJacob Faibussowitsch PetscCall(ISDestroy(&intFacetsIS)); 23099566063dSJacob Faibussowitsch PetscCall(MatDestroy(&vecMat)); 23109566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->allIntFacets, &intFacetsArray)); 23119566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(dofMap, &dofMapArray)); 23129566063dSJacob Faibussowitsch PetscCall(ISDestroy(&dofMap)); 2313f98464cbSLawrence Mitchell } 23149566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm)); 23159566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 2316fa84ea4cSLawrence Mitchell 23173ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2318fa84ea4cSLawrence Mitchell } 2319fa84ea4cSLawrence Mitchell 2320d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatch_ScatterLocal_Private(PC pc, PetscInt p, Vec x, Vec y, InsertMode mode, ScatterMode scat, PatchScatterType scattertype) 2321d71ae5a4SJacob Faibussowitsch { 23224bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 23234bbf5ea8SMatthew G. Knepley const PetscScalar *xArray = NULL; 23244bbf5ea8SMatthew G. Knepley PetscScalar *yArray = NULL; 23254bbf5ea8SMatthew G. Knepley const PetscInt *gtolArray = NULL; 23264bbf5ea8SMatthew G. Knepley PetscInt dof, offset, lidx; 23274bbf5ea8SMatthew G. Knepley 23284bbf5ea8SMatthew G. Knepley PetscFunctionBeginHot; 23299566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(x, &xArray)); 23309566063dSJacob Faibussowitsch PetscCall(VecGetArray(y, &yArray)); 23310904074fSPatrick Farrell if (scattertype == SCATTER_WITHARTIFICIAL) { 23329566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof)); 23339566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offset)); 23349566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->gtolWithArtificial, >olArray)); 23350904074fSPatrick Farrell } else if (scattertype == SCATTER_WITHALL) { 23369566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCountsWithAll, p, &dof)); 23379566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offset)); 23389566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->gtolWithAll, >olArray)); 2339c2e6f3c0SFlorian Wechsung } else { 23409566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &dof)); 23419566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset)); 23429566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->gtol, >olArray)); 2343c2e6f3c0SFlorian Wechsung } 23442472a847SBarry Smith PetscCheck(mode != INSERT_VALUES || scat == SCATTER_FORWARD, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't insert if not scattering forward"); 23452472a847SBarry Smith PetscCheck(mode != ADD_VALUES || scat == SCATTER_REVERSE, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't add if not scattering reverse"); 23464bbf5ea8SMatthew G. Knepley for (lidx = 0; lidx < dof; ++lidx) { 23474bbf5ea8SMatthew G. Knepley const PetscInt gidx = gtolArray[offset + lidx]; 23484bbf5ea8SMatthew G. Knepley 23494bbf5ea8SMatthew G. Knepley if (mode == INSERT_VALUES) yArray[lidx] = xArray[gidx]; /* Forward */ 23504bbf5ea8SMatthew G. Knepley else yArray[gidx] += xArray[lidx]; /* Reverse */ 23514bbf5ea8SMatthew G. Knepley } 23520904074fSPatrick Farrell if (scattertype == SCATTER_WITHARTIFICIAL) { 23539566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->gtolWithArtificial, >olArray)); 23540904074fSPatrick Farrell } else if (scattertype == SCATTER_WITHALL) { 23559566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->gtolWithAll, >olArray)); 2356c2e6f3c0SFlorian Wechsung } else { 23579566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->gtol, >olArray)); 2358c2e6f3c0SFlorian Wechsung } 23599566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(x, &xArray)); 23609566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(y, &yArray)); 23613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 23624bbf5ea8SMatthew G. Knepley } 23634bbf5ea8SMatthew G. Knepley 2364d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_PATCH_Linear(PC pc) 2365d71ae5a4SJacob Faibussowitsch { 2366dadc69c5SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 2367dadc69c5SMatthew G. Knepley const char *prefix; 2368dadc69c5SMatthew G. Knepley PetscInt i; 2369dadc69c5SMatthew G. Knepley 2370dadc69c5SMatthew G. Knepley PetscFunctionBegin; 2371dadc69c5SMatthew G. Knepley if (!pc->setupcalled) { 23727827d75bSBarry Smith PetscCheck(patch->save_operators || !patch->denseinverse, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Can't have dense inverse without save operators"); 2373c73d2cf6SLawrence Mitchell if (!patch->denseinverse) { 23749566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(patch->npatch, &patch->solver)); 23759566063dSJacob Faibussowitsch PetscCall(PCGetOptionsPrefix(pc, &prefix)); 2376dadc69c5SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) { 2377dadc69c5SMatthew G. Knepley KSP ksp; 2378dadc69c5SMatthew G. Knepley PC subpc; 2379dadc69c5SMatthew G. Knepley 23809566063dSJacob Faibussowitsch PetscCall(KSPCreate(PETSC_COMM_SELF, &ksp)); 23813821be0aSBarry Smith PetscCall(KSPSetNestLevel(ksp, pc->kspnestlevel)); 23829566063dSJacob Faibussowitsch PetscCall(KSPSetErrorIfNotConverged(ksp, pc->erroriffailure)); 23839566063dSJacob Faibussowitsch PetscCall(KSPSetOptionsPrefix(ksp, prefix)); 23849566063dSJacob Faibussowitsch PetscCall(KSPAppendOptionsPrefix(ksp, "sub_")); 23859566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)ksp, (PetscObject)pc, 1)); 23869566063dSJacob Faibussowitsch PetscCall(KSPGetPC(ksp, &subpc)); 23879566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)subpc, (PetscObject)pc, 1)); 2388dadc69c5SMatthew G. Knepley patch->solver[i] = (PetscObject)ksp; 2389dadc69c5SMatthew G. Knepley } 2390dadc69c5SMatthew G. Knepley } 2391c73d2cf6SLawrence Mitchell } 2392dadc69c5SMatthew G. Knepley if (patch->save_operators) { 23931baa6e33SBarry Smith if (patch->precomputeElementTensors) PetscCall(PCPatchPrecomputePatchTensors_Private(pc)); 2394dadc69c5SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) { 23959566063dSJacob Faibussowitsch PetscCall(PCPatchComputeOperator_Internal(pc, NULL, patch->mat[i], i, PETSC_FALSE)); 2396c73d2cf6SLawrence Mitchell if (!patch->denseinverse) { 23979566063dSJacob Faibussowitsch PetscCall(KSPSetOperators((KSP)patch->solver[i], patch->mat[i], patch->mat[i])); 23989d4fc724SLawrence Mitchell } else if (patch->mat[i] && !patch->densesolve) { 23999d4fc724SLawrence Mitchell /* Setup matmult callback */ 24009566063dSJacob Faibussowitsch PetscCall(MatGetOperation(patch->mat[i], MATOP_MULT, (void (**)(void)) & patch->densesolve)); 2401dadc69c5SMatthew G. Knepley } 2402dadc69c5SMatthew G. Knepley } 2403c73d2cf6SLawrence Mitchell } 240434d8b122SPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 240534d8b122SPatrick Farrell for (i = 0; i < patch->npatch; ++i) { 24061202d238SPatrick Farrell /* Instead of padding patch->patchUpdate with zeros to get */ 24071202d238SPatrick Farrell /* patch->patchUpdateWithArtificial and then multiplying with the matrix, */ 240834d8b122SPatrick Farrell /* just get rid of the columns that correspond to the dofs with */ 240934d8b122SPatrick Farrell /* artificial bcs. That's of course fairly inefficient, hopefully we */ 241034d8b122SPatrick Farrell /* can just assemble the rectangular matrix in the first place. */ 241134d8b122SPatrick Farrell Mat matSquare; 241234d8b122SPatrick Farrell IS rowis; 241334d8b122SPatrick Farrell PetscInt dof; 241434d8b122SPatrick Farrell 24159566063dSJacob Faibussowitsch PetscCall(MatGetSize(patch->mat[i], &dof, NULL)); 241634d8b122SPatrick Farrell if (dof == 0) { 241734d8b122SPatrick Farrell patch->matWithArtificial[i] = NULL; 241834d8b122SPatrick Farrell continue; 241934d8b122SPatrick Farrell } 242034d8b122SPatrick Farrell 24219566063dSJacob Faibussowitsch PetscCall(PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE)); 24229566063dSJacob Faibussowitsch PetscCall(PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE)); 242334d8b122SPatrick Farrell 24249566063dSJacob Faibussowitsch PetscCall(MatGetSize(matSquare, &dof, NULL)); 24259566063dSJacob Faibussowitsch PetscCall(ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis)); 242634d8b122SPatrick Farrell if (pc->setupcalled) { 24279566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_REUSE_MATRIX, &patch->matWithArtificial[i])); 242834d8b122SPatrick Farrell } else { 24299566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &patch->matWithArtificial[i])); 243034d8b122SPatrick Farrell } 24319566063dSJacob Faibussowitsch PetscCall(ISDestroy(&rowis)); 24329566063dSJacob Faibussowitsch PetscCall(MatDestroy(&matSquare)); 243334d8b122SPatrick Farrell } 243434d8b122SPatrick Farrell } 24353ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2436dadc69c5SMatthew G. Knepley } 2437dadc69c5SMatthew G. Knepley 2438d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_PATCH(PC pc) 2439d71ae5a4SJacob Faibussowitsch { 24404bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 2441557beb66SLawrence Mitchell PetscInt i; 244239fd2e8aSPatrick Farrell PetscBool isNonlinear; 24439d4fc724SLawrence Mitchell PetscInt maxDof = -1, maxDofWithArtificial = -1; 24444bbf5ea8SMatthew G. Knepley 24454bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 24464bbf5ea8SMatthew G. Knepley if (!pc->setupcalled) { 24474bbf5ea8SMatthew G. Knepley PetscInt pStart, pEnd, p; 24484bbf5ea8SMatthew G. Knepley PetscInt localSize; 24494bbf5ea8SMatthew G. Knepley 24509566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_CreatePatches, pc, 0, 0, 0)); 24514bbf5ea8SMatthew G. Knepley 2452debbdec3SPatrick Farrell isNonlinear = patch->isNonlinear; 24535f824522SMatthew G. Knepley if (!patch->nsubspaces) { 2454b6bb21d1SLawrence Mitchell DM dm, plex; 24555f824522SMatthew G. Knepley PetscSection s; 2456bd026e97SJed Brown PetscInt cStart, cEnd, c, Nf, f, numGlobalBcs = 0, *globalBcs, *Nb, **cellDofs; 24575f824522SMatthew G. Knepley 24589566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm)); 245928b400f6SJacob Faibussowitsch PetscCheck(dm, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Must set DM for PCPATCH or call PCPatchSetDiscretisationInfo()"); 24609566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex)); 2461b6bb21d1SLawrence Mitchell dm = plex; 24629566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, &s)); 24639566063dSJacob Faibussowitsch PetscCall(PetscSectionGetNumFields(s, &Nf)); 24649566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(s, &pStart, &pEnd)); 24655f824522SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 24665f824522SMatthew G. Knepley PetscInt cdof; 24679566063dSJacob Faibussowitsch PetscCall(PetscSectionGetConstraintDof(s, p, &cdof)); 24685f824522SMatthew G. Knepley numGlobalBcs += cdof; 24695f824522SMatthew G. Knepley } 24709566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 24719566063dSJacob Faibussowitsch PetscCall(PetscMalloc3(Nf, &Nb, Nf, &cellDofs, numGlobalBcs, &globalBcs)); 24725f824522SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 24735f824522SMatthew G. Knepley PetscFE fe; 24745f824522SMatthew G. Knepley PetscDualSpace sp; 24755f824522SMatthew G. Knepley PetscInt cdoff = 0; 24765f824522SMatthew G. Knepley 24779566063dSJacob Faibussowitsch PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe)); 24789566063dSJacob Faibussowitsch /* PetscCall(PetscFEGetNumComponents(fe, &Nc[f])); */ 24799566063dSJacob Faibussowitsch PetscCall(PetscFEGetDualSpace(fe, &sp)); 24809566063dSJacob Faibussowitsch PetscCall(PetscDualSpaceGetDimension(sp, &Nb[f])); 24815f824522SMatthew G. Knepley 24829566063dSJacob Faibussowitsch PetscCall(PetscMalloc1((cEnd - cStart) * Nb[f], &cellDofs[f])); 24835f824522SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 24845f824522SMatthew G. Knepley PetscInt *closure = NULL; 24855f824522SMatthew G. Knepley PetscInt clSize = 0, cl; 24865f824522SMatthew G. Knepley 24879566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure)); 24885f824522SMatthew G. Knepley for (cl = 0; cl < clSize * 2; cl += 2) { 24895f824522SMatthew G. Knepley const PetscInt p = closure[cl]; 24905f824522SMatthew G. Knepley PetscInt fdof, d, foff; 24915f824522SMatthew G. Knepley 24929566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldDof(s, p, f, &fdof)); 24939566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldOffset(s, p, f, &foff)); 24945f824522SMatthew G. Knepley for (d = 0; d < fdof; ++d, ++cdoff) cellDofs[f][cdoff] = foff + d; 24955f824522SMatthew G. Knepley } 24969566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure)); 24975f824522SMatthew G. Knepley } 249863a3b9bcSJacob Faibussowitsch PetscCheck(cdoff == (cEnd - cStart) * Nb[f], PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_SIZ, "Total number of cellDofs %" PetscInt_FMT " for field %" PetscInt_FMT " should be Nc (%" PetscInt_FMT ") * cellDof (%" PetscInt_FMT ")", cdoff, f, cEnd - cStart, Nb[f]); 24995f824522SMatthew G. Knepley } 25005f824522SMatthew G. Knepley numGlobalBcs = 0; 25015f824522SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 25025f824522SMatthew G. Knepley const PetscInt *ind; 25035f824522SMatthew G. Knepley PetscInt off, cdof, d; 25045f824522SMatthew G. Knepley 25059566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(s, p, &off)); 25069566063dSJacob Faibussowitsch PetscCall(PetscSectionGetConstraintDof(s, p, &cdof)); 25079566063dSJacob Faibussowitsch PetscCall(PetscSectionGetConstraintIndices(s, p, &ind)); 25085f824522SMatthew G. Knepley for (d = 0; d < cdof; ++d) globalBcs[numGlobalBcs++] = off + ind[d]; 25095f824522SMatthew G. Knepley } 25105f824522SMatthew G. Knepley 25119566063dSJacob Faibussowitsch PetscCall(PCPatchSetDiscretisationInfoCombined(pc, dm, Nb, (const PetscInt **)cellDofs, numGlobalBcs, globalBcs, numGlobalBcs, globalBcs)); 251248a46eb9SPierre Jolivet for (f = 0; f < Nf; ++f) PetscCall(PetscFree(cellDofs[f])); 25139566063dSJacob Faibussowitsch PetscCall(PetscFree3(Nb, cellDofs, globalBcs)); 25149566063dSJacob Faibussowitsch PetscCall(PCPatchSetComputeFunction(pc, PCPatchComputeFunction_DMPlex_Private, NULL)); 25159566063dSJacob Faibussowitsch PetscCall(PCPatchSetComputeOperator(pc, PCPatchComputeOperator_DMPlex_Private, NULL)); 25169566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm)); 25175f824522SMatthew G. Knepley } 25185f824522SMatthew G. Knepley 25194bbf5ea8SMatthew G. Knepley localSize = patch->subspaceOffsets[patch->nsubspaces]; 25209566063dSJacob Faibussowitsch PetscCall(VecCreateSeq(PETSC_COMM_SELF, localSize, &patch->localRHS)); 25219566063dSJacob Faibussowitsch PetscCall(VecSetUp(patch->localRHS)); 25229566063dSJacob Faibussowitsch PetscCall(VecDuplicate(patch->localRHS, &patch->localUpdate)); 25239566063dSJacob Faibussowitsch PetscCall(PCPatchCreateCellPatches(pc)); 25249566063dSJacob Faibussowitsch PetscCall(PCPatchCreateCellPatchDiscretisationInfo(pc)); 25254bbf5ea8SMatthew G. Knepley 25264bbf5ea8SMatthew G. Knepley /* OK, now build the work vectors */ 25279566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, &pEnd)); 2528c2e6f3c0SFlorian Wechsung 252948a46eb9SPierre Jolivet if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) PetscCall(PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithArtificial)); 253048a46eb9SPierre Jolivet if (isNonlinear) PetscCall(PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithAll)); 25314bbf5ea8SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 25324bbf5ea8SMatthew G. Knepley PetscInt dof; 25334bbf5ea8SMatthew G. Knepley 25349566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &dof)); 25352f613bf5SBarry Smith maxDof = PetscMax(maxDof, dof); 25360904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 25373bb0e8f7SKarl Rupp const PetscInt *gtolArray, *gtolArrayWithArtificial = NULL; 25383bb0e8f7SKarl Rupp PetscInt numPatchDofs, offset; 25393bb0e8f7SKarl Rupp PetscInt numPatchDofsWithArtificial, offsetWithArtificial; 25403bb0e8f7SKarl Rupp PetscInt dofWithoutArtificialCounter = 0; 25413bb0e8f7SKarl Rupp PetscInt *patchWithoutArtificialToWithArtificialArray; 25423bb0e8f7SKarl Rupp 25439566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof)); 25449d4fc724SLawrence Mitchell maxDofWithArtificial = PetscMax(maxDofWithArtificial, dof); 2545c2e6f3c0SFlorian Wechsung 2546e047a90bSFlorian Wechsung /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */ 2547e047a90bSFlorian Wechsung /* the index in the patch with all dofs */ 25489566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->gtol, >olArray)); 254963deea8eSPatrick Farrell 25509566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs)); 255147aca4a6SPatrick Farrell if (numPatchDofs == 0) { 255247aca4a6SPatrick Farrell patch->dofMappingWithoutToWithArtificial[p - pStart] = NULL; 255347aca4a6SPatrick Farrell continue; 255447aca4a6SPatrick Farrell } 255563deea8eSPatrick Farrell 25569566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset)); 25579566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->gtolWithArtificial, >olArrayWithArtificial)); 25589566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &numPatchDofsWithArtificial)); 25599566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offsetWithArtificial)); 2560c2e6f3c0SFlorian Wechsung 25619566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPatchDofs, &patchWithoutArtificialToWithArtificialArray)); 2562b0c21b6aSKarl Rupp for (i = 0; i < numPatchDofsWithArtificial; i++) { 2563e047a90bSFlorian Wechsung if (gtolArrayWithArtificial[i + offsetWithArtificial] == gtolArray[offset + dofWithoutArtificialCounter]) { 2564c2e6f3c0SFlorian Wechsung patchWithoutArtificialToWithArtificialArray[dofWithoutArtificialCounter] = i; 2565c2e6f3c0SFlorian Wechsung dofWithoutArtificialCounter++; 25669371c9d4SSatish Balay if (dofWithoutArtificialCounter == numPatchDofs) break; 2567c2e6f3c0SFlorian Wechsung } 2568c2e6f3c0SFlorian Wechsung } 25699566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutArtificialToWithArtificialArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithArtificial[p - pStart])); 25709566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->gtol, >olArray)); 25719566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->gtolWithArtificial, >olArrayWithArtificial)); 2572c2e6f3c0SFlorian Wechsung } 25730997d788SPatrick Farrell } 25740997d788SPatrick Farrell for (p = pStart; p < pEnd; ++p) { 25750904074fSPatrick Farrell if (isNonlinear) { 25760904074fSPatrick Farrell const PetscInt *gtolArray, *gtolArrayWithAll = NULL; 25770904074fSPatrick Farrell PetscInt numPatchDofs, offset; 25780904074fSPatrick Farrell PetscInt numPatchDofsWithAll, offsetWithAll; 25790904074fSPatrick Farrell PetscInt dofWithoutAllCounter = 0; 25800904074fSPatrick Farrell PetscInt *patchWithoutAllToWithAllArray; 25810904074fSPatrick Farrell 25820904074fSPatrick Farrell /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */ 25830904074fSPatrick Farrell /* the index in the patch with all dofs */ 25849566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->gtol, >olArray)); 25850904074fSPatrick Farrell 25869566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs)); 258747aca4a6SPatrick Farrell if (numPatchDofs == 0) { 2588b88cb22dSPatrick Farrell patch->dofMappingWithoutToWithAll[p - pStart] = NULL; 258947aca4a6SPatrick Farrell continue; 259047aca4a6SPatrick Farrell } 25910904074fSPatrick Farrell 25929566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset)); 25939566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->gtolWithAll, >olArrayWithAll)); 25949566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCountsWithAll, p, &numPatchDofsWithAll)); 25959566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offsetWithAll)); 25960904074fSPatrick Farrell 25979566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPatchDofs, &patchWithoutAllToWithAllArray)); 25980904074fSPatrick Farrell 25990904074fSPatrick Farrell for (i = 0; i < numPatchDofsWithAll; i++) { 26000904074fSPatrick Farrell if (gtolArrayWithAll[i + offsetWithAll] == gtolArray[offset + dofWithoutAllCounter]) { 26010904074fSPatrick Farrell patchWithoutAllToWithAllArray[dofWithoutAllCounter] = i; 26020904074fSPatrick Farrell dofWithoutAllCounter++; 26039371c9d4SSatish Balay if (dofWithoutAllCounter == numPatchDofs) break; 26040904074fSPatrick Farrell } 26050904074fSPatrick Farrell } 26069566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutAllToWithAllArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithAll[p - pStart])); 26079566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->gtol, >olArray)); 26089566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->gtolWithAll, >olArrayWithAll)); 26090904074fSPatrick Farrell } 26104bbf5ea8SMatthew G. Knepley } 261160dd46caSLawrence Mitchell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 26129566063dSJacob Faibussowitsch PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDofWithArtificial, &patch->patchRHSWithArtificial)); 26139566063dSJacob Faibussowitsch PetscCall(VecSetUp(patch->patchRHSWithArtificial)); 261460dd46caSLawrence Mitchell } 26159566063dSJacob Faibussowitsch PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDof, &patch->patchRHS)); 26169566063dSJacob Faibussowitsch PetscCall(VecSetUp(patch->patchRHS)); 26179566063dSJacob Faibussowitsch PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDof, &patch->patchUpdate)); 26189566063dSJacob Faibussowitsch PetscCall(VecSetUp(patch->patchUpdate)); 26194bbf5ea8SMatthew G. Knepley if (patch->save_operators) { 26209566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(patch->npatch, &patch->mat)); 262148a46eb9SPierre Jolivet for (i = 0; i < patch->npatch; ++i) PetscCall(PCPatchCreateMatrix_Private(pc, i, &patch->mat[i], PETSC_FALSE)); 26224bbf5ea8SMatthew G. Knepley } 26239566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_CreatePatches, pc, 0, 0, 0)); 26244bbf5ea8SMatthew G. Knepley 26254bbf5ea8SMatthew G. Knepley /* If desired, calculate weights for dof multiplicity */ 26264bbf5ea8SMatthew G. Knepley if (patch->partition_of_unity) { 26273bb0e8f7SKarl Rupp PetscScalar *input = NULL; 26283bb0e8f7SKarl Rupp PetscScalar *output = NULL; 26293bb0e8f7SKarl Rupp Vec global; 26303bb0e8f7SKarl Rupp 26319566063dSJacob Faibussowitsch PetscCall(VecDuplicate(patch->localRHS, &patch->dof_weights)); 263261c4b389SFlorian Wechsung if (patch->local_composition_type == PC_COMPOSITE_ADDITIVE) { 26334bbf5ea8SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) { 26344bbf5ea8SMatthew G. Knepley PetscInt dof; 26354bbf5ea8SMatthew G. Knepley 26369566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCounts, i + pStart, &dof)); 26374bbf5ea8SMatthew G. Knepley if (dof <= 0) continue; 26389566063dSJacob Faibussowitsch PetscCall(VecSet(patch->patchRHS, 1.0)); 26399566063dSJacob Faibussowitsch PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHS, patch->dof_weights, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR)); 26404bbf5ea8SMatthew G. Knepley } 2641c2e6f3c0SFlorian Wechsung } else { 2642e047a90bSFlorian Wechsung /* multiplicative is actually only locally multiplicative and globally additive. need the pou where the mesh decomposition overlaps */ 26439566063dSJacob Faibussowitsch PetscCall(VecSet(patch->dof_weights, 1.0)); 26444bbf5ea8SMatthew G. Knepley } 2645d132cafaSFlorian Wechsung 26463ba16761SJacob Faibussowitsch PetscCall(VecDuplicate(patch->dof_weights, &global)); 26473ba16761SJacob Faibussowitsch PetscCall(VecSet(global, 0.)); 2648d132cafaSFlorian Wechsung 26499566063dSJacob Faibussowitsch PetscCall(VecGetArray(patch->dof_weights, &input)); 26509566063dSJacob Faibussowitsch PetscCall(VecGetArray(global, &output)); 26519566063dSJacob Faibussowitsch PetscCall(PetscSFReduceBegin(patch->sectionSF, MPIU_SCALAR, input, output, MPI_SUM)); 26529566063dSJacob Faibussowitsch PetscCall(PetscSFReduceEnd(patch->sectionSF, MPIU_SCALAR, input, output, MPI_SUM)); 26539566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(patch->dof_weights, &input)); 26549566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(global, &output)); 2655d132cafaSFlorian Wechsung 26569566063dSJacob Faibussowitsch PetscCall(VecReciprocal(global)); 2657d132cafaSFlorian Wechsung 26589566063dSJacob Faibussowitsch PetscCall(VecGetArray(patch->dof_weights, &output)); 26599566063dSJacob Faibussowitsch PetscCall(VecGetArray(global, &input)); 26609566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(patch->sectionSF, MPIU_SCALAR, input, output, MPI_REPLACE)); 26619566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(patch->sectionSF, MPIU_SCALAR, input, output, MPI_REPLACE)); 26629566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(patch->dof_weights, &output)); 26639566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(global, &input)); 26649566063dSJacob Faibussowitsch PetscCall(VecDestroy(&global)); 26654bbf5ea8SMatthew G. Knepley } 266648a46eb9SPierre Jolivet if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE && patch->save_operators && !patch->isNonlinear) PetscCall(PetscMalloc1(patch->npatch, &patch->matWithArtificial)); 26674bbf5ea8SMatthew G. Knepley } 26689566063dSJacob Faibussowitsch PetscCall((*patch->setupsolver)(pc)); 26693ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 26704bbf5ea8SMatthew G. Knepley } 2671dadc69c5SMatthew G. Knepley 2672d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_PATCH_Linear(PC pc, PetscInt i, Vec x, Vec y) 2673d71ae5a4SJacob Faibussowitsch { 2674dadc69c5SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 2675c73d2cf6SLawrence Mitchell KSP ksp; 26769d4fc724SLawrence Mitchell Mat op; 26779d4fc724SLawrence Mitchell PetscInt m, n; 2678dadc69c5SMatthew G. Knepley 2679dadc69c5SMatthew G. Knepley PetscFunctionBegin; 2680c73d2cf6SLawrence Mitchell if (patch->denseinverse) { 26819566063dSJacob Faibussowitsch PetscCall((*patch->densesolve)(patch->mat[i], x, y)); 26823ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2683c73d2cf6SLawrence Mitchell } 2684c73d2cf6SLawrence Mitchell ksp = (KSP)patch->solver[i]; 2685dadc69c5SMatthew G. Knepley if (!patch->save_operators) { 2686dadc69c5SMatthew G. Knepley Mat mat; 2687dadc69c5SMatthew G. Knepley 26889566063dSJacob Faibussowitsch PetscCall(PCPatchCreateMatrix_Private(pc, i, &mat, PETSC_FALSE)); 2689dadc69c5SMatthew G. Knepley /* Populate operator here. */ 26909566063dSJacob Faibussowitsch PetscCall(PCPatchComputeOperator_Internal(pc, NULL, mat, i, PETSC_FALSE)); 26919566063dSJacob Faibussowitsch PetscCall(KSPSetOperators(ksp, mat, mat)); 2692dadc69c5SMatthew G. Knepley /* Drop reference so the KSPSetOperators below will blow it away. */ 26939566063dSJacob Faibussowitsch PetscCall(MatDestroy(&mat)); 2694dadc69c5SMatthew G. Knepley } 26959566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0)); 269648a46eb9SPierre Jolivet if (!ksp->setfromoptionscalled) PetscCall(KSPSetFromOptions(ksp)); 26979d4fc724SLawrence Mitchell /* Disgusting trick to reuse work vectors */ 26989566063dSJacob Faibussowitsch PetscCall(KSPGetOperators(ksp, &op, NULL)); 26999566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(op, &m, &n)); 27009d4fc724SLawrence Mitchell x->map->n = m; 27019d4fc724SLawrence Mitchell y->map->n = n; 27029d4fc724SLawrence Mitchell x->map->N = m; 27039d4fc724SLawrence Mitchell y->map->N = n; 27049566063dSJacob Faibussowitsch PetscCall(KSPSolve(ksp, x, y)); 27059566063dSJacob Faibussowitsch PetscCall(KSPCheckSolve(ksp, pc, y)); 27069566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0)); 2707dadc69c5SMatthew G. Knepley if (!patch->save_operators) { 2708dadc69c5SMatthew G. Knepley PC pc; 27099566063dSJacob Faibussowitsch PetscCall(KSPSetOperators(ksp, NULL, NULL)); 27109566063dSJacob Faibussowitsch PetscCall(KSPGetPC(ksp, &pc)); 2711dadc69c5SMatthew G. Knepley /* Destroy PC context too, otherwise the factored matrix hangs around. */ 27129566063dSJacob Faibussowitsch PetscCall(PCReset(pc)); 27134bbf5ea8SMatthew G. Knepley } 27143ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 27154bbf5ea8SMatthew G. Knepley } 27164bbf5ea8SMatthew G. Knepley 2717d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCUpdateMultiplicative_PATCH_Linear(PC pc, PetscInt i, PetscInt pStart) 2718d71ae5a4SJacob Faibussowitsch { 27196c9c532dSPatrick Farrell PC_PATCH *patch = (PC_PATCH *)pc->data; 27206c9c532dSPatrick Farrell Mat multMat; 27219d4fc724SLawrence Mitchell PetscInt n, m; 27226c9c532dSPatrick Farrell 27234d04e9f1SPatrick Farrell PetscFunctionBegin; 27244d04e9f1SPatrick Farrell 27256c9c532dSPatrick Farrell if (patch->save_operators) { 27266c9c532dSPatrick Farrell multMat = patch->matWithArtificial[i]; 27276c9c532dSPatrick Farrell } else { 27286c9c532dSPatrick Farrell /*Very inefficient, hopefully we can just assemble the rectangular matrix in the first place.*/ 27296c9c532dSPatrick Farrell Mat matSquare; 27306c9c532dSPatrick Farrell PetscInt dof; 27316c9c532dSPatrick Farrell IS rowis; 27329566063dSJacob Faibussowitsch PetscCall(PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE)); 27339566063dSJacob Faibussowitsch PetscCall(PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE)); 27349566063dSJacob Faibussowitsch PetscCall(MatGetSize(matSquare, &dof, NULL)); 27359566063dSJacob Faibussowitsch PetscCall(ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis)); 27369566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &multMat)); 27379566063dSJacob Faibussowitsch PetscCall(MatDestroy(&matSquare)); 27389566063dSJacob Faibussowitsch PetscCall(ISDestroy(&rowis)); 27396c9c532dSPatrick Farrell } 27409d4fc724SLawrence Mitchell /* Disgusting trick to reuse work vectors */ 27419566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(multMat, &m, &n)); 27429d4fc724SLawrence Mitchell patch->patchUpdate->map->n = n; 27439d4fc724SLawrence Mitchell patch->patchRHSWithArtificial->map->n = m; 27449d4fc724SLawrence Mitchell patch->patchUpdate->map->N = n; 27459d4fc724SLawrence Mitchell patch->patchRHSWithArtificial->map->N = m; 27469566063dSJacob Faibussowitsch PetscCall(MatMult(multMat, patch->patchUpdate, patch->patchRHSWithArtificial)); 27479566063dSJacob Faibussowitsch PetscCall(VecScale(patch->patchRHSWithArtificial, -1.0)); 27489566063dSJacob Faibussowitsch PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHSWithArtificial, patch->localRHS, ADD_VALUES, SCATTER_REVERSE, SCATTER_WITHARTIFICIAL)); 274948a46eb9SPierre Jolivet if (!patch->save_operators) PetscCall(MatDestroy(&multMat)); 27503ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 27516c9c532dSPatrick Farrell } 27526c9c532dSPatrick Farrell 2753d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_PATCH(PC pc, Vec x, Vec y) 2754d71ae5a4SJacob Faibussowitsch { 27554bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 27561202d238SPatrick Farrell const PetscScalar *globalRHS = NULL; 27571202d238SPatrick Farrell PetscScalar *localRHS = NULL; 27581202d238SPatrick Farrell PetscScalar *globalUpdate = NULL; 27594bbf5ea8SMatthew G. Knepley const PetscInt *bcNodes = NULL; 27604bbf5ea8SMatthew G. Knepley PetscInt nsweep = patch->symmetrise_sweep ? 2 : 1; 27614bbf5ea8SMatthew G. Knepley PetscInt start[2] = {0, 0}; 27624bbf5ea8SMatthew G. Knepley PetscInt end[2] = {-1, -1}; 27634bbf5ea8SMatthew G. Knepley const PetscInt inc[2] = {1, -1}; 27641202d238SPatrick Farrell const PetscScalar *localUpdate; 27654bbf5ea8SMatthew G. Knepley const PetscInt *iterationSet; 27664bbf5ea8SMatthew G. Knepley PetscInt pStart, numBcs, n, sweep, bc, j; 27674bbf5ea8SMatthew G. Knepley 27684bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 27699566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_Apply, pc, 0, 0, 0)); 27709566063dSJacob Faibussowitsch PetscCall(PetscOptionsPushGetViewerOff(PETSC_TRUE)); 277192d50984SMatthew G. Knepley /* start, end, inc have 2 entries to manage a second backward sweep if we symmetrize */ 27724bbf5ea8SMatthew G. Knepley end[0] = patch->npatch; 27734bbf5ea8SMatthew G. Knepley start[1] = patch->npatch - 1; 27744bbf5ea8SMatthew G. Knepley if (patch->user_patches) { 27759566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(patch->iterationSet, &end[0])); 27764bbf5ea8SMatthew G. Knepley start[1] = end[0] - 1; 27779566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->iterationSet, &iterationSet)); 27784bbf5ea8SMatthew G. Knepley } 27794bbf5ea8SMatthew G. Knepley /* Scatter from global space into overlapped local spaces */ 27809566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(x, &globalRHS)); 27819566063dSJacob Faibussowitsch PetscCall(VecGetArray(patch->localRHS, &localRHS)); 27829566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(patch->sectionSF, MPIU_SCALAR, globalRHS, localRHS, MPI_REPLACE)); 27839566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(patch->sectionSF, MPIU_SCALAR, globalRHS, localRHS, MPI_REPLACE)); 27849566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(x, &globalRHS)); 27859566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(patch->localRHS, &localRHS)); 27864bbf5ea8SMatthew G. Knepley 27879566063dSJacob Faibussowitsch PetscCall(VecSet(patch->localUpdate, 0.0)); 27889566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, NULL)); 27899566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0)); 27904bbf5ea8SMatthew G. Knepley for (sweep = 0; sweep < nsweep; sweep++) { 27914bbf5ea8SMatthew G. Knepley for (j = start[sweep]; j * inc[sweep] < end[sweep] * inc[sweep]; j += inc[sweep]) { 27924bbf5ea8SMatthew G. Knepley PetscInt i = patch->user_patches ? iterationSet[j] : j; 27934bbf5ea8SMatthew G. Knepley PetscInt start, len; 27944bbf5ea8SMatthew G. Knepley 27959566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCounts, i + pStart, &len)); 27969566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCounts, i + pStart, &start)); 27974bbf5ea8SMatthew G. Knepley /* TODO: Squash out these guys in the setup as well. */ 27984bbf5ea8SMatthew G. Knepley if (len <= 0) continue; 27994bbf5ea8SMatthew G. Knepley /* TODO: Do we need different scatters for X and Y? */ 28009566063dSJacob Faibussowitsch PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->localRHS, patch->patchRHS, INSERT_VALUES, SCATTER_FORWARD, SCATTER_INTERIOR)); 28019566063dSJacob Faibussowitsch PetscCall((*patch->applysolver)(pc, i, patch->patchRHS, patch->patchUpdate)); 28029566063dSJacob Faibussowitsch PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchUpdate, patch->localUpdate, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR)); 280348a46eb9SPierre Jolivet if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) PetscCall((*patch->updatemultiplicative)(pc, i, pStart)); 28044bbf5ea8SMatthew G. Knepley } 28054bbf5ea8SMatthew G. Knepley } 28069566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0)); 28079566063dSJacob Faibussowitsch if (patch->user_patches) PetscCall(ISRestoreIndices(patch->iterationSet, &iterationSet)); 28084bbf5ea8SMatthew G. Knepley /* XXX: should we do this on the global vector? */ 28091baa6e33SBarry Smith if (patch->partition_of_unity) PetscCall(VecPointwiseMult(patch->localUpdate, patch->localUpdate, patch->dof_weights)); 28101202d238SPatrick Farrell /* Now patch->localUpdate contains the solution of the patch solves, so we need to combine them all. */ 28119566063dSJacob Faibussowitsch PetscCall(VecSet(y, 0.0)); 28129566063dSJacob Faibussowitsch PetscCall(VecGetArray(y, &globalUpdate)); 28139566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(patch->localUpdate, &localUpdate)); 28149566063dSJacob Faibussowitsch PetscCall(PetscSFReduceBegin(patch->sectionSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM)); 28159566063dSJacob Faibussowitsch PetscCall(PetscSFReduceEnd(patch->sectionSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM)); 28169566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(patch->localUpdate, &localUpdate)); 28174bbf5ea8SMatthew G. Knepley 28184bbf5ea8SMatthew G. Knepley /* Now we need to send the global BC values through */ 28199566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(x, &globalRHS)); 28209566063dSJacob Faibussowitsch PetscCall(ISGetSize(patch->globalBcNodes, &numBcs)); 28219566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->globalBcNodes, &bcNodes)); 28229566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(x, &n)); 28234bbf5ea8SMatthew G. Knepley for (bc = 0; bc < numBcs; ++bc) { 28244bbf5ea8SMatthew G. Knepley const PetscInt idx = bcNodes[bc]; 28251202d238SPatrick Farrell if (idx < n) globalUpdate[idx] = globalRHS[idx]; 28264bbf5ea8SMatthew G. Knepley } 28274bbf5ea8SMatthew G. Knepley 28289566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->globalBcNodes, &bcNodes)); 28299566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(x, &globalRHS)); 28309566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(y, &globalUpdate)); 28314bbf5ea8SMatthew G. Knepley 28329566063dSJacob Faibussowitsch PetscCall(PetscOptionsPopGetViewerOff()); 28339566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_Apply, pc, 0, 0, 0)); 28343ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 28354bbf5ea8SMatthew G. Knepley } 28364bbf5ea8SMatthew G. Knepley 2837d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCReset_PATCH_Linear(PC pc) 2838d71ae5a4SJacob Faibussowitsch { 2839dadc69c5SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 2840dadc69c5SMatthew G. Knepley PetscInt i; 2841dadc69c5SMatthew G. Knepley 2842dadc69c5SMatthew G. Knepley PetscFunctionBegin; 2843dadc69c5SMatthew G. Knepley if (patch->solver) { 28449566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(KSPReset((KSP)patch->solver[i])); 2845dadc69c5SMatthew G. Knepley } 28463ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2847dadc69c5SMatthew G. Knepley } 2848dadc69c5SMatthew G. Knepley 2849d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCReset_PATCH(PC pc) 2850d71ae5a4SJacob Faibussowitsch { 28514bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 28524bbf5ea8SMatthew G. Knepley PetscInt i; 28534bbf5ea8SMatthew G. Knepley 28544bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 2855fa84ea4cSLawrence Mitchell 28569566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&patch->sectionSF)); 28579566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->cellCounts)); 28589566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->pointCounts)); 28599566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->cellNumbering)); 28609566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->gtolCounts)); 28619566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->gtol)); 28629566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->cells)); 28639566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->points)); 28649566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->dofs)); 28659566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->offs)); 28669566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->patchSection)); 28679566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->ghostBcNodes)); 28689566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->globalBcNodes)); 28699566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->gtolCountsWithArtificial)); 28709566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->gtolWithArtificial)); 28719566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->dofsWithArtificial)); 28729566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->offsWithArtificial)); 28739566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->gtolCountsWithAll)); 28749566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->gtolWithAll)); 28759566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->dofsWithAll)); 28769566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->offsWithAll)); 28779566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->cellMats)); 28789566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->intFacetMats)); 28799566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->allCells)); 28809566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->intFacets)); 28819566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->extFacets)); 28829566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->intFacetsToPatchCell)); 28839566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->intFacetCounts)); 28849566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->extFacetCounts)); 28854bbf5ea8SMatthew G. Knepley 28869371c9d4SSatish Balay if (patch->dofSection) 28879371c9d4SSatish Balay for (i = 0; i < patch->nsubspaces; i++) PetscCall(PetscSectionDestroy(&patch->dofSection[i])); 28889566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->dofSection)); 28899566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->bs)); 28909566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->nodesPerCell)); 28919371c9d4SSatish Balay if (patch->cellNodeMap) 28929371c9d4SSatish Balay for (i = 0; i < patch->nsubspaces; i++) PetscCall(PetscFree(patch->cellNodeMap[i])); 28939566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->cellNodeMap)); 28949566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->subspaceOffsets)); 28954bbf5ea8SMatthew G. Knepley 28969566063dSJacob Faibussowitsch PetscCall((*patch->resetsolver)(pc)); 28974bbf5ea8SMatthew G. Knepley 289848a46eb9SPierre Jolivet if (patch->subspaces_to_exclude) PetscCall(PetscHSetIDestroy(&patch->subspaces_to_exclude)); 2899e4c66b91SPatrick Farrell 29009566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->localRHS)); 29019566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->localUpdate)); 29029566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->patchRHS)); 29039566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->patchUpdate)); 29049566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->dof_weights)); 29054bbf5ea8SMatthew G. Knepley if (patch->patch_dof_weights) { 29069566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(VecDestroy(&patch->patch_dof_weights[i])); 29079566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->patch_dof_weights)); 29084bbf5ea8SMatthew G. Knepley } 29094bbf5ea8SMatthew G. Knepley if (patch->mat) { 29109566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(MatDestroy(&patch->mat[i])); 29119566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->mat)); 29125f824522SMatthew G. Knepley } 29130997d788SPatrick Farrell if (patch->matWithArtificial && !patch->isNonlinear) { 29149566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(MatDestroy(&patch->matWithArtificial[i])); 29159566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->matWithArtificial)); 291611ac8bb0SFlorian Wechsung } 29179566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->patchRHSWithArtificial)); 291896b79ebeSFlorian Wechsung if (patch->dofMappingWithoutToWithArtificial) { 29199566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->dofMappingWithoutToWithArtificial[i])); 29209566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->dofMappingWithoutToWithArtificial)); 292196b79ebeSFlorian Wechsung } 29220904074fSPatrick Farrell if (patch->dofMappingWithoutToWithAll) { 29239566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->dofMappingWithoutToWithAll[i])); 29249566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->dofMappingWithoutToWithAll)); 29250904074fSPatrick Farrell } 29269566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->sub_mat_type)); 29275f824522SMatthew G. Knepley if (patch->userIS) { 29289566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->userIS[i])); 29299566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->userIS)); 29305f824522SMatthew G. Knepley } 29319566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->precomputedTensorLocations)); 29329566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->precomputedIntFacetTensorLocations)); 2933f98464cbSLawrence Mitchell 29340a545947SLisandro Dalcin patch->bs = NULL; 29354bbf5ea8SMatthew G. Knepley patch->cellNodeMap = NULL; 29367974b488SMatthew G. Knepley patch->nsubspaces = 0; 29379566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->iterationSet)); 29385f824522SMatthew G. Knepley 29399566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&patch->viewerSection)); 29403ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 29414bbf5ea8SMatthew G. Knepley } 29424bbf5ea8SMatthew G. Knepley 2943d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_PATCH_Linear(PC pc) 2944d71ae5a4SJacob Faibussowitsch { 29454bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 29464bbf5ea8SMatthew G. Knepley PetscInt i; 29474bbf5ea8SMatthew G. Knepley 29484bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 2949dadc69c5SMatthew G. Knepley if (patch->solver) { 29509566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(KSPDestroy((KSP *)&patch->solver[i])); 29519566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->solver)); 29524bbf5ea8SMatthew G. Knepley } 29533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2954dadc69c5SMatthew G. Knepley } 2955dadc69c5SMatthew G. Knepley 2956d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_PATCH(PC pc) 2957d71ae5a4SJacob Faibussowitsch { 2958dadc69c5SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 2959dadc69c5SMatthew G. Knepley 2960dadc69c5SMatthew G. Knepley PetscFunctionBegin; 29619566063dSJacob Faibussowitsch PetscCall(PCReset_PATCH(pc)); 29629566063dSJacob Faibussowitsch PetscCall((*patch->destroysolver)(pc)); 29639566063dSJacob Faibussowitsch PetscCall(PetscFree(pc->data)); 29643ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 29654bbf5ea8SMatthew G. Knepley } 29664bbf5ea8SMatthew G. Knepley 2967d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetFromOptions_PATCH(PC pc, PetscOptionItems *PetscOptionsObject) 2968d71ae5a4SJacob Faibussowitsch { 29694bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 29704bbf5ea8SMatthew G. Knepley PCPatchConstructType patchConstructionType = PC_PATCH_STAR; 29715f824522SMatthew G. Knepley char sub_mat_type[PETSC_MAX_PATH_LEN]; 297210534d48SPatrick Farrell char option[PETSC_MAX_PATH_LEN]; 29735f824522SMatthew G. Knepley const char *prefix; 29744bbf5ea8SMatthew G. Knepley PetscBool flg, dimflg, codimflg; 29755f824522SMatthew G. Knepley MPI_Comm comm; 2976a48c39c8SPatrick Farrell PetscInt *ifields, nfields, k; 297761c4b389SFlorian Wechsung PCCompositeType loctype = PC_COMPOSITE_ADDITIVE; 29784bbf5ea8SMatthew G. Knepley 29794bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 29809566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)pc, &comm)); 29819566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)pc, &prefix)); 2982d0609cedSBarry Smith PetscOptionsHeadBegin(PetscOptionsObject, "Patch solver options"); 298310534d48SPatrick Farrell 29849566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_save_operators", patch->classname)); 29859566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool(option, "Store all patch operators for lifetime of object?", "PCPatchSetSaveOperators", patch->save_operators, &patch->save_operators, &flg)); 298610534d48SPatrick Farrell 29879566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_precompute_element_tensors", patch->classname)); 29889566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool(option, "Compute each element tensor only once?", "PCPatchSetPrecomputeElementTensors", patch->precomputeElementTensors, &patch->precomputeElementTensors, &flg)); 29899566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_partition_of_unity", patch->classname)); 29909566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool(option, "Weight contributions by dof multiplicity?", "PCPatchSetPartitionOfUnity", patch->partition_of_unity, &patch->partition_of_unity, &flg)); 299110534d48SPatrick Farrell 29929566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_local_type", patch->classname)); 29939566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum(option, "Type of local solver composition (additive or multiplicative)", "PCPatchSetLocalComposition", PCCompositeTypes, (PetscEnum)loctype, (PetscEnum *)&loctype, &flg)); 29949566063dSJacob Faibussowitsch if (flg) PetscCall(PCPatchSetLocalComposition(pc, loctype)); 29959566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_dense_inverse", patch->classname)); 29969566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool(option, "Compute inverses of patch matrices and apply directly? Ignores KSP/PC settings on patch.", "PCPatchSetDenseInverse", patch->denseinverse, &patch->denseinverse, &flg)); 29979566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_dim", patch->classname)); 29989566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt(option, "What dimension of mesh point to construct patches by? (0 = vertices)", "PCPATCH", patch->dim, &patch->dim, &dimflg)); 29999566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_codim", patch->classname)); 30009566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt(option, "What co-dimension of mesh point to construct patches by? (0 = cells)", "PCPATCH", patch->codim, &patch->codim, &codimflg)); 300108401ef6SPierre Jolivet PetscCheck(!dimflg || !codimflg, comm, PETSC_ERR_ARG_WRONG, "Can only set one of dimension or co-dimension"); 300210534d48SPatrick Farrell 30039566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_type", patch->classname)); 30049566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum(option, "How should the patches be constructed?", "PCPatchSetConstructType", PCPatchConstructTypes, (PetscEnum)patchConstructionType, (PetscEnum *)&patchConstructionType, &flg)); 30059566063dSJacob Faibussowitsch if (flg) PetscCall(PCPatchSetConstructType(pc, patchConstructionType, NULL, NULL)); 300610534d48SPatrick Farrell 30079566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_vanka_dim", patch->classname)); 30089566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt(option, "Topological dimension of entities for Vanka to ignore", "PCPATCH", patch->vankadim, &patch->vankadim, &flg)); 300910534d48SPatrick Farrell 30109566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_ignore_dim", patch->classname)); 30119566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt(option, "Topological dimension of entities for completion to ignore", "PCPATCH", patch->ignoredim, &patch->ignoredim, &flg)); 301210534d48SPatrick Farrell 30139566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_pardecomp_overlap", patch->classname)); 30149566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt(option, "What overlap should we use in construct type pardecomp?", "PCPATCH", patch->pardecomp_overlap, &patch->pardecomp_overlap, &flg)); 3015b525f888SPatrick Farrell 30169566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_sub_mat_type", patch->classname)); 30179566063dSJacob Faibussowitsch PetscCall(PetscOptionsFList(option, "Matrix type for patch solves", "PCPatchSetSubMatType", MatList, NULL, sub_mat_type, PETSC_MAX_PATH_LEN, &flg)); 30189566063dSJacob Faibussowitsch if (flg) PetscCall(PCPatchSetSubMatType(pc, sub_mat_type)); 301910534d48SPatrick Farrell 30209566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_symmetrise_sweep", patch->classname)); 30219566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool(option, "Go start->end, end->start?", "PCPATCH", patch->symmetrise_sweep, &patch->symmetrise_sweep, &flg)); 3022e4c66b91SPatrick Farrell 3023a48c39c8SPatrick Farrell /* If the user has set the number of subspaces, use that for the buffer size, 3024a48c39c8SPatrick Farrell otherwise use a large number */ 3025a48c39c8SPatrick Farrell if (patch->nsubspaces <= 0) { 3026a48c39c8SPatrick Farrell nfields = 128; 3027a48c39c8SPatrick Farrell } else { 3028a48c39c8SPatrick Farrell nfields = patch->nsubspaces; 3029a48c39c8SPatrick Farrell } 30309566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nfields, &ifields)); 30319566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exclude_subspaces", patch->classname)); 30329566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetIntArray(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, option, ifields, &nfields, &flg)); 303308401ef6SPierre Jolivet PetscCheck(!flg || !(patchConstructionType == PC_PATCH_USER), comm, PETSC_ERR_ARG_INCOMP, "We cannot support excluding a subspace with user patches because we do not index patches with a mesh point"); 3034e4c66b91SPatrick Farrell if (flg) { 30359566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(patch->subspaces_to_exclude)); 303648a46eb9SPierre Jolivet for (k = 0; k < nfields; k++) PetscCall(PetscHSetIAdd(patch->subspaces_to_exclude, ifields[k])); 3037e4c66b91SPatrick Farrell } 30389566063dSJacob Faibussowitsch PetscCall(PetscFree(ifields)); 30395f824522SMatthew G. Knepley 30409566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_patches_view", patch->classname)); 30419566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool(option, "Print out information during patch construction", "PCPATCH", patch->viewPatches, &patch->viewPatches, &flg)); 30429566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_cells_view", patch->classname)); 30439566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerCells, &patch->formatCells, &patch->viewCells)); 30449566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_interior_facets_view", patch->classname)); 30459566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerIntFacets, &patch->formatIntFacets, &patch->viewIntFacets)); 30469566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exterior_facets_view", patch->classname)); 30479566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerExtFacets, &patch->formatExtFacets, &patch->viewExtFacets)); 30489566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_points_view", patch->classname)); 30499566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerPoints, &patch->formatPoints, &patch->viewPoints)); 30509566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_section_view", patch->classname)); 30519566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerSection, &patch->formatSection, &patch->viewSection)); 30529566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_mat_view", patch->classname)); 30539566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerMatrix, &patch->formatMatrix, &patch->viewMatrix)); 3054d0609cedSBarry Smith PetscOptionsHeadEnd(); 30555f824522SMatthew G. Knepley patch->optionsSet = PETSC_TRUE; 30563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 30574bbf5ea8SMatthew G. Knepley } 30584bbf5ea8SMatthew G. Knepley 3059d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUpOnBlocks_PATCH(PC pc) 3060d71ae5a4SJacob Faibussowitsch { 30614bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 30624bbf5ea8SMatthew G. Knepley KSPConvergedReason reason; 30634bbf5ea8SMatthew G. Knepley PetscInt i; 30644bbf5ea8SMatthew G. Knepley 30654bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 3066a1eac568SLawrence Mitchell if (!patch->save_operators) { 3067a1eac568SLawrence Mitchell /* Can't do this here because the sub KSPs don't have an operator attached yet. */ 30683ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3069a1eac568SLawrence Mitchell } 3070c73d2cf6SLawrence Mitchell if (patch->denseinverse) { 3071c73d2cf6SLawrence Mitchell /* No solvers */ 30723ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3073c73d2cf6SLawrence Mitchell } 30744bbf5ea8SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) { 307548a46eb9SPierre Jolivet if (!((KSP)patch->solver[i])->setfromoptionscalled) PetscCall(KSPSetFromOptions((KSP)patch->solver[i])); 30769566063dSJacob Faibussowitsch PetscCall(KSPSetUp((KSP)patch->solver[i])); 30779566063dSJacob Faibussowitsch PetscCall(KSPGetConvergedReason((KSP)patch->solver[i], &reason)); 3078c0decd05SBarry Smith if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR; 30794bbf5ea8SMatthew G. Knepley } 30803ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 30814bbf5ea8SMatthew G. Knepley } 30824bbf5ea8SMatthew G. Knepley 3083d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCView_PATCH(PC pc, PetscViewer viewer) 3084d71ae5a4SJacob Faibussowitsch { 30854bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data; 30864bbf5ea8SMatthew G. Knepley PetscViewer sviewer; 30874bbf5ea8SMatthew G. Knepley PetscBool isascii; 30884bbf5ea8SMatthew G. Knepley PetscMPIInt rank; 30894bbf5ea8SMatthew G. Knepley 30904bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 30914bbf5ea8SMatthew G. Knepley /* TODO Redo tabbing with set tbas in new style */ 30929566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii)); 30933ba16761SJacob Faibussowitsch if (!isascii) PetscFunctionReturn(PETSC_SUCCESS); 30949566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank)); 30959566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 309663a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Subspace Correction preconditioner with %" PetscInt_FMT " patches\n", patch->npatch)); 309761c4b389SFlorian Wechsung if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 30989566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Schwarz type: multiplicative\n")); 3099e047a90bSFlorian Wechsung } else { 31009566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Schwarz type: additive\n")); 3101c2e6f3c0SFlorian Wechsung } 31029566063dSJacob Faibussowitsch if (patch->partition_of_unity) PetscCall(PetscViewerASCIIPrintf(viewer, "Weighting by partition of unity\n")); 31039566063dSJacob Faibussowitsch else PetscCall(PetscViewerASCIIPrintf(viewer, "Not weighting by partition of unity\n")); 31049566063dSJacob Faibussowitsch if (patch->symmetrise_sweep) PetscCall(PetscViewerASCIIPrintf(viewer, "Symmetrising sweep (start->end, then end->start)\n")); 31059566063dSJacob Faibussowitsch else PetscCall(PetscViewerASCIIPrintf(viewer, "Not symmetrising sweep\n")); 31069566063dSJacob Faibussowitsch if (!patch->precomputeElementTensors) PetscCall(PetscViewerASCIIPrintf(viewer, "Not precomputing element tensors (overlapping cells rebuilt in every patch assembly)\n")); 31079566063dSJacob Faibussowitsch else PetscCall(PetscViewerASCIIPrintf(viewer, "Precomputing element tensors (each cell assembled only once)\n")); 31089566063dSJacob Faibussowitsch if (!patch->save_operators) PetscCall(PetscViewerASCIIPrintf(viewer, "Not saving patch operators (rebuilt every PCApply)\n")); 31099566063dSJacob Faibussowitsch else PetscCall(PetscViewerASCIIPrintf(viewer, "Saving patch operators (rebuilt every PCSetUp)\n")); 31109566063dSJacob Faibussowitsch if (patch->patchconstructop == PCPatchConstruct_Star) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: star\n")); 31119566063dSJacob Faibussowitsch else if (patch->patchconstructop == PCPatchConstruct_Vanka) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: Vanka\n")); 31129566063dSJacob Faibussowitsch else if (patch->patchconstructop == PCPatchConstruct_User) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: user-specified\n")); 31139566063dSJacob Faibussowitsch else PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: unknown\n")); 31145d30859aSPatrick Farrell 3115c73d2cf6SLawrence Mitchell if (patch->denseinverse) { 31169566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Explicitly forming dense inverse and applying patch solver via MatMult.\n")); 3117c73d2cf6SLawrence Mitchell } else { 31185d30859aSPatrick Farrell if (patch->isNonlinear) { 31199566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "SNES on patches (all same):\n")); 31205d30859aSPatrick Farrell } else { 31219566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "KSP on patches (all same):\n")); 31225d30859aSPatrick Farrell } 3123dadc69c5SMatthew G. Knepley if (patch->solver) { 31249566063dSJacob Faibussowitsch PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer)); 3125dd400576SPatrick Sanan if (rank == 0) { 31269566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(sviewer)); 31279566063dSJacob Faibussowitsch PetscCall(PetscObjectView(patch->solver[0], sviewer)); 31289566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(sviewer)); 31294bbf5ea8SMatthew G. Knepley } 31309566063dSJacob Faibussowitsch PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer)); 31314bbf5ea8SMatthew G. Knepley } else { 31329566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 31339566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Solver not yet set.\n")); 31349566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 31354bbf5ea8SMatthew G. Knepley } 3136c73d2cf6SLawrence Mitchell } 31379566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 31383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 31394bbf5ea8SMatthew G. Knepley } 31404bbf5ea8SMatthew G. Knepley 3141e5893cccSMatthew G. Knepley /*MC 3142f1580f4eSBarry Smith PCPATCH - A `PC` object that encapsulates flexible definition of blocks for overlapping and non-overlapping 314398ed095eSMatthew G. Knepley small block additive preconditioners. Block definition is based on topology from 3144f1580f4eSBarry Smith a `DM` and equation numbering from a `PetscSection`. 3145e5893cccSMatthew G. Knepley 3146e5893cccSMatthew G. Knepley Options Database Keys: 3147e5893cccSMatthew G. Knepley + -pc_patch_cells_view - Views the process local cell numbers for each patch 3148e5893cccSMatthew G. Knepley . -pc_patch_points_view - Views the process local mesh point numbers for each patch 3149e5893cccSMatthew G. Knepley . -pc_patch_g2l_view - Views the map between global dofs and patch local dofs for each patch 3150e5893cccSMatthew G. Knepley . -pc_patch_patches_view - Views the global dofs associated with each patch and its boundary 3151e5893cccSMatthew G. Knepley - -pc_patch_sub_mat_view - Views the matrix associated with each patch 3152e5893cccSMatthew G. Knepley 3153e5893cccSMatthew G. Knepley Level: intermediate 3154e5893cccSMatthew G. Knepley 3155*562efe2eSBarry Smith .seealso: [](ch_ksp), `PCType`, `PCCreate()`, `PCSetType()`, `PCASM`, `PCJACOBI`, `PCPBJACOBI`, `PCVPBJACOBI`, `SNESPATCH` 3156e5893cccSMatthew G. Knepley M*/ 3157d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PCCreate_Patch(PC pc) 3158d71ae5a4SJacob Faibussowitsch { 31594bbf5ea8SMatthew G. Knepley PC_PATCH *patch; 31604bbf5ea8SMatthew G. Knepley 31614bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 3162f39ec787SMatthew G. Knepley PetscCall(PetscCitationsRegister(PCPatchCitation, &PCPatchcite)); 31634dfa11a4SJacob Faibussowitsch PetscCall(PetscNew(&patch)); 31644bbf5ea8SMatthew G. Knepley 316548a46eb9SPierre Jolivet if (patch->subspaces_to_exclude) PetscCall(PetscHSetIDestroy(&patch->subspaces_to_exclude)); 31669566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&patch->subspaces_to_exclude)); 3167e4c66b91SPatrick Farrell 316810534d48SPatrick Farrell patch->classname = "pc"; 3169debbdec3SPatrick Farrell patch->isNonlinear = PETSC_FALSE; 317010534d48SPatrick Farrell 31714bbf5ea8SMatthew G. Knepley /* Set some defaults */ 31725f824522SMatthew G. Knepley patch->combined = PETSC_FALSE; 31734bbf5ea8SMatthew G. Knepley patch->save_operators = PETSC_TRUE; 317461c4b389SFlorian Wechsung patch->local_composition_type = PC_COMPOSITE_ADDITIVE; 3175fa84ea4cSLawrence Mitchell patch->precomputeElementTensors = PETSC_FALSE; 31764bbf5ea8SMatthew G. Knepley patch->partition_of_unity = PETSC_FALSE; 31774bbf5ea8SMatthew G. Knepley patch->codim = -1; 31784bbf5ea8SMatthew G. Knepley patch->dim = -1; 31794bbf5ea8SMatthew G. Knepley patch->vankadim = -1; 31805f824522SMatthew G. Knepley patch->ignoredim = -1; 3181b525f888SPatrick Farrell patch->pardecomp_overlap = 0; 31824bbf5ea8SMatthew G. Knepley patch->patchconstructop = PCPatchConstruct_Star; 31834bbf5ea8SMatthew G. Knepley patch->symmetrise_sweep = PETSC_FALSE; 31845f824522SMatthew G. Knepley patch->npatch = 0; 31854bbf5ea8SMatthew G. Knepley patch->userIS = NULL; 31865f824522SMatthew G. Knepley patch->optionsSet = PETSC_FALSE; 31874bbf5ea8SMatthew G. Knepley patch->iterationSet = NULL; 31884bbf5ea8SMatthew G. Knepley patch->user_patches = PETSC_FALSE; 31899566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(MATDENSE, (char **)&patch->sub_mat_type)); 31905f824522SMatthew G. Knepley patch->viewPatches = PETSC_FALSE; 31915f824522SMatthew G. Knepley patch->viewCells = PETSC_FALSE; 31925f824522SMatthew G. Knepley patch->viewPoints = PETSC_FALSE; 31935f824522SMatthew G. Knepley patch->viewSection = PETSC_FALSE; 31945f824522SMatthew G. Knepley patch->viewMatrix = PETSC_FALSE; 31959d4fc724SLawrence Mitchell patch->densesolve = NULL; 3196dadc69c5SMatthew G. Knepley patch->setupsolver = PCSetUp_PATCH_Linear; 3197dadc69c5SMatthew G. Knepley patch->applysolver = PCApply_PATCH_Linear; 3198dadc69c5SMatthew G. Knepley patch->resetsolver = PCReset_PATCH_Linear; 3199dadc69c5SMatthew G. Knepley patch->destroysolver = PCDestroy_PATCH_Linear; 32006c9c532dSPatrick Farrell patch->updatemultiplicative = PCUpdateMultiplicative_PATCH_Linear; 320147aca4a6SPatrick Farrell patch->dofMappingWithoutToWithArtificial = NULL; 320247aca4a6SPatrick Farrell patch->dofMappingWithoutToWithAll = NULL; 32034bbf5ea8SMatthew G. Knepley 32044bbf5ea8SMatthew G. Knepley pc->data = (void *)patch; 32054bbf5ea8SMatthew G. Knepley pc->ops->apply = PCApply_PATCH; 32060a545947SLisandro Dalcin pc->ops->applytranspose = NULL; /* PCApplyTranspose_PATCH; */ 32074bbf5ea8SMatthew G. Knepley pc->ops->setup = PCSetUp_PATCH; 32084bbf5ea8SMatthew G. Knepley pc->ops->reset = PCReset_PATCH; 32094bbf5ea8SMatthew G. Knepley pc->ops->destroy = PCDestroy_PATCH; 32104bbf5ea8SMatthew G. Knepley pc->ops->setfromoptions = PCSetFromOptions_PATCH; 32114bbf5ea8SMatthew G. Knepley pc->ops->setuponblocks = PCSetUpOnBlocks_PATCH; 32124bbf5ea8SMatthew G. Knepley pc->ops->view = PCView_PATCH; 32130a545947SLisandro Dalcin pc->ops->applyrichardson = NULL; 32144bbf5ea8SMatthew G. Knepley 32153ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 32164bbf5ea8SMatthew G. Knepley } 3217