xref: /petsc/src/ksp/pc/impls/patch/pcpatch.c (revision 2f613bf53f46f9356e00a2ca2bd69453be72fc31)
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 
125f824522SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode ObjectView(PetscObject obj, PetscViewer viewer, PetscViewerFormat format)
135f824522SMatthew G. Knepley {
145f824522SMatthew G. Knepley   PetscErrorCode ierr;
155f824522SMatthew G. Knepley 
165f824522SMatthew G. Knepley   ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr);
175f824522SMatthew G. Knepley   ierr = PetscObjectView(obj, viewer);CHKERRQ(ierr);
185f824522SMatthew G. Knepley   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
197974b488SMatthew G. Knepley   return(0);
205f824522SMatthew G. Knepley }
215f824522SMatthew G. Knepley 
221b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_Star(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
234bbf5ea8SMatthew G. Knepley {
244bbf5ea8SMatthew G. Knepley   PetscInt       starSize;
254bbf5ea8SMatthew G. Knepley   PetscInt      *star = NULL, si;
264bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
274bbf5ea8SMatthew G. Knepley 
284bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
29*2f613bf5SBarry Smith   ierr = PetscHSetIClear(ht);CHKERRQ(ierr);
304bbf5ea8SMatthew G. Knepley   /* To start with, add the point we care about */
311b68eb51SMatthew G. Knepley   ierr = PetscHSetIAdd(ht, point);CHKERRQ(ierr);
324bbf5ea8SMatthew G. Knepley   /* Loop over all the points that this point connects to */
334bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
341b68eb51SMatthew G. Knepley   for (si = 0; si < starSize*2; si += 2) {ierr = PetscHSetIAdd(ht, star[si]);CHKERRQ(ierr);}
354bbf5ea8SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
364bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
374bbf5ea8SMatthew G. Knepley }
384bbf5ea8SMatthew G. Knepley 
391b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_Vanka(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
404bbf5ea8SMatthew G. Knepley {
414bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) vpatch;
424bbf5ea8SMatthew G. Knepley   PetscInt       starSize;
434bbf5ea8SMatthew G. Knepley   PetscInt      *star = NULL;
444bbf5ea8SMatthew G. Knepley   PetscBool      shouldIgnore = PETSC_FALSE;
454bbf5ea8SMatthew G. Knepley   PetscInt       cStart, cEnd, iStart, iEnd, si;
464bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
474bbf5ea8SMatthew G. Knepley 
484bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
491b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(ht);CHKERRQ(ierr);
504bbf5ea8SMatthew G. Knepley   /* To start with, add the point we care about */
511b68eb51SMatthew G. Knepley   ierr = PetscHSetIAdd(ht, point);CHKERRQ(ierr);
524bbf5ea8SMatthew G. Knepley   /* Should we ignore any points of a certain dimension? */
534bbf5ea8SMatthew G. Knepley   if (patch->vankadim >= 0) {
544bbf5ea8SMatthew G. Knepley     shouldIgnore = PETSC_TRUE;
554bbf5ea8SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, patch->vankadim, &iStart, &iEnd);CHKERRQ(ierr);
564bbf5ea8SMatthew G. Knepley   }
574bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
584bbf5ea8SMatthew G. Knepley   /* Loop over all the cells that this point connects to */
594bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
605f824522SMatthew G. Knepley   for (si = 0; si < starSize*2; si += 2) {
614bbf5ea8SMatthew G. Knepley     const PetscInt cell = star[si];
624bbf5ea8SMatthew G. Knepley     PetscInt       closureSize;
634bbf5ea8SMatthew G. Knepley     PetscInt      *closure = NULL, ci;
644bbf5ea8SMatthew G. Knepley 
654bbf5ea8SMatthew G. Knepley     if (cell < cStart || cell >= cEnd) continue;
664bbf5ea8SMatthew G. Knepley     /* now loop over all entities in the closure of that cell */
674bbf5ea8SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
685f824522SMatthew G. Knepley     for (ci = 0; ci < closureSize*2; ci += 2) {
694bbf5ea8SMatthew G. Knepley       const PetscInt newpoint = closure[ci];
704bbf5ea8SMatthew G. Knepley 
714bbf5ea8SMatthew G. Knepley       /* We've been told to ignore entities of this type.*/
724bbf5ea8SMatthew G. Knepley       if (shouldIgnore && newpoint >= iStart && newpoint < iEnd) continue;
731b68eb51SMatthew G. Knepley       ierr = PetscHSetIAdd(ht, newpoint);CHKERRQ(ierr);
744bbf5ea8SMatthew G. Knepley     }
754bbf5ea8SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
764bbf5ea8SMatthew G. Knepley   }
774bbf5ea8SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
784bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
794bbf5ea8SMatthew G. Knepley }
804bbf5ea8SMatthew G. Knepley 
81e5b9877fSPatrick Farrell static PetscErrorCode PCPatchConstruct_Pardecomp(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
820a390943SPatrick Farrell {
83b525f888SPatrick Farrell   PC_PATCH       *patch = (PC_PATCH *) vpatch;
840a390943SPatrick Farrell   DMLabel         ghost = NULL;
850a390943SPatrick Farrell   const PetscInt *leaves;
860a390943SPatrick Farrell   PetscInt        nleaves, pStart, pEnd, loc;
870a390943SPatrick Farrell   PetscBool       isFiredrake;
880a390943SPatrick Farrell   PetscBool       flg;
89b525f888SPatrick Farrell   PetscInt        starSize;
90b525f888SPatrick Farrell   PetscInt       *star = NULL;
9125fd193aSPatrick Farrell   PetscInt        opoint, overlapi;
920a390943SPatrick Farrell   PetscErrorCode  ierr;
930a390943SPatrick Farrell 
940a390943SPatrick Farrell   PetscFunctionBegin;
95*2f613bf5SBarry Smith   ierr = PetscHSetIClear(ht);CHKERRQ(ierr);
960a390943SPatrick Farrell 
97b6bb21d1SLawrence Mitchell   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
980a390943SPatrick Farrell 
990a390943SPatrick Farrell   ierr = DMHasLabel(dm, "pyop2_ghost", &isFiredrake);CHKERRQ(ierr);
1000a390943SPatrick Farrell   if (isFiredrake) {
1010a390943SPatrick Farrell     ierr = DMGetLabel(dm, "pyop2_ghost", &ghost);CHKERRQ(ierr);
1020a390943SPatrick Farrell     ierr = DMLabelCreateIndex(ghost, pStart, pEnd);CHKERRQ(ierr);
1030a390943SPatrick Farrell   } else {
1040a390943SPatrick Farrell     PetscSF sf;
1050a390943SPatrick Farrell     ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
1060a390943SPatrick Farrell     ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
1070a390943SPatrick Farrell     nleaves = PetscMax(nleaves, 0);
1080a390943SPatrick Farrell   }
1090a390943SPatrick Farrell 
11025fd193aSPatrick Farrell   for (opoint = pStart; opoint < pEnd; ++opoint) {
111bfabdd78SPatrick Farrell     if (ghost) {ierr = DMLabelHasPoint(ghost, opoint, &flg);CHKERRQ(ierr);}
112bfabdd78SPatrick Farrell     else       {ierr = PetscFindInt(opoint, nleaves, leaves, &loc);CHKERRQ(ierr); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;}
1130a390943SPatrick Farrell     /* Not an owned entity, don't make a cell patch. */
1140a390943SPatrick Farrell     if (flg) continue;
115bfabdd78SPatrick Farrell     ierr = PetscHSetIAdd(ht, opoint);CHKERRQ(ierr);
1160a390943SPatrick Farrell   }
1170a390943SPatrick Farrell 
118b525f888SPatrick Farrell   /* Now build the overlap for the patch */
11925fd193aSPatrick Farrell   for (overlapi = 0; overlapi < patch->pardecomp_overlap; ++overlapi) {
120b525f888SPatrick Farrell     PetscInt index = 0;
121b525f888SPatrick Farrell     PetscInt *htpoints = NULL;
122b525f888SPatrick Farrell     PetscInt htsize;
12325fd193aSPatrick Farrell     PetscInt i;
124b525f888SPatrick Farrell 
125b525f888SPatrick Farrell     ierr = PetscHSetIGetSize(ht, &htsize);CHKERRQ(ierr);
126b525f888SPatrick Farrell     ierr = PetscMalloc1(htsize, &htpoints);CHKERRQ(ierr);
127b525f888SPatrick Farrell     ierr = PetscHSetIGetElems(ht, &index, htpoints);CHKERRQ(ierr);
128b525f888SPatrick Farrell 
12925fd193aSPatrick Farrell     for (i = 0; i < htsize; ++i) {
13025fd193aSPatrick Farrell       PetscInt hpoint = htpoints[i];
13125fd193aSPatrick Farrell       PetscInt si;
132b525f888SPatrick Farrell 
13325fd193aSPatrick Farrell       ierr = DMPlexGetTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
13425fd193aSPatrick Farrell       for (si = 0; si < starSize*2; si += 2) {
135b525f888SPatrick Farrell         const PetscInt starp = star[si];
136b525f888SPatrick Farrell         PetscInt       closureSize;
137b525f888SPatrick Farrell         PetscInt      *closure = NULL, ci;
138b525f888SPatrick Farrell 
139b525f888SPatrick Farrell         /* now loop over all entities in the closure of starp */
140b525f888SPatrick Farrell         ierr = DMPlexGetTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
14125fd193aSPatrick Farrell         for (ci = 0; ci < closureSize*2; ci += 2) {
142b525f888SPatrick Farrell           const PetscInt closstarp = closure[ci];
143b525f888SPatrick Farrell           ierr = PetscHSetIAdd(ht, closstarp);CHKERRQ(ierr);
144b525f888SPatrick Farrell         }
145b525f888SPatrick Farrell         ierr = DMPlexRestoreTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
146b525f888SPatrick Farrell       }
14725fd193aSPatrick Farrell       ierr = DMPlexRestoreTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
148b525f888SPatrick Farrell     }
149b525f888SPatrick Farrell     ierr = PetscFree(htpoints);CHKERRQ(ierr);
150b525f888SPatrick Farrell   }
151b525f888SPatrick Farrell 
1520a390943SPatrick Farrell   PetscFunctionReturn(0);
1530a390943SPatrick Farrell }
1540a390943SPatrick Farrell 
1554bbf5ea8SMatthew G. Knepley /* The user's already set the patches in patch->userIS. Build the hash tables */
1561b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_User(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
1574bbf5ea8SMatthew G. Knepley {
1584bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch   = (PC_PATCH *) vpatch;
1594bbf5ea8SMatthew G. Knepley   IS              patchis = patch->userIS[point];
1604bbf5ea8SMatthew G. Knepley   PetscInt        n;
1614bbf5ea8SMatthew G. Knepley   const PetscInt *patchdata;
1624bbf5ea8SMatthew G. Knepley   PetscInt        pStart, pEnd, i;
1634bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
1644bbf5ea8SMatthew G. Knepley 
1654bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1661b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(ht);CHKERRQ(ierr);
1671b68eb51SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
1684bbf5ea8SMatthew G. Knepley   ierr = ISGetLocalSize(patchis, &n);CHKERRQ(ierr);
1694bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patchis, &patchdata);CHKERRQ(ierr);
1704bbf5ea8SMatthew G. Knepley   for (i = 0; i < n; ++i) {
1714bbf5ea8SMatthew G. Knepley     const PetscInt ownedpoint = patchdata[i];
1724bbf5ea8SMatthew G. Knepley 
1734bbf5ea8SMatthew G. Knepley     if (ownedpoint < pStart || ownedpoint >= pEnd) {
1744bbf5ea8SMatthew G. Knepley       SETERRQ3(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D was not in [%D, %D)", ownedpoint, pStart, pEnd);
1754bbf5ea8SMatthew G. Knepley     }
1761b68eb51SMatthew G. Knepley     ierr = PetscHSetIAdd(ht, ownedpoint);CHKERRQ(ierr);
1774bbf5ea8SMatthew G. Knepley   }
1784bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patchis, &patchdata);CHKERRQ(ierr);
1794bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
1804bbf5ea8SMatthew G. Knepley }
1814bbf5ea8SMatthew G. Knepley 
1824bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateDefaultSF_Private(PC pc, PetscInt n, const PetscSF *sf, const PetscInt *bs)
1834bbf5ea8SMatthew G. Knepley {
1844bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
1854bbf5ea8SMatthew G. Knepley   PetscInt       i;
1864bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
1874bbf5ea8SMatthew G. Knepley 
1884bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1894bbf5ea8SMatthew G. Knepley   if (n == 1 && bs[0] == 1) {
1901bb6d2a8SBarry Smith     patch->sectionSF = sf[0];
1911bb6d2a8SBarry Smith     ierr = PetscObjectReference((PetscObject) patch->sectionSF);CHKERRQ(ierr);
1924bbf5ea8SMatthew G. Knepley   } else {
1934bbf5ea8SMatthew G. Knepley     PetscInt     allRoots = 0, allLeaves = 0;
1944bbf5ea8SMatthew G. Knepley     PetscInt     leafOffset = 0;
1954bbf5ea8SMatthew G. Knepley     PetscInt    *ilocal = NULL;
1964bbf5ea8SMatthew G. Knepley     PetscSFNode *iremote = NULL;
1974bbf5ea8SMatthew G. Knepley     PetscInt    *remoteOffsets = NULL;
1984bbf5ea8SMatthew G. Knepley     PetscInt     index = 0;
1991b68eb51SMatthew G. Knepley     PetscHMapI   rankToIndex;
2004bbf5ea8SMatthew G. Knepley     PetscInt     numRanks = 0;
2014bbf5ea8SMatthew G. Knepley     PetscSFNode *remote = NULL;
2024bbf5ea8SMatthew G. Knepley     PetscSF      rankSF;
2034bbf5ea8SMatthew G. Knepley     PetscInt    *ranks = NULL;
2044bbf5ea8SMatthew G. Knepley     PetscInt    *offsets = NULL;
2054bbf5ea8SMatthew G. Knepley     MPI_Datatype contig;
2061b68eb51SMatthew G. Knepley     PetscHSetI   ranksUniq;
2074bbf5ea8SMatthew G. Knepley 
2084bbf5ea8SMatthew G. Knepley     /* First figure out how many dofs there are in the concatenated numbering.
2094bbf5ea8SMatthew G. Knepley      * allRoots: number of owned global dofs;
2104bbf5ea8SMatthew G. Knepley      * allLeaves: number of visible dofs (global + ghosted).
2114bbf5ea8SMatthew G. Knepley      */
2124bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2134bbf5ea8SMatthew G. Knepley       PetscInt nroots, nleaves;
2144bbf5ea8SMatthew G. Knepley 
2154bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetGraph(sf[i], &nroots, &nleaves, NULL, NULL);CHKERRQ(ierr);
2164bbf5ea8SMatthew G. Knepley       allRoots  += nroots * bs[i];
2174bbf5ea8SMatthew G. Knepley       allLeaves += nleaves * bs[i];
2184bbf5ea8SMatthew G. Knepley     }
2194bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(allLeaves, &ilocal);CHKERRQ(ierr);
2204bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(allLeaves, &iremote);CHKERRQ(ierr);
2214bbf5ea8SMatthew G. Knepley     /* Now build an SF that just contains process connectivity. */
2221b68eb51SMatthew G. Knepley     ierr = PetscHSetICreate(&ranksUniq);CHKERRQ(ierr);
2234bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2244bbf5ea8SMatthew G. Knepley       const PetscMPIInt *ranks = NULL;
2254bbf5ea8SMatthew G. Knepley       PetscInt           nranks, j;
2264bbf5ea8SMatthew G. Knepley 
2274bbf5ea8SMatthew G. Knepley       ierr = PetscSFSetUp(sf[i]);CHKERRQ(ierr);
228dec1416fSJunchao Zhang       ierr = PetscSFGetRootRanks(sf[i], &nranks, &ranks, NULL, NULL, NULL);CHKERRQ(ierr);
2294bbf5ea8SMatthew G. Knepley       /* These are all the ranks who communicate with me. */
2304bbf5ea8SMatthew G. Knepley       for (j = 0; j < nranks; ++j) {
2311b68eb51SMatthew G. Knepley         ierr = PetscHSetIAdd(ranksUniq, (PetscInt) ranks[j]);CHKERRQ(ierr);
2324bbf5ea8SMatthew G. Knepley       }
2334bbf5ea8SMatthew G. Knepley     }
2341b68eb51SMatthew G. Knepley     ierr = PetscHSetIGetSize(ranksUniq, &numRanks);CHKERRQ(ierr);
2354bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(numRanks, &remote);CHKERRQ(ierr);
2364bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(numRanks, &ranks);CHKERRQ(ierr);
2371b68eb51SMatthew G. Knepley     ierr = PetscHSetIGetElems(ranksUniq, &index, ranks);CHKERRQ(ierr);
2384bbf5ea8SMatthew G. Knepley 
2391b68eb51SMatthew G. Knepley     ierr = PetscHMapICreate(&rankToIndex);CHKERRQ(ierr);
2404bbf5ea8SMatthew G. Knepley     for (i = 0; i < numRanks; ++i) {
2414bbf5ea8SMatthew G. Knepley       remote[i].rank  = ranks[i];
2424bbf5ea8SMatthew G. Knepley       remote[i].index = 0;
2431b68eb51SMatthew G. Knepley       ierr = PetscHMapISet(rankToIndex, ranks[i], i);CHKERRQ(ierr);
2444bbf5ea8SMatthew G. Knepley     }
2454bbf5ea8SMatthew G. Knepley     ierr = PetscFree(ranks);CHKERRQ(ierr);
2461b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&ranksUniq);CHKERRQ(ierr);
2474bbf5ea8SMatthew G. Knepley     ierr = PetscSFCreate(PetscObjectComm((PetscObject) pc), &rankSF);CHKERRQ(ierr);
2484bbf5ea8SMatthew G. Knepley     ierr = PetscSFSetGraph(rankSF, 1, numRanks, NULL, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
2494bbf5ea8SMatthew G. Knepley     ierr = PetscSFSetUp(rankSF);CHKERRQ(ierr);
2504bbf5ea8SMatthew G. Knepley     /* OK, use it to communicate the root offset on the remote
2514bbf5ea8SMatthew G. Knepley      * processes for each subspace. */
2524bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(n, &offsets);CHKERRQ(ierr);
2534bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(n*numRanks, &remoteOffsets);CHKERRQ(ierr);
2544bbf5ea8SMatthew G. Knepley 
2554bbf5ea8SMatthew G. Knepley     offsets[0] = 0;
2564bbf5ea8SMatthew G. Knepley     for (i = 1; i < n; ++i) {
2574bbf5ea8SMatthew G. Knepley       PetscInt nroots;
2584bbf5ea8SMatthew G. Knepley 
2594bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetGraph(sf[i-1], &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
2604bbf5ea8SMatthew G. Knepley       offsets[i] = offsets[i-1] + nroots*bs[i-1];
2614bbf5ea8SMatthew G. Knepley     }
2624bbf5ea8SMatthew G. Knepley     /* Offsets are the offsets on the current process of the
2634bbf5ea8SMatthew G. Knepley      * global dof numbering for the subspaces. */
264ffc4695bSBarry Smith     ierr = MPI_Type_contiguous(n, MPIU_INT, &contig);CHKERRMPI(ierr);
265ffc4695bSBarry Smith     ierr = MPI_Type_commit(&contig);CHKERRMPI(ierr);
2664bbf5ea8SMatthew G. Knepley 
267ad227feaSJunchao Zhang     ierr = PetscSFBcastBegin(rankSF, contig, offsets, remoteOffsets,MPI_REPLACE);CHKERRQ(ierr);
268ad227feaSJunchao Zhang     ierr = PetscSFBcastEnd(rankSF, contig, offsets, remoteOffsets,MPI_REPLACE);CHKERRQ(ierr);
269ffc4695bSBarry Smith     ierr = MPI_Type_free(&contig);CHKERRMPI(ierr);
2704bbf5ea8SMatthew G. Knepley     ierr = PetscFree(offsets);CHKERRQ(ierr);
2714bbf5ea8SMatthew G. Knepley     ierr = PetscSFDestroy(&rankSF);CHKERRQ(ierr);
2724bbf5ea8SMatthew G. Knepley     /* Now remoteOffsets contains the offsets on the remote
2734bbf5ea8SMatthew G. Knepley      * processes who communicate with me.  So now we can
2744bbf5ea8SMatthew G. Knepley      * concatenate the list of SFs into a single one. */
2754bbf5ea8SMatthew G. Knepley     index = 0;
2764bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2774bbf5ea8SMatthew G. Knepley       const PetscSFNode *remote = NULL;
2784bbf5ea8SMatthew G. Knepley       const PetscInt    *local  = NULL;
2794bbf5ea8SMatthew G. Knepley       PetscInt           nroots, nleaves, j;
2804bbf5ea8SMatthew G. Knepley 
2814bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetGraph(sf[i], &nroots, &nleaves, &local, &remote);CHKERRQ(ierr);
2824bbf5ea8SMatthew G. Knepley       for (j = 0; j < nleaves; ++j) {
2834bbf5ea8SMatthew G. Knepley         PetscInt rank = remote[j].rank;
2844bbf5ea8SMatthew G. Knepley         PetscInt idx, rootOffset, k;
2854bbf5ea8SMatthew G. Knepley 
2861b68eb51SMatthew G. Knepley         ierr = PetscHMapIGet(rankToIndex, rank, &idx);CHKERRQ(ierr);
2874bbf5ea8SMatthew G. Knepley         if (idx == -1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Didn't find rank, huh?");
2884bbf5ea8SMatthew G. Knepley         /* Offset on given rank for ith subspace */
2894bbf5ea8SMatthew G. Knepley         rootOffset = remoteOffsets[n*idx + i];
2904bbf5ea8SMatthew G. Knepley         for (k = 0; k < bs[i]; ++k) {
29173ec7555SLawrence Mitchell           ilocal[index]        = (local ? local[j] : j)*bs[i] + k + leafOffset;
2924bbf5ea8SMatthew G. Knepley           iremote[index].rank  = remote[j].rank;
2934bbf5ea8SMatthew G. Knepley           iremote[index].index = remote[j].index*bs[i] + k + rootOffset;
2944bbf5ea8SMatthew G. Knepley           ++index;
2954bbf5ea8SMatthew G. Knepley         }
2964bbf5ea8SMatthew G. Knepley       }
2974bbf5ea8SMatthew G. Knepley       leafOffset += nleaves * bs[i];
2984bbf5ea8SMatthew G. Knepley     }
2991b68eb51SMatthew G. Knepley     ierr = PetscHMapIDestroy(&rankToIndex);CHKERRQ(ierr);
3004bbf5ea8SMatthew G. Knepley     ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
3011bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)pc), &patch->sectionSF);CHKERRQ(ierr);
3021bb6d2a8SBarry Smith     ierr = PetscSFSetGraph(patch->sectionSF, allRoots, allLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER);CHKERRQ(ierr);
3034bbf5ea8SMatthew G. Knepley   }
3044bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3054bbf5ea8SMatthew G. Knepley }
3064bbf5ea8SMatthew G. Knepley 
307c73d2cf6SLawrence Mitchell PetscErrorCode PCPatchSetDenseInverse(PC pc, PetscBool flg)
308c73d2cf6SLawrence Mitchell {
309c73d2cf6SLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *) pc->data;
310c73d2cf6SLawrence Mitchell   PetscFunctionBegin;
311c73d2cf6SLawrence Mitchell   patch->denseinverse = flg;
312c73d2cf6SLawrence Mitchell   PetscFunctionReturn(0);
313c73d2cf6SLawrence Mitchell }
314c73d2cf6SLawrence Mitchell 
315c73d2cf6SLawrence Mitchell PetscErrorCode PCPatchGetDenseInverse(PC pc, PetscBool *flg)
316c73d2cf6SLawrence Mitchell {
317c73d2cf6SLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *) pc->data;
318c73d2cf6SLawrence Mitchell   PetscFunctionBegin;
319c73d2cf6SLawrence Mitchell   *flg = patch->denseinverse;
320c73d2cf6SLawrence Mitchell   PetscFunctionReturn(0);
321c73d2cf6SLawrence Mitchell }
322c73d2cf6SLawrence Mitchell 
3234bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3245f824522SMatthew G. Knepley PetscErrorCode PCPatchSetIgnoreDim(PC pc, PetscInt dim)
3255f824522SMatthew G. Knepley {
3265f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3275f824522SMatthew G. Knepley   PetscFunctionBegin;
3285f824522SMatthew G. Knepley   patch->ignoredim = dim;
3295f824522SMatthew G. Knepley   PetscFunctionReturn(0);
3305f824522SMatthew G. Knepley }
3315f824522SMatthew G. Knepley 
3325f824522SMatthew G. Knepley /* TODO: Docs */
3335f824522SMatthew G. Knepley PetscErrorCode PCPatchGetIgnoreDim(PC pc, PetscInt *dim)
3345f824522SMatthew G. Knepley {
3355f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3365f824522SMatthew G. Knepley   PetscFunctionBegin;
3375f824522SMatthew G. Knepley   *dim = patch->ignoredim;
3385f824522SMatthew G. Knepley   PetscFunctionReturn(0);
3395f824522SMatthew G. Knepley }
3405f824522SMatthew G. Knepley 
3415f824522SMatthew G. Knepley /* TODO: Docs */
3424bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetSaveOperators(PC pc, PetscBool flg)
3434bbf5ea8SMatthew G. Knepley {
3444bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3454bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3464bbf5ea8SMatthew G. Knepley   patch->save_operators = flg;
3474bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3484bbf5ea8SMatthew G. Knepley }
3494bbf5ea8SMatthew G. Knepley 
3504bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3514bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetSaveOperators(PC pc, PetscBool *flg)
3524bbf5ea8SMatthew G. Knepley {
3534bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3544bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3554bbf5ea8SMatthew G. Knepley   *flg = patch->save_operators;
3564bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3574bbf5ea8SMatthew G. Knepley }
3584bbf5ea8SMatthew G. Knepley 
3594bbf5ea8SMatthew G. Knepley /* TODO: Docs */
360fa84ea4cSLawrence Mitchell PetscErrorCode PCPatchSetPrecomputeElementTensors(PC pc, PetscBool flg)
361fa84ea4cSLawrence Mitchell {
362fa84ea4cSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *) pc->data;
363fa84ea4cSLawrence Mitchell   PetscFunctionBegin;
364fa84ea4cSLawrence Mitchell   patch->precomputeElementTensors = flg;
365fa84ea4cSLawrence Mitchell   PetscFunctionReturn(0);
366fa84ea4cSLawrence Mitchell }
367fa84ea4cSLawrence Mitchell 
368fa84ea4cSLawrence Mitchell /* TODO: Docs */
369fa84ea4cSLawrence Mitchell PetscErrorCode PCPatchGetPrecomputeElementTensors(PC pc, PetscBool *flg)
370fa84ea4cSLawrence Mitchell {
371fa84ea4cSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *) pc->data;
372fa84ea4cSLawrence Mitchell   PetscFunctionBegin;
373fa84ea4cSLawrence Mitchell   *flg = patch->precomputeElementTensors;
374fa84ea4cSLawrence Mitchell   PetscFunctionReturn(0);
375fa84ea4cSLawrence Mitchell }
376fa84ea4cSLawrence Mitchell 
377fa84ea4cSLawrence Mitchell /* TODO: Docs */
3784bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetPartitionOfUnity(PC pc, PetscBool flg)
3794bbf5ea8SMatthew G. Knepley {
3804bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3814bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3824bbf5ea8SMatthew G. Knepley   patch->partition_of_unity = flg;
3834bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3844bbf5ea8SMatthew G. Knepley }
3854bbf5ea8SMatthew G. Knepley 
3864bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3874bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetPartitionOfUnity(PC pc, PetscBool *flg)
3884bbf5ea8SMatthew G. Knepley {
3894bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3904bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3914bbf5ea8SMatthew G. Knepley   *flg = patch->partition_of_unity;
3924bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3934bbf5ea8SMatthew G. Knepley }
3944bbf5ea8SMatthew G. Knepley 
3954bbf5ea8SMatthew G. Knepley /* TODO: Docs */
39661c4b389SFlorian Wechsung PetscErrorCode PCPatchSetLocalComposition(PC pc, PCCompositeType type)
397c2e6f3c0SFlorian Wechsung {
398c2e6f3c0SFlorian Wechsung   PC_PATCH *patch = (PC_PATCH *) pc->data;
399c2e6f3c0SFlorian Wechsung   PetscFunctionBegin;
40061c4b389SFlorian Wechsung   if (type != PC_COMPOSITE_ADDITIVE && type != PC_COMPOSITE_MULTIPLICATIVE) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Only supports additive or multiplicative as the local type");
40161c4b389SFlorian Wechsung   patch->local_composition_type = type;
402c2e6f3c0SFlorian Wechsung   PetscFunctionReturn(0);
403c2e6f3c0SFlorian Wechsung }
404c2e6f3c0SFlorian Wechsung 
405c2e6f3c0SFlorian Wechsung /* TODO: Docs */
40661c4b389SFlorian Wechsung PetscErrorCode PCPatchGetLocalComposition(PC pc, PCCompositeType *type)
407c2e6f3c0SFlorian Wechsung {
408c2e6f3c0SFlorian Wechsung   PC_PATCH *patch = (PC_PATCH *) pc->data;
409c2e6f3c0SFlorian Wechsung   PetscFunctionBegin;
41061c4b389SFlorian Wechsung   *type = patch->local_composition_type;
411c2e6f3c0SFlorian Wechsung   PetscFunctionReturn(0);
412c2e6f3c0SFlorian Wechsung }
413c2e6f3c0SFlorian Wechsung 
414c2e6f3c0SFlorian Wechsung /* TODO: Docs */
4154bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetSubMatType(PC pc, MatType sub_mat_type)
4164bbf5ea8SMatthew G. Knepley {
4174bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
4184bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
4194bbf5ea8SMatthew G. Knepley 
4204bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4214bbf5ea8SMatthew G. Knepley   if (patch->sub_mat_type) {ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr);}
4224bbf5ea8SMatthew G. Knepley   ierr = PetscStrallocpy(sub_mat_type, (char **) &patch->sub_mat_type);CHKERRQ(ierr);
4234bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4244bbf5ea8SMatthew G. Knepley }
4254bbf5ea8SMatthew G. Knepley 
4264bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4274bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetSubMatType(PC pc, MatType *sub_mat_type)
4284bbf5ea8SMatthew G. Knepley {
4294bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4304bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4314bbf5ea8SMatthew G. Knepley   *sub_mat_type = patch->sub_mat_type;
4324bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4334bbf5ea8SMatthew G. Knepley }
4344bbf5ea8SMatthew G. Knepley 
4354bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4364bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetCellNumbering(PC pc, PetscSection cellNumbering)
4374bbf5ea8SMatthew G. Knepley {
4384bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
4394bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
4404bbf5ea8SMatthew G. Knepley 
4414bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4424bbf5ea8SMatthew G. Knepley   patch->cellNumbering = cellNumbering;
4434bbf5ea8SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) cellNumbering);CHKERRQ(ierr);
4444bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4454bbf5ea8SMatthew G. Knepley }
4464bbf5ea8SMatthew G. Knepley 
4474bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4484bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetCellNumbering(PC pc, PetscSection *cellNumbering)
4494bbf5ea8SMatthew G. Knepley {
4504bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4514bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4524bbf5ea8SMatthew G. Knepley   *cellNumbering = patch->cellNumbering;
4534bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4544bbf5ea8SMatthew G. Knepley }
4554bbf5ea8SMatthew G. Knepley 
4564bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4574bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetConstructType(PC pc, PCPatchConstructType ctype, PetscErrorCode (*func)(PC, PetscInt *, IS **, IS *, void *), void *ctx)
4584bbf5ea8SMatthew G. Knepley {
4594bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4604bbf5ea8SMatthew G. Knepley 
4614bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4624bbf5ea8SMatthew G. Knepley   patch->ctype = ctype;
4634bbf5ea8SMatthew G. Knepley   switch (ctype) {
4644bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
46540c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
4664bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Star;
4674bbf5ea8SMatthew G. Knepley     break;
4684bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
46940c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
4704bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Vanka;
4714bbf5ea8SMatthew G. Knepley     break;
472e5b9877fSPatrick Farrell   case PC_PATCH_PARDECOMP:
4730a390943SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
474e5b9877fSPatrick Farrell     patch->patchconstructop = PCPatchConstruct_Pardecomp;
4750a390943SPatrick Farrell     break;
4764bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
4774bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
4784bbf5ea8SMatthew G. Knepley     patch->user_patches     = PETSC_TRUE;
4794bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_User;
480bdd9e0cdSPatrick Farrell     if (func) {
4814bbf5ea8SMatthew G. Knepley       patch->userpatchconstructionop = func;
4824bbf5ea8SMatthew G. Knepley       patch->userpatchconstructctx   = ctx;
483bdd9e0cdSPatrick Farrell     }
4844bbf5ea8SMatthew G. Knepley     break;
4854bbf5ea8SMatthew G. Knepley   default:
4864bbf5ea8SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype);
4874bbf5ea8SMatthew G. Knepley   }
4884bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4894bbf5ea8SMatthew G. Knepley }
4904bbf5ea8SMatthew G. Knepley 
4914bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4924bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetConstructType(PC pc, PCPatchConstructType *ctype, PetscErrorCode (**func)(PC, PetscInt *, IS **, IS *, void *), void **ctx)
4934bbf5ea8SMatthew G. Knepley {
4944bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4954bbf5ea8SMatthew G. Knepley 
4964bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4974bbf5ea8SMatthew G. Knepley   *ctype = patch->ctype;
4984bbf5ea8SMatthew G. Knepley   switch (patch->ctype) {
4994bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
5004bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
501e5b9877fSPatrick Farrell   case PC_PATCH_PARDECOMP:
5024bbf5ea8SMatthew G. Knepley     break;
5034bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
5044bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
5054bbf5ea8SMatthew G. Knepley     *func = patch->userpatchconstructionop;
5064bbf5ea8SMatthew G. Knepley     *ctx  = patch->userpatchconstructctx;
5074bbf5ea8SMatthew G. Knepley     break;
5084bbf5ea8SMatthew G. Knepley   default:
5094bbf5ea8SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype);
5104bbf5ea8SMatthew G. Knepley   }
5114bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5124bbf5ea8SMatthew G. Knepley }
5134bbf5ea8SMatthew G. Knepley 
5144bbf5ea8SMatthew G. Knepley /* TODO: Docs */
5154bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfo(PC pc, PetscInt nsubspaces, DM *dms, PetscInt *bs, PetscInt *nodesPerCell, const PetscInt **cellNodeMap,
5164bbf5ea8SMatthew G. Knepley                                             const PetscInt *subspaceOffsets, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
5174bbf5ea8SMatthew G. Knepley {
5184bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
519b6bb21d1SLawrence Mitchell   DM             dm, plex;
5204bbf5ea8SMatthew G. Knepley   PetscSF       *sfs;
5215f824522SMatthew G. Knepley   PetscInt       cStart, cEnd, i, j;
5224bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
5234bbf5ea8SMatthew G. Knepley 
5244bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
5255f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
526b6bb21d1SLawrence Mitchell   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
527b6bb21d1SLawrence Mitchell   dm = plex;
5285f824522SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
5294bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &sfs);CHKERRQ(ierr);
5304bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->dofSection);CHKERRQ(ierr);
5314bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->bs);CHKERRQ(ierr);
5324bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->nodesPerCell);CHKERRQ(ierr);
5334bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->cellNodeMap);CHKERRQ(ierr);
5344bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces+1, &patch->subspaceOffsets);CHKERRQ(ierr);
5354bbf5ea8SMatthew G. Knepley 
5364bbf5ea8SMatthew G. Knepley   patch->nsubspaces       = nsubspaces;
5374bbf5ea8SMatthew G. Knepley   patch->totalDofsPerCell = 0;
5384bbf5ea8SMatthew G. Knepley   for (i = 0; i < nsubspaces; ++i) {
53992fd8e1eSJed Brown     ierr = DMGetLocalSection(dms[i], &patch->dofSection[i]);CHKERRQ(ierr);
5404bbf5ea8SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) patch->dofSection[i]);CHKERRQ(ierr);
5411bb6d2a8SBarry Smith     ierr = DMGetSectionSF(dms[i], &sfs[i]);CHKERRQ(ierr);
5424bbf5ea8SMatthew G. Knepley     patch->bs[i]              = bs[i];
5434bbf5ea8SMatthew G. Knepley     patch->nodesPerCell[i]    = nodesPerCell[i];
5444bbf5ea8SMatthew G. Knepley     patch->totalDofsPerCell  += nodesPerCell[i]*bs[i];
54580e8a965SFlorian Wechsung     ierr = PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i]);CHKERRQ(ierr);
54680e8a965SFlorian Wechsung     for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
5474bbf5ea8SMatthew G. Knepley     patch->subspaceOffsets[i] = subspaceOffsets[i];
5484bbf5ea8SMatthew G. Knepley   }
5494bbf5ea8SMatthew G. Knepley   ierr = PCPatchCreateDefaultSF_Private(pc, nsubspaces, sfs, patch->bs);CHKERRQ(ierr);
5504bbf5ea8SMatthew G. Knepley   ierr = PetscFree(sfs);CHKERRQ(ierr);
5514bbf5ea8SMatthew G. Knepley 
5524bbf5ea8SMatthew G. Knepley   patch->subspaceOffsets[nsubspaces] = subspaceOffsets[nsubspaces];
5534bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes);CHKERRQ(ierr);
5544bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes);CHKERRQ(ierr);
555b6bb21d1SLawrence Mitchell   ierr = DMDestroy(&dm);CHKERRQ(ierr);
5564bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5574bbf5ea8SMatthew G. Knepley }
5584bbf5ea8SMatthew G. Knepley 
5594bbf5ea8SMatthew G. Knepley /* TODO: Docs */
5605f824522SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfoCombined(PC pc, DM dm, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
5615f824522SMatthew G. Knepley {
5625f824522SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
5635f824522SMatthew G. Knepley   PetscInt       cStart, cEnd, i, j;
5645f824522SMatthew G. Knepley   PetscErrorCode ierr;
5655f824522SMatthew G. Knepley 
5665f824522SMatthew G. Knepley   PetscFunctionBegin;
5675f824522SMatthew G. Knepley   patch->combined = PETSC_TRUE;
5685f824522SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
5695f824522SMatthew G. Knepley   ierr = DMGetNumFields(dm, &patch->nsubspaces);CHKERRQ(ierr);
5705f824522SMatthew G. Knepley   ierr = PetscCalloc1(patch->nsubspaces, &patch->dofSection);CHKERRQ(ierr);
5715f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->bs);CHKERRQ(ierr);
5725f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->nodesPerCell);CHKERRQ(ierr);
5735f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->cellNodeMap);CHKERRQ(ierr);
5745f824522SMatthew G. Knepley   ierr = PetscCalloc1(patch->nsubspaces+1, &patch->subspaceOffsets);CHKERRQ(ierr);
57592fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &patch->dofSection[0]);CHKERRQ(ierr);
5765f824522SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) patch->dofSection[0]);CHKERRQ(ierr);
5775f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(patch->dofSection[0], &patch->subspaceOffsets[patch->nsubspaces]);CHKERRQ(ierr);
5785f824522SMatthew G. Knepley   patch->totalDofsPerCell = 0;
5795f824522SMatthew G. Knepley   for (i = 0; i < patch->nsubspaces; ++i) {
5805f824522SMatthew G. Knepley     patch->bs[i]             = 1;
5815f824522SMatthew G. Knepley     patch->nodesPerCell[i]   = nodesPerCell[i];
5825f824522SMatthew G. Knepley     patch->totalDofsPerCell += nodesPerCell[i];
5835f824522SMatthew G. Knepley     ierr = PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i]);CHKERRQ(ierr);
5845f824522SMatthew G. Knepley     for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
5855f824522SMatthew G. Knepley   }
5861bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &patch->sectionSF);CHKERRQ(ierr);
5871bb6d2a8SBarry Smith   ierr = PetscObjectReference((PetscObject) patch->sectionSF);CHKERRQ(ierr);
5885f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes);CHKERRQ(ierr);
5895f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes);CHKERRQ(ierr);
5905f824522SMatthew G. Knepley   PetscFunctionReturn(0);
5915f824522SMatthew G. Knepley }
5925f824522SMatthew G. Knepley 
5935f824522SMatthew G. Knepley /*@C
5945f824522SMatthew G. Knepley 
59592d50984SMatthew G. Knepley   PCPatchSetComputeFunction - Set the callback used to compute patch residuals
59692d50984SMatthew G. Knepley 
59799b7e5c6SPatrick Farrell   Logically collective on PC
59899b7e5c6SPatrick Farrell 
59992d50984SMatthew G. Knepley   Input Parameters:
60092d50984SMatthew G. Knepley + pc   - The PC
60192d50984SMatthew G. Knepley . func - The callback
60292d50984SMatthew G. Knepley - ctx  - The user context
60392d50984SMatthew G. Knepley 
6047a50e09dSPatrick Farrell   Calling sequence of func:
6057a50e09dSPatrick Farrell $   func (PC pc,PetscInt point,Vec x,Vec f,IS cellIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx)
6067a50e09dSPatrick Farrell 
6077a50e09dSPatrick Farrell +  pc               - The PC
6087a50e09dSPatrick Farrell .  point            - The point
6097a50e09dSPatrick Farrell .  x                - The input solution (not used in linear problems)
6107a50e09dSPatrick Farrell .  f                - The patch residual vector
6117a50e09dSPatrick Farrell .  cellIS           - An array of the cell numbers
6127a50e09dSPatrick Farrell .  n                - The size of dofsArray
6137a50e09dSPatrick Farrell .  dofsArray        - The dofmap for the dofs to be solved for
6147a50e09dSPatrick Farrell .  dofsArrayWithAll - The dofmap for all dofs on the patch
6157a50e09dSPatrick Farrell -  ctx              - The user context
6167a50e09dSPatrick Farrell 
61792d50984SMatthew G. Knepley   Level: advanced
61892d50984SMatthew G. Knepley 
6197a50e09dSPatrick Farrell   Notes:
62026dc5b63SLawrence Mitchell   The entries of F (the output residual vector) have been set to zero before the call.
62192d50984SMatthew G. Knepley 
62226dc5b63SLawrence Mitchell .seealso: PCPatchSetComputeOperator(), PCPatchGetComputeOperator(), PCPatchSetDiscretisationInfo(), PCPatchSetComputeFunctionInteriorFacets()
62392d50984SMatthew G. Knepley @*/
62439fd2e8aSPatrick Farrell PetscErrorCode PCPatchSetComputeFunction(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Vec, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
62592d50984SMatthew G. Knepley {
62692d50984SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
62792d50984SMatthew G. Knepley 
62892d50984SMatthew G. Knepley   PetscFunctionBegin;
62992d50984SMatthew G. Knepley   patch->usercomputef    = func;
63092d50984SMatthew G. Knepley   patch->usercomputefctx = ctx;
63192d50984SMatthew G. Knepley   PetscFunctionReturn(0);
63292d50984SMatthew G. Knepley }
63392d50984SMatthew G. Knepley 
63492d50984SMatthew G. Knepley /*@C
63592d50984SMatthew G. Knepley 
63659109abcSLawrence Mitchell   PCPatchSetComputeFunctionInteriorFacets - Set the callback used to compute facet integrals for patch residuals
63759109abcSLawrence Mitchell 
6387a50e09dSPatrick Farrell   Logically collective on PC
6397a50e09dSPatrick Farrell 
64059109abcSLawrence Mitchell   Input Parameters:
64159109abcSLawrence Mitchell + pc   - The PC
64259109abcSLawrence Mitchell . func - The callback
64359109abcSLawrence Mitchell - ctx  - The user context
64459109abcSLawrence Mitchell 
6457a50e09dSPatrick Farrell   Calling sequence of func:
6467a50e09dSPatrick Farrell $   func (PC pc,PetscInt point,Vec x,Vec f,IS facetIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx)
6477a50e09dSPatrick Farrell 
6487a50e09dSPatrick Farrell +  pc               - The PC
6497a50e09dSPatrick Farrell .  point            - The point
6507a50e09dSPatrick Farrell .  x                - The input solution (not used in linear problems)
6517a50e09dSPatrick Farrell .  f                - The patch residual vector
6527a50e09dSPatrick Farrell .  facetIS          - An array of the facet numbers
6537a50e09dSPatrick Farrell .  n                - The size of dofsArray
6547a50e09dSPatrick Farrell .  dofsArray        - The dofmap for the dofs to be solved for
6557a50e09dSPatrick Farrell .  dofsArrayWithAll - The dofmap for all dofs on the patch
6567a50e09dSPatrick Farrell -  ctx              - The user context
6577a50e09dSPatrick Farrell 
65859109abcSLawrence Mitchell   Level: advanced
65959109abcSLawrence Mitchell 
6607a50e09dSPatrick Farrell   Notes:
66126dc5b63SLawrence Mitchell   The entries of F (the output residual vector) have been set to zero before the call.
66259109abcSLawrence Mitchell 
66326dc5b63SLawrence Mitchell .seealso: PCPatchSetComputeOperator(), PCPatchGetComputeOperator(), PCPatchSetDiscretisationInfo(), PCPatchSetComputeFunction()
66459109abcSLawrence Mitchell @*/
66559109abcSLawrence Mitchell PetscErrorCode PCPatchSetComputeFunctionInteriorFacets(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Vec, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
66659109abcSLawrence Mitchell {
66759109abcSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *) pc->data;
66859109abcSLawrence Mitchell 
66959109abcSLawrence Mitchell   PetscFunctionBegin;
67059109abcSLawrence Mitchell   patch->usercomputefintfacet    = func;
67159109abcSLawrence Mitchell   patch->usercomputefintfacetctx = ctx;
67259109abcSLawrence Mitchell   PetscFunctionReturn(0);
67359109abcSLawrence Mitchell }
67459109abcSLawrence Mitchell 
67559109abcSLawrence Mitchell /*@C
67659109abcSLawrence Mitchell 
6775f824522SMatthew G. Knepley   PCPatchSetComputeOperator - Set the callback used to compute patch matrices
6785f824522SMatthew G. Knepley 
6797a50e09dSPatrick Farrell   Logically collective on PC
6807a50e09dSPatrick Farrell 
6815f824522SMatthew G. Knepley   Input Parameters:
6825f824522SMatthew G. Knepley + pc   - The PC
6835f824522SMatthew G. Knepley . func - The callback
6845f824522SMatthew G. Knepley - ctx  - The user context
6855f824522SMatthew G. Knepley 
6867a50e09dSPatrick Farrell   Calling sequence of func:
6877a50e09dSPatrick Farrell $   func (PC pc,PetscInt point,Vec x,Mat mat,IS facetIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx)
6887a50e09dSPatrick Farrell 
6897a50e09dSPatrick Farrell +  pc               - The PC
6907a50e09dSPatrick Farrell .  point            - The point
6917a50e09dSPatrick Farrell .  x                - The input solution (not used in linear problems)
6927a50e09dSPatrick Farrell .  mat              - The patch matrix
6937a50e09dSPatrick Farrell .  cellIS           - An array of the cell numbers
6947a50e09dSPatrick Farrell .  n                - The size of dofsArray
6957a50e09dSPatrick Farrell .  dofsArray        - The dofmap for the dofs to be solved for
6967a50e09dSPatrick Farrell .  dofsArrayWithAll - The dofmap for all dofs on the patch
6977a50e09dSPatrick Farrell -  ctx              - The user context
6987a50e09dSPatrick Farrell 
6995f824522SMatthew G. Knepley   Level: advanced
7005f824522SMatthew G. Knepley 
7017a50e09dSPatrick Farrell   Notes:
7027a50e09dSPatrick Farrell   The matrix entries have been set to zero before the call.
7035f824522SMatthew G. Knepley 
704723f9013SMatthew G. Knepley .seealso: PCPatchGetComputeOperator(), PCPatchSetComputeFunction(), PCPatchSetDiscretisationInfo()
7055f824522SMatthew G. Knepley @*/
7064d04e9f1SPatrick Farrell PetscErrorCode PCPatchSetComputeOperator(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Mat, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
7074bbf5ea8SMatthew G. Knepley {
7084bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
7094bbf5ea8SMatthew G. Knepley 
7104bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
7114bbf5ea8SMatthew G. Knepley   patch->usercomputeop    = func;
712723f9013SMatthew G. Knepley   patch->usercomputeopctx = ctx;
7134bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
7144bbf5ea8SMatthew G. Knepley }
7154bbf5ea8SMatthew G. Knepley 
71659109abcSLawrence Mitchell /*@C
71759109abcSLawrence Mitchell 
7187a50e09dSPatrick Farrell   PCPatchSetComputeOperatorInteriorFacets - Set the callback used to compute facet integrals for patch matrices
71959109abcSLawrence Mitchell 
72099b7e5c6SPatrick Farrell   Logically collective on PC
72199b7e5c6SPatrick Farrell 
72259109abcSLawrence Mitchell   Input Parameters:
72359109abcSLawrence Mitchell + pc   - The PC
72459109abcSLawrence Mitchell . func - The callback
72559109abcSLawrence Mitchell - ctx  - The user context
72659109abcSLawrence Mitchell 
7277a50e09dSPatrick Farrell   Calling sequence of func:
7287a50e09dSPatrick Farrell $   func (PC pc,PetscInt point,Vec x,Mat mat,IS facetIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx)
7297a50e09dSPatrick Farrell 
7307a50e09dSPatrick Farrell +  pc               - The PC
7317a50e09dSPatrick Farrell .  point            - The point
7327a50e09dSPatrick Farrell .  x                - The input solution (not used in linear problems)
7337a50e09dSPatrick Farrell .  mat              - The patch matrix
7347a50e09dSPatrick Farrell .  facetIS          - An array of the facet numbers
7357a50e09dSPatrick Farrell .  n                - The size of dofsArray
7367a50e09dSPatrick Farrell .  dofsArray        - The dofmap for the dofs to be solved for
7377a50e09dSPatrick Farrell .  dofsArrayWithAll - The dofmap for all dofs on the patch
7387a50e09dSPatrick Farrell -  ctx              - The user context
7397a50e09dSPatrick Farrell 
74059109abcSLawrence Mitchell   Level: advanced
74159109abcSLawrence Mitchell 
7427a50e09dSPatrick Farrell   Notes:
7437a50e09dSPatrick Farrell   The matrix entries have been set to zero before the call.
74459109abcSLawrence Mitchell 
74559109abcSLawrence Mitchell .seealso: PCPatchGetComputeOperator(), PCPatchSetComputeFunction(), PCPatchSetDiscretisationInfo()
74659109abcSLawrence Mitchell @*/
74759109abcSLawrence Mitchell PetscErrorCode PCPatchSetComputeOperatorInteriorFacets(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Mat, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
74859109abcSLawrence Mitchell {
74959109abcSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *) pc->data;
75059109abcSLawrence Mitchell 
75159109abcSLawrence Mitchell   PetscFunctionBegin;
75259109abcSLawrence Mitchell   patch->usercomputeopintfacet    = func;
75359109abcSLawrence Mitchell   patch->usercomputeopintfacetctx = ctx;
75459109abcSLawrence Mitchell   PetscFunctionReturn(0);
75559109abcSLawrence Mitchell }
75659109abcSLawrence Mitchell 
7574bbf5ea8SMatthew G. Knepley /* On entry, ht contains the topological entities whose dofs we are responsible for solving for;
7584bbf5ea8SMatthew G. Knepley    on exit, cht contains all the topological entities we need to compute their residuals.
7594bbf5ea8SMatthew G. Knepley    In full generality this should incorporate knowledge of the sparsity pattern of the matrix;
7604bbf5ea8SMatthew G. Knepley    here we assume a standard FE sparsity pattern.*/
7614bbf5ea8SMatthew G. Knepley /* TODO: Use DMPlexGetAdjacency() */
7621b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchCompleteCellPatch(PC pc, PetscHSetI ht, PetscHSetI cht)
7634bbf5ea8SMatthew G. Knepley {
764b6bb21d1SLawrence Mitchell   DM             dm, plex;
765bc7fa33aSFlorian Wechsung   PC_PATCH      *patch = (PC_PATCH *) pc->data;
7661b68eb51SMatthew G. Knepley   PetscHashIter  hi;
7674bbf5ea8SMatthew G. Knepley   PetscInt       point;
7684bbf5ea8SMatthew G. Knepley   PetscInt      *star = NULL, *closure = NULL;
7694c954380SMatthew G. Knepley   PetscInt       ignoredim, iStart = 0, iEnd = -1, starSize, closureSize, si, ci;
770bc7fa33aSFlorian Wechsung   PetscInt      *fStar = NULL, *fClosure = NULL;
771bc7fa33aSFlorian Wechsung   PetscInt       fBegin, fEnd, fsi, fci, fStarSize, fClosureSize;
7724bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
7734bbf5ea8SMatthew G. Knepley 
7744bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
7755f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
776b6bb21d1SLawrence Mitchell   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
777b6bb21d1SLawrence Mitchell   dm = plex;
778bc7fa33aSFlorian Wechsung   ierr = DMPlexGetHeightStratum(dm, 1, &fBegin, &fEnd);CHKERRQ(ierr);
7795f824522SMatthew G. Knepley   ierr = PCPatchGetIgnoreDim(pc, &ignoredim);CHKERRQ(ierr);
7805f824522SMatthew G. Knepley   if (ignoredim >= 0) {ierr = DMPlexGetDepthStratum(dm, ignoredim, &iStart, &iEnd);CHKERRQ(ierr);}
7811b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(cht);CHKERRQ(ierr);
7821b68eb51SMatthew G. Knepley   PetscHashIterBegin(ht, hi);
7831b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(ht, hi)) {
7844c954380SMatthew G. Knepley 
7851b68eb51SMatthew G. Knepley     PetscHashIterGetKey(ht, hi, point);
7861b68eb51SMatthew G. Knepley     PetscHashIterNext(ht, hi);
7874bbf5ea8SMatthew G. Knepley 
7884bbf5ea8SMatthew G. Knepley     /* Loop over all the cells that this point connects to */
7894bbf5ea8SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
7905f824522SMatthew G. Knepley     for (si = 0; si < starSize*2; si += 2) {
7914c954380SMatthew G. Knepley       const PetscInt ownedpoint = star[si];
7925f824522SMatthew G. Knepley       /* TODO Check for point in cht before running through closure again */
7934bbf5ea8SMatthew G. Knepley       /* now loop over all entities in the closure of that cell */
7944bbf5ea8SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, ownedpoint, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
7955f824522SMatthew G. Knepley       for (ci = 0; ci < closureSize*2; ci += 2) {
7964c954380SMatthew G. Knepley         const PetscInt seenpoint = closure[ci];
7975f824522SMatthew G. Knepley         if (ignoredim >= 0 && seenpoint >= iStart && seenpoint < iEnd) continue;
7981b68eb51SMatthew G. Knepley         ierr = PetscHSetIAdd(cht, seenpoint);CHKERRQ(ierr);
799bc7fa33aSFlorian Wechsung         /* Facet integrals couple dofs across facets, so in that case for each of
800bc7fa33aSFlorian Wechsung          * the facets we need to add all dofs on the other side of the facet to
801bc7fa33aSFlorian Wechsung          * the seen dofs. */
802bc7fa33aSFlorian Wechsung         if (patch->usercomputeopintfacet) {
803bc7fa33aSFlorian Wechsung           if (fBegin <= seenpoint && seenpoint < fEnd) {
804bc7fa33aSFlorian Wechsung             ierr = DMPlexGetTransitiveClosure(dm, seenpoint, PETSC_FALSE, &fStarSize, &fStar);CHKERRQ(ierr);
805bc7fa33aSFlorian Wechsung             for (fsi = 0; fsi < fStarSize*2; fsi += 2) {
806bc7fa33aSFlorian Wechsung               ierr = DMPlexGetTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, &fClosureSize, &fClosure);CHKERRQ(ierr);
807bc7fa33aSFlorian Wechsung               for (fci = 0; fci < fClosureSize*2; fci += 2) {
808bc7fa33aSFlorian Wechsung                 ierr = PetscHSetIAdd(cht, fClosure[fci]);CHKERRQ(ierr);
809bc7fa33aSFlorian Wechsung               }
810bc7fa33aSFlorian Wechsung               ierr = DMPlexRestoreTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, NULL, &fClosure);CHKERRQ(ierr);
811bc7fa33aSFlorian Wechsung             }
812bc7fa33aSFlorian Wechsung             ierr = DMPlexRestoreTransitiveClosure(dm, seenpoint, PETSC_FALSE, NULL, &fStar);CHKERRQ(ierr);
813bc7fa33aSFlorian Wechsung           }
814bc7fa33aSFlorian Wechsung         }
8154bbf5ea8SMatthew G. Knepley       }
8162d76c0eeSPatrick Farrell       ierr = DMPlexRestoreTransitiveClosure(dm, ownedpoint, PETSC_TRUE, NULL, &closure);CHKERRQ(ierr);
8174bbf5ea8SMatthew G. Knepley     }
8182d76c0eeSPatrick Farrell     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, NULL, &star);CHKERRQ(ierr);
8194bbf5ea8SMatthew G. Knepley   }
820b6bb21d1SLawrence Mitchell   ierr = DMDestroy(&dm);CHKERRQ(ierr);
8215f824522SMatthew G. Knepley   PetscFunctionReturn(0);
8225f824522SMatthew G. Knepley }
8235f824522SMatthew G. Knepley 
8245f824522SMatthew G. Knepley static PetscErrorCode PCPatchGetGlobalDofs(PC pc, PetscSection dofSection[], PetscInt f, PetscBool combined, PetscInt p, PetscInt *dof, PetscInt *off)
8255f824522SMatthew G. Knepley {
8265f824522SMatthew G. Knepley   PetscErrorCode ierr;
8275f824522SMatthew G. Knepley 
8285f824522SMatthew G. Knepley   PetscFunctionBegin;
8295f824522SMatthew G. Knepley   if (combined) {
8305f824522SMatthew G. Knepley     if (f < 0) {
8315f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetDof(dofSection[0], p, dof);CHKERRQ(ierr);}
8325f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetOffset(dofSection[0], p, off);CHKERRQ(ierr);}
8335f824522SMatthew G. Knepley     } else {
8345f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetFieldDof(dofSection[0], p, f, dof);CHKERRQ(ierr);}
8355f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetFieldOffset(dofSection[0], p, f, off);CHKERRQ(ierr);}
8365f824522SMatthew G. Knepley     }
8375f824522SMatthew G. Knepley   } else {
8385f824522SMatthew G. Knepley     if (f < 0) {
8395f824522SMatthew G. Knepley       PC_PATCH *patch = (PC_PATCH *) pc->data;
8405f824522SMatthew G. Knepley       PetscInt  fdof, g;
8415f824522SMatthew G. Knepley 
8425f824522SMatthew G. Knepley       if (dof) {
8435f824522SMatthew G. Knepley         *dof = 0;
8445f824522SMatthew G. Knepley         for (g = 0; g < patch->nsubspaces; ++g) {
8455f824522SMatthew G. Knepley           ierr = PetscSectionGetDof(dofSection[g], p, &fdof);CHKERRQ(ierr);
8465f824522SMatthew G. Knepley           *dof += fdof;
8475f824522SMatthew G. Knepley         }
8485f824522SMatthew G. Knepley       }
849624e31c3SLawrence Mitchell       if (off) {
850624e31c3SLawrence Mitchell         *off = 0;
851624e31c3SLawrence Mitchell         for (g = 0; g < patch->nsubspaces; ++g) {
852624e31c3SLawrence Mitchell           ierr = PetscSectionGetOffset(dofSection[g], p, &fdof);CHKERRQ(ierr);
853624e31c3SLawrence Mitchell           *off += fdof;
854624e31c3SLawrence Mitchell         }
855624e31c3SLawrence Mitchell       }
8565f824522SMatthew G. Knepley     } else {
8575f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetDof(dofSection[f], p, dof);CHKERRQ(ierr);}
8585f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetOffset(dofSection[f], p, off);CHKERRQ(ierr);}
8595f824522SMatthew G. Knepley     }
8605f824522SMatthew G. Knepley   }
8614bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
8624bbf5ea8SMatthew G. Knepley }
8634bbf5ea8SMatthew G. Knepley 
8644bbf5ea8SMatthew G. Knepley /* Given a hash table with a set of topological entities (pts), compute the degrees of
8654bbf5ea8SMatthew G. Knepley    freedom in global concatenated numbering on those entities.
8664bbf5ea8SMatthew G. Knepley    For Vanka smoothing, this needs to do something special: ignore dofs of the
8674bbf5ea8SMatthew G. Knepley    constraint subspace on entities that aren't the base entity we're building the patch
8684bbf5ea8SMatthew G. Knepley    around. */
869e4c66b91SPatrick Farrell static PetscErrorCode PCPatchGetPointDofs(PC pc, PetscHSetI pts, PetscHSetI dofs, PetscInt base, PetscHSetI* subspaces_to_exclude)
8704bbf5ea8SMatthew G. Knepley {
8715f824522SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
8721b68eb51SMatthew G. Knepley   PetscHashIter  hi;
8734bbf5ea8SMatthew G. Knepley   PetscInt       ldof, loff;
8744bbf5ea8SMatthew G. Knepley   PetscInt       k, p;
8754bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
8764bbf5ea8SMatthew G. Knepley 
8774bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
8781b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(dofs);CHKERRQ(ierr);
8794bbf5ea8SMatthew G. Knepley   for (k = 0; k < patch->nsubspaces; ++k) {
8804bbf5ea8SMatthew G. Knepley     PetscInt subspaceOffset = patch->subspaceOffsets[k];
8814bbf5ea8SMatthew G. Knepley     PetscInt bs             = patch->bs[k];
8824bbf5ea8SMatthew G. Knepley     PetscInt j, l;
8834bbf5ea8SMatthew G. Knepley 
884e4c66b91SPatrick Farrell     if (subspaces_to_exclude != NULL) {
885e4c66b91SPatrick Farrell       PetscBool should_exclude_k = PETSC_FALSE;
886*2f613bf5SBarry Smith       ierr = PetscHSetIHas(*subspaces_to_exclude, k, &should_exclude_k);CHKERRQ(ierr);
887e4c66b91SPatrick Farrell       if (should_exclude_k) {
8884bbf5ea8SMatthew G. Knepley         /* only get this subspace dofs at the base entity, not any others */
8895f824522SMatthew G. Knepley         ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, base, &ldof, &loff);CHKERRQ(ierr);
8904bbf5ea8SMatthew G. Knepley         if (0 == ldof) continue;
8914bbf5ea8SMatthew G. Knepley         for (j = loff; j < ldof + loff; ++j) {
8924bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
8934bbf5ea8SMatthew G. Knepley             PetscInt dof = bs*j + l + subspaceOffset;
8941b68eb51SMatthew G. Knepley             ierr = PetscHSetIAdd(dofs, dof);CHKERRQ(ierr);
8954bbf5ea8SMatthew G. Knepley           }
8964bbf5ea8SMatthew G. Knepley         }
8974bbf5ea8SMatthew G. Knepley         continue; /* skip the other dofs of this subspace */
8984bbf5ea8SMatthew G. Knepley       }
899e4c66b91SPatrick Farrell     }
9004bbf5ea8SMatthew G. Knepley 
9011b68eb51SMatthew G. Knepley     PetscHashIterBegin(pts, hi);
9021b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(pts, hi)) {
9031b68eb51SMatthew G. Knepley       PetscHashIterGetKey(pts, hi, p);
9041b68eb51SMatthew G. Knepley       PetscHashIterNext(pts, hi);
9055f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, p, &ldof, &loff);CHKERRQ(ierr);
9064bbf5ea8SMatthew G. Knepley       if (0 == ldof) continue;
9074bbf5ea8SMatthew G. Knepley       for (j = loff; j < ldof + loff; ++j) {
9084bbf5ea8SMatthew G. Knepley         for (l = 0; l < bs; ++l) {
9094bbf5ea8SMatthew G. Knepley           PetscInt dof = bs*j + l + subspaceOffset;
9101b68eb51SMatthew G. Knepley           ierr = PetscHSetIAdd(dofs, dof);CHKERRQ(ierr);
9114bbf5ea8SMatthew G. Knepley         }
9124bbf5ea8SMatthew G. Knepley       }
9134bbf5ea8SMatthew G. Knepley     }
9144bbf5ea8SMatthew G. Knepley   }
9154bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
9164bbf5ea8SMatthew G. Knepley }
9174bbf5ea8SMatthew G. Knepley 
9184bbf5ea8SMatthew G. Knepley /* Given two hash tables A and B, compute the keys in B that are not in A, and put them in C */
9191b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchComputeSetDifference_Private(PetscHSetI A, PetscHSetI B, PetscHSetI C)
9204bbf5ea8SMatthew G. Knepley {
9211b68eb51SMatthew G. Knepley   PetscHashIter  hi;
9221b68eb51SMatthew G. Knepley   PetscInt       key;
9234bbf5ea8SMatthew G. Knepley   PetscBool      flg;
9241b68eb51SMatthew G. Knepley   PetscErrorCode ierr;
9254bbf5ea8SMatthew G. Knepley 
9264bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
9271b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(C);CHKERRQ(ierr);
9281b68eb51SMatthew G. Knepley   PetscHashIterBegin(B, hi);
9291b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(B, hi)) {
9301b68eb51SMatthew G. Knepley     PetscHashIterGetKey(B, hi, key);
9311b68eb51SMatthew G. Knepley     PetscHashIterNext(B, hi);
9321b68eb51SMatthew G. Knepley     ierr = PetscHSetIHas(A, key, &flg);CHKERRQ(ierr);
9331b68eb51SMatthew G. Knepley     if (!flg) {ierr = PetscHSetIAdd(C, key);CHKERRQ(ierr);}
9344bbf5ea8SMatthew G. Knepley   }
9354bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
9364bbf5ea8SMatthew G. Knepley }
9374bbf5ea8SMatthew G. Knepley 
9384bbf5ea8SMatthew G. Knepley /*
9394bbf5ea8SMatthew G. Knepley  * PCPatchCreateCellPatches - create patches.
9404bbf5ea8SMatthew G. Knepley  *
9414bbf5ea8SMatthew G. Knepley  * Input Parameters:
9424bbf5ea8SMatthew G. Knepley  * + dm - The DMPlex object defining the mesh
9434bbf5ea8SMatthew G. Knepley  *
9444bbf5ea8SMatthew G. Knepley  * Output Parameters:
9454bbf5ea8SMatthew G. Knepley  * + cellCounts  - Section with counts of cells around each vertex
9465f824522SMatthew G. Knepley  * . cells       - IS of the cell point indices of cells in each patch
9475f824522SMatthew G. Knepley  * . pointCounts - Section with counts of cells around each vertex
9485f824522SMatthew G. Knepley  * - point       - IS of the cell point indices of cells in each patch
9494bbf5ea8SMatthew G. Knepley  */
9504bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatches(PC pc)
9514bbf5ea8SMatthew G. Knepley {
9524bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
9535f824522SMatthew G. Knepley   DMLabel         ghost = NULL;
9544bbf5ea8SMatthew G. Knepley   DM              dm, plex;
9551b68eb51SMatthew G. Knepley   PetscHSetI      ht, cht;
9560e126c0bSLawrence Mitchell   PetscSection    cellCounts,  pointCounts, intFacetCounts, extFacetCounts;
957eb62eeaaSLawrence Mitchell   PetscInt       *cellsArray, *pointsArray, *intFacetsArray, *extFacetsArray, *intFacetsToPatchCell;
9580e126c0bSLawrence Mitchell   PetscInt        numCells, numPoints, numIntFacets, numExtFacets;
9595f824522SMatthew G. Knepley   const PetscInt *leaves;
96033cbca70SPatrick Farrell   PetscInt        nleaves, pStart, pEnd, cStart, cEnd, vStart, vEnd, fStart, fEnd, v;
9615f824522SMatthew G. Knepley   PetscBool       isFiredrake;
9624bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
9634bbf5ea8SMatthew G. Knepley 
9644bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
9654bbf5ea8SMatthew G. Knepley   /* Used to keep track of the cells in the patch. */
9661b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&ht);CHKERRQ(ierr);
9671b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&cht);CHKERRQ(ierr);
9684bbf5ea8SMatthew G. Knepley 
9694bbf5ea8SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
9704bbf5ea8SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONGSTATE, "DM not yet set on patch PC\n");
9714bbf5ea8SMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
972b6bb21d1SLawrence Mitchell   dm = plex;
973b6bb21d1SLawrence Mitchell   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
974b6bb21d1SLawrence Mitchell   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
9754bbf5ea8SMatthew G. Knepley 
9764bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
9775f824522SMatthew G. Knepley     ierr = patch->userpatchconstructionop(pc, &patch->npatch, &patch->userIS, &patch->iterationSet, patch->userpatchconstructctx);CHKERRQ(ierr);
9785f824522SMatthew G. Knepley     vStart = 0; vEnd = patch->npatch;
979e5b9877fSPatrick Farrell   } else if (patch->ctype == PC_PATCH_PARDECOMP) {
9800a390943SPatrick Farrell     vStart = 0; vEnd = 1;
9815f824522SMatthew G. Knepley   } else if (patch->codim < 0) {
982b6bb21d1SLawrence Mitchell     if (patch->dim < 0) {ierr = DMPlexGetDepthStratum(dm,  0,            &vStart, &vEnd);CHKERRQ(ierr);}
983b6bb21d1SLawrence Mitchell     else                {ierr = DMPlexGetDepthStratum(dm,  patch->dim,   &vStart, &vEnd);CHKERRQ(ierr);}
984b6bb21d1SLawrence Mitchell   } else                {ierr = DMPlexGetHeightStratum(dm, patch->codim, &vStart, &vEnd);CHKERRQ(ierr);}
9855f824522SMatthew G. Knepley   patch->npatch = vEnd - vStart;
9864bbf5ea8SMatthew G. Knepley 
9874bbf5ea8SMatthew G. Knepley   /* These labels mark the owned points.  We only create patches around points that this process owns. */
9885f824522SMatthew G. Knepley   ierr = DMHasLabel(dm, "pyop2_ghost", &isFiredrake);CHKERRQ(ierr);
9895f824522SMatthew G. Knepley   if (isFiredrake) {
9904bbf5ea8SMatthew G. Knepley     ierr = DMGetLabel(dm, "pyop2_ghost", &ghost);CHKERRQ(ierr);
9914bbf5ea8SMatthew G. Knepley     ierr = DMLabelCreateIndex(ghost, pStart, pEnd);CHKERRQ(ierr);
9925f824522SMatthew G. Knepley   } else {
9935f824522SMatthew G. Knepley     PetscSF sf;
9945f824522SMatthew G. Knepley 
9955f824522SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
9965f824522SMatthew G. Knepley     ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
9975f824522SMatthew G. Knepley     nleaves = PetscMax(nleaves, 0);
9985f824522SMatthew G. Knepley   }
9994bbf5ea8SMatthew G. Knepley 
10004bbf5ea8SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->cellCounts);CHKERRQ(ierr);
10015f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->cellCounts, "Patch Cell Layout");CHKERRQ(ierr);
10024bbf5ea8SMatthew G. Knepley   cellCounts = patch->cellCounts;
10034bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetChart(cellCounts, vStart, vEnd);CHKERRQ(ierr);
10045f824522SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->pointCounts);CHKERRQ(ierr);
10055f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->pointCounts, "Patch Point Layout");CHKERRQ(ierr);
10065f824522SMatthew G. Knepley   pointCounts = patch->pointCounts;
10075f824522SMatthew G. Knepley   ierr = PetscSectionSetChart(pointCounts, vStart, vEnd);CHKERRQ(ierr);
10080e126c0bSLawrence Mitchell   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->extFacetCounts);CHKERRQ(ierr);
10090e126c0bSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->extFacetCounts, "Patch Exterior Facet Layout");CHKERRQ(ierr);
10100e126c0bSLawrence Mitchell   extFacetCounts = patch->extFacetCounts;
10110e126c0bSLawrence Mitchell   ierr = PetscSectionSetChart(extFacetCounts, vStart, vEnd);CHKERRQ(ierr);
10120e126c0bSLawrence Mitchell   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->intFacetCounts);CHKERRQ(ierr);
10130e126c0bSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->intFacetCounts, "Patch Interior Facet Layout");CHKERRQ(ierr);
10140e126c0bSLawrence Mitchell   intFacetCounts = patch->intFacetCounts;
10150e126c0bSLawrence Mitchell   ierr = PetscSectionSetChart(intFacetCounts, vStart, vEnd);CHKERRQ(ierr);
10165f824522SMatthew G. Knepley   /* Count cells and points in the patch surrounding each entity */
10170e126c0bSLawrence Mitchell   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
10184bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
10191b68eb51SMatthew G. Knepley     PetscHashIter hi;
10205f824522SMatthew G. Knepley     PetscInt       chtSize, loc = -1;
10215f824522SMatthew G. Knepley     PetscBool      flg;
10224bbf5ea8SMatthew G. Knepley 
1023b525f888SPatrick Farrell     if (!patch->user_patches && patch->ctype != PC_PATCH_PARDECOMP) {
10245f824522SMatthew G. Knepley       if (ghost) {ierr = DMLabelHasPoint(ghost, v, &flg);CHKERRQ(ierr);}
1025928bb9adSStefano Zampini       else       {ierr = PetscFindInt(v, nleaves, leaves, &loc);CHKERRQ(ierr); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;}
10264bbf5ea8SMatthew G. Knepley       /* Not an owned entity, don't make a cell patch. */
10274bbf5ea8SMatthew G. Knepley       if (flg) continue;
10284bbf5ea8SMatthew G. Knepley     }
10294bbf5ea8SMatthew G. Knepley 
10304bbf5ea8SMatthew G. Knepley     ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr);
10315f824522SMatthew G. Knepley     ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr);
10321b68eb51SMatthew G. Knepley     ierr = PetscHSetIGetSize(cht, &chtSize);CHKERRQ(ierr);
10334bbf5ea8SMatthew G. Knepley     /* empty patch, continue */
10344bbf5ea8SMatthew G. Knepley     if (chtSize == 0) continue;
10354bbf5ea8SMatthew G. Knepley 
10364bbf5ea8SMatthew G. Knepley     /* safe because size(cht) > 0 from above */
10371b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
10381b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
10395f824522SMatthew G. Knepley       PetscInt point, pdof;
10404bbf5ea8SMatthew G. Knepley 
10411b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
10420e126c0bSLawrence Mitchell       if (fStart <= point && point < fEnd) {
10430e126c0bSLawrence Mitchell         const PetscInt *support;
10440e126c0bSLawrence Mitchell         PetscInt supportSize, p;
10450e126c0bSLawrence Mitchell         PetscBool interior = PETSC_TRUE;
10460e126c0bSLawrence Mitchell         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
10470e126c0bSLawrence Mitchell         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
10480e126c0bSLawrence Mitchell         if (supportSize == 1) {
10490e126c0bSLawrence Mitchell           interior = PETSC_FALSE;
10500e126c0bSLawrence Mitchell         } else {
10510e126c0bSLawrence Mitchell           for (p = 0; p < supportSize; p++) {
10520e126c0bSLawrence Mitchell             PetscBool found;
10530e126c0bSLawrence Mitchell             /* FIXME: can I do this while iterating over cht? */
1054*2f613bf5SBarry Smith             ierr = PetscHSetIHas(cht, support[p], &found);CHKERRQ(ierr);
10550e126c0bSLawrence Mitchell             if (!found) {
10560e126c0bSLawrence Mitchell               interior = PETSC_FALSE;
10570e126c0bSLawrence Mitchell               break;
10580e126c0bSLawrence Mitchell             }
10590e126c0bSLawrence Mitchell           }
10600e126c0bSLawrence Mitchell         }
10610e126c0bSLawrence Mitchell         if (interior) {
10620e126c0bSLawrence Mitchell           ierr = PetscSectionAddDof(intFacetCounts, v, 1);CHKERRQ(ierr);
10630e126c0bSLawrence Mitchell         } else {
10640e126c0bSLawrence Mitchell           ierr = PetscSectionAddDof(extFacetCounts, v, 1);CHKERRQ(ierr);
10650e126c0bSLawrence Mitchell         }
10660e126c0bSLawrence Mitchell       }
10675f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr);
10685f824522SMatthew G. Knepley       if (pdof)                            {ierr = PetscSectionAddDof(pointCounts, v, 1);CHKERRQ(ierr);}
10695f824522SMatthew G. Knepley       if (point >= cStart && point < cEnd) {ierr = PetscSectionAddDof(cellCounts, v, 1);CHKERRQ(ierr);}
10701b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
10714bbf5ea8SMatthew G. Knepley     }
10724bbf5ea8SMatthew G. Knepley   }
10735f824522SMatthew G. Knepley   if (isFiredrake) {ierr = DMLabelDestroyIndex(ghost);CHKERRQ(ierr);}
10744bbf5ea8SMatthew G. Knepley 
10754bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetUp(cellCounts);CHKERRQ(ierr);
10764bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr);
10774bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numCells, &cellsArray);CHKERRQ(ierr);
10785f824522SMatthew G. Knepley   ierr = PetscSectionSetUp(pointCounts);CHKERRQ(ierr);
10795f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(pointCounts, &numPoints);CHKERRQ(ierr);
10805f824522SMatthew G. Knepley   ierr = PetscMalloc1(numPoints, &pointsArray);CHKERRQ(ierr);
10814bbf5ea8SMatthew G. Knepley 
10820e126c0bSLawrence Mitchell   ierr = PetscSectionSetUp(intFacetCounts);CHKERRQ(ierr);
10830e126c0bSLawrence Mitchell   ierr = PetscSectionSetUp(extFacetCounts);CHKERRQ(ierr);
10840e126c0bSLawrence Mitchell   ierr = PetscSectionGetStorageSize(intFacetCounts, &numIntFacets);CHKERRQ(ierr);
10850e126c0bSLawrence Mitchell   ierr = PetscSectionGetStorageSize(extFacetCounts, &numExtFacets);CHKERRQ(ierr);
10860e126c0bSLawrence Mitchell   ierr = PetscMalloc1(numIntFacets, &intFacetsArray);CHKERRQ(ierr);
1087eb62eeaaSLawrence Mitchell   ierr = PetscMalloc1(numIntFacets*2, &intFacetsToPatchCell);CHKERRQ(ierr);
10880e126c0bSLawrence Mitchell   ierr = PetscMalloc1(numExtFacets, &extFacetsArray);CHKERRQ(ierr);
10890e126c0bSLawrence Mitchell 
10904bbf5ea8SMatthew G. Knepley   /* Now that we know how much space we need, run through again and actually remember the cells. */
10914bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; v++) {
10921b68eb51SMatthew G. Knepley     PetscHashIter hi;
10930e126c0bSLawrence Mitchell     PetscInt       dof, off, cdof, coff, efdof, efoff, ifdof, ifoff, pdof, n = 0, cn = 0, ifn = 0, efn = 0;
10944bbf5ea8SMatthew G. Knepley 
10955f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(pointCounts, v, &dof);CHKERRQ(ierr);
10965f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(pointCounts, v, &off);CHKERRQ(ierr);
10975f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &cdof);CHKERRQ(ierr);
10985f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &coff);CHKERRQ(ierr);
10990e126c0bSLawrence Mitchell     ierr = PetscSectionGetDof(intFacetCounts, v, &ifdof);CHKERRQ(ierr);
11000e126c0bSLawrence Mitchell     ierr = PetscSectionGetOffset(intFacetCounts, v, &ifoff);CHKERRQ(ierr);
11010e126c0bSLawrence Mitchell     ierr = PetscSectionGetDof(extFacetCounts, v, &efdof);CHKERRQ(ierr);
11020e126c0bSLawrence Mitchell     ierr = PetscSectionGetOffset(extFacetCounts, v, &efoff);CHKERRQ(ierr);
11035f824522SMatthew G. Knepley     if (dof <= 0) continue;
11044bbf5ea8SMatthew G. Knepley     ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr);
11055f824522SMatthew G. Knepley     ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr);
11061b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
11071b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
11084bbf5ea8SMatthew G. Knepley       PetscInt point;
11094bbf5ea8SMatthew G. Knepley 
11101b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
11110e126c0bSLawrence Mitchell       if (fStart <= point && point < fEnd) {
11120e126c0bSLawrence Mitchell         const PetscInt *support;
11130e126c0bSLawrence Mitchell         PetscInt       supportSize, p;
11140e126c0bSLawrence Mitchell         PetscBool      interior = PETSC_TRUE;
11150e126c0bSLawrence Mitchell         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
11160e126c0bSLawrence Mitchell         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
11170e126c0bSLawrence Mitchell         if (supportSize == 1) {
11180e126c0bSLawrence Mitchell           interior = PETSC_FALSE;
11190e126c0bSLawrence Mitchell         } else {
11200e126c0bSLawrence Mitchell           for (p = 0; p < supportSize; p++) {
11210e126c0bSLawrence Mitchell             PetscBool found;
11220e126c0bSLawrence Mitchell             /* FIXME: can I do this while iterating over cht? */
1123*2f613bf5SBarry Smith             ierr = PetscHSetIHas(cht, support[p], &found);CHKERRQ(ierr);
11240e126c0bSLawrence Mitchell             if (!found) {
11250e126c0bSLawrence Mitchell               interior = PETSC_FALSE;
11260e126c0bSLawrence Mitchell               break;
11270e126c0bSLawrence Mitchell             }
11280e126c0bSLawrence Mitchell           }
11290e126c0bSLawrence Mitchell         }
11300e126c0bSLawrence Mitchell         if (interior) {
113144b625f7SLawrence Mitchell           intFacetsToPatchCell[2*(ifoff + ifn)] = support[0];
113244b625f7SLawrence Mitchell           intFacetsToPatchCell[2*(ifoff + ifn) + 1] = support[1];
11330e126c0bSLawrence Mitchell           intFacetsArray[ifoff + ifn++] = point;
11340e126c0bSLawrence Mitchell         } else {
11350e126c0bSLawrence Mitchell           extFacetsArray[efoff + efn++] = point;
11360e126c0bSLawrence Mitchell         }
11370e126c0bSLawrence Mitchell       }
11385f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr);
11395f824522SMatthew G. Knepley       if (pdof)                            {pointsArray[off + n++] = point;}
11405f824522SMatthew G. Knepley       if (point >= cStart && point < cEnd) {cellsArray[coff + cn++] = point;}
11411b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
11424bbf5ea8SMatthew G. Knepley     }
11430e126c0bSLawrence Mitchell     if (ifn != ifdof) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of interior facets in patch %D is %D, but should be %D", v, ifn, ifdof);
11440e126c0bSLawrence Mitchell     if (efn != efdof) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of exterior facets in patch %D is %D, but should be %D", v, efn, efdof);
11455f824522SMatthew G. Knepley     if (cn != cdof) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of cells in patch %D is %D, but should be %D", v, cn, cdof);
11465f824522SMatthew G. Knepley     if (n  != dof)  SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of points in patch %D is %D, but should be %D", v, n, dof);
1147eb62eeaaSLawrence Mitchell 
1148eb62eeaaSLawrence Mitchell     for (ifn = 0; ifn < ifdof; ifn++) {
114944b625f7SLawrence Mitchell       PetscInt  cell0 = intFacetsToPatchCell[2*(ifoff + ifn)];
115044b625f7SLawrence Mitchell       PetscInt  cell1 = intFacetsToPatchCell[2*(ifoff + ifn) + 1];
1151eb62eeaaSLawrence Mitchell       PetscBool found0 = PETSC_FALSE, found1 = PETSC_FALSE;
1152eb62eeaaSLawrence Mitchell       for (n = 0; n < cdof; n++) {
11537c54fef0SLawrence Mitchell         if (!found0 && cell0 == cellsArray[coff + n]) {
1154c3faab33SLawrence Mitchell           intFacetsToPatchCell[2*(ifoff + ifn)] = n;
1155eb62eeaaSLawrence Mitchell           found0 = PETSC_TRUE;
1156eb62eeaaSLawrence Mitchell         }
11577c54fef0SLawrence Mitchell         if (!found1 && cell1 == cellsArray[coff + n]) {
1158c3faab33SLawrence Mitchell           intFacetsToPatchCell[2*(ifoff + ifn) + 1] = n;
115980fc4459SLawrence Mitchell           found1 = PETSC_TRUE;
1160eb62eeaaSLawrence Mitchell         }
1161eb62eeaaSLawrence Mitchell         if (found0 && found1) break;
1162eb62eeaaSLawrence Mitchell       }
116344b625f7SLawrence Mitchell       if (!(found0 && found1)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Didn't manage to find local point numbers for facet support");
1164eb62eeaaSLawrence Mitchell     }
11654bbf5ea8SMatthew G. Knepley   }
11661b68eb51SMatthew G. Knepley   ierr = PetscHSetIDestroy(&ht);CHKERRQ(ierr);
11671b68eb51SMatthew G. Knepley   ierr = PetscHSetIDestroy(&cht);CHKERRQ(ierr);
11685f824522SMatthew G. Knepley 
11695f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numCells,  cellsArray,  PETSC_OWN_POINTER, &patch->cells);CHKERRQ(ierr);
11705f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->cells,  "Patch Cells");CHKERRQ(ierr);
11715f824522SMatthew G. Knepley   if (patch->viewCells) {
11725f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->cellCounts, patch->viewerCells, patch->formatCells);CHKERRQ(ierr);
11735f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->cells,      patch->viewerCells, patch->formatCells);CHKERRQ(ierr);
11745f824522SMatthew G. Knepley   }
11750e126c0bSLawrence Mitchell   ierr = ISCreateGeneral(PETSC_COMM_SELF, numIntFacets,  intFacetsArray,  PETSC_OWN_POINTER, &patch->intFacets);CHKERRQ(ierr);
11760e126c0bSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->intFacets,  "Patch Interior Facets");CHKERRQ(ierr);
1177eb62eeaaSLawrence Mitchell   ierr = ISCreateGeneral(PETSC_COMM_SELF, 2*numIntFacets, intFacetsToPatchCell, PETSC_OWN_POINTER, &patch->intFacetsToPatchCell);CHKERRQ(ierr);
1178eb62eeaaSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->intFacetsToPatchCell,  "Patch Interior Facets local support");CHKERRQ(ierr);
11790e126c0bSLawrence Mitchell   if (patch->viewIntFacets) {
1180fbb82ab5SLawrence Mitchell     ierr = ObjectView((PetscObject) patch->intFacetCounts,       patch->viewerIntFacets, patch->formatIntFacets);CHKERRQ(ierr);
11810e126c0bSLawrence Mitchell     ierr = ObjectView((PetscObject) patch->intFacets,            patch->viewerIntFacets, patch->formatIntFacets);CHKERRQ(ierr);
1182eb62eeaaSLawrence Mitchell     ierr = ObjectView((PetscObject) patch->intFacetsToPatchCell, patch->viewerIntFacets, patch->formatIntFacets);CHKERRQ(ierr);
11830e126c0bSLawrence Mitchell   }
11840e126c0bSLawrence Mitchell   ierr = ISCreateGeneral(PETSC_COMM_SELF, numExtFacets,  extFacetsArray,  PETSC_OWN_POINTER, &patch->extFacets);CHKERRQ(ierr);
11850e126c0bSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->extFacets,  "Patch Exterior Facets");CHKERRQ(ierr);
11860e126c0bSLawrence Mitchell   if (patch->viewExtFacets) {
1187fbb82ab5SLawrence Mitchell     ierr = ObjectView((PetscObject) patch->extFacetCounts, patch->viewerExtFacets, patch->formatExtFacets);CHKERRQ(ierr);
11880e126c0bSLawrence Mitchell     ierr = ObjectView((PetscObject) patch->extFacets,      patch->viewerExtFacets, patch->formatExtFacets);CHKERRQ(ierr);
11890e126c0bSLawrence Mitchell   }
11905f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints, pointsArray, PETSC_OWN_POINTER, &patch->points);CHKERRQ(ierr);
11915f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->points, "Patch Points");CHKERRQ(ierr);
11925f824522SMatthew G. Knepley   if (patch->viewPoints) {
11935f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->pointCounts, patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr);
11945f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->points,      patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr);
11955f824522SMatthew G. Knepley   }
1196b6bb21d1SLawrence Mitchell   ierr = DMDestroy(&dm);CHKERRQ(ierr);
11974bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
11984bbf5ea8SMatthew G. Knepley }
11994bbf5ea8SMatthew G. Knepley 
12004bbf5ea8SMatthew G. Knepley /*
12014bbf5ea8SMatthew G. Knepley  * PCPatchCreateCellPatchDiscretisationInfo - Build the dof maps for cell patches
12024bbf5ea8SMatthew G. Knepley  *
12034bbf5ea8SMatthew G. Knepley  * Input Parameters:
12044bbf5ea8SMatthew G. Knepley  * + dm - The DMPlex object defining the mesh
12054bbf5ea8SMatthew G. Knepley  * . cellCounts - Section with counts of cells around each vertex
12064bbf5ea8SMatthew G. Knepley  * . cells - IS of the cell point indices of cells in each patch
12074bbf5ea8SMatthew G. Knepley  * . cellNumbering - Section mapping plex cell points to Firedrake cell indices.
12084bbf5ea8SMatthew G. Knepley  * . nodesPerCell - number of nodes per cell.
12094bbf5ea8SMatthew G. Knepley  * - cellNodeMap - map from cells to node indices (nodesPerCell * numCells)
12104bbf5ea8SMatthew G. Knepley  *
12114bbf5ea8SMatthew G. Knepley  * Output Parameters:
12125f824522SMatthew G. Knepley  * + dofs - IS of local dof numbers of each cell in the patch, where local is a patch local numbering
12134bbf5ea8SMatthew G. Knepley  * . gtolCounts - Section with counts of dofs per cell patch
12144bbf5ea8SMatthew G. Knepley  * - gtol - IS mapping from global dofs to local dofs for each patch.
12154bbf5ea8SMatthew G. Knepley  */
12164bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatchDiscretisationInfo(PC pc)
12174bbf5ea8SMatthew G. Knepley {
12184bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch           = (PC_PATCH *) pc->data;
12194bbf5ea8SMatthew G. Knepley   PetscSection    cellCounts      = patch->cellCounts;
12205f824522SMatthew G. Knepley   PetscSection    pointCounts     = patch->pointCounts;
12210904074fSPatrick Farrell   PetscSection    gtolCounts, gtolCountsWithArtificial = NULL, gtolCountsWithAll = NULL;
12224bbf5ea8SMatthew G. Knepley   IS              cells           = patch->cells;
12235f824522SMatthew G. Knepley   IS              points          = patch->points;
12244bbf5ea8SMatthew G. Knepley   PetscSection    cellNumbering   = patch->cellNumbering;
12255f824522SMatthew G. Knepley   PetscInt        Nf              = patch->nsubspaces;
12265f824522SMatthew G. Knepley   PetscInt        numCells, numPoints;
12274bbf5ea8SMatthew G. Knepley   PetscInt        numDofs;
12280904074fSPatrick Farrell   PetscInt        numGlobalDofs, numGlobalDofsWithArtificial, numGlobalDofsWithAll;
12294bbf5ea8SMatthew G. Knepley   PetscInt        totalDofsPerCell = patch->totalDofsPerCell;
12304bbf5ea8SMatthew G. Knepley   PetscInt        vStart, vEnd, v;
12315f824522SMatthew G. Knepley   const PetscInt *cellsArray, *pointsArray;
12324bbf5ea8SMatthew G. Knepley   PetscInt       *newCellsArray   = NULL;
12334bbf5ea8SMatthew G. Knepley   PetscInt       *dofsArray       = NULL;
1234c2e6f3c0SFlorian Wechsung   PetscInt       *dofsArrayWithArtificial = NULL;
12350904074fSPatrick Farrell   PetscInt       *dofsArrayWithAll = NULL;
12365f824522SMatthew G. Knepley   PetscInt       *offsArray       = NULL;
1237c2e6f3c0SFlorian Wechsung   PetscInt       *offsArrayWithArtificial = NULL;
12380904074fSPatrick Farrell   PetscInt       *offsArrayWithAll = NULL;
12394bbf5ea8SMatthew G. Knepley   PetscInt       *asmArray        = NULL;
1240c2e6f3c0SFlorian Wechsung   PetscInt       *asmArrayWithArtificial = NULL;
12410904074fSPatrick Farrell   PetscInt       *asmArrayWithAll = NULL;
12424bbf5ea8SMatthew G. Knepley   PetscInt       *globalDofsArray = NULL;
1243c2e6f3c0SFlorian Wechsung   PetscInt       *globalDofsArrayWithArtificial = NULL;
12440904074fSPatrick Farrell   PetscInt       *globalDofsArrayWithAll = NULL;
12454bbf5ea8SMatthew G. Knepley   PetscInt        globalIndex     = 0;
12464bbf5ea8SMatthew G. Knepley   PetscInt        key             = 0;
12474bbf5ea8SMatthew G. Knepley   PetscInt        asmKey          = 0;
1248b6bb21d1SLawrence Mitchell   DM              dm              = NULL, plex;
1249557beb66SLawrence Mitchell   const PetscInt *bcNodes         = NULL;
12501b68eb51SMatthew G. Knepley   PetscHMapI      ht;
1251c2e6f3c0SFlorian Wechsung   PetscHMapI      htWithArtificial;
12520904074fSPatrick Farrell   PetscHMapI      htWithAll;
12531b68eb51SMatthew G. Knepley   PetscHSetI      globalBcs;
1254557beb66SLawrence Mitchell   PetscInt        numBcs;
12551b68eb51SMatthew G. Knepley   PetscHSetI      ownedpts, seenpts, owneddofs, seendofs, artificialbcs;
1256cda239d9SMatthew G. Knepley   PetscInt        pStart, pEnd, p, i;
125710534d48SPatrick Farrell   char            option[PETSC_MAX_PATH_LEN];
125839fd2e8aSPatrick Farrell   PetscBool       isNonlinear;
12594bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
12604bbf5ea8SMatthew G. Knepley 
12614bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1262557beb66SLawrence Mitchell 
1263557beb66SLawrence Mitchell   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
1264b6bb21d1SLawrence Mitchell   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
1265b6bb21d1SLawrence Mitchell   dm = plex;
12664bbf5ea8SMatthew G. Knepley   /* dofcounts section is cellcounts section * dofPerCell */
12674bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr);
12685f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(patch->pointCounts, &numPoints);CHKERRQ(ierr);
12694bbf5ea8SMatthew G. Knepley   numDofs = numCells * totalDofsPerCell;
12704bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numDofs, &dofsArray);CHKERRQ(ierr);
12715f824522SMatthew G. Knepley   ierr = PetscMalloc1(numPoints*Nf, &offsArray);CHKERRQ(ierr);
12724bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numDofs, &asmArray);CHKERRQ(ierr);
12734bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numCells, &newCellsArray);CHKERRQ(ierr);
12744bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(cellCounts, &vStart, &vEnd);CHKERRQ(ierr);
12754bbf5ea8SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCounts);CHKERRQ(ierr);
12764bbf5ea8SMatthew G. Knepley   gtolCounts = patch->gtolCounts;
12774bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetChart(gtolCounts, vStart, vEnd);CHKERRQ(ierr);
12785f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->gtolCounts, "Patch Global Index Section");CHKERRQ(ierr);
12794bbf5ea8SMatthew G. Knepley 
1280b6bb21d1SLawrence Mitchell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1281f0dcb611SKarl Rupp     ierr = PetscMalloc1(numPoints*Nf, &offsArrayWithArtificial);CHKERRQ(ierr);
1282c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numDofs, &asmArrayWithArtificial);CHKERRQ(ierr);
1283c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numDofs, &dofsArrayWithArtificial);CHKERRQ(ierr);
1284c2e6f3c0SFlorian Wechsung     ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithArtificial);CHKERRQ(ierr);
1285c2e6f3c0SFlorian Wechsung     gtolCountsWithArtificial = patch->gtolCountsWithArtificial;
1286c2e6f3c0SFlorian Wechsung     ierr = PetscSectionSetChart(gtolCountsWithArtificial, vStart, vEnd);CHKERRQ(ierr);
1287c2e6f3c0SFlorian Wechsung     ierr = PetscObjectSetName((PetscObject) patch->gtolCountsWithArtificial, "Patch Global Index Section Including Artificial BCs");CHKERRQ(ierr);
1288c2e6f3c0SFlorian Wechsung   }
1289c2e6f3c0SFlorian Wechsung 
12900904074fSPatrick Farrell   isNonlinear = patch->isNonlinear;
1291b6bb21d1SLawrence Mitchell   if (isNonlinear) {
12920904074fSPatrick Farrell     ierr = PetscMalloc1(numPoints*Nf, &offsArrayWithAll);CHKERRQ(ierr);
12930904074fSPatrick Farrell     ierr = PetscMalloc1(numDofs, &asmArrayWithAll);CHKERRQ(ierr);
12940904074fSPatrick Farrell     ierr = PetscMalloc1(numDofs, &dofsArrayWithAll);CHKERRQ(ierr);
12950904074fSPatrick Farrell     ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithAll);CHKERRQ(ierr);
12960904074fSPatrick Farrell     gtolCountsWithAll = patch->gtolCountsWithAll;
12970904074fSPatrick Farrell     ierr = PetscSectionSetChart(gtolCountsWithAll, vStart, vEnd);CHKERRQ(ierr);
12980904074fSPatrick Farrell     ierr = PetscObjectSetName((PetscObject) patch->gtolCountsWithAll, "Patch Global Index Section Including All BCs");CHKERRQ(ierr);
12990904074fSPatrick Farrell   }
13000904074fSPatrick Farrell 
1301557beb66SLawrence Mitchell   /* Outside the patch loop, get the dofs that are globally-enforced Dirichlet
1302557beb66SLawrence Mitchell    conditions */
13031b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&globalBcs);CHKERRQ(ierr);
1304557beb66SLawrence Mitchell   ierr = ISGetIndices(patch->ghostBcNodes, &bcNodes);CHKERRQ(ierr);
1305557beb66SLawrence Mitchell   ierr = ISGetSize(patch->ghostBcNodes, &numBcs);CHKERRQ(ierr);
1306cda239d9SMatthew G. Knepley   for (i = 0; i < numBcs; ++i) {
13071b68eb51SMatthew G. Knepley     ierr = PetscHSetIAdd(globalBcs, bcNodes[i]);CHKERRQ(ierr); /* these are already in concatenated numbering */
1308557beb66SLawrence Mitchell   }
1309557beb66SLawrence Mitchell   ierr = ISRestoreIndices(patch->ghostBcNodes, &bcNodes);CHKERRQ(ierr);
1310557beb66SLawrence Mitchell   ierr = ISDestroy(&patch->ghostBcNodes);CHKERRQ(ierr); /* memory optimisation */
1311557beb66SLawrence Mitchell 
1312557beb66SLawrence Mitchell   /* Hash tables for artificial BC construction */
13131b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&ownedpts);CHKERRQ(ierr);
13141b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&seenpts);CHKERRQ(ierr);
13151b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&owneddofs);CHKERRQ(ierr);
13161b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&seendofs);CHKERRQ(ierr);
13171b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&artificialbcs);CHKERRQ(ierr);
1318557beb66SLawrence Mitchell 
13194bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(cells, &cellsArray);CHKERRQ(ierr);
13205f824522SMatthew G. Knepley   ierr = ISGetIndices(points, &pointsArray);CHKERRQ(ierr);
13211b68eb51SMatthew G. Knepley   ierr = PetscHMapICreate(&ht);CHKERRQ(ierr);
1322c2e6f3c0SFlorian Wechsung   ierr = PetscHMapICreate(&htWithArtificial);CHKERRQ(ierr);
13230904074fSPatrick Farrell   ierr = PetscHMapICreate(&htWithAll);CHKERRQ(ierr);
13244bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
13254bbf5ea8SMatthew G. Knepley     PetscInt localIndex = 0;
1326c2e6f3c0SFlorian Wechsung     PetscInt localIndexWithArtificial = 0;
13270904074fSPatrick Farrell     PetscInt localIndexWithAll = 0;
13284bbf5ea8SMatthew G. Knepley     PetscInt dof, off, i, j, k, l;
13294bbf5ea8SMatthew G. Knepley 
13301b68eb51SMatthew G. Knepley     ierr = PetscHMapIClear(ht);CHKERRQ(ierr);
1331c2e6f3c0SFlorian Wechsung     ierr = PetscHMapIClear(htWithArtificial);CHKERRQ(ierr);
13320904074fSPatrick Farrell     ierr = PetscHMapIClear(htWithAll);CHKERRQ(ierr);
13334bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr);
13344bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr);
13354bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
13364bbf5ea8SMatthew G. Knepley 
1337557beb66SLawrence Mitchell     /* Calculate the global numbers of the artificial BC dofs here first */
1338557beb66SLawrence Mitchell     ierr = patch->patchconstructop((void*)patch, dm, v, ownedpts);CHKERRQ(ierr);
1339557beb66SLawrence Mitchell     ierr = PCPatchCompleteCellPatch(pc, ownedpts, seenpts);CHKERRQ(ierr);
1340e4c66b91SPatrick Farrell     ierr = PCPatchGetPointDofs(pc, ownedpts, owneddofs, v, &patch->subspaces_to_exclude);CHKERRQ(ierr);
1341e4c66b91SPatrick Farrell     ierr = PCPatchGetPointDofs(pc, seenpts, seendofs, v, NULL);CHKERRQ(ierr);
1342557beb66SLawrence Mitchell     ierr = PCPatchComputeSetDifference_Private(owneddofs, seendofs, artificialbcs);CHKERRQ(ierr);
13438135ed82SLawrence Mitchell     if (patch->viewPatches) {
13441b68eb51SMatthew G. Knepley       PetscHSetI    globalbcdofs;
13451b68eb51SMatthew G. Knepley       PetscHashIter hi;
13468135ed82SLawrence Mitchell       MPI_Comm      comm = PetscObjectComm((PetscObject)pc);
13471b68eb51SMatthew G. Knepley 
13481b68eb51SMatthew G. Knepley       ierr = PetscHSetICreate(&globalbcdofs);CHKERRQ(ierr);
13498135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: owned dofs:\n", v);CHKERRQ(ierr);
13501b68eb51SMatthew G. Knepley       PetscHashIterBegin(owneddofs, hi);
13511b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(owneddofs, hi)) {
13528135ed82SLawrence Mitchell         PetscInt globalDof;
13538135ed82SLawrence Mitchell 
13541b68eb51SMatthew G. Knepley         PetscHashIterGetKey(owneddofs, hi, globalDof);
13551b68eb51SMatthew G. Knepley         PetscHashIterNext(owneddofs, hi);
13568135ed82SLawrence Mitchell         ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof);CHKERRQ(ierr);
13578135ed82SLawrence Mitchell       }
13588135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n");CHKERRQ(ierr);
13598135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: seen dofs:\n", v);CHKERRQ(ierr);
13601b68eb51SMatthew G. Knepley       PetscHashIterBegin(seendofs, hi);
13611b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(seendofs, hi)) {
13628135ed82SLawrence Mitchell         PetscInt globalDof;
13638135ed82SLawrence Mitchell         PetscBool flg;
13648135ed82SLawrence Mitchell 
13651b68eb51SMatthew G. Knepley         PetscHashIterGetKey(seendofs, hi, globalDof);
13661b68eb51SMatthew G. Knepley         PetscHashIterNext(seendofs, hi);
13678135ed82SLawrence Mitchell         ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof);CHKERRQ(ierr);
13688135ed82SLawrence Mitchell 
13691b68eb51SMatthew G. Knepley         ierr = PetscHSetIHas(globalBcs, globalDof, &flg);CHKERRQ(ierr);
13701b68eb51SMatthew G. Knepley         if (flg) {ierr = PetscHSetIAdd(globalbcdofs, globalDof);CHKERRQ(ierr);}
13718135ed82SLawrence Mitchell       }
13728135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n");CHKERRQ(ierr);
13738135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: global BCs:\n", v);CHKERRQ(ierr);
13741b68eb51SMatthew G. Knepley       ierr = PetscHSetIGetSize(globalbcdofs, &numBcs);CHKERRQ(ierr);
13758135ed82SLawrence Mitchell       if (numBcs > 0) {
13761b68eb51SMatthew G. Knepley         PetscHashIterBegin(globalbcdofs, hi);
13771b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(globalbcdofs, hi)) {
13788135ed82SLawrence Mitchell           PetscInt globalDof;
13791b68eb51SMatthew G. Knepley           PetscHashIterGetKey(globalbcdofs, hi, globalDof);
13801b68eb51SMatthew G. Knepley           PetscHashIterNext(globalbcdofs, hi);
13818135ed82SLawrence Mitchell           ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof);CHKERRQ(ierr);
13828135ed82SLawrence Mitchell         }
13838135ed82SLawrence Mitchell       }
13848135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n");CHKERRQ(ierr);
13858135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: artificial BCs:\n", v);CHKERRQ(ierr);
13861b68eb51SMatthew G. Knepley       ierr = PetscHSetIGetSize(artificialbcs, &numBcs);CHKERRQ(ierr);
13878135ed82SLawrence Mitchell       if (numBcs > 0) {
13881b68eb51SMatthew G. Knepley         PetscHashIterBegin(artificialbcs, hi);
13891b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(artificialbcs, hi)) {
13908135ed82SLawrence Mitchell           PetscInt globalDof;
13911b68eb51SMatthew G. Knepley           PetscHashIterGetKey(artificialbcs, hi, globalDof);
13921b68eb51SMatthew G. Knepley           PetscHashIterNext(artificialbcs, hi);
13938135ed82SLawrence Mitchell           ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof);CHKERRQ(ierr);
13948135ed82SLawrence Mitchell         }
13958135ed82SLawrence Mitchell       }
13968135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n\n");CHKERRQ(ierr);
13971b68eb51SMatthew G. Knepley       ierr = PetscHSetIDestroy(&globalbcdofs);CHKERRQ(ierr);
13988135ed82SLawrence Mitchell     }
13994bbf5ea8SMatthew G. Knepley    for (k = 0; k < patch->nsubspaces; ++k) {
14004bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
14014bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
14024bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
14034bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
14044bbf5ea8SMatthew G. Knepley 
14054bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
14064bbf5ea8SMatthew G. Knepley         /* Walk over the cells in this patch. */
14074bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
14085f824522SMatthew G. Knepley         PetscInt       cell = c;
14094bbf5ea8SMatthew G. Knepley 
14105f824522SMatthew G. Knepley         /* TODO Change this to an IS */
14115f824522SMatthew G. Knepley         if (cellNumbering) {
14124bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetDof(cellNumbering, c, &cell);CHKERRQ(ierr);
14134bbf5ea8SMatthew G. Knepley           if (cell <= 0) SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_OUTOFRANGE, "Cell %D doesn't appear in cell numbering map", c);
14144bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);
14155f824522SMatthew G. Knepley         }
14164bbf5ea8SMatthew G. Knepley         newCellsArray[i] = cell;
14174bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
14184bbf5ea8SMatthew G. Knepley           /* For each global dof, map it into contiguous local storage. */
14194bbf5ea8SMatthew G. Knepley           const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + subspaceOffset;
14204bbf5ea8SMatthew G. Knepley           /* finally, loop over block size */
14214bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
14221b68eb51SMatthew G. Knepley             PetscInt  localDof;
14231b68eb51SMatthew G. Knepley             PetscBool isGlobalBcDof, isArtificialBcDof;
14244bbf5ea8SMatthew G. Knepley 
1425557beb66SLawrence Mitchell             /* first, check if this is either a globally enforced or locally enforced BC dof */
14261b68eb51SMatthew G. Knepley             ierr = PetscHSetIHas(globalBcs, globalDof + l, &isGlobalBcDof);CHKERRQ(ierr);
14271b68eb51SMatthew G. Knepley             ierr = PetscHSetIHas(artificialbcs, globalDof + l, &isArtificialBcDof);CHKERRQ(ierr);
1428557beb66SLawrence Mitchell 
1429557beb66SLawrence Mitchell             /* if it's either, don't ever give it a local dof number */
14301b68eb51SMatthew G. Knepley             if (isGlobalBcDof || isArtificialBcDof) {
1431c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = -1; /* don't use this in assembly in this patch */
1432557beb66SLawrence Mitchell             } else {
14331b68eb51SMatthew G. Knepley               ierr = PetscHMapIGet(ht, globalDof + l, &localDof);CHKERRQ(ierr);
14344bbf5ea8SMatthew G. Knepley               if (localDof == -1) {
14354bbf5ea8SMatthew G. Knepley                 localDof = localIndex++;
14361b68eb51SMatthew G. Knepley                 ierr = PetscHMapISet(ht, globalDof + l, localDof);CHKERRQ(ierr);
14374bbf5ea8SMatthew G. Knepley               }
14384bbf5ea8SMatthew G. Knepley               if (globalIndex >= numDofs) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
14394bbf5ea8SMatthew G. Knepley               /* And store. */
1440c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = localDof;
14414bbf5ea8SMatthew G. Knepley             }
1442c2e6f3c0SFlorian Wechsung 
14430904074fSPatrick Farrell             if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1444c2e6f3c0SFlorian Wechsung               if (isGlobalBcDof) {
1445e047a90bSFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = -1; /* don't use this in assembly in this patch */
1446c2e6f3c0SFlorian Wechsung               } else {
1447c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapIGet(htWithArtificial, globalDof + l, &localDof);CHKERRQ(ierr);
1448c2e6f3c0SFlorian Wechsung                 if (localDof == -1) {
1449c2e6f3c0SFlorian Wechsung                   localDof = localIndexWithArtificial++;
1450c2e6f3c0SFlorian Wechsung                   ierr = PetscHMapISet(htWithArtificial, globalDof + l, localDof);CHKERRQ(ierr);
1451c2e6f3c0SFlorian Wechsung                 }
1452c2e6f3c0SFlorian Wechsung                 if (globalIndex >= numDofs) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
1453c2e6f3c0SFlorian Wechsung                 /* And store.*/
1454c2e6f3c0SFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = localDof;
1455c2e6f3c0SFlorian Wechsung               }
1456c2e6f3c0SFlorian Wechsung             }
14570904074fSPatrick Farrell 
14580904074fSPatrick Farrell             if (isNonlinear) {
14590904074fSPatrick Farrell               /* Build the dofmap for the function space with _all_ dofs,
14600904074fSPatrick Farrell                  including those in any kind of boundary condition */
14610904074fSPatrick Farrell               ierr = PetscHMapIGet(htWithAll, globalDof + l, &localDof);CHKERRQ(ierr);
14620904074fSPatrick Farrell               if (localDof == -1) {
14630904074fSPatrick Farrell                 localDof = localIndexWithAll++;
14640904074fSPatrick Farrell                 ierr = PetscHMapISet(htWithAll, globalDof + l, localDof);CHKERRQ(ierr);
14650904074fSPatrick Farrell               }
14660904074fSPatrick Farrell               if (globalIndex >= numDofs) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
14670904074fSPatrick Farrell               /* And store.*/
14680904074fSPatrick Farrell               dofsArrayWithAll[globalIndex] = localDof;
14690904074fSPatrick Farrell             }
1470c2e6f3c0SFlorian Wechsung             globalIndex++;
14714bbf5ea8SMatthew G. Knepley           }
14724bbf5ea8SMatthew G. Knepley         }
14734bbf5ea8SMatthew G. Knepley       }
1474557beb66SLawrence Mitchell     }
14754bbf5ea8SMatthew G. Knepley      /*How many local dofs in this patch? */
14760904074fSPatrick Farrell    if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1477c2e6f3c0SFlorian Wechsung      ierr = PetscHMapIGetSize(htWithArtificial, &dof);CHKERRQ(ierr);
1478c2e6f3c0SFlorian Wechsung      ierr = PetscSectionSetDof(gtolCountsWithArtificial, v, dof);CHKERRQ(ierr);
1479c2e6f3c0SFlorian Wechsung    }
14800904074fSPatrick Farrell    if (isNonlinear) {
14810904074fSPatrick Farrell      ierr = PetscHMapIGetSize(htWithAll, &dof);CHKERRQ(ierr);
14820904074fSPatrick Farrell      ierr = PetscSectionSetDof(gtolCountsWithAll, v, dof);CHKERRQ(ierr);
14830904074fSPatrick Farrell    }
14841b68eb51SMatthew G. Knepley     ierr = PetscHMapIGetSize(ht, &dof);CHKERRQ(ierr);
14854bbf5ea8SMatthew G. Knepley     ierr = PetscSectionSetDof(gtolCounts, v, dof);CHKERRQ(ierr);
14864bbf5ea8SMatthew G. Knepley   }
1487b6bb21d1SLawrence Mitchell 
1488b6bb21d1SLawrence Mitchell   ierr = DMDestroy(&dm);CHKERRQ(ierr);
14894bbf5ea8SMatthew G. Knepley   if (globalIndex != numDofs) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Expected number of dofs (%d) doesn't match found number (%d)", numDofs, globalIndex);
14904bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetUp(gtolCounts);CHKERRQ(ierr);
14914bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(gtolCounts, &numGlobalDofs);CHKERRQ(ierr);
14924bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numGlobalDofs, &globalDofsArray);CHKERRQ(ierr);
14934bbf5ea8SMatthew G. Knepley 
14940904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1495c2e6f3c0SFlorian Wechsung     ierr = PetscSectionSetUp(gtolCountsWithArtificial);CHKERRQ(ierr);
1496c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetStorageSize(gtolCountsWithArtificial, &numGlobalDofsWithArtificial);CHKERRQ(ierr);
1497c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numGlobalDofsWithArtificial, &globalDofsArrayWithArtificial);CHKERRQ(ierr);
1498c2e6f3c0SFlorian Wechsung   }
14990904074fSPatrick Farrell   if (isNonlinear) {
15000904074fSPatrick Farrell     ierr = PetscSectionSetUp(gtolCountsWithAll);CHKERRQ(ierr);
15010904074fSPatrick Farrell     ierr = PetscSectionGetStorageSize(gtolCountsWithAll, &numGlobalDofsWithAll);CHKERRQ(ierr);
15020904074fSPatrick Farrell     ierr = PetscMalloc1(numGlobalDofsWithAll, &globalDofsArrayWithAll);CHKERRQ(ierr);
15030904074fSPatrick Farrell   }
15044bbf5ea8SMatthew 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. */
15054bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
15061b68eb51SMatthew G. Knepley     PetscHashIter hi;
15075f824522SMatthew G. Knepley     PetscInt      dof, off, Np, ooff, i, j, k, l;
15084bbf5ea8SMatthew G. Knepley 
15091b68eb51SMatthew G. Knepley     ierr = PetscHMapIClear(ht);CHKERRQ(ierr);
1510c2e6f3c0SFlorian Wechsung     ierr = PetscHMapIClear(htWithArtificial);CHKERRQ(ierr);
15110904074fSPatrick Farrell     ierr = PetscHMapIClear(htWithAll);CHKERRQ(ierr);
15124bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr);
15134bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr);
15145f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(pointCounts, v, &Np);CHKERRQ(ierr);
15155f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(pointCounts, v, &ooff);CHKERRQ(ierr);
15164bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
15174bbf5ea8SMatthew G. Knepley 
15184bbf5ea8SMatthew G. Knepley     for (k = 0; k < patch->nsubspaces; ++k) {
15194bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
15204bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
15214bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
15224bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
1523d490bb3dSLawrence Mitchell       PetscInt        goff;
15244bbf5ea8SMatthew G. Knepley 
15254bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
15264bbf5ea8SMatthew G. Knepley         /* Reconstruct mapping of global-to-local on this patch. */
15274bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
15285f824522SMatthew G. Knepley         PetscInt       cell = c;
15294bbf5ea8SMatthew G. Knepley 
15305f824522SMatthew G. Knepley         if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);}
15314bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
15324bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
15335f824522SMatthew G. Knepley             const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset;
1534c2e6f3c0SFlorian Wechsung             const PetscInt localDof  = dofsArray[key];
15351b68eb51SMatthew G. Knepley             if (localDof >= 0) {ierr = PetscHMapISet(ht, globalDof, localDof);CHKERRQ(ierr);}
15360904074fSPatrick Farrell             if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1537c2e6f3c0SFlorian Wechsung               const PetscInt localDofWithArtificial = dofsArrayWithArtificial[key];
1538c2e6f3c0SFlorian Wechsung               if (localDofWithArtificial >= 0) {
1539c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapISet(htWithArtificial, globalDof, localDofWithArtificial);CHKERRQ(ierr);
1540c2e6f3c0SFlorian Wechsung               }
1541c2e6f3c0SFlorian Wechsung             }
15420904074fSPatrick Farrell             if (isNonlinear) {
15430904074fSPatrick Farrell               const PetscInt localDofWithAll = dofsArrayWithAll[key];
15440904074fSPatrick Farrell               if (localDofWithAll >= 0) {
15450904074fSPatrick Farrell                 ierr = PetscHMapISet(htWithAll, globalDof, localDofWithAll);CHKERRQ(ierr);
15460904074fSPatrick Farrell               }
15470904074fSPatrick Farrell             }
1548c2e6f3c0SFlorian Wechsung             key++;
15494bbf5ea8SMatthew G. Knepley           }
15504bbf5ea8SMatthew G. Knepley         }
15514bbf5ea8SMatthew G. Knepley       }
1552557beb66SLawrence Mitchell 
15534bbf5ea8SMatthew G. Knepley       /* Shove it in the output data structure. */
15544bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetOffset(gtolCounts, v, &goff);CHKERRQ(ierr);
15551b68eb51SMatthew G. Knepley       PetscHashIterBegin(ht, hi);
15561b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(ht, hi)) {
15574bbf5ea8SMatthew G. Knepley         PetscInt globalDof, localDof;
15584bbf5ea8SMatthew G. Knepley 
15591b68eb51SMatthew G. Knepley         PetscHashIterGetKey(ht, hi, globalDof);
15601b68eb51SMatthew G. Knepley         PetscHashIterGetVal(ht, hi, localDof);
15614bbf5ea8SMatthew G. Knepley         if (globalDof >= 0) globalDofsArray[goff + localDof] = globalDof;
15621b68eb51SMatthew G. Knepley         PetscHashIterNext(ht, hi);
15634bbf5ea8SMatthew G. Knepley       }
15645f824522SMatthew G. Knepley 
15650904074fSPatrick Farrell       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1566c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(gtolCountsWithArtificial, v, &goff);CHKERRQ(ierr);
1567c2e6f3c0SFlorian Wechsung         PetscHashIterBegin(htWithArtificial, hi);
1568c2e6f3c0SFlorian Wechsung         while (!PetscHashIterAtEnd(htWithArtificial, hi)) {
1569c2e6f3c0SFlorian Wechsung           PetscInt globalDof, localDof;
1570c2e6f3c0SFlorian Wechsung           PetscHashIterGetKey(htWithArtificial, hi, globalDof);
1571c2e6f3c0SFlorian Wechsung           PetscHashIterGetVal(htWithArtificial, hi, localDof);
1572c2e6f3c0SFlorian Wechsung           if (globalDof >= 0) globalDofsArrayWithArtificial[goff + localDof] = globalDof;
1573c2e6f3c0SFlorian Wechsung           PetscHashIterNext(htWithArtificial, hi);
1574c2e6f3c0SFlorian Wechsung         }
1575c2e6f3c0SFlorian Wechsung       }
15760904074fSPatrick Farrell       if (isNonlinear) {
15770904074fSPatrick Farrell         ierr = PetscSectionGetOffset(gtolCountsWithAll, v, &goff);CHKERRQ(ierr);
15780904074fSPatrick Farrell         PetscHashIterBegin(htWithAll, hi);
15790904074fSPatrick Farrell         while (!PetscHashIterAtEnd(htWithAll, hi)) {
15800904074fSPatrick Farrell           PetscInt globalDof, localDof;
15810904074fSPatrick Farrell           PetscHashIterGetKey(htWithAll, hi, globalDof);
15820904074fSPatrick Farrell           PetscHashIterGetVal(htWithAll, hi, localDof);
15830904074fSPatrick Farrell           if (globalDof >= 0) globalDofsArrayWithAll[goff + localDof] = globalDof;
15840904074fSPatrick Farrell           PetscHashIterNext(htWithAll, hi);
15850904074fSPatrick Farrell         }
15860904074fSPatrick Farrell       }
1587c2e6f3c0SFlorian Wechsung 
15885f824522SMatthew G. Knepley       for (p = 0; p < Np; ++p) {
15895f824522SMatthew G. Knepley         const PetscInt point = pointsArray[ooff + p];
15905f824522SMatthew G. Knepley         PetscInt       globalDof, localDof;
15915f824522SMatthew G. Knepley 
15925f824522SMatthew G. Knepley         ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, point, NULL, &globalDof);CHKERRQ(ierr);
15931b68eb51SMatthew G. Knepley         ierr = PetscHMapIGet(ht, globalDof, &localDof);CHKERRQ(ierr);
15945f824522SMatthew G. Knepley         offsArray[(ooff + p)*Nf + k] = localDof;
15950904074fSPatrick Farrell         if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1596c2e6f3c0SFlorian Wechsung           ierr = PetscHMapIGet(htWithArtificial, globalDof, &localDof);CHKERRQ(ierr);
1597c2e6f3c0SFlorian Wechsung           offsArrayWithArtificial[(ooff + p)*Nf + k] = localDof;
1598c2e6f3c0SFlorian Wechsung         }
15990904074fSPatrick Farrell         if (isNonlinear) {
16000904074fSPatrick Farrell           ierr = PetscHMapIGet(htWithAll, globalDof, &localDof);CHKERRQ(ierr);
16010904074fSPatrick Farrell           offsArrayWithAll[(ooff + p)*Nf + k] = localDof;
16020904074fSPatrick Farrell         }
16035f824522SMatthew G. Knepley       }
16044bbf5ea8SMatthew G. Knepley     }
16054bbf5ea8SMatthew G. Knepley 
16060cd083f8SSatish Balay     ierr = PetscHSetIDestroy(&globalBcs);CHKERRQ(ierr);
16071b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&ownedpts);CHKERRQ(ierr);
16081b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&seenpts);CHKERRQ(ierr);
16091b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&owneddofs);CHKERRQ(ierr);
16101b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&seendofs);CHKERRQ(ierr);
16111b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&artificialbcs);CHKERRQ(ierr);
1612557beb66SLawrence Mitchell 
16134bbf5ea8SMatthew G. Knepley       /* At this point, we have a hash table ht built that maps globalDof -> localDof.
16144bbf5ea8SMatthew G. Knepley      We need to create the dof table laid out cellwise first, then by subspace,
16154bbf5ea8SMatthew G. Knepley      as the assembler assembles cell-wise and we need to stuff the different
16164bbf5ea8SMatthew G. Knepley      contributions of the different function spaces to the right places. So we loop
16174bbf5ea8SMatthew G. Knepley      over cells, then over subspaces. */
16184bbf5ea8SMatthew G. Knepley     if (patch->nsubspaces > 1) { /* for nsubspaces = 1, data we need is already in dofsArray */
16194bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
16204bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
16215f824522SMatthew G. Knepley         PetscInt       cell = c;
16224bbf5ea8SMatthew G. Knepley 
16235f824522SMatthew G. Knepley         if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);}
16244bbf5ea8SMatthew G. Knepley         for (k = 0; k < patch->nsubspaces; ++k) {
16254bbf5ea8SMatthew G. Knepley           const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
16264bbf5ea8SMatthew G. Knepley           PetscInt        nodesPerCell   = patch->nodesPerCell[k];
16274bbf5ea8SMatthew G. Knepley           PetscInt        subspaceOffset = patch->subspaceOffsets[k];
16284bbf5ea8SMatthew G. Knepley           PetscInt        bs             = patch->bs[k];
16294bbf5ea8SMatthew G. Knepley 
16304bbf5ea8SMatthew G. Knepley           for (j = 0; j < nodesPerCell; ++j) {
16314bbf5ea8SMatthew G. Knepley             for (l = 0; l < bs; ++l) {
16325f824522SMatthew G. Knepley               const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset;
16334bbf5ea8SMatthew G. Knepley               PetscInt       localDof;
16344bbf5ea8SMatthew G. Knepley 
16351b68eb51SMatthew G. Knepley               ierr = PetscHMapIGet(ht, globalDof, &localDof);CHKERRQ(ierr);
1636557beb66SLawrence Mitchell               /* If it's not in the hash table, i.e. is a BC dof,
16371b68eb51SMatthew G. Knepley                then the PetscHSetIMap above gives -1, which matches
1638557beb66SLawrence Mitchell                exactly the convention for PETSc's matrix assembly to
1639557beb66SLawrence Mitchell                ignore the dof. So we don't need to do anything here */
1640c2e6f3c0SFlorian Wechsung               asmArray[asmKey] = localDof;
16410904074fSPatrick Farrell               if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1642c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapIGet(htWithArtificial, globalDof, &localDof);CHKERRQ(ierr);
1643c2e6f3c0SFlorian Wechsung                 asmArrayWithArtificial[asmKey] = localDof;
1644c2e6f3c0SFlorian Wechsung               }
16450904074fSPatrick Farrell               if (isNonlinear) {
16460904074fSPatrick Farrell                 ierr = PetscHMapIGet(htWithAll, globalDof, &localDof);CHKERRQ(ierr);
16470904074fSPatrick Farrell                 asmArrayWithAll[asmKey] = localDof;
16480904074fSPatrick Farrell               }
1649c2e6f3c0SFlorian Wechsung               asmKey++;
16504bbf5ea8SMatthew G. Knepley             }
16514bbf5ea8SMatthew G. Knepley           }
16524bbf5ea8SMatthew G. Knepley         }
16534bbf5ea8SMatthew G. Knepley       }
16544bbf5ea8SMatthew G. Knepley     }
16554bbf5ea8SMatthew G. Knepley   }
1656c2e6f3c0SFlorian Wechsung   if (1 == patch->nsubspaces) {
1657580bdb30SBarry Smith     ierr = PetscArraycpy(asmArray, dofsArray, numDofs);CHKERRQ(ierr);
16580904074fSPatrick Farrell     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1659580bdb30SBarry Smith       ierr = PetscArraycpy(asmArrayWithArtificial, dofsArrayWithArtificial, numDofs);CHKERRQ(ierr);
1660c2e6f3c0SFlorian Wechsung     }
16610904074fSPatrick Farrell     if (isNonlinear) {
1662580bdb30SBarry Smith       ierr = PetscArraycpy(asmArrayWithAll, dofsArrayWithAll, numDofs);CHKERRQ(ierr);
16630904074fSPatrick Farrell     }
1664c2e6f3c0SFlorian Wechsung   }
16654bbf5ea8SMatthew G. Knepley 
16661b68eb51SMatthew G. Knepley   ierr = PetscHMapIDestroy(&ht);CHKERRQ(ierr);
1667c2e6f3c0SFlorian Wechsung   ierr = PetscHMapIDestroy(&htWithArtificial);CHKERRQ(ierr);
16680904074fSPatrick Farrell   ierr = PetscHMapIDestroy(&htWithAll);CHKERRQ(ierr);
16694bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(cells, &cellsArray);CHKERRQ(ierr);
16705f824522SMatthew G. Knepley   ierr = ISRestoreIndices(points, &pointsArray);CHKERRQ(ierr);
16714bbf5ea8SMatthew G. Knepley   ierr = PetscFree(dofsArray);CHKERRQ(ierr);
16720904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1673c2e6f3c0SFlorian Wechsung     ierr = PetscFree(dofsArrayWithArtificial);CHKERRQ(ierr);
1674c2e6f3c0SFlorian Wechsung   }
16750904074fSPatrick Farrell   if (isNonlinear) {
16760904074fSPatrick Farrell     ierr = PetscFree(dofsArrayWithAll);CHKERRQ(ierr);
16770904074fSPatrick Farrell   }
16785f824522SMatthew G. Knepley   /* Create placeholder section for map from points to patch dofs */
16795f824522SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->patchSection);CHKERRQ(ierr);
16805f824522SMatthew G. Knepley   ierr = PetscSectionSetNumFields(patch->patchSection, patch->nsubspaces);CHKERRQ(ierr);
16811e5fa6bbSLawrence Mitchell   if (patch->combined) {
16821e5fa6bbSLawrence Mitchell     PetscInt numFields;
16831e5fa6bbSLawrence Mitchell     ierr = PetscSectionGetNumFields(patch->dofSection[0], &numFields);CHKERRQ(ierr);
16841e5fa6bbSLawrence Mitchell     if (numFields != patch->nsubspaces) SETERRQ2(PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Mismatch between number of section fields %D and number of subspaces %D", numFields, patch->nsubspaces);
16855f824522SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->dofSection[0], &pStart, &pEnd);CHKERRQ(ierr);
16865f824522SMatthew G. Knepley     ierr = PetscSectionSetChart(patch->patchSection, pStart, pEnd);CHKERRQ(ierr);
16875f824522SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
16885f824522SMatthew G. Knepley       PetscInt dof, fdof, f;
16895f824522SMatthew G. Knepley 
16905f824522SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->dofSection[0], p, &dof);CHKERRQ(ierr);
16915f824522SMatthew G. Knepley       ierr = PetscSectionSetDof(patch->patchSection, p, dof);CHKERRQ(ierr);
16925f824522SMatthew G. Knepley       for (f = 0; f < patch->nsubspaces; ++f) {
16931e5fa6bbSLawrence Mitchell         ierr = PetscSectionGetFieldDof(patch->dofSection[0], p, f, &fdof);CHKERRQ(ierr);
16945f824522SMatthew G. Knepley         ierr = PetscSectionSetFieldDof(patch->patchSection, p, f, fdof);CHKERRQ(ierr);
16955f824522SMatthew G. Knepley       }
16961e5fa6bbSLawrence Mitchell     }
16971e5fa6bbSLawrence Mitchell   } else {
16981e5fa6bbSLawrence Mitchell     PetscInt pStartf, pEndf, f;
16991e5fa6bbSLawrence Mitchell     pStart = PETSC_MAX_INT;
17001e5fa6bbSLawrence Mitchell     pEnd = PETSC_MIN_INT;
17011e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
17021e5fa6bbSLawrence Mitchell       ierr = PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf);CHKERRQ(ierr);
17031e5fa6bbSLawrence Mitchell       pStart = PetscMin(pStart, pStartf);
17041e5fa6bbSLawrence Mitchell       pEnd = PetscMax(pEnd, pEndf);
17051e5fa6bbSLawrence Mitchell     }
17061e5fa6bbSLawrence Mitchell     ierr = PetscSectionSetChart(patch->patchSection, pStart, pEnd);CHKERRQ(ierr);
17071e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
17081e5fa6bbSLawrence Mitchell       ierr = PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf);CHKERRQ(ierr);
17091e5fa6bbSLawrence Mitchell       for (p = pStartf; p < pEndf; ++p) {
17101e5fa6bbSLawrence Mitchell         PetscInt fdof;
17111e5fa6bbSLawrence Mitchell         ierr = PetscSectionGetDof(patch->dofSection[f], p, &fdof);CHKERRQ(ierr);
17121e5fa6bbSLawrence Mitchell         ierr = PetscSectionAddDof(patch->patchSection, p, fdof);CHKERRQ(ierr);
17131e5fa6bbSLawrence Mitchell         ierr = PetscSectionSetFieldDof(patch->patchSection, p, f, fdof);CHKERRQ(ierr);
1714bdd9e0cdSPatrick Farrell       }
1715bdd9e0cdSPatrick Farrell     }
17165f824522SMatthew G. Knepley   }
17175f824522SMatthew G. Knepley   ierr = PetscSectionSetUp(patch->patchSection);CHKERRQ(ierr);
17185f824522SMatthew G. Knepley   ierr = PetscSectionSetUseFieldOffsets(patch->patchSection, PETSC_TRUE);CHKERRQ(ierr);
17194bbf5ea8SMatthew G. Knepley   /* Replace cell indices with firedrake-numbered ones. */
17204bbf5ea8SMatthew G. Knepley   ierr = ISGeneralSetIndices(cells, numCells, (const PetscInt *) newCellsArray, PETSC_OWN_POINTER);CHKERRQ(ierr);
17214bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofs, globalDofsArray, PETSC_OWN_POINTER, &patch->gtol);CHKERRQ(ierr);
17225f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->gtol, "Global Indices");CHKERRQ(ierr);
172310534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_g2l_view", patch->classname);CHKERRQ(ierr);
172410534d48SPatrick Farrell   ierr = PetscSectionViewFromOptions(patch->gtolCounts, (PetscObject) pc, option);CHKERRQ(ierr);
172510534d48SPatrick Farrell   ierr = ISViewFromOptions(patch->gtol, (PetscObject) pc, option);CHKERRQ(ierr);
17264bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArray, PETSC_OWN_POINTER, &patch->dofs);CHKERRQ(ierr);
17275f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArray, PETSC_OWN_POINTER, &patch->offs);CHKERRQ(ierr);
17280904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1729c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithArtificial, globalDofsArrayWithArtificial, PETSC_OWN_POINTER, &patch->gtolWithArtificial);CHKERRQ(ierr);
1730c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithArtificial, PETSC_OWN_POINTER, &patch->dofsWithArtificial);CHKERRQ(ierr);
1731c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithArtificial, PETSC_OWN_POINTER, &patch->offsWithArtificial);CHKERRQ(ierr);
1732c2e6f3c0SFlorian Wechsung   }
17330904074fSPatrick Farrell   if (isNonlinear) {
17340904074fSPatrick Farrell     ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithAll, globalDofsArrayWithAll, PETSC_OWN_POINTER, &patch->gtolWithAll);CHKERRQ(ierr);
17350904074fSPatrick Farrell     ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithAll, PETSC_OWN_POINTER, &patch->dofsWithAll);CHKERRQ(ierr);
17360904074fSPatrick Farrell     ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithAll, PETSC_OWN_POINTER, &patch->offsWithAll);CHKERRQ(ierr);
17370904074fSPatrick Farrell   }
17384bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
17394bbf5ea8SMatthew G. Knepley }
17404bbf5ea8SMatthew G. Knepley 
1741c2e6f3c0SFlorian Wechsung static PetscErrorCode PCPatchCreateMatrix_Private(PC pc, PetscInt point, Mat *mat, PetscBool withArtificial)
17424bbf5ea8SMatthew G. Knepley {
17434bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
17444bbf5ea8SMatthew G. Knepley   PetscBool      flg;
17454bbf5ea8SMatthew G. Knepley   PetscInt       csize, rsize;
17464bbf5ea8SMatthew G. Knepley   const char    *prefix = NULL;
17474bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
17484bbf5ea8SMatthew G. Knepley 
17494bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1750c2e6f3c0SFlorian Wechsung   if (withArtificial) {
1751e047a90bSFlorian Wechsung     /* would be nice if we could create a rectangular matrix of size numDofsWithArtificial x numDofs here */
17529d4fc724SLawrence Mitchell     PetscInt pStart;
17539d4fc724SLawrence Mitchell     ierr = PetscSectionGetChart(patch->gtolCountsWithArtificial, &pStart, NULL);CHKERRQ(ierr);
17549d4fc724SLawrence Mitchell     ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, point + pStart, &rsize);CHKERRQ(ierr);
17559d4fc724SLawrence Mitchell     csize = rsize;
1756ff201f6aSFlorian Wechsung   } else {
17579d4fc724SLawrence Mitchell     PetscInt pStart;
17589d4fc724SLawrence Mitchell     ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, NULL);CHKERRQ(ierr);
17599d4fc724SLawrence Mitchell     ierr = PetscSectionGetDof(patch->gtolCounts, point + pStart, &rsize);CHKERRQ(ierr);
17609d4fc724SLawrence Mitchell     csize = rsize;
1761c2e6f3c0SFlorian Wechsung   }
1762c2e6f3c0SFlorian Wechsung 
17634bbf5ea8SMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, mat);CHKERRQ(ierr);
17644bbf5ea8SMatthew G. Knepley   ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr);
17654bbf5ea8SMatthew G. Knepley   ierr = MatSetOptionsPrefix(*mat, prefix);CHKERRQ(ierr);
17665f824522SMatthew G. Knepley   ierr = MatAppendOptionsPrefix(*mat, "pc_patch_sub_");CHKERRQ(ierr);
17674bbf5ea8SMatthew G. Knepley   if (patch->sub_mat_type)       {ierr = MatSetType(*mat, patch->sub_mat_type);CHKERRQ(ierr);}
17687974b488SMatthew G. Knepley   else if (!patch->sub_mat_type) {ierr = MatSetType(*mat, MATDENSE);CHKERRQ(ierr);}
17694bbf5ea8SMatthew G. Knepley   ierr = MatSetSizes(*mat, rsize, csize, rsize, csize);CHKERRQ(ierr);
17704bbf5ea8SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) *mat, MATDENSE, &flg);CHKERRQ(ierr);
17714bbf5ea8SMatthew G. Knepley   if (!flg) {ierr = PetscObjectTypeCompare((PetscObject)*mat, MATSEQDENSE, &flg);CHKERRQ(ierr);}
17724bbf5ea8SMatthew G. Knepley   /* Sparse patch matrices */
17734bbf5ea8SMatthew G. Knepley   if (!flg) {
17744bbf5ea8SMatthew G. Knepley     PetscBT         bt;
17754bbf5ea8SMatthew G. Knepley     PetscInt       *dnnz      = NULL;
17764bbf5ea8SMatthew G. Knepley     const PetscInt *dofsArray = NULL;
17774bbf5ea8SMatthew G. Knepley     PetscInt        pStart, pEnd, ncell, offset, c, i, j;
17784bbf5ea8SMatthew G. Knepley 
1779c2e6f3c0SFlorian Wechsung     if (withArtificial) {
1780c2e6f3c0SFlorian Wechsung       ierr = ISGetIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1781ff201f6aSFlorian Wechsung     } else {
17824bbf5ea8SMatthew G. Knepley       ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1783c2e6f3c0SFlorian Wechsung     }
17844bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
17854bbf5ea8SMatthew G. Knepley     point += pStart;
17862758c9b9SBarry Smith     if (point >= pEnd) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %D not in [%D, %D)\n", point, pStart, pEnd);
17874bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
17884bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
17894bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventBegin(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr);
1790b2866507SPatrick Farrell     /* A PetscBT uses N^2 bits to store the sparsity pattern on a
17914bbf5ea8SMatthew G. Knepley      * patch. This is probably OK if the patches are not too big,
1792b2866507SPatrick Farrell      * but uses too much memory. We therefore switch based on rsize. */
1793b2866507SPatrick Farrell     if (rsize < 3000) { /* FIXME: I picked this switch value out of my hat */
1794d63cebbaSPatrick Farrell       PetscScalar *zeroes;
1795d63cebbaSPatrick Farrell       PetscInt rows;
1796d63cebbaSPatrick Farrell 
1797b2866507SPatrick Farrell       ierr = PetscCalloc1(rsize, &dnnz);CHKERRQ(ierr);
17984bbf5ea8SMatthew G. Knepley       ierr = PetscBTCreate(rsize*rsize, &bt);CHKERRQ(ierr);
17994bbf5ea8SMatthew G. Knepley       for (c = 0; c < ncell; ++c) {
18004bbf5ea8SMatthew G. Knepley         const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell;
18014bbf5ea8SMatthew G. Knepley         for (i = 0; i < patch->totalDofsPerCell; ++i) {
18024bbf5ea8SMatthew G. Knepley           const PetscInt row = idx[i];
1803557beb66SLawrence Mitchell           if (row < 0) continue;
18044bbf5ea8SMatthew G. Knepley           for (j = 0; j < patch->totalDofsPerCell; ++j) {
18054bbf5ea8SMatthew G. Knepley             const PetscInt col = idx[j];
18064bbf5ea8SMatthew G. Knepley             const PetscInt key = row*rsize + col;
1807557beb66SLawrence Mitchell             if (col < 0) continue;
18084bbf5ea8SMatthew G. Knepley             if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
18094bbf5ea8SMatthew G. Knepley           }
18104bbf5ea8SMatthew G. Knepley         }
18114bbf5ea8SMatthew G. Knepley       }
1812d63cebbaSPatrick Farrell 
1813d63cebbaSPatrick Farrell       if (patch->usercomputeopintfacet) {
1814d63cebbaSPatrick Farrell         const PetscInt *intFacetsArray = NULL;
1815d63cebbaSPatrick Farrell         PetscInt        i, numIntFacets, intFacetOffset;
1816d63cebbaSPatrick Farrell         const PetscInt *facetCells = NULL;
1817d63cebbaSPatrick Farrell 
1818d63cebbaSPatrick Farrell         ierr = PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets);CHKERRQ(ierr);
1819d63cebbaSPatrick Farrell         ierr = PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset);CHKERRQ(ierr);
1820d63cebbaSPatrick Farrell         ierr = ISGetIndices(patch->intFacetsToPatchCell, &facetCells);CHKERRQ(ierr);
1821d63cebbaSPatrick Farrell         ierr = ISGetIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
1822d63cebbaSPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
1823d63cebbaSPatrick Farrell           const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0];
1824d63cebbaSPatrick Farrell           const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1];
1825d63cebbaSPatrick Farrell           PetscInt       celli, cellj;
1826d63cebbaSPatrick Farrell 
1827d63cebbaSPatrick Farrell           for (celli = 0; celli < patch->totalDofsPerCell; celli++) {
1828d63cebbaSPatrick Farrell             const PetscInt row = dofsArray[(offset + cell0)*patch->totalDofsPerCell + celli];
1829b5c64f08SPatrick Farrell             if (row < 0) continue;
1830d63cebbaSPatrick Farrell             for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) {
1831d63cebbaSPatrick Farrell               const PetscInt col = dofsArray[(offset + cell1)*patch->totalDofsPerCell + cellj];
1832d63cebbaSPatrick Farrell               const PetscInt key = row*rsize + col;
1833d63cebbaSPatrick Farrell               if (col < 0) continue;
1834d63cebbaSPatrick Farrell               if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
1835d63cebbaSPatrick Farrell             }
1836d63cebbaSPatrick Farrell           }
1837d63cebbaSPatrick Farrell 
1838d63cebbaSPatrick Farrell           for (celli = 0; celli < patch->totalDofsPerCell; celli++) {
1839d63cebbaSPatrick Farrell             const PetscInt row = dofsArray[(offset + cell1)*patch->totalDofsPerCell + celli];
1840b5c64f08SPatrick Farrell             if (row < 0) continue;
1841d63cebbaSPatrick Farrell             for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) {
1842d63cebbaSPatrick Farrell               const PetscInt col = dofsArray[(offset + cell0)*patch->totalDofsPerCell + cellj];
1843d63cebbaSPatrick Farrell               const PetscInt key = row*rsize + col;
1844d63cebbaSPatrick Farrell               if (col < 0) continue;
1845d63cebbaSPatrick Farrell               if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
1846d63cebbaSPatrick Farrell             }
1847d63cebbaSPatrick Farrell           }
1848d63cebbaSPatrick Farrell         }
1849d63cebbaSPatrick Farrell       }
18504bbf5ea8SMatthew G. Knepley       ierr = PetscBTDestroy(&bt);CHKERRQ(ierr);
18514bbf5ea8SMatthew G. Knepley       ierr = MatXAIJSetPreallocation(*mat, 1, dnnz, NULL, NULL, NULL);CHKERRQ(ierr);
18524bbf5ea8SMatthew G. Knepley       ierr = PetscFree(dnnz);CHKERRQ(ierr);
1853d63cebbaSPatrick Farrell 
1854d63cebbaSPatrick Farrell       ierr = PetscCalloc1(patch->totalDofsPerCell*patch->totalDofsPerCell, &zeroes);CHKERRQ(ierr);
1855d63cebbaSPatrick Farrell       for (c = 0; c < ncell; ++c) {
1856d63cebbaSPatrick Farrell         const PetscInt *idx = &dofsArray[(offset + c)*patch->totalDofsPerCell];
1857d63cebbaSPatrick Farrell         ierr = MatSetValues(*mat, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, zeroes, INSERT_VALUES);CHKERRQ(ierr);
1858d63cebbaSPatrick Farrell       }
1859d63cebbaSPatrick Farrell       ierr = MatGetLocalSize(*mat, &rows, NULL);CHKERRQ(ierr);
1860d63cebbaSPatrick Farrell       for (i = 0; i < rows; ++i) {
1861d63cebbaSPatrick Farrell         ierr = MatSetValues(*mat, 1, &i, 1, &i, zeroes, INSERT_VALUES);CHKERRQ(ierr);
1862d63cebbaSPatrick Farrell       }
1863d63cebbaSPatrick Farrell 
1864d63cebbaSPatrick Farrell       if (patch->usercomputeopintfacet) {
1865d63cebbaSPatrick Farrell         const PetscInt *intFacetsArray = NULL;
1866d63cebbaSPatrick Farrell         PetscInt i, numIntFacets, intFacetOffset;
1867d63cebbaSPatrick Farrell         const PetscInt *facetCells = NULL;
1868d63cebbaSPatrick Farrell 
1869d63cebbaSPatrick Farrell         ierr = PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets);CHKERRQ(ierr);
1870d63cebbaSPatrick Farrell         ierr = PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset);CHKERRQ(ierr);
1871d63cebbaSPatrick Farrell         ierr = ISGetIndices(patch->intFacetsToPatchCell, &facetCells);CHKERRQ(ierr);
1872d63cebbaSPatrick Farrell         ierr = ISGetIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
1873d63cebbaSPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
1874d63cebbaSPatrick Farrell           const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0];
1875d63cebbaSPatrick Farrell           const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1];
1876d63cebbaSPatrick Farrell           const PetscInt *cell0idx = &dofsArray[(offset + cell0)*patch->totalDofsPerCell];
1877d63cebbaSPatrick Farrell           const PetscInt *cell1idx = &dofsArray[(offset + cell1)*patch->totalDofsPerCell];
1878d63cebbaSPatrick Farrell           ierr = MatSetValues(*mat, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, zeroes, INSERT_VALUES);CHKERRQ(ierr);
1879d63cebbaSPatrick Farrell           ierr = MatSetValues(*mat, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, zeroes, INSERT_VALUES);CHKERRQ(ierr);
1880d63cebbaSPatrick Farrell         }
1881d63cebbaSPatrick Farrell       }
1882d63cebbaSPatrick Farrell 
1883d63cebbaSPatrick Farrell       ierr = MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1884d63cebbaSPatrick Farrell       ierr = MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1885d63cebbaSPatrick Farrell 
1886d63cebbaSPatrick Farrell       ierr = PetscFree(zeroes);CHKERRQ(ierr);
1887d63cebbaSPatrick Farrell 
1888b2866507SPatrick Farrell     } else { /* rsize too big, use MATPREALLOCATOR */
1889b2866507SPatrick Farrell       Mat preallocator;
1890b2866507SPatrick Farrell       PetscScalar* vals;
1891b2866507SPatrick Farrell 
1892b2866507SPatrick Farrell       ierr = PetscCalloc1(patch->totalDofsPerCell*patch->totalDofsPerCell, &vals);CHKERRQ(ierr);
1893b2866507SPatrick Farrell       ierr = MatCreate(PETSC_COMM_SELF, &preallocator);CHKERRQ(ierr);
1894b2866507SPatrick Farrell       ierr = MatSetType(preallocator, MATPREALLOCATOR);CHKERRQ(ierr);
1895b2866507SPatrick Farrell       ierr = MatSetSizes(preallocator, rsize, rsize, rsize, rsize);CHKERRQ(ierr);
1896b2866507SPatrick Farrell       ierr = MatSetUp(preallocator);CHKERRQ(ierr);
189711bcd083SPatrick Farrell 
1898b2866507SPatrick Farrell       for (c = 0; c < ncell; ++c) {
1899b2866507SPatrick Farrell         const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell;
1900b2866507SPatrick Farrell         ierr = MatSetValues(preallocator, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, vals, INSERT_VALUES);CHKERRQ(ierr);
1901b2866507SPatrick Farrell       }
190211bcd083SPatrick Farrell 
190311bcd083SPatrick Farrell       if (patch->usercomputeopintfacet) {
190411bcd083SPatrick Farrell         const PetscInt *intFacetsArray = NULL;
190511bcd083SPatrick Farrell         PetscInt        i, numIntFacets, intFacetOffset;
190611bcd083SPatrick Farrell         const PetscInt *facetCells = NULL;
190711bcd083SPatrick Farrell 
190811bcd083SPatrick Farrell         ierr = PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets);CHKERRQ(ierr);
190911bcd083SPatrick Farrell         ierr = PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset);CHKERRQ(ierr);
191011bcd083SPatrick Farrell         ierr = ISGetIndices(patch->intFacetsToPatchCell, &facetCells);CHKERRQ(ierr);
191111bcd083SPatrick Farrell         ierr = ISGetIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
191211bcd083SPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
191311bcd083SPatrick Farrell           const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0];
191411bcd083SPatrick Farrell           const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1];
191511bcd083SPatrick Farrell           const PetscInt *cell0idx = &dofsArray[(offset + cell0)*patch->totalDofsPerCell];
191611bcd083SPatrick Farrell           const PetscInt *cell1idx = &dofsArray[(offset + cell1)*patch->totalDofsPerCell];
191711bcd083SPatrick Farrell           ierr = MatSetValues(preallocator, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, vals, INSERT_VALUES);CHKERRQ(ierr);
191811bcd083SPatrick Farrell           ierr = MatSetValues(preallocator, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, vals, INSERT_VALUES);CHKERRQ(ierr);
191911bcd083SPatrick Farrell         }
192011bcd083SPatrick Farrell       }
192111bcd083SPatrick Farrell 
1922b2866507SPatrick Farrell       ierr = PetscFree(vals);CHKERRQ(ierr);
1923b2866507SPatrick Farrell       ierr = MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1924b2866507SPatrick Farrell       ierr = MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1925b2866507SPatrick Farrell       ierr = MatPreallocatorPreallocate(preallocator, PETSC_TRUE, *mat);CHKERRQ(ierr);
1926b2866507SPatrick Farrell       ierr = MatDestroy(&preallocator);CHKERRQ(ierr);
1927b2866507SPatrick Farrell     }
19284bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr);
1929fe117d09SFlorian Wechsung     if (withArtificial) {
1930fe117d09SFlorian Wechsung       ierr = ISRestoreIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1931fe117d09SFlorian Wechsung     } else {
19324bbf5ea8SMatthew G. Knepley       ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
19334bbf5ea8SMatthew G. Knepley     }
1934fe117d09SFlorian Wechsung   }
19354bbf5ea8SMatthew G. Knepley   ierr = MatSetUp(*mat);CHKERRQ(ierr);
19364bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
19374bbf5ea8SMatthew G. Knepley }
19384bbf5ea8SMatthew G. Knepley 
19390904074fSPatrick Farrell static PetscErrorCode PCPatchComputeFunction_DMPlex_Private(PC pc, PetscInt patchNum, Vec x, Vec F, IS cellIS, PetscInt n, const PetscInt *l2p, const PetscInt *l2pWithAll, void *ctx)
194092d50984SMatthew G. Knepley {
194192d50984SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
1942b6bb21d1SLawrence Mitchell   DM              dm, plex;
194392d50984SMatthew G. Knepley   PetscSection    s;
194492d50984SMatthew G. Knepley   const PetscInt *parray, *oarray;
194592d50984SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
194692d50984SMatthew G. Knepley   PetscErrorCode  ierr;
194792d50984SMatthew G. Knepley 
194892d50984SMatthew G. Knepley   PetscFunctionBegin;
1949fa84ea4cSLawrence Mitchell   if (patch->precomputeElementTensors) SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Precomputing element tensors not implemented with DMPlex compute operator");
195092d50984SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
1951b6bb21d1SLawrence Mitchell   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
1952b6bb21d1SLawrence Mitchell   dm = plex;
195392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
195492d50984SMatthew G. Knepley   /* Set offset into patch */
195592d50984SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->pointCounts, patchNum, &Np);CHKERRQ(ierr);
195692d50984SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->pointCounts, patchNum, &poff);CHKERRQ(ierr);
195792d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->points, &parray);CHKERRQ(ierr);
195892d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->offs,   &oarray);CHKERRQ(ierr);
195992d50984SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
196092d50984SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
196192d50984SMatthew G. Knepley       const PetscInt point = parray[poff+p];
196292d50984SMatthew G. Knepley       PetscInt       dof;
196392d50984SMatthew G. Knepley 
196492d50984SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(patch->patchSection, point, f, &dof);CHKERRQ(ierr);
196592d50984SMatthew G. Knepley       ierr = PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);
196692d50984SMatthew G. Knepley       if (patch->nsubspaces == 1) {ierr = PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);}
196792d50984SMatthew G. Knepley       else                        {ierr = PetscSectionSetOffset(patch->patchSection, point, -1);CHKERRQ(ierr);}
196892d50984SMatthew G. Knepley     }
196992d50984SMatthew G. Knepley   }
197092d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->points, &parray);CHKERRQ(ierr);
197192d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->offs,   &oarray);CHKERRQ(ierr);
197292d50984SMatthew G. Knepley   if (patch->viewSection) {ierr = ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection);CHKERRQ(ierr);}
1973b6bb21d1SLawrence Mitchell   ierr = DMPlexComputeResidual_Patch_Internal(dm, patch->patchSection, cellIS, 0.0, x, NULL, F, ctx);CHKERRQ(ierr);
1974b6bb21d1SLawrence Mitchell   ierr = DMDestroy(&dm);CHKERRQ(ierr);
197592d50984SMatthew G. Knepley   PetscFunctionReturn(0);
197692d50984SMatthew G. Knepley }
197792d50984SMatthew G. Knepley 
197892d50984SMatthew G. Knepley PetscErrorCode PCPatchComputeFunction_Internal(PC pc, Vec x, Vec F, PetscInt point)
197992d50984SMatthew G. Knepley {
198092d50984SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
198192d50984SMatthew G. Knepley   const PetscInt *dofsArray;
19820904074fSPatrick Farrell   const PetscInt *dofsArrayWithAll;
198392d50984SMatthew G. Knepley   const PetscInt *cellsArray;
198492d50984SMatthew G. Knepley   PetscInt        ncell, offset, pStart, pEnd;
198592d50984SMatthew G. Knepley   PetscErrorCode  ierr;
198692d50984SMatthew G. Knepley 
198792d50984SMatthew G. Knepley   PetscFunctionBegin;
198892d50984SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
198992d50984SMatthew G. Knepley   if (!patch->usercomputeop) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback\n");
199092d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
19910904074fSPatrick Farrell   ierr = ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
199292d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
199392d50984SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
199492d50984SMatthew G. Knepley 
199592d50984SMatthew G. Knepley   point += pStart;
19962758c9b9SBarry Smith   if (point >= pEnd) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %D not in [%D, %D)\n", point, pStart, pEnd);
199792d50984SMatthew G. Knepley 
199892d50984SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
199992d50984SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
200092d50984SMatthew G. Knepley   if (ncell <= 0) {
200192d50984SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
200292d50984SMatthew G. Knepley     PetscFunctionReturn(0);
200392d50984SMatthew G. Knepley   }
200426dc5b63SLawrence Mitchell   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
200592d50984SMatthew G. Knepley   PetscStackPush("PCPatch user callback");
200692d50984SMatthew G. Knepley   /* Cannot reuse the same IS because the geometry info is being cached in it */
200792d50984SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS);CHKERRQ(ierr);
200839fd2e8aSPatrick Farrell   ierr = patch->usercomputef(pc, point, x, F, patch->cellIS, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell,
20090904074fSPatrick Farrell                                                                                             dofsArrayWithAll + offset*patch->totalDofsPerCell,
201039fd2e8aSPatrick Farrell                                                                                             patch->usercomputefctx);CHKERRQ(ierr);
201192d50984SMatthew G. Knepley   PetscStackPop;
201292d50984SMatthew G. Knepley   ierr = ISDestroy(&patch->cellIS);CHKERRQ(ierr);
201392d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
20140904074fSPatrick Farrell   ierr = ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
201592d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
201692d50984SMatthew G. Knepley   if (patch->viewMatrix) {
201792d50984SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
201892d50984SMatthew G. Knepley 
201992d50984SMatthew G. Knepley     ierr = PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch vector for Point %D", point);CHKERRQ(ierr);
202092d50984SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) F, name);CHKERRQ(ierr);
202192d50984SMatthew G. Knepley     ierr = ObjectView((PetscObject) F, patch->viewerMatrix, patch->formatMatrix);CHKERRQ(ierr);
202292d50984SMatthew G. Knepley   }
202392d50984SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
202492d50984SMatthew G. Knepley   PetscFunctionReturn(0);
202592d50984SMatthew G. Knepley }
202692d50984SMatthew G. Knepley 
20270904074fSPatrick Farrell static PetscErrorCode PCPatchComputeOperator_DMPlex_Private(PC pc, PetscInt patchNum, Vec x, Mat J, IS cellIS, PetscInt n, const PetscInt *l2p, const PetscInt *l2pWithAll, void *ctx)
20285f824522SMatthew G. Knepley {
20295f824522SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
2030b6bb21d1SLawrence Mitchell   DM              dm, plex;
20315f824522SMatthew G. Knepley   PetscSection    s;
20325f824522SMatthew G. Knepley   const PetscInt *parray, *oarray;
20335f824522SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
20345f824522SMatthew G. Knepley   PetscErrorCode  ierr;
20355f824522SMatthew G. Knepley 
20365f824522SMatthew G. Knepley   PetscFunctionBegin;
20375f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
2038b6bb21d1SLawrence Mitchell   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
2039b6bb21d1SLawrence Mitchell   dm = plex;
204092fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
20415f824522SMatthew G. Knepley   /* Set offset into patch */
20425f824522SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->pointCounts, patchNum, &Np);CHKERRQ(ierr);
20435f824522SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->pointCounts, patchNum, &poff);CHKERRQ(ierr);
20445f824522SMatthew G. Knepley   ierr = ISGetIndices(patch->points, &parray);CHKERRQ(ierr);
20455f824522SMatthew G. Knepley   ierr = ISGetIndices(patch->offs,   &oarray);CHKERRQ(ierr);
20465f824522SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
20475f824522SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
20485f824522SMatthew G. Knepley       const PetscInt point = parray[poff+p];
20495f824522SMatthew G. Knepley       PetscInt       dof;
20505f824522SMatthew G. Knepley 
20515f824522SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(patch->patchSection, point, f, &dof);CHKERRQ(ierr);
20525f824522SMatthew G. Knepley       ierr = PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);
20535f824522SMatthew G. Knepley       if (patch->nsubspaces == 1) {ierr = PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);}
20545f824522SMatthew G. Knepley       else                        {ierr = PetscSectionSetOffset(patch->patchSection, point, -1);CHKERRQ(ierr);}
20555f824522SMatthew G. Knepley     }
20565f824522SMatthew G. Knepley   }
20575f824522SMatthew G. Knepley   ierr = ISRestoreIndices(patch->points, &parray);CHKERRQ(ierr);
20585f824522SMatthew G. Knepley   ierr = ISRestoreIndices(patch->offs,   &oarray);CHKERRQ(ierr);
20595f824522SMatthew G. Knepley   if (patch->viewSection) {ierr = ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection);CHKERRQ(ierr);}
20605f824522SMatthew G. Knepley   /* TODO Shut off MatViewFromOptions() in MatAssemblyEnd() here */
2061b6bb21d1SLawrence Mitchell   ierr = DMPlexComputeJacobian_Patch_Internal(dm, patch->patchSection, patch->patchSection, cellIS, 0.0, 0.0, x, NULL, J, J, ctx);CHKERRQ(ierr);
2062b6bb21d1SLawrence Mitchell   ierr = DMDestroy(&dm);CHKERRQ(ierr);
20635f824522SMatthew G. Knepley   PetscFunctionReturn(0);
20645f824522SMatthew G. Knepley }
20655f824522SMatthew G. Knepley 
2066a685ae26SLawrence Mitchell /* This function zeros mat on entry */
206734d8b122SPatrick Farrell PetscErrorCode PCPatchComputeOperator_Internal(PC pc, Vec x, Mat mat, PetscInt point, PetscBool withArtificial)
20684bbf5ea8SMatthew G. Knepley {
20694bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
20704bbf5ea8SMatthew G. Knepley   const PetscInt *dofsArray;
20710904074fSPatrick Farrell   const PetscInt *dofsArrayWithAll = NULL;
20724bbf5ea8SMatthew G. Knepley   const PetscInt *cellsArray;
2073eb62eeaaSLawrence Mitchell   PetscInt        ncell, offset, pStart, pEnd, numIntFacets, intFacetOffset;
20744d04e9f1SPatrick Farrell   PetscBool       isNonlinear;
20754bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
20764bbf5ea8SMatthew G. Knepley 
20774bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
20784bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
2079debbdec3SPatrick Farrell   isNonlinear = patch->isNonlinear;
20804bbf5ea8SMatthew G. Knepley   if (!patch->usercomputeop) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback\n");
2081c2e6f3c0SFlorian Wechsung   if (withArtificial) {
2082c2e6f3c0SFlorian Wechsung     ierr = ISGetIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
2083c2e6f3c0SFlorian Wechsung   } else {
20844bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
2085c2e6f3c0SFlorian Wechsung   }
20864d04e9f1SPatrick Farrell   if (isNonlinear) {
20870904074fSPatrick Farrell     ierr = ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
20884d04e9f1SPatrick Farrell   }
20894bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
20904bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
20914bbf5ea8SMatthew G. Knepley 
20924bbf5ea8SMatthew G. Knepley   point += pStart;
20932758c9b9SBarry Smith   if (point >= pEnd) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %D not in [%D, %D)\n", point, pStart, pEnd);
20944bbf5ea8SMatthew G. Knepley 
20954bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
20964bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
20974bbf5ea8SMatthew G. Knepley   if (ncell <= 0) {
20984bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
20994bbf5ea8SMatthew G. Knepley     PetscFunctionReturn(0);
21004bbf5ea8SMatthew G. Knepley   }
2101a685ae26SLawrence Mitchell   ierr = MatZeroEntries(mat);CHKERRQ(ierr);
2102fa84ea4cSLawrence Mitchell   if (patch->precomputeElementTensors) {
2103fa84ea4cSLawrence Mitchell     PetscInt           i;
2104fa84ea4cSLawrence Mitchell     PetscInt           ndof = patch->totalDofsPerCell;
2105fa84ea4cSLawrence Mitchell     const PetscScalar *elementTensors;
2106fa84ea4cSLawrence Mitchell 
2107fa84ea4cSLawrence Mitchell     ierr = VecGetArrayRead(patch->cellMats, &elementTensors);CHKERRQ(ierr);
2108fa84ea4cSLawrence Mitchell     for (i = 0; i < ncell; i++) {
2109fa84ea4cSLawrence Mitchell       const PetscInt     cell = cellsArray[i + offset];
2110fa84ea4cSLawrence Mitchell       const PetscInt    *idx  = dofsArray + (offset + i)*ndof;
2111fe988be2SFlorian Wechsung       const PetscScalar *v    = elementTensors + patch->precomputedTensorLocations[cell]*ndof*ndof;
2112fa84ea4cSLawrence Mitchell       ierr = MatSetValues(mat, ndof, idx, ndof, idx, v, ADD_VALUES);CHKERRQ(ierr);
2113fa84ea4cSLawrence Mitchell     }
2114fa84ea4cSLawrence Mitchell     ierr = VecRestoreArrayRead(patch->cellMats, &elementTensors);CHKERRQ(ierr);
2115fa84ea4cSLawrence Mitchell     ierr = MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2116fa84ea4cSLawrence Mitchell     ierr = MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2117fa84ea4cSLawrence Mitchell   } else {
21184bbf5ea8SMatthew G. Knepley     PetscStackPush("PCPatch user callback");
21192aa6f319SMatthew G. Knepley     /* Cannot reuse the same IS because the geometry info is being cached in it */
21202aa6f319SMatthew G. Knepley     ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS);CHKERRQ(ierr);
21210904074fSPatrick Farrell     ierr = patch->usercomputeop(pc, point, x, mat, patch->cellIS, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell, dofsArrayWithAll ? dofsArrayWithAll + offset*patch->totalDofsPerCell : NULL, patch->usercomputeopctx);CHKERRQ(ierr);
2122fa84ea4cSLawrence Mitchell   }
212359109abcSLawrence Mitchell   if (patch->usercomputeopintfacet) {
2124eb62eeaaSLawrence Mitchell     ierr = PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets);CHKERRQ(ierr);
2125eb62eeaaSLawrence Mitchell     ierr = PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset);CHKERRQ(ierr);
2126eb62eeaaSLawrence Mitchell     if (numIntFacets > 0) {
2127eb62eeaaSLawrence Mitchell       /* For each interior facet, grab the two cells (in local numbering, and concatenate dof numberings for those cells) */
2128eb62eeaaSLawrence Mitchell       PetscInt       *facetDofs = NULL, *facetDofsWithAll = NULL;
2129eb62eeaaSLawrence Mitchell       const PetscInt *intFacetsArray = NULL;
2130eb62eeaaSLawrence Mitchell       PetscInt        idx = 0;
2131eb62eeaaSLawrence Mitchell       PetscInt        i, c, d;
2132de2d1767SPatrick Farrell       PetscInt        fStart;
2133b6bb21d1SLawrence Mitchell       DM              dm, plex;
2134eb62eeaaSLawrence Mitchell       IS              facetIS = NULL;
2135eb62eeaaSLawrence Mitchell       const PetscInt *facetCells = NULL;
21367a50e09dSPatrick Farrell 
2137eb62eeaaSLawrence Mitchell       ierr = ISGetIndices(patch->intFacetsToPatchCell, &facetCells);CHKERRQ(ierr);
2138eb62eeaaSLawrence Mitchell       ierr = ISGetIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
2139de2d1767SPatrick Farrell       ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
2140b6bb21d1SLawrence Mitchell       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
2141b6bb21d1SLawrence Mitchell       dm = plex;
2142de2d1767SPatrick Farrell       ierr = DMPlexGetHeightStratum(dm, 1, &fStart, NULL);CHKERRQ(ierr);
2143eb62eeaaSLawrence Mitchell       /* FIXME: Pull this malloc out. */
2144eb62eeaaSLawrence Mitchell       ierr = PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofs);CHKERRQ(ierr);
2145eb62eeaaSLawrence Mitchell       if (dofsArrayWithAll) {
2146eb62eeaaSLawrence Mitchell         ierr = PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofsWithAll);CHKERRQ(ierr);
2147eb62eeaaSLawrence Mitchell       }
2148f98464cbSLawrence Mitchell       if (patch->precomputeElementTensors) {
2149f98464cbSLawrence Mitchell         PetscInt           nFacetDof = 2*patch->totalDofsPerCell;
2150f98464cbSLawrence Mitchell         const PetscScalar *elementTensors;
2151f98464cbSLawrence Mitchell 
2152f98464cbSLawrence Mitchell         ierr = VecGetArrayRead(patch->intFacetMats, &elementTensors);CHKERRQ(ierr);
2153f98464cbSLawrence Mitchell 
2154f98464cbSLawrence Mitchell         for (i = 0; i < numIntFacets; i++) {
2155f98464cbSLawrence Mitchell           const PetscInt     facet = intFacetsArray[i + intFacetOffset];
2156de2d1767SPatrick Farrell           const PetscScalar *v     = elementTensors + patch->precomputedIntFacetTensorLocations[facet - fStart]*nFacetDof*nFacetDof;
2157f98464cbSLawrence Mitchell           idx = 0;
2158f98464cbSLawrence Mitchell           /*
2159f98464cbSLawrence Mitchell            * 0--1
2160f98464cbSLawrence Mitchell            * |\-|
2161f98464cbSLawrence Mitchell            * |+\|
2162f98464cbSLawrence Mitchell            * 2--3
2163f98464cbSLawrence Mitchell            * [0, 2, 3, 0, 1, 3]
2164f98464cbSLawrence Mitchell            */
2165f98464cbSLawrence Mitchell           for (c = 0; c < 2; c++) {
2166f98464cbSLawrence Mitchell             const PetscInt cell = facetCells[2*(intFacetOffset + i) + c];
2167f98464cbSLawrence Mitchell             for (d = 0; d < patch->totalDofsPerCell; d++) {
2168f98464cbSLawrence Mitchell               facetDofs[idx] = dofsArray[(offset + cell)*patch->totalDofsPerCell + d];
2169f98464cbSLawrence Mitchell               idx++;
2170f98464cbSLawrence Mitchell             }
2171f98464cbSLawrence Mitchell           }
2172f98464cbSLawrence Mitchell           ierr = MatSetValues(mat, nFacetDof, facetDofs, nFacetDof, facetDofs, v, ADD_VALUES);CHKERRQ(ierr);
2173f98464cbSLawrence Mitchell         }
2174f98464cbSLawrence Mitchell         ierr = VecRestoreArrayRead(patch->intFacetMats, &elementTensors);CHKERRQ(ierr);
2175f98464cbSLawrence Mitchell       } else {
2176eb62eeaaSLawrence Mitchell         /*
2177eb62eeaaSLawrence Mitchell          * 0--1
2178eb62eeaaSLawrence Mitchell          * |\-|
2179eb62eeaaSLawrence Mitchell          * |+\|
2180eb62eeaaSLawrence Mitchell          * 2--3
2181eb62eeaaSLawrence Mitchell          * [0, 2, 3, 0, 1, 3]
2182eb62eeaaSLawrence Mitchell          */
2183eb62eeaaSLawrence Mitchell         for (i = 0; i < numIntFacets; i++) {
2184eb62eeaaSLawrence Mitchell           for (c = 0; c < 2; c++) {
2185eb62eeaaSLawrence Mitchell             const PetscInt cell = facetCells[2*(intFacetOffset + i) + c];
2186eb62eeaaSLawrence Mitchell             for (d = 0; d < patch->totalDofsPerCell; d++) {
2187eb62eeaaSLawrence Mitchell               facetDofs[idx] = dofsArray[(offset + cell)*patch->totalDofsPerCell + d];
2188eb62eeaaSLawrence Mitchell               if (dofsArrayWithAll) {
2189eb62eeaaSLawrence Mitchell                 facetDofsWithAll[idx] = dofsArrayWithAll[(offset + cell)*patch->totalDofsPerCell + d];
2190eb62eeaaSLawrence Mitchell               }
2191eb62eeaaSLawrence Mitchell               idx++;
2192eb62eeaaSLawrence Mitchell             }
2193eb62eeaaSLawrence Mitchell           }
2194eb62eeaaSLawrence Mitchell         }
2195eb62eeaaSLawrence Mitchell         ierr = ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray + intFacetOffset, PETSC_USE_POINTER, &facetIS);CHKERRQ(ierr);
2196ec0d443bSLawrence Mitchell         ierr = patch->usercomputeopintfacet(pc, point, x, mat, facetIS, 2*numIntFacets*patch->totalDofsPerCell, facetDofs, facetDofsWithAll, patch->usercomputeopintfacetctx);CHKERRQ(ierr);
2197eb62eeaaSLawrence Mitchell         ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
2198f98464cbSLawrence Mitchell       }
2199eb62eeaaSLawrence Mitchell       ierr = ISRestoreIndices(patch->intFacetsToPatchCell, &facetCells);CHKERRQ(ierr);
2200f98464cbSLawrence Mitchell       ierr = ISRestoreIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
2201eb62eeaaSLawrence Mitchell       ierr = PetscFree(facetDofs);CHKERRQ(ierr);
2202eb62eeaaSLawrence Mitchell       ierr = PetscFree(facetDofsWithAll);CHKERRQ(ierr);
2203b6bb21d1SLawrence Mitchell       ierr = DMDestroy(&dm);CHKERRQ(ierr);
2204eb62eeaaSLawrence Mitchell     }
220559109abcSLawrence Mitchell   }
22066710cc29SPatrick Farrell 
22076710cc29SPatrick Farrell   ierr = MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
22086710cc29SPatrick Farrell   ierr = MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
22096710cc29SPatrick Farrell 
2210c73d2cf6SLawrence Mitchell   if (!(withArtificial || isNonlinear) && patch->denseinverse) {
2211c73d2cf6SLawrence Mitchell     MatFactorInfo info;
2212c73d2cf6SLawrence Mitchell     PetscBool     flg;
2213c73d2cf6SLawrence Mitchell     ierr = PetscObjectTypeCompare((PetscObject)mat, MATSEQDENSE, &flg);CHKERRQ(ierr);
2214c73d2cf6SLawrence Mitchell     if (!flg) SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Invalid Mat type for dense inverse");
2215c73d2cf6SLawrence Mitchell     ierr = MatFactorInfoInitialize(&info);CHKERRQ(ierr);
2216c73d2cf6SLawrence Mitchell     ierr = MatLUFactor(mat, NULL, NULL, &info);CHKERRQ(ierr);
2217c73d2cf6SLawrence Mitchell     ierr = MatSeqDenseInvertFactors_Private(mat);CHKERRQ(ierr);
2218c73d2cf6SLawrence Mitchell   }
22194bbf5ea8SMatthew G. Knepley   PetscStackPop;
22202aa6f319SMatthew G. Knepley   ierr = ISDestroy(&patch->cellIS);CHKERRQ(ierr);
22214d04e9f1SPatrick Farrell   if (withArtificial) {
2222c2e6f3c0SFlorian Wechsung     ierr = ISRestoreIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
2223c2e6f3c0SFlorian Wechsung   } else {
22244bbf5ea8SMatthew G. Knepley     ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
2225c2e6f3c0SFlorian Wechsung   }
22264d04e9f1SPatrick Farrell   if (isNonlinear) {
22270904074fSPatrick Farrell     ierr = ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
22284d04e9f1SPatrick Farrell   }
22294bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
22302aa6f319SMatthew G. Knepley   if (patch->viewMatrix) {
22312aa6f319SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
22322aa6f319SMatthew G. Knepley 
22332aa6f319SMatthew G. Knepley     ierr = PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch matrix for Point %D", point);CHKERRQ(ierr);
22342aa6f319SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) mat, name);CHKERRQ(ierr);
22352aa6f319SMatthew G. Knepley     ierr = ObjectView((PetscObject) mat, patch->viewerMatrix, patch->formatMatrix);CHKERRQ(ierr);
22362aa6f319SMatthew G. Knepley   }
22374bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
22384bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
22394bbf5ea8SMatthew G. Knepley }
22404bbf5ea8SMatthew G. Knepley 
2241fa84ea4cSLawrence Mitchell static PetscErrorCode MatSetValues_PCPatch_Private(Mat mat, PetscInt m, const PetscInt idxm[],
2242fa84ea4cSLawrence Mitchell                                                    PetscInt n, const PetscInt idxn[], const PetscScalar *v, InsertMode addv)
2243fa84ea4cSLawrence Mitchell {
2244fa84ea4cSLawrence Mitchell   Vec            data;
2245fa84ea4cSLawrence Mitchell   PetscScalar   *array;
2246fe988be2SFlorian Wechsung   PetscInt       bs, nz, i, j, cell;
2247fa84ea4cSLawrence Mitchell   PetscErrorCode ierr;
2248fa84ea4cSLawrence Mitchell 
2249fa84ea4cSLawrence Mitchell   ierr = MatShellGetContext(mat, &data);CHKERRQ(ierr);
2250fa84ea4cSLawrence Mitchell   ierr = VecGetBlockSize(data, &bs);CHKERRQ(ierr);
2251fa84ea4cSLawrence Mitchell   ierr = VecGetSize(data, &nz);CHKERRQ(ierr);
2252fa84ea4cSLawrence Mitchell   ierr = VecGetArray(data, &array);CHKERRQ(ierr);
2253fa84ea4cSLawrence Mitchell   if (m != n) SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Only for square insertion");
225433cbca70SPatrick Farrell   cell = (PetscInt)(idxm[0]/bs); /* use the fact that this is called once per cell */
2255fa84ea4cSLawrence Mitchell   for (i = 0; i < m; i++) {
2256fa84ea4cSLawrence Mitchell     if (idxm[i] != idxn[i]) SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Row and column indices must match!");
2257fa84ea4cSLawrence Mitchell     for (j = 0; j < n; j++) {
2258fa84ea4cSLawrence Mitchell       const PetscScalar v_ = v[i*bs + j];
2259fa84ea4cSLawrence Mitchell       /* Indexing is special to the data structure we have! */
2260fa84ea4cSLawrence Mitchell       if (addv == INSERT_VALUES) {
2261fe988be2SFlorian Wechsung         array[cell*bs*bs + i*bs + j] = v_;
2262fa84ea4cSLawrence Mitchell       } else {
2263fe988be2SFlorian Wechsung         array[cell*bs*bs + i*bs + j] += v_;
2264fa84ea4cSLawrence Mitchell       }
2265fa84ea4cSLawrence Mitchell     }
2266fa84ea4cSLawrence Mitchell   }
2267fa84ea4cSLawrence Mitchell   ierr = VecRestoreArray(data, &array);CHKERRQ(ierr);
2268fa84ea4cSLawrence Mitchell   PetscFunctionReturn(0);
2269fa84ea4cSLawrence Mitchell }
2270fa84ea4cSLawrence Mitchell 
2271fa84ea4cSLawrence Mitchell static PetscErrorCode PCPatchPrecomputePatchTensors_Private(PC pc)
2272fa84ea4cSLawrence Mitchell {
2273fa84ea4cSLawrence Mitchell   PC_PATCH       *patch = (PC_PATCH *)pc->data;
2274fa84ea4cSLawrence Mitchell   const PetscInt *cellsArray;
2275fa84ea4cSLawrence Mitchell   PetscInt        ncell, offset;
2276fa84ea4cSLawrence Mitchell   const PetscInt *dofMapArray;
2277fa84ea4cSLawrence Mitchell   PetscInt        i, j;
2278fa84ea4cSLawrence Mitchell   IS              dofMap;
2279fa84ea4cSLawrence Mitchell   IS              cellIS;
2280fa84ea4cSLawrence Mitchell   const PetscInt  ndof  = patch->totalDofsPerCell;
2281fa84ea4cSLawrence Mitchell   PetscErrorCode  ierr;
2282fa84ea4cSLawrence Mitchell   Mat             vecMat;
2283fe988be2SFlorian Wechsung   PetscInt        cStart, cEnd;
2284fe988be2SFlorian Wechsung   DM              dm, plex;
2285fe988be2SFlorian Wechsung 
2286e9c2c94bSFlorian Wechsung   ierr = ISGetSize(patch->cells, &ncell);CHKERRQ(ierr);
2287e9c2c94bSFlorian Wechsung   if (!ncell) { /* No cells to assemble over -> skip */
2288e9c2c94bSFlorian Wechsung     PetscFunctionReturn(0);
2289e9c2c94bSFlorian Wechsung   }
2290e9c2c94bSFlorian Wechsung 
2291fa84ea4cSLawrence Mitchell   ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
2292fa84ea4cSLawrence Mitchell 
2293b6bb21d1SLawrence Mitchell   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
2294b6bb21d1SLawrence Mitchell   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
2295b6bb21d1SLawrence Mitchell   dm = plex;
2296fa84ea4cSLawrence Mitchell   if (!patch->allCells) {
2297fa84ea4cSLawrence Mitchell     PetscHSetI      cells;
2298fa84ea4cSLawrence Mitchell     PetscHashIter   hi;
2299fa84ea4cSLawrence Mitchell     PetscInt        pStart, pEnd;
2300fa84ea4cSLawrence Mitchell     PetscInt        *allCells = NULL;
2301fa84ea4cSLawrence Mitchell     ierr = PetscHSetICreate(&cells);CHKERRQ(ierr);
2302fa84ea4cSLawrence Mitchell     ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
2303fa84ea4cSLawrence Mitchell     ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
2304fa84ea4cSLawrence Mitchell     for (i = pStart; i < pEnd; i++) {
2305fa84ea4cSLawrence Mitchell       ierr = PetscSectionGetDof(patch->cellCounts, i, &ncell);CHKERRQ(ierr);
2306fa84ea4cSLawrence Mitchell       ierr = PetscSectionGetOffset(patch->cellCounts, i, &offset);CHKERRQ(ierr);
2307fa84ea4cSLawrence Mitchell       if (ncell <= 0) continue;
2308fa84ea4cSLawrence Mitchell       for (j = 0; j < ncell; j++) {
2309*2f613bf5SBarry Smith         ierr = PetscHSetIAdd(cells, cellsArray[offset + j]);CHKERRQ(ierr);
2310fa84ea4cSLawrence Mitchell       }
2311fa84ea4cSLawrence Mitchell     }
2312fa84ea4cSLawrence Mitchell     ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
2313fa84ea4cSLawrence Mitchell     ierr = PetscHSetIGetSize(cells, &ncell);CHKERRQ(ierr);
2314fa84ea4cSLawrence Mitchell     ierr = PetscMalloc1(ncell, &allCells);CHKERRQ(ierr);
2315fe988be2SFlorian Wechsung     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2316fe988be2SFlorian Wechsung     ierr = PetscMalloc1(cEnd-cStart, &patch->precomputedTensorLocations);CHKERRQ(ierr);
2317fa84ea4cSLawrence Mitchell     i = 0;
2318fa84ea4cSLawrence Mitchell     PetscHashIterBegin(cells, hi);
2319fa84ea4cSLawrence Mitchell     while (!PetscHashIterAtEnd(cells, hi)) {
2320fe988be2SFlorian Wechsung       PetscHashIterGetKey(cells, hi, allCells[i]);
2321fe988be2SFlorian Wechsung       patch->precomputedTensorLocations[allCells[i]] = i;
2322fa84ea4cSLawrence Mitchell       PetscHashIterNext(cells, hi);
2323fe988be2SFlorian Wechsung       i++;
2324fa84ea4cSLawrence Mitchell     }
2325fa84ea4cSLawrence Mitchell     ierr = PetscHSetIDestroy(&cells);CHKERRQ(ierr);
2326fa84ea4cSLawrence Mitchell     ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, allCells, PETSC_OWN_POINTER, &patch->allCells);CHKERRQ(ierr);
2327fa84ea4cSLawrence Mitchell   }
2328f98464cbSLawrence Mitchell   ierr = ISGetSize(patch->allCells, &ncell);CHKERRQ(ierr);
2329fa84ea4cSLawrence Mitchell   if (!patch->cellMats) {
2330fa84ea4cSLawrence Mitchell     ierr = VecCreateSeq(PETSC_COMM_SELF, ncell*ndof*ndof, &patch->cellMats);CHKERRQ(ierr);
2331fa84ea4cSLawrence Mitchell     ierr = VecSetBlockSize(patch->cellMats, ndof);CHKERRQ(ierr);
2332fa84ea4cSLawrence Mitchell   }
2333fa84ea4cSLawrence Mitchell   ierr = VecSet(patch->cellMats, 0);CHKERRQ(ierr);
2334fa84ea4cSLawrence Mitchell 
2335fa84ea4cSLawrence Mitchell   ierr = MatCreateShell(PETSC_COMM_SELF, ncell*ndof, ncell*ndof, ncell*ndof, ncell*ndof,
233689d5b078SSatish Balay                         (void*)patch->cellMats, &vecMat);CHKERRQ(ierr);
2337fa84ea4cSLawrence Mitchell   ierr = MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void(*)(void))&MatSetValues_PCPatch_Private);CHKERRQ(ierr);
2338fa84ea4cSLawrence Mitchell   ierr = ISGetSize(patch->allCells, &ncell);CHKERRQ(ierr);
2339fa84ea4cSLawrence Mitchell   ierr = ISCreateStride(PETSC_COMM_SELF, ndof*ncell, 0, 1, &dofMap);CHKERRQ(ierr);
2340fa84ea4cSLawrence Mitchell   ierr = ISGetIndices(dofMap, &dofMapArray);CHKERRQ(ierr);
2341fa84ea4cSLawrence Mitchell   ierr = ISGetIndices(patch->allCells, &cellsArray);CHKERRQ(ierr);
2342fa84ea4cSLawrence Mitchell   ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray, PETSC_USE_POINTER, &cellIS);CHKERRQ(ierr);
2343fa84ea4cSLawrence Mitchell   PetscStackPush("PCPatch user callback");
2344fa84ea4cSLawrence Mitchell   /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */
2345fa84ea4cSLawrence Mitchell   ierr = patch->usercomputeop(pc, -1, NULL, vecMat, cellIS, ndof*ncell, dofMapArray, NULL, patch->usercomputeopctx);CHKERRQ(ierr);
2346f98464cbSLawrence Mitchell   PetscStackPop;
2347fa84ea4cSLawrence Mitchell   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
2348fa84ea4cSLawrence Mitchell   ierr = MatDestroy(&vecMat);CHKERRQ(ierr);
2349fa84ea4cSLawrence Mitchell   ierr = ISRestoreIndices(patch->allCells, &cellsArray);CHKERRQ(ierr);
2350fa84ea4cSLawrence Mitchell   ierr = ISRestoreIndices(dofMap, &dofMapArray);CHKERRQ(ierr);
2351fa84ea4cSLawrence Mitchell   ierr = ISDestroy(&dofMap);CHKERRQ(ierr);
2352f98464cbSLawrence Mitchell 
2353f98464cbSLawrence Mitchell   if (patch->usercomputeopintfacet) {
2354f98464cbSLawrence Mitchell     PetscInt nIntFacets;
2355f98464cbSLawrence Mitchell     IS       intFacetsIS;
2356f98464cbSLawrence Mitchell     const PetscInt *intFacetsArray = NULL;
2357f98464cbSLawrence Mitchell     if (!patch->allIntFacets) {
2358f98464cbSLawrence Mitchell       PetscHSetI      facets;
2359f98464cbSLawrence Mitchell       PetscHashIter   hi;
2360f98464cbSLawrence Mitchell       PetscInt        pStart, pEnd, fStart, fEnd;
2361f98464cbSLawrence Mitchell       PetscInt        *allIntFacets = NULL;
2362f98464cbSLawrence Mitchell       ierr = PetscHSetICreate(&facets);CHKERRQ(ierr);
2363f98464cbSLawrence Mitchell       ierr = ISGetIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
2364f98464cbSLawrence Mitchell       ierr = PetscSectionGetChart(patch->intFacetCounts, &pStart, &pEnd);CHKERRQ(ierr);
2365de2d1767SPatrick Farrell       ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
2366f98464cbSLawrence Mitchell       for (i = pStart; i < pEnd; i++) {
2367f98464cbSLawrence Mitchell         ierr = PetscSectionGetDof(patch->intFacetCounts, i, &nIntFacets);CHKERRQ(ierr);
2368f98464cbSLawrence Mitchell         ierr = PetscSectionGetOffset(patch->intFacetCounts, i, &offset);CHKERRQ(ierr);
2369f98464cbSLawrence Mitchell         if (nIntFacets <= 0) continue;
2370f98464cbSLawrence Mitchell         for (j = 0; j < nIntFacets; j++) {
2371*2f613bf5SBarry Smith           ierr = PetscHSetIAdd(facets, intFacetsArray[offset + j]);CHKERRQ(ierr);
2372f98464cbSLawrence Mitchell         }
2373f98464cbSLawrence Mitchell       }
2374f98464cbSLawrence Mitchell       ierr = ISRestoreIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
2375f98464cbSLawrence Mitchell       ierr = PetscHSetIGetSize(facets, &nIntFacets);CHKERRQ(ierr);
2376f98464cbSLawrence Mitchell       ierr = PetscMalloc1(nIntFacets, &allIntFacets);CHKERRQ(ierr);
2377f98464cbSLawrence Mitchell       ierr = PetscMalloc1(fEnd-fStart, &patch->precomputedIntFacetTensorLocations);CHKERRQ(ierr);
2378f98464cbSLawrence Mitchell       i = 0;
2379f98464cbSLawrence Mitchell       PetscHashIterBegin(facets, hi);
2380f98464cbSLawrence Mitchell       while (!PetscHashIterAtEnd(facets, hi)) {
2381f98464cbSLawrence Mitchell         PetscHashIterGetKey(facets, hi, allIntFacets[i]);
2382de2d1767SPatrick Farrell         patch->precomputedIntFacetTensorLocations[allIntFacets[i] - fStart] = i;
2383f98464cbSLawrence Mitchell         PetscHashIterNext(facets, hi);
2384f98464cbSLawrence Mitchell         i++;
2385f98464cbSLawrence Mitchell       }
2386f98464cbSLawrence Mitchell       ierr = PetscHSetIDestroy(&facets);CHKERRQ(ierr);
2387f98464cbSLawrence Mitchell       ierr = ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, allIntFacets, PETSC_OWN_POINTER, &patch->allIntFacets);CHKERRQ(ierr);
2388f98464cbSLawrence Mitchell     }
2389f98464cbSLawrence Mitchell     ierr = ISGetSize(patch->allIntFacets, &nIntFacets);CHKERRQ(ierr);
2390f98464cbSLawrence Mitchell     if (!patch->intFacetMats) {
2391f98464cbSLawrence Mitchell       ierr = VecCreateSeq(PETSC_COMM_SELF, nIntFacets*ndof*ndof*4, &patch->intFacetMats);CHKERRQ(ierr);
2392f98464cbSLawrence Mitchell       ierr = VecSetBlockSize(patch->intFacetMats, ndof*2);CHKERRQ(ierr);
2393f98464cbSLawrence Mitchell     }
2394f98464cbSLawrence Mitchell     ierr = VecSet(patch->intFacetMats, 0);CHKERRQ(ierr);
2395f98464cbSLawrence Mitchell 
2396f98464cbSLawrence Mitchell     ierr = MatCreateShell(PETSC_COMM_SELF, nIntFacets*ndof*2, nIntFacets*ndof*2, nIntFacets*ndof*2, nIntFacets*ndof*2,
239789d5b078SSatish Balay                           (void*)patch->intFacetMats, &vecMat);CHKERRQ(ierr);
2398f98464cbSLawrence Mitchell     ierr = MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void(*)(void))&MatSetValues_PCPatch_Private);CHKERRQ(ierr);
2399f98464cbSLawrence Mitchell     ierr = ISCreateStride(PETSC_COMM_SELF, 2*ndof*nIntFacets, 0, 1, &dofMap);CHKERRQ(ierr);
2400f98464cbSLawrence Mitchell     ierr = ISGetIndices(dofMap, &dofMapArray);CHKERRQ(ierr);
2401f98464cbSLawrence Mitchell     ierr = ISGetIndices(patch->allIntFacets, &intFacetsArray);CHKERRQ(ierr);
2402f98464cbSLawrence Mitchell     ierr = ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, intFacetsArray, PETSC_USE_POINTER, &intFacetsIS);CHKERRQ(ierr);
2403f98464cbSLawrence Mitchell     PetscStackPush("PCPatch user callback (interior facets)");
2404f98464cbSLawrence Mitchell     /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */
2405f98464cbSLawrence Mitchell     ierr = patch->usercomputeopintfacet(pc, -1, NULL, vecMat, intFacetsIS, 2*ndof*nIntFacets, dofMapArray, NULL, patch->usercomputeopintfacetctx);CHKERRQ(ierr);
2406f98464cbSLawrence Mitchell     PetscStackPop;
2407f98464cbSLawrence Mitchell     ierr = ISDestroy(&intFacetsIS);CHKERRQ(ierr);
2408f98464cbSLawrence Mitchell     ierr = MatDestroy(&vecMat);CHKERRQ(ierr);
2409f98464cbSLawrence Mitchell     ierr = ISRestoreIndices(patch->allIntFacets, &intFacetsArray);CHKERRQ(ierr);
2410f98464cbSLawrence Mitchell     ierr = ISRestoreIndices(dofMap, &dofMapArray);CHKERRQ(ierr);
2411f98464cbSLawrence Mitchell     ierr = ISDestroy(&dofMap);CHKERRQ(ierr);
2412f98464cbSLawrence Mitchell   }
2413b6bb21d1SLawrence Mitchell   ierr = DMDestroy(&dm);CHKERRQ(ierr);
2414fa84ea4cSLawrence Mitchell   ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
2415fa84ea4cSLawrence Mitchell 
2416fa84ea4cSLawrence Mitchell   PetscFunctionReturn(0);
2417fa84ea4cSLawrence Mitchell }
2418fa84ea4cSLawrence Mitchell 
24190904074fSPatrick Farrell PetscErrorCode PCPatch_ScatterLocal_Private(PC pc, PetscInt p, Vec x, Vec y, InsertMode mode, ScatterMode scat, PatchScatterType scattertype)
24204bbf5ea8SMatthew G. Knepley {
24214bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch     = (PC_PATCH *) pc->data;
24224bbf5ea8SMatthew G. Knepley   const PetscScalar *xArray    = NULL;
24234bbf5ea8SMatthew G. Knepley   PetscScalar       *yArray    = NULL;
24244bbf5ea8SMatthew G. Knepley   const PetscInt    *gtolArray = NULL;
24254bbf5ea8SMatthew G. Knepley   PetscInt           dof, offset, lidx;
24264bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
24274bbf5ea8SMatthew G. Knepley 
24284bbf5ea8SMatthew G. Knepley   PetscFunctionBeginHot;
24294bbf5ea8SMatthew G. Knepley   ierr = VecGetArrayRead(x, &xArray);CHKERRQ(ierr);
24304bbf5ea8SMatthew G. Knepley   ierr = VecGetArray(y, &yArray);CHKERRQ(ierr);
24310904074fSPatrick Farrell   if (scattertype == SCATTER_WITHARTIFICIAL) {
2432c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof);CHKERRQ(ierr);
2433c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offset);CHKERRQ(ierr);
2434c2e6f3c0SFlorian Wechsung     ierr = ISGetIndices(patch->gtolWithArtificial, &gtolArray);CHKERRQ(ierr);
24350904074fSPatrick Farrell   } else if (scattertype == SCATTER_WITHALL) {
24360904074fSPatrick Farrell     ierr = PetscSectionGetDof(patch->gtolCountsWithAll, p, &dof);CHKERRQ(ierr);
24370904074fSPatrick Farrell     ierr = PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offset);CHKERRQ(ierr);
24380904074fSPatrick Farrell     ierr = ISGetIndices(patch->gtolWithAll, &gtolArray);CHKERRQ(ierr);
2439c2e6f3c0SFlorian Wechsung   } else {
24404bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr);
24414bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
24424bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
2443c2e6f3c0SFlorian Wechsung   }
24444bbf5ea8SMatthew G. Knepley   if (mode == INSERT_VALUES && scat != SCATTER_FORWARD) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't insert if not scattering forward\n");
24454bbf5ea8SMatthew G. Knepley   if (mode == ADD_VALUES    && scat != SCATTER_REVERSE) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't add if not scattering reverse\n");
24464bbf5ea8SMatthew G. Knepley   for (lidx = 0; lidx < dof; ++lidx) {
24474bbf5ea8SMatthew G. Knepley     const PetscInt gidx = gtolArray[offset+lidx];
24484bbf5ea8SMatthew G. Knepley 
24494bbf5ea8SMatthew G. Knepley     if (mode == INSERT_VALUES) yArray[lidx]  = xArray[gidx]; /* Forward */
24504bbf5ea8SMatthew G. Knepley     else                       yArray[gidx] += xArray[lidx]; /* Reverse */
24514bbf5ea8SMatthew G. Knepley   }
24520904074fSPatrick Farrell   if (scattertype == SCATTER_WITHARTIFICIAL) {
2453c2e6f3c0SFlorian Wechsung     ierr = ISRestoreIndices(patch->gtolWithArtificial, &gtolArray);CHKERRQ(ierr);
24540904074fSPatrick Farrell   } else if (scattertype == SCATTER_WITHALL) {
24550904074fSPatrick Farrell     ierr = ISRestoreIndices(patch->gtolWithAll, &gtolArray);CHKERRQ(ierr);
2456c2e6f3c0SFlorian Wechsung   } else {
24574bbf5ea8SMatthew G. Knepley     ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
2458c2e6f3c0SFlorian Wechsung   }
24594bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArrayRead(x, &xArray);CHKERRQ(ierr);
24604bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArray(y, &yArray);CHKERRQ(ierr);
24614bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
24624bbf5ea8SMatthew G. Knepley }
24634bbf5ea8SMatthew G. Knepley 
2464dadc69c5SMatthew G. Knepley static PetscErrorCode PCSetUp_PATCH_Linear(PC pc)
2465dadc69c5SMatthew G. Knepley {
2466dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2467dadc69c5SMatthew G. Knepley   const char    *prefix;
2468dadc69c5SMatthew G. Knepley   PetscInt       i;
2469dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2470dadc69c5SMatthew G. Knepley 
2471dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2472dadc69c5SMatthew G. Knepley   if (!pc->setupcalled) {
2473c73d2cf6SLawrence Mitchell     if (!patch->save_operators && patch->denseinverse) SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Can't have dense inverse without save operators");
2474c73d2cf6SLawrence Mitchell     if (!patch->denseinverse) {
2475dadc69c5SMatthew G. Knepley       ierr = PetscMalloc1(patch->npatch, &patch->solver);CHKERRQ(ierr);
2476dadc69c5SMatthew G. Knepley       ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr);
2477dadc69c5SMatthew G. Knepley       for (i = 0; i < patch->npatch; ++i) {
2478dadc69c5SMatthew G. Knepley         KSP ksp;
2479dadc69c5SMatthew G. Knepley         PC  subpc;
2480dadc69c5SMatthew G. Knepley 
2481dadc69c5SMatthew G. Knepley         ierr = KSPCreate(PETSC_COMM_SELF, &ksp);CHKERRQ(ierr);
2482ddad275aSPatrick Farrell         ierr = KSPSetErrorIfNotConverged(ksp, pc->erroriffailure);CHKERRQ(ierr);
2483dadc69c5SMatthew G. Knepley         ierr = KSPSetOptionsPrefix(ksp, prefix);CHKERRQ(ierr);
2484dadc69c5SMatthew G. Knepley         ierr = KSPAppendOptionsPrefix(ksp, "sub_");CHKERRQ(ierr);
2485dadc69c5SMatthew G. Knepley         ierr = PetscObjectIncrementTabLevel((PetscObject) ksp, (PetscObject) pc, 1);CHKERRQ(ierr);
2486dadc69c5SMatthew G. Knepley         ierr = KSPGetPC(ksp, &subpc);CHKERRQ(ierr);
2487dadc69c5SMatthew G. Knepley         ierr = PetscObjectIncrementTabLevel((PetscObject) subpc, (PetscObject) pc, 1);CHKERRQ(ierr);
2488dadc69c5SMatthew G. Knepley         ierr = PetscLogObjectParent((PetscObject) pc, (PetscObject) ksp);CHKERRQ(ierr);
2489dadc69c5SMatthew G. Knepley         patch->solver[i] = (PetscObject) ksp;
2490dadc69c5SMatthew G. Knepley       }
2491dadc69c5SMatthew G. Knepley     }
2492c73d2cf6SLawrence Mitchell   }
2493dadc69c5SMatthew G. Knepley   if (patch->save_operators) {
2494e9c2c94bSFlorian Wechsung     if (patch->precomputeElementTensors) {
2495fe988be2SFlorian Wechsung       ierr = PCPatchPrecomputePatchTensors_Private(pc);CHKERRQ(ierr);
2496fe988be2SFlorian Wechsung     }
2497dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {
249834d8b122SPatrick Farrell       ierr = PCPatchComputeOperator_Internal(pc, NULL, patch->mat[i], i, PETSC_FALSE);CHKERRQ(ierr);
2499c73d2cf6SLawrence Mitchell       if (!patch->denseinverse) {
2500dadc69c5SMatthew G. Knepley         ierr = KSPSetOperators((KSP) patch->solver[i], patch->mat[i], patch->mat[i]);CHKERRQ(ierr);
25019d4fc724SLawrence Mitchell       } else if (patch->mat[i] && !patch->densesolve) {
25029d4fc724SLawrence Mitchell         /* Setup matmult callback */
25039d4fc724SLawrence Mitchell         ierr = MatGetOperation(patch->mat[i], MATOP_MULT, (void (**)(void))&patch->densesolve);CHKERRQ(ierr);
2504dadc69c5SMatthew G. Knepley       }
2505dadc69c5SMatthew G. Knepley     }
2506c73d2cf6SLawrence Mitchell   }
250734d8b122SPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
250834d8b122SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {
25091202d238SPatrick Farrell       /* Instead of padding patch->patchUpdate with zeros to get */
25101202d238SPatrick Farrell       /* patch->patchUpdateWithArtificial and then multiplying with the matrix, */
251134d8b122SPatrick Farrell       /* just get rid of the columns that correspond to the dofs with */
251234d8b122SPatrick Farrell       /* artificial bcs. That's of course fairly inefficient, hopefully we */
251334d8b122SPatrick Farrell       /* can just assemble the rectangular matrix in the first place. */
251434d8b122SPatrick Farrell       Mat matSquare;
251534d8b122SPatrick Farrell       IS rowis;
251634d8b122SPatrick Farrell       PetscInt dof;
251734d8b122SPatrick Farrell 
251834d8b122SPatrick Farrell       ierr = MatGetSize(patch->mat[i], &dof, NULL);CHKERRQ(ierr);
251934d8b122SPatrick Farrell       if (dof == 0) {
252034d8b122SPatrick Farrell         patch->matWithArtificial[i] = NULL;
252134d8b122SPatrick Farrell         continue;
252234d8b122SPatrick Farrell       }
252334d8b122SPatrick Farrell 
252434d8b122SPatrick Farrell       ierr = PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE);CHKERRQ(ierr);
252534d8b122SPatrick Farrell       ierr = PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE);CHKERRQ(ierr);
252634d8b122SPatrick Farrell 
252734d8b122SPatrick Farrell       ierr = MatGetSize(matSquare, &dof, NULL);CHKERRQ(ierr);
252834d8b122SPatrick Farrell       ierr = ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis);CHKERRQ(ierr);
252934d8b122SPatrick Farrell       if (pc->setupcalled) {
253034d8b122SPatrick Farrell         ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_REUSE_MATRIX, &patch->matWithArtificial[i]);CHKERRQ(ierr);
253134d8b122SPatrick Farrell       } else {
253234d8b122SPatrick Farrell         ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &patch->matWithArtificial[i]);CHKERRQ(ierr);
253334d8b122SPatrick Farrell       }
253434d8b122SPatrick Farrell       ierr = ISDestroy(&rowis);CHKERRQ(ierr);
253534d8b122SPatrick Farrell       ierr = MatDestroy(&matSquare);CHKERRQ(ierr);
253634d8b122SPatrick Farrell     }
253734d8b122SPatrick Farrell   }
2538dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
2539dadc69c5SMatthew G. Knepley }
2540dadc69c5SMatthew G. Knepley 
25414bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUp_PATCH(PC pc)
25424bbf5ea8SMatthew G. Knepley {
25434bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2544557beb66SLawrence Mitchell   PetscInt       i;
254539fd2e8aSPatrick Farrell   PetscBool      isNonlinear;
25469d4fc724SLawrence Mitchell   PetscInt       maxDof = -1, maxDofWithArtificial = -1;
25474bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
25484bbf5ea8SMatthew G. Knepley 
25494bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
25504bbf5ea8SMatthew G. Knepley   if (!pc->setupcalled) {
25514bbf5ea8SMatthew G. Knepley     PetscInt pStart, pEnd, p;
25524bbf5ea8SMatthew G. Knepley     PetscInt localSize;
25534bbf5ea8SMatthew G. Knepley 
25544bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventBegin(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr);
25554bbf5ea8SMatthew G. Knepley 
2556debbdec3SPatrick Farrell     isNonlinear = patch->isNonlinear;
25575f824522SMatthew G. Knepley     if (!patch->nsubspaces) {
2558b6bb21d1SLawrence Mitchell       DM           dm, plex;
25595f824522SMatthew G. Knepley       PetscSection s;
2560e72c1634SMatthew G. Knepley       PetscInt     cStart, cEnd, c, Nf, f, numGlobalBcs = 0, *globalBcs, *Nb, totNb = 0, **cellDofs;
25615f824522SMatthew G. Knepley 
25625f824522SMatthew G. Knepley       ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
25635f824522SMatthew G. Knepley       if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONG, "Must set DM for PCPATCH or call PCPatchSetDiscretisationInfo()");
2564b6bb21d1SLawrence Mitchell       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
2565b6bb21d1SLawrence Mitchell       dm = plex;
256692fd8e1eSJed Brown       ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
25675f824522SMatthew G. Knepley       ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
25685f824522SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
25695f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
25705f824522SMatthew G. Knepley         PetscInt cdof;
25715f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
25725f824522SMatthew G. Knepley         numGlobalBcs += cdof;
25735f824522SMatthew G. Knepley       }
25745f824522SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
25755f824522SMatthew G. Knepley       ierr = PetscMalloc3(Nf, &Nb, Nf, &cellDofs, numGlobalBcs, &globalBcs);CHKERRQ(ierr);
25765f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
25775f824522SMatthew G. Knepley         PetscFE        fe;
25785f824522SMatthew G. Knepley         PetscDualSpace sp;
25795f824522SMatthew G. Knepley         PetscInt       cdoff = 0;
25805f824522SMatthew G. Knepley 
258144a7f3ddSMatthew G. Knepley         ierr = DMGetField(dm, f, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
25825f824522SMatthew G. Knepley         /* ierr = PetscFEGetNumComponents(fe, &Nc[f]);CHKERRQ(ierr); */
25835f824522SMatthew G. Knepley         ierr = PetscFEGetDualSpace(fe, &sp);CHKERRQ(ierr);
25845f824522SMatthew G. Knepley         ierr = PetscDualSpaceGetDimension(sp, &Nb[f]);CHKERRQ(ierr);
25855f824522SMatthew G. Knepley         totNb += Nb[f];
25865f824522SMatthew G. Knepley 
25875f824522SMatthew G. Knepley         ierr = PetscMalloc1((cEnd-cStart)*Nb[f], &cellDofs[f]);CHKERRQ(ierr);
25885f824522SMatthew G. Knepley         for (c = cStart; c < cEnd; ++c) {
25895f824522SMatthew G. Knepley           PetscInt *closure = NULL;
25905f824522SMatthew G. Knepley           PetscInt  clSize  = 0, cl;
25915f824522SMatthew G. Knepley 
25925f824522SMatthew G. Knepley           ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
25935f824522SMatthew G. Knepley           for (cl = 0; cl < clSize*2; cl += 2) {
25945f824522SMatthew G. Knepley             const PetscInt p = closure[cl];
25955f824522SMatthew G. Knepley             PetscInt       fdof, d, foff;
25965f824522SMatthew G. Knepley 
25975f824522SMatthew G. Knepley             ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr);
25985f824522SMatthew G. Knepley             ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr);
25995f824522SMatthew G. Knepley             for (d = 0; d < fdof; ++d, ++cdoff) cellDofs[f][cdoff] = foff + d;
26005f824522SMatthew G. Knepley           }
26015f824522SMatthew G. Knepley           ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
26025f824522SMatthew G. Knepley         }
26035f824522SMatthew G. Knepley         if (cdoff != (cEnd-cStart)*Nb[f]) SETERRQ4(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_SIZ, "Total number of cellDofs %D for field %D should be Nc (%D) * cellDof (%D)", cdoff, f, cEnd-cStart, Nb[f]);
26045f824522SMatthew G. Knepley       }
26055f824522SMatthew G. Knepley       numGlobalBcs = 0;
26065f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
26075f824522SMatthew G. Knepley         const PetscInt *ind;
26085f824522SMatthew G. Knepley         PetscInt        off, cdof, d;
26095f824522SMatthew G. Knepley 
26105f824522SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
26115f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
26125f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &ind);CHKERRQ(ierr);
26135f824522SMatthew G. Knepley         for (d = 0; d < cdof; ++d) globalBcs[numGlobalBcs++] = off + ind[d];
26145f824522SMatthew G. Knepley       }
26155f824522SMatthew G. Knepley 
26165f824522SMatthew G. Knepley       ierr = PCPatchSetDiscretisationInfoCombined(pc, dm, Nb, (const PetscInt **) cellDofs, numGlobalBcs, globalBcs, numGlobalBcs, globalBcs);CHKERRQ(ierr);
26175f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
26185f824522SMatthew G. Knepley         ierr = PetscFree(cellDofs[f]);CHKERRQ(ierr);
26195f824522SMatthew G. Knepley       }
26205f824522SMatthew G. Knepley       ierr = PetscFree3(Nb, cellDofs, globalBcs);CHKERRQ(ierr);
262192d50984SMatthew G. Knepley       ierr = PCPatchSetComputeFunction(pc, PCPatchComputeFunction_DMPlex_Private, NULL);CHKERRQ(ierr);
26225f824522SMatthew G. Knepley       ierr = PCPatchSetComputeOperator(pc, PCPatchComputeOperator_DMPlex_Private, NULL);CHKERRQ(ierr);
2623b6bb21d1SLawrence Mitchell       ierr = DMDestroy(&dm);CHKERRQ(ierr);
26245f824522SMatthew G. Knepley     }
26255f824522SMatthew G. Knepley 
26264bbf5ea8SMatthew G. Knepley     localSize = patch->subspaceOffsets[patch->nsubspaces];
26271202d238SPatrick Farrell     ierr = VecCreateSeq(PETSC_COMM_SELF, localSize, &patch->localRHS);CHKERRQ(ierr);
26281202d238SPatrick Farrell     ierr = VecSetUp(patch->localRHS);CHKERRQ(ierr);
26291202d238SPatrick Farrell     ierr = VecDuplicate(patch->localRHS, &patch->localUpdate);CHKERRQ(ierr);
26304bbf5ea8SMatthew G. Knepley     ierr = PCPatchCreateCellPatches(pc);CHKERRQ(ierr);
26314bbf5ea8SMatthew G. Knepley     ierr = PCPatchCreateCellPatchDiscretisationInfo(pc);CHKERRQ(ierr);
26324bbf5ea8SMatthew G. Knepley 
26334bbf5ea8SMatthew G. Knepley     /* OK, now build the work vectors */
26344bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, &pEnd);CHKERRQ(ierr);
2635c2e6f3c0SFlorian Wechsung 
263661c4b389SFlorian Wechsung     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
2637c2e6f3c0SFlorian Wechsung       ierr = PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithArtificial);CHKERRQ(ierr);
2638c2e6f3c0SFlorian Wechsung     }
26390904074fSPatrick Farrell     if (isNonlinear) {
26400904074fSPatrick Farrell       ierr = PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithAll);CHKERRQ(ierr);
26410904074fSPatrick Farrell     }
26424bbf5ea8SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26434bbf5ea8SMatthew G. Knepley       PetscInt dof;
26444bbf5ea8SMatthew G. Knepley 
26454bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr);
2646*2f613bf5SBarry Smith       maxDof = PetscMax(maxDof, dof);
26470904074fSPatrick Farrell       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
26483bb0e8f7SKarl Rupp         const PetscInt    *gtolArray, *gtolArrayWithArtificial = NULL;
26493bb0e8f7SKarl Rupp         PetscInt           numPatchDofs, offset;
26503bb0e8f7SKarl Rupp         PetscInt           numPatchDofsWithArtificial, offsetWithArtificial;
26513bb0e8f7SKarl Rupp         PetscInt           dofWithoutArtificialCounter = 0;
26523bb0e8f7SKarl Rupp         PetscInt          *patchWithoutArtificialToWithArtificialArray;
26533bb0e8f7SKarl Rupp 
2654c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof);CHKERRQ(ierr);
26559d4fc724SLawrence Mitchell         maxDofWithArtificial = PetscMax(maxDofWithArtificial, dof);
2656c2e6f3c0SFlorian Wechsung 
2657e047a90bSFlorian Wechsung         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
2658e047a90bSFlorian Wechsung         /* the index in the patch with all dofs */
2659c2e6f3c0SFlorian Wechsung         ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
266063deea8eSPatrick Farrell 
2661c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs);CHKERRQ(ierr);
266247aca4a6SPatrick Farrell         if (numPatchDofs == 0) {
266347aca4a6SPatrick Farrell           patch->dofMappingWithoutToWithArtificial[p-pStart] = NULL;
266447aca4a6SPatrick Farrell           continue;
266547aca4a6SPatrick Farrell         }
266663deea8eSPatrick Farrell 
2667c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
2668c2e6f3c0SFlorian Wechsung         ierr = ISGetIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial);CHKERRQ(ierr);
2669c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &numPatchDofsWithArtificial);CHKERRQ(ierr);
2670c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offsetWithArtificial);CHKERRQ(ierr);
2671c2e6f3c0SFlorian Wechsung 
2672c2e6f3c0SFlorian Wechsung         ierr = PetscMalloc1(numPatchDofs, &patchWithoutArtificialToWithArtificialArray);CHKERRQ(ierr);
2673b0c21b6aSKarl Rupp         for (i=0; i<numPatchDofsWithArtificial; i++) {
2674e047a90bSFlorian Wechsung           if (gtolArrayWithArtificial[i+offsetWithArtificial] == gtolArray[offset+dofWithoutArtificialCounter]) {
2675c2e6f3c0SFlorian Wechsung             patchWithoutArtificialToWithArtificialArray[dofWithoutArtificialCounter] = i;
2676c2e6f3c0SFlorian Wechsung             dofWithoutArtificialCounter++;
2677c2e6f3c0SFlorian Wechsung             if (dofWithoutArtificialCounter == numPatchDofs)
2678c2e6f3c0SFlorian Wechsung               break;
2679c2e6f3c0SFlorian Wechsung           }
2680c2e6f3c0SFlorian Wechsung         }
2681168bb5e4SPatrick Farrell         ierr = ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutArtificialToWithArtificialArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithArtificial[p-pStart]);CHKERRQ(ierr);
2682c2e6f3c0SFlorian Wechsung         ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
2683c2e6f3c0SFlorian Wechsung         ierr = ISRestoreIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial);CHKERRQ(ierr);
2684c2e6f3c0SFlorian Wechsung       }
26850904074fSPatrick Farrell       if (isNonlinear) {
26860904074fSPatrick Farrell         const PetscInt    *gtolArray, *gtolArrayWithAll = NULL;
26870904074fSPatrick Farrell         PetscInt           numPatchDofs, offset;
26880904074fSPatrick Farrell         PetscInt           numPatchDofsWithAll, offsetWithAll;
26890904074fSPatrick Farrell         PetscInt           dofWithoutAllCounter = 0;
26900904074fSPatrick Farrell         PetscInt          *patchWithoutAllToWithAllArray;
26910904074fSPatrick Farrell 
26920904074fSPatrick Farrell         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
26930904074fSPatrick Farrell         /* the index in the patch with all dofs */
26940904074fSPatrick Farrell         ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
26950904074fSPatrick Farrell 
26960904074fSPatrick Farrell         ierr = PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs);CHKERRQ(ierr);
269747aca4a6SPatrick Farrell         if (numPatchDofs == 0) {
2698b88cb22dSPatrick Farrell           patch->dofMappingWithoutToWithAll[p-pStart] = NULL;
269947aca4a6SPatrick Farrell           continue;
270047aca4a6SPatrick Farrell         }
27010904074fSPatrick Farrell 
27020904074fSPatrick Farrell         ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
27030904074fSPatrick Farrell         ierr = ISGetIndices(patch->gtolWithAll, &gtolArrayWithAll);CHKERRQ(ierr);
27040904074fSPatrick Farrell         ierr = PetscSectionGetDof(patch->gtolCountsWithAll, p, &numPatchDofsWithAll);CHKERRQ(ierr);
27050904074fSPatrick Farrell         ierr = PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offsetWithAll);CHKERRQ(ierr);
27060904074fSPatrick Farrell 
27070904074fSPatrick Farrell         ierr = PetscMalloc1(numPatchDofs, &patchWithoutAllToWithAllArray);CHKERRQ(ierr);
27080904074fSPatrick Farrell 
27090904074fSPatrick Farrell         for (i=0; i<numPatchDofsWithAll; i++) {
27100904074fSPatrick Farrell           if (gtolArrayWithAll[i+offsetWithAll] == gtolArray[offset+dofWithoutAllCounter]) {
27110904074fSPatrick Farrell             patchWithoutAllToWithAllArray[dofWithoutAllCounter] = i;
27120904074fSPatrick Farrell             dofWithoutAllCounter++;
27130904074fSPatrick Farrell             if (dofWithoutAllCounter == numPatchDofs)
27140904074fSPatrick Farrell               break;
27150904074fSPatrick Farrell           }
27160904074fSPatrick Farrell         }
2717168bb5e4SPatrick Farrell         ierr = ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutAllToWithAllArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithAll[p-pStart]);CHKERRQ(ierr);
27180904074fSPatrick Farrell         ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
27190904074fSPatrick Farrell         ierr = ISRestoreIndices(patch->gtolWithAll, &gtolArrayWithAll);CHKERRQ(ierr);
27200904074fSPatrick Farrell       }
27214bbf5ea8SMatthew G. Knepley     }
272260dd46caSLawrence Mitchell     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
27239d4fc724SLawrence Mitchell       ierr = VecCreateSeq(PETSC_COMM_SELF, maxDofWithArtificial, &patch->patchRHSWithArtificial);CHKERRQ(ierr);
27249d4fc724SLawrence Mitchell       ierr = VecSetUp(patch->patchRHSWithArtificial);CHKERRQ(ierr);
272560dd46caSLawrence Mitchell     }
27269d4fc724SLawrence Mitchell     ierr = VecCreateSeq(PETSC_COMM_SELF, maxDof, &patch->patchRHS);CHKERRQ(ierr);
27279d4fc724SLawrence Mitchell     ierr = VecSetUp(patch->patchRHS);CHKERRQ(ierr);
27289d4fc724SLawrence Mitchell     ierr = VecCreateSeq(PETSC_COMM_SELF, maxDof, &patch->patchUpdate);CHKERRQ(ierr);
27299d4fc724SLawrence Mitchell     ierr = VecSetUp(patch->patchUpdate);CHKERRQ(ierr);
27304bbf5ea8SMatthew G. Knepley     if (patch->save_operators) {
27314bbf5ea8SMatthew G. Knepley       ierr = PetscMalloc1(patch->npatch, &patch->mat);CHKERRQ(ierr);
27324bbf5ea8SMatthew G. Knepley       for (i = 0; i < patch->npatch; ++i) {
2733c2e6f3c0SFlorian Wechsung         ierr = PCPatchCreateMatrix_Private(pc, i, &patch->mat[i], PETSC_FALSE);CHKERRQ(ierr);
27344bbf5ea8SMatthew G. Knepley       }
27354bbf5ea8SMatthew G. Knepley     }
27364bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr);
27374bbf5ea8SMatthew G. Knepley 
27384bbf5ea8SMatthew G. Knepley     /* If desired, calculate weights for dof multiplicity */
27394bbf5ea8SMatthew G. Knepley     if (patch->partition_of_unity) {
27403bb0e8f7SKarl Rupp       PetscScalar *input = NULL;
27413bb0e8f7SKarl Rupp       PetscScalar *output = NULL;
27423bb0e8f7SKarl Rupp       Vec         global;
27433bb0e8f7SKarl Rupp 
27441202d238SPatrick Farrell       ierr = VecDuplicate(patch->localRHS, &patch->dof_weights);CHKERRQ(ierr);
274561c4b389SFlorian Wechsung       if (patch->local_composition_type == PC_COMPOSITE_ADDITIVE) {
27464bbf5ea8SMatthew G. Knepley         for (i = 0; i < patch->npatch; ++i) {
27474bbf5ea8SMatthew G. Knepley           PetscInt dof;
27484bbf5ea8SMatthew G. Knepley 
27494bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &dof);CHKERRQ(ierr);
27504bbf5ea8SMatthew G. Knepley           if (dof <= 0) continue;
27519d4fc724SLawrence Mitchell           ierr = VecSet(patch->patchRHS, 1.0);CHKERRQ(ierr);
27529d4fc724SLawrence Mitchell           ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchRHS, patch->dof_weights, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR);CHKERRQ(ierr);
27534bbf5ea8SMatthew G. Knepley         }
2754c2e6f3c0SFlorian Wechsung       } else {
2755e047a90bSFlorian Wechsung         /* multiplicative is actually only locally multiplicative and globally additive. need the pou where the mesh decomposition overlaps */
2756c2e6f3c0SFlorian Wechsung         ierr = VecSet(patch->dof_weights, 1.0);CHKERRQ(ierr);
27574bbf5ea8SMatthew G. Knepley       }
2758d132cafaSFlorian Wechsung 
2759d132cafaSFlorian Wechsung       VecDuplicate(patch->dof_weights, &global);
2760d132cafaSFlorian Wechsung       VecSet(global, 0.);
2761d132cafaSFlorian Wechsung 
2762d132cafaSFlorian Wechsung       ierr = VecGetArray(patch->dof_weights, &input);CHKERRQ(ierr);
2763d132cafaSFlorian Wechsung       ierr = VecGetArray(global, &output);CHKERRQ(ierr);
27641bb6d2a8SBarry Smith       ierr = PetscSFReduceBegin(patch->sectionSF, MPIU_SCALAR, input, output, MPI_SUM);CHKERRQ(ierr);
27651bb6d2a8SBarry Smith       ierr = PetscSFReduceEnd(patch->sectionSF, MPIU_SCALAR, input, output, MPI_SUM);CHKERRQ(ierr);
2766d132cafaSFlorian Wechsung       ierr = VecRestoreArray(patch->dof_weights, &input);CHKERRQ(ierr);
2767d132cafaSFlorian Wechsung       ierr = VecRestoreArray(global, &output);CHKERRQ(ierr);
2768d132cafaSFlorian Wechsung 
276905528938SFlorian Wechsung       ierr = VecReciprocal(global);CHKERRQ(ierr);
2770d132cafaSFlorian Wechsung 
2771d132cafaSFlorian Wechsung       ierr = VecGetArray(patch->dof_weights, &output);CHKERRQ(ierr);
2772d132cafaSFlorian Wechsung       ierr = VecGetArray(global, &input);CHKERRQ(ierr);
2773ad227feaSJunchao Zhang       ierr = PetscSFBcastBegin(patch->sectionSF, MPIU_SCALAR, input, output,MPI_REPLACE);CHKERRQ(ierr);
2774ad227feaSJunchao Zhang       ierr = PetscSFBcastEnd(patch->sectionSF, MPIU_SCALAR, input, output,MPI_REPLACE);CHKERRQ(ierr);
2775d132cafaSFlorian Wechsung       ierr = VecRestoreArray(patch->dof_weights, &output);CHKERRQ(ierr);
2776d132cafaSFlorian Wechsung       ierr = VecRestoreArray(global, &input);CHKERRQ(ierr);
2777d132cafaSFlorian Wechsung       ierr = VecDestroy(&global);CHKERRQ(ierr);
27784bbf5ea8SMatthew G. Knepley     }
277961c4b389SFlorian Wechsung     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE && patch->save_operators) {
278096b79ebeSFlorian Wechsung       ierr = PetscMalloc1(patch->npatch, &patch->matWithArtificial);CHKERRQ(ierr);
27814bbf5ea8SMatthew G. Knepley     }
27824bbf5ea8SMatthew G. Knepley   }
2783dadc69c5SMatthew G. Knepley   ierr = (*patch->setupsolver)(pc);CHKERRQ(ierr);
2784dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
27854bbf5ea8SMatthew G. Knepley }
2786dadc69c5SMatthew G. Knepley 
2787dadc69c5SMatthew G. Knepley static PetscErrorCode PCApply_PATCH_Linear(PC pc, PetscInt i, Vec x, Vec y)
2788dadc69c5SMatthew G. Knepley {
2789dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2790c73d2cf6SLawrence Mitchell   KSP            ksp;
27919d4fc724SLawrence Mitchell   Mat            op;
27929d4fc724SLawrence Mitchell   PetscInt       m, n;
2793dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2794dadc69c5SMatthew G. Knepley 
2795dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2796c73d2cf6SLawrence Mitchell   if (patch->denseinverse) {
27979d4fc724SLawrence Mitchell     ierr = (*patch->densesolve)(patch->mat[i], x, y);CHKERRQ(ierr);
2798c73d2cf6SLawrence Mitchell     PetscFunctionReturn(0);
2799c73d2cf6SLawrence Mitchell   }
2800c73d2cf6SLawrence Mitchell   ksp = (KSP) patch->solver[i];
2801dadc69c5SMatthew G. Knepley   if (!patch->save_operators) {
2802dadc69c5SMatthew G. Knepley     Mat mat;
2803dadc69c5SMatthew G. Knepley 
280434d8b122SPatrick Farrell     ierr = PCPatchCreateMatrix_Private(pc, i, &mat, PETSC_FALSE);CHKERRQ(ierr);
2805dadc69c5SMatthew G. Knepley     /* Populate operator here. */
280634d8b122SPatrick Farrell     ierr = PCPatchComputeOperator_Internal(pc, NULL, mat, i, PETSC_FALSE);CHKERRQ(ierr);
2807dadc69c5SMatthew G. Knepley     ierr = KSPSetOperators(ksp, mat, mat);CHKERRQ(ierr);
2808dadc69c5SMatthew G. Knepley     /* Drop reference so the KSPSetOperators below will blow it away. */
2809dadc69c5SMatthew G. Knepley     ierr = MatDestroy(&mat);CHKERRQ(ierr);
2810dadc69c5SMatthew G. Knepley   }
2811dadc69c5SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr);
2812dadc69c5SMatthew G. Knepley   if (!ksp->setfromoptionscalled) {
2813dadc69c5SMatthew G. Knepley     ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
2814dadc69c5SMatthew G. Knepley   }
28159d4fc724SLawrence Mitchell   /* Disgusting trick to reuse work vectors */
28169d4fc724SLawrence Mitchell   ierr = KSPGetOperators(ksp, &op, NULL);CHKERRQ(ierr);
28179d4fc724SLawrence Mitchell   ierr = MatGetLocalSize(op, &m, &n);CHKERRQ(ierr);
28189d4fc724SLawrence Mitchell   x->map->n = m;
28199d4fc724SLawrence Mitchell   y->map->n = n;
28209d4fc724SLawrence Mitchell   x->map->N = m;
28219d4fc724SLawrence Mitchell   y->map->N = n;
2822dadc69c5SMatthew G. Knepley   ierr = KSPSolve(ksp, x, y);CHKERRQ(ierr);
2823ddad275aSPatrick Farrell   ierr = KSPCheckSolve(ksp, pc, y);CHKERRQ(ierr);
2824dadc69c5SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr);
2825dadc69c5SMatthew G. Knepley   if (!patch->save_operators) {
2826dadc69c5SMatthew G. Knepley     PC pc;
2827dadc69c5SMatthew G. Knepley     ierr = KSPSetOperators(ksp, NULL, NULL);CHKERRQ(ierr);
2828dadc69c5SMatthew G. Knepley     ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
2829dadc69c5SMatthew G. Knepley     /* Destroy PC context too, otherwise the factored matrix hangs around. */
2830dadc69c5SMatthew G. Knepley     ierr = PCReset(pc);CHKERRQ(ierr);
28314bbf5ea8SMatthew G. Knepley   }
28324bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
28334bbf5ea8SMatthew G. Knepley }
28344bbf5ea8SMatthew G. Knepley 
28356c9c532dSPatrick Farrell static PetscErrorCode PCUpdateMultiplicative_PATCH_Linear(PC pc, PetscInt i, PetscInt pStart)
28366c9c532dSPatrick Farrell {
28376c9c532dSPatrick Farrell   PC_PATCH      *patch = (PC_PATCH *) pc->data;
28386c9c532dSPatrick Farrell   Mat            multMat;
28399d4fc724SLawrence Mitchell   PetscInt       n, m;
28406c9c532dSPatrick Farrell   PetscErrorCode ierr;
28416c9c532dSPatrick Farrell 
28424d04e9f1SPatrick Farrell   PetscFunctionBegin;
28434d04e9f1SPatrick Farrell 
28446c9c532dSPatrick Farrell   if (patch->save_operators) {
28456c9c532dSPatrick Farrell     multMat = patch->matWithArtificial[i];
28466c9c532dSPatrick Farrell   } else {
28476c9c532dSPatrick Farrell     /*Very inefficient, hopefully we can just assemble the rectangular matrix in the first place.*/
28486c9c532dSPatrick Farrell     Mat      matSquare;
28496c9c532dSPatrick Farrell     PetscInt dof;
28506c9c532dSPatrick Farrell     IS       rowis;
28516c9c532dSPatrick Farrell     ierr = PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE);CHKERRQ(ierr);
28526c9c532dSPatrick Farrell     ierr = PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE);CHKERRQ(ierr);
28536c9c532dSPatrick Farrell     ierr = MatGetSize(matSquare, &dof, NULL);CHKERRQ(ierr);
28546c9c532dSPatrick Farrell     ierr = ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis);CHKERRQ(ierr);
28556c9c532dSPatrick Farrell     ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &multMat);CHKERRQ(ierr);
28566c9c532dSPatrick Farrell     ierr = MatDestroy(&matSquare);CHKERRQ(ierr);
28576c9c532dSPatrick Farrell     ierr = ISDestroy(&rowis);CHKERRQ(ierr);
28586c9c532dSPatrick Farrell   }
28599d4fc724SLawrence Mitchell   /* Disgusting trick to reuse work vectors */
28609d4fc724SLawrence Mitchell   ierr = MatGetLocalSize(multMat, &m, &n);CHKERRQ(ierr);
28619d4fc724SLawrence Mitchell   patch->patchUpdate->map->n = n;
28629d4fc724SLawrence Mitchell   patch->patchRHSWithArtificial->map->n = m;
28639d4fc724SLawrence Mitchell   patch->patchUpdate->map->N = n;
28649d4fc724SLawrence Mitchell   patch->patchRHSWithArtificial->map->N = m;
28659d4fc724SLawrence Mitchell   ierr = MatMult(multMat, patch->patchUpdate, patch->patchRHSWithArtificial);CHKERRQ(ierr);
28669d4fc724SLawrence Mitchell   ierr = VecScale(patch->patchRHSWithArtificial, -1.0);CHKERRQ(ierr);
28679d4fc724SLawrence Mitchell   ierr = PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHSWithArtificial, patch->localRHS, ADD_VALUES, SCATTER_REVERSE, SCATTER_WITHARTIFICIAL);CHKERRQ(ierr);
28686c9c532dSPatrick Farrell   if (!patch->save_operators) {
28696c9c532dSPatrick Farrell     ierr = MatDestroy(&multMat);CHKERRQ(ierr);
28706c9c532dSPatrick Farrell   }
28714d04e9f1SPatrick Farrell   PetscFunctionReturn(0);
28726c9c532dSPatrick Farrell }
28736c9c532dSPatrick Farrell 
28744bbf5ea8SMatthew G. Knepley static PetscErrorCode PCApply_PATCH(PC pc, Vec x, Vec y)
28754bbf5ea8SMatthew G. Knepley {
28764bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch    = (PC_PATCH *) pc->data;
28771202d238SPatrick Farrell   const PetscScalar *globalRHS  = NULL;
28781202d238SPatrick Farrell   PetscScalar       *localRHS   = NULL;
28791202d238SPatrick Farrell   PetscScalar       *globalUpdate  = NULL;
28804bbf5ea8SMatthew G. Knepley   const PetscInt    *bcNodes  = NULL;
28814bbf5ea8SMatthew G. Knepley   PetscInt           nsweep   = patch->symmetrise_sweep ? 2 : 1;
28824bbf5ea8SMatthew G. Knepley   PetscInt           start[2] = {0, 0};
28834bbf5ea8SMatthew G. Knepley   PetscInt           end[2]   = {-1, -1};
28844bbf5ea8SMatthew G. Knepley   const PetscInt     inc[2]   = {1, -1};
28851202d238SPatrick Farrell   const PetscScalar *localUpdate;
28864bbf5ea8SMatthew G. Knepley   const PetscInt    *iterationSet;
28874bbf5ea8SMatthew G. Knepley   PetscInt           pStart, numBcs, n, sweep, bc, j;
28884bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
28894bbf5ea8SMatthew G. Knepley 
28904bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
28914bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr);
28924bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsPushGetViewerOff(PETSC_TRUE);CHKERRQ(ierr);
289392d50984SMatthew G. Knepley   /* start, end, inc have 2 entries to manage a second backward sweep if we symmetrize */
28944bbf5ea8SMatthew G. Knepley   end[0]   = patch->npatch;
28954bbf5ea8SMatthew G. Knepley   start[1] = patch->npatch-1;
28964bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
28974bbf5ea8SMatthew G. Knepley     ierr = ISGetLocalSize(patch->iterationSet, &end[0]);CHKERRQ(ierr);
28984bbf5ea8SMatthew G. Knepley     start[1] = end[0] - 1;
28994bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr);
29004bbf5ea8SMatthew G. Knepley   }
29014bbf5ea8SMatthew G. Knepley   /* Scatter from global space into overlapped local spaces */
29021202d238SPatrick Farrell   ierr = VecGetArrayRead(x, &globalRHS);CHKERRQ(ierr);
29031202d238SPatrick Farrell   ierr = VecGetArray(patch->localRHS, &localRHS);CHKERRQ(ierr);
2904ad227feaSJunchao Zhang   ierr = PetscSFBcastBegin(patch->sectionSF, MPIU_SCALAR, globalRHS, localRHS,MPI_REPLACE);CHKERRQ(ierr);
2905ad227feaSJunchao Zhang   ierr = PetscSFBcastEnd(patch->sectionSF, MPIU_SCALAR, globalRHS, localRHS,MPI_REPLACE);CHKERRQ(ierr);
29061202d238SPatrick Farrell   ierr = VecRestoreArrayRead(x, &globalRHS);CHKERRQ(ierr);
29071202d238SPatrick Farrell   ierr = VecRestoreArray(patch->localRHS, &localRHS);CHKERRQ(ierr);
29084bbf5ea8SMatthew G. Knepley 
29091202d238SPatrick Farrell   ierr = VecSet(patch->localUpdate, 0.0);CHKERRQ(ierr);
29104bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, NULL);CHKERRQ(ierr);
29119d4fc724SLawrence Mitchell   ierr = PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr);
29124bbf5ea8SMatthew G. Knepley   for (sweep = 0; sweep < nsweep; sweep++) {
29134bbf5ea8SMatthew G. Knepley     for (j = start[sweep]; j*inc[sweep] < end[sweep]*inc[sweep]; j += inc[sweep]) {
29144bbf5ea8SMatthew G. Knepley       PetscInt i = patch->user_patches ? iterationSet[j] : j;
29154bbf5ea8SMatthew G. Knepley       PetscInt start, len;
29164bbf5ea8SMatthew G. Knepley 
29174bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &len);CHKERRQ(ierr);
29184bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetOffset(patch->gtolCounts, i+pStart, &start);CHKERRQ(ierr);
29194bbf5ea8SMatthew G. Knepley       /* TODO: Squash out these guys in the setup as well. */
29204bbf5ea8SMatthew G. Knepley       if (len <= 0) continue;
29214bbf5ea8SMatthew G. Knepley       /* TODO: Do we need different scatters for X and Y? */
29229d4fc724SLawrence Mitchell       ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->localRHS, patch->patchRHS, INSERT_VALUES, SCATTER_FORWARD, SCATTER_INTERIOR);CHKERRQ(ierr);
29239d4fc724SLawrence Mitchell       ierr = (*patch->applysolver)(pc, i, patch->patchRHS, patch->patchUpdate);CHKERRQ(ierr);
29249d4fc724SLawrence Mitchell       ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchUpdate, patch->localUpdate, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR);CHKERRQ(ierr);
292561c4b389SFlorian Wechsung       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
29266c9c532dSPatrick Farrell         ierr = (*patch->updatemultiplicative)(pc, i, pStart);CHKERRQ(ierr);
2927c2e6f3c0SFlorian Wechsung       }
29284bbf5ea8SMatthew G. Knepley     }
29294bbf5ea8SMatthew G. Knepley   }
29309d4fc724SLawrence Mitchell   ierr = PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr);
29314bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {ierr = ISRestoreIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr);}
29324bbf5ea8SMatthew G. Knepley   /* XXX: should we do this on the global vector? */
293373ec7555SLawrence Mitchell   if (patch->partition_of_unity) {
29341202d238SPatrick Farrell     ierr = VecPointwiseMult(patch->localUpdate, patch->localUpdate, patch->dof_weights);CHKERRQ(ierr);
29354bbf5ea8SMatthew G. Knepley   }
29361202d238SPatrick Farrell   /* Now patch->localUpdate contains the solution of the patch solves, so we need to combine them all. */
29374bbf5ea8SMatthew G. Knepley   ierr = VecSet(y, 0.0);CHKERRQ(ierr);
29381202d238SPatrick Farrell   ierr = VecGetArray(y, &globalUpdate);CHKERRQ(ierr);
29391202d238SPatrick Farrell   ierr = VecGetArrayRead(patch->localUpdate, &localUpdate);CHKERRQ(ierr);
29401bb6d2a8SBarry Smith   ierr = PetscSFReduceBegin(patch->sectionSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM);CHKERRQ(ierr);
29411bb6d2a8SBarry Smith   ierr = PetscSFReduceEnd(patch->sectionSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM);CHKERRQ(ierr);
29421202d238SPatrick Farrell   ierr = VecRestoreArrayRead(patch->localUpdate, &localUpdate);CHKERRQ(ierr);
29434bbf5ea8SMatthew G. Knepley 
29444bbf5ea8SMatthew G. Knepley   /* Now we need to send the global BC values through */
29451202d238SPatrick Farrell   ierr = VecGetArrayRead(x, &globalRHS);CHKERRQ(ierr);
29464bbf5ea8SMatthew G. Knepley   ierr = ISGetSize(patch->globalBcNodes, &numBcs);CHKERRQ(ierr);
29474bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr);
29484bbf5ea8SMatthew G. Knepley   ierr = VecGetLocalSize(x, &n);CHKERRQ(ierr);
29494bbf5ea8SMatthew G. Knepley   for (bc = 0; bc < numBcs; ++bc) {
29504bbf5ea8SMatthew G. Knepley     const PetscInt idx = bcNodes[bc];
29511202d238SPatrick Farrell     if (idx < n) globalUpdate[idx] = globalRHS[idx];
29524bbf5ea8SMatthew G. Knepley   }
29534bbf5ea8SMatthew G. Knepley 
29544bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr);
29551202d238SPatrick Farrell   ierr = VecRestoreArrayRead(x, &globalRHS);CHKERRQ(ierr);
29561202d238SPatrick Farrell   ierr = VecRestoreArray(y, &globalUpdate);CHKERRQ(ierr);
29574bbf5ea8SMatthew G. Knepley 
29584bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsPopGetViewerOff();CHKERRQ(ierr);
29594bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr);
29604bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
29614bbf5ea8SMatthew G. Knepley }
29624bbf5ea8SMatthew G. Knepley 
2963dadc69c5SMatthew G. Knepley static PetscErrorCode PCReset_PATCH_Linear(PC pc)
2964dadc69c5SMatthew G. Knepley {
2965dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2966dadc69c5SMatthew G. Knepley   PetscInt       i;
2967dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2968dadc69c5SMatthew G. Knepley 
2969dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2970dadc69c5SMatthew G. Knepley   if (patch->solver) {
2971dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = KSPReset((KSP) patch->solver[i]);CHKERRQ(ierr);}
2972dadc69c5SMatthew G. Knepley   }
2973dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
2974dadc69c5SMatthew G. Knepley }
2975dadc69c5SMatthew G. Knepley 
29764bbf5ea8SMatthew G. Knepley static PetscErrorCode PCReset_PATCH(PC pc)
29774bbf5ea8SMatthew G. Knepley {
29784bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
29794bbf5ea8SMatthew G. Knepley   PetscInt       i;
29804bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
29814bbf5ea8SMatthew G. Knepley 
29824bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2983fa84ea4cSLawrence Mitchell 
29841bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&patch->sectionSF);CHKERRQ(ierr);
29854bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->cellCounts);CHKERRQ(ierr);
29865f824522SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->pointCounts);CHKERRQ(ierr);
29874bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->cellNumbering);CHKERRQ(ierr);
29884bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->gtolCounts);CHKERRQ(ierr);
29894bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->gtol);CHKERRQ(ierr);
29904bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->cells);CHKERRQ(ierr);
29915f824522SMatthew G. Knepley   ierr = ISDestroy(&patch->points);CHKERRQ(ierr);
29924bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->dofs);CHKERRQ(ierr);
29935f824522SMatthew G. Knepley   ierr = ISDestroy(&patch->offs);CHKERRQ(ierr);
29945f824522SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->patchSection);CHKERRQ(ierr);
29954bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->ghostBcNodes);CHKERRQ(ierr);
29964bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->globalBcNodes);CHKERRQ(ierr);
299711ac8bb0SFlorian Wechsung   ierr = PetscSectionDestroy(&patch->gtolCountsWithArtificial);CHKERRQ(ierr);
299811ac8bb0SFlorian Wechsung   ierr = ISDestroy(&patch->gtolWithArtificial);CHKERRQ(ierr);
299911ac8bb0SFlorian Wechsung   ierr = ISDestroy(&patch->dofsWithArtificial);CHKERRQ(ierr);
300011ac8bb0SFlorian Wechsung   ierr = ISDestroy(&patch->offsWithArtificial);CHKERRQ(ierr);
30010904074fSPatrick Farrell   ierr = PetscSectionDestroy(&patch->gtolCountsWithAll);CHKERRQ(ierr);
30020904074fSPatrick Farrell   ierr = ISDestroy(&patch->gtolWithAll);CHKERRQ(ierr);
30030904074fSPatrick Farrell   ierr = ISDestroy(&patch->dofsWithAll);CHKERRQ(ierr);
30040904074fSPatrick Farrell   ierr = ISDestroy(&patch->offsWithAll);CHKERRQ(ierr);
3005fa84ea4cSLawrence Mitchell   ierr = VecDestroy(&patch->cellMats);CHKERRQ(ierr);
3006f98464cbSLawrence Mitchell   ierr = VecDestroy(&patch->intFacetMats);CHKERRQ(ierr);
3007fa84ea4cSLawrence Mitchell   ierr = ISDestroy(&patch->allCells);CHKERRQ(ierr);
3008e6fe6948SPatrick Farrell   ierr = ISDestroy(&patch->intFacets);CHKERRQ(ierr);
3009e6fe6948SPatrick Farrell   ierr = ISDestroy(&patch->extFacets);CHKERRQ(ierr);
3010e6fe6948SPatrick Farrell   ierr = ISDestroy(&patch->intFacetsToPatchCell);CHKERRQ(ierr);
3011b65fa9bbSPatrick Farrell   ierr = PetscSectionDestroy(&patch->intFacetCounts);CHKERRQ(ierr);
3012b65fa9bbSPatrick Farrell   ierr = PetscSectionDestroy(&patch->extFacetCounts);CHKERRQ(ierr);
30134bbf5ea8SMatthew G. Knepley 
30145f824522SMatthew G. Knepley   if (patch->dofSection) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscSectionDestroy(&patch->dofSection[i]);CHKERRQ(ierr);}
30154bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->dofSection);CHKERRQ(ierr);
30164bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->bs);CHKERRQ(ierr);
30174bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->nodesPerCell);CHKERRQ(ierr);
30185f824522SMatthew G. Knepley   if (patch->cellNodeMap) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscFree(patch->cellNodeMap[i]);CHKERRQ(ierr);}
30194bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->cellNodeMap);CHKERRQ(ierr);
30204bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->subspaceOffsets);CHKERRQ(ierr);
30214bbf5ea8SMatthew G. Knepley 
3022dadc69c5SMatthew G. Knepley   ierr = (*patch->resetsolver)(pc);CHKERRQ(ierr);
30234bbf5ea8SMatthew G. Knepley 
3024e4c66b91SPatrick Farrell   if (patch->subspaces_to_exclude) {
3025*2f613bf5SBarry Smith     ierr = PetscHSetIDestroy(&patch->subspaces_to_exclude);CHKERRQ(ierr);
3026e4c66b91SPatrick Farrell   }
3027e4c66b91SPatrick Farrell 
30281202d238SPatrick Farrell   ierr = VecDestroy(&patch->localRHS);CHKERRQ(ierr);
30291202d238SPatrick Farrell   ierr = VecDestroy(&patch->localUpdate);CHKERRQ(ierr);
30309d4fc724SLawrence Mitchell   ierr = VecDestroy(&patch->patchRHS);CHKERRQ(ierr);
30319d4fc724SLawrence Mitchell   ierr = VecDestroy(&patch->patchUpdate);CHKERRQ(ierr);
30324bbf5ea8SMatthew G. Knepley   ierr = VecDestroy(&patch->dof_weights);CHKERRQ(ierr);
30334bbf5ea8SMatthew G. Knepley   if (patch->patch_dof_weights) {
30345f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patch_dof_weights[i]);CHKERRQ(ierr);}
30354bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->patch_dof_weights);CHKERRQ(ierr);
30364bbf5ea8SMatthew G. Knepley   }
30374bbf5ea8SMatthew G. Knepley   if (patch->mat) {
30385f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->mat[i]);CHKERRQ(ierr);}
30394bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->mat);CHKERRQ(ierr);
30405f824522SMatthew G. Knepley   }
304111ac8bb0SFlorian Wechsung   if (patch->matWithArtificial) {
304211ac8bb0SFlorian Wechsung     for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->matWithArtificial[i]);CHKERRQ(ierr);}
304311ac8bb0SFlorian Wechsung     ierr = PetscFree(patch->matWithArtificial);CHKERRQ(ierr);
304411ac8bb0SFlorian Wechsung   }
30459d4fc724SLawrence Mitchell   ierr = VecDestroy(&patch->patchRHSWithArtificial);CHKERRQ(ierr);
304696b79ebeSFlorian Wechsung   if (patch->dofMappingWithoutToWithArtificial) {
304796b79ebeSFlorian Wechsung     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->dofMappingWithoutToWithArtificial[i]);CHKERRQ(ierr);}
304896b79ebeSFlorian Wechsung     ierr = PetscFree(patch->dofMappingWithoutToWithArtificial);CHKERRQ(ierr);
304996b79ebeSFlorian Wechsung 
305096b79ebeSFlorian Wechsung   }
30510904074fSPatrick Farrell   if (patch->dofMappingWithoutToWithAll) {
30520904074fSPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->dofMappingWithoutToWithAll[i]);CHKERRQ(ierr);}
30530904074fSPatrick Farrell     ierr = PetscFree(patch->dofMappingWithoutToWithAll);CHKERRQ(ierr);
30540904074fSPatrick Farrell 
30550904074fSPatrick Farrell   }
30564bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr);
30575f824522SMatthew G. Knepley   if (patch->userIS) {
30585f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->userIS[i]);CHKERRQ(ierr);}
30595f824522SMatthew G. Knepley     ierr = PetscFree(patch->userIS);CHKERRQ(ierr);
30605f824522SMatthew G. Knepley   }
3061fe988be2SFlorian Wechsung   ierr = PetscFree(patch->precomputedTensorLocations);CHKERRQ(ierr);
3062f98464cbSLawrence Mitchell   ierr = PetscFree(patch->precomputedIntFacetTensorLocations);CHKERRQ(ierr);
3063f98464cbSLawrence Mitchell 
30640a545947SLisandro Dalcin   patch->bs          = NULL;
30654bbf5ea8SMatthew G. Knepley   patch->cellNodeMap = NULL;
30667974b488SMatthew G. Knepley   patch->nsubspaces  = 0;
30674bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->iterationSet);CHKERRQ(ierr);
30685f824522SMatthew G. Knepley 
30695f824522SMatthew G. Knepley   ierr = PetscViewerDestroy(&patch->viewerSection);CHKERRQ(ierr);
30704bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
30714bbf5ea8SMatthew G. Knepley }
30724bbf5ea8SMatthew G. Knepley 
3073dadc69c5SMatthew G. Knepley static PetscErrorCode PCDestroy_PATCH_Linear(PC pc)
30744bbf5ea8SMatthew G. Knepley {
30754bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
30764bbf5ea8SMatthew G. Knepley   PetscInt       i;
30774bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
30784bbf5ea8SMatthew G. Knepley 
30794bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3080dadc69c5SMatthew G. Knepley   if (patch->solver) {
3081dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = KSPDestroy((KSP *) &patch->solver[i]);CHKERRQ(ierr);}
3082dadc69c5SMatthew G. Knepley     ierr = PetscFree(patch->solver);CHKERRQ(ierr);
30834bbf5ea8SMatthew G. Knepley   }
3084dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
3085dadc69c5SMatthew G. Knepley }
3086dadc69c5SMatthew G. Knepley 
3087dadc69c5SMatthew G. Knepley static PetscErrorCode PCDestroy_PATCH(PC pc)
3088dadc69c5SMatthew G. Knepley {
3089dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
3090dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
3091dadc69c5SMatthew G. Knepley 
3092dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
3093dadc69c5SMatthew G. Knepley   ierr = PCReset_PATCH(pc);CHKERRQ(ierr);
3094dadc69c5SMatthew G. Knepley   ierr = (*patch->destroysolver)(pc);CHKERRQ(ierr);
30954bbf5ea8SMatthew G. Knepley   ierr = PetscFree(pc->data);CHKERRQ(ierr);
30964bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
30974bbf5ea8SMatthew G. Knepley }
30984bbf5ea8SMatthew G. Knepley 
30994bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetFromOptions_PATCH(PetscOptionItems *PetscOptionsObject, PC pc)
31004bbf5ea8SMatthew G. Knepley {
31014bbf5ea8SMatthew G. Knepley   PC_PATCH            *patch = (PC_PATCH *) pc->data;
31024bbf5ea8SMatthew G. Knepley   PCPatchConstructType patchConstructionType = PC_PATCH_STAR;
31035f824522SMatthew G. Knepley   char                 sub_mat_type[PETSC_MAX_PATH_LEN];
310410534d48SPatrick Farrell   char                 option[PETSC_MAX_PATH_LEN];
31055f824522SMatthew G. Knepley   const char          *prefix;
31064bbf5ea8SMatthew G. Knepley   PetscBool            flg, dimflg, codimflg;
31075f824522SMatthew G. Knepley   MPI_Comm             comm;
3108a48c39c8SPatrick Farrell   PetscInt            *ifields, nfields, k;
31094bbf5ea8SMatthew G. Knepley   PetscErrorCode       ierr;
311061c4b389SFlorian Wechsung   PCCompositeType      loctype = PC_COMPOSITE_ADDITIVE;
31114bbf5ea8SMatthew G. Knepley 
31124bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
31135f824522SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) pc, &comm);CHKERRQ(ierr);
31145f824522SMatthew G. Knepley   ierr = PetscObjectGetOptionsPrefix((PetscObject) pc, &prefix);CHKERRQ(ierr);
311510534d48SPatrick Farrell   ierr = PetscOptionsHead(PetscOptionsObject, "Patch solver options");CHKERRQ(ierr);
311610534d48SPatrick Farrell 
31171b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_save_operators", patch->classname);CHKERRQ(ierr);
311810534d48SPatrick Farrell   ierr = PetscOptionsBool(option,  "Store all patch operators for lifetime of object?", "PCPatchSetSaveOperators", patch->save_operators, &patch->save_operators, &flg);CHKERRQ(ierr);
311910534d48SPatrick Farrell 
312089d5b078SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_precompute_element_tensors", patch->classname);CHKERRQ(ierr);
3121fa84ea4cSLawrence Mitchell   ierr = PetscOptionsBool(option,  "Compute each element tensor only once?", "PCPatchSetPrecomputeElementTensors", patch->precomputeElementTensors, &patch->precomputeElementTensors, &flg);CHKERRQ(ierr);
312289d5b078SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_partition_of_unity", patch->classname);CHKERRQ(ierr);
312310534d48SPatrick Farrell   ierr = PetscOptionsBool(option, "Weight contributions by dof multiplicity?", "PCPatchSetPartitionOfUnity", patch->partition_of_unity, &patch->partition_of_unity, &flg);CHKERRQ(ierr);
312410534d48SPatrick Farrell 
31251b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_local_type", patch->classname);CHKERRQ(ierr);
312610534d48SPatrick Farrell   ierr = PetscOptionsEnum(option,"Type of local solver composition (additive or multiplicative)","PCPatchSetLocalComposition",PCCompositeTypes,(PetscEnum)loctype,(PetscEnum*)&loctype,&flg);CHKERRQ(ierr);
312761c4b389SFlorian Wechsung   if (flg) { ierr = PCPatchSetLocalComposition(pc, loctype);CHKERRQ(ierr);}
3128c73d2cf6SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_dense_inverse", patch->classname);CHKERRQ(ierr);
3129c73d2cf6SLawrence Mitchell   ierr = PetscOptionsBool(option, "Compute inverses of patch matrices and apply directly? Ignores KSP/PC settings on patch.", "PCPatchSetDenseInverse", patch->denseinverse, &patch->denseinverse, &flg);CHKERRQ(ierr);
31301b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_dim", patch->classname);CHKERRQ(ierr);
313110534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "What dimension of mesh point to construct patches by? (0 = vertices)", "PCPATCH", patch->dim, &patch->dim, &dimflg);CHKERRQ(ierr);
31321b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_codim", patch->classname);CHKERRQ(ierr);
313310534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "What co-dimension of mesh point to construct patches by? (0 = cells)", "PCPATCH", patch->codim, &patch->codim, &codimflg);CHKERRQ(ierr);
313415b861d2SVaclav Hapla   if (dimflg && codimflg) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Can only set one of dimension or co-dimension");
313510534d48SPatrick Farrell 
31361b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_type", patch->classname);CHKERRQ(ierr);
313710534d48SPatrick Farrell   ierr = PetscOptionsEnum(option, "How should the patches be constructed?", "PCPatchSetConstructType", PCPatchConstructTypes, (PetscEnum) patchConstructionType, (PetscEnum *) &patchConstructionType, &flg);CHKERRQ(ierr);
31384bbf5ea8SMatthew G. Knepley   if (flg) {ierr = PCPatchSetConstructType(pc, patchConstructionType, NULL, NULL);CHKERRQ(ierr);}
313910534d48SPatrick Farrell 
31401b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_vanka_dim", patch->classname);CHKERRQ(ierr);
314110534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "Topological dimension of entities for Vanka to ignore", "PCPATCH", patch->vankadim, &patch->vankadim, &flg);CHKERRQ(ierr);
314210534d48SPatrick Farrell 
31431b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_ignore_dim", patch->classname);CHKERRQ(ierr);
314410534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "Topological dimension of entities for completion to ignore", "PCPATCH", patch->ignoredim, &patch->ignoredim, &flg);CHKERRQ(ierr);
314510534d48SPatrick Farrell 
31461b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_pardecomp_overlap", patch->classname);CHKERRQ(ierr);
3147b525f888SPatrick Farrell   ierr = PetscOptionsInt(option, "What overlap should we use in construct type pardecomp?", "PCPATCH", patch->pardecomp_overlap, &patch->pardecomp_overlap, &flg);CHKERRQ(ierr);
3148b525f888SPatrick Farrell 
31491b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_sub_mat_type", patch->classname);CHKERRQ(ierr);
315010534d48SPatrick Farrell   ierr = PetscOptionsFList(option, "Matrix type for patch solves", "PCPatchSetSubMatType", MatList, NULL, sub_mat_type, PETSC_MAX_PATH_LEN, &flg);CHKERRQ(ierr);
31514bbf5ea8SMatthew G. Knepley   if (flg) {ierr = PCPatchSetSubMatType(pc, sub_mat_type);CHKERRQ(ierr);}
315210534d48SPatrick Farrell 
31531b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_symmetrise_sweep", patch->classname);CHKERRQ(ierr);
315410534d48SPatrick Farrell   ierr = PetscOptionsBool(option, "Go start->end, end->start?", "PCPATCH", patch->symmetrise_sweep, &patch->symmetrise_sweep, &flg);CHKERRQ(ierr);
3155e4c66b91SPatrick Farrell 
3156a48c39c8SPatrick Farrell   /* If the user has set the number of subspaces, use that for the buffer size,
3157a48c39c8SPatrick Farrell      otherwise use a large number */
3158a48c39c8SPatrick Farrell   if (patch->nsubspaces <= 0) {
3159a48c39c8SPatrick Farrell     nfields = 128;
3160a48c39c8SPatrick Farrell   } else {
3161a48c39c8SPatrick Farrell     nfields = patch->nsubspaces;
3162a48c39c8SPatrick Farrell   }
3163a48c39c8SPatrick Farrell   ierr = PetscMalloc1(nfields, &ifields);CHKERRQ(ierr);
31641b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exclude_subspaces", patch->classname);CHKERRQ(ierr);
316510534d48SPatrick Farrell   ierr = PetscOptionsGetIntArray(((PetscObject)pc)->options,((PetscObject)pc)->prefix,option,ifields,&nfields,&flg);CHKERRQ(ierr);
3166e4c66b91SPatrick Farrell   if (flg && (patchConstructionType == PC_PATCH_USER)) SETERRQ(comm, PETSC_ERR_ARG_INCOMP, "We cannot support excluding a subspace with user patches because we do not index patches with a mesh point");
3167e4c66b91SPatrick Farrell   if (flg) {
3168*2f613bf5SBarry Smith     ierr = PetscHSetIClear(patch->subspaces_to_exclude);CHKERRQ(ierr);
316959b66c28SPatrick Farrell     for (k = 0; k < nfields; k++) {
3170*2f613bf5SBarry Smith       ierr = PetscHSetIAdd(patch->subspaces_to_exclude, ifields[k]);CHKERRQ(ierr);
3171e4c66b91SPatrick Farrell     }
3172e4c66b91SPatrick Farrell   }
317359b66c28SPatrick Farrell   ierr = PetscFree(ifields);CHKERRQ(ierr);
31745f824522SMatthew G. Knepley 
3175fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_patches_view", patch->classname);CHKERRQ(ierr);
317610534d48SPatrick Farrell   ierr = PetscOptionsBool(option, "Print out information during patch construction", "PCPATCH", patch->viewPatches, &patch->viewPatches, &flg);CHKERRQ(ierr);
3177fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_cells_view", patch->classname);CHKERRQ(ierr);
317810534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerCells, &patch->formatCells, &patch->viewCells);CHKERRQ(ierr);
3179fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_interior_facets_view", patch->classname);CHKERRQ(ierr);
3180fbb82ab5SLawrence Mitchell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerIntFacets, &patch->formatIntFacets, &patch->viewIntFacets);CHKERRQ(ierr);
3181fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exterior_facets_view", patch->classname);CHKERRQ(ierr);
3182fbb82ab5SLawrence Mitchell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerExtFacets, &patch->formatExtFacets, &patch->viewExtFacets);CHKERRQ(ierr);
3183fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_points_view", patch->classname);CHKERRQ(ierr);
318410534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerPoints, &patch->formatPoints, &patch->viewPoints);CHKERRQ(ierr);
3185fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_section_view", patch->classname);CHKERRQ(ierr);
318610534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerSection, &patch->formatSection, &patch->viewSection);CHKERRQ(ierr);
3187fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_mat_view", patch->classname);CHKERRQ(ierr);
318810534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerMatrix, &patch->formatMatrix, &patch->viewMatrix);CHKERRQ(ierr);
31894bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsTail();CHKERRQ(ierr);
31905f824522SMatthew G. Knepley   patch->optionsSet = PETSC_TRUE;
31914bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
31924bbf5ea8SMatthew G. Knepley }
31934bbf5ea8SMatthew G. Knepley 
31944bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUpOnBlocks_PATCH(PC pc)
31954bbf5ea8SMatthew G. Knepley {
31964bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch = (PC_PATCH*) pc->data;
31974bbf5ea8SMatthew G. Knepley   KSPConvergedReason reason;
31984bbf5ea8SMatthew G. Knepley   PetscInt           i;
31994bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
32004bbf5ea8SMatthew G. Knepley 
32014bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3202a1eac568SLawrence Mitchell   if (!patch->save_operators) {
3203a1eac568SLawrence Mitchell     /* Can't do this here because the sub KSPs don't have an operator attached yet. */
3204a1eac568SLawrence Mitchell     PetscFunctionReturn(0);
3205a1eac568SLawrence Mitchell   }
3206c73d2cf6SLawrence Mitchell   if (patch->denseinverse) {
3207c73d2cf6SLawrence Mitchell     /* No solvers */
3208c73d2cf6SLawrence Mitchell     PetscFunctionReturn(0);
3209c73d2cf6SLawrence Mitchell   }
32104bbf5ea8SMatthew G. Knepley   for (i = 0; i < patch->npatch; ++i) {
3211dadc69c5SMatthew G. Knepley     if (!((KSP) patch->solver[i])->setfromoptionscalled) {
3212dadc69c5SMatthew G. Knepley       ierr = KSPSetFromOptions((KSP) patch->solver[i]);CHKERRQ(ierr);
3213a1eac568SLawrence Mitchell     }
3214dadc69c5SMatthew G. Knepley     ierr = KSPSetUp((KSP) patch->solver[i]);CHKERRQ(ierr);
3215dadc69c5SMatthew G. Knepley     ierr = KSPGetConvergedReason((KSP) patch->solver[i], &reason);CHKERRQ(ierr);
3216c0decd05SBarry Smith     if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR;
32174bbf5ea8SMatthew G. Knepley   }
32184bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
32194bbf5ea8SMatthew G. Knepley }
32204bbf5ea8SMatthew G. Knepley 
32214bbf5ea8SMatthew G. Knepley static PetscErrorCode PCView_PATCH(PC pc, PetscViewer viewer)
32224bbf5ea8SMatthew G. Knepley {
32234bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
32244bbf5ea8SMatthew G. Knepley   PetscViewer    sviewer;
32254bbf5ea8SMatthew G. Knepley   PetscBool      isascii;
32264bbf5ea8SMatthew G. Knepley   PetscMPIInt    rank;
32274bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
32284bbf5ea8SMatthew G. Knepley 
32294bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
32304bbf5ea8SMatthew G. Knepley   /* TODO Redo tabbing with set tbas in new style */
32314bbf5ea8SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii);CHKERRQ(ierr);
32324bbf5ea8SMatthew G. Knepley   if (!isascii) PetscFunctionReturn(0);
3233ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) pc), &rank);CHKERRMPI(ierr);
32344bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
32354bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "Subspace Correction preconditioner with %d patches\n", patch->npatch);CHKERRQ(ierr);
323661c4b389SFlorian Wechsung   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
3237c2e6f3c0SFlorian Wechsung     ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: multiplicative\n");CHKERRQ(ierr);
3238e047a90bSFlorian Wechsung   } else {
323973ec7555SLawrence Mitchell     ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: additive\n");CHKERRQ(ierr);
3240c2e6f3c0SFlorian Wechsung   }
32414bbf5ea8SMatthew G. Knepley   if (patch->partition_of_unity) {ierr = PetscViewerASCIIPrintf(viewer, "Weighting by partition of unity\n");CHKERRQ(ierr);}
32424bbf5ea8SMatthew G. Knepley   else                           {ierr = PetscViewerASCIIPrintf(viewer, "Not weighting by partition of unity\n");CHKERRQ(ierr);}
32434bbf5ea8SMatthew G. Knepley   if (patch->symmetrise_sweep) {ierr = PetscViewerASCIIPrintf(viewer, "Symmetrising sweep (start->end, then end->start)\n");CHKERRQ(ierr);}
32444bbf5ea8SMatthew G. Knepley   else                         {ierr = PetscViewerASCIIPrintf(viewer, "Not symmetrising sweep\n");CHKERRQ(ierr);}
3245fa84ea4cSLawrence Mitchell   if (!patch->precomputeElementTensors) {ierr = PetscViewerASCIIPrintf(viewer, "Not precomputing element tensors (overlapping cells rebuilt in every patch assembly)\n");CHKERRQ(ierr);}
3246fa84ea4cSLawrence Mitchell   else                            {ierr = PetscViewerASCIIPrintf(viewer, "Precomputing element tensors (each cell assembled only once)\n");CHKERRQ(ierr);}
32474bbf5ea8SMatthew G. Knepley   if (!patch->save_operators) {ierr = PetscViewerASCIIPrintf(viewer, "Not saving patch operators (rebuilt every PCApply)\n");CHKERRQ(ierr);}
32484bbf5ea8SMatthew G. Knepley   else                        {ierr = PetscViewerASCIIPrintf(viewer, "Saving patch operators (rebuilt every PCSetUp)\n");CHKERRQ(ierr);}
32494bbf5ea8SMatthew G. Knepley   if (patch->patchconstructop == PCPatchConstruct_Star)       {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: star\n");CHKERRQ(ierr);}
32504bbf5ea8SMatthew G. Knepley   else if (patch->patchconstructop == PCPatchConstruct_Vanka) {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: Vanka\n");CHKERRQ(ierr);}
32514bbf5ea8SMatthew G. Knepley   else if (patch->patchconstructop == PCPatchConstruct_User)  {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: user-specified\n");CHKERRQ(ierr);}
32524bbf5ea8SMatthew G. Knepley   else                                                        {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: unknown\n");CHKERRQ(ierr);}
32535d30859aSPatrick Farrell 
3254c73d2cf6SLawrence Mitchell   if (patch->denseinverse) {
32559d4fc724SLawrence Mitchell     ierr = PetscViewerASCIIPrintf(viewer, "Explicitly forming dense inverse and applying patch solver via MatMult.\n");CHKERRQ(ierr);
3256c73d2cf6SLawrence Mitchell   } else {
32575d30859aSPatrick Farrell     if (patch->isNonlinear) {
32585d30859aSPatrick Farrell       ierr = PetscViewerASCIIPrintf(viewer, "SNES on patches (all same):\n");CHKERRQ(ierr);
32595d30859aSPatrick Farrell     } else {
32605d30859aSPatrick Farrell       ierr = PetscViewerASCIIPrintf(viewer, "KSP on patches (all same):\n");CHKERRQ(ierr);
32615d30859aSPatrick Farrell     }
3262dadc69c5SMatthew G. Knepley     if (patch->solver) {
32634bbf5ea8SMatthew G. Knepley       ierr = PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr);
3264dd400576SPatrick Sanan       if (rank == 0) {
32654bbf5ea8SMatthew G. Knepley         ierr = PetscViewerASCIIPushTab(sviewer);CHKERRQ(ierr);
3266dadc69c5SMatthew G. Knepley         ierr = PetscObjectView(patch->solver[0], sviewer);CHKERRQ(ierr);
32674bbf5ea8SMatthew G. Knepley         ierr = PetscViewerASCIIPopTab(sviewer);CHKERRQ(ierr);
32684bbf5ea8SMatthew G. Knepley       }
32694bbf5ea8SMatthew G. Knepley       ierr = PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr);
32704bbf5ea8SMatthew G. Knepley     } else {
32714bbf5ea8SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
3272dadc69c5SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "Solver not yet set.\n");CHKERRQ(ierr);
32734bbf5ea8SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
32744bbf5ea8SMatthew G. Knepley     }
3275c73d2cf6SLawrence Mitchell   }
32764bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
32774bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
32784bbf5ea8SMatthew G. Knepley }
32794bbf5ea8SMatthew G. Knepley 
3280e5893cccSMatthew G. Knepley /*MC
328198ed095eSMatthew G. Knepley   PCPATCH - A PC object that encapsulates flexible definition of blocks for overlapping and non-overlapping
328298ed095eSMatthew G. Knepley             small block additive preconditioners. Block definition is based on topology from
3283e5893cccSMatthew G. Knepley             a DM and equation numbering from a PetscSection.
3284e5893cccSMatthew G. Knepley 
3285e5893cccSMatthew G. Knepley   Options Database Keys:
3286e5893cccSMatthew G. Knepley + -pc_patch_cells_view   - Views the process local cell numbers for each patch
3287e5893cccSMatthew G. Knepley . -pc_patch_points_view  - Views the process local mesh point numbers for each patch
3288e5893cccSMatthew G. Knepley . -pc_patch_g2l_view     - Views the map between global dofs and patch local dofs for each patch
3289e5893cccSMatthew G. Knepley . -pc_patch_patches_view - Views the global dofs associated with each patch and its boundary
3290e5893cccSMatthew G. Knepley - -pc_patch_sub_mat_view - Views the matrix associated with each patch
3291e5893cccSMatthew G. Knepley 
3292e5893cccSMatthew G. Knepley   Level: intermediate
3293e5893cccSMatthew G. Knepley 
3294e5893cccSMatthew G. Knepley .seealso: PCType, PCCreate(), PCSetType()
3295e5893cccSMatthew G. Knepley M*/
3296642283e9SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PCCreate_Patch(PC pc)
32974bbf5ea8SMatthew G. Knepley {
32984bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch;
32994bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
33004bbf5ea8SMatthew G. Knepley 
33014bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
33024bbf5ea8SMatthew G. Knepley   ierr = PetscNewLog(pc, &patch);CHKERRQ(ierr);
33034bbf5ea8SMatthew G. Knepley 
3304e4c66b91SPatrick Farrell   if (patch->subspaces_to_exclude) {
3305*2f613bf5SBarry Smith     ierr = PetscHSetIDestroy(&patch->subspaces_to_exclude);CHKERRQ(ierr);
3306e4c66b91SPatrick Farrell   }
3307*2f613bf5SBarry Smith   ierr = PetscHSetICreate(&patch->subspaces_to_exclude);CHKERRQ(ierr);
3308e4c66b91SPatrick Farrell 
330910534d48SPatrick Farrell   patch->classname = "pc";
3310debbdec3SPatrick Farrell   patch->isNonlinear = PETSC_FALSE;
331110534d48SPatrick Farrell 
33124bbf5ea8SMatthew G. Knepley   /* Set some defaults */
33135f824522SMatthew G. Knepley   patch->combined           = PETSC_FALSE;
33144bbf5ea8SMatthew G. Knepley   patch->save_operators     = PETSC_TRUE;
331561c4b389SFlorian Wechsung   patch->local_composition_type = PC_COMPOSITE_ADDITIVE;
3316fa84ea4cSLawrence Mitchell   patch->precomputeElementTensors = PETSC_FALSE;
33174bbf5ea8SMatthew G. Knepley   patch->partition_of_unity = PETSC_FALSE;
33184bbf5ea8SMatthew G. Knepley   patch->codim              = -1;
33194bbf5ea8SMatthew G. Knepley   patch->dim                = -1;
33204bbf5ea8SMatthew G. Knepley   patch->vankadim           = -1;
33215f824522SMatthew G. Knepley   patch->ignoredim          = -1;
3322b525f888SPatrick Farrell   patch->pardecomp_overlap  = 0;
33234bbf5ea8SMatthew G. Knepley   patch->patchconstructop   = PCPatchConstruct_Star;
33244bbf5ea8SMatthew G. Knepley   patch->symmetrise_sweep   = PETSC_FALSE;
33255f824522SMatthew G. Knepley   patch->npatch             = 0;
33264bbf5ea8SMatthew G. Knepley   patch->userIS             = NULL;
33275f824522SMatthew G. Knepley   patch->optionsSet         = PETSC_FALSE;
33284bbf5ea8SMatthew G. Knepley   patch->iterationSet       = NULL;
33294bbf5ea8SMatthew G. Knepley   patch->user_patches       = PETSC_FALSE;
33305f824522SMatthew G. Knepley   ierr = PetscStrallocpy(MATDENSE, (char **) &patch->sub_mat_type);CHKERRQ(ierr);
33315f824522SMatthew G. Knepley   patch->viewPatches        = PETSC_FALSE;
33325f824522SMatthew G. Knepley   patch->viewCells          = PETSC_FALSE;
33335f824522SMatthew G. Knepley   patch->viewPoints         = PETSC_FALSE;
33345f824522SMatthew G. Knepley   patch->viewSection        = PETSC_FALSE;
33355f824522SMatthew G. Knepley   patch->viewMatrix         = PETSC_FALSE;
33369d4fc724SLawrence Mitchell   patch->densesolve         = NULL;
3337dadc69c5SMatthew G. Knepley   patch->setupsolver        = PCSetUp_PATCH_Linear;
3338dadc69c5SMatthew G. Knepley   patch->applysolver        = PCApply_PATCH_Linear;
3339dadc69c5SMatthew G. Knepley   patch->resetsolver        = PCReset_PATCH_Linear;
3340dadc69c5SMatthew G. Knepley   patch->destroysolver      = PCDestroy_PATCH_Linear;
33416c9c532dSPatrick Farrell   patch->updatemultiplicative = PCUpdateMultiplicative_PATCH_Linear;
334247aca4a6SPatrick Farrell   patch->dofMappingWithoutToWithArtificial = NULL;
334347aca4a6SPatrick Farrell   patch->dofMappingWithoutToWithAll = NULL;
33444bbf5ea8SMatthew G. Knepley 
33454bbf5ea8SMatthew G. Knepley   pc->data                 = (void *) patch;
33464bbf5ea8SMatthew G. Knepley   pc->ops->apply           = PCApply_PATCH;
33470a545947SLisandro Dalcin   pc->ops->applytranspose  = NULL; /* PCApplyTranspose_PATCH; */
33484bbf5ea8SMatthew G. Knepley   pc->ops->setup           = PCSetUp_PATCH;
33494bbf5ea8SMatthew G. Knepley   pc->ops->reset           = PCReset_PATCH;
33504bbf5ea8SMatthew G. Knepley   pc->ops->destroy         = PCDestroy_PATCH;
33514bbf5ea8SMatthew G. Knepley   pc->ops->setfromoptions  = PCSetFromOptions_PATCH;
33524bbf5ea8SMatthew G. Knepley   pc->ops->setuponblocks   = PCSetUpOnBlocks_PATCH;
33534bbf5ea8SMatthew G. Knepley   pc->ops->view            = PCView_PATCH;
33540a545947SLisandro Dalcin   pc->ops->applyrichardson = NULL;
33554bbf5ea8SMatthew G. Knepley 
33564bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
33574bbf5ea8SMatthew G. Knepley }
3358