xref: /petsc/src/ksp/pc/impls/patch/pcpatch.c (revision de2d1767c214326e03a21b668228eb778a3eaa87)
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   DM              plex;
870a390943SPatrick Farrell   PetscBool       flg;
88b525f888SPatrick Farrell   PetscInt        starSize;
89b525f888SPatrick Farrell   PetscInt       *star = NULL;
9025fd193aSPatrick Farrell   PetscInt        opoint, overlapi;
910a390943SPatrick Farrell   PetscErrorCode  ierr;
920a390943SPatrick Farrell 
930a390943SPatrick Farrell   PetscFunctionBegin;
940a390943SPatrick Farrell   PetscHSetIClear(ht);
950a390943SPatrick Farrell 
960a390943SPatrick Farrell   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
970a390943SPatrick Farrell   ierr = DMPlexGetChart(plex, &pStart, &pEnd);CHKERRQ(ierr);
980a390943SPatrick Farrell 
990a390943SPatrick Farrell   ierr = DMHasLabel(dm, "pyop2_ghost", &isFiredrake);CHKERRQ(ierr);
1000a390943SPatrick Farrell   if (isFiredrake) {
1010a390943SPatrick Farrell     ierr = DMGetLabel(dm, "pyop2_ghost", &ghost);CHKERRQ(ierr);
1020a390943SPatrick Farrell     ierr = DMLabelCreateIndex(ghost, pStart, pEnd);CHKERRQ(ierr);
1030a390943SPatrick Farrell   } else {
1040a390943SPatrick Farrell     PetscSF sf;
1050a390943SPatrick Farrell     ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
1060a390943SPatrick Farrell     ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
1070a390943SPatrick Farrell     nleaves = PetscMax(nleaves, 0);
1080a390943SPatrick Farrell   }
1090a390943SPatrick Farrell 
11025fd193aSPatrick Farrell   for (opoint = pStart; opoint < pEnd; ++opoint) {
111bfabdd78SPatrick Farrell     if (ghost) {ierr = DMLabelHasPoint(ghost, opoint, &flg);CHKERRQ(ierr);}
112bfabdd78SPatrick Farrell     else       {ierr = PetscFindInt(opoint, nleaves, leaves, &loc);CHKERRQ(ierr); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;}
1130a390943SPatrick Farrell     /* Not an owned entity, don't make a cell patch. */
1140a390943SPatrick Farrell     if (flg) continue;
115bfabdd78SPatrick Farrell     ierr = PetscHSetIAdd(ht, opoint);CHKERRQ(ierr);
1160a390943SPatrick Farrell   }
1170a390943SPatrick Farrell 
118b525f888SPatrick Farrell   /* Now build the overlap for the patch */
11925fd193aSPatrick Farrell   for (overlapi = 0; overlapi < patch->pardecomp_overlap; ++overlapi) {
120b525f888SPatrick Farrell     PetscInt index = 0;
121b525f888SPatrick Farrell     PetscInt *htpoints = NULL;
122b525f888SPatrick Farrell     PetscInt htsize;
12325fd193aSPatrick Farrell     PetscInt i;
124b525f888SPatrick Farrell 
125b525f888SPatrick Farrell     ierr = PetscHSetIGetSize(ht, &htsize);CHKERRQ(ierr);
126b525f888SPatrick Farrell     ierr = PetscMalloc1(htsize, &htpoints);CHKERRQ(ierr);
127b525f888SPatrick Farrell     ierr = PetscHSetIGetElems(ht, &index, htpoints);CHKERRQ(ierr);
128b525f888SPatrick Farrell 
12925fd193aSPatrick Farrell     for (i = 0; i < htsize; ++i) {
13025fd193aSPatrick Farrell       PetscInt hpoint = htpoints[i];
13125fd193aSPatrick Farrell       PetscInt si;
132b525f888SPatrick Farrell 
13325fd193aSPatrick Farrell       ierr = DMPlexGetTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
13425fd193aSPatrick Farrell       for (si = 0; si < starSize*2; si += 2) {
135b525f888SPatrick Farrell         const PetscInt starp = star[si];
136b525f888SPatrick Farrell         PetscInt       closureSize;
137b525f888SPatrick Farrell         PetscInt      *closure = NULL, ci;
138b525f888SPatrick Farrell 
139b525f888SPatrick Farrell         /* now loop over all entities in the closure of starp */
140b525f888SPatrick Farrell         ierr = DMPlexGetTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
14125fd193aSPatrick Farrell         for (ci = 0; ci < closureSize*2; ci += 2) {
142b525f888SPatrick Farrell           const PetscInt closstarp = closure[ci];
143b525f888SPatrick Farrell           ierr = PetscHSetIAdd(ht, closstarp);CHKERRQ(ierr);
144b525f888SPatrick Farrell         }
145b525f888SPatrick Farrell         ierr = DMPlexRestoreTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
146b525f888SPatrick Farrell       }
14725fd193aSPatrick Farrell       ierr = DMPlexRestoreTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
148b525f888SPatrick Farrell     }
149b525f888SPatrick Farrell     ierr = PetscFree(htpoints);CHKERRQ(ierr);
150b525f888SPatrick Farrell   }
151b525f888SPatrick Farrell 
1520a390943SPatrick Farrell   PetscFunctionReturn(0);
1530a390943SPatrick Farrell }
1540a390943SPatrick Farrell 
1554bbf5ea8SMatthew G. Knepley /* The user's already set the patches in patch->userIS. Build the hash tables */
1561b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_User(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
1574bbf5ea8SMatthew G. Knepley {
1584bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch   = (PC_PATCH *) vpatch;
1594bbf5ea8SMatthew G. Knepley   IS              patchis = patch->userIS[point];
1604bbf5ea8SMatthew G. Knepley   PetscInt        n;
1614bbf5ea8SMatthew G. Knepley   const PetscInt *patchdata;
1624bbf5ea8SMatthew G. Knepley   PetscInt        pStart, pEnd, i;
1634bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
1644bbf5ea8SMatthew G. Knepley 
1654bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1661b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(ht);CHKERRQ(ierr);
1671b68eb51SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
1684bbf5ea8SMatthew G. Knepley   ierr = ISGetLocalSize(patchis, &n);CHKERRQ(ierr);
1694bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patchis, &patchdata);CHKERRQ(ierr);
1704bbf5ea8SMatthew G. Knepley   for (i = 0; i < n; ++i) {
1714bbf5ea8SMatthew G. Knepley     const PetscInt ownedpoint = patchdata[i];
1724bbf5ea8SMatthew G. Knepley 
1734bbf5ea8SMatthew G. Knepley     if (ownedpoint < pStart || ownedpoint >= pEnd) {
1744bbf5ea8SMatthew G. Knepley       SETERRQ3(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D was not in [%D, %D)", ownedpoint, pStart, pEnd);
1754bbf5ea8SMatthew G. Knepley     }
1761b68eb51SMatthew G. Knepley     ierr = PetscHSetIAdd(ht, ownedpoint);CHKERRQ(ierr);
1774bbf5ea8SMatthew G. Knepley   }
1784bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patchis, &patchdata);CHKERRQ(ierr);
1794bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
1804bbf5ea8SMatthew G. Knepley }
1814bbf5ea8SMatthew G. Knepley 
1824bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateDefaultSF_Private(PC pc, PetscInt n, const PetscSF *sf, const PetscInt *bs)
1834bbf5ea8SMatthew G. Knepley {
1844bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
1854bbf5ea8SMatthew G. Knepley   PetscInt       i;
1864bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
1874bbf5ea8SMatthew G. Knepley 
1884bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1894bbf5ea8SMatthew G. Knepley   if (n == 1 && bs[0] == 1) {
1904bbf5ea8SMatthew G. Knepley     patch->defaultSF = sf[0];
1914bbf5ea8SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) patch->defaultSF);CHKERRQ(ierr);
1924bbf5ea8SMatthew G. Knepley   } else {
1934bbf5ea8SMatthew G. Knepley     PetscInt     allRoots = 0, allLeaves = 0;
1944bbf5ea8SMatthew G. Knepley     PetscInt     leafOffset = 0;
1954bbf5ea8SMatthew G. Knepley     PetscInt    *ilocal = NULL;
1964bbf5ea8SMatthew G. Knepley     PetscSFNode *iremote = NULL;
1974bbf5ea8SMatthew G. Knepley     PetscInt    *remoteOffsets = NULL;
1984bbf5ea8SMatthew G. Knepley     PetscInt     index = 0;
1991b68eb51SMatthew G. Knepley     PetscHMapI   rankToIndex;
2004bbf5ea8SMatthew G. Knepley     PetscInt     numRanks = 0;
2014bbf5ea8SMatthew G. Knepley     PetscSFNode *remote = NULL;
2024bbf5ea8SMatthew G. Knepley     PetscSF      rankSF;
2034bbf5ea8SMatthew G. Knepley     PetscInt    *ranks = NULL;
2044bbf5ea8SMatthew G. Knepley     PetscInt    *offsets = NULL;
2054bbf5ea8SMatthew G. Knepley     MPI_Datatype contig;
2061b68eb51SMatthew G. Knepley     PetscHSetI   ranksUniq;
2074bbf5ea8SMatthew G. Knepley 
2084bbf5ea8SMatthew G. Knepley     /* First figure out how many dofs there are in the concatenated numbering.
2094bbf5ea8SMatthew G. Knepley      * allRoots: number of owned global dofs;
2104bbf5ea8SMatthew G. Knepley      * allLeaves: number of visible dofs (global + ghosted).
2114bbf5ea8SMatthew G. Knepley      */
2124bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2134bbf5ea8SMatthew G. Knepley       PetscInt nroots, nleaves;
2144bbf5ea8SMatthew G. Knepley 
2154bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetGraph(sf[i], &nroots, &nleaves, NULL, NULL);CHKERRQ(ierr);
2164bbf5ea8SMatthew G. Knepley       allRoots  += nroots * bs[i];
2174bbf5ea8SMatthew G. Knepley       allLeaves += nleaves * bs[i];
2184bbf5ea8SMatthew G. Knepley     }
2194bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(allLeaves, &ilocal);CHKERRQ(ierr);
2204bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(allLeaves, &iremote);CHKERRQ(ierr);
2214bbf5ea8SMatthew G. Knepley     /* Now build an SF that just contains process connectivity. */
2221b68eb51SMatthew G. Knepley     ierr = PetscHSetICreate(&ranksUniq);CHKERRQ(ierr);
2234bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2244bbf5ea8SMatthew G. Knepley       const PetscMPIInt *ranks = NULL;
2254bbf5ea8SMatthew G. Knepley       PetscInt           nranks, j;
2264bbf5ea8SMatthew G. Knepley 
2274bbf5ea8SMatthew G. Knepley       ierr = PetscSFSetUp(sf[i]);CHKERRQ(ierr);
2284bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetRanks(sf[i], &nranks, &ranks, NULL, NULL, NULL);CHKERRQ(ierr);
2294bbf5ea8SMatthew G. Knepley       /* These are all the ranks who communicate with me. */
2304bbf5ea8SMatthew G. Knepley       for (j = 0; j < nranks; ++j) {
2311b68eb51SMatthew G. Knepley         ierr = PetscHSetIAdd(ranksUniq, (PetscInt) ranks[j]);CHKERRQ(ierr);
2324bbf5ea8SMatthew G. Knepley       }
2334bbf5ea8SMatthew G. Knepley     }
2341b68eb51SMatthew G. Knepley     ierr = PetscHSetIGetSize(ranksUniq, &numRanks);CHKERRQ(ierr);
2354bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(numRanks, &remote);CHKERRQ(ierr);
2364bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(numRanks, &ranks);CHKERRQ(ierr);
2371b68eb51SMatthew G. Knepley     ierr = PetscHSetIGetElems(ranksUniq, &index, ranks);CHKERRQ(ierr);
2384bbf5ea8SMatthew G. Knepley 
2391b68eb51SMatthew G. Knepley     ierr = PetscHMapICreate(&rankToIndex);CHKERRQ(ierr);
2404bbf5ea8SMatthew G. Knepley     for (i = 0; i < numRanks; ++i) {
2414bbf5ea8SMatthew G. Knepley       remote[i].rank  = ranks[i];
2424bbf5ea8SMatthew G. Knepley       remote[i].index = 0;
2431b68eb51SMatthew G. Knepley       ierr = PetscHMapISet(rankToIndex, ranks[i], i);CHKERRQ(ierr);
2444bbf5ea8SMatthew G. Knepley     }
2454bbf5ea8SMatthew G. Knepley     ierr = PetscFree(ranks);CHKERRQ(ierr);
2461b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&ranksUniq);CHKERRQ(ierr);
2474bbf5ea8SMatthew G. Knepley     ierr = PetscSFCreate(PetscObjectComm((PetscObject) pc), &rankSF);CHKERRQ(ierr);
2484bbf5ea8SMatthew G. Knepley     ierr = PetscSFSetGraph(rankSF, 1, numRanks, NULL, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
2494bbf5ea8SMatthew G. Knepley     ierr = PetscSFSetUp(rankSF);CHKERRQ(ierr);
2504bbf5ea8SMatthew G. Knepley     /* OK, use it to communicate the root offset on the remote
2514bbf5ea8SMatthew G. Knepley      * processes for each subspace. */
2524bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(n, &offsets);CHKERRQ(ierr);
2534bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(n*numRanks, &remoteOffsets);CHKERRQ(ierr);
2544bbf5ea8SMatthew G. Knepley 
2554bbf5ea8SMatthew G. Knepley     offsets[0] = 0;
2564bbf5ea8SMatthew G. Knepley     for (i = 1; i < n; ++i) {
2574bbf5ea8SMatthew G. Knepley       PetscInt nroots;
2584bbf5ea8SMatthew G. Knepley 
2594bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetGraph(sf[i-1], &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
2604bbf5ea8SMatthew G. Knepley       offsets[i] = offsets[i-1] + nroots*bs[i-1];
2614bbf5ea8SMatthew G. Knepley     }
2624bbf5ea8SMatthew G. Knepley     /* Offsets are the offsets on the current process of the
2634bbf5ea8SMatthew G. Knepley      * global dof numbering for the subspaces. */
2644bbf5ea8SMatthew G. Knepley     ierr = MPI_Type_contiguous(n, MPIU_INT, &contig);CHKERRQ(ierr);
2654bbf5ea8SMatthew G. Knepley     ierr = MPI_Type_commit(&contig);CHKERRQ(ierr);
2664bbf5ea8SMatthew G. Knepley 
2674bbf5ea8SMatthew G. Knepley     ierr = PetscSFBcastBegin(rankSF, contig, offsets, remoteOffsets);CHKERRQ(ierr);
2684bbf5ea8SMatthew G. Knepley     ierr = PetscSFBcastEnd(rankSF, contig, offsets, remoteOffsets);CHKERRQ(ierr);
2694bbf5ea8SMatthew G. Knepley     ierr = MPI_Type_free(&contig);CHKERRQ(ierr);
2704bbf5ea8SMatthew G. Knepley     ierr = PetscFree(offsets);CHKERRQ(ierr);
2714bbf5ea8SMatthew G. Knepley     ierr = PetscSFDestroy(&rankSF);CHKERRQ(ierr);
2724bbf5ea8SMatthew G. Knepley     /* Now remoteOffsets contains the offsets on the remote
2734bbf5ea8SMatthew G. Knepley      * processes who communicate with me.  So now we can
2744bbf5ea8SMatthew G. Knepley      * concatenate the list of SFs into a single one. */
2754bbf5ea8SMatthew G. Knepley     index = 0;
2764bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2774bbf5ea8SMatthew G. Knepley       const PetscSFNode *remote = NULL;
2784bbf5ea8SMatthew G. Knepley       const PetscInt    *local  = NULL;
2794bbf5ea8SMatthew G. Knepley       PetscInt           nroots, nleaves, j;
2804bbf5ea8SMatthew G. Knepley 
2814bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetGraph(sf[i], &nroots, &nleaves, &local, &remote);CHKERRQ(ierr);
2824bbf5ea8SMatthew G. Knepley       for (j = 0; j < nleaves; ++j) {
2834bbf5ea8SMatthew G. Knepley         PetscInt rank = remote[j].rank;
2844bbf5ea8SMatthew G. Knepley         PetscInt idx, rootOffset, k;
2854bbf5ea8SMatthew G. Knepley 
2861b68eb51SMatthew G. Knepley         ierr = PetscHMapIGet(rankToIndex, rank, &idx);CHKERRQ(ierr);
2874bbf5ea8SMatthew G. Knepley         if (idx == -1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Didn't find rank, huh?");
2884bbf5ea8SMatthew G. Knepley         /* Offset on given rank for ith subspace */
2894bbf5ea8SMatthew G. Knepley         rootOffset = remoteOffsets[n*idx + i];
2904bbf5ea8SMatthew G. Knepley         for (k = 0; k < bs[i]; ++k) {
29173ec7555SLawrence Mitchell           ilocal[index]        = (local ? local[j] : j)*bs[i] + k + leafOffset;
2924bbf5ea8SMatthew G. Knepley           iremote[index].rank  = remote[j].rank;
2934bbf5ea8SMatthew G. Knepley           iremote[index].index = remote[j].index*bs[i] + k + rootOffset;
2944bbf5ea8SMatthew G. Knepley           ++index;
2954bbf5ea8SMatthew G. Knepley         }
2964bbf5ea8SMatthew G. Knepley       }
2974bbf5ea8SMatthew G. Knepley       leafOffset += nleaves * bs[i];
2984bbf5ea8SMatthew G. Knepley     }
2991b68eb51SMatthew G. Knepley     ierr = PetscHMapIDestroy(&rankToIndex);CHKERRQ(ierr);
3004bbf5ea8SMatthew G. Knepley     ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
3014bbf5ea8SMatthew G. Knepley     ierr = PetscSFCreate(PetscObjectComm((PetscObject)pc), &patch->defaultSF);CHKERRQ(ierr);
3024bbf5ea8SMatthew G. Knepley     ierr = PetscSFSetGraph(patch->defaultSF, allRoots, allLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER);CHKERRQ(ierr);
3034bbf5ea8SMatthew G. Knepley   }
3044bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3054bbf5ea8SMatthew G. Knepley }
3064bbf5ea8SMatthew G. Knepley 
3074bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3085f824522SMatthew G. Knepley PetscErrorCode PCPatchSetIgnoreDim(PC pc, PetscInt dim)
3095f824522SMatthew G. Knepley {
3105f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3115f824522SMatthew G. Knepley   PetscFunctionBegin;
3125f824522SMatthew G. Knepley   patch->ignoredim = dim;
3135f824522SMatthew G. Knepley   PetscFunctionReturn(0);
3145f824522SMatthew G. Knepley }
3155f824522SMatthew G. Knepley 
3165f824522SMatthew G. Knepley /* TODO: Docs */
3175f824522SMatthew G. Knepley PetscErrorCode PCPatchGetIgnoreDim(PC pc, PetscInt *dim)
3185f824522SMatthew G. Knepley {
3195f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3205f824522SMatthew G. Knepley   PetscFunctionBegin;
3215f824522SMatthew G. Knepley   *dim = patch->ignoredim;
3225f824522SMatthew G. Knepley   PetscFunctionReturn(0);
3235f824522SMatthew G. Knepley }
3245f824522SMatthew G. Knepley 
3255f824522SMatthew G. Knepley /* TODO: Docs */
3264bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetSaveOperators(PC pc, PetscBool flg)
3274bbf5ea8SMatthew G. Knepley {
3284bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3294bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3304bbf5ea8SMatthew G. Knepley   patch->save_operators = flg;
3314bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3324bbf5ea8SMatthew G. Knepley }
3334bbf5ea8SMatthew G. Knepley 
3344bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3354bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetSaveOperators(PC pc, PetscBool *flg)
3364bbf5ea8SMatthew G. Knepley {
3374bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3384bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3394bbf5ea8SMatthew G. Knepley   *flg = patch->save_operators;
3404bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3414bbf5ea8SMatthew G. Knepley }
3424bbf5ea8SMatthew G. Knepley 
3434bbf5ea8SMatthew G. Knepley /* TODO: Docs */
344fa84ea4cSLawrence Mitchell PetscErrorCode PCPatchSetPrecomputeElementTensors(PC pc, PetscBool flg)
345fa84ea4cSLawrence Mitchell {
346fa84ea4cSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *) pc->data;
347fa84ea4cSLawrence Mitchell   PetscFunctionBegin;
348fa84ea4cSLawrence Mitchell   patch->precomputeElementTensors = flg;
349fa84ea4cSLawrence Mitchell   PetscFunctionReturn(0);
350fa84ea4cSLawrence Mitchell }
351fa84ea4cSLawrence Mitchell 
352fa84ea4cSLawrence Mitchell /* TODO: Docs */
353fa84ea4cSLawrence Mitchell PetscErrorCode PCPatchGetPrecomputeElementTensors(PC pc, PetscBool *flg)
354fa84ea4cSLawrence Mitchell {
355fa84ea4cSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *) pc->data;
356fa84ea4cSLawrence Mitchell   PetscFunctionBegin;
357fa84ea4cSLawrence Mitchell   *flg = patch->precomputeElementTensors;
358fa84ea4cSLawrence Mitchell   PetscFunctionReturn(0);
359fa84ea4cSLawrence Mitchell }
360fa84ea4cSLawrence Mitchell 
361fa84ea4cSLawrence Mitchell /* TODO: Docs */
3624bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetPartitionOfUnity(PC pc, PetscBool flg)
3634bbf5ea8SMatthew G. Knepley {
3644bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3654bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3664bbf5ea8SMatthew G. Knepley   patch->partition_of_unity = flg;
3674bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3684bbf5ea8SMatthew G. Knepley }
3694bbf5ea8SMatthew G. Knepley 
3704bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3714bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetPartitionOfUnity(PC pc, PetscBool *flg)
3724bbf5ea8SMatthew G. Knepley {
3734bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3744bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3754bbf5ea8SMatthew G. Knepley   *flg = patch->partition_of_unity;
3764bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3774bbf5ea8SMatthew G. Knepley }
3784bbf5ea8SMatthew G. Knepley 
3794bbf5ea8SMatthew G. Knepley /* TODO: Docs */
38061c4b389SFlorian Wechsung PetscErrorCode PCPatchSetLocalComposition(PC pc, PCCompositeType type)
381c2e6f3c0SFlorian Wechsung {
382c2e6f3c0SFlorian Wechsung   PC_PATCH *patch = (PC_PATCH *) pc->data;
383c2e6f3c0SFlorian Wechsung   PetscFunctionBegin;
38461c4b389SFlorian 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");
38561c4b389SFlorian Wechsung   patch->local_composition_type = type;
386c2e6f3c0SFlorian Wechsung   PetscFunctionReturn(0);
387c2e6f3c0SFlorian Wechsung }
388c2e6f3c0SFlorian Wechsung 
389c2e6f3c0SFlorian Wechsung /* TODO: Docs */
39061c4b389SFlorian Wechsung PetscErrorCode PCPatchGetLocalComposition(PC pc, PCCompositeType *type)
391c2e6f3c0SFlorian Wechsung {
392c2e6f3c0SFlorian Wechsung   PC_PATCH *patch = (PC_PATCH *) pc->data;
393c2e6f3c0SFlorian Wechsung   PetscFunctionBegin;
39461c4b389SFlorian Wechsung   *type = patch->local_composition_type;
395c2e6f3c0SFlorian Wechsung   PetscFunctionReturn(0);
396c2e6f3c0SFlorian Wechsung }
397c2e6f3c0SFlorian Wechsung 
398c2e6f3c0SFlorian Wechsung /* TODO: Docs */
3994bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetSubMatType(PC pc, MatType sub_mat_type)
4004bbf5ea8SMatthew G. Knepley {
4014bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
4024bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
4034bbf5ea8SMatthew G. Knepley 
4044bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4054bbf5ea8SMatthew G. Knepley   if (patch->sub_mat_type) {ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr);}
4064bbf5ea8SMatthew G. Knepley   ierr = PetscStrallocpy(sub_mat_type, (char **) &patch->sub_mat_type);CHKERRQ(ierr);
4074bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4084bbf5ea8SMatthew G. Knepley }
4094bbf5ea8SMatthew G. Knepley 
4104bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4114bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetSubMatType(PC pc, MatType *sub_mat_type)
4124bbf5ea8SMatthew G. Knepley {
4134bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4144bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4154bbf5ea8SMatthew G. Knepley   *sub_mat_type = patch->sub_mat_type;
4164bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4174bbf5ea8SMatthew G. Knepley }
4184bbf5ea8SMatthew G. Knepley 
4194bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4204bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetCellNumbering(PC pc, PetscSection cellNumbering)
4214bbf5ea8SMatthew G. Knepley {
4224bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
4234bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
4244bbf5ea8SMatthew G. Knepley 
4254bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4264bbf5ea8SMatthew G. Knepley   patch->cellNumbering = cellNumbering;
4274bbf5ea8SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) cellNumbering);CHKERRQ(ierr);
4284bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4294bbf5ea8SMatthew G. Knepley }
4304bbf5ea8SMatthew G. Knepley 
4314bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4324bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetCellNumbering(PC pc, PetscSection *cellNumbering)
4334bbf5ea8SMatthew G. Knepley {
4344bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4354bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4364bbf5ea8SMatthew G. Knepley   *cellNumbering = patch->cellNumbering;
4374bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4384bbf5ea8SMatthew G. Knepley }
4394bbf5ea8SMatthew G. Knepley 
4404bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4414bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetConstructType(PC pc, PCPatchConstructType ctype, PetscErrorCode (*func)(PC, PetscInt *, IS **, IS *, void *), void *ctx)
4424bbf5ea8SMatthew G. Knepley {
4434bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4444bbf5ea8SMatthew G. Knepley 
4454bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4464bbf5ea8SMatthew G. Knepley   patch->ctype = ctype;
4474bbf5ea8SMatthew G. Knepley   switch (ctype) {
4484bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
44940c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
4504bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Star;
4514bbf5ea8SMatthew G. Knepley     break;
4524bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
45340c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
4544bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Vanka;
4554bbf5ea8SMatthew G. Knepley     break;
456e5b9877fSPatrick Farrell   case PC_PATCH_PARDECOMP:
4570a390943SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
458e5b9877fSPatrick Farrell     patch->patchconstructop = PCPatchConstruct_Pardecomp;
4590a390943SPatrick Farrell     break;
4604bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
4614bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
4624bbf5ea8SMatthew G. Knepley     patch->user_patches     = PETSC_TRUE;
4634bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_User;
464bdd9e0cdSPatrick Farrell     if (func) {
4654bbf5ea8SMatthew G. Knepley       patch->userpatchconstructionop = func;
4664bbf5ea8SMatthew G. Knepley       patch->userpatchconstructctx   = ctx;
467bdd9e0cdSPatrick Farrell     }
4684bbf5ea8SMatthew G. Knepley     break;
4694bbf5ea8SMatthew G. Knepley   default:
4704bbf5ea8SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype);
4714bbf5ea8SMatthew G. Knepley   }
4724bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4734bbf5ea8SMatthew G. Knepley }
4744bbf5ea8SMatthew G. Knepley 
4754bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4764bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetConstructType(PC pc, PCPatchConstructType *ctype, PetscErrorCode (**func)(PC, PetscInt *, IS **, IS *, void *), void **ctx)
4774bbf5ea8SMatthew G. Knepley {
4784bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4794bbf5ea8SMatthew G. Knepley 
4804bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4814bbf5ea8SMatthew G. Knepley   *ctype = patch->ctype;
4824bbf5ea8SMatthew G. Knepley   switch (patch->ctype) {
4834bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
4844bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
485e5b9877fSPatrick Farrell   case PC_PATCH_PARDECOMP:
4864bbf5ea8SMatthew G. Knepley     break;
4874bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
4884bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
4894bbf5ea8SMatthew G. Knepley     *func = patch->userpatchconstructionop;
4904bbf5ea8SMatthew G. Knepley     *ctx  = patch->userpatchconstructctx;
4914bbf5ea8SMatthew G. Knepley     break;
4924bbf5ea8SMatthew G. Knepley   default:
4934bbf5ea8SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype);
4944bbf5ea8SMatthew G. Knepley   }
4954bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4964bbf5ea8SMatthew G. Knepley }
4974bbf5ea8SMatthew G. Knepley 
4984bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4994bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfo(PC pc, PetscInt nsubspaces, DM *dms, PetscInt *bs, PetscInt *nodesPerCell, const PetscInt **cellNodeMap,
5004bbf5ea8SMatthew G. Knepley                                             const PetscInt *subspaceOffsets, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
5014bbf5ea8SMatthew G. Knepley {
5024bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
5035f824522SMatthew G. Knepley   DM             dm;
5044bbf5ea8SMatthew G. Knepley   PetscSF       *sfs;
5055f824522SMatthew G. Knepley   PetscInt       cStart, cEnd, i, j;
5064bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
5074bbf5ea8SMatthew G. Knepley 
5084bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
5095f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
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);
5374bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5384bbf5ea8SMatthew G. Knepley }
5394bbf5ea8SMatthew G. Knepley 
5404bbf5ea8SMatthew G. Knepley /* TODO: Docs */
5415f824522SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfoCombined(PC pc, DM dm, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
5425f824522SMatthew G. Knepley {
5435f824522SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
5445f824522SMatthew G. Knepley   PetscInt       cStart, cEnd, i, j;
5455f824522SMatthew G. Knepley   PetscErrorCode ierr;
5465f824522SMatthew G. Knepley 
5475f824522SMatthew G. Knepley   PetscFunctionBegin;
5485f824522SMatthew G. Knepley   patch->combined = PETSC_TRUE;
5495f824522SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
5505f824522SMatthew G. Knepley   ierr = DMGetNumFields(dm, &patch->nsubspaces);CHKERRQ(ierr);
5515f824522SMatthew G. Knepley   ierr = PetscCalloc1(patch->nsubspaces, &patch->dofSection);CHKERRQ(ierr);
5525f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->bs);CHKERRQ(ierr);
5535f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->nodesPerCell);CHKERRQ(ierr);
5545f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->cellNodeMap);CHKERRQ(ierr);
5555f824522SMatthew G. Knepley   ierr = PetscCalloc1(patch->nsubspaces+1, &patch->subspaceOffsets);CHKERRQ(ierr);
5565f824522SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &patch->dofSection[0]);CHKERRQ(ierr);
5575f824522SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) patch->dofSection[0]);CHKERRQ(ierr);
5585f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(patch->dofSection[0], &patch->subspaceOffsets[patch->nsubspaces]);CHKERRQ(ierr);
5595f824522SMatthew G. Knepley   patch->totalDofsPerCell = 0;
5605f824522SMatthew G. Knepley   for (i = 0; i < patch->nsubspaces; ++i) {
5615f824522SMatthew G. Knepley     patch->bs[i]             = 1;
5625f824522SMatthew G. Knepley     patch->nodesPerCell[i]   = nodesPerCell[i];
5635f824522SMatthew G. Knepley     patch->totalDofsPerCell += nodesPerCell[i];
5645f824522SMatthew G. Knepley     ierr = PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i]);CHKERRQ(ierr);
5655f824522SMatthew G. Knepley     for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
5665f824522SMatthew G. Knepley   }
5675f824522SMatthew G. Knepley   ierr = DMGetDefaultSF(dm, &patch->defaultSF);CHKERRQ(ierr);
5685f824522SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) patch->defaultSF);CHKERRQ(ierr);
5695f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes);CHKERRQ(ierr);
5705f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes);CHKERRQ(ierr);
5715f824522SMatthew G. Knepley   PetscFunctionReturn(0);
5725f824522SMatthew G. Knepley }
5735f824522SMatthew G. Knepley 
5745f824522SMatthew G. Knepley /*@C
5755f824522SMatthew G. Knepley 
57692d50984SMatthew G. Knepley   PCPatchSetComputeFunction - Set the callback used to compute patch residuals
57792d50984SMatthew G. Knepley 
57892d50984SMatthew G. Knepley   Input Parameters:
57992d50984SMatthew G. Knepley + pc   - The PC
58092d50984SMatthew G. Knepley . func - The callback
58192d50984SMatthew G. Knepley - ctx  - The user context
58292d50984SMatthew G. Knepley 
58392d50984SMatthew G. Knepley   Level: advanced
58492d50984SMatthew G. Knepley 
58592d50984SMatthew G. Knepley   Note:
58692d50984SMatthew G. Knepley   The callback has signature:
58792d50984SMatthew G. Knepley +  usercomputef(pc, point, x, f, cellIS, n, u, ctx)
58892d50984SMatthew G. Knepley +  pc     - The PC
58992d50984SMatthew G. Knepley +  point  - The point
59092d50984SMatthew G. Knepley +  x      - The input solution (not used in linear problems)
59192d50984SMatthew G. Knepley +  f      - The patch residual vector
59292d50984SMatthew G. Knepley +  cellIS - An array of the cell numbers
59392d50984SMatthew G. Knepley +  n      - The size of g2l
59492d50984SMatthew G. Knepley +  g2l    - The global to local dof translation table
59592d50984SMatthew G. Knepley +  ctx    - The user context
59692d50984SMatthew G. Knepley   and can assume that the matrix entries have been set to zero before the call.
59792d50984SMatthew G. Knepley 
59892d50984SMatthew G. Knepley .seealso: PCPatchSetComputeOperator(), PCPatchGetComputeOperator(), PCPatchSetDiscretisationInfo()
59992d50984SMatthew G. Knepley @*/
60039fd2e8aSPatrick Farrell PetscErrorCode PCPatchSetComputeFunction(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Vec, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
60192d50984SMatthew G. Knepley {
60292d50984SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
60392d50984SMatthew G. Knepley 
60492d50984SMatthew G. Knepley   PetscFunctionBegin;
60592d50984SMatthew G. Knepley   patch->usercomputef    = func;
60692d50984SMatthew G. Knepley   patch->usercomputefctx = ctx;
60792d50984SMatthew G. Knepley   PetscFunctionReturn(0);
60892d50984SMatthew G. Knepley }
60992d50984SMatthew G. Knepley 
61092d50984SMatthew G. Knepley /*@C
61192d50984SMatthew G. Knepley 
61259109abcSLawrence Mitchell   PCPatchSetComputeFunctionInteriorFacets - Set the callback used to compute facet integrals for patch residuals
61359109abcSLawrence Mitchell 
61459109abcSLawrence Mitchell   Input Parameters:
61559109abcSLawrence Mitchell + pc   - The PC
61659109abcSLawrence Mitchell . func - The callback
61759109abcSLawrence Mitchell - ctx  - The user context
61859109abcSLawrence Mitchell 
61959109abcSLawrence Mitchell   Level: advanced
62059109abcSLawrence Mitchell 
62159109abcSLawrence Mitchell   Note:
62259109abcSLawrence Mitchell   The callback has signature:
62359109abcSLawrence Mitchell +  usercomputef(pc, point, x, f, facetIS, n, u, ctx)
62459109abcSLawrence Mitchell +  pc     - The PC
62559109abcSLawrence Mitchell +  point  - The point
62659109abcSLawrence Mitchell +  x      - The input solution (not used in linear problems)
62759109abcSLawrence Mitchell +  f      - The patch residual vector
62859109abcSLawrence Mitchell +  facetIS - An array of the facet numbers
62959109abcSLawrence Mitchell +  n      - The size of g2l
63059109abcSLawrence Mitchell +  g2l    - The global to local dof translation table
63159109abcSLawrence Mitchell +  ctx    - The user context
63259109abcSLawrence Mitchell   and can assume that the matrix entries have been set to zero before the call.
63359109abcSLawrence Mitchell 
63459109abcSLawrence Mitchell .seealso: PCPatchSetComputeOperator(), PCPatchGetComputeOperator(), PCPatchSetDiscretisationInfo()
63559109abcSLawrence Mitchell @*/
63659109abcSLawrence Mitchell PetscErrorCode PCPatchSetComputeFunctionInteriorFacets(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Vec, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
63759109abcSLawrence Mitchell {
63859109abcSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *) pc->data;
63959109abcSLawrence Mitchell 
64059109abcSLawrence Mitchell   PetscFunctionBegin;
64159109abcSLawrence Mitchell   patch->usercomputefintfacet    = func;
64259109abcSLawrence Mitchell   patch->usercomputefintfacetctx = ctx;
64359109abcSLawrence Mitchell   PetscFunctionReturn(0);
64459109abcSLawrence Mitchell }
64559109abcSLawrence Mitchell 
64659109abcSLawrence Mitchell /*@C
64759109abcSLawrence Mitchell 
6485f824522SMatthew G. Knepley   PCPatchSetComputeOperator - Set the callback used to compute patch matrices
6495f824522SMatthew G. Knepley 
6505f824522SMatthew G. Knepley   Input Parameters:
6515f824522SMatthew G. Knepley + pc   - The PC
6525f824522SMatthew G. Knepley . func - The callback
6535f824522SMatthew G. Knepley - ctx  - The user context
6545f824522SMatthew G. Knepley 
6555f824522SMatthew G. Knepley   Level: advanced
6565f824522SMatthew G. Knepley 
6575f824522SMatthew G. Knepley   Note:
6585f824522SMatthew G. Knepley   The callback has signature:
659723f9013SMatthew G. Knepley +  usercomputeop(pc, point, x, mat, cellIS, n, u, ctx)
6605f824522SMatthew G. Knepley +  pc     - The PC
661bdd9e0cdSPatrick Farrell +  point  - The point
662723f9013SMatthew G. Knepley +  x      - The input solution (not used in linear problems)
6635f824522SMatthew G. Knepley +  mat    - The patch matrix
6646f158342SMatthew G. Knepley +  cellIS - An array of the cell numbers
6655f824522SMatthew G. Knepley +  n      - The size of g2l
6665f824522SMatthew G. Knepley +  g2l    - The global to local dof translation table
6675f824522SMatthew G. Knepley +  ctx    - The user context
6685f824522SMatthew G. Knepley   and can assume that the matrix entries have been set to zero before the call.
6695f824522SMatthew G. Knepley 
670723f9013SMatthew G. Knepley .seealso: PCPatchGetComputeOperator(), PCPatchSetComputeFunction(), PCPatchSetDiscretisationInfo()
6715f824522SMatthew G. Knepley @*/
6724d04e9f1SPatrick Farrell PetscErrorCode PCPatchSetComputeOperator(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Mat, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
6734bbf5ea8SMatthew G. Knepley {
6744bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
6754bbf5ea8SMatthew G. Knepley 
6764bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
6774bbf5ea8SMatthew G. Knepley   patch->usercomputeop    = func;
678723f9013SMatthew G. Knepley   patch->usercomputeopctx = ctx;
6794bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
6804bbf5ea8SMatthew G. Knepley }
6814bbf5ea8SMatthew G. Knepley 
68259109abcSLawrence Mitchell /*@C
68359109abcSLawrence Mitchell 
68459109abcSLawrence Mitchell   PCPatchSetComputeOperator - Set the callback used to compute facet integrals for patch matrices
68559109abcSLawrence Mitchell 
68659109abcSLawrence Mitchell   Input Parameters:
68759109abcSLawrence Mitchell + pc   - The PC
68859109abcSLawrence Mitchell . func - The callback
68959109abcSLawrence Mitchell - ctx  - The user context
69059109abcSLawrence Mitchell 
69159109abcSLawrence Mitchell   Level: advanced
69259109abcSLawrence Mitchell 
69359109abcSLawrence Mitchell   Note:
69459109abcSLawrence Mitchell   The callback has signature:
69559109abcSLawrence Mitchell +  usercomputeopintfacet(pc, point, x, mat, facetIS, n, u, ctx)
69659109abcSLawrence Mitchell +  pc     - The PC
69759109abcSLawrence Mitchell +  point  - The point
69859109abcSLawrence Mitchell +  x      - The input solution (not used in linear problems)
69959109abcSLawrence Mitchell +  mat    - The patch matrix
70059109abcSLawrence Mitchell +  facetIS - An IS of the facet numbers
70159109abcSLawrence Mitchell +  n      - The size of g2l
70259109abcSLawrence Mitchell +  g2l    - The global to local dof translation table
70359109abcSLawrence Mitchell +  ctx    - The user context
70459109abcSLawrence Mitchell   and can assume that the matrix entries have been set to zero before the call.
70559109abcSLawrence Mitchell 
70659109abcSLawrence Mitchell .seealso: PCPatchGetComputeOperator(), PCPatchSetComputeFunction(), PCPatchSetDiscretisationInfo()
70759109abcSLawrence Mitchell @*/
70859109abcSLawrence Mitchell PetscErrorCode PCPatchSetComputeOperatorInteriorFacets(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Mat, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
70959109abcSLawrence Mitchell {
71059109abcSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *) pc->data;
71159109abcSLawrence Mitchell 
71259109abcSLawrence Mitchell   PetscFunctionBegin;
71359109abcSLawrence Mitchell   patch->usercomputeopintfacet    = func;
71459109abcSLawrence Mitchell   patch->usercomputeopintfacetctx = ctx;
71559109abcSLawrence Mitchell   PetscFunctionReturn(0);
71659109abcSLawrence Mitchell }
71759109abcSLawrence Mitchell 
7184bbf5ea8SMatthew G. Knepley /* On entry, ht contains the topological entities whose dofs we are responsible for solving for;
7194bbf5ea8SMatthew G. Knepley    on exit, cht contains all the topological entities we need to compute their residuals.
7204bbf5ea8SMatthew G. Knepley    In full generality this should incorporate knowledge of the sparsity pattern of the matrix;
7214bbf5ea8SMatthew G. Knepley    here we assume a standard FE sparsity pattern.*/
7224bbf5ea8SMatthew G. Knepley /* TODO: Use DMPlexGetAdjacency() */
7231b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchCompleteCellPatch(PC pc, PetscHSetI ht, PetscHSetI cht)
7244bbf5ea8SMatthew G. Knepley {
7255f824522SMatthew G. Knepley   DM             dm;
726bc7fa33aSFlorian Wechsung   PC_PATCH      *patch = (PC_PATCH *) pc->data;
7271b68eb51SMatthew G. Knepley   PetscHashIter  hi;
7284bbf5ea8SMatthew G. Knepley   PetscInt       point;
7294bbf5ea8SMatthew G. Knepley   PetscInt      *star = NULL, *closure = NULL;
7304c954380SMatthew G. Knepley   PetscInt       ignoredim, iStart = 0, iEnd = -1, starSize, closureSize, si, ci;
731bc7fa33aSFlorian Wechsung   PetscInt      *fStar = NULL, *fClosure = NULL;
732bc7fa33aSFlorian Wechsung   PetscInt       fBegin, fEnd, fsi, fci, fStarSize, fClosureSize;
7334bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
7344bbf5ea8SMatthew G. Knepley 
7354bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
7365f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
737bc7fa33aSFlorian Wechsung   ierr = DMPlexGetHeightStratum(dm, 1, &fBegin, &fEnd);CHKERRQ(ierr);
7385f824522SMatthew G. Knepley   ierr = PCPatchGetIgnoreDim(pc, &ignoredim);CHKERRQ(ierr);
7395f824522SMatthew G. Knepley   if (ignoredim >= 0) {ierr = DMPlexGetDepthStratum(dm, ignoredim, &iStart, &iEnd);CHKERRQ(ierr);}
7401b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(cht);CHKERRQ(ierr);
7411b68eb51SMatthew G. Knepley   PetscHashIterBegin(ht, hi);
7421b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(ht, hi)) {
7434c954380SMatthew G. Knepley 
7441b68eb51SMatthew G. Knepley     PetscHashIterGetKey(ht, hi, point);
7451b68eb51SMatthew G. Knepley     PetscHashIterNext(ht, hi);
7464bbf5ea8SMatthew G. Knepley 
7474bbf5ea8SMatthew G. Knepley     /* Loop over all the cells that this point connects to */
7484bbf5ea8SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
7495f824522SMatthew G. Knepley     for (si = 0; si < starSize*2; si += 2) {
7504c954380SMatthew G. Knepley       const PetscInt ownedpoint = star[si];
7515f824522SMatthew G. Knepley       /* TODO Check for point in cht before running through closure again */
7524bbf5ea8SMatthew G. Knepley       /* now loop over all entities in the closure of that cell */
7534bbf5ea8SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, ownedpoint, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
7545f824522SMatthew G. Knepley       for (ci = 0; ci < closureSize*2; ci += 2) {
7554c954380SMatthew G. Knepley         const PetscInt seenpoint = closure[ci];
7565f824522SMatthew G. Knepley         if (ignoredim >= 0 && seenpoint >= iStart && seenpoint < iEnd) continue;
7571b68eb51SMatthew G. Knepley         ierr = PetscHSetIAdd(cht, seenpoint);CHKERRQ(ierr);
758bc7fa33aSFlorian Wechsung         /* Facet integrals couple dofs across facets, so in that case for each of
759bc7fa33aSFlorian Wechsung          * the facets we need to add all dofs on the other side of the facet to
760bc7fa33aSFlorian Wechsung          * the seen dofs. */
761bc7fa33aSFlorian Wechsung         if(patch->usercomputeopintfacet){
762bc7fa33aSFlorian Wechsung           if(fBegin <= seenpoint && seenpoint < fEnd){
763bc7fa33aSFlorian Wechsung             ierr = DMPlexGetTransitiveClosure(dm, seenpoint, PETSC_FALSE, &fStarSize, &fStar);CHKERRQ(ierr);
764bc7fa33aSFlorian Wechsung             for (fsi = 0; fsi < fStarSize*2; fsi += 2) {
765bc7fa33aSFlorian Wechsung               ierr = DMPlexGetTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, &fClosureSize, &fClosure);CHKERRQ(ierr);
766bc7fa33aSFlorian Wechsung               for (fci = 0; fci < fClosureSize*2; fci += 2) {
767bc7fa33aSFlorian Wechsung                 ierr = PetscHSetIAdd(cht, fClosure[fci]);CHKERRQ(ierr);
768bc7fa33aSFlorian Wechsung               }
769bc7fa33aSFlorian Wechsung               ierr = DMPlexRestoreTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, NULL, &fClosure);CHKERRQ(ierr);
770bc7fa33aSFlorian Wechsung             }
771bc7fa33aSFlorian Wechsung             ierr = DMPlexRestoreTransitiveClosure(dm, seenpoint, PETSC_FALSE, NULL, &fStar);CHKERRQ(ierr);
772bc7fa33aSFlorian Wechsung           }
773bc7fa33aSFlorian Wechsung         }
7744bbf5ea8SMatthew G. Knepley       }
7752d76c0eeSPatrick Farrell       ierr = DMPlexRestoreTransitiveClosure(dm, ownedpoint, PETSC_TRUE, NULL, &closure);CHKERRQ(ierr);
7764bbf5ea8SMatthew G. Knepley     }
7772d76c0eeSPatrick Farrell     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, NULL, &star);CHKERRQ(ierr);
7784bbf5ea8SMatthew G. Knepley   }
7795f824522SMatthew G. Knepley   PetscFunctionReturn(0);
7805f824522SMatthew G. Knepley }
7815f824522SMatthew G. Knepley 
7825f824522SMatthew G. Knepley static PetscErrorCode PCPatchGetGlobalDofs(PC pc, PetscSection dofSection[], PetscInt f, PetscBool combined, PetscInt p, PetscInt *dof, PetscInt *off)
7835f824522SMatthew G. Knepley {
7845f824522SMatthew G. Knepley   PetscErrorCode ierr;
7855f824522SMatthew G. Knepley 
7865f824522SMatthew G. Knepley   PetscFunctionBegin;
7875f824522SMatthew G. Knepley   if (combined) {
7885f824522SMatthew G. Knepley     if (f < 0) {
7895f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetDof(dofSection[0], p, dof);CHKERRQ(ierr);}
7905f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetOffset(dofSection[0], p, off);CHKERRQ(ierr);}
7915f824522SMatthew G. Knepley     } else {
7925f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetFieldDof(dofSection[0], p, f, dof);CHKERRQ(ierr);}
7935f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetFieldOffset(dofSection[0], p, f, off);CHKERRQ(ierr);}
7945f824522SMatthew G. Knepley     }
7955f824522SMatthew G. Knepley   } else {
7965f824522SMatthew G. Knepley     if (f < 0) {
7975f824522SMatthew G. Knepley       PC_PATCH *patch = (PC_PATCH *) pc->data;
7985f824522SMatthew G. Knepley       PetscInt  fdof, g;
7995f824522SMatthew G. Knepley 
8005f824522SMatthew G. Knepley       if (dof) {
8015f824522SMatthew G. Knepley         *dof = 0;
8025f824522SMatthew G. Knepley         for (g = 0; g < patch->nsubspaces; ++g) {
8035f824522SMatthew G. Knepley           ierr = PetscSectionGetDof(dofSection[g], p, &fdof);CHKERRQ(ierr);
8045f824522SMatthew G. Knepley           *dof += fdof;
8055f824522SMatthew G. Knepley         }
8065f824522SMatthew G. Knepley       }
807624e31c3SLawrence Mitchell       if (off) {
808624e31c3SLawrence Mitchell         *off = 0;
809624e31c3SLawrence Mitchell         for (g = 0; g < patch->nsubspaces; ++g) {
810624e31c3SLawrence Mitchell           ierr = PetscSectionGetOffset(dofSection[g], p, &fdof);CHKERRQ(ierr);
811624e31c3SLawrence Mitchell           *off += fdof;
812624e31c3SLawrence Mitchell         }
813624e31c3SLawrence Mitchell       }
8145f824522SMatthew G. Knepley     } else {
8155f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetDof(dofSection[f], p, dof);CHKERRQ(ierr);}
8165f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetOffset(dofSection[f], p, off);CHKERRQ(ierr);}
8175f824522SMatthew G. Knepley     }
8185f824522SMatthew G. Knepley   }
8194bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
8204bbf5ea8SMatthew G. Knepley }
8214bbf5ea8SMatthew G. Knepley 
8224bbf5ea8SMatthew G. Knepley /* Given a hash table with a set of topological entities (pts), compute the degrees of
8234bbf5ea8SMatthew G. Knepley    freedom in global concatenated numbering on those entities.
8244bbf5ea8SMatthew G. Knepley    For Vanka smoothing, this needs to do something special: ignore dofs of the
8254bbf5ea8SMatthew G. Knepley    constraint subspace on entities that aren't the base entity we're building the patch
8264bbf5ea8SMatthew G. Knepley    around. */
827e4c66b91SPatrick Farrell static PetscErrorCode PCPatchGetPointDofs(PC pc, PetscHSetI pts, PetscHSetI dofs, PetscInt base, PetscHSetI* subspaces_to_exclude)
8284bbf5ea8SMatthew G. Knepley {
8295f824522SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
8301b68eb51SMatthew G. Knepley   PetscHashIter  hi;
8314bbf5ea8SMatthew G. Knepley   PetscInt       ldof, loff;
8324bbf5ea8SMatthew G. Knepley   PetscInt       k, p;
8334bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
8344bbf5ea8SMatthew G. Knepley 
8354bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
8361b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(dofs);CHKERRQ(ierr);
8374bbf5ea8SMatthew G. Knepley   for (k = 0; k < patch->nsubspaces; ++k) {
8384bbf5ea8SMatthew G. Knepley     PetscInt subspaceOffset = patch->subspaceOffsets[k];
8394bbf5ea8SMatthew G. Knepley     PetscInt bs             = patch->bs[k];
8404bbf5ea8SMatthew G. Knepley     PetscInt j, l;
8414bbf5ea8SMatthew G. Knepley 
842e4c66b91SPatrick Farrell     if (subspaces_to_exclude != NULL) {
843e4c66b91SPatrick Farrell       PetscBool should_exclude_k = PETSC_FALSE;
844e4c66b91SPatrick Farrell       PetscHSetIHas(*subspaces_to_exclude, k, &should_exclude_k);
845e4c66b91SPatrick Farrell       if (should_exclude_k) {
8464bbf5ea8SMatthew G. Knepley         /* only get this subspace dofs at the base entity, not any others */
8475f824522SMatthew G. Knepley         ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, base, &ldof, &loff);CHKERRQ(ierr);
8484bbf5ea8SMatthew G. Knepley         if (0 == ldof) continue;
8494bbf5ea8SMatthew G. Knepley         for (j = loff; j < ldof + loff; ++j) {
8504bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
8514bbf5ea8SMatthew G. Knepley             PetscInt dof = bs*j + l + subspaceOffset;
8521b68eb51SMatthew G. Knepley             ierr = PetscHSetIAdd(dofs, dof);CHKERRQ(ierr);
8534bbf5ea8SMatthew G. Knepley           }
8544bbf5ea8SMatthew G. Knepley         }
8554bbf5ea8SMatthew G. Knepley         continue; /* skip the other dofs of this subspace */
8564bbf5ea8SMatthew G. Knepley       }
857e4c66b91SPatrick Farrell     }
8584bbf5ea8SMatthew G. Knepley 
8591b68eb51SMatthew G. Knepley     PetscHashIterBegin(pts, hi);
8601b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(pts, hi)) {
8611b68eb51SMatthew G. Knepley       PetscHashIterGetKey(pts, hi, p);
8621b68eb51SMatthew G. Knepley       PetscHashIterNext(pts, hi);
8635f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, p, &ldof, &loff);CHKERRQ(ierr);
8644bbf5ea8SMatthew G. Knepley       if (0 == ldof) continue;
8654bbf5ea8SMatthew G. Knepley       for (j = loff; j < ldof + loff; ++j) {
8664bbf5ea8SMatthew G. Knepley         for (l = 0; l < bs; ++l) {
8674bbf5ea8SMatthew G. Knepley           PetscInt dof = bs*j + l + subspaceOffset;
8681b68eb51SMatthew G. Knepley           ierr = PetscHSetIAdd(dofs, dof);CHKERRQ(ierr);
8694bbf5ea8SMatthew G. Knepley         }
8704bbf5ea8SMatthew G. Knepley       }
8714bbf5ea8SMatthew G. Knepley     }
8724bbf5ea8SMatthew G. Knepley   }
8734bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
8744bbf5ea8SMatthew G. Knepley }
8754bbf5ea8SMatthew G. Knepley 
8764bbf5ea8SMatthew G. Knepley /* Given two hash tables A and B, compute the keys in B that are not in A, and put them in C */
8771b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchComputeSetDifference_Private(PetscHSetI A, PetscHSetI B, PetscHSetI C)
8784bbf5ea8SMatthew G. Knepley {
8791b68eb51SMatthew G. Knepley   PetscHashIter  hi;
8801b68eb51SMatthew G. Knepley   PetscInt       key;
8814bbf5ea8SMatthew G. Knepley   PetscBool      flg;
8821b68eb51SMatthew G. Knepley   PetscErrorCode ierr;
8834bbf5ea8SMatthew G. Knepley 
8844bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
8851b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(C);CHKERRQ(ierr);
8861b68eb51SMatthew G. Knepley   PetscHashIterBegin(B, hi);
8871b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(B, hi)) {
8881b68eb51SMatthew G. Knepley     PetscHashIterGetKey(B, hi, key);
8891b68eb51SMatthew G. Knepley     PetscHashIterNext(B, hi);
8901b68eb51SMatthew G. Knepley     ierr = PetscHSetIHas(A, key, &flg);CHKERRQ(ierr);
8911b68eb51SMatthew G. Knepley     if (!flg) {ierr = PetscHSetIAdd(C, key);CHKERRQ(ierr);}
8924bbf5ea8SMatthew G. Knepley   }
8934bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
8944bbf5ea8SMatthew G. Knepley }
8954bbf5ea8SMatthew G. Knepley 
8964bbf5ea8SMatthew G. Knepley /*
8974bbf5ea8SMatthew G. Knepley  * PCPatchCreateCellPatches - create patches.
8984bbf5ea8SMatthew G. Knepley  *
8994bbf5ea8SMatthew G. Knepley  * Input Parameters:
9004bbf5ea8SMatthew G. Knepley  * + dm - The DMPlex object defining the mesh
9014bbf5ea8SMatthew G. Knepley  *
9024bbf5ea8SMatthew G. Knepley  * Output Parameters:
9034bbf5ea8SMatthew G. Knepley  * + cellCounts  - Section with counts of cells around each vertex
9045f824522SMatthew G. Knepley  * . cells       - IS of the cell point indices of cells in each patch
9055f824522SMatthew G. Knepley  * . pointCounts - Section with counts of cells around each vertex
9065f824522SMatthew G. Knepley  * - point       - IS of the cell point indices of cells in each patch
9074bbf5ea8SMatthew G. Knepley  */
9084bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatches(PC pc)
9094bbf5ea8SMatthew G. Knepley {
9104bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
9115f824522SMatthew G. Knepley   DMLabel         ghost = NULL;
9124bbf5ea8SMatthew G. Knepley   DM              dm, plex;
9131b68eb51SMatthew G. Knepley   PetscHSetI      ht, cht;
9140e126c0bSLawrence Mitchell   PetscSection    cellCounts,  pointCounts, intFacetCounts, extFacetCounts;
915eb62eeaaSLawrence Mitchell   PetscInt       *cellsArray, *pointsArray, *intFacetsArray, *extFacetsArray, *intFacetsToPatchCell;
9160e126c0bSLawrence Mitchell   PetscInt        numCells, numPoints, numIntFacets, numExtFacets;
9175f824522SMatthew G. Knepley   const PetscInt *leaves;
9185f824522SMatthew G. Knepley   PetscInt        nleaves, pStart, pEnd, cStart, cEnd, vStart, vEnd, v;
9195f824522SMatthew G. Knepley   PetscBool       isFiredrake;
9204bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
9214bbf5ea8SMatthew G. Knepley 
9224bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
9234bbf5ea8SMatthew G. Knepley   /* Used to keep track of the cells in the patch. */
9241b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&ht);CHKERRQ(ierr);
9251b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&cht);CHKERRQ(ierr);
9264bbf5ea8SMatthew G. Knepley 
9274bbf5ea8SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
9284bbf5ea8SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONGSTATE, "DM not yet set on patch PC\n");
9294bbf5ea8SMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
9304bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetChart(plex, &pStart, &pEnd);CHKERRQ(ierr);
9314bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr);
9324bbf5ea8SMatthew G. Knepley 
9334bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
9345f824522SMatthew G. Knepley     ierr = patch->userpatchconstructionop(pc, &patch->npatch, &patch->userIS, &patch->iterationSet, patch->userpatchconstructctx);CHKERRQ(ierr);
9355f824522SMatthew G. Knepley     vStart = 0; vEnd = patch->npatch;
936e5b9877fSPatrick Farrell   } else if (patch->ctype == PC_PATCH_PARDECOMP) {
9370a390943SPatrick Farrell     vStart = 0; vEnd = 1;
9385f824522SMatthew G. Knepley   } else if (patch->codim < 0) {
9395f824522SMatthew G. Knepley     if (patch->dim < 0) {ierr = DMPlexGetDepthStratum(plex,  0,            &vStart, &vEnd);CHKERRQ(ierr);}
9405f824522SMatthew G. Knepley     else                {ierr = DMPlexGetDepthStratum(plex,  patch->dim,   &vStart, &vEnd);CHKERRQ(ierr);}
9415f824522SMatthew G. Knepley   } else                {ierr = DMPlexGetHeightStratum(plex, patch->codim, &vStart, &vEnd);CHKERRQ(ierr);}
9425f824522SMatthew G. Knepley   patch->npatch = vEnd - vStart;
9434bbf5ea8SMatthew G. Knepley 
9444bbf5ea8SMatthew G. Knepley   /* These labels mark the owned points.  We only create patches around points that this process owns. */
9455f824522SMatthew G. Knepley   ierr = DMHasLabel(dm, "pyop2_ghost", &isFiredrake);CHKERRQ(ierr);
9465f824522SMatthew G. Knepley   if (isFiredrake) {
9474bbf5ea8SMatthew G. Knepley     ierr = DMGetLabel(dm, "pyop2_ghost", &ghost);CHKERRQ(ierr);
9484bbf5ea8SMatthew G. Knepley     ierr = DMLabelCreateIndex(ghost, pStart, pEnd);CHKERRQ(ierr);
9495f824522SMatthew G. Knepley   } else {
9505f824522SMatthew G. Knepley     PetscSF sf;
9515f824522SMatthew G. Knepley 
9525f824522SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
9535f824522SMatthew G. Knepley     ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
9545f824522SMatthew G. Knepley     nleaves = PetscMax(nleaves, 0);
9555f824522SMatthew G. Knepley   }
9564bbf5ea8SMatthew G. Knepley 
9574bbf5ea8SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->cellCounts);CHKERRQ(ierr);
9585f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->cellCounts, "Patch Cell Layout");CHKERRQ(ierr);
9594bbf5ea8SMatthew G. Knepley   cellCounts = patch->cellCounts;
9604bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetChart(cellCounts, vStart, vEnd);CHKERRQ(ierr);
9615f824522SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->pointCounts);CHKERRQ(ierr);
9625f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->pointCounts, "Patch Point Layout");CHKERRQ(ierr);
9635f824522SMatthew G. Knepley   pointCounts = patch->pointCounts;
9645f824522SMatthew G. Knepley   ierr = PetscSectionSetChart(pointCounts, vStart, vEnd);CHKERRQ(ierr);
9650e126c0bSLawrence Mitchell   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->extFacetCounts);CHKERRQ(ierr);
9660e126c0bSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->extFacetCounts, "Patch Exterior Facet Layout");CHKERRQ(ierr);
9670e126c0bSLawrence Mitchell   extFacetCounts = patch->extFacetCounts;
9680e126c0bSLawrence Mitchell   ierr = PetscSectionSetChart(extFacetCounts, vStart, vEnd);CHKERRQ(ierr);
9690e126c0bSLawrence Mitchell   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->intFacetCounts);CHKERRQ(ierr);
9700e126c0bSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->intFacetCounts, "Patch Interior Facet Layout");CHKERRQ(ierr);
9710e126c0bSLawrence Mitchell   intFacetCounts = patch->intFacetCounts;
9720e126c0bSLawrence Mitchell   ierr = PetscSectionSetChart(intFacetCounts, vStart, vEnd);CHKERRQ(ierr);
9735f824522SMatthew G. Knepley   /* Count cells and points in the patch surrounding each entity */
9740e126c0bSLawrence Mitchell   PetscInt       fStart, fEnd;
9750e126c0bSLawrence Mitchell   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
9764bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
9771b68eb51SMatthew G. Knepley     PetscHashIter hi;
9785f824522SMatthew G. Knepley     PetscInt       chtSize, loc = -1;
9795f824522SMatthew G. Knepley     PetscBool      flg;
9804bbf5ea8SMatthew G. Knepley 
981b525f888SPatrick Farrell     if (!patch->user_patches && patch->ctype != PC_PATCH_PARDECOMP) {
9825f824522SMatthew G. Knepley       if (ghost) {ierr = DMLabelHasPoint(ghost, v, &flg);CHKERRQ(ierr);}
983928bb9adSStefano Zampini       else       {ierr = PetscFindInt(v, nleaves, leaves, &loc);CHKERRQ(ierr); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;}
9844bbf5ea8SMatthew G. Knepley       /* Not an owned entity, don't make a cell patch. */
9854bbf5ea8SMatthew G. Knepley       if (flg) continue;
9864bbf5ea8SMatthew G. Knepley     }
9874bbf5ea8SMatthew G. Knepley 
9884bbf5ea8SMatthew G. Knepley     ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr);
9895f824522SMatthew G. Knepley     ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr);
9901b68eb51SMatthew G. Knepley     ierr = PetscHSetIGetSize(cht, &chtSize);CHKERRQ(ierr);
9914bbf5ea8SMatthew G. Knepley     /* empty patch, continue */
9924bbf5ea8SMatthew G. Knepley     if (chtSize == 0) continue;
9934bbf5ea8SMatthew G. Knepley 
9944bbf5ea8SMatthew G. Knepley     /* safe because size(cht) > 0 from above */
9951b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
9961b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
9975f824522SMatthew G. Knepley       PetscInt point, pdof;
9984bbf5ea8SMatthew G. Knepley 
9991b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
10000e126c0bSLawrence Mitchell       if (fStart <= point && point < fEnd) {
10010e126c0bSLawrence Mitchell         const PetscInt *support;
10020e126c0bSLawrence Mitchell         PetscInt supportSize, p;
10030e126c0bSLawrence Mitchell         PetscBool interior = PETSC_TRUE;
10040e126c0bSLawrence Mitchell         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
10050e126c0bSLawrence Mitchell         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
10060e126c0bSLawrence Mitchell         if (supportSize == 1) {
10070e126c0bSLawrence Mitchell           interior = PETSC_FALSE;
10080e126c0bSLawrence Mitchell         } else {
10090e126c0bSLawrence Mitchell           for (p = 0; p < supportSize; p++) {
10100e126c0bSLawrence Mitchell             PetscBool found;
10110e126c0bSLawrence Mitchell             /* FIXME: can I do this while iterating over cht? */
10120e126c0bSLawrence Mitchell             PetscHSetIHas(cht, support[p], &found);
10130e126c0bSLawrence Mitchell             if (!found) {
10140e126c0bSLawrence Mitchell               interior = PETSC_FALSE;
10150e126c0bSLawrence Mitchell               break;
10160e126c0bSLawrence Mitchell             }
10170e126c0bSLawrence Mitchell           }
10180e126c0bSLawrence Mitchell         }
10190e126c0bSLawrence Mitchell         if (interior) {
10200e126c0bSLawrence Mitchell           ierr = PetscSectionAddDof(intFacetCounts, v, 1);CHKERRQ(ierr);
10210e126c0bSLawrence Mitchell         } else {
10220e126c0bSLawrence Mitchell           ierr = PetscSectionAddDof(extFacetCounts, v, 1);CHKERRQ(ierr);
10230e126c0bSLawrence Mitchell         }
10240e126c0bSLawrence Mitchell       }
10255f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr);
10265f824522SMatthew G. Knepley       if (pdof)                            {ierr = PetscSectionAddDof(pointCounts, v, 1);CHKERRQ(ierr);}
10275f824522SMatthew G. Knepley       if (point >= cStart && point < cEnd) {ierr = PetscSectionAddDof(cellCounts, v, 1);CHKERRQ(ierr);}
10281b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
10294bbf5ea8SMatthew G. Knepley     }
10304bbf5ea8SMatthew G. Knepley   }
10315f824522SMatthew G. Knepley   if (isFiredrake) {ierr = DMLabelDestroyIndex(ghost);CHKERRQ(ierr);}
10324bbf5ea8SMatthew G. Knepley 
10334bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetUp(cellCounts);CHKERRQ(ierr);
10344bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr);
10354bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numCells, &cellsArray);CHKERRQ(ierr);
10365f824522SMatthew G. Knepley   ierr = PetscSectionSetUp(pointCounts);CHKERRQ(ierr);
10375f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(pointCounts, &numPoints);CHKERRQ(ierr);
10385f824522SMatthew G. Knepley   ierr = PetscMalloc1(numPoints, &pointsArray);CHKERRQ(ierr);
10394bbf5ea8SMatthew G. Knepley 
10400e126c0bSLawrence Mitchell   ierr = PetscSectionSetUp(intFacetCounts);CHKERRQ(ierr);
10410e126c0bSLawrence Mitchell   ierr = PetscSectionSetUp(extFacetCounts);CHKERRQ(ierr);
10420e126c0bSLawrence Mitchell   ierr = PetscSectionGetStorageSize(intFacetCounts, &numIntFacets);CHKERRQ(ierr);
10430e126c0bSLawrence Mitchell   ierr = PetscSectionGetStorageSize(extFacetCounts, &numExtFacets);CHKERRQ(ierr);
10440e126c0bSLawrence Mitchell   ierr = PetscMalloc1(numIntFacets, &intFacetsArray);CHKERRQ(ierr);
1045eb62eeaaSLawrence Mitchell   ierr = PetscMalloc1(numIntFacets*2, &intFacetsToPatchCell);CHKERRQ(ierr);
10460e126c0bSLawrence Mitchell   ierr = PetscMalloc1(numExtFacets, &extFacetsArray);CHKERRQ(ierr);
10470e126c0bSLawrence Mitchell 
1048eb62eeaaSLawrence Mitchell 
10494bbf5ea8SMatthew G. Knepley   /* Now that we know how much space we need, run through again and actually remember the cells. */
10504bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; v++ ) {
10511b68eb51SMatthew G. Knepley     PetscHashIter hi;
10520e126c0bSLawrence Mitchell     PetscInt       dof, off, cdof, coff, efdof, efoff, ifdof, ifoff, pdof, n = 0, cn = 0, ifn = 0, efn = 0;
10534bbf5ea8SMatthew G. Knepley 
10545f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(pointCounts, v, &dof);CHKERRQ(ierr);
10555f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(pointCounts, v, &off);CHKERRQ(ierr);
10565f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &cdof);CHKERRQ(ierr);
10575f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &coff);CHKERRQ(ierr);
10580e126c0bSLawrence Mitchell     ierr = PetscSectionGetDof(intFacetCounts, v, &ifdof);CHKERRQ(ierr);
10590e126c0bSLawrence Mitchell     ierr = PetscSectionGetOffset(intFacetCounts, v, &ifoff);CHKERRQ(ierr);
10600e126c0bSLawrence Mitchell     ierr = PetscSectionGetDof(extFacetCounts, v, &efdof);CHKERRQ(ierr);
10610e126c0bSLawrence Mitchell     ierr = PetscSectionGetOffset(extFacetCounts, v, &efoff);CHKERRQ(ierr);
10625f824522SMatthew G. Knepley     if (dof <= 0) continue;
10634bbf5ea8SMatthew G. Knepley     ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr);
10645f824522SMatthew G. Knepley     ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr);
10651b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
10661b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
10674bbf5ea8SMatthew G. Knepley       PetscInt point;
10684bbf5ea8SMatthew G. Knepley 
10691b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
10700e126c0bSLawrence Mitchell       if (fStart <= point && point < fEnd) {
10710e126c0bSLawrence Mitchell         const PetscInt *support;
10720e126c0bSLawrence Mitchell         PetscInt supportSize, p;
10730e126c0bSLawrence Mitchell         PetscBool interior = PETSC_TRUE;
10740e126c0bSLawrence Mitchell         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
10750e126c0bSLawrence Mitchell         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
10760e126c0bSLawrence Mitchell         if (supportSize == 1) {
10770e126c0bSLawrence Mitchell           interior = PETSC_FALSE;
10780e126c0bSLawrence Mitchell         } else {
10790e126c0bSLawrence Mitchell           for (p = 0; p < supportSize; p++) {
10800e126c0bSLawrence Mitchell             PetscBool found;
10810e126c0bSLawrence Mitchell             /* FIXME: can I do this while iterating over cht? */
10820e126c0bSLawrence Mitchell             PetscHSetIHas(cht, support[p], &found);
10830e126c0bSLawrence Mitchell             if (!found) {
10840e126c0bSLawrence Mitchell               interior = PETSC_FALSE;
10850e126c0bSLawrence Mitchell               break;
10860e126c0bSLawrence Mitchell             }
10870e126c0bSLawrence Mitchell           }
10880e126c0bSLawrence Mitchell         }
10890e126c0bSLawrence Mitchell         if (interior) {
109044b625f7SLawrence Mitchell           intFacetsToPatchCell[2*(ifoff + ifn)] = support[0];
109144b625f7SLawrence Mitchell           intFacetsToPatchCell[2*(ifoff + ifn) + 1] = support[1];
10920e126c0bSLawrence Mitchell           intFacetsArray[ifoff + ifn++] = point;
10930e126c0bSLawrence Mitchell         } else {
10940e126c0bSLawrence Mitchell           extFacetsArray[efoff + efn++] = point;
10950e126c0bSLawrence Mitchell         }
10960e126c0bSLawrence Mitchell       }
10975f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr);
10985f824522SMatthew G. Knepley       if (pdof)                            {pointsArray[off + n++] = point;}
10995f824522SMatthew G. Knepley       if (point >= cStart && point < cEnd) {cellsArray[coff + cn++] = point;}
11001b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
11014bbf5ea8SMatthew G. Knepley     }
11020e126c0bSLawrence 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);
11030e126c0bSLawrence 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);
11045f824522SMatthew 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);
11055f824522SMatthew 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);
1106eb62eeaaSLawrence Mitchell 
1107eb62eeaaSLawrence Mitchell     for (ifn = 0; ifn < ifdof; ifn++) {
110844b625f7SLawrence Mitchell       PetscInt cell0 = intFacetsToPatchCell[2*(ifoff + ifn)];
110944b625f7SLawrence Mitchell       PetscInt cell1 = intFacetsToPatchCell[2*(ifoff + ifn) + 1];
1110eb62eeaaSLawrence Mitchell       PetscBool found0 = PETSC_FALSE, found1 = PETSC_FALSE;
1111eb62eeaaSLawrence Mitchell       for (n = 0; n < cdof; n++) {
11127c54fef0SLawrence Mitchell         if (!found0 && cell0 == cellsArray[coff + n]) {
1113c3faab33SLawrence Mitchell           intFacetsToPatchCell[2*(ifoff + ifn)] = n;
1114eb62eeaaSLawrence Mitchell           found0 = PETSC_TRUE;
1115eb62eeaaSLawrence Mitchell         }
11167c54fef0SLawrence Mitchell         if (!found1 && cell1 == cellsArray[coff + n]) {
1117c3faab33SLawrence Mitchell           intFacetsToPatchCell[2*(ifoff + ifn) + 1] = n;
111880fc4459SLawrence Mitchell           found1 = PETSC_TRUE;
1119eb62eeaaSLawrence Mitchell         }
1120eb62eeaaSLawrence Mitchell         if (found0 && found1) break;
1121eb62eeaaSLawrence Mitchell       }
112244b625f7SLawrence Mitchell       if (!(found0 && found1)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Didn't manage to find local point numbers for facet support");
1123eb62eeaaSLawrence Mitchell     }
11244bbf5ea8SMatthew G. Knepley   }
11251b68eb51SMatthew G. Knepley   ierr = PetscHSetIDestroy(&ht);CHKERRQ(ierr);
11261b68eb51SMatthew G. Knepley   ierr = PetscHSetIDestroy(&cht);CHKERRQ(ierr);
11274bbf5ea8SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
11285f824522SMatthew G. Knepley 
11295f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numCells,  cellsArray,  PETSC_OWN_POINTER, &patch->cells);CHKERRQ(ierr);
11305f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->cells,  "Patch Cells");CHKERRQ(ierr);
11315f824522SMatthew G. Knepley   if (patch->viewCells) {
11325f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->cellCounts, patch->viewerCells, patch->formatCells);CHKERRQ(ierr);
11335f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->cells,      patch->viewerCells, patch->formatCells);CHKERRQ(ierr);
11345f824522SMatthew G. Knepley   }
11350e126c0bSLawrence Mitchell   ierr = ISCreateGeneral(PETSC_COMM_SELF, numIntFacets,  intFacetsArray,  PETSC_OWN_POINTER, &patch->intFacets);CHKERRQ(ierr);
11360e126c0bSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->intFacets,  "Patch Interior Facets");CHKERRQ(ierr);
1137eb62eeaaSLawrence Mitchell   ierr = ISCreateGeneral(PETSC_COMM_SELF, 2*numIntFacets, intFacetsToPatchCell, PETSC_OWN_POINTER, &patch->intFacetsToPatchCell);CHKERRQ(ierr);
1138eb62eeaaSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->intFacetsToPatchCell,  "Patch Interior Facets local support");CHKERRQ(ierr);
11390e126c0bSLawrence Mitchell   if (patch->viewIntFacets) {
1140fbb82ab5SLawrence Mitchell     ierr = ObjectView((PetscObject) patch->intFacetCounts,       patch->viewerIntFacets, patch->formatIntFacets);CHKERRQ(ierr);
11410e126c0bSLawrence Mitchell     ierr = ObjectView((PetscObject) patch->intFacets,            patch->viewerIntFacets, patch->formatIntFacets);CHKERRQ(ierr);
1142eb62eeaaSLawrence Mitchell     ierr = ObjectView((PetscObject) patch->intFacetsToPatchCell, patch->viewerIntFacets, patch->formatIntFacets);CHKERRQ(ierr);
11430e126c0bSLawrence Mitchell   }
11440e126c0bSLawrence Mitchell   ierr = ISCreateGeneral(PETSC_COMM_SELF, numExtFacets,  extFacetsArray,  PETSC_OWN_POINTER, &patch->extFacets);CHKERRQ(ierr);
11450e126c0bSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->extFacets,  "Patch Exterior Facets");CHKERRQ(ierr);
11460e126c0bSLawrence Mitchell   if (patch->viewExtFacets) {
1147fbb82ab5SLawrence Mitchell     ierr = ObjectView((PetscObject) patch->extFacetCounts, patch->viewerExtFacets, patch->formatExtFacets);CHKERRQ(ierr);
11480e126c0bSLawrence Mitchell     ierr = ObjectView((PetscObject) patch->extFacets,      patch->viewerExtFacets, patch->formatExtFacets);CHKERRQ(ierr);
11490e126c0bSLawrence Mitchell   }
11505f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints, pointsArray, PETSC_OWN_POINTER, &patch->points);CHKERRQ(ierr);
11515f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->points, "Patch Points");CHKERRQ(ierr);
11525f824522SMatthew G. Knepley   if (patch->viewPoints) {
11535f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->pointCounts, patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr);
11545f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->points,      patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr);
11555f824522SMatthew G. Knepley   }
11564bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
11574bbf5ea8SMatthew G. Knepley }
11584bbf5ea8SMatthew G. Knepley 
11594bbf5ea8SMatthew G. Knepley /*
11604bbf5ea8SMatthew G. Knepley  * PCPatchCreateCellPatchDiscretisationInfo - Build the dof maps for cell patches
11614bbf5ea8SMatthew G. Knepley  *
11624bbf5ea8SMatthew G. Knepley  * Input Parameters:
11634bbf5ea8SMatthew G. Knepley  * + dm - The DMPlex object defining the mesh
11644bbf5ea8SMatthew G. Knepley  * . cellCounts - Section with counts of cells around each vertex
11654bbf5ea8SMatthew G. Knepley  * . cells - IS of the cell point indices of cells in each patch
11664bbf5ea8SMatthew G. Knepley  * . cellNumbering - Section mapping plex cell points to Firedrake cell indices.
11674bbf5ea8SMatthew G. Knepley  * . nodesPerCell - number of nodes per cell.
11684bbf5ea8SMatthew G. Knepley  * - cellNodeMap - map from cells to node indices (nodesPerCell * numCells)
11694bbf5ea8SMatthew G. Knepley  *
11704bbf5ea8SMatthew G. Knepley  * Output Parameters:
11715f824522SMatthew G. Knepley  * + dofs - IS of local dof numbers of each cell in the patch, where local is a patch local numbering
11724bbf5ea8SMatthew G. Knepley  * . gtolCounts - Section with counts of dofs per cell patch
11734bbf5ea8SMatthew G. Knepley  * - gtol - IS mapping from global dofs to local dofs for each patch.
11744bbf5ea8SMatthew G. Knepley  */
11754bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatchDiscretisationInfo(PC pc)
11764bbf5ea8SMatthew G. Knepley {
11774bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch           = (PC_PATCH *) pc->data;
11784bbf5ea8SMatthew G. Knepley   PetscSection    cellCounts      = patch->cellCounts;
11795f824522SMatthew G. Knepley   PetscSection    pointCounts     = patch->pointCounts;
11800904074fSPatrick Farrell   PetscSection    gtolCounts, gtolCountsWithArtificial = NULL, gtolCountsWithAll = NULL;
11814bbf5ea8SMatthew G. Knepley   IS              cells           = patch->cells;
11825f824522SMatthew G. Knepley   IS              points          = patch->points;
11834bbf5ea8SMatthew G. Knepley   PetscSection    cellNumbering   = patch->cellNumbering;
11845f824522SMatthew G. Knepley   PetscInt        Nf              = patch->nsubspaces;
11855f824522SMatthew G. Knepley   PetscInt        numCells, numPoints;
11864bbf5ea8SMatthew G. Knepley   PetscInt        numDofs;
11870904074fSPatrick Farrell   PetscInt        numGlobalDofs, numGlobalDofsWithArtificial, numGlobalDofsWithAll;
11884bbf5ea8SMatthew G. Knepley   PetscInt        totalDofsPerCell = patch->totalDofsPerCell;
11894bbf5ea8SMatthew G. Knepley   PetscInt        vStart, vEnd, v;
11905f824522SMatthew G. Knepley   const PetscInt *cellsArray, *pointsArray;
11914bbf5ea8SMatthew G. Knepley   PetscInt       *newCellsArray   = NULL;
11924bbf5ea8SMatthew G. Knepley   PetscInt       *dofsArray       = NULL;
1193c2e6f3c0SFlorian Wechsung   PetscInt       *dofsArrayWithArtificial = NULL;
11940904074fSPatrick Farrell   PetscInt       *dofsArrayWithAll = NULL;
11955f824522SMatthew G. Knepley   PetscInt       *offsArray       = NULL;
1196c2e6f3c0SFlorian Wechsung   PetscInt       *offsArrayWithArtificial = NULL;
11970904074fSPatrick Farrell   PetscInt       *offsArrayWithAll = NULL;
11984bbf5ea8SMatthew G. Knepley   PetscInt       *asmArray        = NULL;
1199c2e6f3c0SFlorian Wechsung   PetscInt       *asmArrayWithArtificial = NULL;
12000904074fSPatrick Farrell   PetscInt       *asmArrayWithAll = NULL;
12014bbf5ea8SMatthew G. Knepley   PetscInt       *globalDofsArray = NULL;
1202c2e6f3c0SFlorian Wechsung   PetscInt       *globalDofsArrayWithArtificial = NULL;
12030904074fSPatrick Farrell   PetscInt       *globalDofsArrayWithAll = NULL;
12044bbf5ea8SMatthew G. Knepley   PetscInt        globalIndex     = 0;
12054bbf5ea8SMatthew G. Knepley   PetscInt        key             = 0;
12064bbf5ea8SMatthew G. Knepley   PetscInt        asmKey          = 0;
1207557beb66SLawrence Mitchell   DM              dm              = NULL;
1208557beb66SLawrence Mitchell   const PetscInt *bcNodes         = NULL;
12091b68eb51SMatthew G. Knepley   PetscHMapI      ht;
1210c2e6f3c0SFlorian Wechsung   PetscHMapI      htWithArtificial;
12110904074fSPatrick Farrell   PetscHMapI      htWithAll;
12121b68eb51SMatthew G. Knepley   PetscHSetI      globalBcs;
1213557beb66SLawrence Mitchell   PetscInt        numBcs;
12141b68eb51SMatthew G. Knepley   PetscHSetI      ownedpts, seenpts, owneddofs, seendofs, artificialbcs;
1215cda239d9SMatthew G. Knepley   PetscInt        pStart, pEnd, p, i;
121610534d48SPatrick Farrell   char            option[PETSC_MAX_PATH_LEN];
121739fd2e8aSPatrick Farrell   PetscBool       isNonlinear;
12184bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
12194bbf5ea8SMatthew G. Knepley 
12204bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1221557beb66SLawrence Mitchell 
1222557beb66SLawrence Mitchell   ierr = PCGetDM(pc, &dm); CHKERRQ(ierr);
12234bbf5ea8SMatthew G. Knepley   /* dofcounts section is cellcounts section * dofPerCell */
12244bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr);
12255f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(patch->pointCounts, &numPoints);CHKERRQ(ierr);
12264bbf5ea8SMatthew G. Knepley   numDofs = numCells * totalDofsPerCell;
12274bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numDofs, &dofsArray);CHKERRQ(ierr);
12285f824522SMatthew G. Knepley   ierr = PetscMalloc1(numPoints*Nf, &offsArray);CHKERRQ(ierr);
12294bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numDofs, &asmArray);CHKERRQ(ierr);
12304bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numCells, &newCellsArray);CHKERRQ(ierr);
12314bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(cellCounts, &vStart, &vEnd);CHKERRQ(ierr);
12324bbf5ea8SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCounts);CHKERRQ(ierr);
12334bbf5ea8SMatthew G. Knepley   gtolCounts = patch->gtolCounts;
12344bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetChart(gtolCounts, vStart, vEnd);CHKERRQ(ierr);
12355f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->gtolCounts, "Patch Global Index Section");CHKERRQ(ierr);
12364bbf5ea8SMatthew G. Knepley 
12370904074fSPatrick Farrell   if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE)
1238c2e6f3c0SFlorian Wechsung   {
1239f0dcb611SKarl Rupp     ierr = PetscMalloc1(numPoints*Nf, &offsArrayWithArtificial);CHKERRQ(ierr);
1240c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numDofs, &asmArrayWithArtificial);CHKERRQ(ierr);
1241c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numDofs, &dofsArrayWithArtificial);CHKERRQ(ierr);
1242c2e6f3c0SFlorian Wechsung     ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithArtificial);CHKERRQ(ierr);
1243c2e6f3c0SFlorian Wechsung     gtolCountsWithArtificial = patch->gtolCountsWithArtificial;
1244c2e6f3c0SFlorian Wechsung     ierr = PetscSectionSetChart(gtolCountsWithArtificial, vStart, vEnd);CHKERRQ(ierr);
1245c2e6f3c0SFlorian Wechsung     ierr = PetscObjectSetName((PetscObject) patch->gtolCountsWithArtificial, "Patch Global Index Section Including Artificial BCs");CHKERRQ(ierr);
1246c2e6f3c0SFlorian Wechsung   }
1247c2e6f3c0SFlorian Wechsung 
12480904074fSPatrick Farrell   isNonlinear = patch->isNonlinear;
12490904074fSPatrick Farrell   if(isNonlinear)
12500904074fSPatrick Farrell   {
12510904074fSPatrick Farrell     ierr = PetscMalloc1(numPoints*Nf, &offsArrayWithAll);CHKERRQ(ierr);
12520904074fSPatrick Farrell     ierr = PetscMalloc1(numDofs, &asmArrayWithAll);CHKERRQ(ierr);
12530904074fSPatrick Farrell     ierr = PetscMalloc1(numDofs, &dofsArrayWithAll);CHKERRQ(ierr);
12540904074fSPatrick Farrell     ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithAll);CHKERRQ(ierr);
12550904074fSPatrick Farrell     gtolCountsWithAll = patch->gtolCountsWithAll;
12560904074fSPatrick Farrell     ierr = PetscSectionSetChart(gtolCountsWithAll, vStart, vEnd);CHKERRQ(ierr);
12570904074fSPatrick Farrell     ierr = PetscObjectSetName((PetscObject) patch->gtolCountsWithAll, "Patch Global Index Section Including All BCs");CHKERRQ(ierr);
12580904074fSPatrick Farrell   }
12590904074fSPatrick Farrell 
1260557beb66SLawrence Mitchell   /* Outside the patch loop, get the dofs that are globally-enforced Dirichlet
1261557beb66SLawrence Mitchell    conditions */
12621b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&globalBcs);CHKERRQ(ierr);
1263557beb66SLawrence Mitchell   ierr = ISGetIndices(patch->ghostBcNodes, &bcNodes); CHKERRQ(ierr);
1264557beb66SLawrence Mitchell   ierr = ISGetSize(patch->ghostBcNodes, &numBcs); CHKERRQ(ierr);
1265cda239d9SMatthew G. Knepley   for (i = 0; i < numBcs; ++i) {
12661b68eb51SMatthew G. Knepley     ierr = PetscHSetIAdd(globalBcs, bcNodes[i]);CHKERRQ(ierr); /* these are already in concatenated numbering */
1267557beb66SLawrence Mitchell   }
1268557beb66SLawrence Mitchell   ierr = ISRestoreIndices(patch->ghostBcNodes, &bcNodes); CHKERRQ(ierr);
1269557beb66SLawrence Mitchell   ierr = ISDestroy(&patch->ghostBcNodes); CHKERRQ(ierr); /* memory optimisation */
1270557beb66SLawrence Mitchell 
1271557beb66SLawrence Mitchell   /* Hash tables for artificial BC construction */
12721b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&ownedpts);CHKERRQ(ierr);
12731b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&seenpts);CHKERRQ(ierr);
12741b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&owneddofs);CHKERRQ(ierr);
12751b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&seendofs);CHKERRQ(ierr);
12761b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&artificialbcs);CHKERRQ(ierr);
1277557beb66SLawrence Mitchell 
12784bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(cells, &cellsArray);CHKERRQ(ierr);
12795f824522SMatthew G. Knepley   ierr = ISGetIndices(points, &pointsArray);CHKERRQ(ierr);
12801b68eb51SMatthew G. Knepley   ierr = PetscHMapICreate(&ht);CHKERRQ(ierr);
1281c2e6f3c0SFlorian Wechsung   ierr = PetscHMapICreate(&htWithArtificial);CHKERRQ(ierr);
12820904074fSPatrick Farrell   ierr = PetscHMapICreate(&htWithAll);CHKERRQ(ierr);
12834bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
12844bbf5ea8SMatthew G. Knepley     PetscInt localIndex = 0;
1285c2e6f3c0SFlorian Wechsung     PetscInt localIndexWithArtificial = 0;
12860904074fSPatrick Farrell     PetscInt localIndexWithAll = 0;
12874bbf5ea8SMatthew G. Knepley     PetscInt dof, off, i, j, k, l;
12884bbf5ea8SMatthew G. Knepley 
12891b68eb51SMatthew G. Knepley     ierr = PetscHMapIClear(ht);CHKERRQ(ierr);
1290c2e6f3c0SFlorian Wechsung     ierr = PetscHMapIClear(htWithArtificial);CHKERRQ(ierr);
12910904074fSPatrick Farrell     ierr = PetscHMapIClear(htWithAll);CHKERRQ(ierr);
12924bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr);
12934bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr);
12944bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
12954bbf5ea8SMatthew G. Knepley 
1296557beb66SLawrence Mitchell     /* Calculate the global numbers of the artificial BC dofs here first */
1297557beb66SLawrence Mitchell     ierr = patch->patchconstructop((void*)patch, dm, v, ownedpts); CHKERRQ(ierr);
1298557beb66SLawrence Mitchell     ierr = PCPatchCompleteCellPatch(pc, ownedpts, seenpts); CHKERRQ(ierr);
1299e4c66b91SPatrick Farrell     ierr = PCPatchGetPointDofs(pc, ownedpts, owneddofs, v, &patch->subspaces_to_exclude); CHKERRQ(ierr);
1300e4c66b91SPatrick Farrell     ierr = PCPatchGetPointDofs(pc, seenpts, seendofs, v, NULL); CHKERRQ(ierr);
1301557beb66SLawrence Mitchell     ierr = PCPatchComputeSetDifference_Private(owneddofs, seendofs, artificialbcs); CHKERRQ(ierr);
13028135ed82SLawrence Mitchell     if (patch->viewPatches) {
13031b68eb51SMatthew G. Knepley       PetscHSetI globalbcdofs;
13041b68eb51SMatthew G. Knepley       PetscHashIter hi;
13058135ed82SLawrence Mitchell       MPI_Comm comm = PetscObjectComm((PetscObject)pc);
13061b68eb51SMatthew G. Knepley 
13071b68eb51SMatthew G. Knepley       ierr = PetscHSetICreate(&globalbcdofs);CHKERRQ(ierr);
13088135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: owned dofs:\n", v); CHKERRQ(ierr);
13091b68eb51SMatthew G. Knepley       PetscHashIterBegin(owneddofs, hi);
13101b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(owneddofs, hi)) {
13118135ed82SLawrence Mitchell         PetscInt globalDof;
13128135ed82SLawrence Mitchell 
13131b68eb51SMatthew G. Knepley         PetscHashIterGetKey(owneddofs, hi, globalDof);
13141b68eb51SMatthew G. Knepley         PetscHashIterNext(owneddofs, hi);
13158135ed82SLawrence Mitchell         ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
13168135ed82SLawrence Mitchell       }
13178135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n"); CHKERRQ(ierr);
13188135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: seen dofs:\n", v); CHKERRQ(ierr);
13191b68eb51SMatthew G. Knepley       PetscHashIterBegin(seendofs, hi);
13201b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(seendofs, hi)) {
13218135ed82SLawrence Mitchell         PetscInt globalDof;
13228135ed82SLawrence Mitchell         PetscBool flg;
13238135ed82SLawrence Mitchell 
13241b68eb51SMatthew G. Knepley         PetscHashIterGetKey(seendofs, hi, globalDof);
13251b68eb51SMatthew G. Knepley         PetscHashIterNext(seendofs, hi);
13268135ed82SLawrence Mitchell         ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
13278135ed82SLawrence Mitchell 
13281b68eb51SMatthew G. Knepley         ierr = PetscHSetIHas(globalBcs, globalDof, &flg);CHKERRQ(ierr);
13291b68eb51SMatthew G. Knepley         if (flg) {ierr = PetscHSetIAdd(globalbcdofs, globalDof);CHKERRQ(ierr);}
13308135ed82SLawrence Mitchell       }
13318135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n"); CHKERRQ(ierr);
13328135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: global BCs:\n", v); CHKERRQ(ierr);
13331b68eb51SMatthew G. Knepley       ierr = PetscHSetIGetSize(globalbcdofs, &numBcs);CHKERRQ(ierr);
13348135ed82SLawrence Mitchell       if (numBcs > 0) {
13351b68eb51SMatthew G. Knepley         PetscHashIterBegin(globalbcdofs, hi);
13361b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(globalbcdofs, hi)) {
13378135ed82SLawrence Mitchell           PetscInt globalDof;
13381b68eb51SMatthew G. Knepley           PetscHashIterGetKey(globalbcdofs, hi, globalDof);
13391b68eb51SMatthew G. Knepley           PetscHashIterNext(globalbcdofs, hi);
13408135ed82SLawrence Mitchell           ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof);CHKERRQ(ierr);
13418135ed82SLawrence Mitchell         }
13428135ed82SLawrence Mitchell       }
13438135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n");CHKERRQ(ierr);
13448135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: artificial BCs:\n", v);CHKERRQ(ierr);
13451b68eb51SMatthew G. Knepley       ierr = PetscHSetIGetSize(artificialbcs, &numBcs);CHKERRQ(ierr);
13468135ed82SLawrence Mitchell       if (numBcs > 0) {
13471b68eb51SMatthew G. Knepley         PetscHashIterBegin(artificialbcs, hi);
13481b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(artificialbcs, hi)) {
13498135ed82SLawrence Mitchell           PetscInt globalDof;
13501b68eb51SMatthew G. Knepley           PetscHashIterGetKey(artificialbcs, hi, globalDof);
13511b68eb51SMatthew G. Knepley           PetscHashIterNext(artificialbcs, hi);
13528135ed82SLawrence Mitchell           ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
13538135ed82SLawrence Mitchell         }
13548135ed82SLawrence Mitchell       }
13558135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n\n"); CHKERRQ(ierr);
13561b68eb51SMatthew G. Knepley       ierr = PetscHSetIDestroy(&globalbcdofs);CHKERRQ(ierr);
13578135ed82SLawrence Mitchell     }
13584bbf5ea8SMatthew G. Knepley    for (k = 0; k < patch->nsubspaces; ++k) {
13594bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
13604bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
13614bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
13624bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
13634bbf5ea8SMatthew G. Knepley 
13644bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
13654bbf5ea8SMatthew G. Knepley         /* Walk over the cells in this patch. */
13664bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
13675f824522SMatthew G. Knepley         PetscInt       cell = c;
13684bbf5ea8SMatthew G. Knepley 
13695f824522SMatthew G. Knepley         /* TODO Change this to an IS */
13705f824522SMatthew G. Knepley         if (cellNumbering) {
13714bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetDof(cellNumbering, c, &cell);CHKERRQ(ierr);
13724bbf5ea8SMatthew G. Knepley           if (cell <= 0) SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_OUTOFRANGE, "Cell %D doesn't appear in cell numbering map", c);
13734bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);
13745f824522SMatthew G. Knepley         }
13754bbf5ea8SMatthew G. Knepley         newCellsArray[i] = cell;
13764bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
13774bbf5ea8SMatthew G. Knepley           /* For each global dof, map it into contiguous local storage. */
13784bbf5ea8SMatthew G. Knepley           const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + subspaceOffset;
13794bbf5ea8SMatthew G. Knepley           /* finally, loop over block size */
13804bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
13811b68eb51SMatthew G. Knepley             PetscInt  localDof;
13821b68eb51SMatthew G. Knepley             PetscBool isGlobalBcDof, isArtificialBcDof;
13834bbf5ea8SMatthew G. Knepley 
1384557beb66SLawrence Mitchell             /* first, check if this is either a globally enforced or locally enforced BC dof */
13851b68eb51SMatthew G. Knepley             ierr = PetscHSetIHas(globalBcs, globalDof + l, &isGlobalBcDof);CHKERRQ(ierr);
13861b68eb51SMatthew G. Knepley             ierr = PetscHSetIHas(artificialbcs, globalDof + l, &isArtificialBcDof);CHKERRQ(ierr);
1387557beb66SLawrence Mitchell 
1388557beb66SLawrence Mitchell             /* if it's either, don't ever give it a local dof number */
13891b68eb51SMatthew G. Knepley             if (isGlobalBcDof || isArtificialBcDof) {
1390c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = -1; /* don't use this in assembly in this patch */
1391557beb66SLawrence Mitchell             } else {
13921b68eb51SMatthew G. Knepley               ierr = PetscHMapIGet(ht, globalDof + l, &localDof);CHKERRQ(ierr);
13934bbf5ea8SMatthew G. Knepley               if (localDof == -1) {
13944bbf5ea8SMatthew G. Knepley                 localDof = localIndex++;
13951b68eb51SMatthew G. Knepley                 ierr = PetscHMapISet(ht, globalDof + l, localDof);CHKERRQ(ierr);
13964bbf5ea8SMatthew G. Knepley               }
13974bbf5ea8SMatthew G. Knepley               if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
13984bbf5ea8SMatthew G. Knepley               /* And store. */
1399c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = localDof;
14004bbf5ea8SMatthew G. Knepley             }
1401c2e6f3c0SFlorian Wechsung 
14020904074fSPatrick Farrell             if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1403c2e6f3c0SFlorian Wechsung               if (isGlobalBcDof) {
1404e047a90bSFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = -1; /* don't use this in assembly in this patch */
1405c2e6f3c0SFlorian Wechsung               } else {
1406c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapIGet(htWithArtificial, globalDof + l, &localDof);CHKERRQ(ierr);
1407c2e6f3c0SFlorian Wechsung                 if (localDof == -1) {
1408c2e6f3c0SFlorian Wechsung                   localDof = localIndexWithArtificial++;
1409c2e6f3c0SFlorian Wechsung                   ierr = PetscHMapISet(htWithArtificial, globalDof + l, localDof);CHKERRQ(ierr);
1410c2e6f3c0SFlorian Wechsung                 }
1411c2e6f3c0SFlorian Wechsung                 if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
1412c2e6f3c0SFlorian Wechsung                 /* And store.*/
1413c2e6f3c0SFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = localDof;
1414c2e6f3c0SFlorian Wechsung               }
1415c2e6f3c0SFlorian Wechsung             }
14160904074fSPatrick Farrell 
14170904074fSPatrick Farrell             if(isNonlinear) {
14180904074fSPatrick Farrell               /* Build the dofmap for the function space with _all_ dofs,
14190904074fSPatrick Farrell                  including those in any kind of boundary condition */
14200904074fSPatrick Farrell               ierr = PetscHMapIGet(htWithAll, globalDof + l, &localDof);CHKERRQ(ierr);
14210904074fSPatrick Farrell               if (localDof == -1) {
14220904074fSPatrick Farrell                 localDof = localIndexWithAll++;
14230904074fSPatrick Farrell                 ierr = PetscHMapISet(htWithAll, globalDof + l, localDof);CHKERRQ(ierr);
14240904074fSPatrick Farrell               }
14250904074fSPatrick Farrell               if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
14260904074fSPatrick Farrell               /* And store.*/
14270904074fSPatrick Farrell               dofsArrayWithAll[globalIndex] = localDof;
14280904074fSPatrick Farrell             }
1429c2e6f3c0SFlorian Wechsung             globalIndex++;
14304bbf5ea8SMatthew G. Knepley           }
14314bbf5ea8SMatthew G. Knepley         }
14324bbf5ea8SMatthew G. Knepley       }
1433557beb66SLawrence Mitchell     }
14344bbf5ea8SMatthew G. Knepley      /*How many local dofs in this patch? */
14350904074fSPatrick Farrell    if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1436c2e6f3c0SFlorian Wechsung      ierr = PetscHMapIGetSize(htWithArtificial, &dof);CHKERRQ(ierr);
1437c2e6f3c0SFlorian Wechsung      ierr = PetscSectionSetDof(gtolCountsWithArtificial, v, dof);CHKERRQ(ierr);
1438c2e6f3c0SFlorian Wechsung    }
14390904074fSPatrick Farrell    if (isNonlinear) {
14400904074fSPatrick Farrell      ierr = PetscHMapIGetSize(htWithAll, &dof);CHKERRQ(ierr);
14410904074fSPatrick Farrell      ierr = PetscSectionSetDof(gtolCountsWithAll, v, dof);CHKERRQ(ierr);
14420904074fSPatrick Farrell    }
14431b68eb51SMatthew G. Knepley     ierr = PetscHMapIGetSize(ht, &dof);CHKERRQ(ierr);
14444bbf5ea8SMatthew G. Knepley     ierr = PetscSectionSetDof(gtolCounts, v, dof);CHKERRQ(ierr);
14454bbf5ea8SMatthew G. Knepley   }
14464bbf5ea8SMatthew 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);
14474bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetUp(gtolCounts);CHKERRQ(ierr);
14484bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(gtolCounts, &numGlobalDofs);CHKERRQ(ierr);
14494bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numGlobalDofs, &globalDofsArray);CHKERRQ(ierr);
14504bbf5ea8SMatthew G. Knepley 
14510904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1452c2e6f3c0SFlorian Wechsung     ierr = PetscSectionSetUp(gtolCountsWithArtificial);CHKERRQ(ierr);
1453c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetStorageSize(gtolCountsWithArtificial, &numGlobalDofsWithArtificial);CHKERRQ(ierr);
1454c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numGlobalDofsWithArtificial, &globalDofsArrayWithArtificial);CHKERRQ(ierr);
1455c2e6f3c0SFlorian Wechsung   }
14560904074fSPatrick Farrell   if (isNonlinear) {
14570904074fSPatrick Farrell     ierr = PetscSectionSetUp(gtolCountsWithAll);CHKERRQ(ierr);
14580904074fSPatrick Farrell     ierr = PetscSectionGetStorageSize(gtolCountsWithAll, &numGlobalDofsWithAll);CHKERRQ(ierr);
14590904074fSPatrick Farrell     ierr = PetscMalloc1(numGlobalDofsWithAll, &globalDofsArrayWithAll);CHKERRQ(ierr);
14600904074fSPatrick Farrell   }
14614bbf5ea8SMatthew 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. */
14624bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
14631b68eb51SMatthew G. Knepley     PetscHashIter hi;
14645f824522SMatthew G. Knepley     PetscInt      dof, off, Np, ooff, i, j, k, l;
14654bbf5ea8SMatthew G. Knepley 
14661b68eb51SMatthew G. Knepley     ierr = PetscHMapIClear(ht);CHKERRQ(ierr);
1467c2e6f3c0SFlorian Wechsung     ierr = PetscHMapIClear(htWithArtificial);CHKERRQ(ierr);
14680904074fSPatrick Farrell     ierr = PetscHMapIClear(htWithAll);CHKERRQ(ierr);
14694bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr);
14704bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr);
14715f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(pointCounts, v, &Np);CHKERRQ(ierr);
14725f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(pointCounts, v, &ooff);CHKERRQ(ierr);
14734bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
14744bbf5ea8SMatthew G. Knepley 
14754bbf5ea8SMatthew G. Knepley     for (k = 0; k < patch->nsubspaces; ++k) {
14764bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
14774bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
14784bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
14794bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
1480d490bb3dSLawrence Mitchell       PetscInt        goff;
14814bbf5ea8SMatthew G. Knepley 
14824bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
14834bbf5ea8SMatthew G. Knepley         /* Reconstruct mapping of global-to-local on this patch. */
14844bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
14855f824522SMatthew G. Knepley         PetscInt       cell = c;
14864bbf5ea8SMatthew G. Knepley 
14875f824522SMatthew G. Knepley         if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);}
14884bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
14894bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
14905f824522SMatthew G. Knepley             const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset;
1491c2e6f3c0SFlorian Wechsung             const PetscInt localDof  = dofsArray[key];
14921b68eb51SMatthew G. Knepley             if (localDof >= 0) {ierr = PetscHMapISet(ht, globalDof, localDof);CHKERRQ(ierr);}
14930904074fSPatrick Farrell             if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1494c2e6f3c0SFlorian Wechsung               const PetscInt localDofWithArtificial = dofsArrayWithArtificial[key];
1495c2e6f3c0SFlorian Wechsung               if (localDofWithArtificial >= 0) {
1496c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapISet(htWithArtificial, globalDof, localDofWithArtificial);CHKERRQ(ierr);
1497c2e6f3c0SFlorian Wechsung               }
1498c2e6f3c0SFlorian Wechsung             }
14990904074fSPatrick Farrell             if (isNonlinear) {
15000904074fSPatrick Farrell               const PetscInt localDofWithAll = dofsArrayWithAll[key];
15010904074fSPatrick Farrell               if (localDofWithAll >= 0) {
15020904074fSPatrick Farrell                 ierr = PetscHMapISet(htWithAll, globalDof, localDofWithAll);CHKERRQ(ierr);
15030904074fSPatrick Farrell               }
15040904074fSPatrick Farrell             }
1505c2e6f3c0SFlorian Wechsung             key++;
15064bbf5ea8SMatthew G. Knepley           }
15074bbf5ea8SMatthew G. Knepley         }
15084bbf5ea8SMatthew G. Knepley       }
1509557beb66SLawrence Mitchell 
15104bbf5ea8SMatthew G. Knepley       /* Shove it in the output data structure. */
15114bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetOffset(gtolCounts, v, &goff);CHKERRQ(ierr);
15121b68eb51SMatthew G. Knepley       PetscHashIterBegin(ht, hi);
15131b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(ht, hi)) {
15144bbf5ea8SMatthew G. Knepley         PetscInt globalDof, localDof;
15154bbf5ea8SMatthew G. Knepley 
15161b68eb51SMatthew G. Knepley         PetscHashIterGetKey(ht, hi, globalDof);
15171b68eb51SMatthew G. Knepley         PetscHashIterGetVal(ht, hi, localDof);
15184bbf5ea8SMatthew G. Knepley         if (globalDof >= 0) globalDofsArray[goff + localDof] = globalDof;
15191b68eb51SMatthew G. Knepley         PetscHashIterNext(ht, hi);
15204bbf5ea8SMatthew G. Knepley       }
15215f824522SMatthew G. Knepley 
15220904074fSPatrick Farrell       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1523c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(gtolCountsWithArtificial, v, &goff);CHKERRQ(ierr);
1524c2e6f3c0SFlorian Wechsung         PetscHashIterBegin(htWithArtificial, hi);
1525c2e6f3c0SFlorian Wechsung         while (!PetscHashIterAtEnd(htWithArtificial, hi)) {
1526c2e6f3c0SFlorian Wechsung           PetscInt globalDof, localDof;
1527c2e6f3c0SFlorian Wechsung           PetscHashIterGetKey(htWithArtificial, hi, globalDof);
1528c2e6f3c0SFlorian Wechsung           PetscHashIterGetVal(htWithArtificial, hi, localDof);
1529c2e6f3c0SFlorian Wechsung           if (globalDof >= 0) globalDofsArrayWithArtificial[goff + localDof] = globalDof;
1530c2e6f3c0SFlorian Wechsung           PetscHashIterNext(htWithArtificial, hi);
1531c2e6f3c0SFlorian Wechsung         }
1532c2e6f3c0SFlorian Wechsung       }
15330904074fSPatrick Farrell       if (isNonlinear) {
15340904074fSPatrick Farrell         ierr = PetscSectionGetOffset(gtolCountsWithAll, v, &goff);CHKERRQ(ierr);
15350904074fSPatrick Farrell         PetscHashIterBegin(htWithAll, hi);
15360904074fSPatrick Farrell         while (!PetscHashIterAtEnd(htWithAll, hi)) {
15370904074fSPatrick Farrell           PetscInt globalDof, localDof;
15380904074fSPatrick Farrell           PetscHashIterGetKey(htWithAll, hi, globalDof);
15390904074fSPatrick Farrell           PetscHashIterGetVal(htWithAll, hi, localDof);
15400904074fSPatrick Farrell           if (globalDof >= 0) globalDofsArrayWithAll[goff + localDof] = globalDof;
15410904074fSPatrick Farrell           PetscHashIterNext(htWithAll, hi);
15420904074fSPatrick Farrell         }
15430904074fSPatrick Farrell       }
1544c2e6f3c0SFlorian Wechsung 
15455f824522SMatthew G. Knepley       for (p = 0; p < Np; ++p) {
15465f824522SMatthew G. Knepley         const PetscInt point = pointsArray[ooff + p];
15475f824522SMatthew G. Knepley         PetscInt       globalDof, localDof;
15485f824522SMatthew G. Knepley 
15495f824522SMatthew G. Knepley         ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, point, NULL, &globalDof);CHKERRQ(ierr);
15501b68eb51SMatthew G. Knepley         ierr = PetscHMapIGet(ht, globalDof, &localDof);CHKERRQ(ierr);
15515f824522SMatthew G. Knepley         offsArray[(ooff + p)*Nf + k] = localDof;
15520904074fSPatrick Farrell         if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1553c2e6f3c0SFlorian Wechsung           ierr = PetscHMapIGet(htWithArtificial, globalDof, &localDof);CHKERRQ(ierr);
1554c2e6f3c0SFlorian Wechsung           offsArrayWithArtificial[(ooff + p)*Nf + k] = localDof;
1555c2e6f3c0SFlorian Wechsung         }
15560904074fSPatrick Farrell         if (isNonlinear) {
15570904074fSPatrick Farrell           ierr = PetscHMapIGet(htWithAll, globalDof, &localDof);CHKERRQ(ierr);
15580904074fSPatrick Farrell           offsArrayWithAll[(ooff + p)*Nf + k] = localDof;
15590904074fSPatrick Farrell         }
15605f824522SMatthew G. Knepley       }
15614bbf5ea8SMatthew G. Knepley     }
15624bbf5ea8SMatthew G. Knepley 
15630cd083f8SSatish Balay     ierr = PetscHSetIDestroy(&globalBcs);CHKERRQ(ierr);
15641b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&ownedpts);CHKERRQ(ierr);
15651b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&seenpts);CHKERRQ(ierr);
15661b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&owneddofs);CHKERRQ(ierr);
15671b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&seendofs);CHKERRQ(ierr);
15681b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&artificialbcs);CHKERRQ(ierr);
1569557beb66SLawrence Mitchell 
15704bbf5ea8SMatthew G. Knepley       /* At this point, we have a hash table ht built that maps globalDof -> localDof.
15714bbf5ea8SMatthew G. Knepley      We need to create the dof table laid out cellwise first, then by subspace,
15724bbf5ea8SMatthew G. Knepley      as the assembler assembles cell-wise and we need to stuff the different
15734bbf5ea8SMatthew G. Knepley      contributions of the different function spaces to the right places. So we loop
15744bbf5ea8SMatthew G. Knepley      over cells, then over subspaces. */
15754bbf5ea8SMatthew G. Knepley     if (patch->nsubspaces > 1) { /* for nsubspaces = 1, data we need is already in dofsArray */
15764bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
15774bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
15785f824522SMatthew G. Knepley         PetscInt       cell = c;
15794bbf5ea8SMatthew G. Knepley 
15805f824522SMatthew G. Knepley         if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);}
15814bbf5ea8SMatthew G. Knepley         for (k = 0; k < patch->nsubspaces; ++k) {
15824bbf5ea8SMatthew G. Knepley           const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
15834bbf5ea8SMatthew G. Knepley           PetscInt        nodesPerCell   = patch->nodesPerCell[k];
15844bbf5ea8SMatthew G. Knepley           PetscInt        subspaceOffset = patch->subspaceOffsets[k];
15854bbf5ea8SMatthew G. Knepley           PetscInt        bs             = patch->bs[k];
15864bbf5ea8SMatthew G. Knepley 
15874bbf5ea8SMatthew G. Knepley           for (j = 0; j < nodesPerCell; ++j) {
15884bbf5ea8SMatthew G. Knepley             for (l = 0; l < bs; ++l) {
15895f824522SMatthew G. Knepley               const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset;
15904bbf5ea8SMatthew G. Knepley               PetscInt       localDof;
15914bbf5ea8SMatthew G. Knepley 
15921b68eb51SMatthew G. Knepley               ierr = PetscHMapIGet(ht, globalDof, &localDof);CHKERRQ(ierr);
1593557beb66SLawrence Mitchell               /* If it's not in the hash table, i.e. is a BC dof,
15941b68eb51SMatthew G. Knepley                then the PetscHSetIMap above gives -1, which matches
1595557beb66SLawrence Mitchell                exactly the convention for PETSc's matrix assembly to
1596557beb66SLawrence Mitchell                ignore the dof. So we don't need to do anything here */
1597c2e6f3c0SFlorian Wechsung               asmArray[asmKey] = localDof;
15980904074fSPatrick Farrell               if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1599c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapIGet(htWithArtificial, globalDof, &localDof);CHKERRQ(ierr);
1600c2e6f3c0SFlorian Wechsung                 asmArrayWithArtificial[asmKey] = localDof;
1601c2e6f3c0SFlorian Wechsung               }
16020904074fSPatrick Farrell               if (isNonlinear) {
16030904074fSPatrick Farrell                 ierr = PetscHMapIGet(htWithAll, globalDof, &localDof);CHKERRQ(ierr);
16040904074fSPatrick Farrell                 asmArrayWithAll[asmKey] = localDof;
16050904074fSPatrick Farrell               }
1606c2e6f3c0SFlorian Wechsung               asmKey++;
16074bbf5ea8SMatthew G. Knepley             }
16084bbf5ea8SMatthew G. Knepley           }
16094bbf5ea8SMatthew G. Knepley         }
16104bbf5ea8SMatthew G. Knepley       }
16114bbf5ea8SMatthew G. Knepley     }
16124bbf5ea8SMatthew G. Knepley   }
1613c2e6f3c0SFlorian Wechsung   if (1 == patch->nsubspaces) {
1614c2e6f3c0SFlorian Wechsung     ierr = PetscMemcpy(asmArray, dofsArray, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
16150904074fSPatrick Farrell     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1616c2e6f3c0SFlorian Wechsung       ierr = PetscMemcpy(asmArrayWithArtificial, dofsArrayWithArtificial, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
1617c2e6f3c0SFlorian Wechsung     }
16180904074fSPatrick Farrell     if (isNonlinear) {
16190904074fSPatrick Farrell       ierr = PetscMemcpy(asmArrayWithAll, dofsArrayWithAll, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
16200904074fSPatrick Farrell     }
1621c2e6f3c0SFlorian Wechsung   }
16224bbf5ea8SMatthew G. Knepley 
16231b68eb51SMatthew G. Knepley   ierr = PetscHMapIDestroy(&ht);CHKERRQ(ierr);
1624c2e6f3c0SFlorian Wechsung   ierr = PetscHMapIDestroy(&htWithArtificial);CHKERRQ(ierr);
16250904074fSPatrick Farrell   ierr = PetscHMapIDestroy(&htWithAll);CHKERRQ(ierr);
16264bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(cells, &cellsArray);CHKERRQ(ierr);
16275f824522SMatthew G. Knepley   ierr = ISRestoreIndices(points, &pointsArray);CHKERRQ(ierr);
16284bbf5ea8SMatthew G. Knepley   ierr = PetscFree(dofsArray);CHKERRQ(ierr);
16290904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1630c2e6f3c0SFlorian Wechsung     ierr = PetscFree(dofsArrayWithArtificial);CHKERRQ(ierr);
1631c2e6f3c0SFlorian Wechsung   }
16320904074fSPatrick Farrell   if (isNonlinear) {
16330904074fSPatrick Farrell     ierr = PetscFree(dofsArrayWithAll);CHKERRQ(ierr);
16340904074fSPatrick Farrell   }
16355f824522SMatthew G. Knepley   /* Create placeholder section for map from points to patch dofs */
16365f824522SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->patchSection);CHKERRQ(ierr);
16375f824522SMatthew G. Knepley   ierr = PetscSectionSetNumFields(patch->patchSection, patch->nsubspaces);CHKERRQ(ierr);
16381e5fa6bbSLawrence Mitchell   if (patch->combined) {
16391e5fa6bbSLawrence Mitchell     PetscInt numFields;
16401e5fa6bbSLawrence Mitchell     ierr = PetscSectionGetNumFields(patch->dofSection[0], &numFields);CHKERRQ(ierr);
16411e5fa6bbSLawrence 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);
16425f824522SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->dofSection[0], &pStart, &pEnd);CHKERRQ(ierr);
16435f824522SMatthew G. Knepley     ierr = PetscSectionSetChart(patch->patchSection, pStart, pEnd);CHKERRQ(ierr);
16445f824522SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
16455f824522SMatthew G. Knepley       PetscInt dof, fdof, f;
16465f824522SMatthew G. Knepley 
16475f824522SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->dofSection[0], p, &dof);CHKERRQ(ierr);
16485f824522SMatthew G. Knepley       ierr = PetscSectionSetDof(patch->patchSection, p, dof);CHKERRQ(ierr);
16495f824522SMatthew G. Knepley       for (f = 0; f < patch->nsubspaces; ++f) {
16501e5fa6bbSLawrence Mitchell         ierr = PetscSectionGetFieldDof(patch->dofSection[0], p, f, &fdof);CHKERRQ(ierr);
16515f824522SMatthew G. Knepley         ierr = PetscSectionSetFieldDof(patch->patchSection, p, f, fdof);CHKERRQ(ierr);
16525f824522SMatthew G. Knepley       }
16531e5fa6bbSLawrence Mitchell     }
16541e5fa6bbSLawrence Mitchell   } else {
16551e5fa6bbSLawrence Mitchell     PetscInt pStartf, pEndf, f;
16561e5fa6bbSLawrence Mitchell     pStart = PETSC_MAX_INT;
16571e5fa6bbSLawrence Mitchell     pEnd = PETSC_MIN_INT;
16581e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
16591e5fa6bbSLawrence Mitchell       ierr = PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf);CHKERRQ(ierr);
16601e5fa6bbSLawrence Mitchell       pStart = PetscMin(pStart, pStartf);
16611e5fa6bbSLawrence Mitchell       pEnd = PetscMax(pEnd, pEndf);
16621e5fa6bbSLawrence Mitchell     }
16631e5fa6bbSLawrence Mitchell     ierr = PetscSectionSetChart(patch->patchSection, pStart, pEnd);CHKERRQ(ierr);
16641e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
16651e5fa6bbSLawrence Mitchell       ierr = PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf);CHKERRQ(ierr);
16661e5fa6bbSLawrence Mitchell       for (p = pStartf; p < pEndf; ++p) {
16671e5fa6bbSLawrence Mitchell         PetscInt fdof;
16681e5fa6bbSLawrence Mitchell         ierr = PetscSectionGetDof(patch->dofSection[f], p, &fdof);CHKERRQ(ierr);
16691e5fa6bbSLawrence Mitchell         ierr = PetscSectionAddDof(patch->patchSection, p, fdof);CHKERRQ(ierr);
16701e5fa6bbSLawrence Mitchell         ierr = PetscSectionSetFieldDof(patch->patchSection, p, f, fdof);CHKERRQ(ierr);
1671bdd9e0cdSPatrick Farrell       }
1672bdd9e0cdSPatrick Farrell     }
16735f824522SMatthew G. Knepley   }
16745f824522SMatthew G. Knepley   ierr = PetscSectionSetUp(patch->patchSection);CHKERRQ(ierr);
16755f824522SMatthew G. Knepley   ierr = PetscSectionSetUseFieldOffsets(patch->patchSection, PETSC_TRUE);CHKERRQ(ierr);
16764bbf5ea8SMatthew G. Knepley   /* Replace cell indices with firedrake-numbered ones. */
16774bbf5ea8SMatthew G. Knepley   ierr = ISGeneralSetIndices(cells, numCells, (const PetscInt *) newCellsArray, PETSC_OWN_POINTER);CHKERRQ(ierr);
16784bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofs, globalDofsArray, PETSC_OWN_POINTER, &patch->gtol);CHKERRQ(ierr);
16795f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->gtol, "Global Indices");CHKERRQ(ierr);
168010534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_g2l_view", patch->classname);CHKERRQ(ierr);
168110534d48SPatrick Farrell   ierr = PetscSectionViewFromOptions(patch->gtolCounts, (PetscObject) pc, option);CHKERRQ(ierr);
168210534d48SPatrick Farrell   ierr = ISViewFromOptions(patch->gtol, (PetscObject) pc, option);CHKERRQ(ierr);
16834bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArray, PETSC_OWN_POINTER, &patch->dofs);CHKERRQ(ierr);
16845f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArray, PETSC_OWN_POINTER, &patch->offs);CHKERRQ(ierr);
16850904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1686c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithArtificial, globalDofsArrayWithArtificial, PETSC_OWN_POINTER, &patch->gtolWithArtificial);CHKERRQ(ierr);
1687c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithArtificial, PETSC_OWN_POINTER, &patch->dofsWithArtificial);CHKERRQ(ierr);
1688c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithArtificial, PETSC_OWN_POINTER, &patch->offsWithArtificial);CHKERRQ(ierr);
1689c2e6f3c0SFlorian Wechsung   }
16900904074fSPatrick Farrell   if (isNonlinear) {
16910904074fSPatrick Farrell     ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithAll, globalDofsArrayWithAll, PETSC_OWN_POINTER, &patch->gtolWithAll);CHKERRQ(ierr);
16920904074fSPatrick Farrell     ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithAll, PETSC_OWN_POINTER, &patch->dofsWithAll);CHKERRQ(ierr);
16930904074fSPatrick Farrell     ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithAll, PETSC_OWN_POINTER, &patch->offsWithAll);CHKERRQ(ierr);
16940904074fSPatrick Farrell   }
16954bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
16964bbf5ea8SMatthew G. Knepley }
16974bbf5ea8SMatthew G. Knepley 
1698c2e6f3c0SFlorian Wechsung static PetscErrorCode PCPatchCreateMatrix_Private(PC pc, PetscInt point, Mat *mat, PetscBool withArtificial)
16994bbf5ea8SMatthew G. Knepley {
17004bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
17014bbf5ea8SMatthew G. Knepley   Vec            x, y;
17024bbf5ea8SMatthew G. Knepley   PetscBool      flg;
17034bbf5ea8SMatthew G. Knepley   PetscInt       csize, rsize;
17044bbf5ea8SMatthew G. Knepley   const char    *prefix = NULL;
17054bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
17064bbf5ea8SMatthew G. Knepley 
17074bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1708c2e6f3c0SFlorian Wechsung   if(withArtificial) {
1709e047a90bSFlorian Wechsung     /* would be nice if we could create a rectangular matrix of size numDofsWithArtificial x numDofs here */
17101202d238SPatrick Farrell     x = patch->patchRHSWithArtificial[point];
17111202d238SPatrick Farrell     y = patch->patchRHSWithArtificial[point];
1712ff201f6aSFlorian Wechsung   } else {
17131202d238SPatrick Farrell     x = patch->patchRHS[point];
17141202d238SPatrick Farrell     y = patch->patchUpdate[point];
1715c2e6f3c0SFlorian Wechsung   }
1716c2e6f3c0SFlorian Wechsung 
17174bbf5ea8SMatthew G. Knepley   ierr = VecGetSize(x, &csize);CHKERRQ(ierr);
17184bbf5ea8SMatthew G. Knepley   ierr = VecGetSize(y, &rsize);CHKERRQ(ierr);
17194bbf5ea8SMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, mat);CHKERRQ(ierr);
17204bbf5ea8SMatthew G. Knepley   ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr);
17214bbf5ea8SMatthew G. Knepley   ierr = MatSetOptionsPrefix(*mat, prefix);CHKERRQ(ierr);
17225f824522SMatthew G. Knepley   ierr = MatAppendOptionsPrefix(*mat, "pc_patch_sub_");CHKERRQ(ierr);
17234bbf5ea8SMatthew G. Knepley   if (patch->sub_mat_type)       {ierr = MatSetType(*mat, patch->sub_mat_type);CHKERRQ(ierr);}
17247974b488SMatthew G. Knepley   else if (!patch->sub_mat_type) {ierr = MatSetType(*mat, MATDENSE);CHKERRQ(ierr);}
17254bbf5ea8SMatthew G. Knepley   ierr = MatSetSizes(*mat, rsize, csize, rsize, csize);CHKERRQ(ierr);
17264bbf5ea8SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) *mat, MATDENSE, &flg);CHKERRQ(ierr);
17274bbf5ea8SMatthew G. Knepley   if (!flg) {ierr = PetscObjectTypeCompare((PetscObject)*mat, MATSEQDENSE, &flg);CHKERRQ(ierr);}
17284bbf5ea8SMatthew G. Knepley   /* Sparse patch matrices */
17294bbf5ea8SMatthew G. Knepley   if (!flg) {
17304bbf5ea8SMatthew G. Knepley     PetscBT         bt;
17314bbf5ea8SMatthew G. Knepley     PetscInt       *dnnz      = NULL;
17324bbf5ea8SMatthew G. Knepley     const PetscInt *dofsArray = NULL;
17334bbf5ea8SMatthew G. Knepley     PetscInt        pStart, pEnd, ncell, offset, c, i, j;
17344bbf5ea8SMatthew G. Knepley 
1735c2e6f3c0SFlorian Wechsung     if(withArtificial) {
1736c2e6f3c0SFlorian Wechsung       ierr = ISGetIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1737ff201f6aSFlorian Wechsung     } else {
17384bbf5ea8SMatthew G. Knepley       ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1739c2e6f3c0SFlorian Wechsung     }
17404bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
17414bbf5ea8SMatthew G. Knepley     point += pStart;
17424bbf5ea8SMatthew 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);
17434bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
17444bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
17454bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventBegin(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr);
1746b2866507SPatrick Farrell     /* A PetscBT uses N^2 bits to store the sparsity pattern on a
17474bbf5ea8SMatthew G. Knepley      * patch. This is probably OK if the patches are not too big,
1748b2866507SPatrick Farrell      * but uses too much memory. We therefore switch based on rsize. */
1749b2866507SPatrick Farrell     if (rsize < 3000) { /* FIXME: I picked this switch value out of my hat */
1750d63cebbaSPatrick Farrell       PetscScalar *zeroes;
1751d63cebbaSPatrick Farrell       PetscInt rows;
1752d63cebbaSPatrick Farrell 
1753b2866507SPatrick Farrell       ierr = PetscCalloc1(rsize, &dnnz);CHKERRQ(ierr);
17544bbf5ea8SMatthew G. Knepley       ierr = PetscBTCreate(rsize*rsize, &bt);CHKERRQ(ierr);
17554bbf5ea8SMatthew G. Knepley       for (c = 0; c < ncell; ++c) {
17564bbf5ea8SMatthew G. Knepley         const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell;
17574bbf5ea8SMatthew G. Knepley         for (i = 0; i < patch->totalDofsPerCell; ++i) {
17584bbf5ea8SMatthew G. Knepley           const PetscInt row = idx[i];
1759557beb66SLawrence Mitchell           if (row < 0) continue;
17604bbf5ea8SMatthew G. Knepley           for (j = 0; j < patch->totalDofsPerCell; ++j) {
17614bbf5ea8SMatthew G. Knepley             const PetscInt col = idx[j];
17624bbf5ea8SMatthew G. Knepley             const PetscInt key = row*rsize + col;
1763557beb66SLawrence Mitchell             if (col < 0) continue;
17644bbf5ea8SMatthew G. Knepley             if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
17654bbf5ea8SMatthew G. Knepley           }
17664bbf5ea8SMatthew G. Knepley         }
17674bbf5ea8SMatthew G. Knepley       }
1768d63cebbaSPatrick Farrell 
1769d63cebbaSPatrick Farrell       if (patch->usercomputeopintfacet) {
1770d63cebbaSPatrick Farrell         const PetscInt *intFacetsArray = NULL;
1771d63cebbaSPatrick Farrell         PetscInt i, numIntFacets, intFacetOffset;
1772d63cebbaSPatrick Farrell         const PetscInt *facetCells = NULL;
1773d63cebbaSPatrick Farrell 
1774d63cebbaSPatrick Farrell         ierr = PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets);CHKERRQ(ierr);
1775d63cebbaSPatrick Farrell         ierr = PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset);CHKERRQ(ierr);
1776d63cebbaSPatrick Farrell         ierr = ISGetIndices(patch->intFacetsToPatchCell, &facetCells);CHKERRQ(ierr);
1777d63cebbaSPatrick Farrell         ierr = ISGetIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
1778d63cebbaSPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
1779d63cebbaSPatrick Farrell           const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0];
1780d63cebbaSPatrick Farrell           const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1];
1781d63cebbaSPatrick Farrell           PetscInt celli, cellj;
1782d63cebbaSPatrick Farrell 
1783d63cebbaSPatrick Farrell           for (celli = 0; celli < patch->totalDofsPerCell; celli++) {
1784d63cebbaSPatrick Farrell             const PetscInt row = dofsArray[(offset + cell0)*patch->totalDofsPerCell + celli];
1785b5c64f08SPatrick Farrell             if (row < 0) continue;
1786d63cebbaSPatrick Farrell             for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) {
1787d63cebbaSPatrick Farrell                 const PetscInt col = dofsArray[(offset + cell1)*patch->totalDofsPerCell + cellj];
1788d63cebbaSPatrick Farrell                 const PetscInt key = row*rsize + col;
1789d63cebbaSPatrick Farrell                 if (col < 0) continue;
1790d63cebbaSPatrick Farrell                 if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
1791d63cebbaSPatrick Farrell             }
1792d63cebbaSPatrick Farrell           }
1793d63cebbaSPatrick Farrell 
1794d63cebbaSPatrick Farrell           for (celli = 0; celli < patch->totalDofsPerCell; celli++) {
1795d63cebbaSPatrick Farrell             const PetscInt row = dofsArray[(offset + cell1)*patch->totalDofsPerCell + celli];
1796b5c64f08SPatrick Farrell             if (row < 0) continue;
1797d63cebbaSPatrick Farrell             for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) {
1798d63cebbaSPatrick Farrell                 const PetscInt col = dofsArray[(offset + cell0)*patch->totalDofsPerCell + cellj];
1799d63cebbaSPatrick Farrell                 const PetscInt key = row*rsize + col;
1800d63cebbaSPatrick Farrell                 if (col < 0) continue;
1801d63cebbaSPatrick Farrell                 if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
1802d63cebbaSPatrick Farrell             }
1803d63cebbaSPatrick Farrell           }
1804d63cebbaSPatrick Farrell         }
1805d63cebbaSPatrick Farrell       }
18064bbf5ea8SMatthew G. Knepley       ierr = PetscBTDestroy(&bt);CHKERRQ(ierr);
18074bbf5ea8SMatthew G. Knepley       ierr = MatXAIJSetPreallocation(*mat, 1, dnnz, NULL, NULL, NULL);CHKERRQ(ierr);
18084bbf5ea8SMatthew G. Knepley       ierr = PetscFree(dnnz);CHKERRQ(ierr);
1809d63cebbaSPatrick Farrell 
1810d63cebbaSPatrick Farrell       ierr = PetscCalloc1(patch->totalDofsPerCell*patch->totalDofsPerCell, &zeroes);CHKERRQ(ierr);
1811d63cebbaSPatrick Farrell       for (c = 0; c < ncell; ++c) {
1812d63cebbaSPatrick Farrell         const PetscInt *idx = &dofsArray[(offset + c)*patch->totalDofsPerCell];
1813d63cebbaSPatrick Farrell         ierr = MatSetValues(*mat, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, zeroes, INSERT_VALUES);CHKERRQ(ierr);
1814d63cebbaSPatrick Farrell       }
1815d63cebbaSPatrick Farrell       ierr = MatGetLocalSize(*mat, &rows, NULL);CHKERRQ(ierr);
1816d63cebbaSPatrick Farrell       for (i = 0; i < rows; ++i) {
1817d63cebbaSPatrick Farrell         ierr = MatSetValues(*mat, 1, &i, 1, &i, zeroes, INSERT_VALUES);CHKERRQ(ierr);
1818d63cebbaSPatrick Farrell       }
1819d63cebbaSPatrick Farrell 
1820d63cebbaSPatrick Farrell       if (patch->usercomputeopintfacet) {
1821d63cebbaSPatrick Farrell         const PetscInt *intFacetsArray = NULL;
1822d63cebbaSPatrick Farrell         PetscInt i, numIntFacets, intFacetOffset;
1823d63cebbaSPatrick Farrell         const PetscInt *facetCells = NULL;
1824d63cebbaSPatrick Farrell 
1825d63cebbaSPatrick Farrell         ierr = PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets);CHKERRQ(ierr);
1826d63cebbaSPatrick Farrell         ierr = PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset);CHKERRQ(ierr);
1827d63cebbaSPatrick Farrell         ierr = ISGetIndices(patch->intFacetsToPatchCell, &facetCells);CHKERRQ(ierr);
1828d63cebbaSPatrick Farrell         ierr = ISGetIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
1829d63cebbaSPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
1830d63cebbaSPatrick Farrell           const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0];
1831d63cebbaSPatrick Farrell           const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1];
1832d63cebbaSPatrick Farrell           const PetscInt *cell0idx = &dofsArray[(offset + cell0)*patch->totalDofsPerCell];
1833d63cebbaSPatrick Farrell           const PetscInt *cell1idx = &dofsArray[(offset + cell1)*patch->totalDofsPerCell];
1834d63cebbaSPatrick Farrell           ierr = MatSetValues(*mat, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, zeroes, INSERT_VALUES);CHKERRQ(ierr);
1835d63cebbaSPatrick Farrell           ierr = MatSetValues(*mat, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, zeroes, INSERT_VALUES);CHKERRQ(ierr);
1836d63cebbaSPatrick Farrell         }
1837d63cebbaSPatrick Farrell       }
1838d63cebbaSPatrick Farrell 
1839d63cebbaSPatrick Farrell       ierr = MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1840d63cebbaSPatrick Farrell       ierr = MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1841d63cebbaSPatrick Farrell 
1842d63cebbaSPatrick Farrell       ierr = PetscFree(zeroes);CHKERRQ(ierr);
1843d63cebbaSPatrick Farrell 
1844b2866507SPatrick Farrell     } else { /* rsize too big, use MATPREALLOCATOR */
1845b2866507SPatrick Farrell       Mat preallocator;
1846b2866507SPatrick Farrell       PetscScalar* vals;
1847b2866507SPatrick Farrell 
1848b2866507SPatrick Farrell       ierr = PetscCalloc1(patch->totalDofsPerCell*patch->totalDofsPerCell, &vals);CHKERRQ(ierr);
1849b2866507SPatrick Farrell       ierr = MatCreate(PETSC_COMM_SELF, &preallocator);CHKERRQ(ierr);
1850b2866507SPatrick Farrell       ierr = MatSetType(preallocator, MATPREALLOCATOR);CHKERRQ(ierr);
1851b2866507SPatrick Farrell       ierr = MatSetSizes(preallocator, rsize, rsize, rsize, rsize);CHKERRQ(ierr);
1852b2866507SPatrick Farrell       ierr = MatSetUp(preallocator);CHKERRQ(ierr);
185311bcd083SPatrick Farrell 
1854b2866507SPatrick Farrell       for (c = 0; c < ncell; ++c) {
1855b2866507SPatrick Farrell         const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell;
1856b2866507SPatrick Farrell         ierr = MatSetValues(preallocator, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, vals, INSERT_VALUES);CHKERRQ(ierr);
1857b2866507SPatrick Farrell       }
185811bcd083SPatrick Farrell 
185911bcd083SPatrick Farrell       if (patch->usercomputeopintfacet) {
186011bcd083SPatrick Farrell         const PetscInt *intFacetsArray = NULL;
186111bcd083SPatrick Farrell         PetscInt i, numIntFacets, intFacetOffset;
186211bcd083SPatrick Farrell         const PetscInt *facetCells = NULL;
186311bcd083SPatrick Farrell 
186411bcd083SPatrick Farrell         ierr = PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets);CHKERRQ(ierr);
186511bcd083SPatrick Farrell         ierr = PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset);CHKERRQ(ierr);
186611bcd083SPatrick Farrell         ierr = ISGetIndices(patch->intFacetsToPatchCell, &facetCells);CHKERRQ(ierr);
186711bcd083SPatrick Farrell         ierr = ISGetIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
186811bcd083SPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
186911bcd083SPatrick Farrell           const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0];
187011bcd083SPatrick Farrell           const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1];
187111bcd083SPatrick Farrell           const PetscInt *cell0idx = &dofsArray[(offset + cell0)*patch->totalDofsPerCell];
187211bcd083SPatrick Farrell           const PetscInt *cell1idx = &dofsArray[(offset + cell1)*patch->totalDofsPerCell];
187311bcd083SPatrick Farrell           ierr = MatSetValues(preallocator, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, vals, INSERT_VALUES);CHKERRQ(ierr);
187411bcd083SPatrick Farrell           ierr = MatSetValues(preallocator, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, vals, INSERT_VALUES);CHKERRQ(ierr);
187511bcd083SPatrick Farrell         }
187611bcd083SPatrick Farrell       }
187711bcd083SPatrick Farrell 
1878b2866507SPatrick Farrell       ierr = PetscFree(vals);CHKERRQ(ierr);
1879b2866507SPatrick Farrell       ierr = MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1880b2866507SPatrick Farrell       ierr = MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1881b2866507SPatrick Farrell       ierr = MatPreallocatorPreallocate(preallocator, PETSC_TRUE, *mat);CHKERRQ(ierr);
1882b2866507SPatrick Farrell       ierr = MatDestroy(&preallocator);CHKERRQ(ierr);
1883b2866507SPatrick Farrell     }
18844bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr);
1885fe117d09SFlorian Wechsung     if(withArtificial) {
1886fe117d09SFlorian Wechsung       ierr = ISRestoreIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1887fe117d09SFlorian Wechsung     } else {
18884bbf5ea8SMatthew G. Knepley       ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
18894bbf5ea8SMatthew G. Knepley     }
1890fe117d09SFlorian Wechsung   }
18914bbf5ea8SMatthew G. Knepley   ierr = MatSetUp(*mat);CHKERRQ(ierr);
18924bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
18934bbf5ea8SMatthew G. Knepley }
18944bbf5ea8SMatthew G. Knepley 
18950904074fSPatrick 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)
189692d50984SMatthew G. Knepley {
189792d50984SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
189892d50984SMatthew G. Knepley   DM              dm;
189992d50984SMatthew G. Knepley   PetscSection    s;
190092d50984SMatthew G. Knepley   const PetscInt *parray, *oarray;
190192d50984SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
190292d50984SMatthew G. Knepley   PetscErrorCode  ierr;
190392d50984SMatthew G. Knepley 
190492d50984SMatthew G. Knepley   PetscFunctionBegin;
1905fa84ea4cSLawrence Mitchell   if (patch->precomputeElementTensors) SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Precomputing element tensors not implemented with DMPlex compute operator");
190692d50984SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
190792d50984SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
190892d50984SMatthew G. Knepley   /* Set offset into patch */
190992d50984SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->pointCounts, patchNum, &Np);CHKERRQ(ierr);
191092d50984SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->pointCounts, patchNum, &poff);CHKERRQ(ierr);
191192d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->points, &parray);CHKERRQ(ierr);
191292d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->offs,   &oarray);CHKERRQ(ierr);
191392d50984SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
191492d50984SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
191592d50984SMatthew G. Knepley       const PetscInt point = parray[poff+p];
191692d50984SMatthew G. Knepley       PetscInt       dof;
191792d50984SMatthew G. Knepley 
191892d50984SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(patch->patchSection, point, f, &dof);CHKERRQ(ierr);
191992d50984SMatthew G. Knepley       ierr = PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);
192092d50984SMatthew G. Knepley       if (patch->nsubspaces == 1) {ierr = PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);}
192192d50984SMatthew G. Knepley       else                        {ierr = PetscSectionSetOffset(patch->patchSection, point, -1);CHKERRQ(ierr);}
192292d50984SMatthew G. Knepley     }
192392d50984SMatthew G. Knepley   }
192492d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->points, &parray);CHKERRQ(ierr);
192592d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->offs,   &oarray);CHKERRQ(ierr);
192692d50984SMatthew G. Knepley   if (patch->viewSection) {ierr = ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection);CHKERRQ(ierr);}
192792d50984SMatthew G. Knepley   ierr = DMPlexComputeResidual_Patch_Internal(pc->dm, patch->patchSection, cellIS, 0.0, x, NULL, F, ctx);CHKERRQ(ierr);
192892d50984SMatthew G. Knepley   PetscFunctionReturn(0);
192992d50984SMatthew G. Knepley }
193092d50984SMatthew G. Knepley 
193192d50984SMatthew G. Knepley PetscErrorCode PCPatchComputeFunction_Internal(PC pc, Vec x, Vec F, PetscInt point)
193292d50984SMatthew G. Knepley {
193392d50984SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
193492d50984SMatthew G. Knepley   const PetscInt *dofsArray;
19350904074fSPatrick Farrell   const PetscInt *dofsArrayWithAll;
193692d50984SMatthew G. Knepley   const PetscInt *cellsArray;
193792d50984SMatthew G. Knepley   PetscInt        ncell, offset, pStart, pEnd;
193892d50984SMatthew G. Knepley   PetscErrorCode  ierr;
193992d50984SMatthew G. Knepley 
194092d50984SMatthew G. Knepley   PetscFunctionBegin;
194192d50984SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
194292d50984SMatthew G. Knepley   if (!patch->usercomputeop) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback\n");
194392d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
19440904074fSPatrick Farrell   ierr = ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
194592d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
194692d50984SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
194792d50984SMatthew G. Knepley 
194892d50984SMatthew G. Knepley   point += pStart;
194992d50984SMatthew 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);
195092d50984SMatthew G. Knepley 
195192d50984SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
195292d50984SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
195392d50984SMatthew G. Knepley   if (ncell <= 0) {
195492d50984SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
195592d50984SMatthew G. Knepley     PetscFunctionReturn(0);
195692d50984SMatthew G. Knepley   }
195792d50984SMatthew G. Knepley   PetscStackPush("PCPatch user callback");
195892d50984SMatthew G. Knepley   /* Cannot reuse the same IS because the geometry info is being cached in it */
195992d50984SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS);CHKERRQ(ierr);
196039fd2e8aSPatrick Farrell   ierr = patch->usercomputef(pc, point, x, F, patch->cellIS, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell,
19610904074fSPatrick Farrell                                                                                             dofsArrayWithAll + offset*patch->totalDofsPerCell,
196239fd2e8aSPatrick Farrell                                                                                             patch->usercomputefctx);CHKERRQ(ierr);
196392d50984SMatthew G. Knepley   PetscStackPop;
196492d50984SMatthew G. Knepley   ierr = ISDestroy(&patch->cellIS);CHKERRQ(ierr);
196592d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
19660904074fSPatrick Farrell   ierr = ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
196792d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
196892d50984SMatthew G. Knepley   if (patch->viewMatrix) {
196992d50984SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
197092d50984SMatthew G. Knepley 
197192d50984SMatthew G. Knepley     ierr = PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch vector for Point %D", point);CHKERRQ(ierr);
197292d50984SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) F, name);CHKERRQ(ierr);
197392d50984SMatthew G. Knepley     ierr = ObjectView((PetscObject) F, patch->viewerMatrix, patch->formatMatrix);CHKERRQ(ierr);
197492d50984SMatthew G. Knepley   }
197592d50984SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
197692d50984SMatthew G. Knepley   PetscFunctionReturn(0);
197792d50984SMatthew G. Knepley }
197892d50984SMatthew G. Knepley 
19790904074fSPatrick 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)
19805f824522SMatthew G. Knepley {
19815f824522SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
19825f824522SMatthew G. Knepley   DM              dm;
19835f824522SMatthew G. Knepley   PetscSection    s;
19845f824522SMatthew G. Knepley   const PetscInt *parray, *oarray;
19855f824522SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
19865f824522SMatthew G. Knepley   PetscErrorCode  ierr;
19875f824522SMatthew G. Knepley 
19885f824522SMatthew G. Knepley   PetscFunctionBegin;
19895f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
19905f824522SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
19915f824522SMatthew G. Knepley   /* Set offset into patch */
19925f824522SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->pointCounts, patchNum, &Np);CHKERRQ(ierr);
19935f824522SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->pointCounts, patchNum, &poff);CHKERRQ(ierr);
19945f824522SMatthew G. Knepley   ierr = ISGetIndices(patch->points, &parray);CHKERRQ(ierr);
19955f824522SMatthew G. Knepley   ierr = ISGetIndices(patch->offs,   &oarray);CHKERRQ(ierr);
19965f824522SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
19975f824522SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
19985f824522SMatthew G. Knepley       const PetscInt point = parray[poff+p];
19995f824522SMatthew G. Knepley       PetscInt       dof;
20005f824522SMatthew G. Knepley 
20015f824522SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(patch->patchSection, point, f, &dof);CHKERRQ(ierr);
20025f824522SMatthew G. Knepley       ierr = PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);
20035f824522SMatthew G. Knepley       if (patch->nsubspaces == 1) {ierr = PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);}
20045f824522SMatthew G. Knepley       else                        {ierr = PetscSectionSetOffset(patch->patchSection, point, -1);CHKERRQ(ierr);}
20055f824522SMatthew G. Knepley     }
20065f824522SMatthew G. Knepley   }
20075f824522SMatthew G. Knepley   ierr = ISRestoreIndices(patch->points, &parray);CHKERRQ(ierr);
20085f824522SMatthew G. Knepley   ierr = ISRestoreIndices(patch->offs,   &oarray);CHKERRQ(ierr);
20095f824522SMatthew G. Knepley   if (patch->viewSection) {ierr = ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection);CHKERRQ(ierr);}
20105f824522SMatthew G. Knepley   /* TODO Shut off MatViewFromOptions() in MatAssemblyEnd() here */
2011723f9013SMatthew G. Knepley   ierr = DMPlexComputeJacobian_Patch_Internal(pc->dm, patch->patchSection, patch->patchSection, cellIS, 0.0, 0.0, x, NULL, J, J, ctx);CHKERRQ(ierr);
20125f824522SMatthew G. Knepley   PetscFunctionReturn(0);
20135f824522SMatthew G. Knepley }
20145f824522SMatthew G. Knepley 
201534d8b122SPatrick Farrell PetscErrorCode PCPatchComputeOperator_Internal(PC pc, Vec x, Mat mat, PetscInt point, PetscBool withArtificial)
20164bbf5ea8SMatthew G. Knepley {
20174bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
20184bbf5ea8SMatthew G. Knepley   const PetscInt *dofsArray;
20190904074fSPatrick Farrell   const PetscInt *dofsArrayWithAll = NULL;
20204bbf5ea8SMatthew G. Knepley   const PetscInt *cellsArray;
2021eb62eeaaSLawrence Mitchell   PetscInt        ncell, offset, pStart, pEnd, numIntFacets, intFacetOffset;
20224d04e9f1SPatrick Farrell   PetscBool       isNonlinear;
20234bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
20244bbf5ea8SMatthew G. Knepley 
20254bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
20264bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
2027debbdec3SPatrick Farrell   isNonlinear = patch->isNonlinear;
20284bbf5ea8SMatthew G. Knepley   if (!patch->usercomputeop) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback\n");
2029c2e6f3c0SFlorian Wechsung   if(withArtificial) {
2030c2e6f3c0SFlorian Wechsung     ierr = ISGetIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
2031c2e6f3c0SFlorian Wechsung   } else {
20324bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
2033c2e6f3c0SFlorian Wechsung   }
20344d04e9f1SPatrick Farrell   if (isNonlinear) {
20350904074fSPatrick Farrell     ierr = ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
20364d04e9f1SPatrick Farrell   }
20374bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
20384bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
20394bbf5ea8SMatthew G. Knepley 
20404bbf5ea8SMatthew G. Knepley   point += pStart;
20414bbf5ea8SMatthew 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);
20424bbf5ea8SMatthew G. Knepley 
20434bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
20444bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
20454bbf5ea8SMatthew G. Knepley   if (ncell <= 0) {
20464bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
20474bbf5ea8SMatthew G. Knepley     PetscFunctionReturn(0);
20484bbf5ea8SMatthew G. Knepley   }
2049fa84ea4cSLawrence Mitchell   if (patch->precomputeElementTensors) {
2050fa84ea4cSLawrence Mitchell     PetscInt           i;
2051fa84ea4cSLawrence Mitchell     PetscInt           ndof = patch->totalDofsPerCell;
2052fa84ea4cSLawrence Mitchell     const PetscScalar *elementTensors;
2053fa84ea4cSLawrence Mitchell 
2054fa84ea4cSLawrence Mitchell     ierr = VecGetArrayRead(patch->cellMats, &elementTensors);CHKERRQ(ierr);
2055fa84ea4cSLawrence Mitchell     for (i = 0; i < ncell; i++) {
2056fa84ea4cSLawrence Mitchell       const PetscInt     cell = cellsArray[i + offset];
2057fa84ea4cSLawrence Mitchell       const PetscInt    *idx  = dofsArray + (offset + i)*ndof;
2058fe988be2SFlorian Wechsung       const PetscScalar *v    = elementTensors + patch->precomputedTensorLocations[cell]*ndof*ndof;
2059fa84ea4cSLawrence Mitchell       ierr = MatSetValues(mat, ndof, idx, ndof, idx, v, ADD_VALUES);CHKERRQ(ierr);
2060fa84ea4cSLawrence Mitchell     }
2061fa84ea4cSLawrence Mitchell     ierr = VecRestoreArrayRead(patch->cellMats, &elementTensors);CHKERRQ(ierr);
2062fa84ea4cSLawrence Mitchell     ierr = MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2063fa84ea4cSLawrence Mitchell     ierr = MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2064fa84ea4cSLawrence Mitchell   } else {
20654bbf5ea8SMatthew G. Knepley     PetscStackPush("PCPatch user callback");
20662aa6f319SMatthew G. Knepley     /* Cannot reuse the same IS because the geometry info is being cached in it */
20672aa6f319SMatthew G. Knepley     ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS);CHKERRQ(ierr);
20680904074fSPatrick 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);
2069fa84ea4cSLawrence Mitchell   }
207059109abcSLawrence Mitchell   if (patch->usercomputeopintfacet) {
2071eb62eeaaSLawrence Mitchell     ierr = PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets);CHKERRQ(ierr);
2072eb62eeaaSLawrence Mitchell     ierr = PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset);CHKERRQ(ierr);
2073eb62eeaaSLawrence Mitchell     if (numIntFacets > 0) {
2074eb62eeaaSLawrence Mitchell       /* For each interior facet, grab the two cells (in local numbering, and concatenate dof numberings for those cells) */
2075eb62eeaaSLawrence Mitchell       PetscInt *facetDofs = NULL, *facetDofsWithAll = NULL;
2076eb62eeaaSLawrence Mitchell       const PetscInt *intFacetsArray = NULL;
2077eb62eeaaSLawrence Mitchell       PetscInt idx = 0;
2078eb62eeaaSLawrence Mitchell       PetscInt i, c, d;
2079*de2d1767SPatrick Farrell       PetscInt fStart;
2080*de2d1767SPatrick Farrell       DM       dm;
2081eb62eeaaSLawrence Mitchell       IS       facetIS = NULL;
2082eb62eeaaSLawrence Mitchell       const PetscInt *facetCells = NULL;
2083eb62eeaaSLawrence Mitchell       ierr = ISGetIndices(patch->intFacetsToPatchCell, &facetCells);CHKERRQ(ierr);
2084eb62eeaaSLawrence Mitchell       ierr = ISGetIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
2085*de2d1767SPatrick Farrell       ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
2086*de2d1767SPatrick Farrell       ierr = DMPlexGetHeightStratum(dm, 1, &fStart, NULL);CHKERRQ(ierr);
2087eb62eeaaSLawrence Mitchell       /* FIXME: Pull this malloc out. */
2088eb62eeaaSLawrence Mitchell       ierr = PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofs);CHKERRQ(ierr);
2089eb62eeaaSLawrence Mitchell       if (dofsArrayWithAll) {
2090eb62eeaaSLawrence Mitchell         ierr = PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofsWithAll);CHKERRQ(ierr);
2091eb62eeaaSLawrence Mitchell       }
2092f98464cbSLawrence Mitchell       if (patch->precomputeElementTensors) {
2093f98464cbSLawrence Mitchell         PetscInt           nFacetDof = 2*patch->totalDofsPerCell;
2094f98464cbSLawrence Mitchell         const PetscScalar *elementTensors;
2095f98464cbSLawrence Mitchell 
2096f98464cbSLawrence Mitchell         ierr = VecGetArrayRead(patch->intFacetMats, &elementTensors);CHKERRQ(ierr);
2097f98464cbSLawrence Mitchell 
2098f98464cbSLawrence Mitchell         for (i = 0; i < numIntFacets; i++) {
2099f98464cbSLawrence Mitchell           const PetscInt     facet = intFacetsArray[i + intFacetOffset];
2100*de2d1767SPatrick Farrell           const PetscScalar *v     = elementTensors + patch->precomputedIntFacetTensorLocations[facet - fStart]*nFacetDof*nFacetDof;
2101f98464cbSLawrence Mitchell           idx = 0;
2102f98464cbSLawrence Mitchell           /*
2103f98464cbSLawrence Mitchell            * 0--1
2104f98464cbSLawrence Mitchell            * |\-|
2105f98464cbSLawrence Mitchell            * |+\|
2106f98464cbSLawrence Mitchell            * 2--3
2107f98464cbSLawrence Mitchell            * [0, 2, 3, 0, 1, 3]
2108f98464cbSLawrence Mitchell            */
2109f98464cbSLawrence Mitchell           for (c = 0; c < 2; c++) {
2110f98464cbSLawrence Mitchell             const PetscInt cell = facetCells[2*(intFacetOffset + i) + c];
2111f98464cbSLawrence Mitchell             for (d = 0; d < patch->totalDofsPerCell; d++) {
2112f98464cbSLawrence Mitchell               facetDofs[idx] = dofsArray[(offset + cell)*patch->totalDofsPerCell + d];
2113f98464cbSLawrence Mitchell               idx++;
2114f98464cbSLawrence Mitchell             }
2115f98464cbSLawrence Mitchell           }
2116f98464cbSLawrence Mitchell           ierr = MatSetValues(mat, nFacetDof, facetDofs, nFacetDof, facetDofs, v, ADD_VALUES);CHKERRQ(ierr);
2117f98464cbSLawrence Mitchell         }
2118f98464cbSLawrence Mitchell         ierr = VecRestoreArrayRead(patch->intFacetMats, &elementTensors);CHKERRQ(ierr);
2119f98464cbSLawrence Mitchell       } else {
2120eb62eeaaSLawrence Mitchell         /*
2121eb62eeaaSLawrence Mitchell          * 0--1
2122eb62eeaaSLawrence Mitchell          * |\-|
2123eb62eeaaSLawrence Mitchell          * |+\|
2124eb62eeaaSLawrence Mitchell          * 2--3
2125eb62eeaaSLawrence Mitchell          * [0, 2, 3, 0, 1, 3]
2126eb62eeaaSLawrence Mitchell          */
2127eb62eeaaSLawrence Mitchell         for (i = 0; i < numIntFacets; i++) {
2128eb62eeaaSLawrence Mitchell           for (c = 0; c < 2; c++) {
2129eb62eeaaSLawrence Mitchell             const PetscInt cell = facetCells[2*(intFacetOffset + i) + c];
2130eb62eeaaSLawrence Mitchell             for (d = 0; d < patch->totalDofsPerCell; d++) {
2131eb62eeaaSLawrence Mitchell               facetDofs[idx] = dofsArray[(offset + cell)*patch->totalDofsPerCell + d];
2132eb62eeaaSLawrence Mitchell               if (dofsArrayWithAll) {
2133eb62eeaaSLawrence Mitchell                 facetDofsWithAll[idx] = dofsArrayWithAll[(offset + cell)*patch->totalDofsPerCell + d];
2134eb62eeaaSLawrence Mitchell               }
2135eb62eeaaSLawrence Mitchell               idx++;
2136eb62eeaaSLawrence Mitchell             }
2137eb62eeaaSLawrence Mitchell           }
2138eb62eeaaSLawrence Mitchell         }
2139eb62eeaaSLawrence Mitchell         ierr = ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray + intFacetOffset, PETSC_USE_POINTER, &facetIS);CHKERRQ(ierr);
2140ec0d443bSLawrence Mitchell         ierr = patch->usercomputeopintfacet(pc, point, x, mat, facetIS, 2*numIntFacets*patch->totalDofsPerCell, facetDofs, facetDofsWithAll, patch->usercomputeopintfacetctx);CHKERRQ(ierr);
2141eb62eeaaSLawrence Mitchell         ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
2142f98464cbSLawrence Mitchell       }
2143eb62eeaaSLawrence Mitchell       ierr = ISRestoreIndices(patch->intFacetsToPatchCell, &facetCells);CHKERRQ(ierr);
2144f98464cbSLawrence Mitchell       ierr = ISRestoreIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
2145eb62eeaaSLawrence Mitchell       ierr = PetscFree(facetDofs);CHKERRQ(ierr);
2146eb62eeaaSLawrence Mitchell       ierr = PetscFree(facetDofsWithAll);CHKERRQ(ierr);
2147eb62eeaaSLawrence Mitchell     }
214859109abcSLawrence Mitchell   }
21496710cc29SPatrick Farrell 
21506710cc29SPatrick Farrell   ierr = MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
21516710cc29SPatrick Farrell   ierr = MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
21526710cc29SPatrick Farrell 
21534bbf5ea8SMatthew G. Knepley   PetscStackPop;
21542aa6f319SMatthew G. Knepley   ierr = ISDestroy(&patch->cellIS);CHKERRQ(ierr);
21554d04e9f1SPatrick Farrell   if(withArtificial) {
2156c2e6f3c0SFlorian Wechsung     ierr = ISRestoreIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
2157c2e6f3c0SFlorian Wechsung   } else {
21584bbf5ea8SMatthew G. Knepley     ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
2159c2e6f3c0SFlorian Wechsung   }
21604d04e9f1SPatrick Farrell   if (isNonlinear) {
21610904074fSPatrick Farrell     ierr = ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
21624d04e9f1SPatrick Farrell   }
21634bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
21642aa6f319SMatthew G. Knepley   if (patch->viewMatrix) {
21652aa6f319SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
21662aa6f319SMatthew G. Knepley 
21672aa6f319SMatthew G. Knepley     ierr = PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch matrix for Point %D", point);CHKERRQ(ierr);
21682aa6f319SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) mat, name);CHKERRQ(ierr);
21692aa6f319SMatthew G. Knepley     ierr = ObjectView((PetscObject) mat, patch->viewerMatrix, patch->formatMatrix);CHKERRQ(ierr);
21702aa6f319SMatthew G. Knepley   }
21714bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
21724bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
21734bbf5ea8SMatthew G. Knepley }
21744bbf5ea8SMatthew G. Knepley 
2175fa84ea4cSLawrence Mitchell static PetscErrorCode MatSetValues_PCPatch_Private(Mat mat, PetscInt m, const PetscInt idxm[],
2176fa84ea4cSLawrence Mitchell                                                    PetscInt n, const PetscInt idxn[], const PetscScalar *v, InsertMode addv)
2177fa84ea4cSLawrence Mitchell {
2178fa84ea4cSLawrence Mitchell   Vec data;
2179fa84ea4cSLawrence Mitchell   PetscScalar *array;
2180fe988be2SFlorian Wechsung   PetscInt bs, nz, i, j, cell;
2181fa84ea4cSLawrence Mitchell   PetscErrorCode ierr;
2182fa84ea4cSLawrence Mitchell 
2183fa84ea4cSLawrence Mitchell   ierr = MatShellGetContext(mat, &data);CHKERRQ(ierr);
2184fa84ea4cSLawrence Mitchell   ierr = VecGetBlockSize(data, &bs);CHKERRQ(ierr);
2185fa84ea4cSLawrence Mitchell   ierr = VecGetSize(data, &nz);CHKERRQ(ierr);
2186fa84ea4cSLawrence Mitchell   ierr = VecGetArray(data, &array);CHKERRQ(ierr);
2187fa84ea4cSLawrence Mitchell   if (m != n) SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Only for square insertion");
2188fe988be2SFlorian Wechsung   cell = (PetscInt)(idxm[0]/bs); // use the fact that this is called once per cell
2189fa84ea4cSLawrence Mitchell   for (i = 0; i < m; i++) {
2190fe988be2SFlorian Wechsung     //const PetscInt row = idxm[i];
2191fa84ea4cSLawrence Mitchell     if (idxm[i] != idxn[i]) SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Row and column indices must match!");
2192fa84ea4cSLawrence Mitchell     for (j = 0; j < n; j++) {
2193fa84ea4cSLawrence Mitchell       const PetscScalar v_ = v[i*bs + j];
2194fa84ea4cSLawrence Mitchell       /* Indexing is special to the data structure we have! */
2195fa84ea4cSLawrence Mitchell       if (addv == INSERT_VALUES) {
2196fe988be2SFlorian Wechsung         array[cell*bs*bs + i*bs + j] = v_;
2197fa84ea4cSLawrence Mitchell       } else {
2198fe988be2SFlorian Wechsung         array[cell*bs*bs + i*bs + j] += v_;
2199fa84ea4cSLawrence Mitchell       }
2200fa84ea4cSLawrence Mitchell     }
2201fa84ea4cSLawrence Mitchell   }
2202fa84ea4cSLawrence Mitchell   ierr = VecRestoreArray(data, &array);CHKERRQ(ierr);
2203fa84ea4cSLawrence Mitchell   PetscFunctionReturn(0);
2204fa84ea4cSLawrence Mitchell }
2205fa84ea4cSLawrence Mitchell 
2206fa84ea4cSLawrence Mitchell static PetscErrorCode PCPatchPrecomputePatchTensors_Private(PC pc)
2207fa84ea4cSLawrence Mitchell {
2208fa84ea4cSLawrence Mitchell   PC_PATCH       *patch = (PC_PATCH *)pc->data;
2209fa84ea4cSLawrence Mitchell   const PetscInt *cellsArray;
2210fa84ea4cSLawrence Mitchell   PetscInt        ncell, offset;
2211fa84ea4cSLawrence Mitchell   const PetscInt *dofMapArray;
2212fa84ea4cSLawrence Mitchell   PetscInt        i, j;
2213fa84ea4cSLawrence Mitchell   IS              dofMap;
2214fa84ea4cSLawrence Mitchell   IS              cellIS;
2215fa84ea4cSLawrence Mitchell   const PetscInt  ndof  = patch->totalDofsPerCell;
2216fa84ea4cSLawrence Mitchell   PetscErrorCode  ierr;
2217fa84ea4cSLawrence Mitchell   Mat             vecMat;
2218fe988be2SFlorian Wechsung   PetscInt        cStart, cEnd;
2219fe988be2SFlorian Wechsung   DM              dm, plex;
2220fe988be2SFlorian Wechsung 
2221fa84ea4cSLawrence Mitchell 
2222fa84ea4cSLawrence Mitchell   ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
2223fa84ea4cSLawrence Mitchell 
2224fa84ea4cSLawrence Mitchell   if (!patch->allCells) {
2225fa84ea4cSLawrence Mitchell     PetscHSetI      cells;
2226fa84ea4cSLawrence Mitchell     PetscHashIter   hi;
2227fa84ea4cSLawrence Mitchell     PetscInt pStart, pEnd;
2228fa84ea4cSLawrence Mitchell     PetscInt *allCells = NULL;
2229fa84ea4cSLawrence Mitchell     ierr = PetscHSetICreate(&cells);CHKERRQ(ierr);
2230fa84ea4cSLawrence Mitchell     ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
2231fa84ea4cSLawrence Mitchell     ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
2232fa84ea4cSLawrence Mitchell     for (i = pStart; i < pEnd; i++) {
2233fa84ea4cSLawrence Mitchell       ierr = PetscSectionGetDof(patch->cellCounts, i, &ncell);CHKERRQ(ierr);
2234fa84ea4cSLawrence Mitchell       ierr = PetscSectionGetOffset(patch->cellCounts, i, &offset);CHKERRQ(ierr);
2235fa84ea4cSLawrence Mitchell       if (ncell <= 0) continue;
2236fa84ea4cSLawrence Mitchell       for (j = 0; j < ncell; j++) {
2237fa84ea4cSLawrence Mitchell         PetscHSetIAdd(cells, cellsArray[offset + j]);CHKERRQ(ierr);
2238fa84ea4cSLawrence Mitchell       }
2239fa84ea4cSLawrence Mitchell     }
2240fa84ea4cSLawrence Mitchell     ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
2241fa84ea4cSLawrence Mitchell     ierr = PetscHSetIGetSize(cells, &ncell);CHKERRQ(ierr);
2242fa84ea4cSLawrence Mitchell     ierr = PetscMalloc1(ncell, &allCells);CHKERRQ(ierr);
2243fe988be2SFlorian Wechsung     ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
2244fe988be2SFlorian Wechsung     ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
2245fe988be2SFlorian Wechsung     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2246fe988be2SFlorian Wechsung     ierr = PetscMalloc1(cEnd-cStart, &patch->precomputedTensorLocations);CHKERRQ(ierr);
2247fa84ea4cSLawrence Mitchell     i = 0;
2248fa84ea4cSLawrence Mitchell     PetscHashIterBegin(cells, hi);
2249fa84ea4cSLawrence Mitchell     while (!PetscHashIterAtEnd(cells, hi)) {
2250fe988be2SFlorian Wechsung       PetscHashIterGetKey(cells, hi, allCells[i]);
2251fe988be2SFlorian Wechsung       patch->precomputedTensorLocations[allCells[i]] = i;
2252fa84ea4cSLawrence Mitchell       PetscHashIterNext(cells, hi);
2253fe988be2SFlorian Wechsung       i++;
2254fa84ea4cSLawrence Mitchell     }
2255fa84ea4cSLawrence Mitchell     ierr = PetscHSetIDestroy(&cells);CHKERRQ(ierr);
2256fa84ea4cSLawrence Mitchell     ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, allCells, PETSC_OWN_POINTER, &patch->allCells);CHKERRQ(ierr);
2257fa84ea4cSLawrence Mitchell   }
2258f98464cbSLawrence Mitchell   ierr = ISGetSize(patch->allCells, &ncell);CHKERRQ(ierr);
2259fa84ea4cSLawrence Mitchell   if (!patch->cellMats) {
2260fa84ea4cSLawrence Mitchell     ierr = VecCreateSeq(PETSC_COMM_SELF, ncell*ndof*ndof, &patch->cellMats);CHKERRQ(ierr);
2261fa84ea4cSLawrence Mitchell     ierr = VecSetBlockSize(patch->cellMats, ndof);CHKERRQ(ierr);
2262fa84ea4cSLawrence Mitchell   }
2263fa84ea4cSLawrence Mitchell   ierr = VecSet(patch->cellMats, 0);CHKERRQ(ierr);
2264fa84ea4cSLawrence Mitchell 
2265fa84ea4cSLawrence Mitchell   ierr = MatCreateShell(PETSC_COMM_SELF, ncell*ndof, ncell*ndof, ncell*ndof, ncell*ndof,
2266fa84ea4cSLawrence Mitchell                         (void*)patch->cellMats, &vecMat);
2267fa84ea4cSLawrence Mitchell   ierr = MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void(*)(void))&MatSetValues_PCPatch_Private);CHKERRQ(ierr);
2268fa84ea4cSLawrence Mitchell   ierr = ISGetSize(patch->allCells, &ncell);CHKERRQ(ierr);
2269fa84ea4cSLawrence Mitchell   ierr = ISCreateStride(PETSC_COMM_SELF, ndof*ncell, 0, 1, &dofMap);CHKERRQ(ierr);
2270fa84ea4cSLawrence Mitchell   ierr = ISGetIndices(dofMap, &dofMapArray);CHKERRQ(ierr);
2271fa84ea4cSLawrence Mitchell   ierr = ISGetIndices(patch->allCells, &cellsArray);CHKERRQ(ierr);
2272fa84ea4cSLawrence Mitchell   ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray, PETSC_USE_POINTER, &cellIS);CHKERRQ(ierr);
2273fa84ea4cSLawrence Mitchell   PetscStackPush("PCPatch user callback");
2274fa84ea4cSLawrence Mitchell   /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */
2275fa84ea4cSLawrence Mitchell   ierr = patch->usercomputeop(pc, -1, NULL, vecMat, cellIS, ndof*ncell, dofMapArray, NULL, patch->usercomputeopctx);CHKERRQ(ierr);
2276f98464cbSLawrence Mitchell   PetscStackPop;
2277fa84ea4cSLawrence Mitchell   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
2278fa84ea4cSLawrence Mitchell   ierr = MatDestroy(&vecMat);CHKERRQ(ierr);
2279fa84ea4cSLawrence Mitchell   ierr = ISRestoreIndices(patch->allCells, &cellsArray);CHKERRQ(ierr);
2280fa84ea4cSLawrence Mitchell   ierr = ISRestoreIndices(dofMap, &dofMapArray);CHKERRQ(ierr);
2281fa84ea4cSLawrence Mitchell   ierr = ISDestroy(&dofMap);CHKERRQ(ierr);
2282f98464cbSLawrence Mitchell 
2283f98464cbSLawrence Mitchell   if (patch->usercomputeopintfacet) {
2284f98464cbSLawrence Mitchell     PetscInt nIntFacets;
2285f98464cbSLawrence Mitchell     IS       intFacetsIS;
2286f98464cbSLawrence Mitchell     const PetscInt *intFacetsArray = NULL;
2287f98464cbSLawrence Mitchell     if (!patch->allIntFacets) {
2288f98464cbSLawrence Mitchell       PetscHSetI      facets;
2289f98464cbSLawrence Mitchell       PetscHashIter   hi;
2290f98464cbSLawrence Mitchell       PetscInt pStart, pEnd, fStart, fEnd;
2291f98464cbSLawrence Mitchell       PetscInt *allIntFacets = NULL;
2292f98464cbSLawrence Mitchell       ierr = PetscHSetICreate(&facets);CHKERRQ(ierr);
2293f98464cbSLawrence Mitchell       ierr = ISGetIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
2294f98464cbSLawrence Mitchell       ierr = PetscSectionGetChart(patch->intFacetCounts, &pStart, &pEnd);CHKERRQ(ierr);
2295*de2d1767SPatrick Farrell       ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
2296f98464cbSLawrence Mitchell       for (i = pStart; i < pEnd; i++) {
2297f98464cbSLawrence Mitchell         ierr = PetscSectionGetDof(patch->intFacetCounts, i, &nIntFacets);CHKERRQ(ierr);
2298f98464cbSLawrence Mitchell         ierr = PetscSectionGetOffset(patch->intFacetCounts, i, &offset);CHKERRQ(ierr);
2299f98464cbSLawrence Mitchell         if (nIntFacets <= 0) continue;
2300f98464cbSLawrence Mitchell         for (j = 0; j < nIntFacets; j++) {
2301f98464cbSLawrence Mitchell           PetscHSetIAdd(facets, intFacetsArray[offset + j]);CHKERRQ(ierr);
2302f98464cbSLawrence Mitchell         }
2303f98464cbSLawrence Mitchell       }
2304f98464cbSLawrence Mitchell       ierr = ISRestoreIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
2305f98464cbSLawrence Mitchell       ierr = PetscHSetIGetSize(facets, &nIntFacets);CHKERRQ(ierr);
2306f98464cbSLawrence Mitchell       ierr = PetscMalloc1(nIntFacets, &allIntFacets);CHKERRQ(ierr);
2307f98464cbSLawrence Mitchell       ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
2308f98464cbSLawrence Mitchell       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
2309f98464cbSLawrence Mitchell       ierr = PetscMalloc1(fEnd-fStart, &patch->precomputedIntFacetTensorLocations);CHKERRQ(ierr);
2310f98464cbSLawrence Mitchell       i = 0;
2311f98464cbSLawrence Mitchell       PetscHashIterBegin(facets, hi);
2312f98464cbSLawrence Mitchell       while (!PetscHashIterAtEnd(facets, hi)) {
2313f98464cbSLawrence Mitchell         PetscHashIterGetKey(facets, hi, allIntFacets[i]);
2314*de2d1767SPatrick Farrell         patch->precomputedIntFacetTensorLocations[allIntFacets[i] - fStart] = i;
2315f98464cbSLawrence Mitchell         PetscHashIterNext(facets, hi);
2316f98464cbSLawrence Mitchell         i++;
2317f98464cbSLawrence Mitchell       }
2318f98464cbSLawrence Mitchell       ierr = PetscHSetIDestroy(&facets);CHKERRQ(ierr);
2319f98464cbSLawrence Mitchell       ierr = ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, allIntFacets, PETSC_OWN_POINTER, &patch->allIntFacets);CHKERRQ(ierr);
2320f98464cbSLawrence Mitchell     }
2321f98464cbSLawrence Mitchell     ierr = ISGetSize(patch->allIntFacets, &nIntFacets);CHKERRQ(ierr);
2322f98464cbSLawrence Mitchell     if (!patch->intFacetMats) {
2323f98464cbSLawrence Mitchell       ierr = VecCreateSeq(PETSC_COMM_SELF, nIntFacets*ndof*ndof*4, &patch->intFacetMats);CHKERRQ(ierr);
2324f98464cbSLawrence Mitchell       ierr = VecSetBlockSize(patch->intFacetMats, ndof*2);CHKERRQ(ierr);
2325f98464cbSLawrence Mitchell     }
2326f98464cbSLawrence Mitchell     ierr = VecSet(patch->intFacetMats, 0);CHKERRQ(ierr);
2327f98464cbSLawrence Mitchell 
2328f98464cbSLawrence Mitchell     ierr = MatCreateShell(PETSC_COMM_SELF, nIntFacets*ndof*2, nIntFacets*ndof*2, nIntFacets*ndof*2, nIntFacets*ndof*2,
2329f98464cbSLawrence Mitchell                           (void*)patch->intFacetMats, &vecMat);
2330f98464cbSLawrence Mitchell     ierr = MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void(*)(void))&MatSetValues_PCPatch_Private);CHKERRQ(ierr);
2331f98464cbSLawrence Mitchell     ierr = ISCreateStride(PETSC_COMM_SELF, 2*ndof*nIntFacets, 0, 1, &dofMap);CHKERRQ(ierr);
2332f98464cbSLawrence Mitchell     ierr = ISGetIndices(dofMap, &dofMapArray);CHKERRQ(ierr);
2333f98464cbSLawrence Mitchell     ierr = ISGetIndices(patch->allIntFacets, &intFacetsArray);CHKERRQ(ierr);
2334f98464cbSLawrence Mitchell     ierr = ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, intFacetsArray, PETSC_USE_POINTER, &intFacetsIS);CHKERRQ(ierr);
2335f98464cbSLawrence Mitchell     PetscStackPush("PCPatch user callback (interior facets)");
2336f98464cbSLawrence Mitchell     /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */
2337f98464cbSLawrence Mitchell     ierr = patch->usercomputeopintfacet(pc, -1, NULL, vecMat, intFacetsIS, 2*ndof*nIntFacets, dofMapArray, NULL, patch->usercomputeopintfacetctx);CHKERRQ(ierr);
2338f98464cbSLawrence Mitchell     PetscStackPop;
2339f98464cbSLawrence Mitchell     ierr = ISDestroy(&intFacetsIS);CHKERRQ(ierr);
2340f98464cbSLawrence Mitchell     ierr = MatDestroy(&vecMat);CHKERRQ(ierr);
2341f98464cbSLawrence Mitchell     ierr = ISRestoreIndices(patch->allIntFacets, &intFacetsArray);CHKERRQ(ierr);
2342f98464cbSLawrence Mitchell     ierr = ISRestoreIndices(dofMap, &dofMapArray);CHKERRQ(ierr);
2343f98464cbSLawrence Mitchell     ierr = ISDestroy(&dofMap);CHKERRQ(ierr);
2344f98464cbSLawrence Mitchell   }
2345fa84ea4cSLawrence Mitchell   ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
2346fa84ea4cSLawrence Mitchell 
2347fa84ea4cSLawrence Mitchell   PetscFunctionReturn(0);
2348fa84ea4cSLawrence Mitchell }
2349fa84ea4cSLawrence Mitchell 
23500904074fSPatrick Farrell PetscErrorCode PCPatch_ScatterLocal_Private(PC pc, PetscInt p, Vec x, Vec y, InsertMode mode, ScatterMode scat, PatchScatterType scattertype)
23514bbf5ea8SMatthew G. Knepley {
23524bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch     = (PC_PATCH *) pc->data;
23534bbf5ea8SMatthew G. Knepley   const PetscScalar *xArray    = NULL;
23544bbf5ea8SMatthew G. Knepley   PetscScalar       *yArray    = NULL;
23554bbf5ea8SMatthew G. Knepley   const PetscInt    *gtolArray = NULL;
23564bbf5ea8SMatthew G. Knepley   PetscInt           dof, offset, lidx;
23574bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
23584bbf5ea8SMatthew G. Knepley 
23594bbf5ea8SMatthew G. Knepley   PetscFunctionBeginHot;
23604bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Scatter, pc, 0, 0, 0);CHKERRQ(ierr);
23614bbf5ea8SMatthew G. Knepley   ierr = VecGetArrayRead(x, &xArray);CHKERRQ(ierr);
23624bbf5ea8SMatthew G. Knepley   ierr = VecGetArray(y, &yArray);CHKERRQ(ierr);
23630904074fSPatrick Farrell   if (scattertype == SCATTER_WITHARTIFICIAL) {
2364c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof);CHKERRQ(ierr);
2365c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offset);CHKERRQ(ierr);
2366c2e6f3c0SFlorian Wechsung     ierr = ISGetIndices(patch->gtolWithArtificial, &gtolArray);CHKERRQ(ierr);
23670904074fSPatrick Farrell   } else if (scattertype == SCATTER_WITHALL) {
23680904074fSPatrick Farrell     ierr = PetscSectionGetDof(patch->gtolCountsWithAll, p, &dof);CHKERRQ(ierr);
23690904074fSPatrick Farrell     ierr = PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offset);CHKERRQ(ierr);
23700904074fSPatrick Farrell     ierr = ISGetIndices(patch->gtolWithAll, &gtolArray);CHKERRQ(ierr);
2371c2e6f3c0SFlorian Wechsung   } else {
23724bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr);
23734bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
23744bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
2375c2e6f3c0SFlorian Wechsung   }
23764bbf5ea8SMatthew 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");
23774bbf5ea8SMatthew 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");
23784bbf5ea8SMatthew G. Knepley   for (lidx = 0; lidx < dof; ++lidx) {
23794bbf5ea8SMatthew G. Knepley     const PetscInt gidx = gtolArray[offset+lidx];
23804bbf5ea8SMatthew G. Knepley 
23814bbf5ea8SMatthew G. Knepley     if (mode == INSERT_VALUES) yArray[lidx]  = xArray[gidx]; /* Forward */
23824bbf5ea8SMatthew G. Knepley     else                       yArray[gidx] += xArray[lidx]; /* Reverse */
23834bbf5ea8SMatthew G. Knepley   }
23840904074fSPatrick Farrell   if (scattertype == SCATTER_WITHARTIFICIAL) {
2385c2e6f3c0SFlorian Wechsung     ierr = ISRestoreIndices(patch->gtolWithArtificial, &gtolArray);CHKERRQ(ierr);
23860904074fSPatrick Farrell   } else if (scattertype == SCATTER_WITHALL) {
23870904074fSPatrick Farrell     ierr = ISRestoreIndices(patch->gtolWithAll, &gtolArray);CHKERRQ(ierr);
2388c2e6f3c0SFlorian Wechsung   } else {
23894bbf5ea8SMatthew G. Knepley     ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
2390c2e6f3c0SFlorian Wechsung   }
23914bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArrayRead(x, &xArray);CHKERRQ(ierr);
23924bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArray(y, &yArray);CHKERRQ(ierr);
23934bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Scatter, pc, 0, 0, 0);CHKERRQ(ierr);
23944bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
23954bbf5ea8SMatthew G. Knepley }
23964bbf5ea8SMatthew G. Knepley 
2397dadc69c5SMatthew G. Knepley static PetscErrorCode PCSetUp_PATCH_Linear(PC pc)
2398dadc69c5SMatthew G. Knepley {
2399dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2400dadc69c5SMatthew G. Knepley   const char    *prefix;
2401dadc69c5SMatthew G. Knepley   PetscInt       i;
2402dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2403dadc69c5SMatthew G. Knepley 
2404dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2405dadc69c5SMatthew G. Knepley   if (!pc->setupcalled) {
2406dadc69c5SMatthew G. Knepley     ierr = PetscMalloc1(patch->npatch, &patch->solver);CHKERRQ(ierr);
2407dadc69c5SMatthew G. Knepley     ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr);
2408dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {
2409dadc69c5SMatthew G. Knepley       KSP ksp;
2410dadc69c5SMatthew G. Knepley       PC  subpc;
2411dadc69c5SMatthew G. Knepley 
2412dadc69c5SMatthew G. Knepley       ierr = KSPCreate(PETSC_COMM_SELF, &ksp);CHKERRQ(ierr);
2413ddad275aSPatrick Farrell       ierr = KSPSetErrorIfNotConverged(ksp, pc->erroriffailure);CHKERRQ(ierr);
2414dadc69c5SMatthew G. Knepley       ierr = KSPSetOptionsPrefix(ksp, prefix);CHKERRQ(ierr);
2415dadc69c5SMatthew G. Knepley       ierr = KSPAppendOptionsPrefix(ksp, "sub_");CHKERRQ(ierr);
2416dadc69c5SMatthew G. Knepley       ierr = PetscObjectIncrementTabLevel((PetscObject) ksp, (PetscObject) pc, 1);CHKERRQ(ierr);
2417dadc69c5SMatthew G. Knepley       ierr = KSPGetPC(ksp, &subpc);CHKERRQ(ierr);
2418dadc69c5SMatthew G. Knepley       ierr = PetscObjectIncrementTabLevel((PetscObject) subpc, (PetscObject) pc, 1);CHKERRQ(ierr);
2419dadc69c5SMatthew G. Knepley       ierr = PetscLogObjectParent((PetscObject) pc, (PetscObject) ksp);CHKERRQ(ierr);
2420dadc69c5SMatthew G. Knepley       patch->solver[i] = (PetscObject) ksp;
2421dadc69c5SMatthew G. Knepley     }
2422dadc69c5SMatthew G. Knepley   }
2423dadc69c5SMatthew G. Knepley   if (patch->save_operators) {
2424fe988be2SFlorian Wechsung     if (patch->precomputeElementTensors) {
2425fe988be2SFlorian Wechsung       ierr = PCPatchPrecomputePatchTensors_Private(pc);CHKERRQ(ierr);
2426fe988be2SFlorian Wechsung     }
2427dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {
2428dadc69c5SMatthew G. Knepley       ierr = MatZeroEntries(patch->mat[i]);CHKERRQ(ierr);
242934d8b122SPatrick Farrell       ierr = PCPatchComputeOperator_Internal(pc, NULL, patch->mat[i], i, PETSC_FALSE);CHKERRQ(ierr);
2430dadc69c5SMatthew G. Knepley       ierr = KSPSetOperators((KSP) patch->solver[i], patch->mat[i], patch->mat[i]);CHKERRQ(ierr);
2431dadc69c5SMatthew G. Knepley     }
2432dadc69c5SMatthew G. Knepley   }
243334d8b122SPatrick Farrell   if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
243434d8b122SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {
24351202d238SPatrick Farrell       /* Instead of padding patch->patchUpdate with zeros to get */
24361202d238SPatrick Farrell       /* patch->patchUpdateWithArtificial and then multiplying with the matrix, */
243734d8b122SPatrick Farrell       /* just get rid of the columns that correspond to the dofs with */
243834d8b122SPatrick Farrell       /* artificial bcs. That's of course fairly inefficient, hopefully we */
243934d8b122SPatrick Farrell       /* can just assemble the rectangular matrix in the first place. */
244034d8b122SPatrick Farrell       Mat matSquare;
244134d8b122SPatrick Farrell       IS rowis;
244234d8b122SPatrick Farrell       PetscInt dof;
244334d8b122SPatrick Farrell 
244434d8b122SPatrick Farrell       ierr = MatGetSize(patch->mat[i], &dof, NULL);CHKERRQ(ierr);
244534d8b122SPatrick Farrell       if (dof == 0) {
244634d8b122SPatrick Farrell         patch->matWithArtificial[i] = NULL;
244734d8b122SPatrick Farrell         continue;
244834d8b122SPatrick Farrell       }
244934d8b122SPatrick Farrell 
245034d8b122SPatrick Farrell       ierr = PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE);CHKERRQ(ierr);
245134d8b122SPatrick Farrell       ierr = MatZeroEntries(matSquare);CHKERRQ(ierr);
245234d8b122SPatrick Farrell       ierr = PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE);CHKERRQ(ierr);
245334d8b122SPatrick Farrell 
245434d8b122SPatrick Farrell       ierr = MatGetSize(matSquare, &dof, NULL);CHKERRQ(ierr);
245534d8b122SPatrick Farrell       ierr = ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis); CHKERRQ(ierr);
245634d8b122SPatrick Farrell       if(pc->setupcalled) {
245734d8b122SPatrick Farrell         ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_REUSE_MATRIX, &patch->matWithArtificial[i]); CHKERRQ(ierr);
245834d8b122SPatrick Farrell       } else {
245934d8b122SPatrick Farrell         ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &patch->matWithArtificial[i]); CHKERRQ(ierr);
246034d8b122SPatrick Farrell       }
246134d8b122SPatrick Farrell       ierr = ISDestroy(&rowis); CHKERRQ(ierr);
246234d8b122SPatrick Farrell       ierr = MatDestroy(&matSquare);CHKERRQ(ierr);
246334d8b122SPatrick Farrell     }
246434d8b122SPatrick Farrell   }
2465dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
2466dadc69c5SMatthew G. Knepley }
2467dadc69c5SMatthew G. Knepley 
24684bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUp_PATCH(PC pc)
24694bbf5ea8SMatthew G. Knepley {
24704bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2471557beb66SLawrence Mitchell   PetscInt       i;
247239fd2e8aSPatrick Farrell   PetscBool       isNonlinear;
24734bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
24744bbf5ea8SMatthew G. Knepley 
24754bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
24764bbf5ea8SMatthew G. Knepley   if (!pc->setupcalled) {
24774bbf5ea8SMatthew G. Knepley     PetscInt pStart, pEnd, p;
24784bbf5ea8SMatthew G. Knepley     PetscInt localSize;
24794bbf5ea8SMatthew G. Knepley 
24804bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventBegin(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr);
24814bbf5ea8SMatthew G. Knepley 
2482debbdec3SPatrick Farrell     isNonlinear = patch->isNonlinear;
24835f824522SMatthew G. Knepley     if (!patch->nsubspaces) {
24845f824522SMatthew G. Knepley       DM           dm;
24855f824522SMatthew G. Knepley       PetscSection s;
2486e72c1634SMatthew G. Knepley       PetscInt     cStart, cEnd, c, Nf, f, numGlobalBcs = 0, *globalBcs, *Nb, totNb = 0, **cellDofs;
24875f824522SMatthew G. Knepley 
24885f824522SMatthew G. Knepley       ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
24895f824522SMatthew G. Knepley       if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONG, "Must set DM for PCPATCH or call PCPatchSetDiscretisationInfo()");
24905f824522SMatthew G. Knepley       ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
24915f824522SMatthew G. Knepley       ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
24925f824522SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
24935f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
24945f824522SMatthew G. Knepley         PetscInt cdof;
24955f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
24965f824522SMatthew G. Knepley         numGlobalBcs += cdof;
24975f824522SMatthew G. Knepley       }
24985f824522SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
24995f824522SMatthew G. Knepley       ierr = PetscMalloc3(Nf, &Nb, Nf, &cellDofs, numGlobalBcs, &globalBcs);CHKERRQ(ierr);
25005f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
25015f824522SMatthew G. Knepley         PetscFE        fe;
25025f824522SMatthew G. Knepley         PetscDualSpace sp;
25035f824522SMatthew G. Knepley         PetscInt       cdoff = 0;
25045f824522SMatthew G. Knepley 
250544a7f3ddSMatthew G. Knepley         ierr = DMGetField(dm, f, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
25065f824522SMatthew G. Knepley         /* ierr = PetscFEGetNumComponents(fe, &Nc[f]);CHKERRQ(ierr); */
25075f824522SMatthew G. Knepley         ierr = PetscFEGetDualSpace(fe, &sp);CHKERRQ(ierr);
25085f824522SMatthew G. Knepley         ierr = PetscDualSpaceGetDimension(sp, &Nb[f]);CHKERRQ(ierr);
25095f824522SMatthew G. Knepley         totNb += Nb[f];
25105f824522SMatthew G. Knepley 
25115f824522SMatthew G. Knepley         ierr = PetscMalloc1((cEnd-cStart)*Nb[f], &cellDofs[f]);CHKERRQ(ierr);
25125f824522SMatthew G. Knepley         for (c = cStart; c < cEnd; ++c) {
25135f824522SMatthew G. Knepley           PetscInt *closure = NULL;
25145f824522SMatthew G. Knepley           PetscInt  clSize  = 0, cl;
25155f824522SMatthew G. Knepley 
25165f824522SMatthew G. Knepley           ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
25175f824522SMatthew G. Knepley           for (cl = 0; cl < clSize*2; cl += 2) {
25185f824522SMatthew G. Knepley             const PetscInt p = closure[cl];
25195f824522SMatthew G. Knepley             PetscInt       fdof, d, foff;
25205f824522SMatthew G. Knepley 
25215f824522SMatthew G. Knepley             ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr);
25225f824522SMatthew G. Knepley             ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr);
25235f824522SMatthew G. Knepley             for (d = 0; d < fdof; ++d, ++cdoff) cellDofs[f][cdoff] = foff + d;
25245f824522SMatthew G. Knepley           }
25255f824522SMatthew G. Knepley           ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
25265f824522SMatthew G. Knepley         }
25275f824522SMatthew 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]);
25285f824522SMatthew G. Knepley       }
25295f824522SMatthew G. Knepley       numGlobalBcs = 0;
25305f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
25315f824522SMatthew G. Knepley         const PetscInt *ind;
25325f824522SMatthew G. Knepley         PetscInt        off, cdof, d;
25335f824522SMatthew G. Knepley 
25345f824522SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
25355f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
25365f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &ind);CHKERRQ(ierr);
25375f824522SMatthew G. Knepley         for (d = 0; d < cdof; ++d) globalBcs[numGlobalBcs++] = off + ind[d];
25385f824522SMatthew G. Knepley       }
25395f824522SMatthew G. Knepley 
25405f824522SMatthew G. Knepley       ierr = PCPatchSetDiscretisationInfoCombined(pc, dm, Nb, (const PetscInt **) cellDofs, numGlobalBcs, globalBcs, numGlobalBcs, globalBcs);CHKERRQ(ierr);
25415f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
25425f824522SMatthew G. Knepley         ierr = PetscFree(cellDofs[f]);CHKERRQ(ierr);
25435f824522SMatthew G. Knepley       }
25445f824522SMatthew G. Knepley       ierr = PetscFree3(Nb, cellDofs, globalBcs);CHKERRQ(ierr);
254592d50984SMatthew G. Knepley       ierr = PCPatchSetComputeFunction(pc, PCPatchComputeFunction_DMPlex_Private, NULL);CHKERRQ(ierr);
25465f824522SMatthew G. Knepley       ierr = PCPatchSetComputeOperator(pc, PCPatchComputeOperator_DMPlex_Private, NULL);CHKERRQ(ierr);
25475f824522SMatthew G. Knepley     }
25485f824522SMatthew G. Knepley 
25494bbf5ea8SMatthew G. Knepley     localSize = patch->subspaceOffsets[patch->nsubspaces];
25501202d238SPatrick Farrell     ierr = VecCreateSeq(PETSC_COMM_SELF, localSize, &patch->localRHS);CHKERRQ(ierr);
25511202d238SPatrick Farrell     ierr = VecSetUp(patch->localRHS);CHKERRQ(ierr);
25521202d238SPatrick Farrell     ierr = VecDuplicate(patch->localRHS, &patch->localUpdate);CHKERRQ(ierr);
25534bbf5ea8SMatthew G. Knepley     ierr = PCPatchCreateCellPatches(pc);CHKERRQ(ierr);
25544bbf5ea8SMatthew G. Knepley     ierr = PCPatchCreateCellPatchDiscretisationInfo(pc);CHKERRQ(ierr);
25554bbf5ea8SMatthew G. Knepley 
25564bbf5ea8SMatthew G. Knepley     /* OK, now build the work vectors */
25574bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, &pEnd);CHKERRQ(ierr);
25581202d238SPatrick Farrell     ierr = PetscMalloc1(patch->npatch, &patch->patchRHS);CHKERRQ(ierr);
25591202d238SPatrick Farrell     ierr = PetscMalloc1(patch->npatch, &patch->patchUpdate);CHKERRQ(ierr);
2560c2e6f3c0SFlorian Wechsung 
256161c4b389SFlorian Wechsung     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
25621202d238SPatrick Farrell       ierr = PetscMalloc1(patch->npatch, &patch->patchRHSWithArtificial);CHKERRQ(ierr);
2563c2e6f3c0SFlorian Wechsung       ierr = PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithArtificial);CHKERRQ(ierr);
2564c2e6f3c0SFlorian Wechsung     }
25650904074fSPatrick Farrell     if (isNonlinear) {
25660904074fSPatrick Farrell       ierr = PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithAll);CHKERRQ(ierr);
25670904074fSPatrick Farrell     }
25684bbf5ea8SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
25694bbf5ea8SMatthew G. Knepley       PetscInt dof;
25704bbf5ea8SMatthew G. Knepley 
25714bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr);
25721202d238SPatrick Farrell       ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchRHS[p-pStart]);CHKERRQ(ierr);
25731202d238SPatrick Farrell       ierr = VecSetUp(patch->patchRHS[p-pStart]);CHKERRQ(ierr);
25741202d238SPatrick Farrell       ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchUpdate[p-pStart]);CHKERRQ(ierr);
25751202d238SPatrick Farrell       ierr = VecSetUp(patch->patchUpdate[p-pStart]);CHKERRQ(ierr);
25760904074fSPatrick Farrell       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
25773bb0e8f7SKarl Rupp         const PetscInt    *gtolArray, *gtolArrayWithArtificial = NULL;
25783bb0e8f7SKarl Rupp         PetscInt           numPatchDofs, offset;
25793bb0e8f7SKarl Rupp         PetscInt           numPatchDofsWithArtificial, offsetWithArtificial;
25803bb0e8f7SKarl Rupp         PetscInt           dofWithoutArtificialCounter = 0;
25813bb0e8f7SKarl Rupp         PetscInt          *patchWithoutArtificialToWithArtificialArray;
25823bb0e8f7SKarl Rupp 
2583c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof);CHKERRQ(ierr);
25841202d238SPatrick Farrell         ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchRHSWithArtificial[p-pStart]);CHKERRQ(ierr);
25851202d238SPatrick Farrell         ierr = VecSetUp(patch->patchRHSWithArtificial[p-pStart]);CHKERRQ(ierr);
2586c2e6f3c0SFlorian Wechsung 
2587e047a90bSFlorian Wechsung         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
2588e047a90bSFlorian Wechsung         /* the index in the patch with all dofs */
2589c2e6f3c0SFlorian Wechsung         ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
259063deea8eSPatrick Farrell 
2591c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs);CHKERRQ(ierr);
259247aca4a6SPatrick Farrell         if (numPatchDofs == 0) {
259347aca4a6SPatrick Farrell           patch->dofMappingWithoutToWithArtificial[p-pStart] = NULL;
259447aca4a6SPatrick Farrell           continue;
259547aca4a6SPatrick Farrell         }
259663deea8eSPatrick Farrell 
2597c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
2598c2e6f3c0SFlorian Wechsung         ierr = ISGetIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial);CHKERRQ(ierr);
2599c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &numPatchDofsWithArtificial);CHKERRQ(ierr);
2600c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offsetWithArtificial);CHKERRQ(ierr);
2601c2e6f3c0SFlorian Wechsung 
2602c2e6f3c0SFlorian Wechsung         ierr = PetscMalloc1(numPatchDofs, &patchWithoutArtificialToWithArtificialArray);CHKERRQ(ierr);
2603b0c21b6aSKarl Rupp         for (i=0; i<numPatchDofsWithArtificial; i++) {
2604e047a90bSFlorian Wechsung           if (gtolArrayWithArtificial[i+offsetWithArtificial] == gtolArray[offset+dofWithoutArtificialCounter]) {
2605c2e6f3c0SFlorian Wechsung             patchWithoutArtificialToWithArtificialArray[dofWithoutArtificialCounter] = i;
2606c2e6f3c0SFlorian Wechsung             dofWithoutArtificialCounter++;
2607c2e6f3c0SFlorian Wechsung             if (dofWithoutArtificialCounter == numPatchDofs)
2608c2e6f3c0SFlorian Wechsung               break;
2609c2e6f3c0SFlorian Wechsung           }
2610c2e6f3c0SFlorian Wechsung         }
2611168bb5e4SPatrick Farrell         ierr = ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutArtificialToWithArtificialArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithArtificial[p-pStart]);CHKERRQ(ierr);
2612c2e6f3c0SFlorian Wechsung         ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
2613c2e6f3c0SFlorian Wechsung         ierr = ISRestoreIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial);CHKERRQ(ierr);
2614c2e6f3c0SFlorian Wechsung       }
26150904074fSPatrick Farrell       if (isNonlinear) {
26160904074fSPatrick Farrell         const PetscInt    *gtolArray, *gtolArrayWithAll = NULL;
26170904074fSPatrick Farrell         PetscInt           numPatchDofs, offset;
26180904074fSPatrick Farrell         PetscInt           numPatchDofsWithAll, offsetWithAll;
26190904074fSPatrick Farrell         PetscInt           dofWithoutAllCounter = 0;
26200904074fSPatrick Farrell         PetscInt          *patchWithoutAllToWithAllArray;
26210904074fSPatrick Farrell 
26220904074fSPatrick Farrell         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
26230904074fSPatrick Farrell         /* the index in the patch with all dofs */
26240904074fSPatrick Farrell         ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
26250904074fSPatrick Farrell 
26260904074fSPatrick Farrell         ierr = PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs);CHKERRQ(ierr);
262747aca4a6SPatrick Farrell         if (numPatchDofs == 0) {
2628b88cb22dSPatrick Farrell           patch->dofMappingWithoutToWithAll[p-pStart] = NULL;
262947aca4a6SPatrick Farrell           continue;
263047aca4a6SPatrick Farrell         }
26310904074fSPatrick Farrell 
26320904074fSPatrick Farrell         ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
26330904074fSPatrick Farrell         ierr = ISGetIndices(patch->gtolWithAll, &gtolArrayWithAll);CHKERRQ(ierr);
26340904074fSPatrick Farrell         ierr = PetscSectionGetDof(patch->gtolCountsWithAll, p, &numPatchDofsWithAll);CHKERRQ(ierr);
26350904074fSPatrick Farrell         ierr = PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offsetWithAll);CHKERRQ(ierr);
26360904074fSPatrick Farrell 
26370904074fSPatrick Farrell         ierr = PetscMalloc1(numPatchDofs, &patchWithoutAllToWithAllArray);CHKERRQ(ierr);
26380904074fSPatrick Farrell 
26390904074fSPatrick Farrell         for (i=0; i<numPatchDofsWithAll; i++) {
26400904074fSPatrick Farrell           if (gtolArrayWithAll[i+offsetWithAll] == gtolArray[offset+dofWithoutAllCounter]) {
26410904074fSPatrick Farrell             patchWithoutAllToWithAllArray[dofWithoutAllCounter] = i;
26420904074fSPatrick Farrell             dofWithoutAllCounter++;
26430904074fSPatrick Farrell             if (dofWithoutAllCounter == numPatchDofs)
26440904074fSPatrick Farrell               break;
26450904074fSPatrick Farrell           }
26460904074fSPatrick Farrell         }
2647168bb5e4SPatrick Farrell         ierr = ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutAllToWithAllArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithAll[p-pStart]);CHKERRQ(ierr);
26480904074fSPatrick Farrell         ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
26490904074fSPatrick Farrell         ierr = ISRestoreIndices(patch->gtolWithAll, &gtolArrayWithAll);CHKERRQ(ierr);
26500904074fSPatrick Farrell       }
26514bbf5ea8SMatthew G. Knepley     }
26524bbf5ea8SMatthew G. Knepley     if (patch->save_operators) {
26534bbf5ea8SMatthew G. Knepley       ierr = PetscMalloc1(patch->npatch, &patch->mat);CHKERRQ(ierr);
26544bbf5ea8SMatthew G. Knepley       for (i = 0; i < patch->npatch; ++i) {
2655c2e6f3c0SFlorian Wechsung         ierr = PCPatchCreateMatrix_Private(pc, i, &patch->mat[i], PETSC_FALSE);CHKERRQ(ierr);
26564bbf5ea8SMatthew G. Knepley       }
26574bbf5ea8SMatthew G. Knepley     }
26584bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr);
26594bbf5ea8SMatthew G. Knepley 
26604bbf5ea8SMatthew G. Knepley     /* If desired, calculate weights for dof multiplicity */
26614bbf5ea8SMatthew G. Knepley     if (patch->partition_of_unity) {
26623bb0e8f7SKarl Rupp       PetscScalar *input = NULL;
26633bb0e8f7SKarl Rupp       PetscScalar *output = NULL;
26643bb0e8f7SKarl Rupp       Vec global;
26653bb0e8f7SKarl Rupp 
26661202d238SPatrick Farrell       ierr = VecDuplicate(patch->localRHS, &patch->dof_weights);CHKERRQ(ierr);
266761c4b389SFlorian Wechsung       if(patch->local_composition_type == PC_COMPOSITE_ADDITIVE) {
26684bbf5ea8SMatthew G. Knepley         for (i = 0; i < patch->npatch; ++i) {
26694bbf5ea8SMatthew G. Knepley           PetscInt dof;
26704bbf5ea8SMatthew G. Knepley 
26714bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &dof);CHKERRQ(ierr);
26724bbf5ea8SMatthew G. Knepley           if (dof <= 0) continue;
26731202d238SPatrick Farrell           ierr = VecSet(patch->patchRHS[i], 1.0);CHKERRQ(ierr);
26740904074fSPatrick Farrell           ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchRHS[i], patch->dof_weights, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR);CHKERRQ(ierr);
26754bbf5ea8SMatthew G. Knepley         }
2676c2e6f3c0SFlorian Wechsung       } else {
2677e047a90bSFlorian Wechsung         /* multiplicative is actually only locally multiplicative and globally additive. need the pou where the mesh decomposition overlaps */
2678c2e6f3c0SFlorian Wechsung         ierr = VecSet(patch->dof_weights, 1.0);CHKERRQ(ierr);
26794bbf5ea8SMatthew G. Knepley       }
2680d132cafaSFlorian Wechsung 
2681d132cafaSFlorian Wechsung       VecDuplicate(patch->dof_weights, &global);
2682d132cafaSFlorian Wechsung       VecSet(global, 0.);
2683d132cafaSFlorian Wechsung 
2684d132cafaSFlorian Wechsung       ierr = VecGetArray(patch->dof_weights, &input);CHKERRQ(ierr);
2685d132cafaSFlorian Wechsung       ierr = VecGetArray(global, &output);CHKERRQ(ierr);
2686d132cafaSFlorian Wechsung       ierr = PetscSFReduceBegin(patch->defaultSF, MPIU_SCALAR, input, output, MPI_SUM);CHKERRQ(ierr);
2687d132cafaSFlorian Wechsung       ierr = PetscSFReduceEnd(patch->defaultSF, MPIU_SCALAR, input, output, MPI_SUM);CHKERRQ(ierr);
2688d132cafaSFlorian Wechsung       ierr = VecRestoreArray(patch->dof_weights, &input);CHKERRQ(ierr);
2689d132cafaSFlorian Wechsung       ierr = VecRestoreArray(global, &output);CHKERRQ(ierr);
2690d132cafaSFlorian Wechsung 
269105528938SFlorian Wechsung       ierr = VecReciprocal(global);CHKERRQ(ierr);
2692d132cafaSFlorian Wechsung 
2693d132cafaSFlorian Wechsung       ierr = VecGetArray(patch->dof_weights, &output);CHKERRQ(ierr);
2694d132cafaSFlorian Wechsung       ierr = VecGetArray(global, &input);CHKERRQ(ierr);
2695d132cafaSFlorian Wechsung       ierr = PetscSFBcastBegin(patch->defaultSF, MPIU_SCALAR, input, output);CHKERRQ(ierr);
2696d132cafaSFlorian Wechsung       ierr = PetscSFBcastEnd(patch->defaultSF, MPIU_SCALAR, input, output);CHKERRQ(ierr);
2697d132cafaSFlorian Wechsung       ierr = VecRestoreArray(patch->dof_weights, &output);CHKERRQ(ierr);
2698d132cafaSFlorian Wechsung       ierr = VecRestoreArray(global, &input);CHKERRQ(ierr);
2699d132cafaSFlorian Wechsung       ierr = VecDestroy(&global);CHKERRQ(ierr);
27004bbf5ea8SMatthew G. Knepley     }
270161c4b389SFlorian Wechsung     if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE && patch->save_operators) {
270296b79ebeSFlorian Wechsung       ierr = PetscMalloc1(patch->npatch, &patch->matWithArtificial);CHKERRQ(ierr);
27034bbf5ea8SMatthew G. Knepley     }
27044bbf5ea8SMatthew G. Knepley   }
2705dadc69c5SMatthew G. Knepley   ierr = (*patch->setupsolver)(pc);CHKERRQ(ierr);
2706dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
27074bbf5ea8SMatthew G. Knepley }
2708dadc69c5SMatthew G. Knepley 
2709dadc69c5SMatthew G. Knepley static PetscErrorCode PCApply_PATCH_Linear(PC pc, PetscInt i, Vec x, Vec y)
2710dadc69c5SMatthew G. Knepley {
2711dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2712dadc69c5SMatthew G. Knepley   KSP            ksp   = (KSP) patch->solver[i];
2713dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2714dadc69c5SMatthew G. Knepley 
2715dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2716dadc69c5SMatthew G. Knepley   if (!patch->save_operators) {
2717dadc69c5SMatthew G. Knepley     Mat mat;
2718dadc69c5SMatthew G. Knepley 
271934d8b122SPatrick Farrell     ierr = PCPatchCreateMatrix_Private(pc, i, &mat, PETSC_FALSE);CHKERRQ(ierr);
2720dadc69c5SMatthew G. Knepley     /* Populate operator here. */
272134d8b122SPatrick Farrell     ierr = PCPatchComputeOperator_Internal(pc, NULL, mat, i, PETSC_FALSE);CHKERRQ(ierr);
2722dadc69c5SMatthew G. Knepley     ierr = KSPSetOperators(ksp, mat, mat);CHKERRQ(ierr);
2723dadc69c5SMatthew G. Knepley     /* Drop reference so the KSPSetOperators below will blow it away. */
2724dadc69c5SMatthew G. Knepley     ierr = MatDestroy(&mat);CHKERRQ(ierr);
2725dadc69c5SMatthew G. Knepley   }
2726dadc69c5SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr);
2727dadc69c5SMatthew G. Knepley   if (!ksp->setfromoptionscalled) {
2728dadc69c5SMatthew G. Knepley     ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
2729dadc69c5SMatthew G. Knepley   }
2730dadc69c5SMatthew G. Knepley   ierr = KSPSolve(ksp, x, y);CHKERRQ(ierr);
2731ddad275aSPatrick Farrell   ierr = KSPCheckSolve(ksp, pc, y);CHKERRQ(ierr);
2732dadc69c5SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr);
2733dadc69c5SMatthew G. Knepley   if (!patch->save_operators) {
2734dadc69c5SMatthew G. Knepley     PC pc;
2735dadc69c5SMatthew G. Knepley     ierr = KSPSetOperators(ksp, NULL, NULL);CHKERRQ(ierr);
2736dadc69c5SMatthew G. Knepley     ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
2737dadc69c5SMatthew G. Knepley     /* Destroy PC context too, otherwise the factored matrix hangs around. */
2738dadc69c5SMatthew G. Knepley     ierr = PCReset(pc);CHKERRQ(ierr);
27394bbf5ea8SMatthew G. Knepley   }
27404bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
27414bbf5ea8SMatthew G. Knepley }
27424bbf5ea8SMatthew G. Knepley 
27436c9c532dSPatrick Farrell static PetscErrorCode PCUpdateMultiplicative_PATCH_Linear(PC pc, PetscInt i, PetscInt pStart)
27446c9c532dSPatrick Farrell {
27456c9c532dSPatrick Farrell   PC_PATCH      *patch = (PC_PATCH *) pc->data;
27466c9c532dSPatrick Farrell   Mat multMat;
27476c9c532dSPatrick Farrell   PetscErrorCode ierr;
27486c9c532dSPatrick Farrell 
27494d04e9f1SPatrick Farrell   PetscFunctionBegin;
27504d04e9f1SPatrick Farrell 
27516c9c532dSPatrick Farrell   if (patch->save_operators) {
27526c9c532dSPatrick Farrell     multMat = patch->matWithArtificial[i];
27536c9c532dSPatrick Farrell   } else {
27546c9c532dSPatrick Farrell     /*Very inefficient, hopefully we can just assemble the rectangular matrix in the first place.*/
27556c9c532dSPatrick Farrell     Mat matSquare;
27566c9c532dSPatrick Farrell     PetscInt dof;
27576c9c532dSPatrick Farrell     IS rowis;
27586c9c532dSPatrick Farrell     ierr = PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE);CHKERRQ(ierr);
27596c9c532dSPatrick Farrell     ierr = MatZeroEntries(matSquare);CHKERRQ(ierr);
27606c9c532dSPatrick Farrell     ierr = PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE);CHKERRQ(ierr);
27616c9c532dSPatrick Farrell     ierr = MatGetSize(matSquare, &dof, NULL);CHKERRQ(ierr);
27626c9c532dSPatrick Farrell     ierr = ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis); CHKERRQ(ierr);
27636c9c532dSPatrick Farrell     ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &multMat); CHKERRQ(ierr);
27646c9c532dSPatrick Farrell     ierr = MatDestroy(&matSquare);CHKERRQ(ierr);
27656c9c532dSPatrick Farrell     ierr = ISDestroy(&rowis); CHKERRQ(ierr);
27666c9c532dSPatrick Farrell   }
27676c9c532dSPatrick Farrell   ierr = MatMult(multMat, patch->patchUpdate[i], patch->patchRHSWithArtificial[i]); CHKERRQ(ierr);
27686c9c532dSPatrick Farrell   ierr = VecScale(patch->patchRHSWithArtificial[i], -1.0); CHKERRQ(ierr);
27690904074fSPatrick Farrell   ierr = PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHSWithArtificial[i], patch->localRHS, ADD_VALUES, SCATTER_REVERSE, SCATTER_WITHARTIFICIAL); CHKERRQ(ierr);
27706c9c532dSPatrick Farrell   if (!patch->save_operators) {
27716c9c532dSPatrick Farrell     ierr = MatDestroy(&multMat); CHKERRQ(ierr);
27726c9c532dSPatrick Farrell   }
27734d04e9f1SPatrick Farrell   PetscFunctionReturn(0);
27746c9c532dSPatrick Farrell }
27756c9c532dSPatrick Farrell 
27764bbf5ea8SMatthew G. Knepley static PetscErrorCode PCApply_PATCH(PC pc, Vec x, Vec y)
27774bbf5ea8SMatthew G. Knepley {
27784bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch    = (PC_PATCH *) pc->data;
27791202d238SPatrick Farrell   const PetscScalar *globalRHS  = NULL;
27801202d238SPatrick Farrell   PetscScalar       *localRHS   = NULL;
27811202d238SPatrick Farrell   PetscScalar       *globalUpdate  = NULL;
27824bbf5ea8SMatthew G. Knepley   const PetscInt    *bcNodes  = NULL;
27834bbf5ea8SMatthew G. Knepley   PetscInt           nsweep   = patch->symmetrise_sweep ? 2 : 1;
27844bbf5ea8SMatthew G. Knepley   PetscInt           start[2] = {0, 0};
27854bbf5ea8SMatthew G. Knepley   PetscInt           end[2]   = {-1, -1};
27864bbf5ea8SMatthew G. Knepley   const PetscInt     inc[2]   = {1, -1};
27871202d238SPatrick Farrell   const PetscScalar *localUpdate;
27884bbf5ea8SMatthew G. Knepley   const PetscInt    *iterationSet;
27894bbf5ea8SMatthew G. Knepley   PetscInt           pStart, numBcs, n, sweep, bc, j;
27904bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
27914bbf5ea8SMatthew G. Knepley 
27924bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
27934bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr);
27944bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsPushGetViewerOff(PETSC_TRUE);CHKERRQ(ierr);
279592d50984SMatthew G. Knepley   /* start, end, inc have 2 entries to manage a second backward sweep if we symmetrize */
27964bbf5ea8SMatthew G. Knepley   end[0]   = patch->npatch;
27974bbf5ea8SMatthew G. Knepley   start[1] = patch->npatch-1;
27984bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
27994bbf5ea8SMatthew G. Knepley     ierr = ISGetLocalSize(patch->iterationSet, &end[0]);CHKERRQ(ierr);
28004bbf5ea8SMatthew G. Knepley     start[1] = end[0] - 1;
28014bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr);
28024bbf5ea8SMatthew G. Knepley   }
28034bbf5ea8SMatthew G. Knepley   /* Scatter from global space into overlapped local spaces */
28041202d238SPatrick Farrell   ierr = VecGetArrayRead(x, &globalRHS);CHKERRQ(ierr);
28051202d238SPatrick Farrell   ierr = VecGetArray(patch->localRHS, &localRHS);CHKERRQ(ierr);
28061202d238SPatrick Farrell   ierr = PetscSFBcastBegin(patch->defaultSF, MPIU_SCALAR, globalRHS, localRHS);CHKERRQ(ierr);
28071202d238SPatrick Farrell   ierr = PetscSFBcastEnd(patch->defaultSF, MPIU_SCALAR, globalRHS, localRHS);CHKERRQ(ierr);
28081202d238SPatrick Farrell   ierr = VecRestoreArrayRead(x, &globalRHS);CHKERRQ(ierr);
28091202d238SPatrick Farrell   ierr = VecRestoreArray(patch->localRHS, &localRHS);CHKERRQ(ierr);
28104bbf5ea8SMatthew G. Knepley 
28111202d238SPatrick Farrell   ierr = VecSet(patch->localUpdate, 0.0);CHKERRQ(ierr);
28124bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, NULL);CHKERRQ(ierr);
28134bbf5ea8SMatthew G. Knepley   for (sweep = 0; sweep < nsweep; sweep++) {
28144bbf5ea8SMatthew G. Knepley     for (j = start[sweep]; j*inc[sweep] < end[sweep]*inc[sweep]; j += inc[sweep]) {
28154bbf5ea8SMatthew G. Knepley       PetscInt i       = patch->user_patches ? iterationSet[j] : j;
28164bbf5ea8SMatthew G. Knepley       PetscInt start, len;
28174bbf5ea8SMatthew G. Knepley 
28184bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &len);CHKERRQ(ierr);
28194bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetOffset(patch->gtolCounts, i+pStart, &start);CHKERRQ(ierr);
28204bbf5ea8SMatthew G. Knepley       /* TODO: Squash out these guys in the setup as well. */
28214bbf5ea8SMatthew G. Knepley       if (len <= 0) continue;
28224bbf5ea8SMatthew G. Knepley       /* TODO: Do we need different scatters for X and Y? */
28230904074fSPatrick Farrell       ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->localRHS, patch->patchRHS[i], INSERT_VALUES, SCATTER_FORWARD, SCATTER_INTERIOR);CHKERRQ(ierr);
28241202d238SPatrick Farrell       ierr = (*patch->applysolver)(pc, i, patch->patchRHS[i], patch->patchUpdate[i]);CHKERRQ(ierr);
28250904074fSPatrick Farrell       ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchUpdate[i], patch->localUpdate, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR);CHKERRQ(ierr);
282661c4b389SFlorian Wechsung       if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
28276c9c532dSPatrick Farrell         ierr = (*patch->updatemultiplicative)(pc, i, pStart);CHKERRQ(ierr);
2828c2e6f3c0SFlorian Wechsung       }
28294bbf5ea8SMatthew G. Knepley     }
28304bbf5ea8SMatthew G. Knepley   }
28314bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {ierr = ISRestoreIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr);}
28324bbf5ea8SMatthew G. Knepley   /* XXX: should we do this on the global vector? */
283373ec7555SLawrence Mitchell   if (patch->partition_of_unity) {
28341202d238SPatrick Farrell     ierr = VecPointwiseMult(patch->localUpdate, patch->localUpdate, patch->dof_weights);CHKERRQ(ierr);
28354bbf5ea8SMatthew G. Knepley   }
28361202d238SPatrick Farrell   /* Now patch->localUpdate contains the solution of the patch solves, so we need to combine them all. */
28374bbf5ea8SMatthew G. Knepley   ierr = VecSet(y, 0.0);CHKERRQ(ierr);
28381202d238SPatrick Farrell   ierr = VecGetArray(y, &globalUpdate);CHKERRQ(ierr);
28391202d238SPatrick Farrell   ierr = VecGetArrayRead(patch->localUpdate, &localUpdate);CHKERRQ(ierr);
28401202d238SPatrick Farrell   ierr = PetscSFReduceBegin(patch->defaultSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM);CHKERRQ(ierr);
28411202d238SPatrick Farrell   ierr = PetscSFReduceEnd(patch->defaultSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM);CHKERRQ(ierr);
28421202d238SPatrick Farrell   ierr = VecRestoreArrayRead(patch->localUpdate, &localUpdate);CHKERRQ(ierr);
28434bbf5ea8SMatthew G. Knepley 
28444bbf5ea8SMatthew G. Knepley   /* Now we need to send the global BC values through */
28451202d238SPatrick Farrell   ierr = VecGetArrayRead(x, &globalRHS);CHKERRQ(ierr);
28464bbf5ea8SMatthew G. Knepley   ierr = ISGetSize(patch->globalBcNodes, &numBcs);CHKERRQ(ierr);
28474bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr);
28484bbf5ea8SMatthew G. Knepley   ierr = VecGetLocalSize(x, &n);CHKERRQ(ierr);
28494bbf5ea8SMatthew G. Knepley   for (bc = 0; bc < numBcs; ++bc) {
28504bbf5ea8SMatthew G. Knepley     const PetscInt idx = bcNodes[bc];
28511202d238SPatrick Farrell     if (idx < n) globalUpdate[idx] = globalRHS[idx];
28524bbf5ea8SMatthew G. Knepley   }
28534bbf5ea8SMatthew G. Knepley 
28544bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr);
28551202d238SPatrick Farrell   ierr = VecRestoreArrayRead(x, &globalRHS);CHKERRQ(ierr);
28561202d238SPatrick Farrell   ierr = VecRestoreArray(y, &globalUpdate);CHKERRQ(ierr);
28574bbf5ea8SMatthew G. Knepley 
28584bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsPopGetViewerOff();CHKERRQ(ierr);
28594bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr);
28604bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
28614bbf5ea8SMatthew G. Knepley }
28624bbf5ea8SMatthew G. Knepley 
2863dadc69c5SMatthew G. Knepley static PetscErrorCode PCReset_PATCH_Linear(PC pc)
2864dadc69c5SMatthew G. Knepley {
2865dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2866dadc69c5SMatthew G. Knepley   PetscInt       i;
2867dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2868dadc69c5SMatthew G. Knepley 
2869dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2870dadc69c5SMatthew G. Knepley   if (patch->solver) {
2871dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = KSPReset((KSP) patch->solver[i]);CHKERRQ(ierr);}
2872dadc69c5SMatthew G. Knepley   }
2873dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
2874dadc69c5SMatthew G. Knepley }
2875dadc69c5SMatthew G. Knepley 
28764bbf5ea8SMatthew G. Knepley static PetscErrorCode PCReset_PATCH(PC pc)
28774bbf5ea8SMatthew G. Knepley {
28784bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
28794bbf5ea8SMatthew G. Knepley   PetscInt       i;
28804bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
28814bbf5ea8SMatthew G. Knepley 
28824bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2883fa84ea4cSLawrence Mitchell 
28844bbf5ea8SMatthew G. Knepley   ierr = PetscSFDestroy(&patch->defaultSF);CHKERRQ(ierr);
28854bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->cellCounts);CHKERRQ(ierr);
28865f824522SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->pointCounts);CHKERRQ(ierr);
28874bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->cellNumbering);CHKERRQ(ierr);
28884bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->gtolCounts);CHKERRQ(ierr);
28894bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->gtol);CHKERRQ(ierr);
28904bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->cells);CHKERRQ(ierr);
28915f824522SMatthew G. Knepley   ierr = ISDestroy(&patch->points);CHKERRQ(ierr);
28924bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->dofs);CHKERRQ(ierr);
28935f824522SMatthew G. Knepley   ierr = ISDestroy(&patch->offs);CHKERRQ(ierr);
28945f824522SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->patchSection);CHKERRQ(ierr);
28954bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->ghostBcNodes);CHKERRQ(ierr);
28964bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->globalBcNodes);CHKERRQ(ierr);
289711ac8bb0SFlorian Wechsung   ierr = PetscSectionDestroy(&patch->gtolCountsWithArtificial);CHKERRQ(ierr);
289811ac8bb0SFlorian Wechsung   ierr = ISDestroy(&patch->gtolWithArtificial);CHKERRQ(ierr);
289911ac8bb0SFlorian Wechsung   ierr = ISDestroy(&patch->dofsWithArtificial);CHKERRQ(ierr);
290011ac8bb0SFlorian Wechsung   ierr = ISDestroy(&patch->offsWithArtificial);CHKERRQ(ierr);
29010904074fSPatrick Farrell   ierr = PetscSectionDestroy(&patch->gtolCountsWithAll);CHKERRQ(ierr);
29020904074fSPatrick Farrell   ierr = ISDestroy(&patch->gtolWithAll);CHKERRQ(ierr);
29030904074fSPatrick Farrell   ierr = ISDestroy(&patch->dofsWithAll);CHKERRQ(ierr);
29040904074fSPatrick Farrell   ierr = ISDestroy(&patch->offsWithAll);CHKERRQ(ierr);
2905fa84ea4cSLawrence Mitchell   ierr = VecDestroy(&patch->cellMats);CHKERRQ(ierr);
2906f98464cbSLawrence Mitchell   ierr = VecDestroy(&patch->intFacetMats);CHKERRQ(ierr);
2907fa84ea4cSLawrence Mitchell   ierr = ISDestroy(&patch->allCells);CHKERRQ(ierr);
29084bbf5ea8SMatthew G. Knepley 
29095f824522SMatthew G. Knepley   if (patch->dofSection) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscSectionDestroy(&patch->dofSection[i]);CHKERRQ(ierr);}
29104bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->dofSection);CHKERRQ(ierr);
29114bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->bs);CHKERRQ(ierr);
29124bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->nodesPerCell);CHKERRQ(ierr);
29135f824522SMatthew G. Knepley   if (patch->cellNodeMap) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscFree(patch->cellNodeMap[i]);CHKERRQ(ierr);}
29144bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->cellNodeMap);CHKERRQ(ierr);
29154bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->subspaceOffsets);CHKERRQ(ierr);
29164bbf5ea8SMatthew G. Knepley 
2917dadc69c5SMatthew G. Knepley   ierr = (*patch->resetsolver)(pc);CHKERRQ(ierr);
29184bbf5ea8SMatthew G. Knepley 
2919e4c66b91SPatrick Farrell   if (patch->subspaces_to_exclude) {
2920e4c66b91SPatrick Farrell     PetscHSetIDestroy(&patch->subspaces_to_exclude);
2921e4c66b91SPatrick Farrell   }
2922e4c66b91SPatrick Farrell 
29231202d238SPatrick Farrell   ierr = VecDestroy(&patch->localRHS);CHKERRQ(ierr);
29241202d238SPatrick Farrell   ierr = VecDestroy(&patch->localUpdate);CHKERRQ(ierr);
29251202d238SPatrick Farrell   if (patch->patchRHS) {
29261202d238SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchRHS[i]);CHKERRQ(ierr);}
29271202d238SPatrick Farrell     ierr = PetscFree(patch->patchRHS);CHKERRQ(ierr);
29284bbf5ea8SMatthew G. Knepley   }
29291202d238SPatrick Farrell   if (patch->patchUpdate) {
29301202d238SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchUpdate[i]);CHKERRQ(ierr);}
29311202d238SPatrick Farrell     ierr = PetscFree(patch->patchUpdate);CHKERRQ(ierr);
29324bbf5ea8SMatthew G. Knepley   }
29334bbf5ea8SMatthew G. Knepley   ierr = VecDestroy(&patch->dof_weights);CHKERRQ(ierr);
29344bbf5ea8SMatthew G. Knepley   if (patch->patch_dof_weights) {
29355f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patch_dof_weights[i]);CHKERRQ(ierr);}
29364bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->patch_dof_weights);CHKERRQ(ierr);
29374bbf5ea8SMatthew G. Knepley   }
29384bbf5ea8SMatthew G. Knepley   if (patch->mat) {
29395f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->mat[i]);CHKERRQ(ierr);}
29404bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->mat);CHKERRQ(ierr);
29415f824522SMatthew G. Knepley   }
294211ac8bb0SFlorian Wechsung   if (patch->matWithArtificial) {
294311ac8bb0SFlorian Wechsung     for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->matWithArtificial[i]);CHKERRQ(ierr);}
294411ac8bb0SFlorian Wechsung     ierr = PetscFree(patch->matWithArtificial);CHKERRQ(ierr);
294511ac8bb0SFlorian Wechsung   }
29461202d238SPatrick Farrell   if (patch->patchRHSWithArtificial) {
29471202d238SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchRHSWithArtificial[i]);CHKERRQ(ierr);}
29481202d238SPatrick Farrell     ierr = PetscFree(patch->patchRHSWithArtificial);CHKERRQ(ierr);
294911ac8bb0SFlorian Wechsung   }
295096b79ebeSFlorian Wechsung   if(patch->dofMappingWithoutToWithArtificial) {
295196b79ebeSFlorian Wechsung     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->dofMappingWithoutToWithArtificial[i]);CHKERRQ(ierr);}
295296b79ebeSFlorian Wechsung     ierr = PetscFree(patch->dofMappingWithoutToWithArtificial);CHKERRQ(ierr);
295396b79ebeSFlorian Wechsung 
295496b79ebeSFlorian Wechsung   }
29550904074fSPatrick Farrell   if(patch->dofMappingWithoutToWithAll) {
29560904074fSPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->dofMappingWithoutToWithAll[i]);CHKERRQ(ierr);}
29570904074fSPatrick Farrell     ierr = PetscFree(patch->dofMappingWithoutToWithAll);CHKERRQ(ierr);
29580904074fSPatrick Farrell 
29590904074fSPatrick Farrell   }
29604bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr);
29615f824522SMatthew G. Knepley   if (patch->userIS) {
29625f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->userIS[i]);CHKERRQ(ierr);}
29635f824522SMatthew G. Knepley     ierr = PetscFree(patch->userIS);CHKERRQ(ierr);
29645f824522SMatthew G. Knepley   }
2965fe988be2SFlorian Wechsung   ierr = PetscFree(patch->precomputedTensorLocations);CHKERRQ(ierr);
2966f98464cbSLawrence Mitchell   ierr = PetscFree(patch->precomputedIntFacetTensorLocations);CHKERRQ(ierr);
2967f98464cbSLawrence Mitchell 
29684bbf5ea8SMatthew G. Knepley   patch->bs          = 0;
29694bbf5ea8SMatthew G. Knepley   patch->cellNodeMap = NULL;
29707974b488SMatthew G. Knepley   patch->nsubspaces  = 0;
29714bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->iterationSet);CHKERRQ(ierr);
29725f824522SMatthew G. Knepley 
29735f824522SMatthew G. Knepley   ierr = PetscViewerDestroy(&patch->viewerSection);CHKERRQ(ierr);
29744bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
29754bbf5ea8SMatthew G. Knepley }
29764bbf5ea8SMatthew G. Knepley 
2977dadc69c5SMatthew G. Knepley static PetscErrorCode PCDestroy_PATCH_Linear(PC pc)
29784bbf5ea8SMatthew G. Knepley {
29794bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
29804bbf5ea8SMatthew G. Knepley   PetscInt       i;
29814bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
29824bbf5ea8SMatthew G. Knepley 
29834bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2984dadc69c5SMatthew G. Knepley   if (patch->solver) {
2985dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = KSPDestroy((KSP *) &patch->solver[i]);CHKERRQ(ierr);}
2986dadc69c5SMatthew G. Knepley     ierr = PetscFree(patch->solver);CHKERRQ(ierr);
29874bbf5ea8SMatthew G. Knepley   }
2988dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
2989dadc69c5SMatthew G. Knepley }
2990dadc69c5SMatthew G. Knepley 
2991dadc69c5SMatthew G. Knepley static PetscErrorCode PCDestroy_PATCH(PC pc)
2992dadc69c5SMatthew G. Knepley {
2993dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2994dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2995dadc69c5SMatthew G. Knepley 
2996dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2997dadc69c5SMatthew G. Knepley   ierr = PCReset_PATCH(pc);CHKERRQ(ierr);
2998dadc69c5SMatthew G. Knepley   ierr = (*patch->destroysolver)(pc);CHKERRQ(ierr);
29994bbf5ea8SMatthew G. Knepley   ierr = PetscFree(pc->data);CHKERRQ(ierr);
30004bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
30014bbf5ea8SMatthew G. Knepley }
30024bbf5ea8SMatthew G. Knepley 
30034bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetFromOptions_PATCH(PetscOptionItems *PetscOptionsObject, PC pc)
30044bbf5ea8SMatthew G. Knepley {
30054bbf5ea8SMatthew G. Knepley   PC_PATCH            *patch = (PC_PATCH *) pc->data;
30064bbf5ea8SMatthew G. Knepley   PCPatchConstructType patchConstructionType = PC_PATCH_STAR;
30075f824522SMatthew G. Knepley   char                 sub_mat_type[PETSC_MAX_PATH_LEN];
300810534d48SPatrick Farrell   char                 option[PETSC_MAX_PATH_LEN];
30095f824522SMatthew G. Knepley   const char          *prefix;
30104bbf5ea8SMatthew G. Knepley   PetscBool            flg, dimflg, codimflg;
30115f824522SMatthew G. Knepley   MPI_Comm             comm;
3012a48c39c8SPatrick Farrell   PetscInt            *ifields, nfields, k;
30134bbf5ea8SMatthew G. Knepley   PetscErrorCode       ierr;
301461c4b389SFlorian Wechsung   PCCompositeType loctype = PC_COMPOSITE_ADDITIVE;
30154bbf5ea8SMatthew G. Knepley 
30164bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
30175f824522SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) pc, &comm);CHKERRQ(ierr);
30185f824522SMatthew G. Knepley   ierr = PetscObjectGetOptionsPrefix((PetscObject) pc, &prefix);CHKERRQ(ierr);
301910534d48SPatrick Farrell   ierr = PetscOptionsHead(PetscOptionsObject, "Patch solver options");CHKERRQ(ierr);
302010534d48SPatrick Farrell 
30211b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_save_operators", patch->classname);CHKERRQ(ierr);
302210534d48SPatrick Farrell   ierr = PetscOptionsBool(option,  "Store all patch operators for lifetime of object?", "PCPatchSetSaveOperators", patch->save_operators, &patch->save_operators, &flg);CHKERRQ(ierr);
302310534d48SPatrick Farrell 
3024fa84ea4cSLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_precompute_element_tensors", patch->classname);
3025fa84ea4cSLawrence Mitchell   ierr = PetscOptionsBool(option,  "Compute each element tensor only once?", "PCPatchSetPrecomputeElementTensors", patch->precomputeElementTensors, &patch->precomputeElementTensors, &flg);CHKERRQ(ierr);
302610534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_partition_of_unity", patch->classname);
302710534d48SPatrick Farrell   ierr = PetscOptionsBool(option, "Weight contributions by dof multiplicity?", "PCPatchSetPartitionOfUnity", patch->partition_of_unity, &patch->partition_of_unity, &flg);CHKERRQ(ierr);
302810534d48SPatrick Farrell 
30291b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_local_type", patch->classname);CHKERRQ(ierr);
303010534d48SPatrick Farrell   ierr = PetscOptionsEnum(option,"Type of local solver composition (additive or multiplicative)","PCPatchSetLocalComposition",PCCompositeTypes,(PetscEnum)loctype,(PetscEnum*)&loctype,&flg);CHKERRQ(ierr);
303161c4b389SFlorian Wechsung   if(flg) { ierr = PCPatchSetLocalComposition(pc, loctype);CHKERRQ(ierr);}
303210534d48SPatrick Farrell 
30331b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_dim", patch->classname);CHKERRQ(ierr);
303410534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "What dimension of mesh point to construct patches by? (0 = vertices)", "PCPATCH", patch->dim, &patch->dim, &dimflg);CHKERRQ(ierr);
30351b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_codim", patch->classname);CHKERRQ(ierr);
303610534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "What co-dimension of mesh point to construct patches by? (0 = cells)", "PCPATCH", patch->codim, &patch->codim, &codimflg);CHKERRQ(ierr);
303715b861d2SVaclav Hapla   if (dimflg && codimflg) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Can only set one of dimension or co-dimension");
303810534d48SPatrick Farrell 
30391b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_type", patch->classname);CHKERRQ(ierr);
304010534d48SPatrick Farrell   ierr = PetscOptionsEnum(option, "How should the patches be constructed?", "PCPatchSetConstructType", PCPatchConstructTypes, (PetscEnum) patchConstructionType, (PetscEnum *) &patchConstructionType, &flg);CHKERRQ(ierr);
30414bbf5ea8SMatthew G. Knepley   if (flg) {ierr = PCPatchSetConstructType(pc, patchConstructionType, NULL, NULL);CHKERRQ(ierr);}
304210534d48SPatrick Farrell 
30431b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_vanka_dim", patch->classname);CHKERRQ(ierr);
304410534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "Topological dimension of entities for Vanka to ignore", "PCPATCH", patch->vankadim, &patch->vankadim, &flg);CHKERRQ(ierr);
304510534d48SPatrick Farrell 
30461b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_ignore_dim", patch->classname);CHKERRQ(ierr);
304710534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "Topological dimension of entities for completion to ignore", "PCPATCH", patch->ignoredim, &patch->ignoredim, &flg);CHKERRQ(ierr);
304810534d48SPatrick Farrell 
30491b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_pardecomp_overlap", patch->classname);CHKERRQ(ierr);
3050b525f888SPatrick Farrell   ierr = PetscOptionsInt(option, "What overlap should we use in construct type pardecomp?", "PCPATCH", patch->pardecomp_overlap, &patch->pardecomp_overlap, &flg);CHKERRQ(ierr);
3051b525f888SPatrick Farrell 
30521b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_sub_mat_type", patch->classname);CHKERRQ(ierr);
305310534d48SPatrick Farrell   ierr = PetscOptionsFList(option, "Matrix type for patch solves", "PCPatchSetSubMatType", MatList, NULL, sub_mat_type, PETSC_MAX_PATH_LEN, &flg);CHKERRQ(ierr);
30544bbf5ea8SMatthew G. Knepley   if (flg) {ierr = PCPatchSetSubMatType(pc, sub_mat_type);CHKERRQ(ierr);}
305510534d48SPatrick Farrell 
30561b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_symmetrise_sweep", patch->classname);CHKERRQ(ierr);
305710534d48SPatrick Farrell   ierr = PetscOptionsBool(option, "Go start->end, end->start?", "PCPATCH", patch->symmetrise_sweep, &patch->symmetrise_sweep, &flg);CHKERRQ(ierr);
3058e4c66b91SPatrick Farrell 
3059a48c39c8SPatrick Farrell   /* If the user has set the number of subspaces, use that for the buffer size,
3060a48c39c8SPatrick Farrell      otherwise use a large number */
3061a48c39c8SPatrick Farrell   if (patch->nsubspaces <= 0) {
3062a48c39c8SPatrick Farrell     nfields = 128;
3063a48c39c8SPatrick Farrell   } else {
3064a48c39c8SPatrick Farrell     nfields = patch->nsubspaces;
3065a48c39c8SPatrick Farrell   }
3066a48c39c8SPatrick Farrell   ierr = PetscMalloc1(nfields, &ifields);CHKERRQ(ierr);
30671b5e6740SSatish Balay   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exclude_subspaces", patch->classname);CHKERRQ(ierr);
306810534d48SPatrick Farrell   ierr = PetscOptionsGetIntArray(((PetscObject)pc)->options,((PetscObject)pc)->prefix,option,ifields,&nfields,&flg);CHKERRQ(ierr);
3069e4c66b91SPatrick 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");
3070e4c66b91SPatrick Farrell   if (flg) {
3071e4c66b91SPatrick Farrell     PetscHSetIClear(patch->subspaces_to_exclude);
307259b66c28SPatrick Farrell     for (k = 0; k < nfields; k++) {
3073e4c66b91SPatrick Farrell       PetscHSetIAdd(patch->subspaces_to_exclude, ifields[k]);
3074e4c66b91SPatrick Farrell     }
3075e4c66b91SPatrick Farrell   }
307659b66c28SPatrick Farrell   ierr = PetscFree(ifields);CHKERRQ(ierr);
30775f824522SMatthew G. Knepley 
3078fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_patches_view", patch->classname);CHKERRQ(ierr);
307910534d48SPatrick Farrell   ierr = PetscOptionsBool(option, "Print out information during patch construction", "PCPATCH", patch->viewPatches, &patch->viewPatches, &flg);CHKERRQ(ierr);
3080fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_cells_view", patch->classname);CHKERRQ(ierr);
308110534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerCells, &patch->formatCells, &patch->viewCells);CHKERRQ(ierr);
3082fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_interior_facets_view", patch->classname);CHKERRQ(ierr);
3083fbb82ab5SLawrence Mitchell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerIntFacets, &patch->formatIntFacets, &patch->viewIntFacets);CHKERRQ(ierr);
3084fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exterior_facets_view", patch->classname);CHKERRQ(ierr);
3085fbb82ab5SLawrence Mitchell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerExtFacets, &patch->formatExtFacets, &patch->viewExtFacets);CHKERRQ(ierr);
3086fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_points_view", patch->classname);CHKERRQ(ierr);
308710534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerPoints, &patch->formatPoints, &patch->viewPoints);CHKERRQ(ierr);
3088fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_section_view", patch->classname);CHKERRQ(ierr);
308910534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerSection, &patch->formatSection, &patch->viewSection);CHKERRQ(ierr);
3090fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_mat_view", patch->classname);CHKERRQ(ierr);
309110534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerMatrix, &patch->formatMatrix, &patch->viewMatrix);CHKERRQ(ierr);
30924bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsTail();CHKERRQ(ierr);
30935f824522SMatthew G. Knepley   patch->optionsSet = PETSC_TRUE;
30944bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
30954bbf5ea8SMatthew G. Knepley }
30964bbf5ea8SMatthew G. Knepley 
30974bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUpOnBlocks_PATCH(PC pc)
30984bbf5ea8SMatthew G. Knepley {
30994bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch = (PC_PATCH*) pc->data;
31004bbf5ea8SMatthew G. Knepley   KSPConvergedReason reason;
31014bbf5ea8SMatthew G. Knepley   PetscInt           i;
31024bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
31034bbf5ea8SMatthew G. Knepley 
31044bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3105a1eac568SLawrence Mitchell   if (!patch->save_operators) {
3106a1eac568SLawrence Mitchell     /* Can't do this here because the sub KSPs don't have an operator attached yet. */
3107a1eac568SLawrence Mitchell     PetscFunctionReturn(0);
3108a1eac568SLawrence Mitchell   }
31094bbf5ea8SMatthew G. Knepley   for (i = 0; i < patch->npatch; ++i) {
3110dadc69c5SMatthew G. Knepley     if (!((KSP) patch->solver[i])->setfromoptionscalled) {
3111dadc69c5SMatthew G. Knepley       ierr = KSPSetFromOptions((KSP) patch->solver[i]);CHKERRQ(ierr);
3112a1eac568SLawrence Mitchell     }
3113dadc69c5SMatthew G. Knepley     ierr = KSPSetUp((KSP) patch->solver[i]);CHKERRQ(ierr);
3114dadc69c5SMatthew G. Knepley     ierr = KSPGetConvergedReason((KSP) patch->solver[i], &reason);CHKERRQ(ierr);
3115c0decd05SBarry Smith     if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR;
31164bbf5ea8SMatthew G. Knepley   }
31174bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
31184bbf5ea8SMatthew G. Knepley }
31194bbf5ea8SMatthew G. Knepley 
31204bbf5ea8SMatthew G. Knepley static PetscErrorCode PCView_PATCH(PC pc, PetscViewer viewer)
31214bbf5ea8SMatthew G. Knepley {
31224bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
31234bbf5ea8SMatthew G. Knepley   PetscViewer    sviewer;
31244bbf5ea8SMatthew G. Knepley   PetscBool      isascii;
31254bbf5ea8SMatthew G. Knepley   PetscMPIInt    rank;
31264bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
31274bbf5ea8SMatthew G. Knepley 
31284bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
31294bbf5ea8SMatthew G. Knepley   /* TODO Redo tabbing with set tbas in new style */
31304bbf5ea8SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii);CHKERRQ(ierr);
31314bbf5ea8SMatthew G. Knepley   if (!isascii) PetscFunctionReturn(0);
31324bbf5ea8SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) pc), &rank);CHKERRQ(ierr);
31334bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
31344bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "Subspace Correction preconditioner with %d patches\n", patch->npatch);CHKERRQ(ierr);
313561c4b389SFlorian Wechsung   if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
3136c2e6f3c0SFlorian Wechsung     ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: multiplicative\n");CHKERRQ(ierr);
3137e047a90bSFlorian Wechsung   } else {
313873ec7555SLawrence Mitchell     ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: additive\n");CHKERRQ(ierr);
3139c2e6f3c0SFlorian Wechsung   }
31404bbf5ea8SMatthew G. Knepley   if (patch->partition_of_unity) {ierr = PetscViewerASCIIPrintf(viewer, "Weighting by partition of unity\n");CHKERRQ(ierr);}
31414bbf5ea8SMatthew G. Knepley   else                           {ierr = PetscViewerASCIIPrintf(viewer, "Not weighting by partition of unity\n");CHKERRQ(ierr);}
31424bbf5ea8SMatthew G. Knepley   if (patch->symmetrise_sweep) {ierr = PetscViewerASCIIPrintf(viewer, "Symmetrising sweep (start->end, then end->start)\n");CHKERRQ(ierr);}
31434bbf5ea8SMatthew G. Knepley   else                         {ierr = PetscViewerASCIIPrintf(viewer, "Not symmetrising sweep\n");CHKERRQ(ierr);}
3144fa84ea4cSLawrence Mitchell   if (!patch->precomputeElementTensors) {ierr = PetscViewerASCIIPrintf(viewer, "Not precomputing element tensors (overlapping cells rebuilt in every patch assembly)\n");CHKERRQ(ierr);}
3145fa84ea4cSLawrence Mitchell   else                            {ierr = PetscViewerASCIIPrintf(viewer, "Precomputing element tensors (each cell assembled only once)\n");CHKERRQ(ierr);}
31464bbf5ea8SMatthew G. Knepley   if (!patch->save_operators) {ierr = PetscViewerASCIIPrintf(viewer, "Not saving patch operators (rebuilt every PCApply)\n");CHKERRQ(ierr);}
31474bbf5ea8SMatthew G. Knepley   else                        {ierr = PetscViewerASCIIPrintf(viewer, "Saving patch operators (rebuilt every PCSetUp)\n");CHKERRQ(ierr);}
31484bbf5ea8SMatthew G. Knepley   if (patch->patchconstructop == PCPatchConstruct_Star)       {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: star\n");CHKERRQ(ierr);}
31494bbf5ea8SMatthew G. Knepley   else if (patch->patchconstructop == PCPatchConstruct_Vanka) {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: Vanka\n");CHKERRQ(ierr);}
31504bbf5ea8SMatthew G. Knepley   else if (patch->patchconstructop == PCPatchConstruct_User)  {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: user-specified\n");CHKERRQ(ierr);}
31514bbf5ea8SMatthew G. Knepley   else                                                        {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: unknown\n");CHKERRQ(ierr);}
31525d30859aSPatrick Farrell 
31535d30859aSPatrick Farrell   if (patch->isNonlinear) {
31545d30859aSPatrick Farrell     ierr = PetscViewerASCIIPrintf(viewer, "SNES on patches (all same):\n");CHKERRQ(ierr);
31555d30859aSPatrick Farrell   } else {
31565d30859aSPatrick Farrell     ierr = PetscViewerASCIIPrintf(viewer, "KSP on patches (all same):\n");CHKERRQ(ierr);
31575d30859aSPatrick Farrell   }
3158dadc69c5SMatthew G. Knepley   if (patch->solver) {
31594bbf5ea8SMatthew G. Knepley     ierr = PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr);
31604bbf5ea8SMatthew G. Knepley     if (!rank) {
31614bbf5ea8SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(sviewer);CHKERRQ(ierr);
3162dadc69c5SMatthew G. Knepley       ierr = PetscObjectView(patch->solver[0], sviewer);CHKERRQ(ierr);
31634bbf5ea8SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(sviewer);CHKERRQ(ierr);
31644bbf5ea8SMatthew G. Knepley     }
31654bbf5ea8SMatthew G. Knepley     ierr = PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr);
31664bbf5ea8SMatthew G. Knepley   } else {
31674bbf5ea8SMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
3168dadc69c5SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Solver not yet set.\n");CHKERRQ(ierr);
31694bbf5ea8SMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
31704bbf5ea8SMatthew G. Knepley   }
31714bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
31724bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
31734bbf5ea8SMatthew G. Knepley }
31744bbf5ea8SMatthew G. Knepley 
3175e5893cccSMatthew G. Knepley /*MC
317698ed095eSMatthew G. Knepley   PCPATCH - A PC object that encapsulates flexible definition of blocks for overlapping and non-overlapping
317798ed095eSMatthew G. Knepley             small block additive preconditioners. Block definition is based on topology from
3178e5893cccSMatthew G. Knepley             a DM and equation numbering from a PetscSection.
3179e5893cccSMatthew G. Knepley 
3180e5893cccSMatthew G. Knepley   Options Database Keys:
3181e5893cccSMatthew G. Knepley + -pc_patch_cells_view   - Views the process local cell numbers for each patch
3182e5893cccSMatthew G. Knepley . -pc_patch_points_view  - Views the process local mesh point numbers for each patch
3183e5893cccSMatthew G. Knepley . -pc_patch_g2l_view     - Views the map between global dofs and patch local dofs for each patch
3184e5893cccSMatthew G. Knepley . -pc_patch_patches_view - Views the global dofs associated with each patch and its boundary
3185e5893cccSMatthew G. Knepley - -pc_patch_sub_mat_view - Views the matrix associated with each patch
3186e5893cccSMatthew G. Knepley 
3187e5893cccSMatthew G. Knepley   Level: intermediate
3188e5893cccSMatthew G. Knepley 
3189e5893cccSMatthew G. Knepley .seealso: PCType, PCCreate(), PCSetType()
3190e5893cccSMatthew G. Knepley M*/
3191642283e9SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PCCreate_Patch(PC pc)
31924bbf5ea8SMatthew G. Knepley {
31934bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch;
31944bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
31954bbf5ea8SMatthew G. Knepley 
31964bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
31974bbf5ea8SMatthew G. Knepley   ierr = PetscNewLog(pc, &patch);CHKERRQ(ierr);
31984bbf5ea8SMatthew G. Knepley 
3199e4c66b91SPatrick Farrell   if (patch->subspaces_to_exclude) {
3200e4c66b91SPatrick Farrell     PetscHSetIDestroy(&patch->subspaces_to_exclude);
3201e4c66b91SPatrick Farrell   }
3202e4c66b91SPatrick Farrell   PetscHSetICreate(&patch->subspaces_to_exclude);
3203e4c66b91SPatrick Farrell 
320410534d48SPatrick Farrell   patch->classname = "pc";
3205debbdec3SPatrick Farrell   patch->isNonlinear = PETSC_FALSE;
320610534d48SPatrick Farrell 
32074bbf5ea8SMatthew G. Knepley   /* Set some defaults */
32085f824522SMatthew G. Knepley   patch->combined           = PETSC_FALSE;
32094bbf5ea8SMatthew G. Knepley   patch->save_operators     = PETSC_TRUE;
321061c4b389SFlorian Wechsung   patch->local_composition_type = PC_COMPOSITE_ADDITIVE;
3211fa84ea4cSLawrence Mitchell   patch->precomputeElementTensors = PETSC_FALSE;
32124bbf5ea8SMatthew G. Knepley   patch->partition_of_unity = PETSC_FALSE;
32134bbf5ea8SMatthew G. Knepley   patch->codim              = -1;
32144bbf5ea8SMatthew G. Knepley   patch->dim                = -1;
32154bbf5ea8SMatthew G. Knepley   patch->vankadim           = -1;
32165f824522SMatthew G. Knepley   patch->ignoredim          = -1;
3217b525f888SPatrick Farrell   patch->pardecomp_overlap  = 0;
32184bbf5ea8SMatthew G. Knepley   patch->patchconstructop   = PCPatchConstruct_Star;
32194bbf5ea8SMatthew G. Knepley   patch->symmetrise_sweep   = PETSC_FALSE;
32205f824522SMatthew G. Knepley   patch->npatch             = 0;
32214bbf5ea8SMatthew G. Knepley   patch->userIS             = NULL;
32225f824522SMatthew G. Knepley   patch->optionsSet         = PETSC_FALSE;
32234bbf5ea8SMatthew G. Knepley   patch->iterationSet       = NULL;
32244bbf5ea8SMatthew G. Knepley   patch->user_patches       = PETSC_FALSE;
32255f824522SMatthew G. Knepley   ierr = PetscStrallocpy(MATDENSE, (char **) &patch->sub_mat_type);CHKERRQ(ierr);
32265f824522SMatthew G. Knepley   patch->viewPatches        = PETSC_FALSE;
32275f824522SMatthew G. Knepley   patch->viewCells          = PETSC_FALSE;
32285f824522SMatthew G. Knepley   patch->viewPoints         = PETSC_FALSE;
32295f824522SMatthew G. Knepley   patch->viewSection        = PETSC_FALSE;
32305f824522SMatthew G. Knepley   patch->viewMatrix         = PETSC_FALSE;
3231dadc69c5SMatthew G. Knepley   patch->setupsolver        = PCSetUp_PATCH_Linear;
3232dadc69c5SMatthew G. Knepley   patch->applysolver        = PCApply_PATCH_Linear;
3233dadc69c5SMatthew G. Knepley   patch->resetsolver        = PCReset_PATCH_Linear;
3234dadc69c5SMatthew G. Knepley   patch->destroysolver      = PCDestroy_PATCH_Linear;
32356c9c532dSPatrick Farrell   patch->updatemultiplicative = PCUpdateMultiplicative_PATCH_Linear;
323647aca4a6SPatrick Farrell   patch->dofMappingWithoutToWithArtificial = NULL;
323747aca4a6SPatrick Farrell   patch->dofMappingWithoutToWithAll = NULL;
32384bbf5ea8SMatthew G. Knepley 
32394bbf5ea8SMatthew G. Knepley   pc->data                 = (void *) patch;
32404bbf5ea8SMatthew G. Knepley   pc->ops->apply           = PCApply_PATCH;
32414bbf5ea8SMatthew G. Knepley   pc->ops->applytranspose  = 0; /* PCApplyTranspose_PATCH; */
32424bbf5ea8SMatthew G. Knepley   pc->ops->setup           = PCSetUp_PATCH;
32434bbf5ea8SMatthew G. Knepley   pc->ops->reset           = PCReset_PATCH;
32444bbf5ea8SMatthew G. Knepley   pc->ops->destroy         = PCDestroy_PATCH;
32454bbf5ea8SMatthew G. Knepley   pc->ops->setfromoptions  = PCSetFromOptions_PATCH;
32464bbf5ea8SMatthew G. Knepley   pc->ops->setuponblocks   = PCSetUpOnBlocks_PATCH;
32474bbf5ea8SMatthew G. Knepley   pc->ops->view            = PCView_PATCH;
32484bbf5ea8SMatthew G. Knepley   pc->ops->applyrichardson = 0;
32494bbf5ea8SMatthew G. Knepley 
32504bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
32514bbf5ea8SMatthew G. Knepley }
3252