xref: /petsc/src/ksp/pc/impls/patch/pcpatch.c (revision ba38deedd2838b0f18e3744cb0bafd6392690fde)
14bbf5ea8SMatthew G. Knepley #include <petsc/private/pcpatchimpl.h> /*I "petscpc.h" I*/
254ab768cSLawrence Mitchell #include <petsc/private/kspimpl.h>     /* For ksp->setfromoptionscalled */
39d4fc724SLawrence Mitchell #include <petsc/private/vecimpl.h>     /* For vec->map */
45f824522SMatthew G. Knepley #include <petsc/private/dmpleximpl.h>  /* For DMPlexComputeJacobian_Patch_Internal() */
54bbf5ea8SMatthew G. Knepley #include <petscsf.h>
64bbf5ea8SMatthew G. Knepley #include <petscbt.h>
75f824522SMatthew G. Knepley #include <petscds.h>
8c73d2cf6SLawrence Mitchell #include <../src/mat/impls/dense/seq/dense.h> /*I "petscmat.h" I*/
94bbf5ea8SMatthew G. Knepley 
109d4fc724SLawrence Mitchell PetscLogEvent PC_Patch_CreatePatches, PC_Patch_ComputeOp, PC_Patch_Solve, PC_Patch_Apply, PC_Patch_Prealloc;
114bbf5ea8SMatthew G. Knepley 
12d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode ObjectView(PetscObject obj, PetscViewer viewer, PetscViewerFormat format)
13d71ae5a4SJacob Faibussowitsch {
149566063dSJacob Faibussowitsch   PetscCall(PetscViewerPushFormat(viewer, format));
159566063dSJacob Faibussowitsch   PetscCall(PetscObjectView(obj, viewer));
169566063dSJacob Faibussowitsch   PetscCall(PetscViewerPopFormat(viewer));
173ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
185f824522SMatthew G. Knepley }
195f824522SMatthew G. Knepley 
20d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchConstruct_Star(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
21d71ae5a4SJacob Faibussowitsch {
224bbf5ea8SMatthew G. Knepley   PetscInt  starSize;
234bbf5ea8SMatthew G. Knepley   PetscInt *star = NULL, si;
244bbf5ea8SMatthew G. Knepley 
254bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
269566063dSJacob Faibussowitsch   PetscCall(PetscHSetIClear(ht));
274bbf5ea8SMatthew G. Knepley   /* To start with, add the point we care about */
289566063dSJacob Faibussowitsch   PetscCall(PetscHSetIAdd(ht, point));
294bbf5ea8SMatthew G. Knepley   /* Loop over all the points that this point connects to */
309566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star));
319566063dSJacob Faibussowitsch   for (si = 0; si < starSize * 2; si += 2) PetscCall(PetscHSetIAdd(ht, star[si]));
329566063dSJacob Faibussowitsch   PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star));
333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
344bbf5ea8SMatthew G. Knepley }
354bbf5ea8SMatthew G. Knepley 
36d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchConstruct_Vanka(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
37d71ae5a4SJacob Faibussowitsch {
384bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)vpatch;
394bbf5ea8SMatthew G. Knepley   PetscInt  starSize;
404bbf5ea8SMatthew G. Knepley   PetscInt *star         = NULL;
414bbf5ea8SMatthew G. Knepley   PetscBool shouldIgnore = PETSC_FALSE;
424bbf5ea8SMatthew G. Knepley   PetscInt  cStart, cEnd, iStart, iEnd, si;
434bbf5ea8SMatthew G. Knepley 
444bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
459566063dSJacob Faibussowitsch   PetscCall(PetscHSetIClear(ht));
464bbf5ea8SMatthew G. Knepley   /* To start with, add the point we care about */
479566063dSJacob Faibussowitsch   PetscCall(PetscHSetIAdd(ht, point));
484bbf5ea8SMatthew G. Knepley   /* Should we ignore any points of a certain dimension? */
494bbf5ea8SMatthew G. Knepley   if (patch->vankadim >= 0) {
504bbf5ea8SMatthew G. Knepley     shouldIgnore = PETSC_TRUE;
519566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, patch->vankadim, &iStart, &iEnd));
524bbf5ea8SMatthew G. Knepley   }
539566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
544bbf5ea8SMatthew G. Knepley   /* Loop over all the cells that this point connects to */
559566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star));
565f824522SMatthew G. Knepley   for (si = 0; si < starSize * 2; si += 2) {
574bbf5ea8SMatthew G. Knepley     const PetscInt cell = star[si];
584bbf5ea8SMatthew G. Knepley     PetscInt       closureSize;
594bbf5ea8SMatthew G. Knepley     PetscInt      *closure = NULL, ci;
604bbf5ea8SMatthew G. Knepley 
614bbf5ea8SMatthew G. Knepley     if (cell < cStart || cell >= cEnd) continue;
624bbf5ea8SMatthew G. Knepley     /* now loop over all entities in the closure of that cell */
639566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
645f824522SMatthew G. Knepley     for (ci = 0; ci < closureSize * 2; ci += 2) {
654bbf5ea8SMatthew G. Knepley       const PetscInt newpoint = closure[ci];
664bbf5ea8SMatthew G. Knepley 
674bbf5ea8SMatthew G. Knepley       /* We've been told to ignore entities of this type.*/
684bbf5ea8SMatthew G. Knepley       if (shouldIgnore && newpoint >= iStart && newpoint < iEnd) continue;
699566063dSJacob Faibussowitsch       PetscCall(PetscHSetIAdd(ht, newpoint));
704bbf5ea8SMatthew G. Knepley     }
719566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
724bbf5ea8SMatthew G. Knepley   }
739566063dSJacob Faibussowitsch   PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star));
743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
754bbf5ea8SMatthew G. Knepley }
764bbf5ea8SMatthew G. Knepley 
77d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchConstruct_Pardecomp(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
78d71ae5a4SJacob Faibussowitsch {
79b525f888SPatrick Farrell   PC_PATCH       *patch   = (PC_PATCH *)vpatch;
800a390943SPatrick Farrell   DMLabel         ghost   = NULL;
81eae3dc7dSJacob Faibussowitsch   const PetscInt *leaves  = NULL;
82eae3dc7dSJacob Faibussowitsch   PetscInt        nleaves = 0, pStart, pEnd, loc;
830a390943SPatrick Farrell   PetscBool       isFiredrake;
840a390943SPatrick Farrell   PetscBool       flg;
85b525f888SPatrick Farrell   PetscInt        starSize;
86b525f888SPatrick Farrell   PetscInt       *star = NULL;
8725fd193aSPatrick Farrell   PetscInt        opoint, overlapi;
880a390943SPatrick Farrell 
890a390943SPatrick Farrell   PetscFunctionBegin;
909566063dSJacob Faibussowitsch   PetscCall(PetscHSetIClear(ht));
910a390943SPatrick Farrell 
929566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
930a390943SPatrick Farrell 
949566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, "pyop2_ghost", &isFiredrake));
950a390943SPatrick Farrell   if (isFiredrake) {
969566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, "pyop2_ghost", &ghost));
979566063dSJacob Faibussowitsch     PetscCall(DMLabelCreateIndex(ghost, pStart, pEnd));
980a390943SPatrick Farrell   } else {
990a390943SPatrick Farrell     PetscSF sf;
1009566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sf));
1019566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL));
1020a390943SPatrick Farrell     nleaves = PetscMax(nleaves, 0);
1030a390943SPatrick Farrell   }
1040a390943SPatrick Farrell 
10525fd193aSPatrick Farrell   for (opoint = pStart; opoint < pEnd; ++opoint) {
1069566063dSJacob Faibussowitsch     if (ghost) PetscCall(DMLabelHasPoint(ghost, opoint, &flg));
1079371c9d4SSatish Balay     else {
1089371c9d4SSatish Balay       PetscCall(PetscFindInt(opoint, nleaves, leaves, &loc));
1099371c9d4SSatish Balay       flg = loc >= 0 ? PETSC_TRUE : PETSC_FALSE;
1109371c9d4SSatish Balay     }
1110a390943SPatrick Farrell     /* Not an owned entity, don't make a cell patch. */
1120a390943SPatrick Farrell     if (flg) continue;
1139566063dSJacob Faibussowitsch     PetscCall(PetscHSetIAdd(ht, opoint));
1140a390943SPatrick Farrell   }
1150a390943SPatrick Farrell 
116b525f888SPatrick Farrell   /* Now build the overlap for the patch */
11725fd193aSPatrick Farrell   for (overlapi = 0; overlapi < patch->pardecomp_overlap; ++overlapi) {
118b525f888SPatrick Farrell     PetscInt  index    = 0;
119b525f888SPatrick Farrell     PetscInt *htpoints = NULL;
120b525f888SPatrick Farrell     PetscInt  htsize;
12125fd193aSPatrick Farrell     PetscInt  i;
122b525f888SPatrick Farrell 
1239566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(ht, &htsize));
1249566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(htsize, &htpoints));
1259566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetElems(ht, &index, htpoints));
126b525f888SPatrick Farrell 
12725fd193aSPatrick Farrell     for (i = 0; i < htsize; ++i) {
12825fd193aSPatrick Farrell       PetscInt hpoint = htpoints[i];
12925fd193aSPatrick Farrell       PetscInt si;
130b525f888SPatrick Farrell 
1319566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star));
13225fd193aSPatrick Farrell       for (si = 0; si < starSize * 2; si += 2) {
133b525f888SPatrick Farrell         const PetscInt starp = star[si];
134b525f888SPatrick Farrell         PetscInt       closureSize;
135b525f888SPatrick Farrell         PetscInt      *closure = NULL, ci;
136b525f888SPatrick Farrell 
137b525f888SPatrick Farrell         /* now loop over all entities in the closure of starp */
1389566063dSJacob Faibussowitsch         PetscCall(DMPlexGetTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure));
13925fd193aSPatrick Farrell         for (ci = 0; ci < closureSize * 2; ci += 2) {
140b525f888SPatrick Farrell           const PetscInt closstarp = closure[ci];
1419566063dSJacob Faibussowitsch           PetscCall(PetscHSetIAdd(ht, closstarp));
142b525f888SPatrick Farrell         }
1439566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure));
144b525f888SPatrick Farrell       }
1459566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star));
146b525f888SPatrick Farrell     }
1479566063dSJacob Faibussowitsch     PetscCall(PetscFree(htpoints));
148b525f888SPatrick Farrell   }
149b525f888SPatrick Farrell 
1503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1510a390943SPatrick Farrell }
1520a390943SPatrick Farrell 
1534bbf5ea8SMatthew G. Knepley /* The user's already set the patches in patch->userIS. Build the hash tables */
154d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchConstruct_User(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
155d71ae5a4SJacob Faibussowitsch {
1564bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch   = (PC_PATCH *)vpatch;
1574bbf5ea8SMatthew G. Knepley   IS              patchis = patch->userIS[point];
1584bbf5ea8SMatthew G. Knepley   PetscInt        n;
1594bbf5ea8SMatthew G. Knepley   const PetscInt *patchdata;
1604bbf5ea8SMatthew G. Knepley   PetscInt        pStart, pEnd, i;
1614bbf5ea8SMatthew G. Knepley 
1624bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1639566063dSJacob Faibussowitsch   PetscCall(PetscHSetIClear(ht));
1649566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
1659566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(patchis, &n));
1669566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patchis, &patchdata));
1674bbf5ea8SMatthew G. Knepley   for (i = 0; i < n; ++i) {
1684bbf5ea8SMatthew G. Knepley     const PetscInt ownedpoint = patchdata[i];
1694bbf5ea8SMatthew G. Knepley 
1707a46b595SBarry 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);
1719566063dSJacob Faibussowitsch     PetscCall(PetscHSetIAdd(ht, ownedpoint));
1724bbf5ea8SMatthew G. Knepley   }
1739566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patchis, &patchdata));
1743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1754bbf5ea8SMatthew G. Knepley }
1764bbf5ea8SMatthew G. Knepley 
177d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCreateDefaultSF_Private(PC pc, PetscInt n, const PetscSF *sf, const PetscInt *bs)
178d71ae5a4SJacob Faibussowitsch {
1794bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
1804bbf5ea8SMatthew G. Knepley   PetscInt  i;
1814bbf5ea8SMatthew G. Knepley 
1824bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1834bbf5ea8SMatthew G. Knepley   if (n == 1 && bs[0] == 1) {
1841bb6d2a8SBarry Smith     patch->sectionSF = sf[0];
1859566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)patch->sectionSF));
1864bbf5ea8SMatthew G. Knepley   } else {
1874bbf5ea8SMatthew G. Knepley     PetscInt     allRoots = 0, allLeaves = 0;
1884bbf5ea8SMatthew G. Knepley     PetscInt     leafOffset    = 0;
1894bbf5ea8SMatthew G. Knepley     PetscInt    *ilocal        = NULL;
1904bbf5ea8SMatthew G. Knepley     PetscSFNode *iremote       = NULL;
1914bbf5ea8SMatthew G. Knepley     PetscInt    *remoteOffsets = NULL;
1924bbf5ea8SMatthew G. Knepley     PetscInt     index         = 0;
1931b68eb51SMatthew G. Knepley     PetscHMapI   rankToIndex;
1944bbf5ea8SMatthew G. Knepley     PetscInt     numRanks = 0;
1954bbf5ea8SMatthew G. Knepley     PetscSFNode *remote   = NULL;
1964bbf5ea8SMatthew G. Knepley     PetscSF      rankSF;
1974bbf5ea8SMatthew G. Knepley     PetscInt    *ranks   = NULL;
1984bbf5ea8SMatthew G. Knepley     PetscInt    *offsets = NULL;
1994bbf5ea8SMatthew G. Knepley     MPI_Datatype contig;
2001b68eb51SMatthew G. Knepley     PetscHSetI   ranksUniq;
2014bbf5ea8SMatthew G. Knepley 
2024bbf5ea8SMatthew G. Knepley     /* First figure out how many dofs there are in the concatenated numbering.
203f1580f4eSBarry Smith        allRoots: number of owned global dofs;
204f1580f4eSBarry Smith        allLeaves: number of visible dofs (global + ghosted).
2054bbf5ea8SMatthew G. Knepley     */
2064bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2074bbf5ea8SMatthew G. Knepley       PetscInt nroots, nleaves;
2084bbf5ea8SMatthew G. Knepley 
2099566063dSJacob Faibussowitsch       PetscCall(PetscSFGetGraph(sf[i], &nroots, &nleaves, NULL, NULL));
2104bbf5ea8SMatthew G. Knepley       allRoots += nroots * bs[i];
2114bbf5ea8SMatthew G. Knepley       allLeaves += nleaves * bs[i];
2124bbf5ea8SMatthew G. Knepley     }
2139566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(allLeaves, &ilocal));
2149566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(allLeaves, &iremote));
2154bbf5ea8SMatthew G. Knepley     /* Now build an SF that just contains process connectivity. */
2169566063dSJacob Faibussowitsch     PetscCall(PetscHSetICreate(&ranksUniq));
2174bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2184bbf5ea8SMatthew G. Knepley       const PetscMPIInt *ranks = NULL;
2194bbf5ea8SMatthew G. Knepley       PetscInt           nranks, j;
2204bbf5ea8SMatthew G. Knepley 
2219566063dSJacob Faibussowitsch       PetscCall(PetscSFSetUp(sf[i]));
2229566063dSJacob Faibussowitsch       PetscCall(PetscSFGetRootRanks(sf[i], &nranks, &ranks, NULL, NULL, NULL));
2234bbf5ea8SMatthew G. Knepley       /* These are all the ranks who communicate with me. */
22448a46eb9SPierre Jolivet       for (j = 0; j < nranks; ++j) PetscCall(PetscHSetIAdd(ranksUniq, (PetscInt)ranks[j]));
2254bbf5ea8SMatthew G. Knepley     }
2269566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(ranksUniq, &numRanks));
2279566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numRanks, &remote));
2289566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numRanks, &ranks));
2299566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetElems(ranksUniq, &index, ranks));
2304bbf5ea8SMatthew G. Knepley 
2319566063dSJacob Faibussowitsch     PetscCall(PetscHMapICreate(&rankToIndex));
2324bbf5ea8SMatthew G. Knepley     for (i = 0; i < numRanks; ++i) {
2334bbf5ea8SMatthew G. Knepley       remote[i].rank  = ranks[i];
2344bbf5ea8SMatthew G. Knepley       remote[i].index = 0;
2359566063dSJacob Faibussowitsch       PetscCall(PetscHMapISet(rankToIndex, ranks[i], i));
2364bbf5ea8SMatthew G. Knepley     }
2379566063dSJacob Faibussowitsch     PetscCall(PetscFree(ranks));
2389566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&ranksUniq));
2399566063dSJacob Faibussowitsch     PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)pc), &rankSF));
2409566063dSJacob Faibussowitsch     PetscCall(PetscSFSetGraph(rankSF, 1, numRanks, NULL, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER));
2419566063dSJacob Faibussowitsch     PetscCall(PetscSFSetUp(rankSF));
242f1580f4eSBarry Smith     /* OK, use it to communicate the root offset on the remote processes for each subspace. */
2439566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &offsets));
2449566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n * numRanks, &remoteOffsets));
2454bbf5ea8SMatthew G. Knepley 
2464bbf5ea8SMatthew G. Knepley     offsets[0] = 0;
2474bbf5ea8SMatthew G. Knepley     for (i = 1; i < n; ++i) {
2484bbf5ea8SMatthew G. Knepley       PetscInt nroots;
2494bbf5ea8SMatthew G. Knepley 
2509566063dSJacob Faibussowitsch       PetscCall(PetscSFGetGraph(sf[i - 1], &nroots, NULL, NULL, NULL));
2514bbf5ea8SMatthew G. Knepley       offsets[i] = offsets[i - 1] + nroots * bs[i - 1];
2524bbf5ea8SMatthew G. Knepley     }
253f1580f4eSBarry Smith     /* Offsets are the offsets on the current process of the global dof numbering for the subspaces. */
2549566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_contiguous(n, MPIU_INT, &contig));
2559566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_commit(&contig));
2564bbf5ea8SMatthew G. Knepley 
2579566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(rankSF, contig, offsets, remoteOffsets, MPI_REPLACE));
2589566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(rankSF, contig, offsets, remoteOffsets, MPI_REPLACE));
2599566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_free(&contig));
2609566063dSJacob Faibussowitsch     PetscCall(PetscFree(offsets));
2619566063dSJacob Faibussowitsch     PetscCall(PetscSFDestroy(&rankSF));
2624bbf5ea8SMatthew G. Knepley     /* Now remoteOffsets contains the offsets on the remote
263f1580f4eSBarry Smith       processes who communicate with me.  So now we can
264f1580f4eSBarry Smith       concatenate the list of SFs into a single one. */
2654bbf5ea8SMatthew G. Knepley     index = 0;
2664bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2674bbf5ea8SMatthew G. Knepley       const PetscSFNode *remote = NULL;
2684bbf5ea8SMatthew G. Knepley       const PetscInt    *local  = NULL;
2694bbf5ea8SMatthew G. Knepley       PetscInt           nroots, nleaves, j;
2704bbf5ea8SMatthew G. Knepley 
2719566063dSJacob Faibussowitsch       PetscCall(PetscSFGetGraph(sf[i], &nroots, &nleaves, &local, &remote));
2724bbf5ea8SMatthew G. Knepley       for (j = 0; j < nleaves; ++j) {
2734bbf5ea8SMatthew G. Knepley         PetscInt rank = remote[j].rank;
2744bbf5ea8SMatthew G. Knepley         PetscInt idx, rootOffset, k;
2754bbf5ea8SMatthew G. Knepley 
2769566063dSJacob Faibussowitsch         PetscCall(PetscHMapIGet(rankToIndex, rank, &idx));
27708401ef6SPierre Jolivet         PetscCheck(idx != -1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Didn't find rank, huh?");
2784bbf5ea8SMatthew G. Knepley         /* Offset on given rank for ith subspace */
2794bbf5ea8SMatthew G. Knepley         rootOffset = remoteOffsets[n * idx + i];
2804bbf5ea8SMatthew G. Knepley         for (k = 0; k < bs[i]; ++k) {
28173ec7555SLawrence Mitchell           ilocal[index]        = (local ? local[j] : j) * bs[i] + k + leafOffset;
2824bbf5ea8SMatthew G. Knepley           iremote[index].rank  = remote[j].rank;
2834bbf5ea8SMatthew G. Knepley           iremote[index].index = remote[j].index * bs[i] + k + rootOffset;
2844bbf5ea8SMatthew G. Knepley           ++index;
2854bbf5ea8SMatthew G. Knepley         }
2864bbf5ea8SMatthew G. Knepley       }
2874bbf5ea8SMatthew G. Knepley       leafOffset += nleaves * bs[i];
2884bbf5ea8SMatthew G. Knepley     }
2899566063dSJacob Faibussowitsch     PetscCall(PetscHMapIDestroy(&rankToIndex));
2909566063dSJacob Faibussowitsch     PetscCall(PetscFree(remoteOffsets));
2919566063dSJacob Faibussowitsch     PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)pc), &patch->sectionSF));
2929566063dSJacob Faibussowitsch     PetscCall(PetscSFSetGraph(patch->sectionSF, allRoots, allLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER));
2934bbf5ea8SMatthew G. Knepley   }
2943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2954bbf5ea8SMatthew G. Knepley }
2964bbf5ea8SMatthew G. Knepley 
2974bbf5ea8SMatthew G. Knepley /* TODO: Docs */
298*ba38deedSJacob Faibussowitsch static PetscErrorCode PCPatchGetIgnoreDim(PC pc, PetscInt *dim)
299d71ae5a4SJacob Faibussowitsch {
3005f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
3015f824522SMatthew G. Knepley   PetscFunctionBegin;
3025f824522SMatthew G. Knepley   *dim = patch->ignoredim;
3033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3045f824522SMatthew G. Knepley }
3055f824522SMatthew G. Knepley 
3065f824522SMatthew G. Knepley /* TODO: Docs */
307d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetSaveOperators(PC pc, PetscBool flg)
308d71ae5a4SJacob Faibussowitsch {
3094bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
3104bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3114bbf5ea8SMatthew G. Knepley   patch->save_operators = flg;
3123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3134bbf5ea8SMatthew G. Knepley }
3144bbf5ea8SMatthew G. Knepley 
3154bbf5ea8SMatthew G. Knepley /* TODO: Docs */
316d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetSaveOperators(PC pc, PetscBool *flg)
317d71ae5a4SJacob Faibussowitsch {
3184bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
3194bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3204bbf5ea8SMatthew G. Knepley   *flg = patch->save_operators;
3213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3224bbf5ea8SMatthew G. Knepley }
3234bbf5ea8SMatthew G. Knepley 
3244bbf5ea8SMatthew G. Knepley /* TODO: Docs */
325d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetPrecomputeElementTensors(PC pc, PetscBool flg)
326d71ae5a4SJacob Faibussowitsch {
327fa84ea4cSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *)pc->data;
328fa84ea4cSLawrence Mitchell   PetscFunctionBegin;
329fa84ea4cSLawrence Mitchell   patch->precomputeElementTensors = flg;
3303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
331fa84ea4cSLawrence Mitchell }
332fa84ea4cSLawrence Mitchell 
333fa84ea4cSLawrence Mitchell /* TODO: Docs */
334d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetPrecomputeElementTensors(PC pc, PetscBool *flg)
335d71ae5a4SJacob Faibussowitsch {
336fa84ea4cSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *)pc->data;
337fa84ea4cSLawrence Mitchell   PetscFunctionBegin;
338fa84ea4cSLawrence Mitchell   *flg = patch->precomputeElementTensors;
3393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
340fa84ea4cSLawrence Mitchell }
341fa84ea4cSLawrence Mitchell 
342fa84ea4cSLawrence Mitchell /* TODO: Docs */
343d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetPartitionOfUnity(PC pc, PetscBool flg)
344d71ae5a4SJacob Faibussowitsch {
3454bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
3464bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3474bbf5ea8SMatthew G. Knepley   patch->partition_of_unity = flg;
3483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3494bbf5ea8SMatthew G. Knepley }
3504bbf5ea8SMatthew G. Knepley 
3514bbf5ea8SMatthew G. Knepley /* TODO: Docs */
352d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetPartitionOfUnity(PC pc, PetscBool *flg)
353d71ae5a4SJacob Faibussowitsch {
3544bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
3554bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3564bbf5ea8SMatthew G. Knepley   *flg = patch->partition_of_unity;
3573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3584bbf5ea8SMatthew G. Knepley }
3594bbf5ea8SMatthew G. Knepley 
3604bbf5ea8SMatthew G. Knepley /* TODO: Docs */
361*ba38deedSJacob Faibussowitsch static PetscErrorCode PCPatchSetLocalComposition(PC pc, PCCompositeType type)
362d71ae5a4SJacob Faibussowitsch {
363c2e6f3c0SFlorian Wechsung   PC_PATCH *patch = (PC_PATCH *)pc->data;
364c2e6f3c0SFlorian Wechsung   PetscFunctionBegin;
3652472a847SBarry 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");
36661c4b389SFlorian Wechsung   patch->local_composition_type = type;
3673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
368c2e6f3c0SFlorian Wechsung }
369c2e6f3c0SFlorian Wechsung 
370c2e6f3c0SFlorian Wechsung /* TODO: Docs */
371d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetSubMatType(PC pc, MatType sub_mat_type)
372d71ae5a4SJacob Faibussowitsch {
3734bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
3744bbf5ea8SMatthew G. Knepley 
3754bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3769566063dSJacob Faibussowitsch   if (patch->sub_mat_type) PetscCall(PetscFree(patch->sub_mat_type));
3779566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(sub_mat_type, (char **)&patch->sub_mat_type));
3783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3794bbf5ea8SMatthew G. Knepley }
3804bbf5ea8SMatthew G. Knepley 
3814bbf5ea8SMatthew G. Knepley /* TODO: Docs */
382d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetSubMatType(PC pc, MatType *sub_mat_type)
383d71ae5a4SJacob Faibussowitsch {
3844bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
3854bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3864bbf5ea8SMatthew G. Knepley   *sub_mat_type = patch->sub_mat_type;
3873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3884bbf5ea8SMatthew G. Knepley }
3894bbf5ea8SMatthew G. Knepley 
3904bbf5ea8SMatthew G. Knepley /* TODO: Docs */
391d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetCellNumbering(PC pc, PetscSection cellNumbering)
392d71ae5a4SJacob Faibussowitsch {
3934bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
3944bbf5ea8SMatthew G. Knepley 
3954bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3964bbf5ea8SMatthew G. Knepley   patch->cellNumbering = cellNumbering;
3979566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cellNumbering));
3983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3994bbf5ea8SMatthew G. Knepley }
4004bbf5ea8SMatthew G. Knepley 
4014bbf5ea8SMatthew G. Knepley /* TODO: Docs */
402d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetCellNumbering(PC pc, PetscSection *cellNumbering)
403d71ae5a4SJacob Faibussowitsch {
4044bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
4054bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4064bbf5ea8SMatthew G. Knepley   *cellNumbering = patch->cellNumbering;
4073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4084bbf5ea8SMatthew G. Knepley }
4094bbf5ea8SMatthew G. Knepley 
4104bbf5ea8SMatthew G. Knepley /* TODO: Docs */
411d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetConstructType(PC pc, PCPatchConstructType ctype, PetscErrorCode (*func)(PC, PetscInt *, IS **, IS *, void *), void *ctx)
412d71ae5a4SJacob Faibussowitsch {
4134bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
4144bbf5ea8SMatthew G. Knepley 
4154bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4164bbf5ea8SMatthew G. Knepley   patch->ctype = ctype;
4174bbf5ea8SMatthew G. Knepley   switch (ctype) {
4184bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
41940c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
4204bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Star;
4214bbf5ea8SMatthew G. Knepley     break;
4224bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
42340c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
4244bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Vanka;
4254bbf5ea8SMatthew G. Knepley     break;
426e5b9877fSPatrick Farrell   case PC_PATCH_PARDECOMP:
4270a390943SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
428e5b9877fSPatrick Farrell     patch->patchconstructop = PCPatchConstruct_Pardecomp;
4290a390943SPatrick Farrell     break;
4304bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
4314bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
4324bbf5ea8SMatthew G. Knepley     patch->user_patches     = PETSC_TRUE;
4334bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_User;
434bdd9e0cdSPatrick Farrell     if (func) {
4354bbf5ea8SMatthew G. Knepley       patch->userpatchconstructionop = func;
4364bbf5ea8SMatthew G. Knepley       patch->userpatchconstructctx   = ctx;
437bdd9e0cdSPatrick Farrell     }
4384bbf5ea8SMatthew G. Knepley     break;
439d71ae5a4SJacob Faibussowitsch   default:
440d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_USER, "Unknown patch construction type %" PetscInt_FMT, (PetscInt)patch->ctype);
4414bbf5ea8SMatthew G. Knepley   }
4423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4434bbf5ea8SMatthew G. Knepley }
4444bbf5ea8SMatthew G. Knepley 
4454bbf5ea8SMatthew G. Knepley /* TODO: Docs */
446d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetConstructType(PC pc, PCPatchConstructType *ctype, PetscErrorCode (**func)(PC, PetscInt *, IS **, IS *, void *), void **ctx)
447d71ae5a4SJacob Faibussowitsch {
4484bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
4494bbf5ea8SMatthew G. Knepley 
4504bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4514bbf5ea8SMatthew G. Knepley   *ctype = patch->ctype;
4524bbf5ea8SMatthew G. Knepley   switch (patch->ctype) {
4534bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
4544bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
455d71ae5a4SJacob Faibussowitsch   case PC_PATCH_PARDECOMP:
456d71ae5a4SJacob Faibussowitsch     break;
4574bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
4584bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
4594bbf5ea8SMatthew G. Knepley     *func = patch->userpatchconstructionop;
4604bbf5ea8SMatthew G. Knepley     *ctx  = patch->userpatchconstructctx;
4614bbf5ea8SMatthew G. Knepley     break;
462d71ae5a4SJacob Faibussowitsch   default:
463d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_USER, "Unknown patch construction type %" PetscInt_FMT, (PetscInt)patch->ctype);
4644bbf5ea8SMatthew G. Knepley   }
4653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4664bbf5ea8SMatthew G. Knepley }
4674bbf5ea8SMatthew G. Knepley 
4684bbf5ea8SMatthew G. Knepley /* TODO: Docs */
469d71ae5a4SJacob 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)
470d71ae5a4SJacob Faibussowitsch {
4714bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
472b6bb21d1SLawrence Mitchell   DM        dm, plex;
4734bbf5ea8SMatthew G. Knepley   PetscSF  *sfs;
4745f824522SMatthew G. Knepley   PetscInt  cStart, cEnd, i, j;
4754bbf5ea8SMatthew G. Knepley 
4764bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4779566063dSJacob Faibussowitsch   PetscCall(PCGetDM(pc, &dm));
4789566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
479b6bb21d1SLawrence Mitchell   dm = plex;
4809566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
4819566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nsubspaces, &sfs));
4829566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nsubspaces, &patch->dofSection));
4839566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nsubspaces, &patch->bs));
4849566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nsubspaces, &patch->nodesPerCell));
4859566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nsubspaces, &patch->cellNodeMap));
4869566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nsubspaces + 1, &patch->subspaceOffsets));
4874bbf5ea8SMatthew G. Knepley 
4884bbf5ea8SMatthew G. Knepley   patch->nsubspaces       = nsubspaces;
4894bbf5ea8SMatthew G. Knepley   patch->totalDofsPerCell = 0;
4904bbf5ea8SMatthew G. Knepley   for (i = 0; i < nsubspaces; ++i) {
4919566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dms[i], &patch->dofSection[i]));
4929566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)patch->dofSection[i]));
4939566063dSJacob Faibussowitsch     PetscCall(DMGetSectionSF(dms[i], &sfs[i]));
4944bbf5ea8SMatthew G. Knepley     patch->bs[i]           = bs[i];
4954bbf5ea8SMatthew G. Knepley     patch->nodesPerCell[i] = nodesPerCell[i];
4964bbf5ea8SMatthew G. Knepley     patch->totalDofsPerCell += nodesPerCell[i] * bs[i];
4979566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1((cEnd - cStart) * nodesPerCell[i], &patch->cellNodeMap[i]));
49880e8a965SFlorian Wechsung     for (j = 0; j < (cEnd - cStart) * nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
4994bbf5ea8SMatthew G. Knepley     patch->subspaceOffsets[i] = subspaceOffsets[i];
5004bbf5ea8SMatthew G. Knepley   }
5019566063dSJacob Faibussowitsch   PetscCall(PCPatchCreateDefaultSF_Private(pc, nsubspaces, sfs, patch->bs));
5029566063dSJacob Faibussowitsch   PetscCall(PetscFree(sfs));
5034bbf5ea8SMatthew G. Knepley 
5044bbf5ea8SMatthew G. Knepley   patch->subspaceOffsets[nsubspaces] = subspaceOffsets[nsubspaces];
5059566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes));
5069566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes));
5079566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
5083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5094bbf5ea8SMatthew G. Knepley }
5104bbf5ea8SMatthew G. Knepley 
5114bbf5ea8SMatthew G. Knepley /* TODO: Docs */
512*ba38deedSJacob Faibussowitsch static PetscErrorCode PCPatchSetDiscretisationInfoCombined(PC pc, DM dm, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
513d71ae5a4SJacob Faibussowitsch {
5145f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
5155f824522SMatthew G. Knepley   PetscInt  cStart, cEnd, i, j;
5165f824522SMatthew G. Knepley 
5175f824522SMatthew G. Knepley   PetscFunctionBegin;
5185f824522SMatthew G. Knepley   patch->combined = PETSC_TRUE;
5199566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
5209566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &patch->nsubspaces));
5219566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(patch->nsubspaces, &patch->dofSection));
5229566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(patch->nsubspaces, &patch->bs));
5239566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(patch->nsubspaces, &patch->nodesPerCell));
5249566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(patch->nsubspaces, &patch->cellNodeMap));
5259566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(patch->nsubspaces + 1, &patch->subspaceOffsets));
5269566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &patch->dofSection[0]));
5279566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)patch->dofSection[0]));
5289566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(patch->dofSection[0], &patch->subspaceOffsets[patch->nsubspaces]));
5295f824522SMatthew G. Knepley   patch->totalDofsPerCell = 0;
5305f824522SMatthew G. Knepley   for (i = 0; i < patch->nsubspaces; ++i) {
5315f824522SMatthew G. Knepley     patch->bs[i]           = 1;
5325f824522SMatthew G. Knepley     patch->nodesPerCell[i] = nodesPerCell[i];
5335f824522SMatthew G. Knepley     patch->totalDofsPerCell += nodesPerCell[i];
5349566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1((cEnd - cStart) * nodesPerCell[i], &patch->cellNodeMap[i]));
5355f824522SMatthew G. Knepley     for (j = 0; j < (cEnd - cStart) * nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
5365f824522SMatthew G. Knepley   }
5379566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &patch->sectionSF));
5389566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)patch->sectionSF));
5399566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes));
5409566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes));
5413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5425f824522SMatthew G. Knepley }
5435f824522SMatthew G. Knepley 
5445f824522SMatthew G. Knepley /*@C
54504c3f3b8SBarry Smith   PCPatchSetComputeFunction - Set the callback function used to compute patch residuals
54692d50984SMatthew G. Knepley 
54720f4b53cSBarry Smith   Logically Collective
54899b7e5c6SPatrick Farrell 
54992d50984SMatthew G. Knepley   Input Parameters:
55020f4b53cSBarry Smith + pc   - The `PC`
55104c3f3b8SBarry Smith . func - The callback function
55292d50984SMatthew G. Knepley - ctx  - The user context
55392d50984SMatthew G. Knepley 
55420f4b53cSBarry Smith   Calling sequence of `func`:
55520f4b53cSBarry Smith + pc               - The `PC`
5567a50e09dSPatrick Farrell . point            - The point
5577a50e09dSPatrick Farrell . x                - The input solution (not used in linear problems)
5587a50e09dSPatrick Farrell . f                - The patch residual vector
5597a50e09dSPatrick Farrell . cellIS           - An array of the cell numbers
56020f4b53cSBarry Smith . n                - The size of `dofsArray`
5617a50e09dSPatrick Farrell . dofsArray        - The dofmap for the dofs to be solved for
5627a50e09dSPatrick Farrell . dofsArrayWithAll - The dofmap for all dofs on the patch
5637a50e09dSPatrick Farrell - ctx              - The user context
5647a50e09dSPatrick Farrell 
56592d50984SMatthew G. Knepley   Level: advanced
56692d50984SMatthew G. Knepley 
567f1580f4eSBarry Smith   Note:
56804c3f3b8SBarry Smith   The entries of `f` (the output residual vector) have been set to zero before the call.
56992d50984SMatthew G. Knepley 
570db781477SPatrick Sanan .seealso: `PCPatchSetComputeOperator()`, `PCPatchGetComputeOperator()`, `PCPatchSetDiscretisationInfo()`, `PCPatchSetComputeFunctionInteriorFacets()`
57192d50984SMatthew G. Knepley @*/
57204c3f3b8SBarry 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)
573d71ae5a4SJacob Faibussowitsch {
57492d50984SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
57592d50984SMatthew G. Knepley 
57692d50984SMatthew G. Knepley   PetscFunctionBegin;
57792d50984SMatthew G. Knepley   patch->usercomputef    = func;
57892d50984SMatthew G. Knepley   patch->usercomputefctx = ctx;
5793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58092d50984SMatthew G. Knepley }
58192d50984SMatthew G. Knepley 
58292d50984SMatthew G. Knepley /*@C
58304c3f3b8SBarry Smith   PCPatchSetComputeFunctionInteriorFacets - Set the callback function used to compute facet integrals for patch residuals
58459109abcSLawrence Mitchell 
58520f4b53cSBarry Smith   Logically Collective
5867a50e09dSPatrick Farrell 
58759109abcSLawrence Mitchell   Input Parameters:
58820f4b53cSBarry Smith + pc   - The `PC`
58904c3f3b8SBarry Smith . func - The callback function
59059109abcSLawrence Mitchell - ctx  - The user context
59159109abcSLawrence Mitchell 
59220f4b53cSBarry Smith   Calling sequence of `func`:
59320f4b53cSBarry Smith + pc               - The `PC`
5947a50e09dSPatrick Farrell . point            - The point
5957a50e09dSPatrick Farrell . x                - The input solution (not used in linear problems)
5967a50e09dSPatrick Farrell . f                - The patch residual vector
5977a50e09dSPatrick Farrell . facetIS          - An array of the facet numbers
59820f4b53cSBarry Smith . n                - The size of `dofsArray`
5997a50e09dSPatrick Farrell . dofsArray        - The dofmap for the dofs to be solved for
6007a50e09dSPatrick Farrell . dofsArrayWithAll - The dofmap for all dofs on the patch
6017a50e09dSPatrick Farrell - ctx              - The user context
6027a50e09dSPatrick Farrell 
60359109abcSLawrence Mitchell   Level: advanced
60459109abcSLawrence Mitchell 
605f1580f4eSBarry Smith   Note:
60604c3f3b8SBarry Smith   The entries of `f` (the output residual vector) have been set to zero before the call.
60759109abcSLawrence Mitchell 
608db781477SPatrick Sanan .seealso: `PCPatchSetComputeOperator()`, `PCPatchGetComputeOperator()`, `PCPatchSetDiscretisationInfo()`, `PCPatchSetComputeFunction()`
60959109abcSLawrence Mitchell @*/
61004c3f3b8SBarry 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)
611d71ae5a4SJacob Faibussowitsch {
61259109abcSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *)pc->data;
61359109abcSLawrence Mitchell 
61459109abcSLawrence Mitchell   PetscFunctionBegin;
61559109abcSLawrence Mitchell   patch->usercomputefintfacet    = func;
61659109abcSLawrence Mitchell   patch->usercomputefintfacetctx = ctx;
6173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
61859109abcSLawrence Mitchell }
61959109abcSLawrence Mitchell 
62059109abcSLawrence Mitchell /*@C
62104c3f3b8SBarry Smith   PCPatchSetComputeOperator - Set the callback function used to compute patch matrices
6225f824522SMatthew G. Knepley 
62320f4b53cSBarry Smith   Logically Collective
6247a50e09dSPatrick Farrell 
6255f824522SMatthew G. Knepley   Input Parameters:
62620f4b53cSBarry Smith + pc   - The `PC`
62704c3f3b8SBarry Smith . func - The callback function
6285f824522SMatthew G. Knepley - ctx  - The user context
6295f824522SMatthew G. Knepley 
63020f4b53cSBarry Smith   Calling sequence of `func`:
63120f4b53cSBarry Smith + pc               - The `PC`
6327a50e09dSPatrick Farrell . point            - The point
6337a50e09dSPatrick Farrell . x                - The input solution (not used in linear problems)
6347a50e09dSPatrick Farrell . mat              - The patch matrix
63504c3f3b8SBarry Smith . facetIS          - An array of the cell numbers
63620f4b53cSBarry Smith . n                - The size of `dofsArray`
6377a50e09dSPatrick Farrell . dofsArray        - The dofmap for the dofs to be solved for
6387a50e09dSPatrick Farrell . dofsArrayWithAll - The dofmap for all dofs on the patch
6397a50e09dSPatrick Farrell - ctx              - The user context
6407a50e09dSPatrick Farrell 
6415f824522SMatthew G. Knepley   Level: advanced
6425f824522SMatthew G. Knepley 
643f1580f4eSBarry Smith   Note:
6447a50e09dSPatrick Farrell   The matrix entries have been set to zero before the call.
6455f824522SMatthew G. Knepley 
646db781477SPatrick Sanan .seealso: `PCPatchGetComputeOperator()`, `PCPatchSetComputeFunction()`, `PCPatchSetDiscretisationInfo()`
6475f824522SMatthew G. Knepley @*/
64804c3f3b8SBarry 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)
649d71ae5a4SJacob Faibussowitsch {
6504bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
6514bbf5ea8SMatthew G. Knepley 
6524bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
6534bbf5ea8SMatthew G. Knepley   patch->usercomputeop    = func;
654723f9013SMatthew G. Knepley   patch->usercomputeopctx = ctx;
6553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6564bbf5ea8SMatthew G. Knepley }
6574bbf5ea8SMatthew G. Knepley 
65859109abcSLawrence Mitchell /*@C
65959109abcSLawrence Mitchell 
66004c3f3b8SBarry Smith   PCPatchSetComputeOperatorInteriorFacets - Set the callback function used to compute facet integrals for patch matrices
66159109abcSLawrence Mitchell 
66220f4b53cSBarry Smith   Logically Collective
66399b7e5c6SPatrick Farrell 
66459109abcSLawrence Mitchell   Input Parameters:
66520f4b53cSBarry Smith + pc   - The `PC`
66604c3f3b8SBarry Smith . func - The callback function
66759109abcSLawrence Mitchell - ctx  - The user context
66859109abcSLawrence Mitchell 
66920f4b53cSBarry Smith   Calling sequence of `func`:
67020f4b53cSBarry Smith + pc               - The `PC`
6717a50e09dSPatrick Farrell . point            - The point
6727a50e09dSPatrick Farrell . x                - The input solution (not used in linear problems)
6737a50e09dSPatrick Farrell . mat              - The patch matrix
6747a50e09dSPatrick Farrell . facetIS          - An array of the facet numbers
67520f4b53cSBarry Smith . n                - The size of `dofsArray`
6767a50e09dSPatrick Farrell . dofsArray        - The dofmap for the dofs to be solved for
6777a50e09dSPatrick Farrell . dofsArrayWithAll - The dofmap for all dofs on the patch
6787a50e09dSPatrick Farrell - ctx              - The user context
6797a50e09dSPatrick Farrell 
68059109abcSLawrence Mitchell   Level: advanced
68159109abcSLawrence Mitchell 
682f1580f4eSBarry Smith   Note:
6837a50e09dSPatrick Farrell   The matrix entries have been set to zero before the call.
68459109abcSLawrence Mitchell 
685db781477SPatrick Sanan .seealso: `PCPatchGetComputeOperator()`, `PCPatchSetComputeFunction()`, `PCPatchSetDiscretisationInfo()`
68659109abcSLawrence Mitchell @*/
68704c3f3b8SBarry 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)
688d71ae5a4SJacob Faibussowitsch {
68959109abcSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *)pc->data;
69059109abcSLawrence Mitchell 
69159109abcSLawrence Mitchell   PetscFunctionBegin;
69259109abcSLawrence Mitchell   patch->usercomputeopintfacet    = func;
69359109abcSLawrence Mitchell   patch->usercomputeopintfacetctx = ctx;
6943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
69559109abcSLawrence Mitchell }
69659109abcSLawrence Mitchell 
6974bbf5ea8SMatthew G. Knepley /* On entry, ht contains the topological entities whose dofs we are responsible for solving for;
6984bbf5ea8SMatthew G. Knepley    on exit, cht contains all the topological entities we need to compute their residuals.
6994bbf5ea8SMatthew G. Knepley    In full generality this should incorporate knowledge of the sparsity pattern of the matrix;
7004bbf5ea8SMatthew G. Knepley    here we assume a standard FE sparsity pattern.*/
7014bbf5ea8SMatthew G. Knepley /* TODO: Use DMPlexGetAdjacency() */
702d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCompleteCellPatch(PC pc, PetscHSetI ht, PetscHSetI cht)
703d71ae5a4SJacob Faibussowitsch {
704b6bb21d1SLawrence Mitchell   DM            dm, plex;
705bc7fa33aSFlorian Wechsung   PC_PATCH     *patch = (PC_PATCH *)pc->data;
7061b68eb51SMatthew G. Knepley   PetscHashIter hi;
7074bbf5ea8SMatthew G. Knepley   PetscInt      point;
7084bbf5ea8SMatthew G. Knepley   PetscInt     *star = NULL, *closure = NULL;
7094c954380SMatthew G. Knepley   PetscInt      ignoredim, iStart = 0, iEnd = -1, starSize, closureSize, si, ci;
710bc7fa33aSFlorian Wechsung   PetscInt     *fStar = NULL, *fClosure = NULL;
711bc7fa33aSFlorian Wechsung   PetscInt      fBegin, fEnd, fsi, fci, fStarSize, fClosureSize;
7124bbf5ea8SMatthew G. Knepley 
7134bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
7149566063dSJacob Faibussowitsch   PetscCall(PCGetDM(pc, &dm));
7159566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
716b6bb21d1SLawrence Mitchell   dm = plex;
7179566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fBegin, &fEnd));
7189566063dSJacob Faibussowitsch   PetscCall(PCPatchGetIgnoreDim(pc, &ignoredim));
7199566063dSJacob Faibussowitsch   if (ignoredim >= 0) PetscCall(DMPlexGetDepthStratum(dm, ignoredim, &iStart, &iEnd));
7209566063dSJacob Faibussowitsch   PetscCall(PetscHSetIClear(cht));
7211b68eb51SMatthew G. Knepley   PetscHashIterBegin(ht, hi);
7221b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(ht, hi)) {
7231b68eb51SMatthew G. Knepley     PetscHashIterGetKey(ht, hi, point);
7241b68eb51SMatthew G. Knepley     PetscHashIterNext(ht, hi);
7254bbf5ea8SMatthew G. Knepley 
7264bbf5ea8SMatthew G. Knepley     /* Loop over all the cells that this point connects to */
7279566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star));
7285f824522SMatthew G. Knepley     for (si = 0; si < starSize * 2; si += 2) {
7294c954380SMatthew G. Knepley       const PetscInt ownedpoint = star[si];
7305f824522SMatthew G. Knepley       /* TODO Check for point in cht before running through closure again */
7314bbf5ea8SMatthew G. Knepley       /* now loop over all entities in the closure of that cell */
7329566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, ownedpoint, PETSC_TRUE, &closureSize, &closure));
7335f824522SMatthew G. Knepley       for (ci = 0; ci < closureSize * 2; ci += 2) {
7344c954380SMatthew G. Knepley         const PetscInt seenpoint = closure[ci];
7355f824522SMatthew G. Knepley         if (ignoredim >= 0 && seenpoint >= iStart && seenpoint < iEnd) continue;
7369566063dSJacob Faibussowitsch         PetscCall(PetscHSetIAdd(cht, seenpoint));
737bc7fa33aSFlorian Wechsung         /* Facet integrals couple dofs across facets, so in that case for each of
738f1580f4eSBarry Smith           the facets we need to add all dofs on the other side of the facet to
739f1580f4eSBarry Smith           the seen dofs. */
740bc7fa33aSFlorian Wechsung         if (patch->usercomputeopintfacet) {
741bc7fa33aSFlorian Wechsung           if (fBegin <= seenpoint && seenpoint < fEnd) {
7429566063dSJacob Faibussowitsch             PetscCall(DMPlexGetTransitiveClosure(dm, seenpoint, PETSC_FALSE, &fStarSize, &fStar));
743bc7fa33aSFlorian Wechsung             for (fsi = 0; fsi < fStarSize * 2; fsi += 2) {
7449566063dSJacob Faibussowitsch               PetscCall(DMPlexGetTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, &fClosureSize, &fClosure));
74548a46eb9SPierre Jolivet               for (fci = 0; fci < fClosureSize * 2; fci += 2) PetscCall(PetscHSetIAdd(cht, fClosure[fci]));
7469566063dSJacob Faibussowitsch               PetscCall(DMPlexRestoreTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, NULL, &fClosure));
747bc7fa33aSFlorian Wechsung             }
7489566063dSJacob Faibussowitsch             PetscCall(DMPlexRestoreTransitiveClosure(dm, seenpoint, PETSC_FALSE, NULL, &fStar));
749bc7fa33aSFlorian Wechsung           }
750bc7fa33aSFlorian Wechsung         }
7514bbf5ea8SMatthew G. Knepley       }
7529566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, ownedpoint, PETSC_TRUE, NULL, &closure));
7534bbf5ea8SMatthew G. Knepley     }
7549566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, NULL, &star));
7554bbf5ea8SMatthew G. Knepley   }
7569566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
7573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7585f824522SMatthew G. Knepley }
7595f824522SMatthew G. Knepley 
760d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchGetGlobalDofs(PC pc, PetscSection dofSection[], PetscInt f, PetscBool combined, PetscInt p, PetscInt *dof, PetscInt *off)
761d71ae5a4SJacob Faibussowitsch {
7625f824522SMatthew G. Knepley   PetscFunctionBegin;
7635f824522SMatthew G. Knepley   if (combined) {
7645f824522SMatthew G. Knepley     if (f < 0) {
7659566063dSJacob Faibussowitsch       if (dof) PetscCall(PetscSectionGetDof(dofSection[0], p, dof));
7669566063dSJacob Faibussowitsch       if (off) PetscCall(PetscSectionGetOffset(dofSection[0], p, off));
7675f824522SMatthew G. Knepley     } else {
7689566063dSJacob Faibussowitsch       if (dof) PetscCall(PetscSectionGetFieldDof(dofSection[0], p, f, dof));
7699566063dSJacob Faibussowitsch       if (off) PetscCall(PetscSectionGetFieldOffset(dofSection[0], p, f, off));
7705f824522SMatthew G. Knepley     }
7715f824522SMatthew G. Knepley   } else {
7725f824522SMatthew G. Knepley     if (f < 0) {
7735f824522SMatthew G. Knepley       PC_PATCH *patch = (PC_PATCH *)pc->data;
7745f824522SMatthew G. Knepley       PetscInt  fdof, g;
7755f824522SMatthew G. Knepley 
7765f824522SMatthew G. Knepley       if (dof) {
7775f824522SMatthew G. Knepley         *dof = 0;
7785f824522SMatthew G. Knepley         for (g = 0; g < patch->nsubspaces; ++g) {
7799566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetDof(dofSection[g], p, &fdof));
7805f824522SMatthew G. Knepley           *dof += fdof;
7815f824522SMatthew G. Knepley         }
7825f824522SMatthew G. Knepley       }
783624e31c3SLawrence Mitchell       if (off) {
784624e31c3SLawrence Mitchell         *off = 0;
785624e31c3SLawrence Mitchell         for (g = 0; g < patch->nsubspaces; ++g) {
7869566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetOffset(dofSection[g], p, &fdof));
787624e31c3SLawrence Mitchell           *off += fdof;
788624e31c3SLawrence Mitchell         }
789624e31c3SLawrence Mitchell       }
7905f824522SMatthew G. Knepley     } else {
7919566063dSJacob Faibussowitsch       if (dof) PetscCall(PetscSectionGetDof(dofSection[f], p, dof));
7929566063dSJacob Faibussowitsch       if (off) PetscCall(PetscSectionGetOffset(dofSection[f], p, off));
7935f824522SMatthew G. Knepley     }
7945f824522SMatthew G. Knepley   }
7953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7964bbf5ea8SMatthew G. Knepley }
7974bbf5ea8SMatthew G. Knepley 
7984bbf5ea8SMatthew G. Knepley /* Given a hash table with a set of topological entities (pts), compute the degrees of
7994bbf5ea8SMatthew G. Knepley    freedom in global concatenated numbering on those entities.
8004bbf5ea8SMatthew G. Knepley    For Vanka smoothing, this needs to do something special: ignore dofs of the
8014bbf5ea8SMatthew G. Knepley    constraint subspace on entities that aren't the base entity we're building the patch
8024bbf5ea8SMatthew G. Knepley    around. */
803d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchGetPointDofs(PC pc, PetscHSetI pts, PetscHSetI dofs, PetscInt base, PetscHSetI *subspaces_to_exclude)
804d71ae5a4SJacob Faibussowitsch {
8055f824522SMatthew G. Knepley   PC_PATCH     *patch = (PC_PATCH *)pc->data;
8061b68eb51SMatthew G. Knepley   PetscHashIter hi;
8074bbf5ea8SMatthew G. Knepley   PetscInt      ldof, loff;
8084bbf5ea8SMatthew G. Knepley   PetscInt      k, p;
8094bbf5ea8SMatthew G. Knepley 
8104bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
8119566063dSJacob Faibussowitsch   PetscCall(PetscHSetIClear(dofs));
8124bbf5ea8SMatthew G. Knepley   for (k = 0; k < patch->nsubspaces; ++k) {
8134bbf5ea8SMatthew G. Knepley     PetscInt subspaceOffset = patch->subspaceOffsets[k];
8144bbf5ea8SMatthew G. Knepley     PetscInt bs             = patch->bs[k];
8154bbf5ea8SMatthew G. Knepley     PetscInt j, l;
8164bbf5ea8SMatthew G. Knepley 
817e4c66b91SPatrick Farrell     if (subspaces_to_exclude != NULL) {
818e4c66b91SPatrick Farrell       PetscBool should_exclude_k = PETSC_FALSE;
8199566063dSJacob Faibussowitsch       PetscCall(PetscHSetIHas(*subspaces_to_exclude, k, &should_exclude_k));
820e4c66b91SPatrick Farrell       if (should_exclude_k) {
8214bbf5ea8SMatthew G. Knepley         /* only get this subspace dofs at the base entity, not any others */
8229566063dSJacob Faibussowitsch         PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, base, &ldof, &loff));
8234bbf5ea8SMatthew G. Knepley         if (0 == ldof) continue;
8244bbf5ea8SMatthew G. Knepley         for (j = loff; j < ldof + loff; ++j) {
8254bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
8264bbf5ea8SMatthew G. Knepley             PetscInt dof = bs * j + l + subspaceOffset;
8279566063dSJacob Faibussowitsch             PetscCall(PetscHSetIAdd(dofs, dof));
8284bbf5ea8SMatthew G. Knepley           }
8294bbf5ea8SMatthew G. Knepley         }
8304bbf5ea8SMatthew G. Knepley         continue; /* skip the other dofs of this subspace */
8314bbf5ea8SMatthew G. Knepley       }
832e4c66b91SPatrick Farrell     }
8334bbf5ea8SMatthew G. Knepley 
8341b68eb51SMatthew G. Knepley     PetscHashIterBegin(pts, hi);
8351b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(pts, hi)) {
8361b68eb51SMatthew G. Knepley       PetscHashIterGetKey(pts, hi, p);
8371b68eb51SMatthew G. Knepley       PetscHashIterNext(pts, hi);
8389566063dSJacob Faibussowitsch       PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, p, &ldof, &loff));
8394bbf5ea8SMatthew G. Knepley       if (0 == ldof) continue;
8404bbf5ea8SMatthew G. Knepley       for (j = loff; j < ldof + loff; ++j) {
8414bbf5ea8SMatthew G. Knepley         for (l = 0; l < bs; ++l) {
8424bbf5ea8SMatthew G. Knepley           PetscInt dof = bs * j + l + subspaceOffset;
8439566063dSJacob Faibussowitsch           PetscCall(PetscHSetIAdd(dofs, dof));
8444bbf5ea8SMatthew G. Knepley         }
8454bbf5ea8SMatthew G. Knepley       }
8464bbf5ea8SMatthew G. Knepley     }
8474bbf5ea8SMatthew G. Knepley   }
8483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8494bbf5ea8SMatthew G. Knepley }
8504bbf5ea8SMatthew G. Knepley 
8514bbf5ea8SMatthew G. Knepley /* Given two hash tables A and B, compute the keys in B that are not in A, and put them in C */
852d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchComputeSetDifference_Private(PetscHSetI A, PetscHSetI B, PetscHSetI C)
853d71ae5a4SJacob Faibussowitsch {
8541b68eb51SMatthew G. Knepley   PetscHashIter hi;
8551b68eb51SMatthew G. Knepley   PetscInt      key;
8564bbf5ea8SMatthew G. Knepley   PetscBool     flg;
8574bbf5ea8SMatthew G. Knepley 
8584bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
8599566063dSJacob Faibussowitsch   PetscCall(PetscHSetIClear(C));
8601b68eb51SMatthew G. Knepley   PetscHashIterBegin(B, hi);
8611b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(B, hi)) {
8621b68eb51SMatthew G. Knepley     PetscHashIterGetKey(B, hi, key);
8631b68eb51SMatthew G. Knepley     PetscHashIterNext(B, hi);
8649566063dSJacob Faibussowitsch     PetscCall(PetscHSetIHas(A, key, &flg));
8659566063dSJacob Faibussowitsch     if (!flg) PetscCall(PetscHSetIAdd(C, key));
8664bbf5ea8SMatthew G. Knepley   }
8673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8684bbf5ea8SMatthew G. Knepley }
8694bbf5ea8SMatthew G. Knepley 
87004c3f3b8SBarry Smith // PetscClangLinter pragma disable: -fdoc-sowing-chars
8714bbf5ea8SMatthew G. Knepley /*
872f1580f4eSBarry Smith   PCPatchCreateCellPatches - create patches.
873f1580f4eSBarry Smith 
874f1580f4eSBarry Smith   Input Parameter:
875f1580f4eSBarry Smith   . dm - The DMPlex object defining the mesh
876f1580f4eSBarry Smith 
877f1580f4eSBarry Smith   Output Parameters:
878f1580f4eSBarry Smith   + cellCounts  - Section with counts of cells around each vertex
879f1580f4eSBarry Smith   . cells       - IS of the cell point indices of cells in each patch
880f1580f4eSBarry Smith   . pointCounts - Section with counts of cells around each vertex
881f1580f4eSBarry Smith   - point       - IS of the cell point indices of cells in each patch
8824bbf5ea8SMatthew G. Knepley  */
883d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCreateCellPatches(PC pc)
884d71ae5a4SJacob Faibussowitsch {
8854bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *)pc->data;
8865f824522SMatthew G. Knepley   DMLabel         ghost = NULL;
8874bbf5ea8SMatthew G. Knepley   DM              dm, plex;
88876ce8f1aSJose E. Roman   PetscHSetI      ht = NULL, cht = NULL;
8890e126c0bSLawrence Mitchell   PetscSection    cellCounts, pointCounts, intFacetCounts, extFacetCounts;
890eb62eeaaSLawrence Mitchell   PetscInt       *cellsArray, *pointsArray, *intFacetsArray, *extFacetsArray, *intFacetsToPatchCell;
8910e126c0bSLawrence Mitchell   PetscInt        numCells, numPoints, numIntFacets, numExtFacets;
8925f824522SMatthew G. Knepley   const PetscInt *leaves;
89333cbca70SPatrick Farrell   PetscInt        nleaves, pStart, pEnd, cStart, cEnd, vStart, vEnd, fStart, fEnd, v;
8945f824522SMatthew G. Knepley   PetscBool       isFiredrake;
8954bbf5ea8SMatthew G. Knepley 
8964bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
8974bbf5ea8SMatthew G. Knepley   /* Used to keep track of the cells in the patch. */
8989566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&ht));
8999566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&cht));
9004bbf5ea8SMatthew G. Knepley 
9019566063dSJacob Faibussowitsch   PetscCall(PCGetDM(pc, &dm));
90228b400f6SJacob Faibussowitsch   PetscCheck(dm, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "DM not yet set on patch PC");
9039566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
904b6bb21d1SLawrence Mitchell   dm = plex;
9059566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
9069566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
9074bbf5ea8SMatthew G. Knepley 
9084bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
9099566063dSJacob Faibussowitsch     PetscCall(patch->userpatchconstructionop(pc, &patch->npatch, &patch->userIS, &patch->iterationSet, patch->userpatchconstructctx));
9109371c9d4SSatish Balay     vStart = 0;
9119371c9d4SSatish Balay     vEnd   = patch->npatch;
912e5b9877fSPatrick Farrell   } else if (patch->ctype == PC_PATCH_PARDECOMP) {
9139371c9d4SSatish Balay     vStart = 0;
9149371c9d4SSatish Balay     vEnd   = 1;
9155f824522SMatthew G. Knepley   } else if (patch->codim < 0) {
9169566063dSJacob Faibussowitsch     if (patch->dim < 0) PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
9179566063dSJacob Faibussowitsch     else PetscCall(DMPlexGetDepthStratum(dm, patch->dim, &vStart, &vEnd));
9189566063dSJacob Faibussowitsch   } else PetscCall(DMPlexGetHeightStratum(dm, patch->codim, &vStart, &vEnd));
9195f824522SMatthew G. Knepley   patch->npatch = vEnd - vStart;
9204bbf5ea8SMatthew G. Knepley 
9214bbf5ea8SMatthew G. Knepley   /* These labels mark the owned points.  We only create patches around points that this process owns. */
9229566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, "pyop2_ghost", &isFiredrake));
9235f824522SMatthew G. Knepley   if (isFiredrake) {
9249566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, "pyop2_ghost", &ghost));
9259566063dSJacob Faibussowitsch     PetscCall(DMLabelCreateIndex(ghost, pStart, pEnd));
9265f824522SMatthew G. Knepley   } else {
9275f824522SMatthew G. Knepley     PetscSF sf;
9285f824522SMatthew G. Knepley 
9299566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sf));
9309566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL));
9315f824522SMatthew G. Knepley     nleaves = PetscMax(nleaves, 0);
9325f824522SMatthew G. Knepley   }
9334bbf5ea8SMatthew G. Knepley 
9349566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->cellCounts));
9359566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->cellCounts, "Patch Cell Layout"));
9364bbf5ea8SMatthew G. Knepley   cellCounts = patch->cellCounts;
9379566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(cellCounts, vStart, vEnd));
9389566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->pointCounts));
9399566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->pointCounts, "Patch Point Layout"));
9405f824522SMatthew G. Knepley   pointCounts = patch->pointCounts;
9419566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(pointCounts, vStart, vEnd));
9429566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->extFacetCounts));
9439566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->extFacetCounts, "Patch Exterior Facet Layout"));
9440e126c0bSLawrence Mitchell   extFacetCounts = patch->extFacetCounts;
9459566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(extFacetCounts, vStart, vEnd));
9469566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->intFacetCounts));
9479566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->intFacetCounts, "Patch Interior Facet Layout"));
9480e126c0bSLawrence Mitchell   intFacetCounts = patch->intFacetCounts;
9499566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(intFacetCounts, vStart, vEnd));
9505f824522SMatthew G. Knepley   /* Count cells and points in the patch surrounding each entity */
9519566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
9524bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
9531b68eb51SMatthew G. Knepley     PetscHashIter hi;
9545f824522SMatthew G. Knepley     PetscInt      chtSize, loc = -1;
9555f824522SMatthew G. Knepley     PetscBool     flg;
9564bbf5ea8SMatthew G. Knepley 
957b525f888SPatrick Farrell     if (!patch->user_patches && patch->ctype != PC_PATCH_PARDECOMP) {
9589566063dSJacob Faibussowitsch       if (ghost) PetscCall(DMLabelHasPoint(ghost, v, &flg));
9599371c9d4SSatish Balay       else {
9609371c9d4SSatish Balay         PetscCall(PetscFindInt(v, nleaves, leaves, &loc));
9619371c9d4SSatish Balay         flg = loc >= 0 ? PETSC_TRUE : PETSC_FALSE;
9629371c9d4SSatish Balay       }
9634bbf5ea8SMatthew G. Knepley       /* Not an owned entity, don't make a cell patch. */
9644bbf5ea8SMatthew G. Knepley       if (flg) continue;
9654bbf5ea8SMatthew G. Knepley     }
9664bbf5ea8SMatthew G. Knepley 
9679566063dSJacob Faibussowitsch     PetscCall(patch->patchconstructop((void *)patch, dm, v, ht));
9689566063dSJacob Faibussowitsch     PetscCall(PCPatchCompleteCellPatch(pc, ht, cht));
9699566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(cht, &chtSize));
9704bbf5ea8SMatthew G. Knepley     /* empty patch, continue */
9714bbf5ea8SMatthew G. Knepley     if (chtSize == 0) continue;
9724bbf5ea8SMatthew G. Knepley 
9734bbf5ea8SMatthew G. Knepley     /* safe because size(cht) > 0 from above */
9741b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
9751b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
9765f824522SMatthew G. Knepley       PetscInt point, pdof;
9774bbf5ea8SMatthew G. Knepley 
9781b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
9790e126c0bSLawrence Mitchell       if (fStart <= point && point < fEnd) {
9800e126c0bSLawrence Mitchell         const PetscInt *support;
9810e126c0bSLawrence Mitchell         PetscInt        supportSize, p;
9820e126c0bSLawrence Mitchell         PetscBool       interior = PETSC_TRUE;
9839566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
9849566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
9850e126c0bSLawrence Mitchell         if (supportSize == 1) {
9860e126c0bSLawrence Mitchell           interior = PETSC_FALSE;
9870e126c0bSLawrence Mitchell         } else {
9880e126c0bSLawrence Mitchell           for (p = 0; p < supportSize; p++) {
9890e126c0bSLawrence Mitchell             PetscBool found;
9900e126c0bSLawrence Mitchell             /* FIXME: can I do this while iterating over cht? */
9919566063dSJacob Faibussowitsch             PetscCall(PetscHSetIHas(cht, support[p], &found));
9920e126c0bSLawrence Mitchell             if (!found) {
9930e126c0bSLawrence Mitchell               interior = PETSC_FALSE;
9940e126c0bSLawrence Mitchell               break;
9950e126c0bSLawrence Mitchell             }
9960e126c0bSLawrence Mitchell           }
9970e126c0bSLawrence Mitchell         }
9980e126c0bSLawrence Mitchell         if (interior) {
9999566063dSJacob Faibussowitsch           PetscCall(PetscSectionAddDof(intFacetCounts, v, 1));
10000e126c0bSLawrence Mitchell         } else {
10019566063dSJacob Faibussowitsch           PetscCall(PetscSectionAddDof(extFacetCounts, v, 1));
10020e126c0bSLawrence Mitchell         }
10030e126c0bSLawrence Mitchell       }
10049566063dSJacob Faibussowitsch       PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL));
10059566063dSJacob Faibussowitsch       if (pdof) PetscCall(PetscSectionAddDof(pointCounts, v, 1));
10069566063dSJacob Faibussowitsch       if (point >= cStart && point < cEnd) PetscCall(PetscSectionAddDof(cellCounts, v, 1));
10071b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
10084bbf5ea8SMatthew G. Knepley     }
10094bbf5ea8SMatthew G. Knepley   }
10109566063dSJacob Faibussowitsch   if (isFiredrake) PetscCall(DMLabelDestroyIndex(ghost));
10114bbf5ea8SMatthew G. Knepley 
10129566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(cellCounts));
10139566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(cellCounts, &numCells));
10149566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numCells, &cellsArray));
10159566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(pointCounts));
10169566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(pointCounts, &numPoints));
10179566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numPoints, &pointsArray));
10184bbf5ea8SMatthew G. Knepley 
10199566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(intFacetCounts));
10209566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(extFacetCounts));
10219566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(intFacetCounts, &numIntFacets));
10229566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(extFacetCounts, &numExtFacets));
10239566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numIntFacets, &intFacetsArray));
10249566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numIntFacets * 2, &intFacetsToPatchCell));
10259566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numExtFacets, &extFacetsArray));
10260e126c0bSLawrence Mitchell 
10274bbf5ea8SMatthew G. Knepley   /* Now that we know how much space we need, run through again and actually remember the cells. */
10284bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; v++) {
10291b68eb51SMatthew G. Knepley     PetscHashIter hi;
10300e126c0bSLawrence Mitchell     PetscInt      dof, off, cdof, coff, efdof, efoff, ifdof, ifoff, pdof, n = 0, cn = 0, ifn = 0, efn = 0;
10314bbf5ea8SMatthew G. Knepley 
10329566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(pointCounts, v, &dof));
10339566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(pointCounts, v, &off));
10349566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(cellCounts, v, &cdof));
10359566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(cellCounts, v, &coff));
10369566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(intFacetCounts, v, &ifdof));
10379566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(intFacetCounts, v, &ifoff));
10389566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(extFacetCounts, v, &efdof));
10399566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(extFacetCounts, v, &efoff));
10405f824522SMatthew G. Knepley     if (dof <= 0) continue;
10419566063dSJacob Faibussowitsch     PetscCall(patch->patchconstructop((void *)patch, dm, v, ht));
10429566063dSJacob Faibussowitsch     PetscCall(PCPatchCompleteCellPatch(pc, ht, cht));
10431b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
10441b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
10454bbf5ea8SMatthew G. Knepley       PetscInt point;
10464bbf5ea8SMatthew G. Knepley 
10471b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
10480e126c0bSLawrence Mitchell       if (fStart <= point && point < fEnd) {
10490e126c0bSLawrence Mitchell         const PetscInt *support;
10500e126c0bSLawrence Mitchell         PetscInt        supportSize, p;
10510e126c0bSLawrence Mitchell         PetscBool       interior = PETSC_TRUE;
10529566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
10539566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
10540e126c0bSLawrence Mitchell         if (supportSize == 1) {
10550e126c0bSLawrence Mitchell           interior = PETSC_FALSE;
10560e126c0bSLawrence Mitchell         } else {
10570e126c0bSLawrence Mitchell           for (p = 0; p < supportSize; p++) {
10580e126c0bSLawrence Mitchell             PetscBool found;
10590e126c0bSLawrence Mitchell             /* FIXME: can I do this while iterating over cht? */
10609566063dSJacob Faibussowitsch             PetscCall(PetscHSetIHas(cht, support[p], &found));
10610e126c0bSLawrence Mitchell             if (!found) {
10620e126c0bSLawrence Mitchell               interior = PETSC_FALSE;
10630e126c0bSLawrence Mitchell               break;
10640e126c0bSLawrence Mitchell             }
10650e126c0bSLawrence Mitchell           }
10660e126c0bSLawrence Mitchell         }
10670e126c0bSLawrence Mitchell         if (interior) {
106844b625f7SLawrence Mitchell           intFacetsToPatchCell[2 * (ifoff + ifn)]     = support[0];
106944b625f7SLawrence Mitchell           intFacetsToPatchCell[2 * (ifoff + ifn) + 1] = support[1];
10700e126c0bSLawrence Mitchell           intFacetsArray[ifoff + ifn++]               = point;
10710e126c0bSLawrence Mitchell         } else {
10720e126c0bSLawrence Mitchell           extFacetsArray[efoff + efn++] = point;
10730e126c0bSLawrence Mitchell         }
10740e126c0bSLawrence Mitchell       }
10759566063dSJacob Faibussowitsch       PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL));
1076ad540459SPierre Jolivet       if (pdof) pointsArray[off + n++] = point;
1077ad540459SPierre Jolivet       if (point >= cStart && point < cEnd) cellsArray[coff + cn++] = point;
10781b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
10794bbf5ea8SMatthew G. Knepley     }
108063a3b9bcSJacob 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);
108163a3b9bcSJacob 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);
108263a3b9bcSJacob 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);
108363a3b9bcSJacob 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);
1084eb62eeaaSLawrence Mitchell 
1085eb62eeaaSLawrence Mitchell     for (ifn = 0; ifn < ifdof; ifn++) {
108644b625f7SLawrence Mitchell       PetscInt  cell0  = intFacetsToPatchCell[2 * (ifoff + ifn)];
108744b625f7SLawrence Mitchell       PetscInt  cell1  = intFacetsToPatchCell[2 * (ifoff + ifn) + 1];
1088eb62eeaaSLawrence Mitchell       PetscBool found0 = PETSC_FALSE, found1 = PETSC_FALSE;
1089eb62eeaaSLawrence Mitchell       for (n = 0; n < cdof; n++) {
10907c54fef0SLawrence Mitchell         if (!found0 && cell0 == cellsArray[coff + n]) {
1091c3faab33SLawrence Mitchell           intFacetsToPatchCell[2 * (ifoff + ifn)] = n;
1092eb62eeaaSLawrence Mitchell           found0                                  = PETSC_TRUE;
1093eb62eeaaSLawrence Mitchell         }
10947c54fef0SLawrence Mitchell         if (!found1 && cell1 == cellsArray[coff + n]) {
1095c3faab33SLawrence Mitchell           intFacetsToPatchCell[2 * (ifoff + ifn) + 1] = n;
109680fc4459SLawrence Mitchell           found1                                      = PETSC_TRUE;
1097eb62eeaaSLawrence Mitchell         }
1098eb62eeaaSLawrence Mitchell         if (found0 && found1) break;
1099eb62eeaaSLawrence Mitchell       }
11007827d75bSBarry Smith       PetscCheck(found0 && found1, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Didn't manage to find local point numbers for facet support");
1101eb62eeaaSLawrence Mitchell     }
11024bbf5ea8SMatthew G. Knepley   }
11039566063dSJacob Faibussowitsch   PetscCall(PetscHSetIDestroy(&ht));
11049566063dSJacob Faibussowitsch   PetscCall(PetscHSetIDestroy(&cht));
11055f824522SMatthew G. Knepley 
11069566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numCells, cellsArray, PETSC_OWN_POINTER, &patch->cells));
11079566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->cells, "Patch Cells"));
11085f824522SMatthew G. Knepley   if (patch->viewCells) {
11099566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->cellCounts, patch->viewerCells, patch->formatCells));
11109566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->cells, patch->viewerCells, patch->formatCells));
11115f824522SMatthew G. Knepley   }
11129566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray, PETSC_OWN_POINTER, &patch->intFacets));
11139566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->intFacets, "Patch Interior Facets"));
11149566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 2 * numIntFacets, intFacetsToPatchCell, PETSC_OWN_POINTER, &patch->intFacetsToPatchCell));
11159566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->intFacetsToPatchCell, "Patch Interior Facets local support"));
11160e126c0bSLawrence Mitchell   if (patch->viewIntFacets) {
11179566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->intFacetCounts, patch->viewerIntFacets, patch->formatIntFacets));
11189566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->intFacets, patch->viewerIntFacets, patch->formatIntFacets));
11199566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->intFacetsToPatchCell, patch->viewerIntFacets, patch->formatIntFacets));
11200e126c0bSLawrence Mitchell   }
11219566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numExtFacets, extFacetsArray, PETSC_OWN_POINTER, &patch->extFacets));
11229566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->extFacets, "Patch Exterior Facets"));
11230e126c0bSLawrence Mitchell   if (patch->viewExtFacets) {
11249566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->extFacetCounts, patch->viewerExtFacets, patch->formatExtFacets));
11259566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->extFacets, patch->viewerExtFacets, patch->formatExtFacets));
11260e126c0bSLawrence Mitchell   }
11279566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints, pointsArray, PETSC_OWN_POINTER, &patch->points));
11289566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->points, "Patch Points"));
11295f824522SMatthew G. Knepley   if (patch->viewPoints) {
11309566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->pointCounts, patch->viewerPoints, patch->formatPoints));
11319566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->points, patch->viewerPoints, patch->formatPoints));
11325f824522SMatthew G. Knepley   }
11339566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
11343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11354bbf5ea8SMatthew G. Knepley }
11364bbf5ea8SMatthew G. Knepley 
11374bbf5ea8SMatthew G. Knepley /*
1138f1580f4eSBarry Smith   PCPatchCreateCellPatchDiscretisationInfo - Build the dof maps for cell patches
1139f1580f4eSBarry Smith 
1140f1580f4eSBarry Smith   Input Parameters:
1141f1580f4eSBarry Smith   + dm - The DMPlex object defining the mesh
1142f1580f4eSBarry Smith   . cellCounts - Section with counts of cells around each vertex
1143f1580f4eSBarry Smith   . cells - IS of the cell point indices of cells in each patch
1144f1580f4eSBarry Smith   . cellNumbering - Section mapping plex cell points to Firedrake cell indices.
1145f1580f4eSBarry Smith   . nodesPerCell - number of nodes per cell.
1146f1580f4eSBarry Smith   - cellNodeMap - map from cells to node indices (nodesPerCell * numCells)
1147f1580f4eSBarry Smith 
1148f1580f4eSBarry Smith   Output Parameters:
1149f1580f4eSBarry Smith   + dofs - IS of local dof numbers of each cell in the patch, where local is a patch local numbering
1150f1580f4eSBarry Smith   . gtolCounts - Section with counts of dofs per cell patch
1151f1580f4eSBarry Smith   - gtol - IS mapping from global dofs to local dofs for each patch.
11524bbf5ea8SMatthew G. Knepley  */
1153d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCreateCellPatchDiscretisationInfo(PC pc)
1154d71ae5a4SJacob Faibussowitsch {
11554bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch       = (PC_PATCH *)pc->data;
11564bbf5ea8SMatthew G. Knepley   PetscSection    cellCounts  = patch->cellCounts;
11575f824522SMatthew G. Knepley   PetscSection    pointCounts = patch->pointCounts;
11580904074fSPatrick Farrell   PetscSection    gtolCounts, gtolCountsWithArtificial = NULL, gtolCountsWithAll = NULL;
11594bbf5ea8SMatthew G. Knepley   IS              cells         = patch->cells;
11605f824522SMatthew G. Knepley   IS              points        = patch->points;
11614bbf5ea8SMatthew G. Knepley   PetscSection    cellNumbering = patch->cellNumbering;
11625f824522SMatthew G. Knepley   PetscInt        Nf            = patch->nsubspaces;
11635f824522SMatthew G. Knepley   PetscInt        numCells, numPoints;
11644bbf5ea8SMatthew G. Knepley   PetscInt        numDofs;
11650904074fSPatrick Farrell   PetscInt        numGlobalDofs, numGlobalDofsWithArtificial, numGlobalDofsWithAll;
11664bbf5ea8SMatthew G. Knepley   PetscInt        totalDofsPerCell = patch->totalDofsPerCell;
11674bbf5ea8SMatthew G. Knepley   PetscInt        vStart, vEnd, v;
11685f824522SMatthew G. Knepley   const PetscInt *cellsArray, *pointsArray;
11694bbf5ea8SMatthew G. Knepley   PetscInt       *newCellsArray                 = NULL;
11704bbf5ea8SMatthew G. Knepley   PetscInt       *dofsArray                     = NULL;
1171c2e6f3c0SFlorian Wechsung   PetscInt       *dofsArrayWithArtificial       = NULL;
11720904074fSPatrick Farrell   PetscInt       *dofsArrayWithAll              = NULL;
11735f824522SMatthew G. Knepley   PetscInt       *offsArray                     = NULL;
1174c2e6f3c0SFlorian Wechsung   PetscInt       *offsArrayWithArtificial       = NULL;
11750904074fSPatrick Farrell   PetscInt       *offsArrayWithAll              = NULL;
11764bbf5ea8SMatthew G. Knepley   PetscInt       *asmArray                      = NULL;
1177c2e6f3c0SFlorian Wechsung   PetscInt       *asmArrayWithArtificial        = NULL;
11780904074fSPatrick Farrell   PetscInt       *asmArrayWithAll               = NULL;
11794bbf5ea8SMatthew G. Knepley   PetscInt       *globalDofsArray               = NULL;
1180c2e6f3c0SFlorian Wechsung   PetscInt       *globalDofsArrayWithArtificial = NULL;
11810904074fSPatrick Farrell   PetscInt       *globalDofsArrayWithAll        = NULL;
11824bbf5ea8SMatthew G. Knepley   PetscInt        globalIndex                   = 0;
11834bbf5ea8SMatthew G. Knepley   PetscInt        key                           = 0;
11844bbf5ea8SMatthew G. Knepley   PetscInt        asmKey                        = 0;
1185b6bb21d1SLawrence Mitchell   DM              dm                            = NULL, plex;
1186557beb66SLawrence Mitchell   const PetscInt *bcNodes                       = NULL;
11871b68eb51SMatthew G. Knepley   PetscHMapI      ht;
1188c2e6f3c0SFlorian Wechsung   PetscHMapI      htWithArtificial;
11890904074fSPatrick Farrell   PetscHMapI      htWithAll;
11901b68eb51SMatthew G. Knepley   PetscHSetI      globalBcs;
1191557beb66SLawrence Mitchell   PetscInt        numBcs;
11921b68eb51SMatthew G. Knepley   PetscHSetI      ownedpts, seenpts, owneddofs, seendofs, artificialbcs;
1193cda239d9SMatthew G. Knepley   PetscInt        pStart, pEnd, p, i;
119410534d48SPatrick Farrell   char            option[PETSC_MAX_PATH_LEN];
119539fd2e8aSPatrick Farrell   PetscBool       isNonlinear;
11964bbf5ea8SMatthew G. Knepley 
11974bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1198557beb66SLawrence Mitchell 
11999566063dSJacob Faibussowitsch   PetscCall(PCGetDM(pc, &dm));
12009566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
1201b6bb21d1SLawrence Mitchell   dm = plex;
12024bbf5ea8SMatthew G. Knepley   /* dofcounts section is cellcounts section * dofPerCell */
12039566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(cellCounts, &numCells));
12049566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(patch->pointCounts, &numPoints));
12054bbf5ea8SMatthew G. Knepley   numDofs = numCells * totalDofsPerCell;
12069566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numDofs, &dofsArray));
12079566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numPoints * Nf, &offsArray));
12089566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numDofs, &asmArray));
12099566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numCells, &newCellsArray));
12109566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(cellCounts, &vStart, &vEnd));
12119566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCounts));
12124bbf5ea8SMatthew G. Knepley   gtolCounts = patch->gtolCounts;
12139566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(gtolCounts, vStart, vEnd));
12149566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->gtolCounts, "Patch Global Index Section"));
12154bbf5ea8SMatthew G. Knepley 
1216b6bb21d1SLawrence Mitchell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
12179566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numPoints * Nf, &offsArrayWithArtificial));
12189566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numDofs, &asmArrayWithArtificial));
12199566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numDofs, &dofsArrayWithArtificial));
12209566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithArtificial));
1221c2e6f3c0SFlorian Wechsung     gtolCountsWithArtificial = patch->gtolCountsWithArtificial;
12229566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(gtolCountsWithArtificial, vStart, vEnd));
12239566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)patch->gtolCountsWithArtificial, "Patch Global Index Section Including Artificial BCs"));
1224c2e6f3c0SFlorian Wechsung   }
1225c2e6f3c0SFlorian Wechsung 
12260904074fSPatrick Farrell   isNonlinear = patch->isNonlinear;
1227b6bb21d1SLawrence Mitchell   if (isNonlinear) {
12289566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numPoints * Nf, &offsArrayWithAll));
12299566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numDofs, &asmArrayWithAll));
12309566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numDofs, &dofsArrayWithAll));
12319566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithAll));
12320904074fSPatrick Farrell     gtolCountsWithAll = patch->gtolCountsWithAll;
12339566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(gtolCountsWithAll, vStart, vEnd));
12349566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)patch->gtolCountsWithAll, "Patch Global Index Section Including All BCs"));
12350904074fSPatrick Farrell   }
12360904074fSPatrick Farrell 
1237557beb66SLawrence Mitchell   /* Outside the patch loop, get the dofs that are globally-enforced Dirichlet
1238557beb66SLawrence Mitchell    conditions */
12399566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&globalBcs));
12409566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->ghostBcNodes, &bcNodes));
12419566063dSJacob Faibussowitsch   PetscCall(ISGetSize(patch->ghostBcNodes, &numBcs));
12429371c9d4SSatish Balay   for (i = 0; i < numBcs; ++i) { PetscCall(PetscHSetIAdd(globalBcs, bcNodes[i])); /* these are already in concatenated numbering */ }
12439566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->ghostBcNodes, &bcNodes));
12449566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->ghostBcNodes)); /* memory optimisation */
1245557beb66SLawrence Mitchell 
1246557beb66SLawrence Mitchell   /* Hash tables for artificial BC construction */
12479566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&ownedpts));
12489566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&seenpts));
12499566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&owneddofs));
12509566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&seendofs));
12519566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&artificialbcs));
1252557beb66SLawrence Mitchell 
12539566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(cells, &cellsArray));
12549566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(points, &pointsArray));
12559566063dSJacob Faibussowitsch   PetscCall(PetscHMapICreate(&ht));
12569566063dSJacob Faibussowitsch   PetscCall(PetscHMapICreate(&htWithArtificial));
12579566063dSJacob Faibussowitsch   PetscCall(PetscHMapICreate(&htWithAll));
12584bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
12594bbf5ea8SMatthew G. Knepley     PetscInt localIndex               = 0;
1260c2e6f3c0SFlorian Wechsung     PetscInt localIndexWithArtificial = 0;
12610904074fSPatrick Farrell     PetscInt localIndexWithAll        = 0;
12624bbf5ea8SMatthew G. Knepley     PetscInt dof, off, i, j, k, l;
12634bbf5ea8SMatthew G. Knepley 
12649566063dSJacob Faibussowitsch     PetscCall(PetscHMapIClear(ht));
12659566063dSJacob Faibussowitsch     PetscCall(PetscHMapIClear(htWithArtificial));
12669566063dSJacob Faibussowitsch     PetscCall(PetscHMapIClear(htWithAll));
12679566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(cellCounts, v, &dof));
12689566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(cellCounts, v, &off));
12694bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
12704bbf5ea8SMatthew G. Knepley 
1271557beb66SLawrence Mitchell     /* Calculate the global numbers of the artificial BC dofs here first */
12729566063dSJacob Faibussowitsch     PetscCall(patch->patchconstructop((void *)patch, dm, v, ownedpts));
12739566063dSJacob Faibussowitsch     PetscCall(PCPatchCompleteCellPatch(pc, ownedpts, seenpts));
12749566063dSJacob Faibussowitsch     PetscCall(PCPatchGetPointDofs(pc, ownedpts, owneddofs, v, &patch->subspaces_to_exclude));
12759566063dSJacob Faibussowitsch     PetscCall(PCPatchGetPointDofs(pc, seenpts, seendofs, v, NULL));
12769566063dSJacob Faibussowitsch     PetscCall(PCPatchComputeSetDifference_Private(owneddofs, seendofs, artificialbcs));
12778135ed82SLawrence Mitchell     if (patch->viewPatches) {
12781b68eb51SMatthew G. Knepley       PetscHSetI    globalbcdofs;
12791b68eb51SMatthew G. Knepley       PetscHashIter hi;
12808135ed82SLawrence Mitchell       MPI_Comm      comm = PetscObjectComm((PetscObject)pc);
12811b68eb51SMatthew G. Knepley 
12829566063dSJacob Faibussowitsch       PetscCall(PetscHSetICreate(&globalbcdofs));
128363a3b9bcSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": owned dofs:\n", v));
12841b68eb51SMatthew G. Knepley       PetscHashIterBegin(owneddofs, hi);
12851b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(owneddofs, hi)) {
12868135ed82SLawrence Mitchell         PetscInt globalDof;
12878135ed82SLawrence Mitchell 
12881b68eb51SMatthew G. Knepley         PetscHashIterGetKey(owneddofs, hi, globalDof);
12891b68eb51SMatthew G. Knepley         PetscHashIterNext(owneddofs, hi);
129063a3b9bcSJacob Faibussowitsch         PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof));
12918135ed82SLawrence Mitchell       }
12929566063dSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "\n"));
129363a3b9bcSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": seen dofs:\n", v));
12941b68eb51SMatthew G. Knepley       PetscHashIterBegin(seendofs, hi);
12951b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(seendofs, hi)) {
12968135ed82SLawrence Mitchell         PetscInt  globalDof;
12978135ed82SLawrence Mitchell         PetscBool flg;
12988135ed82SLawrence Mitchell 
12991b68eb51SMatthew G. Knepley         PetscHashIterGetKey(seendofs, hi, globalDof);
13001b68eb51SMatthew G. Knepley         PetscHashIterNext(seendofs, hi);
130163a3b9bcSJacob Faibussowitsch         PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof));
13028135ed82SLawrence Mitchell 
13039566063dSJacob Faibussowitsch         PetscCall(PetscHSetIHas(globalBcs, globalDof, &flg));
13049566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscHSetIAdd(globalbcdofs, globalDof));
13058135ed82SLawrence Mitchell       }
13069566063dSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "\n"));
130763a3b9bcSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": global BCs:\n", v));
13089566063dSJacob Faibussowitsch       PetscCall(PetscHSetIGetSize(globalbcdofs, &numBcs));
13098135ed82SLawrence Mitchell       if (numBcs > 0) {
13101b68eb51SMatthew G. Knepley         PetscHashIterBegin(globalbcdofs, hi);
13111b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(globalbcdofs, hi)) {
13128135ed82SLawrence Mitchell           PetscInt globalDof;
13131b68eb51SMatthew G. Knepley           PetscHashIterGetKey(globalbcdofs, hi, globalDof);
13141b68eb51SMatthew G. Knepley           PetscHashIterNext(globalbcdofs, hi);
131563a3b9bcSJacob Faibussowitsch           PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof));
13168135ed82SLawrence Mitchell         }
13178135ed82SLawrence Mitchell       }
13189566063dSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "\n"));
131963a3b9bcSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": artificial BCs:\n", v));
13209566063dSJacob Faibussowitsch       PetscCall(PetscHSetIGetSize(artificialbcs, &numBcs));
13218135ed82SLawrence Mitchell       if (numBcs > 0) {
13221b68eb51SMatthew G. Knepley         PetscHashIterBegin(artificialbcs, hi);
13231b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(artificialbcs, hi)) {
13248135ed82SLawrence Mitchell           PetscInt globalDof;
13251b68eb51SMatthew G. Knepley           PetscHashIterGetKey(artificialbcs, hi, globalDof);
13261b68eb51SMatthew G. Knepley           PetscHashIterNext(artificialbcs, hi);
132763a3b9bcSJacob Faibussowitsch           PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof));
13288135ed82SLawrence Mitchell         }
13298135ed82SLawrence Mitchell       }
13309566063dSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "\n\n"));
13319566063dSJacob Faibussowitsch       PetscCall(PetscHSetIDestroy(&globalbcdofs));
13328135ed82SLawrence Mitchell     }
13334bbf5ea8SMatthew G. Knepley     for (k = 0; k < patch->nsubspaces; ++k) {
13344bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
13354bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
13364bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
13374bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
13384bbf5ea8SMatthew G. Knepley 
13394bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
13404bbf5ea8SMatthew G. Knepley         /* Walk over the cells in this patch. */
13414bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
13425f824522SMatthew G. Knepley         PetscInt       cell = c;
13434bbf5ea8SMatthew G. Knepley 
13445f824522SMatthew G. Knepley         /* TODO Change this to an IS */
13455f824522SMatthew G. Knepley         if (cellNumbering) {
13469566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetDof(cellNumbering, c, &cell));
134763a3b9bcSJacob Faibussowitsch           PetscCheck(cell > 0, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Cell %" PetscInt_FMT " doesn't appear in cell numbering map", c);
13489566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetOffset(cellNumbering, c, &cell));
13495f824522SMatthew G. Knepley         }
13504bbf5ea8SMatthew G. Knepley         newCellsArray[i] = cell;
13514bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
13524bbf5ea8SMatthew G. Knepley           /* For each global dof, map it into contiguous local storage. */
13534bbf5ea8SMatthew G. Knepley           const PetscInt globalDof = cellNodeMap[cell * nodesPerCell + j] * bs + subspaceOffset;
13544bbf5ea8SMatthew G. Knepley           /* finally, loop over block size */
13554bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
13561b68eb51SMatthew G. Knepley             PetscInt  localDof;
13571b68eb51SMatthew G. Knepley             PetscBool isGlobalBcDof, isArtificialBcDof;
13584bbf5ea8SMatthew G. Knepley 
1359557beb66SLawrence Mitchell             /* first, check if this is either a globally enforced or locally enforced BC dof */
13609566063dSJacob Faibussowitsch             PetscCall(PetscHSetIHas(globalBcs, globalDof + l, &isGlobalBcDof));
13619566063dSJacob Faibussowitsch             PetscCall(PetscHSetIHas(artificialbcs, globalDof + l, &isArtificialBcDof));
1362557beb66SLawrence Mitchell 
1363557beb66SLawrence Mitchell             /* if it's either, don't ever give it a local dof number */
13641b68eb51SMatthew G. Knepley             if (isGlobalBcDof || isArtificialBcDof) {
1365c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = -1; /* don't use this in assembly in this patch */
1366557beb66SLawrence Mitchell             } else {
13679566063dSJacob Faibussowitsch               PetscCall(PetscHMapIGet(ht, globalDof + l, &localDof));
13684bbf5ea8SMatthew G. Knepley               if (localDof == -1) {
13694bbf5ea8SMatthew G. Knepley                 localDof = localIndex++;
13709566063dSJacob Faibussowitsch                 PetscCall(PetscHMapISet(ht, globalDof + l, localDof));
13714bbf5ea8SMatthew G. Knepley               }
137263a3b9bcSJacob Faibussowitsch               PetscCheck(globalIndex < numDofs, PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %" PetscInt_FMT " than expected %" PetscInt_FMT, globalIndex + 1, numDofs);
13734bbf5ea8SMatthew G. Knepley               /* And store. */
1374c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = localDof;
13754bbf5ea8SMatthew G. Knepley             }
1376c2e6f3c0SFlorian Wechsung 
13770904074fSPatrick Farrell             if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1378c2e6f3c0SFlorian Wechsung               if (isGlobalBcDof) {
1379e047a90bSFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = -1; /* don't use this in assembly in this patch */
1380c2e6f3c0SFlorian Wechsung               } else {
13819566063dSJacob Faibussowitsch                 PetscCall(PetscHMapIGet(htWithArtificial, globalDof + l, &localDof));
1382c2e6f3c0SFlorian Wechsung                 if (localDof == -1) {
1383c2e6f3c0SFlorian Wechsung                   localDof = localIndexWithArtificial++;
13849566063dSJacob Faibussowitsch                   PetscCall(PetscHMapISet(htWithArtificial, globalDof + l, localDof));
1385c2e6f3c0SFlorian Wechsung                 }
138663a3b9bcSJacob Faibussowitsch                 PetscCheck(globalIndex < numDofs, PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %" PetscInt_FMT " than expected %" PetscInt_FMT, globalIndex + 1, numDofs);
1387c2e6f3c0SFlorian Wechsung                 /* And store.*/
1388c2e6f3c0SFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = localDof;
1389c2e6f3c0SFlorian Wechsung               }
1390c2e6f3c0SFlorian Wechsung             }
13910904074fSPatrick Farrell 
13920904074fSPatrick Farrell             if (isNonlinear) {
13930904074fSPatrick Farrell               /* Build the dofmap for the function space with _all_ dofs,
13940904074fSPatrick Farrell    including those in any kind of boundary condition */
13959566063dSJacob Faibussowitsch               PetscCall(PetscHMapIGet(htWithAll, globalDof + l, &localDof));
13960904074fSPatrick Farrell               if (localDof == -1) {
13970904074fSPatrick Farrell                 localDof = localIndexWithAll++;
13989566063dSJacob Faibussowitsch                 PetscCall(PetscHMapISet(htWithAll, globalDof + l, localDof));
13990904074fSPatrick Farrell               }
140063a3b9bcSJacob Faibussowitsch               PetscCheck(globalIndex < numDofs, PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %" PetscInt_FMT " than expected %" PetscInt_FMT, globalIndex + 1, numDofs);
14010904074fSPatrick Farrell               /* And store.*/
14020904074fSPatrick Farrell               dofsArrayWithAll[globalIndex] = localDof;
14030904074fSPatrick Farrell             }
1404c2e6f3c0SFlorian Wechsung             globalIndex++;
14054bbf5ea8SMatthew G. Knepley           }
14064bbf5ea8SMatthew G. Knepley         }
14074bbf5ea8SMatthew G. Knepley       }
1408557beb66SLawrence Mitchell     }
14094bbf5ea8SMatthew G. Knepley     /*How many local dofs in this patch? */
14100904074fSPatrick Farrell     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
14119566063dSJacob Faibussowitsch       PetscCall(PetscHMapIGetSize(htWithArtificial, &dof));
14129566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(gtolCountsWithArtificial, v, dof));
1413c2e6f3c0SFlorian Wechsung     }
14140904074fSPatrick Farrell     if (isNonlinear) {
14159566063dSJacob Faibussowitsch       PetscCall(PetscHMapIGetSize(htWithAll, &dof));
14169566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(gtolCountsWithAll, v, dof));
14170904074fSPatrick Farrell     }
14189566063dSJacob Faibussowitsch     PetscCall(PetscHMapIGetSize(ht, &dof));
14199566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(gtolCounts, v, dof));
14204bbf5ea8SMatthew G. Knepley   }
1421b6bb21d1SLawrence Mitchell 
14229566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
142363a3b9bcSJacob 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);
14249566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(gtolCounts));
14259566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(gtolCounts, &numGlobalDofs));
14269566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numGlobalDofs, &globalDofsArray));
14274bbf5ea8SMatthew G. Knepley 
14280904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
14299566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(gtolCountsWithArtificial));
14309566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(gtolCountsWithArtificial, &numGlobalDofsWithArtificial));
14319566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numGlobalDofsWithArtificial, &globalDofsArrayWithArtificial));
1432c2e6f3c0SFlorian Wechsung   }
14330904074fSPatrick Farrell   if (isNonlinear) {
14349566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(gtolCountsWithAll));
14359566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(gtolCountsWithAll, &numGlobalDofsWithAll));
14369566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numGlobalDofsWithAll, &globalDofsArrayWithAll));
14370904074fSPatrick Farrell   }
14384bbf5ea8SMatthew 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. */
14394bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
14401b68eb51SMatthew G. Knepley     PetscHashIter hi;
14415f824522SMatthew G. Knepley     PetscInt      dof, off, Np, ooff, i, j, k, l;
14424bbf5ea8SMatthew G. Knepley 
14439566063dSJacob Faibussowitsch     PetscCall(PetscHMapIClear(ht));
14449566063dSJacob Faibussowitsch     PetscCall(PetscHMapIClear(htWithArtificial));
14459566063dSJacob Faibussowitsch     PetscCall(PetscHMapIClear(htWithAll));
14469566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(cellCounts, v, &dof));
14479566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(cellCounts, v, &off));
14489566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(pointCounts, v, &Np));
14499566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(pointCounts, v, &ooff));
14504bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
14514bbf5ea8SMatthew G. Knepley 
14524bbf5ea8SMatthew G. Knepley     for (k = 0; k < patch->nsubspaces; ++k) {
14534bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
14544bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
14554bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
14564bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
1457d490bb3dSLawrence Mitchell       PetscInt        goff;
14584bbf5ea8SMatthew G. Knepley 
14594bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
14604bbf5ea8SMatthew G. Knepley         /* Reconstruct mapping of global-to-local on this patch. */
14614bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
14625f824522SMatthew G. Knepley         PetscInt       cell = c;
14634bbf5ea8SMatthew G. Knepley 
14649566063dSJacob Faibussowitsch         if (cellNumbering) PetscCall(PetscSectionGetOffset(cellNumbering, c, &cell));
14654bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
14664bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
14675f824522SMatthew G. Knepley             const PetscInt globalDof = cellNodeMap[cell * nodesPerCell + j] * bs + l + subspaceOffset;
1468c2e6f3c0SFlorian Wechsung             const PetscInt localDof  = dofsArray[key];
14699566063dSJacob Faibussowitsch             if (localDof >= 0) PetscCall(PetscHMapISet(ht, globalDof, localDof));
14700904074fSPatrick Farrell             if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1471c2e6f3c0SFlorian Wechsung               const PetscInt localDofWithArtificial = dofsArrayWithArtificial[key];
147248a46eb9SPierre Jolivet               if (localDofWithArtificial >= 0) PetscCall(PetscHMapISet(htWithArtificial, globalDof, localDofWithArtificial));
1473c2e6f3c0SFlorian Wechsung             }
14740904074fSPatrick Farrell             if (isNonlinear) {
14750904074fSPatrick Farrell               const PetscInt localDofWithAll = dofsArrayWithAll[key];
147648a46eb9SPierre Jolivet               if (localDofWithAll >= 0) PetscCall(PetscHMapISet(htWithAll, globalDof, localDofWithAll));
14770904074fSPatrick Farrell             }
1478c2e6f3c0SFlorian Wechsung             key++;
14794bbf5ea8SMatthew G. Knepley           }
14804bbf5ea8SMatthew G. Knepley         }
14814bbf5ea8SMatthew G. Knepley       }
1482557beb66SLawrence Mitchell 
14834bbf5ea8SMatthew G. Knepley       /* Shove it in the output data structure. */
14849566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(gtolCounts, v, &goff));
14851b68eb51SMatthew G. Knepley       PetscHashIterBegin(ht, hi);
14861b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(ht, hi)) {
14874bbf5ea8SMatthew G. Knepley         PetscInt globalDof, localDof;
14884bbf5ea8SMatthew G. Knepley 
14891b68eb51SMatthew G. Knepley         PetscHashIterGetKey(ht, hi, globalDof);
14901b68eb51SMatthew G. Knepley         PetscHashIterGetVal(ht, hi, localDof);
14914bbf5ea8SMatthew G. Knepley         if (globalDof >= 0) globalDofsArray[goff + localDof] = globalDof;
14921b68eb51SMatthew G. Knepley         PetscHashIterNext(ht, hi);
14934bbf5ea8SMatthew G. Knepley       }
14945f824522SMatthew G. Knepley 
14950904074fSPatrick Farrell       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
14969566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gtolCountsWithArtificial, v, &goff));
1497c2e6f3c0SFlorian Wechsung         PetscHashIterBegin(htWithArtificial, hi);
1498c2e6f3c0SFlorian Wechsung         while (!PetscHashIterAtEnd(htWithArtificial, hi)) {
1499c2e6f3c0SFlorian Wechsung           PetscInt globalDof, localDof;
1500c2e6f3c0SFlorian Wechsung           PetscHashIterGetKey(htWithArtificial, hi, globalDof);
1501c2e6f3c0SFlorian Wechsung           PetscHashIterGetVal(htWithArtificial, hi, localDof);
1502c2e6f3c0SFlorian Wechsung           if (globalDof >= 0) globalDofsArrayWithArtificial[goff + localDof] = globalDof;
1503c2e6f3c0SFlorian Wechsung           PetscHashIterNext(htWithArtificial, hi);
1504c2e6f3c0SFlorian Wechsung         }
1505c2e6f3c0SFlorian Wechsung       }
15060904074fSPatrick Farrell       if (isNonlinear) {
15079566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gtolCountsWithAll, v, &goff));
15080904074fSPatrick Farrell         PetscHashIterBegin(htWithAll, hi);
15090904074fSPatrick Farrell         while (!PetscHashIterAtEnd(htWithAll, hi)) {
15100904074fSPatrick Farrell           PetscInt globalDof, localDof;
15110904074fSPatrick Farrell           PetscHashIterGetKey(htWithAll, hi, globalDof);
15120904074fSPatrick Farrell           PetscHashIterGetVal(htWithAll, hi, localDof);
15130904074fSPatrick Farrell           if (globalDof >= 0) globalDofsArrayWithAll[goff + localDof] = globalDof;
15140904074fSPatrick Farrell           PetscHashIterNext(htWithAll, hi);
15150904074fSPatrick Farrell         }
15160904074fSPatrick Farrell       }
1517c2e6f3c0SFlorian Wechsung 
15185f824522SMatthew G. Knepley       for (p = 0; p < Np; ++p) {
15195f824522SMatthew G. Knepley         const PetscInt point = pointsArray[ooff + p];
15205f824522SMatthew G. Knepley         PetscInt       globalDof, localDof;
15215f824522SMatthew G. Knepley 
15229566063dSJacob Faibussowitsch         PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, point, NULL, &globalDof));
15239566063dSJacob Faibussowitsch         PetscCall(PetscHMapIGet(ht, globalDof, &localDof));
15245f824522SMatthew G. Knepley         offsArray[(ooff + p) * Nf + k] = localDof;
15250904074fSPatrick Farrell         if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
15269566063dSJacob Faibussowitsch           PetscCall(PetscHMapIGet(htWithArtificial, globalDof, &localDof));
1527c2e6f3c0SFlorian Wechsung           offsArrayWithArtificial[(ooff + p) * Nf + k] = localDof;
1528c2e6f3c0SFlorian Wechsung         }
15290904074fSPatrick Farrell         if (isNonlinear) {
15309566063dSJacob Faibussowitsch           PetscCall(PetscHMapIGet(htWithAll, globalDof, &localDof));
15310904074fSPatrick Farrell           offsArrayWithAll[(ooff + p) * Nf + k] = localDof;
15320904074fSPatrick Farrell         }
15335f824522SMatthew G. Knepley       }
15344bbf5ea8SMatthew G. Knepley     }
15354bbf5ea8SMatthew G. Knepley 
15369566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&globalBcs));
15379566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&ownedpts));
15389566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&seenpts));
15399566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&owneddofs));
15409566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&seendofs));
15419566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&artificialbcs));
1542557beb66SLawrence Mitchell 
15434bbf5ea8SMatthew G. Knepley     /* At this point, we have a hash table ht built that maps globalDof -> localDof.
15444bbf5ea8SMatthew G. Knepley    We need to create the dof table laid out cellwise first, then by subspace,
15454bbf5ea8SMatthew G. Knepley    as the assembler assembles cell-wise and we need to stuff the different
15464bbf5ea8SMatthew G. Knepley    contributions of the different function spaces to the right places. So we loop
15474bbf5ea8SMatthew G. Knepley    over cells, then over subspaces. */
15484bbf5ea8SMatthew G. Knepley     if (patch->nsubspaces > 1) { /* for nsubspaces = 1, data we need is already in dofsArray */
15494bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
15504bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
15515f824522SMatthew G. Knepley         PetscInt       cell = c;
15524bbf5ea8SMatthew G. Knepley 
15539566063dSJacob Faibussowitsch         if (cellNumbering) PetscCall(PetscSectionGetOffset(cellNumbering, c, &cell));
15544bbf5ea8SMatthew G. Knepley         for (k = 0; k < patch->nsubspaces; ++k) {
15554bbf5ea8SMatthew G. Knepley           const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
15564bbf5ea8SMatthew G. Knepley           PetscInt        nodesPerCell   = patch->nodesPerCell[k];
15574bbf5ea8SMatthew G. Knepley           PetscInt        subspaceOffset = patch->subspaceOffsets[k];
15584bbf5ea8SMatthew G. Knepley           PetscInt        bs             = patch->bs[k];
15594bbf5ea8SMatthew G. Knepley 
15604bbf5ea8SMatthew G. Knepley           for (j = 0; j < nodesPerCell; ++j) {
15614bbf5ea8SMatthew G. Knepley             for (l = 0; l < bs; ++l) {
15625f824522SMatthew G. Knepley               const PetscInt globalDof = cellNodeMap[cell * nodesPerCell + j] * bs + l + subspaceOffset;
15634bbf5ea8SMatthew G. Knepley               PetscInt       localDof;
15644bbf5ea8SMatthew G. Knepley 
15659566063dSJacob Faibussowitsch               PetscCall(PetscHMapIGet(ht, globalDof, &localDof));
1566557beb66SLawrence Mitchell               /* If it's not in the hash table, i.e. is a BC dof,
15671b68eb51SMatthew G. Knepley    then the PetscHSetIMap above gives -1, which matches
1568557beb66SLawrence Mitchell    exactly the convention for PETSc's matrix assembly to
1569557beb66SLawrence Mitchell    ignore the dof. So we don't need to do anything here */
1570c2e6f3c0SFlorian Wechsung               asmArray[asmKey] = localDof;
15710904074fSPatrick Farrell               if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
15729566063dSJacob Faibussowitsch                 PetscCall(PetscHMapIGet(htWithArtificial, globalDof, &localDof));
1573c2e6f3c0SFlorian Wechsung                 asmArrayWithArtificial[asmKey] = localDof;
1574c2e6f3c0SFlorian Wechsung               }
15750904074fSPatrick Farrell               if (isNonlinear) {
15769566063dSJacob Faibussowitsch                 PetscCall(PetscHMapIGet(htWithAll, globalDof, &localDof));
15770904074fSPatrick Farrell                 asmArrayWithAll[asmKey] = localDof;
15780904074fSPatrick Farrell               }
1579c2e6f3c0SFlorian Wechsung               asmKey++;
15804bbf5ea8SMatthew G. Knepley             }
15814bbf5ea8SMatthew G. Knepley           }
15824bbf5ea8SMatthew G. Knepley         }
15834bbf5ea8SMatthew G. Knepley       }
15844bbf5ea8SMatthew G. Knepley     }
15854bbf5ea8SMatthew G. Knepley   }
1586c2e6f3c0SFlorian Wechsung   if (1 == patch->nsubspaces) {
15879566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(asmArray, dofsArray, numDofs));
158848a46eb9SPierre Jolivet     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) PetscCall(PetscArraycpy(asmArrayWithArtificial, dofsArrayWithArtificial, numDofs));
15891baa6e33SBarry Smith     if (isNonlinear) PetscCall(PetscArraycpy(asmArrayWithAll, dofsArrayWithAll, numDofs));
1590c2e6f3c0SFlorian Wechsung   }
15914bbf5ea8SMatthew G. Knepley 
15929566063dSJacob Faibussowitsch   PetscCall(PetscHMapIDestroy(&ht));
15939566063dSJacob Faibussowitsch   PetscCall(PetscHMapIDestroy(&htWithArtificial));
15949566063dSJacob Faibussowitsch   PetscCall(PetscHMapIDestroy(&htWithAll));
15959566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(cells, &cellsArray));
15969566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(points, &pointsArray));
15979566063dSJacob Faibussowitsch   PetscCall(PetscFree(dofsArray));
159848a46eb9SPierre Jolivet   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) PetscCall(PetscFree(dofsArrayWithArtificial));
15991baa6e33SBarry Smith   if (isNonlinear) PetscCall(PetscFree(dofsArrayWithAll));
16005f824522SMatthew G. Knepley   /* Create placeholder section for map from points to patch dofs */
16019566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->patchSection));
16029566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(patch->patchSection, patch->nsubspaces));
16031e5fa6bbSLawrence Mitchell   if (patch->combined) {
16041e5fa6bbSLawrence Mitchell     PetscInt numFields;
16059566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(patch->dofSection[0], &numFields));
160663a3b9bcSJacob 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);
16079566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(patch->dofSection[0], &pStart, &pEnd));
16089566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(patch->patchSection, pStart, pEnd));
16095f824522SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
16105f824522SMatthew G. Knepley       PetscInt dof, fdof, f;
16115f824522SMatthew G. Knepley 
16129566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(patch->dofSection[0], p, &dof));
16139566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(patch->patchSection, p, dof));
16145f824522SMatthew G. Knepley       for (f = 0; f < patch->nsubspaces; ++f) {
16159566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldDof(patch->dofSection[0], p, f, &fdof));
16169566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetFieldDof(patch->patchSection, p, f, fdof));
16175f824522SMatthew G. Knepley       }
16181e5fa6bbSLawrence Mitchell     }
16191e5fa6bbSLawrence Mitchell   } else {
16201e5fa6bbSLawrence Mitchell     PetscInt pStartf, pEndf, f;
16211e5fa6bbSLawrence Mitchell     pStart = PETSC_MAX_INT;
16221e5fa6bbSLawrence Mitchell     pEnd   = PETSC_MIN_INT;
16231e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
16249566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf));
16251e5fa6bbSLawrence Mitchell       pStart = PetscMin(pStart, pStartf);
16261e5fa6bbSLawrence Mitchell       pEnd   = PetscMax(pEnd, pEndf);
16271e5fa6bbSLawrence Mitchell     }
16289566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(patch->patchSection, pStart, pEnd));
16291e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
16309566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf));
16311e5fa6bbSLawrence Mitchell       for (p = pStartf; p < pEndf; ++p) {
16321e5fa6bbSLawrence Mitchell         PetscInt fdof;
16339566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->dofSection[f], p, &fdof));
16349566063dSJacob Faibussowitsch         PetscCall(PetscSectionAddDof(patch->patchSection, p, fdof));
16359566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetFieldDof(patch->patchSection, p, f, fdof));
1636bdd9e0cdSPatrick Farrell       }
1637bdd9e0cdSPatrick Farrell     }
16385f824522SMatthew G. Knepley   }
16399566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(patch->patchSection));
16409566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUseFieldOffsets(patch->patchSection, PETSC_TRUE));
16414bbf5ea8SMatthew G. Knepley   /* Replace cell indices with firedrake-numbered ones. */
16429566063dSJacob Faibussowitsch   PetscCall(ISGeneralSetIndices(cells, numCells, (const PetscInt *)newCellsArray, PETSC_OWN_POINTER));
16439566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofs, globalDofsArray, PETSC_OWN_POINTER, &patch->gtol));
16449566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->gtol, "Global Indices"));
16459566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_g2l_view", patch->classname));
16469566063dSJacob Faibussowitsch   PetscCall(PetscSectionViewFromOptions(patch->gtolCounts, (PetscObject)pc, option));
16479566063dSJacob Faibussowitsch   PetscCall(ISViewFromOptions(patch->gtol, (PetscObject)pc, option));
16489566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArray, PETSC_OWN_POINTER, &patch->dofs));
16499566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints * Nf, offsArray, PETSC_OWN_POINTER, &patch->offs));
16500904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
16519566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithArtificial, globalDofsArrayWithArtificial, PETSC_OWN_POINTER, &patch->gtolWithArtificial));
16529566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithArtificial, PETSC_OWN_POINTER, &patch->dofsWithArtificial));
16539566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints * Nf, offsArrayWithArtificial, PETSC_OWN_POINTER, &patch->offsWithArtificial));
1654c2e6f3c0SFlorian Wechsung   }
16550904074fSPatrick Farrell   if (isNonlinear) {
16569566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithAll, globalDofsArrayWithAll, PETSC_OWN_POINTER, &patch->gtolWithAll));
16579566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithAll, PETSC_OWN_POINTER, &patch->dofsWithAll));
16589566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints * Nf, offsArrayWithAll, PETSC_OWN_POINTER, &patch->offsWithAll));
16590904074fSPatrick Farrell   }
16603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
16614bbf5ea8SMatthew G. Knepley }
16624bbf5ea8SMatthew G. Knepley 
1663d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCreateMatrix_Private(PC pc, PetscInt point, Mat *mat, PetscBool withArtificial)
1664d71ae5a4SJacob Faibussowitsch {
16654bbf5ea8SMatthew G. Knepley   PC_PATCH   *patch = (PC_PATCH *)pc->data;
16664bbf5ea8SMatthew G. Knepley   PetscBool   flg;
16674bbf5ea8SMatthew G. Knepley   PetscInt    csize, rsize;
16684bbf5ea8SMatthew G. Knepley   const char *prefix = NULL;
16694bbf5ea8SMatthew G. Knepley 
16704bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1671c2e6f3c0SFlorian Wechsung   if (withArtificial) {
1672e047a90bSFlorian Wechsung     /* would be nice if we could create a rectangular matrix of size numDofsWithArtificial x numDofs here */
16739d4fc724SLawrence Mitchell     PetscInt pStart;
16749566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(patch->gtolCountsWithArtificial, &pStart, NULL));
16759566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, point + pStart, &rsize));
16769d4fc724SLawrence Mitchell     csize = rsize;
1677ff201f6aSFlorian Wechsung   } else {
16789d4fc724SLawrence Mitchell     PetscInt pStart;
16799566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, NULL));
16809566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(patch->gtolCounts, point + pStart, &rsize));
16819d4fc724SLawrence Mitchell     csize = rsize;
1682c2e6f3c0SFlorian Wechsung   }
1683c2e6f3c0SFlorian Wechsung 
16849566063dSJacob Faibussowitsch   PetscCall(MatCreate(PETSC_COMM_SELF, mat));
16859566063dSJacob Faibussowitsch   PetscCall(PCGetOptionsPrefix(pc, &prefix));
16869566063dSJacob Faibussowitsch   PetscCall(MatSetOptionsPrefix(*mat, prefix));
16879566063dSJacob Faibussowitsch   PetscCall(MatAppendOptionsPrefix(*mat, "pc_patch_sub_"));
16889566063dSJacob Faibussowitsch   if (patch->sub_mat_type) PetscCall(MatSetType(*mat, patch->sub_mat_type));
16899566063dSJacob Faibussowitsch   else if (!patch->sub_mat_type) PetscCall(MatSetType(*mat, MATDENSE));
16909566063dSJacob Faibussowitsch   PetscCall(MatSetSizes(*mat, rsize, csize, rsize, csize));
16919566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)*mat, MATDENSE, &flg));
16929566063dSJacob Faibussowitsch   if (!flg) PetscCall(PetscObjectTypeCompare((PetscObject)*mat, MATSEQDENSE, &flg));
16934bbf5ea8SMatthew G. Knepley   /* Sparse patch matrices */
16944bbf5ea8SMatthew G. Knepley   if (!flg) {
16954bbf5ea8SMatthew G. Knepley     PetscBT         bt;
16964bbf5ea8SMatthew G. Knepley     PetscInt       *dnnz      = NULL;
16974bbf5ea8SMatthew G. Knepley     const PetscInt *dofsArray = NULL;
16984bbf5ea8SMatthew G. Knepley     PetscInt        pStart, pEnd, ncell, offset, c, i, j;
16994bbf5ea8SMatthew G. Knepley 
1700c2e6f3c0SFlorian Wechsung     if (withArtificial) {
17019566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(patch->dofsWithArtificial, &dofsArray));
1702ff201f6aSFlorian Wechsung     } else {
17039566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(patch->dofs, &dofsArray));
1704c2e6f3c0SFlorian Wechsung     }
17059566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd));
17064bbf5ea8SMatthew G. Knepley     point += pStart;
170763a3b9bcSJacob Faibussowitsch     PetscCheck(point < pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %" PetscInt_FMT " not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, pStart, pEnd);
17089566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(patch->cellCounts, point, &ncell));
17099566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(patch->cellCounts, point, &offset));
17109566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(PC_Patch_Prealloc, pc, 0, 0, 0));
1711b2866507SPatrick Farrell     /* A PetscBT uses N^2 bits to store the sparsity pattern on a
17124bbf5ea8SMatthew G. Knepley    * patch. This is probably OK if the patches are not too big,
1713b2866507SPatrick Farrell    * but uses too much memory. We therefore switch based on rsize. */
1714b2866507SPatrick Farrell     if (rsize < 3000) { /* FIXME: I picked this switch value out of my hat */
1715d63cebbaSPatrick Farrell       PetscScalar *zeroes;
1716d63cebbaSPatrick Farrell       PetscInt     rows;
1717d63cebbaSPatrick Farrell 
17189566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(rsize, &dnnz));
17199566063dSJacob Faibussowitsch       PetscCall(PetscBTCreate(rsize * rsize, &bt));
17204bbf5ea8SMatthew G. Knepley       for (c = 0; c < ncell; ++c) {
17214bbf5ea8SMatthew G. Knepley         const PetscInt *idx = dofsArray + (offset + c) * patch->totalDofsPerCell;
17224bbf5ea8SMatthew G. Knepley         for (i = 0; i < patch->totalDofsPerCell; ++i) {
17234bbf5ea8SMatthew G. Knepley           const PetscInt row = idx[i];
1724557beb66SLawrence Mitchell           if (row < 0) continue;
17254bbf5ea8SMatthew G. Knepley           for (j = 0; j < patch->totalDofsPerCell; ++j) {
17264bbf5ea8SMatthew G. Knepley             const PetscInt col = idx[j];
17274bbf5ea8SMatthew G. Knepley             const PetscInt key = row * rsize + col;
1728557beb66SLawrence Mitchell             if (col < 0) continue;
17294bbf5ea8SMatthew G. Knepley             if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
17304bbf5ea8SMatthew G. Knepley           }
17314bbf5ea8SMatthew G. Knepley         }
17324bbf5ea8SMatthew G. Knepley       }
1733d63cebbaSPatrick Farrell 
1734d63cebbaSPatrick Farrell       if (patch->usercomputeopintfacet) {
1735d63cebbaSPatrick Farrell         const PetscInt *intFacetsArray = NULL;
1736d63cebbaSPatrick Farrell         PetscInt        i, numIntFacets, intFacetOffset;
1737d63cebbaSPatrick Farrell         const PetscInt *facetCells = NULL;
1738d63cebbaSPatrick Farrell 
17399566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets));
17409566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset));
17419566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells));
17429566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray));
1743d63cebbaSPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
1744d63cebbaSPatrick Farrell           const PetscInt cell0 = facetCells[2 * (intFacetOffset + i) + 0];
1745d63cebbaSPatrick Farrell           const PetscInt cell1 = facetCells[2 * (intFacetOffset + i) + 1];
1746d63cebbaSPatrick Farrell           PetscInt       celli, cellj;
1747d63cebbaSPatrick Farrell 
1748d63cebbaSPatrick Farrell           for (celli = 0; celli < patch->totalDofsPerCell; celli++) {
1749d63cebbaSPatrick Farrell             const PetscInt row = dofsArray[(offset + cell0) * patch->totalDofsPerCell + celli];
1750b5c64f08SPatrick Farrell             if (row < 0) continue;
1751d63cebbaSPatrick Farrell             for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) {
1752d63cebbaSPatrick Farrell               const PetscInt col = dofsArray[(offset + cell1) * patch->totalDofsPerCell + cellj];
1753d63cebbaSPatrick Farrell               const PetscInt key = row * rsize + col;
1754d63cebbaSPatrick Farrell               if (col < 0) continue;
1755d63cebbaSPatrick Farrell               if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
1756d63cebbaSPatrick Farrell             }
1757d63cebbaSPatrick Farrell           }
1758d63cebbaSPatrick Farrell 
1759d63cebbaSPatrick Farrell           for (celli = 0; celli < patch->totalDofsPerCell; celli++) {
1760d63cebbaSPatrick Farrell             const PetscInt row = dofsArray[(offset + cell1) * patch->totalDofsPerCell + celli];
1761b5c64f08SPatrick Farrell             if (row < 0) continue;
1762d63cebbaSPatrick Farrell             for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) {
1763d63cebbaSPatrick Farrell               const PetscInt col = dofsArray[(offset + cell0) * patch->totalDofsPerCell + cellj];
1764d63cebbaSPatrick Farrell               const PetscInt key = row * rsize + col;
1765d63cebbaSPatrick Farrell               if (col < 0) continue;
1766d63cebbaSPatrick Farrell               if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
1767d63cebbaSPatrick Farrell             }
1768d63cebbaSPatrick Farrell           }
1769d63cebbaSPatrick Farrell         }
1770d63cebbaSPatrick Farrell       }
17719566063dSJacob Faibussowitsch       PetscCall(PetscBTDestroy(&bt));
17729566063dSJacob Faibussowitsch       PetscCall(MatXAIJSetPreallocation(*mat, 1, dnnz, NULL, NULL, NULL));
17739566063dSJacob Faibussowitsch       PetscCall(PetscFree(dnnz));
1774d63cebbaSPatrick Farrell 
17759566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(patch->totalDofsPerCell * patch->totalDofsPerCell, &zeroes));
1776d63cebbaSPatrick Farrell       for (c = 0; c < ncell; ++c) {
1777d63cebbaSPatrick Farrell         const PetscInt *idx = &dofsArray[(offset + c) * patch->totalDofsPerCell];
17789566063dSJacob Faibussowitsch         PetscCall(MatSetValues(*mat, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, zeroes, INSERT_VALUES));
1779d63cebbaSPatrick Farrell       }
17809566063dSJacob Faibussowitsch       PetscCall(MatGetLocalSize(*mat, &rows, NULL));
178148a46eb9SPierre Jolivet       for (i = 0; i < rows; ++i) PetscCall(MatSetValues(*mat, 1, &i, 1, &i, zeroes, INSERT_VALUES));
1782d63cebbaSPatrick Farrell 
1783d63cebbaSPatrick Farrell       if (patch->usercomputeopintfacet) {
1784d63cebbaSPatrick Farrell         const PetscInt *intFacetsArray = NULL;
1785d63cebbaSPatrick Farrell         PetscInt        i, numIntFacets, intFacetOffset;
1786d63cebbaSPatrick Farrell         const PetscInt *facetCells = NULL;
1787d63cebbaSPatrick Farrell 
17889566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets));
17899566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset));
17909566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells));
17919566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray));
1792d63cebbaSPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
1793d63cebbaSPatrick Farrell           const PetscInt  cell0    = facetCells[2 * (intFacetOffset + i) + 0];
1794d63cebbaSPatrick Farrell           const PetscInt  cell1    = facetCells[2 * (intFacetOffset + i) + 1];
1795d63cebbaSPatrick Farrell           const PetscInt *cell0idx = &dofsArray[(offset + cell0) * patch->totalDofsPerCell];
1796d63cebbaSPatrick Farrell           const PetscInt *cell1idx = &dofsArray[(offset + cell1) * patch->totalDofsPerCell];
17979566063dSJacob Faibussowitsch           PetscCall(MatSetValues(*mat, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, zeroes, INSERT_VALUES));
17989566063dSJacob Faibussowitsch           PetscCall(MatSetValues(*mat, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, zeroes, INSERT_VALUES));
1799d63cebbaSPatrick Farrell         }
1800d63cebbaSPatrick Farrell       }
1801d63cebbaSPatrick Farrell 
18029566063dSJacob Faibussowitsch       PetscCall(MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY));
18039566063dSJacob Faibussowitsch       PetscCall(MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY));
1804d63cebbaSPatrick Farrell 
18059566063dSJacob Faibussowitsch       PetscCall(PetscFree(zeroes));
1806d63cebbaSPatrick Farrell 
1807b2866507SPatrick Farrell     } else { /* rsize too big, use MATPREALLOCATOR */
1808b2866507SPatrick Farrell       Mat          preallocator;
1809b2866507SPatrick Farrell       PetscScalar *vals;
1810b2866507SPatrick Farrell 
18119566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(patch->totalDofsPerCell * patch->totalDofsPerCell, &vals));
18129566063dSJacob Faibussowitsch       PetscCall(MatCreate(PETSC_COMM_SELF, &preallocator));
18139566063dSJacob Faibussowitsch       PetscCall(MatSetType(preallocator, MATPREALLOCATOR));
18149566063dSJacob Faibussowitsch       PetscCall(MatSetSizes(preallocator, rsize, rsize, rsize, rsize));
18159566063dSJacob Faibussowitsch       PetscCall(MatSetUp(preallocator));
181611bcd083SPatrick Farrell 
1817b2866507SPatrick Farrell       for (c = 0; c < ncell; ++c) {
1818b2866507SPatrick Farrell         const PetscInt *idx = dofsArray + (offset + c) * patch->totalDofsPerCell;
18199566063dSJacob Faibussowitsch         PetscCall(MatSetValues(preallocator, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, vals, INSERT_VALUES));
1820b2866507SPatrick Farrell       }
182111bcd083SPatrick Farrell 
182211bcd083SPatrick Farrell       if (patch->usercomputeopintfacet) {
182311bcd083SPatrick Farrell         const PetscInt *intFacetsArray = NULL;
182411bcd083SPatrick Farrell         PetscInt        i, numIntFacets, intFacetOffset;
182511bcd083SPatrick Farrell         const PetscInt *facetCells = NULL;
182611bcd083SPatrick Farrell 
18279566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets));
18289566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset));
18299566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells));
18309566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray));
183111bcd083SPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
183211bcd083SPatrick Farrell           const PetscInt  cell0    = facetCells[2 * (intFacetOffset + i) + 0];
183311bcd083SPatrick Farrell           const PetscInt  cell1    = facetCells[2 * (intFacetOffset + i) + 1];
183411bcd083SPatrick Farrell           const PetscInt *cell0idx = &dofsArray[(offset + cell0) * patch->totalDofsPerCell];
183511bcd083SPatrick Farrell           const PetscInt *cell1idx = &dofsArray[(offset + cell1) * patch->totalDofsPerCell];
18369566063dSJacob Faibussowitsch           PetscCall(MatSetValues(preallocator, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, vals, INSERT_VALUES));
18379566063dSJacob Faibussowitsch           PetscCall(MatSetValues(preallocator, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, vals, INSERT_VALUES));
183811bcd083SPatrick Farrell         }
183911bcd083SPatrick Farrell       }
184011bcd083SPatrick Farrell 
18419566063dSJacob Faibussowitsch       PetscCall(PetscFree(vals));
18429566063dSJacob Faibussowitsch       PetscCall(MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY));
18439566063dSJacob Faibussowitsch       PetscCall(MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY));
18449566063dSJacob Faibussowitsch       PetscCall(MatPreallocatorPreallocate(preallocator, PETSC_TRUE, *mat));
18459566063dSJacob Faibussowitsch       PetscCall(MatDestroy(&preallocator));
1846b2866507SPatrick Farrell     }
18479566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(PC_Patch_Prealloc, pc, 0, 0, 0));
1848fe117d09SFlorian Wechsung     if (withArtificial) {
18499566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(patch->dofsWithArtificial, &dofsArray));
1850fe117d09SFlorian Wechsung     } else {
18519566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(patch->dofs, &dofsArray));
18524bbf5ea8SMatthew G. Knepley     }
1853fe117d09SFlorian Wechsung   }
18549566063dSJacob Faibussowitsch   PetscCall(MatSetUp(*mat));
18553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
18564bbf5ea8SMatthew G. Knepley }
18574bbf5ea8SMatthew G. Knepley 
1858d71ae5a4SJacob 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)
1859d71ae5a4SJacob Faibussowitsch {
186092d50984SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *)pc->data;
1861b6bb21d1SLawrence Mitchell   DM              dm, plex;
186292d50984SMatthew G. Knepley   PetscSection    s;
186392d50984SMatthew G. Knepley   const PetscInt *parray, *oarray;
186492d50984SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
186592d50984SMatthew G. Knepley 
186692d50984SMatthew G. Knepley   PetscFunctionBegin;
186728b400f6SJacob Faibussowitsch   PetscCheck(!patch->precomputeElementTensors, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Precomputing element tensors not implemented with DMPlex compute operator");
18689566063dSJacob Faibussowitsch   PetscCall(PCGetDM(pc, &dm));
18699566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
1870b6bb21d1SLawrence Mitchell   dm = plex;
18719566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
187292d50984SMatthew G. Knepley   /* Set offset into patch */
18739566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetDof(patch->pointCounts, patchNum, &Np));
18749566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(patch->pointCounts, patchNum, &poff));
18759566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->points, &parray));
18769566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->offs, &oarray));
187792d50984SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
187892d50984SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
187992d50984SMatthew G. Knepley       const PetscInt point = parray[poff + p];
188092d50984SMatthew G. Knepley       PetscInt       dof;
188192d50984SMatthew G. Knepley 
18829566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(patch->patchSection, point, f, &dof));
18839566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff + p) * Nf + f]));
18849566063dSJacob Faibussowitsch       if (patch->nsubspaces == 1) PetscCall(PetscSectionSetOffset(patch->patchSection, point, oarray[(poff + p) * Nf + f]));
18859566063dSJacob Faibussowitsch       else PetscCall(PetscSectionSetOffset(patch->patchSection, point, -1));
188692d50984SMatthew G. Knepley     }
188792d50984SMatthew G. Knepley   }
18889566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->points, &parray));
18899566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->offs, &oarray));
18909566063dSJacob Faibussowitsch   if (patch->viewSection) PetscCall(ObjectView((PetscObject)patch->patchSection, patch->viewerSection, patch->formatSection));
18919566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeResidual_Patch_Internal(dm, patch->patchSection, cellIS, 0.0, x, NULL, F, ctx));
18929566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
18933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
189492d50984SMatthew G. Knepley }
189592d50984SMatthew G. Knepley 
1896d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchComputeFunction_Internal(PC pc, Vec x, Vec F, PetscInt point)
1897d71ae5a4SJacob Faibussowitsch {
189892d50984SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *)pc->data;
189992d50984SMatthew G. Knepley   const PetscInt *dofsArray;
19000904074fSPatrick Farrell   const PetscInt *dofsArrayWithAll;
190192d50984SMatthew G. Knepley   const PetscInt *cellsArray;
190292d50984SMatthew G. Knepley   PetscInt        ncell, offset, pStart, pEnd;
190392d50984SMatthew G. Knepley 
190492d50984SMatthew G. Knepley   PetscFunctionBegin;
19059566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0));
1906ef1023bdSBarry Smith   PetscCheck(patch->usercomputeop, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set callback");
19079566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->dofs, &dofsArray));
19089566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll));
19099566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->cells, &cellsArray));
19109566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd));
191192d50984SMatthew G. Knepley 
191292d50984SMatthew G. Knepley   point += pStart;
191363a3b9bcSJacob Faibussowitsch   PetscCheck(point < pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %" PetscInt_FMT " not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, pStart, pEnd);
191492d50984SMatthew G. Knepley 
19159566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetDof(patch->cellCounts, point, &ncell));
19169566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(patch->cellCounts, point, &offset));
191792d50984SMatthew G. Knepley   if (ncell <= 0) {
19189566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
19193ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
192092d50984SMatthew G. Knepley   }
19219566063dSJacob Faibussowitsch   PetscCall(VecSet(F, 0.0));
192292d50984SMatthew G. Knepley   /* Cannot reuse the same IS because the geometry info is being cached in it */
19239566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS));
1924792fecdfSBarry 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));
19259566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->cellIS));
19269566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->dofs, &dofsArray));
19279566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll));
19289566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->cells, &cellsArray));
192992d50984SMatthew G. Knepley   if (patch->viewMatrix) {
193092d50984SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
193192d50984SMatthew G. Knepley 
193263a3b9bcSJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN - 1, "Patch vector for Point %" PetscInt_FMT, point));
19339566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)F, name));
19349566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)F, patch->viewerMatrix, patch->formatMatrix));
193592d50984SMatthew G. Knepley   }
19369566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
19373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
193892d50984SMatthew G. Knepley }
193992d50984SMatthew G. Knepley 
1940d71ae5a4SJacob 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)
1941d71ae5a4SJacob Faibussowitsch {
19425f824522SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *)pc->data;
1943b6bb21d1SLawrence Mitchell   DM              dm, plex;
19445f824522SMatthew G. Knepley   PetscSection    s;
19455f824522SMatthew G. Knepley   const PetscInt *parray, *oarray;
19465f824522SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
19475f824522SMatthew G. Knepley 
19485f824522SMatthew G. Knepley   PetscFunctionBegin;
19499566063dSJacob Faibussowitsch   PetscCall(PCGetDM(pc, &dm));
19509566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
1951b6bb21d1SLawrence Mitchell   dm = plex;
19529566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
19535f824522SMatthew G. Knepley   /* Set offset into patch */
19549566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetDof(patch->pointCounts, patchNum, &Np));
19559566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(patch->pointCounts, patchNum, &poff));
19569566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->points, &parray));
19579566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->offs, &oarray));
19585f824522SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
19595f824522SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
19605f824522SMatthew G. Knepley       const PetscInt point = parray[poff + p];
19615f824522SMatthew G. Knepley       PetscInt       dof;
19625f824522SMatthew G. Knepley 
19639566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(patch->patchSection, point, f, &dof));
19649566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff + p) * Nf + f]));
19659566063dSJacob Faibussowitsch       if (patch->nsubspaces == 1) PetscCall(PetscSectionSetOffset(patch->patchSection, point, oarray[(poff + p) * Nf + f]));
19669566063dSJacob Faibussowitsch       else PetscCall(PetscSectionSetOffset(patch->patchSection, point, -1));
19675f824522SMatthew G. Knepley     }
19685f824522SMatthew G. Knepley   }
19699566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->points, &parray));
19709566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->offs, &oarray));
19719566063dSJacob Faibussowitsch   if (patch->viewSection) PetscCall(ObjectView((PetscObject)patch->patchSection, patch->viewerSection, patch->formatSection));
19725f824522SMatthew G. Knepley   /* TODO Shut off MatViewFromOptions() in MatAssemblyEnd() here */
19739566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeJacobian_Patch_Internal(dm, patch->patchSection, patch->patchSection, cellIS, 0.0, 0.0, x, NULL, J, J, ctx));
19749566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
19753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19765f824522SMatthew G. Knepley }
19775f824522SMatthew G. Knepley 
1978a685ae26SLawrence Mitchell /* This function zeros mat on entry */
1979d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchComputeOperator_Internal(PC pc, Vec x, Mat mat, PetscInt point, PetscBool withArtificial)
1980d71ae5a4SJacob Faibussowitsch {
19814bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *)pc->data;
19824bbf5ea8SMatthew G. Knepley   const PetscInt *dofsArray;
19830904074fSPatrick Farrell   const PetscInt *dofsArrayWithAll = NULL;
19844bbf5ea8SMatthew G. Knepley   const PetscInt *cellsArray;
1985eb62eeaaSLawrence Mitchell   PetscInt        ncell, offset, pStart, pEnd, numIntFacets, intFacetOffset;
19864d04e9f1SPatrick Farrell   PetscBool       isNonlinear;
19874bbf5ea8SMatthew G. Knepley 
19884bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
19899566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0));
1990debbdec3SPatrick Farrell   isNonlinear = patch->isNonlinear;
1991ef1023bdSBarry Smith   PetscCheck(patch->usercomputeop, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set callback");
1992c2e6f3c0SFlorian Wechsung   if (withArtificial) {
19939566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->dofsWithArtificial, &dofsArray));
1994c2e6f3c0SFlorian Wechsung   } else {
19959566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->dofs, &dofsArray));
1996c2e6f3c0SFlorian Wechsung   }
199748a46eb9SPierre Jolivet   if (isNonlinear) PetscCall(ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll));
19989566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->cells, &cellsArray));
19999566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd));
20004bbf5ea8SMatthew G. Knepley 
20014bbf5ea8SMatthew G. Knepley   point += pStart;
200263a3b9bcSJacob Faibussowitsch   PetscCheck(point < pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %" PetscInt_FMT " not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, pStart, pEnd);
20034bbf5ea8SMatthew G. Knepley 
20049566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetDof(patch->cellCounts, point, &ncell));
20059566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(patch->cellCounts, point, &offset));
20064bbf5ea8SMatthew G. Knepley   if (ncell <= 0) {
20079566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
20083ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
20094bbf5ea8SMatthew G. Knepley   }
20109566063dSJacob Faibussowitsch   PetscCall(MatZeroEntries(mat));
2011fa84ea4cSLawrence Mitchell   if (patch->precomputeElementTensors) {
2012fa84ea4cSLawrence Mitchell     PetscInt           i;
2013fa84ea4cSLawrence Mitchell     PetscInt           ndof = patch->totalDofsPerCell;
2014fa84ea4cSLawrence Mitchell     const PetscScalar *elementTensors;
2015fa84ea4cSLawrence Mitchell 
20169566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(patch->cellMats, &elementTensors));
2017fa84ea4cSLawrence Mitchell     for (i = 0; i < ncell; i++) {
2018fa84ea4cSLawrence Mitchell       const PetscInt     cell = cellsArray[i + offset];
2019fa84ea4cSLawrence Mitchell       const PetscInt    *idx  = dofsArray + (offset + i) * ndof;
2020fe988be2SFlorian Wechsung       const PetscScalar *v    = elementTensors + patch->precomputedTensorLocations[cell] * ndof * ndof;
20219566063dSJacob Faibussowitsch       PetscCall(MatSetValues(mat, ndof, idx, ndof, idx, v, ADD_VALUES));
2022fa84ea4cSLawrence Mitchell     }
20239566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(patch->cellMats, &elementTensors));
20249566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
20259566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
2026fa84ea4cSLawrence Mitchell   } else {
20272aa6f319SMatthew G. Knepley     /* Cannot reuse the same IS because the geometry info is being cached in it */
20289566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS));
20299371c9d4SSatish Balay     PetscCallBack("PCPatch callback",
20309371c9d4SSatish Balay                   patch->usercomputeop(pc, point, x, mat, patch->cellIS, ncell * patch->totalDofsPerCell, dofsArray + offset * patch->totalDofsPerCell, dofsArrayWithAll ? dofsArrayWithAll + offset * patch->totalDofsPerCell : NULL, patch->usercomputeopctx));
2031fa84ea4cSLawrence Mitchell   }
203259109abcSLawrence Mitchell   if (patch->usercomputeopintfacet) {
20339566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets));
20349566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset));
2035eb62eeaaSLawrence Mitchell     if (numIntFacets > 0) {
2036eb62eeaaSLawrence Mitchell       /* For each interior facet, grab the two cells (in local numbering, and concatenate dof numberings for those cells) */
2037eb62eeaaSLawrence Mitchell       PetscInt       *facetDofs = NULL, *facetDofsWithAll = NULL;
2038eb62eeaaSLawrence Mitchell       const PetscInt *intFacetsArray = NULL;
2039eb62eeaaSLawrence Mitchell       PetscInt        idx            = 0;
2040eb62eeaaSLawrence Mitchell       PetscInt        i, c, d;
2041de2d1767SPatrick Farrell       PetscInt        fStart;
2042b6bb21d1SLawrence Mitchell       DM              dm, plex;
2043eb62eeaaSLawrence Mitchell       IS              facetIS    = NULL;
2044eb62eeaaSLawrence Mitchell       const PetscInt *facetCells = NULL;
20457a50e09dSPatrick Farrell 
20469566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells));
20479566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray));
20489566063dSJacob Faibussowitsch       PetscCall(PCGetDM(pc, &dm));
20499566063dSJacob Faibussowitsch       PetscCall(DMConvert(dm, DMPLEX, &plex));
2050b6bb21d1SLawrence Mitchell       dm = plex;
20519566063dSJacob Faibussowitsch       PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, NULL));
2052eb62eeaaSLawrence Mitchell       /* FIXME: Pull this malloc out. */
20539566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofs));
205448a46eb9SPierre Jolivet       if (dofsArrayWithAll) PetscCall(PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofsWithAll));
2055f98464cbSLawrence Mitchell       if (patch->precomputeElementTensors) {
2056f98464cbSLawrence Mitchell         PetscInt           nFacetDof = 2 * patch->totalDofsPerCell;
2057f98464cbSLawrence Mitchell         const PetscScalar *elementTensors;
2058f98464cbSLawrence Mitchell 
20599566063dSJacob Faibussowitsch         PetscCall(VecGetArrayRead(patch->intFacetMats, &elementTensors));
2060f98464cbSLawrence Mitchell 
2061f98464cbSLawrence Mitchell         for (i = 0; i < numIntFacets; i++) {
2062f98464cbSLawrence Mitchell           const PetscInt     facet = intFacetsArray[i + intFacetOffset];
2063de2d1767SPatrick Farrell           const PetscScalar *v     = elementTensors + patch->precomputedIntFacetTensorLocations[facet - fStart] * nFacetDof * nFacetDof;
2064f98464cbSLawrence Mitchell           idx                      = 0;
2065f98464cbSLawrence Mitchell           /*
2066f1580f4eSBarry Smith      0--1
2067f1580f4eSBarry Smith      |\-|
2068f1580f4eSBarry Smith      |+\|
2069f1580f4eSBarry Smith      2--3
2070f1580f4eSBarry Smith      [0, 2, 3, 0, 1, 3]
2071f98464cbSLawrence Mitchell    */
2072f98464cbSLawrence Mitchell           for (c = 0; c < 2; c++) {
2073f98464cbSLawrence Mitchell             const PetscInt cell = facetCells[2 * (intFacetOffset + i) + c];
2074f98464cbSLawrence Mitchell             for (d = 0; d < patch->totalDofsPerCell; d++) {
2075f98464cbSLawrence Mitchell               facetDofs[idx] = dofsArray[(offset + cell) * patch->totalDofsPerCell + d];
2076f98464cbSLawrence Mitchell               idx++;
2077f98464cbSLawrence Mitchell             }
2078f98464cbSLawrence Mitchell           }
20799566063dSJacob Faibussowitsch           PetscCall(MatSetValues(mat, nFacetDof, facetDofs, nFacetDof, facetDofs, v, ADD_VALUES));
2080f98464cbSLawrence Mitchell         }
20819566063dSJacob Faibussowitsch         PetscCall(VecRestoreArrayRead(patch->intFacetMats, &elementTensors));
2082f98464cbSLawrence Mitchell       } else {
2083eb62eeaaSLawrence Mitchell         /*
2084f1580f4eSBarry Smith      0--1
2085f1580f4eSBarry Smith      |\-|
2086f1580f4eSBarry Smith      |+\|
2087f1580f4eSBarry Smith      2--3
2088f1580f4eSBarry Smith      [0, 2, 3, 0, 1, 3]
2089eb62eeaaSLawrence Mitchell    */
2090eb62eeaaSLawrence Mitchell         for (i = 0; i < numIntFacets; i++) {
2091eb62eeaaSLawrence Mitchell           for (c = 0; c < 2; c++) {
2092eb62eeaaSLawrence Mitchell             const PetscInt cell = facetCells[2 * (intFacetOffset + i) + c];
2093eb62eeaaSLawrence Mitchell             for (d = 0; d < patch->totalDofsPerCell; d++) {
2094eb62eeaaSLawrence Mitchell               facetDofs[idx] = dofsArray[(offset + cell) * patch->totalDofsPerCell + d];
2095ad540459SPierre Jolivet               if (dofsArrayWithAll) facetDofsWithAll[idx] = dofsArrayWithAll[(offset + cell) * patch->totalDofsPerCell + d];
2096eb62eeaaSLawrence Mitchell               idx++;
2097eb62eeaaSLawrence Mitchell             }
2098eb62eeaaSLawrence Mitchell           }
2099eb62eeaaSLawrence Mitchell         }
21009566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray + intFacetOffset, PETSC_USE_POINTER, &facetIS));
21019566063dSJacob Faibussowitsch         PetscCall(patch->usercomputeopintfacet(pc, point, x, mat, facetIS, 2 * numIntFacets * patch->totalDofsPerCell, facetDofs, facetDofsWithAll, patch->usercomputeopintfacetctx));
21029566063dSJacob Faibussowitsch         PetscCall(ISDestroy(&facetIS));
2103f98464cbSLawrence Mitchell       }
21049566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(patch->intFacetsToPatchCell, &facetCells));
21059566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(patch->intFacets, &intFacetsArray));
21069566063dSJacob Faibussowitsch       PetscCall(PetscFree(facetDofs));
21079566063dSJacob Faibussowitsch       PetscCall(PetscFree(facetDofsWithAll));
21089566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dm));
2109eb62eeaaSLawrence Mitchell     }
211059109abcSLawrence Mitchell   }
21116710cc29SPatrick Farrell 
21129566063dSJacob Faibussowitsch   PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
21139566063dSJacob Faibussowitsch   PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
21146710cc29SPatrick Farrell 
2115c73d2cf6SLawrence Mitchell   if (!(withArtificial || isNonlinear) && patch->denseinverse) {
2116c73d2cf6SLawrence Mitchell     MatFactorInfo info;
2117c73d2cf6SLawrence Mitchell     PetscBool     flg;
21189566063dSJacob Faibussowitsch     PetscCall(PetscObjectTypeCompare((PetscObject)mat, MATSEQDENSE, &flg));
211928b400f6SJacob Faibussowitsch     PetscCheck(flg, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Invalid Mat type for dense inverse");
21209566063dSJacob Faibussowitsch     PetscCall(MatFactorInfoInitialize(&info));
21219566063dSJacob Faibussowitsch     PetscCall(MatLUFactor(mat, NULL, NULL, &info));
21229566063dSJacob Faibussowitsch     PetscCall(MatSeqDenseInvertFactors_Private(mat));
2123c73d2cf6SLawrence Mitchell   }
21249566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->cellIS));
21254d04e9f1SPatrick Farrell   if (withArtificial) {
21269566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(patch->dofsWithArtificial, &dofsArray));
2127c2e6f3c0SFlorian Wechsung   } else {
21289566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(patch->dofs, &dofsArray));
2129c2e6f3c0SFlorian Wechsung   }
213048a46eb9SPierre Jolivet   if (isNonlinear) PetscCall(ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll));
21319566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->cells, &cellsArray));
21322aa6f319SMatthew G. Knepley   if (patch->viewMatrix) {
21332aa6f319SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
21342aa6f319SMatthew G. Knepley 
213563a3b9bcSJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN - 1, "Patch matrix for Point %" PetscInt_FMT, point));
21369566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)mat, name));
21379566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)mat, patch->viewerMatrix, patch->formatMatrix));
21382aa6f319SMatthew G. Knepley   }
21399566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
21403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21414bbf5ea8SMatthew G. Knepley }
21424bbf5ea8SMatthew G. Knepley 
2143d71ae5a4SJacob Faibussowitsch static PetscErrorCode MatSetValues_PCPatch_Private(Mat mat, PetscInt m, const PetscInt idxm[], PetscInt n, const PetscInt idxn[], const PetscScalar *v, InsertMode addv)
2144d71ae5a4SJacob Faibussowitsch {
2145fa84ea4cSLawrence Mitchell   Vec          data;
2146fa84ea4cSLawrence Mitchell   PetscScalar *array;
2147fe988be2SFlorian Wechsung   PetscInt     bs, nz, i, j, cell;
2148fa84ea4cSLawrence Mitchell 
21499566063dSJacob Faibussowitsch   PetscCall(MatShellGetContext(mat, &data));
21509566063dSJacob Faibussowitsch   PetscCall(VecGetBlockSize(data, &bs));
21519566063dSJacob Faibussowitsch   PetscCall(VecGetSize(data, &nz));
21529566063dSJacob Faibussowitsch   PetscCall(VecGetArray(data, &array));
215308401ef6SPierre Jolivet   PetscCheck(m == n, PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Only for square insertion");
215433cbca70SPatrick Farrell   cell = (PetscInt)(idxm[0] / bs); /* use the fact that this is called once per cell */
2155fa84ea4cSLawrence Mitchell   for (i = 0; i < m; i++) {
215608401ef6SPierre Jolivet     PetscCheck(idxm[i] == idxn[i], PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Row and column indices must match!");
2157fa84ea4cSLawrence Mitchell     for (j = 0; j < n; j++) {
2158fa84ea4cSLawrence Mitchell       const PetscScalar v_ = v[i * bs + j];
2159fa84ea4cSLawrence Mitchell       /* Indexing is special to the data structure we have! */
2160fa84ea4cSLawrence Mitchell       if (addv == INSERT_VALUES) {
2161fe988be2SFlorian Wechsung         array[cell * bs * bs + i * bs + j] = v_;
2162fa84ea4cSLawrence Mitchell       } else {
2163fe988be2SFlorian Wechsung         array[cell * bs * bs + i * bs + j] += v_;
2164fa84ea4cSLawrence Mitchell       }
2165fa84ea4cSLawrence Mitchell     }
2166fa84ea4cSLawrence Mitchell   }
21679566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(data, &array));
21683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2169fa84ea4cSLawrence Mitchell }
2170fa84ea4cSLawrence Mitchell 
2171d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchPrecomputePatchTensors_Private(PC pc)
2172d71ae5a4SJacob Faibussowitsch {
2173fa84ea4cSLawrence Mitchell   PC_PATCH       *patch = (PC_PATCH *)pc->data;
2174fa84ea4cSLawrence Mitchell   const PetscInt *cellsArray;
2175fa84ea4cSLawrence Mitchell   PetscInt        ncell, offset;
2176fa84ea4cSLawrence Mitchell   const PetscInt *dofMapArray;
2177fa84ea4cSLawrence Mitchell   PetscInt        i, j;
2178fa84ea4cSLawrence Mitchell   IS              dofMap;
2179fa84ea4cSLawrence Mitchell   IS              cellIS;
2180fa84ea4cSLawrence Mitchell   const PetscInt  ndof = patch->totalDofsPerCell;
2181fa84ea4cSLawrence Mitchell   Mat             vecMat;
2182fe988be2SFlorian Wechsung   PetscInt        cStart, cEnd;
2183fe988be2SFlorian Wechsung   DM              dm, plex;
2184fe988be2SFlorian Wechsung 
21859566063dSJacob Faibussowitsch   PetscCall(ISGetSize(patch->cells, &ncell));
2186e9c2c94bSFlorian Wechsung   if (!ncell) { /* No cells to assemble over -> skip */
21873ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2188e9c2c94bSFlorian Wechsung   }
2189e9c2c94bSFlorian Wechsung 
21909566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0));
2191fa84ea4cSLawrence Mitchell 
21929566063dSJacob Faibussowitsch   PetscCall(PCGetDM(pc, &dm));
21939566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
2194b6bb21d1SLawrence Mitchell   dm = plex;
2195fa84ea4cSLawrence Mitchell   if (!patch->allCells) {
2196fa84ea4cSLawrence Mitchell     PetscHSetI    cells;
2197fa84ea4cSLawrence Mitchell     PetscHashIter hi;
2198fa84ea4cSLawrence Mitchell     PetscInt      pStart, pEnd;
2199fa84ea4cSLawrence Mitchell     PetscInt     *allCells = NULL;
22009566063dSJacob Faibussowitsch     PetscCall(PetscHSetICreate(&cells));
22019566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->cells, &cellsArray));
22029566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd));
2203fa84ea4cSLawrence Mitchell     for (i = pStart; i < pEnd; i++) {
22049566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(patch->cellCounts, i, &ncell));
22059566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(patch->cellCounts, i, &offset));
2206fa84ea4cSLawrence Mitchell       if (ncell <= 0) continue;
220748a46eb9SPierre Jolivet       for (j = 0; j < ncell; j++) PetscCall(PetscHSetIAdd(cells, cellsArray[offset + j]));
2208fa84ea4cSLawrence Mitchell     }
22099566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(patch->cells, &cellsArray));
22109566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(cells, &ncell));
22119566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(ncell, &allCells));
22129566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
22139566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(cEnd - cStart, &patch->precomputedTensorLocations));
2214fa84ea4cSLawrence Mitchell     i = 0;
2215fa84ea4cSLawrence Mitchell     PetscHashIterBegin(cells, hi);
2216fa84ea4cSLawrence Mitchell     while (!PetscHashIterAtEnd(cells, hi)) {
2217fe988be2SFlorian Wechsung       PetscHashIterGetKey(cells, hi, allCells[i]);
2218fe988be2SFlorian Wechsung       patch->precomputedTensorLocations[allCells[i]] = i;
2219fa84ea4cSLawrence Mitchell       PetscHashIterNext(cells, hi);
2220fe988be2SFlorian Wechsung       i++;
2221fa84ea4cSLawrence Mitchell     }
22229566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&cells));
22239566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, allCells, PETSC_OWN_POINTER, &patch->allCells));
2224fa84ea4cSLawrence Mitchell   }
22259566063dSJacob Faibussowitsch   PetscCall(ISGetSize(patch->allCells, &ncell));
2226fa84ea4cSLawrence Mitchell   if (!patch->cellMats) {
22279566063dSJacob Faibussowitsch     PetscCall(VecCreateSeq(PETSC_COMM_SELF, ncell * ndof * ndof, &patch->cellMats));
22289566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(patch->cellMats, ndof));
2229fa84ea4cSLawrence Mitchell   }
22309566063dSJacob Faibussowitsch   PetscCall(VecSet(patch->cellMats, 0));
2231fa84ea4cSLawrence Mitchell 
2232d0609cedSBarry Smith   PetscCall(MatCreateShell(PETSC_COMM_SELF, ncell * ndof, ncell * ndof, ncell * ndof, ncell * ndof, (void *)patch->cellMats, &vecMat));
22339566063dSJacob Faibussowitsch   PetscCall(MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void (*)(void)) & MatSetValues_PCPatch_Private));
22349566063dSJacob Faibussowitsch   PetscCall(ISGetSize(patch->allCells, &ncell));
22359566063dSJacob Faibussowitsch   PetscCall(ISCreateStride(PETSC_COMM_SELF, ndof * ncell, 0, 1, &dofMap));
22369566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(dofMap, &dofMapArray));
22379566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->allCells, &cellsArray));
22389566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray, PETSC_USE_POINTER, &cellIS));
2239fa84ea4cSLawrence Mitchell   /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */
2240792fecdfSBarry Smith   PetscCallBack("PCPatch callback", patch->usercomputeop(pc, -1, NULL, vecMat, cellIS, ndof * ncell, dofMapArray, NULL, patch->usercomputeopctx));
22419566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&cellIS));
22429566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&vecMat));
22439566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->allCells, &cellsArray));
22449566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(dofMap, &dofMapArray));
22459566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&dofMap));
2246f98464cbSLawrence Mitchell 
2247f98464cbSLawrence Mitchell   if (patch->usercomputeopintfacet) {
2248f98464cbSLawrence Mitchell     PetscInt        nIntFacets;
2249f98464cbSLawrence Mitchell     IS              intFacetsIS;
2250f98464cbSLawrence Mitchell     const PetscInt *intFacetsArray = NULL;
2251f98464cbSLawrence Mitchell     if (!patch->allIntFacets) {
2252f98464cbSLawrence Mitchell       PetscHSetI    facets;
2253f98464cbSLawrence Mitchell       PetscHashIter hi;
2254f98464cbSLawrence Mitchell       PetscInt      pStart, pEnd, fStart, fEnd;
2255f98464cbSLawrence Mitchell       PetscInt     *allIntFacets = NULL;
22569566063dSJacob Faibussowitsch       PetscCall(PetscHSetICreate(&facets));
22579566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray));
22589566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(patch->intFacetCounts, &pStart, &pEnd));
22599566063dSJacob Faibussowitsch       PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
2260f98464cbSLawrence Mitchell       for (i = pStart; i < pEnd; i++) {
22619566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->intFacetCounts, i, &nIntFacets));
22629566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->intFacetCounts, i, &offset));
2263f98464cbSLawrence Mitchell         if (nIntFacets <= 0) continue;
226448a46eb9SPierre Jolivet         for (j = 0; j < nIntFacets; j++) PetscCall(PetscHSetIAdd(facets, intFacetsArray[offset + j]));
2265f98464cbSLawrence Mitchell       }
22669566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(patch->intFacets, &intFacetsArray));
22679566063dSJacob Faibussowitsch       PetscCall(PetscHSetIGetSize(facets, &nIntFacets));
22689566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nIntFacets, &allIntFacets));
22699566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fEnd - fStart, &patch->precomputedIntFacetTensorLocations));
2270f98464cbSLawrence Mitchell       i = 0;
2271f98464cbSLawrence Mitchell       PetscHashIterBegin(facets, hi);
2272f98464cbSLawrence Mitchell       while (!PetscHashIterAtEnd(facets, hi)) {
2273f98464cbSLawrence Mitchell         PetscHashIterGetKey(facets, hi, allIntFacets[i]);
2274de2d1767SPatrick Farrell         patch->precomputedIntFacetTensorLocations[allIntFacets[i] - fStart] = i;
2275f98464cbSLawrence Mitchell         PetscHashIterNext(facets, hi);
2276f98464cbSLawrence Mitchell         i++;
2277f98464cbSLawrence Mitchell       }
22789566063dSJacob Faibussowitsch       PetscCall(PetscHSetIDestroy(&facets));
22799566063dSJacob Faibussowitsch       PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, allIntFacets, PETSC_OWN_POINTER, &patch->allIntFacets));
2280f98464cbSLawrence Mitchell     }
22819566063dSJacob Faibussowitsch     PetscCall(ISGetSize(patch->allIntFacets, &nIntFacets));
2282f98464cbSLawrence Mitchell     if (!patch->intFacetMats) {
22839566063dSJacob Faibussowitsch       PetscCall(VecCreateSeq(PETSC_COMM_SELF, nIntFacets * ndof * ndof * 4, &patch->intFacetMats));
22849566063dSJacob Faibussowitsch       PetscCall(VecSetBlockSize(patch->intFacetMats, ndof * 2));
2285f98464cbSLawrence Mitchell     }
22869566063dSJacob Faibussowitsch     PetscCall(VecSet(patch->intFacetMats, 0));
2287f98464cbSLawrence Mitchell 
2288d0609cedSBarry Smith     PetscCall(MatCreateShell(PETSC_COMM_SELF, nIntFacets * ndof * 2, nIntFacets * ndof * 2, nIntFacets * ndof * 2, nIntFacets * ndof * 2, (void *)patch->intFacetMats, &vecMat));
22899566063dSJacob Faibussowitsch     PetscCall(MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void (*)(void)) & MatSetValues_PCPatch_Private));
22909566063dSJacob Faibussowitsch     PetscCall(ISCreateStride(PETSC_COMM_SELF, 2 * ndof * nIntFacets, 0, 1, &dofMap));
22919566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(dofMap, &dofMapArray));
22929566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->allIntFacets, &intFacetsArray));
22939566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, intFacetsArray, PETSC_USE_POINTER, &intFacetsIS));
2294f98464cbSLawrence Mitchell     /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */
2295792fecdfSBarry Smith     PetscCallBack("PCPatch callback (interior facets)", patch->usercomputeopintfacet(pc, -1, NULL, vecMat, intFacetsIS, 2 * ndof * nIntFacets, dofMapArray, NULL, patch->usercomputeopintfacetctx));
22969566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&intFacetsIS));
22979566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&vecMat));
22989566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(patch->allIntFacets, &intFacetsArray));
22999566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(dofMap, &dofMapArray));
23009566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dofMap));
2301f98464cbSLawrence Mitchell   }
23029566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
23039566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
2304fa84ea4cSLawrence Mitchell 
23053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2306fa84ea4cSLawrence Mitchell }
2307fa84ea4cSLawrence Mitchell 
2308d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatch_ScatterLocal_Private(PC pc, PetscInt p, Vec x, Vec y, InsertMode mode, ScatterMode scat, PatchScatterType scattertype)
2309d71ae5a4SJacob Faibussowitsch {
23104bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch     = (PC_PATCH *)pc->data;
23114bbf5ea8SMatthew G. Knepley   const PetscScalar *xArray    = NULL;
23124bbf5ea8SMatthew G. Knepley   PetscScalar       *yArray    = NULL;
23134bbf5ea8SMatthew G. Knepley   const PetscInt    *gtolArray = NULL;
23144bbf5ea8SMatthew G. Knepley   PetscInt           dof, offset, lidx;
23154bbf5ea8SMatthew G. Knepley 
23164bbf5ea8SMatthew G. Knepley   PetscFunctionBeginHot;
23179566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(x, &xArray));
23189566063dSJacob Faibussowitsch   PetscCall(VecGetArray(y, &yArray));
23190904074fSPatrick Farrell   if (scattertype == SCATTER_WITHARTIFICIAL) {
23209566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof));
23219566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offset));
23229566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->gtolWithArtificial, &gtolArray));
23230904074fSPatrick Farrell   } else if (scattertype == SCATTER_WITHALL) {
23249566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(patch->gtolCountsWithAll, p, &dof));
23259566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offset));
23269566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->gtolWithAll, &gtolArray));
2327c2e6f3c0SFlorian Wechsung   } else {
23289566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &dof));
23299566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset));
23309566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->gtol, &gtolArray));
2331c2e6f3c0SFlorian Wechsung   }
23322472a847SBarry Smith   PetscCheck(mode != INSERT_VALUES || scat == SCATTER_FORWARD, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't insert if not scattering forward");
23332472a847SBarry Smith   PetscCheck(mode != ADD_VALUES || scat == SCATTER_REVERSE, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't add if not scattering reverse");
23344bbf5ea8SMatthew G. Knepley   for (lidx = 0; lidx < dof; ++lidx) {
23354bbf5ea8SMatthew G. Knepley     const PetscInt gidx = gtolArray[offset + lidx];
23364bbf5ea8SMatthew G. Knepley 
23374bbf5ea8SMatthew G. Knepley     if (mode == INSERT_VALUES) yArray[lidx] = xArray[gidx]; /* Forward */
23384bbf5ea8SMatthew G. Knepley     else yArray[gidx] += xArray[lidx];                      /* Reverse */
23394bbf5ea8SMatthew G. Knepley   }
23400904074fSPatrick Farrell   if (scattertype == SCATTER_WITHARTIFICIAL) {
23419566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(patch->gtolWithArtificial, &gtolArray));
23420904074fSPatrick Farrell   } else if (scattertype == SCATTER_WITHALL) {
23439566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(patch->gtolWithAll, &gtolArray));
2344c2e6f3c0SFlorian Wechsung   } else {
23459566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(patch->gtol, &gtolArray));
2346c2e6f3c0SFlorian Wechsung   }
23479566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(x, &xArray));
23489566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(y, &yArray));
23493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
23504bbf5ea8SMatthew G. Knepley }
23514bbf5ea8SMatthew G. Knepley 
2352d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_PATCH_Linear(PC pc)
2353d71ae5a4SJacob Faibussowitsch {
2354dadc69c5SMatthew G. Knepley   PC_PATCH   *patch = (PC_PATCH *)pc->data;
2355dadc69c5SMatthew G. Knepley   const char *prefix;
2356dadc69c5SMatthew G. Knepley   PetscInt    i;
2357dadc69c5SMatthew G. Knepley 
2358dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2359dadc69c5SMatthew G. Knepley   if (!pc->setupcalled) {
23607827d75bSBarry Smith     PetscCheck(patch->save_operators || !patch->denseinverse, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Can't have dense inverse without save operators");
2361c73d2cf6SLawrence Mitchell     if (!patch->denseinverse) {
23629566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(patch->npatch, &patch->solver));
23639566063dSJacob Faibussowitsch       PetscCall(PCGetOptionsPrefix(pc, &prefix));
2364dadc69c5SMatthew G. Knepley       for (i = 0; i < patch->npatch; ++i) {
2365dadc69c5SMatthew G. Knepley         KSP ksp;
2366dadc69c5SMatthew G. Knepley         PC  subpc;
2367dadc69c5SMatthew G. Knepley 
23689566063dSJacob Faibussowitsch         PetscCall(KSPCreate(PETSC_COMM_SELF, &ksp));
23693821be0aSBarry Smith         PetscCall(KSPSetNestLevel(ksp, pc->kspnestlevel));
23709566063dSJacob Faibussowitsch         PetscCall(KSPSetErrorIfNotConverged(ksp, pc->erroriffailure));
23719566063dSJacob Faibussowitsch         PetscCall(KSPSetOptionsPrefix(ksp, prefix));
23729566063dSJacob Faibussowitsch         PetscCall(KSPAppendOptionsPrefix(ksp, "sub_"));
23739566063dSJacob Faibussowitsch         PetscCall(PetscObjectIncrementTabLevel((PetscObject)ksp, (PetscObject)pc, 1));
23749566063dSJacob Faibussowitsch         PetscCall(KSPGetPC(ksp, &subpc));
23759566063dSJacob Faibussowitsch         PetscCall(PetscObjectIncrementTabLevel((PetscObject)subpc, (PetscObject)pc, 1));
2376dadc69c5SMatthew G. Knepley         patch->solver[i] = (PetscObject)ksp;
2377dadc69c5SMatthew G. Knepley       }
2378dadc69c5SMatthew G. Knepley     }
2379c73d2cf6SLawrence Mitchell   }
2380dadc69c5SMatthew G. Knepley   if (patch->save_operators) {
23811baa6e33SBarry Smith     if (patch->precomputeElementTensors) PetscCall(PCPatchPrecomputePatchTensors_Private(pc));
2382dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {
23839566063dSJacob Faibussowitsch       PetscCall(PCPatchComputeOperator_Internal(pc, NULL, patch->mat[i], i, PETSC_FALSE));
2384c73d2cf6SLawrence Mitchell       if (!patch->denseinverse) {
23859566063dSJacob Faibussowitsch         PetscCall(KSPSetOperators((KSP)patch->solver[i], patch->mat[i], patch->mat[i]));
23869d4fc724SLawrence Mitchell       } else if (patch->mat[i] && !patch->densesolve) {
23879d4fc724SLawrence Mitchell         /* Setup matmult callback */
23889566063dSJacob Faibussowitsch         PetscCall(MatGetOperation(patch->mat[i], MATOP_MULT, (void (**)(void)) & patch->densesolve));
2389dadc69c5SMatthew G. Knepley       }
2390dadc69c5SMatthew G. Knepley     }
2391c73d2cf6SLawrence Mitchell   }
239234d8b122SPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
239334d8b122SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {
23941202d238SPatrick Farrell       /* Instead of padding patch->patchUpdate with zeros to get */
23951202d238SPatrick Farrell       /* patch->patchUpdateWithArtificial and then multiplying with the matrix, */
239634d8b122SPatrick Farrell       /* just get rid of the columns that correspond to the dofs with */
239734d8b122SPatrick Farrell       /* artificial bcs. That's of course fairly inefficient, hopefully we */
239834d8b122SPatrick Farrell       /* can just assemble the rectangular matrix in the first place. */
239934d8b122SPatrick Farrell       Mat      matSquare;
240034d8b122SPatrick Farrell       IS       rowis;
240134d8b122SPatrick Farrell       PetscInt dof;
240234d8b122SPatrick Farrell 
24039566063dSJacob Faibussowitsch       PetscCall(MatGetSize(patch->mat[i], &dof, NULL));
240434d8b122SPatrick Farrell       if (dof == 0) {
240534d8b122SPatrick Farrell         patch->matWithArtificial[i] = NULL;
240634d8b122SPatrick Farrell         continue;
240734d8b122SPatrick Farrell       }
240834d8b122SPatrick Farrell 
24099566063dSJacob Faibussowitsch       PetscCall(PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE));
24109566063dSJacob Faibussowitsch       PetscCall(PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE));
241134d8b122SPatrick Farrell 
24129566063dSJacob Faibussowitsch       PetscCall(MatGetSize(matSquare, &dof, NULL));
24139566063dSJacob Faibussowitsch       PetscCall(ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis));
241434d8b122SPatrick Farrell       if (pc->setupcalled) {
24159566063dSJacob Faibussowitsch         PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_REUSE_MATRIX, &patch->matWithArtificial[i]));
241634d8b122SPatrick Farrell       } else {
24179566063dSJacob Faibussowitsch         PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &patch->matWithArtificial[i]));
241834d8b122SPatrick Farrell       }
24199566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&rowis));
24209566063dSJacob Faibussowitsch       PetscCall(MatDestroy(&matSquare));
242134d8b122SPatrick Farrell     }
242234d8b122SPatrick Farrell   }
24233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2424dadc69c5SMatthew G. Knepley }
2425dadc69c5SMatthew G. Knepley 
2426d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_PATCH(PC pc)
2427d71ae5a4SJacob Faibussowitsch {
24284bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
2429557beb66SLawrence Mitchell   PetscInt  i;
243039fd2e8aSPatrick Farrell   PetscBool isNonlinear;
24319d4fc724SLawrence Mitchell   PetscInt  maxDof = -1, maxDofWithArtificial = -1;
24324bbf5ea8SMatthew G. Knepley 
24334bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
24344bbf5ea8SMatthew G. Knepley   if (!pc->setupcalled) {
24354bbf5ea8SMatthew G. Knepley     PetscInt pStart, pEnd, p;
24364bbf5ea8SMatthew G. Knepley     PetscInt localSize;
24374bbf5ea8SMatthew G. Knepley 
24389566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(PC_Patch_CreatePatches, pc, 0, 0, 0));
24394bbf5ea8SMatthew G. Knepley 
2440debbdec3SPatrick Farrell     isNonlinear = patch->isNonlinear;
24415f824522SMatthew G. Knepley     if (!patch->nsubspaces) {
2442b6bb21d1SLawrence Mitchell       DM           dm, plex;
24435f824522SMatthew G. Knepley       PetscSection s;
2444bd026e97SJed Brown       PetscInt     cStart, cEnd, c, Nf, f, numGlobalBcs = 0, *globalBcs, *Nb, **cellDofs;
24455f824522SMatthew G. Knepley 
24469566063dSJacob Faibussowitsch       PetscCall(PCGetDM(pc, &dm));
244728b400f6SJacob Faibussowitsch       PetscCheck(dm, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Must set DM for PCPATCH or call PCPatchSetDiscretisationInfo()");
24489566063dSJacob Faibussowitsch       PetscCall(DMConvert(dm, DMPLEX, &plex));
2449b6bb21d1SLawrence Mitchell       dm = plex;
24509566063dSJacob Faibussowitsch       PetscCall(DMGetLocalSection(dm, &s));
24519566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetNumFields(s, &Nf));
24529566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
24535f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
24545f824522SMatthew G. Knepley         PetscInt cdof;
24559566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
24565f824522SMatthew G. Knepley         numGlobalBcs += cdof;
24575f824522SMatthew G. Knepley       }
24589566063dSJacob Faibussowitsch       PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
24599566063dSJacob Faibussowitsch       PetscCall(PetscMalloc3(Nf, &Nb, Nf, &cellDofs, numGlobalBcs, &globalBcs));
24605f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
24615f824522SMatthew G. Knepley         PetscFE        fe;
24625f824522SMatthew G. Knepley         PetscDualSpace sp;
24635f824522SMatthew G. Knepley         PetscInt       cdoff = 0;
24645f824522SMatthew G. Knepley 
24659566063dSJacob Faibussowitsch         PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
24669566063dSJacob Faibussowitsch         /* PetscCall(PetscFEGetNumComponents(fe, &Nc[f])); */
24679566063dSJacob Faibussowitsch         PetscCall(PetscFEGetDualSpace(fe, &sp));
24689566063dSJacob Faibussowitsch         PetscCall(PetscDualSpaceGetDimension(sp, &Nb[f]));
24695f824522SMatthew G. Knepley 
24709566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1((cEnd - cStart) * Nb[f], &cellDofs[f]));
24715f824522SMatthew G. Knepley         for (c = cStart; c < cEnd; ++c) {
24725f824522SMatthew G. Knepley           PetscInt *closure = NULL;
24735f824522SMatthew G. Knepley           PetscInt  clSize  = 0, cl;
24745f824522SMatthew G. Knepley 
24759566063dSJacob Faibussowitsch           PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure));
24765f824522SMatthew G. Knepley           for (cl = 0; cl < clSize * 2; cl += 2) {
24775f824522SMatthew G. Knepley             const PetscInt p = closure[cl];
24785f824522SMatthew G. Knepley             PetscInt       fdof, d, foff;
24795f824522SMatthew G. Knepley 
24809566063dSJacob Faibussowitsch             PetscCall(PetscSectionGetFieldDof(s, p, f, &fdof));
24819566063dSJacob Faibussowitsch             PetscCall(PetscSectionGetFieldOffset(s, p, f, &foff));
24825f824522SMatthew G. Knepley             for (d = 0; d < fdof; ++d, ++cdoff) cellDofs[f][cdoff] = foff + d;
24835f824522SMatthew G. Knepley           }
24849566063dSJacob Faibussowitsch           PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure));
24855f824522SMatthew G. Knepley         }
248663a3b9bcSJacob 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]);
24875f824522SMatthew G. Knepley       }
24885f824522SMatthew G. Knepley       numGlobalBcs = 0;
24895f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
24905f824522SMatthew G. Knepley         const PetscInt *ind;
24915f824522SMatthew G. Knepley         PetscInt        off, cdof, d;
24925f824522SMatthew G. Knepley 
24939566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
24949566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
24959566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(s, p, &ind));
24965f824522SMatthew G. Knepley         for (d = 0; d < cdof; ++d) globalBcs[numGlobalBcs++] = off + ind[d];
24975f824522SMatthew G. Knepley       }
24985f824522SMatthew G. Knepley 
24999566063dSJacob Faibussowitsch       PetscCall(PCPatchSetDiscretisationInfoCombined(pc, dm, Nb, (const PetscInt **)cellDofs, numGlobalBcs, globalBcs, numGlobalBcs, globalBcs));
250048a46eb9SPierre Jolivet       for (f = 0; f < Nf; ++f) PetscCall(PetscFree(cellDofs[f]));
25019566063dSJacob Faibussowitsch       PetscCall(PetscFree3(Nb, cellDofs, globalBcs));
25029566063dSJacob Faibussowitsch       PetscCall(PCPatchSetComputeFunction(pc, PCPatchComputeFunction_DMPlex_Private, NULL));
25039566063dSJacob Faibussowitsch       PetscCall(PCPatchSetComputeOperator(pc, PCPatchComputeOperator_DMPlex_Private, NULL));
25049566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dm));
25055f824522SMatthew G. Knepley     }
25065f824522SMatthew G. Knepley 
25074bbf5ea8SMatthew G. Knepley     localSize = patch->subspaceOffsets[patch->nsubspaces];
25089566063dSJacob Faibussowitsch     PetscCall(VecCreateSeq(PETSC_COMM_SELF, localSize, &patch->localRHS));
25099566063dSJacob Faibussowitsch     PetscCall(VecSetUp(patch->localRHS));
25109566063dSJacob Faibussowitsch     PetscCall(VecDuplicate(patch->localRHS, &patch->localUpdate));
25119566063dSJacob Faibussowitsch     PetscCall(PCPatchCreateCellPatches(pc));
25129566063dSJacob Faibussowitsch     PetscCall(PCPatchCreateCellPatchDiscretisationInfo(pc));
25134bbf5ea8SMatthew G. Knepley 
25144bbf5ea8SMatthew G. Knepley     /* OK, now build the work vectors */
25159566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, &pEnd));
2516c2e6f3c0SFlorian Wechsung 
251748a46eb9SPierre Jolivet     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) PetscCall(PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithArtificial));
251848a46eb9SPierre Jolivet     if (isNonlinear) PetscCall(PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithAll));
25194bbf5ea8SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
25204bbf5ea8SMatthew G. Knepley       PetscInt dof;
25214bbf5ea8SMatthew G. Knepley 
25229566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &dof));
25232f613bf5SBarry Smith       maxDof = PetscMax(maxDof, dof);
25240904074fSPatrick Farrell       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
25253bb0e8f7SKarl Rupp         const PetscInt *gtolArray, *gtolArrayWithArtificial = NULL;
25263bb0e8f7SKarl Rupp         PetscInt        numPatchDofs, offset;
25273bb0e8f7SKarl Rupp         PetscInt        numPatchDofsWithArtificial, offsetWithArtificial;
25283bb0e8f7SKarl Rupp         PetscInt        dofWithoutArtificialCounter = 0;
25293bb0e8f7SKarl Rupp         PetscInt       *patchWithoutArtificialToWithArtificialArray;
25303bb0e8f7SKarl Rupp 
25319566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof));
25329d4fc724SLawrence Mitchell         maxDofWithArtificial = PetscMax(maxDofWithArtificial, dof);
2533c2e6f3c0SFlorian Wechsung 
2534e047a90bSFlorian Wechsung         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
2535e047a90bSFlorian Wechsung         /* the index in the patch with all dofs */
25369566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->gtol, &gtolArray));
253763deea8eSPatrick Farrell 
25389566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs));
253947aca4a6SPatrick Farrell         if (numPatchDofs == 0) {
254047aca4a6SPatrick Farrell           patch->dofMappingWithoutToWithArtificial[p - pStart] = NULL;
254147aca4a6SPatrick Farrell           continue;
254247aca4a6SPatrick Farrell         }
254363deea8eSPatrick Farrell 
25449566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset));
25459566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial));
25469566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &numPatchDofsWithArtificial));
25479566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offsetWithArtificial));
2548c2e6f3c0SFlorian Wechsung 
25499566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(numPatchDofs, &patchWithoutArtificialToWithArtificialArray));
2550b0c21b6aSKarl Rupp         for (i = 0; i < numPatchDofsWithArtificial; i++) {
2551e047a90bSFlorian Wechsung           if (gtolArrayWithArtificial[i + offsetWithArtificial] == gtolArray[offset + dofWithoutArtificialCounter]) {
2552c2e6f3c0SFlorian Wechsung             patchWithoutArtificialToWithArtificialArray[dofWithoutArtificialCounter] = i;
2553c2e6f3c0SFlorian Wechsung             dofWithoutArtificialCounter++;
25549371c9d4SSatish Balay             if (dofWithoutArtificialCounter == numPatchDofs) break;
2555c2e6f3c0SFlorian Wechsung           }
2556c2e6f3c0SFlorian Wechsung         }
25579566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutArtificialToWithArtificialArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithArtificial[p - pStart]));
25589566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(patch->gtol, &gtolArray));
25599566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial));
2560c2e6f3c0SFlorian Wechsung       }
25610997d788SPatrick Farrell     }
25620997d788SPatrick Farrell     for (p = pStart; p < pEnd; ++p) {
25630904074fSPatrick Farrell       if (isNonlinear) {
25640904074fSPatrick Farrell         const PetscInt *gtolArray, *gtolArrayWithAll = NULL;
25650904074fSPatrick Farrell         PetscInt        numPatchDofs, offset;
25660904074fSPatrick Farrell         PetscInt        numPatchDofsWithAll, offsetWithAll;
25670904074fSPatrick Farrell         PetscInt        dofWithoutAllCounter = 0;
25680904074fSPatrick Farrell         PetscInt       *patchWithoutAllToWithAllArray;
25690904074fSPatrick Farrell 
25700904074fSPatrick Farrell         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
25710904074fSPatrick Farrell         /* the index in the patch with all dofs */
25729566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->gtol, &gtolArray));
25730904074fSPatrick Farrell 
25749566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs));
257547aca4a6SPatrick Farrell         if (numPatchDofs == 0) {
2576b88cb22dSPatrick Farrell           patch->dofMappingWithoutToWithAll[p - pStart] = NULL;
257747aca4a6SPatrick Farrell           continue;
257847aca4a6SPatrick Farrell         }
25790904074fSPatrick Farrell 
25809566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset));
25819566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->gtolWithAll, &gtolArrayWithAll));
25829566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->gtolCountsWithAll, p, &numPatchDofsWithAll));
25839566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offsetWithAll));
25840904074fSPatrick Farrell 
25859566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(numPatchDofs, &patchWithoutAllToWithAllArray));
25860904074fSPatrick Farrell 
25870904074fSPatrick Farrell         for (i = 0; i < numPatchDofsWithAll; i++) {
25880904074fSPatrick Farrell           if (gtolArrayWithAll[i + offsetWithAll] == gtolArray[offset + dofWithoutAllCounter]) {
25890904074fSPatrick Farrell             patchWithoutAllToWithAllArray[dofWithoutAllCounter] = i;
25900904074fSPatrick Farrell             dofWithoutAllCounter++;
25919371c9d4SSatish Balay             if (dofWithoutAllCounter == numPatchDofs) break;
25920904074fSPatrick Farrell           }
25930904074fSPatrick Farrell         }
25949566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutAllToWithAllArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithAll[p - pStart]));
25959566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(patch->gtol, &gtolArray));
25969566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(patch->gtolWithAll, &gtolArrayWithAll));
25970904074fSPatrick Farrell       }
25984bbf5ea8SMatthew G. Knepley     }
259960dd46caSLawrence Mitchell     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
26009566063dSJacob Faibussowitsch       PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDofWithArtificial, &patch->patchRHSWithArtificial));
26019566063dSJacob Faibussowitsch       PetscCall(VecSetUp(patch->patchRHSWithArtificial));
260260dd46caSLawrence Mitchell     }
26039566063dSJacob Faibussowitsch     PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDof, &patch->patchRHS));
26049566063dSJacob Faibussowitsch     PetscCall(VecSetUp(patch->patchRHS));
26059566063dSJacob Faibussowitsch     PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDof, &patch->patchUpdate));
26069566063dSJacob Faibussowitsch     PetscCall(VecSetUp(patch->patchUpdate));
26074bbf5ea8SMatthew G. Knepley     if (patch->save_operators) {
26089566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(patch->npatch, &patch->mat));
260948a46eb9SPierre Jolivet       for (i = 0; i < patch->npatch; ++i) PetscCall(PCPatchCreateMatrix_Private(pc, i, &patch->mat[i], PETSC_FALSE));
26104bbf5ea8SMatthew G. Knepley     }
26119566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(PC_Patch_CreatePatches, pc, 0, 0, 0));
26124bbf5ea8SMatthew G. Knepley 
26134bbf5ea8SMatthew G. Knepley     /* If desired, calculate weights for dof multiplicity */
26144bbf5ea8SMatthew G. Knepley     if (patch->partition_of_unity) {
26153bb0e8f7SKarl Rupp       PetscScalar *input  = NULL;
26163bb0e8f7SKarl Rupp       PetscScalar *output = NULL;
26173bb0e8f7SKarl Rupp       Vec          global;
26183bb0e8f7SKarl Rupp 
26199566063dSJacob Faibussowitsch       PetscCall(VecDuplicate(patch->localRHS, &patch->dof_weights));
262061c4b389SFlorian Wechsung       if (patch->local_composition_type == PC_COMPOSITE_ADDITIVE) {
26214bbf5ea8SMatthew G. Knepley         for (i = 0; i < patch->npatch; ++i) {
26224bbf5ea8SMatthew G. Knepley           PetscInt dof;
26234bbf5ea8SMatthew G. Knepley 
26249566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetDof(patch->gtolCounts, i + pStart, &dof));
26254bbf5ea8SMatthew G. Knepley           if (dof <= 0) continue;
26269566063dSJacob Faibussowitsch           PetscCall(VecSet(patch->patchRHS, 1.0));
26279566063dSJacob Faibussowitsch           PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHS, patch->dof_weights, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR));
26284bbf5ea8SMatthew G. Knepley         }
2629c2e6f3c0SFlorian Wechsung       } else {
2630e047a90bSFlorian Wechsung         /* multiplicative is actually only locally multiplicative and globally additive. need the pou where the mesh decomposition overlaps */
26319566063dSJacob Faibussowitsch         PetscCall(VecSet(patch->dof_weights, 1.0));
26324bbf5ea8SMatthew G. Knepley       }
2633d132cafaSFlorian Wechsung 
26343ba16761SJacob Faibussowitsch       PetscCall(VecDuplicate(patch->dof_weights, &global));
26353ba16761SJacob Faibussowitsch       PetscCall(VecSet(global, 0.));
2636d132cafaSFlorian Wechsung 
26379566063dSJacob Faibussowitsch       PetscCall(VecGetArray(patch->dof_weights, &input));
26389566063dSJacob Faibussowitsch       PetscCall(VecGetArray(global, &output));
26399566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceBegin(patch->sectionSF, MPIU_SCALAR, input, output, MPI_SUM));
26409566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceEnd(patch->sectionSF, MPIU_SCALAR, input, output, MPI_SUM));
26419566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(patch->dof_weights, &input));
26429566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(global, &output));
2643d132cafaSFlorian Wechsung 
26449566063dSJacob Faibussowitsch       PetscCall(VecReciprocal(global));
2645d132cafaSFlorian Wechsung 
26469566063dSJacob Faibussowitsch       PetscCall(VecGetArray(patch->dof_weights, &output));
26479566063dSJacob Faibussowitsch       PetscCall(VecGetArray(global, &input));
26489566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(patch->sectionSF, MPIU_SCALAR, input, output, MPI_REPLACE));
26499566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(patch->sectionSF, MPIU_SCALAR, input, output, MPI_REPLACE));
26509566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(patch->dof_weights, &output));
26519566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(global, &input));
26529566063dSJacob Faibussowitsch       PetscCall(VecDestroy(&global));
26534bbf5ea8SMatthew G. Knepley     }
265448a46eb9SPierre Jolivet     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE && patch->save_operators && !patch->isNonlinear) PetscCall(PetscMalloc1(patch->npatch, &patch->matWithArtificial));
26554bbf5ea8SMatthew G. Knepley   }
26569566063dSJacob Faibussowitsch   PetscCall((*patch->setupsolver)(pc));
26573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
26584bbf5ea8SMatthew G. Knepley }
2659dadc69c5SMatthew G. Knepley 
2660d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_PATCH_Linear(PC pc, PetscInt i, Vec x, Vec y)
2661d71ae5a4SJacob Faibussowitsch {
2662dadc69c5SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
2663c73d2cf6SLawrence Mitchell   KSP       ksp;
26649d4fc724SLawrence Mitchell   Mat       op;
26659d4fc724SLawrence Mitchell   PetscInt  m, n;
2666dadc69c5SMatthew G. Knepley 
2667dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2668c73d2cf6SLawrence Mitchell   if (patch->denseinverse) {
26699566063dSJacob Faibussowitsch     PetscCall((*patch->densesolve)(patch->mat[i], x, y));
26703ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2671c73d2cf6SLawrence Mitchell   }
2672c73d2cf6SLawrence Mitchell   ksp = (KSP)patch->solver[i];
2673dadc69c5SMatthew G. Knepley   if (!patch->save_operators) {
2674dadc69c5SMatthew G. Knepley     Mat mat;
2675dadc69c5SMatthew G. Knepley 
26769566063dSJacob Faibussowitsch     PetscCall(PCPatchCreateMatrix_Private(pc, i, &mat, PETSC_FALSE));
2677dadc69c5SMatthew G. Knepley     /* Populate operator here. */
26789566063dSJacob Faibussowitsch     PetscCall(PCPatchComputeOperator_Internal(pc, NULL, mat, i, PETSC_FALSE));
26799566063dSJacob Faibussowitsch     PetscCall(KSPSetOperators(ksp, mat, mat));
2680dadc69c5SMatthew G. Knepley     /* Drop reference so the KSPSetOperators below will blow it away. */
26819566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&mat));
2682dadc69c5SMatthew G. Knepley   }
26839566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0));
268448a46eb9SPierre Jolivet   if (!ksp->setfromoptionscalled) PetscCall(KSPSetFromOptions(ksp));
26859d4fc724SLawrence Mitchell   /* Disgusting trick to reuse work vectors */
26869566063dSJacob Faibussowitsch   PetscCall(KSPGetOperators(ksp, &op, NULL));
26879566063dSJacob Faibussowitsch   PetscCall(MatGetLocalSize(op, &m, &n));
26889d4fc724SLawrence Mitchell   x->map->n = m;
26899d4fc724SLawrence Mitchell   y->map->n = n;
26909d4fc724SLawrence Mitchell   x->map->N = m;
26919d4fc724SLawrence Mitchell   y->map->N = n;
26929566063dSJacob Faibussowitsch   PetscCall(KSPSolve(ksp, x, y));
26939566063dSJacob Faibussowitsch   PetscCall(KSPCheckSolve(ksp, pc, y));
26949566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0));
2695dadc69c5SMatthew G. Knepley   if (!patch->save_operators) {
2696dadc69c5SMatthew G. Knepley     PC pc;
26979566063dSJacob Faibussowitsch     PetscCall(KSPSetOperators(ksp, NULL, NULL));
26989566063dSJacob Faibussowitsch     PetscCall(KSPGetPC(ksp, &pc));
2699dadc69c5SMatthew G. Knepley     /* Destroy PC context too, otherwise the factored matrix hangs around. */
27009566063dSJacob Faibussowitsch     PetscCall(PCReset(pc));
27014bbf5ea8SMatthew G. Knepley   }
27023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27034bbf5ea8SMatthew G. Knepley }
27044bbf5ea8SMatthew G. Knepley 
2705d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCUpdateMultiplicative_PATCH_Linear(PC pc, PetscInt i, PetscInt pStart)
2706d71ae5a4SJacob Faibussowitsch {
27076c9c532dSPatrick Farrell   PC_PATCH *patch = (PC_PATCH *)pc->data;
27086c9c532dSPatrick Farrell   Mat       multMat;
27099d4fc724SLawrence Mitchell   PetscInt  n, m;
27106c9c532dSPatrick Farrell 
27114d04e9f1SPatrick Farrell   PetscFunctionBegin;
27124d04e9f1SPatrick Farrell 
27136c9c532dSPatrick Farrell   if (patch->save_operators) {
27146c9c532dSPatrick Farrell     multMat = patch->matWithArtificial[i];
27156c9c532dSPatrick Farrell   } else {
27166c9c532dSPatrick Farrell     /*Very inefficient, hopefully we can just assemble the rectangular matrix in the first place.*/
27176c9c532dSPatrick Farrell     Mat      matSquare;
27186c9c532dSPatrick Farrell     PetscInt dof;
27196c9c532dSPatrick Farrell     IS       rowis;
27209566063dSJacob Faibussowitsch     PetscCall(PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE));
27219566063dSJacob Faibussowitsch     PetscCall(PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE));
27229566063dSJacob Faibussowitsch     PetscCall(MatGetSize(matSquare, &dof, NULL));
27239566063dSJacob Faibussowitsch     PetscCall(ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis));
27249566063dSJacob Faibussowitsch     PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &multMat));
27259566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&matSquare));
27269566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&rowis));
27276c9c532dSPatrick Farrell   }
27289d4fc724SLawrence Mitchell   /* Disgusting trick to reuse work vectors */
27299566063dSJacob Faibussowitsch   PetscCall(MatGetLocalSize(multMat, &m, &n));
27309d4fc724SLawrence Mitchell   patch->patchUpdate->map->n            = n;
27319d4fc724SLawrence Mitchell   patch->patchRHSWithArtificial->map->n = m;
27329d4fc724SLawrence Mitchell   patch->patchUpdate->map->N            = n;
27339d4fc724SLawrence Mitchell   patch->patchRHSWithArtificial->map->N = m;
27349566063dSJacob Faibussowitsch   PetscCall(MatMult(multMat, patch->patchUpdate, patch->patchRHSWithArtificial));
27359566063dSJacob Faibussowitsch   PetscCall(VecScale(patch->patchRHSWithArtificial, -1.0));
27369566063dSJacob Faibussowitsch   PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHSWithArtificial, patch->localRHS, ADD_VALUES, SCATTER_REVERSE, SCATTER_WITHARTIFICIAL));
273748a46eb9SPierre Jolivet   if (!patch->save_operators) PetscCall(MatDestroy(&multMat));
27383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27396c9c532dSPatrick Farrell }
27406c9c532dSPatrick Farrell 
2741d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_PATCH(PC pc, Vec x, Vec y)
2742d71ae5a4SJacob Faibussowitsch {
27434bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch        = (PC_PATCH *)pc->data;
27441202d238SPatrick Farrell   const PetscScalar *globalRHS    = NULL;
27451202d238SPatrick Farrell   PetscScalar       *localRHS     = NULL;
27461202d238SPatrick Farrell   PetscScalar       *globalUpdate = NULL;
27474bbf5ea8SMatthew G. Knepley   const PetscInt    *bcNodes      = NULL;
27484bbf5ea8SMatthew G. Knepley   PetscInt           nsweep       = patch->symmetrise_sweep ? 2 : 1;
27494bbf5ea8SMatthew G. Knepley   PetscInt           start[2]     = {0, 0};
27504bbf5ea8SMatthew G. Knepley   PetscInt           end[2]       = {-1, -1};
27514bbf5ea8SMatthew G. Knepley   const PetscInt     inc[2]       = {1, -1};
27521202d238SPatrick Farrell   const PetscScalar *localUpdate;
27534bbf5ea8SMatthew G. Knepley   const PetscInt    *iterationSet;
27544bbf5ea8SMatthew G. Knepley   PetscInt           pStart, numBcs, n, sweep, bc, j;
27554bbf5ea8SMatthew G. Knepley 
27564bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
27579566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(PC_Patch_Apply, pc, 0, 0, 0));
27589566063dSJacob Faibussowitsch   PetscCall(PetscOptionsPushGetViewerOff(PETSC_TRUE));
275992d50984SMatthew G. Knepley   /* start, end, inc have 2 entries to manage a second backward sweep if we symmetrize */
27604bbf5ea8SMatthew G. Knepley   end[0]   = patch->npatch;
27614bbf5ea8SMatthew G. Knepley   start[1] = patch->npatch - 1;
27624bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
27639566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(patch->iterationSet, &end[0]));
27644bbf5ea8SMatthew G. Knepley     start[1] = end[0] - 1;
27659566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->iterationSet, &iterationSet));
27664bbf5ea8SMatthew G. Knepley   }
27674bbf5ea8SMatthew G. Knepley   /* Scatter from global space into overlapped local spaces */
27689566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(x, &globalRHS));
27699566063dSJacob Faibussowitsch   PetscCall(VecGetArray(patch->localRHS, &localRHS));
27709566063dSJacob Faibussowitsch   PetscCall(PetscSFBcastBegin(patch->sectionSF, MPIU_SCALAR, globalRHS, localRHS, MPI_REPLACE));
27719566063dSJacob Faibussowitsch   PetscCall(PetscSFBcastEnd(patch->sectionSF, MPIU_SCALAR, globalRHS, localRHS, MPI_REPLACE));
27729566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(x, &globalRHS));
27739566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(patch->localRHS, &localRHS));
27744bbf5ea8SMatthew G. Knepley 
27759566063dSJacob Faibussowitsch   PetscCall(VecSet(patch->localUpdate, 0.0));
27769566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, NULL));
27779566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0));
27784bbf5ea8SMatthew G. Knepley   for (sweep = 0; sweep < nsweep; sweep++) {
27794bbf5ea8SMatthew G. Knepley     for (j = start[sweep]; j * inc[sweep] < end[sweep] * inc[sweep]; j += inc[sweep]) {
27804bbf5ea8SMatthew G. Knepley       PetscInt i = patch->user_patches ? iterationSet[j] : j;
27814bbf5ea8SMatthew G. Knepley       PetscInt start, len;
27824bbf5ea8SMatthew G. Knepley 
27839566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(patch->gtolCounts, i + pStart, &len));
27849566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(patch->gtolCounts, i + pStart, &start));
27854bbf5ea8SMatthew G. Knepley       /* TODO: Squash out these guys in the setup as well. */
27864bbf5ea8SMatthew G. Knepley       if (len <= 0) continue;
27874bbf5ea8SMatthew G. Knepley       /* TODO: Do we need different scatters for X and Y? */
27889566063dSJacob Faibussowitsch       PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->localRHS, patch->patchRHS, INSERT_VALUES, SCATTER_FORWARD, SCATTER_INTERIOR));
27899566063dSJacob Faibussowitsch       PetscCall((*patch->applysolver)(pc, i, patch->patchRHS, patch->patchUpdate));
27909566063dSJacob Faibussowitsch       PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchUpdate, patch->localUpdate, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR));
279148a46eb9SPierre Jolivet       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) PetscCall((*patch->updatemultiplicative)(pc, i, pStart));
27924bbf5ea8SMatthew G. Knepley     }
27934bbf5ea8SMatthew G. Knepley   }
27949566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0));
27959566063dSJacob Faibussowitsch   if (patch->user_patches) PetscCall(ISRestoreIndices(patch->iterationSet, &iterationSet));
27964bbf5ea8SMatthew G. Knepley   /* XXX: should we do this on the global vector? */
27971baa6e33SBarry Smith   if (patch->partition_of_unity) PetscCall(VecPointwiseMult(patch->localUpdate, patch->localUpdate, patch->dof_weights));
27981202d238SPatrick Farrell   /* Now patch->localUpdate contains the solution of the patch solves, so we need to combine them all. */
27999566063dSJacob Faibussowitsch   PetscCall(VecSet(y, 0.0));
28009566063dSJacob Faibussowitsch   PetscCall(VecGetArray(y, &globalUpdate));
28019566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(patch->localUpdate, &localUpdate));
28029566063dSJacob Faibussowitsch   PetscCall(PetscSFReduceBegin(patch->sectionSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM));
28039566063dSJacob Faibussowitsch   PetscCall(PetscSFReduceEnd(patch->sectionSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM));
28049566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(patch->localUpdate, &localUpdate));
28054bbf5ea8SMatthew G. Knepley 
28064bbf5ea8SMatthew G. Knepley   /* Now we need to send the global BC values through */
28079566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(x, &globalRHS));
28089566063dSJacob Faibussowitsch   PetscCall(ISGetSize(patch->globalBcNodes, &numBcs));
28099566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->globalBcNodes, &bcNodes));
28109566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(x, &n));
28114bbf5ea8SMatthew G. Knepley   for (bc = 0; bc < numBcs; ++bc) {
28124bbf5ea8SMatthew G. Knepley     const PetscInt idx = bcNodes[bc];
28131202d238SPatrick Farrell     if (idx < n) globalUpdate[idx] = globalRHS[idx];
28144bbf5ea8SMatthew G. Knepley   }
28154bbf5ea8SMatthew G. Knepley 
28169566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->globalBcNodes, &bcNodes));
28179566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(x, &globalRHS));
28189566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(y, &globalUpdate));
28194bbf5ea8SMatthew G. Knepley 
28209566063dSJacob Faibussowitsch   PetscCall(PetscOptionsPopGetViewerOff());
28219566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(PC_Patch_Apply, pc, 0, 0, 0));
28223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
28234bbf5ea8SMatthew G. Knepley }
28244bbf5ea8SMatthew G. Knepley 
2825d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCReset_PATCH_Linear(PC pc)
2826d71ae5a4SJacob Faibussowitsch {
2827dadc69c5SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
2828dadc69c5SMatthew G. Knepley   PetscInt  i;
2829dadc69c5SMatthew G. Knepley 
2830dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2831dadc69c5SMatthew G. Knepley   if (patch->solver) {
28329566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(KSPReset((KSP)patch->solver[i]));
2833dadc69c5SMatthew G. Knepley   }
28343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2835dadc69c5SMatthew G. Knepley }
2836dadc69c5SMatthew G. Knepley 
2837d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCReset_PATCH(PC pc)
2838d71ae5a4SJacob Faibussowitsch {
28394bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
28404bbf5ea8SMatthew G. Knepley   PetscInt  i;
28414bbf5ea8SMatthew G. Knepley 
28424bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2843fa84ea4cSLawrence Mitchell 
28449566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&patch->sectionSF));
28459566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->cellCounts));
28469566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->pointCounts));
28479566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->cellNumbering));
28489566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->gtolCounts));
28499566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->gtol));
28509566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->cells));
28519566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->points));
28529566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->dofs));
28539566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->offs));
28549566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->patchSection));
28559566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->ghostBcNodes));
28569566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->globalBcNodes));
28579566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->gtolCountsWithArtificial));
28589566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->gtolWithArtificial));
28599566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->dofsWithArtificial));
28609566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->offsWithArtificial));
28619566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->gtolCountsWithAll));
28629566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->gtolWithAll));
28639566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->dofsWithAll));
28649566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->offsWithAll));
28659566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->cellMats));
28669566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->intFacetMats));
28679566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->allCells));
28689566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->intFacets));
28699566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->extFacets));
28709566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->intFacetsToPatchCell));
28719566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->intFacetCounts));
28729566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->extFacetCounts));
28734bbf5ea8SMatthew G. Knepley 
28749371c9d4SSatish Balay   if (patch->dofSection)
28759371c9d4SSatish Balay     for (i = 0; i < patch->nsubspaces; i++) PetscCall(PetscSectionDestroy(&patch->dofSection[i]));
28769566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->dofSection));
28779566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->bs));
28789566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->nodesPerCell));
28799371c9d4SSatish Balay   if (patch->cellNodeMap)
28809371c9d4SSatish Balay     for (i = 0; i < patch->nsubspaces; i++) PetscCall(PetscFree(patch->cellNodeMap[i]));
28819566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->cellNodeMap));
28829566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->subspaceOffsets));
28834bbf5ea8SMatthew G. Knepley 
28849566063dSJacob Faibussowitsch   PetscCall((*patch->resetsolver)(pc));
28854bbf5ea8SMatthew G. Knepley 
288648a46eb9SPierre Jolivet   if (patch->subspaces_to_exclude) PetscCall(PetscHSetIDestroy(&patch->subspaces_to_exclude));
2887e4c66b91SPatrick Farrell 
28889566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->localRHS));
28899566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->localUpdate));
28909566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->patchRHS));
28919566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->patchUpdate));
28929566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->dof_weights));
28934bbf5ea8SMatthew G. Knepley   if (patch->patch_dof_weights) {
28949566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(VecDestroy(&patch->patch_dof_weights[i]));
28959566063dSJacob Faibussowitsch     PetscCall(PetscFree(patch->patch_dof_weights));
28964bbf5ea8SMatthew G. Knepley   }
28974bbf5ea8SMatthew G. Knepley   if (patch->mat) {
28989566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(MatDestroy(&patch->mat[i]));
28999566063dSJacob Faibussowitsch     PetscCall(PetscFree(patch->mat));
29005f824522SMatthew G. Knepley   }
29010997d788SPatrick Farrell   if (patch->matWithArtificial && !patch->isNonlinear) {
29029566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(MatDestroy(&patch->matWithArtificial[i]));
29039566063dSJacob Faibussowitsch     PetscCall(PetscFree(patch->matWithArtificial));
290411ac8bb0SFlorian Wechsung   }
29059566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->patchRHSWithArtificial));
290696b79ebeSFlorian Wechsung   if (patch->dofMappingWithoutToWithArtificial) {
29079566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->dofMappingWithoutToWithArtificial[i]));
29089566063dSJacob Faibussowitsch     PetscCall(PetscFree(patch->dofMappingWithoutToWithArtificial));
290996b79ebeSFlorian Wechsung   }
29100904074fSPatrick Farrell   if (patch->dofMappingWithoutToWithAll) {
29119566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->dofMappingWithoutToWithAll[i]));
29129566063dSJacob Faibussowitsch     PetscCall(PetscFree(patch->dofMappingWithoutToWithAll));
29130904074fSPatrick Farrell   }
29149566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->sub_mat_type));
29155f824522SMatthew G. Knepley   if (patch->userIS) {
29169566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->userIS[i]));
29179566063dSJacob Faibussowitsch     PetscCall(PetscFree(patch->userIS));
29185f824522SMatthew G. Knepley   }
29199566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->precomputedTensorLocations));
29209566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->precomputedIntFacetTensorLocations));
2921f98464cbSLawrence Mitchell 
29220a545947SLisandro Dalcin   patch->bs          = NULL;
29234bbf5ea8SMatthew G. Knepley   patch->cellNodeMap = NULL;
29247974b488SMatthew G. Knepley   patch->nsubspaces  = 0;
29259566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->iterationSet));
29265f824522SMatthew G. Knepley 
29279566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&patch->viewerSection));
29283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29294bbf5ea8SMatthew G. Knepley }
29304bbf5ea8SMatthew G. Knepley 
2931d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_PATCH_Linear(PC pc)
2932d71ae5a4SJacob Faibussowitsch {
29334bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
29344bbf5ea8SMatthew G. Knepley   PetscInt  i;
29354bbf5ea8SMatthew G. Knepley 
29364bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2937dadc69c5SMatthew G. Knepley   if (patch->solver) {
29389566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(KSPDestroy((KSP *)&patch->solver[i]));
29399566063dSJacob Faibussowitsch     PetscCall(PetscFree(patch->solver));
29404bbf5ea8SMatthew G. Knepley   }
29413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2942dadc69c5SMatthew G. Knepley }
2943dadc69c5SMatthew G. Knepley 
2944d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_PATCH(PC pc)
2945d71ae5a4SJacob Faibussowitsch {
2946dadc69c5SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
2947dadc69c5SMatthew G. Knepley 
2948dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
29499566063dSJacob Faibussowitsch   PetscCall(PCReset_PATCH(pc));
29509566063dSJacob Faibussowitsch   PetscCall((*patch->destroysolver)(pc));
29519566063dSJacob Faibussowitsch   PetscCall(PetscFree(pc->data));
29523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29534bbf5ea8SMatthew G. Knepley }
29544bbf5ea8SMatthew G. Knepley 
2955d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetFromOptions_PATCH(PC pc, PetscOptionItems *PetscOptionsObject)
2956d71ae5a4SJacob Faibussowitsch {
29574bbf5ea8SMatthew G. Knepley   PC_PATCH            *patch                 = (PC_PATCH *)pc->data;
29584bbf5ea8SMatthew G. Knepley   PCPatchConstructType patchConstructionType = PC_PATCH_STAR;
29595f824522SMatthew G. Knepley   char                 sub_mat_type[PETSC_MAX_PATH_LEN];
296010534d48SPatrick Farrell   char                 option[PETSC_MAX_PATH_LEN];
29615f824522SMatthew G. Knepley   const char          *prefix;
29624bbf5ea8SMatthew G. Knepley   PetscBool            flg, dimflg, codimflg;
29635f824522SMatthew G. Knepley   MPI_Comm             comm;
2964a48c39c8SPatrick Farrell   PetscInt            *ifields, nfields, k;
296561c4b389SFlorian Wechsung   PCCompositeType      loctype = PC_COMPOSITE_ADDITIVE;
29664bbf5ea8SMatthew G. Knepley 
29674bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
29689566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)pc, &comm));
29699566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)pc, &prefix));
2970d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "Patch solver options");
297110534d48SPatrick Farrell 
29729566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_save_operators", patch->classname));
29739566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool(option, "Store all patch operators for lifetime of object?", "PCPatchSetSaveOperators", patch->save_operators, &patch->save_operators, &flg));
297410534d48SPatrick Farrell 
29759566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_precompute_element_tensors", patch->classname));
29769566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool(option, "Compute each element tensor only once?", "PCPatchSetPrecomputeElementTensors", patch->precomputeElementTensors, &patch->precomputeElementTensors, &flg));
29779566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_partition_of_unity", patch->classname));
29789566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool(option, "Weight contributions by dof multiplicity?", "PCPatchSetPartitionOfUnity", patch->partition_of_unity, &patch->partition_of_unity, &flg));
297910534d48SPatrick Farrell 
29809566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_local_type", patch->classname));
29819566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum(option, "Type of local solver composition (additive or multiplicative)", "PCPatchSetLocalComposition", PCCompositeTypes, (PetscEnum)loctype, (PetscEnum *)&loctype, &flg));
29829566063dSJacob Faibussowitsch   if (flg) PetscCall(PCPatchSetLocalComposition(pc, loctype));
29839566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_dense_inverse", patch->classname));
29849566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool(option, "Compute inverses of patch matrices and apply directly? Ignores KSP/PC settings on patch.", "PCPatchSetDenseInverse", patch->denseinverse, &patch->denseinverse, &flg));
29859566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_dim", patch->classname));
29869566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt(option, "What dimension of mesh point to construct patches by? (0 = vertices)", "PCPATCH", patch->dim, &patch->dim, &dimflg));
29879566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_codim", patch->classname));
29889566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt(option, "What co-dimension of mesh point to construct patches by? (0 = cells)", "PCPATCH", patch->codim, &patch->codim, &codimflg));
298908401ef6SPierre Jolivet   PetscCheck(!dimflg || !codimflg, comm, PETSC_ERR_ARG_WRONG, "Can only set one of dimension or co-dimension");
299010534d48SPatrick Farrell 
29919566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_type", patch->classname));
29929566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum(option, "How should the patches be constructed?", "PCPatchSetConstructType", PCPatchConstructTypes, (PetscEnum)patchConstructionType, (PetscEnum *)&patchConstructionType, &flg));
29939566063dSJacob Faibussowitsch   if (flg) PetscCall(PCPatchSetConstructType(pc, patchConstructionType, NULL, NULL));
299410534d48SPatrick Farrell 
29959566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_vanka_dim", patch->classname));
29969566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt(option, "Topological dimension of entities for Vanka to ignore", "PCPATCH", patch->vankadim, &patch->vankadim, &flg));
299710534d48SPatrick Farrell 
29989566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_ignore_dim", patch->classname));
29999566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt(option, "Topological dimension of entities for completion to ignore", "PCPATCH", patch->ignoredim, &patch->ignoredim, &flg));
300010534d48SPatrick Farrell 
30019566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_pardecomp_overlap", patch->classname));
30029566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt(option, "What overlap should we use in construct type pardecomp?", "PCPATCH", patch->pardecomp_overlap, &patch->pardecomp_overlap, &flg));
3003b525f888SPatrick Farrell 
30049566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_sub_mat_type", patch->classname));
30059566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList(option, "Matrix type for patch solves", "PCPatchSetSubMatType", MatList, NULL, sub_mat_type, PETSC_MAX_PATH_LEN, &flg));
30069566063dSJacob Faibussowitsch   if (flg) PetscCall(PCPatchSetSubMatType(pc, sub_mat_type));
300710534d48SPatrick Farrell 
30089566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_symmetrise_sweep", patch->classname));
30099566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool(option, "Go start->end, end->start?", "PCPATCH", patch->symmetrise_sweep, &patch->symmetrise_sweep, &flg));
3010e4c66b91SPatrick Farrell 
3011a48c39c8SPatrick Farrell   /* If the user has set the number of subspaces, use that for the buffer size,
3012a48c39c8SPatrick Farrell    otherwise use a large number */
3013a48c39c8SPatrick Farrell   if (patch->nsubspaces <= 0) {
3014a48c39c8SPatrick Farrell     nfields = 128;
3015a48c39c8SPatrick Farrell   } else {
3016a48c39c8SPatrick Farrell     nfields = patch->nsubspaces;
3017a48c39c8SPatrick Farrell   }
30189566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nfields, &ifields));
30199566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exclude_subspaces", patch->classname));
30209566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetIntArray(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, option, ifields, &nfields, &flg));
302108401ef6SPierre 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");
3022e4c66b91SPatrick Farrell   if (flg) {
30239566063dSJacob Faibussowitsch     PetscCall(PetscHSetIClear(patch->subspaces_to_exclude));
302448a46eb9SPierre Jolivet     for (k = 0; k < nfields; k++) PetscCall(PetscHSetIAdd(patch->subspaces_to_exclude, ifields[k]));
3025e4c66b91SPatrick Farrell   }
30269566063dSJacob Faibussowitsch   PetscCall(PetscFree(ifields));
30275f824522SMatthew G. Knepley 
30289566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_patches_view", patch->classname));
30299566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool(option, "Print out information during patch construction", "PCPATCH", patch->viewPatches, &patch->viewPatches, &flg));
30309566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_cells_view", patch->classname));
30319566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerCells, &patch->formatCells, &patch->viewCells));
30329566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_interior_facets_view", patch->classname));
30339566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerIntFacets, &patch->formatIntFacets, &patch->viewIntFacets));
30349566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exterior_facets_view", patch->classname));
30359566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerExtFacets, &patch->formatExtFacets, &patch->viewExtFacets));
30369566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_points_view", patch->classname));
30379566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerPoints, &patch->formatPoints, &patch->viewPoints));
30389566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_section_view", patch->classname));
30399566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerSection, &patch->formatSection, &patch->viewSection));
30409566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_mat_view", patch->classname));
30419566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerMatrix, &patch->formatMatrix, &patch->viewMatrix));
3042d0609cedSBarry Smith   PetscOptionsHeadEnd();
30435f824522SMatthew G. Knepley   patch->optionsSet = PETSC_TRUE;
30443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
30454bbf5ea8SMatthew G. Knepley }
30464bbf5ea8SMatthew G. Knepley 
3047d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUpOnBlocks_PATCH(PC pc)
3048d71ae5a4SJacob Faibussowitsch {
30494bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch = (PC_PATCH *)pc->data;
30504bbf5ea8SMatthew G. Knepley   KSPConvergedReason reason;
30514bbf5ea8SMatthew G. Knepley   PetscInt           i;
30524bbf5ea8SMatthew G. Knepley 
30534bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3054a1eac568SLawrence Mitchell   if (!patch->save_operators) {
3055a1eac568SLawrence Mitchell     /* Can't do this here because the sub KSPs don't have an operator attached yet. */
30563ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3057a1eac568SLawrence Mitchell   }
3058c73d2cf6SLawrence Mitchell   if (patch->denseinverse) {
3059c73d2cf6SLawrence Mitchell     /* No solvers */
30603ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3061c73d2cf6SLawrence Mitchell   }
30624bbf5ea8SMatthew G. Knepley   for (i = 0; i < patch->npatch; ++i) {
306348a46eb9SPierre Jolivet     if (!((KSP)patch->solver[i])->setfromoptionscalled) PetscCall(KSPSetFromOptions((KSP)patch->solver[i]));
30649566063dSJacob Faibussowitsch     PetscCall(KSPSetUp((KSP)patch->solver[i]));
30659566063dSJacob Faibussowitsch     PetscCall(KSPGetConvergedReason((KSP)patch->solver[i], &reason));
3066c0decd05SBarry Smith     if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR;
30674bbf5ea8SMatthew G. Knepley   }
30683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
30694bbf5ea8SMatthew G. Knepley }
30704bbf5ea8SMatthew G. Knepley 
3071d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCView_PATCH(PC pc, PetscViewer viewer)
3072d71ae5a4SJacob Faibussowitsch {
30734bbf5ea8SMatthew G. Knepley   PC_PATCH   *patch = (PC_PATCH *)pc->data;
30744bbf5ea8SMatthew G. Knepley   PetscViewer sviewer;
30754bbf5ea8SMatthew G. Knepley   PetscBool   isascii;
30764bbf5ea8SMatthew G. Knepley   PetscMPIInt rank;
30774bbf5ea8SMatthew G. Knepley 
30784bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
30794bbf5ea8SMatthew G. Knepley   /* TODO Redo tabbing with set tbas in new style */
30809566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
30813ba16761SJacob Faibussowitsch   if (!isascii) PetscFunctionReturn(PETSC_SUCCESS);
30829566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
30839566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPushTab(viewer));
308463a3b9bcSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "Subspace Correction preconditioner with %" PetscInt_FMT " patches\n", patch->npatch));
308561c4b389SFlorian Wechsung   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
30869566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "Schwarz type: multiplicative\n"));
3087e047a90bSFlorian Wechsung   } else {
30889566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "Schwarz type: additive\n"));
3089c2e6f3c0SFlorian Wechsung   }
30909566063dSJacob Faibussowitsch   if (patch->partition_of_unity) PetscCall(PetscViewerASCIIPrintf(viewer, "Weighting by partition of unity\n"));
30919566063dSJacob Faibussowitsch   else PetscCall(PetscViewerASCIIPrintf(viewer, "Not weighting by partition of unity\n"));
30929566063dSJacob Faibussowitsch   if (patch->symmetrise_sweep) PetscCall(PetscViewerASCIIPrintf(viewer, "Symmetrising sweep (start->end, then end->start)\n"));
30939566063dSJacob Faibussowitsch   else PetscCall(PetscViewerASCIIPrintf(viewer, "Not symmetrising sweep\n"));
30949566063dSJacob Faibussowitsch   if (!patch->precomputeElementTensors) PetscCall(PetscViewerASCIIPrintf(viewer, "Not precomputing element tensors (overlapping cells rebuilt in every patch assembly)\n"));
30959566063dSJacob Faibussowitsch   else PetscCall(PetscViewerASCIIPrintf(viewer, "Precomputing element tensors (each cell assembled only once)\n"));
30969566063dSJacob Faibussowitsch   if (!patch->save_operators) PetscCall(PetscViewerASCIIPrintf(viewer, "Not saving patch operators (rebuilt every PCApply)\n"));
30979566063dSJacob Faibussowitsch   else PetscCall(PetscViewerASCIIPrintf(viewer, "Saving patch operators (rebuilt every PCSetUp)\n"));
30989566063dSJacob Faibussowitsch   if (patch->patchconstructop == PCPatchConstruct_Star) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: star\n"));
30999566063dSJacob Faibussowitsch   else if (patch->patchconstructop == PCPatchConstruct_Vanka) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: Vanka\n"));
31009566063dSJacob Faibussowitsch   else if (patch->patchconstructop == PCPatchConstruct_User) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: user-specified\n"));
31019566063dSJacob Faibussowitsch   else PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: unknown\n"));
31025d30859aSPatrick Farrell 
3103c73d2cf6SLawrence Mitchell   if (patch->denseinverse) {
31049566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "Explicitly forming dense inverse and applying patch solver via MatMult.\n"));
3105c73d2cf6SLawrence Mitchell   } else {
31065d30859aSPatrick Farrell     if (patch->isNonlinear) {
31079566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "SNES on patches (all same):\n"));
31085d30859aSPatrick Farrell     } else {
31099566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "KSP on patches (all same):\n"));
31105d30859aSPatrick Farrell     }
3111dadc69c5SMatthew G. Knepley     if (patch->solver) {
31129566063dSJacob Faibussowitsch       PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
3113dd400576SPatrick Sanan       if (rank == 0) {
31149566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPushTab(sviewer));
31159566063dSJacob Faibussowitsch         PetscCall(PetscObjectView(patch->solver[0], sviewer));
31169566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPopTab(sviewer));
31174bbf5ea8SMatthew G. Knepley       }
31189566063dSJacob Faibussowitsch       PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
31194bbf5ea8SMatthew G. Knepley     } else {
31209566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
31219566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "Solver not yet set.\n"));
31229566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
31234bbf5ea8SMatthew G. Knepley     }
3124c73d2cf6SLawrence Mitchell   }
31259566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPopTab(viewer));
31263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31274bbf5ea8SMatthew G. Knepley }
31284bbf5ea8SMatthew G. Knepley 
3129e5893cccSMatthew G. Knepley /*MC
3130f1580f4eSBarry Smith    PCPATCH - A `PC` object that encapsulates flexible definition of blocks for overlapping and non-overlapping
313198ed095eSMatthew G. Knepley    small block additive preconditioners. Block definition is based on topology from
3132f1580f4eSBarry Smith    a `DM` and equation numbering from a `PetscSection`.
3133e5893cccSMatthew G. Knepley 
3134e5893cccSMatthew G. Knepley    Options Database Keys:
3135e5893cccSMatthew G. Knepley + -pc_patch_cells_view   - Views the process local cell numbers for each patch
3136e5893cccSMatthew G. Knepley . -pc_patch_points_view  - Views the process local mesh point numbers for each patch
3137e5893cccSMatthew G. Knepley . -pc_patch_g2l_view     - Views the map between global dofs and patch local dofs for each patch
3138e5893cccSMatthew G. Knepley . -pc_patch_patches_view - Views the global dofs associated with each patch and its boundary
3139e5893cccSMatthew G. Knepley - -pc_patch_sub_mat_view - Views the matrix associated with each patch
3140e5893cccSMatthew G. Knepley 
3141e5893cccSMatthew G. Knepley    Level: intermediate
3142e5893cccSMatthew G. Knepley 
3143f1580f4eSBarry Smith .seealso: `PCType`, `PCCreate()`, `PCSetType()`, `PCASM`, `PCJACOBI`, `PCPBJACOBI`, `PCVPBJACOBI`, `SNESPATCH`
3144e5893cccSMatthew G. Knepley M*/
3145d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PCCreate_Patch(PC pc)
3146d71ae5a4SJacob Faibussowitsch {
31474bbf5ea8SMatthew G. Knepley   PC_PATCH *patch;
31484bbf5ea8SMatthew G. Knepley 
31494bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
31504dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&patch));
31514bbf5ea8SMatthew G. Knepley 
315248a46eb9SPierre Jolivet   if (patch->subspaces_to_exclude) PetscCall(PetscHSetIDestroy(&patch->subspaces_to_exclude));
31539566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&patch->subspaces_to_exclude));
3154e4c66b91SPatrick Farrell 
315510534d48SPatrick Farrell   patch->classname   = "pc";
3156debbdec3SPatrick Farrell   patch->isNonlinear = PETSC_FALSE;
315710534d48SPatrick Farrell 
31584bbf5ea8SMatthew G. Knepley   /* Set some defaults */
31595f824522SMatthew G. Knepley   patch->combined                 = PETSC_FALSE;
31604bbf5ea8SMatthew G. Knepley   patch->save_operators           = PETSC_TRUE;
316161c4b389SFlorian Wechsung   patch->local_composition_type   = PC_COMPOSITE_ADDITIVE;
3162fa84ea4cSLawrence Mitchell   patch->precomputeElementTensors = PETSC_FALSE;
31634bbf5ea8SMatthew G. Knepley   patch->partition_of_unity       = PETSC_FALSE;
31644bbf5ea8SMatthew G. Knepley   patch->codim                    = -1;
31654bbf5ea8SMatthew G. Knepley   patch->dim                      = -1;
31664bbf5ea8SMatthew G. Knepley   patch->vankadim                 = -1;
31675f824522SMatthew G. Knepley   patch->ignoredim                = -1;
3168b525f888SPatrick Farrell   patch->pardecomp_overlap        = 0;
31694bbf5ea8SMatthew G. Knepley   patch->patchconstructop         = PCPatchConstruct_Star;
31704bbf5ea8SMatthew G. Knepley   patch->symmetrise_sweep         = PETSC_FALSE;
31715f824522SMatthew G. Knepley   patch->npatch                   = 0;
31724bbf5ea8SMatthew G. Knepley   patch->userIS                   = NULL;
31735f824522SMatthew G. Knepley   patch->optionsSet               = PETSC_FALSE;
31744bbf5ea8SMatthew G. Knepley   patch->iterationSet             = NULL;
31754bbf5ea8SMatthew G. Knepley   patch->user_patches             = PETSC_FALSE;
31769566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(MATDENSE, (char **)&patch->sub_mat_type));
31775f824522SMatthew G. Knepley   patch->viewPatches                       = PETSC_FALSE;
31785f824522SMatthew G. Knepley   patch->viewCells                         = PETSC_FALSE;
31795f824522SMatthew G. Knepley   patch->viewPoints                        = PETSC_FALSE;
31805f824522SMatthew G. Knepley   patch->viewSection                       = PETSC_FALSE;
31815f824522SMatthew G. Knepley   patch->viewMatrix                        = PETSC_FALSE;
31829d4fc724SLawrence Mitchell   patch->densesolve                        = NULL;
3183dadc69c5SMatthew G. Knepley   patch->setupsolver                       = PCSetUp_PATCH_Linear;
3184dadc69c5SMatthew G. Knepley   patch->applysolver                       = PCApply_PATCH_Linear;
3185dadc69c5SMatthew G. Knepley   patch->resetsolver                       = PCReset_PATCH_Linear;
3186dadc69c5SMatthew G. Knepley   patch->destroysolver                     = PCDestroy_PATCH_Linear;
31876c9c532dSPatrick Farrell   patch->updatemultiplicative              = PCUpdateMultiplicative_PATCH_Linear;
318847aca4a6SPatrick Farrell   patch->dofMappingWithoutToWithArtificial = NULL;
318947aca4a6SPatrick Farrell   patch->dofMappingWithoutToWithAll        = NULL;
31904bbf5ea8SMatthew G. Knepley 
31914bbf5ea8SMatthew G. Knepley   pc->data                 = (void *)patch;
31924bbf5ea8SMatthew G. Knepley   pc->ops->apply           = PCApply_PATCH;
31930a545947SLisandro Dalcin   pc->ops->applytranspose  = NULL; /* PCApplyTranspose_PATCH; */
31944bbf5ea8SMatthew G. Knepley   pc->ops->setup           = PCSetUp_PATCH;
31954bbf5ea8SMatthew G. Knepley   pc->ops->reset           = PCReset_PATCH;
31964bbf5ea8SMatthew G. Knepley   pc->ops->destroy         = PCDestroy_PATCH;
31974bbf5ea8SMatthew G. Knepley   pc->ops->setfromoptions  = PCSetFromOptions_PATCH;
31984bbf5ea8SMatthew G. Knepley   pc->ops->setuponblocks   = PCSetUpOnBlocks_PATCH;
31994bbf5ea8SMatthew G. Knepley   pc->ops->view            = PCView_PATCH;
32000a545947SLisandro Dalcin   pc->ops->applyrichardson = NULL;
32014bbf5ea8SMatthew G. Knepley 
32023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
32034bbf5ea8SMatthew G. Knepley }
3204