xref: /petsc/src/ksp/pc/impls/patch/pcpatch.c (revision eae3dc7d82c4e75c6efc83af6cf84b0783a1b49f)
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;
81*eae3dc7dSJacob Faibussowitsch   const PetscInt *leaves  = NULL;
82*eae3dc7dSJacob 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 
297d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetDenseInverse(PC pc, PetscBool flg)
298d71ae5a4SJacob Faibussowitsch {
299c73d2cf6SLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *)pc->data;
300c73d2cf6SLawrence Mitchell   PetscFunctionBegin;
301c73d2cf6SLawrence Mitchell   patch->denseinverse = flg;
3023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
303c73d2cf6SLawrence Mitchell }
304c73d2cf6SLawrence Mitchell 
305d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetDenseInverse(PC pc, PetscBool *flg)
306d71ae5a4SJacob Faibussowitsch {
307c73d2cf6SLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *)pc->data;
308c73d2cf6SLawrence Mitchell   PetscFunctionBegin;
309c73d2cf6SLawrence Mitchell   *flg = patch->denseinverse;
3103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
311c73d2cf6SLawrence Mitchell }
312c73d2cf6SLawrence Mitchell 
3134bbf5ea8SMatthew G. Knepley /* TODO: Docs */
314d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetIgnoreDim(PC pc, PetscInt dim)
315d71ae5a4SJacob Faibussowitsch {
3165f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
3175f824522SMatthew G. Knepley   PetscFunctionBegin;
3185f824522SMatthew G. Knepley   patch->ignoredim = dim;
3193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3205f824522SMatthew G. Knepley }
3215f824522SMatthew G. Knepley 
3225f824522SMatthew G. Knepley /* TODO: Docs */
323d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetIgnoreDim(PC pc, PetscInt *dim)
324d71ae5a4SJacob Faibussowitsch {
3255f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
3265f824522SMatthew G. Knepley   PetscFunctionBegin;
3275f824522SMatthew G. Knepley   *dim = patch->ignoredim;
3283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3295f824522SMatthew G. Knepley }
3305f824522SMatthew G. Knepley 
3315f824522SMatthew G. Knepley /* TODO: Docs */
332d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetSaveOperators(PC pc, PetscBool flg)
333d71ae5a4SJacob Faibussowitsch {
3344bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
3354bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3364bbf5ea8SMatthew G. Knepley   patch->save_operators = flg;
3373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3384bbf5ea8SMatthew G. Knepley }
3394bbf5ea8SMatthew G. Knepley 
3404bbf5ea8SMatthew G. Knepley /* TODO: Docs */
341d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetSaveOperators(PC pc, PetscBool *flg)
342d71ae5a4SJacob Faibussowitsch {
3434bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
3444bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3454bbf5ea8SMatthew G. Knepley   *flg = patch->save_operators;
3463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3474bbf5ea8SMatthew G. Knepley }
3484bbf5ea8SMatthew G. Knepley 
3494bbf5ea8SMatthew G. Knepley /* TODO: Docs */
350d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetPrecomputeElementTensors(PC pc, PetscBool flg)
351d71ae5a4SJacob Faibussowitsch {
352fa84ea4cSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *)pc->data;
353fa84ea4cSLawrence Mitchell   PetscFunctionBegin;
354fa84ea4cSLawrence Mitchell   patch->precomputeElementTensors = flg;
3553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
356fa84ea4cSLawrence Mitchell }
357fa84ea4cSLawrence Mitchell 
358fa84ea4cSLawrence Mitchell /* TODO: Docs */
359d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetPrecomputeElementTensors(PC pc, PetscBool *flg)
360d71ae5a4SJacob Faibussowitsch {
361fa84ea4cSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *)pc->data;
362fa84ea4cSLawrence Mitchell   PetscFunctionBegin;
363fa84ea4cSLawrence Mitchell   *flg = patch->precomputeElementTensors;
3643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
365fa84ea4cSLawrence Mitchell }
366fa84ea4cSLawrence Mitchell 
367fa84ea4cSLawrence Mitchell /* TODO: Docs */
368d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetPartitionOfUnity(PC pc, PetscBool flg)
369d71ae5a4SJacob Faibussowitsch {
3704bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
3714bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3724bbf5ea8SMatthew G. Knepley   patch->partition_of_unity = flg;
3733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3744bbf5ea8SMatthew G. Knepley }
3754bbf5ea8SMatthew G. Knepley 
3764bbf5ea8SMatthew G. Knepley /* TODO: Docs */
377d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetPartitionOfUnity(PC pc, PetscBool *flg)
378d71ae5a4SJacob Faibussowitsch {
3794bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
3804bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3814bbf5ea8SMatthew G. Knepley   *flg = patch->partition_of_unity;
3823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3834bbf5ea8SMatthew G. Knepley }
3844bbf5ea8SMatthew G. Knepley 
3854bbf5ea8SMatthew G. Knepley /* TODO: Docs */
386d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetLocalComposition(PC pc, PCCompositeType type)
387d71ae5a4SJacob Faibussowitsch {
388c2e6f3c0SFlorian Wechsung   PC_PATCH *patch = (PC_PATCH *)pc->data;
389c2e6f3c0SFlorian Wechsung   PetscFunctionBegin;
3902472a847SBarry 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");
39161c4b389SFlorian Wechsung   patch->local_composition_type = type;
3923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
393c2e6f3c0SFlorian Wechsung }
394c2e6f3c0SFlorian Wechsung 
395c2e6f3c0SFlorian Wechsung /* TODO: Docs */
396d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetLocalComposition(PC pc, PCCompositeType *type)
397d71ae5a4SJacob Faibussowitsch {
398c2e6f3c0SFlorian Wechsung   PC_PATCH *patch = (PC_PATCH *)pc->data;
399c2e6f3c0SFlorian Wechsung   PetscFunctionBegin;
40061c4b389SFlorian Wechsung   *type = patch->local_composition_type;
4013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
402c2e6f3c0SFlorian Wechsung }
403c2e6f3c0SFlorian Wechsung 
404c2e6f3c0SFlorian Wechsung /* TODO: Docs */
405d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetSubMatType(PC pc, MatType sub_mat_type)
406d71ae5a4SJacob Faibussowitsch {
4074bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
4084bbf5ea8SMatthew G. Knepley 
4094bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4109566063dSJacob Faibussowitsch   if (patch->sub_mat_type) PetscCall(PetscFree(patch->sub_mat_type));
4119566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(sub_mat_type, (char **)&patch->sub_mat_type));
4123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4134bbf5ea8SMatthew G. Knepley }
4144bbf5ea8SMatthew G. Knepley 
4154bbf5ea8SMatthew G. Knepley /* TODO: Docs */
416d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetSubMatType(PC pc, MatType *sub_mat_type)
417d71ae5a4SJacob Faibussowitsch {
4184bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
4194bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4204bbf5ea8SMatthew G. Knepley   *sub_mat_type = patch->sub_mat_type;
4213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4224bbf5ea8SMatthew G. Knepley }
4234bbf5ea8SMatthew G. Knepley 
4244bbf5ea8SMatthew G. Knepley /* TODO: Docs */
425d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetCellNumbering(PC pc, PetscSection cellNumbering)
426d71ae5a4SJacob Faibussowitsch {
4274bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
4284bbf5ea8SMatthew G. Knepley 
4294bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4304bbf5ea8SMatthew G. Knepley   patch->cellNumbering = cellNumbering;
4319566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cellNumbering));
4323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4334bbf5ea8SMatthew G. Knepley }
4344bbf5ea8SMatthew G. Knepley 
4354bbf5ea8SMatthew G. Knepley /* TODO: Docs */
436d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetCellNumbering(PC pc, PetscSection *cellNumbering)
437d71ae5a4SJacob Faibussowitsch {
4384bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
4394bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4404bbf5ea8SMatthew G. Knepley   *cellNumbering = patch->cellNumbering;
4413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4424bbf5ea8SMatthew G. Knepley }
4434bbf5ea8SMatthew G. Knepley 
4444bbf5ea8SMatthew G. Knepley /* TODO: Docs */
445d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetConstructType(PC pc, PCPatchConstructType ctype, PetscErrorCode (*func)(PC, PetscInt *, IS **, IS *, void *), void *ctx)
446d71ae5a4SJacob Faibussowitsch {
4474bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
4484bbf5ea8SMatthew G. Knepley 
4494bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4504bbf5ea8SMatthew G. Knepley   patch->ctype = ctype;
4514bbf5ea8SMatthew G. Knepley   switch (ctype) {
4524bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
45340c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
4544bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Star;
4554bbf5ea8SMatthew G. Knepley     break;
4564bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
45740c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
4584bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Vanka;
4594bbf5ea8SMatthew G. Knepley     break;
460e5b9877fSPatrick Farrell   case PC_PATCH_PARDECOMP:
4610a390943SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
462e5b9877fSPatrick Farrell     patch->patchconstructop = PCPatchConstruct_Pardecomp;
4630a390943SPatrick Farrell     break;
4644bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
4654bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
4664bbf5ea8SMatthew G. Knepley     patch->user_patches     = PETSC_TRUE;
4674bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_User;
468bdd9e0cdSPatrick Farrell     if (func) {
4694bbf5ea8SMatthew G. Knepley       patch->userpatchconstructionop = func;
4704bbf5ea8SMatthew G. Knepley       patch->userpatchconstructctx   = ctx;
471bdd9e0cdSPatrick Farrell     }
4724bbf5ea8SMatthew G. Knepley     break;
473d71ae5a4SJacob Faibussowitsch   default:
474d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_USER, "Unknown patch construction type %" PetscInt_FMT, (PetscInt)patch->ctype);
4754bbf5ea8SMatthew G. Knepley   }
4763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4774bbf5ea8SMatthew G. Knepley }
4784bbf5ea8SMatthew G. Knepley 
4794bbf5ea8SMatthew G. Knepley /* TODO: Docs */
480d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchGetConstructType(PC pc, PCPatchConstructType *ctype, PetscErrorCode (**func)(PC, PetscInt *, IS **, IS *, void *), void **ctx)
481d71ae5a4SJacob Faibussowitsch {
4824bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
4834bbf5ea8SMatthew G. Knepley 
4844bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4854bbf5ea8SMatthew G. Knepley   *ctype = patch->ctype;
4864bbf5ea8SMatthew G. Knepley   switch (patch->ctype) {
4874bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
4884bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
489d71ae5a4SJacob Faibussowitsch   case PC_PATCH_PARDECOMP:
490d71ae5a4SJacob Faibussowitsch     break;
4914bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
4924bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
4934bbf5ea8SMatthew G. Knepley     *func = patch->userpatchconstructionop;
4944bbf5ea8SMatthew G. Knepley     *ctx  = patch->userpatchconstructctx;
4954bbf5ea8SMatthew G. Knepley     break;
496d71ae5a4SJacob Faibussowitsch   default:
497d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_USER, "Unknown patch construction type %" PetscInt_FMT, (PetscInt)patch->ctype);
4984bbf5ea8SMatthew G. Knepley   }
4993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5004bbf5ea8SMatthew G. Knepley }
5014bbf5ea8SMatthew G. Knepley 
5024bbf5ea8SMatthew G. Knepley /* TODO: Docs */
503d71ae5a4SJacob 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)
504d71ae5a4SJacob Faibussowitsch {
5054bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
506b6bb21d1SLawrence Mitchell   DM        dm, plex;
5074bbf5ea8SMatthew G. Knepley   PetscSF  *sfs;
5085f824522SMatthew G. Knepley   PetscInt  cStart, cEnd, i, j;
5094bbf5ea8SMatthew G. Knepley 
5104bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
5119566063dSJacob Faibussowitsch   PetscCall(PCGetDM(pc, &dm));
5129566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
513b6bb21d1SLawrence Mitchell   dm = plex;
5149566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
5159566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nsubspaces, &sfs));
5169566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nsubspaces, &patch->dofSection));
5179566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nsubspaces, &patch->bs));
5189566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nsubspaces, &patch->nodesPerCell));
5199566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nsubspaces, &patch->cellNodeMap));
5209566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nsubspaces + 1, &patch->subspaceOffsets));
5214bbf5ea8SMatthew G. Knepley 
5224bbf5ea8SMatthew G. Knepley   patch->nsubspaces       = nsubspaces;
5234bbf5ea8SMatthew G. Knepley   patch->totalDofsPerCell = 0;
5244bbf5ea8SMatthew G. Knepley   for (i = 0; i < nsubspaces; ++i) {
5259566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dms[i], &patch->dofSection[i]));
5269566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)patch->dofSection[i]));
5279566063dSJacob Faibussowitsch     PetscCall(DMGetSectionSF(dms[i], &sfs[i]));
5284bbf5ea8SMatthew G. Knepley     patch->bs[i]           = bs[i];
5294bbf5ea8SMatthew G. Knepley     patch->nodesPerCell[i] = nodesPerCell[i];
5304bbf5ea8SMatthew G. Knepley     patch->totalDofsPerCell += nodesPerCell[i] * bs[i];
5319566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1((cEnd - cStart) * nodesPerCell[i], &patch->cellNodeMap[i]));
53280e8a965SFlorian Wechsung     for (j = 0; j < (cEnd - cStart) * nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
5334bbf5ea8SMatthew G. Knepley     patch->subspaceOffsets[i] = subspaceOffsets[i];
5344bbf5ea8SMatthew G. Knepley   }
5359566063dSJacob Faibussowitsch   PetscCall(PCPatchCreateDefaultSF_Private(pc, nsubspaces, sfs, patch->bs));
5369566063dSJacob Faibussowitsch   PetscCall(PetscFree(sfs));
5374bbf5ea8SMatthew G. Knepley 
5384bbf5ea8SMatthew G. Knepley   patch->subspaceOffsets[nsubspaces] = subspaceOffsets[nsubspaces];
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));
5419566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
5423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5434bbf5ea8SMatthew G. Knepley }
5444bbf5ea8SMatthew G. Knepley 
5454bbf5ea8SMatthew G. Knepley /* TODO: Docs */
546d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetDiscretisationInfoCombined(PC pc, DM dm, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
547d71ae5a4SJacob Faibussowitsch {
5485f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
5495f824522SMatthew G. Knepley   PetscInt  cStart, cEnd, i, j;
5505f824522SMatthew G. Knepley 
5515f824522SMatthew G. Knepley   PetscFunctionBegin;
5525f824522SMatthew G. Knepley   patch->combined = PETSC_TRUE;
5539566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
5549566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &patch->nsubspaces));
5559566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(patch->nsubspaces, &patch->dofSection));
5569566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(patch->nsubspaces, &patch->bs));
5579566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(patch->nsubspaces, &patch->nodesPerCell));
5589566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(patch->nsubspaces, &patch->cellNodeMap));
5599566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(patch->nsubspaces + 1, &patch->subspaceOffsets));
5609566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &patch->dofSection[0]));
5619566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)patch->dofSection[0]));
5629566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(patch->dofSection[0], &patch->subspaceOffsets[patch->nsubspaces]));
5635f824522SMatthew G. Knepley   patch->totalDofsPerCell = 0;
5645f824522SMatthew G. Knepley   for (i = 0; i < patch->nsubspaces; ++i) {
5655f824522SMatthew G. Knepley     patch->bs[i]           = 1;
5665f824522SMatthew G. Knepley     patch->nodesPerCell[i] = nodesPerCell[i];
5675f824522SMatthew G. Knepley     patch->totalDofsPerCell += nodesPerCell[i];
5689566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1((cEnd - cStart) * nodesPerCell[i], &patch->cellNodeMap[i]));
5695f824522SMatthew G. Knepley     for (j = 0; j < (cEnd - cStart) * nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
5705f824522SMatthew G. Knepley   }
5719566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &patch->sectionSF));
5729566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)patch->sectionSF));
5739566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes));
5749566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes));
5753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5765f824522SMatthew G. Knepley }
5775f824522SMatthew G. Knepley 
5785f824522SMatthew G. Knepley /*@C
5795f824522SMatthew G. Knepley 
58092d50984SMatthew G. Knepley   PCPatchSetComputeFunction - Set the callback used to compute patch residuals
58192d50984SMatthew G. Knepley 
582c3339decSBarry Smith   Logically collective
58399b7e5c6SPatrick Farrell 
58492d50984SMatthew G. Knepley   Input Parameters:
58592d50984SMatthew G. Knepley + pc   - The PC
58692d50984SMatthew G. Knepley . func - The callback
58792d50984SMatthew G. Knepley - ctx  - The user context
58892d50984SMatthew G. Knepley 
5897a50e09dSPatrick Farrell   Calling sequence of func:
5907a50e09dSPatrick Farrell $   func (PC pc,PetscInt point,Vec x,Vec f,IS cellIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx)
5917a50e09dSPatrick Farrell 
5927a50e09dSPatrick Farrell +  pc               - The PC
5937a50e09dSPatrick Farrell .  point            - The point
5947a50e09dSPatrick Farrell .  x                - The input solution (not used in linear problems)
5957a50e09dSPatrick Farrell .  f                - The patch residual vector
5967a50e09dSPatrick Farrell .  cellIS           - An array of the cell numbers
5977a50e09dSPatrick Farrell .  n                - The size of dofsArray
5987a50e09dSPatrick Farrell .  dofsArray        - The dofmap for the dofs to be solved for
5997a50e09dSPatrick Farrell .  dofsArrayWithAll - The dofmap for all dofs on the patch
6007a50e09dSPatrick Farrell -  ctx              - The user context
6017a50e09dSPatrick Farrell 
60292d50984SMatthew G. Knepley   Level: advanced
60392d50984SMatthew G. Knepley 
604f1580f4eSBarry Smith   Note:
60526dc5b63SLawrence Mitchell   The entries of F (the output residual vector) have been set to zero before the call.
60692d50984SMatthew G. Knepley 
607db781477SPatrick Sanan .seealso: `PCPatchSetComputeOperator()`, `PCPatchGetComputeOperator()`, `PCPatchSetDiscretisationInfo()`, `PCPatchSetComputeFunctionInteriorFacets()`
60892d50984SMatthew G. Knepley @*/
609d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetComputeFunction(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Vec, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
610d71ae5a4SJacob Faibussowitsch {
61192d50984SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
61292d50984SMatthew G. Knepley 
61392d50984SMatthew G. Knepley   PetscFunctionBegin;
61492d50984SMatthew G. Knepley   patch->usercomputef    = func;
61592d50984SMatthew G. Knepley   patch->usercomputefctx = ctx;
6163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
61792d50984SMatthew G. Knepley }
61892d50984SMatthew G. Knepley 
61992d50984SMatthew G. Knepley /*@C
62092d50984SMatthew G. Knepley 
62159109abcSLawrence Mitchell   PCPatchSetComputeFunctionInteriorFacets - Set the callback used to compute facet integrals for patch residuals
62259109abcSLawrence Mitchell 
623c3339decSBarry Smith   Logically collective
6247a50e09dSPatrick Farrell 
62559109abcSLawrence Mitchell   Input Parameters:
62659109abcSLawrence Mitchell + pc   - The PC
62759109abcSLawrence Mitchell . func - The callback
62859109abcSLawrence Mitchell - ctx  - The user context
62959109abcSLawrence Mitchell 
6307a50e09dSPatrick Farrell   Calling sequence of func:
6317a50e09dSPatrick Farrell $   func (PC pc,PetscInt point,Vec x,Vec f,IS facetIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx)
6327a50e09dSPatrick Farrell 
6337a50e09dSPatrick Farrell +  pc               - The PC
6347a50e09dSPatrick Farrell .  point            - The point
6357a50e09dSPatrick Farrell .  x                - The input solution (not used in linear problems)
6367a50e09dSPatrick Farrell .  f                - The patch residual vector
6377a50e09dSPatrick Farrell .  facetIS          - An array of the facet numbers
6387a50e09dSPatrick Farrell .  n                - The size of dofsArray
6397a50e09dSPatrick Farrell .  dofsArray        - The dofmap for the dofs to be solved for
6407a50e09dSPatrick Farrell .  dofsArrayWithAll - The dofmap for all dofs on the patch
6417a50e09dSPatrick Farrell -  ctx              - The user context
6427a50e09dSPatrick Farrell 
64359109abcSLawrence Mitchell   Level: advanced
64459109abcSLawrence Mitchell 
645f1580f4eSBarry Smith   Note:
64626dc5b63SLawrence Mitchell   The entries of F (the output residual vector) have been set to zero before the call.
64759109abcSLawrence Mitchell 
648db781477SPatrick Sanan .seealso: `PCPatchSetComputeOperator()`, `PCPatchGetComputeOperator()`, `PCPatchSetDiscretisationInfo()`, `PCPatchSetComputeFunction()`
64959109abcSLawrence Mitchell @*/
650d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetComputeFunctionInteriorFacets(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Vec, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
651d71ae5a4SJacob Faibussowitsch {
65259109abcSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *)pc->data;
65359109abcSLawrence Mitchell 
65459109abcSLawrence Mitchell   PetscFunctionBegin;
65559109abcSLawrence Mitchell   patch->usercomputefintfacet    = func;
65659109abcSLawrence Mitchell   patch->usercomputefintfacetctx = ctx;
6573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
65859109abcSLawrence Mitchell }
65959109abcSLawrence Mitchell 
66059109abcSLawrence Mitchell /*@C
66159109abcSLawrence Mitchell 
6625f824522SMatthew G. Knepley   PCPatchSetComputeOperator - Set the callback used to compute patch matrices
6635f824522SMatthew G. Knepley 
664c3339decSBarry Smith   Logically collective
6657a50e09dSPatrick Farrell 
6665f824522SMatthew G. Knepley   Input Parameters:
6675f824522SMatthew G. Knepley + pc   - The PC
6685f824522SMatthew G. Knepley . func - The callback
6695f824522SMatthew G. Knepley - ctx  - The user context
6705f824522SMatthew G. Knepley 
6717a50e09dSPatrick Farrell   Calling sequence of func:
6727a50e09dSPatrick Farrell $   func (PC pc,PetscInt point,Vec x,Mat mat,IS facetIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx)
6737a50e09dSPatrick Farrell 
6747a50e09dSPatrick Farrell +  pc               - The PC
6757a50e09dSPatrick Farrell .  point            - The point
6767a50e09dSPatrick Farrell .  x                - The input solution (not used in linear problems)
6777a50e09dSPatrick Farrell .  mat              - The patch matrix
6787a50e09dSPatrick Farrell .  cellIS           - An array of the cell numbers
6797a50e09dSPatrick Farrell .  n                - The size of dofsArray
6807a50e09dSPatrick Farrell .  dofsArray        - The dofmap for the dofs to be solved for
6817a50e09dSPatrick Farrell .  dofsArrayWithAll - The dofmap for all dofs on the patch
6827a50e09dSPatrick Farrell -  ctx              - The user context
6837a50e09dSPatrick Farrell 
6845f824522SMatthew G. Knepley   Level: advanced
6855f824522SMatthew G. Knepley 
686f1580f4eSBarry Smith   Note:
6877a50e09dSPatrick Farrell   The matrix entries have been set to zero before the call.
6885f824522SMatthew G. Knepley 
689db781477SPatrick Sanan .seealso: `PCPatchGetComputeOperator()`, `PCPatchSetComputeFunction()`, `PCPatchSetDiscretisationInfo()`
6905f824522SMatthew G. Knepley @*/
691d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetComputeOperator(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Mat, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
692d71ae5a4SJacob Faibussowitsch {
6934bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
6944bbf5ea8SMatthew G. Knepley 
6954bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
6964bbf5ea8SMatthew G. Knepley   patch->usercomputeop    = func;
697723f9013SMatthew G. Knepley   patch->usercomputeopctx = ctx;
6983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6994bbf5ea8SMatthew G. Knepley }
7004bbf5ea8SMatthew G. Knepley 
70159109abcSLawrence Mitchell /*@C
70259109abcSLawrence Mitchell 
7037a50e09dSPatrick Farrell   PCPatchSetComputeOperatorInteriorFacets - Set the callback used to compute facet integrals for patch matrices
70459109abcSLawrence Mitchell 
705c3339decSBarry Smith   Logically collective
70699b7e5c6SPatrick Farrell 
70759109abcSLawrence Mitchell   Input Parameters:
70859109abcSLawrence Mitchell + pc   - The PC
70959109abcSLawrence Mitchell . func - The callback
71059109abcSLawrence Mitchell - ctx  - The user context
71159109abcSLawrence Mitchell 
7127a50e09dSPatrick Farrell   Calling sequence of func:
7137a50e09dSPatrick Farrell $   func (PC pc,PetscInt point,Vec x,Mat mat,IS facetIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx)
7147a50e09dSPatrick Farrell 
7157a50e09dSPatrick Farrell +  pc               - The PC
7167a50e09dSPatrick Farrell .  point            - The point
7177a50e09dSPatrick Farrell .  x                - The input solution (not used in linear problems)
7187a50e09dSPatrick Farrell .  mat              - The patch matrix
7197a50e09dSPatrick Farrell .  facetIS          - An array of the facet numbers
7207a50e09dSPatrick Farrell .  n                - The size of dofsArray
7217a50e09dSPatrick Farrell .  dofsArray        - The dofmap for the dofs to be solved for
7227a50e09dSPatrick Farrell .  dofsArrayWithAll - The dofmap for all dofs on the patch
7237a50e09dSPatrick Farrell -  ctx              - The user context
7247a50e09dSPatrick Farrell 
72559109abcSLawrence Mitchell   Level: advanced
72659109abcSLawrence Mitchell 
727f1580f4eSBarry Smith   Note:
7287a50e09dSPatrick Farrell   The matrix entries have been set to zero before the call.
72959109abcSLawrence Mitchell 
730db781477SPatrick Sanan .seealso: `PCPatchGetComputeOperator()`, `PCPatchSetComputeFunction()`, `PCPatchSetDiscretisationInfo()`
73159109abcSLawrence Mitchell @*/
732d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchSetComputeOperatorInteriorFacets(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Mat, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
733d71ae5a4SJacob Faibussowitsch {
73459109abcSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *)pc->data;
73559109abcSLawrence Mitchell 
73659109abcSLawrence Mitchell   PetscFunctionBegin;
73759109abcSLawrence Mitchell   patch->usercomputeopintfacet    = func;
73859109abcSLawrence Mitchell   patch->usercomputeopintfacetctx = ctx;
7393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
74059109abcSLawrence Mitchell }
74159109abcSLawrence Mitchell 
7424bbf5ea8SMatthew G. Knepley /* On entry, ht contains the topological entities whose dofs we are responsible for solving for;
7434bbf5ea8SMatthew G. Knepley    on exit, cht contains all the topological entities we need to compute their residuals.
7444bbf5ea8SMatthew G. Knepley    In full generality this should incorporate knowledge of the sparsity pattern of the matrix;
7454bbf5ea8SMatthew G. Knepley    here we assume a standard FE sparsity pattern.*/
7464bbf5ea8SMatthew G. Knepley /* TODO: Use DMPlexGetAdjacency() */
747d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCompleteCellPatch(PC pc, PetscHSetI ht, PetscHSetI cht)
748d71ae5a4SJacob Faibussowitsch {
749b6bb21d1SLawrence Mitchell   DM            dm, plex;
750bc7fa33aSFlorian Wechsung   PC_PATCH     *patch = (PC_PATCH *)pc->data;
7511b68eb51SMatthew G. Knepley   PetscHashIter hi;
7524bbf5ea8SMatthew G. Knepley   PetscInt      point;
7534bbf5ea8SMatthew G. Knepley   PetscInt     *star = NULL, *closure = NULL;
7544c954380SMatthew G. Knepley   PetscInt      ignoredim, iStart = 0, iEnd = -1, starSize, closureSize, si, ci;
755bc7fa33aSFlorian Wechsung   PetscInt     *fStar = NULL, *fClosure = NULL;
756bc7fa33aSFlorian Wechsung   PetscInt      fBegin, fEnd, fsi, fci, fStarSize, fClosureSize;
7574bbf5ea8SMatthew G. Knepley 
7584bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
7599566063dSJacob Faibussowitsch   PetscCall(PCGetDM(pc, &dm));
7609566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
761b6bb21d1SLawrence Mitchell   dm = plex;
7629566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fBegin, &fEnd));
7639566063dSJacob Faibussowitsch   PetscCall(PCPatchGetIgnoreDim(pc, &ignoredim));
7649566063dSJacob Faibussowitsch   if (ignoredim >= 0) PetscCall(DMPlexGetDepthStratum(dm, ignoredim, &iStart, &iEnd));
7659566063dSJacob Faibussowitsch   PetscCall(PetscHSetIClear(cht));
7661b68eb51SMatthew G. Knepley   PetscHashIterBegin(ht, hi);
7671b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(ht, hi)) {
7681b68eb51SMatthew G. Knepley     PetscHashIterGetKey(ht, hi, point);
7691b68eb51SMatthew G. Knepley     PetscHashIterNext(ht, hi);
7704bbf5ea8SMatthew G. Knepley 
7714bbf5ea8SMatthew G. Knepley     /* Loop over all the cells that this point connects to */
7729566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star));
7735f824522SMatthew G. Knepley     for (si = 0; si < starSize * 2; si += 2) {
7744c954380SMatthew G. Knepley       const PetscInt ownedpoint = star[si];
7755f824522SMatthew G. Knepley       /* TODO Check for point in cht before running through closure again */
7764bbf5ea8SMatthew G. Knepley       /* now loop over all entities in the closure of that cell */
7779566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, ownedpoint, PETSC_TRUE, &closureSize, &closure));
7785f824522SMatthew G. Knepley       for (ci = 0; ci < closureSize * 2; ci += 2) {
7794c954380SMatthew G. Knepley         const PetscInt seenpoint = closure[ci];
7805f824522SMatthew G. Knepley         if (ignoredim >= 0 && seenpoint >= iStart && seenpoint < iEnd) continue;
7819566063dSJacob Faibussowitsch         PetscCall(PetscHSetIAdd(cht, seenpoint));
782bc7fa33aSFlorian Wechsung         /* Facet integrals couple dofs across facets, so in that case for each of
783f1580f4eSBarry Smith           the facets we need to add all dofs on the other side of the facet to
784f1580f4eSBarry Smith           the seen dofs. */
785bc7fa33aSFlorian Wechsung         if (patch->usercomputeopintfacet) {
786bc7fa33aSFlorian Wechsung           if (fBegin <= seenpoint && seenpoint < fEnd) {
7879566063dSJacob Faibussowitsch             PetscCall(DMPlexGetTransitiveClosure(dm, seenpoint, PETSC_FALSE, &fStarSize, &fStar));
788bc7fa33aSFlorian Wechsung             for (fsi = 0; fsi < fStarSize * 2; fsi += 2) {
7899566063dSJacob Faibussowitsch               PetscCall(DMPlexGetTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, &fClosureSize, &fClosure));
79048a46eb9SPierre Jolivet               for (fci = 0; fci < fClosureSize * 2; fci += 2) PetscCall(PetscHSetIAdd(cht, fClosure[fci]));
7919566063dSJacob Faibussowitsch               PetscCall(DMPlexRestoreTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, NULL, &fClosure));
792bc7fa33aSFlorian Wechsung             }
7939566063dSJacob Faibussowitsch             PetscCall(DMPlexRestoreTransitiveClosure(dm, seenpoint, PETSC_FALSE, NULL, &fStar));
794bc7fa33aSFlorian Wechsung           }
795bc7fa33aSFlorian Wechsung         }
7964bbf5ea8SMatthew G. Knepley       }
7979566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, ownedpoint, PETSC_TRUE, NULL, &closure));
7984bbf5ea8SMatthew G. Knepley     }
7999566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, NULL, &star));
8004bbf5ea8SMatthew G. Knepley   }
8019566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
8023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8035f824522SMatthew G. Knepley }
8045f824522SMatthew G. Knepley 
805d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchGetGlobalDofs(PC pc, PetscSection dofSection[], PetscInt f, PetscBool combined, PetscInt p, PetscInt *dof, PetscInt *off)
806d71ae5a4SJacob Faibussowitsch {
8075f824522SMatthew G. Knepley   PetscFunctionBegin;
8085f824522SMatthew G. Knepley   if (combined) {
8095f824522SMatthew G. Knepley     if (f < 0) {
8109566063dSJacob Faibussowitsch       if (dof) PetscCall(PetscSectionGetDof(dofSection[0], p, dof));
8119566063dSJacob Faibussowitsch       if (off) PetscCall(PetscSectionGetOffset(dofSection[0], p, off));
8125f824522SMatthew G. Knepley     } else {
8139566063dSJacob Faibussowitsch       if (dof) PetscCall(PetscSectionGetFieldDof(dofSection[0], p, f, dof));
8149566063dSJacob Faibussowitsch       if (off) PetscCall(PetscSectionGetFieldOffset(dofSection[0], p, f, off));
8155f824522SMatthew G. Knepley     }
8165f824522SMatthew G. Knepley   } else {
8175f824522SMatthew G. Knepley     if (f < 0) {
8185f824522SMatthew G. Knepley       PC_PATCH *patch = (PC_PATCH *)pc->data;
8195f824522SMatthew G. Knepley       PetscInt  fdof, g;
8205f824522SMatthew G. Knepley 
8215f824522SMatthew G. Knepley       if (dof) {
8225f824522SMatthew G. Knepley         *dof = 0;
8235f824522SMatthew G. Knepley         for (g = 0; g < patch->nsubspaces; ++g) {
8249566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetDof(dofSection[g], p, &fdof));
8255f824522SMatthew G. Knepley           *dof += fdof;
8265f824522SMatthew G. Knepley         }
8275f824522SMatthew G. Knepley       }
828624e31c3SLawrence Mitchell       if (off) {
829624e31c3SLawrence Mitchell         *off = 0;
830624e31c3SLawrence Mitchell         for (g = 0; g < patch->nsubspaces; ++g) {
8319566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetOffset(dofSection[g], p, &fdof));
832624e31c3SLawrence Mitchell           *off += fdof;
833624e31c3SLawrence Mitchell         }
834624e31c3SLawrence Mitchell       }
8355f824522SMatthew G. Knepley     } else {
8369566063dSJacob Faibussowitsch       if (dof) PetscCall(PetscSectionGetDof(dofSection[f], p, dof));
8379566063dSJacob Faibussowitsch       if (off) PetscCall(PetscSectionGetOffset(dofSection[f], p, off));
8385f824522SMatthew G. Knepley     }
8395f824522SMatthew G. Knepley   }
8403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8414bbf5ea8SMatthew G. Knepley }
8424bbf5ea8SMatthew G. Knepley 
8434bbf5ea8SMatthew G. Knepley /* Given a hash table with a set of topological entities (pts), compute the degrees of
8444bbf5ea8SMatthew G. Knepley    freedom in global concatenated numbering on those entities.
8454bbf5ea8SMatthew G. Knepley    For Vanka smoothing, this needs to do something special: ignore dofs of the
8464bbf5ea8SMatthew G. Knepley    constraint subspace on entities that aren't the base entity we're building the patch
8474bbf5ea8SMatthew G. Knepley    around. */
848d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchGetPointDofs(PC pc, PetscHSetI pts, PetscHSetI dofs, PetscInt base, PetscHSetI *subspaces_to_exclude)
849d71ae5a4SJacob Faibussowitsch {
8505f824522SMatthew G. Knepley   PC_PATCH     *patch = (PC_PATCH *)pc->data;
8511b68eb51SMatthew G. Knepley   PetscHashIter hi;
8524bbf5ea8SMatthew G. Knepley   PetscInt      ldof, loff;
8534bbf5ea8SMatthew G. Knepley   PetscInt      k, p;
8544bbf5ea8SMatthew G. Knepley 
8554bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
8569566063dSJacob Faibussowitsch   PetscCall(PetscHSetIClear(dofs));
8574bbf5ea8SMatthew G. Knepley   for (k = 0; k < patch->nsubspaces; ++k) {
8584bbf5ea8SMatthew G. Knepley     PetscInt subspaceOffset = patch->subspaceOffsets[k];
8594bbf5ea8SMatthew G. Knepley     PetscInt bs             = patch->bs[k];
8604bbf5ea8SMatthew G. Knepley     PetscInt j, l;
8614bbf5ea8SMatthew G. Knepley 
862e4c66b91SPatrick Farrell     if (subspaces_to_exclude != NULL) {
863e4c66b91SPatrick Farrell       PetscBool should_exclude_k = PETSC_FALSE;
8649566063dSJacob Faibussowitsch       PetscCall(PetscHSetIHas(*subspaces_to_exclude, k, &should_exclude_k));
865e4c66b91SPatrick Farrell       if (should_exclude_k) {
8664bbf5ea8SMatthew G. Knepley         /* only get this subspace dofs at the base entity, not any others */
8679566063dSJacob Faibussowitsch         PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, base, &ldof, &loff));
8684bbf5ea8SMatthew G. Knepley         if (0 == ldof) continue;
8694bbf5ea8SMatthew G. Knepley         for (j = loff; j < ldof + loff; ++j) {
8704bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
8714bbf5ea8SMatthew G. Knepley             PetscInt dof = bs * j + l + subspaceOffset;
8729566063dSJacob Faibussowitsch             PetscCall(PetscHSetIAdd(dofs, dof));
8734bbf5ea8SMatthew G. Knepley           }
8744bbf5ea8SMatthew G. Knepley         }
8754bbf5ea8SMatthew G. Knepley         continue; /* skip the other dofs of this subspace */
8764bbf5ea8SMatthew G. Knepley       }
877e4c66b91SPatrick Farrell     }
8784bbf5ea8SMatthew G. Knepley 
8791b68eb51SMatthew G. Knepley     PetscHashIterBegin(pts, hi);
8801b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(pts, hi)) {
8811b68eb51SMatthew G. Knepley       PetscHashIterGetKey(pts, hi, p);
8821b68eb51SMatthew G. Knepley       PetscHashIterNext(pts, hi);
8839566063dSJacob Faibussowitsch       PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, p, &ldof, &loff));
8844bbf5ea8SMatthew G. Knepley       if (0 == ldof) continue;
8854bbf5ea8SMatthew G. Knepley       for (j = loff; j < ldof + loff; ++j) {
8864bbf5ea8SMatthew G. Knepley         for (l = 0; l < bs; ++l) {
8874bbf5ea8SMatthew G. Knepley           PetscInt dof = bs * j + l + subspaceOffset;
8889566063dSJacob Faibussowitsch           PetscCall(PetscHSetIAdd(dofs, dof));
8894bbf5ea8SMatthew G. Knepley         }
8904bbf5ea8SMatthew G. Knepley       }
8914bbf5ea8SMatthew G. Knepley     }
8924bbf5ea8SMatthew G. Knepley   }
8933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8944bbf5ea8SMatthew G. Knepley }
8954bbf5ea8SMatthew G. Knepley 
8964bbf5ea8SMatthew G. Knepley /* Given two hash tables A and B, compute the keys in B that are not in A, and put them in C */
897d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchComputeSetDifference_Private(PetscHSetI A, PetscHSetI B, PetscHSetI C)
898d71ae5a4SJacob Faibussowitsch {
8991b68eb51SMatthew G. Knepley   PetscHashIter hi;
9001b68eb51SMatthew G. Knepley   PetscInt      key;
9014bbf5ea8SMatthew G. Knepley   PetscBool     flg;
9024bbf5ea8SMatthew G. Knepley 
9034bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
9049566063dSJacob Faibussowitsch   PetscCall(PetscHSetIClear(C));
9051b68eb51SMatthew G. Knepley   PetscHashIterBegin(B, hi);
9061b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(B, hi)) {
9071b68eb51SMatthew G. Knepley     PetscHashIterGetKey(B, hi, key);
9081b68eb51SMatthew G. Knepley     PetscHashIterNext(B, hi);
9099566063dSJacob Faibussowitsch     PetscCall(PetscHSetIHas(A, key, &flg));
9109566063dSJacob Faibussowitsch     if (!flg) PetscCall(PetscHSetIAdd(C, key));
9114bbf5ea8SMatthew G. Knepley   }
9123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9134bbf5ea8SMatthew G. Knepley }
9144bbf5ea8SMatthew G. Knepley 
9154bbf5ea8SMatthew G. Knepley /*
916f1580f4eSBarry Smith   PCPatchCreateCellPatches - create patches.
917f1580f4eSBarry Smith 
918f1580f4eSBarry Smith   Input Parameter:
919f1580f4eSBarry Smith   . dm - The DMPlex object defining the mesh
920f1580f4eSBarry Smith 
921f1580f4eSBarry Smith   Output Parameters:
922f1580f4eSBarry Smith   + cellCounts  - Section with counts of cells around each vertex
923f1580f4eSBarry Smith   . cells       - IS of the cell point indices of cells in each patch
924f1580f4eSBarry Smith   . pointCounts - Section with counts of cells around each vertex
925f1580f4eSBarry Smith   - point       - IS of the cell point indices of cells in each patch
9264bbf5ea8SMatthew G. Knepley  */
927d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCreateCellPatches(PC pc)
928d71ae5a4SJacob Faibussowitsch {
9294bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *)pc->data;
9305f824522SMatthew G. Knepley   DMLabel         ghost = NULL;
9314bbf5ea8SMatthew G. Knepley   DM              dm, plex;
93276ce8f1aSJose E. Roman   PetscHSetI      ht = NULL, cht = NULL;
9330e126c0bSLawrence Mitchell   PetscSection    cellCounts, pointCounts, intFacetCounts, extFacetCounts;
934eb62eeaaSLawrence Mitchell   PetscInt       *cellsArray, *pointsArray, *intFacetsArray, *extFacetsArray, *intFacetsToPatchCell;
9350e126c0bSLawrence Mitchell   PetscInt        numCells, numPoints, numIntFacets, numExtFacets;
9365f824522SMatthew G. Knepley   const PetscInt *leaves;
93733cbca70SPatrick Farrell   PetscInt        nleaves, pStart, pEnd, cStart, cEnd, vStart, vEnd, fStart, fEnd, v;
9385f824522SMatthew G. Knepley   PetscBool       isFiredrake;
9394bbf5ea8SMatthew G. Knepley 
9404bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
9414bbf5ea8SMatthew G. Knepley   /* Used to keep track of the cells in the patch. */
9429566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&ht));
9439566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&cht));
9444bbf5ea8SMatthew G. Knepley 
9459566063dSJacob Faibussowitsch   PetscCall(PCGetDM(pc, &dm));
94628b400f6SJacob Faibussowitsch   PetscCheck(dm, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "DM not yet set on patch PC");
9479566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
948b6bb21d1SLawrence Mitchell   dm = plex;
9499566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
9509566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
9514bbf5ea8SMatthew G. Knepley 
9524bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
9539566063dSJacob Faibussowitsch     PetscCall(patch->userpatchconstructionop(pc, &patch->npatch, &patch->userIS, &patch->iterationSet, patch->userpatchconstructctx));
9549371c9d4SSatish Balay     vStart = 0;
9559371c9d4SSatish Balay     vEnd   = patch->npatch;
956e5b9877fSPatrick Farrell   } else if (patch->ctype == PC_PATCH_PARDECOMP) {
9579371c9d4SSatish Balay     vStart = 0;
9589371c9d4SSatish Balay     vEnd   = 1;
9595f824522SMatthew G. Knepley   } else if (patch->codim < 0) {
9609566063dSJacob Faibussowitsch     if (patch->dim < 0) PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
9619566063dSJacob Faibussowitsch     else PetscCall(DMPlexGetDepthStratum(dm, patch->dim, &vStart, &vEnd));
9629566063dSJacob Faibussowitsch   } else PetscCall(DMPlexGetHeightStratum(dm, patch->codim, &vStart, &vEnd));
9635f824522SMatthew G. Knepley   patch->npatch = vEnd - vStart;
9644bbf5ea8SMatthew G. Knepley 
9654bbf5ea8SMatthew G. Knepley   /* These labels mark the owned points.  We only create patches around points that this process owns. */
9669566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, "pyop2_ghost", &isFiredrake));
9675f824522SMatthew G. Knepley   if (isFiredrake) {
9689566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, "pyop2_ghost", &ghost));
9699566063dSJacob Faibussowitsch     PetscCall(DMLabelCreateIndex(ghost, pStart, pEnd));
9705f824522SMatthew G. Knepley   } else {
9715f824522SMatthew G. Knepley     PetscSF sf;
9725f824522SMatthew G. Knepley 
9739566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sf));
9749566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL));
9755f824522SMatthew G. Knepley     nleaves = PetscMax(nleaves, 0);
9765f824522SMatthew G. Knepley   }
9774bbf5ea8SMatthew G. Knepley 
9789566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->cellCounts));
9799566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->cellCounts, "Patch Cell Layout"));
9804bbf5ea8SMatthew G. Knepley   cellCounts = patch->cellCounts;
9819566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(cellCounts, vStart, vEnd));
9829566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->pointCounts));
9839566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->pointCounts, "Patch Point Layout"));
9845f824522SMatthew G. Knepley   pointCounts = patch->pointCounts;
9859566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(pointCounts, vStart, vEnd));
9869566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->extFacetCounts));
9879566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->extFacetCounts, "Patch Exterior Facet Layout"));
9880e126c0bSLawrence Mitchell   extFacetCounts = patch->extFacetCounts;
9899566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(extFacetCounts, vStart, vEnd));
9909566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->intFacetCounts));
9919566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->intFacetCounts, "Patch Interior Facet Layout"));
9920e126c0bSLawrence Mitchell   intFacetCounts = patch->intFacetCounts;
9939566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(intFacetCounts, vStart, vEnd));
9945f824522SMatthew G. Knepley   /* Count cells and points in the patch surrounding each entity */
9959566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
9964bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
9971b68eb51SMatthew G. Knepley     PetscHashIter hi;
9985f824522SMatthew G. Knepley     PetscInt      chtSize, loc = -1;
9995f824522SMatthew G. Knepley     PetscBool     flg;
10004bbf5ea8SMatthew G. Knepley 
1001b525f888SPatrick Farrell     if (!patch->user_patches && patch->ctype != PC_PATCH_PARDECOMP) {
10029566063dSJacob Faibussowitsch       if (ghost) PetscCall(DMLabelHasPoint(ghost, v, &flg));
10039371c9d4SSatish Balay       else {
10049371c9d4SSatish Balay         PetscCall(PetscFindInt(v, nleaves, leaves, &loc));
10059371c9d4SSatish Balay         flg = loc >= 0 ? PETSC_TRUE : PETSC_FALSE;
10069371c9d4SSatish Balay       }
10074bbf5ea8SMatthew G. Knepley       /* Not an owned entity, don't make a cell patch. */
10084bbf5ea8SMatthew G. Knepley       if (flg) continue;
10094bbf5ea8SMatthew G. Knepley     }
10104bbf5ea8SMatthew G. Knepley 
10119566063dSJacob Faibussowitsch     PetscCall(patch->patchconstructop((void *)patch, dm, v, ht));
10129566063dSJacob Faibussowitsch     PetscCall(PCPatchCompleteCellPatch(pc, ht, cht));
10139566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(cht, &chtSize));
10144bbf5ea8SMatthew G. Knepley     /* empty patch, continue */
10154bbf5ea8SMatthew G. Knepley     if (chtSize == 0) continue;
10164bbf5ea8SMatthew G. Knepley 
10174bbf5ea8SMatthew G. Knepley     /* safe because size(cht) > 0 from above */
10181b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
10191b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
10205f824522SMatthew G. Knepley       PetscInt point, pdof;
10214bbf5ea8SMatthew G. Knepley 
10221b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
10230e126c0bSLawrence Mitchell       if (fStart <= point && point < fEnd) {
10240e126c0bSLawrence Mitchell         const PetscInt *support;
10250e126c0bSLawrence Mitchell         PetscInt        supportSize, p;
10260e126c0bSLawrence Mitchell         PetscBool       interior = PETSC_TRUE;
10279566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
10289566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
10290e126c0bSLawrence Mitchell         if (supportSize == 1) {
10300e126c0bSLawrence Mitchell           interior = PETSC_FALSE;
10310e126c0bSLawrence Mitchell         } else {
10320e126c0bSLawrence Mitchell           for (p = 0; p < supportSize; p++) {
10330e126c0bSLawrence Mitchell             PetscBool found;
10340e126c0bSLawrence Mitchell             /* FIXME: can I do this while iterating over cht? */
10359566063dSJacob Faibussowitsch             PetscCall(PetscHSetIHas(cht, support[p], &found));
10360e126c0bSLawrence Mitchell             if (!found) {
10370e126c0bSLawrence Mitchell               interior = PETSC_FALSE;
10380e126c0bSLawrence Mitchell               break;
10390e126c0bSLawrence Mitchell             }
10400e126c0bSLawrence Mitchell           }
10410e126c0bSLawrence Mitchell         }
10420e126c0bSLawrence Mitchell         if (interior) {
10439566063dSJacob Faibussowitsch           PetscCall(PetscSectionAddDof(intFacetCounts, v, 1));
10440e126c0bSLawrence Mitchell         } else {
10459566063dSJacob Faibussowitsch           PetscCall(PetscSectionAddDof(extFacetCounts, v, 1));
10460e126c0bSLawrence Mitchell         }
10470e126c0bSLawrence Mitchell       }
10489566063dSJacob Faibussowitsch       PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL));
10499566063dSJacob Faibussowitsch       if (pdof) PetscCall(PetscSectionAddDof(pointCounts, v, 1));
10509566063dSJacob Faibussowitsch       if (point >= cStart && point < cEnd) PetscCall(PetscSectionAddDof(cellCounts, v, 1));
10511b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
10524bbf5ea8SMatthew G. Knepley     }
10534bbf5ea8SMatthew G. Knepley   }
10549566063dSJacob Faibussowitsch   if (isFiredrake) PetscCall(DMLabelDestroyIndex(ghost));
10554bbf5ea8SMatthew G. Knepley 
10569566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(cellCounts));
10579566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(cellCounts, &numCells));
10589566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numCells, &cellsArray));
10599566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(pointCounts));
10609566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(pointCounts, &numPoints));
10619566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numPoints, &pointsArray));
10624bbf5ea8SMatthew G. Knepley 
10639566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(intFacetCounts));
10649566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(extFacetCounts));
10659566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(intFacetCounts, &numIntFacets));
10669566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(extFacetCounts, &numExtFacets));
10679566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numIntFacets, &intFacetsArray));
10689566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numIntFacets * 2, &intFacetsToPatchCell));
10699566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numExtFacets, &extFacetsArray));
10700e126c0bSLawrence Mitchell 
10714bbf5ea8SMatthew G. Knepley   /* Now that we know how much space we need, run through again and actually remember the cells. */
10724bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; v++) {
10731b68eb51SMatthew G. Knepley     PetscHashIter hi;
10740e126c0bSLawrence Mitchell     PetscInt      dof, off, cdof, coff, efdof, efoff, ifdof, ifoff, pdof, n = 0, cn = 0, ifn = 0, efn = 0;
10754bbf5ea8SMatthew G. Knepley 
10769566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(pointCounts, v, &dof));
10779566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(pointCounts, v, &off));
10789566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(cellCounts, v, &cdof));
10799566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(cellCounts, v, &coff));
10809566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(intFacetCounts, v, &ifdof));
10819566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(intFacetCounts, v, &ifoff));
10829566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(extFacetCounts, v, &efdof));
10839566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(extFacetCounts, v, &efoff));
10845f824522SMatthew G. Knepley     if (dof <= 0) continue;
10859566063dSJacob Faibussowitsch     PetscCall(patch->patchconstructop((void *)patch, dm, v, ht));
10869566063dSJacob Faibussowitsch     PetscCall(PCPatchCompleteCellPatch(pc, ht, cht));
10871b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
10881b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
10894bbf5ea8SMatthew G. Knepley       PetscInt point;
10904bbf5ea8SMatthew G. Knepley 
10911b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
10920e126c0bSLawrence Mitchell       if (fStart <= point && point < fEnd) {
10930e126c0bSLawrence Mitchell         const PetscInt *support;
10940e126c0bSLawrence Mitchell         PetscInt        supportSize, p;
10950e126c0bSLawrence Mitchell         PetscBool       interior = PETSC_TRUE;
10969566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
10979566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
10980e126c0bSLawrence Mitchell         if (supportSize == 1) {
10990e126c0bSLawrence Mitchell           interior = PETSC_FALSE;
11000e126c0bSLawrence Mitchell         } else {
11010e126c0bSLawrence Mitchell           for (p = 0; p < supportSize; p++) {
11020e126c0bSLawrence Mitchell             PetscBool found;
11030e126c0bSLawrence Mitchell             /* FIXME: can I do this while iterating over cht? */
11049566063dSJacob Faibussowitsch             PetscCall(PetscHSetIHas(cht, support[p], &found));
11050e126c0bSLawrence Mitchell             if (!found) {
11060e126c0bSLawrence Mitchell               interior = PETSC_FALSE;
11070e126c0bSLawrence Mitchell               break;
11080e126c0bSLawrence Mitchell             }
11090e126c0bSLawrence Mitchell           }
11100e126c0bSLawrence Mitchell         }
11110e126c0bSLawrence Mitchell         if (interior) {
111244b625f7SLawrence Mitchell           intFacetsToPatchCell[2 * (ifoff + ifn)]     = support[0];
111344b625f7SLawrence Mitchell           intFacetsToPatchCell[2 * (ifoff + ifn) + 1] = support[1];
11140e126c0bSLawrence Mitchell           intFacetsArray[ifoff + ifn++]               = point;
11150e126c0bSLawrence Mitchell         } else {
11160e126c0bSLawrence Mitchell           extFacetsArray[efoff + efn++] = point;
11170e126c0bSLawrence Mitchell         }
11180e126c0bSLawrence Mitchell       }
11199566063dSJacob Faibussowitsch       PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL));
1120ad540459SPierre Jolivet       if (pdof) pointsArray[off + n++] = point;
1121ad540459SPierre Jolivet       if (point >= cStart && point < cEnd) cellsArray[coff + cn++] = point;
11221b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
11234bbf5ea8SMatthew G. Knepley     }
112463a3b9bcSJacob 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);
112563a3b9bcSJacob 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);
112663a3b9bcSJacob 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);
112763a3b9bcSJacob 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);
1128eb62eeaaSLawrence Mitchell 
1129eb62eeaaSLawrence Mitchell     for (ifn = 0; ifn < ifdof; ifn++) {
113044b625f7SLawrence Mitchell       PetscInt  cell0  = intFacetsToPatchCell[2 * (ifoff + ifn)];
113144b625f7SLawrence Mitchell       PetscInt  cell1  = intFacetsToPatchCell[2 * (ifoff + ifn) + 1];
1132eb62eeaaSLawrence Mitchell       PetscBool found0 = PETSC_FALSE, found1 = PETSC_FALSE;
1133eb62eeaaSLawrence Mitchell       for (n = 0; n < cdof; n++) {
11347c54fef0SLawrence Mitchell         if (!found0 && cell0 == cellsArray[coff + n]) {
1135c3faab33SLawrence Mitchell           intFacetsToPatchCell[2 * (ifoff + ifn)] = n;
1136eb62eeaaSLawrence Mitchell           found0                                  = PETSC_TRUE;
1137eb62eeaaSLawrence Mitchell         }
11387c54fef0SLawrence Mitchell         if (!found1 && cell1 == cellsArray[coff + n]) {
1139c3faab33SLawrence Mitchell           intFacetsToPatchCell[2 * (ifoff + ifn) + 1] = n;
114080fc4459SLawrence Mitchell           found1                                      = PETSC_TRUE;
1141eb62eeaaSLawrence Mitchell         }
1142eb62eeaaSLawrence Mitchell         if (found0 && found1) break;
1143eb62eeaaSLawrence Mitchell       }
11447827d75bSBarry Smith       PetscCheck(found0 && found1, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Didn't manage to find local point numbers for facet support");
1145eb62eeaaSLawrence Mitchell     }
11464bbf5ea8SMatthew G. Knepley   }
11479566063dSJacob Faibussowitsch   PetscCall(PetscHSetIDestroy(&ht));
11489566063dSJacob Faibussowitsch   PetscCall(PetscHSetIDestroy(&cht));
11495f824522SMatthew G. Knepley 
11509566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numCells, cellsArray, PETSC_OWN_POINTER, &patch->cells));
11519566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->cells, "Patch Cells"));
11525f824522SMatthew G. Knepley   if (patch->viewCells) {
11539566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->cellCounts, patch->viewerCells, patch->formatCells));
11549566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->cells, patch->viewerCells, patch->formatCells));
11555f824522SMatthew G. Knepley   }
11569566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray, PETSC_OWN_POINTER, &patch->intFacets));
11579566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->intFacets, "Patch Interior Facets"));
11589566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 2 * numIntFacets, intFacetsToPatchCell, PETSC_OWN_POINTER, &patch->intFacetsToPatchCell));
11599566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->intFacetsToPatchCell, "Patch Interior Facets local support"));
11600e126c0bSLawrence Mitchell   if (patch->viewIntFacets) {
11619566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->intFacetCounts, patch->viewerIntFacets, patch->formatIntFacets));
11629566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->intFacets, patch->viewerIntFacets, patch->formatIntFacets));
11639566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->intFacetsToPatchCell, patch->viewerIntFacets, patch->formatIntFacets));
11640e126c0bSLawrence Mitchell   }
11659566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numExtFacets, extFacetsArray, PETSC_OWN_POINTER, &patch->extFacets));
11669566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->extFacets, "Patch Exterior Facets"));
11670e126c0bSLawrence Mitchell   if (patch->viewExtFacets) {
11689566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->extFacetCounts, patch->viewerExtFacets, patch->formatExtFacets));
11699566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->extFacets, patch->viewerExtFacets, patch->formatExtFacets));
11700e126c0bSLawrence Mitchell   }
11719566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints, pointsArray, PETSC_OWN_POINTER, &patch->points));
11729566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->points, "Patch Points"));
11735f824522SMatthew G. Knepley   if (patch->viewPoints) {
11749566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->pointCounts, patch->viewerPoints, patch->formatPoints));
11759566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)patch->points, patch->viewerPoints, patch->formatPoints));
11765f824522SMatthew G. Knepley   }
11779566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
11783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11794bbf5ea8SMatthew G. Knepley }
11804bbf5ea8SMatthew G. Knepley 
11814bbf5ea8SMatthew G. Knepley /*
1182f1580f4eSBarry Smith   PCPatchCreateCellPatchDiscretisationInfo - Build the dof maps for cell patches
1183f1580f4eSBarry Smith 
1184f1580f4eSBarry Smith   Input Parameters:
1185f1580f4eSBarry Smith   + dm - The DMPlex object defining the mesh
1186f1580f4eSBarry Smith   . cellCounts - Section with counts of cells around each vertex
1187f1580f4eSBarry Smith   . cells - IS of the cell point indices of cells in each patch
1188f1580f4eSBarry Smith   . cellNumbering - Section mapping plex cell points to Firedrake cell indices.
1189f1580f4eSBarry Smith   . nodesPerCell - number of nodes per cell.
1190f1580f4eSBarry Smith   - cellNodeMap - map from cells to node indices (nodesPerCell * numCells)
1191f1580f4eSBarry Smith 
1192f1580f4eSBarry Smith   Output Parameters:
1193f1580f4eSBarry Smith   + dofs - IS of local dof numbers of each cell in the patch, where local is a patch local numbering
1194f1580f4eSBarry Smith   . gtolCounts - Section with counts of dofs per cell patch
1195f1580f4eSBarry Smith   - gtol - IS mapping from global dofs to local dofs for each patch.
11964bbf5ea8SMatthew G. Knepley  */
1197d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCreateCellPatchDiscretisationInfo(PC pc)
1198d71ae5a4SJacob Faibussowitsch {
11994bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch       = (PC_PATCH *)pc->data;
12004bbf5ea8SMatthew G. Knepley   PetscSection    cellCounts  = patch->cellCounts;
12015f824522SMatthew G. Knepley   PetscSection    pointCounts = patch->pointCounts;
12020904074fSPatrick Farrell   PetscSection    gtolCounts, gtolCountsWithArtificial = NULL, gtolCountsWithAll = NULL;
12034bbf5ea8SMatthew G. Knepley   IS              cells         = patch->cells;
12045f824522SMatthew G. Knepley   IS              points        = patch->points;
12054bbf5ea8SMatthew G. Knepley   PetscSection    cellNumbering = patch->cellNumbering;
12065f824522SMatthew G. Knepley   PetscInt        Nf            = patch->nsubspaces;
12075f824522SMatthew G. Knepley   PetscInt        numCells, numPoints;
12084bbf5ea8SMatthew G. Knepley   PetscInt        numDofs;
12090904074fSPatrick Farrell   PetscInt        numGlobalDofs, numGlobalDofsWithArtificial, numGlobalDofsWithAll;
12104bbf5ea8SMatthew G. Knepley   PetscInt        totalDofsPerCell = patch->totalDofsPerCell;
12114bbf5ea8SMatthew G. Knepley   PetscInt        vStart, vEnd, v;
12125f824522SMatthew G. Knepley   const PetscInt *cellsArray, *pointsArray;
12134bbf5ea8SMatthew G. Knepley   PetscInt       *newCellsArray                 = NULL;
12144bbf5ea8SMatthew G. Knepley   PetscInt       *dofsArray                     = NULL;
1215c2e6f3c0SFlorian Wechsung   PetscInt       *dofsArrayWithArtificial       = NULL;
12160904074fSPatrick Farrell   PetscInt       *dofsArrayWithAll              = NULL;
12175f824522SMatthew G. Knepley   PetscInt       *offsArray                     = NULL;
1218c2e6f3c0SFlorian Wechsung   PetscInt       *offsArrayWithArtificial       = NULL;
12190904074fSPatrick Farrell   PetscInt       *offsArrayWithAll              = NULL;
12204bbf5ea8SMatthew G. Knepley   PetscInt       *asmArray                      = NULL;
1221c2e6f3c0SFlorian Wechsung   PetscInt       *asmArrayWithArtificial        = NULL;
12220904074fSPatrick Farrell   PetscInt       *asmArrayWithAll               = NULL;
12234bbf5ea8SMatthew G. Knepley   PetscInt       *globalDofsArray               = NULL;
1224c2e6f3c0SFlorian Wechsung   PetscInt       *globalDofsArrayWithArtificial = NULL;
12250904074fSPatrick Farrell   PetscInt       *globalDofsArrayWithAll        = NULL;
12264bbf5ea8SMatthew G. Knepley   PetscInt        globalIndex                   = 0;
12274bbf5ea8SMatthew G. Knepley   PetscInt        key                           = 0;
12284bbf5ea8SMatthew G. Knepley   PetscInt        asmKey                        = 0;
1229b6bb21d1SLawrence Mitchell   DM              dm                            = NULL, plex;
1230557beb66SLawrence Mitchell   const PetscInt *bcNodes                       = NULL;
12311b68eb51SMatthew G. Knepley   PetscHMapI      ht;
1232c2e6f3c0SFlorian Wechsung   PetscHMapI      htWithArtificial;
12330904074fSPatrick Farrell   PetscHMapI      htWithAll;
12341b68eb51SMatthew G. Knepley   PetscHSetI      globalBcs;
1235557beb66SLawrence Mitchell   PetscInt        numBcs;
12361b68eb51SMatthew G. Knepley   PetscHSetI      ownedpts, seenpts, owneddofs, seendofs, artificialbcs;
1237cda239d9SMatthew G. Knepley   PetscInt        pStart, pEnd, p, i;
123810534d48SPatrick Farrell   char            option[PETSC_MAX_PATH_LEN];
123939fd2e8aSPatrick Farrell   PetscBool       isNonlinear;
12404bbf5ea8SMatthew G. Knepley 
12414bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1242557beb66SLawrence Mitchell 
12439566063dSJacob Faibussowitsch   PetscCall(PCGetDM(pc, &dm));
12449566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
1245b6bb21d1SLawrence Mitchell   dm = plex;
12464bbf5ea8SMatthew G. Knepley   /* dofcounts section is cellcounts section * dofPerCell */
12479566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(cellCounts, &numCells));
12489566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(patch->pointCounts, &numPoints));
12494bbf5ea8SMatthew G. Knepley   numDofs = numCells * totalDofsPerCell;
12509566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numDofs, &dofsArray));
12519566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numPoints * Nf, &offsArray));
12529566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numDofs, &asmArray));
12539566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numCells, &newCellsArray));
12549566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(cellCounts, &vStart, &vEnd));
12559566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCounts));
12564bbf5ea8SMatthew G. Knepley   gtolCounts = patch->gtolCounts;
12579566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(gtolCounts, vStart, vEnd));
12589566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->gtolCounts, "Patch Global Index Section"));
12594bbf5ea8SMatthew G. Knepley 
1260b6bb21d1SLawrence Mitchell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
12619566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numPoints * Nf, &offsArrayWithArtificial));
12629566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numDofs, &asmArrayWithArtificial));
12639566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numDofs, &dofsArrayWithArtificial));
12649566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithArtificial));
1265c2e6f3c0SFlorian Wechsung     gtolCountsWithArtificial = patch->gtolCountsWithArtificial;
12669566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(gtolCountsWithArtificial, vStart, vEnd));
12679566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)patch->gtolCountsWithArtificial, "Patch Global Index Section Including Artificial BCs"));
1268c2e6f3c0SFlorian Wechsung   }
1269c2e6f3c0SFlorian Wechsung 
12700904074fSPatrick Farrell   isNonlinear = patch->isNonlinear;
1271b6bb21d1SLawrence Mitchell   if (isNonlinear) {
12729566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numPoints * Nf, &offsArrayWithAll));
12739566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numDofs, &asmArrayWithAll));
12749566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numDofs, &dofsArrayWithAll));
12759566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithAll));
12760904074fSPatrick Farrell     gtolCountsWithAll = patch->gtolCountsWithAll;
12779566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(gtolCountsWithAll, vStart, vEnd));
12789566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)patch->gtolCountsWithAll, "Patch Global Index Section Including All BCs"));
12790904074fSPatrick Farrell   }
12800904074fSPatrick Farrell 
1281557beb66SLawrence Mitchell   /* Outside the patch loop, get the dofs that are globally-enforced Dirichlet
1282557beb66SLawrence Mitchell    conditions */
12839566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&globalBcs));
12849566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->ghostBcNodes, &bcNodes));
12859566063dSJacob Faibussowitsch   PetscCall(ISGetSize(patch->ghostBcNodes, &numBcs));
12869371c9d4SSatish Balay   for (i = 0; i < numBcs; ++i) { PetscCall(PetscHSetIAdd(globalBcs, bcNodes[i])); /* these are already in concatenated numbering */ }
12879566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->ghostBcNodes, &bcNodes));
12889566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->ghostBcNodes)); /* memory optimisation */
1289557beb66SLawrence Mitchell 
1290557beb66SLawrence Mitchell   /* Hash tables for artificial BC construction */
12919566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&ownedpts));
12929566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&seenpts));
12939566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&owneddofs));
12949566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&seendofs));
12959566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&artificialbcs));
1296557beb66SLawrence Mitchell 
12979566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(cells, &cellsArray));
12989566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(points, &pointsArray));
12999566063dSJacob Faibussowitsch   PetscCall(PetscHMapICreate(&ht));
13009566063dSJacob Faibussowitsch   PetscCall(PetscHMapICreate(&htWithArtificial));
13019566063dSJacob Faibussowitsch   PetscCall(PetscHMapICreate(&htWithAll));
13024bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
13034bbf5ea8SMatthew G. Knepley     PetscInt localIndex               = 0;
1304c2e6f3c0SFlorian Wechsung     PetscInt localIndexWithArtificial = 0;
13050904074fSPatrick Farrell     PetscInt localIndexWithAll        = 0;
13064bbf5ea8SMatthew G. Knepley     PetscInt dof, off, i, j, k, l;
13074bbf5ea8SMatthew G. Knepley 
13089566063dSJacob Faibussowitsch     PetscCall(PetscHMapIClear(ht));
13099566063dSJacob Faibussowitsch     PetscCall(PetscHMapIClear(htWithArtificial));
13109566063dSJacob Faibussowitsch     PetscCall(PetscHMapIClear(htWithAll));
13119566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(cellCounts, v, &dof));
13129566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(cellCounts, v, &off));
13134bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
13144bbf5ea8SMatthew G. Knepley 
1315557beb66SLawrence Mitchell     /* Calculate the global numbers of the artificial BC dofs here first */
13169566063dSJacob Faibussowitsch     PetscCall(patch->patchconstructop((void *)patch, dm, v, ownedpts));
13179566063dSJacob Faibussowitsch     PetscCall(PCPatchCompleteCellPatch(pc, ownedpts, seenpts));
13189566063dSJacob Faibussowitsch     PetscCall(PCPatchGetPointDofs(pc, ownedpts, owneddofs, v, &patch->subspaces_to_exclude));
13199566063dSJacob Faibussowitsch     PetscCall(PCPatchGetPointDofs(pc, seenpts, seendofs, v, NULL));
13209566063dSJacob Faibussowitsch     PetscCall(PCPatchComputeSetDifference_Private(owneddofs, seendofs, artificialbcs));
13218135ed82SLawrence Mitchell     if (patch->viewPatches) {
13221b68eb51SMatthew G. Knepley       PetscHSetI    globalbcdofs;
13231b68eb51SMatthew G. Knepley       PetscHashIter hi;
13248135ed82SLawrence Mitchell       MPI_Comm      comm = PetscObjectComm((PetscObject)pc);
13251b68eb51SMatthew G. Knepley 
13269566063dSJacob Faibussowitsch       PetscCall(PetscHSetICreate(&globalbcdofs));
132763a3b9bcSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": owned dofs:\n", v));
13281b68eb51SMatthew G. Knepley       PetscHashIterBegin(owneddofs, hi);
13291b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(owneddofs, hi)) {
13308135ed82SLawrence Mitchell         PetscInt globalDof;
13318135ed82SLawrence Mitchell 
13321b68eb51SMatthew G. Knepley         PetscHashIterGetKey(owneddofs, hi, globalDof);
13331b68eb51SMatthew G. Knepley         PetscHashIterNext(owneddofs, hi);
133463a3b9bcSJacob Faibussowitsch         PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof));
13358135ed82SLawrence Mitchell       }
13369566063dSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "\n"));
133763a3b9bcSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": seen dofs:\n", v));
13381b68eb51SMatthew G. Knepley       PetscHashIterBegin(seendofs, hi);
13391b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(seendofs, hi)) {
13408135ed82SLawrence Mitchell         PetscInt  globalDof;
13418135ed82SLawrence Mitchell         PetscBool flg;
13428135ed82SLawrence Mitchell 
13431b68eb51SMatthew G. Knepley         PetscHashIterGetKey(seendofs, hi, globalDof);
13441b68eb51SMatthew G. Knepley         PetscHashIterNext(seendofs, hi);
134563a3b9bcSJacob Faibussowitsch         PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof));
13468135ed82SLawrence Mitchell 
13479566063dSJacob Faibussowitsch         PetscCall(PetscHSetIHas(globalBcs, globalDof, &flg));
13489566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscHSetIAdd(globalbcdofs, globalDof));
13498135ed82SLawrence Mitchell       }
13509566063dSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "\n"));
135163a3b9bcSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": global BCs:\n", v));
13529566063dSJacob Faibussowitsch       PetscCall(PetscHSetIGetSize(globalbcdofs, &numBcs));
13538135ed82SLawrence Mitchell       if (numBcs > 0) {
13541b68eb51SMatthew G. Knepley         PetscHashIterBegin(globalbcdofs, hi);
13551b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(globalbcdofs, hi)) {
13568135ed82SLawrence Mitchell           PetscInt globalDof;
13571b68eb51SMatthew G. Knepley           PetscHashIterGetKey(globalbcdofs, hi, globalDof);
13581b68eb51SMatthew G. Knepley           PetscHashIterNext(globalbcdofs, hi);
135963a3b9bcSJacob Faibussowitsch           PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof));
13608135ed82SLawrence Mitchell         }
13618135ed82SLawrence Mitchell       }
13629566063dSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "\n"));
136363a3b9bcSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "Patch %" PetscInt_FMT ": artificial BCs:\n", v));
13649566063dSJacob Faibussowitsch       PetscCall(PetscHSetIGetSize(artificialbcs, &numBcs));
13658135ed82SLawrence Mitchell       if (numBcs > 0) {
13661b68eb51SMatthew G. Knepley         PetscHashIterBegin(artificialbcs, hi);
13671b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(artificialbcs, hi)) {
13688135ed82SLawrence Mitchell           PetscInt globalDof;
13691b68eb51SMatthew G. Knepley           PetscHashIterGetKey(artificialbcs, hi, globalDof);
13701b68eb51SMatthew G. Knepley           PetscHashIterNext(artificialbcs, hi);
137163a3b9bcSJacob Faibussowitsch           PetscCall(PetscSynchronizedPrintf(comm, "%" PetscInt_FMT " ", globalDof));
13728135ed82SLawrence Mitchell         }
13738135ed82SLawrence Mitchell       }
13749566063dSJacob Faibussowitsch       PetscCall(PetscSynchronizedPrintf(comm, "\n\n"));
13759566063dSJacob Faibussowitsch       PetscCall(PetscHSetIDestroy(&globalbcdofs));
13768135ed82SLawrence Mitchell     }
13774bbf5ea8SMatthew G. Knepley     for (k = 0; k < patch->nsubspaces; ++k) {
13784bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
13794bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
13804bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
13814bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
13824bbf5ea8SMatthew G. Knepley 
13834bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
13844bbf5ea8SMatthew G. Knepley         /* Walk over the cells in this patch. */
13854bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
13865f824522SMatthew G. Knepley         PetscInt       cell = c;
13874bbf5ea8SMatthew G. Knepley 
13885f824522SMatthew G. Knepley         /* TODO Change this to an IS */
13895f824522SMatthew G. Knepley         if (cellNumbering) {
13909566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetDof(cellNumbering, c, &cell));
139163a3b9bcSJacob Faibussowitsch           PetscCheck(cell > 0, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Cell %" PetscInt_FMT " doesn't appear in cell numbering map", c);
13929566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetOffset(cellNumbering, c, &cell));
13935f824522SMatthew G. Knepley         }
13944bbf5ea8SMatthew G. Knepley         newCellsArray[i] = cell;
13954bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
13964bbf5ea8SMatthew G. Knepley           /* For each global dof, map it into contiguous local storage. */
13974bbf5ea8SMatthew G. Knepley           const PetscInt globalDof = cellNodeMap[cell * nodesPerCell + j] * bs + subspaceOffset;
13984bbf5ea8SMatthew G. Knepley           /* finally, loop over block size */
13994bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
14001b68eb51SMatthew G. Knepley             PetscInt  localDof;
14011b68eb51SMatthew G. Knepley             PetscBool isGlobalBcDof, isArtificialBcDof;
14024bbf5ea8SMatthew G. Knepley 
1403557beb66SLawrence Mitchell             /* first, check if this is either a globally enforced or locally enforced BC dof */
14049566063dSJacob Faibussowitsch             PetscCall(PetscHSetIHas(globalBcs, globalDof + l, &isGlobalBcDof));
14059566063dSJacob Faibussowitsch             PetscCall(PetscHSetIHas(artificialbcs, globalDof + l, &isArtificialBcDof));
1406557beb66SLawrence Mitchell 
1407557beb66SLawrence Mitchell             /* if it's either, don't ever give it a local dof number */
14081b68eb51SMatthew G. Knepley             if (isGlobalBcDof || isArtificialBcDof) {
1409c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = -1; /* don't use this in assembly in this patch */
1410557beb66SLawrence Mitchell             } else {
14119566063dSJacob Faibussowitsch               PetscCall(PetscHMapIGet(ht, globalDof + l, &localDof));
14124bbf5ea8SMatthew G. Knepley               if (localDof == -1) {
14134bbf5ea8SMatthew G. Knepley                 localDof = localIndex++;
14149566063dSJacob Faibussowitsch                 PetscCall(PetscHMapISet(ht, globalDof + l, localDof));
14154bbf5ea8SMatthew G. Knepley               }
141663a3b9bcSJacob Faibussowitsch               PetscCheck(globalIndex < numDofs, PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %" PetscInt_FMT " than expected %" PetscInt_FMT, globalIndex + 1, numDofs);
14174bbf5ea8SMatthew G. Knepley               /* And store. */
1418c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = localDof;
14194bbf5ea8SMatthew G. Knepley             }
1420c2e6f3c0SFlorian Wechsung 
14210904074fSPatrick Farrell             if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1422c2e6f3c0SFlorian Wechsung               if (isGlobalBcDof) {
1423e047a90bSFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = -1; /* don't use this in assembly in this patch */
1424c2e6f3c0SFlorian Wechsung               } else {
14259566063dSJacob Faibussowitsch                 PetscCall(PetscHMapIGet(htWithArtificial, globalDof + l, &localDof));
1426c2e6f3c0SFlorian Wechsung                 if (localDof == -1) {
1427c2e6f3c0SFlorian Wechsung                   localDof = localIndexWithArtificial++;
14289566063dSJacob Faibussowitsch                   PetscCall(PetscHMapISet(htWithArtificial, globalDof + l, localDof));
1429c2e6f3c0SFlorian Wechsung                 }
143063a3b9bcSJacob Faibussowitsch                 PetscCheck(globalIndex < numDofs, PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %" PetscInt_FMT " than expected %" PetscInt_FMT, globalIndex + 1, numDofs);
1431c2e6f3c0SFlorian Wechsung                 /* And store.*/
1432c2e6f3c0SFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = localDof;
1433c2e6f3c0SFlorian Wechsung               }
1434c2e6f3c0SFlorian Wechsung             }
14350904074fSPatrick Farrell 
14360904074fSPatrick Farrell             if (isNonlinear) {
14370904074fSPatrick Farrell               /* Build the dofmap for the function space with _all_ dofs,
14380904074fSPatrick Farrell    including those in any kind of boundary condition */
14399566063dSJacob Faibussowitsch               PetscCall(PetscHMapIGet(htWithAll, globalDof + l, &localDof));
14400904074fSPatrick Farrell               if (localDof == -1) {
14410904074fSPatrick Farrell                 localDof = localIndexWithAll++;
14429566063dSJacob Faibussowitsch                 PetscCall(PetscHMapISet(htWithAll, globalDof + l, localDof));
14430904074fSPatrick Farrell               }
144463a3b9bcSJacob Faibussowitsch               PetscCheck(globalIndex < numDofs, PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %" PetscInt_FMT " than expected %" PetscInt_FMT, globalIndex + 1, numDofs);
14450904074fSPatrick Farrell               /* And store.*/
14460904074fSPatrick Farrell               dofsArrayWithAll[globalIndex] = localDof;
14470904074fSPatrick Farrell             }
1448c2e6f3c0SFlorian Wechsung             globalIndex++;
14494bbf5ea8SMatthew G. Knepley           }
14504bbf5ea8SMatthew G. Knepley         }
14514bbf5ea8SMatthew G. Knepley       }
1452557beb66SLawrence Mitchell     }
14534bbf5ea8SMatthew G. Knepley     /*How many local dofs in this patch? */
14540904074fSPatrick Farrell     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
14559566063dSJacob Faibussowitsch       PetscCall(PetscHMapIGetSize(htWithArtificial, &dof));
14569566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(gtolCountsWithArtificial, v, dof));
1457c2e6f3c0SFlorian Wechsung     }
14580904074fSPatrick Farrell     if (isNonlinear) {
14599566063dSJacob Faibussowitsch       PetscCall(PetscHMapIGetSize(htWithAll, &dof));
14609566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(gtolCountsWithAll, v, dof));
14610904074fSPatrick Farrell     }
14629566063dSJacob Faibussowitsch     PetscCall(PetscHMapIGetSize(ht, &dof));
14639566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(gtolCounts, v, dof));
14644bbf5ea8SMatthew G. Knepley   }
1465b6bb21d1SLawrence Mitchell 
14669566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
146763a3b9bcSJacob 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);
14689566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(gtolCounts));
14699566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(gtolCounts, &numGlobalDofs));
14709566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numGlobalDofs, &globalDofsArray));
14714bbf5ea8SMatthew G. Knepley 
14720904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
14739566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(gtolCountsWithArtificial));
14749566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(gtolCountsWithArtificial, &numGlobalDofsWithArtificial));
14759566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numGlobalDofsWithArtificial, &globalDofsArrayWithArtificial));
1476c2e6f3c0SFlorian Wechsung   }
14770904074fSPatrick Farrell   if (isNonlinear) {
14789566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(gtolCountsWithAll));
14799566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(gtolCountsWithAll, &numGlobalDofsWithAll));
14809566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numGlobalDofsWithAll, &globalDofsArrayWithAll));
14810904074fSPatrick Farrell   }
14824bbf5ea8SMatthew 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. */
14834bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
14841b68eb51SMatthew G. Knepley     PetscHashIter hi;
14855f824522SMatthew G. Knepley     PetscInt      dof, off, Np, ooff, i, j, k, l;
14864bbf5ea8SMatthew G. Knepley 
14879566063dSJacob Faibussowitsch     PetscCall(PetscHMapIClear(ht));
14889566063dSJacob Faibussowitsch     PetscCall(PetscHMapIClear(htWithArtificial));
14899566063dSJacob Faibussowitsch     PetscCall(PetscHMapIClear(htWithAll));
14909566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(cellCounts, v, &dof));
14919566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(cellCounts, v, &off));
14929566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(pointCounts, v, &Np));
14939566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(pointCounts, v, &ooff));
14944bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
14954bbf5ea8SMatthew G. Knepley 
14964bbf5ea8SMatthew G. Knepley     for (k = 0; k < patch->nsubspaces; ++k) {
14974bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
14984bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
14994bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
15004bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
1501d490bb3dSLawrence Mitchell       PetscInt        goff;
15024bbf5ea8SMatthew G. Knepley 
15034bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
15044bbf5ea8SMatthew G. Knepley         /* Reconstruct mapping of global-to-local on this patch. */
15054bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
15065f824522SMatthew G. Knepley         PetscInt       cell = c;
15074bbf5ea8SMatthew G. Knepley 
15089566063dSJacob Faibussowitsch         if (cellNumbering) PetscCall(PetscSectionGetOffset(cellNumbering, c, &cell));
15094bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
15104bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
15115f824522SMatthew G. Knepley             const PetscInt globalDof = cellNodeMap[cell * nodesPerCell + j] * bs + l + subspaceOffset;
1512c2e6f3c0SFlorian Wechsung             const PetscInt localDof  = dofsArray[key];
15139566063dSJacob Faibussowitsch             if (localDof >= 0) PetscCall(PetscHMapISet(ht, globalDof, localDof));
15140904074fSPatrick Farrell             if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1515c2e6f3c0SFlorian Wechsung               const PetscInt localDofWithArtificial = dofsArrayWithArtificial[key];
151648a46eb9SPierre Jolivet               if (localDofWithArtificial >= 0) PetscCall(PetscHMapISet(htWithArtificial, globalDof, localDofWithArtificial));
1517c2e6f3c0SFlorian Wechsung             }
15180904074fSPatrick Farrell             if (isNonlinear) {
15190904074fSPatrick Farrell               const PetscInt localDofWithAll = dofsArrayWithAll[key];
152048a46eb9SPierre Jolivet               if (localDofWithAll >= 0) PetscCall(PetscHMapISet(htWithAll, globalDof, localDofWithAll));
15210904074fSPatrick Farrell             }
1522c2e6f3c0SFlorian Wechsung             key++;
15234bbf5ea8SMatthew G. Knepley           }
15244bbf5ea8SMatthew G. Knepley         }
15254bbf5ea8SMatthew G. Knepley       }
1526557beb66SLawrence Mitchell 
15274bbf5ea8SMatthew G. Knepley       /* Shove it in the output data structure. */
15289566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(gtolCounts, v, &goff));
15291b68eb51SMatthew G. Knepley       PetscHashIterBegin(ht, hi);
15301b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(ht, hi)) {
15314bbf5ea8SMatthew G. Knepley         PetscInt globalDof, localDof;
15324bbf5ea8SMatthew G. Knepley 
15331b68eb51SMatthew G. Knepley         PetscHashIterGetKey(ht, hi, globalDof);
15341b68eb51SMatthew G. Knepley         PetscHashIterGetVal(ht, hi, localDof);
15354bbf5ea8SMatthew G. Knepley         if (globalDof >= 0) globalDofsArray[goff + localDof] = globalDof;
15361b68eb51SMatthew G. Knepley         PetscHashIterNext(ht, hi);
15374bbf5ea8SMatthew G. Knepley       }
15385f824522SMatthew G. Knepley 
15390904074fSPatrick Farrell       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
15409566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gtolCountsWithArtificial, v, &goff));
1541c2e6f3c0SFlorian Wechsung         PetscHashIterBegin(htWithArtificial, hi);
1542c2e6f3c0SFlorian Wechsung         while (!PetscHashIterAtEnd(htWithArtificial, hi)) {
1543c2e6f3c0SFlorian Wechsung           PetscInt globalDof, localDof;
1544c2e6f3c0SFlorian Wechsung           PetscHashIterGetKey(htWithArtificial, hi, globalDof);
1545c2e6f3c0SFlorian Wechsung           PetscHashIterGetVal(htWithArtificial, hi, localDof);
1546c2e6f3c0SFlorian Wechsung           if (globalDof >= 0) globalDofsArrayWithArtificial[goff + localDof] = globalDof;
1547c2e6f3c0SFlorian Wechsung           PetscHashIterNext(htWithArtificial, hi);
1548c2e6f3c0SFlorian Wechsung         }
1549c2e6f3c0SFlorian Wechsung       }
15500904074fSPatrick Farrell       if (isNonlinear) {
15519566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gtolCountsWithAll, v, &goff));
15520904074fSPatrick Farrell         PetscHashIterBegin(htWithAll, hi);
15530904074fSPatrick Farrell         while (!PetscHashIterAtEnd(htWithAll, hi)) {
15540904074fSPatrick Farrell           PetscInt globalDof, localDof;
15550904074fSPatrick Farrell           PetscHashIterGetKey(htWithAll, hi, globalDof);
15560904074fSPatrick Farrell           PetscHashIterGetVal(htWithAll, hi, localDof);
15570904074fSPatrick Farrell           if (globalDof >= 0) globalDofsArrayWithAll[goff + localDof] = globalDof;
15580904074fSPatrick Farrell           PetscHashIterNext(htWithAll, hi);
15590904074fSPatrick Farrell         }
15600904074fSPatrick Farrell       }
1561c2e6f3c0SFlorian Wechsung 
15625f824522SMatthew G. Knepley       for (p = 0; p < Np; ++p) {
15635f824522SMatthew G. Knepley         const PetscInt point = pointsArray[ooff + p];
15645f824522SMatthew G. Knepley         PetscInt       globalDof, localDof;
15655f824522SMatthew G. Knepley 
15669566063dSJacob Faibussowitsch         PetscCall(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, point, NULL, &globalDof));
15679566063dSJacob Faibussowitsch         PetscCall(PetscHMapIGet(ht, globalDof, &localDof));
15685f824522SMatthew G. Knepley         offsArray[(ooff + p) * Nf + k] = localDof;
15690904074fSPatrick Farrell         if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
15709566063dSJacob Faibussowitsch           PetscCall(PetscHMapIGet(htWithArtificial, globalDof, &localDof));
1571c2e6f3c0SFlorian Wechsung           offsArrayWithArtificial[(ooff + p) * Nf + k] = localDof;
1572c2e6f3c0SFlorian Wechsung         }
15730904074fSPatrick Farrell         if (isNonlinear) {
15749566063dSJacob Faibussowitsch           PetscCall(PetscHMapIGet(htWithAll, globalDof, &localDof));
15750904074fSPatrick Farrell           offsArrayWithAll[(ooff + p) * Nf + k] = localDof;
15760904074fSPatrick Farrell         }
15775f824522SMatthew G. Knepley       }
15784bbf5ea8SMatthew G. Knepley     }
15794bbf5ea8SMatthew G. Knepley 
15809566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&globalBcs));
15819566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&ownedpts));
15829566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&seenpts));
15839566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&owneddofs));
15849566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&seendofs));
15859566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&artificialbcs));
1586557beb66SLawrence Mitchell 
15874bbf5ea8SMatthew G. Knepley     /* At this point, we have a hash table ht built that maps globalDof -> localDof.
15884bbf5ea8SMatthew G. Knepley    We need to create the dof table laid out cellwise first, then by subspace,
15894bbf5ea8SMatthew G. Knepley    as the assembler assembles cell-wise and we need to stuff the different
15904bbf5ea8SMatthew G. Knepley    contributions of the different function spaces to the right places. So we loop
15914bbf5ea8SMatthew G. Knepley    over cells, then over subspaces. */
15924bbf5ea8SMatthew G. Knepley     if (patch->nsubspaces > 1) { /* for nsubspaces = 1, data we need is already in dofsArray */
15934bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
15944bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
15955f824522SMatthew G. Knepley         PetscInt       cell = c;
15964bbf5ea8SMatthew G. Knepley 
15979566063dSJacob Faibussowitsch         if (cellNumbering) PetscCall(PetscSectionGetOffset(cellNumbering, c, &cell));
15984bbf5ea8SMatthew G. Knepley         for (k = 0; k < patch->nsubspaces; ++k) {
15994bbf5ea8SMatthew G. Knepley           const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
16004bbf5ea8SMatthew G. Knepley           PetscInt        nodesPerCell   = patch->nodesPerCell[k];
16014bbf5ea8SMatthew G. Knepley           PetscInt        subspaceOffset = patch->subspaceOffsets[k];
16024bbf5ea8SMatthew G. Knepley           PetscInt        bs             = patch->bs[k];
16034bbf5ea8SMatthew G. Knepley 
16044bbf5ea8SMatthew G. Knepley           for (j = 0; j < nodesPerCell; ++j) {
16054bbf5ea8SMatthew G. Knepley             for (l = 0; l < bs; ++l) {
16065f824522SMatthew G. Knepley               const PetscInt globalDof = cellNodeMap[cell * nodesPerCell + j] * bs + l + subspaceOffset;
16074bbf5ea8SMatthew G. Knepley               PetscInt       localDof;
16084bbf5ea8SMatthew G. Knepley 
16099566063dSJacob Faibussowitsch               PetscCall(PetscHMapIGet(ht, globalDof, &localDof));
1610557beb66SLawrence Mitchell               /* If it's not in the hash table, i.e. is a BC dof,
16111b68eb51SMatthew G. Knepley    then the PetscHSetIMap above gives -1, which matches
1612557beb66SLawrence Mitchell    exactly the convention for PETSc's matrix assembly to
1613557beb66SLawrence Mitchell    ignore the dof. So we don't need to do anything here */
1614c2e6f3c0SFlorian Wechsung               asmArray[asmKey] = localDof;
16150904074fSPatrick Farrell               if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
16169566063dSJacob Faibussowitsch                 PetscCall(PetscHMapIGet(htWithArtificial, globalDof, &localDof));
1617c2e6f3c0SFlorian Wechsung                 asmArrayWithArtificial[asmKey] = localDof;
1618c2e6f3c0SFlorian Wechsung               }
16190904074fSPatrick Farrell               if (isNonlinear) {
16209566063dSJacob Faibussowitsch                 PetscCall(PetscHMapIGet(htWithAll, globalDof, &localDof));
16210904074fSPatrick Farrell                 asmArrayWithAll[asmKey] = localDof;
16220904074fSPatrick Farrell               }
1623c2e6f3c0SFlorian Wechsung               asmKey++;
16244bbf5ea8SMatthew G. Knepley             }
16254bbf5ea8SMatthew G. Knepley           }
16264bbf5ea8SMatthew G. Knepley         }
16274bbf5ea8SMatthew G. Knepley       }
16284bbf5ea8SMatthew G. Knepley     }
16294bbf5ea8SMatthew G. Knepley   }
1630c2e6f3c0SFlorian Wechsung   if (1 == patch->nsubspaces) {
16319566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(asmArray, dofsArray, numDofs));
163248a46eb9SPierre Jolivet     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) PetscCall(PetscArraycpy(asmArrayWithArtificial, dofsArrayWithArtificial, numDofs));
16331baa6e33SBarry Smith     if (isNonlinear) PetscCall(PetscArraycpy(asmArrayWithAll, dofsArrayWithAll, numDofs));
1634c2e6f3c0SFlorian Wechsung   }
16354bbf5ea8SMatthew G. Knepley 
16369566063dSJacob Faibussowitsch   PetscCall(PetscHMapIDestroy(&ht));
16379566063dSJacob Faibussowitsch   PetscCall(PetscHMapIDestroy(&htWithArtificial));
16389566063dSJacob Faibussowitsch   PetscCall(PetscHMapIDestroy(&htWithAll));
16399566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(cells, &cellsArray));
16409566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(points, &pointsArray));
16419566063dSJacob Faibussowitsch   PetscCall(PetscFree(dofsArray));
164248a46eb9SPierre Jolivet   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) PetscCall(PetscFree(dofsArrayWithArtificial));
16431baa6e33SBarry Smith   if (isNonlinear) PetscCall(PetscFree(dofsArrayWithAll));
16445f824522SMatthew G. Knepley   /* Create placeholder section for map from points to patch dofs */
16459566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &patch->patchSection));
16469566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(patch->patchSection, patch->nsubspaces));
16471e5fa6bbSLawrence Mitchell   if (patch->combined) {
16481e5fa6bbSLawrence Mitchell     PetscInt numFields;
16499566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(patch->dofSection[0], &numFields));
165063a3b9bcSJacob 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);
16519566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(patch->dofSection[0], &pStart, &pEnd));
16529566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(patch->patchSection, pStart, pEnd));
16535f824522SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
16545f824522SMatthew G. Knepley       PetscInt dof, fdof, f;
16555f824522SMatthew G. Knepley 
16569566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(patch->dofSection[0], p, &dof));
16579566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(patch->patchSection, p, dof));
16585f824522SMatthew G. Knepley       for (f = 0; f < patch->nsubspaces; ++f) {
16599566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldDof(patch->dofSection[0], p, f, &fdof));
16609566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetFieldDof(patch->patchSection, p, f, fdof));
16615f824522SMatthew G. Knepley       }
16621e5fa6bbSLawrence Mitchell     }
16631e5fa6bbSLawrence Mitchell   } else {
16641e5fa6bbSLawrence Mitchell     PetscInt pStartf, pEndf, f;
16651e5fa6bbSLawrence Mitchell     pStart = PETSC_MAX_INT;
16661e5fa6bbSLawrence Mitchell     pEnd   = PETSC_MIN_INT;
16671e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
16689566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf));
16691e5fa6bbSLawrence Mitchell       pStart = PetscMin(pStart, pStartf);
16701e5fa6bbSLawrence Mitchell       pEnd   = PetscMax(pEnd, pEndf);
16711e5fa6bbSLawrence Mitchell     }
16729566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(patch->patchSection, pStart, pEnd));
16731e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
16749566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf));
16751e5fa6bbSLawrence Mitchell       for (p = pStartf; p < pEndf; ++p) {
16761e5fa6bbSLawrence Mitchell         PetscInt fdof;
16779566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->dofSection[f], p, &fdof));
16789566063dSJacob Faibussowitsch         PetscCall(PetscSectionAddDof(patch->patchSection, p, fdof));
16799566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetFieldDof(patch->patchSection, p, f, fdof));
1680bdd9e0cdSPatrick Farrell       }
1681bdd9e0cdSPatrick Farrell     }
16825f824522SMatthew G. Knepley   }
16839566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(patch->patchSection));
16849566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUseFieldOffsets(patch->patchSection, PETSC_TRUE));
16854bbf5ea8SMatthew G. Knepley   /* Replace cell indices with firedrake-numbered ones. */
16869566063dSJacob Faibussowitsch   PetscCall(ISGeneralSetIndices(cells, numCells, (const PetscInt *)newCellsArray, PETSC_OWN_POINTER));
16879566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofs, globalDofsArray, PETSC_OWN_POINTER, &patch->gtol));
16889566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)patch->gtol, "Global Indices"));
16899566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_g2l_view", patch->classname));
16909566063dSJacob Faibussowitsch   PetscCall(PetscSectionViewFromOptions(patch->gtolCounts, (PetscObject)pc, option));
16919566063dSJacob Faibussowitsch   PetscCall(ISViewFromOptions(patch->gtol, (PetscObject)pc, option));
16929566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArray, PETSC_OWN_POINTER, &patch->dofs));
16939566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints * Nf, offsArray, PETSC_OWN_POINTER, &patch->offs));
16940904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
16959566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithArtificial, globalDofsArrayWithArtificial, PETSC_OWN_POINTER, &patch->gtolWithArtificial));
16969566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithArtificial, PETSC_OWN_POINTER, &patch->dofsWithArtificial));
16979566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints * Nf, offsArrayWithArtificial, PETSC_OWN_POINTER, &patch->offsWithArtificial));
1698c2e6f3c0SFlorian Wechsung   }
16990904074fSPatrick Farrell   if (isNonlinear) {
17009566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithAll, globalDofsArrayWithAll, PETSC_OWN_POINTER, &patch->gtolWithAll));
17019566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithAll, PETSC_OWN_POINTER, &patch->dofsWithAll));
17029566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPoints * Nf, offsArrayWithAll, PETSC_OWN_POINTER, &patch->offsWithAll));
17030904074fSPatrick Farrell   }
17043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
17054bbf5ea8SMatthew G. Knepley }
17064bbf5ea8SMatthew G. Knepley 
1707d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchCreateMatrix_Private(PC pc, PetscInt point, Mat *mat, PetscBool withArtificial)
1708d71ae5a4SJacob Faibussowitsch {
17094bbf5ea8SMatthew G. Knepley   PC_PATCH   *patch = (PC_PATCH *)pc->data;
17104bbf5ea8SMatthew G. Knepley   PetscBool   flg;
17114bbf5ea8SMatthew G. Knepley   PetscInt    csize, rsize;
17124bbf5ea8SMatthew G. Knepley   const char *prefix = NULL;
17134bbf5ea8SMatthew G. Knepley 
17144bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1715c2e6f3c0SFlorian Wechsung   if (withArtificial) {
1716e047a90bSFlorian Wechsung     /* would be nice if we could create a rectangular matrix of size numDofsWithArtificial x numDofs here */
17179d4fc724SLawrence Mitchell     PetscInt pStart;
17189566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(patch->gtolCountsWithArtificial, &pStart, NULL));
17199566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, point + pStart, &rsize));
17209d4fc724SLawrence Mitchell     csize = rsize;
1721ff201f6aSFlorian Wechsung   } else {
17229d4fc724SLawrence Mitchell     PetscInt pStart;
17239566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, NULL));
17249566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(patch->gtolCounts, point + pStart, &rsize));
17259d4fc724SLawrence Mitchell     csize = rsize;
1726c2e6f3c0SFlorian Wechsung   }
1727c2e6f3c0SFlorian Wechsung 
17289566063dSJacob Faibussowitsch   PetscCall(MatCreate(PETSC_COMM_SELF, mat));
17299566063dSJacob Faibussowitsch   PetscCall(PCGetOptionsPrefix(pc, &prefix));
17309566063dSJacob Faibussowitsch   PetscCall(MatSetOptionsPrefix(*mat, prefix));
17319566063dSJacob Faibussowitsch   PetscCall(MatAppendOptionsPrefix(*mat, "pc_patch_sub_"));
17329566063dSJacob Faibussowitsch   if (patch->sub_mat_type) PetscCall(MatSetType(*mat, patch->sub_mat_type));
17339566063dSJacob Faibussowitsch   else if (!patch->sub_mat_type) PetscCall(MatSetType(*mat, MATDENSE));
17349566063dSJacob Faibussowitsch   PetscCall(MatSetSizes(*mat, rsize, csize, rsize, csize));
17359566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)*mat, MATDENSE, &flg));
17369566063dSJacob Faibussowitsch   if (!flg) PetscCall(PetscObjectTypeCompare((PetscObject)*mat, MATSEQDENSE, &flg));
17374bbf5ea8SMatthew G. Knepley   /* Sparse patch matrices */
17384bbf5ea8SMatthew G. Knepley   if (!flg) {
17394bbf5ea8SMatthew G. Knepley     PetscBT         bt;
17404bbf5ea8SMatthew G. Knepley     PetscInt       *dnnz      = NULL;
17414bbf5ea8SMatthew G. Knepley     const PetscInt *dofsArray = NULL;
17424bbf5ea8SMatthew G. Knepley     PetscInt        pStart, pEnd, ncell, offset, c, i, j;
17434bbf5ea8SMatthew G. Knepley 
1744c2e6f3c0SFlorian Wechsung     if (withArtificial) {
17459566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(patch->dofsWithArtificial, &dofsArray));
1746ff201f6aSFlorian Wechsung     } else {
17479566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(patch->dofs, &dofsArray));
1748c2e6f3c0SFlorian Wechsung     }
17499566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd));
17504bbf5ea8SMatthew G. Knepley     point += pStart;
175163a3b9bcSJacob Faibussowitsch     PetscCheck(point < pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %" PetscInt_FMT " not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, pStart, pEnd);
17529566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(patch->cellCounts, point, &ncell));
17539566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(patch->cellCounts, point, &offset));
17549566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(PC_Patch_Prealloc, pc, 0, 0, 0));
1755b2866507SPatrick Farrell     /* A PetscBT uses N^2 bits to store the sparsity pattern on a
17564bbf5ea8SMatthew G. Knepley    * patch. This is probably OK if the patches are not too big,
1757b2866507SPatrick Farrell    * but uses too much memory. We therefore switch based on rsize. */
1758b2866507SPatrick Farrell     if (rsize < 3000) { /* FIXME: I picked this switch value out of my hat */
1759d63cebbaSPatrick Farrell       PetscScalar *zeroes;
1760d63cebbaSPatrick Farrell       PetscInt     rows;
1761d63cebbaSPatrick Farrell 
17629566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(rsize, &dnnz));
17639566063dSJacob Faibussowitsch       PetscCall(PetscBTCreate(rsize * rsize, &bt));
17644bbf5ea8SMatthew G. Knepley       for (c = 0; c < ncell; ++c) {
17654bbf5ea8SMatthew G. Knepley         const PetscInt *idx = dofsArray + (offset + c) * patch->totalDofsPerCell;
17664bbf5ea8SMatthew G. Knepley         for (i = 0; i < patch->totalDofsPerCell; ++i) {
17674bbf5ea8SMatthew G. Knepley           const PetscInt row = idx[i];
1768557beb66SLawrence Mitchell           if (row < 0) continue;
17694bbf5ea8SMatthew G. Knepley           for (j = 0; j < patch->totalDofsPerCell; ++j) {
17704bbf5ea8SMatthew G. Knepley             const PetscInt col = idx[j];
17714bbf5ea8SMatthew G. Knepley             const PetscInt key = row * rsize + col;
1772557beb66SLawrence Mitchell             if (col < 0) continue;
17734bbf5ea8SMatthew G. Knepley             if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
17744bbf5ea8SMatthew G. Knepley           }
17754bbf5ea8SMatthew G. Knepley         }
17764bbf5ea8SMatthew G. Knepley       }
1777d63cebbaSPatrick Farrell 
1778d63cebbaSPatrick Farrell       if (patch->usercomputeopintfacet) {
1779d63cebbaSPatrick Farrell         const PetscInt *intFacetsArray = NULL;
1780d63cebbaSPatrick Farrell         PetscInt        i, numIntFacets, intFacetOffset;
1781d63cebbaSPatrick Farrell         const PetscInt *facetCells = NULL;
1782d63cebbaSPatrick Farrell 
17839566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets));
17849566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset));
17859566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells));
17869566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray));
1787d63cebbaSPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
1788d63cebbaSPatrick Farrell           const PetscInt cell0 = facetCells[2 * (intFacetOffset + i) + 0];
1789d63cebbaSPatrick Farrell           const PetscInt cell1 = facetCells[2 * (intFacetOffset + i) + 1];
1790d63cebbaSPatrick Farrell           PetscInt       celli, cellj;
1791d63cebbaSPatrick Farrell 
1792d63cebbaSPatrick Farrell           for (celli = 0; celli < patch->totalDofsPerCell; celli++) {
1793d63cebbaSPatrick Farrell             const PetscInt row = dofsArray[(offset + cell0) * patch->totalDofsPerCell + celli];
1794b5c64f08SPatrick Farrell             if (row < 0) continue;
1795d63cebbaSPatrick Farrell             for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) {
1796d63cebbaSPatrick Farrell               const PetscInt col = dofsArray[(offset + cell1) * patch->totalDofsPerCell + cellj];
1797d63cebbaSPatrick Farrell               const PetscInt key = row * rsize + col;
1798d63cebbaSPatrick Farrell               if (col < 0) continue;
1799d63cebbaSPatrick Farrell               if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
1800d63cebbaSPatrick Farrell             }
1801d63cebbaSPatrick Farrell           }
1802d63cebbaSPatrick Farrell 
1803d63cebbaSPatrick Farrell           for (celli = 0; celli < patch->totalDofsPerCell; celli++) {
1804d63cebbaSPatrick Farrell             const PetscInt row = dofsArray[(offset + cell1) * patch->totalDofsPerCell + celli];
1805b5c64f08SPatrick Farrell             if (row < 0) continue;
1806d63cebbaSPatrick Farrell             for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) {
1807d63cebbaSPatrick Farrell               const PetscInt col = dofsArray[(offset + cell0) * patch->totalDofsPerCell + cellj];
1808d63cebbaSPatrick Farrell               const PetscInt key = row * rsize + col;
1809d63cebbaSPatrick Farrell               if (col < 0) continue;
1810d63cebbaSPatrick Farrell               if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
1811d63cebbaSPatrick Farrell             }
1812d63cebbaSPatrick Farrell           }
1813d63cebbaSPatrick Farrell         }
1814d63cebbaSPatrick Farrell       }
18159566063dSJacob Faibussowitsch       PetscCall(PetscBTDestroy(&bt));
18169566063dSJacob Faibussowitsch       PetscCall(MatXAIJSetPreallocation(*mat, 1, dnnz, NULL, NULL, NULL));
18179566063dSJacob Faibussowitsch       PetscCall(PetscFree(dnnz));
1818d63cebbaSPatrick Farrell 
18199566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(patch->totalDofsPerCell * patch->totalDofsPerCell, &zeroes));
1820d63cebbaSPatrick Farrell       for (c = 0; c < ncell; ++c) {
1821d63cebbaSPatrick Farrell         const PetscInt *idx = &dofsArray[(offset + c) * patch->totalDofsPerCell];
18229566063dSJacob Faibussowitsch         PetscCall(MatSetValues(*mat, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, zeroes, INSERT_VALUES));
1823d63cebbaSPatrick Farrell       }
18249566063dSJacob Faibussowitsch       PetscCall(MatGetLocalSize(*mat, &rows, NULL));
182548a46eb9SPierre Jolivet       for (i = 0; i < rows; ++i) PetscCall(MatSetValues(*mat, 1, &i, 1, &i, zeroes, INSERT_VALUES));
1826d63cebbaSPatrick Farrell 
1827d63cebbaSPatrick Farrell       if (patch->usercomputeopintfacet) {
1828d63cebbaSPatrick Farrell         const PetscInt *intFacetsArray = NULL;
1829d63cebbaSPatrick Farrell         PetscInt        i, numIntFacets, intFacetOffset;
1830d63cebbaSPatrick Farrell         const PetscInt *facetCells = NULL;
1831d63cebbaSPatrick Farrell 
18329566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets));
18339566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset));
18349566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells));
18359566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray));
1836d63cebbaSPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
1837d63cebbaSPatrick Farrell           const PetscInt  cell0    = facetCells[2 * (intFacetOffset + i) + 0];
1838d63cebbaSPatrick Farrell           const PetscInt  cell1    = facetCells[2 * (intFacetOffset + i) + 1];
1839d63cebbaSPatrick Farrell           const PetscInt *cell0idx = &dofsArray[(offset + cell0) * patch->totalDofsPerCell];
1840d63cebbaSPatrick Farrell           const PetscInt *cell1idx = &dofsArray[(offset + cell1) * patch->totalDofsPerCell];
18419566063dSJacob Faibussowitsch           PetscCall(MatSetValues(*mat, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, zeroes, INSERT_VALUES));
18429566063dSJacob Faibussowitsch           PetscCall(MatSetValues(*mat, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, zeroes, INSERT_VALUES));
1843d63cebbaSPatrick Farrell         }
1844d63cebbaSPatrick Farrell       }
1845d63cebbaSPatrick Farrell 
18469566063dSJacob Faibussowitsch       PetscCall(MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY));
18479566063dSJacob Faibussowitsch       PetscCall(MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY));
1848d63cebbaSPatrick Farrell 
18499566063dSJacob Faibussowitsch       PetscCall(PetscFree(zeroes));
1850d63cebbaSPatrick Farrell 
1851b2866507SPatrick Farrell     } else { /* rsize too big, use MATPREALLOCATOR */
1852b2866507SPatrick Farrell       Mat          preallocator;
1853b2866507SPatrick Farrell       PetscScalar *vals;
1854b2866507SPatrick Farrell 
18559566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(patch->totalDofsPerCell * patch->totalDofsPerCell, &vals));
18569566063dSJacob Faibussowitsch       PetscCall(MatCreate(PETSC_COMM_SELF, &preallocator));
18579566063dSJacob Faibussowitsch       PetscCall(MatSetType(preallocator, MATPREALLOCATOR));
18589566063dSJacob Faibussowitsch       PetscCall(MatSetSizes(preallocator, rsize, rsize, rsize, rsize));
18599566063dSJacob Faibussowitsch       PetscCall(MatSetUp(preallocator));
186011bcd083SPatrick Farrell 
1861b2866507SPatrick Farrell       for (c = 0; c < ncell; ++c) {
1862b2866507SPatrick Farrell         const PetscInt *idx = dofsArray + (offset + c) * patch->totalDofsPerCell;
18639566063dSJacob Faibussowitsch         PetscCall(MatSetValues(preallocator, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, vals, INSERT_VALUES));
1864b2866507SPatrick Farrell       }
186511bcd083SPatrick Farrell 
186611bcd083SPatrick Farrell       if (patch->usercomputeopintfacet) {
186711bcd083SPatrick Farrell         const PetscInt *intFacetsArray = NULL;
186811bcd083SPatrick Farrell         PetscInt        i, numIntFacets, intFacetOffset;
186911bcd083SPatrick Farrell         const PetscInt *facetCells = NULL;
187011bcd083SPatrick Farrell 
18719566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets));
18729566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset));
18739566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells));
18749566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray));
187511bcd083SPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
187611bcd083SPatrick Farrell           const PetscInt  cell0    = facetCells[2 * (intFacetOffset + i) + 0];
187711bcd083SPatrick Farrell           const PetscInt  cell1    = facetCells[2 * (intFacetOffset + i) + 1];
187811bcd083SPatrick Farrell           const PetscInt *cell0idx = &dofsArray[(offset + cell0) * patch->totalDofsPerCell];
187911bcd083SPatrick Farrell           const PetscInt *cell1idx = &dofsArray[(offset + cell1) * patch->totalDofsPerCell];
18809566063dSJacob Faibussowitsch           PetscCall(MatSetValues(preallocator, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, vals, INSERT_VALUES));
18819566063dSJacob Faibussowitsch           PetscCall(MatSetValues(preallocator, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, vals, INSERT_VALUES));
188211bcd083SPatrick Farrell         }
188311bcd083SPatrick Farrell       }
188411bcd083SPatrick Farrell 
18859566063dSJacob Faibussowitsch       PetscCall(PetscFree(vals));
18869566063dSJacob Faibussowitsch       PetscCall(MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY));
18879566063dSJacob Faibussowitsch       PetscCall(MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY));
18889566063dSJacob Faibussowitsch       PetscCall(MatPreallocatorPreallocate(preallocator, PETSC_TRUE, *mat));
18899566063dSJacob Faibussowitsch       PetscCall(MatDestroy(&preallocator));
1890b2866507SPatrick Farrell     }
18919566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(PC_Patch_Prealloc, pc, 0, 0, 0));
1892fe117d09SFlorian Wechsung     if (withArtificial) {
18939566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(patch->dofsWithArtificial, &dofsArray));
1894fe117d09SFlorian Wechsung     } else {
18959566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(patch->dofs, &dofsArray));
18964bbf5ea8SMatthew G. Knepley     }
1897fe117d09SFlorian Wechsung   }
18989566063dSJacob Faibussowitsch   PetscCall(MatSetUp(*mat));
18993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19004bbf5ea8SMatthew G. Knepley }
19014bbf5ea8SMatthew G. Knepley 
1902d71ae5a4SJacob 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)
1903d71ae5a4SJacob Faibussowitsch {
190492d50984SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *)pc->data;
1905b6bb21d1SLawrence Mitchell   DM              dm, plex;
190692d50984SMatthew G. Knepley   PetscSection    s;
190792d50984SMatthew G. Knepley   const PetscInt *parray, *oarray;
190892d50984SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
190992d50984SMatthew G. Knepley 
191092d50984SMatthew G. Knepley   PetscFunctionBegin;
191128b400f6SJacob Faibussowitsch   PetscCheck(!patch->precomputeElementTensors, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Precomputing element tensors not implemented with DMPlex compute operator");
19129566063dSJacob Faibussowitsch   PetscCall(PCGetDM(pc, &dm));
19139566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
1914b6bb21d1SLawrence Mitchell   dm = plex;
19159566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
191692d50984SMatthew G. Knepley   /* Set offset into patch */
19179566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetDof(patch->pointCounts, patchNum, &Np));
19189566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(patch->pointCounts, patchNum, &poff));
19199566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->points, &parray));
19209566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->offs, &oarray));
192192d50984SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
192292d50984SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
192392d50984SMatthew G. Knepley       const PetscInt point = parray[poff + p];
192492d50984SMatthew G. Knepley       PetscInt       dof;
192592d50984SMatthew G. Knepley 
19269566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(patch->patchSection, point, f, &dof));
19279566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff + p) * Nf + f]));
19289566063dSJacob Faibussowitsch       if (patch->nsubspaces == 1) PetscCall(PetscSectionSetOffset(patch->patchSection, point, oarray[(poff + p) * Nf + f]));
19299566063dSJacob Faibussowitsch       else PetscCall(PetscSectionSetOffset(patch->patchSection, point, -1));
193092d50984SMatthew G. Knepley     }
193192d50984SMatthew G. Knepley   }
19329566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->points, &parray));
19339566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->offs, &oarray));
19349566063dSJacob Faibussowitsch   if (patch->viewSection) PetscCall(ObjectView((PetscObject)patch->patchSection, patch->viewerSection, patch->formatSection));
19359566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeResidual_Patch_Internal(dm, patch->patchSection, cellIS, 0.0, x, NULL, F, ctx));
19369566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
19373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
193892d50984SMatthew G. Knepley }
193992d50984SMatthew G. Knepley 
1940d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchComputeFunction_Internal(PC pc, Vec x, Vec F, PetscInt point)
1941d71ae5a4SJacob Faibussowitsch {
194292d50984SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *)pc->data;
194392d50984SMatthew G. Knepley   const PetscInt *dofsArray;
19440904074fSPatrick Farrell   const PetscInt *dofsArrayWithAll;
194592d50984SMatthew G. Knepley   const PetscInt *cellsArray;
194692d50984SMatthew G. Knepley   PetscInt        ncell, offset, pStart, pEnd;
194792d50984SMatthew G. Knepley 
194892d50984SMatthew G. Knepley   PetscFunctionBegin;
19499566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0));
1950ef1023bdSBarry Smith   PetscCheck(patch->usercomputeop, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set callback");
19519566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->dofs, &dofsArray));
19529566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll));
19539566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->cells, &cellsArray));
19549566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd));
195592d50984SMatthew G. Knepley 
195692d50984SMatthew G. Knepley   point += pStart;
195763a3b9bcSJacob Faibussowitsch   PetscCheck(point < pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %" PetscInt_FMT " not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, pStart, pEnd);
195892d50984SMatthew G. Knepley 
19599566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetDof(patch->cellCounts, point, &ncell));
19609566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(patch->cellCounts, point, &offset));
196192d50984SMatthew G. Knepley   if (ncell <= 0) {
19629566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
19633ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
196492d50984SMatthew G. Knepley   }
19659566063dSJacob Faibussowitsch   PetscCall(VecSet(F, 0.0));
196692d50984SMatthew G. Knepley   /* Cannot reuse the same IS because the geometry info is being cached in it */
19679566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS));
1968792fecdfSBarry 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));
19699566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->cellIS));
19709566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->dofs, &dofsArray));
19719566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll));
19729566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->cells, &cellsArray));
197392d50984SMatthew G. Knepley   if (patch->viewMatrix) {
197492d50984SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
197592d50984SMatthew G. Knepley 
197663a3b9bcSJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN - 1, "Patch vector for Point %" PetscInt_FMT, point));
19779566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)F, name));
19789566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)F, patch->viewerMatrix, patch->formatMatrix));
197992d50984SMatthew G. Knepley   }
19809566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
19813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
198292d50984SMatthew G. Knepley }
198392d50984SMatthew G. Knepley 
1984d71ae5a4SJacob 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)
1985d71ae5a4SJacob Faibussowitsch {
19865f824522SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *)pc->data;
1987b6bb21d1SLawrence Mitchell   DM              dm, plex;
19885f824522SMatthew G. Knepley   PetscSection    s;
19895f824522SMatthew G. Knepley   const PetscInt *parray, *oarray;
19905f824522SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
19915f824522SMatthew G. Knepley 
19925f824522SMatthew G. Knepley   PetscFunctionBegin;
19939566063dSJacob Faibussowitsch   PetscCall(PCGetDM(pc, &dm));
19949566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
1995b6bb21d1SLawrence Mitchell   dm = plex;
19969566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
19975f824522SMatthew G. Knepley   /* Set offset into patch */
19989566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetDof(patch->pointCounts, patchNum, &Np));
19999566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(patch->pointCounts, patchNum, &poff));
20009566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->points, &parray));
20019566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->offs, &oarray));
20025f824522SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
20035f824522SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
20045f824522SMatthew G. Knepley       const PetscInt point = parray[poff + p];
20055f824522SMatthew G. Knepley       PetscInt       dof;
20065f824522SMatthew G. Knepley 
20079566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(patch->patchSection, point, f, &dof));
20089566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff + p) * Nf + f]));
20099566063dSJacob Faibussowitsch       if (patch->nsubspaces == 1) PetscCall(PetscSectionSetOffset(patch->patchSection, point, oarray[(poff + p) * Nf + f]));
20109566063dSJacob Faibussowitsch       else PetscCall(PetscSectionSetOffset(patch->patchSection, point, -1));
20115f824522SMatthew G. Knepley     }
20125f824522SMatthew G. Knepley   }
20139566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->points, &parray));
20149566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->offs, &oarray));
20159566063dSJacob Faibussowitsch   if (patch->viewSection) PetscCall(ObjectView((PetscObject)patch->patchSection, patch->viewerSection, patch->formatSection));
20165f824522SMatthew G. Knepley   /* TODO Shut off MatViewFromOptions() in MatAssemblyEnd() here */
20179566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeJacobian_Patch_Internal(dm, patch->patchSection, patch->patchSection, cellIS, 0.0, 0.0, x, NULL, J, J, ctx));
20189566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
20193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
20205f824522SMatthew G. Knepley }
20215f824522SMatthew G. Knepley 
2022a685ae26SLawrence Mitchell /* This function zeros mat on entry */
2023d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatchComputeOperator_Internal(PC pc, Vec x, Mat mat, PetscInt point, PetscBool withArtificial)
2024d71ae5a4SJacob Faibussowitsch {
20254bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *)pc->data;
20264bbf5ea8SMatthew G. Knepley   const PetscInt *dofsArray;
20270904074fSPatrick Farrell   const PetscInt *dofsArrayWithAll = NULL;
20284bbf5ea8SMatthew G. Knepley   const PetscInt *cellsArray;
2029eb62eeaaSLawrence Mitchell   PetscInt        ncell, offset, pStart, pEnd, numIntFacets, intFacetOffset;
20304d04e9f1SPatrick Farrell   PetscBool       isNonlinear;
20314bbf5ea8SMatthew G. Knepley 
20324bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
20339566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0));
2034debbdec3SPatrick Farrell   isNonlinear = patch->isNonlinear;
2035ef1023bdSBarry Smith   PetscCheck(patch->usercomputeop, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set callback");
2036c2e6f3c0SFlorian Wechsung   if (withArtificial) {
20379566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->dofsWithArtificial, &dofsArray));
2038c2e6f3c0SFlorian Wechsung   } else {
20399566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->dofs, &dofsArray));
2040c2e6f3c0SFlorian Wechsung   }
204148a46eb9SPierre Jolivet   if (isNonlinear) PetscCall(ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll));
20429566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->cells, &cellsArray));
20439566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd));
20444bbf5ea8SMatthew G. Knepley 
20454bbf5ea8SMatthew G. Knepley   point += pStart;
204663a3b9bcSJacob Faibussowitsch   PetscCheck(point < pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %" PetscInt_FMT " not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, pStart, pEnd);
20474bbf5ea8SMatthew G. Knepley 
20489566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetDof(patch->cellCounts, point, &ncell));
20499566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(patch->cellCounts, point, &offset));
20504bbf5ea8SMatthew G. Knepley   if (ncell <= 0) {
20519566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
20523ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
20534bbf5ea8SMatthew G. Knepley   }
20549566063dSJacob Faibussowitsch   PetscCall(MatZeroEntries(mat));
2055fa84ea4cSLawrence Mitchell   if (patch->precomputeElementTensors) {
2056fa84ea4cSLawrence Mitchell     PetscInt           i;
2057fa84ea4cSLawrence Mitchell     PetscInt           ndof = patch->totalDofsPerCell;
2058fa84ea4cSLawrence Mitchell     const PetscScalar *elementTensors;
2059fa84ea4cSLawrence Mitchell 
20609566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(patch->cellMats, &elementTensors));
2061fa84ea4cSLawrence Mitchell     for (i = 0; i < ncell; i++) {
2062fa84ea4cSLawrence Mitchell       const PetscInt     cell = cellsArray[i + offset];
2063fa84ea4cSLawrence Mitchell       const PetscInt    *idx  = dofsArray + (offset + i) * ndof;
2064fe988be2SFlorian Wechsung       const PetscScalar *v    = elementTensors + patch->precomputedTensorLocations[cell] * ndof * ndof;
20659566063dSJacob Faibussowitsch       PetscCall(MatSetValues(mat, ndof, idx, ndof, idx, v, ADD_VALUES));
2066fa84ea4cSLawrence Mitchell     }
20679566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(patch->cellMats, &elementTensors));
20689566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
20699566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
2070fa84ea4cSLawrence Mitchell   } else {
20712aa6f319SMatthew G. Knepley     /* Cannot reuse the same IS because the geometry info is being cached in it */
20729566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS));
20739371c9d4SSatish Balay     PetscCallBack("PCPatch callback",
20749371c9d4SSatish Balay                   patch->usercomputeop(pc, point, x, mat, patch->cellIS, ncell * patch->totalDofsPerCell, dofsArray + offset * patch->totalDofsPerCell, dofsArrayWithAll ? dofsArrayWithAll + offset * patch->totalDofsPerCell : NULL, patch->usercomputeopctx));
2075fa84ea4cSLawrence Mitchell   }
207659109abcSLawrence Mitchell   if (patch->usercomputeopintfacet) {
20779566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets));
20789566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset));
2079eb62eeaaSLawrence Mitchell     if (numIntFacets > 0) {
2080eb62eeaaSLawrence Mitchell       /* For each interior facet, grab the two cells (in local numbering, and concatenate dof numberings for those cells) */
2081eb62eeaaSLawrence Mitchell       PetscInt       *facetDofs = NULL, *facetDofsWithAll = NULL;
2082eb62eeaaSLawrence Mitchell       const PetscInt *intFacetsArray = NULL;
2083eb62eeaaSLawrence Mitchell       PetscInt        idx            = 0;
2084eb62eeaaSLawrence Mitchell       PetscInt        i, c, d;
2085de2d1767SPatrick Farrell       PetscInt        fStart;
2086b6bb21d1SLawrence Mitchell       DM              dm, plex;
2087eb62eeaaSLawrence Mitchell       IS              facetIS    = NULL;
2088eb62eeaaSLawrence Mitchell       const PetscInt *facetCells = NULL;
20897a50e09dSPatrick Farrell 
20909566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(patch->intFacetsToPatchCell, &facetCells));
20919566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray));
20929566063dSJacob Faibussowitsch       PetscCall(PCGetDM(pc, &dm));
20939566063dSJacob Faibussowitsch       PetscCall(DMConvert(dm, DMPLEX, &plex));
2094b6bb21d1SLawrence Mitchell       dm = plex;
20959566063dSJacob Faibussowitsch       PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, NULL));
2096eb62eeaaSLawrence Mitchell       /* FIXME: Pull this malloc out. */
20979566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofs));
209848a46eb9SPierre Jolivet       if (dofsArrayWithAll) PetscCall(PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofsWithAll));
2099f98464cbSLawrence Mitchell       if (patch->precomputeElementTensors) {
2100f98464cbSLawrence Mitchell         PetscInt           nFacetDof = 2 * patch->totalDofsPerCell;
2101f98464cbSLawrence Mitchell         const PetscScalar *elementTensors;
2102f98464cbSLawrence Mitchell 
21039566063dSJacob Faibussowitsch         PetscCall(VecGetArrayRead(patch->intFacetMats, &elementTensors));
2104f98464cbSLawrence Mitchell 
2105f98464cbSLawrence Mitchell         for (i = 0; i < numIntFacets; i++) {
2106f98464cbSLawrence Mitchell           const PetscInt     facet = intFacetsArray[i + intFacetOffset];
2107de2d1767SPatrick Farrell           const PetscScalar *v     = elementTensors + patch->precomputedIntFacetTensorLocations[facet - fStart] * nFacetDof * nFacetDof;
2108f98464cbSLawrence Mitchell           idx                      = 0;
2109f98464cbSLawrence Mitchell           /*
2110f1580f4eSBarry Smith      0--1
2111f1580f4eSBarry Smith      |\-|
2112f1580f4eSBarry Smith      |+\|
2113f1580f4eSBarry Smith      2--3
2114f1580f4eSBarry Smith      [0, 2, 3, 0, 1, 3]
2115f98464cbSLawrence Mitchell    */
2116f98464cbSLawrence Mitchell           for (c = 0; c < 2; c++) {
2117f98464cbSLawrence Mitchell             const PetscInt cell = facetCells[2 * (intFacetOffset + i) + c];
2118f98464cbSLawrence Mitchell             for (d = 0; d < patch->totalDofsPerCell; d++) {
2119f98464cbSLawrence Mitchell               facetDofs[idx] = dofsArray[(offset + cell) * patch->totalDofsPerCell + d];
2120f98464cbSLawrence Mitchell               idx++;
2121f98464cbSLawrence Mitchell             }
2122f98464cbSLawrence Mitchell           }
21239566063dSJacob Faibussowitsch           PetscCall(MatSetValues(mat, nFacetDof, facetDofs, nFacetDof, facetDofs, v, ADD_VALUES));
2124f98464cbSLawrence Mitchell         }
21259566063dSJacob Faibussowitsch         PetscCall(VecRestoreArrayRead(patch->intFacetMats, &elementTensors));
2126f98464cbSLawrence Mitchell       } else {
2127eb62eeaaSLawrence Mitchell         /*
2128f1580f4eSBarry Smith      0--1
2129f1580f4eSBarry Smith      |\-|
2130f1580f4eSBarry Smith      |+\|
2131f1580f4eSBarry Smith      2--3
2132f1580f4eSBarry Smith      [0, 2, 3, 0, 1, 3]
2133eb62eeaaSLawrence Mitchell    */
2134eb62eeaaSLawrence Mitchell         for (i = 0; i < numIntFacets; i++) {
2135eb62eeaaSLawrence Mitchell           for (c = 0; c < 2; c++) {
2136eb62eeaaSLawrence Mitchell             const PetscInt cell = facetCells[2 * (intFacetOffset + i) + c];
2137eb62eeaaSLawrence Mitchell             for (d = 0; d < patch->totalDofsPerCell; d++) {
2138eb62eeaaSLawrence Mitchell               facetDofs[idx] = dofsArray[(offset + cell) * patch->totalDofsPerCell + d];
2139ad540459SPierre Jolivet               if (dofsArrayWithAll) facetDofsWithAll[idx] = dofsArrayWithAll[(offset + cell) * patch->totalDofsPerCell + d];
2140eb62eeaaSLawrence Mitchell               idx++;
2141eb62eeaaSLawrence Mitchell             }
2142eb62eeaaSLawrence Mitchell           }
2143eb62eeaaSLawrence Mitchell         }
21449566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray + intFacetOffset, PETSC_USE_POINTER, &facetIS));
21459566063dSJacob Faibussowitsch         PetscCall(patch->usercomputeopintfacet(pc, point, x, mat, facetIS, 2 * numIntFacets * patch->totalDofsPerCell, facetDofs, facetDofsWithAll, patch->usercomputeopintfacetctx));
21469566063dSJacob Faibussowitsch         PetscCall(ISDestroy(&facetIS));
2147f98464cbSLawrence Mitchell       }
21489566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(patch->intFacetsToPatchCell, &facetCells));
21499566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(patch->intFacets, &intFacetsArray));
21509566063dSJacob Faibussowitsch       PetscCall(PetscFree(facetDofs));
21519566063dSJacob Faibussowitsch       PetscCall(PetscFree(facetDofsWithAll));
21529566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dm));
2153eb62eeaaSLawrence Mitchell     }
215459109abcSLawrence Mitchell   }
21556710cc29SPatrick Farrell 
21569566063dSJacob Faibussowitsch   PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
21579566063dSJacob Faibussowitsch   PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
21586710cc29SPatrick Farrell 
2159c73d2cf6SLawrence Mitchell   if (!(withArtificial || isNonlinear) && patch->denseinverse) {
2160c73d2cf6SLawrence Mitchell     MatFactorInfo info;
2161c73d2cf6SLawrence Mitchell     PetscBool     flg;
21629566063dSJacob Faibussowitsch     PetscCall(PetscObjectTypeCompare((PetscObject)mat, MATSEQDENSE, &flg));
216328b400f6SJacob Faibussowitsch     PetscCheck(flg, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Invalid Mat type for dense inverse");
21649566063dSJacob Faibussowitsch     PetscCall(MatFactorInfoInitialize(&info));
21659566063dSJacob Faibussowitsch     PetscCall(MatLUFactor(mat, NULL, NULL, &info));
21669566063dSJacob Faibussowitsch     PetscCall(MatSeqDenseInvertFactors_Private(mat));
2167c73d2cf6SLawrence Mitchell   }
21689566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->cellIS));
21694d04e9f1SPatrick Farrell   if (withArtificial) {
21709566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(patch->dofsWithArtificial, &dofsArray));
2171c2e6f3c0SFlorian Wechsung   } else {
21729566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(patch->dofs, &dofsArray));
2173c2e6f3c0SFlorian Wechsung   }
217448a46eb9SPierre Jolivet   if (isNonlinear) PetscCall(ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll));
21759566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->cells, &cellsArray));
21762aa6f319SMatthew G. Knepley   if (patch->viewMatrix) {
21772aa6f319SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
21782aa6f319SMatthew G. Knepley 
217963a3b9bcSJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN - 1, "Patch matrix for Point %" PetscInt_FMT, point));
21809566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)mat, name));
21819566063dSJacob Faibussowitsch     PetscCall(ObjectView((PetscObject)mat, patch->viewerMatrix, patch->formatMatrix));
21822aa6f319SMatthew G. Knepley   }
21839566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
21843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21854bbf5ea8SMatthew G. Knepley }
21864bbf5ea8SMatthew G. Knepley 
2187d71ae5a4SJacob Faibussowitsch static PetscErrorCode MatSetValues_PCPatch_Private(Mat mat, PetscInt m, const PetscInt idxm[], PetscInt n, const PetscInt idxn[], const PetscScalar *v, InsertMode addv)
2188d71ae5a4SJacob Faibussowitsch {
2189fa84ea4cSLawrence Mitchell   Vec          data;
2190fa84ea4cSLawrence Mitchell   PetscScalar *array;
2191fe988be2SFlorian Wechsung   PetscInt     bs, nz, i, j, cell;
2192fa84ea4cSLawrence Mitchell 
21939566063dSJacob Faibussowitsch   PetscCall(MatShellGetContext(mat, &data));
21949566063dSJacob Faibussowitsch   PetscCall(VecGetBlockSize(data, &bs));
21959566063dSJacob Faibussowitsch   PetscCall(VecGetSize(data, &nz));
21969566063dSJacob Faibussowitsch   PetscCall(VecGetArray(data, &array));
219708401ef6SPierre Jolivet   PetscCheck(m == n, PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Only for square insertion");
219833cbca70SPatrick Farrell   cell = (PetscInt)(idxm[0] / bs); /* use the fact that this is called once per cell */
2199fa84ea4cSLawrence Mitchell   for (i = 0; i < m; i++) {
220008401ef6SPierre Jolivet     PetscCheck(idxm[i] == idxn[i], PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Row and column indices must match!");
2201fa84ea4cSLawrence Mitchell     for (j = 0; j < n; j++) {
2202fa84ea4cSLawrence Mitchell       const PetscScalar v_ = v[i * bs + j];
2203fa84ea4cSLawrence Mitchell       /* Indexing is special to the data structure we have! */
2204fa84ea4cSLawrence Mitchell       if (addv == INSERT_VALUES) {
2205fe988be2SFlorian Wechsung         array[cell * bs * bs + i * bs + j] = v_;
2206fa84ea4cSLawrence Mitchell       } else {
2207fe988be2SFlorian Wechsung         array[cell * bs * bs + i * bs + j] += v_;
2208fa84ea4cSLawrence Mitchell       }
2209fa84ea4cSLawrence Mitchell     }
2210fa84ea4cSLawrence Mitchell   }
22119566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(data, &array));
22123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2213fa84ea4cSLawrence Mitchell }
2214fa84ea4cSLawrence Mitchell 
2215d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPatchPrecomputePatchTensors_Private(PC pc)
2216d71ae5a4SJacob Faibussowitsch {
2217fa84ea4cSLawrence Mitchell   PC_PATCH       *patch = (PC_PATCH *)pc->data;
2218fa84ea4cSLawrence Mitchell   const PetscInt *cellsArray;
2219fa84ea4cSLawrence Mitchell   PetscInt        ncell, offset;
2220fa84ea4cSLawrence Mitchell   const PetscInt *dofMapArray;
2221fa84ea4cSLawrence Mitchell   PetscInt        i, j;
2222fa84ea4cSLawrence Mitchell   IS              dofMap;
2223fa84ea4cSLawrence Mitchell   IS              cellIS;
2224fa84ea4cSLawrence Mitchell   const PetscInt  ndof = patch->totalDofsPerCell;
2225fa84ea4cSLawrence Mitchell   Mat             vecMat;
2226fe988be2SFlorian Wechsung   PetscInt        cStart, cEnd;
2227fe988be2SFlorian Wechsung   DM              dm, plex;
2228fe988be2SFlorian Wechsung 
22299566063dSJacob Faibussowitsch   PetscCall(ISGetSize(patch->cells, &ncell));
2230e9c2c94bSFlorian Wechsung   if (!ncell) { /* No cells to assemble over -> skip */
22313ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2232e9c2c94bSFlorian Wechsung   }
2233e9c2c94bSFlorian Wechsung 
22349566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0));
2235fa84ea4cSLawrence Mitchell 
22369566063dSJacob Faibussowitsch   PetscCall(PCGetDM(pc, &dm));
22379566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
2238b6bb21d1SLawrence Mitchell   dm = plex;
2239fa84ea4cSLawrence Mitchell   if (!patch->allCells) {
2240fa84ea4cSLawrence Mitchell     PetscHSetI    cells;
2241fa84ea4cSLawrence Mitchell     PetscHashIter hi;
2242fa84ea4cSLawrence Mitchell     PetscInt      pStart, pEnd;
2243fa84ea4cSLawrence Mitchell     PetscInt     *allCells = NULL;
22449566063dSJacob Faibussowitsch     PetscCall(PetscHSetICreate(&cells));
22459566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->cells, &cellsArray));
22469566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd));
2247fa84ea4cSLawrence Mitchell     for (i = pStart; i < pEnd; i++) {
22489566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(patch->cellCounts, i, &ncell));
22499566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(patch->cellCounts, i, &offset));
2250fa84ea4cSLawrence Mitchell       if (ncell <= 0) continue;
225148a46eb9SPierre Jolivet       for (j = 0; j < ncell; j++) PetscCall(PetscHSetIAdd(cells, cellsArray[offset + j]));
2252fa84ea4cSLawrence Mitchell     }
22539566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(patch->cells, &cellsArray));
22549566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(cells, &ncell));
22559566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(ncell, &allCells));
22569566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
22579566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(cEnd - cStart, &patch->precomputedTensorLocations));
2258fa84ea4cSLawrence Mitchell     i = 0;
2259fa84ea4cSLawrence Mitchell     PetscHashIterBegin(cells, hi);
2260fa84ea4cSLawrence Mitchell     while (!PetscHashIterAtEnd(cells, hi)) {
2261fe988be2SFlorian Wechsung       PetscHashIterGetKey(cells, hi, allCells[i]);
2262fe988be2SFlorian Wechsung       patch->precomputedTensorLocations[allCells[i]] = i;
2263fa84ea4cSLawrence Mitchell       PetscHashIterNext(cells, hi);
2264fe988be2SFlorian Wechsung       i++;
2265fa84ea4cSLawrence Mitchell     }
22669566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&cells));
22679566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, allCells, PETSC_OWN_POINTER, &patch->allCells));
2268fa84ea4cSLawrence Mitchell   }
22699566063dSJacob Faibussowitsch   PetscCall(ISGetSize(patch->allCells, &ncell));
2270fa84ea4cSLawrence Mitchell   if (!patch->cellMats) {
22719566063dSJacob Faibussowitsch     PetscCall(VecCreateSeq(PETSC_COMM_SELF, ncell * ndof * ndof, &patch->cellMats));
22729566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(patch->cellMats, ndof));
2273fa84ea4cSLawrence Mitchell   }
22749566063dSJacob Faibussowitsch   PetscCall(VecSet(patch->cellMats, 0));
2275fa84ea4cSLawrence Mitchell 
2276d0609cedSBarry Smith   PetscCall(MatCreateShell(PETSC_COMM_SELF, ncell * ndof, ncell * ndof, ncell * ndof, ncell * ndof, (void *)patch->cellMats, &vecMat));
22779566063dSJacob Faibussowitsch   PetscCall(MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void (*)(void)) & MatSetValues_PCPatch_Private));
22789566063dSJacob Faibussowitsch   PetscCall(ISGetSize(patch->allCells, &ncell));
22799566063dSJacob Faibussowitsch   PetscCall(ISCreateStride(PETSC_COMM_SELF, ndof * ncell, 0, 1, &dofMap));
22809566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(dofMap, &dofMapArray));
22819566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->allCells, &cellsArray));
22829566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray, PETSC_USE_POINTER, &cellIS));
2283fa84ea4cSLawrence Mitchell   /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */
2284792fecdfSBarry Smith   PetscCallBack("PCPatch callback", patch->usercomputeop(pc, -1, NULL, vecMat, cellIS, ndof * ncell, dofMapArray, NULL, patch->usercomputeopctx));
22859566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&cellIS));
22869566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&vecMat));
22879566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->allCells, &cellsArray));
22889566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(dofMap, &dofMapArray));
22899566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&dofMap));
2290f98464cbSLawrence Mitchell 
2291f98464cbSLawrence Mitchell   if (patch->usercomputeopintfacet) {
2292f98464cbSLawrence Mitchell     PetscInt        nIntFacets;
2293f98464cbSLawrence Mitchell     IS              intFacetsIS;
2294f98464cbSLawrence Mitchell     const PetscInt *intFacetsArray = NULL;
2295f98464cbSLawrence Mitchell     if (!patch->allIntFacets) {
2296f98464cbSLawrence Mitchell       PetscHSetI    facets;
2297f98464cbSLawrence Mitchell       PetscHashIter hi;
2298f98464cbSLawrence Mitchell       PetscInt      pStart, pEnd, fStart, fEnd;
2299f98464cbSLawrence Mitchell       PetscInt     *allIntFacets = NULL;
23009566063dSJacob Faibussowitsch       PetscCall(PetscHSetICreate(&facets));
23019566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(patch->intFacets, &intFacetsArray));
23029566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(patch->intFacetCounts, &pStart, &pEnd));
23039566063dSJacob Faibussowitsch       PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
2304f98464cbSLawrence Mitchell       for (i = pStart; i < pEnd; i++) {
23059566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->intFacetCounts, i, &nIntFacets));
23069566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->intFacetCounts, i, &offset));
2307f98464cbSLawrence Mitchell         if (nIntFacets <= 0) continue;
230848a46eb9SPierre Jolivet         for (j = 0; j < nIntFacets; j++) PetscCall(PetscHSetIAdd(facets, intFacetsArray[offset + j]));
2309f98464cbSLawrence Mitchell       }
23109566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(patch->intFacets, &intFacetsArray));
23119566063dSJacob Faibussowitsch       PetscCall(PetscHSetIGetSize(facets, &nIntFacets));
23129566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nIntFacets, &allIntFacets));
23139566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fEnd - fStart, &patch->precomputedIntFacetTensorLocations));
2314f98464cbSLawrence Mitchell       i = 0;
2315f98464cbSLawrence Mitchell       PetscHashIterBegin(facets, hi);
2316f98464cbSLawrence Mitchell       while (!PetscHashIterAtEnd(facets, hi)) {
2317f98464cbSLawrence Mitchell         PetscHashIterGetKey(facets, hi, allIntFacets[i]);
2318de2d1767SPatrick Farrell         patch->precomputedIntFacetTensorLocations[allIntFacets[i] - fStart] = i;
2319f98464cbSLawrence Mitchell         PetscHashIterNext(facets, hi);
2320f98464cbSLawrence Mitchell         i++;
2321f98464cbSLawrence Mitchell       }
23229566063dSJacob Faibussowitsch       PetscCall(PetscHSetIDestroy(&facets));
23239566063dSJacob Faibussowitsch       PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, allIntFacets, PETSC_OWN_POINTER, &patch->allIntFacets));
2324f98464cbSLawrence Mitchell     }
23259566063dSJacob Faibussowitsch     PetscCall(ISGetSize(patch->allIntFacets, &nIntFacets));
2326f98464cbSLawrence Mitchell     if (!patch->intFacetMats) {
23279566063dSJacob Faibussowitsch       PetscCall(VecCreateSeq(PETSC_COMM_SELF, nIntFacets * ndof * ndof * 4, &patch->intFacetMats));
23289566063dSJacob Faibussowitsch       PetscCall(VecSetBlockSize(patch->intFacetMats, ndof * 2));
2329f98464cbSLawrence Mitchell     }
23309566063dSJacob Faibussowitsch     PetscCall(VecSet(patch->intFacetMats, 0));
2331f98464cbSLawrence Mitchell 
2332d0609cedSBarry Smith     PetscCall(MatCreateShell(PETSC_COMM_SELF, nIntFacets * ndof * 2, nIntFacets * ndof * 2, nIntFacets * ndof * 2, nIntFacets * ndof * 2, (void *)patch->intFacetMats, &vecMat));
23339566063dSJacob Faibussowitsch     PetscCall(MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void (*)(void)) & MatSetValues_PCPatch_Private));
23349566063dSJacob Faibussowitsch     PetscCall(ISCreateStride(PETSC_COMM_SELF, 2 * ndof * nIntFacets, 0, 1, &dofMap));
23359566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(dofMap, &dofMapArray));
23369566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->allIntFacets, &intFacetsArray));
23379566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, intFacetsArray, PETSC_USE_POINTER, &intFacetsIS));
2338f98464cbSLawrence Mitchell     /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */
2339792fecdfSBarry Smith     PetscCallBack("PCPatch callback (interior facets)", patch->usercomputeopintfacet(pc, -1, NULL, vecMat, intFacetsIS, 2 * ndof * nIntFacets, dofMapArray, NULL, patch->usercomputeopintfacetctx));
23409566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&intFacetsIS));
23419566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&vecMat));
23429566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(patch->allIntFacets, &intFacetsArray));
23439566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(dofMap, &dofMapArray));
23449566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dofMap));
2345f98464cbSLawrence Mitchell   }
23469566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm));
23479566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
2348fa84ea4cSLawrence Mitchell 
23493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2350fa84ea4cSLawrence Mitchell }
2351fa84ea4cSLawrence Mitchell 
2352d71ae5a4SJacob Faibussowitsch PetscErrorCode PCPatch_ScatterLocal_Private(PC pc, PetscInt p, Vec x, Vec y, InsertMode mode, ScatterMode scat, PatchScatterType scattertype)
2353d71ae5a4SJacob Faibussowitsch {
23544bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch     = (PC_PATCH *)pc->data;
23554bbf5ea8SMatthew G. Knepley   const PetscScalar *xArray    = NULL;
23564bbf5ea8SMatthew G. Knepley   PetscScalar       *yArray    = NULL;
23574bbf5ea8SMatthew G. Knepley   const PetscInt    *gtolArray = NULL;
23584bbf5ea8SMatthew G. Knepley   PetscInt           dof, offset, lidx;
23594bbf5ea8SMatthew G. Knepley 
23604bbf5ea8SMatthew G. Knepley   PetscFunctionBeginHot;
23619566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(x, &xArray));
23629566063dSJacob Faibussowitsch   PetscCall(VecGetArray(y, &yArray));
23630904074fSPatrick Farrell   if (scattertype == SCATTER_WITHARTIFICIAL) {
23649566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof));
23659566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offset));
23669566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->gtolWithArtificial, &gtolArray));
23670904074fSPatrick Farrell   } else if (scattertype == SCATTER_WITHALL) {
23689566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(patch->gtolCountsWithAll, p, &dof));
23699566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offset));
23709566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->gtolWithAll, &gtolArray));
2371c2e6f3c0SFlorian Wechsung   } else {
23729566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &dof));
23739566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset));
23749566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->gtol, &gtolArray));
2375c2e6f3c0SFlorian Wechsung   }
23762472a847SBarry Smith   PetscCheck(mode != INSERT_VALUES || scat == SCATTER_FORWARD, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't insert if not scattering forward");
23772472a847SBarry Smith   PetscCheck(mode != ADD_VALUES || scat == SCATTER_REVERSE, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't add if not scattering reverse");
23784bbf5ea8SMatthew G. Knepley   for (lidx = 0; lidx < dof; ++lidx) {
23794bbf5ea8SMatthew G. Knepley     const PetscInt gidx = gtolArray[offset + lidx];
23804bbf5ea8SMatthew G. Knepley 
23814bbf5ea8SMatthew G. Knepley     if (mode == INSERT_VALUES) yArray[lidx] = xArray[gidx]; /* Forward */
23824bbf5ea8SMatthew G. Knepley     else yArray[gidx] += xArray[lidx];                      /* Reverse */
23834bbf5ea8SMatthew G. Knepley   }
23840904074fSPatrick Farrell   if (scattertype == SCATTER_WITHARTIFICIAL) {
23859566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(patch->gtolWithArtificial, &gtolArray));
23860904074fSPatrick Farrell   } else if (scattertype == SCATTER_WITHALL) {
23879566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(patch->gtolWithAll, &gtolArray));
2388c2e6f3c0SFlorian Wechsung   } else {
23899566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(patch->gtol, &gtolArray));
2390c2e6f3c0SFlorian Wechsung   }
23919566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(x, &xArray));
23929566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(y, &yArray));
23933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
23944bbf5ea8SMatthew G. Knepley }
23954bbf5ea8SMatthew G. Knepley 
2396d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_PATCH_Linear(PC pc)
2397d71ae5a4SJacob Faibussowitsch {
2398dadc69c5SMatthew G. Knepley   PC_PATCH   *patch = (PC_PATCH *)pc->data;
2399dadc69c5SMatthew G. Knepley   const char *prefix;
2400dadc69c5SMatthew G. Knepley   PetscInt    i;
2401dadc69c5SMatthew G. Knepley 
2402dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2403dadc69c5SMatthew G. Knepley   if (!pc->setupcalled) {
24047827d75bSBarry Smith     PetscCheck(patch->save_operators || !patch->denseinverse, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Can't have dense inverse without save operators");
2405c73d2cf6SLawrence Mitchell     if (!patch->denseinverse) {
24069566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(patch->npatch, &patch->solver));
24079566063dSJacob Faibussowitsch       PetscCall(PCGetOptionsPrefix(pc, &prefix));
2408dadc69c5SMatthew G. Knepley       for (i = 0; i < patch->npatch; ++i) {
2409dadc69c5SMatthew G. Knepley         KSP ksp;
2410dadc69c5SMatthew G. Knepley         PC  subpc;
2411dadc69c5SMatthew G. Knepley 
24129566063dSJacob Faibussowitsch         PetscCall(KSPCreate(PETSC_COMM_SELF, &ksp));
24139566063dSJacob Faibussowitsch         PetscCall(KSPSetErrorIfNotConverged(ksp, pc->erroriffailure));
24149566063dSJacob Faibussowitsch         PetscCall(KSPSetOptionsPrefix(ksp, prefix));
24159566063dSJacob Faibussowitsch         PetscCall(KSPAppendOptionsPrefix(ksp, "sub_"));
24169566063dSJacob Faibussowitsch         PetscCall(PetscObjectIncrementTabLevel((PetscObject)ksp, (PetscObject)pc, 1));
24179566063dSJacob Faibussowitsch         PetscCall(KSPGetPC(ksp, &subpc));
24189566063dSJacob Faibussowitsch         PetscCall(PetscObjectIncrementTabLevel((PetscObject)subpc, (PetscObject)pc, 1));
2419dadc69c5SMatthew G. Knepley         patch->solver[i] = (PetscObject)ksp;
2420dadc69c5SMatthew G. Knepley       }
2421dadc69c5SMatthew G. Knepley     }
2422c73d2cf6SLawrence Mitchell   }
2423dadc69c5SMatthew G. Knepley   if (patch->save_operators) {
24241baa6e33SBarry Smith     if (patch->precomputeElementTensors) PetscCall(PCPatchPrecomputePatchTensors_Private(pc));
2425dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {
24269566063dSJacob Faibussowitsch       PetscCall(PCPatchComputeOperator_Internal(pc, NULL, patch->mat[i], i, PETSC_FALSE));
2427c73d2cf6SLawrence Mitchell       if (!patch->denseinverse) {
24289566063dSJacob Faibussowitsch         PetscCall(KSPSetOperators((KSP)patch->solver[i], patch->mat[i], patch->mat[i]));
24299d4fc724SLawrence Mitchell       } else if (patch->mat[i] && !patch->densesolve) {
24309d4fc724SLawrence Mitchell         /* Setup matmult callback */
24319566063dSJacob Faibussowitsch         PetscCall(MatGetOperation(patch->mat[i], MATOP_MULT, (void (**)(void)) & patch->densesolve));
2432dadc69c5SMatthew G. Knepley       }
2433dadc69c5SMatthew G. Knepley     }
2434c73d2cf6SLawrence Mitchell   }
243534d8b122SPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
243634d8b122SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {
24371202d238SPatrick Farrell       /* Instead of padding patch->patchUpdate with zeros to get */
24381202d238SPatrick Farrell       /* patch->patchUpdateWithArtificial and then multiplying with the matrix, */
243934d8b122SPatrick Farrell       /* just get rid of the columns that correspond to the dofs with */
244034d8b122SPatrick Farrell       /* artificial bcs. That's of course fairly inefficient, hopefully we */
244134d8b122SPatrick Farrell       /* can just assemble the rectangular matrix in the first place. */
244234d8b122SPatrick Farrell       Mat      matSquare;
244334d8b122SPatrick Farrell       IS       rowis;
244434d8b122SPatrick Farrell       PetscInt dof;
244534d8b122SPatrick Farrell 
24469566063dSJacob Faibussowitsch       PetscCall(MatGetSize(patch->mat[i], &dof, NULL));
244734d8b122SPatrick Farrell       if (dof == 0) {
244834d8b122SPatrick Farrell         patch->matWithArtificial[i] = NULL;
244934d8b122SPatrick Farrell         continue;
245034d8b122SPatrick Farrell       }
245134d8b122SPatrick Farrell 
24529566063dSJacob Faibussowitsch       PetscCall(PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE));
24539566063dSJacob Faibussowitsch       PetscCall(PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE));
245434d8b122SPatrick Farrell 
24559566063dSJacob Faibussowitsch       PetscCall(MatGetSize(matSquare, &dof, NULL));
24569566063dSJacob Faibussowitsch       PetscCall(ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis));
245734d8b122SPatrick Farrell       if (pc->setupcalled) {
24589566063dSJacob Faibussowitsch         PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_REUSE_MATRIX, &patch->matWithArtificial[i]));
245934d8b122SPatrick Farrell       } else {
24609566063dSJacob Faibussowitsch         PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &patch->matWithArtificial[i]));
246134d8b122SPatrick Farrell       }
24629566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&rowis));
24639566063dSJacob Faibussowitsch       PetscCall(MatDestroy(&matSquare));
246434d8b122SPatrick Farrell     }
246534d8b122SPatrick Farrell   }
24663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2467dadc69c5SMatthew G. Knepley }
2468dadc69c5SMatthew G. Knepley 
2469d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_PATCH(PC pc)
2470d71ae5a4SJacob Faibussowitsch {
24714bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
2472557beb66SLawrence Mitchell   PetscInt  i;
247339fd2e8aSPatrick Farrell   PetscBool isNonlinear;
24749d4fc724SLawrence Mitchell   PetscInt  maxDof = -1, maxDofWithArtificial = -1;
24754bbf5ea8SMatthew G. Knepley 
24764bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
24774bbf5ea8SMatthew G. Knepley   if (!pc->setupcalled) {
24784bbf5ea8SMatthew G. Knepley     PetscInt pStart, pEnd, p;
24794bbf5ea8SMatthew G. Knepley     PetscInt localSize;
24804bbf5ea8SMatthew G. Knepley 
24819566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(PC_Patch_CreatePatches, pc, 0, 0, 0));
24824bbf5ea8SMatthew G. Knepley 
2483debbdec3SPatrick Farrell     isNonlinear = patch->isNonlinear;
24845f824522SMatthew G. Knepley     if (!patch->nsubspaces) {
2485b6bb21d1SLawrence Mitchell       DM           dm, plex;
24865f824522SMatthew G. Knepley       PetscSection s;
2487bd026e97SJed Brown       PetscInt     cStart, cEnd, c, Nf, f, numGlobalBcs = 0, *globalBcs, *Nb, **cellDofs;
24885f824522SMatthew G. Knepley 
24899566063dSJacob Faibussowitsch       PetscCall(PCGetDM(pc, &dm));
249028b400f6SJacob Faibussowitsch       PetscCheck(dm, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Must set DM for PCPATCH or call PCPatchSetDiscretisationInfo()");
24919566063dSJacob Faibussowitsch       PetscCall(DMConvert(dm, DMPLEX, &plex));
2492b6bb21d1SLawrence Mitchell       dm = plex;
24939566063dSJacob Faibussowitsch       PetscCall(DMGetLocalSection(dm, &s));
24949566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetNumFields(s, &Nf));
24959566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
24965f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
24975f824522SMatthew G. Knepley         PetscInt cdof;
24989566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
24995f824522SMatthew G. Knepley         numGlobalBcs += cdof;
25005f824522SMatthew G. Knepley       }
25019566063dSJacob Faibussowitsch       PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
25029566063dSJacob Faibussowitsch       PetscCall(PetscMalloc3(Nf, &Nb, Nf, &cellDofs, numGlobalBcs, &globalBcs));
25035f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
25045f824522SMatthew G. Knepley         PetscFE        fe;
25055f824522SMatthew G. Knepley         PetscDualSpace sp;
25065f824522SMatthew G. Knepley         PetscInt       cdoff = 0;
25075f824522SMatthew G. Knepley 
25089566063dSJacob Faibussowitsch         PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
25099566063dSJacob Faibussowitsch         /* PetscCall(PetscFEGetNumComponents(fe, &Nc[f])); */
25109566063dSJacob Faibussowitsch         PetscCall(PetscFEGetDualSpace(fe, &sp));
25119566063dSJacob Faibussowitsch         PetscCall(PetscDualSpaceGetDimension(sp, &Nb[f]));
25125f824522SMatthew G. Knepley 
25139566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1((cEnd - cStart) * Nb[f], &cellDofs[f]));
25145f824522SMatthew G. Knepley         for (c = cStart; c < cEnd; ++c) {
25155f824522SMatthew G. Knepley           PetscInt *closure = NULL;
25165f824522SMatthew G. Knepley           PetscInt  clSize  = 0, cl;
25175f824522SMatthew G. Knepley 
25189566063dSJacob Faibussowitsch           PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure));
25195f824522SMatthew G. Knepley           for (cl = 0; cl < clSize * 2; cl += 2) {
25205f824522SMatthew G. Knepley             const PetscInt p = closure[cl];
25215f824522SMatthew G. Knepley             PetscInt       fdof, d, foff;
25225f824522SMatthew G. Knepley 
25239566063dSJacob Faibussowitsch             PetscCall(PetscSectionGetFieldDof(s, p, f, &fdof));
25249566063dSJacob Faibussowitsch             PetscCall(PetscSectionGetFieldOffset(s, p, f, &foff));
25255f824522SMatthew G. Knepley             for (d = 0; d < fdof; ++d, ++cdoff) cellDofs[f][cdoff] = foff + d;
25265f824522SMatthew G. Knepley           }
25279566063dSJacob Faibussowitsch           PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure));
25285f824522SMatthew G. Knepley         }
252963a3b9bcSJacob 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]);
25305f824522SMatthew G. Knepley       }
25315f824522SMatthew G. Knepley       numGlobalBcs = 0;
25325f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
25335f824522SMatthew G. Knepley         const PetscInt *ind;
25345f824522SMatthew G. Knepley         PetscInt        off, cdof, d;
25355f824522SMatthew G. Knepley 
25369566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
25379566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
25389566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(s, p, &ind));
25395f824522SMatthew G. Knepley         for (d = 0; d < cdof; ++d) globalBcs[numGlobalBcs++] = off + ind[d];
25405f824522SMatthew G. Knepley       }
25415f824522SMatthew G. Knepley 
25429566063dSJacob Faibussowitsch       PetscCall(PCPatchSetDiscretisationInfoCombined(pc, dm, Nb, (const PetscInt **)cellDofs, numGlobalBcs, globalBcs, numGlobalBcs, globalBcs));
254348a46eb9SPierre Jolivet       for (f = 0; f < Nf; ++f) PetscCall(PetscFree(cellDofs[f]));
25449566063dSJacob Faibussowitsch       PetscCall(PetscFree3(Nb, cellDofs, globalBcs));
25459566063dSJacob Faibussowitsch       PetscCall(PCPatchSetComputeFunction(pc, PCPatchComputeFunction_DMPlex_Private, NULL));
25469566063dSJacob Faibussowitsch       PetscCall(PCPatchSetComputeOperator(pc, PCPatchComputeOperator_DMPlex_Private, NULL));
25479566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dm));
25485f824522SMatthew G. Knepley     }
25495f824522SMatthew G. Knepley 
25504bbf5ea8SMatthew G. Knepley     localSize = patch->subspaceOffsets[patch->nsubspaces];
25519566063dSJacob Faibussowitsch     PetscCall(VecCreateSeq(PETSC_COMM_SELF, localSize, &patch->localRHS));
25529566063dSJacob Faibussowitsch     PetscCall(VecSetUp(patch->localRHS));
25539566063dSJacob Faibussowitsch     PetscCall(VecDuplicate(patch->localRHS, &patch->localUpdate));
25549566063dSJacob Faibussowitsch     PetscCall(PCPatchCreateCellPatches(pc));
25559566063dSJacob Faibussowitsch     PetscCall(PCPatchCreateCellPatchDiscretisationInfo(pc));
25564bbf5ea8SMatthew G. Knepley 
25574bbf5ea8SMatthew G. Knepley     /* OK, now build the work vectors */
25589566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, &pEnd));
2559c2e6f3c0SFlorian Wechsung 
256048a46eb9SPierre Jolivet     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) PetscCall(PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithArtificial));
256148a46eb9SPierre Jolivet     if (isNonlinear) PetscCall(PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithAll));
25624bbf5ea8SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
25634bbf5ea8SMatthew G. Knepley       PetscInt dof;
25644bbf5ea8SMatthew G. Knepley 
25659566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &dof));
25662f613bf5SBarry Smith       maxDof = PetscMax(maxDof, dof);
25670904074fSPatrick Farrell       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
25683bb0e8f7SKarl Rupp         const PetscInt *gtolArray, *gtolArrayWithArtificial = NULL;
25693bb0e8f7SKarl Rupp         PetscInt        numPatchDofs, offset;
25703bb0e8f7SKarl Rupp         PetscInt        numPatchDofsWithArtificial, offsetWithArtificial;
25713bb0e8f7SKarl Rupp         PetscInt        dofWithoutArtificialCounter = 0;
25723bb0e8f7SKarl Rupp         PetscInt       *patchWithoutArtificialToWithArtificialArray;
25733bb0e8f7SKarl Rupp 
25749566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof));
25759d4fc724SLawrence Mitchell         maxDofWithArtificial = PetscMax(maxDofWithArtificial, dof);
2576c2e6f3c0SFlorian Wechsung 
2577e047a90bSFlorian Wechsung         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
2578e047a90bSFlorian Wechsung         /* the index in the patch with all dofs */
25799566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->gtol, &gtolArray));
258063deea8eSPatrick Farrell 
25819566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs));
258247aca4a6SPatrick Farrell         if (numPatchDofs == 0) {
258347aca4a6SPatrick Farrell           patch->dofMappingWithoutToWithArtificial[p - pStart] = NULL;
258447aca4a6SPatrick Farrell           continue;
258547aca4a6SPatrick Farrell         }
258663deea8eSPatrick Farrell 
25879566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset));
25889566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial));
25899566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &numPatchDofsWithArtificial));
25909566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offsetWithArtificial));
2591c2e6f3c0SFlorian Wechsung 
25929566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(numPatchDofs, &patchWithoutArtificialToWithArtificialArray));
2593b0c21b6aSKarl Rupp         for (i = 0; i < numPatchDofsWithArtificial; i++) {
2594e047a90bSFlorian Wechsung           if (gtolArrayWithArtificial[i + offsetWithArtificial] == gtolArray[offset + dofWithoutArtificialCounter]) {
2595c2e6f3c0SFlorian Wechsung             patchWithoutArtificialToWithArtificialArray[dofWithoutArtificialCounter] = i;
2596c2e6f3c0SFlorian Wechsung             dofWithoutArtificialCounter++;
25979371c9d4SSatish Balay             if (dofWithoutArtificialCounter == numPatchDofs) break;
2598c2e6f3c0SFlorian Wechsung           }
2599c2e6f3c0SFlorian Wechsung         }
26009566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutArtificialToWithArtificialArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithArtificial[p - pStart]));
26019566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(patch->gtol, &gtolArray));
26029566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial));
2603c2e6f3c0SFlorian Wechsung       }
26040997d788SPatrick Farrell     }
26050997d788SPatrick Farrell     for (p = pStart; p < pEnd; ++p) {
26060904074fSPatrick Farrell       if (isNonlinear) {
26070904074fSPatrick Farrell         const PetscInt *gtolArray, *gtolArrayWithAll = NULL;
26080904074fSPatrick Farrell         PetscInt        numPatchDofs, offset;
26090904074fSPatrick Farrell         PetscInt        numPatchDofsWithAll, offsetWithAll;
26100904074fSPatrick Farrell         PetscInt        dofWithoutAllCounter = 0;
26110904074fSPatrick Farrell         PetscInt       *patchWithoutAllToWithAllArray;
26120904074fSPatrick Farrell 
26130904074fSPatrick Farrell         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
26140904074fSPatrick Farrell         /* the index in the patch with all dofs */
26159566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->gtol, &gtolArray));
26160904074fSPatrick Farrell 
26179566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs));
261847aca4a6SPatrick Farrell         if (numPatchDofs == 0) {
2619b88cb22dSPatrick Farrell           patch->dofMappingWithoutToWithAll[p - pStart] = NULL;
262047aca4a6SPatrick Farrell           continue;
262147aca4a6SPatrick Farrell         }
26220904074fSPatrick Farrell 
26239566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->gtolCounts, p, &offset));
26249566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(patch->gtolWithAll, &gtolArrayWithAll));
26259566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(patch->gtolCountsWithAll, p, &numPatchDofsWithAll));
26269566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offsetWithAll));
26270904074fSPatrick Farrell 
26289566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(numPatchDofs, &patchWithoutAllToWithAllArray));
26290904074fSPatrick Farrell 
26300904074fSPatrick Farrell         for (i = 0; i < numPatchDofsWithAll; i++) {
26310904074fSPatrick Farrell           if (gtolArrayWithAll[i + offsetWithAll] == gtolArray[offset + dofWithoutAllCounter]) {
26320904074fSPatrick Farrell             patchWithoutAllToWithAllArray[dofWithoutAllCounter] = i;
26330904074fSPatrick Farrell             dofWithoutAllCounter++;
26349371c9d4SSatish Balay             if (dofWithoutAllCounter == numPatchDofs) break;
26350904074fSPatrick Farrell           }
26360904074fSPatrick Farrell         }
26379566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutAllToWithAllArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithAll[p - pStart]));
26389566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(patch->gtol, &gtolArray));
26399566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(patch->gtolWithAll, &gtolArrayWithAll));
26400904074fSPatrick Farrell       }
26414bbf5ea8SMatthew G. Knepley     }
264260dd46caSLawrence Mitchell     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
26439566063dSJacob Faibussowitsch       PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDofWithArtificial, &patch->patchRHSWithArtificial));
26449566063dSJacob Faibussowitsch       PetscCall(VecSetUp(patch->patchRHSWithArtificial));
264560dd46caSLawrence Mitchell     }
26469566063dSJacob Faibussowitsch     PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDof, &patch->patchRHS));
26479566063dSJacob Faibussowitsch     PetscCall(VecSetUp(patch->patchRHS));
26489566063dSJacob Faibussowitsch     PetscCall(VecCreateSeq(PETSC_COMM_SELF, maxDof, &patch->patchUpdate));
26499566063dSJacob Faibussowitsch     PetscCall(VecSetUp(patch->patchUpdate));
26504bbf5ea8SMatthew G. Knepley     if (patch->save_operators) {
26519566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(patch->npatch, &patch->mat));
265248a46eb9SPierre Jolivet       for (i = 0; i < patch->npatch; ++i) PetscCall(PCPatchCreateMatrix_Private(pc, i, &patch->mat[i], PETSC_FALSE));
26534bbf5ea8SMatthew G. Knepley     }
26549566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(PC_Patch_CreatePatches, pc, 0, 0, 0));
26554bbf5ea8SMatthew G. Knepley 
26564bbf5ea8SMatthew G. Knepley     /* If desired, calculate weights for dof multiplicity */
26574bbf5ea8SMatthew G. Knepley     if (patch->partition_of_unity) {
26583bb0e8f7SKarl Rupp       PetscScalar *input  = NULL;
26593bb0e8f7SKarl Rupp       PetscScalar *output = NULL;
26603bb0e8f7SKarl Rupp       Vec          global;
26613bb0e8f7SKarl Rupp 
26629566063dSJacob Faibussowitsch       PetscCall(VecDuplicate(patch->localRHS, &patch->dof_weights));
266361c4b389SFlorian Wechsung       if (patch->local_composition_type == PC_COMPOSITE_ADDITIVE) {
26644bbf5ea8SMatthew G. Knepley         for (i = 0; i < patch->npatch; ++i) {
26654bbf5ea8SMatthew G. Knepley           PetscInt dof;
26664bbf5ea8SMatthew G. Knepley 
26679566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetDof(patch->gtolCounts, i + pStart, &dof));
26684bbf5ea8SMatthew G. Knepley           if (dof <= 0) continue;
26699566063dSJacob Faibussowitsch           PetscCall(VecSet(patch->patchRHS, 1.0));
26709566063dSJacob Faibussowitsch           PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHS, patch->dof_weights, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR));
26714bbf5ea8SMatthew G. Knepley         }
2672c2e6f3c0SFlorian Wechsung       } else {
2673e047a90bSFlorian Wechsung         /* multiplicative is actually only locally multiplicative and globally additive. need the pou where the mesh decomposition overlaps */
26749566063dSJacob Faibussowitsch         PetscCall(VecSet(patch->dof_weights, 1.0));
26754bbf5ea8SMatthew G. Knepley       }
2676d132cafaSFlorian Wechsung 
26773ba16761SJacob Faibussowitsch       PetscCall(VecDuplicate(patch->dof_weights, &global));
26783ba16761SJacob Faibussowitsch       PetscCall(VecSet(global, 0.));
2679d132cafaSFlorian Wechsung 
26809566063dSJacob Faibussowitsch       PetscCall(VecGetArray(patch->dof_weights, &input));
26819566063dSJacob Faibussowitsch       PetscCall(VecGetArray(global, &output));
26829566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceBegin(patch->sectionSF, MPIU_SCALAR, input, output, MPI_SUM));
26839566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceEnd(patch->sectionSF, MPIU_SCALAR, input, output, MPI_SUM));
26849566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(patch->dof_weights, &input));
26859566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(global, &output));
2686d132cafaSFlorian Wechsung 
26879566063dSJacob Faibussowitsch       PetscCall(VecReciprocal(global));
2688d132cafaSFlorian Wechsung 
26899566063dSJacob Faibussowitsch       PetscCall(VecGetArray(patch->dof_weights, &output));
26909566063dSJacob Faibussowitsch       PetscCall(VecGetArray(global, &input));
26919566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(patch->sectionSF, MPIU_SCALAR, input, output, MPI_REPLACE));
26929566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(patch->sectionSF, MPIU_SCALAR, input, output, MPI_REPLACE));
26939566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(patch->dof_weights, &output));
26949566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(global, &input));
26959566063dSJacob Faibussowitsch       PetscCall(VecDestroy(&global));
26964bbf5ea8SMatthew G. Knepley     }
269748a46eb9SPierre Jolivet     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE && patch->save_operators && !patch->isNonlinear) PetscCall(PetscMalloc1(patch->npatch, &patch->matWithArtificial));
26984bbf5ea8SMatthew G. Knepley   }
26999566063dSJacob Faibussowitsch   PetscCall((*patch->setupsolver)(pc));
27003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27014bbf5ea8SMatthew G. Knepley }
2702dadc69c5SMatthew G. Knepley 
2703d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_PATCH_Linear(PC pc, PetscInt i, Vec x, Vec y)
2704d71ae5a4SJacob Faibussowitsch {
2705dadc69c5SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
2706c73d2cf6SLawrence Mitchell   KSP       ksp;
27079d4fc724SLawrence Mitchell   Mat       op;
27089d4fc724SLawrence Mitchell   PetscInt  m, n;
2709dadc69c5SMatthew G. Knepley 
2710dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2711c73d2cf6SLawrence Mitchell   if (patch->denseinverse) {
27129566063dSJacob Faibussowitsch     PetscCall((*patch->densesolve)(patch->mat[i], x, y));
27133ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2714c73d2cf6SLawrence Mitchell   }
2715c73d2cf6SLawrence Mitchell   ksp = (KSP)patch->solver[i];
2716dadc69c5SMatthew G. Knepley   if (!patch->save_operators) {
2717dadc69c5SMatthew G. Knepley     Mat mat;
2718dadc69c5SMatthew G. Knepley 
27199566063dSJacob Faibussowitsch     PetscCall(PCPatchCreateMatrix_Private(pc, i, &mat, PETSC_FALSE));
2720dadc69c5SMatthew G. Knepley     /* Populate operator here. */
27219566063dSJacob Faibussowitsch     PetscCall(PCPatchComputeOperator_Internal(pc, NULL, mat, i, PETSC_FALSE));
27229566063dSJacob Faibussowitsch     PetscCall(KSPSetOperators(ksp, mat, mat));
2723dadc69c5SMatthew G. Knepley     /* Drop reference so the KSPSetOperators below will blow it away. */
27249566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&mat));
2725dadc69c5SMatthew G. Knepley   }
27269566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0));
272748a46eb9SPierre Jolivet   if (!ksp->setfromoptionscalled) PetscCall(KSPSetFromOptions(ksp));
27289d4fc724SLawrence Mitchell   /* Disgusting trick to reuse work vectors */
27299566063dSJacob Faibussowitsch   PetscCall(KSPGetOperators(ksp, &op, NULL));
27309566063dSJacob Faibussowitsch   PetscCall(MatGetLocalSize(op, &m, &n));
27319d4fc724SLawrence Mitchell   x->map->n = m;
27329d4fc724SLawrence Mitchell   y->map->n = n;
27339d4fc724SLawrence Mitchell   x->map->N = m;
27349d4fc724SLawrence Mitchell   y->map->N = n;
27359566063dSJacob Faibussowitsch   PetscCall(KSPSolve(ksp, x, y));
27369566063dSJacob Faibussowitsch   PetscCall(KSPCheckSolve(ksp, pc, y));
27379566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0));
2738dadc69c5SMatthew G. Knepley   if (!patch->save_operators) {
2739dadc69c5SMatthew G. Knepley     PC pc;
27409566063dSJacob Faibussowitsch     PetscCall(KSPSetOperators(ksp, NULL, NULL));
27419566063dSJacob Faibussowitsch     PetscCall(KSPGetPC(ksp, &pc));
2742dadc69c5SMatthew G. Knepley     /* Destroy PC context too, otherwise the factored matrix hangs around. */
27439566063dSJacob Faibussowitsch     PetscCall(PCReset(pc));
27444bbf5ea8SMatthew G. Knepley   }
27453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27464bbf5ea8SMatthew G. Knepley }
27474bbf5ea8SMatthew G. Knepley 
2748d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCUpdateMultiplicative_PATCH_Linear(PC pc, PetscInt i, PetscInt pStart)
2749d71ae5a4SJacob Faibussowitsch {
27506c9c532dSPatrick Farrell   PC_PATCH *patch = (PC_PATCH *)pc->data;
27516c9c532dSPatrick Farrell   Mat       multMat;
27529d4fc724SLawrence Mitchell   PetscInt  n, m;
27536c9c532dSPatrick Farrell 
27544d04e9f1SPatrick Farrell   PetscFunctionBegin;
27554d04e9f1SPatrick Farrell 
27566c9c532dSPatrick Farrell   if (patch->save_operators) {
27576c9c532dSPatrick Farrell     multMat = patch->matWithArtificial[i];
27586c9c532dSPatrick Farrell   } else {
27596c9c532dSPatrick Farrell     /*Very inefficient, hopefully we can just assemble the rectangular matrix in the first place.*/
27606c9c532dSPatrick Farrell     Mat      matSquare;
27616c9c532dSPatrick Farrell     PetscInt dof;
27626c9c532dSPatrick Farrell     IS       rowis;
27639566063dSJacob Faibussowitsch     PetscCall(PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE));
27649566063dSJacob Faibussowitsch     PetscCall(PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE));
27659566063dSJacob Faibussowitsch     PetscCall(MatGetSize(matSquare, &dof, NULL));
27669566063dSJacob Faibussowitsch     PetscCall(ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis));
27679566063dSJacob Faibussowitsch     PetscCall(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &multMat));
27689566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&matSquare));
27699566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&rowis));
27706c9c532dSPatrick Farrell   }
27719d4fc724SLawrence Mitchell   /* Disgusting trick to reuse work vectors */
27729566063dSJacob Faibussowitsch   PetscCall(MatGetLocalSize(multMat, &m, &n));
27739d4fc724SLawrence Mitchell   patch->patchUpdate->map->n            = n;
27749d4fc724SLawrence Mitchell   patch->patchRHSWithArtificial->map->n = m;
27759d4fc724SLawrence Mitchell   patch->patchUpdate->map->N            = n;
27769d4fc724SLawrence Mitchell   patch->patchRHSWithArtificial->map->N = m;
27779566063dSJacob Faibussowitsch   PetscCall(MatMult(multMat, patch->patchUpdate, patch->patchRHSWithArtificial));
27789566063dSJacob Faibussowitsch   PetscCall(VecScale(patch->patchRHSWithArtificial, -1.0));
27799566063dSJacob Faibussowitsch   PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHSWithArtificial, patch->localRHS, ADD_VALUES, SCATTER_REVERSE, SCATTER_WITHARTIFICIAL));
278048a46eb9SPierre Jolivet   if (!patch->save_operators) PetscCall(MatDestroy(&multMat));
27813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27826c9c532dSPatrick Farrell }
27836c9c532dSPatrick Farrell 
2784d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_PATCH(PC pc, Vec x, Vec y)
2785d71ae5a4SJacob Faibussowitsch {
27864bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch        = (PC_PATCH *)pc->data;
27871202d238SPatrick Farrell   const PetscScalar *globalRHS    = NULL;
27881202d238SPatrick Farrell   PetscScalar       *localRHS     = NULL;
27891202d238SPatrick Farrell   PetscScalar       *globalUpdate = NULL;
27904bbf5ea8SMatthew G. Knepley   const PetscInt    *bcNodes      = NULL;
27914bbf5ea8SMatthew G. Knepley   PetscInt           nsweep       = patch->symmetrise_sweep ? 2 : 1;
27924bbf5ea8SMatthew G. Knepley   PetscInt           start[2]     = {0, 0};
27934bbf5ea8SMatthew G. Knepley   PetscInt           end[2]       = {-1, -1};
27944bbf5ea8SMatthew G. Knepley   const PetscInt     inc[2]       = {1, -1};
27951202d238SPatrick Farrell   const PetscScalar *localUpdate;
27964bbf5ea8SMatthew G. Knepley   const PetscInt    *iterationSet;
27974bbf5ea8SMatthew G. Knepley   PetscInt           pStart, numBcs, n, sweep, bc, j;
27984bbf5ea8SMatthew G. Knepley 
27994bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
28009566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(PC_Patch_Apply, pc, 0, 0, 0));
28019566063dSJacob Faibussowitsch   PetscCall(PetscOptionsPushGetViewerOff(PETSC_TRUE));
280292d50984SMatthew G. Knepley   /* start, end, inc have 2 entries to manage a second backward sweep if we symmetrize */
28034bbf5ea8SMatthew G. Knepley   end[0]   = patch->npatch;
28044bbf5ea8SMatthew G. Knepley   start[1] = patch->npatch - 1;
28054bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
28069566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(patch->iterationSet, &end[0]));
28074bbf5ea8SMatthew G. Knepley     start[1] = end[0] - 1;
28089566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(patch->iterationSet, &iterationSet));
28094bbf5ea8SMatthew G. Knepley   }
28104bbf5ea8SMatthew G. Knepley   /* Scatter from global space into overlapped local spaces */
28119566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(x, &globalRHS));
28129566063dSJacob Faibussowitsch   PetscCall(VecGetArray(patch->localRHS, &localRHS));
28139566063dSJacob Faibussowitsch   PetscCall(PetscSFBcastBegin(patch->sectionSF, MPIU_SCALAR, globalRHS, localRHS, MPI_REPLACE));
28149566063dSJacob Faibussowitsch   PetscCall(PetscSFBcastEnd(patch->sectionSF, MPIU_SCALAR, globalRHS, localRHS, MPI_REPLACE));
28159566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(x, &globalRHS));
28169566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(patch->localRHS, &localRHS));
28174bbf5ea8SMatthew G. Knepley 
28189566063dSJacob Faibussowitsch   PetscCall(VecSet(patch->localUpdate, 0.0));
28199566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(patch->gtolCounts, &pStart, NULL));
28209566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0));
28214bbf5ea8SMatthew G. Knepley   for (sweep = 0; sweep < nsweep; sweep++) {
28224bbf5ea8SMatthew G. Knepley     for (j = start[sweep]; j * inc[sweep] < end[sweep] * inc[sweep]; j += inc[sweep]) {
28234bbf5ea8SMatthew G. Knepley       PetscInt i = patch->user_patches ? iterationSet[j] : j;
28244bbf5ea8SMatthew G. Knepley       PetscInt start, len;
28254bbf5ea8SMatthew G. Knepley 
28269566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(patch->gtolCounts, i + pStart, &len));
28279566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(patch->gtolCounts, i + pStart, &start));
28284bbf5ea8SMatthew G. Knepley       /* TODO: Squash out these guys in the setup as well. */
28294bbf5ea8SMatthew G. Knepley       if (len <= 0) continue;
28304bbf5ea8SMatthew G. Knepley       /* TODO: Do we need different scatters for X and Y? */
28319566063dSJacob Faibussowitsch       PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->localRHS, patch->patchRHS, INSERT_VALUES, SCATTER_FORWARD, SCATTER_INTERIOR));
28329566063dSJacob Faibussowitsch       PetscCall((*patch->applysolver)(pc, i, patch->patchRHS, patch->patchUpdate));
28339566063dSJacob Faibussowitsch       PetscCall(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchUpdate, patch->localUpdate, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR));
283448a46eb9SPierre Jolivet       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) PetscCall((*patch->updatemultiplicative)(pc, i, pStart));
28354bbf5ea8SMatthew G. Knepley     }
28364bbf5ea8SMatthew G. Knepley   }
28379566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0));
28389566063dSJacob Faibussowitsch   if (patch->user_patches) PetscCall(ISRestoreIndices(patch->iterationSet, &iterationSet));
28394bbf5ea8SMatthew G. Knepley   /* XXX: should we do this on the global vector? */
28401baa6e33SBarry Smith   if (patch->partition_of_unity) PetscCall(VecPointwiseMult(patch->localUpdate, patch->localUpdate, patch->dof_weights));
28411202d238SPatrick Farrell   /* Now patch->localUpdate contains the solution of the patch solves, so we need to combine them all. */
28429566063dSJacob Faibussowitsch   PetscCall(VecSet(y, 0.0));
28439566063dSJacob Faibussowitsch   PetscCall(VecGetArray(y, &globalUpdate));
28449566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(patch->localUpdate, &localUpdate));
28459566063dSJacob Faibussowitsch   PetscCall(PetscSFReduceBegin(patch->sectionSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM));
28469566063dSJacob Faibussowitsch   PetscCall(PetscSFReduceEnd(patch->sectionSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM));
28479566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(patch->localUpdate, &localUpdate));
28484bbf5ea8SMatthew G. Knepley 
28494bbf5ea8SMatthew G. Knepley   /* Now we need to send the global BC values through */
28509566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(x, &globalRHS));
28519566063dSJacob Faibussowitsch   PetscCall(ISGetSize(patch->globalBcNodes, &numBcs));
28529566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(patch->globalBcNodes, &bcNodes));
28539566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(x, &n));
28544bbf5ea8SMatthew G. Knepley   for (bc = 0; bc < numBcs; ++bc) {
28554bbf5ea8SMatthew G. Knepley     const PetscInt idx = bcNodes[bc];
28561202d238SPatrick Farrell     if (idx < n) globalUpdate[idx] = globalRHS[idx];
28574bbf5ea8SMatthew G. Knepley   }
28584bbf5ea8SMatthew G. Knepley 
28599566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(patch->globalBcNodes, &bcNodes));
28609566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(x, &globalRHS));
28619566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(y, &globalUpdate));
28624bbf5ea8SMatthew G. Knepley 
28639566063dSJacob Faibussowitsch   PetscCall(PetscOptionsPopGetViewerOff());
28649566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(PC_Patch_Apply, pc, 0, 0, 0));
28653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
28664bbf5ea8SMatthew G. Knepley }
28674bbf5ea8SMatthew G. Knepley 
2868d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCReset_PATCH_Linear(PC pc)
2869d71ae5a4SJacob Faibussowitsch {
2870dadc69c5SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
2871dadc69c5SMatthew G. Knepley   PetscInt  i;
2872dadc69c5SMatthew G. Knepley 
2873dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2874dadc69c5SMatthew G. Knepley   if (patch->solver) {
28759566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(KSPReset((KSP)patch->solver[i]));
2876dadc69c5SMatthew G. Knepley   }
28773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2878dadc69c5SMatthew G. Knepley }
2879dadc69c5SMatthew G. Knepley 
2880d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCReset_PATCH(PC pc)
2881d71ae5a4SJacob Faibussowitsch {
28824bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
28834bbf5ea8SMatthew G. Knepley   PetscInt  i;
28844bbf5ea8SMatthew G. Knepley 
28854bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2886fa84ea4cSLawrence Mitchell 
28879566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&patch->sectionSF));
28889566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->cellCounts));
28899566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->pointCounts));
28909566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->cellNumbering));
28919566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->gtolCounts));
28929566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->gtol));
28939566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->cells));
28949566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->points));
28959566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->dofs));
28969566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->offs));
28979566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->patchSection));
28989566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->ghostBcNodes));
28999566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->globalBcNodes));
29009566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->gtolCountsWithArtificial));
29019566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->gtolWithArtificial));
29029566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->dofsWithArtificial));
29039566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->offsWithArtificial));
29049566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->gtolCountsWithAll));
29059566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->gtolWithAll));
29069566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->dofsWithAll));
29079566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->offsWithAll));
29089566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->cellMats));
29099566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->intFacetMats));
29109566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->allCells));
29119566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->intFacets));
29129566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->extFacets));
29139566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->intFacetsToPatchCell));
29149566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->intFacetCounts));
29159566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&patch->extFacetCounts));
29164bbf5ea8SMatthew G. Knepley 
29179371c9d4SSatish Balay   if (patch->dofSection)
29189371c9d4SSatish Balay     for (i = 0; i < patch->nsubspaces; i++) PetscCall(PetscSectionDestroy(&patch->dofSection[i]));
29199566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->dofSection));
29209566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->bs));
29219566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->nodesPerCell));
29229371c9d4SSatish Balay   if (patch->cellNodeMap)
29239371c9d4SSatish Balay     for (i = 0; i < patch->nsubspaces; i++) PetscCall(PetscFree(patch->cellNodeMap[i]));
29249566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->cellNodeMap));
29259566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->subspaceOffsets));
29264bbf5ea8SMatthew G. Knepley 
29279566063dSJacob Faibussowitsch   PetscCall((*patch->resetsolver)(pc));
29284bbf5ea8SMatthew G. Knepley 
292948a46eb9SPierre Jolivet   if (patch->subspaces_to_exclude) PetscCall(PetscHSetIDestroy(&patch->subspaces_to_exclude));
2930e4c66b91SPatrick Farrell 
29319566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->localRHS));
29329566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->localUpdate));
29339566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->patchRHS));
29349566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->patchUpdate));
29359566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->dof_weights));
29364bbf5ea8SMatthew G. Knepley   if (patch->patch_dof_weights) {
29379566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(VecDestroy(&patch->patch_dof_weights[i]));
29389566063dSJacob Faibussowitsch     PetscCall(PetscFree(patch->patch_dof_weights));
29394bbf5ea8SMatthew G. Knepley   }
29404bbf5ea8SMatthew G. Knepley   if (patch->mat) {
29419566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(MatDestroy(&patch->mat[i]));
29429566063dSJacob Faibussowitsch     PetscCall(PetscFree(patch->mat));
29435f824522SMatthew G. Knepley   }
29440997d788SPatrick Farrell   if (patch->matWithArtificial && !patch->isNonlinear) {
29459566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(MatDestroy(&patch->matWithArtificial[i]));
29469566063dSJacob Faibussowitsch     PetscCall(PetscFree(patch->matWithArtificial));
294711ac8bb0SFlorian Wechsung   }
29489566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&patch->patchRHSWithArtificial));
294996b79ebeSFlorian Wechsung   if (patch->dofMappingWithoutToWithArtificial) {
29509566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->dofMappingWithoutToWithArtificial[i]));
29519566063dSJacob Faibussowitsch     PetscCall(PetscFree(patch->dofMappingWithoutToWithArtificial));
295296b79ebeSFlorian Wechsung   }
29530904074fSPatrick Farrell   if (patch->dofMappingWithoutToWithAll) {
29549566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->dofMappingWithoutToWithAll[i]));
29559566063dSJacob Faibussowitsch     PetscCall(PetscFree(patch->dofMappingWithoutToWithAll));
29560904074fSPatrick Farrell   }
29579566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->sub_mat_type));
29585f824522SMatthew G. Knepley   if (patch->userIS) {
29599566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(ISDestroy(&patch->userIS[i]));
29609566063dSJacob Faibussowitsch     PetscCall(PetscFree(patch->userIS));
29615f824522SMatthew G. Knepley   }
29629566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->precomputedTensorLocations));
29639566063dSJacob Faibussowitsch   PetscCall(PetscFree(patch->precomputedIntFacetTensorLocations));
2964f98464cbSLawrence Mitchell 
29650a545947SLisandro Dalcin   patch->bs          = NULL;
29664bbf5ea8SMatthew G. Knepley   patch->cellNodeMap = NULL;
29677974b488SMatthew G. Knepley   patch->nsubspaces  = 0;
29689566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&patch->iterationSet));
29695f824522SMatthew G. Knepley 
29709566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&patch->viewerSection));
29713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29724bbf5ea8SMatthew G. Knepley }
29734bbf5ea8SMatthew G. Knepley 
2974d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_PATCH_Linear(PC pc)
2975d71ae5a4SJacob Faibussowitsch {
29764bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
29774bbf5ea8SMatthew G. Knepley   PetscInt  i;
29784bbf5ea8SMatthew G. Knepley 
29794bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2980dadc69c5SMatthew G. Knepley   if (patch->solver) {
29819566063dSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) PetscCall(KSPDestroy((KSP *)&patch->solver[i]));
29829566063dSJacob Faibussowitsch     PetscCall(PetscFree(patch->solver));
29834bbf5ea8SMatthew G. Knepley   }
29843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2985dadc69c5SMatthew G. Knepley }
2986dadc69c5SMatthew G. Knepley 
2987d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_PATCH(PC pc)
2988d71ae5a4SJacob Faibussowitsch {
2989dadc69c5SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *)pc->data;
2990dadc69c5SMatthew G. Knepley 
2991dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
29929566063dSJacob Faibussowitsch   PetscCall(PCReset_PATCH(pc));
29939566063dSJacob Faibussowitsch   PetscCall((*patch->destroysolver)(pc));
29949566063dSJacob Faibussowitsch   PetscCall(PetscFree(pc->data));
29953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29964bbf5ea8SMatthew G. Knepley }
29974bbf5ea8SMatthew G. Knepley 
2998d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetFromOptions_PATCH(PC pc, PetscOptionItems *PetscOptionsObject)
2999d71ae5a4SJacob Faibussowitsch {
30004bbf5ea8SMatthew G. Knepley   PC_PATCH            *patch                 = (PC_PATCH *)pc->data;
30014bbf5ea8SMatthew G. Knepley   PCPatchConstructType patchConstructionType = PC_PATCH_STAR;
30025f824522SMatthew G. Knepley   char                 sub_mat_type[PETSC_MAX_PATH_LEN];
300310534d48SPatrick Farrell   char                 option[PETSC_MAX_PATH_LEN];
30045f824522SMatthew G. Knepley   const char          *prefix;
30054bbf5ea8SMatthew G. Knepley   PetscBool            flg, dimflg, codimflg;
30065f824522SMatthew G. Knepley   MPI_Comm             comm;
3007a48c39c8SPatrick Farrell   PetscInt            *ifields, nfields, k;
300861c4b389SFlorian Wechsung   PCCompositeType      loctype = PC_COMPOSITE_ADDITIVE;
30094bbf5ea8SMatthew G. Knepley 
30104bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
30119566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)pc, &comm));
30129566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)pc, &prefix));
3013d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "Patch solver options");
301410534d48SPatrick Farrell 
30159566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_save_operators", patch->classname));
30169566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool(option, "Store all patch operators for lifetime of object?", "PCPatchSetSaveOperators", patch->save_operators, &patch->save_operators, &flg));
301710534d48SPatrick Farrell 
30189566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_precompute_element_tensors", patch->classname));
30199566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool(option, "Compute each element tensor only once?", "PCPatchSetPrecomputeElementTensors", patch->precomputeElementTensors, &patch->precomputeElementTensors, &flg));
30209566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_partition_of_unity", patch->classname));
30219566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool(option, "Weight contributions by dof multiplicity?", "PCPatchSetPartitionOfUnity", patch->partition_of_unity, &patch->partition_of_unity, &flg));
302210534d48SPatrick Farrell 
30239566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_local_type", patch->classname));
30249566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum(option, "Type of local solver composition (additive or multiplicative)", "PCPatchSetLocalComposition", PCCompositeTypes, (PetscEnum)loctype, (PetscEnum *)&loctype, &flg));
30259566063dSJacob Faibussowitsch   if (flg) PetscCall(PCPatchSetLocalComposition(pc, loctype));
30269566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_dense_inverse", patch->classname));
30279566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool(option, "Compute inverses of patch matrices and apply directly? Ignores KSP/PC settings on patch.", "PCPatchSetDenseInverse", patch->denseinverse, &patch->denseinverse, &flg));
30289566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_dim", patch->classname));
30299566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt(option, "What dimension of mesh point to construct patches by? (0 = vertices)", "PCPATCH", patch->dim, &patch->dim, &dimflg));
30309566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_codim", patch->classname));
30319566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt(option, "What co-dimension of mesh point to construct patches by? (0 = cells)", "PCPATCH", patch->codim, &patch->codim, &codimflg));
303208401ef6SPierre Jolivet   PetscCheck(!dimflg || !codimflg, comm, PETSC_ERR_ARG_WRONG, "Can only set one of dimension or co-dimension");
303310534d48SPatrick Farrell 
30349566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_type", patch->classname));
30359566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum(option, "How should the patches be constructed?", "PCPatchSetConstructType", PCPatchConstructTypes, (PetscEnum)patchConstructionType, (PetscEnum *)&patchConstructionType, &flg));
30369566063dSJacob Faibussowitsch   if (flg) PetscCall(PCPatchSetConstructType(pc, patchConstructionType, NULL, NULL));
303710534d48SPatrick Farrell 
30389566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_vanka_dim", patch->classname));
30399566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt(option, "Topological dimension of entities for Vanka to ignore", "PCPATCH", patch->vankadim, &patch->vankadim, &flg));
304010534d48SPatrick Farrell 
30419566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_ignore_dim", patch->classname));
30429566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt(option, "Topological dimension of entities for completion to ignore", "PCPATCH", patch->ignoredim, &patch->ignoredim, &flg));
304310534d48SPatrick Farrell 
30449566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_pardecomp_overlap", patch->classname));
30459566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt(option, "What overlap should we use in construct type pardecomp?", "PCPATCH", patch->pardecomp_overlap, &patch->pardecomp_overlap, &flg));
3046b525f888SPatrick Farrell 
30479566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_sub_mat_type", patch->classname));
30489566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList(option, "Matrix type for patch solves", "PCPatchSetSubMatType", MatList, NULL, sub_mat_type, PETSC_MAX_PATH_LEN, &flg));
30499566063dSJacob Faibussowitsch   if (flg) PetscCall(PCPatchSetSubMatType(pc, sub_mat_type));
305010534d48SPatrick Farrell 
30519566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_symmetrise_sweep", patch->classname));
30529566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool(option, "Go start->end, end->start?", "PCPATCH", patch->symmetrise_sweep, &patch->symmetrise_sweep, &flg));
3053e4c66b91SPatrick Farrell 
3054a48c39c8SPatrick Farrell   /* If the user has set the number of subspaces, use that for the buffer size,
3055a48c39c8SPatrick Farrell    otherwise use a large number */
3056a48c39c8SPatrick Farrell   if (patch->nsubspaces <= 0) {
3057a48c39c8SPatrick Farrell     nfields = 128;
3058a48c39c8SPatrick Farrell   } else {
3059a48c39c8SPatrick Farrell     nfields = patch->nsubspaces;
3060a48c39c8SPatrick Farrell   }
30619566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(nfields, &ifields));
30629566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exclude_subspaces", patch->classname));
30639566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetIntArray(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, option, ifields, &nfields, &flg));
306408401ef6SPierre 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");
3065e4c66b91SPatrick Farrell   if (flg) {
30669566063dSJacob Faibussowitsch     PetscCall(PetscHSetIClear(patch->subspaces_to_exclude));
306748a46eb9SPierre Jolivet     for (k = 0; k < nfields; k++) PetscCall(PetscHSetIAdd(patch->subspaces_to_exclude, ifields[k]));
3068e4c66b91SPatrick Farrell   }
30699566063dSJacob Faibussowitsch   PetscCall(PetscFree(ifields));
30705f824522SMatthew G. Knepley 
30719566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_patches_view", patch->classname));
30729566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool(option, "Print out information during patch construction", "PCPATCH", patch->viewPatches, &patch->viewPatches, &flg));
30739566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_cells_view", patch->classname));
30749566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerCells, &patch->formatCells, &patch->viewCells));
30759566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_interior_facets_view", patch->classname));
30769566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerIntFacets, &patch->formatIntFacets, &patch->viewIntFacets));
30779566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exterior_facets_view", patch->classname));
30789566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerExtFacets, &patch->formatExtFacets, &patch->viewExtFacets));
30799566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_points_view", patch->classname));
30809566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerPoints, &patch->formatPoints, &patch->viewPoints));
30819566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_section_view", patch->classname));
30829566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerSection, &patch->formatSection, &patch->viewSection));
30839566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_mat_view", patch->classname));
30849566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(comm, ((PetscObject)pc)->options, prefix, option, &patch->viewerMatrix, &patch->formatMatrix, &patch->viewMatrix));
3085d0609cedSBarry Smith   PetscOptionsHeadEnd();
30865f824522SMatthew G. Knepley   patch->optionsSet = PETSC_TRUE;
30873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
30884bbf5ea8SMatthew G. Knepley }
30894bbf5ea8SMatthew G. Knepley 
3090d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUpOnBlocks_PATCH(PC pc)
3091d71ae5a4SJacob Faibussowitsch {
30924bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch = (PC_PATCH *)pc->data;
30934bbf5ea8SMatthew G. Knepley   KSPConvergedReason reason;
30944bbf5ea8SMatthew G. Knepley   PetscInt           i;
30954bbf5ea8SMatthew G. Knepley 
30964bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3097a1eac568SLawrence Mitchell   if (!patch->save_operators) {
3098a1eac568SLawrence Mitchell     /* Can't do this here because the sub KSPs don't have an operator attached yet. */
30993ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3100a1eac568SLawrence Mitchell   }
3101c73d2cf6SLawrence Mitchell   if (patch->denseinverse) {
3102c73d2cf6SLawrence Mitchell     /* No solvers */
31033ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3104c73d2cf6SLawrence Mitchell   }
31054bbf5ea8SMatthew G. Knepley   for (i = 0; i < patch->npatch; ++i) {
310648a46eb9SPierre Jolivet     if (!((KSP)patch->solver[i])->setfromoptionscalled) PetscCall(KSPSetFromOptions((KSP)patch->solver[i]));
31079566063dSJacob Faibussowitsch     PetscCall(KSPSetUp((KSP)patch->solver[i]));
31089566063dSJacob Faibussowitsch     PetscCall(KSPGetConvergedReason((KSP)patch->solver[i], &reason));
3109c0decd05SBarry Smith     if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR;
31104bbf5ea8SMatthew G. Knepley   }
31113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31124bbf5ea8SMatthew G. Knepley }
31134bbf5ea8SMatthew G. Knepley 
3114d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCView_PATCH(PC pc, PetscViewer viewer)
3115d71ae5a4SJacob Faibussowitsch {
31164bbf5ea8SMatthew G. Knepley   PC_PATCH   *patch = (PC_PATCH *)pc->data;
31174bbf5ea8SMatthew G. Knepley   PetscViewer sviewer;
31184bbf5ea8SMatthew G. Knepley   PetscBool   isascii;
31194bbf5ea8SMatthew G. Knepley   PetscMPIInt rank;
31204bbf5ea8SMatthew G. Knepley 
31214bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
31224bbf5ea8SMatthew G. Knepley   /* TODO Redo tabbing with set tbas in new style */
31239566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
31243ba16761SJacob Faibussowitsch   if (!isascii) PetscFunctionReturn(PETSC_SUCCESS);
31259566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
31269566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPushTab(viewer));
312763a3b9bcSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "Subspace Correction preconditioner with %" PetscInt_FMT " patches\n", patch->npatch));
312861c4b389SFlorian Wechsung   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
31299566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "Schwarz type: multiplicative\n"));
3130e047a90bSFlorian Wechsung   } else {
31319566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "Schwarz type: additive\n"));
3132c2e6f3c0SFlorian Wechsung   }
31339566063dSJacob Faibussowitsch   if (patch->partition_of_unity) PetscCall(PetscViewerASCIIPrintf(viewer, "Weighting by partition of unity\n"));
31349566063dSJacob Faibussowitsch   else PetscCall(PetscViewerASCIIPrintf(viewer, "Not weighting by partition of unity\n"));
31359566063dSJacob Faibussowitsch   if (patch->symmetrise_sweep) PetscCall(PetscViewerASCIIPrintf(viewer, "Symmetrising sweep (start->end, then end->start)\n"));
31369566063dSJacob Faibussowitsch   else PetscCall(PetscViewerASCIIPrintf(viewer, "Not symmetrising sweep\n"));
31379566063dSJacob Faibussowitsch   if (!patch->precomputeElementTensors) PetscCall(PetscViewerASCIIPrintf(viewer, "Not precomputing element tensors (overlapping cells rebuilt in every patch assembly)\n"));
31389566063dSJacob Faibussowitsch   else PetscCall(PetscViewerASCIIPrintf(viewer, "Precomputing element tensors (each cell assembled only once)\n"));
31399566063dSJacob Faibussowitsch   if (!patch->save_operators) PetscCall(PetscViewerASCIIPrintf(viewer, "Not saving patch operators (rebuilt every PCApply)\n"));
31409566063dSJacob Faibussowitsch   else PetscCall(PetscViewerASCIIPrintf(viewer, "Saving patch operators (rebuilt every PCSetUp)\n"));
31419566063dSJacob Faibussowitsch   if (patch->patchconstructop == PCPatchConstruct_Star) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: star\n"));
31429566063dSJacob Faibussowitsch   else if (patch->patchconstructop == PCPatchConstruct_Vanka) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: Vanka\n"));
31439566063dSJacob Faibussowitsch   else if (patch->patchconstructop == PCPatchConstruct_User) PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: user-specified\n"));
31449566063dSJacob Faibussowitsch   else PetscCall(PetscViewerASCIIPrintf(viewer, "Patch construction operator: unknown\n"));
31455d30859aSPatrick Farrell 
3146c73d2cf6SLawrence Mitchell   if (patch->denseinverse) {
31479566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "Explicitly forming dense inverse and applying patch solver via MatMult.\n"));
3148c73d2cf6SLawrence Mitchell   } else {
31495d30859aSPatrick Farrell     if (patch->isNonlinear) {
31509566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "SNES on patches (all same):\n"));
31515d30859aSPatrick Farrell     } else {
31529566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "KSP on patches (all same):\n"));
31535d30859aSPatrick Farrell     }
3154dadc69c5SMatthew G. Knepley     if (patch->solver) {
31559566063dSJacob Faibussowitsch       PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
3156dd400576SPatrick Sanan       if (rank == 0) {
31579566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPushTab(sviewer));
31589566063dSJacob Faibussowitsch         PetscCall(PetscObjectView(patch->solver[0], sviewer));
31599566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPopTab(sviewer));
31604bbf5ea8SMatthew G. Knepley       }
31619566063dSJacob Faibussowitsch       PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
31624bbf5ea8SMatthew G. Knepley     } else {
31639566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
31649566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "Solver not yet set.\n"));
31659566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
31664bbf5ea8SMatthew G. Knepley     }
3167c73d2cf6SLawrence Mitchell   }
31689566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPopTab(viewer));
31693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31704bbf5ea8SMatthew G. Knepley }
31714bbf5ea8SMatthew G. Knepley 
3172e5893cccSMatthew G. Knepley /*MC
3173f1580f4eSBarry Smith    PCPATCH - A `PC` object that encapsulates flexible definition of blocks for overlapping and non-overlapping
317498ed095eSMatthew G. Knepley    small block additive preconditioners. Block definition is based on topology from
3175f1580f4eSBarry Smith    a `DM` and equation numbering from a `PetscSection`.
3176e5893cccSMatthew G. Knepley 
3177e5893cccSMatthew G. Knepley    Options Database Keys:
3178e5893cccSMatthew G. Knepley + -pc_patch_cells_view   - Views the process local cell numbers for each patch
3179e5893cccSMatthew G. Knepley . -pc_patch_points_view  - Views the process local mesh point numbers for each patch
3180e5893cccSMatthew G. Knepley . -pc_patch_g2l_view     - Views the map between global dofs and patch local dofs for each patch
3181e5893cccSMatthew G. Knepley . -pc_patch_patches_view - Views the global dofs associated with each patch and its boundary
3182e5893cccSMatthew G. Knepley - -pc_patch_sub_mat_view - Views the matrix associated with each patch
3183e5893cccSMatthew G. Knepley 
3184e5893cccSMatthew G. Knepley    Level: intermediate
3185e5893cccSMatthew G. Knepley 
3186f1580f4eSBarry Smith .seealso: `PCType`, `PCCreate()`, `PCSetType()`, `PCASM`, `PCJACOBI`, `PCPBJACOBI`, `PCVPBJACOBI`, `SNESPATCH`
3187e5893cccSMatthew G. Knepley M*/
3188d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PCCreate_Patch(PC pc)
3189d71ae5a4SJacob Faibussowitsch {
31904bbf5ea8SMatthew G. Knepley   PC_PATCH *patch;
31914bbf5ea8SMatthew G. Knepley 
31924bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
31934dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&patch));
31944bbf5ea8SMatthew G. Knepley 
319548a46eb9SPierre Jolivet   if (patch->subspaces_to_exclude) PetscCall(PetscHSetIDestroy(&patch->subspaces_to_exclude));
31969566063dSJacob Faibussowitsch   PetscCall(PetscHSetICreate(&patch->subspaces_to_exclude));
3197e4c66b91SPatrick Farrell 
319810534d48SPatrick Farrell   patch->classname   = "pc";
3199debbdec3SPatrick Farrell   patch->isNonlinear = PETSC_FALSE;
320010534d48SPatrick Farrell 
32014bbf5ea8SMatthew G. Knepley   /* Set some defaults */
32025f824522SMatthew G. Knepley   patch->combined                 = PETSC_FALSE;
32034bbf5ea8SMatthew G. Knepley   patch->save_operators           = PETSC_TRUE;
320461c4b389SFlorian Wechsung   patch->local_composition_type   = PC_COMPOSITE_ADDITIVE;
3205fa84ea4cSLawrence Mitchell   patch->precomputeElementTensors = PETSC_FALSE;
32064bbf5ea8SMatthew G. Knepley   patch->partition_of_unity       = PETSC_FALSE;
32074bbf5ea8SMatthew G. Knepley   patch->codim                    = -1;
32084bbf5ea8SMatthew G. Knepley   patch->dim                      = -1;
32094bbf5ea8SMatthew G. Knepley   patch->vankadim                 = -1;
32105f824522SMatthew G. Knepley   patch->ignoredim                = -1;
3211b525f888SPatrick Farrell   patch->pardecomp_overlap        = 0;
32124bbf5ea8SMatthew G. Knepley   patch->patchconstructop         = PCPatchConstruct_Star;
32134bbf5ea8SMatthew G. Knepley   patch->symmetrise_sweep         = PETSC_FALSE;
32145f824522SMatthew G. Knepley   patch->npatch                   = 0;
32154bbf5ea8SMatthew G. Knepley   patch->userIS                   = NULL;
32165f824522SMatthew G. Knepley   patch->optionsSet               = PETSC_FALSE;
32174bbf5ea8SMatthew G. Knepley   patch->iterationSet             = NULL;
32184bbf5ea8SMatthew G. Knepley   patch->user_patches             = PETSC_FALSE;
32199566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(MATDENSE, (char **)&patch->sub_mat_type));
32205f824522SMatthew G. Knepley   patch->viewPatches                       = PETSC_FALSE;
32215f824522SMatthew G. Knepley   patch->viewCells                         = PETSC_FALSE;
32225f824522SMatthew G. Knepley   patch->viewPoints                        = PETSC_FALSE;
32235f824522SMatthew G. Knepley   patch->viewSection                       = PETSC_FALSE;
32245f824522SMatthew G. Knepley   patch->viewMatrix                        = PETSC_FALSE;
32259d4fc724SLawrence Mitchell   patch->densesolve                        = NULL;
3226dadc69c5SMatthew G. Knepley   patch->setupsolver                       = PCSetUp_PATCH_Linear;
3227dadc69c5SMatthew G. Knepley   patch->applysolver                       = PCApply_PATCH_Linear;
3228dadc69c5SMatthew G. Knepley   patch->resetsolver                       = PCReset_PATCH_Linear;
3229dadc69c5SMatthew G. Knepley   patch->destroysolver                     = PCDestroy_PATCH_Linear;
32306c9c532dSPatrick Farrell   patch->updatemultiplicative              = PCUpdateMultiplicative_PATCH_Linear;
323147aca4a6SPatrick Farrell   patch->dofMappingWithoutToWithArtificial = NULL;
323247aca4a6SPatrick Farrell   patch->dofMappingWithoutToWithAll        = NULL;
32334bbf5ea8SMatthew G. Knepley 
32344bbf5ea8SMatthew G. Knepley   pc->data                 = (void *)patch;
32354bbf5ea8SMatthew G. Knepley   pc->ops->apply           = PCApply_PATCH;
32360a545947SLisandro Dalcin   pc->ops->applytranspose  = NULL; /* PCApplyTranspose_PATCH; */
32374bbf5ea8SMatthew G. Knepley   pc->ops->setup           = PCSetUp_PATCH;
32384bbf5ea8SMatthew G. Knepley   pc->ops->reset           = PCReset_PATCH;
32394bbf5ea8SMatthew G. Knepley   pc->ops->destroy         = PCDestroy_PATCH;
32404bbf5ea8SMatthew G. Knepley   pc->ops->setfromoptions  = PCSetFromOptions_PATCH;
32414bbf5ea8SMatthew G. Knepley   pc->ops->setuponblocks   = PCSetUpOnBlocks_PATCH;
32424bbf5ea8SMatthew G. Knepley   pc->ops->view            = PCView_PATCH;
32430a545947SLisandro Dalcin   pc->ops->applyrichardson = NULL;
32444bbf5ea8SMatthew G. Knepley 
32453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
32464bbf5ea8SMatthew G. Knepley }
3247