xref: /petsc/src/ksp/pc/impls/patch/pcpatch.c (revision 648c30bcb65f74c3cbd15d7a91a7ed7c1890e25b)
14bbf5ea8SMatthew G. Knepley #include <petsc/private/pcpatchimpl.h> /*I "petscpc.h" I*/
254ab768cSLawrence Mitchell #include <petsc/private/kspimpl.h>     /* For ksp->setfromoptionscalled */
39d4fc724SLawrence Mitchell #include <petsc/private/vecimpl.h>     /* For vec->map */
45f824522SMatthew G. Knepley #include <petsc/private/dmpleximpl.h>  /* For DMPlexComputeJacobian_Patch_Internal() */
54bbf5ea8SMatthew G. Knepley #include <petscsf.h>
64bbf5ea8SMatthew G. Knepley #include <petscbt.h>
75f824522SMatthew G. Knepley #include <petscds.h>
8c73d2cf6SLawrence Mitchell #include <../src/mat/impls/dense/seq/dense.h> /*I "petscmat.h" I*/
94bbf5ea8SMatthew G. Knepley 
10f39ec787SMatthew G. Knepley PetscBool  PCPatchcite       = PETSC_FALSE;
11f39ec787SMatthew G. Knepley const char PCPatchCitation[] = "@article{FarrellKnepleyWechsungMitchell2020,\n"
12f39ec787SMatthew G. Knepley                                "title   = {{PCPATCH}: software for the topological construction of multigrid relaxation methods},\n"
13f39ec787SMatthew G. Knepley                                "author  = {Patrick E Farrell and Matthew G Knepley and Lawrence Mitchell and Florian Wechsung},\n"
14f39ec787SMatthew G. Knepley                                "journal = {ACM Transaction on Mathematical Software},\n"
15f39ec787SMatthew G. Knepley                                "eprint  = {http://arxiv.org/abs/1912.08516},\n"
16f39ec787SMatthew G. Knepley                                "volume  = {47},\n"
17f39ec787SMatthew G. Knepley                                "number  = {3},\n"
18f39ec787SMatthew G. Knepley                                "pages   = {1--22},\n"
19f39ec787SMatthew G. Knepley                                "year    = {2021},\n"
20f39ec787SMatthew G. Knepley                                "petsc_uses={KSP,DMPlex}\n}\n";
21f39ec787SMatthew G. Knepley 
229d4fc724SLawrence Mitchell PetscLogEvent PC_Patch_CreatePatches, PC_Patch_ComputeOp, PC_Patch_Solve, PC_Patch_Apply, PC_Patch_Prealloc;
234bbf5ea8SMatthew G. Knepley 
24d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode ObjectView(PetscObject obj, PetscViewer viewer, PetscViewerFormat format)
25d71ae5a4SJacob Faibussowitsch {
269566063dSJacob Faibussowitsch   PetscCall(PetscViewerPushFormat(viewer, format));
279566063dSJacob Faibussowitsch   PetscCall(PetscObjectView(obj, viewer));
289566063dSJacob Faibussowitsch   PetscCall(PetscViewerPopFormat(viewer));
293ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
305f824522SMatthew G. Knepley }
315f824522SMatthew G. Knepley 
32d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchConstruct_Star(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
33d71ae5a4SJacob Faibussowitsch {
344bbf5ea8SMatthew G. Knepley   PetscInt  starSize;
354bbf5ea8SMatthew G. Knepley   PetscInt *star = NULL, si;
364bbf5ea8SMatthew G. Knepley 
374bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
389566063dSJacob Faibussowitsch   PetscCall(PetscHSetIClear(ht));
394bbf5ea8SMatthew G. Knepley   /* To start with, add the point we care about */
409566063dSJacob Faibussowitsch   PetscCall(PetscHSetIAdd(ht, point));
414bbf5ea8SMatthew G. Knepley   /* Loop over all the points that this point connects to */
429566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star));
439566063dSJacob Faibussowitsch   for (si = 0; si < starSize * 2; si += 2) PetscCall(PetscHSetIAdd(ht, star[si]));
449566063dSJacob Faibussowitsch   PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star));
453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
464bbf5ea8SMatthew G. Knepley }
474bbf5ea8SMatthew G. Knepley 
48d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchConstruct_Vanka(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
49d71ae5a4SJacob Faibussowitsch {
504bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)vpatch;
514bbf5ea8SMatthew G. Knepley   PetscInt  starSize;
524bbf5ea8SMatthew G. Knepley   PetscInt *star         = NULL;
534bbf5ea8SMatthew G. Knepley   PetscBool shouldIgnore = PETSC_FALSE;
544bbf5ea8SMatthew G. Knepley   PetscInt  cStart, cEnd, iStart, iEnd, si;
554bbf5ea8SMatthew G. Knepley 
564bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
579566063dSJacob Faibussowitsch   PetscCall(PetscHSetIClear(ht));
584bbf5ea8SMatthew G. Knepley   /* To start with, add the point we care about */
599566063dSJacob Faibussowitsch   PetscCall(PetscHSetIAdd(ht, point));
604bbf5ea8SMatthew G. Knepley   /* Should we ignore any points of a certain dimension? */
614bbf5ea8SMatthew G. Knepley   if (patch->vankadim >= 0) {
624bbf5ea8SMatthew G. Knepley     shouldIgnore = PETSC_TRUE;
639566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, patch->vankadim, &iStart, &iEnd));
644bbf5ea8SMatthew G. Knepley   }
659566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
664bbf5ea8SMatthew G. Knepley   /* Loop over all the cells that this point connects to */
679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star));
685f824522SMatthew G. Knepley   for (si = 0; si < starSize * 2; si += 2) {
694bbf5ea8SMatthew G. Knepley     const PetscInt cell = star[si];
704bbf5ea8SMatthew G. Knepley     PetscInt       closureSize;
714bbf5ea8SMatthew G. Knepley     PetscInt      *closure = NULL, ci;
724bbf5ea8SMatthew G. Knepley 
734bbf5ea8SMatthew G. Knepley     if (cell < cStart || cell >= cEnd) continue;
744bbf5ea8SMatthew G. Knepley     /* now loop over all entities in the closure of that cell */
759566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
765f824522SMatthew G. Knepley     for (ci = 0; ci < closureSize * 2; ci += 2) {
774bbf5ea8SMatthew G. Knepley       const PetscInt newpoint = closure[ci];
784bbf5ea8SMatthew G. Knepley 
794bbf5ea8SMatthew G. Knepley       /* We've been told to ignore entities of this type.*/
804bbf5ea8SMatthew G. Knepley       if (shouldIgnore && newpoint >= iStart && newpoint < iEnd) continue;
819566063dSJacob Faibussowitsch       PetscCall(PetscHSetIAdd(ht, newpoint));
824bbf5ea8SMatthew G. Knepley     }
839566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
844bbf5ea8SMatthew G. Knepley   }
859566063dSJacob Faibussowitsch   PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star));
863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
874bbf5ea8SMatthew G. Knepley }
884bbf5ea8SMatthew G. Knepley 
89d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchConstruct_Pardecomp(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
90d71ae5a4SJacob Faibussowitsch {
91b525f888SPatrick Farrell   PC_PATCH       *patch   = (PC_PATCH *)vpatch;
920a390943SPatrick Farrell   DMLabel         ghost   = NULL;
93eae3dc7dSJacob Faibussowitsch   const PetscInt *leaves  = NULL;
94eae3dc7dSJacob Faibussowitsch   PetscInt        nleaves = 0, pStart, pEnd, loc;
950a390943SPatrick Farrell   PetscBool       isFiredrake;
960a390943SPatrick Farrell   PetscBool       flg;
97b525f888SPatrick Farrell   PetscInt        starSize;
98b525f888SPatrick Farrell   PetscInt       *star = NULL;
9925fd193aSPatrick Farrell   PetscInt        opoint, overlapi;
1000a390943SPatrick Farrell 
1010a390943SPatrick Farrell   PetscFunctionBegin;
1029566063dSJacob Faibussowitsch   PetscCall(PetscHSetIClear(ht));
1030a390943SPatrick Farrell 
1049566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
1050a390943SPatrick Farrell 
1069566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, "pyop2_ghost", &isFiredrake));
1070a390943SPatrick Farrell   if (isFiredrake) {
1089566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, "pyop2_ghost", &ghost));
1099566063dSJacob Faibussowitsch     PetscCall(DMLabelCreateIndex(ghost, pStart, pEnd));
1100a390943SPatrick Farrell   } else {
1110a390943SPatrick Farrell     PetscSF sf;
1129566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sf));
1139566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL));
1140a390943SPatrick Farrell     nleaves = PetscMax(nleaves, 0);
1150a390943SPatrick Farrell   }
1160a390943SPatrick Farrell 
11725fd193aSPatrick Farrell   for (opoint = pStart; opoint < pEnd; ++opoint) {
1189566063dSJacob Faibussowitsch     if (ghost) PetscCall(DMLabelHasPoint(ghost, opoint, &flg));
1199371c9d4SSatish Balay     else {
1209371c9d4SSatish Balay       PetscCall(PetscFindInt(opoint, nleaves, leaves, &loc));
1219371c9d4SSatish Balay       flg = loc >= 0 ? PETSC_TRUE : PETSC_FALSE;
1229371c9d4SSatish Balay     }
1230a390943SPatrick Farrell     /* Not an owned entity, don't make a cell patch. */
1240a390943SPatrick Farrell     if (flg) continue;
1259566063dSJacob Faibussowitsch     PetscCall(PetscHSetIAdd(ht, opoint));
1260a390943SPatrick Farrell   }
1270a390943SPatrick Farrell 
128b525f888SPatrick Farrell   /* Now build the overlap for the patch */
12925fd193aSPatrick Farrell   for (overlapi = 0; overlapi < patch->pardecomp_overlap; ++overlapi) {
130b525f888SPatrick Farrell     PetscInt  index    = 0;
131b525f888SPatrick Farrell     PetscInt *htpoints = NULL;
132b525f888SPatrick Farrell     PetscInt  htsize;
13325fd193aSPatrick Farrell     PetscInt  i;
134b525f888SPatrick Farrell 
1359566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(ht, &htsize));
1369566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(htsize, &htpoints));
1379566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetElems(ht, &index, htpoints));
138b525f888SPatrick Farrell 
13925fd193aSPatrick Farrell     for (i = 0; i < htsize; ++i) {
14025fd193aSPatrick Farrell       PetscInt hpoint = htpoints[i];
14125fd193aSPatrick Farrell       PetscInt si;
142b525f888SPatrick Farrell 
1439566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star));
14425fd193aSPatrick Farrell       for (si = 0; si < starSize * 2; si += 2) {
145b525f888SPatrick Farrell         const PetscInt starp = star[si];
146b525f888SPatrick Farrell         PetscInt       closureSize;
147b525f888SPatrick Farrell         PetscInt      *closure = NULL, ci;
148b525f888SPatrick Farrell 
149b525f888SPatrick Farrell         /* now loop over all entities in the closure of starp */
1509566063dSJacob Faibussowitsch         PetscCall(DMPlexGetTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure));
15125fd193aSPatrick Farrell         for (ci = 0; ci < closureSize * 2; ci += 2) {
152b525f888SPatrick Farrell           const PetscInt closstarp = closure[ci];
1539566063dSJacob Faibussowitsch           PetscCall(PetscHSetIAdd(ht, closstarp));
154b525f888SPatrick Farrell         }
1559566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure));
156b525f888SPatrick Farrell       }
1579566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star));
158b525f888SPatrick Farrell     }
1599566063dSJacob Faibussowitsch     PetscCall(PetscFree(htpoints));
160b525f888SPatrick Farrell   }
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 */
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 
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   PetscInt  i;
1924bbf5ea8SMatthew G. Knepley 
1934bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1944bbf5ea8SMatthew G. Knepley   if (n == 1 && bs[0] == 1) {
1951bb6d2a8SBarry Smith     patch->sectionSF = sf[0];
1969566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)patch->sectionSF));
1974bbf5ea8SMatthew G. Knepley   } else {
1984bbf5ea8SMatthew G. Knepley     PetscInt     allRoots = 0, allLeaves = 0;
1994bbf5ea8SMatthew G. Knepley     PetscInt     leafOffset    = 0;
2004bbf5ea8SMatthew G. Knepley     PetscInt    *ilocal        = NULL;
2014bbf5ea8SMatthew G. Knepley     PetscSFNode *iremote       = NULL;
2024bbf5ea8SMatthew G. Knepley     PetscInt    *remoteOffsets = NULL;
2034bbf5ea8SMatthew G. Knepley     PetscInt     index         = 0;
2041b68eb51SMatthew G. Knepley     PetscHMapI   rankToIndex;
2054bbf5ea8SMatthew G. Knepley     PetscInt     numRanks = 0;
2064bbf5ea8SMatthew G. Knepley     PetscSFNode *remote   = NULL;
2074bbf5ea8SMatthew G. Knepley     PetscSF      rankSF;
2084bbf5ea8SMatthew G. Knepley     PetscInt    *ranks   = NULL;
2094bbf5ea8SMatthew G. Knepley     PetscInt    *offsets = NULL;
2104bbf5ea8SMatthew G. Knepley     MPI_Datatype contig;
2111b68eb51SMatthew G. Knepley     PetscHSetI   ranksUniq;
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     */
2174bbf5ea8SMatthew G. Knepley     for (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));
2284bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2294bbf5ea8SMatthew G. Knepley       const PetscMPIInt *ranks = NULL;
2304bbf5ea8SMatthew G. Knepley       PetscInt           nranks, j;
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. */
23548a46eb9SPierre Jolivet       for (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));
2434bbf5ea8SMatthew G. Knepley     for (i = 0; i < numRanks; ++i) {
2444bbf5ea8SMatthew G. Knepley       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;
2584bbf5ea8SMatthew G. Knepley     for (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. */
2659566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_contiguous(n, MPIU_INT, &contig));
2669566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_commit(&contig));
2674bbf5ea8SMatthew G. Knepley 
2689566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(rankSF, contig, offsets, remoteOffsets, MPI_REPLACE));
2699566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(rankSF, contig, offsets, remoteOffsets, MPI_REPLACE));
2709566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_free(&contig));
2719566063dSJacob Faibussowitsch     PetscCall(PetscFree(offsets));
2729566063dSJacob Faibussowitsch     PetscCall(PetscSFDestroy(&rankSF));
2734bbf5ea8SMatthew G. Knepley     /* Now remoteOffsets contains the offsets on the remote
274f1580f4eSBarry Smith       processes who communicate with me.  So now we can
275f1580f4eSBarry Smith       concatenate the list of SFs into a single one. */
2764bbf5ea8SMatthew G. Knepley     index = 0;
2774bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2784bbf5ea8SMatthew G. Knepley       const PetscSFNode *remote = NULL;
2794bbf5ea8SMatthew G. Knepley       const PetscInt    *local  = NULL;
2804bbf5ea8SMatthew G. Knepley       PetscInt           nroots, nleaves, j;
2814bbf5ea8SMatthew G. Knepley 
2829566063dSJacob Faibussowitsch       PetscCall(PetscSFGetGraph(sf[i], &nroots, &nleaves, &local, &remote));
2834bbf5ea8SMatthew G. Knepley       for (j = 0; j < nleaves; ++j) {
2844bbf5ea8SMatthew G. Knepley         PetscInt rank = remote[j].rank;
2854bbf5ea8SMatthew G. Knepley         PetscInt idx, rootOffset, k;
2864bbf5ea8SMatthew G. Knepley 
2879566063dSJacob Faibussowitsch         PetscCall(PetscHMapIGet(rankToIndex, rank, &idx));
28808401ef6SPierre Jolivet         PetscCheck(idx != -1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Didn't find rank, huh?");
2894bbf5ea8SMatthew G. Knepley         /* Offset on given rank for ith subspace */
2904bbf5ea8SMatthew G. Knepley         rootOffset = remoteOffsets[n * idx + i];
2914bbf5ea8SMatthew G. Knepley         for (k = 0; k < bs[i]; ++k) {
29273ec7555SLawrence Mitchell           ilocal[index]        = (local ? local[j] : j) * bs[i] + k + leafOffset;
2934bbf5ea8SMatthew G. Knepley           iremote[index].rank  = remote[j].rank;
2944bbf5ea8SMatthew G. Knepley           iremote[index].index = remote[j].index * bs[i] + k + rootOffset;
2954bbf5ea8SMatthew G. Knepley           ++index;
2964bbf5ea8SMatthew G. Knepley         }
2974bbf5ea8SMatthew G. Knepley       }
2984bbf5ea8SMatthew G. Knepley       leafOffset += nleaves * bs[i];
2994bbf5ea8SMatthew G. Knepley     }
3009566063dSJacob Faibussowitsch     PetscCall(PetscHMapIDestroy(&rankToIndex));
3019566063dSJacob Faibussowitsch     PetscCall(PetscFree(remoteOffsets));
3029566063dSJacob Faibussowitsch     PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)pc), &patch->sectionSF));
3039566063dSJacob Faibussowitsch     PetscCall(PetscSFSetGraph(patch->sectionSF, allRoots, allLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER));
3044bbf5ea8SMatthew G. Knepley   }
3053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3064bbf5ea8SMatthew G. Knepley }
3074bbf5ea8SMatthew G. Knepley 
3084bbf5ea8SMatthew G. Knepley /* TODO: Docs */
309ba38deedSJacob Faibussowitsch static PetscErrorCode PCPatchGetIgnoreDim(PC pc, PetscInt *dim)
310d71ae5a4SJacob Faibussowitsch {
3115f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
3124d86920dSPierre Jolivet 
3135f824522SMatthew G. Knepley   PetscFunctionBegin;
3145f824522SMatthew G. Knepley   *dim = patch->ignoredim;
3153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3165f824522SMatthew G. Knepley }
3175f824522SMatthew G. Knepley 
3185f824522SMatthew G. Knepley /* TODO: Docs */
319d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetSaveOperators(PC pc, PetscBool flg)
320d71ae5a4SJacob Faibussowitsch {
3214bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
3224d86920dSPierre Jolivet 
3234bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3244bbf5ea8SMatthew G. Knepley   patch->save_operators = flg;
3253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3264bbf5ea8SMatthew G. Knepley }
3274bbf5ea8SMatthew G. Knepley 
3284bbf5ea8SMatthew G. Knepley /* TODO: Docs */
329d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetSaveOperators(PC pc, PetscBool *flg)
330d71ae5a4SJacob Faibussowitsch {
3314bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
3324d86920dSPierre Jolivet 
3334bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3344bbf5ea8SMatthew G. Knepley   *flg = patch->save_operators;
3353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3364bbf5ea8SMatthew G. Knepley }
3374bbf5ea8SMatthew G. Knepley 
3384bbf5ea8SMatthew G. Knepley /* TODO: Docs */
339d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetPrecomputeElementTensors(PC pc, PetscBool flg)
340d71ae5a4SJacob Faibussowitsch {
341fa84ea4cSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *)pc->data;
3424d86920dSPierre Jolivet 
343fa84ea4cSLawrence Mitchell   PetscFunctionBegin;
344fa84ea4cSLawrence Mitchell   patch->precomputeElementTensors = flg;
3453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
346fa84ea4cSLawrence Mitchell }
347fa84ea4cSLawrence Mitchell 
348fa84ea4cSLawrence Mitchell /* TODO: Docs */
349d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetPrecomputeElementTensors(PC pc, PetscBool *flg)
350d71ae5a4SJacob Faibussowitsch {
351fa84ea4cSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *)pc->data;
3524d86920dSPierre Jolivet 
353fa84ea4cSLawrence Mitchell   PetscFunctionBegin;
354fa84ea4cSLawrence Mitchell   *flg = patch->precomputeElementTensors;
3553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
356fa84ea4cSLawrence Mitchell }
357fa84ea4cSLawrence Mitchell 
358fa84ea4cSLawrence Mitchell /* TODO: Docs */
359d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetPartitionOfUnity(PC pc, PetscBool flg)
360d71ae5a4SJacob Faibussowitsch {
3614bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
3624d86920dSPierre Jolivet 
3634bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3644bbf5ea8SMatthew G. Knepley   patch->partition_of_unity = flg;
3653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3664bbf5ea8SMatthew G. Knepley }
3674bbf5ea8SMatthew G. Knepley 
3684bbf5ea8SMatthew G. Knepley /* TODO: Docs */
369d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetPartitionOfUnity(PC pc, PetscBool *flg)
370d71ae5a4SJacob Faibussowitsch {
3714bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
3724d86920dSPierre Jolivet 
3734bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3744bbf5ea8SMatthew G. Knepley   *flg = patch->partition_of_unity;
3753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3764bbf5ea8SMatthew G. Knepley }
3774bbf5ea8SMatthew G. Knepley 
3784bbf5ea8SMatthew G. Knepley /* TODO: Docs */
379ba38deedSJacob Faibussowitsch static PetscErrorCode PCPatchSetLocalComposition(PC pc, PCCompositeType type)
380d71ae5a4SJacob Faibussowitsch {
381c2e6f3c0SFlorian Wechsung   PC_PATCH *patch = (PC_PATCH *)pc->data;
3824d86920dSPierre Jolivet 
383c2e6f3c0SFlorian Wechsung   PetscFunctionBegin;
3842472a847SBarry 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");
38561c4b389SFlorian Wechsung   patch->local_composition_type = type;
3863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
387c2e6f3c0SFlorian Wechsung }
388c2e6f3c0SFlorian Wechsung 
389c2e6f3c0SFlorian Wechsung /* TODO: Docs */
390d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetSubMatType(PC pc, MatType sub_mat_type)
391d71ae5a4SJacob Faibussowitsch {
3924bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
3934bbf5ea8SMatthew G. Knepley 
3944bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3959566063dSJacob Faibussowitsch   if (patch->sub_mat_type) PetscCall(PetscFree(patch->sub_mat_type));
3969566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(sub_mat_type, (char **)&patch->sub_mat_type));
3973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3984bbf5ea8SMatthew G. Knepley }
3994bbf5ea8SMatthew G. Knepley 
4004bbf5ea8SMatthew G. Knepley /* TODO: Docs */
401d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetSubMatType(PC pc, MatType *sub_mat_type)
402d71ae5a4SJacob Faibussowitsch {
4034bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
4044d86920dSPierre Jolivet 
4054bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4064bbf5ea8SMatthew G. Knepley   *sub_mat_type = patch->sub_mat_type;
4073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4084bbf5ea8SMatthew G. Knepley }
4094bbf5ea8SMatthew G. Knepley 
4104bbf5ea8SMatthew G. Knepley /* TODO: Docs */
411d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetCellNumbering(PC pc, PetscSection cellNumbering)
412d71ae5a4SJacob Faibussowitsch {
4134bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
4144bbf5ea8SMatthew G. Knepley 
4154bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4164bbf5ea8SMatthew G. Knepley   patch->cellNumbering = cellNumbering;
4179566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cellNumbering));
4183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4194bbf5ea8SMatthew G. Knepley }
4204bbf5ea8SMatthew G. Knepley 
4214bbf5ea8SMatthew G. Knepley /* TODO: Docs */
422d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetCellNumbering(PC pc, PetscSection *cellNumbering)
423d71ae5a4SJacob Faibussowitsch {
4244bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
4254d86920dSPierre Jolivet 
4264bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4274bbf5ea8SMatthew G. Knepley   *cellNumbering = patch->cellNumbering;
4283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4294bbf5ea8SMatthew G. Knepley }
4304bbf5ea8SMatthew G. Knepley 
4314bbf5ea8SMatthew G. Knepley /* TODO: Docs */
432d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetConstructType(PC pc, PCPatchConstructType ctype, PetscErrorCode (*func)(PC, PetscInt *, IS **, IS *, void *), void *ctx)
433d71ae5a4SJacob Faibussowitsch {
4344bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
4354bbf5ea8SMatthew G. Knepley 
4364bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4374bbf5ea8SMatthew G. Knepley   patch->ctype = ctype;
4384bbf5ea8SMatthew G. Knepley   switch (ctype) {
4394bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
44040c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
4414bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Star;
4424bbf5ea8SMatthew G. Knepley     break;
4434bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
44440c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
4454bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Vanka;
4464bbf5ea8SMatthew G. Knepley     break;
447e5b9877fSPatrick Farrell   case PC_PATCH_PARDECOMP:
4480a390943SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
449e5b9877fSPatrick Farrell     patch->patchconstructop = PCPatchConstruct_Pardecomp;
4500a390943SPatrick Farrell     break;
4514bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
4524bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
4534bbf5ea8SMatthew G. Knepley     patch->user_patches     = PETSC_TRUE;
4544bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_User;
455bdd9e0cdSPatrick Farrell     if (func) {
4564bbf5ea8SMatthew G. Knepley       patch->userpatchconstructionop = func;
4574bbf5ea8SMatthew G. Knepley       patch->userpatchconstructctx   = ctx;
458bdd9e0cdSPatrick Farrell     }
4594bbf5ea8SMatthew G. Knepley     break;
460d71ae5a4SJacob Faibussowitsch   default:
461d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_USER, "Unknown patch construction type %" PetscInt_FMT, (PetscInt)patch->ctype);
4624bbf5ea8SMatthew G. Knepley   }
4633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4644bbf5ea8SMatthew G. Knepley }
4654bbf5ea8SMatthew G. Knepley 
4664bbf5ea8SMatthew G. Knepley /* TODO: Docs */
467d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetConstructType(PC pc, PCPatchConstructType *ctype, PetscErrorCode (**func)(PC, PetscInt *, IS **, IS *, void *), void **ctx)
468d71ae5a4SJacob Faibussowitsch {
4694bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
4704bbf5ea8SMatthew G. Knepley 
4714bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4724bbf5ea8SMatthew G. Knepley   *ctype = patch->ctype;
4734bbf5ea8SMatthew G. Knepley   switch (patch->ctype) {
4744bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
4754bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
476d71ae5a4SJacob Faibussowitsch   case PC_PATCH_PARDECOMP:
477d71ae5a4SJacob Faibussowitsch     break;
4784bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
4794bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
4804bbf5ea8SMatthew G. Knepley     *func = patch->userpatchconstructionop;
4814bbf5ea8SMatthew G. Knepley     *ctx  = patch->userpatchconstructctx;
4824bbf5ea8SMatthew G. Knepley     break;
483d71ae5a4SJacob Faibussowitsch   default:
484d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_USER, "Unknown patch construction type %" PetscInt_FMT, (PetscInt)patch->ctype);
4854bbf5ea8SMatthew G. Knepley   }
4863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4874bbf5ea8SMatthew G. Knepley }
4884bbf5ea8SMatthew G. Knepley 
4894bbf5ea8SMatthew G. Knepley /* TODO: Docs */
490d71ae5a4SJacob 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)
491d71ae5a4SJacob Faibussowitsch {
4924bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
493b6bb21d1SLawrence Mitchell   DM        dm, plex;
4944bbf5ea8SMatthew G. Knepley   PetscSF  *sfs;
4955f824522SMatthew G. Knepley   PetscInt  cStart, cEnd, i, j;
4964bbf5ea8SMatthew G. Knepley 
4974bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4989566063dSJacob Faibussowitsch   PetscCall(PCGetDM(pc, &dm));
4999566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
500b6bb21d1SLawrence Mitchell   dm = plex;
5019566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
5029566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nsubspaces, &sfs));
5039566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nsubspaces, &patch->dofSection));
5049566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nsubspaces, &patch->bs));
5059566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nsubspaces, &patch->nodesPerCell));
5069566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nsubspaces, &patch->cellNodeMap));
5079566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nsubspaces + 1, &patch->subspaceOffsets));
5084bbf5ea8SMatthew G. Knepley 
5094bbf5ea8SMatthew G. Knepley   patch->nsubspaces       = nsubspaces;
5104bbf5ea8SMatthew G. Knepley   patch->totalDofsPerCell = 0;
5114bbf5ea8SMatthew G. Knepley   for (i = 0; i < nsubspaces; ++i) {
5129566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dms[i], &patch->dofSection[i]));
5139566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)patch->dofSection[i]));
5149566063dSJacob Faibussowitsch     PetscCall(DMGetSectionSF(dms[i], &sfs[i]));
5154bbf5ea8SMatthew G. Knepley     patch->bs[i]           = bs[i];
5164bbf5ea8SMatthew G. Knepley     patch->nodesPerCell[i] = nodesPerCell[i];
5174bbf5ea8SMatthew G. Knepley     patch->totalDofsPerCell += nodesPerCell[i] * bs[i];
5189566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1((cEnd - cStart) * nodesPerCell[i], &patch->cellNodeMap[i]));
51980e8a965SFlorian Wechsung     for (j = 0; j < (cEnd - cStart) * nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
5204bbf5ea8SMatthew G. Knepley     patch->subspaceOffsets[i] = subspaceOffsets[i];
5214bbf5ea8SMatthew G. Knepley   }
5229566063dSJacob Faibussowitsch   PetscCall(PCPatchCreateDefaultSF_Private(pc, nsubspaces, sfs, patch->bs));
5239566063dSJacob Faibussowitsch   PetscCall(PetscFree(sfs));
5244bbf5ea8SMatthew G. Knepley 
5254bbf5ea8SMatthew G. Knepley   patch->subspaceOffsets[nsubspaces] = subspaceOffsets[nsubspaces];
5269566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes));
5279566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes));
5289566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
5293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5304bbf5ea8SMatthew G. Knepley }
5314bbf5ea8SMatthew G. Knepley 
5324bbf5ea8SMatthew G. Knepley /* TODO: Docs */
533ba38deedSJacob Faibussowitsch static PetscErrorCode PCPatchSetDiscretisationInfoCombined(PC pc, DM dm, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
534d71ae5a4SJacob Faibussowitsch {
5355f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
5365f824522SMatthew G. Knepley   PetscInt  cStart, cEnd, i, j;
5375f824522SMatthew G. Knepley 
5385f824522SMatthew G. Knepley   PetscFunctionBegin;
5395f824522SMatthew G. Knepley   patch->combined = PETSC_TRUE;
5409566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
5419566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &patch->nsubspaces));
5429566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(patch->nsubspaces, &patch->dofSection));
5439566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(patch->nsubspaces, &patch->bs));
5449566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(patch->nsubspaces, &patch->nodesPerCell));
5459566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(patch->nsubspaces, &patch->cellNodeMap));
5469566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(patch->nsubspaces + 1, &patch->subspaceOffsets));
5479566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &patch->dofSection[0]));
5489566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)patch->dofSection[0]));
5499566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(patch->dofSection[0], &patch->subspaceOffsets[patch->nsubspaces]));
5505f824522SMatthew G. Knepley   patch->totalDofsPerCell = 0;
5515f824522SMatthew G. Knepley   for (i = 0; i < patch->nsubspaces; ++i) {
5525f824522SMatthew G. Knepley     patch->bs[i]           = 1;
5535f824522SMatthew G. Knepley     patch->nodesPerCell[i] = nodesPerCell[i];
5545f824522SMatthew G. Knepley     patch->totalDofsPerCell += nodesPerCell[i];
5559566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1((cEnd - cStart) * nodesPerCell[i], &patch->cellNodeMap[i]));
5565f824522SMatthew G. Knepley     for (j = 0; j < (cEnd - cStart) * nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
5575f824522SMatthew G. Knepley   }
5589566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &patch->sectionSF));
5599566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)patch->sectionSF));
5609566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes));
5619566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes));
5623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5635f824522SMatthew G. Knepley }
5645f824522SMatthew G. Knepley 
5655f824522SMatthew G. Knepley /*@C
56604c3f3b8SBarry Smith   PCPatchSetComputeFunction - Set the callback function used to compute patch residuals
56792d50984SMatthew G. Knepley 
56820f4b53cSBarry Smith   Logically Collective
56999b7e5c6SPatrick Farrell 
57092d50984SMatthew G. Knepley   Input Parameters:
57120f4b53cSBarry Smith + pc   - The `PC`
57204c3f3b8SBarry Smith . func - The callback function
57392d50984SMatthew G. Knepley - ctx  - The user context
57492d50984SMatthew G. Knepley 
57520f4b53cSBarry Smith   Calling sequence of `func`:
57620f4b53cSBarry Smith + pc               - The `PC`
5777a50e09dSPatrick Farrell . point            - The point
5787a50e09dSPatrick Farrell . x                - The input solution (not used in linear problems)
5797a50e09dSPatrick Farrell . f                - The patch residual vector
5807a50e09dSPatrick Farrell . cellIS           - An array of the cell numbers
58120f4b53cSBarry Smith . n                - The size of `dofsArray`
5827a50e09dSPatrick Farrell . dofsArray        - The dofmap for the dofs to be solved for
5837a50e09dSPatrick Farrell . dofsArrayWithAll - The dofmap for all dofs on the patch
5847a50e09dSPatrick Farrell - ctx              - The user context
5857a50e09dSPatrick Farrell 
58692d50984SMatthew G. Knepley   Level: advanced
58792d50984SMatthew G. Knepley 
588f1580f4eSBarry Smith   Note:
58904c3f3b8SBarry Smith   The entries of `f` (the output residual vector) have been set to zero before the call.
59092d50984SMatthew G. Knepley 
591562efe2eSBarry Smith .seealso: [](ch_ksp), `PCPatchSetComputeOperator()`, `PCPatchGetComputeOperator()`, `PCPatchSetDiscretisationInfo()`, `PCPatchSetComputeFunctionInteriorFacets()`
59292d50984SMatthew G. Knepley @*/
59304c3f3b8SBarry Smith PetscErrorCode PCPatchSetComputeFunction(PC pc, PetscErrorCode (*func)(PC pc, PetscInt point, Vec x, Vec f, IS cellIS, PetscInt n, const PetscInt *dofsArray, const PetscInt *dofsArrayWithAll, void *ctx), void *ctx)
594d71ae5a4SJacob Faibussowitsch {
59592d50984SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
59692d50984SMatthew G. Knepley 
59792d50984SMatthew G. Knepley   PetscFunctionBegin;
59892d50984SMatthew G. Knepley   patch->usercomputef    = func;
59992d50984SMatthew G. Knepley   patch->usercomputefctx = ctx;
6003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
60192d50984SMatthew G. Knepley }
60292d50984SMatthew G. Knepley 
60392d50984SMatthew G. Knepley /*@C
60404c3f3b8SBarry Smith   PCPatchSetComputeFunctionInteriorFacets - Set the callback function used to compute facet integrals for patch residuals
60559109abcSLawrence Mitchell 
60620f4b53cSBarry Smith   Logically Collective
6077a50e09dSPatrick Farrell 
60859109abcSLawrence Mitchell   Input Parameters:
60920f4b53cSBarry Smith + pc   - The `PC`
61004c3f3b8SBarry Smith . func - The callback function
61159109abcSLawrence Mitchell - ctx  - The user context
61259109abcSLawrence Mitchell 
61320f4b53cSBarry Smith   Calling sequence of `func`:
61420f4b53cSBarry Smith + pc               - The `PC`
6157a50e09dSPatrick Farrell . point            - The point
6167a50e09dSPatrick Farrell . x                - The input solution (not used in linear problems)
6177a50e09dSPatrick Farrell . f                - The patch residual vector
6187a50e09dSPatrick Farrell . facetIS          - An array of the facet numbers
61920f4b53cSBarry Smith . n                - The size of `dofsArray`
6207a50e09dSPatrick Farrell . dofsArray        - The dofmap for the dofs to be solved for
6217a50e09dSPatrick Farrell . dofsArrayWithAll - The dofmap for all dofs on the patch
6227a50e09dSPatrick Farrell - ctx              - The user context
6237a50e09dSPatrick Farrell 
62459109abcSLawrence Mitchell   Level: advanced
62559109abcSLawrence Mitchell 
626f1580f4eSBarry Smith   Note:
62704c3f3b8SBarry Smith   The entries of `f` (the output residual vector) have been set to zero before the call.
62859109abcSLawrence Mitchell 
629562efe2eSBarry Smith .seealso: [](ch_ksp), `PCPatchSetComputeOperator()`, `PCPatchGetComputeOperator()`, `PCPatchSetDiscretisationInfo()`, `PCPatchSetComputeFunction()`
63059109abcSLawrence Mitchell @*/
63104c3f3b8SBarry Smith PetscErrorCode PCPatchSetComputeFunctionInteriorFacets(PC pc, PetscErrorCode (*func)(PC pc, PetscInt point, Vec x, Vec f, IS facetIS, PetscInt n, const PetscInt *dofsArray, const PetscInt *dofsArrayWithAll, void *ctx), void *ctx)
632d71ae5a4SJacob Faibussowitsch {
63359109abcSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *)pc->data;
63459109abcSLawrence Mitchell 
63559109abcSLawrence Mitchell   PetscFunctionBegin;
63659109abcSLawrence Mitchell   patch->usercomputefintfacet    = func;
63759109abcSLawrence Mitchell   patch->usercomputefintfacetctx = ctx;
6383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
63959109abcSLawrence Mitchell }
64059109abcSLawrence Mitchell 
64159109abcSLawrence Mitchell /*@C
64204c3f3b8SBarry Smith   PCPatchSetComputeOperator - Set the callback function used to compute patch matrices
6435f824522SMatthew G. Knepley 
64420f4b53cSBarry Smith   Logically Collective
6457a50e09dSPatrick Farrell 
6465f824522SMatthew G. Knepley   Input Parameters:
64720f4b53cSBarry Smith + pc   - The `PC`
64804c3f3b8SBarry Smith . func - The callback function
6495f824522SMatthew G. Knepley - ctx  - The user context
6505f824522SMatthew G. Knepley 
65120f4b53cSBarry Smith   Calling sequence of `func`:
65220f4b53cSBarry Smith + pc               - The `PC`
6537a50e09dSPatrick Farrell . point            - The point
6547a50e09dSPatrick Farrell . x                - The input solution (not used in linear problems)
6557a50e09dSPatrick Farrell . mat              - The patch matrix
65604c3f3b8SBarry Smith . facetIS          - An array of the cell numbers
65720f4b53cSBarry Smith . n                - The size of `dofsArray`
6587a50e09dSPatrick Farrell . dofsArray        - The dofmap for the dofs to be solved for
6597a50e09dSPatrick Farrell . dofsArrayWithAll - The dofmap for all dofs on the patch
6607a50e09dSPatrick Farrell - ctx              - The user context
6617a50e09dSPatrick Farrell 
6625f824522SMatthew G. Knepley   Level: advanced
6635f824522SMatthew G. Knepley 
664f1580f4eSBarry Smith   Note:
6657a50e09dSPatrick Farrell   The matrix entries have been set to zero before the call.
6665f824522SMatthew G. Knepley 
667562efe2eSBarry Smith .seealso: [](ch_ksp), `PCPatchGetComputeOperator()`, `PCPatchSetComputeFunction()`, `PCPatchSetDiscretisationInfo()`
6685f824522SMatthew G. Knepley @*/
66904c3f3b8SBarry Smith PetscErrorCode PCPatchSetComputeOperator(PC pc, PetscErrorCode (*func)(PC pc, PetscInt point, Vec x, Mat mat, IS facetIS, PetscInt n, const PetscInt *dofsArray, const PetscInt *dofsArrayWithAll, void *ctx), void *ctx)
670d71ae5a4SJacob Faibussowitsch {
6714bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
6724bbf5ea8SMatthew G. Knepley 
6734bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
6744bbf5ea8SMatthew G. Knepley   patch->usercomputeop    = func;
675723f9013SMatthew G. Knepley   patch->usercomputeopctx = ctx;
6763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6774bbf5ea8SMatthew G. Knepley }
6784bbf5ea8SMatthew G. Knepley 
67959109abcSLawrence Mitchell /*@C
68004c3f3b8SBarry Smith   PCPatchSetComputeOperatorInteriorFacets - Set the callback function used to compute facet integrals for patch matrices
68159109abcSLawrence Mitchell 
68220f4b53cSBarry Smith   Logically Collective
68399b7e5c6SPatrick Farrell 
68459109abcSLawrence Mitchell   Input Parameters:
68520f4b53cSBarry Smith + pc   - The `PC`
68604c3f3b8SBarry Smith . func - The callback function
68759109abcSLawrence Mitchell - ctx  - The user context
68859109abcSLawrence Mitchell 
68920f4b53cSBarry Smith   Calling sequence of `func`:
69020f4b53cSBarry Smith + pc               - The `PC`
6917a50e09dSPatrick Farrell . point            - The point
6927a50e09dSPatrick Farrell . x                - The input solution (not used in linear problems)
6937a50e09dSPatrick Farrell . mat              - The patch matrix
6947a50e09dSPatrick Farrell . facetIS          - An array of the facet numbers
69520f4b53cSBarry Smith . n                - The size of `dofsArray`
6967a50e09dSPatrick Farrell . dofsArray        - The dofmap for the dofs to be solved for
6977a50e09dSPatrick Farrell . dofsArrayWithAll - The dofmap for all dofs on the patch
6987a50e09dSPatrick Farrell - ctx              - The user context
6997a50e09dSPatrick Farrell 
70059109abcSLawrence Mitchell   Level: advanced
70159109abcSLawrence Mitchell 
702f1580f4eSBarry Smith   Note:
7037a50e09dSPatrick Farrell   The matrix entries have been set to zero before the call.
70459109abcSLawrence Mitchell 
705562efe2eSBarry Smith .seealso: [](ch_ksp), `PCPatchGetComputeOperator()`, `PCPatchSetComputeFunction()`, `PCPatchSetDiscretisationInfo()`
70659109abcSLawrence Mitchell @*/
70704c3f3b8SBarry Smith PetscErrorCode PCPatchSetComputeOperatorInteriorFacets(PC pc, PetscErrorCode (*func)(PC pc, PetscInt point, Vec x, Mat mat, IS facetIS, PetscInt n, const PetscInt *dofsArray, const PetscInt *dofsArrayWithAll, void *ctx), void *ctx)
708d71ae5a4SJacob Faibussowitsch {
70959109abcSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *)pc->data;
71059109abcSLawrence Mitchell 
71159109abcSLawrence Mitchell   PetscFunctionBegin;
71259109abcSLawrence Mitchell   patch->usercomputeopintfacet    = func;
71359109abcSLawrence Mitchell   patch->usercomputeopintfacetctx = ctx;
7143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
71559109abcSLawrence Mitchell }
71659109abcSLawrence Mitchell 
7174bbf5ea8SMatthew G. Knepley /* On entry, ht contains the topological entities whose dofs we are responsible for solving for;
7184bbf5ea8SMatthew G. Knepley    on exit, cht contains all the topological entities we need to compute their residuals.
7194bbf5ea8SMatthew G. Knepley    In full generality this should incorporate knowledge of the sparsity pattern of the matrix;
7204bbf5ea8SMatthew G. Knepley    here we assume a standard FE sparsity pattern.*/
7214bbf5ea8SMatthew G. Knepley /* TODO: Use DMPlexGetAdjacency() */
722d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCompleteCellPatch(PC pc, PetscHSetI ht, PetscHSetI cht)
723d71ae5a4SJacob Faibussowitsch {
724b6bb21d1SLawrence Mitchell   DM            dm, plex;
725bc7fa33aSFlorian Wechsung   PC_PATCH     *patch = (PC_PATCH *)pc->data;
7261b68eb51SMatthew G. Knepley   PetscHashIter hi;
7274bbf5ea8SMatthew G. Knepley   PetscInt      point;
7284bbf5ea8SMatthew G. Knepley   PetscInt     *star = NULL, *closure = NULL;
7294c954380SMatthew G. Knepley   PetscInt      ignoredim, iStart = 0, iEnd = -1, starSize, closureSize, si, ci;
730bc7fa33aSFlorian Wechsung   PetscInt     *fStar = NULL, *fClosure = NULL;
731bc7fa33aSFlorian Wechsung   PetscInt      fBegin, fEnd, fsi, fci, fStarSize, fClosureSize;
7324bbf5ea8SMatthew G. Knepley 
7334bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
7349566063dSJacob Faibussowitsch   PetscCall(PCGetDM(pc, &dm));
7359566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
736b6bb21d1SLawrence Mitchell   dm = plex;
7379566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fBegin, &fEnd));
7389566063dSJacob Faibussowitsch   PetscCall(PCPatchGetIgnoreDim(pc, &ignoredim));
7399566063dSJacob Faibussowitsch   if (ignoredim >= 0) PetscCall(DMPlexGetDepthStratum(dm, ignoredim, &iStart, &iEnd));
7409566063dSJacob Faibussowitsch   PetscCall(PetscHSetIClear(cht));
7411b68eb51SMatthew G. Knepley   PetscHashIterBegin(ht, hi);
7421b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(ht, hi)) {
7431b68eb51SMatthew G. Knepley     PetscHashIterGetKey(ht, hi, point);
7441b68eb51SMatthew G. Knepley     PetscHashIterNext(ht, hi);
7454bbf5ea8SMatthew G. Knepley 
7464bbf5ea8SMatthew G. Knepley     /* Loop over all the cells that this point connects to */
7479566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star));
7485f824522SMatthew G. Knepley     for (si = 0; si < starSize * 2; si += 2) {
7494c954380SMatthew G. Knepley       const PetscInt ownedpoint = star[si];
7505f824522SMatthew G. Knepley       /* TODO Check for point in cht before running through closure again */
7514bbf5ea8SMatthew G. Knepley       /* now loop over all entities in the closure of that cell */
7529566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, ownedpoint, PETSC_TRUE, &closureSize, &closure));
7535f824522SMatthew G. Knepley       for (ci = 0; ci < closureSize * 2; ci += 2) {
7544c954380SMatthew G. Knepley         const PetscInt seenpoint = closure[ci];
7555f824522SMatthew G. Knepley         if (ignoredim >= 0 && seenpoint >= iStart && seenpoint < iEnd) continue;
7569566063dSJacob Faibussowitsch         PetscCall(PetscHSetIAdd(cht, seenpoint));
757bc7fa33aSFlorian Wechsung         /* Facet integrals couple dofs across facets, so in that case for each of
758f1580f4eSBarry Smith           the facets we need to add all dofs on the other side of the facet to
759f1580f4eSBarry Smith           the seen dofs. */
760bc7fa33aSFlorian Wechsung         if (patch->usercomputeopintfacet) {
761bc7fa33aSFlorian Wechsung           if (fBegin <= seenpoint && seenpoint < fEnd) {
7629566063dSJacob Faibussowitsch             PetscCall(DMPlexGetTransitiveClosure(dm, seenpoint, PETSC_FALSE, &fStarSize, &fStar));
763bc7fa33aSFlorian Wechsung             for (fsi = 0; fsi < fStarSize * 2; fsi += 2) {
7649566063dSJacob Faibussowitsch               PetscCall(DMPlexGetTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, &fClosureSize, &fClosure));
76548a46eb9SPierre Jolivet               for (fci = 0; fci < fClosureSize * 2; fci += 2) PetscCall(PetscHSetIAdd(cht, fClosure[fci]));
7669566063dSJacob Faibussowitsch               PetscCall(DMPlexRestoreTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, NULL, &fClosure));
767bc7fa33aSFlorian Wechsung             }
7689566063dSJacob Faibussowitsch             PetscCall(DMPlexRestoreTransitiveClosure(dm, seenpoint, PETSC_FALSE, NULL, &fStar));
769bc7fa33aSFlorian Wechsung           }
770bc7fa33aSFlorian Wechsung         }
7714bbf5ea8SMatthew G. Knepley       }
7729566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, ownedpoint, PETSC_TRUE, NULL, &closure));
7734bbf5ea8SMatthew G. Knepley     }
7749566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, NULL, &star));
7754bbf5ea8SMatthew G. Knepley   }
7769566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
7773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7785f824522SMatthew G. Knepley }
7795f824522SMatthew G. Knepley 
780d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchGetGlobalDofs(PC pc, PetscSection dofSection[], PetscInt f, PetscBool combined, PetscInt p, PetscInt *dof, PetscInt *off)
781d71ae5a4SJacob Faibussowitsch {
7825f824522SMatthew G. Knepley   PetscFunctionBegin;
7835f824522SMatthew G. Knepley   if (combined) {
7845f824522SMatthew G. Knepley     if (f < 0) {
7859566063dSJacob Faibussowitsch       if (dof) PetscCall(PetscSectionGetDof(dofSection[0], p, dof));
7869566063dSJacob Faibussowitsch       if (off) PetscCall(PetscSectionGetOffset(dofSection[0], p, off));
7875f824522SMatthew G. Knepley     } else {
7889566063dSJacob Faibussowitsch       if (dof) PetscCall(PetscSectionGetFieldDof(dofSection[0], p, f, dof));
7899566063dSJacob Faibussowitsch       if (off) PetscCall(PetscSectionGetFieldOffset(dofSection[0], p, f, off));
7905f824522SMatthew G. Knepley     }
7915f824522SMatthew G. Knepley   } else {
7925f824522SMatthew G. Knepley     if (f < 0) {
7935f824522SMatthew G. Knepley       PC_PATCH *patch = (PC_PATCH *)pc->data;
7945f824522SMatthew G. Knepley       PetscInt  fdof, g;
7955f824522SMatthew G. Knepley 
7965f824522SMatthew G. Knepley       if (dof) {
7975f824522SMatthew G. Knepley         *dof = 0;
7985f824522SMatthew G. Knepley         for (g = 0; g < patch->nsubspaces; ++g) {
7999566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetDof(dofSection[g], p, &fdof));
8005f824522SMatthew G. Knepley           *dof += fdof;
8015f824522SMatthew G. Knepley         }
8025f824522SMatthew G. Knepley       }
803624e31c3SLawrence Mitchell       if (off) {
804624e31c3SLawrence Mitchell         *off = 0;
805624e31c3SLawrence Mitchell         for (g = 0; g < patch->nsubspaces; ++g) {
8069566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetOffset(dofSection[g], p, &fdof));
807624e31c3SLawrence Mitchell           *off += fdof;
808624e31c3SLawrence Mitchell         }
809624e31c3SLawrence Mitchell       }
8105f824522SMatthew G. Knepley     } else {
8119566063dSJacob Faibussowitsch       if (dof) PetscCall(PetscSectionGetDof(dofSection[f], p, dof));
8129566063dSJacob Faibussowitsch       if (off) PetscCall(PetscSectionGetOffset(dofSection[f], p, off));
8135f824522SMatthew G. Knepley     }
8145f824522SMatthew G. Knepley   }
8153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8164bbf5ea8SMatthew G. Knepley }
8174bbf5ea8SMatthew G. Knepley 
8184bbf5ea8SMatthew G. Knepley /* Given a hash table with a set of topological entities (pts), compute the degrees of
8194bbf5ea8SMatthew G. Knepley    freedom in global concatenated numbering on those entities.
8204bbf5ea8SMatthew G. Knepley    For Vanka smoothing, this needs to do something special: ignore dofs of the
8214bbf5ea8SMatthew G. Knepley    constraint subspace on entities that aren't the base entity we're building the patch
8224bbf5ea8SMatthew G. Knepley    around. */
823d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchGetPointDofs(PC pc, PetscHSetI pts, PetscHSetI dofs, PetscInt base, PetscHSetI *subspaces_to_exclude)
824d71ae5a4SJacob Faibussowitsch {
8255f824522SMatthew G. Knepley   PC_PATCH     *patch = (PC_PATCH *)pc->data;
8261b68eb51SMatthew G. Knepley   PetscHashIter hi;
8274bbf5ea8SMatthew G. Knepley   PetscInt      ldof, loff;
8284bbf5ea8SMatthew G. Knepley   PetscInt      k, p;
8294bbf5ea8SMatthew G. Knepley 
8304bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
8319566063dSJacob Faibussowitsch   PetscCall(PetscHSetIClear(dofs));
8324bbf5ea8SMatthew G. Knepley   for (k = 0; k < patch->nsubspaces; ++k) {
8334bbf5ea8SMatthew G. Knepley     PetscInt subspaceOffset = patch->subspaceOffsets[k];
8344bbf5ea8SMatthew G. Knepley     PetscInt bs             = patch->bs[k];
8354bbf5ea8SMatthew G. Knepley     PetscInt j, l;
8364bbf5ea8SMatthew G. Knepley 
837e4c66b91SPatrick Farrell     if (subspaces_to_exclude != NULL) {
838e4c66b91SPatrick Farrell       PetscBool should_exclude_k = PETSC_FALSE;
8399566063dSJacob Faibussowitsch       PetscCall(PetscHSetIHas(*subspaces_to_exclude, k, &should_exclude_k));
840e4c66b91SPatrick Farrell       if (should_exclude_k) {
8414bbf5ea8SMatthew G. Knepley         /* only get this subspace dofs at the base entity, not any others */
8429566063dSJacob Faibussowitsch         PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, base, &ldof, &loff));
8434bbf5ea8SMatthew G. Knepley         if (0 == ldof) continue;
8444bbf5ea8SMatthew G. Knepley         for (j = loff; j < ldof + loff; ++j) {
8454bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
8464bbf5ea8SMatthew G. Knepley             PetscInt dof = bs * j + l + subspaceOffset;
8479566063dSJacob Faibussowitsch             PetscCall(PetscHSetIAdd(dofs, dof));
8484bbf5ea8SMatthew G. Knepley           }
8494bbf5ea8SMatthew G. Knepley         }
8504bbf5ea8SMatthew G. Knepley         continue; /* skip the other dofs of this subspace */
8514bbf5ea8SMatthew G. Knepley       }
852e4c66b91SPatrick Farrell     }
8534bbf5ea8SMatthew G. Knepley 
8541b68eb51SMatthew G. Knepley     PetscHashIterBegin(pts, hi);
8551b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(pts, hi)) {
8561b68eb51SMatthew G. Knepley       PetscHashIterGetKey(pts, hi, p);
8571b68eb51SMatthew G. Knepley       PetscHashIterNext(pts, hi);
8589566063dSJacob Faibussowitsch       PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, p, &ldof, &loff));
8594bbf5ea8SMatthew G. Knepley       if (0 == ldof) continue;
8604bbf5ea8SMatthew G. Knepley       for (j = loff; j < ldof + loff; ++j) {
8614bbf5ea8SMatthew G. Knepley         for (l = 0; l < bs; ++l) {
8624bbf5ea8SMatthew G. Knepley           PetscInt dof = bs * j + l + subspaceOffset;
8639566063dSJacob Faibussowitsch           PetscCall(PetscHSetIAdd(dofs, dof));
8644bbf5ea8SMatthew G. Knepley         }
8654bbf5ea8SMatthew G. Knepley       }
8664bbf5ea8SMatthew G. Knepley     }
8674bbf5ea8SMatthew G. Knepley   }
8683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8694bbf5ea8SMatthew G. Knepley }
8704bbf5ea8SMatthew G. Knepley 
8714bbf5ea8SMatthew G. Knepley /* Given two hash tables A and B, compute the keys in B that are not in A, and put them in C */
872d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchComputeSetDifference_Private(PetscHSetI A, PetscHSetI B, PetscHSetI C)
873d71ae5a4SJacob Faibussowitsch {
8741b68eb51SMatthew G. Knepley   PetscHashIter hi;
8751b68eb51SMatthew G. Knepley   PetscInt      key;
8764bbf5ea8SMatthew G. Knepley   PetscBool     flg;
8774bbf5ea8SMatthew G. Knepley 
8784bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
8799566063dSJacob Faibussowitsch   PetscCall(PetscHSetIClear(C));
8801b68eb51SMatthew G. Knepley   PetscHashIterBegin(B, hi);
8811b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(B, hi)) {
8821b68eb51SMatthew G. Knepley     PetscHashIterGetKey(B, hi, key);
8831b68eb51SMatthew G. Knepley     PetscHashIterNext(B, hi);
8849566063dSJacob Faibussowitsch     PetscCall(PetscHSetIHas(A, key, &flg));
8859566063dSJacob Faibussowitsch     if (!flg) PetscCall(PetscHSetIAdd(C, key));
8864bbf5ea8SMatthew G. Knepley   }
8873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8884bbf5ea8SMatthew G. Knepley }
8894bbf5ea8SMatthew G. Knepley 
89004c3f3b8SBarry Smith // PetscClangLinter pragma disable: -fdoc-sowing-chars
8914bbf5ea8SMatthew G. Knepley /*
892f1580f4eSBarry Smith   PCPatchCreateCellPatches - create patches.
893f1580f4eSBarry Smith 
894f1580f4eSBarry Smith   Input Parameter:
895f1580f4eSBarry Smith   . dm - The DMPlex object defining the mesh
896f1580f4eSBarry Smith 
897f1580f4eSBarry Smith   Output Parameters:
898f1580f4eSBarry Smith   + cellCounts  - Section with counts of cells around each vertex
899f1580f4eSBarry Smith   . cells       - IS of the cell point indices of cells in each patch
900f1580f4eSBarry Smith   . pointCounts - Section with counts of cells around each vertex
901f1580f4eSBarry Smith   - point       - IS of the cell point indices of cells in each patch
9024bbf5ea8SMatthew G. Knepley  */
903d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCreateCellPatches(PC pc)
904d71ae5a4SJacob Faibussowitsch {
9054bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *)pc->data;
9065f824522SMatthew G. Knepley   DMLabel         ghost = NULL;
9074bbf5ea8SMatthew G. Knepley   DM              dm, plex;
90876ce8f1aSJose E. Roman   PetscHSetI      ht = NULL, cht = NULL;
9090e126c0bSLawrence Mitchell   PetscSection    cellCounts, pointCounts, intFacetCounts, extFacetCounts;
910eb62eeaaSLawrence Mitchell   PetscInt       *cellsArray, *pointsArray, *intFacetsArray, *extFacetsArray, *intFacetsToPatchCell;
9110e126c0bSLawrence Mitchell   PetscInt        numCells, numPoints, numIntFacets, numExtFacets;
9125f824522SMatthew G. Knepley   const PetscInt *leaves;
91333cbca70SPatrick Farrell   PetscInt        nleaves, pStart, pEnd, cStart, cEnd, vStart, vEnd, fStart, fEnd, v;
9145f824522SMatthew G. Knepley   PetscBool       isFiredrake;
9154bbf5ea8SMatthew G. Knepley 
9164bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
9174bbf5ea8SMatthew G. Knepley   /* Used to keep track of the cells in the patch. */
9189566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&ht));
9199566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&cht));
9204bbf5ea8SMatthew G. Knepley 
9219566063dSJacob Faibussowitsch   PetscCall(PCGetDM(pc, &dm));
92228b400f6SJacob Faibussowitsch   PetscCheck(dm, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "DM not yet set on patch PC");
9239566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
924b6bb21d1SLawrence Mitchell   dm = plex;
9259566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
9269566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
9274bbf5ea8SMatthew G. Knepley 
9284bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
9299566063dSJacob Faibussowitsch     PetscCall(patch->userpatchconstructionop(pc, &patch->npatch, &patch->userIS, &patch->iterationSet, patch->userpatchconstructctx));
9309371c9d4SSatish Balay     vStart = 0;
9319371c9d4SSatish Balay     vEnd   = patch->npatch;
932e5b9877fSPatrick Farrell   } else if (patch->ctype == PC_PATCH_PARDECOMP) {
9339371c9d4SSatish Balay     vStart = 0;
9349371c9d4SSatish Balay     vEnd   = 1;
9355f824522SMatthew G. Knepley   } else if (patch->codim < 0) {
9369566063dSJacob Faibussowitsch     if (patch->dim < 0) PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
9379566063dSJacob Faibussowitsch     else PetscCall(DMPlexGetDepthStratum(dm, patch->dim, &vStart, &vEnd));
9389566063dSJacob Faibussowitsch   } else PetscCall(DMPlexGetHeightStratum(dm, patch->codim, &vStart, &vEnd));
9395f824522SMatthew G. Knepley   patch->npatch = vEnd - vStart;
9404bbf5ea8SMatthew G. Knepley 
9414bbf5ea8SMatthew G. Knepley   /* These labels mark the owned points.  We only create patches around points that this process owns. */
9429566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, "pyop2_ghost", &isFiredrake));
9435f824522SMatthew G. Knepley   if (isFiredrake) {
9449566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, "pyop2_ghost", &ghost));
9459566063dSJacob Faibussowitsch     PetscCall(DMLabelCreateIndex(ghost, pStart, pEnd));
9465f824522SMatthew G. Knepley   } else {
9475f824522SMatthew G. Knepley     PetscSF sf;
9485f824522SMatthew G. Knepley 
9499566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sf));
9509566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL));
9515f824522SMatthew G. Knepley     nleaves = PetscMax(nleaves, 0);
9525f824522SMatthew G. Knepley   }
9534bbf5ea8SMatthew G. Knepley 
9549566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->cellCounts));
9559566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->cellCounts, "Patch Cell Layout"));
9564bbf5ea8SMatthew G. Knepley   cellCounts = patch->cellCounts;
9579566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(cellCounts, vStart, vEnd));
9589566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->pointCounts));
9599566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->pointCounts, "Patch Point Layout"));
9605f824522SMatthew G. Knepley   pointCounts = patch->pointCounts;
9619566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(pointCounts, vStart, vEnd));
9629566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->extFacetCounts));
9639566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->extFacetCounts, "Patch Exterior Facet Layout"));
9640e126c0bSLawrence Mitchell   extFacetCounts = patch->extFacetCounts;
9659566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(extFacetCounts, vStart, vEnd));
9669566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->intFacetCounts));
9679566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->intFacetCounts, "Patch Interior Facet Layout"));
9680e126c0bSLawrence Mitchell   intFacetCounts = patch->intFacetCounts;
9699566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(intFacetCounts, vStart, vEnd));
9705f824522SMatthew G. Knepley   /* Count cells and points in the patch surrounding each entity */
9719566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
9724bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
9731b68eb51SMatthew G. Knepley     PetscHashIter hi;
9745f824522SMatthew G. Knepley     PetscInt      chtSize, loc = -1;
9755f824522SMatthew G. Knepley     PetscBool     flg;
9764bbf5ea8SMatthew G. Knepley 
977b525f888SPatrick Farrell     if (!patch->user_patches && patch->ctype != PC_PATCH_PARDECOMP) {
9789566063dSJacob Faibussowitsch       if (ghost) PetscCall(DMLabelHasPoint(ghost, v, &flg));
9799371c9d4SSatish Balay       else {
9809371c9d4SSatish Balay         PetscCall(PetscFindInt(v, nleaves, leaves, &loc));
9819371c9d4SSatish Balay         flg = loc >= 0 ? PETSC_TRUE : PETSC_FALSE;
9829371c9d4SSatish Balay       }
9834bbf5ea8SMatthew G. Knepley       /* Not an owned entity, don't make a cell patch. */
9844bbf5ea8SMatthew G. Knepley       if (flg) continue;
9854bbf5ea8SMatthew G. Knepley     }
9864bbf5ea8SMatthew G. Knepley 
9879566063dSJacob Faibussowitsch     PetscCall(patch->patchconstructop((void *)patch, dm, v, ht));
9889566063dSJacob Faibussowitsch     PetscCall(PCPatchCompleteCellPatch(pc, ht, cht));
9899566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(cht, &chtSize));
9904bbf5ea8SMatthew G. Knepley     /* empty patch, continue */
9914bbf5ea8SMatthew G. Knepley     if (chtSize == 0) continue;
9924bbf5ea8SMatthew G. Knepley 
9934bbf5ea8SMatthew G. Knepley     /* safe because size(cht) > 0 from above */
9941b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
9951b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
9965f824522SMatthew G. Knepley       PetscInt point, pdof;
9974bbf5ea8SMatthew G. Knepley 
9981b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
9990e126c0bSLawrence Mitchell       if (fStart <= point && point < fEnd) {
10000e126c0bSLawrence Mitchell         const PetscInt *support;
10010e126c0bSLawrence Mitchell         PetscInt        supportSize, p;
10020e126c0bSLawrence Mitchell         PetscBool       interior = PETSC_TRUE;
10039566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
10049566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
10050e126c0bSLawrence Mitchell         if (supportSize == 1) {
10060e126c0bSLawrence Mitchell           interior = PETSC_FALSE;
10070e126c0bSLawrence Mitchell         } else {
10080e126c0bSLawrence Mitchell           for (p = 0; p < supportSize; p++) {
10090e126c0bSLawrence Mitchell             PetscBool found;
10100e126c0bSLawrence Mitchell             /* FIXME: can I do this while iterating over cht? */
10119566063dSJacob Faibussowitsch             PetscCall(PetscHSetIHas(cht, support[p], &found));
10120e126c0bSLawrence Mitchell             if (!found) {
10130e126c0bSLawrence Mitchell               interior = PETSC_FALSE;
10140e126c0bSLawrence Mitchell               break;
10150e126c0bSLawrence Mitchell             }
10160e126c0bSLawrence Mitchell           }
10170e126c0bSLawrence Mitchell         }
10180e126c0bSLawrence Mitchell         if (interior) {
10199566063dSJacob Faibussowitsch           PetscCall(PetscSectionAddDof(intFacetCounts, v, 1));
10200e126c0bSLawrence Mitchell         } else {
10219566063dSJacob Faibussowitsch           PetscCall(PetscSectionAddDof(extFacetCounts, v, 1));
10220e126c0bSLawrence Mitchell         }
10230e126c0bSLawrence Mitchell       }
10249566063dSJacob Faibussowitsch       PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL));
10259566063dSJacob Faibussowitsch       if (pdof) PetscCall(PetscSectionAddDof(pointCounts, v, 1));
10269566063dSJacob Faibussowitsch       if (point >= cStart && point < cEnd) PetscCall(PetscSectionAddDof(cellCounts, v, 1));
10271b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
10284bbf5ea8SMatthew G. Knepley     }
10294bbf5ea8SMatthew G. Knepley   }
10309566063dSJacob Faibussowitsch   if (isFiredrake) PetscCall(DMLabelDestroyIndex(ghost));
10314bbf5ea8SMatthew G. Knepley 
10329566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(cellCounts));
10339566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(cellCounts, &numCells));
10349566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numCells, &cellsArray));
10359566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(pointCounts));
10369566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(pointCounts, &numPoints));
10379566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numPoints, &pointsArray));
10384bbf5ea8SMatthew G. Knepley 
10399566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(intFacetCounts));
10409566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(extFacetCounts));
10419566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(intFacetCounts, &numIntFacets));
10429566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(extFacetCounts, &numExtFacets));
10439566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numIntFacets, &intFacetsArray));
10449566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numIntFacets * 2, &intFacetsToPatchCell));
10459566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numExtFacets, &extFacetsArray));
10460e126c0bSLawrence Mitchell 
10474bbf5ea8SMatthew G. Knepley   /* Now that we know how much space we need, run through again and actually remember the cells. */
10484bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; v++) {
10491b68eb51SMatthew G. Knepley     PetscHashIter hi;
10500e126c0bSLawrence Mitchell     PetscInt      dof, off, cdof, coff, efdof, efoff, ifdof, ifoff, pdof, n = 0, cn = 0, ifn = 0, efn = 0;
10514bbf5ea8SMatthew G. Knepley 
10529566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(pointCounts, v, &dof));
10539566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(pointCounts, v, &off));
10549566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(cellCounts, v, &cdof));
10559566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(cellCounts, v, &coff));
10569566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(intFacetCounts, v, &ifdof));
10579566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(intFacetCounts, v, &ifoff));
10589566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(extFacetCounts, v, &efdof));
10599566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(extFacetCounts, v, &efoff));
10605f824522SMatthew G. Knepley     if (dof <= 0) continue;
10619566063dSJacob Faibussowitsch     PetscCall(patch->patchconstructop((void *)patch, dm, v, ht));
10629566063dSJacob Faibussowitsch     PetscCall(PCPatchCompleteCellPatch(pc, ht, cht));
10631b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
10641b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
10654bbf5ea8SMatthew G. Knepley       PetscInt point;
10664bbf5ea8SMatthew G. Knepley 
10671b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
10680e126c0bSLawrence Mitchell       if (fStart <= point && point < fEnd) {
10690e126c0bSLawrence Mitchell         const PetscInt *support;
10700e126c0bSLawrence Mitchell         PetscInt        supportSize, p;
10710e126c0bSLawrence Mitchell         PetscBool       interior = PETSC_TRUE;
10729566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
10739566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
10740e126c0bSLawrence Mitchell         if (supportSize == 1) {
10750e126c0bSLawrence Mitchell           interior = PETSC_FALSE;
10760e126c0bSLawrence Mitchell         } else {
10770e126c0bSLawrence Mitchell           for (p = 0; p < supportSize; p++) {
10780e126c0bSLawrence Mitchell             PetscBool found;
10790e126c0bSLawrence Mitchell             /* FIXME: can I do this while iterating over cht? */
10809566063dSJacob Faibussowitsch             PetscCall(PetscHSetIHas(cht, support[p], &found));
10810e126c0bSLawrence Mitchell             if (!found) {
10820e126c0bSLawrence Mitchell               interior = PETSC_FALSE;
10830e126c0bSLawrence Mitchell               break;
10840e126c0bSLawrence Mitchell             }
10850e126c0bSLawrence Mitchell           }
10860e126c0bSLawrence Mitchell         }
10870e126c0bSLawrence Mitchell         if (interior) {
108844b625f7SLawrence Mitchell           intFacetsToPatchCell[2 * (ifoff + ifn)]     = support[0];
108944b625f7SLawrence Mitchell           intFacetsToPatchCell[2 * (ifoff + ifn) + 1] = support[1];
10900e126c0bSLawrence Mitchell           intFacetsArray[ifoff + ifn++]               = point;
10910e126c0bSLawrence Mitchell         } else {
10920e126c0bSLawrence Mitchell           extFacetsArray[efoff + efn++] = point;
10930e126c0bSLawrence Mitchell         }
10940e126c0bSLawrence Mitchell       }
10959566063dSJacob Faibussowitsch       PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL));
1096ad540459SPierre Jolivet       if (pdof) pointsArray[off + n++] = point;
1097ad540459SPierre Jolivet       if (point >= cStart && point < cEnd) cellsArray[coff + cn++] = point;
10981b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
10994bbf5ea8SMatthew G. Knepley     }
110063a3b9bcSJacob 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);
110163a3b9bcSJacob 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);
110263a3b9bcSJacob 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);
110363a3b9bcSJacob 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);
1104eb62eeaaSLawrence Mitchell 
1105eb62eeaaSLawrence Mitchell     for (ifn = 0; ifn < ifdof; ifn++) {
110644b625f7SLawrence Mitchell       PetscInt  cell0  = intFacetsToPatchCell[2 * (ifoff + ifn)];
110744b625f7SLawrence Mitchell       PetscInt  cell1  = intFacetsToPatchCell[2 * (ifoff + ifn) + 1];
1108eb62eeaaSLawrence Mitchell       PetscBool found0 = PETSC_FALSE, found1 = PETSC_FALSE;
1109eb62eeaaSLawrence Mitchell       for (n = 0; n < cdof; n++) {
11107c54fef0SLawrence Mitchell         if (!found0 && cell0 == cellsArray[coff + n]) {
1111c3faab33SLawrence Mitchell           intFacetsToPatchCell[2 * (ifoff + ifn)] = n;
1112eb62eeaaSLawrence Mitchell           found0                                  = PETSC_TRUE;
1113eb62eeaaSLawrence Mitchell         }
11147c54fef0SLawrence Mitchell         if (!found1 && cell1 == cellsArray[coff + n]) {
1115c3faab33SLawrence Mitchell           intFacetsToPatchCell[2 * (ifoff + ifn) + 1] = n;
111680fc4459SLawrence Mitchell           found1                                      = PETSC_TRUE;
1117eb62eeaaSLawrence Mitchell         }
1118eb62eeaaSLawrence Mitchell         if (found0 && found1) break;
1119eb62eeaaSLawrence Mitchell       }
11207827d75bSBarry Smith       PetscCheck(found0 && found1, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Didn't manage to find local point numbers for facet support");
1121eb62eeaaSLawrence Mitchell     }
11224bbf5ea8SMatthew G. Knepley   }
11239566063dSJacob Faibussowitsch   PetscCall(PetscHSetIDestroy(&ht));
11249566063dSJacob Faibussowitsch   PetscCall(PetscHSetIDestroy(&cht));
11255f824522SMatthew G. Knepley 
11269566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numCells, cellsArray, PETSC_OWN_POINTER, &patch->cells));
11279566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->cells, "Patch Cells"));
11285f824522SMatthew G. Knepley   if (patch->viewCells) {
11299566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->cellCounts, patch->viewerCells, patch->formatCells));
11309566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->cells, patch->viewerCells, patch->formatCells));
11315f824522SMatthew G. Knepley   }
11329566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray, PETSC_OWN_POINTER, &patch->intFacets));
11339566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->intFacets, "Patch Interior Facets"));
11349566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 2 * numIntFacets, intFacetsToPatchCell, PETSC_OWN_POINTER, &patch->intFacetsToPatchCell));
11359566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->intFacetsToPatchCell, "Patch Interior Facets local support"));
11360e126c0bSLawrence Mitchell   if (patch->viewIntFacets) {
11379566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->intFacetCounts, patch->viewerIntFacets, patch->formatIntFacets));
11389566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->intFacets, patch->viewerIntFacets, patch->formatIntFacets));
11399566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->intFacetsToPatchCell, patch->viewerIntFacets, patch->formatIntFacets));
11400e126c0bSLawrence Mitchell   }
11419566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numExtFacets, extFacetsArray, PETSC_OWN_POINTER, &patch->extFacets));
11429566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->extFacets, "Patch Exterior Facets"));
11430e126c0bSLawrence Mitchell   if (patch->viewExtFacets) {
11449566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->extFacetCounts, patch->viewerExtFacets, patch->formatExtFacets));
11459566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->extFacets, patch->viewerExtFacets, patch->formatExtFacets));
11460e126c0bSLawrence Mitchell   }
11479566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints, pointsArray, PETSC_OWN_POINTER, &patch->points));
11489566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->points, "Patch Points"));
11495f824522SMatthew G. Knepley   if (patch->viewPoints) {
11509566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->pointCounts, patch->viewerPoints, patch->formatPoints));
11519566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->points, patch->viewerPoints, patch->formatPoints));
11525f824522SMatthew G. Knepley   }
11539566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
11543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11554bbf5ea8SMatthew G. Knepley }
11564bbf5ea8SMatthew G. Knepley 
11574bbf5ea8SMatthew G. Knepley /*
1158f1580f4eSBarry Smith   PCPatchCreateCellPatchDiscretisationInfo - Build the dof maps for cell patches
1159f1580f4eSBarry Smith 
1160f1580f4eSBarry Smith   Input Parameters:
1161f1580f4eSBarry Smith   + dm - The DMPlex object defining the mesh
1162f1580f4eSBarry Smith   . cellCounts - Section with counts of cells around each vertex
1163f1580f4eSBarry Smith   . cells - IS of the cell point indices of cells in each patch
1164f1580f4eSBarry Smith   . cellNumbering - Section mapping plex cell points to Firedrake cell indices.
1165f1580f4eSBarry Smith   . nodesPerCell - number of nodes per cell.
1166f1580f4eSBarry Smith   - cellNodeMap - map from cells to node indices (nodesPerCell * numCells)
1167f1580f4eSBarry Smith 
1168f1580f4eSBarry Smith   Output Parameters:
1169f1580f4eSBarry Smith   + dofs - IS of local dof numbers of each cell in the patch, where local is a patch local numbering
1170f1580f4eSBarry Smith   . gtolCounts - Section with counts of dofs per cell patch
1171f1580f4eSBarry Smith   - gtol - IS mapping from global dofs to local dofs for each patch.
11724bbf5ea8SMatthew G. Knepley  */
1173d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCreateCellPatchDiscretisationInfo(PC pc)
1174d71ae5a4SJacob Faibussowitsch {
11754bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch       = (PC_PATCH *)pc->data;
11764bbf5ea8SMatthew G. Knepley   PetscSection    cellCounts  = patch->cellCounts;
11775f824522SMatthew G. Knepley   PetscSection    pointCounts = patch->pointCounts;
11780904074fSPatrick Farrell   PetscSection    gtolCounts, gtolCountsWithArtificial = NULL, gtolCountsWithAll = NULL;
11794bbf5ea8SMatthew G. Knepley   IS              cells         = patch->cells;
11805f824522SMatthew G. Knepley   IS              points        = patch->points;
11814bbf5ea8SMatthew G. Knepley   PetscSection    cellNumbering = patch->cellNumbering;
11825f824522SMatthew G. Knepley   PetscInt        Nf            = patch->nsubspaces;
11835f824522SMatthew G. Knepley   PetscInt        numCells, numPoints;
11844bbf5ea8SMatthew G. Knepley   PetscInt        numDofs;
11850904074fSPatrick Farrell   PetscInt        numGlobalDofs, numGlobalDofsWithArtificial, numGlobalDofsWithAll;
11864bbf5ea8SMatthew G. Knepley   PetscInt        totalDofsPerCell = patch->totalDofsPerCell;
11874bbf5ea8SMatthew G. Knepley   PetscInt        vStart, vEnd, v;
11885f824522SMatthew G. Knepley   const PetscInt *cellsArray, *pointsArray;
11894bbf5ea8SMatthew G. Knepley   PetscInt       *newCellsArray                 = NULL;
11904bbf5ea8SMatthew G. Knepley   PetscInt       *dofsArray                     = NULL;
1191c2e6f3c0SFlorian Wechsung   PetscInt       *dofsArrayWithArtificial       = NULL;
11920904074fSPatrick Farrell   PetscInt       *dofsArrayWithAll              = NULL;
11935f824522SMatthew G. Knepley   PetscInt       *offsArray                     = NULL;
1194c2e6f3c0SFlorian Wechsung   PetscInt       *offsArrayWithArtificial       = NULL;
11950904074fSPatrick Farrell   PetscInt       *offsArrayWithAll              = NULL;
11964bbf5ea8SMatthew G. Knepley   PetscInt       *asmArray                      = NULL;
1197c2e6f3c0SFlorian Wechsung   PetscInt       *asmArrayWithArtificial        = NULL;
11980904074fSPatrick Farrell   PetscInt       *asmArrayWithAll               = NULL;
11994bbf5ea8SMatthew G. Knepley   PetscInt       *globalDofsArray               = NULL;
1200c2e6f3c0SFlorian Wechsung   PetscInt       *globalDofsArrayWithArtificial = NULL;
12010904074fSPatrick Farrell   PetscInt       *globalDofsArrayWithAll        = NULL;
12024bbf5ea8SMatthew G. Knepley   PetscInt        globalIndex                   = 0;
12034bbf5ea8SMatthew G. Knepley   PetscInt        key                           = 0;
12044bbf5ea8SMatthew G. Knepley   PetscInt        asmKey                        = 0;
1205b6bb21d1SLawrence Mitchell   DM              dm                            = NULL, plex;
1206557beb66SLawrence Mitchell   const PetscInt *bcNodes                       = NULL;
12071b68eb51SMatthew G. Knepley   PetscHMapI      ht;
1208c2e6f3c0SFlorian Wechsung   PetscHMapI      htWithArtificial;
12090904074fSPatrick Farrell   PetscHMapI      htWithAll;
12101b68eb51SMatthew G. Knepley   PetscHSetI      globalBcs;
1211557beb66SLawrence Mitchell   PetscInt        numBcs;
12121b68eb51SMatthew G. Knepley   PetscHSetI      ownedpts, seenpts, owneddofs, seendofs, artificialbcs;
1213cda239d9SMatthew G. Knepley   PetscInt        pStart, pEnd, p, i;
121410534d48SPatrick Farrell   char            option[PETSC_MAX_PATH_LEN];
121539fd2e8aSPatrick Farrell   PetscBool       isNonlinear;
12164bbf5ea8SMatthew G. Knepley 
12174bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
12189566063dSJacob Faibussowitsch   PetscCall(PCGetDM(pc, &dm));
12199566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
1220b6bb21d1SLawrence Mitchell   dm = plex;
12214bbf5ea8SMatthew G. Knepley   /* dofcounts section is cellcounts section * dofPerCell */
12229566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(cellCounts, &numCells));
12239566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(patch->pointCounts, &numPoints));
12244bbf5ea8SMatthew G. Knepley   numDofs = numCells * totalDofsPerCell;
12259566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numDofs, &dofsArray));
12269566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numPoints * Nf, &offsArray));
12279566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numDofs, &asmArray));
12289566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numCells, &newCellsArray));
12299566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(cellCounts, &vStart, &vEnd));
12309566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCounts));
12314bbf5ea8SMatthew G. Knepley   gtolCounts = patch->gtolCounts;
12329566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(gtolCounts, vStart, vEnd));
12339566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->gtolCounts, "Patch Global Index Section"));
12344bbf5ea8SMatthew G. Knepley 
1235b6bb21d1SLawrence Mitchell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
12369566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numPoints * Nf, &offsArrayWithArtificial));
12379566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numDofs, &asmArrayWithArtificial));
12389566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numDofs, &dofsArrayWithArtificial));
12399566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithArtificial));
1240c2e6f3c0SFlorian Wechsung     gtolCountsWithArtificial = patch->gtolCountsWithArtificial;
12419566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(gtolCountsWithArtificial, vStart, vEnd));
12429566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)patch->gtolCountsWithArtificial, "Patch Global Index Section Including Artificial BCs"));
1243c2e6f3c0SFlorian Wechsung   }
1244c2e6f3c0SFlorian Wechsung 
12450904074fSPatrick Farrell   isNonlinear = patch->isNonlinear;
1246b6bb21d1SLawrence Mitchell   if (isNonlinear) {
12479566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numPoints * Nf, &offsArrayWithAll));
12489566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numDofs, &asmArrayWithAll));
12499566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numDofs, &dofsArrayWithAll));
12509566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithAll));
12510904074fSPatrick Farrell     gtolCountsWithAll = patch->gtolCountsWithAll;
12529566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(gtolCountsWithAll, vStart, vEnd));
12539566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)patch->gtolCountsWithAll, "Patch Global Index Section Including All BCs"));
12540904074fSPatrick Farrell   }
12550904074fSPatrick Farrell 
1256557beb66SLawrence Mitchell   /* Outside the patch loop, get the dofs that are globally-enforced Dirichlet
1257557beb66SLawrence Mitchell    conditions */
12589566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&globalBcs));
12599566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->ghostBcNodes, &bcNodes));
12609566063dSJacob Faibussowitsch   PetscCall(ISGetSize(patch->ghostBcNodes, &numBcs));
12619371c9d4SSatish Balay   for (i = 0; i < numBcs; ++i) { PetscCall(PetscHSetIAdd(globalBcs, bcNodes[i])); /* these are already in concatenated numbering */ }
12629566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->ghostBcNodes, &bcNodes));
12639566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->ghostBcNodes)); /* memory optimisation */
1264557beb66SLawrence Mitchell 
1265557beb66SLawrence Mitchell   /* Hash tables for artificial BC construction */
12669566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&ownedpts));
12679566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&seenpts));
12689566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&owneddofs));
12699566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&seendofs));
12709566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&artificialbcs));
1271557beb66SLawrence Mitchell 
12729566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(cells, &cellsArray));
12739566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(points, &pointsArray));
12749566063dSJacob Faibussowitsch   PetscCall(PetscHMapICreate(&ht));
12759566063dSJacob Faibussowitsch   PetscCall(PetscHMapICreate(&htWithArtificial));
12769566063dSJacob Faibussowitsch   PetscCall(PetscHMapICreate(&htWithAll));
12774bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
12784bbf5ea8SMatthew G. Knepley     PetscInt localIndex               = 0;
1279c2e6f3c0SFlorian Wechsung     PetscInt localIndexWithArtificial = 0;
12800904074fSPatrick Farrell     PetscInt localIndexWithAll        = 0;
12814bbf5ea8SMatthew G. Knepley     PetscInt dof, off, i, j, k, l;
12824bbf5ea8SMatthew G. Knepley 
12839566063dSJacob Faibussowitsch     PetscCall(PetscHMapIClear(ht));
12849566063dSJacob Faibussowitsch     PetscCall(PetscHMapIClear(htWithArtificial));
12859566063dSJacob Faibussowitsch     PetscCall(PetscHMapIClear(htWithAll));
12869566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(cellCounts, v, &dof));
12879566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(cellCounts, v, &off));
12884bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
12894bbf5ea8SMatthew G. Knepley 
1290557beb66SLawrence Mitchell     /* Calculate the global numbers of the artificial BC dofs here first */
12919566063dSJacob Faibussowitsch     PetscCall(patch->patchconstructop((void *)patch, dm, v, ownedpts));
12929566063dSJacob Faibussowitsch     PetscCall(PCPatchCompleteCellPatch(pc, ownedpts, seenpts));
12939566063dSJacob Faibussowitsch     PetscCall(PCPatchGetPointDofs(pc, ownedpts, owneddofs, v, &patch->subspaces_to_exclude));
12949566063dSJacob Faibussowitsch     PetscCall(PCPatchGetPointDofs(pc, seenpts, seendofs, v, NULL));
12959566063dSJacob Faibussowitsch     PetscCall(PCPatchComputeSetDifference_Private(owneddofs, seendofs, artificialbcs));
12968135ed82SLawrence Mitchell     if (patch->viewPatches) {
12971b68eb51SMatthew G. Knepley       PetscHSetI    globalbcdofs;
12981b68eb51SMatthew G. Knepley       PetscHashIter hi;
12998135ed82SLawrence Mitchell       MPI_Comm      comm = PetscObjectComm((PetscObject)pc);
13001b68eb51SMatthew G. Knepley 
13019566063dSJacob Faibussowitsch       PetscCall(PetscHSetICreate(&globalbcdofs));
130263a3b9bcSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": owned dofs:\n", v));
13031b68eb51SMatthew G. Knepley       PetscHashIterBegin(owneddofs, hi);
13041b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(owneddofs, hi)) {
13058135ed82SLawrence Mitchell         PetscInt globalDof;
13068135ed82SLawrence Mitchell 
13071b68eb51SMatthew G. Knepley         PetscHashIterGetKey(owneddofs, hi, globalDof);
13081b68eb51SMatthew G. Knepley         PetscHashIterNext(owneddofs, hi);
130963a3b9bcSJacob Faibussowitsch         PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof));
13108135ed82SLawrence Mitchell       }
13119566063dSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "\n"));
131263a3b9bcSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": seen dofs:\n", v));
13131b68eb51SMatthew G. Knepley       PetscHashIterBegin(seendofs, hi);
13141b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(seendofs, hi)) {
13158135ed82SLawrence Mitchell         PetscInt  globalDof;
13168135ed82SLawrence Mitchell         PetscBool flg;
13178135ed82SLawrence Mitchell 
13181b68eb51SMatthew G. Knepley         PetscHashIterGetKey(seendofs, hi, globalDof);
13191b68eb51SMatthew G. Knepley         PetscHashIterNext(seendofs, hi);
132063a3b9bcSJacob Faibussowitsch         PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof));
13218135ed82SLawrence Mitchell 
13229566063dSJacob Faibussowitsch         PetscCall(PetscHSetIHas(globalBcs, globalDof, &flg));
13239566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscHSetIAdd(globalbcdofs, globalDof));
13248135ed82SLawrence Mitchell       }
13259566063dSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "\n"));
132663a3b9bcSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": global BCs:\n", v));
13279566063dSJacob Faibussowitsch       PetscCall(PetscHSetIGetSize(globalbcdofs, &numBcs));
13288135ed82SLawrence Mitchell       if (numBcs > 0) {
13291b68eb51SMatthew G. Knepley         PetscHashIterBegin(globalbcdofs, hi);
13301b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(globalbcdofs, hi)) {
13318135ed82SLawrence Mitchell           PetscInt globalDof;
13321b68eb51SMatthew G. Knepley           PetscHashIterGetKey(globalbcdofs, hi, globalDof);
13331b68eb51SMatthew G. Knepley           PetscHashIterNext(globalbcdofs, hi);
133463a3b9bcSJacob Faibussowitsch           PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof));
13358135ed82SLawrence Mitchell         }
13368135ed82SLawrence Mitchell       }
13379566063dSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "\n"));
133863a3b9bcSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": artificial BCs:\n", v));
13399566063dSJacob Faibussowitsch       PetscCall(PetscHSetIGetSize(artificialbcs, &numBcs));
13408135ed82SLawrence Mitchell       if (numBcs > 0) {
13411b68eb51SMatthew G. Knepley         PetscHashIterBegin(artificialbcs, hi);
13421b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(artificialbcs, hi)) {
13438135ed82SLawrence Mitchell           PetscInt globalDof;
13441b68eb51SMatthew G. Knepley           PetscHashIterGetKey(artificialbcs, hi, globalDof);
13451b68eb51SMatthew G. Knepley           PetscHashIterNext(artificialbcs, hi);
134663a3b9bcSJacob Faibussowitsch           PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof));
13478135ed82SLawrence Mitchell         }
13488135ed82SLawrence Mitchell       }
13499566063dSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "\n\n"));
13509566063dSJacob Faibussowitsch       PetscCall(PetscHSetIDestroy(&globalbcdofs));
13518135ed82SLawrence Mitchell     }
13524bbf5ea8SMatthew G. Knepley     for (k = 0; k < patch->nsubspaces; ++k) {
13534bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
13544bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
13554bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
13564bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
13574bbf5ea8SMatthew G. Knepley 
13584bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
13594bbf5ea8SMatthew G. Knepley         /* Walk over the cells in this patch. */
13604bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
13615f824522SMatthew G. Knepley         PetscInt       cell = c;
13624bbf5ea8SMatthew G. Knepley 
13635f824522SMatthew G. Knepley         /* TODO Change this to an IS */
13645f824522SMatthew G. Knepley         if (cellNumbering) {
13659566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetDof(cellNumbering, c, &cell));
136663a3b9bcSJacob Faibussowitsch           PetscCheck(cell > 0, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Cell %" PetscInt_FMT " doesn't appear in cell numbering map", c);
13679566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetOffset(cellNumbering, c, &cell));
13685f824522SMatthew G. Knepley         }
13694bbf5ea8SMatthew G. Knepley         newCellsArray[i] = cell;
13704bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
13714bbf5ea8SMatthew G. Knepley           /* For each global dof, map it into contiguous local storage. */
13724bbf5ea8SMatthew G. Knepley           const PetscInt globalDof = cellNodeMap[cell * nodesPerCell + j] * bs + subspaceOffset;
13734bbf5ea8SMatthew G. Knepley           /* finally, loop over block size */
13744bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
13751b68eb51SMatthew G. Knepley             PetscInt  localDof;
13761b68eb51SMatthew G. Knepley             PetscBool isGlobalBcDof, isArtificialBcDof;
13774bbf5ea8SMatthew G. Knepley 
1378557beb66SLawrence Mitchell             /* first, check if this is either a globally enforced or locally enforced BC dof */
13799566063dSJacob Faibussowitsch             PetscCall(PetscHSetIHas(globalBcs, globalDof + l, &isGlobalBcDof));
13809566063dSJacob Faibussowitsch             PetscCall(PetscHSetIHas(artificialbcs, globalDof + l, &isArtificialBcDof));
1381557beb66SLawrence Mitchell 
1382557beb66SLawrence Mitchell             /* if it's either, don't ever give it a local dof number */
13831b68eb51SMatthew G. Knepley             if (isGlobalBcDof || isArtificialBcDof) {
1384c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = -1; /* don't use this in assembly in this patch */
1385557beb66SLawrence Mitchell             } else {
13869566063dSJacob Faibussowitsch               PetscCall(PetscHMapIGet(ht, globalDof + l, &localDof));
13874bbf5ea8SMatthew G. Knepley               if (localDof == -1) {
13884bbf5ea8SMatthew G. Knepley                 localDof = localIndex++;
13899566063dSJacob Faibussowitsch                 PetscCall(PetscHMapISet(ht, globalDof + l, localDof));
13904bbf5ea8SMatthew G. Knepley               }
139163a3b9bcSJacob Faibussowitsch               PetscCheck(globalIndex < numDofs, PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %" PetscInt_FMT " than expected %" PetscInt_FMT, globalIndex + 1, numDofs);
13924bbf5ea8SMatthew G. Knepley               /* And store. */
1393c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = localDof;
13944bbf5ea8SMatthew G. Knepley             }
1395c2e6f3c0SFlorian Wechsung 
13960904074fSPatrick Farrell             if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1397c2e6f3c0SFlorian Wechsung               if (isGlobalBcDof) {
1398e047a90bSFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = -1; /* don't use this in assembly in this patch */
1399c2e6f3c0SFlorian Wechsung               } else {
14009566063dSJacob Faibussowitsch                 PetscCall(PetscHMapIGet(htWithArtificial, globalDof + l, &localDof));
1401c2e6f3c0SFlorian Wechsung                 if (localDof == -1) {
1402c2e6f3c0SFlorian Wechsung                   localDof = localIndexWithArtificial++;
14039566063dSJacob Faibussowitsch                   PetscCall(PetscHMapISet(htWithArtificial, globalDof + l, localDof));
1404c2e6f3c0SFlorian Wechsung                 }
140563a3b9bcSJacob Faibussowitsch                 PetscCheck(globalIndex < numDofs, PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %" PetscInt_FMT " than expected %" PetscInt_FMT, globalIndex + 1, numDofs);
1406c2e6f3c0SFlorian Wechsung                 /* And store.*/
1407c2e6f3c0SFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = localDof;
1408c2e6f3c0SFlorian Wechsung               }
1409c2e6f3c0SFlorian Wechsung             }
14100904074fSPatrick Farrell 
14110904074fSPatrick Farrell             if (isNonlinear) {
14120904074fSPatrick Farrell               /* Build the dofmap for the function space with _all_ dofs,
14130904074fSPatrick Farrell    including those in any kind of boundary condition */
14149566063dSJacob Faibussowitsch               PetscCall(PetscHMapIGet(htWithAll, globalDof + l, &localDof));
14150904074fSPatrick Farrell               if (localDof == -1) {
14160904074fSPatrick Farrell                 localDof = localIndexWithAll++;
14179566063dSJacob Faibussowitsch                 PetscCall(PetscHMapISet(htWithAll, globalDof + l, localDof));
14180904074fSPatrick Farrell               }
141963a3b9bcSJacob Faibussowitsch               PetscCheck(globalIndex < numDofs, PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %" PetscInt_FMT " than expected %" PetscInt_FMT, globalIndex + 1, numDofs);
14200904074fSPatrick Farrell               /* And store.*/
14210904074fSPatrick Farrell               dofsArrayWithAll[globalIndex] = localDof;
14220904074fSPatrick Farrell             }
1423c2e6f3c0SFlorian Wechsung             globalIndex++;
14244bbf5ea8SMatthew G. Knepley           }
14254bbf5ea8SMatthew G. Knepley         }
14264bbf5ea8SMatthew G. Knepley       }
1427557beb66SLawrence Mitchell     }
14284bbf5ea8SMatthew G. Knepley     /* How many local dofs in this patch? */
14290904074fSPatrick Farrell     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
14309566063dSJacob Faibussowitsch       PetscCall(PetscHMapIGetSize(htWithArtificial, &dof));
14319566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(gtolCountsWithArtificial, v, dof));
1432c2e6f3c0SFlorian Wechsung     }
14330904074fSPatrick Farrell     if (isNonlinear) {
14349566063dSJacob Faibussowitsch       PetscCall(PetscHMapIGetSize(htWithAll, &dof));
14359566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(gtolCountsWithAll, v, dof));
14360904074fSPatrick Farrell     }
14379566063dSJacob Faibussowitsch     PetscCall(PetscHMapIGetSize(ht, &dof));
14389566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(gtolCounts, v, dof));
14394bbf5ea8SMatthew G. Knepley   }
1440b6bb21d1SLawrence Mitchell 
14419566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
144263a3b9bcSJacob 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);
14439566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(gtolCounts));
14449566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(gtolCounts, &numGlobalDofs));
14459566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numGlobalDofs, &globalDofsArray));
14464bbf5ea8SMatthew G. Knepley 
14470904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
14489566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(gtolCountsWithArtificial));
14499566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(gtolCountsWithArtificial, &numGlobalDofsWithArtificial));
14509566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numGlobalDofsWithArtificial, &globalDofsArrayWithArtificial));
1451c2e6f3c0SFlorian Wechsung   }
14520904074fSPatrick Farrell   if (isNonlinear) {
14539566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(gtolCountsWithAll));
14549566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(gtolCountsWithAll, &numGlobalDofsWithAll));
14559566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numGlobalDofsWithAll, &globalDofsArrayWithAll));
14560904074fSPatrick Farrell   }
14574bbf5ea8SMatthew 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. */
14584bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
14591b68eb51SMatthew G. Knepley     PetscHashIter hi;
14605f824522SMatthew G. Knepley     PetscInt      dof, off, Np, ooff, i, j, k, l;
14614bbf5ea8SMatthew G. Knepley 
14629566063dSJacob Faibussowitsch     PetscCall(PetscHMapIClear(ht));
14639566063dSJacob Faibussowitsch     PetscCall(PetscHMapIClear(htWithArtificial));
14649566063dSJacob Faibussowitsch     PetscCall(PetscHMapIClear(htWithAll));
14659566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(cellCounts, v, &dof));
14669566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(cellCounts, v, &off));
14679566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(pointCounts, v, &Np));
14689566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(pointCounts, v, &ooff));
14694bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
14704bbf5ea8SMatthew G. Knepley 
14714bbf5ea8SMatthew G. Knepley     for (k = 0; k < patch->nsubspaces; ++k) {
14724bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
14734bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
14744bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
14754bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
1476d490bb3dSLawrence Mitchell       PetscInt        goff;
14774bbf5ea8SMatthew G. Knepley 
14784bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
14794bbf5ea8SMatthew G. Knepley         /* Reconstruct mapping of global-to-local on this patch. */
14804bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
14815f824522SMatthew G. Knepley         PetscInt       cell = c;
14824bbf5ea8SMatthew G. Knepley 
14839566063dSJacob Faibussowitsch         if (cellNumbering) PetscCall(PetscSectionGetOffset(cellNumbering, c, &cell));
14844bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
14854bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
14865f824522SMatthew G. Knepley             const PetscInt globalDof = cellNodeMap[cell * nodesPerCell + j] * bs + l + subspaceOffset;
1487c2e6f3c0SFlorian Wechsung             const PetscInt localDof  = dofsArray[key];
14889566063dSJacob Faibussowitsch             if (localDof >= 0) PetscCall(PetscHMapISet(ht, globalDof, localDof));
14890904074fSPatrick Farrell             if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1490c2e6f3c0SFlorian Wechsung               const PetscInt localDofWithArtificial = dofsArrayWithArtificial[key];
149148a46eb9SPierre Jolivet               if (localDofWithArtificial >= 0) PetscCall(PetscHMapISet(htWithArtificial, globalDof, localDofWithArtificial));
1492c2e6f3c0SFlorian Wechsung             }
14930904074fSPatrick Farrell             if (isNonlinear) {
14940904074fSPatrick Farrell               const PetscInt localDofWithAll = dofsArrayWithAll[key];
149548a46eb9SPierre Jolivet               if (localDofWithAll >= 0) PetscCall(PetscHMapISet(htWithAll, globalDof, localDofWithAll));
14960904074fSPatrick Farrell             }
1497c2e6f3c0SFlorian Wechsung             key++;
14984bbf5ea8SMatthew G. Knepley           }
14994bbf5ea8SMatthew G. Knepley         }
15004bbf5ea8SMatthew G. Knepley       }
1501557beb66SLawrence Mitchell 
15024bbf5ea8SMatthew G. Knepley       /* Shove it in the output data structure. */
15039566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(gtolCounts, v, &goff));
15041b68eb51SMatthew G. Knepley       PetscHashIterBegin(ht, hi);
15051b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(ht, hi)) {
15064bbf5ea8SMatthew G. Knepley         PetscInt globalDof, localDof;
15074bbf5ea8SMatthew G. Knepley 
15081b68eb51SMatthew G. Knepley         PetscHashIterGetKey(ht, hi, globalDof);
15091b68eb51SMatthew G. Knepley         PetscHashIterGetVal(ht, hi, localDof);
15104bbf5ea8SMatthew G. Knepley         if (globalDof >= 0) globalDofsArray[goff + localDof] = globalDof;
15111b68eb51SMatthew G. Knepley         PetscHashIterNext(ht, hi);
15124bbf5ea8SMatthew G. Knepley       }
15135f824522SMatthew G. Knepley 
15140904074fSPatrick Farrell       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
15159566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gtolCountsWithArtificial, v, &goff));
1516c2e6f3c0SFlorian Wechsung         PetscHashIterBegin(htWithArtificial, hi);
1517c2e6f3c0SFlorian Wechsung         while (!PetscHashIterAtEnd(htWithArtificial, hi)) {
1518c2e6f3c0SFlorian Wechsung           PetscInt globalDof, localDof;
1519c2e6f3c0SFlorian Wechsung           PetscHashIterGetKey(htWithArtificial, hi, globalDof);
1520c2e6f3c0SFlorian Wechsung           PetscHashIterGetVal(htWithArtificial, hi, localDof);
1521c2e6f3c0SFlorian Wechsung           if (globalDof >= 0) globalDofsArrayWithArtificial[goff + localDof] = globalDof;
1522c2e6f3c0SFlorian Wechsung           PetscHashIterNext(htWithArtificial, hi);
1523c2e6f3c0SFlorian Wechsung         }
1524c2e6f3c0SFlorian Wechsung       }
15250904074fSPatrick Farrell       if (isNonlinear) {
15269566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gtolCountsWithAll, v, &goff));
15270904074fSPatrick Farrell         PetscHashIterBegin(htWithAll, hi);
15280904074fSPatrick Farrell         while (!PetscHashIterAtEnd(htWithAll, hi)) {
15290904074fSPatrick Farrell           PetscInt globalDof, localDof;
15300904074fSPatrick Farrell           PetscHashIterGetKey(htWithAll, hi, globalDof);
15310904074fSPatrick Farrell           PetscHashIterGetVal(htWithAll, hi, localDof);
15320904074fSPatrick Farrell           if (globalDof >= 0) globalDofsArrayWithAll[goff + localDof] = globalDof;
15330904074fSPatrick Farrell           PetscHashIterNext(htWithAll, hi);
15340904074fSPatrick Farrell         }
15350904074fSPatrick Farrell       }
1536c2e6f3c0SFlorian Wechsung 
15375f824522SMatthew G. Knepley       for (p = 0; p < Np; ++p) {
15385f824522SMatthew G. Knepley         const PetscInt point = pointsArray[ooff + p];
15395f824522SMatthew G. Knepley         PetscInt       globalDof, localDof;
15405f824522SMatthew G. Knepley 
15419566063dSJacob Faibussowitsch         PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, point, NULL, &globalDof));
15429566063dSJacob Faibussowitsch         PetscCall(PetscHMapIGet(ht, globalDof, &localDof));
15435f824522SMatthew G. Knepley         offsArray[(ooff + p) * Nf + k] = localDof;
15440904074fSPatrick Farrell         if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
15459566063dSJacob Faibussowitsch           PetscCall(PetscHMapIGet(htWithArtificial, globalDof, &localDof));
1546c2e6f3c0SFlorian Wechsung           offsArrayWithArtificial[(ooff + p) * Nf + k] = localDof;
1547c2e6f3c0SFlorian Wechsung         }
15480904074fSPatrick Farrell         if (isNonlinear) {
15499566063dSJacob Faibussowitsch           PetscCall(PetscHMapIGet(htWithAll, globalDof, &localDof));
15500904074fSPatrick Farrell           offsArrayWithAll[(ooff + p) * Nf + k] = localDof;
15510904074fSPatrick Farrell         }
15525f824522SMatthew G. Knepley       }
15534bbf5ea8SMatthew G. Knepley     }
15544bbf5ea8SMatthew G. Knepley 
15559566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&globalBcs));
15569566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&ownedpts));
15579566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&seenpts));
15589566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&owneddofs));
15599566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&seendofs));
15609566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&artificialbcs));
1561557beb66SLawrence Mitchell 
15624bbf5ea8SMatthew G. Knepley     /* At this point, we have a hash table ht built that maps globalDof -> localDof.
15634bbf5ea8SMatthew G. Knepley    We need to create the dof table laid out cellwise first, then by subspace,
15644bbf5ea8SMatthew G. Knepley    as the assembler assembles cell-wise and we need to stuff the different
15654bbf5ea8SMatthew G. Knepley    contributions of the different function spaces to the right places. So we loop
15664bbf5ea8SMatthew G. Knepley    over cells, then over subspaces. */
15674bbf5ea8SMatthew G. Knepley     if (patch->nsubspaces > 1) { /* for nsubspaces = 1, data we need is already in dofsArray */
15684bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
15694bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
15705f824522SMatthew G. Knepley         PetscInt       cell = c;
15714bbf5ea8SMatthew G. Knepley 
15729566063dSJacob Faibussowitsch         if (cellNumbering) PetscCall(PetscSectionGetOffset(cellNumbering, c, &cell));
15734bbf5ea8SMatthew G. Knepley         for (k = 0; k < patch->nsubspaces; ++k) {
15744bbf5ea8SMatthew G. Knepley           const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
15754bbf5ea8SMatthew G. Knepley           PetscInt        nodesPerCell   = patch->nodesPerCell[k];
15764bbf5ea8SMatthew G. Knepley           PetscInt        subspaceOffset = patch->subspaceOffsets[k];
15774bbf5ea8SMatthew G. Knepley           PetscInt        bs             = patch->bs[k];
15784bbf5ea8SMatthew G. Knepley 
15794bbf5ea8SMatthew G. Knepley           for (j = 0; j < nodesPerCell; ++j) {
15804bbf5ea8SMatthew G. Knepley             for (l = 0; l < bs; ++l) {
15815f824522SMatthew G. Knepley               const PetscInt globalDof = cellNodeMap[cell * nodesPerCell + j] * bs + l + subspaceOffset;
15824bbf5ea8SMatthew G. Knepley               PetscInt       localDof;
15834bbf5ea8SMatthew G. Knepley 
15849566063dSJacob Faibussowitsch               PetscCall(PetscHMapIGet(ht, globalDof, &localDof));
1585557beb66SLawrence Mitchell               /* If it's not in the hash table, i.e. is a BC dof,
15861b68eb51SMatthew G. Knepley    then the PetscHSetIMap above gives -1, which matches
1587557beb66SLawrence Mitchell    exactly the convention for PETSc's matrix assembly to
1588557beb66SLawrence Mitchell    ignore the dof. So we don't need to do anything here */
1589c2e6f3c0SFlorian Wechsung               asmArray[asmKey] = localDof;
15900904074fSPatrick Farrell               if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
15919566063dSJacob Faibussowitsch                 PetscCall(PetscHMapIGet(htWithArtificial, globalDof, &localDof));
1592c2e6f3c0SFlorian Wechsung                 asmArrayWithArtificial[asmKey] = localDof;
1593c2e6f3c0SFlorian Wechsung               }
15940904074fSPatrick Farrell               if (isNonlinear) {
15959566063dSJacob Faibussowitsch                 PetscCall(PetscHMapIGet(htWithAll, globalDof, &localDof));
15960904074fSPatrick Farrell                 asmArrayWithAll[asmKey] = localDof;
15970904074fSPatrick Farrell               }
1598c2e6f3c0SFlorian Wechsung               asmKey++;
15994bbf5ea8SMatthew G. Knepley             }
16004bbf5ea8SMatthew G. Knepley           }
16014bbf5ea8SMatthew G. Knepley         }
16024bbf5ea8SMatthew G. Knepley       }
16034bbf5ea8SMatthew G. Knepley     }
16044bbf5ea8SMatthew G. Knepley   }
1605c2e6f3c0SFlorian Wechsung   if (1 == patch->nsubspaces) {
16069566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(asmArray, dofsArray, numDofs));
160748a46eb9SPierre Jolivet     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) PetscCall(PetscArraycpy(asmArrayWithArtificial, dofsArrayWithArtificial, numDofs));
16081baa6e33SBarry Smith     if (isNonlinear) PetscCall(PetscArraycpy(asmArrayWithAll, dofsArrayWithAll, numDofs));
1609c2e6f3c0SFlorian Wechsung   }
16104bbf5ea8SMatthew G. Knepley 
16119566063dSJacob Faibussowitsch   PetscCall(PetscHMapIDestroy(&ht));
16129566063dSJacob Faibussowitsch   PetscCall(PetscHMapIDestroy(&htWithArtificial));
16139566063dSJacob Faibussowitsch   PetscCall(PetscHMapIDestroy(&htWithAll));
16149566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(cells, &cellsArray));
16159566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(points, &pointsArray));
16169566063dSJacob Faibussowitsch   PetscCall(PetscFree(dofsArray));
161748a46eb9SPierre Jolivet   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) PetscCall(PetscFree(dofsArrayWithArtificial));
16181baa6e33SBarry Smith   if (isNonlinear) PetscCall(PetscFree(dofsArrayWithAll));
16195f824522SMatthew G. Knepley   /* Create placeholder section for map from points to patch dofs */
16209566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->patchSection));
16219566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(patch->patchSection, patch->nsubspaces));
16221e5fa6bbSLawrence Mitchell   if (patch->combined) {
16231e5fa6bbSLawrence Mitchell     PetscInt numFields;
16249566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(patch->dofSection[0], &numFields));
162563a3b9bcSJacob 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);
16269566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(patch->dofSection[0], &pStart, &pEnd));
16279566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(patch->patchSection, pStart, pEnd));
16285f824522SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
16295f824522SMatthew G. Knepley       PetscInt dof, fdof, f;
16305f824522SMatthew G. Knepley 
16319566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(patch->dofSection[0], p, &dof));
16329566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(patch->patchSection, p, dof));
16335f824522SMatthew G. Knepley       for (f = 0; f < patch->nsubspaces; ++f) {
16349566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldDof(patch->dofSection[0], p, f, &fdof));
16359566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetFieldDof(patch->patchSection, p, f, fdof));
16365f824522SMatthew G. Knepley       }
16371e5fa6bbSLawrence Mitchell     }
16381e5fa6bbSLawrence Mitchell   } else {
16391e5fa6bbSLawrence Mitchell     PetscInt pStartf, pEndf, f;
16401e5fa6bbSLawrence Mitchell     pStart = PETSC_MAX_INT;
16411e5fa6bbSLawrence Mitchell     pEnd   = PETSC_MIN_INT;
16421e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
16439566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf));
16441e5fa6bbSLawrence Mitchell       pStart = PetscMin(pStart, pStartf);
16451e5fa6bbSLawrence Mitchell       pEnd   = PetscMax(pEnd, pEndf);
16461e5fa6bbSLawrence Mitchell     }
16479566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(patch->patchSection, pStart, pEnd));
16481e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
16499566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf));
16501e5fa6bbSLawrence Mitchell       for (p = pStartf; p < pEndf; ++p) {
16511e5fa6bbSLawrence Mitchell         PetscInt fdof;
16529566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->dofSection[f], p, &fdof));
16539566063dSJacob Faibussowitsch         PetscCall(PetscSectionAddDof(patch->patchSection, p, fdof));
16549566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetFieldDof(patch->patchSection, p, f, fdof));
1655bdd9e0cdSPatrick Farrell       }
1656bdd9e0cdSPatrick Farrell     }
16575f824522SMatthew G. Knepley   }
16589566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(patch->patchSection));
16599566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUseFieldOffsets(patch->patchSection, PETSC_TRUE));
16604bbf5ea8SMatthew G. Knepley   /* Replace cell indices with firedrake-numbered ones. */
16619566063dSJacob Faibussowitsch   PetscCall(ISGeneralSetIndices(cells, numCells, (const PetscInt *)newCellsArray, PETSC_OWN_POINTER));
16629566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofs, globalDofsArray, PETSC_OWN_POINTER, &patch->gtol));
16639566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->gtol, "Global Indices"));
16649566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_g2l_view", patch->classname));
16659566063dSJacob Faibussowitsch   PetscCall(PetscSectionViewFromOptions(patch->gtolCounts, (PetscObject)pc, option));
16669566063dSJacob Faibussowitsch   PetscCall(ISViewFromOptions(patch->gtol, (PetscObject)pc, option));
16679566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArray, PETSC_OWN_POINTER, &patch->dofs));
16689566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints * Nf, offsArray, PETSC_OWN_POINTER, &patch->offs));
16690904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
16709566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithArtificial, globalDofsArrayWithArtificial, PETSC_OWN_POINTER, &patch->gtolWithArtificial));
16719566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithArtificial, PETSC_OWN_POINTER, &patch->dofsWithArtificial));
16729566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints * Nf, offsArrayWithArtificial, PETSC_OWN_POINTER, &patch->offsWithArtificial));
1673c2e6f3c0SFlorian Wechsung   }
16740904074fSPatrick Farrell   if (isNonlinear) {
16759566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithAll, globalDofsArrayWithAll, PETSC_OWN_POINTER, &patch->gtolWithAll));
16769566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithAll, PETSC_OWN_POINTER, &patch->dofsWithAll));
16779566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints * Nf, offsArrayWithAll, PETSC_OWN_POINTER, &patch->offsWithAll));
16780904074fSPatrick Farrell   }
16793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
16804bbf5ea8SMatthew G. Knepley }
16814bbf5ea8SMatthew G. Knepley 
1682d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCreateMatrix_Private(PC pc, PetscInt point, Mat *mat, PetscBool withArtificial)
1683d71ae5a4SJacob Faibussowitsch {
16844bbf5ea8SMatthew G. Knepley   PC_PATCH   *patch = (PC_PATCH *)pc->data;
16854bbf5ea8SMatthew G. Knepley   PetscBool   flg;
16864bbf5ea8SMatthew G. Knepley   PetscInt    csize, rsize;
16874bbf5ea8SMatthew G. Knepley   const char *prefix = NULL;
16884bbf5ea8SMatthew G. Knepley 
16894bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1690c2e6f3c0SFlorian Wechsung   if (withArtificial) {
1691e047a90bSFlorian Wechsung     /* would be nice if we could create a rectangular matrix of size numDofsWithArtificial x numDofs here */
16929d4fc724SLawrence Mitchell     PetscInt pStart;
16939566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(patch->gtolCountsWithArtificial, &pStart, NULL));
16949566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, point + pStart, &rsize));
16959d4fc724SLawrence Mitchell     csize = rsize;
1696ff201f6aSFlorian Wechsung   } else {
16979d4fc724SLawrence Mitchell     PetscInt pStart;
16989566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, NULL));
16999566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(patch->gtolCounts, point + pStart, &rsize));
17009d4fc724SLawrence Mitchell     csize = rsize;
1701c2e6f3c0SFlorian Wechsung   }
1702c2e6f3c0SFlorian Wechsung 
17039566063dSJacob Faibussowitsch   PetscCall(MatCreate(PETSC_COMM_SELF, mat));
17049566063dSJacob Faibussowitsch   PetscCall(PCGetOptionsPrefix(pc, &prefix));
17059566063dSJacob Faibussowitsch   PetscCall(MatSetOptionsPrefix(*mat, prefix));
17069566063dSJacob Faibussowitsch   PetscCall(MatAppendOptionsPrefix(*mat, "pc_patch_sub_"));
17079566063dSJacob Faibussowitsch   if (patch->sub_mat_type) PetscCall(MatSetType(*mat, patch->sub_mat_type));
17089566063dSJacob Faibussowitsch   else if (!patch->sub_mat_type) PetscCall(MatSetType(*mat, MATDENSE));
17099566063dSJacob Faibussowitsch   PetscCall(MatSetSizes(*mat, rsize, csize, rsize, csize));
17109566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)*mat, MATDENSE, &flg));
17119566063dSJacob Faibussowitsch   if (!flg) PetscCall(PetscObjectTypeCompare((PetscObject)*mat, MATSEQDENSE, &flg));
17124bbf5ea8SMatthew G. Knepley   /* Sparse patch matrices */
17134bbf5ea8SMatthew G. Knepley   if (!flg) {
17144bbf5ea8SMatthew G. Knepley     PetscBT         bt;
17154bbf5ea8SMatthew G. Knepley     PetscInt       *dnnz      = NULL;
17164bbf5ea8SMatthew G. Knepley     const PetscInt *dofsArray = NULL;
17174bbf5ea8SMatthew G. Knepley     PetscInt        pStart, pEnd, ncell, offset, c, i, j;
17184bbf5ea8SMatthew G. Knepley 
1719c2e6f3c0SFlorian Wechsung     if (withArtificial) {
17209566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(patch->dofsWithArtificial, &dofsArray));
1721ff201f6aSFlorian Wechsung     } else {
17229566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(patch->dofs, &dofsArray));
1723c2e6f3c0SFlorian Wechsung     }
17249566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd));
17254bbf5ea8SMatthew G. Knepley     point += pStart;
172663a3b9bcSJacob Faibussowitsch     PetscCheck(point < pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %" PetscInt_FMT " not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, pStart, pEnd);
17279566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(patch->cellCounts, point, &ncell));
17289566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(patch->cellCounts, point, &offset));
17299566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(PC_Patch_Prealloc, pc, 0, 0, 0));
1730b2866507SPatrick Farrell     /* A PetscBT uses N^2 bits to store the sparsity pattern on a
17314bbf5ea8SMatthew G. Knepley    * patch. This is probably OK if the patches are not too big,
1732b2866507SPatrick Farrell    * but uses too much memory. We therefore switch based on rsize. */
1733b2866507SPatrick Farrell     if (rsize < 3000) { /* FIXME: I picked this switch value out of my hat */
1734d63cebbaSPatrick Farrell       PetscScalar *zeroes;
1735d63cebbaSPatrick Farrell       PetscInt     rows;
1736d63cebbaSPatrick Farrell 
17379566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(rsize, &dnnz));
17389566063dSJacob Faibussowitsch       PetscCall(PetscBTCreate(rsize * rsize, &bt));
17394bbf5ea8SMatthew G. Knepley       for (c = 0; c < ncell; ++c) {
17404bbf5ea8SMatthew G. Knepley         const PetscInt *idx = dofsArray + (offset + c) * patch->totalDofsPerCell;
17414bbf5ea8SMatthew G. Knepley         for (i = 0; i < patch->totalDofsPerCell; ++i) {
17424bbf5ea8SMatthew G. Knepley           const PetscInt row = idx[i];
1743557beb66SLawrence Mitchell           if (row < 0) continue;
17444bbf5ea8SMatthew G. Knepley           for (j = 0; j < patch->totalDofsPerCell; ++j) {
17454bbf5ea8SMatthew G. Knepley             const PetscInt col = idx[j];
17464bbf5ea8SMatthew G. Knepley             const PetscInt key = row * rsize + col;
1747557beb66SLawrence Mitchell             if (col < 0) continue;
17484bbf5ea8SMatthew G. Knepley             if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
17494bbf5ea8SMatthew G. Knepley           }
17504bbf5ea8SMatthew G. Knepley         }
17514bbf5ea8SMatthew G. Knepley       }
1752d63cebbaSPatrick Farrell 
1753d63cebbaSPatrick Farrell       if (patch->usercomputeopintfacet) {
1754d63cebbaSPatrick Farrell         const PetscInt *intFacetsArray = NULL;
1755d63cebbaSPatrick Farrell         PetscInt        i, numIntFacets, intFacetOffset;
1756d63cebbaSPatrick Farrell         const PetscInt *facetCells = NULL;
1757d63cebbaSPatrick Farrell 
17589566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets));
17599566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset));
17609566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells));
17619566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray));
1762d63cebbaSPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
1763d63cebbaSPatrick Farrell           const PetscInt cell0 = facetCells[2 * (intFacetOffset + i) + 0];
1764d63cebbaSPatrick Farrell           const PetscInt cell1 = facetCells[2 * (intFacetOffset + i) + 1];
1765d63cebbaSPatrick Farrell           PetscInt       celli, cellj;
1766d63cebbaSPatrick Farrell 
1767d63cebbaSPatrick Farrell           for (celli = 0; celli < patch->totalDofsPerCell; celli++) {
1768d63cebbaSPatrick Farrell             const PetscInt row = dofsArray[(offset + cell0) * patch->totalDofsPerCell + celli];
1769b5c64f08SPatrick Farrell             if (row < 0) continue;
1770d63cebbaSPatrick Farrell             for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) {
1771d63cebbaSPatrick Farrell               const PetscInt col = dofsArray[(offset + cell1) * patch->totalDofsPerCell + cellj];
1772d63cebbaSPatrick Farrell               const PetscInt key = row * rsize + col;
1773d63cebbaSPatrick Farrell               if (col < 0) continue;
1774d63cebbaSPatrick Farrell               if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
1775d63cebbaSPatrick Farrell             }
1776d63cebbaSPatrick Farrell           }
1777d63cebbaSPatrick Farrell 
1778d63cebbaSPatrick Farrell           for (celli = 0; celli < patch->totalDofsPerCell; celli++) {
1779d63cebbaSPatrick Farrell             const PetscInt row = dofsArray[(offset + cell1) * patch->totalDofsPerCell + celli];
1780b5c64f08SPatrick Farrell             if (row < 0) continue;
1781d63cebbaSPatrick Farrell             for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) {
1782d63cebbaSPatrick Farrell               const PetscInt col = dofsArray[(offset + cell0) * patch->totalDofsPerCell + cellj];
1783d63cebbaSPatrick Farrell               const PetscInt key = row * rsize + col;
1784d63cebbaSPatrick Farrell               if (col < 0) continue;
1785d63cebbaSPatrick Farrell               if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
1786d63cebbaSPatrick Farrell             }
1787d63cebbaSPatrick Farrell           }
1788d63cebbaSPatrick Farrell         }
1789d63cebbaSPatrick Farrell       }
17909566063dSJacob Faibussowitsch       PetscCall(PetscBTDestroy(&bt));
17919566063dSJacob Faibussowitsch       PetscCall(MatXAIJSetPreallocation(*mat, 1, dnnz, NULL, NULL, NULL));
17929566063dSJacob Faibussowitsch       PetscCall(PetscFree(dnnz));
1793d63cebbaSPatrick Farrell 
17949566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(patch->totalDofsPerCell * patch->totalDofsPerCell, &zeroes));
1795d63cebbaSPatrick Farrell       for (c = 0; c < ncell; ++c) {
1796d63cebbaSPatrick Farrell         const PetscInt *idx = &dofsArray[(offset + c) * patch->totalDofsPerCell];
17979566063dSJacob Faibussowitsch         PetscCall(MatSetValues(*mat, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, zeroes, INSERT_VALUES));
1798d63cebbaSPatrick Farrell       }
17999566063dSJacob Faibussowitsch       PetscCall(MatGetLocalSize(*mat, &rows, NULL));
180048a46eb9SPierre Jolivet       for (i = 0; i < rows; ++i) PetscCall(MatSetValues(*mat, 1, &i, 1, &i, zeroes, INSERT_VALUES));
1801d63cebbaSPatrick Farrell 
1802d63cebbaSPatrick Farrell       if (patch->usercomputeopintfacet) {
1803d63cebbaSPatrick Farrell         const PetscInt *intFacetsArray = NULL;
1804d63cebbaSPatrick Farrell         PetscInt        i, numIntFacets, intFacetOffset;
1805d63cebbaSPatrick Farrell         const PetscInt *facetCells = NULL;
1806d63cebbaSPatrick Farrell 
18079566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets));
18089566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset));
18099566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells));
18109566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray));
1811d63cebbaSPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
1812d63cebbaSPatrick Farrell           const PetscInt  cell0    = facetCells[2 * (intFacetOffset + i) + 0];
1813d63cebbaSPatrick Farrell           const PetscInt  cell1    = facetCells[2 * (intFacetOffset + i) + 1];
1814d63cebbaSPatrick Farrell           const PetscInt *cell0idx = &dofsArray[(offset + cell0) * patch->totalDofsPerCell];
1815d63cebbaSPatrick Farrell           const PetscInt *cell1idx = &dofsArray[(offset + cell1) * patch->totalDofsPerCell];
18169566063dSJacob Faibussowitsch           PetscCall(MatSetValues(*mat, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, zeroes, INSERT_VALUES));
18179566063dSJacob Faibussowitsch           PetscCall(MatSetValues(*mat, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, zeroes, INSERT_VALUES));
1818d63cebbaSPatrick Farrell         }
1819d63cebbaSPatrick Farrell       }
1820d63cebbaSPatrick Farrell 
18219566063dSJacob Faibussowitsch       PetscCall(MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY));
18229566063dSJacob Faibussowitsch       PetscCall(MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY));
1823d63cebbaSPatrick Farrell 
18249566063dSJacob Faibussowitsch       PetscCall(PetscFree(zeroes));
1825d63cebbaSPatrick Farrell 
1826b2866507SPatrick Farrell     } else { /* rsize too big, use MATPREALLOCATOR */
1827b2866507SPatrick Farrell       Mat          preallocator;
1828b2866507SPatrick Farrell       PetscScalar *vals;
1829b2866507SPatrick Farrell 
18309566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(patch->totalDofsPerCell * patch->totalDofsPerCell, &vals));
18319566063dSJacob Faibussowitsch       PetscCall(MatCreate(PETSC_COMM_SELF, &preallocator));
18329566063dSJacob Faibussowitsch       PetscCall(MatSetType(preallocator, MATPREALLOCATOR));
18339566063dSJacob Faibussowitsch       PetscCall(MatSetSizes(preallocator, rsize, rsize, rsize, rsize));
18349566063dSJacob Faibussowitsch       PetscCall(MatSetUp(preallocator));
183511bcd083SPatrick Farrell 
1836b2866507SPatrick Farrell       for (c = 0; c < ncell; ++c) {
1837b2866507SPatrick Farrell         const PetscInt *idx = dofsArray + (offset + c) * patch->totalDofsPerCell;
18389566063dSJacob Faibussowitsch         PetscCall(MatSetValues(preallocator, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, vals, INSERT_VALUES));
1839b2866507SPatrick Farrell       }
184011bcd083SPatrick Farrell 
184111bcd083SPatrick Farrell       if (patch->usercomputeopintfacet) {
184211bcd083SPatrick Farrell         const PetscInt *intFacetsArray = NULL;
184311bcd083SPatrick Farrell         PetscInt        i, numIntFacets, intFacetOffset;
184411bcd083SPatrick Farrell         const PetscInt *facetCells = NULL;
184511bcd083SPatrick Farrell 
18469566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets));
18479566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset));
18489566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells));
18499566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray));
185011bcd083SPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
185111bcd083SPatrick Farrell           const PetscInt  cell0    = facetCells[2 * (intFacetOffset + i) + 0];
185211bcd083SPatrick Farrell           const PetscInt  cell1    = facetCells[2 * (intFacetOffset + i) + 1];
185311bcd083SPatrick Farrell           const PetscInt *cell0idx = &dofsArray[(offset + cell0) * patch->totalDofsPerCell];
185411bcd083SPatrick Farrell           const PetscInt *cell1idx = &dofsArray[(offset + cell1) * patch->totalDofsPerCell];
18559566063dSJacob Faibussowitsch           PetscCall(MatSetValues(preallocator, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, vals, INSERT_VALUES));
18569566063dSJacob Faibussowitsch           PetscCall(MatSetValues(preallocator, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, vals, INSERT_VALUES));
185711bcd083SPatrick Farrell         }
185811bcd083SPatrick Farrell       }
185911bcd083SPatrick Farrell 
18609566063dSJacob Faibussowitsch       PetscCall(PetscFree(vals));
18619566063dSJacob Faibussowitsch       PetscCall(MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY));
18629566063dSJacob Faibussowitsch       PetscCall(MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY));
18639566063dSJacob Faibussowitsch       PetscCall(MatPreallocatorPreallocate(preallocator, PETSC_TRUE, *mat));
18649566063dSJacob Faibussowitsch       PetscCall(MatDestroy(&preallocator));
1865b2866507SPatrick Farrell     }
18669566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(PC_Patch_Prealloc, pc, 0, 0, 0));
1867fe117d09SFlorian Wechsung     if (withArtificial) {
18689566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(patch->dofsWithArtificial, &dofsArray));
1869fe117d09SFlorian Wechsung     } else {
18709566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(patch->dofs, &dofsArray));
18714bbf5ea8SMatthew G. Knepley     }
1872fe117d09SFlorian Wechsung   }
18739566063dSJacob Faibussowitsch   PetscCall(MatSetUp(*mat));
18743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
18754bbf5ea8SMatthew G. Knepley }
18764bbf5ea8SMatthew G. Knepley 
1877d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchComputeFunction_DMPlex_Private(PC pc, PetscInt patchNum, Vec x, Vec F, IS cellIS, PetscInt n, const PetscInt *l2p, const PetscInt *l2pWithAll, void *ctx)
1878d71ae5a4SJacob Faibussowitsch {
187992d50984SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *)pc->data;
1880b6bb21d1SLawrence Mitchell   DM              dm, plex;
188192d50984SMatthew G. Knepley   PetscSection    s;
188292d50984SMatthew G. Knepley   const PetscInt *parray, *oarray;
188392d50984SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
188492d50984SMatthew G. Knepley 
188592d50984SMatthew G. Knepley   PetscFunctionBegin;
188628b400f6SJacob Faibussowitsch   PetscCheck(!patch->precomputeElementTensors, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Precomputing element tensors not implemented with DMPlex compute operator");
18879566063dSJacob Faibussowitsch   PetscCall(PCGetDM(pc, &dm));
18889566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
1889b6bb21d1SLawrence Mitchell   dm = plex;
18909566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
189192d50984SMatthew G. Knepley   /* Set offset into patch */
18929566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetDof(patch->pointCounts, patchNum, &Np));
18939566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(patch->pointCounts, patchNum, &poff));
18949566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->points, &parray));
18959566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->offs, &oarray));
189692d50984SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
189792d50984SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
189892d50984SMatthew G. Knepley       const PetscInt point = parray[poff + p];
189992d50984SMatthew G. Knepley       PetscInt       dof;
190092d50984SMatthew G. Knepley 
19019566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(patch->patchSection, point, f, &dof));
19029566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff + p) * Nf + f]));
19039566063dSJacob Faibussowitsch       if (patch->nsubspaces == 1) PetscCall(PetscSectionSetOffset(patch->patchSection, point, oarray[(poff + p) * Nf + f]));
19049566063dSJacob Faibussowitsch       else PetscCall(PetscSectionSetOffset(patch->patchSection, point, -1));
190592d50984SMatthew G. Knepley     }
190692d50984SMatthew G. Knepley   }
19079566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->points, &parray));
19089566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->offs, &oarray));
19099566063dSJacob Faibussowitsch   if (patch->viewSection) PetscCall(ObjectView((PetscObject)patch->patchSection, patch->viewerSection, patch->formatSection));
19109566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeResidual_Patch_Internal(dm, patch->patchSection, cellIS, 0.0, x, NULL, F, ctx));
19119566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
19123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
191392d50984SMatthew G. Knepley }
191492d50984SMatthew G. Knepley 
1915d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchComputeFunction_Internal(PC pc, Vec x, Vec F, PetscInt point)
1916d71ae5a4SJacob Faibussowitsch {
191792d50984SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *)pc->data;
191892d50984SMatthew G. Knepley   const PetscInt *dofsArray;
19190904074fSPatrick Farrell   const PetscInt *dofsArrayWithAll;
192092d50984SMatthew G. Knepley   const PetscInt *cellsArray;
192192d50984SMatthew G. Knepley   PetscInt        ncell, offset, pStart, pEnd;
192292d50984SMatthew G. Knepley 
192392d50984SMatthew G. Knepley   PetscFunctionBegin;
19249566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0));
1925ef1023bdSBarry Smith   PetscCheck(patch->usercomputeop, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set callback");
19269566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->dofs, &dofsArray));
19279566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll));
19289566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->cells, &cellsArray));
19299566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd));
193092d50984SMatthew G. Knepley 
193192d50984SMatthew G. Knepley   point += pStart;
193263a3b9bcSJacob Faibussowitsch   PetscCheck(point < pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %" PetscInt_FMT " not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, pStart, pEnd);
193392d50984SMatthew G. Knepley 
19349566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetDof(patch->cellCounts, point, &ncell));
19359566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(patch->cellCounts, point, &offset));
193692d50984SMatthew G. Knepley   if (ncell <= 0) {
19379566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
19383ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
193992d50984SMatthew G. Knepley   }
19409566063dSJacob Faibussowitsch   PetscCall(VecSet(F, 0.0));
194192d50984SMatthew G. Knepley   /* Cannot reuse the same IS because the geometry info is being cached in it */
19429566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS));
1943792fecdfSBarry 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));
19449566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->cellIS));
19459566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->dofs, &dofsArray));
19469566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll));
19479566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->cells, &cellsArray));
194892d50984SMatthew G. Knepley   if (patch->viewMatrix) {
194992d50984SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
195092d50984SMatthew G. Knepley 
195163a3b9bcSJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN - 1, "Patch vector for Point %" PetscInt_FMT, point));
19529566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)F, name));
19539566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)F, patch->viewerMatrix, patch->formatMatrix));
195492d50984SMatthew G. Knepley   }
19559566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
19563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
195792d50984SMatthew G. Knepley }
195892d50984SMatthew G. Knepley 
1959d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchComputeOperator_DMPlex_Private(PC pc, PetscInt patchNum, Vec x, Mat J, IS cellIS, PetscInt n, const PetscInt *l2p, const PetscInt *l2pWithAll, void *ctx)
1960d71ae5a4SJacob Faibussowitsch {
19615f824522SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *)pc->data;
1962b6bb21d1SLawrence Mitchell   DM              dm, plex;
19635f824522SMatthew G. Knepley   PetscSection    s;
19645f824522SMatthew G. Knepley   const PetscInt *parray, *oarray;
19655f824522SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
19665f824522SMatthew G. Knepley 
19675f824522SMatthew G. Knepley   PetscFunctionBegin;
19689566063dSJacob Faibussowitsch   PetscCall(PCGetDM(pc, &dm));
19699566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
1970b6bb21d1SLawrence Mitchell   dm = plex;
19719566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
19725f824522SMatthew G. Knepley   /* Set offset into patch */
19739566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetDof(patch->pointCounts, patchNum, &Np));
19749566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(patch->pointCounts, patchNum, &poff));
19759566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->points, &parray));
19769566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->offs, &oarray));
19775f824522SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
19785f824522SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
19795f824522SMatthew G. Knepley       const PetscInt point = parray[poff + p];
19805f824522SMatthew G. Knepley       PetscInt       dof;
19815f824522SMatthew G. Knepley 
19829566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(patch->patchSection, point, f, &dof));
19839566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff + p) * Nf + f]));
19849566063dSJacob Faibussowitsch       if (patch->nsubspaces == 1) PetscCall(PetscSectionSetOffset(patch->patchSection, point, oarray[(poff + p) * Nf + f]));
19859566063dSJacob Faibussowitsch       else PetscCall(PetscSectionSetOffset(patch->patchSection, point, -1));
19865f824522SMatthew G. Knepley     }
19875f824522SMatthew G. Knepley   }
19889566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->points, &parray));
19899566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->offs, &oarray));
19909566063dSJacob Faibussowitsch   if (patch->viewSection) PetscCall(ObjectView((PetscObject)patch->patchSection, patch->viewerSection, patch->formatSection));
19915f824522SMatthew G. Knepley   /* TODO Shut off MatViewFromOptions() in MatAssemblyEnd() here */
19929566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeJacobian_Patch_Internal(dm, patch->patchSection, patch->patchSection, cellIS, 0.0, 0.0, x, NULL, J, J, ctx));
19939566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
19943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19955f824522SMatthew G. Knepley }
19965f824522SMatthew G. Knepley 
1997a685ae26SLawrence Mitchell /* This function zeros mat on entry */
1998d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchComputeOperator_Internal(PC pc, Vec x, Mat mat, PetscInt point, PetscBool withArtificial)
1999d71ae5a4SJacob Faibussowitsch {
20004bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *)pc->data;
20014bbf5ea8SMatthew G. Knepley   const PetscInt *dofsArray;
20020904074fSPatrick Farrell   const PetscInt *dofsArrayWithAll = NULL;
20034bbf5ea8SMatthew G. Knepley   const PetscInt *cellsArray;
2004eb62eeaaSLawrence Mitchell   PetscInt        ncell, offset, pStart, pEnd, numIntFacets, intFacetOffset;
20054d04e9f1SPatrick Farrell   PetscBool       isNonlinear;
20064bbf5ea8SMatthew G. Knepley 
20074bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
20089566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0));
2009debbdec3SPatrick Farrell   isNonlinear = patch->isNonlinear;
2010ef1023bdSBarry Smith   PetscCheck(patch->usercomputeop, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set callback");
2011c2e6f3c0SFlorian Wechsung   if (withArtificial) {
20129566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->dofsWithArtificial, &dofsArray));
2013c2e6f3c0SFlorian Wechsung   } else {
20149566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->dofs, &dofsArray));
2015c2e6f3c0SFlorian Wechsung   }
201648a46eb9SPierre Jolivet   if (isNonlinear) PetscCall(ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll));
20179566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->cells, &cellsArray));
20189566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd));
20194bbf5ea8SMatthew G. Knepley 
20204bbf5ea8SMatthew G. Knepley   point += pStart;
202163a3b9bcSJacob Faibussowitsch   PetscCheck(point < pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %" PetscInt_FMT " not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, pStart, pEnd);
20224bbf5ea8SMatthew G. Knepley 
20239566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetDof(patch->cellCounts, point, &ncell));
20249566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(patch->cellCounts, point, &offset));
20254bbf5ea8SMatthew G. Knepley   if (ncell <= 0) {
20269566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
20273ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
20284bbf5ea8SMatthew G. Knepley   }
20299566063dSJacob Faibussowitsch   PetscCall(MatZeroEntries(mat));
2030fa84ea4cSLawrence Mitchell   if (patch->precomputeElementTensors) {
2031fa84ea4cSLawrence Mitchell     PetscInt           i;
2032fa84ea4cSLawrence Mitchell     PetscInt           ndof = patch->totalDofsPerCell;
2033fa84ea4cSLawrence Mitchell     const PetscScalar *elementTensors;
2034fa84ea4cSLawrence Mitchell 
20359566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(patch->cellMats, &elementTensors));
2036fa84ea4cSLawrence Mitchell     for (i = 0; i < ncell; i++) {
2037fa84ea4cSLawrence Mitchell       const PetscInt     cell = cellsArray[i + offset];
2038fa84ea4cSLawrence Mitchell       const PetscInt    *idx  = dofsArray + (offset + i) * ndof;
2039fe988be2SFlorian Wechsung       const PetscScalar *v    = elementTensors + patch->precomputedTensorLocations[cell] * ndof * ndof;
20409566063dSJacob Faibussowitsch       PetscCall(MatSetValues(mat, ndof, idx, ndof, idx, v, ADD_VALUES));
2041fa84ea4cSLawrence Mitchell     }
20429566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(patch->cellMats, &elementTensors));
20439566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
20449566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
2045fa84ea4cSLawrence Mitchell   } else {
20462aa6f319SMatthew G. Knepley     /* Cannot reuse the same IS because the geometry info is being cached in it */
20479566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS));
20489371c9d4SSatish Balay     PetscCallBack("PCPatch callback",
20499371c9d4SSatish Balay                   patch->usercomputeop(pc, point, x, mat, patch->cellIS, ncell * patch->totalDofsPerCell, dofsArray + offset * patch->totalDofsPerCell, dofsArrayWithAll ? dofsArrayWithAll + offset * patch->totalDofsPerCell : NULL, patch->usercomputeopctx));
2050fa84ea4cSLawrence Mitchell   }
205159109abcSLawrence Mitchell   if (patch->usercomputeopintfacet) {
20529566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets));
20539566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset));
2054eb62eeaaSLawrence Mitchell     if (numIntFacets > 0) {
2055eb62eeaaSLawrence Mitchell       /* For each interior facet, grab the two cells (in local numbering, and concatenate dof numberings for those cells) */
2056eb62eeaaSLawrence Mitchell       PetscInt       *facetDofs = NULL, *facetDofsWithAll = NULL;
2057eb62eeaaSLawrence Mitchell       const PetscInt *intFacetsArray = NULL;
2058eb62eeaaSLawrence Mitchell       PetscInt        idx            = 0;
2059eb62eeaaSLawrence Mitchell       PetscInt        i, c, d;
2060de2d1767SPatrick Farrell       PetscInt        fStart;
2061b6bb21d1SLawrence Mitchell       DM              dm, plex;
2062eb62eeaaSLawrence Mitchell       IS              facetIS    = NULL;
2063eb62eeaaSLawrence Mitchell       const PetscInt *facetCells = NULL;
20647a50e09dSPatrick Farrell 
20659566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells));
20669566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray));
20679566063dSJacob Faibussowitsch       PetscCall(PCGetDM(pc, &dm));
20689566063dSJacob Faibussowitsch       PetscCall(DMConvert(dm, DMPLEX, &plex));
2069b6bb21d1SLawrence Mitchell       dm = plex;
20709566063dSJacob Faibussowitsch       PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, NULL));
2071eb62eeaaSLawrence Mitchell       /* FIXME: Pull this malloc out. */
20729566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofs));
207348a46eb9SPierre Jolivet       if (dofsArrayWithAll) PetscCall(PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofsWithAll));
2074f98464cbSLawrence Mitchell       if (patch->precomputeElementTensors) {
2075f98464cbSLawrence Mitchell         PetscInt           nFacetDof = 2 * patch->totalDofsPerCell;
2076f98464cbSLawrence Mitchell         const PetscScalar *elementTensors;
2077f98464cbSLawrence Mitchell 
20789566063dSJacob Faibussowitsch         PetscCall(VecGetArrayRead(patch->intFacetMats, &elementTensors));
2079f98464cbSLawrence Mitchell 
2080f98464cbSLawrence Mitchell         for (i = 0; i < numIntFacets; i++) {
2081f98464cbSLawrence Mitchell           const PetscInt     facet = intFacetsArray[i + intFacetOffset];
2082de2d1767SPatrick Farrell           const PetscScalar *v     = elementTensors + patch->precomputedIntFacetTensorLocations[facet - fStart] * nFacetDof * nFacetDof;
2083f98464cbSLawrence Mitchell           idx                      = 0;
2084f98464cbSLawrence Mitchell           /*
2085f1580f4eSBarry Smith      0--1
2086f1580f4eSBarry Smith      |\-|
2087f1580f4eSBarry Smith      |+\|
2088f1580f4eSBarry Smith      2--3
2089f1580f4eSBarry Smith      [0, 2, 3, 0, 1, 3]
2090f98464cbSLawrence Mitchell    */
2091f98464cbSLawrence Mitchell           for (c = 0; c < 2; c++) {
2092f98464cbSLawrence Mitchell             const PetscInt cell = facetCells[2 * (intFacetOffset + i) + c];
2093f98464cbSLawrence Mitchell             for (d = 0; d < patch->totalDofsPerCell; d++) {
2094f98464cbSLawrence Mitchell               facetDofs[idx] = dofsArray[(offset + cell) * patch->totalDofsPerCell + d];
2095f98464cbSLawrence Mitchell               idx++;
2096f98464cbSLawrence Mitchell             }
2097f98464cbSLawrence Mitchell           }
20989566063dSJacob Faibussowitsch           PetscCall(MatSetValues(mat, nFacetDof, facetDofs, nFacetDof, facetDofs, v, ADD_VALUES));
2099f98464cbSLawrence Mitchell         }
21009566063dSJacob Faibussowitsch         PetscCall(VecRestoreArrayRead(patch->intFacetMats, &elementTensors));
2101f98464cbSLawrence Mitchell       } else {
2102eb62eeaaSLawrence Mitchell         /*
2103f1580f4eSBarry Smith      0--1
2104f1580f4eSBarry Smith      |\-|
2105f1580f4eSBarry Smith      |+\|
2106f1580f4eSBarry Smith      2--3
2107f1580f4eSBarry Smith      [0, 2, 3, 0, 1, 3]
2108eb62eeaaSLawrence Mitchell    */
2109eb62eeaaSLawrence Mitchell         for (i = 0; i < numIntFacets; i++) {
2110eb62eeaaSLawrence Mitchell           for (c = 0; c < 2; c++) {
2111eb62eeaaSLawrence Mitchell             const PetscInt cell = facetCells[2 * (intFacetOffset + i) + c];
2112eb62eeaaSLawrence Mitchell             for (d = 0; d < patch->totalDofsPerCell; d++) {
2113eb62eeaaSLawrence Mitchell               facetDofs[idx] = dofsArray[(offset + cell) * patch->totalDofsPerCell + d];
2114ad540459SPierre Jolivet               if (dofsArrayWithAll) facetDofsWithAll[idx] = dofsArrayWithAll[(offset + cell) * patch->totalDofsPerCell + d];
2115eb62eeaaSLawrence Mitchell               idx++;
2116eb62eeaaSLawrence Mitchell             }
2117eb62eeaaSLawrence Mitchell           }
2118eb62eeaaSLawrence Mitchell         }
21199566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray + intFacetOffset, PETSC_USE_POINTER, &facetIS));
21209566063dSJacob Faibussowitsch         PetscCall(patch->usercomputeopintfacet(pc, point, x, mat, facetIS, 2 * numIntFacets * patch->totalDofsPerCell, facetDofs, facetDofsWithAll, patch->usercomputeopintfacetctx));
21219566063dSJacob Faibussowitsch         PetscCall(ISDestroy(&facetIS));
2122f98464cbSLawrence Mitchell       }
21239566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(patch->intFacetsToPatchCell, &facetCells));
21249566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(patch->intFacets, &intFacetsArray));
21259566063dSJacob Faibussowitsch       PetscCall(PetscFree(facetDofs));
21269566063dSJacob Faibussowitsch       PetscCall(PetscFree(facetDofsWithAll));
21279566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dm));
2128eb62eeaaSLawrence Mitchell     }
212959109abcSLawrence Mitchell   }
21306710cc29SPatrick Farrell 
21319566063dSJacob Faibussowitsch   PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
21329566063dSJacob Faibussowitsch   PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
21336710cc29SPatrick Farrell 
2134c73d2cf6SLawrence Mitchell   if (!(withArtificial || isNonlinear) && patch->denseinverse) {
2135c73d2cf6SLawrence Mitchell     MatFactorInfo info;
2136c73d2cf6SLawrence Mitchell     PetscBool     flg;
21379566063dSJacob Faibussowitsch     PetscCall(PetscObjectTypeCompare((PetscObject)mat, MATSEQDENSE, &flg));
213828b400f6SJacob Faibussowitsch     PetscCheck(flg, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Invalid Mat type for dense inverse");
21399566063dSJacob Faibussowitsch     PetscCall(MatFactorInfoInitialize(&info));
21409566063dSJacob Faibussowitsch     PetscCall(MatLUFactor(mat, NULL, NULL, &info));
21419566063dSJacob Faibussowitsch     PetscCall(MatSeqDenseInvertFactors_Private(mat));
2142c73d2cf6SLawrence Mitchell   }
21439566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->cellIS));
21444d04e9f1SPatrick Farrell   if (withArtificial) {
21459566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(patch->dofsWithArtificial, &dofsArray));
2146c2e6f3c0SFlorian Wechsung   } else {
21479566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(patch->dofs, &dofsArray));
2148c2e6f3c0SFlorian Wechsung   }
214948a46eb9SPierre Jolivet   if (isNonlinear) PetscCall(ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll));
21509566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->cells, &cellsArray));
21512aa6f319SMatthew G. Knepley   if (patch->viewMatrix) {
21522aa6f319SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
21532aa6f319SMatthew G. Knepley 
215463a3b9bcSJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN - 1, "Patch matrix for Point %" PetscInt_FMT, point));
21559566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)mat, name));
21569566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)mat, patch->viewerMatrix, patch->formatMatrix));
21572aa6f319SMatthew G. Knepley   }
21589566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
21593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21604bbf5ea8SMatthew G. Knepley }
21614bbf5ea8SMatthew G. Knepley 
2162d71ae5a4SJacob Faibussowitsch static PetscErrorCode MatSetValues_PCPatch_Private(Mat mat, PetscInt m, const PetscInt idxm[], PetscInt n, const PetscInt idxn[], const PetscScalar *v, InsertMode addv)
2163d71ae5a4SJacob Faibussowitsch {
2164fa84ea4cSLawrence Mitchell   Vec          data;
2165fa84ea4cSLawrence Mitchell   PetscScalar *array;
2166fe988be2SFlorian Wechsung   PetscInt     bs, nz, i, j, cell;
2167fa84ea4cSLawrence Mitchell 
21689566063dSJacob Faibussowitsch   PetscCall(MatShellGetContext(mat, &data));
21699566063dSJacob Faibussowitsch   PetscCall(VecGetBlockSize(data, &bs));
21709566063dSJacob Faibussowitsch   PetscCall(VecGetSize(data, &nz));
21719566063dSJacob Faibussowitsch   PetscCall(VecGetArray(data, &array));
217208401ef6SPierre Jolivet   PetscCheck(m == n, PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Only for square insertion");
217333cbca70SPatrick Farrell   cell = (PetscInt)(idxm[0] / bs); /* use the fact that this is called once per cell */
2174fa84ea4cSLawrence Mitchell   for (i = 0; i < m; i++) {
217508401ef6SPierre Jolivet     PetscCheck(idxm[i] == idxn[i], PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Row and column indices must match!");
2176fa84ea4cSLawrence Mitchell     for (j = 0; j < n; j++) {
2177fa84ea4cSLawrence Mitchell       const PetscScalar v_ = v[i * bs + j];
2178fa84ea4cSLawrence Mitchell       /* Indexing is special to the data structure we have! */
2179fa84ea4cSLawrence Mitchell       if (addv == INSERT_VALUES) {
2180fe988be2SFlorian Wechsung         array[cell * bs * bs + i * bs + j] = v_;
2181fa84ea4cSLawrence Mitchell       } else {
2182fe988be2SFlorian Wechsung         array[cell * bs * bs + i * bs + j] += v_;
2183fa84ea4cSLawrence Mitchell       }
2184fa84ea4cSLawrence Mitchell     }
2185fa84ea4cSLawrence Mitchell   }
21869566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(data, &array));
21873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2188fa84ea4cSLawrence Mitchell }
2189fa84ea4cSLawrence Mitchell 
2190d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchPrecomputePatchTensors_Private(PC pc)
2191d71ae5a4SJacob Faibussowitsch {
2192fa84ea4cSLawrence Mitchell   PC_PATCH       *patch = (PC_PATCH *)pc->data;
2193fa84ea4cSLawrence Mitchell   const PetscInt *cellsArray;
2194fa84ea4cSLawrence Mitchell   PetscInt        ncell, offset;
2195fa84ea4cSLawrence Mitchell   const PetscInt *dofMapArray;
2196fa84ea4cSLawrence Mitchell   PetscInt        i, j;
2197fa84ea4cSLawrence Mitchell   IS              dofMap;
2198fa84ea4cSLawrence Mitchell   IS              cellIS;
2199fa84ea4cSLawrence Mitchell   const PetscInt  ndof = patch->totalDofsPerCell;
2200fa84ea4cSLawrence Mitchell   Mat             vecMat;
2201fe988be2SFlorian Wechsung   PetscInt        cStart, cEnd;
2202fe988be2SFlorian Wechsung   DM              dm, plex;
2203fe988be2SFlorian Wechsung 
22049566063dSJacob Faibussowitsch   PetscCall(ISGetSize(patch->cells, &ncell));
2205e9c2c94bSFlorian Wechsung   if (!ncell) { /* No cells to assemble over -> skip */
22063ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2207e9c2c94bSFlorian Wechsung   }
2208e9c2c94bSFlorian Wechsung 
22099566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0));
2210fa84ea4cSLawrence Mitchell 
22119566063dSJacob Faibussowitsch   PetscCall(PCGetDM(pc, &dm));
22129566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
2213b6bb21d1SLawrence Mitchell   dm = plex;
2214fa84ea4cSLawrence Mitchell   if (!patch->allCells) {
2215fa84ea4cSLawrence Mitchell     PetscHSetI    cells;
2216fa84ea4cSLawrence Mitchell     PetscHashIter hi;
2217fa84ea4cSLawrence Mitchell     PetscInt      pStart, pEnd;
2218fa84ea4cSLawrence Mitchell     PetscInt     *allCells = NULL;
22199566063dSJacob Faibussowitsch     PetscCall(PetscHSetICreate(&cells));
22209566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->cells, &cellsArray));
22219566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd));
2222fa84ea4cSLawrence Mitchell     for (i = pStart; i < pEnd; i++) {
22239566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(patch->cellCounts, i, &ncell));
22249566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(patch->cellCounts, i, &offset));
2225fa84ea4cSLawrence Mitchell       if (ncell <= 0) continue;
222648a46eb9SPierre Jolivet       for (j = 0; j < ncell; j++) PetscCall(PetscHSetIAdd(cells, cellsArray[offset + j]));
2227fa84ea4cSLawrence Mitchell     }
22289566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(patch->cells, &cellsArray));
22299566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(cells, &ncell));
22309566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(ncell, &allCells));
22319566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
22329566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(cEnd - cStart, &patch->precomputedTensorLocations));
2233fa84ea4cSLawrence Mitchell     i = 0;
2234fa84ea4cSLawrence Mitchell     PetscHashIterBegin(cells, hi);
2235fa84ea4cSLawrence Mitchell     while (!PetscHashIterAtEnd(cells, hi)) {
2236fe988be2SFlorian Wechsung       PetscHashIterGetKey(cells, hi, allCells[i]);
2237fe988be2SFlorian Wechsung       patch->precomputedTensorLocations[allCells[i]] = i;
2238fa84ea4cSLawrence Mitchell       PetscHashIterNext(cells, hi);
2239fe988be2SFlorian Wechsung       i++;
2240fa84ea4cSLawrence Mitchell     }
22419566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&cells));
22429566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, allCells, PETSC_OWN_POINTER, &patch->allCells));
2243fa84ea4cSLawrence Mitchell   }
22449566063dSJacob Faibussowitsch   PetscCall(ISGetSize(patch->allCells, &ncell));
2245fa84ea4cSLawrence Mitchell   if (!patch->cellMats) {
22469566063dSJacob Faibussowitsch     PetscCall(VecCreateSeq(PETSC_COMM_SELF, ncell * ndof * ndof, &patch->cellMats));
22479566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(patch->cellMats, ndof));
2248fa84ea4cSLawrence Mitchell   }
22499566063dSJacob Faibussowitsch   PetscCall(VecSet(patch->cellMats, 0));
2250fa84ea4cSLawrence Mitchell 
2251d0609cedSBarry Smith   PetscCall(MatCreateShell(PETSC_COMM_SELF, ncell * ndof, ncell * ndof, ncell * ndof, ncell * ndof, (void *)patch->cellMats, &vecMat));
22529566063dSJacob Faibussowitsch   PetscCall(MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void (*)(void)) & MatSetValues_PCPatch_Private));
22539566063dSJacob Faibussowitsch   PetscCall(ISGetSize(patch->allCells, &ncell));
22549566063dSJacob Faibussowitsch   PetscCall(ISCreateStride(PETSC_COMM_SELF, ndof * ncell, 0, 1, &dofMap));
22559566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(dofMap, &dofMapArray));
22569566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->allCells, &cellsArray));
22579566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray, PETSC_USE_POINTER, &cellIS));
2258fa84ea4cSLawrence Mitchell   /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */
2259792fecdfSBarry Smith   PetscCallBack("PCPatch callback", patch->usercomputeop(pc, -1, NULL, vecMat, cellIS, ndof * ncell, dofMapArray, NULL, patch->usercomputeopctx));
22609566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&cellIS));
22619566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&vecMat));
22629566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->allCells, &cellsArray));
22639566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(dofMap, &dofMapArray));
22649566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&dofMap));
2265f98464cbSLawrence Mitchell 
2266f98464cbSLawrence Mitchell   if (patch->usercomputeopintfacet) {
2267f98464cbSLawrence Mitchell     PetscInt        nIntFacets;
2268f98464cbSLawrence Mitchell     IS              intFacetsIS;
2269f98464cbSLawrence Mitchell     const PetscInt *intFacetsArray = NULL;
2270f98464cbSLawrence Mitchell     if (!patch->allIntFacets) {
2271f98464cbSLawrence Mitchell       PetscHSetI    facets;
2272f98464cbSLawrence Mitchell       PetscHashIter hi;
2273f98464cbSLawrence Mitchell       PetscInt      pStart, pEnd, fStart, fEnd;
2274f98464cbSLawrence Mitchell       PetscInt     *allIntFacets = NULL;
22759566063dSJacob Faibussowitsch       PetscCall(PetscHSetICreate(&facets));
22769566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray));
22779566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(patch->intFacetCounts, &pStart, &pEnd));
22789566063dSJacob Faibussowitsch       PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
2279f98464cbSLawrence Mitchell       for (i = pStart; i < pEnd; i++) {
22809566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->intFacetCounts, i, &nIntFacets));
22819566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->intFacetCounts, i, &offset));
2282f98464cbSLawrence Mitchell         if (nIntFacets <= 0) continue;
228348a46eb9SPierre Jolivet         for (j = 0; j < nIntFacets; j++) PetscCall(PetscHSetIAdd(facets, intFacetsArray[offset + j]));
2284f98464cbSLawrence Mitchell       }
22859566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(patch->intFacets, &intFacetsArray));
22869566063dSJacob Faibussowitsch       PetscCall(PetscHSetIGetSize(facets, &nIntFacets));
22879566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nIntFacets, &allIntFacets));
22889566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fEnd - fStart, &patch->precomputedIntFacetTensorLocations));
2289f98464cbSLawrence Mitchell       i = 0;
2290f98464cbSLawrence Mitchell       PetscHashIterBegin(facets, hi);
2291f98464cbSLawrence Mitchell       while (!PetscHashIterAtEnd(facets, hi)) {
2292f98464cbSLawrence Mitchell         PetscHashIterGetKey(facets, hi, allIntFacets[i]);
2293de2d1767SPatrick Farrell         patch->precomputedIntFacetTensorLocations[allIntFacets[i] - fStart] = i;
2294f98464cbSLawrence Mitchell         PetscHashIterNext(facets, hi);
2295f98464cbSLawrence Mitchell         i++;
2296f98464cbSLawrence Mitchell       }
22979566063dSJacob Faibussowitsch       PetscCall(PetscHSetIDestroy(&facets));
22989566063dSJacob Faibussowitsch       PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, allIntFacets, PETSC_OWN_POINTER, &patch->allIntFacets));
2299f98464cbSLawrence Mitchell     }
23009566063dSJacob Faibussowitsch     PetscCall(ISGetSize(patch->allIntFacets, &nIntFacets));
2301f98464cbSLawrence Mitchell     if (!patch->intFacetMats) {
23029566063dSJacob Faibussowitsch       PetscCall(VecCreateSeq(PETSC_COMM_SELF, nIntFacets * ndof * ndof * 4, &patch->intFacetMats));
23039566063dSJacob Faibussowitsch       PetscCall(VecSetBlockSize(patch->intFacetMats, ndof * 2));
2304f98464cbSLawrence Mitchell     }
23059566063dSJacob Faibussowitsch     PetscCall(VecSet(patch->intFacetMats, 0));
2306f98464cbSLawrence Mitchell 
2307d0609cedSBarry Smith     PetscCall(MatCreateShell(PETSC_COMM_SELF, nIntFacets * ndof * 2, nIntFacets * ndof * 2, nIntFacets * ndof * 2, nIntFacets * ndof * 2, (void *)patch->intFacetMats, &vecMat));
23089566063dSJacob Faibussowitsch     PetscCall(MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void (*)(void)) & MatSetValues_PCPatch_Private));
23099566063dSJacob Faibussowitsch     PetscCall(ISCreateStride(PETSC_COMM_SELF, 2 * ndof * nIntFacets, 0, 1, &dofMap));
23109566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(dofMap, &dofMapArray));
23119566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->allIntFacets, &intFacetsArray));
23129566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, intFacetsArray, PETSC_USE_POINTER, &intFacetsIS));
2313f98464cbSLawrence Mitchell     /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */
2314792fecdfSBarry Smith     PetscCallBack("PCPatch callback (interior facets)", patch->usercomputeopintfacet(pc, -1, NULL, vecMat, intFacetsIS, 2 * ndof * nIntFacets, dofMapArray, NULL, patch->usercomputeopintfacetctx));
23159566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&intFacetsIS));
23169566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&vecMat));
23179566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(patch->allIntFacets, &intFacetsArray));
23189566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(dofMap, &dofMapArray));
23199566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dofMap));
2320f98464cbSLawrence Mitchell   }
23219566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
23229566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
23233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2324fa84ea4cSLawrence Mitchell }
2325fa84ea4cSLawrence Mitchell 
2326d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatch_ScatterLocal_Private(PC pc, PetscInt p, Vec x, Vec y, InsertMode mode, ScatterMode scat, PatchScatterType scattertype)
2327d71ae5a4SJacob Faibussowitsch {
23284bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch     = (PC_PATCH *)pc->data;
23294bbf5ea8SMatthew G. Knepley   const PetscScalar *xArray    = NULL;
23304bbf5ea8SMatthew G. Knepley   PetscScalar       *yArray    = NULL;
23314bbf5ea8SMatthew G. Knepley   const PetscInt    *gtolArray = NULL;
23324bbf5ea8SMatthew G. Knepley   PetscInt           dof, offset, lidx;
23334bbf5ea8SMatthew G. Knepley 
23344bbf5ea8SMatthew G. Knepley   PetscFunctionBeginHot;
23359566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(x, &xArray));
23369566063dSJacob Faibussowitsch   PetscCall(VecGetArray(y, &yArray));
23370904074fSPatrick Farrell   if (scattertype == SCATTER_WITHARTIFICIAL) {
23389566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof));
23399566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offset));
23409566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->gtolWithArtificial, &gtolArray));
23410904074fSPatrick Farrell   } else if (scattertype == SCATTER_WITHALL) {
23429566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(patch->gtolCountsWithAll, p, &dof));
23439566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offset));
23449566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->gtolWithAll, &gtolArray));
2345c2e6f3c0SFlorian Wechsung   } else {
23469566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &dof));
23479566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset));
23489566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->gtol, &gtolArray));
2349c2e6f3c0SFlorian Wechsung   }
23502472a847SBarry Smith   PetscCheck(mode != INSERT_VALUES || scat == SCATTER_FORWARD, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't insert if not scattering forward");
23512472a847SBarry Smith   PetscCheck(mode != ADD_VALUES || scat == SCATTER_REVERSE, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't add if not scattering reverse");
23524bbf5ea8SMatthew G. Knepley   for (lidx = 0; lidx < dof; ++lidx) {
23534bbf5ea8SMatthew G. Knepley     const PetscInt gidx = gtolArray[offset + lidx];
23544bbf5ea8SMatthew G. Knepley 
23554bbf5ea8SMatthew G. Knepley     if (mode == INSERT_VALUES) yArray[lidx] = xArray[gidx]; /* Forward */
23564bbf5ea8SMatthew G. Knepley     else yArray[gidx] += xArray[lidx];                      /* Reverse */
23574bbf5ea8SMatthew G. Knepley   }
23580904074fSPatrick Farrell   if (scattertype == SCATTER_WITHARTIFICIAL) {
23599566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(patch->gtolWithArtificial, &gtolArray));
23600904074fSPatrick Farrell   } else if (scattertype == SCATTER_WITHALL) {
23619566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(patch->gtolWithAll, &gtolArray));
2362c2e6f3c0SFlorian Wechsung   } else {
23639566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(patch->gtol, &gtolArray));
2364c2e6f3c0SFlorian Wechsung   }
23659566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(x, &xArray));
23669566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(y, &yArray));
23673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
23684bbf5ea8SMatthew G. Knepley }
23694bbf5ea8SMatthew G. Knepley 
2370d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_PATCH_Linear(PC pc)
2371d71ae5a4SJacob Faibussowitsch {
2372dadc69c5SMatthew G. Knepley   PC_PATCH   *patch = (PC_PATCH *)pc->data;
2373dadc69c5SMatthew G. Knepley   const char *prefix;
2374dadc69c5SMatthew G. Knepley   PetscInt    i;
2375dadc69c5SMatthew G. Knepley 
2376dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2377dadc69c5SMatthew G. Knepley   if (!pc->setupcalled) {
23787827d75bSBarry Smith     PetscCheck(patch->save_operators || !patch->denseinverse, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Can't have dense inverse without save operators");
2379c73d2cf6SLawrence Mitchell     if (!patch->denseinverse) {
23809566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(patch->npatch, &patch->solver));
23819566063dSJacob Faibussowitsch       PetscCall(PCGetOptionsPrefix(pc, &prefix));
2382dadc69c5SMatthew G. Knepley       for (i = 0; i < patch->npatch; ++i) {
2383dadc69c5SMatthew G. Knepley         KSP ksp;
2384dadc69c5SMatthew G. Knepley         PC  subpc;
2385dadc69c5SMatthew G. Knepley 
23869566063dSJacob Faibussowitsch         PetscCall(KSPCreate(PETSC_COMM_SELF, &ksp));
23873821be0aSBarry Smith         PetscCall(KSPSetNestLevel(ksp, pc->kspnestlevel));
23889566063dSJacob Faibussowitsch         PetscCall(KSPSetErrorIfNotConverged(ksp, pc->erroriffailure));
23899566063dSJacob Faibussowitsch         PetscCall(KSPSetOptionsPrefix(ksp, prefix));
23909566063dSJacob Faibussowitsch         PetscCall(KSPAppendOptionsPrefix(ksp, "sub_"));
23919566063dSJacob Faibussowitsch         PetscCall(PetscObjectIncrementTabLevel((PetscObject)ksp, (PetscObject)pc, 1));
23929566063dSJacob Faibussowitsch         PetscCall(KSPGetPC(ksp, &subpc));
23939566063dSJacob Faibussowitsch         PetscCall(PetscObjectIncrementTabLevel((PetscObject)subpc, (PetscObject)pc, 1));
2394dadc69c5SMatthew G. Knepley         patch->solver[i] = (PetscObject)ksp;
2395dadc69c5SMatthew G. Knepley       }
2396dadc69c5SMatthew G. Knepley     }
2397c73d2cf6SLawrence Mitchell   }
2398dadc69c5SMatthew G. Knepley   if (patch->save_operators) {
23991baa6e33SBarry Smith     if (patch->precomputeElementTensors) PetscCall(PCPatchPrecomputePatchTensors_Private(pc));
2400dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {
24019566063dSJacob Faibussowitsch       PetscCall(PCPatchComputeOperator_Internal(pc, NULL, patch->mat[i], i, PETSC_FALSE));
2402c73d2cf6SLawrence Mitchell       if (!patch->denseinverse) {
24039566063dSJacob Faibussowitsch         PetscCall(KSPSetOperators((KSP)patch->solver[i], patch->mat[i], patch->mat[i]));
24049d4fc724SLawrence Mitchell       } else if (patch->mat[i] && !patch->densesolve) {
24059d4fc724SLawrence Mitchell         /* Setup matmult callback */
24069566063dSJacob Faibussowitsch         PetscCall(MatGetOperation(patch->mat[i], MATOP_MULT, (void (**)(void)) & patch->densesolve));
2407dadc69c5SMatthew G. Knepley       }
2408dadc69c5SMatthew G. Knepley     }
2409c73d2cf6SLawrence Mitchell   }
241034d8b122SPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
241134d8b122SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {
24121202d238SPatrick Farrell       /* Instead of padding patch->patchUpdate with zeros to get */
24131202d238SPatrick Farrell       /* patch->patchUpdateWithArtificial and then multiplying with the matrix, */
241434d8b122SPatrick Farrell       /* just get rid of the columns that correspond to the dofs with */
241534d8b122SPatrick Farrell       /* artificial bcs. That's of course fairly inefficient, hopefully we */
241634d8b122SPatrick Farrell       /* can just assemble the rectangular matrix in the first place. */
241734d8b122SPatrick Farrell       Mat      matSquare;
241834d8b122SPatrick Farrell       IS       rowis;
241934d8b122SPatrick Farrell       PetscInt dof;
242034d8b122SPatrick Farrell 
24219566063dSJacob Faibussowitsch       PetscCall(MatGetSize(patch->mat[i], &dof, NULL));
242234d8b122SPatrick Farrell       if (dof == 0) {
242334d8b122SPatrick Farrell         patch->matWithArtificial[i] = NULL;
242434d8b122SPatrick Farrell         continue;
242534d8b122SPatrick Farrell       }
242634d8b122SPatrick Farrell 
24279566063dSJacob Faibussowitsch       PetscCall(PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE));
24289566063dSJacob Faibussowitsch       PetscCall(PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE));
242934d8b122SPatrick Farrell 
24309566063dSJacob Faibussowitsch       PetscCall(MatGetSize(matSquare, &dof, NULL));
24319566063dSJacob Faibussowitsch       PetscCall(ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis));
243234d8b122SPatrick Farrell       if (pc->setupcalled) {
24339566063dSJacob Faibussowitsch         PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_REUSE_MATRIX, &patch->matWithArtificial[i]));
243434d8b122SPatrick Farrell       } else {
24359566063dSJacob Faibussowitsch         PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &patch->matWithArtificial[i]));
243634d8b122SPatrick Farrell       }
24379566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&rowis));
24389566063dSJacob Faibussowitsch       PetscCall(MatDestroy(&matSquare));
243934d8b122SPatrick Farrell     }
244034d8b122SPatrick Farrell   }
24413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2442dadc69c5SMatthew G. Knepley }
2443dadc69c5SMatthew G. Knepley 
2444d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_PATCH(PC pc)
2445d71ae5a4SJacob Faibussowitsch {
24464bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
2447557beb66SLawrence Mitchell   PetscInt  i;
244839fd2e8aSPatrick Farrell   PetscBool isNonlinear;
24499d4fc724SLawrence Mitchell   PetscInt  maxDof = -1, maxDofWithArtificial = -1;
24504bbf5ea8SMatthew G. Knepley 
24514bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
24524bbf5ea8SMatthew G. Knepley   if (!pc->setupcalled) {
24534bbf5ea8SMatthew G. Knepley     PetscInt pStart, pEnd, p;
24544bbf5ea8SMatthew G. Knepley     PetscInt localSize;
24554bbf5ea8SMatthew G. Knepley 
24569566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(PC_Patch_CreatePatches, pc, 0, 0, 0));
24574bbf5ea8SMatthew G. Knepley 
2458debbdec3SPatrick Farrell     isNonlinear = patch->isNonlinear;
24595f824522SMatthew G. Knepley     if (!patch->nsubspaces) {
2460b6bb21d1SLawrence Mitchell       DM           dm, plex;
24615f824522SMatthew G. Knepley       PetscSection s;
2462bd026e97SJed Brown       PetscInt     cStart, cEnd, c, Nf, f, numGlobalBcs = 0, *globalBcs, *Nb, **cellDofs;
24635f824522SMatthew G. Knepley 
24649566063dSJacob Faibussowitsch       PetscCall(PCGetDM(pc, &dm));
246528b400f6SJacob Faibussowitsch       PetscCheck(dm, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Must set DM for PCPATCH or call PCPatchSetDiscretisationInfo()");
24669566063dSJacob Faibussowitsch       PetscCall(DMConvert(dm, DMPLEX, &plex));
2467b6bb21d1SLawrence Mitchell       dm = plex;
24689566063dSJacob Faibussowitsch       PetscCall(DMGetLocalSection(dm, &s));
24699566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetNumFields(s, &Nf));
24709566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
24715f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
24725f824522SMatthew G. Knepley         PetscInt cdof;
24739566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
24745f824522SMatthew G. Knepley         numGlobalBcs += cdof;
24755f824522SMatthew G. Knepley       }
24769566063dSJacob Faibussowitsch       PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
24779566063dSJacob Faibussowitsch       PetscCall(PetscMalloc3(Nf, &Nb, Nf, &cellDofs, numGlobalBcs, &globalBcs));
24785f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
24795f824522SMatthew G. Knepley         PetscFE        fe;
24805f824522SMatthew G. Knepley         PetscDualSpace sp;
24815f824522SMatthew G. Knepley         PetscInt       cdoff = 0;
24825f824522SMatthew G. Knepley 
24839566063dSJacob Faibussowitsch         PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
24849566063dSJacob Faibussowitsch         /* PetscCall(PetscFEGetNumComponents(fe, &Nc[f])); */
24859566063dSJacob Faibussowitsch         PetscCall(PetscFEGetDualSpace(fe, &sp));
24869566063dSJacob Faibussowitsch         PetscCall(PetscDualSpaceGetDimension(sp, &Nb[f]));
24875f824522SMatthew G. Knepley 
24889566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1((cEnd - cStart) * Nb[f], &cellDofs[f]));
24895f824522SMatthew G. Knepley         for (c = cStart; c < cEnd; ++c) {
24905f824522SMatthew G. Knepley           PetscInt *closure = NULL;
24915f824522SMatthew G. Knepley           PetscInt  clSize  = 0, cl;
24925f824522SMatthew G. Knepley 
24939566063dSJacob Faibussowitsch           PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure));
24945f824522SMatthew G. Knepley           for (cl = 0; cl < clSize * 2; cl += 2) {
24955f824522SMatthew G. Knepley             const PetscInt p = closure[cl];
24965f824522SMatthew G. Knepley             PetscInt       fdof, d, foff;
24975f824522SMatthew G. Knepley 
24989566063dSJacob Faibussowitsch             PetscCall(PetscSectionGetFieldDof(s, p, f, &fdof));
24999566063dSJacob Faibussowitsch             PetscCall(PetscSectionGetFieldOffset(s, p, f, &foff));
25005f824522SMatthew G. Knepley             for (d = 0; d < fdof; ++d, ++cdoff) cellDofs[f][cdoff] = foff + d;
25015f824522SMatthew G. Knepley           }
25029566063dSJacob Faibussowitsch           PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure));
25035f824522SMatthew G. Knepley         }
250463a3b9bcSJacob 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]);
25055f824522SMatthew G. Knepley       }
25065f824522SMatthew G. Knepley       numGlobalBcs = 0;
25075f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
25085f824522SMatthew G. Knepley         const PetscInt *ind;
25095f824522SMatthew G. Knepley         PetscInt        off, cdof, d;
25105f824522SMatthew G. Knepley 
25119566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
25129566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
25139566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(s, p, &ind));
25145f824522SMatthew G. Knepley         for (d = 0; d < cdof; ++d) globalBcs[numGlobalBcs++] = off + ind[d];
25155f824522SMatthew G. Knepley       }
25165f824522SMatthew G. Knepley 
25179566063dSJacob Faibussowitsch       PetscCall(PCPatchSetDiscretisationInfoCombined(pc, dm, Nb, (const PetscInt **)cellDofs, numGlobalBcs, globalBcs, numGlobalBcs, globalBcs));
251848a46eb9SPierre Jolivet       for (f = 0; f < Nf; ++f) PetscCall(PetscFree(cellDofs[f]));
25199566063dSJacob Faibussowitsch       PetscCall(PetscFree3(Nb, cellDofs, globalBcs));
25209566063dSJacob Faibussowitsch       PetscCall(PCPatchSetComputeFunction(pc, PCPatchComputeFunction_DMPlex_Private, NULL));
25219566063dSJacob Faibussowitsch       PetscCall(PCPatchSetComputeOperator(pc, PCPatchComputeOperator_DMPlex_Private, NULL));
25229566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dm));
25235f824522SMatthew G. Knepley     }
25245f824522SMatthew G. Knepley 
25254bbf5ea8SMatthew G. Knepley     localSize = patch->subspaceOffsets[patch->nsubspaces];
25269566063dSJacob Faibussowitsch     PetscCall(VecCreateSeq(PETSC_COMM_SELF, localSize, &patch->localRHS));
25279566063dSJacob Faibussowitsch     PetscCall(VecSetUp(patch->localRHS));
25289566063dSJacob Faibussowitsch     PetscCall(VecDuplicate(patch->localRHS, &patch->localUpdate));
25299566063dSJacob Faibussowitsch     PetscCall(PCPatchCreateCellPatches(pc));
25309566063dSJacob Faibussowitsch     PetscCall(PCPatchCreateCellPatchDiscretisationInfo(pc));
25314bbf5ea8SMatthew G. Knepley 
25324bbf5ea8SMatthew G. Knepley     /* OK, now build the work vectors */
25339566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, &pEnd));
2534c2e6f3c0SFlorian Wechsung 
253548a46eb9SPierre Jolivet     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) PetscCall(PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithArtificial));
253648a46eb9SPierre Jolivet     if (isNonlinear) PetscCall(PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithAll));
25374bbf5ea8SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
25384bbf5ea8SMatthew G. Knepley       PetscInt dof;
25394bbf5ea8SMatthew G. Knepley 
25409566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &dof));
25412f613bf5SBarry Smith       maxDof = PetscMax(maxDof, dof);
25420904074fSPatrick Farrell       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
25433bb0e8f7SKarl Rupp         const PetscInt *gtolArray, *gtolArrayWithArtificial = NULL;
25443bb0e8f7SKarl Rupp         PetscInt        numPatchDofs, offset;
25453bb0e8f7SKarl Rupp         PetscInt        numPatchDofsWithArtificial, offsetWithArtificial;
25463bb0e8f7SKarl Rupp         PetscInt        dofWithoutArtificialCounter = 0;
25473bb0e8f7SKarl Rupp         PetscInt       *patchWithoutArtificialToWithArtificialArray;
25483bb0e8f7SKarl Rupp 
25499566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof));
25509d4fc724SLawrence Mitchell         maxDofWithArtificial = PetscMax(maxDofWithArtificial, dof);
2551c2e6f3c0SFlorian Wechsung 
2552e047a90bSFlorian Wechsung         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
2553e047a90bSFlorian Wechsung         /* the index in the patch with all dofs */
25549566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->gtol, &gtolArray));
255563deea8eSPatrick Farrell 
25569566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs));
255747aca4a6SPatrick Farrell         if (numPatchDofs == 0) {
255847aca4a6SPatrick Farrell           patch->dofMappingWithoutToWithArtificial[p - pStart] = NULL;
255947aca4a6SPatrick Farrell           continue;
256047aca4a6SPatrick Farrell         }
256163deea8eSPatrick Farrell 
25629566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset));
25639566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial));
25649566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &numPatchDofsWithArtificial));
25659566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offsetWithArtificial));
2566c2e6f3c0SFlorian Wechsung 
25679566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(numPatchDofs, &patchWithoutArtificialToWithArtificialArray));
2568b0c21b6aSKarl Rupp         for (i = 0; i < numPatchDofsWithArtificial; i++) {
2569e047a90bSFlorian Wechsung           if (gtolArrayWithArtificial[i + offsetWithArtificial] == gtolArray[offset + dofWithoutArtificialCounter]) {
2570c2e6f3c0SFlorian Wechsung             patchWithoutArtificialToWithArtificialArray[dofWithoutArtificialCounter] = i;
2571c2e6f3c0SFlorian Wechsung             dofWithoutArtificialCounter++;
25729371c9d4SSatish Balay             if (dofWithoutArtificialCounter == numPatchDofs) break;
2573c2e6f3c0SFlorian Wechsung           }
2574c2e6f3c0SFlorian Wechsung         }
25759566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutArtificialToWithArtificialArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithArtificial[p - pStart]));
25769566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(patch->gtol, &gtolArray));
25779566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial));
2578c2e6f3c0SFlorian Wechsung       }
25790997d788SPatrick Farrell     }
25800997d788SPatrick Farrell     for (p = pStart; p < pEnd; ++p) {
25810904074fSPatrick Farrell       if (isNonlinear) {
25820904074fSPatrick Farrell         const PetscInt *gtolArray, *gtolArrayWithAll = NULL;
25830904074fSPatrick Farrell         PetscInt        numPatchDofs, offset;
25840904074fSPatrick Farrell         PetscInt        numPatchDofsWithAll, offsetWithAll;
25850904074fSPatrick Farrell         PetscInt        dofWithoutAllCounter = 0;
25860904074fSPatrick Farrell         PetscInt       *patchWithoutAllToWithAllArray;
25870904074fSPatrick Farrell 
25880904074fSPatrick Farrell         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
25890904074fSPatrick Farrell         /* the index in the patch with all dofs */
25909566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->gtol, &gtolArray));
25910904074fSPatrick Farrell 
25929566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs));
259347aca4a6SPatrick Farrell         if (numPatchDofs == 0) {
2594b88cb22dSPatrick Farrell           patch->dofMappingWithoutToWithAll[p - pStart] = NULL;
259547aca4a6SPatrick Farrell           continue;
259647aca4a6SPatrick Farrell         }
25970904074fSPatrick Farrell 
25989566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset));
25999566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->gtolWithAll, &gtolArrayWithAll));
26009566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->gtolCountsWithAll, p, &numPatchDofsWithAll));
26019566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offsetWithAll));
26020904074fSPatrick Farrell 
26039566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(numPatchDofs, &patchWithoutAllToWithAllArray));
26040904074fSPatrick Farrell 
26050904074fSPatrick Farrell         for (i = 0; i < numPatchDofsWithAll; i++) {
26060904074fSPatrick Farrell           if (gtolArrayWithAll[i + offsetWithAll] == gtolArray[offset + dofWithoutAllCounter]) {
26070904074fSPatrick Farrell             patchWithoutAllToWithAllArray[dofWithoutAllCounter] = i;
26080904074fSPatrick Farrell             dofWithoutAllCounter++;
26099371c9d4SSatish Balay             if (dofWithoutAllCounter == numPatchDofs) break;
26100904074fSPatrick Farrell           }
26110904074fSPatrick Farrell         }
26129566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutAllToWithAllArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithAll[p - pStart]));
26139566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(patch->gtol, &gtolArray));
26149566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(patch->gtolWithAll, &gtolArrayWithAll));
26150904074fSPatrick Farrell       }
26164bbf5ea8SMatthew G. Knepley     }
261760dd46caSLawrence Mitchell     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
26189566063dSJacob Faibussowitsch       PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDofWithArtificial, &patch->patchRHSWithArtificial));
26199566063dSJacob Faibussowitsch       PetscCall(VecSetUp(patch->patchRHSWithArtificial));
262060dd46caSLawrence Mitchell     }
26219566063dSJacob Faibussowitsch     PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDof, &patch->patchRHS));
26229566063dSJacob Faibussowitsch     PetscCall(VecSetUp(patch->patchRHS));
26239566063dSJacob Faibussowitsch     PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDof, &patch->patchUpdate));
26249566063dSJacob Faibussowitsch     PetscCall(VecSetUp(patch->patchUpdate));
26254bbf5ea8SMatthew G. Knepley     if (patch->save_operators) {
26269566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(patch->npatch, &patch->mat));
262748a46eb9SPierre Jolivet       for (i = 0; i < patch->npatch; ++i) PetscCall(PCPatchCreateMatrix_Private(pc, i, &patch->mat[i], PETSC_FALSE));
26284bbf5ea8SMatthew G. Knepley     }
26299566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(PC_Patch_CreatePatches, pc, 0, 0, 0));
26304bbf5ea8SMatthew G. Knepley 
26314bbf5ea8SMatthew G. Knepley     /* If desired, calculate weights for dof multiplicity */
26324bbf5ea8SMatthew G. Knepley     if (patch->partition_of_unity) {
26333bb0e8f7SKarl Rupp       PetscScalar *input  = NULL;
26343bb0e8f7SKarl Rupp       PetscScalar *output = NULL;
26353bb0e8f7SKarl Rupp       Vec          global;
26363bb0e8f7SKarl Rupp 
26379566063dSJacob Faibussowitsch       PetscCall(VecDuplicate(patch->localRHS, &patch->dof_weights));
263861c4b389SFlorian Wechsung       if (patch->local_composition_type == PC_COMPOSITE_ADDITIVE) {
26394bbf5ea8SMatthew G. Knepley         for (i = 0; i < patch->npatch; ++i) {
26404bbf5ea8SMatthew G. Knepley           PetscInt dof;
26414bbf5ea8SMatthew G. Knepley 
26429566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetDof(patch->gtolCounts, i + pStart, &dof));
26434bbf5ea8SMatthew G. Knepley           if (dof <= 0) continue;
26449566063dSJacob Faibussowitsch           PetscCall(VecSet(patch->patchRHS, 1.0));
26459566063dSJacob Faibussowitsch           PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHS, patch->dof_weights, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR));
26464bbf5ea8SMatthew G. Knepley         }
2647c2e6f3c0SFlorian Wechsung       } else {
2648e047a90bSFlorian Wechsung         /* multiplicative is actually only locally multiplicative and globally additive. need the pou where the mesh decomposition overlaps */
26499566063dSJacob Faibussowitsch         PetscCall(VecSet(patch->dof_weights, 1.0));
26504bbf5ea8SMatthew G. Knepley       }
2651d132cafaSFlorian Wechsung 
26523ba16761SJacob Faibussowitsch       PetscCall(VecDuplicate(patch->dof_weights, &global));
26533ba16761SJacob Faibussowitsch       PetscCall(VecSet(global, 0.));
2654d132cafaSFlorian Wechsung 
26559566063dSJacob Faibussowitsch       PetscCall(VecGetArray(patch->dof_weights, &input));
26569566063dSJacob Faibussowitsch       PetscCall(VecGetArray(global, &output));
26579566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceBegin(patch->sectionSF, MPIU_SCALAR, input, output, MPI_SUM));
26589566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceEnd(patch->sectionSF, MPIU_SCALAR, input, output, MPI_SUM));
26599566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(patch->dof_weights, &input));
26609566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(global, &output));
2661d132cafaSFlorian Wechsung 
26629566063dSJacob Faibussowitsch       PetscCall(VecReciprocal(global));
2663d132cafaSFlorian Wechsung 
26649566063dSJacob Faibussowitsch       PetscCall(VecGetArray(patch->dof_weights, &output));
26659566063dSJacob Faibussowitsch       PetscCall(VecGetArray(global, &input));
26669566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(patch->sectionSF, MPIU_SCALAR, input, output, MPI_REPLACE));
26679566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(patch->sectionSF, MPIU_SCALAR, input, output, MPI_REPLACE));
26689566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(patch->dof_weights, &output));
26699566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(global, &input));
26709566063dSJacob Faibussowitsch       PetscCall(VecDestroy(&global));
26714bbf5ea8SMatthew G. Knepley     }
267248a46eb9SPierre Jolivet     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE && patch->save_operators && !patch->isNonlinear) PetscCall(PetscMalloc1(patch->npatch, &patch->matWithArtificial));
26734bbf5ea8SMatthew G. Knepley   }
26749566063dSJacob Faibussowitsch   PetscCall((*patch->setupsolver)(pc));
26753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
26764bbf5ea8SMatthew G. Knepley }
2677dadc69c5SMatthew G. Knepley 
2678d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_PATCH_Linear(PC pc, PetscInt i, Vec x, Vec y)
2679d71ae5a4SJacob Faibussowitsch {
2680dadc69c5SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
2681c73d2cf6SLawrence Mitchell   KSP       ksp;
26829d4fc724SLawrence Mitchell   Mat       op;
26839d4fc724SLawrence Mitchell   PetscInt  m, n;
2684dadc69c5SMatthew G. Knepley 
2685dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2686c73d2cf6SLawrence Mitchell   if (patch->denseinverse) {
26879566063dSJacob Faibussowitsch     PetscCall((*patch->densesolve)(patch->mat[i], x, y));
26883ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2689c73d2cf6SLawrence Mitchell   }
2690c73d2cf6SLawrence Mitchell   ksp = (KSP)patch->solver[i];
2691dadc69c5SMatthew G. Knepley   if (!patch->save_operators) {
2692dadc69c5SMatthew G. Knepley     Mat mat;
2693dadc69c5SMatthew G. Knepley 
26949566063dSJacob Faibussowitsch     PetscCall(PCPatchCreateMatrix_Private(pc, i, &mat, PETSC_FALSE));
2695dadc69c5SMatthew G. Knepley     /* Populate operator here. */
26969566063dSJacob Faibussowitsch     PetscCall(PCPatchComputeOperator_Internal(pc, NULL, mat, i, PETSC_FALSE));
26979566063dSJacob Faibussowitsch     PetscCall(KSPSetOperators(ksp, mat, mat));
2698dadc69c5SMatthew G. Knepley     /* Drop reference so the KSPSetOperators below will blow it away. */
26999566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&mat));
2700dadc69c5SMatthew G. Knepley   }
27019566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0));
270248a46eb9SPierre Jolivet   if (!ksp->setfromoptionscalled) PetscCall(KSPSetFromOptions(ksp));
27039d4fc724SLawrence Mitchell   /* Disgusting trick to reuse work vectors */
27049566063dSJacob Faibussowitsch   PetscCall(KSPGetOperators(ksp, &op, NULL));
27059566063dSJacob Faibussowitsch   PetscCall(MatGetLocalSize(op, &m, &n));
27069d4fc724SLawrence Mitchell   x->map->n = m;
27079d4fc724SLawrence Mitchell   y->map->n = n;
27089d4fc724SLawrence Mitchell   x->map->N = m;
27099d4fc724SLawrence Mitchell   y->map->N = n;
27109566063dSJacob Faibussowitsch   PetscCall(KSPSolve(ksp, x, y));
27119566063dSJacob Faibussowitsch   PetscCall(KSPCheckSolve(ksp, pc, y));
27129566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0));
2713dadc69c5SMatthew G. Knepley   if (!patch->save_operators) {
2714dadc69c5SMatthew G. Knepley     PC pc;
27159566063dSJacob Faibussowitsch     PetscCall(KSPSetOperators(ksp, NULL, NULL));
27169566063dSJacob Faibussowitsch     PetscCall(KSPGetPC(ksp, &pc));
2717dadc69c5SMatthew G. Knepley     /* Destroy PC context too, otherwise the factored matrix hangs around. */
27189566063dSJacob Faibussowitsch     PetscCall(PCReset(pc));
27194bbf5ea8SMatthew G. Knepley   }
27203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27214bbf5ea8SMatthew G. Knepley }
27224bbf5ea8SMatthew G. Knepley 
2723d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCUpdateMultiplicative_PATCH_Linear(PC pc, PetscInt i, PetscInt pStart)
2724d71ae5a4SJacob Faibussowitsch {
27256c9c532dSPatrick Farrell   PC_PATCH *patch = (PC_PATCH *)pc->data;
27266c9c532dSPatrick Farrell   Mat       multMat;
27279d4fc724SLawrence Mitchell   PetscInt  n, m;
27286c9c532dSPatrick Farrell 
27294d04e9f1SPatrick Farrell   PetscFunctionBegin;
27306c9c532dSPatrick Farrell   if (patch->save_operators) {
27316c9c532dSPatrick Farrell     multMat = patch->matWithArtificial[i];
27326c9c532dSPatrick Farrell   } else {
27336c9c532dSPatrick Farrell     /*Very inefficient, hopefully we can just assemble the rectangular matrix in the first place.*/
27346c9c532dSPatrick Farrell     Mat      matSquare;
27356c9c532dSPatrick Farrell     PetscInt dof;
27366c9c532dSPatrick Farrell     IS       rowis;
27379566063dSJacob Faibussowitsch     PetscCall(PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE));
27389566063dSJacob Faibussowitsch     PetscCall(PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE));
27399566063dSJacob Faibussowitsch     PetscCall(MatGetSize(matSquare, &dof, NULL));
27409566063dSJacob Faibussowitsch     PetscCall(ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis));
27419566063dSJacob Faibussowitsch     PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &multMat));
27429566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&matSquare));
27439566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&rowis));
27446c9c532dSPatrick Farrell   }
27459d4fc724SLawrence Mitchell   /* Disgusting trick to reuse work vectors */
27469566063dSJacob Faibussowitsch   PetscCall(MatGetLocalSize(multMat, &m, &n));
27479d4fc724SLawrence Mitchell   patch->patchUpdate->map->n            = n;
27489d4fc724SLawrence Mitchell   patch->patchRHSWithArtificial->map->n = m;
27499d4fc724SLawrence Mitchell   patch->patchUpdate->map->N            = n;
27509d4fc724SLawrence Mitchell   patch->patchRHSWithArtificial->map->N = m;
27519566063dSJacob Faibussowitsch   PetscCall(MatMult(multMat, patch->patchUpdate, patch->patchRHSWithArtificial));
27529566063dSJacob Faibussowitsch   PetscCall(VecScale(patch->patchRHSWithArtificial, -1.0));
27539566063dSJacob Faibussowitsch   PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHSWithArtificial, patch->localRHS, ADD_VALUES, SCATTER_REVERSE, SCATTER_WITHARTIFICIAL));
275448a46eb9SPierre Jolivet   if (!patch->save_operators) PetscCall(MatDestroy(&multMat));
27553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27566c9c532dSPatrick Farrell }
27576c9c532dSPatrick Farrell 
2758d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_PATCH(PC pc, Vec x, Vec y)
2759d71ae5a4SJacob Faibussowitsch {
27604bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch        = (PC_PATCH *)pc->data;
27611202d238SPatrick Farrell   const PetscScalar *globalRHS    = NULL;
27621202d238SPatrick Farrell   PetscScalar       *localRHS     = NULL;
27631202d238SPatrick Farrell   PetscScalar       *globalUpdate = NULL;
27644bbf5ea8SMatthew G. Knepley   const PetscInt    *bcNodes      = NULL;
27654bbf5ea8SMatthew G. Knepley   PetscInt           nsweep       = patch->symmetrise_sweep ? 2 : 1;
27664bbf5ea8SMatthew G. Knepley   PetscInt           start[2]     = {0, 0};
27674bbf5ea8SMatthew G. Knepley   PetscInt           end[2]       = {-1, -1};
27684bbf5ea8SMatthew G. Knepley   const PetscInt     inc[2]       = {1, -1};
27691202d238SPatrick Farrell   const PetscScalar *localUpdate;
27704bbf5ea8SMatthew G. Knepley   const PetscInt    *iterationSet;
27714bbf5ea8SMatthew G. Knepley   PetscInt           pStart, numBcs, n, sweep, bc, j;
27724bbf5ea8SMatthew G. Knepley 
27734bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
27749566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(PC_Patch_Apply, pc, 0, 0, 0));
2775*648c30bcSBarry Smith   PetscCall(PetscOptionsPushCreateViewerOff(PETSC_TRUE));
277692d50984SMatthew G. Knepley   /* start, end, inc have 2 entries to manage a second backward sweep if we symmetrize */
27774bbf5ea8SMatthew G. Knepley   end[0]   = patch->npatch;
27784bbf5ea8SMatthew G. Knepley   start[1] = patch->npatch - 1;
27794bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
27809566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(patch->iterationSet, &end[0]));
27814bbf5ea8SMatthew G. Knepley     start[1] = end[0] - 1;
27829566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->iterationSet, &iterationSet));
27834bbf5ea8SMatthew G. Knepley   }
27844bbf5ea8SMatthew G. Knepley   /* Scatter from global space into overlapped local spaces */
27859566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(x, &globalRHS));
27869566063dSJacob Faibussowitsch   PetscCall(VecGetArray(patch->localRHS, &localRHS));
27879566063dSJacob Faibussowitsch   PetscCall(PetscSFBcastBegin(patch->sectionSF, MPIU_SCALAR, globalRHS, localRHS, MPI_REPLACE));
27889566063dSJacob Faibussowitsch   PetscCall(PetscSFBcastEnd(patch->sectionSF, MPIU_SCALAR, globalRHS, localRHS, MPI_REPLACE));
27899566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(x, &globalRHS));
27909566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(patch->localRHS, &localRHS));
27914bbf5ea8SMatthew G. Knepley 
27929566063dSJacob Faibussowitsch   PetscCall(VecSet(patch->localUpdate, 0.0));
27939566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, NULL));
27949566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0));
27954bbf5ea8SMatthew G. Knepley   for (sweep = 0; sweep < nsweep; sweep++) {
27964bbf5ea8SMatthew G. Knepley     for (j = start[sweep]; j * inc[sweep] < end[sweep] * inc[sweep]; j += inc[sweep]) {
27974bbf5ea8SMatthew G. Knepley       PetscInt i = patch->user_patches ? iterationSet[j] : j;
27984bbf5ea8SMatthew G. Knepley       PetscInt start, len;
27994bbf5ea8SMatthew G. Knepley 
28009566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(patch->gtolCounts, i + pStart, &len));
28019566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(patch->gtolCounts, i + pStart, &start));
28024bbf5ea8SMatthew G. Knepley       /* TODO: Squash out these guys in the setup as well. */
28034bbf5ea8SMatthew G. Knepley       if (len <= 0) continue;
28044bbf5ea8SMatthew G. Knepley       /* TODO: Do we need different scatters for X and Y? */
28059566063dSJacob Faibussowitsch       PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->localRHS, patch->patchRHS, INSERT_VALUES, SCATTER_FORWARD, SCATTER_INTERIOR));
28069566063dSJacob Faibussowitsch       PetscCall((*patch->applysolver)(pc, i, patch->patchRHS, patch->patchUpdate));
28079566063dSJacob Faibussowitsch       PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchUpdate, patch->localUpdate, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR));
280848a46eb9SPierre Jolivet       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) PetscCall((*patch->updatemultiplicative)(pc, i, pStart));
28094bbf5ea8SMatthew G. Knepley     }
28104bbf5ea8SMatthew G. Knepley   }
28119566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0));
28129566063dSJacob Faibussowitsch   if (patch->user_patches) PetscCall(ISRestoreIndices(patch->iterationSet, &iterationSet));
28134bbf5ea8SMatthew G. Knepley   /* XXX: should we do this on the global vector? */
28141baa6e33SBarry Smith   if (patch->partition_of_unity) PetscCall(VecPointwiseMult(patch->localUpdate, patch->localUpdate, patch->dof_weights));
28151202d238SPatrick Farrell   /* Now patch->localUpdate contains the solution of the patch solves, so we need to combine them all. */
28169566063dSJacob Faibussowitsch   PetscCall(VecSet(y, 0.0));
28179566063dSJacob Faibussowitsch   PetscCall(VecGetArray(y, &globalUpdate));
28189566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(patch->localUpdate, &localUpdate));
28199566063dSJacob Faibussowitsch   PetscCall(PetscSFReduceBegin(patch->sectionSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM));
28209566063dSJacob Faibussowitsch   PetscCall(PetscSFReduceEnd(patch->sectionSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM));
28219566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(patch->localUpdate, &localUpdate));
28224bbf5ea8SMatthew G. Knepley 
28234bbf5ea8SMatthew G. Knepley   /* Now we need to send the global BC values through */
28249566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(x, &globalRHS));
28259566063dSJacob Faibussowitsch   PetscCall(ISGetSize(patch->globalBcNodes, &numBcs));
28269566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->globalBcNodes, &bcNodes));
28279566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(x, &n));
28284bbf5ea8SMatthew G. Knepley   for (bc = 0; bc < numBcs; ++bc) {
28294bbf5ea8SMatthew G. Knepley     const PetscInt idx = bcNodes[bc];
28301202d238SPatrick Farrell     if (idx < n) globalUpdate[idx] = globalRHS[idx];
28314bbf5ea8SMatthew G. Knepley   }
28324bbf5ea8SMatthew G. Knepley 
28339566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->globalBcNodes, &bcNodes));
28349566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(x, &globalRHS));
28359566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(y, &globalUpdate));
28364bbf5ea8SMatthew G. Knepley 
2837*648c30bcSBarry Smith   PetscCall(PetscOptionsPopCreateViewerOff());
28389566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(PC_Patch_Apply, pc, 0, 0, 0));
28393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
28404bbf5ea8SMatthew G. Knepley }
28414bbf5ea8SMatthew G. Knepley 
2842d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCReset_PATCH_Linear(PC pc)
2843d71ae5a4SJacob Faibussowitsch {
2844dadc69c5SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
2845dadc69c5SMatthew G. Knepley   PetscInt  i;
2846dadc69c5SMatthew G. Knepley 
2847dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2848dadc69c5SMatthew G. Knepley   if (patch->solver) {
28499566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(KSPReset((KSP)patch->solver[i]));
2850dadc69c5SMatthew G. Knepley   }
28513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2852dadc69c5SMatthew G. Knepley }
2853dadc69c5SMatthew G. Knepley 
2854d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCReset_PATCH(PC pc)
2855d71ae5a4SJacob Faibussowitsch {
28564bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
28574bbf5ea8SMatthew G. Knepley   PetscInt  i;
28584bbf5ea8SMatthew G. Knepley 
28594bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
28609566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&patch->sectionSF));
28619566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->cellCounts));
28629566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->pointCounts));
28639566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->cellNumbering));
28649566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->gtolCounts));
28659566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->gtol));
28669566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->cells));
28679566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->points));
28689566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->dofs));
28699566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->offs));
28709566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->patchSection));
28719566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->ghostBcNodes));
28729566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->globalBcNodes));
28739566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->gtolCountsWithArtificial));
28749566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->gtolWithArtificial));
28759566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->dofsWithArtificial));
28769566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->offsWithArtificial));
28779566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->gtolCountsWithAll));
28789566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->gtolWithAll));
28799566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->dofsWithAll));
28809566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->offsWithAll));
28819566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->cellMats));
28829566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->intFacetMats));
28839566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->allCells));
28849566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->intFacets));
28859566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->extFacets));
28869566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->intFacetsToPatchCell));
28879566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->intFacetCounts));
28889566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->extFacetCounts));
28894bbf5ea8SMatthew G. Knepley 
28909371c9d4SSatish Balay   if (patch->dofSection)
28919371c9d4SSatish Balay     for (i = 0; i < patch->nsubspaces; i++) PetscCall(PetscSectionDestroy(&patch->dofSection[i]));
28929566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->dofSection));
28939566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->bs));
28949566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->nodesPerCell));
28959371c9d4SSatish Balay   if (patch->cellNodeMap)
28969371c9d4SSatish Balay     for (i = 0; i < patch->nsubspaces; i++) PetscCall(PetscFree(patch->cellNodeMap[i]));
28979566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->cellNodeMap));
28989566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->subspaceOffsets));
28994bbf5ea8SMatthew G. Knepley 
29009566063dSJacob Faibussowitsch   PetscCall((*patch->resetsolver)(pc));
29014bbf5ea8SMatthew G. Knepley 
290248a46eb9SPierre Jolivet   if (patch->subspaces_to_exclude) PetscCall(PetscHSetIDestroy(&patch->subspaces_to_exclude));
2903e4c66b91SPatrick Farrell 
29049566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->localRHS));
29059566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->localUpdate));
29069566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->patchRHS));
29079566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->patchUpdate));
29089566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->dof_weights));
29094bbf5ea8SMatthew G. Knepley   if (patch->patch_dof_weights) {
29109566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(VecDestroy(&patch->patch_dof_weights[i]));
29119566063dSJacob Faibussowitsch     PetscCall(PetscFree(patch->patch_dof_weights));
29124bbf5ea8SMatthew G. Knepley   }
29134bbf5ea8SMatthew G. Knepley   if (patch->mat) {
29149566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(MatDestroy(&patch->mat[i]));
29159566063dSJacob Faibussowitsch     PetscCall(PetscFree(patch->mat));
29165f824522SMatthew G. Knepley   }
29170997d788SPatrick Farrell   if (patch->matWithArtificial && !patch->isNonlinear) {
29189566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(MatDestroy(&patch->matWithArtificial[i]));
29199566063dSJacob Faibussowitsch     PetscCall(PetscFree(patch->matWithArtificial));
292011ac8bb0SFlorian Wechsung   }
29219566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->patchRHSWithArtificial));
292296b79ebeSFlorian Wechsung   if (patch->dofMappingWithoutToWithArtificial) {
29239566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->dofMappingWithoutToWithArtificial[i]));
29249566063dSJacob Faibussowitsch     PetscCall(PetscFree(patch->dofMappingWithoutToWithArtificial));
292596b79ebeSFlorian Wechsung   }
29260904074fSPatrick Farrell   if (patch->dofMappingWithoutToWithAll) {
29279566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->dofMappingWithoutToWithAll[i]));
29289566063dSJacob Faibussowitsch     PetscCall(PetscFree(patch->dofMappingWithoutToWithAll));
29290904074fSPatrick Farrell   }
29309566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->sub_mat_type));
29315f824522SMatthew G. Knepley   if (patch->userIS) {
29329566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->userIS[i]));
29339566063dSJacob Faibussowitsch     PetscCall(PetscFree(patch->userIS));
29345f824522SMatthew G. Knepley   }
29359566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->precomputedTensorLocations));
29369566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->precomputedIntFacetTensorLocations));
2937f98464cbSLawrence Mitchell 
29380a545947SLisandro Dalcin   patch->bs          = NULL;
29394bbf5ea8SMatthew G. Knepley   patch->cellNodeMap = NULL;
29407974b488SMatthew G. Knepley   patch->nsubspaces  = 0;
29419566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->iterationSet));
29425f824522SMatthew G. Knepley 
2943*648c30bcSBarry Smith   PetscCall(PetscViewerDestroy(&patch->viewerCells));
2944*648c30bcSBarry Smith   PetscCall(PetscViewerDestroy(&patch->viewerIntFacets));
2945*648c30bcSBarry Smith   PetscCall(PetscViewerDestroy(&patch->viewerPoints));
2946*648c30bcSBarry Smith   PetscCall(PetscViewerDestroy(&patch->viewerSection));
2947*648c30bcSBarry Smith   PetscCall(PetscViewerDestroy(&patch->viewerMatrix));
29483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29494bbf5ea8SMatthew G. Knepley }
29504bbf5ea8SMatthew G. Knepley 
2951d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_PATCH_Linear(PC pc)
2952d71ae5a4SJacob Faibussowitsch {
29534bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
29544bbf5ea8SMatthew G. Knepley   PetscInt  i;
29554bbf5ea8SMatthew G. Knepley 
29564bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2957dadc69c5SMatthew G. Knepley   if (patch->solver) {
29589566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(KSPDestroy((KSP *)&patch->solver[i]));
29599566063dSJacob Faibussowitsch     PetscCall(PetscFree(patch->solver));
29604bbf5ea8SMatthew G. Knepley   }
29613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2962dadc69c5SMatthew G. Knepley }
2963dadc69c5SMatthew G. Knepley 
2964d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_PATCH(PC pc)
2965d71ae5a4SJacob Faibussowitsch {
2966dadc69c5SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
2967dadc69c5SMatthew G. Knepley 
2968dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
29699566063dSJacob Faibussowitsch   PetscCall(PCReset_PATCH(pc));
29709566063dSJacob Faibussowitsch   PetscCall((*patch->destroysolver)(pc));
29719566063dSJacob Faibussowitsch   PetscCall(PetscFree(pc->data));
29723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29734bbf5ea8SMatthew G. Knepley }
29744bbf5ea8SMatthew G. Knepley 
2975d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetFromOptions_PATCH(PC pc, PetscOptionItems *PetscOptionsObject)
2976d71ae5a4SJacob Faibussowitsch {
29774bbf5ea8SMatthew G. Knepley   PC_PATCH            *patch                 = (PC_PATCH *)pc->data;
29784bbf5ea8SMatthew G. Knepley   PCPatchConstructType patchConstructionType = PC_PATCH_STAR;
29795f824522SMatthew G. Knepley   char                 sub_mat_type[PETSC_MAX_PATH_LEN];
298010534d48SPatrick Farrell   char                 option[PETSC_MAX_PATH_LEN];
29815f824522SMatthew G. Knepley   const char          *prefix;
29824bbf5ea8SMatthew G. Knepley   PetscBool            flg, dimflg, codimflg;
29835f824522SMatthew G. Knepley   MPI_Comm             comm;
2984a48c39c8SPatrick Farrell   PetscInt            *ifields, nfields, k;
298561c4b389SFlorian Wechsung   PCCompositeType      loctype = PC_COMPOSITE_ADDITIVE;
29864bbf5ea8SMatthew G. Knepley 
29874bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
29889566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)pc, &comm));
29899566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)pc, &prefix));
2990d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "Patch solver options");
299110534d48SPatrick Farrell 
29929566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_save_operators", patch->classname));
29939566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool(option, "Store all patch operators for lifetime of object?", "PCPatchSetSaveOperators", patch->save_operators, &patch->save_operators, &flg));
299410534d48SPatrick Farrell 
29959566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_precompute_element_tensors", patch->classname));
29969566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool(option, "Compute each element tensor only once?", "PCPatchSetPrecomputeElementTensors", patch->precomputeElementTensors, &patch->precomputeElementTensors, &flg));
29979566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_partition_of_unity", patch->classname));
29989566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool(option, "Weight contributions by dof multiplicity?", "PCPatchSetPartitionOfUnity", patch->partition_of_unity, &patch->partition_of_unity, &flg));
299910534d48SPatrick Farrell 
30009566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_local_type", patch->classname));
30019566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum(option, "Type of local solver composition (additive or multiplicative)", "PCPatchSetLocalComposition", PCCompositeTypes, (PetscEnum)loctype, (PetscEnum *)&loctype, &flg));
30029566063dSJacob Faibussowitsch   if (flg) PetscCall(PCPatchSetLocalComposition(pc, loctype));
30039566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_dense_inverse", patch->classname));
30049566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool(option, "Compute inverses of patch matrices and apply directly? Ignores KSP/PC settings on patch.", "PCPatchSetDenseInverse", patch->denseinverse, &patch->denseinverse, &flg));
30059566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_dim", patch->classname));
30069566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt(option, "What dimension of mesh point to construct patches by? (0 = vertices)", "PCPATCH", patch->dim, &patch->dim, &dimflg));
30079566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_codim", patch->classname));
30089566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt(option, "What co-dimension of mesh point to construct patches by? (0 = cells)", "PCPATCH", patch->codim, &patch->codim, &codimflg));
300908401ef6SPierre Jolivet   PetscCheck(!dimflg || !codimflg, comm, PETSC_ERR_ARG_WRONG, "Can only set one of dimension or co-dimension");
301010534d48SPatrick Farrell 
30119566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_type", patch->classname));
30129566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum(option, "How should the patches be constructed?", "PCPatchSetConstructType", PCPatchConstructTypes, (PetscEnum)patchConstructionType, (PetscEnum *)&patchConstructionType, &flg));
30139566063dSJacob Faibussowitsch   if (flg) PetscCall(PCPatchSetConstructType(pc, patchConstructionType, NULL, NULL));
301410534d48SPatrick Farrell 
30159566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_vanka_dim", patch->classname));
30169566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt(option, "Topological dimension of entities for Vanka to ignore", "PCPATCH", patch->vankadim, &patch->vankadim, &flg));
301710534d48SPatrick Farrell 
30189566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_ignore_dim", patch->classname));
30199566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt(option, "Topological dimension of entities for completion to ignore", "PCPATCH", patch->ignoredim, &patch->ignoredim, &flg));
302010534d48SPatrick Farrell 
30219566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_pardecomp_overlap", patch->classname));
30229566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt(option, "What overlap should we use in construct type pardecomp?", "PCPATCH", patch->pardecomp_overlap, &patch->pardecomp_overlap, &flg));
3023b525f888SPatrick Farrell 
30249566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_sub_mat_type", patch->classname));
30259566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList(option, "Matrix type for patch solves", "PCPatchSetSubMatType", MatList, NULL, sub_mat_type, PETSC_MAX_PATH_LEN, &flg));
30269566063dSJacob Faibussowitsch   if (flg) PetscCall(PCPatchSetSubMatType(pc, sub_mat_type));
302710534d48SPatrick Farrell 
30289566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_symmetrise_sweep", patch->classname));
30299566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool(option, "Go start->end, end->start?", "PCPATCH", patch->symmetrise_sweep, &patch->symmetrise_sweep, &flg));
3030e4c66b91SPatrick Farrell 
3031a48c39c8SPatrick Farrell   /* If the user has set the number of subspaces, use that for the buffer size,
3032a48c39c8SPatrick Farrell    otherwise use a large number */
3033a48c39c8SPatrick Farrell   if (patch->nsubspaces <= 0) {
3034a48c39c8SPatrick Farrell     nfields = 128;
3035a48c39c8SPatrick Farrell   } else {
3036a48c39c8SPatrick Farrell     nfields = patch->nsubspaces;
3037a48c39c8SPatrick Farrell   }
30389566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nfields, &ifields));
30399566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exclude_subspaces", patch->classname));
30409566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetIntArray(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, option, ifields, &nfields, &flg));
304108401ef6SPierre 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");
3042e4c66b91SPatrick Farrell   if (flg) {
30439566063dSJacob Faibussowitsch     PetscCall(PetscHSetIClear(patch->subspaces_to_exclude));
304448a46eb9SPierre Jolivet     for (k = 0; k < nfields; k++) PetscCall(PetscHSetIAdd(patch->subspaces_to_exclude, ifields[k]));
3045e4c66b91SPatrick Farrell   }
30469566063dSJacob Faibussowitsch   PetscCall(PetscFree(ifields));
30475f824522SMatthew G. Knepley 
30489566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_patches_view", patch->classname));
30499566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool(option, "Print out information during patch construction", "PCPATCH", patch->viewPatches, &patch->viewPatches, &flg));
30509566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_cells_view", patch->classname));
3051*648c30bcSBarry Smith   PetscCall(PetscOptionsCreateViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerCells, &patch->formatCells, &patch->viewCells));
30529566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_interior_facets_view", patch->classname));
3053*648c30bcSBarry Smith   PetscCall(PetscOptionsCreateViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerIntFacets, &patch->formatIntFacets, &patch->viewIntFacets));
30549566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exterior_facets_view", patch->classname));
3055*648c30bcSBarry Smith   PetscCall(PetscOptionsCreateViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerExtFacets, &patch->formatExtFacets, &patch->viewExtFacets));
30569566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_points_view", patch->classname));
3057*648c30bcSBarry Smith   PetscCall(PetscOptionsCreateViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerPoints, &patch->formatPoints, &patch->viewPoints));
30589566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_section_view", patch->classname));
3059*648c30bcSBarry Smith   PetscCall(PetscOptionsCreateViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerSection, &patch->formatSection, &patch->viewSection));
30609566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_mat_view", patch->classname));
3061*648c30bcSBarry Smith   PetscCall(PetscOptionsCreateViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerMatrix, &patch->formatMatrix, &patch->viewMatrix));
3062d0609cedSBarry Smith   PetscOptionsHeadEnd();
30635f824522SMatthew G. Knepley   patch->optionsSet = PETSC_TRUE;
30643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
30654bbf5ea8SMatthew G. Knepley }
30664bbf5ea8SMatthew G. Knepley 
3067d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUpOnBlocks_PATCH(PC pc)
3068d71ae5a4SJacob Faibussowitsch {
30694bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch = (PC_PATCH *)pc->data;
30704bbf5ea8SMatthew G. Knepley   KSPConvergedReason reason;
30714bbf5ea8SMatthew G. Knepley   PetscInt           i;
30724bbf5ea8SMatthew G. Knepley 
30734bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3074a1eac568SLawrence Mitchell   if (!patch->save_operators) {
3075a1eac568SLawrence Mitchell     /* Can't do this here because the sub KSPs don't have an operator attached yet. */
30763ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3077a1eac568SLawrence Mitchell   }
3078c73d2cf6SLawrence Mitchell   if (patch->denseinverse) {
3079c73d2cf6SLawrence Mitchell     /* No solvers */
30803ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3081c73d2cf6SLawrence Mitchell   }
30824bbf5ea8SMatthew G. Knepley   for (i = 0; i < patch->npatch; ++i) {
308348a46eb9SPierre Jolivet     if (!((KSP)patch->solver[i])->setfromoptionscalled) PetscCall(KSPSetFromOptions((KSP)patch->solver[i]));
30849566063dSJacob Faibussowitsch     PetscCall(KSPSetUp((KSP)patch->solver[i]));
30859566063dSJacob Faibussowitsch     PetscCall(KSPGetConvergedReason((KSP)patch->solver[i], &reason));
3086c0decd05SBarry Smith     if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR;
30874bbf5ea8SMatthew G. Knepley   }
30883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
30894bbf5ea8SMatthew G. Knepley }
30904bbf5ea8SMatthew G. Knepley 
3091d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCView_PATCH(PC pc, PetscViewer viewer)
3092d71ae5a4SJacob Faibussowitsch {
30934bbf5ea8SMatthew G. Knepley   PC_PATCH   *patch = (PC_PATCH *)pc->data;
30944bbf5ea8SMatthew G. Knepley   PetscViewer sviewer;
30954bbf5ea8SMatthew G. Knepley   PetscBool   isascii;
30964bbf5ea8SMatthew G. Knepley   PetscMPIInt rank;
30974bbf5ea8SMatthew G. Knepley 
30984bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
30994bbf5ea8SMatthew G. Knepley   /* TODO Redo tabbing with set tbas in new style */
31009566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
31013ba16761SJacob Faibussowitsch   if (!isascii) PetscFunctionReturn(PETSC_SUCCESS);
31029566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
31039566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPushTab(viewer));
310463a3b9bcSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "Subspace Correction preconditioner with %" PetscInt_FMT " patches\n", patch->npatch));
310561c4b389SFlorian Wechsung   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
31069566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "Schwarz type: multiplicative\n"));
3107e047a90bSFlorian Wechsung   } else {
31089566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "Schwarz type: additive\n"));
3109c2e6f3c0SFlorian Wechsung   }
31109566063dSJacob Faibussowitsch   if (patch->partition_of_unity) PetscCall(PetscViewerASCIIPrintf(viewer, "Weighting by partition of unity\n"));
31119566063dSJacob Faibussowitsch   else PetscCall(PetscViewerASCIIPrintf(viewer, "Not weighting by partition of unity\n"));
31129566063dSJacob Faibussowitsch   if (patch->symmetrise_sweep) PetscCall(PetscViewerASCIIPrintf(viewer, "Symmetrising sweep (start->end, then end->start)\n"));
31139566063dSJacob Faibussowitsch   else PetscCall(PetscViewerASCIIPrintf(viewer, "Not symmetrising sweep\n"));
31149566063dSJacob Faibussowitsch   if (!patch->precomputeElementTensors) PetscCall(PetscViewerASCIIPrintf(viewer, "Not precomputing element tensors (overlapping cells rebuilt in every patch assembly)\n"));
31159566063dSJacob Faibussowitsch   else PetscCall(PetscViewerASCIIPrintf(viewer, "Precomputing element tensors (each cell assembled only once)\n"));
31169566063dSJacob Faibussowitsch   if (!patch->save_operators) PetscCall(PetscViewerASCIIPrintf(viewer, "Not saving patch operators (rebuilt every PCApply)\n"));
31179566063dSJacob Faibussowitsch   else PetscCall(PetscViewerASCIIPrintf(viewer, "Saving patch operators (rebuilt every PCSetUp)\n"));
31189566063dSJacob Faibussowitsch   if (patch->patchconstructop == PCPatchConstruct_Star) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: star\n"));
31199566063dSJacob Faibussowitsch   else if (patch->patchconstructop == PCPatchConstruct_Vanka) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: Vanka\n"));
31209566063dSJacob Faibussowitsch   else if (patch->patchconstructop == PCPatchConstruct_User) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: user-specified\n"));
31219566063dSJacob Faibussowitsch   else PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: unknown\n"));
31225d30859aSPatrick Farrell 
3123c73d2cf6SLawrence Mitchell   if (patch->denseinverse) {
31249566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "Explicitly forming dense inverse and applying patch solver via MatMult.\n"));
3125c73d2cf6SLawrence Mitchell   } else {
31265d30859aSPatrick Farrell     if (patch->isNonlinear) {
31279566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "SNES on patches (all same):\n"));
31285d30859aSPatrick Farrell     } else {
31299566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "KSP on patches (all same):\n"));
31305d30859aSPatrick Farrell     }
3131dadc69c5SMatthew G. Knepley     if (patch->solver) {
31329566063dSJacob Faibussowitsch       PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
3133dd400576SPatrick Sanan       if (rank == 0) {
31349566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPushTab(sviewer));
31359566063dSJacob Faibussowitsch         PetscCall(PetscObjectView(patch->solver[0], sviewer));
31369566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPopTab(sviewer));
31374bbf5ea8SMatthew G. Knepley       }
31389566063dSJacob Faibussowitsch       PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
31394bbf5ea8SMatthew G. Knepley     } else {
31409566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
31419566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "Solver not yet set.\n"));
31429566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
31434bbf5ea8SMatthew G. Knepley     }
3144c73d2cf6SLawrence Mitchell   }
31459566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPopTab(viewer));
31463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31474bbf5ea8SMatthew G. Knepley }
31484bbf5ea8SMatthew G. Knepley 
3149e5893cccSMatthew G. Knepley /*MC
3150f1580f4eSBarry Smith    PCPATCH - A `PC` object that encapsulates flexible definition of blocks for overlapping and non-overlapping
315198ed095eSMatthew G. Knepley    small block additive preconditioners. Block definition is based on topology from
3152f1580f4eSBarry Smith    a `DM` and equation numbering from a `PetscSection`.
3153e5893cccSMatthew G. Knepley 
3154e5893cccSMatthew G. Knepley    Options Database Keys:
3155e5893cccSMatthew G. Knepley + -pc_patch_cells_view   - Views the process local cell numbers for each patch
3156e5893cccSMatthew G. Knepley . -pc_patch_points_view  - Views the process local mesh point numbers for each patch
3157e5893cccSMatthew G. Knepley . -pc_patch_g2l_view     - Views the map between global dofs and patch local dofs for each patch
3158e5893cccSMatthew G. Knepley . -pc_patch_patches_view - Views the global dofs associated with each patch and its boundary
3159e5893cccSMatthew G. Knepley - -pc_patch_sub_mat_view - Views the matrix associated with each patch
3160e5893cccSMatthew G. Knepley 
3161e5893cccSMatthew G. Knepley    Level: intermediate
3162e5893cccSMatthew G. Knepley 
3163562efe2eSBarry Smith .seealso: [](ch_ksp), `PCType`, `PCCreate()`, `PCSetType()`, `PCASM`, `PCJACOBI`, `PCPBJACOBI`, `PCVPBJACOBI`, `SNESPATCH`
3164e5893cccSMatthew G. Knepley M*/
3165d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PCCreate_Patch(PC pc)
3166d71ae5a4SJacob Faibussowitsch {
31674bbf5ea8SMatthew G. Knepley   PC_PATCH *patch;
31684bbf5ea8SMatthew G. Knepley 
31694bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3170f39ec787SMatthew G. Knepley   PetscCall(PetscCitationsRegister(PCPatchCitation, &PCPatchcite));
31714dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&patch));
31724bbf5ea8SMatthew G. Knepley 
317348a46eb9SPierre Jolivet   if (patch->subspaces_to_exclude) PetscCall(PetscHSetIDestroy(&patch->subspaces_to_exclude));
31749566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&patch->subspaces_to_exclude));
3175e4c66b91SPatrick Farrell 
317610534d48SPatrick Farrell   patch->classname   = "pc";
3177debbdec3SPatrick Farrell   patch->isNonlinear = PETSC_FALSE;
317810534d48SPatrick Farrell 
31794bbf5ea8SMatthew G. Knepley   /* Set some defaults */
31805f824522SMatthew G. Knepley   patch->combined                 = PETSC_FALSE;
31814bbf5ea8SMatthew G. Knepley   patch->save_operators           = PETSC_TRUE;
318261c4b389SFlorian Wechsung   patch->local_composition_type   = PC_COMPOSITE_ADDITIVE;
3183fa84ea4cSLawrence Mitchell   patch->precomputeElementTensors = PETSC_FALSE;
31844bbf5ea8SMatthew G. Knepley   patch->partition_of_unity       = PETSC_FALSE;
31854bbf5ea8SMatthew G. Knepley   patch->codim                    = -1;
31864bbf5ea8SMatthew G. Knepley   patch->dim                      = -1;
31874bbf5ea8SMatthew G. Knepley   patch->vankadim                 = -1;
31885f824522SMatthew G. Knepley   patch->ignoredim                = -1;
3189b525f888SPatrick Farrell   patch->pardecomp_overlap        = 0;
31904bbf5ea8SMatthew G. Knepley   patch->patchconstructop         = PCPatchConstruct_Star;
31914bbf5ea8SMatthew G. Knepley   patch->symmetrise_sweep         = PETSC_FALSE;
31925f824522SMatthew G. Knepley   patch->npatch                   = 0;
31934bbf5ea8SMatthew G. Knepley   patch->userIS                   = NULL;
31945f824522SMatthew G. Knepley   patch->optionsSet               = PETSC_FALSE;
31954bbf5ea8SMatthew G. Knepley   patch->iterationSet             = NULL;
31964bbf5ea8SMatthew G. Knepley   patch->user_patches             = PETSC_FALSE;
31979566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(MATDENSE, (char **)&patch->sub_mat_type));
31985f824522SMatthew G. Knepley   patch->viewPatches                       = PETSC_FALSE;
31995f824522SMatthew G. Knepley   patch->viewCells                         = PETSC_FALSE;
32005f824522SMatthew G. Knepley   patch->viewPoints                        = PETSC_FALSE;
32015f824522SMatthew G. Knepley   patch->viewSection                       = PETSC_FALSE;
32025f824522SMatthew G. Knepley   patch->viewMatrix                        = PETSC_FALSE;
32039d4fc724SLawrence Mitchell   patch->densesolve                        = NULL;
3204dadc69c5SMatthew G. Knepley   patch->setupsolver                       = PCSetUp_PATCH_Linear;
3205dadc69c5SMatthew G. Knepley   patch->applysolver                       = PCApply_PATCH_Linear;
3206dadc69c5SMatthew G. Knepley   patch->resetsolver                       = PCReset_PATCH_Linear;
3207dadc69c5SMatthew G. Knepley   patch->destroysolver                     = PCDestroy_PATCH_Linear;
32086c9c532dSPatrick Farrell   patch->updatemultiplicative              = PCUpdateMultiplicative_PATCH_Linear;
320947aca4a6SPatrick Farrell   patch->dofMappingWithoutToWithArtificial = NULL;
321047aca4a6SPatrick Farrell   patch->dofMappingWithoutToWithAll        = NULL;
32114bbf5ea8SMatthew G. Knepley 
32124bbf5ea8SMatthew G. Knepley   pc->data                 = (void *)patch;
32134bbf5ea8SMatthew G. Knepley   pc->ops->apply           = PCApply_PATCH;
32140a545947SLisandro Dalcin   pc->ops->applytranspose  = NULL; /* PCApplyTranspose_PATCH; */
32154bbf5ea8SMatthew G. Knepley   pc->ops->setup           = PCSetUp_PATCH;
32164bbf5ea8SMatthew G. Knepley   pc->ops->reset           = PCReset_PATCH;
32174bbf5ea8SMatthew G. Knepley   pc->ops->destroy         = PCDestroy_PATCH;
32184bbf5ea8SMatthew G. Knepley   pc->ops->setfromoptions  = PCSetFromOptions_PATCH;
32194bbf5ea8SMatthew G. Knepley   pc->ops->setuponblocks   = PCSetUpOnBlocks_PATCH;
32204bbf5ea8SMatthew G. Knepley   pc->ops->view            = PCView_PATCH;
32210a545947SLisandro Dalcin   pc->ops->applyrichardson = NULL;
32223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
32234bbf5ea8SMatthew G. Knepley }
3224