xref: /petsc/src/ksp/pc/impls/patch/pcpatch.c (revision 2d76c0eea226fe2cf2283ed4f2fdf94481d87c66) !
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;
900a390943SPatrick Farrell   PetscErrorCode  ierr;
910a390943SPatrick Farrell 
920a390943SPatrick Farrell   PetscFunctionBegin;
930a390943SPatrick Farrell   PetscHSetIClear(ht);
940a390943SPatrick Farrell 
950a390943SPatrick Farrell   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
960a390943SPatrick Farrell   ierr = DMPlexGetChart(plex, &pStart, &pEnd);CHKERRQ(ierr);
970a390943SPatrick Farrell 
980a390943SPatrick Farrell   ierr = DMHasLabel(dm, "pyop2_ghost", &isFiredrake);CHKERRQ(ierr);
990a390943SPatrick Farrell   if (isFiredrake) {
1000a390943SPatrick Farrell     ierr = DMGetLabel(dm, "pyop2_ghost", &ghost);CHKERRQ(ierr);
1010a390943SPatrick Farrell     ierr = DMLabelCreateIndex(ghost, pStart, pEnd);CHKERRQ(ierr);
1020a390943SPatrick Farrell   } else {
1030a390943SPatrick Farrell     PetscSF sf;
1040a390943SPatrick Farrell     ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
1050a390943SPatrick Farrell     ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
1060a390943SPatrick Farrell     nleaves = PetscMax(nleaves, 0);
1070a390943SPatrick Farrell   }
1080a390943SPatrick Farrell 
1090a390943SPatrick Farrell   for (PetscInt point = pStart; point < pEnd; ++point) {
1100a390943SPatrick Farrell     if (ghost) {ierr = DMLabelHasPoint(ghost, point, &flg);CHKERRQ(ierr);}
1110a390943SPatrick Farrell     else       {ierr = PetscFindInt(point, nleaves, leaves, &loc);CHKERRQ(ierr); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;}
1120a390943SPatrick Farrell     /* Not an owned entity, don't make a cell patch. */
1130a390943SPatrick Farrell     if (flg) continue;
1140a390943SPatrick Farrell     ierr = PetscHSetIAdd(ht, point);CHKERRQ(ierr);
1150a390943SPatrick Farrell   }
1160a390943SPatrick Farrell 
117b525f888SPatrick Farrell   /* Now build the overlap for the patch */
118b525f888SPatrick Farrell   for (PetscInt overlapi = 0; overlapi < patch->pardecomp_overlap; ++overlapi) {
119b525f888SPatrick Farrell     PetscInt index = 0;
120b525f888SPatrick Farrell     PetscInt *htpoints = NULL;
121b525f888SPatrick Farrell     PetscInt htsize;
122b525f888SPatrick Farrell 
123b525f888SPatrick Farrell     ierr = PetscHSetIGetSize(ht, &htsize);CHKERRQ(ierr);
124b525f888SPatrick Farrell     ierr = PetscMalloc1(htsize, &htpoints);CHKERRQ(ierr);
125b525f888SPatrick Farrell     ierr = PetscHSetIGetElems(ht, &index, htpoints);CHKERRQ(ierr);
126b525f888SPatrick Farrell 
127b525f888SPatrick Farrell     for (PetscInt i = 0; i < htsize; ++i) {
128b525f888SPatrick Farrell       point = htpoints[i];
129b525f888SPatrick Farrell 
130b525f888SPatrick Farrell       ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
131b525f888SPatrick Farrell       for (PetscInt si = 0; si < starSize*2; si += 2) {
132b525f888SPatrick Farrell         const PetscInt starp = star[si];
133b525f888SPatrick Farrell         PetscInt       closureSize;
134b525f888SPatrick Farrell         PetscInt      *closure = NULL, ci;
135b525f888SPatrick Farrell 
136b525f888SPatrick Farrell         /* now loop over all entities in the closure of starp */
137b525f888SPatrick Farrell         ierr = DMPlexGetTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
138b525f888SPatrick Farrell         for (PetscInt ci = 0; ci < closureSize*2; ci += 2) {
139b525f888SPatrick Farrell           const PetscInt closstarp = closure[ci];
140b525f888SPatrick Farrell           ierr = PetscHSetIAdd(ht, closstarp);CHKERRQ(ierr);
141b525f888SPatrick Farrell         }
142b525f888SPatrick Farrell         ierr = DMPlexRestoreTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
143b525f888SPatrick Farrell       }
144b525f888SPatrick Farrell       ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
145b525f888SPatrick Farrell     }
146b525f888SPatrick Farrell     ierr = PetscFree(htpoints);CHKERRQ(ierr);
147b525f888SPatrick Farrell   }
148b525f888SPatrick Farrell 
1490a390943SPatrick Farrell   PetscFunctionReturn(0);
1500a390943SPatrick Farrell }
1510a390943SPatrick Farrell 
1524bbf5ea8SMatthew G. Knepley /* The user's already set the patches in patch->userIS. Build the hash tables */
1531b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_User(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
1544bbf5ea8SMatthew G. Knepley {
1554bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch   = (PC_PATCH *) vpatch;
1564bbf5ea8SMatthew G. Knepley   IS              patchis = patch->userIS[point];
1574bbf5ea8SMatthew G. Knepley   PetscInt        n;
1584bbf5ea8SMatthew G. Knepley   const PetscInt *patchdata;
1594bbf5ea8SMatthew G. Knepley   PetscInt        pStart, pEnd, i;
1604bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
1614bbf5ea8SMatthew G. Knepley 
1624bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1631b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(ht);CHKERRQ(ierr);
1641b68eb51SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
1654bbf5ea8SMatthew G. Knepley   ierr = ISGetLocalSize(patchis, &n);CHKERRQ(ierr);
1664bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patchis, &patchdata);CHKERRQ(ierr);
1674bbf5ea8SMatthew G. Knepley   for (i = 0; i < n; ++i) {
1684bbf5ea8SMatthew G. Knepley     const PetscInt ownedpoint = patchdata[i];
1694bbf5ea8SMatthew G. Knepley 
1704bbf5ea8SMatthew G. Knepley     if (ownedpoint < pStart || ownedpoint >= pEnd) {
1714bbf5ea8SMatthew G. Knepley       SETERRQ3(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D was not in [%D, %D)", ownedpoint, pStart, pEnd);
1724bbf5ea8SMatthew G. Knepley     }
1731b68eb51SMatthew G. Knepley     ierr = PetscHSetIAdd(ht, ownedpoint);CHKERRQ(ierr);
1744bbf5ea8SMatthew G. Knepley   }
1754bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patchis, &patchdata);CHKERRQ(ierr);
1764bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
1774bbf5ea8SMatthew G. Knepley }
1784bbf5ea8SMatthew G. Knepley 
1794bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateDefaultSF_Private(PC pc, PetscInt n, const PetscSF *sf, const PetscInt *bs)
1804bbf5ea8SMatthew G. Knepley {
1814bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
1824bbf5ea8SMatthew G. Knepley   PetscInt       i;
1834bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
1844bbf5ea8SMatthew G. Knepley 
1854bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1864bbf5ea8SMatthew G. Knepley   if (n == 1 && bs[0] == 1) {
1874bbf5ea8SMatthew G. Knepley     patch->defaultSF = sf[0];
1884bbf5ea8SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) patch->defaultSF);CHKERRQ(ierr);
1894bbf5ea8SMatthew G. Knepley   } else {
1904bbf5ea8SMatthew G. Knepley     PetscInt     allRoots = 0, allLeaves = 0;
1914bbf5ea8SMatthew G. Knepley     PetscInt     leafOffset = 0;
1924bbf5ea8SMatthew G. Knepley     PetscInt    *ilocal = NULL;
1934bbf5ea8SMatthew G. Knepley     PetscSFNode *iremote = NULL;
1944bbf5ea8SMatthew G. Knepley     PetscInt    *remoteOffsets = NULL;
1954bbf5ea8SMatthew G. Knepley     PetscInt     index = 0;
1961b68eb51SMatthew G. Knepley     PetscHMapI   rankToIndex;
1974bbf5ea8SMatthew G. Knepley     PetscInt     numRanks = 0;
1984bbf5ea8SMatthew G. Knepley     PetscSFNode *remote = NULL;
1994bbf5ea8SMatthew G. Knepley     PetscSF      rankSF;
2004bbf5ea8SMatthew G. Knepley     PetscInt    *ranks = NULL;
2014bbf5ea8SMatthew G. Knepley     PetscInt    *offsets = NULL;
2024bbf5ea8SMatthew G. Knepley     MPI_Datatype contig;
2031b68eb51SMatthew G. Knepley     PetscHSetI   ranksUniq;
2044bbf5ea8SMatthew G. Knepley 
2054bbf5ea8SMatthew G. Knepley     /* First figure out how many dofs there are in the concatenated numbering.
2064bbf5ea8SMatthew G. Knepley      * allRoots: number of owned global dofs;
2074bbf5ea8SMatthew G. Knepley      * allLeaves: number of visible dofs (global + ghosted).
2084bbf5ea8SMatthew G. Knepley      */
2094bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2104bbf5ea8SMatthew G. Knepley       PetscInt nroots, nleaves;
2114bbf5ea8SMatthew G. Knepley 
2124bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetGraph(sf[i], &nroots, &nleaves, NULL, NULL);CHKERRQ(ierr);
2134bbf5ea8SMatthew G. Knepley       allRoots  += nroots * bs[i];
2144bbf5ea8SMatthew G. Knepley       allLeaves += nleaves * bs[i];
2154bbf5ea8SMatthew G. Knepley     }
2164bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(allLeaves, &ilocal);CHKERRQ(ierr);
2174bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(allLeaves, &iremote);CHKERRQ(ierr);
2184bbf5ea8SMatthew G. Knepley     /* Now build an SF that just contains process connectivity. */
2191b68eb51SMatthew G. Knepley     ierr = PetscHSetICreate(&ranksUniq);CHKERRQ(ierr);
2204bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2214bbf5ea8SMatthew G. Knepley       const PetscMPIInt *ranks = NULL;
2224bbf5ea8SMatthew G. Knepley       PetscInt           nranks, j;
2234bbf5ea8SMatthew G. Knepley 
2244bbf5ea8SMatthew G. Knepley       ierr = PetscSFSetUp(sf[i]);CHKERRQ(ierr);
2254bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetRanks(sf[i], &nranks, &ranks, NULL, NULL, NULL);CHKERRQ(ierr);
2264bbf5ea8SMatthew G. Knepley       /* These are all the ranks who communicate with me. */
2274bbf5ea8SMatthew G. Knepley       for (j = 0; j < nranks; ++j) {
2281b68eb51SMatthew G. Knepley         ierr = PetscHSetIAdd(ranksUniq, (PetscInt) ranks[j]);CHKERRQ(ierr);
2294bbf5ea8SMatthew G. Knepley       }
2304bbf5ea8SMatthew G. Knepley     }
2311b68eb51SMatthew G. Knepley     ierr = PetscHSetIGetSize(ranksUniq, &numRanks);CHKERRQ(ierr);
2324bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(numRanks, &remote);CHKERRQ(ierr);
2334bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(numRanks, &ranks);CHKERRQ(ierr);
2341b68eb51SMatthew G. Knepley     ierr = PetscHSetIGetElems(ranksUniq, &index, ranks);CHKERRQ(ierr);
2354bbf5ea8SMatthew G. Knepley 
2361b68eb51SMatthew G. Knepley     ierr = PetscHMapICreate(&rankToIndex);CHKERRQ(ierr);
2374bbf5ea8SMatthew G. Knepley     for (i = 0; i < numRanks; ++i) {
2384bbf5ea8SMatthew G. Knepley       remote[i].rank  = ranks[i];
2394bbf5ea8SMatthew G. Knepley       remote[i].index = 0;
2401b68eb51SMatthew G. Knepley       ierr = PetscHMapISet(rankToIndex, ranks[i], i);CHKERRQ(ierr);
2414bbf5ea8SMatthew G. Knepley     }
2424bbf5ea8SMatthew G. Knepley     ierr = PetscFree(ranks);CHKERRQ(ierr);
2431b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&ranksUniq);CHKERRQ(ierr);
2444bbf5ea8SMatthew G. Knepley     ierr = PetscSFCreate(PetscObjectComm((PetscObject) pc), &rankSF);CHKERRQ(ierr);
2454bbf5ea8SMatthew G. Knepley     ierr = PetscSFSetGraph(rankSF, 1, numRanks, NULL, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
2464bbf5ea8SMatthew G. Knepley     ierr = PetscSFSetUp(rankSF);CHKERRQ(ierr);
2474bbf5ea8SMatthew G. Knepley     /* OK, use it to communicate the root offset on the remote
2484bbf5ea8SMatthew G. Knepley      * processes for each subspace. */
2494bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(n, &offsets);CHKERRQ(ierr);
2504bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(n*numRanks, &remoteOffsets);CHKERRQ(ierr);
2514bbf5ea8SMatthew G. Knepley 
2524bbf5ea8SMatthew G. Knepley     offsets[0] = 0;
2534bbf5ea8SMatthew G. Knepley     for (i = 1; i < n; ++i) {
2544bbf5ea8SMatthew G. Knepley       PetscInt nroots;
2554bbf5ea8SMatthew G. Knepley 
2564bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetGraph(sf[i-1], &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
2574bbf5ea8SMatthew G. Knepley       offsets[i] = offsets[i-1] + nroots*bs[i-1];
2584bbf5ea8SMatthew G. Knepley     }
2594bbf5ea8SMatthew G. Knepley     /* Offsets are the offsets on the current process of the
2604bbf5ea8SMatthew G. Knepley      * global dof numbering for the subspaces. */
2614bbf5ea8SMatthew G. Knepley     ierr = MPI_Type_contiguous(n, MPIU_INT, &contig);CHKERRQ(ierr);
2624bbf5ea8SMatthew G. Knepley     ierr = MPI_Type_commit(&contig);CHKERRQ(ierr);
2634bbf5ea8SMatthew G. Knepley 
2644bbf5ea8SMatthew G. Knepley     ierr = PetscSFBcastBegin(rankSF, contig, offsets, remoteOffsets);CHKERRQ(ierr);
2654bbf5ea8SMatthew G. Knepley     ierr = PetscSFBcastEnd(rankSF, contig, offsets, remoteOffsets);CHKERRQ(ierr);
2664bbf5ea8SMatthew G. Knepley     ierr = MPI_Type_free(&contig);CHKERRQ(ierr);
2674bbf5ea8SMatthew G. Knepley     ierr = PetscFree(offsets);CHKERRQ(ierr);
2684bbf5ea8SMatthew G. Knepley     ierr = PetscSFDestroy(&rankSF);CHKERRQ(ierr);
2694bbf5ea8SMatthew G. Knepley     /* Now remoteOffsets contains the offsets on the remote
2704bbf5ea8SMatthew G. Knepley      * processes who communicate with me.  So now we can
2714bbf5ea8SMatthew G. Knepley      * concatenate the list of SFs into a single one. */
2724bbf5ea8SMatthew G. Knepley     index = 0;
2734bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2744bbf5ea8SMatthew G. Knepley       const PetscSFNode *remote = NULL;
2754bbf5ea8SMatthew G. Knepley       const PetscInt    *local  = NULL;
2764bbf5ea8SMatthew G. Knepley       PetscInt           nroots, nleaves, j;
2774bbf5ea8SMatthew G. Knepley 
2784bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetGraph(sf[i], &nroots, &nleaves, &local, &remote);CHKERRQ(ierr);
2794bbf5ea8SMatthew G. Knepley       for (j = 0; j < nleaves; ++j) {
2804bbf5ea8SMatthew G. Knepley         PetscInt rank = remote[j].rank;
2814bbf5ea8SMatthew G. Knepley         PetscInt idx, rootOffset, k;
2824bbf5ea8SMatthew G. Knepley 
2831b68eb51SMatthew G. Knepley         ierr = PetscHMapIGet(rankToIndex, rank, &idx);CHKERRQ(ierr);
2844bbf5ea8SMatthew G. Knepley         if (idx == -1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Didn't find rank, huh?");
2854bbf5ea8SMatthew G. Knepley         /* Offset on given rank for ith subspace */
2864bbf5ea8SMatthew G. Knepley         rootOffset = remoteOffsets[n*idx + i];
2874bbf5ea8SMatthew G. Knepley         for (k = 0; k < bs[i]; ++k) {
28873ec7555SLawrence Mitchell           ilocal[index]        = (local ? local[j] : j)*bs[i] + k + leafOffset;
2894bbf5ea8SMatthew G. Knepley           iremote[index].rank  = remote[j].rank;
2904bbf5ea8SMatthew G. Knepley           iremote[index].index = remote[j].index*bs[i] + k + rootOffset;
2914bbf5ea8SMatthew G. Knepley           ++index;
2924bbf5ea8SMatthew G. Knepley         }
2934bbf5ea8SMatthew G. Knepley       }
2944bbf5ea8SMatthew G. Knepley       leafOffset += nleaves * bs[i];
2954bbf5ea8SMatthew G. Knepley     }
2961b68eb51SMatthew G. Knepley     ierr = PetscHMapIDestroy(&rankToIndex);CHKERRQ(ierr);
2974bbf5ea8SMatthew G. Knepley     ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
2984bbf5ea8SMatthew G. Knepley     ierr = PetscSFCreate(PetscObjectComm((PetscObject)pc), &patch->defaultSF);CHKERRQ(ierr);
2994bbf5ea8SMatthew G. Knepley     ierr = PetscSFSetGraph(patch->defaultSF, allRoots, allLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER);CHKERRQ(ierr);
3004bbf5ea8SMatthew G. Knepley   }
3014bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3024bbf5ea8SMatthew G. Knepley }
3034bbf5ea8SMatthew G. Knepley 
3044bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3055f824522SMatthew G. Knepley PetscErrorCode PCPatchSetIgnoreDim(PC pc, PetscInt dim)
3065f824522SMatthew G. Knepley {
3075f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3085f824522SMatthew G. Knepley   PetscFunctionBegin;
3095f824522SMatthew G. Knepley   patch->ignoredim = dim;
3105f824522SMatthew G. Knepley   PetscFunctionReturn(0);
3115f824522SMatthew G. Knepley }
3125f824522SMatthew G. Knepley 
3135f824522SMatthew G. Knepley /* TODO: Docs */
3145f824522SMatthew G. Knepley PetscErrorCode PCPatchGetIgnoreDim(PC pc, PetscInt *dim)
3155f824522SMatthew G. Knepley {
3165f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3175f824522SMatthew G. Knepley   PetscFunctionBegin;
3185f824522SMatthew G. Knepley   *dim = patch->ignoredim;
3195f824522SMatthew G. Knepley   PetscFunctionReturn(0);
3205f824522SMatthew G. Knepley }
3215f824522SMatthew G. Knepley 
3225f824522SMatthew G. Knepley /* TODO: Docs */
3234bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetSaveOperators(PC pc, PetscBool flg)
3244bbf5ea8SMatthew G. Knepley {
3254bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3264bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3274bbf5ea8SMatthew G. Knepley   patch->save_operators = flg;
3284bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3294bbf5ea8SMatthew G. Knepley }
3304bbf5ea8SMatthew G. Knepley 
3314bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3324bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetSaveOperators(PC pc, PetscBool *flg)
3334bbf5ea8SMatthew G. Knepley {
3344bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3354bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3364bbf5ea8SMatthew G. Knepley   *flg = patch->save_operators;
3374bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3384bbf5ea8SMatthew G. Knepley }
3394bbf5ea8SMatthew G. Knepley 
3404bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3414bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetPartitionOfUnity(PC pc, PetscBool flg)
3424bbf5ea8SMatthew G. Knepley {
3434bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3444bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3454bbf5ea8SMatthew G. Knepley   patch->partition_of_unity = flg;
3464bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3474bbf5ea8SMatthew G. Knepley }
3484bbf5ea8SMatthew G. Knepley 
3494bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3504bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetPartitionOfUnity(PC pc, PetscBool *flg)
3514bbf5ea8SMatthew G. Knepley {
3524bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3534bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3544bbf5ea8SMatthew G. Knepley   *flg = patch->partition_of_unity;
3554bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3564bbf5ea8SMatthew G. Knepley }
3574bbf5ea8SMatthew G. Knepley 
3584bbf5ea8SMatthew G. Knepley /* TODO: Docs */
35961c4b389SFlorian Wechsung PetscErrorCode PCPatchSetLocalComposition(PC pc, PCCompositeType type)
360c2e6f3c0SFlorian Wechsung {
361c2e6f3c0SFlorian Wechsung   PC_PATCH *patch = (PC_PATCH *) pc->data;
362c2e6f3c0SFlorian Wechsung   PetscFunctionBegin;
36361c4b389SFlorian 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");
36461c4b389SFlorian Wechsung   patch->local_composition_type = type;
365c2e6f3c0SFlorian Wechsung   PetscFunctionReturn(0);
366c2e6f3c0SFlorian Wechsung }
367c2e6f3c0SFlorian Wechsung 
368c2e6f3c0SFlorian Wechsung /* TODO: Docs */
36961c4b389SFlorian Wechsung PetscErrorCode PCPatchGetLocalComposition(PC pc, PCCompositeType *type)
370c2e6f3c0SFlorian Wechsung {
371c2e6f3c0SFlorian Wechsung   PC_PATCH *patch = (PC_PATCH *) pc->data;
372c2e6f3c0SFlorian Wechsung   PetscFunctionBegin;
37361c4b389SFlorian Wechsung   *type = patch->local_composition_type;
374c2e6f3c0SFlorian Wechsung   PetscFunctionReturn(0);
375c2e6f3c0SFlorian Wechsung }
376c2e6f3c0SFlorian Wechsung 
377c2e6f3c0SFlorian Wechsung /* TODO: Docs */
3784bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetSubMatType(PC pc, MatType sub_mat_type)
3794bbf5ea8SMatthew G. Knepley {
3804bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
3814bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
3824bbf5ea8SMatthew G. Knepley 
3834bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3844bbf5ea8SMatthew G. Knepley   if (patch->sub_mat_type) {ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr);}
3854bbf5ea8SMatthew G. Knepley   ierr = PetscStrallocpy(sub_mat_type, (char **) &patch->sub_mat_type);CHKERRQ(ierr);
3864bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3874bbf5ea8SMatthew G. Knepley }
3884bbf5ea8SMatthew G. Knepley 
3894bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3904bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetSubMatType(PC pc, MatType *sub_mat_type)
3914bbf5ea8SMatthew G. Knepley {
3924bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3934bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3944bbf5ea8SMatthew G. Knepley   *sub_mat_type = patch->sub_mat_type;
3954bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3964bbf5ea8SMatthew G. Knepley }
3974bbf5ea8SMatthew G. Knepley 
3984bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3994bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetCellNumbering(PC pc, PetscSection cellNumbering)
4004bbf5ea8SMatthew G. Knepley {
4014bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
4024bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
4034bbf5ea8SMatthew G. Knepley 
4044bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4054bbf5ea8SMatthew G. Knepley   patch->cellNumbering = cellNumbering;
4064bbf5ea8SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) cellNumbering);CHKERRQ(ierr);
4074bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4084bbf5ea8SMatthew G. Knepley }
4094bbf5ea8SMatthew G. Knepley 
4104bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4114bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetCellNumbering(PC pc, PetscSection *cellNumbering)
4124bbf5ea8SMatthew G. Knepley {
4134bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4144bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4154bbf5ea8SMatthew G. Knepley   *cellNumbering = patch->cellNumbering;
4164bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4174bbf5ea8SMatthew G. Knepley }
4184bbf5ea8SMatthew G. Knepley 
4194bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4204bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetConstructType(PC pc, PCPatchConstructType ctype, PetscErrorCode (*func)(PC, PetscInt *, IS **, IS *, void *), void *ctx)
4214bbf5ea8SMatthew G. Knepley {
4224bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4234bbf5ea8SMatthew G. Knepley 
4244bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4254bbf5ea8SMatthew G. Knepley   patch->ctype = ctype;
4264bbf5ea8SMatthew G. Knepley   switch (ctype) {
4274bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
42840c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
4294bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Star;
4304bbf5ea8SMatthew G. Knepley     break;
4314bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
43240c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
4334bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Vanka;
4344bbf5ea8SMatthew G. Knepley     break;
435e5b9877fSPatrick Farrell   case PC_PATCH_PARDECOMP:
4360a390943SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
437e5b9877fSPatrick Farrell     patch->patchconstructop = PCPatchConstruct_Pardecomp;
4380a390943SPatrick Farrell     break;
4394bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
4404bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
4414bbf5ea8SMatthew G. Knepley     patch->user_patches     = PETSC_TRUE;
4424bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_User;
443bdd9e0cdSPatrick Farrell     if (func) {
4444bbf5ea8SMatthew G. Knepley       patch->userpatchconstructionop = func;
4454bbf5ea8SMatthew G. Knepley       patch->userpatchconstructctx   = ctx;
446bdd9e0cdSPatrick Farrell     }
4474bbf5ea8SMatthew G. Knepley     break;
4484bbf5ea8SMatthew G. Knepley   default:
4494bbf5ea8SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype);
4504bbf5ea8SMatthew G. Knepley   }
4514bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4524bbf5ea8SMatthew G. Knepley }
4534bbf5ea8SMatthew G. Knepley 
4544bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4554bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetConstructType(PC pc, PCPatchConstructType *ctype, PetscErrorCode (**func)(PC, PetscInt *, IS **, IS *, void *), void **ctx)
4564bbf5ea8SMatthew G. Knepley {
4574bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4584bbf5ea8SMatthew G. Knepley 
4594bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4604bbf5ea8SMatthew G. Knepley   *ctype = patch->ctype;
4614bbf5ea8SMatthew G. Knepley   switch (patch->ctype) {
4624bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
4634bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
464e5b9877fSPatrick Farrell   case PC_PATCH_PARDECOMP:
4654bbf5ea8SMatthew G. Knepley     break;
4664bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
4674bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
4684bbf5ea8SMatthew G. Knepley     *func = patch->userpatchconstructionop;
4694bbf5ea8SMatthew G. Knepley     *ctx  = patch->userpatchconstructctx;
4704bbf5ea8SMatthew G. Knepley     break;
4714bbf5ea8SMatthew G. Knepley   default:
4724bbf5ea8SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype);
4734bbf5ea8SMatthew G. Knepley   }
4744bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4754bbf5ea8SMatthew G. Knepley }
4764bbf5ea8SMatthew G. Knepley 
4774bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4784bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfo(PC pc, PetscInt nsubspaces, DM *dms, PetscInt *bs, PetscInt *nodesPerCell, const PetscInt **cellNodeMap,
4794bbf5ea8SMatthew G. Knepley                                             const PetscInt *subspaceOffsets, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
4804bbf5ea8SMatthew G. Knepley {
4814bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
4825f824522SMatthew G. Knepley   DM             dm;
4834bbf5ea8SMatthew G. Knepley   PetscSF       *sfs;
4845f824522SMatthew G. Knepley   PetscInt       cStart, cEnd, i, j;
4854bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
4864bbf5ea8SMatthew G. Knepley 
4874bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4885f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
4895f824522SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
4904bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &sfs);CHKERRQ(ierr);
4914bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->dofSection);CHKERRQ(ierr);
4924bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->bs);CHKERRQ(ierr);
4934bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->nodesPerCell);CHKERRQ(ierr);
4944bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->cellNodeMap);CHKERRQ(ierr);
4954bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces+1, &patch->subspaceOffsets);CHKERRQ(ierr);
4964bbf5ea8SMatthew G. Knepley 
4974bbf5ea8SMatthew G. Knepley   patch->nsubspaces       = nsubspaces;
4984bbf5ea8SMatthew G. Knepley   patch->totalDofsPerCell = 0;
4994bbf5ea8SMatthew G. Knepley   for (i = 0; i < nsubspaces; ++i) {
5004bbf5ea8SMatthew G. Knepley     ierr = DMGetDefaultSection(dms[i], &patch->dofSection[i]);CHKERRQ(ierr);
5014bbf5ea8SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) patch->dofSection[i]);CHKERRQ(ierr);
5024bbf5ea8SMatthew G. Knepley     ierr = DMGetDefaultSF(dms[i], &sfs[i]);CHKERRQ(ierr);
5034bbf5ea8SMatthew G. Knepley     patch->bs[i]              = bs[i];
5044bbf5ea8SMatthew G. Knepley     patch->nodesPerCell[i]    = nodesPerCell[i];
5054bbf5ea8SMatthew G. Knepley     patch->totalDofsPerCell  += nodesPerCell[i]*bs[i];
50680e8a965SFlorian Wechsung     ierr = PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i]);CHKERRQ(ierr);
50780e8a965SFlorian Wechsung     for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
5084bbf5ea8SMatthew G. Knepley     patch->subspaceOffsets[i] = subspaceOffsets[i];
5094bbf5ea8SMatthew G. Knepley   }
5104bbf5ea8SMatthew G. Knepley   ierr = PCPatchCreateDefaultSF_Private(pc, nsubspaces, sfs, patch->bs);CHKERRQ(ierr);
5114bbf5ea8SMatthew G. Knepley   ierr = PetscFree(sfs);CHKERRQ(ierr);
5124bbf5ea8SMatthew G. Knepley 
5134bbf5ea8SMatthew G. Knepley   patch->subspaceOffsets[nsubspaces] = subspaceOffsets[nsubspaces];
5144bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes);CHKERRQ(ierr);
5154bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes);CHKERRQ(ierr);
5164bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5174bbf5ea8SMatthew G. Knepley }
5184bbf5ea8SMatthew G. Knepley 
5194bbf5ea8SMatthew G. Knepley /* TODO: Docs */
5205f824522SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfoCombined(PC pc, DM dm, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
5215f824522SMatthew G. Knepley {
5225f824522SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
5235f824522SMatthew G. Knepley   PetscInt       cStart, cEnd, i, j;
5245f824522SMatthew G. Knepley   PetscErrorCode ierr;
5255f824522SMatthew G. Knepley 
5265f824522SMatthew G. Knepley   PetscFunctionBegin;
5275f824522SMatthew G. Knepley   patch->combined = PETSC_TRUE;
5285f824522SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
5295f824522SMatthew G. Knepley   ierr = DMGetNumFields(dm, &patch->nsubspaces);CHKERRQ(ierr);
5305f824522SMatthew G. Knepley   ierr = PetscCalloc1(patch->nsubspaces, &patch->dofSection);CHKERRQ(ierr);
5315f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->bs);CHKERRQ(ierr);
5325f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->nodesPerCell);CHKERRQ(ierr);
5335f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->cellNodeMap);CHKERRQ(ierr);
5345f824522SMatthew G. Knepley   ierr = PetscCalloc1(patch->nsubspaces+1, &patch->subspaceOffsets);CHKERRQ(ierr);
5355f824522SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &patch->dofSection[0]);CHKERRQ(ierr);
5365f824522SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) patch->dofSection[0]);CHKERRQ(ierr);
5375f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(patch->dofSection[0], &patch->subspaceOffsets[patch->nsubspaces]);CHKERRQ(ierr);
5385f824522SMatthew G. Knepley   patch->totalDofsPerCell = 0;
5395f824522SMatthew G. Knepley   for (i = 0; i < patch->nsubspaces; ++i) {
5405f824522SMatthew G. Knepley     patch->bs[i]             = 1;
5415f824522SMatthew G. Knepley     patch->nodesPerCell[i]   = nodesPerCell[i];
5425f824522SMatthew G. Knepley     patch->totalDofsPerCell += nodesPerCell[i];
5435f824522SMatthew G. Knepley     ierr = PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i]);CHKERRQ(ierr);
5445f824522SMatthew G. Knepley     for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
5455f824522SMatthew G. Knepley   }
5465f824522SMatthew G. Knepley   ierr = DMGetDefaultSF(dm, &patch->defaultSF);CHKERRQ(ierr);
5475f824522SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) patch->defaultSF);CHKERRQ(ierr);
5485f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes);CHKERRQ(ierr);
5495f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes);CHKERRQ(ierr);
5505f824522SMatthew G. Knepley   PetscFunctionReturn(0);
5515f824522SMatthew G. Knepley }
5525f824522SMatthew G. Knepley 
5535f824522SMatthew G. Knepley /*@C
5545f824522SMatthew G. Knepley 
55592d50984SMatthew G. Knepley   PCPatchSetComputeFunction - Set the callback used to compute patch residuals
55692d50984SMatthew G. Knepley 
55792d50984SMatthew G. Knepley   Input Parameters:
55892d50984SMatthew G. Knepley + pc   - The PC
55992d50984SMatthew G. Knepley . func - The callback
56092d50984SMatthew G. Knepley - ctx  - The user context
56192d50984SMatthew G. Knepley 
56292d50984SMatthew G. Knepley   Level: advanced
56392d50984SMatthew G. Knepley 
56492d50984SMatthew G. Knepley   Note:
56592d50984SMatthew G. Knepley   The callback has signature:
56692d50984SMatthew G. Knepley +  usercomputef(pc, point, x, f, cellIS, n, u, ctx)
56792d50984SMatthew G. Knepley +  pc     - The PC
56892d50984SMatthew G. Knepley +  point  - The point
56992d50984SMatthew G. Knepley +  x      - The input solution (not used in linear problems)
57092d50984SMatthew G. Knepley +  f      - The patch residual vector
57192d50984SMatthew G. Knepley +  cellIS - An array of the cell numbers
57292d50984SMatthew G. Knepley +  n      - The size of g2l
57392d50984SMatthew G. Knepley +  g2l    - The global to local dof translation table
57492d50984SMatthew G. Knepley +  ctx    - The user context
57592d50984SMatthew G. Knepley   and can assume that the matrix entries have been set to zero before the call.
57692d50984SMatthew G. Knepley 
57792d50984SMatthew G. Knepley .seealso: PCPatchSetComputeOperator(), PCPatchGetComputeOperator(), PCPatchSetDiscretisationInfo()
57892d50984SMatthew G. Knepley @*/
57939fd2e8aSPatrick Farrell PetscErrorCode PCPatchSetComputeFunction(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Vec, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
58092d50984SMatthew G. Knepley {
58192d50984SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
58292d50984SMatthew G. Knepley 
58392d50984SMatthew G. Knepley   PetscFunctionBegin;
58492d50984SMatthew G. Knepley   patch->usercomputef    = func;
58592d50984SMatthew G. Knepley   patch->usercomputefctx = ctx;
58692d50984SMatthew G. Knepley   PetscFunctionReturn(0);
58792d50984SMatthew G. Knepley }
58892d50984SMatthew G. Knepley 
58992d50984SMatthew G. Knepley /*@C
59092d50984SMatthew G. Knepley 
5915f824522SMatthew G. Knepley   PCPatchSetComputeOperator - Set the callback used to compute patch matrices
5925f824522SMatthew G. Knepley 
5935f824522SMatthew G. Knepley   Input Parameters:
5945f824522SMatthew G. Knepley + pc   - The PC
5955f824522SMatthew G. Knepley . func - The callback
5965f824522SMatthew G. Knepley - ctx  - The user context
5975f824522SMatthew G. Knepley 
5985f824522SMatthew G. Knepley   Level: advanced
5995f824522SMatthew G. Knepley 
6005f824522SMatthew G. Knepley   Note:
6015f824522SMatthew G. Knepley   The callback has signature:
602723f9013SMatthew G. Knepley +  usercomputeop(pc, point, x, mat, cellIS, n, u, ctx)
6035f824522SMatthew G. Knepley +  pc     - The PC
604bdd9e0cdSPatrick Farrell +  point  - The point
605723f9013SMatthew G. Knepley +  x      - The input solution (not used in linear problems)
6065f824522SMatthew G. Knepley +  mat    - The patch matrix
6076f158342SMatthew G. Knepley +  cellIS - An array of the cell numbers
6085f824522SMatthew G. Knepley +  n      - The size of g2l
6095f824522SMatthew G. Knepley +  g2l    - The global to local dof translation table
6105f824522SMatthew G. Knepley +  ctx    - The user context
6115f824522SMatthew G. Knepley   and can assume that the matrix entries have been set to zero before the call.
6125f824522SMatthew G. Knepley 
613723f9013SMatthew G. Knepley .seealso: PCPatchGetComputeOperator(), PCPatchSetComputeFunction(), PCPatchSetDiscretisationInfo()
6145f824522SMatthew G. Knepley @*/
6154d04e9f1SPatrick Farrell PetscErrorCode PCPatchSetComputeOperator(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Mat, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
6164bbf5ea8SMatthew G. Knepley {
6174bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
6184bbf5ea8SMatthew G. Knepley 
6194bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
6204bbf5ea8SMatthew G. Knepley   patch->usercomputeop    = func;
621723f9013SMatthew G. Knepley   patch->usercomputeopctx = ctx;
6224bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
6234bbf5ea8SMatthew G. Knepley }
6244bbf5ea8SMatthew G. Knepley 
6254bbf5ea8SMatthew G. Knepley /* On entry, ht contains the topological entities whose dofs we are responsible for solving for;
6264bbf5ea8SMatthew G. Knepley    on exit, cht contains all the topological entities we need to compute their residuals.
6274bbf5ea8SMatthew G. Knepley    In full generality this should incorporate knowledge of the sparsity pattern of the matrix;
6284bbf5ea8SMatthew G. Knepley    here we assume a standard FE sparsity pattern.*/
6294bbf5ea8SMatthew G. Knepley /* TODO: Use DMPlexGetAdjacency() */
6301b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchCompleteCellPatch(PC pc, PetscHSetI ht, PetscHSetI cht)
6314bbf5ea8SMatthew G. Knepley {
6325f824522SMatthew G. Knepley   DM             dm;
6331b68eb51SMatthew G. Knepley   PetscHashIter  hi;
6344bbf5ea8SMatthew G. Knepley   PetscInt       point;
6354bbf5ea8SMatthew G. Knepley   PetscInt      *star = NULL, *closure = NULL;
6364c954380SMatthew G. Knepley   PetscInt       ignoredim, iStart = 0, iEnd = -1, starSize, closureSize, si, ci;
6374bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
6384bbf5ea8SMatthew G. Knepley 
6394bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
6405f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
6415f824522SMatthew G. Knepley   ierr = PCPatchGetIgnoreDim(pc, &ignoredim);CHKERRQ(ierr);
6425f824522SMatthew G. Knepley   if (ignoredim >= 0) {ierr = DMPlexGetDepthStratum(dm, ignoredim, &iStart, &iEnd);CHKERRQ(ierr);}
6431b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(cht);CHKERRQ(ierr);
6441b68eb51SMatthew G. Knepley   PetscHashIterBegin(ht, hi);
6451b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(ht, hi)) {
6464c954380SMatthew G. Knepley 
6471b68eb51SMatthew G. Knepley     PetscHashIterGetKey(ht, hi, point);
6481b68eb51SMatthew G. Knepley     PetscHashIterNext(ht, hi);
6494bbf5ea8SMatthew G. Knepley 
6504bbf5ea8SMatthew G. Knepley     /* Loop over all the cells that this point connects to */
6514bbf5ea8SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
6525f824522SMatthew G. Knepley     for (si = 0; si < starSize*2; si += 2) {
6534c954380SMatthew G. Knepley       const PetscInt ownedpoint = star[si];
6545f824522SMatthew G. Knepley       /* TODO Check for point in cht before running through closure again */
6554bbf5ea8SMatthew G. Knepley       /* now loop over all entities in the closure of that cell */
6564bbf5ea8SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, ownedpoint, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
6575f824522SMatthew G. Knepley       for (ci = 0; ci < closureSize*2; ci += 2) {
6584c954380SMatthew G. Knepley         const PetscInt seenpoint = closure[ci];
6595f824522SMatthew G. Knepley         if (ignoredim >= 0 && seenpoint >= iStart && seenpoint < iEnd) continue;
6601b68eb51SMatthew G. Knepley         ierr = PetscHSetIAdd(cht, seenpoint);CHKERRQ(ierr);
6614bbf5ea8SMatthew G. Knepley       }
662*2d76c0eeSPatrick Farrell       ierr = DMPlexRestoreTransitiveClosure(dm, ownedpoint, PETSC_TRUE, NULL, &closure);CHKERRQ(ierr);
6634bbf5ea8SMatthew G. Knepley     }
664*2d76c0eeSPatrick Farrell     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, NULL, &star);CHKERRQ(ierr);
6654bbf5ea8SMatthew G. Knepley   }
6665f824522SMatthew G. Knepley   PetscFunctionReturn(0);
6675f824522SMatthew G. Knepley }
6685f824522SMatthew G. Knepley 
6695f824522SMatthew G. Knepley static PetscErrorCode PCPatchGetGlobalDofs(PC pc, PetscSection dofSection[], PetscInt f, PetscBool combined, PetscInt p, PetscInt *dof, PetscInt *off)
6705f824522SMatthew G. Knepley {
6715f824522SMatthew G. Knepley   PetscErrorCode ierr;
6725f824522SMatthew G. Knepley 
6735f824522SMatthew G. Knepley   PetscFunctionBegin;
6745f824522SMatthew G. Knepley   if (combined) {
6755f824522SMatthew G. Knepley     if (f < 0) {
6765f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetDof(dofSection[0], p, dof);CHKERRQ(ierr);}
6775f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetOffset(dofSection[0], p, off);CHKERRQ(ierr);}
6785f824522SMatthew G. Knepley     } else {
6795f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetFieldDof(dofSection[0], p, f, dof);CHKERRQ(ierr);}
6805f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetFieldOffset(dofSection[0], p, f, off);CHKERRQ(ierr);}
6815f824522SMatthew G. Knepley     }
6825f824522SMatthew G. Knepley   } else {
6835f824522SMatthew G. Knepley     if (f < 0) {
6845f824522SMatthew G. Knepley       PC_PATCH *patch = (PC_PATCH *) pc->data;
6855f824522SMatthew G. Knepley       PetscInt  fdof, g;
6865f824522SMatthew G. Knepley 
6875f824522SMatthew G. Knepley       if (dof) {
6885f824522SMatthew G. Knepley         *dof = 0;
6895f824522SMatthew G. Knepley         for (g = 0; g < patch->nsubspaces; ++g) {
6905f824522SMatthew G. Knepley           ierr = PetscSectionGetDof(dofSection[g], p, &fdof);CHKERRQ(ierr);
6915f824522SMatthew G. Knepley           *dof += fdof;
6925f824522SMatthew G. Knepley         }
6935f824522SMatthew G. Knepley       }
694624e31c3SLawrence Mitchell       if (off) {
695624e31c3SLawrence Mitchell         *off = 0;
696624e31c3SLawrence Mitchell         for (g = 0; g < patch->nsubspaces; ++g) {
697624e31c3SLawrence Mitchell           ierr = PetscSectionGetOffset(dofSection[g], p, &fdof);CHKERRQ(ierr);
698624e31c3SLawrence Mitchell           *off += fdof;
699624e31c3SLawrence Mitchell         }
700624e31c3SLawrence Mitchell       }
7015f824522SMatthew G. Knepley     } else {
7025f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetDof(dofSection[f], p, dof);CHKERRQ(ierr);}
7035f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetOffset(dofSection[f], p, off);CHKERRQ(ierr);}
7045f824522SMatthew G. Knepley     }
7055f824522SMatthew G. Knepley   }
7064bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
7074bbf5ea8SMatthew G. Knepley }
7084bbf5ea8SMatthew G. Knepley 
7094bbf5ea8SMatthew G. Knepley /* Given a hash table with a set of topological entities (pts), compute the degrees of
7104bbf5ea8SMatthew G. Knepley    freedom in global concatenated numbering on those entities.
7114bbf5ea8SMatthew G. Knepley    For Vanka smoothing, this needs to do something special: ignore dofs of the
7124bbf5ea8SMatthew G. Knepley    constraint subspace on entities that aren't the base entity we're building the patch
7134bbf5ea8SMatthew G. Knepley    around. */
714e4c66b91SPatrick Farrell static PetscErrorCode PCPatchGetPointDofs(PC pc, PetscHSetI pts, PetscHSetI dofs, PetscInt base, PetscHSetI* subspaces_to_exclude)
7154bbf5ea8SMatthew G. Knepley {
7165f824522SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
7171b68eb51SMatthew G. Knepley   PetscHashIter  hi;
7184bbf5ea8SMatthew G. Knepley   PetscInt       ldof, loff;
7194bbf5ea8SMatthew G. Knepley   PetscInt       k, p;
7204bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
7214bbf5ea8SMatthew G. Knepley 
7224bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
7231b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(dofs);CHKERRQ(ierr);
7244bbf5ea8SMatthew G. Knepley   for (k = 0; k < patch->nsubspaces; ++k) {
7254bbf5ea8SMatthew G. Knepley     PetscInt subspaceOffset = patch->subspaceOffsets[k];
7264bbf5ea8SMatthew G. Knepley     PetscInt bs             = patch->bs[k];
7274bbf5ea8SMatthew G. Knepley     PetscInt j, l;
7284bbf5ea8SMatthew G. Knepley 
729e4c66b91SPatrick Farrell     if (subspaces_to_exclude != NULL) {
730e4c66b91SPatrick Farrell       PetscBool should_exclude_k = PETSC_FALSE;
731e4c66b91SPatrick Farrell       PetscHSetIHas(*subspaces_to_exclude, k, &should_exclude_k);
732e4c66b91SPatrick Farrell       if (should_exclude_k) {
7334bbf5ea8SMatthew G. Knepley         /* only get this subspace dofs at the base entity, not any others */
7345f824522SMatthew G. Knepley         ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, base, &ldof, &loff);CHKERRQ(ierr);
7354bbf5ea8SMatthew G. Knepley         if (0 == ldof) continue;
7364bbf5ea8SMatthew G. Knepley         for (j = loff; j < ldof + loff; ++j) {
7374bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
7384bbf5ea8SMatthew G. Knepley             PetscInt dof = bs*j + l + subspaceOffset;
7391b68eb51SMatthew G. Knepley             ierr = PetscHSetIAdd(dofs, dof);CHKERRQ(ierr);
7404bbf5ea8SMatthew G. Knepley           }
7414bbf5ea8SMatthew G. Knepley         }
7424bbf5ea8SMatthew G. Knepley         continue; /* skip the other dofs of this subspace */
7434bbf5ea8SMatthew G. Knepley       }
744e4c66b91SPatrick Farrell     }
7454bbf5ea8SMatthew G. Knepley 
7461b68eb51SMatthew G. Knepley     PetscHashIterBegin(pts, hi);
7471b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(pts, hi)) {
7481b68eb51SMatthew G. Knepley       PetscHashIterGetKey(pts, hi, p);
7491b68eb51SMatthew G. Knepley       PetscHashIterNext(pts, hi);
7505f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, p, &ldof, &loff);CHKERRQ(ierr);
7514bbf5ea8SMatthew G. Knepley       if (0 == ldof) continue;
7524bbf5ea8SMatthew G. Knepley       for (j = loff; j < ldof + loff; ++j) {
7534bbf5ea8SMatthew G. Knepley         for (l = 0; l < bs; ++l) {
7544bbf5ea8SMatthew G. Knepley           PetscInt dof = bs*j + l + subspaceOffset;
7551b68eb51SMatthew G. Knepley           ierr = PetscHSetIAdd(dofs, dof);CHKERRQ(ierr);
7564bbf5ea8SMatthew G. Knepley         }
7574bbf5ea8SMatthew G. Knepley       }
7584bbf5ea8SMatthew G. Knepley     }
7594bbf5ea8SMatthew G. Knepley   }
7604bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
7614bbf5ea8SMatthew G. Knepley }
7624bbf5ea8SMatthew G. Knepley 
7634bbf5ea8SMatthew G. Knepley /* Given two hash tables A and B, compute the keys in B that are not in A, and put them in C */
7641b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchComputeSetDifference_Private(PetscHSetI A, PetscHSetI B, PetscHSetI C)
7654bbf5ea8SMatthew G. Knepley {
7661b68eb51SMatthew G. Knepley   PetscHashIter  hi;
7671b68eb51SMatthew G. Knepley   PetscInt       key;
7684bbf5ea8SMatthew G. Knepley   PetscBool      flg;
7691b68eb51SMatthew G. Knepley   PetscErrorCode ierr;
7704bbf5ea8SMatthew G. Knepley 
7714bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
7721b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(C);CHKERRQ(ierr);
7731b68eb51SMatthew G. Knepley   PetscHashIterBegin(B, hi);
7741b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(B, hi)) {
7751b68eb51SMatthew G. Knepley     PetscHashIterGetKey(B, hi, key);
7761b68eb51SMatthew G. Knepley     PetscHashIterNext(B, hi);
7771b68eb51SMatthew G. Knepley     ierr = PetscHSetIHas(A, key, &flg);CHKERRQ(ierr);
7781b68eb51SMatthew G. Knepley     if (!flg) {ierr = PetscHSetIAdd(C, key);CHKERRQ(ierr);}
7794bbf5ea8SMatthew G. Knepley   }
7804bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
7814bbf5ea8SMatthew G. Knepley }
7824bbf5ea8SMatthew G. Knepley 
7834bbf5ea8SMatthew G. Knepley /*
7844bbf5ea8SMatthew G. Knepley  * PCPatchCreateCellPatches - create patches.
7854bbf5ea8SMatthew G. Knepley  *
7864bbf5ea8SMatthew G. Knepley  * Input Parameters:
7874bbf5ea8SMatthew G. Knepley  * + dm - The DMPlex object defining the mesh
7884bbf5ea8SMatthew G. Knepley  *
7894bbf5ea8SMatthew G. Knepley  * Output Parameters:
7904bbf5ea8SMatthew G. Knepley  * + cellCounts  - Section with counts of cells around each vertex
7915f824522SMatthew G. Knepley  * . cells       - IS of the cell point indices of cells in each patch
7925f824522SMatthew G. Knepley  * . pointCounts - Section with counts of cells around each vertex
7935f824522SMatthew G. Knepley  * - point       - IS of the cell point indices of cells in each patch
7944bbf5ea8SMatthew G. Knepley  */
7954bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatches(PC pc)
7964bbf5ea8SMatthew G. Knepley {
7974bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
7985f824522SMatthew G. Knepley   DMLabel         ghost = NULL;
7994bbf5ea8SMatthew G. Knepley   DM              dm, plex;
8001b68eb51SMatthew G. Knepley   PetscHSetI      ht, cht;
8015f824522SMatthew G. Knepley   PetscSection    cellCounts,  pointCounts;
8025f824522SMatthew G. Knepley   PetscInt       *cellsArray, *pointsArray;
8035f824522SMatthew G. Knepley   PetscInt        numCells,    numPoints;
8045f824522SMatthew G. Knepley   const PetscInt *leaves;
8055f824522SMatthew G. Knepley   PetscInt        nleaves, pStart, pEnd, cStart, cEnd, vStart, vEnd, v;
8065f824522SMatthew G. Knepley   PetscBool       isFiredrake;
8074bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
8084bbf5ea8SMatthew G. Knepley 
8094bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
8104bbf5ea8SMatthew G. Knepley   /* Used to keep track of the cells in the patch. */
8111b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&ht);CHKERRQ(ierr);
8121b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&cht);CHKERRQ(ierr);
8134bbf5ea8SMatthew G. Knepley 
8144bbf5ea8SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
8154bbf5ea8SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONGSTATE, "DM not yet set on patch PC\n");
8164bbf5ea8SMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
8174bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetChart(plex, &pStart, &pEnd);CHKERRQ(ierr);
8184bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr);
8194bbf5ea8SMatthew G. Knepley 
8204bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
8215f824522SMatthew G. Knepley     ierr = patch->userpatchconstructionop(pc, &patch->npatch, &patch->userIS, &patch->iterationSet, patch->userpatchconstructctx);CHKERRQ(ierr);
8225f824522SMatthew G. Knepley     vStart = 0; vEnd = patch->npatch;
823e5b9877fSPatrick Farrell   } else if (patch->ctype == PC_PATCH_PARDECOMP) {
8240a390943SPatrick Farrell     vStart = 0; vEnd = 1;
8255f824522SMatthew G. Knepley   } else if (patch->codim < 0) {
8265f824522SMatthew G. Knepley     if (patch->dim < 0) {ierr = DMPlexGetDepthStratum(plex,  0,            &vStart, &vEnd);CHKERRQ(ierr);}
8275f824522SMatthew G. Knepley     else                {ierr = DMPlexGetDepthStratum(plex,  patch->dim,   &vStart, &vEnd);CHKERRQ(ierr);}
8285f824522SMatthew G. Knepley   } else                {ierr = DMPlexGetHeightStratum(plex, patch->codim, &vStart, &vEnd);CHKERRQ(ierr);}
8295f824522SMatthew G. Knepley   patch->npatch = vEnd - vStart;
8304bbf5ea8SMatthew G. Knepley 
8314bbf5ea8SMatthew G. Knepley   /* These labels mark the owned points.  We only create patches around points that this process owns. */
8325f824522SMatthew G. Knepley   ierr = DMHasLabel(dm, "pyop2_ghost", &isFiredrake);CHKERRQ(ierr);
8335f824522SMatthew G. Knepley   if (isFiredrake) {
8344bbf5ea8SMatthew G. Knepley     ierr = DMGetLabel(dm, "pyop2_ghost", &ghost);CHKERRQ(ierr);
8354bbf5ea8SMatthew G. Knepley     ierr = DMLabelCreateIndex(ghost, pStart, pEnd);CHKERRQ(ierr);
8365f824522SMatthew G. Knepley   } else {
8375f824522SMatthew G. Knepley     PetscSF sf;
8385f824522SMatthew G. Knepley 
8395f824522SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
8405f824522SMatthew G. Knepley     ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
8415f824522SMatthew G. Knepley     nleaves = PetscMax(nleaves, 0);
8425f824522SMatthew G. Knepley   }
8434bbf5ea8SMatthew G. Knepley 
8444bbf5ea8SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->cellCounts);CHKERRQ(ierr);
8455f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->cellCounts, "Patch Cell Layout");CHKERRQ(ierr);
8464bbf5ea8SMatthew G. Knepley   cellCounts = patch->cellCounts;
8474bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetChart(cellCounts, vStart, vEnd);CHKERRQ(ierr);
8485f824522SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->pointCounts);CHKERRQ(ierr);
8495f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->pointCounts, "Patch Point Layout");CHKERRQ(ierr);
8505f824522SMatthew G. Knepley   pointCounts = patch->pointCounts;
8515f824522SMatthew G. Knepley   ierr = PetscSectionSetChart(pointCounts, vStart, vEnd);CHKERRQ(ierr);
8525f824522SMatthew G. Knepley   /* Count cells and points in the patch surrounding each entity */
8534bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
8541b68eb51SMatthew G. Knepley     PetscHashIter hi;
8555f824522SMatthew G. Knepley     PetscInt       chtSize, loc = -1;
8565f824522SMatthew G. Knepley     PetscBool      flg;
8574bbf5ea8SMatthew G. Knepley 
858b525f888SPatrick Farrell     if (!patch->user_patches && patch->ctype != PC_PATCH_PARDECOMP) {
8595f824522SMatthew G. Knepley       if (ghost) {ierr = DMLabelHasPoint(ghost, v, &flg);CHKERRQ(ierr);}
860928bb9adSStefano Zampini       else       {ierr = PetscFindInt(v, nleaves, leaves, &loc);CHKERRQ(ierr); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;}
8614bbf5ea8SMatthew G. Knepley       /* Not an owned entity, don't make a cell patch. */
8624bbf5ea8SMatthew G. Knepley       if (flg) continue;
8634bbf5ea8SMatthew G. Knepley     }
8644bbf5ea8SMatthew G. Knepley 
8654bbf5ea8SMatthew G. Knepley     ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr);
8665f824522SMatthew G. Knepley     ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr);
8671b68eb51SMatthew G. Knepley     ierr = PetscHSetIGetSize(cht, &chtSize);CHKERRQ(ierr);
8684bbf5ea8SMatthew G. Knepley     /* empty patch, continue */
8694bbf5ea8SMatthew G. Knepley     if (chtSize == 0) continue;
8704bbf5ea8SMatthew G. Knepley 
8714bbf5ea8SMatthew G. Knepley     /* safe because size(cht) > 0 from above */
8721b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
8731b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
8745f824522SMatthew G. Knepley       PetscInt point, pdof;
8754bbf5ea8SMatthew G. Knepley 
8761b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
8775f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr);
8785f824522SMatthew G. Knepley       if (pdof)                            {ierr = PetscSectionAddDof(pointCounts, v, 1);CHKERRQ(ierr);}
8795f824522SMatthew G. Knepley       if (point >= cStart && point < cEnd) {ierr = PetscSectionAddDof(cellCounts, v, 1);CHKERRQ(ierr);}
8801b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
8814bbf5ea8SMatthew G. Knepley     }
8824bbf5ea8SMatthew G. Knepley   }
8835f824522SMatthew G. Knepley   if (isFiredrake) {ierr = DMLabelDestroyIndex(ghost);CHKERRQ(ierr);}
8844bbf5ea8SMatthew G. Knepley 
8854bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetUp(cellCounts);CHKERRQ(ierr);
8864bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr);
8874bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numCells, &cellsArray);CHKERRQ(ierr);
8885f824522SMatthew G. Knepley   ierr = PetscSectionSetUp(pointCounts);CHKERRQ(ierr);
8895f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(pointCounts, &numPoints);CHKERRQ(ierr);
8905f824522SMatthew G. Knepley   ierr = PetscMalloc1(numPoints, &pointsArray);CHKERRQ(ierr);
8914bbf5ea8SMatthew G. Knepley 
8924bbf5ea8SMatthew G. Knepley   /* Now that we know how much space we need, run through again and actually remember the cells. */
8934bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; v++ ) {
8941b68eb51SMatthew G. Knepley     PetscHashIter hi;
8955f824522SMatthew G. Knepley     PetscInt       dof, off, cdof, coff, pdof, n = 0, cn = 0;
8964bbf5ea8SMatthew G. Knepley 
8975f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(pointCounts, v, &dof);CHKERRQ(ierr);
8985f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(pointCounts, v, &off);CHKERRQ(ierr);
8995f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &cdof);CHKERRQ(ierr);
9005f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &coff);CHKERRQ(ierr);
9015f824522SMatthew G. Knepley     if (dof <= 0) continue;
9024bbf5ea8SMatthew G. Knepley     ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr);
9035f824522SMatthew G. Knepley     ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr);
9041b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
9051b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
9064bbf5ea8SMatthew G. Knepley       PetscInt point;
9074bbf5ea8SMatthew G. Knepley 
9081b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
9095f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr);
9105f824522SMatthew G. Knepley       if (pdof)                            {pointsArray[off + n++] = point;}
9115f824522SMatthew G. Knepley       if (point >= cStart && point < cEnd) {cellsArray[coff + cn++] = point;}
9121b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
9134bbf5ea8SMatthew G. Knepley     }
9145f824522SMatthew 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);
9155f824522SMatthew 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);
9164bbf5ea8SMatthew G. Knepley   }
9171b68eb51SMatthew G. Knepley   ierr = PetscHSetIDestroy(&ht);CHKERRQ(ierr);
9181b68eb51SMatthew G. Knepley   ierr = PetscHSetIDestroy(&cht);CHKERRQ(ierr);
9194bbf5ea8SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
9205f824522SMatthew G. Knepley 
9215f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numCells,  cellsArray,  PETSC_OWN_POINTER, &patch->cells);CHKERRQ(ierr);
9225f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->cells,  "Patch Cells");CHKERRQ(ierr);
9235f824522SMatthew G. Knepley   if (patch->viewCells) {
9245f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->cellCounts, patch->viewerCells, patch->formatCells);CHKERRQ(ierr);
9255f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->cells,      patch->viewerCells, patch->formatCells);CHKERRQ(ierr);
9265f824522SMatthew G. Knepley   }
9275f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints, pointsArray, PETSC_OWN_POINTER, &patch->points);CHKERRQ(ierr);
9285f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->points, "Patch Points");CHKERRQ(ierr);
9295f824522SMatthew G. Knepley   if (patch->viewPoints) {
9305f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->pointCounts, patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr);
9315f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->points,      patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr);
9325f824522SMatthew G. Knepley   }
9334bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
9344bbf5ea8SMatthew G. Knepley }
9354bbf5ea8SMatthew G. Knepley 
9364bbf5ea8SMatthew G. Knepley /*
9374bbf5ea8SMatthew G. Knepley  * PCPatchCreateCellPatchDiscretisationInfo - Build the dof maps for cell patches
9384bbf5ea8SMatthew G. Knepley  *
9394bbf5ea8SMatthew G. Knepley  * Input Parameters:
9404bbf5ea8SMatthew G. Knepley  * + dm - The DMPlex object defining the mesh
9414bbf5ea8SMatthew G. Knepley  * . cellCounts - Section with counts of cells around each vertex
9424bbf5ea8SMatthew G. Knepley  * . cells - IS of the cell point indices of cells in each patch
9434bbf5ea8SMatthew G. Knepley  * . cellNumbering - Section mapping plex cell points to Firedrake cell indices.
9444bbf5ea8SMatthew G. Knepley  * . nodesPerCell - number of nodes per cell.
9454bbf5ea8SMatthew G. Knepley  * - cellNodeMap - map from cells to node indices (nodesPerCell * numCells)
9464bbf5ea8SMatthew G. Knepley  *
9474bbf5ea8SMatthew G. Knepley  * Output Parameters:
9485f824522SMatthew G. Knepley  * + dofs - IS of local dof numbers of each cell in the patch, where local is a patch local numbering
9494bbf5ea8SMatthew G. Knepley  * . gtolCounts - Section with counts of dofs per cell patch
9504bbf5ea8SMatthew G. Knepley  * - gtol - IS mapping from global dofs to local dofs for each patch.
9514bbf5ea8SMatthew G. Knepley  */
9524bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatchDiscretisationInfo(PC pc)
9534bbf5ea8SMatthew G. Knepley {
9544bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch           = (PC_PATCH *) pc->data;
9554bbf5ea8SMatthew G. Knepley   PetscSection    cellCounts      = patch->cellCounts;
9565f824522SMatthew G. Knepley   PetscSection    pointCounts     = patch->pointCounts;
9570904074fSPatrick Farrell   PetscSection    gtolCounts, gtolCountsWithArtificial = NULL, gtolCountsWithAll = NULL;
9584bbf5ea8SMatthew G. Knepley   IS              cells           = patch->cells;
9595f824522SMatthew G. Knepley   IS              points          = patch->points;
9604bbf5ea8SMatthew G. Knepley   PetscSection    cellNumbering   = patch->cellNumbering;
9615f824522SMatthew G. Knepley   PetscInt        Nf              = patch->nsubspaces;
9625f824522SMatthew G. Knepley   PetscInt        numCells, numPoints;
9634bbf5ea8SMatthew G. Knepley   PetscInt        numDofs;
9640904074fSPatrick Farrell   PetscInt        numGlobalDofs, numGlobalDofsWithArtificial, numGlobalDofsWithAll;
9654bbf5ea8SMatthew G. Knepley   PetscInt        totalDofsPerCell = patch->totalDofsPerCell;
9664bbf5ea8SMatthew G. Knepley   PetscInt        vStart, vEnd, v;
9675f824522SMatthew G. Knepley   const PetscInt *cellsArray, *pointsArray;
9684bbf5ea8SMatthew G. Knepley   PetscInt       *newCellsArray   = NULL;
9694bbf5ea8SMatthew G. Knepley   PetscInt       *dofsArray       = NULL;
970c2e6f3c0SFlorian Wechsung   PetscInt       *dofsArrayWithArtificial = NULL;
9710904074fSPatrick Farrell   PetscInt       *dofsArrayWithAll = NULL;
9725f824522SMatthew G. Knepley   PetscInt       *offsArray       = NULL;
973c2e6f3c0SFlorian Wechsung   PetscInt       *offsArrayWithArtificial = NULL;
9740904074fSPatrick Farrell   PetscInt       *offsArrayWithAll = NULL;
9754bbf5ea8SMatthew G. Knepley   PetscInt       *asmArray        = NULL;
976c2e6f3c0SFlorian Wechsung   PetscInt       *asmArrayWithArtificial = NULL;
9770904074fSPatrick Farrell   PetscInt       *asmArrayWithAll = NULL;
9784bbf5ea8SMatthew G. Knepley   PetscInt       *globalDofsArray = NULL;
979c2e6f3c0SFlorian Wechsung   PetscInt       *globalDofsArrayWithArtificial = NULL;
9800904074fSPatrick Farrell   PetscInt       *globalDofsArrayWithAll = NULL;
9814bbf5ea8SMatthew G. Knepley   PetscInt        globalIndex     = 0;
9824bbf5ea8SMatthew G. Knepley   PetscInt        key             = 0;
9834bbf5ea8SMatthew G. Knepley   PetscInt        asmKey          = 0;
984557beb66SLawrence Mitchell   DM              dm              = NULL;
985557beb66SLawrence Mitchell   const PetscInt *bcNodes         = NULL;
9861b68eb51SMatthew G. Knepley   PetscHMapI      ht;
987c2e6f3c0SFlorian Wechsung   PetscHMapI      htWithArtificial;
9880904074fSPatrick Farrell   PetscHMapI      htWithAll;
9891b68eb51SMatthew G. Knepley   PetscHSetI      globalBcs;
990557beb66SLawrence Mitchell   PetscInt        numBcs;
9911b68eb51SMatthew G. Knepley   PetscHSetI      ownedpts, seenpts, owneddofs, seendofs, artificialbcs;
992cda239d9SMatthew G. Knepley   PetscInt        pStart, pEnd, p, i;
99310534d48SPatrick Farrell   char            option[PETSC_MAX_PATH_LEN];
99439fd2e8aSPatrick Farrell   PetscBool       isNonlinear;
9954bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
9964bbf5ea8SMatthew G. Knepley 
9974bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
998557beb66SLawrence Mitchell 
999557beb66SLawrence Mitchell   ierr = PCGetDM(pc, &dm); CHKERRQ(ierr);
10004bbf5ea8SMatthew G. Knepley   /* dofcounts section is cellcounts section * dofPerCell */
10014bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr);
10025f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(patch->pointCounts, &numPoints);CHKERRQ(ierr);
10034bbf5ea8SMatthew G. Knepley   numDofs = numCells * totalDofsPerCell;
10044bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numDofs, &dofsArray);CHKERRQ(ierr);
10055f824522SMatthew G. Knepley   ierr = PetscMalloc1(numPoints*Nf, &offsArray);CHKERRQ(ierr);
10064bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numDofs, &asmArray);CHKERRQ(ierr);
10074bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numCells, &newCellsArray);CHKERRQ(ierr);
10084bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(cellCounts, &vStart, &vEnd);CHKERRQ(ierr);
10094bbf5ea8SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCounts);CHKERRQ(ierr);
10104bbf5ea8SMatthew G. Knepley   gtolCounts = patch->gtolCounts;
10114bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetChart(gtolCounts, vStart, vEnd);CHKERRQ(ierr);
10125f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->gtolCounts, "Patch Global Index Section");CHKERRQ(ierr);
10134bbf5ea8SMatthew G. Knepley 
10140904074fSPatrick Farrell   if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE)
1015c2e6f3c0SFlorian Wechsung   {
1016f0dcb611SKarl Rupp     ierr = PetscMalloc1(numPoints*Nf, &offsArrayWithArtificial);CHKERRQ(ierr);
1017c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numDofs, &asmArrayWithArtificial);CHKERRQ(ierr);
1018c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numDofs, &dofsArrayWithArtificial);CHKERRQ(ierr);
1019c2e6f3c0SFlorian Wechsung     ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithArtificial);CHKERRQ(ierr);
1020c2e6f3c0SFlorian Wechsung     gtolCountsWithArtificial = patch->gtolCountsWithArtificial;
1021c2e6f3c0SFlorian Wechsung     ierr = PetscSectionSetChart(gtolCountsWithArtificial, vStart, vEnd);CHKERRQ(ierr);
1022c2e6f3c0SFlorian Wechsung     ierr = PetscObjectSetName((PetscObject) patch->gtolCountsWithArtificial, "Patch Global Index Section Including Artificial BCs");CHKERRQ(ierr);
1023c2e6f3c0SFlorian Wechsung   }
1024c2e6f3c0SFlorian Wechsung 
10250904074fSPatrick Farrell   isNonlinear = patch->isNonlinear;
10260904074fSPatrick Farrell   if(isNonlinear)
10270904074fSPatrick Farrell   {
10280904074fSPatrick Farrell     ierr = PetscMalloc1(numPoints*Nf, &offsArrayWithAll);CHKERRQ(ierr);
10290904074fSPatrick Farrell     ierr = PetscMalloc1(numDofs, &asmArrayWithAll);CHKERRQ(ierr);
10300904074fSPatrick Farrell     ierr = PetscMalloc1(numDofs, &dofsArrayWithAll);CHKERRQ(ierr);
10310904074fSPatrick Farrell     ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithAll);CHKERRQ(ierr);
10320904074fSPatrick Farrell     gtolCountsWithAll = patch->gtolCountsWithAll;
10330904074fSPatrick Farrell     ierr = PetscSectionSetChart(gtolCountsWithAll, vStart, vEnd);CHKERRQ(ierr);
10340904074fSPatrick Farrell     ierr = PetscObjectSetName((PetscObject) patch->gtolCountsWithAll, "Patch Global Index Section Including All BCs");CHKERRQ(ierr);
10350904074fSPatrick Farrell   }
10360904074fSPatrick Farrell 
1037557beb66SLawrence Mitchell   /* Outside the patch loop, get the dofs that are globally-enforced Dirichlet
1038557beb66SLawrence Mitchell    conditions */
10391b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&globalBcs);CHKERRQ(ierr);
1040557beb66SLawrence Mitchell   ierr = ISGetIndices(patch->ghostBcNodes, &bcNodes); CHKERRQ(ierr);
1041557beb66SLawrence Mitchell   ierr = ISGetSize(patch->ghostBcNodes, &numBcs); CHKERRQ(ierr);
1042cda239d9SMatthew G. Knepley   for (i = 0; i < numBcs; ++i) {
10431b68eb51SMatthew G. Knepley     ierr = PetscHSetIAdd(globalBcs, bcNodes[i]);CHKERRQ(ierr); /* these are already in concatenated numbering */
1044557beb66SLawrence Mitchell   }
1045557beb66SLawrence Mitchell   ierr = ISRestoreIndices(patch->ghostBcNodes, &bcNodes); CHKERRQ(ierr);
1046557beb66SLawrence Mitchell   ierr = ISDestroy(&patch->ghostBcNodes); CHKERRQ(ierr); /* memory optimisation */
1047557beb66SLawrence Mitchell 
1048557beb66SLawrence Mitchell   /* Hash tables for artificial BC construction */
10491b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&ownedpts);CHKERRQ(ierr);
10501b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&seenpts);CHKERRQ(ierr);
10511b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&owneddofs);CHKERRQ(ierr);
10521b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&seendofs);CHKERRQ(ierr);
10531b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&artificialbcs);CHKERRQ(ierr);
1054557beb66SLawrence Mitchell 
10554bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(cells, &cellsArray);CHKERRQ(ierr);
10565f824522SMatthew G. Knepley   ierr = ISGetIndices(points, &pointsArray);CHKERRQ(ierr);
10571b68eb51SMatthew G. Knepley   ierr = PetscHMapICreate(&ht);CHKERRQ(ierr);
1058c2e6f3c0SFlorian Wechsung   ierr = PetscHMapICreate(&htWithArtificial);CHKERRQ(ierr);
10590904074fSPatrick Farrell   ierr = PetscHMapICreate(&htWithAll);CHKERRQ(ierr);
10604bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
10614bbf5ea8SMatthew G. Knepley     PetscInt localIndex = 0;
1062c2e6f3c0SFlorian Wechsung     PetscInt localIndexWithArtificial = 0;
10630904074fSPatrick Farrell     PetscInt localIndexWithAll = 0;
10644bbf5ea8SMatthew G. Knepley     PetscInt dof, off, i, j, k, l;
10654bbf5ea8SMatthew G. Knepley 
10661b68eb51SMatthew G. Knepley     ierr = PetscHMapIClear(ht);CHKERRQ(ierr);
1067c2e6f3c0SFlorian Wechsung     ierr = PetscHMapIClear(htWithArtificial);CHKERRQ(ierr);
10680904074fSPatrick Farrell     ierr = PetscHMapIClear(htWithAll);CHKERRQ(ierr);
10694bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr);
10704bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr);
10714bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
10724bbf5ea8SMatthew G. Knepley 
1073557beb66SLawrence Mitchell     /* Calculate the global numbers of the artificial BC dofs here first */
1074557beb66SLawrence Mitchell     ierr = patch->patchconstructop((void*)patch, dm, v, ownedpts); CHKERRQ(ierr);
1075557beb66SLawrence Mitchell     ierr = PCPatchCompleteCellPatch(pc, ownedpts, seenpts); CHKERRQ(ierr);
1076e4c66b91SPatrick Farrell     ierr = PCPatchGetPointDofs(pc, ownedpts, owneddofs, v, &patch->subspaces_to_exclude); CHKERRQ(ierr);
1077e4c66b91SPatrick Farrell     ierr = PCPatchGetPointDofs(pc, seenpts, seendofs, v, NULL); CHKERRQ(ierr);
1078557beb66SLawrence Mitchell     ierr = PCPatchComputeSetDifference_Private(owneddofs, seendofs, artificialbcs); CHKERRQ(ierr);
10798135ed82SLawrence Mitchell     if (patch->viewPatches) {
10801b68eb51SMatthew G. Knepley       PetscHSetI globalbcdofs;
10811b68eb51SMatthew G. Knepley       PetscHashIter hi;
10828135ed82SLawrence Mitchell       MPI_Comm comm = PetscObjectComm((PetscObject)pc);
10831b68eb51SMatthew G. Knepley 
10841b68eb51SMatthew G. Knepley       ierr = PetscHSetICreate(&globalbcdofs);CHKERRQ(ierr);
10858135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: owned dofs:\n", v); CHKERRQ(ierr);
10861b68eb51SMatthew G. Knepley       PetscHashIterBegin(owneddofs, hi);
10871b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(owneddofs, hi)) {
10888135ed82SLawrence Mitchell         PetscInt globalDof;
10898135ed82SLawrence Mitchell 
10901b68eb51SMatthew G. Knepley         PetscHashIterGetKey(owneddofs, hi, globalDof);
10911b68eb51SMatthew G. Knepley         PetscHashIterNext(owneddofs, hi);
10928135ed82SLawrence Mitchell         ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
10938135ed82SLawrence Mitchell       }
10948135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n"); CHKERRQ(ierr);
10958135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: seen dofs:\n", v); CHKERRQ(ierr);
10961b68eb51SMatthew G. Knepley       PetscHashIterBegin(seendofs, hi);
10971b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(seendofs, hi)) {
10988135ed82SLawrence Mitchell         PetscInt globalDof;
10998135ed82SLawrence Mitchell         PetscBool flg;
11008135ed82SLawrence Mitchell 
11011b68eb51SMatthew G. Knepley         PetscHashIterGetKey(seendofs, hi, globalDof);
11021b68eb51SMatthew G. Knepley         PetscHashIterNext(seendofs, hi);
11038135ed82SLawrence Mitchell         ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
11048135ed82SLawrence Mitchell 
11051b68eb51SMatthew G. Knepley         ierr = PetscHSetIHas(globalBcs, globalDof, &flg);CHKERRQ(ierr);
11061b68eb51SMatthew G. Knepley         if (flg) {ierr = PetscHSetIAdd(globalbcdofs, globalDof);CHKERRQ(ierr);}
11078135ed82SLawrence Mitchell       }
11088135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n"); CHKERRQ(ierr);
11098135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: global BCs:\n", v); CHKERRQ(ierr);
11101b68eb51SMatthew G. Knepley       ierr = PetscHSetIGetSize(globalbcdofs, &numBcs);CHKERRQ(ierr);
11118135ed82SLawrence Mitchell       if (numBcs > 0) {
11121b68eb51SMatthew G. Knepley         PetscHashIterBegin(globalbcdofs, hi);
11131b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(globalbcdofs, hi)) {
11148135ed82SLawrence Mitchell           PetscInt globalDof;
11151b68eb51SMatthew G. Knepley           PetscHashIterGetKey(globalbcdofs, hi, globalDof);
11161b68eb51SMatthew G. Knepley           PetscHashIterNext(globalbcdofs, hi);
11178135ed82SLawrence Mitchell           ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof);CHKERRQ(ierr);
11188135ed82SLawrence Mitchell         }
11198135ed82SLawrence Mitchell       }
11208135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n");CHKERRQ(ierr);
11218135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: artificial BCs:\n", v);CHKERRQ(ierr);
11221b68eb51SMatthew G. Knepley       ierr = PetscHSetIGetSize(artificialbcs, &numBcs);CHKERRQ(ierr);
11238135ed82SLawrence Mitchell       if (numBcs > 0) {
11241b68eb51SMatthew G. Knepley         PetscHashIterBegin(artificialbcs, hi);
11251b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(artificialbcs, hi)) {
11268135ed82SLawrence Mitchell           PetscInt globalDof;
11271b68eb51SMatthew G. Knepley           PetscHashIterGetKey(artificialbcs, hi, globalDof);
11281b68eb51SMatthew G. Knepley           PetscHashIterNext(artificialbcs, hi);
11298135ed82SLawrence Mitchell           ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
11308135ed82SLawrence Mitchell         }
11318135ed82SLawrence Mitchell       }
11328135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n\n"); CHKERRQ(ierr);
11331b68eb51SMatthew G. Knepley       ierr = PetscHSetIDestroy(&globalbcdofs);CHKERRQ(ierr);
11348135ed82SLawrence Mitchell     }
11354bbf5ea8SMatthew G. Knepley    for (k = 0; k < patch->nsubspaces; ++k) {
11364bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
11374bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
11384bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
11394bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
11404bbf5ea8SMatthew G. Knepley 
11414bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
11424bbf5ea8SMatthew G. Knepley         /* Walk over the cells in this patch. */
11434bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
11445f824522SMatthew G. Knepley         PetscInt       cell = c;
11454bbf5ea8SMatthew G. Knepley 
11465f824522SMatthew G. Knepley         /* TODO Change this to an IS */
11475f824522SMatthew G. Knepley         if (cellNumbering) {
11484bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetDof(cellNumbering, c, &cell);CHKERRQ(ierr);
11494bbf5ea8SMatthew G. Knepley           if (cell <= 0) SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_OUTOFRANGE, "Cell %D doesn't appear in cell numbering map", c);
11504bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);
11515f824522SMatthew G. Knepley         }
11524bbf5ea8SMatthew G. Knepley         newCellsArray[i] = cell;
11534bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
11544bbf5ea8SMatthew G. Knepley           /* For each global dof, map it into contiguous local storage. */
11554bbf5ea8SMatthew G. Knepley           const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + subspaceOffset;
11564bbf5ea8SMatthew G. Knepley           /* finally, loop over block size */
11574bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
11581b68eb51SMatthew G. Knepley             PetscInt  localDof;
11591b68eb51SMatthew G. Knepley             PetscBool isGlobalBcDof, isArtificialBcDof;
11604bbf5ea8SMatthew G. Knepley 
1161557beb66SLawrence Mitchell             /* first, check if this is either a globally enforced or locally enforced BC dof */
11621b68eb51SMatthew G. Knepley             ierr = PetscHSetIHas(globalBcs, globalDof + l, &isGlobalBcDof);CHKERRQ(ierr);
11631b68eb51SMatthew G. Knepley             ierr = PetscHSetIHas(artificialbcs, globalDof + l, &isArtificialBcDof);CHKERRQ(ierr);
1164557beb66SLawrence Mitchell 
1165557beb66SLawrence Mitchell             /* if it's either, don't ever give it a local dof number */
11661b68eb51SMatthew G. Knepley             if (isGlobalBcDof || isArtificialBcDof) {
1167c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = -1; /* don't use this in assembly in this patch */
1168557beb66SLawrence Mitchell             } else {
11691b68eb51SMatthew G. Knepley               ierr = PetscHMapIGet(ht, globalDof + l, &localDof);CHKERRQ(ierr);
11704bbf5ea8SMatthew G. Knepley               if (localDof == -1) {
11714bbf5ea8SMatthew G. Knepley                 localDof = localIndex++;
11721b68eb51SMatthew G. Knepley                 ierr = PetscHMapISet(ht, globalDof + l, localDof);CHKERRQ(ierr);
11734bbf5ea8SMatthew G. Knepley               }
11744bbf5ea8SMatthew G. Knepley               if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
11754bbf5ea8SMatthew G. Knepley               /* And store. */
1176c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = localDof;
11774bbf5ea8SMatthew G. Knepley             }
1178c2e6f3c0SFlorian Wechsung 
11790904074fSPatrick Farrell             if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1180c2e6f3c0SFlorian Wechsung               if (isGlobalBcDof) {
1181e047a90bSFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = -1; /* don't use this in assembly in this patch */
1182c2e6f3c0SFlorian Wechsung               } else {
1183c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapIGet(htWithArtificial, globalDof + l, &localDof);CHKERRQ(ierr);
1184c2e6f3c0SFlorian Wechsung                 if (localDof == -1) {
1185c2e6f3c0SFlorian Wechsung                   localDof = localIndexWithArtificial++;
1186c2e6f3c0SFlorian Wechsung                   ierr = PetscHMapISet(htWithArtificial, globalDof + l, localDof);CHKERRQ(ierr);
1187c2e6f3c0SFlorian Wechsung                 }
1188c2e6f3c0SFlorian Wechsung                 if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
1189c2e6f3c0SFlorian Wechsung                 /* And store.*/
1190c2e6f3c0SFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = localDof;
1191c2e6f3c0SFlorian Wechsung               }
1192c2e6f3c0SFlorian Wechsung             }
11930904074fSPatrick Farrell 
11940904074fSPatrick Farrell             if(isNonlinear) {
11950904074fSPatrick Farrell               /* Build the dofmap for the function space with _all_ dofs,
11960904074fSPatrick Farrell                  including those in any kind of boundary condition */
11970904074fSPatrick Farrell               ierr = PetscHMapIGet(htWithAll, globalDof + l, &localDof);CHKERRQ(ierr);
11980904074fSPatrick Farrell               if (localDof == -1) {
11990904074fSPatrick Farrell                 localDof = localIndexWithAll++;
12000904074fSPatrick Farrell                 ierr = PetscHMapISet(htWithAll, globalDof + l, localDof);CHKERRQ(ierr);
12010904074fSPatrick Farrell               }
12020904074fSPatrick Farrell               if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
12030904074fSPatrick Farrell               /* And store.*/
12040904074fSPatrick Farrell               dofsArrayWithAll[globalIndex] = localDof;
12050904074fSPatrick Farrell             }
1206c2e6f3c0SFlorian Wechsung             globalIndex++;
12074bbf5ea8SMatthew G. Knepley           }
12084bbf5ea8SMatthew G. Knepley         }
12094bbf5ea8SMatthew G. Knepley       }
1210557beb66SLawrence Mitchell     }
12114bbf5ea8SMatthew G. Knepley      /*How many local dofs in this patch? */
12120904074fSPatrick Farrell    if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1213c2e6f3c0SFlorian Wechsung      ierr = PetscHMapIGetSize(htWithArtificial, &dof);CHKERRQ(ierr);
1214c2e6f3c0SFlorian Wechsung      ierr = PetscSectionSetDof(gtolCountsWithArtificial, v, dof);CHKERRQ(ierr);
1215c2e6f3c0SFlorian Wechsung    }
12160904074fSPatrick Farrell    if (isNonlinear) {
12170904074fSPatrick Farrell      ierr = PetscHMapIGetSize(htWithAll, &dof);CHKERRQ(ierr);
12180904074fSPatrick Farrell      ierr = PetscSectionSetDof(gtolCountsWithAll, v, dof);CHKERRQ(ierr);
12190904074fSPatrick Farrell    }
12201b68eb51SMatthew G. Knepley     ierr = PetscHMapIGetSize(ht, &dof);CHKERRQ(ierr);
12214bbf5ea8SMatthew G. Knepley     ierr = PetscSectionSetDof(gtolCounts, v, dof);CHKERRQ(ierr);
12224bbf5ea8SMatthew G. Knepley   }
12234bbf5ea8SMatthew 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);
12244bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetUp(gtolCounts);CHKERRQ(ierr);
12254bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(gtolCounts, &numGlobalDofs);CHKERRQ(ierr);
12264bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numGlobalDofs, &globalDofsArray);CHKERRQ(ierr);
12274bbf5ea8SMatthew G. Knepley 
12280904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1229c2e6f3c0SFlorian Wechsung     ierr = PetscSectionSetUp(gtolCountsWithArtificial);CHKERRQ(ierr);
1230c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetStorageSize(gtolCountsWithArtificial, &numGlobalDofsWithArtificial);CHKERRQ(ierr);
1231c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numGlobalDofsWithArtificial, &globalDofsArrayWithArtificial);CHKERRQ(ierr);
1232c2e6f3c0SFlorian Wechsung   }
12330904074fSPatrick Farrell   if (isNonlinear) {
12340904074fSPatrick Farrell     ierr = PetscSectionSetUp(gtolCountsWithAll);CHKERRQ(ierr);
12350904074fSPatrick Farrell     ierr = PetscSectionGetStorageSize(gtolCountsWithAll, &numGlobalDofsWithAll);CHKERRQ(ierr);
12360904074fSPatrick Farrell     ierr = PetscMalloc1(numGlobalDofsWithAll, &globalDofsArrayWithAll);CHKERRQ(ierr);
12370904074fSPatrick Farrell   }
12384bbf5ea8SMatthew 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. */
12394bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
12401b68eb51SMatthew G. Knepley     PetscHashIter hi;
12415f824522SMatthew G. Knepley     PetscInt      dof, off, Np, ooff, i, j, k, l;
12424bbf5ea8SMatthew G. Knepley 
12431b68eb51SMatthew G. Knepley     ierr = PetscHMapIClear(ht);CHKERRQ(ierr);
1244c2e6f3c0SFlorian Wechsung     ierr = PetscHMapIClear(htWithArtificial);CHKERRQ(ierr);
12450904074fSPatrick Farrell     ierr = PetscHMapIClear(htWithAll);CHKERRQ(ierr);
12464bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr);
12474bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr);
12485f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(pointCounts, v, &Np);CHKERRQ(ierr);
12495f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(pointCounts, v, &ooff);CHKERRQ(ierr);
12504bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
12514bbf5ea8SMatthew G. Knepley 
12524bbf5ea8SMatthew G. Knepley     for (k = 0; k < patch->nsubspaces; ++k) {
12534bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
12544bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
12554bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
12564bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
1257d490bb3dSLawrence Mitchell       PetscInt        goff;
12584bbf5ea8SMatthew G. Knepley 
12594bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
12604bbf5ea8SMatthew G. Knepley         /* Reconstruct mapping of global-to-local on this patch. */
12614bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
12625f824522SMatthew G. Knepley         PetscInt       cell = c;
12634bbf5ea8SMatthew G. Knepley 
12645f824522SMatthew G. Knepley         if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);}
12654bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
12664bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
12675f824522SMatthew G. Knepley             const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset;
1268c2e6f3c0SFlorian Wechsung             const PetscInt localDof  = dofsArray[key];
12691b68eb51SMatthew G. Knepley             if (localDof >= 0) {ierr = PetscHMapISet(ht, globalDof, localDof);CHKERRQ(ierr);}
12700904074fSPatrick Farrell             if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1271c2e6f3c0SFlorian Wechsung               const PetscInt localDofWithArtificial = dofsArrayWithArtificial[key];
1272c2e6f3c0SFlorian Wechsung               if (localDofWithArtificial >= 0) {
1273c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapISet(htWithArtificial, globalDof, localDofWithArtificial);CHKERRQ(ierr);
1274c2e6f3c0SFlorian Wechsung               }
1275c2e6f3c0SFlorian Wechsung             }
12760904074fSPatrick Farrell             if (isNonlinear) {
12770904074fSPatrick Farrell               const PetscInt localDofWithAll = dofsArrayWithAll[key];
12780904074fSPatrick Farrell               if (localDofWithAll >= 0) {
12790904074fSPatrick Farrell                 ierr = PetscHMapISet(htWithAll, globalDof, localDofWithAll);CHKERRQ(ierr);
12800904074fSPatrick Farrell               }
12810904074fSPatrick Farrell             }
1282c2e6f3c0SFlorian Wechsung             key++;
12834bbf5ea8SMatthew G. Knepley           }
12844bbf5ea8SMatthew G. Knepley         }
12854bbf5ea8SMatthew G. Knepley       }
1286557beb66SLawrence Mitchell 
12874bbf5ea8SMatthew G. Knepley       /* Shove it in the output data structure. */
12884bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetOffset(gtolCounts, v, &goff);CHKERRQ(ierr);
12891b68eb51SMatthew G. Knepley       PetscHashIterBegin(ht, hi);
12901b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(ht, hi)) {
12914bbf5ea8SMatthew G. Knepley         PetscInt globalDof, localDof;
12924bbf5ea8SMatthew G. Knepley 
12931b68eb51SMatthew G. Knepley         PetscHashIterGetKey(ht, hi, globalDof);
12941b68eb51SMatthew G. Knepley         PetscHashIterGetVal(ht, hi, localDof);
12954bbf5ea8SMatthew G. Knepley         if (globalDof >= 0) globalDofsArray[goff + localDof] = globalDof;
12961b68eb51SMatthew G. Knepley         PetscHashIterNext(ht, hi);
12974bbf5ea8SMatthew G. Knepley       }
12985f824522SMatthew G. Knepley 
12990904074fSPatrick Farrell       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1300c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(gtolCountsWithArtificial, v, &goff);CHKERRQ(ierr);
1301c2e6f3c0SFlorian Wechsung         PetscHashIterBegin(htWithArtificial, hi);
1302c2e6f3c0SFlorian Wechsung         while (!PetscHashIterAtEnd(htWithArtificial, hi)) {
1303c2e6f3c0SFlorian Wechsung           PetscInt globalDof, localDof;
1304c2e6f3c0SFlorian Wechsung           PetscHashIterGetKey(htWithArtificial, hi, globalDof);
1305c2e6f3c0SFlorian Wechsung           PetscHashIterGetVal(htWithArtificial, hi, localDof);
1306c2e6f3c0SFlorian Wechsung           if (globalDof >= 0) globalDofsArrayWithArtificial[goff + localDof] = globalDof;
1307c2e6f3c0SFlorian Wechsung           PetscHashIterNext(htWithArtificial, hi);
1308c2e6f3c0SFlorian Wechsung         }
1309c2e6f3c0SFlorian Wechsung       }
13100904074fSPatrick Farrell       if (isNonlinear) {
13110904074fSPatrick Farrell         ierr = PetscSectionGetOffset(gtolCountsWithAll, v, &goff);CHKERRQ(ierr);
13120904074fSPatrick Farrell         PetscHashIterBegin(htWithAll, hi);
13130904074fSPatrick Farrell         while (!PetscHashIterAtEnd(htWithAll, hi)) {
13140904074fSPatrick Farrell           PetscInt globalDof, localDof;
13150904074fSPatrick Farrell           PetscHashIterGetKey(htWithAll, hi, globalDof);
13160904074fSPatrick Farrell           PetscHashIterGetVal(htWithAll, hi, localDof);
13170904074fSPatrick Farrell           if (globalDof >= 0) globalDofsArrayWithAll[goff + localDof] = globalDof;
13180904074fSPatrick Farrell           PetscHashIterNext(htWithAll, hi);
13190904074fSPatrick Farrell         }
13200904074fSPatrick Farrell       }
1321c2e6f3c0SFlorian Wechsung 
13225f824522SMatthew G. Knepley       for (p = 0; p < Np; ++p) {
13235f824522SMatthew G. Knepley         const PetscInt point = pointsArray[ooff + p];
13245f824522SMatthew G. Knepley         PetscInt       globalDof, localDof;
13255f824522SMatthew G. Knepley 
13265f824522SMatthew G. Knepley         ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, point, NULL, &globalDof);CHKERRQ(ierr);
13271b68eb51SMatthew G. Knepley         ierr = PetscHMapIGet(ht, globalDof, &localDof);CHKERRQ(ierr);
13285f824522SMatthew G. Knepley         offsArray[(ooff + p)*Nf + k] = localDof;
13290904074fSPatrick Farrell         if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1330c2e6f3c0SFlorian Wechsung           ierr = PetscHMapIGet(htWithArtificial, globalDof, &localDof);CHKERRQ(ierr);
1331c2e6f3c0SFlorian Wechsung           offsArrayWithArtificial[(ooff + p)*Nf + k] = localDof;
1332c2e6f3c0SFlorian Wechsung         }
13330904074fSPatrick Farrell         if (isNonlinear) {
13340904074fSPatrick Farrell           ierr = PetscHMapIGet(htWithAll, globalDof, &localDof);CHKERRQ(ierr);
13350904074fSPatrick Farrell           offsArrayWithAll[(ooff + p)*Nf + k] = localDof;
13360904074fSPatrick Farrell         }
13375f824522SMatthew G. Knepley       }
13384bbf5ea8SMatthew G. Knepley     }
13394bbf5ea8SMatthew G. Knepley 
13400cd083f8SSatish Balay     ierr = PetscHSetIDestroy(&globalBcs);CHKERRQ(ierr);
13411b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&ownedpts);CHKERRQ(ierr);
13421b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&seenpts);CHKERRQ(ierr);
13431b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&owneddofs);CHKERRQ(ierr);
13441b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&seendofs);CHKERRQ(ierr);
13451b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&artificialbcs);CHKERRQ(ierr);
1346557beb66SLawrence Mitchell 
13474bbf5ea8SMatthew G. Knepley       /* At this point, we have a hash table ht built that maps globalDof -> localDof.
13484bbf5ea8SMatthew G. Knepley      We need to create the dof table laid out cellwise first, then by subspace,
13494bbf5ea8SMatthew G. Knepley      as the assembler assembles cell-wise and we need to stuff the different
13504bbf5ea8SMatthew G. Knepley      contributions of the different function spaces to the right places. So we loop
13514bbf5ea8SMatthew G. Knepley      over cells, then over subspaces. */
13524bbf5ea8SMatthew G. Knepley     if (patch->nsubspaces > 1) { /* for nsubspaces = 1, data we need is already in dofsArray */
13534bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
13544bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
13555f824522SMatthew G. Knepley         PetscInt       cell = c;
13564bbf5ea8SMatthew G. Knepley 
13575f824522SMatthew G. Knepley         if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);}
13584bbf5ea8SMatthew G. Knepley         for (k = 0; k < patch->nsubspaces; ++k) {
13594bbf5ea8SMatthew G. Knepley           const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
13604bbf5ea8SMatthew G. Knepley           PetscInt        nodesPerCell   = patch->nodesPerCell[k];
13614bbf5ea8SMatthew G. Knepley           PetscInt        subspaceOffset = patch->subspaceOffsets[k];
13624bbf5ea8SMatthew G. Knepley           PetscInt        bs             = patch->bs[k];
13634bbf5ea8SMatthew G. Knepley 
13644bbf5ea8SMatthew G. Knepley           for (j = 0; j < nodesPerCell; ++j) {
13654bbf5ea8SMatthew G. Knepley             for (l = 0; l < bs; ++l) {
13665f824522SMatthew G. Knepley               const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset;
13674bbf5ea8SMatthew G. Knepley               PetscInt       localDof;
13684bbf5ea8SMatthew G. Knepley 
13691b68eb51SMatthew G. Knepley               ierr = PetscHMapIGet(ht, globalDof, &localDof);CHKERRQ(ierr);
1370557beb66SLawrence Mitchell               /* If it's not in the hash table, i.e. is a BC dof,
13711b68eb51SMatthew G. Knepley                then the PetscHSetIMap above gives -1, which matches
1372557beb66SLawrence Mitchell                exactly the convention for PETSc's matrix assembly to
1373557beb66SLawrence Mitchell                ignore the dof. So we don't need to do anything here */
1374c2e6f3c0SFlorian Wechsung               asmArray[asmKey] = localDof;
13750904074fSPatrick Farrell               if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1376c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapIGet(htWithArtificial, globalDof, &localDof);CHKERRQ(ierr);
1377c2e6f3c0SFlorian Wechsung                 asmArrayWithArtificial[asmKey] = localDof;
1378c2e6f3c0SFlorian Wechsung               }
13790904074fSPatrick Farrell               if (isNonlinear) {
13800904074fSPatrick Farrell                 ierr = PetscHMapIGet(htWithAll, globalDof, &localDof);CHKERRQ(ierr);
13810904074fSPatrick Farrell                 asmArrayWithAll[asmKey] = localDof;
13820904074fSPatrick Farrell               }
1383c2e6f3c0SFlorian Wechsung               asmKey++;
13844bbf5ea8SMatthew G. Knepley             }
13854bbf5ea8SMatthew G. Knepley           }
13864bbf5ea8SMatthew G. Knepley         }
13874bbf5ea8SMatthew G. Knepley       }
13884bbf5ea8SMatthew G. Knepley     }
13894bbf5ea8SMatthew G. Knepley   }
1390c2e6f3c0SFlorian Wechsung   if (1 == patch->nsubspaces) {
1391c2e6f3c0SFlorian Wechsung     ierr = PetscMemcpy(asmArray, dofsArray, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
13920904074fSPatrick Farrell     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1393c2e6f3c0SFlorian Wechsung       ierr = PetscMemcpy(asmArrayWithArtificial, dofsArrayWithArtificial, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
1394c2e6f3c0SFlorian Wechsung     }
13950904074fSPatrick Farrell     if (isNonlinear) {
13960904074fSPatrick Farrell       ierr = PetscMemcpy(asmArrayWithAll, dofsArrayWithAll, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
13970904074fSPatrick Farrell     }
1398c2e6f3c0SFlorian Wechsung   }
13994bbf5ea8SMatthew G. Knepley 
14001b68eb51SMatthew G. Knepley   ierr = PetscHMapIDestroy(&ht);CHKERRQ(ierr);
1401c2e6f3c0SFlorian Wechsung   ierr = PetscHMapIDestroy(&htWithArtificial);CHKERRQ(ierr);
14020904074fSPatrick Farrell   ierr = PetscHMapIDestroy(&htWithAll);CHKERRQ(ierr);
14034bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(cells, &cellsArray);CHKERRQ(ierr);
14045f824522SMatthew G. Knepley   ierr = ISRestoreIndices(points, &pointsArray);CHKERRQ(ierr);
14054bbf5ea8SMatthew G. Knepley   ierr = PetscFree(dofsArray);CHKERRQ(ierr);
14060904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1407c2e6f3c0SFlorian Wechsung     ierr = PetscFree(dofsArrayWithArtificial);CHKERRQ(ierr);
1408c2e6f3c0SFlorian Wechsung   }
14090904074fSPatrick Farrell   if (isNonlinear) {
14100904074fSPatrick Farrell     ierr = PetscFree(dofsArrayWithAll);CHKERRQ(ierr);
14110904074fSPatrick Farrell   }
14125f824522SMatthew G. Knepley   /* Create placeholder section for map from points to patch dofs */
14135f824522SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->patchSection);CHKERRQ(ierr);
14145f824522SMatthew G. Knepley   ierr = PetscSectionSetNumFields(patch->patchSection, patch->nsubspaces);CHKERRQ(ierr);
14151e5fa6bbSLawrence Mitchell   if (patch->combined) {
14161e5fa6bbSLawrence Mitchell     PetscInt numFields;
14171e5fa6bbSLawrence Mitchell     ierr = PetscSectionGetNumFields(patch->dofSection[0], &numFields);CHKERRQ(ierr);
14181e5fa6bbSLawrence 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);
14195f824522SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->dofSection[0], &pStart, &pEnd);CHKERRQ(ierr);
14205f824522SMatthew G. Knepley     ierr = PetscSectionSetChart(patch->patchSection, pStart, pEnd);CHKERRQ(ierr);
14215f824522SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
14225f824522SMatthew G. Knepley       PetscInt dof, fdof, f;
14235f824522SMatthew G. Knepley 
14245f824522SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->dofSection[0], p, &dof);CHKERRQ(ierr);
14255f824522SMatthew G. Knepley       ierr = PetscSectionSetDof(patch->patchSection, p, dof);CHKERRQ(ierr);
14265f824522SMatthew G. Knepley       for (f = 0; f < patch->nsubspaces; ++f) {
14271e5fa6bbSLawrence Mitchell         ierr = PetscSectionGetFieldDof(patch->dofSection[0], p, f, &fdof);CHKERRQ(ierr);
14285f824522SMatthew G. Knepley         ierr = PetscSectionSetFieldDof(patch->patchSection, p, f, fdof);CHKERRQ(ierr);
14295f824522SMatthew G. Knepley       }
14301e5fa6bbSLawrence Mitchell     }
14311e5fa6bbSLawrence Mitchell   } else {
14321e5fa6bbSLawrence Mitchell     PetscInt pStartf, pEndf, f;
14331e5fa6bbSLawrence Mitchell     pStart = PETSC_MAX_INT;
14341e5fa6bbSLawrence Mitchell     pEnd = PETSC_MIN_INT;
14351e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
14361e5fa6bbSLawrence Mitchell       ierr = PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf);CHKERRQ(ierr);
14371e5fa6bbSLawrence Mitchell       pStart = PetscMin(pStart, pStartf);
14381e5fa6bbSLawrence Mitchell       pEnd = PetscMax(pEnd, pEndf);
14391e5fa6bbSLawrence Mitchell     }
14401e5fa6bbSLawrence Mitchell     ierr = PetscSectionSetChart(patch->patchSection, pStart, pEnd);CHKERRQ(ierr);
14411e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
14421e5fa6bbSLawrence Mitchell       ierr = PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf);CHKERRQ(ierr);
14431e5fa6bbSLawrence Mitchell       for (p = pStartf; p < pEndf; ++p) {
14441e5fa6bbSLawrence Mitchell         PetscInt fdof;
14451e5fa6bbSLawrence Mitchell         ierr = PetscSectionGetDof(patch->dofSection[f], p, &fdof);CHKERRQ(ierr);
14461e5fa6bbSLawrence Mitchell         ierr = PetscSectionAddDof(patch->patchSection, p, fdof);CHKERRQ(ierr);
14471e5fa6bbSLawrence Mitchell         ierr = PetscSectionSetFieldDof(patch->patchSection, p, f, fdof);CHKERRQ(ierr);
1448bdd9e0cdSPatrick Farrell       }
1449bdd9e0cdSPatrick Farrell     }
14505f824522SMatthew G. Knepley   }
14515f824522SMatthew G. Knepley   ierr = PetscSectionSetUp(patch->patchSection);CHKERRQ(ierr);
14525f824522SMatthew G. Knepley   ierr = PetscSectionSetUseFieldOffsets(patch->patchSection, PETSC_TRUE);CHKERRQ(ierr);
14534bbf5ea8SMatthew G. Knepley   /* Replace cell indices with firedrake-numbered ones. */
14544bbf5ea8SMatthew G. Knepley   ierr = ISGeneralSetIndices(cells, numCells, (const PetscInt *) newCellsArray, PETSC_OWN_POINTER);CHKERRQ(ierr);
14554bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofs, globalDofsArray, PETSC_OWN_POINTER, &patch->gtol);CHKERRQ(ierr);
14565f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->gtol, "Global Indices");CHKERRQ(ierr);
145710534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_g2l_view", patch->classname);CHKERRQ(ierr);
145810534d48SPatrick Farrell   ierr = PetscSectionViewFromOptions(patch->gtolCounts, (PetscObject) pc, option);CHKERRQ(ierr);
145910534d48SPatrick Farrell   ierr = ISViewFromOptions(patch->gtol, (PetscObject) pc, option);CHKERRQ(ierr);
14604bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArray, PETSC_OWN_POINTER, &patch->dofs);CHKERRQ(ierr);
14615f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArray, PETSC_OWN_POINTER, &patch->offs);CHKERRQ(ierr);
14620904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1463c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithArtificial, globalDofsArrayWithArtificial, PETSC_OWN_POINTER, &patch->gtolWithArtificial);CHKERRQ(ierr);
1464c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithArtificial, PETSC_OWN_POINTER, &patch->dofsWithArtificial);CHKERRQ(ierr);
1465c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithArtificial, PETSC_OWN_POINTER, &patch->offsWithArtificial);CHKERRQ(ierr);
1466c2e6f3c0SFlorian Wechsung   }
14670904074fSPatrick Farrell   if (isNonlinear) {
14680904074fSPatrick Farrell     ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithAll, globalDofsArrayWithAll, PETSC_OWN_POINTER, &patch->gtolWithAll);CHKERRQ(ierr);
14690904074fSPatrick Farrell     ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithAll, PETSC_OWN_POINTER, &patch->dofsWithAll);CHKERRQ(ierr);
14700904074fSPatrick Farrell     ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithAll, PETSC_OWN_POINTER, &patch->offsWithAll);CHKERRQ(ierr);
14710904074fSPatrick Farrell   }
14724bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
14734bbf5ea8SMatthew G. Knepley }
14744bbf5ea8SMatthew G. Knepley 
14754bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchZeroFillMatrix_Private(Mat mat, const PetscInt ncell, const PetscInt ndof, const PetscInt *dof)
14764bbf5ea8SMatthew G. Knepley {
147723b8bdd9SMatthew G. Knepley   PetscScalar    *values = NULL;
14784bbf5ea8SMatthew G. Knepley   PetscInt        rows, c, i;
14794bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
14804bbf5ea8SMatthew G. Knepley 
14814bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
14824bbf5ea8SMatthew G. Knepley   ierr = PetscCalloc1(ndof*ndof, &values);CHKERRQ(ierr);
14834bbf5ea8SMatthew G. Knepley   for (c = 0; c < ncell; ++c) {
14844bbf5ea8SMatthew G. Knepley     const PetscInt *idx = &dof[ndof*c];
14854bbf5ea8SMatthew G. Knepley     ierr = MatSetValues(mat, ndof, idx, ndof, idx, values, INSERT_VALUES);CHKERRQ(ierr);
14864bbf5ea8SMatthew G. Knepley   }
14874bbf5ea8SMatthew G. Knepley   ierr = MatGetLocalSize(mat, &rows, NULL);CHKERRQ(ierr);
14884bbf5ea8SMatthew G. Knepley   for (i = 0; i < rows; ++i) {
14894bbf5ea8SMatthew G. Knepley     ierr = MatSetValues(mat, 1, &i, 1, &i, values, INSERT_VALUES);CHKERRQ(ierr);
14904bbf5ea8SMatthew G. Knepley   }
14914bbf5ea8SMatthew G. Knepley   ierr = MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14924bbf5ea8SMatthew G. Knepley   ierr = MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14934bbf5ea8SMatthew G. Knepley   ierr = PetscFree(values);CHKERRQ(ierr);
14944bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
14954bbf5ea8SMatthew G. Knepley }
14964bbf5ea8SMatthew G. Knepley 
1497c2e6f3c0SFlorian Wechsung static PetscErrorCode PCPatchCreateMatrix_Private(PC pc, PetscInt point, Mat *mat, PetscBool withArtificial)
14984bbf5ea8SMatthew G. Knepley {
14994bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
15004bbf5ea8SMatthew G. Knepley   Vec            x, y;
15014bbf5ea8SMatthew G. Knepley   PetscBool      flg;
15024bbf5ea8SMatthew G. Knepley   PetscInt       csize, rsize;
15034bbf5ea8SMatthew G. Knepley   const char    *prefix = NULL;
15044bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
15054bbf5ea8SMatthew G. Knepley 
15064bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1507c2e6f3c0SFlorian Wechsung   if(withArtificial) {
1508e047a90bSFlorian Wechsung     /* would be nice if we could create a rectangular matrix of size numDofsWithArtificial x numDofs here */
15091202d238SPatrick Farrell     x = patch->patchRHSWithArtificial[point];
15101202d238SPatrick Farrell     y = patch->patchRHSWithArtificial[point];
1511ff201f6aSFlorian Wechsung   } else {
15121202d238SPatrick Farrell     x = patch->patchRHS[point];
15131202d238SPatrick Farrell     y = patch->patchUpdate[point];
1514c2e6f3c0SFlorian Wechsung   }
1515c2e6f3c0SFlorian Wechsung 
15164bbf5ea8SMatthew G. Knepley   ierr = VecGetSize(x, &csize);CHKERRQ(ierr);
15174bbf5ea8SMatthew G. Knepley   ierr = VecGetSize(y, &rsize);CHKERRQ(ierr);
15184bbf5ea8SMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, mat);CHKERRQ(ierr);
15194bbf5ea8SMatthew G. Knepley   ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr);
15204bbf5ea8SMatthew G. Knepley   ierr = MatSetOptionsPrefix(*mat, prefix);CHKERRQ(ierr);
15215f824522SMatthew G. Knepley   ierr = MatAppendOptionsPrefix(*mat, "pc_patch_sub_");CHKERRQ(ierr);
15224bbf5ea8SMatthew G. Knepley   if (patch->sub_mat_type)       {ierr = MatSetType(*mat, patch->sub_mat_type);CHKERRQ(ierr);}
15237974b488SMatthew G. Knepley   else if (!patch->sub_mat_type) {ierr = MatSetType(*mat, MATDENSE);CHKERRQ(ierr);}
15244bbf5ea8SMatthew G. Knepley   ierr = MatSetSizes(*mat, rsize, csize, rsize, csize);CHKERRQ(ierr);
15254bbf5ea8SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) *mat, MATDENSE, &flg);CHKERRQ(ierr);
15264bbf5ea8SMatthew G. Knepley   if (!flg) {ierr = PetscObjectTypeCompare((PetscObject)*mat, MATSEQDENSE, &flg);CHKERRQ(ierr);}
15274bbf5ea8SMatthew G. Knepley   /* Sparse patch matrices */
15284bbf5ea8SMatthew G. Knepley   if (!flg) {
15294bbf5ea8SMatthew G. Knepley     PetscBT         bt;
15304bbf5ea8SMatthew G. Knepley     PetscInt       *dnnz      = NULL;
15314bbf5ea8SMatthew G. Knepley     const PetscInt *dofsArray = NULL;
15324bbf5ea8SMatthew G. Knepley     PetscInt        pStart, pEnd, ncell, offset, c, i, j;
15334bbf5ea8SMatthew G. Knepley 
1534c2e6f3c0SFlorian Wechsung     if(withArtificial) {
1535c2e6f3c0SFlorian Wechsung       ierr = ISGetIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1536ff201f6aSFlorian Wechsung     } else {
15374bbf5ea8SMatthew G. Knepley       ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1538c2e6f3c0SFlorian Wechsung     }
15394bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
15404bbf5ea8SMatthew G. Knepley     point += pStart;
15414bbf5ea8SMatthew 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);
15424bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
15434bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
15444bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventBegin(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr);
1545b2866507SPatrick Farrell     /* A PetscBT uses N^2 bits to store the sparsity pattern on a
15464bbf5ea8SMatthew G. Knepley      * patch. This is probably OK if the patches are not too big,
1547b2866507SPatrick Farrell      * but uses too much memory. We therefore switch based on rsize. */
1548b2866507SPatrick Farrell     if (rsize < 3000) { /* FIXME: I picked this switch value out of my hat */
1549b2866507SPatrick Farrell       ierr = PetscCalloc1(rsize, &dnnz);CHKERRQ(ierr);
15504bbf5ea8SMatthew G. Knepley       ierr = PetscBTCreate(rsize*rsize, &bt);CHKERRQ(ierr);
15514bbf5ea8SMatthew G. Knepley       for (c = 0; c < ncell; ++c) {
15524bbf5ea8SMatthew G. Knepley         const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell;
15534bbf5ea8SMatthew G. Knepley         for (i = 0; i < patch->totalDofsPerCell; ++i) {
15544bbf5ea8SMatthew G. Knepley           const PetscInt row = idx[i];
1555557beb66SLawrence Mitchell           if (row < 0) continue;
15564bbf5ea8SMatthew G. Knepley           for (j = 0; j < patch->totalDofsPerCell; ++j) {
15574bbf5ea8SMatthew G. Knepley             const PetscInt col = idx[j];
15584bbf5ea8SMatthew G. Knepley             const PetscInt key = row*rsize + col;
1559557beb66SLawrence Mitchell             if (col < 0) continue;
15604bbf5ea8SMatthew G. Knepley             if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
15614bbf5ea8SMatthew G. Knepley           }
15624bbf5ea8SMatthew G. Knepley         }
15634bbf5ea8SMatthew G. Knepley       }
15644bbf5ea8SMatthew G. Knepley       ierr = PetscBTDestroy(&bt);CHKERRQ(ierr);
15654bbf5ea8SMatthew G. Knepley       ierr = MatXAIJSetPreallocation(*mat, 1, dnnz, NULL, NULL, NULL);CHKERRQ(ierr);
15664bbf5ea8SMatthew G. Knepley       ierr = PetscFree(dnnz);CHKERRQ(ierr);
15674bbf5ea8SMatthew G. Knepley       ierr = PCPatchZeroFillMatrix_Private(*mat, ncell, patch->totalDofsPerCell, &dofsArray[offset*patch->totalDofsPerCell]);CHKERRQ(ierr);
1568b2866507SPatrick Farrell     } else { /* rsize too big, use MATPREALLOCATOR */
1569b2866507SPatrick Farrell       Mat preallocator;
1570b2866507SPatrick Farrell       PetscScalar* vals;
1571b2866507SPatrick Farrell 
1572b2866507SPatrick Farrell       ierr = PetscCalloc1(patch->totalDofsPerCell*patch->totalDofsPerCell, &vals);CHKERRQ(ierr);
1573b2866507SPatrick Farrell       ierr = MatCreate(PETSC_COMM_SELF, &preallocator);CHKERRQ(ierr);
1574b2866507SPatrick Farrell       ierr = MatSetType(preallocator, MATPREALLOCATOR);CHKERRQ(ierr);
1575b2866507SPatrick Farrell       ierr = MatSetSizes(preallocator, rsize, rsize, rsize, rsize);CHKERRQ(ierr);
1576b2866507SPatrick Farrell       ierr = MatSetUp(preallocator);CHKERRQ(ierr);
1577b2866507SPatrick Farrell       for (c = 0; c < ncell; ++c) {
1578b2866507SPatrick Farrell         const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell;
1579b2866507SPatrick Farrell         ierr = MatSetValues(preallocator, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, vals, INSERT_VALUES);CHKERRQ(ierr);
1580b2866507SPatrick Farrell       }
1581b2866507SPatrick Farrell       ierr = PetscFree(vals);CHKERRQ(ierr);
1582b2866507SPatrick Farrell       ierr = MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1583b2866507SPatrick Farrell       ierr = MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1584b2866507SPatrick Farrell       ierr = MatPreallocatorPreallocate(preallocator, PETSC_TRUE, *mat);CHKERRQ(ierr);
1585b2866507SPatrick Farrell       ierr = MatDestroy(&preallocator);CHKERRQ(ierr);
1586b2866507SPatrick Farrell     }
15874bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr);
1588fe117d09SFlorian Wechsung     if(withArtificial) {
1589fe117d09SFlorian Wechsung       ierr = ISRestoreIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1590fe117d09SFlorian Wechsung     } else {
15914bbf5ea8SMatthew G. Knepley       ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
15924bbf5ea8SMatthew G. Knepley     }
1593fe117d09SFlorian Wechsung   }
15944bbf5ea8SMatthew G. Knepley   ierr = MatSetUp(*mat);CHKERRQ(ierr);
15954bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
15964bbf5ea8SMatthew G. Knepley }
15974bbf5ea8SMatthew G. Knepley 
15980904074fSPatrick 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)
159992d50984SMatthew G. Knepley {
160092d50984SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
160192d50984SMatthew G. Knepley   DM              dm;
160292d50984SMatthew G. Knepley   PetscSection    s;
160392d50984SMatthew G. Knepley   const PetscInt *parray, *oarray;
160492d50984SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
160592d50984SMatthew G. Knepley   PetscErrorCode  ierr;
160692d50984SMatthew G. Knepley 
160792d50984SMatthew G. Knepley   PetscFunctionBegin;
160892d50984SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
160992d50984SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
161092d50984SMatthew G. Knepley   /* Set offset into patch */
161192d50984SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->pointCounts, patchNum, &Np);CHKERRQ(ierr);
161292d50984SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->pointCounts, patchNum, &poff);CHKERRQ(ierr);
161392d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->points, &parray);CHKERRQ(ierr);
161492d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->offs,   &oarray);CHKERRQ(ierr);
161592d50984SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
161692d50984SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
161792d50984SMatthew G. Knepley       const PetscInt point = parray[poff+p];
161892d50984SMatthew G. Knepley       PetscInt       dof;
161992d50984SMatthew G. Knepley 
162092d50984SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(patch->patchSection, point, f, &dof);CHKERRQ(ierr);
162192d50984SMatthew G. Knepley       ierr = PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);
162292d50984SMatthew G. Knepley       if (patch->nsubspaces == 1) {ierr = PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);}
162392d50984SMatthew G. Knepley       else                        {ierr = PetscSectionSetOffset(patch->patchSection, point, -1);CHKERRQ(ierr);}
162492d50984SMatthew G. Knepley     }
162592d50984SMatthew G. Knepley   }
162692d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->points, &parray);CHKERRQ(ierr);
162792d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->offs,   &oarray);CHKERRQ(ierr);
162892d50984SMatthew G. Knepley   if (patch->viewSection) {ierr = ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection);CHKERRQ(ierr);}
162992d50984SMatthew G. Knepley   ierr = DMPlexComputeResidual_Patch_Internal(pc->dm, patch->patchSection, cellIS, 0.0, x, NULL, F, ctx);CHKERRQ(ierr);
163092d50984SMatthew G. Knepley   PetscFunctionReturn(0);
163192d50984SMatthew G. Knepley }
163292d50984SMatthew G. Knepley 
163392d50984SMatthew G. Knepley PetscErrorCode PCPatchComputeFunction_Internal(PC pc, Vec x, Vec F, PetscInt point)
163492d50984SMatthew G. Knepley {
163592d50984SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
163692d50984SMatthew G. Knepley   const PetscInt *dofsArray;
16370904074fSPatrick Farrell   const PetscInt *dofsArrayWithAll;
163892d50984SMatthew G. Knepley   const PetscInt *cellsArray;
163992d50984SMatthew G. Knepley   PetscInt        ncell, offset, pStart, pEnd;
164092d50984SMatthew G. Knepley   PetscErrorCode  ierr;
164192d50984SMatthew G. Knepley 
164292d50984SMatthew G. Knepley   PetscFunctionBegin;
164392d50984SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
164492d50984SMatthew G. Knepley   if (!patch->usercomputeop) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback\n");
164592d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
16460904074fSPatrick Farrell   ierr = ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
164792d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
164892d50984SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
164992d50984SMatthew G. Knepley 
165092d50984SMatthew G. Knepley   point += pStart;
165192d50984SMatthew 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);
165292d50984SMatthew G. Knepley 
165392d50984SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
165492d50984SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
165592d50984SMatthew G. Knepley   if (ncell <= 0) {
165692d50984SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
165792d50984SMatthew G. Knepley     PetscFunctionReturn(0);
165892d50984SMatthew G. Knepley   }
165992d50984SMatthew G. Knepley   PetscStackPush("PCPatch user callback");
166092d50984SMatthew G. Knepley   /* Cannot reuse the same IS because the geometry info is being cached in it */
166192d50984SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS);CHKERRQ(ierr);
166239fd2e8aSPatrick Farrell   ierr = patch->usercomputef(pc, point, x, F, patch->cellIS, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell,
16630904074fSPatrick Farrell                                                                                             dofsArrayWithAll + offset*patch->totalDofsPerCell,
166439fd2e8aSPatrick Farrell                                                                                             patch->usercomputefctx);CHKERRQ(ierr);
166592d50984SMatthew G. Knepley   PetscStackPop;
166692d50984SMatthew G. Knepley   ierr = ISDestroy(&patch->cellIS);CHKERRQ(ierr);
166792d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
16680904074fSPatrick Farrell   ierr = ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
166992d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
167092d50984SMatthew G. Knepley   if (patch->viewMatrix) {
167192d50984SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
167292d50984SMatthew G. Knepley 
167392d50984SMatthew G. Knepley     ierr = PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch vector for Point %D", point);CHKERRQ(ierr);
167492d50984SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) F, name);CHKERRQ(ierr);
167592d50984SMatthew G. Knepley     ierr = ObjectView((PetscObject) F, patch->viewerMatrix, patch->formatMatrix);CHKERRQ(ierr);
167692d50984SMatthew G. Knepley   }
167792d50984SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
167892d50984SMatthew G. Knepley   PetscFunctionReturn(0);
167992d50984SMatthew G. Knepley }
168092d50984SMatthew G. Knepley 
16810904074fSPatrick 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)
16825f824522SMatthew G. Knepley {
16835f824522SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
16845f824522SMatthew G. Knepley   DM              dm;
16855f824522SMatthew G. Knepley   PetscSection    s;
16865f824522SMatthew G. Knepley   const PetscInt *parray, *oarray;
16875f824522SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
16885f824522SMatthew G. Knepley   PetscErrorCode  ierr;
16895f824522SMatthew G. Knepley 
16905f824522SMatthew G. Knepley   PetscFunctionBegin;
16915f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
16925f824522SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
16935f824522SMatthew G. Knepley   /* Set offset into patch */
16945f824522SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->pointCounts, patchNum, &Np);CHKERRQ(ierr);
16955f824522SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->pointCounts, patchNum, &poff);CHKERRQ(ierr);
16965f824522SMatthew G. Knepley   ierr = ISGetIndices(patch->points, &parray);CHKERRQ(ierr);
16975f824522SMatthew G. Knepley   ierr = ISGetIndices(patch->offs,   &oarray);CHKERRQ(ierr);
16985f824522SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
16995f824522SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
17005f824522SMatthew G. Knepley       const PetscInt point = parray[poff+p];
17015f824522SMatthew G. Knepley       PetscInt       dof;
17025f824522SMatthew G. Knepley 
17035f824522SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(patch->patchSection, point, f, &dof);CHKERRQ(ierr);
17045f824522SMatthew G. Knepley       ierr = PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);
17055f824522SMatthew G. Knepley       if (patch->nsubspaces == 1) {ierr = PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);}
17065f824522SMatthew G. Knepley       else                        {ierr = PetscSectionSetOffset(patch->patchSection, point, -1);CHKERRQ(ierr);}
17075f824522SMatthew G. Knepley     }
17085f824522SMatthew G. Knepley   }
17095f824522SMatthew G. Knepley   ierr = ISRestoreIndices(patch->points, &parray);CHKERRQ(ierr);
17105f824522SMatthew G. Knepley   ierr = ISRestoreIndices(patch->offs,   &oarray);CHKERRQ(ierr);
17115f824522SMatthew G. Knepley   if (patch->viewSection) {ierr = ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection);CHKERRQ(ierr);}
17125f824522SMatthew G. Knepley   /* TODO Shut off MatViewFromOptions() in MatAssemblyEnd() here */
1713723f9013SMatthew G. Knepley   ierr = DMPlexComputeJacobian_Patch_Internal(pc->dm, patch->patchSection, patch->patchSection, cellIS, 0.0, 0.0, x, NULL, J, J, ctx);CHKERRQ(ierr);
17145f824522SMatthew G. Knepley   PetscFunctionReturn(0);
17155f824522SMatthew G. Knepley }
17165f824522SMatthew G. Knepley 
171734d8b122SPatrick Farrell PetscErrorCode PCPatchComputeOperator_Internal(PC pc, Vec x, Mat mat, PetscInt point, PetscBool withArtificial)
17184bbf5ea8SMatthew G. Knepley {
17194bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
17204bbf5ea8SMatthew G. Knepley   const PetscInt *dofsArray;
17214d04e9f1SPatrick Farrell   const PetscInt *dofsArrayWithArtificial = NULL;
17220904074fSPatrick Farrell   const PetscInt *dofsArrayWithAll = NULL;
17234bbf5ea8SMatthew G. Knepley   const PetscInt *cellsArray;
17244bbf5ea8SMatthew G. Knepley   PetscInt        ncell, offset, pStart, pEnd;
17254d04e9f1SPatrick Farrell   PetscBool       isNonlinear;
17264bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
17274bbf5ea8SMatthew G. Knepley 
17284bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
17294bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
1730debbdec3SPatrick Farrell   isNonlinear = patch->isNonlinear;
17314bbf5ea8SMatthew G. Knepley   if (!patch->usercomputeop) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback\n");
1732c2e6f3c0SFlorian Wechsung   if(withArtificial) {
1733c2e6f3c0SFlorian Wechsung     ierr = ISGetIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1734c2e6f3c0SFlorian Wechsung   } else {
17354bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1736c2e6f3c0SFlorian Wechsung   }
17374d04e9f1SPatrick Farrell   if (isNonlinear) {
17380904074fSPatrick Farrell     ierr = ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
17394d04e9f1SPatrick Farrell   }
17404bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
17414bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
17424bbf5ea8SMatthew G. Knepley 
17434bbf5ea8SMatthew G. Knepley   point += pStart;
17444bbf5ea8SMatthew 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);
17454bbf5ea8SMatthew G. Knepley 
17464bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
17474bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
17484bbf5ea8SMatthew G. Knepley   if (ncell <= 0) {
17494bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
17504bbf5ea8SMatthew G. Knepley     PetscFunctionReturn(0);
17514bbf5ea8SMatthew G. Knepley   }
17524bbf5ea8SMatthew G. Knepley   PetscStackPush("PCPatch user callback");
17532aa6f319SMatthew G. Knepley   /* Cannot reuse the same IS because the geometry info is being cached in it */
17542aa6f319SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS);CHKERRQ(ierr);
17550904074fSPatrick 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);
17564bbf5ea8SMatthew G. Knepley   PetscStackPop;
17572aa6f319SMatthew G. Knepley   ierr = ISDestroy(&patch->cellIS);CHKERRQ(ierr);
17584d04e9f1SPatrick Farrell   if(withArtificial) {
1759c2e6f3c0SFlorian Wechsung     ierr = ISRestoreIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1760c2e6f3c0SFlorian Wechsung   } else {
17614bbf5ea8SMatthew G. Knepley     ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1762c2e6f3c0SFlorian Wechsung   }
17634d04e9f1SPatrick Farrell   if (isNonlinear) {
17640904074fSPatrick Farrell     ierr = ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
17654d04e9f1SPatrick Farrell   }
17664bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
17672aa6f319SMatthew G. Knepley   if (patch->viewMatrix) {
17682aa6f319SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
17692aa6f319SMatthew G. Knepley 
17702aa6f319SMatthew G. Knepley     ierr = PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch matrix for Point %D", point);CHKERRQ(ierr);
17712aa6f319SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) mat, name);CHKERRQ(ierr);
17722aa6f319SMatthew G. Knepley     ierr = ObjectView((PetscObject) mat, patch->viewerMatrix, patch->formatMatrix);CHKERRQ(ierr);
17732aa6f319SMatthew G. Knepley   }
17744bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
17754bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
17764bbf5ea8SMatthew G. Knepley }
17774bbf5ea8SMatthew G. Knepley 
17780904074fSPatrick Farrell PetscErrorCode PCPatch_ScatterLocal_Private(PC pc, PetscInt p, Vec x, Vec y, InsertMode mode, ScatterMode scat, PatchScatterType scattertype)
17794bbf5ea8SMatthew G. Knepley {
17804bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch     = (PC_PATCH *) pc->data;
17814bbf5ea8SMatthew G. Knepley   const PetscScalar *xArray    = NULL;
17824bbf5ea8SMatthew G. Knepley   PetscScalar       *yArray    = NULL;
17834bbf5ea8SMatthew G. Knepley   const PetscInt    *gtolArray = NULL;
17844bbf5ea8SMatthew G. Knepley   PetscInt           dof, offset, lidx;
17854bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
17864bbf5ea8SMatthew G. Knepley 
17874bbf5ea8SMatthew G. Knepley   PetscFunctionBeginHot;
17884bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Scatter, pc, 0, 0, 0);CHKERRQ(ierr);
17894bbf5ea8SMatthew G. Knepley   ierr = VecGetArrayRead(x, &xArray);CHKERRQ(ierr);
17904bbf5ea8SMatthew G. Knepley   ierr = VecGetArray(y, &yArray);CHKERRQ(ierr);
17910904074fSPatrick Farrell   if (scattertype == SCATTER_WITHARTIFICIAL) {
1792c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof);CHKERRQ(ierr);
1793c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offset);CHKERRQ(ierr);
1794c2e6f3c0SFlorian Wechsung     ierr = ISGetIndices(patch->gtolWithArtificial, &gtolArray);CHKERRQ(ierr);
17950904074fSPatrick Farrell   } else if (scattertype == SCATTER_WITHALL) {
17960904074fSPatrick Farrell     ierr = PetscSectionGetDof(patch->gtolCountsWithAll, p, &dof);CHKERRQ(ierr);
17970904074fSPatrick Farrell     ierr = PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offset);CHKERRQ(ierr);
17980904074fSPatrick Farrell     ierr = ISGetIndices(patch->gtolWithAll, &gtolArray);CHKERRQ(ierr);
1799c2e6f3c0SFlorian Wechsung   } else {
18004bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr);
18014bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
18024bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
1803c2e6f3c0SFlorian Wechsung   }
18044bbf5ea8SMatthew 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");
18054bbf5ea8SMatthew 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");
18064bbf5ea8SMatthew G. Knepley   for (lidx = 0; lidx < dof; ++lidx) {
18074bbf5ea8SMatthew G. Knepley     const PetscInt gidx = gtolArray[offset+lidx];
18084bbf5ea8SMatthew G. Knepley 
18094bbf5ea8SMatthew G. Knepley     if (mode == INSERT_VALUES) yArray[lidx]  = xArray[gidx]; /* Forward */
18104bbf5ea8SMatthew G. Knepley     else                       yArray[gidx] += xArray[lidx]; /* Reverse */
18114bbf5ea8SMatthew G. Knepley   }
18120904074fSPatrick Farrell   if (scattertype == SCATTER_WITHARTIFICIAL) {
1813c2e6f3c0SFlorian Wechsung     ierr = ISRestoreIndices(patch->gtolWithArtificial, &gtolArray);CHKERRQ(ierr);
18140904074fSPatrick Farrell   } else if (scattertype == SCATTER_WITHALL) {
18150904074fSPatrick Farrell     ierr = ISRestoreIndices(patch->gtolWithAll, &gtolArray);CHKERRQ(ierr);
1816c2e6f3c0SFlorian Wechsung   } else {
18174bbf5ea8SMatthew G. Knepley     ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
1818c2e6f3c0SFlorian Wechsung   }
18194bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArrayRead(x, &xArray);CHKERRQ(ierr);
18204bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArray(y, &yArray);CHKERRQ(ierr);
18214bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Scatter, pc, 0, 0, 0);CHKERRQ(ierr);
18224bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
18234bbf5ea8SMatthew G. Knepley }
18244bbf5ea8SMatthew G. Knepley 
1825dadc69c5SMatthew G. Knepley static PetscErrorCode PCSetUp_PATCH_Linear(PC pc)
1826dadc69c5SMatthew G. Knepley {
1827dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
1828dadc69c5SMatthew G. Knepley   const char    *prefix;
1829dadc69c5SMatthew G. Knepley   PetscInt       i;
1830dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
1831dadc69c5SMatthew G. Knepley 
1832dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
1833dadc69c5SMatthew G. Knepley   if (!pc->setupcalled) {
1834dadc69c5SMatthew G. Knepley     ierr = PetscMalloc1(patch->npatch, &patch->solver);CHKERRQ(ierr);
1835dadc69c5SMatthew G. Knepley     ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr);
1836dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {
1837dadc69c5SMatthew G. Knepley       KSP ksp;
1838dadc69c5SMatthew G. Knepley       PC  subpc;
1839dadc69c5SMatthew G. Knepley 
1840dadc69c5SMatthew G. Knepley       ierr = KSPCreate(PETSC_COMM_SELF, &ksp);CHKERRQ(ierr);
1841dadc69c5SMatthew G. Knepley       ierr = KSPSetOptionsPrefix(ksp, prefix);CHKERRQ(ierr);
1842dadc69c5SMatthew G. Knepley       ierr = KSPAppendOptionsPrefix(ksp, "sub_");CHKERRQ(ierr);
1843dadc69c5SMatthew G. Knepley       ierr = PetscObjectIncrementTabLevel((PetscObject) ksp, (PetscObject) pc, 1);CHKERRQ(ierr);
1844dadc69c5SMatthew G. Knepley       ierr = KSPGetPC(ksp, &subpc);CHKERRQ(ierr);
1845dadc69c5SMatthew G. Knepley       ierr = PetscObjectIncrementTabLevel((PetscObject) subpc, (PetscObject) pc, 1);CHKERRQ(ierr);
1846dadc69c5SMatthew G. Knepley       ierr = PetscLogObjectParent((PetscObject) pc, (PetscObject) ksp);CHKERRQ(ierr);
1847dadc69c5SMatthew G. Knepley       patch->solver[i] = (PetscObject) ksp;
1848dadc69c5SMatthew G. Knepley     }
1849dadc69c5SMatthew G. Knepley   }
1850dadc69c5SMatthew G. Knepley   if (patch->save_operators) {
1851dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {
1852dadc69c5SMatthew G. Knepley       ierr = MatZeroEntries(patch->mat[i]);CHKERRQ(ierr);
185334d8b122SPatrick Farrell       ierr = PCPatchComputeOperator_Internal(pc, NULL, patch->mat[i], i, PETSC_FALSE);CHKERRQ(ierr);
1854dadc69c5SMatthew G. Knepley       ierr = KSPSetOperators((KSP) patch->solver[i], patch->mat[i], patch->mat[i]);CHKERRQ(ierr);
1855dadc69c5SMatthew G. Knepley     }
1856dadc69c5SMatthew G. Knepley   }
185734d8b122SPatrick Farrell   if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
185834d8b122SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {
18591202d238SPatrick Farrell       /* Instead of padding patch->patchUpdate with zeros to get */
18601202d238SPatrick Farrell       /* patch->patchUpdateWithArtificial and then multiplying with the matrix, */
186134d8b122SPatrick Farrell       /* just get rid of the columns that correspond to the dofs with */
186234d8b122SPatrick Farrell       /* artificial bcs. That's of course fairly inefficient, hopefully we */
186334d8b122SPatrick Farrell       /* can just assemble the rectangular matrix in the first place. */
186434d8b122SPatrick Farrell       Mat matSquare;
186534d8b122SPatrick Farrell       IS rowis;
186634d8b122SPatrick Farrell       PetscInt dof;
186734d8b122SPatrick Farrell 
186834d8b122SPatrick Farrell       ierr = MatGetSize(patch->mat[i], &dof, NULL);CHKERRQ(ierr);
186934d8b122SPatrick Farrell       if (dof == 0) {
187034d8b122SPatrick Farrell         patch->matWithArtificial[i] = NULL;
187134d8b122SPatrick Farrell         continue;
187234d8b122SPatrick Farrell       }
187334d8b122SPatrick Farrell 
187434d8b122SPatrick Farrell       ierr = PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE);CHKERRQ(ierr);
187534d8b122SPatrick Farrell       ierr = MatZeroEntries(matSquare);CHKERRQ(ierr);
187634d8b122SPatrick Farrell       ierr = PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE);CHKERRQ(ierr);
187734d8b122SPatrick Farrell 
187834d8b122SPatrick Farrell       ierr = MatGetSize(matSquare, &dof, NULL);CHKERRQ(ierr);
187934d8b122SPatrick Farrell       ierr = ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis); CHKERRQ(ierr);
188034d8b122SPatrick Farrell       if(pc->setupcalled) {
188134d8b122SPatrick Farrell         ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_REUSE_MATRIX, &patch->matWithArtificial[i]); CHKERRQ(ierr);
188234d8b122SPatrick Farrell       } else {
188334d8b122SPatrick Farrell         ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &patch->matWithArtificial[i]); CHKERRQ(ierr);
188434d8b122SPatrick Farrell       }
188534d8b122SPatrick Farrell       ierr = ISDestroy(&rowis); CHKERRQ(ierr);
188634d8b122SPatrick Farrell       ierr = MatDestroy(&matSquare);CHKERRQ(ierr);
188734d8b122SPatrick Farrell     }
188834d8b122SPatrick Farrell   }
1889dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
1890dadc69c5SMatthew G. Knepley }
1891dadc69c5SMatthew G. Knepley 
18924bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUp_PATCH(PC pc)
18934bbf5ea8SMatthew G. Knepley {
18944bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
1895557beb66SLawrence Mitchell   PetscInt       i;
189639fd2e8aSPatrick Farrell   PetscBool       isNonlinear;
18974bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
18984bbf5ea8SMatthew G. Knepley 
18994bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
19004bbf5ea8SMatthew G. Knepley   if (!pc->setupcalled) {
19014bbf5ea8SMatthew G. Knepley     PetscInt pStart, pEnd, p;
19024bbf5ea8SMatthew G. Knepley     PetscInt localSize;
19034bbf5ea8SMatthew G. Knepley 
19044bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventBegin(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr);
19054bbf5ea8SMatthew G. Knepley 
1906debbdec3SPatrick Farrell     isNonlinear = patch->isNonlinear;
19075f824522SMatthew G. Knepley     if (!patch->nsubspaces) {
19085f824522SMatthew G. Knepley       DM           dm;
19095f824522SMatthew G. Knepley       PetscDS      prob;
19105f824522SMatthew G. Knepley       PetscSection s;
1911e72c1634SMatthew G. Knepley       PetscInt     cStart, cEnd, c, Nf, f, numGlobalBcs = 0, *globalBcs, *Nb, totNb = 0, **cellDofs;
19125f824522SMatthew G. Knepley 
19135f824522SMatthew G. Knepley       ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
19145f824522SMatthew G. Knepley       if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONG, "Must set DM for PCPATCH or call PCPatchSetDiscretisationInfo()");
19155f824522SMatthew G. Knepley       ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
19165f824522SMatthew G. Knepley       ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
19175f824522SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
19185f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
19195f824522SMatthew G. Knepley         PetscInt cdof;
19205f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
19215f824522SMatthew G. Knepley         numGlobalBcs += cdof;
19225f824522SMatthew G. Knepley       }
19235f824522SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
19245f824522SMatthew G. Knepley       ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
19255f824522SMatthew G. Knepley       ierr = PetscMalloc3(Nf, &Nb, Nf, &cellDofs, numGlobalBcs, &globalBcs);CHKERRQ(ierr);
19265f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
19275f824522SMatthew G. Knepley         PetscFE        fe;
19285f824522SMatthew G. Knepley         PetscDualSpace sp;
19295f824522SMatthew G. Knepley         PetscInt       cdoff = 0;
19305f824522SMatthew G. Knepley 
19315f824522SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
19325f824522SMatthew G. Knepley         /* ierr = PetscFEGetNumComponents(fe, &Nc[f]);CHKERRQ(ierr); */
19335f824522SMatthew G. Knepley         ierr = PetscFEGetDualSpace(fe, &sp);CHKERRQ(ierr);
19345f824522SMatthew G. Knepley         ierr = PetscDualSpaceGetDimension(sp, &Nb[f]);CHKERRQ(ierr);
19355f824522SMatthew G. Knepley         totNb += Nb[f];
19365f824522SMatthew G. Knepley 
19375f824522SMatthew G. Knepley         ierr = PetscMalloc1((cEnd-cStart)*Nb[f], &cellDofs[f]);CHKERRQ(ierr);
19385f824522SMatthew G. Knepley         for (c = cStart; c < cEnd; ++c) {
19395f824522SMatthew G. Knepley           PetscInt *closure = NULL;
19405f824522SMatthew G. Knepley           PetscInt  clSize  = 0, cl;
19415f824522SMatthew G. Knepley 
19425f824522SMatthew G. Knepley           ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
19435f824522SMatthew G. Knepley           for (cl = 0; cl < clSize*2; cl += 2) {
19445f824522SMatthew G. Knepley             const PetscInt p = closure[cl];
19455f824522SMatthew G. Knepley             PetscInt       fdof, d, foff;
19465f824522SMatthew G. Knepley 
19475f824522SMatthew G. Knepley             ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr);
19485f824522SMatthew G. Knepley             ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr);
19495f824522SMatthew G. Knepley             for (d = 0; d < fdof; ++d, ++cdoff) cellDofs[f][cdoff] = foff + d;
19505f824522SMatthew G. Knepley           }
19515f824522SMatthew G. Knepley           ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
19525f824522SMatthew G. Knepley         }
19535f824522SMatthew 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]);
19545f824522SMatthew G. Knepley       }
19555f824522SMatthew G. Knepley       numGlobalBcs = 0;
19565f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
19575f824522SMatthew G. Knepley         const PetscInt *ind;
19585f824522SMatthew G. Knepley         PetscInt        off, cdof, d;
19595f824522SMatthew G. Knepley 
19605f824522SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
19615f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
19625f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &ind);CHKERRQ(ierr);
19635f824522SMatthew G. Knepley         for (d = 0; d < cdof; ++d) globalBcs[numGlobalBcs++] = off + ind[d];
19645f824522SMatthew G. Knepley       }
19655f824522SMatthew G. Knepley 
19665f824522SMatthew G. Knepley       ierr = PCPatchSetDiscretisationInfoCombined(pc, dm, Nb, (const PetscInt **) cellDofs, numGlobalBcs, globalBcs, numGlobalBcs, globalBcs);CHKERRQ(ierr);
19675f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
19685f824522SMatthew G. Knepley         ierr = PetscFree(cellDofs[f]);CHKERRQ(ierr);
19695f824522SMatthew G. Knepley       }
19705f824522SMatthew G. Knepley       ierr = PetscFree3(Nb, cellDofs, globalBcs);CHKERRQ(ierr);
197192d50984SMatthew G. Knepley       ierr = PCPatchSetComputeFunction(pc, PCPatchComputeFunction_DMPlex_Private, NULL);CHKERRQ(ierr);
19725f824522SMatthew G. Knepley       ierr = PCPatchSetComputeOperator(pc, PCPatchComputeOperator_DMPlex_Private, NULL);CHKERRQ(ierr);
19735f824522SMatthew G. Knepley     }
19745f824522SMatthew G. Knepley 
19754bbf5ea8SMatthew G. Knepley     localSize = patch->subspaceOffsets[patch->nsubspaces];
19761202d238SPatrick Farrell     ierr = VecCreateSeq(PETSC_COMM_SELF, localSize, &patch->localRHS);CHKERRQ(ierr);
19771202d238SPatrick Farrell     ierr = VecSetUp(patch->localRHS);CHKERRQ(ierr);
19781202d238SPatrick Farrell     ierr = VecDuplicate(patch->localRHS, &patch->localUpdate);CHKERRQ(ierr);
19794bbf5ea8SMatthew G. Knepley     ierr = PCPatchCreateCellPatches(pc);CHKERRQ(ierr);
19804bbf5ea8SMatthew G. Knepley     ierr = PCPatchCreateCellPatchDiscretisationInfo(pc);CHKERRQ(ierr);
19814bbf5ea8SMatthew G. Knepley 
19824bbf5ea8SMatthew G. Knepley     /* OK, now build the work vectors */
19834bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, &pEnd);CHKERRQ(ierr);
19841202d238SPatrick Farrell     ierr = PetscMalloc1(patch->npatch, &patch->patchRHS);CHKERRQ(ierr);
19851202d238SPatrick Farrell     ierr = PetscMalloc1(patch->npatch, &patch->patchUpdate);CHKERRQ(ierr);
1986c2e6f3c0SFlorian Wechsung 
198761c4b389SFlorian Wechsung     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
19881202d238SPatrick Farrell       ierr = PetscMalloc1(patch->npatch, &patch->patchRHSWithArtificial);CHKERRQ(ierr);
1989c2e6f3c0SFlorian Wechsung       ierr = PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithArtificial);CHKERRQ(ierr);
1990c2e6f3c0SFlorian Wechsung     }
19910904074fSPatrick Farrell     if (isNonlinear) {
19920904074fSPatrick Farrell       ierr = PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithAll);CHKERRQ(ierr);
19930904074fSPatrick Farrell     }
19944bbf5ea8SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
19954bbf5ea8SMatthew G. Knepley       PetscInt dof;
19964bbf5ea8SMatthew G. Knepley 
19974bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr);
19981202d238SPatrick Farrell       ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchRHS[p-pStart]);CHKERRQ(ierr);
19991202d238SPatrick Farrell       ierr = VecSetUp(patch->patchRHS[p-pStart]);CHKERRQ(ierr);
20001202d238SPatrick Farrell       ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchUpdate[p-pStart]);CHKERRQ(ierr);
20011202d238SPatrick Farrell       ierr = VecSetUp(patch->patchUpdate[p-pStart]);CHKERRQ(ierr);
20020904074fSPatrick Farrell       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
20033bb0e8f7SKarl Rupp         const PetscInt    *gtolArray, *gtolArrayWithArtificial = NULL;
20043bb0e8f7SKarl Rupp         PetscInt           numPatchDofs, offset;
20053bb0e8f7SKarl Rupp         PetscInt           numPatchDofsWithArtificial, offsetWithArtificial;
20063bb0e8f7SKarl Rupp         PetscInt           dofWithoutArtificialCounter = 0;
20073bb0e8f7SKarl Rupp         PetscInt          *patchWithoutArtificialToWithArtificialArray;
20083bb0e8f7SKarl Rupp 
2009c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof);CHKERRQ(ierr);
20101202d238SPatrick Farrell         ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchRHSWithArtificial[p-pStart]);CHKERRQ(ierr);
20111202d238SPatrick Farrell         ierr = VecSetUp(patch->patchRHSWithArtificial[p-pStart]);CHKERRQ(ierr);
2012c2e6f3c0SFlorian Wechsung 
2013e047a90bSFlorian Wechsung         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
2014e047a90bSFlorian Wechsung         /* the index in the patch with all dofs */
2015c2e6f3c0SFlorian Wechsung         ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
201663deea8eSPatrick Farrell 
2017c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs);CHKERRQ(ierr);
2018168bb5e4SPatrick Farrell         if (numPatchDofs == 0) continue;
201963deea8eSPatrick Farrell 
2020c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
2021c2e6f3c0SFlorian Wechsung         ierr = ISGetIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial);CHKERRQ(ierr);
2022c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &numPatchDofsWithArtificial);CHKERRQ(ierr);
2023c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offsetWithArtificial);CHKERRQ(ierr);
2024c2e6f3c0SFlorian Wechsung 
2025c2e6f3c0SFlorian Wechsung         ierr = PetscMalloc1(numPatchDofs, &patchWithoutArtificialToWithArtificialArray);CHKERRQ(ierr);
2026b0c21b6aSKarl Rupp         for (i=0; i<numPatchDofsWithArtificial; i++) {
2027e047a90bSFlorian Wechsung           if (gtolArrayWithArtificial[i+offsetWithArtificial] == gtolArray[offset+dofWithoutArtificialCounter]) {
2028c2e6f3c0SFlorian Wechsung             patchWithoutArtificialToWithArtificialArray[dofWithoutArtificialCounter] = i;
2029c2e6f3c0SFlorian Wechsung             dofWithoutArtificialCounter++;
2030c2e6f3c0SFlorian Wechsung             if (dofWithoutArtificialCounter == numPatchDofs)
2031c2e6f3c0SFlorian Wechsung               break;
2032c2e6f3c0SFlorian Wechsung           }
2033c2e6f3c0SFlorian Wechsung         }
2034168bb5e4SPatrick Farrell         ierr = ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutArtificialToWithArtificialArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithArtificial[p-pStart]);CHKERRQ(ierr);
2035c2e6f3c0SFlorian Wechsung         ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
2036c2e6f3c0SFlorian Wechsung         ierr = ISRestoreIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial);CHKERRQ(ierr);
2037c2e6f3c0SFlorian Wechsung       }
20380904074fSPatrick Farrell       if (isNonlinear) {
20390904074fSPatrick Farrell         const PetscInt    *gtolArray, *gtolArrayWithAll = NULL;
20400904074fSPatrick Farrell         PetscInt           numPatchDofs, offset;
20410904074fSPatrick Farrell         PetscInt           numPatchDofsWithAll, offsetWithAll;
20420904074fSPatrick Farrell         PetscInt           dofWithoutAllCounter = 0;
20430904074fSPatrick Farrell         PetscInt          *patchWithoutAllToWithAllArray;
20440904074fSPatrick Farrell 
20450904074fSPatrick Farrell         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
20460904074fSPatrick Farrell         /* the index in the patch with all dofs */
20470904074fSPatrick Farrell         ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
20480904074fSPatrick Farrell 
20490904074fSPatrick Farrell         ierr = PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs);CHKERRQ(ierr);
2050168bb5e4SPatrick Farrell         if (numPatchDofs == 0) continue;
20510904074fSPatrick Farrell 
20520904074fSPatrick Farrell         ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
20530904074fSPatrick Farrell         ierr = ISGetIndices(patch->gtolWithAll, &gtolArrayWithAll);CHKERRQ(ierr);
20540904074fSPatrick Farrell         ierr = PetscSectionGetDof(patch->gtolCountsWithAll, p, &numPatchDofsWithAll);CHKERRQ(ierr);
20550904074fSPatrick Farrell         ierr = PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offsetWithAll);CHKERRQ(ierr);
20560904074fSPatrick Farrell 
20570904074fSPatrick Farrell         ierr = PetscMalloc1(numPatchDofs, &patchWithoutAllToWithAllArray);CHKERRQ(ierr);
20580904074fSPatrick Farrell 
20590904074fSPatrick Farrell         for (i=0; i<numPatchDofsWithAll; i++) {
20600904074fSPatrick Farrell           if (gtolArrayWithAll[i+offsetWithAll] == gtolArray[offset+dofWithoutAllCounter]) {
20610904074fSPatrick Farrell             patchWithoutAllToWithAllArray[dofWithoutAllCounter] = i;
20620904074fSPatrick Farrell             dofWithoutAllCounter++;
20630904074fSPatrick Farrell             if (dofWithoutAllCounter == numPatchDofs)
20640904074fSPatrick Farrell               break;
20650904074fSPatrick Farrell           }
20660904074fSPatrick Farrell         }
2067168bb5e4SPatrick Farrell         ierr = ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutAllToWithAllArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithAll[p-pStart]);CHKERRQ(ierr);
20680904074fSPatrick Farrell         ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
20690904074fSPatrick Farrell         ierr = ISRestoreIndices(patch->gtolWithAll, &gtolArrayWithAll);CHKERRQ(ierr);
20700904074fSPatrick Farrell       }
20714bbf5ea8SMatthew G. Knepley     }
20724bbf5ea8SMatthew G. Knepley     if (patch->save_operators) {
20734bbf5ea8SMatthew G. Knepley       ierr = PetscMalloc1(patch->npatch, &patch->mat);CHKERRQ(ierr);
20744bbf5ea8SMatthew G. Knepley       for (i = 0; i < patch->npatch; ++i) {
2075c2e6f3c0SFlorian Wechsung         ierr = PCPatchCreateMatrix_Private(pc, i, &patch->mat[i], PETSC_FALSE);CHKERRQ(ierr);
20764bbf5ea8SMatthew G. Knepley       }
20774bbf5ea8SMatthew G. Knepley     }
20784bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr);
20794bbf5ea8SMatthew G. Knepley 
20804bbf5ea8SMatthew G. Knepley     /* If desired, calculate weights for dof multiplicity */
20814bbf5ea8SMatthew G. Knepley     if (patch->partition_of_unity) {
20823bb0e8f7SKarl Rupp       PetscScalar *input = NULL;
20833bb0e8f7SKarl Rupp       PetscScalar *output = NULL;
20843bb0e8f7SKarl Rupp       Vec global;
20853bb0e8f7SKarl Rupp 
20861202d238SPatrick Farrell       ierr = VecDuplicate(patch->localRHS, &patch->dof_weights);CHKERRQ(ierr);
208761c4b389SFlorian Wechsung       if(patch->local_composition_type == PC_COMPOSITE_ADDITIVE) {
20884bbf5ea8SMatthew G. Knepley         for (i = 0; i < patch->npatch; ++i) {
20894bbf5ea8SMatthew G. Knepley           PetscInt dof;
20904bbf5ea8SMatthew G. Knepley 
20914bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &dof);CHKERRQ(ierr);
20924bbf5ea8SMatthew G. Knepley           if (dof <= 0) continue;
20931202d238SPatrick Farrell           ierr = VecSet(patch->patchRHS[i], 1.0);CHKERRQ(ierr);
20940904074fSPatrick Farrell           ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchRHS[i], patch->dof_weights, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR);CHKERRQ(ierr);
20954bbf5ea8SMatthew G. Knepley         }
2096c2e6f3c0SFlorian Wechsung       } else {
2097e047a90bSFlorian Wechsung         /* multiplicative is actually only locally multiplicative and globally additive. need the pou where the mesh decomposition overlaps */
2098c2e6f3c0SFlorian Wechsung         ierr = VecSet(patch->dof_weights, 1.0);CHKERRQ(ierr);
20994bbf5ea8SMatthew G. Knepley       }
2100d132cafaSFlorian Wechsung 
2101d132cafaSFlorian Wechsung       VecDuplicate(patch->dof_weights, &global);
2102d132cafaSFlorian Wechsung       VecSet(global, 0.);
2103d132cafaSFlorian Wechsung 
2104d132cafaSFlorian Wechsung       ierr = VecGetArray(patch->dof_weights, &input);CHKERRQ(ierr);
2105d132cafaSFlorian Wechsung       ierr = VecGetArray(global, &output);CHKERRQ(ierr);
2106d132cafaSFlorian Wechsung       ierr = PetscSFReduceBegin(patch->defaultSF, MPIU_SCALAR, input, output, MPI_SUM);CHKERRQ(ierr);
2107d132cafaSFlorian Wechsung       ierr = PetscSFReduceEnd(patch->defaultSF, MPIU_SCALAR, input, output, MPI_SUM);CHKERRQ(ierr);
2108d132cafaSFlorian Wechsung       ierr = VecRestoreArray(patch->dof_weights, &input);CHKERRQ(ierr);
2109d132cafaSFlorian Wechsung       ierr = VecRestoreArray(global, &output);CHKERRQ(ierr);
2110d132cafaSFlorian Wechsung 
211105528938SFlorian Wechsung       ierr = VecReciprocal(global);CHKERRQ(ierr);
2112d132cafaSFlorian Wechsung 
2113d132cafaSFlorian Wechsung       ierr = VecGetArray(patch->dof_weights, &output);CHKERRQ(ierr);
2114d132cafaSFlorian Wechsung       ierr = VecGetArray(global, &input);CHKERRQ(ierr);
2115d132cafaSFlorian Wechsung       ierr = PetscSFBcastBegin(patch->defaultSF, MPIU_SCALAR, input, output);CHKERRQ(ierr);
2116d132cafaSFlorian Wechsung       ierr = PetscSFBcastEnd(patch->defaultSF, MPIU_SCALAR, input, output);CHKERRQ(ierr);
2117d132cafaSFlorian Wechsung       ierr = VecRestoreArray(patch->dof_weights, &output);CHKERRQ(ierr);
2118d132cafaSFlorian Wechsung       ierr = VecRestoreArray(global, &input);CHKERRQ(ierr);
2119d132cafaSFlorian Wechsung       ierr = VecDestroy(&global);CHKERRQ(ierr);
21204bbf5ea8SMatthew G. Knepley     }
212161c4b389SFlorian Wechsung     if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE && patch->save_operators) {
212296b79ebeSFlorian Wechsung       ierr = PetscMalloc1(patch->npatch, &patch->matWithArtificial);CHKERRQ(ierr);
21234bbf5ea8SMatthew G. Knepley     }
21244bbf5ea8SMatthew G. Knepley   }
2125dadc69c5SMatthew G. Knepley   ierr = (*patch->setupsolver)(pc);CHKERRQ(ierr);
2126dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
21274bbf5ea8SMatthew G. Knepley }
2128dadc69c5SMatthew G. Knepley 
2129dadc69c5SMatthew G. Knepley static PetscErrorCode PCApply_PATCH_Linear(PC pc, PetscInt i, Vec x, Vec y)
2130dadc69c5SMatthew G. Knepley {
2131dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2132dadc69c5SMatthew G. Knepley   KSP            ksp   = (KSP) patch->solver[i];
2133dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2134dadc69c5SMatthew G. Knepley 
2135dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2136dadc69c5SMatthew G. Knepley   if (!patch->save_operators) {
2137dadc69c5SMatthew G. Knepley     Mat mat;
2138dadc69c5SMatthew G. Knepley 
213934d8b122SPatrick Farrell     ierr = PCPatchCreateMatrix_Private(pc, i, &mat, PETSC_FALSE);CHKERRQ(ierr);
2140dadc69c5SMatthew G. Knepley     /* Populate operator here. */
214134d8b122SPatrick Farrell     ierr = PCPatchComputeOperator_Internal(pc, NULL, mat, i, PETSC_FALSE);CHKERRQ(ierr);
2142dadc69c5SMatthew G. Knepley     ierr = KSPSetOperators(ksp, mat, mat);CHKERRQ(ierr);
2143dadc69c5SMatthew G. Knepley     /* Drop reference so the KSPSetOperators below will blow it away. */
2144dadc69c5SMatthew G. Knepley     ierr = MatDestroy(&mat);CHKERRQ(ierr);
2145dadc69c5SMatthew G. Knepley   }
2146dadc69c5SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr);
2147dadc69c5SMatthew G. Knepley   if (!ksp->setfromoptionscalled) {
2148dadc69c5SMatthew G. Knepley     ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
2149dadc69c5SMatthew G. Knepley   }
2150dadc69c5SMatthew G. Knepley   ierr = KSPSolve(ksp, x, y);CHKERRQ(ierr);
2151dadc69c5SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr);
2152dadc69c5SMatthew G. Knepley   if (!patch->save_operators) {
2153dadc69c5SMatthew G. Knepley     PC pc;
2154dadc69c5SMatthew G. Knepley     ierr = KSPSetOperators(ksp, NULL, NULL);CHKERRQ(ierr);
2155dadc69c5SMatthew G. Knepley     ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
2156dadc69c5SMatthew G. Knepley     /* Destroy PC context too, otherwise the factored matrix hangs around. */
2157dadc69c5SMatthew G. Knepley     ierr = PCReset(pc);CHKERRQ(ierr);
21584bbf5ea8SMatthew G. Knepley   }
21594bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
21604bbf5ea8SMatthew G. Knepley }
21614bbf5ea8SMatthew G. Knepley 
21626c9c532dSPatrick Farrell static PetscErrorCode PCUpdateMultiplicative_PATCH_Linear(PC pc, PetscInt i, PetscInt pStart)
21636c9c532dSPatrick Farrell {
21646c9c532dSPatrick Farrell   PC_PATCH      *patch = (PC_PATCH *) pc->data;
21656c9c532dSPatrick Farrell   Mat multMat;
21666c9c532dSPatrick Farrell   PetscErrorCode ierr;
21676c9c532dSPatrick Farrell 
21684d04e9f1SPatrick Farrell   PetscFunctionBegin;
21694d04e9f1SPatrick Farrell 
21706c9c532dSPatrick Farrell   if (patch->save_operators) {
21716c9c532dSPatrick Farrell     multMat = patch->matWithArtificial[i];
21726c9c532dSPatrick Farrell   } else {
21736c9c532dSPatrick Farrell     /*Very inefficient, hopefully we can just assemble the rectangular matrix in the first place.*/
21746c9c532dSPatrick Farrell     Mat matSquare;
21756c9c532dSPatrick Farrell     PetscInt dof;
21766c9c532dSPatrick Farrell     IS rowis;
21776c9c532dSPatrick Farrell     ierr = PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE);CHKERRQ(ierr);
21786c9c532dSPatrick Farrell     ierr = MatZeroEntries(matSquare);CHKERRQ(ierr);
21796c9c532dSPatrick Farrell     ierr = PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE);CHKERRQ(ierr);
21806c9c532dSPatrick Farrell     ierr = MatGetSize(matSquare, &dof, NULL);CHKERRQ(ierr);
21816c9c532dSPatrick Farrell     ierr = ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis); CHKERRQ(ierr);
21826c9c532dSPatrick Farrell     ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &multMat); CHKERRQ(ierr);
21836c9c532dSPatrick Farrell     ierr = MatDestroy(&matSquare);CHKERRQ(ierr);
21846c9c532dSPatrick Farrell     ierr = ISDestroy(&rowis); CHKERRQ(ierr);
21856c9c532dSPatrick Farrell   }
21866c9c532dSPatrick Farrell   ierr = MatMult(multMat, patch->patchUpdate[i], patch->patchRHSWithArtificial[i]); CHKERRQ(ierr);
21876c9c532dSPatrick Farrell   ierr = VecScale(patch->patchRHSWithArtificial[i], -1.0); CHKERRQ(ierr);
21880904074fSPatrick Farrell   ierr = PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHSWithArtificial[i], patch->localRHS, ADD_VALUES, SCATTER_REVERSE, SCATTER_WITHARTIFICIAL); CHKERRQ(ierr);
21896c9c532dSPatrick Farrell   if (!patch->save_operators) {
21906c9c532dSPatrick Farrell     ierr = MatDestroy(&multMat); CHKERRQ(ierr);
21916c9c532dSPatrick Farrell   }
21924d04e9f1SPatrick Farrell   PetscFunctionReturn(0);
21936c9c532dSPatrick Farrell }
21946c9c532dSPatrick Farrell 
21954bbf5ea8SMatthew G. Knepley static PetscErrorCode PCApply_PATCH(PC pc, Vec x, Vec y)
21964bbf5ea8SMatthew G. Knepley {
21974bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch    = (PC_PATCH *) pc->data;
21981202d238SPatrick Farrell   const PetscScalar *globalRHS  = NULL;
21991202d238SPatrick Farrell   PetscScalar       *localRHS   = NULL;
22001202d238SPatrick Farrell   PetscScalar       *globalUpdate  = NULL;
22014bbf5ea8SMatthew G. Knepley   const PetscInt    *bcNodes  = NULL;
22024bbf5ea8SMatthew G. Knepley   PetscInt           nsweep   = patch->symmetrise_sweep ? 2 : 1;
22034bbf5ea8SMatthew G. Knepley   PetscInt           start[2] = {0, 0};
22044bbf5ea8SMatthew G. Knepley   PetscInt           end[2]   = {-1, -1};
22054bbf5ea8SMatthew G. Knepley   const PetscInt     inc[2]   = {1, -1};
22061202d238SPatrick Farrell   const PetscScalar *localUpdate;
22074bbf5ea8SMatthew G. Knepley   const PetscInt    *iterationSet;
22084bbf5ea8SMatthew G. Knepley   PetscInt           pStart, numBcs, n, sweep, bc, j;
22094bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
22104bbf5ea8SMatthew G. Knepley 
22114bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
22124bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr);
22134bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsPushGetViewerOff(PETSC_TRUE);CHKERRQ(ierr);
221492d50984SMatthew G. Knepley   /* start, end, inc have 2 entries to manage a second backward sweep if we symmetrize */
22154bbf5ea8SMatthew G. Knepley   end[0]   = patch->npatch;
22164bbf5ea8SMatthew G. Knepley   start[1] = patch->npatch-1;
22174bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
22184bbf5ea8SMatthew G. Knepley     ierr = ISGetLocalSize(patch->iterationSet, &end[0]);CHKERRQ(ierr);
22194bbf5ea8SMatthew G. Knepley     start[1] = end[0] - 1;
22204bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr);
22214bbf5ea8SMatthew G. Knepley   }
22224bbf5ea8SMatthew G. Knepley   /* Scatter from global space into overlapped local spaces */
22231202d238SPatrick Farrell   ierr = VecGetArrayRead(x, &globalRHS);CHKERRQ(ierr);
22241202d238SPatrick Farrell   ierr = VecGetArray(patch->localRHS, &localRHS);CHKERRQ(ierr);
22251202d238SPatrick Farrell   ierr = PetscSFBcastBegin(patch->defaultSF, MPIU_SCALAR, globalRHS, localRHS);CHKERRQ(ierr);
22261202d238SPatrick Farrell   ierr = PetscSFBcastEnd(patch->defaultSF, MPIU_SCALAR, globalRHS, localRHS);CHKERRQ(ierr);
22271202d238SPatrick Farrell   ierr = VecRestoreArrayRead(x, &globalRHS);CHKERRQ(ierr);
22281202d238SPatrick Farrell   ierr = VecRestoreArray(patch->localRHS, &localRHS);CHKERRQ(ierr);
22294bbf5ea8SMatthew G. Knepley 
22301202d238SPatrick Farrell   ierr = VecSet(patch->localUpdate, 0.0);CHKERRQ(ierr);
22314bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, NULL);CHKERRQ(ierr);
22324bbf5ea8SMatthew G. Knepley   for (sweep = 0; sweep < nsweep; sweep++) {
22334bbf5ea8SMatthew G. Knepley     for (j = start[sweep]; j*inc[sweep] < end[sweep]*inc[sweep]; j += inc[sweep]) {
22344bbf5ea8SMatthew G. Knepley       PetscInt i       = patch->user_patches ? iterationSet[j] : j;
22354bbf5ea8SMatthew G. Knepley       PetscInt start, len;
22364bbf5ea8SMatthew G. Knepley 
22374bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &len);CHKERRQ(ierr);
22384bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetOffset(patch->gtolCounts, i+pStart, &start);CHKERRQ(ierr);
22394bbf5ea8SMatthew G. Knepley       /* TODO: Squash out these guys in the setup as well. */
22404bbf5ea8SMatthew G. Knepley       if (len <= 0) continue;
22414bbf5ea8SMatthew G. Knepley       /* TODO: Do we need different scatters for X and Y? */
22420904074fSPatrick Farrell       ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->localRHS, patch->patchRHS[i], INSERT_VALUES, SCATTER_FORWARD, SCATTER_INTERIOR);CHKERRQ(ierr);
22431202d238SPatrick Farrell       ierr = (*patch->applysolver)(pc, i, patch->patchRHS[i], patch->patchUpdate[i]);CHKERRQ(ierr);
22440904074fSPatrick Farrell       ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchUpdate[i], patch->localUpdate, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR);CHKERRQ(ierr);
224561c4b389SFlorian Wechsung       if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
22466c9c532dSPatrick Farrell         ierr = (*patch->updatemultiplicative)(pc, i, pStart);CHKERRQ(ierr);
2247c2e6f3c0SFlorian Wechsung       }
22484bbf5ea8SMatthew G. Knepley     }
22494bbf5ea8SMatthew G. Knepley   }
22504bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {ierr = ISRestoreIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr);}
22514bbf5ea8SMatthew G. Knepley   /* XXX: should we do this on the global vector? */
225273ec7555SLawrence Mitchell   if (patch->partition_of_unity) {
22531202d238SPatrick Farrell     ierr = VecPointwiseMult(patch->localUpdate, patch->localUpdate, patch->dof_weights);CHKERRQ(ierr);
22544bbf5ea8SMatthew G. Knepley   }
22551202d238SPatrick Farrell   /* Now patch->localUpdate contains the solution of the patch solves, so we need to combine them all. */
22564bbf5ea8SMatthew G. Knepley   ierr = VecSet(y, 0.0);CHKERRQ(ierr);
22571202d238SPatrick Farrell   ierr = VecGetArray(y, &globalUpdate);CHKERRQ(ierr);
22581202d238SPatrick Farrell   ierr = VecGetArrayRead(patch->localUpdate, &localUpdate);CHKERRQ(ierr);
22591202d238SPatrick Farrell   ierr = PetscSFReduceBegin(patch->defaultSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM);CHKERRQ(ierr);
22601202d238SPatrick Farrell   ierr = PetscSFReduceEnd(patch->defaultSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM);CHKERRQ(ierr);
22611202d238SPatrick Farrell   ierr = VecRestoreArrayRead(patch->localUpdate, &localUpdate);CHKERRQ(ierr);
22624bbf5ea8SMatthew G. Knepley 
22634bbf5ea8SMatthew G. Knepley   /* Now we need to send the global BC values through */
22641202d238SPatrick Farrell   ierr = VecGetArrayRead(x, &globalRHS);CHKERRQ(ierr);
22654bbf5ea8SMatthew G. Knepley   ierr = ISGetSize(patch->globalBcNodes, &numBcs);CHKERRQ(ierr);
22664bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr);
22674bbf5ea8SMatthew G. Knepley   ierr = VecGetLocalSize(x, &n);CHKERRQ(ierr);
22684bbf5ea8SMatthew G. Knepley   for (bc = 0; bc < numBcs; ++bc) {
22694bbf5ea8SMatthew G. Knepley     const PetscInt idx = bcNodes[bc];
22701202d238SPatrick Farrell     if (idx < n) globalUpdate[idx] = globalRHS[idx];
22714bbf5ea8SMatthew G. Knepley   }
22724bbf5ea8SMatthew G. Knepley 
22734bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr);
22741202d238SPatrick Farrell   ierr = VecRestoreArrayRead(x, &globalRHS);CHKERRQ(ierr);
22751202d238SPatrick Farrell   ierr = VecRestoreArray(y, &globalUpdate);CHKERRQ(ierr);
22764bbf5ea8SMatthew G. Knepley 
22774bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsPopGetViewerOff();CHKERRQ(ierr);
22784bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr);
22794bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
22804bbf5ea8SMatthew G. Knepley }
22814bbf5ea8SMatthew G. Knepley 
2282dadc69c5SMatthew G. Knepley static PetscErrorCode PCReset_PATCH_Linear(PC pc)
2283dadc69c5SMatthew G. Knepley {
2284dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2285dadc69c5SMatthew G. Knepley   PetscInt       i;
2286dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2287dadc69c5SMatthew G. Knepley 
2288dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2289dadc69c5SMatthew G. Knepley   if (patch->solver) {
2290dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = KSPReset((KSP) patch->solver[i]);CHKERRQ(ierr);}
2291dadc69c5SMatthew G. Knepley   }
2292dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
2293dadc69c5SMatthew G. Knepley }
2294dadc69c5SMatthew G. Knepley 
22954bbf5ea8SMatthew G. Knepley static PetscErrorCode PCReset_PATCH(PC pc)
22964bbf5ea8SMatthew G. Knepley {
22974bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
22984bbf5ea8SMatthew G. Knepley   PetscInt       i;
22994bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
23004bbf5ea8SMatthew G. Knepley 
23014bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
23024bbf5ea8SMatthew G. Knepley   /* TODO: Get rid of all these ifs */
23034bbf5ea8SMatthew G. Knepley   ierr = PetscSFDestroy(&patch->defaultSF);CHKERRQ(ierr);
23044bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->cellCounts);CHKERRQ(ierr);
23055f824522SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->pointCounts);CHKERRQ(ierr);
23064bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->cellNumbering);CHKERRQ(ierr);
23074bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->gtolCounts);CHKERRQ(ierr);
23084bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->gtol);CHKERRQ(ierr);
23094bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->cells);CHKERRQ(ierr);
23105f824522SMatthew G. Knepley   ierr = ISDestroy(&patch->points);CHKERRQ(ierr);
23114bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->dofs);CHKERRQ(ierr);
23125f824522SMatthew G. Knepley   ierr = ISDestroy(&patch->offs);CHKERRQ(ierr);
23135f824522SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->patchSection);CHKERRQ(ierr);
23144bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->ghostBcNodes);CHKERRQ(ierr);
23154bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->globalBcNodes);CHKERRQ(ierr);
231611ac8bb0SFlorian Wechsung   ierr = PetscSectionDestroy(&patch->gtolCountsWithArtificial);CHKERRQ(ierr);
231711ac8bb0SFlorian Wechsung   ierr = ISDestroy(&patch->gtolWithArtificial);CHKERRQ(ierr);
231811ac8bb0SFlorian Wechsung   ierr = ISDestroy(&patch->dofsWithArtificial);CHKERRQ(ierr);
231911ac8bb0SFlorian Wechsung   ierr = ISDestroy(&patch->offsWithArtificial);CHKERRQ(ierr);
23200904074fSPatrick Farrell   ierr = PetscSectionDestroy(&patch->gtolCountsWithAll);CHKERRQ(ierr);
23210904074fSPatrick Farrell   ierr = ISDestroy(&patch->gtolWithAll);CHKERRQ(ierr);
23220904074fSPatrick Farrell   ierr = ISDestroy(&patch->dofsWithAll);CHKERRQ(ierr);
23230904074fSPatrick Farrell   ierr = ISDestroy(&patch->offsWithAll);CHKERRQ(ierr);
232411ac8bb0SFlorian Wechsung 
23254bbf5ea8SMatthew G. Knepley 
23265f824522SMatthew G. Knepley   if (patch->dofSection) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscSectionDestroy(&patch->dofSection[i]);CHKERRQ(ierr);}
23274bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->dofSection);CHKERRQ(ierr);
23284bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->bs);CHKERRQ(ierr);
23294bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->nodesPerCell);CHKERRQ(ierr);
23305f824522SMatthew G. Knepley   if (patch->cellNodeMap) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscFree(patch->cellNodeMap[i]);CHKERRQ(ierr);}
23314bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->cellNodeMap);CHKERRQ(ierr);
23324bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->subspaceOffsets);CHKERRQ(ierr);
23334bbf5ea8SMatthew G. Knepley 
2334dadc69c5SMatthew G. Knepley   ierr = (*patch->resetsolver)(pc);CHKERRQ(ierr);
23354bbf5ea8SMatthew G. Knepley 
2336e4c66b91SPatrick Farrell   if (patch->subspaces_to_exclude) {
2337e4c66b91SPatrick Farrell     PetscHSetIDestroy(&patch->subspaces_to_exclude);
2338e4c66b91SPatrick Farrell   }
2339e4c66b91SPatrick Farrell 
23401202d238SPatrick Farrell   ierr = VecDestroy(&patch->localRHS);CHKERRQ(ierr);
23411202d238SPatrick Farrell   ierr = VecDestroy(&patch->localUpdate);CHKERRQ(ierr);
23421202d238SPatrick Farrell   if (patch->patchRHS) {
23431202d238SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchRHS[i]);CHKERRQ(ierr);}
23441202d238SPatrick Farrell     ierr = PetscFree(patch->patchRHS);CHKERRQ(ierr);
23454bbf5ea8SMatthew G. Knepley   }
23461202d238SPatrick Farrell   if (patch->patchUpdate) {
23471202d238SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchUpdate[i]);CHKERRQ(ierr);}
23481202d238SPatrick Farrell     ierr = PetscFree(patch->patchUpdate);CHKERRQ(ierr);
23494bbf5ea8SMatthew G. Knepley   }
23504bbf5ea8SMatthew G. Knepley   ierr = VecDestroy(&patch->dof_weights);CHKERRQ(ierr);
23514bbf5ea8SMatthew G. Knepley   if (patch->patch_dof_weights) {
23525f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patch_dof_weights[i]);CHKERRQ(ierr);}
23534bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->patch_dof_weights);CHKERRQ(ierr);
23544bbf5ea8SMatthew G. Knepley   }
23554bbf5ea8SMatthew G. Knepley   if (patch->mat) {
23565f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->mat[i]);CHKERRQ(ierr);}
23574bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->mat);CHKERRQ(ierr);
23585f824522SMatthew G. Knepley   }
235911ac8bb0SFlorian Wechsung   if (patch->matWithArtificial) {
236011ac8bb0SFlorian Wechsung     for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->matWithArtificial[i]);CHKERRQ(ierr);}
236111ac8bb0SFlorian Wechsung     ierr = PetscFree(patch->matWithArtificial);CHKERRQ(ierr);
236211ac8bb0SFlorian Wechsung   }
23631202d238SPatrick Farrell   if (patch->patchRHSWithArtificial) {
23641202d238SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchRHSWithArtificial[i]);CHKERRQ(ierr);}
23651202d238SPatrick Farrell     ierr = PetscFree(patch->patchRHSWithArtificial);CHKERRQ(ierr);
236611ac8bb0SFlorian Wechsung   }
236796b79ebeSFlorian Wechsung   if(patch->dofMappingWithoutToWithArtificial) {
236896b79ebeSFlorian Wechsung     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->dofMappingWithoutToWithArtificial[i]);CHKERRQ(ierr);}
236996b79ebeSFlorian Wechsung     ierr = PetscFree(patch->dofMappingWithoutToWithArtificial);CHKERRQ(ierr);
237096b79ebeSFlorian Wechsung 
237196b79ebeSFlorian Wechsung   }
23720904074fSPatrick Farrell   if(patch->dofMappingWithoutToWithAll) {
23730904074fSPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->dofMappingWithoutToWithAll[i]);CHKERRQ(ierr);}
23740904074fSPatrick Farrell     ierr = PetscFree(patch->dofMappingWithoutToWithAll);CHKERRQ(ierr);
23750904074fSPatrick Farrell 
23760904074fSPatrick Farrell   }
23774bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr);
23785f824522SMatthew G. Knepley   if (patch->userIS) {
23795f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->userIS[i]);CHKERRQ(ierr);}
23805f824522SMatthew G. Knepley     ierr = PetscFree(patch->userIS);CHKERRQ(ierr);
23815f824522SMatthew G. Knepley   }
23824bbf5ea8SMatthew G. Knepley   patch->bs          = 0;
23834bbf5ea8SMatthew G. Knepley   patch->cellNodeMap = NULL;
23847974b488SMatthew G. Knepley   patch->nsubspaces  = 0;
23854bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->iterationSet);CHKERRQ(ierr);
23865f824522SMatthew G. Knepley 
23875f824522SMatthew G. Knepley   ierr = PetscViewerDestroy(&patch->viewerSection);CHKERRQ(ierr);
23884bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
23894bbf5ea8SMatthew G. Knepley }
23904bbf5ea8SMatthew G. Knepley 
2391dadc69c5SMatthew G. Knepley static PetscErrorCode PCDestroy_PATCH_Linear(PC pc)
23924bbf5ea8SMatthew G. Knepley {
23934bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
23944bbf5ea8SMatthew G. Knepley   PetscInt       i;
23954bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
23964bbf5ea8SMatthew G. Knepley 
23974bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2398dadc69c5SMatthew G. Knepley   if (patch->solver) {
2399dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = KSPDestroy((KSP *) &patch->solver[i]);CHKERRQ(ierr);}
2400dadc69c5SMatthew G. Knepley     ierr = PetscFree(patch->solver);CHKERRQ(ierr);
24014bbf5ea8SMatthew G. Knepley   }
2402dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
2403dadc69c5SMatthew G. Knepley }
2404dadc69c5SMatthew G. Knepley 
2405dadc69c5SMatthew G. Knepley static PetscErrorCode PCDestroy_PATCH(PC pc)
2406dadc69c5SMatthew G. Knepley {
2407dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2408dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2409dadc69c5SMatthew G. Knepley 
2410dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2411dadc69c5SMatthew G. Knepley   ierr = PCReset_PATCH(pc);CHKERRQ(ierr);
2412dadc69c5SMatthew G. Knepley   ierr = (*patch->destroysolver)(pc);CHKERRQ(ierr);
24134bbf5ea8SMatthew G. Knepley   ierr = PetscFree(pc->data);CHKERRQ(ierr);
24144bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
24154bbf5ea8SMatthew G. Knepley }
24164bbf5ea8SMatthew G. Knepley 
24174bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetFromOptions_PATCH(PetscOptionItems *PetscOptionsObject, PC pc)
24184bbf5ea8SMatthew G. Knepley {
24194bbf5ea8SMatthew G. Knepley   PC_PATCH            *patch = (PC_PATCH *) pc->data;
24204bbf5ea8SMatthew G. Knepley   PCPatchConstructType patchConstructionType = PC_PATCH_STAR;
24215f824522SMatthew G. Knepley   char                 sub_mat_type[PETSC_MAX_PATH_LEN];
242210534d48SPatrick Farrell   char                 option[PETSC_MAX_PATH_LEN];
24235f824522SMatthew G. Knepley   const char          *prefix;
24244bbf5ea8SMatthew G. Knepley   PetscBool            flg, dimflg, codimflg;
24255f824522SMatthew G. Knepley   MPI_Comm             comm;
2426a48c39c8SPatrick Farrell   PetscInt            *ifields, nfields, k;
24274bbf5ea8SMatthew G. Knepley   PetscErrorCode       ierr;
242861c4b389SFlorian Wechsung   PCCompositeType loctype = PC_COMPOSITE_ADDITIVE;
24294bbf5ea8SMatthew G. Knepley 
24304bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
24315f824522SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) pc, &comm);CHKERRQ(ierr);
24325f824522SMatthew G. Knepley   ierr = PetscObjectGetOptionsPrefix((PetscObject) pc, &prefix);CHKERRQ(ierr);
243310534d48SPatrick Farrell   ierr = PetscOptionsHead(PetscOptionsObject, "Patch solver options");CHKERRQ(ierr);
243410534d48SPatrick Farrell 
243510534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_save_operators", patch->classname);
243610534d48SPatrick Farrell   ierr = PetscOptionsBool(option,  "Store all patch operators for lifetime of object?", "PCPatchSetSaveOperators", patch->save_operators, &patch->save_operators, &flg);CHKERRQ(ierr);
243710534d48SPatrick Farrell 
243810534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_partition_of_unity", patch->classname);
243910534d48SPatrick Farrell   ierr = PetscOptionsBool(option, "Weight contributions by dof multiplicity?", "PCPatchSetPartitionOfUnity", patch->partition_of_unity, &patch->partition_of_unity, &flg);CHKERRQ(ierr);
244010534d48SPatrick Farrell 
244110534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_local_type", patch->classname);
244210534d48SPatrick Farrell   ierr = PetscOptionsEnum(option,"Type of local solver composition (additive or multiplicative)","PCPatchSetLocalComposition",PCCompositeTypes,(PetscEnum)loctype,(PetscEnum*)&loctype,&flg);CHKERRQ(ierr);
244361c4b389SFlorian Wechsung   if(flg) { ierr = PCPatchSetLocalComposition(pc, loctype);CHKERRQ(ierr);}
244410534d48SPatrick Farrell 
244510534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_dim", patch->classname);
244610534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "What dimension of mesh point to construct patches by? (0 = vertices)", "PCPATCH", patch->dim, &patch->dim, &dimflg);CHKERRQ(ierr);
244710534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_codim", patch->classname);
244810534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "What co-dimension of mesh point to construct patches by? (0 = cells)", "PCPATCH", patch->codim, &patch->codim, &codimflg);CHKERRQ(ierr);
244961c4b389SFlorian Wechsung   if (dimflg && codimflg) {SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Can only set one of dimension or co-dimension");CHKERRQ(ierr);}
245010534d48SPatrick Farrell 
245110534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_type", patch->classname);
245210534d48SPatrick Farrell   ierr = PetscOptionsEnum(option, "How should the patches be constructed?", "PCPatchSetConstructType", PCPatchConstructTypes, (PetscEnum) patchConstructionType, (PetscEnum *) &patchConstructionType, &flg);CHKERRQ(ierr);
24534bbf5ea8SMatthew G. Knepley   if (flg) {ierr = PCPatchSetConstructType(pc, patchConstructionType, NULL, NULL);CHKERRQ(ierr);}
245410534d48SPatrick Farrell 
245510534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_vanka_dim", patch->classname);
245610534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "Topological dimension of entities for Vanka to ignore", "PCPATCH", patch->vankadim, &patch->vankadim, &flg);CHKERRQ(ierr);
245710534d48SPatrick Farrell 
245810534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_ignore_dim", patch->classname);
245910534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "Topological dimension of entities for completion to ignore", "PCPATCH", patch->ignoredim, &patch->ignoredim, &flg);CHKERRQ(ierr);
246010534d48SPatrick Farrell 
2461b525f888SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_pardecomp_overlap", patch->classname);
2462b525f888SPatrick Farrell   ierr = PetscOptionsInt(option, "What overlap should we use in construct type pardecomp?", "PCPATCH", patch->pardecomp_overlap, &patch->pardecomp_overlap, &flg);CHKERRQ(ierr);
2463b525f888SPatrick Farrell 
246410534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_sub_mat_type", patch->classname);
246510534d48SPatrick Farrell   ierr = PetscOptionsFList(option, "Matrix type for patch solves", "PCPatchSetSubMatType", MatList, NULL, sub_mat_type, PETSC_MAX_PATH_LEN, &flg);CHKERRQ(ierr);
24664bbf5ea8SMatthew G. Knepley   if (flg) {ierr = PCPatchSetSubMatType(pc, sub_mat_type);CHKERRQ(ierr);}
246710534d48SPatrick Farrell 
246810534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_symmetrise_sweep", patch->classname);
246910534d48SPatrick Farrell   ierr = PetscOptionsBool(option, "Go start->end, end->start?", "PCPATCH", patch->symmetrise_sweep, &patch->symmetrise_sweep, &flg);CHKERRQ(ierr);
2470e4c66b91SPatrick Farrell 
2471a48c39c8SPatrick Farrell   /* If the user has set the number of subspaces, use that for the buffer size,
2472a48c39c8SPatrick Farrell      otherwise use a large number */
2473a48c39c8SPatrick Farrell   if (patch->nsubspaces <= 0) {
2474a48c39c8SPatrick Farrell     nfields = 128;
2475a48c39c8SPatrick Farrell   } else {
2476a48c39c8SPatrick Farrell     nfields = patch->nsubspaces;
2477a48c39c8SPatrick Farrell   }
2478a48c39c8SPatrick Farrell   ierr = PetscMalloc1(nfields, &ifields);CHKERRQ(ierr);
247910534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exclude_subspaces", patch->classname);
248010534d48SPatrick Farrell   ierr = PetscOptionsGetIntArray(((PetscObject)pc)->options,((PetscObject)pc)->prefix,option,ifields,&nfields,&flg);CHKERRQ(ierr);
2481e4c66b91SPatrick 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");
2482e4c66b91SPatrick Farrell   if (flg) {
2483e4c66b91SPatrick Farrell     PetscHSetIClear(patch->subspaces_to_exclude);
248459b66c28SPatrick Farrell     for (k = 0; k < nfields; k++) {
2485e4c66b91SPatrick Farrell       PetscHSetIAdd(patch->subspaces_to_exclude, ifields[k]);
2486e4c66b91SPatrick Farrell     }
2487e4c66b91SPatrick Farrell   }
248859b66c28SPatrick Farrell   ierr = PetscFree(ifields);CHKERRQ(ierr);
24895f824522SMatthew G. Knepley 
249010534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_patches_view", patch->classname);
249110534d48SPatrick Farrell   ierr = PetscOptionsBool(option, "Print out information during patch construction", "PCPATCH", patch->viewPatches, &patch->viewPatches, &flg);CHKERRQ(ierr);
249210534d48SPatrick Farrell 
249310534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_cells_view", patch->classname);
249410534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options,prefix, option,   &patch->viewerCells,   &patch->formatCells,   &patch->viewCells);CHKERRQ(ierr);
249510534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_points_view", patch->classname);
249610534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options,prefix, option,  &patch->viewerPoints,  &patch->formatPoints,  &patch->viewPoints);CHKERRQ(ierr);
249710534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_section_view", patch->classname);
249810534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options,prefix, option, &patch->viewerSection, &patch->formatSection, &patch->viewSection);CHKERRQ(ierr);
249910534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_mat_view", patch->classname);
250010534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options,prefix, option,     &patch->viewerMatrix,  &patch->formatMatrix,  &patch->viewMatrix);CHKERRQ(ierr);
25014bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsTail();CHKERRQ(ierr);
25025f824522SMatthew G. Knepley   patch->optionsSet = PETSC_TRUE;
25034bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
25044bbf5ea8SMatthew G. Knepley }
25054bbf5ea8SMatthew G. Knepley 
25064bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUpOnBlocks_PATCH(PC pc)
25074bbf5ea8SMatthew G. Knepley {
25084bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch = (PC_PATCH*) pc->data;
25094bbf5ea8SMatthew G. Knepley   KSPConvergedReason reason;
25104bbf5ea8SMatthew G. Knepley   PetscInt           i;
25114bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
25124bbf5ea8SMatthew G. Knepley 
25134bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2514a1eac568SLawrence Mitchell   if (!patch->save_operators) {
2515a1eac568SLawrence Mitchell     /* Can't do this here because the sub KSPs don't have an operator attached yet. */
2516a1eac568SLawrence Mitchell     PetscFunctionReturn(0);
2517a1eac568SLawrence Mitchell   }
25184bbf5ea8SMatthew G. Knepley   for (i = 0; i < patch->npatch; ++i) {
2519dadc69c5SMatthew G. Knepley     if (!((KSP) patch->solver[i])->setfromoptionscalled) {
2520dadc69c5SMatthew G. Knepley       ierr = KSPSetFromOptions((KSP) patch->solver[i]);CHKERRQ(ierr);
2521a1eac568SLawrence Mitchell     }
2522dadc69c5SMatthew G. Knepley     ierr = KSPSetUp((KSP) patch->solver[i]);CHKERRQ(ierr);
2523dadc69c5SMatthew G. Knepley     ierr = KSPGetConvergedReason((KSP) patch->solver[i], &reason);CHKERRQ(ierr);
25244bbf5ea8SMatthew G. Knepley     if (reason == KSP_DIVERGED_PCSETUP_FAILED) pc->failedreason = PC_SUBPC_ERROR;
25254bbf5ea8SMatthew G. Knepley   }
25264bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
25274bbf5ea8SMatthew G. Knepley }
25284bbf5ea8SMatthew G. Knepley 
25294bbf5ea8SMatthew G. Knepley static PetscErrorCode PCView_PATCH(PC pc, PetscViewer viewer)
25304bbf5ea8SMatthew G. Knepley {
25314bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
25324bbf5ea8SMatthew G. Knepley   PetscViewer    sviewer;
25334bbf5ea8SMatthew G. Knepley   PetscBool      isascii;
25344bbf5ea8SMatthew G. Knepley   PetscMPIInt    rank;
25354bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
25364bbf5ea8SMatthew G. Knepley 
25374bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
25384bbf5ea8SMatthew G. Knepley   /* TODO Redo tabbing with set tbas in new style */
25394bbf5ea8SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii);CHKERRQ(ierr);
25404bbf5ea8SMatthew G. Knepley   if (!isascii) PetscFunctionReturn(0);
25414bbf5ea8SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) pc), &rank);CHKERRQ(ierr);
25424bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
25434bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "Subspace Correction preconditioner with %d patches\n", patch->npatch);CHKERRQ(ierr);
254461c4b389SFlorian Wechsung   if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
2545c2e6f3c0SFlorian Wechsung     ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: multiplicative\n");CHKERRQ(ierr);
2546e047a90bSFlorian Wechsung   } else {
254773ec7555SLawrence Mitchell     ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: additive\n");CHKERRQ(ierr);
2548c2e6f3c0SFlorian Wechsung   }
25494bbf5ea8SMatthew G. Knepley   if (patch->partition_of_unity) {ierr = PetscViewerASCIIPrintf(viewer, "Weighting by partition of unity\n");CHKERRQ(ierr);}
25504bbf5ea8SMatthew G. Knepley   else                           {ierr = PetscViewerASCIIPrintf(viewer, "Not weighting by partition of unity\n");CHKERRQ(ierr);}
25514bbf5ea8SMatthew G. Knepley   if (patch->symmetrise_sweep) {ierr = PetscViewerASCIIPrintf(viewer, "Symmetrising sweep (start->end, then end->start)\n");CHKERRQ(ierr);}
25524bbf5ea8SMatthew G. Knepley   else                         {ierr = PetscViewerASCIIPrintf(viewer, "Not symmetrising sweep\n");CHKERRQ(ierr);}
25534bbf5ea8SMatthew G. Knepley   if (!patch->save_operators) {ierr = PetscViewerASCIIPrintf(viewer, "Not saving patch operators (rebuilt every PCApply)\n");CHKERRQ(ierr);}
25544bbf5ea8SMatthew G. Knepley   else                        {ierr = PetscViewerASCIIPrintf(viewer, "Saving patch operators (rebuilt every PCSetUp)\n");CHKERRQ(ierr);}
25554bbf5ea8SMatthew G. Knepley   if (patch->patchconstructop == PCPatchConstruct_Star)       {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: star\n");CHKERRQ(ierr);}
25564bbf5ea8SMatthew G. Knepley   else if (patch->patchconstructop == PCPatchConstruct_Vanka) {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: Vanka\n");CHKERRQ(ierr);}
25574bbf5ea8SMatthew G. Knepley   else if (patch->patchconstructop == PCPatchConstruct_User)  {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: user-specified\n");CHKERRQ(ierr);}
25584bbf5ea8SMatthew G. Knepley   else                                                        {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: unknown\n");CHKERRQ(ierr);}
2559dadc69c5SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "Solver on patches (all same):\n");CHKERRQ(ierr);
2560dadc69c5SMatthew G. Knepley   if (patch->solver) {
25614bbf5ea8SMatthew G. Knepley     ierr = PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr);
25624bbf5ea8SMatthew G. Knepley     if (!rank) {
25634bbf5ea8SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(sviewer);CHKERRQ(ierr);
2564dadc69c5SMatthew G. Knepley       ierr = PetscObjectView(patch->solver[0], sviewer);CHKERRQ(ierr);
25654bbf5ea8SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(sviewer);CHKERRQ(ierr);
25664bbf5ea8SMatthew G. Knepley     }
25674bbf5ea8SMatthew G. Knepley     ierr = PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr);
25684bbf5ea8SMatthew G. Knepley   } else {
25694bbf5ea8SMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2570dadc69c5SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Solver not yet set.\n");CHKERRQ(ierr);
25714bbf5ea8SMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
25724bbf5ea8SMatthew G. Knepley   }
25734bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
25744bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
25754bbf5ea8SMatthew G. Knepley }
25764bbf5ea8SMatthew G. Knepley 
2577e5893cccSMatthew G. Knepley /*MC
257898ed095eSMatthew G. Knepley   PCPATCH - A PC object that encapsulates flexible definition of blocks for overlapping and non-overlapping
257998ed095eSMatthew G. Knepley             small block additive preconditioners. Block definition is based on topology from
2580e5893cccSMatthew G. Knepley             a DM and equation numbering from a PetscSection.
2581e5893cccSMatthew G. Knepley 
2582e5893cccSMatthew G. Knepley   Options Database Keys:
2583e5893cccSMatthew G. Knepley + -pc_patch_cells_view   - Views the process local cell numbers for each patch
2584e5893cccSMatthew G. Knepley . -pc_patch_points_view  - Views the process local mesh point numbers for each patch
2585e5893cccSMatthew G. Knepley . -pc_patch_g2l_view     - Views the map between global dofs and patch local dofs for each patch
2586e5893cccSMatthew G. Knepley . -pc_patch_patches_view - Views the global dofs associated with each patch and its boundary
2587e5893cccSMatthew G. Knepley - -pc_patch_sub_mat_view - Views the matrix associated with each patch
2588e5893cccSMatthew G. Knepley 
2589e5893cccSMatthew G. Knepley   Level: intermediate
2590e5893cccSMatthew G. Knepley 
2591e5893cccSMatthew G. Knepley .seealso: PCType, PCCreate(), PCSetType()
2592e5893cccSMatthew G. Knepley M*/
2593642283e9SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PCCreate_Patch(PC pc)
25944bbf5ea8SMatthew G. Knepley {
25954bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch;
25964bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
25974bbf5ea8SMatthew G. Knepley 
25984bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
25994bbf5ea8SMatthew G. Knepley   ierr = PetscNewLog(pc, &patch);CHKERRQ(ierr);
26004bbf5ea8SMatthew G. Knepley 
2601e4c66b91SPatrick Farrell   if (patch->subspaces_to_exclude) {
2602e4c66b91SPatrick Farrell     PetscHSetIDestroy(&patch->subspaces_to_exclude);
2603e4c66b91SPatrick Farrell   }
2604e4c66b91SPatrick Farrell   PetscHSetICreate(&patch->subspaces_to_exclude);
2605e4c66b91SPatrick Farrell 
260610534d48SPatrick Farrell   patch->classname = "pc";
2607debbdec3SPatrick Farrell   patch->isNonlinear = PETSC_FALSE;
260810534d48SPatrick Farrell 
26094bbf5ea8SMatthew G. Knepley   /* Set some defaults */
26105f824522SMatthew G. Knepley   patch->combined           = PETSC_FALSE;
26114bbf5ea8SMatthew G. Knepley   patch->save_operators     = PETSC_TRUE;
261261c4b389SFlorian Wechsung   patch->local_composition_type = PC_COMPOSITE_ADDITIVE;
26134bbf5ea8SMatthew G. Knepley   patch->partition_of_unity = PETSC_FALSE;
26144bbf5ea8SMatthew G. Knepley   patch->codim              = -1;
26154bbf5ea8SMatthew G. Knepley   patch->dim                = -1;
26164bbf5ea8SMatthew G. Knepley   patch->vankadim           = -1;
26175f824522SMatthew G. Knepley   patch->ignoredim          = -1;
2618b525f888SPatrick Farrell   patch->pardecomp_overlap  = 0;
26194bbf5ea8SMatthew G. Knepley   patch->patchconstructop   = PCPatchConstruct_Star;
26204bbf5ea8SMatthew G. Knepley   patch->symmetrise_sweep   = PETSC_FALSE;
26215f824522SMatthew G. Knepley   patch->npatch             = 0;
26224bbf5ea8SMatthew G. Knepley   patch->userIS             = NULL;
26235f824522SMatthew G. Knepley   patch->optionsSet         = PETSC_FALSE;
26244bbf5ea8SMatthew G. Knepley   patch->iterationSet       = NULL;
26254bbf5ea8SMatthew G. Knepley   patch->user_patches       = PETSC_FALSE;
26265f824522SMatthew G. Knepley   ierr = PetscStrallocpy(MATDENSE, (char **) &patch->sub_mat_type);CHKERRQ(ierr);
26275f824522SMatthew G. Knepley   patch->viewPatches        = PETSC_FALSE;
26285f824522SMatthew G. Knepley   patch->viewCells          = PETSC_FALSE;
26295f824522SMatthew G. Knepley   patch->viewPoints         = PETSC_FALSE;
26305f824522SMatthew G. Knepley   patch->viewSection        = PETSC_FALSE;
26315f824522SMatthew G. Knepley   patch->viewMatrix         = PETSC_FALSE;
2632dadc69c5SMatthew G. Knepley   patch->setupsolver        = PCSetUp_PATCH_Linear;
2633dadc69c5SMatthew G. Knepley   patch->applysolver        = PCApply_PATCH_Linear;
2634dadc69c5SMatthew G. Knepley   patch->resetsolver        = PCReset_PATCH_Linear;
2635dadc69c5SMatthew G. Knepley   patch->destroysolver      = PCDestroy_PATCH_Linear;
26366c9c532dSPatrick Farrell   patch->updatemultiplicative = PCUpdateMultiplicative_PATCH_Linear;
26374bbf5ea8SMatthew G. Knepley 
26384bbf5ea8SMatthew G. Knepley   pc->data                 = (void *) patch;
26394bbf5ea8SMatthew G. Knepley   pc->ops->apply           = PCApply_PATCH;
26404bbf5ea8SMatthew G. Knepley   pc->ops->applytranspose  = 0; /* PCApplyTranspose_PATCH; */
26414bbf5ea8SMatthew G. Knepley   pc->ops->setup           = PCSetUp_PATCH;
26424bbf5ea8SMatthew G. Knepley   pc->ops->reset           = PCReset_PATCH;
26434bbf5ea8SMatthew G. Knepley   pc->ops->destroy         = PCDestroy_PATCH;
26444bbf5ea8SMatthew G. Knepley   pc->ops->setfromoptions  = PCSetFromOptions_PATCH;
26454bbf5ea8SMatthew G. Knepley   pc->ops->setuponblocks   = PCSetUpOnBlocks_PATCH;
26464bbf5ea8SMatthew G. Knepley   pc->ops->view            = PCView_PATCH;
26474bbf5ea8SMatthew G. Knepley   pc->ops->applyrichardson = 0;
26484bbf5ea8SMatthew G. Knepley 
26494bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
26504bbf5ea8SMatthew G. Knepley }
2651