14bbf5ea8SMatthew G. Knepley #include <petsc/private/pcpatchimpl.h> /*I "petscpc.h" I*/
254ab768cSLawrence Mitchell #include <petsc/private/kspimpl.h> /* For ksp->setfromoptionscalled */
39d4fc724SLawrence Mitchell #include <petsc/private/vecimpl.h> /* For vec->map */
45f824522SMatthew G. Knepley #include <petsc/private/dmpleximpl.h> /* For DMPlexComputeJacobian_Patch_Internal() */
54bbf5ea8SMatthew G. Knepley #include <petscsf.h>
64bbf5ea8SMatthew G. Knepley #include <petscbt.h>
75f824522SMatthew G. Knepley #include <petscds.h>
8c73d2cf6SLawrence Mitchell #include <../src/mat/impls/dense/seq/dense.h> /*I "petscmat.h" I*/
94bbf5ea8SMatthew G. Knepley
10f39ec787SMatthew G. Knepley PetscBool PCPatchcite = PETSC_FALSE;
11f39ec787SMatthew G. Knepley const char PCPatchCitation[] = "@article{FarrellKnepleyWechsungMitchell2020,\n"
12f39ec787SMatthew G. Knepley "title = {{PCPATCH}: software for the topological construction of multigrid relaxation methods},\n"
13f39ec787SMatthew G. Knepley "author = {Patrick E Farrell and Matthew G Knepley and Lawrence Mitchell and Florian Wechsung},\n"
14f39ec787SMatthew G. Knepley "journal = {ACM Transaction on Mathematical Software},\n"
15f39ec787SMatthew G. Knepley "eprint = {http://arxiv.org/abs/1912.08516},\n"
16f39ec787SMatthew G. Knepley "volume = {47},\n"
17f39ec787SMatthew G. Knepley "number = {3},\n"
18f39ec787SMatthew G. Knepley "pages = {1--22},\n"
19f39ec787SMatthew G. Knepley "year = {2021},\n"
20f39ec787SMatthew G. Knepley "petsc_uses={KSP,DMPlex}\n}\n";
21f39ec787SMatthew G. Knepley
229d4fc724SLawrence Mitchell PetscLogEvent PC_Patch_CreatePatches, PC_Patch_ComputeOp, PC_Patch_Solve, PC_Patch_Apply, PC_Patch_Prealloc;
234bbf5ea8SMatthew G. Knepley
ObjectView(PetscObject obj,PetscViewer viewer,PetscViewerFormat format)24d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode ObjectView(PetscObject obj, PetscViewer viewer, PetscViewerFormat format)
25d71ae5a4SJacob Faibussowitsch {
269566063dSJacob Faibussowitsch PetscCall(PetscViewerPushFormat(viewer, format));
279566063dSJacob Faibussowitsch PetscCall(PetscObjectView(obj, viewer));
289566063dSJacob Faibussowitsch PetscCall(PetscViewerPopFormat(viewer));
293ba16761SJacob Faibussowitsch return PETSC_SUCCESS;
305f824522SMatthew G. Knepley }
315f824522SMatthew G. Knepley
PCPatchConstruct_Star(void * vpatch,DM dm,PetscInt point,PetscHSetI ht)32d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchConstruct_Star(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
33d71ae5a4SJacob Faibussowitsch {
344bbf5ea8SMatthew G. Knepley PetscInt starSize;
354bbf5ea8SMatthew G. Knepley PetscInt *star = NULL, si;
364bbf5ea8SMatthew G. Knepley
374bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
389566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(ht));
394bbf5ea8SMatthew G. Knepley /* To start with, add the point we care about */
409566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(ht, point));
414bbf5ea8SMatthew G. Knepley /* Loop over all the points that this point connects to */
429566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star));
439566063dSJacob Faibussowitsch for (si = 0; si < starSize * 2; si += 2) PetscCall(PetscHSetIAdd(ht, star[si]));
449566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star));
453ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
464bbf5ea8SMatthew G. Knepley }
474bbf5ea8SMatthew G. Knepley
PCPatchConstruct_Vanka(void * vpatch,DM dm,PetscInt point,PetscHSetI ht)48d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchConstruct_Vanka(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
49d71ae5a4SJacob Faibussowitsch {
504bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)vpatch;
514bbf5ea8SMatthew G. Knepley PetscInt starSize;
524bbf5ea8SMatthew G. Knepley PetscInt *star = NULL;
534bbf5ea8SMatthew G. Knepley PetscBool shouldIgnore = PETSC_FALSE;
544bbf5ea8SMatthew G. Knepley PetscInt cStart, cEnd, iStart, iEnd, si;
554bbf5ea8SMatthew G. Knepley
564bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
579566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(ht));
584bbf5ea8SMatthew G. Knepley /* To start with, add the point we care about */
599566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(ht, point));
604bbf5ea8SMatthew G. Knepley /* Should we ignore any points of a certain dimension? */
614bbf5ea8SMatthew G. Knepley if (patch->vankadim >= 0) {
624bbf5ea8SMatthew G. Knepley shouldIgnore = PETSC_TRUE;
639566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, patch->vankadim, &iStart, &iEnd));
644bbf5ea8SMatthew G. Knepley }
659566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
664bbf5ea8SMatthew G. Knepley /* Loop over all the cells that this point connects to */
679566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star));
685f824522SMatthew G. Knepley for (si = 0; si < starSize * 2; si += 2) {
694bbf5ea8SMatthew G. Knepley const PetscInt cell = star[si];
704bbf5ea8SMatthew G. Knepley PetscInt closureSize;
714bbf5ea8SMatthew G. Knepley PetscInt *closure = NULL, ci;
724bbf5ea8SMatthew G. Knepley
734bbf5ea8SMatthew G. Knepley if (cell < cStart || cell >= cEnd) continue;
744bbf5ea8SMatthew G. Knepley /* now loop over all entities in the closure of that cell */
759566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
765f824522SMatthew G. Knepley for (ci = 0; ci < closureSize * 2; ci += 2) {
774bbf5ea8SMatthew G. Knepley const PetscInt newpoint = closure[ci];
784bbf5ea8SMatthew G. Knepley
794bbf5ea8SMatthew G. Knepley /* We've been told to ignore entities of this type.*/
804bbf5ea8SMatthew G. Knepley if (shouldIgnore && newpoint >= iStart && newpoint < iEnd) continue;
819566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(ht, newpoint));
824bbf5ea8SMatthew G. Knepley }
839566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
844bbf5ea8SMatthew G. Knepley }
859566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star));
863ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
874bbf5ea8SMatthew G. Knepley }
884bbf5ea8SMatthew G. Knepley
PCPatchConstruct_Pardecomp(void * vpatch,DM dm,PetscInt point,PetscHSetI ht)89d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchConstruct_Pardecomp(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
90d71ae5a4SJacob Faibussowitsch {
91b525f888SPatrick Farrell PC_PATCH *patch = (PC_PATCH *)vpatch;
920a390943SPatrick Farrell DMLabel ghost = NULL;
93eae3dc7dSJacob Faibussowitsch const PetscInt *leaves = NULL;
94eae3dc7dSJacob Faibussowitsch PetscInt nleaves = 0, pStart, pEnd, loc;
950a390943SPatrick Farrell PetscBool isFiredrake;
960a390943SPatrick Farrell PetscBool flg;
97b525f888SPatrick Farrell PetscInt starSize;
98b525f888SPatrick Farrell PetscInt *star = NULL;
9925fd193aSPatrick Farrell PetscInt opoint, overlapi;
1000a390943SPatrick Farrell
1010a390943SPatrick Farrell PetscFunctionBegin;
1029566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(ht));
1030a390943SPatrick Farrell
1049566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
1050a390943SPatrick Farrell
1069566063dSJacob Faibussowitsch PetscCall(DMHasLabel(dm, "pyop2_ghost", &isFiredrake));
1070a390943SPatrick Farrell if (isFiredrake) {
1089566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, "pyop2_ghost", &ghost));
1099566063dSJacob Faibussowitsch PetscCall(DMLabelCreateIndex(ghost, pStart, pEnd));
1100a390943SPatrick Farrell } else {
1110a390943SPatrick Farrell PetscSF sf;
1129566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm, &sf));
1139566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL));
1140a390943SPatrick Farrell nleaves = PetscMax(nleaves, 0);
1150a390943SPatrick Farrell }
1160a390943SPatrick Farrell
11725fd193aSPatrick Farrell for (opoint = pStart; opoint < pEnd; ++opoint) {
1189566063dSJacob Faibussowitsch if (ghost) PetscCall(DMLabelHasPoint(ghost, opoint, &flg));
1199371c9d4SSatish Balay else {
1209371c9d4SSatish Balay PetscCall(PetscFindInt(opoint, nleaves, leaves, &loc));
1219371c9d4SSatish Balay flg = loc >= 0 ? PETSC_TRUE : PETSC_FALSE;
1229371c9d4SSatish Balay }
1230a390943SPatrick Farrell /* Not an owned entity, don't make a cell patch. */
1240a390943SPatrick Farrell if (flg) continue;
1259566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(ht, opoint));
1260a390943SPatrick Farrell }
1270a390943SPatrick Farrell
128b525f888SPatrick Farrell /* Now build the overlap for the patch */
12925fd193aSPatrick Farrell for (overlapi = 0; overlapi < patch->pardecomp_overlap; ++overlapi) {
130b525f888SPatrick Farrell PetscInt index = 0;
131b525f888SPatrick Farrell PetscInt *htpoints = NULL;
132b525f888SPatrick Farrell PetscInt htsize;
13325fd193aSPatrick Farrell PetscInt i;
134b525f888SPatrick Farrell
1359566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(ht, &htsize));
1369566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(htsize, &htpoints));
1379566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetElems(ht, &index, htpoints));
138b525f888SPatrick Farrell
13925fd193aSPatrick Farrell for (i = 0; i < htsize; ++i) {
14025fd193aSPatrick Farrell PetscInt hpoint = htpoints[i];
14125fd193aSPatrick Farrell PetscInt si;
142b525f888SPatrick Farrell
1439566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star));
14425fd193aSPatrick Farrell for (si = 0; si < starSize * 2; si += 2) {
145b525f888SPatrick Farrell const PetscInt starp = star[si];
146b525f888SPatrick Farrell PetscInt closureSize;
147b525f888SPatrick Farrell PetscInt *closure = NULL, ci;
148b525f888SPatrick Farrell
149b525f888SPatrick Farrell /* now loop over all entities in the closure of starp */
1509566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure));
15125fd193aSPatrick Farrell for (ci = 0; ci < closureSize * 2; ci += 2) {
152b525f888SPatrick Farrell const PetscInt closstarp = closure[ci];
1539566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(ht, closstarp));
154b525f888SPatrick Farrell }
1559566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure));
156b525f888SPatrick Farrell }
1579566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star));
158b525f888SPatrick Farrell }
1599566063dSJacob Faibussowitsch PetscCall(PetscFree(htpoints));
160b525f888SPatrick Farrell }
1613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
1620a390943SPatrick Farrell }
1630a390943SPatrick Farrell
1644bbf5ea8SMatthew G. Knepley /* The user's already set the patches in patch->userIS. Build the hash tables */
PCPatchConstruct_User(void * vpatch,DM dm,PetscInt point,PetscHSetI ht)165d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchConstruct_User(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
166d71ae5a4SJacob Faibussowitsch {
1674bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)vpatch;
1684bbf5ea8SMatthew G. Knepley IS patchis = patch->userIS[point];
1694bbf5ea8SMatthew G. Knepley PetscInt n;
1704bbf5ea8SMatthew G. Knepley const PetscInt *patchdata;
1714bbf5ea8SMatthew G. Knepley PetscInt pStart, pEnd, i;
1724bbf5ea8SMatthew G. Knepley
1734bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
1749566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(ht));
1759566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
1769566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(patchis, &n));
1779566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patchis, &patchdata));
1784bbf5ea8SMatthew G. Knepley for (i = 0; i < n; ++i) {
1794bbf5ea8SMatthew G. Knepley const PetscInt ownedpoint = patchdata[i];
1804bbf5ea8SMatthew G. Knepley
1817a46b595SBarry 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);
1829566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(ht, ownedpoint));
1834bbf5ea8SMatthew G. Knepley }
1849566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patchis, &patchdata));
1853ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
1864bbf5ea8SMatthew G. Knepley }
1874bbf5ea8SMatthew G. Knepley
PCPatchCreateDefaultSF_Private(PC pc,PetscInt n,const PetscSF * sf,const PetscInt * bs)188d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCreateDefaultSF_Private(PC pc, PetscInt n, const PetscSF *sf, const PetscInt *bs)
189d71ae5a4SJacob Faibussowitsch {
1904bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
1914bbf5ea8SMatthew G. Knepley
1924bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
1934bbf5ea8SMatthew G. Knepley if (n == 1 && bs[0] == 1) {
1941bb6d2a8SBarry Smith patch->sectionSF = sf[0];
1959566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)patch->sectionSF));
1964bbf5ea8SMatthew G. Knepley } else {
1974bbf5ea8SMatthew G. Knepley PetscInt allRoots = 0, allLeaves = 0;
1984bbf5ea8SMatthew G. Knepley PetscInt leafOffset = 0;
1994bbf5ea8SMatthew G. Knepley PetscInt *ilocal = NULL;
2004bbf5ea8SMatthew G. Knepley PetscSFNode *iremote = NULL;
2014bbf5ea8SMatthew G. Knepley PetscInt *remoteOffsets = NULL;
2024bbf5ea8SMatthew G. Knepley PetscInt index = 0;
2031b68eb51SMatthew G. Knepley PetscHMapI rankToIndex;
2044bbf5ea8SMatthew G. Knepley PetscInt numRanks = 0;
2054bbf5ea8SMatthew G. Knepley PetscSFNode *remote = NULL;
2064bbf5ea8SMatthew G. Knepley PetscSF rankSF;
2074bbf5ea8SMatthew G. Knepley PetscInt *ranks = NULL;
2084bbf5ea8SMatthew G. Knepley PetscInt *offsets = NULL;
2094bbf5ea8SMatthew G. Knepley MPI_Datatype contig;
2101b68eb51SMatthew G. Knepley PetscHSetI ranksUniq;
2116497c311SBarry Smith PetscMPIInt in;
2124bbf5ea8SMatthew G. Knepley
2134bbf5ea8SMatthew G. Knepley /* First figure out how many dofs there are in the concatenated numbering.
214f1580f4eSBarry Smith allRoots: number of owned global dofs;
215f1580f4eSBarry Smith allLeaves: number of visible dofs (global + ghosted).
2164bbf5ea8SMatthew G. Knepley */
217a3f1d042SPierre Jolivet for (PetscInt i = 0; i < n; ++i) {
2184bbf5ea8SMatthew G. Knepley PetscInt nroots, nleaves;
2194bbf5ea8SMatthew G. Knepley
2209566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sf[i], &nroots, &nleaves, NULL, NULL));
2214bbf5ea8SMatthew G. Knepley allRoots += nroots * bs[i];
2224bbf5ea8SMatthew G. Knepley allLeaves += nleaves * bs[i];
2234bbf5ea8SMatthew G. Knepley }
2249566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(allLeaves, &ilocal));
2259566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(allLeaves, &iremote));
2264bbf5ea8SMatthew G. Knepley /* Now build an SF that just contains process connectivity. */
2279566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&ranksUniq));
228a3f1d042SPierre Jolivet for (PetscInt i = 0; i < n; ++i) {
2294bbf5ea8SMatthew G. Knepley const PetscMPIInt *ranks = NULL;
2306497c311SBarry Smith PetscMPIInt nranks;
2314bbf5ea8SMatthew G. Knepley
2329566063dSJacob Faibussowitsch PetscCall(PetscSFSetUp(sf[i]));
2339566063dSJacob Faibussowitsch PetscCall(PetscSFGetRootRanks(sf[i], &nranks, &ranks, NULL, NULL, NULL));
2344bbf5ea8SMatthew G. Knepley /* These are all the ranks who communicate with me. */
2356497c311SBarry Smith for (PetscMPIInt j = 0; j < nranks; ++j) PetscCall(PetscHSetIAdd(ranksUniq, (PetscInt)ranks[j]));
2364bbf5ea8SMatthew G. Knepley }
2379566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(ranksUniq, &numRanks));
2389566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numRanks, &remote));
2399566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numRanks, &ranks));
2409566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetElems(ranksUniq, &index, ranks));
2414bbf5ea8SMatthew G. Knepley
2429566063dSJacob Faibussowitsch PetscCall(PetscHMapICreate(&rankToIndex));
2436497c311SBarry Smith for (PetscInt i = 0; i < numRanks; ++i) {
244835f2295SStefano Zampini remote[i].rank = ranks[i];
2454bbf5ea8SMatthew G. Knepley remote[i].index = 0;
2469566063dSJacob Faibussowitsch PetscCall(PetscHMapISet(rankToIndex, ranks[i], i));
2474bbf5ea8SMatthew G. Knepley }
2489566063dSJacob Faibussowitsch PetscCall(PetscFree(ranks));
2499566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&ranksUniq));
2509566063dSJacob Faibussowitsch PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)pc), &rankSF));
2519566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(rankSF, 1, numRanks, NULL, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER));
2529566063dSJacob Faibussowitsch PetscCall(PetscSFSetUp(rankSF));
253f1580f4eSBarry Smith /* OK, use it to communicate the root offset on the remote processes for each subspace. */
2549566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(n, &offsets));
2559566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(n * numRanks, &remoteOffsets));
2564bbf5ea8SMatthew G. Knepley
2574bbf5ea8SMatthew G. Knepley offsets[0] = 0;
258a3f1d042SPierre Jolivet for (PetscInt i = 1; i < n; ++i) {
2594bbf5ea8SMatthew G. Knepley PetscInt nroots;
2604bbf5ea8SMatthew G. Knepley
2619566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sf[i - 1], &nroots, NULL, NULL, NULL));
2624bbf5ea8SMatthew G. Knepley offsets[i] = offsets[i - 1] + nroots * bs[i - 1];
2634bbf5ea8SMatthew G. Knepley }
264f1580f4eSBarry Smith /* Offsets are the offsets on the current process of the global dof numbering for the subspaces. */
2656497c311SBarry Smith PetscCall(PetscMPIIntCast(n, &in));
2666497c311SBarry Smith PetscCallMPI(MPI_Type_contiguous(in, MPIU_INT, &contig));
2679566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_commit(&contig));
2684bbf5ea8SMatthew G. Knepley
2699566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(rankSF, contig, offsets, remoteOffsets, MPI_REPLACE));
2709566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(rankSF, contig, offsets, remoteOffsets, MPI_REPLACE));
2719566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_free(&contig));
2729566063dSJacob Faibussowitsch PetscCall(PetscFree(offsets));
2739566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&rankSF));
2744bbf5ea8SMatthew G. Knepley /* Now remoteOffsets contains the offsets on the remote
275f1580f4eSBarry Smith processes who communicate with me. So now we can
276f1580f4eSBarry Smith concatenate the list of SFs into a single one. */
2774bbf5ea8SMatthew G. Knepley index = 0;
278a3f1d042SPierre Jolivet for (PetscInt i = 0; i < n; ++i) {
2794bbf5ea8SMatthew G. Knepley const PetscSFNode *remote = NULL;
2804bbf5ea8SMatthew G. Knepley const PetscInt *local = NULL;
2814bbf5ea8SMatthew G. Knepley PetscInt nroots, nleaves, j;
2824bbf5ea8SMatthew G. Knepley
2839566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sf[i], &nroots, &nleaves, &local, &remote));
2844bbf5ea8SMatthew G. Knepley for (j = 0; j < nleaves; ++j) {
2854bbf5ea8SMatthew G. Knepley PetscInt rank = remote[j].rank;
2864bbf5ea8SMatthew G. Knepley PetscInt idx, rootOffset, k;
2874bbf5ea8SMatthew G. Knepley
2889566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(rankToIndex, rank, &idx));
28908401ef6SPierre Jolivet PetscCheck(idx != -1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Didn't find rank, huh?");
2904bbf5ea8SMatthew G. Knepley /* Offset on given rank for ith subspace */
2914bbf5ea8SMatthew G. Knepley rootOffset = remoteOffsets[n * idx + i];
2924bbf5ea8SMatthew G. Knepley for (k = 0; k < bs[i]; ++k) {
29373ec7555SLawrence Mitchell ilocal[index] = (local ? local[j] : j) * bs[i] + k + leafOffset;
2944bbf5ea8SMatthew G. Knepley iremote[index].rank = remote[j].rank;
2954bbf5ea8SMatthew G. Knepley iremote[index].index = remote[j].index * bs[i] + k + rootOffset;
2964bbf5ea8SMatthew G. Knepley ++index;
2974bbf5ea8SMatthew G. Knepley }
2984bbf5ea8SMatthew G. Knepley }
2994bbf5ea8SMatthew G. Knepley leafOffset += nleaves * bs[i];
3004bbf5ea8SMatthew G. Knepley }
3019566063dSJacob Faibussowitsch PetscCall(PetscHMapIDestroy(&rankToIndex));
3029566063dSJacob Faibussowitsch PetscCall(PetscFree(remoteOffsets));
3039566063dSJacob Faibussowitsch PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)pc), &patch->sectionSF));
3049566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(patch->sectionSF, allRoots, allLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER));
3054bbf5ea8SMatthew G. Knepley }
3063ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3074bbf5ea8SMatthew G. Knepley }
3084bbf5ea8SMatthew G. Knepley
3094bbf5ea8SMatthew G. Knepley /* TODO: Docs */
PCPatchGetIgnoreDim(PC pc,PetscInt * dim)310ba38deedSJacob Faibussowitsch static PetscErrorCode PCPatchGetIgnoreDim(PC pc, PetscInt *dim)
311d71ae5a4SJacob Faibussowitsch {
3125f824522SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
3134d86920dSPierre Jolivet
3145f824522SMatthew G. Knepley PetscFunctionBegin;
3155f824522SMatthew G. Knepley *dim = patch->ignoredim;
3163ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3175f824522SMatthew G. Knepley }
3185f824522SMatthew G. Knepley
3195f824522SMatthew G. Knepley /* TODO: Docs */
PCPatchSetSaveOperators(PC pc,PetscBool flg)320d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetSaveOperators(PC pc, PetscBool flg)
321d71ae5a4SJacob Faibussowitsch {
3224bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
3234d86920dSPierre Jolivet
3244bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
3254bbf5ea8SMatthew G. Knepley patch->save_operators = flg;
3263ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3274bbf5ea8SMatthew G. Knepley }
3284bbf5ea8SMatthew G. Knepley
3294bbf5ea8SMatthew G. Knepley /* TODO: Docs */
PCPatchGetSaveOperators(PC pc,PetscBool * flg)330d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetSaveOperators(PC pc, PetscBool *flg)
331d71ae5a4SJacob Faibussowitsch {
3324bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
3334d86920dSPierre Jolivet
3344bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
3354bbf5ea8SMatthew G. Knepley *flg = patch->save_operators;
3363ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3374bbf5ea8SMatthew G. Knepley }
3384bbf5ea8SMatthew G. Knepley
3394bbf5ea8SMatthew G. Knepley /* TODO: Docs */
PCPatchSetPrecomputeElementTensors(PC pc,PetscBool flg)340d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetPrecomputeElementTensors(PC pc, PetscBool flg)
341d71ae5a4SJacob Faibussowitsch {
342fa84ea4cSLawrence Mitchell PC_PATCH *patch = (PC_PATCH *)pc->data;
3434d86920dSPierre Jolivet
344fa84ea4cSLawrence Mitchell PetscFunctionBegin;
345fa84ea4cSLawrence Mitchell patch->precomputeElementTensors = flg;
3463ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
347fa84ea4cSLawrence Mitchell }
348fa84ea4cSLawrence Mitchell
349fa84ea4cSLawrence Mitchell /* TODO: Docs */
PCPatchGetPrecomputeElementTensors(PC pc,PetscBool * flg)350d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetPrecomputeElementTensors(PC pc, PetscBool *flg)
351d71ae5a4SJacob Faibussowitsch {
352fa84ea4cSLawrence Mitchell PC_PATCH *patch = (PC_PATCH *)pc->data;
3534d86920dSPierre Jolivet
354fa84ea4cSLawrence Mitchell PetscFunctionBegin;
355fa84ea4cSLawrence Mitchell *flg = patch->precomputeElementTensors;
3563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
357fa84ea4cSLawrence Mitchell }
358fa84ea4cSLawrence Mitchell
359fa84ea4cSLawrence Mitchell /* TODO: Docs */
PCPatchSetPartitionOfUnity(PC pc,PetscBool flg)360d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetPartitionOfUnity(PC pc, PetscBool flg)
361d71ae5a4SJacob Faibussowitsch {
3624bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
3634d86920dSPierre Jolivet
3644bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
3654bbf5ea8SMatthew G. Knepley patch->partition_of_unity = flg;
3663ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3674bbf5ea8SMatthew G. Knepley }
3684bbf5ea8SMatthew G. Knepley
3694bbf5ea8SMatthew G. Knepley /* TODO: Docs */
PCPatchGetPartitionOfUnity(PC pc,PetscBool * flg)370d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetPartitionOfUnity(PC pc, PetscBool *flg)
371d71ae5a4SJacob Faibussowitsch {
3724bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
3734d86920dSPierre Jolivet
3744bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
3754bbf5ea8SMatthew G. Knepley *flg = patch->partition_of_unity;
3763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3774bbf5ea8SMatthew G. Knepley }
3784bbf5ea8SMatthew G. Knepley
3794bbf5ea8SMatthew G. Knepley /* TODO: Docs */
PCPatchSetLocalComposition(PC pc,PCCompositeType type)380ba38deedSJacob Faibussowitsch static PetscErrorCode PCPatchSetLocalComposition(PC pc, PCCompositeType type)
381d71ae5a4SJacob Faibussowitsch {
382c2e6f3c0SFlorian Wechsung PC_PATCH *patch = (PC_PATCH *)pc->data;
3834d86920dSPierre Jolivet
384c2e6f3c0SFlorian Wechsung PetscFunctionBegin;
3852472a847SBarry 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");
38661c4b389SFlorian Wechsung patch->local_composition_type = type;
3873ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
388c2e6f3c0SFlorian Wechsung }
389c2e6f3c0SFlorian Wechsung
390c2e6f3c0SFlorian Wechsung /* TODO: Docs */
PCPatchGetSubKSP(PC pc,PetscInt * npatch,KSP ** ksp)391fb80e629SPablo Brubeck PetscErrorCode PCPatchGetSubKSP(PC pc, PetscInt *npatch, KSP **ksp)
392fb80e629SPablo Brubeck {
393fb80e629SPablo Brubeck PC_PATCH *patch = (PC_PATCH *)pc->data;
394fb80e629SPablo Brubeck
395fb80e629SPablo Brubeck PetscFunctionBegin;
396fb80e629SPablo Brubeck PetscCheck(pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ORDER, "Need to call PCSetUp() on PC (or KSPSetUp() on the outer KSP object) before calling here");
397fb80e629SPablo Brubeck PetscCall(PetscMalloc1(patch->npatch, ksp));
398a3f1d042SPierre Jolivet for (PetscInt i = 0; i < patch->npatch; ++i) (*ksp)[i] = (KSP)patch->solver[i];
399fb80e629SPablo Brubeck if (npatch) *npatch = patch->npatch;
400fb80e629SPablo Brubeck PetscFunctionReturn(PETSC_SUCCESS);
401fb80e629SPablo Brubeck }
402fb80e629SPablo Brubeck
403fb80e629SPablo Brubeck /* TODO: Docs */
PCPatchSetSubMatType(PC pc,MatType sub_mat_type)404d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetSubMatType(PC pc, MatType sub_mat_type)
405d71ae5a4SJacob Faibussowitsch {
4064bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
4074bbf5ea8SMatthew G. Knepley
4084bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
4099566063dSJacob Faibussowitsch if (patch->sub_mat_type) PetscCall(PetscFree(patch->sub_mat_type));
4109566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(sub_mat_type, (char **)&patch->sub_mat_type));
4113ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
4124bbf5ea8SMatthew G. Knepley }
4134bbf5ea8SMatthew G. Knepley
4144bbf5ea8SMatthew G. Knepley /* TODO: Docs */
PCPatchGetSubMatType(PC pc,MatType * sub_mat_type)415d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetSubMatType(PC pc, MatType *sub_mat_type)
416d71ae5a4SJacob Faibussowitsch {
4174bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
4184d86920dSPierre Jolivet
4194bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
4204bbf5ea8SMatthew G. Knepley *sub_mat_type = patch->sub_mat_type;
4213ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
4224bbf5ea8SMatthew G. Knepley }
4234bbf5ea8SMatthew G. Knepley
4244bbf5ea8SMatthew G. Knepley /* TODO: Docs */
PCPatchSetCellNumbering(PC pc,PetscSection cellNumbering)425d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetCellNumbering(PC pc, PetscSection cellNumbering)
426d71ae5a4SJacob Faibussowitsch {
4274bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
4284bbf5ea8SMatthew G. Knepley
4294bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
4304bbf5ea8SMatthew G. Knepley patch->cellNumbering = cellNumbering;
4319566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)cellNumbering));
4323ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
4334bbf5ea8SMatthew G. Knepley }
4344bbf5ea8SMatthew G. Knepley
4354bbf5ea8SMatthew G. Knepley /* TODO: Docs */
PCPatchGetCellNumbering(PC pc,PetscSection * cellNumbering)436d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetCellNumbering(PC pc, PetscSection *cellNumbering)
437d71ae5a4SJacob Faibussowitsch {
4384bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
4394d86920dSPierre Jolivet
4404bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
4414bbf5ea8SMatthew G. Knepley *cellNumbering = patch->cellNumbering;
4423ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
4434bbf5ea8SMatthew G. Knepley }
4444bbf5ea8SMatthew G. Knepley
4454bbf5ea8SMatthew G. Knepley /* TODO: Docs */
PCPatchSetConstructType(PC pc,PCPatchConstructType ctype,PetscErrorCode (* func)(PC,PetscInt *,IS **,IS *,void *),PetscCtx ctx)446*2a8381b2SBarry Smith PetscErrorCode PCPatchSetConstructType(PC pc, PCPatchConstructType ctype, PetscErrorCode (*func)(PC, PetscInt *, IS **, IS *, void *), PetscCtx ctx)
447d71ae5a4SJacob Faibussowitsch {
4484bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
4494bbf5ea8SMatthew G. Knepley
4504bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
4514bbf5ea8SMatthew G. Knepley patch->ctype = ctype;
4524bbf5ea8SMatthew G. Knepley switch (ctype) {
4534bbf5ea8SMatthew G. Knepley case PC_PATCH_STAR:
45440c17a03SPatrick Farrell patch->user_patches = PETSC_FALSE;
4554bbf5ea8SMatthew G. Knepley patch->patchconstructop = PCPatchConstruct_Star;
4564bbf5ea8SMatthew G. Knepley break;
4574bbf5ea8SMatthew G. Knepley case PC_PATCH_VANKA:
45840c17a03SPatrick Farrell patch->user_patches = PETSC_FALSE;
4594bbf5ea8SMatthew G. Knepley patch->patchconstructop = PCPatchConstruct_Vanka;
4604bbf5ea8SMatthew G. Knepley break;
461e5b9877fSPatrick Farrell case PC_PATCH_PARDECOMP:
4620a390943SPatrick Farrell patch->user_patches = PETSC_FALSE;
463e5b9877fSPatrick Farrell patch->patchconstructop = PCPatchConstruct_Pardecomp;
4640a390943SPatrick Farrell break;
4654bbf5ea8SMatthew G. Knepley case PC_PATCH_USER:
4664bbf5ea8SMatthew G. Knepley case PC_PATCH_PYTHON:
4674bbf5ea8SMatthew G. Knepley patch->user_patches = PETSC_TRUE;
4684bbf5ea8SMatthew G. Knepley patch->patchconstructop = PCPatchConstruct_User;
469bdd9e0cdSPatrick Farrell if (func) {
4704bbf5ea8SMatthew G. Knepley patch->userpatchconstructionop = func;
4714bbf5ea8SMatthew G. Knepley patch->userpatchconstructctx = ctx;
472bdd9e0cdSPatrick Farrell }
4734bbf5ea8SMatthew G. Knepley break;
474d71ae5a4SJacob Faibussowitsch default:
475d71ae5a4SJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_USER, "Unknown patch construction type %" PetscInt_FMT, (PetscInt)patch->ctype);
4764bbf5ea8SMatthew G. Knepley }
4773ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
4784bbf5ea8SMatthew G. Knepley }
4794bbf5ea8SMatthew G. Knepley
4804bbf5ea8SMatthew G. Knepley /* TODO: Docs */
PCPatchGetConstructType(PC pc,PCPatchConstructType * ctype,PetscErrorCode (** func)(PC,PetscInt *,IS **,IS *,void *),void ** ctx)481d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetConstructType(PC pc, PCPatchConstructType *ctype, PetscErrorCode (**func)(PC, PetscInt *, IS **, IS *, void *), void **ctx)
482d71ae5a4SJacob Faibussowitsch {
4834bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
4844bbf5ea8SMatthew G. Knepley
4854bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
4864bbf5ea8SMatthew G. Knepley *ctype = patch->ctype;
4874bbf5ea8SMatthew G. Knepley switch (patch->ctype) {
4884bbf5ea8SMatthew G. Knepley case PC_PATCH_STAR:
4894bbf5ea8SMatthew G. Knepley case PC_PATCH_VANKA:
490d71ae5a4SJacob Faibussowitsch case PC_PATCH_PARDECOMP:
491d71ae5a4SJacob Faibussowitsch break;
4924bbf5ea8SMatthew G. Knepley case PC_PATCH_USER:
4934bbf5ea8SMatthew G. Knepley case PC_PATCH_PYTHON:
4944bbf5ea8SMatthew G. Knepley *func = patch->userpatchconstructionop;
4954bbf5ea8SMatthew G. Knepley *ctx = patch->userpatchconstructctx;
4964bbf5ea8SMatthew G. Knepley break;
497d71ae5a4SJacob Faibussowitsch default:
498d71ae5a4SJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_USER, "Unknown patch construction type %" PetscInt_FMT, (PetscInt)patch->ctype);
4994bbf5ea8SMatthew G. Knepley }
5003ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
5014bbf5ea8SMatthew G. Knepley }
5024bbf5ea8SMatthew G. Knepley
5034bbf5ea8SMatthew G. Knepley /* TODO: Docs */
PCPatchSetDiscretisationInfo(PC pc,PetscInt nsubspaces,DM * dms,PetscInt * bs,PetscInt * nodesPerCell,const PetscInt ** cellNodeMap,const PetscInt * subspaceOffsets,PetscInt numGhostBcs,const PetscInt * ghostBcNodes,PetscInt numGlobalBcs,const PetscInt * globalBcNodes)504d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetDiscretisationInfo(PC pc, PetscInt nsubspaces, DM *dms, PetscInt *bs, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, const PetscInt *subspaceOffsets, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
505d71ae5a4SJacob Faibussowitsch {
5064bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
507b6bb21d1SLawrence Mitchell DM dm, plex;
5084bbf5ea8SMatthew G. Knepley PetscSF *sfs;
5095f824522SMatthew G. Knepley PetscInt cStart, cEnd, i, j;
5104bbf5ea8SMatthew G. Knepley
5114bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
5129566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm));
5139566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex));
514b6bb21d1SLawrence Mitchell dm = plex;
5159566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
5169566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nsubspaces, &sfs));
5179566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nsubspaces, &patch->dofSection));
5189566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nsubspaces, &patch->bs));
5199566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nsubspaces, &patch->nodesPerCell));
5209566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nsubspaces, &patch->cellNodeMap));
5219566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nsubspaces + 1, &patch->subspaceOffsets));
5224bbf5ea8SMatthew G. Knepley
5234bbf5ea8SMatthew G. Knepley patch->nsubspaces = nsubspaces;
5244bbf5ea8SMatthew G. Knepley patch->totalDofsPerCell = 0;
5254bbf5ea8SMatthew G. Knepley for (i = 0; i < nsubspaces; ++i) {
5269566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dms[i], &patch->dofSection[i]));
5279566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)patch->dofSection[i]));
5289566063dSJacob Faibussowitsch PetscCall(DMGetSectionSF(dms[i], &sfs[i]));
5294bbf5ea8SMatthew G. Knepley patch->bs[i] = bs[i];
5304bbf5ea8SMatthew G. Knepley patch->nodesPerCell[i] = nodesPerCell[i];
5314bbf5ea8SMatthew G. Knepley patch->totalDofsPerCell += nodesPerCell[i] * bs[i];
5329566063dSJacob Faibussowitsch PetscCall(PetscMalloc1((cEnd - cStart) * nodesPerCell[i], &patch->cellNodeMap[i]));
53380e8a965SFlorian Wechsung for (j = 0; j < (cEnd - cStart) * nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
5344bbf5ea8SMatthew G. Knepley patch->subspaceOffsets[i] = subspaceOffsets[i];
5354bbf5ea8SMatthew G. Knepley }
5369566063dSJacob Faibussowitsch PetscCall(PCPatchCreateDefaultSF_Private(pc, nsubspaces, sfs, patch->bs));
5379566063dSJacob Faibussowitsch PetscCall(PetscFree(sfs));
5384bbf5ea8SMatthew G. Knepley
5394bbf5ea8SMatthew G. Knepley patch->subspaceOffsets[nsubspaces] = subspaceOffsets[nsubspaces];
5409566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes));
5419566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes));
5429566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm));
5433ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
5444bbf5ea8SMatthew G. Knepley }
5454bbf5ea8SMatthew G. Knepley
5464bbf5ea8SMatthew G. Knepley /* TODO: Docs */
PCPatchSetDiscretisationInfoCombined(PC pc,DM dm,PetscInt * nodesPerCell,const PetscInt ** cellNodeMap,PetscInt numGhostBcs,const PetscInt * ghostBcNodes,PetscInt numGlobalBcs,const PetscInt * globalBcNodes)547ba38deedSJacob Faibussowitsch static PetscErrorCode PCPatchSetDiscretisationInfoCombined(PC pc, DM dm, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
548d71ae5a4SJacob Faibussowitsch {
5495f824522SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
5505f824522SMatthew G. Knepley PetscInt cStart, cEnd, i, j;
5515f824522SMatthew G. Knepley
5525f824522SMatthew G. Knepley PetscFunctionBegin;
5535f824522SMatthew G. Knepley patch->combined = PETSC_TRUE;
5549566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
5559566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &patch->nsubspaces));
5569566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(patch->nsubspaces, &patch->dofSection));
5579566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(patch->nsubspaces, &patch->bs));
5589566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(patch->nsubspaces, &patch->nodesPerCell));
5599566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(patch->nsubspaces, &patch->cellNodeMap));
5609566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(patch->nsubspaces + 1, &patch->subspaceOffsets));
5619566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, &patch->dofSection[0]));
5629566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)patch->dofSection[0]));
5639566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(patch->dofSection[0], &patch->subspaceOffsets[patch->nsubspaces]));
5645f824522SMatthew G. Knepley patch->totalDofsPerCell = 0;
5655f824522SMatthew G. Knepley for (i = 0; i < patch->nsubspaces; ++i) {
5665f824522SMatthew G. Knepley patch->bs[i] = 1;
5675f824522SMatthew G. Knepley patch->nodesPerCell[i] = nodesPerCell[i];
5685f824522SMatthew G. Knepley patch->totalDofsPerCell += nodesPerCell[i];
5699566063dSJacob Faibussowitsch PetscCall(PetscMalloc1((cEnd - cStart) * nodesPerCell[i], &patch->cellNodeMap[i]));
5705f824522SMatthew G. Knepley for (j = 0; j < (cEnd - cStart) * nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
5715f824522SMatthew G. Knepley }
5729566063dSJacob Faibussowitsch PetscCall(DMGetSectionSF(dm, &patch->sectionSF));
5739566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)patch->sectionSF));
5749566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes));
5759566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes));
5763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
5775f824522SMatthew G. Knepley }
5785f824522SMatthew G. Knepley
5795f824522SMatthew G. Knepley /*@C
58004c3f3b8SBarry Smith PCPatchSetComputeFunction - Set the callback function used to compute patch residuals
58192d50984SMatthew G. Knepley
58220f4b53cSBarry Smith Logically Collective
58399b7e5c6SPatrick Farrell
58492d50984SMatthew G. Knepley Input Parameters:
58520f4b53cSBarry Smith + pc - The `PC`
58604c3f3b8SBarry Smith . func - The callback function
58792d50984SMatthew G. Knepley - ctx - The user context
58892d50984SMatthew G. Knepley
58920f4b53cSBarry Smith Calling sequence of `func`:
59020f4b53cSBarry Smith + pc - The `PC`
5917a50e09dSPatrick Farrell . point - The point
5927a50e09dSPatrick Farrell . x - The input solution (not used in linear problems)
5937a50e09dSPatrick Farrell . f - The patch residual vector
5947a50e09dSPatrick Farrell . cellIS - An array of the cell numbers
59520f4b53cSBarry Smith . n - The size of `dofsArray`
5967a50e09dSPatrick Farrell . dofsArray - The dofmap for the dofs to be solved for
5977a50e09dSPatrick Farrell . dofsArrayWithAll - The dofmap for all dofs on the patch
5987a50e09dSPatrick Farrell - ctx - The user context
5997a50e09dSPatrick Farrell
60092d50984SMatthew G. Knepley Level: advanced
60192d50984SMatthew G. Knepley
602f1580f4eSBarry Smith Note:
60304c3f3b8SBarry Smith The entries of `f` (the output residual vector) have been set to zero before the call.
60492d50984SMatthew G. Knepley
605562efe2eSBarry Smith .seealso: [](ch_ksp), `PCPatchSetComputeOperator()`, `PCPatchGetComputeOperator()`, `PCPatchSetDiscretisationInfo()`, `PCPatchSetComputeFunctionInteriorFacets()`
60692d50984SMatthew G. Knepley @*/
PCPatchSetComputeFunction(PC pc,PetscErrorCode (* func)(PC pc,PetscInt point,Vec x,Vec f,IS cellIS,PetscInt n,const PetscInt * dofsArray,const PetscInt * dofsArrayWithAll,PetscCtx ctx),PetscCtx ctx)607*2a8381b2SBarry Smith PetscErrorCode PCPatchSetComputeFunction(PC pc, PetscErrorCode (*func)(PC pc, PetscInt point, Vec x, Vec f, IS cellIS, PetscInt n, const PetscInt *dofsArray, const PetscInt *dofsArrayWithAll, PetscCtx ctx), PetscCtx ctx)
608d71ae5a4SJacob Faibussowitsch {
60992d50984SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
61092d50984SMatthew G. Knepley
61192d50984SMatthew G. Knepley PetscFunctionBegin;
61292d50984SMatthew G. Knepley patch->usercomputef = func;
61392d50984SMatthew G. Knepley patch->usercomputefctx = ctx;
6143ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
61592d50984SMatthew G. Knepley }
61692d50984SMatthew G. Knepley
61792d50984SMatthew G. Knepley /*@C
61804c3f3b8SBarry Smith PCPatchSetComputeFunctionInteriorFacets - Set the callback function used to compute facet integrals for patch residuals
61959109abcSLawrence Mitchell
62020f4b53cSBarry Smith Logically Collective
6217a50e09dSPatrick Farrell
62259109abcSLawrence Mitchell Input Parameters:
62320f4b53cSBarry Smith + pc - The `PC`
62404c3f3b8SBarry Smith . func - The callback function
62559109abcSLawrence Mitchell - ctx - The user context
62659109abcSLawrence Mitchell
62720f4b53cSBarry Smith Calling sequence of `func`:
62820f4b53cSBarry Smith + pc - The `PC`
6297a50e09dSPatrick Farrell . point - The point
6307a50e09dSPatrick Farrell . x - The input solution (not used in linear problems)
6317a50e09dSPatrick Farrell . f - The patch residual vector
6327a50e09dSPatrick Farrell . facetIS - An array of the facet numbers
63320f4b53cSBarry Smith . n - The size of `dofsArray`
6347a50e09dSPatrick Farrell . dofsArray - The dofmap for the dofs to be solved for
6357a50e09dSPatrick Farrell . dofsArrayWithAll - The dofmap for all dofs on the patch
6367a50e09dSPatrick Farrell - ctx - The user context
6377a50e09dSPatrick Farrell
63859109abcSLawrence Mitchell Level: advanced
63959109abcSLawrence Mitchell
640f1580f4eSBarry Smith Note:
64104c3f3b8SBarry Smith The entries of `f` (the output residual vector) have been set to zero before the call.
64259109abcSLawrence Mitchell
643562efe2eSBarry Smith .seealso: [](ch_ksp), `PCPatchSetComputeOperator()`, `PCPatchGetComputeOperator()`, `PCPatchSetDiscretisationInfo()`, `PCPatchSetComputeFunction()`
64459109abcSLawrence Mitchell @*/
PCPatchSetComputeFunctionInteriorFacets(PC pc,PetscErrorCode (* func)(PC pc,PetscInt point,Vec x,Vec f,IS facetIS,PetscInt n,const PetscInt * dofsArray,const PetscInt * dofsArrayWithAll,PetscCtx ctx),PetscCtx ctx)645*2a8381b2SBarry Smith PetscErrorCode PCPatchSetComputeFunctionInteriorFacets(PC pc, PetscErrorCode (*func)(PC pc, PetscInt point, Vec x, Vec f, IS facetIS, PetscInt n, const PetscInt *dofsArray, const PetscInt *dofsArrayWithAll, PetscCtx ctx), PetscCtx ctx)
646d71ae5a4SJacob Faibussowitsch {
64759109abcSLawrence Mitchell PC_PATCH *patch = (PC_PATCH *)pc->data;
64859109abcSLawrence Mitchell
64959109abcSLawrence Mitchell PetscFunctionBegin;
65059109abcSLawrence Mitchell patch->usercomputefintfacet = func;
65159109abcSLawrence Mitchell patch->usercomputefintfacetctx = ctx;
6523ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
65359109abcSLawrence Mitchell }
65459109abcSLawrence Mitchell
65559109abcSLawrence Mitchell /*@C
65604c3f3b8SBarry Smith PCPatchSetComputeOperator - Set the callback function used to compute patch matrices
6575f824522SMatthew G. Knepley
65820f4b53cSBarry Smith Logically Collective
6597a50e09dSPatrick Farrell
6605f824522SMatthew G. Knepley Input Parameters:
66120f4b53cSBarry Smith + pc - The `PC`
66204c3f3b8SBarry Smith . func - The callback function
6635f824522SMatthew G. Knepley - ctx - The user context
6645f824522SMatthew G. Knepley
66520f4b53cSBarry Smith Calling sequence of `func`:
66620f4b53cSBarry Smith + pc - The `PC`
6677a50e09dSPatrick Farrell . point - The point
6687a50e09dSPatrick Farrell . x - The input solution (not used in linear problems)
6697a50e09dSPatrick Farrell . mat - The patch matrix
67004c3f3b8SBarry Smith . facetIS - An array of the cell numbers
67120f4b53cSBarry Smith . n - The size of `dofsArray`
6727a50e09dSPatrick Farrell . dofsArray - The dofmap for the dofs to be solved for
6737a50e09dSPatrick Farrell . dofsArrayWithAll - The dofmap for all dofs on the patch
6747a50e09dSPatrick Farrell - ctx - The user context
6757a50e09dSPatrick Farrell
6765f824522SMatthew G. Knepley Level: advanced
6775f824522SMatthew G. Knepley
678f1580f4eSBarry Smith Note:
6797a50e09dSPatrick Farrell The matrix entries have been set to zero before the call.
6805f824522SMatthew G. Knepley
681562efe2eSBarry Smith .seealso: [](ch_ksp), `PCPatchGetComputeOperator()`, `PCPatchSetComputeFunction()`, `PCPatchSetDiscretisationInfo()`
6825f824522SMatthew G. Knepley @*/
PCPatchSetComputeOperator(PC pc,PetscErrorCode (* func)(PC pc,PetscInt point,Vec x,Mat mat,IS facetIS,PetscInt n,const PetscInt * dofsArray,const PetscInt * dofsArrayWithAll,PetscCtx ctx),PetscCtx ctx)683*2a8381b2SBarry Smith PetscErrorCode PCPatchSetComputeOperator(PC pc, PetscErrorCode (*func)(PC pc, PetscInt point, Vec x, Mat mat, IS facetIS, PetscInt n, const PetscInt *dofsArray, const PetscInt *dofsArrayWithAll, PetscCtx ctx), PetscCtx ctx)
684d71ae5a4SJacob Faibussowitsch {
6854bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
6864bbf5ea8SMatthew G. Knepley
6874bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
6884bbf5ea8SMatthew G. Knepley patch->usercomputeop = func;
689723f9013SMatthew G. Knepley patch->usercomputeopctx = ctx;
6903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
6914bbf5ea8SMatthew G. Knepley }
6924bbf5ea8SMatthew G. Knepley
69359109abcSLawrence Mitchell /*@C
69404c3f3b8SBarry Smith PCPatchSetComputeOperatorInteriorFacets - Set the callback function used to compute facet integrals for patch matrices
69559109abcSLawrence Mitchell
69620f4b53cSBarry Smith Logically Collective
69799b7e5c6SPatrick Farrell
69859109abcSLawrence Mitchell Input Parameters:
69920f4b53cSBarry Smith + pc - The `PC`
70004c3f3b8SBarry Smith . func - The callback function
70159109abcSLawrence Mitchell - ctx - The user context
70259109abcSLawrence Mitchell
70320f4b53cSBarry Smith Calling sequence of `func`:
70420f4b53cSBarry Smith + pc - The `PC`
7057a50e09dSPatrick Farrell . point - The point
7067a50e09dSPatrick Farrell . x - The input solution (not used in linear problems)
7077a50e09dSPatrick Farrell . mat - The patch matrix
7087a50e09dSPatrick Farrell . facetIS - An array of the facet numbers
70920f4b53cSBarry Smith . n - The size of `dofsArray`
7107a50e09dSPatrick Farrell . dofsArray - The dofmap for the dofs to be solved for
7117a50e09dSPatrick Farrell . dofsArrayWithAll - The dofmap for all dofs on the patch
7127a50e09dSPatrick Farrell - ctx - The user context
7137a50e09dSPatrick Farrell
71459109abcSLawrence Mitchell Level: advanced
71559109abcSLawrence Mitchell
716f1580f4eSBarry Smith Note:
7177a50e09dSPatrick Farrell The matrix entries have been set to zero before the call.
71859109abcSLawrence Mitchell
719562efe2eSBarry Smith .seealso: [](ch_ksp), `PCPatchGetComputeOperator()`, `PCPatchSetComputeFunction()`, `PCPatchSetDiscretisationInfo()`
72059109abcSLawrence Mitchell @*/
PCPatchSetComputeOperatorInteriorFacets(PC pc,PetscErrorCode (* func)(PC pc,PetscInt point,Vec x,Mat mat,IS facetIS,PetscInt n,const PetscInt * dofsArray,const PetscInt * dofsArrayWithAll,PetscCtx ctx),PetscCtx ctx)721*2a8381b2SBarry Smith PetscErrorCode PCPatchSetComputeOperatorInteriorFacets(PC pc, PetscErrorCode (*func)(PC pc, PetscInt point, Vec x, Mat mat, IS facetIS, PetscInt n, const PetscInt *dofsArray, const PetscInt *dofsArrayWithAll, PetscCtx ctx), PetscCtx ctx)
722d71ae5a4SJacob Faibussowitsch {
72359109abcSLawrence Mitchell PC_PATCH *patch = (PC_PATCH *)pc->data;
72459109abcSLawrence Mitchell
72559109abcSLawrence Mitchell PetscFunctionBegin;
72659109abcSLawrence Mitchell patch->usercomputeopintfacet = func;
72759109abcSLawrence Mitchell patch->usercomputeopintfacetctx = ctx;
7283ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
72959109abcSLawrence Mitchell }
73059109abcSLawrence Mitchell
7314bbf5ea8SMatthew G. Knepley /* On entry, ht contains the topological entities whose dofs we are responsible for solving for;
7324bbf5ea8SMatthew G. Knepley on exit, cht contains all the topological entities we need to compute their residuals.
7334bbf5ea8SMatthew G. Knepley In full generality this should incorporate knowledge of the sparsity pattern of the matrix;
7344bbf5ea8SMatthew G. Knepley here we assume a standard FE sparsity pattern.*/
7354bbf5ea8SMatthew G. Knepley /* TODO: Use DMPlexGetAdjacency() */
PCPatchCompleteCellPatch(PC pc,PetscHSetI ht,PetscHSetI cht)736d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCompleteCellPatch(PC pc, PetscHSetI ht, PetscHSetI cht)
737d71ae5a4SJacob Faibussowitsch {
738b6bb21d1SLawrence Mitchell DM dm, plex;
739bc7fa33aSFlorian Wechsung PC_PATCH *patch = (PC_PATCH *)pc->data;
7401b68eb51SMatthew G. Knepley PetscHashIter hi;
7414bbf5ea8SMatthew G. Knepley PetscInt point;
7424bbf5ea8SMatthew G. Knepley PetscInt *star = NULL, *closure = NULL;
7434c954380SMatthew G. Knepley PetscInt ignoredim, iStart = 0, iEnd = -1, starSize, closureSize, si, ci;
744bc7fa33aSFlorian Wechsung PetscInt *fStar = NULL, *fClosure = NULL;
745bc7fa33aSFlorian Wechsung PetscInt fBegin, fEnd, fsi, fci, fStarSize, fClosureSize;
7464bbf5ea8SMatthew G. Knepley
7474bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
7489566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm));
7499566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex));
750b6bb21d1SLawrence Mitchell dm = plex;
7519566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fBegin, &fEnd));
7529566063dSJacob Faibussowitsch PetscCall(PCPatchGetIgnoreDim(pc, &ignoredim));
7539566063dSJacob Faibussowitsch if (ignoredim >= 0) PetscCall(DMPlexGetDepthStratum(dm, ignoredim, &iStart, &iEnd));
7549566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(cht));
7551b68eb51SMatthew G. Knepley PetscHashIterBegin(ht, hi);
7561b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(ht, hi)) {
7571b68eb51SMatthew G. Knepley PetscHashIterGetKey(ht, hi, point);
7581b68eb51SMatthew G. Knepley PetscHashIterNext(ht, hi);
7594bbf5ea8SMatthew G. Knepley
7604bbf5ea8SMatthew G. Knepley /* Loop over all the cells that this point connects to */
7619566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star));
7625f824522SMatthew G. Knepley for (si = 0; si < starSize * 2; si += 2) {
7634c954380SMatthew G. Knepley const PetscInt ownedpoint = star[si];
7645f824522SMatthew G. Knepley /* TODO Check for point in cht before running through closure again */
7654bbf5ea8SMatthew G. Knepley /* now loop over all entities in the closure of that cell */
7669566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, ownedpoint, PETSC_TRUE, &closureSize, &closure));
7675f824522SMatthew G. Knepley for (ci = 0; ci < closureSize * 2; ci += 2) {
7684c954380SMatthew G. Knepley const PetscInt seenpoint = closure[ci];
7695f824522SMatthew G. Knepley if (ignoredim >= 0 && seenpoint >= iStart && seenpoint < iEnd) continue;
7709566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(cht, seenpoint));
771bc7fa33aSFlorian Wechsung /* Facet integrals couple dofs across facets, so in that case for each of
772f1580f4eSBarry Smith the facets we need to add all dofs on the other side of the facet to
773f1580f4eSBarry Smith the seen dofs. */
774bc7fa33aSFlorian Wechsung if (patch->usercomputeopintfacet) {
775bc7fa33aSFlorian Wechsung if (fBegin <= seenpoint && seenpoint < fEnd) {
7769566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, seenpoint, PETSC_FALSE, &fStarSize, &fStar));
777bc7fa33aSFlorian Wechsung for (fsi = 0; fsi < fStarSize * 2; fsi += 2) {
7789566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, &fClosureSize, &fClosure));
77948a46eb9SPierre Jolivet for (fci = 0; fci < fClosureSize * 2; fci += 2) PetscCall(PetscHSetIAdd(cht, fClosure[fci]));
7809566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, NULL, &fClosure));
781bc7fa33aSFlorian Wechsung }
7829566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, seenpoint, PETSC_FALSE, NULL, &fStar));
783bc7fa33aSFlorian Wechsung }
784bc7fa33aSFlorian Wechsung }
7854bbf5ea8SMatthew G. Knepley }
7869566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, ownedpoint, PETSC_TRUE, NULL, &closure));
7874bbf5ea8SMatthew G. Knepley }
7889566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, NULL, &star));
7894bbf5ea8SMatthew G. Knepley }
7909566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm));
7913ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
7925f824522SMatthew G. Knepley }
7935f824522SMatthew G. Knepley
PCPatchGetGlobalDofs(PC pc,PetscSection dofSection[],PetscInt f,PetscBool combined,PetscInt p,PetscInt * dof,PetscInt * off)794d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchGetGlobalDofs(PC pc, PetscSection dofSection[], PetscInt f, PetscBool combined, PetscInt p, PetscInt *dof, PetscInt *off)
795d71ae5a4SJacob Faibussowitsch {
7965f824522SMatthew G. Knepley PetscFunctionBegin;
7975f824522SMatthew G. Knepley if (combined) {
7985f824522SMatthew G. Knepley if (f < 0) {
7999566063dSJacob Faibussowitsch if (dof) PetscCall(PetscSectionGetDof(dofSection[0], p, dof));
8009566063dSJacob Faibussowitsch if (off) PetscCall(PetscSectionGetOffset(dofSection[0], p, off));
8015f824522SMatthew G. Knepley } else {
8029566063dSJacob Faibussowitsch if (dof) PetscCall(PetscSectionGetFieldDof(dofSection[0], p, f, dof));
8039566063dSJacob Faibussowitsch if (off) PetscCall(PetscSectionGetFieldOffset(dofSection[0], p, f, off));
8045f824522SMatthew G. Knepley }
8055f824522SMatthew G. Knepley } else {
8065f824522SMatthew G. Knepley if (f < 0) {
8075f824522SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
8085f824522SMatthew G. Knepley PetscInt fdof, g;
8095f824522SMatthew G. Knepley
8105f824522SMatthew G. Knepley if (dof) {
8115f824522SMatthew G. Knepley *dof = 0;
8125f824522SMatthew G. Knepley for (g = 0; g < patch->nsubspaces; ++g) {
8139566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(dofSection[g], p, &fdof));
8145f824522SMatthew G. Knepley *dof += fdof;
8155f824522SMatthew G. Knepley }
8165f824522SMatthew G. Knepley }
817624e31c3SLawrence Mitchell if (off) {
818624e31c3SLawrence Mitchell *off = 0;
819624e31c3SLawrence Mitchell for (g = 0; g < patch->nsubspaces; ++g) {
8209566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(dofSection[g], p, &fdof));
821624e31c3SLawrence Mitchell *off += fdof;
822624e31c3SLawrence Mitchell }
823624e31c3SLawrence Mitchell }
8245f824522SMatthew G. Knepley } else {
8259566063dSJacob Faibussowitsch if (dof) PetscCall(PetscSectionGetDof(dofSection[f], p, dof));
8269566063dSJacob Faibussowitsch if (off) PetscCall(PetscSectionGetOffset(dofSection[f], p, off));
8275f824522SMatthew G. Knepley }
8285f824522SMatthew G. Knepley }
8293ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
8304bbf5ea8SMatthew G. Knepley }
8314bbf5ea8SMatthew G. Knepley
8324bbf5ea8SMatthew G. Knepley /* Given a hash table with a set of topological entities (pts), compute the degrees of
8334bbf5ea8SMatthew G. Knepley freedom in global concatenated numbering on those entities.
8344bbf5ea8SMatthew G. Knepley For Vanka smoothing, this needs to do something special: ignore dofs of the
8354bbf5ea8SMatthew G. Knepley constraint subspace on entities that aren't the base entity we're building the patch
8364bbf5ea8SMatthew G. Knepley around. */
PCPatchGetPointDofs(PC pc,PetscHSetI pts,PetscHSetI dofs,PetscInt base,PetscHSetI * subspaces_to_exclude)837d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchGetPointDofs(PC pc, PetscHSetI pts, PetscHSetI dofs, PetscInt base, PetscHSetI *subspaces_to_exclude)
838d71ae5a4SJacob Faibussowitsch {
8395f824522SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
8401b68eb51SMatthew G. Knepley PetscHashIter hi;
8414bbf5ea8SMatthew G. Knepley PetscInt ldof, loff;
8424bbf5ea8SMatthew G. Knepley PetscInt k, p;
8434bbf5ea8SMatthew G. Knepley
8444bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
8459566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(dofs));
8464bbf5ea8SMatthew G. Knepley for (k = 0; k < patch->nsubspaces; ++k) {
8474bbf5ea8SMatthew G. Knepley PetscInt subspaceOffset = patch->subspaceOffsets[k];
8484bbf5ea8SMatthew G. Knepley PetscInt bs = patch->bs[k];
8494bbf5ea8SMatthew G. Knepley PetscInt j, l;
8504bbf5ea8SMatthew G. Knepley
851e4c66b91SPatrick Farrell if (subspaces_to_exclude != NULL) {
852e4c66b91SPatrick Farrell PetscBool should_exclude_k = PETSC_FALSE;
8539566063dSJacob Faibussowitsch PetscCall(PetscHSetIHas(*subspaces_to_exclude, k, &should_exclude_k));
854e4c66b91SPatrick Farrell if (should_exclude_k) {
8554bbf5ea8SMatthew G. Knepley /* only get this subspace dofs at the base entity, not any others */
8569566063dSJacob Faibussowitsch PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, base, &ldof, &loff));
8574bbf5ea8SMatthew G. Knepley if (0 == ldof) continue;
8584bbf5ea8SMatthew G. Knepley for (j = loff; j < ldof + loff; ++j) {
8594bbf5ea8SMatthew G. Knepley for (l = 0; l < bs; ++l) {
8604bbf5ea8SMatthew G. Knepley PetscInt dof = bs * j + l + subspaceOffset;
8619566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(dofs, dof));
8624bbf5ea8SMatthew G. Knepley }
8634bbf5ea8SMatthew G. Knepley }
8644bbf5ea8SMatthew G. Knepley continue; /* skip the other dofs of this subspace */
8654bbf5ea8SMatthew G. Knepley }
866e4c66b91SPatrick Farrell }
8674bbf5ea8SMatthew G. Knepley
8681b68eb51SMatthew G. Knepley PetscHashIterBegin(pts, hi);
8691b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(pts, hi)) {
8701b68eb51SMatthew G. Knepley PetscHashIterGetKey(pts, hi, p);
8711b68eb51SMatthew G. Knepley PetscHashIterNext(pts, hi);
8729566063dSJacob Faibussowitsch PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, p, &ldof, &loff));
8734bbf5ea8SMatthew G. Knepley if (0 == ldof) continue;
8744bbf5ea8SMatthew G. Knepley for (j = loff; j < ldof + loff; ++j) {
8754bbf5ea8SMatthew G. Knepley for (l = 0; l < bs; ++l) {
8764bbf5ea8SMatthew G. Knepley PetscInt dof = bs * j + l + subspaceOffset;
8779566063dSJacob Faibussowitsch PetscCall(PetscHSetIAdd(dofs, dof));
8784bbf5ea8SMatthew G. Knepley }
8794bbf5ea8SMatthew G. Knepley }
8804bbf5ea8SMatthew G. Knepley }
8814bbf5ea8SMatthew G. Knepley }
8823ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
8834bbf5ea8SMatthew G. Knepley }
8844bbf5ea8SMatthew G. Knepley
8854bbf5ea8SMatthew G. Knepley /* Given two hash tables A and B, compute the keys in B that are not in A, and put them in C */
PCPatchComputeSetDifference_Private(PetscHSetI A,PetscHSetI B,PetscHSetI C)886d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchComputeSetDifference_Private(PetscHSetI A, PetscHSetI B, PetscHSetI C)
887d71ae5a4SJacob Faibussowitsch {
8881b68eb51SMatthew G. Knepley PetscHashIter hi;
8891b68eb51SMatthew G. Knepley PetscInt key;
8904bbf5ea8SMatthew G. Knepley PetscBool flg;
8914bbf5ea8SMatthew G. Knepley
8924bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
8939566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(C));
8941b68eb51SMatthew G. Knepley PetscHashIterBegin(B, hi);
8951b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(B, hi)) {
8961b68eb51SMatthew G. Knepley PetscHashIterGetKey(B, hi, key);
8971b68eb51SMatthew G. Knepley PetscHashIterNext(B, hi);
8989566063dSJacob Faibussowitsch PetscCall(PetscHSetIHas(A, key, &flg));
8999566063dSJacob Faibussowitsch if (!flg) PetscCall(PetscHSetIAdd(C, key));
9004bbf5ea8SMatthew G. Knepley }
9013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
9024bbf5ea8SMatthew G. Knepley }
9034bbf5ea8SMatthew G. Knepley
90404c3f3b8SBarry Smith // PetscClangLinter pragma disable: -fdoc-sowing-chars
9054bbf5ea8SMatthew G. Knepley /*
906f1580f4eSBarry Smith PCPatchCreateCellPatches - create patches.
907f1580f4eSBarry Smith
908f1580f4eSBarry Smith Input Parameter:
909f1580f4eSBarry Smith . dm - The DMPlex object defining the mesh
910f1580f4eSBarry Smith
911f1580f4eSBarry Smith Output Parameters:
912f1580f4eSBarry Smith + cellCounts - Section with counts of cells around each vertex
913f1580f4eSBarry Smith . cells - IS of the cell point indices of cells in each patch
914f1580f4eSBarry Smith . pointCounts - Section with counts of cells around each vertex
915f1580f4eSBarry Smith - point - IS of the cell point indices of cells in each patch
9164bbf5ea8SMatthew G. Knepley */
PCPatchCreateCellPatches(PC pc)917d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCreateCellPatches(PC pc)
918d71ae5a4SJacob Faibussowitsch {
9194bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
9205f824522SMatthew G. Knepley DMLabel ghost = NULL;
9214bbf5ea8SMatthew G. Knepley DM dm, plex;
92276ce8f1aSJose E. Roman PetscHSetI ht = NULL, cht = NULL;
9230e126c0bSLawrence Mitchell PetscSection cellCounts, pointCounts, intFacetCounts, extFacetCounts;
924eb62eeaaSLawrence Mitchell PetscInt *cellsArray, *pointsArray, *intFacetsArray, *extFacetsArray, *intFacetsToPatchCell;
9250e126c0bSLawrence Mitchell PetscInt numCells, numPoints, numIntFacets, numExtFacets;
9265f824522SMatthew G. Knepley const PetscInt *leaves;
92733cbca70SPatrick Farrell PetscInt nleaves, pStart, pEnd, cStart, cEnd, vStart, vEnd, fStart, fEnd, v;
9285f824522SMatthew G. Knepley PetscBool isFiredrake;
9294bbf5ea8SMatthew G. Knepley
9304bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
9314bbf5ea8SMatthew G. Knepley /* Used to keep track of the cells in the patch. */
9329566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&ht));
9339566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&cht));
9344bbf5ea8SMatthew G. Knepley
9359566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm));
93628b400f6SJacob Faibussowitsch PetscCheck(dm, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "DM not yet set on patch PC");
9379566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex));
938b6bb21d1SLawrence Mitchell dm = plex;
9399566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
9409566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
9414bbf5ea8SMatthew G. Knepley
9424bbf5ea8SMatthew G. Knepley if (patch->user_patches) {
9439566063dSJacob Faibussowitsch PetscCall(patch->userpatchconstructionop(pc, &patch->npatch, &patch->userIS, &patch->iterationSet, patch->userpatchconstructctx));
9449371c9d4SSatish Balay vStart = 0;
9459371c9d4SSatish Balay vEnd = patch->npatch;
946e5b9877fSPatrick Farrell } else if (patch->ctype == PC_PATCH_PARDECOMP) {
9479371c9d4SSatish Balay vStart = 0;
9489371c9d4SSatish Balay vEnd = 1;
9495f824522SMatthew G. Knepley } else if (patch->codim < 0) {
9509566063dSJacob Faibussowitsch if (patch->dim < 0) PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
9519566063dSJacob Faibussowitsch else PetscCall(DMPlexGetDepthStratum(dm, patch->dim, &vStart, &vEnd));
9529566063dSJacob Faibussowitsch } else PetscCall(DMPlexGetHeightStratum(dm, patch->codim, &vStart, &vEnd));
9535f824522SMatthew G. Knepley patch->npatch = vEnd - vStart;
9544bbf5ea8SMatthew G. Knepley
9554bbf5ea8SMatthew G. Knepley /* These labels mark the owned points. We only create patches around points that this process owns. */
9569566063dSJacob Faibussowitsch PetscCall(DMHasLabel(dm, "pyop2_ghost", &isFiredrake));
9575f824522SMatthew G. Knepley if (isFiredrake) {
9589566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, "pyop2_ghost", &ghost));
9599566063dSJacob Faibussowitsch PetscCall(DMLabelCreateIndex(ghost, pStart, pEnd));
9605f824522SMatthew G. Knepley } else {
9615f824522SMatthew G. Knepley PetscSF sf;
9625f824522SMatthew G. Knepley
9639566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm, &sf));
9649566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL));
9655f824522SMatthew G. Knepley nleaves = PetscMax(nleaves, 0);
9665f824522SMatthew G. Knepley }
9674bbf5ea8SMatthew G. Knepley
9689566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->cellCounts));
9699566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->cellCounts, "Patch Cell Layout"));
9704bbf5ea8SMatthew G. Knepley cellCounts = patch->cellCounts;
9719566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(cellCounts, vStart, vEnd));
9729566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->pointCounts));
9739566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->pointCounts, "Patch Point Layout"));
9745f824522SMatthew G. Knepley pointCounts = patch->pointCounts;
9759566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(pointCounts, vStart, vEnd));
9769566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->extFacetCounts));
9779566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->extFacetCounts, "Patch Exterior Facet Layout"));
9780e126c0bSLawrence Mitchell extFacetCounts = patch->extFacetCounts;
9799566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(extFacetCounts, vStart, vEnd));
9809566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->intFacetCounts));
9819566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->intFacetCounts, "Patch Interior Facet Layout"));
9820e126c0bSLawrence Mitchell intFacetCounts = patch->intFacetCounts;
9839566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(intFacetCounts, vStart, vEnd));
9845f824522SMatthew G. Knepley /* Count cells and points in the patch surrounding each entity */
9859566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
9864bbf5ea8SMatthew G. Knepley for (v = vStart; v < vEnd; ++v) {
9871b68eb51SMatthew G. Knepley PetscHashIter hi;
9885f824522SMatthew G. Knepley PetscInt chtSize, loc = -1;
9895f824522SMatthew G. Knepley PetscBool flg;
9904bbf5ea8SMatthew G. Knepley
991b525f888SPatrick Farrell if (!patch->user_patches && patch->ctype != PC_PATCH_PARDECOMP) {
9929566063dSJacob Faibussowitsch if (ghost) PetscCall(DMLabelHasPoint(ghost, v, &flg));
9939371c9d4SSatish Balay else {
9949371c9d4SSatish Balay PetscCall(PetscFindInt(v, nleaves, leaves, &loc));
9959371c9d4SSatish Balay flg = loc >= 0 ? PETSC_TRUE : PETSC_FALSE;
9969371c9d4SSatish Balay }
9974bbf5ea8SMatthew G. Knepley /* Not an owned entity, don't make a cell patch. */
9984bbf5ea8SMatthew G. Knepley if (flg) continue;
9994bbf5ea8SMatthew G. Knepley }
10004bbf5ea8SMatthew G. Knepley
10019566063dSJacob Faibussowitsch PetscCall(patch->patchconstructop((void *)patch, dm, v, ht));
10029566063dSJacob Faibussowitsch PetscCall(PCPatchCompleteCellPatch(pc, ht, cht));
10039566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(cht, &chtSize));
10044bbf5ea8SMatthew G. Knepley /* empty patch, continue */
10054bbf5ea8SMatthew G. Knepley if (chtSize == 0) continue;
10064bbf5ea8SMatthew G. Knepley
10074bbf5ea8SMatthew G. Knepley /* safe because size(cht) > 0 from above */
10081b68eb51SMatthew G. Knepley PetscHashIterBegin(cht, hi);
10091b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(cht, hi)) {
10105f824522SMatthew G. Knepley PetscInt point, pdof;
10114bbf5ea8SMatthew G. Knepley
10121b68eb51SMatthew G. Knepley PetscHashIterGetKey(cht, hi, point);
10130e126c0bSLawrence Mitchell if (fStart <= point && point < fEnd) {
10140e126c0bSLawrence Mitchell const PetscInt *support;
10150e126c0bSLawrence Mitchell PetscInt supportSize, p;
10160e126c0bSLawrence Mitchell PetscBool interior = PETSC_TRUE;
10179566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, point, &support));
10189566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
10190e126c0bSLawrence Mitchell if (supportSize == 1) {
10200e126c0bSLawrence Mitchell interior = PETSC_FALSE;
10210e126c0bSLawrence Mitchell } else {
10220e126c0bSLawrence Mitchell for (p = 0; p < supportSize; p++) {
10230e126c0bSLawrence Mitchell PetscBool found;
10240e126c0bSLawrence Mitchell /* FIXME: can I do this while iterating over cht? */
10259566063dSJacob Faibussowitsch PetscCall(PetscHSetIHas(cht, support[p], &found));
10260e126c0bSLawrence Mitchell if (!found) {
10270e126c0bSLawrence Mitchell interior = PETSC_FALSE;
10280e126c0bSLawrence Mitchell break;
10290e126c0bSLawrence Mitchell }
10300e126c0bSLawrence Mitchell }
10310e126c0bSLawrence Mitchell }
10320e126c0bSLawrence Mitchell if (interior) {
10339566063dSJacob Faibussowitsch PetscCall(PetscSectionAddDof(intFacetCounts, v, 1));
10340e126c0bSLawrence Mitchell } else {
10359566063dSJacob Faibussowitsch PetscCall(PetscSectionAddDof(extFacetCounts, v, 1));
10360e126c0bSLawrence Mitchell }
10370e126c0bSLawrence Mitchell }
10389566063dSJacob Faibussowitsch PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL));
10399566063dSJacob Faibussowitsch if (pdof) PetscCall(PetscSectionAddDof(pointCounts, v, 1));
10409566063dSJacob Faibussowitsch if (point >= cStart && point < cEnd) PetscCall(PetscSectionAddDof(cellCounts, v, 1));
10411b68eb51SMatthew G. Knepley PetscHashIterNext(cht, hi);
10424bbf5ea8SMatthew G. Knepley }
10434bbf5ea8SMatthew G. Knepley }
10449566063dSJacob Faibussowitsch if (isFiredrake) PetscCall(DMLabelDestroyIndex(ghost));
10454bbf5ea8SMatthew G. Knepley
10469566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(cellCounts));
10479566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(cellCounts, &numCells));
10489566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numCells, &cellsArray));
10499566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(pointCounts));
10509566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(pointCounts, &numPoints));
10519566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPoints, &pointsArray));
10524bbf5ea8SMatthew G. Knepley
10539566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(intFacetCounts));
10549566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(extFacetCounts));
10559566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(intFacetCounts, &numIntFacets));
10569566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(extFacetCounts, &numExtFacets));
10579566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numIntFacets, &intFacetsArray));
10589566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numIntFacets * 2, &intFacetsToPatchCell));
10599566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numExtFacets, &extFacetsArray));
10600e126c0bSLawrence Mitchell
10614bbf5ea8SMatthew G. Knepley /* Now that we know how much space we need, run through again and actually remember the cells. */
10624bbf5ea8SMatthew G. Knepley for (v = vStart; v < vEnd; v++) {
10631b68eb51SMatthew G. Knepley PetscHashIter hi;
10640e126c0bSLawrence Mitchell PetscInt dof, off, cdof, coff, efdof, efoff, ifdof, ifoff, pdof, n = 0, cn = 0, ifn = 0, efn = 0;
10654bbf5ea8SMatthew G. Knepley
10669566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(pointCounts, v, &dof));
10679566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(pointCounts, v, &off));
10689566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(cellCounts, v, &cdof));
10699566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(cellCounts, v, &coff));
10709566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(intFacetCounts, v, &ifdof));
10719566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(intFacetCounts, v, &ifoff));
10729566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(extFacetCounts, v, &efdof));
10739566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(extFacetCounts, v, &efoff));
10745f824522SMatthew G. Knepley if (dof <= 0) continue;
10759566063dSJacob Faibussowitsch PetscCall(patch->patchconstructop((void *)patch, dm, v, ht));
10769566063dSJacob Faibussowitsch PetscCall(PCPatchCompleteCellPatch(pc, ht, cht));
10771b68eb51SMatthew G. Knepley PetscHashIterBegin(cht, hi);
10781b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(cht, hi)) {
10794bbf5ea8SMatthew G. Knepley PetscInt point;
10804bbf5ea8SMatthew G. Knepley
10811b68eb51SMatthew G. Knepley PetscHashIterGetKey(cht, hi, point);
10820e126c0bSLawrence Mitchell if (fStart <= point && point < fEnd) {
10830e126c0bSLawrence Mitchell const PetscInt *support;
10840e126c0bSLawrence Mitchell PetscInt supportSize, p;
10850e126c0bSLawrence Mitchell PetscBool interior = PETSC_TRUE;
10869566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, point, &support));
10879566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
10880e126c0bSLawrence Mitchell if (supportSize == 1) {
10890e126c0bSLawrence Mitchell interior = PETSC_FALSE;
10900e126c0bSLawrence Mitchell } else {
10910e126c0bSLawrence Mitchell for (p = 0; p < supportSize; p++) {
10920e126c0bSLawrence Mitchell PetscBool found;
10930e126c0bSLawrence Mitchell /* FIXME: can I do this while iterating over cht? */
10949566063dSJacob Faibussowitsch PetscCall(PetscHSetIHas(cht, support[p], &found));
10950e126c0bSLawrence Mitchell if (!found) {
10960e126c0bSLawrence Mitchell interior = PETSC_FALSE;
10970e126c0bSLawrence Mitchell break;
10980e126c0bSLawrence Mitchell }
10990e126c0bSLawrence Mitchell }
11000e126c0bSLawrence Mitchell }
11010e126c0bSLawrence Mitchell if (interior) {
110244b625f7SLawrence Mitchell intFacetsToPatchCell[2 * (ifoff + ifn)] = support[0];
110344b625f7SLawrence Mitchell intFacetsToPatchCell[2 * (ifoff + ifn) + 1] = support[1];
11040e126c0bSLawrence Mitchell intFacetsArray[ifoff + ifn++] = point;
11050e126c0bSLawrence Mitchell } else {
11060e126c0bSLawrence Mitchell extFacetsArray[efoff + efn++] = point;
11070e126c0bSLawrence Mitchell }
11080e126c0bSLawrence Mitchell }
11099566063dSJacob Faibussowitsch PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL));
1110ad540459SPierre Jolivet if (pdof) pointsArray[off + n++] = point;
1111ad540459SPierre Jolivet if (point >= cStart && point < cEnd) cellsArray[coff + cn++] = point;
11121b68eb51SMatthew G. Knepley PetscHashIterNext(cht, hi);
11134bbf5ea8SMatthew G. Knepley }
111463a3b9bcSJacob 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);
111563a3b9bcSJacob 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);
111663a3b9bcSJacob 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);
111763a3b9bcSJacob 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);
1118eb62eeaaSLawrence Mitchell
1119eb62eeaaSLawrence Mitchell for (ifn = 0; ifn < ifdof; ifn++) {
112044b625f7SLawrence Mitchell PetscInt cell0 = intFacetsToPatchCell[2 * (ifoff + ifn)];
112144b625f7SLawrence Mitchell PetscInt cell1 = intFacetsToPatchCell[2 * (ifoff + ifn) + 1];
1122eb62eeaaSLawrence Mitchell PetscBool found0 = PETSC_FALSE, found1 = PETSC_FALSE;
1123eb62eeaaSLawrence Mitchell for (n = 0; n < cdof; n++) {
11247c54fef0SLawrence Mitchell if (!found0 && cell0 == cellsArray[coff + n]) {
1125c3faab33SLawrence Mitchell intFacetsToPatchCell[2 * (ifoff + ifn)] = n;
1126eb62eeaaSLawrence Mitchell found0 = PETSC_TRUE;
1127eb62eeaaSLawrence Mitchell }
11287c54fef0SLawrence Mitchell if (!found1 && cell1 == cellsArray[coff + n]) {
1129c3faab33SLawrence Mitchell intFacetsToPatchCell[2 * (ifoff + ifn) + 1] = n;
113080fc4459SLawrence Mitchell found1 = PETSC_TRUE;
1131eb62eeaaSLawrence Mitchell }
1132eb62eeaaSLawrence Mitchell if (found0 && found1) break;
1133eb62eeaaSLawrence Mitchell }
11347827d75bSBarry Smith PetscCheck(found0 && found1, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Didn't manage to find local point numbers for facet support");
1135eb62eeaaSLawrence Mitchell }
11364bbf5ea8SMatthew G. Knepley }
11379566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&ht));
11389566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&cht));
11395f824522SMatthew G. Knepley
11409566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numCells, cellsArray, PETSC_OWN_POINTER, &patch->cells));
11419566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->cells, "Patch Cells"));
11425f824522SMatthew G. Knepley if (patch->viewCells) {
11439566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject)patch->cellCounts, patch->viewerCells, patch->formatCells));
11449566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject)patch->cells, patch->viewerCells, patch->formatCells));
11455f824522SMatthew G. Knepley }
11469566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray, PETSC_OWN_POINTER, &patch->intFacets));
11479566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->intFacets, "Patch Interior Facets"));
11489566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 2 * numIntFacets, intFacetsToPatchCell, PETSC_OWN_POINTER, &patch->intFacetsToPatchCell));
11499566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->intFacetsToPatchCell, "Patch Interior Facets local support"));
11500e126c0bSLawrence Mitchell if (patch->viewIntFacets) {
11519566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject)patch->intFacetCounts, patch->viewerIntFacets, patch->formatIntFacets));
11529566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject)patch->intFacets, patch->viewerIntFacets, patch->formatIntFacets));
11539566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject)patch->intFacetsToPatchCell, patch->viewerIntFacets, patch->formatIntFacets));
11540e126c0bSLawrence Mitchell }
11559566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numExtFacets, extFacetsArray, PETSC_OWN_POINTER, &patch->extFacets));
11569566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->extFacets, "Patch Exterior Facets"));
11570e126c0bSLawrence Mitchell if (patch->viewExtFacets) {
11589566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject)patch->extFacetCounts, patch->viewerExtFacets, patch->formatExtFacets));
11599566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject)patch->extFacets, patch->viewerExtFacets, patch->formatExtFacets));
11600e126c0bSLawrence Mitchell }
11619566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints, pointsArray, PETSC_OWN_POINTER, &patch->points));
11629566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->points, "Patch Points"));
11635f824522SMatthew G. Knepley if (patch->viewPoints) {
11649566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject)patch->pointCounts, patch->viewerPoints, patch->formatPoints));
11659566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject)patch->points, patch->viewerPoints, patch->formatPoints));
11665f824522SMatthew G. Knepley }
11679566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm));
11683ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
11694bbf5ea8SMatthew G. Knepley }
11704bbf5ea8SMatthew G. Knepley
11714bbf5ea8SMatthew G. Knepley /*
1172f1580f4eSBarry Smith PCPatchCreateCellPatchDiscretisationInfo - Build the dof maps for cell patches
1173f1580f4eSBarry Smith
1174f1580f4eSBarry Smith Input Parameters:
1175f1580f4eSBarry Smith + dm - The DMPlex object defining the mesh
1176f1580f4eSBarry Smith . cellCounts - Section with counts of cells around each vertex
1177f1580f4eSBarry Smith . cells - IS of the cell point indices of cells in each patch
1178f1580f4eSBarry Smith . cellNumbering - Section mapping plex cell points to Firedrake cell indices.
1179f1580f4eSBarry Smith . nodesPerCell - number of nodes per cell.
1180f1580f4eSBarry Smith - cellNodeMap - map from cells to node indices (nodesPerCell * numCells)
1181f1580f4eSBarry Smith
1182f1580f4eSBarry Smith Output Parameters:
1183f1580f4eSBarry Smith + dofs - IS of local dof numbers of each cell in the patch, where local is a patch local numbering
1184f1580f4eSBarry Smith . gtolCounts - Section with counts of dofs per cell patch
1185f1580f4eSBarry Smith - gtol - IS mapping from global dofs to local dofs for each patch.
11864bbf5ea8SMatthew G. Knepley */
PCPatchCreateCellPatchDiscretisationInfo(PC pc)1187d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCreateCellPatchDiscretisationInfo(PC pc)
1188d71ae5a4SJacob Faibussowitsch {
11894bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
11904bbf5ea8SMatthew G. Knepley PetscSection cellCounts = patch->cellCounts;
11915f824522SMatthew G. Knepley PetscSection pointCounts = patch->pointCounts;
11920904074fSPatrick Farrell PetscSection gtolCounts, gtolCountsWithArtificial = NULL, gtolCountsWithAll = NULL;
11934bbf5ea8SMatthew G. Knepley IS cells = patch->cells;
11945f824522SMatthew G. Knepley IS points = patch->points;
11954bbf5ea8SMatthew G. Knepley PetscSection cellNumbering = patch->cellNumbering;
11965f824522SMatthew G. Knepley PetscInt Nf = patch->nsubspaces;
11975f824522SMatthew G. Knepley PetscInt numCells, numPoints;
11984bbf5ea8SMatthew G. Knepley PetscInt numDofs;
11990904074fSPatrick Farrell PetscInt numGlobalDofs, numGlobalDofsWithArtificial, numGlobalDofsWithAll;
12004bbf5ea8SMatthew G. Knepley PetscInt totalDofsPerCell = patch->totalDofsPerCell;
12014bbf5ea8SMatthew G. Knepley PetscInt vStart, vEnd, v;
12025f824522SMatthew G. Knepley const PetscInt *cellsArray, *pointsArray;
12034bbf5ea8SMatthew G. Knepley PetscInt *newCellsArray = NULL;
12044bbf5ea8SMatthew G. Knepley PetscInt *dofsArray = NULL;
1205c2e6f3c0SFlorian Wechsung PetscInt *dofsArrayWithArtificial = NULL;
12060904074fSPatrick Farrell PetscInt *dofsArrayWithAll = NULL;
12075f824522SMatthew G. Knepley PetscInt *offsArray = NULL;
1208c2e6f3c0SFlorian Wechsung PetscInt *offsArrayWithArtificial = NULL;
12090904074fSPatrick Farrell PetscInt *offsArrayWithAll = NULL;
12104bbf5ea8SMatthew G. Knepley PetscInt *asmArray = NULL;
1211c2e6f3c0SFlorian Wechsung PetscInt *asmArrayWithArtificial = NULL;
12120904074fSPatrick Farrell PetscInt *asmArrayWithAll = NULL;
12134bbf5ea8SMatthew G. Knepley PetscInt *globalDofsArray = NULL;
1214c2e6f3c0SFlorian Wechsung PetscInt *globalDofsArrayWithArtificial = NULL;
12150904074fSPatrick Farrell PetscInt *globalDofsArrayWithAll = NULL;
12164bbf5ea8SMatthew G. Knepley PetscInt globalIndex = 0;
12174bbf5ea8SMatthew G. Knepley PetscInt key = 0;
12184bbf5ea8SMatthew G. Knepley PetscInt asmKey = 0;
1219b6bb21d1SLawrence Mitchell DM dm = NULL, plex;
1220557beb66SLawrence Mitchell const PetscInt *bcNodes = NULL;
12211b68eb51SMatthew G. Knepley PetscHMapI ht;
1222c2e6f3c0SFlorian Wechsung PetscHMapI htWithArtificial;
12230904074fSPatrick Farrell PetscHMapI htWithAll;
12241b68eb51SMatthew G. Knepley PetscHSetI globalBcs;
1225557beb66SLawrence Mitchell PetscInt numBcs;
12261b68eb51SMatthew G. Knepley PetscHSetI ownedpts, seenpts, owneddofs, seendofs, artificialbcs;
1227cda239d9SMatthew G. Knepley PetscInt pStart, pEnd, p, i;
122810534d48SPatrick Farrell char option[PETSC_MAX_PATH_LEN];
122939fd2e8aSPatrick Farrell PetscBool isNonlinear;
12304bbf5ea8SMatthew G. Knepley
12314bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
12329566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm));
12339566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex));
1234b6bb21d1SLawrence Mitchell dm = plex;
12354bbf5ea8SMatthew G. Knepley /* dofcounts section is cellcounts section * dofPerCell */
12369566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(cellCounts, &numCells));
12379566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(patch->pointCounts, &numPoints));
12384bbf5ea8SMatthew G. Knepley numDofs = numCells * totalDofsPerCell;
12399566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numDofs, &dofsArray));
12409566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPoints * Nf, &offsArray));
12419566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numDofs, &asmArray));
12429566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numCells, &newCellsArray));
12439566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(cellCounts, &vStart, &vEnd));
12449566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCounts));
12454bbf5ea8SMatthew G. Knepley gtolCounts = patch->gtolCounts;
12469566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(gtolCounts, vStart, vEnd));
12479566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->gtolCounts, "Patch Global Index Section"));
12484bbf5ea8SMatthew G. Knepley
1249b6bb21d1SLawrence Mitchell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
12509566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPoints * Nf, &offsArrayWithArtificial));
12519566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numDofs, &asmArrayWithArtificial));
12529566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numDofs, &dofsArrayWithArtificial));
12539566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithArtificial));
1254c2e6f3c0SFlorian Wechsung gtolCountsWithArtificial = patch->gtolCountsWithArtificial;
12559566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(gtolCountsWithArtificial, vStart, vEnd));
12569566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->gtolCountsWithArtificial, "Patch Global Index Section Including Artificial BCs"));
1257c2e6f3c0SFlorian Wechsung }
1258c2e6f3c0SFlorian Wechsung
12590904074fSPatrick Farrell isNonlinear = patch->isNonlinear;
1260b6bb21d1SLawrence Mitchell if (isNonlinear) {
12619566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPoints * Nf, &offsArrayWithAll));
12629566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numDofs, &asmArrayWithAll));
12639566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numDofs, &dofsArrayWithAll));
12649566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithAll));
12650904074fSPatrick Farrell gtolCountsWithAll = patch->gtolCountsWithAll;
12669566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(gtolCountsWithAll, vStart, vEnd));
12679566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->gtolCountsWithAll, "Patch Global Index Section Including All BCs"));
12680904074fSPatrick Farrell }
12690904074fSPatrick Farrell
1270557beb66SLawrence Mitchell /* Outside the patch loop, get the dofs that are globally-enforced Dirichlet
1271557beb66SLawrence Mitchell conditions */
12729566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&globalBcs));
12739566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->ghostBcNodes, &bcNodes));
12749566063dSJacob Faibussowitsch PetscCall(ISGetSize(patch->ghostBcNodes, &numBcs));
1275ac530a7eSPierre Jolivet for (i = 0; i < numBcs; ++i) PetscCall(PetscHSetIAdd(globalBcs, bcNodes[i])); /* these are already in concatenated numbering */
12769566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->ghostBcNodes, &bcNodes));
12779566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->ghostBcNodes)); /* memory optimisation */
1278557beb66SLawrence Mitchell
1279557beb66SLawrence Mitchell /* Hash tables for artificial BC construction */
12809566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&ownedpts));
12819566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&seenpts));
12829566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&owneddofs));
12839566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&seendofs));
12849566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&artificialbcs));
1285557beb66SLawrence Mitchell
12869566063dSJacob Faibussowitsch PetscCall(ISGetIndices(cells, &cellsArray));
12879566063dSJacob Faibussowitsch PetscCall(ISGetIndices(points, &pointsArray));
12889566063dSJacob Faibussowitsch PetscCall(PetscHMapICreate(&ht));
12899566063dSJacob Faibussowitsch PetscCall(PetscHMapICreate(&htWithArtificial));
12909566063dSJacob Faibussowitsch PetscCall(PetscHMapICreate(&htWithAll));
12914bbf5ea8SMatthew G. Knepley for (v = vStart; v < vEnd; ++v) {
12924bbf5ea8SMatthew G. Knepley PetscInt localIndex = 0;
1293c2e6f3c0SFlorian Wechsung PetscInt localIndexWithArtificial = 0;
12940904074fSPatrick Farrell PetscInt localIndexWithAll = 0;
12954bbf5ea8SMatthew G. Knepley PetscInt dof, off, i, j, k, l;
12964bbf5ea8SMatthew G. Knepley
12979566063dSJacob Faibussowitsch PetscCall(PetscHMapIClear(ht));
12989566063dSJacob Faibussowitsch PetscCall(PetscHMapIClear(htWithArtificial));
12999566063dSJacob Faibussowitsch PetscCall(PetscHMapIClear(htWithAll));
13009566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(cellCounts, v, &dof));
13019566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(cellCounts, v, &off));
13024bbf5ea8SMatthew G. Knepley if (dof <= 0) continue;
13034bbf5ea8SMatthew G. Knepley
1304557beb66SLawrence Mitchell /* Calculate the global numbers of the artificial BC dofs here first */
13059566063dSJacob Faibussowitsch PetscCall(patch->patchconstructop((void *)patch, dm, v, ownedpts));
13069566063dSJacob Faibussowitsch PetscCall(PCPatchCompleteCellPatch(pc, ownedpts, seenpts));
13079566063dSJacob Faibussowitsch PetscCall(PCPatchGetPointDofs(pc, ownedpts, owneddofs, v, &patch->subspaces_to_exclude));
13089566063dSJacob Faibussowitsch PetscCall(PCPatchGetPointDofs(pc, seenpts, seendofs, v, NULL));
13099566063dSJacob Faibussowitsch PetscCall(PCPatchComputeSetDifference_Private(owneddofs, seendofs, artificialbcs));
13108135ed82SLawrence Mitchell if (patch->viewPatches) {
13111b68eb51SMatthew G. Knepley PetscHSetI globalbcdofs;
13121b68eb51SMatthew G. Knepley PetscHashIter hi;
13138135ed82SLawrence Mitchell MPI_Comm comm = PetscObjectComm((PetscObject)pc);
13141b68eb51SMatthew G. Knepley
13159566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&globalbcdofs));
131663a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": owned dofs:\n", v));
13171b68eb51SMatthew G. Knepley PetscHashIterBegin(owneddofs, hi);
13181b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(owneddofs, hi)) {
13198135ed82SLawrence Mitchell PetscInt globalDof;
13208135ed82SLawrence Mitchell
13211b68eb51SMatthew G. Knepley PetscHashIterGetKey(owneddofs, hi, globalDof);
13221b68eb51SMatthew G. Knepley PetscHashIterNext(owneddofs, hi);
132363a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof));
13248135ed82SLawrence Mitchell }
13259566063dSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "\n"));
132663a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": seen dofs:\n", v));
13271b68eb51SMatthew G. Knepley PetscHashIterBegin(seendofs, hi);
13281b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(seendofs, hi)) {
13298135ed82SLawrence Mitchell PetscInt globalDof;
13308135ed82SLawrence Mitchell PetscBool flg;
13318135ed82SLawrence Mitchell
13321b68eb51SMatthew G. Knepley PetscHashIterGetKey(seendofs, hi, globalDof);
13331b68eb51SMatthew G. Knepley PetscHashIterNext(seendofs, hi);
133463a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof));
13358135ed82SLawrence Mitchell
13369566063dSJacob Faibussowitsch PetscCall(PetscHSetIHas(globalBcs, globalDof, &flg));
13379566063dSJacob Faibussowitsch if (flg) PetscCall(PetscHSetIAdd(globalbcdofs, globalDof));
13388135ed82SLawrence Mitchell }
13399566063dSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "\n"));
134063a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": global BCs:\n", v));
13419566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(globalbcdofs, &numBcs));
13428135ed82SLawrence Mitchell if (numBcs > 0) {
13431b68eb51SMatthew G. Knepley PetscHashIterBegin(globalbcdofs, hi);
13441b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(globalbcdofs, hi)) {
13458135ed82SLawrence Mitchell PetscInt globalDof;
13461b68eb51SMatthew G. Knepley PetscHashIterGetKey(globalbcdofs, hi, globalDof);
13471b68eb51SMatthew G. Knepley PetscHashIterNext(globalbcdofs, hi);
134863a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof));
13498135ed82SLawrence Mitchell }
13508135ed82SLawrence Mitchell }
13519566063dSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "\n"));
135263a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": artificial BCs:\n", v));
13539566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(artificialbcs, &numBcs));
13548135ed82SLawrence Mitchell if (numBcs > 0) {
13551b68eb51SMatthew G. Knepley PetscHashIterBegin(artificialbcs, hi);
13561b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(artificialbcs, hi)) {
13578135ed82SLawrence Mitchell PetscInt globalDof;
13581b68eb51SMatthew G. Knepley PetscHashIterGetKey(artificialbcs, hi, globalDof);
13591b68eb51SMatthew G. Knepley PetscHashIterNext(artificialbcs, hi);
136063a3b9bcSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof));
13618135ed82SLawrence Mitchell }
13628135ed82SLawrence Mitchell }
13639566063dSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(comm, "\n\n"));
13649566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&globalbcdofs));
13658135ed82SLawrence Mitchell }
13664bbf5ea8SMatthew G. Knepley for (k = 0; k < patch->nsubspaces; ++k) {
13674bbf5ea8SMatthew G. Knepley const PetscInt *cellNodeMap = patch->cellNodeMap[k];
13684bbf5ea8SMatthew G. Knepley PetscInt nodesPerCell = patch->nodesPerCell[k];
13694bbf5ea8SMatthew G. Knepley PetscInt subspaceOffset = patch->subspaceOffsets[k];
13704bbf5ea8SMatthew G. Knepley PetscInt bs = patch->bs[k];
13714bbf5ea8SMatthew G. Knepley
13724bbf5ea8SMatthew G. Knepley for (i = off; i < off + dof; ++i) {
13734bbf5ea8SMatthew G. Knepley /* Walk over the cells in this patch. */
13744bbf5ea8SMatthew G. Knepley const PetscInt c = cellsArray[i];
13755f824522SMatthew G. Knepley PetscInt cell = c;
13764bbf5ea8SMatthew G. Knepley
13775f824522SMatthew G. Knepley /* TODO Change this to an IS */
13785f824522SMatthew G. Knepley if (cellNumbering) {
13799566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(cellNumbering, c, &cell));
138063a3b9bcSJacob Faibussowitsch PetscCheck(cell > 0, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Cell %" PetscInt_FMT " doesn't appear in cell numbering map", c);
13819566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(cellNumbering, c, &cell));
13825f824522SMatthew G. Knepley }
13834bbf5ea8SMatthew G. Knepley newCellsArray[i] = cell;
13844bbf5ea8SMatthew G. Knepley for (j = 0; j < nodesPerCell; ++j) {
13854bbf5ea8SMatthew G. Knepley /* For each global dof, map it into contiguous local storage. */
13864bbf5ea8SMatthew G. Knepley const PetscInt globalDof = cellNodeMap[cell * nodesPerCell + j] * bs + subspaceOffset;
13874bbf5ea8SMatthew G. Knepley /* finally, loop over block size */
13884bbf5ea8SMatthew G. Knepley for (l = 0; l < bs; ++l) {
13891b68eb51SMatthew G. Knepley PetscInt localDof;
13901b68eb51SMatthew G. Knepley PetscBool isGlobalBcDof, isArtificialBcDof;
13914bbf5ea8SMatthew G. Knepley
1392557beb66SLawrence Mitchell /* first, check if this is either a globally enforced or locally enforced BC dof */
13939566063dSJacob Faibussowitsch PetscCall(PetscHSetIHas(globalBcs, globalDof + l, &isGlobalBcDof));
13949566063dSJacob Faibussowitsch PetscCall(PetscHSetIHas(artificialbcs, globalDof + l, &isArtificialBcDof));
1395557beb66SLawrence Mitchell
1396557beb66SLawrence Mitchell /* if it's either, don't ever give it a local dof number */
13971b68eb51SMatthew G. Knepley if (isGlobalBcDof || isArtificialBcDof) {
1398c2e6f3c0SFlorian Wechsung dofsArray[globalIndex] = -1; /* don't use this in assembly in this patch */
1399557beb66SLawrence Mitchell } else {
14009566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(ht, globalDof + l, &localDof));
14014bbf5ea8SMatthew G. Knepley if (localDof == -1) {
14024bbf5ea8SMatthew G. Knepley localDof = localIndex++;
14039566063dSJacob Faibussowitsch PetscCall(PetscHMapISet(ht, globalDof + l, localDof));
14044bbf5ea8SMatthew G. Knepley }
140563a3b9bcSJacob Faibussowitsch PetscCheck(globalIndex < numDofs, PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %" PetscInt_FMT " than expected %" PetscInt_FMT, globalIndex + 1, numDofs);
14064bbf5ea8SMatthew G. Knepley /* And store. */
1407c2e6f3c0SFlorian Wechsung dofsArray[globalIndex] = localDof;
14084bbf5ea8SMatthew G. Knepley }
1409c2e6f3c0SFlorian Wechsung
14100904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1411c2e6f3c0SFlorian Wechsung if (isGlobalBcDof) {
1412e047a90bSFlorian Wechsung dofsArrayWithArtificial[globalIndex] = -1; /* don't use this in assembly in this patch */
1413c2e6f3c0SFlorian Wechsung } else {
14149566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(htWithArtificial, globalDof + l, &localDof));
1415c2e6f3c0SFlorian Wechsung if (localDof == -1) {
1416c2e6f3c0SFlorian Wechsung localDof = localIndexWithArtificial++;
14179566063dSJacob Faibussowitsch PetscCall(PetscHMapISet(htWithArtificial, globalDof + l, localDof));
1418c2e6f3c0SFlorian Wechsung }
141963a3b9bcSJacob Faibussowitsch PetscCheck(globalIndex < numDofs, PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %" PetscInt_FMT " than expected %" PetscInt_FMT, globalIndex + 1, numDofs);
1420c2e6f3c0SFlorian Wechsung /* And store.*/
1421c2e6f3c0SFlorian Wechsung dofsArrayWithArtificial[globalIndex] = localDof;
1422c2e6f3c0SFlorian Wechsung }
1423c2e6f3c0SFlorian Wechsung }
14240904074fSPatrick Farrell
14250904074fSPatrick Farrell if (isNonlinear) {
14260904074fSPatrick Farrell /* Build the dofmap for the function space with _all_ dofs,
14270904074fSPatrick Farrell including those in any kind of boundary condition */
14289566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(htWithAll, globalDof + l, &localDof));
14290904074fSPatrick Farrell if (localDof == -1) {
14300904074fSPatrick Farrell localDof = localIndexWithAll++;
14319566063dSJacob Faibussowitsch PetscCall(PetscHMapISet(htWithAll, globalDof + l, localDof));
14320904074fSPatrick Farrell }
143363a3b9bcSJacob Faibussowitsch PetscCheck(globalIndex < numDofs, PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %" PetscInt_FMT " than expected %" PetscInt_FMT, globalIndex + 1, numDofs);
14340904074fSPatrick Farrell /* And store.*/
14350904074fSPatrick Farrell dofsArrayWithAll[globalIndex] = localDof;
14360904074fSPatrick Farrell }
1437c2e6f3c0SFlorian Wechsung globalIndex++;
14384bbf5ea8SMatthew G. Knepley }
14394bbf5ea8SMatthew G. Knepley }
14404bbf5ea8SMatthew G. Knepley }
1441557beb66SLawrence Mitchell }
14424bbf5ea8SMatthew G. Knepley /* How many local dofs in this patch? */
14430904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
14449566063dSJacob Faibussowitsch PetscCall(PetscHMapIGetSize(htWithArtificial, &dof));
14459566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(gtolCountsWithArtificial, v, dof));
1446c2e6f3c0SFlorian Wechsung }
14470904074fSPatrick Farrell if (isNonlinear) {
14489566063dSJacob Faibussowitsch PetscCall(PetscHMapIGetSize(htWithAll, &dof));
14499566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(gtolCountsWithAll, v, dof));
14500904074fSPatrick Farrell }
14519566063dSJacob Faibussowitsch PetscCall(PetscHMapIGetSize(ht, &dof));
14529566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(gtolCounts, v, dof));
14534bbf5ea8SMatthew G. Knepley }
1454b6bb21d1SLawrence Mitchell
14559566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm));
145663a3b9bcSJacob 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);
14579566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(gtolCounts));
14589566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(gtolCounts, &numGlobalDofs));
14599566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numGlobalDofs, &globalDofsArray));
14604bbf5ea8SMatthew G. Knepley
14610904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
14629566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(gtolCountsWithArtificial));
14639566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(gtolCountsWithArtificial, &numGlobalDofsWithArtificial));
14649566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numGlobalDofsWithArtificial, &globalDofsArrayWithArtificial));
1465c2e6f3c0SFlorian Wechsung }
14660904074fSPatrick Farrell if (isNonlinear) {
14679566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(gtolCountsWithAll));
14689566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(gtolCountsWithAll, &numGlobalDofsWithAll));
14699566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numGlobalDofsWithAll, &globalDofsArrayWithAll));
14700904074fSPatrick Farrell }
14714bbf5ea8SMatthew 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. */
14724bbf5ea8SMatthew G. Knepley for (v = vStart; v < vEnd; ++v) {
14731b68eb51SMatthew G. Knepley PetscHashIter hi;
14745f824522SMatthew G. Knepley PetscInt dof, off, Np, ooff, i, j, k, l;
14754bbf5ea8SMatthew G. Knepley
14769566063dSJacob Faibussowitsch PetscCall(PetscHMapIClear(ht));
14779566063dSJacob Faibussowitsch PetscCall(PetscHMapIClear(htWithArtificial));
14789566063dSJacob Faibussowitsch PetscCall(PetscHMapIClear(htWithAll));
14799566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(cellCounts, v, &dof));
14809566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(cellCounts, v, &off));
14819566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(pointCounts, v, &Np));
14829566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(pointCounts, v, &ooff));
14834bbf5ea8SMatthew G. Knepley if (dof <= 0) continue;
14844bbf5ea8SMatthew G. Knepley
14854bbf5ea8SMatthew G. Knepley for (k = 0; k < patch->nsubspaces; ++k) {
14864bbf5ea8SMatthew G. Knepley const PetscInt *cellNodeMap = patch->cellNodeMap[k];
14874bbf5ea8SMatthew G. Knepley PetscInt nodesPerCell = patch->nodesPerCell[k];
14884bbf5ea8SMatthew G. Knepley PetscInt subspaceOffset = patch->subspaceOffsets[k];
14894bbf5ea8SMatthew G. Knepley PetscInt bs = patch->bs[k];
1490d490bb3dSLawrence Mitchell PetscInt goff;
14914bbf5ea8SMatthew G. Knepley
14924bbf5ea8SMatthew G. Knepley for (i = off; i < off + dof; ++i) {
14934bbf5ea8SMatthew G. Knepley /* Reconstruct mapping of global-to-local on this patch. */
14944bbf5ea8SMatthew G. Knepley const PetscInt c = cellsArray[i];
14955f824522SMatthew G. Knepley PetscInt cell = c;
14964bbf5ea8SMatthew G. Knepley
14979566063dSJacob Faibussowitsch if (cellNumbering) PetscCall(PetscSectionGetOffset(cellNumbering, c, &cell));
14984bbf5ea8SMatthew G. Knepley for (j = 0; j < nodesPerCell; ++j) {
14994bbf5ea8SMatthew G. Knepley for (l = 0; l < bs; ++l) {
15005f824522SMatthew G. Knepley const PetscInt globalDof = cellNodeMap[cell * nodesPerCell + j] * bs + l + subspaceOffset;
1501c2e6f3c0SFlorian Wechsung const PetscInt localDof = dofsArray[key];
15029566063dSJacob Faibussowitsch if (localDof >= 0) PetscCall(PetscHMapISet(ht, globalDof, localDof));
15030904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1504c2e6f3c0SFlorian Wechsung const PetscInt localDofWithArtificial = dofsArrayWithArtificial[key];
150548a46eb9SPierre Jolivet if (localDofWithArtificial >= 0) PetscCall(PetscHMapISet(htWithArtificial, globalDof, localDofWithArtificial));
1506c2e6f3c0SFlorian Wechsung }
15070904074fSPatrick Farrell if (isNonlinear) {
15080904074fSPatrick Farrell const PetscInt localDofWithAll = dofsArrayWithAll[key];
150948a46eb9SPierre Jolivet if (localDofWithAll >= 0) PetscCall(PetscHMapISet(htWithAll, globalDof, localDofWithAll));
15100904074fSPatrick Farrell }
1511c2e6f3c0SFlorian Wechsung key++;
15124bbf5ea8SMatthew G. Knepley }
15134bbf5ea8SMatthew G. Knepley }
15144bbf5ea8SMatthew G. Knepley }
1515557beb66SLawrence Mitchell
15164bbf5ea8SMatthew G. Knepley /* Shove it in the output data structure. */
15179566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(gtolCounts, v, &goff));
15181b68eb51SMatthew G. Knepley PetscHashIterBegin(ht, hi);
15191b68eb51SMatthew G. Knepley while (!PetscHashIterAtEnd(ht, hi)) {
15204bbf5ea8SMatthew G. Knepley PetscInt globalDof, localDof;
15214bbf5ea8SMatthew G. Knepley
15221b68eb51SMatthew G. Knepley PetscHashIterGetKey(ht, hi, globalDof);
15231b68eb51SMatthew G. Knepley PetscHashIterGetVal(ht, hi, localDof);
15244bbf5ea8SMatthew G. Knepley if (globalDof >= 0) globalDofsArray[goff + localDof] = globalDof;
15251b68eb51SMatthew G. Knepley PetscHashIterNext(ht, hi);
15264bbf5ea8SMatthew G. Knepley }
15275f824522SMatthew G. Knepley
15280904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
15299566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(gtolCountsWithArtificial, v, &goff));
1530c2e6f3c0SFlorian Wechsung PetscHashIterBegin(htWithArtificial, hi);
1531c2e6f3c0SFlorian Wechsung while (!PetscHashIterAtEnd(htWithArtificial, hi)) {
1532c2e6f3c0SFlorian Wechsung PetscInt globalDof, localDof;
1533c2e6f3c0SFlorian Wechsung PetscHashIterGetKey(htWithArtificial, hi, globalDof);
1534c2e6f3c0SFlorian Wechsung PetscHashIterGetVal(htWithArtificial, hi, localDof);
1535c2e6f3c0SFlorian Wechsung if (globalDof >= 0) globalDofsArrayWithArtificial[goff + localDof] = globalDof;
1536c2e6f3c0SFlorian Wechsung PetscHashIterNext(htWithArtificial, hi);
1537c2e6f3c0SFlorian Wechsung }
1538c2e6f3c0SFlorian Wechsung }
15390904074fSPatrick Farrell if (isNonlinear) {
15409566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(gtolCountsWithAll, v, &goff));
15410904074fSPatrick Farrell PetscHashIterBegin(htWithAll, hi);
15420904074fSPatrick Farrell while (!PetscHashIterAtEnd(htWithAll, hi)) {
15430904074fSPatrick Farrell PetscInt globalDof, localDof;
15440904074fSPatrick Farrell PetscHashIterGetKey(htWithAll, hi, globalDof);
15450904074fSPatrick Farrell PetscHashIterGetVal(htWithAll, hi, localDof);
15460904074fSPatrick Farrell if (globalDof >= 0) globalDofsArrayWithAll[goff + localDof] = globalDof;
15470904074fSPatrick Farrell PetscHashIterNext(htWithAll, hi);
15480904074fSPatrick Farrell }
15490904074fSPatrick Farrell }
1550c2e6f3c0SFlorian Wechsung
15515f824522SMatthew G. Knepley for (p = 0; p < Np; ++p) {
15525f824522SMatthew G. Knepley const PetscInt point = pointsArray[ooff + p];
15535f824522SMatthew G. Knepley PetscInt globalDof, localDof;
15545f824522SMatthew G. Knepley
15559566063dSJacob Faibussowitsch PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, point, NULL, &globalDof));
15569566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(ht, globalDof, &localDof));
15575f824522SMatthew G. Knepley offsArray[(ooff + p) * Nf + k] = localDof;
15580904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
15599566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(htWithArtificial, globalDof, &localDof));
1560c2e6f3c0SFlorian Wechsung offsArrayWithArtificial[(ooff + p) * Nf + k] = localDof;
1561c2e6f3c0SFlorian Wechsung }
15620904074fSPatrick Farrell if (isNonlinear) {
15639566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(htWithAll, globalDof, &localDof));
15640904074fSPatrick Farrell offsArrayWithAll[(ooff + p) * Nf + k] = localDof;
15650904074fSPatrick Farrell }
15665f824522SMatthew G. Knepley }
15674bbf5ea8SMatthew G. Knepley }
15684bbf5ea8SMatthew G. Knepley
15699566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&globalBcs));
15709566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&ownedpts));
15719566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&seenpts));
15729566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&owneddofs));
15739566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&seendofs));
15749566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&artificialbcs));
1575557beb66SLawrence Mitchell
15764bbf5ea8SMatthew G. Knepley /* At this point, we have a hash table ht built that maps globalDof -> localDof.
15774bbf5ea8SMatthew G. Knepley We need to create the dof table laid out cellwise first, then by subspace,
15784bbf5ea8SMatthew G. Knepley as the assembler assembles cell-wise and we need to stuff the different
15794bbf5ea8SMatthew G. Knepley contributions of the different function spaces to the right places. So we loop
15804bbf5ea8SMatthew G. Knepley over cells, then over subspaces. */
15814bbf5ea8SMatthew G. Knepley if (patch->nsubspaces > 1) { /* for nsubspaces = 1, data we need is already in dofsArray */
15824bbf5ea8SMatthew G. Knepley for (i = off; i < off + dof; ++i) {
15834bbf5ea8SMatthew G. Knepley const PetscInt c = cellsArray[i];
15845f824522SMatthew G. Knepley PetscInt cell = c;
15854bbf5ea8SMatthew G. Knepley
15869566063dSJacob Faibussowitsch if (cellNumbering) PetscCall(PetscSectionGetOffset(cellNumbering, c, &cell));
15874bbf5ea8SMatthew G. Knepley for (k = 0; k < patch->nsubspaces; ++k) {
15884bbf5ea8SMatthew G. Knepley const PetscInt *cellNodeMap = patch->cellNodeMap[k];
15894bbf5ea8SMatthew G. Knepley PetscInt nodesPerCell = patch->nodesPerCell[k];
15904bbf5ea8SMatthew G. Knepley PetscInt subspaceOffset = patch->subspaceOffsets[k];
15914bbf5ea8SMatthew G. Knepley PetscInt bs = patch->bs[k];
15924bbf5ea8SMatthew G. Knepley
15934bbf5ea8SMatthew G. Knepley for (j = 0; j < nodesPerCell; ++j) {
15944bbf5ea8SMatthew G. Knepley for (l = 0; l < bs; ++l) {
15955f824522SMatthew G. Knepley const PetscInt globalDof = cellNodeMap[cell * nodesPerCell + j] * bs + l + subspaceOffset;
15964bbf5ea8SMatthew G. Knepley PetscInt localDof;
15974bbf5ea8SMatthew G. Knepley
15989566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(ht, globalDof, &localDof));
1599557beb66SLawrence Mitchell /* If it's not in the hash table, i.e. is a BC dof,
16001b68eb51SMatthew G. Knepley then the PetscHSetIMap above gives -1, which matches
1601557beb66SLawrence Mitchell exactly the convention for PETSc's matrix assembly to
1602557beb66SLawrence Mitchell ignore the dof. So we don't need to do anything here */
1603c2e6f3c0SFlorian Wechsung asmArray[asmKey] = localDof;
16040904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
16059566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(htWithArtificial, globalDof, &localDof));
1606c2e6f3c0SFlorian Wechsung asmArrayWithArtificial[asmKey] = localDof;
1607c2e6f3c0SFlorian Wechsung }
16080904074fSPatrick Farrell if (isNonlinear) {
16099566063dSJacob Faibussowitsch PetscCall(PetscHMapIGet(htWithAll, globalDof, &localDof));
16100904074fSPatrick Farrell asmArrayWithAll[asmKey] = localDof;
16110904074fSPatrick Farrell }
1612c2e6f3c0SFlorian Wechsung asmKey++;
16134bbf5ea8SMatthew G. Knepley }
16144bbf5ea8SMatthew G. Knepley }
16154bbf5ea8SMatthew G. Knepley }
16164bbf5ea8SMatthew G. Knepley }
16174bbf5ea8SMatthew G. Knepley }
16184bbf5ea8SMatthew G. Knepley }
1619c2e6f3c0SFlorian Wechsung if (1 == patch->nsubspaces) {
16209566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(asmArray, dofsArray, numDofs));
162148a46eb9SPierre Jolivet if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) PetscCall(PetscArraycpy(asmArrayWithArtificial, dofsArrayWithArtificial, numDofs));
16221baa6e33SBarry Smith if (isNonlinear) PetscCall(PetscArraycpy(asmArrayWithAll, dofsArrayWithAll, numDofs));
1623c2e6f3c0SFlorian Wechsung }
16244bbf5ea8SMatthew G. Knepley
16259566063dSJacob Faibussowitsch PetscCall(PetscHMapIDestroy(&ht));
16269566063dSJacob Faibussowitsch PetscCall(PetscHMapIDestroy(&htWithArtificial));
16279566063dSJacob Faibussowitsch PetscCall(PetscHMapIDestroy(&htWithAll));
16289566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(cells, &cellsArray));
16299566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(points, &pointsArray));
16309566063dSJacob Faibussowitsch PetscCall(PetscFree(dofsArray));
163148a46eb9SPierre Jolivet if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) PetscCall(PetscFree(dofsArrayWithArtificial));
16321baa6e33SBarry Smith if (isNonlinear) PetscCall(PetscFree(dofsArrayWithAll));
16335f824522SMatthew G. Knepley /* Create placeholder section for map from points to patch dofs */
16349566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->patchSection));
16359566063dSJacob Faibussowitsch PetscCall(PetscSectionSetNumFields(patch->patchSection, patch->nsubspaces));
16361e5fa6bbSLawrence Mitchell if (patch->combined) {
16371e5fa6bbSLawrence Mitchell PetscInt numFields;
16389566063dSJacob Faibussowitsch PetscCall(PetscSectionGetNumFields(patch->dofSection[0], &numFields));
163963a3b9bcSJacob 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);
16409566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->dofSection[0], &pStart, &pEnd));
16419566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(patch->patchSection, pStart, pEnd));
16425f824522SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) {
16435f824522SMatthew G. Knepley PetscInt dof, fdof, f;
16445f824522SMatthew G. Knepley
16459566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->dofSection[0], p, &dof));
16469566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(patch->patchSection, p, dof));
16475f824522SMatthew G. Knepley for (f = 0; f < patch->nsubspaces; ++f) {
16489566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldDof(patch->dofSection[0], p, f, &fdof));
16499566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(patch->patchSection, p, f, fdof));
16505f824522SMatthew G. Knepley }
16511e5fa6bbSLawrence Mitchell }
16521e5fa6bbSLawrence Mitchell } else {
16531e5fa6bbSLawrence Mitchell PetscInt pStartf, pEndf, f;
16541690c2aeSBarry Smith pStart = PETSC_INT_MAX;
16551690c2aeSBarry Smith pEnd = PETSC_INT_MIN;
16561e5fa6bbSLawrence Mitchell for (f = 0; f < patch->nsubspaces; ++f) {
16579566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf));
16581e5fa6bbSLawrence Mitchell pStart = PetscMin(pStart, pStartf);
16591e5fa6bbSLawrence Mitchell pEnd = PetscMax(pEnd, pEndf);
16601e5fa6bbSLawrence Mitchell }
16619566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(patch->patchSection, pStart, pEnd));
16621e5fa6bbSLawrence Mitchell for (f = 0; f < patch->nsubspaces; ++f) {
16639566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf));
16641e5fa6bbSLawrence Mitchell for (p = pStartf; p < pEndf; ++p) {
16651e5fa6bbSLawrence Mitchell PetscInt fdof;
16669566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->dofSection[f], p, &fdof));
16679566063dSJacob Faibussowitsch PetscCall(PetscSectionAddDof(patch->patchSection, p, fdof));
16689566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(patch->patchSection, p, f, fdof));
1669bdd9e0cdSPatrick Farrell }
1670bdd9e0cdSPatrick Farrell }
16715f824522SMatthew G. Knepley }
16729566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(patch->patchSection));
16739566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUseFieldOffsets(patch->patchSection, PETSC_TRUE));
16744bbf5ea8SMatthew G. Knepley /* Replace cell indices with firedrake-numbered ones. */
16759566063dSJacob Faibussowitsch PetscCall(ISGeneralSetIndices(cells, numCells, (const PetscInt *)newCellsArray, PETSC_OWN_POINTER));
16769566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofs, globalDofsArray, PETSC_OWN_POINTER, &patch->gtol));
16779566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)patch->gtol, "Global Indices"));
16789566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_g2l_view", patch->classname));
16799566063dSJacob Faibussowitsch PetscCall(PetscSectionViewFromOptions(patch->gtolCounts, (PetscObject)pc, option));
16809566063dSJacob Faibussowitsch PetscCall(ISViewFromOptions(patch->gtol, (PetscObject)pc, option));
16819566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArray, PETSC_OWN_POINTER, &patch->dofs));
16829566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints * Nf, offsArray, PETSC_OWN_POINTER, &patch->offs));
16830904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
16849566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithArtificial, globalDofsArrayWithArtificial, PETSC_OWN_POINTER, &patch->gtolWithArtificial));
16859566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithArtificial, PETSC_OWN_POINTER, &patch->dofsWithArtificial));
16869566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints * Nf, offsArrayWithArtificial, PETSC_OWN_POINTER, &patch->offsWithArtificial));
1687c2e6f3c0SFlorian Wechsung }
16880904074fSPatrick Farrell if (isNonlinear) {
16899566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithAll, globalDofsArrayWithAll, PETSC_OWN_POINTER, &patch->gtolWithAll));
16909566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithAll, PETSC_OWN_POINTER, &patch->dofsWithAll));
16919566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints * Nf, offsArrayWithAll, PETSC_OWN_POINTER, &patch->offsWithAll));
16920904074fSPatrick Farrell }
16933ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
16944bbf5ea8SMatthew G. Knepley }
16954bbf5ea8SMatthew G. Knepley
PCPatchCreateMatrix_Private(PC pc,PetscInt point,Mat * mat,PetscBool withArtificial)1696d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCreateMatrix_Private(PC pc, PetscInt point, Mat *mat, PetscBool withArtificial)
1697d71ae5a4SJacob Faibussowitsch {
16984bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
16994bbf5ea8SMatthew G. Knepley PetscBool flg;
17004bbf5ea8SMatthew G. Knepley PetscInt csize, rsize;
17014bbf5ea8SMatthew G. Knepley const char *prefix = NULL;
17024bbf5ea8SMatthew G. Knepley
17034bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
1704c2e6f3c0SFlorian Wechsung if (withArtificial) {
1705e047a90bSFlorian Wechsung /* would be nice if we could create a rectangular matrix of size numDofsWithArtificial x numDofs here */
17069d4fc724SLawrence Mitchell PetscInt pStart;
17079566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->gtolCountsWithArtificial, &pStart, NULL));
17089566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, point + pStart, &rsize));
17099d4fc724SLawrence Mitchell csize = rsize;
1710ff201f6aSFlorian Wechsung } else {
17119d4fc724SLawrence Mitchell PetscInt pStart;
17129566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, NULL));
17139566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCounts, point + pStart, &rsize));
17149d4fc724SLawrence Mitchell csize = rsize;
1715c2e6f3c0SFlorian Wechsung }
1716c2e6f3c0SFlorian Wechsung
17179566063dSJacob Faibussowitsch PetscCall(MatCreate(PETSC_COMM_SELF, mat));
17189566063dSJacob Faibussowitsch PetscCall(PCGetOptionsPrefix(pc, &prefix));
17199566063dSJacob Faibussowitsch PetscCall(MatSetOptionsPrefix(*mat, prefix));
17209566063dSJacob Faibussowitsch PetscCall(MatAppendOptionsPrefix(*mat, "pc_patch_sub_"));
17219566063dSJacob Faibussowitsch if (patch->sub_mat_type) PetscCall(MatSetType(*mat, patch->sub_mat_type));
17229566063dSJacob Faibussowitsch else if (!patch->sub_mat_type) PetscCall(MatSetType(*mat, MATDENSE));
17239566063dSJacob Faibussowitsch PetscCall(MatSetSizes(*mat, rsize, csize, rsize, csize));
17249566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)*mat, MATDENSE, &flg));
17259566063dSJacob Faibussowitsch if (!flg) PetscCall(PetscObjectTypeCompare((PetscObject)*mat, MATSEQDENSE, &flg));
17264bbf5ea8SMatthew G. Knepley /* Sparse patch matrices */
17274bbf5ea8SMatthew G. Knepley if (!flg) {
17284bbf5ea8SMatthew G. Knepley PetscBT bt;
17294bbf5ea8SMatthew G. Knepley PetscInt *dnnz = NULL;
17304bbf5ea8SMatthew G. Knepley const PetscInt *dofsArray = NULL;
17314bbf5ea8SMatthew G. Knepley PetscInt pStart, pEnd, ncell, offset, c, i, j;
17324bbf5ea8SMatthew G. Knepley
1733c2e6f3c0SFlorian Wechsung if (withArtificial) {
17349566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->dofsWithArtificial, &dofsArray));
1735ff201f6aSFlorian Wechsung } else {
17369566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->dofs, &dofsArray));
1737c2e6f3c0SFlorian Wechsung }
17389566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd));
17394bbf5ea8SMatthew G. Knepley point += pStart;
174063a3b9bcSJacob Faibussowitsch PetscCheck(point < pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %" PetscInt_FMT " not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, pStart, pEnd);
17419566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->cellCounts, point, &ncell));
17429566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->cellCounts, point, &offset));
17439566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_Prealloc, pc, 0, 0, 0));
1744b2866507SPatrick Farrell /* A PetscBT uses N^2 bits to store the sparsity pattern on a
17454bbf5ea8SMatthew G. Knepley * patch. This is probably OK if the patches are not too big,
1746b2866507SPatrick Farrell * but uses too much memory. We therefore switch based on rsize. */
1747b2866507SPatrick Farrell if (rsize < 3000) { /* FIXME: I picked this switch value out of my hat */
1748d63cebbaSPatrick Farrell PetscScalar *zeroes;
1749d63cebbaSPatrick Farrell PetscInt rows;
1750d63cebbaSPatrick Farrell
17519566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(rsize, &dnnz));
17529566063dSJacob Faibussowitsch PetscCall(PetscBTCreate(rsize * rsize, &bt));
17534bbf5ea8SMatthew G. Knepley for (c = 0; c < ncell; ++c) {
17544bbf5ea8SMatthew G. Knepley const PetscInt *idx = dofsArray + (offset + c) * patch->totalDofsPerCell;
17554bbf5ea8SMatthew G. Knepley for (i = 0; i < patch->totalDofsPerCell; ++i) {
17564bbf5ea8SMatthew G. Knepley const PetscInt row = idx[i];
1757557beb66SLawrence Mitchell if (row < 0) continue;
17584bbf5ea8SMatthew G. Knepley for (j = 0; j < patch->totalDofsPerCell; ++j) {
17594bbf5ea8SMatthew G. Knepley const PetscInt col = idx[j];
17604bbf5ea8SMatthew G. Knepley const PetscInt key = row * rsize + col;
1761557beb66SLawrence Mitchell if (col < 0) continue;
17624bbf5ea8SMatthew G. Knepley if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
17634bbf5ea8SMatthew G. Knepley }
17644bbf5ea8SMatthew G. Knepley }
17654bbf5ea8SMatthew G. Knepley }
1766d63cebbaSPatrick Farrell
1767d63cebbaSPatrick Farrell if (patch->usercomputeopintfacet) {
1768d63cebbaSPatrick Farrell const PetscInt *intFacetsArray = NULL;
1769d63cebbaSPatrick Farrell PetscInt i, numIntFacets, intFacetOffset;
1770d63cebbaSPatrick Farrell const PetscInt *facetCells = NULL;
1771d63cebbaSPatrick Farrell
17729566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets));
17739566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset));
17749566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells));
17759566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray));
1776d63cebbaSPatrick Farrell for (i = 0; i < numIntFacets; i++) {
1777d63cebbaSPatrick Farrell const PetscInt cell0 = facetCells[2 * (intFacetOffset + i) + 0];
1778d63cebbaSPatrick Farrell const PetscInt cell1 = facetCells[2 * (intFacetOffset + i) + 1];
1779d63cebbaSPatrick Farrell PetscInt celli, cellj;
1780d63cebbaSPatrick Farrell
1781d63cebbaSPatrick Farrell for (celli = 0; celli < patch->totalDofsPerCell; celli++) {
1782d63cebbaSPatrick Farrell const PetscInt row = dofsArray[(offset + cell0) * patch->totalDofsPerCell + celli];
1783b5c64f08SPatrick Farrell if (row < 0) continue;
1784d63cebbaSPatrick Farrell for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) {
1785d63cebbaSPatrick Farrell const PetscInt col = dofsArray[(offset + cell1) * patch->totalDofsPerCell + cellj];
1786d63cebbaSPatrick Farrell const PetscInt key = row * rsize + col;
1787d63cebbaSPatrick Farrell if (col < 0) continue;
1788d63cebbaSPatrick Farrell if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
1789d63cebbaSPatrick Farrell }
1790d63cebbaSPatrick Farrell }
1791d63cebbaSPatrick Farrell
1792d63cebbaSPatrick Farrell for (celli = 0; celli < patch->totalDofsPerCell; celli++) {
1793d63cebbaSPatrick Farrell const PetscInt row = dofsArray[(offset + cell1) * patch->totalDofsPerCell + celli];
1794b5c64f08SPatrick Farrell if (row < 0) continue;
1795d63cebbaSPatrick Farrell for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) {
1796d63cebbaSPatrick Farrell const PetscInt col = dofsArray[(offset + cell0) * patch->totalDofsPerCell + cellj];
1797d63cebbaSPatrick Farrell const PetscInt key = row * rsize + col;
1798d63cebbaSPatrick Farrell if (col < 0) continue;
1799d63cebbaSPatrick Farrell if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
1800d63cebbaSPatrick Farrell }
1801d63cebbaSPatrick Farrell }
1802d63cebbaSPatrick Farrell }
1803d63cebbaSPatrick Farrell }
18049566063dSJacob Faibussowitsch PetscCall(PetscBTDestroy(&bt));
18059566063dSJacob Faibussowitsch PetscCall(MatXAIJSetPreallocation(*mat, 1, dnnz, NULL, NULL, NULL));
18069566063dSJacob Faibussowitsch PetscCall(PetscFree(dnnz));
1807d63cebbaSPatrick Farrell
18089566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(patch->totalDofsPerCell * patch->totalDofsPerCell, &zeroes));
1809d63cebbaSPatrick Farrell for (c = 0; c < ncell; ++c) {
1810d63cebbaSPatrick Farrell const PetscInt *idx = &dofsArray[(offset + c) * patch->totalDofsPerCell];
18119566063dSJacob Faibussowitsch PetscCall(MatSetValues(*mat, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, zeroes, INSERT_VALUES));
1812d63cebbaSPatrick Farrell }
18139566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(*mat, &rows, NULL));
181448a46eb9SPierre Jolivet for (i = 0; i < rows; ++i) PetscCall(MatSetValues(*mat, 1, &i, 1, &i, zeroes, INSERT_VALUES));
1815d63cebbaSPatrick Farrell
1816d63cebbaSPatrick Farrell if (patch->usercomputeopintfacet) {
1817d63cebbaSPatrick Farrell const PetscInt *intFacetsArray = NULL;
1818d63cebbaSPatrick Farrell PetscInt i, numIntFacets, intFacetOffset;
1819d63cebbaSPatrick Farrell const PetscInt *facetCells = NULL;
1820d63cebbaSPatrick Farrell
18219566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets));
18229566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset));
18239566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells));
18249566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray));
1825d63cebbaSPatrick Farrell for (i = 0; i < numIntFacets; i++) {
1826d63cebbaSPatrick Farrell const PetscInt cell0 = facetCells[2 * (intFacetOffset + i) + 0];
1827d63cebbaSPatrick Farrell const PetscInt cell1 = facetCells[2 * (intFacetOffset + i) + 1];
1828d63cebbaSPatrick Farrell const PetscInt *cell0idx = &dofsArray[(offset + cell0) * patch->totalDofsPerCell];
1829d63cebbaSPatrick Farrell const PetscInt *cell1idx = &dofsArray[(offset + cell1) * patch->totalDofsPerCell];
18309566063dSJacob Faibussowitsch PetscCall(MatSetValues(*mat, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, zeroes, INSERT_VALUES));
18319566063dSJacob Faibussowitsch PetscCall(MatSetValues(*mat, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, zeroes, INSERT_VALUES));
1832d63cebbaSPatrick Farrell }
1833d63cebbaSPatrick Farrell }
1834d63cebbaSPatrick Farrell
18359566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY));
18369566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY));
1837d63cebbaSPatrick Farrell
18389566063dSJacob Faibussowitsch PetscCall(PetscFree(zeroes));
1839d63cebbaSPatrick Farrell
1840b2866507SPatrick Farrell } else { /* rsize too big, use MATPREALLOCATOR */
1841b2866507SPatrick Farrell Mat preallocator;
1842b2866507SPatrick Farrell PetscScalar *vals;
1843b2866507SPatrick Farrell
18449566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(patch->totalDofsPerCell * patch->totalDofsPerCell, &vals));
18459566063dSJacob Faibussowitsch PetscCall(MatCreate(PETSC_COMM_SELF, &preallocator));
18469566063dSJacob Faibussowitsch PetscCall(MatSetType(preallocator, MATPREALLOCATOR));
18479566063dSJacob Faibussowitsch PetscCall(MatSetSizes(preallocator, rsize, rsize, rsize, rsize));
18489566063dSJacob Faibussowitsch PetscCall(MatSetUp(preallocator));
184911bcd083SPatrick Farrell
1850b2866507SPatrick Farrell for (c = 0; c < ncell; ++c) {
1851b2866507SPatrick Farrell const PetscInt *idx = dofsArray + (offset + c) * patch->totalDofsPerCell;
18529566063dSJacob Faibussowitsch PetscCall(MatSetValues(preallocator, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, vals, INSERT_VALUES));
1853b2866507SPatrick Farrell }
185411bcd083SPatrick Farrell
185511bcd083SPatrick Farrell if (patch->usercomputeopintfacet) {
185611bcd083SPatrick Farrell const PetscInt *intFacetsArray = NULL;
185711bcd083SPatrick Farrell PetscInt i, numIntFacets, intFacetOffset;
185811bcd083SPatrick Farrell const PetscInt *facetCells = NULL;
185911bcd083SPatrick Farrell
18609566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets));
18619566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset));
18629566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells));
18639566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray));
186411bcd083SPatrick Farrell for (i = 0; i < numIntFacets; i++) {
186511bcd083SPatrick Farrell const PetscInt cell0 = facetCells[2 * (intFacetOffset + i) + 0];
186611bcd083SPatrick Farrell const PetscInt cell1 = facetCells[2 * (intFacetOffset + i) + 1];
186711bcd083SPatrick Farrell const PetscInt *cell0idx = &dofsArray[(offset + cell0) * patch->totalDofsPerCell];
186811bcd083SPatrick Farrell const PetscInt *cell1idx = &dofsArray[(offset + cell1) * patch->totalDofsPerCell];
18699566063dSJacob Faibussowitsch PetscCall(MatSetValues(preallocator, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, vals, INSERT_VALUES));
18709566063dSJacob Faibussowitsch PetscCall(MatSetValues(preallocator, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, vals, INSERT_VALUES));
187111bcd083SPatrick Farrell }
187211bcd083SPatrick Farrell }
187311bcd083SPatrick Farrell
18749566063dSJacob Faibussowitsch PetscCall(PetscFree(vals));
18759566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY));
18769566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY));
18779566063dSJacob Faibussowitsch PetscCall(MatPreallocatorPreallocate(preallocator, PETSC_TRUE, *mat));
18789566063dSJacob Faibussowitsch PetscCall(MatDestroy(&preallocator));
1879b2866507SPatrick Farrell }
18809566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_Prealloc, pc, 0, 0, 0));
1881fe117d09SFlorian Wechsung if (withArtificial) {
18829566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->dofsWithArtificial, &dofsArray));
1883fe117d09SFlorian Wechsung } else {
18849566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->dofs, &dofsArray));
18854bbf5ea8SMatthew G. Knepley }
1886fe117d09SFlorian Wechsung }
18879566063dSJacob Faibussowitsch PetscCall(MatSetUp(*mat));
18883ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
18894bbf5ea8SMatthew G. Knepley }
18904bbf5ea8SMatthew G. Knepley
PCPatchComputeFunction_DMPlex_Private(PC pc,PetscInt patchNum,Vec x,Vec F,IS cellIS,PetscInt n,const PetscInt * l2p,const PetscInt * l2pWithAll,PetscCtx ctx)1891*2a8381b2SBarry Smith static PetscErrorCode PCPatchComputeFunction_DMPlex_Private(PC pc, PetscInt patchNum, Vec x, Vec F, IS cellIS, PetscInt n, const PetscInt *l2p, const PetscInt *l2pWithAll, PetscCtx ctx)
1892d71ae5a4SJacob Faibussowitsch {
189392d50984SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
1894b6bb21d1SLawrence Mitchell DM dm, plex;
189592d50984SMatthew G. Knepley PetscSection s;
189692d50984SMatthew G. Knepley const PetscInt *parray, *oarray;
189792d50984SMatthew G. Knepley PetscInt Nf = patch->nsubspaces, Np, poff, p, f;
189892d50984SMatthew G. Knepley
189992d50984SMatthew G. Knepley PetscFunctionBegin;
190028b400f6SJacob Faibussowitsch PetscCheck(!patch->precomputeElementTensors, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Precomputing element tensors not implemented with DMPlex compute operator");
19019566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm));
19029566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex));
1903b6bb21d1SLawrence Mitchell dm = plex;
19049566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, &s));
190592d50984SMatthew G. Knepley /* Set offset into patch */
19069566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->pointCounts, patchNum, &Np));
19079566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->pointCounts, patchNum, &poff));
19089566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->points, &parray));
19099566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->offs, &oarray));
191092d50984SMatthew G. Knepley for (f = 0; f < Nf; ++f) {
191192d50984SMatthew G. Knepley for (p = 0; p < Np; ++p) {
191292d50984SMatthew G. Knepley const PetscInt point = parray[poff + p];
191392d50984SMatthew G. Knepley PetscInt dof;
191492d50984SMatthew G. Knepley
19159566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldDof(patch->patchSection, point, f, &dof));
19169566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff + p) * Nf + f]));
19179566063dSJacob Faibussowitsch if (patch->nsubspaces == 1) PetscCall(PetscSectionSetOffset(patch->patchSection, point, oarray[(poff + p) * Nf + f]));
19189566063dSJacob Faibussowitsch else PetscCall(PetscSectionSetOffset(patch->patchSection, point, -1));
191992d50984SMatthew G. Knepley }
192092d50984SMatthew G. Knepley }
19219566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->points, &parray));
19229566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->offs, &oarray));
19239566063dSJacob Faibussowitsch if (patch->viewSection) PetscCall(ObjectView((PetscObject)patch->patchSection, patch->viewerSection, patch->formatSection));
19249566063dSJacob Faibussowitsch PetscCall(DMPlexComputeResidual_Patch_Internal(dm, patch->patchSection, cellIS, 0.0, x, NULL, F, ctx));
19259566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm));
19263ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
192792d50984SMatthew G. Knepley }
192892d50984SMatthew G. Knepley
PCPatchComputeFunction_Internal(PC pc,Vec x,Vec F,PetscInt point)1929d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchComputeFunction_Internal(PC pc, Vec x, Vec F, PetscInt point)
1930d71ae5a4SJacob Faibussowitsch {
193192d50984SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
193292d50984SMatthew G. Knepley const PetscInt *dofsArray;
19330904074fSPatrick Farrell const PetscInt *dofsArrayWithAll;
193492d50984SMatthew G. Knepley const PetscInt *cellsArray;
193592d50984SMatthew G. Knepley PetscInt ncell, offset, pStart, pEnd;
193692d50984SMatthew G. Knepley
193792d50984SMatthew G. Knepley PetscFunctionBegin;
19389566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0));
1939ef1023bdSBarry Smith PetscCheck(patch->usercomputeop, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set callback");
19409566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->dofs, &dofsArray));
19419566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll));
19429566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->cells, &cellsArray));
19439566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd));
194492d50984SMatthew G. Knepley
194592d50984SMatthew G. Knepley point += pStart;
194663a3b9bcSJacob Faibussowitsch PetscCheck(point < pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %" PetscInt_FMT " not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, pStart, pEnd);
194792d50984SMatthew G. Knepley
19489566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->cellCounts, point, &ncell));
19499566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->cellCounts, point, &offset));
195092d50984SMatthew G. Knepley if (ncell <= 0) {
19519566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
19523ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
195392d50984SMatthew G. Knepley }
19549566063dSJacob Faibussowitsch PetscCall(VecSet(F, 0.0));
195592d50984SMatthew G. Knepley /* Cannot reuse the same IS because the geometry info is being cached in it */
19569566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS));
1957792fecdfSBarry 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));
19589566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->cellIS));
19599566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->dofs, &dofsArray));
19609566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll));
19619566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->cells, &cellsArray));
196292d50984SMatthew G. Knepley if (patch->viewMatrix) {
196392d50984SMatthew G. Knepley char name[PETSC_MAX_PATH_LEN];
196492d50984SMatthew G. Knepley
196563a3b9bcSJacob Faibussowitsch PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN - 1, "Patch vector for Point %" PetscInt_FMT, point));
19669566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)F, name));
19679566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject)F, patch->viewerMatrix, patch->formatMatrix));
196892d50984SMatthew G. Knepley }
19699566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
19703ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
197192d50984SMatthew G. Knepley }
197292d50984SMatthew G. Knepley
PCPatchComputeOperator_DMPlex_Private(PC pc,PetscInt patchNum,Vec x,Mat J,IS cellIS,PetscInt n,const PetscInt * l2p,const PetscInt * l2pWithAll,PetscCtx ctx)1973*2a8381b2SBarry Smith static PetscErrorCode PCPatchComputeOperator_DMPlex_Private(PC pc, PetscInt patchNum, Vec x, Mat J, IS cellIS, PetscInt n, const PetscInt *l2p, const PetscInt *l2pWithAll, PetscCtx ctx)
1974d71ae5a4SJacob Faibussowitsch {
19755f824522SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
1976b6bb21d1SLawrence Mitchell DM dm, plex;
19775f824522SMatthew G. Knepley PetscSection s;
19785f824522SMatthew G. Knepley const PetscInt *parray, *oarray;
19795f824522SMatthew G. Knepley PetscInt Nf = patch->nsubspaces, Np, poff, p, f;
19805f824522SMatthew G. Knepley
19815f824522SMatthew G. Knepley PetscFunctionBegin;
19829566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm));
19839566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex));
1984b6bb21d1SLawrence Mitchell dm = plex;
19859566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, &s));
19865f824522SMatthew G. Knepley /* Set offset into patch */
19879566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->pointCounts, patchNum, &Np));
19889566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->pointCounts, patchNum, &poff));
19899566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->points, &parray));
19909566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->offs, &oarray));
19915f824522SMatthew G. Knepley for (f = 0; f < Nf; ++f) {
19925f824522SMatthew G. Knepley for (p = 0; p < Np; ++p) {
19935f824522SMatthew G. Knepley const PetscInt point = parray[poff + p];
19945f824522SMatthew G. Knepley PetscInt dof;
19955f824522SMatthew G. Knepley
19969566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldDof(patch->patchSection, point, f, &dof));
19979566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff + p) * Nf + f]));
19989566063dSJacob Faibussowitsch if (patch->nsubspaces == 1) PetscCall(PetscSectionSetOffset(patch->patchSection, point, oarray[(poff + p) * Nf + f]));
19999566063dSJacob Faibussowitsch else PetscCall(PetscSectionSetOffset(patch->patchSection, point, -1));
20005f824522SMatthew G. Knepley }
20015f824522SMatthew G. Knepley }
20029566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->points, &parray));
20039566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->offs, &oarray));
20049566063dSJacob Faibussowitsch if (patch->viewSection) PetscCall(ObjectView((PetscObject)patch->patchSection, patch->viewerSection, patch->formatSection));
20055f824522SMatthew G. Knepley /* TODO Shut off MatViewFromOptions() in MatAssemblyEnd() here */
20069566063dSJacob Faibussowitsch PetscCall(DMPlexComputeJacobian_Patch_Internal(dm, patch->patchSection, patch->patchSection, cellIS, 0.0, 0.0, x, NULL, J, J, ctx));
20079566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm));
20083ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
20095f824522SMatthew G. Knepley }
20105f824522SMatthew G. Knepley
2011a685ae26SLawrence Mitchell /* This function zeros mat on entry */
PCPatchComputeOperator_Internal(PC pc,Vec x,Mat mat,PetscInt point,PetscBool withArtificial)2012d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchComputeOperator_Internal(PC pc, Vec x, Mat mat, PetscInt point, PetscBool withArtificial)
2013d71ae5a4SJacob Faibussowitsch {
20144bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
20154bbf5ea8SMatthew G. Knepley const PetscInt *dofsArray;
20160904074fSPatrick Farrell const PetscInt *dofsArrayWithAll = NULL;
20174bbf5ea8SMatthew G. Knepley const PetscInt *cellsArray;
2018eb62eeaaSLawrence Mitchell PetscInt ncell, offset, pStart, pEnd, numIntFacets, intFacetOffset;
20194d04e9f1SPatrick Farrell PetscBool isNonlinear;
20204bbf5ea8SMatthew G. Knepley
20214bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
20229566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0));
2023debbdec3SPatrick Farrell isNonlinear = patch->isNonlinear;
2024ef1023bdSBarry Smith PetscCheck(patch->usercomputeop, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set callback");
2025c2e6f3c0SFlorian Wechsung if (withArtificial) {
20269566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->dofsWithArtificial, &dofsArray));
2027c2e6f3c0SFlorian Wechsung } else {
20289566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->dofs, &dofsArray));
2029c2e6f3c0SFlorian Wechsung }
203048a46eb9SPierre Jolivet if (isNonlinear) PetscCall(ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll));
20319566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->cells, &cellsArray));
20329566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd));
20334bbf5ea8SMatthew G. Knepley
20344bbf5ea8SMatthew G. Knepley point += pStart;
203563a3b9bcSJacob Faibussowitsch PetscCheck(point < pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %" PetscInt_FMT " not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, pStart, pEnd);
20364bbf5ea8SMatthew G. Knepley
20379566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->cellCounts, point, &ncell));
20389566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->cellCounts, point, &offset));
20394bbf5ea8SMatthew G. Knepley if (ncell <= 0) {
20409566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
20413ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
20424bbf5ea8SMatthew G. Knepley }
20439566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(mat));
2044fa84ea4cSLawrence Mitchell if (patch->precomputeElementTensors) {
2045fa84ea4cSLawrence Mitchell PetscInt i;
2046fa84ea4cSLawrence Mitchell PetscInt ndof = patch->totalDofsPerCell;
2047fa84ea4cSLawrence Mitchell const PetscScalar *elementTensors;
2048fa84ea4cSLawrence Mitchell
20499566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(patch->cellMats, &elementTensors));
2050fa84ea4cSLawrence Mitchell for (i = 0; i < ncell; i++) {
2051fa84ea4cSLawrence Mitchell const PetscInt cell = cellsArray[i + offset];
2052fa84ea4cSLawrence Mitchell const PetscInt *idx = dofsArray + (offset + i) * ndof;
2053fe988be2SFlorian Wechsung const PetscScalar *v = elementTensors + patch->precomputedTensorLocations[cell] * ndof * ndof;
20549566063dSJacob Faibussowitsch PetscCall(MatSetValues(mat, ndof, idx, ndof, idx, v, ADD_VALUES));
2055fa84ea4cSLawrence Mitchell }
20569566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(patch->cellMats, &elementTensors));
20579566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
20589566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
2059fa84ea4cSLawrence Mitchell } else {
20602aa6f319SMatthew G. Knepley /* Cannot reuse the same IS because the geometry info is being cached in it */
20619566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS));
20629371c9d4SSatish Balay PetscCallBack("PCPatch callback",
2063de0a9f35SPierre Jolivet patch->usercomputeop(pc, point, x, mat, patch->cellIS, ncell * patch->totalDofsPerCell, dofsArray + offset * patch->totalDofsPerCell, PetscSafePointerPlusOffset(dofsArrayWithAll, offset * patch->totalDofsPerCell), patch->usercomputeopctx));
2064fa84ea4cSLawrence Mitchell }
206559109abcSLawrence Mitchell if (patch->usercomputeopintfacet) {
20669566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets));
20679566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset));
2068eb62eeaaSLawrence Mitchell if (numIntFacets > 0) {
2069eb62eeaaSLawrence Mitchell /* For each interior facet, grab the two cells (in local numbering, and concatenate dof numberings for those cells) */
2070eb62eeaaSLawrence Mitchell PetscInt *facetDofs = NULL, *facetDofsWithAll = NULL;
2071eb62eeaaSLawrence Mitchell const PetscInt *intFacetsArray = NULL;
2072eb62eeaaSLawrence Mitchell PetscInt idx = 0;
2073eb62eeaaSLawrence Mitchell PetscInt i, c, d;
2074de2d1767SPatrick Farrell PetscInt fStart;
2075b6bb21d1SLawrence Mitchell DM dm, plex;
2076eb62eeaaSLawrence Mitchell IS facetIS = NULL;
2077eb62eeaaSLawrence Mitchell const PetscInt *facetCells = NULL;
20787a50e09dSPatrick Farrell
20799566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells));
20809566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray));
20819566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm));
20829566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex));
2083b6bb21d1SLawrence Mitchell dm = plex;
20849566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, NULL));
2085eb62eeaaSLawrence Mitchell /* FIXME: Pull this malloc out. */
20869566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofs));
208748a46eb9SPierre Jolivet if (dofsArrayWithAll) PetscCall(PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofsWithAll));
2088f98464cbSLawrence Mitchell if (patch->precomputeElementTensors) {
2089f98464cbSLawrence Mitchell PetscInt nFacetDof = 2 * patch->totalDofsPerCell;
2090f98464cbSLawrence Mitchell const PetscScalar *elementTensors;
2091f98464cbSLawrence Mitchell
20929566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(patch->intFacetMats, &elementTensors));
2093f98464cbSLawrence Mitchell
2094f98464cbSLawrence Mitchell for (i = 0; i < numIntFacets; i++) {
2095f98464cbSLawrence Mitchell const PetscInt facet = intFacetsArray[i + intFacetOffset];
2096de2d1767SPatrick Farrell const PetscScalar *v = elementTensors + patch->precomputedIntFacetTensorLocations[facet - fStart] * nFacetDof * nFacetDof;
2097f98464cbSLawrence Mitchell idx = 0;
2098f98464cbSLawrence Mitchell /*
2099f1580f4eSBarry Smith 0--1
2100f1580f4eSBarry Smith |\-|
2101f1580f4eSBarry Smith |+\|
2102f1580f4eSBarry Smith 2--3
2103f1580f4eSBarry Smith [0, 2, 3, 0, 1, 3]
2104f98464cbSLawrence Mitchell */
2105f98464cbSLawrence Mitchell for (c = 0; c < 2; c++) {
2106f98464cbSLawrence Mitchell const PetscInt cell = facetCells[2 * (intFacetOffset + i) + c];
2107f98464cbSLawrence Mitchell for (d = 0; d < patch->totalDofsPerCell; d++) {
2108f98464cbSLawrence Mitchell facetDofs[idx] = dofsArray[(offset + cell) * patch->totalDofsPerCell + d];
2109f98464cbSLawrence Mitchell idx++;
2110f98464cbSLawrence Mitchell }
2111f98464cbSLawrence Mitchell }
21129566063dSJacob Faibussowitsch PetscCall(MatSetValues(mat, nFacetDof, facetDofs, nFacetDof, facetDofs, v, ADD_VALUES));
2113f98464cbSLawrence Mitchell }
21149566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(patch->intFacetMats, &elementTensors));
2115f98464cbSLawrence Mitchell } else {
2116eb62eeaaSLawrence Mitchell /*
2117f1580f4eSBarry Smith 0--1
2118f1580f4eSBarry Smith |\-|
2119f1580f4eSBarry Smith |+\|
2120f1580f4eSBarry Smith 2--3
2121f1580f4eSBarry Smith [0, 2, 3, 0, 1, 3]
2122eb62eeaaSLawrence Mitchell */
2123eb62eeaaSLawrence Mitchell for (i = 0; i < numIntFacets; i++) {
2124eb62eeaaSLawrence Mitchell for (c = 0; c < 2; c++) {
2125eb62eeaaSLawrence Mitchell const PetscInt cell = facetCells[2 * (intFacetOffset + i) + c];
2126eb62eeaaSLawrence Mitchell for (d = 0; d < patch->totalDofsPerCell; d++) {
2127eb62eeaaSLawrence Mitchell facetDofs[idx] = dofsArray[(offset + cell) * patch->totalDofsPerCell + d];
2128ad540459SPierre Jolivet if (dofsArrayWithAll) facetDofsWithAll[idx] = dofsArrayWithAll[(offset + cell) * patch->totalDofsPerCell + d];
2129eb62eeaaSLawrence Mitchell idx++;
2130eb62eeaaSLawrence Mitchell }
2131eb62eeaaSLawrence Mitchell }
2132eb62eeaaSLawrence Mitchell }
21339566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray + intFacetOffset, PETSC_USE_POINTER, &facetIS));
21349566063dSJacob Faibussowitsch PetscCall(patch->usercomputeopintfacet(pc, point, x, mat, facetIS, 2 * numIntFacets * patch->totalDofsPerCell, facetDofs, facetDofsWithAll, patch->usercomputeopintfacetctx));
21359566063dSJacob Faibussowitsch PetscCall(ISDestroy(&facetIS));
2136f98464cbSLawrence Mitchell }
21379566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->intFacetsToPatchCell, &facetCells));
21389566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->intFacets, &intFacetsArray));
21399566063dSJacob Faibussowitsch PetscCall(PetscFree(facetDofs));
21409566063dSJacob Faibussowitsch PetscCall(PetscFree(facetDofsWithAll));
21419566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm));
2142eb62eeaaSLawrence Mitchell }
214359109abcSLawrence Mitchell }
21446710cc29SPatrick Farrell
21459566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
21469566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
21476710cc29SPatrick Farrell
2148c73d2cf6SLawrence Mitchell if (!(withArtificial || isNonlinear) && patch->denseinverse) {
2149c73d2cf6SLawrence Mitchell MatFactorInfo info;
2150c73d2cf6SLawrence Mitchell PetscBool flg;
21519566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)mat, MATSEQDENSE, &flg));
215228b400f6SJacob Faibussowitsch PetscCheck(flg, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Invalid Mat type for dense inverse");
21539566063dSJacob Faibussowitsch PetscCall(MatFactorInfoInitialize(&info));
21549566063dSJacob Faibussowitsch PetscCall(MatLUFactor(mat, NULL, NULL, &info));
21559566063dSJacob Faibussowitsch PetscCall(MatSeqDenseInvertFactors_Private(mat));
2156c73d2cf6SLawrence Mitchell }
21579566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->cellIS));
21584d04e9f1SPatrick Farrell if (withArtificial) {
21599566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->dofsWithArtificial, &dofsArray));
2160c2e6f3c0SFlorian Wechsung } else {
21619566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->dofs, &dofsArray));
2162c2e6f3c0SFlorian Wechsung }
216348a46eb9SPierre Jolivet if (isNonlinear) PetscCall(ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll));
21649566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->cells, &cellsArray));
21652aa6f319SMatthew G. Knepley if (patch->viewMatrix) {
21662aa6f319SMatthew G. Knepley char name[PETSC_MAX_PATH_LEN];
21672aa6f319SMatthew G. Knepley
216863a3b9bcSJacob Faibussowitsch PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN - 1, "Patch matrix for Point %" PetscInt_FMT, point));
21699566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)mat, name));
21709566063dSJacob Faibussowitsch PetscCall(ObjectView((PetscObject)mat, patch->viewerMatrix, patch->formatMatrix));
21712aa6f319SMatthew G. Knepley }
21729566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
21733ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
21744bbf5ea8SMatthew G. Knepley }
21754bbf5ea8SMatthew G. Knepley
MatSetValues_PCPatch_Private(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],const PetscScalar * v,InsertMode addv)2176d71ae5a4SJacob Faibussowitsch static PetscErrorCode MatSetValues_PCPatch_Private(Mat mat, PetscInt m, const PetscInt idxm[], PetscInt n, const PetscInt idxn[], const PetscScalar *v, InsertMode addv)
2177d71ae5a4SJacob Faibussowitsch {
2178fa84ea4cSLawrence Mitchell Vec data;
2179fa84ea4cSLawrence Mitchell PetscScalar *array;
2180fe988be2SFlorian Wechsung PetscInt bs, nz, i, j, cell;
2181fa84ea4cSLawrence Mitchell
21829566063dSJacob Faibussowitsch PetscCall(MatShellGetContext(mat, &data));
21839566063dSJacob Faibussowitsch PetscCall(VecGetBlockSize(data, &bs));
21849566063dSJacob Faibussowitsch PetscCall(VecGetSize(data, &nz));
21859566063dSJacob Faibussowitsch PetscCall(VecGetArray(data, &array));
218608401ef6SPierre Jolivet PetscCheck(m == n, PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Only for square insertion");
2187835f2295SStefano Zampini cell = idxm[0] / bs; /* use the fact that this is called once per cell */
2188fa84ea4cSLawrence Mitchell for (i = 0; i < m; i++) {
218908401ef6SPierre Jolivet PetscCheck(idxm[i] == idxn[i], PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Row and column indices must match!");
2190fa84ea4cSLawrence Mitchell for (j = 0; j < n; j++) {
2191fa84ea4cSLawrence Mitchell const PetscScalar v_ = v[i * bs + j];
2192fa84ea4cSLawrence Mitchell /* Indexing is special to the data structure we have! */
2193fa84ea4cSLawrence Mitchell if (addv == INSERT_VALUES) {
2194fe988be2SFlorian Wechsung array[cell * bs * bs + i * bs + j] = v_;
2195fa84ea4cSLawrence Mitchell } else {
2196fe988be2SFlorian Wechsung array[cell * bs * bs + i * bs + j] += v_;
2197fa84ea4cSLawrence Mitchell }
2198fa84ea4cSLawrence Mitchell }
2199fa84ea4cSLawrence Mitchell }
22009566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(data, &array));
22013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
2202fa84ea4cSLawrence Mitchell }
2203fa84ea4cSLawrence Mitchell
PCPatchPrecomputePatchTensors_Private(PC pc)2204d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchPrecomputePatchTensors_Private(PC pc)
2205d71ae5a4SJacob Faibussowitsch {
2206fa84ea4cSLawrence Mitchell PC_PATCH *patch = (PC_PATCH *)pc->data;
2207fa84ea4cSLawrence Mitchell const PetscInt *cellsArray;
2208fa84ea4cSLawrence Mitchell PetscInt ncell, offset;
2209fa84ea4cSLawrence Mitchell const PetscInt *dofMapArray;
2210fa84ea4cSLawrence Mitchell PetscInt i, j;
2211fa84ea4cSLawrence Mitchell IS dofMap;
2212fa84ea4cSLawrence Mitchell IS cellIS;
2213fa84ea4cSLawrence Mitchell const PetscInt ndof = patch->totalDofsPerCell;
2214fa84ea4cSLawrence Mitchell Mat vecMat;
2215fe988be2SFlorian Wechsung PetscInt cStart, cEnd;
2216fe988be2SFlorian Wechsung DM dm, plex;
2217fe988be2SFlorian Wechsung
22189566063dSJacob Faibussowitsch PetscCall(ISGetSize(patch->cells, &ncell));
2219e9c2c94bSFlorian Wechsung if (!ncell) { /* No cells to assemble over -> skip */
22203ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
2221e9c2c94bSFlorian Wechsung }
2222e9c2c94bSFlorian Wechsung
22239566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0));
2224fa84ea4cSLawrence Mitchell
22259566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm));
22269566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex));
2227b6bb21d1SLawrence Mitchell dm = plex;
2228fa84ea4cSLawrence Mitchell if (!patch->allCells) {
2229fa84ea4cSLawrence Mitchell PetscHSetI cells;
2230fa84ea4cSLawrence Mitchell PetscHashIter hi;
2231fa84ea4cSLawrence Mitchell PetscInt pStart, pEnd;
2232fa84ea4cSLawrence Mitchell PetscInt *allCells = NULL;
22339566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&cells));
22349566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->cells, &cellsArray));
22359566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd));
2236fa84ea4cSLawrence Mitchell for (i = pStart; i < pEnd; i++) {
22379566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->cellCounts, i, &ncell));
22389566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->cellCounts, i, &offset));
2239fa84ea4cSLawrence Mitchell if (ncell <= 0) continue;
224048a46eb9SPierre Jolivet for (j = 0; j < ncell; j++) PetscCall(PetscHSetIAdd(cells, cellsArray[offset + j]));
2241fa84ea4cSLawrence Mitchell }
22429566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->cells, &cellsArray));
22439566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(cells, &ncell));
22449566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(ncell, &allCells));
22459566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
22469566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(cEnd - cStart, &patch->precomputedTensorLocations));
2247fa84ea4cSLawrence Mitchell i = 0;
2248fa84ea4cSLawrence Mitchell PetscHashIterBegin(cells, hi);
2249fa84ea4cSLawrence Mitchell while (!PetscHashIterAtEnd(cells, hi)) {
2250fe988be2SFlorian Wechsung PetscHashIterGetKey(cells, hi, allCells[i]);
2251fe988be2SFlorian Wechsung patch->precomputedTensorLocations[allCells[i]] = i;
2252fa84ea4cSLawrence Mitchell PetscHashIterNext(cells, hi);
2253fe988be2SFlorian Wechsung i++;
2254fa84ea4cSLawrence Mitchell }
22559566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&cells));
22569566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, allCells, PETSC_OWN_POINTER, &patch->allCells));
2257fa84ea4cSLawrence Mitchell }
22589566063dSJacob Faibussowitsch PetscCall(ISGetSize(patch->allCells, &ncell));
2259fa84ea4cSLawrence Mitchell if (!patch->cellMats) {
22609566063dSJacob Faibussowitsch PetscCall(VecCreateSeq(PETSC_COMM_SELF, ncell * ndof * ndof, &patch->cellMats));
22619566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(patch->cellMats, ndof));
2262fa84ea4cSLawrence Mitchell }
22639566063dSJacob Faibussowitsch PetscCall(VecSet(patch->cellMats, 0));
2264fa84ea4cSLawrence Mitchell
2265d0609cedSBarry Smith PetscCall(MatCreateShell(PETSC_COMM_SELF, ncell * ndof, ncell * ndof, ncell * ndof, ncell * ndof, (void *)patch->cellMats, &vecMat));
226657d50842SBarry Smith PetscCall(MatShellSetOperation(vecMat, MATOP_SET_VALUES, (PetscErrorCodeFn *)MatSetValues_PCPatch_Private));
22679566063dSJacob Faibussowitsch PetscCall(ISGetSize(patch->allCells, &ncell));
22689566063dSJacob Faibussowitsch PetscCall(ISCreateStride(PETSC_COMM_SELF, ndof * ncell, 0, 1, &dofMap));
22699566063dSJacob Faibussowitsch PetscCall(ISGetIndices(dofMap, &dofMapArray));
22709566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->allCells, &cellsArray));
22719566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray, PETSC_USE_POINTER, &cellIS));
2272fa84ea4cSLawrence Mitchell /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */
2273792fecdfSBarry Smith PetscCallBack("PCPatch callback", patch->usercomputeop(pc, -1, NULL, vecMat, cellIS, ndof * ncell, dofMapArray, NULL, patch->usercomputeopctx));
22749566063dSJacob Faibussowitsch PetscCall(ISDestroy(&cellIS));
22759566063dSJacob Faibussowitsch PetscCall(MatDestroy(&vecMat));
22769566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->allCells, &cellsArray));
22779566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(dofMap, &dofMapArray));
22789566063dSJacob Faibussowitsch PetscCall(ISDestroy(&dofMap));
2279f98464cbSLawrence Mitchell
2280f98464cbSLawrence Mitchell if (patch->usercomputeopintfacet) {
2281f98464cbSLawrence Mitchell PetscInt nIntFacets;
2282f98464cbSLawrence Mitchell IS intFacetsIS;
2283f98464cbSLawrence Mitchell const PetscInt *intFacetsArray = NULL;
2284f98464cbSLawrence Mitchell if (!patch->allIntFacets) {
2285f98464cbSLawrence Mitchell PetscHSetI facets;
2286f98464cbSLawrence Mitchell PetscHashIter hi;
2287f98464cbSLawrence Mitchell PetscInt pStart, pEnd, fStart, fEnd;
2288f98464cbSLawrence Mitchell PetscInt *allIntFacets = NULL;
22899566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&facets));
22909566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray));
22919566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->intFacetCounts, &pStart, &pEnd));
22929566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
2293f98464cbSLawrence Mitchell for (i = pStart; i < pEnd; i++) {
22949566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->intFacetCounts, i, &nIntFacets));
22959566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->intFacetCounts, i, &offset));
2296f98464cbSLawrence Mitchell if (nIntFacets <= 0) continue;
229748a46eb9SPierre Jolivet for (j = 0; j < nIntFacets; j++) PetscCall(PetscHSetIAdd(facets, intFacetsArray[offset + j]));
2298f98464cbSLawrence Mitchell }
22999566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->intFacets, &intFacetsArray));
23009566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(facets, &nIntFacets));
23019566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nIntFacets, &allIntFacets));
23029566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(fEnd - fStart, &patch->precomputedIntFacetTensorLocations));
2303f98464cbSLawrence Mitchell i = 0;
2304f98464cbSLawrence Mitchell PetscHashIterBegin(facets, hi);
2305f98464cbSLawrence Mitchell while (!PetscHashIterAtEnd(facets, hi)) {
2306f98464cbSLawrence Mitchell PetscHashIterGetKey(facets, hi, allIntFacets[i]);
2307de2d1767SPatrick Farrell patch->precomputedIntFacetTensorLocations[allIntFacets[i] - fStart] = i;
2308f98464cbSLawrence Mitchell PetscHashIterNext(facets, hi);
2309f98464cbSLawrence Mitchell i++;
2310f98464cbSLawrence Mitchell }
23119566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&facets));
23129566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, allIntFacets, PETSC_OWN_POINTER, &patch->allIntFacets));
2313f98464cbSLawrence Mitchell }
23149566063dSJacob Faibussowitsch PetscCall(ISGetSize(patch->allIntFacets, &nIntFacets));
2315f98464cbSLawrence Mitchell if (!patch->intFacetMats) {
23169566063dSJacob Faibussowitsch PetscCall(VecCreateSeq(PETSC_COMM_SELF, nIntFacets * ndof * ndof * 4, &patch->intFacetMats));
23179566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(patch->intFacetMats, ndof * 2));
2318f98464cbSLawrence Mitchell }
23199566063dSJacob Faibussowitsch PetscCall(VecSet(patch->intFacetMats, 0));
2320f98464cbSLawrence Mitchell
2321d0609cedSBarry Smith PetscCall(MatCreateShell(PETSC_COMM_SELF, nIntFacets * ndof * 2, nIntFacets * ndof * 2, nIntFacets * ndof * 2, nIntFacets * ndof * 2, (void *)patch->intFacetMats, &vecMat));
232257d50842SBarry Smith PetscCall(MatShellSetOperation(vecMat, MATOP_SET_VALUES, (PetscErrorCodeFn *)MatSetValues_PCPatch_Private));
23239566063dSJacob Faibussowitsch PetscCall(ISCreateStride(PETSC_COMM_SELF, 2 * ndof * nIntFacets, 0, 1, &dofMap));
23249566063dSJacob Faibussowitsch PetscCall(ISGetIndices(dofMap, &dofMapArray));
23259566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->allIntFacets, &intFacetsArray));
23269566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, intFacetsArray, PETSC_USE_POINTER, &intFacetsIS));
2327f98464cbSLawrence Mitchell /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */
2328792fecdfSBarry Smith PetscCallBack("PCPatch callback (interior facets)", patch->usercomputeopintfacet(pc, -1, NULL, vecMat, intFacetsIS, 2 * ndof * nIntFacets, dofMapArray, NULL, patch->usercomputeopintfacetctx));
23299566063dSJacob Faibussowitsch PetscCall(ISDestroy(&intFacetsIS));
23309566063dSJacob Faibussowitsch PetscCall(MatDestroy(&vecMat));
23319566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->allIntFacets, &intFacetsArray));
23329566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(dofMap, &dofMapArray));
23339566063dSJacob Faibussowitsch PetscCall(ISDestroy(&dofMap));
2334f98464cbSLawrence Mitchell }
23359566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm));
23369566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
23373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
2338fa84ea4cSLawrence Mitchell }
2339fa84ea4cSLawrence Mitchell
PCPatch_ScatterLocal_Private(PC pc,PetscInt p,Vec x,Vec y,InsertMode mode,ScatterMode scat,PatchScatterType scattertype)2340d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatch_ScatterLocal_Private(PC pc, PetscInt p, Vec x, Vec y, InsertMode mode, ScatterMode scat, PatchScatterType scattertype)
2341d71ae5a4SJacob Faibussowitsch {
23424bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
23434bbf5ea8SMatthew G. Knepley const PetscScalar *xArray = NULL;
23444bbf5ea8SMatthew G. Knepley PetscScalar *yArray = NULL;
23454bbf5ea8SMatthew G. Knepley const PetscInt *gtolArray = NULL;
23464bbf5ea8SMatthew G. Knepley PetscInt dof, offset, lidx;
23474bbf5ea8SMatthew G. Knepley
23484bbf5ea8SMatthew G. Knepley PetscFunctionBeginHot;
23499566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(x, &xArray));
23509566063dSJacob Faibussowitsch PetscCall(VecGetArray(y, &yArray));
23510904074fSPatrick Farrell if (scattertype == SCATTER_WITHARTIFICIAL) {
23529566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof));
23539566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offset));
23549566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->gtolWithArtificial, >olArray));
23550904074fSPatrick Farrell } else if (scattertype == SCATTER_WITHALL) {
23569566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCountsWithAll, p, &dof));
23579566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offset));
23589566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->gtolWithAll, >olArray));
2359c2e6f3c0SFlorian Wechsung } else {
23609566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &dof));
23619566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset));
23629566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->gtol, >olArray));
2363c2e6f3c0SFlorian Wechsung }
23642472a847SBarry Smith PetscCheck(mode != INSERT_VALUES || scat == SCATTER_FORWARD, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't insert if not scattering forward");
23652472a847SBarry Smith PetscCheck(mode != ADD_VALUES || scat == SCATTER_REVERSE, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't add if not scattering reverse");
23664bbf5ea8SMatthew G. Knepley for (lidx = 0; lidx < dof; ++lidx) {
23674bbf5ea8SMatthew G. Knepley const PetscInt gidx = gtolArray[offset + lidx];
23684bbf5ea8SMatthew G. Knepley
23694bbf5ea8SMatthew G. Knepley if (mode == INSERT_VALUES) yArray[lidx] = xArray[gidx]; /* Forward */
23704bbf5ea8SMatthew G. Knepley else yArray[gidx] += xArray[lidx]; /* Reverse */
23714bbf5ea8SMatthew G. Knepley }
23720904074fSPatrick Farrell if (scattertype == SCATTER_WITHARTIFICIAL) {
23739566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->gtolWithArtificial, >olArray));
23740904074fSPatrick Farrell } else if (scattertype == SCATTER_WITHALL) {
23759566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->gtolWithAll, >olArray));
2376c2e6f3c0SFlorian Wechsung } else {
23779566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->gtol, >olArray));
2378c2e6f3c0SFlorian Wechsung }
23799566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(x, &xArray));
23809566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(y, &yArray));
23813ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
23824bbf5ea8SMatthew G. Knepley }
23834bbf5ea8SMatthew G. Knepley
PCSetUp_PATCH_Linear(PC pc)2384d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_PATCH_Linear(PC pc)
2385d71ae5a4SJacob Faibussowitsch {
2386dadc69c5SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
2387dadc69c5SMatthew G. Knepley const char *prefix;
2388dadc69c5SMatthew G. Knepley PetscInt i;
2389dadc69c5SMatthew G. Knepley
2390dadc69c5SMatthew G. Knepley PetscFunctionBegin;
2391dadc69c5SMatthew G. Knepley if (!pc->setupcalled) {
23927827d75bSBarry Smith PetscCheck(patch->save_operators || !patch->denseinverse, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Can't have dense inverse without save operators");
2393c73d2cf6SLawrence Mitchell if (!patch->denseinverse) {
23949566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(patch->npatch, &patch->solver));
23959566063dSJacob Faibussowitsch PetscCall(PCGetOptionsPrefix(pc, &prefix));
2396dadc69c5SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) {
2397dadc69c5SMatthew G. Knepley KSP ksp;
2398dadc69c5SMatthew G. Knepley PC subpc;
2399dadc69c5SMatthew G. Knepley
24009566063dSJacob Faibussowitsch PetscCall(KSPCreate(PETSC_COMM_SELF, &ksp));
24013821be0aSBarry Smith PetscCall(KSPSetNestLevel(ksp, pc->kspnestlevel));
24029566063dSJacob Faibussowitsch PetscCall(KSPSetErrorIfNotConverged(ksp, pc->erroriffailure));
24039566063dSJacob Faibussowitsch PetscCall(KSPSetOptionsPrefix(ksp, prefix));
24049566063dSJacob Faibussowitsch PetscCall(KSPAppendOptionsPrefix(ksp, "sub_"));
24059566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)ksp, (PetscObject)pc, 1));
24069566063dSJacob Faibussowitsch PetscCall(KSPGetPC(ksp, &subpc));
24079566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)subpc, (PetscObject)pc, 1));
2408dadc69c5SMatthew G. Knepley patch->solver[i] = (PetscObject)ksp;
2409dadc69c5SMatthew G. Knepley }
2410dadc69c5SMatthew G. Knepley }
2411c73d2cf6SLawrence Mitchell }
2412dadc69c5SMatthew G. Knepley if (patch->save_operators) {
24131baa6e33SBarry Smith if (patch->precomputeElementTensors) PetscCall(PCPatchPrecomputePatchTensors_Private(pc));
2414dadc69c5SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) {
24159566063dSJacob Faibussowitsch PetscCall(PCPatchComputeOperator_Internal(pc, NULL, patch->mat[i], i, PETSC_FALSE));
2416c73d2cf6SLawrence Mitchell if (!patch->denseinverse) {
24179566063dSJacob Faibussowitsch PetscCall(KSPSetOperators((KSP)patch->solver[i], patch->mat[i], patch->mat[i]));
24189d4fc724SLawrence Mitchell } else if (patch->mat[i] && !patch->densesolve) {
24199d4fc724SLawrence Mitchell /* Setup matmult callback */
242057d50842SBarry Smith PetscCall(MatGetOperation(patch->mat[i], MATOP_MULT, (PetscErrorCodeFn **)&patch->densesolve));
2421dadc69c5SMatthew G. Knepley }
2422dadc69c5SMatthew G. Knepley }
2423c73d2cf6SLawrence Mitchell }
242434d8b122SPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
242534d8b122SPatrick Farrell for (i = 0; i < patch->npatch; ++i) {
24261202d238SPatrick Farrell /* Instead of padding patch->patchUpdate with zeros to get */
24271202d238SPatrick Farrell /* patch->patchUpdateWithArtificial and then multiplying with the matrix, */
242834d8b122SPatrick Farrell /* just get rid of the columns that correspond to the dofs with */
242934d8b122SPatrick Farrell /* artificial bcs. That's of course fairly inefficient, hopefully we */
243034d8b122SPatrick Farrell /* can just assemble the rectangular matrix in the first place. */
243134d8b122SPatrick Farrell Mat matSquare;
243234d8b122SPatrick Farrell IS rowis;
243334d8b122SPatrick Farrell PetscInt dof;
243434d8b122SPatrick Farrell
24359566063dSJacob Faibussowitsch PetscCall(MatGetSize(patch->mat[i], &dof, NULL));
243634d8b122SPatrick Farrell if (dof == 0) {
243734d8b122SPatrick Farrell patch->matWithArtificial[i] = NULL;
243834d8b122SPatrick Farrell continue;
243934d8b122SPatrick Farrell }
244034d8b122SPatrick Farrell
24419566063dSJacob Faibussowitsch PetscCall(PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE));
24429566063dSJacob Faibussowitsch PetscCall(PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE));
244334d8b122SPatrick Farrell
24449566063dSJacob Faibussowitsch PetscCall(MatGetSize(matSquare, &dof, NULL));
24459566063dSJacob Faibussowitsch PetscCall(ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis));
244634d8b122SPatrick Farrell if (pc->setupcalled) {
24479566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_REUSE_MATRIX, &patch->matWithArtificial[i]));
244834d8b122SPatrick Farrell } else {
24499566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &patch->matWithArtificial[i]));
245034d8b122SPatrick Farrell }
24519566063dSJacob Faibussowitsch PetscCall(ISDestroy(&rowis));
24529566063dSJacob Faibussowitsch PetscCall(MatDestroy(&matSquare));
245334d8b122SPatrick Farrell }
245434d8b122SPatrick Farrell }
24553ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
2456dadc69c5SMatthew G. Knepley }
2457dadc69c5SMatthew G. Knepley
PCSetUp_PATCH(PC pc)2458d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_PATCH(PC pc)
2459d71ae5a4SJacob Faibussowitsch {
24604bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
2461557beb66SLawrence Mitchell PetscInt i;
246239fd2e8aSPatrick Farrell PetscBool isNonlinear;
24639d4fc724SLawrence Mitchell PetscInt maxDof = -1, maxDofWithArtificial = -1;
24644bbf5ea8SMatthew G. Knepley
24654bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
24664bbf5ea8SMatthew G. Knepley if (!pc->setupcalled) {
24674bbf5ea8SMatthew G. Knepley PetscInt pStart, pEnd, p;
24684bbf5ea8SMatthew G. Knepley PetscInt localSize;
24694bbf5ea8SMatthew G. Knepley
24709566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_CreatePatches, pc, 0, 0, 0));
24714bbf5ea8SMatthew G. Knepley
2472debbdec3SPatrick Farrell isNonlinear = patch->isNonlinear;
24735f824522SMatthew G. Knepley if (!patch->nsubspaces) {
2474b6bb21d1SLawrence Mitchell DM dm, plex;
24755f824522SMatthew G. Knepley PetscSection s;
2476bd026e97SJed Brown PetscInt cStart, cEnd, c, Nf, f, numGlobalBcs = 0, *globalBcs, *Nb, **cellDofs;
24775f824522SMatthew G. Knepley
24789566063dSJacob Faibussowitsch PetscCall(PCGetDM(pc, &dm));
247928b400f6SJacob Faibussowitsch PetscCheck(dm, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Must set DM for PCPATCH or call PCPatchSetDiscretisationInfo()");
24809566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex));
2481b6bb21d1SLawrence Mitchell dm = plex;
24829566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, &s));
24839566063dSJacob Faibussowitsch PetscCall(PetscSectionGetNumFields(s, &Nf));
24849566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
24855f824522SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) {
24865f824522SMatthew G. Knepley PetscInt cdof;
24879566063dSJacob Faibussowitsch PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
24885f824522SMatthew G. Knepley numGlobalBcs += cdof;
24895f824522SMatthew G. Knepley }
24909566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
24919566063dSJacob Faibussowitsch PetscCall(PetscMalloc3(Nf, &Nb, Nf, &cellDofs, numGlobalBcs, &globalBcs));
24925f824522SMatthew G. Knepley for (f = 0; f < Nf; ++f) {
24935f824522SMatthew G. Knepley PetscFE fe;
24945f824522SMatthew G. Knepley PetscDualSpace sp;
24955f824522SMatthew G. Knepley PetscInt cdoff = 0;
24965f824522SMatthew G. Knepley
24979566063dSJacob Faibussowitsch PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
24989566063dSJacob Faibussowitsch /* PetscCall(PetscFEGetNumComponents(fe, &Nc[f])); */
24999566063dSJacob Faibussowitsch PetscCall(PetscFEGetDualSpace(fe, &sp));
25009566063dSJacob Faibussowitsch PetscCall(PetscDualSpaceGetDimension(sp, &Nb[f]));
25015f824522SMatthew G. Knepley
25029566063dSJacob Faibussowitsch PetscCall(PetscMalloc1((cEnd - cStart) * Nb[f], &cellDofs[f]));
25035f824522SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) {
25045f824522SMatthew G. Knepley PetscInt *closure = NULL;
25055f824522SMatthew G. Knepley PetscInt clSize = 0, cl;
25065f824522SMatthew G. Knepley
25079566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure));
25085f824522SMatthew G. Knepley for (cl = 0; cl < clSize * 2; cl += 2) {
25095f824522SMatthew G. Knepley const PetscInt p = closure[cl];
25105f824522SMatthew G. Knepley PetscInt fdof, d, foff;
25115f824522SMatthew G. Knepley
25129566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldDof(s, p, f, &fdof));
25139566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldOffset(s, p, f, &foff));
25145f824522SMatthew G. Knepley for (d = 0; d < fdof; ++d, ++cdoff) cellDofs[f][cdoff] = foff + d;
25155f824522SMatthew G. Knepley }
25169566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure));
25175f824522SMatthew G. Knepley }
251863a3b9bcSJacob 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]);
25195f824522SMatthew G. Knepley }
25205f824522SMatthew G. Knepley numGlobalBcs = 0;
25215f824522SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) {
25225f824522SMatthew G. Knepley const PetscInt *ind;
25235f824522SMatthew G. Knepley PetscInt off, cdof, d;
25245f824522SMatthew G. Knepley
25259566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(s, p, &off));
25269566063dSJacob Faibussowitsch PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
25279566063dSJacob Faibussowitsch PetscCall(PetscSectionGetConstraintIndices(s, p, &ind));
25285f824522SMatthew G. Knepley for (d = 0; d < cdof; ++d) globalBcs[numGlobalBcs++] = off + ind[d];
25295f824522SMatthew G. Knepley }
25305f824522SMatthew G. Knepley
25319566063dSJacob Faibussowitsch PetscCall(PCPatchSetDiscretisationInfoCombined(pc, dm, Nb, (const PetscInt **)cellDofs, numGlobalBcs, globalBcs, numGlobalBcs, globalBcs));
253248a46eb9SPierre Jolivet for (f = 0; f < Nf; ++f) PetscCall(PetscFree(cellDofs[f]));
25339566063dSJacob Faibussowitsch PetscCall(PetscFree3(Nb, cellDofs, globalBcs));
25349566063dSJacob Faibussowitsch PetscCall(PCPatchSetComputeFunction(pc, PCPatchComputeFunction_DMPlex_Private, NULL));
25359566063dSJacob Faibussowitsch PetscCall(PCPatchSetComputeOperator(pc, PCPatchComputeOperator_DMPlex_Private, NULL));
25369566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm));
25375f824522SMatthew G. Knepley }
25385f824522SMatthew G. Knepley
25394bbf5ea8SMatthew G. Knepley localSize = patch->subspaceOffsets[patch->nsubspaces];
25409566063dSJacob Faibussowitsch PetscCall(VecCreateSeq(PETSC_COMM_SELF, localSize, &patch->localRHS));
25419566063dSJacob Faibussowitsch PetscCall(VecSetUp(patch->localRHS));
25429566063dSJacob Faibussowitsch PetscCall(VecDuplicate(patch->localRHS, &patch->localUpdate));
25439566063dSJacob Faibussowitsch PetscCall(PCPatchCreateCellPatches(pc));
25449566063dSJacob Faibussowitsch PetscCall(PCPatchCreateCellPatchDiscretisationInfo(pc));
25454bbf5ea8SMatthew G. Knepley
25464bbf5ea8SMatthew G. Knepley /* OK, now build the work vectors */
25479566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, &pEnd));
2548c2e6f3c0SFlorian Wechsung
254948a46eb9SPierre Jolivet if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) PetscCall(PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithArtificial));
255048a46eb9SPierre Jolivet if (isNonlinear) PetscCall(PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithAll));
25514bbf5ea8SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) {
25524bbf5ea8SMatthew G. Knepley PetscInt dof;
25534bbf5ea8SMatthew G. Knepley
25549566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &dof));
25552f613bf5SBarry Smith maxDof = PetscMax(maxDof, dof);
25560904074fSPatrick Farrell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
25573bb0e8f7SKarl Rupp const PetscInt *gtolArray, *gtolArrayWithArtificial = NULL;
25583bb0e8f7SKarl Rupp PetscInt numPatchDofs, offset;
25593bb0e8f7SKarl Rupp PetscInt numPatchDofsWithArtificial, offsetWithArtificial;
25603bb0e8f7SKarl Rupp PetscInt dofWithoutArtificialCounter = 0;
25613bb0e8f7SKarl Rupp PetscInt *patchWithoutArtificialToWithArtificialArray;
25623bb0e8f7SKarl Rupp
25639566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof));
25649d4fc724SLawrence Mitchell maxDofWithArtificial = PetscMax(maxDofWithArtificial, dof);
2565c2e6f3c0SFlorian Wechsung
2566e047a90bSFlorian Wechsung /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
2567e047a90bSFlorian Wechsung /* the index in the patch with all dofs */
25689566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->gtol, >olArray));
256963deea8eSPatrick Farrell
25709566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs));
257147aca4a6SPatrick Farrell if (numPatchDofs == 0) {
257247aca4a6SPatrick Farrell patch->dofMappingWithoutToWithArtificial[p - pStart] = NULL;
257347aca4a6SPatrick Farrell continue;
257447aca4a6SPatrick Farrell }
257563deea8eSPatrick Farrell
25769566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset));
25779566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->gtolWithArtificial, >olArrayWithArtificial));
25789566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &numPatchDofsWithArtificial));
25799566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offsetWithArtificial));
2580c2e6f3c0SFlorian Wechsung
25819566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPatchDofs, &patchWithoutArtificialToWithArtificialArray));
2582b0c21b6aSKarl Rupp for (i = 0; i < numPatchDofsWithArtificial; i++) {
2583e047a90bSFlorian Wechsung if (gtolArrayWithArtificial[i + offsetWithArtificial] == gtolArray[offset + dofWithoutArtificialCounter]) {
2584c2e6f3c0SFlorian Wechsung patchWithoutArtificialToWithArtificialArray[dofWithoutArtificialCounter] = i;
2585c2e6f3c0SFlorian Wechsung dofWithoutArtificialCounter++;
25869371c9d4SSatish Balay if (dofWithoutArtificialCounter == numPatchDofs) break;
2587c2e6f3c0SFlorian Wechsung }
2588c2e6f3c0SFlorian Wechsung }
25899566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutArtificialToWithArtificialArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithArtificial[p - pStart]));
25909566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->gtol, >olArray));
25919566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->gtolWithArtificial, >olArrayWithArtificial));
2592c2e6f3c0SFlorian Wechsung }
25930997d788SPatrick Farrell }
25940997d788SPatrick Farrell for (p = pStart; p < pEnd; ++p) {
25950904074fSPatrick Farrell if (isNonlinear) {
25960904074fSPatrick Farrell const PetscInt *gtolArray, *gtolArrayWithAll = NULL;
25970904074fSPatrick Farrell PetscInt numPatchDofs, offset;
25980904074fSPatrick Farrell PetscInt numPatchDofsWithAll, offsetWithAll;
25990904074fSPatrick Farrell PetscInt dofWithoutAllCounter = 0;
26000904074fSPatrick Farrell PetscInt *patchWithoutAllToWithAllArray;
26010904074fSPatrick Farrell
26020904074fSPatrick Farrell /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
26030904074fSPatrick Farrell /* the index in the patch with all dofs */
26049566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->gtol, >olArray));
26050904074fSPatrick Farrell
26069566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs));
260747aca4a6SPatrick Farrell if (numPatchDofs == 0) {
2608b88cb22dSPatrick Farrell patch->dofMappingWithoutToWithAll[p - pStart] = NULL;
260947aca4a6SPatrick Farrell continue;
261047aca4a6SPatrick Farrell }
26110904074fSPatrick Farrell
26129566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset));
26139566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->gtolWithAll, >olArrayWithAll));
26149566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCountsWithAll, p, &numPatchDofsWithAll));
26159566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offsetWithAll));
26160904074fSPatrick Farrell
26179566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numPatchDofs, &patchWithoutAllToWithAllArray));
26180904074fSPatrick Farrell
26190904074fSPatrick Farrell for (i = 0; i < numPatchDofsWithAll; i++) {
26200904074fSPatrick Farrell if (gtolArrayWithAll[i + offsetWithAll] == gtolArray[offset + dofWithoutAllCounter]) {
26210904074fSPatrick Farrell patchWithoutAllToWithAllArray[dofWithoutAllCounter] = i;
26220904074fSPatrick Farrell dofWithoutAllCounter++;
26239371c9d4SSatish Balay if (dofWithoutAllCounter == numPatchDofs) break;
26240904074fSPatrick Farrell }
26250904074fSPatrick Farrell }
26269566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutAllToWithAllArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithAll[p - pStart]));
26279566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->gtol, >olArray));
26289566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->gtolWithAll, >olArrayWithAll));
26290904074fSPatrick Farrell }
26304bbf5ea8SMatthew G. Knepley }
263160dd46caSLawrence Mitchell if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
26329566063dSJacob Faibussowitsch PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDofWithArtificial, &patch->patchRHSWithArtificial));
26339566063dSJacob Faibussowitsch PetscCall(VecSetUp(patch->patchRHSWithArtificial));
263460dd46caSLawrence Mitchell }
26359566063dSJacob Faibussowitsch PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDof, &patch->patchRHS));
26369566063dSJacob Faibussowitsch PetscCall(VecSetUp(patch->patchRHS));
26379566063dSJacob Faibussowitsch PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDof, &patch->patchUpdate));
26389566063dSJacob Faibussowitsch PetscCall(VecSetUp(patch->patchUpdate));
26394bbf5ea8SMatthew G. Knepley if (patch->save_operators) {
26409566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(patch->npatch, &patch->mat));
264148a46eb9SPierre Jolivet for (i = 0; i < patch->npatch; ++i) PetscCall(PCPatchCreateMatrix_Private(pc, i, &patch->mat[i], PETSC_FALSE));
26424bbf5ea8SMatthew G. Knepley }
26439566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_CreatePatches, pc, 0, 0, 0));
26444bbf5ea8SMatthew G. Knepley
26454bbf5ea8SMatthew G. Knepley /* If desired, calculate weights for dof multiplicity */
26464bbf5ea8SMatthew G. Knepley if (patch->partition_of_unity) {
26473bb0e8f7SKarl Rupp PetscScalar *input = NULL;
26483bb0e8f7SKarl Rupp PetscScalar *output = NULL;
26493bb0e8f7SKarl Rupp Vec global;
26503bb0e8f7SKarl Rupp
26519566063dSJacob Faibussowitsch PetscCall(VecDuplicate(patch->localRHS, &patch->dof_weights));
265261c4b389SFlorian Wechsung if (patch->local_composition_type == PC_COMPOSITE_ADDITIVE) {
26534bbf5ea8SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) {
26544bbf5ea8SMatthew G. Knepley PetscInt dof;
26554bbf5ea8SMatthew G. Knepley
26569566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCounts, i + pStart, &dof));
26574bbf5ea8SMatthew G. Knepley if (dof <= 0) continue;
26589566063dSJacob Faibussowitsch PetscCall(VecSet(patch->patchRHS, 1.0));
26599566063dSJacob Faibussowitsch PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHS, patch->dof_weights, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR));
26604bbf5ea8SMatthew G. Knepley }
2661c2e6f3c0SFlorian Wechsung } else {
2662e047a90bSFlorian Wechsung /* multiplicative is actually only locally multiplicative and globally additive. need the pou where the mesh decomposition overlaps */
26639566063dSJacob Faibussowitsch PetscCall(VecSet(patch->dof_weights, 1.0));
26644bbf5ea8SMatthew G. Knepley }
2665d132cafaSFlorian Wechsung
26663ba16761SJacob Faibussowitsch PetscCall(VecDuplicate(patch->dof_weights, &global));
26673ba16761SJacob Faibussowitsch PetscCall(VecSet(global, 0.));
2668d132cafaSFlorian Wechsung
26699566063dSJacob Faibussowitsch PetscCall(VecGetArray(patch->dof_weights, &input));
26709566063dSJacob Faibussowitsch PetscCall(VecGetArray(global, &output));
26719566063dSJacob Faibussowitsch PetscCall(PetscSFReduceBegin(patch->sectionSF, MPIU_SCALAR, input, output, MPI_SUM));
26729566063dSJacob Faibussowitsch PetscCall(PetscSFReduceEnd(patch->sectionSF, MPIU_SCALAR, input, output, MPI_SUM));
26739566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(patch->dof_weights, &input));
26749566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(global, &output));
2675d132cafaSFlorian Wechsung
26769566063dSJacob Faibussowitsch PetscCall(VecReciprocal(global));
2677d132cafaSFlorian Wechsung
26789566063dSJacob Faibussowitsch PetscCall(VecGetArray(patch->dof_weights, &output));
26799566063dSJacob Faibussowitsch PetscCall(VecGetArray(global, &input));
26809566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(patch->sectionSF, MPIU_SCALAR, input, output, MPI_REPLACE));
26819566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(patch->sectionSF, MPIU_SCALAR, input, output, MPI_REPLACE));
26829566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(patch->dof_weights, &output));
26839566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(global, &input));
26849566063dSJacob Faibussowitsch PetscCall(VecDestroy(&global));
26854bbf5ea8SMatthew G. Knepley }
268648a46eb9SPierre Jolivet if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE && patch->save_operators && !patch->isNonlinear) PetscCall(PetscMalloc1(patch->npatch, &patch->matWithArtificial));
26874bbf5ea8SMatthew G. Knepley }
26889566063dSJacob Faibussowitsch PetscCall((*patch->setupsolver)(pc));
26893ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
26904bbf5ea8SMatthew G. Knepley }
2691dadc69c5SMatthew G. Knepley
PCApply_PATCH_Linear(PC pc,PetscInt i,Vec x,Vec y)2692d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_PATCH_Linear(PC pc, PetscInt i, Vec x, Vec y)
2693d71ae5a4SJacob Faibussowitsch {
2694dadc69c5SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
2695c73d2cf6SLawrence Mitchell KSP ksp;
26969d4fc724SLawrence Mitchell Mat op;
26979d4fc724SLawrence Mitchell PetscInt m, n;
2698dadc69c5SMatthew G. Knepley
2699dadc69c5SMatthew G. Knepley PetscFunctionBegin;
2700c73d2cf6SLawrence Mitchell if (patch->denseinverse) {
27019566063dSJacob Faibussowitsch PetscCall((*patch->densesolve)(patch->mat[i], x, y));
27023ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
2703c73d2cf6SLawrence Mitchell }
2704c73d2cf6SLawrence Mitchell ksp = (KSP)patch->solver[i];
2705dadc69c5SMatthew G. Knepley if (!patch->save_operators) {
2706dadc69c5SMatthew G. Knepley Mat mat;
2707dadc69c5SMatthew G. Knepley
27089566063dSJacob Faibussowitsch PetscCall(PCPatchCreateMatrix_Private(pc, i, &mat, PETSC_FALSE));
2709dadc69c5SMatthew G. Knepley /* Populate operator here. */
27109566063dSJacob Faibussowitsch PetscCall(PCPatchComputeOperator_Internal(pc, NULL, mat, i, PETSC_FALSE));
27119566063dSJacob Faibussowitsch PetscCall(KSPSetOperators(ksp, mat, mat));
2712dadc69c5SMatthew G. Knepley /* Drop reference so the KSPSetOperators below will blow it away. */
27139566063dSJacob Faibussowitsch PetscCall(MatDestroy(&mat));
2714dadc69c5SMatthew G. Knepley }
27159566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0));
271648a46eb9SPierre Jolivet if (!ksp->setfromoptionscalled) PetscCall(KSPSetFromOptions(ksp));
27179d4fc724SLawrence Mitchell /* Disgusting trick to reuse work vectors */
27189566063dSJacob Faibussowitsch PetscCall(KSPGetOperators(ksp, &op, NULL));
27199566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(op, &m, &n));
27209d4fc724SLawrence Mitchell x->map->n = m;
27219d4fc724SLawrence Mitchell y->map->n = n;
27229d4fc724SLawrence Mitchell x->map->N = m;
27239d4fc724SLawrence Mitchell y->map->N = n;
27249566063dSJacob Faibussowitsch PetscCall(KSPSolve(ksp, x, y));
27259566063dSJacob Faibussowitsch PetscCall(KSPCheckSolve(ksp, pc, y));
27269566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0));
2727dadc69c5SMatthew G. Knepley if (!patch->save_operators) {
2728dadc69c5SMatthew G. Knepley PC pc;
27299566063dSJacob Faibussowitsch PetscCall(KSPSetOperators(ksp, NULL, NULL));
27309566063dSJacob Faibussowitsch PetscCall(KSPGetPC(ksp, &pc));
2731dadc69c5SMatthew G. Knepley /* Destroy PC context too, otherwise the factored matrix hangs around. */
27329566063dSJacob Faibussowitsch PetscCall(PCReset(pc));
27334bbf5ea8SMatthew G. Knepley }
27343ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
27354bbf5ea8SMatthew G. Knepley }
27364bbf5ea8SMatthew G. Knepley
PCUpdateMultiplicative_PATCH_Linear(PC pc,PetscInt i,PetscInt pStart)2737d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCUpdateMultiplicative_PATCH_Linear(PC pc, PetscInt i, PetscInt pStart)
2738d71ae5a4SJacob Faibussowitsch {
27396c9c532dSPatrick Farrell PC_PATCH *patch = (PC_PATCH *)pc->data;
27406c9c532dSPatrick Farrell Mat multMat;
27419d4fc724SLawrence Mitchell PetscInt n, m;
27426c9c532dSPatrick Farrell
27434d04e9f1SPatrick Farrell PetscFunctionBegin;
27446c9c532dSPatrick Farrell if (patch->save_operators) {
27456c9c532dSPatrick Farrell multMat = patch->matWithArtificial[i];
27466c9c532dSPatrick Farrell } else {
27476c9c532dSPatrick Farrell /*Very inefficient, hopefully we can just assemble the rectangular matrix in the first place.*/
27486c9c532dSPatrick Farrell Mat matSquare;
27496c9c532dSPatrick Farrell PetscInt dof;
27506c9c532dSPatrick Farrell IS rowis;
27519566063dSJacob Faibussowitsch PetscCall(PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE));
27529566063dSJacob Faibussowitsch PetscCall(PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE));
27539566063dSJacob Faibussowitsch PetscCall(MatGetSize(matSquare, &dof, NULL));
27549566063dSJacob Faibussowitsch PetscCall(ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis));
27559566063dSJacob Faibussowitsch PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &multMat));
27569566063dSJacob Faibussowitsch PetscCall(MatDestroy(&matSquare));
27579566063dSJacob Faibussowitsch PetscCall(ISDestroy(&rowis));
27586c9c532dSPatrick Farrell }
27599d4fc724SLawrence Mitchell /* Disgusting trick to reuse work vectors */
27609566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(multMat, &m, &n));
27619d4fc724SLawrence Mitchell patch->patchUpdate->map->n = n;
27629d4fc724SLawrence Mitchell patch->patchRHSWithArtificial->map->n = m;
27639d4fc724SLawrence Mitchell patch->patchUpdate->map->N = n;
27649d4fc724SLawrence Mitchell patch->patchRHSWithArtificial->map->N = m;
27659566063dSJacob Faibussowitsch PetscCall(MatMult(multMat, patch->patchUpdate, patch->patchRHSWithArtificial));
27669566063dSJacob Faibussowitsch PetscCall(VecScale(patch->patchRHSWithArtificial, -1.0));
27679566063dSJacob Faibussowitsch PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHSWithArtificial, patch->localRHS, ADD_VALUES, SCATTER_REVERSE, SCATTER_WITHARTIFICIAL));
276848a46eb9SPierre Jolivet if (!patch->save_operators) PetscCall(MatDestroy(&multMat));
27693ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
27706c9c532dSPatrick Farrell }
27716c9c532dSPatrick Farrell
PCApply_PATCH(PC pc,Vec x,Vec y)2772d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_PATCH(PC pc, Vec x, Vec y)
2773d71ae5a4SJacob Faibussowitsch {
27744bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
27751202d238SPatrick Farrell const PetscScalar *globalRHS = NULL;
27761202d238SPatrick Farrell PetscScalar *localRHS = NULL;
27771202d238SPatrick Farrell PetscScalar *globalUpdate = NULL;
27784bbf5ea8SMatthew G. Knepley const PetscInt *bcNodes = NULL;
27794bbf5ea8SMatthew G. Knepley PetscInt nsweep = patch->symmetrise_sweep ? 2 : 1;
27804bbf5ea8SMatthew G. Knepley PetscInt start[2] = {0, 0};
27814bbf5ea8SMatthew G. Knepley PetscInt end[2] = {-1, -1};
27824bbf5ea8SMatthew G. Knepley const PetscInt inc[2] = {1, -1};
27831202d238SPatrick Farrell const PetscScalar *localUpdate;
27844bbf5ea8SMatthew G. Knepley const PetscInt *iterationSet;
27854bbf5ea8SMatthew G. Knepley PetscInt pStart, numBcs, n, sweep, bc, j;
27864bbf5ea8SMatthew G. Knepley
27874bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
27889566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_Apply, pc, 0, 0, 0));
2789648c30bcSBarry Smith PetscCall(PetscOptionsPushCreateViewerOff(PETSC_TRUE));
279092d50984SMatthew G. Knepley /* start, end, inc have 2 entries to manage a second backward sweep if we symmetrize */
27914bbf5ea8SMatthew G. Knepley end[0] = patch->npatch;
27924bbf5ea8SMatthew G. Knepley start[1] = patch->npatch - 1;
27934bbf5ea8SMatthew G. Knepley if (patch->user_patches) {
27949566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(patch->iterationSet, &end[0]));
27954bbf5ea8SMatthew G. Knepley start[1] = end[0] - 1;
27969566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->iterationSet, &iterationSet));
27974bbf5ea8SMatthew G. Knepley }
27984bbf5ea8SMatthew G. Knepley /* Scatter from global space into overlapped local spaces */
27999566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(x, &globalRHS));
28009566063dSJacob Faibussowitsch PetscCall(VecGetArray(patch->localRHS, &localRHS));
28019566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(patch->sectionSF, MPIU_SCALAR, globalRHS, localRHS, MPI_REPLACE));
28029566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(patch->sectionSF, MPIU_SCALAR, globalRHS, localRHS, MPI_REPLACE));
28039566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(x, &globalRHS));
28049566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(patch->localRHS, &localRHS));
28054bbf5ea8SMatthew G. Knepley
28069566063dSJacob Faibussowitsch PetscCall(VecSet(patch->localUpdate, 0.0));
28079566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, NULL));
28089566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0));
28094bbf5ea8SMatthew G. Knepley for (sweep = 0; sweep < nsweep; sweep++) {
28104bbf5ea8SMatthew G. Knepley for (j = start[sweep]; j * inc[sweep] < end[sweep] * inc[sweep]; j += inc[sweep]) {
28114bbf5ea8SMatthew G. Knepley PetscInt i = patch->user_patches ? iterationSet[j] : j;
28124bbf5ea8SMatthew G. Knepley PetscInt start, len;
28134bbf5ea8SMatthew G. Knepley
28149566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(patch->gtolCounts, i + pStart, &len));
28159566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(patch->gtolCounts, i + pStart, &start));
28164bbf5ea8SMatthew G. Knepley /* TODO: Squash out these guys in the setup as well. */
28174bbf5ea8SMatthew G. Knepley if (len <= 0) continue;
28184bbf5ea8SMatthew G. Knepley /* TODO: Do we need different scatters for X and Y? */
28199566063dSJacob Faibussowitsch PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->localRHS, patch->patchRHS, INSERT_VALUES, SCATTER_FORWARD, SCATTER_INTERIOR));
28209566063dSJacob Faibussowitsch PetscCall((*patch->applysolver)(pc, i, patch->patchRHS, patch->patchUpdate));
28219566063dSJacob Faibussowitsch PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchUpdate, patch->localUpdate, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR));
282248a46eb9SPierre Jolivet if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) PetscCall((*patch->updatemultiplicative)(pc, i, pStart));
28234bbf5ea8SMatthew G. Knepley }
28244bbf5ea8SMatthew G. Knepley }
28259566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0));
28269566063dSJacob Faibussowitsch if (patch->user_patches) PetscCall(ISRestoreIndices(patch->iterationSet, &iterationSet));
28274bbf5ea8SMatthew G. Knepley /* XXX: should we do this on the global vector? */
28281baa6e33SBarry Smith if (patch->partition_of_unity) PetscCall(VecPointwiseMult(patch->localUpdate, patch->localUpdate, patch->dof_weights));
28291202d238SPatrick Farrell /* Now patch->localUpdate contains the solution of the patch solves, so we need to combine them all. */
28309566063dSJacob Faibussowitsch PetscCall(VecSet(y, 0.0));
28319566063dSJacob Faibussowitsch PetscCall(VecGetArray(y, &globalUpdate));
28329566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(patch->localUpdate, &localUpdate));
28339566063dSJacob Faibussowitsch PetscCall(PetscSFReduceBegin(patch->sectionSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM));
28349566063dSJacob Faibussowitsch PetscCall(PetscSFReduceEnd(patch->sectionSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM));
28359566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(patch->localUpdate, &localUpdate));
28364bbf5ea8SMatthew G. Knepley
28374bbf5ea8SMatthew G. Knepley /* Now we need to send the global BC values through */
28389566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(x, &globalRHS));
28399566063dSJacob Faibussowitsch PetscCall(ISGetSize(patch->globalBcNodes, &numBcs));
28409566063dSJacob Faibussowitsch PetscCall(ISGetIndices(patch->globalBcNodes, &bcNodes));
28419566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(x, &n));
28424bbf5ea8SMatthew G. Knepley for (bc = 0; bc < numBcs; ++bc) {
28434bbf5ea8SMatthew G. Knepley const PetscInt idx = bcNodes[bc];
28441202d238SPatrick Farrell if (idx < n) globalUpdate[idx] = globalRHS[idx];
28454bbf5ea8SMatthew G. Knepley }
28464bbf5ea8SMatthew G. Knepley
28479566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(patch->globalBcNodes, &bcNodes));
28489566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(x, &globalRHS));
28499566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(y, &globalUpdate));
28504bbf5ea8SMatthew G. Knepley
2851648c30bcSBarry Smith PetscCall(PetscOptionsPopCreateViewerOff());
28529566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(PC_Patch_Apply, pc, 0, 0, 0));
28533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
28544bbf5ea8SMatthew G. Knepley }
28554bbf5ea8SMatthew G. Knepley
PCReset_PATCH_Linear(PC pc)2856d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCReset_PATCH_Linear(PC pc)
2857d71ae5a4SJacob Faibussowitsch {
2858dadc69c5SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
2859dadc69c5SMatthew G. Knepley PetscInt i;
2860dadc69c5SMatthew G. Knepley
2861dadc69c5SMatthew G. Knepley PetscFunctionBegin;
2862dadc69c5SMatthew G. Knepley if (patch->solver) {
28639566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(KSPReset((KSP)patch->solver[i]));
2864dadc69c5SMatthew G. Knepley }
28653ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
2866dadc69c5SMatthew G. Knepley }
2867dadc69c5SMatthew G. Knepley
PCReset_PATCH(PC pc)2868d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCReset_PATCH(PC pc)
2869d71ae5a4SJacob Faibussowitsch {
28704bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
28714bbf5ea8SMatthew G. Knepley PetscInt i;
28724bbf5ea8SMatthew G. Knepley
28734bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
28749566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&patch->sectionSF));
28759566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->cellCounts));
28769566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->pointCounts));
28779566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->cellNumbering));
28789566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->gtolCounts));
28799566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->gtol));
28809566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->cells));
28819566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->points));
28829566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->dofs));
28839566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->offs));
28849566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->patchSection));
28859566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->ghostBcNodes));
28869566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->globalBcNodes));
28879566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->gtolCountsWithArtificial));
28889566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->gtolWithArtificial));
28899566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->dofsWithArtificial));
28909566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->offsWithArtificial));
28919566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->gtolCountsWithAll));
28929566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->gtolWithAll));
28939566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->dofsWithAll));
28949566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->offsWithAll));
28959566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->cellMats));
28969566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->intFacetMats));
28979566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->allCells));
28989566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->intFacets));
28999566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->extFacets));
29009566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->intFacetsToPatchCell));
29019566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->intFacetCounts));
29029566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&patch->extFacetCounts));
29034bbf5ea8SMatthew G. Knepley
29049371c9d4SSatish Balay if (patch->dofSection)
29059371c9d4SSatish Balay for (i = 0; i < patch->nsubspaces; i++) PetscCall(PetscSectionDestroy(&patch->dofSection[i]));
29069566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->dofSection));
29079566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->bs));
29089566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->nodesPerCell));
29099371c9d4SSatish Balay if (patch->cellNodeMap)
29109371c9d4SSatish Balay for (i = 0; i < patch->nsubspaces; i++) PetscCall(PetscFree(patch->cellNodeMap[i]));
29119566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->cellNodeMap));
29129566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->subspaceOffsets));
29134bbf5ea8SMatthew G. Knepley
29149566063dSJacob Faibussowitsch PetscCall((*patch->resetsolver)(pc));
29154bbf5ea8SMatthew G. Knepley
291648a46eb9SPierre Jolivet if (patch->subspaces_to_exclude) PetscCall(PetscHSetIDestroy(&patch->subspaces_to_exclude));
2917e4c66b91SPatrick Farrell
29189566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->localRHS));
29199566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->localUpdate));
29209566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->patchRHS));
29219566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->patchUpdate));
29229566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->dof_weights));
29234bbf5ea8SMatthew G. Knepley if (patch->patch_dof_weights) {
29249566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(VecDestroy(&patch->patch_dof_weights[i]));
29259566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->patch_dof_weights));
29264bbf5ea8SMatthew G. Knepley }
29274bbf5ea8SMatthew G. Knepley if (patch->mat) {
29289566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(MatDestroy(&patch->mat[i]));
29299566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->mat));
29305f824522SMatthew G. Knepley }
29310997d788SPatrick Farrell if (patch->matWithArtificial && !patch->isNonlinear) {
29329566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(MatDestroy(&patch->matWithArtificial[i]));
29339566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->matWithArtificial));
293411ac8bb0SFlorian Wechsung }
29359566063dSJacob Faibussowitsch PetscCall(VecDestroy(&patch->patchRHSWithArtificial));
293696b79ebeSFlorian Wechsung if (patch->dofMappingWithoutToWithArtificial) {
29379566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->dofMappingWithoutToWithArtificial[i]));
29389566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->dofMappingWithoutToWithArtificial));
293996b79ebeSFlorian Wechsung }
29400904074fSPatrick Farrell if (patch->dofMappingWithoutToWithAll) {
29419566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->dofMappingWithoutToWithAll[i]));
29429566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->dofMappingWithoutToWithAll));
29430904074fSPatrick Farrell }
29449566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->sub_mat_type));
29455f824522SMatthew G. Knepley if (patch->userIS) {
29469566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->userIS[i]));
29479566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->userIS));
29485f824522SMatthew G. Knepley }
29499566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->precomputedTensorLocations));
29509566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->precomputedIntFacetTensorLocations));
2951f98464cbSLawrence Mitchell
29520a545947SLisandro Dalcin patch->bs = NULL;
29534bbf5ea8SMatthew G. Knepley patch->cellNodeMap = NULL;
29547974b488SMatthew G. Knepley patch->nsubspaces = 0;
29559566063dSJacob Faibussowitsch PetscCall(ISDestroy(&patch->iterationSet));
29565f824522SMatthew G. Knepley
2957648c30bcSBarry Smith PetscCall(PetscViewerDestroy(&patch->viewerCells));
2958648c30bcSBarry Smith PetscCall(PetscViewerDestroy(&patch->viewerIntFacets));
2959648c30bcSBarry Smith PetscCall(PetscViewerDestroy(&patch->viewerPoints));
2960648c30bcSBarry Smith PetscCall(PetscViewerDestroy(&patch->viewerSection));
2961648c30bcSBarry Smith PetscCall(PetscViewerDestroy(&patch->viewerMatrix));
29623ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
29634bbf5ea8SMatthew G. Knepley }
29644bbf5ea8SMatthew G. Knepley
PCDestroy_PATCH_Linear(PC pc)2965d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_PATCH_Linear(PC pc)
2966d71ae5a4SJacob Faibussowitsch {
29674bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
29684bbf5ea8SMatthew G. Knepley PetscInt i;
29694bbf5ea8SMatthew G. Knepley
29704bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
2971dadc69c5SMatthew G. Knepley if (patch->solver) {
29729566063dSJacob Faibussowitsch for (i = 0; i < patch->npatch; ++i) PetscCall(KSPDestroy((KSP *)&patch->solver[i]));
29739566063dSJacob Faibussowitsch PetscCall(PetscFree(patch->solver));
29744bbf5ea8SMatthew G. Knepley }
29753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
2976dadc69c5SMatthew G. Knepley }
2977dadc69c5SMatthew G. Knepley
PCDestroy_PATCH(PC pc)2978d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_PATCH(PC pc)
2979d71ae5a4SJacob Faibussowitsch {
2980dadc69c5SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
2981dadc69c5SMatthew G. Knepley
2982dadc69c5SMatthew G. Knepley PetscFunctionBegin;
29839566063dSJacob Faibussowitsch PetscCall(PCReset_PATCH(pc));
29849566063dSJacob Faibussowitsch PetscCall((*patch->destroysolver)(pc));
29859566063dSJacob Faibussowitsch PetscCall(PetscFree(pc->data));
29863ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
29874bbf5ea8SMatthew G. Knepley }
29884bbf5ea8SMatthew G. Knepley
PCSetFromOptions_PATCH(PC pc,PetscOptionItems PetscOptionsObject)2989ce78bad3SBarry Smith static PetscErrorCode PCSetFromOptions_PATCH(PC pc, PetscOptionItems PetscOptionsObject)
2990d71ae5a4SJacob Faibussowitsch {
29914bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
29924bbf5ea8SMatthew G. Knepley PCPatchConstructType patchConstructionType = PC_PATCH_STAR;
29935f824522SMatthew G. Knepley char sub_mat_type[PETSC_MAX_PATH_LEN];
299410534d48SPatrick Farrell char option[PETSC_MAX_PATH_LEN];
29955f824522SMatthew G. Knepley const char *prefix;
29964bbf5ea8SMatthew G. Knepley PetscBool flg, dimflg, codimflg;
29975f824522SMatthew G. Knepley MPI_Comm comm;
2998a48c39c8SPatrick Farrell PetscInt *ifields, nfields, k;
299961c4b389SFlorian Wechsung PCCompositeType loctype = PC_COMPOSITE_ADDITIVE;
30004bbf5ea8SMatthew G. Knepley
30014bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
30029566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)pc, &comm));
30039566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)pc, &prefix));
3004d0609cedSBarry Smith PetscOptionsHeadBegin(PetscOptionsObject, "Patch solver options");
300510534d48SPatrick Farrell
30069566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_save_operators", patch->classname));
30079566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool(option, "Store all patch operators for lifetime of object?", "PCPatchSetSaveOperators", patch->save_operators, &patch->save_operators, &flg));
300810534d48SPatrick Farrell
30099566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_precompute_element_tensors", patch->classname));
30109566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool(option, "Compute each element tensor only once?", "PCPatchSetPrecomputeElementTensors", patch->precomputeElementTensors, &patch->precomputeElementTensors, &flg));
30119566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_partition_of_unity", patch->classname));
30129566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool(option, "Weight contributions by dof multiplicity?", "PCPatchSetPartitionOfUnity", patch->partition_of_unity, &patch->partition_of_unity, &flg));
301310534d48SPatrick Farrell
30149566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_local_type", patch->classname));
30159566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum(option, "Type of local solver composition (additive or multiplicative)", "PCPatchSetLocalComposition", PCCompositeTypes, (PetscEnum)loctype, (PetscEnum *)&loctype, &flg));
30169566063dSJacob Faibussowitsch if (flg) PetscCall(PCPatchSetLocalComposition(pc, loctype));
30179566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_dense_inverse", patch->classname));
30189566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool(option, "Compute inverses of patch matrices and apply directly? Ignores KSP/PC settings on patch.", "PCPatchSetDenseInverse", patch->denseinverse, &patch->denseinverse, &flg));
30199566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_dim", patch->classname));
30209566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt(option, "What dimension of mesh point to construct patches by? (0 = vertices)", "PCPATCH", patch->dim, &patch->dim, &dimflg));
30219566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_codim", patch->classname));
30229566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt(option, "What co-dimension of mesh point to construct patches by? (0 = cells)", "PCPATCH", patch->codim, &patch->codim, &codimflg));
302308401ef6SPierre Jolivet PetscCheck(!dimflg || !codimflg, comm, PETSC_ERR_ARG_WRONG, "Can only set one of dimension or co-dimension");
302410534d48SPatrick Farrell
30259566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_type", patch->classname));
30269566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum(option, "How should the patches be constructed?", "PCPatchSetConstructType", PCPatchConstructTypes, (PetscEnum)patchConstructionType, (PetscEnum *)&patchConstructionType, &flg));
30279566063dSJacob Faibussowitsch if (flg) PetscCall(PCPatchSetConstructType(pc, patchConstructionType, NULL, NULL));
302810534d48SPatrick Farrell
30299566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_vanka_dim", patch->classname));
30309566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt(option, "Topological dimension of entities for Vanka to ignore", "PCPATCH", patch->vankadim, &patch->vankadim, &flg));
303110534d48SPatrick Farrell
30329566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_ignore_dim", patch->classname));
30339566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt(option, "Topological dimension of entities for completion to ignore", "PCPATCH", patch->ignoredim, &patch->ignoredim, &flg));
303410534d48SPatrick Farrell
30359566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_pardecomp_overlap", patch->classname));
30369566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt(option, "What overlap should we use in construct type pardecomp?", "PCPATCH", patch->pardecomp_overlap, &patch->pardecomp_overlap, &flg));
3037b525f888SPatrick Farrell
30389566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_sub_mat_type", patch->classname));
30399566063dSJacob Faibussowitsch PetscCall(PetscOptionsFList(option, "Matrix type for patch solves", "PCPatchSetSubMatType", MatList, NULL, sub_mat_type, PETSC_MAX_PATH_LEN, &flg));
30409566063dSJacob Faibussowitsch if (flg) PetscCall(PCPatchSetSubMatType(pc, sub_mat_type));
304110534d48SPatrick Farrell
30429566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_symmetrise_sweep", patch->classname));
30439566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool(option, "Go start->end, end->start?", "PCPATCH", patch->symmetrise_sweep, &patch->symmetrise_sweep, &flg));
3044e4c66b91SPatrick Farrell
3045a48c39c8SPatrick Farrell /* If the user has set the number of subspaces, use that for the buffer size,
3046a48c39c8SPatrick Farrell otherwise use a large number */
3047a48c39c8SPatrick Farrell if (patch->nsubspaces <= 0) {
3048a48c39c8SPatrick Farrell nfields = 128;
3049a48c39c8SPatrick Farrell } else {
3050a48c39c8SPatrick Farrell nfields = patch->nsubspaces;
3051a48c39c8SPatrick Farrell }
30529566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nfields, &ifields));
30539566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exclude_subspaces", patch->classname));
30549566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetIntArray(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, option, ifields, &nfields, &flg));
305508401ef6SPierre 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");
3056e4c66b91SPatrick Farrell if (flg) {
30579566063dSJacob Faibussowitsch PetscCall(PetscHSetIClear(patch->subspaces_to_exclude));
305848a46eb9SPierre Jolivet for (k = 0; k < nfields; k++) PetscCall(PetscHSetIAdd(patch->subspaces_to_exclude, ifields[k]));
3059e4c66b91SPatrick Farrell }
30609566063dSJacob Faibussowitsch PetscCall(PetscFree(ifields));
30615f824522SMatthew G. Knepley
30629566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_patches_view", patch->classname));
30639566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool(option, "Print out information during patch construction", "PCPATCH", patch->viewPatches, &patch->viewPatches, &flg));
30649566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_cells_view", patch->classname));
3065648c30bcSBarry Smith PetscCall(PetscOptionsCreateViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerCells, &patch->formatCells, &patch->viewCells));
30669566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_interior_facets_view", patch->classname));
3067648c30bcSBarry Smith PetscCall(PetscOptionsCreateViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerIntFacets, &patch->formatIntFacets, &patch->viewIntFacets));
30689566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exterior_facets_view", patch->classname));
3069648c30bcSBarry Smith PetscCall(PetscOptionsCreateViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerExtFacets, &patch->formatExtFacets, &patch->viewExtFacets));
30709566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_points_view", patch->classname));
3071648c30bcSBarry Smith PetscCall(PetscOptionsCreateViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerPoints, &patch->formatPoints, &patch->viewPoints));
30729566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_section_view", patch->classname));
3073648c30bcSBarry Smith PetscCall(PetscOptionsCreateViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerSection, &patch->formatSection, &patch->viewSection));
30749566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_mat_view", patch->classname));
3075648c30bcSBarry Smith PetscCall(PetscOptionsCreateViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerMatrix, &patch->formatMatrix, &patch->viewMatrix));
3076d0609cedSBarry Smith PetscOptionsHeadEnd();
30775f824522SMatthew G. Knepley patch->optionsSet = PETSC_TRUE;
30783ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
30794bbf5ea8SMatthew G. Knepley }
30804bbf5ea8SMatthew G. Knepley
PCSetUpOnBlocks_PATCH(PC pc)3081d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUpOnBlocks_PATCH(PC pc)
3082d71ae5a4SJacob Faibussowitsch {
30834bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
30844bbf5ea8SMatthew G. Knepley KSPConvergedReason reason;
30854bbf5ea8SMatthew G. Knepley PetscInt i;
30864bbf5ea8SMatthew G. Knepley
30874bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
3088a1eac568SLawrence Mitchell if (!patch->save_operators) {
3089a1eac568SLawrence Mitchell /* Can't do this here because the sub KSPs don't have an operator attached yet. */
30903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3091a1eac568SLawrence Mitchell }
3092c73d2cf6SLawrence Mitchell if (patch->denseinverse) {
3093c73d2cf6SLawrence Mitchell /* No solvers */
30943ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3095c73d2cf6SLawrence Mitchell }
30964bbf5ea8SMatthew G. Knepley for (i = 0; i < patch->npatch; ++i) {
309748a46eb9SPierre Jolivet if (!((KSP)patch->solver[i])->setfromoptionscalled) PetscCall(KSPSetFromOptions((KSP)patch->solver[i]));
30989566063dSJacob Faibussowitsch PetscCall(KSPSetUp((KSP)patch->solver[i]));
30999566063dSJacob Faibussowitsch PetscCall(KSPGetConvergedReason((KSP)patch->solver[i], &reason));
3100c0decd05SBarry Smith if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR;
31014bbf5ea8SMatthew G. Knepley }
31023ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
31034bbf5ea8SMatthew G. Knepley }
31044bbf5ea8SMatthew G. Knepley
PCView_PATCH(PC pc,PetscViewer viewer)3105d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCView_PATCH(PC pc, PetscViewer viewer)
3106d71ae5a4SJacob Faibussowitsch {
31074bbf5ea8SMatthew G. Knepley PC_PATCH *patch = (PC_PATCH *)pc->data;
31084bbf5ea8SMatthew G. Knepley PetscViewer sviewer;
31094bbf5ea8SMatthew G. Knepley PetscBool isascii;
31104bbf5ea8SMatthew G. Knepley PetscMPIInt rank;
31114bbf5ea8SMatthew G. Knepley
31124bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
31134bbf5ea8SMatthew G. Knepley /* TODO Redo tabbing with set tbas in new style */
31149566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
31153ba16761SJacob Faibussowitsch if (!isascii) PetscFunctionReturn(PETSC_SUCCESS);
31169566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
31179566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer));
311863a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Subspace Correction preconditioner with %" PetscInt_FMT " patches\n", patch->npatch));
311961c4b389SFlorian Wechsung if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
31209566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Schwarz type: multiplicative\n"));
3121e047a90bSFlorian Wechsung } else {
31229566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Schwarz type: additive\n"));
3123c2e6f3c0SFlorian Wechsung }
31249566063dSJacob Faibussowitsch if (patch->partition_of_unity) PetscCall(PetscViewerASCIIPrintf(viewer, "Weighting by partition of unity\n"));
31259566063dSJacob Faibussowitsch else PetscCall(PetscViewerASCIIPrintf(viewer, "Not weighting by partition of unity\n"));
31269566063dSJacob Faibussowitsch if (patch->symmetrise_sweep) PetscCall(PetscViewerASCIIPrintf(viewer, "Symmetrising sweep (start->end, then end->start)\n"));
31279566063dSJacob Faibussowitsch else PetscCall(PetscViewerASCIIPrintf(viewer, "Not symmetrising sweep\n"));
31289566063dSJacob Faibussowitsch if (!patch->precomputeElementTensors) PetscCall(PetscViewerASCIIPrintf(viewer, "Not precomputing element tensors (overlapping cells rebuilt in every patch assembly)\n"));
31299566063dSJacob Faibussowitsch else PetscCall(PetscViewerASCIIPrintf(viewer, "Precomputing element tensors (each cell assembled only once)\n"));
31309566063dSJacob Faibussowitsch if (!patch->save_operators) PetscCall(PetscViewerASCIIPrintf(viewer, "Not saving patch operators (rebuilt every PCApply)\n"));
31319566063dSJacob Faibussowitsch else PetscCall(PetscViewerASCIIPrintf(viewer, "Saving patch operators (rebuilt every PCSetUp)\n"));
31329566063dSJacob Faibussowitsch if (patch->patchconstructop == PCPatchConstruct_Star) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: star\n"));
31339566063dSJacob Faibussowitsch else if (patch->patchconstructop == PCPatchConstruct_Vanka) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: Vanka\n"));
31349566063dSJacob Faibussowitsch else if (patch->patchconstructop == PCPatchConstruct_User) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: user-specified\n"));
31359566063dSJacob Faibussowitsch else PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: unknown\n"));
31365d30859aSPatrick Farrell
3137c73d2cf6SLawrence Mitchell if (patch->denseinverse) {
31389566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Explicitly forming dense inverse and applying patch solver via MatMult.\n"));
3139c73d2cf6SLawrence Mitchell } else {
31405d30859aSPatrick Farrell if (patch->isNonlinear) {
31419566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "SNES on patches (all same):\n"));
31425d30859aSPatrick Farrell } else {
31439566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "KSP on patches (all same):\n"));
31445d30859aSPatrick Farrell }
3145dadc69c5SMatthew G. Knepley if (patch->solver) {
31469566063dSJacob Faibussowitsch PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
3147dd400576SPatrick Sanan if (rank == 0) {
31489566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(sviewer));
31499566063dSJacob Faibussowitsch PetscCall(PetscObjectView(patch->solver[0], sviewer));
31509566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(sviewer));
31514bbf5ea8SMatthew G. Knepley }
31529566063dSJacob Faibussowitsch PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
31534bbf5ea8SMatthew G. Knepley } else {
31549566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer));
31559566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Solver not yet set.\n"));
31569566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer));
31574bbf5ea8SMatthew G. Knepley }
3158c73d2cf6SLawrence Mitchell }
31599566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer));
31603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
31614bbf5ea8SMatthew G. Knepley }
31624bbf5ea8SMatthew G. Knepley
3163e5893cccSMatthew G. Knepley /*MC
3164f1580f4eSBarry Smith PCPATCH - A `PC` object that encapsulates flexible definition of blocks for overlapping and non-overlapping
316598ed095eSMatthew G. Knepley small block additive preconditioners. Block definition is based on topology from
3166f1580f4eSBarry Smith a `DM` and equation numbering from a `PetscSection`.
3167e5893cccSMatthew G. Knepley
3168e5893cccSMatthew G. Knepley Options Database Keys:
3169e5893cccSMatthew G. Knepley + -pc_patch_cells_view - Views the process local cell numbers for each patch
3170e5893cccSMatthew G. Knepley . -pc_patch_points_view - Views the process local mesh point numbers for each patch
3171e5893cccSMatthew G. Knepley . -pc_patch_g2l_view - Views the map between global dofs and patch local dofs for each patch
3172e5893cccSMatthew G. Knepley . -pc_patch_patches_view - Views the global dofs associated with each patch and its boundary
3173e5893cccSMatthew G. Knepley - -pc_patch_sub_mat_view - Views the matrix associated with each patch
3174e5893cccSMatthew G. Knepley
3175e5893cccSMatthew G. Knepley Level: intermediate
3176e5893cccSMatthew G. Knepley
3177562efe2eSBarry Smith .seealso: [](ch_ksp), `PCType`, `PCCreate()`, `PCSetType()`, `PCASM`, `PCJACOBI`, `PCPBJACOBI`, `PCVPBJACOBI`, `SNESPATCH`
3178e5893cccSMatthew G. Knepley M*/
PCCreate_Patch(PC pc)3179d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PCCreate_Patch(PC pc)
3180d71ae5a4SJacob Faibussowitsch {
31814bbf5ea8SMatthew G. Knepley PC_PATCH *patch;
31824bbf5ea8SMatthew G. Knepley
31834bbf5ea8SMatthew G. Knepley PetscFunctionBegin;
3184f39ec787SMatthew G. Knepley PetscCall(PetscCitationsRegister(PCPatchCitation, &PCPatchcite));
31854dfa11a4SJacob Faibussowitsch PetscCall(PetscNew(&patch));
31864bbf5ea8SMatthew G. Knepley
318748a46eb9SPierre Jolivet if (patch->subspaces_to_exclude) PetscCall(PetscHSetIDestroy(&patch->subspaces_to_exclude));
31889566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&patch->subspaces_to_exclude));
3189e4c66b91SPatrick Farrell
319010534d48SPatrick Farrell patch->classname = "pc";
3191debbdec3SPatrick Farrell patch->isNonlinear = PETSC_FALSE;
319210534d48SPatrick Farrell
31934bbf5ea8SMatthew G. Knepley /* Set some defaults */
31945f824522SMatthew G. Knepley patch->combined = PETSC_FALSE;
31954bbf5ea8SMatthew G. Knepley patch->save_operators = PETSC_TRUE;
319661c4b389SFlorian Wechsung patch->local_composition_type = PC_COMPOSITE_ADDITIVE;
3197fa84ea4cSLawrence Mitchell patch->precomputeElementTensors = PETSC_FALSE;
31984bbf5ea8SMatthew G. Knepley patch->partition_of_unity = PETSC_FALSE;
31994bbf5ea8SMatthew G. Knepley patch->codim = -1;
32004bbf5ea8SMatthew G. Knepley patch->dim = -1;
32014bbf5ea8SMatthew G. Knepley patch->vankadim = -1;
32025f824522SMatthew G. Knepley patch->ignoredim = -1;
3203b525f888SPatrick Farrell patch->pardecomp_overlap = 0;
32044bbf5ea8SMatthew G. Knepley patch->patchconstructop = PCPatchConstruct_Star;
32054bbf5ea8SMatthew G. Knepley patch->symmetrise_sweep = PETSC_FALSE;
32065f824522SMatthew G. Knepley patch->npatch = 0;
32074bbf5ea8SMatthew G. Knepley patch->userIS = NULL;
32085f824522SMatthew G. Knepley patch->optionsSet = PETSC_FALSE;
32094bbf5ea8SMatthew G. Knepley patch->iterationSet = NULL;
32104bbf5ea8SMatthew G. Knepley patch->user_patches = PETSC_FALSE;
32119566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(MATDENSE, (char **)&patch->sub_mat_type));
32125f824522SMatthew G. Knepley patch->viewPatches = PETSC_FALSE;
32135f824522SMatthew G. Knepley patch->viewCells = PETSC_FALSE;
32145f824522SMatthew G. Knepley patch->viewPoints = PETSC_FALSE;
32155f824522SMatthew G. Knepley patch->viewSection = PETSC_FALSE;
32165f824522SMatthew G. Knepley patch->viewMatrix = PETSC_FALSE;
32179d4fc724SLawrence Mitchell patch->densesolve = NULL;
3218dadc69c5SMatthew G. Knepley patch->setupsolver = PCSetUp_PATCH_Linear;
3219dadc69c5SMatthew G. Knepley patch->applysolver = PCApply_PATCH_Linear;
3220dadc69c5SMatthew G. Knepley patch->resetsolver = PCReset_PATCH_Linear;
3221dadc69c5SMatthew G. Knepley patch->destroysolver = PCDestroy_PATCH_Linear;
32226c9c532dSPatrick Farrell patch->updatemultiplicative = PCUpdateMultiplicative_PATCH_Linear;
322347aca4a6SPatrick Farrell patch->dofMappingWithoutToWithArtificial = NULL;
322447aca4a6SPatrick Farrell patch->dofMappingWithoutToWithAll = NULL;
32254bbf5ea8SMatthew G. Knepley
32264bbf5ea8SMatthew G. Knepley pc->data = (void *)patch;
32274bbf5ea8SMatthew G. Knepley pc->ops->apply = PCApply_PATCH;
32280a545947SLisandro Dalcin pc->ops->applytranspose = NULL; /* PCApplyTranspose_PATCH; */
32294bbf5ea8SMatthew G. Knepley pc->ops->setup = PCSetUp_PATCH;
32304bbf5ea8SMatthew G. Knepley pc->ops->reset = PCReset_PATCH;
32314bbf5ea8SMatthew G. Knepley pc->ops->destroy = PCDestroy_PATCH;
32324bbf5ea8SMatthew G. Knepley pc->ops->setfromoptions = PCSetFromOptions_PATCH;
32334bbf5ea8SMatthew G. Knepley pc->ops->setuponblocks = PCSetUpOnBlocks_PATCH;
32344bbf5ea8SMatthew G. Knepley pc->ops->view = PCView_PATCH;
32350a545947SLisandro Dalcin pc->ops->applyrichardson = NULL;
32363ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
32374bbf5ea8SMatthew G. Knepley }
3238