xref: /petsc/src/ksp/pc/impls/patch/pcpatch.c (revision eb62eeaaf0d42826a5f37c78ce2a90ed1f5c2b10)
14bbf5ea8SMatthew G. Knepley #include <petsc/private/pcpatchimpl.h>     /*I "petscpc.h" I*/
254ab768cSLawrence Mitchell #include <petsc/private/kspimpl.h>         /* For ksp->setfromoptionscalled */
35f824522SMatthew G. Knepley #include <petsc/private/dmpleximpl.h> /* For DMPlexComputeJacobian_Patch_Internal() */
44bbf5ea8SMatthew G. Knepley #include <petscsf.h>
54bbf5ea8SMatthew G. Knepley #include <petscbt.h>
65f824522SMatthew G. Knepley #include <petscds.h>
74bbf5ea8SMatthew G. Knepley 
84bbf5ea8SMatthew G. Knepley PetscLogEvent PC_Patch_CreatePatches, PC_Patch_ComputeOp, PC_Patch_Solve, PC_Patch_Scatter, PC_Patch_Apply, PC_Patch_Prealloc;
94bbf5ea8SMatthew G. Knepley 
105f824522SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode ObjectView(PetscObject obj, PetscViewer viewer, PetscViewerFormat format)
115f824522SMatthew G. Knepley {
125f824522SMatthew G. Knepley   PetscErrorCode ierr;
135f824522SMatthew G. Knepley 
145f824522SMatthew G. Knepley   ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr);
155f824522SMatthew G. Knepley   ierr = PetscObjectView(obj, viewer);CHKERRQ(ierr);
165f824522SMatthew G. Knepley   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
177974b488SMatthew G. Knepley   return(0);
185f824522SMatthew G. Knepley }
195f824522SMatthew G. Knepley 
201b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_Star(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
214bbf5ea8SMatthew G. Knepley {
224bbf5ea8SMatthew G. Knepley   PetscInt       starSize;
234bbf5ea8SMatthew G. Knepley   PetscInt      *star = NULL, si;
244bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
254bbf5ea8SMatthew G. Knepley 
264bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
271b68eb51SMatthew G. Knepley   PetscHSetIClear(ht);
284bbf5ea8SMatthew G. Knepley   /* To start with, add the point we care about */
291b68eb51SMatthew G. Knepley   ierr = PetscHSetIAdd(ht, point);CHKERRQ(ierr);
304bbf5ea8SMatthew G. Knepley   /* Loop over all the points that this point connects to */
314bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
321b68eb51SMatthew G. Knepley   for (si = 0; si < starSize*2; si += 2) {ierr = PetscHSetIAdd(ht, star[si]);CHKERRQ(ierr);}
334bbf5ea8SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
344bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
354bbf5ea8SMatthew G. Knepley }
364bbf5ea8SMatthew G. Knepley 
371b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_Vanka(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
384bbf5ea8SMatthew G. Knepley {
394bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) vpatch;
404bbf5ea8SMatthew G. Knepley   PetscInt       starSize;
414bbf5ea8SMatthew G. Knepley   PetscInt      *star = NULL;
424bbf5ea8SMatthew G. Knepley   PetscBool      shouldIgnore = PETSC_FALSE;
434bbf5ea8SMatthew G. Knepley   PetscInt       cStart, cEnd, iStart, iEnd, si;
444bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
454bbf5ea8SMatthew G. Knepley 
464bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
471b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(ht);CHKERRQ(ierr);
484bbf5ea8SMatthew G. Knepley   /* To start with, add the point we care about */
491b68eb51SMatthew G. Knepley   ierr = PetscHSetIAdd(ht, point);CHKERRQ(ierr);
504bbf5ea8SMatthew G. Knepley   /* Should we ignore any points of a certain dimension? */
514bbf5ea8SMatthew G. Knepley   if (patch->vankadim >= 0) {
524bbf5ea8SMatthew G. Knepley     shouldIgnore = PETSC_TRUE;
534bbf5ea8SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, patch->vankadim, &iStart, &iEnd);CHKERRQ(ierr);
544bbf5ea8SMatthew G. Knepley   }
554bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
564bbf5ea8SMatthew G. Knepley   /* Loop over all the cells that this point connects to */
574bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
585f824522SMatthew G. Knepley   for (si = 0; si < starSize*2; si += 2) {
594bbf5ea8SMatthew G. Knepley     const PetscInt cell = star[si];
604bbf5ea8SMatthew G. Knepley     PetscInt       closureSize;
614bbf5ea8SMatthew G. Knepley     PetscInt      *closure = NULL, ci;
624bbf5ea8SMatthew G. Knepley 
634bbf5ea8SMatthew G. Knepley     if (cell < cStart || cell >= cEnd) continue;
644bbf5ea8SMatthew G. Knepley     /* now loop over all entities in the closure of that cell */
654bbf5ea8SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
665f824522SMatthew G. Knepley     for (ci = 0; ci < closureSize*2; ci += 2) {
674bbf5ea8SMatthew G. Knepley       const PetscInt newpoint = closure[ci];
684bbf5ea8SMatthew G. Knepley 
694bbf5ea8SMatthew G. Knepley       /* We've been told to ignore entities of this type.*/
704bbf5ea8SMatthew G. Knepley       if (shouldIgnore && newpoint >= iStart && newpoint < iEnd) continue;
711b68eb51SMatthew G. Knepley       ierr = PetscHSetIAdd(ht, newpoint);CHKERRQ(ierr);
724bbf5ea8SMatthew G. Knepley     }
734bbf5ea8SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
744bbf5ea8SMatthew G. Knepley   }
754bbf5ea8SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
764bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
774bbf5ea8SMatthew G. Knepley }
784bbf5ea8SMatthew G. Knepley 
79e5b9877fSPatrick Farrell static PetscErrorCode PCPatchConstruct_Pardecomp(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
800a390943SPatrick Farrell {
81b525f888SPatrick Farrell   PC_PATCH       *patch = (PC_PATCH *) vpatch;
820a390943SPatrick Farrell   DMLabel         ghost = NULL;
830a390943SPatrick Farrell   const PetscInt *leaves;
840a390943SPatrick Farrell   PetscInt        nleaves, pStart, pEnd, loc;
850a390943SPatrick Farrell   PetscBool       isFiredrake;
860a390943SPatrick Farrell   DM              plex;
870a390943SPatrick Farrell   PetscBool       flg;
88b525f888SPatrick Farrell   PetscInt        starSize;
89b525f888SPatrick Farrell   PetscInt       *star = NULL;
9025fd193aSPatrick Farrell   PetscInt        opoint, overlapi;
910a390943SPatrick Farrell   PetscErrorCode  ierr;
920a390943SPatrick Farrell 
930a390943SPatrick Farrell   PetscFunctionBegin;
940a390943SPatrick Farrell   PetscHSetIClear(ht);
950a390943SPatrick Farrell 
960a390943SPatrick Farrell   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
970a390943SPatrick Farrell   ierr = DMPlexGetChart(plex, &pStart, &pEnd);CHKERRQ(ierr);
980a390943SPatrick Farrell 
990a390943SPatrick Farrell   ierr = DMHasLabel(dm, "pyop2_ghost", &isFiredrake);CHKERRQ(ierr);
1000a390943SPatrick Farrell   if (isFiredrake) {
1010a390943SPatrick Farrell     ierr = DMGetLabel(dm, "pyop2_ghost", &ghost);CHKERRQ(ierr);
1020a390943SPatrick Farrell     ierr = DMLabelCreateIndex(ghost, pStart, pEnd);CHKERRQ(ierr);
1030a390943SPatrick Farrell   } else {
1040a390943SPatrick Farrell     PetscSF sf;
1050a390943SPatrick Farrell     ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
1060a390943SPatrick Farrell     ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
1070a390943SPatrick Farrell     nleaves = PetscMax(nleaves, 0);
1080a390943SPatrick Farrell   }
1090a390943SPatrick Farrell 
11025fd193aSPatrick Farrell   for (opoint = pStart; opoint < pEnd; ++opoint) {
111bfabdd78SPatrick Farrell     if (ghost) {ierr = DMLabelHasPoint(ghost, opoint, &flg);CHKERRQ(ierr);}
112bfabdd78SPatrick Farrell     else       {ierr = PetscFindInt(opoint, nleaves, leaves, &loc);CHKERRQ(ierr); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;}
1130a390943SPatrick Farrell     /* Not an owned entity, don't make a cell patch. */
1140a390943SPatrick Farrell     if (flg) continue;
115bfabdd78SPatrick Farrell     ierr = PetscHSetIAdd(ht, opoint);CHKERRQ(ierr);
1160a390943SPatrick Farrell   }
1170a390943SPatrick Farrell 
118b525f888SPatrick Farrell   /* Now build the overlap for the patch */
11925fd193aSPatrick Farrell   for (overlapi = 0; overlapi < patch->pardecomp_overlap; ++overlapi) {
120b525f888SPatrick Farrell     PetscInt index = 0;
121b525f888SPatrick Farrell     PetscInt *htpoints = NULL;
122b525f888SPatrick Farrell     PetscInt htsize;
12325fd193aSPatrick Farrell     PetscInt i;
124b525f888SPatrick Farrell 
125b525f888SPatrick Farrell     ierr = PetscHSetIGetSize(ht, &htsize);CHKERRQ(ierr);
126b525f888SPatrick Farrell     ierr = PetscMalloc1(htsize, &htpoints);CHKERRQ(ierr);
127b525f888SPatrick Farrell     ierr = PetscHSetIGetElems(ht, &index, htpoints);CHKERRQ(ierr);
128b525f888SPatrick Farrell 
12925fd193aSPatrick Farrell     for (i = 0; i < htsize; ++i) {
13025fd193aSPatrick Farrell       PetscInt hpoint = htpoints[i];
13125fd193aSPatrick Farrell       PetscInt si;
132b525f888SPatrick Farrell 
13325fd193aSPatrick Farrell       ierr = DMPlexGetTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
13425fd193aSPatrick Farrell       for (si = 0; si < starSize*2; si += 2) {
135b525f888SPatrick Farrell         const PetscInt starp = star[si];
136b525f888SPatrick Farrell         PetscInt       closureSize;
137b525f888SPatrick Farrell         PetscInt      *closure = NULL, ci;
138b525f888SPatrick Farrell 
139b525f888SPatrick Farrell         /* now loop over all entities in the closure of starp */
140b525f888SPatrick Farrell         ierr = DMPlexGetTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
14125fd193aSPatrick Farrell         for (ci = 0; ci < closureSize*2; ci += 2) {
142b525f888SPatrick Farrell           const PetscInt closstarp = closure[ci];
143b525f888SPatrick Farrell           ierr = PetscHSetIAdd(ht, closstarp);CHKERRQ(ierr);
144b525f888SPatrick Farrell         }
145b525f888SPatrick Farrell         ierr = DMPlexRestoreTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
146b525f888SPatrick Farrell       }
14725fd193aSPatrick Farrell       ierr = DMPlexRestoreTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
148b525f888SPatrick Farrell     }
149b525f888SPatrick Farrell     ierr = PetscFree(htpoints);CHKERRQ(ierr);
150b525f888SPatrick Farrell   }
151b525f888SPatrick Farrell 
1520a390943SPatrick Farrell   PetscFunctionReturn(0);
1530a390943SPatrick Farrell }
1540a390943SPatrick Farrell 
1554bbf5ea8SMatthew G. Knepley /* The user's already set the patches in patch->userIS. Build the hash tables */
1561b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_User(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
1574bbf5ea8SMatthew G. Knepley {
1584bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch   = (PC_PATCH *) vpatch;
1594bbf5ea8SMatthew G. Knepley   IS              patchis = patch->userIS[point];
1604bbf5ea8SMatthew G. Knepley   PetscInt        n;
1614bbf5ea8SMatthew G. Knepley   const PetscInt *patchdata;
1624bbf5ea8SMatthew G. Knepley   PetscInt        pStart, pEnd, i;
1634bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
1644bbf5ea8SMatthew G. Knepley 
1654bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1661b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(ht);CHKERRQ(ierr);
1671b68eb51SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
1684bbf5ea8SMatthew G. Knepley   ierr = ISGetLocalSize(patchis, &n);CHKERRQ(ierr);
1694bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patchis, &patchdata);CHKERRQ(ierr);
1704bbf5ea8SMatthew G. Knepley   for (i = 0; i < n; ++i) {
1714bbf5ea8SMatthew G. Knepley     const PetscInt ownedpoint = patchdata[i];
1724bbf5ea8SMatthew G. Knepley 
1734bbf5ea8SMatthew G. Knepley     if (ownedpoint < pStart || ownedpoint >= pEnd) {
1744bbf5ea8SMatthew G. Knepley       SETERRQ3(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D was not in [%D, %D)", ownedpoint, pStart, pEnd);
1754bbf5ea8SMatthew G. Knepley     }
1761b68eb51SMatthew G. Knepley     ierr = PetscHSetIAdd(ht, ownedpoint);CHKERRQ(ierr);
1774bbf5ea8SMatthew G. Knepley   }
1784bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patchis, &patchdata);CHKERRQ(ierr);
1794bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
1804bbf5ea8SMatthew G. Knepley }
1814bbf5ea8SMatthew G. Knepley 
1824bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateDefaultSF_Private(PC pc, PetscInt n, const PetscSF *sf, const PetscInt *bs)
1834bbf5ea8SMatthew G. Knepley {
1844bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
1854bbf5ea8SMatthew G. Knepley   PetscInt       i;
1864bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
1874bbf5ea8SMatthew G. Knepley 
1884bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1894bbf5ea8SMatthew G. Knepley   if (n == 1 && bs[0] == 1) {
1904bbf5ea8SMatthew G. Knepley     patch->defaultSF = sf[0];
1914bbf5ea8SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) patch->defaultSF);CHKERRQ(ierr);
1924bbf5ea8SMatthew G. Knepley   } else {
1934bbf5ea8SMatthew G. Knepley     PetscInt     allRoots = 0, allLeaves = 0;
1944bbf5ea8SMatthew G. Knepley     PetscInt     leafOffset = 0;
1954bbf5ea8SMatthew G. Knepley     PetscInt    *ilocal = NULL;
1964bbf5ea8SMatthew G. Knepley     PetscSFNode *iremote = NULL;
1974bbf5ea8SMatthew G. Knepley     PetscInt    *remoteOffsets = NULL;
1984bbf5ea8SMatthew G. Knepley     PetscInt     index = 0;
1991b68eb51SMatthew G. Knepley     PetscHMapI   rankToIndex;
2004bbf5ea8SMatthew G. Knepley     PetscInt     numRanks = 0;
2014bbf5ea8SMatthew G. Knepley     PetscSFNode *remote = NULL;
2024bbf5ea8SMatthew G. Knepley     PetscSF      rankSF;
2034bbf5ea8SMatthew G. Knepley     PetscInt    *ranks = NULL;
2044bbf5ea8SMatthew G. Knepley     PetscInt    *offsets = NULL;
2054bbf5ea8SMatthew G. Knepley     MPI_Datatype contig;
2061b68eb51SMatthew G. Knepley     PetscHSetI   ranksUniq;
2074bbf5ea8SMatthew G. Knepley 
2084bbf5ea8SMatthew G. Knepley     /* First figure out how many dofs there are in the concatenated numbering.
2094bbf5ea8SMatthew G. Knepley      * allRoots: number of owned global dofs;
2104bbf5ea8SMatthew G. Knepley      * allLeaves: number of visible dofs (global + ghosted).
2114bbf5ea8SMatthew G. Knepley      */
2124bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2134bbf5ea8SMatthew G. Knepley       PetscInt nroots, nleaves;
2144bbf5ea8SMatthew G. Knepley 
2154bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetGraph(sf[i], &nroots, &nleaves, NULL, NULL);CHKERRQ(ierr);
2164bbf5ea8SMatthew G. Knepley       allRoots  += nroots * bs[i];
2174bbf5ea8SMatthew G. Knepley       allLeaves += nleaves * bs[i];
2184bbf5ea8SMatthew G. Knepley     }
2194bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(allLeaves, &ilocal);CHKERRQ(ierr);
2204bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(allLeaves, &iremote);CHKERRQ(ierr);
2214bbf5ea8SMatthew G. Knepley     /* Now build an SF that just contains process connectivity. */
2221b68eb51SMatthew G. Knepley     ierr = PetscHSetICreate(&ranksUniq);CHKERRQ(ierr);
2234bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2244bbf5ea8SMatthew G. Knepley       const PetscMPIInt *ranks = NULL;
2254bbf5ea8SMatthew G. Knepley       PetscInt           nranks, j;
2264bbf5ea8SMatthew G. Knepley 
2274bbf5ea8SMatthew G. Knepley       ierr = PetscSFSetUp(sf[i]);CHKERRQ(ierr);
2284bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetRanks(sf[i], &nranks, &ranks, NULL, NULL, NULL);CHKERRQ(ierr);
2294bbf5ea8SMatthew G. Knepley       /* These are all the ranks who communicate with me. */
2304bbf5ea8SMatthew G. Knepley       for (j = 0; j < nranks; ++j) {
2311b68eb51SMatthew G. Knepley         ierr = PetscHSetIAdd(ranksUniq, (PetscInt) ranks[j]);CHKERRQ(ierr);
2324bbf5ea8SMatthew G. Knepley       }
2334bbf5ea8SMatthew G. Knepley     }
2341b68eb51SMatthew G. Knepley     ierr = PetscHSetIGetSize(ranksUniq, &numRanks);CHKERRQ(ierr);
2354bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(numRanks, &remote);CHKERRQ(ierr);
2364bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(numRanks, &ranks);CHKERRQ(ierr);
2371b68eb51SMatthew G. Knepley     ierr = PetscHSetIGetElems(ranksUniq, &index, ranks);CHKERRQ(ierr);
2384bbf5ea8SMatthew G. Knepley 
2391b68eb51SMatthew G. Knepley     ierr = PetscHMapICreate(&rankToIndex);CHKERRQ(ierr);
2404bbf5ea8SMatthew G. Knepley     for (i = 0; i < numRanks; ++i) {
2414bbf5ea8SMatthew G. Knepley       remote[i].rank  = ranks[i];
2424bbf5ea8SMatthew G. Knepley       remote[i].index = 0;
2431b68eb51SMatthew G. Knepley       ierr = PetscHMapISet(rankToIndex, ranks[i], i);CHKERRQ(ierr);
2444bbf5ea8SMatthew G. Knepley     }
2454bbf5ea8SMatthew G. Knepley     ierr = PetscFree(ranks);CHKERRQ(ierr);
2461b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&ranksUniq);CHKERRQ(ierr);
2474bbf5ea8SMatthew G. Knepley     ierr = PetscSFCreate(PetscObjectComm((PetscObject) pc), &rankSF);CHKERRQ(ierr);
2484bbf5ea8SMatthew G. Knepley     ierr = PetscSFSetGraph(rankSF, 1, numRanks, NULL, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
2494bbf5ea8SMatthew G. Knepley     ierr = PetscSFSetUp(rankSF);CHKERRQ(ierr);
2504bbf5ea8SMatthew G. Knepley     /* OK, use it to communicate the root offset on the remote
2514bbf5ea8SMatthew G. Knepley      * processes for each subspace. */
2524bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(n, &offsets);CHKERRQ(ierr);
2534bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(n*numRanks, &remoteOffsets);CHKERRQ(ierr);
2544bbf5ea8SMatthew G. Knepley 
2554bbf5ea8SMatthew G. Knepley     offsets[0] = 0;
2564bbf5ea8SMatthew G. Knepley     for (i = 1; i < n; ++i) {
2574bbf5ea8SMatthew G. Knepley       PetscInt nroots;
2584bbf5ea8SMatthew G. Knepley 
2594bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetGraph(sf[i-1], &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
2604bbf5ea8SMatthew G. Knepley       offsets[i] = offsets[i-1] + nroots*bs[i-1];
2614bbf5ea8SMatthew G. Knepley     }
2624bbf5ea8SMatthew G. Knepley     /* Offsets are the offsets on the current process of the
2634bbf5ea8SMatthew G. Knepley      * global dof numbering for the subspaces. */
2644bbf5ea8SMatthew G. Knepley     ierr = MPI_Type_contiguous(n, MPIU_INT, &contig);CHKERRQ(ierr);
2654bbf5ea8SMatthew G. Knepley     ierr = MPI_Type_commit(&contig);CHKERRQ(ierr);
2664bbf5ea8SMatthew G. Knepley 
2674bbf5ea8SMatthew G. Knepley     ierr = PetscSFBcastBegin(rankSF, contig, offsets, remoteOffsets);CHKERRQ(ierr);
2684bbf5ea8SMatthew G. Knepley     ierr = PetscSFBcastEnd(rankSF, contig, offsets, remoteOffsets);CHKERRQ(ierr);
2694bbf5ea8SMatthew G. Knepley     ierr = MPI_Type_free(&contig);CHKERRQ(ierr);
2704bbf5ea8SMatthew G. Knepley     ierr = PetscFree(offsets);CHKERRQ(ierr);
2714bbf5ea8SMatthew G. Knepley     ierr = PetscSFDestroy(&rankSF);CHKERRQ(ierr);
2724bbf5ea8SMatthew G. Knepley     /* Now remoteOffsets contains the offsets on the remote
2734bbf5ea8SMatthew G. Knepley      * processes who communicate with me.  So now we can
2744bbf5ea8SMatthew G. Knepley      * concatenate the list of SFs into a single one. */
2754bbf5ea8SMatthew G. Knepley     index = 0;
2764bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2774bbf5ea8SMatthew G. Knepley       const PetscSFNode *remote = NULL;
2784bbf5ea8SMatthew G. Knepley       const PetscInt    *local  = NULL;
2794bbf5ea8SMatthew G. Knepley       PetscInt           nroots, nleaves, j;
2804bbf5ea8SMatthew G. Knepley 
2814bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetGraph(sf[i], &nroots, &nleaves, &local, &remote);CHKERRQ(ierr);
2824bbf5ea8SMatthew G. Knepley       for (j = 0; j < nleaves; ++j) {
2834bbf5ea8SMatthew G. Knepley         PetscInt rank = remote[j].rank;
2844bbf5ea8SMatthew G. Knepley         PetscInt idx, rootOffset, k;
2854bbf5ea8SMatthew G. Knepley 
2861b68eb51SMatthew G. Knepley         ierr = PetscHMapIGet(rankToIndex, rank, &idx);CHKERRQ(ierr);
2874bbf5ea8SMatthew G. Knepley         if (idx == -1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Didn't find rank, huh?");
2884bbf5ea8SMatthew G. Knepley         /* Offset on given rank for ith subspace */
2894bbf5ea8SMatthew G. Knepley         rootOffset = remoteOffsets[n*idx + i];
2904bbf5ea8SMatthew G. Knepley         for (k = 0; k < bs[i]; ++k) {
29173ec7555SLawrence Mitchell           ilocal[index]        = (local ? local[j] : j)*bs[i] + k + leafOffset;
2924bbf5ea8SMatthew G. Knepley           iremote[index].rank  = remote[j].rank;
2934bbf5ea8SMatthew G. Knepley           iremote[index].index = remote[j].index*bs[i] + k + rootOffset;
2944bbf5ea8SMatthew G. Knepley           ++index;
2954bbf5ea8SMatthew G. Knepley         }
2964bbf5ea8SMatthew G. Knepley       }
2974bbf5ea8SMatthew G. Knepley       leafOffset += nleaves * bs[i];
2984bbf5ea8SMatthew G. Knepley     }
2991b68eb51SMatthew G. Knepley     ierr = PetscHMapIDestroy(&rankToIndex);CHKERRQ(ierr);
3004bbf5ea8SMatthew G. Knepley     ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
3014bbf5ea8SMatthew G. Knepley     ierr = PetscSFCreate(PetscObjectComm((PetscObject)pc), &patch->defaultSF);CHKERRQ(ierr);
3024bbf5ea8SMatthew G. Knepley     ierr = PetscSFSetGraph(patch->defaultSF, allRoots, allLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER);CHKERRQ(ierr);
3034bbf5ea8SMatthew G. Knepley   }
3044bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3054bbf5ea8SMatthew G. Knepley }
3064bbf5ea8SMatthew G. Knepley 
3074bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3085f824522SMatthew G. Knepley PetscErrorCode PCPatchSetIgnoreDim(PC pc, PetscInt dim)
3095f824522SMatthew G. Knepley {
3105f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3115f824522SMatthew G. Knepley   PetscFunctionBegin;
3125f824522SMatthew G. Knepley   patch->ignoredim = dim;
3135f824522SMatthew G. Knepley   PetscFunctionReturn(0);
3145f824522SMatthew G. Knepley }
3155f824522SMatthew G. Knepley 
3165f824522SMatthew G. Knepley /* TODO: Docs */
3175f824522SMatthew G. Knepley PetscErrorCode PCPatchGetIgnoreDim(PC pc, PetscInt *dim)
3185f824522SMatthew G. Knepley {
3195f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3205f824522SMatthew G. Knepley   PetscFunctionBegin;
3215f824522SMatthew G. Knepley   *dim = patch->ignoredim;
3225f824522SMatthew G. Knepley   PetscFunctionReturn(0);
3235f824522SMatthew G. Knepley }
3245f824522SMatthew G. Knepley 
3255f824522SMatthew G. Knepley /* TODO: Docs */
3264bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetSaveOperators(PC pc, PetscBool flg)
3274bbf5ea8SMatthew G. Knepley {
3284bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3294bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3304bbf5ea8SMatthew G. Knepley   patch->save_operators = flg;
3314bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3324bbf5ea8SMatthew G. Knepley }
3334bbf5ea8SMatthew G. Knepley 
3344bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3354bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetSaveOperators(PC pc, PetscBool *flg)
3364bbf5ea8SMatthew G. Knepley {
3374bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3384bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3394bbf5ea8SMatthew G. Knepley   *flg = patch->save_operators;
3404bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3414bbf5ea8SMatthew G. Knepley }
3424bbf5ea8SMatthew G. Knepley 
3434bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3444bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetPartitionOfUnity(PC pc, PetscBool flg)
3454bbf5ea8SMatthew G. Knepley {
3464bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3474bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3484bbf5ea8SMatthew G. Knepley   patch->partition_of_unity = flg;
3494bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3504bbf5ea8SMatthew G. Knepley }
3514bbf5ea8SMatthew G. Knepley 
3524bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3534bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetPartitionOfUnity(PC pc, PetscBool *flg)
3544bbf5ea8SMatthew G. Knepley {
3554bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3564bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3574bbf5ea8SMatthew G. Knepley   *flg = patch->partition_of_unity;
3584bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3594bbf5ea8SMatthew G. Knepley }
3604bbf5ea8SMatthew G. Knepley 
3614bbf5ea8SMatthew G. Knepley /* TODO: Docs */
36261c4b389SFlorian Wechsung PetscErrorCode PCPatchSetLocalComposition(PC pc, PCCompositeType type)
363c2e6f3c0SFlorian Wechsung {
364c2e6f3c0SFlorian Wechsung   PC_PATCH *patch = (PC_PATCH *) pc->data;
365c2e6f3c0SFlorian Wechsung   PetscFunctionBegin;
36661c4b389SFlorian Wechsung   if (type != PC_COMPOSITE_ADDITIVE && type != PC_COMPOSITE_MULTIPLICATIVE) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Only supports additive or multiplicative as the local type");
36761c4b389SFlorian Wechsung   patch->local_composition_type = type;
368c2e6f3c0SFlorian Wechsung   PetscFunctionReturn(0);
369c2e6f3c0SFlorian Wechsung }
370c2e6f3c0SFlorian Wechsung 
371c2e6f3c0SFlorian Wechsung /* TODO: Docs */
37261c4b389SFlorian Wechsung PetscErrorCode PCPatchGetLocalComposition(PC pc, PCCompositeType *type)
373c2e6f3c0SFlorian Wechsung {
374c2e6f3c0SFlorian Wechsung   PC_PATCH *patch = (PC_PATCH *) pc->data;
375c2e6f3c0SFlorian Wechsung   PetscFunctionBegin;
37661c4b389SFlorian Wechsung   *type = patch->local_composition_type;
377c2e6f3c0SFlorian Wechsung   PetscFunctionReturn(0);
378c2e6f3c0SFlorian Wechsung }
379c2e6f3c0SFlorian Wechsung 
380c2e6f3c0SFlorian Wechsung /* TODO: Docs */
3814bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetSubMatType(PC pc, MatType sub_mat_type)
3824bbf5ea8SMatthew G. Knepley {
3834bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
3844bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
3854bbf5ea8SMatthew G. Knepley 
3864bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3874bbf5ea8SMatthew G. Knepley   if (patch->sub_mat_type) {ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr);}
3884bbf5ea8SMatthew G. Knepley   ierr = PetscStrallocpy(sub_mat_type, (char **) &patch->sub_mat_type);CHKERRQ(ierr);
3894bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3904bbf5ea8SMatthew G. Knepley }
3914bbf5ea8SMatthew G. Knepley 
3924bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3934bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetSubMatType(PC pc, MatType *sub_mat_type)
3944bbf5ea8SMatthew G. Knepley {
3954bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3964bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3974bbf5ea8SMatthew G. Knepley   *sub_mat_type = patch->sub_mat_type;
3984bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3994bbf5ea8SMatthew G. Knepley }
4004bbf5ea8SMatthew G. Knepley 
4014bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4024bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetCellNumbering(PC pc, PetscSection cellNumbering)
4034bbf5ea8SMatthew G. Knepley {
4044bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
4054bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
4064bbf5ea8SMatthew G. Knepley 
4074bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4084bbf5ea8SMatthew G. Knepley   patch->cellNumbering = cellNumbering;
4094bbf5ea8SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) cellNumbering);CHKERRQ(ierr);
4104bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4114bbf5ea8SMatthew G. Knepley }
4124bbf5ea8SMatthew G. Knepley 
4134bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4144bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetCellNumbering(PC pc, PetscSection *cellNumbering)
4154bbf5ea8SMatthew G. Knepley {
4164bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4174bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4184bbf5ea8SMatthew G. Knepley   *cellNumbering = patch->cellNumbering;
4194bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4204bbf5ea8SMatthew G. Knepley }
4214bbf5ea8SMatthew G. Knepley 
4224bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4234bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetConstructType(PC pc, PCPatchConstructType ctype, PetscErrorCode (*func)(PC, PetscInt *, IS **, IS *, void *), void *ctx)
4244bbf5ea8SMatthew G. Knepley {
4254bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4264bbf5ea8SMatthew G. Knepley 
4274bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4284bbf5ea8SMatthew G. Knepley   patch->ctype = ctype;
4294bbf5ea8SMatthew G. Knepley   switch (ctype) {
4304bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
43140c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
4324bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Star;
4334bbf5ea8SMatthew G. Knepley     break;
4344bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
43540c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
4364bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Vanka;
4374bbf5ea8SMatthew G. Knepley     break;
438e5b9877fSPatrick Farrell   case PC_PATCH_PARDECOMP:
4390a390943SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
440e5b9877fSPatrick Farrell     patch->patchconstructop = PCPatchConstruct_Pardecomp;
4410a390943SPatrick Farrell     break;
4424bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
4434bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
4444bbf5ea8SMatthew G. Knepley     patch->user_patches     = PETSC_TRUE;
4454bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_User;
446bdd9e0cdSPatrick Farrell     if (func) {
4474bbf5ea8SMatthew G. Knepley       patch->userpatchconstructionop = func;
4484bbf5ea8SMatthew G. Knepley       patch->userpatchconstructctx   = ctx;
449bdd9e0cdSPatrick Farrell     }
4504bbf5ea8SMatthew G. Knepley     break;
4514bbf5ea8SMatthew G. Knepley   default:
4524bbf5ea8SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype);
4534bbf5ea8SMatthew G. Knepley   }
4544bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4554bbf5ea8SMatthew G. Knepley }
4564bbf5ea8SMatthew G. Knepley 
4574bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4584bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetConstructType(PC pc, PCPatchConstructType *ctype, PetscErrorCode (**func)(PC, PetscInt *, IS **, IS *, void *), void **ctx)
4594bbf5ea8SMatthew G. Knepley {
4604bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4614bbf5ea8SMatthew G. Knepley 
4624bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4634bbf5ea8SMatthew G. Knepley   *ctype = patch->ctype;
4644bbf5ea8SMatthew G. Knepley   switch (patch->ctype) {
4654bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
4664bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
467e5b9877fSPatrick Farrell   case PC_PATCH_PARDECOMP:
4684bbf5ea8SMatthew G. Knepley     break;
4694bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
4704bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
4714bbf5ea8SMatthew G. Knepley     *func = patch->userpatchconstructionop;
4724bbf5ea8SMatthew G. Knepley     *ctx  = patch->userpatchconstructctx;
4734bbf5ea8SMatthew G. Knepley     break;
4744bbf5ea8SMatthew G. Knepley   default:
4754bbf5ea8SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype);
4764bbf5ea8SMatthew G. Knepley   }
4774bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4784bbf5ea8SMatthew G. Knepley }
4794bbf5ea8SMatthew G. Knepley 
4804bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4814bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfo(PC pc, PetscInt nsubspaces, DM *dms, PetscInt *bs, PetscInt *nodesPerCell, const PetscInt **cellNodeMap,
4824bbf5ea8SMatthew G. Knepley                                             const PetscInt *subspaceOffsets, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
4834bbf5ea8SMatthew G. Knepley {
4844bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
4855f824522SMatthew G. Knepley   DM             dm;
4864bbf5ea8SMatthew G. Knepley   PetscSF       *sfs;
4875f824522SMatthew G. Knepley   PetscInt       cStart, cEnd, i, j;
4884bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
4894bbf5ea8SMatthew G. Knepley 
4904bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4915f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
4925f824522SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
4934bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &sfs);CHKERRQ(ierr);
4944bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->dofSection);CHKERRQ(ierr);
4954bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->bs);CHKERRQ(ierr);
4964bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->nodesPerCell);CHKERRQ(ierr);
4974bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->cellNodeMap);CHKERRQ(ierr);
4984bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces+1, &patch->subspaceOffsets);CHKERRQ(ierr);
4994bbf5ea8SMatthew G. Knepley 
5004bbf5ea8SMatthew G. Knepley   patch->nsubspaces       = nsubspaces;
5014bbf5ea8SMatthew G. Knepley   patch->totalDofsPerCell = 0;
5024bbf5ea8SMatthew G. Knepley   for (i = 0; i < nsubspaces; ++i) {
5034bbf5ea8SMatthew G. Knepley     ierr = DMGetDefaultSection(dms[i], &patch->dofSection[i]);CHKERRQ(ierr);
5044bbf5ea8SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) patch->dofSection[i]);CHKERRQ(ierr);
5054bbf5ea8SMatthew G. Knepley     ierr = DMGetDefaultSF(dms[i], &sfs[i]);CHKERRQ(ierr);
5064bbf5ea8SMatthew G. Knepley     patch->bs[i]              = bs[i];
5074bbf5ea8SMatthew G. Knepley     patch->nodesPerCell[i]    = nodesPerCell[i];
5084bbf5ea8SMatthew G. Knepley     patch->totalDofsPerCell  += nodesPerCell[i]*bs[i];
50980e8a965SFlorian Wechsung     ierr = PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i]);CHKERRQ(ierr);
51080e8a965SFlorian Wechsung     for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
5114bbf5ea8SMatthew G. Knepley     patch->subspaceOffsets[i] = subspaceOffsets[i];
5124bbf5ea8SMatthew G. Knepley   }
5134bbf5ea8SMatthew G. Knepley   ierr = PCPatchCreateDefaultSF_Private(pc, nsubspaces, sfs, patch->bs);CHKERRQ(ierr);
5144bbf5ea8SMatthew G. Knepley   ierr = PetscFree(sfs);CHKERRQ(ierr);
5154bbf5ea8SMatthew G. Knepley 
5164bbf5ea8SMatthew G. Knepley   patch->subspaceOffsets[nsubspaces] = subspaceOffsets[nsubspaces];
5174bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes);CHKERRQ(ierr);
5184bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes);CHKERRQ(ierr);
5194bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5204bbf5ea8SMatthew G. Knepley }
5214bbf5ea8SMatthew G. Knepley 
5224bbf5ea8SMatthew G. Knepley /* TODO: Docs */
5235f824522SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfoCombined(PC pc, DM dm, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
5245f824522SMatthew G. Knepley {
5255f824522SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
5265f824522SMatthew G. Knepley   PetscInt       cStart, cEnd, i, j;
5275f824522SMatthew G. Knepley   PetscErrorCode ierr;
5285f824522SMatthew G. Knepley 
5295f824522SMatthew G. Knepley   PetscFunctionBegin;
5305f824522SMatthew G. Knepley   patch->combined = PETSC_TRUE;
5315f824522SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
5325f824522SMatthew G. Knepley   ierr = DMGetNumFields(dm, &patch->nsubspaces);CHKERRQ(ierr);
5335f824522SMatthew G. Knepley   ierr = PetscCalloc1(patch->nsubspaces, &patch->dofSection);CHKERRQ(ierr);
5345f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->bs);CHKERRQ(ierr);
5355f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->nodesPerCell);CHKERRQ(ierr);
5365f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->cellNodeMap);CHKERRQ(ierr);
5375f824522SMatthew G. Knepley   ierr = PetscCalloc1(patch->nsubspaces+1, &patch->subspaceOffsets);CHKERRQ(ierr);
5385f824522SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &patch->dofSection[0]);CHKERRQ(ierr);
5395f824522SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) patch->dofSection[0]);CHKERRQ(ierr);
5405f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(patch->dofSection[0], &patch->subspaceOffsets[patch->nsubspaces]);CHKERRQ(ierr);
5415f824522SMatthew G. Knepley   patch->totalDofsPerCell = 0;
5425f824522SMatthew G. Knepley   for (i = 0; i < patch->nsubspaces; ++i) {
5435f824522SMatthew G. Knepley     patch->bs[i]             = 1;
5445f824522SMatthew G. Knepley     patch->nodesPerCell[i]   = nodesPerCell[i];
5455f824522SMatthew G. Knepley     patch->totalDofsPerCell += nodesPerCell[i];
5465f824522SMatthew G. Knepley     ierr = PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i]);CHKERRQ(ierr);
5475f824522SMatthew G. Knepley     for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
5485f824522SMatthew G. Knepley   }
5495f824522SMatthew G. Knepley   ierr = DMGetDefaultSF(dm, &patch->defaultSF);CHKERRQ(ierr);
5505f824522SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) patch->defaultSF);CHKERRQ(ierr);
5515f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes);CHKERRQ(ierr);
5525f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes);CHKERRQ(ierr);
5535f824522SMatthew G. Knepley   PetscFunctionReturn(0);
5545f824522SMatthew G. Knepley }
5555f824522SMatthew G. Knepley 
5565f824522SMatthew G. Knepley /*@C
5575f824522SMatthew G. Knepley 
55892d50984SMatthew G. Knepley   PCPatchSetComputeFunction - Set the callback used to compute patch residuals
55992d50984SMatthew G. Knepley 
56092d50984SMatthew G. Knepley   Input Parameters:
56192d50984SMatthew G. Knepley + pc   - The PC
56292d50984SMatthew G. Knepley . func - The callback
56392d50984SMatthew G. Knepley - ctx  - The user context
56492d50984SMatthew G. Knepley 
56592d50984SMatthew G. Knepley   Level: advanced
56692d50984SMatthew G. Knepley 
56792d50984SMatthew G. Knepley   Note:
56892d50984SMatthew G. Knepley   The callback has signature:
56992d50984SMatthew G. Knepley +  usercomputef(pc, point, x, f, cellIS, n, u, ctx)
57092d50984SMatthew G. Knepley +  pc     - The PC
57192d50984SMatthew G. Knepley +  point  - The point
57292d50984SMatthew G. Knepley +  x      - The input solution (not used in linear problems)
57392d50984SMatthew G. Knepley +  f      - The patch residual vector
57492d50984SMatthew G. Knepley +  cellIS - An array of the cell numbers
57592d50984SMatthew G. Knepley +  n      - The size of g2l
57692d50984SMatthew G. Knepley +  g2l    - The global to local dof translation table
57792d50984SMatthew G. Knepley +  ctx    - The user context
57892d50984SMatthew G. Knepley   and can assume that the matrix entries have been set to zero before the call.
57992d50984SMatthew G. Knepley 
58092d50984SMatthew G. Knepley .seealso: PCPatchSetComputeOperator(), PCPatchGetComputeOperator(), PCPatchSetDiscretisationInfo()
58192d50984SMatthew G. Knepley @*/
58239fd2e8aSPatrick Farrell PetscErrorCode PCPatchSetComputeFunction(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Vec, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
58392d50984SMatthew G. Knepley {
58492d50984SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
58592d50984SMatthew G. Knepley 
58692d50984SMatthew G. Knepley   PetscFunctionBegin;
58792d50984SMatthew G. Knepley   patch->usercomputef    = func;
58892d50984SMatthew G. Knepley   patch->usercomputefctx = ctx;
58992d50984SMatthew G. Knepley   PetscFunctionReturn(0);
59092d50984SMatthew G. Knepley }
59192d50984SMatthew G. Knepley 
59292d50984SMatthew G. Knepley /*@C
59392d50984SMatthew G. Knepley 
5945f824522SMatthew G. Knepley   PCPatchSetComputeOperator - Set the callback used to compute patch matrices
5955f824522SMatthew G. Knepley 
5965f824522SMatthew G. Knepley   Input Parameters:
5975f824522SMatthew G. Knepley + pc   - The PC
5985f824522SMatthew G. Knepley . func - The callback
5995f824522SMatthew G. Knepley - ctx  - The user context
6005f824522SMatthew G. Knepley 
6015f824522SMatthew G. Knepley   Level: advanced
6025f824522SMatthew G. Knepley 
6035f824522SMatthew G. Knepley   Note:
6045f824522SMatthew G. Knepley   The callback has signature:
605723f9013SMatthew G. Knepley +  usercomputeop(pc, point, x, mat, cellIS, n, u, ctx)
6065f824522SMatthew G. Knepley +  pc     - The PC
607bdd9e0cdSPatrick Farrell +  point  - The point
608723f9013SMatthew G. Knepley +  x      - The input solution (not used in linear problems)
6095f824522SMatthew G. Knepley +  mat    - The patch matrix
6106f158342SMatthew G. Knepley +  cellIS - An array of the cell numbers
6115f824522SMatthew G. Knepley +  n      - The size of g2l
6125f824522SMatthew G. Knepley +  g2l    - The global to local dof translation table
6135f824522SMatthew G. Knepley +  ctx    - The user context
6145f824522SMatthew G. Knepley   and can assume that the matrix entries have been set to zero before the call.
6155f824522SMatthew G. Knepley 
616723f9013SMatthew G. Knepley .seealso: PCPatchGetComputeOperator(), PCPatchSetComputeFunction(), PCPatchSetDiscretisationInfo()
6175f824522SMatthew G. Knepley @*/
6184d04e9f1SPatrick Farrell PetscErrorCode PCPatchSetComputeOperator(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Mat, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
6194bbf5ea8SMatthew G. Knepley {
6204bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
6214bbf5ea8SMatthew G. Knepley 
6224bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
6234bbf5ea8SMatthew G. Knepley   patch->usercomputeop    = func;
624723f9013SMatthew G. Knepley   patch->usercomputeopctx = ctx;
6254bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
6264bbf5ea8SMatthew G. Knepley }
6274bbf5ea8SMatthew G. Knepley 
6284bbf5ea8SMatthew G. Knepley /* On entry, ht contains the topological entities whose dofs we are responsible for solving for;
6294bbf5ea8SMatthew G. Knepley    on exit, cht contains all the topological entities we need to compute their residuals.
6304bbf5ea8SMatthew G. Knepley    In full generality this should incorporate knowledge of the sparsity pattern of the matrix;
6314bbf5ea8SMatthew G. Knepley    here we assume a standard FE sparsity pattern.*/
6324bbf5ea8SMatthew G. Knepley /* TODO: Use DMPlexGetAdjacency() */
6331b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchCompleteCellPatch(PC pc, PetscHSetI ht, PetscHSetI cht)
6344bbf5ea8SMatthew G. Knepley {
6355f824522SMatthew G. Knepley   DM             dm;
6361b68eb51SMatthew G. Knepley   PetscHashIter  hi;
6374bbf5ea8SMatthew G. Knepley   PetscInt       point;
6384bbf5ea8SMatthew G. Knepley   PetscInt      *star = NULL, *closure = NULL;
6394c954380SMatthew G. Knepley   PetscInt       ignoredim, iStart = 0, iEnd = -1, starSize, closureSize, si, ci;
6404bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
6414bbf5ea8SMatthew G. Knepley 
6424bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
6435f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
6445f824522SMatthew G. Knepley   ierr = PCPatchGetIgnoreDim(pc, &ignoredim);CHKERRQ(ierr);
6455f824522SMatthew G. Knepley   if (ignoredim >= 0) {ierr = DMPlexGetDepthStratum(dm, ignoredim, &iStart, &iEnd);CHKERRQ(ierr);}
6461b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(cht);CHKERRQ(ierr);
6471b68eb51SMatthew G. Knepley   PetscHashIterBegin(ht, hi);
6481b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(ht, hi)) {
6494c954380SMatthew G. Knepley 
6501b68eb51SMatthew G. Knepley     PetscHashIterGetKey(ht, hi, point);
6511b68eb51SMatthew G. Knepley     PetscHashIterNext(ht, hi);
6524bbf5ea8SMatthew G. Knepley 
6534bbf5ea8SMatthew G. Knepley     /* Loop over all the cells that this point connects to */
6544bbf5ea8SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
6555f824522SMatthew G. Knepley     for (si = 0; si < starSize*2; si += 2) {
6564c954380SMatthew G. Knepley       const PetscInt ownedpoint = star[si];
6575f824522SMatthew G. Knepley       /* TODO Check for point in cht before running through closure again */
6584bbf5ea8SMatthew G. Knepley       /* now loop over all entities in the closure of that cell */
6594bbf5ea8SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, ownedpoint, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
6605f824522SMatthew G. Knepley       for (ci = 0; ci < closureSize*2; ci += 2) {
6614c954380SMatthew G. Knepley         const PetscInt seenpoint = closure[ci];
6625f824522SMatthew G. Knepley         if (ignoredim >= 0 && seenpoint >= iStart && seenpoint < iEnd) continue;
6631b68eb51SMatthew G. Knepley         ierr = PetscHSetIAdd(cht, seenpoint);CHKERRQ(ierr);
6644bbf5ea8SMatthew G. Knepley       }
6652d76c0eeSPatrick Farrell       ierr = DMPlexRestoreTransitiveClosure(dm, ownedpoint, PETSC_TRUE, NULL, &closure);CHKERRQ(ierr);
6664bbf5ea8SMatthew G. Knepley     }
6672d76c0eeSPatrick Farrell     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, NULL, &star);CHKERRQ(ierr);
6684bbf5ea8SMatthew G. Knepley   }
6695f824522SMatthew G. Knepley   PetscFunctionReturn(0);
6705f824522SMatthew G. Knepley }
6715f824522SMatthew G. Knepley 
6725f824522SMatthew G. Knepley static PetscErrorCode PCPatchGetGlobalDofs(PC pc, PetscSection dofSection[], PetscInt f, PetscBool combined, PetscInt p, PetscInt *dof, PetscInt *off)
6735f824522SMatthew G. Knepley {
6745f824522SMatthew G. Knepley   PetscErrorCode ierr;
6755f824522SMatthew G. Knepley 
6765f824522SMatthew G. Knepley   PetscFunctionBegin;
6775f824522SMatthew G. Knepley   if (combined) {
6785f824522SMatthew G. Knepley     if (f < 0) {
6795f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetDof(dofSection[0], p, dof);CHKERRQ(ierr);}
6805f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetOffset(dofSection[0], p, off);CHKERRQ(ierr);}
6815f824522SMatthew G. Knepley     } else {
6825f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetFieldDof(dofSection[0], p, f, dof);CHKERRQ(ierr);}
6835f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetFieldOffset(dofSection[0], p, f, off);CHKERRQ(ierr);}
6845f824522SMatthew G. Knepley     }
6855f824522SMatthew G. Knepley   } else {
6865f824522SMatthew G. Knepley     if (f < 0) {
6875f824522SMatthew G. Knepley       PC_PATCH *patch = (PC_PATCH *) pc->data;
6885f824522SMatthew G. Knepley       PetscInt  fdof, g;
6895f824522SMatthew G. Knepley 
6905f824522SMatthew G. Knepley       if (dof) {
6915f824522SMatthew G. Knepley         *dof = 0;
6925f824522SMatthew G. Knepley         for (g = 0; g < patch->nsubspaces; ++g) {
6935f824522SMatthew G. Knepley           ierr = PetscSectionGetDof(dofSection[g], p, &fdof);CHKERRQ(ierr);
6945f824522SMatthew G. Knepley           *dof += fdof;
6955f824522SMatthew G. Knepley         }
6965f824522SMatthew G. Knepley       }
697624e31c3SLawrence Mitchell       if (off) {
698624e31c3SLawrence Mitchell         *off = 0;
699624e31c3SLawrence Mitchell         for (g = 0; g < patch->nsubspaces; ++g) {
700624e31c3SLawrence Mitchell           ierr = PetscSectionGetOffset(dofSection[g], p, &fdof);CHKERRQ(ierr);
701624e31c3SLawrence Mitchell           *off += fdof;
702624e31c3SLawrence Mitchell         }
703624e31c3SLawrence Mitchell       }
7045f824522SMatthew G. Knepley     } else {
7055f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetDof(dofSection[f], p, dof);CHKERRQ(ierr);}
7065f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetOffset(dofSection[f], p, off);CHKERRQ(ierr);}
7075f824522SMatthew G. Knepley     }
7085f824522SMatthew G. Knepley   }
7094bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
7104bbf5ea8SMatthew G. Knepley }
7114bbf5ea8SMatthew G. Knepley 
7124bbf5ea8SMatthew G. Knepley /* Given a hash table with a set of topological entities (pts), compute the degrees of
7134bbf5ea8SMatthew G. Knepley    freedom in global concatenated numbering on those entities.
7144bbf5ea8SMatthew G. Knepley    For Vanka smoothing, this needs to do something special: ignore dofs of the
7154bbf5ea8SMatthew G. Knepley    constraint subspace on entities that aren't the base entity we're building the patch
7164bbf5ea8SMatthew G. Knepley    around. */
717e4c66b91SPatrick Farrell static PetscErrorCode PCPatchGetPointDofs(PC pc, PetscHSetI pts, PetscHSetI dofs, PetscInt base, PetscHSetI* subspaces_to_exclude)
7184bbf5ea8SMatthew G. Knepley {
7195f824522SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
7201b68eb51SMatthew G. Knepley   PetscHashIter  hi;
7214bbf5ea8SMatthew G. Knepley   PetscInt       ldof, loff;
7224bbf5ea8SMatthew G. Knepley   PetscInt       k, p;
7234bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
7244bbf5ea8SMatthew G. Knepley 
7254bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
7261b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(dofs);CHKERRQ(ierr);
7274bbf5ea8SMatthew G. Knepley   for (k = 0; k < patch->nsubspaces; ++k) {
7284bbf5ea8SMatthew G. Knepley     PetscInt subspaceOffset = patch->subspaceOffsets[k];
7294bbf5ea8SMatthew G. Knepley     PetscInt bs             = patch->bs[k];
7304bbf5ea8SMatthew G. Knepley     PetscInt j, l;
7314bbf5ea8SMatthew G. Knepley 
732e4c66b91SPatrick Farrell     if (subspaces_to_exclude != NULL) {
733e4c66b91SPatrick Farrell       PetscBool should_exclude_k = PETSC_FALSE;
734e4c66b91SPatrick Farrell       PetscHSetIHas(*subspaces_to_exclude, k, &should_exclude_k);
735e4c66b91SPatrick Farrell       if (should_exclude_k) {
7364bbf5ea8SMatthew G. Knepley         /* only get this subspace dofs at the base entity, not any others */
7375f824522SMatthew G. Knepley         ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, base, &ldof, &loff);CHKERRQ(ierr);
7384bbf5ea8SMatthew G. Knepley         if (0 == ldof) continue;
7394bbf5ea8SMatthew G. Knepley         for (j = loff; j < ldof + loff; ++j) {
7404bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
7414bbf5ea8SMatthew G. Knepley             PetscInt dof = bs*j + l + subspaceOffset;
7421b68eb51SMatthew G. Knepley             ierr = PetscHSetIAdd(dofs, dof);CHKERRQ(ierr);
7434bbf5ea8SMatthew G. Knepley           }
7444bbf5ea8SMatthew G. Knepley         }
7454bbf5ea8SMatthew G. Knepley         continue; /* skip the other dofs of this subspace */
7464bbf5ea8SMatthew G. Knepley       }
747e4c66b91SPatrick Farrell     }
7484bbf5ea8SMatthew G. Knepley 
7491b68eb51SMatthew G. Knepley     PetscHashIterBegin(pts, hi);
7501b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(pts, hi)) {
7511b68eb51SMatthew G. Knepley       PetscHashIterGetKey(pts, hi, p);
7521b68eb51SMatthew G. Knepley       PetscHashIterNext(pts, hi);
7535f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, p, &ldof, &loff);CHKERRQ(ierr);
7544bbf5ea8SMatthew G. Knepley       if (0 == ldof) continue;
7554bbf5ea8SMatthew G. Knepley       for (j = loff; j < ldof + loff; ++j) {
7564bbf5ea8SMatthew G. Knepley         for (l = 0; l < bs; ++l) {
7574bbf5ea8SMatthew G. Knepley           PetscInt dof = bs*j + l + subspaceOffset;
7581b68eb51SMatthew G. Knepley           ierr = PetscHSetIAdd(dofs, dof);CHKERRQ(ierr);
7594bbf5ea8SMatthew G. Knepley         }
7604bbf5ea8SMatthew G. Knepley       }
7614bbf5ea8SMatthew G. Knepley     }
7624bbf5ea8SMatthew G. Knepley   }
7634bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
7644bbf5ea8SMatthew G. Knepley }
7654bbf5ea8SMatthew G. Knepley 
7664bbf5ea8SMatthew G. Knepley /* Given two hash tables A and B, compute the keys in B that are not in A, and put them in C */
7671b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchComputeSetDifference_Private(PetscHSetI A, PetscHSetI B, PetscHSetI C)
7684bbf5ea8SMatthew G. Knepley {
7691b68eb51SMatthew G. Knepley   PetscHashIter  hi;
7701b68eb51SMatthew G. Knepley   PetscInt       key;
7714bbf5ea8SMatthew G. Knepley   PetscBool      flg;
7721b68eb51SMatthew G. Knepley   PetscErrorCode ierr;
7734bbf5ea8SMatthew G. Knepley 
7744bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
7751b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(C);CHKERRQ(ierr);
7761b68eb51SMatthew G. Knepley   PetscHashIterBegin(B, hi);
7771b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(B, hi)) {
7781b68eb51SMatthew G. Knepley     PetscHashIterGetKey(B, hi, key);
7791b68eb51SMatthew G. Knepley     PetscHashIterNext(B, hi);
7801b68eb51SMatthew G. Knepley     ierr = PetscHSetIHas(A, key, &flg);CHKERRQ(ierr);
7811b68eb51SMatthew G. Knepley     if (!flg) {ierr = PetscHSetIAdd(C, key);CHKERRQ(ierr);}
7824bbf5ea8SMatthew G. Knepley   }
7834bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
7844bbf5ea8SMatthew G. Knepley }
7854bbf5ea8SMatthew G. Knepley 
7864bbf5ea8SMatthew G. Knepley /*
7874bbf5ea8SMatthew G. Knepley  * PCPatchCreateCellPatches - create patches.
7884bbf5ea8SMatthew G. Knepley  *
7894bbf5ea8SMatthew G. Knepley  * Input Parameters:
7904bbf5ea8SMatthew G. Knepley  * + dm - The DMPlex object defining the mesh
7914bbf5ea8SMatthew G. Knepley  *
7924bbf5ea8SMatthew G. Knepley  * Output Parameters:
7934bbf5ea8SMatthew G. Knepley  * + cellCounts  - Section with counts of cells around each vertex
7945f824522SMatthew G. Knepley  * . cells       - IS of the cell point indices of cells in each patch
7955f824522SMatthew G. Knepley  * . pointCounts - Section with counts of cells around each vertex
7965f824522SMatthew G. Knepley  * - point       - IS of the cell point indices of cells in each patch
7974bbf5ea8SMatthew G. Knepley  */
7984bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatches(PC pc)
7994bbf5ea8SMatthew G. Knepley {
8004bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
8015f824522SMatthew G. Knepley   DMLabel         ghost = NULL;
8024bbf5ea8SMatthew G. Knepley   DM              dm, plex;
8031b68eb51SMatthew G. Knepley   PetscHSetI      ht, cht;
8040e126c0bSLawrence Mitchell   PetscSection    cellCounts,  pointCounts, intFacetCounts, extFacetCounts;
805*eb62eeaaSLawrence Mitchell   PetscInt       *cellsArray, *pointsArray, *intFacetsArray, *extFacetsArray, *intFacetsToPatchCell;
8060e126c0bSLawrence Mitchell   PetscInt        numCells, numPoints, numIntFacets, numExtFacets;
8075f824522SMatthew G. Knepley   const PetscInt *leaves;
8085f824522SMatthew G. Knepley   PetscInt        nleaves, pStart, pEnd, cStart, cEnd, vStart, vEnd, v;
8095f824522SMatthew G. Knepley   PetscBool       isFiredrake;
8104bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
8114bbf5ea8SMatthew G. Knepley 
8124bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
8134bbf5ea8SMatthew G. Knepley   /* Used to keep track of the cells in the patch. */
8141b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&ht);CHKERRQ(ierr);
8151b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&cht);CHKERRQ(ierr);
8164bbf5ea8SMatthew G. Knepley 
8174bbf5ea8SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
8184bbf5ea8SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONGSTATE, "DM not yet set on patch PC\n");
8194bbf5ea8SMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
8204bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetChart(plex, &pStart, &pEnd);CHKERRQ(ierr);
8214bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr);
8224bbf5ea8SMatthew G. Knepley 
8234bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
8245f824522SMatthew G. Knepley     ierr = patch->userpatchconstructionop(pc, &patch->npatch, &patch->userIS, &patch->iterationSet, patch->userpatchconstructctx);CHKERRQ(ierr);
8255f824522SMatthew G. Knepley     vStart = 0; vEnd = patch->npatch;
826e5b9877fSPatrick Farrell   } else if (patch->ctype == PC_PATCH_PARDECOMP) {
8270a390943SPatrick Farrell     vStart = 0; vEnd = 1;
8285f824522SMatthew G. Knepley   } else if (patch->codim < 0) {
8295f824522SMatthew G. Knepley     if (patch->dim < 0) {ierr = DMPlexGetDepthStratum(plex,  0,            &vStart, &vEnd);CHKERRQ(ierr);}
8305f824522SMatthew G. Knepley     else                {ierr = DMPlexGetDepthStratum(plex,  patch->dim,   &vStart, &vEnd);CHKERRQ(ierr);}
8315f824522SMatthew G. Knepley   } else                {ierr = DMPlexGetHeightStratum(plex, patch->codim, &vStart, &vEnd);CHKERRQ(ierr);}
8325f824522SMatthew G. Knepley   patch->npatch = vEnd - vStart;
8334bbf5ea8SMatthew G. Knepley 
8344bbf5ea8SMatthew G. Knepley   /* These labels mark the owned points.  We only create patches around points that this process owns. */
8355f824522SMatthew G. Knepley   ierr = DMHasLabel(dm, "pyop2_ghost", &isFiredrake);CHKERRQ(ierr);
8365f824522SMatthew G. Knepley   if (isFiredrake) {
8374bbf5ea8SMatthew G. Knepley     ierr = DMGetLabel(dm, "pyop2_ghost", &ghost);CHKERRQ(ierr);
8384bbf5ea8SMatthew G. Knepley     ierr = DMLabelCreateIndex(ghost, pStart, pEnd);CHKERRQ(ierr);
8395f824522SMatthew G. Knepley   } else {
8405f824522SMatthew G. Knepley     PetscSF sf;
8415f824522SMatthew G. Knepley 
8425f824522SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
8435f824522SMatthew G. Knepley     ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
8445f824522SMatthew G. Knepley     nleaves = PetscMax(nleaves, 0);
8455f824522SMatthew G. Knepley   }
8464bbf5ea8SMatthew G. Knepley 
8474bbf5ea8SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->cellCounts);CHKERRQ(ierr);
8485f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->cellCounts, "Patch Cell Layout");CHKERRQ(ierr);
8494bbf5ea8SMatthew G. Knepley   cellCounts = patch->cellCounts;
8504bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetChart(cellCounts, vStart, vEnd);CHKERRQ(ierr);
8515f824522SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->pointCounts);CHKERRQ(ierr);
8525f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->pointCounts, "Patch Point Layout");CHKERRQ(ierr);
8535f824522SMatthew G. Knepley   pointCounts = patch->pointCounts;
8545f824522SMatthew G. Knepley   ierr = PetscSectionSetChart(pointCounts, vStart, vEnd);CHKERRQ(ierr);
8550e126c0bSLawrence Mitchell   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->extFacetCounts);CHKERRQ(ierr);
8560e126c0bSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->extFacetCounts, "Patch Exterior Facet Layout");CHKERRQ(ierr);
8570e126c0bSLawrence Mitchell   extFacetCounts = patch->extFacetCounts;
8580e126c0bSLawrence Mitchell   ierr = PetscSectionSetChart(extFacetCounts, vStart, vEnd);CHKERRQ(ierr);
8590e126c0bSLawrence Mitchell   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->intFacetCounts);CHKERRQ(ierr);
8600e126c0bSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->intFacetCounts, "Patch Interior Facet Layout");CHKERRQ(ierr);
8610e126c0bSLawrence Mitchell   intFacetCounts = patch->intFacetCounts;
8620e126c0bSLawrence Mitchell   ierr = PetscSectionSetChart(intFacetCounts, vStart, vEnd);CHKERRQ(ierr);
8635f824522SMatthew G. Knepley   /* Count cells and points in the patch surrounding each entity */
8640e126c0bSLawrence Mitchell   PetscInt       fStart, fEnd;
8650e126c0bSLawrence Mitchell   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
8664bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
8671b68eb51SMatthew G. Knepley     PetscHashIter hi;
8685f824522SMatthew G. Knepley     PetscInt       chtSize, loc = -1;
8695f824522SMatthew G. Knepley     PetscBool      flg;
8704bbf5ea8SMatthew G. Knepley 
871b525f888SPatrick Farrell     if (!patch->user_patches && patch->ctype != PC_PATCH_PARDECOMP) {
8725f824522SMatthew G. Knepley       if (ghost) {ierr = DMLabelHasPoint(ghost, v, &flg);CHKERRQ(ierr);}
873928bb9adSStefano Zampini       else       {ierr = PetscFindInt(v, nleaves, leaves, &loc);CHKERRQ(ierr); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;}
8744bbf5ea8SMatthew G. Knepley       /* Not an owned entity, don't make a cell patch. */
8754bbf5ea8SMatthew G. Knepley       if (flg) continue;
8764bbf5ea8SMatthew G. Knepley     }
8774bbf5ea8SMatthew G. Knepley 
8784bbf5ea8SMatthew G. Knepley     ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr);
8795f824522SMatthew G. Knepley     ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr);
8801b68eb51SMatthew G. Knepley     ierr = PetscHSetIGetSize(cht, &chtSize);CHKERRQ(ierr);
8814bbf5ea8SMatthew G. Knepley     /* empty patch, continue */
8824bbf5ea8SMatthew G. Knepley     if (chtSize == 0) continue;
8834bbf5ea8SMatthew G. Knepley 
8844bbf5ea8SMatthew G. Knepley     /* safe because size(cht) > 0 from above */
8851b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
8861b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
8875f824522SMatthew G. Knepley       PetscInt point, pdof;
8884bbf5ea8SMatthew G. Knepley 
8891b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
8900e126c0bSLawrence Mitchell       if (fStart <= point && point < fEnd) {
8910e126c0bSLawrence Mitchell         const PetscInt *support;
8920e126c0bSLawrence Mitchell         PetscInt supportSize, p;
8930e126c0bSLawrence Mitchell         PetscBool interior = PETSC_TRUE;
8940e126c0bSLawrence Mitchell         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
8950e126c0bSLawrence Mitchell         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
8960e126c0bSLawrence Mitchell         if (supportSize == 1) {
8970e126c0bSLawrence Mitchell           interior = PETSC_FALSE;
8980e126c0bSLawrence Mitchell         } else {
8990e126c0bSLawrence Mitchell           for (p = 0; p < supportSize; p++) {
9000e126c0bSLawrence Mitchell             PetscBool found;
9010e126c0bSLawrence Mitchell             /* FIXME: can I do this while iterating over cht? */
9020e126c0bSLawrence Mitchell             PetscHSetIHas(cht, support[p], &found);
9030e126c0bSLawrence Mitchell             if (!found) {
9040e126c0bSLawrence Mitchell               interior = PETSC_FALSE;
9050e126c0bSLawrence Mitchell               break;
9060e126c0bSLawrence Mitchell             }
9070e126c0bSLawrence Mitchell           }
9080e126c0bSLawrence Mitchell         }
9090e126c0bSLawrence Mitchell         if (interior) {
9100e126c0bSLawrence Mitchell           ierr = PetscSectionAddDof(intFacetCounts, v, 1);CHKERRQ(ierr);
9110e126c0bSLawrence Mitchell         } else {
9120e126c0bSLawrence Mitchell           ierr = PetscSectionAddDof(extFacetCounts, v, 1);CHKERRQ(ierr);
9130e126c0bSLawrence Mitchell         }
9140e126c0bSLawrence Mitchell       }
9155f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr);
9165f824522SMatthew G. Knepley       if (pdof)                            {ierr = PetscSectionAddDof(pointCounts, v, 1);CHKERRQ(ierr);}
9175f824522SMatthew G. Knepley       if (point >= cStart && point < cEnd) {ierr = PetscSectionAddDof(cellCounts, v, 1);CHKERRQ(ierr);}
9181b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
9194bbf5ea8SMatthew G. Knepley     }
9204bbf5ea8SMatthew G. Knepley   }
9215f824522SMatthew G. Knepley   if (isFiredrake) {ierr = DMLabelDestroyIndex(ghost);CHKERRQ(ierr);}
9224bbf5ea8SMatthew G. Knepley 
9234bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetUp(cellCounts);CHKERRQ(ierr);
9244bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr);
9254bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numCells, &cellsArray);CHKERRQ(ierr);
9265f824522SMatthew G. Knepley   ierr = PetscSectionSetUp(pointCounts);CHKERRQ(ierr);
9275f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(pointCounts, &numPoints);CHKERRQ(ierr);
9285f824522SMatthew G. Knepley   ierr = PetscMalloc1(numPoints, &pointsArray);CHKERRQ(ierr);
9294bbf5ea8SMatthew G. Knepley 
9300e126c0bSLawrence Mitchell   ierr = PetscSectionSetUp(intFacetCounts);CHKERRQ(ierr);
9310e126c0bSLawrence Mitchell   ierr = PetscSectionSetUp(extFacetCounts);CHKERRQ(ierr);
9320e126c0bSLawrence Mitchell   ierr = PetscSectionGetStorageSize(intFacetCounts, &numIntFacets);CHKERRQ(ierr);
9330e126c0bSLawrence Mitchell   ierr = PetscSectionGetStorageSize(extFacetCounts, &numExtFacets);CHKERRQ(ierr);
9340e126c0bSLawrence Mitchell   ierr = PetscMalloc1(numIntFacets, &intFacetsArray);CHKERRQ(ierr);
935*eb62eeaaSLawrence Mitchell   ierr = PetscMalloc1(numIntFacets*2, &intFacetsToPatchCell);CHKERRQ(ierr);
9360e126c0bSLawrence Mitchell   ierr = PetscMalloc1(numExtFacets, &extFacetsArray);CHKERRQ(ierr);
9370e126c0bSLawrence Mitchell 
938*eb62eeaaSLawrence Mitchell 
9394bbf5ea8SMatthew G. Knepley   /* Now that we know how much space we need, run through again and actually remember the cells. */
9404bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; v++ ) {
9411b68eb51SMatthew G. Knepley     PetscHashIter hi;
9420e126c0bSLawrence Mitchell     PetscInt       dof, off, cdof, coff, efdof, efoff, ifdof, ifoff, pdof, n = 0, cn = 0, ifn = 0, efn = 0;
9434bbf5ea8SMatthew G. Knepley 
9445f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(pointCounts, v, &dof);CHKERRQ(ierr);
9455f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(pointCounts, v, &off);CHKERRQ(ierr);
9465f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &cdof);CHKERRQ(ierr);
9475f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &coff);CHKERRQ(ierr);
9480e126c0bSLawrence Mitchell     ierr = PetscSectionGetDof(intFacetCounts, v, &ifdof);CHKERRQ(ierr);
9490e126c0bSLawrence Mitchell     ierr = PetscSectionGetOffset(intFacetCounts, v, &ifoff);CHKERRQ(ierr);
9500e126c0bSLawrence Mitchell     ierr = PetscSectionGetDof(extFacetCounts, v, &efdof);CHKERRQ(ierr);
9510e126c0bSLawrence Mitchell     ierr = PetscSectionGetOffset(extFacetCounts, v, &efoff);CHKERRQ(ierr);
9525f824522SMatthew G. Knepley     if (dof <= 0) continue;
9534bbf5ea8SMatthew G. Knepley     ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr);
9545f824522SMatthew G. Knepley     ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr);
9551b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
9561b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
9574bbf5ea8SMatthew G. Knepley       PetscInt point;
9584bbf5ea8SMatthew G. Knepley 
9591b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
9600e126c0bSLawrence Mitchell       if (fStart <= point && point < fEnd) {
9610e126c0bSLawrence Mitchell         const PetscInt *support;
9620e126c0bSLawrence Mitchell         PetscInt supportSize, p;
9630e126c0bSLawrence Mitchell         PetscBool interior = PETSC_TRUE;
9640e126c0bSLawrence Mitchell         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
9650e126c0bSLawrence Mitchell         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
9660e126c0bSLawrence Mitchell         if (supportSize == 1) {
9670e126c0bSLawrence Mitchell           interior = PETSC_FALSE;
9680e126c0bSLawrence Mitchell         } else {
9690e126c0bSLawrence Mitchell           for (p = 0; p < supportSize; p++) {
9700e126c0bSLawrence Mitchell             PetscBool found;
9710e126c0bSLawrence Mitchell             /* FIXME: can I do this while iterating over cht? */
9720e126c0bSLawrence Mitchell             PetscHSetIHas(cht, support[p], &found);
9730e126c0bSLawrence Mitchell             if (!found) {
9740e126c0bSLawrence Mitchell               interior = PETSC_FALSE;
9750e126c0bSLawrence Mitchell               break;
9760e126c0bSLawrence Mitchell             }
9770e126c0bSLawrence Mitchell           }
9780e126c0bSLawrence Mitchell         }
9790e126c0bSLawrence Mitchell         if (interior) {
980*eb62eeaaSLawrence Mitchell           intFacetsToPatchCell[2*ifoff + ifn] = support[0];
981*eb62eeaaSLawrence Mitchell           intFacetsToPatchCell[2*ifoff + ifn + 1] = support[1];
9820e126c0bSLawrence Mitchell           intFacetsArray[ifoff + ifn++] = point;
9830e126c0bSLawrence Mitchell         } else {
9840e126c0bSLawrence Mitchell           extFacetsArray[efoff + efn++] = point;
9850e126c0bSLawrence Mitchell         }
9860e126c0bSLawrence Mitchell       }
9875f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr);
9885f824522SMatthew G. Knepley       if (pdof)                            {pointsArray[off + n++] = point;}
9895f824522SMatthew G. Knepley       if (point >= cStart && point < cEnd) {cellsArray[coff + cn++] = point;}
9901b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
9914bbf5ea8SMatthew G. Knepley     }
9920e126c0bSLawrence Mitchell     if (ifn != ifdof) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of interior facets in patch %D is %D, but should be %D", v, ifn, ifdof);
9930e126c0bSLawrence Mitchell     if (efn != efdof) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of exterior facets in patch %D is %D, but should be %D", v, efn, efdof);
9945f824522SMatthew 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);
9955f824522SMatthew 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);
996*eb62eeaaSLawrence Mitchell 
997*eb62eeaaSLawrence Mitchell     for (ifn = 0; ifn < ifdof; ifn++) {
998*eb62eeaaSLawrence Mitchell       PetscInt cell0 = intFacetsToPatchCell[2*ifoff + ifn];
999*eb62eeaaSLawrence Mitchell       PetscInt cell1 = intFacetsToPatchCell[2*ifoff + ifn + 1];
1000*eb62eeaaSLawrence Mitchell       PetscBool found0 = PETSC_FALSE, found1 = PETSC_FALSE;
1001*eb62eeaaSLawrence Mitchell       for (n = 0; n < cdof; n++) {
1002*eb62eeaaSLawrence Mitchell         if (!found0 && cell0 == cellsArray[off + n]) {
1003*eb62eeaaSLawrence Mitchell           intFacetsToPatchCell[2*ifoff + ifn] = cellsArray[off + n];
1004*eb62eeaaSLawrence Mitchell           found0 = PETSC_TRUE;
1005*eb62eeaaSLawrence Mitchell         }
1006*eb62eeaaSLawrence Mitchell         if (!found1 && cell1 == cellsArray[off + n]) {
1007*eb62eeaaSLawrence Mitchell           intFacetsToPatchCell[2*ifoff + ifn + 1] = cellsArray[off + n];
1008*eb62eeaaSLawrence Mitchell         }
1009*eb62eeaaSLawrence Mitchell         if (found0 && found1) break;
1010*eb62eeaaSLawrence Mitchell       }
1011*eb62eeaaSLawrence Mitchell     }
10124bbf5ea8SMatthew G. Knepley   }
10131b68eb51SMatthew G. Knepley   ierr = PetscHSetIDestroy(&ht);CHKERRQ(ierr);
10141b68eb51SMatthew G. Knepley   ierr = PetscHSetIDestroy(&cht);CHKERRQ(ierr);
10154bbf5ea8SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
10165f824522SMatthew G. Knepley 
10175f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numCells,  cellsArray,  PETSC_OWN_POINTER, &patch->cells);CHKERRQ(ierr);
10185f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->cells,  "Patch Cells");CHKERRQ(ierr);
10195f824522SMatthew G. Knepley   if (patch->viewCells) {
10205f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->cellCounts, patch->viewerCells, patch->formatCells);CHKERRQ(ierr);
10215f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->cells,      patch->viewerCells, patch->formatCells);CHKERRQ(ierr);
10225f824522SMatthew G. Knepley   }
10230e126c0bSLawrence Mitchell   ierr = ISCreateGeneral(PETSC_COMM_SELF, numIntFacets,  intFacetsArray,  PETSC_OWN_POINTER, &patch->intFacets);CHKERRQ(ierr);
10240e126c0bSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->intFacets,  "Patch Interior Facets");CHKERRQ(ierr);
1025*eb62eeaaSLawrence Mitchell   ierr = ISCreateGeneral(PETSC_COMM_SELF, 2*numIntFacets, intFacetsToPatchCell, PETSC_OWN_POINTER, &patch->intFacetsToPatchCell);CHKERRQ(ierr);
1026*eb62eeaaSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->intFacetsToPatchCell,  "Patch Interior Facets local support");CHKERRQ(ierr);
10270e126c0bSLawrence Mitchell   if (patch->viewIntFacets) {
1028fbb82ab5SLawrence Mitchell     ierr = ObjectView((PetscObject) patch->intFacetCounts,       patch->viewerIntFacets, patch->formatIntFacets);CHKERRQ(ierr);
10290e126c0bSLawrence Mitchell     ierr = ObjectView((PetscObject) patch->intFacets,            patch->viewerIntFacets, patch->formatIntFacets);CHKERRQ(ierr);
1030*eb62eeaaSLawrence Mitchell     ierr = ObjectView((PetscObject) patch->intFacetsToPatchCell, patch->viewerIntFacets, patch->formatIntFacets);CHKERRQ(ierr);
10310e126c0bSLawrence Mitchell   }
10320e126c0bSLawrence Mitchell   ierr = ISCreateGeneral(PETSC_COMM_SELF, numExtFacets,  extFacetsArray,  PETSC_OWN_POINTER, &patch->extFacets);CHKERRQ(ierr);
10330e126c0bSLawrence Mitchell   ierr = PetscObjectSetName((PetscObject) patch->extFacets,  "Patch Exterior Facets");CHKERRQ(ierr);
10340e126c0bSLawrence Mitchell   if (patch->viewExtFacets) {
1035fbb82ab5SLawrence Mitchell     ierr = ObjectView((PetscObject) patch->extFacetCounts, patch->viewerExtFacets, patch->formatExtFacets);CHKERRQ(ierr);
10360e126c0bSLawrence Mitchell     ierr = ObjectView((PetscObject) patch->extFacets,      patch->viewerExtFacets, patch->formatExtFacets);CHKERRQ(ierr);
10370e126c0bSLawrence Mitchell   }
10385f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints, pointsArray, PETSC_OWN_POINTER, &patch->points);CHKERRQ(ierr);
10395f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->points, "Patch Points");CHKERRQ(ierr);
10405f824522SMatthew G. Knepley   if (patch->viewPoints) {
10415f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->pointCounts, patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr);
10425f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->points,      patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr);
10435f824522SMatthew G. Knepley   }
10444bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
10454bbf5ea8SMatthew G. Knepley }
10464bbf5ea8SMatthew G. Knepley 
10474bbf5ea8SMatthew G. Knepley /*
10484bbf5ea8SMatthew G. Knepley  * PCPatchCreateCellPatchDiscretisationInfo - Build the dof maps for cell patches
10494bbf5ea8SMatthew G. Knepley  *
10504bbf5ea8SMatthew G. Knepley  * Input Parameters:
10514bbf5ea8SMatthew G. Knepley  * + dm - The DMPlex object defining the mesh
10524bbf5ea8SMatthew G. Knepley  * . cellCounts - Section with counts of cells around each vertex
10534bbf5ea8SMatthew G. Knepley  * . cells - IS of the cell point indices of cells in each patch
10544bbf5ea8SMatthew G. Knepley  * . cellNumbering - Section mapping plex cell points to Firedrake cell indices.
10554bbf5ea8SMatthew G. Knepley  * . nodesPerCell - number of nodes per cell.
10564bbf5ea8SMatthew G. Knepley  * - cellNodeMap - map from cells to node indices (nodesPerCell * numCells)
10574bbf5ea8SMatthew G. Knepley  *
10584bbf5ea8SMatthew G. Knepley  * Output Parameters:
10595f824522SMatthew G. Knepley  * + dofs - IS of local dof numbers of each cell in the patch, where local is a patch local numbering
10604bbf5ea8SMatthew G. Knepley  * . gtolCounts - Section with counts of dofs per cell patch
10614bbf5ea8SMatthew G. Knepley  * - gtol - IS mapping from global dofs to local dofs for each patch.
10624bbf5ea8SMatthew G. Knepley  */
10634bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatchDiscretisationInfo(PC pc)
10644bbf5ea8SMatthew G. Knepley {
10654bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch           = (PC_PATCH *) pc->data;
10664bbf5ea8SMatthew G. Knepley   PetscSection    cellCounts      = patch->cellCounts;
10675f824522SMatthew G. Knepley   PetscSection    pointCounts     = patch->pointCounts;
10680904074fSPatrick Farrell   PetscSection    gtolCounts, gtolCountsWithArtificial = NULL, gtolCountsWithAll = NULL;
10694bbf5ea8SMatthew G. Knepley   IS              cells           = patch->cells;
10705f824522SMatthew G. Knepley   IS              points          = patch->points;
10714bbf5ea8SMatthew G. Knepley   PetscSection    cellNumbering   = patch->cellNumbering;
10725f824522SMatthew G. Knepley   PetscInt        Nf              = patch->nsubspaces;
10735f824522SMatthew G. Knepley   PetscInt        numCells, numPoints;
10744bbf5ea8SMatthew G. Knepley   PetscInt        numDofs;
10750904074fSPatrick Farrell   PetscInt        numGlobalDofs, numGlobalDofsWithArtificial, numGlobalDofsWithAll;
10764bbf5ea8SMatthew G. Knepley   PetscInt        totalDofsPerCell = patch->totalDofsPerCell;
10774bbf5ea8SMatthew G. Knepley   PetscInt        vStart, vEnd, v;
10785f824522SMatthew G. Knepley   const PetscInt *cellsArray, *pointsArray;
10794bbf5ea8SMatthew G. Knepley   PetscInt       *newCellsArray   = NULL;
10804bbf5ea8SMatthew G. Knepley   PetscInt       *dofsArray       = NULL;
1081c2e6f3c0SFlorian Wechsung   PetscInt       *dofsArrayWithArtificial = NULL;
10820904074fSPatrick Farrell   PetscInt       *dofsArrayWithAll = NULL;
10835f824522SMatthew G. Knepley   PetscInt       *offsArray       = NULL;
1084c2e6f3c0SFlorian Wechsung   PetscInt       *offsArrayWithArtificial = NULL;
10850904074fSPatrick Farrell   PetscInt       *offsArrayWithAll = NULL;
10864bbf5ea8SMatthew G. Knepley   PetscInt       *asmArray        = NULL;
1087c2e6f3c0SFlorian Wechsung   PetscInt       *asmArrayWithArtificial = NULL;
10880904074fSPatrick Farrell   PetscInt       *asmArrayWithAll = NULL;
10894bbf5ea8SMatthew G. Knepley   PetscInt       *globalDofsArray = NULL;
1090c2e6f3c0SFlorian Wechsung   PetscInt       *globalDofsArrayWithArtificial = NULL;
10910904074fSPatrick Farrell   PetscInt       *globalDofsArrayWithAll = NULL;
10924bbf5ea8SMatthew G. Knepley   PetscInt        globalIndex     = 0;
10934bbf5ea8SMatthew G. Knepley   PetscInt        key             = 0;
10944bbf5ea8SMatthew G. Knepley   PetscInt        asmKey          = 0;
1095557beb66SLawrence Mitchell   DM              dm              = NULL;
1096557beb66SLawrence Mitchell   const PetscInt *bcNodes         = NULL;
10971b68eb51SMatthew G. Knepley   PetscHMapI      ht;
1098c2e6f3c0SFlorian Wechsung   PetscHMapI      htWithArtificial;
10990904074fSPatrick Farrell   PetscHMapI      htWithAll;
11001b68eb51SMatthew G. Knepley   PetscHSetI      globalBcs;
1101557beb66SLawrence Mitchell   PetscInt        numBcs;
11021b68eb51SMatthew G. Knepley   PetscHSetI      ownedpts, seenpts, owneddofs, seendofs, artificialbcs;
1103cda239d9SMatthew G. Knepley   PetscInt        pStart, pEnd, p, i;
110410534d48SPatrick Farrell   char            option[PETSC_MAX_PATH_LEN];
110539fd2e8aSPatrick Farrell   PetscBool       isNonlinear;
11064bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
11074bbf5ea8SMatthew G. Knepley 
11084bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1109557beb66SLawrence Mitchell 
1110557beb66SLawrence Mitchell   ierr = PCGetDM(pc, &dm); CHKERRQ(ierr);
11114bbf5ea8SMatthew G. Knepley   /* dofcounts section is cellcounts section * dofPerCell */
11124bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr);
11135f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(patch->pointCounts, &numPoints);CHKERRQ(ierr);
11144bbf5ea8SMatthew G. Knepley   numDofs = numCells * totalDofsPerCell;
11154bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numDofs, &dofsArray);CHKERRQ(ierr);
11165f824522SMatthew G. Knepley   ierr = PetscMalloc1(numPoints*Nf, &offsArray);CHKERRQ(ierr);
11174bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numDofs, &asmArray);CHKERRQ(ierr);
11184bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numCells, &newCellsArray);CHKERRQ(ierr);
11194bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(cellCounts, &vStart, &vEnd);CHKERRQ(ierr);
11204bbf5ea8SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCounts);CHKERRQ(ierr);
11214bbf5ea8SMatthew G. Knepley   gtolCounts = patch->gtolCounts;
11224bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetChart(gtolCounts, vStart, vEnd);CHKERRQ(ierr);
11235f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->gtolCounts, "Patch Global Index Section");CHKERRQ(ierr);
11244bbf5ea8SMatthew G. Knepley 
11250904074fSPatrick Farrell   if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE)
1126c2e6f3c0SFlorian Wechsung   {
1127f0dcb611SKarl Rupp     ierr = PetscMalloc1(numPoints*Nf, &offsArrayWithArtificial);CHKERRQ(ierr);
1128c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numDofs, &asmArrayWithArtificial);CHKERRQ(ierr);
1129c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numDofs, &dofsArrayWithArtificial);CHKERRQ(ierr);
1130c2e6f3c0SFlorian Wechsung     ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithArtificial);CHKERRQ(ierr);
1131c2e6f3c0SFlorian Wechsung     gtolCountsWithArtificial = patch->gtolCountsWithArtificial;
1132c2e6f3c0SFlorian Wechsung     ierr = PetscSectionSetChart(gtolCountsWithArtificial, vStart, vEnd);CHKERRQ(ierr);
1133c2e6f3c0SFlorian Wechsung     ierr = PetscObjectSetName((PetscObject) patch->gtolCountsWithArtificial, "Patch Global Index Section Including Artificial BCs");CHKERRQ(ierr);
1134c2e6f3c0SFlorian Wechsung   }
1135c2e6f3c0SFlorian Wechsung 
11360904074fSPatrick Farrell   isNonlinear = patch->isNonlinear;
11370904074fSPatrick Farrell   if(isNonlinear)
11380904074fSPatrick Farrell   {
11390904074fSPatrick Farrell     ierr = PetscMalloc1(numPoints*Nf, &offsArrayWithAll);CHKERRQ(ierr);
11400904074fSPatrick Farrell     ierr = PetscMalloc1(numDofs, &asmArrayWithAll);CHKERRQ(ierr);
11410904074fSPatrick Farrell     ierr = PetscMalloc1(numDofs, &dofsArrayWithAll);CHKERRQ(ierr);
11420904074fSPatrick Farrell     ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithAll);CHKERRQ(ierr);
11430904074fSPatrick Farrell     gtolCountsWithAll = patch->gtolCountsWithAll;
11440904074fSPatrick Farrell     ierr = PetscSectionSetChart(gtolCountsWithAll, vStart, vEnd);CHKERRQ(ierr);
11450904074fSPatrick Farrell     ierr = PetscObjectSetName((PetscObject) patch->gtolCountsWithAll, "Patch Global Index Section Including All BCs");CHKERRQ(ierr);
11460904074fSPatrick Farrell   }
11470904074fSPatrick Farrell 
1148557beb66SLawrence Mitchell   /* Outside the patch loop, get the dofs that are globally-enforced Dirichlet
1149557beb66SLawrence Mitchell    conditions */
11501b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&globalBcs);CHKERRQ(ierr);
1151557beb66SLawrence Mitchell   ierr = ISGetIndices(patch->ghostBcNodes, &bcNodes); CHKERRQ(ierr);
1152557beb66SLawrence Mitchell   ierr = ISGetSize(patch->ghostBcNodes, &numBcs); CHKERRQ(ierr);
1153cda239d9SMatthew G. Knepley   for (i = 0; i < numBcs; ++i) {
11541b68eb51SMatthew G. Knepley     ierr = PetscHSetIAdd(globalBcs, bcNodes[i]);CHKERRQ(ierr); /* these are already in concatenated numbering */
1155557beb66SLawrence Mitchell   }
1156557beb66SLawrence Mitchell   ierr = ISRestoreIndices(patch->ghostBcNodes, &bcNodes); CHKERRQ(ierr);
1157557beb66SLawrence Mitchell   ierr = ISDestroy(&patch->ghostBcNodes); CHKERRQ(ierr); /* memory optimisation */
1158557beb66SLawrence Mitchell 
1159557beb66SLawrence Mitchell   /* Hash tables for artificial BC construction */
11601b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&ownedpts);CHKERRQ(ierr);
11611b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&seenpts);CHKERRQ(ierr);
11621b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&owneddofs);CHKERRQ(ierr);
11631b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&seendofs);CHKERRQ(ierr);
11641b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&artificialbcs);CHKERRQ(ierr);
1165557beb66SLawrence Mitchell 
11664bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(cells, &cellsArray);CHKERRQ(ierr);
11675f824522SMatthew G. Knepley   ierr = ISGetIndices(points, &pointsArray);CHKERRQ(ierr);
11681b68eb51SMatthew G. Knepley   ierr = PetscHMapICreate(&ht);CHKERRQ(ierr);
1169c2e6f3c0SFlorian Wechsung   ierr = PetscHMapICreate(&htWithArtificial);CHKERRQ(ierr);
11700904074fSPatrick Farrell   ierr = PetscHMapICreate(&htWithAll);CHKERRQ(ierr);
11714bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
11724bbf5ea8SMatthew G. Knepley     PetscInt localIndex = 0;
1173c2e6f3c0SFlorian Wechsung     PetscInt localIndexWithArtificial = 0;
11740904074fSPatrick Farrell     PetscInt localIndexWithAll = 0;
11754bbf5ea8SMatthew G. Knepley     PetscInt dof, off, i, j, k, l;
11764bbf5ea8SMatthew G. Knepley 
11771b68eb51SMatthew G. Knepley     ierr = PetscHMapIClear(ht);CHKERRQ(ierr);
1178c2e6f3c0SFlorian Wechsung     ierr = PetscHMapIClear(htWithArtificial);CHKERRQ(ierr);
11790904074fSPatrick Farrell     ierr = PetscHMapIClear(htWithAll);CHKERRQ(ierr);
11804bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr);
11814bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr);
11824bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
11834bbf5ea8SMatthew G. Knepley 
1184557beb66SLawrence Mitchell     /* Calculate the global numbers of the artificial BC dofs here first */
1185557beb66SLawrence Mitchell     ierr = patch->patchconstructop((void*)patch, dm, v, ownedpts); CHKERRQ(ierr);
1186557beb66SLawrence Mitchell     ierr = PCPatchCompleteCellPatch(pc, ownedpts, seenpts); CHKERRQ(ierr);
1187e4c66b91SPatrick Farrell     ierr = PCPatchGetPointDofs(pc, ownedpts, owneddofs, v, &patch->subspaces_to_exclude); CHKERRQ(ierr);
1188e4c66b91SPatrick Farrell     ierr = PCPatchGetPointDofs(pc, seenpts, seendofs, v, NULL); CHKERRQ(ierr);
1189557beb66SLawrence Mitchell     ierr = PCPatchComputeSetDifference_Private(owneddofs, seendofs, artificialbcs); CHKERRQ(ierr);
11908135ed82SLawrence Mitchell     if (patch->viewPatches) {
11911b68eb51SMatthew G. Knepley       PetscHSetI globalbcdofs;
11921b68eb51SMatthew G. Knepley       PetscHashIter hi;
11938135ed82SLawrence Mitchell       MPI_Comm comm = PetscObjectComm((PetscObject)pc);
11941b68eb51SMatthew G. Knepley 
11951b68eb51SMatthew G. Knepley       ierr = PetscHSetICreate(&globalbcdofs);CHKERRQ(ierr);
11968135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: owned dofs:\n", v); CHKERRQ(ierr);
11971b68eb51SMatthew G. Knepley       PetscHashIterBegin(owneddofs, hi);
11981b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(owneddofs, hi)) {
11998135ed82SLawrence Mitchell         PetscInt globalDof;
12008135ed82SLawrence Mitchell 
12011b68eb51SMatthew G. Knepley         PetscHashIterGetKey(owneddofs, hi, globalDof);
12021b68eb51SMatthew G. Knepley         PetscHashIterNext(owneddofs, hi);
12038135ed82SLawrence Mitchell         ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
12048135ed82SLawrence Mitchell       }
12058135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n"); CHKERRQ(ierr);
12068135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: seen dofs:\n", v); CHKERRQ(ierr);
12071b68eb51SMatthew G. Knepley       PetscHashIterBegin(seendofs, hi);
12081b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(seendofs, hi)) {
12098135ed82SLawrence Mitchell         PetscInt globalDof;
12108135ed82SLawrence Mitchell         PetscBool flg;
12118135ed82SLawrence Mitchell 
12121b68eb51SMatthew G. Knepley         PetscHashIterGetKey(seendofs, hi, globalDof);
12131b68eb51SMatthew G. Knepley         PetscHashIterNext(seendofs, hi);
12148135ed82SLawrence Mitchell         ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
12158135ed82SLawrence Mitchell 
12161b68eb51SMatthew G. Knepley         ierr = PetscHSetIHas(globalBcs, globalDof, &flg);CHKERRQ(ierr);
12171b68eb51SMatthew G. Knepley         if (flg) {ierr = PetscHSetIAdd(globalbcdofs, globalDof);CHKERRQ(ierr);}
12188135ed82SLawrence Mitchell       }
12198135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n"); CHKERRQ(ierr);
12208135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: global BCs:\n", v); CHKERRQ(ierr);
12211b68eb51SMatthew G. Knepley       ierr = PetscHSetIGetSize(globalbcdofs, &numBcs);CHKERRQ(ierr);
12228135ed82SLawrence Mitchell       if (numBcs > 0) {
12231b68eb51SMatthew G. Knepley         PetscHashIterBegin(globalbcdofs, hi);
12241b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(globalbcdofs, hi)) {
12258135ed82SLawrence Mitchell           PetscInt globalDof;
12261b68eb51SMatthew G. Knepley           PetscHashIterGetKey(globalbcdofs, hi, globalDof);
12271b68eb51SMatthew G. Knepley           PetscHashIterNext(globalbcdofs, hi);
12288135ed82SLawrence Mitchell           ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof);CHKERRQ(ierr);
12298135ed82SLawrence Mitchell         }
12308135ed82SLawrence Mitchell       }
12318135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n");CHKERRQ(ierr);
12328135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: artificial BCs:\n", v);CHKERRQ(ierr);
12331b68eb51SMatthew G. Knepley       ierr = PetscHSetIGetSize(artificialbcs, &numBcs);CHKERRQ(ierr);
12348135ed82SLawrence Mitchell       if (numBcs > 0) {
12351b68eb51SMatthew G. Knepley         PetscHashIterBegin(artificialbcs, hi);
12361b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(artificialbcs, hi)) {
12378135ed82SLawrence Mitchell           PetscInt globalDof;
12381b68eb51SMatthew G. Knepley           PetscHashIterGetKey(artificialbcs, hi, globalDof);
12391b68eb51SMatthew G. Knepley           PetscHashIterNext(artificialbcs, hi);
12408135ed82SLawrence Mitchell           ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
12418135ed82SLawrence Mitchell         }
12428135ed82SLawrence Mitchell       }
12438135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n\n"); CHKERRQ(ierr);
12441b68eb51SMatthew G. Knepley       ierr = PetscHSetIDestroy(&globalbcdofs);CHKERRQ(ierr);
12458135ed82SLawrence Mitchell     }
12464bbf5ea8SMatthew G. Knepley    for (k = 0; k < patch->nsubspaces; ++k) {
12474bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
12484bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
12494bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
12504bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
12514bbf5ea8SMatthew G. Knepley 
12524bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
12534bbf5ea8SMatthew G. Knepley         /* Walk over the cells in this patch. */
12544bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
12555f824522SMatthew G. Knepley         PetscInt       cell = c;
12564bbf5ea8SMatthew G. Knepley 
12575f824522SMatthew G. Knepley         /* TODO Change this to an IS */
12585f824522SMatthew G. Knepley         if (cellNumbering) {
12594bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetDof(cellNumbering, c, &cell);CHKERRQ(ierr);
12604bbf5ea8SMatthew G. Knepley           if (cell <= 0) SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_OUTOFRANGE, "Cell %D doesn't appear in cell numbering map", c);
12614bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);
12625f824522SMatthew G. Knepley         }
12634bbf5ea8SMatthew G. Knepley         newCellsArray[i] = cell;
12644bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
12654bbf5ea8SMatthew G. Knepley           /* For each global dof, map it into contiguous local storage. */
12664bbf5ea8SMatthew G. Knepley           const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + subspaceOffset;
12674bbf5ea8SMatthew G. Knepley           /* finally, loop over block size */
12684bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
12691b68eb51SMatthew G. Knepley             PetscInt  localDof;
12701b68eb51SMatthew G. Knepley             PetscBool isGlobalBcDof, isArtificialBcDof;
12714bbf5ea8SMatthew G. Knepley 
1272557beb66SLawrence Mitchell             /* first, check if this is either a globally enforced or locally enforced BC dof */
12731b68eb51SMatthew G. Knepley             ierr = PetscHSetIHas(globalBcs, globalDof + l, &isGlobalBcDof);CHKERRQ(ierr);
12741b68eb51SMatthew G. Knepley             ierr = PetscHSetIHas(artificialbcs, globalDof + l, &isArtificialBcDof);CHKERRQ(ierr);
1275557beb66SLawrence Mitchell 
1276557beb66SLawrence Mitchell             /* if it's either, don't ever give it a local dof number */
12771b68eb51SMatthew G. Knepley             if (isGlobalBcDof || isArtificialBcDof) {
1278c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = -1; /* don't use this in assembly in this patch */
1279557beb66SLawrence Mitchell             } else {
12801b68eb51SMatthew G. Knepley               ierr = PetscHMapIGet(ht, globalDof + l, &localDof);CHKERRQ(ierr);
12814bbf5ea8SMatthew G. Knepley               if (localDof == -1) {
12824bbf5ea8SMatthew G. Knepley                 localDof = localIndex++;
12831b68eb51SMatthew G. Knepley                 ierr = PetscHMapISet(ht, globalDof + l, localDof);CHKERRQ(ierr);
12844bbf5ea8SMatthew G. Knepley               }
12854bbf5ea8SMatthew G. Knepley               if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
12864bbf5ea8SMatthew G. Knepley               /* And store. */
1287c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = localDof;
12884bbf5ea8SMatthew G. Knepley             }
1289c2e6f3c0SFlorian Wechsung 
12900904074fSPatrick Farrell             if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1291c2e6f3c0SFlorian Wechsung               if (isGlobalBcDof) {
1292e047a90bSFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = -1; /* don't use this in assembly in this patch */
1293c2e6f3c0SFlorian Wechsung               } else {
1294c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapIGet(htWithArtificial, globalDof + l, &localDof);CHKERRQ(ierr);
1295c2e6f3c0SFlorian Wechsung                 if (localDof == -1) {
1296c2e6f3c0SFlorian Wechsung                   localDof = localIndexWithArtificial++;
1297c2e6f3c0SFlorian Wechsung                   ierr = PetscHMapISet(htWithArtificial, globalDof + l, localDof);CHKERRQ(ierr);
1298c2e6f3c0SFlorian Wechsung                 }
1299c2e6f3c0SFlorian Wechsung                 if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
1300c2e6f3c0SFlorian Wechsung                 /* And store.*/
1301c2e6f3c0SFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = localDof;
1302c2e6f3c0SFlorian Wechsung               }
1303c2e6f3c0SFlorian Wechsung             }
13040904074fSPatrick Farrell 
13050904074fSPatrick Farrell             if(isNonlinear) {
13060904074fSPatrick Farrell               /* Build the dofmap for the function space with _all_ dofs,
13070904074fSPatrick Farrell                  including those in any kind of boundary condition */
13080904074fSPatrick Farrell               ierr = PetscHMapIGet(htWithAll, globalDof + l, &localDof);CHKERRQ(ierr);
13090904074fSPatrick Farrell               if (localDof == -1) {
13100904074fSPatrick Farrell                 localDof = localIndexWithAll++;
13110904074fSPatrick Farrell                 ierr = PetscHMapISet(htWithAll, globalDof + l, localDof);CHKERRQ(ierr);
13120904074fSPatrick Farrell               }
13130904074fSPatrick Farrell               if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
13140904074fSPatrick Farrell               /* And store.*/
13150904074fSPatrick Farrell               dofsArrayWithAll[globalIndex] = localDof;
13160904074fSPatrick Farrell             }
1317c2e6f3c0SFlorian Wechsung             globalIndex++;
13184bbf5ea8SMatthew G. Knepley           }
13194bbf5ea8SMatthew G. Knepley         }
13204bbf5ea8SMatthew G. Knepley       }
1321557beb66SLawrence Mitchell     }
13224bbf5ea8SMatthew G. Knepley      /*How many local dofs in this patch? */
13230904074fSPatrick Farrell    if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1324c2e6f3c0SFlorian Wechsung      ierr = PetscHMapIGetSize(htWithArtificial, &dof);CHKERRQ(ierr);
1325c2e6f3c0SFlorian Wechsung      ierr = PetscSectionSetDof(gtolCountsWithArtificial, v, dof);CHKERRQ(ierr);
1326c2e6f3c0SFlorian Wechsung    }
13270904074fSPatrick Farrell    if (isNonlinear) {
13280904074fSPatrick Farrell      ierr = PetscHMapIGetSize(htWithAll, &dof);CHKERRQ(ierr);
13290904074fSPatrick Farrell      ierr = PetscSectionSetDof(gtolCountsWithAll, v, dof);CHKERRQ(ierr);
13300904074fSPatrick Farrell    }
13311b68eb51SMatthew G. Knepley     ierr = PetscHMapIGetSize(ht, &dof);CHKERRQ(ierr);
13324bbf5ea8SMatthew G. Knepley     ierr = PetscSectionSetDof(gtolCounts, v, dof);CHKERRQ(ierr);
13334bbf5ea8SMatthew G. Knepley   }
13344bbf5ea8SMatthew 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);
13354bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetUp(gtolCounts);CHKERRQ(ierr);
13364bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(gtolCounts, &numGlobalDofs);CHKERRQ(ierr);
13374bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numGlobalDofs, &globalDofsArray);CHKERRQ(ierr);
13384bbf5ea8SMatthew G. Knepley 
13390904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1340c2e6f3c0SFlorian Wechsung     ierr = PetscSectionSetUp(gtolCountsWithArtificial);CHKERRQ(ierr);
1341c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetStorageSize(gtolCountsWithArtificial, &numGlobalDofsWithArtificial);CHKERRQ(ierr);
1342c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numGlobalDofsWithArtificial, &globalDofsArrayWithArtificial);CHKERRQ(ierr);
1343c2e6f3c0SFlorian Wechsung   }
13440904074fSPatrick Farrell   if (isNonlinear) {
13450904074fSPatrick Farrell     ierr = PetscSectionSetUp(gtolCountsWithAll);CHKERRQ(ierr);
13460904074fSPatrick Farrell     ierr = PetscSectionGetStorageSize(gtolCountsWithAll, &numGlobalDofsWithAll);CHKERRQ(ierr);
13470904074fSPatrick Farrell     ierr = PetscMalloc1(numGlobalDofsWithAll, &globalDofsArrayWithAll);CHKERRQ(ierr);
13480904074fSPatrick Farrell   }
13494bbf5ea8SMatthew 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. */
13504bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
13511b68eb51SMatthew G. Knepley     PetscHashIter hi;
13525f824522SMatthew G. Knepley     PetscInt      dof, off, Np, ooff, i, j, k, l;
13534bbf5ea8SMatthew G. Knepley 
13541b68eb51SMatthew G. Knepley     ierr = PetscHMapIClear(ht);CHKERRQ(ierr);
1355c2e6f3c0SFlorian Wechsung     ierr = PetscHMapIClear(htWithArtificial);CHKERRQ(ierr);
13560904074fSPatrick Farrell     ierr = PetscHMapIClear(htWithAll);CHKERRQ(ierr);
13574bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr);
13584bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr);
13595f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(pointCounts, v, &Np);CHKERRQ(ierr);
13605f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(pointCounts, v, &ooff);CHKERRQ(ierr);
13614bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
13624bbf5ea8SMatthew G. Knepley 
13634bbf5ea8SMatthew G. Knepley     for (k = 0; k < patch->nsubspaces; ++k) {
13644bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
13654bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
13664bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
13674bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
1368d490bb3dSLawrence Mitchell       PetscInt        goff;
13694bbf5ea8SMatthew G. Knepley 
13704bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
13714bbf5ea8SMatthew G. Knepley         /* Reconstruct mapping of global-to-local on this patch. */
13724bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
13735f824522SMatthew G. Knepley         PetscInt       cell = c;
13744bbf5ea8SMatthew G. Knepley 
13755f824522SMatthew G. Knepley         if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);}
13764bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
13774bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
13785f824522SMatthew G. Knepley             const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset;
1379c2e6f3c0SFlorian Wechsung             const PetscInt localDof  = dofsArray[key];
13801b68eb51SMatthew G. Knepley             if (localDof >= 0) {ierr = PetscHMapISet(ht, globalDof, localDof);CHKERRQ(ierr);}
13810904074fSPatrick Farrell             if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1382c2e6f3c0SFlorian Wechsung               const PetscInt localDofWithArtificial = dofsArrayWithArtificial[key];
1383c2e6f3c0SFlorian Wechsung               if (localDofWithArtificial >= 0) {
1384c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapISet(htWithArtificial, globalDof, localDofWithArtificial);CHKERRQ(ierr);
1385c2e6f3c0SFlorian Wechsung               }
1386c2e6f3c0SFlorian Wechsung             }
13870904074fSPatrick Farrell             if (isNonlinear) {
13880904074fSPatrick Farrell               const PetscInt localDofWithAll = dofsArrayWithAll[key];
13890904074fSPatrick Farrell               if (localDofWithAll >= 0) {
13900904074fSPatrick Farrell                 ierr = PetscHMapISet(htWithAll, globalDof, localDofWithAll);CHKERRQ(ierr);
13910904074fSPatrick Farrell               }
13920904074fSPatrick Farrell             }
1393c2e6f3c0SFlorian Wechsung             key++;
13944bbf5ea8SMatthew G. Knepley           }
13954bbf5ea8SMatthew G. Knepley         }
13964bbf5ea8SMatthew G. Knepley       }
1397557beb66SLawrence Mitchell 
13984bbf5ea8SMatthew G. Knepley       /* Shove it in the output data structure. */
13994bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetOffset(gtolCounts, v, &goff);CHKERRQ(ierr);
14001b68eb51SMatthew G. Knepley       PetscHashIterBegin(ht, hi);
14011b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(ht, hi)) {
14024bbf5ea8SMatthew G. Knepley         PetscInt globalDof, localDof;
14034bbf5ea8SMatthew G. Knepley 
14041b68eb51SMatthew G. Knepley         PetscHashIterGetKey(ht, hi, globalDof);
14051b68eb51SMatthew G. Knepley         PetscHashIterGetVal(ht, hi, localDof);
14064bbf5ea8SMatthew G. Knepley         if (globalDof >= 0) globalDofsArray[goff + localDof] = globalDof;
14071b68eb51SMatthew G. Knepley         PetscHashIterNext(ht, hi);
14084bbf5ea8SMatthew G. Knepley       }
14095f824522SMatthew G. Knepley 
14100904074fSPatrick Farrell       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1411c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(gtolCountsWithArtificial, v, &goff);CHKERRQ(ierr);
1412c2e6f3c0SFlorian Wechsung         PetscHashIterBegin(htWithArtificial, hi);
1413c2e6f3c0SFlorian Wechsung         while (!PetscHashIterAtEnd(htWithArtificial, hi)) {
1414c2e6f3c0SFlorian Wechsung           PetscInt globalDof, localDof;
1415c2e6f3c0SFlorian Wechsung           PetscHashIterGetKey(htWithArtificial, hi, globalDof);
1416c2e6f3c0SFlorian Wechsung           PetscHashIterGetVal(htWithArtificial, hi, localDof);
1417c2e6f3c0SFlorian Wechsung           if (globalDof >= 0) globalDofsArrayWithArtificial[goff + localDof] = globalDof;
1418c2e6f3c0SFlorian Wechsung           PetscHashIterNext(htWithArtificial, hi);
1419c2e6f3c0SFlorian Wechsung         }
1420c2e6f3c0SFlorian Wechsung       }
14210904074fSPatrick Farrell       if (isNonlinear) {
14220904074fSPatrick Farrell         ierr = PetscSectionGetOffset(gtolCountsWithAll, v, &goff);CHKERRQ(ierr);
14230904074fSPatrick Farrell         PetscHashIterBegin(htWithAll, hi);
14240904074fSPatrick Farrell         while (!PetscHashIterAtEnd(htWithAll, hi)) {
14250904074fSPatrick Farrell           PetscInt globalDof, localDof;
14260904074fSPatrick Farrell           PetscHashIterGetKey(htWithAll, hi, globalDof);
14270904074fSPatrick Farrell           PetscHashIterGetVal(htWithAll, hi, localDof);
14280904074fSPatrick Farrell           if (globalDof >= 0) globalDofsArrayWithAll[goff + localDof] = globalDof;
14290904074fSPatrick Farrell           PetscHashIterNext(htWithAll, hi);
14300904074fSPatrick Farrell         }
14310904074fSPatrick Farrell       }
1432c2e6f3c0SFlorian Wechsung 
14335f824522SMatthew G. Knepley       for (p = 0; p < Np; ++p) {
14345f824522SMatthew G. Knepley         const PetscInt point = pointsArray[ooff + p];
14355f824522SMatthew G. Knepley         PetscInt       globalDof, localDof;
14365f824522SMatthew G. Knepley 
14375f824522SMatthew G. Knepley         ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, point, NULL, &globalDof);CHKERRQ(ierr);
14381b68eb51SMatthew G. Knepley         ierr = PetscHMapIGet(ht, globalDof, &localDof);CHKERRQ(ierr);
14395f824522SMatthew G. Knepley         offsArray[(ooff + p)*Nf + k] = localDof;
14400904074fSPatrick Farrell         if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1441c2e6f3c0SFlorian Wechsung           ierr = PetscHMapIGet(htWithArtificial, globalDof, &localDof);CHKERRQ(ierr);
1442c2e6f3c0SFlorian Wechsung           offsArrayWithArtificial[(ooff + p)*Nf + k] = localDof;
1443c2e6f3c0SFlorian Wechsung         }
14440904074fSPatrick Farrell         if (isNonlinear) {
14450904074fSPatrick Farrell           ierr = PetscHMapIGet(htWithAll, globalDof, &localDof);CHKERRQ(ierr);
14460904074fSPatrick Farrell           offsArrayWithAll[(ooff + p)*Nf + k] = localDof;
14470904074fSPatrick Farrell         }
14485f824522SMatthew G. Knepley       }
14494bbf5ea8SMatthew G. Knepley     }
14504bbf5ea8SMatthew G. Knepley 
14510cd083f8SSatish Balay     ierr = PetscHSetIDestroy(&globalBcs);CHKERRQ(ierr);
14521b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&ownedpts);CHKERRQ(ierr);
14531b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&seenpts);CHKERRQ(ierr);
14541b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&owneddofs);CHKERRQ(ierr);
14551b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&seendofs);CHKERRQ(ierr);
14561b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&artificialbcs);CHKERRQ(ierr);
1457557beb66SLawrence Mitchell 
14584bbf5ea8SMatthew G. Knepley       /* At this point, we have a hash table ht built that maps globalDof -> localDof.
14594bbf5ea8SMatthew G. Knepley      We need to create the dof table laid out cellwise first, then by subspace,
14604bbf5ea8SMatthew G. Knepley      as the assembler assembles cell-wise and we need to stuff the different
14614bbf5ea8SMatthew G. Knepley      contributions of the different function spaces to the right places. So we loop
14624bbf5ea8SMatthew G. Knepley      over cells, then over subspaces. */
14634bbf5ea8SMatthew G. Knepley     if (patch->nsubspaces > 1) { /* for nsubspaces = 1, data we need is already in dofsArray */
14644bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
14654bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
14665f824522SMatthew G. Knepley         PetscInt       cell = c;
14674bbf5ea8SMatthew G. Knepley 
14685f824522SMatthew G. Knepley         if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);}
14694bbf5ea8SMatthew G. Knepley         for (k = 0; k < patch->nsubspaces; ++k) {
14704bbf5ea8SMatthew G. Knepley           const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
14714bbf5ea8SMatthew G. Knepley           PetscInt        nodesPerCell   = patch->nodesPerCell[k];
14724bbf5ea8SMatthew G. Knepley           PetscInt        subspaceOffset = patch->subspaceOffsets[k];
14734bbf5ea8SMatthew G. Knepley           PetscInt        bs             = patch->bs[k];
14744bbf5ea8SMatthew G. Knepley 
14754bbf5ea8SMatthew G. Knepley           for (j = 0; j < nodesPerCell; ++j) {
14764bbf5ea8SMatthew G. Knepley             for (l = 0; l < bs; ++l) {
14775f824522SMatthew G. Knepley               const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset;
14784bbf5ea8SMatthew G. Knepley               PetscInt       localDof;
14794bbf5ea8SMatthew G. Knepley 
14801b68eb51SMatthew G. Knepley               ierr = PetscHMapIGet(ht, globalDof, &localDof);CHKERRQ(ierr);
1481557beb66SLawrence Mitchell               /* If it's not in the hash table, i.e. is a BC dof,
14821b68eb51SMatthew G. Knepley                then the PetscHSetIMap above gives -1, which matches
1483557beb66SLawrence Mitchell                exactly the convention for PETSc's matrix assembly to
1484557beb66SLawrence Mitchell                ignore the dof. So we don't need to do anything here */
1485c2e6f3c0SFlorian Wechsung               asmArray[asmKey] = localDof;
14860904074fSPatrick Farrell               if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1487c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapIGet(htWithArtificial, globalDof, &localDof);CHKERRQ(ierr);
1488c2e6f3c0SFlorian Wechsung                 asmArrayWithArtificial[asmKey] = localDof;
1489c2e6f3c0SFlorian Wechsung               }
14900904074fSPatrick Farrell               if (isNonlinear) {
14910904074fSPatrick Farrell                 ierr = PetscHMapIGet(htWithAll, globalDof, &localDof);CHKERRQ(ierr);
14920904074fSPatrick Farrell                 asmArrayWithAll[asmKey] = localDof;
14930904074fSPatrick Farrell               }
1494c2e6f3c0SFlorian Wechsung               asmKey++;
14954bbf5ea8SMatthew G. Knepley             }
14964bbf5ea8SMatthew G. Knepley           }
14974bbf5ea8SMatthew G. Knepley         }
14984bbf5ea8SMatthew G. Knepley       }
14994bbf5ea8SMatthew G. Knepley     }
15004bbf5ea8SMatthew G. Knepley   }
1501c2e6f3c0SFlorian Wechsung   if (1 == patch->nsubspaces) {
1502c2e6f3c0SFlorian Wechsung     ierr = PetscMemcpy(asmArray, dofsArray, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
15030904074fSPatrick Farrell     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1504c2e6f3c0SFlorian Wechsung       ierr = PetscMemcpy(asmArrayWithArtificial, dofsArrayWithArtificial, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
1505c2e6f3c0SFlorian Wechsung     }
15060904074fSPatrick Farrell     if (isNonlinear) {
15070904074fSPatrick Farrell       ierr = PetscMemcpy(asmArrayWithAll, dofsArrayWithAll, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
15080904074fSPatrick Farrell     }
1509c2e6f3c0SFlorian Wechsung   }
15104bbf5ea8SMatthew G. Knepley 
15111b68eb51SMatthew G. Knepley   ierr = PetscHMapIDestroy(&ht);CHKERRQ(ierr);
1512c2e6f3c0SFlorian Wechsung   ierr = PetscHMapIDestroy(&htWithArtificial);CHKERRQ(ierr);
15130904074fSPatrick Farrell   ierr = PetscHMapIDestroy(&htWithAll);CHKERRQ(ierr);
15144bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(cells, &cellsArray);CHKERRQ(ierr);
15155f824522SMatthew G. Knepley   ierr = ISRestoreIndices(points, &pointsArray);CHKERRQ(ierr);
15164bbf5ea8SMatthew G. Knepley   ierr = PetscFree(dofsArray);CHKERRQ(ierr);
15170904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1518c2e6f3c0SFlorian Wechsung     ierr = PetscFree(dofsArrayWithArtificial);CHKERRQ(ierr);
1519c2e6f3c0SFlorian Wechsung   }
15200904074fSPatrick Farrell   if (isNonlinear) {
15210904074fSPatrick Farrell     ierr = PetscFree(dofsArrayWithAll);CHKERRQ(ierr);
15220904074fSPatrick Farrell   }
15235f824522SMatthew G. Knepley   /* Create placeholder section for map from points to patch dofs */
15245f824522SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->patchSection);CHKERRQ(ierr);
15255f824522SMatthew G. Knepley   ierr = PetscSectionSetNumFields(patch->patchSection, patch->nsubspaces);CHKERRQ(ierr);
15261e5fa6bbSLawrence Mitchell   if (patch->combined) {
15271e5fa6bbSLawrence Mitchell     PetscInt numFields;
15281e5fa6bbSLawrence Mitchell     ierr = PetscSectionGetNumFields(patch->dofSection[0], &numFields);CHKERRQ(ierr);
15291e5fa6bbSLawrence 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);
15305f824522SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->dofSection[0], &pStart, &pEnd);CHKERRQ(ierr);
15315f824522SMatthew G. Knepley     ierr = PetscSectionSetChart(patch->patchSection, pStart, pEnd);CHKERRQ(ierr);
15325f824522SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
15335f824522SMatthew G. Knepley       PetscInt dof, fdof, f;
15345f824522SMatthew G. Knepley 
15355f824522SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->dofSection[0], p, &dof);CHKERRQ(ierr);
15365f824522SMatthew G. Knepley       ierr = PetscSectionSetDof(patch->patchSection, p, dof);CHKERRQ(ierr);
15375f824522SMatthew G. Knepley       for (f = 0; f < patch->nsubspaces; ++f) {
15381e5fa6bbSLawrence Mitchell         ierr = PetscSectionGetFieldDof(patch->dofSection[0], p, f, &fdof);CHKERRQ(ierr);
15395f824522SMatthew G. Knepley         ierr = PetscSectionSetFieldDof(patch->patchSection, p, f, fdof);CHKERRQ(ierr);
15405f824522SMatthew G. Knepley       }
15411e5fa6bbSLawrence Mitchell     }
15421e5fa6bbSLawrence Mitchell   } else {
15431e5fa6bbSLawrence Mitchell     PetscInt pStartf, pEndf, f;
15441e5fa6bbSLawrence Mitchell     pStart = PETSC_MAX_INT;
15451e5fa6bbSLawrence Mitchell     pEnd = PETSC_MIN_INT;
15461e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
15471e5fa6bbSLawrence Mitchell       ierr = PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf);CHKERRQ(ierr);
15481e5fa6bbSLawrence Mitchell       pStart = PetscMin(pStart, pStartf);
15491e5fa6bbSLawrence Mitchell       pEnd = PetscMax(pEnd, pEndf);
15501e5fa6bbSLawrence Mitchell     }
15511e5fa6bbSLawrence Mitchell     ierr = PetscSectionSetChart(patch->patchSection, pStart, pEnd);CHKERRQ(ierr);
15521e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
15531e5fa6bbSLawrence Mitchell       ierr = PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf);CHKERRQ(ierr);
15541e5fa6bbSLawrence Mitchell       for (p = pStartf; p < pEndf; ++p) {
15551e5fa6bbSLawrence Mitchell         PetscInt fdof;
15561e5fa6bbSLawrence Mitchell         ierr = PetscSectionGetDof(patch->dofSection[f], p, &fdof);CHKERRQ(ierr);
15571e5fa6bbSLawrence Mitchell         ierr = PetscSectionAddDof(patch->patchSection, p, fdof);CHKERRQ(ierr);
15581e5fa6bbSLawrence Mitchell         ierr = PetscSectionSetFieldDof(patch->patchSection, p, f, fdof);CHKERRQ(ierr);
1559bdd9e0cdSPatrick Farrell       }
1560bdd9e0cdSPatrick Farrell     }
15615f824522SMatthew G. Knepley   }
15625f824522SMatthew G. Knepley   ierr = PetscSectionSetUp(patch->patchSection);CHKERRQ(ierr);
15635f824522SMatthew G. Knepley   ierr = PetscSectionSetUseFieldOffsets(patch->patchSection, PETSC_TRUE);CHKERRQ(ierr);
15644bbf5ea8SMatthew G. Knepley   /* Replace cell indices with firedrake-numbered ones. */
15654bbf5ea8SMatthew G. Knepley   ierr = ISGeneralSetIndices(cells, numCells, (const PetscInt *) newCellsArray, PETSC_OWN_POINTER);CHKERRQ(ierr);
15664bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofs, globalDofsArray, PETSC_OWN_POINTER, &patch->gtol);CHKERRQ(ierr);
15675f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->gtol, "Global Indices");CHKERRQ(ierr);
156810534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_g2l_view", patch->classname);CHKERRQ(ierr);
156910534d48SPatrick Farrell   ierr = PetscSectionViewFromOptions(patch->gtolCounts, (PetscObject) pc, option);CHKERRQ(ierr);
157010534d48SPatrick Farrell   ierr = ISViewFromOptions(patch->gtol, (PetscObject) pc, option);CHKERRQ(ierr);
15714bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArray, PETSC_OWN_POINTER, &patch->dofs);CHKERRQ(ierr);
15725f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArray, PETSC_OWN_POINTER, &patch->offs);CHKERRQ(ierr);
15730904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1574c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithArtificial, globalDofsArrayWithArtificial, PETSC_OWN_POINTER, &patch->gtolWithArtificial);CHKERRQ(ierr);
1575c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithArtificial, PETSC_OWN_POINTER, &patch->dofsWithArtificial);CHKERRQ(ierr);
1576c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithArtificial, PETSC_OWN_POINTER, &patch->offsWithArtificial);CHKERRQ(ierr);
1577c2e6f3c0SFlorian Wechsung   }
15780904074fSPatrick Farrell   if (isNonlinear) {
15790904074fSPatrick Farrell     ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithAll, globalDofsArrayWithAll, PETSC_OWN_POINTER, &patch->gtolWithAll);CHKERRQ(ierr);
15800904074fSPatrick Farrell     ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithAll, PETSC_OWN_POINTER, &patch->dofsWithAll);CHKERRQ(ierr);
15810904074fSPatrick Farrell     ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithAll, PETSC_OWN_POINTER, &patch->offsWithAll);CHKERRQ(ierr);
15820904074fSPatrick Farrell   }
15834bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
15844bbf5ea8SMatthew G. Knepley }
15854bbf5ea8SMatthew G. Knepley 
15864bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchZeroFillMatrix_Private(Mat mat, const PetscInt ncell, const PetscInt ndof, const PetscInt *dof)
15874bbf5ea8SMatthew G. Knepley {
158823b8bdd9SMatthew G. Knepley   PetscScalar    *values = NULL;
15894bbf5ea8SMatthew G. Knepley   PetscInt        rows, c, i;
15904bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
15914bbf5ea8SMatthew G. Knepley 
15924bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
15934bbf5ea8SMatthew G. Knepley   ierr = PetscCalloc1(ndof*ndof, &values);CHKERRQ(ierr);
15944bbf5ea8SMatthew G. Knepley   for (c = 0; c < ncell; ++c) {
15954bbf5ea8SMatthew G. Knepley     const PetscInt *idx = &dof[ndof*c];
15964bbf5ea8SMatthew G. Knepley     ierr = MatSetValues(mat, ndof, idx, ndof, idx, values, INSERT_VALUES);CHKERRQ(ierr);
15974bbf5ea8SMatthew G. Knepley   }
15984bbf5ea8SMatthew G. Knepley   ierr = MatGetLocalSize(mat, &rows, NULL);CHKERRQ(ierr);
15994bbf5ea8SMatthew G. Knepley   for (i = 0; i < rows; ++i) {
16004bbf5ea8SMatthew G. Knepley     ierr = MatSetValues(mat, 1, &i, 1, &i, values, INSERT_VALUES);CHKERRQ(ierr);
16014bbf5ea8SMatthew G. Knepley   }
16024bbf5ea8SMatthew G. Knepley   ierr = MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16034bbf5ea8SMatthew G. Knepley   ierr = MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16044bbf5ea8SMatthew G. Knepley   ierr = PetscFree(values);CHKERRQ(ierr);
16054bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
16064bbf5ea8SMatthew G. Knepley }
16074bbf5ea8SMatthew G. Knepley 
1608c2e6f3c0SFlorian Wechsung static PetscErrorCode PCPatchCreateMatrix_Private(PC pc, PetscInt point, Mat *mat, PetscBool withArtificial)
16094bbf5ea8SMatthew G. Knepley {
16104bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
16114bbf5ea8SMatthew G. Knepley   Vec            x, y;
16124bbf5ea8SMatthew G. Knepley   PetscBool      flg;
16134bbf5ea8SMatthew G. Knepley   PetscInt       csize, rsize;
16144bbf5ea8SMatthew G. Knepley   const char    *prefix = NULL;
16154bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
16164bbf5ea8SMatthew G. Knepley 
16174bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1618c2e6f3c0SFlorian Wechsung   if(withArtificial) {
1619e047a90bSFlorian Wechsung     /* would be nice if we could create a rectangular matrix of size numDofsWithArtificial x numDofs here */
16201202d238SPatrick Farrell     x = patch->patchRHSWithArtificial[point];
16211202d238SPatrick Farrell     y = patch->patchRHSWithArtificial[point];
1622ff201f6aSFlorian Wechsung   } else {
16231202d238SPatrick Farrell     x = patch->patchRHS[point];
16241202d238SPatrick Farrell     y = patch->patchUpdate[point];
1625c2e6f3c0SFlorian Wechsung   }
1626c2e6f3c0SFlorian Wechsung 
16274bbf5ea8SMatthew G. Knepley   ierr = VecGetSize(x, &csize);CHKERRQ(ierr);
16284bbf5ea8SMatthew G. Knepley   ierr = VecGetSize(y, &rsize);CHKERRQ(ierr);
16294bbf5ea8SMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, mat);CHKERRQ(ierr);
16304bbf5ea8SMatthew G. Knepley   ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr);
16314bbf5ea8SMatthew G. Knepley   ierr = MatSetOptionsPrefix(*mat, prefix);CHKERRQ(ierr);
16325f824522SMatthew G. Knepley   ierr = MatAppendOptionsPrefix(*mat, "pc_patch_sub_");CHKERRQ(ierr);
16334bbf5ea8SMatthew G. Knepley   if (patch->sub_mat_type)       {ierr = MatSetType(*mat, patch->sub_mat_type);CHKERRQ(ierr);}
16347974b488SMatthew G. Knepley   else if (!patch->sub_mat_type) {ierr = MatSetType(*mat, MATDENSE);CHKERRQ(ierr);}
16354bbf5ea8SMatthew G. Knepley   ierr = MatSetSizes(*mat, rsize, csize, rsize, csize);CHKERRQ(ierr);
16364bbf5ea8SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) *mat, MATDENSE, &flg);CHKERRQ(ierr);
16374bbf5ea8SMatthew G. Knepley   if (!flg) {ierr = PetscObjectTypeCompare((PetscObject)*mat, MATSEQDENSE, &flg);CHKERRQ(ierr);}
16384bbf5ea8SMatthew G. Knepley   /* Sparse patch matrices */
16394bbf5ea8SMatthew G. Knepley   if (!flg) {
16404bbf5ea8SMatthew G. Knepley     PetscBT         bt;
16414bbf5ea8SMatthew G. Knepley     PetscInt       *dnnz      = NULL;
16424bbf5ea8SMatthew G. Knepley     const PetscInt *dofsArray = NULL;
16434bbf5ea8SMatthew G. Knepley     PetscInt        pStart, pEnd, ncell, offset, c, i, j;
16444bbf5ea8SMatthew G. Knepley 
1645c2e6f3c0SFlorian Wechsung     if(withArtificial) {
1646c2e6f3c0SFlorian Wechsung       ierr = ISGetIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1647ff201f6aSFlorian Wechsung     } else {
16484bbf5ea8SMatthew G. Knepley       ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1649c2e6f3c0SFlorian Wechsung     }
16504bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
16514bbf5ea8SMatthew G. Knepley     point += pStart;
16524bbf5ea8SMatthew 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);
16534bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
16544bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
16554bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventBegin(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr);
1656b2866507SPatrick Farrell     /* A PetscBT uses N^2 bits to store the sparsity pattern on a
16574bbf5ea8SMatthew G. Knepley      * patch. This is probably OK if the patches are not too big,
1658b2866507SPatrick Farrell      * but uses too much memory. We therefore switch based on rsize. */
1659b2866507SPatrick Farrell     if (rsize < 3000) { /* FIXME: I picked this switch value out of my hat */
1660b2866507SPatrick Farrell       ierr = PetscCalloc1(rsize, &dnnz);CHKERRQ(ierr);
16614bbf5ea8SMatthew G. Knepley       ierr = PetscBTCreate(rsize*rsize, &bt);CHKERRQ(ierr);
16624bbf5ea8SMatthew G. Knepley       for (c = 0; c < ncell; ++c) {
16634bbf5ea8SMatthew G. Knepley         const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell;
16644bbf5ea8SMatthew G. Knepley         for (i = 0; i < patch->totalDofsPerCell; ++i) {
16654bbf5ea8SMatthew G. Knepley           const PetscInt row = idx[i];
1666557beb66SLawrence Mitchell           if (row < 0) continue;
16674bbf5ea8SMatthew G. Knepley           for (j = 0; j < patch->totalDofsPerCell; ++j) {
16684bbf5ea8SMatthew G. Knepley             const PetscInt col = idx[j];
16694bbf5ea8SMatthew G. Knepley             const PetscInt key = row*rsize + col;
1670557beb66SLawrence Mitchell             if (col < 0) continue;
16714bbf5ea8SMatthew G. Knepley             if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
16724bbf5ea8SMatthew G. Knepley           }
16734bbf5ea8SMatthew G. Knepley         }
16744bbf5ea8SMatthew G. Knepley       }
16754bbf5ea8SMatthew G. Knepley       ierr = PetscBTDestroy(&bt);CHKERRQ(ierr);
16764bbf5ea8SMatthew G. Knepley       ierr = MatXAIJSetPreallocation(*mat, 1, dnnz, NULL, NULL, NULL);CHKERRQ(ierr);
16774bbf5ea8SMatthew G. Knepley       ierr = PetscFree(dnnz);CHKERRQ(ierr);
16784bbf5ea8SMatthew G. Knepley       ierr = PCPatchZeroFillMatrix_Private(*mat, ncell, patch->totalDofsPerCell, &dofsArray[offset*patch->totalDofsPerCell]);CHKERRQ(ierr);
1679b2866507SPatrick Farrell     } else { /* rsize too big, use MATPREALLOCATOR */
1680b2866507SPatrick Farrell       Mat preallocator;
1681b2866507SPatrick Farrell       PetscScalar* vals;
1682b2866507SPatrick Farrell 
1683b2866507SPatrick Farrell       ierr = PetscCalloc1(patch->totalDofsPerCell*patch->totalDofsPerCell, &vals);CHKERRQ(ierr);
1684b2866507SPatrick Farrell       ierr = MatCreate(PETSC_COMM_SELF, &preallocator);CHKERRQ(ierr);
1685b2866507SPatrick Farrell       ierr = MatSetType(preallocator, MATPREALLOCATOR);CHKERRQ(ierr);
1686b2866507SPatrick Farrell       ierr = MatSetSizes(preallocator, rsize, rsize, rsize, rsize);CHKERRQ(ierr);
1687b2866507SPatrick Farrell       ierr = MatSetUp(preallocator);CHKERRQ(ierr);
1688b2866507SPatrick Farrell       for (c = 0; c < ncell; ++c) {
1689b2866507SPatrick Farrell         const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell;
1690b2866507SPatrick Farrell         ierr = MatSetValues(preallocator, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, vals, INSERT_VALUES);CHKERRQ(ierr);
1691b2866507SPatrick Farrell       }
1692b2866507SPatrick Farrell       ierr = PetscFree(vals);CHKERRQ(ierr);
1693b2866507SPatrick Farrell       ierr = MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1694b2866507SPatrick Farrell       ierr = MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1695b2866507SPatrick Farrell       ierr = MatPreallocatorPreallocate(preallocator, PETSC_TRUE, *mat);CHKERRQ(ierr);
1696b2866507SPatrick Farrell       ierr = MatDestroy(&preallocator);CHKERRQ(ierr);
1697b2866507SPatrick Farrell     }
16984bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr);
1699fe117d09SFlorian Wechsung     if(withArtificial) {
1700fe117d09SFlorian Wechsung       ierr = ISRestoreIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1701fe117d09SFlorian Wechsung     } else {
17024bbf5ea8SMatthew G. Knepley       ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
17034bbf5ea8SMatthew G. Knepley     }
1704fe117d09SFlorian Wechsung   }
17054bbf5ea8SMatthew G. Knepley   ierr = MatSetUp(*mat);CHKERRQ(ierr);
17064bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
17074bbf5ea8SMatthew G. Knepley }
17084bbf5ea8SMatthew G. Knepley 
17090904074fSPatrick 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)
171092d50984SMatthew G. Knepley {
171192d50984SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
171292d50984SMatthew G. Knepley   DM              dm;
171392d50984SMatthew G. Knepley   PetscSection    s;
171492d50984SMatthew G. Knepley   const PetscInt *parray, *oarray;
171592d50984SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
171692d50984SMatthew G. Knepley   PetscErrorCode  ierr;
171792d50984SMatthew G. Knepley 
171892d50984SMatthew G. Knepley   PetscFunctionBegin;
171992d50984SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
172092d50984SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
172192d50984SMatthew G. Knepley   /* Set offset into patch */
172292d50984SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->pointCounts, patchNum, &Np);CHKERRQ(ierr);
172392d50984SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->pointCounts, patchNum, &poff);CHKERRQ(ierr);
172492d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->points, &parray);CHKERRQ(ierr);
172592d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->offs,   &oarray);CHKERRQ(ierr);
172692d50984SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
172792d50984SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
172892d50984SMatthew G. Knepley       const PetscInt point = parray[poff+p];
172992d50984SMatthew G. Knepley       PetscInt       dof;
173092d50984SMatthew G. Knepley 
173192d50984SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(patch->patchSection, point, f, &dof);CHKERRQ(ierr);
173292d50984SMatthew G. Knepley       ierr = PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);
173392d50984SMatthew G. Knepley       if (patch->nsubspaces == 1) {ierr = PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);}
173492d50984SMatthew G. Knepley       else                        {ierr = PetscSectionSetOffset(patch->patchSection, point, -1);CHKERRQ(ierr);}
173592d50984SMatthew G. Knepley     }
173692d50984SMatthew G. Knepley   }
173792d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->points, &parray);CHKERRQ(ierr);
173892d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->offs,   &oarray);CHKERRQ(ierr);
173992d50984SMatthew G. Knepley   if (patch->viewSection) {ierr = ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection);CHKERRQ(ierr);}
174092d50984SMatthew G. Knepley   ierr = DMPlexComputeResidual_Patch_Internal(pc->dm, patch->patchSection, cellIS, 0.0, x, NULL, F, ctx);CHKERRQ(ierr);
174192d50984SMatthew G. Knepley   PetscFunctionReturn(0);
174292d50984SMatthew G. Knepley }
174392d50984SMatthew G. Knepley 
174492d50984SMatthew G. Knepley PetscErrorCode PCPatchComputeFunction_Internal(PC pc, Vec x, Vec F, PetscInt point)
174592d50984SMatthew G. Knepley {
174692d50984SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
174792d50984SMatthew G. Knepley   const PetscInt *dofsArray;
17480904074fSPatrick Farrell   const PetscInt *dofsArrayWithAll;
174992d50984SMatthew G. Knepley   const PetscInt *cellsArray;
175092d50984SMatthew G. Knepley   PetscInt        ncell, offset, pStart, pEnd;
175192d50984SMatthew G. Knepley   PetscErrorCode  ierr;
175292d50984SMatthew G. Knepley 
175392d50984SMatthew G. Knepley   PetscFunctionBegin;
175492d50984SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
175592d50984SMatthew G. Knepley   if (!patch->usercomputeop) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback\n");
175692d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
17570904074fSPatrick Farrell   ierr = ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
175892d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
175992d50984SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
176092d50984SMatthew G. Knepley 
176192d50984SMatthew G. Knepley   point += pStart;
176292d50984SMatthew 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);
176392d50984SMatthew G. Knepley 
176492d50984SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
176592d50984SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
176692d50984SMatthew G. Knepley   if (ncell <= 0) {
176792d50984SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
176892d50984SMatthew G. Knepley     PetscFunctionReturn(0);
176992d50984SMatthew G. Knepley   }
177092d50984SMatthew G. Knepley   PetscStackPush("PCPatch user callback");
177192d50984SMatthew G. Knepley   /* Cannot reuse the same IS because the geometry info is being cached in it */
177292d50984SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS);CHKERRQ(ierr);
177339fd2e8aSPatrick Farrell   ierr = patch->usercomputef(pc, point, x, F, patch->cellIS, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell,
17740904074fSPatrick Farrell                                                                                             dofsArrayWithAll + offset*patch->totalDofsPerCell,
177539fd2e8aSPatrick Farrell                                                                                             patch->usercomputefctx);CHKERRQ(ierr);
177692d50984SMatthew G. Knepley   PetscStackPop;
177792d50984SMatthew G. Knepley   ierr = ISDestroy(&patch->cellIS);CHKERRQ(ierr);
177892d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
17790904074fSPatrick Farrell   ierr = ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
178092d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
178192d50984SMatthew G. Knepley   if (patch->viewMatrix) {
178292d50984SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
178392d50984SMatthew G. Knepley 
178492d50984SMatthew G. Knepley     ierr = PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch vector for Point %D", point);CHKERRQ(ierr);
178592d50984SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) F, name);CHKERRQ(ierr);
178692d50984SMatthew G. Knepley     ierr = ObjectView((PetscObject) F, patch->viewerMatrix, patch->formatMatrix);CHKERRQ(ierr);
178792d50984SMatthew G. Knepley   }
178892d50984SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
178992d50984SMatthew G. Knepley   PetscFunctionReturn(0);
179092d50984SMatthew G. Knepley }
179192d50984SMatthew G. Knepley 
17920904074fSPatrick 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)
17935f824522SMatthew G. Knepley {
17945f824522SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
17955f824522SMatthew G. Knepley   DM              dm;
17965f824522SMatthew G. Knepley   PetscSection    s;
17975f824522SMatthew G. Knepley   const PetscInt *parray, *oarray;
17985f824522SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
17995f824522SMatthew G. Knepley   PetscErrorCode  ierr;
18005f824522SMatthew G. Knepley 
18015f824522SMatthew G. Knepley   PetscFunctionBegin;
18025f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
18035f824522SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
18045f824522SMatthew G. Knepley   /* Set offset into patch */
18055f824522SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->pointCounts, patchNum, &Np);CHKERRQ(ierr);
18065f824522SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->pointCounts, patchNum, &poff);CHKERRQ(ierr);
18075f824522SMatthew G. Knepley   ierr = ISGetIndices(patch->points, &parray);CHKERRQ(ierr);
18085f824522SMatthew G. Knepley   ierr = ISGetIndices(patch->offs,   &oarray);CHKERRQ(ierr);
18095f824522SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
18105f824522SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
18115f824522SMatthew G. Knepley       const PetscInt point = parray[poff+p];
18125f824522SMatthew G. Knepley       PetscInt       dof;
18135f824522SMatthew G. Knepley 
18145f824522SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(patch->patchSection, point, f, &dof);CHKERRQ(ierr);
18155f824522SMatthew G. Knepley       ierr = PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);
18165f824522SMatthew G. Knepley       if (patch->nsubspaces == 1) {ierr = PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);}
18175f824522SMatthew G. Knepley       else                        {ierr = PetscSectionSetOffset(patch->patchSection, point, -1);CHKERRQ(ierr);}
18185f824522SMatthew G. Knepley     }
18195f824522SMatthew G. Knepley   }
18205f824522SMatthew G. Knepley   ierr = ISRestoreIndices(patch->points, &parray);CHKERRQ(ierr);
18215f824522SMatthew G. Knepley   ierr = ISRestoreIndices(patch->offs,   &oarray);CHKERRQ(ierr);
18225f824522SMatthew G. Knepley   if (patch->viewSection) {ierr = ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection);CHKERRQ(ierr);}
18235f824522SMatthew G. Knepley   /* TODO Shut off MatViewFromOptions() in MatAssemblyEnd() here */
1824723f9013SMatthew G. Knepley   ierr = DMPlexComputeJacobian_Patch_Internal(pc->dm, patch->patchSection, patch->patchSection, cellIS, 0.0, 0.0, x, NULL, J, J, ctx);CHKERRQ(ierr);
18255f824522SMatthew G. Knepley   PetscFunctionReturn(0);
18265f824522SMatthew G. Knepley }
18275f824522SMatthew G. Knepley 
182834d8b122SPatrick Farrell PetscErrorCode PCPatchComputeOperator_Internal(PC pc, Vec x, Mat mat, PetscInt point, PetscBool withArtificial)
18294bbf5ea8SMatthew G. Knepley {
18304bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
18314bbf5ea8SMatthew G. Knepley   const PetscInt *dofsArray;
18324d04e9f1SPatrick Farrell   const PetscInt *dofsArrayWithArtificial = NULL;
18330904074fSPatrick Farrell   const PetscInt *dofsArrayWithAll = NULL;
18344bbf5ea8SMatthew G. Knepley   const PetscInt *cellsArray;
1835*eb62eeaaSLawrence Mitchell   PetscInt        ncell, offset, pStart, pEnd, numIntFacets, intFacetOffset;
18364d04e9f1SPatrick Farrell   PetscBool       isNonlinear;
18374bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
18384bbf5ea8SMatthew G. Knepley 
18394bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
18404bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
1841debbdec3SPatrick Farrell   isNonlinear = patch->isNonlinear;
18424bbf5ea8SMatthew G. Knepley   if (!patch->usercomputeop) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback\n");
1843c2e6f3c0SFlorian Wechsung   if(withArtificial) {
1844c2e6f3c0SFlorian Wechsung     ierr = ISGetIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1845c2e6f3c0SFlorian Wechsung   } else {
18464bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1847c2e6f3c0SFlorian Wechsung   }
18484d04e9f1SPatrick Farrell   if (isNonlinear) {
18490904074fSPatrick Farrell     ierr = ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
18504d04e9f1SPatrick Farrell   }
18514bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
18524bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
18534bbf5ea8SMatthew G. Knepley 
18544bbf5ea8SMatthew G. Knepley   point += pStart;
18554bbf5ea8SMatthew 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);
18564bbf5ea8SMatthew G. Knepley 
18574bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
18584bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
18594bbf5ea8SMatthew G. Knepley   if (ncell <= 0) {
18604bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
18614bbf5ea8SMatthew G. Knepley     PetscFunctionReturn(0);
18624bbf5ea8SMatthew G. Knepley   }
18634bbf5ea8SMatthew G. Knepley   PetscStackPush("PCPatch user callback");
18642aa6f319SMatthew G. Knepley   /* Cannot reuse the same IS because the geometry info is being cached in it */
18652aa6f319SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS);CHKERRQ(ierr);
18660904074fSPatrick 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);
1867*eb62eeaaSLawrence Mitchell 
1868*eb62eeaaSLawrence Mitchell   ierr = PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets);CHKERRQ(ierr);
1869*eb62eeaaSLawrence Mitchell   ierr = PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset);CHKERRQ(ierr);
1870*eb62eeaaSLawrence Mitchell   if (numIntFacets > 0) {
1871*eb62eeaaSLawrence Mitchell     /* For each interior facet, grab the two cells (in local numbering, and concatenate dof numberings for those cells) */
1872*eb62eeaaSLawrence Mitchell     PetscInt *facetDofs = NULL, *facetDofsWithAll = NULL;
1873*eb62eeaaSLawrence Mitchell     const PetscInt *intFacetsArray = NULL;
1874*eb62eeaaSLawrence Mitchell     PetscInt idx = 0;
1875*eb62eeaaSLawrence Mitchell     PetscInt i, c, d;
1876*eb62eeaaSLawrence Mitchell     IS       facetIS = NULL;
1877*eb62eeaaSLawrence Mitchell     const PetscInt *facetCells = NULL;
1878*eb62eeaaSLawrence Mitchell     ierr = ISGetIndices(patch->intFacetsToPatchCell, &facetCells);CHKERRQ(ierr);
1879*eb62eeaaSLawrence Mitchell     ierr = ISGetIndices(patch->intFacets, &intFacetsArray);CHKERRQ(ierr);
1880*eb62eeaaSLawrence Mitchell     /* FIXME: Pull this malloc out. */
1881*eb62eeaaSLawrence Mitchell     ierr = PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofs);CHKERRQ(ierr);
1882*eb62eeaaSLawrence Mitchell     if (dofsArrayWithAll) {
1883*eb62eeaaSLawrence Mitchell       ierr = PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofsWithAll);CHKERRQ(ierr);
1884*eb62eeaaSLawrence Mitchell     }
1885*eb62eeaaSLawrence Mitchell     /*
1886*eb62eeaaSLawrence Mitchell      * 0--1
1887*eb62eeaaSLawrence Mitchell      * |\-|
1888*eb62eeaaSLawrence Mitchell      * |+\|
1889*eb62eeaaSLawrence Mitchell      * 2--3
1890*eb62eeaaSLawrence Mitchell      * [0, 2, 3, 0, 1, 3]
1891*eb62eeaaSLawrence Mitchell      */
1892*eb62eeaaSLawrence Mitchell     for (i = 0; i < numIntFacets; i++) {
1893*eb62eeaaSLawrence Mitchell       for (c = 0; c < 2; c++) {
1894*eb62eeaaSLawrence Mitchell         const PetscInt cell = facetCells[2*(intFacetOffset + i) + c];
1895*eb62eeaaSLawrence Mitchell         for (d = 0; d < patch->totalDofsPerCell; d++) {
1896*eb62eeaaSLawrence Mitchell           facetDofs[idx] = dofsArray[(offset + cell)*patch->totalDofsPerCell + d];
1897*eb62eeaaSLawrence Mitchell           if (dofsArrayWithAll) {
1898*eb62eeaaSLawrence Mitchell             facetDofsWithAll[idx] = dofsArrayWithAll[(offset + cell)*patch->totalDofsPerCell + d];
1899*eb62eeaaSLawrence Mitchell           }
1900*eb62eeaaSLawrence Mitchell           idx++;
1901*eb62eeaaSLawrence Mitchell         }
1902*eb62eeaaSLawrence Mitchell       }
1903*eb62eeaaSLawrence Mitchell     }
1904*eb62eeaaSLawrence Mitchell     ierr = ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray + intFacetOffset, PETSC_USE_POINTER, &facetIS);CHKERRQ(ierr);
1905*eb62eeaaSLawrence Mitchell     /*
1906*eb62eeaaSLawrence Mitchell      * ierr = patch->usercomputeopintfacet(pc, point, x, mat, facetIS, 2*numIntFacets*patch->totalDofsPerCell, facetDofs, facetDofsWithAll, patch->usercomputeopctx);CHKERRQ(ierr);
1907*eb62eeaaSLawrence Mitchell      */
1908*eb62eeaaSLawrence Mitchell     ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
1909*eb62eeaaSLawrence Mitchell     ierr = ISRestoreIndices(patch->intFacetsToPatchCell, &facetCells);CHKERRQ(ierr);
1910*eb62eeaaSLawrence Mitchell     ierr = PetscFree(facetDofs);CHKERRQ(ierr);
1911*eb62eeaaSLawrence Mitchell     ierr = PetscFree(facetDofsWithAll);CHKERRQ(ierr);
1912*eb62eeaaSLawrence Mitchell   }
19134bbf5ea8SMatthew G. Knepley   PetscStackPop;
19142aa6f319SMatthew G. Knepley   ierr = ISDestroy(&patch->cellIS);CHKERRQ(ierr);
19154d04e9f1SPatrick Farrell   if(withArtificial) {
1916c2e6f3c0SFlorian Wechsung     ierr = ISRestoreIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1917c2e6f3c0SFlorian Wechsung   } else {
19184bbf5ea8SMatthew G. Knepley     ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1919c2e6f3c0SFlorian Wechsung   }
19204d04e9f1SPatrick Farrell   if (isNonlinear) {
19210904074fSPatrick Farrell     ierr = ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
19224d04e9f1SPatrick Farrell   }
19234bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
19242aa6f319SMatthew G. Knepley   if (patch->viewMatrix) {
19252aa6f319SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
19262aa6f319SMatthew G. Knepley 
19272aa6f319SMatthew G. Knepley     ierr = PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch matrix for Point %D", point);CHKERRQ(ierr);
19282aa6f319SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) mat, name);CHKERRQ(ierr);
19292aa6f319SMatthew G. Knepley     ierr = ObjectView((PetscObject) mat, patch->viewerMatrix, patch->formatMatrix);CHKERRQ(ierr);
19302aa6f319SMatthew G. Knepley   }
19314bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
19324bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
19334bbf5ea8SMatthew G. Knepley }
19344bbf5ea8SMatthew G. Knepley 
19350904074fSPatrick Farrell PetscErrorCode PCPatch_ScatterLocal_Private(PC pc, PetscInt p, Vec x, Vec y, InsertMode mode, ScatterMode scat, PatchScatterType scattertype)
19364bbf5ea8SMatthew G. Knepley {
19374bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch     = (PC_PATCH *) pc->data;
19384bbf5ea8SMatthew G. Knepley   const PetscScalar *xArray    = NULL;
19394bbf5ea8SMatthew G. Knepley   PetscScalar       *yArray    = NULL;
19404bbf5ea8SMatthew G. Knepley   const PetscInt    *gtolArray = NULL;
19414bbf5ea8SMatthew G. Knepley   PetscInt           dof, offset, lidx;
19424bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
19434bbf5ea8SMatthew G. Knepley 
19444bbf5ea8SMatthew G. Knepley   PetscFunctionBeginHot;
19454bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Scatter, pc, 0, 0, 0);CHKERRQ(ierr);
19464bbf5ea8SMatthew G. Knepley   ierr = VecGetArrayRead(x, &xArray);CHKERRQ(ierr);
19474bbf5ea8SMatthew G. Knepley   ierr = VecGetArray(y, &yArray);CHKERRQ(ierr);
19480904074fSPatrick Farrell   if (scattertype == SCATTER_WITHARTIFICIAL) {
1949c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof);CHKERRQ(ierr);
1950c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offset);CHKERRQ(ierr);
1951c2e6f3c0SFlorian Wechsung     ierr = ISGetIndices(patch->gtolWithArtificial, &gtolArray);CHKERRQ(ierr);
19520904074fSPatrick Farrell   } else if (scattertype == SCATTER_WITHALL) {
19530904074fSPatrick Farrell     ierr = PetscSectionGetDof(patch->gtolCountsWithAll, p, &dof);CHKERRQ(ierr);
19540904074fSPatrick Farrell     ierr = PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offset);CHKERRQ(ierr);
19550904074fSPatrick Farrell     ierr = ISGetIndices(patch->gtolWithAll, &gtolArray);CHKERRQ(ierr);
1956c2e6f3c0SFlorian Wechsung   } else {
19574bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr);
19584bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
19594bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
1960c2e6f3c0SFlorian Wechsung   }
19614bbf5ea8SMatthew 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");
19624bbf5ea8SMatthew 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");
19634bbf5ea8SMatthew G. Knepley   for (lidx = 0; lidx < dof; ++lidx) {
19644bbf5ea8SMatthew G. Knepley     const PetscInt gidx = gtolArray[offset+lidx];
19654bbf5ea8SMatthew G. Knepley 
19664bbf5ea8SMatthew G. Knepley     if (mode == INSERT_VALUES) yArray[lidx]  = xArray[gidx]; /* Forward */
19674bbf5ea8SMatthew G. Knepley     else                       yArray[gidx] += xArray[lidx]; /* Reverse */
19684bbf5ea8SMatthew G. Knepley   }
19690904074fSPatrick Farrell   if (scattertype == SCATTER_WITHARTIFICIAL) {
1970c2e6f3c0SFlorian Wechsung     ierr = ISRestoreIndices(patch->gtolWithArtificial, &gtolArray);CHKERRQ(ierr);
19710904074fSPatrick Farrell   } else if (scattertype == SCATTER_WITHALL) {
19720904074fSPatrick Farrell     ierr = ISRestoreIndices(patch->gtolWithAll, &gtolArray);CHKERRQ(ierr);
1973c2e6f3c0SFlorian Wechsung   } else {
19744bbf5ea8SMatthew G. Knepley     ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
1975c2e6f3c0SFlorian Wechsung   }
19764bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArrayRead(x, &xArray);CHKERRQ(ierr);
19774bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArray(y, &yArray);CHKERRQ(ierr);
19784bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Scatter, pc, 0, 0, 0);CHKERRQ(ierr);
19794bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
19804bbf5ea8SMatthew G. Knepley }
19814bbf5ea8SMatthew G. Knepley 
1982dadc69c5SMatthew G. Knepley static PetscErrorCode PCSetUp_PATCH_Linear(PC pc)
1983dadc69c5SMatthew G. Knepley {
1984dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
1985dadc69c5SMatthew G. Knepley   const char    *prefix;
1986dadc69c5SMatthew G. Knepley   PetscInt       i;
1987dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
1988dadc69c5SMatthew G. Knepley 
1989dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
1990dadc69c5SMatthew G. Knepley   if (!pc->setupcalled) {
1991dadc69c5SMatthew G. Knepley     ierr = PetscMalloc1(patch->npatch, &patch->solver);CHKERRQ(ierr);
1992dadc69c5SMatthew G. Knepley     ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr);
1993dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {
1994dadc69c5SMatthew G. Knepley       KSP ksp;
1995dadc69c5SMatthew G. Knepley       PC  subpc;
1996dadc69c5SMatthew G. Knepley 
1997dadc69c5SMatthew G. Knepley       ierr = KSPCreate(PETSC_COMM_SELF, &ksp);CHKERRQ(ierr);
1998ddad275aSPatrick Farrell       ierr = KSPSetErrorIfNotConverged(ksp, pc->erroriffailure);CHKERRQ(ierr);
1999dadc69c5SMatthew G. Knepley       ierr = KSPSetOptionsPrefix(ksp, prefix);CHKERRQ(ierr);
2000dadc69c5SMatthew G. Knepley       ierr = KSPAppendOptionsPrefix(ksp, "sub_");CHKERRQ(ierr);
2001dadc69c5SMatthew G. Knepley       ierr = PetscObjectIncrementTabLevel((PetscObject) ksp, (PetscObject) pc, 1);CHKERRQ(ierr);
2002dadc69c5SMatthew G. Knepley       ierr = KSPGetPC(ksp, &subpc);CHKERRQ(ierr);
2003dadc69c5SMatthew G. Knepley       ierr = PetscObjectIncrementTabLevel((PetscObject) subpc, (PetscObject) pc, 1);CHKERRQ(ierr);
2004dadc69c5SMatthew G. Knepley       ierr = PetscLogObjectParent((PetscObject) pc, (PetscObject) ksp);CHKERRQ(ierr);
2005dadc69c5SMatthew G. Knepley       patch->solver[i] = (PetscObject) ksp;
2006dadc69c5SMatthew G. Knepley     }
2007dadc69c5SMatthew G. Knepley   }
2008dadc69c5SMatthew G. Knepley   if (patch->save_operators) {
2009dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {
2010dadc69c5SMatthew G. Knepley       ierr = MatZeroEntries(patch->mat[i]);CHKERRQ(ierr);
201134d8b122SPatrick Farrell       ierr = PCPatchComputeOperator_Internal(pc, NULL, patch->mat[i], i, PETSC_FALSE);CHKERRQ(ierr);
2012dadc69c5SMatthew G. Knepley       ierr = KSPSetOperators((KSP) patch->solver[i], patch->mat[i], patch->mat[i]);CHKERRQ(ierr);
2013dadc69c5SMatthew G. Knepley     }
2014dadc69c5SMatthew G. Knepley   }
201534d8b122SPatrick Farrell   if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
201634d8b122SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {
20171202d238SPatrick Farrell       /* Instead of padding patch->patchUpdate with zeros to get */
20181202d238SPatrick Farrell       /* patch->patchUpdateWithArtificial and then multiplying with the matrix, */
201934d8b122SPatrick Farrell       /* just get rid of the columns that correspond to the dofs with */
202034d8b122SPatrick Farrell       /* artificial bcs. That's of course fairly inefficient, hopefully we */
202134d8b122SPatrick Farrell       /* can just assemble the rectangular matrix in the first place. */
202234d8b122SPatrick Farrell       Mat matSquare;
202334d8b122SPatrick Farrell       IS rowis;
202434d8b122SPatrick Farrell       PetscInt dof;
202534d8b122SPatrick Farrell 
202634d8b122SPatrick Farrell       ierr = MatGetSize(patch->mat[i], &dof, NULL);CHKERRQ(ierr);
202734d8b122SPatrick Farrell       if (dof == 0) {
202834d8b122SPatrick Farrell         patch->matWithArtificial[i] = NULL;
202934d8b122SPatrick Farrell         continue;
203034d8b122SPatrick Farrell       }
203134d8b122SPatrick Farrell 
203234d8b122SPatrick Farrell       ierr = PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE);CHKERRQ(ierr);
203334d8b122SPatrick Farrell       ierr = MatZeroEntries(matSquare);CHKERRQ(ierr);
203434d8b122SPatrick Farrell       ierr = PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE);CHKERRQ(ierr);
203534d8b122SPatrick Farrell 
203634d8b122SPatrick Farrell       ierr = MatGetSize(matSquare, &dof, NULL);CHKERRQ(ierr);
203734d8b122SPatrick Farrell       ierr = ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis); CHKERRQ(ierr);
203834d8b122SPatrick Farrell       if(pc->setupcalled) {
203934d8b122SPatrick Farrell         ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_REUSE_MATRIX, &patch->matWithArtificial[i]); CHKERRQ(ierr);
204034d8b122SPatrick Farrell       } else {
204134d8b122SPatrick Farrell         ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &patch->matWithArtificial[i]); CHKERRQ(ierr);
204234d8b122SPatrick Farrell       }
204334d8b122SPatrick Farrell       ierr = ISDestroy(&rowis); CHKERRQ(ierr);
204434d8b122SPatrick Farrell       ierr = MatDestroy(&matSquare);CHKERRQ(ierr);
204534d8b122SPatrick Farrell     }
204634d8b122SPatrick Farrell   }
2047dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
2048dadc69c5SMatthew G. Knepley }
2049dadc69c5SMatthew G. Knepley 
20504bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUp_PATCH(PC pc)
20514bbf5ea8SMatthew G. Knepley {
20524bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2053557beb66SLawrence Mitchell   PetscInt       i;
205439fd2e8aSPatrick Farrell   PetscBool       isNonlinear;
20554bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
20564bbf5ea8SMatthew G. Knepley 
20574bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
20584bbf5ea8SMatthew G. Knepley   if (!pc->setupcalled) {
20594bbf5ea8SMatthew G. Knepley     PetscInt pStart, pEnd, p;
20604bbf5ea8SMatthew G. Knepley     PetscInt localSize;
20614bbf5ea8SMatthew G. Knepley 
20624bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventBegin(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr);
20634bbf5ea8SMatthew G. Knepley 
2064debbdec3SPatrick Farrell     isNonlinear = patch->isNonlinear;
20655f824522SMatthew G. Knepley     if (!patch->nsubspaces) {
20665f824522SMatthew G. Knepley       DM           dm;
20675f824522SMatthew G. Knepley       PetscDS      prob;
20685f824522SMatthew G. Knepley       PetscSection s;
2069e72c1634SMatthew G. Knepley       PetscInt     cStart, cEnd, c, Nf, f, numGlobalBcs = 0, *globalBcs, *Nb, totNb = 0, **cellDofs;
20705f824522SMatthew G. Knepley 
20715f824522SMatthew G. Knepley       ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
20725f824522SMatthew G. Knepley       if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONG, "Must set DM for PCPATCH or call PCPatchSetDiscretisationInfo()");
20735f824522SMatthew G. Knepley       ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
20745f824522SMatthew G. Knepley       ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
20755f824522SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
20765f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
20775f824522SMatthew G. Knepley         PetscInt cdof;
20785f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
20795f824522SMatthew G. Knepley         numGlobalBcs += cdof;
20805f824522SMatthew G. Knepley       }
20815f824522SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
20825f824522SMatthew G. Knepley       ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
20835f824522SMatthew G. Knepley       ierr = PetscMalloc3(Nf, &Nb, Nf, &cellDofs, numGlobalBcs, &globalBcs);CHKERRQ(ierr);
20845f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
20855f824522SMatthew G. Knepley         PetscFE        fe;
20865f824522SMatthew G. Knepley         PetscDualSpace sp;
20875f824522SMatthew G. Knepley         PetscInt       cdoff = 0;
20885f824522SMatthew G. Knepley 
20895f824522SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
20905f824522SMatthew G. Knepley         /* ierr = PetscFEGetNumComponents(fe, &Nc[f]);CHKERRQ(ierr); */
20915f824522SMatthew G. Knepley         ierr = PetscFEGetDualSpace(fe, &sp);CHKERRQ(ierr);
20925f824522SMatthew G. Knepley         ierr = PetscDualSpaceGetDimension(sp, &Nb[f]);CHKERRQ(ierr);
20935f824522SMatthew G. Knepley         totNb += Nb[f];
20945f824522SMatthew G. Knepley 
20955f824522SMatthew G. Knepley         ierr = PetscMalloc1((cEnd-cStart)*Nb[f], &cellDofs[f]);CHKERRQ(ierr);
20965f824522SMatthew G. Knepley         for (c = cStart; c < cEnd; ++c) {
20975f824522SMatthew G. Knepley           PetscInt *closure = NULL;
20985f824522SMatthew G. Knepley           PetscInt  clSize  = 0, cl;
20995f824522SMatthew G. Knepley 
21005f824522SMatthew G. Knepley           ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
21015f824522SMatthew G. Knepley           for (cl = 0; cl < clSize*2; cl += 2) {
21025f824522SMatthew G. Knepley             const PetscInt p = closure[cl];
21035f824522SMatthew G. Knepley             PetscInt       fdof, d, foff;
21045f824522SMatthew G. Knepley 
21055f824522SMatthew G. Knepley             ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr);
21065f824522SMatthew G. Knepley             ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr);
21075f824522SMatthew G. Knepley             for (d = 0; d < fdof; ++d, ++cdoff) cellDofs[f][cdoff] = foff + d;
21085f824522SMatthew G. Knepley           }
21095f824522SMatthew G. Knepley           ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
21105f824522SMatthew G. Knepley         }
21115f824522SMatthew 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]);
21125f824522SMatthew G. Knepley       }
21135f824522SMatthew G. Knepley       numGlobalBcs = 0;
21145f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
21155f824522SMatthew G. Knepley         const PetscInt *ind;
21165f824522SMatthew G. Knepley         PetscInt        off, cdof, d;
21175f824522SMatthew G. Knepley 
21185f824522SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
21195f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
21205f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &ind);CHKERRQ(ierr);
21215f824522SMatthew G. Knepley         for (d = 0; d < cdof; ++d) globalBcs[numGlobalBcs++] = off + ind[d];
21225f824522SMatthew G. Knepley       }
21235f824522SMatthew G. Knepley 
21245f824522SMatthew G. Knepley       ierr = PCPatchSetDiscretisationInfoCombined(pc, dm, Nb, (const PetscInt **) cellDofs, numGlobalBcs, globalBcs, numGlobalBcs, globalBcs);CHKERRQ(ierr);
21255f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
21265f824522SMatthew G. Knepley         ierr = PetscFree(cellDofs[f]);CHKERRQ(ierr);
21275f824522SMatthew G. Knepley       }
21285f824522SMatthew G. Knepley       ierr = PetscFree3(Nb, cellDofs, globalBcs);CHKERRQ(ierr);
212992d50984SMatthew G. Knepley       ierr = PCPatchSetComputeFunction(pc, PCPatchComputeFunction_DMPlex_Private, NULL);CHKERRQ(ierr);
21305f824522SMatthew G. Knepley       ierr = PCPatchSetComputeOperator(pc, PCPatchComputeOperator_DMPlex_Private, NULL);CHKERRQ(ierr);
21315f824522SMatthew G. Knepley     }
21325f824522SMatthew G. Knepley 
21334bbf5ea8SMatthew G. Knepley     localSize = patch->subspaceOffsets[patch->nsubspaces];
21341202d238SPatrick Farrell     ierr = VecCreateSeq(PETSC_COMM_SELF, localSize, &patch->localRHS);CHKERRQ(ierr);
21351202d238SPatrick Farrell     ierr = VecSetUp(patch->localRHS);CHKERRQ(ierr);
21361202d238SPatrick Farrell     ierr = VecDuplicate(patch->localRHS, &patch->localUpdate);CHKERRQ(ierr);
21374bbf5ea8SMatthew G. Knepley     ierr = PCPatchCreateCellPatches(pc);CHKERRQ(ierr);
21384bbf5ea8SMatthew G. Knepley     ierr = PCPatchCreateCellPatchDiscretisationInfo(pc);CHKERRQ(ierr);
21394bbf5ea8SMatthew G. Knepley 
21404bbf5ea8SMatthew G. Knepley     /* OK, now build the work vectors */
21414bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, &pEnd);CHKERRQ(ierr);
21421202d238SPatrick Farrell     ierr = PetscMalloc1(patch->npatch, &patch->patchRHS);CHKERRQ(ierr);
21431202d238SPatrick Farrell     ierr = PetscMalloc1(patch->npatch, &patch->patchUpdate);CHKERRQ(ierr);
2144c2e6f3c0SFlorian Wechsung 
214561c4b389SFlorian Wechsung     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
21461202d238SPatrick Farrell       ierr = PetscMalloc1(patch->npatch, &patch->patchRHSWithArtificial);CHKERRQ(ierr);
2147c2e6f3c0SFlorian Wechsung       ierr = PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithArtificial);CHKERRQ(ierr);
2148c2e6f3c0SFlorian Wechsung     }
21490904074fSPatrick Farrell     if (isNonlinear) {
21500904074fSPatrick Farrell       ierr = PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithAll);CHKERRQ(ierr);
21510904074fSPatrick Farrell     }
21524bbf5ea8SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
21534bbf5ea8SMatthew G. Knepley       PetscInt dof;
21544bbf5ea8SMatthew G. Knepley 
21554bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr);
21561202d238SPatrick Farrell       ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchRHS[p-pStart]);CHKERRQ(ierr);
21571202d238SPatrick Farrell       ierr = VecSetUp(patch->patchRHS[p-pStart]);CHKERRQ(ierr);
21581202d238SPatrick Farrell       ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchUpdate[p-pStart]);CHKERRQ(ierr);
21591202d238SPatrick Farrell       ierr = VecSetUp(patch->patchUpdate[p-pStart]);CHKERRQ(ierr);
21600904074fSPatrick Farrell       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
21613bb0e8f7SKarl Rupp         const PetscInt    *gtolArray, *gtolArrayWithArtificial = NULL;
21623bb0e8f7SKarl Rupp         PetscInt           numPatchDofs, offset;
21633bb0e8f7SKarl Rupp         PetscInt           numPatchDofsWithArtificial, offsetWithArtificial;
21643bb0e8f7SKarl Rupp         PetscInt           dofWithoutArtificialCounter = 0;
21653bb0e8f7SKarl Rupp         PetscInt          *patchWithoutArtificialToWithArtificialArray;
21663bb0e8f7SKarl Rupp 
2167c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof);CHKERRQ(ierr);
21681202d238SPatrick Farrell         ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchRHSWithArtificial[p-pStart]);CHKERRQ(ierr);
21691202d238SPatrick Farrell         ierr = VecSetUp(patch->patchRHSWithArtificial[p-pStart]);CHKERRQ(ierr);
2170c2e6f3c0SFlorian Wechsung 
2171e047a90bSFlorian Wechsung         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
2172e047a90bSFlorian Wechsung         /* the index in the patch with all dofs */
2173c2e6f3c0SFlorian Wechsung         ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
217463deea8eSPatrick Farrell 
2175c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs);CHKERRQ(ierr);
217647aca4a6SPatrick Farrell         if (numPatchDofs == 0) {
217747aca4a6SPatrick Farrell           patch->dofMappingWithoutToWithArtificial[p-pStart] = NULL;
217847aca4a6SPatrick Farrell           continue;
217947aca4a6SPatrick Farrell         }
218063deea8eSPatrick Farrell 
2181c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
2182c2e6f3c0SFlorian Wechsung         ierr = ISGetIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial);CHKERRQ(ierr);
2183c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &numPatchDofsWithArtificial);CHKERRQ(ierr);
2184c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offsetWithArtificial);CHKERRQ(ierr);
2185c2e6f3c0SFlorian Wechsung 
2186c2e6f3c0SFlorian Wechsung         ierr = PetscMalloc1(numPatchDofs, &patchWithoutArtificialToWithArtificialArray);CHKERRQ(ierr);
2187b0c21b6aSKarl Rupp         for (i=0; i<numPatchDofsWithArtificial; i++) {
2188e047a90bSFlorian Wechsung           if (gtolArrayWithArtificial[i+offsetWithArtificial] == gtolArray[offset+dofWithoutArtificialCounter]) {
2189c2e6f3c0SFlorian Wechsung             patchWithoutArtificialToWithArtificialArray[dofWithoutArtificialCounter] = i;
2190c2e6f3c0SFlorian Wechsung             dofWithoutArtificialCounter++;
2191c2e6f3c0SFlorian Wechsung             if (dofWithoutArtificialCounter == numPatchDofs)
2192c2e6f3c0SFlorian Wechsung               break;
2193c2e6f3c0SFlorian Wechsung           }
2194c2e6f3c0SFlorian Wechsung         }
2195168bb5e4SPatrick Farrell         ierr = ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutArtificialToWithArtificialArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithArtificial[p-pStart]);CHKERRQ(ierr);
2196c2e6f3c0SFlorian Wechsung         ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
2197c2e6f3c0SFlorian Wechsung         ierr = ISRestoreIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial);CHKERRQ(ierr);
2198c2e6f3c0SFlorian Wechsung       }
21990904074fSPatrick Farrell       if (isNonlinear) {
22000904074fSPatrick Farrell         const PetscInt    *gtolArray, *gtolArrayWithAll = NULL;
22010904074fSPatrick Farrell         PetscInt           numPatchDofs, offset;
22020904074fSPatrick Farrell         PetscInt           numPatchDofsWithAll, offsetWithAll;
22030904074fSPatrick Farrell         PetscInt           dofWithoutAllCounter = 0;
22040904074fSPatrick Farrell         PetscInt          *patchWithoutAllToWithAllArray;
22050904074fSPatrick Farrell 
22060904074fSPatrick Farrell         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
22070904074fSPatrick Farrell         /* the index in the patch with all dofs */
22080904074fSPatrick Farrell         ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
22090904074fSPatrick Farrell 
22100904074fSPatrick Farrell         ierr = PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs);CHKERRQ(ierr);
221147aca4a6SPatrick Farrell         if (numPatchDofs == 0) {
2212b88cb22dSPatrick Farrell           patch->dofMappingWithoutToWithAll[p-pStart] = NULL;
221347aca4a6SPatrick Farrell           continue;
221447aca4a6SPatrick Farrell         }
22150904074fSPatrick Farrell 
22160904074fSPatrick Farrell         ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
22170904074fSPatrick Farrell         ierr = ISGetIndices(patch->gtolWithAll, &gtolArrayWithAll);CHKERRQ(ierr);
22180904074fSPatrick Farrell         ierr = PetscSectionGetDof(patch->gtolCountsWithAll, p, &numPatchDofsWithAll);CHKERRQ(ierr);
22190904074fSPatrick Farrell         ierr = PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offsetWithAll);CHKERRQ(ierr);
22200904074fSPatrick Farrell 
22210904074fSPatrick Farrell         ierr = PetscMalloc1(numPatchDofs, &patchWithoutAllToWithAllArray);CHKERRQ(ierr);
22220904074fSPatrick Farrell 
22230904074fSPatrick Farrell         for (i=0; i<numPatchDofsWithAll; i++) {
22240904074fSPatrick Farrell           if (gtolArrayWithAll[i+offsetWithAll] == gtolArray[offset+dofWithoutAllCounter]) {
22250904074fSPatrick Farrell             patchWithoutAllToWithAllArray[dofWithoutAllCounter] = i;
22260904074fSPatrick Farrell             dofWithoutAllCounter++;
22270904074fSPatrick Farrell             if (dofWithoutAllCounter == numPatchDofs)
22280904074fSPatrick Farrell               break;
22290904074fSPatrick Farrell           }
22300904074fSPatrick Farrell         }
2231168bb5e4SPatrick Farrell         ierr = ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutAllToWithAllArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithAll[p-pStart]);CHKERRQ(ierr);
22320904074fSPatrick Farrell         ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
22330904074fSPatrick Farrell         ierr = ISRestoreIndices(patch->gtolWithAll, &gtolArrayWithAll);CHKERRQ(ierr);
22340904074fSPatrick Farrell       }
22354bbf5ea8SMatthew G. Knepley     }
22364bbf5ea8SMatthew G. Knepley     if (patch->save_operators) {
22374bbf5ea8SMatthew G. Knepley       ierr = PetscMalloc1(patch->npatch, &patch->mat);CHKERRQ(ierr);
22384bbf5ea8SMatthew G. Knepley       for (i = 0; i < patch->npatch; ++i) {
2239c2e6f3c0SFlorian Wechsung         ierr = PCPatchCreateMatrix_Private(pc, i, &patch->mat[i], PETSC_FALSE);CHKERRQ(ierr);
22404bbf5ea8SMatthew G. Knepley       }
22414bbf5ea8SMatthew G. Knepley     }
22424bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr);
22434bbf5ea8SMatthew G. Knepley 
22444bbf5ea8SMatthew G. Knepley     /* If desired, calculate weights for dof multiplicity */
22454bbf5ea8SMatthew G. Knepley     if (patch->partition_of_unity) {
22463bb0e8f7SKarl Rupp       PetscScalar *input = NULL;
22473bb0e8f7SKarl Rupp       PetscScalar *output = NULL;
22483bb0e8f7SKarl Rupp       Vec global;
22493bb0e8f7SKarl Rupp 
22501202d238SPatrick Farrell       ierr = VecDuplicate(patch->localRHS, &patch->dof_weights);CHKERRQ(ierr);
225161c4b389SFlorian Wechsung       if(patch->local_composition_type == PC_COMPOSITE_ADDITIVE) {
22524bbf5ea8SMatthew G. Knepley         for (i = 0; i < patch->npatch; ++i) {
22534bbf5ea8SMatthew G. Knepley           PetscInt dof;
22544bbf5ea8SMatthew G. Knepley 
22554bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &dof);CHKERRQ(ierr);
22564bbf5ea8SMatthew G. Knepley           if (dof <= 0) continue;
22571202d238SPatrick Farrell           ierr = VecSet(patch->patchRHS[i], 1.0);CHKERRQ(ierr);
22580904074fSPatrick Farrell           ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchRHS[i], patch->dof_weights, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR);CHKERRQ(ierr);
22594bbf5ea8SMatthew G. Knepley         }
2260c2e6f3c0SFlorian Wechsung       } else {
2261e047a90bSFlorian Wechsung         /* multiplicative is actually only locally multiplicative and globally additive. need the pou where the mesh decomposition overlaps */
2262c2e6f3c0SFlorian Wechsung         ierr = VecSet(patch->dof_weights, 1.0);CHKERRQ(ierr);
22634bbf5ea8SMatthew G. Knepley       }
2264d132cafaSFlorian Wechsung 
2265d132cafaSFlorian Wechsung       VecDuplicate(patch->dof_weights, &global);
2266d132cafaSFlorian Wechsung       VecSet(global, 0.);
2267d132cafaSFlorian Wechsung 
2268d132cafaSFlorian Wechsung       ierr = VecGetArray(patch->dof_weights, &input);CHKERRQ(ierr);
2269d132cafaSFlorian Wechsung       ierr = VecGetArray(global, &output);CHKERRQ(ierr);
2270d132cafaSFlorian Wechsung       ierr = PetscSFReduceBegin(patch->defaultSF, MPIU_SCALAR, input, output, MPI_SUM);CHKERRQ(ierr);
2271d132cafaSFlorian Wechsung       ierr = PetscSFReduceEnd(patch->defaultSF, MPIU_SCALAR, input, output, MPI_SUM);CHKERRQ(ierr);
2272d132cafaSFlorian Wechsung       ierr = VecRestoreArray(patch->dof_weights, &input);CHKERRQ(ierr);
2273d132cafaSFlorian Wechsung       ierr = VecRestoreArray(global, &output);CHKERRQ(ierr);
2274d132cafaSFlorian Wechsung 
227505528938SFlorian Wechsung       ierr = VecReciprocal(global);CHKERRQ(ierr);
2276d132cafaSFlorian Wechsung 
2277d132cafaSFlorian Wechsung       ierr = VecGetArray(patch->dof_weights, &output);CHKERRQ(ierr);
2278d132cafaSFlorian Wechsung       ierr = VecGetArray(global, &input);CHKERRQ(ierr);
2279d132cafaSFlorian Wechsung       ierr = PetscSFBcastBegin(patch->defaultSF, MPIU_SCALAR, input, output);CHKERRQ(ierr);
2280d132cafaSFlorian Wechsung       ierr = PetscSFBcastEnd(patch->defaultSF, MPIU_SCALAR, input, output);CHKERRQ(ierr);
2281d132cafaSFlorian Wechsung       ierr = VecRestoreArray(patch->dof_weights, &output);CHKERRQ(ierr);
2282d132cafaSFlorian Wechsung       ierr = VecRestoreArray(global, &input);CHKERRQ(ierr);
2283d132cafaSFlorian Wechsung       ierr = VecDestroy(&global);CHKERRQ(ierr);
22844bbf5ea8SMatthew G. Knepley     }
228561c4b389SFlorian Wechsung     if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE && patch->save_operators) {
228696b79ebeSFlorian Wechsung       ierr = PetscMalloc1(patch->npatch, &patch->matWithArtificial);CHKERRQ(ierr);
22874bbf5ea8SMatthew G. Knepley     }
22884bbf5ea8SMatthew G. Knepley   }
2289dadc69c5SMatthew G. Knepley   ierr = (*patch->setupsolver)(pc);CHKERRQ(ierr);
2290dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
22914bbf5ea8SMatthew G. Knepley }
2292dadc69c5SMatthew G. Knepley 
2293dadc69c5SMatthew G. Knepley static PetscErrorCode PCApply_PATCH_Linear(PC pc, PetscInt i, Vec x, Vec y)
2294dadc69c5SMatthew G. Knepley {
2295dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2296dadc69c5SMatthew G. Knepley   KSP            ksp   = (KSP) patch->solver[i];
2297dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2298dadc69c5SMatthew G. Knepley 
2299dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2300dadc69c5SMatthew G. Knepley   if (!patch->save_operators) {
2301dadc69c5SMatthew G. Knepley     Mat mat;
2302dadc69c5SMatthew G. Knepley 
230334d8b122SPatrick Farrell     ierr = PCPatchCreateMatrix_Private(pc, i, &mat, PETSC_FALSE);CHKERRQ(ierr);
2304dadc69c5SMatthew G. Knepley     /* Populate operator here. */
230534d8b122SPatrick Farrell     ierr = PCPatchComputeOperator_Internal(pc, NULL, mat, i, PETSC_FALSE);CHKERRQ(ierr);
2306dadc69c5SMatthew G. Knepley     ierr = KSPSetOperators(ksp, mat, mat);CHKERRQ(ierr);
2307dadc69c5SMatthew G. Knepley     /* Drop reference so the KSPSetOperators below will blow it away. */
2308dadc69c5SMatthew G. Knepley     ierr = MatDestroy(&mat);CHKERRQ(ierr);
2309dadc69c5SMatthew G. Knepley   }
2310dadc69c5SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr);
2311dadc69c5SMatthew G. Knepley   if (!ksp->setfromoptionscalled) {
2312dadc69c5SMatthew G. Knepley     ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
2313dadc69c5SMatthew G. Knepley   }
2314dadc69c5SMatthew G. Knepley   ierr = KSPSolve(ksp, x, y);CHKERRQ(ierr);
2315ddad275aSPatrick Farrell   ierr = KSPCheckSolve(ksp, pc, y);CHKERRQ(ierr);
2316dadc69c5SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr);
2317dadc69c5SMatthew G. Knepley   if (!patch->save_operators) {
2318dadc69c5SMatthew G. Knepley     PC pc;
2319dadc69c5SMatthew G. Knepley     ierr = KSPSetOperators(ksp, NULL, NULL);CHKERRQ(ierr);
2320dadc69c5SMatthew G. Knepley     ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
2321dadc69c5SMatthew G. Knepley     /* Destroy PC context too, otherwise the factored matrix hangs around. */
2322dadc69c5SMatthew G. Knepley     ierr = PCReset(pc);CHKERRQ(ierr);
23234bbf5ea8SMatthew G. Knepley   }
23244bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
23254bbf5ea8SMatthew G. Knepley }
23264bbf5ea8SMatthew G. Knepley 
23276c9c532dSPatrick Farrell static PetscErrorCode PCUpdateMultiplicative_PATCH_Linear(PC pc, PetscInt i, PetscInt pStart)
23286c9c532dSPatrick Farrell {
23296c9c532dSPatrick Farrell   PC_PATCH      *patch = (PC_PATCH *) pc->data;
23306c9c532dSPatrick Farrell   Mat multMat;
23316c9c532dSPatrick Farrell   PetscErrorCode ierr;
23326c9c532dSPatrick Farrell 
23334d04e9f1SPatrick Farrell   PetscFunctionBegin;
23344d04e9f1SPatrick Farrell 
23356c9c532dSPatrick Farrell   if (patch->save_operators) {
23366c9c532dSPatrick Farrell     multMat = patch->matWithArtificial[i];
23376c9c532dSPatrick Farrell   } else {
23386c9c532dSPatrick Farrell     /*Very inefficient, hopefully we can just assemble the rectangular matrix in the first place.*/
23396c9c532dSPatrick Farrell     Mat matSquare;
23406c9c532dSPatrick Farrell     PetscInt dof;
23416c9c532dSPatrick Farrell     IS rowis;
23426c9c532dSPatrick Farrell     ierr = PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE);CHKERRQ(ierr);
23436c9c532dSPatrick Farrell     ierr = MatZeroEntries(matSquare);CHKERRQ(ierr);
23446c9c532dSPatrick Farrell     ierr = PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE);CHKERRQ(ierr);
23456c9c532dSPatrick Farrell     ierr = MatGetSize(matSquare, &dof, NULL);CHKERRQ(ierr);
23466c9c532dSPatrick Farrell     ierr = ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis); CHKERRQ(ierr);
23476c9c532dSPatrick Farrell     ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &multMat); CHKERRQ(ierr);
23486c9c532dSPatrick Farrell     ierr = MatDestroy(&matSquare);CHKERRQ(ierr);
23496c9c532dSPatrick Farrell     ierr = ISDestroy(&rowis); CHKERRQ(ierr);
23506c9c532dSPatrick Farrell   }
23516c9c532dSPatrick Farrell   ierr = MatMult(multMat, patch->patchUpdate[i], patch->patchRHSWithArtificial[i]); CHKERRQ(ierr);
23526c9c532dSPatrick Farrell   ierr = VecScale(patch->patchRHSWithArtificial[i], -1.0); CHKERRQ(ierr);
23530904074fSPatrick Farrell   ierr = PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHSWithArtificial[i], patch->localRHS, ADD_VALUES, SCATTER_REVERSE, SCATTER_WITHARTIFICIAL); CHKERRQ(ierr);
23546c9c532dSPatrick Farrell   if (!patch->save_operators) {
23556c9c532dSPatrick Farrell     ierr = MatDestroy(&multMat); CHKERRQ(ierr);
23566c9c532dSPatrick Farrell   }
23574d04e9f1SPatrick Farrell   PetscFunctionReturn(0);
23586c9c532dSPatrick Farrell }
23596c9c532dSPatrick Farrell 
23604bbf5ea8SMatthew G. Knepley static PetscErrorCode PCApply_PATCH(PC pc, Vec x, Vec y)
23614bbf5ea8SMatthew G. Knepley {
23624bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch    = (PC_PATCH *) pc->data;
23631202d238SPatrick Farrell   const PetscScalar *globalRHS  = NULL;
23641202d238SPatrick Farrell   PetscScalar       *localRHS   = NULL;
23651202d238SPatrick Farrell   PetscScalar       *globalUpdate  = NULL;
23664bbf5ea8SMatthew G. Knepley   const PetscInt    *bcNodes  = NULL;
23674bbf5ea8SMatthew G. Knepley   PetscInt           nsweep   = patch->symmetrise_sweep ? 2 : 1;
23684bbf5ea8SMatthew G. Knepley   PetscInt           start[2] = {0, 0};
23694bbf5ea8SMatthew G. Knepley   PetscInt           end[2]   = {-1, -1};
23704bbf5ea8SMatthew G. Knepley   const PetscInt     inc[2]   = {1, -1};
23711202d238SPatrick Farrell   const PetscScalar *localUpdate;
23724bbf5ea8SMatthew G. Knepley   const PetscInt    *iterationSet;
23734bbf5ea8SMatthew G. Knepley   PetscInt           pStart, numBcs, n, sweep, bc, j;
23744bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
23754bbf5ea8SMatthew G. Knepley 
23764bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
23774bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr);
23784bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsPushGetViewerOff(PETSC_TRUE);CHKERRQ(ierr);
237992d50984SMatthew G. Knepley   /* start, end, inc have 2 entries to manage a second backward sweep if we symmetrize */
23804bbf5ea8SMatthew G. Knepley   end[0]   = patch->npatch;
23814bbf5ea8SMatthew G. Knepley   start[1] = patch->npatch-1;
23824bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
23834bbf5ea8SMatthew G. Knepley     ierr = ISGetLocalSize(patch->iterationSet, &end[0]);CHKERRQ(ierr);
23844bbf5ea8SMatthew G. Knepley     start[1] = end[0] - 1;
23854bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr);
23864bbf5ea8SMatthew G. Knepley   }
23874bbf5ea8SMatthew G. Knepley   /* Scatter from global space into overlapped local spaces */
23881202d238SPatrick Farrell   ierr = VecGetArrayRead(x, &globalRHS);CHKERRQ(ierr);
23891202d238SPatrick Farrell   ierr = VecGetArray(patch->localRHS, &localRHS);CHKERRQ(ierr);
23901202d238SPatrick Farrell   ierr = PetscSFBcastBegin(patch->defaultSF, MPIU_SCALAR, globalRHS, localRHS);CHKERRQ(ierr);
23911202d238SPatrick Farrell   ierr = PetscSFBcastEnd(patch->defaultSF, MPIU_SCALAR, globalRHS, localRHS);CHKERRQ(ierr);
23921202d238SPatrick Farrell   ierr = VecRestoreArrayRead(x, &globalRHS);CHKERRQ(ierr);
23931202d238SPatrick Farrell   ierr = VecRestoreArray(patch->localRHS, &localRHS);CHKERRQ(ierr);
23944bbf5ea8SMatthew G. Knepley 
23951202d238SPatrick Farrell   ierr = VecSet(patch->localUpdate, 0.0);CHKERRQ(ierr);
23964bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, NULL);CHKERRQ(ierr);
23974bbf5ea8SMatthew G. Knepley   for (sweep = 0; sweep < nsweep; sweep++) {
23984bbf5ea8SMatthew G. Knepley     for (j = start[sweep]; j*inc[sweep] < end[sweep]*inc[sweep]; j += inc[sweep]) {
23994bbf5ea8SMatthew G. Knepley       PetscInt i       = patch->user_patches ? iterationSet[j] : j;
24004bbf5ea8SMatthew G. Knepley       PetscInt start, len;
24014bbf5ea8SMatthew G. Knepley 
24024bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &len);CHKERRQ(ierr);
24034bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetOffset(patch->gtolCounts, i+pStart, &start);CHKERRQ(ierr);
24044bbf5ea8SMatthew G. Knepley       /* TODO: Squash out these guys in the setup as well. */
24054bbf5ea8SMatthew G. Knepley       if (len <= 0) continue;
24064bbf5ea8SMatthew G. Knepley       /* TODO: Do we need different scatters for X and Y? */
24070904074fSPatrick Farrell       ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->localRHS, patch->patchRHS[i], INSERT_VALUES, SCATTER_FORWARD, SCATTER_INTERIOR);CHKERRQ(ierr);
24081202d238SPatrick Farrell       ierr = (*patch->applysolver)(pc, i, patch->patchRHS[i], patch->patchUpdate[i]);CHKERRQ(ierr);
24090904074fSPatrick Farrell       ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchUpdate[i], patch->localUpdate, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR);CHKERRQ(ierr);
241061c4b389SFlorian Wechsung       if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
24116c9c532dSPatrick Farrell         ierr = (*patch->updatemultiplicative)(pc, i, pStart);CHKERRQ(ierr);
2412c2e6f3c0SFlorian Wechsung       }
24134bbf5ea8SMatthew G. Knepley     }
24144bbf5ea8SMatthew G. Knepley   }
24154bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {ierr = ISRestoreIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr);}
24164bbf5ea8SMatthew G. Knepley   /* XXX: should we do this on the global vector? */
241773ec7555SLawrence Mitchell   if (patch->partition_of_unity) {
24181202d238SPatrick Farrell     ierr = VecPointwiseMult(patch->localUpdate, patch->localUpdate, patch->dof_weights);CHKERRQ(ierr);
24194bbf5ea8SMatthew G. Knepley   }
24201202d238SPatrick Farrell   /* Now patch->localUpdate contains the solution of the patch solves, so we need to combine them all. */
24214bbf5ea8SMatthew G. Knepley   ierr = VecSet(y, 0.0);CHKERRQ(ierr);
24221202d238SPatrick Farrell   ierr = VecGetArray(y, &globalUpdate);CHKERRQ(ierr);
24231202d238SPatrick Farrell   ierr = VecGetArrayRead(patch->localUpdate, &localUpdate);CHKERRQ(ierr);
24241202d238SPatrick Farrell   ierr = PetscSFReduceBegin(patch->defaultSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM);CHKERRQ(ierr);
24251202d238SPatrick Farrell   ierr = PetscSFReduceEnd(patch->defaultSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM);CHKERRQ(ierr);
24261202d238SPatrick Farrell   ierr = VecRestoreArrayRead(patch->localUpdate, &localUpdate);CHKERRQ(ierr);
24274bbf5ea8SMatthew G. Knepley 
24284bbf5ea8SMatthew G. Knepley   /* Now we need to send the global BC values through */
24291202d238SPatrick Farrell   ierr = VecGetArrayRead(x, &globalRHS);CHKERRQ(ierr);
24304bbf5ea8SMatthew G. Knepley   ierr = ISGetSize(patch->globalBcNodes, &numBcs);CHKERRQ(ierr);
24314bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr);
24324bbf5ea8SMatthew G. Knepley   ierr = VecGetLocalSize(x, &n);CHKERRQ(ierr);
24334bbf5ea8SMatthew G. Knepley   for (bc = 0; bc < numBcs; ++bc) {
24344bbf5ea8SMatthew G. Knepley     const PetscInt idx = bcNodes[bc];
24351202d238SPatrick Farrell     if (idx < n) globalUpdate[idx] = globalRHS[idx];
24364bbf5ea8SMatthew G. Knepley   }
24374bbf5ea8SMatthew G. Knepley 
24384bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr);
24391202d238SPatrick Farrell   ierr = VecRestoreArrayRead(x, &globalRHS);CHKERRQ(ierr);
24401202d238SPatrick Farrell   ierr = VecRestoreArray(y, &globalUpdate);CHKERRQ(ierr);
24414bbf5ea8SMatthew G. Knepley 
24424bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsPopGetViewerOff();CHKERRQ(ierr);
24434bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr);
24444bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
24454bbf5ea8SMatthew G. Knepley }
24464bbf5ea8SMatthew G. Knepley 
2447dadc69c5SMatthew G. Knepley static PetscErrorCode PCReset_PATCH_Linear(PC pc)
2448dadc69c5SMatthew G. Knepley {
2449dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2450dadc69c5SMatthew G. Knepley   PetscInt       i;
2451dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2452dadc69c5SMatthew G. Knepley 
2453dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2454dadc69c5SMatthew G. Knepley   if (patch->solver) {
2455dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = KSPReset((KSP) patch->solver[i]);CHKERRQ(ierr);}
2456dadc69c5SMatthew G. Knepley   }
2457dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
2458dadc69c5SMatthew G. Knepley }
2459dadc69c5SMatthew G. Knepley 
24604bbf5ea8SMatthew G. Knepley static PetscErrorCode PCReset_PATCH(PC pc)
24614bbf5ea8SMatthew G. Knepley {
24624bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
24634bbf5ea8SMatthew G. Knepley   PetscInt       i;
24644bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
24654bbf5ea8SMatthew G. Knepley 
24664bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
24674bbf5ea8SMatthew G. Knepley   /* TODO: Get rid of all these ifs */
24684bbf5ea8SMatthew G. Knepley   ierr = PetscSFDestroy(&patch->defaultSF);CHKERRQ(ierr);
24694bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->cellCounts);CHKERRQ(ierr);
24705f824522SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->pointCounts);CHKERRQ(ierr);
24714bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->cellNumbering);CHKERRQ(ierr);
24724bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->gtolCounts);CHKERRQ(ierr);
24734bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->gtol);CHKERRQ(ierr);
24744bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->cells);CHKERRQ(ierr);
24755f824522SMatthew G. Knepley   ierr = ISDestroy(&patch->points);CHKERRQ(ierr);
24764bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->dofs);CHKERRQ(ierr);
24775f824522SMatthew G. Knepley   ierr = ISDestroy(&patch->offs);CHKERRQ(ierr);
24785f824522SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->patchSection);CHKERRQ(ierr);
24794bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->ghostBcNodes);CHKERRQ(ierr);
24804bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->globalBcNodes);CHKERRQ(ierr);
248111ac8bb0SFlorian Wechsung   ierr = PetscSectionDestroy(&patch->gtolCountsWithArtificial);CHKERRQ(ierr);
248211ac8bb0SFlorian Wechsung   ierr = ISDestroy(&patch->gtolWithArtificial);CHKERRQ(ierr);
248311ac8bb0SFlorian Wechsung   ierr = ISDestroy(&patch->dofsWithArtificial);CHKERRQ(ierr);
248411ac8bb0SFlorian Wechsung   ierr = ISDestroy(&patch->offsWithArtificial);CHKERRQ(ierr);
24850904074fSPatrick Farrell   ierr = PetscSectionDestroy(&patch->gtolCountsWithAll);CHKERRQ(ierr);
24860904074fSPatrick Farrell   ierr = ISDestroy(&patch->gtolWithAll);CHKERRQ(ierr);
24870904074fSPatrick Farrell   ierr = ISDestroy(&patch->dofsWithAll);CHKERRQ(ierr);
24880904074fSPatrick Farrell   ierr = ISDestroy(&patch->offsWithAll);CHKERRQ(ierr);
248911ac8bb0SFlorian Wechsung 
24904bbf5ea8SMatthew G. Knepley 
24915f824522SMatthew G. Knepley   if (patch->dofSection) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscSectionDestroy(&patch->dofSection[i]);CHKERRQ(ierr);}
24924bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->dofSection);CHKERRQ(ierr);
24934bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->bs);CHKERRQ(ierr);
24944bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->nodesPerCell);CHKERRQ(ierr);
24955f824522SMatthew G. Knepley   if (patch->cellNodeMap) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscFree(patch->cellNodeMap[i]);CHKERRQ(ierr);}
24964bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->cellNodeMap);CHKERRQ(ierr);
24974bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->subspaceOffsets);CHKERRQ(ierr);
24984bbf5ea8SMatthew G. Knepley 
2499dadc69c5SMatthew G. Knepley   ierr = (*patch->resetsolver)(pc);CHKERRQ(ierr);
25004bbf5ea8SMatthew G. Knepley 
2501e4c66b91SPatrick Farrell   if (patch->subspaces_to_exclude) {
2502e4c66b91SPatrick Farrell     PetscHSetIDestroy(&patch->subspaces_to_exclude);
2503e4c66b91SPatrick Farrell   }
2504e4c66b91SPatrick Farrell 
25051202d238SPatrick Farrell   ierr = VecDestroy(&patch->localRHS);CHKERRQ(ierr);
25061202d238SPatrick Farrell   ierr = VecDestroy(&patch->localUpdate);CHKERRQ(ierr);
25071202d238SPatrick Farrell   if (patch->patchRHS) {
25081202d238SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchRHS[i]);CHKERRQ(ierr);}
25091202d238SPatrick Farrell     ierr = PetscFree(patch->patchRHS);CHKERRQ(ierr);
25104bbf5ea8SMatthew G. Knepley   }
25111202d238SPatrick Farrell   if (patch->patchUpdate) {
25121202d238SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchUpdate[i]);CHKERRQ(ierr);}
25131202d238SPatrick Farrell     ierr = PetscFree(patch->patchUpdate);CHKERRQ(ierr);
25144bbf5ea8SMatthew G. Knepley   }
25154bbf5ea8SMatthew G. Knepley   ierr = VecDestroy(&patch->dof_weights);CHKERRQ(ierr);
25164bbf5ea8SMatthew G. Knepley   if (patch->patch_dof_weights) {
25175f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patch_dof_weights[i]);CHKERRQ(ierr);}
25184bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->patch_dof_weights);CHKERRQ(ierr);
25194bbf5ea8SMatthew G. Knepley   }
25204bbf5ea8SMatthew G. Knepley   if (patch->mat) {
25215f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->mat[i]);CHKERRQ(ierr);}
25224bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->mat);CHKERRQ(ierr);
25235f824522SMatthew G. Knepley   }
252411ac8bb0SFlorian Wechsung   if (patch->matWithArtificial) {
252511ac8bb0SFlorian Wechsung     for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->matWithArtificial[i]);CHKERRQ(ierr);}
252611ac8bb0SFlorian Wechsung     ierr = PetscFree(patch->matWithArtificial);CHKERRQ(ierr);
252711ac8bb0SFlorian Wechsung   }
25281202d238SPatrick Farrell   if (patch->patchRHSWithArtificial) {
25291202d238SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchRHSWithArtificial[i]);CHKERRQ(ierr);}
25301202d238SPatrick Farrell     ierr = PetscFree(patch->patchRHSWithArtificial);CHKERRQ(ierr);
253111ac8bb0SFlorian Wechsung   }
253296b79ebeSFlorian Wechsung   if(patch->dofMappingWithoutToWithArtificial) {
253396b79ebeSFlorian Wechsung     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->dofMappingWithoutToWithArtificial[i]);CHKERRQ(ierr);}
253496b79ebeSFlorian Wechsung     ierr = PetscFree(patch->dofMappingWithoutToWithArtificial);CHKERRQ(ierr);
253596b79ebeSFlorian Wechsung 
253696b79ebeSFlorian Wechsung   }
25370904074fSPatrick Farrell   if(patch->dofMappingWithoutToWithAll) {
25380904074fSPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->dofMappingWithoutToWithAll[i]);CHKERRQ(ierr);}
25390904074fSPatrick Farrell     ierr = PetscFree(patch->dofMappingWithoutToWithAll);CHKERRQ(ierr);
25400904074fSPatrick Farrell 
25410904074fSPatrick Farrell   }
25424bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr);
25435f824522SMatthew G. Knepley   if (patch->userIS) {
25445f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->userIS[i]);CHKERRQ(ierr);}
25455f824522SMatthew G. Knepley     ierr = PetscFree(patch->userIS);CHKERRQ(ierr);
25465f824522SMatthew G. Knepley   }
25474bbf5ea8SMatthew G. Knepley   patch->bs          = 0;
25484bbf5ea8SMatthew G. Knepley   patch->cellNodeMap = NULL;
25497974b488SMatthew G. Knepley   patch->nsubspaces  = 0;
25504bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->iterationSet);CHKERRQ(ierr);
25515f824522SMatthew G. Knepley 
25525f824522SMatthew G. Knepley   ierr = PetscViewerDestroy(&patch->viewerSection);CHKERRQ(ierr);
25534bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
25544bbf5ea8SMatthew G. Knepley }
25554bbf5ea8SMatthew G. Knepley 
2556dadc69c5SMatthew G. Knepley static PetscErrorCode PCDestroy_PATCH_Linear(PC pc)
25574bbf5ea8SMatthew G. Knepley {
25584bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
25594bbf5ea8SMatthew G. Knepley   PetscInt       i;
25604bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
25614bbf5ea8SMatthew G. Knepley 
25624bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2563dadc69c5SMatthew G. Knepley   if (patch->solver) {
2564dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = KSPDestroy((KSP *) &patch->solver[i]);CHKERRQ(ierr);}
2565dadc69c5SMatthew G. Knepley     ierr = PetscFree(patch->solver);CHKERRQ(ierr);
25664bbf5ea8SMatthew G. Knepley   }
2567dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
2568dadc69c5SMatthew G. Knepley }
2569dadc69c5SMatthew G. Knepley 
2570dadc69c5SMatthew G. Knepley static PetscErrorCode PCDestroy_PATCH(PC pc)
2571dadc69c5SMatthew G. Knepley {
2572dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2573dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2574dadc69c5SMatthew G. Knepley 
2575dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2576dadc69c5SMatthew G. Knepley   ierr = PCReset_PATCH(pc);CHKERRQ(ierr);
2577dadc69c5SMatthew G. Knepley   ierr = (*patch->destroysolver)(pc);CHKERRQ(ierr);
25784bbf5ea8SMatthew G. Knepley   ierr = PetscFree(pc->data);CHKERRQ(ierr);
25794bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
25804bbf5ea8SMatthew G. Knepley }
25814bbf5ea8SMatthew G. Knepley 
25824bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetFromOptions_PATCH(PetscOptionItems *PetscOptionsObject, PC pc)
25834bbf5ea8SMatthew G. Knepley {
25844bbf5ea8SMatthew G. Knepley   PC_PATCH            *patch = (PC_PATCH *) pc->data;
25854bbf5ea8SMatthew G. Knepley   PCPatchConstructType patchConstructionType = PC_PATCH_STAR;
25865f824522SMatthew G. Knepley   char                 sub_mat_type[PETSC_MAX_PATH_LEN];
258710534d48SPatrick Farrell   char                 option[PETSC_MAX_PATH_LEN];
25885f824522SMatthew G. Knepley   const char          *prefix;
25894bbf5ea8SMatthew G. Knepley   PetscBool            flg, dimflg, codimflg;
25905f824522SMatthew G. Knepley   MPI_Comm             comm;
2591a48c39c8SPatrick Farrell   PetscInt            *ifields, nfields, k;
25924bbf5ea8SMatthew G. Knepley   PetscErrorCode       ierr;
259361c4b389SFlorian Wechsung   PCCompositeType loctype = PC_COMPOSITE_ADDITIVE;
25944bbf5ea8SMatthew G. Knepley 
25954bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
25965f824522SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) pc, &comm);CHKERRQ(ierr);
25975f824522SMatthew G. Knepley   ierr = PetscObjectGetOptionsPrefix((PetscObject) pc, &prefix);CHKERRQ(ierr);
259810534d48SPatrick Farrell   ierr = PetscOptionsHead(PetscOptionsObject, "Patch solver options");CHKERRQ(ierr);
259910534d48SPatrick Farrell 
260010534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_save_operators", patch->classname);
260110534d48SPatrick Farrell   ierr = PetscOptionsBool(option,  "Store all patch operators for lifetime of object?", "PCPatchSetSaveOperators", patch->save_operators, &patch->save_operators, &flg);CHKERRQ(ierr);
260210534d48SPatrick Farrell 
260310534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_partition_of_unity", patch->classname);
260410534d48SPatrick Farrell   ierr = PetscOptionsBool(option, "Weight contributions by dof multiplicity?", "PCPatchSetPartitionOfUnity", patch->partition_of_unity, &patch->partition_of_unity, &flg);CHKERRQ(ierr);
260510534d48SPatrick Farrell 
260610534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_local_type", patch->classname);
260710534d48SPatrick Farrell   ierr = PetscOptionsEnum(option,"Type of local solver composition (additive or multiplicative)","PCPatchSetLocalComposition",PCCompositeTypes,(PetscEnum)loctype,(PetscEnum*)&loctype,&flg);CHKERRQ(ierr);
260861c4b389SFlorian Wechsung   if(flg) { ierr = PCPatchSetLocalComposition(pc, loctype);CHKERRQ(ierr);}
260910534d48SPatrick Farrell 
261010534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_dim", patch->classname);
261110534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "What dimension of mesh point to construct patches by? (0 = vertices)", "PCPATCH", patch->dim, &patch->dim, &dimflg);CHKERRQ(ierr);
261210534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_codim", patch->classname);
261310534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "What co-dimension of mesh point to construct patches by? (0 = cells)", "PCPATCH", patch->codim, &patch->codim, &codimflg);CHKERRQ(ierr);
261461c4b389SFlorian Wechsung   if (dimflg && codimflg) {SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Can only set one of dimension or co-dimension");CHKERRQ(ierr);}
261510534d48SPatrick Farrell 
261610534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_type", patch->classname);
261710534d48SPatrick Farrell   ierr = PetscOptionsEnum(option, "How should the patches be constructed?", "PCPatchSetConstructType", PCPatchConstructTypes, (PetscEnum) patchConstructionType, (PetscEnum *) &patchConstructionType, &flg);CHKERRQ(ierr);
26184bbf5ea8SMatthew G. Knepley   if (flg) {ierr = PCPatchSetConstructType(pc, patchConstructionType, NULL, NULL);CHKERRQ(ierr);}
261910534d48SPatrick Farrell 
262010534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_vanka_dim", patch->classname);
262110534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "Topological dimension of entities for Vanka to ignore", "PCPATCH", patch->vankadim, &patch->vankadim, &flg);CHKERRQ(ierr);
262210534d48SPatrick Farrell 
262310534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_ignore_dim", patch->classname);
262410534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "Topological dimension of entities for completion to ignore", "PCPATCH", patch->ignoredim, &patch->ignoredim, &flg);CHKERRQ(ierr);
262510534d48SPatrick Farrell 
2626b525f888SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_pardecomp_overlap", patch->classname);
2627b525f888SPatrick Farrell   ierr = PetscOptionsInt(option, "What overlap should we use in construct type pardecomp?", "PCPATCH", patch->pardecomp_overlap, &patch->pardecomp_overlap, &flg);CHKERRQ(ierr);
2628b525f888SPatrick Farrell 
262910534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_sub_mat_type", patch->classname);
263010534d48SPatrick Farrell   ierr = PetscOptionsFList(option, "Matrix type for patch solves", "PCPatchSetSubMatType", MatList, NULL, sub_mat_type, PETSC_MAX_PATH_LEN, &flg);CHKERRQ(ierr);
26314bbf5ea8SMatthew G. Knepley   if (flg) {ierr = PCPatchSetSubMatType(pc, sub_mat_type);CHKERRQ(ierr);}
263210534d48SPatrick Farrell 
263310534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_symmetrise_sweep", patch->classname);
263410534d48SPatrick Farrell   ierr = PetscOptionsBool(option, "Go start->end, end->start?", "PCPATCH", patch->symmetrise_sweep, &patch->symmetrise_sweep, &flg);CHKERRQ(ierr);
2635e4c66b91SPatrick Farrell 
2636a48c39c8SPatrick Farrell   /* If the user has set the number of subspaces, use that for the buffer size,
2637a48c39c8SPatrick Farrell      otherwise use a large number */
2638a48c39c8SPatrick Farrell   if (patch->nsubspaces <= 0) {
2639a48c39c8SPatrick Farrell     nfields = 128;
2640a48c39c8SPatrick Farrell   } else {
2641a48c39c8SPatrick Farrell     nfields = patch->nsubspaces;
2642a48c39c8SPatrick Farrell   }
2643a48c39c8SPatrick Farrell   ierr = PetscMalloc1(nfields, &ifields);CHKERRQ(ierr);
264410534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exclude_subspaces", patch->classname);
264510534d48SPatrick Farrell   ierr = PetscOptionsGetIntArray(((PetscObject)pc)->options,((PetscObject)pc)->prefix,option,ifields,&nfields,&flg);CHKERRQ(ierr);
2646e4c66b91SPatrick 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");
2647e4c66b91SPatrick Farrell   if (flg) {
2648e4c66b91SPatrick Farrell     PetscHSetIClear(patch->subspaces_to_exclude);
264959b66c28SPatrick Farrell     for (k = 0; k < nfields; k++) {
2650e4c66b91SPatrick Farrell       PetscHSetIAdd(patch->subspaces_to_exclude, ifields[k]);
2651e4c66b91SPatrick Farrell     }
2652e4c66b91SPatrick Farrell   }
265359b66c28SPatrick Farrell   ierr = PetscFree(ifields);CHKERRQ(ierr);
26545f824522SMatthew G. Knepley 
2655fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_patches_view", patch->classname);CHKERRQ(ierr);
265610534d48SPatrick Farrell   ierr = PetscOptionsBool(option, "Print out information during patch construction", "PCPATCH", patch->viewPatches, &patch->viewPatches, &flg);CHKERRQ(ierr);
2657fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_cells_view", patch->classname);CHKERRQ(ierr);
265810534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerCells, &patch->formatCells, &patch->viewCells);CHKERRQ(ierr);
2659fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_interior_facets_view", patch->classname);CHKERRQ(ierr);
2660fbb82ab5SLawrence Mitchell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerIntFacets, &patch->formatIntFacets, &patch->viewIntFacets);CHKERRQ(ierr);
2661fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exterior_facets_view", patch->classname);CHKERRQ(ierr);
2662fbb82ab5SLawrence Mitchell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerExtFacets, &patch->formatExtFacets, &patch->viewExtFacets);CHKERRQ(ierr);
2663fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_points_view", patch->classname);CHKERRQ(ierr);
266410534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerPoints, &patch->formatPoints, &patch->viewPoints);CHKERRQ(ierr);
2665fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_section_view", patch->classname);CHKERRQ(ierr);
266610534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerSection, &patch->formatSection, &patch->viewSection);CHKERRQ(ierr);
2667fbb82ab5SLawrence Mitchell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_mat_view", patch->classname);CHKERRQ(ierr);
266810534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerMatrix, &patch->formatMatrix, &patch->viewMatrix);CHKERRQ(ierr);
26694bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsTail();CHKERRQ(ierr);
26705f824522SMatthew G. Knepley   patch->optionsSet = PETSC_TRUE;
26714bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
26724bbf5ea8SMatthew G. Knepley }
26734bbf5ea8SMatthew G. Knepley 
26744bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUpOnBlocks_PATCH(PC pc)
26754bbf5ea8SMatthew G. Knepley {
26764bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch = (PC_PATCH*) pc->data;
26774bbf5ea8SMatthew G. Knepley   KSPConvergedReason reason;
26784bbf5ea8SMatthew G. Knepley   PetscInt           i;
26794bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
26804bbf5ea8SMatthew G. Knepley 
26814bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2682a1eac568SLawrence Mitchell   if (!patch->save_operators) {
2683a1eac568SLawrence Mitchell     /* Can't do this here because the sub KSPs don't have an operator attached yet. */
2684a1eac568SLawrence Mitchell     PetscFunctionReturn(0);
2685a1eac568SLawrence Mitchell   }
26864bbf5ea8SMatthew G. Knepley   for (i = 0; i < patch->npatch; ++i) {
2687dadc69c5SMatthew G. Knepley     if (!((KSP) patch->solver[i])->setfromoptionscalled) {
2688dadc69c5SMatthew G. Knepley       ierr = KSPSetFromOptions((KSP) patch->solver[i]);CHKERRQ(ierr);
2689a1eac568SLawrence Mitchell     }
2690dadc69c5SMatthew G. Knepley     ierr = KSPSetUp((KSP) patch->solver[i]);CHKERRQ(ierr);
2691dadc69c5SMatthew G. Knepley     ierr = KSPGetConvergedReason((KSP) patch->solver[i], &reason);CHKERRQ(ierr);
2692c0decd05SBarry Smith     if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR;
26934bbf5ea8SMatthew G. Knepley   }
26944bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
26954bbf5ea8SMatthew G. Knepley }
26964bbf5ea8SMatthew G. Knepley 
26974bbf5ea8SMatthew G. Knepley static PetscErrorCode PCView_PATCH(PC pc, PetscViewer viewer)
26984bbf5ea8SMatthew G. Knepley {
26994bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
27004bbf5ea8SMatthew G. Knepley   PetscViewer    sviewer;
27014bbf5ea8SMatthew G. Knepley   PetscBool      isascii;
27024bbf5ea8SMatthew G. Knepley   PetscMPIInt    rank;
27034bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
27044bbf5ea8SMatthew G. Knepley 
27054bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
27064bbf5ea8SMatthew G. Knepley   /* TODO Redo tabbing with set tbas in new style */
27074bbf5ea8SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii);CHKERRQ(ierr);
27084bbf5ea8SMatthew G. Knepley   if (!isascii) PetscFunctionReturn(0);
27094bbf5ea8SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) pc), &rank);CHKERRQ(ierr);
27104bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
27114bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "Subspace Correction preconditioner with %d patches\n", patch->npatch);CHKERRQ(ierr);
271261c4b389SFlorian Wechsung   if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
2713c2e6f3c0SFlorian Wechsung     ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: multiplicative\n");CHKERRQ(ierr);
2714e047a90bSFlorian Wechsung   } else {
271573ec7555SLawrence Mitchell     ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: additive\n");CHKERRQ(ierr);
2716c2e6f3c0SFlorian Wechsung   }
27174bbf5ea8SMatthew G. Knepley   if (patch->partition_of_unity) {ierr = PetscViewerASCIIPrintf(viewer, "Weighting by partition of unity\n");CHKERRQ(ierr);}
27184bbf5ea8SMatthew G. Knepley   else                           {ierr = PetscViewerASCIIPrintf(viewer, "Not weighting by partition of unity\n");CHKERRQ(ierr);}
27194bbf5ea8SMatthew G. Knepley   if (patch->symmetrise_sweep) {ierr = PetscViewerASCIIPrintf(viewer, "Symmetrising sweep (start->end, then end->start)\n");CHKERRQ(ierr);}
27204bbf5ea8SMatthew G. Knepley   else                         {ierr = PetscViewerASCIIPrintf(viewer, "Not symmetrising sweep\n");CHKERRQ(ierr);}
27214bbf5ea8SMatthew G. Knepley   if (!patch->save_operators) {ierr = PetscViewerASCIIPrintf(viewer, "Not saving patch operators (rebuilt every PCApply)\n");CHKERRQ(ierr);}
27224bbf5ea8SMatthew G. Knepley   else                        {ierr = PetscViewerASCIIPrintf(viewer, "Saving patch operators (rebuilt every PCSetUp)\n");CHKERRQ(ierr);}
27234bbf5ea8SMatthew G. Knepley   if (patch->patchconstructop == PCPatchConstruct_Star)       {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: star\n");CHKERRQ(ierr);}
27244bbf5ea8SMatthew G. Knepley   else if (patch->patchconstructop == PCPatchConstruct_Vanka) {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: Vanka\n");CHKERRQ(ierr);}
27254bbf5ea8SMatthew G. Knepley   else if (patch->patchconstructop == PCPatchConstruct_User)  {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: user-specified\n");CHKERRQ(ierr);}
27264bbf5ea8SMatthew G. Knepley   else                                                        {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: unknown\n");CHKERRQ(ierr);}
27275d30859aSPatrick Farrell 
27285d30859aSPatrick Farrell   if (patch->isNonlinear) {
27295d30859aSPatrick Farrell     ierr = PetscViewerASCIIPrintf(viewer, "SNES on patches (all same):\n");CHKERRQ(ierr);
27305d30859aSPatrick Farrell   } else {
27315d30859aSPatrick Farrell     ierr = PetscViewerASCIIPrintf(viewer, "KSP on patches (all same):\n");CHKERRQ(ierr);
27325d30859aSPatrick Farrell   }
2733dadc69c5SMatthew G. Knepley   if (patch->solver) {
27344bbf5ea8SMatthew G. Knepley     ierr = PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr);
27354bbf5ea8SMatthew G. Knepley     if (!rank) {
27364bbf5ea8SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(sviewer);CHKERRQ(ierr);
2737dadc69c5SMatthew G. Knepley       ierr = PetscObjectView(patch->solver[0], sviewer);CHKERRQ(ierr);
27384bbf5ea8SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(sviewer);CHKERRQ(ierr);
27394bbf5ea8SMatthew G. Knepley     }
27404bbf5ea8SMatthew G. Knepley     ierr = PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr);
27414bbf5ea8SMatthew G. Knepley   } else {
27424bbf5ea8SMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2743dadc69c5SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Solver not yet set.\n");CHKERRQ(ierr);
27444bbf5ea8SMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
27454bbf5ea8SMatthew G. Knepley   }
27464bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
27474bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
27484bbf5ea8SMatthew G. Knepley }
27494bbf5ea8SMatthew G. Knepley 
2750e5893cccSMatthew G. Knepley /*MC
275198ed095eSMatthew G. Knepley   PCPATCH - A PC object that encapsulates flexible definition of blocks for overlapping and non-overlapping
275298ed095eSMatthew G. Knepley             small block additive preconditioners. Block definition is based on topology from
2753e5893cccSMatthew G. Knepley             a DM and equation numbering from a PetscSection.
2754e5893cccSMatthew G. Knepley 
2755e5893cccSMatthew G. Knepley   Options Database Keys:
2756e5893cccSMatthew G. Knepley + -pc_patch_cells_view   - Views the process local cell numbers for each patch
2757e5893cccSMatthew G. Knepley . -pc_patch_points_view  - Views the process local mesh point numbers for each patch
2758e5893cccSMatthew G. Knepley . -pc_patch_g2l_view     - Views the map between global dofs and patch local dofs for each patch
2759e5893cccSMatthew G. Knepley . -pc_patch_patches_view - Views the global dofs associated with each patch and its boundary
2760e5893cccSMatthew G. Knepley - -pc_patch_sub_mat_view - Views the matrix associated with each patch
2761e5893cccSMatthew G. Knepley 
2762e5893cccSMatthew G. Knepley   Level: intermediate
2763e5893cccSMatthew G. Knepley 
2764e5893cccSMatthew G. Knepley .seealso: PCType, PCCreate(), PCSetType()
2765e5893cccSMatthew G. Knepley M*/
2766642283e9SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PCCreate_Patch(PC pc)
27674bbf5ea8SMatthew G. Knepley {
27684bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch;
27694bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
27704bbf5ea8SMatthew G. Knepley 
27714bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
27724bbf5ea8SMatthew G. Knepley   ierr = PetscNewLog(pc, &patch);CHKERRQ(ierr);
27734bbf5ea8SMatthew G. Knepley 
2774e4c66b91SPatrick Farrell   if (patch->subspaces_to_exclude) {
2775e4c66b91SPatrick Farrell     PetscHSetIDestroy(&patch->subspaces_to_exclude);
2776e4c66b91SPatrick Farrell   }
2777e4c66b91SPatrick Farrell   PetscHSetICreate(&patch->subspaces_to_exclude);
2778e4c66b91SPatrick Farrell 
277910534d48SPatrick Farrell   patch->classname = "pc";
2780debbdec3SPatrick Farrell   patch->isNonlinear = PETSC_FALSE;
278110534d48SPatrick Farrell 
27824bbf5ea8SMatthew G. Knepley   /* Set some defaults */
27835f824522SMatthew G. Knepley   patch->combined           = PETSC_FALSE;
27844bbf5ea8SMatthew G. Knepley   patch->save_operators     = PETSC_TRUE;
278561c4b389SFlorian Wechsung   patch->local_composition_type = PC_COMPOSITE_ADDITIVE;
27864bbf5ea8SMatthew G. Knepley   patch->partition_of_unity = PETSC_FALSE;
27874bbf5ea8SMatthew G. Knepley   patch->codim              = -1;
27884bbf5ea8SMatthew G. Knepley   patch->dim                = -1;
27894bbf5ea8SMatthew G. Knepley   patch->vankadim           = -1;
27905f824522SMatthew G. Knepley   patch->ignoredim          = -1;
2791b525f888SPatrick Farrell   patch->pardecomp_overlap  = 0;
27924bbf5ea8SMatthew G. Knepley   patch->patchconstructop   = PCPatchConstruct_Star;
27934bbf5ea8SMatthew G. Knepley   patch->symmetrise_sweep   = PETSC_FALSE;
27945f824522SMatthew G. Knepley   patch->npatch             = 0;
27954bbf5ea8SMatthew G. Knepley   patch->userIS             = NULL;
27965f824522SMatthew G. Knepley   patch->optionsSet         = PETSC_FALSE;
27974bbf5ea8SMatthew G. Knepley   patch->iterationSet       = NULL;
27984bbf5ea8SMatthew G. Knepley   patch->user_patches       = PETSC_FALSE;
27995f824522SMatthew G. Knepley   ierr = PetscStrallocpy(MATDENSE, (char **) &patch->sub_mat_type);CHKERRQ(ierr);
28005f824522SMatthew G. Knepley   patch->viewPatches        = PETSC_FALSE;
28015f824522SMatthew G. Knepley   patch->viewCells          = PETSC_FALSE;
28025f824522SMatthew G. Knepley   patch->viewPoints         = PETSC_FALSE;
28035f824522SMatthew G. Knepley   patch->viewSection        = PETSC_FALSE;
28045f824522SMatthew G. Knepley   patch->viewMatrix         = PETSC_FALSE;
2805dadc69c5SMatthew G. Knepley   patch->setupsolver        = PCSetUp_PATCH_Linear;
2806dadc69c5SMatthew G. Knepley   patch->applysolver        = PCApply_PATCH_Linear;
2807dadc69c5SMatthew G. Knepley   patch->resetsolver        = PCReset_PATCH_Linear;
2808dadc69c5SMatthew G. Knepley   patch->destroysolver      = PCDestroy_PATCH_Linear;
28096c9c532dSPatrick Farrell   patch->updatemultiplicative = PCUpdateMultiplicative_PATCH_Linear;
281047aca4a6SPatrick Farrell   patch->dofMappingWithoutToWithArtificial = NULL;
281147aca4a6SPatrick Farrell   patch->dofMappingWithoutToWithAll = NULL;
28124bbf5ea8SMatthew G. Knepley 
28134bbf5ea8SMatthew G. Knepley   pc->data                 = (void *) patch;
28144bbf5ea8SMatthew G. Knepley   pc->ops->apply           = PCApply_PATCH;
28154bbf5ea8SMatthew G. Knepley   pc->ops->applytranspose  = 0; /* PCApplyTranspose_PATCH; */
28164bbf5ea8SMatthew G. Knepley   pc->ops->setup           = PCSetUp_PATCH;
28174bbf5ea8SMatthew G. Knepley   pc->ops->reset           = PCReset_PATCH;
28184bbf5ea8SMatthew G. Knepley   pc->ops->destroy         = PCDestroy_PATCH;
28194bbf5ea8SMatthew G. Knepley   pc->ops->setfromoptions  = PCSetFromOptions_PATCH;
28204bbf5ea8SMatthew G. Knepley   pc->ops->setuponblocks   = PCSetUpOnBlocks_PATCH;
28214bbf5ea8SMatthew G. Knepley   pc->ops->view            = PCView_PATCH;
28224bbf5ea8SMatthew G. Knepley   pc->ops->applyrichardson = 0;
28234bbf5ea8SMatthew G. Knepley 
28244bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
28254bbf5ea8SMatthew G. Knepley }
2826