xref: /petsc/src/ksp/pc/impls/patch/pcpatch.c (revision b6bb21d10fb6c1fc5986423d6692dc8582775330)
14bbf5ea8SMatthew G. Knepley #include <petsc/private/pcpatchimpl.h>     /*I "petscpc.h" I*/
254ab768cSLawrence Mitchell #include <petsc/private/kspimpl.h>         /* For ksp->setfromoptionscalled */
35f824522SMatthew G. Knepley #include <petsc/private/dmpleximpl.h> /* For DMPlexComputeJacobian_Patch_Internal() */
44bbf5ea8SMatthew G. Knepley #include <petscsf.h>
54bbf5ea8SMatthew G. Knepley #include <petscbt.h>
65f824522SMatthew G. Knepley #include <petscds.h>
74bbf5ea8SMatthew G. Knepley 
84bbf5ea8SMatthew G. Knepley PetscLogEvent PC_Patch_CreatePatches, PC_Patch_ComputeOp, PC_Patch_Solve, PC_Patch_Scatter, PC_Patch_Apply, PC_Patch_Prealloc;
94bbf5ea8SMatthew G. Knepley 
105f824522SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode ObjectView(PetscObject obj, PetscViewer viewer, PetscViewerFormat format)
115f824522SMatthew G. Knepley {
125f824522SMatthew G. Knepley   PetscErrorCode ierr;
135f824522SMatthew G. Knepley 
145f824522SMatthew G. Knepley   ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr);
155f824522SMatthew G. Knepley   ierr = PetscObjectView(obj, viewer);CHKERRQ(ierr);
165f824522SMatthew G. Knepley   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
177974b488SMatthew G. Knepley   return(0);
185f824522SMatthew G. Knepley }
195f824522SMatthew G. Knepley 
201b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_Star(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
214bbf5ea8SMatthew G. Knepley {
224bbf5ea8SMatthew G. Knepley   PetscInt       starSize;
234bbf5ea8SMatthew G. Knepley   PetscInt      *star = NULL, si;
244bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
254bbf5ea8SMatthew G. Knepley 
264bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
271b68eb51SMatthew G. Knepley   PetscHSetIClear(ht);
284bbf5ea8SMatthew G. Knepley   /* To start with, add the point we care about */
291b68eb51SMatthew G. Knepley   ierr = PetscHSetIAdd(ht, point);CHKERRQ(ierr);
304bbf5ea8SMatthew G. Knepley   /* Loop over all the points that this point connects to */
314bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
321b68eb51SMatthew G. Knepley   for (si = 0; si < starSize*2; si += 2) {ierr = PetscHSetIAdd(ht, star[si]);CHKERRQ(ierr);}
334bbf5ea8SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
344bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
354bbf5ea8SMatthew G. Knepley }
364bbf5ea8SMatthew G. Knepley 
371b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_Vanka(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
384bbf5ea8SMatthew G. Knepley {
394bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) vpatch;
404bbf5ea8SMatthew G. Knepley   PetscInt       starSize;
414bbf5ea8SMatthew G. Knepley   PetscInt      *star = NULL;
424bbf5ea8SMatthew G. Knepley   PetscBool      shouldIgnore = PETSC_FALSE;
434bbf5ea8SMatthew G. Knepley   PetscInt       cStart, cEnd, iStart, iEnd, si;
444bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
454bbf5ea8SMatthew G. Knepley 
464bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
471b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(ht);CHKERRQ(ierr);
484bbf5ea8SMatthew G. Knepley   /* To start with, add the point we care about */
491b68eb51SMatthew G. Knepley   ierr = PetscHSetIAdd(ht, point);CHKERRQ(ierr);
504bbf5ea8SMatthew G. Knepley   /* Should we ignore any points of a certain dimension? */
514bbf5ea8SMatthew G. Knepley   if (patch->vankadim >= 0) {
524bbf5ea8SMatthew G. Knepley     shouldIgnore = PETSC_TRUE;
534bbf5ea8SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, patch->vankadim, &iStart, &iEnd);CHKERRQ(ierr);
544bbf5ea8SMatthew G. Knepley   }
554bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
564bbf5ea8SMatthew G. Knepley   /* Loop over all the cells that this point connects to */
574bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
585f824522SMatthew G. Knepley   for (si = 0; si < starSize*2; si += 2) {
594bbf5ea8SMatthew G. Knepley     const PetscInt cell = star[si];
604bbf5ea8SMatthew G. Knepley     PetscInt       closureSize;
614bbf5ea8SMatthew G. Knepley     PetscInt      *closure = NULL, ci;
624bbf5ea8SMatthew G. Knepley 
634bbf5ea8SMatthew G. Knepley     if (cell < cStart || cell >= cEnd) continue;
644bbf5ea8SMatthew G. Knepley     /* now loop over all entities in the closure of that cell */
654bbf5ea8SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
665f824522SMatthew G. Knepley     for (ci = 0; ci < closureSize*2; ci += 2) {
674bbf5ea8SMatthew G. Knepley       const PetscInt newpoint = closure[ci];
684bbf5ea8SMatthew G. Knepley 
694bbf5ea8SMatthew G. Knepley       /* We've been told to ignore entities of this type.*/
704bbf5ea8SMatthew G. Knepley       if (shouldIgnore && newpoint >= iStart && newpoint < iEnd) continue;
711b68eb51SMatthew G. Knepley       ierr = PetscHSetIAdd(ht, newpoint);CHKERRQ(ierr);
724bbf5ea8SMatthew G. Knepley     }
734bbf5ea8SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
744bbf5ea8SMatthew G. Knepley   }
754bbf5ea8SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
764bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
774bbf5ea8SMatthew G. Knepley }
784bbf5ea8SMatthew G. Knepley 
79e5b9877fSPatrick Farrell static PetscErrorCode PCPatchConstruct_Pardecomp(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
800a390943SPatrick Farrell {
81b525f888SPatrick Farrell   PC_PATCH       *patch = (PC_PATCH *) vpatch;
820a390943SPatrick Farrell   DMLabel         ghost = NULL;
830a390943SPatrick Farrell   const PetscInt *leaves;
840a390943SPatrick Farrell   PetscInt        nleaves, pStart, pEnd, loc;
850a390943SPatrick Farrell   PetscBool       isFiredrake;
860a390943SPatrick Farrell   PetscBool       flg;
87b525f888SPatrick Farrell   PetscInt        starSize;
88b525f888SPatrick Farrell   PetscInt       *star = NULL;
8925fd193aSPatrick Farrell   PetscInt        opoint, overlapi;
900a390943SPatrick Farrell   PetscErrorCode  ierr;
910a390943SPatrick Farrell 
920a390943SPatrick Farrell   PetscFunctionBegin;
930a390943SPatrick Farrell   PetscHSetIClear(ht);
940a390943SPatrick Farrell 
95*b6bb21d1SLawrence Mitchell   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
960a390943SPatrick Farrell 
970a390943SPatrick Farrell   ierr = DMHasLabel(dm, "pyop2_ghost", &isFiredrake);CHKERRQ(ierr);
980a390943SPatrick Farrell   if (isFiredrake) {
990a390943SPatrick Farrell     ierr = DMGetLabel(dm, "pyop2_ghost", &ghost);CHKERRQ(ierr);
1000a390943SPatrick Farrell     ierr = DMLabelCreateIndex(ghost, pStart, pEnd);CHKERRQ(ierr);
1010a390943SPatrick Farrell   } else {
1020a390943SPatrick Farrell     PetscSF sf;
1030a390943SPatrick Farrell     ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
1040a390943SPatrick Farrell     ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
1050a390943SPatrick Farrell     nleaves = PetscMax(nleaves, 0);
1060a390943SPatrick Farrell   }
1070a390943SPatrick Farrell 
10825fd193aSPatrick Farrell   for (opoint = pStart; opoint < pEnd; ++opoint) {
109bfabdd78SPatrick Farrell     if (ghost) {ierr = DMLabelHasPoint(ghost, opoint, &flg);CHKERRQ(ierr);}
110bfabdd78SPatrick Farrell     else       {ierr = PetscFindInt(opoint, nleaves, leaves, &loc);CHKERRQ(ierr); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;}
1110a390943SPatrick Farrell     /* Not an owned entity, don't make a cell patch. */
1120a390943SPatrick Farrell     if (flg) continue;
113bfabdd78SPatrick Farrell     ierr = PetscHSetIAdd(ht, opoint);CHKERRQ(ierr);
1140a390943SPatrick Farrell   }
1150a390943SPatrick Farrell 
116b525f888SPatrick Farrell   /* Now build the overlap for the patch */
11725fd193aSPatrick Farrell   for (overlapi = 0; overlapi < patch->pardecomp_overlap; ++overlapi) {
118b525f888SPatrick Farrell     PetscInt index = 0;
119b525f888SPatrick Farrell     PetscInt *htpoints = NULL;
120b525f888SPatrick Farrell     PetscInt htsize;
12125fd193aSPatrick Farrell     PetscInt i;
122b525f888SPatrick Farrell 
123b525f888SPatrick Farrell     ierr = PetscHSetIGetSize(ht, &htsize);CHKERRQ(ierr);
124b525f888SPatrick Farrell     ierr = PetscMalloc1(htsize, &htpoints);CHKERRQ(ierr);
125b525f888SPatrick Farrell     ierr = PetscHSetIGetElems(ht, &index, htpoints);CHKERRQ(ierr);
126b525f888SPatrick Farrell 
12725fd193aSPatrick Farrell     for (i = 0; i < htsize; ++i) {
12825fd193aSPatrick Farrell       PetscInt hpoint = htpoints[i];
12925fd193aSPatrick Farrell       PetscInt si;
130b525f888SPatrick Farrell 
13125fd193aSPatrick Farrell       ierr = DMPlexGetTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
13225fd193aSPatrick Farrell       for (si = 0; si < starSize*2; si += 2) {
133b525f888SPatrick Farrell         const PetscInt starp = star[si];
134b525f888SPatrick Farrell         PetscInt       closureSize;
135b525f888SPatrick Farrell         PetscInt      *closure = NULL, ci;
136b525f888SPatrick Farrell 
137b525f888SPatrick Farrell         /* now loop over all entities in the closure of starp */
138b525f888SPatrick Farrell         ierr = DMPlexGetTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
13925fd193aSPatrick Farrell         for (ci = 0; ci < closureSize*2; ci += 2) {
140b525f888SPatrick Farrell           const PetscInt closstarp = closure[ci];
141b525f888SPatrick Farrell           ierr = PetscHSetIAdd(ht, closstarp);CHKERRQ(ierr);
142b525f888SPatrick Farrell         }
143b525f888SPatrick Farrell         ierr = DMPlexRestoreTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
144b525f888SPatrick Farrell       }
14525fd193aSPatrick Farrell       ierr = DMPlexRestoreTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
146b525f888SPatrick Farrell     }
147b525f888SPatrick Farrell     ierr = PetscFree(htpoints);CHKERRQ(ierr);
148b525f888SPatrick Farrell   }
149b525f888SPatrick Farrell 
1500a390943SPatrick Farrell   PetscFunctionReturn(0);
1510a390943SPatrick Farrell }
1520a390943SPatrick Farrell 
1534bbf5ea8SMatthew G. Knepley /* The user's already set the patches in patch->userIS. Build the hash tables */
1541b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_User(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
1554bbf5ea8SMatthew G. Knepley {
1564bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch   = (PC_PATCH *) vpatch;
1574bbf5ea8SMatthew G. Knepley   IS              patchis = patch->userIS[point];
1584bbf5ea8SMatthew G. Knepley   PetscInt        n;
1594bbf5ea8SMatthew G. Knepley   const PetscInt *patchdata;
1604bbf5ea8SMatthew G. Knepley   PetscInt        pStart, pEnd, i;
1614bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
1624bbf5ea8SMatthew G. Knepley 
1634bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1641b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(ht);CHKERRQ(ierr);
1651b68eb51SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
1664bbf5ea8SMatthew G. Knepley   ierr = ISGetLocalSize(patchis, &n);CHKERRQ(ierr);
1674bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patchis, &patchdata);CHKERRQ(ierr);
1684bbf5ea8SMatthew G. Knepley   for (i = 0; i < n; ++i) {
1694bbf5ea8SMatthew G. Knepley     const PetscInt ownedpoint = patchdata[i];
1704bbf5ea8SMatthew G. Knepley 
1714bbf5ea8SMatthew G. Knepley     if (ownedpoint < pStart || ownedpoint >= pEnd) {
1724bbf5ea8SMatthew G. Knepley       SETERRQ3(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D was not in [%D, %D)", ownedpoint, pStart, pEnd);
1734bbf5ea8SMatthew G. Knepley     }
1741b68eb51SMatthew G. Knepley     ierr = PetscHSetIAdd(ht, ownedpoint);CHKERRQ(ierr);
1754bbf5ea8SMatthew G. Knepley   }
1764bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patchis, &patchdata);CHKERRQ(ierr);
1774bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
1784bbf5ea8SMatthew G. Knepley }
1794bbf5ea8SMatthew G. Knepley 
1804bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateDefaultSF_Private(PC pc, PetscInt n, const PetscSF *sf, const PetscInt *bs)
1814bbf5ea8SMatthew G. Knepley {
1824bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
1834bbf5ea8SMatthew G. Knepley   PetscInt       i;
1844bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
1854bbf5ea8SMatthew G. Knepley 
1864bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1874bbf5ea8SMatthew G. Knepley   if (n == 1 && bs[0] == 1) {
1884bbf5ea8SMatthew G. Knepley     patch->defaultSF = sf[0];
1894bbf5ea8SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) patch->defaultSF);CHKERRQ(ierr);
1904bbf5ea8SMatthew G. Knepley   } else {
1914bbf5ea8SMatthew G. Knepley     PetscInt     allRoots = 0, allLeaves = 0;
1924bbf5ea8SMatthew G. Knepley     PetscInt     leafOffset = 0;
1934bbf5ea8SMatthew G. Knepley     PetscInt    *ilocal = NULL;
1944bbf5ea8SMatthew G. Knepley     PetscSFNode *iremote = NULL;
1954bbf5ea8SMatthew G. Knepley     PetscInt    *remoteOffsets = NULL;
1964bbf5ea8SMatthew G. Knepley     PetscInt     index = 0;
1971b68eb51SMatthew G. Knepley     PetscHMapI   rankToIndex;
1984bbf5ea8SMatthew G. Knepley     PetscInt     numRanks = 0;
1994bbf5ea8SMatthew G. Knepley     PetscSFNode *remote = NULL;
2004bbf5ea8SMatthew G. Knepley     PetscSF      rankSF;
2014bbf5ea8SMatthew G. Knepley     PetscInt    *ranks = NULL;
2024bbf5ea8SMatthew G. Knepley     PetscInt    *offsets = NULL;
2034bbf5ea8SMatthew G. Knepley     MPI_Datatype contig;
2041b68eb51SMatthew G. Knepley     PetscHSetI   ranksUniq;
2054bbf5ea8SMatthew G. Knepley 
2064bbf5ea8SMatthew G. Knepley     /* First figure out how many dofs there are in the concatenated numbering.
2074bbf5ea8SMatthew G. Knepley      * allRoots: number of owned global dofs;
2084bbf5ea8SMatthew G. Knepley      * allLeaves: number of visible dofs (global + ghosted).
2094bbf5ea8SMatthew G. Knepley      */
2104bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2114bbf5ea8SMatthew G. Knepley       PetscInt nroots, nleaves;
2124bbf5ea8SMatthew G. Knepley 
2134bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetGraph(sf[i], &nroots, &nleaves, NULL, NULL);CHKERRQ(ierr);
2144bbf5ea8SMatthew G. Knepley       allRoots  += nroots * bs[i];
2154bbf5ea8SMatthew G. Knepley       allLeaves += nleaves * bs[i];
2164bbf5ea8SMatthew G. Knepley     }
2174bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(allLeaves, &ilocal);CHKERRQ(ierr);
2184bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(allLeaves, &iremote);CHKERRQ(ierr);
2194bbf5ea8SMatthew G. Knepley     /* Now build an SF that just contains process connectivity. */
2201b68eb51SMatthew G. Knepley     ierr = PetscHSetICreate(&ranksUniq);CHKERRQ(ierr);
2214bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2224bbf5ea8SMatthew G. Knepley       const PetscMPIInt *ranks = NULL;
2234bbf5ea8SMatthew G. Knepley       PetscInt           nranks, j;
2244bbf5ea8SMatthew G. Knepley 
2254bbf5ea8SMatthew G. Knepley       ierr = PetscSFSetUp(sf[i]);CHKERRQ(ierr);
2264bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetRanks(sf[i], &nranks, &ranks, NULL, NULL, NULL);CHKERRQ(ierr);
2274bbf5ea8SMatthew G. Knepley       /* These are all the ranks who communicate with me. */
2284bbf5ea8SMatthew G. Knepley       for (j = 0; j < nranks; ++j) {
2291b68eb51SMatthew G. Knepley         ierr = PetscHSetIAdd(ranksUniq, (PetscInt) ranks[j]);CHKERRQ(ierr);
2304bbf5ea8SMatthew G. Knepley       }
2314bbf5ea8SMatthew G. Knepley     }
2321b68eb51SMatthew G. Knepley     ierr = PetscHSetIGetSize(ranksUniq, &numRanks);CHKERRQ(ierr);
2334bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(numRanks, &remote);CHKERRQ(ierr);
2344bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(numRanks, &ranks);CHKERRQ(ierr);
2351b68eb51SMatthew G. Knepley     ierr = PetscHSetIGetElems(ranksUniq, &index, ranks);CHKERRQ(ierr);
2364bbf5ea8SMatthew G. Knepley 
2371b68eb51SMatthew G. Knepley     ierr = PetscHMapICreate(&rankToIndex);CHKERRQ(ierr);
2384bbf5ea8SMatthew G. Knepley     for (i = 0; i < numRanks; ++i) {
2394bbf5ea8SMatthew G. Knepley       remote[i].rank  = ranks[i];
2404bbf5ea8SMatthew G. Knepley       remote[i].index = 0;
2411b68eb51SMatthew G. Knepley       ierr = PetscHMapISet(rankToIndex, ranks[i], i);CHKERRQ(ierr);
2424bbf5ea8SMatthew G. Knepley     }
2434bbf5ea8SMatthew G. Knepley     ierr = PetscFree(ranks);CHKERRQ(ierr);
2441b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&ranksUniq);CHKERRQ(ierr);
2454bbf5ea8SMatthew G. Knepley     ierr = PetscSFCreate(PetscObjectComm((PetscObject) pc), &rankSF);CHKERRQ(ierr);
2464bbf5ea8SMatthew G. Knepley     ierr = PetscSFSetGraph(rankSF, 1, numRanks, NULL, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
2474bbf5ea8SMatthew G. Knepley     ierr = PetscSFSetUp(rankSF);CHKERRQ(ierr);
2484bbf5ea8SMatthew G. Knepley     /* OK, use it to communicate the root offset on the remote
2494bbf5ea8SMatthew G. Knepley      * processes for each subspace. */
2504bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(n, &offsets);CHKERRQ(ierr);
2514bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(n*numRanks, &remoteOffsets);CHKERRQ(ierr);
2524bbf5ea8SMatthew G. Knepley 
2534bbf5ea8SMatthew G. Knepley     offsets[0] = 0;
2544bbf5ea8SMatthew G. Knepley     for (i = 1; i < n; ++i) {
2554bbf5ea8SMatthew G. Knepley       PetscInt nroots;
2564bbf5ea8SMatthew G. Knepley 
2574bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetGraph(sf[i-1], &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
2584bbf5ea8SMatthew G. Knepley       offsets[i] = offsets[i-1] + nroots*bs[i-1];
2594bbf5ea8SMatthew G. Knepley     }
2604bbf5ea8SMatthew G. Knepley     /* Offsets are the offsets on the current process of the
2614bbf5ea8SMatthew G. Knepley      * global dof numbering for the subspaces. */
2624bbf5ea8SMatthew G. Knepley     ierr = MPI_Type_contiguous(n, MPIU_INT, &contig);CHKERRQ(ierr);
2634bbf5ea8SMatthew G. Knepley     ierr = MPI_Type_commit(&contig);CHKERRQ(ierr);
2644bbf5ea8SMatthew G. Knepley 
2654bbf5ea8SMatthew G. Knepley     ierr = PetscSFBcastBegin(rankSF, contig, offsets, remoteOffsets);CHKERRQ(ierr);
2664bbf5ea8SMatthew G. Knepley     ierr = PetscSFBcastEnd(rankSF, contig, offsets, remoteOffsets);CHKERRQ(ierr);
2674bbf5ea8SMatthew G. Knepley     ierr = MPI_Type_free(&contig);CHKERRQ(ierr);
2684bbf5ea8SMatthew G. Knepley     ierr = PetscFree(offsets);CHKERRQ(ierr);
2694bbf5ea8SMatthew G. Knepley     ierr = PetscSFDestroy(&rankSF);CHKERRQ(ierr);
2704bbf5ea8SMatthew G. Knepley     /* Now remoteOffsets contains the offsets on the remote
2714bbf5ea8SMatthew G. Knepley      * processes who communicate with me.  So now we can
2724bbf5ea8SMatthew G. Knepley      * concatenate the list of SFs into a single one. */
2734bbf5ea8SMatthew G. Knepley     index = 0;
2744bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2754bbf5ea8SMatthew G. Knepley       const PetscSFNode *remote = NULL;
2764bbf5ea8SMatthew G. Knepley       const PetscInt    *local  = NULL;
2774bbf5ea8SMatthew G. Knepley       PetscInt           nroots, nleaves, j;
2784bbf5ea8SMatthew G. Knepley 
2794bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetGraph(sf[i], &nroots, &nleaves, &local, &remote);CHKERRQ(ierr);
2804bbf5ea8SMatthew G. Knepley       for (j = 0; j < nleaves; ++j) {
2814bbf5ea8SMatthew G. Knepley         PetscInt rank = remote[j].rank;
2824bbf5ea8SMatthew G. Knepley         PetscInt idx, rootOffset, k;
2834bbf5ea8SMatthew G. Knepley 
2841b68eb51SMatthew G. Knepley         ierr = PetscHMapIGet(rankToIndex, rank, &idx);CHKERRQ(ierr);
2854bbf5ea8SMatthew G. Knepley         if (idx == -1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Didn't find rank, huh?");
2864bbf5ea8SMatthew G. Knepley         /* Offset on given rank for ith subspace */
2874bbf5ea8SMatthew G. Knepley         rootOffset = remoteOffsets[n*idx + i];
2884bbf5ea8SMatthew G. Knepley         for (k = 0; k < bs[i]; ++k) {
28973ec7555SLawrence Mitchell           ilocal[index]        = (local ? local[j] : j)*bs[i] + k + leafOffset;
2904bbf5ea8SMatthew G. Knepley           iremote[index].rank  = remote[j].rank;
2914bbf5ea8SMatthew G. Knepley           iremote[index].index = remote[j].index*bs[i] + k + rootOffset;
2924bbf5ea8SMatthew G. Knepley           ++index;
2934bbf5ea8SMatthew G. Knepley         }
2944bbf5ea8SMatthew G. Knepley       }
2954bbf5ea8SMatthew G. Knepley       leafOffset += nleaves * bs[i];
2964bbf5ea8SMatthew G. Knepley     }
2971b68eb51SMatthew G. Knepley     ierr = PetscHMapIDestroy(&rankToIndex);CHKERRQ(ierr);
2984bbf5ea8SMatthew G. Knepley     ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
2994bbf5ea8SMatthew G. Knepley     ierr = PetscSFCreate(PetscObjectComm((PetscObject)pc), &patch->defaultSF);CHKERRQ(ierr);
3004bbf5ea8SMatthew G. Knepley     ierr = PetscSFSetGraph(patch->defaultSF, allRoots, allLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER);CHKERRQ(ierr);
3014bbf5ea8SMatthew G. Knepley   }
3024bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3034bbf5ea8SMatthew G. Knepley }
3044bbf5ea8SMatthew G. Knepley 
3054bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3065f824522SMatthew G. Knepley PetscErrorCode PCPatchSetIgnoreDim(PC pc, PetscInt dim)
3075f824522SMatthew G. Knepley {
3085f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3095f824522SMatthew G. Knepley   PetscFunctionBegin;
3105f824522SMatthew G. Knepley   patch->ignoredim = dim;
3115f824522SMatthew G. Knepley   PetscFunctionReturn(0);
3125f824522SMatthew G. Knepley }
3135f824522SMatthew G. Knepley 
3145f824522SMatthew G. Knepley /* TODO: Docs */
3155f824522SMatthew G. Knepley PetscErrorCode PCPatchGetIgnoreDim(PC pc, PetscInt *dim)
3165f824522SMatthew G. Knepley {
3175f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3185f824522SMatthew G. Knepley   PetscFunctionBegin;
3195f824522SMatthew G. Knepley   *dim = patch->ignoredim;
3205f824522SMatthew G. Knepley   PetscFunctionReturn(0);
3215f824522SMatthew G. Knepley }
3225f824522SMatthew G. Knepley 
3235f824522SMatthew G. Knepley /* TODO: Docs */
3244bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetSaveOperators(PC pc, PetscBool flg)
3254bbf5ea8SMatthew G. Knepley {
3264bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3274bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3284bbf5ea8SMatthew G. Knepley   patch->save_operators = flg;
3294bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3304bbf5ea8SMatthew G. Knepley }
3314bbf5ea8SMatthew G. Knepley 
3324bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3334bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetSaveOperators(PC pc, PetscBool *flg)
3344bbf5ea8SMatthew G. Knepley {
3354bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3364bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3374bbf5ea8SMatthew G. Knepley   *flg = patch->save_operators;
3384bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3394bbf5ea8SMatthew G. Knepley }
3404bbf5ea8SMatthew G. Knepley 
3414bbf5ea8SMatthew G. Knepley /* TODO: Docs */
342fa84ea4cSLawrence Mitchell PetscErrorCode PCPatchSetPrecomputeElementTensors(PC pc, PetscBool flg)
343fa84ea4cSLawrence Mitchell {
344fa84ea4cSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *) pc->data;
345fa84ea4cSLawrence Mitchell   PetscFunctionBegin;
346fa84ea4cSLawrence Mitchell   patch->precomputeElementTensors = flg;
347fa84ea4cSLawrence Mitchell   PetscFunctionReturn(0);
348fa84ea4cSLawrence Mitchell }
349fa84ea4cSLawrence Mitchell 
350fa84ea4cSLawrence Mitchell /* TODO: Docs */
351fa84ea4cSLawrence Mitchell PetscErrorCode PCPatchGetPrecomputeElementTensors(PC pc, PetscBool *flg)
352fa84ea4cSLawrence Mitchell {
353fa84ea4cSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *) pc->data;
354fa84ea4cSLawrence Mitchell   PetscFunctionBegin;
355fa84ea4cSLawrence Mitchell   *flg = patch->precomputeElementTensors;
356fa84ea4cSLawrence Mitchell   PetscFunctionReturn(0);
357fa84ea4cSLawrence Mitchell }
358fa84ea4cSLawrence Mitchell 
359fa84ea4cSLawrence Mitchell /* TODO: Docs */
3604bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetPartitionOfUnity(PC pc, PetscBool flg)
3614bbf5ea8SMatthew G. Knepley {
3624bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3634bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3644bbf5ea8SMatthew G. Knepley   patch->partition_of_unity = flg;
3654bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3664bbf5ea8SMatthew G. Knepley }
3674bbf5ea8SMatthew G. Knepley 
3684bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3694bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetPartitionOfUnity(PC pc, PetscBool *flg)
3704bbf5ea8SMatthew G. Knepley {
3714bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3724bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3734bbf5ea8SMatthew G. Knepley   *flg = patch->partition_of_unity;
3744bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3754bbf5ea8SMatthew G. Knepley }
3764bbf5ea8SMatthew G. Knepley 
3774bbf5ea8SMatthew G. Knepley /* TODO: Docs */
37861c4b389SFlorian Wechsung PetscErrorCode PCPatchSetLocalComposition(PC pc, PCCompositeType type)
379c2e6f3c0SFlorian Wechsung {
380c2e6f3c0SFlorian Wechsung   PC_PATCH *patch = (PC_PATCH *) pc->data;
381c2e6f3c0SFlorian Wechsung   PetscFunctionBegin;
38261c4b389SFlorian 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");
38361c4b389SFlorian Wechsung   patch->local_composition_type = type;
384c2e6f3c0SFlorian Wechsung   PetscFunctionReturn(0);
385c2e6f3c0SFlorian Wechsung }
386c2e6f3c0SFlorian Wechsung 
387c2e6f3c0SFlorian Wechsung /* TODO: Docs */
38861c4b389SFlorian Wechsung PetscErrorCode PCPatchGetLocalComposition(PC pc, PCCompositeType *type)
389c2e6f3c0SFlorian Wechsung {
390c2e6f3c0SFlorian Wechsung   PC_PATCH *patch = (PC_PATCH *) pc->data;
391c2e6f3c0SFlorian Wechsung   PetscFunctionBegin;
39261c4b389SFlorian Wechsung   *type = patch->local_composition_type;
393c2e6f3c0SFlorian Wechsung   PetscFunctionReturn(0);
394c2e6f3c0SFlorian Wechsung }
395c2e6f3c0SFlorian Wechsung 
396c2e6f3c0SFlorian Wechsung /* TODO: Docs */
3974bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetSubMatType(PC pc, MatType sub_mat_type)
3984bbf5ea8SMatthew G. Knepley {
3994bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
4004bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
4014bbf5ea8SMatthew G. Knepley 
4024bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4034bbf5ea8SMatthew G. Knepley   if (patch->sub_mat_type) {ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr);}
4044bbf5ea8SMatthew G. Knepley   ierr = PetscStrallocpy(sub_mat_type, (char **) &patch->sub_mat_type);CHKERRQ(ierr);
4054bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4064bbf5ea8SMatthew G. Knepley }
4074bbf5ea8SMatthew G. Knepley 
4084bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4094bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetSubMatType(PC pc, MatType *sub_mat_type)
4104bbf5ea8SMatthew G. Knepley {
4114bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4124bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4134bbf5ea8SMatthew G. Knepley   *sub_mat_type = patch->sub_mat_type;
4144bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4154bbf5ea8SMatthew G. Knepley }
4164bbf5ea8SMatthew G. Knepley 
4174bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4184bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetCellNumbering(PC pc, PetscSection cellNumbering)
4194bbf5ea8SMatthew G. Knepley {
4204bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
4214bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
4224bbf5ea8SMatthew G. Knepley 
4234bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4244bbf5ea8SMatthew G. Knepley   patch->cellNumbering = cellNumbering;
4254bbf5ea8SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) cellNumbering);CHKERRQ(ierr);
4264bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4274bbf5ea8SMatthew G. Knepley }
4284bbf5ea8SMatthew G. Knepley 
4294bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4304bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetCellNumbering(PC pc, PetscSection *cellNumbering)
4314bbf5ea8SMatthew G. Knepley {
4324bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4334bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4344bbf5ea8SMatthew G. Knepley   *cellNumbering = patch->cellNumbering;
4354bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4364bbf5ea8SMatthew G. Knepley }
4374bbf5ea8SMatthew G. Knepley 
4384bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4394bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetConstructType(PC pc, PCPatchConstructType ctype, PetscErrorCode (*func)(PC, PetscInt *, IS **, IS *, void *), void *ctx)
4404bbf5ea8SMatthew G. Knepley {
4414bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4424bbf5ea8SMatthew G. Knepley 
4434bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4444bbf5ea8SMatthew G. Knepley   patch->ctype = ctype;
4454bbf5ea8SMatthew G. Knepley   switch (ctype) {
4464bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
44740c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
4484bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Star;
4494bbf5ea8SMatthew G. Knepley     break;
4504bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
45140c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
4524bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Vanka;
4534bbf5ea8SMatthew G. Knepley     break;
454e5b9877fSPatrick Farrell   case PC_PATCH_PARDECOMP:
4550a390943SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
456e5b9877fSPatrick Farrell     patch->patchconstructop = PCPatchConstruct_Pardecomp;
4570a390943SPatrick Farrell     break;
4584bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
4594bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
4604bbf5ea8SMatthew G. Knepley     patch->user_patches     = PETSC_TRUE;
4614bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_User;
462bdd9e0cdSPatrick Farrell     if (func) {
4634bbf5ea8SMatthew G. Knepley       patch->userpatchconstructionop = func;
4644bbf5ea8SMatthew G. Knepley       patch->userpatchconstructctx   = ctx;
465bdd9e0cdSPatrick Farrell     }
4664bbf5ea8SMatthew G. Knepley     break;
4674bbf5ea8SMatthew G. Knepley   default:
4684bbf5ea8SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype);
4694bbf5ea8SMatthew G. Knepley   }
4704bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4714bbf5ea8SMatthew G. Knepley }
4724bbf5ea8SMatthew G. Knepley 
4734bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4744bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetConstructType(PC pc, PCPatchConstructType *ctype, PetscErrorCode (**func)(PC, PetscInt *, IS **, IS *, void *), void **ctx)
4754bbf5ea8SMatthew G. Knepley {
4764bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4774bbf5ea8SMatthew G. Knepley 
4784bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4794bbf5ea8SMatthew G. Knepley   *ctype = patch->ctype;
4804bbf5ea8SMatthew G. Knepley   switch (patch->ctype) {
4814bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
4824bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
483e5b9877fSPatrick Farrell   case PC_PATCH_PARDECOMP:
4844bbf5ea8SMatthew G. Knepley     break;
4854bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
4864bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
4874bbf5ea8SMatthew G. Knepley     *func = patch->userpatchconstructionop;
4884bbf5ea8SMatthew G. Knepley     *ctx  = patch->userpatchconstructctx;
4894bbf5ea8SMatthew G. Knepley     break;
4904bbf5ea8SMatthew G. Knepley   default:
4914bbf5ea8SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype);
4924bbf5ea8SMatthew G. Knepley   }
4934bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4944bbf5ea8SMatthew G. Knepley }
4954bbf5ea8SMatthew G. Knepley 
4964bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4974bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfo(PC pc, PetscInt nsubspaces, DM *dms, PetscInt *bs, PetscInt *nodesPerCell, const PetscInt **cellNodeMap,
4984bbf5ea8SMatthew G. Knepley                                             const PetscInt *subspaceOffsets, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
4994bbf5ea8SMatthew G. Knepley {
5004bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
501*b6bb21d1SLawrence Mitchell   DM             dm, plex;
5024bbf5ea8SMatthew G. Knepley   PetscSF       *sfs;
5035f824522SMatthew G. Knepley   PetscInt       cStart, cEnd, i, j;
5044bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
5054bbf5ea8SMatthew G. Knepley 
5064bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
5075f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
508*b6bb21d1SLawrence Mitchell   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
509*b6bb21d1SLawrence Mitchell   dm = plex;
5105f824522SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
5114bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &sfs);CHKERRQ(ierr);
5124bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->dofSection);CHKERRQ(ierr);
5134bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->bs);CHKERRQ(ierr);
5144bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->nodesPerCell);CHKERRQ(ierr);
5154bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->cellNodeMap);CHKERRQ(ierr);
5164bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces+1, &patch->subspaceOffsets);CHKERRQ(ierr);
5174bbf5ea8SMatthew G. Knepley 
5184bbf5ea8SMatthew G. Knepley   patch->nsubspaces       = nsubspaces;
5194bbf5ea8SMatthew G. Knepley   patch->totalDofsPerCell = 0;
5204bbf5ea8SMatthew G. Knepley   for (i = 0; i < nsubspaces; ++i) {
5214bbf5ea8SMatthew G. Knepley     ierr = DMGetDefaultSection(dms[i], &patch->dofSection[i]);CHKERRQ(ierr);
5224bbf5ea8SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) patch->dofSection[i]);CHKERRQ(ierr);
5234bbf5ea8SMatthew G. Knepley     ierr = DMGetDefaultSF(dms[i], &sfs[i]);CHKERRQ(ierr);
5244bbf5ea8SMatthew G. Knepley     patch->bs[i]              = bs[i];
5254bbf5ea8SMatthew G. Knepley     patch->nodesPerCell[i]    = nodesPerCell[i];
5264bbf5ea8SMatthew G. Knepley     patch->totalDofsPerCell  += nodesPerCell[i]*bs[i];
52780e8a965SFlorian Wechsung     ierr = PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i]);CHKERRQ(ierr);
52880e8a965SFlorian Wechsung     for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
5294bbf5ea8SMatthew G. Knepley     patch->subspaceOffsets[i] = subspaceOffsets[i];
5304bbf5ea8SMatthew G. Knepley   }
5314bbf5ea8SMatthew G. Knepley   ierr = PCPatchCreateDefaultSF_Private(pc, nsubspaces, sfs, patch->bs);CHKERRQ(ierr);
5324bbf5ea8SMatthew G. Knepley   ierr = PetscFree(sfs);CHKERRQ(ierr);
5334bbf5ea8SMatthew G. Knepley 
5344bbf5ea8SMatthew G. Knepley   patch->subspaceOffsets[nsubspaces] = subspaceOffsets[nsubspaces];
5354bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes);CHKERRQ(ierr);
5364bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes);CHKERRQ(ierr);
537*b6bb21d1SLawrence Mitchell   ierr = DMDestroy(&dm);CHKERRQ(ierr);
5384bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5394bbf5ea8SMatthew G. Knepley }
5404bbf5ea8SMatthew G. Knepley 
5414bbf5ea8SMatthew G. Knepley /* TODO: Docs */
5425f824522SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfoCombined(PC pc, DM dm, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
5435f824522SMatthew G. Knepley {
5445f824522SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
5455f824522SMatthew G. Knepley   PetscInt       cStart, cEnd, i, j;
5465f824522SMatthew G. Knepley   PetscErrorCode ierr;
5475f824522SMatthew G. Knepley 
5485f824522SMatthew G. Knepley   PetscFunctionBegin;
5495f824522SMatthew G. Knepley   patch->combined = PETSC_TRUE;
5505f824522SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
5515f824522SMatthew G. Knepley   ierr = DMGetNumFields(dm, &patch->nsubspaces);CHKERRQ(ierr);
5525f824522SMatthew G. Knepley   ierr = PetscCalloc1(patch->nsubspaces, &patch->dofSection);CHKERRQ(ierr);
5535f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->bs);CHKERRQ(ierr);
5545f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->nodesPerCell);CHKERRQ(ierr);
5555f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->cellNodeMap);CHKERRQ(ierr);
5565f824522SMatthew G. Knepley   ierr = PetscCalloc1(patch->nsubspaces+1, &patch->subspaceOffsets);CHKERRQ(ierr);
5575f824522SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &patch->dofSection[0]);CHKERRQ(ierr);
5585f824522SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) patch->dofSection[0]);CHKERRQ(ierr);
5595f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(patch->dofSection[0], &patch->subspaceOffsets[patch->nsubspaces]);CHKERRQ(ierr);
5605f824522SMatthew G. Knepley   patch->totalDofsPerCell = 0;
5615f824522SMatthew G. Knepley   for (i = 0; i < patch->nsubspaces; ++i) {
5625f824522SMatthew G. Knepley     patch->bs[i]             = 1;
5635f824522SMatthew G. Knepley     patch->nodesPerCell[i]   = nodesPerCell[i];
5645f824522SMatthew G. Knepley     patch->totalDofsPerCell += nodesPerCell[i];
5655f824522SMatthew G. Knepley     ierr = PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i]);CHKERRQ(ierr);
5665f824522SMatthew G. Knepley     for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
5675f824522SMatthew G. Knepley   }
5685f824522SMatthew G. Knepley   ierr = DMGetDefaultSF(dm, &patch->defaultSF);CHKERRQ(ierr);
5695f824522SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) patch->defaultSF);CHKERRQ(ierr);
5705f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes);CHKERRQ(ierr);
5715f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes);CHKERRQ(ierr);
5725f824522SMatthew G. Knepley   PetscFunctionReturn(0);
5735f824522SMatthew G. Knepley }
5745f824522SMatthew G. Knepley 
5755f824522SMatthew G. Knepley /*@C
5765f824522SMatthew G. Knepley 
57792d50984SMatthew G. Knepley   PCPatchSetComputeFunction - Set the callback used to compute patch residuals
57892d50984SMatthew G. Knepley 
57999b7e5c6SPatrick Farrell   Logically collective on PC
58099b7e5c6SPatrick Farrell 
58192d50984SMatthew G. Knepley   Input Parameters:
58292d50984SMatthew G. Knepley + pc   - The PC
58392d50984SMatthew G. Knepley . func - The callback
58492d50984SMatthew G. Knepley - ctx  - The user context
58592d50984SMatthew G. Knepley 
5867a50e09dSPatrick Farrell   Calling sequence of func:
5877a50e09dSPatrick Farrell $   func (PC pc,PetscInt point,Vec x,Vec f,IS cellIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx)
5887a50e09dSPatrick Farrell 
5897a50e09dSPatrick Farrell +  pc               - The PC
5907a50e09dSPatrick Farrell .  point            - The point
5917a50e09dSPatrick Farrell .  x                - The input solution (not used in linear problems)
5927a50e09dSPatrick Farrell .  f                - The patch residual vector
5937a50e09dSPatrick Farrell .  cellIS           - An array of the cell numbers
5947a50e09dSPatrick Farrell .  n                - The size of dofsArray
5957a50e09dSPatrick Farrell .  dofsArray        - The dofmap for the dofs to be solved for
5967a50e09dSPatrick Farrell .  dofsArrayWithAll - The dofmap for all dofs on the patch
5977a50e09dSPatrick Farrell -  ctx              - The user context
5987a50e09dSPatrick Farrell 
59992d50984SMatthew G. Knepley   Level: advanced
60092d50984SMatthew G. Knepley 
6017a50e09dSPatrick Farrell   Notes:
60226dc5b63SLawrence Mitchell   The entries of F (the output residual vector) have been set to zero before the call.
60392d50984SMatthew G. Knepley 
60426dc5b63SLawrence Mitchell .seealso: PCPatchSetComputeOperator(), PCPatchGetComputeOperator(), PCPatchSetDiscretisationInfo(), PCPatchSetComputeFunctionInteriorFacets()
60592d50984SMatthew G. Knepley @*/
60639fd2e8aSPatrick Farrell PetscErrorCode PCPatchSetComputeFunction(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Vec, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
60792d50984SMatthew G. Knepley {
60892d50984SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
60992d50984SMatthew G. Knepley 
61092d50984SMatthew G. Knepley   PetscFunctionBegin;
61192d50984SMatthew G. Knepley   patch->usercomputef    = func;
61292d50984SMatthew G. Knepley   patch->usercomputefctx = ctx;
61392d50984SMatthew G. Knepley   PetscFunctionReturn(0);
61492d50984SMatthew G. Knepley }
61592d50984SMatthew G. Knepley 
61692d50984SMatthew G. Knepley /*@C
61792d50984SMatthew G. Knepley 
61859109abcSLawrence Mitchell   PCPatchSetComputeFunctionInteriorFacets - Set the callback used to compute facet integrals for patch residuals
61959109abcSLawrence Mitchell 
6207a50e09dSPatrick Farrell   Logically collective on PC
6217a50e09dSPatrick Farrell 
62259109abcSLawrence Mitchell   Input Parameters:
62359109abcSLawrence Mitchell + pc   - The PC
62459109abcSLawrence Mitchell . func - The callback
62559109abcSLawrence Mitchell - ctx  - The user context
62659109abcSLawrence Mitchell 
6277a50e09dSPatrick Farrell   Calling sequence of func:
6287a50e09dSPatrick Farrell $   func (PC pc,PetscInt point,Vec x,Vec f,IS facetIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx)
6297a50e09dSPatrick Farrell 
6307a50e09dSPatrick Farrell +  pc               - The PC
6317a50e09dSPatrick Farrell .  point            - The point
6327a50e09dSPatrick Farrell .  x                - The input solution (not used in linear problems)
6337a50e09dSPatrick Farrell .  f                - The patch residual vector
6347a50e09dSPatrick Farrell .  facetIS          - An array of the facet numbers
6357a50e09dSPatrick Farrell .  n                - The size of dofsArray
6367a50e09dSPatrick Farrell .  dofsArray        - The dofmap for the dofs to be solved for
6377a50e09dSPatrick Farrell .  dofsArrayWithAll - The dofmap for all dofs on the patch
6387a50e09dSPatrick Farrell -  ctx              - The user context
6397a50e09dSPatrick Farrell 
64059109abcSLawrence Mitchell   Level: advanced
64159109abcSLawrence Mitchell 
6427a50e09dSPatrick Farrell   Notes:
64326dc5b63SLawrence Mitchell   The entries of F (the output residual vector) have been set to zero before the call.
64459109abcSLawrence Mitchell 
64526dc5b63SLawrence Mitchell .seealso: PCPatchSetComputeOperator(), PCPatchGetComputeOperator(), PCPatchSetDiscretisationInfo(), PCPatchSetComputeFunction()
64659109abcSLawrence Mitchell @*/
64759109abcSLawrence Mitchell PetscErrorCode PCPatchSetComputeFunctionInteriorFacets(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Vec, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
64859109abcSLawrence Mitchell {
64959109abcSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *) pc->data;
65059109abcSLawrence Mitchell 
65159109abcSLawrence Mitchell   PetscFunctionBegin;
65259109abcSLawrence Mitchell   patch->usercomputefintfacet    = func;
65359109abcSLawrence Mitchell   patch->usercomputefintfacetctx = ctx;
65459109abcSLawrence Mitchell   PetscFunctionReturn(0);
65559109abcSLawrence Mitchell }
65659109abcSLawrence Mitchell 
65759109abcSLawrence Mitchell /*@C
65859109abcSLawrence Mitchell 
6595f824522SMatthew G. Knepley   PCPatchSetComputeOperator - Set the callback used to compute patch matrices
6605f824522SMatthew G. Knepley 
6617a50e09dSPatrick Farrell   Logically collective on PC
6627a50e09dSPatrick Farrell 
6635f824522SMatthew G. Knepley   Input Parameters:
6645f824522SMatthew G. Knepley + pc   - The PC
6655f824522SMatthew G. Knepley . func - The callback
6665f824522SMatthew G. Knepley - ctx  - The user context
6675f824522SMatthew G. Knepley 
6687a50e09dSPatrick Farrell   Calling sequence of func:
6697a50e09dSPatrick Farrell $   func (PC pc,PetscInt point,Vec x,Mat mat,IS facetIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx)
6707a50e09dSPatrick Farrell 
6717a50e09dSPatrick Farrell +  pc               - The PC
6727a50e09dSPatrick Farrell .  point            - The point
6737a50e09dSPatrick Farrell .  x                - The input solution (not used in linear problems)
6747a50e09dSPatrick Farrell .  mat              - The patch matrix
6757a50e09dSPatrick Farrell .  cellIS           - An array of the cell numbers
6767a50e09dSPatrick Farrell .  n                - The size of dofsArray
6777a50e09dSPatrick Farrell .  dofsArray        - The dofmap for the dofs to be solved for
6787a50e09dSPatrick Farrell .  dofsArrayWithAll - The dofmap for all dofs on the patch
6797a50e09dSPatrick Farrell -  ctx              - The user context
6807a50e09dSPatrick Farrell 
6815f824522SMatthew G. Knepley   Level: advanced
6825f824522SMatthew G. Knepley 
6837a50e09dSPatrick Farrell   Notes:
6847a50e09dSPatrick Farrell   The matrix entries have been set to zero before the call.
6855f824522SMatthew G. Knepley 
686723f9013SMatthew G. Knepley .seealso: PCPatchGetComputeOperator(), PCPatchSetComputeFunction(), PCPatchSetDiscretisationInfo()
6875f824522SMatthew G. Knepley @*/
6884d04e9f1SPatrick Farrell PetscErrorCode PCPatchSetComputeOperator(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Mat, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
6894bbf5ea8SMatthew G. Knepley {
6904bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
6914bbf5ea8SMatthew G. Knepley 
6924bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
6934bbf5ea8SMatthew G. Knepley   patch->usercomputeop    = func;
694723f9013SMatthew G. Knepley   patch->usercomputeopctx = ctx;
6954bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
6964bbf5ea8SMatthew G. Knepley }
6974bbf5ea8SMatthew G. Knepley 
69859109abcSLawrence Mitchell /*@C
69959109abcSLawrence Mitchell 
7007a50e09dSPatrick Farrell   PCPatchSetComputeOperatorInteriorFacets - Set the callback used to compute facet integrals for patch matrices
70159109abcSLawrence Mitchell 
70299b7e5c6SPatrick Farrell   Logically collective on PC
70399b7e5c6SPatrick Farrell 
70459109abcSLawrence Mitchell   Input Parameters:
70559109abcSLawrence Mitchell + pc   - The PC
70659109abcSLawrence Mitchell . func - The callback
70759109abcSLawrence Mitchell - ctx  - The user context
70859109abcSLawrence Mitchell 
7097a50e09dSPatrick Farrell   Calling sequence of func:
7107a50e09dSPatrick Farrell $   func (PC pc,PetscInt point,Vec x,Mat mat,IS facetIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx)
7117a50e09dSPatrick Farrell 
7127a50e09dSPatrick Farrell +  pc               - The PC
7137a50e09dSPatrick Farrell .  point            - The point
7147a50e09dSPatrick Farrell .  x                - The input solution (not used in linear problems)
7157a50e09dSPatrick Farrell .  mat              - The patch matrix
7167a50e09dSPatrick Farrell .  facetIS          - An array of the facet numbers
7177a50e09dSPatrick Farrell .  n                - The size of dofsArray
7187a50e09dSPatrick Farrell .  dofsArray        - The dofmap for the dofs to be solved for
7197a50e09dSPatrick Farrell .  dofsArrayWithAll - The dofmap for all dofs on the patch
7207a50e09dSPatrick Farrell -  ctx              - The user context
7217a50e09dSPatrick Farrell 
72259109abcSLawrence Mitchell   Level: advanced
72359109abcSLawrence Mitchell 
7247a50e09dSPatrick Farrell   Notes:
7257a50e09dSPatrick Farrell   The matrix entries have been set to zero before the call.
72659109abcSLawrence Mitchell 
72759109abcSLawrence Mitchell .seealso: PCPatchGetComputeOperator(), PCPatchSetComputeFunction(), PCPatchSetDiscretisationInfo()
72859109abcSLawrence Mitchell @*/
72959109abcSLawrence Mitchell PetscErrorCode PCPatchSetComputeOperatorInteriorFacets(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Mat, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
73059109abcSLawrence Mitchell {
73159109abcSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *) pc->data;
73259109abcSLawrence Mitchell 
73359109abcSLawrence Mitchell   PetscFunctionBegin;
73459109abcSLawrence Mitchell   patch->usercomputeopintfacet    = func;
73559109abcSLawrence Mitchell   patch->usercomputeopintfacetctx = ctx;
73659109abcSLawrence Mitchell   PetscFunctionReturn(0);
73759109abcSLawrence Mitchell }
73859109abcSLawrence Mitchell 
7394bbf5ea8SMatthew G. Knepley /* On entry, ht contains the topological entities whose dofs we are responsible for solving for;
7404bbf5ea8SMatthew G. Knepley    on exit, cht contains all the topological entities we need to compute their residuals.
7414bbf5ea8SMatthew G. Knepley    In full generality this should incorporate knowledge of the sparsity pattern of the matrix;
7424bbf5ea8SMatthew G. Knepley    here we assume a standard FE sparsity pattern.*/
7434bbf5ea8SMatthew G. Knepley /* TODO: Use DMPlexGetAdjacency() */
7441b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchCompleteCellPatch(PC pc, PetscHSetI ht, PetscHSetI cht)
7454bbf5ea8SMatthew G. Knepley {
746*b6bb21d1SLawrence Mitchell   DM             dm, plex;
747bc7fa33aSFlorian Wechsung   PC_PATCH      *patch = (PC_PATCH *) pc->data;
7481b68eb51SMatthew G. Knepley   PetscHashIter  hi;
7494bbf5ea8SMatthew G. Knepley   PetscInt       point;
7504bbf5ea8SMatthew G. Knepley   PetscInt      *star = NULL, *closure = NULL;
7514c954380SMatthew G. Knepley   PetscInt       ignoredim, iStart = 0, iEnd = -1, starSize, closureSize, si, ci;
752bc7fa33aSFlorian Wechsung   PetscInt      *fStar = NULL, *fClosure = NULL;
753bc7fa33aSFlorian Wechsung   PetscInt       fBegin, fEnd, fsi, fci, fStarSize, fClosureSize;
7544bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
7554bbf5ea8SMatthew G. Knepley 
7564bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
7575f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
758*b6bb21d1SLawrence Mitchell   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
759*b6bb21d1SLawrence Mitchell   dm = plex;
760bc7fa33aSFlorian Wechsung   ierr = DMPlexGetHeightStratum(dm, 1, &fBegin, &fEnd);CHKERRQ(ierr);
7615f824522SMatthew G. Knepley   ierr = PCPatchGetIgnoreDim(pc, &ignoredim);CHKERRQ(ierr);
7625f824522SMatthew G. Knepley   if (ignoredim >= 0) {ierr = DMPlexGetDepthStratum(dm, ignoredim, &iStart, &iEnd);CHKERRQ(ierr);}
7631b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(cht);CHKERRQ(ierr);
7641b68eb51SMatthew G. Knepley   PetscHashIterBegin(ht, hi);
7651b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(ht, hi)) {
7664c954380SMatthew G. Knepley 
7671b68eb51SMatthew G. Knepley     PetscHashIterGetKey(ht, hi, point);
7681b68eb51SMatthew G. Knepley     PetscHashIterNext(ht, hi);
7694bbf5ea8SMatthew G. Knepley 
7704bbf5ea8SMatthew G. Knepley     /* Loop over all the cells that this point connects to */
7714bbf5ea8SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
7725f824522SMatthew G. Knepley     for (si = 0; si < starSize*2; si += 2) {
7734c954380SMatthew G. Knepley       const PetscInt ownedpoint = star[si];
7745f824522SMatthew G. Knepley       /* TODO Check for point in cht before running through closure again */
7754bbf5ea8SMatthew G. Knepley       /* now loop over all entities in the closure of that cell */
7764bbf5ea8SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, ownedpoint, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
7775f824522SMatthew G. Knepley       for (ci = 0; ci < closureSize*2; ci += 2) {
7784c954380SMatthew G. Knepley         const PetscInt seenpoint = closure[ci];
7795f824522SMatthew G. Knepley         if (ignoredim >= 0 && seenpoint >= iStart && seenpoint < iEnd) continue;
7801b68eb51SMatthew G. Knepley         ierr = PetscHSetIAdd(cht, seenpoint);CHKERRQ(ierr);
781bc7fa33aSFlorian Wechsung         /* Facet integrals couple dofs across facets, so in that case for each of
782bc7fa33aSFlorian Wechsung          * the facets we need to add all dofs on the other side of the facet to
783bc7fa33aSFlorian Wechsung          * the seen dofs. */
784bc7fa33aSFlorian Wechsung         if(patch->usercomputeopintfacet){
785bc7fa33aSFlorian Wechsung           if(fBegin <= seenpoint && seenpoint < fEnd){
786bc7fa33aSFlorian Wechsung             ierr = DMPlexGetTransitiveClosure(dm, seenpoint, PETSC_FALSE, &fStarSize, &fStar);CHKERRQ(ierr);
787bc7fa33aSFlorian Wechsung             for (fsi = 0; fsi < fStarSize*2; fsi += 2) {
788bc7fa33aSFlorian Wechsung               ierr = DMPlexGetTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, &fClosureSize, &fClosure);CHKERRQ(ierr);
789bc7fa33aSFlorian Wechsung               for (fci = 0; fci < fClosureSize*2; fci += 2) {
790bc7fa33aSFlorian Wechsung                 ierr = PetscHSetIAdd(cht, fClosure[fci]);CHKERRQ(ierr);
791bc7fa33aSFlorian Wechsung               }
792bc7fa33aSFlorian Wechsung               ierr = DMPlexRestoreTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, NULL, &fClosure);CHKERRQ(ierr);
793bc7fa33aSFlorian Wechsung             }
794bc7fa33aSFlorian Wechsung             ierr = DMPlexRestoreTransitiveClosure(dm, seenpoint, PETSC_FALSE, NULL, &fStar);CHKERRQ(ierr);
795bc7fa33aSFlorian Wechsung           }
796bc7fa33aSFlorian Wechsung         }
7974bbf5ea8SMatthew G. Knepley       }
7982d76c0eeSPatrick Farrell       ierr = DMPlexRestoreTransitiveClosure(dm, ownedpoint, PETSC_TRUE, NULL, &closure);CHKERRQ(ierr);
7994bbf5ea8SMatthew G. Knepley     }
8002d76c0eeSPatrick Farrell     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, NULL, &star);CHKERRQ(ierr);
8014bbf5ea8SMatthew G. Knepley   }
802*b6bb21d1SLawrence Mitchell   ierr = DMDestroy(&dm);CHKERRQ(ierr);
8035f824522SMatthew G. Knepley   PetscFunctionReturn(0);
8045f824522SMatthew G. Knepley }
8055f824522SMatthew G. Knepley 
8065f824522SMatthew G. Knepley static PetscErrorCode PCPatchGetGlobalDofs(PC pc, PetscSection dofSection[], PetscInt f, PetscBool combined, PetscInt p, PetscInt *dof, PetscInt *off)
8075f824522SMatthew G. Knepley {
8085f824522SMatthew G. Knepley   PetscErrorCode ierr;
8095f824522SMatthew G. Knepley 
8105f824522SMatthew G. Knepley   PetscFunctionBegin;
8115f824522SMatthew G. Knepley   if (combined) {
8125f824522SMatthew G. Knepley     if (f < 0) {
8135f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetDof(dofSection[0], p, dof);CHKERRQ(ierr);}
8145f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetOffset(dofSection[0], p, off);CHKERRQ(ierr);}
8155f824522SMatthew G. Knepley     } else {
8165f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetFieldDof(dofSection[0], p, f, dof);CHKERRQ(ierr);}
8175f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetFieldOffset(dofSection[0], p, f, off);CHKERRQ(ierr);}
8185f824522SMatthew G. Knepley     }
8195f824522SMatthew G. Knepley   } else {
8205f824522SMatthew G. Knepley     if (f < 0) {
8215f824522SMatthew G. Knepley       PC_PATCH *patch = (PC_PATCH *) pc->data;
8225f824522SMatthew G. Knepley       PetscInt  fdof, g;
8235f824522SMatthew G. Knepley 
8245f824522SMatthew G. Knepley       if (dof) {
8255f824522SMatthew G. Knepley         *dof = 0;
8265f824522SMatthew G. Knepley         for (g = 0; g < patch->nsubspaces; ++g) {
8275f824522SMatthew G. Knepley           ierr = PetscSectionGetDof(dofSection[g], p, &fdof);CHKERRQ(ierr);
8285f824522SMatthew G. Knepley           *dof += fdof;
8295f824522SMatthew G. Knepley         }
8305f824522SMatthew G. Knepley       }
831624e31c3SLawrence Mitchell       if (off) {
832624e31c3SLawrence Mitchell         *off = 0;
833624e31c3SLawrence Mitchell         for (g = 0; g < patch->nsubspaces; ++g) {
834624e31c3SLawrence Mitchell           ierr = PetscSectionGetOffset(dofSection[g], p, &fdof);CHKERRQ(ierr);
835624e31c3SLawrence Mitchell           *off += fdof;
836624e31c3SLawrence Mitchell         }
837624e31c3SLawrence Mitchell       }
8385f824522SMatthew G. Knepley     } else {
8395f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetDof(dofSection[f], p, dof);CHKERRQ(ierr);}
8405f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetOffset(dofSection[f], p, off);CHKERRQ(ierr);}
8415f824522SMatthew G. Knepley     }
8425f824522SMatthew G. Knepley   }
8434bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
8444bbf5ea8SMatthew G. Knepley }
8454bbf5ea8SMatthew G. Knepley 
8464bbf5ea8SMatthew G. Knepley /* Given a hash table with a set of topological entities (pts), compute the degrees of
8474bbf5ea8SMatthew G. Knepley    freedom in global concatenated numbering on those entities.
8484bbf5ea8SMatthew G. Knepley    For Vanka smoothing, this needs to do something special: ignore dofs of the
8494bbf5ea8SMatthew G. Knepley    constraint subspace on entities that aren't the base entity we're building the patch
8504bbf5ea8SMatthew G. Knepley    around. */
851e4c66b91SPatrick Farrell static PetscErrorCode PCPatchGetPointDofs(PC pc, PetscHSetI pts, PetscHSetI dofs, PetscInt base, PetscHSetI* subspaces_to_exclude)
8524bbf5ea8SMatthew G. Knepley {
8535f824522SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
8541b68eb51SMatthew G. Knepley   PetscHashIter  hi;
8554bbf5ea8SMatthew G. Knepley   PetscInt       ldof, loff;
8564bbf5ea8SMatthew G. Knepley   PetscInt       k, p;
8574bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
8584bbf5ea8SMatthew G. Knepley 
8594bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
8601b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(dofs);CHKERRQ(ierr);
8614bbf5ea8SMatthew G. Knepley   for (k = 0; k < patch->nsubspaces; ++k) {
8624bbf5ea8SMatthew G. Knepley     PetscInt subspaceOffset = patch->subspaceOffsets[k];
8634bbf5ea8SMatthew G. Knepley     PetscInt bs             = patch->bs[k];
8644bbf5ea8SMatthew G. Knepley     PetscInt j, l;
8654bbf5ea8SMatthew G. Knepley 
866e4c66b91SPatrick Farrell     if (subspaces_to_exclude != NULL) {
867e4c66b91SPatrick Farrell       PetscBool should_exclude_k = PETSC_FALSE;
868e4c66b91SPatrick Farrell       PetscHSetIHas(*subspaces_to_exclude, k, &should_exclude_k);
869e4c66b91SPatrick Farrell       if (should_exclude_k) {
8704bbf5ea8SMatthew G. Knepley         /* only get this subspace dofs at the base entity, not any others */
8715f824522SMatthew G. Knepley         ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, base, &ldof, &loff);CHKERRQ(ierr);
8724bbf5ea8SMatthew G. Knepley         if (0 == ldof) continue;
8734bbf5ea8SMatthew G. Knepley         for (j = loff; j < ldof + loff; ++j) {
8744bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
8754bbf5ea8SMatthew G. Knepley             PetscInt dof = bs*j + l + subspaceOffset;
8761b68eb51SMatthew G. Knepley             ierr = PetscHSetIAdd(dofs, dof);CHKERRQ(ierr);
8774bbf5ea8SMatthew G. Knepley           }
8784bbf5ea8SMatthew G. Knepley         }
8794bbf5ea8SMatthew G. Knepley         continue; /* skip the other dofs of this subspace */
8804bbf5ea8SMatthew G. Knepley       }
881e4c66b91SPatrick Farrell     }
8824bbf5ea8SMatthew G. Knepley 
8831b68eb51SMatthew G. Knepley     PetscHashIterBegin(pts, hi);
8841b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(pts, hi)) {
8851b68eb51SMatthew G. Knepley       PetscHashIterGetKey(pts, hi, p);
8861b68eb51SMatthew G. Knepley       PetscHashIterNext(pts, hi);
8875f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, p, &ldof, &loff);CHKERRQ(ierr);
8884bbf5ea8SMatthew G. Knepley       if (0 == ldof) continue;
8894bbf5ea8SMatthew G. Knepley       for (j = loff; j < ldof + loff; ++j) {
8904bbf5ea8SMatthew G. Knepley         for (l = 0; l < bs; ++l) {
8914bbf5ea8SMatthew G. Knepley           PetscInt dof = bs*j + l + subspaceOffset;
8921b68eb51SMatthew G. Knepley           ierr = PetscHSetIAdd(dofs, dof);CHKERRQ(ierr);
8934bbf5ea8SMatthew G. Knepley         }
8944bbf5ea8SMatthew G. Knepley       }
8954bbf5ea8SMatthew G. Knepley     }
8964bbf5ea8SMatthew G. Knepley   }
8974bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
8984bbf5ea8SMatthew G. Knepley }
8994bbf5ea8SMatthew G. Knepley 
9004bbf5ea8SMatthew G. Knepley /* Given two hash tables A and B, compute the keys in B that are not in A, and put them in C */
9011b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchComputeSetDifference_Private(PetscHSetI A, PetscHSetI B, PetscHSetI C)
9024bbf5ea8SMatthew G. Knepley {
9031b68eb51SMatthew G. Knepley   PetscHashIter  hi;
9041b68eb51SMatthew G. Knepley   PetscInt       key;
9054bbf5ea8SMatthew G. Knepley   PetscBool      flg;
9061b68eb51SMatthew G. Knepley   PetscErrorCode ierr;
9074bbf5ea8SMatthew G. Knepley 
9084bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
9091b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(C);CHKERRQ(ierr);
9101b68eb51SMatthew G. Knepley   PetscHashIterBegin(B, hi);
9111b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(B, hi)) {
9121b68eb51SMatthew G. Knepley     PetscHashIterGetKey(B, hi, key);
9131b68eb51SMatthew G. Knepley     PetscHashIterNext(B, hi);
9141b68eb51SMatthew G. Knepley     ierr = PetscHSetIHas(A, key, &flg);CHKERRQ(ierr);
9151b68eb51SMatthew G. Knepley     if (!flg) {ierr = PetscHSetIAdd(C, key);CHKERRQ(ierr);}
9164bbf5ea8SMatthew G. Knepley   }
9174bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
9184bbf5ea8SMatthew G. Knepley }
9194bbf5ea8SMatthew G. Knepley 
9204bbf5ea8SMatthew G. Knepley /*
9214bbf5ea8SMatthew G. Knepley  * PCPatchCreateCellPatches - create patches.
9224bbf5ea8SMatthew G. Knepley  *
9234bbf5ea8SMatthew G. Knepley  * Input Parameters:
9244bbf5ea8SMatthew G. Knepley  * + dm - The DMPlex object defining the mesh
9254bbf5ea8SMatthew G. Knepley  *
9264bbf5ea8SMatthew G. Knepley  * Output Parameters:
9274bbf5ea8SMatthew G. Knepley  * + cellCounts  - Section with counts of cells around each vertex
9285f824522SMatthew G. Knepley  * . cells       - IS of the cell point indices of cells in each patch
9295f824522SMatthew G. Knepley  * . pointCounts - Section with counts of cells around each vertex
9305f824522SMatthew G. Knepley  * - point       - IS of the cell point indices of cells in each patch
9314bbf5ea8SMatthew G. Knepley  */
9324bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatches(PC pc)
9334bbf5ea8SMatthew G. Knepley {
9344bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
9355f824522SMatthew G. Knepley   DMLabel         ghost = NULL;
9364bbf5ea8SMatthew G. Knepley   DM              dm, plex;
9371b68eb51SMatthew G. Knepley   PetscHSetI      ht, cht;
9380e126c0bSLawrence Mitchell   PetscSection    cellCounts,  pointCounts, intFacetCounts, extFacetCounts;
939eb62eeaaSLawrence Mitchell   PetscInt       *cellsArray, *pointsArray, *intFacetsArray, *extFacetsArray, *intFacetsToPatchCell;
9400e126c0bSLawrence Mitchell   PetscInt        numCells, numPoints, numIntFacets, numExtFacets;
9415f824522SMatthew G. Knepley   const PetscInt *leaves;
94233cbca70SPatrick Farrell   PetscInt        nleaves, pStart, pEnd, cStart, cEnd, vStart, vEnd, fStart, fEnd, v;
9435f824522SMatthew G. Knepley   PetscBool       isFiredrake;
9444bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
9454bbf5ea8SMatthew G. Knepley 
9464bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
9474bbf5ea8SMatthew G. Knepley   /* Used to keep track of the cells in the patch. */
9481b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&ht);CHKERRQ(ierr);
9491b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&cht);CHKERRQ(ierr);
9504bbf5ea8SMatthew G. Knepley 
9514bbf5ea8SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
9524bbf5ea8SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONGSTATE, "DM not yet set on patch PC\n");
9534bbf5ea8SMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
954*b6bb21d1SLawrence Mitchell   dm = plex;
955*b6bb21d1SLawrence Mitchell   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
956*b6bb21d1SLawrence Mitchell   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
9574bbf5ea8SMatthew G. Knepley 
9584bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
9595f824522SMatthew G. Knepley     ierr = patch->userpatchconstructionop(pc, &patch->npatch, &patch->userIS, &patch->iterationSet, patch->userpatchconstructctx);CHKERRQ(ierr);
9605f824522SMatthew G. Knepley     vStart = 0; vEnd = patch->npatch;
961e5b9877fSPatrick Farrell   } else if (patch->ctype == PC_PATCH_PARDECOMP) {
9620a390943SPatrick Farrell     vStart = 0; vEnd = 1;
9635f824522SMatthew G. Knepley   } else if (patch->codim < 0) {
964*b6bb21d1SLawrence Mitchell     if (patch->dim < 0) {ierr = DMPlexGetDepthStratum(dm,  0,            &vStart, &vEnd);CHKERRQ(ierr);}
965*b6bb21d1SLawrence Mitchell     else                {ierr = DMPlexGetDepthStratum(dm,  patch->dim,   &vStart, &vEnd);CHKERRQ(ierr);}
966*b6bb21d1SLawrence Mitchell   } else                {ierr = DMPlexGetHeightStratum(dm, patch->codim, &vStart, &vEnd);CHKERRQ(ierr);}
9675f824522SMatthew G. Knepley   patch->npatch = vEnd - vStart;
9684bbf5ea8SMatthew G. Knepley 
9694bbf5ea8SMatthew G. Knepley   /* These labels mark the owned points.  We only create patches around points that this process owns. */
9705f824522SMatthew G. Knepley   ierr = DMHasLabel(dm, "pyop2_ghost", &isFiredrake);CHKERRQ(ierr);
9715f824522SMatthew G. Knepley   if (isFiredrake) {
9724bbf5ea8SMatthew G. Knepley     ierr = DMGetLabel(dm, "pyop2_ghost", &ghost);CHKERRQ(ierr);
9734bbf5ea8SMatthew G. Knepley     ierr = DMLabelCreateIndex(ghost, pStart, pEnd);CHKERRQ(ierr);
9745f824522SMatthew G. Knepley   } else {
9755f824522SMatthew G. Knepley     PetscSF sf;
9765f824522SMatthew G. Knepley 
9775f824522SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
9785f824522SMatthew G. Knepley     ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
9795f824522SMatthew G. Knepley     nleaves = PetscMax(nleaves, 0);
9805f824522SMatthew G. Knepley   }
9814bbf5ea8SMatthew G. Knepley 
9824bbf5ea8SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->cellCounts);CHKERRQ(ierr);
9835f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->cellCounts, "Patch Cell Layout");CHKERRQ(ierr);
9844bbf5ea8SMatthew G. Knepley   cellCounts = patch->cellCounts;
9854bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetChart(cellCounts, vStart, vEnd);CHKERRQ(ierr);
9865f824522SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->pointCounts);CHKERRQ(ierr);
9875f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->pointCounts, "Patch Point Layout");CHKERRQ(ierr);
9885f824522SMatthew G. Knepley   pointCounts = patch->pointCounts;
9895f824522SMatthew G. Knepley   ierr = PetscSectionSetChart(pointCounts, vStart, vEnd);CHKERRQ(ierr);
9900e126c0bSLawrence Mitchell   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->extFacetCounts);CHKERRQ(ierr);
9910e126c0bSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->extFacetCounts, "Patch Exterior Facet Layout");CHKERRQ(ierr);
9920e126c0bSLawrence Mitchell   extFacetCounts = patch->extFacetCounts;
9930e126c0bSLawrence Mitchell   ierr = PetscSectionSetChart(extFacetCounts, vStart, vEnd);CHKERRQ(ierr);
9940e126c0bSLawrence Mitchell   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->intFacetCounts);CHKERRQ(ierr);
9950e126c0bSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->intFacetCounts, "Patch Interior Facet Layout");CHKERRQ(ierr);
9960e126c0bSLawrence Mitchell   intFacetCounts = patch->intFacetCounts;
9970e126c0bSLawrence Mitchell   ierr = PetscSectionSetChart(intFacetCounts, vStart, vEnd);CHKERRQ(ierr);
9985f824522SMatthew G. Knepley   /* Count cells and points in the patch surrounding each entity */
9990e126c0bSLawrence Mitchell   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
10004bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
10011b68eb51SMatthew G. Knepley     PetscHashIter hi;
10025f824522SMatthew G. Knepley     PetscInt       chtSize, loc = -1;
10035f824522SMatthew G. Knepley     PetscBool      flg;
10044bbf5ea8SMatthew G. Knepley 
1005b525f888SPatrick Farrell     if (!patch->user_patches && patch->ctype != PC_PATCH_PARDECOMP) {
10065f824522SMatthew G. Knepley       if (ghost) {ierr = DMLabelHasPoint(ghost, v, &flg);CHKERRQ(ierr);}
1007928bb9adSStefano Zampini       else       {ierr = PetscFindInt(v, nleaves, leaves, &loc);CHKERRQ(ierr); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;}
10084bbf5ea8SMatthew G. Knepley       /* Not an owned entity, don't make a cell patch. */
10094bbf5ea8SMatthew G. Knepley       if (flg) continue;
10104bbf5ea8SMatthew G. Knepley     }
10114bbf5ea8SMatthew G. Knepley 
10124bbf5ea8SMatthew G. Knepley     ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr);
10135f824522SMatthew G. Knepley     ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr);
10141b68eb51SMatthew G. Knepley     ierr = PetscHSetIGetSize(cht, &chtSize);CHKERRQ(ierr);
10154bbf5ea8SMatthew G. Knepley     /* empty patch, continue */
10164bbf5ea8SMatthew G. Knepley     if (chtSize == 0) continue;
10174bbf5ea8SMatthew G. Knepley 
10184bbf5ea8SMatthew G. Knepley     /* safe because size(cht) > 0 from above */
10191b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
10201b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
10215f824522SMatthew G. Knepley       PetscInt point, pdof;
10224bbf5ea8SMatthew G. Knepley 
10231b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
10240e126c0bSLawrence Mitchell       if (fStart <= point && point < fEnd) {
10250e126c0bSLawrence Mitchell         const PetscInt *support;
10260e126c0bSLawrence Mitchell         PetscInt supportSize, p;
10270e126c0bSLawrence Mitchell         PetscBool interior = PETSC_TRUE;
10280e126c0bSLawrence Mitchell         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
10290e126c0bSLawrence Mitchell         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
10300e126c0bSLawrence Mitchell         if (supportSize == 1) {
10310e126c0bSLawrence Mitchell           interior = PETSC_FALSE;
10320e126c0bSLawrence Mitchell         } else {
10330e126c0bSLawrence Mitchell           for (p = 0; p < supportSize; p++) {
10340e126c0bSLawrence Mitchell             PetscBool found;
10350e126c0bSLawrence Mitchell             /* FIXME: can I do this while iterating over cht? */
10360e126c0bSLawrence Mitchell             PetscHSetIHas(cht, support[p], &found);
10370e126c0bSLawrence Mitchell             if (!found) {
10380e126c0bSLawrence Mitchell               interior = PETSC_FALSE;
10390e126c0bSLawrence Mitchell               break;
10400e126c0bSLawrence Mitchell             }
10410e126c0bSLawrence Mitchell           }
10420e126c0bSLawrence Mitchell         }
10430e126c0bSLawrence Mitchell         if (interior) {
10440e126c0bSLawrence Mitchell           ierr = PetscSectionAddDof(intFacetCounts, v, 1);CHKERRQ(ierr);
10450e126c0bSLawrence Mitchell         } else {
10460e126c0bSLawrence Mitchell           ierr = PetscSectionAddDof(extFacetCounts, v, 1);CHKERRQ(ierr);
10470e126c0bSLawrence Mitchell         }
10480e126c0bSLawrence Mitchell       }
10495f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr);
10505f824522SMatthew G. Knepley       if (pdof)                            {ierr = PetscSectionAddDof(pointCounts, v, 1);CHKERRQ(ierr);}
10515f824522SMatthew G. Knepley       if (point >= cStart && point < cEnd) {ierr = PetscSectionAddDof(cellCounts, v, 1);CHKERRQ(ierr);}
10521b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
10534bbf5ea8SMatthew G. Knepley     }
10544bbf5ea8SMatthew G. Knepley   }
10555f824522SMatthew G. Knepley   if (isFiredrake) {ierr = DMLabelDestroyIndex(ghost);CHKERRQ(ierr);}
10564bbf5ea8SMatthew G. Knepley 
10574bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetUp(cellCounts);CHKERRQ(ierr);
10584bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr);
10594bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numCells, &cellsArray);CHKERRQ(ierr);
10605f824522SMatthew G. Knepley   ierr = PetscSectionSetUp(pointCounts);CHKERRQ(ierr);
10615f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(pointCounts, &numPoints);CHKERRQ(ierr);
10625f824522SMatthew G. Knepley   ierr = PetscMalloc1(numPoints, &pointsArray);CHKERRQ(ierr);
10634bbf5ea8SMatthew G. Knepley 
10640e126c0bSLawrence Mitchell   ierr = PetscSectionSetUp(intFacetCounts);CHKERRQ(ierr);
10650e126c0bSLawrence Mitchell   ierr = PetscSectionSetUp(extFacetCounts);CHKERRQ(ierr);
10660e126c0bSLawrence Mitchell   ierr = PetscSectionGetStorageSize(intFacetCounts, &numIntFacets);CHKERRQ(ierr);
10670e126c0bSLawrence Mitchell   ierr = PetscSectionGetStorageSize(extFacetCounts, &numExtFacets);CHKERRQ(ierr);
10680e126c0bSLawrence Mitchell   ierr = PetscMalloc1(numIntFacets, &intFacetsArray);CHKERRQ(ierr);
1069eb62eeaaSLawrence Mitchell   ierr = PetscMalloc1(numIntFacets*2, &intFacetsToPatchCell);CHKERRQ(ierr);
10700e126c0bSLawrence Mitchell   ierr = PetscMalloc1(numExtFacets, &extFacetsArray);CHKERRQ(ierr);
10710e126c0bSLawrence Mitchell 
1072eb62eeaaSLawrence Mitchell 
10734bbf5ea8SMatthew G. Knepley   /* Now that we know how much space we need, run through again and actually remember the cells. */
10744bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; v++ ) {
10751b68eb51SMatthew G. Knepley     PetscHashIter hi;
10760e126c0bSLawrence Mitchell     PetscInt       dof, off, cdof, coff, efdof, efoff, ifdof, ifoff, pdof, n = 0, cn = 0, ifn = 0, efn = 0;
10774bbf5ea8SMatthew G. Knepley 
10785f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(pointCounts, v, &dof);CHKERRQ(ierr);
10795f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(pointCounts, v, &off);CHKERRQ(ierr);
10805f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &cdof);CHKERRQ(ierr);
10815f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &coff);CHKERRQ(ierr);
10820e126c0bSLawrence Mitchell     ierr = PetscSectionGetDof(intFacetCounts, v, &ifdof);CHKERRQ(ierr);
10830e126c0bSLawrence Mitchell     ierr = PetscSectionGetOffset(intFacetCounts, v, &ifoff);CHKERRQ(ierr);
10840e126c0bSLawrence Mitchell     ierr = PetscSectionGetDof(extFacetCounts, v, &efdof);CHKERRQ(ierr);
10850e126c0bSLawrence Mitchell     ierr = PetscSectionGetOffset(extFacetCounts, v, &efoff);CHKERRQ(ierr);
10865f824522SMatthew G. Knepley     if (dof <= 0) continue;
10874bbf5ea8SMatthew G. Knepley     ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr);
10885f824522SMatthew G. Knepley     ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr);
10891b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
10901b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
10914bbf5ea8SMatthew G. Knepley       PetscInt point;
10924bbf5ea8SMatthew G. Knepley 
10931b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
10940e126c0bSLawrence Mitchell       if (fStart <= point && point < fEnd) {
10950e126c0bSLawrence Mitchell         const PetscInt *support;
10960e126c0bSLawrence Mitchell         PetscInt supportSize, p;
10970e126c0bSLawrence Mitchell         PetscBool interior = PETSC_TRUE;
10980e126c0bSLawrence Mitchell         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
10990e126c0bSLawrence Mitchell         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
11000e126c0bSLawrence Mitchell         if (supportSize == 1) {
11010e126c0bSLawrence Mitchell           interior = PETSC_FALSE;
11020e126c0bSLawrence Mitchell         } else {
11030e126c0bSLawrence Mitchell           for (p = 0; p < supportSize; p++) {
11040e126c0bSLawrence Mitchell             PetscBool found;
11050e126c0bSLawrence Mitchell             /* FIXME: can I do this while iterating over cht? */
11060e126c0bSLawrence Mitchell             PetscHSetIHas(cht, support[p], &found);
11070e126c0bSLawrence Mitchell             if (!found) {
11080e126c0bSLawrence Mitchell               interior = PETSC_FALSE;
11090e126c0bSLawrence Mitchell               break;
11100e126c0bSLawrence Mitchell             }
11110e126c0bSLawrence Mitchell           }
11120e126c0bSLawrence Mitchell         }
11130e126c0bSLawrence Mitchell         if (interior) {
111444b625f7SLawrence Mitchell           intFacetsToPatchCell[2*(ifoff + ifn)] = support[0];
111544b625f7SLawrence Mitchell           intFacetsToPatchCell[2*(ifoff + ifn) + 1] = support[1];
11160e126c0bSLawrence Mitchell           intFacetsArray[ifoff + ifn++] = point;
11170e126c0bSLawrence Mitchell         } else {
11180e126c0bSLawrence Mitchell           extFacetsArray[efoff + efn++] = point;
11190e126c0bSLawrence Mitchell         }
11200e126c0bSLawrence Mitchell       }
11215f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr);
11225f824522SMatthew G. Knepley       if (pdof)                            {pointsArray[off + n++] = point;}
11235f824522SMatthew G. Knepley       if (point >= cStart && point < cEnd) {cellsArray[coff + cn++] = point;}
11241b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
11254bbf5ea8SMatthew G. Knepley     }
11260e126c0bSLawrence 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);
11270e126c0bSLawrence 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);
11285f824522SMatthew 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);
11295f824522SMatthew 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);
1130eb62eeaaSLawrence Mitchell 
1131eb62eeaaSLawrence Mitchell     for (ifn = 0; ifn < ifdof; ifn++) {
113244b625f7SLawrence Mitchell       PetscInt cell0 = intFacetsToPatchCell[2*(ifoff + ifn)];
113344b625f7SLawrence Mitchell       PetscInt cell1 = intFacetsToPatchCell[2*(ifoff + ifn) + 1];
1134eb62eeaaSLawrence Mitchell       PetscBool found0 = PETSC_FALSE, found1 = PETSC_FALSE;
1135eb62eeaaSLawrence Mitchell       for (n = 0; n < cdof; n++) {
11367c54fef0SLawrence Mitchell         if (!found0 && cell0 == cellsArray[coff + n]) {
1137c3faab33SLawrence Mitchell           intFacetsToPatchCell[2*(ifoff + ifn)] = n;
1138eb62eeaaSLawrence Mitchell           found0 = PETSC_TRUE;
1139eb62eeaaSLawrence Mitchell         }
11407c54fef0SLawrence Mitchell         if (!found1 && cell1 == cellsArray[coff + n]) {
1141c3faab33SLawrence Mitchell           intFacetsToPatchCell[2*(ifoff + ifn) + 1] = n;
114280fc4459SLawrence Mitchell           found1 = PETSC_TRUE;
1143eb62eeaaSLawrence Mitchell         }
1144eb62eeaaSLawrence Mitchell         if (found0 && found1) break;
1145eb62eeaaSLawrence Mitchell       }
114644b625f7SLawrence Mitchell       if (!(found0 && found1)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Didn't manage to find local point numbers for facet support");
1147eb62eeaaSLawrence Mitchell     }
11484bbf5ea8SMatthew G. Knepley   }
11491b68eb51SMatthew G. Knepley   ierr = PetscHSetIDestroy(&ht);CHKERRQ(ierr);
11501b68eb51SMatthew G. Knepley   ierr = PetscHSetIDestroy(&cht);CHKERRQ(ierr);
11514bbf5ea8SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
11525f824522SMatthew G. Knepley 
11535f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numCells,  cellsArray,  PETSC_OWN_POINTER, &patch->cells);CHKERRQ(ierr);
11545f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->cells,  "Patch Cells");CHKERRQ(ierr);
11555f824522SMatthew G. Knepley   if (patch->viewCells) {
11565f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->cellCounts, patch->viewerCells, patch->formatCells);CHKERRQ(ierr);
11575f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->cells,      patch->viewerCells, patch->formatCells);CHKERRQ(ierr);
11585f824522SMatthew G. Knepley   }
11590e126c0bSLawrence Mitchell   ierr = ISCreateGeneral(PETSC_COMM_SELF, numIntFacets,  intFacetsArray,  PETSC_OWN_POINTER, &patch->intFacets);CHKERRQ(ierr);
11600e126c0bSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->intFacets,  "Patch Interior Facets");CHKERRQ(ierr);
1161eb62eeaaSLawrence Mitchell   ierr = ISCreateGeneral(PETSC_COMM_SELF, 2*numIntFacets, intFacetsToPatchCell, PETSC_OWN_POINTER, &patch->intFacetsToPatchCell);CHKERRQ(ierr);
1162eb62eeaaSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->intFacetsToPatchCell,  "Patch Interior Facets local support");CHKERRQ(ierr);
11630e126c0bSLawrence Mitchell   if (patch->viewIntFacets) {
1164fbb82ab5SLawrence Mitchell     ierr = ObjectView((PetscObject) patch->intFacetCounts,       patch->viewerIntFacets, patch->formatIntFacets);CHKERRQ(ierr);
11650e126c0bSLawrence Mitchell     ierr = ObjectView((PetscObject) patch->intFacets,            patch->viewerIntFacets, patch->formatIntFacets);CHKERRQ(ierr);
1166eb62eeaaSLawrence Mitchell     ierr = ObjectView((PetscObject) patch->intFacetsToPatchCell, patch->viewerIntFacets, patch->formatIntFacets);CHKERRQ(ierr);
11670e126c0bSLawrence Mitchell   }
11680e126c0bSLawrence Mitchell   ierr = ISCreateGeneral(PETSC_COMM_SELF, numExtFacets,  extFacetsArray,  PETSC_OWN_POINTER, &patch->extFacets);CHKERRQ(ierr);
11690e126c0bSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->extFacets,  "Patch Exterior Facets");CHKERRQ(ierr);
11700e126c0bSLawrence Mitchell   if (patch->viewExtFacets) {
1171fbb82ab5SLawrence Mitchell     ierr = ObjectView((PetscObject) patch->extFacetCounts, patch->viewerExtFacets, patch->formatExtFacets);CHKERRQ(ierr);
11720e126c0bSLawrence Mitchell     ierr = ObjectView((PetscObject) patch->extFacets,      patch->viewerExtFacets, patch->formatExtFacets);CHKERRQ(ierr);
11730e126c0bSLawrence Mitchell   }
11745f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints, pointsArray, PETSC_OWN_POINTER, &patch->points);CHKERRQ(ierr);
11755f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->points, "Patch Points");CHKERRQ(ierr);
11765f824522SMatthew G. Knepley   if (patch->viewPoints) {
11775f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->pointCounts, patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr);
11785f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->points,      patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr);
11795f824522SMatthew G. Knepley   }
1180*b6bb21d1SLawrence Mitchell   ierr = DMDestroy(&dm);CHKERRQ(ierr);
11814bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
11824bbf5ea8SMatthew G. Knepley }
11834bbf5ea8SMatthew G. Knepley 
11844bbf5ea8SMatthew G. Knepley /*
11854bbf5ea8SMatthew G. Knepley  * PCPatchCreateCellPatchDiscretisationInfo - Build the dof maps for cell patches
11864bbf5ea8SMatthew G. Knepley  *
11874bbf5ea8SMatthew G. Knepley  * Input Parameters:
11884bbf5ea8SMatthew G. Knepley  * + dm - The DMPlex object defining the mesh
11894bbf5ea8SMatthew G. Knepley  * . cellCounts - Section with counts of cells around each vertex
11904bbf5ea8SMatthew G. Knepley  * . cells - IS of the cell point indices of cells in each patch
11914bbf5ea8SMatthew G. Knepley  * . cellNumbering - Section mapping plex cell points to Firedrake cell indices.
11924bbf5ea8SMatthew G. Knepley  * . nodesPerCell - number of nodes per cell.
11934bbf5ea8SMatthew G. Knepley  * - cellNodeMap - map from cells to node indices (nodesPerCell * numCells)
11944bbf5ea8SMatthew G. Knepley  *
11954bbf5ea8SMatthew G. Knepley  * Output Parameters:
11965f824522SMatthew G. Knepley  * + dofs - IS of local dof numbers of each cell in the patch, where local is a patch local numbering
11974bbf5ea8SMatthew G. Knepley  * . gtolCounts - Section with counts of dofs per cell patch
11984bbf5ea8SMatthew G. Knepley  * - gtol - IS mapping from global dofs to local dofs for each patch.
11994bbf5ea8SMatthew G. Knepley  */
12004bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatchDiscretisationInfo(PC pc)
12014bbf5ea8SMatthew G. Knepley {
12024bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch           = (PC_PATCH *) pc->data;
12034bbf5ea8SMatthew G. Knepley   PetscSection    cellCounts      = patch->cellCounts;
12045f824522SMatthew G. Knepley   PetscSection    pointCounts     = patch->pointCounts;
12050904074fSPatrick Farrell   PetscSection    gtolCounts, gtolCountsWithArtificial = NULL, gtolCountsWithAll = NULL;
12064bbf5ea8SMatthew G. Knepley   IS              cells           = patch->cells;
12075f824522SMatthew G. Knepley   IS              points          = patch->points;
12084bbf5ea8SMatthew G. Knepley   PetscSection    cellNumbering   = patch->cellNumbering;
12095f824522SMatthew G. Knepley   PetscInt        Nf              = patch->nsubspaces;
12105f824522SMatthew G. Knepley   PetscInt        numCells, numPoints;
12114bbf5ea8SMatthew G. Knepley   PetscInt        numDofs;
12120904074fSPatrick Farrell   PetscInt        numGlobalDofs, numGlobalDofsWithArtificial, numGlobalDofsWithAll;
12134bbf5ea8SMatthew G. Knepley   PetscInt        totalDofsPerCell = patch->totalDofsPerCell;
12144bbf5ea8SMatthew G. Knepley   PetscInt        vStart, vEnd, v;
12155f824522SMatthew G. Knepley   const PetscInt *cellsArray, *pointsArray;
12164bbf5ea8SMatthew G. Knepley   PetscInt       *newCellsArray   = NULL;
12174bbf5ea8SMatthew G. Knepley   PetscInt       *dofsArray       = NULL;
1218c2e6f3c0SFlorian Wechsung   PetscInt       *dofsArrayWithArtificial = NULL;
12190904074fSPatrick Farrell   PetscInt       *dofsArrayWithAll = NULL;
12205f824522SMatthew G. Knepley   PetscInt       *offsArray       = NULL;
1221c2e6f3c0SFlorian Wechsung   PetscInt       *offsArrayWithArtificial = NULL;
12220904074fSPatrick Farrell   PetscInt       *offsArrayWithAll = NULL;
12234bbf5ea8SMatthew G. Knepley   PetscInt       *asmArray        = NULL;
1224c2e6f3c0SFlorian Wechsung   PetscInt       *asmArrayWithArtificial = NULL;
12250904074fSPatrick Farrell   PetscInt       *asmArrayWithAll = NULL;
12264bbf5ea8SMatthew G. Knepley   PetscInt       *globalDofsArray = NULL;
1227c2e6f3c0SFlorian Wechsung   PetscInt       *globalDofsArrayWithArtificial = NULL;
12280904074fSPatrick Farrell   PetscInt       *globalDofsArrayWithAll = NULL;
12294bbf5ea8SMatthew G. Knepley   PetscInt        globalIndex     = 0;
12304bbf5ea8SMatthew G. Knepley   PetscInt        key             = 0;
12314bbf5ea8SMatthew G. Knepley   PetscInt        asmKey          = 0;
1232*b6bb21d1SLawrence Mitchell   DM              dm              = NULL, plex;
1233557beb66SLawrence Mitchell   const PetscInt *bcNodes         = NULL;
12341b68eb51SMatthew G. Knepley   PetscHMapI      ht;
1235c2e6f3c0SFlorian Wechsung   PetscHMapI      htWithArtificial;
12360904074fSPatrick Farrell   PetscHMapI      htWithAll;
12371b68eb51SMatthew G. Knepley   PetscHSetI      globalBcs;
1238557beb66SLawrence Mitchell   PetscInt        numBcs;
12391b68eb51SMatthew G. Knepley   PetscHSetI      ownedpts, seenpts, owneddofs, seendofs, artificialbcs;
1240cda239d9SMatthew G. Knepley   PetscInt        pStart, pEnd, p, i;
124110534d48SPatrick Farrell   char            option[PETSC_MAX_PATH_LEN];
124239fd2e8aSPatrick Farrell   PetscBool       isNonlinear;
12434bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
12444bbf5ea8SMatthew G. Knepley 
12454bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1246557beb66SLawrence Mitchell 
1247557beb66SLawrence Mitchell   ierr = PCGetDM(pc, &dm); CHKERRQ(ierr);
1248*b6bb21d1SLawrence Mitchell   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
1249*b6bb21d1SLawrence Mitchell   dm = plex;
12504bbf5ea8SMatthew G. Knepley   /* dofcounts section is cellcounts section * dofPerCell */
12514bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr);
12525f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(patch->pointCounts, &numPoints);CHKERRQ(ierr);
12534bbf5ea8SMatthew G. Knepley   numDofs = numCells * totalDofsPerCell;
12544bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numDofs, &dofsArray);CHKERRQ(ierr);
12555f824522SMatthew G. Knepley   ierr = PetscMalloc1(numPoints*Nf, &offsArray);CHKERRQ(ierr);
12564bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numDofs, &asmArray);CHKERRQ(ierr);
12574bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numCells, &newCellsArray);CHKERRQ(ierr);
12584bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(cellCounts, &vStart, &vEnd);CHKERRQ(ierr);
12594bbf5ea8SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCounts);CHKERRQ(ierr);
12604bbf5ea8SMatthew G. Knepley   gtolCounts = patch->gtolCounts;
12614bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetChart(gtolCounts, vStart, vEnd);CHKERRQ(ierr);
12625f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->gtolCounts, "Patch Global Index Section");CHKERRQ(ierr);
12634bbf5ea8SMatthew G. Knepley 
1264*b6bb21d1SLawrence Mitchell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1265f0dcb611SKarl Rupp     ierr = PetscMalloc1(numPoints*Nf, &offsArrayWithArtificial);CHKERRQ(ierr);
1266c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numDofs, &asmArrayWithArtificial);CHKERRQ(ierr);
1267c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numDofs, &dofsArrayWithArtificial);CHKERRQ(ierr);
1268c2e6f3c0SFlorian Wechsung     ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithArtificial);CHKERRQ(ierr);
1269c2e6f3c0SFlorian Wechsung     gtolCountsWithArtificial = patch->gtolCountsWithArtificial;
1270c2e6f3c0SFlorian Wechsung     ierr = PetscSectionSetChart(gtolCountsWithArtificial, vStart, vEnd);CHKERRQ(ierr);
1271c2e6f3c0SFlorian Wechsung     ierr = PetscObjectSetName((PetscObject) patch->gtolCountsWithArtificial, "Patch Global Index Section Including Artificial BCs");CHKERRQ(ierr);
1272c2e6f3c0SFlorian Wechsung   }
1273c2e6f3c0SFlorian Wechsung 
12740904074fSPatrick Farrell   isNonlinear = patch->isNonlinear;
1275*b6bb21d1SLawrence Mitchell   if (isNonlinear) {
12760904074fSPatrick Farrell     ierr = PetscMalloc1(numPoints*Nf, &offsArrayWithAll);CHKERRQ(ierr);
12770904074fSPatrick Farrell     ierr = PetscMalloc1(numDofs, &asmArrayWithAll);CHKERRQ(ierr);
12780904074fSPatrick Farrell     ierr = PetscMalloc1(numDofs, &dofsArrayWithAll);CHKERRQ(ierr);
12790904074fSPatrick Farrell     ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithAll);CHKERRQ(ierr);
12800904074fSPatrick Farrell     gtolCountsWithAll = patch->gtolCountsWithAll;
12810904074fSPatrick Farrell     ierr = PetscSectionSetChart(gtolCountsWithAll, vStart, vEnd);CHKERRQ(ierr);
12820904074fSPatrick Farrell     ierr = PetscObjectSetName((PetscObject) patch->gtolCountsWithAll, "Patch Global Index Section Including All BCs");CHKERRQ(ierr);
12830904074fSPatrick Farrell   }
12840904074fSPatrick Farrell 
1285557beb66SLawrence Mitchell   /* Outside the patch loop, get the dofs that are globally-enforced Dirichlet
1286557beb66SLawrence Mitchell    conditions */
12871b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&globalBcs);CHKERRQ(ierr);
1288557beb66SLawrence Mitchell   ierr = ISGetIndices(patch->ghostBcNodes, &bcNodes); CHKERRQ(ierr);
1289557beb66SLawrence Mitchell   ierr = ISGetSize(patch->ghostBcNodes, &numBcs); CHKERRQ(ierr);
1290cda239d9SMatthew G. Knepley   for (i = 0; i < numBcs; ++i) {
12911b68eb51SMatthew G. Knepley     ierr = PetscHSetIAdd(globalBcs, bcNodes[i]);CHKERRQ(ierr); /* these are already in concatenated numbering */
1292557beb66SLawrence Mitchell   }
1293557beb66SLawrence Mitchell   ierr = ISRestoreIndices(patch->ghostBcNodes, &bcNodes); CHKERRQ(ierr);
1294557beb66SLawrence Mitchell   ierr = ISDestroy(&patch->ghostBcNodes); CHKERRQ(ierr); /* memory optimisation */
1295557beb66SLawrence Mitchell 
1296557beb66SLawrence Mitchell   /* Hash tables for artificial BC construction */
12971b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&ownedpts);CHKERRQ(ierr);
12981b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&seenpts);CHKERRQ(ierr);
12991b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&owneddofs);CHKERRQ(ierr);
13001b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&seendofs);CHKERRQ(ierr);
13011b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&artificialbcs);CHKERRQ(ierr);
1302557beb66SLawrence Mitchell 
13034bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(cells, &cellsArray);CHKERRQ(ierr);
13045f824522SMatthew G. Knepley   ierr = ISGetIndices(points, &pointsArray);CHKERRQ(ierr);
13051b68eb51SMatthew G. Knepley   ierr = PetscHMapICreate(&ht);CHKERRQ(ierr);
1306c2e6f3c0SFlorian Wechsung   ierr = PetscHMapICreate(&htWithArtificial);CHKERRQ(ierr);
13070904074fSPatrick Farrell   ierr = PetscHMapICreate(&htWithAll);CHKERRQ(ierr);
13084bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
13094bbf5ea8SMatthew G. Knepley     PetscInt localIndex = 0;
1310c2e6f3c0SFlorian Wechsung     PetscInt localIndexWithArtificial = 0;
13110904074fSPatrick Farrell     PetscInt localIndexWithAll = 0;
13124bbf5ea8SMatthew G. Knepley     PetscInt dof, off, i, j, k, l;
13134bbf5ea8SMatthew G. Knepley 
13141b68eb51SMatthew G. Knepley     ierr = PetscHMapIClear(ht);CHKERRQ(ierr);
1315c2e6f3c0SFlorian Wechsung     ierr = PetscHMapIClear(htWithArtificial);CHKERRQ(ierr);
13160904074fSPatrick Farrell     ierr = PetscHMapIClear(htWithAll);CHKERRQ(ierr);
13174bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr);
13184bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr);
13194bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
13204bbf5ea8SMatthew G. Knepley 
1321557beb66SLawrence Mitchell     /* Calculate the global numbers of the artificial BC dofs here first */
1322557beb66SLawrence Mitchell     ierr = patch->patchconstructop((void*)patch, dm, v, ownedpts); CHKERRQ(ierr);
1323557beb66SLawrence Mitchell     ierr = PCPatchCompleteCellPatch(pc, ownedpts, seenpts); CHKERRQ(ierr);
1324e4c66b91SPatrick Farrell     ierr = PCPatchGetPointDofs(pc, ownedpts, owneddofs, v, &patch->subspaces_to_exclude); CHKERRQ(ierr);
1325e4c66b91SPatrick Farrell     ierr = PCPatchGetPointDofs(pc, seenpts, seendofs, v, NULL); CHKERRQ(ierr);
1326557beb66SLawrence Mitchell     ierr = PCPatchComputeSetDifference_Private(owneddofs, seendofs, artificialbcs); CHKERRQ(ierr);
13278135ed82SLawrence Mitchell     if (patch->viewPatches) {
13281b68eb51SMatthew G. Knepley       PetscHSetI globalbcdofs;
13291b68eb51SMatthew G. Knepley       PetscHashIter hi;
13308135ed82SLawrence Mitchell       MPI_Comm comm = PetscObjectComm((PetscObject)pc);
13311b68eb51SMatthew G. Knepley 
13321b68eb51SMatthew G. Knepley       ierr = PetscHSetICreate(&globalbcdofs);CHKERRQ(ierr);
13338135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: owned dofs:\n", v); CHKERRQ(ierr);
13341b68eb51SMatthew G. Knepley       PetscHashIterBegin(owneddofs, hi);
13351b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(owneddofs, hi)) {
13368135ed82SLawrence Mitchell         PetscInt globalDof;
13378135ed82SLawrence Mitchell 
13381b68eb51SMatthew G. Knepley         PetscHashIterGetKey(owneddofs, hi, globalDof);
13391b68eb51SMatthew G. Knepley         PetscHashIterNext(owneddofs, hi);
13408135ed82SLawrence Mitchell         ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
13418135ed82SLawrence Mitchell       }
13428135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n"); CHKERRQ(ierr);
13438135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: seen dofs:\n", v); CHKERRQ(ierr);
13441b68eb51SMatthew G. Knepley       PetscHashIterBegin(seendofs, hi);
13451b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(seendofs, hi)) {
13468135ed82SLawrence Mitchell         PetscInt globalDof;
13478135ed82SLawrence Mitchell         PetscBool flg;
13488135ed82SLawrence Mitchell 
13491b68eb51SMatthew G. Knepley         PetscHashIterGetKey(seendofs, hi, globalDof);
13501b68eb51SMatthew G. Knepley         PetscHashIterNext(seendofs, hi);
13518135ed82SLawrence Mitchell         ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
13528135ed82SLawrence Mitchell 
13531b68eb51SMatthew G. Knepley         ierr = PetscHSetIHas(globalBcs, globalDof, &flg);CHKERRQ(ierr);
13541b68eb51SMatthew G. Knepley         if (flg) {ierr = PetscHSetIAdd(globalbcdofs, globalDof);CHKERRQ(ierr);}
13558135ed82SLawrence Mitchell       }
13568135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n"); CHKERRQ(ierr);
13578135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: global BCs:\n", v); CHKERRQ(ierr);
13581b68eb51SMatthew G. Knepley       ierr = PetscHSetIGetSize(globalbcdofs, &numBcs);CHKERRQ(ierr);
13598135ed82SLawrence Mitchell       if (numBcs > 0) {
13601b68eb51SMatthew G. Knepley         PetscHashIterBegin(globalbcdofs, hi);
13611b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(globalbcdofs, hi)) {
13628135ed82SLawrence Mitchell           PetscInt globalDof;
13631b68eb51SMatthew G. Knepley           PetscHashIterGetKey(globalbcdofs, hi, globalDof);
13641b68eb51SMatthew G. Knepley           PetscHashIterNext(globalbcdofs, hi);
13658135ed82SLawrence Mitchell           ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof);CHKERRQ(ierr);
13668135ed82SLawrence Mitchell         }
13678135ed82SLawrence Mitchell       }
13688135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n");CHKERRQ(ierr);
13698135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: artificial BCs:\n", v);CHKERRQ(ierr);
13701b68eb51SMatthew G. Knepley       ierr = PetscHSetIGetSize(artificialbcs, &numBcs);CHKERRQ(ierr);
13718135ed82SLawrence Mitchell       if (numBcs > 0) {
13721b68eb51SMatthew G. Knepley         PetscHashIterBegin(artificialbcs, hi);
13731b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(artificialbcs, hi)) {
13748135ed82SLawrence Mitchell           PetscInt globalDof;
13751b68eb51SMatthew G. Knepley           PetscHashIterGetKey(artificialbcs, hi, globalDof);
13761b68eb51SMatthew G. Knepley           PetscHashIterNext(artificialbcs, hi);
13778135ed82SLawrence Mitchell           ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
13788135ed82SLawrence Mitchell         }
13798135ed82SLawrence Mitchell       }
13808135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n\n"); CHKERRQ(ierr);
13811b68eb51SMatthew G. Knepley       ierr = PetscHSetIDestroy(&globalbcdofs);CHKERRQ(ierr);
13828135ed82SLawrence Mitchell     }
13834bbf5ea8SMatthew G. Knepley    for (k = 0; k < patch->nsubspaces; ++k) {
13844bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
13854bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
13864bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
13874bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
13884bbf5ea8SMatthew G. Knepley 
13894bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
13904bbf5ea8SMatthew G. Knepley         /* Walk over the cells in this patch. */
13914bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
13925f824522SMatthew G. Knepley         PetscInt       cell = c;
13934bbf5ea8SMatthew G. Knepley 
13945f824522SMatthew G. Knepley         /* TODO Change this to an IS */
13955f824522SMatthew G. Knepley         if (cellNumbering) {
13964bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetDof(cellNumbering, c, &cell);CHKERRQ(ierr);
13974bbf5ea8SMatthew G. Knepley           if (cell <= 0) SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_OUTOFRANGE, "Cell %D doesn't appear in cell numbering map", c);
13984bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);
13995f824522SMatthew G. Knepley         }
14004bbf5ea8SMatthew G. Knepley         newCellsArray[i] = cell;
14014bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
14024bbf5ea8SMatthew G. Knepley           /* For each global dof, map it into contiguous local storage. */
14034bbf5ea8SMatthew G. Knepley           const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + subspaceOffset;
14044bbf5ea8SMatthew G. Knepley           /* finally, loop over block size */
14054bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
14061b68eb51SMatthew G. Knepley             PetscInt  localDof;
14071b68eb51SMatthew G. Knepley             PetscBool isGlobalBcDof, isArtificialBcDof;
14084bbf5ea8SMatthew G. Knepley 
1409557beb66SLawrence Mitchell             /* first, check if this is either a globally enforced or locally enforced BC dof */
14101b68eb51SMatthew G. Knepley             ierr = PetscHSetIHas(globalBcs, globalDof + l, &isGlobalBcDof);CHKERRQ(ierr);
14111b68eb51SMatthew G. Knepley             ierr = PetscHSetIHas(artificialbcs, globalDof + l, &isArtificialBcDof);CHKERRQ(ierr);
1412557beb66SLawrence Mitchell 
1413557beb66SLawrence Mitchell             /* if it's either, don't ever give it a local dof number */
14141b68eb51SMatthew G. Knepley             if (isGlobalBcDof || isArtificialBcDof) {
1415c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = -1; /* don't use this in assembly in this patch */
1416557beb66SLawrence Mitchell             } else {
14171b68eb51SMatthew G. Knepley               ierr = PetscHMapIGet(ht, globalDof + l, &localDof);CHKERRQ(ierr);
14184bbf5ea8SMatthew G. Knepley               if (localDof == -1) {
14194bbf5ea8SMatthew G. Knepley                 localDof = localIndex++;
14201b68eb51SMatthew G. Knepley                 ierr = PetscHMapISet(ht, globalDof + l, localDof);CHKERRQ(ierr);
14214bbf5ea8SMatthew G. Knepley               }
14224bbf5ea8SMatthew G. Knepley               if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
14234bbf5ea8SMatthew G. Knepley               /* And store. */
1424c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = localDof;
14254bbf5ea8SMatthew G. Knepley             }
1426c2e6f3c0SFlorian Wechsung 
14270904074fSPatrick Farrell             if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1428c2e6f3c0SFlorian Wechsung               if (isGlobalBcDof) {
1429e047a90bSFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = -1; /* don't use this in assembly in this patch */
1430c2e6f3c0SFlorian Wechsung               } else {
1431c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapIGet(htWithArtificial, globalDof + l, &localDof);CHKERRQ(ierr);
1432c2e6f3c0SFlorian Wechsung                 if (localDof == -1) {
1433c2e6f3c0SFlorian Wechsung                   localDof = localIndexWithArtificial++;
1434c2e6f3c0SFlorian Wechsung                   ierr = PetscHMapISet(htWithArtificial, globalDof + l, localDof);CHKERRQ(ierr);
1435c2e6f3c0SFlorian Wechsung                 }
1436c2e6f3c0SFlorian Wechsung                 if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
1437c2e6f3c0SFlorian Wechsung                 /* And store.*/
1438c2e6f3c0SFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = localDof;
1439c2e6f3c0SFlorian Wechsung               }
1440c2e6f3c0SFlorian Wechsung             }
14410904074fSPatrick Farrell 
14420904074fSPatrick Farrell             if(isNonlinear) {
14430904074fSPatrick Farrell               /* Build the dofmap for the function space with _all_ dofs,
14440904074fSPatrick Farrell                  including those in any kind of boundary condition */
14450904074fSPatrick Farrell               ierr = PetscHMapIGet(htWithAll, globalDof + l, &localDof);CHKERRQ(ierr);
14460904074fSPatrick Farrell               if (localDof == -1) {
14470904074fSPatrick Farrell                 localDof = localIndexWithAll++;
14480904074fSPatrick Farrell                 ierr = PetscHMapISet(htWithAll, globalDof + l, localDof);CHKERRQ(ierr);
14490904074fSPatrick Farrell               }
14500904074fSPatrick Farrell               if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
14510904074fSPatrick Farrell               /* And store.*/
14520904074fSPatrick Farrell               dofsArrayWithAll[globalIndex] = localDof;
14530904074fSPatrick Farrell             }
1454c2e6f3c0SFlorian Wechsung             globalIndex++;
14554bbf5ea8SMatthew G. Knepley           }
14564bbf5ea8SMatthew G. Knepley         }
14574bbf5ea8SMatthew G. Knepley       }
1458557beb66SLawrence Mitchell     }
14594bbf5ea8SMatthew G. Knepley      /*How many local dofs in this patch? */
14600904074fSPatrick Farrell    if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1461c2e6f3c0SFlorian Wechsung      ierr = PetscHMapIGetSize(htWithArtificial, &dof);CHKERRQ(ierr);
1462c2e6f3c0SFlorian Wechsung      ierr = PetscSectionSetDof(gtolCountsWithArtificial, v, dof);CHKERRQ(ierr);
1463c2e6f3c0SFlorian Wechsung    }
14640904074fSPatrick Farrell    if (isNonlinear) {
14650904074fSPatrick Farrell      ierr = PetscHMapIGetSize(htWithAll, &dof);CHKERRQ(ierr);
14660904074fSPatrick Farrell      ierr = PetscSectionSetDof(gtolCountsWithAll, v, dof);CHKERRQ(ierr);
14670904074fSPatrick Farrell    }
14681b68eb51SMatthew G. Knepley     ierr = PetscHMapIGetSize(ht, &dof);CHKERRQ(ierr);
14694bbf5ea8SMatthew G. Knepley     ierr = PetscSectionSetDof(gtolCounts, v, dof);CHKERRQ(ierr);
14704bbf5ea8SMatthew G. Knepley   }
1471*b6bb21d1SLawrence Mitchell 
1472*b6bb21d1SLawrence Mitchell   ierr = DMDestroy(&dm);CHKERRQ(ierr);
14734bbf5ea8SMatthew 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);
14744bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetUp(gtolCounts);CHKERRQ(ierr);
14754bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(gtolCounts, &numGlobalDofs);CHKERRQ(ierr);
14764bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numGlobalDofs, &globalDofsArray);CHKERRQ(ierr);
14774bbf5ea8SMatthew G. Knepley 
14780904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1479c2e6f3c0SFlorian Wechsung     ierr = PetscSectionSetUp(gtolCountsWithArtificial);CHKERRQ(ierr);
1480c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetStorageSize(gtolCountsWithArtificial, &numGlobalDofsWithArtificial);CHKERRQ(ierr);
1481c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numGlobalDofsWithArtificial, &globalDofsArrayWithArtificial);CHKERRQ(ierr);
1482c2e6f3c0SFlorian Wechsung   }
14830904074fSPatrick Farrell   if (isNonlinear) {
14840904074fSPatrick Farrell     ierr = PetscSectionSetUp(gtolCountsWithAll);CHKERRQ(ierr);
14850904074fSPatrick Farrell     ierr = PetscSectionGetStorageSize(gtolCountsWithAll, &numGlobalDofsWithAll);CHKERRQ(ierr);
14860904074fSPatrick Farrell     ierr = PetscMalloc1(numGlobalDofsWithAll, &globalDofsArrayWithAll);CHKERRQ(ierr);
14870904074fSPatrick Farrell   }
14884bbf5ea8SMatthew 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. */
14894bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
14901b68eb51SMatthew G. Knepley     PetscHashIter hi;
14915f824522SMatthew G. Knepley     PetscInt      dof, off, Np, ooff, i, j, k, l;
14924bbf5ea8SMatthew G. Knepley 
14931b68eb51SMatthew G. Knepley     ierr = PetscHMapIClear(ht);CHKERRQ(ierr);
1494c2e6f3c0SFlorian Wechsung     ierr = PetscHMapIClear(htWithArtificial);CHKERRQ(ierr);
14950904074fSPatrick Farrell     ierr = PetscHMapIClear(htWithAll);CHKERRQ(ierr);
14964bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr);
14974bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr);
14985f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(pointCounts, v, &Np);CHKERRQ(ierr);
14995f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(pointCounts, v, &ooff);CHKERRQ(ierr);
15004bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
15014bbf5ea8SMatthew G. Knepley 
15024bbf5ea8SMatthew G. Knepley     for (k = 0; k < patch->nsubspaces; ++k) {
15034bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
15044bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
15054bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
15064bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
1507d490bb3dSLawrence Mitchell       PetscInt        goff;
15084bbf5ea8SMatthew G. Knepley 
15094bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
15104bbf5ea8SMatthew G. Knepley         /* Reconstruct mapping of global-to-local on this patch. */
15114bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
15125f824522SMatthew G. Knepley         PetscInt       cell = c;
15134bbf5ea8SMatthew G. Knepley 
15145f824522SMatthew G. Knepley         if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);}
15154bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
15164bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
15175f824522SMatthew G. Knepley             const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset;
1518c2e6f3c0SFlorian Wechsung             const PetscInt localDof  = dofsArray[key];
15191b68eb51SMatthew G. Knepley             if (localDof >= 0) {ierr = PetscHMapISet(ht, globalDof, localDof);CHKERRQ(ierr);}
15200904074fSPatrick Farrell             if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1521c2e6f3c0SFlorian Wechsung               const PetscInt localDofWithArtificial = dofsArrayWithArtificial[key];
1522c2e6f3c0SFlorian Wechsung               if (localDofWithArtificial >= 0) {
1523c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapISet(htWithArtificial, globalDof, localDofWithArtificial);CHKERRQ(ierr);
1524c2e6f3c0SFlorian Wechsung               }
1525c2e6f3c0SFlorian Wechsung             }
15260904074fSPatrick Farrell             if (isNonlinear) {
15270904074fSPatrick Farrell               const PetscInt localDofWithAll = dofsArrayWithAll[key];
15280904074fSPatrick Farrell               if (localDofWithAll >= 0) {
15290904074fSPatrick Farrell                 ierr = PetscHMapISet(htWithAll, globalDof, localDofWithAll);CHKERRQ(ierr);
15300904074fSPatrick Farrell               }
15310904074fSPatrick Farrell             }
1532c2e6f3c0SFlorian Wechsung             key++;
15334bbf5ea8SMatthew G. Knepley           }
15344bbf5ea8SMatthew G. Knepley         }
15354bbf5ea8SMatthew G. Knepley       }
1536557beb66SLawrence Mitchell 
15374bbf5ea8SMatthew G. Knepley       /* Shove it in the output data structure. */
15384bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetOffset(gtolCounts, v, &goff);CHKERRQ(ierr);
15391b68eb51SMatthew G. Knepley       PetscHashIterBegin(ht, hi);
15401b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(ht, hi)) {
15414bbf5ea8SMatthew G. Knepley         PetscInt globalDof, localDof;
15424bbf5ea8SMatthew G. Knepley 
15431b68eb51SMatthew G. Knepley         PetscHashIterGetKey(ht, hi, globalDof);
15441b68eb51SMatthew G. Knepley         PetscHashIterGetVal(ht, hi, localDof);
15454bbf5ea8SMatthew G. Knepley         if (globalDof >= 0) globalDofsArray[goff + localDof] = globalDof;
15461b68eb51SMatthew G. Knepley         PetscHashIterNext(ht, hi);
15474bbf5ea8SMatthew G. Knepley       }
15485f824522SMatthew G. Knepley 
15490904074fSPatrick Farrell       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1550c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(gtolCountsWithArtificial, v, &goff);CHKERRQ(ierr);
1551c2e6f3c0SFlorian Wechsung         PetscHashIterBegin(htWithArtificial, hi);
1552c2e6f3c0SFlorian Wechsung         while (!PetscHashIterAtEnd(htWithArtificial, hi)) {
1553c2e6f3c0SFlorian Wechsung           PetscInt globalDof, localDof;
1554c2e6f3c0SFlorian Wechsung           PetscHashIterGetKey(htWithArtificial, hi, globalDof);
1555c2e6f3c0SFlorian Wechsung           PetscHashIterGetVal(htWithArtificial, hi, localDof);
1556c2e6f3c0SFlorian Wechsung           if (globalDof >= 0) globalDofsArrayWithArtificial[goff + localDof] = globalDof;
1557c2e6f3c0SFlorian Wechsung           PetscHashIterNext(htWithArtificial, hi);
1558c2e6f3c0SFlorian Wechsung         }
1559c2e6f3c0SFlorian Wechsung       }
15600904074fSPatrick Farrell       if (isNonlinear) {
15610904074fSPatrick Farrell         ierr = PetscSectionGetOffset(gtolCountsWithAll, v, &goff);CHKERRQ(ierr);
15620904074fSPatrick Farrell         PetscHashIterBegin(htWithAll, hi);
15630904074fSPatrick Farrell         while (!PetscHashIterAtEnd(htWithAll, hi)) {
15640904074fSPatrick Farrell           PetscInt globalDof, localDof;
15650904074fSPatrick Farrell           PetscHashIterGetKey(htWithAll, hi, globalDof);
15660904074fSPatrick Farrell           PetscHashIterGetVal(htWithAll, hi, localDof);
15670904074fSPatrick Farrell           if (globalDof >= 0) globalDofsArrayWithAll[goff + localDof] = globalDof;
15680904074fSPatrick Farrell           PetscHashIterNext(htWithAll, hi);
15690904074fSPatrick Farrell         }
15700904074fSPatrick Farrell       }
1571c2e6f3c0SFlorian Wechsung 
15725f824522SMatthew G. Knepley       for (p = 0; p < Np; ++p) {
15735f824522SMatthew G. Knepley         const PetscInt point = pointsArray[ooff + p];
15745f824522SMatthew G. Knepley         PetscInt       globalDof, localDof;
15755f824522SMatthew G. Knepley 
15765f824522SMatthew G. Knepley         ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, point, NULL, &globalDof);CHKERRQ(ierr);
15771b68eb51SMatthew G. Knepley         ierr = PetscHMapIGet(ht, globalDof, &localDof);CHKERRQ(ierr);
15785f824522SMatthew G. Knepley         offsArray[(ooff + p)*Nf + k] = localDof;
15790904074fSPatrick Farrell         if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1580c2e6f3c0SFlorian Wechsung           ierr = PetscHMapIGet(htWithArtificial, globalDof, &localDof);CHKERRQ(ierr);
1581c2e6f3c0SFlorian Wechsung           offsArrayWithArtificial[(ooff + p)*Nf + k] = localDof;
1582c2e6f3c0SFlorian Wechsung         }
15830904074fSPatrick Farrell         if (isNonlinear) {
15840904074fSPatrick Farrell           ierr = PetscHMapIGet(htWithAll, globalDof, &localDof);CHKERRQ(ierr);
15850904074fSPatrick Farrell           offsArrayWithAll[(ooff + p)*Nf + k] = localDof;
15860904074fSPatrick Farrell         }
15875f824522SMatthew G. Knepley       }
15884bbf5ea8SMatthew G. Knepley     }
15894bbf5ea8SMatthew G. Knepley 
15900cd083f8SSatish Balay     ierr = PetscHSetIDestroy(&globalBcs);CHKERRQ(ierr);
15911b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&ownedpts);CHKERRQ(ierr);
15921b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&seenpts);CHKERRQ(ierr);
15931b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&owneddofs);CHKERRQ(ierr);
15941b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&seendofs);CHKERRQ(ierr);
15951b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&artificialbcs);CHKERRQ(ierr);
1596557beb66SLawrence Mitchell 
15974bbf5ea8SMatthew G. Knepley       /* At this point, we have a hash table ht built that maps globalDof -> localDof.
15984bbf5ea8SMatthew G. Knepley      We need to create the dof table laid out cellwise first, then by subspace,
15994bbf5ea8SMatthew G. Knepley      as the assembler assembles cell-wise and we need to stuff the different
16004bbf5ea8SMatthew G. Knepley      contributions of the different function spaces to the right places. So we loop
16014bbf5ea8SMatthew G. Knepley      over cells, then over subspaces. */
16024bbf5ea8SMatthew G. Knepley     if (patch->nsubspaces > 1) { /* for nsubspaces = 1, data we need is already in dofsArray */
16034bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
16044bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
16055f824522SMatthew G. Knepley         PetscInt       cell = c;
16064bbf5ea8SMatthew G. Knepley 
16075f824522SMatthew G. Knepley         if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);}
16084bbf5ea8SMatthew G. Knepley         for (k = 0; k < patch->nsubspaces; ++k) {
16094bbf5ea8SMatthew G. Knepley           const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
16104bbf5ea8SMatthew G. Knepley           PetscInt        nodesPerCell   = patch->nodesPerCell[k];
16114bbf5ea8SMatthew G. Knepley           PetscInt        subspaceOffset = patch->subspaceOffsets[k];
16124bbf5ea8SMatthew G. Knepley           PetscInt        bs             = patch->bs[k];
16134bbf5ea8SMatthew G. Knepley 
16144bbf5ea8SMatthew G. Knepley           for (j = 0; j < nodesPerCell; ++j) {
16154bbf5ea8SMatthew G. Knepley             for (l = 0; l < bs; ++l) {
16165f824522SMatthew G. Knepley               const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset;
16174bbf5ea8SMatthew G. Knepley               PetscInt       localDof;
16184bbf5ea8SMatthew G. Knepley 
16191b68eb51SMatthew G. Knepley               ierr = PetscHMapIGet(ht, globalDof, &localDof);CHKERRQ(ierr);
1620557beb66SLawrence Mitchell               /* If it's not in the hash table, i.e. is a BC dof,
16211b68eb51SMatthew G. Knepley                then the PetscHSetIMap above gives -1, which matches
1622557beb66SLawrence Mitchell                exactly the convention for PETSc's matrix assembly to
1623557beb66SLawrence Mitchell                ignore the dof. So we don't need to do anything here */
1624c2e6f3c0SFlorian Wechsung               asmArray[asmKey] = localDof;
16250904074fSPatrick Farrell               if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1626c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapIGet(htWithArtificial, globalDof, &localDof);CHKERRQ(ierr);
1627c2e6f3c0SFlorian Wechsung                 asmArrayWithArtificial[asmKey] = localDof;
1628c2e6f3c0SFlorian Wechsung               }
16290904074fSPatrick Farrell               if (isNonlinear) {
16300904074fSPatrick Farrell                 ierr = PetscHMapIGet(htWithAll, globalDof, &localDof);CHKERRQ(ierr);
16310904074fSPatrick Farrell                 asmArrayWithAll[asmKey] = localDof;
16320904074fSPatrick Farrell               }
1633c2e6f3c0SFlorian Wechsung               asmKey++;
16344bbf5ea8SMatthew G. Knepley             }
16354bbf5ea8SMatthew G. Knepley           }
16364bbf5ea8SMatthew G. Knepley         }
16374bbf5ea8SMatthew G. Knepley       }
16384bbf5ea8SMatthew G. Knepley     }
16394bbf5ea8SMatthew G. Knepley   }
1640c2e6f3c0SFlorian Wechsung   if (1 == patch->nsubspaces) {
1641c2e6f3c0SFlorian Wechsung     ierr = PetscMemcpy(asmArray, dofsArray, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
16420904074fSPatrick Farrell     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1643c2e6f3c0SFlorian Wechsung       ierr = PetscMemcpy(asmArrayWithArtificial, dofsArrayWithArtificial, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
1644c2e6f3c0SFlorian Wechsung     }
16450904074fSPatrick Farrell     if (isNonlinear) {
16460904074fSPatrick Farrell       ierr = PetscMemcpy(asmArrayWithAll, dofsArrayWithAll, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
16470904074fSPatrick Farrell     }
1648c2e6f3c0SFlorian Wechsung   }
16494bbf5ea8SMatthew G. Knepley 
16501b68eb51SMatthew G. Knepley   ierr = PetscHMapIDestroy(&ht);CHKERRQ(ierr);
1651c2e6f3c0SFlorian Wechsung   ierr = PetscHMapIDestroy(&htWithArtificial);CHKERRQ(ierr);
16520904074fSPatrick Farrell   ierr = PetscHMapIDestroy(&htWithAll);CHKERRQ(ierr);
16534bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(cells, &cellsArray);CHKERRQ(ierr);
16545f824522SMatthew G. Knepley   ierr = ISRestoreIndices(points, &pointsArray);CHKERRQ(ierr);
16554bbf5ea8SMatthew G. Knepley   ierr = PetscFree(dofsArray);CHKERRQ(ierr);
16560904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1657c2e6f3c0SFlorian Wechsung     ierr = PetscFree(dofsArrayWithArtificial);CHKERRQ(ierr);
1658c2e6f3c0SFlorian Wechsung   }
16590904074fSPatrick Farrell   if (isNonlinear) {
16600904074fSPatrick Farrell     ierr = PetscFree(dofsArrayWithAll);CHKERRQ(ierr);
16610904074fSPatrick Farrell   }
16625f824522SMatthew G. Knepley   /* Create placeholder section for map from points to patch dofs */
16635f824522SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->patchSection);CHKERRQ(ierr);
16645f824522SMatthew G. Knepley   ierr = PetscSectionSetNumFields(patch->patchSection, patch->nsubspaces);CHKERRQ(ierr);
16651e5fa6bbSLawrence Mitchell   if (patch->combined) {
16661e5fa6bbSLawrence Mitchell     PetscInt numFields;
16671e5fa6bbSLawrence Mitchell     ierr = PetscSectionGetNumFields(patch->dofSection[0], &numFields);CHKERRQ(ierr);
16681e5fa6bbSLawrence 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);
16695f824522SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->dofSection[0], &pStart, &pEnd);CHKERRQ(ierr);
16705f824522SMatthew G. Knepley     ierr = PetscSectionSetChart(patch->patchSection, pStart, pEnd);CHKERRQ(ierr);
16715f824522SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
16725f824522SMatthew G. Knepley       PetscInt dof, fdof, f;
16735f824522SMatthew G. Knepley 
16745f824522SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->dofSection[0], p, &dof);CHKERRQ(ierr);
16755f824522SMatthew G. Knepley       ierr = PetscSectionSetDof(patch->patchSection, p, dof);CHKERRQ(ierr);
16765f824522SMatthew G. Knepley       for (f = 0; f < patch->nsubspaces; ++f) {
16771e5fa6bbSLawrence Mitchell         ierr = PetscSectionGetFieldDof(patch->dofSection[0], p, f, &fdof);CHKERRQ(ierr);
16785f824522SMatthew G. Knepley         ierr = PetscSectionSetFieldDof(patch->patchSection, p, f, fdof);CHKERRQ(ierr);
16795f824522SMatthew G. Knepley       }
16801e5fa6bbSLawrence Mitchell     }
16811e5fa6bbSLawrence Mitchell   } else {
16821e5fa6bbSLawrence Mitchell     PetscInt pStartf, pEndf, f;
16831e5fa6bbSLawrence Mitchell     pStart = PETSC_MAX_INT;
16841e5fa6bbSLawrence Mitchell     pEnd = PETSC_MIN_INT;
16851e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
16861e5fa6bbSLawrence Mitchell       ierr = PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf);CHKERRQ(ierr);
16871e5fa6bbSLawrence Mitchell       pStart = PetscMin(pStart, pStartf);
16881e5fa6bbSLawrence Mitchell       pEnd = PetscMax(pEnd, pEndf);
16891e5fa6bbSLawrence Mitchell     }
16901e5fa6bbSLawrence Mitchell     ierr = PetscSectionSetChart(patch->patchSection, pStart, pEnd);CHKERRQ(ierr);
16911e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
16921e5fa6bbSLawrence Mitchell       ierr = PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf);CHKERRQ(ierr);
16931e5fa6bbSLawrence Mitchell       for (p = pStartf; p < pEndf; ++p) {
16941e5fa6bbSLawrence Mitchell         PetscInt fdof;
16951e5fa6bbSLawrence Mitchell         ierr = PetscSectionGetDof(patch->dofSection[f], p, &fdof);CHKERRQ(ierr);
16961e5fa6bbSLawrence Mitchell         ierr = PetscSectionAddDof(patch->patchSection, p, fdof);CHKERRQ(ierr);
16971e5fa6bbSLawrence Mitchell         ierr = PetscSectionSetFieldDof(patch->patchSection, p, f, fdof);CHKERRQ(ierr);
1698bdd9e0cdSPatrick Farrell       }
1699bdd9e0cdSPatrick Farrell     }
17005f824522SMatthew G. Knepley   }
17015f824522SMatthew G. Knepley   ierr = PetscSectionSetUp(patch->patchSection);CHKERRQ(ierr);
17025f824522SMatthew G. Knepley   ierr = PetscSectionSetUseFieldOffsets(patch->patchSection, PETSC_TRUE);CHKERRQ(ierr);
17034bbf5ea8SMatthew G. Knepley   /* Replace cell indices with firedrake-numbered ones. */
17044bbf5ea8SMatthew G. Knepley   ierr = ISGeneralSetIndices(cells, numCells, (const PetscInt *) newCellsArray, PETSC_OWN_POINTER);CHKERRQ(ierr);
17054bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofs, globalDofsArray, PETSC_OWN_POINTER, &patch->gtol);CHKERRQ(ierr);
17065f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->gtol, "Global Indices");CHKERRQ(ierr);
170710534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_g2l_view", patch->classname);CHKERRQ(ierr);
170810534d48SPatrick Farrell   ierr = PetscSectionViewFromOptions(patch->gtolCounts, (PetscObject) pc, option);CHKERRQ(ierr);
170910534d48SPatrick Farrell   ierr = ISViewFromOptions(patch->gtol, (PetscObject) pc, option);CHKERRQ(ierr);
17104bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArray, PETSC_OWN_POINTER, &patch->dofs);CHKERRQ(ierr);
17115f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArray, PETSC_OWN_POINTER, &patch->offs);CHKERRQ(ierr);
17120904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1713c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithArtificial, globalDofsArrayWithArtificial, PETSC_OWN_POINTER, &patch->gtolWithArtificial);CHKERRQ(ierr);
1714c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithArtificial, PETSC_OWN_POINTER, &patch->dofsWithArtificial);CHKERRQ(ierr);
1715c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithArtificial, PETSC_OWN_POINTER, &patch->offsWithArtificial);CHKERRQ(ierr);
1716c2e6f3c0SFlorian Wechsung   }
17170904074fSPatrick Farrell   if (isNonlinear) {
17180904074fSPatrick Farrell     ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithAll, globalDofsArrayWithAll, PETSC_OWN_POINTER, &patch->gtolWithAll);CHKERRQ(ierr);
17190904074fSPatrick Farrell     ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithAll, PETSC_OWN_POINTER, &patch->dofsWithAll);CHKERRQ(ierr);
17200904074fSPatrick Farrell     ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithAll, PETSC_OWN_POINTER, &patch->offsWithAll);CHKERRQ(ierr);
17210904074fSPatrick Farrell   }
17224bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
17234bbf5ea8SMatthew G. Knepley }
17244bbf5ea8SMatthew G. Knepley 
1725c2e6f3c0SFlorian Wechsung static PetscErrorCode PCPatchCreateMatrix_Private(PC pc, PetscInt point, Mat *mat, PetscBool withArtificial)
17264bbf5ea8SMatthew G. Knepley {
17274bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
17284bbf5ea8SMatthew G. Knepley   Vec            x, y;
17294bbf5ea8SMatthew G. Knepley   PetscBool      flg;
17304bbf5ea8SMatthew G. Knepley   PetscInt       csize, rsize;
17314bbf5ea8SMatthew G. Knepley   const char    *prefix = NULL;
17324bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
17334bbf5ea8SMatthew G. Knepley 
17344bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1735c2e6f3c0SFlorian Wechsung   if(withArtificial) {
1736e047a90bSFlorian Wechsung     /* would be nice if we could create a rectangular matrix of size numDofsWithArtificial x numDofs here */
17371202d238SPatrick Farrell     x = patch->patchRHSWithArtificial[point];
17381202d238SPatrick Farrell     y = patch->patchRHSWithArtificial[point];
1739ff201f6aSFlorian Wechsung   } else {
17401202d238SPatrick Farrell     x = patch->patchRHS[point];
17411202d238SPatrick Farrell     y = patch->patchUpdate[point];
1742c2e6f3c0SFlorian Wechsung   }
1743c2e6f3c0SFlorian Wechsung 
17444bbf5ea8SMatthew G. Knepley   ierr = VecGetSize(x, &csize);CHKERRQ(ierr);
17454bbf5ea8SMatthew G. Knepley   ierr = VecGetSize(y, &rsize);CHKERRQ(ierr);
17464bbf5ea8SMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, mat);CHKERRQ(ierr);
17474bbf5ea8SMatthew G. Knepley   ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr);
17484bbf5ea8SMatthew G. Knepley   ierr = MatSetOptionsPrefix(*mat, prefix);CHKERRQ(ierr);
17495f824522SMatthew G. Knepley   ierr = MatAppendOptionsPrefix(*mat, "pc_patch_sub_");CHKERRQ(ierr);
17504bbf5ea8SMatthew G. Knepley   if (patch->sub_mat_type)       {ierr = MatSetType(*mat, patch->sub_mat_type);CHKERRQ(ierr);}
17517974b488SMatthew G. Knepley   else if (!patch->sub_mat_type) {ierr = MatSetType(*mat, MATDENSE);CHKERRQ(ierr);}
17524bbf5ea8SMatthew G. Knepley   ierr = MatSetSizes(*mat, rsize, csize, rsize, csize);CHKERRQ(ierr);
17534bbf5ea8SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) *mat, MATDENSE, &flg);CHKERRQ(ierr);
17544bbf5ea8SMatthew G. Knepley   if (!flg) {ierr = PetscObjectTypeCompare((PetscObject)*mat, MATSEQDENSE, &flg);CHKERRQ(ierr);}
17554bbf5ea8SMatthew G. Knepley   /* Sparse patch matrices */
17564bbf5ea8SMatthew G. Knepley   if (!flg) {
17574bbf5ea8SMatthew G. Knepley     PetscBT         bt;
17584bbf5ea8SMatthew G. Knepley     PetscInt       *dnnz      = NULL;
17594bbf5ea8SMatthew G. Knepley     const PetscInt *dofsArray = NULL;
17604bbf5ea8SMatthew G. Knepley     PetscInt        pStart, pEnd, ncell, offset, c, i, j;
17614bbf5ea8SMatthew G. Knepley 
1762c2e6f3c0SFlorian Wechsung     if(withArtificial) {
1763c2e6f3c0SFlorian Wechsung       ierr = ISGetIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1764ff201f6aSFlorian Wechsung     } else {
17654bbf5ea8SMatthew G. Knepley       ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1766c2e6f3c0SFlorian Wechsung     }
17674bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
17684bbf5ea8SMatthew G. Knepley     point += pStart;
17694bbf5ea8SMatthew G. Knepley     if (point >= pEnd) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %D not in [%D, %D)\n", point, pStart, pEnd);CHKERRQ(ierr);
17704bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
17714bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
17724bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventBegin(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr);
1773b2866507SPatrick Farrell     /* A PetscBT uses N^2 bits to store the sparsity pattern on a
17744bbf5ea8SMatthew G. Knepley      * patch. This is probably OK if the patches are not too big,
1775b2866507SPatrick Farrell      * but uses too much memory. We therefore switch based on rsize. */
1776b2866507SPatrick Farrell     if (rsize < 3000) { /* FIXME: I picked this switch value out of my hat */
1777d63cebbaSPatrick Farrell       PetscScalar *zeroes;
1778d63cebbaSPatrick Farrell       PetscInt rows;
1779d63cebbaSPatrick Farrell 
1780b2866507SPatrick Farrell       ierr = PetscCalloc1(rsize, &dnnz);CHKERRQ(ierr);
17814bbf5ea8SMatthew G. Knepley       ierr = PetscBTCreate(rsize*rsize, &bt);CHKERRQ(ierr);
17824bbf5ea8SMatthew G. Knepley       for (c = 0; c < ncell; ++c) {
17834bbf5ea8SMatthew G. Knepley         const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell;
17844bbf5ea8SMatthew G. Knepley         for (i = 0; i < patch->totalDofsPerCell; ++i) {
17854bbf5ea8SMatthew G. Knepley           const PetscInt row = idx[i];
1786557beb66SLawrence Mitchell           if (row < 0) continue;
17874bbf5ea8SMatthew G. Knepley           for (j = 0; j < patch->totalDofsPerCell; ++j) {
17884bbf5ea8SMatthew G. Knepley             const PetscInt col = idx[j];
17894bbf5ea8SMatthew G. Knepley             const PetscInt key = row*rsize + col;
1790557beb66SLawrence Mitchell             if (col < 0) continue;
17914bbf5ea8SMatthew G. Knepley             if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
17924bbf5ea8SMatthew G. Knepley           }
17934bbf5ea8SMatthew G. Knepley         }
17944bbf5ea8SMatthew G. Knepley       }
1795d63cebbaSPatrick Farrell 
1796d63cebbaSPatrick Farrell       if (patch->usercomputeopintfacet) {
1797d63cebbaSPatrick Farrell         const PetscInt *intFacetsArray = NULL;
1798d63cebbaSPatrick Farrell         PetscInt        i, numIntFacets, intFacetOffset;
1799d63cebbaSPatrick Farrell         const PetscInt *facetCells = NULL;
1800d63cebbaSPatrick Farrell 
1801d63cebbaSPatrick Farrell         ierr = PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets);CHKERRQ(ierr);
1802d63cebbaSPatrick Farrell         ierr = PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset);CHKERRQ(ierr);
1803d63cebbaSPatrick Farrell         ierr = ISGetIndices(patch->intFacetsToPatchCell, &facetCells);CHKERRQ(ierr);
1804d63cebbaSPatrick Farrell         ierr = ISGetIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
1805d63cebbaSPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
1806d63cebbaSPatrick Farrell           const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0];
1807d63cebbaSPatrick Farrell           const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1];
1808d63cebbaSPatrick Farrell           PetscInt       celli, cellj;
1809d63cebbaSPatrick Farrell 
1810d63cebbaSPatrick Farrell           for (celli = 0; celli < patch->totalDofsPerCell; celli++) {
1811d63cebbaSPatrick Farrell             const PetscInt row = dofsArray[(offset + cell0)*patch->totalDofsPerCell + celli];
1812b5c64f08SPatrick Farrell             if (row < 0) continue;
1813d63cebbaSPatrick Farrell             for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) {
1814d63cebbaSPatrick Farrell               const PetscInt col = dofsArray[(offset + cell1)*patch->totalDofsPerCell + cellj];
1815d63cebbaSPatrick Farrell               const PetscInt key = row*rsize + col;
1816d63cebbaSPatrick Farrell               if (col < 0) continue;
1817d63cebbaSPatrick Farrell               if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
1818d63cebbaSPatrick Farrell             }
1819d63cebbaSPatrick Farrell           }
1820d63cebbaSPatrick Farrell 
1821d63cebbaSPatrick Farrell           for (celli = 0; celli < patch->totalDofsPerCell; celli++) {
1822d63cebbaSPatrick Farrell             const PetscInt row = dofsArray[(offset + cell1)*patch->totalDofsPerCell + celli];
1823b5c64f08SPatrick Farrell             if (row < 0) continue;
1824d63cebbaSPatrick Farrell             for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) {
1825d63cebbaSPatrick Farrell               const PetscInt col = dofsArray[(offset + cell0)*patch->totalDofsPerCell + cellj];
1826d63cebbaSPatrick Farrell               const PetscInt key = row*rsize + col;
1827d63cebbaSPatrick Farrell               if (col < 0) continue;
1828d63cebbaSPatrick Farrell               if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
1829d63cebbaSPatrick Farrell             }
1830d63cebbaSPatrick Farrell           }
1831d63cebbaSPatrick Farrell         }
1832d63cebbaSPatrick Farrell       }
18334bbf5ea8SMatthew G. Knepley       ierr = PetscBTDestroy(&bt);CHKERRQ(ierr);
18344bbf5ea8SMatthew G. Knepley       ierr = MatXAIJSetPreallocation(*mat, 1, dnnz, NULL, NULL, NULL);CHKERRQ(ierr);
18354bbf5ea8SMatthew G. Knepley       ierr = PetscFree(dnnz);CHKERRQ(ierr);
1836d63cebbaSPatrick Farrell 
1837d63cebbaSPatrick Farrell       ierr = PetscCalloc1(patch->totalDofsPerCell*patch->totalDofsPerCell, &zeroes);CHKERRQ(ierr);
1838d63cebbaSPatrick Farrell       for (c = 0; c < ncell; ++c) {
1839d63cebbaSPatrick Farrell         const PetscInt *idx = &dofsArray[(offset + c)*patch->totalDofsPerCell];
1840d63cebbaSPatrick Farrell         ierr = MatSetValues(*mat, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, zeroes, INSERT_VALUES);CHKERRQ(ierr);
1841d63cebbaSPatrick Farrell       }
1842d63cebbaSPatrick Farrell       ierr = MatGetLocalSize(*mat, &rows, NULL);CHKERRQ(ierr);
1843d63cebbaSPatrick Farrell       for (i = 0; i < rows; ++i) {
1844d63cebbaSPatrick Farrell         ierr = MatSetValues(*mat, 1, &i, 1, &i, zeroes, INSERT_VALUES);CHKERRQ(ierr);
1845d63cebbaSPatrick Farrell       }
1846d63cebbaSPatrick Farrell 
1847d63cebbaSPatrick Farrell       if (patch->usercomputeopintfacet) {
1848d63cebbaSPatrick Farrell         const PetscInt *intFacetsArray = NULL;
1849d63cebbaSPatrick Farrell         PetscInt i, numIntFacets, intFacetOffset;
1850d63cebbaSPatrick Farrell         const PetscInt *facetCells = NULL;
1851d63cebbaSPatrick Farrell 
1852d63cebbaSPatrick Farrell         ierr = PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets);CHKERRQ(ierr);
1853d63cebbaSPatrick Farrell         ierr = PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset);CHKERRQ(ierr);
1854d63cebbaSPatrick Farrell         ierr = ISGetIndices(patch->intFacetsToPatchCell, &facetCells);CHKERRQ(ierr);
1855d63cebbaSPatrick Farrell         ierr = ISGetIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
1856d63cebbaSPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
1857d63cebbaSPatrick Farrell           const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0];
1858d63cebbaSPatrick Farrell           const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1];
1859d63cebbaSPatrick Farrell           const PetscInt *cell0idx = &dofsArray[(offset + cell0)*patch->totalDofsPerCell];
1860d63cebbaSPatrick Farrell           const PetscInt *cell1idx = &dofsArray[(offset + cell1)*patch->totalDofsPerCell];
1861d63cebbaSPatrick Farrell           ierr = MatSetValues(*mat, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, zeroes, INSERT_VALUES);CHKERRQ(ierr);
1862d63cebbaSPatrick Farrell           ierr = MatSetValues(*mat, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, zeroes, INSERT_VALUES);CHKERRQ(ierr);
1863d63cebbaSPatrick Farrell         }
1864d63cebbaSPatrick Farrell       }
1865d63cebbaSPatrick Farrell 
1866d63cebbaSPatrick Farrell       ierr = MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1867d63cebbaSPatrick Farrell       ierr = MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1868d63cebbaSPatrick Farrell 
1869d63cebbaSPatrick Farrell       ierr = PetscFree(zeroes);CHKERRQ(ierr);
1870d63cebbaSPatrick Farrell 
1871b2866507SPatrick Farrell     } else { /* rsize too big, use MATPREALLOCATOR */
1872b2866507SPatrick Farrell       Mat preallocator;
1873b2866507SPatrick Farrell       PetscScalar* vals;
1874b2866507SPatrick Farrell 
1875b2866507SPatrick Farrell       ierr = PetscCalloc1(patch->totalDofsPerCell*patch->totalDofsPerCell, &vals);CHKERRQ(ierr);
1876b2866507SPatrick Farrell       ierr = MatCreate(PETSC_COMM_SELF, &preallocator);CHKERRQ(ierr);
1877b2866507SPatrick Farrell       ierr = MatSetType(preallocator, MATPREALLOCATOR);CHKERRQ(ierr);
1878b2866507SPatrick Farrell       ierr = MatSetSizes(preallocator, rsize, rsize, rsize, rsize);CHKERRQ(ierr);
1879b2866507SPatrick Farrell       ierr = MatSetUp(preallocator);CHKERRQ(ierr);
188011bcd083SPatrick Farrell 
1881b2866507SPatrick Farrell       for (c = 0; c < ncell; ++c) {
1882b2866507SPatrick Farrell         const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell;
1883b2866507SPatrick Farrell         ierr = MatSetValues(preallocator, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, vals, INSERT_VALUES);CHKERRQ(ierr);
1884b2866507SPatrick Farrell       }
188511bcd083SPatrick Farrell 
188611bcd083SPatrick Farrell       if (patch->usercomputeopintfacet) {
188711bcd083SPatrick Farrell         const PetscInt *intFacetsArray = NULL;
188811bcd083SPatrick Farrell         PetscInt        i, numIntFacets, intFacetOffset;
188911bcd083SPatrick Farrell         const PetscInt *facetCells = NULL;
189011bcd083SPatrick Farrell 
189111bcd083SPatrick Farrell         ierr = PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets);CHKERRQ(ierr);
189211bcd083SPatrick Farrell         ierr = PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset);CHKERRQ(ierr);
189311bcd083SPatrick Farrell         ierr = ISGetIndices(patch->intFacetsToPatchCell, &facetCells);CHKERRQ(ierr);
189411bcd083SPatrick Farrell         ierr = ISGetIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
189511bcd083SPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
189611bcd083SPatrick Farrell           const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0];
189711bcd083SPatrick Farrell           const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1];
189811bcd083SPatrick Farrell           const PetscInt *cell0idx = &dofsArray[(offset + cell0)*patch->totalDofsPerCell];
189911bcd083SPatrick Farrell           const PetscInt *cell1idx = &dofsArray[(offset + cell1)*patch->totalDofsPerCell];
190011bcd083SPatrick Farrell           ierr = MatSetValues(preallocator, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, vals, INSERT_VALUES);CHKERRQ(ierr);
190111bcd083SPatrick Farrell           ierr = MatSetValues(preallocator, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, vals, INSERT_VALUES);CHKERRQ(ierr);
190211bcd083SPatrick Farrell         }
190311bcd083SPatrick Farrell       }
190411bcd083SPatrick Farrell 
1905b2866507SPatrick Farrell       ierr = PetscFree(vals);CHKERRQ(ierr);
1906b2866507SPatrick Farrell       ierr = MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1907b2866507SPatrick Farrell       ierr = MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1908b2866507SPatrick Farrell       ierr = MatPreallocatorPreallocate(preallocator, PETSC_TRUE, *mat);CHKERRQ(ierr);
1909b2866507SPatrick Farrell       ierr = MatDestroy(&preallocator);CHKERRQ(ierr);
1910b2866507SPatrick Farrell     }
19114bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr);
1912fe117d09SFlorian Wechsung     if(withArtificial) {
1913fe117d09SFlorian Wechsung       ierr = ISRestoreIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1914fe117d09SFlorian Wechsung     } else {
19154bbf5ea8SMatthew G. Knepley       ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
19164bbf5ea8SMatthew G. Knepley     }
1917fe117d09SFlorian Wechsung   }
19184bbf5ea8SMatthew G. Knepley   ierr = MatSetUp(*mat);CHKERRQ(ierr);
19194bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
19204bbf5ea8SMatthew G. Knepley }
19214bbf5ea8SMatthew G. Knepley 
19220904074fSPatrick 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)
192392d50984SMatthew G. Knepley {
192492d50984SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
1925*b6bb21d1SLawrence Mitchell   DM              dm, plex;
192692d50984SMatthew G. Knepley   PetscSection    s;
192792d50984SMatthew G. Knepley   const PetscInt *parray, *oarray;
192892d50984SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
192992d50984SMatthew G. Knepley   PetscErrorCode  ierr;
193092d50984SMatthew G. Knepley 
193192d50984SMatthew G. Knepley   PetscFunctionBegin;
1932fa84ea4cSLawrence Mitchell   if (patch->precomputeElementTensors) SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Precomputing element tensors not implemented with DMPlex compute operator");
193392d50984SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
1934*b6bb21d1SLawrence Mitchell   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
1935*b6bb21d1SLawrence Mitchell   dm = plex;
193692d50984SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
193792d50984SMatthew G. Knepley   /* Set offset into patch */
193892d50984SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->pointCounts, patchNum, &Np);CHKERRQ(ierr);
193992d50984SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->pointCounts, patchNum, &poff);CHKERRQ(ierr);
194092d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->points, &parray);CHKERRQ(ierr);
194192d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->offs,   &oarray);CHKERRQ(ierr);
194292d50984SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
194392d50984SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
194492d50984SMatthew G. Knepley       const PetscInt point = parray[poff+p];
194592d50984SMatthew G. Knepley       PetscInt       dof;
194692d50984SMatthew G. Knepley 
194792d50984SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(patch->patchSection, point, f, &dof);CHKERRQ(ierr);
194892d50984SMatthew G. Knepley       ierr = PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);
194992d50984SMatthew G. Knepley       if (patch->nsubspaces == 1) {ierr = PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);}
195092d50984SMatthew G. Knepley       else                        {ierr = PetscSectionSetOffset(patch->patchSection, point, -1);CHKERRQ(ierr);}
195192d50984SMatthew G. Knepley     }
195292d50984SMatthew G. Knepley   }
195392d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->points, &parray);CHKERRQ(ierr);
195492d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->offs,   &oarray);CHKERRQ(ierr);
195592d50984SMatthew G. Knepley   if (patch->viewSection) {ierr = ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection);CHKERRQ(ierr);}
1956*b6bb21d1SLawrence Mitchell   ierr = DMPlexComputeResidual_Patch_Internal(dm, patch->patchSection, cellIS, 0.0, x, NULL, F, ctx);CHKERRQ(ierr);
1957*b6bb21d1SLawrence Mitchell   ierr = DMDestroy(&dm);CHKERRQ(ierr);
195892d50984SMatthew G. Knepley   PetscFunctionReturn(0);
195992d50984SMatthew G. Knepley }
196092d50984SMatthew G. Knepley 
196192d50984SMatthew G. Knepley PetscErrorCode PCPatchComputeFunction_Internal(PC pc, Vec x, Vec F, PetscInt point)
196292d50984SMatthew G. Knepley {
196392d50984SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
196492d50984SMatthew G. Knepley   const PetscInt *dofsArray;
19650904074fSPatrick Farrell   const PetscInt *dofsArrayWithAll;
196692d50984SMatthew G. Knepley   const PetscInt *cellsArray;
196792d50984SMatthew G. Knepley   PetscInt        ncell, offset, pStart, pEnd;
196892d50984SMatthew G. Knepley   PetscErrorCode  ierr;
196992d50984SMatthew G. Knepley 
197092d50984SMatthew G. Knepley   PetscFunctionBegin;
197192d50984SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
197292d50984SMatthew G. Knepley   if (!patch->usercomputeop) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback\n");
197392d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
19740904074fSPatrick Farrell   ierr = ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
197592d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
197692d50984SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
197792d50984SMatthew G. Knepley 
197892d50984SMatthew G. Knepley   point += pStart;
197992d50984SMatthew G. Knepley   if (point >= pEnd) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %D not in [%D, %D)\n", point, pStart, pEnd);CHKERRQ(ierr);
198092d50984SMatthew G. Knepley 
198192d50984SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
198292d50984SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
198392d50984SMatthew G. Knepley   if (ncell <= 0) {
198492d50984SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
198592d50984SMatthew G. Knepley     PetscFunctionReturn(0);
198692d50984SMatthew G. Knepley   }
198726dc5b63SLawrence Mitchell   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
198892d50984SMatthew G. Knepley   PetscStackPush("PCPatch user callback");
198992d50984SMatthew G. Knepley   /* Cannot reuse the same IS because the geometry info is being cached in it */
199092d50984SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS);CHKERRQ(ierr);
199139fd2e8aSPatrick Farrell   ierr = patch->usercomputef(pc, point, x, F, patch->cellIS, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell,
19920904074fSPatrick Farrell                                                                                             dofsArrayWithAll + offset*patch->totalDofsPerCell,
199339fd2e8aSPatrick Farrell                                                                                             patch->usercomputefctx);CHKERRQ(ierr);
199492d50984SMatthew G. Knepley   PetscStackPop;
199592d50984SMatthew G. Knepley   ierr = ISDestroy(&patch->cellIS);CHKERRQ(ierr);
199692d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
19970904074fSPatrick Farrell   ierr = ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
199892d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
199992d50984SMatthew G. Knepley   if (patch->viewMatrix) {
200092d50984SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
200192d50984SMatthew G. Knepley 
200292d50984SMatthew G. Knepley     ierr = PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch vector for Point %D", point);CHKERRQ(ierr);
200392d50984SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) F, name);CHKERRQ(ierr);
200492d50984SMatthew G. Knepley     ierr = ObjectView((PetscObject) F, patch->viewerMatrix, patch->formatMatrix);CHKERRQ(ierr);
200592d50984SMatthew G. Knepley   }
200692d50984SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
200792d50984SMatthew G. Knepley   PetscFunctionReturn(0);
200892d50984SMatthew G. Knepley }
200992d50984SMatthew G. Knepley 
20100904074fSPatrick 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)
20115f824522SMatthew G. Knepley {
20125f824522SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
2013*b6bb21d1SLawrence Mitchell   DM              dm, plex;
20145f824522SMatthew G. Knepley   PetscSection    s;
20155f824522SMatthew G. Knepley   const PetscInt *parray, *oarray;
20165f824522SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
20175f824522SMatthew G. Knepley   PetscErrorCode  ierr;
20185f824522SMatthew G. Knepley 
20195f824522SMatthew G. Knepley   PetscFunctionBegin;
20205f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
2021*b6bb21d1SLawrence Mitchell   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
2022*b6bb21d1SLawrence Mitchell   dm = plex;
20235f824522SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
20245f824522SMatthew G. Knepley   /* Set offset into patch */
20255f824522SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->pointCounts, patchNum, &Np);CHKERRQ(ierr);
20265f824522SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->pointCounts, patchNum, &poff);CHKERRQ(ierr);
20275f824522SMatthew G. Knepley   ierr = ISGetIndices(patch->points, &parray);CHKERRQ(ierr);
20285f824522SMatthew G. Knepley   ierr = ISGetIndices(patch->offs,   &oarray);CHKERRQ(ierr);
20295f824522SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
20305f824522SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
20315f824522SMatthew G. Knepley       const PetscInt point = parray[poff+p];
20325f824522SMatthew G. Knepley       PetscInt       dof;
20335f824522SMatthew G. Knepley 
20345f824522SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(patch->patchSection, point, f, &dof);CHKERRQ(ierr);
20355f824522SMatthew G. Knepley       ierr = PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);
20365f824522SMatthew G. Knepley       if (patch->nsubspaces == 1) {ierr = PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);}
20375f824522SMatthew G. Knepley       else                        {ierr = PetscSectionSetOffset(patch->patchSection, point, -1);CHKERRQ(ierr);}
20385f824522SMatthew G. Knepley     }
20395f824522SMatthew G. Knepley   }
20405f824522SMatthew G. Knepley   ierr = ISRestoreIndices(patch->points, &parray);CHKERRQ(ierr);
20415f824522SMatthew G. Knepley   ierr = ISRestoreIndices(patch->offs,   &oarray);CHKERRQ(ierr);
20425f824522SMatthew G. Knepley   if (patch->viewSection) {ierr = ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection);CHKERRQ(ierr);}
20435f824522SMatthew G. Knepley   /* TODO Shut off MatViewFromOptions() in MatAssemblyEnd() here */
2044*b6bb21d1SLawrence Mitchell   ierr = DMPlexComputeJacobian_Patch_Internal(dm, patch->patchSection, patch->patchSection, cellIS, 0.0, 0.0, x, NULL, J, J, ctx);CHKERRQ(ierr);
2045*b6bb21d1SLawrence Mitchell   ierr = DMDestroy(&dm);CHKERRQ(ierr);
20465f824522SMatthew G. Knepley   PetscFunctionReturn(0);
20475f824522SMatthew G. Knepley }
20485f824522SMatthew G. Knepley 
2049a685ae26SLawrence Mitchell /* This function zeros mat on entry */
205034d8b122SPatrick Farrell PetscErrorCode PCPatchComputeOperator_Internal(PC pc, Vec x, Mat mat, PetscInt point, PetscBool withArtificial)
20514bbf5ea8SMatthew G. Knepley {
20524bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
20534bbf5ea8SMatthew G. Knepley   const PetscInt *dofsArray;
20540904074fSPatrick Farrell   const PetscInt *dofsArrayWithAll = NULL;
20554bbf5ea8SMatthew G. Knepley   const PetscInt *cellsArray;
2056eb62eeaaSLawrence Mitchell   PetscInt        ncell, offset, pStart, pEnd, numIntFacets, intFacetOffset;
20574d04e9f1SPatrick Farrell   PetscBool       isNonlinear;
20584bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
20594bbf5ea8SMatthew G. Knepley 
20604bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
20614bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
2062debbdec3SPatrick Farrell   isNonlinear = patch->isNonlinear;
20634bbf5ea8SMatthew G. Knepley   if (!patch->usercomputeop) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback\n");
2064c2e6f3c0SFlorian Wechsung   if(withArtificial) {
2065c2e6f3c0SFlorian Wechsung     ierr = ISGetIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
2066c2e6f3c0SFlorian Wechsung   } else {
20674bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
2068c2e6f3c0SFlorian Wechsung   }
20694d04e9f1SPatrick Farrell   if (isNonlinear) {
20700904074fSPatrick Farrell     ierr = ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
20714d04e9f1SPatrick Farrell   }
20724bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
20734bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
20744bbf5ea8SMatthew G. Knepley 
20754bbf5ea8SMatthew G. Knepley   point += pStart;
20764bbf5ea8SMatthew G. Knepley   if (point >= pEnd) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %D not in [%D, %D)\n", point, pStart, pEnd);CHKERRQ(ierr);
20774bbf5ea8SMatthew G. Knepley 
20784bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
20794bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
20804bbf5ea8SMatthew G. Knepley   if (ncell <= 0) {
20814bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
20824bbf5ea8SMatthew G. Knepley     PetscFunctionReturn(0);
20834bbf5ea8SMatthew G. Knepley   }
2084a685ae26SLawrence Mitchell   ierr = MatZeroEntries(mat);CHKERRQ(ierr);
2085fa84ea4cSLawrence Mitchell   if (patch->precomputeElementTensors) {
2086fa84ea4cSLawrence Mitchell     PetscInt           i;
2087fa84ea4cSLawrence Mitchell     PetscInt           ndof = patch->totalDofsPerCell;
2088fa84ea4cSLawrence Mitchell     const PetscScalar *elementTensors;
2089fa84ea4cSLawrence Mitchell 
2090fa84ea4cSLawrence Mitchell     ierr = VecGetArrayRead(patch->cellMats, &elementTensors);CHKERRQ(ierr);
2091fa84ea4cSLawrence Mitchell     for (i = 0; i < ncell; i++) {
2092fa84ea4cSLawrence Mitchell       const PetscInt     cell = cellsArray[i + offset];
2093fa84ea4cSLawrence Mitchell       const PetscInt    *idx  = dofsArray + (offset + i)*ndof;
2094fe988be2SFlorian Wechsung       const PetscScalar *v    = elementTensors + patch->precomputedTensorLocations[cell]*ndof*ndof;
2095fa84ea4cSLawrence Mitchell       ierr = MatSetValues(mat, ndof, idx, ndof, idx, v, ADD_VALUES);CHKERRQ(ierr);
2096fa84ea4cSLawrence Mitchell     }
2097fa84ea4cSLawrence Mitchell     ierr = VecRestoreArrayRead(patch->cellMats, &elementTensors);CHKERRQ(ierr);
2098fa84ea4cSLawrence Mitchell     ierr = MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2099fa84ea4cSLawrence Mitchell     ierr = MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2100fa84ea4cSLawrence Mitchell   } else {
21014bbf5ea8SMatthew G. Knepley     PetscStackPush("PCPatch user callback");
21022aa6f319SMatthew G. Knepley     /* Cannot reuse the same IS because the geometry info is being cached in it */
21032aa6f319SMatthew G. Knepley     ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS);CHKERRQ(ierr);
21040904074fSPatrick 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);
2105fa84ea4cSLawrence Mitchell   }
210659109abcSLawrence Mitchell   if (patch->usercomputeopintfacet) {
2107eb62eeaaSLawrence Mitchell     ierr = PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets);CHKERRQ(ierr);
2108eb62eeaaSLawrence Mitchell     ierr = PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset);CHKERRQ(ierr);
2109eb62eeaaSLawrence Mitchell     if (numIntFacets > 0) {
2110eb62eeaaSLawrence Mitchell       /* For each interior facet, grab the two cells (in local numbering, and concatenate dof numberings for those cells) */
2111eb62eeaaSLawrence Mitchell       PetscInt       *facetDofs = NULL, *facetDofsWithAll = NULL;
2112eb62eeaaSLawrence Mitchell       const PetscInt *intFacetsArray = NULL;
2113eb62eeaaSLawrence Mitchell       PetscInt        idx = 0;
2114eb62eeaaSLawrence Mitchell       PetscInt        i, c, d;
2115de2d1767SPatrick Farrell       PetscInt        fStart;
2116*b6bb21d1SLawrence Mitchell       DM              dm, plex;
2117eb62eeaaSLawrence Mitchell       IS              facetIS = NULL;
2118eb62eeaaSLawrence Mitchell       const PetscInt *facetCells = NULL;
21197a50e09dSPatrick Farrell 
2120eb62eeaaSLawrence Mitchell       ierr = ISGetIndices(patch->intFacetsToPatchCell, &facetCells);CHKERRQ(ierr);
2121eb62eeaaSLawrence Mitchell       ierr = ISGetIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
2122de2d1767SPatrick Farrell       ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
2123*b6bb21d1SLawrence Mitchell       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
2124*b6bb21d1SLawrence Mitchell       dm = plex;
2125de2d1767SPatrick Farrell       ierr = DMPlexGetHeightStratum(dm, 1, &fStart, NULL);CHKERRQ(ierr);
2126eb62eeaaSLawrence Mitchell       /* FIXME: Pull this malloc out. */
2127eb62eeaaSLawrence Mitchell       ierr = PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofs);CHKERRQ(ierr);
2128eb62eeaaSLawrence Mitchell       if (dofsArrayWithAll) {
2129eb62eeaaSLawrence Mitchell         ierr = PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofsWithAll);CHKERRQ(ierr);
2130eb62eeaaSLawrence Mitchell       }
2131f98464cbSLawrence Mitchell       if (patch->precomputeElementTensors) {
2132f98464cbSLawrence Mitchell         PetscInt           nFacetDof = 2*patch->totalDofsPerCell;
2133f98464cbSLawrence Mitchell         const PetscScalar *elementTensors;
2134f98464cbSLawrence Mitchell 
2135f98464cbSLawrence Mitchell         ierr = VecGetArrayRead(patch->intFacetMats, &elementTensors);CHKERRQ(ierr);
2136f98464cbSLawrence Mitchell 
2137f98464cbSLawrence Mitchell         for (i = 0; i < numIntFacets; i++) {
2138f98464cbSLawrence Mitchell           const PetscInt     facet = intFacetsArray[i + intFacetOffset];
2139de2d1767SPatrick Farrell           const PetscScalar *v     = elementTensors + patch->precomputedIntFacetTensorLocations[facet - fStart]*nFacetDof*nFacetDof;
2140f98464cbSLawrence Mitchell           idx = 0;
2141f98464cbSLawrence Mitchell           /*
2142f98464cbSLawrence Mitchell            * 0--1
2143f98464cbSLawrence Mitchell            * |\-|
2144f98464cbSLawrence Mitchell            * |+\|
2145f98464cbSLawrence Mitchell            * 2--3
2146f98464cbSLawrence Mitchell            * [0, 2, 3, 0, 1, 3]
2147f98464cbSLawrence Mitchell            */
2148f98464cbSLawrence Mitchell           for (c = 0; c < 2; c++) {
2149f98464cbSLawrence Mitchell             const PetscInt cell = facetCells[2*(intFacetOffset + i) + c];
2150f98464cbSLawrence Mitchell             for (d = 0; d < patch->totalDofsPerCell; d++) {
2151f98464cbSLawrence Mitchell               facetDofs[idx] = dofsArray[(offset + cell)*patch->totalDofsPerCell + d];
2152f98464cbSLawrence Mitchell               idx++;
2153f98464cbSLawrence Mitchell             }
2154f98464cbSLawrence Mitchell           }
2155f98464cbSLawrence Mitchell           ierr = MatSetValues(mat, nFacetDof, facetDofs, nFacetDof, facetDofs, v, ADD_VALUES);CHKERRQ(ierr);
2156f98464cbSLawrence Mitchell         }
2157f98464cbSLawrence Mitchell         ierr = VecRestoreArrayRead(patch->intFacetMats, &elementTensors);CHKERRQ(ierr);
2158f98464cbSLawrence Mitchell       } else {
2159eb62eeaaSLawrence Mitchell         /*
2160eb62eeaaSLawrence Mitchell          * 0--1
2161eb62eeaaSLawrence Mitchell          * |\-|
2162eb62eeaaSLawrence Mitchell          * |+\|
2163eb62eeaaSLawrence Mitchell          * 2--3
2164eb62eeaaSLawrence Mitchell          * [0, 2, 3, 0, 1, 3]
2165eb62eeaaSLawrence Mitchell          */
2166eb62eeaaSLawrence Mitchell         for (i = 0; i < numIntFacets; i++) {
2167eb62eeaaSLawrence Mitchell           for (c = 0; c < 2; c++) {
2168eb62eeaaSLawrence Mitchell             const PetscInt cell = facetCells[2*(intFacetOffset + i) + c];
2169eb62eeaaSLawrence Mitchell             for (d = 0; d < patch->totalDofsPerCell; d++) {
2170eb62eeaaSLawrence Mitchell               facetDofs[idx] = dofsArray[(offset + cell)*patch->totalDofsPerCell + d];
2171eb62eeaaSLawrence Mitchell               if (dofsArrayWithAll) {
2172eb62eeaaSLawrence Mitchell                 facetDofsWithAll[idx] = dofsArrayWithAll[(offset + cell)*patch->totalDofsPerCell + d];
2173eb62eeaaSLawrence Mitchell               }
2174eb62eeaaSLawrence Mitchell               idx++;
2175eb62eeaaSLawrence Mitchell             }
2176eb62eeaaSLawrence Mitchell           }
2177eb62eeaaSLawrence Mitchell         }
2178eb62eeaaSLawrence Mitchell         ierr = ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray + intFacetOffset, PETSC_USE_POINTER, &facetIS);CHKERRQ(ierr);
2179ec0d443bSLawrence Mitchell         ierr = patch->usercomputeopintfacet(pc, point, x, mat, facetIS, 2*numIntFacets*patch->totalDofsPerCell, facetDofs, facetDofsWithAll, patch->usercomputeopintfacetctx);CHKERRQ(ierr);
2180eb62eeaaSLawrence Mitchell         ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
2181f98464cbSLawrence Mitchell       }
2182eb62eeaaSLawrence Mitchell       ierr = ISRestoreIndices(patch->intFacetsToPatchCell, &facetCells);CHKERRQ(ierr);
2183f98464cbSLawrence Mitchell       ierr = ISRestoreIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
2184eb62eeaaSLawrence Mitchell       ierr = PetscFree(facetDofs);CHKERRQ(ierr);
2185eb62eeaaSLawrence Mitchell       ierr = PetscFree(facetDofsWithAll);CHKERRQ(ierr);
2186*b6bb21d1SLawrence Mitchell       ierr = DMDestroy(&dm);CHKERRQ(ierr);
2187eb62eeaaSLawrence Mitchell     }
218859109abcSLawrence Mitchell   }
21896710cc29SPatrick Farrell 
21906710cc29SPatrick Farrell   ierr = MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
21916710cc29SPatrick Farrell   ierr = MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
21926710cc29SPatrick Farrell 
21934bbf5ea8SMatthew G. Knepley   PetscStackPop;
21942aa6f319SMatthew G. Knepley   ierr = ISDestroy(&patch->cellIS);CHKERRQ(ierr);
21954d04e9f1SPatrick Farrell   if(withArtificial) {
2196c2e6f3c0SFlorian Wechsung     ierr = ISRestoreIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
2197c2e6f3c0SFlorian Wechsung   } else {
21984bbf5ea8SMatthew G. Knepley     ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
2199c2e6f3c0SFlorian Wechsung   }
22004d04e9f1SPatrick Farrell   if (isNonlinear) {
22010904074fSPatrick Farrell     ierr = ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
22024d04e9f1SPatrick Farrell   }
22034bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
22042aa6f319SMatthew G. Knepley   if (patch->viewMatrix) {
22052aa6f319SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
22062aa6f319SMatthew G. Knepley 
22072aa6f319SMatthew G. Knepley     ierr = PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch matrix for Point %D", point);CHKERRQ(ierr);
22082aa6f319SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) mat, name);CHKERRQ(ierr);
22092aa6f319SMatthew G. Knepley     ierr = ObjectView((PetscObject) mat, patch->viewerMatrix, patch->formatMatrix);CHKERRQ(ierr);
22102aa6f319SMatthew G. Knepley   }
22114bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
22124bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
22134bbf5ea8SMatthew G. Knepley }
22144bbf5ea8SMatthew G. Knepley 
2215fa84ea4cSLawrence Mitchell static PetscErrorCode MatSetValues_PCPatch_Private(Mat mat, PetscInt m, const PetscInt idxm[],
2216fa84ea4cSLawrence Mitchell                                                    PetscInt n, const PetscInt idxn[], const PetscScalar *v, InsertMode addv)
2217fa84ea4cSLawrence Mitchell {
2218fa84ea4cSLawrence Mitchell   Vec            data;
2219fa84ea4cSLawrence Mitchell   PetscScalar   *array;
2220fe988be2SFlorian Wechsung   PetscInt       bs, nz, i, j, cell;
2221fa84ea4cSLawrence Mitchell   PetscErrorCode ierr;
2222fa84ea4cSLawrence Mitchell 
2223fa84ea4cSLawrence Mitchell   ierr = MatShellGetContext(mat, &data);CHKERRQ(ierr);
2224fa84ea4cSLawrence Mitchell   ierr = VecGetBlockSize(data, &bs);CHKERRQ(ierr);
2225fa84ea4cSLawrence Mitchell   ierr = VecGetSize(data, &nz);CHKERRQ(ierr);
2226fa84ea4cSLawrence Mitchell   ierr = VecGetArray(data, &array);CHKERRQ(ierr);
2227fa84ea4cSLawrence Mitchell   if (m != n) SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Only for square insertion");
222833cbca70SPatrick Farrell   cell = (PetscInt)(idxm[0]/bs); /* use the fact that this is called once per cell */
2229fa84ea4cSLawrence Mitchell   for (i = 0; i < m; i++) {
2230fa84ea4cSLawrence Mitchell     if (idxm[i] != idxn[i]) SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Row and column indices must match!");
2231fa84ea4cSLawrence Mitchell     for (j = 0; j < n; j++) {
2232fa84ea4cSLawrence Mitchell       const PetscScalar v_ = v[i*bs + j];
2233fa84ea4cSLawrence Mitchell       /* Indexing is special to the data structure we have! */
2234fa84ea4cSLawrence Mitchell       if (addv == INSERT_VALUES) {
2235fe988be2SFlorian Wechsung         array[cell*bs*bs + i*bs + j] = v_;
2236fa84ea4cSLawrence Mitchell       } else {
2237fe988be2SFlorian Wechsung         array[cell*bs*bs + i*bs + j] += v_;
2238fa84ea4cSLawrence Mitchell       }
2239fa84ea4cSLawrence Mitchell     }
2240fa84ea4cSLawrence Mitchell   }
2241fa84ea4cSLawrence Mitchell   ierr = VecRestoreArray(data, &array);CHKERRQ(ierr);
2242fa84ea4cSLawrence Mitchell   PetscFunctionReturn(0);
2243fa84ea4cSLawrence Mitchell }
2244fa84ea4cSLawrence Mitchell 
2245fa84ea4cSLawrence Mitchell static PetscErrorCode PCPatchPrecomputePatchTensors_Private(PC pc)
2246fa84ea4cSLawrence Mitchell {
2247fa84ea4cSLawrence Mitchell   PC_PATCH       *patch = (PC_PATCH *)pc->data;
2248fa84ea4cSLawrence Mitchell   const PetscInt *cellsArray;
2249fa84ea4cSLawrence Mitchell   PetscInt        ncell, offset;
2250fa84ea4cSLawrence Mitchell   const PetscInt *dofMapArray;
2251fa84ea4cSLawrence Mitchell   PetscInt        i, j;
2252fa84ea4cSLawrence Mitchell   IS              dofMap;
2253fa84ea4cSLawrence Mitchell   IS              cellIS;
2254fa84ea4cSLawrence Mitchell   const PetscInt  ndof  = patch->totalDofsPerCell;
2255fa84ea4cSLawrence Mitchell   PetscErrorCode  ierr;
2256fa84ea4cSLawrence Mitchell   Mat             vecMat;
2257fe988be2SFlorian Wechsung   PetscInt        cStart, cEnd;
2258fe988be2SFlorian Wechsung   DM              dm, plex;
2259fe988be2SFlorian Wechsung 
2260fa84ea4cSLawrence Mitchell 
2261e9c2c94bSFlorian Wechsung   ierr = ISGetSize(patch->cells, &ncell);CHKERRQ(ierr);
2262e9c2c94bSFlorian Wechsung   if (!ncell) { /* No cells to assemble over -> skip */
2263e9c2c94bSFlorian Wechsung     PetscFunctionReturn(0);
2264e9c2c94bSFlorian Wechsung   }
2265e9c2c94bSFlorian Wechsung 
2266fa84ea4cSLawrence Mitchell   ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
2267fa84ea4cSLawrence Mitchell 
2268*b6bb21d1SLawrence Mitchell   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
2269*b6bb21d1SLawrence Mitchell   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
2270*b6bb21d1SLawrence Mitchell   dm = plex;
2271fa84ea4cSLawrence Mitchell   if (!patch->allCells) {
2272fa84ea4cSLawrence Mitchell     PetscHSetI      cells;
2273fa84ea4cSLawrence Mitchell     PetscHashIter   hi;
2274fa84ea4cSLawrence Mitchell     PetscInt pStart, pEnd;
2275fa84ea4cSLawrence Mitchell     PetscInt *allCells = NULL;
2276fa84ea4cSLawrence Mitchell     ierr = PetscHSetICreate(&cells);CHKERRQ(ierr);
2277fa84ea4cSLawrence Mitchell     ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
2278fa84ea4cSLawrence Mitchell     ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
2279fa84ea4cSLawrence Mitchell     for (i = pStart; i < pEnd; i++) {
2280fa84ea4cSLawrence Mitchell       ierr = PetscSectionGetDof(patch->cellCounts, i, &ncell);CHKERRQ(ierr);
2281fa84ea4cSLawrence Mitchell       ierr = PetscSectionGetOffset(patch->cellCounts, i, &offset);CHKERRQ(ierr);
2282fa84ea4cSLawrence Mitchell       if (ncell <= 0) continue;
2283fa84ea4cSLawrence Mitchell       for (j = 0; j < ncell; j++) {
2284fa84ea4cSLawrence Mitchell         PetscHSetIAdd(cells, cellsArray[offset + j]);CHKERRQ(ierr);
2285fa84ea4cSLawrence Mitchell       }
2286fa84ea4cSLawrence Mitchell     }
2287fa84ea4cSLawrence Mitchell     ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
2288fa84ea4cSLawrence Mitchell     ierr = PetscHSetIGetSize(cells, &ncell);CHKERRQ(ierr);
2289fa84ea4cSLawrence Mitchell     ierr = PetscMalloc1(ncell, &allCells);CHKERRQ(ierr);
2290fe988be2SFlorian Wechsung     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2291fe988be2SFlorian Wechsung     ierr = PetscMalloc1(cEnd-cStart, &patch->precomputedTensorLocations);CHKERRQ(ierr);
2292fa84ea4cSLawrence Mitchell     i = 0;
2293fa84ea4cSLawrence Mitchell     PetscHashIterBegin(cells, hi);
2294fa84ea4cSLawrence Mitchell     while (!PetscHashIterAtEnd(cells, hi)) {
2295fe988be2SFlorian Wechsung       PetscHashIterGetKey(cells, hi, allCells[i]);
2296fe988be2SFlorian Wechsung       patch->precomputedTensorLocations[allCells[i]] = i;
2297fa84ea4cSLawrence Mitchell       PetscHashIterNext(cells, hi);
2298fe988be2SFlorian Wechsung       i++;
2299fa84ea4cSLawrence Mitchell     }
2300fa84ea4cSLawrence Mitchell     ierr = PetscHSetIDestroy(&cells);CHKERRQ(ierr);
2301fa84ea4cSLawrence Mitchell     ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, allCells, PETSC_OWN_POINTER, &patch->allCells);CHKERRQ(ierr);
2302fa84ea4cSLawrence Mitchell   }
2303f98464cbSLawrence Mitchell   ierr = ISGetSize(patch->allCells, &ncell);CHKERRQ(ierr);
2304fa84ea4cSLawrence Mitchell   if (!patch->cellMats) {
2305fa84ea4cSLawrence Mitchell     ierr = VecCreateSeq(PETSC_COMM_SELF, ncell*ndof*ndof, &patch->cellMats);CHKERRQ(ierr);
2306fa84ea4cSLawrence Mitchell     ierr = VecSetBlockSize(patch->cellMats, ndof);CHKERRQ(ierr);
2307fa84ea4cSLawrence Mitchell   }
2308fa84ea4cSLawrence Mitchell   ierr = VecSet(patch->cellMats, 0);CHKERRQ(ierr);
2309fa84ea4cSLawrence Mitchell 
2310fa84ea4cSLawrence Mitchell   ierr = MatCreateShell(PETSC_COMM_SELF, ncell*ndof, ncell*ndof, ncell*ndof, ncell*ndof,
231189d5b078SSatish Balay                         (void*)patch->cellMats, &vecMat);CHKERRQ(ierr);
2312fa84ea4cSLawrence Mitchell   ierr = MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void(*)(void))&MatSetValues_PCPatch_Private);CHKERRQ(ierr);
2313fa84ea4cSLawrence Mitchell   ierr = ISGetSize(patch->allCells, &ncell);CHKERRQ(ierr);
2314fa84ea4cSLawrence Mitchell   ierr = ISCreateStride(PETSC_COMM_SELF, ndof*ncell, 0, 1, &dofMap);CHKERRQ(ierr);
2315fa84ea4cSLawrence Mitchell   ierr = ISGetIndices(dofMap, &dofMapArray);CHKERRQ(ierr);
2316fa84ea4cSLawrence Mitchell   ierr = ISGetIndices(patch->allCells, &cellsArray);CHKERRQ(ierr);
2317fa84ea4cSLawrence Mitchell   ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray, PETSC_USE_POINTER, &cellIS);CHKERRQ(ierr);
2318fa84ea4cSLawrence Mitchell   PetscStackPush("PCPatch user callback");
2319fa84ea4cSLawrence Mitchell   /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */
2320fa84ea4cSLawrence Mitchell   ierr = patch->usercomputeop(pc, -1, NULL, vecMat, cellIS, ndof*ncell, dofMapArray, NULL, patch->usercomputeopctx);CHKERRQ(ierr);
2321f98464cbSLawrence Mitchell   PetscStackPop;
2322fa84ea4cSLawrence Mitchell   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
2323fa84ea4cSLawrence Mitchell   ierr = MatDestroy(&vecMat);CHKERRQ(ierr);
2324fa84ea4cSLawrence Mitchell   ierr = ISRestoreIndices(patch->allCells, &cellsArray);CHKERRQ(ierr);
2325fa84ea4cSLawrence Mitchell   ierr = ISRestoreIndices(dofMap, &dofMapArray);CHKERRQ(ierr);
2326fa84ea4cSLawrence Mitchell   ierr = ISDestroy(&dofMap);CHKERRQ(ierr);
2327f98464cbSLawrence Mitchell 
2328f98464cbSLawrence Mitchell   if (patch->usercomputeopintfacet) {
2329f98464cbSLawrence Mitchell     PetscInt nIntFacets;
2330f98464cbSLawrence Mitchell     IS       intFacetsIS;
2331f98464cbSLawrence Mitchell     const PetscInt *intFacetsArray = NULL;
2332f98464cbSLawrence Mitchell     if (!patch->allIntFacets) {
2333f98464cbSLawrence Mitchell       PetscHSetI      facets;
2334f98464cbSLawrence Mitchell       PetscHashIter   hi;
2335f98464cbSLawrence Mitchell       PetscInt pStart, pEnd, fStart, fEnd;
2336f98464cbSLawrence Mitchell       PetscInt *allIntFacets = NULL;
2337f98464cbSLawrence Mitchell       ierr = PetscHSetICreate(&facets);CHKERRQ(ierr);
2338f98464cbSLawrence Mitchell       ierr = ISGetIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
2339f98464cbSLawrence Mitchell       ierr = PetscSectionGetChart(patch->intFacetCounts, &pStart, &pEnd);CHKERRQ(ierr);
2340de2d1767SPatrick Farrell       ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
2341f98464cbSLawrence Mitchell       for (i = pStart; i < pEnd; i++) {
2342f98464cbSLawrence Mitchell         ierr = PetscSectionGetDof(patch->intFacetCounts, i, &nIntFacets);CHKERRQ(ierr);
2343f98464cbSLawrence Mitchell         ierr = PetscSectionGetOffset(patch->intFacetCounts, i, &offset);CHKERRQ(ierr);
2344f98464cbSLawrence Mitchell         if (nIntFacets <= 0) continue;
2345f98464cbSLawrence Mitchell         for (j = 0; j < nIntFacets; j++) {
2346f98464cbSLawrence Mitchell           PetscHSetIAdd(facets, intFacetsArray[offset + j]);CHKERRQ(ierr);
2347f98464cbSLawrence Mitchell         }
2348f98464cbSLawrence Mitchell       }
2349f98464cbSLawrence Mitchell       ierr = ISRestoreIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
2350f98464cbSLawrence Mitchell       ierr = PetscHSetIGetSize(facets, &nIntFacets);CHKERRQ(ierr);
2351f98464cbSLawrence Mitchell       ierr = PetscMalloc1(nIntFacets, &allIntFacets);CHKERRQ(ierr);
2352f98464cbSLawrence Mitchell       ierr = PetscMalloc1(fEnd-fStart, &patch->precomputedIntFacetTensorLocations);CHKERRQ(ierr);
2353f98464cbSLawrence Mitchell       i = 0;
2354f98464cbSLawrence Mitchell       PetscHashIterBegin(facets, hi);
2355f98464cbSLawrence Mitchell       while (!PetscHashIterAtEnd(facets, hi)) {
2356f98464cbSLawrence Mitchell         PetscHashIterGetKey(facets, hi, allIntFacets[i]);
2357de2d1767SPatrick Farrell         patch->precomputedIntFacetTensorLocations[allIntFacets[i] - fStart] = i;
2358f98464cbSLawrence Mitchell         PetscHashIterNext(facets, hi);
2359f98464cbSLawrence Mitchell         i++;
2360f98464cbSLawrence Mitchell       }
2361f98464cbSLawrence Mitchell       ierr = PetscHSetIDestroy(&facets);CHKERRQ(ierr);
2362f98464cbSLawrence Mitchell       ierr = ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, allIntFacets, PETSC_OWN_POINTER, &patch->allIntFacets);CHKERRQ(ierr);
2363f98464cbSLawrence Mitchell     }
2364f98464cbSLawrence Mitchell     ierr = ISGetSize(patch->allIntFacets, &nIntFacets);CHKERRQ(ierr);
2365f98464cbSLawrence Mitchell     if (!patch->intFacetMats) {
2366f98464cbSLawrence Mitchell       ierr = VecCreateSeq(PETSC_COMM_SELF, nIntFacets*ndof*ndof*4, &patch->intFacetMats);CHKERRQ(ierr);
2367f98464cbSLawrence Mitchell       ierr = VecSetBlockSize(patch->intFacetMats, ndof*2);CHKERRQ(ierr);
2368f98464cbSLawrence Mitchell     }
2369f98464cbSLawrence Mitchell     ierr = VecSet(patch->intFacetMats, 0);CHKERRQ(ierr);
2370f98464cbSLawrence Mitchell 
2371f98464cbSLawrence Mitchell     ierr = MatCreateShell(PETSC_COMM_SELF, nIntFacets*ndof*2, nIntFacets*ndof*2, nIntFacets*ndof*2, nIntFacets*ndof*2,
237289d5b078SSatish Balay                           (void*)patch->intFacetMats, &vecMat);CHKERRQ(ierr);
2373f98464cbSLawrence Mitchell     ierr = MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void(*)(void))&MatSetValues_PCPatch_Private);CHKERRQ(ierr);
2374f98464cbSLawrence Mitchell     ierr = ISCreateStride(PETSC_COMM_SELF, 2*ndof*nIntFacets, 0, 1, &dofMap);CHKERRQ(ierr);
2375f98464cbSLawrence Mitchell     ierr = ISGetIndices(dofMap, &dofMapArray);CHKERRQ(ierr);
2376f98464cbSLawrence Mitchell     ierr = ISGetIndices(patch->allIntFacets, &intFacetsArray);CHKERRQ(ierr);
2377f98464cbSLawrence Mitchell     ierr = ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, intFacetsArray, PETSC_USE_POINTER, &intFacetsIS);CHKERRQ(ierr);
2378f98464cbSLawrence Mitchell     PetscStackPush("PCPatch user callback (interior facets)");
2379f98464cbSLawrence Mitchell     /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */
2380f98464cbSLawrence Mitchell     ierr = patch->usercomputeopintfacet(pc, -1, NULL, vecMat, intFacetsIS, 2*ndof*nIntFacets, dofMapArray, NULL, patch->usercomputeopintfacetctx);CHKERRQ(ierr);
2381f98464cbSLawrence Mitchell     PetscStackPop;
2382f98464cbSLawrence Mitchell     ierr = ISDestroy(&intFacetsIS);CHKERRQ(ierr);
2383f98464cbSLawrence Mitchell     ierr = MatDestroy(&vecMat);CHKERRQ(ierr);
2384f98464cbSLawrence Mitchell     ierr = ISRestoreIndices(patch->allIntFacets, &intFacetsArray);CHKERRQ(ierr);
2385f98464cbSLawrence Mitchell     ierr = ISRestoreIndices(dofMap, &dofMapArray);CHKERRQ(ierr);
2386f98464cbSLawrence Mitchell     ierr = ISDestroy(&dofMap);CHKERRQ(ierr);
2387f98464cbSLawrence Mitchell   }
2388*b6bb21d1SLawrence Mitchell   ierr = DMDestroy(&dm);CHKERRQ(ierr);
2389fa84ea4cSLawrence Mitchell   ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
2390fa84ea4cSLawrence Mitchell 
2391fa84ea4cSLawrence Mitchell   PetscFunctionReturn(0);
2392fa84ea4cSLawrence Mitchell }
2393fa84ea4cSLawrence Mitchell 
23940904074fSPatrick Farrell PetscErrorCode PCPatch_ScatterLocal_Private(PC pc, PetscInt p, Vec x, Vec y, InsertMode mode, ScatterMode scat, PatchScatterType scattertype)
23954bbf5ea8SMatthew G. Knepley {
23964bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch     = (PC_PATCH *) pc->data;
23974bbf5ea8SMatthew G. Knepley   const PetscScalar *xArray    = NULL;
23984bbf5ea8SMatthew G. Knepley   PetscScalar       *yArray    = NULL;
23994bbf5ea8SMatthew G. Knepley   const PetscInt    *gtolArray = NULL;
24004bbf5ea8SMatthew G. Knepley   PetscInt           dof, offset, lidx;
24014bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
24024bbf5ea8SMatthew G. Knepley 
24034bbf5ea8SMatthew G. Knepley   PetscFunctionBeginHot;
24044bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Scatter, pc, 0, 0, 0);CHKERRQ(ierr);
24054bbf5ea8SMatthew G. Knepley   ierr = VecGetArrayRead(x, &xArray);CHKERRQ(ierr);
24064bbf5ea8SMatthew G. Knepley   ierr = VecGetArray(y, &yArray);CHKERRQ(ierr);
24070904074fSPatrick Farrell   if (scattertype == SCATTER_WITHARTIFICIAL) {
2408c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof);CHKERRQ(ierr);
2409c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offset);CHKERRQ(ierr);
2410c2e6f3c0SFlorian Wechsung     ierr = ISGetIndices(patch->gtolWithArtificial, &gtolArray);CHKERRQ(ierr);
24110904074fSPatrick Farrell   } else if (scattertype == SCATTER_WITHALL) {
24120904074fSPatrick Farrell     ierr = PetscSectionGetDof(patch->gtolCountsWithAll, p, &dof);CHKERRQ(ierr);
24130904074fSPatrick Farrell     ierr = PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offset);CHKERRQ(ierr);
24140904074fSPatrick Farrell     ierr = ISGetIndices(patch->gtolWithAll, &gtolArray);CHKERRQ(ierr);
2415c2e6f3c0SFlorian Wechsung   } else {
24164bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr);
24174bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
24184bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
2419c2e6f3c0SFlorian Wechsung   }
24204bbf5ea8SMatthew 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");
24214bbf5ea8SMatthew 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");
24224bbf5ea8SMatthew G. Knepley   for (lidx = 0; lidx < dof; ++lidx) {
24234bbf5ea8SMatthew G. Knepley     const PetscInt gidx = gtolArray[offset+lidx];
24244bbf5ea8SMatthew G. Knepley 
24254bbf5ea8SMatthew G. Knepley     if (mode == INSERT_VALUES) yArray[lidx]  = xArray[gidx]; /* Forward */
24264bbf5ea8SMatthew G. Knepley     else                       yArray[gidx] += xArray[lidx]; /* Reverse */
24274bbf5ea8SMatthew G. Knepley   }
24280904074fSPatrick Farrell   if (scattertype == SCATTER_WITHARTIFICIAL) {
2429c2e6f3c0SFlorian Wechsung     ierr = ISRestoreIndices(patch->gtolWithArtificial, &gtolArray);CHKERRQ(ierr);
24300904074fSPatrick Farrell   } else if (scattertype == SCATTER_WITHALL) {
24310904074fSPatrick Farrell     ierr = ISRestoreIndices(patch->gtolWithAll, &gtolArray);CHKERRQ(ierr);
2432c2e6f3c0SFlorian Wechsung   } else {
24334bbf5ea8SMatthew G. Knepley     ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
2434c2e6f3c0SFlorian Wechsung   }
24354bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArrayRead(x, &xArray);CHKERRQ(ierr);
24364bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArray(y, &yArray);CHKERRQ(ierr);
24374bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Scatter, pc, 0, 0, 0);CHKERRQ(ierr);
24384bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
24394bbf5ea8SMatthew G. Knepley }
24404bbf5ea8SMatthew G. Knepley 
2441dadc69c5SMatthew G. Knepley static PetscErrorCode PCSetUp_PATCH_Linear(PC pc)
2442dadc69c5SMatthew G. Knepley {
2443dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2444dadc69c5SMatthew G. Knepley   const char    *prefix;
2445dadc69c5SMatthew G. Knepley   PetscInt       i;
2446dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2447dadc69c5SMatthew G. Knepley 
2448dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2449dadc69c5SMatthew G. Knepley   if (!pc->setupcalled) {
2450dadc69c5SMatthew G. Knepley     ierr = PetscMalloc1(patch->npatch, &patch->solver);CHKERRQ(ierr);
2451dadc69c5SMatthew G. Knepley     ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr);
2452dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {
2453dadc69c5SMatthew G. Knepley       KSP ksp;
2454dadc69c5SMatthew G. Knepley       PC  subpc;
2455dadc69c5SMatthew G. Knepley 
2456dadc69c5SMatthew G. Knepley       ierr = KSPCreate(PETSC_COMM_SELF, &ksp);CHKERRQ(ierr);
2457ddad275aSPatrick Farrell       ierr = KSPSetErrorIfNotConverged(ksp, pc->erroriffailure);CHKERRQ(ierr);
2458dadc69c5SMatthew G. Knepley       ierr = KSPSetOptionsPrefix(ksp, prefix);CHKERRQ(ierr);
2459dadc69c5SMatthew G. Knepley       ierr = KSPAppendOptionsPrefix(ksp, "sub_");CHKERRQ(ierr);
2460dadc69c5SMatthew G. Knepley       ierr = PetscObjectIncrementTabLevel((PetscObject) ksp, (PetscObject) pc, 1);CHKERRQ(ierr);
2461dadc69c5SMatthew G. Knepley       ierr = KSPGetPC(ksp, &subpc);CHKERRQ(ierr);
2462dadc69c5SMatthew G. Knepley       ierr = PetscObjectIncrementTabLevel((PetscObject) subpc, (PetscObject) pc, 1);CHKERRQ(ierr);
2463dadc69c5SMatthew G. Knepley       ierr = PetscLogObjectParent((PetscObject) pc, (PetscObject) ksp);CHKERRQ(ierr);
2464dadc69c5SMatthew G. Knepley       patch->solver[i] = (PetscObject) ksp;
2465dadc69c5SMatthew G. Knepley     }
2466dadc69c5SMatthew G. Knepley   }
2467dadc69c5SMatthew G. Knepley   if (patch->save_operators) {
2468e9c2c94bSFlorian Wechsung     if (patch->precomputeElementTensors) {
2469fe988be2SFlorian Wechsung       ierr = PCPatchPrecomputePatchTensors_Private(pc);CHKERRQ(ierr);
2470fe988be2SFlorian Wechsung     }
2471dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {
247234d8b122SPatrick Farrell       ierr = PCPatchComputeOperator_Internal(pc, NULL, patch->mat[i], i, PETSC_FALSE);CHKERRQ(ierr);
2473dadc69c5SMatthew G. Knepley       ierr = KSPSetOperators((KSP) patch->solver[i], patch->mat[i], patch->mat[i]);CHKERRQ(ierr);
2474dadc69c5SMatthew G. Knepley     }
2475dadc69c5SMatthew G. Knepley   }
247634d8b122SPatrick Farrell   if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
247734d8b122SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {
24781202d238SPatrick Farrell       /* Instead of padding patch->patchUpdate with zeros to get */
24791202d238SPatrick Farrell       /* patch->patchUpdateWithArtificial and then multiplying with the matrix, */
248034d8b122SPatrick Farrell       /* just get rid of the columns that correspond to the dofs with */
248134d8b122SPatrick Farrell       /* artificial bcs. That's of course fairly inefficient, hopefully we */
248234d8b122SPatrick Farrell       /* can just assemble the rectangular matrix in the first place. */
248334d8b122SPatrick Farrell       Mat matSquare;
248434d8b122SPatrick Farrell       IS rowis;
248534d8b122SPatrick Farrell       PetscInt dof;
248634d8b122SPatrick Farrell 
248734d8b122SPatrick Farrell       ierr = MatGetSize(patch->mat[i], &dof, NULL);CHKERRQ(ierr);
248834d8b122SPatrick Farrell       if (dof == 0) {
248934d8b122SPatrick Farrell         patch->matWithArtificial[i] = NULL;
249034d8b122SPatrick Farrell         continue;
249134d8b122SPatrick Farrell       }
249234d8b122SPatrick Farrell 
249334d8b122SPatrick Farrell       ierr = PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE);CHKERRQ(ierr);
249434d8b122SPatrick Farrell       ierr = PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE);CHKERRQ(ierr);
249534d8b122SPatrick Farrell 
249634d8b122SPatrick Farrell       ierr = MatGetSize(matSquare, &dof, NULL);CHKERRQ(ierr);
249734d8b122SPatrick Farrell       ierr = ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis); CHKERRQ(ierr);
249834d8b122SPatrick Farrell       if(pc->setupcalled) {
249934d8b122SPatrick Farrell         ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_REUSE_MATRIX, &patch->matWithArtificial[i]); CHKERRQ(ierr);
250034d8b122SPatrick Farrell       } else {
250134d8b122SPatrick Farrell         ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &patch->matWithArtificial[i]); CHKERRQ(ierr);
250234d8b122SPatrick Farrell       }
250334d8b122SPatrick Farrell       ierr = ISDestroy(&rowis); CHKERRQ(ierr);
250434d8b122SPatrick Farrell       ierr = MatDestroy(&matSquare);CHKERRQ(ierr);
250534d8b122SPatrick Farrell     }
250634d8b122SPatrick Farrell   }
2507dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
2508dadc69c5SMatthew G. Knepley }
2509dadc69c5SMatthew G. Knepley 
25104bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUp_PATCH(PC pc)
25114bbf5ea8SMatthew G. Knepley {
25124bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2513557beb66SLawrence Mitchell   PetscInt       i;
251439fd2e8aSPatrick Farrell   PetscBool       isNonlinear;
25154bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
25164bbf5ea8SMatthew G. Knepley 
25174bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
25184bbf5ea8SMatthew G. Knepley   if (!pc->setupcalled) {
25194bbf5ea8SMatthew G. Knepley     PetscInt pStart, pEnd, p;
25204bbf5ea8SMatthew G. Knepley     PetscInt localSize;
25214bbf5ea8SMatthew G. Knepley 
25224bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventBegin(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr);
25234bbf5ea8SMatthew G. Knepley 
2524debbdec3SPatrick Farrell     isNonlinear = patch->isNonlinear;
25255f824522SMatthew G. Knepley     if (!patch->nsubspaces) {
2526*b6bb21d1SLawrence Mitchell       DM           dm, plex;
25275f824522SMatthew G. Knepley       PetscSection s;
2528e72c1634SMatthew G. Knepley       PetscInt     cStart, cEnd, c, Nf, f, numGlobalBcs = 0, *globalBcs, *Nb, totNb = 0, **cellDofs;
25295f824522SMatthew G. Knepley 
25305f824522SMatthew G. Knepley       ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
25315f824522SMatthew G. Knepley       if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONG, "Must set DM for PCPATCH or call PCPatchSetDiscretisationInfo()");
2532*b6bb21d1SLawrence Mitchell       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
2533*b6bb21d1SLawrence Mitchell       dm = plex;
25345f824522SMatthew G. Knepley       ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
25355f824522SMatthew G. Knepley       ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
25365f824522SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
25375f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
25385f824522SMatthew G. Knepley         PetscInt cdof;
25395f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
25405f824522SMatthew G. Knepley         numGlobalBcs += cdof;
25415f824522SMatthew G. Knepley       }
25425f824522SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
25435f824522SMatthew G. Knepley       ierr = PetscMalloc3(Nf, &Nb, Nf, &cellDofs, numGlobalBcs, &globalBcs);CHKERRQ(ierr);
25445f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
25455f824522SMatthew G. Knepley         PetscFE        fe;
25465f824522SMatthew G. Knepley         PetscDualSpace sp;
25475f824522SMatthew G. Knepley         PetscInt       cdoff = 0;
25485f824522SMatthew G. Knepley 
254944a7f3ddSMatthew G. Knepley         ierr = DMGetField(dm, f, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
25505f824522SMatthew G. Knepley         /* ierr = PetscFEGetNumComponents(fe, &Nc[f]);CHKERRQ(ierr); */
25515f824522SMatthew G. Knepley         ierr = PetscFEGetDualSpace(fe, &sp);CHKERRQ(ierr);
25525f824522SMatthew G. Knepley         ierr = PetscDualSpaceGetDimension(sp, &Nb[f]);CHKERRQ(ierr);
25535f824522SMatthew G. Knepley         totNb += Nb[f];
25545f824522SMatthew G. Knepley 
25555f824522SMatthew G. Knepley         ierr = PetscMalloc1((cEnd-cStart)*Nb[f], &cellDofs[f]);CHKERRQ(ierr);
25565f824522SMatthew G. Knepley         for (c = cStart; c < cEnd; ++c) {
25575f824522SMatthew G. Knepley           PetscInt *closure = NULL;
25585f824522SMatthew G. Knepley           PetscInt  clSize  = 0, cl;
25595f824522SMatthew G. Knepley 
25605f824522SMatthew G. Knepley           ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
25615f824522SMatthew G. Knepley           for (cl = 0; cl < clSize*2; cl += 2) {
25625f824522SMatthew G. Knepley             const PetscInt p = closure[cl];
25635f824522SMatthew G. Knepley             PetscInt       fdof, d, foff;
25645f824522SMatthew G. Knepley 
25655f824522SMatthew G. Knepley             ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr);
25665f824522SMatthew G. Knepley             ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr);
25675f824522SMatthew G. Knepley             for (d = 0; d < fdof; ++d, ++cdoff) cellDofs[f][cdoff] = foff + d;
25685f824522SMatthew G. Knepley           }
25695f824522SMatthew G. Knepley           ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
25705f824522SMatthew G. Knepley         }
25715f824522SMatthew 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]);
25725f824522SMatthew G. Knepley       }
25735f824522SMatthew G. Knepley       numGlobalBcs = 0;
25745f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
25755f824522SMatthew G. Knepley         const PetscInt *ind;
25765f824522SMatthew G. Knepley         PetscInt        off, cdof, d;
25775f824522SMatthew G. Knepley 
25785f824522SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
25795f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
25805f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &ind);CHKERRQ(ierr);
25815f824522SMatthew G. Knepley         for (d = 0; d < cdof; ++d) globalBcs[numGlobalBcs++] = off + ind[d];
25825f824522SMatthew G. Knepley       }
25835f824522SMatthew G. Knepley 
25845f824522SMatthew G. Knepley       ierr = PCPatchSetDiscretisationInfoCombined(pc, dm, Nb, (const PetscInt **) cellDofs, numGlobalBcs, globalBcs, numGlobalBcs, globalBcs);CHKERRQ(ierr);
25855f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
25865f824522SMatthew G. Knepley         ierr = PetscFree(cellDofs[f]);CHKERRQ(ierr);
25875f824522SMatthew G. Knepley       }
25885f824522SMatthew G. Knepley       ierr = PetscFree3(Nb, cellDofs, globalBcs);CHKERRQ(ierr);
258992d50984SMatthew G. Knepley       ierr = PCPatchSetComputeFunction(pc, PCPatchComputeFunction_DMPlex_Private, NULL);CHKERRQ(ierr);
25905f824522SMatthew G. Knepley       ierr = PCPatchSetComputeOperator(pc, PCPatchComputeOperator_DMPlex_Private, NULL);CHKERRQ(ierr);
2591*b6bb21d1SLawrence Mitchell       ierr = DMDestroy(&dm);CHKERRQ(ierr);
25925f824522SMatthew G. Knepley     }
25935f824522SMatthew G. Knepley 
25944bbf5ea8SMatthew G. Knepley     localSize = patch->subspaceOffsets[patch->nsubspaces];
25951202d238SPatrick Farrell     ierr = VecCreateSeq(PETSC_COMM_SELF, localSize, &patch->localRHS);CHKERRQ(ierr);
25961202d238SPatrick Farrell     ierr = VecSetUp(patch->localRHS);CHKERRQ(ierr);
25971202d238SPatrick Farrell     ierr = VecDuplicate(patch->localRHS, &patch->localUpdate);CHKERRQ(ierr);
25984bbf5ea8SMatthew G. Knepley     ierr = PCPatchCreateCellPatches(pc);CHKERRQ(ierr);
25994bbf5ea8SMatthew G. Knepley     ierr = PCPatchCreateCellPatchDiscretisationInfo(pc);CHKERRQ(ierr);
26004bbf5ea8SMatthew G. Knepley 
26014bbf5ea8SMatthew G. Knepley     /* OK, now build the work vectors */
26024bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, &pEnd);CHKERRQ(ierr);
26031202d238SPatrick Farrell     ierr = PetscMalloc1(patch->npatch, &patch->patchRHS);CHKERRQ(ierr);
26041202d238SPatrick Farrell     ierr = PetscMalloc1(patch->npatch, &patch->patchUpdate);CHKERRQ(ierr);
2605c2e6f3c0SFlorian Wechsung 
260661c4b389SFlorian Wechsung     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
26071202d238SPatrick Farrell       ierr = PetscMalloc1(patch->npatch, &patch->patchRHSWithArtificial);CHKERRQ(ierr);
2608c2e6f3c0SFlorian Wechsung       ierr = PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithArtificial);CHKERRQ(ierr);
2609c2e6f3c0SFlorian Wechsung     }
26100904074fSPatrick Farrell     if (isNonlinear) {
26110904074fSPatrick Farrell       ierr = PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithAll);CHKERRQ(ierr);
26120904074fSPatrick Farrell     }
26134bbf5ea8SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26144bbf5ea8SMatthew G. Knepley       PetscInt dof;
26154bbf5ea8SMatthew G. Knepley 
26164bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr);
26171202d238SPatrick Farrell       ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchRHS[p-pStart]);CHKERRQ(ierr);
26181202d238SPatrick Farrell       ierr = VecSetUp(patch->patchRHS[p-pStart]);CHKERRQ(ierr);
26191202d238SPatrick Farrell       ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchUpdate[p-pStart]);CHKERRQ(ierr);
26201202d238SPatrick Farrell       ierr = VecSetUp(patch->patchUpdate[p-pStart]);CHKERRQ(ierr);
26210904074fSPatrick Farrell       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
26223bb0e8f7SKarl Rupp         const PetscInt    *gtolArray, *gtolArrayWithArtificial = NULL;
26233bb0e8f7SKarl Rupp         PetscInt           numPatchDofs, offset;
26243bb0e8f7SKarl Rupp         PetscInt           numPatchDofsWithArtificial, offsetWithArtificial;
26253bb0e8f7SKarl Rupp         PetscInt           dofWithoutArtificialCounter = 0;
26263bb0e8f7SKarl Rupp         PetscInt          *patchWithoutArtificialToWithArtificialArray;
26273bb0e8f7SKarl Rupp 
2628c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof);CHKERRQ(ierr);
26291202d238SPatrick Farrell         ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchRHSWithArtificial[p-pStart]);CHKERRQ(ierr);
26301202d238SPatrick Farrell         ierr = VecSetUp(patch->patchRHSWithArtificial[p-pStart]);CHKERRQ(ierr);
2631c2e6f3c0SFlorian Wechsung 
2632e047a90bSFlorian Wechsung         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
2633e047a90bSFlorian Wechsung         /* the index in the patch with all dofs */
2634c2e6f3c0SFlorian Wechsung         ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
263563deea8eSPatrick Farrell 
2636c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs);CHKERRQ(ierr);
263747aca4a6SPatrick Farrell         if (numPatchDofs == 0) {
263847aca4a6SPatrick Farrell           patch->dofMappingWithoutToWithArtificial[p-pStart] = NULL;
263947aca4a6SPatrick Farrell           continue;
264047aca4a6SPatrick Farrell         }
264163deea8eSPatrick Farrell 
2642c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
2643c2e6f3c0SFlorian Wechsung         ierr = ISGetIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial);CHKERRQ(ierr);
2644c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &numPatchDofsWithArtificial);CHKERRQ(ierr);
2645c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offsetWithArtificial);CHKERRQ(ierr);
2646c2e6f3c0SFlorian Wechsung 
2647c2e6f3c0SFlorian Wechsung         ierr = PetscMalloc1(numPatchDofs, &patchWithoutArtificialToWithArtificialArray);CHKERRQ(ierr);
2648b0c21b6aSKarl Rupp         for (i=0; i<numPatchDofsWithArtificial; i++) {
2649e047a90bSFlorian Wechsung           if (gtolArrayWithArtificial[i+offsetWithArtificial] == gtolArray[offset+dofWithoutArtificialCounter]) {
2650c2e6f3c0SFlorian Wechsung             patchWithoutArtificialToWithArtificialArray[dofWithoutArtificialCounter] = i;
2651c2e6f3c0SFlorian Wechsung             dofWithoutArtificialCounter++;
2652c2e6f3c0SFlorian Wechsung             if (dofWithoutArtificialCounter == numPatchDofs)
2653c2e6f3c0SFlorian Wechsung               break;
2654c2e6f3c0SFlorian Wechsung           }
2655c2e6f3c0SFlorian Wechsung         }
2656168bb5e4SPatrick Farrell         ierr = ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutArtificialToWithArtificialArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithArtificial[p-pStart]);CHKERRQ(ierr);
2657c2e6f3c0SFlorian Wechsung         ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
2658c2e6f3c0SFlorian Wechsung         ierr = ISRestoreIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial);CHKERRQ(ierr);
2659c2e6f3c0SFlorian Wechsung       }
26600904074fSPatrick Farrell       if (isNonlinear) {
26610904074fSPatrick Farrell         const PetscInt    *gtolArray, *gtolArrayWithAll = NULL;
26620904074fSPatrick Farrell         PetscInt           numPatchDofs, offset;
26630904074fSPatrick Farrell         PetscInt           numPatchDofsWithAll, offsetWithAll;
26640904074fSPatrick Farrell         PetscInt           dofWithoutAllCounter = 0;
26650904074fSPatrick Farrell         PetscInt          *patchWithoutAllToWithAllArray;
26660904074fSPatrick Farrell 
26670904074fSPatrick Farrell         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
26680904074fSPatrick Farrell         /* the index in the patch with all dofs */
26690904074fSPatrick Farrell         ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
26700904074fSPatrick Farrell 
26710904074fSPatrick Farrell         ierr = PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs);CHKERRQ(ierr);
267247aca4a6SPatrick Farrell         if (numPatchDofs == 0) {
2673b88cb22dSPatrick Farrell           patch->dofMappingWithoutToWithAll[p-pStart] = NULL;
267447aca4a6SPatrick Farrell           continue;
267547aca4a6SPatrick Farrell         }
26760904074fSPatrick Farrell 
26770904074fSPatrick Farrell         ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
26780904074fSPatrick Farrell         ierr = ISGetIndices(patch->gtolWithAll, &gtolArrayWithAll);CHKERRQ(ierr);
26790904074fSPatrick Farrell         ierr = PetscSectionGetDof(patch->gtolCountsWithAll, p, &numPatchDofsWithAll);CHKERRQ(ierr);
26800904074fSPatrick Farrell         ierr = PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offsetWithAll);CHKERRQ(ierr);
26810904074fSPatrick Farrell 
26820904074fSPatrick Farrell         ierr = PetscMalloc1(numPatchDofs, &patchWithoutAllToWithAllArray);CHKERRQ(ierr);
26830904074fSPatrick Farrell 
26840904074fSPatrick Farrell         for (i=0; i<numPatchDofsWithAll; i++) {
26850904074fSPatrick Farrell           if (gtolArrayWithAll[i+offsetWithAll] == gtolArray[offset+dofWithoutAllCounter]) {
26860904074fSPatrick Farrell             patchWithoutAllToWithAllArray[dofWithoutAllCounter] = i;
26870904074fSPatrick Farrell             dofWithoutAllCounter++;
26880904074fSPatrick Farrell             if (dofWithoutAllCounter == numPatchDofs)
26890904074fSPatrick Farrell               break;
26900904074fSPatrick Farrell           }
26910904074fSPatrick Farrell         }
2692168bb5e4SPatrick Farrell         ierr = ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutAllToWithAllArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithAll[p-pStart]);CHKERRQ(ierr);
26930904074fSPatrick Farrell         ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
26940904074fSPatrick Farrell         ierr = ISRestoreIndices(patch->gtolWithAll, &gtolArrayWithAll);CHKERRQ(ierr);
26950904074fSPatrick Farrell       }
26964bbf5ea8SMatthew G. Knepley     }
26974bbf5ea8SMatthew G. Knepley     if (patch->save_operators) {
26984bbf5ea8SMatthew G. Knepley       ierr = PetscMalloc1(patch->npatch, &patch->mat);CHKERRQ(ierr);
26994bbf5ea8SMatthew G. Knepley       for (i = 0; i < patch->npatch; ++i) {
2700c2e6f3c0SFlorian Wechsung         ierr = PCPatchCreateMatrix_Private(pc, i, &patch->mat[i], PETSC_FALSE);CHKERRQ(ierr);
27014bbf5ea8SMatthew G. Knepley       }
27024bbf5ea8SMatthew G. Knepley     }
27034bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr);
27044bbf5ea8SMatthew G. Knepley 
27054bbf5ea8SMatthew G. Knepley     /* If desired, calculate weights for dof multiplicity */
27064bbf5ea8SMatthew G. Knepley     if (patch->partition_of_unity) {
27073bb0e8f7SKarl Rupp       PetscScalar *input = NULL;
27083bb0e8f7SKarl Rupp       PetscScalar *output = NULL;
27093bb0e8f7SKarl Rupp       Vec global;
27103bb0e8f7SKarl Rupp 
27111202d238SPatrick Farrell       ierr = VecDuplicate(patch->localRHS, &patch->dof_weights);CHKERRQ(ierr);
271261c4b389SFlorian Wechsung       if(patch->local_composition_type == PC_COMPOSITE_ADDITIVE) {
27134bbf5ea8SMatthew G. Knepley         for (i = 0; i < patch->npatch; ++i) {
27144bbf5ea8SMatthew G. Knepley           PetscInt dof;
27154bbf5ea8SMatthew G. Knepley 
27164bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &dof);CHKERRQ(ierr);
27174bbf5ea8SMatthew G. Knepley           if (dof <= 0) continue;
27181202d238SPatrick Farrell           ierr = VecSet(patch->patchRHS[i], 1.0);CHKERRQ(ierr);
27190904074fSPatrick Farrell           ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchRHS[i], patch->dof_weights, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR);CHKERRQ(ierr);
27204bbf5ea8SMatthew G. Knepley         }
2721c2e6f3c0SFlorian Wechsung       } else {
2722e047a90bSFlorian Wechsung         /* multiplicative is actually only locally multiplicative and globally additive. need the pou where the mesh decomposition overlaps */
2723c2e6f3c0SFlorian Wechsung         ierr = VecSet(patch->dof_weights, 1.0);CHKERRQ(ierr);
27244bbf5ea8SMatthew G. Knepley       }
2725d132cafaSFlorian Wechsung 
2726d132cafaSFlorian Wechsung       VecDuplicate(patch->dof_weights, &global);
2727d132cafaSFlorian Wechsung       VecSet(global, 0.);
2728d132cafaSFlorian Wechsung 
2729d132cafaSFlorian Wechsung       ierr = VecGetArray(patch->dof_weights, &input);CHKERRQ(ierr);
2730d132cafaSFlorian Wechsung       ierr = VecGetArray(global, &output);CHKERRQ(ierr);
2731d132cafaSFlorian Wechsung       ierr = PetscSFReduceBegin(patch->defaultSF, MPIU_SCALAR, input, output, MPI_SUM);CHKERRQ(ierr);
2732d132cafaSFlorian Wechsung       ierr = PetscSFReduceEnd(patch->defaultSF, MPIU_SCALAR, input, output, MPI_SUM);CHKERRQ(ierr);
2733d132cafaSFlorian Wechsung       ierr = VecRestoreArray(patch->dof_weights, &input);CHKERRQ(ierr);
2734d132cafaSFlorian Wechsung       ierr = VecRestoreArray(global, &output);CHKERRQ(ierr);
2735d132cafaSFlorian Wechsung 
273605528938SFlorian Wechsung       ierr = VecReciprocal(global);CHKERRQ(ierr);
2737d132cafaSFlorian Wechsung 
2738d132cafaSFlorian Wechsung       ierr = VecGetArray(patch->dof_weights, &output);CHKERRQ(ierr);
2739d132cafaSFlorian Wechsung       ierr = VecGetArray(global, &input);CHKERRQ(ierr);
2740d132cafaSFlorian Wechsung       ierr = PetscSFBcastBegin(patch->defaultSF, MPIU_SCALAR, input, output);CHKERRQ(ierr);
2741d132cafaSFlorian Wechsung       ierr = PetscSFBcastEnd(patch->defaultSF, MPIU_SCALAR, input, output);CHKERRQ(ierr);
2742d132cafaSFlorian Wechsung       ierr = VecRestoreArray(patch->dof_weights, &output);CHKERRQ(ierr);
2743d132cafaSFlorian Wechsung       ierr = VecRestoreArray(global, &input);CHKERRQ(ierr);
2744d132cafaSFlorian Wechsung       ierr = VecDestroy(&global);CHKERRQ(ierr);
27454bbf5ea8SMatthew G. Knepley     }
274661c4b389SFlorian Wechsung     if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE && patch->save_operators) {
274796b79ebeSFlorian Wechsung       ierr = PetscMalloc1(patch->npatch, &patch->matWithArtificial);CHKERRQ(ierr);
27484bbf5ea8SMatthew G. Knepley     }
27494bbf5ea8SMatthew G. Knepley   }
2750dadc69c5SMatthew G. Knepley   ierr = (*patch->setupsolver)(pc);CHKERRQ(ierr);
2751dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
27524bbf5ea8SMatthew G. Knepley }
2753dadc69c5SMatthew G. Knepley 
2754dadc69c5SMatthew G. Knepley static PetscErrorCode PCApply_PATCH_Linear(PC pc, PetscInt i, Vec x, Vec y)
2755dadc69c5SMatthew G. Knepley {
2756dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2757dadc69c5SMatthew G. Knepley   KSP            ksp   = (KSP) patch->solver[i];
2758dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2759dadc69c5SMatthew G. Knepley 
2760dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2761dadc69c5SMatthew G. Knepley   if (!patch->save_operators) {
2762dadc69c5SMatthew G. Knepley     Mat mat;
2763dadc69c5SMatthew G. Knepley 
276434d8b122SPatrick Farrell     ierr = PCPatchCreateMatrix_Private(pc, i, &mat, PETSC_FALSE);CHKERRQ(ierr);
2765dadc69c5SMatthew G. Knepley     /* Populate operator here. */
276634d8b122SPatrick Farrell     ierr = PCPatchComputeOperator_Internal(pc, NULL, mat, i, PETSC_FALSE);CHKERRQ(ierr);
2767dadc69c5SMatthew G. Knepley     ierr = KSPSetOperators(ksp, mat, mat);CHKERRQ(ierr);
2768dadc69c5SMatthew G. Knepley     /* Drop reference so the KSPSetOperators below will blow it away. */
2769dadc69c5SMatthew G. Knepley     ierr = MatDestroy(&mat);CHKERRQ(ierr);
2770dadc69c5SMatthew G. Knepley   }
2771dadc69c5SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr);
2772dadc69c5SMatthew G. Knepley   if (!ksp->setfromoptionscalled) {
2773dadc69c5SMatthew G. Knepley     ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
2774dadc69c5SMatthew G. Knepley   }
2775dadc69c5SMatthew G. Knepley   ierr = KSPSolve(ksp, x, y);CHKERRQ(ierr);
2776ddad275aSPatrick Farrell   ierr = KSPCheckSolve(ksp, pc, y);CHKERRQ(ierr);
2777dadc69c5SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr);
2778dadc69c5SMatthew G. Knepley   if (!patch->save_operators) {
2779dadc69c5SMatthew G. Knepley     PC pc;
2780dadc69c5SMatthew G. Knepley     ierr = KSPSetOperators(ksp, NULL, NULL);CHKERRQ(ierr);
2781dadc69c5SMatthew G. Knepley     ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
2782dadc69c5SMatthew G. Knepley     /* Destroy PC context too, otherwise the factored matrix hangs around. */
2783dadc69c5SMatthew G. Knepley     ierr = PCReset(pc);CHKERRQ(ierr);
27844bbf5ea8SMatthew G. Knepley   }
27854bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
27864bbf5ea8SMatthew G. Knepley }
27874bbf5ea8SMatthew G. Knepley 
27886c9c532dSPatrick Farrell static PetscErrorCode PCUpdateMultiplicative_PATCH_Linear(PC pc, PetscInt i, PetscInt pStart)
27896c9c532dSPatrick Farrell {
27906c9c532dSPatrick Farrell   PC_PATCH      *patch = (PC_PATCH *) pc->data;
27916c9c532dSPatrick Farrell   Mat multMat;
27926c9c532dSPatrick Farrell   PetscErrorCode ierr;
27936c9c532dSPatrick Farrell 
27944d04e9f1SPatrick Farrell   PetscFunctionBegin;
27954d04e9f1SPatrick Farrell 
27966c9c532dSPatrick Farrell   if (patch->save_operators) {
27976c9c532dSPatrick Farrell     multMat = patch->matWithArtificial[i];
27986c9c532dSPatrick Farrell   } else {
27996c9c532dSPatrick Farrell     /*Very inefficient, hopefully we can just assemble the rectangular matrix in the first place.*/
28006c9c532dSPatrick Farrell     Mat matSquare;
28016c9c532dSPatrick Farrell     PetscInt dof;
28026c9c532dSPatrick Farrell     IS rowis;
28036c9c532dSPatrick Farrell     ierr = PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE);CHKERRQ(ierr);
28046c9c532dSPatrick Farrell     ierr = PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE);CHKERRQ(ierr);
28056c9c532dSPatrick Farrell     ierr = MatGetSize(matSquare, &dof, NULL);CHKERRQ(ierr);
28066c9c532dSPatrick Farrell     ierr = ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis); CHKERRQ(ierr);
28076c9c532dSPatrick Farrell     ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &multMat); CHKERRQ(ierr);
28086c9c532dSPatrick Farrell     ierr = MatDestroy(&matSquare);CHKERRQ(ierr);
28096c9c532dSPatrick Farrell     ierr = ISDestroy(&rowis); CHKERRQ(ierr);
28106c9c532dSPatrick Farrell   }
28116c9c532dSPatrick Farrell   ierr = MatMult(multMat, patch->patchUpdate[i], patch->patchRHSWithArtificial[i]); CHKERRQ(ierr);
28126c9c532dSPatrick Farrell   ierr = VecScale(patch->patchRHSWithArtificial[i], -1.0); CHKERRQ(ierr);
28130904074fSPatrick Farrell   ierr = PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHSWithArtificial[i], patch->localRHS, ADD_VALUES, SCATTER_REVERSE, SCATTER_WITHARTIFICIAL); CHKERRQ(ierr);
28146c9c532dSPatrick Farrell   if (!patch->save_operators) {
28156c9c532dSPatrick Farrell     ierr = MatDestroy(&multMat); CHKERRQ(ierr);
28166c9c532dSPatrick Farrell   }
28174d04e9f1SPatrick Farrell   PetscFunctionReturn(0);
28186c9c532dSPatrick Farrell }
28196c9c532dSPatrick Farrell 
28204bbf5ea8SMatthew G. Knepley static PetscErrorCode PCApply_PATCH(PC pc, Vec x, Vec y)
28214bbf5ea8SMatthew G. Knepley {
28224bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch    = (PC_PATCH *) pc->data;
28231202d238SPatrick Farrell   const PetscScalar *globalRHS  = NULL;
28241202d238SPatrick Farrell   PetscScalar       *localRHS   = NULL;
28251202d238SPatrick Farrell   PetscScalar       *globalUpdate  = NULL;
28264bbf5ea8SMatthew G. Knepley   const PetscInt    *bcNodes  = NULL;
28274bbf5ea8SMatthew G. Knepley   PetscInt           nsweep   = patch->symmetrise_sweep ? 2 : 1;
28284bbf5ea8SMatthew G. Knepley   PetscInt           start[2] = {0, 0};
28294bbf5ea8SMatthew G. Knepley   PetscInt           end[2]   = {-1, -1};
28304bbf5ea8SMatthew G. Knepley   const PetscInt     inc[2]   = {1, -1};
28311202d238SPatrick Farrell   const PetscScalar *localUpdate;
28324bbf5ea8SMatthew G. Knepley   const PetscInt    *iterationSet;
28334bbf5ea8SMatthew G. Knepley   PetscInt           pStart, numBcs, n, sweep, bc, j;
28344bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
28354bbf5ea8SMatthew G. Knepley 
28364bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
28374bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr);
28384bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsPushGetViewerOff(PETSC_TRUE);CHKERRQ(ierr);
283992d50984SMatthew G. Knepley   /* start, end, inc have 2 entries to manage a second backward sweep if we symmetrize */
28404bbf5ea8SMatthew G. Knepley   end[0]   = patch->npatch;
28414bbf5ea8SMatthew G. Knepley   start[1] = patch->npatch-1;
28424bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
28434bbf5ea8SMatthew G. Knepley     ierr = ISGetLocalSize(patch->iterationSet, &end[0]);CHKERRQ(ierr);
28444bbf5ea8SMatthew G. Knepley     start[1] = end[0] - 1;
28454bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr);
28464bbf5ea8SMatthew G. Knepley   }
28474bbf5ea8SMatthew G. Knepley   /* Scatter from global space into overlapped local spaces */
28481202d238SPatrick Farrell   ierr = VecGetArrayRead(x, &globalRHS);CHKERRQ(ierr);
28491202d238SPatrick Farrell   ierr = VecGetArray(patch->localRHS, &localRHS);CHKERRQ(ierr);
28501202d238SPatrick Farrell   ierr = PetscSFBcastBegin(patch->defaultSF, MPIU_SCALAR, globalRHS, localRHS);CHKERRQ(ierr);
28511202d238SPatrick Farrell   ierr = PetscSFBcastEnd(patch->defaultSF, MPIU_SCALAR, globalRHS, localRHS);CHKERRQ(ierr);
28521202d238SPatrick Farrell   ierr = VecRestoreArrayRead(x, &globalRHS);CHKERRQ(ierr);
28531202d238SPatrick Farrell   ierr = VecRestoreArray(patch->localRHS, &localRHS);CHKERRQ(ierr);
28544bbf5ea8SMatthew G. Knepley 
28551202d238SPatrick Farrell   ierr = VecSet(patch->localUpdate, 0.0);CHKERRQ(ierr);
28564bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, NULL);CHKERRQ(ierr);
28574bbf5ea8SMatthew G. Knepley   for (sweep = 0; sweep < nsweep; sweep++) {
28584bbf5ea8SMatthew G. Knepley     for (j = start[sweep]; j*inc[sweep] < end[sweep]*inc[sweep]; j += inc[sweep]) {
28594bbf5ea8SMatthew G. Knepley       PetscInt i       = patch->user_patches ? iterationSet[j] : j;
28604bbf5ea8SMatthew G. Knepley       PetscInt start, len;
28614bbf5ea8SMatthew G. Knepley 
28624bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &len);CHKERRQ(ierr);
28634bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetOffset(patch->gtolCounts, i+pStart, &start);CHKERRQ(ierr);
28644bbf5ea8SMatthew G. Knepley       /* TODO: Squash out these guys in the setup as well. */
28654bbf5ea8SMatthew G. Knepley       if (len <= 0) continue;
28664bbf5ea8SMatthew G. Knepley       /* TODO: Do we need different scatters for X and Y? */
28670904074fSPatrick Farrell       ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->localRHS, patch->patchRHS[i], INSERT_VALUES, SCATTER_FORWARD, SCATTER_INTERIOR);CHKERRQ(ierr);
28681202d238SPatrick Farrell       ierr = (*patch->applysolver)(pc, i, patch->patchRHS[i], patch->patchUpdate[i]);CHKERRQ(ierr);
28690904074fSPatrick Farrell       ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchUpdate[i], patch->localUpdate, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR);CHKERRQ(ierr);
287061c4b389SFlorian Wechsung       if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
28716c9c532dSPatrick Farrell         ierr = (*patch->updatemultiplicative)(pc, i, pStart);CHKERRQ(ierr);
2872c2e6f3c0SFlorian Wechsung       }
28734bbf5ea8SMatthew G. Knepley     }
28744bbf5ea8SMatthew G. Knepley   }
28754bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {ierr = ISRestoreIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr);}
28764bbf5ea8SMatthew G. Knepley   /* XXX: should we do this on the global vector? */
287773ec7555SLawrence Mitchell   if (patch->partition_of_unity) {
28781202d238SPatrick Farrell     ierr = VecPointwiseMult(patch->localUpdate, patch->localUpdate, patch->dof_weights);CHKERRQ(ierr);
28794bbf5ea8SMatthew G. Knepley   }
28801202d238SPatrick Farrell   /* Now patch->localUpdate contains the solution of the patch solves, so we need to combine them all. */
28814bbf5ea8SMatthew G. Knepley   ierr = VecSet(y, 0.0);CHKERRQ(ierr);
28821202d238SPatrick Farrell   ierr = VecGetArray(y, &globalUpdate);CHKERRQ(ierr);
28831202d238SPatrick Farrell   ierr = VecGetArrayRead(patch->localUpdate, &localUpdate);CHKERRQ(ierr);
28841202d238SPatrick Farrell   ierr = PetscSFReduceBegin(patch->defaultSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM);CHKERRQ(ierr);
28851202d238SPatrick Farrell   ierr = PetscSFReduceEnd(patch->defaultSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM);CHKERRQ(ierr);
28861202d238SPatrick Farrell   ierr = VecRestoreArrayRead(patch->localUpdate, &localUpdate);CHKERRQ(ierr);
28874bbf5ea8SMatthew G. Knepley 
28884bbf5ea8SMatthew G. Knepley   /* Now we need to send the global BC values through */
28891202d238SPatrick Farrell   ierr = VecGetArrayRead(x, &globalRHS);CHKERRQ(ierr);
28904bbf5ea8SMatthew G. Knepley   ierr = ISGetSize(patch->globalBcNodes, &numBcs);CHKERRQ(ierr);
28914bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr);
28924bbf5ea8SMatthew G. Knepley   ierr = VecGetLocalSize(x, &n);CHKERRQ(ierr);
28934bbf5ea8SMatthew G. Knepley   for (bc = 0; bc < numBcs; ++bc) {
28944bbf5ea8SMatthew G. Knepley     const PetscInt idx = bcNodes[bc];
28951202d238SPatrick Farrell     if (idx < n) globalUpdate[idx] = globalRHS[idx];
28964bbf5ea8SMatthew G. Knepley   }
28974bbf5ea8SMatthew G. Knepley 
28984bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr);
28991202d238SPatrick Farrell   ierr = VecRestoreArrayRead(x, &globalRHS);CHKERRQ(ierr);
29001202d238SPatrick Farrell   ierr = VecRestoreArray(y, &globalUpdate);CHKERRQ(ierr);
29014bbf5ea8SMatthew G. Knepley 
29024bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsPopGetViewerOff();CHKERRQ(ierr);
29034bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr);
29044bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
29054bbf5ea8SMatthew G. Knepley }
29064bbf5ea8SMatthew G. Knepley 
2907dadc69c5SMatthew G. Knepley static PetscErrorCode PCReset_PATCH_Linear(PC pc)
2908dadc69c5SMatthew G. Knepley {
2909dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2910dadc69c5SMatthew G. Knepley   PetscInt       i;
2911dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2912dadc69c5SMatthew G. Knepley 
2913dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2914dadc69c5SMatthew G. Knepley   if (patch->solver) {
2915dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = KSPReset((KSP) patch->solver[i]);CHKERRQ(ierr);}
2916dadc69c5SMatthew G. Knepley   }
2917dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
2918dadc69c5SMatthew G. Knepley }
2919dadc69c5SMatthew G. Knepley 
29204bbf5ea8SMatthew G. Knepley static PetscErrorCode PCReset_PATCH(PC pc)
29214bbf5ea8SMatthew G. Knepley {
29224bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
29234bbf5ea8SMatthew G. Knepley   PetscInt       i;
29244bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
29254bbf5ea8SMatthew G. Knepley 
29264bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2927fa84ea4cSLawrence Mitchell 
29284bbf5ea8SMatthew G. Knepley   ierr = PetscSFDestroy(&patch->defaultSF);CHKERRQ(ierr);
29294bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->cellCounts);CHKERRQ(ierr);
29305f824522SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->pointCounts);CHKERRQ(ierr);
29314bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->cellNumbering);CHKERRQ(ierr);
29324bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->gtolCounts);CHKERRQ(ierr);
29334bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->gtol);CHKERRQ(ierr);
29344bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->cells);CHKERRQ(ierr);
29355f824522SMatthew G. Knepley   ierr = ISDestroy(&patch->points);CHKERRQ(ierr);
29364bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->dofs);CHKERRQ(ierr);
29375f824522SMatthew G. Knepley   ierr = ISDestroy(&patch->offs);CHKERRQ(ierr);
29385f824522SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->patchSection);CHKERRQ(ierr);
29394bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->ghostBcNodes);CHKERRQ(ierr);
29404bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->globalBcNodes);CHKERRQ(ierr);
294111ac8bb0SFlorian Wechsung   ierr = PetscSectionDestroy(&patch->gtolCountsWithArtificial);CHKERRQ(ierr);
294211ac8bb0SFlorian Wechsung   ierr = ISDestroy(&patch->gtolWithArtificial);CHKERRQ(ierr);
294311ac8bb0SFlorian Wechsung   ierr = ISDestroy(&patch->dofsWithArtificial);CHKERRQ(ierr);
294411ac8bb0SFlorian Wechsung   ierr = ISDestroy(&patch->offsWithArtificial);CHKERRQ(ierr);
29450904074fSPatrick Farrell   ierr = PetscSectionDestroy(&patch->gtolCountsWithAll);CHKERRQ(ierr);
29460904074fSPatrick Farrell   ierr = ISDestroy(&patch->gtolWithAll);CHKERRQ(ierr);
29470904074fSPatrick Farrell   ierr = ISDestroy(&patch->dofsWithAll);CHKERRQ(ierr);
29480904074fSPatrick Farrell   ierr = ISDestroy(&patch->offsWithAll);CHKERRQ(ierr);
2949fa84ea4cSLawrence Mitchell   ierr = VecDestroy(&patch->cellMats);CHKERRQ(ierr);
2950f98464cbSLawrence Mitchell   ierr = VecDestroy(&patch->intFacetMats);CHKERRQ(ierr);
2951fa84ea4cSLawrence Mitchell   ierr = ISDestroy(&patch->allCells);CHKERRQ(ierr);
2952e6fe6948SPatrick Farrell   ierr = ISDestroy(&patch->intFacets);CHKERRQ(ierr);
2953e6fe6948SPatrick Farrell   ierr = ISDestroy(&patch->extFacets);CHKERRQ(ierr);
2954e6fe6948SPatrick Farrell   ierr = ISDestroy(&patch->intFacetsToPatchCell);CHKERRQ(ierr);
2955b65fa9bbSPatrick Farrell   ierr = PetscSectionDestroy(&patch->intFacetCounts);CHKERRQ(ierr);
2956b65fa9bbSPatrick Farrell   ierr = PetscSectionDestroy(&patch->extFacetCounts);CHKERRQ(ierr);
29574bbf5ea8SMatthew G. Knepley 
29585f824522SMatthew G. Knepley   if (patch->dofSection) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscSectionDestroy(&patch->dofSection[i]);CHKERRQ(ierr);}
29594bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->dofSection);CHKERRQ(ierr);
29604bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->bs);CHKERRQ(ierr);
29614bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->nodesPerCell);CHKERRQ(ierr);
29625f824522SMatthew G. Knepley   if (patch->cellNodeMap) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscFree(patch->cellNodeMap[i]);CHKERRQ(ierr);}
29634bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->cellNodeMap);CHKERRQ(ierr);
29644bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->subspaceOffsets);CHKERRQ(ierr);
29654bbf5ea8SMatthew G. Knepley 
2966dadc69c5SMatthew G. Knepley   ierr = (*patch->resetsolver)(pc);CHKERRQ(ierr);
29674bbf5ea8SMatthew G. Knepley 
2968e4c66b91SPatrick Farrell   if (patch->subspaces_to_exclude) {
2969e4c66b91SPatrick Farrell     PetscHSetIDestroy(&patch->subspaces_to_exclude);
2970e4c66b91SPatrick Farrell   }
2971e4c66b91SPatrick Farrell 
29721202d238SPatrick Farrell   ierr = VecDestroy(&patch->localRHS);CHKERRQ(ierr);
29731202d238SPatrick Farrell   ierr = VecDestroy(&patch->localUpdate);CHKERRQ(ierr);
29741202d238SPatrick Farrell   if (patch->patchRHS) {
29751202d238SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchRHS[i]);CHKERRQ(ierr);}
29761202d238SPatrick Farrell     ierr = PetscFree(patch->patchRHS);CHKERRQ(ierr);
29774bbf5ea8SMatthew G. Knepley   }
29781202d238SPatrick Farrell   if (patch->patchUpdate) {
29791202d238SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchUpdate[i]);CHKERRQ(ierr);}
29801202d238SPatrick Farrell     ierr = PetscFree(patch->patchUpdate);CHKERRQ(ierr);
29814bbf5ea8SMatthew G. Knepley   }
29824bbf5ea8SMatthew G. Knepley   ierr = VecDestroy(&patch->dof_weights);CHKERRQ(ierr);
29834bbf5ea8SMatthew G. Knepley   if (patch->patch_dof_weights) {
29845f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patch_dof_weights[i]);CHKERRQ(ierr);}
29854bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->patch_dof_weights);CHKERRQ(ierr);
29864bbf5ea8SMatthew G. Knepley   }
29874bbf5ea8SMatthew G. Knepley   if (patch->mat) {
29885f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->mat[i]);CHKERRQ(ierr);}
29894bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->mat);CHKERRQ(ierr);
29905f824522SMatthew G. Knepley   }
299111ac8bb0SFlorian Wechsung   if (patch->matWithArtificial) {
299211ac8bb0SFlorian Wechsung     for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->matWithArtificial[i]);CHKERRQ(ierr);}
299311ac8bb0SFlorian Wechsung     ierr = PetscFree(patch->matWithArtificial);CHKERRQ(ierr);
299411ac8bb0SFlorian Wechsung   }
29951202d238SPatrick Farrell   if (patch->patchRHSWithArtificial) {
29961202d238SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchRHSWithArtificial[i]);CHKERRQ(ierr);}
29971202d238SPatrick Farrell     ierr = PetscFree(patch->patchRHSWithArtificial);CHKERRQ(ierr);
299811ac8bb0SFlorian Wechsung   }
299996b79ebeSFlorian Wechsung   if(patch->dofMappingWithoutToWithArtificial) {
300096b79ebeSFlorian Wechsung     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->dofMappingWithoutToWithArtificial[i]);CHKERRQ(ierr);}
300196b79ebeSFlorian Wechsung     ierr = PetscFree(patch->dofMappingWithoutToWithArtificial);CHKERRQ(ierr);
300296b79ebeSFlorian Wechsung 
300396b79ebeSFlorian Wechsung   }
30040904074fSPatrick Farrell   if(patch->dofMappingWithoutToWithAll) {
30050904074fSPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->dofMappingWithoutToWithAll[i]);CHKERRQ(ierr);}
30060904074fSPatrick Farrell     ierr = PetscFree(patch->dofMappingWithoutToWithAll);CHKERRQ(ierr);
30070904074fSPatrick Farrell 
30080904074fSPatrick Farrell   }
30094bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr);
30105f824522SMatthew G. Knepley   if (patch->userIS) {
30115f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->userIS[i]);CHKERRQ(ierr);}
30125f824522SMatthew G. Knepley     ierr = PetscFree(patch->userIS);CHKERRQ(ierr);
30135f824522SMatthew G. Knepley   }
3014fe988be2SFlorian Wechsung   ierr = PetscFree(patch->precomputedTensorLocations);CHKERRQ(ierr);
3015f98464cbSLawrence Mitchell   ierr = PetscFree(patch->precomputedIntFacetTensorLocations);CHKERRQ(ierr);
3016f98464cbSLawrence Mitchell 
30174bbf5ea8SMatthew G. Knepley   patch->bs          = 0;
30184bbf5ea8SMatthew G. Knepley   patch->cellNodeMap = NULL;
30197974b488SMatthew G. Knepley   patch->nsubspaces  = 0;
30204bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->iterationSet);CHKERRQ(ierr);
30215f824522SMatthew G. Knepley 
30225f824522SMatthew G. Knepley   ierr = PetscViewerDestroy(&patch->viewerSection);CHKERRQ(ierr);
30234bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
30244bbf5ea8SMatthew G. Knepley }
30254bbf5ea8SMatthew G. Knepley 
3026dadc69c5SMatthew G. Knepley static PetscErrorCode PCDestroy_PATCH_Linear(PC pc)
30274bbf5ea8SMatthew G. Knepley {
30284bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
30294bbf5ea8SMatthew G. Knepley   PetscInt       i;
30304bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
30314bbf5ea8SMatthew G. Knepley 
30324bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3033dadc69c5SMatthew G. Knepley   if (patch->solver) {
3034dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = KSPDestroy((KSP *) &patch->solver[i]);CHKERRQ(ierr);}
3035dadc69c5SMatthew G. Knepley     ierr = PetscFree(patch->solver);CHKERRQ(ierr);
30364bbf5ea8SMatthew G. Knepley   }
3037dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
3038dadc69c5SMatthew G. Knepley }
3039dadc69c5SMatthew G. Knepley 
3040dadc69c5SMatthew G. Knepley static PetscErrorCode PCDestroy_PATCH(PC pc)
3041dadc69c5SMatthew G. Knepley {
3042dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
3043dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
3044dadc69c5SMatthew G. Knepley 
3045dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
3046dadc69c5SMatthew G. Knepley   ierr = PCReset_PATCH(pc);CHKERRQ(ierr);
3047dadc69c5SMatthew G. Knepley   ierr = (*patch->destroysolver)(pc);CHKERRQ(ierr);
30484bbf5ea8SMatthew G. Knepley   ierr = PetscFree(pc->data);CHKERRQ(ierr);
30494bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
30504bbf5ea8SMatthew G. Knepley }
30514bbf5ea8SMatthew G. Knepley 
30524bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetFromOptions_PATCH(PetscOptionItems *PetscOptionsObject, PC pc)
30534bbf5ea8SMatthew G. Knepley {
30544bbf5ea8SMatthew G. Knepley   PC_PATCH            *patch = (PC_PATCH *) pc->data;
30554bbf5ea8SMatthew G. Knepley   PCPatchConstructType patchConstructionType = PC_PATCH_STAR;
30565f824522SMatthew G. Knepley   char                 sub_mat_type[PETSC_MAX_PATH_LEN];
305710534d48SPatrick Farrell   char                 option[PETSC_MAX_PATH_LEN];
30585f824522SMatthew G. Knepley   const char          *prefix;
30594bbf5ea8SMatthew G. Knepley   PetscBool            flg, dimflg, codimflg;
30605f824522SMatthew G. Knepley   MPI_Comm             comm;
3061a48c39c8SPatrick Farrell   PetscInt            *ifields, nfields, k;
30624bbf5ea8SMatthew G. Knepley   PetscErrorCode       ierr;
306361c4b389SFlorian Wechsung   PCCompositeType loctype = PC_COMPOSITE_ADDITIVE;
30644bbf5ea8SMatthew G. Knepley 
30654bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
30665f824522SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) pc, &comm);CHKERRQ(ierr);
30675f824522SMatthew G. Knepley   ierr = PetscObjectGetOptionsPrefix((PetscObject) pc, &prefix);CHKERRQ(ierr);
306810534d48SPatrick Farrell   ierr = PetscOptionsHead(PetscOptionsObject, "Patch solver options");CHKERRQ(ierr);
306910534d48SPatrick Farrell 
30701b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_save_operators", patch->classname);CHKERRQ(ierr);
307110534d48SPatrick Farrell   ierr = PetscOptionsBool(option,  "Store all patch operators for lifetime of object?", "PCPatchSetSaveOperators", patch->save_operators, &patch->save_operators, &flg);CHKERRQ(ierr);
307210534d48SPatrick Farrell 
307389d5b078SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_precompute_element_tensors", patch->classname);CHKERRQ(ierr);
3074fa84ea4cSLawrence Mitchell   ierr = PetscOptionsBool(option,  "Compute each element tensor only once?", "PCPatchSetPrecomputeElementTensors", patch->precomputeElementTensors, &patch->precomputeElementTensors, &flg);CHKERRQ(ierr);
307589d5b078SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_partition_of_unity", patch->classname);CHKERRQ(ierr);
307610534d48SPatrick Farrell   ierr = PetscOptionsBool(option, "Weight contributions by dof multiplicity?", "PCPatchSetPartitionOfUnity", patch->partition_of_unity, &patch->partition_of_unity, &flg);CHKERRQ(ierr);
307710534d48SPatrick Farrell 
30781b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_local_type", patch->classname);CHKERRQ(ierr);
307910534d48SPatrick Farrell   ierr = PetscOptionsEnum(option,"Type of local solver composition (additive or multiplicative)","PCPatchSetLocalComposition",PCCompositeTypes,(PetscEnum)loctype,(PetscEnum*)&loctype,&flg);CHKERRQ(ierr);
308061c4b389SFlorian Wechsung   if(flg) { ierr = PCPatchSetLocalComposition(pc, loctype);CHKERRQ(ierr);}
308110534d48SPatrick Farrell 
30821b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_dim", patch->classname);CHKERRQ(ierr);
308310534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "What dimension of mesh point to construct patches by? (0 = vertices)", "PCPATCH", patch->dim, &patch->dim, &dimflg);CHKERRQ(ierr);
30841b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_codim", patch->classname);CHKERRQ(ierr);
308510534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "What co-dimension of mesh point to construct patches by? (0 = cells)", "PCPATCH", patch->codim, &patch->codim, &codimflg);CHKERRQ(ierr);
308615b861d2SVaclav Hapla   if (dimflg && codimflg) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Can only set one of dimension or co-dimension");
308710534d48SPatrick Farrell 
30881b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_type", patch->classname);CHKERRQ(ierr);
308910534d48SPatrick Farrell   ierr = PetscOptionsEnum(option, "How should the patches be constructed?", "PCPatchSetConstructType", PCPatchConstructTypes, (PetscEnum) patchConstructionType, (PetscEnum *) &patchConstructionType, &flg);CHKERRQ(ierr);
30904bbf5ea8SMatthew G. Knepley   if (flg) {ierr = PCPatchSetConstructType(pc, patchConstructionType, NULL, NULL);CHKERRQ(ierr);}
309110534d48SPatrick Farrell 
30921b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_vanka_dim", patch->classname);CHKERRQ(ierr);
309310534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "Topological dimension of entities for Vanka to ignore", "PCPATCH", patch->vankadim, &patch->vankadim, &flg);CHKERRQ(ierr);
309410534d48SPatrick Farrell 
30951b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_ignore_dim", patch->classname);CHKERRQ(ierr);
309610534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "Topological dimension of entities for completion to ignore", "PCPATCH", patch->ignoredim, &patch->ignoredim, &flg);CHKERRQ(ierr);
309710534d48SPatrick Farrell 
30981b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_pardecomp_overlap", patch->classname);CHKERRQ(ierr);
3099b525f888SPatrick Farrell   ierr = PetscOptionsInt(option, "What overlap should we use in construct type pardecomp?", "PCPATCH", patch->pardecomp_overlap, &patch->pardecomp_overlap, &flg);CHKERRQ(ierr);
3100b525f888SPatrick Farrell 
31011b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_sub_mat_type", patch->classname);CHKERRQ(ierr);
310210534d48SPatrick Farrell   ierr = PetscOptionsFList(option, "Matrix type for patch solves", "PCPatchSetSubMatType", MatList, NULL, sub_mat_type, PETSC_MAX_PATH_LEN, &flg);CHKERRQ(ierr);
31034bbf5ea8SMatthew G. Knepley   if (flg) {ierr = PCPatchSetSubMatType(pc, sub_mat_type);CHKERRQ(ierr);}
310410534d48SPatrick Farrell 
31051b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_symmetrise_sweep", patch->classname);CHKERRQ(ierr);
310610534d48SPatrick Farrell   ierr = PetscOptionsBool(option, "Go start->end, end->start?", "PCPATCH", patch->symmetrise_sweep, &patch->symmetrise_sweep, &flg);CHKERRQ(ierr);
3107e4c66b91SPatrick Farrell 
3108a48c39c8SPatrick Farrell   /* If the user has set the number of subspaces, use that for the buffer size,
3109a48c39c8SPatrick Farrell      otherwise use a large number */
3110a48c39c8SPatrick Farrell   if (patch->nsubspaces <= 0) {
3111a48c39c8SPatrick Farrell     nfields = 128;
3112a48c39c8SPatrick Farrell   } else {
3113a48c39c8SPatrick Farrell     nfields = patch->nsubspaces;
3114a48c39c8SPatrick Farrell   }
3115a48c39c8SPatrick Farrell   ierr = PetscMalloc1(nfields, &ifields);CHKERRQ(ierr);
31161b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exclude_subspaces", patch->classname);CHKERRQ(ierr);
311710534d48SPatrick Farrell   ierr = PetscOptionsGetIntArray(((PetscObject)pc)->options,((PetscObject)pc)->prefix,option,ifields,&nfields,&flg);CHKERRQ(ierr);
3118e4c66b91SPatrick 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");
3119e4c66b91SPatrick Farrell   if (flg) {
3120e4c66b91SPatrick Farrell     PetscHSetIClear(patch->subspaces_to_exclude);
312159b66c28SPatrick Farrell     for (k = 0; k < nfields; k++) {
3122e4c66b91SPatrick Farrell       PetscHSetIAdd(patch->subspaces_to_exclude, ifields[k]);
3123e4c66b91SPatrick Farrell     }
3124e4c66b91SPatrick Farrell   }
312559b66c28SPatrick Farrell   ierr = PetscFree(ifields);CHKERRQ(ierr);
31265f824522SMatthew G. Knepley 
3127fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_patches_view", patch->classname);CHKERRQ(ierr);
312810534d48SPatrick Farrell   ierr = PetscOptionsBool(option, "Print out information during patch construction", "PCPATCH", patch->viewPatches, &patch->viewPatches, &flg);CHKERRQ(ierr);
3129fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_cells_view", patch->classname);CHKERRQ(ierr);
313010534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerCells, &patch->formatCells, &patch->viewCells);CHKERRQ(ierr);
3131fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_interior_facets_view", patch->classname);CHKERRQ(ierr);
3132fbb82ab5SLawrence Mitchell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerIntFacets, &patch->formatIntFacets, &patch->viewIntFacets);CHKERRQ(ierr);
3133fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exterior_facets_view", patch->classname);CHKERRQ(ierr);
3134fbb82ab5SLawrence Mitchell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerExtFacets, &patch->formatExtFacets, &patch->viewExtFacets);CHKERRQ(ierr);
3135fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_points_view", patch->classname);CHKERRQ(ierr);
313610534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerPoints, &patch->formatPoints, &patch->viewPoints);CHKERRQ(ierr);
3137fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_section_view", patch->classname);CHKERRQ(ierr);
313810534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerSection, &patch->formatSection, &patch->viewSection);CHKERRQ(ierr);
3139fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_mat_view", patch->classname);CHKERRQ(ierr);
314010534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerMatrix, &patch->formatMatrix, &patch->viewMatrix);CHKERRQ(ierr);
31414bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsTail();CHKERRQ(ierr);
31425f824522SMatthew G. Knepley   patch->optionsSet = PETSC_TRUE;
31434bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
31444bbf5ea8SMatthew G. Knepley }
31454bbf5ea8SMatthew G. Knepley 
31464bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUpOnBlocks_PATCH(PC pc)
31474bbf5ea8SMatthew G. Knepley {
31484bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch = (PC_PATCH*) pc->data;
31494bbf5ea8SMatthew G. Knepley   KSPConvergedReason reason;
31504bbf5ea8SMatthew G. Knepley   PetscInt           i;
31514bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
31524bbf5ea8SMatthew G. Knepley 
31534bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3154a1eac568SLawrence Mitchell   if (!patch->save_operators) {
3155a1eac568SLawrence Mitchell     /* Can't do this here because the sub KSPs don't have an operator attached yet. */
3156a1eac568SLawrence Mitchell     PetscFunctionReturn(0);
3157a1eac568SLawrence Mitchell   }
31584bbf5ea8SMatthew G. Knepley   for (i = 0; i < patch->npatch; ++i) {
3159dadc69c5SMatthew G. Knepley     if (!((KSP) patch->solver[i])->setfromoptionscalled) {
3160dadc69c5SMatthew G. Knepley       ierr = KSPSetFromOptions((KSP) patch->solver[i]);CHKERRQ(ierr);
3161a1eac568SLawrence Mitchell     }
3162dadc69c5SMatthew G. Knepley     ierr = KSPSetUp((KSP) patch->solver[i]);CHKERRQ(ierr);
3163dadc69c5SMatthew G. Knepley     ierr = KSPGetConvergedReason((KSP) patch->solver[i], &reason);CHKERRQ(ierr);
3164c0decd05SBarry Smith     if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR;
31654bbf5ea8SMatthew G. Knepley   }
31664bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
31674bbf5ea8SMatthew G. Knepley }
31684bbf5ea8SMatthew G. Knepley 
31694bbf5ea8SMatthew G. Knepley static PetscErrorCode PCView_PATCH(PC pc, PetscViewer viewer)
31704bbf5ea8SMatthew G. Knepley {
31714bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
31724bbf5ea8SMatthew G. Knepley   PetscViewer    sviewer;
31734bbf5ea8SMatthew G. Knepley   PetscBool      isascii;
31744bbf5ea8SMatthew G. Knepley   PetscMPIInt    rank;
31754bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
31764bbf5ea8SMatthew G. Knepley 
31774bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
31784bbf5ea8SMatthew G. Knepley   /* TODO Redo tabbing with set tbas in new style */
31794bbf5ea8SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii);CHKERRQ(ierr);
31804bbf5ea8SMatthew G. Knepley   if (!isascii) PetscFunctionReturn(0);
31814bbf5ea8SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) pc), &rank);CHKERRQ(ierr);
31824bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
31834bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "Subspace Correction preconditioner with %d patches\n", patch->npatch);CHKERRQ(ierr);
318461c4b389SFlorian Wechsung   if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
3185c2e6f3c0SFlorian Wechsung     ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: multiplicative\n");CHKERRQ(ierr);
3186e047a90bSFlorian Wechsung   } else {
318773ec7555SLawrence Mitchell     ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: additive\n");CHKERRQ(ierr);
3188c2e6f3c0SFlorian Wechsung   }
31894bbf5ea8SMatthew G. Knepley   if (patch->partition_of_unity) {ierr = PetscViewerASCIIPrintf(viewer, "Weighting by partition of unity\n");CHKERRQ(ierr);}
31904bbf5ea8SMatthew G. Knepley   else                           {ierr = PetscViewerASCIIPrintf(viewer, "Not weighting by partition of unity\n");CHKERRQ(ierr);}
31914bbf5ea8SMatthew G. Knepley   if (patch->symmetrise_sweep) {ierr = PetscViewerASCIIPrintf(viewer, "Symmetrising sweep (start->end, then end->start)\n");CHKERRQ(ierr);}
31924bbf5ea8SMatthew G. Knepley   else                         {ierr = PetscViewerASCIIPrintf(viewer, "Not symmetrising sweep\n");CHKERRQ(ierr);}
3193fa84ea4cSLawrence Mitchell   if (!patch->precomputeElementTensors) {ierr = PetscViewerASCIIPrintf(viewer, "Not precomputing element tensors (overlapping cells rebuilt in every patch assembly)\n");CHKERRQ(ierr);}
3194fa84ea4cSLawrence Mitchell   else                            {ierr = PetscViewerASCIIPrintf(viewer, "Precomputing element tensors (each cell assembled only once)\n");CHKERRQ(ierr);}
31954bbf5ea8SMatthew G. Knepley   if (!patch->save_operators) {ierr = PetscViewerASCIIPrintf(viewer, "Not saving patch operators (rebuilt every PCApply)\n");CHKERRQ(ierr);}
31964bbf5ea8SMatthew G. Knepley   else                        {ierr = PetscViewerASCIIPrintf(viewer, "Saving patch operators (rebuilt every PCSetUp)\n");CHKERRQ(ierr);}
31974bbf5ea8SMatthew G. Knepley   if (patch->patchconstructop == PCPatchConstruct_Star)       {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: star\n");CHKERRQ(ierr);}
31984bbf5ea8SMatthew G. Knepley   else if (patch->patchconstructop == PCPatchConstruct_Vanka) {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: Vanka\n");CHKERRQ(ierr);}
31994bbf5ea8SMatthew G. Knepley   else if (patch->patchconstructop == PCPatchConstruct_User)  {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: user-specified\n");CHKERRQ(ierr);}
32004bbf5ea8SMatthew G. Knepley   else                                                        {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: unknown\n");CHKERRQ(ierr);}
32015d30859aSPatrick Farrell 
32025d30859aSPatrick Farrell   if (patch->isNonlinear) {
32035d30859aSPatrick Farrell     ierr = PetscViewerASCIIPrintf(viewer, "SNES on patches (all same):\n");CHKERRQ(ierr);
32045d30859aSPatrick Farrell   } else {
32055d30859aSPatrick Farrell     ierr = PetscViewerASCIIPrintf(viewer, "KSP on patches (all same):\n");CHKERRQ(ierr);
32065d30859aSPatrick Farrell   }
3207dadc69c5SMatthew G. Knepley   if (patch->solver) {
32084bbf5ea8SMatthew G. Knepley     ierr = PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr);
32094bbf5ea8SMatthew G. Knepley     if (!rank) {
32104bbf5ea8SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(sviewer);CHKERRQ(ierr);
3211dadc69c5SMatthew G. Knepley       ierr = PetscObjectView(patch->solver[0], sviewer);CHKERRQ(ierr);
32124bbf5ea8SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(sviewer);CHKERRQ(ierr);
32134bbf5ea8SMatthew G. Knepley     }
32144bbf5ea8SMatthew G. Knepley     ierr = PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr);
32154bbf5ea8SMatthew G. Knepley   } else {
32164bbf5ea8SMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
3217dadc69c5SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Solver not yet set.\n");CHKERRQ(ierr);
32184bbf5ea8SMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
32194bbf5ea8SMatthew G. Knepley   }
32204bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
32214bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
32224bbf5ea8SMatthew G. Knepley }
32234bbf5ea8SMatthew G. Knepley 
3224e5893cccSMatthew G. Knepley /*MC
322598ed095eSMatthew G. Knepley   PCPATCH - A PC object that encapsulates flexible definition of blocks for overlapping and non-overlapping
322698ed095eSMatthew G. Knepley             small block additive preconditioners. Block definition is based on topology from
3227e5893cccSMatthew G. Knepley             a DM and equation numbering from a PetscSection.
3228e5893cccSMatthew G. Knepley 
3229e5893cccSMatthew G. Knepley   Options Database Keys:
3230e5893cccSMatthew G. Knepley + -pc_patch_cells_view   - Views the process local cell numbers for each patch
3231e5893cccSMatthew G. Knepley . -pc_patch_points_view  - Views the process local mesh point numbers for each patch
3232e5893cccSMatthew G. Knepley . -pc_patch_g2l_view     - Views the map between global dofs and patch local dofs for each patch
3233e5893cccSMatthew G. Knepley . -pc_patch_patches_view - Views the global dofs associated with each patch and its boundary
3234e5893cccSMatthew G. Knepley - -pc_patch_sub_mat_view - Views the matrix associated with each patch
3235e5893cccSMatthew G. Knepley 
3236e5893cccSMatthew G. Knepley   Level: intermediate
3237e5893cccSMatthew G. Knepley 
3238e5893cccSMatthew G. Knepley .seealso: PCType, PCCreate(), PCSetType()
3239e5893cccSMatthew G. Knepley M*/
3240642283e9SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PCCreate_Patch(PC pc)
32414bbf5ea8SMatthew G. Knepley {
32424bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch;
32434bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
32444bbf5ea8SMatthew G. Knepley 
32454bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
32464bbf5ea8SMatthew G. Knepley   ierr = PetscNewLog(pc, &patch);CHKERRQ(ierr);
32474bbf5ea8SMatthew G. Knepley 
3248e4c66b91SPatrick Farrell   if (patch->subspaces_to_exclude) {
3249e4c66b91SPatrick Farrell     PetscHSetIDestroy(&patch->subspaces_to_exclude);
3250e4c66b91SPatrick Farrell   }
3251e4c66b91SPatrick Farrell   PetscHSetICreate(&patch->subspaces_to_exclude);
3252e4c66b91SPatrick Farrell 
325310534d48SPatrick Farrell   patch->classname = "pc";
3254debbdec3SPatrick Farrell   patch->isNonlinear = PETSC_FALSE;
325510534d48SPatrick Farrell 
32564bbf5ea8SMatthew G. Knepley   /* Set some defaults */
32575f824522SMatthew G. Knepley   patch->combined           = PETSC_FALSE;
32584bbf5ea8SMatthew G. Knepley   patch->save_operators     = PETSC_TRUE;
325961c4b389SFlorian Wechsung   patch->local_composition_type = PC_COMPOSITE_ADDITIVE;
3260fa84ea4cSLawrence Mitchell   patch->precomputeElementTensors = PETSC_FALSE;
32614bbf5ea8SMatthew G. Knepley   patch->partition_of_unity = PETSC_FALSE;
32624bbf5ea8SMatthew G. Knepley   patch->codim              = -1;
32634bbf5ea8SMatthew G. Knepley   patch->dim                = -1;
32644bbf5ea8SMatthew G. Knepley   patch->vankadim           = -1;
32655f824522SMatthew G. Knepley   patch->ignoredim          = -1;
3266b525f888SPatrick Farrell   patch->pardecomp_overlap  = 0;
32674bbf5ea8SMatthew G. Knepley   patch->patchconstructop   = PCPatchConstruct_Star;
32684bbf5ea8SMatthew G. Knepley   patch->symmetrise_sweep   = PETSC_FALSE;
32695f824522SMatthew G. Knepley   patch->npatch             = 0;
32704bbf5ea8SMatthew G. Knepley   patch->userIS             = NULL;
32715f824522SMatthew G. Knepley   patch->optionsSet         = PETSC_FALSE;
32724bbf5ea8SMatthew G. Knepley   patch->iterationSet       = NULL;
32734bbf5ea8SMatthew G. Knepley   patch->user_patches       = PETSC_FALSE;
32745f824522SMatthew G. Knepley   ierr = PetscStrallocpy(MATDENSE, (char **) &patch->sub_mat_type);CHKERRQ(ierr);
32755f824522SMatthew G. Knepley   patch->viewPatches        = PETSC_FALSE;
32765f824522SMatthew G. Knepley   patch->viewCells          = PETSC_FALSE;
32775f824522SMatthew G. Knepley   patch->viewPoints         = PETSC_FALSE;
32785f824522SMatthew G. Knepley   patch->viewSection        = PETSC_FALSE;
32795f824522SMatthew G. Knepley   patch->viewMatrix         = PETSC_FALSE;
3280dadc69c5SMatthew G. Knepley   patch->setupsolver        = PCSetUp_PATCH_Linear;
3281dadc69c5SMatthew G. Knepley   patch->applysolver        = PCApply_PATCH_Linear;
3282dadc69c5SMatthew G. Knepley   patch->resetsolver        = PCReset_PATCH_Linear;
3283dadc69c5SMatthew G. Knepley   patch->destroysolver      = PCDestroy_PATCH_Linear;
32846c9c532dSPatrick Farrell   patch->updatemultiplicative = PCUpdateMultiplicative_PATCH_Linear;
328547aca4a6SPatrick Farrell   patch->dofMappingWithoutToWithArtificial = NULL;
328647aca4a6SPatrick Farrell   patch->dofMappingWithoutToWithAll = NULL;
32874bbf5ea8SMatthew G. Knepley 
32884bbf5ea8SMatthew G. Knepley   pc->data                 = (void *) patch;
32894bbf5ea8SMatthew G. Knepley   pc->ops->apply           = PCApply_PATCH;
32904bbf5ea8SMatthew G. Knepley   pc->ops->applytranspose  = 0; /* PCApplyTranspose_PATCH; */
32914bbf5ea8SMatthew G. Knepley   pc->ops->setup           = PCSetUp_PATCH;
32924bbf5ea8SMatthew G. Knepley   pc->ops->reset           = PCReset_PATCH;
32934bbf5ea8SMatthew G. Knepley   pc->ops->destroy         = PCDestroy_PATCH;
32944bbf5ea8SMatthew G. Knepley   pc->ops->setfromoptions  = PCSetFromOptions_PATCH;
32954bbf5ea8SMatthew G. Knepley   pc->ops->setuponblocks   = PCSetUpOnBlocks_PATCH;
32964bbf5ea8SMatthew G. Knepley   pc->ops->view            = PCView_PATCH;
32974bbf5ea8SMatthew G. Knepley   pc->ops->applyrichardson = 0;
32984bbf5ea8SMatthew G. Knepley 
32994bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
33004bbf5ea8SMatthew G. Knepley }
3301