xref: /petsc/src/ksp/pc/impls/patch/pcpatch.c (revision bc7fa33a7435ea91943ef308b08afbc7b463e1c4)
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 */
3444bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetPartitionOfUnity(PC pc, PetscBool flg)
3454bbf5ea8SMatthew G. Knepley {
3464bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3474bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3484bbf5ea8SMatthew G. Knepley   patch->partition_of_unity = flg;
3494bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3504bbf5ea8SMatthew G. Knepley }
3514bbf5ea8SMatthew G. Knepley 
3524bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3534bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetPartitionOfUnity(PC pc, PetscBool *flg)
3544bbf5ea8SMatthew G. Knepley {
3554bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3564bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3574bbf5ea8SMatthew G. Knepley   *flg = patch->partition_of_unity;
3584bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3594bbf5ea8SMatthew G. Knepley }
3604bbf5ea8SMatthew G. Knepley 
3614bbf5ea8SMatthew G. Knepley /* TODO: Docs */
36261c4b389SFlorian Wechsung PetscErrorCode PCPatchSetLocalComposition(PC pc, PCCompositeType type)
363c2e6f3c0SFlorian Wechsung {
364c2e6f3c0SFlorian Wechsung   PC_PATCH *patch = (PC_PATCH *) pc->data;
365c2e6f3c0SFlorian Wechsung   PetscFunctionBegin;
36661c4b389SFlorian 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");
36761c4b389SFlorian Wechsung   patch->local_composition_type = type;
368c2e6f3c0SFlorian Wechsung   PetscFunctionReturn(0);
369c2e6f3c0SFlorian Wechsung }
370c2e6f3c0SFlorian Wechsung 
371c2e6f3c0SFlorian Wechsung /* TODO: Docs */
37261c4b389SFlorian Wechsung PetscErrorCode PCPatchGetLocalComposition(PC pc, PCCompositeType *type)
373c2e6f3c0SFlorian Wechsung {
374c2e6f3c0SFlorian Wechsung   PC_PATCH *patch = (PC_PATCH *) pc->data;
375c2e6f3c0SFlorian Wechsung   PetscFunctionBegin;
37661c4b389SFlorian Wechsung   *type = patch->local_composition_type;
377c2e6f3c0SFlorian Wechsung   PetscFunctionReturn(0);
378c2e6f3c0SFlorian Wechsung }
379c2e6f3c0SFlorian Wechsung 
380c2e6f3c0SFlorian Wechsung /* TODO: Docs */
3814bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetSubMatType(PC pc, MatType sub_mat_type)
3824bbf5ea8SMatthew G. Knepley {
3834bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
3844bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
3854bbf5ea8SMatthew G. Knepley 
3864bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3874bbf5ea8SMatthew G. Knepley   if (patch->sub_mat_type) {ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr);}
3884bbf5ea8SMatthew G. Knepley   ierr = PetscStrallocpy(sub_mat_type, (char **) &patch->sub_mat_type);CHKERRQ(ierr);
3894bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3904bbf5ea8SMatthew G. Knepley }
3914bbf5ea8SMatthew G. Knepley 
3924bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3934bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetSubMatType(PC pc, MatType *sub_mat_type)
3944bbf5ea8SMatthew G. Knepley {
3954bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3964bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3974bbf5ea8SMatthew G. Knepley   *sub_mat_type = patch->sub_mat_type;
3984bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3994bbf5ea8SMatthew G. Knepley }
4004bbf5ea8SMatthew G. Knepley 
4014bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4024bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetCellNumbering(PC pc, PetscSection cellNumbering)
4034bbf5ea8SMatthew G. Knepley {
4044bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
4054bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
4064bbf5ea8SMatthew G. Knepley 
4074bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4084bbf5ea8SMatthew G. Knepley   patch->cellNumbering = cellNumbering;
4094bbf5ea8SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) cellNumbering);CHKERRQ(ierr);
4104bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4114bbf5ea8SMatthew G. Knepley }
4124bbf5ea8SMatthew G. Knepley 
4134bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4144bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetCellNumbering(PC pc, PetscSection *cellNumbering)
4154bbf5ea8SMatthew G. Knepley {
4164bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4174bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4184bbf5ea8SMatthew G. Knepley   *cellNumbering = patch->cellNumbering;
4194bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4204bbf5ea8SMatthew G. Knepley }
4214bbf5ea8SMatthew G. Knepley 
4224bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4234bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetConstructType(PC pc, PCPatchConstructType ctype, PetscErrorCode (*func)(PC, PetscInt *, IS **, IS *, void *), void *ctx)
4244bbf5ea8SMatthew G. Knepley {
4254bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4264bbf5ea8SMatthew G. Knepley 
4274bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4284bbf5ea8SMatthew G. Knepley   patch->ctype = ctype;
4294bbf5ea8SMatthew G. Knepley   switch (ctype) {
4304bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
43140c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
4324bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Star;
4334bbf5ea8SMatthew G. Knepley     break;
4344bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
43540c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
4364bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Vanka;
4374bbf5ea8SMatthew G. Knepley     break;
438e5b9877fSPatrick Farrell   case PC_PATCH_PARDECOMP:
4390a390943SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
440e5b9877fSPatrick Farrell     patch->patchconstructop = PCPatchConstruct_Pardecomp;
4410a390943SPatrick Farrell     break;
4424bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
4434bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
4444bbf5ea8SMatthew G. Knepley     patch->user_patches     = PETSC_TRUE;
4454bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_User;
446bdd9e0cdSPatrick Farrell     if (func) {
4474bbf5ea8SMatthew G. Knepley       patch->userpatchconstructionop = func;
4484bbf5ea8SMatthew G. Knepley       patch->userpatchconstructctx   = ctx;
449bdd9e0cdSPatrick Farrell     }
4504bbf5ea8SMatthew G. Knepley     break;
4514bbf5ea8SMatthew G. Knepley   default:
4524bbf5ea8SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype);
4534bbf5ea8SMatthew G. Knepley   }
4544bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4554bbf5ea8SMatthew G. Knepley }
4564bbf5ea8SMatthew G. Knepley 
4574bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4584bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetConstructType(PC pc, PCPatchConstructType *ctype, PetscErrorCode (**func)(PC, PetscInt *, IS **, IS *, void *), void **ctx)
4594bbf5ea8SMatthew G. Knepley {
4604bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4614bbf5ea8SMatthew G. Knepley 
4624bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4634bbf5ea8SMatthew G. Knepley   *ctype = patch->ctype;
4644bbf5ea8SMatthew G. Knepley   switch (patch->ctype) {
4654bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
4664bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
467e5b9877fSPatrick Farrell   case PC_PATCH_PARDECOMP:
4684bbf5ea8SMatthew G. Knepley     break;
4694bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
4704bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
4714bbf5ea8SMatthew G. Knepley     *func = patch->userpatchconstructionop;
4724bbf5ea8SMatthew G. Knepley     *ctx  = patch->userpatchconstructctx;
4734bbf5ea8SMatthew G. Knepley     break;
4744bbf5ea8SMatthew G. Knepley   default:
4754bbf5ea8SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype);
4764bbf5ea8SMatthew G. Knepley   }
4774bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4784bbf5ea8SMatthew G. Knepley }
4794bbf5ea8SMatthew G. Knepley 
4804bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4814bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfo(PC pc, PetscInt nsubspaces, DM *dms, PetscInt *bs, PetscInt *nodesPerCell, const PetscInt **cellNodeMap,
4824bbf5ea8SMatthew G. Knepley                                             const PetscInt *subspaceOffsets, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
4834bbf5ea8SMatthew G. Knepley {
4844bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
4855f824522SMatthew G. Knepley   DM             dm;
4864bbf5ea8SMatthew G. Knepley   PetscSF       *sfs;
4875f824522SMatthew G. Knepley   PetscInt       cStart, cEnd, i, j;
4884bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
4894bbf5ea8SMatthew G. Knepley 
4904bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4915f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
4925f824522SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
4934bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &sfs);CHKERRQ(ierr);
4944bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->dofSection);CHKERRQ(ierr);
4954bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->bs);CHKERRQ(ierr);
4964bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->nodesPerCell);CHKERRQ(ierr);
4974bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->cellNodeMap);CHKERRQ(ierr);
4984bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces+1, &patch->subspaceOffsets);CHKERRQ(ierr);
4994bbf5ea8SMatthew G. Knepley 
5004bbf5ea8SMatthew G. Knepley   patch->nsubspaces       = nsubspaces;
5014bbf5ea8SMatthew G. Knepley   patch->totalDofsPerCell = 0;
5024bbf5ea8SMatthew G. Knepley   for (i = 0; i < nsubspaces; ++i) {
5034bbf5ea8SMatthew G. Knepley     ierr = DMGetDefaultSection(dms[i], &patch->dofSection[i]);CHKERRQ(ierr);
5044bbf5ea8SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) patch->dofSection[i]);CHKERRQ(ierr);
5054bbf5ea8SMatthew G. Knepley     ierr = DMGetDefaultSF(dms[i], &sfs[i]);CHKERRQ(ierr);
5064bbf5ea8SMatthew G. Knepley     patch->bs[i]              = bs[i];
5074bbf5ea8SMatthew G. Knepley     patch->nodesPerCell[i]    = nodesPerCell[i];
5084bbf5ea8SMatthew G. Knepley     patch->totalDofsPerCell  += nodesPerCell[i]*bs[i];
50980e8a965SFlorian Wechsung     ierr = PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i]);CHKERRQ(ierr);
51080e8a965SFlorian Wechsung     for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
5114bbf5ea8SMatthew G. Knepley     patch->subspaceOffsets[i] = subspaceOffsets[i];
5124bbf5ea8SMatthew G. Knepley   }
5134bbf5ea8SMatthew G. Knepley   ierr = PCPatchCreateDefaultSF_Private(pc, nsubspaces, sfs, patch->bs);CHKERRQ(ierr);
5144bbf5ea8SMatthew G. Knepley   ierr = PetscFree(sfs);CHKERRQ(ierr);
5154bbf5ea8SMatthew G. Knepley 
5164bbf5ea8SMatthew G. Knepley   patch->subspaceOffsets[nsubspaces] = subspaceOffsets[nsubspaces];
5174bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes);CHKERRQ(ierr);
5184bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes);CHKERRQ(ierr);
5194bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5204bbf5ea8SMatthew G. Knepley }
5214bbf5ea8SMatthew G. Knepley 
5224bbf5ea8SMatthew G. Knepley /* TODO: Docs */
5235f824522SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfoCombined(PC pc, DM dm, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
5245f824522SMatthew G. Knepley {
5255f824522SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
5265f824522SMatthew G. Knepley   PetscInt       cStart, cEnd, i, j;
5275f824522SMatthew G. Knepley   PetscErrorCode ierr;
5285f824522SMatthew G. Knepley 
5295f824522SMatthew G. Knepley   PetscFunctionBegin;
5305f824522SMatthew G. Knepley   patch->combined = PETSC_TRUE;
5315f824522SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
5325f824522SMatthew G. Knepley   ierr = DMGetNumFields(dm, &patch->nsubspaces);CHKERRQ(ierr);
5335f824522SMatthew G. Knepley   ierr = PetscCalloc1(patch->nsubspaces, &patch->dofSection);CHKERRQ(ierr);
5345f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->bs);CHKERRQ(ierr);
5355f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->nodesPerCell);CHKERRQ(ierr);
5365f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->cellNodeMap);CHKERRQ(ierr);
5375f824522SMatthew G. Knepley   ierr = PetscCalloc1(patch->nsubspaces+1, &patch->subspaceOffsets);CHKERRQ(ierr);
5385f824522SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &patch->dofSection[0]);CHKERRQ(ierr);
5395f824522SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) patch->dofSection[0]);CHKERRQ(ierr);
5405f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(patch->dofSection[0], &patch->subspaceOffsets[patch->nsubspaces]);CHKERRQ(ierr);
5415f824522SMatthew G. Knepley   patch->totalDofsPerCell = 0;
5425f824522SMatthew G. Knepley   for (i = 0; i < patch->nsubspaces; ++i) {
5435f824522SMatthew G. Knepley     patch->bs[i]             = 1;
5445f824522SMatthew G. Knepley     patch->nodesPerCell[i]   = nodesPerCell[i];
5455f824522SMatthew G. Knepley     patch->totalDofsPerCell += nodesPerCell[i];
5465f824522SMatthew G. Knepley     ierr = PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i]);CHKERRQ(ierr);
5475f824522SMatthew G. Knepley     for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
5485f824522SMatthew G. Knepley   }
5495f824522SMatthew G. Knepley   ierr = DMGetDefaultSF(dm, &patch->defaultSF);CHKERRQ(ierr);
5505f824522SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) patch->defaultSF);CHKERRQ(ierr);
5515f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes);CHKERRQ(ierr);
5525f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes);CHKERRQ(ierr);
5535f824522SMatthew G. Knepley   PetscFunctionReturn(0);
5545f824522SMatthew G. Knepley }
5555f824522SMatthew G. Knepley 
5565f824522SMatthew G. Knepley /*@C
5575f824522SMatthew G. Knepley 
55892d50984SMatthew G. Knepley   PCPatchSetComputeFunction - Set the callback used to compute patch residuals
55992d50984SMatthew G. Knepley 
56092d50984SMatthew G. Knepley   Input Parameters:
56192d50984SMatthew G. Knepley + pc   - The PC
56292d50984SMatthew G. Knepley . func - The callback
56392d50984SMatthew G. Knepley - ctx  - The user context
56492d50984SMatthew G. Knepley 
56592d50984SMatthew G. Knepley   Level: advanced
56692d50984SMatthew G. Knepley 
56792d50984SMatthew G. Knepley   Note:
56892d50984SMatthew G. Knepley   The callback has signature:
56992d50984SMatthew G. Knepley +  usercomputef(pc, point, x, f, cellIS, n, u, ctx)
57092d50984SMatthew G. Knepley +  pc     - The PC
57192d50984SMatthew G. Knepley +  point  - The point
57292d50984SMatthew G. Knepley +  x      - The input solution (not used in linear problems)
57392d50984SMatthew G. Knepley +  f      - The patch residual vector
57492d50984SMatthew G. Knepley +  cellIS - An array of the cell numbers
57592d50984SMatthew G. Knepley +  n      - The size of g2l
57692d50984SMatthew G. Knepley +  g2l    - The global to local dof translation table
57792d50984SMatthew G. Knepley +  ctx    - The user context
57892d50984SMatthew G. Knepley   and can assume that the matrix entries have been set to zero before the call.
57992d50984SMatthew G. Knepley 
58092d50984SMatthew G. Knepley .seealso: PCPatchSetComputeOperator(), PCPatchGetComputeOperator(), PCPatchSetDiscretisationInfo()
58192d50984SMatthew G. Knepley @*/
58239fd2e8aSPatrick Farrell PetscErrorCode PCPatchSetComputeFunction(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Vec, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
58392d50984SMatthew G. Knepley {
58492d50984SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
58592d50984SMatthew G. Knepley 
58692d50984SMatthew G. Knepley   PetscFunctionBegin;
58792d50984SMatthew G. Knepley   patch->usercomputef    = func;
58892d50984SMatthew G. Knepley   patch->usercomputefctx = ctx;
58992d50984SMatthew G. Knepley   PetscFunctionReturn(0);
59092d50984SMatthew G. Knepley }
59192d50984SMatthew G. Knepley 
59292d50984SMatthew G. Knepley /*@C
59392d50984SMatthew G. Knepley 
59459109abcSLawrence Mitchell   PCPatchSetComputeFunctionInteriorFacets - Set the callback used to compute facet integrals for patch residuals
59559109abcSLawrence Mitchell 
59659109abcSLawrence Mitchell   Input Parameters:
59759109abcSLawrence Mitchell + pc   - The PC
59859109abcSLawrence Mitchell . func - The callback
59959109abcSLawrence Mitchell - ctx  - The user context
60059109abcSLawrence Mitchell 
60159109abcSLawrence Mitchell   Level: advanced
60259109abcSLawrence Mitchell 
60359109abcSLawrence Mitchell   Note:
60459109abcSLawrence Mitchell   The callback has signature:
60559109abcSLawrence Mitchell +  usercomputef(pc, point, x, f, facetIS, n, u, ctx)
60659109abcSLawrence Mitchell +  pc     - The PC
60759109abcSLawrence Mitchell +  point  - The point
60859109abcSLawrence Mitchell +  x      - The input solution (not used in linear problems)
60959109abcSLawrence Mitchell +  f      - The patch residual vector
61059109abcSLawrence Mitchell +  facetIS - An array of the facet numbers
61159109abcSLawrence Mitchell +  n      - The size of g2l
61259109abcSLawrence Mitchell +  g2l    - The global to local dof translation table
61359109abcSLawrence Mitchell +  ctx    - The user context
61459109abcSLawrence Mitchell   and can assume that the matrix entries have been set to zero before the call.
61559109abcSLawrence Mitchell 
61659109abcSLawrence Mitchell .seealso: PCPatchSetComputeOperator(), PCPatchGetComputeOperator(), PCPatchSetDiscretisationInfo()
61759109abcSLawrence Mitchell @*/
61859109abcSLawrence Mitchell PetscErrorCode PCPatchSetComputeFunctionInteriorFacets(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Vec, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
61959109abcSLawrence Mitchell {
62059109abcSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *) pc->data;
62159109abcSLawrence Mitchell 
62259109abcSLawrence Mitchell   PetscFunctionBegin;
62359109abcSLawrence Mitchell   patch->usercomputefintfacet    = func;
62459109abcSLawrence Mitchell   patch->usercomputefintfacetctx = ctx;
62559109abcSLawrence Mitchell   PetscFunctionReturn(0);
62659109abcSLawrence Mitchell }
62759109abcSLawrence Mitchell 
62859109abcSLawrence Mitchell /*@C
62959109abcSLawrence Mitchell 
6305f824522SMatthew G. Knepley   PCPatchSetComputeOperator - Set the callback used to compute patch matrices
6315f824522SMatthew G. Knepley 
6325f824522SMatthew G. Knepley   Input Parameters:
6335f824522SMatthew G. Knepley + pc   - The PC
6345f824522SMatthew G. Knepley . func - The callback
6355f824522SMatthew G. Knepley - ctx  - The user context
6365f824522SMatthew G. Knepley 
6375f824522SMatthew G. Knepley   Level: advanced
6385f824522SMatthew G. Knepley 
6395f824522SMatthew G. Knepley   Note:
6405f824522SMatthew G. Knepley   The callback has signature:
641723f9013SMatthew G. Knepley +  usercomputeop(pc, point, x, mat, cellIS, n, u, ctx)
6425f824522SMatthew G. Knepley +  pc     - The PC
643bdd9e0cdSPatrick Farrell +  point  - The point
644723f9013SMatthew G. Knepley +  x      - The input solution (not used in linear problems)
6455f824522SMatthew G. Knepley +  mat    - The patch matrix
6466f158342SMatthew G. Knepley +  cellIS - An array of the cell numbers
6475f824522SMatthew G. Knepley +  n      - The size of g2l
6485f824522SMatthew G. Knepley +  g2l    - The global to local dof translation table
6495f824522SMatthew G. Knepley +  ctx    - The user context
6505f824522SMatthew G. Knepley   and can assume that the matrix entries have been set to zero before the call.
6515f824522SMatthew G. Knepley 
652723f9013SMatthew G. Knepley .seealso: PCPatchGetComputeOperator(), PCPatchSetComputeFunction(), PCPatchSetDiscretisationInfo()
6535f824522SMatthew G. Knepley @*/
6544d04e9f1SPatrick Farrell PetscErrorCode PCPatchSetComputeOperator(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Mat, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
6554bbf5ea8SMatthew G. Knepley {
6564bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
6574bbf5ea8SMatthew G. Knepley 
6584bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
6594bbf5ea8SMatthew G. Knepley   patch->usercomputeop    = func;
660723f9013SMatthew G. Knepley   patch->usercomputeopctx = ctx;
6614bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
6624bbf5ea8SMatthew G. Knepley }
6634bbf5ea8SMatthew G. Knepley 
66459109abcSLawrence Mitchell /*@C
66559109abcSLawrence Mitchell 
66659109abcSLawrence Mitchell   PCPatchSetComputeOperator - Set the callback used to compute facet integrals for patch matrices
66759109abcSLawrence Mitchell 
66859109abcSLawrence Mitchell   Input Parameters:
66959109abcSLawrence Mitchell + pc   - The PC
67059109abcSLawrence Mitchell . func - The callback
67159109abcSLawrence Mitchell - ctx  - The user context
67259109abcSLawrence Mitchell 
67359109abcSLawrence Mitchell   Level: advanced
67459109abcSLawrence Mitchell 
67559109abcSLawrence Mitchell   Note:
67659109abcSLawrence Mitchell   The callback has signature:
67759109abcSLawrence Mitchell +  usercomputeopintfacet(pc, point, x, mat, facetIS, n, u, ctx)
67859109abcSLawrence Mitchell +  pc     - The PC
67959109abcSLawrence Mitchell +  point  - The point
68059109abcSLawrence Mitchell +  x      - The input solution (not used in linear problems)
68159109abcSLawrence Mitchell +  mat    - The patch matrix
68259109abcSLawrence Mitchell +  facetIS - An IS of the facet numbers
68359109abcSLawrence Mitchell +  n      - The size of g2l
68459109abcSLawrence Mitchell +  g2l    - The global to local dof translation table
68559109abcSLawrence Mitchell +  ctx    - The user context
68659109abcSLawrence Mitchell   and can assume that the matrix entries have been set to zero before the call.
68759109abcSLawrence Mitchell 
68859109abcSLawrence Mitchell .seealso: PCPatchGetComputeOperator(), PCPatchSetComputeFunction(), PCPatchSetDiscretisationInfo()
68959109abcSLawrence Mitchell @*/
69059109abcSLawrence Mitchell PetscErrorCode PCPatchSetComputeOperatorInteriorFacets(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Mat, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
69159109abcSLawrence Mitchell {
69259109abcSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *) pc->data;
69359109abcSLawrence Mitchell 
69459109abcSLawrence Mitchell   PetscFunctionBegin;
69559109abcSLawrence Mitchell   patch->usercomputeopintfacet    = func;
69659109abcSLawrence Mitchell   patch->usercomputeopintfacetctx = ctx;
69759109abcSLawrence Mitchell   PetscFunctionReturn(0);
69859109abcSLawrence Mitchell }
69959109abcSLawrence Mitchell 
7004bbf5ea8SMatthew G. Knepley /* On entry, ht contains the topological entities whose dofs we are responsible for solving for;
7014bbf5ea8SMatthew G. Knepley    on exit, cht contains all the topological entities we need to compute their residuals.
7024bbf5ea8SMatthew G. Knepley    In full generality this should incorporate knowledge of the sparsity pattern of the matrix;
7034bbf5ea8SMatthew G. Knepley    here we assume a standard FE sparsity pattern.*/
7044bbf5ea8SMatthew G. Knepley /* TODO: Use DMPlexGetAdjacency() */
7051b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchCompleteCellPatch(PC pc, PetscHSetI ht, PetscHSetI cht)
7064bbf5ea8SMatthew G. Knepley {
7075f824522SMatthew G. Knepley   DM             dm;
708*bc7fa33aSFlorian Wechsung   PC_PATCH      *patch = (PC_PATCH *) pc->data;
7091b68eb51SMatthew G. Knepley   PetscHashIter  hi;
7104bbf5ea8SMatthew G. Knepley   PetscInt       point;
7114bbf5ea8SMatthew G. Knepley   PetscInt      *star = NULL, *closure = NULL;
7124c954380SMatthew G. Knepley   PetscInt       ignoredim, iStart = 0, iEnd = -1, starSize, closureSize, si, ci;
713*bc7fa33aSFlorian Wechsung   PetscInt      *fStar = NULL, *fClosure = NULL;
714*bc7fa33aSFlorian Wechsung   PetscInt       fBegin, fEnd, fsi, fci, fStarSize, fClosureSize;
7154bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
7164bbf5ea8SMatthew G. Knepley 
7174bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
7185f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
719*bc7fa33aSFlorian Wechsung   ierr = DMPlexGetHeightStratum(dm, 1, &fBegin, &fEnd);CHKERRQ(ierr);
7205f824522SMatthew G. Knepley   ierr = PCPatchGetIgnoreDim(pc, &ignoredim);CHKERRQ(ierr);
7215f824522SMatthew G. Knepley   if (ignoredim >= 0) {ierr = DMPlexGetDepthStratum(dm, ignoredim, &iStart, &iEnd);CHKERRQ(ierr);}
7221b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(cht);CHKERRQ(ierr);
7231b68eb51SMatthew G. Knepley   PetscHashIterBegin(ht, hi);
7241b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(ht, hi)) {
7254c954380SMatthew G. Knepley 
7261b68eb51SMatthew G. Knepley     PetscHashIterGetKey(ht, hi, point);
7271b68eb51SMatthew G. Knepley     PetscHashIterNext(ht, hi);
7284bbf5ea8SMatthew G. Knepley 
7294bbf5ea8SMatthew G. Knepley     /* Loop over all the cells that this point connects to */
7304bbf5ea8SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
7315f824522SMatthew G. Knepley     for (si = 0; si < starSize*2; si += 2) {
7324c954380SMatthew G. Knepley       const PetscInt ownedpoint = star[si];
7335f824522SMatthew G. Knepley       /* TODO Check for point in cht before running through closure again */
7344bbf5ea8SMatthew G. Knepley       /* now loop over all entities in the closure of that cell */
7354bbf5ea8SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, ownedpoint, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
7365f824522SMatthew G. Knepley       for (ci = 0; ci < closureSize*2; ci += 2) {
7374c954380SMatthew G. Knepley         const PetscInt seenpoint = closure[ci];
7385f824522SMatthew G. Knepley         if (ignoredim >= 0 && seenpoint >= iStart && seenpoint < iEnd) continue;
7391b68eb51SMatthew G. Knepley         ierr = PetscHSetIAdd(cht, seenpoint);CHKERRQ(ierr);
740*bc7fa33aSFlorian Wechsung         /* Facet integrals couple dofs across facets, so in that case for each of
741*bc7fa33aSFlorian Wechsung          * the facets we need to add all dofs on the other side of the facet to
742*bc7fa33aSFlorian Wechsung          * the seen dofs. */
743*bc7fa33aSFlorian Wechsung         if(patch->usercomputeopintfacet){
744*bc7fa33aSFlorian Wechsung           if(fBegin <= seenpoint && seenpoint < fEnd){
745*bc7fa33aSFlorian Wechsung             ierr = DMPlexGetTransitiveClosure(dm, seenpoint, PETSC_FALSE, &fStarSize, &fStar);CHKERRQ(ierr);
746*bc7fa33aSFlorian Wechsung             for (fsi = 0; fsi < fStarSize*2; fsi += 2) {
747*bc7fa33aSFlorian Wechsung               ierr = DMPlexGetTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, &fClosureSize, &fClosure);CHKERRQ(ierr);
748*bc7fa33aSFlorian Wechsung               for (fci = 0; fci < fClosureSize*2; fci += 2) {
749*bc7fa33aSFlorian Wechsung                 ierr = PetscHSetIAdd(cht, fClosure[fci]);CHKERRQ(ierr);
750*bc7fa33aSFlorian Wechsung               }
751*bc7fa33aSFlorian Wechsung               ierr = DMPlexRestoreTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, NULL, &fClosure);CHKERRQ(ierr);
752*bc7fa33aSFlorian Wechsung             }
753*bc7fa33aSFlorian Wechsung             ierr = DMPlexRestoreTransitiveClosure(dm, seenpoint, PETSC_FALSE, NULL, &fStar);CHKERRQ(ierr);
754*bc7fa33aSFlorian Wechsung           }
755*bc7fa33aSFlorian Wechsung         }
7564bbf5ea8SMatthew G. Knepley       }
7572d76c0eeSPatrick Farrell       ierr = DMPlexRestoreTransitiveClosure(dm, ownedpoint, PETSC_TRUE, NULL, &closure);CHKERRQ(ierr);
7584bbf5ea8SMatthew G. Knepley     }
7592d76c0eeSPatrick Farrell     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, NULL, &star);CHKERRQ(ierr);
7604bbf5ea8SMatthew G. Knepley   }
7615f824522SMatthew G. Knepley   PetscFunctionReturn(0);
7625f824522SMatthew G. Knepley }
7635f824522SMatthew G. Knepley 
7645f824522SMatthew G. Knepley static PetscErrorCode PCPatchGetGlobalDofs(PC pc, PetscSection dofSection[], PetscInt f, PetscBool combined, PetscInt p, PetscInt *dof, PetscInt *off)
7655f824522SMatthew G. Knepley {
7665f824522SMatthew G. Knepley   PetscErrorCode ierr;
7675f824522SMatthew G. Knepley 
7685f824522SMatthew G. Knepley   PetscFunctionBegin;
7695f824522SMatthew G. Knepley   if (combined) {
7705f824522SMatthew G. Knepley     if (f < 0) {
7715f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetDof(dofSection[0], p, dof);CHKERRQ(ierr);}
7725f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetOffset(dofSection[0], p, off);CHKERRQ(ierr);}
7735f824522SMatthew G. Knepley     } else {
7745f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetFieldDof(dofSection[0], p, f, dof);CHKERRQ(ierr);}
7755f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetFieldOffset(dofSection[0], p, f, off);CHKERRQ(ierr);}
7765f824522SMatthew G. Knepley     }
7775f824522SMatthew G. Knepley   } else {
7785f824522SMatthew G. Knepley     if (f < 0) {
7795f824522SMatthew G. Knepley       PC_PATCH *patch = (PC_PATCH *) pc->data;
7805f824522SMatthew G. Knepley       PetscInt  fdof, g;
7815f824522SMatthew G. Knepley 
7825f824522SMatthew G. Knepley       if (dof) {
7835f824522SMatthew G. Knepley         *dof = 0;
7845f824522SMatthew G. Knepley         for (g = 0; g < patch->nsubspaces; ++g) {
7855f824522SMatthew G. Knepley           ierr = PetscSectionGetDof(dofSection[g], p, &fdof);CHKERRQ(ierr);
7865f824522SMatthew G. Knepley           *dof += fdof;
7875f824522SMatthew G. Knepley         }
7885f824522SMatthew G. Knepley       }
789624e31c3SLawrence Mitchell       if (off) {
790624e31c3SLawrence Mitchell         *off = 0;
791624e31c3SLawrence Mitchell         for (g = 0; g < patch->nsubspaces; ++g) {
792624e31c3SLawrence Mitchell           ierr = PetscSectionGetOffset(dofSection[g], p, &fdof);CHKERRQ(ierr);
793624e31c3SLawrence Mitchell           *off += fdof;
794624e31c3SLawrence Mitchell         }
795624e31c3SLawrence Mitchell       }
7965f824522SMatthew G. Knepley     } else {
7975f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetDof(dofSection[f], p, dof);CHKERRQ(ierr);}
7985f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetOffset(dofSection[f], p, off);CHKERRQ(ierr);}
7995f824522SMatthew G. Knepley     }
8005f824522SMatthew G. Knepley   }
8014bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
8024bbf5ea8SMatthew G. Knepley }
8034bbf5ea8SMatthew G. Knepley 
8044bbf5ea8SMatthew G. Knepley /* Given a hash table with a set of topological entities (pts), compute the degrees of
8054bbf5ea8SMatthew G. Knepley    freedom in global concatenated numbering on those entities.
8064bbf5ea8SMatthew G. Knepley    For Vanka smoothing, this needs to do something special: ignore dofs of the
8074bbf5ea8SMatthew G. Knepley    constraint subspace on entities that aren't the base entity we're building the patch
8084bbf5ea8SMatthew G. Knepley    around. */
809e4c66b91SPatrick Farrell static PetscErrorCode PCPatchGetPointDofs(PC pc, PetscHSetI pts, PetscHSetI dofs, PetscInt base, PetscHSetI* subspaces_to_exclude)
8104bbf5ea8SMatthew G. Knepley {
8115f824522SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
8121b68eb51SMatthew G. Knepley   PetscHashIter  hi;
8134bbf5ea8SMatthew G. Knepley   PetscInt       ldof, loff;
8144bbf5ea8SMatthew G. Knepley   PetscInt       k, p;
8154bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
8164bbf5ea8SMatthew G. Knepley 
8174bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
8181b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(dofs);CHKERRQ(ierr);
8194bbf5ea8SMatthew G. Knepley   for (k = 0; k < patch->nsubspaces; ++k) {
8204bbf5ea8SMatthew G. Knepley     PetscInt subspaceOffset = patch->subspaceOffsets[k];
8214bbf5ea8SMatthew G. Knepley     PetscInt bs             = patch->bs[k];
8224bbf5ea8SMatthew G. Knepley     PetscInt j, l;
8234bbf5ea8SMatthew G. Knepley 
824e4c66b91SPatrick Farrell     if (subspaces_to_exclude != NULL) {
825e4c66b91SPatrick Farrell       PetscBool should_exclude_k = PETSC_FALSE;
826e4c66b91SPatrick Farrell       PetscHSetIHas(*subspaces_to_exclude, k, &should_exclude_k);
827e4c66b91SPatrick Farrell       if (should_exclude_k) {
8284bbf5ea8SMatthew G. Knepley         /* only get this subspace dofs at the base entity, not any others */
8295f824522SMatthew G. Knepley         ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, base, &ldof, &loff);CHKERRQ(ierr);
8304bbf5ea8SMatthew G. Knepley         if (0 == ldof) continue;
8314bbf5ea8SMatthew G. Knepley         for (j = loff; j < ldof + loff; ++j) {
8324bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
8334bbf5ea8SMatthew G. Knepley             PetscInt dof = bs*j + l + subspaceOffset;
8341b68eb51SMatthew G. Knepley             ierr = PetscHSetIAdd(dofs, dof);CHKERRQ(ierr);
8354bbf5ea8SMatthew G. Knepley           }
8364bbf5ea8SMatthew G. Knepley         }
8374bbf5ea8SMatthew G. Knepley         continue; /* skip the other dofs of this subspace */
8384bbf5ea8SMatthew G. Knepley       }
839e4c66b91SPatrick Farrell     }
8404bbf5ea8SMatthew G. Knepley 
8411b68eb51SMatthew G. Knepley     PetscHashIterBegin(pts, hi);
8421b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(pts, hi)) {
8431b68eb51SMatthew G. Knepley       PetscHashIterGetKey(pts, hi, p);
8441b68eb51SMatthew G. Knepley       PetscHashIterNext(pts, hi);
8455f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, p, &ldof, &loff);CHKERRQ(ierr);
8464bbf5ea8SMatthew G. Knepley       if (0 == ldof) continue;
8474bbf5ea8SMatthew G. Knepley       for (j = loff; j < ldof + loff; ++j) {
8484bbf5ea8SMatthew G. Knepley         for (l = 0; l < bs; ++l) {
8494bbf5ea8SMatthew G. Knepley           PetscInt dof = bs*j + l + subspaceOffset;
8501b68eb51SMatthew G. Knepley           ierr = PetscHSetIAdd(dofs, dof);CHKERRQ(ierr);
8514bbf5ea8SMatthew G. Knepley         }
8524bbf5ea8SMatthew G. Knepley       }
8534bbf5ea8SMatthew G. Knepley     }
8544bbf5ea8SMatthew G. Knepley   }
8554bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
8564bbf5ea8SMatthew G. Knepley }
8574bbf5ea8SMatthew G. Knepley 
8584bbf5ea8SMatthew G. Knepley /* Given two hash tables A and B, compute the keys in B that are not in A, and put them in C */
8591b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchComputeSetDifference_Private(PetscHSetI A, PetscHSetI B, PetscHSetI C)
8604bbf5ea8SMatthew G. Knepley {
8611b68eb51SMatthew G. Knepley   PetscHashIter  hi;
8621b68eb51SMatthew G. Knepley   PetscInt       key;
8634bbf5ea8SMatthew G. Knepley   PetscBool      flg;
8641b68eb51SMatthew G. Knepley   PetscErrorCode ierr;
8654bbf5ea8SMatthew G. Knepley 
8664bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
8671b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(C);CHKERRQ(ierr);
8681b68eb51SMatthew G. Knepley   PetscHashIterBegin(B, hi);
8691b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(B, hi)) {
8701b68eb51SMatthew G. Knepley     PetscHashIterGetKey(B, hi, key);
8711b68eb51SMatthew G. Knepley     PetscHashIterNext(B, hi);
8721b68eb51SMatthew G. Knepley     ierr = PetscHSetIHas(A, key, &flg);CHKERRQ(ierr);
8731b68eb51SMatthew G. Knepley     if (!flg) {ierr = PetscHSetIAdd(C, key);CHKERRQ(ierr);}
8744bbf5ea8SMatthew G. Knepley   }
8754bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
8764bbf5ea8SMatthew G. Knepley }
8774bbf5ea8SMatthew G. Knepley 
8784bbf5ea8SMatthew G. Knepley /*
8794bbf5ea8SMatthew G. Knepley  * PCPatchCreateCellPatches - create patches.
8804bbf5ea8SMatthew G. Knepley  *
8814bbf5ea8SMatthew G. Knepley  * Input Parameters:
8824bbf5ea8SMatthew G. Knepley  * + dm - The DMPlex object defining the mesh
8834bbf5ea8SMatthew G. Knepley  *
8844bbf5ea8SMatthew G. Knepley  * Output Parameters:
8854bbf5ea8SMatthew G. Knepley  * + cellCounts  - Section with counts of cells around each vertex
8865f824522SMatthew G. Knepley  * . cells       - IS of the cell point indices of cells in each patch
8875f824522SMatthew G. Knepley  * . pointCounts - Section with counts of cells around each vertex
8885f824522SMatthew G. Knepley  * - point       - IS of the cell point indices of cells in each patch
8894bbf5ea8SMatthew G. Knepley  */
8904bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatches(PC pc)
8914bbf5ea8SMatthew G. Knepley {
8924bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
8935f824522SMatthew G. Knepley   DMLabel         ghost = NULL;
8944bbf5ea8SMatthew G. Knepley   DM              dm, plex;
8951b68eb51SMatthew G. Knepley   PetscHSetI      ht, cht;
8960e126c0bSLawrence Mitchell   PetscSection    cellCounts,  pointCounts, intFacetCounts, extFacetCounts;
897eb62eeaaSLawrence Mitchell   PetscInt       *cellsArray, *pointsArray, *intFacetsArray, *extFacetsArray, *intFacetsToPatchCell;
8980e126c0bSLawrence Mitchell   PetscInt        numCells, numPoints, numIntFacets, numExtFacets;
8995f824522SMatthew G. Knepley   const PetscInt *leaves;
9005f824522SMatthew G. Knepley   PetscInt        nleaves, pStart, pEnd, cStart, cEnd, vStart, vEnd, v;
9015f824522SMatthew G. Knepley   PetscBool       isFiredrake;
9024bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
9034bbf5ea8SMatthew G. Knepley 
9044bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
9054bbf5ea8SMatthew G. Knepley   /* Used to keep track of the cells in the patch. */
9061b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&ht);CHKERRQ(ierr);
9071b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&cht);CHKERRQ(ierr);
9084bbf5ea8SMatthew G. Knepley 
9094bbf5ea8SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
9104bbf5ea8SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONGSTATE, "DM not yet set on patch PC\n");
9114bbf5ea8SMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
9124bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetChart(plex, &pStart, &pEnd);CHKERRQ(ierr);
9134bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr);
9144bbf5ea8SMatthew G. Knepley 
9154bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
9165f824522SMatthew G. Knepley     ierr = patch->userpatchconstructionop(pc, &patch->npatch, &patch->userIS, &patch->iterationSet, patch->userpatchconstructctx);CHKERRQ(ierr);
9175f824522SMatthew G. Knepley     vStart = 0; vEnd = patch->npatch;
918e5b9877fSPatrick Farrell   } else if (patch->ctype == PC_PATCH_PARDECOMP) {
9190a390943SPatrick Farrell     vStart = 0; vEnd = 1;
9205f824522SMatthew G. Knepley   } else if (patch->codim < 0) {
9215f824522SMatthew G. Knepley     if (patch->dim < 0) {ierr = DMPlexGetDepthStratum(plex,  0,            &vStart, &vEnd);CHKERRQ(ierr);}
9225f824522SMatthew G. Knepley     else                {ierr = DMPlexGetDepthStratum(plex,  patch->dim,   &vStart, &vEnd);CHKERRQ(ierr);}
9235f824522SMatthew G. Knepley   } else                {ierr = DMPlexGetHeightStratum(plex, patch->codim, &vStart, &vEnd);CHKERRQ(ierr);}
9245f824522SMatthew G. Knepley   patch->npatch = vEnd - vStart;
9254bbf5ea8SMatthew G. Knepley 
9264bbf5ea8SMatthew G. Knepley   /* These labels mark the owned points.  We only create patches around points that this process owns. */
9275f824522SMatthew G. Knepley   ierr = DMHasLabel(dm, "pyop2_ghost", &isFiredrake);CHKERRQ(ierr);
9285f824522SMatthew G. Knepley   if (isFiredrake) {
9294bbf5ea8SMatthew G. Knepley     ierr = DMGetLabel(dm, "pyop2_ghost", &ghost);CHKERRQ(ierr);
9304bbf5ea8SMatthew G. Knepley     ierr = DMLabelCreateIndex(ghost, pStart, pEnd);CHKERRQ(ierr);
9315f824522SMatthew G. Knepley   } else {
9325f824522SMatthew G. Knepley     PetscSF sf;
9335f824522SMatthew G. Knepley 
9345f824522SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
9355f824522SMatthew G. Knepley     ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
9365f824522SMatthew G. Knepley     nleaves = PetscMax(nleaves, 0);
9375f824522SMatthew G. Knepley   }
9384bbf5ea8SMatthew G. Knepley 
9394bbf5ea8SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->cellCounts);CHKERRQ(ierr);
9405f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->cellCounts, "Patch Cell Layout");CHKERRQ(ierr);
9414bbf5ea8SMatthew G. Knepley   cellCounts = patch->cellCounts;
9424bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetChart(cellCounts, vStart, vEnd);CHKERRQ(ierr);
9435f824522SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->pointCounts);CHKERRQ(ierr);
9445f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->pointCounts, "Patch Point Layout");CHKERRQ(ierr);
9455f824522SMatthew G. Knepley   pointCounts = patch->pointCounts;
9465f824522SMatthew G. Knepley   ierr = PetscSectionSetChart(pointCounts, vStart, vEnd);CHKERRQ(ierr);
9470e126c0bSLawrence Mitchell   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->extFacetCounts);CHKERRQ(ierr);
9480e126c0bSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->extFacetCounts, "Patch Exterior Facet Layout");CHKERRQ(ierr);
9490e126c0bSLawrence Mitchell   extFacetCounts = patch->extFacetCounts;
9500e126c0bSLawrence Mitchell   ierr = PetscSectionSetChart(extFacetCounts, vStart, vEnd);CHKERRQ(ierr);
9510e126c0bSLawrence Mitchell   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->intFacetCounts);CHKERRQ(ierr);
9520e126c0bSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->intFacetCounts, "Patch Interior Facet Layout");CHKERRQ(ierr);
9530e126c0bSLawrence Mitchell   intFacetCounts = patch->intFacetCounts;
9540e126c0bSLawrence Mitchell   ierr = PetscSectionSetChart(intFacetCounts, vStart, vEnd);CHKERRQ(ierr);
9555f824522SMatthew G. Knepley   /* Count cells and points in the patch surrounding each entity */
9560e126c0bSLawrence Mitchell   PetscInt       fStart, fEnd;
9570e126c0bSLawrence Mitchell   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
9584bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
9591b68eb51SMatthew G. Knepley     PetscHashIter hi;
9605f824522SMatthew G. Knepley     PetscInt       chtSize, loc = -1;
9615f824522SMatthew G. Knepley     PetscBool      flg;
9624bbf5ea8SMatthew G. Knepley 
963b525f888SPatrick Farrell     if (!patch->user_patches && patch->ctype != PC_PATCH_PARDECOMP) {
9645f824522SMatthew G. Knepley       if (ghost) {ierr = DMLabelHasPoint(ghost, v, &flg);CHKERRQ(ierr);}
965928bb9adSStefano Zampini       else       {ierr = PetscFindInt(v, nleaves, leaves, &loc);CHKERRQ(ierr); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;}
9664bbf5ea8SMatthew G. Knepley       /* Not an owned entity, don't make a cell patch. */
9674bbf5ea8SMatthew G. Knepley       if (flg) continue;
9684bbf5ea8SMatthew G. Knepley     }
9694bbf5ea8SMatthew G. Knepley 
9704bbf5ea8SMatthew G. Knepley     ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr);
9715f824522SMatthew G. Knepley     ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr);
9721b68eb51SMatthew G. Knepley     ierr = PetscHSetIGetSize(cht, &chtSize);CHKERRQ(ierr);
9734bbf5ea8SMatthew G. Knepley     /* empty patch, continue */
9744bbf5ea8SMatthew G. Knepley     if (chtSize == 0) continue;
9754bbf5ea8SMatthew G. Knepley 
9764bbf5ea8SMatthew G. Knepley     /* safe because size(cht) > 0 from above */
9771b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
9781b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
9795f824522SMatthew G. Knepley       PetscInt point, pdof;
9804bbf5ea8SMatthew G. Knepley 
9811b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
9820e126c0bSLawrence Mitchell       if (fStart <= point && point < fEnd) {
9830e126c0bSLawrence Mitchell         const PetscInt *support;
9840e126c0bSLawrence Mitchell         PetscInt supportSize, p;
9850e126c0bSLawrence Mitchell         PetscBool interior = PETSC_TRUE;
9860e126c0bSLawrence Mitchell         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
9870e126c0bSLawrence Mitchell         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
9880e126c0bSLawrence Mitchell         if (supportSize == 1) {
9890e126c0bSLawrence Mitchell           interior = PETSC_FALSE;
9900e126c0bSLawrence Mitchell         } else {
9910e126c0bSLawrence Mitchell           for (p = 0; p < supportSize; p++) {
9920e126c0bSLawrence Mitchell             PetscBool found;
9930e126c0bSLawrence Mitchell             /* FIXME: can I do this while iterating over cht? */
9940e126c0bSLawrence Mitchell             PetscHSetIHas(cht, support[p], &found);
9950e126c0bSLawrence Mitchell             if (!found) {
9960e126c0bSLawrence Mitchell               interior = PETSC_FALSE;
9970e126c0bSLawrence Mitchell               break;
9980e126c0bSLawrence Mitchell             }
9990e126c0bSLawrence Mitchell           }
10000e126c0bSLawrence Mitchell         }
10010e126c0bSLawrence Mitchell         if (interior) {
10020e126c0bSLawrence Mitchell           ierr = PetscSectionAddDof(intFacetCounts, v, 1);CHKERRQ(ierr);
10030e126c0bSLawrence Mitchell         } else {
10040e126c0bSLawrence Mitchell           ierr = PetscSectionAddDof(extFacetCounts, v, 1);CHKERRQ(ierr);
10050e126c0bSLawrence Mitchell         }
10060e126c0bSLawrence Mitchell       }
10075f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr);
10085f824522SMatthew G. Knepley       if (pdof)                            {ierr = PetscSectionAddDof(pointCounts, v, 1);CHKERRQ(ierr);}
10095f824522SMatthew G. Knepley       if (point >= cStart && point < cEnd) {ierr = PetscSectionAddDof(cellCounts, v, 1);CHKERRQ(ierr);}
10101b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
10114bbf5ea8SMatthew G. Knepley     }
10124bbf5ea8SMatthew G. Knepley   }
10135f824522SMatthew G. Knepley   if (isFiredrake) {ierr = DMLabelDestroyIndex(ghost);CHKERRQ(ierr);}
10144bbf5ea8SMatthew G. Knepley 
10154bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetUp(cellCounts);CHKERRQ(ierr);
10164bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr);
10174bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numCells, &cellsArray);CHKERRQ(ierr);
10185f824522SMatthew G. Knepley   ierr = PetscSectionSetUp(pointCounts);CHKERRQ(ierr);
10195f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(pointCounts, &numPoints);CHKERRQ(ierr);
10205f824522SMatthew G. Knepley   ierr = PetscMalloc1(numPoints, &pointsArray);CHKERRQ(ierr);
10214bbf5ea8SMatthew G. Knepley 
10220e126c0bSLawrence Mitchell   ierr = PetscSectionSetUp(intFacetCounts);CHKERRQ(ierr);
10230e126c0bSLawrence Mitchell   ierr = PetscSectionSetUp(extFacetCounts);CHKERRQ(ierr);
10240e126c0bSLawrence Mitchell   ierr = PetscSectionGetStorageSize(intFacetCounts, &numIntFacets);CHKERRQ(ierr);
10250e126c0bSLawrence Mitchell   ierr = PetscSectionGetStorageSize(extFacetCounts, &numExtFacets);CHKERRQ(ierr);
10260e126c0bSLawrence Mitchell   ierr = PetscMalloc1(numIntFacets, &intFacetsArray);CHKERRQ(ierr);
1027eb62eeaaSLawrence Mitchell   ierr = PetscMalloc1(numIntFacets*2, &intFacetsToPatchCell);CHKERRQ(ierr);
10280e126c0bSLawrence Mitchell   ierr = PetscMalloc1(numExtFacets, &extFacetsArray);CHKERRQ(ierr);
10290e126c0bSLawrence Mitchell 
1030eb62eeaaSLawrence Mitchell 
10314bbf5ea8SMatthew G. Knepley   /* Now that we know how much space we need, run through again and actually remember the cells. */
10324bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; v++ ) {
10331b68eb51SMatthew G. Knepley     PetscHashIter hi;
10340e126c0bSLawrence Mitchell     PetscInt       dof, off, cdof, coff, efdof, efoff, ifdof, ifoff, pdof, n = 0, cn = 0, ifn = 0, efn = 0;
10354bbf5ea8SMatthew G. Knepley 
10365f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(pointCounts, v, &dof);CHKERRQ(ierr);
10375f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(pointCounts, v, &off);CHKERRQ(ierr);
10385f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &cdof);CHKERRQ(ierr);
10395f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &coff);CHKERRQ(ierr);
10400e126c0bSLawrence Mitchell     ierr = PetscSectionGetDof(intFacetCounts, v, &ifdof);CHKERRQ(ierr);
10410e126c0bSLawrence Mitchell     ierr = PetscSectionGetOffset(intFacetCounts, v, &ifoff);CHKERRQ(ierr);
10420e126c0bSLawrence Mitchell     ierr = PetscSectionGetDof(extFacetCounts, v, &efdof);CHKERRQ(ierr);
10430e126c0bSLawrence Mitchell     ierr = PetscSectionGetOffset(extFacetCounts, v, &efoff);CHKERRQ(ierr);
10445f824522SMatthew G. Knepley     if (dof <= 0) continue;
10454bbf5ea8SMatthew G. Knepley     ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr);
10465f824522SMatthew G. Knepley     ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr);
10471b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
10481b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
10494bbf5ea8SMatthew G. Knepley       PetscInt point;
10504bbf5ea8SMatthew G. Knepley 
10511b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
10520e126c0bSLawrence Mitchell       if (fStart <= point && point < fEnd) {
10530e126c0bSLawrence Mitchell         const PetscInt *support;
10540e126c0bSLawrence Mitchell         PetscInt supportSize, p;
10550e126c0bSLawrence Mitchell         PetscBool interior = PETSC_TRUE;
10560e126c0bSLawrence Mitchell         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
10570e126c0bSLawrence Mitchell         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
10580e126c0bSLawrence Mitchell         if (supportSize == 1) {
10590e126c0bSLawrence Mitchell           interior = PETSC_FALSE;
10600e126c0bSLawrence Mitchell         } else {
10610e126c0bSLawrence Mitchell           for (p = 0; p < supportSize; p++) {
10620e126c0bSLawrence Mitchell             PetscBool found;
10630e126c0bSLawrence Mitchell             /* FIXME: can I do this while iterating over cht? */
10640e126c0bSLawrence Mitchell             PetscHSetIHas(cht, support[p], &found);
10650e126c0bSLawrence Mitchell             if (!found) {
10660e126c0bSLawrence Mitchell               interior = PETSC_FALSE;
10670e126c0bSLawrence Mitchell               break;
10680e126c0bSLawrence Mitchell             }
10690e126c0bSLawrence Mitchell           }
10700e126c0bSLawrence Mitchell         }
10710e126c0bSLawrence Mitchell         if (interior) {
107244b625f7SLawrence Mitchell           intFacetsToPatchCell[2*(ifoff + ifn)] = support[0];
107344b625f7SLawrence Mitchell           intFacetsToPatchCell[2*(ifoff + ifn) + 1] = support[1];
10740e126c0bSLawrence Mitchell           intFacetsArray[ifoff + ifn++] = point;
10750e126c0bSLawrence Mitchell         } else {
10760e126c0bSLawrence Mitchell           extFacetsArray[efoff + efn++] = point;
10770e126c0bSLawrence Mitchell         }
10780e126c0bSLawrence Mitchell       }
10795f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr);
10805f824522SMatthew G. Knepley       if (pdof)                            {pointsArray[off + n++] = point;}
10815f824522SMatthew G. Knepley       if (point >= cStart && point < cEnd) {cellsArray[coff + cn++] = point;}
10821b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
10834bbf5ea8SMatthew G. Knepley     }
10840e126c0bSLawrence 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);
10850e126c0bSLawrence 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);
10865f824522SMatthew 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);
10875f824522SMatthew 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);
1088eb62eeaaSLawrence Mitchell 
1089eb62eeaaSLawrence Mitchell     for (ifn = 0; ifn < ifdof; ifn++) {
109044b625f7SLawrence Mitchell       PetscInt cell0 = intFacetsToPatchCell[2*(ifoff + ifn)];
109144b625f7SLawrence Mitchell       PetscInt cell1 = intFacetsToPatchCell[2*(ifoff + ifn) + 1];
1092eb62eeaaSLawrence Mitchell       PetscBool found0 = PETSC_FALSE, found1 = PETSC_FALSE;
1093eb62eeaaSLawrence Mitchell       for (n = 0; n < cdof; n++) {
10947c54fef0SLawrence Mitchell         if (!found0 && cell0 == cellsArray[coff + n]) {
1095c3faab33SLawrence Mitchell           intFacetsToPatchCell[2*(ifoff + ifn)] = n;
1096eb62eeaaSLawrence Mitchell           found0 = PETSC_TRUE;
1097eb62eeaaSLawrence Mitchell         }
10987c54fef0SLawrence Mitchell         if (!found1 && cell1 == cellsArray[coff + n]) {
1099c3faab33SLawrence Mitchell           intFacetsToPatchCell[2*(ifoff + ifn) + 1] = n;
110080fc4459SLawrence Mitchell           found1 = PETSC_TRUE;
1101eb62eeaaSLawrence Mitchell         }
1102eb62eeaaSLawrence Mitchell         if (found0 && found1) break;
1103eb62eeaaSLawrence Mitchell       }
110444b625f7SLawrence Mitchell       if (!(found0 && found1)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Didn't manage to find local point numbers for facet support");
1105eb62eeaaSLawrence Mitchell     }
11064bbf5ea8SMatthew G. Knepley   }
11071b68eb51SMatthew G. Knepley   ierr = PetscHSetIDestroy(&ht);CHKERRQ(ierr);
11081b68eb51SMatthew G. Knepley   ierr = PetscHSetIDestroy(&cht);CHKERRQ(ierr);
11094bbf5ea8SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
11105f824522SMatthew G. Knepley 
11115f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numCells,  cellsArray,  PETSC_OWN_POINTER, &patch->cells);CHKERRQ(ierr);
11125f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->cells,  "Patch Cells");CHKERRQ(ierr);
11135f824522SMatthew G. Knepley   if (patch->viewCells) {
11145f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->cellCounts, patch->viewerCells, patch->formatCells);CHKERRQ(ierr);
11155f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->cells,      patch->viewerCells, patch->formatCells);CHKERRQ(ierr);
11165f824522SMatthew G. Knepley   }
11170e126c0bSLawrence Mitchell   ierr = ISCreateGeneral(PETSC_COMM_SELF, numIntFacets,  intFacetsArray,  PETSC_OWN_POINTER, &patch->intFacets);CHKERRQ(ierr);
11180e126c0bSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->intFacets,  "Patch Interior Facets");CHKERRQ(ierr);
1119eb62eeaaSLawrence Mitchell   ierr = ISCreateGeneral(PETSC_COMM_SELF, 2*numIntFacets, intFacetsToPatchCell, PETSC_OWN_POINTER, &patch->intFacetsToPatchCell);CHKERRQ(ierr);
1120eb62eeaaSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->intFacetsToPatchCell,  "Patch Interior Facets local support");CHKERRQ(ierr);
11210e126c0bSLawrence Mitchell   if (patch->viewIntFacets) {
1122fbb82ab5SLawrence Mitchell     ierr = ObjectView((PetscObject) patch->intFacetCounts,       patch->viewerIntFacets, patch->formatIntFacets);CHKERRQ(ierr);
11230e126c0bSLawrence Mitchell     ierr = ObjectView((PetscObject) patch->intFacets,            patch->viewerIntFacets, patch->formatIntFacets);CHKERRQ(ierr);
1124eb62eeaaSLawrence Mitchell     ierr = ObjectView((PetscObject) patch->intFacetsToPatchCell, patch->viewerIntFacets, patch->formatIntFacets);CHKERRQ(ierr);
11250e126c0bSLawrence Mitchell   }
11260e126c0bSLawrence Mitchell   ierr = ISCreateGeneral(PETSC_COMM_SELF, numExtFacets,  extFacetsArray,  PETSC_OWN_POINTER, &patch->extFacets);CHKERRQ(ierr);
11270e126c0bSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->extFacets,  "Patch Exterior Facets");CHKERRQ(ierr);
11280e126c0bSLawrence Mitchell   if (patch->viewExtFacets) {
1129fbb82ab5SLawrence Mitchell     ierr = ObjectView((PetscObject) patch->extFacetCounts, patch->viewerExtFacets, patch->formatExtFacets);CHKERRQ(ierr);
11300e126c0bSLawrence Mitchell     ierr = ObjectView((PetscObject) patch->extFacets,      patch->viewerExtFacets, patch->formatExtFacets);CHKERRQ(ierr);
11310e126c0bSLawrence Mitchell   }
11325f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints, pointsArray, PETSC_OWN_POINTER, &patch->points);CHKERRQ(ierr);
11335f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->points, "Patch Points");CHKERRQ(ierr);
11345f824522SMatthew G. Knepley   if (patch->viewPoints) {
11355f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->pointCounts, patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr);
11365f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->points,      patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr);
11375f824522SMatthew G. Knepley   }
11384bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
11394bbf5ea8SMatthew G. Knepley }
11404bbf5ea8SMatthew G. Knepley 
11414bbf5ea8SMatthew G. Knepley /*
11424bbf5ea8SMatthew G. Knepley  * PCPatchCreateCellPatchDiscretisationInfo - Build the dof maps for cell patches
11434bbf5ea8SMatthew G. Knepley  *
11444bbf5ea8SMatthew G. Knepley  * Input Parameters:
11454bbf5ea8SMatthew G. Knepley  * + dm - The DMPlex object defining the mesh
11464bbf5ea8SMatthew G. Knepley  * . cellCounts - Section with counts of cells around each vertex
11474bbf5ea8SMatthew G. Knepley  * . cells - IS of the cell point indices of cells in each patch
11484bbf5ea8SMatthew G. Knepley  * . cellNumbering - Section mapping plex cell points to Firedrake cell indices.
11494bbf5ea8SMatthew G. Knepley  * . nodesPerCell - number of nodes per cell.
11504bbf5ea8SMatthew G. Knepley  * - cellNodeMap - map from cells to node indices (nodesPerCell * numCells)
11514bbf5ea8SMatthew G. Knepley  *
11524bbf5ea8SMatthew G. Knepley  * Output Parameters:
11535f824522SMatthew G. Knepley  * + dofs - IS of local dof numbers of each cell in the patch, where local is a patch local numbering
11544bbf5ea8SMatthew G. Knepley  * . gtolCounts - Section with counts of dofs per cell patch
11554bbf5ea8SMatthew G. Knepley  * - gtol - IS mapping from global dofs to local dofs for each patch.
11564bbf5ea8SMatthew G. Knepley  */
11574bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatchDiscretisationInfo(PC pc)
11584bbf5ea8SMatthew G. Knepley {
11594bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch           = (PC_PATCH *) pc->data;
11604bbf5ea8SMatthew G. Knepley   PetscSection    cellCounts      = patch->cellCounts;
11615f824522SMatthew G. Knepley   PetscSection    pointCounts     = patch->pointCounts;
11620904074fSPatrick Farrell   PetscSection    gtolCounts, gtolCountsWithArtificial = NULL, gtolCountsWithAll = NULL;
11634bbf5ea8SMatthew G. Knepley   IS              cells           = patch->cells;
11645f824522SMatthew G. Knepley   IS              points          = patch->points;
11654bbf5ea8SMatthew G. Knepley   PetscSection    cellNumbering   = patch->cellNumbering;
11665f824522SMatthew G. Knepley   PetscInt        Nf              = patch->nsubspaces;
11675f824522SMatthew G. Knepley   PetscInt        numCells, numPoints;
11684bbf5ea8SMatthew G. Knepley   PetscInt        numDofs;
11690904074fSPatrick Farrell   PetscInt        numGlobalDofs, numGlobalDofsWithArtificial, numGlobalDofsWithAll;
11704bbf5ea8SMatthew G. Knepley   PetscInt        totalDofsPerCell = patch->totalDofsPerCell;
11714bbf5ea8SMatthew G. Knepley   PetscInt        vStart, vEnd, v;
11725f824522SMatthew G. Knepley   const PetscInt *cellsArray, *pointsArray;
11734bbf5ea8SMatthew G. Knepley   PetscInt       *newCellsArray   = NULL;
11744bbf5ea8SMatthew G. Knepley   PetscInt       *dofsArray       = NULL;
1175c2e6f3c0SFlorian Wechsung   PetscInt       *dofsArrayWithArtificial = NULL;
11760904074fSPatrick Farrell   PetscInt       *dofsArrayWithAll = NULL;
11775f824522SMatthew G. Knepley   PetscInt       *offsArray       = NULL;
1178c2e6f3c0SFlorian Wechsung   PetscInt       *offsArrayWithArtificial = NULL;
11790904074fSPatrick Farrell   PetscInt       *offsArrayWithAll = NULL;
11804bbf5ea8SMatthew G. Knepley   PetscInt       *asmArray        = NULL;
1181c2e6f3c0SFlorian Wechsung   PetscInt       *asmArrayWithArtificial = NULL;
11820904074fSPatrick Farrell   PetscInt       *asmArrayWithAll = NULL;
11834bbf5ea8SMatthew G. Knepley   PetscInt       *globalDofsArray = NULL;
1184c2e6f3c0SFlorian Wechsung   PetscInt       *globalDofsArrayWithArtificial = NULL;
11850904074fSPatrick Farrell   PetscInt       *globalDofsArrayWithAll = NULL;
11864bbf5ea8SMatthew G. Knepley   PetscInt        globalIndex     = 0;
11874bbf5ea8SMatthew G. Knepley   PetscInt        key             = 0;
11884bbf5ea8SMatthew G. Knepley   PetscInt        asmKey          = 0;
1189557beb66SLawrence Mitchell   DM              dm              = NULL;
1190557beb66SLawrence Mitchell   const PetscInt *bcNodes         = NULL;
11911b68eb51SMatthew G. Knepley   PetscHMapI      ht;
1192c2e6f3c0SFlorian Wechsung   PetscHMapI      htWithArtificial;
11930904074fSPatrick Farrell   PetscHMapI      htWithAll;
11941b68eb51SMatthew G. Knepley   PetscHSetI      globalBcs;
1195557beb66SLawrence Mitchell   PetscInt        numBcs;
11961b68eb51SMatthew G. Knepley   PetscHSetI      ownedpts, seenpts, owneddofs, seendofs, artificialbcs;
1197cda239d9SMatthew G. Knepley   PetscInt        pStart, pEnd, p, i;
119810534d48SPatrick Farrell   char            option[PETSC_MAX_PATH_LEN];
119939fd2e8aSPatrick Farrell   PetscBool       isNonlinear;
12004bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
12014bbf5ea8SMatthew G. Knepley 
12024bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1203557beb66SLawrence Mitchell 
1204557beb66SLawrence Mitchell   ierr = PCGetDM(pc, &dm); CHKERRQ(ierr);
12054bbf5ea8SMatthew G. Knepley   /* dofcounts section is cellcounts section * dofPerCell */
12064bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr);
12075f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(patch->pointCounts, &numPoints);CHKERRQ(ierr);
12084bbf5ea8SMatthew G. Knepley   numDofs = numCells * totalDofsPerCell;
12094bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numDofs, &dofsArray);CHKERRQ(ierr);
12105f824522SMatthew G. Knepley   ierr = PetscMalloc1(numPoints*Nf, &offsArray);CHKERRQ(ierr);
12114bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numDofs, &asmArray);CHKERRQ(ierr);
12124bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numCells, &newCellsArray);CHKERRQ(ierr);
12134bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(cellCounts, &vStart, &vEnd);CHKERRQ(ierr);
12144bbf5ea8SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCounts);CHKERRQ(ierr);
12154bbf5ea8SMatthew G. Knepley   gtolCounts = patch->gtolCounts;
12164bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetChart(gtolCounts, vStart, vEnd);CHKERRQ(ierr);
12175f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->gtolCounts, "Patch Global Index Section");CHKERRQ(ierr);
12184bbf5ea8SMatthew G. Knepley 
12190904074fSPatrick Farrell   if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE)
1220c2e6f3c0SFlorian Wechsung   {
1221f0dcb611SKarl Rupp     ierr = PetscMalloc1(numPoints*Nf, &offsArrayWithArtificial);CHKERRQ(ierr);
1222c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numDofs, &asmArrayWithArtificial);CHKERRQ(ierr);
1223c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numDofs, &dofsArrayWithArtificial);CHKERRQ(ierr);
1224c2e6f3c0SFlorian Wechsung     ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithArtificial);CHKERRQ(ierr);
1225c2e6f3c0SFlorian Wechsung     gtolCountsWithArtificial = patch->gtolCountsWithArtificial;
1226c2e6f3c0SFlorian Wechsung     ierr = PetscSectionSetChart(gtolCountsWithArtificial, vStart, vEnd);CHKERRQ(ierr);
1227c2e6f3c0SFlorian Wechsung     ierr = PetscObjectSetName((PetscObject) patch->gtolCountsWithArtificial, "Patch Global Index Section Including Artificial BCs");CHKERRQ(ierr);
1228c2e6f3c0SFlorian Wechsung   }
1229c2e6f3c0SFlorian Wechsung 
12300904074fSPatrick Farrell   isNonlinear = patch->isNonlinear;
12310904074fSPatrick Farrell   if(isNonlinear)
12320904074fSPatrick Farrell   {
12330904074fSPatrick Farrell     ierr = PetscMalloc1(numPoints*Nf, &offsArrayWithAll);CHKERRQ(ierr);
12340904074fSPatrick Farrell     ierr = PetscMalloc1(numDofs, &asmArrayWithAll);CHKERRQ(ierr);
12350904074fSPatrick Farrell     ierr = PetscMalloc1(numDofs, &dofsArrayWithAll);CHKERRQ(ierr);
12360904074fSPatrick Farrell     ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithAll);CHKERRQ(ierr);
12370904074fSPatrick Farrell     gtolCountsWithAll = patch->gtolCountsWithAll;
12380904074fSPatrick Farrell     ierr = PetscSectionSetChart(gtolCountsWithAll, vStart, vEnd);CHKERRQ(ierr);
12390904074fSPatrick Farrell     ierr = PetscObjectSetName((PetscObject) patch->gtolCountsWithAll, "Patch Global Index Section Including All BCs");CHKERRQ(ierr);
12400904074fSPatrick Farrell   }
12410904074fSPatrick Farrell 
1242557beb66SLawrence Mitchell   /* Outside the patch loop, get the dofs that are globally-enforced Dirichlet
1243557beb66SLawrence Mitchell    conditions */
12441b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&globalBcs);CHKERRQ(ierr);
1245557beb66SLawrence Mitchell   ierr = ISGetIndices(patch->ghostBcNodes, &bcNodes); CHKERRQ(ierr);
1246557beb66SLawrence Mitchell   ierr = ISGetSize(patch->ghostBcNodes, &numBcs); CHKERRQ(ierr);
1247cda239d9SMatthew G. Knepley   for (i = 0; i < numBcs; ++i) {
12481b68eb51SMatthew G. Knepley     ierr = PetscHSetIAdd(globalBcs, bcNodes[i]);CHKERRQ(ierr); /* these are already in concatenated numbering */
1249557beb66SLawrence Mitchell   }
1250557beb66SLawrence Mitchell   ierr = ISRestoreIndices(patch->ghostBcNodes, &bcNodes); CHKERRQ(ierr);
1251557beb66SLawrence Mitchell   ierr = ISDestroy(&patch->ghostBcNodes); CHKERRQ(ierr); /* memory optimisation */
1252557beb66SLawrence Mitchell 
1253557beb66SLawrence Mitchell   /* Hash tables for artificial BC construction */
12541b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&ownedpts);CHKERRQ(ierr);
12551b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&seenpts);CHKERRQ(ierr);
12561b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&owneddofs);CHKERRQ(ierr);
12571b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&seendofs);CHKERRQ(ierr);
12581b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&artificialbcs);CHKERRQ(ierr);
1259557beb66SLawrence Mitchell 
12604bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(cells, &cellsArray);CHKERRQ(ierr);
12615f824522SMatthew G. Knepley   ierr = ISGetIndices(points, &pointsArray);CHKERRQ(ierr);
12621b68eb51SMatthew G. Knepley   ierr = PetscHMapICreate(&ht);CHKERRQ(ierr);
1263c2e6f3c0SFlorian Wechsung   ierr = PetscHMapICreate(&htWithArtificial);CHKERRQ(ierr);
12640904074fSPatrick Farrell   ierr = PetscHMapICreate(&htWithAll);CHKERRQ(ierr);
12654bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
12664bbf5ea8SMatthew G. Knepley     PetscInt localIndex = 0;
1267c2e6f3c0SFlorian Wechsung     PetscInt localIndexWithArtificial = 0;
12680904074fSPatrick Farrell     PetscInt localIndexWithAll = 0;
12694bbf5ea8SMatthew G. Knepley     PetscInt dof, off, i, j, k, l;
12704bbf5ea8SMatthew G. Knepley 
12711b68eb51SMatthew G. Knepley     ierr = PetscHMapIClear(ht);CHKERRQ(ierr);
1272c2e6f3c0SFlorian Wechsung     ierr = PetscHMapIClear(htWithArtificial);CHKERRQ(ierr);
12730904074fSPatrick Farrell     ierr = PetscHMapIClear(htWithAll);CHKERRQ(ierr);
12744bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr);
12754bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr);
12764bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
12774bbf5ea8SMatthew G. Knepley 
1278557beb66SLawrence Mitchell     /* Calculate the global numbers of the artificial BC dofs here first */
1279557beb66SLawrence Mitchell     ierr = patch->patchconstructop((void*)patch, dm, v, ownedpts); CHKERRQ(ierr);
1280557beb66SLawrence Mitchell     ierr = PCPatchCompleteCellPatch(pc, ownedpts, seenpts); CHKERRQ(ierr);
1281e4c66b91SPatrick Farrell     ierr = PCPatchGetPointDofs(pc, ownedpts, owneddofs, v, &patch->subspaces_to_exclude); CHKERRQ(ierr);
1282e4c66b91SPatrick Farrell     ierr = PCPatchGetPointDofs(pc, seenpts, seendofs, v, NULL); CHKERRQ(ierr);
1283557beb66SLawrence Mitchell     ierr = PCPatchComputeSetDifference_Private(owneddofs, seendofs, artificialbcs); CHKERRQ(ierr);
12848135ed82SLawrence Mitchell     if (patch->viewPatches) {
12851b68eb51SMatthew G. Knepley       PetscHSetI globalbcdofs;
12861b68eb51SMatthew G. Knepley       PetscHashIter hi;
12878135ed82SLawrence Mitchell       MPI_Comm comm = PetscObjectComm((PetscObject)pc);
12881b68eb51SMatthew G. Knepley 
12891b68eb51SMatthew G. Knepley       ierr = PetscHSetICreate(&globalbcdofs);CHKERRQ(ierr);
12908135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: owned dofs:\n", v); CHKERRQ(ierr);
12911b68eb51SMatthew G. Knepley       PetscHashIterBegin(owneddofs, hi);
12921b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(owneddofs, hi)) {
12938135ed82SLawrence Mitchell         PetscInt globalDof;
12948135ed82SLawrence Mitchell 
12951b68eb51SMatthew G. Knepley         PetscHashIterGetKey(owneddofs, hi, globalDof);
12961b68eb51SMatthew G. Knepley         PetscHashIterNext(owneddofs, hi);
12978135ed82SLawrence Mitchell         ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
12988135ed82SLawrence Mitchell       }
12998135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n"); CHKERRQ(ierr);
13008135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: seen dofs:\n", v); CHKERRQ(ierr);
13011b68eb51SMatthew G. Knepley       PetscHashIterBegin(seendofs, hi);
13021b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(seendofs, hi)) {
13038135ed82SLawrence Mitchell         PetscInt globalDof;
13048135ed82SLawrence Mitchell         PetscBool flg;
13058135ed82SLawrence Mitchell 
13061b68eb51SMatthew G. Knepley         PetscHashIterGetKey(seendofs, hi, globalDof);
13071b68eb51SMatthew G. Knepley         PetscHashIterNext(seendofs, hi);
13088135ed82SLawrence Mitchell         ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
13098135ed82SLawrence Mitchell 
13101b68eb51SMatthew G. Knepley         ierr = PetscHSetIHas(globalBcs, globalDof, &flg);CHKERRQ(ierr);
13111b68eb51SMatthew G. Knepley         if (flg) {ierr = PetscHSetIAdd(globalbcdofs, globalDof);CHKERRQ(ierr);}
13128135ed82SLawrence Mitchell       }
13138135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n"); CHKERRQ(ierr);
13148135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: global BCs:\n", v); CHKERRQ(ierr);
13151b68eb51SMatthew G. Knepley       ierr = PetscHSetIGetSize(globalbcdofs, &numBcs);CHKERRQ(ierr);
13168135ed82SLawrence Mitchell       if (numBcs > 0) {
13171b68eb51SMatthew G. Knepley         PetscHashIterBegin(globalbcdofs, hi);
13181b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(globalbcdofs, hi)) {
13198135ed82SLawrence Mitchell           PetscInt globalDof;
13201b68eb51SMatthew G. Knepley           PetscHashIterGetKey(globalbcdofs, hi, globalDof);
13211b68eb51SMatthew G. Knepley           PetscHashIterNext(globalbcdofs, hi);
13228135ed82SLawrence Mitchell           ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof);CHKERRQ(ierr);
13238135ed82SLawrence Mitchell         }
13248135ed82SLawrence Mitchell       }
13258135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n");CHKERRQ(ierr);
13268135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: artificial BCs:\n", v);CHKERRQ(ierr);
13271b68eb51SMatthew G. Knepley       ierr = PetscHSetIGetSize(artificialbcs, &numBcs);CHKERRQ(ierr);
13288135ed82SLawrence Mitchell       if (numBcs > 0) {
13291b68eb51SMatthew G. Knepley         PetscHashIterBegin(artificialbcs, hi);
13301b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(artificialbcs, hi)) {
13318135ed82SLawrence Mitchell           PetscInt globalDof;
13321b68eb51SMatthew G. Knepley           PetscHashIterGetKey(artificialbcs, hi, globalDof);
13331b68eb51SMatthew G. Knepley           PetscHashIterNext(artificialbcs, hi);
13348135ed82SLawrence Mitchell           ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
13358135ed82SLawrence Mitchell         }
13368135ed82SLawrence Mitchell       }
13378135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n\n"); CHKERRQ(ierr);
13381b68eb51SMatthew G. Knepley       ierr = PetscHSetIDestroy(&globalbcdofs);CHKERRQ(ierr);
13398135ed82SLawrence Mitchell     }
13404bbf5ea8SMatthew G. Knepley    for (k = 0; k < patch->nsubspaces; ++k) {
13414bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
13424bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
13434bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
13444bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
13454bbf5ea8SMatthew G. Knepley 
13464bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
13474bbf5ea8SMatthew G. Knepley         /* Walk over the cells in this patch. */
13484bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
13495f824522SMatthew G. Knepley         PetscInt       cell = c;
13504bbf5ea8SMatthew G. Knepley 
13515f824522SMatthew G. Knepley         /* TODO Change this to an IS */
13525f824522SMatthew G. Knepley         if (cellNumbering) {
13534bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetDof(cellNumbering, c, &cell);CHKERRQ(ierr);
13544bbf5ea8SMatthew G. Knepley           if (cell <= 0) SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_OUTOFRANGE, "Cell %D doesn't appear in cell numbering map", c);
13554bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);
13565f824522SMatthew G. Knepley         }
13574bbf5ea8SMatthew G. Knepley         newCellsArray[i] = cell;
13584bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
13594bbf5ea8SMatthew G. Knepley           /* For each global dof, map it into contiguous local storage. */
13604bbf5ea8SMatthew G. Knepley           const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + subspaceOffset;
13614bbf5ea8SMatthew G. Knepley           /* finally, loop over block size */
13624bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
13631b68eb51SMatthew G. Knepley             PetscInt  localDof;
13641b68eb51SMatthew G. Knepley             PetscBool isGlobalBcDof, isArtificialBcDof;
13654bbf5ea8SMatthew G. Knepley 
1366557beb66SLawrence Mitchell             /* first, check if this is either a globally enforced or locally enforced BC dof */
13671b68eb51SMatthew G. Knepley             ierr = PetscHSetIHas(globalBcs, globalDof + l, &isGlobalBcDof);CHKERRQ(ierr);
13681b68eb51SMatthew G. Knepley             ierr = PetscHSetIHas(artificialbcs, globalDof + l, &isArtificialBcDof);CHKERRQ(ierr);
1369557beb66SLawrence Mitchell 
1370557beb66SLawrence Mitchell             /* if it's either, don't ever give it a local dof number */
13711b68eb51SMatthew G. Knepley             if (isGlobalBcDof || isArtificialBcDof) {
1372c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = -1; /* don't use this in assembly in this patch */
1373557beb66SLawrence Mitchell             } else {
13741b68eb51SMatthew G. Knepley               ierr = PetscHMapIGet(ht, globalDof + l, &localDof);CHKERRQ(ierr);
13754bbf5ea8SMatthew G. Knepley               if (localDof == -1) {
13764bbf5ea8SMatthew G. Knepley                 localDof = localIndex++;
13771b68eb51SMatthew G. Knepley                 ierr = PetscHMapISet(ht, globalDof + l, localDof);CHKERRQ(ierr);
13784bbf5ea8SMatthew G. Knepley               }
13794bbf5ea8SMatthew G. Knepley               if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
13804bbf5ea8SMatthew G. Knepley               /* And store. */
1381c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = localDof;
13824bbf5ea8SMatthew G. Knepley             }
1383c2e6f3c0SFlorian Wechsung 
13840904074fSPatrick Farrell             if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1385c2e6f3c0SFlorian Wechsung               if (isGlobalBcDof) {
1386e047a90bSFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = -1; /* don't use this in assembly in this patch */
1387c2e6f3c0SFlorian Wechsung               } else {
1388c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapIGet(htWithArtificial, globalDof + l, &localDof);CHKERRQ(ierr);
1389c2e6f3c0SFlorian Wechsung                 if (localDof == -1) {
1390c2e6f3c0SFlorian Wechsung                   localDof = localIndexWithArtificial++;
1391c2e6f3c0SFlorian Wechsung                   ierr = PetscHMapISet(htWithArtificial, globalDof + l, localDof);CHKERRQ(ierr);
1392c2e6f3c0SFlorian Wechsung                 }
1393c2e6f3c0SFlorian Wechsung                 if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
1394c2e6f3c0SFlorian Wechsung                 /* And store.*/
1395c2e6f3c0SFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = localDof;
1396c2e6f3c0SFlorian Wechsung               }
1397c2e6f3c0SFlorian Wechsung             }
13980904074fSPatrick Farrell 
13990904074fSPatrick Farrell             if(isNonlinear) {
14000904074fSPatrick Farrell               /* Build the dofmap for the function space with _all_ dofs,
14010904074fSPatrick Farrell                  including those in any kind of boundary condition */
14020904074fSPatrick Farrell               ierr = PetscHMapIGet(htWithAll, globalDof + l, &localDof);CHKERRQ(ierr);
14030904074fSPatrick Farrell               if (localDof == -1) {
14040904074fSPatrick Farrell                 localDof = localIndexWithAll++;
14050904074fSPatrick Farrell                 ierr = PetscHMapISet(htWithAll, globalDof + l, localDof);CHKERRQ(ierr);
14060904074fSPatrick Farrell               }
14070904074fSPatrick Farrell               if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
14080904074fSPatrick Farrell               /* And store.*/
14090904074fSPatrick Farrell               dofsArrayWithAll[globalIndex] = localDof;
14100904074fSPatrick Farrell             }
1411c2e6f3c0SFlorian Wechsung             globalIndex++;
14124bbf5ea8SMatthew G. Knepley           }
14134bbf5ea8SMatthew G. Knepley         }
14144bbf5ea8SMatthew G. Knepley       }
1415557beb66SLawrence Mitchell     }
14164bbf5ea8SMatthew G. Knepley      /*How many local dofs in this patch? */
14170904074fSPatrick Farrell    if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1418c2e6f3c0SFlorian Wechsung      ierr = PetscHMapIGetSize(htWithArtificial, &dof);CHKERRQ(ierr);
1419c2e6f3c0SFlorian Wechsung      ierr = PetscSectionSetDof(gtolCountsWithArtificial, v, dof);CHKERRQ(ierr);
1420c2e6f3c0SFlorian Wechsung    }
14210904074fSPatrick Farrell    if (isNonlinear) {
14220904074fSPatrick Farrell      ierr = PetscHMapIGetSize(htWithAll, &dof);CHKERRQ(ierr);
14230904074fSPatrick Farrell      ierr = PetscSectionSetDof(gtolCountsWithAll, v, dof);CHKERRQ(ierr);
14240904074fSPatrick Farrell    }
14251b68eb51SMatthew G. Knepley     ierr = PetscHMapIGetSize(ht, &dof);CHKERRQ(ierr);
14264bbf5ea8SMatthew G. Knepley     ierr = PetscSectionSetDof(gtolCounts, v, dof);CHKERRQ(ierr);
14274bbf5ea8SMatthew G. Knepley   }
14284bbf5ea8SMatthew 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);
14294bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetUp(gtolCounts);CHKERRQ(ierr);
14304bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(gtolCounts, &numGlobalDofs);CHKERRQ(ierr);
14314bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numGlobalDofs, &globalDofsArray);CHKERRQ(ierr);
14324bbf5ea8SMatthew G. Knepley 
14330904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1434c2e6f3c0SFlorian Wechsung     ierr = PetscSectionSetUp(gtolCountsWithArtificial);CHKERRQ(ierr);
1435c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetStorageSize(gtolCountsWithArtificial, &numGlobalDofsWithArtificial);CHKERRQ(ierr);
1436c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numGlobalDofsWithArtificial, &globalDofsArrayWithArtificial);CHKERRQ(ierr);
1437c2e6f3c0SFlorian Wechsung   }
14380904074fSPatrick Farrell   if (isNonlinear) {
14390904074fSPatrick Farrell     ierr = PetscSectionSetUp(gtolCountsWithAll);CHKERRQ(ierr);
14400904074fSPatrick Farrell     ierr = PetscSectionGetStorageSize(gtolCountsWithAll, &numGlobalDofsWithAll);CHKERRQ(ierr);
14410904074fSPatrick Farrell     ierr = PetscMalloc1(numGlobalDofsWithAll, &globalDofsArrayWithAll);CHKERRQ(ierr);
14420904074fSPatrick Farrell   }
14434bbf5ea8SMatthew 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. */
14444bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
14451b68eb51SMatthew G. Knepley     PetscHashIter hi;
14465f824522SMatthew G. Knepley     PetscInt      dof, off, Np, ooff, i, j, k, l;
14474bbf5ea8SMatthew G. Knepley 
14481b68eb51SMatthew G. Knepley     ierr = PetscHMapIClear(ht);CHKERRQ(ierr);
1449c2e6f3c0SFlorian Wechsung     ierr = PetscHMapIClear(htWithArtificial);CHKERRQ(ierr);
14500904074fSPatrick Farrell     ierr = PetscHMapIClear(htWithAll);CHKERRQ(ierr);
14514bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr);
14524bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr);
14535f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(pointCounts, v, &Np);CHKERRQ(ierr);
14545f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(pointCounts, v, &ooff);CHKERRQ(ierr);
14554bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
14564bbf5ea8SMatthew G. Knepley 
14574bbf5ea8SMatthew G. Knepley     for (k = 0; k < patch->nsubspaces; ++k) {
14584bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
14594bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
14604bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
14614bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
1462d490bb3dSLawrence Mitchell       PetscInt        goff;
14634bbf5ea8SMatthew G. Knepley 
14644bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
14654bbf5ea8SMatthew G. Knepley         /* Reconstruct mapping of global-to-local on this patch. */
14664bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
14675f824522SMatthew G. Knepley         PetscInt       cell = c;
14684bbf5ea8SMatthew G. Knepley 
14695f824522SMatthew G. Knepley         if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);}
14704bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
14714bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
14725f824522SMatthew G. Knepley             const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset;
1473c2e6f3c0SFlorian Wechsung             const PetscInt localDof  = dofsArray[key];
14741b68eb51SMatthew G. Knepley             if (localDof >= 0) {ierr = PetscHMapISet(ht, globalDof, localDof);CHKERRQ(ierr);}
14750904074fSPatrick Farrell             if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1476c2e6f3c0SFlorian Wechsung               const PetscInt localDofWithArtificial = dofsArrayWithArtificial[key];
1477c2e6f3c0SFlorian Wechsung               if (localDofWithArtificial >= 0) {
1478c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapISet(htWithArtificial, globalDof, localDofWithArtificial);CHKERRQ(ierr);
1479c2e6f3c0SFlorian Wechsung               }
1480c2e6f3c0SFlorian Wechsung             }
14810904074fSPatrick Farrell             if (isNonlinear) {
14820904074fSPatrick Farrell               const PetscInt localDofWithAll = dofsArrayWithAll[key];
14830904074fSPatrick Farrell               if (localDofWithAll >= 0) {
14840904074fSPatrick Farrell                 ierr = PetscHMapISet(htWithAll, globalDof, localDofWithAll);CHKERRQ(ierr);
14850904074fSPatrick Farrell               }
14860904074fSPatrick Farrell             }
1487c2e6f3c0SFlorian Wechsung             key++;
14884bbf5ea8SMatthew G. Knepley           }
14894bbf5ea8SMatthew G. Knepley         }
14904bbf5ea8SMatthew G. Knepley       }
1491557beb66SLawrence Mitchell 
14924bbf5ea8SMatthew G. Knepley       /* Shove it in the output data structure. */
14934bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetOffset(gtolCounts, v, &goff);CHKERRQ(ierr);
14941b68eb51SMatthew G. Knepley       PetscHashIterBegin(ht, hi);
14951b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(ht, hi)) {
14964bbf5ea8SMatthew G. Knepley         PetscInt globalDof, localDof;
14974bbf5ea8SMatthew G. Knepley 
14981b68eb51SMatthew G. Knepley         PetscHashIterGetKey(ht, hi, globalDof);
14991b68eb51SMatthew G. Knepley         PetscHashIterGetVal(ht, hi, localDof);
15004bbf5ea8SMatthew G. Knepley         if (globalDof >= 0) globalDofsArray[goff + localDof] = globalDof;
15011b68eb51SMatthew G. Knepley         PetscHashIterNext(ht, hi);
15024bbf5ea8SMatthew G. Knepley       }
15035f824522SMatthew G. Knepley 
15040904074fSPatrick Farrell       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1505c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(gtolCountsWithArtificial, v, &goff);CHKERRQ(ierr);
1506c2e6f3c0SFlorian Wechsung         PetscHashIterBegin(htWithArtificial, hi);
1507c2e6f3c0SFlorian Wechsung         while (!PetscHashIterAtEnd(htWithArtificial, hi)) {
1508c2e6f3c0SFlorian Wechsung           PetscInt globalDof, localDof;
1509c2e6f3c0SFlorian Wechsung           PetscHashIterGetKey(htWithArtificial, hi, globalDof);
1510c2e6f3c0SFlorian Wechsung           PetscHashIterGetVal(htWithArtificial, hi, localDof);
1511c2e6f3c0SFlorian Wechsung           if (globalDof >= 0) globalDofsArrayWithArtificial[goff + localDof] = globalDof;
1512c2e6f3c0SFlorian Wechsung           PetscHashIterNext(htWithArtificial, hi);
1513c2e6f3c0SFlorian Wechsung         }
1514c2e6f3c0SFlorian Wechsung       }
15150904074fSPatrick Farrell       if (isNonlinear) {
15160904074fSPatrick Farrell         ierr = PetscSectionGetOffset(gtolCountsWithAll, v, &goff);CHKERRQ(ierr);
15170904074fSPatrick Farrell         PetscHashIterBegin(htWithAll, hi);
15180904074fSPatrick Farrell         while (!PetscHashIterAtEnd(htWithAll, hi)) {
15190904074fSPatrick Farrell           PetscInt globalDof, localDof;
15200904074fSPatrick Farrell           PetscHashIterGetKey(htWithAll, hi, globalDof);
15210904074fSPatrick Farrell           PetscHashIterGetVal(htWithAll, hi, localDof);
15220904074fSPatrick Farrell           if (globalDof >= 0) globalDofsArrayWithAll[goff + localDof] = globalDof;
15230904074fSPatrick Farrell           PetscHashIterNext(htWithAll, hi);
15240904074fSPatrick Farrell         }
15250904074fSPatrick Farrell       }
1526c2e6f3c0SFlorian Wechsung 
15275f824522SMatthew G. Knepley       for (p = 0; p < Np; ++p) {
15285f824522SMatthew G. Knepley         const PetscInt point = pointsArray[ooff + p];
15295f824522SMatthew G. Knepley         PetscInt       globalDof, localDof;
15305f824522SMatthew G. Knepley 
15315f824522SMatthew G. Knepley         ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, point, NULL, &globalDof);CHKERRQ(ierr);
15321b68eb51SMatthew G. Knepley         ierr = PetscHMapIGet(ht, globalDof, &localDof);CHKERRQ(ierr);
15335f824522SMatthew G. Knepley         offsArray[(ooff + p)*Nf + k] = localDof;
15340904074fSPatrick Farrell         if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1535c2e6f3c0SFlorian Wechsung           ierr = PetscHMapIGet(htWithArtificial, globalDof, &localDof);CHKERRQ(ierr);
1536c2e6f3c0SFlorian Wechsung           offsArrayWithArtificial[(ooff + p)*Nf + k] = localDof;
1537c2e6f3c0SFlorian Wechsung         }
15380904074fSPatrick Farrell         if (isNonlinear) {
15390904074fSPatrick Farrell           ierr = PetscHMapIGet(htWithAll, globalDof, &localDof);CHKERRQ(ierr);
15400904074fSPatrick Farrell           offsArrayWithAll[(ooff + p)*Nf + k] = localDof;
15410904074fSPatrick Farrell         }
15425f824522SMatthew G. Knepley       }
15434bbf5ea8SMatthew G. Knepley     }
15444bbf5ea8SMatthew G. Knepley 
15450cd083f8SSatish Balay     ierr = PetscHSetIDestroy(&globalBcs);CHKERRQ(ierr);
15461b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&ownedpts);CHKERRQ(ierr);
15471b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&seenpts);CHKERRQ(ierr);
15481b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&owneddofs);CHKERRQ(ierr);
15491b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&seendofs);CHKERRQ(ierr);
15501b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&artificialbcs);CHKERRQ(ierr);
1551557beb66SLawrence Mitchell 
15524bbf5ea8SMatthew G. Knepley       /* At this point, we have a hash table ht built that maps globalDof -> localDof.
15534bbf5ea8SMatthew G. Knepley      We need to create the dof table laid out cellwise first, then by subspace,
15544bbf5ea8SMatthew G. Knepley      as the assembler assembles cell-wise and we need to stuff the different
15554bbf5ea8SMatthew G. Knepley      contributions of the different function spaces to the right places. So we loop
15564bbf5ea8SMatthew G. Knepley      over cells, then over subspaces. */
15574bbf5ea8SMatthew G. Knepley     if (patch->nsubspaces > 1) { /* for nsubspaces = 1, data we need is already in dofsArray */
15584bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
15594bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
15605f824522SMatthew G. Knepley         PetscInt       cell = c;
15614bbf5ea8SMatthew G. Knepley 
15625f824522SMatthew G. Knepley         if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);}
15634bbf5ea8SMatthew G. Knepley         for (k = 0; k < patch->nsubspaces; ++k) {
15644bbf5ea8SMatthew G. Knepley           const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
15654bbf5ea8SMatthew G. Knepley           PetscInt        nodesPerCell   = patch->nodesPerCell[k];
15664bbf5ea8SMatthew G. Knepley           PetscInt        subspaceOffset = patch->subspaceOffsets[k];
15674bbf5ea8SMatthew G. Knepley           PetscInt        bs             = patch->bs[k];
15684bbf5ea8SMatthew G. Knepley 
15694bbf5ea8SMatthew G. Knepley           for (j = 0; j < nodesPerCell; ++j) {
15704bbf5ea8SMatthew G. Knepley             for (l = 0; l < bs; ++l) {
15715f824522SMatthew G. Knepley               const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset;
15724bbf5ea8SMatthew G. Knepley               PetscInt       localDof;
15734bbf5ea8SMatthew G. Knepley 
15741b68eb51SMatthew G. Knepley               ierr = PetscHMapIGet(ht, globalDof, &localDof);CHKERRQ(ierr);
1575557beb66SLawrence Mitchell               /* If it's not in the hash table, i.e. is a BC dof,
15761b68eb51SMatthew G. Knepley                then the PetscHSetIMap above gives -1, which matches
1577557beb66SLawrence Mitchell                exactly the convention for PETSc's matrix assembly to
1578557beb66SLawrence Mitchell                ignore the dof. So we don't need to do anything here */
1579c2e6f3c0SFlorian Wechsung               asmArray[asmKey] = localDof;
15800904074fSPatrick Farrell               if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1581c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapIGet(htWithArtificial, globalDof, &localDof);CHKERRQ(ierr);
1582c2e6f3c0SFlorian Wechsung                 asmArrayWithArtificial[asmKey] = localDof;
1583c2e6f3c0SFlorian Wechsung               }
15840904074fSPatrick Farrell               if (isNonlinear) {
15850904074fSPatrick Farrell                 ierr = PetscHMapIGet(htWithAll, globalDof, &localDof);CHKERRQ(ierr);
15860904074fSPatrick Farrell                 asmArrayWithAll[asmKey] = localDof;
15870904074fSPatrick Farrell               }
1588c2e6f3c0SFlorian Wechsung               asmKey++;
15894bbf5ea8SMatthew G. Knepley             }
15904bbf5ea8SMatthew G. Knepley           }
15914bbf5ea8SMatthew G. Knepley         }
15924bbf5ea8SMatthew G. Knepley       }
15934bbf5ea8SMatthew G. Knepley     }
15944bbf5ea8SMatthew G. Knepley   }
1595c2e6f3c0SFlorian Wechsung   if (1 == patch->nsubspaces) {
1596c2e6f3c0SFlorian Wechsung     ierr = PetscMemcpy(asmArray, dofsArray, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
15970904074fSPatrick Farrell     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1598c2e6f3c0SFlorian Wechsung       ierr = PetscMemcpy(asmArrayWithArtificial, dofsArrayWithArtificial, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
1599c2e6f3c0SFlorian Wechsung     }
16000904074fSPatrick Farrell     if (isNonlinear) {
16010904074fSPatrick Farrell       ierr = PetscMemcpy(asmArrayWithAll, dofsArrayWithAll, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
16020904074fSPatrick Farrell     }
1603c2e6f3c0SFlorian Wechsung   }
16044bbf5ea8SMatthew G. Knepley 
16051b68eb51SMatthew G. Knepley   ierr = PetscHMapIDestroy(&ht);CHKERRQ(ierr);
1606c2e6f3c0SFlorian Wechsung   ierr = PetscHMapIDestroy(&htWithArtificial);CHKERRQ(ierr);
16070904074fSPatrick Farrell   ierr = PetscHMapIDestroy(&htWithAll);CHKERRQ(ierr);
16084bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(cells, &cellsArray);CHKERRQ(ierr);
16095f824522SMatthew G. Knepley   ierr = ISRestoreIndices(points, &pointsArray);CHKERRQ(ierr);
16104bbf5ea8SMatthew G. Knepley   ierr = PetscFree(dofsArray);CHKERRQ(ierr);
16110904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1612c2e6f3c0SFlorian Wechsung     ierr = PetscFree(dofsArrayWithArtificial);CHKERRQ(ierr);
1613c2e6f3c0SFlorian Wechsung   }
16140904074fSPatrick Farrell   if (isNonlinear) {
16150904074fSPatrick Farrell     ierr = PetscFree(dofsArrayWithAll);CHKERRQ(ierr);
16160904074fSPatrick Farrell   }
16175f824522SMatthew G. Knepley   /* Create placeholder section for map from points to patch dofs */
16185f824522SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->patchSection);CHKERRQ(ierr);
16195f824522SMatthew G. Knepley   ierr = PetscSectionSetNumFields(patch->patchSection, patch->nsubspaces);CHKERRQ(ierr);
16201e5fa6bbSLawrence Mitchell   if (patch->combined) {
16211e5fa6bbSLawrence Mitchell     PetscInt numFields;
16221e5fa6bbSLawrence Mitchell     ierr = PetscSectionGetNumFields(patch->dofSection[0], &numFields);CHKERRQ(ierr);
16231e5fa6bbSLawrence 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);
16245f824522SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->dofSection[0], &pStart, &pEnd);CHKERRQ(ierr);
16255f824522SMatthew G. Knepley     ierr = PetscSectionSetChart(patch->patchSection, pStart, pEnd);CHKERRQ(ierr);
16265f824522SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
16275f824522SMatthew G. Knepley       PetscInt dof, fdof, f;
16285f824522SMatthew G. Knepley 
16295f824522SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->dofSection[0], p, &dof);CHKERRQ(ierr);
16305f824522SMatthew G. Knepley       ierr = PetscSectionSetDof(patch->patchSection, p, dof);CHKERRQ(ierr);
16315f824522SMatthew G. Knepley       for (f = 0; f < patch->nsubspaces; ++f) {
16321e5fa6bbSLawrence Mitchell         ierr = PetscSectionGetFieldDof(patch->dofSection[0], p, f, &fdof);CHKERRQ(ierr);
16335f824522SMatthew G. Knepley         ierr = PetscSectionSetFieldDof(patch->patchSection, p, f, fdof);CHKERRQ(ierr);
16345f824522SMatthew G. Knepley       }
16351e5fa6bbSLawrence Mitchell     }
16361e5fa6bbSLawrence Mitchell   } else {
16371e5fa6bbSLawrence Mitchell     PetscInt pStartf, pEndf, f;
16381e5fa6bbSLawrence Mitchell     pStart = PETSC_MAX_INT;
16391e5fa6bbSLawrence Mitchell     pEnd = PETSC_MIN_INT;
16401e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
16411e5fa6bbSLawrence Mitchell       ierr = PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf);CHKERRQ(ierr);
16421e5fa6bbSLawrence Mitchell       pStart = PetscMin(pStart, pStartf);
16431e5fa6bbSLawrence Mitchell       pEnd = PetscMax(pEnd, pEndf);
16441e5fa6bbSLawrence Mitchell     }
16451e5fa6bbSLawrence Mitchell     ierr = PetscSectionSetChart(patch->patchSection, pStart, pEnd);CHKERRQ(ierr);
16461e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
16471e5fa6bbSLawrence Mitchell       ierr = PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf);CHKERRQ(ierr);
16481e5fa6bbSLawrence Mitchell       for (p = pStartf; p < pEndf; ++p) {
16491e5fa6bbSLawrence Mitchell         PetscInt fdof;
16501e5fa6bbSLawrence Mitchell         ierr = PetscSectionGetDof(patch->dofSection[f], p, &fdof);CHKERRQ(ierr);
16511e5fa6bbSLawrence Mitchell         ierr = PetscSectionAddDof(patch->patchSection, p, fdof);CHKERRQ(ierr);
16521e5fa6bbSLawrence Mitchell         ierr = PetscSectionSetFieldDof(patch->patchSection, p, f, fdof);CHKERRQ(ierr);
1653bdd9e0cdSPatrick Farrell       }
1654bdd9e0cdSPatrick Farrell     }
16555f824522SMatthew G. Knepley   }
16565f824522SMatthew G. Knepley   ierr = PetscSectionSetUp(patch->patchSection);CHKERRQ(ierr);
16575f824522SMatthew G. Knepley   ierr = PetscSectionSetUseFieldOffsets(patch->patchSection, PETSC_TRUE);CHKERRQ(ierr);
16584bbf5ea8SMatthew G. Knepley   /* Replace cell indices with firedrake-numbered ones. */
16594bbf5ea8SMatthew G. Knepley   ierr = ISGeneralSetIndices(cells, numCells, (const PetscInt *) newCellsArray, PETSC_OWN_POINTER);CHKERRQ(ierr);
16604bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofs, globalDofsArray, PETSC_OWN_POINTER, &patch->gtol);CHKERRQ(ierr);
16615f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->gtol, "Global Indices");CHKERRQ(ierr);
166210534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_g2l_view", patch->classname);CHKERRQ(ierr);
166310534d48SPatrick Farrell   ierr = PetscSectionViewFromOptions(patch->gtolCounts, (PetscObject) pc, option);CHKERRQ(ierr);
166410534d48SPatrick Farrell   ierr = ISViewFromOptions(patch->gtol, (PetscObject) pc, option);CHKERRQ(ierr);
16654bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArray, PETSC_OWN_POINTER, &patch->dofs);CHKERRQ(ierr);
16665f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArray, PETSC_OWN_POINTER, &patch->offs);CHKERRQ(ierr);
16670904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1668c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithArtificial, globalDofsArrayWithArtificial, PETSC_OWN_POINTER, &patch->gtolWithArtificial);CHKERRQ(ierr);
1669c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithArtificial, PETSC_OWN_POINTER, &patch->dofsWithArtificial);CHKERRQ(ierr);
1670c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithArtificial, PETSC_OWN_POINTER, &patch->offsWithArtificial);CHKERRQ(ierr);
1671c2e6f3c0SFlorian Wechsung   }
16720904074fSPatrick Farrell   if (isNonlinear) {
16730904074fSPatrick Farrell     ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithAll, globalDofsArrayWithAll, PETSC_OWN_POINTER, &patch->gtolWithAll);CHKERRQ(ierr);
16740904074fSPatrick Farrell     ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithAll, PETSC_OWN_POINTER, &patch->dofsWithAll);CHKERRQ(ierr);
16750904074fSPatrick Farrell     ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithAll, PETSC_OWN_POINTER, &patch->offsWithAll);CHKERRQ(ierr);
16760904074fSPatrick Farrell   }
16774bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
16784bbf5ea8SMatthew G. Knepley }
16794bbf5ea8SMatthew G. Knepley 
1680c2e6f3c0SFlorian Wechsung static PetscErrorCode PCPatchCreateMatrix_Private(PC pc, PetscInt point, Mat *mat, PetscBool withArtificial)
16814bbf5ea8SMatthew G. Knepley {
16824bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
16834bbf5ea8SMatthew G. Knepley   Vec            x, y;
16844bbf5ea8SMatthew G. Knepley   PetscBool      flg;
16854bbf5ea8SMatthew G. Knepley   PetscInt       csize, rsize;
16864bbf5ea8SMatthew G. Knepley   const char    *prefix = NULL;
16874bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
16884bbf5ea8SMatthew G. Knepley 
16894bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1690c2e6f3c0SFlorian Wechsung   if(withArtificial) {
1691e047a90bSFlorian Wechsung     /* would be nice if we could create a rectangular matrix of size numDofsWithArtificial x numDofs here */
16921202d238SPatrick Farrell     x = patch->patchRHSWithArtificial[point];
16931202d238SPatrick Farrell     y = patch->patchRHSWithArtificial[point];
1694ff201f6aSFlorian Wechsung   } else {
16951202d238SPatrick Farrell     x = patch->patchRHS[point];
16961202d238SPatrick Farrell     y = patch->patchUpdate[point];
1697c2e6f3c0SFlorian Wechsung   }
1698c2e6f3c0SFlorian Wechsung 
16994bbf5ea8SMatthew G. Knepley   ierr = VecGetSize(x, &csize);CHKERRQ(ierr);
17004bbf5ea8SMatthew G. Knepley   ierr = VecGetSize(y, &rsize);CHKERRQ(ierr);
17014bbf5ea8SMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, mat);CHKERRQ(ierr);
17024bbf5ea8SMatthew G. Knepley   ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr);
17034bbf5ea8SMatthew G. Knepley   ierr = MatSetOptionsPrefix(*mat, prefix);CHKERRQ(ierr);
17045f824522SMatthew G. Knepley   ierr = MatAppendOptionsPrefix(*mat, "pc_patch_sub_");CHKERRQ(ierr);
17054bbf5ea8SMatthew G. Knepley   if (patch->sub_mat_type)       {ierr = MatSetType(*mat, patch->sub_mat_type);CHKERRQ(ierr);}
17067974b488SMatthew G. Knepley   else if (!patch->sub_mat_type) {ierr = MatSetType(*mat, MATDENSE);CHKERRQ(ierr);}
17074bbf5ea8SMatthew G. Knepley   ierr = MatSetSizes(*mat, rsize, csize, rsize, csize);CHKERRQ(ierr);
17084bbf5ea8SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) *mat, MATDENSE, &flg);CHKERRQ(ierr);
17094bbf5ea8SMatthew G. Knepley   if (!flg) {ierr = PetscObjectTypeCompare((PetscObject)*mat, MATSEQDENSE, &flg);CHKERRQ(ierr);}
17104bbf5ea8SMatthew G. Knepley   /* Sparse patch matrices */
17114bbf5ea8SMatthew G. Knepley   if (!flg) {
17124bbf5ea8SMatthew G. Knepley     PetscBT         bt;
17134bbf5ea8SMatthew G. Knepley     PetscInt       *dnnz      = NULL;
17144bbf5ea8SMatthew G. Knepley     const PetscInt *dofsArray = NULL;
17154bbf5ea8SMatthew G. Knepley     PetscInt        pStart, pEnd, ncell, offset, c, i, j;
17164bbf5ea8SMatthew G. Knepley 
1717c2e6f3c0SFlorian Wechsung     if(withArtificial) {
1718c2e6f3c0SFlorian Wechsung       ierr = ISGetIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1719ff201f6aSFlorian Wechsung     } else {
17204bbf5ea8SMatthew G. Knepley       ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1721c2e6f3c0SFlorian Wechsung     }
17224bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
17234bbf5ea8SMatthew G. Knepley     point += pStart;
17244bbf5ea8SMatthew 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);
17254bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
17264bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
17274bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventBegin(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr);
1728b2866507SPatrick Farrell     /* A PetscBT uses N^2 bits to store the sparsity pattern on a
17294bbf5ea8SMatthew G. Knepley      * patch. This is probably OK if the patches are not too big,
1730b2866507SPatrick Farrell      * but uses too much memory. We therefore switch based on rsize. */
1731b2866507SPatrick Farrell     if (rsize < 3000) { /* FIXME: I picked this switch value out of my hat */
1732d63cebbaSPatrick Farrell       PetscScalar *zeroes;
1733d63cebbaSPatrick Farrell       PetscInt rows;
1734d63cebbaSPatrick Farrell 
1735b2866507SPatrick Farrell       ierr = PetscCalloc1(rsize, &dnnz);CHKERRQ(ierr);
17364bbf5ea8SMatthew G. Knepley       ierr = PetscBTCreate(rsize*rsize, &bt);CHKERRQ(ierr);
17374bbf5ea8SMatthew G. Knepley       for (c = 0; c < ncell; ++c) {
17384bbf5ea8SMatthew G. Knepley         const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell;
17394bbf5ea8SMatthew G. Knepley         for (i = 0; i < patch->totalDofsPerCell; ++i) {
17404bbf5ea8SMatthew G. Knepley           const PetscInt row = idx[i];
1741557beb66SLawrence Mitchell           if (row < 0) continue;
17424bbf5ea8SMatthew G. Knepley           for (j = 0; j < patch->totalDofsPerCell; ++j) {
17434bbf5ea8SMatthew G. Knepley             const PetscInt col = idx[j];
17444bbf5ea8SMatthew G. Knepley             const PetscInt key = row*rsize + col;
1745557beb66SLawrence Mitchell             if (col < 0) continue;
17464bbf5ea8SMatthew G. Knepley             if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
17474bbf5ea8SMatthew G. Knepley           }
17484bbf5ea8SMatthew G. Knepley         }
17494bbf5ea8SMatthew G. Knepley       }
1750d63cebbaSPatrick Farrell 
1751d63cebbaSPatrick Farrell       if (patch->usercomputeopintfacet) {
1752d63cebbaSPatrick Farrell         const PetscInt *intFacetsArray = NULL;
1753d63cebbaSPatrick Farrell         PetscInt i, numIntFacets, intFacetOffset;
1754d63cebbaSPatrick Farrell         const PetscInt *facetCells = NULL;
1755d63cebbaSPatrick Farrell 
1756d63cebbaSPatrick Farrell         ierr = PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets);CHKERRQ(ierr);
1757d63cebbaSPatrick Farrell         ierr = PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset);CHKERRQ(ierr);
1758d63cebbaSPatrick Farrell         ierr = ISGetIndices(patch->intFacetsToPatchCell, &facetCells);CHKERRQ(ierr);
1759d63cebbaSPatrick Farrell         ierr = ISGetIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
1760d63cebbaSPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
1761d63cebbaSPatrick Farrell           const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0];
1762d63cebbaSPatrick Farrell           const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1];
1763d63cebbaSPatrick Farrell           PetscInt celli, cellj;
1764d63cebbaSPatrick Farrell 
1765d63cebbaSPatrick Farrell           for (celli = 0; celli < patch->totalDofsPerCell; celli++) {
1766d63cebbaSPatrick Farrell             const PetscInt row = dofsArray[(offset + cell0)*patch->totalDofsPerCell + celli];
1767b5c64f08SPatrick Farrell             if (row < 0) continue;
1768d63cebbaSPatrick Farrell             for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) {
1769d63cebbaSPatrick Farrell                 const PetscInt col = dofsArray[(offset + cell1)*patch->totalDofsPerCell + cellj];
1770d63cebbaSPatrick Farrell                 const PetscInt key = row*rsize + col;
1771d63cebbaSPatrick Farrell                 if (col < 0) continue;
1772d63cebbaSPatrick Farrell                 if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
1773d63cebbaSPatrick Farrell             }
1774d63cebbaSPatrick Farrell           }
1775d63cebbaSPatrick Farrell 
1776d63cebbaSPatrick Farrell           for (celli = 0; celli < patch->totalDofsPerCell; celli++) {
1777d63cebbaSPatrick Farrell             const PetscInt row = dofsArray[(offset + cell1)*patch->totalDofsPerCell + celli];
1778b5c64f08SPatrick Farrell             if (row < 0) continue;
1779d63cebbaSPatrick Farrell             for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) {
1780d63cebbaSPatrick Farrell                 const PetscInt col = dofsArray[(offset + cell0)*patch->totalDofsPerCell + cellj];
1781d63cebbaSPatrick Farrell                 const PetscInt key = row*rsize + col;
1782d63cebbaSPatrick Farrell                 if (col < 0) continue;
1783d63cebbaSPatrick Farrell                 if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
1784d63cebbaSPatrick Farrell             }
1785d63cebbaSPatrick Farrell           }
1786d63cebbaSPatrick Farrell         }
1787d63cebbaSPatrick Farrell       }
17884bbf5ea8SMatthew G. Knepley       ierr = PetscBTDestroy(&bt);CHKERRQ(ierr);
17894bbf5ea8SMatthew G. Knepley       ierr = MatXAIJSetPreallocation(*mat, 1, dnnz, NULL, NULL, NULL);CHKERRQ(ierr);
17904bbf5ea8SMatthew G. Knepley       ierr = PetscFree(dnnz);CHKERRQ(ierr);
1791d63cebbaSPatrick Farrell 
1792d63cebbaSPatrick Farrell       ierr = PetscCalloc1(patch->totalDofsPerCell*patch->totalDofsPerCell, &zeroes);CHKERRQ(ierr);
1793d63cebbaSPatrick Farrell       for (c = 0; c < ncell; ++c) {
1794d63cebbaSPatrick Farrell         const PetscInt *idx = &dofsArray[(offset + c)*patch->totalDofsPerCell];
1795d63cebbaSPatrick Farrell         ierr = MatSetValues(*mat, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, zeroes, INSERT_VALUES);CHKERRQ(ierr);
1796d63cebbaSPatrick Farrell       }
1797d63cebbaSPatrick Farrell       ierr = MatGetLocalSize(*mat, &rows, NULL);CHKERRQ(ierr);
1798d63cebbaSPatrick Farrell       for (i = 0; i < rows; ++i) {
1799d63cebbaSPatrick Farrell         ierr = MatSetValues(*mat, 1, &i, 1, &i, zeroes, INSERT_VALUES);CHKERRQ(ierr);
1800d63cebbaSPatrick Farrell       }
1801d63cebbaSPatrick Farrell 
1802d63cebbaSPatrick Farrell       if (patch->usercomputeopintfacet) {
1803d63cebbaSPatrick Farrell         const PetscInt *intFacetsArray = NULL;
1804d63cebbaSPatrick Farrell         PetscInt i, numIntFacets, intFacetOffset;
1805d63cebbaSPatrick Farrell         const PetscInt *facetCells = NULL;
1806d63cebbaSPatrick Farrell 
1807d63cebbaSPatrick Farrell         ierr = PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets);CHKERRQ(ierr);
1808d63cebbaSPatrick Farrell         ierr = PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset);CHKERRQ(ierr);
1809d63cebbaSPatrick Farrell         ierr = ISGetIndices(patch->intFacetsToPatchCell, &facetCells);CHKERRQ(ierr);
1810d63cebbaSPatrick Farrell         ierr = ISGetIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
1811d63cebbaSPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
1812d63cebbaSPatrick Farrell           const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0];
1813d63cebbaSPatrick Farrell           const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1];
1814d63cebbaSPatrick Farrell           const PetscInt *cell0idx = &dofsArray[(offset + cell0)*patch->totalDofsPerCell];
1815d63cebbaSPatrick Farrell           const PetscInt *cell1idx = &dofsArray[(offset + cell1)*patch->totalDofsPerCell];
1816d63cebbaSPatrick Farrell           ierr = MatSetValues(*mat, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, zeroes, INSERT_VALUES);CHKERRQ(ierr);
1817d63cebbaSPatrick Farrell           ierr = MatSetValues(*mat, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, zeroes, INSERT_VALUES);CHKERRQ(ierr);
1818d63cebbaSPatrick Farrell         }
1819d63cebbaSPatrick Farrell       }
1820d63cebbaSPatrick Farrell 
1821d63cebbaSPatrick Farrell       ierr = MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1822d63cebbaSPatrick Farrell       ierr = MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1823d63cebbaSPatrick Farrell 
1824d63cebbaSPatrick Farrell       ierr = PetscFree(zeroes);CHKERRQ(ierr);
1825d63cebbaSPatrick Farrell 
1826b2866507SPatrick Farrell     } else { /* rsize too big, use MATPREALLOCATOR */
1827b2866507SPatrick Farrell       Mat preallocator;
1828b2866507SPatrick Farrell       PetscScalar* vals;
1829b2866507SPatrick Farrell 
1830b2866507SPatrick Farrell       ierr = PetscCalloc1(patch->totalDofsPerCell*patch->totalDofsPerCell, &vals);CHKERRQ(ierr);
1831b2866507SPatrick Farrell       ierr = MatCreate(PETSC_COMM_SELF, &preallocator);CHKERRQ(ierr);
1832b2866507SPatrick Farrell       ierr = MatSetType(preallocator, MATPREALLOCATOR);CHKERRQ(ierr);
1833b2866507SPatrick Farrell       ierr = MatSetSizes(preallocator, rsize, rsize, rsize, rsize);CHKERRQ(ierr);
1834b2866507SPatrick Farrell       ierr = MatSetUp(preallocator);CHKERRQ(ierr);
183511bcd083SPatrick Farrell 
1836b2866507SPatrick Farrell       for (c = 0; c < ncell; ++c) {
1837b2866507SPatrick Farrell         const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell;
1838b2866507SPatrick Farrell         ierr = MatSetValues(preallocator, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, vals, INSERT_VALUES);CHKERRQ(ierr);
1839b2866507SPatrick Farrell       }
184011bcd083SPatrick Farrell 
184111bcd083SPatrick Farrell       if (patch->usercomputeopintfacet) {
184211bcd083SPatrick Farrell         const PetscInt *intFacetsArray = NULL;
184311bcd083SPatrick Farrell         PetscInt i, numIntFacets, intFacetOffset;
184411bcd083SPatrick Farrell         const PetscInt *facetCells = NULL;
184511bcd083SPatrick Farrell 
184611bcd083SPatrick Farrell         ierr = PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets);CHKERRQ(ierr);
184711bcd083SPatrick Farrell         ierr = PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset);CHKERRQ(ierr);
184811bcd083SPatrick Farrell         ierr = ISGetIndices(patch->intFacetsToPatchCell, &facetCells);CHKERRQ(ierr);
184911bcd083SPatrick Farrell         ierr = ISGetIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
185011bcd083SPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
185111bcd083SPatrick Farrell           const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0];
185211bcd083SPatrick Farrell           const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1];
185311bcd083SPatrick Farrell           const PetscInt *cell0idx = &dofsArray[(offset + cell0)*patch->totalDofsPerCell];
185411bcd083SPatrick Farrell           const PetscInt *cell1idx = &dofsArray[(offset + cell1)*patch->totalDofsPerCell];
185511bcd083SPatrick Farrell           ierr = MatSetValues(preallocator, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, vals, INSERT_VALUES);CHKERRQ(ierr);
185611bcd083SPatrick Farrell           ierr = MatSetValues(preallocator, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, vals, INSERT_VALUES);CHKERRQ(ierr);
185711bcd083SPatrick Farrell         }
185811bcd083SPatrick Farrell       }
185911bcd083SPatrick Farrell 
1860b2866507SPatrick Farrell       ierr = PetscFree(vals);CHKERRQ(ierr);
1861b2866507SPatrick Farrell       ierr = MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1862b2866507SPatrick Farrell       ierr = MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1863b2866507SPatrick Farrell       ierr = MatPreallocatorPreallocate(preallocator, PETSC_TRUE, *mat);CHKERRQ(ierr);
1864b2866507SPatrick Farrell       ierr = MatDestroy(&preallocator);CHKERRQ(ierr);
1865b2866507SPatrick Farrell     }
18664bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr);
1867fe117d09SFlorian Wechsung     if(withArtificial) {
1868fe117d09SFlorian Wechsung       ierr = ISRestoreIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1869fe117d09SFlorian Wechsung     } else {
18704bbf5ea8SMatthew G. Knepley       ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
18714bbf5ea8SMatthew G. Knepley     }
1872fe117d09SFlorian Wechsung   }
18734bbf5ea8SMatthew G. Knepley   ierr = MatSetUp(*mat);CHKERRQ(ierr);
18744bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
18754bbf5ea8SMatthew G. Knepley }
18764bbf5ea8SMatthew G. Knepley 
18770904074fSPatrick 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)
187892d50984SMatthew G. Knepley {
187992d50984SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
188092d50984SMatthew G. Knepley   DM              dm;
188192d50984SMatthew G. Knepley   PetscSection    s;
188292d50984SMatthew G. Knepley   const PetscInt *parray, *oarray;
188392d50984SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
188492d50984SMatthew G. Knepley   PetscErrorCode  ierr;
188592d50984SMatthew G. Knepley 
188692d50984SMatthew G. Knepley   PetscFunctionBegin;
188792d50984SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
188892d50984SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
188992d50984SMatthew G. Knepley   /* Set offset into patch */
189092d50984SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->pointCounts, patchNum, &Np);CHKERRQ(ierr);
189192d50984SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->pointCounts, patchNum, &poff);CHKERRQ(ierr);
189292d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->points, &parray);CHKERRQ(ierr);
189392d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->offs,   &oarray);CHKERRQ(ierr);
189492d50984SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
189592d50984SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
189692d50984SMatthew G. Knepley       const PetscInt point = parray[poff+p];
189792d50984SMatthew G. Knepley       PetscInt       dof;
189892d50984SMatthew G. Knepley 
189992d50984SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(patch->patchSection, point, f, &dof);CHKERRQ(ierr);
190092d50984SMatthew G. Knepley       ierr = PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);
190192d50984SMatthew G. Knepley       if (patch->nsubspaces == 1) {ierr = PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);}
190292d50984SMatthew G. Knepley       else                        {ierr = PetscSectionSetOffset(patch->patchSection, point, -1);CHKERRQ(ierr);}
190392d50984SMatthew G. Knepley     }
190492d50984SMatthew G. Knepley   }
190592d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->points, &parray);CHKERRQ(ierr);
190692d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->offs,   &oarray);CHKERRQ(ierr);
190792d50984SMatthew G. Knepley   if (patch->viewSection) {ierr = ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection);CHKERRQ(ierr);}
190892d50984SMatthew G. Knepley   ierr = DMPlexComputeResidual_Patch_Internal(pc->dm, patch->patchSection, cellIS, 0.0, x, NULL, F, ctx);CHKERRQ(ierr);
190992d50984SMatthew G. Knepley   PetscFunctionReturn(0);
191092d50984SMatthew G. Knepley }
191192d50984SMatthew G. Knepley 
191292d50984SMatthew G. Knepley PetscErrorCode PCPatchComputeFunction_Internal(PC pc, Vec x, Vec F, PetscInt point)
191392d50984SMatthew G. Knepley {
191492d50984SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
191592d50984SMatthew G. Knepley   const PetscInt *dofsArray;
19160904074fSPatrick Farrell   const PetscInt *dofsArrayWithAll;
191792d50984SMatthew G. Knepley   const PetscInt *cellsArray;
191892d50984SMatthew G. Knepley   PetscInt        ncell, offset, pStart, pEnd;
191992d50984SMatthew G. Knepley   PetscErrorCode  ierr;
192092d50984SMatthew G. Knepley 
192192d50984SMatthew G. Knepley   PetscFunctionBegin;
192292d50984SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
192392d50984SMatthew G. Knepley   if (!patch->usercomputeop) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback\n");
192492d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
19250904074fSPatrick Farrell   ierr = ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
192692d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
192792d50984SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
192892d50984SMatthew G. Knepley 
192992d50984SMatthew G. Knepley   point += pStart;
193092d50984SMatthew 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);
193192d50984SMatthew G. Knepley 
193292d50984SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
193392d50984SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
193492d50984SMatthew G. Knepley   if (ncell <= 0) {
193592d50984SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
193692d50984SMatthew G. Knepley     PetscFunctionReturn(0);
193792d50984SMatthew G. Knepley   }
193892d50984SMatthew G. Knepley   PetscStackPush("PCPatch user callback");
193992d50984SMatthew G. Knepley   /* Cannot reuse the same IS because the geometry info is being cached in it */
194092d50984SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS);CHKERRQ(ierr);
194139fd2e8aSPatrick Farrell   ierr = patch->usercomputef(pc, point, x, F, patch->cellIS, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell,
19420904074fSPatrick Farrell                                                                                             dofsArrayWithAll + offset*patch->totalDofsPerCell,
194339fd2e8aSPatrick Farrell                                                                                             patch->usercomputefctx);CHKERRQ(ierr);
194492d50984SMatthew G. Knepley   PetscStackPop;
194592d50984SMatthew G. Knepley   ierr = ISDestroy(&patch->cellIS);CHKERRQ(ierr);
194692d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
19470904074fSPatrick Farrell   ierr = ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
194892d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
194992d50984SMatthew G. Knepley   if (patch->viewMatrix) {
195092d50984SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
195192d50984SMatthew G. Knepley 
195292d50984SMatthew G. Knepley     ierr = PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch vector for Point %D", point);CHKERRQ(ierr);
195392d50984SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) F, name);CHKERRQ(ierr);
195492d50984SMatthew G. Knepley     ierr = ObjectView((PetscObject) F, patch->viewerMatrix, patch->formatMatrix);CHKERRQ(ierr);
195592d50984SMatthew G. Knepley   }
195692d50984SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
195792d50984SMatthew G. Knepley   PetscFunctionReturn(0);
195892d50984SMatthew G. Knepley }
195992d50984SMatthew G. Knepley 
19600904074fSPatrick 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)
19615f824522SMatthew G. Knepley {
19625f824522SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
19635f824522SMatthew G. Knepley   DM              dm;
19645f824522SMatthew G. Knepley   PetscSection    s;
19655f824522SMatthew G. Knepley   const PetscInt *parray, *oarray;
19665f824522SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
19675f824522SMatthew G. Knepley   PetscErrorCode  ierr;
19685f824522SMatthew G. Knepley 
19695f824522SMatthew G. Knepley   PetscFunctionBegin;
19705f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
19715f824522SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
19725f824522SMatthew G. Knepley   /* Set offset into patch */
19735f824522SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->pointCounts, patchNum, &Np);CHKERRQ(ierr);
19745f824522SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->pointCounts, patchNum, &poff);CHKERRQ(ierr);
19755f824522SMatthew G. Knepley   ierr = ISGetIndices(patch->points, &parray);CHKERRQ(ierr);
19765f824522SMatthew G. Knepley   ierr = ISGetIndices(patch->offs,   &oarray);CHKERRQ(ierr);
19775f824522SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
19785f824522SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
19795f824522SMatthew G. Knepley       const PetscInt point = parray[poff+p];
19805f824522SMatthew G. Knepley       PetscInt       dof;
19815f824522SMatthew G. Knepley 
19825f824522SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(patch->patchSection, point, f, &dof);CHKERRQ(ierr);
19835f824522SMatthew G. Knepley       ierr = PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);
19845f824522SMatthew G. Knepley       if (patch->nsubspaces == 1) {ierr = PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);}
19855f824522SMatthew G. Knepley       else                        {ierr = PetscSectionSetOffset(patch->patchSection, point, -1);CHKERRQ(ierr);}
19865f824522SMatthew G. Knepley     }
19875f824522SMatthew G. Knepley   }
19885f824522SMatthew G. Knepley   ierr = ISRestoreIndices(patch->points, &parray);CHKERRQ(ierr);
19895f824522SMatthew G. Knepley   ierr = ISRestoreIndices(patch->offs,   &oarray);CHKERRQ(ierr);
19905f824522SMatthew G. Knepley   if (patch->viewSection) {ierr = ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection);CHKERRQ(ierr);}
19915f824522SMatthew G. Knepley   /* TODO Shut off MatViewFromOptions() in MatAssemblyEnd() here */
1992723f9013SMatthew G. Knepley   ierr = DMPlexComputeJacobian_Patch_Internal(pc->dm, patch->patchSection, patch->patchSection, cellIS, 0.0, 0.0, x, NULL, J, J, ctx);CHKERRQ(ierr);
19935f824522SMatthew G. Knepley   PetscFunctionReturn(0);
19945f824522SMatthew G. Knepley }
19955f824522SMatthew G. Knepley 
199634d8b122SPatrick Farrell PetscErrorCode PCPatchComputeOperator_Internal(PC pc, Vec x, Mat mat, PetscInt point, PetscBool withArtificial)
19974bbf5ea8SMatthew G. Knepley {
19984bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
19994bbf5ea8SMatthew G. Knepley   const PetscInt *dofsArray;
20004d04e9f1SPatrick Farrell   const PetscInt *dofsArrayWithArtificial = NULL;
20010904074fSPatrick Farrell   const PetscInt *dofsArrayWithAll = NULL;
20024bbf5ea8SMatthew G. Knepley   const PetscInt *cellsArray;
2003eb62eeaaSLawrence Mitchell   PetscInt        ncell, offset, pStart, pEnd, numIntFacets, intFacetOffset;
20044d04e9f1SPatrick Farrell   PetscBool       isNonlinear;
20054bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
20064bbf5ea8SMatthew G. Knepley 
20074bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
20084bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
2009debbdec3SPatrick Farrell   isNonlinear = patch->isNonlinear;
20104bbf5ea8SMatthew G. Knepley   if (!patch->usercomputeop) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback\n");
2011c2e6f3c0SFlorian Wechsung   if(withArtificial) {
2012c2e6f3c0SFlorian Wechsung     ierr = ISGetIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
2013c2e6f3c0SFlorian Wechsung   } else {
20144bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
2015c2e6f3c0SFlorian Wechsung   }
20164d04e9f1SPatrick Farrell   if (isNonlinear) {
20170904074fSPatrick Farrell     ierr = ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
20184d04e9f1SPatrick Farrell   }
20194bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
20204bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
20214bbf5ea8SMatthew G. Knepley 
20224bbf5ea8SMatthew G. Knepley   point += pStart;
20234bbf5ea8SMatthew 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);
20244bbf5ea8SMatthew G. Knepley 
20254bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
20264bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
20274bbf5ea8SMatthew G. Knepley   if (ncell <= 0) {
20284bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
20294bbf5ea8SMatthew G. Knepley     PetscFunctionReturn(0);
20304bbf5ea8SMatthew G. Knepley   }
20314bbf5ea8SMatthew G. Knepley   PetscStackPush("PCPatch user callback");
20322aa6f319SMatthew G. Knepley   /* Cannot reuse the same IS because the geometry info is being cached in it */
20332aa6f319SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS);CHKERRQ(ierr);
20340904074fSPatrick 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);
2035eb62eeaaSLawrence Mitchell 
203659109abcSLawrence Mitchell   if (patch->usercomputeopintfacet) {
2037eb62eeaaSLawrence Mitchell     ierr = PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets);CHKERRQ(ierr);
2038eb62eeaaSLawrence Mitchell     ierr = PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset);CHKERRQ(ierr);
2039eb62eeaaSLawrence Mitchell     if (numIntFacets > 0) {
2040eb62eeaaSLawrence Mitchell       /* For each interior facet, grab the two cells (in local numbering, and concatenate dof numberings for those cells) */
2041eb62eeaaSLawrence Mitchell       PetscInt *facetDofs = NULL, *facetDofsWithAll = NULL;
2042eb62eeaaSLawrence Mitchell       const PetscInt *intFacetsArray = NULL;
2043eb62eeaaSLawrence Mitchell       PetscInt idx = 0;
2044eb62eeaaSLawrence Mitchell       PetscInt i, c, d;
2045eb62eeaaSLawrence Mitchell       IS       facetIS = NULL;
2046eb62eeaaSLawrence Mitchell       const PetscInt *facetCells = NULL;
2047eb62eeaaSLawrence Mitchell       ierr = ISGetIndices(patch->intFacetsToPatchCell, &facetCells);CHKERRQ(ierr);
2048eb62eeaaSLawrence Mitchell       ierr = ISGetIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
2049eb62eeaaSLawrence Mitchell       /* FIXME: Pull this malloc out. */
2050eb62eeaaSLawrence Mitchell       ierr = PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofs);CHKERRQ(ierr);
2051eb62eeaaSLawrence Mitchell       if (dofsArrayWithAll) {
2052eb62eeaaSLawrence Mitchell         ierr = PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofsWithAll);CHKERRQ(ierr);
2053eb62eeaaSLawrence Mitchell       }
2054eb62eeaaSLawrence Mitchell       /*
2055eb62eeaaSLawrence Mitchell        * 0--1
2056eb62eeaaSLawrence Mitchell        * |\-|
2057eb62eeaaSLawrence Mitchell        * |+\|
2058eb62eeaaSLawrence Mitchell        * 2--3
2059eb62eeaaSLawrence Mitchell        * [0, 2, 3, 0, 1, 3]
2060eb62eeaaSLawrence Mitchell        */
2061eb62eeaaSLawrence Mitchell       for (i = 0; i < numIntFacets; i++) {
2062eb62eeaaSLawrence Mitchell         for (c = 0; c < 2; c++) {
2063eb62eeaaSLawrence Mitchell           const PetscInt cell = facetCells[2*(intFacetOffset + i) + c];
2064eb62eeaaSLawrence Mitchell           for (d = 0; d < patch->totalDofsPerCell; d++) {
2065eb62eeaaSLawrence Mitchell             facetDofs[idx] = dofsArray[(offset + cell)*patch->totalDofsPerCell + d];
2066eb62eeaaSLawrence Mitchell             if (dofsArrayWithAll) {
2067eb62eeaaSLawrence Mitchell               facetDofsWithAll[idx] = dofsArrayWithAll[(offset + cell)*patch->totalDofsPerCell + d];
2068eb62eeaaSLawrence Mitchell             }
2069eb62eeaaSLawrence Mitchell             idx++;
2070eb62eeaaSLawrence Mitchell           }
2071eb62eeaaSLawrence Mitchell         }
2072eb62eeaaSLawrence Mitchell       }
2073eb62eeaaSLawrence Mitchell       ierr = ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray + intFacetOffset, PETSC_USE_POINTER, &facetIS);CHKERRQ(ierr);
2074ec0d443bSLawrence Mitchell       ierr = patch->usercomputeopintfacet(pc, point, x, mat, facetIS, 2*numIntFacets*patch->totalDofsPerCell, facetDofs, facetDofsWithAll, patch->usercomputeopintfacetctx);CHKERRQ(ierr);
2075eb62eeaaSLawrence Mitchell       ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
2076eb62eeaaSLawrence Mitchell       ierr = ISRestoreIndices(patch->intFacetsToPatchCell, &facetCells);CHKERRQ(ierr);
2077eb62eeaaSLawrence Mitchell       ierr = PetscFree(facetDofs);CHKERRQ(ierr);
2078eb62eeaaSLawrence Mitchell       ierr = PetscFree(facetDofsWithAll);CHKERRQ(ierr);
2079eb62eeaaSLawrence Mitchell     }
208059109abcSLawrence Mitchell   }
20816710cc29SPatrick Farrell 
20826710cc29SPatrick Farrell   ierr = MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20836710cc29SPatrick Farrell   ierr = MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20846710cc29SPatrick Farrell 
20854bbf5ea8SMatthew G. Knepley   PetscStackPop;
20862aa6f319SMatthew G. Knepley   ierr = ISDestroy(&patch->cellIS);CHKERRQ(ierr);
20874d04e9f1SPatrick Farrell   if(withArtificial) {
2088c2e6f3c0SFlorian Wechsung     ierr = ISRestoreIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
2089c2e6f3c0SFlorian Wechsung   } else {
20904bbf5ea8SMatthew G. Knepley     ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
2091c2e6f3c0SFlorian Wechsung   }
20924d04e9f1SPatrick Farrell   if (isNonlinear) {
20930904074fSPatrick Farrell     ierr = ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
20944d04e9f1SPatrick Farrell   }
20954bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
20962aa6f319SMatthew G. Knepley   if (patch->viewMatrix) {
20972aa6f319SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
20982aa6f319SMatthew G. Knepley 
20992aa6f319SMatthew G. Knepley     ierr = PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch matrix for Point %D", point);CHKERRQ(ierr);
21002aa6f319SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) mat, name);CHKERRQ(ierr);
21012aa6f319SMatthew G. Knepley     ierr = ObjectView((PetscObject) mat, patch->viewerMatrix, patch->formatMatrix);CHKERRQ(ierr);
21022aa6f319SMatthew G. Knepley   }
21034bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
21044bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
21054bbf5ea8SMatthew G. Knepley }
21064bbf5ea8SMatthew G. Knepley 
21070904074fSPatrick Farrell PetscErrorCode PCPatch_ScatterLocal_Private(PC pc, PetscInt p, Vec x, Vec y, InsertMode mode, ScatterMode scat, PatchScatterType scattertype)
21084bbf5ea8SMatthew G. Knepley {
21094bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch     = (PC_PATCH *) pc->data;
21104bbf5ea8SMatthew G. Knepley   const PetscScalar *xArray    = NULL;
21114bbf5ea8SMatthew G. Knepley   PetscScalar       *yArray    = NULL;
21124bbf5ea8SMatthew G. Knepley   const PetscInt    *gtolArray = NULL;
21134bbf5ea8SMatthew G. Knepley   PetscInt           dof, offset, lidx;
21144bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
21154bbf5ea8SMatthew G. Knepley 
21164bbf5ea8SMatthew G. Knepley   PetscFunctionBeginHot;
21174bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Scatter, pc, 0, 0, 0);CHKERRQ(ierr);
21184bbf5ea8SMatthew G. Knepley   ierr = VecGetArrayRead(x, &xArray);CHKERRQ(ierr);
21194bbf5ea8SMatthew G. Knepley   ierr = VecGetArray(y, &yArray);CHKERRQ(ierr);
21200904074fSPatrick Farrell   if (scattertype == SCATTER_WITHARTIFICIAL) {
2121c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof);CHKERRQ(ierr);
2122c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offset);CHKERRQ(ierr);
2123c2e6f3c0SFlorian Wechsung     ierr = ISGetIndices(patch->gtolWithArtificial, &gtolArray);CHKERRQ(ierr);
21240904074fSPatrick Farrell   } else if (scattertype == SCATTER_WITHALL) {
21250904074fSPatrick Farrell     ierr = PetscSectionGetDof(patch->gtolCountsWithAll, p, &dof);CHKERRQ(ierr);
21260904074fSPatrick Farrell     ierr = PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offset);CHKERRQ(ierr);
21270904074fSPatrick Farrell     ierr = ISGetIndices(patch->gtolWithAll, &gtolArray);CHKERRQ(ierr);
2128c2e6f3c0SFlorian Wechsung   } else {
21294bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr);
21304bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
21314bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
2132c2e6f3c0SFlorian Wechsung   }
21334bbf5ea8SMatthew 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");
21344bbf5ea8SMatthew 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");
21354bbf5ea8SMatthew G. Knepley   for (lidx = 0; lidx < dof; ++lidx) {
21364bbf5ea8SMatthew G. Knepley     const PetscInt gidx = gtolArray[offset+lidx];
21374bbf5ea8SMatthew G. Knepley 
21384bbf5ea8SMatthew G. Knepley     if (mode == INSERT_VALUES) yArray[lidx]  = xArray[gidx]; /* Forward */
21394bbf5ea8SMatthew G. Knepley     else                       yArray[gidx] += xArray[lidx]; /* Reverse */
21404bbf5ea8SMatthew G. Knepley   }
21410904074fSPatrick Farrell   if (scattertype == SCATTER_WITHARTIFICIAL) {
2142c2e6f3c0SFlorian Wechsung     ierr = ISRestoreIndices(patch->gtolWithArtificial, &gtolArray);CHKERRQ(ierr);
21430904074fSPatrick Farrell   } else if (scattertype == SCATTER_WITHALL) {
21440904074fSPatrick Farrell     ierr = ISRestoreIndices(patch->gtolWithAll, &gtolArray);CHKERRQ(ierr);
2145c2e6f3c0SFlorian Wechsung   } else {
21464bbf5ea8SMatthew G. Knepley     ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
2147c2e6f3c0SFlorian Wechsung   }
21484bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArrayRead(x, &xArray);CHKERRQ(ierr);
21494bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArray(y, &yArray);CHKERRQ(ierr);
21504bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Scatter, pc, 0, 0, 0);CHKERRQ(ierr);
21514bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
21524bbf5ea8SMatthew G. Knepley }
21534bbf5ea8SMatthew G. Knepley 
2154dadc69c5SMatthew G. Knepley static PetscErrorCode PCSetUp_PATCH_Linear(PC pc)
2155dadc69c5SMatthew G. Knepley {
2156dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2157dadc69c5SMatthew G. Knepley   const char    *prefix;
2158dadc69c5SMatthew G. Knepley   PetscInt       i;
2159dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2160dadc69c5SMatthew G. Knepley 
2161dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2162dadc69c5SMatthew G. Knepley   if (!pc->setupcalled) {
2163dadc69c5SMatthew G. Knepley     ierr = PetscMalloc1(patch->npatch, &patch->solver);CHKERRQ(ierr);
2164dadc69c5SMatthew G. Knepley     ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr);
2165dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {
2166dadc69c5SMatthew G. Knepley       KSP ksp;
2167dadc69c5SMatthew G. Knepley       PC  subpc;
2168dadc69c5SMatthew G. Knepley 
2169dadc69c5SMatthew G. Knepley       ierr = KSPCreate(PETSC_COMM_SELF, &ksp);CHKERRQ(ierr);
2170ddad275aSPatrick Farrell       ierr = KSPSetErrorIfNotConverged(ksp, pc->erroriffailure);CHKERRQ(ierr);
2171dadc69c5SMatthew G. Knepley       ierr = KSPSetOptionsPrefix(ksp, prefix);CHKERRQ(ierr);
2172dadc69c5SMatthew G. Knepley       ierr = KSPAppendOptionsPrefix(ksp, "sub_");CHKERRQ(ierr);
2173dadc69c5SMatthew G. Knepley       ierr = PetscObjectIncrementTabLevel((PetscObject) ksp, (PetscObject) pc, 1);CHKERRQ(ierr);
2174dadc69c5SMatthew G. Knepley       ierr = KSPGetPC(ksp, &subpc);CHKERRQ(ierr);
2175dadc69c5SMatthew G. Knepley       ierr = PetscObjectIncrementTabLevel((PetscObject) subpc, (PetscObject) pc, 1);CHKERRQ(ierr);
2176dadc69c5SMatthew G. Knepley       ierr = PetscLogObjectParent((PetscObject) pc, (PetscObject) ksp);CHKERRQ(ierr);
2177dadc69c5SMatthew G. Knepley       patch->solver[i] = (PetscObject) ksp;
2178dadc69c5SMatthew G. Knepley     }
2179dadc69c5SMatthew G. Knepley   }
2180dadc69c5SMatthew G. Knepley   if (patch->save_operators) {
2181dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {
2182dadc69c5SMatthew G. Knepley       ierr = MatZeroEntries(patch->mat[i]);CHKERRQ(ierr);
218334d8b122SPatrick Farrell       ierr = PCPatchComputeOperator_Internal(pc, NULL, patch->mat[i], i, PETSC_FALSE);CHKERRQ(ierr);
2184dadc69c5SMatthew G. Knepley       ierr = KSPSetOperators((KSP) patch->solver[i], patch->mat[i], patch->mat[i]);CHKERRQ(ierr);
2185dadc69c5SMatthew G. Knepley     }
2186dadc69c5SMatthew G. Knepley   }
218734d8b122SPatrick Farrell   if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
218834d8b122SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {
21891202d238SPatrick Farrell       /* Instead of padding patch->patchUpdate with zeros to get */
21901202d238SPatrick Farrell       /* patch->patchUpdateWithArtificial and then multiplying with the matrix, */
219134d8b122SPatrick Farrell       /* just get rid of the columns that correspond to the dofs with */
219234d8b122SPatrick Farrell       /* artificial bcs. That's of course fairly inefficient, hopefully we */
219334d8b122SPatrick Farrell       /* can just assemble the rectangular matrix in the first place. */
219434d8b122SPatrick Farrell       Mat matSquare;
219534d8b122SPatrick Farrell       IS rowis;
219634d8b122SPatrick Farrell       PetscInt dof;
219734d8b122SPatrick Farrell 
219834d8b122SPatrick Farrell       ierr = MatGetSize(patch->mat[i], &dof, NULL);CHKERRQ(ierr);
219934d8b122SPatrick Farrell       if (dof == 0) {
220034d8b122SPatrick Farrell         patch->matWithArtificial[i] = NULL;
220134d8b122SPatrick Farrell         continue;
220234d8b122SPatrick Farrell       }
220334d8b122SPatrick Farrell 
220434d8b122SPatrick Farrell       ierr = PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE);CHKERRQ(ierr);
220534d8b122SPatrick Farrell       ierr = MatZeroEntries(matSquare);CHKERRQ(ierr);
220634d8b122SPatrick Farrell       ierr = PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE);CHKERRQ(ierr);
220734d8b122SPatrick Farrell 
220834d8b122SPatrick Farrell       ierr = MatGetSize(matSquare, &dof, NULL);CHKERRQ(ierr);
220934d8b122SPatrick Farrell       ierr = ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis); CHKERRQ(ierr);
221034d8b122SPatrick Farrell       if(pc->setupcalled) {
221134d8b122SPatrick Farrell         ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_REUSE_MATRIX, &patch->matWithArtificial[i]); CHKERRQ(ierr);
221234d8b122SPatrick Farrell       } else {
221334d8b122SPatrick Farrell         ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &patch->matWithArtificial[i]); CHKERRQ(ierr);
221434d8b122SPatrick Farrell       }
221534d8b122SPatrick Farrell       ierr = ISDestroy(&rowis); CHKERRQ(ierr);
221634d8b122SPatrick Farrell       ierr = MatDestroy(&matSquare);CHKERRQ(ierr);
221734d8b122SPatrick Farrell     }
221834d8b122SPatrick Farrell   }
2219dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
2220dadc69c5SMatthew G. Knepley }
2221dadc69c5SMatthew G. Knepley 
22224bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUp_PATCH(PC pc)
22234bbf5ea8SMatthew G. Knepley {
22244bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2225557beb66SLawrence Mitchell   PetscInt       i;
222639fd2e8aSPatrick Farrell   PetscBool       isNonlinear;
22274bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
22284bbf5ea8SMatthew G. Knepley 
22294bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
22304bbf5ea8SMatthew G. Knepley   if (!pc->setupcalled) {
22314bbf5ea8SMatthew G. Knepley     PetscInt pStart, pEnd, p;
22324bbf5ea8SMatthew G. Knepley     PetscInt localSize;
22334bbf5ea8SMatthew G. Knepley 
22344bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventBegin(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr);
22354bbf5ea8SMatthew G. Knepley 
2236debbdec3SPatrick Farrell     isNonlinear = patch->isNonlinear;
22375f824522SMatthew G. Knepley     if (!patch->nsubspaces) {
22385f824522SMatthew G. Knepley       DM           dm;
22395f824522SMatthew G. Knepley       PetscDS      prob;
22405f824522SMatthew G. Knepley       PetscSection s;
2241e72c1634SMatthew G. Knepley       PetscInt     cStart, cEnd, c, Nf, f, numGlobalBcs = 0, *globalBcs, *Nb, totNb = 0, **cellDofs;
22425f824522SMatthew G. Knepley 
22435f824522SMatthew G. Knepley       ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
22445f824522SMatthew G. Knepley       if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONG, "Must set DM for PCPATCH or call PCPatchSetDiscretisationInfo()");
22455f824522SMatthew G. Knepley       ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
22465f824522SMatthew G. Knepley       ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
22475f824522SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
22485f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
22495f824522SMatthew G. Knepley         PetscInt cdof;
22505f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
22515f824522SMatthew G. Knepley         numGlobalBcs += cdof;
22525f824522SMatthew G. Knepley       }
22535f824522SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
22545f824522SMatthew G. Knepley       ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
22555f824522SMatthew G. Knepley       ierr = PetscMalloc3(Nf, &Nb, Nf, &cellDofs, numGlobalBcs, &globalBcs);CHKERRQ(ierr);
22565f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
22575f824522SMatthew G. Knepley         PetscFE        fe;
22585f824522SMatthew G. Knepley         PetscDualSpace sp;
22595f824522SMatthew G. Knepley         PetscInt       cdoff = 0;
22605f824522SMatthew G. Knepley 
22615f824522SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
22625f824522SMatthew G. Knepley         /* ierr = PetscFEGetNumComponents(fe, &Nc[f]);CHKERRQ(ierr); */
22635f824522SMatthew G. Knepley         ierr = PetscFEGetDualSpace(fe, &sp);CHKERRQ(ierr);
22645f824522SMatthew G. Knepley         ierr = PetscDualSpaceGetDimension(sp, &Nb[f]);CHKERRQ(ierr);
22655f824522SMatthew G. Knepley         totNb += Nb[f];
22665f824522SMatthew G. Knepley 
22675f824522SMatthew G. Knepley         ierr = PetscMalloc1((cEnd-cStart)*Nb[f], &cellDofs[f]);CHKERRQ(ierr);
22685f824522SMatthew G. Knepley         for (c = cStart; c < cEnd; ++c) {
22695f824522SMatthew G. Knepley           PetscInt *closure = NULL;
22705f824522SMatthew G. Knepley           PetscInt  clSize  = 0, cl;
22715f824522SMatthew G. Knepley 
22725f824522SMatthew G. Knepley           ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
22735f824522SMatthew G. Knepley           for (cl = 0; cl < clSize*2; cl += 2) {
22745f824522SMatthew G. Knepley             const PetscInt p = closure[cl];
22755f824522SMatthew G. Knepley             PetscInt       fdof, d, foff;
22765f824522SMatthew G. Knepley 
22775f824522SMatthew G. Knepley             ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr);
22785f824522SMatthew G. Knepley             ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr);
22795f824522SMatthew G. Knepley             for (d = 0; d < fdof; ++d, ++cdoff) cellDofs[f][cdoff] = foff + d;
22805f824522SMatthew G. Knepley           }
22815f824522SMatthew G. Knepley           ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
22825f824522SMatthew G. Knepley         }
22835f824522SMatthew 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]);
22845f824522SMatthew G. Knepley       }
22855f824522SMatthew G. Knepley       numGlobalBcs = 0;
22865f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
22875f824522SMatthew G. Knepley         const PetscInt *ind;
22885f824522SMatthew G. Knepley         PetscInt        off, cdof, d;
22895f824522SMatthew G. Knepley 
22905f824522SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
22915f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
22925f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &ind);CHKERRQ(ierr);
22935f824522SMatthew G. Knepley         for (d = 0; d < cdof; ++d) globalBcs[numGlobalBcs++] = off + ind[d];
22945f824522SMatthew G. Knepley       }
22955f824522SMatthew G. Knepley 
22965f824522SMatthew G. Knepley       ierr = PCPatchSetDiscretisationInfoCombined(pc, dm, Nb, (const PetscInt **) cellDofs, numGlobalBcs, globalBcs, numGlobalBcs, globalBcs);CHKERRQ(ierr);
22975f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
22985f824522SMatthew G. Knepley         ierr = PetscFree(cellDofs[f]);CHKERRQ(ierr);
22995f824522SMatthew G. Knepley       }
23005f824522SMatthew G. Knepley       ierr = PetscFree3(Nb, cellDofs, globalBcs);CHKERRQ(ierr);
230192d50984SMatthew G. Knepley       ierr = PCPatchSetComputeFunction(pc, PCPatchComputeFunction_DMPlex_Private, NULL);CHKERRQ(ierr);
23025f824522SMatthew G. Knepley       ierr = PCPatchSetComputeOperator(pc, PCPatchComputeOperator_DMPlex_Private, NULL);CHKERRQ(ierr);
23035f824522SMatthew G. Knepley     }
23045f824522SMatthew G. Knepley 
23054bbf5ea8SMatthew G. Knepley     localSize = patch->subspaceOffsets[patch->nsubspaces];
23061202d238SPatrick Farrell     ierr = VecCreateSeq(PETSC_COMM_SELF, localSize, &patch->localRHS);CHKERRQ(ierr);
23071202d238SPatrick Farrell     ierr = VecSetUp(patch->localRHS);CHKERRQ(ierr);
23081202d238SPatrick Farrell     ierr = VecDuplicate(patch->localRHS, &patch->localUpdate);CHKERRQ(ierr);
23094bbf5ea8SMatthew G. Knepley     ierr = PCPatchCreateCellPatches(pc);CHKERRQ(ierr);
23104bbf5ea8SMatthew G. Knepley     ierr = PCPatchCreateCellPatchDiscretisationInfo(pc);CHKERRQ(ierr);
23114bbf5ea8SMatthew G. Knepley 
23124bbf5ea8SMatthew G. Knepley     /* OK, now build the work vectors */
23134bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, &pEnd);CHKERRQ(ierr);
23141202d238SPatrick Farrell     ierr = PetscMalloc1(patch->npatch, &patch->patchRHS);CHKERRQ(ierr);
23151202d238SPatrick Farrell     ierr = PetscMalloc1(patch->npatch, &patch->patchUpdate);CHKERRQ(ierr);
2316c2e6f3c0SFlorian Wechsung 
231761c4b389SFlorian Wechsung     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
23181202d238SPatrick Farrell       ierr = PetscMalloc1(patch->npatch, &patch->patchRHSWithArtificial);CHKERRQ(ierr);
2319c2e6f3c0SFlorian Wechsung       ierr = PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithArtificial);CHKERRQ(ierr);
2320c2e6f3c0SFlorian Wechsung     }
23210904074fSPatrick Farrell     if (isNonlinear) {
23220904074fSPatrick Farrell       ierr = PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithAll);CHKERRQ(ierr);
23230904074fSPatrick Farrell     }
23244bbf5ea8SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
23254bbf5ea8SMatthew G. Knepley       PetscInt dof;
23264bbf5ea8SMatthew G. Knepley 
23274bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr);
23281202d238SPatrick Farrell       ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchRHS[p-pStart]);CHKERRQ(ierr);
23291202d238SPatrick Farrell       ierr = VecSetUp(patch->patchRHS[p-pStart]);CHKERRQ(ierr);
23301202d238SPatrick Farrell       ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchUpdate[p-pStart]);CHKERRQ(ierr);
23311202d238SPatrick Farrell       ierr = VecSetUp(patch->patchUpdate[p-pStart]);CHKERRQ(ierr);
23320904074fSPatrick Farrell       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
23333bb0e8f7SKarl Rupp         const PetscInt    *gtolArray, *gtolArrayWithArtificial = NULL;
23343bb0e8f7SKarl Rupp         PetscInt           numPatchDofs, offset;
23353bb0e8f7SKarl Rupp         PetscInt           numPatchDofsWithArtificial, offsetWithArtificial;
23363bb0e8f7SKarl Rupp         PetscInt           dofWithoutArtificialCounter = 0;
23373bb0e8f7SKarl Rupp         PetscInt          *patchWithoutArtificialToWithArtificialArray;
23383bb0e8f7SKarl Rupp 
2339c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof);CHKERRQ(ierr);
23401202d238SPatrick Farrell         ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchRHSWithArtificial[p-pStart]);CHKERRQ(ierr);
23411202d238SPatrick Farrell         ierr = VecSetUp(patch->patchRHSWithArtificial[p-pStart]);CHKERRQ(ierr);
2342c2e6f3c0SFlorian Wechsung 
2343e047a90bSFlorian Wechsung         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
2344e047a90bSFlorian Wechsung         /* the index in the patch with all dofs */
2345c2e6f3c0SFlorian Wechsung         ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
234663deea8eSPatrick Farrell 
2347c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs);CHKERRQ(ierr);
234847aca4a6SPatrick Farrell         if (numPatchDofs == 0) {
234947aca4a6SPatrick Farrell           patch->dofMappingWithoutToWithArtificial[p-pStart] = NULL;
235047aca4a6SPatrick Farrell           continue;
235147aca4a6SPatrick Farrell         }
235263deea8eSPatrick Farrell 
2353c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
2354c2e6f3c0SFlorian Wechsung         ierr = ISGetIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial);CHKERRQ(ierr);
2355c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &numPatchDofsWithArtificial);CHKERRQ(ierr);
2356c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offsetWithArtificial);CHKERRQ(ierr);
2357c2e6f3c0SFlorian Wechsung 
2358c2e6f3c0SFlorian Wechsung         ierr = PetscMalloc1(numPatchDofs, &patchWithoutArtificialToWithArtificialArray);CHKERRQ(ierr);
2359b0c21b6aSKarl Rupp         for (i=0; i<numPatchDofsWithArtificial; i++) {
2360e047a90bSFlorian Wechsung           if (gtolArrayWithArtificial[i+offsetWithArtificial] == gtolArray[offset+dofWithoutArtificialCounter]) {
2361c2e6f3c0SFlorian Wechsung             patchWithoutArtificialToWithArtificialArray[dofWithoutArtificialCounter] = i;
2362c2e6f3c0SFlorian Wechsung             dofWithoutArtificialCounter++;
2363c2e6f3c0SFlorian Wechsung             if (dofWithoutArtificialCounter == numPatchDofs)
2364c2e6f3c0SFlorian Wechsung               break;
2365c2e6f3c0SFlorian Wechsung           }
2366c2e6f3c0SFlorian Wechsung         }
2367168bb5e4SPatrick Farrell         ierr = ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutArtificialToWithArtificialArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithArtificial[p-pStart]);CHKERRQ(ierr);
2368c2e6f3c0SFlorian Wechsung         ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
2369c2e6f3c0SFlorian Wechsung         ierr = ISRestoreIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial);CHKERRQ(ierr);
2370c2e6f3c0SFlorian Wechsung       }
23710904074fSPatrick Farrell       if (isNonlinear) {
23720904074fSPatrick Farrell         const PetscInt    *gtolArray, *gtolArrayWithAll = NULL;
23730904074fSPatrick Farrell         PetscInt           numPatchDofs, offset;
23740904074fSPatrick Farrell         PetscInt           numPatchDofsWithAll, offsetWithAll;
23750904074fSPatrick Farrell         PetscInt           dofWithoutAllCounter = 0;
23760904074fSPatrick Farrell         PetscInt          *patchWithoutAllToWithAllArray;
23770904074fSPatrick Farrell 
23780904074fSPatrick Farrell         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
23790904074fSPatrick Farrell         /* the index in the patch with all dofs */
23800904074fSPatrick Farrell         ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
23810904074fSPatrick Farrell 
23820904074fSPatrick Farrell         ierr = PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs);CHKERRQ(ierr);
238347aca4a6SPatrick Farrell         if (numPatchDofs == 0) {
2384b88cb22dSPatrick Farrell           patch->dofMappingWithoutToWithAll[p-pStart] = NULL;
238547aca4a6SPatrick Farrell           continue;
238647aca4a6SPatrick Farrell         }
23870904074fSPatrick Farrell 
23880904074fSPatrick Farrell         ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
23890904074fSPatrick Farrell         ierr = ISGetIndices(patch->gtolWithAll, &gtolArrayWithAll);CHKERRQ(ierr);
23900904074fSPatrick Farrell         ierr = PetscSectionGetDof(patch->gtolCountsWithAll, p, &numPatchDofsWithAll);CHKERRQ(ierr);
23910904074fSPatrick Farrell         ierr = PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offsetWithAll);CHKERRQ(ierr);
23920904074fSPatrick Farrell 
23930904074fSPatrick Farrell         ierr = PetscMalloc1(numPatchDofs, &patchWithoutAllToWithAllArray);CHKERRQ(ierr);
23940904074fSPatrick Farrell 
23950904074fSPatrick Farrell         for (i=0; i<numPatchDofsWithAll; i++) {
23960904074fSPatrick Farrell           if (gtolArrayWithAll[i+offsetWithAll] == gtolArray[offset+dofWithoutAllCounter]) {
23970904074fSPatrick Farrell             patchWithoutAllToWithAllArray[dofWithoutAllCounter] = i;
23980904074fSPatrick Farrell             dofWithoutAllCounter++;
23990904074fSPatrick Farrell             if (dofWithoutAllCounter == numPatchDofs)
24000904074fSPatrick Farrell               break;
24010904074fSPatrick Farrell           }
24020904074fSPatrick Farrell         }
2403168bb5e4SPatrick Farrell         ierr = ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutAllToWithAllArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithAll[p-pStart]);CHKERRQ(ierr);
24040904074fSPatrick Farrell         ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
24050904074fSPatrick Farrell         ierr = ISRestoreIndices(patch->gtolWithAll, &gtolArrayWithAll);CHKERRQ(ierr);
24060904074fSPatrick Farrell       }
24074bbf5ea8SMatthew G. Knepley     }
24084bbf5ea8SMatthew G. Knepley     if (patch->save_operators) {
24094bbf5ea8SMatthew G. Knepley       ierr = PetscMalloc1(patch->npatch, &patch->mat);CHKERRQ(ierr);
24104bbf5ea8SMatthew G. Knepley       for (i = 0; i < patch->npatch; ++i) {
2411c2e6f3c0SFlorian Wechsung         ierr = PCPatchCreateMatrix_Private(pc, i, &patch->mat[i], PETSC_FALSE);CHKERRQ(ierr);
24124bbf5ea8SMatthew G. Knepley       }
24134bbf5ea8SMatthew G. Knepley     }
24144bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr);
24154bbf5ea8SMatthew G. Knepley 
24164bbf5ea8SMatthew G. Knepley     /* If desired, calculate weights for dof multiplicity */
24174bbf5ea8SMatthew G. Knepley     if (patch->partition_of_unity) {
24183bb0e8f7SKarl Rupp       PetscScalar *input = NULL;
24193bb0e8f7SKarl Rupp       PetscScalar *output = NULL;
24203bb0e8f7SKarl Rupp       Vec global;
24213bb0e8f7SKarl Rupp 
24221202d238SPatrick Farrell       ierr = VecDuplicate(patch->localRHS, &patch->dof_weights);CHKERRQ(ierr);
242361c4b389SFlorian Wechsung       if(patch->local_composition_type == PC_COMPOSITE_ADDITIVE) {
24244bbf5ea8SMatthew G. Knepley         for (i = 0; i < patch->npatch; ++i) {
24254bbf5ea8SMatthew G. Knepley           PetscInt dof;
24264bbf5ea8SMatthew G. Knepley 
24274bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &dof);CHKERRQ(ierr);
24284bbf5ea8SMatthew G. Knepley           if (dof <= 0) continue;
24291202d238SPatrick Farrell           ierr = VecSet(patch->patchRHS[i], 1.0);CHKERRQ(ierr);
24300904074fSPatrick Farrell           ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchRHS[i], patch->dof_weights, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR);CHKERRQ(ierr);
24314bbf5ea8SMatthew G. Knepley         }
2432c2e6f3c0SFlorian Wechsung       } else {
2433e047a90bSFlorian Wechsung         /* multiplicative is actually only locally multiplicative and globally additive. need the pou where the mesh decomposition overlaps */
2434c2e6f3c0SFlorian Wechsung         ierr = VecSet(patch->dof_weights, 1.0);CHKERRQ(ierr);
24354bbf5ea8SMatthew G. Knepley       }
2436d132cafaSFlorian Wechsung 
2437d132cafaSFlorian Wechsung       VecDuplicate(patch->dof_weights, &global);
2438d132cafaSFlorian Wechsung       VecSet(global, 0.);
2439d132cafaSFlorian Wechsung 
2440d132cafaSFlorian Wechsung       ierr = VecGetArray(patch->dof_weights, &input);CHKERRQ(ierr);
2441d132cafaSFlorian Wechsung       ierr = VecGetArray(global, &output);CHKERRQ(ierr);
2442d132cafaSFlorian Wechsung       ierr = PetscSFReduceBegin(patch->defaultSF, MPIU_SCALAR, input, output, MPI_SUM);CHKERRQ(ierr);
2443d132cafaSFlorian Wechsung       ierr = PetscSFReduceEnd(patch->defaultSF, MPIU_SCALAR, input, output, MPI_SUM);CHKERRQ(ierr);
2444d132cafaSFlorian Wechsung       ierr = VecRestoreArray(patch->dof_weights, &input);CHKERRQ(ierr);
2445d132cafaSFlorian Wechsung       ierr = VecRestoreArray(global, &output);CHKERRQ(ierr);
2446d132cafaSFlorian Wechsung 
244705528938SFlorian Wechsung       ierr = VecReciprocal(global);CHKERRQ(ierr);
2448d132cafaSFlorian Wechsung 
2449d132cafaSFlorian Wechsung       ierr = VecGetArray(patch->dof_weights, &output);CHKERRQ(ierr);
2450d132cafaSFlorian Wechsung       ierr = VecGetArray(global, &input);CHKERRQ(ierr);
2451d132cafaSFlorian Wechsung       ierr = PetscSFBcastBegin(patch->defaultSF, MPIU_SCALAR, input, output);CHKERRQ(ierr);
2452d132cafaSFlorian Wechsung       ierr = PetscSFBcastEnd(patch->defaultSF, MPIU_SCALAR, input, output);CHKERRQ(ierr);
2453d132cafaSFlorian Wechsung       ierr = VecRestoreArray(patch->dof_weights, &output);CHKERRQ(ierr);
2454d132cafaSFlorian Wechsung       ierr = VecRestoreArray(global, &input);CHKERRQ(ierr);
2455d132cafaSFlorian Wechsung       ierr = VecDestroy(&global);CHKERRQ(ierr);
24564bbf5ea8SMatthew G. Knepley     }
245761c4b389SFlorian Wechsung     if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE && patch->save_operators) {
245896b79ebeSFlorian Wechsung       ierr = PetscMalloc1(patch->npatch, &patch->matWithArtificial);CHKERRQ(ierr);
24594bbf5ea8SMatthew G. Knepley     }
24604bbf5ea8SMatthew G. Knepley   }
2461dadc69c5SMatthew G. Knepley   ierr = (*patch->setupsolver)(pc);CHKERRQ(ierr);
2462dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
24634bbf5ea8SMatthew G. Knepley }
2464dadc69c5SMatthew G. Knepley 
2465dadc69c5SMatthew G. Knepley static PetscErrorCode PCApply_PATCH_Linear(PC pc, PetscInt i, Vec x, Vec y)
2466dadc69c5SMatthew G. Knepley {
2467dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2468dadc69c5SMatthew G. Knepley   KSP            ksp   = (KSP) patch->solver[i];
2469dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2470dadc69c5SMatthew G. Knepley 
2471dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2472dadc69c5SMatthew G. Knepley   if (!patch->save_operators) {
2473dadc69c5SMatthew G. Knepley     Mat mat;
2474dadc69c5SMatthew G. Knepley 
247534d8b122SPatrick Farrell     ierr = PCPatchCreateMatrix_Private(pc, i, &mat, PETSC_FALSE);CHKERRQ(ierr);
2476dadc69c5SMatthew G. Knepley     /* Populate operator here. */
247734d8b122SPatrick Farrell     ierr = PCPatchComputeOperator_Internal(pc, NULL, mat, i, PETSC_FALSE);CHKERRQ(ierr);
2478dadc69c5SMatthew G. Knepley     ierr = KSPSetOperators(ksp, mat, mat);CHKERRQ(ierr);
2479dadc69c5SMatthew G. Knepley     /* Drop reference so the KSPSetOperators below will blow it away. */
2480dadc69c5SMatthew G. Knepley     ierr = MatDestroy(&mat);CHKERRQ(ierr);
2481dadc69c5SMatthew G. Knepley   }
2482dadc69c5SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr);
2483dadc69c5SMatthew G. Knepley   if (!ksp->setfromoptionscalled) {
2484dadc69c5SMatthew G. Knepley     ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
2485dadc69c5SMatthew G. Knepley   }
2486dadc69c5SMatthew G. Knepley   ierr = KSPSolve(ksp, x, y);CHKERRQ(ierr);
2487ddad275aSPatrick Farrell   ierr = KSPCheckSolve(ksp, pc, y);CHKERRQ(ierr);
2488dadc69c5SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr);
2489dadc69c5SMatthew G. Knepley   if (!patch->save_operators) {
2490dadc69c5SMatthew G. Knepley     PC pc;
2491dadc69c5SMatthew G. Knepley     ierr = KSPSetOperators(ksp, NULL, NULL);CHKERRQ(ierr);
2492dadc69c5SMatthew G. Knepley     ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
2493dadc69c5SMatthew G. Knepley     /* Destroy PC context too, otherwise the factored matrix hangs around. */
2494dadc69c5SMatthew G. Knepley     ierr = PCReset(pc);CHKERRQ(ierr);
24954bbf5ea8SMatthew G. Knepley   }
24964bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
24974bbf5ea8SMatthew G. Knepley }
24984bbf5ea8SMatthew G. Knepley 
24996c9c532dSPatrick Farrell static PetscErrorCode PCUpdateMultiplicative_PATCH_Linear(PC pc, PetscInt i, PetscInt pStart)
25006c9c532dSPatrick Farrell {
25016c9c532dSPatrick Farrell   PC_PATCH      *patch = (PC_PATCH *) pc->data;
25026c9c532dSPatrick Farrell   Mat multMat;
25036c9c532dSPatrick Farrell   PetscErrorCode ierr;
25046c9c532dSPatrick Farrell 
25054d04e9f1SPatrick Farrell   PetscFunctionBegin;
25064d04e9f1SPatrick Farrell 
25076c9c532dSPatrick Farrell   if (patch->save_operators) {
25086c9c532dSPatrick Farrell     multMat = patch->matWithArtificial[i];
25096c9c532dSPatrick Farrell   } else {
25106c9c532dSPatrick Farrell     /*Very inefficient, hopefully we can just assemble the rectangular matrix in the first place.*/
25116c9c532dSPatrick Farrell     Mat matSquare;
25126c9c532dSPatrick Farrell     PetscInt dof;
25136c9c532dSPatrick Farrell     IS rowis;
25146c9c532dSPatrick Farrell     ierr = PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE);CHKERRQ(ierr);
25156c9c532dSPatrick Farrell     ierr = MatZeroEntries(matSquare);CHKERRQ(ierr);
25166c9c532dSPatrick Farrell     ierr = PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE);CHKERRQ(ierr);
25176c9c532dSPatrick Farrell     ierr = MatGetSize(matSquare, &dof, NULL);CHKERRQ(ierr);
25186c9c532dSPatrick Farrell     ierr = ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis); CHKERRQ(ierr);
25196c9c532dSPatrick Farrell     ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &multMat); CHKERRQ(ierr);
25206c9c532dSPatrick Farrell     ierr = MatDestroy(&matSquare);CHKERRQ(ierr);
25216c9c532dSPatrick Farrell     ierr = ISDestroy(&rowis); CHKERRQ(ierr);
25226c9c532dSPatrick Farrell   }
25236c9c532dSPatrick Farrell   ierr = MatMult(multMat, patch->patchUpdate[i], patch->patchRHSWithArtificial[i]); CHKERRQ(ierr);
25246c9c532dSPatrick Farrell   ierr = VecScale(patch->patchRHSWithArtificial[i], -1.0); CHKERRQ(ierr);
25250904074fSPatrick Farrell   ierr = PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHSWithArtificial[i], patch->localRHS, ADD_VALUES, SCATTER_REVERSE, SCATTER_WITHARTIFICIAL); CHKERRQ(ierr);
25266c9c532dSPatrick Farrell   if (!patch->save_operators) {
25276c9c532dSPatrick Farrell     ierr = MatDestroy(&multMat); CHKERRQ(ierr);
25286c9c532dSPatrick Farrell   }
25294d04e9f1SPatrick Farrell   PetscFunctionReturn(0);
25306c9c532dSPatrick Farrell }
25316c9c532dSPatrick Farrell 
25324bbf5ea8SMatthew G. Knepley static PetscErrorCode PCApply_PATCH(PC pc, Vec x, Vec y)
25334bbf5ea8SMatthew G. Knepley {
25344bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch    = (PC_PATCH *) pc->data;
25351202d238SPatrick Farrell   const PetscScalar *globalRHS  = NULL;
25361202d238SPatrick Farrell   PetscScalar       *localRHS   = NULL;
25371202d238SPatrick Farrell   PetscScalar       *globalUpdate  = NULL;
25384bbf5ea8SMatthew G. Knepley   const PetscInt    *bcNodes  = NULL;
25394bbf5ea8SMatthew G. Knepley   PetscInt           nsweep   = patch->symmetrise_sweep ? 2 : 1;
25404bbf5ea8SMatthew G. Knepley   PetscInt           start[2] = {0, 0};
25414bbf5ea8SMatthew G. Knepley   PetscInt           end[2]   = {-1, -1};
25424bbf5ea8SMatthew G. Knepley   const PetscInt     inc[2]   = {1, -1};
25431202d238SPatrick Farrell   const PetscScalar *localUpdate;
25444bbf5ea8SMatthew G. Knepley   const PetscInt    *iterationSet;
25454bbf5ea8SMatthew G. Knepley   PetscInt           pStart, numBcs, n, sweep, bc, j;
25464bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
25474bbf5ea8SMatthew G. Knepley 
25484bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
25494bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr);
25504bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsPushGetViewerOff(PETSC_TRUE);CHKERRQ(ierr);
255192d50984SMatthew G. Knepley   /* start, end, inc have 2 entries to manage a second backward sweep if we symmetrize */
25524bbf5ea8SMatthew G. Knepley   end[0]   = patch->npatch;
25534bbf5ea8SMatthew G. Knepley   start[1] = patch->npatch-1;
25544bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
25554bbf5ea8SMatthew G. Knepley     ierr = ISGetLocalSize(patch->iterationSet, &end[0]);CHKERRQ(ierr);
25564bbf5ea8SMatthew G. Knepley     start[1] = end[0] - 1;
25574bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr);
25584bbf5ea8SMatthew G. Knepley   }
25594bbf5ea8SMatthew G. Knepley   /* Scatter from global space into overlapped local spaces */
25601202d238SPatrick Farrell   ierr = VecGetArrayRead(x, &globalRHS);CHKERRQ(ierr);
25611202d238SPatrick Farrell   ierr = VecGetArray(patch->localRHS, &localRHS);CHKERRQ(ierr);
25621202d238SPatrick Farrell   ierr = PetscSFBcastBegin(patch->defaultSF, MPIU_SCALAR, globalRHS, localRHS);CHKERRQ(ierr);
25631202d238SPatrick Farrell   ierr = PetscSFBcastEnd(patch->defaultSF, MPIU_SCALAR, globalRHS, localRHS);CHKERRQ(ierr);
25641202d238SPatrick Farrell   ierr = VecRestoreArrayRead(x, &globalRHS);CHKERRQ(ierr);
25651202d238SPatrick Farrell   ierr = VecRestoreArray(patch->localRHS, &localRHS);CHKERRQ(ierr);
25664bbf5ea8SMatthew G. Knepley 
25671202d238SPatrick Farrell   ierr = VecSet(patch->localUpdate, 0.0);CHKERRQ(ierr);
25684bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, NULL);CHKERRQ(ierr);
25694bbf5ea8SMatthew G. Knepley   for (sweep = 0; sweep < nsweep; sweep++) {
25704bbf5ea8SMatthew G. Knepley     for (j = start[sweep]; j*inc[sweep] < end[sweep]*inc[sweep]; j += inc[sweep]) {
25714bbf5ea8SMatthew G. Knepley       PetscInt i       = patch->user_patches ? iterationSet[j] : j;
25724bbf5ea8SMatthew G. Knepley       PetscInt start, len;
25734bbf5ea8SMatthew G. Knepley 
25744bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &len);CHKERRQ(ierr);
25754bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetOffset(patch->gtolCounts, i+pStart, &start);CHKERRQ(ierr);
25764bbf5ea8SMatthew G. Knepley       /* TODO: Squash out these guys in the setup as well. */
25774bbf5ea8SMatthew G. Knepley       if (len <= 0) continue;
25784bbf5ea8SMatthew G. Knepley       /* TODO: Do we need different scatters for X and Y? */
25790904074fSPatrick Farrell       ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->localRHS, patch->patchRHS[i], INSERT_VALUES, SCATTER_FORWARD, SCATTER_INTERIOR);CHKERRQ(ierr);
25801202d238SPatrick Farrell       ierr = (*patch->applysolver)(pc, i, patch->patchRHS[i], patch->patchUpdate[i]);CHKERRQ(ierr);
25810904074fSPatrick Farrell       ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchUpdate[i], patch->localUpdate, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR);CHKERRQ(ierr);
258261c4b389SFlorian Wechsung       if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
25836c9c532dSPatrick Farrell         ierr = (*patch->updatemultiplicative)(pc, i, pStart);CHKERRQ(ierr);
2584c2e6f3c0SFlorian Wechsung       }
25854bbf5ea8SMatthew G. Knepley     }
25864bbf5ea8SMatthew G. Knepley   }
25874bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {ierr = ISRestoreIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr);}
25884bbf5ea8SMatthew G. Knepley   /* XXX: should we do this on the global vector? */
258973ec7555SLawrence Mitchell   if (patch->partition_of_unity) {
25901202d238SPatrick Farrell     ierr = VecPointwiseMult(patch->localUpdate, patch->localUpdate, patch->dof_weights);CHKERRQ(ierr);
25914bbf5ea8SMatthew G. Knepley   }
25921202d238SPatrick Farrell   /* Now patch->localUpdate contains the solution of the patch solves, so we need to combine them all. */
25934bbf5ea8SMatthew G. Knepley   ierr = VecSet(y, 0.0);CHKERRQ(ierr);
25941202d238SPatrick Farrell   ierr = VecGetArray(y, &globalUpdate);CHKERRQ(ierr);
25951202d238SPatrick Farrell   ierr = VecGetArrayRead(patch->localUpdate, &localUpdate);CHKERRQ(ierr);
25961202d238SPatrick Farrell   ierr = PetscSFReduceBegin(patch->defaultSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM);CHKERRQ(ierr);
25971202d238SPatrick Farrell   ierr = PetscSFReduceEnd(patch->defaultSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM);CHKERRQ(ierr);
25981202d238SPatrick Farrell   ierr = VecRestoreArrayRead(patch->localUpdate, &localUpdate);CHKERRQ(ierr);
25994bbf5ea8SMatthew G. Knepley 
26004bbf5ea8SMatthew G. Knepley   /* Now we need to send the global BC values through */
26011202d238SPatrick Farrell   ierr = VecGetArrayRead(x, &globalRHS);CHKERRQ(ierr);
26024bbf5ea8SMatthew G. Knepley   ierr = ISGetSize(patch->globalBcNodes, &numBcs);CHKERRQ(ierr);
26034bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr);
26044bbf5ea8SMatthew G. Knepley   ierr = VecGetLocalSize(x, &n);CHKERRQ(ierr);
26054bbf5ea8SMatthew G. Knepley   for (bc = 0; bc < numBcs; ++bc) {
26064bbf5ea8SMatthew G. Knepley     const PetscInt idx = bcNodes[bc];
26071202d238SPatrick Farrell     if (idx < n) globalUpdate[idx] = globalRHS[idx];
26084bbf5ea8SMatthew G. Knepley   }
26094bbf5ea8SMatthew G. Knepley 
26104bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr);
26111202d238SPatrick Farrell   ierr = VecRestoreArrayRead(x, &globalRHS);CHKERRQ(ierr);
26121202d238SPatrick Farrell   ierr = VecRestoreArray(y, &globalUpdate);CHKERRQ(ierr);
26134bbf5ea8SMatthew G. Knepley 
26144bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsPopGetViewerOff();CHKERRQ(ierr);
26154bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr);
26164bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
26174bbf5ea8SMatthew G. Knepley }
26184bbf5ea8SMatthew G. Knepley 
2619dadc69c5SMatthew G. Knepley static PetscErrorCode PCReset_PATCH_Linear(PC pc)
2620dadc69c5SMatthew G. Knepley {
2621dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2622dadc69c5SMatthew G. Knepley   PetscInt       i;
2623dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2624dadc69c5SMatthew G. Knepley 
2625dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2626dadc69c5SMatthew G. Knepley   if (patch->solver) {
2627dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = KSPReset((KSP) patch->solver[i]);CHKERRQ(ierr);}
2628dadc69c5SMatthew G. Knepley   }
2629dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
2630dadc69c5SMatthew G. Knepley }
2631dadc69c5SMatthew G. Knepley 
26324bbf5ea8SMatthew G. Knepley static PetscErrorCode PCReset_PATCH(PC pc)
26334bbf5ea8SMatthew G. Knepley {
26344bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
26354bbf5ea8SMatthew G. Knepley   PetscInt       i;
26364bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
26374bbf5ea8SMatthew G. Knepley 
26384bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
26394bbf5ea8SMatthew G. Knepley   /* TODO: Get rid of all these ifs */
26404bbf5ea8SMatthew G. Knepley   ierr = PetscSFDestroy(&patch->defaultSF);CHKERRQ(ierr);
26414bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->cellCounts);CHKERRQ(ierr);
26425f824522SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->pointCounts);CHKERRQ(ierr);
26434bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->cellNumbering);CHKERRQ(ierr);
26444bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->gtolCounts);CHKERRQ(ierr);
26454bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->gtol);CHKERRQ(ierr);
26464bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->cells);CHKERRQ(ierr);
26475f824522SMatthew G. Knepley   ierr = ISDestroy(&patch->points);CHKERRQ(ierr);
26484bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->dofs);CHKERRQ(ierr);
26495f824522SMatthew G. Knepley   ierr = ISDestroy(&patch->offs);CHKERRQ(ierr);
26505f824522SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->patchSection);CHKERRQ(ierr);
26514bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->ghostBcNodes);CHKERRQ(ierr);
26524bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->globalBcNodes);CHKERRQ(ierr);
265311ac8bb0SFlorian Wechsung   ierr = PetscSectionDestroy(&patch->gtolCountsWithArtificial);CHKERRQ(ierr);
265411ac8bb0SFlorian Wechsung   ierr = ISDestroy(&patch->gtolWithArtificial);CHKERRQ(ierr);
265511ac8bb0SFlorian Wechsung   ierr = ISDestroy(&patch->dofsWithArtificial);CHKERRQ(ierr);
265611ac8bb0SFlorian Wechsung   ierr = ISDestroy(&patch->offsWithArtificial);CHKERRQ(ierr);
26570904074fSPatrick Farrell   ierr = PetscSectionDestroy(&patch->gtolCountsWithAll);CHKERRQ(ierr);
26580904074fSPatrick Farrell   ierr = ISDestroy(&patch->gtolWithAll);CHKERRQ(ierr);
26590904074fSPatrick Farrell   ierr = ISDestroy(&patch->dofsWithAll);CHKERRQ(ierr);
26600904074fSPatrick Farrell   ierr = ISDestroy(&patch->offsWithAll);CHKERRQ(ierr);
266111ac8bb0SFlorian Wechsung 
26624bbf5ea8SMatthew G. Knepley 
26635f824522SMatthew G. Knepley   if (patch->dofSection) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscSectionDestroy(&patch->dofSection[i]);CHKERRQ(ierr);}
26644bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->dofSection);CHKERRQ(ierr);
26654bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->bs);CHKERRQ(ierr);
26664bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->nodesPerCell);CHKERRQ(ierr);
26675f824522SMatthew G. Knepley   if (patch->cellNodeMap) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscFree(patch->cellNodeMap[i]);CHKERRQ(ierr);}
26684bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->cellNodeMap);CHKERRQ(ierr);
26694bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->subspaceOffsets);CHKERRQ(ierr);
26704bbf5ea8SMatthew G. Knepley 
2671dadc69c5SMatthew G. Knepley   ierr = (*patch->resetsolver)(pc);CHKERRQ(ierr);
26724bbf5ea8SMatthew G. Knepley 
2673e4c66b91SPatrick Farrell   if (patch->subspaces_to_exclude) {
2674e4c66b91SPatrick Farrell     PetscHSetIDestroy(&patch->subspaces_to_exclude);
2675e4c66b91SPatrick Farrell   }
2676e4c66b91SPatrick Farrell 
26771202d238SPatrick Farrell   ierr = VecDestroy(&patch->localRHS);CHKERRQ(ierr);
26781202d238SPatrick Farrell   ierr = VecDestroy(&patch->localUpdate);CHKERRQ(ierr);
26791202d238SPatrick Farrell   if (patch->patchRHS) {
26801202d238SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchRHS[i]);CHKERRQ(ierr);}
26811202d238SPatrick Farrell     ierr = PetscFree(patch->patchRHS);CHKERRQ(ierr);
26824bbf5ea8SMatthew G. Knepley   }
26831202d238SPatrick Farrell   if (patch->patchUpdate) {
26841202d238SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchUpdate[i]);CHKERRQ(ierr);}
26851202d238SPatrick Farrell     ierr = PetscFree(patch->patchUpdate);CHKERRQ(ierr);
26864bbf5ea8SMatthew G. Knepley   }
26874bbf5ea8SMatthew G. Knepley   ierr = VecDestroy(&patch->dof_weights);CHKERRQ(ierr);
26884bbf5ea8SMatthew G. Knepley   if (patch->patch_dof_weights) {
26895f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patch_dof_weights[i]);CHKERRQ(ierr);}
26904bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->patch_dof_weights);CHKERRQ(ierr);
26914bbf5ea8SMatthew G. Knepley   }
26924bbf5ea8SMatthew G. Knepley   if (patch->mat) {
26935f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->mat[i]);CHKERRQ(ierr);}
26944bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->mat);CHKERRQ(ierr);
26955f824522SMatthew G. Knepley   }
269611ac8bb0SFlorian Wechsung   if (patch->matWithArtificial) {
269711ac8bb0SFlorian Wechsung     for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->matWithArtificial[i]);CHKERRQ(ierr);}
269811ac8bb0SFlorian Wechsung     ierr = PetscFree(patch->matWithArtificial);CHKERRQ(ierr);
269911ac8bb0SFlorian Wechsung   }
27001202d238SPatrick Farrell   if (patch->patchRHSWithArtificial) {
27011202d238SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchRHSWithArtificial[i]);CHKERRQ(ierr);}
27021202d238SPatrick Farrell     ierr = PetscFree(patch->patchRHSWithArtificial);CHKERRQ(ierr);
270311ac8bb0SFlorian Wechsung   }
270496b79ebeSFlorian Wechsung   if(patch->dofMappingWithoutToWithArtificial) {
270596b79ebeSFlorian Wechsung     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->dofMappingWithoutToWithArtificial[i]);CHKERRQ(ierr);}
270696b79ebeSFlorian Wechsung     ierr = PetscFree(patch->dofMappingWithoutToWithArtificial);CHKERRQ(ierr);
270796b79ebeSFlorian Wechsung 
270896b79ebeSFlorian Wechsung   }
27090904074fSPatrick Farrell   if(patch->dofMappingWithoutToWithAll) {
27100904074fSPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->dofMappingWithoutToWithAll[i]);CHKERRQ(ierr);}
27110904074fSPatrick Farrell     ierr = PetscFree(patch->dofMappingWithoutToWithAll);CHKERRQ(ierr);
27120904074fSPatrick Farrell 
27130904074fSPatrick Farrell   }
27144bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr);
27155f824522SMatthew G. Knepley   if (patch->userIS) {
27165f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->userIS[i]);CHKERRQ(ierr);}
27175f824522SMatthew G. Knepley     ierr = PetscFree(patch->userIS);CHKERRQ(ierr);
27185f824522SMatthew G. Knepley   }
27194bbf5ea8SMatthew G. Knepley   patch->bs          = 0;
27204bbf5ea8SMatthew G. Knepley   patch->cellNodeMap = NULL;
27217974b488SMatthew G. Knepley   patch->nsubspaces  = 0;
27224bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->iterationSet);CHKERRQ(ierr);
27235f824522SMatthew G. Knepley 
27245f824522SMatthew G. Knepley   ierr = PetscViewerDestroy(&patch->viewerSection);CHKERRQ(ierr);
27254bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
27264bbf5ea8SMatthew G. Knepley }
27274bbf5ea8SMatthew G. Knepley 
2728dadc69c5SMatthew G. Knepley static PetscErrorCode PCDestroy_PATCH_Linear(PC pc)
27294bbf5ea8SMatthew G. Knepley {
27304bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
27314bbf5ea8SMatthew G. Knepley   PetscInt       i;
27324bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
27334bbf5ea8SMatthew G. Knepley 
27344bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2735dadc69c5SMatthew G. Knepley   if (patch->solver) {
2736dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = KSPDestroy((KSP *) &patch->solver[i]);CHKERRQ(ierr);}
2737dadc69c5SMatthew G. Knepley     ierr = PetscFree(patch->solver);CHKERRQ(ierr);
27384bbf5ea8SMatthew G. Knepley   }
2739dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
2740dadc69c5SMatthew G. Knepley }
2741dadc69c5SMatthew G. Knepley 
2742dadc69c5SMatthew G. Knepley static PetscErrorCode PCDestroy_PATCH(PC pc)
2743dadc69c5SMatthew G. Knepley {
2744dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2745dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2746dadc69c5SMatthew G. Knepley 
2747dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2748dadc69c5SMatthew G. Knepley   ierr = PCReset_PATCH(pc);CHKERRQ(ierr);
2749dadc69c5SMatthew G. Knepley   ierr = (*patch->destroysolver)(pc);CHKERRQ(ierr);
27504bbf5ea8SMatthew G. Knepley   ierr = PetscFree(pc->data);CHKERRQ(ierr);
27514bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
27524bbf5ea8SMatthew G. Knepley }
27534bbf5ea8SMatthew G. Knepley 
27544bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetFromOptions_PATCH(PetscOptionItems *PetscOptionsObject, PC pc)
27554bbf5ea8SMatthew G. Knepley {
27564bbf5ea8SMatthew G. Knepley   PC_PATCH            *patch = (PC_PATCH *) pc->data;
27574bbf5ea8SMatthew G. Knepley   PCPatchConstructType patchConstructionType = PC_PATCH_STAR;
27585f824522SMatthew G. Knepley   char                 sub_mat_type[PETSC_MAX_PATH_LEN];
275910534d48SPatrick Farrell   char                 option[PETSC_MAX_PATH_LEN];
27605f824522SMatthew G. Knepley   const char          *prefix;
27614bbf5ea8SMatthew G. Knepley   PetscBool            flg, dimflg, codimflg;
27625f824522SMatthew G. Knepley   MPI_Comm             comm;
2763a48c39c8SPatrick Farrell   PetscInt            *ifields, nfields, k;
27644bbf5ea8SMatthew G. Knepley   PetscErrorCode       ierr;
276561c4b389SFlorian Wechsung   PCCompositeType loctype = PC_COMPOSITE_ADDITIVE;
27664bbf5ea8SMatthew G. Knepley 
27674bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
27685f824522SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) pc, &comm);CHKERRQ(ierr);
27695f824522SMatthew G. Knepley   ierr = PetscObjectGetOptionsPrefix((PetscObject) pc, &prefix);CHKERRQ(ierr);
277010534d48SPatrick Farrell   ierr = PetscOptionsHead(PetscOptionsObject, "Patch solver options");CHKERRQ(ierr);
277110534d48SPatrick Farrell 
277210534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_save_operators", patch->classname);
277310534d48SPatrick Farrell   ierr = PetscOptionsBool(option,  "Store all patch operators for lifetime of object?", "PCPatchSetSaveOperators", patch->save_operators, &patch->save_operators, &flg);CHKERRQ(ierr);
277410534d48SPatrick Farrell 
277510534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_partition_of_unity", patch->classname);
277610534d48SPatrick Farrell   ierr = PetscOptionsBool(option, "Weight contributions by dof multiplicity?", "PCPatchSetPartitionOfUnity", patch->partition_of_unity, &patch->partition_of_unity, &flg);CHKERRQ(ierr);
277710534d48SPatrick Farrell 
277810534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_local_type", patch->classname);
277910534d48SPatrick Farrell   ierr = PetscOptionsEnum(option,"Type of local solver composition (additive or multiplicative)","PCPatchSetLocalComposition",PCCompositeTypes,(PetscEnum)loctype,(PetscEnum*)&loctype,&flg);CHKERRQ(ierr);
278061c4b389SFlorian Wechsung   if(flg) { ierr = PCPatchSetLocalComposition(pc, loctype);CHKERRQ(ierr);}
278110534d48SPatrick Farrell 
278210534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_dim", patch->classname);
278310534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "What dimension of mesh point to construct patches by? (0 = vertices)", "PCPATCH", patch->dim, &patch->dim, &dimflg);CHKERRQ(ierr);
278410534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_codim", patch->classname);
278510534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "What co-dimension of mesh point to construct patches by? (0 = cells)", "PCPATCH", patch->codim, &patch->codim, &codimflg);CHKERRQ(ierr);
278661c4b389SFlorian Wechsung   if (dimflg && codimflg) {SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Can only set one of dimension or co-dimension");CHKERRQ(ierr);}
278710534d48SPatrick Farrell 
278810534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_type", patch->classname);
278910534d48SPatrick Farrell   ierr = PetscOptionsEnum(option, "How should the patches be constructed?", "PCPatchSetConstructType", PCPatchConstructTypes, (PetscEnum) patchConstructionType, (PetscEnum *) &patchConstructionType, &flg);CHKERRQ(ierr);
27904bbf5ea8SMatthew G. Knepley   if (flg) {ierr = PCPatchSetConstructType(pc, patchConstructionType, NULL, NULL);CHKERRQ(ierr);}
279110534d48SPatrick Farrell 
279210534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_vanka_dim", patch->classname);
279310534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "Topological dimension of entities for Vanka to ignore", "PCPATCH", patch->vankadim, &patch->vankadim, &flg);CHKERRQ(ierr);
279410534d48SPatrick Farrell 
279510534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_ignore_dim", patch->classname);
279610534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "Topological dimension of entities for completion to ignore", "PCPATCH", patch->ignoredim, &patch->ignoredim, &flg);CHKERRQ(ierr);
279710534d48SPatrick Farrell 
2798b525f888SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_pardecomp_overlap", patch->classname);
2799b525f888SPatrick Farrell   ierr = PetscOptionsInt(option, "What overlap should we use in construct type pardecomp?", "PCPATCH", patch->pardecomp_overlap, &patch->pardecomp_overlap, &flg);CHKERRQ(ierr);
2800b525f888SPatrick Farrell 
280110534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_sub_mat_type", patch->classname);
280210534d48SPatrick Farrell   ierr = PetscOptionsFList(option, "Matrix type for patch solves", "PCPatchSetSubMatType", MatList, NULL, sub_mat_type, PETSC_MAX_PATH_LEN, &flg);CHKERRQ(ierr);
28034bbf5ea8SMatthew G. Knepley   if (flg) {ierr = PCPatchSetSubMatType(pc, sub_mat_type);CHKERRQ(ierr);}
280410534d48SPatrick Farrell 
280510534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_symmetrise_sweep", patch->classname);
280610534d48SPatrick Farrell   ierr = PetscOptionsBool(option, "Go start->end, end->start?", "PCPATCH", patch->symmetrise_sweep, &patch->symmetrise_sweep, &flg);CHKERRQ(ierr);
2807e4c66b91SPatrick Farrell 
2808a48c39c8SPatrick Farrell   /* If the user has set the number of subspaces, use that for the buffer size,
2809a48c39c8SPatrick Farrell      otherwise use a large number */
2810a48c39c8SPatrick Farrell   if (patch->nsubspaces <= 0) {
2811a48c39c8SPatrick Farrell     nfields = 128;
2812a48c39c8SPatrick Farrell   } else {
2813a48c39c8SPatrick Farrell     nfields = patch->nsubspaces;
2814a48c39c8SPatrick Farrell   }
2815a48c39c8SPatrick Farrell   ierr = PetscMalloc1(nfields, &ifields);CHKERRQ(ierr);
281610534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exclude_subspaces", patch->classname);
281710534d48SPatrick Farrell   ierr = PetscOptionsGetIntArray(((PetscObject)pc)->options,((PetscObject)pc)->prefix,option,ifields,&nfields,&flg);CHKERRQ(ierr);
2818e4c66b91SPatrick 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");
2819e4c66b91SPatrick Farrell   if (flg) {
2820e4c66b91SPatrick Farrell     PetscHSetIClear(patch->subspaces_to_exclude);
282159b66c28SPatrick Farrell     for (k = 0; k < nfields; k++) {
2822e4c66b91SPatrick Farrell       PetscHSetIAdd(patch->subspaces_to_exclude, ifields[k]);
2823e4c66b91SPatrick Farrell     }
2824e4c66b91SPatrick Farrell   }
282559b66c28SPatrick Farrell   ierr = PetscFree(ifields);CHKERRQ(ierr);
28265f824522SMatthew G. Knepley 
2827fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_patches_view", patch->classname);CHKERRQ(ierr);
282810534d48SPatrick Farrell   ierr = PetscOptionsBool(option, "Print out information during patch construction", "PCPATCH", patch->viewPatches, &patch->viewPatches, &flg);CHKERRQ(ierr);
2829fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_cells_view", patch->classname);CHKERRQ(ierr);
283010534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerCells, &patch->formatCells, &patch->viewCells);CHKERRQ(ierr);
2831fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_interior_facets_view", patch->classname);CHKERRQ(ierr);
2832fbb82ab5SLawrence Mitchell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerIntFacets, &patch->formatIntFacets, &patch->viewIntFacets);CHKERRQ(ierr);
2833fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exterior_facets_view", patch->classname);CHKERRQ(ierr);
2834fbb82ab5SLawrence Mitchell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerExtFacets, &patch->formatExtFacets, &patch->viewExtFacets);CHKERRQ(ierr);
2835fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_points_view", patch->classname);CHKERRQ(ierr);
283610534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerPoints, &patch->formatPoints, &patch->viewPoints);CHKERRQ(ierr);
2837fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_section_view", patch->classname);CHKERRQ(ierr);
283810534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerSection, &patch->formatSection, &patch->viewSection);CHKERRQ(ierr);
2839fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_mat_view", patch->classname);CHKERRQ(ierr);
284010534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerMatrix, &patch->formatMatrix, &patch->viewMatrix);CHKERRQ(ierr);
28414bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsTail();CHKERRQ(ierr);
28425f824522SMatthew G. Knepley   patch->optionsSet = PETSC_TRUE;
28434bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
28444bbf5ea8SMatthew G. Knepley }
28454bbf5ea8SMatthew G. Knepley 
28464bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUpOnBlocks_PATCH(PC pc)
28474bbf5ea8SMatthew G. Knepley {
28484bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch = (PC_PATCH*) pc->data;
28494bbf5ea8SMatthew G. Knepley   KSPConvergedReason reason;
28504bbf5ea8SMatthew G. Knepley   PetscInt           i;
28514bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
28524bbf5ea8SMatthew G. Knepley 
28534bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2854a1eac568SLawrence Mitchell   if (!patch->save_operators) {
2855a1eac568SLawrence Mitchell     /* Can't do this here because the sub KSPs don't have an operator attached yet. */
2856a1eac568SLawrence Mitchell     PetscFunctionReturn(0);
2857a1eac568SLawrence Mitchell   }
28584bbf5ea8SMatthew G. Knepley   for (i = 0; i < patch->npatch; ++i) {
2859dadc69c5SMatthew G. Knepley     if (!((KSP) patch->solver[i])->setfromoptionscalled) {
2860dadc69c5SMatthew G. Knepley       ierr = KSPSetFromOptions((KSP) patch->solver[i]);CHKERRQ(ierr);
2861a1eac568SLawrence Mitchell     }
2862dadc69c5SMatthew G. Knepley     ierr = KSPSetUp((KSP) patch->solver[i]);CHKERRQ(ierr);
2863dadc69c5SMatthew G. Knepley     ierr = KSPGetConvergedReason((KSP) patch->solver[i], &reason);CHKERRQ(ierr);
2864c0decd05SBarry Smith     if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR;
28654bbf5ea8SMatthew G. Knepley   }
28664bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
28674bbf5ea8SMatthew G. Knepley }
28684bbf5ea8SMatthew G. Knepley 
28694bbf5ea8SMatthew G. Knepley static PetscErrorCode PCView_PATCH(PC pc, PetscViewer viewer)
28704bbf5ea8SMatthew G. Knepley {
28714bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
28724bbf5ea8SMatthew G. Knepley   PetscViewer    sviewer;
28734bbf5ea8SMatthew G. Knepley   PetscBool      isascii;
28744bbf5ea8SMatthew G. Knepley   PetscMPIInt    rank;
28754bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
28764bbf5ea8SMatthew G. Knepley 
28774bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
28784bbf5ea8SMatthew G. Knepley   /* TODO Redo tabbing with set tbas in new style */
28794bbf5ea8SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii);CHKERRQ(ierr);
28804bbf5ea8SMatthew G. Knepley   if (!isascii) PetscFunctionReturn(0);
28814bbf5ea8SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) pc), &rank);CHKERRQ(ierr);
28824bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
28834bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "Subspace Correction preconditioner with %d patches\n", patch->npatch);CHKERRQ(ierr);
288461c4b389SFlorian Wechsung   if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
2885c2e6f3c0SFlorian Wechsung     ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: multiplicative\n");CHKERRQ(ierr);
2886e047a90bSFlorian Wechsung   } else {
288773ec7555SLawrence Mitchell     ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: additive\n");CHKERRQ(ierr);
2888c2e6f3c0SFlorian Wechsung   }
28894bbf5ea8SMatthew G. Knepley   if (patch->partition_of_unity) {ierr = PetscViewerASCIIPrintf(viewer, "Weighting by partition of unity\n");CHKERRQ(ierr);}
28904bbf5ea8SMatthew G. Knepley   else                           {ierr = PetscViewerASCIIPrintf(viewer, "Not weighting by partition of unity\n");CHKERRQ(ierr);}
28914bbf5ea8SMatthew G. Knepley   if (patch->symmetrise_sweep) {ierr = PetscViewerASCIIPrintf(viewer, "Symmetrising sweep (start->end, then end->start)\n");CHKERRQ(ierr);}
28924bbf5ea8SMatthew G. Knepley   else                         {ierr = PetscViewerASCIIPrintf(viewer, "Not symmetrising sweep\n");CHKERRQ(ierr);}
28934bbf5ea8SMatthew G. Knepley   if (!patch->save_operators) {ierr = PetscViewerASCIIPrintf(viewer, "Not saving patch operators (rebuilt every PCApply)\n");CHKERRQ(ierr);}
28944bbf5ea8SMatthew G. Knepley   else                        {ierr = PetscViewerASCIIPrintf(viewer, "Saving patch operators (rebuilt every PCSetUp)\n");CHKERRQ(ierr);}
28954bbf5ea8SMatthew G. Knepley   if (patch->patchconstructop == PCPatchConstruct_Star)       {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: star\n");CHKERRQ(ierr);}
28964bbf5ea8SMatthew G. Knepley   else if (patch->patchconstructop == PCPatchConstruct_Vanka) {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: Vanka\n");CHKERRQ(ierr);}
28974bbf5ea8SMatthew G. Knepley   else if (patch->patchconstructop == PCPatchConstruct_User)  {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: user-specified\n");CHKERRQ(ierr);}
28984bbf5ea8SMatthew G. Knepley   else                                                        {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: unknown\n");CHKERRQ(ierr);}
28995d30859aSPatrick Farrell 
29005d30859aSPatrick Farrell   if (patch->isNonlinear) {
29015d30859aSPatrick Farrell     ierr = PetscViewerASCIIPrintf(viewer, "SNES on patches (all same):\n");CHKERRQ(ierr);
29025d30859aSPatrick Farrell   } else {
29035d30859aSPatrick Farrell     ierr = PetscViewerASCIIPrintf(viewer, "KSP on patches (all same):\n");CHKERRQ(ierr);
29045d30859aSPatrick Farrell   }
2905dadc69c5SMatthew G. Knepley   if (patch->solver) {
29064bbf5ea8SMatthew G. Knepley     ierr = PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr);
29074bbf5ea8SMatthew G. Knepley     if (!rank) {
29084bbf5ea8SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(sviewer);CHKERRQ(ierr);
2909dadc69c5SMatthew G. Knepley       ierr = PetscObjectView(patch->solver[0], sviewer);CHKERRQ(ierr);
29104bbf5ea8SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(sviewer);CHKERRQ(ierr);
29114bbf5ea8SMatthew G. Knepley     }
29124bbf5ea8SMatthew G. Knepley     ierr = PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr);
29134bbf5ea8SMatthew G. Knepley   } else {
29144bbf5ea8SMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2915dadc69c5SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Solver not yet set.\n");CHKERRQ(ierr);
29164bbf5ea8SMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
29174bbf5ea8SMatthew G. Knepley   }
29184bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
29194bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
29204bbf5ea8SMatthew G. Knepley }
29214bbf5ea8SMatthew G. Knepley 
2922e5893cccSMatthew G. Knepley /*MC
292398ed095eSMatthew G. Knepley   PCPATCH - A PC object that encapsulates flexible definition of blocks for overlapping and non-overlapping
292498ed095eSMatthew G. Knepley             small block additive preconditioners. Block definition is based on topology from
2925e5893cccSMatthew G. Knepley             a DM and equation numbering from a PetscSection.
2926e5893cccSMatthew G. Knepley 
2927e5893cccSMatthew G. Knepley   Options Database Keys:
2928e5893cccSMatthew G. Knepley + -pc_patch_cells_view   - Views the process local cell numbers for each patch
2929e5893cccSMatthew G. Knepley . -pc_patch_points_view  - Views the process local mesh point numbers for each patch
2930e5893cccSMatthew G. Knepley . -pc_patch_g2l_view     - Views the map between global dofs and patch local dofs for each patch
2931e5893cccSMatthew G. Knepley . -pc_patch_patches_view - Views the global dofs associated with each patch and its boundary
2932e5893cccSMatthew G. Knepley - -pc_patch_sub_mat_view - Views the matrix associated with each patch
2933e5893cccSMatthew G. Knepley 
2934e5893cccSMatthew G. Knepley   Level: intermediate
2935e5893cccSMatthew G. Knepley 
2936e5893cccSMatthew G. Knepley .seealso: PCType, PCCreate(), PCSetType()
2937e5893cccSMatthew G. Knepley M*/
2938642283e9SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PCCreate_Patch(PC pc)
29394bbf5ea8SMatthew G. Knepley {
29404bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch;
29414bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
29424bbf5ea8SMatthew G. Knepley 
29434bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
29444bbf5ea8SMatthew G. Knepley   ierr = PetscNewLog(pc, &patch);CHKERRQ(ierr);
29454bbf5ea8SMatthew G. Knepley 
2946e4c66b91SPatrick Farrell   if (patch->subspaces_to_exclude) {
2947e4c66b91SPatrick Farrell     PetscHSetIDestroy(&patch->subspaces_to_exclude);
2948e4c66b91SPatrick Farrell   }
2949e4c66b91SPatrick Farrell   PetscHSetICreate(&patch->subspaces_to_exclude);
2950e4c66b91SPatrick Farrell 
295110534d48SPatrick Farrell   patch->classname = "pc";
2952debbdec3SPatrick Farrell   patch->isNonlinear = PETSC_FALSE;
295310534d48SPatrick Farrell 
29544bbf5ea8SMatthew G. Knepley   /* Set some defaults */
29555f824522SMatthew G. Knepley   patch->combined           = PETSC_FALSE;
29564bbf5ea8SMatthew G. Knepley   patch->save_operators     = PETSC_TRUE;
295761c4b389SFlorian Wechsung   patch->local_composition_type = PC_COMPOSITE_ADDITIVE;
29584bbf5ea8SMatthew G. Knepley   patch->partition_of_unity = PETSC_FALSE;
29594bbf5ea8SMatthew G. Knepley   patch->codim              = -1;
29604bbf5ea8SMatthew G. Knepley   patch->dim                = -1;
29614bbf5ea8SMatthew G. Knepley   patch->vankadim           = -1;
29625f824522SMatthew G. Knepley   patch->ignoredim          = -1;
2963b525f888SPatrick Farrell   patch->pardecomp_overlap  = 0;
29644bbf5ea8SMatthew G. Knepley   patch->patchconstructop   = PCPatchConstruct_Star;
29654bbf5ea8SMatthew G. Knepley   patch->symmetrise_sweep   = PETSC_FALSE;
29665f824522SMatthew G. Knepley   patch->npatch             = 0;
29674bbf5ea8SMatthew G. Knepley   patch->userIS             = NULL;
29685f824522SMatthew G. Knepley   patch->optionsSet         = PETSC_FALSE;
29694bbf5ea8SMatthew G. Knepley   patch->iterationSet       = NULL;
29704bbf5ea8SMatthew G. Knepley   patch->user_patches       = PETSC_FALSE;
29715f824522SMatthew G. Knepley   ierr = PetscStrallocpy(MATDENSE, (char **) &patch->sub_mat_type);CHKERRQ(ierr);
29725f824522SMatthew G. Knepley   patch->viewPatches        = PETSC_FALSE;
29735f824522SMatthew G. Knepley   patch->viewCells          = PETSC_FALSE;
29745f824522SMatthew G. Knepley   patch->viewPoints         = PETSC_FALSE;
29755f824522SMatthew G. Knepley   patch->viewSection        = PETSC_FALSE;
29765f824522SMatthew G. Knepley   patch->viewMatrix         = PETSC_FALSE;
2977dadc69c5SMatthew G. Knepley   patch->setupsolver        = PCSetUp_PATCH_Linear;
2978dadc69c5SMatthew G. Knepley   patch->applysolver        = PCApply_PATCH_Linear;
2979dadc69c5SMatthew G. Knepley   patch->resetsolver        = PCReset_PATCH_Linear;
2980dadc69c5SMatthew G. Knepley   patch->destroysolver      = PCDestroy_PATCH_Linear;
29816c9c532dSPatrick Farrell   patch->updatemultiplicative = PCUpdateMultiplicative_PATCH_Linear;
298247aca4a6SPatrick Farrell   patch->dofMappingWithoutToWithArtificial = NULL;
298347aca4a6SPatrick Farrell   patch->dofMappingWithoutToWithAll = NULL;
29844bbf5ea8SMatthew G. Knepley 
29854bbf5ea8SMatthew G. Knepley   pc->data                 = (void *) patch;
29864bbf5ea8SMatthew G. Knepley   pc->ops->apply           = PCApply_PATCH;
29874bbf5ea8SMatthew G. Knepley   pc->ops->applytranspose  = 0; /* PCApplyTranspose_PATCH; */
29884bbf5ea8SMatthew G. Knepley   pc->ops->setup           = PCSetUp_PATCH;
29894bbf5ea8SMatthew G. Knepley   pc->ops->reset           = PCReset_PATCH;
29904bbf5ea8SMatthew G. Knepley   pc->ops->destroy         = PCDestroy_PATCH;
29914bbf5ea8SMatthew G. Knepley   pc->ops->setfromoptions  = PCSetFromOptions_PATCH;
29924bbf5ea8SMatthew G. Knepley   pc->ops->setuponblocks   = PCSetUpOnBlocks_PATCH;
29934bbf5ea8SMatthew G. Knepley   pc->ops->view            = PCView_PATCH;
29944bbf5ea8SMatthew G. Knepley   pc->ops->applyrichardson = 0;
29954bbf5ea8SMatthew G. Knepley 
29964bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
29974bbf5ea8SMatthew G. Knepley }
2998