xref: /petsc/src/ksp/pc/impls/patch/pcpatch.c (revision 63deea8e7d41b4afc9f2d98e3cc646563e5f534f)
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 
794bbf5ea8SMatthew G. Knepley /* The user's already set the patches in patch->userIS. Build the hash tables */
801b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_User(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
814bbf5ea8SMatthew G. Knepley {
824bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch   = (PC_PATCH *) vpatch;
834bbf5ea8SMatthew G. Knepley   IS              patchis = patch->userIS[point];
844bbf5ea8SMatthew G. Knepley   PetscInt        n;
854bbf5ea8SMatthew G. Knepley   const PetscInt *patchdata;
864bbf5ea8SMatthew G. Knepley   PetscInt        pStart, pEnd, i;
874bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
884bbf5ea8SMatthew G. Knepley 
894bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
901b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(ht);CHKERRQ(ierr);
911b68eb51SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
924bbf5ea8SMatthew G. Knepley   ierr = ISGetLocalSize(patchis, &n);CHKERRQ(ierr);
934bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patchis, &patchdata);CHKERRQ(ierr);
944bbf5ea8SMatthew G. Knepley   for (i = 0; i < n; ++i) {
954bbf5ea8SMatthew G. Knepley     const PetscInt ownedpoint = patchdata[i];
964bbf5ea8SMatthew G. Knepley 
974bbf5ea8SMatthew G. Knepley     if (ownedpoint < pStart || ownedpoint >= pEnd) {
984bbf5ea8SMatthew G. Knepley       SETERRQ3(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D was not in [%D, %D)", ownedpoint, pStart, pEnd);
994bbf5ea8SMatthew G. Knepley     }
1001b68eb51SMatthew G. Knepley     ierr = PetscHSetIAdd(ht, ownedpoint);CHKERRQ(ierr);
1014bbf5ea8SMatthew G. Knepley   }
1024bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patchis, &patchdata);CHKERRQ(ierr);
1034bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
1044bbf5ea8SMatthew G. Knepley }
1054bbf5ea8SMatthew G. Knepley 
1064bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateDefaultSF_Private(PC pc, PetscInt n, const PetscSF *sf, const PetscInt *bs)
1074bbf5ea8SMatthew G. Knepley {
1084bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
1094bbf5ea8SMatthew G. Knepley   PetscInt       i;
1104bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
1114bbf5ea8SMatthew G. Knepley 
1124bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1134bbf5ea8SMatthew G. Knepley   if (n == 1 && bs[0] == 1) {
1144bbf5ea8SMatthew G. Knepley     patch->defaultSF = sf[0];
1154bbf5ea8SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) patch->defaultSF);CHKERRQ(ierr);
1164bbf5ea8SMatthew G. Knepley   } else {
1174bbf5ea8SMatthew G. Knepley     PetscInt     allRoots = 0, allLeaves = 0;
1184bbf5ea8SMatthew G. Knepley     PetscInt     leafOffset = 0;
1194bbf5ea8SMatthew G. Knepley     PetscInt    *ilocal = NULL;
1204bbf5ea8SMatthew G. Knepley     PetscSFNode *iremote = NULL;
1214bbf5ea8SMatthew G. Knepley     PetscInt    *remoteOffsets = NULL;
1224bbf5ea8SMatthew G. Knepley     PetscInt     index = 0;
1231b68eb51SMatthew G. Knepley     PetscHMapI   rankToIndex;
1244bbf5ea8SMatthew G. Knepley     PetscInt     numRanks = 0;
1254bbf5ea8SMatthew G. Knepley     PetscSFNode *remote = NULL;
1264bbf5ea8SMatthew G. Knepley     PetscSF      rankSF;
1274bbf5ea8SMatthew G. Knepley     PetscInt    *ranks = NULL;
1284bbf5ea8SMatthew G. Knepley     PetscInt    *offsets = NULL;
1294bbf5ea8SMatthew G. Knepley     MPI_Datatype contig;
1301b68eb51SMatthew G. Knepley     PetscHSetI   ranksUniq;
1314bbf5ea8SMatthew G. Knepley 
1324bbf5ea8SMatthew G. Knepley     /* First figure out how many dofs there are in the concatenated numbering.
1334bbf5ea8SMatthew G. Knepley      * allRoots: number of owned global dofs;
1344bbf5ea8SMatthew G. Knepley      * allLeaves: number of visible dofs (global + ghosted).
1354bbf5ea8SMatthew G. Knepley      */
1364bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
1374bbf5ea8SMatthew G. Knepley       PetscInt nroots, nleaves;
1384bbf5ea8SMatthew G. Knepley 
1394bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetGraph(sf[i], &nroots, &nleaves, NULL, NULL);CHKERRQ(ierr);
1404bbf5ea8SMatthew G. Knepley       allRoots  += nroots * bs[i];
1414bbf5ea8SMatthew G. Knepley       allLeaves += nleaves * bs[i];
1424bbf5ea8SMatthew G. Knepley     }
1434bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(allLeaves, &ilocal);CHKERRQ(ierr);
1444bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(allLeaves, &iremote);CHKERRQ(ierr);
1454bbf5ea8SMatthew G. Knepley     /* Now build an SF that just contains process connectivity. */
1461b68eb51SMatthew G. Knepley     ierr = PetscHSetICreate(&ranksUniq);CHKERRQ(ierr);
1474bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
1484bbf5ea8SMatthew G. Knepley       const PetscMPIInt *ranks = NULL;
1494bbf5ea8SMatthew G. Knepley       PetscInt           nranks, j;
1504bbf5ea8SMatthew G. Knepley 
1514bbf5ea8SMatthew G. Knepley       ierr = PetscSFSetUp(sf[i]);CHKERRQ(ierr);
1524bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetRanks(sf[i], &nranks, &ranks, NULL, NULL, NULL);CHKERRQ(ierr);
1534bbf5ea8SMatthew G. Knepley       /* These are all the ranks who communicate with me. */
1544bbf5ea8SMatthew G. Knepley       for (j = 0; j < nranks; ++j) {
1551b68eb51SMatthew G. Knepley         ierr = PetscHSetIAdd(ranksUniq, (PetscInt) ranks[j]);CHKERRQ(ierr);
1564bbf5ea8SMatthew G. Knepley       }
1574bbf5ea8SMatthew G. Knepley     }
1581b68eb51SMatthew G. Knepley     ierr = PetscHSetIGetSize(ranksUniq, &numRanks);CHKERRQ(ierr);
1594bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(numRanks, &remote);CHKERRQ(ierr);
1604bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(numRanks, &ranks);CHKERRQ(ierr);
1611b68eb51SMatthew G. Knepley     ierr = PetscHSetIGetElems(ranksUniq, &index, ranks);CHKERRQ(ierr);
1624bbf5ea8SMatthew G. Knepley 
1631b68eb51SMatthew G. Knepley     ierr = PetscHMapICreate(&rankToIndex);CHKERRQ(ierr);
1644bbf5ea8SMatthew G. Knepley     for (i = 0; i < numRanks; ++i) {
1654bbf5ea8SMatthew G. Knepley       remote[i].rank  = ranks[i];
1664bbf5ea8SMatthew G. Knepley       remote[i].index = 0;
1671b68eb51SMatthew G. Knepley       ierr = PetscHMapISet(rankToIndex, ranks[i], i);CHKERRQ(ierr);
1684bbf5ea8SMatthew G. Knepley     }
1694bbf5ea8SMatthew G. Knepley     ierr = PetscFree(ranks);CHKERRQ(ierr);
1701b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&ranksUniq);CHKERRQ(ierr);
1714bbf5ea8SMatthew G. Knepley     ierr = PetscSFCreate(PetscObjectComm((PetscObject) pc), &rankSF);CHKERRQ(ierr);
1724bbf5ea8SMatthew G. Knepley     ierr = PetscSFSetGraph(rankSF, 1, numRanks, NULL, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
1734bbf5ea8SMatthew G. Knepley     ierr = PetscSFSetUp(rankSF);CHKERRQ(ierr);
1744bbf5ea8SMatthew G. Knepley     /* OK, use it to communicate the root offset on the remote
1754bbf5ea8SMatthew G. Knepley      * processes for each subspace. */
1764bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(n, &offsets);CHKERRQ(ierr);
1774bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(n*numRanks, &remoteOffsets);CHKERRQ(ierr);
1784bbf5ea8SMatthew G. Knepley 
1794bbf5ea8SMatthew G. Knepley     offsets[0] = 0;
1804bbf5ea8SMatthew G. Knepley     for (i = 1; i < n; ++i) {
1814bbf5ea8SMatthew G. Knepley       PetscInt nroots;
1824bbf5ea8SMatthew G. Knepley 
1834bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetGraph(sf[i-1], &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
1844bbf5ea8SMatthew G. Knepley       offsets[i] = offsets[i-1] + nroots*bs[i-1];
1854bbf5ea8SMatthew G. Knepley     }
1864bbf5ea8SMatthew G. Knepley     /* Offsets are the offsets on the current process of the
1874bbf5ea8SMatthew G. Knepley      * global dof numbering for the subspaces. */
1884bbf5ea8SMatthew G. Knepley     ierr = MPI_Type_contiguous(n, MPIU_INT, &contig);CHKERRQ(ierr);
1894bbf5ea8SMatthew G. Knepley     ierr = MPI_Type_commit(&contig);CHKERRQ(ierr);
1904bbf5ea8SMatthew G. Knepley 
1914bbf5ea8SMatthew G. Knepley     ierr = PetscSFBcastBegin(rankSF, contig, offsets, remoteOffsets);CHKERRQ(ierr);
1924bbf5ea8SMatthew G. Knepley     ierr = PetscSFBcastEnd(rankSF, contig, offsets, remoteOffsets);CHKERRQ(ierr);
1934bbf5ea8SMatthew G. Knepley     ierr = MPI_Type_free(&contig);CHKERRQ(ierr);
1944bbf5ea8SMatthew G. Knepley     ierr = PetscFree(offsets);CHKERRQ(ierr);
1954bbf5ea8SMatthew G. Knepley     ierr = PetscSFDestroy(&rankSF);CHKERRQ(ierr);
1964bbf5ea8SMatthew G. Knepley     /* Now remoteOffsets contains the offsets on the remote
1974bbf5ea8SMatthew G. Knepley      * processes who communicate with me.  So now we can
1984bbf5ea8SMatthew G. Knepley      * concatenate the list of SFs into a single one. */
1994bbf5ea8SMatthew G. Knepley     index = 0;
2004bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2014bbf5ea8SMatthew G. Knepley       const PetscSFNode *remote = NULL;
2024bbf5ea8SMatthew G. Knepley       const PetscInt    *local  = NULL;
2034bbf5ea8SMatthew G. Knepley       PetscInt           nroots, nleaves, j;
2044bbf5ea8SMatthew G. Knepley 
2054bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetGraph(sf[i], &nroots, &nleaves, &local, &remote);CHKERRQ(ierr);
2064bbf5ea8SMatthew G. Knepley       for (j = 0; j < nleaves; ++j) {
2074bbf5ea8SMatthew G. Knepley         PetscInt rank = remote[j].rank;
2084bbf5ea8SMatthew G. Knepley         PetscInt idx, rootOffset, k;
2094bbf5ea8SMatthew G. Knepley 
2101b68eb51SMatthew G. Knepley         ierr = PetscHMapIGet(rankToIndex, rank, &idx);CHKERRQ(ierr);
2114bbf5ea8SMatthew G. Knepley         if (idx == -1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Didn't find rank, huh?");
2124bbf5ea8SMatthew G. Knepley         /* Offset on given rank for ith subspace */
2134bbf5ea8SMatthew G. Knepley         rootOffset = remoteOffsets[n*idx + i];
2144bbf5ea8SMatthew G. Knepley         for (k = 0; k < bs[i]; ++k) {
21573ec7555SLawrence Mitchell           ilocal[index]        = (local ? local[j] : j)*bs[i] + k + leafOffset;
2164bbf5ea8SMatthew G. Knepley           iremote[index].rank  = remote[j].rank;
2174bbf5ea8SMatthew G. Knepley           iremote[index].index = remote[j].index*bs[i] + k + rootOffset;
2184bbf5ea8SMatthew G. Knepley           ++index;
2194bbf5ea8SMatthew G. Knepley         }
2204bbf5ea8SMatthew G. Knepley       }
2214bbf5ea8SMatthew G. Knepley       leafOffset += nleaves * bs[i];
2224bbf5ea8SMatthew G. Knepley     }
2231b68eb51SMatthew G. Knepley     ierr = PetscHMapIDestroy(&rankToIndex);CHKERRQ(ierr);
2244bbf5ea8SMatthew G. Knepley     ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
2254bbf5ea8SMatthew G. Knepley     ierr = PetscSFCreate(PetscObjectComm((PetscObject)pc), &patch->defaultSF);CHKERRQ(ierr);
2264bbf5ea8SMatthew G. Knepley     ierr = PetscSFSetGraph(patch->defaultSF, allRoots, allLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER);CHKERRQ(ierr);
2274bbf5ea8SMatthew G. Knepley   }
2284bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
2294bbf5ea8SMatthew G. Knepley }
2304bbf5ea8SMatthew G. Knepley 
2314bbf5ea8SMatthew G. Knepley /* TODO: Docs */
2325f824522SMatthew G. Knepley PetscErrorCode PCPatchSetIgnoreDim(PC pc, PetscInt dim)
2335f824522SMatthew G. Knepley {
2345f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
2355f824522SMatthew G. Knepley   PetscFunctionBegin;
2365f824522SMatthew G. Knepley   patch->ignoredim = dim;
2375f824522SMatthew G. Knepley   PetscFunctionReturn(0);
2385f824522SMatthew G. Knepley }
2395f824522SMatthew G. Knepley 
2405f824522SMatthew G. Knepley /* TODO: Docs */
2415f824522SMatthew G. Knepley PetscErrorCode PCPatchGetIgnoreDim(PC pc, PetscInt *dim)
2425f824522SMatthew G. Knepley {
2435f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
2445f824522SMatthew G. Knepley   PetscFunctionBegin;
2455f824522SMatthew G. Knepley   *dim = patch->ignoredim;
2465f824522SMatthew G. Knepley   PetscFunctionReturn(0);
2475f824522SMatthew G. Knepley }
2485f824522SMatthew G. Knepley 
2495f824522SMatthew G. Knepley /* TODO: Docs */
2504bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetSaveOperators(PC pc, PetscBool flg)
2514bbf5ea8SMatthew G. Knepley {
2524bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
2534bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2544bbf5ea8SMatthew G. Knepley   patch->save_operators = flg;
2554bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
2564bbf5ea8SMatthew G. Knepley }
2574bbf5ea8SMatthew G. Knepley 
2584bbf5ea8SMatthew G. Knepley /* TODO: Docs */
2594bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetSaveOperators(PC pc, PetscBool *flg)
2604bbf5ea8SMatthew G. Knepley {
2614bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
2624bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2634bbf5ea8SMatthew G. Knepley   *flg = patch->save_operators;
2644bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
2654bbf5ea8SMatthew G. Knepley }
2664bbf5ea8SMatthew G. Knepley 
2674bbf5ea8SMatthew G. Knepley /* TODO: Docs */
2684bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetPartitionOfUnity(PC pc, PetscBool flg)
2694bbf5ea8SMatthew G. Knepley {
2704bbf5ea8SMatthew G. Knepley     PC_PATCH *patch = (PC_PATCH *) pc->data;
2714bbf5ea8SMatthew G. Knepley     PetscFunctionBegin;
2724bbf5ea8SMatthew G. Knepley     patch->partition_of_unity = flg;
2734bbf5ea8SMatthew G. Knepley     PetscFunctionReturn(0);
2744bbf5ea8SMatthew G. Knepley }
2754bbf5ea8SMatthew G. Knepley 
2764bbf5ea8SMatthew G. Knepley /* TODO: Docs */
2774bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetPartitionOfUnity(PC pc, PetscBool *flg)
2784bbf5ea8SMatthew G. Knepley {
2794bbf5ea8SMatthew G. Knepley     PC_PATCH *patch = (PC_PATCH *) pc->data;
2804bbf5ea8SMatthew G. Knepley     PetscFunctionBegin;
2814bbf5ea8SMatthew G. Knepley     *flg = patch->partition_of_unity;
2824bbf5ea8SMatthew G. Knepley     PetscFunctionReturn(0);
2834bbf5ea8SMatthew G. Knepley }
2844bbf5ea8SMatthew G. Knepley 
2854bbf5ea8SMatthew G. Knepley /* TODO: Docs */
286c2e6f3c0SFlorian Wechsung PetscErrorCode PCPatchSetMultiplicative(PC pc, PetscBool flg)
287c2e6f3c0SFlorian Wechsung {
288c2e6f3c0SFlorian Wechsung   PC_PATCH *patch = (PC_PATCH *) pc->data;
289c2e6f3c0SFlorian Wechsung   PetscFunctionBegin;
290c2e6f3c0SFlorian Wechsung   patch->multiplicative = flg;
291c2e6f3c0SFlorian Wechsung   PetscFunctionReturn(0);
292c2e6f3c0SFlorian Wechsung }
293c2e6f3c0SFlorian Wechsung 
294c2e6f3c0SFlorian Wechsung /* TODO: Docs */
295c2e6f3c0SFlorian Wechsung PetscErrorCode PCPatchGetMultiplicative(PC pc, PetscBool *flg)
296c2e6f3c0SFlorian Wechsung {
297c2e6f3c0SFlorian Wechsung   PC_PATCH *patch = (PC_PATCH *) pc->data;
298c2e6f3c0SFlorian Wechsung   PetscFunctionBegin;
299c2e6f3c0SFlorian Wechsung   *flg = patch->multiplicative;
300c2e6f3c0SFlorian Wechsung   PetscFunctionReturn(0);
301c2e6f3c0SFlorian Wechsung }
302c2e6f3c0SFlorian Wechsung 
303c2e6f3c0SFlorian Wechsung /* TODO: Docs */
3044bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetSubMatType(PC pc, MatType sub_mat_type)
3054bbf5ea8SMatthew G. Knepley {
3064bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
3074bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
3084bbf5ea8SMatthew G. Knepley 
3094bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3104bbf5ea8SMatthew G. Knepley   if (patch->sub_mat_type) {ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr);}
3114bbf5ea8SMatthew G. Knepley   ierr = PetscStrallocpy(sub_mat_type, (char **) &patch->sub_mat_type);CHKERRQ(ierr);
3124bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3134bbf5ea8SMatthew G. Knepley }
3144bbf5ea8SMatthew G. Knepley 
3154bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3164bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetSubMatType(PC pc, MatType *sub_mat_type)
3174bbf5ea8SMatthew G. Knepley {
3184bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3194bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3204bbf5ea8SMatthew G. Knepley   *sub_mat_type = patch->sub_mat_type;
3214bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3224bbf5ea8SMatthew G. Knepley }
3234bbf5ea8SMatthew G. Knepley 
3244bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3254bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetCellNumbering(PC pc, PetscSection cellNumbering)
3264bbf5ea8SMatthew G. Knepley {
3274bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
3284bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
3294bbf5ea8SMatthew G. Knepley 
3304bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3314bbf5ea8SMatthew G. Knepley   patch->cellNumbering = cellNumbering;
3324bbf5ea8SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) cellNumbering);CHKERRQ(ierr);
3334bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3344bbf5ea8SMatthew G. Knepley }
3354bbf5ea8SMatthew G. Knepley 
3364bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3374bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetCellNumbering(PC pc, PetscSection *cellNumbering)
3384bbf5ea8SMatthew G. Knepley {
3394bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3404bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3414bbf5ea8SMatthew G. Knepley   *cellNumbering = patch->cellNumbering;
3424bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3434bbf5ea8SMatthew G. Knepley }
3444bbf5ea8SMatthew G. Knepley 
3454bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3464bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetConstructType(PC pc, PCPatchConstructType ctype, PetscErrorCode (*func)(PC, PetscInt *, IS **, IS *, void *), void *ctx)
3474bbf5ea8SMatthew G. Knepley {
3484bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3494bbf5ea8SMatthew G. Knepley 
3504bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3514bbf5ea8SMatthew G. Knepley   patch->ctype = ctype;
3524bbf5ea8SMatthew G. Knepley   switch (ctype) {
3534bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
35440c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
3554bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Star;
3564bbf5ea8SMatthew G. Knepley     break;
3574bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
35840c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
3594bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Vanka;
3604bbf5ea8SMatthew G. Knepley     break;
3614bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
3624bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
3634bbf5ea8SMatthew G. Knepley     patch->user_patches     = PETSC_TRUE;
3644bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_User;
365bdd9e0cdSPatrick Farrell     if (func) {
3664bbf5ea8SMatthew G. Knepley       patch->userpatchconstructionop = func;
3674bbf5ea8SMatthew G. Knepley       patch->userpatchconstructctx   = ctx;
368bdd9e0cdSPatrick Farrell     }
3694bbf5ea8SMatthew G. Knepley     break;
3704bbf5ea8SMatthew G. Knepley   default:
3714bbf5ea8SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype);
3724bbf5ea8SMatthew G. Knepley   }
3734bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3744bbf5ea8SMatthew G. Knepley }
3754bbf5ea8SMatthew G. Knepley 
3764bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3774bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetConstructType(PC pc, PCPatchConstructType *ctype, PetscErrorCode (**func)(PC, PetscInt *, IS **, IS *, void *), void **ctx)
3784bbf5ea8SMatthew G. Knepley {
3794bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3804bbf5ea8SMatthew G. Knepley 
3814bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3824bbf5ea8SMatthew G. Knepley   *ctype = patch->ctype;
3834bbf5ea8SMatthew G. Knepley   switch (patch->ctype) {
3844bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
3854bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
3864bbf5ea8SMatthew G. Knepley     break;
3874bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
3884bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
3894bbf5ea8SMatthew G. Knepley     *func = patch->userpatchconstructionop;
3904bbf5ea8SMatthew G. Knepley     *ctx  = patch->userpatchconstructctx;
3914bbf5ea8SMatthew G. Knepley     break;
3924bbf5ea8SMatthew G. Knepley   default:
3934bbf5ea8SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype);
3944bbf5ea8SMatthew G. Knepley   }
3954bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3964bbf5ea8SMatthew G. Knepley }
3974bbf5ea8SMatthew G. Knepley 
3984bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3994bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfo(PC pc, PetscInt nsubspaces, DM *dms, PetscInt *bs, PetscInt *nodesPerCell, const PetscInt **cellNodeMap,
4004bbf5ea8SMatthew G. Knepley                                             const PetscInt *subspaceOffsets, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
4014bbf5ea8SMatthew G. Knepley {
4024bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
4035f824522SMatthew G. Knepley   DM             dm;
4044bbf5ea8SMatthew G. Knepley   PetscSF       *sfs;
4055f824522SMatthew G. Knepley   PetscInt       cStart, cEnd, i, j;
4064bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
4074bbf5ea8SMatthew G. Knepley 
4084bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4095f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
4105f824522SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
4114bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &sfs);CHKERRQ(ierr);
4124bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->dofSection);CHKERRQ(ierr);
4134bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->bs);CHKERRQ(ierr);
4144bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->nodesPerCell);CHKERRQ(ierr);
4154bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->cellNodeMap);CHKERRQ(ierr);
4164bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces+1, &patch->subspaceOffsets);CHKERRQ(ierr);
4174bbf5ea8SMatthew G. Knepley 
4184bbf5ea8SMatthew G. Knepley   patch->nsubspaces       = nsubspaces;
4194bbf5ea8SMatthew G. Knepley   patch->totalDofsPerCell = 0;
4204bbf5ea8SMatthew G. Knepley   for (i = 0; i < nsubspaces; ++i) {
4214bbf5ea8SMatthew G. Knepley     ierr = DMGetDefaultSection(dms[i], &patch->dofSection[i]);CHKERRQ(ierr);
4224bbf5ea8SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) patch->dofSection[i]);CHKERRQ(ierr);
4234bbf5ea8SMatthew G. Knepley     ierr = DMGetDefaultSF(dms[i], &sfs[i]);CHKERRQ(ierr);
4244bbf5ea8SMatthew G. Knepley     patch->bs[i]              = bs[i];
4254bbf5ea8SMatthew G. Knepley     patch->nodesPerCell[i]    = nodesPerCell[i];
4264bbf5ea8SMatthew G. Knepley     patch->totalDofsPerCell  += nodesPerCell[i]*bs[i];
4275f824522SMatthew G. Knepley     ierr = PetscMalloc1((cEnd-cStart)*nodesPerCell[i]*bs[i], &patch->cellNodeMap[i]);CHKERRQ(ierr);
4285f824522SMatthew G. Knepley     for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]*bs[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
4294bbf5ea8SMatthew G. Knepley     patch->subspaceOffsets[i] = subspaceOffsets[i];
4304bbf5ea8SMatthew G. Knepley   }
4314bbf5ea8SMatthew G. Knepley   ierr = PCPatchCreateDefaultSF_Private(pc, nsubspaces, sfs, patch->bs);CHKERRQ(ierr);
4324bbf5ea8SMatthew G. Knepley   ierr = PetscFree(sfs);CHKERRQ(ierr);
4334bbf5ea8SMatthew G. Knepley 
4344bbf5ea8SMatthew G. Knepley   patch->subspaceOffsets[nsubspaces] = subspaceOffsets[nsubspaces];
4354bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes);CHKERRQ(ierr);
4364bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes);CHKERRQ(ierr);
4374bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4384bbf5ea8SMatthew G. Knepley }
4394bbf5ea8SMatthew G. Knepley 
4404bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4415f824522SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfoCombined(PC pc, DM dm, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
4425f824522SMatthew G. Knepley {
4435f824522SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
4445f824522SMatthew G. Knepley   PetscInt       cStart, cEnd, i, j;
4455f824522SMatthew G. Knepley   PetscErrorCode ierr;
4465f824522SMatthew G. Knepley 
4475f824522SMatthew G. Knepley   PetscFunctionBegin;
4485f824522SMatthew G. Knepley   patch->combined = PETSC_TRUE;
4495f824522SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
4505f824522SMatthew G. Knepley   ierr = DMGetNumFields(dm, &patch->nsubspaces);CHKERRQ(ierr);
4515f824522SMatthew G. Knepley   ierr = PetscCalloc1(patch->nsubspaces, &patch->dofSection);CHKERRQ(ierr);
4525f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->bs);CHKERRQ(ierr);
4535f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->nodesPerCell);CHKERRQ(ierr);
4545f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->cellNodeMap);CHKERRQ(ierr);
4555f824522SMatthew G. Knepley   ierr = PetscCalloc1(patch->nsubspaces+1, &patch->subspaceOffsets);CHKERRQ(ierr);
4565f824522SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &patch->dofSection[0]);CHKERRQ(ierr);
4575f824522SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) patch->dofSection[0]);CHKERRQ(ierr);
4585f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(patch->dofSection[0], &patch->subspaceOffsets[patch->nsubspaces]);CHKERRQ(ierr);
4595f824522SMatthew G. Knepley   patch->totalDofsPerCell = 0;
4605f824522SMatthew G. Knepley   for (i = 0; i < patch->nsubspaces; ++i) {
4615f824522SMatthew G. Knepley     patch->bs[i]             = 1;
4625f824522SMatthew G. Knepley     patch->nodesPerCell[i]   = nodesPerCell[i];
4635f824522SMatthew G. Knepley     patch->totalDofsPerCell += nodesPerCell[i];
4645f824522SMatthew G. Knepley     ierr = PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i]);CHKERRQ(ierr);
4655f824522SMatthew G. Knepley     for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
4665f824522SMatthew G. Knepley   }
4675f824522SMatthew G. Knepley   ierr = DMGetDefaultSF(dm, &patch->defaultSF);CHKERRQ(ierr);
4685f824522SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) patch->defaultSF);CHKERRQ(ierr);
4695f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes);CHKERRQ(ierr);
4705f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes);CHKERRQ(ierr);
4715f824522SMatthew G. Knepley   PetscFunctionReturn(0);
4725f824522SMatthew G. Knepley }
4735f824522SMatthew G. Knepley 
4745f824522SMatthew G. Knepley /*@C
4755f824522SMatthew G. Knepley 
4765f824522SMatthew G. Knepley   PCPatchSetComputeOperator - Set the callback used to compute patch matrices
4775f824522SMatthew G. Knepley 
4785f824522SMatthew G. Knepley   Input Parameters:
4795f824522SMatthew G. Knepley + pc   - The PC
4805f824522SMatthew G. Knepley . func - The callback
4815f824522SMatthew G. Knepley - ctx  - The user context
4825f824522SMatthew G. Knepley 
4835f824522SMatthew G. Knepley   Level: advanced
4845f824522SMatthew G. Knepley 
4855f824522SMatthew G. Knepley   Note:
4865f824522SMatthew G. Knepley   The callback has signature:
487bdd9e0cdSPatrick Farrell +  usercomputeop(pc, point, mat, cellIS, n, u, ctx)
4885f824522SMatthew G. Knepley +  pc     - The PC
489bdd9e0cdSPatrick Farrell +  point  - The point
4905f824522SMatthew G. Knepley +  mat    - The patch matrix
4916f158342SMatthew G. Knepley +  cellIS - An array of the cell numbers
4925f824522SMatthew G. Knepley +  n      - The size of g2l
4935f824522SMatthew G. Knepley +  g2l    - The global to local dof translation table
4945f824522SMatthew G. Knepley +  ctx    - The user context
4955f824522SMatthew G. Knepley   and can assume that the matrix entries have been set to zero before the call.
4965f824522SMatthew G. Knepley 
4975f824522SMatthew G. Knepley .seealso: PCPatchGetComputeOperator(), PCPatchSetDiscretisationInfo()
4985f824522SMatthew G. Knepley @*/
4996f158342SMatthew G. Knepley PetscErrorCode PCPatchSetComputeOperator(PC pc, PetscErrorCode (*func)(PC, PetscInt, Mat, IS, PetscInt, const PetscInt *, void *), void *ctx)
5004bbf5ea8SMatthew G. Knepley {
5014bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
5024bbf5ea8SMatthew G. Knepley 
5034bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
5044bbf5ea8SMatthew G. Knepley   patch->usercomputeop  = func;
5054bbf5ea8SMatthew G. Knepley   patch->usercomputectx = ctx;
5064bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5074bbf5ea8SMatthew G. Knepley }
5084bbf5ea8SMatthew G. Knepley 
5094bbf5ea8SMatthew G. Knepley /* On entry, ht contains the topological entities whose dofs we are responsible for solving for;
5104bbf5ea8SMatthew G. Knepley    on exit, cht contains all the topological entities we need to compute their residuals.
5114bbf5ea8SMatthew G. Knepley    In full generality this should incorporate knowledge of the sparsity pattern of the matrix;
5124bbf5ea8SMatthew G. Knepley    here we assume a standard FE sparsity pattern.*/
5134bbf5ea8SMatthew G. Knepley /* TODO: Use DMPlexGetAdjacency() */
5141b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchCompleteCellPatch(PC pc, PetscHSetI ht, PetscHSetI cht)
5154bbf5ea8SMatthew G. Knepley {
5165f824522SMatthew G. Knepley   DM             dm;
5171b68eb51SMatthew G. Knepley   PetscHashIter  hi;
5184bbf5ea8SMatthew G. Knepley   PetscInt       point;
5194bbf5ea8SMatthew G. Knepley   PetscInt      *star = NULL, *closure = NULL;
5204c954380SMatthew G. Knepley   PetscInt       ignoredim, iStart = 0, iEnd = -1, starSize, closureSize, si, ci;
5214bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
5224bbf5ea8SMatthew G. Knepley 
5234bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
5245f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
5255f824522SMatthew G. Knepley   ierr = PCPatchGetIgnoreDim(pc, &ignoredim);CHKERRQ(ierr);
5265f824522SMatthew G. Knepley   if (ignoredim >= 0) {ierr = DMPlexGetDepthStratum(dm, ignoredim, &iStart, &iEnd);CHKERRQ(ierr);}
5271b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(cht);CHKERRQ(ierr);
5281b68eb51SMatthew G. Knepley   PetscHashIterBegin(ht, hi);
5291b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(ht, hi)) {
5304c954380SMatthew G. Knepley 
5311b68eb51SMatthew G. Knepley     PetscHashIterGetKey(ht, hi, point);
5321b68eb51SMatthew G. Knepley     PetscHashIterNext(ht, hi);
5334bbf5ea8SMatthew G. Knepley 
5344bbf5ea8SMatthew G. Knepley     /* Loop over all the cells that this point connects to */
5354bbf5ea8SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
5365f824522SMatthew G. Knepley     for (si = 0; si < starSize*2; si += 2) {
5374c954380SMatthew G. Knepley       const PetscInt ownedpoint = star[si];
5385f824522SMatthew G. Knepley       /* TODO Check for point in cht before running through closure again */
5394bbf5ea8SMatthew G. Knepley       /* now loop over all entities in the closure of that cell */
5404bbf5ea8SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, ownedpoint, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
5415f824522SMatthew G. Knepley       for (ci = 0; ci < closureSize*2; ci += 2) {
5424c954380SMatthew G. Knepley         const PetscInt seenpoint = closure[ci];
5435f824522SMatthew G. Knepley         if (ignoredim >= 0 && seenpoint >= iStart && seenpoint < iEnd) continue;
5441b68eb51SMatthew G. Knepley         ierr = PetscHSetIAdd(cht, seenpoint);CHKERRQ(ierr);
5454bbf5ea8SMatthew G. Knepley       }
5464bbf5ea8SMatthew G. Knepley     }
5474bbf5ea8SMatthew G. Knepley   }
5484c954380SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, 0, PETSC_TRUE, NULL, &closure);CHKERRQ(ierr);
5495f824522SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, 0, PETSC_FALSE, NULL, &star);CHKERRQ(ierr);
5505f824522SMatthew G. Knepley   PetscFunctionReturn(0);
5515f824522SMatthew G. Knepley }
5525f824522SMatthew G. Knepley 
5535f824522SMatthew G. Knepley static PetscErrorCode PCPatchGetGlobalDofs(PC pc, PetscSection dofSection[], PetscInt f, PetscBool combined, PetscInt p, PetscInt *dof, PetscInt *off)
5545f824522SMatthew G. Knepley {
5555f824522SMatthew G. Knepley   PetscErrorCode ierr;
5565f824522SMatthew G. Knepley 
5575f824522SMatthew G. Knepley   PetscFunctionBegin;
5585f824522SMatthew G. Knepley   if (combined) {
5595f824522SMatthew G. Knepley     if (f < 0) {
5605f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetDof(dofSection[0], p, dof);CHKERRQ(ierr);}
5615f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetOffset(dofSection[0], p, off);CHKERRQ(ierr);}
5625f824522SMatthew G. Knepley     } else {
5635f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetFieldDof(dofSection[0], p, f, dof);CHKERRQ(ierr);}
5645f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetFieldOffset(dofSection[0], p, f, off);CHKERRQ(ierr);}
5655f824522SMatthew G. Knepley     }
5665f824522SMatthew G. Knepley   } else {
5675f824522SMatthew G. Knepley     if (f < 0) {
5685f824522SMatthew G. Knepley       PC_PATCH *patch = (PC_PATCH *) pc->data;
5695f824522SMatthew G. Knepley       PetscInt  fdof, g;
5705f824522SMatthew G. Knepley 
5715f824522SMatthew G. Knepley       if (dof) {
5725f824522SMatthew G. Knepley         *dof = 0;
5735f824522SMatthew G. Knepley         for (g = 0; g < patch->nsubspaces; ++g) {
5745f824522SMatthew G. Knepley           ierr = PetscSectionGetDof(dofSection[g], p, &fdof);CHKERRQ(ierr);
5755f824522SMatthew G. Knepley           *dof += fdof;
5765f824522SMatthew G. Knepley         }
5775f824522SMatthew G. Knepley       }
578624e31c3SLawrence Mitchell       if (off) {
579624e31c3SLawrence Mitchell         *off = 0;
580624e31c3SLawrence Mitchell         for (g = 0; g < patch->nsubspaces; ++g) {
581624e31c3SLawrence Mitchell           ierr = PetscSectionGetOffset(dofSection[g], p, &fdof);CHKERRQ(ierr);
582624e31c3SLawrence Mitchell           *off += fdof;
583624e31c3SLawrence Mitchell         }
584624e31c3SLawrence Mitchell       }
5855f824522SMatthew G. Knepley     } else {
5865f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetDof(dofSection[f], p, dof);CHKERRQ(ierr);}
5875f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetOffset(dofSection[f], p, off);CHKERRQ(ierr);}
5885f824522SMatthew G. Knepley     }
5895f824522SMatthew G. Knepley   }
5904bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5914bbf5ea8SMatthew G. Knepley }
5924bbf5ea8SMatthew G. Knepley 
5934bbf5ea8SMatthew G. Knepley /* Given a hash table with a set of topological entities (pts), compute the degrees of
5944bbf5ea8SMatthew G. Knepley    freedom in global concatenated numbering on those entities.
5954bbf5ea8SMatthew G. Knepley    For Vanka smoothing, this needs to do something special: ignore dofs of the
5964bbf5ea8SMatthew G. Knepley    constraint subspace on entities that aren't the base entity we're building the patch
5974bbf5ea8SMatthew G. Knepley    around. */
598e4c66b91SPatrick Farrell static PetscErrorCode PCPatchGetPointDofs(PC pc, PetscHSetI pts, PetscHSetI dofs, PetscInt base, PetscHSetI* subspaces_to_exclude)
5994bbf5ea8SMatthew G. Knepley {
6005f824522SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
6011b68eb51SMatthew G. Knepley   PetscHashIter  hi;
6024bbf5ea8SMatthew G. Knepley   PetscInt       ldof, loff;
6034bbf5ea8SMatthew G. Knepley   PetscInt       k, p;
6044bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
6054bbf5ea8SMatthew G. Knepley 
6064bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
6071b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(dofs);CHKERRQ(ierr);
6084bbf5ea8SMatthew G. Knepley   for (k = 0; k < patch->nsubspaces; ++k) {
6094bbf5ea8SMatthew G. Knepley     PetscInt subspaceOffset = patch->subspaceOffsets[k];
6104bbf5ea8SMatthew G. Knepley     PetscInt bs             = patch->bs[k];
6114bbf5ea8SMatthew G. Knepley     PetscInt j, l;
6124bbf5ea8SMatthew G. Knepley 
613e4c66b91SPatrick Farrell     if (subspaces_to_exclude != NULL) {
614e4c66b91SPatrick Farrell       PetscBool should_exclude_k = PETSC_FALSE;
615e4c66b91SPatrick Farrell       PetscHSetIHas(*subspaces_to_exclude, k, &should_exclude_k);
616e4c66b91SPatrick Farrell       if (should_exclude_k) {
6174bbf5ea8SMatthew G. Knepley         /* only get this subspace dofs at the base entity, not any others */
6185f824522SMatthew G. Knepley         ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, base, &ldof, &loff);CHKERRQ(ierr);
6194bbf5ea8SMatthew G. Knepley         if (0 == ldof) continue;
6204bbf5ea8SMatthew G. Knepley         for (j = loff; j < ldof + loff; ++j) {
6214bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
6224bbf5ea8SMatthew G. Knepley             PetscInt dof = bs*j + l + subspaceOffset;
6231b68eb51SMatthew G. Knepley             ierr = PetscHSetIAdd(dofs, dof);CHKERRQ(ierr);
6244bbf5ea8SMatthew G. Knepley           }
6254bbf5ea8SMatthew G. Knepley         }
6264bbf5ea8SMatthew G. Knepley         continue; /* skip the other dofs of this subspace */
6274bbf5ea8SMatthew G. Knepley       }
628e4c66b91SPatrick Farrell     }
6294bbf5ea8SMatthew G. Knepley 
6301b68eb51SMatthew G. Knepley     PetscHashIterBegin(pts, hi);
6311b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(pts, hi)) {
6321b68eb51SMatthew G. Knepley       PetscHashIterGetKey(pts, hi, p);
6331b68eb51SMatthew G. Knepley       PetscHashIterNext(pts, hi);
6345f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, p, &ldof, &loff);CHKERRQ(ierr);
6354bbf5ea8SMatthew G. Knepley       if (0 == ldof) continue;
6364bbf5ea8SMatthew G. Knepley       for (j = loff; j < ldof + loff; ++j) {
6374bbf5ea8SMatthew G. Knepley         for (l = 0; l < bs; ++l) {
6384bbf5ea8SMatthew G. Knepley           PetscInt dof = bs*j + l + subspaceOffset;
6391b68eb51SMatthew G. Knepley           ierr = PetscHSetIAdd(dofs, dof);CHKERRQ(ierr);
6404bbf5ea8SMatthew G. Knepley         }
6414bbf5ea8SMatthew G. Knepley       }
6424bbf5ea8SMatthew G. Knepley     }
6434bbf5ea8SMatthew G. Knepley   }
6444bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
6454bbf5ea8SMatthew G. Knepley }
6464bbf5ea8SMatthew G. Knepley 
6474bbf5ea8SMatthew G. Knepley /* Given two hash tables A and B, compute the keys in B that are not in A, and put them in C */
6481b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchComputeSetDifference_Private(PetscHSetI A, PetscHSetI B, PetscHSetI C)
6494bbf5ea8SMatthew G. Knepley {
6501b68eb51SMatthew G. Knepley   PetscHashIter  hi;
6511b68eb51SMatthew G. Knepley   PetscInt       key;
6524bbf5ea8SMatthew G. Knepley   PetscBool      flg;
6531b68eb51SMatthew G. Knepley   PetscErrorCode ierr;
6544bbf5ea8SMatthew G. Knepley 
6554bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
6561b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(C);CHKERRQ(ierr);
6571b68eb51SMatthew G. Knepley   PetscHashIterBegin(B, hi);
6581b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(B, hi)) {
6591b68eb51SMatthew G. Knepley     PetscHashIterGetKey(B, hi, key);
6601b68eb51SMatthew G. Knepley     PetscHashIterNext(B, hi);
6611b68eb51SMatthew G. Knepley     ierr = PetscHSetIHas(A, key, &flg);CHKERRQ(ierr);
6621b68eb51SMatthew G. Knepley     if (!flg) {ierr = PetscHSetIAdd(C, key);CHKERRQ(ierr);}
6634bbf5ea8SMatthew G. Knepley   }
6644bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
6654bbf5ea8SMatthew G. Knepley }
6664bbf5ea8SMatthew G. Knepley 
6674bbf5ea8SMatthew G. Knepley /*
6684bbf5ea8SMatthew G. Knepley  * PCPatchCreateCellPatches - create patches.
6694bbf5ea8SMatthew G. Knepley  *
6704bbf5ea8SMatthew G. Knepley  * Input Parameters:
6714bbf5ea8SMatthew G. Knepley  * + dm - The DMPlex object defining the mesh
6724bbf5ea8SMatthew G. Knepley  *
6734bbf5ea8SMatthew G. Knepley  * Output Parameters:
6744bbf5ea8SMatthew G. Knepley  * + cellCounts  - Section with counts of cells around each vertex
6755f824522SMatthew G. Knepley  * . cells       - IS of the cell point indices of cells in each patch
6765f824522SMatthew G. Knepley  * . pointCounts - Section with counts of cells around each vertex
6775f824522SMatthew G. Knepley  * - point       - IS of the cell point indices of cells in each patch
6784bbf5ea8SMatthew G. Knepley  */
6794bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatches(PC pc)
6804bbf5ea8SMatthew G. Knepley {
6814bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
6825f824522SMatthew G. Knepley   DMLabel         ghost = NULL;
6834bbf5ea8SMatthew G. Knepley   DM              dm, plex;
6841b68eb51SMatthew G. Knepley   PetscHSetI      ht, cht;
6855f824522SMatthew G. Knepley   PetscSection    cellCounts,  pointCounts;
6865f824522SMatthew G. Knepley   PetscInt       *cellsArray, *pointsArray;
6875f824522SMatthew G. Knepley   PetscInt        numCells,    numPoints;
6885f824522SMatthew G. Knepley   const PetscInt *leaves;
6895f824522SMatthew G. Knepley   PetscInt        nleaves, pStart, pEnd, cStart, cEnd, vStart, vEnd, v;
6905f824522SMatthew G. Knepley   PetscBool       isFiredrake;
6914bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
6924bbf5ea8SMatthew G. Knepley 
6934bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
6944bbf5ea8SMatthew G. Knepley   /* Used to keep track of the cells in the patch. */
6951b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&ht);CHKERRQ(ierr);
6961b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&cht);CHKERRQ(ierr);
6974bbf5ea8SMatthew G. Knepley 
6984bbf5ea8SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
6994bbf5ea8SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONGSTATE, "DM not yet set on patch PC\n");
7004bbf5ea8SMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
7014bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetChart(plex, &pStart, &pEnd);CHKERRQ(ierr);
7024bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr);
7034bbf5ea8SMatthew G. Knepley 
7044bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
7055f824522SMatthew G. Knepley     ierr = patch->userpatchconstructionop(pc, &patch->npatch, &patch->userIS, &patch->iterationSet, patch->userpatchconstructctx);CHKERRQ(ierr);
7065f824522SMatthew G. Knepley     vStart = 0; vEnd = patch->npatch;
7075f824522SMatthew G. Knepley   } else if (patch->codim < 0) {
7085f824522SMatthew G. Knepley     if (patch->dim < 0) {ierr = DMPlexGetDepthStratum(plex,  0,            &vStart, &vEnd);CHKERRQ(ierr);}
7095f824522SMatthew G. Knepley     else                {ierr = DMPlexGetDepthStratum(plex,  patch->dim,   &vStart, &vEnd);CHKERRQ(ierr);}
7105f824522SMatthew G. Knepley   } else                {ierr = DMPlexGetHeightStratum(plex, patch->codim, &vStart, &vEnd);CHKERRQ(ierr);}
7115f824522SMatthew G. Knepley   patch->npatch = vEnd - vStart;
7124bbf5ea8SMatthew G. Knepley 
7134bbf5ea8SMatthew G. Knepley   /* These labels mark the owned points.  We only create patches around points that this process owns. */
7145f824522SMatthew G. Knepley   ierr = DMHasLabel(dm, "pyop2_ghost", &isFiredrake);CHKERRQ(ierr);
7155f824522SMatthew G. Knepley   if (isFiredrake) {
7164bbf5ea8SMatthew G. Knepley     ierr = DMGetLabel(dm, "pyop2_ghost", &ghost);CHKERRQ(ierr);
7174bbf5ea8SMatthew G. Knepley     ierr = DMLabelCreateIndex(ghost, pStart, pEnd);CHKERRQ(ierr);
7185f824522SMatthew G. Knepley   } else {
7195f824522SMatthew G. Knepley     PetscSF sf;
7205f824522SMatthew G. Knepley 
7215f824522SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
7225f824522SMatthew G. Knepley     ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
7235f824522SMatthew G. Knepley     nleaves = PetscMax(nleaves, 0);
7245f824522SMatthew G. Knepley   }
7254bbf5ea8SMatthew G. Knepley 
7264bbf5ea8SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->cellCounts);CHKERRQ(ierr);
7275f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->cellCounts, "Patch Cell Layout");CHKERRQ(ierr);
7284bbf5ea8SMatthew G. Knepley   cellCounts = patch->cellCounts;
7294bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetChart(cellCounts, vStart, vEnd);CHKERRQ(ierr);
7305f824522SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->pointCounts);CHKERRQ(ierr);
7315f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->pointCounts, "Patch Point Layout");CHKERRQ(ierr);
7325f824522SMatthew G. Knepley   pointCounts = patch->pointCounts;
7335f824522SMatthew G. Knepley   ierr = PetscSectionSetChart(pointCounts, vStart, vEnd);CHKERRQ(ierr);
7345f824522SMatthew G. Knepley   /* Count cells and points in the patch surrounding each entity */
7354bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
7361b68eb51SMatthew G. Knepley     PetscHashIter hi;
7375f824522SMatthew G. Knepley     PetscInt       chtSize, loc = -1;
7385f824522SMatthew G. Knepley     PetscBool      flg;
7394bbf5ea8SMatthew G. Knepley 
7404bbf5ea8SMatthew G. Knepley     if (!patch->user_patches) {
7415f824522SMatthew G. Knepley       if (ghost) {ierr = DMLabelHasPoint(ghost, v, &flg);CHKERRQ(ierr);}
742928bb9adSStefano Zampini       else       {ierr = PetscFindInt(v, nleaves, leaves, &loc);CHKERRQ(ierr); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;}
7434bbf5ea8SMatthew G. Knepley       /* Not an owned entity, don't make a cell patch. */
7444bbf5ea8SMatthew G. Knepley       if (flg) continue;
7454bbf5ea8SMatthew G. Knepley     }
7464bbf5ea8SMatthew G. Knepley 
7474bbf5ea8SMatthew G. Knepley     ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr);
7485f824522SMatthew G. Knepley     ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr);
7491b68eb51SMatthew G. Knepley     ierr = PetscHSetIGetSize(cht, &chtSize);CHKERRQ(ierr);
7504bbf5ea8SMatthew G. Knepley     /* empty patch, continue */
7514bbf5ea8SMatthew G. Knepley     if (chtSize == 0) continue;
7524bbf5ea8SMatthew G. Knepley 
7534bbf5ea8SMatthew G. Knepley     /* safe because size(cht) > 0 from above */
7541b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
7551b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
7565f824522SMatthew G. Knepley       PetscInt point, pdof;
7574bbf5ea8SMatthew G. Knepley 
7581b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
7595f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr);
7605f824522SMatthew G. Knepley       if (pdof)                            {ierr = PetscSectionAddDof(pointCounts, v, 1);CHKERRQ(ierr);}
7615f824522SMatthew G. Knepley       if (point >= cStart && point < cEnd) {ierr = PetscSectionAddDof(cellCounts, v, 1);CHKERRQ(ierr);}
7621b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
7634bbf5ea8SMatthew G. Knepley     }
7644bbf5ea8SMatthew G. Knepley   }
7655f824522SMatthew G. Knepley   if (isFiredrake) {ierr = DMLabelDestroyIndex(ghost);CHKERRQ(ierr);}
7664bbf5ea8SMatthew G. Knepley 
7674bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetUp(cellCounts);CHKERRQ(ierr);
7684bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr);
7694bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numCells, &cellsArray);CHKERRQ(ierr);
7705f824522SMatthew G. Knepley   ierr = PetscSectionSetUp(pointCounts);CHKERRQ(ierr);
7715f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(pointCounts, &numPoints);CHKERRQ(ierr);
7725f824522SMatthew G. Knepley   ierr = PetscMalloc1(numPoints, &pointsArray);CHKERRQ(ierr);
7734bbf5ea8SMatthew G. Knepley 
7744bbf5ea8SMatthew G. Knepley   /* Now that we know how much space we need, run through again and actually remember the cells. */
7754bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; v++ ) {
7761b68eb51SMatthew G. Knepley     PetscHashIter hi;
7775f824522SMatthew G. Knepley     PetscInt       dof, off, cdof, coff, pdof, n = 0, cn = 0;
7784bbf5ea8SMatthew G. Knepley 
7795f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(pointCounts, v, &dof);CHKERRQ(ierr);
7805f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(pointCounts, v, &off);CHKERRQ(ierr);
7815f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &cdof);CHKERRQ(ierr);
7825f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &coff);CHKERRQ(ierr);
7835f824522SMatthew G. Knepley     if (dof <= 0) continue;
7844bbf5ea8SMatthew G. Knepley     ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr);
7855f824522SMatthew G. Knepley     ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr);
7861b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
7871b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
7884bbf5ea8SMatthew G. Knepley       PetscInt point;
7894bbf5ea8SMatthew G. Knepley 
7901b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
7915f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr);
7925f824522SMatthew G. Knepley       if (pdof)                            {pointsArray[off + n++] = point;}
7935f824522SMatthew G. Knepley       if (point >= cStart && point < cEnd) {cellsArray[coff + cn++] = point;}
7941b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
7954bbf5ea8SMatthew G. Knepley     }
7965f824522SMatthew 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);
7975f824522SMatthew 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);
7984bbf5ea8SMatthew G. Knepley   }
7991b68eb51SMatthew G. Knepley   ierr = PetscHSetIDestroy(&ht);CHKERRQ(ierr);
8001b68eb51SMatthew G. Knepley   ierr = PetscHSetIDestroy(&cht);CHKERRQ(ierr);
8014bbf5ea8SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
8025f824522SMatthew G. Knepley 
8035f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numCells,  cellsArray,  PETSC_OWN_POINTER, &patch->cells);CHKERRQ(ierr);
8045f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->cells,  "Patch Cells");CHKERRQ(ierr);
8055f824522SMatthew G. Knepley   if (patch->viewCells) {
8065f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->cellCounts, patch->viewerCells, patch->formatCells);CHKERRQ(ierr);
8075f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->cells,      patch->viewerCells, patch->formatCells);CHKERRQ(ierr);
8085f824522SMatthew G. Knepley   }
8095f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints, pointsArray, PETSC_OWN_POINTER, &patch->points);CHKERRQ(ierr);
8105f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->points, "Patch Points");CHKERRQ(ierr);
8115f824522SMatthew G. Knepley   if (patch->viewPoints) {
8125f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->pointCounts, patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr);
8135f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->points,      patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr);
8145f824522SMatthew G. Knepley   }
8154bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
8164bbf5ea8SMatthew G. Knepley }
8174bbf5ea8SMatthew G. Knepley 
8184bbf5ea8SMatthew G. Knepley /*
8194bbf5ea8SMatthew G. Knepley  * PCPatchCreateCellPatchDiscretisationInfo - Build the dof maps for cell patches
8204bbf5ea8SMatthew G. Knepley  *
8214bbf5ea8SMatthew G. Knepley  * Input Parameters:
8224bbf5ea8SMatthew G. Knepley  * + dm - The DMPlex object defining the mesh
8234bbf5ea8SMatthew G. Knepley  * . cellCounts - Section with counts of cells around each vertex
8244bbf5ea8SMatthew G. Knepley  * . cells - IS of the cell point indices of cells in each patch
8254bbf5ea8SMatthew G. Knepley  * . cellNumbering - Section mapping plex cell points to Firedrake cell indices.
8264bbf5ea8SMatthew G. Knepley  * . nodesPerCell - number of nodes per cell.
8274bbf5ea8SMatthew G. Knepley  * - cellNodeMap - map from cells to node indices (nodesPerCell * numCells)
8284bbf5ea8SMatthew G. Knepley  *
8294bbf5ea8SMatthew G. Knepley  * Output Parameters:
8305f824522SMatthew G. Knepley  * + dofs - IS of local dof numbers of each cell in the patch, where local is a patch local numbering
8314bbf5ea8SMatthew G. Knepley  * . gtolCounts - Section with counts of dofs per cell patch
8324bbf5ea8SMatthew G. Knepley  * - gtol - IS mapping from global dofs to local dofs for each patch.
8334bbf5ea8SMatthew G. Knepley  */
8344bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatchDiscretisationInfo(PC pc)
8354bbf5ea8SMatthew G. Knepley {
8364bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch           = (PC_PATCH *) pc->data;
8374bbf5ea8SMatthew G. Knepley   PetscSection    cellCounts      = patch->cellCounts;
8385f824522SMatthew G. Knepley   PetscSection    pointCounts     = patch->pointCounts;
839c2e6f3c0SFlorian Wechsung   PetscSection    gtolCounts, gtolCountsWithArtificial;
8404bbf5ea8SMatthew G. Knepley   IS              cells           = patch->cells;
8415f824522SMatthew G. Knepley   IS              points          = patch->points;
8424bbf5ea8SMatthew G. Knepley   PetscSection    cellNumbering   = patch->cellNumbering;
8435f824522SMatthew G. Knepley   PetscInt        Nf              = patch->nsubspaces;
8445f824522SMatthew G. Knepley   PetscInt        numCells, numPoints;
8454bbf5ea8SMatthew G. Knepley   PetscInt        numDofs;
846c2e6f3c0SFlorian Wechsung   PetscInt        numGlobalDofs, numGlobalDofsWithArtificial;
8474bbf5ea8SMatthew G. Knepley   PetscInt        totalDofsPerCell = patch->totalDofsPerCell;
8484bbf5ea8SMatthew G. Knepley   PetscInt        vStart, vEnd, v;
8495f824522SMatthew G. Knepley   const PetscInt *cellsArray, *pointsArray;
8504bbf5ea8SMatthew G. Knepley   PetscInt       *newCellsArray   = NULL;
8514bbf5ea8SMatthew G. Knepley   PetscInt       *dofsArray       = NULL;
852c2e6f3c0SFlorian Wechsung   PetscInt       *dofsArrayWithArtificial = NULL;
8535f824522SMatthew G. Knepley   PetscInt       *offsArray       = NULL;
854c2e6f3c0SFlorian Wechsung   PetscInt       *offsArrayWithArtificial = NULL;
8554bbf5ea8SMatthew G. Knepley   PetscInt       *asmArray        = NULL;
856c2e6f3c0SFlorian Wechsung   PetscInt       *asmArrayWithArtificial = NULL;
8574bbf5ea8SMatthew G. Knepley   PetscInt       *globalDofsArray = NULL;
858c2e6f3c0SFlorian Wechsung   PetscInt       *globalDofsArrayWithArtificial = NULL;
8594bbf5ea8SMatthew G. Knepley   PetscInt        globalIndex     = 0;
8604bbf5ea8SMatthew G. Knepley   PetscInt        key             = 0;
8614bbf5ea8SMatthew G. Knepley   PetscInt        asmKey          = 0;
862557beb66SLawrence Mitchell   DM              dm              = NULL;
863557beb66SLawrence Mitchell   const PetscInt *bcNodes         = NULL;
8641b68eb51SMatthew G. Knepley   PetscHMapI      ht;
865c2e6f3c0SFlorian Wechsung   PetscHMapI      htWithArtificial;
8661b68eb51SMatthew G. Knepley   PetscHSetI      globalBcs;
867557beb66SLawrence Mitchell   PetscInt        numBcs;
8681b68eb51SMatthew G. Knepley   PetscHSetI      ownedpts, seenpts, owneddofs, seendofs, artificialbcs;
869cda239d9SMatthew G. Knepley   PetscInt        pStart, pEnd, p, i;
8704bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
8714bbf5ea8SMatthew G. Knepley 
8724bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
873557beb66SLawrence Mitchell 
874557beb66SLawrence Mitchell   ierr = PCGetDM(pc, &dm); CHKERRQ(ierr);
8754bbf5ea8SMatthew G. Knepley   /* dofcounts section is cellcounts section * dofPerCell */
8764bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr);
8775f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(patch->pointCounts, &numPoints);CHKERRQ(ierr);
8784bbf5ea8SMatthew G. Knepley   numDofs = numCells * totalDofsPerCell;
8794bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numDofs, &dofsArray);CHKERRQ(ierr);
8805f824522SMatthew G. Knepley   ierr = PetscMalloc1(numPoints*Nf, &offsArray);CHKERRQ(ierr);
881c2e6f3c0SFlorian Wechsung   ierr = PetscMalloc1(numPoints*Nf, &offsArrayWithArtificial);CHKERRQ(ierr);
8824bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numDofs, &asmArray);CHKERRQ(ierr);
8834bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numCells, &newCellsArray);CHKERRQ(ierr);
8844bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(cellCounts, &vStart, &vEnd);CHKERRQ(ierr);
8854bbf5ea8SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCounts);CHKERRQ(ierr);
8864bbf5ea8SMatthew G. Knepley   gtolCounts = patch->gtolCounts;
8874bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetChart(gtolCounts, vStart, vEnd);CHKERRQ(ierr);
8885f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->gtolCounts, "Patch Global Index Section");CHKERRQ(ierr);
8894bbf5ea8SMatthew G. Knepley 
890c2e6f3c0SFlorian Wechsung   if(patch->multiplicative)
891c2e6f3c0SFlorian Wechsung   {
892c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numDofs, &asmArrayWithArtificial);CHKERRQ(ierr);
893c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numDofs, &dofsArrayWithArtificial);CHKERRQ(ierr);
894c2e6f3c0SFlorian Wechsung     ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithArtificial);CHKERRQ(ierr);
895c2e6f3c0SFlorian Wechsung     gtolCountsWithArtificial = patch->gtolCountsWithArtificial;
896c2e6f3c0SFlorian Wechsung     ierr = PetscSectionSetChart(gtolCountsWithArtificial, vStart, vEnd);CHKERRQ(ierr);
897c2e6f3c0SFlorian Wechsung     ierr = PetscObjectSetName((PetscObject) patch->gtolCountsWithArtificial, "Patch Global Index Section Including Artificial BCs");CHKERRQ(ierr);
898c2e6f3c0SFlorian Wechsung   }
899c2e6f3c0SFlorian Wechsung 
900557beb66SLawrence Mitchell   /* Outside the patch loop, get the dofs that are globally-enforced Dirichlet
901557beb66SLawrence Mitchell    conditions */
9021b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&globalBcs);CHKERRQ(ierr);
903557beb66SLawrence Mitchell   ierr = ISGetIndices(patch->ghostBcNodes, &bcNodes); CHKERRQ(ierr);
904557beb66SLawrence Mitchell   ierr = ISGetSize(patch->ghostBcNodes, &numBcs); CHKERRQ(ierr);
905cda239d9SMatthew G. Knepley   for (i = 0; i < numBcs; ++i) {
9061b68eb51SMatthew G. Knepley     ierr = PetscHSetIAdd(globalBcs, bcNodes[i]);CHKERRQ(ierr); /* these are already in concatenated numbering */
907557beb66SLawrence Mitchell   }
908557beb66SLawrence Mitchell   ierr = ISRestoreIndices(patch->ghostBcNodes, &bcNodes); CHKERRQ(ierr);
909557beb66SLawrence Mitchell   ierr = ISDestroy(&patch->ghostBcNodes); CHKERRQ(ierr); /* memory optimisation */
910557beb66SLawrence Mitchell 
911557beb66SLawrence Mitchell   /* Hash tables for artificial BC construction */
9121b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&ownedpts);CHKERRQ(ierr);
9131b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&seenpts);CHKERRQ(ierr);
9141b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&owneddofs);CHKERRQ(ierr);
9151b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&seendofs);CHKERRQ(ierr);
9161b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&artificialbcs);CHKERRQ(ierr);
917557beb66SLawrence Mitchell 
9184bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(cells, &cellsArray);CHKERRQ(ierr);
9195f824522SMatthew G. Knepley   ierr = ISGetIndices(points, &pointsArray);CHKERRQ(ierr);
9201b68eb51SMatthew G. Knepley   ierr = PetscHMapICreate(&ht);CHKERRQ(ierr);
921c2e6f3c0SFlorian Wechsung   ierr = PetscHMapICreate(&htWithArtificial);CHKERRQ(ierr);
9224bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
9234bbf5ea8SMatthew G. Knepley     PetscInt localIndex = 0;
924c2e6f3c0SFlorian Wechsung     PetscInt localIndexWithArtificial = 0;
9254bbf5ea8SMatthew G. Knepley     PetscInt dof, off, i, j, k, l;
9264bbf5ea8SMatthew G. Knepley 
9271b68eb51SMatthew G. Knepley     ierr = PetscHMapIClear(ht);CHKERRQ(ierr);
928c2e6f3c0SFlorian Wechsung     ierr = PetscHMapIClear(htWithArtificial);CHKERRQ(ierr);
9294bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr);
9304bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr);
9314bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
9324bbf5ea8SMatthew G. Knepley 
933557beb66SLawrence Mitchell     /* Calculate the global numbers of the artificial BC dofs here first */
934557beb66SLawrence Mitchell     ierr = patch->patchconstructop((void*)patch, dm, v, ownedpts); CHKERRQ(ierr);
935557beb66SLawrence Mitchell     ierr = PCPatchCompleteCellPatch(pc, ownedpts, seenpts); CHKERRQ(ierr);
936e4c66b91SPatrick Farrell     ierr = PCPatchGetPointDofs(pc, ownedpts, owneddofs, v, &patch->subspaces_to_exclude); CHKERRQ(ierr);
937e4c66b91SPatrick Farrell     ierr = PCPatchGetPointDofs(pc, seenpts, seendofs, v, NULL); CHKERRQ(ierr);
938557beb66SLawrence Mitchell     ierr = PCPatchComputeSetDifference_Private(owneddofs, seendofs, artificialbcs); CHKERRQ(ierr);
9398135ed82SLawrence Mitchell     if (patch->viewPatches) {
9401b68eb51SMatthew G. Knepley       PetscHSetI globalbcdofs;
9411b68eb51SMatthew G. Knepley       PetscHashIter hi;
9428135ed82SLawrence Mitchell       MPI_Comm comm = PetscObjectComm((PetscObject)pc);
9431b68eb51SMatthew G. Knepley 
9441b68eb51SMatthew G. Knepley       ierr = PetscHSetICreate(&globalbcdofs);CHKERRQ(ierr);
9458135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: owned dofs:\n", v); CHKERRQ(ierr);
9461b68eb51SMatthew G. Knepley       PetscHashIterBegin(owneddofs, hi);
9471b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(owneddofs, hi)) {
9488135ed82SLawrence Mitchell         PetscInt globalDof;
9498135ed82SLawrence Mitchell 
9501b68eb51SMatthew G. Knepley         PetscHashIterGetKey(owneddofs, hi, globalDof);
9511b68eb51SMatthew G. Knepley         PetscHashIterNext(owneddofs, hi);
9528135ed82SLawrence Mitchell         ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
9538135ed82SLawrence Mitchell       }
9548135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n"); CHKERRQ(ierr);
9558135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: seen dofs:\n", v); CHKERRQ(ierr);
9561b68eb51SMatthew G. Knepley       PetscHashIterBegin(seendofs, hi);
9571b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(seendofs, hi)) {
9588135ed82SLawrence Mitchell         PetscInt globalDof;
9598135ed82SLawrence Mitchell         PetscBool flg;
9608135ed82SLawrence Mitchell 
9611b68eb51SMatthew G. Knepley         PetscHashIterGetKey(seendofs, hi, globalDof);
9621b68eb51SMatthew G. Knepley         PetscHashIterNext(seendofs, hi);
9638135ed82SLawrence Mitchell         ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
9648135ed82SLawrence Mitchell 
9651b68eb51SMatthew G. Knepley         ierr = PetscHSetIHas(globalBcs, globalDof, &flg);CHKERRQ(ierr);
9661b68eb51SMatthew G. Knepley         if (flg) {ierr = PetscHSetIAdd(globalbcdofs, globalDof);CHKERRQ(ierr);}
9678135ed82SLawrence Mitchell       }
9688135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n"); CHKERRQ(ierr);
9698135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: global BCs:\n", v); CHKERRQ(ierr);
9701b68eb51SMatthew G. Knepley       ierr = PetscHSetIGetSize(globalbcdofs, &numBcs);CHKERRQ(ierr);
9718135ed82SLawrence Mitchell       if (numBcs > 0) {
9721b68eb51SMatthew G. Knepley         PetscHashIterBegin(globalbcdofs, hi);
9731b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(globalbcdofs, hi)) {
9748135ed82SLawrence Mitchell           PetscInt globalDof;
9751b68eb51SMatthew G. Knepley           PetscHashIterGetKey(globalbcdofs, hi, globalDof);
9761b68eb51SMatthew G. Knepley           PetscHashIterNext(globalbcdofs, hi);
9778135ed82SLawrence Mitchell           ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof);CHKERRQ(ierr);
9788135ed82SLawrence Mitchell         }
9798135ed82SLawrence Mitchell       }
9808135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n");CHKERRQ(ierr);
9818135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: artificial BCs:\n", v);CHKERRQ(ierr);
9821b68eb51SMatthew G. Knepley       ierr = PetscHSetIGetSize(artificialbcs, &numBcs);CHKERRQ(ierr);
9838135ed82SLawrence Mitchell       if (numBcs > 0) {
9841b68eb51SMatthew G. Knepley         PetscHashIterBegin(artificialbcs, hi);
9851b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(artificialbcs, hi)) {
9868135ed82SLawrence Mitchell           PetscInt globalDof;
9871b68eb51SMatthew G. Knepley           PetscHashIterGetKey(artificialbcs, hi, globalDof);
9881b68eb51SMatthew G. Knepley           PetscHashIterNext(artificialbcs, hi);
9898135ed82SLawrence Mitchell           ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
9908135ed82SLawrence Mitchell         }
9918135ed82SLawrence Mitchell       }
9928135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n\n"); CHKERRQ(ierr);
9931b68eb51SMatthew G. Knepley       ierr = PetscHSetIDestroy(&globalbcdofs);CHKERRQ(ierr);
9948135ed82SLawrence Mitchell     }
9954bbf5ea8SMatthew G. Knepley    for (k = 0; k < patch->nsubspaces; ++k) {
9964bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
9974bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
9984bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
9994bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
10004bbf5ea8SMatthew G. Knepley 
10014bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
10024bbf5ea8SMatthew G. Knepley         /* Walk over the cells in this patch. */
10034bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
10045f824522SMatthew G. Knepley         PetscInt       cell = c;
10054bbf5ea8SMatthew G. Knepley 
10065f824522SMatthew G. Knepley         /* TODO Change this to an IS */
10075f824522SMatthew G. Knepley         if (cellNumbering) {
10084bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetDof(cellNumbering, c, &cell);CHKERRQ(ierr);
10094bbf5ea8SMatthew G. Knepley           if (cell <= 0) SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_OUTOFRANGE, "Cell %D doesn't appear in cell numbering map", c);
10104bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);
10115f824522SMatthew G. Knepley         }
10124bbf5ea8SMatthew G. Knepley         newCellsArray[i] = cell;
10134bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
10144bbf5ea8SMatthew G. Knepley           /* For each global dof, map it into contiguous local storage. */
10154bbf5ea8SMatthew G. Knepley           const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + subspaceOffset;
10164bbf5ea8SMatthew G. Knepley           /* finally, loop over block size */
10174bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
10181b68eb51SMatthew G. Knepley             PetscInt  localDof;
10191b68eb51SMatthew G. Knepley             PetscBool isGlobalBcDof, isArtificialBcDof;
10204bbf5ea8SMatthew G. Knepley 
1021557beb66SLawrence Mitchell             /* first, check if this is either a globally enforced or locally enforced BC dof */
10221b68eb51SMatthew G. Knepley             ierr = PetscHSetIHas(globalBcs, globalDof + l, &isGlobalBcDof);CHKERRQ(ierr);
10231b68eb51SMatthew G. Knepley             ierr = PetscHSetIHas(artificialbcs, globalDof + l, &isArtificialBcDof);CHKERRQ(ierr);
1024557beb66SLawrence Mitchell 
1025557beb66SLawrence Mitchell             /* if it's either, don't ever give it a local dof number */
10261b68eb51SMatthew G. Knepley             if (isGlobalBcDof || isArtificialBcDof) {
1027c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = -1; /* don't use this in assembly in this patch */
1028557beb66SLawrence Mitchell             } else {
10291b68eb51SMatthew G. Knepley               ierr = PetscHMapIGet(ht, globalDof + l, &localDof);CHKERRQ(ierr);
10304bbf5ea8SMatthew G. Knepley               if (localDof == -1) {
10314bbf5ea8SMatthew G. Knepley                 localDof = localIndex++;
10321b68eb51SMatthew G. Knepley                 ierr = PetscHMapISet(ht, globalDof + l, localDof);CHKERRQ(ierr);
10334bbf5ea8SMatthew G. Knepley               }
10344bbf5ea8SMatthew G. Knepley               if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
10354bbf5ea8SMatthew G. Knepley               /* And store. */
1036c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = localDof;
10374bbf5ea8SMatthew G. Knepley             }
1038c2e6f3c0SFlorian Wechsung 
1039c2e6f3c0SFlorian Wechsung             if(patch->multiplicative)
1040c2e6f3c0SFlorian Wechsung             {
1041c2e6f3c0SFlorian Wechsung               if (isGlobalBcDof) {
1042c2e6f3c0SFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = -1; // don't use this in assembly in this patch
1043c2e6f3c0SFlorian Wechsung               } else {
1044c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapIGet(htWithArtificial, globalDof + l, &localDof);CHKERRQ(ierr);
1045c2e6f3c0SFlorian Wechsung                 if (localDof == -1) {
1046c2e6f3c0SFlorian Wechsung                   localDof = localIndexWithArtificial++;
1047c2e6f3c0SFlorian Wechsung                   ierr = PetscHMapISet(htWithArtificial, globalDof + l, localDof);CHKERRQ(ierr);
1048c2e6f3c0SFlorian Wechsung                 }
1049c2e6f3c0SFlorian Wechsung                 if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
1050c2e6f3c0SFlorian Wechsung                 /* And store.*/
1051c2e6f3c0SFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = localDof;
1052c2e6f3c0SFlorian Wechsung               }
1053c2e6f3c0SFlorian Wechsung             }
1054c2e6f3c0SFlorian Wechsung             globalIndex++;
10554bbf5ea8SMatthew G. Knepley           }
10564bbf5ea8SMatthew G. Knepley         }
10574bbf5ea8SMatthew G. Knepley       }
1058557beb66SLawrence Mitchell     }
10594bbf5ea8SMatthew G. Knepley      /*How many local dofs in this patch? */
1060c2e6f3c0SFlorian Wechsung     if(patch->multiplicative) {
1061c2e6f3c0SFlorian Wechsung         ierr = PetscHMapIGetSize(htWithArtificial, &dof);CHKERRQ(ierr);
1062c2e6f3c0SFlorian Wechsung         ierr = PetscSectionSetDof(gtolCountsWithArtificial, v, dof);CHKERRQ(ierr);
1063c2e6f3c0SFlorian Wechsung     }
10641b68eb51SMatthew G. Knepley     ierr = PetscHMapIGetSize(ht, &dof);CHKERRQ(ierr);
10654bbf5ea8SMatthew G. Knepley     ierr = PetscSectionSetDof(gtolCounts, v, dof);CHKERRQ(ierr);
10664bbf5ea8SMatthew G. Knepley   }
10674bbf5ea8SMatthew 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);
10684bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetUp(gtolCounts);CHKERRQ(ierr);
10694bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(gtolCounts, &numGlobalDofs);CHKERRQ(ierr);
10704bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numGlobalDofs, &globalDofsArray);CHKERRQ(ierr);
10714bbf5ea8SMatthew G. Knepley 
1072c2e6f3c0SFlorian Wechsung   if(patch->multiplicative) {
1073c2e6f3c0SFlorian Wechsung     ierr = PetscSectionSetUp(gtolCountsWithArtificial);CHKERRQ(ierr);
1074c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetStorageSize(gtolCountsWithArtificial, &numGlobalDofsWithArtificial);CHKERRQ(ierr);
1075c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numGlobalDofsWithArtificial, &globalDofsArrayWithArtificial);CHKERRQ(ierr);
1076c2e6f3c0SFlorian Wechsung   }
10774bbf5ea8SMatthew 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. */
10784bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
10791b68eb51SMatthew G. Knepley     PetscHashIter hi;
10805f824522SMatthew G. Knepley     PetscInt      dof, off, Np, ooff, i, j, k, l;
10814bbf5ea8SMatthew G. Knepley 
10821b68eb51SMatthew G. Knepley     ierr = PetscHMapIClear(ht);CHKERRQ(ierr);
1083c2e6f3c0SFlorian Wechsung     ierr = PetscHMapIClear(htWithArtificial);CHKERRQ(ierr);
10844bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr);
10854bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr);
10865f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(pointCounts, v, &Np);CHKERRQ(ierr);
10875f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(pointCounts, v, &ooff);CHKERRQ(ierr);
10884bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
10894bbf5ea8SMatthew G. Knepley 
10904bbf5ea8SMatthew G. Knepley     for (k = 0; k < patch->nsubspaces; ++k) {
10914bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
10924bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
10934bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
10944bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
1095d490bb3dSLawrence Mitchell       PetscInt        goff;
10964bbf5ea8SMatthew G. Knepley 
10974bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
10984bbf5ea8SMatthew G. Knepley         /* Reconstruct mapping of global-to-local on this patch. */
10994bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
11005f824522SMatthew G. Knepley         PetscInt       cell = c;
11014bbf5ea8SMatthew G. Knepley 
11025f824522SMatthew G. Knepley         if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);}
11034bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
11044bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
11055f824522SMatthew G. Knepley             const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset;
1106c2e6f3c0SFlorian Wechsung             const PetscInt localDof  = dofsArray[key];
11071b68eb51SMatthew G. Knepley             if (localDof >= 0) {ierr = PetscHMapISet(ht, globalDof, localDof);CHKERRQ(ierr);}
1108c2e6f3c0SFlorian Wechsung             if(patch->multiplicative) {
1109c2e6f3c0SFlorian Wechsung               const PetscInt localDofWithArtificial = dofsArrayWithArtificial[key];
1110c2e6f3c0SFlorian Wechsung               if (localDofWithArtificial >= 0) {
1111c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapISet(htWithArtificial, globalDof, localDofWithArtificial);CHKERRQ(ierr);
1112c2e6f3c0SFlorian Wechsung               }
1113c2e6f3c0SFlorian Wechsung             }
1114c2e6f3c0SFlorian Wechsung             key++;
11154bbf5ea8SMatthew G. Knepley           }
11164bbf5ea8SMatthew G. Knepley         }
11174bbf5ea8SMatthew G. Knepley       }
1118557beb66SLawrence Mitchell 
11194bbf5ea8SMatthew G. Knepley       /* Shove it in the output data structure. */
11204bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetOffset(gtolCounts, v, &goff);CHKERRQ(ierr);
11211b68eb51SMatthew G. Knepley       PetscHashIterBegin(ht, hi);
11221b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(ht, hi)) {
11234bbf5ea8SMatthew G. Knepley         PetscInt globalDof, localDof;
11244bbf5ea8SMatthew G. Knepley 
11251b68eb51SMatthew G. Knepley         PetscHashIterGetKey(ht, hi, globalDof);
11261b68eb51SMatthew G. Knepley         PetscHashIterGetVal(ht, hi, localDof);
11274bbf5ea8SMatthew G. Knepley         if (globalDof >= 0) globalDofsArray[goff + localDof] = globalDof;
11281b68eb51SMatthew G. Knepley         PetscHashIterNext(ht, hi);
11294bbf5ea8SMatthew G. Knepley       }
11305f824522SMatthew G. Knepley 
1131c2e6f3c0SFlorian Wechsung       if(patch->multiplicative) {
1132c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(gtolCountsWithArtificial, v, &goff);CHKERRQ(ierr);
1133c2e6f3c0SFlorian Wechsung         PetscHashIterBegin(htWithArtificial, hi);
1134c2e6f3c0SFlorian Wechsung         while (!PetscHashIterAtEnd(htWithArtificial, hi)) {
1135c2e6f3c0SFlorian Wechsung           PetscInt globalDof, localDof;
1136c2e6f3c0SFlorian Wechsung           PetscHashIterGetKey(htWithArtificial, hi, globalDof);
1137c2e6f3c0SFlorian Wechsung           PetscHashIterGetVal(htWithArtificial, hi, localDof);
1138c2e6f3c0SFlorian Wechsung           if (globalDof >= 0) globalDofsArrayWithArtificial[goff + localDof] = globalDof;
1139c2e6f3c0SFlorian Wechsung           PetscHashIterNext(htWithArtificial, hi);
1140c2e6f3c0SFlorian Wechsung         }
1141c2e6f3c0SFlorian Wechsung       }
1142c2e6f3c0SFlorian Wechsung 
11435f824522SMatthew G. Knepley       for (p = 0; p < Np; ++p) {
11445f824522SMatthew G. Knepley         const PetscInt point = pointsArray[ooff + p];
11455f824522SMatthew G. Knepley         PetscInt       globalDof, localDof;
11465f824522SMatthew G. Knepley 
11475f824522SMatthew G. Knepley         ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, point, NULL, &globalDof);CHKERRQ(ierr);
11481b68eb51SMatthew G. Knepley         ierr = PetscHMapIGet(ht, globalDof, &localDof);CHKERRQ(ierr);
11495f824522SMatthew G. Knepley         offsArray[(ooff + p)*Nf + k] = localDof;
1150c2e6f3c0SFlorian Wechsung         if(patch->multiplicative) {
1151c2e6f3c0SFlorian Wechsung           ierr = PetscHMapIGet(htWithArtificial, globalDof, &localDof);CHKERRQ(ierr);
1152c2e6f3c0SFlorian Wechsung           offsArrayWithArtificial[(ooff + p)*Nf + k] = localDof;
1153c2e6f3c0SFlorian Wechsung         }
11545f824522SMatthew G. Knepley       }
11554bbf5ea8SMatthew G. Knepley     }
11564bbf5ea8SMatthew G. Knepley 
11570cd083f8SSatish Balay     ierr = PetscHSetIDestroy(&globalBcs);CHKERRQ(ierr);
11581b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&ownedpts);CHKERRQ(ierr);
11591b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&seenpts);CHKERRQ(ierr);
11601b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&owneddofs);CHKERRQ(ierr);
11611b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&seendofs);CHKERRQ(ierr);
11621b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&artificialbcs);CHKERRQ(ierr);
1163557beb66SLawrence Mitchell 
11644bbf5ea8SMatthew G. Knepley       /* At this point, we have a hash table ht built that maps globalDof -> localDof.
11654bbf5ea8SMatthew G. Knepley      We need to create the dof table laid out cellwise first, then by subspace,
11664bbf5ea8SMatthew G. Knepley      as the assembler assembles cell-wise and we need to stuff the different
11674bbf5ea8SMatthew G. Knepley      contributions of the different function spaces to the right places. So we loop
11684bbf5ea8SMatthew G. Knepley      over cells, then over subspaces. */
11694bbf5ea8SMatthew G. Knepley     if (patch->nsubspaces > 1) { /* for nsubspaces = 1, data we need is already in dofsArray */
11704bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
11714bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
11725f824522SMatthew G. Knepley         PetscInt       cell = c;
11734bbf5ea8SMatthew G. Knepley 
11745f824522SMatthew G. Knepley         if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);}
11754bbf5ea8SMatthew G. Knepley         for (k = 0; k < patch->nsubspaces; ++k) {
11764bbf5ea8SMatthew G. Knepley           const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
11774bbf5ea8SMatthew G. Knepley           PetscInt        nodesPerCell   = patch->nodesPerCell[k];
11784bbf5ea8SMatthew G. Knepley           PetscInt        subspaceOffset = patch->subspaceOffsets[k];
11794bbf5ea8SMatthew G. Knepley           PetscInt        bs             = patch->bs[k];
11804bbf5ea8SMatthew G. Knepley 
11814bbf5ea8SMatthew G. Knepley           for (j = 0; j < nodesPerCell; ++j) {
11824bbf5ea8SMatthew G. Knepley             for (l = 0; l < bs; ++l) {
11835f824522SMatthew G. Knepley               const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset;
11844bbf5ea8SMatthew G. Knepley               PetscInt       localDof;
11854bbf5ea8SMatthew G. Knepley 
11861b68eb51SMatthew G. Knepley               ierr = PetscHMapIGet(ht, globalDof, &localDof);CHKERRQ(ierr);
1187557beb66SLawrence Mitchell               /* If it's not in the hash table, i.e. is a BC dof,
11881b68eb51SMatthew G. Knepley                then the PetscHSetIMap above gives -1, which matches
1189557beb66SLawrence Mitchell                exactly the convention for PETSc's matrix assembly to
1190557beb66SLawrence Mitchell                ignore the dof. So we don't need to do anything here */
1191c2e6f3c0SFlorian Wechsung               asmArray[asmKey] = localDof;
1192c2e6f3c0SFlorian Wechsung               if(patch->multiplicative) {
1193c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapIGet(htWithArtificial, globalDof, &localDof);CHKERRQ(ierr);
1194c2e6f3c0SFlorian Wechsung                 asmArrayWithArtificial[asmKey] = localDof;
1195c2e6f3c0SFlorian Wechsung               }
1196c2e6f3c0SFlorian Wechsung               asmKey++;
11974bbf5ea8SMatthew G. Knepley             }
11984bbf5ea8SMatthew G. Knepley           }
11994bbf5ea8SMatthew G. Knepley         }
12004bbf5ea8SMatthew G. Knepley       }
12014bbf5ea8SMatthew G. Knepley     }
12024bbf5ea8SMatthew G. Knepley   }
1203c2e6f3c0SFlorian Wechsung   if (1 == patch->nsubspaces) {
1204c2e6f3c0SFlorian Wechsung     ierr = PetscMemcpy(asmArray, dofsArray, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
1205c2e6f3c0SFlorian Wechsung     if(patch->multiplicative) {
1206c2e6f3c0SFlorian Wechsung       ierr = PetscMemcpy(asmArrayWithArtificial, dofsArrayWithArtificial, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
1207c2e6f3c0SFlorian Wechsung     }
1208c2e6f3c0SFlorian Wechsung   }
12094bbf5ea8SMatthew G. Knepley 
12101b68eb51SMatthew G. Knepley   ierr = PetscHMapIDestroy(&ht);CHKERRQ(ierr);
1211c2e6f3c0SFlorian Wechsung   ierr = PetscHMapIDestroy(&htWithArtificial);CHKERRQ(ierr);
12124bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(cells, &cellsArray);CHKERRQ(ierr);
12135f824522SMatthew G. Knepley   ierr = ISRestoreIndices(points, &pointsArray);CHKERRQ(ierr);
12144bbf5ea8SMatthew G. Knepley   ierr = PetscFree(dofsArray);CHKERRQ(ierr);
1215c2e6f3c0SFlorian Wechsung   if(patch->multiplicative) {
1216c2e6f3c0SFlorian Wechsung     ierr = PetscFree(dofsArrayWithArtificial);CHKERRQ(ierr);
1217c2e6f3c0SFlorian Wechsung   }
12185f824522SMatthew G. Knepley   /* Create placeholder section for map from points to patch dofs */
12195f824522SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->patchSection);CHKERRQ(ierr);
12205f824522SMatthew G. Knepley   ierr = PetscSectionSetNumFields(patch->patchSection, patch->nsubspaces);CHKERRQ(ierr);
12211e5fa6bbSLawrence Mitchell   if (patch->combined) {
12221e5fa6bbSLawrence Mitchell     PetscInt numFields;
12231e5fa6bbSLawrence Mitchell     ierr = PetscSectionGetNumFields(patch->dofSection[0], &numFields);CHKERRQ(ierr);
12241e5fa6bbSLawrence 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);
12255f824522SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->dofSection[0], &pStart, &pEnd);CHKERRQ(ierr);
12265f824522SMatthew G. Knepley     ierr = PetscSectionSetChart(patch->patchSection, pStart, pEnd);CHKERRQ(ierr);
12275f824522SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
12285f824522SMatthew G. Knepley       PetscInt dof, fdof, f;
12295f824522SMatthew G. Knepley 
12305f824522SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->dofSection[0], p, &dof);CHKERRQ(ierr);
12315f824522SMatthew G. Knepley       ierr = PetscSectionSetDof(patch->patchSection, p, dof);CHKERRQ(ierr);
12325f824522SMatthew G. Knepley       for (f = 0; f < patch->nsubspaces; ++f) {
12331e5fa6bbSLawrence Mitchell         ierr = PetscSectionGetFieldDof(patch->dofSection[0], p, f, &fdof);CHKERRQ(ierr);
12345f824522SMatthew G. Knepley         ierr = PetscSectionSetFieldDof(patch->patchSection, p, f, fdof);CHKERRQ(ierr);
12355f824522SMatthew G. Knepley       }
12361e5fa6bbSLawrence Mitchell     }
12371e5fa6bbSLawrence Mitchell   } else {
12381e5fa6bbSLawrence Mitchell     PetscInt pStartf, pEndf, f;
12391e5fa6bbSLawrence Mitchell     pStart = PETSC_MAX_INT;
12401e5fa6bbSLawrence Mitchell     pEnd = PETSC_MIN_INT;
12411e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
12421e5fa6bbSLawrence Mitchell       ierr = PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf);CHKERRQ(ierr);
12431e5fa6bbSLawrence Mitchell       pStart = PetscMin(pStart, pStartf);
12441e5fa6bbSLawrence Mitchell       pEnd = PetscMax(pEnd, pEndf);
12451e5fa6bbSLawrence Mitchell     }
12461e5fa6bbSLawrence Mitchell     ierr = PetscSectionSetChart(patch->patchSection, pStart, pEnd);CHKERRQ(ierr);
12471e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
12481e5fa6bbSLawrence Mitchell       ierr = PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf);CHKERRQ(ierr);
12491e5fa6bbSLawrence Mitchell       for (p = pStartf; p < pEndf; ++p) {
12501e5fa6bbSLawrence Mitchell         PetscInt fdof;
12511e5fa6bbSLawrence Mitchell         ierr = PetscSectionGetDof(patch->dofSection[f], p, &fdof);CHKERRQ(ierr);
12521e5fa6bbSLawrence Mitchell         ierr = PetscSectionAddDof(patch->patchSection, p, fdof);CHKERRQ(ierr);
12531e5fa6bbSLawrence Mitchell         ierr = PetscSectionSetFieldDof(patch->patchSection, p, f, fdof);CHKERRQ(ierr);
1254bdd9e0cdSPatrick Farrell       }
1255bdd9e0cdSPatrick Farrell     }
12565f824522SMatthew G. Knepley   }
12575f824522SMatthew G. Knepley   ierr = PetscSectionSetUp(patch->patchSection);CHKERRQ(ierr);
12585f824522SMatthew G. Knepley   ierr = PetscSectionSetUseFieldOffsets(patch->patchSection, PETSC_TRUE);CHKERRQ(ierr);
12594bbf5ea8SMatthew G. Knepley   /* Replace cell indices with firedrake-numbered ones. */
12604bbf5ea8SMatthew G. Knepley   ierr = ISGeneralSetIndices(cells, numCells, (const PetscInt *) newCellsArray, PETSC_OWN_POINTER);CHKERRQ(ierr);
12614bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofs, globalDofsArray, PETSC_OWN_POINTER, &patch->gtol);CHKERRQ(ierr);
12625f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->gtol, "Global Indices");CHKERRQ(ierr);
12635f824522SMatthew G. Knepley   ierr = PetscSectionViewFromOptions(patch->gtolCounts, (PetscObject) pc, "-pc_patch_g2l_view");CHKERRQ(ierr);
12645f824522SMatthew G. Knepley   ierr = ISViewFromOptions(patch->gtol, (PetscObject) pc, "-pc_patch_g2l_view");CHKERRQ(ierr);
12654bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArray, PETSC_OWN_POINTER, &patch->dofs);CHKERRQ(ierr);
12665f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArray, PETSC_OWN_POINTER, &patch->offs);CHKERRQ(ierr);
1267c2e6f3c0SFlorian Wechsung   if(patch->multiplicative) {
1268c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithArtificial, globalDofsArrayWithArtificial, PETSC_OWN_POINTER, &patch->gtolWithArtificial);CHKERRQ(ierr);
1269c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithArtificial, PETSC_OWN_POINTER, &patch->dofsWithArtificial);CHKERRQ(ierr);
1270c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithArtificial, PETSC_OWN_POINTER, &patch->offsWithArtificial);CHKERRQ(ierr);
1271c2e6f3c0SFlorian Wechsung   }
12724bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
12734bbf5ea8SMatthew G. Knepley }
12744bbf5ea8SMatthew G. Knepley 
12754bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchZeroFillMatrix_Private(Mat mat, const PetscInt ncell, const PetscInt ndof, const PetscInt *dof)
12764bbf5ea8SMatthew G. Knepley {
127723b8bdd9SMatthew G. Knepley   PetscScalar    *values = NULL;
12784bbf5ea8SMatthew G. Knepley   PetscInt        rows, c, i;
12794bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
12804bbf5ea8SMatthew G. Knepley 
12814bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
12824bbf5ea8SMatthew G. Knepley   ierr = PetscCalloc1(ndof*ndof, &values);CHKERRQ(ierr);
12834bbf5ea8SMatthew G. Knepley   for (c = 0; c < ncell; ++c) {
12844bbf5ea8SMatthew G. Knepley     const PetscInt *idx = &dof[ndof*c];
12854bbf5ea8SMatthew G. Knepley     ierr = MatSetValues(mat, ndof, idx, ndof, idx, values, INSERT_VALUES);CHKERRQ(ierr);
12864bbf5ea8SMatthew G. Knepley   }
12874bbf5ea8SMatthew G. Knepley   ierr = MatGetLocalSize(mat, &rows, NULL);CHKERRQ(ierr);
12884bbf5ea8SMatthew G. Knepley   for (i = 0; i < rows; ++i) {
12894bbf5ea8SMatthew G. Knepley     ierr = MatSetValues(mat, 1, &i, 1, &i, values, INSERT_VALUES);CHKERRQ(ierr);
12904bbf5ea8SMatthew G. Knepley   }
12914bbf5ea8SMatthew G. Knepley   ierr = MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
12924bbf5ea8SMatthew G. Knepley   ierr = MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
12934bbf5ea8SMatthew G. Knepley   ierr = PetscFree(values);CHKERRQ(ierr);
12944bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
12954bbf5ea8SMatthew G. Knepley }
12964bbf5ea8SMatthew G. Knepley 
1297c2e6f3c0SFlorian Wechsung static PetscErrorCode PCPatchCreateMatrix_Private(PC pc, PetscInt point, Mat *mat, PetscBool withArtificial)
12984bbf5ea8SMatthew G. Knepley {
12994bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
13004bbf5ea8SMatthew G. Knepley   Vec            x, y;
13014bbf5ea8SMatthew G. Knepley   PetscBool      flg;
13024bbf5ea8SMatthew G. Knepley   PetscInt       csize, rsize;
13034bbf5ea8SMatthew G. Knepley   const char    *prefix = NULL;
13044bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
13054bbf5ea8SMatthew G. Knepley 
13064bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1307c2e6f3c0SFlorian Wechsung   if(withArtificial) {
1308eba130bbSFlorian Wechsung    // would be nice if we could create a rectangular matrix of size numDofsWithArtificial x numDofs here
1309c2e6f3c0SFlorian Wechsung     x = patch->patchXWithArtificial[point];
1310eba130bbSFlorian Wechsung     y = patch->patchXWithArtificial[point];
1311c2e6f3c0SFlorian Wechsung   }
1312c2e6f3c0SFlorian Wechsung   else {
13134bbf5ea8SMatthew G. Knepley     x = patch->patchX[point];
13144bbf5ea8SMatthew G. Knepley     y = patch->patchY[point];
1315c2e6f3c0SFlorian Wechsung   }
1316c2e6f3c0SFlorian Wechsung 
13174bbf5ea8SMatthew G. Knepley   ierr = VecGetSize(x, &csize);CHKERRQ(ierr);
13184bbf5ea8SMatthew G. Knepley   ierr = VecGetSize(y, &rsize);CHKERRQ(ierr);
13194bbf5ea8SMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, mat);CHKERRQ(ierr);
13204bbf5ea8SMatthew G. Knepley   ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr);
13214bbf5ea8SMatthew G. Knepley   ierr = MatSetOptionsPrefix(*mat, prefix);CHKERRQ(ierr);
13225f824522SMatthew G. Knepley   ierr = MatAppendOptionsPrefix(*mat, "pc_patch_sub_");CHKERRQ(ierr);
13234bbf5ea8SMatthew G. Knepley   if (patch->sub_mat_type)       {ierr = MatSetType(*mat, patch->sub_mat_type);CHKERRQ(ierr);}
13247974b488SMatthew G. Knepley   else if (!patch->sub_mat_type) {ierr = MatSetType(*mat, MATDENSE);CHKERRQ(ierr);}
13254bbf5ea8SMatthew G. Knepley   ierr = MatSetSizes(*mat, rsize, csize, rsize, csize);CHKERRQ(ierr);
13264bbf5ea8SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) *mat, MATDENSE, &flg);CHKERRQ(ierr);
13274bbf5ea8SMatthew G. Knepley   if (!flg) {ierr = PetscObjectTypeCompare((PetscObject)*mat, MATSEQDENSE, &flg);CHKERRQ(ierr);}
13284bbf5ea8SMatthew G. Knepley   /* Sparse patch matrices */
13294bbf5ea8SMatthew G. Knepley   if (!flg) {
13304bbf5ea8SMatthew G. Knepley     PetscBT         bt;
13314bbf5ea8SMatthew G. Knepley     PetscInt       *dnnz      = NULL;
13324bbf5ea8SMatthew G. Knepley     const PetscInt *dofsArray = NULL;
13334bbf5ea8SMatthew G. Knepley     PetscInt        pStart, pEnd, ncell, offset, c, i, j;
13344bbf5ea8SMatthew G. Knepley 
1335c2e6f3c0SFlorian Wechsung     if(withArtificial) {
1336c2e6f3c0SFlorian Wechsung         ierr = ISGetIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1337c2e6f3c0SFlorian Wechsung     }
1338c2e6f3c0SFlorian Wechsung     else {
13394bbf5ea8SMatthew G. Knepley         ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1340c2e6f3c0SFlorian Wechsung     }
13414bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
13424bbf5ea8SMatthew G. Knepley     point += pStart;
13434bbf5ea8SMatthew 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);
13444bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
13454bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
13464bbf5ea8SMatthew G. Knepley     ierr = PetscCalloc1(rsize, &dnnz);CHKERRQ(ierr);
13474bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventBegin(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr);
13484bbf5ea8SMatthew G. Knepley     /* XXX: This uses N^2 bits to store the sparsity pattern on a
13494bbf5ea8SMatthew G. Knepley      * patch.  This is probably OK if the patches are not too big,
13504bbf5ea8SMatthew G. Knepley      * but could use quite a bit of memory for planes in 3D.
13514bbf5ea8SMatthew G. Knepley      * Should we switch based on the value of rsize to a
13524bbf5ea8SMatthew G. Knepley      * hash-table (slower, but more memory efficient) approach? */
13534bbf5ea8SMatthew G. Knepley     ierr = PetscBTCreate(rsize*rsize, &bt);CHKERRQ(ierr);
13544bbf5ea8SMatthew G. Knepley     for (c = 0; c < ncell; ++c) {
13554bbf5ea8SMatthew G. Knepley       const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell;
13564bbf5ea8SMatthew G. Knepley       for (i = 0; i < patch->totalDofsPerCell; ++i) {
13574bbf5ea8SMatthew G. Knepley         const PetscInt row = idx[i];
1358557beb66SLawrence Mitchell         if (row < 0) continue;
13594bbf5ea8SMatthew G. Knepley         for (j = 0; j < patch->totalDofsPerCell; ++j) {
13604bbf5ea8SMatthew G. Knepley           const PetscInt col = idx[j];
13614bbf5ea8SMatthew G. Knepley           const PetscInt key = row*rsize + col;
1362557beb66SLawrence Mitchell           if (col < 0) continue;
13634bbf5ea8SMatthew G. Knepley           if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
13644bbf5ea8SMatthew G. Knepley         }
13654bbf5ea8SMatthew G. Knepley       }
13664bbf5ea8SMatthew G. Knepley     }
13674bbf5ea8SMatthew G. Knepley     ierr = PetscBTDestroy(&bt);CHKERRQ(ierr);
13684bbf5ea8SMatthew G. Knepley     ierr = MatXAIJSetPreallocation(*mat, 1, dnnz, NULL, NULL, NULL);CHKERRQ(ierr);
13694bbf5ea8SMatthew G. Knepley     ierr = PetscFree(dnnz);CHKERRQ(ierr);
13704bbf5ea8SMatthew G. Knepley     ierr = PCPatchZeroFillMatrix_Private(*mat, ncell, patch->totalDofsPerCell, &dofsArray[offset*patch->totalDofsPerCell]);CHKERRQ(ierr);
13714bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr);
1372fe117d09SFlorian Wechsung     if(withArtificial) {
1373fe117d09SFlorian Wechsung       ierr = ISRestoreIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1374fe117d09SFlorian Wechsung     } else {
13754bbf5ea8SMatthew G. Knepley       ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
13764bbf5ea8SMatthew G. Knepley     }
1377fe117d09SFlorian Wechsung   }
13784bbf5ea8SMatthew G. Knepley   ierr = MatSetUp(*mat);CHKERRQ(ierr);
13794bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
13804bbf5ea8SMatthew G. Knepley }
13814bbf5ea8SMatthew G. Knepley 
13826f158342SMatthew G. Knepley static PetscErrorCode PCPatchComputeOperator_DMPlex_Private(PC pc, PetscInt patchNum, Mat J, IS cellIS, PetscInt n, const PetscInt *l2p, void *ctx)
13835f824522SMatthew G. Knepley {
13845f824522SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
13855f824522SMatthew G. Knepley   DM              dm;
13865f824522SMatthew G. Knepley   PetscSection    s;
13875f824522SMatthew G. Knepley   const PetscInt *parray, *oarray;
13885f824522SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
13895f824522SMatthew G. Knepley   PetscErrorCode  ierr;
13905f824522SMatthew G. Knepley 
13915f824522SMatthew G. Knepley   PetscFunctionBegin;
13925f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
13935f824522SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
13945f824522SMatthew G. Knepley   /* Set offset into patch */
13955f824522SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->pointCounts, patchNum, &Np);CHKERRQ(ierr);
13965f824522SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->pointCounts, patchNum, &poff);CHKERRQ(ierr);
13975f824522SMatthew G. Knepley   ierr = ISGetIndices(patch->points, &parray);CHKERRQ(ierr);
13985f824522SMatthew G. Knepley   ierr = ISGetIndices(patch->offs,   &oarray);CHKERRQ(ierr);
13995f824522SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
14005f824522SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
14015f824522SMatthew G. Knepley       const PetscInt point = parray[poff+p];
14025f824522SMatthew G. Knepley       PetscInt       dof;
14035f824522SMatthew G. Knepley 
14045f824522SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(patch->patchSection, point, f, &dof);CHKERRQ(ierr);
14055f824522SMatthew G. Knepley       ierr = PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);
14065f824522SMatthew G. Knepley       if (patch->nsubspaces == 1) {ierr = PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);}
14075f824522SMatthew G. Knepley       else                        {ierr = PetscSectionSetOffset(patch->patchSection, point, -1);CHKERRQ(ierr);}
14085f824522SMatthew G. Knepley     }
14095f824522SMatthew G. Knepley   }
14105f824522SMatthew G. Knepley   ierr = ISRestoreIndices(patch->points, &parray);CHKERRQ(ierr);
14115f824522SMatthew G. Knepley   ierr = ISRestoreIndices(patch->offs,   &oarray);CHKERRQ(ierr);
14125f824522SMatthew G. Knepley   if (patch->viewSection) {ierr = ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection);CHKERRQ(ierr);}
14135f824522SMatthew G. Knepley   /* TODO Shut off MatViewFromOptions() in MatAssemblyEnd() here */
14146f158342SMatthew G. Knepley   ierr = DMPlexComputeJacobian_Patch_Internal(pc->dm, patch->patchSection, patch->patchSection, cellIS, 0.0, 0.0, NULL, NULL, J, J, ctx);CHKERRQ(ierr);
14155f824522SMatthew G. Knepley   PetscFunctionReturn(0);
14165f824522SMatthew G. Knepley }
14175f824522SMatthew G. Knepley 
1418c2e6f3c0SFlorian Wechsung static PetscErrorCode PCPatchComputeOperator_Private(PC pc, Mat mat, PetscInt point, PetscBool withArtificial)
14194bbf5ea8SMatthew G. Knepley {
14204bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
14214bbf5ea8SMatthew G. Knepley   const PetscInt *dofsArray;
14224bbf5ea8SMatthew G. Knepley   const PetscInt *cellsArray;
14234bbf5ea8SMatthew G. Knepley   PetscInt        ncell, offset, pStart, pEnd;
14244bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
14254bbf5ea8SMatthew G. Knepley 
14264bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
14274bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
14284bbf5ea8SMatthew G. Knepley   if (!patch->usercomputeop) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback\n");
1429c2e6f3c0SFlorian Wechsung   if(withArtificial) {
1430c2e6f3c0SFlorian Wechsung     ierr = ISGetIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1431c2e6f3c0SFlorian Wechsung   } else {
14324bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1433c2e6f3c0SFlorian Wechsung   }
14344bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
14354bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
14364bbf5ea8SMatthew G. Knepley 
14374bbf5ea8SMatthew G. Knepley   point += pStart;
14384bbf5ea8SMatthew 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);
14394bbf5ea8SMatthew G. Knepley 
14404bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
14414bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
14424bbf5ea8SMatthew G. Knepley   if (ncell <= 0) {
14434bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
14444bbf5ea8SMatthew G. Knepley     PetscFunctionReturn(0);
14454bbf5ea8SMatthew G. Knepley   }
14464bbf5ea8SMatthew G. Knepley   PetscStackPush("PCPatch user callback");
14472aa6f319SMatthew G. Knepley   /* Cannot reuse the same IS because the geometry info is being cached in it */
14482aa6f319SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS);CHKERRQ(ierr);
14496f158342SMatthew G. Knepley   ierr = patch->usercomputeop(pc, point, mat, patch->cellIS, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell, patch->usercomputectx);CHKERRQ(ierr);
14504bbf5ea8SMatthew G. Knepley   PetscStackPop;
14512aa6f319SMatthew G. Knepley   ierr = ISDestroy(&patch->cellIS);CHKERRQ(ierr);
1452c2e6f3c0SFlorian Wechsung   if(withArtificial)
1453c2e6f3c0SFlorian Wechsung   {
1454c2e6f3c0SFlorian Wechsung     ierr = ISRestoreIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1455c2e6f3c0SFlorian Wechsung   } else {
14564bbf5ea8SMatthew G. Knepley     ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1457c2e6f3c0SFlorian Wechsung   }
14584bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
14592aa6f319SMatthew G. Knepley   if (patch->viewMatrix) {
14602aa6f319SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
14612aa6f319SMatthew G. Knepley 
14622aa6f319SMatthew G. Knepley     ierr = PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch matrix for Point %D", point);CHKERRQ(ierr);
14632aa6f319SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) mat, name);CHKERRQ(ierr);
14642aa6f319SMatthew G. Knepley     ierr = ObjectView((PetscObject) mat, patch->viewerMatrix, patch->formatMatrix);CHKERRQ(ierr);
14652aa6f319SMatthew G. Knepley   }
14664bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
14674bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
14684bbf5ea8SMatthew G. Knepley }
14694bbf5ea8SMatthew G. Knepley 
1470c2e6f3c0SFlorian Wechsung static PetscErrorCode PCPatch_ScatterLocal_Private(PC pc, PetscInt p, Vec x, Vec y, InsertMode mode, ScatterMode scat, PetscBool withArtificial)
14714bbf5ea8SMatthew G. Knepley {
14724bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch     = (PC_PATCH *) pc->data;
14734bbf5ea8SMatthew G. Knepley   const PetscScalar *xArray    = NULL;
14744bbf5ea8SMatthew G. Knepley   PetscScalar       *yArray    = NULL;
14754bbf5ea8SMatthew G. Knepley   const PetscInt    *gtolArray = NULL;
14764bbf5ea8SMatthew G. Knepley   PetscInt           dof, offset, lidx;
14774bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
14784bbf5ea8SMatthew G. Knepley 
14794bbf5ea8SMatthew G. Knepley   PetscFunctionBeginHot;
14804bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Scatter, pc, 0, 0, 0);CHKERRQ(ierr);
14814bbf5ea8SMatthew G. Knepley   ierr = VecGetArrayRead(x, &xArray);CHKERRQ(ierr);
14824bbf5ea8SMatthew G. Knepley   ierr = VecGetArray(y, &yArray);CHKERRQ(ierr);
1483c2e6f3c0SFlorian Wechsung   if(withArtificial) {
1484c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof);CHKERRQ(ierr);
1485c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offset);CHKERRQ(ierr);
1486c2e6f3c0SFlorian Wechsung     ierr = ISGetIndices(patch->gtolWithArtificial, &gtolArray);CHKERRQ(ierr);
1487c2e6f3c0SFlorian Wechsung   } else {
14884bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr);
14894bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
14904bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
1491c2e6f3c0SFlorian Wechsung   }
14924bbf5ea8SMatthew 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");
14934bbf5ea8SMatthew 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");
14944bbf5ea8SMatthew G. Knepley   for (lidx = 0; lidx < dof; ++lidx) {
14954bbf5ea8SMatthew G. Knepley     const PetscInt gidx = gtolArray[offset+lidx];
14964bbf5ea8SMatthew G. Knepley 
14974bbf5ea8SMatthew G. Knepley     if (mode == INSERT_VALUES) yArray[lidx]  = xArray[gidx]; /* Forward */
14984bbf5ea8SMatthew G. Knepley     else                       yArray[gidx] += xArray[lidx]; /* Reverse */
14994bbf5ea8SMatthew G. Knepley   }
1500c2e6f3c0SFlorian Wechsung   if(withArtificial) {
1501c2e6f3c0SFlorian Wechsung     ierr = ISRestoreIndices(patch->gtolWithArtificial, &gtolArray);CHKERRQ(ierr);
1502c2e6f3c0SFlorian Wechsung   } else {
15034bbf5ea8SMatthew G. Knepley     ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
1504c2e6f3c0SFlorian Wechsung   }
15054bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArrayRead(x, &xArray);CHKERRQ(ierr);
15064bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArray(y, &yArray);CHKERRQ(ierr);
15074bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Scatter, pc, 0, 0, 0);CHKERRQ(ierr);
15084bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
15094bbf5ea8SMatthew G. Knepley }
15104bbf5ea8SMatthew G. Knepley 
15114bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUp_PATCH(PC pc)
15124bbf5ea8SMatthew G. Knepley {
15134bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch   = (PC_PATCH *) pc->data;
1514557beb66SLawrence Mitchell   PetscInt        i;
15154bbf5ea8SMatthew G. Knepley   const char     *prefix;
15164bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
15174bbf5ea8SMatthew G. Knepley 
15184bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
15194bbf5ea8SMatthew G. Knepley   if (!pc->setupcalled) {
15204bbf5ea8SMatthew G. Knepley     PetscInt pStart, pEnd, p;
15214bbf5ea8SMatthew G. Knepley     PetscInt localSize;
15224bbf5ea8SMatthew G. Knepley 
15234bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventBegin(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr);
15244bbf5ea8SMatthew G. Knepley 
15255f824522SMatthew G. Knepley     if (!patch->nsubspaces) {
15265f824522SMatthew G. Knepley       DM           dm;
15275f824522SMatthew G. Knepley       PetscDS      prob;
15285f824522SMatthew G. Knepley       PetscSection s;
1529e72c1634SMatthew G. Knepley       PetscInt     cStart, cEnd, c, Nf, f, numGlobalBcs = 0, *globalBcs, *Nb, totNb = 0, **cellDofs;
15305f824522SMatthew G. Knepley 
15315f824522SMatthew G. Knepley       ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
15325f824522SMatthew G. Knepley       if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONG, "Must set DM for PCPATCH or call PCPatchSetDiscretisationInfo()");
15335f824522SMatthew G. Knepley       ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
15345f824522SMatthew G. Knepley       ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
15355f824522SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
15365f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
15375f824522SMatthew G. Knepley         PetscInt cdof;
15385f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
15395f824522SMatthew G. Knepley         numGlobalBcs += cdof;
15405f824522SMatthew G. Knepley       }
15415f824522SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
15425f824522SMatthew G. Knepley       ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
15435f824522SMatthew G. Knepley       ierr = PetscMalloc3(Nf, &Nb, Nf, &cellDofs, numGlobalBcs, &globalBcs);CHKERRQ(ierr);
15445f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
15455f824522SMatthew G. Knepley         PetscFE        fe;
15465f824522SMatthew G. Knepley         PetscDualSpace sp;
15475f824522SMatthew G. Knepley         PetscInt       cdoff = 0;
15485f824522SMatthew G. Knepley 
15495f824522SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
15505f824522SMatthew G. Knepley         /* ierr = PetscFEGetNumComponents(fe, &Nc[f]);CHKERRQ(ierr); */
15515f824522SMatthew G. Knepley         ierr = PetscFEGetDualSpace(fe, &sp);CHKERRQ(ierr);
15525f824522SMatthew G. Knepley         ierr = PetscDualSpaceGetDimension(sp, &Nb[f]);CHKERRQ(ierr);
15535f824522SMatthew G. Knepley         totNb += Nb[f];
15545f824522SMatthew G. Knepley 
15555f824522SMatthew G. Knepley         ierr = PetscMalloc1((cEnd-cStart)*Nb[f], &cellDofs[f]);CHKERRQ(ierr);
15565f824522SMatthew G. Knepley         for (c = cStart; c < cEnd; ++c) {
15575f824522SMatthew G. Knepley           PetscInt *closure = NULL;
15585f824522SMatthew G. Knepley           PetscInt  clSize  = 0, cl;
15595f824522SMatthew G. Knepley 
15605f824522SMatthew G. Knepley           ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
15615f824522SMatthew G. Knepley           for (cl = 0; cl < clSize*2; cl += 2) {
15625f824522SMatthew G. Knepley             const PetscInt p = closure[cl];
15635f824522SMatthew G. Knepley             PetscInt       fdof, d, foff;
15645f824522SMatthew G. Knepley 
15655f824522SMatthew G. Knepley             ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr);
15665f824522SMatthew G. Knepley             ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr);
15675f824522SMatthew G. Knepley             for (d = 0; d < fdof; ++d, ++cdoff) cellDofs[f][cdoff] = foff + d;
15685f824522SMatthew G. Knepley           }
15695f824522SMatthew G. Knepley           ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
15705f824522SMatthew G. Knepley         }
15715f824522SMatthew 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]);
15725f824522SMatthew G. Knepley       }
15735f824522SMatthew G. Knepley       numGlobalBcs = 0;
15745f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
15755f824522SMatthew G. Knepley         const PetscInt *ind;
15765f824522SMatthew G. Knepley         PetscInt        off, cdof, d;
15775f824522SMatthew G. Knepley 
15785f824522SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
15795f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
15805f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &ind);CHKERRQ(ierr);
15815f824522SMatthew G. Knepley         for (d = 0; d < cdof; ++d) globalBcs[numGlobalBcs++] = off + ind[d];
15825f824522SMatthew G. Knepley       }
15835f824522SMatthew G. Knepley 
15845f824522SMatthew G. Knepley       ierr = PCPatchSetDiscretisationInfoCombined(pc, dm, Nb, (const PetscInt **) cellDofs, numGlobalBcs, globalBcs, numGlobalBcs, globalBcs);CHKERRQ(ierr);
15855f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
15865f824522SMatthew G. Knepley         ierr = PetscFree(cellDofs[f]);CHKERRQ(ierr);
15875f824522SMatthew G. Knepley       }
15885f824522SMatthew G. Knepley       ierr = PetscFree3(Nb, cellDofs, globalBcs);CHKERRQ(ierr);
15895f824522SMatthew G. Knepley       ierr = PCPatchSetComputeOperator(pc, PCPatchComputeOperator_DMPlex_Private, NULL);CHKERRQ(ierr);
15905f824522SMatthew G. Knepley     }
15915f824522SMatthew G. Knepley 
15924bbf5ea8SMatthew G. Knepley     localSize = patch->subspaceOffsets[patch->nsubspaces];
15934bbf5ea8SMatthew G. Knepley     ierr = VecCreateSeq(PETSC_COMM_SELF, localSize, &patch->localX);CHKERRQ(ierr);
15944bbf5ea8SMatthew G. Knepley     ierr = VecSetUp(patch->localX);CHKERRQ(ierr);
15954bbf5ea8SMatthew G. Knepley     ierr = VecDuplicate(patch->localX, &patch->localY);CHKERRQ(ierr);
15964bbf5ea8SMatthew G. Knepley     ierr = PCPatchCreateCellPatches(pc);CHKERRQ(ierr);
15974bbf5ea8SMatthew G. Knepley     ierr = PCPatchCreateCellPatchDiscretisationInfo(pc);CHKERRQ(ierr);
15984bbf5ea8SMatthew G. Knepley 
15994bbf5ea8SMatthew G. Knepley     /* OK, now build the work vectors */
16004bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, &pEnd);CHKERRQ(ierr);
16014bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(patch->npatch, &patch->patchX);CHKERRQ(ierr);
16024bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(patch->npatch, &patch->patchY);CHKERRQ(ierr);
1603c2e6f3c0SFlorian Wechsung 
1604c2e6f3c0SFlorian Wechsung     if(patch->multiplicative) {
1605c2e6f3c0SFlorian Wechsung       ierr = PetscMalloc1(patch->npatch, &patch->patchXWithArtificial);CHKERRQ(ierr);
1606c2e6f3c0SFlorian Wechsung       ierr = PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithArtificial);CHKERRQ(ierr);
1607c2e6f3c0SFlorian Wechsung     }
16084bbf5ea8SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
16094bbf5ea8SMatthew G. Knepley       PetscInt dof;
16104bbf5ea8SMatthew G. Knepley 
16114bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr);
16124bbf5ea8SMatthew G. Knepley       ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchX[p-pStart]);CHKERRQ(ierr);
16134bbf5ea8SMatthew G. Knepley       ierr = VecSetUp(patch->patchX[p-pStart]);CHKERRQ(ierr);
16144bbf5ea8SMatthew G. Knepley       ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchY[p-pStart]);CHKERRQ(ierr);
16154bbf5ea8SMatthew G. Knepley       ierr = VecSetUp(patch->patchY[p-pStart]);CHKERRQ(ierr);
1616c2e6f3c0SFlorian Wechsung       if(patch->multiplicative) {
1617c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof);CHKERRQ(ierr);
1618c2e6f3c0SFlorian Wechsung         ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchXWithArtificial[p-pStart]);CHKERRQ(ierr);
1619c2e6f3c0SFlorian Wechsung         ierr = VecSetUp(patch->patchXWithArtificial[p-pStart]);CHKERRQ(ierr);
1620c2e6f3c0SFlorian Wechsung 
1621c2e6f3c0SFlorian Wechsung         // Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the
1622c2e6f3c0SFlorian Wechsung         // the index in the patch with all dofs
1623c2e6f3c0SFlorian Wechsung         const PetscInt    *gtolArray, *gtolArrayWithArtificial = NULL;
1624c2e6f3c0SFlorian Wechsung         PetscInt           numPatchDofs, offset;
1625c2e6f3c0SFlorian Wechsung         PetscInt           numPatchDofsWithArtificial, offsetWithArtificial;
1626c2e6f3c0SFlorian Wechsung         ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
1627*63deea8eSPatrick Farrell 
1628c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs);CHKERRQ(ierr);
1629*63deea8eSPatrick Farrell         if (numPatchDofs == 0) continue;
1630*63deea8eSPatrick Farrell 
1631c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
1632c2e6f3c0SFlorian Wechsung         ierr = ISGetIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial);CHKERRQ(ierr);
1633c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &numPatchDofsWithArtificial);CHKERRQ(ierr);
1634c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offsetWithArtificial);CHKERRQ(ierr);
1635c2e6f3c0SFlorian Wechsung 
1636eba130bbSFlorian Wechsung         /*printf("\n--------------------------------------------------\n");*/
1637eba130bbSFlorian Wechsung         /*printf("\n------------ without artificial ------------------\n");*/
1638eba130bbSFlorian Wechsung         /*for (PetscInt i=0; i<numPatchDofs; i++) {*/
1639eba130bbSFlorian Wechsung         /*  printf("%i ", gtolArray[i + offset]);*/
1640eba130bbSFlorian Wechsung         /*}*/
1641eba130bbSFlorian Wechsung         /*printf("\n-------------- with artificial -------------------\n");*/
1642eba130bbSFlorian Wechsung         /*for (PetscInt i=0; i<numPatchDofsWithArtificial; i++) {*/
1643eba130bbSFlorian Wechsung         /*  printf("%i ", gtolArrayWithArtificial[i + offsetWithArtificial]);*/
1644eba130bbSFlorian Wechsung         /*}*/
1645eba130bbSFlorian Wechsung         /*printf("\n--------------------------------------------------\n");*/
1646c2e6f3c0SFlorian Wechsung 
1647c2e6f3c0SFlorian Wechsung         PetscInt dofWithoutArtificialCounter = 0;
1648c2e6f3c0SFlorian Wechsung         PetscInt *patchWithoutArtificialToWithArtificialArray;
1649c2e6f3c0SFlorian Wechsung         ierr = PetscMalloc1(numPatchDofs, &patchWithoutArtificialToWithArtificialArray);CHKERRQ(ierr);
1650c2e6f3c0SFlorian Wechsung 
1651c2e6f3c0SFlorian Wechsung         ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutArtificialToWithArtificialArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithArtificial[p-pStart]);
1652c2e6f3c0SFlorian Wechsung         for(PetscInt i=0; i<numPatchDofsWithArtificial; i++)
1653c2e6f3c0SFlorian Wechsung         {
1654c2e6f3c0SFlorian Wechsung           if(gtolArrayWithArtificial[i+offsetWithArtificial] == gtolArray[offset+dofWithoutArtificialCounter])
1655c2e6f3c0SFlorian Wechsung           {
1656c2e6f3c0SFlorian Wechsung             patchWithoutArtificialToWithArtificialArray[dofWithoutArtificialCounter] = i;
1657c2e6f3c0SFlorian Wechsung             dofWithoutArtificialCounter++;
1658c2e6f3c0SFlorian Wechsung             if(dofWithoutArtificialCounter == numPatchDofs)
1659c2e6f3c0SFlorian Wechsung                 break;
1660c2e6f3c0SFlorian Wechsung           }
1661c2e6f3c0SFlorian Wechsung         }
1662c2e6f3c0SFlorian Wechsung         ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
1663c2e6f3c0SFlorian Wechsung         ierr = ISRestoreIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial);CHKERRQ(ierr);
1664c2e6f3c0SFlorian Wechsung       }
16654bbf5ea8SMatthew G. Knepley     }
16664bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(patch->npatch, &patch->ksp);CHKERRQ(ierr);
16674bbf5ea8SMatthew G. Knepley     ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr);
16684bbf5ea8SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {
1669484055adSMatthew G. Knepley       PC subpc;
1670484055adSMatthew G. Knepley 
16714bbf5ea8SMatthew G. Knepley       ierr = KSPCreate(PETSC_COMM_SELF, &patch->ksp[i]);CHKERRQ(ierr);
16724bbf5ea8SMatthew G. Knepley       ierr = KSPSetOptionsPrefix(patch->ksp[i], prefix);CHKERRQ(ierr);
16734bbf5ea8SMatthew G. Knepley       ierr = KSPAppendOptionsPrefix(patch->ksp[i], "sub_");CHKERRQ(ierr);
1674484055adSMatthew G. Knepley       ierr = PetscObjectIncrementTabLevel((PetscObject) patch->ksp[i], (PetscObject) pc, 1);CHKERRQ(ierr);
1675484055adSMatthew G. Knepley       ierr = KSPGetPC(patch->ksp[i], &subpc);CHKERRQ(ierr);
1676484055adSMatthew G. Knepley       ierr = PetscObjectIncrementTabLevel((PetscObject) subpc, (PetscObject) pc, 1);CHKERRQ(ierr);
1677484055adSMatthew G. Knepley       ierr = PetscLogObjectParent((PetscObject) pc, (PetscObject) patch->ksp[i]);CHKERRQ(ierr);
16784bbf5ea8SMatthew G. Knepley     }
16794bbf5ea8SMatthew G. Knepley     if (patch->save_operators) {
16804bbf5ea8SMatthew G. Knepley       ierr = PetscMalloc1(patch->npatch, &patch->mat);CHKERRQ(ierr);
16814bbf5ea8SMatthew G. Knepley       for (i = 0; i < patch->npatch; ++i) {
1682c2e6f3c0SFlorian Wechsung         ierr = PCPatchCreateMatrix_Private(pc, i, &patch->mat[i], PETSC_FALSE);CHKERRQ(ierr);
1683c2e6f3c0SFlorian Wechsung       }
16844bbf5ea8SMatthew G. Knepley     }
16854bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr);
16864bbf5ea8SMatthew G. Knepley 
16874bbf5ea8SMatthew G. Knepley     /* If desired, calculate weights for dof multiplicity */
16884bbf5ea8SMatthew G. Knepley     if (patch->partition_of_unity) {
16894bbf5ea8SMatthew G. Knepley       ierr = VecDuplicate(patch->localX, &patch->dof_weights);CHKERRQ(ierr);
1690c2e6f3c0SFlorian Wechsung       if(!patch->multiplicative) {
16914bbf5ea8SMatthew G. Knepley         for (i = 0; i < patch->npatch; ++i) {
16924bbf5ea8SMatthew G. Knepley           PetscInt dof;
16934bbf5ea8SMatthew G. Knepley 
16944bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &dof);CHKERRQ(ierr);
16954bbf5ea8SMatthew G. Knepley           if (dof <= 0) continue;
16964bbf5ea8SMatthew G. Knepley           ierr = VecSet(patch->patchX[i], 1.0);CHKERRQ(ierr);
1697c2e6f3c0SFlorian Wechsung           ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchX[i], patch->dof_weights, ADD_VALUES, SCATTER_REVERSE, PETSC_FALSE);CHKERRQ(ierr);
1698c2e6f3c0SFlorian Wechsung         }
1699c2e6f3c0SFlorian Wechsung       } else {
1700c2e6f3c0SFlorian Wechsung         // multiplicative is actually only locally multiplicative and globally additive. need the pou where the mesh decomposition overlaps
1701c2e6f3c0SFlorian Wechsung         ierr = VecSet(patch->dof_weights, 1.0);CHKERRQ(ierr);
17024bbf5ea8SMatthew G. Knepley       }
1703d132cafaSFlorian Wechsung 
1704d132cafaSFlorian Wechsung       PetscScalar *input = NULL;
1705d132cafaSFlorian Wechsung       PetscScalar *output = NULL;
1706d132cafaSFlorian Wechsung       Vec global;
1707d132cafaSFlorian Wechsung       VecDuplicate(patch->dof_weights, &global);
1708d132cafaSFlorian Wechsung       VecSet(global, 0.);
1709d132cafaSFlorian Wechsung 
1710d132cafaSFlorian Wechsung       ierr = VecGetArray(patch->dof_weights, &input);CHKERRQ(ierr);
1711d132cafaSFlorian Wechsung       ierr = VecGetArray(global, &output);CHKERRQ(ierr);
1712d132cafaSFlorian Wechsung       ierr = PetscSFReduceBegin(patch->defaultSF, MPIU_SCALAR, input, output, MPI_SUM);CHKERRQ(ierr);
1713d132cafaSFlorian Wechsung       ierr = PetscSFReduceEnd(patch->defaultSF, MPIU_SCALAR, input, output, MPI_SUM);CHKERRQ(ierr);
1714d132cafaSFlorian Wechsung       ierr = VecRestoreArray(patch->dof_weights, &input);CHKERRQ(ierr);
1715d132cafaSFlorian Wechsung       ierr = VecRestoreArray(global, &output);CHKERRQ(ierr);
1716d132cafaSFlorian Wechsung 
171705528938SFlorian Wechsung       ierr = VecReciprocal(global);CHKERRQ(ierr);
1718d132cafaSFlorian Wechsung 
1719d132cafaSFlorian Wechsung       ierr = VecGetArray(patch->dof_weights, &output);CHKERRQ(ierr);
1720d132cafaSFlorian Wechsung       ierr = VecGetArray(global, &input);CHKERRQ(ierr);
1721d132cafaSFlorian Wechsung       ierr = PetscSFBcastBegin(patch->defaultSF, MPIU_SCALAR, input, output);CHKERRQ(ierr);
1722d132cafaSFlorian Wechsung       ierr = PetscSFBcastEnd(patch->defaultSF, MPIU_SCALAR, input, output);CHKERRQ(ierr);
1723d132cafaSFlorian Wechsung       ierr = VecRestoreArray(patch->dof_weights, &output);CHKERRQ(ierr);
1724d132cafaSFlorian Wechsung       ierr = VecRestoreArray(global, &input);CHKERRQ(ierr);
1725d132cafaSFlorian Wechsung       ierr = VecDestroy(&global);CHKERRQ(ierr);
17264bbf5ea8SMatthew G. Knepley     }
172796b79ebeSFlorian Wechsung     if(patch->multiplicative && patch->save_operators) {
172896b79ebeSFlorian Wechsung       ierr = PetscMalloc1(patch->npatch, &patch->matWithArtificial);CHKERRQ(ierr);
172996b79ebeSFlorian Wechsung     }
17304bbf5ea8SMatthew G. Knepley   }
17314bbf5ea8SMatthew G. Knepley   if (patch->save_operators) {
17324bbf5ea8SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {
17334bbf5ea8SMatthew G. Knepley       ierr = MatZeroEntries(patch->mat[i]);CHKERRQ(ierr);
1734c2e6f3c0SFlorian Wechsung       ierr = PCPatchComputeOperator_Private(pc, patch->mat[i], i, PETSC_FALSE);CHKERRQ(ierr);
17354bbf5ea8SMatthew G. Knepley       ierr = KSPSetOperators(patch->ksp[i], patch->mat[i], patch->mat[i]);CHKERRQ(ierr);
1736c2e6f3c0SFlorian Wechsung     }
1737c2e6f3c0SFlorian Wechsung     if(patch->multiplicative) {
1738c2e6f3c0SFlorian Wechsung       for (i = 0; i < patch->npatch; ++i) {
1739eba130bbSFlorian Wechsung         // Instead of padding patch->patchY with zeros to get
1740eba130bbSFlorian Wechsung         // patch->patchYWithArtificial and then multiplying with the matrix,
1741eba130bbSFlorian Wechsung         // just get rid of the columns that correspond to the dofs with
1742eba130bbSFlorian Wechsung         // artificial bcs. That's of course fairly inefficient, hopefully we
1743eba130bbSFlorian Wechsung         // can just assemble the rectangular matrix in the first place.
1744eba130bbSFlorian Wechsung         Mat matSquare;
1745*63deea8eSPatrick Farrell         IS rowis;
1746*63deea8eSPatrick Farrell         PetscInt dof;
1747*63deea8eSPatrick Farrell 
1748*63deea8eSPatrick Farrell         ierr = MatGetSize(patch->mat[i], &dof, NULL);CHKERRQ(ierr);
1749*63deea8eSPatrick Farrell         if (dof == 0) continue;
1750*63deea8eSPatrick Farrell 
1751eba130bbSFlorian Wechsung         ierr = PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE);CHKERRQ(ierr);
1752eba130bbSFlorian Wechsung         ierr = MatZeroEntries(matSquare);CHKERRQ(ierr);
1753eba130bbSFlorian Wechsung         ierr = PCPatchComputeOperator_Private(pc, matSquare, i, PETSC_TRUE);CHKERRQ(ierr);
1754*63deea8eSPatrick Farrell 
1755eba130bbSFlorian Wechsung         ierr = MatGetSize(matSquare, &dof, NULL);CHKERRQ(ierr);
1756eba130bbSFlorian Wechsung         ierr = ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis); CHKERRQ(ierr);
175796b79ebeSFlorian Wechsung         if(pc->setupcalled) {
175896b79ebeSFlorian Wechsung           ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_REUSE_MATRIX, &patch->matWithArtificial[i]); CHKERRQ(ierr);
175996b79ebeSFlorian Wechsung         } else {
1760eba130bbSFlorian Wechsung           ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &patch->matWithArtificial[i]); CHKERRQ(ierr);
176196b79ebeSFlorian Wechsung         }
1762eba130bbSFlorian Wechsung         ierr = MatDestroy(&matSquare);CHKERRQ(ierr);
1763c2e6f3c0SFlorian Wechsung       }
17644bbf5ea8SMatthew G. Knepley     }
17654bbf5ea8SMatthew G. Knepley   }
17664bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
17674bbf5ea8SMatthew G. Knepley }
17684bbf5ea8SMatthew G. Knepley 
17694bbf5ea8SMatthew G. Knepley static PetscErrorCode PCApply_PATCH(PC pc, Vec x, Vec y)
17704bbf5ea8SMatthew G. Knepley {
17714bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch    = (PC_PATCH *) pc->data;
17724bbf5ea8SMatthew G. Knepley   const PetscScalar *globalX  = NULL;
17734bbf5ea8SMatthew G. Knepley   PetscScalar       *localX   = NULL;
17744bbf5ea8SMatthew G. Knepley   PetscScalar       *globalY  = NULL;
17754bbf5ea8SMatthew G. Knepley   const PetscInt    *bcNodes  = NULL;
17764bbf5ea8SMatthew G. Knepley   PetscInt           nsweep   = patch->symmetrise_sweep ? 2 : 1;
17774bbf5ea8SMatthew G. Knepley   PetscInt           start[2] = {0, 0};
17784bbf5ea8SMatthew G. Knepley   PetscInt           end[2]   = {-1, -1};
17794bbf5ea8SMatthew G. Knepley   const PetscInt     inc[2]   = {1, -1};
17804bbf5ea8SMatthew G. Knepley   const PetscScalar *localY;
17814bbf5ea8SMatthew G. Knepley   const PetscInt    *iterationSet;
17824bbf5ea8SMatthew G. Knepley   PetscInt           pStart, numBcs, n, sweep, bc, j;
17834bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
17844bbf5ea8SMatthew G. Knepley 
17854bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
17864bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr);
17874bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsPushGetViewerOff(PETSC_TRUE);CHKERRQ(ierr);
17884bbf5ea8SMatthew G. Knepley   end[0]   = patch->npatch;
17894bbf5ea8SMatthew G. Knepley   start[1] = patch->npatch-1;
17904bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
17914bbf5ea8SMatthew G. Knepley     ierr = ISGetLocalSize(patch->iterationSet, &end[0]);CHKERRQ(ierr);
17924bbf5ea8SMatthew G. Knepley     start[1] = end[0] - 1;
17934bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr);
17944bbf5ea8SMatthew G. Knepley   }
17954bbf5ea8SMatthew G. Knepley   /* Scatter from global space into overlapped local spaces */
17964bbf5ea8SMatthew G. Knepley   ierr = VecGetArrayRead(x, &globalX);CHKERRQ(ierr);
17974bbf5ea8SMatthew G. Knepley   ierr = VecGetArray(patch->localX, &localX);CHKERRQ(ierr);
17984bbf5ea8SMatthew G. Knepley   ierr = PetscSFBcastBegin(patch->defaultSF, MPIU_SCALAR, globalX, localX);CHKERRQ(ierr);
17994bbf5ea8SMatthew G. Knepley   ierr = PetscSFBcastEnd(patch->defaultSF, MPIU_SCALAR, globalX, localX);CHKERRQ(ierr);
18004bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArrayRead(x, &globalX);CHKERRQ(ierr);
18014bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArray(patch->localX, &localX);CHKERRQ(ierr);
18024bbf5ea8SMatthew G. Knepley 
18034bbf5ea8SMatthew G. Knepley   ierr = VecSet(patch->localY, 0.0);CHKERRQ(ierr);
18044bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, NULL);CHKERRQ(ierr);
18054bbf5ea8SMatthew G. Knepley   for (sweep = 0; sweep < nsweep; sweep++) {
18064bbf5ea8SMatthew G. Knepley     for (j = start[sweep]; j*inc[sweep] < end[sweep]*inc[sweep]; j += inc[sweep]) {
18074bbf5ea8SMatthew G. Knepley       PetscInt i       = patch->user_patches ? iterationSet[j] : j;
18084bbf5ea8SMatthew G. Knepley       PetscInt start, len;
18094bbf5ea8SMatthew G. Knepley 
18104bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &len);CHKERRQ(ierr);
18114bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetOffset(patch->gtolCounts, i+pStart, &start);CHKERRQ(ierr);
18124bbf5ea8SMatthew G. Knepley       /* TODO: Squash out these guys in the setup as well. */
18134bbf5ea8SMatthew G. Knepley       if (len <= 0) continue;
18144bbf5ea8SMatthew G. Knepley       /* TODO: Do we need different scatters for X and Y? */
1815c2e6f3c0SFlorian Wechsung       ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->localX, patch->patchX[i], INSERT_VALUES, SCATTER_FORWARD, PETSC_FALSE);CHKERRQ(ierr);
18164bbf5ea8SMatthew G. Knepley       if (!patch->save_operators) {
18174bbf5ea8SMatthew G. Knepley         Mat mat;
18184bbf5ea8SMatthew G. Knepley 
1819c2e6f3c0SFlorian Wechsung         ierr = PCPatchCreateMatrix_Private(pc, i, &mat, PETSC_FALSE);CHKERRQ(ierr);
18204bbf5ea8SMatthew G. Knepley         /* Populate operator here. */
1821c2e6f3c0SFlorian Wechsung         ierr = PCPatchComputeOperator_Private(pc, mat, i, PETSC_FALSE);CHKERRQ(ierr);
1822928bb9adSStefano Zampini         ierr = KSPSetOperators(patch->ksp[i], mat, mat);CHKERRQ(ierr);
18234bbf5ea8SMatthew G. Knepley         /* Drop reference so the KSPSetOperators below will blow it away. */
18244bbf5ea8SMatthew G. Knepley         ierr = MatDestroy(&mat);CHKERRQ(ierr);
18254bbf5ea8SMatthew G. Knepley       }
18264bbf5ea8SMatthew G. Knepley       ierr = PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr);
1827a1eac568SLawrence Mitchell       if (!patch->ksp[i]->setfromoptionscalled) {
182854ab768cSLawrence Mitchell         ierr = KSPSetFromOptions(patch->ksp[i]);CHKERRQ(ierr);
182954ab768cSLawrence Mitchell       }
18304bbf5ea8SMatthew G. Knepley       ierr = KSPSolve(patch->ksp[i], patch->patchX[i], patch->patchY[i]);CHKERRQ(ierr);
18314bbf5ea8SMatthew G. Knepley       ierr = PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr);
18324bbf5ea8SMatthew G. Knepley 
18334bbf5ea8SMatthew G. Knepley       if (!patch->save_operators) {
18344bbf5ea8SMatthew G. Knepley         PC pc;
18354bbf5ea8SMatthew G. Knepley         ierr = KSPSetOperators(patch->ksp[i], NULL, NULL);CHKERRQ(ierr);
18364bbf5ea8SMatthew G. Knepley         ierr = KSPGetPC(patch->ksp[i], &pc);CHKERRQ(ierr);
18374bbf5ea8SMatthew G. Knepley         /* Destroy PC context too, otherwise the factored matrix hangs around. */
18384bbf5ea8SMatthew G. Knepley         ierr = PCReset(pc);CHKERRQ(ierr);
18394bbf5ea8SMatthew G. Knepley       }
18404bbf5ea8SMatthew G. Knepley 
1841c2e6f3c0SFlorian Wechsung       ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchY[i], patch->localY, ADD_VALUES, SCATTER_REVERSE, PETSC_FALSE);CHKERRQ(ierr);
1842c2e6f3c0SFlorian Wechsung       if(patch->multiplicative){
1843c2e6f3c0SFlorian Wechsung         Mat multMat;
1844c2e6f3c0SFlorian Wechsung         if (patch->save_operators) {
1845c2e6f3c0SFlorian Wechsung           multMat = patch->matWithArtificial[i];
1846c2e6f3c0SFlorian Wechsung         } else {
1847eba130bbSFlorian Wechsung           /*Very inefficient, hopefully we can just assemble the rectangular matrix in the first place.*/
1848eba130bbSFlorian Wechsung           Mat matSquare;
1849eba130bbSFlorian Wechsung           ierr = PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE);CHKERRQ(ierr);
1850eba130bbSFlorian Wechsung           ierr = MatZeroEntries(matSquare);CHKERRQ(ierr);
1851eba130bbSFlorian Wechsung           ierr = PCPatchComputeOperator_Private(pc, matSquare, i, PETSC_TRUE);CHKERRQ(ierr);
1852eba130bbSFlorian Wechsung           PetscInt dof;
1853eba130bbSFlorian Wechsung           ierr = MatGetSize(matSquare, &dof, NULL);CHKERRQ(ierr);
1854eba130bbSFlorian Wechsung           IS rowis;
1855eba130bbSFlorian Wechsung           ierr = ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis); CHKERRQ(ierr);
1856eba130bbSFlorian Wechsung           ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &multMat); CHKERRQ(ierr);
1857eba130bbSFlorian Wechsung           ierr = MatDestroy(&matSquare);CHKERRQ(ierr);
1858c2e6f3c0SFlorian Wechsung         }
1859eba130bbSFlorian Wechsung         ierr = MatMult(multMat, patch->patchY[i], patch->patchXWithArtificial[i]); CHKERRQ(ierr);
1860c2e6f3c0SFlorian Wechsung         ierr = VecScale(patch->patchXWithArtificial[i], -1.0); CHKERRQ(ierr);
1861c2e6f3c0SFlorian Wechsung         ierr = PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchXWithArtificial[i], patch->localX, ADD_VALUES, SCATTER_REVERSE, PETSC_TRUE); CHKERRQ(ierr);
1862c2e6f3c0SFlorian Wechsung         if (patch->multiplicative && !patch->save_operators) {
1863c2e6f3c0SFlorian Wechsung           ierr = MatDestroy(&multMat); CHKERRQ(ierr);
1864c2e6f3c0SFlorian Wechsung         }
1865c2e6f3c0SFlorian Wechsung       }
18664bbf5ea8SMatthew G. Knepley     }
18674bbf5ea8SMatthew G. Knepley   }
18684bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {ierr = ISRestoreIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr);}
18694bbf5ea8SMatthew G. Knepley   /* XXX: should we do this on the global vector? */
187073ec7555SLawrence Mitchell   if (patch->partition_of_unity) {
18714bbf5ea8SMatthew G. Knepley     ierr = VecPointwiseMult(patch->localY, patch->localY, patch->dof_weights);CHKERRQ(ierr);
18724bbf5ea8SMatthew G. Knepley   }
18734bbf5ea8SMatthew G. Knepley   /* Now patch->localY contains the solution of the patch solves, so we need to combine them all. */
18744bbf5ea8SMatthew G. Knepley   ierr = VecSet(y, 0.0);CHKERRQ(ierr);
18754bbf5ea8SMatthew G. Knepley   ierr = VecGetArray(y, &globalY);CHKERRQ(ierr);
18764bbf5ea8SMatthew G. Knepley   ierr = VecGetArrayRead(patch->localY, &localY);CHKERRQ(ierr);
18774bbf5ea8SMatthew G. Knepley   ierr = PetscSFReduceBegin(patch->defaultSF, MPIU_SCALAR, localY, globalY, MPI_SUM);CHKERRQ(ierr);
18784bbf5ea8SMatthew G. Knepley   ierr = PetscSFReduceEnd(patch->defaultSF, MPIU_SCALAR, localY, globalY, MPI_SUM);CHKERRQ(ierr);
18794bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArrayRead(patch->localY, &localY);CHKERRQ(ierr);
18804bbf5ea8SMatthew G. Knepley 
18814bbf5ea8SMatthew G. Knepley   /* Now we need to send the global BC values through */
18824bbf5ea8SMatthew G. Knepley   ierr = VecGetArrayRead(x, &globalX);CHKERRQ(ierr);
18834bbf5ea8SMatthew G. Knepley   ierr = ISGetSize(patch->globalBcNodes, &numBcs);CHKERRQ(ierr);
18844bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr);
18854bbf5ea8SMatthew G. Knepley   ierr = VecGetLocalSize(x, &n);CHKERRQ(ierr);
18864bbf5ea8SMatthew G. Knepley   for (bc = 0; bc < numBcs; ++bc) {
18874bbf5ea8SMatthew G. Knepley     const PetscInt idx = bcNodes[bc];
18884bbf5ea8SMatthew G. Knepley     if (idx < n) globalY[idx] = globalX[idx];
18894bbf5ea8SMatthew G. Knepley   }
18904bbf5ea8SMatthew G. Knepley 
18914bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr);
18924bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArrayRead(x, &globalX);CHKERRQ(ierr);
18934bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArray(y, &globalY);CHKERRQ(ierr);
18944bbf5ea8SMatthew G. Knepley 
18954bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsPopGetViewerOff();CHKERRQ(ierr);
18964bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr);
18974bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
18984bbf5ea8SMatthew G. Knepley }
18994bbf5ea8SMatthew G. Knepley 
19004bbf5ea8SMatthew G. Knepley static PetscErrorCode PCReset_PATCH(PC pc)
19014bbf5ea8SMatthew G. Knepley {
19024bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
19034bbf5ea8SMatthew G. Knepley   PetscInt       i;
19044bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
19054bbf5ea8SMatthew G. Knepley 
19064bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
19074bbf5ea8SMatthew G. Knepley   /* TODO: Get rid of all these ifs */
19084bbf5ea8SMatthew G. Knepley   ierr = PetscSFDestroy(&patch->defaultSF);CHKERRQ(ierr);
19094bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->cellCounts);CHKERRQ(ierr);
19105f824522SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->pointCounts);CHKERRQ(ierr);
19114bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->cellNumbering);CHKERRQ(ierr);
19124bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->gtolCounts);CHKERRQ(ierr);
19134bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->gtol);CHKERRQ(ierr);
19144bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->cells);CHKERRQ(ierr);
19155f824522SMatthew G. Knepley   ierr = ISDestroy(&patch->points);CHKERRQ(ierr);
19164bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->dofs);CHKERRQ(ierr);
19175f824522SMatthew G. Knepley   ierr = ISDestroy(&patch->offs);CHKERRQ(ierr);
19185f824522SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->patchSection);CHKERRQ(ierr);
19194bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->ghostBcNodes);CHKERRQ(ierr);
19204bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->globalBcNodes);CHKERRQ(ierr);
192111ac8bb0SFlorian Wechsung   if(patch->gtolCountsWithArtificial)
192211ac8bb0SFlorian Wechsung     ierr = PetscSectionDestroy(&patch->gtolCountsWithArtificial);CHKERRQ(ierr);
192311ac8bb0SFlorian Wechsung   if(&patch->gtolWithArtificial)
192411ac8bb0SFlorian Wechsung     ierr = ISDestroy(&patch->gtolWithArtificial);CHKERRQ(ierr);
192511ac8bb0SFlorian Wechsung   if(patch->dofsWithArtificial)
192611ac8bb0SFlorian Wechsung     ierr = ISDestroy(&patch->dofsWithArtificial);CHKERRQ(ierr);
192711ac8bb0SFlorian Wechsung   if(patch->offsWithArtificial)
192811ac8bb0SFlorian Wechsung     ierr = ISDestroy(&patch->offsWithArtificial);CHKERRQ(ierr);
192911ac8bb0SFlorian Wechsung 
19304bbf5ea8SMatthew G. Knepley 
19315f824522SMatthew G. Knepley   if (patch->dofSection) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscSectionDestroy(&patch->dofSection[i]);CHKERRQ(ierr);}
19324bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->dofSection);CHKERRQ(ierr);
19334bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->bs);CHKERRQ(ierr);
19344bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->nodesPerCell);CHKERRQ(ierr);
19355f824522SMatthew G. Knepley   if (patch->cellNodeMap) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscFree(patch->cellNodeMap[i]);CHKERRQ(ierr);}
19364bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->cellNodeMap);CHKERRQ(ierr);
19374bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->subspaceOffsets);CHKERRQ(ierr);
19384bbf5ea8SMatthew G. Knepley 
19394bbf5ea8SMatthew G. Knepley   if (patch->ksp) {
19405f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = KSPReset(patch->ksp[i]);CHKERRQ(ierr);}
19414bbf5ea8SMatthew G. Knepley   }
19424bbf5ea8SMatthew G. Knepley 
1943e4c66b91SPatrick Farrell   if (patch->subspaces_to_exclude) {
1944e4c66b91SPatrick Farrell     PetscHSetIDestroy(&patch->subspaces_to_exclude);
1945e4c66b91SPatrick Farrell   }
1946e4c66b91SPatrick Farrell 
19474bbf5ea8SMatthew G. Knepley   ierr = VecDestroy(&patch->localX);CHKERRQ(ierr);
19484bbf5ea8SMatthew G. Knepley   ierr = VecDestroy(&patch->localY);CHKERRQ(ierr);
19494bbf5ea8SMatthew G. Knepley   if (patch->patchX) {
19505f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchX[i]);CHKERRQ(ierr);}
19514bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->patchX);CHKERRQ(ierr);
19524bbf5ea8SMatthew G. Knepley   }
19534bbf5ea8SMatthew G. Knepley   if (patch->patchY) {
19545f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchY[i]);CHKERRQ(ierr);}
19554bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->patchY);CHKERRQ(ierr);
19564bbf5ea8SMatthew G. Knepley   }
19574bbf5ea8SMatthew G. Knepley   ierr = VecDestroy(&patch->dof_weights);CHKERRQ(ierr);
19584bbf5ea8SMatthew G. Knepley   if (patch->patch_dof_weights) {
19595f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patch_dof_weights[i]);CHKERRQ(ierr);}
19604bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->patch_dof_weights);CHKERRQ(ierr);
19614bbf5ea8SMatthew G. Knepley   }
19624bbf5ea8SMatthew G. Knepley   if (patch->mat) {
19635f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->mat[i]);CHKERRQ(ierr);}
19644bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->mat);CHKERRQ(ierr);
19655f824522SMatthew G. Knepley   }
196611ac8bb0SFlorian Wechsung   if (patch->matWithArtificial) {
196711ac8bb0SFlorian Wechsung     for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->matWithArtificial[i]);CHKERRQ(ierr);}
196811ac8bb0SFlorian Wechsung     ierr = PetscFree(patch->matWithArtificial);CHKERRQ(ierr);
196911ac8bb0SFlorian Wechsung   }
197011ac8bb0SFlorian Wechsung   if (patch->patchXWithArtificial) {
197111ac8bb0SFlorian Wechsung     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchXWithArtificial[i]);CHKERRQ(ierr);}
197211ac8bb0SFlorian Wechsung     ierr = PetscFree(patch->patchXWithArtificial);CHKERRQ(ierr);
197311ac8bb0SFlorian Wechsung   }
197496b79ebeSFlorian Wechsung   if(patch->dofMappingWithoutToWithArtificial) {
197596b79ebeSFlorian Wechsung     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->dofMappingWithoutToWithArtificial[i]);CHKERRQ(ierr);}
197696b79ebeSFlorian Wechsung     ierr = PetscFree(patch->dofMappingWithoutToWithArtificial);CHKERRQ(ierr);
197796b79ebeSFlorian Wechsung 
197896b79ebeSFlorian Wechsung   }
19794bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr);
19805f824522SMatthew G. Knepley   if (patch->userIS) {
19815f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->userIS[i]);CHKERRQ(ierr);}
19825f824522SMatthew G. Knepley     ierr = PetscFree(patch->userIS);CHKERRQ(ierr);
19835f824522SMatthew G. Knepley   }
19844bbf5ea8SMatthew G. Knepley   patch->bs          = 0;
19854bbf5ea8SMatthew G. Knepley   patch->cellNodeMap = NULL;
19867974b488SMatthew G. Knepley   patch->nsubspaces  = 0;
19874bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->iterationSet);CHKERRQ(ierr);
19885f824522SMatthew G. Knepley 
19895f824522SMatthew G. Knepley   ierr = PetscViewerDestroy(&patch->viewerSection);CHKERRQ(ierr);
19904bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
19914bbf5ea8SMatthew G. Knepley }
19924bbf5ea8SMatthew G. Knepley 
19934bbf5ea8SMatthew G. Knepley static PetscErrorCode PCDestroy_PATCH(PC pc)
19944bbf5ea8SMatthew G. Knepley {
19954bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
19964bbf5ea8SMatthew G. Knepley   PetscInt       i;
19974bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
19984bbf5ea8SMatthew G. Knepley 
19994bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
20004bbf5ea8SMatthew G. Knepley   ierr = PCReset_PATCH(pc);CHKERRQ(ierr);
20014bbf5ea8SMatthew G. Knepley   if (patch->ksp) {
20024bbf5ea8SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = KSPDestroy(&patch->ksp[i]);CHKERRQ(ierr);}
20034bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->ksp);CHKERRQ(ierr);
20044bbf5ea8SMatthew G. Knepley   }
20054bbf5ea8SMatthew G. Knepley   ierr = PetscFree(pc->data);CHKERRQ(ierr);
20064bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
20074bbf5ea8SMatthew G. Knepley }
20084bbf5ea8SMatthew G. Knepley 
20094bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetFromOptions_PATCH(PetscOptionItems *PetscOptionsObject, PC pc)
20104bbf5ea8SMatthew G. Knepley {
20114bbf5ea8SMatthew G. Knepley   PC_PATCH            *patch = (PC_PATCH *) pc->data;
20124bbf5ea8SMatthew G. Knepley   PCPatchConstructType patchConstructionType = PC_PATCH_STAR;
20135f824522SMatthew G. Knepley   char                 sub_mat_type[PETSC_MAX_PATH_LEN];
20145f824522SMatthew G. Knepley   const char          *prefix;
20154bbf5ea8SMatthew G. Knepley   PetscBool            flg, dimflg, codimflg;
20165f824522SMatthew G. Knepley   MPI_Comm             comm;
2017a48c39c8SPatrick Farrell   PetscInt            *ifields, nfields, k;
20184bbf5ea8SMatthew G. Knepley   PetscErrorCode       ierr;
20194bbf5ea8SMatthew G. Knepley 
20204bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
20215f824522SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) pc, &comm);CHKERRQ(ierr);
20225f824522SMatthew G. Knepley   ierr = PetscObjectGetOptionsPrefix((PetscObject) pc, &prefix);CHKERRQ(ierr);
20234bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsHead(PetscOptionsObject, "Vertex-patch Additive Schwarz options");CHKERRQ(ierr);
20244bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsBool("-pc_patch_save_operators",  "Store all patch operators for lifetime of PC?", "PCPatchSetSaveOperators", patch->save_operators, &patch->save_operators, &flg);CHKERRQ(ierr);
20254bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsBool("-pc_patch_partition_of_unity", "Weight contributions by dof multiplicity?", "PCPatchSetPartitionOfUnity", patch->partition_of_unity, &patch->partition_of_unity, &flg);CHKERRQ(ierr);
2026c2e6f3c0SFlorian Wechsung   ierr = PetscOptionsBool("-pc_patch_multiplicative", "Gauss-Seidel instead of Jacobi?", "PCPatchSetMultiplicative", patch->multiplicative, &patch->multiplicative, &flg);CHKERRQ(ierr);
20275f824522SMatthew G. Knepley   ierr = PetscOptionsInt("-pc_patch_construct_dim", "What dimension of mesh point to construct patches by? (0 = vertices)", "PCPATCH", patch->dim, &patch->dim, &dimflg);CHKERRQ(ierr);
20285f824522SMatthew G. Knepley   ierr = PetscOptionsInt("-pc_patch_construct_codim", "What co-dimension of mesh point to construct patches by? (0 = cells)", "PCPATCH", patch->codim, &patch->codim, &codimflg);CHKERRQ(ierr);
20295f824522SMatthew G. Knepley   if (dimflg && codimflg) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Can only set one of dimension or co-dimension");CHKERRQ(ierr);
20304bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsEnum("-pc_patch_construct_type", "How should the patches be constructed?", "PCPatchSetConstructType", PCPatchConstructTypes, (PetscEnum) patchConstructionType, (PetscEnum *) &patchConstructionType, &flg);CHKERRQ(ierr);
20314bbf5ea8SMatthew G. Knepley   if (flg) {ierr = PCPatchSetConstructType(pc, patchConstructionType, NULL, NULL);CHKERRQ(ierr);}
20325f824522SMatthew G. Knepley   ierr = PetscOptionsInt("-pc_patch_vanka_dim", "Topological dimension of entities for Vanka to ignore", "PCPATCH", patch->vankadim, &patch->vankadim, &flg);CHKERRQ(ierr);
20335f824522SMatthew G. Knepley   ierr = PetscOptionsInt("-pc_patch_ignore_dim", "Topological dimension of entities for completion to ignore", "PCPATCH", patch->ignoredim, &patch->ignoredim, &flg);CHKERRQ(ierr);
20345f824522SMatthew G. Knepley   ierr = PetscOptionsFList("-pc_patch_sub_mat_type", "Matrix type for patch solves", "PCPatchSetSubMatType", MatList, NULL, sub_mat_type, PETSC_MAX_PATH_LEN, &flg);CHKERRQ(ierr);
20354bbf5ea8SMatthew G. Knepley   if (flg) {ierr = PCPatchSetSubMatType(pc, sub_mat_type);CHKERRQ(ierr);}
20365f824522SMatthew G. Knepley   ierr = PetscOptionsBool("-pc_patch_symmetrise_sweep", "Go start->end, end->start?", "PCPATCH", patch->symmetrise_sweep, &patch->symmetrise_sweep, &flg);CHKERRQ(ierr);
2037e4c66b91SPatrick Farrell 
2038a48c39c8SPatrick Farrell   /* If the user has set the number of subspaces, use that for the buffer size,
2039a48c39c8SPatrick Farrell      otherwise use a large number */
2040a48c39c8SPatrick Farrell   if (patch->nsubspaces <= 0) {
2041a48c39c8SPatrick Farrell     nfields = 128;
2042a48c39c8SPatrick Farrell   } else {
2043a48c39c8SPatrick Farrell     nfields = patch->nsubspaces;
2044a48c39c8SPatrick Farrell   }
2045a48c39c8SPatrick Farrell   ierr = PetscMalloc1(nfields, &ifields);CHKERRQ(ierr);
2046e4c66b91SPatrick Farrell   ierr = PetscOptionsGetIntArray(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_patch_exclude_subspaces",ifields,&nfields,&flg);CHKERRQ(ierr);
2047e4c66b91SPatrick 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");
2048e4c66b91SPatrick Farrell   if (flg) {
2049e4c66b91SPatrick Farrell     PetscHSetIClear(patch->subspaces_to_exclude);
205059b66c28SPatrick Farrell     for (k = 0; k < nfields; k++) {
2051e4c66b91SPatrick Farrell       PetscHSetIAdd(patch->subspaces_to_exclude, ifields[k]);
2052e4c66b91SPatrick Farrell     }
2053e4c66b91SPatrick Farrell   }
205459b66c28SPatrick Farrell   ierr = PetscFree(ifields);CHKERRQ(ierr);
20555f824522SMatthew G. Knepley 
20565f824522SMatthew G. Knepley   ierr = PetscOptionsBool("-pc_patch_patches_view", "Print out information during patch construction", "PCPATCH", patch->viewPatches, &patch->viewPatches, &flg);CHKERRQ(ierr);
205716413a6aSBarry Smith   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options,prefix, "-pc_patch_cells_view",   &patch->viewerCells,   &patch->formatCells,   &patch->viewCells);CHKERRQ(ierr);
205816413a6aSBarry Smith   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options,prefix, "-pc_patch_points_view",  &patch->viewerPoints,  &patch->formatPoints,  &patch->viewPoints);CHKERRQ(ierr);
205916413a6aSBarry Smith   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options,prefix, "-pc_patch_section_view", &patch->viewerSection, &patch->formatSection, &patch->viewSection);CHKERRQ(ierr);
206016413a6aSBarry Smith   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options,prefix, "-pc_patch_mat_view",     &patch->viewerMatrix,  &patch->formatMatrix,  &patch->viewMatrix);CHKERRQ(ierr);
20614bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsTail();CHKERRQ(ierr);
20625f824522SMatthew G. Knepley   patch->optionsSet = PETSC_TRUE;
20634bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
20644bbf5ea8SMatthew G. Knepley }
20654bbf5ea8SMatthew G. Knepley 
20664bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUpOnBlocks_PATCH(PC pc)
20674bbf5ea8SMatthew G. Knepley {
20684bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch = (PC_PATCH*) pc->data;
20694bbf5ea8SMatthew G. Knepley   KSPConvergedReason reason;
20704bbf5ea8SMatthew G. Knepley   PetscInt           i;
20714bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
20724bbf5ea8SMatthew G. Knepley 
20734bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2074a1eac568SLawrence Mitchell   if (!patch->save_operators) {
2075a1eac568SLawrence Mitchell     /* Can't do this here because the sub KSPs don't have an operator attached yet. */
2076a1eac568SLawrence Mitchell     PetscFunctionReturn(0);
2077a1eac568SLawrence Mitchell   }
20784bbf5ea8SMatthew G. Knepley   for (i = 0; i < patch->npatch; ++i) {
2079a1eac568SLawrence Mitchell     if (!patch->ksp[i]->setfromoptionscalled) {
2080a1eac568SLawrence Mitchell       ierr = KSPSetFromOptions(patch->ksp[i]);CHKERRQ(ierr);
2081a1eac568SLawrence Mitchell     }
20824bbf5ea8SMatthew G. Knepley     ierr = KSPSetUp(patch->ksp[i]);CHKERRQ(ierr);
20834bbf5ea8SMatthew G. Knepley     ierr = KSPGetConvergedReason(patch->ksp[i], &reason);CHKERRQ(ierr);
20844bbf5ea8SMatthew G. Knepley     if (reason == KSP_DIVERGED_PCSETUP_FAILED) pc->failedreason = PC_SUBPC_ERROR;
20854bbf5ea8SMatthew G. Knepley   }
20864bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
20874bbf5ea8SMatthew G. Knepley }
20884bbf5ea8SMatthew G. Knepley 
20894bbf5ea8SMatthew G. Knepley static PetscErrorCode PCView_PATCH(PC pc, PetscViewer viewer)
20904bbf5ea8SMatthew G. Knepley {
20914bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
20924bbf5ea8SMatthew G. Knepley   PetscViewer    sviewer;
20934bbf5ea8SMatthew G. Knepley   PetscBool      isascii;
20944bbf5ea8SMatthew G. Knepley   PetscMPIInt    rank;
20954bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
20964bbf5ea8SMatthew G. Knepley 
20974bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
20984bbf5ea8SMatthew G. Knepley   /* TODO Redo tabbing with set tbas in new style */
20994bbf5ea8SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii);CHKERRQ(ierr);
21004bbf5ea8SMatthew G. Knepley   if (!isascii) PetscFunctionReturn(0);
21014bbf5ea8SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) pc), &rank);CHKERRQ(ierr);
21024bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
21034bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "Subspace Correction preconditioner with %d patches\n", patch->npatch);CHKERRQ(ierr);
2104c2e6f3c0SFlorian Wechsung   if(patch->multiplicative) {
2105c2e6f3c0SFlorian Wechsung       ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: multiplicative\n");CHKERRQ(ierr);
2106c2e6f3c0SFlorian Wechsung   }
2107c2e6f3c0SFlorian Wechsung   else {
210873ec7555SLawrence Mitchell       ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: additive\n");CHKERRQ(ierr);
2109c2e6f3c0SFlorian Wechsung   }
21104bbf5ea8SMatthew G. Knepley   if (patch->partition_of_unity) {ierr = PetscViewerASCIIPrintf(viewer, "Weighting by partition of unity\n");CHKERRQ(ierr);}
21114bbf5ea8SMatthew G. Knepley   else                           {ierr = PetscViewerASCIIPrintf(viewer, "Not weighting by partition of unity\n");CHKERRQ(ierr);}
21124bbf5ea8SMatthew G. Knepley   if (patch->symmetrise_sweep) {ierr = PetscViewerASCIIPrintf(viewer, "Symmetrising sweep (start->end, then end->start)\n");CHKERRQ(ierr);}
21134bbf5ea8SMatthew G. Knepley   else                         {ierr = PetscViewerASCIIPrintf(viewer, "Not symmetrising sweep\n");CHKERRQ(ierr);}
21144bbf5ea8SMatthew G. Knepley   if (!patch->save_operators) {ierr = PetscViewerASCIIPrintf(viewer, "Not saving patch operators (rebuilt every PCApply)\n");CHKERRQ(ierr);}
21154bbf5ea8SMatthew G. Knepley   else                        {ierr = PetscViewerASCIIPrintf(viewer, "Saving patch operators (rebuilt every PCSetUp)\n");CHKERRQ(ierr);}
21164bbf5ea8SMatthew G. Knepley   if (patch->patchconstructop == PCPatchConstruct_Star)       {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: star\n");CHKERRQ(ierr);}
21174bbf5ea8SMatthew G. Knepley   else if (patch->patchconstructop == PCPatchConstruct_Vanka) {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: Vanka\n");CHKERRQ(ierr);}
21184bbf5ea8SMatthew G. Knepley   else if (patch->patchconstructop == PCPatchConstruct_User)  {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: user-specified\n");CHKERRQ(ierr);}
21194bbf5ea8SMatthew G. Knepley   else                                                        {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: unknown\n");CHKERRQ(ierr);}
21204bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "KSP on patches (all same):\n");CHKERRQ(ierr);
21214bbf5ea8SMatthew G. Knepley   if (patch->ksp) {
21224bbf5ea8SMatthew G. Knepley     ierr = PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr);
21234bbf5ea8SMatthew G. Knepley     if (!rank) {
21244bbf5ea8SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(sviewer);CHKERRQ(ierr);
21254bbf5ea8SMatthew G. Knepley       ierr = KSPView(patch->ksp[0], sviewer);CHKERRQ(ierr);
21264bbf5ea8SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(sviewer);CHKERRQ(ierr);
21274bbf5ea8SMatthew G. Knepley     }
21284bbf5ea8SMatthew G. Knepley     ierr = PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr);
21294bbf5ea8SMatthew G. Knepley   } else {
21304bbf5ea8SMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
21314bbf5ea8SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "KSP not yet set.\n");CHKERRQ(ierr);
21324bbf5ea8SMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
21334bbf5ea8SMatthew G. Knepley   }
21344bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
21354bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
21364bbf5ea8SMatthew G. Knepley }
21374bbf5ea8SMatthew G. Knepley 
2138e5893cccSMatthew G. Knepley /*MC
213998ed095eSMatthew G. Knepley   PCPATCH - A PC object that encapsulates flexible definition of blocks for overlapping and non-overlapping
214098ed095eSMatthew G. Knepley             small block additive preconditioners. Block definition is based on topology from
2141e5893cccSMatthew G. Knepley             a DM and equation numbering from a PetscSection.
2142e5893cccSMatthew G. Knepley 
2143e5893cccSMatthew G. Knepley   Options Database Keys:
2144e5893cccSMatthew G. Knepley + -pc_patch_cells_view   - Views the process local cell numbers for each patch
2145e5893cccSMatthew G. Knepley . -pc_patch_points_view  - Views the process local mesh point numbers for each patch
2146e5893cccSMatthew G. Knepley . -pc_patch_g2l_view     - Views the map between global dofs and patch local dofs for each patch
2147e5893cccSMatthew G. Knepley . -pc_patch_patches_view - Views the global dofs associated with each patch and its boundary
2148e5893cccSMatthew G. Knepley - -pc_patch_sub_mat_view - Views the matrix associated with each patch
2149e5893cccSMatthew G. Knepley 
2150e5893cccSMatthew G. Knepley   Level: intermediate
2151e5893cccSMatthew G. Knepley 
2152e5893cccSMatthew G. Knepley .seealso: PCType, PCCreate(), PCSetType()
2153e5893cccSMatthew G. Knepley M*/
2154642283e9SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PCCreate_Patch(PC pc)
21554bbf5ea8SMatthew G. Knepley {
21564bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch;
21574bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
21584bbf5ea8SMatthew G. Knepley 
21594bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
21604bbf5ea8SMatthew G. Knepley   ierr = PetscNewLog(pc, &patch);CHKERRQ(ierr);
21614bbf5ea8SMatthew G. Knepley 
2162e4c66b91SPatrick Farrell   if (patch->subspaces_to_exclude) {
2163e4c66b91SPatrick Farrell     PetscHSetIDestroy(&patch->subspaces_to_exclude);
2164e4c66b91SPatrick Farrell   }
2165e4c66b91SPatrick Farrell   PetscHSetICreate(&patch->subspaces_to_exclude);
2166e4c66b91SPatrick Farrell 
21674bbf5ea8SMatthew G. Knepley   /* Set some defaults */
21685f824522SMatthew G. Knepley   patch->combined           = PETSC_FALSE;
21694bbf5ea8SMatthew G. Knepley   patch->save_operators     = PETSC_TRUE;
2170c2e6f3c0SFlorian Wechsung   patch->multiplicative     = PETSC_FALSE;
21714bbf5ea8SMatthew G. Knepley   patch->partition_of_unity = PETSC_FALSE;
21724bbf5ea8SMatthew G. Knepley   patch->codim              = -1;
21734bbf5ea8SMatthew G. Knepley   patch->dim                = -1;
21744bbf5ea8SMatthew G. Knepley   patch->vankadim           = -1;
21755f824522SMatthew G. Knepley   patch->ignoredim          = -1;
21764bbf5ea8SMatthew G. Knepley   patch->patchconstructop   = PCPatchConstruct_Star;
21774bbf5ea8SMatthew G. Knepley   patch->symmetrise_sweep   = PETSC_FALSE;
21785f824522SMatthew G. Knepley   patch->npatch             = 0;
21794bbf5ea8SMatthew G. Knepley   patch->userIS             = NULL;
21805f824522SMatthew G. Knepley   patch->optionsSet         = PETSC_FALSE;
21814bbf5ea8SMatthew G. Knepley   patch->iterationSet       = NULL;
21824bbf5ea8SMatthew G. Knepley   patch->user_patches       = PETSC_FALSE;
21835f824522SMatthew G. Knepley   ierr = PetscStrallocpy(MATDENSE, (char **) &patch->sub_mat_type);CHKERRQ(ierr);
21845f824522SMatthew G. Knepley   patch->viewPatches        = PETSC_FALSE;
21855f824522SMatthew G. Knepley   patch->viewCells          = PETSC_FALSE;
21865f824522SMatthew G. Knepley   patch->viewPoints         = PETSC_FALSE;
21875f824522SMatthew G. Knepley   patch->viewSection        = PETSC_FALSE;
21885f824522SMatthew G. Knepley   patch->viewMatrix         = PETSC_FALSE;
21894bbf5ea8SMatthew G. Knepley 
21904bbf5ea8SMatthew G. Knepley   pc->data                 = (void *) patch;
21914bbf5ea8SMatthew G. Knepley   pc->ops->apply           = PCApply_PATCH;
21924bbf5ea8SMatthew G. Knepley   pc->ops->applytranspose  = 0; /* PCApplyTranspose_PATCH; */
21934bbf5ea8SMatthew G. Knepley   pc->ops->setup           = PCSetUp_PATCH;
21944bbf5ea8SMatthew G. Knepley   pc->ops->reset           = PCReset_PATCH;
21954bbf5ea8SMatthew G. Knepley   pc->ops->destroy         = PCDestroy_PATCH;
21964bbf5ea8SMatthew G. Knepley   pc->ops->setfromoptions  = PCSetFromOptions_PATCH;
21974bbf5ea8SMatthew G. Knepley   pc->ops->setuponblocks   = PCSetUpOnBlocks_PATCH;
21984bbf5ea8SMatthew G. Knepley   pc->ops->view            = PCView_PATCH;
21994bbf5ea8SMatthew G. Knepley   pc->ops->applyrichardson = 0;
22004bbf5ea8SMatthew G. Knepley 
22014bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
22024bbf5ea8SMatthew G. Knepley }
2203