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 109d4fc724SLawrence Mitchell PetscLogEvent PC_Patch_CreatePatches, PC_Patch_ComputeOp, PC_Patch_Solve, PC_Patch_Apply, PC_Patch_Prealloc; 114bbf5ea8SMatthew G. Knepley 129fbee547SJacob Faibussowitsch static inline PetscErrorCode ObjectView(PetscObject obj, PetscViewer viewer, PetscViewerFormat format) 135f824522SMatthew G. Knepley { 145f824522SMatthew G. Knepley 159566063dSJacob Faibussowitsch PetscCall(PetscViewerPushFormat(viewer, format)); 169566063dSJacob Faibussowitsch PetscCall(PetscObjectView(obj, viewer)); 179566063dSJacob Faibussowitsch PetscCall(PetscViewerPopFormat(viewer)); 187974b488SMatthew G. Knepley return(0); 195f824522SMatthew G. Knepley } 205f824522SMatthew G. Knepley 211b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_Star(void *vpatch, DM dm, PetscInt point, PetscHSetI ht) 224bbf5ea8SMatthew G. Knepley { 234bbf5ea8SMatthew G. Knepley PetscInt starSize; 244bbf5ea8SMatthew G. Knepley PetscInt *star = NULL, si; 254bbf5ea8SMatthew G. Knepley 264bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 279566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(ht)); 284bbf5ea8SMatthew G. Knepley /* To start with, add the point we care about */ 299566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(ht, point)); 304bbf5ea8SMatthew G. Knepley /* Loop over all the points that this point connects to */ 319566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star)); 329566063dSJacob Faibussowitsch for (si = 0; si < starSize*2; si += 2) PetscCall(PetscHSetIAdd(ht, star[si])); 339566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star)); 344bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 354bbf5ea8SMatthew G. Knepley } 364bbf5ea8SMatthew G. Knepley 371b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_Vanka(void *vpatch, DM dm, PetscInt point, PetscHSetI ht) 384bbf5ea8SMatthew G. Knepley { 394bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) vpatch; 404bbf5ea8SMatthew G. Knepley PetscInt starSize; 414bbf5ea8SMatthew G. Knepley PetscInt *star = NULL; 424bbf5ea8SMatthew G. Knepley PetscBool shouldIgnore = PETSC_FALSE; 434bbf5ea8SMatthew G. Knepley PetscInt cStart, cEnd, iStart, iEnd, si; 444bbf5ea8SMatthew G. Knepley 454bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 469566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(ht)); 474bbf5ea8SMatthew G. Knepley /* To start with, add the point we care about */ 489566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(ht, point)); 494bbf5ea8SMatthew G. Knepley /* Should we ignore any points of a certain dimension? */ 504bbf5ea8SMatthew G. Knepley if (patch->vankadim >= 0) { 514bbf5ea8SMatthew G. Knepley shouldIgnore = PETSC_TRUE; 529566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, patch->vankadim, &iStart, &iEnd)); 534bbf5ea8SMatthew G. Knepley } 549566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 554bbf5ea8SMatthew G. Knepley /* Loop over all the cells that this point connects to */ 569566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star)); 575f824522SMatthew G. Knepley for (si = 0; si < starSize*2; si += 2) { 584bbf5ea8SMatthew G. Knepley const PetscInt cell = star[si]; 594bbf5ea8SMatthew G. Knepley PetscInt closureSize; 604bbf5ea8SMatthew G. Knepley PetscInt *closure = NULL, ci; 614bbf5ea8SMatthew G. Knepley 624bbf5ea8SMatthew G. Knepley if (cell < cStart || cell >= cEnd) continue; 634bbf5ea8SMatthew G. Knepley /* now loop over all entities in the closure of that cell */ 649566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure)); 655f824522SMatthew G. Knepley for (ci = 0; ci < closureSize*2; ci += 2) { 664bbf5ea8SMatthew G. Knepley const PetscInt newpoint = closure[ci]; 674bbf5ea8SMatthew G. Knepley 684bbf5ea8SMatthew G. Knepley /* We've been told to ignore entities of this type.*/ 694bbf5ea8SMatthew G. Knepley if (shouldIgnore && newpoint >= iStart && newpoint < iEnd) continue; 709566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(ht, newpoint)); 714bbf5ea8SMatthew G. Knepley } 729566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure)); 734bbf5ea8SMatthew G. Knepley } 749566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star)); 754bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 764bbf5ea8SMatthew G. Knepley } 774bbf5ea8SMatthew G. Knepley 78e5b9877fSPatrick Farrell static PetscErrorCode PCPatchConstruct_Pardecomp(void *vpatch, DM dm, PetscInt point, PetscHSetI ht) 790a390943SPatrick Farrell { 80b525f888SPatrick Farrell PC_PATCH *patch = (PC_PATCH *) vpatch; 810a390943SPatrick Farrell DMLabel ghost = NULL; 820a390943SPatrick Farrell const PetscInt *leaves; 830a390943SPatrick Farrell PetscInt nleaves, pStart, pEnd, loc; 840a390943SPatrick Farrell PetscBool isFiredrake; 850a390943SPatrick Farrell PetscBool flg; 86b525f888SPatrick Farrell PetscInt starSize; 87b525f888SPatrick Farrell PetscInt *star = NULL; 8825fd193aSPatrick Farrell PetscInt opoint, overlapi; 890a390943SPatrick Farrell 900a390943SPatrick Farrell PetscFunctionBegin; 919566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(ht)); 920a390943SPatrick Farrell 939566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 940a390943SPatrick Farrell 959566063dSJacob Faibussowitsch PetscCall(DMHasLabel(dm, "pyop2_ghost", &isFiredrake)); 960a390943SPatrick Farrell if (isFiredrake) { 979566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, "pyop2_ghost", &ghost)); 989566063dSJacob Faibussowitsch PetscCall(DMLabelCreateIndex(ghost, pStart, pEnd)); 990a390943SPatrick Farrell } else { 1000a390943SPatrick Farrell PetscSF sf; 1019566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm, &sf)); 1029566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL)); 1030a390943SPatrick Farrell nleaves = PetscMax(nleaves, 0); 1040a390943SPatrick Farrell } 1050a390943SPatrick Farrell 10625fd193aSPatrick Farrell for (opoint = pStart; opoint < pEnd; ++opoint) { 1079566063dSJacob Faibussowitsch if (ghost) PetscCall(DMLabelHasPoint(ghost, opoint, &flg)); 1089566063dSJacob Faibussowitsch else {PetscCall(PetscFindInt(opoint, nleaves, leaves, &loc)); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;} 1090a390943SPatrick Farrell /* Not an owned entity, don't make a cell patch. */ 1100a390943SPatrick Farrell if (flg) continue; 1119566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(ht, opoint)); 1120a390943SPatrick Farrell } 1130a390943SPatrick Farrell 114b525f888SPatrick Farrell /* Now build the overlap for the patch */ 11525fd193aSPatrick Farrell for (overlapi = 0; overlapi < patch->pardecomp_overlap; ++overlapi) { 116b525f888SPatrick Farrell PetscInt index = 0; 117b525f888SPatrick Farrell PetscInt *htpoints = NULL; 118b525f888SPatrick Farrell PetscInt htsize; 11925fd193aSPatrick Farrell PetscInt i; 120b525f888SPatrick Farrell 1219566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(ht, &htsize)); 1229566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(htsize, &htpoints)); 1239566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetElems(ht, &index, htpoints)); 124b525f888SPatrick Farrell 12525fd193aSPatrick Farrell for (i = 0; i < htsize; ++i) { 12625fd193aSPatrick Farrell PetscInt hpoint = htpoints[i]; 12725fd193aSPatrick Farrell PetscInt si; 128b525f888SPatrick Farrell 1299566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star)); 13025fd193aSPatrick Farrell for (si = 0; si < starSize*2; si += 2) { 131b525f888SPatrick Farrell const PetscInt starp = star[si]; 132b525f888SPatrick Farrell PetscInt closureSize; 133b525f888SPatrick Farrell PetscInt *closure = NULL, ci; 134b525f888SPatrick Farrell 135b525f888SPatrick Farrell /* now loop over all entities in the closure of starp */ 1369566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure)); 13725fd193aSPatrick Farrell for (ci = 0; ci < closureSize*2; ci += 2) { 138b525f888SPatrick Farrell const PetscInt closstarp = closure[ci]; 1399566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(ht, closstarp)); 140b525f888SPatrick Farrell } 1419566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure)); 142b525f888SPatrick Farrell } 1439566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star)); 144b525f888SPatrick Farrell } 1459566063dSJacob Faibussowitsch PetscCall(PetscFree(htpoints)); 146b525f888SPatrick Farrell } 147b525f888SPatrick Farrell 1480a390943SPatrick Farrell PetscFunctionReturn(0); 1490a390943SPatrick Farrell } 1500a390943SPatrick Farrell 1514bbf5ea8SMatthew G. Knepley /* The user's already set the patches in patch->userIS. Build the hash tables */ 1521b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_User(void *vpatch, DM dm, PetscInt point, PetscHSetI ht) 1534bbf5ea8SMatthew G. Knepley { 1544bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) vpatch; 1554bbf5ea8SMatthew G. Knepley IS patchis = patch->userIS[point]; 1564bbf5ea8SMatthew G. Knepley PetscInt n; 1574bbf5ea8SMatthew G. Knepley const PetscInt *patchdata; 1584bbf5ea8SMatthew G. Knepley PetscInt pStart, pEnd, i; 1594bbf5ea8SMatthew G. Knepley 1604bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 1619566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(ht)); 1629566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 1639566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(patchis, &n)); 1649566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patchis, &patchdata)); 1654bbf5ea8SMatthew G. Knepley for (i = 0; i < n; ++i) { 1664bbf5ea8SMatthew G. Knepley const PetscInt ownedpoint = patchdata[i]; 1674bbf5ea8SMatthew G. Knepley 168*7a46b595SBarry 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); 1699566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(ht, ownedpoint)); 1704bbf5ea8SMatthew G. Knepley } 1719566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patchis, &patchdata)); 1724bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 1734bbf5ea8SMatthew G. Knepley } 1744bbf5ea8SMatthew G. Knepley 1754bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateDefaultSF_Private(PC pc, PetscInt n, const PetscSF *sf, const PetscInt *bs) 1764bbf5ea8SMatthew G. Knepley { 1774bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 1784bbf5ea8SMatthew G. Knepley PetscInt i; 1794bbf5ea8SMatthew G. Knepley 1804bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 1814bbf5ea8SMatthew G. Knepley if (n == 1 && bs[0] == 1) { 1821bb6d2a8SBarry Smith patch->sectionSF = sf[0]; 1839566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject) patch->sectionSF)); 1844bbf5ea8SMatthew G. Knepley } else { 1854bbf5ea8SMatthew G. Knepley PetscInt allRoots = 0, allLeaves = 0; 1864bbf5ea8SMatthew G. Knepley PetscInt leafOffset = 0; 1874bbf5ea8SMatthew G. Knepley PetscInt *ilocal = NULL; 1884bbf5ea8SMatthew G. Knepley PetscSFNode *iremote = NULL; 1894bbf5ea8SMatthew G. Knepley PetscInt *remoteOffsets = NULL; 1904bbf5ea8SMatthew G. Knepley PetscInt index = 0; 1911b68eb51SMatthew G. Knepley PetscHMapI rankToIndex; 1924bbf5ea8SMatthew G. Knepley PetscInt numRanks = 0; 1934bbf5ea8SMatthew G. Knepley PetscSFNode *remote = NULL; 1944bbf5ea8SMatthew G. Knepley PetscSF rankSF; 1954bbf5ea8SMatthew G. Knepley PetscInt *ranks = NULL; 1964bbf5ea8SMatthew G. Knepley PetscInt *offsets = NULL; 1974bbf5ea8SMatthew G. Knepley MPI_Datatype contig; 1981b68eb51SMatthew G. Knepley PetscHSetI ranksUniq; 1994bbf5ea8SMatthew G. Knepley 2004bbf5ea8SMatthew G. Knepley /* First figure out how many dofs there are in the concatenated numbering. 2014bbf5ea8SMatthew G. Knepley * allRoots: number of owned global dofs; 2024bbf5ea8SMatthew G. Knepley * allLeaves: number of visible dofs (global + ghosted). 2034bbf5ea8SMatthew G. Knepley */ 2044bbf5ea8SMatthew G. Knepley for (i = 0; i < n; ++i) { 2054bbf5ea8SMatthew G. Knepley PetscInt nroots, nleaves; 2064bbf5ea8SMatthew G. Knepley 2079566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sf[i], &nroots, &nleaves, NULL, NULL)); 2084bbf5ea8SMatthew G. Knepley allRoots += nroots * bs[i]; 2094bbf5ea8SMatthew G. Knepley allLeaves += nleaves * bs[i]; 2104bbf5ea8SMatthew G. Knepley } 2119566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(allLeaves, &ilocal)); 2129566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(allLeaves, &iremote)); 2134bbf5ea8SMatthew G. Knepley /* Now build an SF that just contains process connectivity. */ 2149566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&ranksUniq)); 2154bbf5ea8SMatthew G. Knepley for (i = 0; i < n; ++i) { 2164bbf5ea8SMatthew G. Knepley const PetscMPIInt *ranks = NULL; 2174bbf5ea8SMatthew G. Knepley PetscInt nranks, j; 2184bbf5ea8SMatthew G. Knepley 2199566063dSJacob Faibussowitsch PetscCall(PetscSFSetUp(sf[i])); 2209566063dSJacob Faibussowitsch PetscCall(PetscSFGetRootRanks(sf[i], &nranks, &ranks, NULL, NULL, NULL)); 2214bbf5ea8SMatthew G. Knepley /* These are all the ranks who communicate with me. */ 2224bbf5ea8SMatthew G. Knepley for (j = 0; j < nranks; ++j) { 2239566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(ranksUniq, (PetscInt) ranks[j])); 2244bbf5ea8SMatthew G. Knepley } 2254bbf5ea8SMatthew G. Knepley } 2269566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(ranksUniq, &numRanks)); 2279566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numRanks, &remote)); 2289566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numRanks, &ranks)); 2299566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetElems(ranksUniq, &index, ranks)); 2304bbf5ea8SMatthew G. Knepley 2319566063dSJacob Faibussowitsch PetscCall(PetscHMapICreate(&rankToIndex)); 2324bbf5ea8SMatthew G. Knepley for (i = 0; i < numRanks; ++i) { 2334bbf5ea8SMatthew G. Knepley remote[i].rank = ranks[i]; 2344bbf5ea8SMatthew G. Knepley remote[i].index = 0; 2359566063dSJacob Faibussowitsch PetscCall(PetscHMapISet(rankToIndex, ranks[i], i)); 2364bbf5ea8SMatthew G. Knepley } 2379566063dSJacob Faibussowitsch PetscCall(PetscFree(ranks)); 2389566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&ranksUniq)); 2399566063dSJacob Faibussowitsch PetscCall(PetscSFCreate(PetscObjectComm((PetscObject) pc), &rankSF)); 2409566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(rankSF, 1, numRanks, NULL, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER)); 2419566063dSJacob Faibussowitsch PetscCall(PetscSFSetUp(rankSF)); 2424bbf5ea8SMatthew G. Knepley /* OK, use it to communicate the root offset on the remote 2434bbf5ea8SMatthew G. Knepley * processes for each subspace. */ 2449566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(n, &offsets)); 2459566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(n*numRanks, &remoteOffsets)); 2464bbf5ea8SMatthew G. Knepley 2474bbf5ea8SMatthew G. Knepley offsets[0] = 0; 2484bbf5ea8SMatthew G. Knepley for (i = 1; i < n; ++i) { 2494bbf5ea8SMatthew G. Knepley PetscInt nroots; 2504bbf5ea8SMatthew G. Knepley 2519566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sf[i-1], &nroots, NULL, NULL, NULL)); 2524bbf5ea8SMatthew G. Knepley offsets[i] = offsets[i-1] + nroots*bs[i-1]; 2534bbf5ea8SMatthew G. Knepley } 2544bbf5ea8SMatthew G. Knepley /* Offsets are the offsets on the current process of the 2554bbf5ea8SMatthew G. Knepley * global dof numbering for the subspaces. */ 2569566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_contiguous(n, MPIU_INT, &contig)); 2579566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_commit(&contig)); 2584bbf5ea8SMatthew G. Knepley 2599566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(rankSF, contig, offsets, remoteOffsets,MPI_REPLACE)); 2609566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(rankSF, contig, offsets, remoteOffsets,MPI_REPLACE)); 2619566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_free(&contig)); 2629566063dSJacob Faibussowitsch PetscCall(PetscFree(offsets)); 2639566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&rankSF)); 2644bbf5ea8SMatthew G. Knepley /* Now remoteOffsets contains the offsets on the remote 2654bbf5ea8SMatthew G. Knepley * processes who communicate with me. So now we can 2664bbf5ea8SMatthew G. Knepley * concatenate the list of SFs into a single one. */ 2674bbf5ea8SMatthew G. Knepley index = 0; 2684bbf5ea8SMatthew G. Knepley for (i = 0; i < n; ++i) { 2694bbf5ea8SMatthew G. Knepley const PetscSFNode *remote = NULL; 2704bbf5ea8SMatthew G. Knepley const PetscInt *local = NULL; 2714bbf5ea8SMatthew G. Knepley PetscInt nroots, nleaves, j; 2724bbf5ea8SMatthew G. Knepley 2739566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sf[i], &nroots, &nleaves, &local, &remote)); 2744bbf5ea8SMatthew G. Knepley for (j = 0; j < nleaves; ++j) { 2754bbf5ea8SMatthew G. Knepley PetscInt rank = remote[j].rank; 2764bbf5ea8SMatthew G. Knepley PetscInt idx, rootOffset, k; 2774bbf5ea8SMatthew G. Knepley 2789566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(rankToIndex, rank, &idx)); 27908401ef6SPierre Jolivet PetscCheck(idx != -1,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Didn't find rank, huh?"); 2804bbf5ea8SMatthew G. Knepley /* Offset on given rank for ith subspace */ 2814bbf5ea8SMatthew G. Knepley rootOffset = remoteOffsets[n*idx + i]; 2824bbf5ea8SMatthew G. Knepley for (k = 0; k < bs[i]; ++k) { 28373ec7555SLawrence Mitchell ilocal[index] = (local ? local[j] : j)*bs[i] + k + leafOffset; 2844bbf5ea8SMatthew G. Knepley iremote[index].rank = remote[j].rank; 2854bbf5ea8SMatthew G. Knepley iremote[index].index = remote[j].index*bs[i] + k + rootOffset; 2864bbf5ea8SMatthew G. Knepley ++index; 2874bbf5ea8SMatthew G. Knepley } 2884bbf5ea8SMatthew G. Knepley } 2894bbf5ea8SMatthew G. Knepley leafOffset += nleaves * bs[i]; 2904bbf5ea8SMatthew G. Knepley } 2919566063dSJacob Faibussowitsch PetscCall(PetscHMapIDestroy(&rankToIndex)); 2929566063dSJacob Faibussowitsch PetscCall(PetscFree(remoteOffsets)); 2939566063dSJacob Faibussowitsch PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)pc), &patch->sectionSF)); 2949566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(patch->sectionSF, allRoots, allLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER)); 2954bbf5ea8SMatthew G. Knepley } 2964bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 2974bbf5ea8SMatthew G. Knepley } 2984bbf5ea8SMatthew G. Knepley 299c73d2cf6SLawrence Mitchell PetscErrorCode PCPatchSetDenseInverse(PC pc, PetscBool flg) 300c73d2cf6SLawrence Mitchell { 301c73d2cf6SLawrence Mitchell PC_PATCH *patch = (PC_PATCH *) pc->data; 302c73d2cf6SLawrence Mitchell PetscFunctionBegin; 303c73d2cf6SLawrence Mitchell patch->denseinverse = flg; 304c73d2cf6SLawrence Mitchell PetscFunctionReturn(0); 305c73d2cf6SLawrence Mitchell } 306c73d2cf6SLawrence Mitchell 307c73d2cf6SLawrence Mitchell PetscErrorCode PCPatchGetDenseInverse(PC pc, PetscBool *flg) 308c73d2cf6SLawrence Mitchell { 309c73d2cf6SLawrence Mitchell PC_PATCH *patch = (PC_PATCH *) pc->data; 310c73d2cf6SLawrence Mitchell PetscFunctionBegin; 311c73d2cf6SLawrence Mitchell *flg = patch->denseinverse; 312c73d2cf6SLawrence Mitchell PetscFunctionReturn(0); 313c73d2cf6SLawrence Mitchell } 314c73d2cf6SLawrence Mitchell 3154bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 3165f824522SMatthew G. Knepley PetscErrorCode PCPatchSetIgnoreDim(PC pc, PetscInt dim) 3175f824522SMatthew G. Knepley { 3185f824522SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 3195f824522SMatthew G. Knepley PetscFunctionBegin; 3205f824522SMatthew G. Knepley patch->ignoredim = dim; 3215f824522SMatthew G. Knepley PetscFunctionReturn(0); 3225f824522SMatthew G. Knepley } 3235f824522SMatthew G. Knepley 3245f824522SMatthew G. Knepley /* TODO: Docs */ 3255f824522SMatthew G. Knepley PetscErrorCode PCPatchGetIgnoreDim(PC pc, PetscInt *dim) 3265f824522SMatthew G. Knepley { 3275f824522SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 3285f824522SMatthew G. Knepley PetscFunctionBegin; 3295f824522SMatthew G. Knepley *dim = patch->ignoredim; 3305f824522SMatthew G. Knepley PetscFunctionReturn(0); 3315f824522SMatthew G. Knepley } 3325f824522SMatthew G. Knepley 3335f824522SMatthew G. Knepley /* TODO: Docs */ 3344bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetSaveOperators(PC pc, PetscBool flg) 3354bbf5ea8SMatthew G. Knepley { 3364bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 3374bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 3384bbf5ea8SMatthew G. Knepley patch->save_operators = flg; 3394bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 3404bbf5ea8SMatthew G. Knepley } 3414bbf5ea8SMatthew G. Knepley 3424bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 3434bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetSaveOperators(PC pc, PetscBool *flg) 3444bbf5ea8SMatthew G. Knepley { 3454bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 3464bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 3474bbf5ea8SMatthew G. Knepley *flg = patch->save_operators; 3484bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 3494bbf5ea8SMatthew G. Knepley } 3504bbf5ea8SMatthew G. Knepley 3514bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 352fa84ea4cSLawrence Mitchell PetscErrorCode PCPatchSetPrecomputeElementTensors(PC pc, PetscBool flg) 353fa84ea4cSLawrence Mitchell { 354fa84ea4cSLawrence Mitchell PC_PATCH *patch = (PC_PATCH *) pc->data; 355fa84ea4cSLawrence Mitchell PetscFunctionBegin; 356fa84ea4cSLawrence Mitchell patch->precomputeElementTensors = flg; 357fa84ea4cSLawrence Mitchell PetscFunctionReturn(0); 358fa84ea4cSLawrence Mitchell } 359fa84ea4cSLawrence Mitchell 360fa84ea4cSLawrence Mitchell /* TODO: Docs */ 361fa84ea4cSLawrence Mitchell PetscErrorCode PCPatchGetPrecomputeElementTensors(PC pc, PetscBool *flg) 362fa84ea4cSLawrence Mitchell { 363fa84ea4cSLawrence Mitchell PC_PATCH *patch = (PC_PATCH *) pc->data; 364fa84ea4cSLawrence Mitchell PetscFunctionBegin; 365fa84ea4cSLawrence Mitchell *flg = patch->precomputeElementTensors; 366fa84ea4cSLawrence Mitchell PetscFunctionReturn(0); 367fa84ea4cSLawrence Mitchell } 368fa84ea4cSLawrence Mitchell 369fa84ea4cSLawrence Mitchell /* TODO: Docs */ 3704bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetPartitionOfUnity(PC pc, PetscBool flg) 3714bbf5ea8SMatthew G. Knepley { 3724bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 3734bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 3744bbf5ea8SMatthew G. Knepley patch->partition_of_unity = flg; 3754bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 3764bbf5ea8SMatthew G. Knepley } 3774bbf5ea8SMatthew G. Knepley 3784bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 3794bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetPartitionOfUnity(PC pc, PetscBool *flg) 3804bbf5ea8SMatthew G. Knepley { 3814bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 3824bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 3834bbf5ea8SMatthew G. Knepley *flg = patch->partition_of_unity; 3844bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 3854bbf5ea8SMatthew G. Knepley } 3864bbf5ea8SMatthew G. Knepley 3874bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 38861c4b389SFlorian Wechsung PetscErrorCode PCPatchSetLocalComposition(PC pc, PCCompositeType type) 389c2e6f3c0SFlorian Wechsung { 390c2e6f3c0SFlorian Wechsung PC_PATCH *patch = (PC_PATCH *) pc->data; 391c2e6f3c0SFlorian Wechsung PetscFunctionBegin; 3922472a847SBarry 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"); 39361c4b389SFlorian Wechsung patch->local_composition_type = type; 394c2e6f3c0SFlorian Wechsung PetscFunctionReturn(0); 395c2e6f3c0SFlorian Wechsung } 396c2e6f3c0SFlorian Wechsung 397c2e6f3c0SFlorian Wechsung /* TODO: Docs */ 39861c4b389SFlorian Wechsung PetscErrorCode PCPatchGetLocalComposition(PC pc, PCCompositeType *type) 399c2e6f3c0SFlorian Wechsung { 400c2e6f3c0SFlorian Wechsung PC_PATCH *patch = (PC_PATCH *) pc->data; 401c2e6f3c0SFlorian Wechsung PetscFunctionBegin; 40261c4b389SFlorian Wechsung *type = patch->local_composition_type; 403c2e6f3c0SFlorian Wechsung PetscFunctionReturn(0); 404c2e6f3c0SFlorian Wechsung } 405c2e6f3c0SFlorian Wechsung 406c2e6f3c0SFlorian Wechsung /* TODO: Docs */ 4074bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetSubMatType(PC pc, MatType sub_mat_type) 4084bbf5ea8SMatthew G. Knepley { 4094bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 4104bbf5ea8SMatthew G. Knepley 4114bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 4129566063dSJacob Faibussowitsch if (patch->sub_mat_type) PetscCall(PetscFree(patch->sub_mat_type)); 4139566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(sub_mat_type, (char **) &patch->sub_mat_type)); 4144bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 4154bbf5ea8SMatthew G. Knepley } 4164bbf5ea8SMatthew G. Knepley 4174bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 4184bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetSubMatType(PC pc, MatType *sub_mat_type) 4194bbf5ea8SMatthew G. Knepley { 4204bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 4214bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 4224bbf5ea8SMatthew G. Knepley *sub_mat_type = patch->sub_mat_type; 4234bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 4244bbf5ea8SMatthew G. Knepley } 4254bbf5ea8SMatthew G. Knepley 4264bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 4274bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetCellNumbering(PC pc, PetscSection cellNumbering) 4284bbf5ea8SMatthew G. Knepley { 4294bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 4304bbf5ea8SMatthew G. Knepley 4314bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 4324bbf5ea8SMatthew G. Knepley patch->cellNumbering = cellNumbering; 4339566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject) cellNumbering)); 4344bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 4354bbf5ea8SMatthew G. Knepley } 4364bbf5ea8SMatthew G. Knepley 4374bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 4384bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetCellNumbering(PC pc, PetscSection *cellNumbering) 4394bbf5ea8SMatthew G. Knepley { 4404bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 4414bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 4424bbf5ea8SMatthew G. Knepley *cellNumbering = patch->cellNumbering; 4434bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 4444bbf5ea8SMatthew G. Knepley } 4454bbf5ea8SMatthew G. Knepley 4464bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 4474bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetConstructType(PC pc, PCPatchConstructType ctype, PetscErrorCode (*func)(PC, PetscInt *, IS **, IS *, void *), void *ctx) 4484bbf5ea8SMatthew G. Knepley { 4494bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 4504bbf5ea8SMatthew G. Knepley 4514bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 4524bbf5ea8SMatthew G. Knepley patch->ctype = ctype; 4534bbf5ea8SMatthew G. Knepley switch (ctype) { 4544bbf5ea8SMatthew G. Knepley case PC_PATCH_STAR: 45540c17a03SPatrick Farrell patch->user_patches = PETSC_FALSE; 4564bbf5ea8SMatthew G. Knepley patch->patchconstructop = PCPatchConstruct_Star; 4574bbf5ea8SMatthew G. Knepley break; 4584bbf5ea8SMatthew G. Knepley case PC_PATCH_VANKA: 45940c17a03SPatrick Farrell patch->user_patches = PETSC_FALSE; 4604bbf5ea8SMatthew G. Knepley patch->patchconstructop = PCPatchConstruct_Vanka; 4614bbf5ea8SMatthew G. Knepley break; 462e5b9877fSPatrick Farrell case PC_PATCH_PARDECOMP: 4630a390943SPatrick Farrell patch->user_patches = PETSC_FALSE; 464e5b9877fSPatrick Farrell patch->patchconstructop = PCPatchConstruct_Pardecomp; 4650a390943SPatrick Farrell break; 4664bbf5ea8SMatthew G. Knepley case PC_PATCH_USER: 4674bbf5ea8SMatthew G. Knepley case PC_PATCH_PYTHON: 4684bbf5ea8SMatthew G. Knepley patch->user_patches = PETSC_TRUE; 4694bbf5ea8SMatthew G. Knepley patch->patchconstructop = PCPatchConstruct_User; 470bdd9e0cdSPatrick Farrell if (func) { 4714bbf5ea8SMatthew G. Knepley patch->userpatchconstructionop = func; 4724bbf5ea8SMatthew G. Knepley patch->userpatchconstructctx = ctx; 473bdd9e0cdSPatrick Farrell } 4744bbf5ea8SMatthew G. Knepley break; 4754bbf5ea8SMatthew G. Knepley default: 47663a3b9bcSJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %" PetscInt_FMT, (PetscInt) patch->ctype); 4774bbf5ea8SMatthew G. Knepley } 4784bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 4794bbf5ea8SMatthew G. Knepley } 4804bbf5ea8SMatthew G. Knepley 4814bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 4824bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetConstructType(PC pc, PCPatchConstructType *ctype, PetscErrorCode (**func)(PC, PetscInt *, IS **, IS *, void *), void **ctx) 4834bbf5ea8SMatthew G. Knepley { 4844bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 4854bbf5ea8SMatthew G. Knepley 4864bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 4874bbf5ea8SMatthew G. Knepley *ctype = patch->ctype; 4884bbf5ea8SMatthew G. Knepley switch (patch->ctype) { 4894bbf5ea8SMatthew G. Knepley case PC_PATCH_STAR: 4904bbf5ea8SMatthew G. Knepley case PC_PATCH_VANKA: 491e5b9877fSPatrick Farrell case PC_PATCH_PARDECOMP: 4924bbf5ea8SMatthew G. Knepley break; 4934bbf5ea8SMatthew G. Knepley case PC_PATCH_USER: 4944bbf5ea8SMatthew G. Knepley case PC_PATCH_PYTHON: 4954bbf5ea8SMatthew G. Knepley *func = patch->userpatchconstructionop; 4964bbf5ea8SMatthew G. Knepley *ctx = patch->userpatchconstructctx; 4974bbf5ea8SMatthew G. Knepley break; 4984bbf5ea8SMatthew G. Knepley default: 49963a3b9bcSJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %" PetscInt_FMT, (PetscInt) patch->ctype); 5004bbf5ea8SMatthew G. Knepley } 5014bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 5024bbf5ea8SMatthew G. Knepley } 5034bbf5ea8SMatthew G. Knepley 5044bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 5054bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfo(PC pc, PetscInt nsubspaces, DM *dms, PetscInt *bs, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, 5064bbf5ea8SMatthew G. Knepley const PetscInt *subspaceOffsets, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes) 5074bbf5ea8SMatthew G. Knepley { 5084bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 509b6bb21d1SLawrence Mitchell DM dm, plex; 5104bbf5ea8SMatthew G. Knepley PetscSF *sfs; 5115f824522SMatthew G. Knepley PetscInt cStart, cEnd, i, j; 5124bbf5ea8SMatthew G. Knepley 5134bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 5149566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm)); 5159566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex)); 516b6bb21d1SLawrence Mitchell dm = plex; 5179566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 5189566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nsubspaces, &sfs)); 5199566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nsubspaces, &patch->dofSection)); 5209566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nsubspaces, &patch->bs)); 5219566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nsubspaces, &patch->nodesPerCell)); 5229566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nsubspaces, &patch->cellNodeMap)); 5239566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nsubspaces+1, &patch->subspaceOffsets)); 5244bbf5ea8SMatthew G. Knepley 5254bbf5ea8SMatthew G. Knepley patch->nsubspaces = nsubspaces; 5264bbf5ea8SMatthew G. Knepley patch->totalDofsPerCell = 0; 5274bbf5ea8SMatthew G. Knepley for (i = 0; i < nsubspaces; ++i) { 5289566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dms[i], &patch->dofSection[i])); 5299566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject) patch->dofSection[i])); 5309566063dSJacob Faibussowitsch PetscCall(DMGetSectionSF(dms[i], &sfs[i])); 5314bbf5ea8SMatthew G. Knepley patch->bs[i] = bs[i]; 5324bbf5ea8SMatthew G. Knepley patch->nodesPerCell[i] = nodesPerCell[i]; 5334bbf5ea8SMatthew G. Knepley patch->totalDofsPerCell += nodesPerCell[i]*bs[i]; 5349566063dSJacob Faibussowitsch PetscCall(PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i])); 53580e8a965SFlorian Wechsung for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j]; 5364bbf5ea8SMatthew G. Knepley patch->subspaceOffsets[i] = subspaceOffsets[i]; 5374bbf5ea8SMatthew G. Knepley } 5389566063dSJacob Faibussowitsch PetscCall(PCPatchCreateDefaultSF_Private(pc, nsubspaces, sfs, patch->bs)); 5399566063dSJacob Faibussowitsch PetscCall(PetscFree(sfs)); 5404bbf5ea8SMatthew G. Knepley 5414bbf5ea8SMatthew G. Knepley patch->subspaceOffsets[nsubspaces] = subspaceOffsets[nsubspaces]; 5429566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes)); 5439566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes)); 5449566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm)); 5454bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 5464bbf5ea8SMatthew G. Knepley } 5474bbf5ea8SMatthew G. Knepley 5484bbf5ea8SMatthew G. Knepley /* TODO: Docs */ 5495f824522SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfoCombined(PC pc, DM dm, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes) 5505f824522SMatthew G. Knepley { 5515f824522SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 5525f824522SMatthew G. Knepley PetscInt cStart, cEnd, i, j; 5535f824522SMatthew G. Knepley 5545f824522SMatthew G. Knepley PetscFunctionBegin; 5555f824522SMatthew G. Knepley patch->combined = PETSC_TRUE; 5569566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 5579566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &patch->nsubspaces)); 5589566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(patch->nsubspaces, &patch->dofSection)); 5599566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(patch->nsubspaces, &patch->bs)); 5609566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(patch->nsubspaces, &patch->nodesPerCell)); 5619566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(patch->nsubspaces, &patch->cellNodeMap)); 5629566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(patch->nsubspaces+1, &patch->subspaceOffsets)); 5639566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, &patch->dofSection[0])); 5649566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject) patch->dofSection[0])); 5659566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(patch->dofSection[0], &patch->subspaceOffsets[patch->nsubspaces])); 5665f824522SMatthew G. Knepley patch->totalDofsPerCell = 0; 5675f824522SMatthew G. Knepley for (i = 0; i < patch->nsubspaces; ++i) { 5685f824522SMatthew G. Knepley patch->bs[i] = 1; 5695f824522SMatthew G. Knepley patch->nodesPerCell[i] = nodesPerCell[i]; 5705f824522SMatthew G. Knepley patch->totalDofsPerCell += nodesPerCell[i]; 5719566063dSJacob Faibussowitsch PetscCall(PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i])); 5725f824522SMatthew G. Knepley for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j]; 5735f824522SMatthew G. Knepley } 5749566063dSJacob Faibussowitsch PetscCall(DMGetSectionSF(dm, &patch->sectionSF)); 5759566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject) patch->sectionSF)); 5769566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes)); 5779566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes)); 5785f824522SMatthew G. Knepley PetscFunctionReturn(0); 5795f824522SMatthew G. Knepley } 5805f824522SMatthew G. Knepley 5815f824522SMatthew G. Knepley /*@C 5825f824522SMatthew G. Knepley 58392d50984SMatthew G. Knepley PCPatchSetComputeFunction - Set the callback used to compute patch residuals 58492d50984SMatthew G. Knepley 58599b7e5c6SPatrick Farrell Logically collective on PC 58699b7e5c6SPatrick Farrell 58792d50984SMatthew G. Knepley Input Parameters: 58892d50984SMatthew G. Knepley + pc - The PC 58992d50984SMatthew G. Knepley . func - The callback 59092d50984SMatthew G. Knepley - ctx - The user context 59192d50984SMatthew G. Knepley 5927a50e09dSPatrick Farrell Calling sequence of func: 5937a50e09dSPatrick Farrell $ func (PC pc,PetscInt point,Vec x,Vec f,IS cellIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx) 5947a50e09dSPatrick Farrell 5957a50e09dSPatrick Farrell + pc - The PC 5967a50e09dSPatrick Farrell . point - The point 5977a50e09dSPatrick Farrell . x - The input solution (not used in linear problems) 5987a50e09dSPatrick Farrell . f - The patch residual vector 5997a50e09dSPatrick Farrell . cellIS - An array of the cell numbers 6007a50e09dSPatrick Farrell . n - The size of dofsArray 6017a50e09dSPatrick Farrell . dofsArray - The dofmap for the dofs to be solved for 6027a50e09dSPatrick Farrell . dofsArrayWithAll - The dofmap for all dofs on the patch 6037a50e09dSPatrick Farrell - ctx - The user context 6047a50e09dSPatrick Farrell 60592d50984SMatthew G. Knepley Level: advanced 60692d50984SMatthew G. Knepley 6077a50e09dSPatrick Farrell Notes: 60826dc5b63SLawrence Mitchell The entries of F (the output residual vector) have been set to zero before the call. 60992d50984SMatthew G. Knepley 610db781477SPatrick Sanan .seealso: `PCPatchSetComputeOperator()`, `PCPatchGetComputeOperator()`, `PCPatchSetDiscretisationInfo()`, `PCPatchSetComputeFunctionInteriorFacets()` 61192d50984SMatthew G. Knepley @*/ 61239fd2e8aSPatrick Farrell PetscErrorCode PCPatchSetComputeFunction(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Vec, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx) 61392d50984SMatthew G. Knepley { 61492d50984SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 61592d50984SMatthew G. Knepley 61692d50984SMatthew G. Knepley PetscFunctionBegin; 61792d50984SMatthew G. Knepley patch->usercomputef = func; 61892d50984SMatthew G. Knepley patch->usercomputefctx = ctx; 61992d50984SMatthew G. Knepley PetscFunctionReturn(0); 62092d50984SMatthew G. Knepley } 62192d50984SMatthew G. Knepley 62292d50984SMatthew G. Knepley /*@C 62392d50984SMatthew G. Knepley 62459109abcSLawrence Mitchell PCPatchSetComputeFunctionInteriorFacets - Set the callback used to compute facet integrals for patch residuals 62559109abcSLawrence Mitchell 6267a50e09dSPatrick Farrell Logically collective on PC 6277a50e09dSPatrick Farrell 62859109abcSLawrence Mitchell Input Parameters: 62959109abcSLawrence Mitchell + pc - The PC 63059109abcSLawrence Mitchell . func - The callback 63159109abcSLawrence Mitchell - ctx - The user context 63259109abcSLawrence Mitchell 6337a50e09dSPatrick Farrell Calling sequence of func: 6347a50e09dSPatrick Farrell $ func (PC pc,PetscInt point,Vec x,Vec f,IS facetIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx) 6357a50e09dSPatrick Farrell 6367a50e09dSPatrick Farrell + pc - The PC 6377a50e09dSPatrick Farrell . point - The point 6387a50e09dSPatrick Farrell . x - The input solution (not used in linear problems) 6397a50e09dSPatrick Farrell . f - The patch residual vector 6407a50e09dSPatrick Farrell . facetIS - An array of the facet numbers 6417a50e09dSPatrick Farrell . n - The size of dofsArray 6427a50e09dSPatrick Farrell . dofsArray - The dofmap for the dofs to be solved for 6437a50e09dSPatrick Farrell . dofsArrayWithAll - The dofmap for all dofs on the patch 6447a50e09dSPatrick Farrell - ctx - The user context 6457a50e09dSPatrick Farrell 64659109abcSLawrence Mitchell Level: advanced 64759109abcSLawrence Mitchell 6487a50e09dSPatrick Farrell Notes: 64926dc5b63SLawrence Mitchell The entries of F (the output residual vector) have been set to zero before the call. 65059109abcSLawrence Mitchell 651db781477SPatrick Sanan .seealso: `PCPatchSetComputeOperator()`, `PCPatchGetComputeOperator()`, `PCPatchSetDiscretisationInfo()`, `PCPatchSetComputeFunction()` 65259109abcSLawrence Mitchell @*/ 65359109abcSLawrence Mitchell PetscErrorCode PCPatchSetComputeFunctionInteriorFacets(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Vec, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx) 65459109abcSLawrence Mitchell { 65559109abcSLawrence Mitchell PC_PATCH *patch = (PC_PATCH *) pc->data; 65659109abcSLawrence Mitchell 65759109abcSLawrence Mitchell PetscFunctionBegin; 65859109abcSLawrence Mitchell patch->usercomputefintfacet = func; 65959109abcSLawrence Mitchell patch->usercomputefintfacetctx = ctx; 66059109abcSLawrence Mitchell PetscFunctionReturn(0); 66159109abcSLawrence Mitchell } 66259109abcSLawrence Mitchell 66359109abcSLawrence Mitchell /*@C 66459109abcSLawrence Mitchell 6655f824522SMatthew G. Knepley PCPatchSetComputeOperator - Set the callback used to compute patch matrices 6665f824522SMatthew G. Knepley 6677a50e09dSPatrick Farrell Logically collective on PC 6687a50e09dSPatrick Farrell 6695f824522SMatthew G. Knepley Input Parameters: 6705f824522SMatthew G. Knepley + pc - The PC 6715f824522SMatthew G. Knepley . func - The callback 6725f824522SMatthew G. Knepley - ctx - The user context 6735f824522SMatthew G. Knepley 6747a50e09dSPatrick Farrell Calling sequence of func: 6757a50e09dSPatrick Farrell $ func (PC pc,PetscInt point,Vec x,Mat mat,IS facetIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx) 6767a50e09dSPatrick Farrell 6777a50e09dSPatrick Farrell + pc - The PC 6787a50e09dSPatrick Farrell . point - The point 6797a50e09dSPatrick Farrell . x - The input solution (not used in linear problems) 6807a50e09dSPatrick Farrell . mat - The patch matrix 6817a50e09dSPatrick Farrell . cellIS - An array of the cell numbers 6827a50e09dSPatrick Farrell . n - The size of dofsArray 6837a50e09dSPatrick Farrell . dofsArray - The dofmap for the dofs to be solved for 6847a50e09dSPatrick Farrell . dofsArrayWithAll - The dofmap for all dofs on the patch 6857a50e09dSPatrick Farrell - ctx - The user context 6867a50e09dSPatrick Farrell 6875f824522SMatthew G. Knepley Level: advanced 6885f824522SMatthew G. Knepley 6897a50e09dSPatrick Farrell Notes: 6907a50e09dSPatrick Farrell The matrix entries have been set to zero before the call. 6915f824522SMatthew G. Knepley 692db781477SPatrick Sanan .seealso: `PCPatchGetComputeOperator()`, `PCPatchSetComputeFunction()`, `PCPatchSetDiscretisationInfo()` 6935f824522SMatthew G. Knepley @*/ 6944d04e9f1SPatrick Farrell PetscErrorCode PCPatchSetComputeOperator(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Mat, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx) 6954bbf5ea8SMatthew G. Knepley { 6964bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 6974bbf5ea8SMatthew G. Knepley 6984bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 6994bbf5ea8SMatthew G. Knepley patch->usercomputeop = func; 700723f9013SMatthew G. Knepley patch->usercomputeopctx = ctx; 7014bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 7024bbf5ea8SMatthew G. Knepley } 7034bbf5ea8SMatthew G. Knepley 70459109abcSLawrence Mitchell /*@C 70559109abcSLawrence Mitchell 7067a50e09dSPatrick Farrell PCPatchSetComputeOperatorInteriorFacets - Set the callback used to compute facet integrals for patch matrices 70759109abcSLawrence Mitchell 70899b7e5c6SPatrick Farrell Logically collective on PC 70999b7e5c6SPatrick Farrell 71059109abcSLawrence Mitchell Input Parameters: 71159109abcSLawrence Mitchell + pc - The PC 71259109abcSLawrence Mitchell . func - The callback 71359109abcSLawrence Mitchell - ctx - The user context 71459109abcSLawrence Mitchell 7157a50e09dSPatrick Farrell Calling sequence of func: 7167a50e09dSPatrick Farrell $ func (PC pc,PetscInt point,Vec x,Mat mat,IS facetIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx) 7177a50e09dSPatrick Farrell 7187a50e09dSPatrick Farrell + pc - The PC 7197a50e09dSPatrick Farrell . point - The point 7207a50e09dSPatrick Farrell . x - The input solution (not used in linear problems) 7217a50e09dSPatrick Farrell . mat - The patch matrix 7227a50e09dSPatrick Farrell . facetIS - An array of the facet numbers 7237a50e09dSPatrick Farrell . n - The size of dofsArray 7247a50e09dSPatrick Farrell . dofsArray - The dofmap for the dofs to be solved for 7257a50e09dSPatrick Farrell . dofsArrayWithAll - The dofmap for all dofs on the patch 7267a50e09dSPatrick Farrell - ctx - The user context 7277a50e09dSPatrick Farrell 72859109abcSLawrence Mitchell Level: advanced 72959109abcSLawrence Mitchell 7307a50e09dSPatrick Farrell Notes: 7317a50e09dSPatrick Farrell The matrix entries have been set to zero before the call. 73259109abcSLawrence Mitchell 733db781477SPatrick Sanan .seealso: `PCPatchGetComputeOperator()`, `PCPatchSetComputeFunction()`, `PCPatchSetDiscretisationInfo()` 73459109abcSLawrence Mitchell @*/ 73559109abcSLawrence Mitchell PetscErrorCode PCPatchSetComputeOperatorInteriorFacets(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Mat, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx) 73659109abcSLawrence Mitchell { 73759109abcSLawrence Mitchell PC_PATCH *patch = (PC_PATCH *) pc->data; 73859109abcSLawrence Mitchell 73959109abcSLawrence Mitchell PetscFunctionBegin; 74059109abcSLawrence Mitchell patch->usercomputeopintfacet = func; 74159109abcSLawrence Mitchell patch->usercomputeopintfacetctx = ctx; 74259109abcSLawrence Mitchell PetscFunctionReturn(0); 74359109abcSLawrence Mitchell } 74459109abcSLawrence Mitchell 7454bbf5ea8SMatthew G. Knepley /* On entry, ht contains the topological entities whose dofs we are responsible for solving for; 7464bbf5ea8SMatthew G. Knepley on exit, cht contains all the topological entities we need to compute their residuals. 7474bbf5ea8SMatthew G. Knepley In full generality this should incorporate knowledge of the sparsity pattern of the matrix; 7484bbf5ea8SMatthew G. Knepley here we assume a standard FE sparsity pattern.*/ 7494bbf5ea8SMatthew G. Knepley /* TODO: Use DMPlexGetAdjacency() */ 7501b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchCompleteCellPatch(PC pc, PetscHSetI ht, PetscHSetI cht) 7514bbf5ea8SMatthew G. Knepley { 752b6bb21d1SLawrence Mitchell DM dm, plex; 753bc7fa33aSFlorian Wechsung PC_PATCH *patch = (PC_PATCH *) pc->data; 7541b68eb51SMatthew G. Knepley PetscHashIter hi; 7554bbf5ea8SMatthew G. Knepley PetscInt point; 7564bbf5ea8SMatthew G. Knepley PetscInt *star = NULL, *closure = NULL; 7574c954380SMatthew G. Knepley PetscInt ignoredim, iStart = 0, iEnd = -1, starSize, closureSize, si, ci; 758bc7fa33aSFlorian Wechsung PetscInt *fStar = NULL, *fClosure = NULL; 759bc7fa33aSFlorian Wechsung PetscInt fBegin, fEnd, fsi, fci, fStarSize, fClosureSize; 7604bbf5ea8SMatthew G. Knepley 7614bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 7629566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm)); 7639566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex)); 764b6bb21d1SLawrence Mitchell dm = plex; 7659566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fBegin, &fEnd)); 7669566063dSJacob Faibussowitsch PetscCall(PCPatchGetIgnoreDim(pc, &ignoredim)); 7679566063dSJacob Faibussowitsch if (ignoredim >= 0) PetscCall(DMPlexGetDepthStratum(dm, ignoredim, &iStart, &iEnd)); 7689566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(cht)); 7691b68eb51SMatthew G. Knepley PetscHashIterBegin(ht, hi); 7701b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(ht, hi)) { 7714c954380SMatthew G. Knepley 7721b68eb51SMatthew G. Knepley PetscHashIterGetKey(ht, hi, point); 7731b68eb51SMatthew G. Knepley PetscHashIterNext(ht, hi); 7744bbf5ea8SMatthew G. Knepley 7754bbf5ea8SMatthew G. Knepley /* Loop over all the cells that this point connects to */ 7769566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star)); 7775f824522SMatthew G. Knepley for (si = 0; si < starSize*2; si += 2) { 7784c954380SMatthew G. Knepley const PetscInt ownedpoint = star[si]; 7795f824522SMatthew G. Knepley /* TODO Check for point in cht before running through closure again */ 7804bbf5ea8SMatthew G. Knepley /* now loop over all entities in the closure of that cell */ 7819566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, ownedpoint, PETSC_TRUE, &closureSize, &closure)); 7825f824522SMatthew G. Knepley for (ci = 0; ci < closureSize*2; ci += 2) { 7834c954380SMatthew G. Knepley const PetscInt seenpoint = closure[ci]; 7845f824522SMatthew G. Knepley if (ignoredim >= 0 && seenpoint >= iStart && seenpoint < iEnd) continue; 7859566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(cht, seenpoint)); 786bc7fa33aSFlorian Wechsung /* Facet integrals couple dofs across facets, so in that case for each of 787bc7fa33aSFlorian Wechsung * the facets we need to add all dofs on the other side of the facet to 788bc7fa33aSFlorian Wechsung * the seen dofs. */ 789bc7fa33aSFlorian Wechsung if (patch->usercomputeopintfacet) { 790bc7fa33aSFlorian Wechsung if (fBegin <= seenpoint && seenpoint < fEnd) { 7919566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, seenpoint, PETSC_FALSE, &fStarSize, &fStar)); 792bc7fa33aSFlorian Wechsung for (fsi = 0; fsi < fStarSize*2; fsi += 2) { 7939566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, &fClosureSize, &fClosure)); 794bc7fa33aSFlorian Wechsung for (fci = 0; fci < fClosureSize*2; fci += 2) { 7959566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(cht, fClosure[fci])); 796bc7fa33aSFlorian Wechsung } 7979566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, NULL, &fClosure)); 798bc7fa33aSFlorian Wechsung } 7999566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, seenpoint, PETSC_FALSE, NULL, &fStar)); 800bc7fa33aSFlorian Wechsung } 801bc7fa33aSFlorian Wechsung } 8024bbf5ea8SMatthew G. Knepley } 8039566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, ownedpoint, PETSC_TRUE, NULL, &closure)); 8044bbf5ea8SMatthew G. Knepley } 8059566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, NULL, &star)); 8064bbf5ea8SMatthew G. Knepley } 8079566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm)); 8085f824522SMatthew G. Knepley PetscFunctionReturn(0); 8095f824522SMatthew G. Knepley } 8105f824522SMatthew G. Knepley 8115f824522SMatthew G. Knepley static PetscErrorCode PCPatchGetGlobalDofs(PC pc, PetscSection dofSection[], PetscInt f, PetscBool combined, PetscInt p, PetscInt *dof, PetscInt *off) 8125f824522SMatthew G. Knepley { 8135f824522SMatthew G. Knepley PetscFunctionBegin; 8145f824522SMatthew G. Knepley if (combined) { 8155f824522SMatthew G. Knepley if (f < 0) { 8169566063dSJacob Faibussowitsch if (dof) PetscCall(PetscSectionGetDof(dofSection[0], p, dof)); 8179566063dSJacob Faibussowitsch if (off) PetscCall(PetscSectionGetOffset(dofSection[0], p, off)); 8185f824522SMatthew G. Knepley } else { 8199566063dSJacob Faibussowitsch if (dof) PetscCall(PetscSectionGetFieldDof(dofSection[0], p, f, dof)); 8209566063dSJacob Faibussowitsch if (off) PetscCall(PetscSectionGetFieldOffset(dofSection[0], p, f, off)); 8215f824522SMatthew G. Knepley } 8225f824522SMatthew G. Knepley } else { 8235f824522SMatthew G. Knepley if (f < 0) { 8245f824522SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 8255f824522SMatthew G. Knepley PetscInt fdof, g; 8265f824522SMatthew G. Knepley 8275f824522SMatthew G. Knepley if (dof) { 8285f824522SMatthew G. Knepley *dof = 0; 8295f824522SMatthew G. Knepley for (g = 0; g < patch->nsubspaces; ++g) { 8309566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(dofSection[g], p, &fdof)); 8315f824522SMatthew G. Knepley *dof += fdof; 8325f824522SMatthew G. Knepley } 8335f824522SMatthew G. Knepley } 834624e31c3SLawrence Mitchell if (off) { 835624e31c3SLawrence Mitchell *off = 0; 836624e31c3SLawrence Mitchell for (g = 0; g < patch->nsubspaces; ++g) { 8379566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(dofSection[g], p, &fdof)); 838624e31c3SLawrence Mitchell *off += fdof; 839624e31c3SLawrence Mitchell } 840624e31c3SLawrence Mitchell } 8415f824522SMatthew G. Knepley } else { 8429566063dSJacob Faibussowitsch if (dof) PetscCall(PetscSectionGetDof(dofSection[f], p, dof)); 8439566063dSJacob Faibussowitsch if (off) PetscCall(PetscSectionGetOffset(dofSection[f], p, off)); 8445f824522SMatthew G. Knepley } 8455f824522SMatthew G. Knepley } 8464bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 8474bbf5ea8SMatthew G. Knepley } 8484bbf5ea8SMatthew G. Knepley 8494bbf5ea8SMatthew G. Knepley /* Given a hash table with a set of topological entities (pts), compute the degrees of 8504bbf5ea8SMatthew G. Knepley freedom in global concatenated numbering on those entities. 8514bbf5ea8SMatthew G. Knepley For Vanka smoothing, this needs to do something special: ignore dofs of the 8524bbf5ea8SMatthew G. Knepley constraint subspace on entities that aren't the base entity we're building the patch 8534bbf5ea8SMatthew G. Knepley around. */ 854e4c66b91SPatrick Farrell static PetscErrorCode PCPatchGetPointDofs(PC pc, PetscHSetI pts, PetscHSetI dofs, PetscInt base, PetscHSetI* subspaces_to_exclude) 8554bbf5ea8SMatthew G. Knepley { 8565f824522SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 8571b68eb51SMatthew G. Knepley PetscHashIter hi; 8584bbf5ea8SMatthew G. Knepley PetscInt ldof, loff; 8594bbf5ea8SMatthew G. Knepley PetscInt k, p; 8604bbf5ea8SMatthew G. Knepley 8614bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 8629566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(dofs)); 8634bbf5ea8SMatthew G. Knepley for (k = 0; k < patch->nsubspaces; ++k) { 8644bbf5ea8SMatthew G. Knepley PetscInt subspaceOffset = patch->subspaceOffsets[k]; 8654bbf5ea8SMatthew G. Knepley PetscInt bs = patch->bs[k]; 8664bbf5ea8SMatthew G. Knepley PetscInt j, l; 8674bbf5ea8SMatthew G. Knepley 868e4c66b91SPatrick Farrell if (subspaces_to_exclude != NULL) { 869e4c66b91SPatrick Farrell PetscBool should_exclude_k = PETSC_FALSE; 8709566063dSJacob Faibussowitsch PetscCall(PetscHSetIHas(*subspaces_to_exclude, k, &should_exclude_k)); 871e4c66b91SPatrick Farrell if (should_exclude_k) { 8724bbf5ea8SMatthew G. Knepley /* only get this subspace dofs at the base entity, not any others */ 8739566063dSJacob Faibussowitsch PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, base, &ldof, &loff)); 8744bbf5ea8SMatthew G. Knepley if (0 == ldof) continue; 8754bbf5ea8SMatthew G. Knepley for (j = loff; j < ldof + loff; ++j) { 8764bbf5ea8SMatthew G. Knepley for (l = 0; l < bs; ++l) { 8774bbf5ea8SMatthew G. Knepley PetscInt dof = bs*j + l + subspaceOffset; 8789566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(dofs, dof)); 8794bbf5ea8SMatthew G. Knepley } 8804bbf5ea8SMatthew G. Knepley } 8814bbf5ea8SMatthew G. Knepley continue; /* skip the other dofs of this subspace */ 8824bbf5ea8SMatthew G. Knepley } 883e4c66b91SPatrick Farrell } 8844bbf5ea8SMatthew G. Knepley 8851b68eb51SMatthew G. Knepley PetscHashIterBegin(pts, hi); 8861b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(pts, hi)) { 8871b68eb51SMatthew G. Knepley PetscHashIterGetKey(pts, hi, p); 8881b68eb51SMatthew G. Knepley PetscHashIterNext(pts, hi); 8899566063dSJacob Faibussowitsch PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, p, &ldof, &loff)); 8904bbf5ea8SMatthew G. Knepley if (0 == ldof) continue; 8914bbf5ea8SMatthew G. Knepley for (j = loff; j < ldof + loff; ++j) { 8924bbf5ea8SMatthew G. Knepley for (l = 0; l < bs; ++l) { 8934bbf5ea8SMatthew G. Knepley PetscInt dof = bs*j + l + subspaceOffset; 8949566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(dofs, dof)); 8954bbf5ea8SMatthew G. Knepley } 8964bbf5ea8SMatthew G. Knepley } 8974bbf5ea8SMatthew G. Knepley } 8984bbf5ea8SMatthew G. Knepley } 8994bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 9004bbf5ea8SMatthew G. Knepley } 9014bbf5ea8SMatthew G. Knepley 9024bbf5ea8SMatthew G. Knepley /* Given two hash tables A and B, compute the keys in B that are not in A, and put them in C */ 9031b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchComputeSetDifference_Private(PetscHSetI A, PetscHSetI B, PetscHSetI C) 9044bbf5ea8SMatthew G. Knepley { 9051b68eb51SMatthew G. Knepley PetscHashIter hi; 9061b68eb51SMatthew G. Knepley PetscInt key; 9074bbf5ea8SMatthew G. Knepley PetscBool flg; 9084bbf5ea8SMatthew G. Knepley 9094bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 9109566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(C)); 9111b68eb51SMatthew G. Knepley PetscHashIterBegin(B, hi); 9121b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(B, hi)) { 9131b68eb51SMatthew G. Knepley PetscHashIterGetKey(B, hi, key); 9141b68eb51SMatthew G. Knepley PetscHashIterNext(B, hi); 9159566063dSJacob Faibussowitsch PetscCall(PetscHSetIHas(A, key, &flg)); 9169566063dSJacob Faibussowitsch if (!flg) PetscCall(PetscHSetIAdd(C, key)); 9174bbf5ea8SMatthew G. Knepley } 9184bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 9194bbf5ea8SMatthew G. Knepley } 9204bbf5ea8SMatthew G. Knepley 9214bbf5ea8SMatthew G. Knepley /* 9224bbf5ea8SMatthew G. Knepley * PCPatchCreateCellPatches - create patches. 9234bbf5ea8SMatthew G. Knepley * 9244bbf5ea8SMatthew G. Knepley * Input Parameters: 9254bbf5ea8SMatthew G. Knepley * + dm - The DMPlex object defining the mesh 9264bbf5ea8SMatthew G. Knepley * 9274bbf5ea8SMatthew G. Knepley * Output Parameters: 9284bbf5ea8SMatthew G. Knepley * + cellCounts - Section with counts of cells around each vertex 9295f824522SMatthew G. Knepley * . cells - IS of the cell point indices of cells in each patch 9305f824522SMatthew G. Knepley * . pointCounts - Section with counts of cells around each vertex 9315f824522SMatthew G. Knepley * - point - IS of the cell point indices of cells in each patch 9324bbf5ea8SMatthew G. Knepley */ 9334bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatches(PC pc) 9344bbf5ea8SMatthew G. Knepley { 9354bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 9365f824522SMatthew G. Knepley DMLabel ghost = NULL; 9374bbf5ea8SMatthew G. Knepley DM dm, plex; 93876ce8f1aSJose E. Roman PetscHSetI ht=NULL, cht=NULL; 9390e126c0bSLawrence Mitchell PetscSection cellCounts, pointCounts, intFacetCounts, extFacetCounts; 940eb62eeaaSLawrence Mitchell PetscInt *cellsArray, *pointsArray, *intFacetsArray, *extFacetsArray, *intFacetsToPatchCell; 9410e126c0bSLawrence Mitchell PetscInt numCells, numPoints, numIntFacets, numExtFacets; 9425f824522SMatthew G. Knepley const PetscInt *leaves; 94333cbca70SPatrick Farrell PetscInt nleaves, pStart, pEnd, cStart, cEnd, vStart, vEnd, fStart, fEnd, v; 9445f824522SMatthew G. Knepley PetscBool isFiredrake; 9454bbf5ea8SMatthew G. Knepley 9464bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 9474bbf5ea8SMatthew G. Knepley /* Used to keep track of the cells in the patch. */ 9489566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&ht)); 9499566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&cht)); 9504bbf5ea8SMatthew G. Knepley 9519566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm)); 95228b400f6SJacob Faibussowitsch PetscCheck(dm,PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONGSTATE, "DM not yet set on patch PC"); 9539566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex)); 954b6bb21d1SLawrence Mitchell dm = plex; 9559566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 9569566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 9574bbf5ea8SMatthew G. Knepley 9584bbf5ea8SMatthew G. Knepley if (patch->user_patches) { 9599566063dSJacob Faibussowitsch PetscCall(patch->userpatchconstructionop(pc, &patch->npatch, &patch->userIS, &patch->iterationSet, patch->userpatchconstructctx)); 9605f824522SMatthew G. Knepley vStart = 0; vEnd = patch->npatch; 961e5b9877fSPatrick Farrell } else if (patch->ctype == PC_PATCH_PARDECOMP) { 9620a390943SPatrick Farrell vStart = 0; vEnd = 1; 9635f824522SMatthew G. Knepley } else if (patch->codim < 0) { 9649566063dSJacob Faibussowitsch if (patch->dim < 0) PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd)); 9659566063dSJacob Faibussowitsch else PetscCall(DMPlexGetDepthStratum(dm, patch->dim, &vStart, &vEnd)); 9669566063dSJacob Faibussowitsch } else PetscCall(DMPlexGetHeightStratum(dm, patch->codim, &vStart, &vEnd)); 9675f824522SMatthew G. Knepley patch->npatch = vEnd - vStart; 9684bbf5ea8SMatthew G. Knepley 9694bbf5ea8SMatthew G. Knepley /* These labels mark the owned points. We only create patches around points that this process owns. */ 9709566063dSJacob Faibussowitsch PetscCall(DMHasLabel(dm, "pyop2_ghost", &isFiredrake)); 9715f824522SMatthew G. Knepley if (isFiredrake) { 9729566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, "pyop2_ghost", &ghost)); 9739566063dSJacob Faibussowitsch PetscCall(DMLabelCreateIndex(ghost, pStart, pEnd)); 9745f824522SMatthew G. Knepley } else { 9755f824522SMatthew G. Knepley PetscSF sf; 9765f824522SMatthew G. Knepley 9779566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm, &sf)); 9789566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL)); 9795f824522SMatthew G. Knepley nleaves = PetscMax(nleaves, 0); 9805f824522SMatthew G. Knepley } 9814bbf5ea8SMatthew G. Knepley 9829566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->cellCounts)); 9839566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) patch->cellCounts, "Patch Cell Layout")); 9844bbf5ea8SMatthew G. Knepley cellCounts = patch->cellCounts; 9859566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(cellCounts, vStart, vEnd)); 9869566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->pointCounts)); 9879566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) patch->pointCounts, "Patch Point Layout")); 9885f824522SMatthew G. Knepley pointCounts = patch->pointCounts; 9899566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(pointCounts, vStart, vEnd)); 9909566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->extFacetCounts)); 9919566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) patch->extFacetCounts, "Patch Exterior Facet Layout")); 9920e126c0bSLawrence Mitchell extFacetCounts = patch->extFacetCounts; 9939566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(extFacetCounts, vStart, vEnd)); 9949566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->intFacetCounts)); 9959566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) patch->intFacetCounts, "Patch Interior Facet Layout")); 9960e126c0bSLawrence Mitchell intFacetCounts = patch->intFacetCounts; 9979566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(intFacetCounts, vStart, vEnd)); 9985f824522SMatthew G. Knepley /* Count cells and points in the patch surrounding each entity */ 9999566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd)); 10004bbf5ea8SMatthew G. Knepley for (v = vStart; v < vEnd; ++v) { 10011b68eb51SMatthew G. Knepley PetscHashIter hi; 10025f824522SMatthew G. Knepley PetscInt chtSize, loc = -1; 10035f824522SMatthew G. Knepley PetscBool flg; 10044bbf5ea8SMatthew G. Knepley 1005b525f888SPatrick Farrell if (!patch->user_patches && patch->ctype != PC_PATCH_PARDECOMP) { 10069566063dSJacob Faibussowitsch if (ghost) PetscCall(DMLabelHasPoint(ghost, v, &flg)); 10079566063dSJacob Faibussowitsch else {PetscCall(PetscFindInt(v, nleaves, leaves, &loc)); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;} 10084bbf5ea8SMatthew G. Knepley /* Not an owned entity, don't make a cell patch. */ 10094bbf5ea8SMatthew G. Knepley if (flg) continue; 10104bbf5ea8SMatthew G. Knepley } 10114bbf5ea8SMatthew G. Knepley 10129566063dSJacob Faibussowitsch PetscCall(patch->patchconstructop((void *) patch, dm, v, ht)); 10139566063dSJacob Faibussowitsch PetscCall(PCPatchCompleteCellPatch(pc, ht, cht)); 10149566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(cht, &chtSize)); 10154bbf5ea8SMatthew G. Knepley /* empty patch, continue */ 10164bbf5ea8SMatthew G. Knepley if (chtSize == 0) continue; 10174bbf5ea8SMatthew G. Knepley 10184bbf5ea8SMatthew G. Knepley /* safe because size(cht) > 0 from above */ 10191b68eb51SMatthew G. Knepley PetscHashIterBegin(cht, hi); 10201b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(cht, hi)) { 10215f824522SMatthew G. Knepley PetscInt point, pdof; 10224bbf5ea8SMatthew G. Knepley 10231b68eb51SMatthew G. Knepley PetscHashIterGetKey(cht, hi, point); 10240e126c0bSLawrence Mitchell if (fStart <= point && point < fEnd) { 10250e126c0bSLawrence Mitchell const PetscInt *support; 10260e126c0bSLawrence Mitchell PetscInt supportSize, p; 10270e126c0bSLawrence Mitchell PetscBool interior = PETSC_TRUE; 10289566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, point, &support)); 10299566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, point, &supportSize)); 10300e126c0bSLawrence Mitchell if (supportSize == 1) { 10310e126c0bSLawrence Mitchell interior = PETSC_FALSE; 10320e126c0bSLawrence Mitchell } else { 10330e126c0bSLawrence Mitchell for (p = 0; p < supportSize; p++) { 10340e126c0bSLawrence Mitchell PetscBool found; 10350e126c0bSLawrence Mitchell /* FIXME: can I do this while iterating over cht? */ 10369566063dSJacob Faibussowitsch PetscCall(PetscHSetIHas(cht, support[p], &found)); 10370e126c0bSLawrence Mitchell if (!found) { 10380e126c0bSLawrence Mitchell interior = PETSC_FALSE; 10390e126c0bSLawrence Mitchell break; 10400e126c0bSLawrence Mitchell } 10410e126c0bSLawrence Mitchell } 10420e126c0bSLawrence Mitchell } 10430e126c0bSLawrence Mitchell if (interior) { 10449566063dSJacob Faibussowitsch PetscCall(PetscSectionAddDof(intFacetCounts, v, 1)); 10450e126c0bSLawrence Mitchell } else { 10469566063dSJacob Faibussowitsch PetscCall(PetscSectionAddDof(extFacetCounts, v, 1)); 10470e126c0bSLawrence Mitchell } 10480e126c0bSLawrence Mitchell } 10499566063dSJacob Faibussowitsch PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL)); 10509566063dSJacob Faibussowitsch if (pdof) PetscCall(PetscSectionAddDof(pointCounts, v, 1)); 10519566063dSJacob Faibussowitsch if (point >= cStart && point < cEnd) PetscCall(PetscSectionAddDof(cellCounts, v, 1)); 10521b68eb51SMatthew G. Knepley PetscHashIterNext(cht, hi); 10534bbf5ea8SMatthew G. Knepley } 10544bbf5ea8SMatthew G. Knepley } 10559566063dSJacob Faibussowitsch if (isFiredrake) PetscCall(DMLabelDestroyIndex(ghost)); 10564bbf5ea8SMatthew G. Knepley 10579566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(cellCounts)); 10589566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(cellCounts, &numCells)); 10599566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numCells, &cellsArray)); 10609566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(pointCounts)); 10619566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(pointCounts, &numPoints)); 10629566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPoints, &pointsArray)); 10634bbf5ea8SMatthew G. Knepley 10649566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(intFacetCounts)); 10659566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(extFacetCounts)); 10669566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(intFacetCounts, &numIntFacets)); 10679566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(extFacetCounts, &numExtFacets)); 10689566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numIntFacets, &intFacetsArray)); 10699566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numIntFacets*2, &intFacetsToPatchCell)); 10709566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numExtFacets, &extFacetsArray)); 10710e126c0bSLawrence Mitchell 10724bbf5ea8SMatthew G. Knepley /* Now that we know how much space we need, run through again and actually remember the cells. */ 10734bbf5ea8SMatthew G. Knepley for (v = vStart; v < vEnd; v++) { 10741b68eb51SMatthew G. Knepley PetscHashIter hi; 10750e126c0bSLawrence Mitchell PetscInt dof, off, cdof, coff, efdof, efoff, ifdof, ifoff, pdof, n = 0, cn = 0, ifn = 0, efn = 0; 10764bbf5ea8SMatthew G. Knepley 10779566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(pointCounts, v, &dof)); 10789566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(pointCounts, v, &off)); 10799566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(cellCounts, v, &cdof)); 10809566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(cellCounts, v, &coff)); 10819566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(intFacetCounts, v, &ifdof)); 10829566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(intFacetCounts, v, &ifoff)); 10839566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(extFacetCounts, v, &efdof)); 10849566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(extFacetCounts, v, &efoff)); 10855f824522SMatthew G. Knepley if (dof <= 0) continue; 10869566063dSJacob Faibussowitsch PetscCall(patch->patchconstructop((void *) patch, dm, v, ht)); 10879566063dSJacob Faibussowitsch PetscCall(PCPatchCompleteCellPatch(pc, ht, cht)); 10881b68eb51SMatthew G. Knepley PetscHashIterBegin(cht, hi); 10891b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(cht, hi)) { 10904bbf5ea8SMatthew G. Knepley PetscInt point; 10914bbf5ea8SMatthew G. Knepley 10921b68eb51SMatthew G. Knepley PetscHashIterGetKey(cht, hi, point); 10930e126c0bSLawrence Mitchell if (fStart <= point && point < fEnd) { 10940e126c0bSLawrence Mitchell const PetscInt *support; 10950e126c0bSLawrence Mitchell PetscInt supportSize, p; 10960e126c0bSLawrence Mitchell PetscBool interior = PETSC_TRUE; 10979566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, point, &support)); 10989566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, point, &supportSize)); 10990e126c0bSLawrence Mitchell if (supportSize == 1) { 11000e126c0bSLawrence Mitchell interior = PETSC_FALSE; 11010e126c0bSLawrence Mitchell } else { 11020e126c0bSLawrence Mitchell for (p = 0; p < supportSize; p++) { 11030e126c0bSLawrence Mitchell PetscBool found; 11040e126c0bSLawrence Mitchell /* FIXME: can I do this while iterating over cht? */ 11059566063dSJacob Faibussowitsch PetscCall(PetscHSetIHas(cht, support[p], &found)); 11060e126c0bSLawrence Mitchell if (!found) { 11070e126c0bSLawrence Mitchell interior = PETSC_FALSE; 11080e126c0bSLawrence Mitchell break; 11090e126c0bSLawrence Mitchell } 11100e126c0bSLawrence Mitchell } 11110e126c0bSLawrence Mitchell } 11120e126c0bSLawrence Mitchell if (interior) { 111344b625f7SLawrence Mitchell intFacetsToPatchCell[2*(ifoff + ifn)] = support[0]; 111444b625f7SLawrence Mitchell intFacetsToPatchCell[2*(ifoff + ifn) + 1] = support[1]; 11150e126c0bSLawrence Mitchell intFacetsArray[ifoff + ifn++] = point; 11160e126c0bSLawrence Mitchell } else { 11170e126c0bSLawrence Mitchell extFacetsArray[efoff + efn++] = point; 11180e126c0bSLawrence Mitchell } 11190e126c0bSLawrence Mitchell } 11209566063dSJacob Faibussowitsch PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL)); 11215f824522SMatthew G. Knepley if (pdof) {pointsArray[off + n++] = point;} 11225f824522SMatthew G. Knepley if (point >= cStart && point < cEnd) {cellsArray[coff + cn++] = point;} 11231b68eb51SMatthew G. Knepley PetscHashIterNext(cht, hi); 11244bbf5ea8SMatthew G. Knepley } 112563a3b9bcSJacob 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); 112663a3b9bcSJacob 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); 112763a3b9bcSJacob 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); 112863a3b9bcSJacob 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); 1129eb62eeaaSLawrence Mitchell 1130eb62eeaaSLawrence Mitchell for (ifn = 0; ifn < ifdof; ifn++) { 113144b625f7SLawrence Mitchell PetscInt cell0 = intFacetsToPatchCell[2*(ifoff + ifn)]; 113244b625f7SLawrence Mitchell PetscInt cell1 = intFacetsToPatchCell[2*(ifoff + ifn) + 1]; 1133eb62eeaaSLawrence Mitchell PetscBool found0 = PETSC_FALSE, found1 = PETSC_FALSE; 1134eb62eeaaSLawrence Mitchell for (n = 0; n < cdof; n++) { 11357c54fef0SLawrence Mitchell if (!found0 && cell0 == cellsArray[coff + n]) { 1136c3faab33SLawrence Mitchell intFacetsToPatchCell[2*(ifoff + ifn)] = n; 1137eb62eeaaSLawrence Mitchell found0 = PETSC_TRUE; 1138eb62eeaaSLawrence Mitchell } 11397c54fef0SLawrence Mitchell if (!found1 && cell1 == cellsArray[coff + n]) { 1140c3faab33SLawrence Mitchell intFacetsToPatchCell[2*(ifoff + ifn) + 1] = n; 114180fc4459SLawrence Mitchell found1 = PETSC_TRUE; 1142eb62eeaaSLawrence Mitchell } 1143eb62eeaaSLawrence Mitchell if (found0 && found1) break; 1144eb62eeaaSLawrence Mitchell } 11457827d75bSBarry Smith PetscCheck(found0 && found1,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Didn't manage to find local point numbers for facet support"); 1146eb62eeaaSLawrence Mitchell } 11474bbf5ea8SMatthew G. Knepley } 11489566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&ht)); 11499566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&cht)); 11505f824522SMatthew G. Knepley 11519566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numCells, cellsArray, PETSC_OWN_POINTER, &patch->cells)); 11529566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) patch->cells, "Patch Cells")); 11535f824522SMatthew G. Knepley if (patch->viewCells) { 11549566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject) patch->cellCounts, patch->viewerCells, patch->formatCells)); 11559566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject) patch->cells, patch->viewerCells, patch->formatCells)); 11565f824522SMatthew G. Knepley } 11579566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray, PETSC_OWN_POINTER, &patch->intFacets)); 11589566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) patch->intFacets, "Patch Interior Facets")); 11599566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 2*numIntFacets, intFacetsToPatchCell, PETSC_OWN_POINTER, &patch->intFacetsToPatchCell)); 11609566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) patch->intFacetsToPatchCell, "Patch Interior Facets local support")); 11610e126c0bSLawrence Mitchell if (patch->viewIntFacets) { 11629566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject) patch->intFacetCounts, patch->viewerIntFacets, patch->formatIntFacets)); 11639566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject) patch->intFacets, patch->viewerIntFacets, patch->formatIntFacets)); 11649566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject) patch->intFacetsToPatchCell, patch->viewerIntFacets, patch->formatIntFacets)); 11650e126c0bSLawrence Mitchell } 11669566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numExtFacets, extFacetsArray, PETSC_OWN_POINTER, &patch->extFacets)); 11679566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) patch->extFacets, "Patch Exterior Facets")); 11680e126c0bSLawrence Mitchell if (patch->viewExtFacets) { 11699566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject) patch->extFacetCounts, patch->viewerExtFacets, patch->formatExtFacets)); 11709566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject) patch->extFacets, patch->viewerExtFacets, patch->formatExtFacets)); 11710e126c0bSLawrence Mitchell } 11729566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints, pointsArray, PETSC_OWN_POINTER, &patch->points)); 11739566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) patch->points, "Patch Points")); 11745f824522SMatthew G. Knepley if (patch->viewPoints) { 11759566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject) patch->pointCounts, patch->viewerPoints, patch->formatPoints)); 11769566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject) patch->points, patch->viewerPoints, patch->formatPoints)); 11775f824522SMatthew G. Knepley } 11789566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm)); 11794bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 11804bbf5ea8SMatthew G. Knepley } 11814bbf5ea8SMatthew G. Knepley 11824bbf5ea8SMatthew G. Knepley /* 11834bbf5ea8SMatthew G. Knepley * PCPatchCreateCellPatchDiscretisationInfo - Build the dof maps for cell patches 11844bbf5ea8SMatthew G. Knepley * 11854bbf5ea8SMatthew G. Knepley * Input Parameters: 11864bbf5ea8SMatthew G. Knepley * + dm - The DMPlex object defining the mesh 11874bbf5ea8SMatthew G. Knepley * . cellCounts - Section with counts of cells around each vertex 11884bbf5ea8SMatthew G. Knepley * . cells - IS of the cell point indices of cells in each patch 11894bbf5ea8SMatthew G. Knepley * . cellNumbering - Section mapping plex cell points to Firedrake cell indices. 11904bbf5ea8SMatthew G. Knepley * . nodesPerCell - number of nodes per cell. 11914bbf5ea8SMatthew G. Knepley * - cellNodeMap - map from cells to node indices (nodesPerCell * numCells) 11924bbf5ea8SMatthew G. Knepley * 11934bbf5ea8SMatthew G. Knepley * Output Parameters: 11945f824522SMatthew G. Knepley * + dofs - IS of local dof numbers of each cell in the patch, where local is a patch local numbering 11954bbf5ea8SMatthew G. Knepley * . gtolCounts - Section with counts of dofs per cell patch 11964bbf5ea8SMatthew G. Knepley * - gtol - IS mapping from global dofs to local dofs for each patch. 11974bbf5ea8SMatthew G. Knepley */ 11984bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatchDiscretisationInfo(PC pc) 11994bbf5ea8SMatthew G. Knepley { 12004bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 12014bbf5ea8SMatthew G. Knepley PetscSection cellCounts = patch->cellCounts; 12025f824522SMatthew G. Knepley PetscSection pointCounts = patch->pointCounts; 12030904074fSPatrick Farrell PetscSection gtolCounts, gtolCountsWithArtificial = NULL, gtolCountsWithAll = NULL; 12044bbf5ea8SMatthew G. Knepley IS cells = patch->cells; 12055f824522SMatthew G. Knepley IS points = patch->points; 12064bbf5ea8SMatthew G. Knepley PetscSection cellNumbering = patch->cellNumbering; 12075f824522SMatthew G. Knepley PetscInt Nf = patch->nsubspaces; 12085f824522SMatthew G. Knepley PetscInt numCells, numPoints; 12094bbf5ea8SMatthew G. Knepley PetscInt numDofs; 12100904074fSPatrick Farrell PetscInt numGlobalDofs, numGlobalDofsWithArtificial, numGlobalDofsWithAll; 12114bbf5ea8SMatthew G. Knepley PetscInt totalDofsPerCell = patch->totalDofsPerCell; 12124bbf5ea8SMatthew G. Knepley PetscInt vStart, vEnd, v; 12135f824522SMatthew G. Knepley const PetscInt *cellsArray, *pointsArray; 12144bbf5ea8SMatthew G. Knepley PetscInt *newCellsArray = NULL; 12154bbf5ea8SMatthew G. Knepley PetscInt *dofsArray = NULL; 1216c2e6f3c0SFlorian Wechsung PetscInt *dofsArrayWithArtificial = NULL; 12170904074fSPatrick Farrell PetscInt *dofsArrayWithAll = NULL; 12185f824522SMatthew G. Knepley PetscInt *offsArray = NULL; 1219c2e6f3c0SFlorian Wechsung PetscInt *offsArrayWithArtificial = NULL; 12200904074fSPatrick Farrell PetscInt *offsArrayWithAll = NULL; 12214bbf5ea8SMatthew G. Knepley PetscInt *asmArray = NULL; 1222c2e6f3c0SFlorian Wechsung PetscInt *asmArrayWithArtificial = NULL; 12230904074fSPatrick Farrell PetscInt *asmArrayWithAll = NULL; 12244bbf5ea8SMatthew G. Knepley PetscInt *globalDofsArray = NULL; 1225c2e6f3c0SFlorian Wechsung PetscInt *globalDofsArrayWithArtificial = NULL; 12260904074fSPatrick Farrell PetscInt *globalDofsArrayWithAll = NULL; 12274bbf5ea8SMatthew G. Knepley PetscInt globalIndex = 0; 12284bbf5ea8SMatthew G. Knepley PetscInt key = 0; 12294bbf5ea8SMatthew G. Knepley PetscInt asmKey = 0; 1230b6bb21d1SLawrence Mitchell DM dm = NULL, plex; 1231557beb66SLawrence Mitchell const PetscInt *bcNodes = NULL; 12321b68eb51SMatthew G. Knepley PetscHMapI ht; 1233c2e6f3c0SFlorian Wechsung PetscHMapI htWithArtificial; 12340904074fSPatrick Farrell PetscHMapI htWithAll; 12351b68eb51SMatthew G. Knepley PetscHSetI globalBcs; 1236557beb66SLawrence Mitchell PetscInt numBcs; 12371b68eb51SMatthew G. Knepley PetscHSetI ownedpts, seenpts, owneddofs, seendofs, artificialbcs; 1238cda239d9SMatthew G. Knepley PetscInt pStart, pEnd, p, i; 123910534d48SPatrick Farrell char option[PETSC_MAX_PATH_LEN]; 124039fd2e8aSPatrick Farrell PetscBool isNonlinear; 12414bbf5ea8SMatthew G. Knepley 12424bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 1243557beb66SLawrence Mitchell 12449566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm)); 12459566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex)); 1246b6bb21d1SLawrence Mitchell dm = plex; 12474bbf5ea8SMatthew G. Knepley /* dofcounts section is cellcounts section * dofPerCell */ 12489566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(cellCounts, &numCells)); 12499566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(patch->pointCounts, &numPoints)); 12504bbf5ea8SMatthew G. Knepley numDofs = numCells * totalDofsPerCell; 12519566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numDofs, &dofsArray)); 12529566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPoints*Nf, &offsArray)); 12539566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numDofs, &asmArray)); 12549566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numCells, &newCellsArray)); 12559566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(cellCounts, &vStart, &vEnd)); 12569566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCounts)); 12574bbf5ea8SMatthew G. Knepley gtolCounts = patch->gtolCounts; 12589566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(gtolCounts, vStart, vEnd)); 12599566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) patch->gtolCounts, "Patch Global Index Section")); 12604bbf5ea8SMatthew G. Knepley 1261b6bb21d1SLawrence Mitchell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 12629566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPoints*Nf, &offsArrayWithArtificial)); 12639566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numDofs, &asmArrayWithArtificial)); 12649566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numDofs, &dofsArrayWithArtificial)); 12659566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithArtificial)); 1266c2e6f3c0SFlorian Wechsung gtolCountsWithArtificial = patch->gtolCountsWithArtificial; 12679566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(gtolCountsWithArtificial, vStart, vEnd)); 12689566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) patch->gtolCountsWithArtificial, "Patch Global Index Section Including Artificial BCs")); 1269c2e6f3c0SFlorian Wechsung } 1270c2e6f3c0SFlorian Wechsung 12710904074fSPatrick Farrell isNonlinear = patch->isNonlinear; 1272b6bb21d1SLawrence Mitchell if (isNonlinear) { 12739566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPoints*Nf, &offsArrayWithAll)); 12749566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numDofs, &asmArrayWithAll)); 12759566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numDofs, &dofsArrayWithAll)); 12769566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithAll)); 12770904074fSPatrick Farrell gtolCountsWithAll = patch->gtolCountsWithAll; 12789566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(gtolCountsWithAll, vStart, vEnd)); 12799566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) patch->gtolCountsWithAll, "Patch Global Index Section Including All BCs")); 12800904074fSPatrick Farrell } 12810904074fSPatrick Farrell 1282557beb66SLawrence Mitchell /* Outside the patch loop, get the dofs that are globally-enforced Dirichlet 1283557beb66SLawrence Mitchell conditions */ 12849566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&globalBcs)); 12859566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->ghostBcNodes, &bcNodes)); 12869566063dSJacob Faibussowitsch PetscCall(ISGetSize(patch->ghostBcNodes, &numBcs)); 1287cda239d9SMatthew G. Knepley for (i = 0; i < numBcs; ++i) { 12889566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(globalBcs, bcNodes[i])); /* these are already in concatenated numbering */ 1289557beb66SLawrence Mitchell } 12909566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->ghostBcNodes, &bcNodes)); 12919566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->ghostBcNodes)); /* memory optimisation */ 1292557beb66SLawrence Mitchell 1293557beb66SLawrence Mitchell /* Hash tables for artificial BC construction */ 12949566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&ownedpts)); 12959566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&seenpts)); 12969566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&owneddofs)); 12979566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&seendofs)); 12989566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&artificialbcs)); 1299557beb66SLawrence Mitchell 13009566063dSJacob Faibussowitsch PetscCall(ISGetIndices(cells, &cellsArray)); 13019566063dSJacob Faibussowitsch PetscCall(ISGetIndices(points, &pointsArray)); 13029566063dSJacob Faibussowitsch PetscCall(PetscHMapICreate(&ht)); 13039566063dSJacob Faibussowitsch PetscCall(PetscHMapICreate(&htWithArtificial)); 13049566063dSJacob Faibussowitsch PetscCall(PetscHMapICreate(&htWithAll)); 13054bbf5ea8SMatthew G. Knepley for (v = vStart; v < vEnd; ++v) { 13064bbf5ea8SMatthew G. Knepley PetscInt localIndex = 0; 1307c2e6f3c0SFlorian Wechsung PetscInt localIndexWithArtificial = 0; 13080904074fSPatrick Farrell PetscInt localIndexWithAll = 0; 13094bbf5ea8SMatthew G. Knepley PetscInt dof, off, i, j, k, l; 13104bbf5ea8SMatthew G. Knepley 13119566063dSJacob Faibussowitsch PetscCall(PetscHMapIClear(ht)); 13129566063dSJacob Faibussowitsch PetscCall(PetscHMapIClear(htWithArtificial)); 13139566063dSJacob Faibussowitsch PetscCall(PetscHMapIClear(htWithAll)); 13149566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(cellCounts, v, &dof)); 13159566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(cellCounts, v, &off)); 13164bbf5ea8SMatthew G. Knepley if (dof <= 0) continue; 13174bbf5ea8SMatthew G. Knepley 1318557beb66SLawrence Mitchell /* Calculate the global numbers of the artificial BC dofs here first */ 13199566063dSJacob Faibussowitsch PetscCall(patch->patchconstructop((void*)patch, dm, v, ownedpts)); 13209566063dSJacob Faibussowitsch PetscCall(PCPatchCompleteCellPatch(pc, ownedpts, seenpts)); 13219566063dSJacob Faibussowitsch PetscCall(PCPatchGetPointDofs(pc, ownedpts, owneddofs, v, &patch->subspaces_to_exclude)); 13229566063dSJacob Faibussowitsch PetscCall(PCPatchGetPointDofs(pc, seenpts, seendofs, v, NULL)); 13239566063dSJacob Faibussowitsch PetscCall(PCPatchComputeSetDifference_Private(owneddofs, seendofs, artificialbcs)); 13248135ed82SLawrence Mitchell if (patch->viewPatches) { 13251b68eb51SMatthew G. Knepley PetscHSetI globalbcdofs; 13261b68eb51SMatthew G. Knepley PetscHashIter hi; 13278135ed82SLawrence Mitchell MPI_Comm comm = PetscObjectComm((PetscObject)pc); 13281b68eb51SMatthew G. Knepley 13299566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&globalbcdofs)); 133063a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": owned dofs:\n", v)); 13311b68eb51SMatthew G. Knepley PetscHashIterBegin(owneddofs, hi); 13321b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(owneddofs, hi)) { 13338135ed82SLawrence Mitchell PetscInt globalDof; 13348135ed82SLawrence Mitchell 13351b68eb51SMatthew G. Knepley PetscHashIterGetKey(owneddofs, hi, globalDof); 13361b68eb51SMatthew G. Knepley PetscHashIterNext(owneddofs, hi); 133763a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof)); 13388135ed82SLawrence Mitchell } 13399566063dSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "\n")); 134063a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": seen dofs:\n", v)); 13411b68eb51SMatthew G. Knepley PetscHashIterBegin(seendofs, hi); 13421b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(seendofs, hi)) { 13438135ed82SLawrence Mitchell PetscInt globalDof; 13448135ed82SLawrence Mitchell PetscBool flg; 13458135ed82SLawrence Mitchell 13461b68eb51SMatthew G. Knepley PetscHashIterGetKey(seendofs, hi, globalDof); 13471b68eb51SMatthew G. Knepley PetscHashIterNext(seendofs, hi); 134863a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof)); 13498135ed82SLawrence Mitchell 13509566063dSJacob Faibussowitsch PetscCall(PetscHSetIHas(globalBcs, globalDof, &flg)); 13519566063dSJacob Faibussowitsch if (flg) PetscCall(PetscHSetIAdd(globalbcdofs, globalDof)); 13528135ed82SLawrence Mitchell } 13539566063dSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "\n")); 135463a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": global BCs:\n", v)); 13559566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(globalbcdofs, &numBcs)); 13568135ed82SLawrence Mitchell if (numBcs > 0) { 13571b68eb51SMatthew G. Knepley PetscHashIterBegin(globalbcdofs, hi); 13581b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(globalbcdofs, hi)) { 13598135ed82SLawrence Mitchell PetscInt globalDof; 13601b68eb51SMatthew G. Knepley PetscHashIterGetKey(globalbcdofs, hi, globalDof); 13611b68eb51SMatthew G. Knepley PetscHashIterNext(globalbcdofs, hi); 136263a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof)); 13638135ed82SLawrence Mitchell } 13648135ed82SLawrence Mitchell } 13659566063dSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "\n")); 136663a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": artificial BCs:\n", v)); 13679566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(artificialbcs, &numBcs)); 13688135ed82SLawrence Mitchell if (numBcs > 0) { 13691b68eb51SMatthew G. Knepley PetscHashIterBegin(artificialbcs, hi); 13701b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(artificialbcs, hi)) { 13718135ed82SLawrence Mitchell PetscInt globalDof; 13721b68eb51SMatthew G. Knepley PetscHashIterGetKey(artificialbcs, hi, globalDof); 13731b68eb51SMatthew G. Knepley PetscHashIterNext(artificialbcs, hi); 137463a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof)); 13758135ed82SLawrence Mitchell } 13768135ed82SLawrence Mitchell } 13779566063dSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "\n\n")); 13789566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&globalbcdofs)); 13798135ed82SLawrence Mitchell } 13804bbf5ea8SMatthew G. Knepley for (k = 0; k < patch->nsubspaces; ++k) { 13814bbf5ea8SMatthew G. Knepley const PetscInt *cellNodeMap = patch->cellNodeMap[k]; 13824bbf5ea8SMatthew G. Knepley PetscInt nodesPerCell = patch->nodesPerCell[k]; 13834bbf5ea8SMatthew G. Knepley PetscInt subspaceOffset = patch->subspaceOffsets[k]; 13844bbf5ea8SMatthew G. Knepley PetscInt bs = patch->bs[k]; 13854bbf5ea8SMatthew G. Knepley 13864bbf5ea8SMatthew G. Knepley for (i = off; i < off + dof; ++i) { 13874bbf5ea8SMatthew G. Knepley /* Walk over the cells in this patch. */ 13884bbf5ea8SMatthew G. Knepley const PetscInt c = cellsArray[i]; 13895f824522SMatthew G. Knepley PetscInt cell = c; 13904bbf5ea8SMatthew G. Knepley 13915f824522SMatthew G. Knepley /* TODO Change this to an IS */ 13925f824522SMatthew G. Knepley if (cellNumbering) { 13939566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(cellNumbering, c, &cell)); 139463a3b9bcSJacob Faibussowitsch PetscCheck(cell > 0,PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_OUTOFRANGE, "Cell %" PetscInt_FMT " doesn't appear in cell numbering map", c); 13959566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(cellNumbering, c, &cell)); 13965f824522SMatthew G. Knepley } 13974bbf5ea8SMatthew G. Knepley newCellsArray[i] = cell; 13984bbf5ea8SMatthew G. Knepley for (j = 0; j < nodesPerCell; ++j) { 13994bbf5ea8SMatthew G. Knepley /* For each global dof, map it into contiguous local storage. */ 14004bbf5ea8SMatthew G. Knepley const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + subspaceOffset; 14014bbf5ea8SMatthew G. Knepley /* finally, loop over block size */ 14024bbf5ea8SMatthew G. Knepley for (l = 0; l < bs; ++l) { 14031b68eb51SMatthew G. Knepley PetscInt localDof; 14041b68eb51SMatthew G. Knepley PetscBool isGlobalBcDof, isArtificialBcDof; 14054bbf5ea8SMatthew G. Knepley 1406557beb66SLawrence Mitchell /* first, check if this is either a globally enforced or locally enforced BC dof */ 14079566063dSJacob Faibussowitsch PetscCall(PetscHSetIHas(globalBcs, globalDof + l, &isGlobalBcDof)); 14089566063dSJacob Faibussowitsch PetscCall(PetscHSetIHas(artificialbcs, globalDof + l, &isArtificialBcDof)); 1409557beb66SLawrence Mitchell 1410557beb66SLawrence Mitchell /* if it's either, don't ever give it a local dof number */ 14111b68eb51SMatthew G. Knepley if (isGlobalBcDof || isArtificialBcDof) { 1412c2e6f3c0SFlorian Wechsung dofsArray[globalIndex] = -1; /* don't use this in assembly in this patch */ 1413557beb66SLawrence Mitchell } else { 14149566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(ht, globalDof + l, &localDof)); 14154bbf5ea8SMatthew G. Knepley if (localDof == -1) { 14164bbf5ea8SMatthew G. Knepley localDof = localIndex++; 14179566063dSJacob Faibussowitsch PetscCall(PetscHMapISet(ht, globalDof + l, localDof)); 14184bbf5ea8SMatthew G. Knepley } 141963a3b9bcSJacob Faibussowitsch PetscCheck(globalIndex < numDofs,PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %" PetscInt_FMT " than expected %" PetscInt_FMT, globalIndex+1, numDofs); 14204bbf5ea8SMatthew G. Knepley /* And store. */ 1421c2e6f3c0SFlorian Wechsung dofsArray[globalIndex] = localDof; 14224bbf5ea8SMatthew G. Knepley } 1423c2e6f3c0SFlorian Wechsung 14240904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1425c2e6f3c0SFlorian Wechsung if (isGlobalBcDof) { 1426e047a90bSFlorian Wechsung dofsArrayWithArtificial[globalIndex] = -1; /* don't use this in assembly in this patch */ 1427c2e6f3c0SFlorian Wechsung } else { 14289566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(htWithArtificial, globalDof + l, &localDof)); 1429c2e6f3c0SFlorian Wechsung if (localDof == -1) { 1430c2e6f3c0SFlorian Wechsung localDof = localIndexWithArtificial++; 14319566063dSJacob Faibussowitsch PetscCall(PetscHMapISet(htWithArtificial, globalDof + l, localDof)); 1432c2e6f3c0SFlorian Wechsung } 143363a3b9bcSJacob Faibussowitsch PetscCheck(globalIndex < numDofs,PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %" PetscInt_FMT " than expected %" PetscInt_FMT, globalIndex+1, numDofs); 1434c2e6f3c0SFlorian Wechsung /* And store.*/ 1435c2e6f3c0SFlorian Wechsung dofsArrayWithArtificial[globalIndex] = localDof; 1436c2e6f3c0SFlorian Wechsung } 1437c2e6f3c0SFlorian Wechsung } 14380904074fSPatrick Farrell 14390904074fSPatrick Farrell if (isNonlinear) { 14400904074fSPatrick Farrell /* Build the dofmap for the function space with _all_ dofs, 14410904074fSPatrick Farrell including those in any kind of boundary condition */ 14429566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(htWithAll, globalDof + l, &localDof)); 14430904074fSPatrick Farrell if (localDof == -1) { 14440904074fSPatrick Farrell localDof = localIndexWithAll++; 14459566063dSJacob Faibussowitsch PetscCall(PetscHMapISet(htWithAll, globalDof + l, localDof)); 14460904074fSPatrick Farrell } 144763a3b9bcSJacob Faibussowitsch PetscCheck(globalIndex < numDofs,PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %" PetscInt_FMT " than expected %" PetscInt_FMT, globalIndex+1, numDofs); 14480904074fSPatrick Farrell /* And store.*/ 14490904074fSPatrick Farrell dofsArrayWithAll[globalIndex] = localDof; 14500904074fSPatrick Farrell } 1451c2e6f3c0SFlorian Wechsung globalIndex++; 14524bbf5ea8SMatthew G. Knepley } 14534bbf5ea8SMatthew G. Knepley } 14544bbf5ea8SMatthew G. Knepley } 1455557beb66SLawrence Mitchell } 14564bbf5ea8SMatthew G. Knepley /*How many local dofs in this patch? */ 14570904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 14589566063dSJacob Faibussowitsch PetscCall(PetscHMapIGetSize(htWithArtificial, &dof)); 14599566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(gtolCountsWithArtificial, v, dof)); 1460c2e6f3c0SFlorian Wechsung } 14610904074fSPatrick Farrell if (isNonlinear) { 14629566063dSJacob Faibussowitsch PetscCall(PetscHMapIGetSize(htWithAll, &dof)); 14639566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(gtolCountsWithAll, v, dof)); 14640904074fSPatrick Farrell } 14659566063dSJacob Faibussowitsch PetscCall(PetscHMapIGetSize(ht, &dof)); 14669566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(gtolCounts, v, dof)); 14674bbf5ea8SMatthew G. Knepley } 1468b6bb21d1SLawrence Mitchell 14699566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm)); 147063a3b9bcSJacob 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); 14719566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(gtolCounts)); 14729566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(gtolCounts, &numGlobalDofs)); 14739566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numGlobalDofs, &globalDofsArray)); 14744bbf5ea8SMatthew G. Knepley 14750904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 14769566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(gtolCountsWithArtificial)); 14779566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(gtolCountsWithArtificial, &numGlobalDofsWithArtificial)); 14789566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numGlobalDofsWithArtificial, &globalDofsArrayWithArtificial)); 1479c2e6f3c0SFlorian Wechsung } 14800904074fSPatrick Farrell if (isNonlinear) { 14819566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(gtolCountsWithAll)); 14829566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(gtolCountsWithAll, &numGlobalDofsWithAll)); 14839566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numGlobalDofsWithAll, &globalDofsArrayWithAll)); 14840904074fSPatrick Farrell } 14854bbf5ea8SMatthew 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. */ 14864bbf5ea8SMatthew G. Knepley for (v = vStart; v < vEnd; ++v) { 14871b68eb51SMatthew G. Knepley PetscHashIter hi; 14885f824522SMatthew G. Knepley PetscInt dof, off, Np, ooff, i, j, k, l; 14894bbf5ea8SMatthew G. Knepley 14909566063dSJacob Faibussowitsch PetscCall(PetscHMapIClear(ht)); 14919566063dSJacob Faibussowitsch PetscCall(PetscHMapIClear(htWithArtificial)); 14929566063dSJacob Faibussowitsch PetscCall(PetscHMapIClear(htWithAll)); 14939566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(cellCounts, v, &dof)); 14949566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(cellCounts, v, &off)); 14959566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(pointCounts, v, &Np)); 14969566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(pointCounts, v, &ooff)); 14974bbf5ea8SMatthew G. Knepley if (dof <= 0) continue; 14984bbf5ea8SMatthew G. Knepley 14994bbf5ea8SMatthew G. Knepley for (k = 0; k < patch->nsubspaces; ++k) { 15004bbf5ea8SMatthew G. Knepley const PetscInt *cellNodeMap = patch->cellNodeMap[k]; 15014bbf5ea8SMatthew G. Knepley PetscInt nodesPerCell = patch->nodesPerCell[k]; 15024bbf5ea8SMatthew G. Knepley PetscInt subspaceOffset = patch->subspaceOffsets[k]; 15034bbf5ea8SMatthew G. Knepley PetscInt bs = patch->bs[k]; 1504d490bb3dSLawrence Mitchell PetscInt goff; 15054bbf5ea8SMatthew G. Knepley 15064bbf5ea8SMatthew G. Knepley for (i = off; i < off + dof; ++i) { 15074bbf5ea8SMatthew G. Knepley /* Reconstruct mapping of global-to-local on this patch. */ 15084bbf5ea8SMatthew G. Knepley const PetscInt c = cellsArray[i]; 15095f824522SMatthew G. Knepley PetscInt cell = c; 15104bbf5ea8SMatthew G. Knepley 15119566063dSJacob Faibussowitsch if (cellNumbering) PetscCall(PetscSectionGetOffset(cellNumbering, c, &cell)); 15124bbf5ea8SMatthew G. Knepley for (j = 0; j < nodesPerCell; ++j) { 15134bbf5ea8SMatthew G. Knepley for (l = 0; l < bs; ++l) { 15145f824522SMatthew G. Knepley const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset; 1515c2e6f3c0SFlorian Wechsung const PetscInt localDof = dofsArray[key]; 15169566063dSJacob Faibussowitsch if (localDof >= 0) PetscCall(PetscHMapISet(ht, globalDof, localDof)); 15170904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 1518c2e6f3c0SFlorian Wechsung const PetscInt localDofWithArtificial = dofsArrayWithArtificial[key]; 1519c2e6f3c0SFlorian Wechsung if (localDofWithArtificial >= 0) { 15209566063dSJacob Faibussowitsch PetscCall(PetscHMapISet(htWithArtificial, globalDof, localDofWithArtificial)); 1521c2e6f3c0SFlorian Wechsung } 1522c2e6f3c0SFlorian Wechsung } 15230904074fSPatrick Farrell if (isNonlinear) { 15240904074fSPatrick Farrell const PetscInt localDofWithAll = dofsArrayWithAll[key]; 15250904074fSPatrick Farrell if (localDofWithAll >= 0) { 15269566063dSJacob Faibussowitsch PetscCall(PetscHMapISet(htWithAll, globalDof, localDofWithAll)); 15270904074fSPatrick Farrell } 15280904074fSPatrick Farrell } 1529c2e6f3c0SFlorian Wechsung key++; 15304bbf5ea8SMatthew G. Knepley } 15314bbf5ea8SMatthew G. Knepley } 15324bbf5ea8SMatthew G. Knepley } 1533557beb66SLawrence Mitchell 15344bbf5ea8SMatthew G. Knepley /* Shove it in the output data structure. */ 15359566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(gtolCounts, v, &goff)); 15361b68eb51SMatthew G. Knepley PetscHashIterBegin(ht, hi); 15371b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(ht, hi)) { 15384bbf5ea8SMatthew G. Knepley PetscInt globalDof, localDof; 15394bbf5ea8SMatthew G. Knepley 15401b68eb51SMatthew G. Knepley PetscHashIterGetKey(ht, hi, globalDof); 15411b68eb51SMatthew G. Knepley PetscHashIterGetVal(ht, hi, localDof); 15424bbf5ea8SMatthew G. Knepley if (globalDof >= 0) globalDofsArray[goff + localDof] = globalDof; 15431b68eb51SMatthew G. Knepley PetscHashIterNext(ht, hi); 15444bbf5ea8SMatthew G. Knepley } 15455f824522SMatthew G. Knepley 15460904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 15479566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(gtolCountsWithArtificial, v, &goff)); 1548c2e6f3c0SFlorian Wechsung PetscHashIterBegin(htWithArtificial, hi); 1549c2e6f3c0SFlorian Wechsung while (!PetscHashIterAtEnd(htWithArtificial, hi)) { 1550c2e6f3c0SFlorian Wechsung PetscInt globalDof, localDof; 1551c2e6f3c0SFlorian Wechsung PetscHashIterGetKey(htWithArtificial, hi, globalDof); 1552c2e6f3c0SFlorian Wechsung PetscHashIterGetVal(htWithArtificial, hi, localDof); 1553c2e6f3c0SFlorian Wechsung if (globalDof >= 0) globalDofsArrayWithArtificial[goff + localDof] = globalDof; 1554c2e6f3c0SFlorian Wechsung PetscHashIterNext(htWithArtificial, hi); 1555c2e6f3c0SFlorian Wechsung } 1556c2e6f3c0SFlorian Wechsung } 15570904074fSPatrick Farrell if (isNonlinear) { 15589566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(gtolCountsWithAll, v, &goff)); 15590904074fSPatrick Farrell PetscHashIterBegin(htWithAll, hi); 15600904074fSPatrick Farrell while (!PetscHashIterAtEnd(htWithAll, hi)) { 15610904074fSPatrick Farrell PetscInt globalDof, localDof; 15620904074fSPatrick Farrell PetscHashIterGetKey(htWithAll, hi, globalDof); 15630904074fSPatrick Farrell PetscHashIterGetVal(htWithAll, hi, localDof); 15640904074fSPatrick Farrell if (globalDof >= 0) globalDofsArrayWithAll[goff + localDof] = globalDof; 15650904074fSPatrick Farrell PetscHashIterNext(htWithAll, hi); 15660904074fSPatrick Farrell } 15670904074fSPatrick Farrell } 1568c2e6f3c0SFlorian Wechsung 15695f824522SMatthew G. Knepley for (p = 0; p < Np; ++p) { 15705f824522SMatthew G. Knepley const PetscInt point = pointsArray[ooff + p]; 15715f824522SMatthew G. Knepley PetscInt globalDof, localDof; 15725f824522SMatthew G. Knepley 15739566063dSJacob Faibussowitsch PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, point, NULL, &globalDof)); 15749566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(ht, globalDof, &localDof)); 15755f824522SMatthew G. Knepley offsArray[(ooff + p)*Nf + k] = localDof; 15760904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 15779566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(htWithArtificial, globalDof, &localDof)); 1578c2e6f3c0SFlorian Wechsung offsArrayWithArtificial[(ooff + p)*Nf + k] = localDof; 1579c2e6f3c0SFlorian Wechsung } 15800904074fSPatrick Farrell if (isNonlinear) { 15819566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(htWithAll, globalDof, &localDof)); 15820904074fSPatrick Farrell offsArrayWithAll[(ooff + p)*Nf + k] = localDof; 15830904074fSPatrick Farrell } 15845f824522SMatthew G. Knepley } 15854bbf5ea8SMatthew G. Knepley } 15864bbf5ea8SMatthew G. Knepley 15879566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&globalBcs)); 15889566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&ownedpts)); 15899566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&seenpts)); 15909566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&owneddofs)); 15919566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&seendofs)); 15929566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&artificialbcs)); 1593557beb66SLawrence Mitchell 15944bbf5ea8SMatthew G. Knepley /* At this point, we have a hash table ht built that maps globalDof -> localDof. 15954bbf5ea8SMatthew G. Knepley We need to create the dof table laid out cellwise first, then by subspace, 15964bbf5ea8SMatthew G. Knepley as the assembler assembles cell-wise and we need to stuff the different 15974bbf5ea8SMatthew G. Knepley contributions of the different function spaces to the right places. So we loop 15984bbf5ea8SMatthew G. Knepley over cells, then over subspaces. */ 15994bbf5ea8SMatthew G. Knepley if (patch->nsubspaces > 1) { /* for nsubspaces = 1, data we need is already in dofsArray */ 16004bbf5ea8SMatthew G. Knepley for (i = off; i < off + dof; ++i) { 16014bbf5ea8SMatthew G. Knepley const PetscInt c = cellsArray[i]; 16025f824522SMatthew G. Knepley PetscInt cell = c; 16034bbf5ea8SMatthew G. Knepley 16049566063dSJacob Faibussowitsch if (cellNumbering) PetscCall(PetscSectionGetOffset(cellNumbering, c, &cell)); 16054bbf5ea8SMatthew G. Knepley for (k = 0; k < patch->nsubspaces; ++k) { 16064bbf5ea8SMatthew G. Knepley const PetscInt *cellNodeMap = patch->cellNodeMap[k]; 16074bbf5ea8SMatthew G. Knepley PetscInt nodesPerCell = patch->nodesPerCell[k]; 16084bbf5ea8SMatthew G. Knepley PetscInt subspaceOffset = patch->subspaceOffsets[k]; 16094bbf5ea8SMatthew G. Knepley PetscInt bs = patch->bs[k]; 16104bbf5ea8SMatthew G. Knepley 16114bbf5ea8SMatthew G. Knepley for (j = 0; j < nodesPerCell; ++j) { 16124bbf5ea8SMatthew G. Knepley for (l = 0; l < bs; ++l) { 16135f824522SMatthew G. Knepley const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset; 16144bbf5ea8SMatthew G. Knepley PetscInt localDof; 16154bbf5ea8SMatthew G. Knepley 16169566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(ht, globalDof, &localDof)); 1617557beb66SLawrence Mitchell /* If it's not in the hash table, i.e. is a BC dof, 16181b68eb51SMatthew G. Knepley then the PetscHSetIMap above gives -1, which matches 1619557beb66SLawrence Mitchell exactly the convention for PETSc's matrix assembly to 1620557beb66SLawrence Mitchell ignore the dof. So we don't need to do anything here */ 1621c2e6f3c0SFlorian Wechsung asmArray[asmKey] = localDof; 16220904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 16239566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(htWithArtificial, globalDof, &localDof)); 1624c2e6f3c0SFlorian Wechsung asmArrayWithArtificial[asmKey] = localDof; 1625c2e6f3c0SFlorian Wechsung } 16260904074fSPatrick Farrell if (isNonlinear) { 16279566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(htWithAll, globalDof, &localDof)); 16280904074fSPatrick Farrell asmArrayWithAll[asmKey] = localDof; 16290904074fSPatrick Farrell } 1630c2e6f3c0SFlorian Wechsung asmKey++; 16314bbf5ea8SMatthew G. Knepley } 16324bbf5ea8SMatthew G. Knepley } 16334bbf5ea8SMatthew G. Knepley } 16344bbf5ea8SMatthew G. Knepley } 16354bbf5ea8SMatthew G. Knepley } 16364bbf5ea8SMatthew G. Knepley } 1637c2e6f3c0SFlorian Wechsung if (1 == patch->nsubspaces) { 16389566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(asmArray, dofsArray, numDofs)); 16390904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 16409566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(asmArrayWithArtificial, dofsArrayWithArtificial, numDofs)); 1641c2e6f3c0SFlorian Wechsung } 16421baa6e33SBarry Smith if (isNonlinear) PetscCall(PetscArraycpy(asmArrayWithAll, dofsArrayWithAll, numDofs)); 1643c2e6f3c0SFlorian Wechsung } 16444bbf5ea8SMatthew G. Knepley 16459566063dSJacob Faibussowitsch PetscCall(PetscHMapIDestroy(&ht)); 16469566063dSJacob Faibussowitsch PetscCall(PetscHMapIDestroy(&htWithArtificial)); 16479566063dSJacob Faibussowitsch PetscCall(PetscHMapIDestroy(&htWithAll)); 16489566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(cells, &cellsArray)); 16499566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(points, &pointsArray)); 16509566063dSJacob Faibussowitsch PetscCall(PetscFree(dofsArray)); 16510904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 16529566063dSJacob Faibussowitsch PetscCall(PetscFree(dofsArrayWithArtificial)); 1653c2e6f3c0SFlorian Wechsung } 16541baa6e33SBarry Smith if (isNonlinear) PetscCall(PetscFree(dofsArrayWithAll)); 16555f824522SMatthew G. Knepley /* Create placeholder section for map from points to patch dofs */ 16569566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->patchSection)); 16579566063dSJacob Faibussowitsch PetscCall(PetscSectionSetNumFields(patch->patchSection, patch->nsubspaces)); 16581e5fa6bbSLawrence Mitchell if (patch->combined) { 16591e5fa6bbSLawrence Mitchell PetscInt numFields; 16609566063dSJacob Faibussowitsch PetscCall(PetscSectionGetNumFields(patch->dofSection[0], &numFields)); 166163a3b9bcSJacob 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); 16629566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->dofSection[0], &pStart, &pEnd)); 16639566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(patch->patchSection, pStart, pEnd)); 16645f824522SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 16655f824522SMatthew G. Knepley PetscInt dof, fdof, f; 16665f824522SMatthew G. Knepley 16679566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->dofSection[0], p, &dof)); 16689566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(patch->patchSection, p, dof)); 16695f824522SMatthew G. Knepley for (f = 0; f < patch->nsubspaces; ++f) { 16709566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldDof(patch->dofSection[0], p, f, &fdof)); 16719566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(patch->patchSection, p, f, fdof)); 16725f824522SMatthew G. Knepley } 16731e5fa6bbSLawrence Mitchell } 16741e5fa6bbSLawrence Mitchell } else { 16751e5fa6bbSLawrence Mitchell PetscInt pStartf, pEndf, f; 16761e5fa6bbSLawrence Mitchell pStart = PETSC_MAX_INT; 16771e5fa6bbSLawrence Mitchell pEnd = PETSC_MIN_INT; 16781e5fa6bbSLawrence Mitchell for (f = 0; f < patch->nsubspaces; ++f) { 16799566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf)); 16801e5fa6bbSLawrence Mitchell pStart = PetscMin(pStart, pStartf); 16811e5fa6bbSLawrence Mitchell pEnd = PetscMax(pEnd, pEndf); 16821e5fa6bbSLawrence Mitchell } 16839566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(patch->patchSection, pStart, pEnd)); 16841e5fa6bbSLawrence Mitchell for (f = 0; f < patch->nsubspaces; ++f) { 16859566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf)); 16861e5fa6bbSLawrence Mitchell for (p = pStartf; p < pEndf; ++p) { 16871e5fa6bbSLawrence Mitchell PetscInt fdof; 16889566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->dofSection[f], p, &fdof)); 16899566063dSJacob Faibussowitsch PetscCall(PetscSectionAddDof(patch->patchSection, p, fdof)); 16909566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(patch->patchSection, p, f, fdof)); 1691bdd9e0cdSPatrick Farrell } 1692bdd9e0cdSPatrick Farrell } 16935f824522SMatthew G. Knepley } 16949566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(patch->patchSection)); 16959566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUseFieldOffsets(patch->patchSection, PETSC_TRUE)); 16964bbf5ea8SMatthew G. Knepley /* Replace cell indices with firedrake-numbered ones. */ 16979566063dSJacob Faibussowitsch PetscCall(ISGeneralSetIndices(cells, numCells, (const PetscInt *) newCellsArray, PETSC_OWN_POINTER)); 16989566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofs, globalDofsArray, PETSC_OWN_POINTER, &patch->gtol)); 16999566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) patch->gtol, "Global Indices")); 17009566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_g2l_view", patch->classname)); 17019566063dSJacob Faibussowitsch PetscCall(PetscSectionViewFromOptions(patch->gtolCounts, (PetscObject) pc, option)); 17029566063dSJacob Faibussowitsch PetscCall(ISViewFromOptions(patch->gtol, (PetscObject) pc, option)); 17039566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArray, PETSC_OWN_POINTER, &patch->dofs)); 17049566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArray, PETSC_OWN_POINTER, &patch->offs)); 17050904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 17069566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithArtificial, globalDofsArrayWithArtificial, PETSC_OWN_POINTER, &patch->gtolWithArtificial)); 17079566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithArtificial, PETSC_OWN_POINTER, &patch->dofsWithArtificial)); 17089566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithArtificial, PETSC_OWN_POINTER, &patch->offsWithArtificial)); 1709c2e6f3c0SFlorian Wechsung } 17100904074fSPatrick Farrell if (isNonlinear) { 17119566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithAll, globalDofsArrayWithAll, PETSC_OWN_POINTER, &patch->gtolWithAll)); 17129566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithAll, PETSC_OWN_POINTER, &patch->dofsWithAll)); 17139566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithAll, PETSC_OWN_POINTER, &patch->offsWithAll)); 17140904074fSPatrick Farrell } 17154bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 17164bbf5ea8SMatthew G. Knepley } 17174bbf5ea8SMatthew G. Knepley 1718c2e6f3c0SFlorian Wechsung static PetscErrorCode PCPatchCreateMatrix_Private(PC pc, PetscInt point, Mat *mat, PetscBool withArtificial) 17194bbf5ea8SMatthew G. Knepley { 17204bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 17214bbf5ea8SMatthew G. Knepley PetscBool flg; 17224bbf5ea8SMatthew G. Knepley PetscInt csize, rsize; 17234bbf5ea8SMatthew G. Knepley const char *prefix = NULL; 17244bbf5ea8SMatthew G. Knepley 17254bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 1726c2e6f3c0SFlorian Wechsung if (withArtificial) { 1727e047a90bSFlorian Wechsung /* would be nice if we could create a rectangular matrix of size numDofsWithArtificial x numDofs here */ 17289d4fc724SLawrence Mitchell PetscInt pStart; 17299566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->gtolCountsWithArtificial, &pStart, NULL)); 17309566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, point + pStart, &rsize)); 17319d4fc724SLawrence Mitchell csize = rsize; 1732ff201f6aSFlorian Wechsung } else { 17339d4fc724SLawrence Mitchell PetscInt pStart; 17349566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, NULL)); 17359566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCounts, point + pStart, &rsize)); 17369d4fc724SLawrence Mitchell csize = rsize; 1737c2e6f3c0SFlorian Wechsung } 1738c2e6f3c0SFlorian Wechsung 17399566063dSJacob Faibussowitsch PetscCall(MatCreate(PETSC_COMM_SELF, mat)); 17409566063dSJacob Faibussowitsch PetscCall(PCGetOptionsPrefix(pc, &prefix)); 17419566063dSJacob Faibussowitsch PetscCall(MatSetOptionsPrefix(*mat, prefix)); 17429566063dSJacob Faibussowitsch PetscCall(MatAppendOptionsPrefix(*mat, "pc_patch_sub_")); 17439566063dSJacob Faibussowitsch if (patch->sub_mat_type) PetscCall(MatSetType(*mat, patch->sub_mat_type)); 17449566063dSJacob Faibussowitsch else if (!patch->sub_mat_type) PetscCall(MatSetType(*mat, MATDENSE)); 17459566063dSJacob Faibussowitsch PetscCall(MatSetSizes(*mat, rsize, csize, rsize, csize)); 17469566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject) *mat, MATDENSE, &flg)); 17479566063dSJacob Faibussowitsch if (!flg) PetscCall(PetscObjectTypeCompare((PetscObject)*mat, MATSEQDENSE, &flg)); 17484bbf5ea8SMatthew G. Knepley /* Sparse patch matrices */ 17494bbf5ea8SMatthew G. Knepley if (!flg) { 17504bbf5ea8SMatthew G. Knepley PetscBT bt; 17514bbf5ea8SMatthew G. Knepley PetscInt *dnnz = NULL; 17524bbf5ea8SMatthew G. Knepley const PetscInt *dofsArray = NULL; 17534bbf5ea8SMatthew G. Knepley PetscInt pStart, pEnd, ncell, offset, c, i, j; 17544bbf5ea8SMatthew G. Knepley 1755c2e6f3c0SFlorian Wechsung if (withArtificial) { 17569566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->dofsWithArtificial, &dofsArray)); 1757ff201f6aSFlorian Wechsung } else { 17589566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->dofs, &dofsArray)); 1759c2e6f3c0SFlorian Wechsung } 17609566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd)); 17614bbf5ea8SMatthew G. Knepley point += pStart; 176263a3b9bcSJacob Faibussowitsch PetscCheck(point < pEnd,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %" PetscInt_FMT " not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, pStart, pEnd); 17639566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->cellCounts, point, &ncell)); 17649566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->cellCounts, point, &offset)); 17659566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_Prealloc, pc, 0, 0, 0)); 1766b2866507SPatrick Farrell /* A PetscBT uses N^2 bits to store the sparsity pattern on a 17674bbf5ea8SMatthew G. Knepley * patch. This is probably OK if the patches are not too big, 1768b2866507SPatrick Farrell * but uses too much memory. We therefore switch based on rsize. */ 1769b2866507SPatrick Farrell if (rsize < 3000) { /* FIXME: I picked this switch value out of my hat */ 1770d63cebbaSPatrick Farrell PetscScalar *zeroes; 1771d63cebbaSPatrick Farrell PetscInt rows; 1772d63cebbaSPatrick Farrell 17739566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(rsize, &dnnz)); 17749566063dSJacob Faibussowitsch PetscCall(PetscBTCreate(rsize*rsize, &bt)); 17754bbf5ea8SMatthew G. Knepley for (c = 0; c < ncell; ++c) { 17764bbf5ea8SMatthew G. Knepley const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell; 17774bbf5ea8SMatthew G. Knepley for (i = 0; i < patch->totalDofsPerCell; ++i) { 17784bbf5ea8SMatthew G. Knepley const PetscInt row = idx[i]; 1779557beb66SLawrence Mitchell if (row < 0) continue; 17804bbf5ea8SMatthew G. Knepley for (j = 0; j < patch->totalDofsPerCell; ++j) { 17814bbf5ea8SMatthew G. Knepley const PetscInt col = idx[j]; 17824bbf5ea8SMatthew G. Knepley const PetscInt key = row*rsize + col; 1783557beb66SLawrence Mitchell if (col < 0) continue; 17844bbf5ea8SMatthew G. Knepley if (!PetscBTLookupSet(bt, key)) ++dnnz[row]; 17854bbf5ea8SMatthew G. Knepley } 17864bbf5ea8SMatthew G. Knepley } 17874bbf5ea8SMatthew G. Knepley } 1788d63cebbaSPatrick Farrell 1789d63cebbaSPatrick Farrell if (patch->usercomputeopintfacet) { 1790d63cebbaSPatrick Farrell const PetscInt *intFacetsArray = NULL; 1791d63cebbaSPatrick Farrell PetscInt i, numIntFacets, intFacetOffset; 1792d63cebbaSPatrick Farrell const PetscInt *facetCells = NULL; 1793d63cebbaSPatrick Farrell 17949566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets)); 17959566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset)); 17969566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells)); 17979566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray)); 1798d63cebbaSPatrick Farrell for (i = 0; i < numIntFacets; i++) { 1799d63cebbaSPatrick Farrell const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0]; 1800d63cebbaSPatrick Farrell const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1]; 1801d63cebbaSPatrick Farrell PetscInt celli, cellj; 1802d63cebbaSPatrick Farrell 1803d63cebbaSPatrick Farrell for (celli = 0; celli < patch->totalDofsPerCell; celli++) { 1804d63cebbaSPatrick Farrell const PetscInt row = dofsArray[(offset + cell0)*patch->totalDofsPerCell + celli]; 1805b5c64f08SPatrick Farrell if (row < 0) continue; 1806d63cebbaSPatrick Farrell for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) { 1807d63cebbaSPatrick Farrell const PetscInt col = dofsArray[(offset + cell1)*patch->totalDofsPerCell + cellj]; 1808d63cebbaSPatrick Farrell const PetscInt key = row*rsize + col; 1809d63cebbaSPatrick Farrell if (col < 0) continue; 1810d63cebbaSPatrick Farrell if (!PetscBTLookupSet(bt, key)) ++dnnz[row]; 1811d63cebbaSPatrick Farrell } 1812d63cebbaSPatrick Farrell } 1813d63cebbaSPatrick Farrell 1814d63cebbaSPatrick Farrell for (celli = 0; celli < patch->totalDofsPerCell; celli++) { 1815d63cebbaSPatrick Farrell const PetscInt row = dofsArray[(offset + cell1)*patch->totalDofsPerCell + celli]; 1816b5c64f08SPatrick Farrell if (row < 0) continue; 1817d63cebbaSPatrick Farrell for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) { 1818d63cebbaSPatrick Farrell const PetscInt col = dofsArray[(offset + cell0)*patch->totalDofsPerCell + cellj]; 1819d63cebbaSPatrick Farrell const PetscInt key = row*rsize + col; 1820d63cebbaSPatrick Farrell if (col < 0) continue; 1821d63cebbaSPatrick Farrell if (!PetscBTLookupSet(bt, key)) ++dnnz[row]; 1822d63cebbaSPatrick Farrell } 1823d63cebbaSPatrick Farrell } 1824d63cebbaSPatrick Farrell } 1825d63cebbaSPatrick Farrell } 18269566063dSJacob Faibussowitsch PetscCall(PetscBTDestroy(&bt)); 18279566063dSJacob Faibussowitsch PetscCall(MatXAIJSetPreallocation(*mat, 1, dnnz, NULL, NULL, NULL)); 18289566063dSJacob Faibussowitsch PetscCall(PetscFree(dnnz)); 1829d63cebbaSPatrick Farrell 18309566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(patch->totalDofsPerCell*patch->totalDofsPerCell, &zeroes)); 1831d63cebbaSPatrick Farrell for (c = 0; c < ncell; ++c) { 1832d63cebbaSPatrick Farrell const PetscInt *idx = &dofsArray[(offset + c)*patch->totalDofsPerCell]; 18339566063dSJacob Faibussowitsch PetscCall(MatSetValues(*mat, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, zeroes, INSERT_VALUES)); 1834d63cebbaSPatrick Farrell } 18359566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(*mat, &rows, NULL)); 1836d63cebbaSPatrick Farrell for (i = 0; i < rows; ++i) { 18379566063dSJacob Faibussowitsch PetscCall(MatSetValues(*mat, 1, &i, 1, &i, zeroes, INSERT_VALUES)); 1838d63cebbaSPatrick Farrell } 1839d63cebbaSPatrick Farrell 1840d63cebbaSPatrick Farrell if (patch->usercomputeopintfacet) { 1841d63cebbaSPatrick Farrell const PetscInt *intFacetsArray = NULL; 1842d63cebbaSPatrick Farrell PetscInt i, numIntFacets, intFacetOffset; 1843d63cebbaSPatrick Farrell const PetscInt *facetCells = NULL; 1844d63cebbaSPatrick Farrell 18459566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets)); 18469566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset)); 18479566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells)); 18489566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray)); 1849d63cebbaSPatrick Farrell for (i = 0; i < numIntFacets; i++) { 1850d63cebbaSPatrick Farrell const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0]; 1851d63cebbaSPatrick Farrell const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1]; 1852d63cebbaSPatrick Farrell const PetscInt *cell0idx = &dofsArray[(offset + cell0)*patch->totalDofsPerCell]; 1853d63cebbaSPatrick Farrell const PetscInt *cell1idx = &dofsArray[(offset + cell1)*patch->totalDofsPerCell]; 18549566063dSJacob Faibussowitsch PetscCall(MatSetValues(*mat, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, zeroes, INSERT_VALUES)); 18559566063dSJacob Faibussowitsch PetscCall(MatSetValues(*mat, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, zeroes, INSERT_VALUES)); 1856d63cebbaSPatrick Farrell } 1857d63cebbaSPatrick Farrell } 1858d63cebbaSPatrick Farrell 18599566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY)); 18609566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY)); 1861d63cebbaSPatrick Farrell 18629566063dSJacob Faibussowitsch PetscCall(PetscFree(zeroes)); 1863d63cebbaSPatrick Farrell 1864b2866507SPatrick Farrell } else { /* rsize too big, use MATPREALLOCATOR */ 1865b2866507SPatrick Farrell Mat preallocator; 1866b2866507SPatrick Farrell PetscScalar* vals; 1867b2866507SPatrick Farrell 18689566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(patch->totalDofsPerCell*patch->totalDofsPerCell, &vals)); 18699566063dSJacob Faibussowitsch PetscCall(MatCreate(PETSC_COMM_SELF, &preallocator)); 18709566063dSJacob Faibussowitsch PetscCall(MatSetType(preallocator, MATPREALLOCATOR)); 18719566063dSJacob Faibussowitsch PetscCall(MatSetSizes(preallocator, rsize, rsize, rsize, rsize)); 18729566063dSJacob Faibussowitsch PetscCall(MatSetUp(preallocator)); 187311bcd083SPatrick Farrell 1874b2866507SPatrick Farrell for (c = 0; c < ncell; ++c) { 1875b2866507SPatrick Farrell const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell; 18769566063dSJacob Faibussowitsch PetscCall(MatSetValues(preallocator, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, vals, INSERT_VALUES)); 1877b2866507SPatrick Farrell } 187811bcd083SPatrick Farrell 187911bcd083SPatrick Farrell if (patch->usercomputeopintfacet) { 188011bcd083SPatrick Farrell const PetscInt *intFacetsArray = NULL; 188111bcd083SPatrick Farrell PetscInt i, numIntFacets, intFacetOffset; 188211bcd083SPatrick Farrell const PetscInt *facetCells = NULL; 188311bcd083SPatrick Farrell 18849566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets)); 18859566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset)); 18869566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells)); 18879566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray)); 188811bcd083SPatrick Farrell for (i = 0; i < numIntFacets; i++) { 188911bcd083SPatrick Farrell const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0]; 189011bcd083SPatrick Farrell const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1]; 189111bcd083SPatrick Farrell const PetscInt *cell0idx = &dofsArray[(offset + cell0)*patch->totalDofsPerCell]; 189211bcd083SPatrick Farrell const PetscInt *cell1idx = &dofsArray[(offset + cell1)*patch->totalDofsPerCell]; 18939566063dSJacob Faibussowitsch PetscCall(MatSetValues(preallocator, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, vals, INSERT_VALUES)); 18949566063dSJacob Faibussowitsch PetscCall(MatSetValues(preallocator, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, vals, INSERT_VALUES)); 189511bcd083SPatrick Farrell } 189611bcd083SPatrick Farrell } 189711bcd083SPatrick Farrell 18989566063dSJacob Faibussowitsch PetscCall(PetscFree(vals)); 18999566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY)); 19009566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY)); 19019566063dSJacob Faibussowitsch PetscCall(MatPreallocatorPreallocate(preallocator, PETSC_TRUE, *mat)); 19029566063dSJacob Faibussowitsch PetscCall(MatDestroy(&preallocator)); 1903b2866507SPatrick Farrell } 19049566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_Prealloc, pc, 0, 0, 0)); 1905fe117d09SFlorian Wechsung if (withArtificial) { 19069566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->dofsWithArtificial, &dofsArray)); 1907fe117d09SFlorian Wechsung } else { 19089566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->dofs, &dofsArray)); 19094bbf5ea8SMatthew G. Knepley } 1910fe117d09SFlorian Wechsung } 19119566063dSJacob Faibussowitsch PetscCall(MatSetUp(*mat)); 19124bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 19134bbf5ea8SMatthew G. Knepley } 19144bbf5ea8SMatthew G. Knepley 19150904074fSPatrick Farrell 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) 191692d50984SMatthew G. Knepley { 191792d50984SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 1918b6bb21d1SLawrence Mitchell DM dm, plex; 191992d50984SMatthew G. Knepley PetscSection s; 192092d50984SMatthew G. Knepley const PetscInt *parray, *oarray; 192192d50984SMatthew G. Knepley PetscInt Nf = patch->nsubspaces, Np, poff, p, f; 192292d50984SMatthew G. Knepley 192392d50984SMatthew G. Knepley PetscFunctionBegin; 192428b400f6SJacob Faibussowitsch PetscCheck(!patch->precomputeElementTensors,PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Precomputing element tensors not implemented with DMPlex compute operator"); 19259566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm)); 19269566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex)); 1927b6bb21d1SLawrence Mitchell dm = plex; 19289566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, &s)); 192992d50984SMatthew G. Knepley /* Set offset into patch */ 19309566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->pointCounts, patchNum, &Np)); 19319566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->pointCounts, patchNum, &poff)); 19329566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->points, &parray)); 19339566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->offs, &oarray)); 193492d50984SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 193592d50984SMatthew G. Knepley for (p = 0; p < Np; ++p) { 193692d50984SMatthew G. Knepley const PetscInt point = parray[poff+p]; 193792d50984SMatthew G. Knepley PetscInt dof; 193892d50984SMatthew G. Knepley 19399566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldDof(patch->patchSection, point, f, &dof)); 19409566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f])); 19419566063dSJacob Faibussowitsch if (patch->nsubspaces == 1) PetscCall(PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f])); 19429566063dSJacob Faibussowitsch else PetscCall(PetscSectionSetOffset(patch->patchSection, point, -1)); 194392d50984SMatthew G. Knepley } 194492d50984SMatthew G. Knepley } 19459566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->points, &parray)); 19469566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->offs, &oarray)); 19479566063dSJacob Faibussowitsch if (patch->viewSection) PetscCall(ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection)); 19489566063dSJacob Faibussowitsch PetscCall(DMPlexComputeResidual_Patch_Internal(dm, patch->patchSection, cellIS, 0.0, x, NULL, F, ctx)); 19499566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm)); 195092d50984SMatthew G. Knepley PetscFunctionReturn(0); 195192d50984SMatthew G. Knepley } 195292d50984SMatthew G. Knepley 195392d50984SMatthew G. Knepley PetscErrorCode PCPatchComputeFunction_Internal(PC pc, Vec x, Vec F, PetscInt point) 195492d50984SMatthew G. Knepley { 195592d50984SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 195692d50984SMatthew G. Knepley const PetscInt *dofsArray; 19570904074fSPatrick Farrell const PetscInt *dofsArrayWithAll; 195892d50984SMatthew G. Knepley const PetscInt *cellsArray; 195992d50984SMatthew G. Knepley PetscInt ncell, offset, pStart, pEnd; 196092d50984SMatthew G. Knepley 196192d50984SMatthew G. Knepley PetscFunctionBegin; 19629566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0)); 1963ef1023bdSBarry Smith PetscCheck(patch->usercomputeop,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set callback"); 19649566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->dofs, &dofsArray)); 19659566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll)); 19669566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->cells, &cellsArray)); 19679566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd)); 196892d50984SMatthew G. Knepley 196992d50984SMatthew G. Knepley point += pStart; 197063a3b9bcSJacob Faibussowitsch PetscCheck(point < pEnd,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %" PetscInt_FMT " not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, pStart, pEnd); 197192d50984SMatthew G. Knepley 19729566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->cellCounts, point, &ncell)); 19739566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->cellCounts, point, &offset)); 197492d50984SMatthew G. Knepley if (ncell <= 0) { 19759566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 197692d50984SMatthew G. Knepley PetscFunctionReturn(0); 197792d50984SMatthew G. Knepley } 19789566063dSJacob Faibussowitsch PetscCall(VecSet(F, 0.0)); 197992d50984SMatthew G. Knepley /* Cannot reuse the same IS because the geometry info is being cached in it */ 19809566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS)); 1981792fecdfSBarry 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)); 19829566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->cellIS)); 19839566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->dofs, &dofsArray)); 19849566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll)); 19859566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->cells, &cellsArray)); 198692d50984SMatthew G. Knepley if (patch->viewMatrix) { 198792d50984SMatthew G. Knepley char name[PETSC_MAX_PATH_LEN]; 198892d50984SMatthew G. Knepley 198963a3b9bcSJacob Faibussowitsch PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch vector for Point %" PetscInt_FMT, point)); 19909566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) F, name)); 19919566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject) F, patch->viewerMatrix, patch->formatMatrix)); 199292d50984SMatthew G. Knepley } 19939566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 199492d50984SMatthew G. Knepley PetscFunctionReturn(0); 199592d50984SMatthew G. Knepley } 199692d50984SMatthew G. Knepley 19970904074fSPatrick Farrell 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) 19985f824522SMatthew G. Knepley { 19995f824522SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 2000b6bb21d1SLawrence Mitchell DM dm, plex; 20015f824522SMatthew G. Knepley PetscSection s; 20025f824522SMatthew G. Knepley const PetscInt *parray, *oarray; 20035f824522SMatthew G. Knepley PetscInt Nf = patch->nsubspaces, Np, poff, p, f; 20045f824522SMatthew G. Knepley 20055f824522SMatthew G. Knepley PetscFunctionBegin; 20069566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm)); 20079566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex)); 2008b6bb21d1SLawrence Mitchell dm = plex; 20099566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, &s)); 20105f824522SMatthew G. Knepley /* Set offset into patch */ 20119566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->pointCounts, patchNum, &Np)); 20129566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->pointCounts, patchNum, &poff)); 20139566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->points, &parray)); 20149566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->offs, &oarray)); 20155f824522SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 20165f824522SMatthew G. Knepley for (p = 0; p < Np; ++p) { 20175f824522SMatthew G. Knepley const PetscInt point = parray[poff+p]; 20185f824522SMatthew G. Knepley PetscInt dof; 20195f824522SMatthew G. Knepley 20209566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldDof(patch->patchSection, point, f, &dof)); 20219566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f])); 20229566063dSJacob Faibussowitsch if (patch->nsubspaces == 1) PetscCall(PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f])); 20239566063dSJacob Faibussowitsch else PetscCall(PetscSectionSetOffset(patch->patchSection, point, -1)); 20245f824522SMatthew G. Knepley } 20255f824522SMatthew G. Knepley } 20269566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->points, &parray)); 20279566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->offs, &oarray)); 20289566063dSJacob Faibussowitsch if (patch->viewSection) PetscCall(ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection)); 20295f824522SMatthew G. Knepley /* TODO Shut off MatViewFromOptions() in MatAssemblyEnd() here */ 20309566063dSJacob Faibussowitsch PetscCall(DMPlexComputeJacobian_Patch_Internal(dm, patch->patchSection, patch->patchSection, cellIS, 0.0, 0.0, x, NULL, J, J, ctx)); 20319566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm)); 20325f824522SMatthew G. Knepley PetscFunctionReturn(0); 20335f824522SMatthew G. Knepley } 20345f824522SMatthew G. Knepley 2035a685ae26SLawrence Mitchell /* This function zeros mat on entry */ 203634d8b122SPatrick Farrell PetscErrorCode PCPatchComputeOperator_Internal(PC pc, Vec x, Mat mat, PetscInt point, PetscBool withArtificial) 20374bbf5ea8SMatthew G. Knepley { 20384bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 20394bbf5ea8SMatthew G. Knepley const PetscInt *dofsArray; 20400904074fSPatrick Farrell const PetscInt *dofsArrayWithAll = NULL; 20414bbf5ea8SMatthew G. Knepley const PetscInt *cellsArray; 2042eb62eeaaSLawrence Mitchell PetscInt ncell, offset, pStart, pEnd, numIntFacets, intFacetOffset; 20434d04e9f1SPatrick Farrell PetscBool isNonlinear; 20444bbf5ea8SMatthew G. Knepley 20454bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 20469566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0)); 2047debbdec3SPatrick Farrell isNonlinear = patch->isNonlinear; 2048ef1023bdSBarry Smith PetscCheck(patch->usercomputeop,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set callback"); 2049c2e6f3c0SFlorian Wechsung if (withArtificial) { 20509566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->dofsWithArtificial, &dofsArray)); 2051c2e6f3c0SFlorian Wechsung } else { 20529566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->dofs, &dofsArray)); 2053c2e6f3c0SFlorian Wechsung } 20544d04e9f1SPatrick Farrell if (isNonlinear) { 20559566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll)); 20564d04e9f1SPatrick Farrell } 20579566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->cells, &cellsArray)); 20589566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd)); 20594bbf5ea8SMatthew G. Knepley 20604bbf5ea8SMatthew G. Knepley point += pStart; 206163a3b9bcSJacob Faibussowitsch PetscCheck(point < pEnd,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %" PetscInt_FMT " not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, pStart, pEnd); 20624bbf5ea8SMatthew G. Knepley 20639566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->cellCounts, point, &ncell)); 20649566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->cellCounts, point, &offset)); 20654bbf5ea8SMatthew G. Knepley if (ncell <= 0) { 20669566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 20674bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 20684bbf5ea8SMatthew G. Knepley } 20699566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(mat)); 2070fa84ea4cSLawrence Mitchell if (patch->precomputeElementTensors) { 2071fa84ea4cSLawrence Mitchell PetscInt i; 2072fa84ea4cSLawrence Mitchell PetscInt ndof = patch->totalDofsPerCell; 2073fa84ea4cSLawrence Mitchell const PetscScalar *elementTensors; 2074fa84ea4cSLawrence Mitchell 20759566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(patch->cellMats, &elementTensors)); 2076fa84ea4cSLawrence Mitchell for (i = 0; i < ncell; i++) { 2077fa84ea4cSLawrence Mitchell const PetscInt cell = cellsArray[i + offset]; 2078fa84ea4cSLawrence Mitchell const PetscInt *idx = dofsArray + (offset + i)*ndof; 2079fe988be2SFlorian Wechsung const PetscScalar *v = elementTensors + patch->precomputedTensorLocations[cell]*ndof*ndof; 20809566063dSJacob Faibussowitsch PetscCall(MatSetValues(mat, ndof, idx, ndof, idx, v, ADD_VALUES)); 2081fa84ea4cSLawrence Mitchell } 20829566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(patch->cellMats, &elementTensors)); 20839566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY)); 20849566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY)); 2085fa84ea4cSLawrence Mitchell } else { 20862aa6f319SMatthew G. Knepley /* Cannot reuse the same IS because the geometry info is being cached in it */ 20879566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS)); 2088792fecdfSBarry Smith PetscCallBack("PCPatch callback",patch->usercomputeop(pc, point, x, mat, patch->cellIS, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell, dofsArrayWithAll ? dofsArrayWithAll + offset*patch->totalDofsPerCell : NULL, patch->usercomputeopctx)); 2089fa84ea4cSLawrence Mitchell } 209059109abcSLawrence Mitchell if (patch->usercomputeopintfacet) { 20919566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets)); 20929566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset)); 2093eb62eeaaSLawrence Mitchell if (numIntFacets > 0) { 2094eb62eeaaSLawrence Mitchell /* For each interior facet, grab the two cells (in local numbering, and concatenate dof numberings for those cells) */ 2095eb62eeaaSLawrence Mitchell PetscInt *facetDofs = NULL, *facetDofsWithAll = NULL; 2096eb62eeaaSLawrence Mitchell const PetscInt *intFacetsArray = NULL; 2097eb62eeaaSLawrence Mitchell PetscInt idx = 0; 2098eb62eeaaSLawrence Mitchell PetscInt i, c, d; 2099de2d1767SPatrick Farrell PetscInt fStart; 2100b6bb21d1SLawrence Mitchell DM dm, plex; 2101eb62eeaaSLawrence Mitchell IS facetIS = NULL; 2102eb62eeaaSLawrence Mitchell const PetscInt *facetCells = NULL; 21037a50e09dSPatrick Farrell 21049566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells)); 21059566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray)); 21069566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm)); 21079566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex)); 2108b6bb21d1SLawrence Mitchell dm = plex; 21099566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, NULL)); 2110eb62eeaaSLawrence Mitchell /* FIXME: Pull this malloc out. */ 21119566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofs)); 2112eb62eeaaSLawrence Mitchell if (dofsArrayWithAll) { 21139566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofsWithAll)); 2114eb62eeaaSLawrence Mitchell } 2115f98464cbSLawrence Mitchell if (patch->precomputeElementTensors) { 2116f98464cbSLawrence Mitchell PetscInt nFacetDof = 2*patch->totalDofsPerCell; 2117f98464cbSLawrence Mitchell const PetscScalar *elementTensors; 2118f98464cbSLawrence Mitchell 21199566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(patch->intFacetMats, &elementTensors)); 2120f98464cbSLawrence Mitchell 2121f98464cbSLawrence Mitchell for (i = 0; i < numIntFacets; i++) { 2122f98464cbSLawrence Mitchell const PetscInt facet = intFacetsArray[i + intFacetOffset]; 2123de2d1767SPatrick Farrell const PetscScalar *v = elementTensors + patch->precomputedIntFacetTensorLocations[facet - fStart]*nFacetDof*nFacetDof; 2124f98464cbSLawrence Mitchell idx = 0; 2125f98464cbSLawrence Mitchell /* 2126f98464cbSLawrence Mitchell * 0--1 2127f98464cbSLawrence Mitchell * |\-| 2128f98464cbSLawrence Mitchell * |+\| 2129f98464cbSLawrence Mitchell * 2--3 2130f98464cbSLawrence Mitchell * [0, 2, 3, 0, 1, 3] 2131f98464cbSLawrence Mitchell */ 2132f98464cbSLawrence Mitchell for (c = 0; c < 2; c++) { 2133f98464cbSLawrence Mitchell const PetscInt cell = facetCells[2*(intFacetOffset + i) + c]; 2134f98464cbSLawrence Mitchell for (d = 0; d < patch->totalDofsPerCell; d++) { 2135f98464cbSLawrence Mitchell facetDofs[idx] = dofsArray[(offset + cell)*patch->totalDofsPerCell + d]; 2136f98464cbSLawrence Mitchell idx++; 2137f98464cbSLawrence Mitchell } 2138f98464cbSLawrence Mitchell } 21399566063dSJacob Faibussowitsch PetscCall(MatSetValues(mat, nFacetDof, facetDofs, nFacetDof, facetDofs, v, ADD_VALUES)); 2140f98464cbSLawrence Mitchell } 21419566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(patch->intFacetMats, &elementTensors)); 2142f98464cbSLawrence Mitchell } else { 2143eb62eeaaSLawrence Mitchell /* 2144eb62eeaaSLawrence Mitchell * 0--1 2145eb62eeaaSLawrence Mitchell * |\-| 2146eb62eeaaSLawrence Mitchell * |+\| 2147eb62eeaaSLawrence Mitchell * 2--3 2148eb62eeaaSLawrence Mitchell * [0, 2, 3, 0, 1, 3] 2149eb62eeaaSLawrence Mitchell */ 2150eb62eeaaSLawrence Mitchell for (i = 0; i < numIntFacets; i++) { 2151eb62eeaaSLawrence Mitchell for (c = 0; c < 2; c++) { 2152eb62eeaaSLawrence Mitchell const PetscInt cell = facetCells[2*(intFacetOffset + i) + c]; 2153eb62eeaaSLawrence Mitchell for (d = 0; d < patch->totalDofsPerCell; d++) { 2154eb62eeaaSLawrence Mitchell facetDofs[idx] = dofsArray[(offset + cell)*patch->totalDofsPerCell + d]; 2155eb62eeaaSLawrence Mitchell if (dofsArrayWithAll) { 2156eb62eeaaSLawrence Mitchell facetDofsWithAll[idx] = dofsArrayWithAll[(offset + cell)*patch->totalDofsPerCell + d]; 2157eb62eeaaSLawrence Mitchell } 2158eb62eeaaSLawrence Mitchell idx++; 2159eb62eeaaSLawrence Mitchell } 2160eb62eeaaSLawrence Mitchell } 2161eb62eeaaSLawrence Mitchell } 21629566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray + intFacetOffset, PETSC_USE_POINTER, &facetIS)); 21639566063dSJacob Faibussowitsch PetscCall(patch->usercomputeopintfacet(pc, point, x, mat, facetIS, 2*numIntFacets*patch->totalDofsPerCell, facetDofs, facetDofsWithAll, patch->usercomputeopintfacetctx)); 21649566063dSJacob Faibussowitsch PetscCall(ISDestroy(&facetIS)); 2165f98464cbSLawrence Mitchell } 21669566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->intFacetsToPatchCell, &facetCells)); 21679566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->intFacets, &intFacetsArray)); 21689566063dSJacob Faibussowitsch PetscCall(PetscFree(facetDofs)); 21699566063dSJacob Faibussowitsch PetscCall(PetscFree(facetDofsWithAll)); 21709566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm)); 2171eb62eeaaSLawrence Mitchell } 217259109abcSLawrence Mitchell } 21736710cc29SPatrick Farrell 21749566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY)); 21759566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY)); 21766710cc29SPatrick Farrell 2177c73d2cf6SLawrence Mitchell if (!(withArtificial || isNonlinear) && patch->denseinverse) { 2178c73d2cf6SLawrence Mitchell MatFactorInfo info; 2179c73d2cf6SLawrence Mitchell PetscBool flg; 21809566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)mat, MATSEQDENSE, &flg)); 218128b400f6SJacob Faibussowitsch PetscCheck(flg,PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Invalid Mat type for dense inverse"); 21829566063dSJacob Faibussowitsch PetscCall(MatFactorInfoInitialize(&info)); 21839566063dSJacob Faibussowitsch PetscCall(MatLUFactor(mat, NULL, NULL, &info)); 21849566063dSJacob Faibussowitsch PetscCall(MatSeqDenseInvertFactors_Private(mat)); 2185c73d2cf6SLawrence Mitchell } 21869566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->cellIS)); 21874d04e9f1SPatrick Farrell if (withArtificial) { 21889566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->dofsWithArtificial, &dofsArray)); 2189c2e6f3c0SFlorian Wechsung } else { 21909566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->dofs, &dofsArray)); 2191c2e6f3c0SFlorian Wechsung } 21924d04e9f1SPatrick Farrell if (isNonlinear) { 21939566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll)); 21944d04e9f1SPatrick Farrell } 21959566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->cells, &cellsArray)); 21962aa6f319SMatthew G. Knepley if (patch->viewMatrix) { 21972aa6f319SMatthew G. Knepley char name[PETSC_MAX_PATH_LEN]; 21982aa6f319SMatthew G. Knepley 219963a3b9bcSJacob Faibussowitsch PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch matrix for Point %" PetscInt_FMT, point)); 22009566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) mat, name)); 22019566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject) mat, patch->viewerMatrix, patch->formatMatrix)); 22022aa6f319SMatthew G. Knepley } 22039566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 22044bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 22054bbf5ea8SMatthew G. Knepley } 22064bbf5ea8SMatthew G. Knepley 2207fa84ea4cSLawrence Mitchell static PetscErrorCode MatSetValues_PCPatch_Private(Mat mat, PetscInt m, const PetscInt idxm[], 2208fa84ea4cSLawrence Mitchell PetscInt n, const PetscInt idxn[], const PetscScalar *v, InsertMode addv) 2209fa84ea4cSLawrence Mitchell { 2210fa84ea4cSLawrence Mitchell Vec data; 2211fa84ea4cSLawrence Mitchell PetscScalar *array; 2212fe988be2SFlorian Wechsung PetscInt bs, nz, i, j, cell; 2213fa84ea4cSLawrence Mitchell 22149566063dSJacob Faibussowitsch PetscCall(MatShellGetContext(mat, &data)); 22159566063dSJacob Faibussowitsch PetscCall(VecGetBlockSize(data, &bs)); 22169566063dSJacob Faibussowitsch PetscCall(VecGetSize(data, &nz)); 22179566063dSJacob Faibussowitsch PetscCall(VecGetArray(data, &array)); 221808401ef6SPierre Jolivet PetscCheck(m == n,PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Only for square insertion"); 221933cbca70SPatrick Farrell cell = (PetscInt)(idxm[0]/bs); /* use the fact that this is called once per cell */ 2220fa84ea4cSLawrence Mitchell for (i = 0; i < m; i++) { 222108401ef6SPierre Jolivet PetscCheck(idxm[i] == idxn[i],PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Row and column indices must match!"); 2222fa84ea4cSLawrence Mitchell for (j = 0; j < n; j++) { 2223fa84ea4cSLawrence Mitchell const PetscScalar v_ = v[i*bs + j]; 2224fa84ea4cSLawrence Mitchell /* Indexing is special to the data structure we have! */ 2225fa84ea4cSLawrence Mitchell if (addv == INSERT_VALUES) { 2226fe988be2SFlorian Wechsung array[cell*bs*bs + i*bs + j] = v_; 2227fa84ea4cSLawrence Mitchell } else { 2228fe988be2SFlorian Wechsung array[cell*bs*bs + i*bs + j] += v_; 2229fa84ea4cSLawrence Mitchell } 2230fa84ea4cSLawrence Mitchell } 2231fa84ea4cSLawrence Mitchell } 22329566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(data, &array)); 2233fa84ea4cSLawrence Mitchell PetscFunctionReturn(0); 2234fa84ea4cSLawrence Mitchell } 2235fa84ea4cSLawrence Mitchell 2236fa84ea4cSLawrence Mitchell static PetscErrorCode PCPatchPrecomputePatchTensors_Private(PC pc) 2237fa84ea4cSLawrence Mitchell { 2238fa84ea4cSLawrence Mitchell PC_PATCH *patch = (PC_PATCH *)pc->data; 2239fa84ea4cSLawrence Mitchell const PetscInt *cellsArray; 2240fa84ea4cSLawrence Mitchell PetscInt ncell, offset; 2241fa84ea4cSLawrence Mitchell const PetscInt *dofMapArray; 2242fa84ea4cSLawrence Mitchell PetscInt i, j; 2243fa84ea4cSLawrence Mitchell IS dofMap; 2244fa84ea4cSLawrence Mitchell IS cellIS; 2245fa84ea4cSLawrence Mitchell const PetscInt ndof = patch->totalDofsPerCell; 2246fa84ea4cSLawrence Mitchell Mat vecMat; 2247fe988be2SFlorian Wechsung PetscInt cStart, cEnd; 2248fe988be2SFlorian Wechsung DM dm, plex; 2249fe988be2SFlorian Wechsung 22509566063dSJacob Faibussowitsch PetscCall(ISGetSize(patch->cells, &ncell)); 2251e9c2c94bSFlorian Wechsung if (!ncell) { /* No cells to assemble over -> skip */ 2252e9c2c94bSFlorian Wechsung PetscFunctionReturn(0); 2253e9c2c94bSFlorian Wechsung } 2254e9c2c94bSFlorian Wechsung 22559566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0)); 2256fa84ea4cSLawrence Mitchell 22579566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm)); 22589566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex)); 2259b6bb21d1SLawrence Mitchell dm = plex; 2260fa84ea4cSLawrence Mitchell if (!patch->allCells) { 2261fa84ea4cSLawrence Mitchell PetscHSetI cells; 2262fa84ea4cSLawrence Mitchell PetscHashIter hi; 2263fa84ea4cSLawrence Mitchell PetscInt pStart, pEnd; 2264fa84ea4cSLawrence Mitchell PetscInt *allCells = NULL; 22659566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&cells)); 22669566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->cells, &cellsArray)); 22679566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd)); 2268fa84ea4cSLawrence Mitchell for (i = pStart; i < pEnd; i++) { 22699566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->cellCounts, i, &ncell)); 22709566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->cellCounts, i, &offset)); 2271fa84ea4cSLawrence Mitchell if (ncell <= 0) continue; 2272fa84ea4cSLawrence Mitchell for (j = 0; j < ncell; j++) { 22739566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(cells, cellsArray[offset + j])); 2274fa84ea4cSLawrence Mitchell } 2275fa84ea4cSLawrence Mitchell } 22769566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->cells, &cellsArray)); 22779566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(cells, &ncell)); 22789566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(ncell, &allCells)); 22799566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 22809566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(cEnd-cStart, &patch->precomputedTensorLocations)); 2281fa84ea4cSLawrence Mitchell i = 0; 2282fa84ea4cSLawrence Mitchell PetscHashIterBegin(cells, hi); 2283fa84ea4cSLawrence Mitchell while (!PetscHashIterAtEnd(cells, hi)) { 2284fe988be2SFlorian Wechsung PetscHashIterGetKey(cells, hi, allCells[i]); 2285fe988be2SFlorian Wechsung patch->precomputedTensorLocations[allCells[i]] = i; 2286fa84ea4cSLawrence Mitchell PetscHashIterNext(cells, hi); 2287fe988be2SFlorian Wechsung i++; 2288fa84ea4cSLawrence Mitchell } 22899566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&cells)); 22909566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, allCells, PETSC_OWN_POINTER, &patch->allCells)); 2291fa84ea4cSLawrence Mitchell } 22929566063dSJacob Faibussowitsch PetscCall(ISGetSize(patch->allCells, &ncell)); 2293fa84ea4cSLawrence Mitchell if (!patch->cellMats) { 22949566063dSJacob Faibussowitsch PetscCall(VecCreateSeq(PETSC_COMM_SELF, ncell*ndof*ndof, &patch->cellMats)); 22959566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(patch->cellMats, ndof)); 2296fa84ea4cSLawrence Mitchell } 22979566063dSJacob Faibussowitsch PetscCall(VecSet(patch->cellMats, 0)); 2298fa84ea4cSLawrence Mitchell 2299d0609cedSBarry Smith PetscCall(MatCreateShell(PETSC_COMM_SELF, ncell*ndof, ncell*ndof, ncell*ndof, ncell*ndof,(void*)patch->cellMats, &vecMat)); 23009566063dSJacob Faibussowitsch PetscCall(MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void(*)(void))&MatSetValues_PCPatch_Private)); 23019566063dSJacob Faibussowitsch PetscCall(ISGetSize(patch->allCells, &ncell)); 23029566063dSJacob Faibussowitsch PetscCall(ISCreateStride(PETSC_COMM_SELF, ndof*ncell, 0, 1, &dofMap)); 23039566063dSJacob Faibussowitsch PetscCall(ISGetIndices(dofMap, &dofMapArray)); 23049566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->allCells, &cellsArray)); 23059566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray, PETSC_USE_POINTER, &cellIS)); 2306fa84ea4cSLawrence 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",patch->usercomputeop(pc, -1, NULL, vecMat, cellIS, ndof*ncell, dofMapArray, NULL, patch->usercomputeopctx)); 23089566063dSJacob Faibussowitsch PetscCall(ISDestroy(&cellIS)); 23099566063dSJacob Faibussowitsch PetscCall(MatDestroy(&vecMat)); 23109566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->allCells, &cellsArray)); 23119566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(dofMap, &dofMapArray)); 23129566063dSJacob Faibussowitsch PetscCall(ISDestroy(&dofMap)); 2313f98464cbSLawrence Mitchell 2314f98464cbSLawrence Mitchell if (patch->usercomputeopintfacet) { 2315f98464cbSLawrence Mitchell PetscInt nIntFacets; 2316f98464cbSLawrence Mitchell IS intFacetsIS; 2317f98464cbSLawrence Mitchell const PetscInt *intFacetsArray = NULL; 2318f98464cbSLawrence Mitchell if (!patch->allIntFacets) { 2319f98464cbSLawrence Mitchell PetscHSetI facets; 2320f98464cbSLawrence Mitchell PetscHashIter hi; 2321f98464cbSLawrence Mitchell PetscInt pStart, pEnd, fStart, fEnd; 2322f98464cbSLawrence Mitchell PetscInt *allIntFacets = NULL; 23239566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&facets)); 23249566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray)); 23259566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->intFacetCounts, &pStart, &pEnd)); 23269566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd)); 2327f98464cbSLawrence Mitchell for (i = pStart; i < pEnd; i++) { 23289566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->intFacetCounts, i, &nIntFacets)); 23299566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->intFacetCounts, i, &offset)); 2330f98464cbSLawrence Mitchell if (nIntFacets <= 0) continue; 2331f98464cbSLawrence Mitchell for (j = 0; j < nIntFacets; j++) { 23329566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(facets, intFacetsArray[offset + j])); 2333f98464cbSLawrence Mitchell } 2334f98464cbSLawrence Mitchell } 23359566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->intFacets, &intFacetsArray)); 23369566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(facets, &nIntFacets)); 23379566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nIntFacets, &allIntFacets)); 23389566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(fEnd-fStart, &patch->precomputedIntFacetTensorLocations)); 2339f98464cbSLawrence Mitchell i = 0; 2340f98464cbSLawrence Mitchell PetscHashIterBegin(facets, hi); 2341f98464cbSLawrence Mitchell while (!PetscHashIterAtEnd(facets, hi)) { 2342f98464cbSLawrence Mitchell PetscHashIterGetKey(facets, hi, allIntFacets[i]); 2343de2d1767SPatrick Farrell patch->precomputedIntFacetTensorLocations[allIntFacets[i] - fStart] = i; 2344f98464cbSLawrence Mitchell PetscHashIterNext(facets, hi); 2345f98464cbSLawrence Mitchell i++; 2346f98464cbSLawrence Mitchell } 23479566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&facets)); 23489566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, allIntFacets, PETSC_OWN_POINTER, &patch->allIntFacets)); 2349f98464cbSLawrence Mitchell } 23509566063dSJacob Faibussowitsch PetscCall(ISGetSize(patch->allIntFacets, &nIntFacets)); 2351f98464cbSLawrence Mitchell if (!patch->intFacetMats) { 23529566063dSJacob Faibussowitsch PetscCall(VecCreateSeq(PETSC_COMM_SELF, nIntFacets*ndof*ndof*4, &patch->intFacetMats)); 23539566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(patch->intFacetMats, ndof*2)); 2354f98464cbSLawrence Mitchell } 23559566063dSJacob Faibussowitsch PetscCall(VecSet(patch->intFacetMats, 0)); 2356f98464cbSLawrence Mitchell 2357d0609cedSBarry Smith PetscCall(MatCreateShell(PETSC_COMM_SELF, nIntFacets*ndof*2, nIntFacets*ndof*2, nIntFacets*ndof*2, nIntFacets*ndof*2,(void*)patch->intFacetMats, &vecMat)); 23589566063dSJacob Faibussowitsch PetscCall(MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void(*)(void))&MatSetValues_PCPatch_Private)); 23599566063dSJacob Faibussowitsch PetscCall(ISCreateStride(PETSC_COMM_SELF, 2*ndof*nIntFacets, 0, 1, &dofMap)); 23609566063dSJacob Faibussowitsch PetscCall(ISGetIndices(dofMap, &dofMapArray)); 23619566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->allIntFacets, &intFacetsArray)); 23629566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, intFacetsArray, PETSC_USE_POINTER, &intFacetsIS)); 2363f98464cbSLawrence Mitchell /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */ 2364792fecdfSBarry Smith PetscCallBack("PCPatch callback (interior facets)",patch->usercomputeopintfacet(pc, -1, NULL, vecMat, intFacetsIS, 2*ndof*nIntFacets, dofMapArray, NULL, patch->usercomputeopintfacetctx)); 23659566063dSJacob Faibussowitsch PetscCall(ISDestroy(&intFacetsIS)); 23669566063dSJacob Faibussowitsch PetscCall(MatDestroy(&vecMat)); 23679566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->allIntFacets, &intFacetsArray)); 23689566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(dofMap, &dofMapArray)); 23699566063dSJacob Faibussowitsch PetscCall(ISDestroy(&dofMap)); 2370f98464cbSLawrence Mitchell } 23719566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm)); 23729566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0)); 2373fa84ea4cSLawrence Mitchell 2374fa84ea4cSLawrence Mitchell PetscFunctionReturn(0); 2375fa84ea4cSLawrence Mitchell } 2376fa84ea4cSLawrence Mitchell 23770904074fSPatrick Farrell PetscErrorCode PCPatch_ScatterLocal_Private(PC pc, PetscInt p, Vec x, Vec y, InsertMode mode, ScatterMode scat, PatchScatterType scattertype) 23784bbf5ea8SMatthew G. Knepley { 23794bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 23804bbf5ea8SMatthew G. Knepley const PetscScalar *xArray = NULL; 23814bbf5ea8SMatthew G. Knepley PetscScalar *yArray = NULL; 23824bbf5ea8SMatthew G. Knepley const PetscInt *gtolArray = NULL; 23834bbf5ea8SMatthew G. Knepley PetscInt dof, offset, lidx; 23844bbf5ea8SMatthew G. Knepley 23854bbf5ea8SMatthew G. Knepley PetscFunctionBeginHot; 23869566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(x, &xArray)); 23879566063dSJacob Faibussowitsch PetscCall(VecGetArray(y, &yArray)); 23880904074fSPatrick Farrell if (scattertype == SCATTER_WITHARTIFICIAL) { 23899566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof)); 23909566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offset)); 23919566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->gtolWithArtificial, >olArray)); 23920904074fSPatrick Farrell } else if (scattertype == SCATTER_WITHALL) { 23939566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCountsWithAll, p, &dof)); 23949566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offset)); 23959566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->gtolWithAll, >olArray)); 2396c2e6f3c0SFlorian Wechsung } else { 23979566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &dof)); 23989566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset)); 23999566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->gtol, >olArray)); 2400c2e6f3c0SFlorian Wechsung } 24012472a847SBarry Smith PetscCheck(mode != INSERT_VALUES || scat == SCATTER_FORWARD,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't insert if not scattering forward"); 24022472a847SBarry Smith PetscCheck(mode != ADD_VALUES || scat == SCATTER_REVERSE,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't add if not scattering reverse"); 24034bbf5ea8SMatthew G. Knepley for (lidx = 0; lidx < dof; ++lidx) { 24044bbf5ea8SMatthew G. Knepley const PetscInt gidx = gtolArray[offset+lidx]; 24054bbf5ea8SMatthew G. Knepley 24064bbf5ea8SMatthew G. Knepley if (mode == INSERT_VALUES) yArray[lidx] = xArray[gidx]; /* Forward */ 24074bbf5ea8SMatthew G. Knepley else yArray[gidx] += xArray[lidx]; /* Reverse */ 24084bbf5ea8SMatthew G. Knepley } 24090904074fSPatrick Farrell if (scattertype == SCATTER_WITHARTIFICIAL) { 24109566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->gtolWithArtificial, >olArray)); 24110904074fSPatrick Farrell } else if (scattertype == SCATTER_WITHALL) { 24129566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->gtolWithAll, >olArray)); 2413c2e6f3c0SFlorian Wechsung } else { 24149566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->gtol, >olArray)); 2415c2e6f3c0SFlorian Wechsung } 24169566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(x, &xArray)); 24179566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(y, &yArray)); 24184bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 24194bbf5ea8SMatthew G. Knepley } 24204bbf5ea8SMatthew G. Knepley 2421dadc69c5SMatthew G. Knepley static PetscErrorCode PCSetUp_PATCH_Linear(PC pc) 2422dadc69c5SMatthew G. Knepley { 2423dadc69c5SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 2424dadc69c5SMatthew G. Knepley const char *prefix; 2425dadc69c5SMatthew G. Knepley PetscInt i; 2426dadc69c5SMatthew G. Knepley 2427dadc69c5SMatthew G. Knepley PetscFunctionBegin; 2428dadc69c5SMatthew G. Knepley if (!pc->setupcalled) { 24297827d75bSBarry Smith PetscCheck(patch->save_operators || !patch->denseinverse,PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Can't have dense inverse without save operators"); 2430c73d2cf6SLawrence Mitchell if (!patch->denseinverse) { 24319566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(patch->npatch, &patch->solver)); 24329566063dSJacob Faibussowitsch PetscCall(PCGetOptionsPrefix(pc, &prefix)); 2433dadc69c5SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) { 2434dadc69c5SMatthew G. Knepley KSP ksp; 2435dadc69c5SMatthew G. Knepley PC subpc; 2436dadc69c5SMatthew G. Knepley 24379566063dSJacob Faibussowitsch PetscCall(KSPCreate(PETSC_COMM_SELF, &ksp)); 24389566063dSJacob Faibussowitsch PetscCall(KSPSetErrorIfNotConverged(ksp, pc->erroriffailure)); 24399566063dSJacob Faibussowitsch PetscCall(KSPSetOptionsPrefix(ksp, prefix)); 24409566063dSJacob Faibussowitsch PetscCall(KSPAppendOptionsPrefix(ksp, "sub_")); 24419566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject) ksp, (PetscObject) pc, 1)); 24429566063dSJacob Faibussowitsch PetscCall(KSPGetPC(ksp, &subpc)); 24439566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject) subpc, (PetscObject) pc, 1)); 24449566063dSJacob Faibussowitsch PetscCall(PetscLogObjectParent((PetscObject) pc, (PetscObject) ksp)); 2445dadc69c5SMatthew G. Knepley patch->solver[i] = (PetscObject) ksp; 2446dadc69c5SMatthew G. Knepley } 2447dadc69c5SMatthew G. Knepley } 2448c73d2cf6SLawrence Mitchell } 2449dadc69c5SMatthew G. Knepley if (patch->save_operators) { 24501baa6e33SBarry Smith if (patch->precomputeElementTensors) PetscCall(PCPatchPrecomputePatchTensors_Private(pc)); 2451dadc69c5SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) { 24529566063dSJacob Faibussowitsch PetscCall(PCPatchComputeOperator_Internal(pc, NULL, patch->mat[i], i, PETSC_FALSE)); 2453c73d2cf6SLawrence Mitchell if (!patch->denseinverse) { 24549566063dSJacob Faibussowitsch PetscCall(KSPSetOperators((KSP) patch->solver[i], patch->mat[i], patch->mat[i])); 24559d4fc724SLawrence Mitchell } else if (patch->mat[i] && !patch->densesolve) { 24569d4fc724SLawrence Mitchell /* Setup matmult callback */ 24579566063dSJacob Faibussowitsch PetscCall(MatGetOperation(patch->mat[i], MATOP_MULT, (void (**)(void))&patch->densesolve)); 2458dadc69c5SMatthew G. Knepley } 2459dadc69c5SMatthew G. Knepley } 2460c73d2cf6SLawrence Mitchell } 246134d8b122SPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 246234d8b122SPatrick Farrell for (i = 0; i < patch->npatch; ++i) { 24631202d238SPatrick Farrell /* Instead of padding patch->patchUpdate with zeros to get */ 24641202d238SPatrick Farrell /* patch->patchUpdateWithArtificial and then multiplying with the matrix, */ 246534d8b122SPatrick Farrell /* just get rid of the columns that correspond to the dofs with */ 246634d8b122SPatrick Farrell /* artificial bcs. That's of course fairly inefficient, hopefully we */ 246734d8b122SPatrick Farrell /* can just assemble the rectangular matrix in the first place. */ 246834d8b122SPatrick Farrell Mat matSquare; 246934d8b122SPatrick Farrell IS rowis; 247034d8b122SPatrick Farrell PetscInt dof; 247134d8b122SPatrick Farrell 24729566063dSJacob Faibussowitsch PetscCall(MatGetSize(patch->mat[i], &dof, NULL)); 247334d8b122SPatrick Farrell if (dof == 0) { 247434d8b122SPatrick Farrell patch->matWithArtificial[i] = NULL; 247534d8b122SPatrick Farrell continue; 247634d8b122SPatrick Farrell } 247734d8b122SPatrick Farrell 24789566063dSJacob Faibussowitsch PetscCall(PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE)); 24799566063dSJacob Faibussowitsch PetscCall(PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE)); 248034d8b122SPatrick Farrell 24819566063dSJacob Faibussowitsch PetscCall(MatGetSize(matSquare, &dof, NULL)); 24829566063dSJacob Faibussowitsch PetscCall(ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis)); 248334d8b122SPatrick Farrell if (pc->setupcalled) { 24849566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_REUSE_MATRIX, &patch->matWithArtificial[i])); 248534d8b122SPatrick Farrell } else { 24869566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &patch->matWithArtificial[i])); 248734d8b122SPatrick Farrell } 24889566063dSJacob Faibussowitsch PetscCall(ISDestroy(&rowis)); 24899566063dSJacob Faibussowitsch PetscCall(MatDestroy(&matSquare)); 249034d8b122SPatrick Farrell } 249134d8b122SPatrick Farrell } 2492dadc69c5SMatthew G. Knepley PetscFunctionReturn(0); 2493dadc69c5SMatthew G. Knepley } 2494dadc69c5SMatthew G. Knepley 24954bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUp_PATCH(PC pc) 24964bbf5ea8SMatthew G. Knepley { 24974bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 2498557beb66SLawrence Mitchell PetscInt i; 249939fd2e8aSPatrick Farrell PetscBool isNonlinear; 25009d4fc724SLawrence Mitchell PetscInt maxDof = -1, maxDofWithArtificial = -1; 25014bbf5ea8SMatthew G. Knepley 25024bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 25034bbf5ea8SMatthew G. Knepley if (!pc->setupcalled) { 25044bbf5ea8SMatthew G. Knepley PetscInt pStart, pEnd, p; 25054bbf5ea8SMatthew G. Knepley PetscInt localSize; 25064bbf5ea8SMatthew G. Knepley 25079566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_CreatePatches, pc, 0, 0, 0)); 25084bbf5ea8SMatthew G. Knepley 2509debbdec3SPatrick Farrell isNonlinear = patch->isNonlinear; 25105f824522SMatthew G. Knepley if (!patch->nsubspaces) { 2511b6bb21d1SLawrence Mitchell DM dm, plex; 25125f824522SMatthew G. Knepley PetscSection s; 2513bd026e97SJed Brown PetscInt cStart, cEnd, c, Nf, f, numGlobalBcs = 0, *globalBcs, *Nb, **cellDofs; 25145f824522SMatthew G. Knepley 25159566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm)); 251628b400f6SJacob Faibussowitsch PetscCheck(dm,PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONG, "Must set DM for PCPATCH or call PCPatchSetDiscretisationInfo()"); 25179566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex)); 2518b6bb21d1SLawrence Mitchell dm = plex; 25199566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, &s)); 25209566063dSJacob Faibussowitsch PetscCall(PetscSectionGetNumFields(s, &Nf)); 25219566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(s, &pStart, &pEnd)); 25225f824522SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 25235f824522SMatthew G. Knepley PetscInt cdof; 25249566063dSJacob Faibussowitsch PetscCall(PetscSectionGetConstraintDof(s, p, &cdof)); 25255f824522SMatthew G. Knepley numGlobalBcs += cdof; 25265f824522SMatthew G. Knepley } 25279566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 25289566063dSJacob Faibussowitsch PetscCall(PetscMalloc3(Nf, &Nb, Nf, &cellDofs, numGlobalBcs, &globalBcs)); 25295f824522SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 25305f824522SMatthew G. Knepley PetscFE fe; 25315f824522SMatthew G. Knepley PetscDualSpace sp; 25325f824522SMatthew G. Knepley PetscInt cdoff = 0; 25335f824522SMatthew G. Knepley 25349566063dSJacob Faibussowitsch PetscCall(DMGetField(dm, f, NULL, (PetscObject *) &fe)); 25359566063dSJacob Faibussowitsch /* PetscCall(PetscFEGetNumComponents(fe, &Nc[f])); */ 25369566063dSJacob Faibussowitsch PetscCall(PetscFEGetDualSpace(fe, &sp)); 25379566063dSJacob Faibussowitsch PetscCall(PetscDualSpaceGetDimension(sp, &Nb[f])); 25385f824522SMatthew G. Knepley 25399566063dSJacob Faibussowitsch PetscCall(PetscMalloc1((cEnd-cStart)*Nb[f], &cellDofs[f])); 25405f824522SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 25415f824522SMatthew G. Knepley PetscInt *closure = NULL; 25425f824522SMatthew G. Knepley PetscInt clSize = 0, cl; 25435f824522SMatthew G. Knepley 25449566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure)); 25455f824522SMatthew G. Knepley for (cl = 0; cl < clSize*2; cl += 2) { 25465f824522SMatthew G. Knepley const PetscInt p = closure[cl]; 25475f824522SMatthew G. Knepley PetscInt fdof, d, foff; 25485f824522SMatthew G. Knepley 25499566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldDof(s, p, f, &fdof)); 25509566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldOffset(s, p, f, &foff)); 25515f824522SMatthew G. Knepley for (d = 0; d < fdof; ++d, ++cdoff) cellDofs[f][cdoff] = foff + d; 25525f824522SMatthew G. Knepley } 25539566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure)); 25545f824522SMatthew G. Knepley } 255563a3b9bcSJacob 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]); 25565f824522SMatthew G. Knepley } 25575f824522SMatthew G. Knepley numGlobalBcs = 0; 25585f824522SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 25595f824522SMatthew G. Knepley const PetscInt *ind; 25605f824522SMatthew G. Knepley PetscInt off, cdof, d; 25615f824522SMatthew G. Knepley 25629566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(s, p, &off)); 25639566063dSJacob Faibussowitsch PetscCall(PetscSectionGetConstraintDof(s, p, &cdof)); 25649566063dSJacob Faibussowitsch PetscCall(PetscSectionGetConstraintIndices(s, p, &ind)); 25655f824522SMatthew G. Knepley for (d = 0; d < cdof; ++d) globalBcs[numGlobalBcs++] = off + ind[d]; 25665f824522SMatthew G. Knepley } 25675f824522SMatthew G. Knepley 25689566063dSJacob Faibussowitsch PetscCall(PCPatchSetDiscretisationInfoCombined(pc, dm, Nb, (const PetscInt **) cellDofs, numGlobalBcs, globalBcs, numGlobalBcs, globalBcs)); 25695f824522SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 25709566063dSJacob Faibussowitsch PetscCall(PetscFree(cellDofs[f])); 25715f824522SMatthew G. Knepley } 25729566063dSJacob Faibussowitsch PetscCall(PetscFree3(Nb, cellDofs, globalBcs)); 25739566063dSJacob Faibussowitsch PetscCall(PCPatchSetComputeFunction(pc, PCPatchComputeFunction_DMPlex_Private, NULL)); 25749566063dSJacob Faibussowitsch PetscCall(PCPatchSetComputeOperator(pc, PCPatchComputeOperator_DMPlex_Private, NULL)); 25759566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm)); 25765f824522SMatthew G. Knepley } 25775f824522SMatthew G. Knepley 25784bbf5ea8SMatthew G. Knepley localSize = patch->subspaceOffsets[patch->nsubspaces]; 25799566063dSJacob Faibussowitsch PetscCall(VecCreateSeq(PETSC_COMM_SELF, localSize, &patch->localRHS)); 25809566063dSJacob Faibussowitsch PetscCall(VecSetUp(patch->localRHS)); 25819566063dSJacob Faibussowitsch PetscCall(VecDuplicate(patch->localRHS, &patch->localUpdate)); 25829566063dSJacob Faibussowitsch PetscCall(PCPatchCreateCellPatches(pc)); 25839566063dSJacob Faibussowitsch PetscCall(PCPatchCreateCellPatchDiscretisationInfo(pc)); 25844bbf5ea8SMatthew G. Knepley 25854bbf5ea8SMatthew G. Knepley /* OK, now build the work vectors */ 25869566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, &pEnd)); 2587c2e6f3c0SFlorian Wechsung 258861c4b389SFlorian Wechsung if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 25899566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithArtificial)); 2590c2e6f3c0SFlorian Wechsung } 25910904074fSPatrick Farrell if (isNonlinear) { 25929566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithAll)); 25930904074fSPatrick Farrell } 25944bbf5ea8SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 25954bbf5ea8SMatthew G. Knepley PetscInt dof; 25964bbf5ea8SMatthew G. Knepley 25979566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &dof)); 25982f613bf5SBarry Smith maxDof = PetscMax(maxDof, dof); 25990904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 26003bb0e8f7SKarl Rupp const PetscInt *gtolArray, *gtolArrayWithArtificial = NULL; 26013bb0e8f7SKarl Rupp PetscInt numPatchDofs, offset; 26023bb0e8f7SKarl Rupp PetscInt numPatchDofsWithArtificial, offsetWithArtificial; 26033bb0e8f7SKarl Rupp PetscInt dofWithoutArtificialCounter = 0; 26043bb0e8f7SKarl Rupp PetscInt *patchWithoutArtificialToWithArtificialArray; 26053bb0e8f7SKarl Rupp 26069566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof)); 26079d4fc724SLawrence Mitchell maxDofWithArtificial = PetscMax(maxDofWithArtificial, dof); 2608c2e6f3c0SFlorian Wechsung 2609e047a90bSFlorian Wechsung /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */ 2610e047a90bSFlorian Wechsung /* the index in the patch with all dofs */ 26119566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->gtol, >olArray)); 261263deea8eSPatrick Farrell 26139566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs)); 261447aca4a6SPatrick Farrell if (numPatchDofs == 0) { 261547aca4a6SPatrick Farrell patch->dofMappingWithoutToWithArtificial[p-pStart] = NULL; 261647aca4a6SPatrick Farrell continue; 261747aca4a6SPatrick Farrell } 261863deea8eSPatrick Farrell 26199566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset)); 26209566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->gtolWithArtificial, >olArrayWithArtificial)); 26219566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &numPatchDofsWithArtificial)); 26229566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offsetWithArtificial)); 2623c2e6f3c0SFlorian Wechsung 26249566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPatchDofs, &patchWithoutArtificialToWithArtificialArray)); 2625b0c21b6aSKarl Rupp for (i=0; i<numPatchDofsWithArtificial; i++) { 2626e047a90bSFlorian Wechsung if (gtolArrayWithArtificial[i+offsetWithArtificial] == gtolArray[offset+dofWithoutArtificialCounter]) { 2627c2e6f3c0SFlorian Wechsung patchWithoutArtificialToWithArtificialArray[dofWithoutArtificialCounter] = i; 2628c2e6f3c0SFlorian Wechsung dofWithoutArtificialCounter++; 2629c2e6f3c0SFlorian Wechsung if (dofWithoutArtificialCounter == numPatchDofs) 2630c2e6f3c0SFlorian Wechsung break; 2631c2e6f3c0SFlorian Wechsung } 2632c2e6f3c0SFlorian Wechsung } 26339566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutArtificialToWithArtificialArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithArtificial[p-pStart])); 26349566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->gtol, >olArray)); 26359566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->gtolWithArtificial, >olArrayWithArtificial)); 2636c2e6f3c0SFlorian Wechsung } 26370904074fSPatrick Farrell if (isNonlinear) { 26380904074fSPatrick Farrell const PetscInt *gtolArray, *gtolArrayWithAll = NULL; 26390904074fSPatrick Farrell PetscInt numPatchDofs, offset; 26400904074fSPatrick Farrell PetscInt numPatchDofsWithAll, offsetWithAll; 26410904074fSPatrick Farrell PetscInt dofWithoutAllCounter = 0; 26420904074fSPatrick Farrell PetscInt *patchWithoutAllToWithAllArray; 26430904074fSPatrick Farrell 26440904074fSPatrick Farrell /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */ 26450904074fSPatrick Farrell /* the index in the patch with all dofs */ 26469566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->gtol, >olArray)); 26470904074fSPatrick Farrell 26489566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs)); 264947aca4a6SPatrick Farrell if (numPatchDofs == 0) { 2650b88cb22dSPatrick Farrell patch->dofMappingWithoutToWithAll[p-pStart] = NULL; 265147aca4a6SPatrick Farrell continue; 265247aca4a6SPatrick Farrell } 26530904074fSPatrick Farrell 26549566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset)); 26559566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->gtolWithAll, >olArrayWithAll)); 26569566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCountsWithAll, p, &numPatchDofsWithAll)); 26579566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offsetWithAll)); 26580904074fSPatrick Farrell 26599566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPatchDofs, &patchWithoutAllToWithAllArray)); 26600904074fSPatrick Farrell 26610904074fSPatrick Farrell for (i=0; i<numPatchDofsWithAll; i++) { 26620904074fSPatrick Farrell if (gtolArrayWithAll[i+offsetWithAll] == gtolArray[offset+dofWithoutAllCounter]) { 26630904074fSPatrick Farrell patchWithoutAllToWithAllArray[dofWithoutAllCounter] = i; 26640904074fSPatrick Farrell dofWithoutAllCounter++; 26650904074fSPatrick Farrell if (dofWithoutAllCounter == numPatchDofs) 26660904074fSPatrick Farrell break; 26670904074fSPatrick Farrell } 26680904074fSPatrick Farrell } 26699566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutAllToWithAllArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithAll[p-pStart])); 26709566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->gtol, >olArray)); 26719566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->gtolWithAll, >olArrayWithAll)); 26720904074fSPatrick Farrell } 26734bbf5ea8SMatthew G. Knepley } 267460dd46caSLawrence Mitchell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 26759566063dSJacob Faibussowitsch PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDofWithArtificial, &patch->patchRHSWithArtificial)); 26769566063dSJacob Faibussowitsch PetscCall(VecSetUp(patch->patchRHSWithArtificial)); 267760dd46caSLawrence Mitchell } 26789566063dSJacob Faibussowitsch PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDof, &patch->patchRHS)); 26799566063dSJacob Faibussowitsch PetscCall(VecSetUp(patch->patchRHS)); 26809566063dSJacob Faibussowitsch PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDof, &patch->patchUpdate)); 26819566063dSJacob Faibussowitsch PetscCall(VecSetUp(patch->patchUpdate)); 26824bbf5ea8SMatthew G. Knepley if (patch->save_operators) { 26839566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(patch->npatch, &patch->mat)); 26844bbf5ea8SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) { 26859566063dSJacob Faibussowitsch PetscCall(PCPatchCreateMatrix_Private(pc, i, &patch->mat[i], PETSC_FALSE)); 26864bbf5ea8SMatthew G. Knepley } 26874bbf5ea8SMatthew G. Knepley } 26889566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_CreatePatches, pc, 0, 0, 0)); 26894bbf5ea8SMatthew G. Knepley 26904bbf5ea8SMatthew G. Knepley /* If desired, calculate weights for dof multiplicity */ 26914bbf5ea8SMatthew G. Knepley if (patch->partition_of_unity) { 26923bb0e8f7SKarl Rupp PetscScalar *input = NULL; 26933bb0e8f7SKarl Rupp PetscScalar *output = NULL; 26943bb0e8f7SKarl Rupp Vec global; 26953bb0e8f7SKarl Rupp 26969566063dSJacob Faibussowitsch PetscCall(VecDuplicate(patch->localRHS, &patch->dof_weights)); 269761c4b389SFlorian Wechsung if (patch->local_composition_type == PC_COMPOSITE_ADDITIVE) { 26984bbf5ea8SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) { 26994bbf5ea8SMatthew G. Knepley PetscInt dof; 27004bbf5ea8SMatthew G. Knepley 27019566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCounts, i+pStart, &dof)); 27024bbf5ea8SMatthew G. Knepley if (dof <= 0) continue; 27039566063dSJacob Faibussowitsch PetscCall(VecSet(patch->patchRHS, 1.0)); 27049566063dSJacob Faibussowitsch PetscCall(PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchRHS, patch->dof_weights, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR)); 27054bbf5ea8SMatthew G. Knepley } 2706c2e6f3c0SFlorian Wechsung } else { 2707e047a90bSFlorian Wechsung /* multiplicative is actually only locally multiplicative and globally additive. need the pou where the mesh decomposition overlaps */ 27089566063dSJacob Faibussowitsch PetscCall(VecSet(patch->dof_weights, 1.0)); 27094bbf5ea8SMatthew G. Knepley } 2710d132cafaSFlorian Wechsung 2711d132cafaSFlorian Wechsung VecDuplicate(patch->dof_weights, &global); 2712d132cafaSFlorian Wechsung VecSet(global, 0.); 2713d132cafaSFlorian Wechsung 27149566063dSJacob Faibussowitsch PetscCall(VecGetArray(patch->dof_weights, &input)); 27159566063dSJacob Faibussowitsch PetscCall(VecGetArray(global, &output)); 27169566063dSJacob Faibussowitsch PetscCall(PetscSFReduceBegin(patch->sectionSF, MPIU_SCALAR, input, output, MPI_SUM)); 27179566063dSJacob Faibussowitsch PetscCall(PetscSFReduceEnd(patch->sectionSF, MPIU_SCALAR, input, output, MPI_SUM)); 27189566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(patch->dof_weights, &input)); 27199566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(global, &output)); 2720d132cafaSFlorian Wechsung 27219566063dSJacob Faibussowitsch PetscCall(VecReciprocal(global)); 2722d132cafaSFlorian Wechsung 27239566063dSJacob Faibussowitsch PetscCall(VecGetArray(patch->dof_weights, &output)); 27249566063dSJacob Faibussowitsch PetscCall(VecGetArray(global, &input)); 27259566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(patch->sectionSF, MPIU_SCALAR, input, output,MPI_REPLACE)); 27269566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(patch->sectionSF, MPIU_SCALAR, input, output,MPI_REPLACE)); 27279566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(patch->dof_weights, &output)); 27289566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(global, &input)); 27299566063dSJacob Faibussowitsch PetscCall(VecDestroy(&global)); 27304bbf5ea8SMatthew G. Knepley } 273161c4b389SFlorian Wechsung if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE && patch->save_operators) { 27329566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(patch->npatch, &patch->matWithArtificial)); 27334bbf5ea8SMatthew G. Knepley } 27344bbf5ea8SMatthew G. Knepley } 27359566063dSJacob Faibussowitsch PetscCall((*patch->setupsolver)(pc)); 2736dadc69c5SMatthew G. Knepley PetscFunctionReturn(0); 27374bbf5ea8SMatthew G. Knepley } 2738dadc69c5SMatthew G. Knepley 2739dadc69c5SMatthew G. Knepley static PetscErrorCode PCApply_PATCH_Linear(PC pc, PetscInt i, Vec x, Vec y) 2740dadc69c5SMatthew G. Knepley { 2741dadc69c5SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 2742c73d2cf6SLawrence Mitchell KSP ksp; 27439d4fc724SLawrence Mitchell Mat op; 27449d4fc724SLawrence Mitchell PetscInt m, n; 2745dadc69c5SMatthew G. Knepley 2746dadc69c5SMatthew G. Knepley PetscFunctionBegin; 2747c73d2cf6SLawrence Mitchell if (patch->denseinverse) { 27489566063dSJacob Faibussowitsch PetscCall((*patch->densesolve)(patch->mat[i], x, y)); 2749c73d2cf6SLawrence Mitchell PetscFunctionReturn(0); 2750c73d2cf6SLawrence Mitchell } 2751c73d2cf6SLawrence Mitchell ksp = (KSP) patch->solver[i]; 2752dadc69c5SMatthew G. Knepley if (!patch->save_operators) { 2753dadc69c5SMatthew G. Knepley Mat mat; 2754dadc69c5SMatthew G. Knepley 27559566063dSJacob Faibussowitsch PetscCall(PCPatchCreateMatrix_Private(pc, i, &mat, PETSC_FALSE)); 2756dadc69c5SMatthew G. Knepley /* Populate operator here. */ 27579566063dSJacob Faibussowitsch PetscCall(PCPatchComputeOperator_Internal(pc, NULL, mat, i, PETSC_FALSE)); 27589566063dSJacob Faibussowitsch PetscCall(KSPSetOperators(ksp, mat, mat)); 2759dadc69c5SMatthew G. Knepley /* Drop reference so the KSPSetOperators below will blow it away. */ 27609566063dSJacob Faibussowitsch PetscCall(MatDestroy(&mat)); 2761dadc69c5SMatthew G. Knepley } 27629566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0)); 2763dadc69c5SMatthew G. Knepley if (!ksp->setfromoptionscalled) { 27649566063dSJacob Faibussowitsch PetscCall(KSPSetFromOptions(ksp)); 2765dadc69c5SMatthew G. Knepley } 27669d4fc724SLawrence Mitchell /* Disgusting trick to reuse work vectors */ 27679566063dSJacob Faibussowitsch PetscCall(KSPGetOperators(ksp, &op, NULL)); 27689566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(op, &m, &n)); 27699d4fc724SLawrence Mitchell x->map->n = m; 27709d4fc724SLawrence Mitchell y->map->n = n; 27719d4fc724SLawrence Mitchell x->map->N = m; 27729d4fc724SLawrence Mitchell y->map->N = n; 27739566063dSJacob Faibussowitsch PetscCall(KSPSolve(ksp, x, y)); 27749566063dSJacob Faibussowitsch PetscCall(KSPCheckSolve(ksp, pc, y)); 27759566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0)); 2776dadc69c5SMatthew G. Knepley if (!patch->save_operators) { 2777dadc69c5SMatthew G. Knepley PC pc; 27789566063dSJacob Faibussowitsch PetscCall(KSPSetOperators(ksp, NULL, NULL)); 27799566063dSJacob Faibussowitsch PetscCall(KSPGetPC(ksp, &pc)); 2780dadc69c5SMatthew G. Knepley /* Destroy PC context too, otherwise the factored matrix hangs around. */ 27819566063dSJacob Faibussowitsch PetscCall(PCReset(pc)); 27824bbf5ea8SMatthew G. Knepley } 27834bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 27844bbf5ea8SMatthew G. Knepley } 27854bbf5ea8SMatthew G. Knepley 27866c9c532dSPatrick Farrell static PetscErrorCode PCUpdateMultiplicative_PATCH_Linear(PC pc, PetscInt i, PetscInt pStart) 27876c9c532dSPatrick Farrell { 27886c9c532dSPatrick Farrell PC_PATCH *patch = (PC_PATCH *) pc->data; 27896c9c532dSPatrick Farrell Mat multMat; 27909d4fc724SLawrence Mitchell PetscInt n, m; 27916c9c532dSPatrick Farrell 27924d04e9f1SPatrick Farrell PetscFunctionBegin; 27934d04e9f1SPatrick Farrell 27946c9c532dSPatrick Farrell if (patch->save_operators) { 27956c9c532dSPatrick Farrell multMat = patch->matWithArtificial[i]; 27966c9c532dSPatrick Farrell } else { 27976c9c532dSPatrick Farrell /*Very inefficient, hopefully we can just assemble the rectangular matrix in the first place.*/ 27986c9c532dSPatrick Farrell Mat matSquare; 27996c9c532dSPatrick Farrell PetscInt dof; 28006c9c532dSPatrick Farrell IS rowis; 28019566063dSJacob Faibussowitsch PetscCall(PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE)); 28029566063dSJacob Faibussowitsch PetscCall(PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE)); 28039566063dSJacob Faibussowitsch PetscCall(MatGetSize(matSquare, &dof, NULL)); 28049566063dSJacob Faibussowitsch PetscCall(ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis)); 28059566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &multMat)); 28069566063dSJacob Faibussowitsch PetscCall(MatDestroy(&matSquare)); 28079566063dSJacob Faibussowitsch PetscCall(ISDestroy(&rowis)); 28086c9c532dSPatrick Farrell } 28099d4fc724SLawrence Mitchell /* Disgusting trick to reuse work vectors */ 28109566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(multMat, &m, &n)); 28119d4fc724SLawrence Mitchell patch->patchUpdate->map->n = n; 28129d4fc724SLawrence Mitchell patch->patchRHSWithArtificial->map->n = m; 28139d4fc724SLawrence Mitchell patch->patchUpdate->map->N = n; 28149d4fc724SLawrence Mitchell patch->patchRHSWithArtificial->map->N = m; 28159566063dSJacob Faibussowitsch PetscCall(MatMult(multMat, patch->patchUpdate, patch->patchRHSWithArtificial)); 28169566063dSJacob Faibussowitsch PetscCall(VecScale(patch->patchRHSWithArtificial, -1.0)); 28179566063dSJacob Faibussowitsch PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHSWithArtificial, patch->localRHS, ADD_VALUES, SCATTER_REVERSE, SCATTER_WITHARTIFICIAL)); 28186c9c532dSPatrick Farrell if (!patch->save_operators) { 28199566063dSJacob Faibussowitsch PetscCall(MatDestroy(&multMat)); 28206c9c532dSPatrick Farrell } 28214d04e9f1SPatrick Farrell PetscFunctionReturn(0); 28226c9c532dSPatrick Farrell } 28236c9c532dSPatrick Farrell 28244bbf5ea8SMatthew G. Knepley static PetscErrorCode PCApply_PATCH(PC pc, Vec x, Vec y) 28254bbf5ea8SMatthew G. Knepley { 28264bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 28271202d238SPatrick Farrell const PetscScalar *globalRHS = NULL; 28281202d238SPatrick Farrell PetscScalar *localRHS = NULL; 28291202d238SPatrick Farrell PetscScalar *globalUpdate = NULL; 28304bbf5ea8SMatthew G. Knepley const PetscInt *bcNodes = NULL; 28314bbf5ea8SMatthew G. Knepley PetscInt nsweep = patch->symmetrise_sweep ? 2 : 1; 28324bbf5ea8SMatthew G. Knepley PetscInt start[2] = {0, 0}; 28334bbf5ea8SMatthew G. Knepley PetscInt end[2] = {-1, -1}; 28344bbf5ea8SMatthew G. Knepley const PetscInt inc[2] = {1, -1}; 28351202d238SPatrick Farrell const PetscScalar *localUpdate; 28364bbf5ea8SMatthew G. Knepley const PetscInt *iterationSet; 28374bbf5ea8SMatthew G. Knepley PetscInt pStart, numBcs, n, sweep, bc, j; 28384bbf5ea8SMatthew G. Knepley 28394bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 28409566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_Apply, pc, 0, 0, 0)); 28419566063dSJacob Faibussowitsch PetscCall(PetscOptionsPushGetViewerOff(PETSC_TRUE)); 284292d50984SMatthew G. Knepley /* start, end, inc have 2 entries to manage a second backward sweep if we symmetrize */ 28434bbf5ea8SMatthew G. Knepley end[0] = patch->npatch; 28444bbf5ea8SMatthew G. Knepley start[1] = patch->npatch-1; 28454bbf5ea8SMatthew G. Knepley if (patch->user_patches) { 28469566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(patch->iterationSet, &end[0])); 28474bbf5ea8SMatthew G. Knepley start[1] = end[0] - 1; 28489566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->iterationSet, &iterationSet)); 28494bbf5ea8SMatthew G. Knepley } 28504bbf5ea8SMatthew G. Knepley /* Scatter from global space into overlapped local spaces */ 28519566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(x, &globalRHS)); 28529566063dSJacob Faibussowitsch PetscCall(VecGetArray(patch->localRHS, &localRHS)); 28539566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(patch->sectionSF, MPIU_SCALAR, globalRHS, localRHS,MPI_REPLACE)); 28549566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(patch->sectionSF, MPIU_SCALAR, globalRHS, localRHS,MPI_REPLACE)); 28559566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(x, &globalRHS)); 28569566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(patch->localRHS, &localRHS)); 28574bbf5ea8SMatthew G. Knepley 28589566063dSJacob Faibussowitsch PetscCall(VecSet(patch->localUpdate, 0.0)); 28599566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, NULL)); 28609566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0)); 28614bbf5ea8SMatthew G. Knepley for (sweep = 0; sweep < nsweep; sweep++) { 28624bbf5ea8SMatthew G. Knepley for (j = start[sweep]; j*inc[sweep] < end[sweep]*inc[sweep]; j += inc[sweep]) { 28634bbf5ea8SMatthew G. Knepley PetscInt i = patch->user_patches ? iterationSet[j] : j; 28644bbf5ea8SMatthew G. Knepley PetscInt start, len; 28654bbf5ea8SMatthew G. Knepley 28669566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCounts, i+pStart, &len)); 28679566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCounts, i+pStart, &start)); 28684bbf5ea8SMatthew G. Knepley /* TODO: Squash out these guys in the setup as well. */ 28694bbf5ea8SMatthew G. Knepley if (len <= 0) continue; 28704bbf5ea8SMatthew G. Knepley /* TODO: Do we need different scatters for X and Y? */ 28719566063dSJacob Faibussowitsch PetscCall(PCPatch_ScatterLocal_Private(pc, i+pStart, patch->localRHS, patch->patchRHS, INSERT_VALUES, SCATTER_FORWARD, SCATTER_INTERIOR)); 28729566063dSJacob Faibussowitsch PetscCall((*patch->applysolver)(pc, i, patch->patchRHS, patch->patchUpdate)); 28739566063dSJacob Faibussowitsch PetscCall(PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchUpdate, patch->localUpdate, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR)); 287461c4b389SFlorian Wechsung if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 28759566063dSJacob Faibussowitsch PetscCall((*patch->updatemultiplicative)(pc, i, pStart)); 2876c2e6f3c0SFlorian Wechsung } 28774bbf5ea8SMatthew G. Knepley } 28784bbf5ea8SMatthew G. Knepley } 28799566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0)); 28809566063dSJacob Faibussowitsch if (patch->user_patches) PetscCall(ISRestoreIndices(patch->iterationSet, &iterationSet)); 28814bbf5ea8SMatthew G. Knepley /* XXX: should we do this on the global vector? */ 28821baa6e33SBarry Smith if (patch->partition_of_unity) PetscCall(VecPointwiseMult(patch->localUpdate, patch->localUpdate, patch->dof_weights)); 28831202d238SPatrick Farrell /* Now patch->localUpdate contains the solution of the patch solves, so we need to combine them all. */ 28849566063dSJacob Faibussowitsch PetscCall(VecSet(y, 0.0)); 28859566063dSJacob Faibussowitsch PetscCall(VecGetArray(y, &globalUpdate)); 28869566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(patch->localUpdate, &localUpdate)); 28879566063dSJacob Faibussowitsch PetscCall(PetscSFReduceBegin(patch->sectionSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM)); 28889566063dSJacob Faibussowitsch PetscCall(PetscSFReduceEnd(patch->sectionSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM)); 28899566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(patch->localUpdate, &localUpdate)); 28904bbf5ea8SMatthew G. Knepley 28914bbf5ea8SMatthew G. Knepley /* Now we need to send the global BC values through */ 28929566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(x, &globalRHS)); 28939566063dSJacob Faibussowitsch PetscCall(ISGetSize(patch->globalBcNodes, &numBcs)); 28949566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->globalBcNodes, &bcNodes)); 28959566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(x, &n)); 28964bbf5ea8SMatthew G. Knepley for (bc = 0; bc < numBcs; ++bc) { 28974bbf5ea8SMatthew G. Knepley const PetscInt idx = bcNodes[bc]; 28981202d238SPatrick Farrell if (idx < n) globalUpdate[idx] = globalRHS[idx]; 28994bbf5ea8SMatthew G. Knepley } 29004bbf5ea8SMatthew G. Knepley 29019566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->globalBcNodes, &bcNodes)); 29029566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(x, &globalRHS)); 29039566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(y, &globalUpdate)); 29044bbf5ea8SMatthew G. Knepley 29059566063dSJacob Faibussowitsch PetscCall(PetscOptionsPopGetViewerOff()); 29069566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_Apply, pc, 0, 0, 0)); 29074bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 29084bbf5ea8SMatthew G. Knepley } 29094bbf5ea8SMatthew G. Knepley 2910dadc69c5SMatthew G. Knepley static PetscErrorCode PCReset_PATCH_Linear(PC pc) 2911dadc69c5SMatthew G. Knepley { 2912dadc69c5SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 2913dadc69c5SMatthew G. Knepley PetscInt i; 2914dadc69c5SMatthew G. Knepley 2915dadc69c5SMatthew G. Knepley PetscFunctionBegin; 2916dadc69c5SMatthew G. Knepley if (patch->solver) { 29179566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(KSPReset((KSP) patch->solver[i])); 2918dadc69c5SMatthew G. Knepley } 2919dadc69c5SMatthew G. Knepley PetscFunctionReturn(0); 2920dadc69c5SMatthew G. Knepley } 2921dadc69c5SMatthew G. Knepley 29224bbf5ea8SMatthew G. Knepley static PetscErrorCode PCReset_PATCH(PC pc) 29234bbf5ea8SMatthew G. Knepley { 29244bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 29254bbf5ea8SMatthew G. Knepley PetscInt i; 29264bbf5ea8SMatthew G. Knepley 29274bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 2928fa84ea4cSLawrence Mitchell 29299566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&patch->sectionSF)); 29309566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->cellCounts)); 29319566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->pointCounts)); 29329566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->cellNumbering)); 29339566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->gtolCounts)); 29349566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->gtol)); 29359566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->cells)); 29369566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->points)); 29379566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->dofs)); 29389566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->offs)); 29399566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->patchSection)); 29409566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->ghostBcNodes)); 29419566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->globalBcNodes)); 29429566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->gtolCountsWithArtificial)); 29439566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->gtolWithArtificial)); 29449566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->dofsWithArtificial)); 29459566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->offsWithArtificial)); 29469566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->gtolCountsWithAll)); 29479566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->gtolWithAll)); 29489566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->dofsWithAll)); 29499566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->offsWithAll)); 29509566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->cellMats)); 29519566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->intFacetMats)); 29529566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->allCells)); 29539566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->intFacets)); 29549566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->extFacets)); 29559566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->intFacetsToPatchCell)); 29569566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->intFacetCounts)); 29579566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->extFacetCounts)); 29584bbf5ea8SMatthew G. Knepley 29599566063dSJacob Faibussowitsch if (patch->dofSection) for (i = 0; i < patch->nsubspaces; i++) PetscCall(PetscSectionDestroy(&patch->dofSection[i])); 29609566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->dofSection)); 29619566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->bs)); 29629566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->nodesPerCell)); 29639566063dSJacob Faibussowitsch if (patch->cellNodeMap) for (i = 0; i < patch->nsubspaces; i++) PetscCall(PetscFree(patch->cellNodeMap[i])); 29649566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->cellNodeMap)); 29659566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->subspaceOffsets)); 29664bbf5ea8SMatthew G. Knepley 29679566063dSJacob Faibussowitsch PetscCall((*patch->resetsolver)(pc)); 29684bbf5ea8SMatthew G. Knepley 2969e4c66b91SPatrick Farrell if (patch->subspaces_to_exclude) { 29709566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&patch->subspaces_to_exclude)); 2971e4c66b91SPatrick Farrell } 2972e4c66b91SPatrick Farrell 29739566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->localRHS)); 29749566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->localUpdate)); 29759566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->patchRHS)); 29769566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->patchUpdate)); 29779566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->dof_weights)); 29784bbf5ea8SMatthew G. Knepley if (patch->patch_dof_weights) { 29799566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(VecDestroy(&patch->patch_dof_weights[i])); 29809566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->patch_dof_weights)); 29814bbf5ea8SMatthew G. Knepley } 29824bbf5ea8SMatthew G. Knepley if (patch->mat) { 29839566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(MatDestroy(&patch->mat[i])); 29849566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->mat)); 29855f824522SMatthew G. Knepley } 298611ac8bb0SFlorian Wechsung if (patch->matWithArtificial) { 29879566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(MatDestroy(&patch->matWithArtificial[i])); 29889566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->matWithArtificial)); 298911ac8bb0SFlorian Wechsung } 29909566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->patchRHSWithArtificial)); 299196b79ebeSFlorian Wechsung if (patch->dofMappingWithoutToWithArtificial) { 29929566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->dofMappingWithoutToWithArtificial[i])); 29939566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->dofMappingWithoutToWithArtificial)); 299496b79ebeSFlorian Wechsung 299596b79ebeSFlorian Wechsung } 29960904074fSPatrick Farrell if (patch->dofMappingWithoutToWithAll) { 29979566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->dofMappingWithoutToWithAll[i])); 29989566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->dofMappingWithoutToWithAll)); 29990904074fSPatrick Farrell 30000904074fSPatrick Farrell } 30019566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->sub_mat_type)); 30025f824522SMatthew G. Knepley if (patch->userIS) { 30039566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->userIS[i])); 30049566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->userIS)); 30055f824522SMatthew G. Knepley } 30069566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->precomputedTensorLocations)); 30079566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->precomputedIntFacetTensorLocations)); 3008f98464cbSLawrence Mitchell 30090a545947SLisandro Dalcin patch->bs = NULL; 30104bbf5ea8SMatthew G. Knepley patch->cellNodeMap = NULL; 30117974b488SMatthew G. Knepley patch->nsubspaces = 0; 30129566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->iterationSet)); 30135f824522SMatthew G. Knepley 30149566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&patch->viewerSection)); 30154bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 30164bbf5ea8SMatthew G. Knepley } 30174bbf5ea8SMatthew G. Knepley 3018dadc69c5SMatthew G. Knepley static PetscErrorCode PCDestroy_PATCH_Linear(PC pc) 30194bbf5ea8SMatthew G. Knepley { 30204bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 30214bbf5ea8SMatthew G. Knepley PetscInt i; 30224bbf5ea8SMatthew G. Knepley 30234bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 3024dadc69c5SMatthew G. Knepley if (patch->solver) { 30259566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(KSPDestroy((KSP *) &patch->solver[i])); 30269566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->solver)); 30274bbf5ea8SMatthew G. Knepley } 3028dadc69c5SMatthew G. Knepley PetscFunctionReturn(0); 3029dadc69c5SMatthew G. Knepley } 3030dadc69c5SMatthew G. Knepley 3031dadc69c5SMatthew G. Knepley static PetscErrorCode PCDestroy_PATCH(PC pc) 3032dadc69c5SMatthew G. Knepley { 3033dadc69c5SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 3034dadc69c5SMatthew G. Knepley 3035dadc69c5SMatthew G. Knepley PetscFunctionBegin; 30369566063dSJacob Faibussowitsch PetscCall(PCReset_PATCH(pc)); 30379566063dSJacob Faibussowitsch PetscCall((*patch->destroysolver)(pc)); 30389566063dSJacob Faibussowitsch PetscCall(PetscFree(pc->data)); 30394bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 30404bbf5ea8SMatthew G. Knepley } 30414bbf5ea8SMatthew G. Knepley 30424bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetFromOptions_PATCH(PetscOptionItems *PetscOptionsObject, PC pc) 30434bbf5ea8SMatthew G. Knepley { 30444bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 30454bbf5ea8SMatthew G. Knepley PCPatchConstructType patchConstructionType = PC_PATCH_STAR; 30465f824522SMatthew G. Knepley char sub_mat_type[PETSC_MAX_PATH_LEN]; 304710534d48SPatrick Farrell char option[PETSC_MAX_PATH_LEN]; 30485f824522SMatthew G. Knepley const char *prefix; 30494bbf5ea8SMatthew G. Knepley PetscBool flg, dimflg, codimflg; 30505f824522SMatthew G. Knepley MPI_Comm comm; 3051a48c39c8SPatrick Farrell PetscInt *ifields, nfields, k; 305261c4b389SFlorian Wechsung PCCompositeType loctype = PC_COMPOSITE_ADDITIVE; 30534bbf5ea8SMatthew G. Knepley 30544bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 30559566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject) pc, &comm)); 30569566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject) pc, &prefix)); 3057d0609cedSBarry Smith PetscOptionsHeadBegin(PetscOptionsObject, "Patch solver options"); 305810534d48SPatrick Farrell 30599566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_save_operators", patch->classname)); 30609566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool(option, "Store all patch operators for lifetime of object?", "PCPatchSetSaveOperators", patch->save_operators, &patch->save_operators, &flg)); 306110534d48SPatrick Farrell 30629566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_precompute_element_tensors", patch->classname)); 30639566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool(option, "Compute each element tensor only once?", "PCPatchSetPrecomputeElementTensors", patch->precomputeElementTensors, &patch->precomputeElementTensors, &flg)); 30649566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_partition_of_unity", patch->classname)); 30659566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool(option, "Weight contributions by dof multiplicity?", "PCPatchSetPartitionOfUnity", patch->partition_of_unity, &patch->partition_of_unity, &flg)); 306610534d48SPatrick Farrell 30679566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_local_type", patch->classname)); 30689566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum(option,"Type of local solver composition (additive or multiplicative)","PCPatchSetLocalComposition",PCCompositeTypes,(PetscEnum)loctype,(PetscEnum*)&loctype,&flg)); 30699566063dSJacob Faibussowitsch if (flg) PetscCall(PCPatchSetLocalComposition(pc, loctype)); 30709566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_dense_inverse", patch->classname)); 30719566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool(option, "Compute inverses of patch matrices and apply directly? Ignores KSP/PC settings on patch.", "PCPatchSetDenseInverse", patch->denseinverse, &patch->denseinverse, &flg)); 30729566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_dim", patch->classname)); 30739566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt(option, "What dimension of mesh point to construct patches by? (0 = vertices)", "PCPATCH", patch->dim, &patch->dim, &dimflg)); 30749566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_codim", patch->classname)); 30759566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt(option, "What co-dimension of mesh point to construct patches by? (0 = cells)", "PCPATCH", patch->codim, &patch->codim, &codimflg)); 307608401ef6SPierre Jolivet PetscCheck(!dimflg || !codimflg,comm, PETSC_ERR_ARG_WRONG, "Can only set one of dimension or co-dimension"); 307710534d48SPatrick Farrell 30789566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_type", patch->classname)); 30799566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum(option, "How should the patches be constructed?", "PCPatchSetConstructType", PCPatchConstructTypes, (PetscEnum) patchConstructionType, (PetscEnum *) &patchConstructionType, &flg)); 30809566063dSJacob Faibussowitsch if (flg) PetscCall(PCPatchSetConstructType(pc, patchConstructionType, NULL, NULL)); 308110534d48SPatrick Farrell 30829566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_vanka_dim", patch->classname)); 30839566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt(option, "Topological dimension of entities for Vanka to ignore", "PCPATCH", patch->vankadim, &patch->vankadim, &flg)); 308410534d48SPatrick Farrell 30859566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_ignore_dim", patch->classname)); 30869566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt(option, "Topological dimension of entities for completion to ignore", "PCPATCH", patch->ignoredim, &patch->ignoredim, &flg)); 308710534d48SPatrick Farrell 30889566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_pardecomp_overlap", patch->classname)); 30899566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt(option, "What overlap should we use in construct type pardecomp?", "PCPATCH", patch->pardecomp_overlap, &patch->pardecomp_overlap, &flg)); 3090b525f888SPatrick Farrell 30919566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_sub_mat_type", patch->classname)); 30929566063dSJacob Faibussowitsch PetscCall(PetscOptionsFList(option, "Matrix type for patch solves", "PCPatchSetSubMatType", MatList, NULL, sub_mat_type, PETSC_MAX_PATH_LEN, &flg)); 30939566063dSJacob Faibussowitsch if (flg) PetscCall(PCPatchSetSubMatType(pc, sub_mat_type)); 309410534d48SPatrick Farrell 30959566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_symmetrise_sweep", patch->classname)); 30969566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool(option, "Go start->end, end->start?", "PCPATCH", patch->symmetrise_sweep, &patch->symmetrise_sweep, &flg)); 3097e4c66b91SPatrick Farrell 3098a48c39c8SPatrick Farrell /* If the user has set the number of subspaces, use that for the buffer size, 3099a48c39c8SPatrick Farrell otherwise use a large number */ 3100a48c39c8SPatrick Farrell if (patch->nsubspaces <= 0) { 3101a48c39c8SPatrick Farrell nfields = 128; 3102a48c39c8SPatrick Farrell } else { 3103a48c39c8SPatrick Farrell nfields = patch->nsubspaces; 3104a48c39c8SPatrick Farrell } 31059566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nfields, &ifields)); 31069566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exclude_subspaces", patch->classname)); 31079566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetIntArray(((PetscObject)pc)->options,((PetscObject)pc)->prefix,option,ifields,&nfields,&flg)); 310808401ef6SPierre 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"); 3109e4c66b91SPatrick Farrell if (flg) { 31109566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(patch->subspaces_to_exclude)); 311159b66c28SPatrick Farrell for (k = 0; k < nfields; k++) { 31129566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(patch->subspaces_to_exclude, ifields[k])); 3113e4c66b91SPatrick Farrell } 3114e4c66b91SPatrick Farrell } 31159566063dSJacob Faibussowitsch PetscCall(PetscFree(ifields)); 31165f824522SMatthew G. Knepley 31179566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_patches_view", patch->classname)); 31189566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool(option, "Print out information during patch construction", "PCPATCH", patch->viewPatches, &patch->viewPatches, &flg)); 31199566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_cells_view", patch->classname)); 31209566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerCells, &patch->formatCells, &patch->viewCells)); 31219566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_interior_facets_view", patch->classname)); 31229566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerIntFacets, &patch->formatIntFacets, &patch->viewIntFacets)); 31239566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exterior_facets_view", patch->classname)); 31249566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerExtFacets, &patch->formatExtFacets, &patch->viewExtFacets)); 31259566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_points_view", patch->classname)); 31269566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerPoints, &patch->formatPoints, &patch->viewPoints)); 31279566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_section_view", patch->classname)); 31289566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerSection, &patch->formatSection, &patch->viewSection)); 31299566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_mat_view", patch->classname)); 31309566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerMatrix, &patch->formatMatrix, &patch->viewMatrix)); 3131d0609cedSBarry Smith PetscOptionsHeadEnd(); 31325f824522SMatthew G. Knepley patch->optionsSet = PETSC_TRUE; 31334bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 31344bbf5ea8SMatthew G. Knepley } 31354bbf5ea8SMatthew G. Knepley 31364bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUpOnBlocks_PATCH(PC pc) 31374bbf5ea8SMatthew G. Knepley { 31384bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH*) pc->data; 31394bbf5ea8SMatthew G. Knepley KSPConvergedReason reason; 31404bbf5ea8SMatthew G. Knepley PetscInt i; 31414bbf5ea8SMatthew G. Knepley 31424bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 3143a1eac568SLawrence Mitchell if (!patch->save_operators) { 3144a1eac568SLawrence Mitchell /* Can't do this here because the sub KSPs don't have an operator attached yet. */ 3145a1eac568SLawrence Mitchell PetscFunctionReturn(0); 3146a1eac568SLawrence Mitchell } 3147c73d2cf6SLawrence Mitchell if (patch->denseinverse) { 3148c73d2cf6SLawrence Mitchell /* No solvers */ 3149c73d2cf6SLawrence Mitchell PetscFunctionReturn(0); 3150c73d2cf6SLawrence Mitchell } 31514bbf5ea8SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) { 3152dadc69c5SMatthew G. Knepley if (!((KSP) patch->solver[i])->setfromoptionscalled) { 31539566063dSJacob Faibussowitsch PetscCall(KSPSetFromOptions((KSP) patch->solver[i])); 3154a1eac568SLawrence Mitchell } 31559566063dSJacob Faibussowitsch PetscCall(KSPSetUp((KSP) patch->solver[i])); 31569566063dSJacob Faibussowitsch PetscCall(KSPGetConvergedReason((KSP) patch->solver[i], &reason)); 3157c0decd05SBarry Smith if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR; 31584bbf5ea8SMatthew G. Knepley } 31594bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 31604bbf5ea8SMatthew G. Knepley } 31614bbf5ea8SMatthew G. Knepley 31624bbf5ea8SMatthew G. Knepley static PetscErrorCode PCView_PATCH(PC pc, PetscViewer viewer) 31634bbf5ea8SMatthew G. Knepley { 31644bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *) pc->data; 31654bbf5ea8SMatthew G. Knepley PetscViewer sviewer; 31664bbf5ea8SMatthew G. Knepley PetscBool isascii; 31674bbf5ea8SMatthew G. Knepley PetscMPIInt rank; 31684bbf5ea8SMatthew G. Knepley 31694bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 31704bbf5ea8SMatthew G. Knepley /* TODO Redo tabbing with set tbas in new style */ 31719566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii)); 31724bbf5ea8SMatthew G. Knepley if (!isascii) PetscFunctionReturn(0); 31739566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) pc), &rank)); 31749566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 317563a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Subspace Correction preconditioner with %" PetscInt_FMT " patches\n", patch->npatch)); 317661c4b389SFlorian Wechsung if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) { 31779566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Schwarz type: multiplicative\n")); 3178e047a90bSFlorian Wechsung } else { 31799566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Schwarz type: additive\n")); 3180c2e6f3c0SFlorian Wechsung } 31819566063dSJacob Faibussowitsch if (patch->partition_of_unity) PetscCall(PetscViewerASCIIPrintf(viewer, "Weighting by partition of unity\n")); 31829566063dSJacob Faibussowitsch else PetscCall(PetscViewerASCIIPrintf(viewer, "Not weighting by partition of unity\n")); 31839566063dSJacob Faibussowitsch if (patch->symmetrise_sweep) PetscCall(PetscViewerASCIIPrintf(viewer, "Symmetrising sweep (start->end, then end->start)\n")); 31849566063dSJacob Faibussowitsch else PetscCall(PetscViewerASCIIPrintf(viewer, "Not symmetrising sweep\n")); 31859566063dSJacob Faibussowitsch if (!patch->precomputeElementTensors) PetscCall(PetscViewerASCIIPrintf(viewer, "Not precomputing element tensors (overlapping cells rebuilt in every patch assembly)\n")); 31869566063dSJacob Faibussowitsch else PetscCall(PetscViewerASCIIPrintf(viewer, "Precomputing element tensors (each cell assembled only once)\n")); 31879566063dSJacob Faibussowitsch if (!patch->save_operators) PetscCall(PetscViewerASCIIPrintf(viewer, "Not saving patch operators (rebuilt every PCApply)\n")); 31889566063dSJacob Faibussowitsch else PetscCall(PetscViewerASCIIPrintf(viewer, "Saving patch operators (rebuilt every PCSetUp)\n")); 31899566063dSJacob Faibussowitsch if (patch->patchconstructop == PCPatchConstruct_Star) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: star\n")); 31909566063dSJacob Faibussowitsch else if (patch->patchconstructop == PCPatchConstruct_Vanka) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: Vanka\n")); 31919566063dSJacob Faibussowitsch else if (patch->patchconstructop == PCPatchConstruct_User) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: user-specified\n")); 31929566063dSJacob Faibussowitsch else PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: unknown\n")); 31935d30859aSPatrick Farrell 3194c73d2cf6SLawrence Mitchell if (patch->denseinverse) { 31959566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Explicitly forming dense inverse and applying patch solver via MatMult.\n")); 3196c73d2cf6SLawrence Mitchell } else { 31975d30859aSPatrick Farrell if (patch->isNonlinear) { 31989566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "SNES on patches (all same):\n")); 31995d30859aSPatrick Farrell } else { 32009566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "KSP on patches (all same):\n")); 32015d30859aSPatrick Farrell } 3202dadc69c5SMatthew G. Knepley if (patch->solver) { 32039566063dSJacob Faibussowitsch PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer)); 3204dd400576SPatrick Sanan if (rank == 0) { 32059566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(sviewer)); 32069566063dSJacob Faibussowitsch PetscCall(PetscObjectView(patch->solver[0], sviewer)); 32079566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(sviewer)); 32084bbf5ea8SMatthew G. Knepley } 32099566063dSJacob Faibussowitsch PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer)); 32104bbf5ea8SMatthew G. Knepley } else { 32119566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 32129566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Solver not yet set.\n")); 32139566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 32144bbf5ea8SMatthew G. Knepley } 3215c73d2cf6SLawrence Mitchell } 32169566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 32174bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 32184bbf5ea8SMatthew G. Knepley } 32194bbf5ea8SMatthew G. Knepley 3220e5893cccSMatthew G. Knepley /*MC 322198ed095eSMatthew G. Knepley PCPATCH - A PC object that encapsulates flexible definition of blocks for overlapping and non-overlapping 322298ed095eSMatthew G. Knepley small block additive preconditioners. Block definition is based on topology from 3223e5893cccSMatthew G. Knepley a DM and equation numbering from a PetscSection. 3224e5893cccSMatthew G. Knepley 3225e5893cccSMatthew G. Knepley Options Database Keys: 3226e5893cccSMatthew G. Knepley + -pc_patch_cells_view - Views the process local cell numbers for each patch 3227e5893cccSMatthew G. Knepley . -pc_patch_points_view - Views the process local mesh point numbers for each patch 3228e5893cccSMatthew G. Knepley . -pc_patch_g2l_view - Views the map between global dofs and patch local dofs for each patch 3229e5893cccSMatthew G. Knepley . -pc_patch_patches_view - Views the global dofs associated with each patch and its boundary 3230e5893cccSMatthew G. Knepley - -pc_patch_sub_mat_view - Views the matrix associated with each patch 3231e5893cccSMatthew G. Knepley 3232e5893cccSMatthew G. Knepley Level: intermediate 3233e5893cccSMatthew G. Knepley 3234db781477SPatrick Sanan .seealso: `PCType`, `PCCreate()`, `PCSetType()` 3235e5893cccSMatthew G. Knepley M*/ 3236642283e9SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PCCreate_Patch(PC pc) 32374bbf5ea8SMatthew G. Knepley { 32384bbf5ea8SMatthew G. Knepley PC_PATCH *patch; 32394bbf5ea8SMatthew G. Knepley 32404bbf5ea8SMatthew G. Knepley PetscFunctionBegin; 32419566063dSJacob Faibussowitsch PetscCall(PetscNewLog(pc, &patch)); 32424bbf5ea8SMatthew G. Knepley 3243e4c66b91SPatrick Farrell if (patch->subspaces_to_exclude) { 32449566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&patch->subspaces_to_exclude)); 3245e4c66b91SPatrick Farrell } 32469566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&patch->subspaces_to_exclude)); 3247e4c66b91SPatrick Farrell 324810534d48SPatrick Farrell patch->classname = "pc"; 3249debbdec3SPatrick Farrell patch->isNonlinear = PETSC_FALSE; 325010534d48SPatrick Farrell 32514bbf5ea8SMatthew G. Knepley /* Set some defaults */ 32525f824522SMatthew G. Knepley patch->combined = PETSC_FALSE; 32534bbf5ea8SMatthew G. Knepley patch->save_operators = PETSC_TRUE; 325461c4b389SFlorian Wechsung patch->local_composition_type = PC_COMPOSITE_ADDITIVE; 3255fa84ea4cSLawrence Mitchell patch->precomputeElementTensors = PETSC_FALSE; 32564bbf5ea8SMatthew G. Knepley patch->partition_of_unity = PETSC_FALSE; 32574bbf5ea8SMatthew G. Knepley patch->codim = -1; 32584bbf5ea8SMatthew G. Knepley patch->dim = -1; 32594bbf5ea8SMatthew G. Knepley patch->vankadim = -1; 32605f824522SMatthew G. Knepley patch->ignoredim = -1; 3261b525f888SPatrick Farrell patch->pardecomp_overlap = 0; 32624bbf5ea8SMatthew G. Knepley patch->patchconstructop = PCPatchConstruct_Star; 32634bbf5ea8SMatthew G. Knepley patch->symmetrise_sweep = PETSC_FALSE; 32645f824522SMatthew G. Knepley patch->npatch = 0; 32654bbf5ea8SMatthew G. Knepley patch->userIS = NULL; 32665f824522SMatthew G. Knepley patch->optionsSet = PETSC_FALSE; 32674bbf5ea8SMatthew G. Knepley patch->iterationSet = NULL; 32684bbf5ea8SMatthew G. Knepley patch->user_patches = PETSC_FALSE; 32699566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(MATDENSE, (char **) &patch->sub_mat_type)); 32705f824522SMatthew G. Knepley patch->viewPatches = PETSC_FALSE; 32715f824522SMatthew G. Knepley patch->viewCells = PETSC_FALSE; 32725f824522SMatthew G. Knepley patch->viewPoints = PETSC_FALSE; 32735f824522SMatthew G. Knepley patch->viewSection = PETSC_FALSE; 32745f824522SMatthew G. Knepley patch->viewMatrix = PETSC_FALSE; 32759d4fc724SLawrence Mitchell patch->densesolve = NULL; 3276dadc69c5SMatthew G. Knepley patch->setupsolver = PCSetUp_PATCH_Linear; 3277dadc69c5SMatthew G. Knepley patch->applysolver = PCApply_PATCH_Linear; 3278dadc69c5SMatthew G. Knepley patch->resetsolver = PCReset_PATCH_Linear; 3279dadc69c5SMatthew G. Knepley patch->destroysolver = PCDestroy_PATCH_Linear; 32806c9c532dSPatrick Farrell patch->updatemultiplicative = PCUpdateMultiplicative_PATCH_Linear; 328147aca4a6SPatrick Farrell patch->dofMappingWithoutToWithArtificial = NULL; 328247aca4a6SPatrick Farrell patch->dofMappingWithoutToWithAll = NULL; 32834bbf5ea8SMatthew G. Knepley 32844bbf5ea8SMatthew G. Knepley pc->data = (void *) patch; 32854bbf5ea8SMatthew G. Knepley pc->ops->apply = PCApply_PATCH; 32860a545947SLisandro Dalcin pc->ops->applytranspose = NULL; /* PCApplyTranspose_PATCH; */ 32874bbf5ea8SMatthew G. Knepley pc->ops->setup = PCSetUp_PATCH; 32884bbf5ea8SMatthew G. Knepley pc->ops->reset = PCReset_PATCH; 32894bbf5ea8SMatthew G. Knepley pc->ops->destroy = PCDestroy_PATCH; 32904bbf5ea8SMatthew G. Knepley pc->ops->setfromoptions = PCSetFromOptions_PATCH; 32914bbf5ea8SMatthew G. Knepley pc->ops->setuponblocks = PCSetUpOnBlocks_PATCH; 32924bbf5ea8SMatthew G. Knepley pc->ops->view = PCView_PATCH; 32930a545947SLisandro Dalcin pc->ops->applyrichardson = NULL; 32944bbf5ea8SMatthew G. Knepley 32954bbf5ea8SMatthew G. Knepley PetscFunctionReturn(0); 32964bbf5ea8SMatthew G. Knepley } 3297