xref: /petsc/src/ksp/pc/impls/patch/pcpatch.c (revision 0904074fa5c219b600f2ba4aedb333b7bd0095b4)
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 */
28661c4b389SFlorian Wechsung PetscErrorCode PCPatchSetLocalComposition(PC pc, PCCompositeType type)
287c2e6f3c0SFlorian Wechsung {
288c2e6f3c0SFlorian Wechsung   PC_PATCH *patch = (PC_PATCH *) pc->data;
289c2e6f3c0SFlorian Wechsung   PetscFunctionBegin;
29061c4b389SFlorian Wechsung   if (type != PC_COMPOSITE_ADDITIVE && type != PC_COMPOSITE_MULTIPLICATIVE) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Only supports additive or multiplicative as the local type");
29161c4b389SFlorian Wechsung   patch->local_composition_type = type;
292c2e6f3c0SFlorian Wechsung   PetscFunctionReturn(0);
293c2e6f3c0SFlorian Wechsung }
294c2e6f3c0SFlorian Wechsung 
295c2e6f3c0SFlorian Wechsung /* TODO: Docs */
29661c4b389SFlorian Wechsung PetscErrorCode PCPatchGetLocalComposition(PC pc, PCCompositeType *type)
297c2e6f3c0SFlorian Wechsung {
298c2e6f3c0SFlorian Wechsung   PC_PATCH *patch = (PC_PATCH *) pc->data;
299c2e6f3c0SFlorian Wechsung   PetscFunctionBegin;
30061c4b389SFlorian Wechsung   *type = patch->local_composition_type;
301c2e6f3c0SFlorian Wechsung   PetscFunctionReturn(0);
302c2e6f3c0SFlorian Wechsung }
303c2e6f3c0SFlorian Wechsung 
304c2e6f3c0SFlorian Wechsung /* TODO: Docs */
3054bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetSubMatType(PC pc, MatType sub_mat_type)
3064bbf5ea8SMatthew G. Knepley {
3074bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
3084bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
3094bbf5ea8SMatthew G. Knepley 
3104bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3114bbf5ea8SMatthew G. Knepley   if (patch->sub_mat_type) {ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr);}
3124bbf5ea8SMatthew G. Knepley   ierr = PetscStrallocpy(sub_mat_type, (char **) &patch->sub_mat_type);CHKERRQ(ierr);
3134bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3144bbf5ea8SMatthew G. Knepley }
3154bbf5ea8SMatthew G. Knepley 
3164bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3174bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetSubMatType(PC pc, MatType *sub_mat_type)
3184bbf5ea8SMatthew G. Knepley {
3194bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3204bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3214bbf5ea8SMatthew G. Knepley   *sub_mat_type = patch->sub_mat_type;
3224bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3234bbf5ea8SMatthew G. Knepley }
3244bbf5ea8SMatthew G. Knepley 
3254bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3264bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetCellNumbering(PC pc, PetscSection cellNumbering)
3274bbf5ea8SMatthew G. Knepley {
3284bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
3294bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
3304bbf5ea8SMatthew G. Knepley 
3314bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3324bbf5ea8SMatthew G. Knepley   patch->cellNumbering = cellNumbering;
3334bbf5ea8SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) cellNumbering);CHKERRQ(ierr);
3344bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3354bbf5ea8SMatthew G. Knepley }
3364bbf5ea8SMatthew G. Knepley 
3374bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3384bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetCellNumbering(PC pc, PetscSection *cellNumbering)
3394bbf5ea8SMatthew G. Knepley {
3404bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3414bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3424bbf5ea8SMatthew G. Knepley   *cellNumbering = patch->cellNumbering;
3434bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3444bbf5ea8SMatthew G. Knepley }
3454bbf5ea8SMatthew G. Knepley 
3464bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3474bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetConstructType(PC pc, PCPatchConstructType ctype, PetscErrorCode (*func)(PC, PetscInt *, IS **, IS *, void *), void *ctx)
3484bbf5ea8SMatthew G. Knepley {
3494bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3504bbf5ea8SMatthew G. Knepley 
3514bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3524bbf5ea8SMatthew G. Knepley   patch->ctype = ctype;
3534bbf5ea8SMatthew G. Knepley   switch (ctype) {
3544bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
35540c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
3564bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Star;
3574bbf5ea8SMatthew G. Knepley     break;
3584bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
35940c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
3604bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Vanka;
3614bbf5ea8SMatthew G. Knepley     break;
3624bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
3634bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
3644bbf5ea8SMatthew G. Knepley     patch->user_patches     = PETSC_TRUE;
3654bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_User;
366bdd9e0cdSPatrick Farrell     if (func) {
3674bbf5ea8SMatthew G. Knepley       patch->userpatchconstructionop = func;
3684bbf5ea8SMatthew G. Knepley       patch->userpatchconstructctx   = ctx;
369bdd9e0cdSPatrick Farrell     }
3704bbf5ea8SMatthew G. Knepley     break;
3714bbf5ea8SMatthew G. Knepley   default:
3724bbf5ea8SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype);
3734bbf5ea8SMatthew G. Knepley   }
3744bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3754bbf5ea8SMatthew G. Knepley }
3764bbf5ea8SMatthew G. Knepley 
3774bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3784bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetConstructType(PC pc, PCPatchConstructType *ctype, PetscErrorCode (**func)(PC, PetscInt *, IS **, IS *, void *), void **ctx)
3794bbf5ea8SMatthew G. Knepley {
3804bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3814bbf5ea8SMatthew G. Knepley 
3824bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3834bbf5ea8SMatthew G. Knepley   *ctype = patch->ctype;
3844bbf5ea8SMatthew G. Knepley   switch (patch->ctype) {
3854bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
3864bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
3874bbf5ea8SMatthew G. Knepley     break;
3884bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
3894bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
3904bbf5ea8SMatthew G. Knepley     *func = patch->userpatchconstructionop;
3914bbf5ea8SMatthew G. Knepley     *ctx  = patch->userpatchconstructctx;
3924bbf5ea8SMatthew G. Knepley     break;
3934bbf5ea8SMatthew G. Knepley   default:
3944bbf5ea8SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype);
3954bbf5ea8SMatthew G. Knepley   }
3964bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3974bbf5ea8SMatthew G. Knepley }
3984bbf5ea8SMatthew G. Knepley 
3994bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4004bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfo(PC pc, PetscInt nsubspaces, DM *dms, PetscInt *bs, PetscInt *nodesPerCell, const PetscInt **cellNodeMap,
4014bbf5ea8SMatthew G. Knepley                                             const PetscInt *subspaceOffsets, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
4024bbf5ea8SMatthew G. Knepley {
4034bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
4045f824522SMatthew G. Knepley   DM             dm;
4054bbf5ea8SMatthew G. Knepley   PetscSF       *sfs;
4065f824522SMatthew G. Knepley   PetscInt       cStart, cEnd, i, j;
4074bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
4084bbf5ea8SMatthew G. Knepley 
4094bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4105f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
4115f824522SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
4124bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &sfs);CHKERRQ(ierr);
4134bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->dofSection);CHKERRQ(ierr);
4144bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->bs);CHKERRQ(ierr);
4154bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->nodesPerCell);CHKERRQ(ierr);
4164bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->cellNodeMap);CHKERRQ(ierr);
4174bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces+1, &patch->subspaceOffsets);CHKERRQ(ierr);
4184bbf5ea8SMatthew G. Knepley 
4194bbf5ea8SMatthew G. Knepley   patch->nsubspaces       = nsubspaces;
4204bbf5ea8SMatthew G. Knepley   patch->totalDofsPerCell = 0;
4214bbf5ea8SMatthew G. Knepley   for (i = 0; i < nsubspaces; ++i) {
4224bbf5ea8SMatthew G. Knepley     ierr = DMGetDefaultSection(dms[i], &patch->dofSection[i]);CHKERRQ(ierr);
4234bbf5ea8SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) patch->dofSection[i]);CHKERRQ(ierr);
4244bbf5ea8SMatthew G. Knepley     ierr = DMGetDefaultSF(dms[i], &sfs[i]);CHKERRQ(ierr);
4254bbf5ea8SMatthew G. Knepley     patch->bs[i]              = bs[i];
4264bbf5ea8SMatthew G. Knepley     patch->nodesPerCell[i]    = nodesPerCell[i];
4274bbf5ea8SMatthew G. Knepley     patch->totalDofsPerCell  += nodesPerCell[i]*bs[i];
42880e8a965SFlorian Wechsung     ierr = PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i]);CHKERRQ(ierr);
42980e8a965SFlorian Wechsung     for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
4304bbf5ea8SMatthew G. Knepley     patch->subspaceOffsets[i] = subspaceOffsets[i];
4314bbf5ea8SMatthew G. Knepley   }
4324bbf5ea8SMatthew G. Knepley   ierr = PCPatchCreateDefaultSF_Private(pc, nsubspaces, sfs, patch->bs);CHKERRQ(ierr);
4334bbf5ea8SMatthew G. Knepley   ierr = PetscFree(sfs);CHKERRQ(ierr);
4344bbf5ea8SMatthew G. Knepley 
4354bbf5ea8SMatthew G. Knepley   patch->subspaceOffsets[nsubspaces] = subspaceOffsets[nsubspaces];
4364bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes);CHKERRQ(ierr);
4374bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes);CHKERRQ(ierr);
4384bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4394bbf5ea8SMatthew G. Knepley }
4404bbf5ea8SMatthew G. Knepley 
4414bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4425f824522SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfoCombined(PC pc, DM dm, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
4435f824522SMatthew G. Knepley {
4445f824522SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
4455f824522SMatthew G. Knepley   PetscInt       cStart, cEnd, i, j;
4465f824522SMatthew G. Knepley   PetscErrorCode ierr;
4475f824522SMatthew G. Knepley 
4485f824522SMatthew G. Knepley   PetscFunctionBegin;
4495f824522SMatthew G. Knepley   patch->combined = PETSC_TRUE;
4505f824522SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
4515f824522SMatthew G. Knepley   ierr = DMGetNumFields(dm, &patch->nsubspaces);CHKERRQ(ierr);
4525f824522SMatthew G. Knepley   ierr = PetscCalloc1(patch->nsubspaces, &patch->dofSection);CHKERRQ(ierr);
4535f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->bs);CHKERRQ(ierr);
4545f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->nodesPerCell);CHKERRQ(ierr);
4555f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->cellNodeMap);CHKERRQ(ierr);
4565f824522SMatthew G. Knepley   ierr = PetscCalloc1(patch->nsubspaces+1, &patch->subspaceOffsets);CHKERRQ(ierr);
4575f824522SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &patch->dofSection[0]);CHKERRQ(ierr);
4585f824522SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) patch->dofSection[0]);CHKERRQ(ierr);
4595f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(patch->dofSection[0], &patch->subspaceOffsets[patch->nsubspaces]);CHKERRQ(ierr);
4605f824522SMatthew G. Knepley   patch->totalDofsPerCell = 0;
4615f824522SMatthew G. Knepley   for (i = 0; i < patch->nsubspaces; ++i) {
4625f824522SMatthew G. Knepley     patch->bs[i]             = 1;
4635f824522SMatthew G. Knepley     patch->nodesPerCell[i]   = nodesPerCell[i];
4645f824522SMatthew G. Knepley     patch->totalDofsPerCell += nodesPerCell[i];
4655f824522SMatthew G. Knepley     ierr = PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i]);CHKERRQ(ierr);
4665f824522SMatthew G. Knepley     for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
4675f824522SMatthew G. Knepley   }
4685f824522SMatthew G. Knepley   ierr = DMGetDefaultSF(dm, &patch->defaultSF);CHKERRQ(ierr);
4695f824522SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) patch->defaultSF);CHKERRQ(ierr);
4705f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes);CHKERRQ(ierr);
4715f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes);CHKERRQ(ierr);
4725f824522SMatthew G. Knepley   PetscFunctionReturn(0);
4735f824522SMatthew G. Knepley }
4745f824522SMatthew G. Knepley 
4755f824522SMatthew G. Knepley /*@C
4765f824522SMatthew G. Knepley 
47792d50984SMatthew G. Knepley   PCPatchSetComputeFunction - Set the callback used to compute patch residuals
47892d50984SMatthew G. Knepley 
47992d50984SMatthew G. Knepley   Input Parameters:
48092d50984SMatthew G. Knepley + pc   - The PC
48192d50984SMatthew G. Knepley . func - The callback
48292d50984SMatthew G. Knepley - ctx  - The user context
48392d50984SMatthew G. Knepley 
48492d50984SMatthew G. Knepley   Level: advanced
48592d50984SMatthew G. Knepley 
48692d50984SMatthew G. Knepley   Note:
48792d50984SMatthew G. Knepley   The callback has signature:
48892d50984SMatthew G. Knepley +  usercomputef(pc, point, x, f, cellIS, n, u, ctx)
48992d50984SMatthew G. Knepley +  pc     - The PC
49092d50984SMatthew G. Knepley +  point  - The point
49192d50984SMatthew G. Knepley +  x      - The input solution (not used in linear problems)
49292d50984SMatthew G. Knepley +  f      - The patch residual vector
49392d50984SMatthew G. Knepley +  cellIS - An array of the cell numbers
49492d50984SMatthew G. Knepley +  n      - The size of g2l
49592d50984SMatthew G. Knepley +  g2l    - The global to local dof translation table
49692d50984SMatthew G. Knepley +  ctx    - The user context
49792d50984SMatthew G. Knepley   and can assume that the matrix entries have been set to zero before the call.
49892d50984SMatthew G. Knepley 
49992d50984SMatthew G. Knepley .seealso: PCPatchSetComputeOperator(), PCPatchGetComputeOperator(), PCPatchSetDiscretisationInfo()
50092d50984SMatthew G. Knepley @*/
50139fd2e8aSPatrick Farrell PetscErrorCode PCPatchSetComputeFunction(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Vec, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
50292d50984SMatthew G. Knepley {
50392d50984SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
50492d50984SMatthew G. Knepley 
50592d50984SMatthew G. Knepley   PetscFunctionBegin;
50692d50984SMatthew G. Knepley   patch->usercomputef    = func;
50792d50984SMatthew G. Knepley   patch->usercomputefctx = ctx;
50892d50984SMatthew G. Knepley   PetscFunctionReturn(0);
50992d50984SMatthew G. Knepley }
51092d50984SMatthew G. Knepley 
51192d50984SMatthew G. Knepley /*@C
51292d50984SMatthew G. Knepley 
5135f824522SMatthew G. Knepley   PCPatchSetComputeOperator - Set the callback used to compute patch matrices
5145f824522SMatthew G. Knepley 
5155f824522SMatthew G. Knepley   Input Parameters:
5165f824522SMatthew G. Knepley + pc   - The PC
5175f824522SMatthew G. Knepley . func - The callback
5185f824522SMatthew G. Knepley - ctx  - The user context
5195f824522SMatthew G. Knepley 
5205f824522SMatthew G. Knepley   Level: advanced
5215f824522SMatthew G. Knepley 
5225f824522SMatthew G. Knepley   Note:
5235f824522SMatthew G. Knepley   The callback has signature:
524723f9013SMatthew G. Knepley +  usercomputeop(pc, point, x, mat, cellIS, n, u, ctx)
5255f824522SMatthew G. Knepley +  pc     - The PC
526bdd9e0cdSPatrick Farrell +  point  - The point
527723f9013SMatthew G. Knepley +  x      - The input solution (not used in linear problems)
5285f824522SMatthew G. Knepley +  mat    - The patch matrix
5296f158342SMatthew G. Knepley +  cellIS - An array of the cell numbers
5305f824522SMatthew G. Knepley +  n      - The size of g2l
5315f824522SMatthew G. Knepley +  g2l    - The global to local dof translation table
5325f824522SMatthew G. Knepley +  ctx    - The user context
5335f824522SMatthew G. Knepley   and can assume that the matrix entries have been set to zero before the call.
5345f824522SMatthew G. Knepley 
535723f9013SMatthew G. Knepley .seealso: PCPatchGetComputeOperator(), PCPatchSetComputeFunction(), PCPatchSetDiscretisationInfo()
5365f824522SMatthew G. Knepley @*/
5374d04e9f1SPatrick Farrell PetscErrorCode PCPatchSetComputeOperator(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Mat, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
5384bbf5ea8SMatthew G. Knepley {
5394bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
5404bbf5ea8SMatthew G. Knepley 
5414bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
5424bbf5ea8SMatthew G. Knepley   patch->usercomputeop    = func;
543723f9013SMatthew G. Knepley   patch->usercomputeopctx = ctx;
5444bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5454bbf5ea8SMatthew G. Knepley }
5464bbf5ea8SMatthew G. Knepley 
5474bbf5ea8SMatthew G. Knepley /* On entry, ht contains the topological entities whose dofs we are responsible for solving for;
5484bbf5ea8SMatthew G. Knepley    on exit, cht contains all the topological entities we need to compute their residuals.
5494bbf5ea8SMatthew G. Knepley    In full generality this should incorporate knowledge of the sparsity pattern of the matrix;
5504bbf5ea8SMatthew G. Knepley    here we assume a standard FE sparsity pattern.*/
5514bbf5ea8SMatthew G. Knepley /* TODO: Use DMPlexGetAdjacency() */
5521b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchCompleteCellPatch(PC pc, PetscHSetI ht, PetscHSetI cht)
5534bbf5ea8SMatthew G. Knepley {
5545f824522SMatthew G. Knepley   DM             dm;
5551b68eb51SMatthew G. Knepley   PetscHashIter  hi;
5564bbf5ea8SMatthew G. Knepley   PetscInt       point;
5574bbf5ea8SMatthew G. Knepley   PetscInt      *star = NULL, *closure = NULL;
5584c954380SMatthew G. Knepley   PetscInt       ignoredim, iStart = 0, iEnd = -1, starSize, closureSize, si, ci;
5594bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
5604bbf5ea8SMatthew G. Knepley 
5614bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
5625f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
5635f824522SMatthew G. Knepley   ierr = PCPatchGetIgnoreDim(pc, &ignoredim);CHKERRQ(ierr);
5645f824522SMatthew G. Knepley   if (ignoredim >= 0) {ierr = DMPlexGetDepthStratum(dm, ignoredim, &iStart, &iEnd);CHKERRQ(ierr);}
5651b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(cht);CHKERRQ(ierr);
5661b68eb51SMatthew G. Knepley   PetscHashIterBegin(ht, hi);
5671b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(ht, hi)) {
5684c954380SMatthew G. Knepley 
5691b68eb51SMatthew G. Knepley     PetscHashIterGetKey(ht, hi, point);
5701b68eb51SMatthew G. Knepley     PetscHashIterNext(ht, hi);
5714bbf5ea8SMatthew G. Knepley 
5724bbf5ea8SMatthew G. Knepley     /* Loop over all the cells that this point connects to */
5734bbf5ea8SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
5745f824522SMatthew G. Knepley     for (si = 0; si < starSize*2; si += 2) {
5754c954380SMatthew G. Knepley       const PetscInt ownedpoint = star[si];
5765f824522SMatthew G. Knepley       /* TODO Check for point in cht before running through closure again */
5774bbf5ea8SMatthew G. Knepley       /* now loop over all entities in the closure of that cell */
5784bbf5ea8SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, ownedpoint, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
5795f824522SMatthew G. Knepley       for (ci = 0; ci < closureSize*2; ci += 2) {
5804c954380SMatthew G. Knepley         const PetscInt seenpoint = closure[ci];
5815f824522SMatthew G. Knepley         if (ignoredim >= 0 && seenpoint >= iStart && seenpoint < iEnd) continue;
5821b68eb51SMatthew G. Knepley         ierr = PetscHSetIAdd(cht, seenpoint);CHKERRQ(ierr);
5834bbf5ea8SMatthew G. Knepley       }
5844bbf5ea8SMatthew G. Knepley     }
5854bbf5ea8SMatthew G. Knepley   }
5864c954380SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, 0, PETSC_TRUE, NULL, &closure);CHKERRQ(ierr);
5875f824522SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, 0, PETSC_FALSE, NULL, &star);CHKERRQ(ierr);
5885f824522SMatthew G. Knepley   PetscFunctionReturn(0);
5895f824522SMatthew G. Knepley }
5905f824522SMatthew G. Knepley 
5915f824522SMatthew G. Knepley static PetscErrorCode PCPatchGetGlobalDofs(PC pc, PetscSection dofSection[], PetscInt f, PetscBool combined, PetscInt p, PetscInt *dof, PetscInt *off)
5925f824522SMatthew G. Knepley {
5935f824522SMatthew G. Knepley   PetscErrorCode ierr;
5945f824522SMatthew G. Knepley 
5955f824522SMatthew G. Knepley   PetscFunctionBegin;
5965f824522SMatthew G. Knepley   if (combined) {
5975f824522SMatthew G. Knepley     if (f < 0) {
5985f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetDof(dofSection[0], p, dof);CHKERRQ(ierr);}
5995f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetOffset(dofSection[0], p, off);CHKERRQ(ierr);}
6005f824522SMatthew G. Knepley     } else {
6015f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetFieldDof(dofSection[0], p, f, dof);CHKERRQ(ierr);}
6025f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetFieldOffset(dofSection[0], p, f, off);CHKERRQ(ierr);}
6035f824522SMatthew G. Knepley     }
6045f824522SMatthew G. Knepley   } else {
6055f824522SMatthew G. Knepley     if (f < 0) {
6065f824522SMatthew G. Knepley       PC_PATCH *patch = (PC_PATCH *) pc->data;
6075f824522SMatthew G. Knepley       PetscInt  fdof, g;
6085f824522SMatthew G. Knepley 
6095f824522SMatthew G. Knepley       if (dof) {
6105f824522SMatthew G. Knepley         *dof = 0;
6115f824522SMatthew G. Knepley         for (g = 0; g < patch->nsubspaces; ++g) {
6125f824522SMatthew G. Knepley           ierr = PetscSectionGetDof(dofSection[g], p, &fdof);CHKERRQ(ierr);
6135f824522SMatthew G. Knepley           *dof += fdof;
6145f824522SMatthew G. Knepley         }
6155f824522SMatthew G. Knepley       }
616624e31c3SLawrence Mitchell       if (off) {
617624e31c3SLawrence Mitchell         *off = 0;
618624e31c3SLawrence Mitchell         for (g = 0; g < patch->nsubspaces; ++g) {
619624e31c3SLawrence Mitchell           ierr = PetscSectionGetOffset(dofSection[g], p, &fdof);CHKERRQ(ierr);
620624e31c3SLawrence Mitchell           *off += fdof;
621624e31c3SLawrence Mitchell         }
622624e31c3SLawrence Mitchell       }
6235f824522SMatthew G. Knepley     } else {
6245f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetDof(dofSection[f], p, dof);CHKERRQ(ierr);}
6255f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetOffset(dofSection[f], p, off);CHKERRQ(ierr);}
6265f824522SMatthew G. Knepley     }
6275f824522SMatthew G. Knepley   }
6284bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
6294bbf5ea8SMatthew G. Knepley }
6304bbf5ea8SMatthew G. Knepley 
6314bbf5ea8SMatthew G. Knepley /* Given a hash table with a set of topological entities (pts), compute the degrees of
6324bbf5ea8SMatthew G. Knepley    freedom in global concatenated numbering on those entities.
6334bbf5ea8SMatthew G. Knepley    For Vanka smoothing, this needs to do something special: ignore dofs of the
6344bbf5ea8SMatthew G. Knepley    constraint subspace on entities that aren't the base entity we're building the patch
6354bbf5ea8SMatthew G. Knepley    around. */
636e4c66b91SPatrick Farrell static PetscErrorCode PCPatchGetPointDofs(PC pc, PetscHSetI pts, PetscHSetI dofs, PetscInt base, PetscHSetI* subspaces_to_exclude)
6374bbf5ea8SMatthew G. Knepley {
6385f824522SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
6391b68eb51SMatthew G. Knepley   PetscHashIter  hi;
6404bbf5ea8SMatthew G. Knepley   PetscInt       ldof, loff;
6414bbf5ea8SMatthew G. Knepley   PetscInt       k, p;
6424bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
6434bbf5ea8SMatthew G. Knepley 
6444bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
6451b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(dofs);CHKERRQ(ierr);
6464bbf5ea8SMatthew G. Knepley   for (k = 0; k < patch->nsubspaces; ++k) {
6474bbf5ea8SMatthew G. Knepley     PetscInt subspaceOffset = patch->subspaceOffsets[k];
6484bbf5ea8SMatthew G. Knepley     PetscInt bs             = patch->bs[k];
6494bbf5ea8SMatthew G. Knepley     PetscInt j, l;
6504bbf5ea8SMatthew G. Knepley 
651e4c66b91SPatrick Farrell     if (subspaces_to_exclude != NULL) {
652e4c66b91SPatrick Farrell       PetscBool should_exclude_k = PETSC_FALSE;
653e4c66b91SPatrick Farrell       PetscHSetIHas(*subspaces_to_exclude, k, &should_exclude_k);
654e4c66b91SPatrick Farrell       if (should_exclude_k) {
6554bbf5ea8SMatthew G. Knepley         /* only get this subspace dofs at the base entity, not any others */
6565f824522SMatthew G. Knepley         ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, base, &ldof, &loff);CHKERRQ(ierr);
6574bbf5ea8SMatthew G. Knepley         if (0 == ldof) continue;
6584bbf5ea8SMatthew G. Knepley         for (j = loff; j < ldof + loff; ++j) {
6594bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
6604bbf5ea8SMatthew G. Knepley             PetscInt dof = bs*j + l + subspaceOffset;
6611b68eb51SMatthew G. Knepley             ierr = PetscHSetIAdd(dofs, dof);CHKERRQ(ierr);
6624bbf5ea8SMatthew G. Knepley           }
6634bbf5ea8SMatthew G. Knepley         }
6644bbf5ea8SMatthew G. Knepley         continue; /* skip the other dofs of this subspace */
6654bbf5ea8SMatthew G. Knepley       }
666e4c66b91SPatrick Farrell     }
6674bbf5ea8SMatthew G. Knepley 
6681b68eb51SMatthew G. Knepley     PetscHashIterBegin(pts, hi);
6691b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(pts, hi)) {
6701b68eb51SMatthew G. Knepley       PetscHashIterGetKey(pts, hi, p);
6711b68eb51SMatthew G. Knepley       PetscHashIterNext(pts, hi);
6725f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, p, &ldof, &loff);CHKERRQ(ierr);
6734bbf5ea8SMatthew G. Knepley       if (0 == ldof) continue;
6744bbf5ea8SMatthew G. Knepley       for (j = loff; j < ldof + loff; ++j) {
6754bbf5ea8SMatthew G. Knepley         for (l = 0; l < bs; ++l) {
6764bbf5ea8SMatthew G. Knepley           PetscInt dof = bs*j + l + subspaceOffset;
6771b68eb51SMatthew G. Knepley           ierr = PetscHSetIAdd(dofs, dof);CHKERRQ(ierr);
6784bbf5ea8SMatthew G. Knepley         }
6794bbf5ea8SMatthew G. Knepley       }
6804bbf5ea8SMatthew G. Knepley     }
6814bbf5ea8SMatthew G. Knepley   }
6824bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
6834bbf5ea8SMatthew G. Knepley }
6844bbf5ea8SMatthew G. Knepley 
6854bbf5ea8SMatthew G. Knepley /* Given two hash tables A and B, compute the keys in B that are not in A, and put them in C */
6861b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchComputeSetDifference_Private(PetscHSetI A, PetscHSetI B, PetscHSetI C)
6874bbf5ea8SMatthew G. Knepley {
6881b68eb51SMatthew G. Knepley   PetscHashIter  hi;
6891b68eb51SMatthew G. Knepley   PetscInt       key;
6904bbf5ea8SMatthew G. Knepley   PetscBool      flg;
6911b68eb51SMatthew G. Knepley   PetscErrorCode ierr;
6924bbf5ea8SMatthew G. Knepley 
6934bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
6941b68eb51SMatthew G. Knepley   ierr = PetscHSetIClear(C);CHKERRQ(ierr);
6951b68eb51SMatthew G. Knepley   PetscHashIterBegin(B, hi);
6961b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(B, hi)) {
6971b68eb51SMatthew G. Knepley     PetscHashIterGetKey(B, hi, key);
6981b68eb51SMatthew G. Knepley     PetscHashIterNext(B, hi);
6991b68eb51SMatthew G. Knepley     ierr = PetscHSetIHas(A, key, &flg);CHKERRQ(ierr);
7001b68eb51SMatthew G. Knepley     if (!flg) {ierr = PetscHSetIAdd(C, key);CHKERRQ(ierr);}
7014bbf5ea8SMatthew G. Knepley   }
7024bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
7034bbf5ea8SMatthew G. Knepley }
7044bbf5ea8SMatthew G. Knepley 
7054bbf5ea8SMatthew G. Knepley /*
7064bbf5ea8SMatthew G. Knepley  * PCPatchCreateCellPatches - create patches.
7074bbf5ea8SMatthew G. Knepley  *
7084bbf5ea8SMatthew G. Knepley  * Input Parameters:
7094bbf5ea8SMatthew G. Knepley  * + dm - The DMPlex object defining the mesh
7104bbf5ea8SMatthew G. Knepley  *
7114bbf5ea8SMatthew G. Knepley  * Output Parameters:
7124bbf5ea8SMatthew G. Knepley  * + cellCounts  - Section with counts of cells around each vertex
7135f824522SMatthew G. Knepley  * . cells       - IS of the cell point indices of cells in each patch
7145f824522SMatthew G. Knepley  * . pointCounts - Section with counts of cells around each vertex
7155f824522SMatthew G. Knepley  * - point       - IS of the cell point indices of cells in each patch
7164bbf5ea8SMatthew G. Knepley  */
7174bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatches(PC pc)
7184bbf5ea8SMatthew G. Knepley {
7194bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
7205f824522SMatthew G. Knepley   DMLabel         ghost = NULL;
7214bbf5ea8SMatthew G. Knepley   DM              dm, plex;
7221b68eb51SMatthew G. Knepley   PetscHSetI      ht, cht;
7235f824522SMatthew G. Knepley   PetscSection    cellCounts,  pointCounts;
7245f824522SMatthew G. Knepley   PetscInt       *cellsArray, *pointsArray;
7255f824522SMatthew G. Knepley   PetscInt        numCells,    numPoints;
7265f824522SMatthew G. Knepley   const PetscInt *leaves;
7275f824522SMatthew G. Knepley   PetscInt        nleaves, pStart, pEnd, cStart, cEnd, vStart, vEnd, v;
7285f824522SMatthew G. Knepley   PetscBool       isFiredrake;
7294bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
7304bbf5ea8SMatthew G. Knepley 
7314bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
7324bbf5ea8SMatthew G. Knepley   /* Used to keep track of the cells in the patch. */
7331b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&ht);CHKERRQ(ierr);
7341b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&cht);CHKERRQ(ierr);
7354bbf5ea8SMatthew G. Knepley 
7364bbf5ea8SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
7374bbf5ea8SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONGSTATE, "DM not yet set on patch PC\n");
7384bbf5ea8SMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
7394bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetChart(plex, &pStart, &pEnd);CHKERRQ(ierr);
7404bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr);
7414bbf5ea8SMatthew G. Knepley 
7424bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
7435f824522SMatthew G. Knepley     ierr = patch->userpatchconstructionop(pc, &patch->npatch, &patch->userIS, &patch->iterationSet, patch->userpatchconstructctx);CHKERRQ(ierr);
7445f824522SMatthew G. Knepley     vStart = 0; vEnd = patch->npatch;
7455f824522SMatthew G. Knepley   } else if (patch->codim < 0) {
7465f824522SMatthew G. Knepley     if (patch->dim < 0) {ierr = DMPlexGetDepthStratum(plex,  0,            &vStart, &vEnd);CHKERRQ(ierr);}
7475f824522SMatthew G. Knepley     else                {ierr = DMPlexGetDepthStratum(plex,  patch->dim,   &vStart, &vEnd);CHKERRQ(ierr);}
7485f824522SMatthew G. Knepley   } else                {ierr = DMPlexGetHeightStratum(plex, patch->codim, &vStart, &vEnd);CHKERRQ(ierr);}
7495f824522SMatthew G. Knepley   patch->npatch = vEnd - vStart;
7504bbf5ea8SMatthew G. Knepley 
7514bbf5ea8SMatthew G. Knepley   /* These labels mark the owned points.  We only create patches around points that this process owns. */
7525f824522SMatthew G. Knepley   ierr = DMHasLabel(dm, "pyop2_ghost", &isFiredrake);CHKERRQ(ierr);
7535f824522SMatthew G. Knepley   if (isFiredrake) {
7544bbf5ea8SMatthew G. Knepley     ierr = DMGetLabel(dm, "pyop2_ghost", &ghost);CHKERRQ(ierr);
7554bbf5ea8SMatthew G. Knepley     ierr = DMLabelCreateIndex(ghost, pStart, pEnd);CHKERRQ(ierr);
7565f824522SMatthew G. Knepley   } else {
7575f824522SMatthew G. Knepley     PetscSF sf;
7585f824522SMatthew G. Knepley 
7595f824522SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
7605f824522SMatthew G. Knepley     ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
7615f824522SMatthew G. Knepley     nleaves = PetscMax(nleaves, 0);
7625f824522SMatthew G. Knepley   }
7634bbf5ea8SMatthew G. Knepley 
7644bbf5ea8SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->cellCounts);CHKERRQ(ierr);
7655f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->cellCounts, "Patch Cell Layout");CHKERRQ(ierr);
7664bbf5ea8SMatthew G. Knepley   cellCounts = patch->cellCounts;
7674bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetChart(cellCounts, vStart, vEnd);CHKERRQ(ierr);
7685f824522SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->pointCounts);CHKERRQ(ierr);
7695f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->pointCounts, "Patch Point Layout");CHKERRQ(ierr);
7705f824522SMatthew G. Knepley   pointCounts = patch->pointCounts;
7715f824522SMatthew G. Knepley   ierr = PetscSectionSetChart(pointCounts, vStart, vEnd);CHKERRQ(ierr);
7725f824522SMatthew G. Knepley   /* Count cells and points in the patch surrounding each entity */
7734bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
7741b68eb51SMatthew G. Knepley     PetscHashIter hi;
7755f824522SMatthew G. Knepley     PetscInt       chtSize, loc = -1;
7765f824522SMatthew G. Knepley     PetscBool      flg;
7774bbf5ea8SMatthew G. Knepley 
7784bbf5ea8SMatthew G. Knepley     if (!patch->user_patches) {
7795f824522SMatthew G. Knepley       if (ghost) {ierr = DMLabelHasPoint(ghost, v, &flg);CHKERRQ(ierr);}
780928bb9adSStefano Zampini       else       {ierr = PetscFindInt(v, nleaves, leaves, &loc);CHKERRQ(ierr); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;}
7814bbf5ea8SMatthew G. Knepley       /* Not an owned entity, don't make a cell patch. */
7824bbf5ea8SMatthew G. Knepley       if (flg) continue;
7834bbf5ea8SMatthew G. Knepley     }
7844bbf5ea8SMatthew G. Knepley 
7854bbf5ea8SMatthew G. Knepley     ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr);
7865f824522SMatthew G. Knepley     ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr);
7871b68eb51SMatthew G. Knepley     ierr = PetscHSetIGetSize(cht, &chtSize);CHKERRQ(ierr);
7884bbf5ea8SMatthew G. Knepley     /* empty patch, continue */
7894bbf5ea8SMatthew G. Knepley     if (chtSize == 0) continue;
7904bbf5ea8SMatthew G. Knepley 
7914bbf5ea8SMatthew G. Knepley     /* safe because size(cht) > 0 from above */
7921b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
7931b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
7945f824522SMatthew G. Knepley       PetscInt point, pdof;
7954bbf5ea8SMatthew G. Knepley 
7961b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
7975f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr);
7985f824522SMatthew G. Knepley       if (pdof)                            {ierr = PetscSectionAddDof(pointCounts, v, 1);CHKERRQ(ierr);}
7995f824522SMatthew G. Knepley       if (point >= cStart && point < cEnd) {ierr = PetscSectionAddDof(cellCounts, v, 1);CHKERRQ(ierr);}
8001b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
8014bbf5ea8SMatthew G. Knepley     }
8024bbf5ea8SMatthew G. Knepley   }
8035f824522SMatthew G. Knepley   if (isFiredrake) {ierr = DMLabelDestroyIndex(ghost);CHKERRQ(ierr);}
8044bbf5ea8SMatthew G. Knepley 
8054bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetUp(cellCounts);CHKERRQ(ierr);
8064bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr);
8074bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numCells, &cellsArray);CHKERRQ(ierr);
8085f824522SMatthew G. Knepley   ierr = PetscSectionSetUp(pointCounts);CHKERRQ(ierr);
8095f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(pointCounts, &numPoints);CHKERRQ(ierr);
8105f824522SMatthew G. Knepley   ierr = PetscMalloc1(numPoints, &pointsArray);CHKERRQ(ierr);
8114bbf5ea8SMatthew G. Knepley 
8124bbf5ea8SMatthew G. Knepley   /* Now that we know how much space we need, run through again and actually remember the cells. */
8134bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; v++ ) {
8141b68eb51SMatthew G. Knepley     PetscHashIter hi;
8155f824522SMatthew G. Knepley     PetscInt       dof, off, cdof, coff, pdof, n = 0, cn = 0;
8164bbf5ea8SMatthew G. Knepley 
8175f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(pointCounts, v, &dof);CHKERRQ(ierr);
8185f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(pointCounts, v, &off);CHKERRQ(ierr);
8195f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &cdof);CHKERRQ(ierr);
8205f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &coff);CHKERRQ(ierr);
8215f824522SMatthew G. Knepley     if (dof <= 0) continue;
8224bbf5ea8SMatthew G. Knepley     ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr);
8235f824522SMatthew G. Knepley     ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr);
8241b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
8251b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
8264bbf5ea8SMatthew G. Knepley       PetscInt point;
8274bbf5ea8SMatthew G. Knepley 
8281b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
8295f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr);
8305f824522SMatthew G. Knepley       if (pdof)                            {pointsArray[off + n++] = point;}
8315f824522SMatthew G. Knepley       if (point >= cStart && point < cEnd) {cellsArray[coff + cn++] = point;}
8321b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
8334bbf5ea8SMatthew G. Knepley     }
8345f824522SMatthew 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);
8355f824522SMatthew 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);
8364bbf5ea8SMatthew G. Knepley   }
8371b68eb51SMatthew G. Knepley   ierr = PetscHSetIDestroy(&ht);CHKERRQ(ierr);
8381b68eb51SMatthew G. Knepley   ierr = PetscHSetIDestroy(&cht);CHKERRQ(ierr);
8394bbf5ea8SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
8405f824522SMatthew G. Knepley 
8415f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numCells,  cellsArray,  PETSC_OWN_POINTER, &patch->cells);CHKERRQ(ierr);
8425f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->cells,  "Patch Cells");CHKERRQ(ierr);
8435f824522SMatthew G. Knepley   if (patch->viewCells) {
8445f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->cellCounts, patch->viewerCells, patch->formatCells);CHKERRQ(ierr);
8455f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->cells,      patch->viewerCells, patch->formatCells);CHKERRQ(ierr);
8465f824522SMatthew G. Knepley   }
8475f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints, pointsArray, PETSC_OWN_POINTER, &patch->points);CHKERRQ(ierr);
8485f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->points, "Patch Points");CHKERRQ(ierr);
8495f824522SMatthew G. Knepley   if (patch->viewPoints) {
8505f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->pointCounts, patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr);
8515f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->points,      patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr);
8525f824522SMatthew G. Knepley   }
8534bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
8544bbf5ea8SMatthew G. Knepley }
8554bbf5ea8SMatthew G. Knepley 
8564bbf5ea8SMatthew G. Knepley /*
8574bbf5ea8SMatthew G. Knepley  * PCPatchCreateCellPatchDiscretisationInfo - Build the dof maps for cell patches
8584bbf5ea8SMatthew G. Knepley  *
8594bbf5ea8SMatthew G. Knepley  * Input Parameters:
8604bbf5ea8SMatthew G. Knepley  * + dm - The DMPlex object defining the mesh
8614bbf5ea8SMatthew G. Knepley  * . cellCounts - Section with counts of cells around each vertex
8624bbf5ea8SMatthew G. Knepley  * . cells - IS of the cell point indices of cells in each patch
8634bbf5ea8SMatthew G. Knepley  * . cellNumbering - Section mapping plex cell points to Firedrake cell indices.
8644bbf5ea8SMatthew G. Knepley  * . nodesPerCell - number of nodes per cell.
8654bbf5ea8SMatthew G. Knepley  * - cellNodeMap - map from cells to node indices (nodesPerCell * numCells)
8664bbf5ea8SMatthew G. Knepley  *
8674bbf5ea8SMatthew G. Knepley  * Output Parameters:
8685f824522SMatthew G. Knepley  * + dofs - IS of local dof numbers of each cell in the patch, where local is a patch local numbering
8694bbf5ea8SMatthew G. Knepley  * . gtolCounts - Section with counts of dofs per cell patch
8704bbf5ea8SMatthew G. Knepley  * - gtol - IS mapping from global dofs to local dofs for each patch.
8714bbf5ea8SMatthew G. Knepley  */
8724bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatchDiscretisationInfo(PC pc)
8734bbf5ea8SMatthew G. Knepley {
8744bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch           = (PC_PATCH *) pc->data;
8754bbf5ea8SMatthew G. Knepley   PetscSection    cellCounts      = patch->cellCounts;
8765f824522SMatthew G. Knepley   PetscSection    pointCounts     = patch->pointCounts;
877*0904074fSPatrick Farrell   PetscSection    gtolCounts, gtolCountsWithArtificial = NULL, gtolCountsWithAll = NULL;
8784bbf5ea8SMatthew G. Knepley   IS              cells           = patch->cells;
8795f824522SMatthew G. Knepley   IS              points          = patch->points;
8804bbf5ea8SMatthew G. Knepley   PetscSection    cellNumbering   = patch->cellNumbering;
8815f824522SMatthew G. Knepley   PetscInt        Nf              = patch->nsubspaces;
8825f824522SMatthew G. Knepley   PetscInt        numCells, numPoints;
8834bbf5ea8SMatthew G. Knepley   PetscInt        numDofs;
884*0904074fSPatrick Farrell   PetscInt        numGlobalDofs, numGlobalDofsWithArtificial, numGlobalDofsWithAll;
8854bbf5ea8SMatthew G. Knepley   PetscInt        totalDofsPerCell = patch->totalDofsPerCell;
8864bbf5ea8SMatthew G. Knepley   PetscInt        vStart, vEnd, v;
8875f824522SMatthew G. Knepley   const PetscInt *cellsArray, *pointsArray;
8884bbf5ea8SMatthew G. Knepley   PetscInt       *newCellsArray   = NULL;
8894bbf5ea8SMatthew G. Knepley   PetscInt       *dofsArray       = NULL;
890c2e6f3c0SFlorian Wechsung   PetscInt       *dofsArrayWithArtificial = NULL;
891*0904074fSPatrick Farrell   PetscInt       *dofsArrayWithAll = NULL;
8925f824522SMatthew G. Knepley   PetscInt       *offsArray       = NULL;
893c2e6f3c0SFlorian Wechsung   PetscInt       *offsArrayWithArtificial = NULL;
894*0904074fSPatrick Farrell   PetscInt       *offsArrayWithAll = NULL;
8954bbf5ea8SMatthew G. Knepley   PetscInt       *asmArray        = NULL;
896c2e6f3c0SFlorian Wechsung   PetscInt       *asmArrayWithArtificial = NULL;
897*0904074fSPatrick Farrell   PetscInt       *asmArrayWithAll = NULL;
8984bbf5ea8SMatthew G. Knepley   PetscInt       *globalDofsArray = NULL;
899c2e6f3c0SFlorian Wechsung   PetscInt       *globalDofsArrayWithArtificial = NULL;
900*0904074fSPatrick Farrell   PetscInt       *globalDofsArrayWithAll = NULL;
9014bbf5ea8SMatthew G. Knepley   PetscInt        globalIndex     = 0;
9024bbf5ea8SMatthew G. Knepley   PetscInt        key             = 0;
9034bbf5ea8SMatthew G. Knepley   PetscInt        asmKey          = 0;
904557beb66SLawrence Mitchell   DM              dm              = NULL;
905557beb66SLawrence Mitchell   const PetscInt *bcNodes         = NULL;
9061b68eb51SMatthew G. Knepley   PetscHMapI      ht;
907c2e6f3c0SFlorian Wechsung   PetscHMapI      htWithArtificial;
908*0904074fSPatrick Farrell   PetscHMapI      htWithAll;
9091b68eb51SMatthew G. Knepley   PetscHSetI      globalBcs;
910557beb66SLawrence Mitchell   PetscInt        numBcs;
9111b68eb51SMatthew G. Knepley   PetscHSetI      ownedpts, seenpts, owneddofs, seendofs, artificialbcs;
912cda239d9SMatthew G. Knepley   PetscInt        pStart, pEnd, p, i;
91310534d48SPatrick Farrell   char            option[PETSC_MAX_PATH_LEN];
91439fd2e8aSPatrick Farrell   PetscBool       isNonlinear;
9154bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
9164bbf5ea8SMatthew G. Knepley 
9174bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
918557beb66SLawrence Mitchell 
919557beb66SLawrence Mitchell   ierr = PCGetDM(pc, &dm); CHKERRQ(ierr);
9204bbf5ea8SMatthew G. Knepley   /* dofcounts section is cellcounts section * dofPerCell */
9214bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr);
9225f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(patch->pointCounts, &numPoints);CHKERRQ(ierr);
9234bbf5ea8SMatthew G. Knepley   numDofs = numCells * totalDofsPerCell;
9244bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numDofs, &dofsArray);CHKERRQ(ierr);
9255f824522SMatthew G. Knepley   ierr = PetscMalloc1(numPoints*Nf, &offsArray);CHKERRQ(ierr);
9264bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numDofs, &asmArray);CHKERRQ(ierr);
9274bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numCells, &newCellsArray);CHKERRQ(ierr);
9284bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(cellCounts, &vStart, &vEnd);CHKERRQ(ierr);
9294bbf5ea8SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCounts);CHKERRQ(ierr);
9304bbf5ea8SMatthew G. Knepley   gtolCounts = patch->gtolCounts;
9314bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetChart(gtolCounts, vStart, vEnd);CHKERRQ(ierr);
9325f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->gtolCounts, "Patch Global Index Section");CHKERRQ(ierr);
9334bbf5ea8SMatthew G. Knepley 
934*0904074fSPatrick Farrell   if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE)
935c2e6f3c0SFlorian Wechsung   {
936f0dcb611SKarl Rupp     ierr = PetscMalloc1(numPoints*Nf, &offsArrayWithArtificial);CHKERRQ(ierr);
937c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numDofs, &asmArrayWithArtificial);CHKERRQ(ierr);
938c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numDofs, &dofsArrayWithArtificial);CHKERRQ(ierr);
939c2e6f3c0SFlorian Wechsung     ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithArtificial);CHKERRQ(ierr);
940c2e6f3c0SFlorian Wechsung     gtolCountsWithArtificial = patch->gtolCountsWithArtificial;
941c2e6f3c0SFlorian Wechsung     ierr = PetscSectionSetChart(gtolCountsWithArtificial, vStart, vEnd);CHKERRQ(ierr);
942c2e6f3c0SFlorian Wechsung     ierr = PetscObjectSetName((PetscObject) patch->gtolCountsWithArtificial, "Patch Global Index Section Including Artificial BCs");CHKERRQ(ierr);
943c2e6f3c0SFlorian Wechsung   }
944c2e6f3c0SFlorian Wechsung 
945*0904074fSPatrick Farrell   isNonlinear = patch->isNonlinear;
946*0904074fSPatrick Farrell   if(isNonlinear)
947*0904074fSPatrick Farrell   {
948*0904074fSPatrick Farrell     ierr = PetscMalloc1(numPoints*Nf, &offsArrayWithAll);CHKERRQ(ierr);
949*0904074fSPatrick Farrell     ierr = PetscMalloc1(numDofs, &asmArrayWithAll);CHKERRQ(ierr);
950*0904074fSPatrick Farrell     ierr = PetscMalloc1(numDofs, &dofsArrayWithAll);CHKERRQ(ierr);
951*0904074fSPatrick Farrell     ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithAll);CHKERRQ(ierr);
952*0904074fSPatrick Farrell     gtolCountsWithAll = patch->gtolCountsWithAll;
953*0904074fSPatrick Farrell     ierr = PetscSectionSetChart(gtolCountsWithAll, vStart, vEnd);CHKERRQ(ierr);
954*0904074fSPatrick Farrell     ierr = PetscObjectSetName((PetscObject) patch->gtolCountsWithAll, "Patch Global Index Section Including All BCs");CHKERRQ(ierr);
955*0904074fSPatrick Farrell   }
956*0904074fSPatrick Farrell 
957557beb66SLawrence Mitchell   /* Outside the patch loop, get the dofs that are globally-enforced Dirichlet
958557beb66SLawrence Mitchell    conditions */
9591b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&globalBcs);CHKERRQ(ierr);
960557beb66SLawrence Mitchell   ierr = ISGetIndices(patch->ghostBcNodes, &bcNodes); CHKERRQ(ierr);
961557beb66SLawrence Mitchell   ierr = ISGetSize(patch->ghostBcNodes, &numBcs); CHKERRQ(ierr);
962cda239d9SMatthew G. Knepley   for (i = 0; i < numBcs; ++i) {
9631b68eb51SMatthew G. Knepley     ierr = PetscHSetIAdd(globalBcs, bcNodes[i]);CHKERRQ(ierr); /* these are already in concatenated numbering */
964557beb66SLawrence Mitchell   }
965557beb66SLawrence Mitchell   ierr = ISRestoreIndices(patch->ghostBcNodes, &bcNodes); CHKERRQ(ierr);
966557beb66SLawrence Mitchell   ierr = ISDestroy(&patch->ghostBcNodes); CHKERRQ(ierr); /* memory optimisation */
967557beb66SLawrence Mitchell 
968557beb66SLawrence Mitchell   /* Hash tables for artificial BC construction */
9691b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&ownedpts);CHKERRQ(ierr);
9701b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&seenpts);CHKERRQ(ierr);
9711b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&owneddofs);CHKERRQ(ierr);
9721b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&seendofs);CHKERRQ(ierr);
9731b68eb51SMatthew G. Knepley   ierr = PetscHSetICreate(&artificialbcs);CHKERRQ(ierr);
974557beb66SLawrence Mitchell 
9754bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(cells, &cellsArray);CHKERRQ(ierr);
9765f824522SMatthew G. Knepley   ierr = ISGetIndices(points, &pointsArray);CHKERRQ(ierr);
9771b68eb51SMatthew G. Knepley   ierr = PetscHMapICreate(&ht);CHKERRQ(ierr);
978c2e6f3c0SFlorian Wechsung   ierr = PetscHMapICreate(&htWithArtificial);CHKERRQ(ierr);
979*0904074fSPatrick Farrell   ierr = PetscHMapICreate(&htWithAll);CHKERRQ(ierr);
9804bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
9814bbf5ea8SMatthew G. Knepley     PetscInt localIndex = 0;
982c2e6f3c0SFlorian Wechsung     PetscInt localIndexWithArtificial = 0;
983*0904074fSPatrick Farrell     PetscInt localIndexWithAll = 0;
9844bbf5ea8SMatthew G. Knepley     PetscInt dof, off, i, j, k, l;
9854bbf5ea8SMatthew G. Knepley 
9861b68eb51SMatthew G. Knepley     ierr = PetscHMapIClear(ht);CHKERRQ(ierr);
987c2e6f3c0SFlorian Wechsung     ierr = PetscHMapIClear(htWithArtificial);CHKERRQ(ierr);
988*0904074fSPatrick Farrell     ierr = PetscHMapIClear(htWithAll);CHKERRQ(ierr);
9894bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr);
9904bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr);
9914bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
9924bbf5ea8SMatthew G. Knepley 
993557beb66SLawrence Mitchell     /* Calculate the global numbers of the artificial BC dofs here first */
994557beb66SLawrence Mitchell     ierr = patch->patchconstructop((void*)patch, dm, v, ownedpts); CHKERRQ(ierr);
995557beb66SLawrence Mitchell     ierr = PCPatchCompleteCellPatch(pc, ownedpts, seenpts); CHKERRQ(ierr);
996e4c66b91SPatrick Farrell     ierr = PCPatchGetPointDofs(pc, ownedpts, owneddofs, v, &patch->subspaces_to_exclude); CHKERRQ(ierr);
997e4c66b91SPatrick Farrell     ierr = PCPatchGetPointDofs(pc, seenpts, seendofs, v, NULL); CHKERRQ(ierr);
998557beb66SLawrence Mitchell     ierr = PCPatchComputeSetDifference_Private(owneddofs, seendofs, artificialbcs); CHKERRQ(ierr);
9998135ed82SLawrence Mitchell     if (patch->viewPatches) {
10001b68eb51SMatthew G. Knepley       PetscHSetI globalbcdofs;
10011b68eb51SMatthew G. Knepley       PetscHashIter hi;
10028135ed82SLawrence Mitchell       MPI_Comm comm = PetscObjectComm((PetscObject)pc);
10031b68eb51SMatthew G. Knepley 
10041b68eb51SMatthew G. Knepley       ierr = PetscHSetICreate(&globalbcdofs);CHKERRQ(ierr);
10058135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: owned dofs:\n", v); CHKERRQ(ierr);
10061b68eb51SMatthew G. Knepley       PetscHashIterBegin(owneddofs, hi);
10071b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(owneddofs, hi)) {
10088135ed82SLawrence Mitchell         PetscInt globalDof;
10098135ed82SLawrence Mitchell 
10101b68eb51SMatthew G. Knepley         PetscHashIterGetKey(owneddofs, hi, globalDof);
10111b68eb51SMatthew G. Knepley         PetscHashIterNext(owneddofs, hi);
10128135ed82SLawrence Mitchell         ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
10138135ed82SLawrence Mitchell       }
10148135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n"); CHKERRQ(ierr);
10158135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: seen dofs:\n", v); CHKERRQ(ierr);
10161b68eb51SMatthew G. Knepley       PetscHashIterBegin(seendofs, hi);
10171b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(seendofs, hi)) {
10188135ed82SLawrence Mitchell         PetscInt globalDof;
10198135ed82SLawrence Mitchell         PetscBool flg;
10208135ed82SLawrence Mitchell 
10211b68eb51SMatthew G. Knepley         PetscHashIterGetKey(seendofs, hi, globalDof);
10221b68eb51SMatthew G. Knepley         PetscHashIterNext(seendofs, hi);
10238135ed82SLawrence Mitchell         ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
10248135ed82SLawrence Mitchell 
10251b68eb51SMatthew G. Knepley         ierr = PetscHSetIHas(globalBcs, globalDof, &flg);CHKERRQ(ierr);
10261b68eb51SMatthew G. Knepley         if (flg) {ierr = PetscHSetIAdd(globalbcdofs, globalDof);CHKERRQ(ierr);}
10278135ed82SLawrence Mitchell       }
10288135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n"); CHKERRQ(ierr);
10298135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: global BCs:\n", v); CHKERRQ(ierr);
10301b68eb51SMatthew G. Knepley       ierr = PetscHSetIGetSize(globalbcdofs, &numBcs);CHKERRQ(ierr);
10318135ed82SLawrence Mitchell       if (numBcs > 0) {
10321b68eb51SMatthew G. Knepley         PetscHashIterBegin(globalbcdofs, hi);
10331b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(globalbcdofs, hi)) {
10348135ed82SLawrence Mitchell           PetscInt globalDof;
10351b68eb51SMatthew G. Knepley           PetscHashIterGetKey(globalbcdofs, hi, globalDof);
10361b68eb51SMatthew G. Knepley           PetscHashIterNext(globalbcdofs, hi);
10378135ed82SLawrence Mitchell           ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof);CHKERRQ(ierr);
10388135ed82SLawrence Mitchell         }
10398135ed82SLawrence Mitchell       }
10408135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n");CHKERRQ(ierr);
10418135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "Patch %d: artificial BCs:\n", v);CHKERRQ(ierr);
10421b68eb51SMatthew G. Knepley       ierr = PetscHSetIGetSize(artificialbcs, &numBcs);CHKERRQ(ierr);
10438135ed82SLawrence Mitchell       if (numBcs > 0) {
10441b68eb51SMatthew G. Knepley         PetscHashIterBegin(artificialbcs, hi);
10451b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(artificialbcs, hi)) {
10468135ed82SLawrence Mitchell           PetscInt globalDof;
10471b68eb51SMatthew G. Knepley           PetscHashIterGetKey(artificialbcs, hi, globalDof);
10481b68eb51SMatthew G. Knepley           PetscHashIterNext(artificialbcs, hi);
10498135ed82SLawrence Mitchell           ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
10508135ed82SLawrence Mitchell         }
10518135ed82SLawrence Mitchell       }
10528135ed82SLawrence Mitchell       ierr = PetscSynchronizedPrintf(comm, "\n\n"); CHKERRQ(ierr);
10531b68eb51SMatthew G. Knepley       ierr = PetscHSetIDestroy(&globalbcdofs);CHKERRQ(ierr);
10548135ed82SLawrence Mitchell     }
10554bbf5ea8SMatthew G. Knepley    for (k = 0; k < patch->nsubspaces; ++k) {
10564bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
10574bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
10584bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
10594bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
10604bbf5ea8SMatthew G. Knepley 
10614bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
10624bbf5ea8SMatthew G. Knepley         /* Walk over the cells in this patch. */
10634bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
10645f824522SMatthew G. Knepley         PetscInt       cell = c;
10654bbf5ea8SMatthew G. Knepley 
10665f824522SMatthew G. Knepley         /* TODO Change this to an IS */
10675f824522SMatthew G. Knepley         if (cellNumbering) {
10684bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetDof(cellNumbering, c, &cell);CHKERRQ(ierr);
10694bbf5ea8SMatthew G. Knepley           if (cell <= 0) SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_OUTOFRANGE, "Cell %D doesn't appear in cell numbering map", c);
10704bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);
10715f824522SMatthew G. Knepley         }
10724bbf5ea8SMatthew G. Knepley         newCellsArray[i] = cell;
10734bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
10744bbf5ea8SMatthew G. Knepley           /* For each global dof, map it into contiguous local storage. */
10754bbf5ea8SMatthew G. Knepley           const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + subspaceOffset;
10764bbf5ea8SMatthew G. Knepley           /* finally, loop over block size */
10774bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
10781b68eb51SMatthew G. Knepley             PetscInt  localDof;
10791b68eb51SMatthew G. Knepley             PetscBool isGlobalBcDof, isArtificialBcDof;
10804bbf5ea8SMatthew G. Knepley 
1081557beb66SLawrence Mitchell             /* first, check if this is either a globally enforced or locally enforced BC dof */
10821b68eb51SMatthew G. Knepley             ierr = PetscHSetIHas(globalBcs, globalDof + l, &isGlobalBcDof);CHKERRQ(ierr);
10831b68eb51SMatthew G. Knepley             ierr = PetscHSetIHas(artificialbcs, globalDof + l, &isArtificialBcDof);CHKERRQ(ierr);
1084557beb66SLawrence Mitchell 
1085557beb66SLawrence Mitchell             /* if it's either, don't ever give it a local dof number */
10861b68eb51SMatthew G. Knepley             if (isGlobalBcDof || isArtificialBcDof) {
1087c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = -1; /* don't use this in assembly in this patch */
1088557beb66SLawrence Mitchell             } else {
10891b68eb51SMatthew G. Knepley               ierr = PetscHMapIGet(ht, globalDof + l, &localDof);CHKERRQ(ierr);
10904bbf5ea8SMatthew G. Knepley               if (localDof == -1) {
10914bbf5ea8SMatthew G. Knepley                 localDof = localIndex++;
10921b68eb51SMatthew G. Knepley                 ierr = PetscHMapISet(ht, globalDof + l, localDof);CHKERRQ(ierr);
10934bbf5ea8SMatthew G. Knepley               }
10944bbf5ea8SMatthew G. Knepley               if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
10954bbf5ea8SMatthew G. Knepley               /* And store. */
1096c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = localDof;
10974bbf5ea8SMatthew G. Knepley             }
1098c2e6f3c0SFlorian Wechsung 
1099*0904074fSPatrick Farrell             if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1100c2e6f3c0SFlorian Wechsung               if (isGlobalBcDof) {
1101e047a90bSFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = -1; /* don't use this in assembly in this patch */
1102c2e6f3c0SFlorian Wechsung               } else {
1103c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapIGet(htWithArtificial, globalDof + l, &localDof);CHKERRQ(ierr);
1104c2e6f3c0SFlorian Wechsung                 if (localDof == -1) {
1105c2e6f3c0SFlorian Wechsung                   localDof = localIndexWithArtificial++;
1106c2e6f3c0SFlorian Wechsung                   ierr = PetscHMapISet(htWithArtificial, globalDof + l, localDof);CHKERRQ(ierr);
1107c2e6f3c0SFlorian Wechsung                 }
1108c2e6f3c0SFlorian Wechsung                 if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
1109c2e6f3c0SFlorian Wechsung                 /* And store.*/
1110c2e6f3c0SFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = localDof;
1111c2e6f3c0SFlorian Wechsung               }
1112c2e6f3c0SFlorian Wechsung             }
1113*0904074fSPatrick Farrell 
1114*0904074fSPatrick Farrell             if(isNonlinear) {
1115*0904074fSPatrick Farrell               /* Build the dofmap for the function space with _all_ dofs,
1116*0904074fSPatrick Farrell                  including those in any kind of boundary condition */
1117*0904074fSPatrick Farrell               ierr = PetscHMapIGet(htWithAll, globalDof + l, &localDof);CHKERRQ(ierr);
1118*0904074fSPatrick Farrell               if (localDof == -1) {
1119*0904074fSPatrick Farrell                 localDof = localIndexWithAll++;
1120*0904074fSPatrick Farrell                 ierr = PetscHMapISet(htWithAll, globalDof + l, localDof);CHKERRQ(ierr);
1121*0904074fSPatrick Farrell               }
1122*0904074fSPatrick Farrell               if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
1123*0904074fSPatrick Farrell               /* And store.*/
1124*0904074fSPatrick Farrell               dofsArrayWithAll[globalIndex] = localDof;
1125*0904074fSPatrick Farrell             }
1126c2e6f3c0SFlorian Wechsung             globalIndex++;
11274bbf5ea8SMatthew G. Knepley           }
11284bbf5ea8SMatthew G. Knepley         }
11294bbf5ea8SMatthew G. Knepley       }
1130557beb66SLawrence Mitchell     }
11314bbf5ea8SMatthew G. Knepley      /*How many local dofs in this patch? */
1132*0904074fSPatrick Farrell    if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1133c2e6f3c0SFlorian Wechsung      ierr = PetscHMapIGetSize(htWithArtificial, &dof);CHKERRQ(ierr);
1134c2e6f3c0SFlorian Wechsung      ierr = PetscSectionSetDof(gtolCountsWithArtificial, v, dof);CHKERRQ(ierr);
1135c2e6f3c0SFlorian Wechsung    }
1136*0904074fSPatrick Farrell    if (isNonlinear) {
1137*0904074fSPatrick Farrell      ierr = PetscHMapIGetSize(htWithAll, &dof);CHKERRQ(ierr);
1138*0904074fSPatrick Farrell      ierr = PetscSectionSetDof(gtolCountsWithAll, v, dof);CHKERRQ(ierr);
1139*0904074fSPatrick Farrell    }
11401b68eb51SMatthew G. Knepley     ierr = PetscHMapIGetSize(ht, &dof);CHKERRQ(ierr);
11414bbf5ea8SMatthew G. Knepley     ierr = PetscSectionSetDof(gtolCounts, v, dof);CHKERRQ(ierr);
11424bbf5ea8SMatthew G. Knepley   }
11434bbf5ea8SMatthew 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);
11444bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetUp(gtolCounts);CHKERRQ(ierr);
11454bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(gtolCounts, &numGlobalDofs);CHKERRQ(ierr);
11464bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numGlobalDofs, &globalDofsArray);CHKERRQ(ierr);
11474bbf5ea8SMatthew G. Knepley 
1148*0904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1149c2e6f3c0SFlorian Wechsung     ierr = PetscSectionSetUp(gtolCountsWithArtificial);CHKERRQ(ierr);
1150c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetStorageSize(gtolCountsWithArtificial, &numGlobalDofsWithArtificial);CHKERRQ(ierr);
1151c2e6f3c0SFlorian Wechsung     ierr = PetscMalloc1(numGlobalDofsWithArtificial, &globalDofsArrayWithArtificial);CHKERRQ(ierr);
1152c2e6f3c0SFlorian Wechsung   }
1153*0904074fSPatrick Farrell   if (isNonlinear) {
1154*0904074fSPatrick Farrell     ierr = PetscSectionSetUp(gtolCountsWithAll);CHKERRQ(ierr);
1155*0904074fSPatrick Farrell     ierr = PetscSectionGetStorageSize(gtolCountsWithAll, &numGlobalDofsWithAll);CHKERRQ(ierr);
1156*0904074fSPatrick Farrell     ierr = PetscMalloc1(numGlobalDofsWithAll, &globalDofsArrayWithAll);CHKERRQ(ierr);
1157*0904074fSPatrick Farrell   }
11584bbf5ea8SMatthew 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. */
11594bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
11601b68eb51SMatthew G. Knepley     PetscHashIter hi;
11615f824522SMatthew G. Knepley     PetscInt      dof, off, Np, ooff, i, j, k, l;
11624bbf5ea8SMatthew G. Knepley 
11631b68eb51SMatthew G. Knepley     ierr = PetscHMapIClear(ht);CHKERRQ(ierr);
1164c2e6f3c0SFlorian Wechsung     ierr = PetscHMapIClear(htWithArtificial);CHKERRQ(ierr);
1165*0904074fSPatrick Farrell     ierr = PetscHMapIClear(htWithAll);CHKERRQ(ierr);
11664bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr);
11674bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr);
11685f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(pointCounts, v, &Np);CHKERRQ(ierr);
11695f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(pointCounts, v, &ooff);CHKERRQ(ierr);
11704bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
11714bbf5ea8SMatthew G. Knepley 
11724bbf5ea8SMatthew G. Knepley     for (k = 0; k < patch->nsubspaces; ++k) {
11734bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
11744bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
11754bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
11764bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
1177d490bb3dSLawrence Mitchell       PetscInt        goff;
11784bbf5ea8SMatthew G. Knepley 
11794bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
11804bbf5ea8SMatthew G. Knepley         /* Reconstruct mapping of global-to-local on this patch. */
11814bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
11825f824522SMatthew G. Knepley         PetscInt       cell = c;
11834bbf5ea8SMatthew G. Knepley 
11845f824522SMatthew G. Knepley         if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);}
11854bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
11864bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
11875f824522SMatthew G. Knepley             const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset;
1188c2e6f3c0SFlorian Wechsung             const PetscInt localDof  = dofsArray[key];
11891b68eb51SMatthew G. Knepley             if (localDof >= 0) {ierr = PetscHMapISet(ht, globalDof, localDof);CHKERRQ(ierr);}
1190*0904074fSPatrick Farrell             if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1191c2e6f3c0SFlorian Wechsung               const PetscInt localDofWithArtificial = dofsArrayWithArtificial[key];
1192c2e6f3c0SFlorian Wechsung               if (localDofWithArtificial >= 0) {
1193c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapISet(htWithArtificial, globalDof, localDofWithArtificial);CHKERRQ(ierr);
1194c2e6f3c0SFlorian Wechsung               }
1195c2e6f3c0SFlorian Wechsung             }
1196*0904074fSPatrick Farrell             if (isNonlinear) {
1197*0904074fSPatrick Farrell               const PetscInt localDofWithAll = dofsArrayWithAll[key];
1198*0904074fSPatrick Farrell               if (localDofWithAll >= 0) {
1199*0904074fSPatrick Farrell                 ierr = PetscHMapISet(htWithAll, globalDof, localDofWithAll);CHKERRQ(ierr);
1200*0904074fSPatrick Farrell               }
1201*0904074fSPatrick Farrell             }
1202c2e6f3c0SFlorian Wechsung             key++;
12034bbf5ea8SMatthew G. Knepley           }
12044bbf5ea8SMatthew G. Knepley         }
12054bbf5ea8SMatthew G. Knepley       }
1206557beb66SLawrence Mitchell 
12074bbf5ea8SMatthew G. Knepley       /* Shove it in the output data structure. */
12084bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetOffset(gtolCounts, v, &goff);CHKERRQ(ierr);
12091b68eb51SMatthew G. Knepley       PetscHashIterBegin(ht, hi);
12101b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(ht, hi)) {
12114bbf5ea8SMatthew G. Knepley         PetscInt globalDof, localDof;
12124bbf5ea8SMatthew G. Knepley 
12131b68eb51SMatthew G. Knepley         PetscHashIterGetKey(ht, hi, globalDof);
12141b68eb51SMatthew G. Knepley         PetscHashIterGetVal(ht, hi, localDof);
12154bbf5ea8SMatthew G. Knepley         if (globalDof >= 0) globalDofsArray[goff + localDof] = globalDof;
12161b68eb51SMatthew G. Knepley         PetscHashIterNext(ht, hi);
12174bbf5ea8SMatthew G. Knepley       }
12185f824522SMatthew G. Knepley 
1219*0904074fSPatrick Farrell       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1220c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(gtolCountsWithArtificial, v, &goff);CHKERRQ(ierr);
1221c2e6f3c0SFlorian Wechsung         PetscHashIterBegin(htWithArtificial, hi);
1222c2e6f3c0SFlorian Wechsung         while (!PetscHashIterAtEnd(htWithArtificial, hi)) {
1223c2e6f3c0SFlorian Wechsung           PetscInt globalDof, localDof;
1224c2e6f3c0SFlorian Wechsung           PetscHashIterGetKey(htWithArtificial, hi, globalDof);
1225c2e6f3c0SFlorian Wechsung           PetscHashIterGetVal(htWithArtificial, hi, localDof);
1226c2e6f3c0SFlorian Wechsung           if (globalDof >= 0) globalDofsArrayWithArtificial[goff + localDof] = globalDof;
1227c2e6f3c0SFlorian Wechsung           PetscHashIterNext(htWithArtificial, hi);
1228c2e6f3c0SFlorian Wechsung         }
1229c2e6f3c0SFlorian Wechsung       }
1230*0904074fSPatrick Farrell       if (isNonlinear) {
1231*0904074fSPatrick Farrell         ierr = PetscSectionGetOffset(gtolCountsWithAll, v, &goff);CHKERRQ(ierr);
1232*0904074fSPatrick Farrell         PetscHashIterBegin(htWithAll, hi);
1233*0904074fSPatrick Farrell         while (!PetscHashIterAtEnd(htWithAll, hi)) {
1234*0904074fSPatrick Farrell           PetscInt globalDof, localDof;
1235*0904074fSPatrick Farrell           PetscHashIterGetKey(htWithAll, hi, globalDof);
1236*0904074fSPatrick Farrell           PetscHashIterGetVal(htWithAll, hi, localDof);
1237*0904074fSPatrick Farrell           if (globalDof >= 0) globalDofsArrayWithAll[goff + localDof] = globalDof;
1238*0904074fSPatrick Farrell           PetscHashIterNext(htWithAll, hi);
1239*0904074fSPatrick Farrell         }
1240*0904074fSPatrick Farrell       }
1241c2e6f3c0SFlorian Wechsung 
12425f824522SMatthew G. Knepley       for (p = 0; p < Np; ++p) {
12435f824522SMatthew G. Knepley         const PetscInt point = pointsArray[ooff + p];
12445f824522SMatthew G. Knepley         PetscInt       globalDof, localDof;
12455f824522SMatthew G. Knepley 
12465f824522SMatthew G. Knepley         ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, point, NULL, &globalDof);CHKERRQ(ierr);
12471b68eb51SMatthew G. Knepley         ierr = PetscHMapIGet(ht, globalDof, &localDof);CHKERRQ(ierr);
12485f824522SMatthew G. Knepley         offsArray[(ooff + p)*Nf + k] = localDof;
1249*0904074fSPatrick Farrell         if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1250c2e6f3c0SFlorian Wechsung           ierr = PetscHMapIGet(htWithArtificial, globalDof, &localDof);CHKERRQ(ierr);
1251c2e6f3c0SFlorian Wechsung           offsArrayWithArtificial[(ooff + p)*Nf + k] = localDof;
1252c2e6f3c0SFlorian Wechsung         }
1253*0904074fSPatrick Farrell         if (isNonlinear) {
1254*0904074fSPatrick Farrell           ierr = PetscHMapIGet(htWithAll, globalDof, &localDof);CHKERRQ(ierr);
1255*0904074fSPatrick Farrell           offsArrayWithAll[(ooff + p)*Nf + k] = localDof;
1256*0904074fSPatrick Farrell         }
12575f824522SMatthew G. Knepley       }
12584bbf5ea8SMatthew G. Knepley     }
12594bbf5ea8SMatthew G. Knepley 
12600cd083f8SSatish Balay     ierr = PetscHSetIDestroy(&globalBcs);CHKERRQ(ierr);
12611b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&ownedpts);CHKERRQ(ierr);
12621b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&seenpts);CHKERRQ(ierr);
12631b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&owneddofs);CHKERRQ(ierr);
12641b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&seendofs);CHKERRQ(ierr);
12651b68eb51SMatthew G. Knepley     ierr = PetscHSetIDestroy(&artificialbcs);CHKERRQ(ierr);
1266557beb66SLawrence Mitchell 
12674bbf5ea8SMatthew G. Knepley       /* At this point, we have a hash table ht built that maps globalDof -> localDof.
12684bbf5ea8SMatthew G. Knepley      We need to create the dof table laid out cellwise first, then by subspace,
12694bbf5ea8SMatthew G. Knepley      as the assembler assembles cell-wise and we need to stuff the different
12704bbf5ea8SMatthew G. Knepley      contributions of the different function spaces to the right places. So we loop
12714bbf5ea8SMatthew G. Knepley      over cells, then over subspaces. */
12724bbf5ea8SMatthew G. Knepley     if (patch->nsubspaces > 1) { /* for nsubspaces = 1, data we need is already in dofsArray */
12734bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
12744bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
12755f824522SMatthew G. Knepley         PetscInt       cell = c;
12764bbf5ea8SMatthew G. Knepley 
12775f824522SMatthew G. Knepley         if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);}
12784bbf5ea8SMatthew G. Knepley         for (k = 0; k < patch->nsubspaces; ++k) {
12794bbf5ea8SMatthew G. Knepley           const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
12804bbf5ea8SMatthew G. Knepley           PetscInt        nodesPerCell   = patch->nodesPerCell[k];
12814bbf5ea8SMatthew G. Knepley           PetscInt        subspaceOffset = patch->subspaceOffsets[k];
12824bbf5ea8SMatthew G. Knepley           PetscInt        bs             = patch->bs[k];
12834bbf5ea8SMatthew G. Knepley 
12844bbf5ea8SMatthew G. Knepley           for (j = 0; j < nodesPerCell; ++j) {
12854bbf5ea8SMatthew G. Knepley             for (l = 0; l < bs; ++l) {
12865f824522SMatthew G. Knepley               const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset;
12874bbf5ea8SMatthew G. Knepley               PetscInt       localDof;
12884bbf5ea8SMatthew G. Knepley 
12891b68eb51SMatthew G. Knepley               ierr = PetscHMapIGet(ht, globalDof, &localDof);CHKERRQ(ierr);
1290557beb66SLawrence Mitchell               /* If it's not in the hash table, i.e. is a BC dof,
12911b68eb51SMatthew G. Knepley                then the PetscHSetIMap above gives -1, which matches
1292557beb66SLawrence Mitchell                exactly the convention for PETSc's matrix assembly to
1293557beb66SLawrence Mitchell                ignore the dof. So we don't need to do anything here */
1294c2e6f3c0SFlorian Wechsung               asmArray[asmKey] = localDof;
1295*0904074fSPatrick Farrell               if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1296c2e6f3c0SFlorian Wechsung                 ierr = PetscHMapIGet(htWithArtificial, globalDof, &localDof);CHKERRQ(ierr);
1297c2e6f3c0SFlorian Wechsung                 asmArrayWithArtificial[asmKey] = localDof;
1298c2e6f3c0SFlorian Wechsung               }
1299*0904074fSPatrick Farrell               if (isNonlinear) {
1300*0904074fSPatrick Farrell                 ierr = PetscHMapIGet(htWithAll, globalDof, &localDof);CHKERRQ(ierr);
1301*0904074fSPatrick Farrell                 asmArrayWithAll[asmKey] = localDof;
1302*0904074fSPatrick Farrell               }
1303c2e6f3c0SFlorian Wechsung               asmKey++;
13044bbf5ea8SMatthew G. Knepley             }
13054bbf5ea8SMatthew G. Knepley           }
13064bbf5ea8SMatthew G. Knepley         }
13074bbf5ea8SMatthew G. Knepley       }
13084bbf5ea8SMatthew G. Knepley     }
13094bbf5ea8SMatthew G. Knepley   }
1310c2e6f3c0SFlorian Wechsung   if (1 == patch->nsubspaces) {
1311c2e6f3c0SFlorian Wechsung     ierr = PetscMemcpy(asmArray, dofsArray, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
1312*0904074fSPatrick Farrell     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1313c2e6f3c0SFlorian Wechsung       ierr = PetscMemcpy(asmArrayWithArtificial, dofsArrayWithArtificial, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
1314c2e6f3c0SFlorian Wechsung     }
1315*0904074fSPatrick Farrell     if (isNonlinear) {
1316*0904074fSPatrick Farrell       ierr = PetscMemcpy(asmArrayWithAll, dofsArrayWithAll, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
1317*0904074fSPatrick Farrell     }
1318c2e6f3c0SFlorian Wechsung   }
13194bbf5ea8SMatthew G. Knepley 
13201b68eb51SMatthew G. Knepley   ierr = PetscHMapIDestroy(&ht);CHKERRQ(ierr);
1321c2e6f3c0SFlorian Wechsung   ierr = PetscHMapIDestroy(&htWithArtificial);CHKERRQ(ierr);
1322*0904074fSPatrick Farrell   ierr = PetscHMapIDestroy(&htWithAll);CHKERRQ(ierr);
13234bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(cells, &cellsArray);CHKERRQ(ierr);
13245f824522SMatthew G. Knepley   ierr = ISRestoreIndices(points, &pointsArray);CHKERRQ(ierr);
13254bbf5ea8SMatthew G. Knepley   ierr = PetscFree(dofsArray);CHKERRQ(ierr);
1326*0904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1327c2e6f3c0SFlorian Wechsung     ierr = PetscFree(dofsArrayWithArtificial);CHKERRQ(ierr);
1328c2e6f3c0SFlorian Wechsung   }
1329*0904074fSPatrick Farrell   if (isNonlinear) {
1330*0904074fSPatrick Farrell     ierr = PetscFree(dofsArrayWithAll);CHKERRQ(ierr);
1331*0904074fSPatrick Farrell   }
13325f824522SMatthew G. Knepley   /* Create placeholder section for map from points to patch dofs */
13335f824522SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->patchSection);CHKERRQ(ierr);
13345f824522SMatthew G. Knepley   ierr = PetscSectionSetNumFields(patch->patchSection, patch->nsubspaces);CHKERRQ(ierr);
13351e5fa6bbSLawrence Mitchell   if (patch->combined) {
13361e5fa6bbSLawrence Mitchell     PetscInt numFields;
13371e5fa6bbSLawrence Mitchell     ierr = PetscSectionGetNumFields(patch->dofSection[0], &numFields);CHKERRQ(ierr);
13381e5fa6bbSLawrence 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);
13395f824522SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->dofSection[0], &pStart, &pEnd);CHKERRQ(ierr);
13405f824522SMatthew G. Knepley     ierr = PetscSectionSetChart(patch->patchSection, pStart, pEnd);CHKERRQ(ierr);
13415f824522SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
13425f824522SMatthew G. Knepley       PetscInt dof, fdof, f;
13435f824522SMatthew G. Knepley 
13445f824522SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->dofSection[0], p, &dof);CHKERRQ(ierr);
13455f824522SMatthew G. Knepley       ierr = PetscSectionSetDof(patch->patchSection, p, dof);CHKERRQ(ierr);
13465f824522SMatthew G. Knepley       for (f = 0; f < patch->nsubspaces; ++f) {
13471e5fa6bbSLawrence Mitchell         ierr = PetscSectionGetFieldDof(patch->dofSection[0], p, f, &fdof);CHKERRQ(ierr);
13485f824522SMatthew G. Knepley         ierr = PetscSectionSetFieldDof(patch->patchSection, p, f, fdof);CHKERRQ(ierr);
13495f824522SMatthew G. Knepley       }
13501e5fa6bbSLawrence Mitchell     }
13511e5fa6bbSLawrence Mitchell   } else {
13521e5fa6bbSLawrence Mitchell     PetscInt pStartf, pEndf, f;
13531e5fa6bbSLawrence Mitchell     pStart = PETSC_MAX_INT;
13541e5fa6bbSLawrence Mitchell     pEnd = PETSC_MIN_INT;
13551e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
13561e5fa6bbSLawrence Mitchell       ierr = PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf);CHKERRQ(ierr);
13571e5fa6bbSLawrence Mitchell       pStart = PetscMin(pStart, pStartf);
13581e5fa6bbSLawrence Mitchell       pEnd = PetscMax(pEnd, pEndf);
13591e5fa6bbSLawrence Mitchell     }
13601e5fa6bbSLawrence Mitchell     ierr = PetscSectionSetChart(patch->patchSection, pStart, pEnd);CHKERRQ(ierr);
13611e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
13621e5fa6bbSLawrence Mitchell       ierr = PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf);CHKERRQ(ierr);
13631e5fa6bbSLawrence Mitchell       for (p = pStartf; p < pEndf; ++p) {
13641e5fa6bbSLawrence Mitchell         PetscInt fdof;
13651e5fa6bbSLawrence Mitchell         ierr = PetscSectionGetDof(patch->dofSection[f], p, &fdof);CHKERRQ(ierr);
13661e5fa6bbSLawrence Mitchell         ierr = PetscSectionAddDof(patch->patchSection, p, fdof);CHKERRQ(ierr);
13671e5fa6bbSLawrence Mitchell         ierr = PetscSectionSetFieldDof(patch->patchSection, p, f, fdof);CHKERRQ(ierr);
1368bdd9e0cdSPatrick Farrell       }
1369bdd9e0cdSPatrick Farrell     }
13705f824522SMatthew G. Knepley   }
13715f824522SMatthew G. Knepley   ierr = PetscSectionSetUp(patch->patchSection);CHKERRQ(ierr);
13725f824522SMatthew G. Knepley   ierr = PetscSectionSetUseFieldOffsets(patch->patchSection, PETSC_TRUE);CHKERRQ(ierr);
13734bbf5ea8SMatthew G. Knepley   /* Replace cell indices with firedrake-numbered ones. */
13744bbf5ea8SMatthew G. Knepley   ierr = ISGeneralSetIndices(cells, numCells, (const PetscInt *) newCellsArray, PETSC_OWN_POINTER);CHKERRQ(ierr);
13754bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofs, globalDofsArray, PETSC_OWN_POINTER, &patch->gtol);CHKERRQ(ierr);
13765f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->gtol, "Global Indices");CHKERRQ(ierr);
137710534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_g2l_view", patch->classname);CHKERRQ(ierr);
137810534d48SPatrick Farrell   ierr = PetscSectionViewFromOptions(patch->gtolCounts, (PetscObject) pc, option);CHKERRQ(ierr);
137910534d48SPatrick Farrell   ierr = ISViewFromOptions(patch->gtol, (PetscObject) pc, option);CHKERRQ(ierr);
13804bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArray, PETSC_OWN_POINTER, &patch->dofs);CHKERRQ(ierr);
13815f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArray, PETSC_OWN_POINTER, &patch->offs);CHKERRQ(ierr);
1382*0904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1383c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithArtificial, globalDofsArrayWithArtificial, PETSC_OWN_POINTER, &patch->gtolWithArtificial);CHKERRQ(ierr);
1384c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithArtificial, PETSC_OWN_POINTER, &patch->dofsWithArtificial);CHKERRQ(ierr);
1385c2e6f3c0SFlorian Wechsung     ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithArtificial, PETSC_OWN_POINTER, &patch->offsWithArtificial);CHKERRQ(ierr);
1386c2e6f3c0SFlorian Wechsung   }
1387*0904074fSPatrick Farrell   if (isNonlinear) {
1388*0904074fSPatrick Farrell     ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithAll, globalDofsArrayWithAll, PETSC_OWN_POINTER, &patch->gtolWithAll);CHKERRQ(ierr);
1389*0904074fSPatrick Farrell     ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithAll, PETSC_OWN_POINTER, &patch->dofsWithAll);CHKERRQ(ierr);
1390*0904074fSPatrick Farrell     ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithAll, PETSC_OWN_POINTER, &patch->offsWithAll);CHKERRQ(ierr);
1391*0904074fSPatrick Farrell   }
13924bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
13934bbf5ea8SMatthew G. Knepley }
13944bbf5ea8SMatthew G. Knepley 
13954bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchZeroFillMatrix_Private(Mat mat, const PetscInt ncell, const PetscInt ndof, const PetscInt *dof)
13964bbf5ea8SMatthew G. Knepley {
139723b8bdd9SMatthew G. Knepley   PetscScalar    *values = NULL;
13984bbf5ea8SMatthew G. Knepley   PetscInt        rows, c, i;
13994bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
14004bbf5ea8SMatthew G. Knepley 
14014bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
14024bbf5ea8SMatthew G. Knepley   ierr = PetscCalloc1(ndof*ndof, &values);CHKERRQ(ierr);
14034bbf5ea8SMatthew G. Knepley   for (c = 0; c < ncell; ++c) {
14044bbf5ea8SMatthew G. Knepley     const PetscInt *idx = &dof[ndof*c];
14054bbf5ea8SMatthew G. Knepley     ierr = MatSetValues(mat, ndof, idx, ndof, idx, values, INSERT_VALUES);CHKERRQ(ierr);
14064bbf5ea8SMatthew G. Knepley   }
14074bbf5ea8SMatthew G. Knepley   ierr = MatGetLocalSize(mat, &rows, NULL);CHKERRQ(ierr);
14084bbf5ea8SMatthew G. Knepley   for (i = 0; i < rows; ++i) {
14094bbf5ea8SMatthew G. Knepley     ierr = MatSetValues(mat, 1, &i, 1, &i, values, INSERT_VALUES);CHKERRQ(ierr);
14104bbf5ea8SMatthew G. Knepley   }
14114bbf5ea8SMatthew G. Knepley   ierr = MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14124bbf5ea8SMatthew G. Knepley   ierr = MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14134bbf5ea8SMatthew G. Knepley   ierr = PetscFree(values);CHKERRQ(ierr);
14144bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
14154bbf5ea8SMatthew G. Knepley }
14164bbf5ea8SMatthew G. Knepley 
1417c2e6f3c0SFlorian Wechsung static PetscErrorCode PCPatchCreateMatrix_Private(PC pc, PetscInt point, Mat *mat, PetscBool withArtificial)
14184bbf5ea8SMatthew G. Knepley {
14194bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
14204bbf5ea8SMatthew G. Knepley   Vec            x, y;
14214bbf5ea8SMatthew G. Knepley   PetscBool      flg;
14224bbf5ea8SMatthew G. Knepley   PetscInt       csize, rsize;
14234bbf5ea8SMatthew G. Knepley   const char    *prefix = NULL;
14244bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
14254bbf5ea8SMatthew G. Knepley 
14264bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1427c2e6f3c0SFlorian Wechsung   if(withArtificial) {
1428e047a90bSFlorian Wechsung     /* would be nice if we could create a rectangular matrix of size numDofsWithArtificial x numDofs here */
14291202d238SPatrick Farrell     x = patch->patchRHSWithArtificial[point];
14301202d238SPatrick Farrell     y = patch->patchRHSWithArtificial[point];
1431ff201f6aSFlorian Wechsung   } else {
14321202d238SPatrick Farrell     x = patch->patchRHS[point];
14331202d238SPatrick Farrell     y = patch->patchUpdate[point];
1434c2e6f3c0SFlorian Wechsung   }
1435c2e6f3c0SFlorian Wechsung 
14364bbf5ea8SMatthew G. Knepley   ierr = VecGetSize(x, &csize);CHKERRQ(ierr);
14374bbf5ea8SMatthew G. Knepley   ierr = VecGetSize(y, &rsize);CHKERRQ(ierr);
14384bbf5ea8SMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, mat);CHKERRQ(ierr);
14394bbf5ea8SMatthew G. Knepley   ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr);
14404bbf5ea8SMatthew G. Knepley   ierr = MatSetOptionsPrefix(*mat, prefix);CHKERRQ(ierr);
14415f824522SMatthew G. Knepley   ierr = MatAppendOptionsPrefix(*mat, "pc_patch_sub_");CHKERRQ(ierr);
14424bbf5ea8SMatthew G. Knepley   if (patch->sub_mat_type)       {ierr = MatSetType(*mat, patch->sub_mat_type);CHKERRQ(ierr);}
14437974b488SMatthew G. Knepley   else if (!patch->sub_mat_type) {ierr = MatSetType(*mat, MATDENSE);CHKERRQ(ierr);}
14444bbf5ea8SMatthew G. Knepley   ierr = MatSetSizes(*mat, rsize, csize, rsize, csize);CHKERRQ(ierr);
14454bbf5ea8SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) *mat, MATDENSE, &flg);CHKERRQ(ierr);
14464bbf5ea8SMatthew G. Knepley   if (!flg) {ierr = PetscObjectTypeCompare((PetscObject)*mat, MATSEQDENSE, &flg);CHKERRQ(ierr);}
14474bbf5ea8SMatthew G. Knepley   /* Sparse patch matrices */
14484bbf5ea8SMatthew G. Knepley   if (!flg) {
14494bbf5ea8SMatthew G. Knepley     PetscBT         bt;
14504bbf5ea8SMatthew G. Knepley     PetscInt       *dnnz      = NULL;
14514bbf5ea8SMatthew G. Knepley     const PetscInt *dofsArray = NULL;
14524bbf5ea8SMatthew G. Knepley     PetscInt        pStart, pEnd, ncell, offset, c, i, j;
14534bbf5ea8SMatthew G. Knepley 
1454c2e6f3c0SFlorian Wechsung     if(withArtificial) {
1455c2e6f3c0SFlorian Wechsung       ierr = ISGetIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1456ff201f6aSFlorian Wechsung     } else {
14574bbf5ea8SMatthew G. Knepley       ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1458c2e6f3c0SFlorian Wechsung     }
14594bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
14604bbf5ea8SMatthew G. Knepley     point += pStart;
14614bbf5ea8SMatthew 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);
14624bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
14634bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
14644bbf5ea8SMatthew G. Knepley     ierr = PetscCalloc1(rsize, &dnnz);CHKERRQ(ierr);
14654bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventBegin(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr);
14664bbf5ea8SMatthew G. Knepley     /* XXX: This uses N^2 bits to store the sparsity pattern on a
14674bbf5ea8SMatthew G. Knepley      * patch.  This is probably OK if the patches are not too big,
14684bbf5ea8SMatthew G. Knepley      * but could use quite a bit of memory for planes in 3D.
14694bbf5ea8SMatthew G. Knepley      * Should we switch based on the value of rsize to a
14704bbf5ea8SMatthew G. Knepley      * hash-table (slower, but more memory efficient) approach? */
14714bbf5ea8SMatthew G. Knepley     ierr = PetscBTCreate(rsize*rsize, &bt);CHKERRQ(ierr);
14724bbf5ea8SMatthew G. Knepley     for (c = 0; c < ncell; ++c) {
14734bbf5ea8SMatthew G. Knepley       const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell;
14744bbf5ea8SMatthew G. Knepley       for (i = 0; i < patch->totalDofsPerCell; ++i) {
14754bbf5ea8SMatthew G. Knepley         const PetscInt row = idx[i];
1476557beb66SLawrence Mitchell         if (row < 0) continue;
14774bbf5ea8SMatthew G. Knepley         for (j = 0; j < patch->totalDofsPerCell; ++j) {
14784bbf5ea8SMatthew G. Knepley           const PetscInt col = idx[j];
14794bbf5ea8SMatthew G. Knepley           const PetscInt key = row*rsize + col;
1480557beb66SLawrence Mitchell           if (col < 0) continue;
14814bbf5ea8SMatthew G. Knepley           if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
14824bbf5ea8SMatthew G. Knepley         }
14834bbf5ea8SMatthew G. Knepley       }
14844bbf5ea8SMatthew G. Knepley     }
14854bbf5ea8SMatthew G. Knepley     ierr = PetscBTDestroy(&bt);CHKERRQ(ierr);
14864bbf5ea8SMatthew G. Knepley     ierr = MatXAIJSetPreallocation(*mat, 1, dnnz, NULL, NULL, NULL);CHKERRQ(ierr);
14874bbf5ea8SMatthew G. Knepley     ierr = PetscFree(dnnz);CHKERRQ(ierr);
14884bbf5ea8SMatthew G. Knepley     ierr = PCPatchZeroFillMatrix_Private(*mat, ncell, patch->totalDofsPerCell, &dofsArray[offset*patch->totalDofsPerCell]);CHKERRQ(ierr);
14894bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr);
1490fe117d09SFlorian Wechsung     if(withArtificial) {
1491fe117d09SFlorian Wechsung       ierr = ISRestoreIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1492fe117d09SFlorian Wechsung     } else {
14934bbf5ea8SMatthew G. Knepley       ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
14944bbf5ea8SMatthew G. Knepley     }
1495fe117d09SFlorian Wechsung   }
14964bbf5ea8SMatthew G. Knepley   ierr = MatSetUp(*mat);CHKERRQ(ierr);
14974bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
14984bbf5ea8SMatthew G. Knepley }
14994bbf5ea8SMatthew G. Knepley 
1500*0904074fSPatrick Farrell static PetscErrorCode PCPatchComputeFunction_DMPlex_Private(PC pc, PetscInt patchNum, Vec x, Vec F, IS cellIS, PetscInt n, const PetscInt *l2p, const PetscInt *l2pWithAll, void *ctx)
150192d50984SMatthew G. Knepley {
150292d50984SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
150392d50984SMatthew G. Knepley   DM              dm;
150492d50984SMatthew G. Knepley   PetscSection    s;
150592d50984SMatthew G. Knepley   const PetscInt *parray, *oarray;
150692d50984SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
150792d50984SMatthew G. Knepley   PetscErrorCode  ierr;
150892d50984SMatthew G. Knepley 
150992d50984SMatthew G. Knepley   PetscFunctionBegin;
151092d50984SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
151192d50984SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
151292d50984SMatthew G. Knepley   /* Set offset into patch */
151392d50984SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->pointCounts, patchNum, &Np);CHKERRQ(ierr);
151492d50984SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->pointCounts, patchNum, &poff);CHKERRQ(ierr);
151592d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->points, &parray);CHKERRQ(ierr);
151692d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->offs,   &oarray);CHKERRQ(ierr);
151792d50984SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
151892d50984SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
151992d50984SMatthew G. Knepley       const PetscInt point = parray[poff+p];
152092d50984SMatthew G. Knepley       PetscInt       dof;
152192d50984SMatthew G. Knepley 
152292d50984SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(patch->patchSection, point, f, &dof);CHKERRQ(ierr);
152392d50984SMatthew G. Knepley       ierr = PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);
152492d50984SMatthew G. Knepley       if (patch->nsubspaces == 1) {ierr = PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);}
152592d50984SMatthew G. Knepley       else                        {ierr = PetscSectionSetOffset(patch->patchSection, point, -1);CHKERRQ(ierr);}
152692d50984SMatthew G. Knepley     }
152792d50984SMatthew G. Knepley   }
152892d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->points, &parray);CHKERRQ(ierr);
152992d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->offs,   &oarray);CHKERRQ(ierr);
153092d50984SMatthew G. Knepley   if (patch->viewSection) {ierr = ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection);CHKERRQ(ierr);}
153192d50984SMatthew G. Knepley   ierr = DMPlexComputeResidual_Patch_Internal(pc->dm, patch->patchSection, cellIS, 0.0, x, NULL, F, ctx);CHKERRQ(ierr);
153292d50984SMatthew G. Knepley   PetscFunctionReturn(0);
153392d50984SMatthew G. Knepley }
153492d50984SMatthew G. Knepley 
153592d50984SMatthew G. Knepley PetscErrorCode PCPatchComputeFunction_Internal(PC pc, Vec x, Vec F, PetscInt point)
153692d50984SMatthew G. Knepley {
153792d50984SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
153892d50984SMatthew G. Knepley   const PetscInt *dofsArray;
1539*0904074fSPatrick Farrell   const PetscInt *dofsArrayWithAll;
154092d50984SMatthew G. Knepley   const PetscInt *cellsArray;
154192d50984SMatthew G. Knepley   PetscInt        ncell, offset, pStart, pEnd;
154292d50984SMatthew G. Knepley   PetscErrorCode  ierr;
154392d50984SMatthew G. Knepley 
154492d50984SMatthew G. Knepley   PetscFunctionBegin;
154592d50984SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
154692d50984SMatthew G. Knepley   if (!patch->usercomputeop) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback\n");
154792d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1548*0904074fSPatrick Farrell   ierr = ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
154992d50984SMatthew G. Knepley   ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
155092d50984SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
155192d50984SMatthew G. Knepley 
155292d50984SMatthew G. Knepley   point += pStart;
155392d50984SMatthew 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);
155492d50984SMatthew G. Knepley 
155592d50984SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
155692d50984SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
155792d50984SMatthew G. Knepley   if (ncell <= 0) {
155892d50984SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
155992d50984SMatthew G. Knepley     PetscFunctionReturn(0);
156092d50984SMatthew G. Knepley   }
156192d50984SMatthew G. Knepley   PetscStackPush("PCPatch user callback");
156292d50984SMatthew G. Knepley   /* Cannot reuse the same IS because the geometry info is being cached in it */
156392d50984SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS);CHKERRQ(ierr);
156439fd2e8aSPatrick Farrell   ierr = patch->usercomputef(pc, point, x, F, patch->cellIS, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell,
1565*0904074fSPatrick Farrell                                                                                             dofsArrayWithAll + offset*patch->totalDofsPerCell,
156639fd2e8aSPatrick Farrell                                                                                             patch->usercomputefctx);CHKERRQ(ierr);
156792d50984SMatthew G. Knepley   PetscStackPop;
156892d50984SMatthew G. Knepley   ierr = ISDestroy(&patch->cellIS);CHKERRQ(ierr);
156992d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1570*0904074fSPatrick Farrell   ierr = ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
157192d50984SMatthew G. Knepley   ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
157292d50984SMatthew G. Knepley   if (patch->viewMatrix) {
157392d50984SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
157492d50984SMatthew G. Knepley 
157592d50984SMatthew G. Knepley     ierr = PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch vector for Point %D", point);CHKERRQ(ierr);
157692d50984SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) F, name);CHKERRQ(ierr);
157792d50984SMatthew G. Knepley     ierr = ObjectView((PetscObject) F, patch->viewerMatrix, patch->formatMatrix);CHKERRQ(ierr);
157892d50984SMatthew G. Knepley   }
157992d50984SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
158092d50984SMatthew G. Knepley   PetscFunctionReturn(0);
158192d50984SMatthew G. Knepley }
158292d50984SMatthew G. Knepley 
1583*0904074fSPatrick Farrell static PetscErrorCode PCPatchComputeOperator_DMPlex_Private(PC pc, PetscInt patchNum, Vec x, Mat J, IS cellIS, PetscInt n, const PetscInt *l2p, const PetscInt *l2pWithAll, void *ctx)
15845f824522SMatthew G. Knepley {
15855f824522SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
15865f824522SMatthew G. Knepley   DM              dm;
15875f824522SMatthew G. Knepley   PetscSection    s;
15885f824522SMatthew G. Knepley   const PetscInt *parray, *oarray;
15895f824522SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
15905f824522SMatthew G. Knepley   PetscErrorCode  ierr;
15915f824522SMatthew G. Knepley 
15925f824522SMatthew G. Knepley   PetscFunctionBegin;
15935f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
15945f824522SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
15955f824522SMatthew G. Knepley   /* Set offset into patch */
15965f824522SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->pointCounts, patchNum, &Np);CHKERRQ(ierr);
15975f824522SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->pointCounts, patchNum, &poff);CHKERRQ(ierr);
15985f824522SMatthew G. Knepley   ierr = ISGetIndices(patch->points, &parray);CHKERRQ(ierr);
15995f824522SMatthew G. Knepley   ierr = ISGetIndices(patch->offs,   &oarray);CHKERRQ(ierr);
16005f824522SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
16015f824522SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
16025f824522SMatthew G. Knepley       const PetscInt point = parray[poff+p];
16035f824522SMatthew G. Knepley       PetscInt       dof;
16045f824522SMatthew G. Knepley 
16055f824522SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(patch->patchSection, point, f, &dof);CHKERRQ(ierr);
16065f824522SMatthew G. Knepley       ierr = PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);
16075f824522SMatthew G. Knepley       if (patch->nsubspaces == 1) {ierr = PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);}
16085f824522SMatthew G. Knepley       else                        {ierr = PetscSectionSetOffset(patch->patchSection, point, -1);CHKERRQ(ierr);}
16095f824522SMatthew G. Knepley     }
16105f824522SMatthew G. Knepley   }
16115f824522SMatthew G. Knepley   ierr = ISRestoreIndices(patch->points, &parray);CHKERRQ(ierr);
16125f824522SMatthew G. Knepley   ierr = ISRestoreIndices(patch->offs,   &oarray);CHKERRQ(ierr);
16135f824522SMatthew G. Knepley   if (patch->viewSection) {ierr = ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection);CHKERRQ(ierr);}
16145f824522SMatthew G. Knepley   /* TODO Shut off MatViewFromOptions() in MatAssemblyEnd() here */
1615723f9013SMatthew G. Knepley   ierr = DMPlexComputeJacobian_Patch_Internal(pc->dm, patch->patchSection, patch->patchSection, cellIS, 0.0, 0.0, x, NULL, J, J, ctx);CHKERRQ(ierr);
16165f824522SMatthew G. Knepley   PetscFunctionReturn(0);
16175f824522SMatthew G. Knepley }
16185f824522SMatthew G. Knepley 
161934d8b122SPatrick Farrell PetscErrorCode PCPatchComputeOperator_Internal(PC pc, Vec x, Mat mat, PetscInt point, PetscBool withArtificial)
16204bbf5ea8SMatthew G. Knepley {
16214bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
16224bbf5ea8SMatthew G. Knepley   const PetscInt *dofsArray;
16234d04e9f1SPatrick Farrell   const PetscInt *dofsArrayWithArtificial = NULL;
1624*0904074fSPatrick Farrell   const PetscInt *dofsArrayWithAll = NULL;
16254bbf5ea8SMatthew G. Knepley   const PetscInt *cellsArray;
16264bbf5ea8SMatthew G. Knepley   PetscInt        ncell, offset, pStart, pEnd;
16274d04e9f1SPatrick Farrell   PetscBool       isNonlinear;
16284bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
16294bbf5ea8SMatthew G. Knepley 
16304bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
16314bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
1632debbdec3SPatrick Farrell   isNonlinear = patch->isNonlinear;
16334bbf5ea8SMatthew G. Knepley   if (!patch->usercomputeop) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback\n");
1634c2e6f3c0SFlorian Wechsung   if(withArtificial) {
1635c2e6f3c0SFlorian Wechsung     ierr = ISGetIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1636c2e6f3c0SFlorian Wechsung   } else {
16374bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1638c2e6f3c0SFlorian Wechsung   }
16394d04e9f1SPatrick Farrell   if (isNonlinear) {
1640*0904074fSPatrick Farrell     ierr = ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
16414d04e9f1SPatrick Farrell   }
16424bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
16434bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
16444bbf5ea8SMatthew G. Knepley 
16454bbf5ea8SMatthew G. Knepley   point += pStart;
16464bbf5ea8SMatthew 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);
16474bbf5ea8SMatthew G. Knepley 
16484bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
16494bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
16504bbf5ea8SMatthew G. Knepley   if (ncell <= 0) {
16514bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
16524bbf5ea8SMatthew G. Knepley     PetscFunctionReturn(0);
16534bbf5ea8SMatthew G. Knepley   }
16544bbf5ea8SMatthew G. Knepley   PetscStackPush("PCPatch user callback");
16552aa6f319SMatthew G. Knepley   /* Cannot reuse the same IS because the geometry info is being cached in it */
16562aa6f319SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS);CHKERRQ(ierr);
1657*0904074fSPatrick Farrell   ierr = patch->usercomputeop(pc, point, x, mat, patch->cellIS, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell, dofsArrayWithAll ? dofsArrayWithAll + offset*patch->totalDofsPerCell : NULL, patch->usercomputeopctx);CHKERRQ(ierr);
16584bbf5ea8SMatthew G. Knepley   PetscStackPop;
16592aa6f319SMatthew G. Knepley   ierr = ISDestroy(&patch->cellIS);CHKERRQ(ierr);
16604d04e9f1SPatrick Farrell   if(withArtificial) {
1661c2e6f3c0SFlorian Wechsung     ierr = ISRestoreIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1662c2e6f3c0SFlorian Wechsung   } else {
16634bbf5ea8SMatthew G. Knepley     ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1664c2e6f3c0SFlorian Wechsung   }
16654d04e9f1SPatrick Farrell   if (isNonlinear) {
1666*0904074fSPatrick Farrell     ierr = ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
16674d04e9f1SPatrick Farrell   }
16684bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
16692aa6f319SMatthew G. Knepley   if (patch->viewMatrix) {
16702aa6f319SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
16712aa6f319SMatthew G. Knepley 
16722aa6f319SMatthew G. Knepley     ierr = PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch matrix for Point %D", point);CHKERRQ(ierr);
16732aa6f319SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) mat, name);CHKERRQ(ierr);
16742aa6f319SMatthew G. Knepley     ierr = ObjectView((PetscObject) mat, patch->viewerMatrix, patch->formatMatrix);CHKERRQ(ierr);
16752aa6f319SMatthew G. Knepley   }
16764bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
16774bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
16784bbf5ea8SMatthew G. Knepley }
16794bbf5ea8SMatthew G. Knepley 
1680*0904074fSPatrick Farrell PetscErrorCode PCPatch_ScatterLocal_Private(PC pc, PetscInt p, Vec x, Vec y, InsertMode mode, ScatterMode scat, PatchScatterType scattertype)
16814bbf5ea8SMatthew G. Knepley {
16824bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch     = (PC_PATCH *) pc->data;
16834bbf5ea8SMatthew G. Knepley   const PetscScalar *xArray    = NULL;
16844bbf5ea8SMatthew G. Knepley   PetscScalar       *yArray    = NULL;
16854bbf5ea8SMatthew G. Knepley   const PetscInt    *gtolArray = NULL;
16864bbf5ea8SMatthew G. Knepley   PetscInt           dof, offset, lidx;
16874bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
16884bbf5ea8SMatthew G. Knepley 
16894bbf5ea8SMatthew G. Knepley   PetscFunctionBeginHot;
16904bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Scatter, pc, 0, 0, 0);CHKERRQ(ierr);
16914bbf5ea8SMatthew G. Knepley   ierr = VecGetArrayRead(x, &xArray);CHKERRQ(ierr);
16924bbf5ea8SMatthew G. Knepley   ierr = VecGetArray(y, &yArray);CHKERRQ(ierr);
1693*0904074fSPatrick Farrell   if (scattertype == SCATTER_WITHARTIFICIAL) {
1694c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof);CHKERRQ(ierr);
1695c2e6f3c0SFlorian Wechsung     ierr = PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offset);CHKERRQ(ierr);
1696c2e6f3c0SFlorian Wechsung     ierr = ISGetIndices(patch->gtolWithArtificial, &gtolArray);CHKERRQ(ierr);
1697*0904074fSPatrick Farrell   } else if (scattertype == SCATTER_WITHALL) {
1698*0904074fSPatrick Farrell     ierr = PetscSectionGetDof(patch->gtolCountsWithAll, p, &dof);CHKERRQ(ierr);
1699*0904074fSPatrick Farrell     ierr = PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offset);CHKERRQ(ierr);
1700*0904074fSPatrick Farrell     ierr = ISGetIndices(patch->gtolWithAll, &gtolArray);CHKERRQ(ierr);
1701c2e6f3c0SFlorian Wechsung   } else {
17024bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr);
17034bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
17044bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
1705c2e6f3c0SFlorian Wechsung   }
17064bbf5ea8SMatthew 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");
17074bbf5ea8SMatthew 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");
17084bbf5ea8SMatthew G. Knepley   for (lidx = 0; lidx < dof; ++lidx) {
17094bbf5ea8SMatthew G. Knepley     const PetscInt gidx = gtolArray[offset+lidx];
17104bbf5ea8SMatthew G. Knepley 
17114bbf5ea8SMatthew G. Knepley     if (mode == INSERT_VALUES) yArray[lidx]  = xArray[gidx]; /* Forward */
17124bbf5ea8SMatthew G. Knepley     else                       yArray[gidx] += xArray[lidx]; /* Reverse */
17134bbf5ea8SMatthew G. Knepley   }
1714*0904074fSPatrick Farrell   if (scattertype == SCATTER_WITHARTIFICIAL) {
1715c2e6f3c0SFlorian Wechsung     ierr = ISRestoreIndices(patch->gtolWithArtificial, &gtolArray);CHKERRQ(ierr);
1716*0904074fSPatrick Farrell   } else if (scattertype == SCATTER_WITHALL) {
1717*0904074fSPatrick Farrell     ierr = ISRestoreIndices(patch->gtolWithAll, &gtolArray);CHKERRQ(ierr);
1718c2e6f3c0SFlorian Wechsung   } else {
17194bbf5ea8SMatthew G. Knepley     ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
1720c2e6f3c0SFlorian Wechsung   }
17214bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArrayRead(x, &xArray);CHKERRQ(ierr);
17224bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArray(y, &yArray);CHKERRQ(ierr);
17234bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Scatter, pc, 0, 0, 0);CHKERRQ(ierr);
17244bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
17254bbf5ea8SMatthew G. Knepley }
17264bbf5ea8SMatthew G. Knepley 
1727dadc69c5SMatthew G. Knepley static PetscErrorCode PCSetUp_PATCH_Linear(PC pc)
1728dadc69c5SMatthew G. Knepley {
1729dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
1730dadc69c5SMatthew G. Knepley   const char    *prefix;
1731dadc69c5SMatthew G. Knepley   PetscInt       i;
1732dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
1733dadc69c5SMatthew G. Knepley 
1734dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
1735dadc69c5SMatthew G. Knepley   if (!pc->setupcalled) {
1736dadc69c5SMatthew G. Knepley     ierr = PetscMalloc1(patch->npatch, &patch->solver);CHKERRQ(ierr);
1737dadc69c5SMatthew G. Knepley     ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr);
1738dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {
1739dadc69c5SMatthew G. Knepley       KSP ksp;
1740dadc69c5SMatthew G. Knepley       PC  subpc;
1741dadc69c5SMatthew G. Knepley 
1742dadc69c5SMatthew G. Knepley       ierr = KSPCreate(PETSC_COMM_SELF, &ksp);CHKERRQ(ierr);
1743dadc69c5SMatthew G. Knepley       ierr = KSPSetOptionsPrefix(ksp, prefix);CHKERRQ(ierr);
1744dadc69c5SMatthew G. Knepley       ierr = KSPAppendOptionsPrefix(ksp, "sub_");CHKERRQ(ierr);
1745dadc69c5SMatthew G. Knepley       ierr = PetscObjectIncrementTabLevel((PetscObject) ksp, (PetscObject) pc, 1);CHKERRQ(ierr);
1746dadc69c5SMatthew G. Knepley       ierr = KSPGetPC(ksp, &subpc);CHKERRQ(ierr);
1747dadc69c5SMatthew G. Knepley       ierr = PetscObjectIncrementTabLevel((PetscObject) subpc, (PetscObject) pc, 1);CHKERRQ(ierr);
1748dadc69c5SMatthew G. Knepley       ierr = PetscLogObjectParent((PetscObject) pc, (PetscObject) ksp);CHKERRQ(ierr);
1749dadc69c5SMatthew G. Knepley       patch->solver[i] = (PetscObject) ksp;
1750dadc69c5SMatthew G. Knepley     }
1751dadc69c5SMatthew G. Knepley   }
1752dadc69c5SMatthew G. Knepley   if (patch->save_operators) {
1753dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {
1754dadc69c5SMatthew G. Knepley       ierr = MatZeroEntries(patch->mat[i]);CHKERRQ(ierr);
175534d8b122SPatrick Farrell       ierr = PCPatchComputeOperator_Internal(pc, NULL, patch->mat[i], i, PETSC_FALSE);CHKERRQ(ierr);
1756dadc69c5SMatthew G. Knepley       ierr = KSPSetOperators((KSP) patch->solver[i], patch->mat[i], patch->mat[i]);CHKERRQ(ierr);
1757dadc69c5SMatthew G. Knepley     }
1758dadc69c5SMatthew G. Knepley   }
175934d8b122SPatrick Farrell   if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
176034d8b122SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {
17611202d238SPatrick Farrell       /* Instead of padding patch->patchUpdate with zeros to get */
17621202d238SPatrick Farrell       /* patch->patchUpdateWithArtificial and then multiplying with the matrix, */
176334d8b122SPatrick Farrell       /* just get rid of the columns that correspond to the dofs with */
176434d8b122SPatrick Farrell       /* artificial bcs. That's of course fairly inefficient, hopefully we */
176534d8b122SPatrick Farrell       /* can just assemble the rectangular matrix in the first place. */
176634d8b122SPatrick Farrell       Mat matSquare;
176734d8b122SPatrick Farrell       IS rowis;
176834d8b122SPatrick Farrell       PetscInt dof;
176934d8b122SPatrick Farrell 
177034d8b122SPatrick Farrell       ierr = MatGetSize(patch->mat[i], &dof, NULL);CHKERRQ(ierr);
177134d8b122SPatrick Farrell       if (dof == 0) {
177234d8b122SPatrick Farrell         patch->matWithArtificial[i] = NULL;
177334d8b122SPatrick Farrell         continue;
177434d8b122SPatrick Farrell       }
177534d8b122SPatrick Farrell 
177634d8b122SPatrick Farrell       ierr = PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE);CHKERRQ(ierr);
177734d8b122SPatrick Farrell       ierr = MatZeroEntries(matSquare);CHKERRQ(ierr);
177834d8b122SPatrick Farrell       ierr = PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE);CHKERRQ(ierr);
177934d8b122SPatrick Farrell 
178034d8b122SPatrick Farrell       ierr = MatGetSize(matSquare, &dof, NULL);CHKERRQ(ierr);
178134d8b122SPatrick Farrell       ierr = ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis); CHKERRQ(ierr);
178234d8b122SPatrick Farrell       if(pc->setupcalled) {
178334d8b122SPatrick Farrell         ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_REUSE_MATRIX, &patch->matWithArtificial[i]); CHKERRQ(ierr);
178434d8b122SPatrick Farrell       } else {
178534d8b122SPatrick Farrell         ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &patch->matWithArtificial[i]); CHKERRQ(ierr);
178634d8b122SPatrick Farrell       }
178734d8b122SPatrick Farrell       ierr = ISDestroy(&rowis); CHKERRQ(ierr);
178834d8b122SPatrick Farrell       ierr = MatDestroy(&matSquare);CHKERRQ(ierr);
178934d8b122SPatrick Farrell     }
179034d8b122SPatrick Farrell   }
1791dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
1792dadc69c5SMatthew G. Knepley }
1793dadc69c5SMatthew G. Knepley 
17944bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUp_PATCH(PC pc)
17954bbf5ea8SMatthew G. Knepley {
17964bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
1797557beb66SLawrence Mitchell   PetscInt       i;
179839fd2e8aSPatrick Farrell   PetscBool       isNonlinear;
17994bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
18004bbf5ea8SMatthew G. Knepley 
18014bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
18024bbf5ea8SMatthew G. Knepley   if (!pc->setupcalled) {
18034bbf5ea8SMatthew G. Knepley     PetscInt pStart, pEnd, p;
18044bbf5ea8SMatthew G. Knepley     PetscInt localSize;
18054bbf5ea8SMatthew G. Knepley 
18064bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventBegin(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr);
18074bbf5ea8SMatthew G. Knepley 
1808debbdec3SPatrick Farrell     isNonlinear = patch->isNonlinear;
18095f824522SMatthew G. Knepley     if (!patch->nsubspaces) {
18105f824522SMatthew G. Knepley       DM           dm;
18115f824522SMatthew G. Knepley       PetscDS      prob;
18125f824522SMatthew G. Knepley       PetscSection s;
1813e72c1634SMatthew G. Knepley       PetscInt     cStart, cEnd, c, Nf, f, numGlobalBcs = 0, *globalBcs, *Nb, totNb = 0, **cellDofs;
18145f824522SMatthew G. Knepley 
18155f824522SMatthew G. Knepley       ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
18165f824522SMatthew G. Knepley       if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONG, "Must set DM for PCPATCH or call PCPatchSetDiscretisationInfo()");
18175f824522SMatthew G. Knepley       ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
18185f824522SMatthew G. Knepley       ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
18195f824522SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
18205f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
18215f824522SMatthew G. Knepley         PetscInt cdof;
18225f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
18235f824522SMatthew G. Knepley         numGlobalBcs += cdof;
18245f824522SMatthew G. Knepley       }
18255f824522SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
18265f824522SMatthew G. Knepley       ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
18275f824522SMatthew G. Knepley       ierr = PetscMalloc3(Nf, &Nb, Nf, &cellDofs, numGlobalBcs, &globalBcs);CHKERRQ(ierr);
18285f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
18295f824522SMatthew G. Knepley         PetscFE        fe;
18305f824522SMatthew G. Knepley         PetscDualSpace sp;
18315f824522SMatthew G. Knepley         PetscInt       cdoff = 0;
18325f824522SMatthew G. Knepley 
18335f824522SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
18345f824522SMatthew G. Knepley         /* ierr = PetscFEGetNumComponents(fe, &Nc[f]);CHKERRQ(ierr); */
18355f824522SMatthew G. Knepley         ierr = PetscFEGetDualSpace(fe, &sp);CHKERRQ(ierr);
18365f824522SMatthew G. Knepley         ierr = PetscDualSpaceGetDimension(sp, &Nb[f]);CHKERRQ(ierr);
18375f824522SMatthew G. Knepley         totNb += Nb[f];
18385f824522SMatthew G. Knepley 
18395f824522SMatthew G. Knepley         ierr = PetscMalloc1((cEnd-cStart)*Nb[f], &cellDofs[f]);CHKERRQ(ierr);
18405f824522SMatthew G. Knepley         for (c = cStart; c < cEnd; ++c) {
18415f824522SMatthew G. Knepley           PetscInt *closure = NULL;
18425f824522SMatthew G. Knepley           PetscInt  clSize  = 0, cl;
18435f824522SMatthew G. Knepley 
18445f824522SMatthew G. Knepley           ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
18455f824522SMatthew G. Knepley           for (cl = 0; cl < clSize*2; cl += 2) {
18465f824522SMatthew G. Knepley             const PetscInt p = closure[cl];
18475f824522SMatthew G. Knepley             PetscInt       fdof, d, foff;
18485f824522SMatthew G. Knepley 
18495f824522SMatthew G. Knepley             ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr);
18505f824522SMatthew G. Knepley             ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr);
18515f824522SMatthew G. Knepley             for (d = 0; d < fdof; ++d, ++cdoff) cellDofs[f][cdoff] = foff + d;
18525f824522SMatthew G. Knepley           }
18535f824522SMatthew G. Knepley           ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
18545f824522SMatthew G. Knepley         }
18555f824522SMatthew 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]);
18565f824522SMatthew G. Knepley       }
18575f824522SMatthew G. Knepley       numGlobalBcs = 0;
18585f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
18595f824522SMatthew G. Knepley         const PetscInt *ind;
18605f824522SMatthew G. Knepley         PetscInt        off, cdof, d;
18615f824522SMatthew G. Knepley 
18625f824522SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
18635f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
18645f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &ind);CHKERRQ(ierr);
18655f824522SMatthew G. Knepley         for (d = 0; d < cdof; ++d) globalBcs[numGlobalBcs++] = off + ind[d];
18665f824522SMatthew G. Knepley       }
18675f824522SMatthew G. Knepley 
18685f824522SMatthew G. Knepley       ierr = PCPatchSetDiscretisationInfoCombined(pc, dm, Nb, (const PetscInt **) cellDofs, numGlobalBcs, globalBcs, numGlobalBcs, globalBcs);CHKERRQ(ierr);
18695f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
18705f824522SMatthew G. Knepley         ierr = PetscFree(cellDofs[f]);CHKERRQ(ierr);
18715f824522SMatthew G. Knepley       }
18725f824522SMatthew G. Knepley       ierr = PetscFree3(Nb, cellDofs, globalBcs);CHKERRQ(ierr);
187392d50984SMatthew G. Knepley       ierr = PCPatchSetComputeFunction(pc, PCPatchComputeFunction_DMPlex_Private, NULL);CHKERRQ(ierr);
18745f824522SMatthew G. Knepley       ierr = PCPatchSetComputeOperator(pc, PCPatchComputeOperator_DMPlex_Private, NULL);CHKERRQ(ierr);
18755f824522SMatthew G. Knepley     }
18765f824522SMatthew G. Knepley 
18774bbf5ea8SMatthew G. Knepley     localSize = patch->subspaceOffsets[patch->nsubspaces];
18781202d238SPatrick Farrell     ierr = VecCreateSeq(PETSC_COMM_SELF, localSize, &patch->localRHS);CHKERRQ(ierr);
18791202d238SPatrick Farrell     ierr = VecSetUp(patch->localRHS);CHKERRQ(ierr);
18801202d238SPatrick Farrell     ierr = VecDuplicate(patch->localRHS, &patch->localUpdate);CHKERRQ(ierr);
18814bbf5ea8SMatthew G. Knepley     ierr = PCPatchCreateCellPatches(pc);CHKERRQ(ierr);
18824bbf5ea8SMatthew G. Knepley     ierr = PCPatchCreateCellPatchDiscretisationInfo(pc);CHKERRQ(ierr);
18834bbf5ea8SMatthew G. Knepley 
18844bbf5ea8SMatthew G. Knepley     /* OK, now build the work vectors */
18854bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, &pEnd);CHKERRQ(ierr);
18861202d238SPatrick Farrell     ierr = PetscMalloc1(patch->npatch, &patch->patchRHS);CHKERRQ(ierr);
18871202d238SPatrick Farrell     ierr = PetscMalloc1(patch->npatch, &patch->patchUpdate);CHKERRQ(ierr);
1888c2e6f3c0SFlorian Wechsung 
188961c4b389SFlorian Wechsung     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
18901202d238SPatrick Farrell       ierr = PetscMalloc1(patch->npatch, &patch->patchRHSWithArtificial);CHKERRQ(ierr);
1891c2e6f3c0SFlorian Wechsung       ierr = PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithArtificial);CHKERRQ(ierr);
1892c2e6f3c0SFlorian Wechsung     }
1893*0904074fSPatrick Farrell     if (isNonlinear) {
1894*0904074fSPatrick Farrell       ierr = PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithAll);CHKERRQ(ierr);
1895*0904074fSPatrick Farrell     }
18964bbf5ea8SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
18974bbf5ea8SMatthew G. Knepley       PetscInt dof;
18984bbf5ea8SMatthew G. Knepley 
18994bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr);
19001202d238SPatrick Farrell       ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchRHS[p-pStart]);CHKERRQ(ierr);
19011202d238SPatrick Farrell       ierr = VecSetUp(patch->patchRHS[p-pStart]);CHKERRQ(ierr);
19021202d238SPatrick Farrell       ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchUpdate[p-pStart]);CHKERRQ(ierr);
19031202d238SPatrick Farrell       ierr = VecSetUp(patch->patchUpdate[p-pStart]);CHKERRQ(ierr);
1904*0904074fSPatrick Farrell       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
19053bb0e8f7SKarl Rupp         const PetscInt    *gtolArray, *gtolArrayWithArtificial = NULL;
19063bb0e8f7SKarl Rupp         PetscInt           numPatchDofs, offset;
19073bb0e8f7SKarl Rupp         PetscInt           numPatchDofsWithArtificial, offsetWithArtificial;
19083bb0e8f7SKarl Rupp         PetscInt           dofWithoutArtificialCounter = 0;
19093bb0e8f7SKarl Rupp         PetscInt          *patchWithoutArtificialToWithArtificialArray;
19103bb0e8f7SKarl Rupp 
1911c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof);CHKERRQ(ierr);
19121202d238SPatrick Farrell         ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchRHSWithArtificial[p-pStart]);CHKERRQ(ierr);
19131202d238SPatrick Farrell         ierr = VecSetUp(patch->patchRHSWithArtificial[p-pStart]);CHKERRQ(ierr);
1914c2e6f3c0SFlorian Wechsung 
1915e047a90bSFlorian Wechsung         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
1916e047a90bSFlorian Wechsung         /* the index in the patch with all dofs */
1917c2e6f3c0SFlorian Wechsung         ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
191863deea8eSPatrick Farrell 
1919c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs);CHKERRQ(ierr);
192063deea8eSPatrick Farrell 
1921c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
1922c2e6f3c0SFlorian Wechsung         ierr = ISGetIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial);CHKERRQ(ierr);
1923c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &numPatchDofsWithArtificial);CHKERRQ(ierr);
1924c2e6f3c0SFlorian Wechsung         ierr = PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offsetWithArtificial);CHKERRQ(ierr);
1925c2e6f3c0SFlorian Wechsung 
1926c2e6f3c0SFlorian Wechsung         ierr = PetscMalloc1(numPatchDofs, &patchWithoutArtificialToWithArtificialArray);CHKERRQ(ierr);
1927c2e6f3c0SFlorian Wechsung 
19283bb0e8f7SKarl Rupp         ierr = ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutArtificialToWithArtificialArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithArtificial[p-pStart]);CHKERRQ(ierr);
19297e6cf999SPatrick Farrell         if (numPatchDofs == 0) continue;
1930b0c21b6aSKarl Rupp         for (i=0; i<numPatchDofsWithArtificial; i++) {
1931e047a90bSFlorian Wechsung           if (gtolArrayWithArtificial[i+offsetWithArtificial] == gtolArray[offset+dofWithoutArtificialCounter]) {
1932c2e6f3c0SFlorian Wechsung             patchWithoutArtificialToWithArtificialArray[dofWithoutArtificialCounter] = i;
1933c2e6f3c0SFlorian Wechsung             dofWithoutArtificialCounter++;
1934c2e6f3c0SFlorian Wechsung             if (dofWithoutArtificialCounter == numPatchDofs)
1935c2e6f3c0SFlorian Wechsung               break;
1936c2e6f3c0SFlorian Wechsung           }
1937c2e6f3c0SFlorian Wechsung         }
1938c2e6f3c0SFlorian Wechsung         ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
1939c2e6f3c0SFlorian Wechsung         ierr = ISRestoreIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial);CHKERRQ(ierr);
1940c2e6f3c0SFlorian Wechsung       }
1941*0904074fSPatrick Farrell       if (isNonlinear) {
1942*0904074fSPatrick Farrell         const PetscInt    *gtolArray, *gtolArrayWithAll = NULL;
1943*0904074fSPatrick Farrell         PetscInt           numPatchDofs, offset;
1944*0904074fSPatrick Farrell         PetscInt           numPatchDofsWithAll, offsetWithAll;
1945*0904074fSPatrick Farrell         PetscInt           dofWithoutAllCounter = 0;
1946*0904074fSPatrick Farrell         PetscInt          *patchWithoutAllToWithAllArray;
1947*0904074fSPatrick Farrell 
1948*0904074fSPatrick Farrell         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
1949*0904074fSPatrick Farrell         /* the index in the patch with all dofs */
1950*0904074fSPatrick Farrell         ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
1951*0904074fSPatrick Farrell 
1952*0904074fSPatrick Farrell         ierr = PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs);CHKERRQ(ierr);
1953*0904074fSPatrick Farrell 
1954*0904074fSPatrick Farrell         ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
1955*0904074fSPatrick Farrell         ierr = ISGetIndices(patch->gtolWithAll, &gtolArrayWithAll);CHKERRQ(ierr);
1956*0904074fSPatrick Farrell         ierr = PetscSectionGetDof(patch->gtolCountsWithAll, p, &numPatchDofsWithAll);CHKERRQ(ierr);
1957*0904074fSPatrick Farrell         ierr = PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offsetWithAll);CHKERRQ(ierr);
1958*0904074fSPatrick Farrell 
1959*0904074fSPatrick Farrell         ierr = PetscMalloc1(numPatchDofs, &patchWithoutAllToWithAllArray);CHKERRQ(ierr);
1960*0904074fSPatrick Farrell 
1961*0904074fSPatrick Farrell         ierr = ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutAllToWithAllArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithAll[p-pStart]);CHKERRQ(ierr);
1962*0904074fSPatrick Farrell         if (numPatchDofs == 0) continue;
1963*0904074fSPatrick Farrell         for (i=0; i<numPatchDofsWithAll; i++) {
1964*0904074fSPatrick Farrell           if (gtolArrayWithAll[i+offsetWithAll] == gtolArray[offset+dofWithoutAllCounter]) {
1965*0904074fSPatrick Farrell             patchWithoutAllToWithAllArray[dofWithoutAllCounter] = i;
1966*0904074fSPatrick Farrell             dofWithoutAllCounter++;
1967*0904074fSPatrick Farrell             if (dofWithoutAllCounter == numPatchDofs)
1968*0904074fSPatrick Farrell               break;
1969*0904074fSPatrick Farrell           }
1970*0904074fSPatrick Farrell         }
1971*0904074fSPatrick Farrell         ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
1972*0904074fSPatrick Farrell         ierr = ISRestoreIndices(patch->gtolWithAll, &gtolArrayWithAll);CHKERRQ(ierr);
1973*0904074fSPatrick Farrell       }
19744bbf5ea8SMatthew G. Knepley     }
19754bbf5ea8SMatthew G. Knepley     if (patch->save_operators) {
19764bbf5ea8SMatthew G. Knepley       ierr = PetscMalloc1(patch->npatch, &patch->mat);CHKERRQ(ierr);
19774bbf5ea8SMatthew G. Knepley       for (i = 0; i < patch->npatch; ++i) {
1978c2e6f3c0SFlorian Wechsung         ierr = PCPatchCreateMatrix_Private(pc, i, &patch->mat[i], PETSC_FALSE);CHKERRQ(ierr);
19794bbf5ea8SMatthew G. Knepley       }
19804bbf5ea8SMatthew G. Knepley     }
19814bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr);
19824bbf5ea8SMatthew G. Knepley 
19834bbf5ea8SMatthew G. Knepley     /* If desired, calculate weights for dof multiplicity */
19844bbf5ea8SMatthew G. Knepley     if (patch->partition_of_unity) {
19853bb0e8f7SKarl Rupp       PetscScalar *input = NULL;
19863bb0e8f7SKarl Rupp       PetscScalar *output = NULL;
19873bb0e8f7SKarl Rupp       Vec global;
19883bb0e8f7SKarl Rupp 
19891202d238SPatrick Farrell       ierr = VecDuplicate(patch->localRHS, &patch->dof_weights);CHKERRQ(ierr);
199061c4b389SFlorian Wechsung       if(patch->local_composition_type == PC_COMPOSITE_ADDITIVE) {
19914bbf5ea8SMatthew G. Knepley         for (i = 0; i < patch->npatch; ++i) {
19924bbf5ea8SMatthew G. Knepley           PetscInt dof;
19934bbf5ea8SMatthew G. Knepley 
19944bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &dof);CHKERRQ(ierr);
19954bbf5ea8SMatthew G. Knepley           if (dof <= 0) continue;
19961202d238SPatrick Farrell           ierr = VecSet(patch->patchRHS[i], 1.0);CHKERRQ(ierr);
1997*0904074fSPatrick Farrell           ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchRHS[i], patch->dof_weights, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR);CHKERRQ(ierr);
19984bbf5ea8SMatthew G. Knepley         }
1999c2e6f3c0SFlorian Wechsung       } else {
2000e047a90bSFlorian Wechsung         /* multiplicative is actually only locally multiplicative and globally additive. need the pou where the mesh decomposition overlaps */
2001c2e6f3c0SFlorian Wechsung         ierr = VecSet(patch->dof_weights, 1.0);CHKERRQ(ierr);
20024bbf5ea8SMatthew G. Knepley       }
2003d132cafaSFlorian Wechsung 
2004d132cafaSFlorian Wechsung       VecDuplicate(patch->dof_weights, &global);
2005d132cafaSFlorian Wechsung       VecSet(global, 0.);
2006d132cafaSFlorian Wechsung 
2007d132cafaSFlorian Wechsung       ierr = VecGetArray(patch->dof_weights, &input);CHKERRQ(ierr);
2008d132cafaSFlorian Wechsung       ierr = VecGetArray(global, &output);CHKERRQ(ierr);
2009d132cafaSFlorian Wechsung       ierr = PetscSFReduceBegin(patch->defaultSF, MPIU_SCALAR, input, output, MPI_SUM);CHKERRQ(ierr);
2010d132cafaSFlorian Wechsung       ierr = PetscSFReduceEnd(patch->defaultSF, MPIU_SCALAR, input, output, MPI_SUM);CHKERRQ(ierr);
2011d132cafaSFlorian Wechsung       ierr = VecRestoreArray(patch->dof_weights, &input);CHKERRQ(ierr);
2012d132cafaSFlorian Wechsung       ierr = VecRestoreArray(global, &output);CHKERRQ(ierr);
2013d132cafaSFlorian Wechsung 
201405528938SFlorian Wechsung       ierr = VecReciprocal(global);CHKERRQ(ierr);
2015d132cafaSFlorian Wechsung 
2016d132cafaSFlorian Wechsung       ierr = VecGetArray(patch->dof_weights, &output);CHKERRQ(ierr);
2017d132cafaSFlorian Wechsung       ierr = VecGetArray(global, &input);CHKERRQ(ierr);
2018d132cafaSFlorian Wechsung       ierr = PetscSFBcastBegin(patch->defaultSF, MPIU_SCALAR, input, output);CHKERRQ(ierr);
2019d132cafaSFlorian Wechsung       ierr = PetscSFBcastEnd(patch->defaultSF, MPIU_SCALAR, input, output);CHKERRQ(ierr);
2020d132cafaSFlorian Wechsung       ierr = VecRestoreArray(patch->dof_weights, &output);CHKERRQ(ierr);
2021d132cafaSFlorian Wechsung       ierr = VecRestoreArray(global, &input);CHKERRQ(ierr);
2022d132cafaSFlorian Wechsung       ierr = VecDestroy(&global);CHKERRQ(ierr);
20234bbf5ea8SMatthew G. Knepley     }
202461c4b389SFlorian Wechsung     if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE && patch->save_operators) {
202596b79ebeSFlorian Wechsung       ierr = PetscMalloc1(patch->npatch, &patch->matWithArtificial);CHKERRQ(ierr);
20264bbf5ea8SMatthew G. Knepley     }
20274bbf5ea8SMatthew G. Knepley   }
2028dadc69c5SMatthew G. Knepley   ierr = (*patch->setupsolver)(pc);CHKERRQ(ierr);
2029dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
20304bbf5ea8SMatthew G. Knepley }
2031dadc69c5SMatthew G. Knepley 
2032dadc69c5SMatthew G. Knepley static PetscErrorCode PCApply_PATCH_Linear(PC pc, PetscInt i, Vec x, Vec y)
2033dadc69c5SMatthew G. Knepley {
2034dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2035dadc69c5SMatthew G. Knepley   KSP            ksp   = (KSP) patch->solver[i];
2036dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2037dadc69c5SMatthew G. Knepley 
2038dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2039dadc69c5SMatthew G. Knepley   if (!patch->save_operators) {
2040dadc69c5SMatthew G. Knepley     Mat mat;
2041dadc69c5SMatthew G. Knepley 
204234d8b122SPatrick Farrell     ierr = PCPatchCreateMatrix_Private(pc, i, &mat, PETSC_FALSE);CHKERRQ(ierr);
2043dadc69c5SMatthew G. Knepley     /* Populate operator here. */
204434d8b122SPatrick Farrell     ierr = PCPatchComputeOperator_Internal(pc, NULL, mat, i, PETSC_FALSE);CHKERRQ(ierr);
2045dadc69c5SMatthew G. Knepley     ierr = KSPSetOperators(ksp, mat, mat);CHKERRQ(ierr);
2046dadc69c5SMatthew G. Knepley     /* Drop reference so the KSPSetOperators below will blow it away. */
2047dadc69c5SMatthew G. Knepley     ierr = MatDestroy(&mat);CHKERRQ(ierr);
2048dadc69c5SMatthew G. Knepley   }
2049dadc69c5SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr);
2050dadc69c5SMatthew G. Knepley   if (!ksp->setfromoptionscalled) {
2051dadc69c5SMatthew G. Knepley     ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
2052dadc69c5SMatthew G. Knepley   }
2053dadc69c5SMatthew G. Knepley   ierr = KSPSolve(ksp, x, y);CHKERRQ(ierr);
2054dadc69c5SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr);
2055dadc69c5SMatthew G. Knepley   if (!patch->save_operators) {
2056dadc69c5SMatthew G. Knepley     PC pc;
2057dadc69c5SMatthew G. Knepley     ierr = KSPSetOperators(ksp, NULL, NULL);CHKERRQ(ierr);
2058dadc69c5SMatthew G. Knepley     ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
2059dadc69c5SMatthew G. Knepley     /* Destroy PC context too, otherwise the factored matrix hangs around. */
2060dadc69c5SMatthew G. Knepley     ierr = PCReset(pc);CHKERRQ(ierr);
20614bbf5ea8SMatthew G. Knepley   }
20624bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
20634bbf5ea8SMatthew G. Knepley }
20644bbf5ea8SMatthew G. Knepley 
20656c9c532dSPatrick Farrell static PetscErrorCode PCUpdateMultiplicative_PATCH_Linear(PC pc, PetscInt i, PetscInt pStart)
20666c9c532dSPatrick Farrell {
20676c9c532dSPatrick Farrell   PC_PATCH      *patch = (PC_PATCH *) pc->data;
20686c9c532dSPatrick Farrell   Mat multMat;
20696c9c532dSPatrick Farrell   PetscErrorCode ierr;
20706c9c532dSPatrick Farrell 
20714d04e9f1SPatrick Farrell   PetscFunctionBegin;
20724d04e9f1SPatrick Farrell 
20736c9c532dSPatrick Farrell   if (patch->save_operators) {
20746c9c532dSPatrick Farrell     multMat = patch->matWithArtificial[i];
20756c9c532dSPatrick Farrell   } else {
20766c9c532dSPatrick Farrell     /*Very inefficient, hopefully we can just assemble the rectangular matrix in the first place.*/
20776c9c532dSPatrick Farrell     Mat matSquare;
20786c9c532dSPatrick Farrell     PetscInt dof;
20796c9c532dSPatrick Farrell     IS rowis;
20806c9c532dSPatrick Farrell     ierr = PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE);CHKERRQ(ierr);
20816c9c532dSPatrick Farrell     ierr = MatZeroEntries(matSquare);CHKERRQ(ierr);
20826c9c532dSPatrick Farrell     ierr = PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE);CHKERRQ(ierr);
20836c9c532dSPatrick Farrell     ierr = MatGetSize(matSquare, &dof, NULL);CHKERRQ(ierr);
20846c9c532dSPatrick Farrell     ierr = ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis); CHKERRQ(ierr);
20856c9c532dSPatrick Farrell     ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &multMat); CHKERRQ(ierr);
20866c9c532dSPatrick Farrell     ierr = MatDestroy(&matSquare);CHKERRQ(ierr);
20876c9c532dSPatrick Farrell     ierr = ISDestroy(&rowis); CHKERRQ(ierr);
20886c9c532dSPatrick Farrell   }
20896c9c532dSPatrick Farrell   ierr = MatMult(multMat, patch->patchUpdate[i], patch->patchRHSWithArtificial[i]); CHKERRQ(ierr);
20906c9c532dSPatrick Farrell   ierr = VecScale(patch->patchRHSWithArtificial[i], -1.0); CHKERRQ(ierr);
2091*0904074fSPatrick Farrell   ierr = PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHSWithArtificial[i], patch->localRHS, ADD_VALUES, SCATTER_REVERSE, SCATTER_WITHARTIFICIAL); CHKERRQ(ierr);
20926c9c532dSPatrick Farrell   if (!patch->save_operators) {
20936c9c532dSPatrick Farrell     ierr = MatDestroy(&multMat); CHKERRQ(ierr);
20946c9c532dSPatrick Farrell   }
20954d04e9f1SPatrick Farrell   PetscFunctionReturn(0);
20966c9c532dSPatrick Farrell }
20976c9c532dSPatrick Farrell 
20984bbf5ea8SMatthew G. Knepley static PetscErrorCode PCApply_PATCH(PC pc, Vec x, Vec y)
20994bbf5ea8SMatthew G. Knepley {
21004bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch    = (PC_PATCH *) pc->data;
21011202d238SPatrick Farrell   const PetscScalar *globalRHS  = NULL;
21021202d238SPatrick Farrell   PetscScalar       *localRHS   = NULL;
21031202d238SPatrick Farrell   PetscScalar       *globalUpdate  = NULL;
21044bbf5ea8SMatthew G. Knepley   const PetscInt    *bcNodes  = NULL;
21054bbf5ea8SMatthew G. Knepley   PetscInt           nsweep   = patch->symmetrise_sweep ? 2 : 1;
21064bbf5ea8SMatthew G. Knepley   PetscInt           start[2] = {0, 0};
21074bbf5ea8SMatthew G. Knepley   PetscInt           end[2]   = {-1, -1};
21084bbf5ea8SMatthew G. Knepley   const PetscInt     inc[2]   = {1, -1};
21091202d238SPatrick Farrell   const PetscScalar *localUpdate;
21104bbf5ea8SMatthew G. Knepley   const PetscInt    *iterationSet;
21114bbf5ea8SMatthew G. Knepley   PetscInt           pStart, numBcs, n, sweep, bc, j;
21124bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
21134bbf5ea8SMatthew G. Knepley 
21144bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
21154bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr);
21164bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsPushGetViewerOff(PETSC_TRUE);CHKERRQ(ierr);
211792d50984SMatthew G. Knepley   /* start, end, inc have 2 entries to manage a second backward sweep if we symmetrize */
21184bbf5ea8SMatthew G. Knepley   end[0]   = patch->npatch;
21194bbf5ea8SMatthew G. Knepley   start[1] = patch->npatch-1;
21204bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
21214bbf5ea8SMatthew G. Knepley     ierr = ISGetLocalSize(patch->iterationSet, &end[0]);CHKERRQ(ierr);
21224bbf5ea8SMatthew G. Knepley     start[1] = end[0] - 1;
21234bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr);
21244bbf5ea8SMatthew G. Knepley   }
21254bbf5ea8SMatthew G. Knepley   /* Scatter from global space into overlapped local spaces */
21261202d238SPatrick Farrell   ierr = VecGetArrayRead(x, &globalRHS);CHKERRQ(ierr);
21271202d238SPatrick Farrell   ierr = VecGetArray(patch->localRHS, &localRHS);CHKERRQ(ierr);
21281202d238SPatrick Farrell   ierr = PetscSFBcastBegin(patch->defaultSF, MPIU_SCALAR, globalRHS, localRHS);CHKERRQ(ierr);
21291202d238SPatrick Farrell   ierr = PetscSFBcastEnd(patch->defaultSF, MPIU_SCALAR, globalRHS, localRHS);CHKERRQ(ierr);
21301202d238SPatrick Farrell   ierr = VecRestoreArrayRead(x, &globalRHS);CHKERRQ(ierr);
21311202d238SPatrick Farrell   ierr = VecRestoreArray(patch->localRHS, &localRHS);CHKERRQ(ierr);
21324bbf5ea8SMatthew G. Knepley 
21331202d238SPatrick Farrell   ierr = VecSet(patch->localUpdate, 0.0);CHKERRQ(ierr);
21344bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, NULL);CHKERRQ(ierr);
21354bbf5ea8SMatthew G. Knepley   for (sweep = 0; sweep < nsweep; sweep++) {
21364bbf5ea8SMatthew G. Knepley     for (j = start[sweep]; j*inc[sweep] < end[sweep]*inc[sweep]; j += inc[sweep]) {
21374bbf5ea8SMatthew G. Knepley       PetscInt i       = patch->user_patches ? iterationSet[j] : j;
21384bbf5ea8SMatthew G. Knepley       PetscInt start, len;
21394bbf5ea8SMatthew G. Knepley 
21404bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &len);CHKERRQ(ierr);
21414bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetOffset(patch->gtolCounts, i+pStart, &start);CHKERRQ(ierr);
21424bbf5ea8SMatthew G. Knepley       /* TODO: Squash out these guys in the setup as well. */
21434bbf5ea8SMatthew G. Knepley       if (len <= 0) continue;
21444bbf5ea8SMatthew G. Knepley       /* TODO: Do we need different scatters for X and Y? */
2145*0904074fSPatrick Farrell       ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->localRHS, patch->patchRHS[i], INSERT_VALUES, SCATTER_FORWARD, SCATTER_INTERIOR);CHKERRQ(ierr);
21461202d238SPatrick Farrell       ierr = (*patch->applysolver)(pc, i, patch->patchRHS[i], patch->patchUpdate[i]);CHKERRQ(ierr);
2147*0904074fSPatrick Farrell       ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchUpdate[i], patch->localUpdate, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR);CHKERRQ(ierr);
214861c4b389SFlorian Wechsung       if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
21496c9c532dSPatrick Farrell         ierr = (*patch->updatemultiplicative)(pc, i, pStart);CHKERRQ(ierr);
2150c2e6f3c0SFlorian Wechsung       }
21514bbf5ea8SMatthew G. Knepley     }
21524bbf5ea8SMatthew G. Knepley   }
21534bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {ierr = ISRestoreIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr);}
21544bbf5ea8SMatthew G. Knepley   /* XXX: should we do this on the global vector? */
215573ec7555SLawrence Mitchell   if (patch->partition_of_unity) {
21561202d238SPatrick Farrell     ierr = VecPointwiseMult(patch->localUpdate, patch->localUpdate, patch->dof_weights);CHKERRQ(ierr);
21574bbf5ea8SMatthew G. Knepley   }
21581202d238SPatrick Farrell   /* Now patch->localUpdate contains the solution of the patch solves, so we need to combine them all. */
21594bbf5ea8SMatthew G. Knepley   ierr = VecSet(y, 0.0);CHKERRQ(ierr);
21601202d238SPatrick Farrell   ierr = VecGetArray(y, &globalUpdate);CHKERRQ(ierr);
21611202d238SPatrick Farrell   ierr = VecGetArrayRead(patch->localUpdate, &localUpdate);CHKERRQ(ierr);
21621202d238SPatrick Farrell   ierr = PetscSFReduceBegin(patch->defaultSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM);CHKERRQ(ierr);
21631202d238SPatrick Farrell   ierr = PetscSFReduceEnd(patch->defaultSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM);CHKERRQ(ierr);
21641202d238SPatrick Farrell   ierr = VecRestoreArrayRead(patch->localUpdate, &localUpdate);CHKERRQ(ierr);
21654bbf5ea8SMatthew G. Knepley 
21664bbf5ea8SMatthew G. Knepley   /* Now we need to send the global BC values through */
21671202d238SPatrick Farrell   ierr = VecGetArrayRead(x, &globalRHS);CHKERRQ(ierr);
21684bbf5ea8SMatthew G. Knepley   ierr = ISGetSize(patch->globalBcNodes, &numBcs);CHKERRQ(ierr);
21694bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr);
21704bbf5ea8SMatthew G. Knepley   ierr = VecGetLocalSize(x, &n);CHKERRQ(ierr);
21714bbf5ea8SMatthew G. Knepley   for (bc = 0; bc < numBcs; ++bc) {
21724bbf5ea8SMatthew G. Knepley     const PetscInt idx = bcNodes[bc];
21731202d238SPatrick Farrell     if (idx < n) globalUpdate[idx] = globalRHS[idx];
21744bbf5ea8SMatthew G. Knepley   }
21754bbf5ea8SMatthew G. Knepley 
21764bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr);
21771202d238SPatrick Farrell   ierr = VecRestoreArrayRead(x, &globalRHS);CHKERRQ(ierr);
21781202d238SPatrick Farrell   ierr = VecRestoreArray(y, &globalUpdate);CHKERRQ(ierr);
21794bbf5ea8SMatthew G. Knepley 
21804bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsPopGetViewerOff();CHKERRQ(ierr);
21814bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr);
21824bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
21834bbf5ea8SMatthew G. Knepley }
21844bbf5ea8SMatthew G. Knepley 
2185dadc69c5SMatthew G. Knepley static PetscErrorCode PCReset_PATCH_Linear(PC pc)
2186dadc69c5SMatthew G. Knepley {
2187dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2188dadc69c5SMatthew G. Knepley   PetscInt       i;
2189dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2190dadc69c5SMatthew G. Knepley 
2191dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2192dadc69c5SMatthew G. Knepley   if (patch->solver) {
2193dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = KSPReset((KSP) patch->solver[i]);CHKERRQ(ierr);}
2194dadc69c5SMatthew G. Knepley   }
2195dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
2196dadc69c5SMatthew G. Knepley }
2197dadc69c5SMatthew G. Knepley 
21984bbf5ea8SMatthew G. Knepley static PetscErrorCode PCReset_PATCH(PC pc)
21994bbf5ea8SMatthew G. Knepley {
22004bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
22014bbf5ea8SMatthew G. Knepley   PetscInt       i;
22024bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
22034bbf5ea8SMatthew G. Knepley 
22044bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
22054bbf5ea8SMatthew G. Knepley   /* TODO: Get rid of all these ifs */
22064bbf5ea8SMatthew G. Knepley   ierr = PetscSFDestroy(&patch->defaultSF);CHKERRQ(ierr);
22074bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->cellCounts);CHKERRQ(ierr);
22085f824522SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->pointCounts);CHKERRQ(ierr);
22094bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->cellNumbering);CHKERRQ(ierr);
22104bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->gtolCounts);CHKERRQ(ierr);
22114bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->gtol);CHKERRQ(ierr);
22124bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->cells);CHKERRQ(ierr);
22135f824522SMatthew G. Knepley   ierr = ISDestroy(&patch->points);CHKERRQ(ierr);
22144bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->dofs);CHKERRQ(ierr);
22155f824522SMatthew G. Knepley   ierr = ISDestroy(&patch->offs);CHKERRQ(ierr);
22165f824522SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->patchSection);CHKERRQ(ierr);
22174bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->ghostBcNodes);CHKERRQ(ierr);
22184bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->globalBcNodes);CHKERRQ(ierr);
221911ac8bb0SFlorian Wechsung   ierr = PetscSectionDestroy(&patch->gtolCountsWithArtificial);CHKERRQ(ierr);
222011ac8bb0SFlorian Wechsung   ierr = ISDestroy(&patch->gtolWithArtificial);CHKERRQ(ierr);
222111ac8bb0SFlorian Wechsung   ierr = ISDestroy(&patch->dofsWithArtificial);CHKERRQ(ierr);
222211ac8bb0SFlorian Wechsung   ierr = ISDestroy(&patch->offsWithArtificial);CHKERRQ(ierr);
2223*0904074fSPatrick Farrell   ierr = PetscSectionDestroy(&patch->gtolCountsWithAll);CHKERRQ(ierr);
2224*0904074fSPatrick Farrell   ierr = ISDestroy(&patch->gtolWithAll);CHKERRQ(ierr);
2225*0904074fSPatrick Farrell   ierr = ISDestroy(&patch->dofsWithAll);CHKERRQ(ierr);
2226*0904074fSPatrick Farrell   ierr = ISDestroy(&patch->offsWithAll);CHKERRQ(ierr);
222711ac8bb0SFlorian Wechsung 
22284bbf5ea8SMatthew G. Knepley 
22295f824522SMatthew G. Knepley   if (patch->dofSection) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscSectionDestroy(&patch->dofSection[i]);CHKERRQ(ierr);}
22304bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->dofSection);CHKERRQ(ierr);
22314bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->bs);CHKERRQ(ierr);
22324bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->nodesPerCell);CHKERRQ(ierr);
22335f824522SMatthew G. Knepley   if (patch->cellNodeMap) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscFree(patch->cellNodeMap[i]);CHKERRQ(ierr);}
22344bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->cellNodeMap);CHKERRQ(ierr);
22354bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->subspaceOffsets);CHKERRQ(ierr);
22364bbf5ea8SMatthew G. Knepley 
2237dadc69c5SMatthew G. Knepley   ierr = (*patch->resetsolver)(pc);CHKERRQ(ierr);
22384bbf5ea8SMatthew G. Knepley 
2239e4c66b91SPatrick Farrell   if (patch->subspaces_to_exclude) {
2240e4c66b91SPatrick Farrell     PetscHSetIDestroy(&patch->subspaces_to_exclude);
2241e4c66b91SPatrick Farrell   }
2242e4c66b91SPatrick Farrell 
22431202d238SPatrick Farrell   ierr = VecDestroy(&patch->localRHS);CHKERRQ(ierr);
22441202d238SPatrick Farrell   ierr = VecDestroy(&patch->localUpdate);CHKERRQ(ierr);
22451202d238SPatrick Farrell   if (patch->patchRHS) {
22461202d238SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchRHS[i]);CHKERRQ(ierr);}
22471202d238SPatrick Farrell     ierr = PetscFree(patch->patchRHS);CHKERRQ(ierr);
22484bbf5ea8SMatthew G. Knepley   }
22491202d238SPatrick Farrell   if (patch->patchUpdate) {
22501202d238SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchUpdate[i]);CHKERRQ(ierr);}
22511202d238SPatrick Farrell     ierr = PetscFree(patch->patchUpdate);CHKERRQ(ierr);
22524bbf5ea8SMatthew G. Knepley   }
22534bbf5ea8SMatthew G. Knepley   ierr = VecDestroy(&patch->dof_weights);CHKERRQ(ierr);
22544bbf5ea8SMatthew G. Knepley   if (patch->patch_dof_weights) {
22555f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patch_dof_weights[i]);CHKERRQ(ierr);}
22564bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->patch_dof_weights);CHKERRQ(ierr);
22574bbf5ea8SMatthew G. Knepley   }
22584bbf5ea8SMatthew G. Knepley   if (patch->mat) {
22595f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->mat[i]);CHKERRQ(ierr);}
22604bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->mat);CHKERRQ(ierr);
22615f824522SMatthew G. Knepley   }
226211ac8bb0SFlorian Wechsung   if (patch->matWithArtificial) {
226311ac8bb0SFlorian Wechsung     for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->matWithArtificial[i]);CHKERRQ(ierr);}
226411ac8bb0SFlorian Wechsung     ierr = PetscFree(patch->matWithArtificial);CHKERRQ(ierr);
226511ac8bb0SFlorian Wechsung   }
22661202d238SPatrick Farrell   if (patch->patchRHSWithArtificial) {
22671202d238SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchRHSWithArtificial[i]);CHKERRQ(ierr);}
22681202d238SPatrick Farrell     ierr = PetscFree(patch->patchRHSWithArtificial);CHKERRQ(ierr);
226911ac8bb0SFlorian Wechsung   }
227096b79ebeSFlorian Wechsung   if(patch->dofMappingWithoutToWithArtificial) {
227196b79ebeSFlorian Wechsung     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->dofMappingWithoutToWithArtificial[i]);CHKERRQ(ierr);}
227296b79ebeSFlorian Wechsung     ierr = PetscFree(patch->dofMappingWithoutToWithArtificial);CHKERRQ(ierr);
227396b79ebeSFlorian Wechsung 
227496b79ebeSFlorian Wechsung   }
2275*0904074fSPatrick Farrell   if(patch->dofMappingWithoutToWithAll) {
2276*0904074fSPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->dofMappingWithoutToWithAll[i]);CHKERRQ(ierr);}
2277*0904074fSPatrick Farrell     ierr = PetscFree(patch->dofMappingWithoutToWithAll);CHKERRQ(ierr);
2278*0904074fSPatrick Farrell 
2279*0904074fSPatrick Farrell   }
22804bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr);
22815f824522SMatthew G. Knepley   if (patch->userIS) {
22825f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->userIS[i]);CHKERRQ(ierr);}
22835f824522SMatthew G. Knepley     ierr = PetscFree(patch->userIS);CHKERRQ(ierr);
22845f824522SMatthew G. Knepley   }
22854bbf5ea8SMatthew G. Knepley   patch->bs          = 0;
22864bbf5ea8SMatthew G. Knepley   patch->cellNodeMap = NULL;
22877974b488SMatthew G. Knepley   patch->nsubspaces  = 0;
22884bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->iterationSet);CHKERRQ(ierr);
22895f824522SMatthew G. Knepley 
22905f824522SMatthew G. Knepley   ierr = PetscViewerDestroy(&patch->viewerSection);CHKERRQ(ierr);
22914bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
22924bbf5ea8SMatthew G. Knepley }
22934bbf5ea8SMatthew G. Knepley 
2294dadc69c5SMatthew G. Knepley static PetscErrorCode PCDestroy_PATCH_Linear(PC pc)
22954bbf5ea8SMatthew G. Knepley {
22964bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
22974bbf5ea8SMatthew G. Knepley   PetscInt       i;
22984bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
22994bbf5ea8SMatthew G. Knepley 
23004bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2301dadc69c5SMatthew G. Knepley   if (patch->solver) {
2302dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = KSPDestroy((KSP *) &patch->solver[i]);CHKERRQ(ierr);}
2303dadc69c5SMatthew G. Knepley     ierr = PetscFree(patch->solver);CHKERRQ(ierr);
23044bbf5ea8SMatthew G. Knepley   }
2305dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
2306dadc69c5SMatthew G. Knepley }
2307dadc69c5SMatthew G. Knepley 
2308dadc69c5SMatthew G. Knepley static PetscErrorCode PCDestroy_PATCH(PC pc)
2309dadc69c5SMatthew G. Knepley {
2310dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2311dadc69c5SMatthew G. Knepley   PetscErrorCode ierr;
2312dadc69c5SMatthew G. Knepley 
2313dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2314dadc69c5SMatthew G. Knepley   ierr = PCReset_PATCH(pc);CHKERRQ(ierr);
2315dadc69c5SMatthew G. Knepley   ierr = (*patch->destroysolver)(pc);CHKERRQ(ierr);
23164bbf5ea8SMatthew G. Knepley   ierr = PetscFree(pc->data);CHKERRQ(ierr);
23174bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
23184bbf5ea8SMatthew G. Knepley }
23194bbf5ea8SMatthew G. Knepley 
23204bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetFromOptions_PATCH(PetscOptionItems *PetscOptionsObject, PC pc)
23214bbf5ea8SMatthew G. Knepley {
23224bbf5ea8SMatthew G. Knepley   PC_PATCH            *patch = (PC_PATCH *) pc->data;
23234bbf5ea8SMatthew G. Knepley   PCPatchConstructType patchConstructionType = PC_PATCH_STAR;
23245f824522SMatthew G. Knepley   char                 sub_mat_type[PETSC_MAX_PATH_LEN];
232510534d48SPatrick Farrell   char                 option[PETSC_MAX_PATH_LEN];
23265f824522SMatthew G. Knepley   const char          *prefix;
23274bbf5ea8SMatthew G. Knepley   PetscBool            flg, dimflg, codimflg;
23285f824522SMatthew G. Knepley   MPI_Comm             comm;
2329a48c39c8SPatrick Farrell   PetscInt            *ifields, nfields, k;
23304bbf5ea8SMatthew G. Knepley   PetscErrorCode       ierr;
233161c4b389SFlorian Wechsung   PCCompositeType loctype = PC_COMPOSITE_ADDITIVE;
23324bbf5ea8SMatthew G. Knepley 
23334bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
23345f824522SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) pc, &comm);CHKERRQ(ierr);
23355f824522SMatthew G. Knepley   ierr = PetscObjectGetOptionsPrefix((PetscObject) pc, &prefix);CHKERRQ(ierr);
233610534d48SPatrick Farrell   ierr = PetscOptionsHead(PetscOptionsObject, "Patch solver options");CHKERRQ(ierr);
233710534d48SPatrick Farrell 
233810534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_save_operators", patch->classname);
233910534d48SPatrick Farrell   ierr = PetscOptionsBool(option,  "Store all patch operators for lifetime of object?", "PCPatchSetSaveOperators", patch->save_operators, &patch->save_operators, &flg);CHKERRQ(ierr);
234010534d48SPatrick Farrell 
234110534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_partition_of_unity", patch->classname);
234210534d48SPatrick Farrell   ierr = PetscOptionsBool(option, "Weight contributions by dof multiplicity?", "PCPatchSetPartitionOfUnity", patch->partition_of_unity, &patch->partition_of_unity, &flg);CHKERRQ(ierr);
234310534d48SPatrick Farrell 
234410534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_local_type", patch->classname);
234510534d48SPatrick Farrell   ierr = PetscOptionsEnum(option,"Type of local solver composition (additive or multiplicative)","PCPatchSetLocalComposition",PCCompositeTypes,(PetscEnum)loctype,(PetscEnum*)&loctype,&flg);CHKERRQ(ierr);
234661c4b389SFlorian Wechsung   if(flg) { ierr = PCPatchSetLocalComposition(pc, loctype);CHKERRQ(ierr);}
234710534d48SPatrick Farrell 
234810534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_dim", patch->classname);
234910534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "What dimension of mesh point to construct patches by? (0 = vertices)", "PCPATCH", patch->dim, &patch->dim, &dimflg);CHKERRQ(ierr);
235010534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_codim", patch->classname);
235110534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "What co-dimension of mesh point to construct patches by? (0 = cells)", "PCPATCH", patch->codim, &patch->codim, &codimflg);CHKERRQ(ierr);
235261c4b389SFlorian Wechsung   if (dimflg && codimflg) {SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Can only set one of dimension or co-dimension");CHKERRQ(ierr);}
235310534d48SPatrick Farrell 
235410534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_type", patch->classname);
235510534d48SPatrick Farrell   ierr = PetscOptionsEnum(option, "How should the patches be constructed?", "PCPatchSetConstructType", PCPatchConstructTypes, (PetscEnum) patchConstructionType, (PetscEnum *) &patchConstructionType, &flg);CHKERRQ(ierr);
23564bbf5ea8SMatthew G. Knepley   if (flg) {ierr = PCPatchSetConstructType(pc, patchConstructionType, NULL, NULL);CHKERRQ(ierr);}
235710534d48SPatrick Farrell 
235810534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_vanka_dim", patch->classname);
235910534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "Topological dimension of entities for Vanka to ignore", "PCPATCH", patch->vankadim, &patch->vankadim, &flg);CHKERRQ(ierr);
236010534d48SPatrick Farrell 
236110534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_ignore_dim", patch->classname);
236210534d48SPatrick Farrell   ierr = PetscOptionsInt(option, "Topological dimension of entities for completion to ignore", "PCPATCH", patch->ignoredim, &patch->ignoredim, &flg);CHKERRQ(ierr);
236310534d48SPatrick Farrell 
236410534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_sub_mat_type", patch->classname);
236510534d48SPatrick Farrell   ierr = PetscOptionsFList(option, "Matrix type for patch solves", "PCPatchSetSubMatType", MatList, NULL, sub_mat_type, PETSC_MAX_PATH_LEN, &flg);CHKERRQ(ierr);
23664bbf5ea8SMatthew G. Knepley   if (flg) {ierr = PCPatchSetSubMatType(pc, sub_mat_type);CHKERRQ(ierr);}
236710534d48SPatrick Farrell 
236810534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_symmetrise_sweep", patch->classname);
236910534d48SPatrick Farrell   ierr = PetscOptionsBool(option, "Go start->end, end->start?", "PCPATCH", patch->symmetrise_sweep, &patch->symmetrise_sweep, &flg);CHKERRQ(ierr);
2370e4c66b91SPatrick Farrell 
2371a48c39c8SPatrick Farrell   /* If the user has set the number of subspaces, use that for the buffer size,
2372a48c39c8SPatrick Farrell      otherwise use a large number */
2373a48c39c8SPatrick Farrell   if (patch->nsubspaces <= 0) {
2374a48c39c8SPatrick Farrell     nfields = 128;
2375a48c39c8SPatrick Farrell   } else {
2376a48c39c8SPatrick Farrell     nfields = patch->nsubspaces;
2377a48c39c8SPatrick Farrell   }
2378a48c39c8SPatrick Farrell   ierr = PetscMalloc1(nfields, &ifields);CHKERRQ(ierr);
237910534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exclude_subspaces", patch->classname);
238010534d48SPatrick Farrell   ierr = PetscOptionsGetIntArray(((PetscObject)pc)->options,((PetscObject)pc)->prefix,option,ifields,&nfields,&flg);CHKERRQ(ierr);
2381e4c66b91SPatrick 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");
2382e4c66b91SPatrick Farrell   if (flg) {
2383e4c66b91SPatrick Farrell     PetscHSetIClear(patch->subspaces_to_exclude);
238459b66c28SPatrick Farrell     for (k = 0; k < nfields; k++) {
2385e4c66b91SPatrick Farrell       PetscHSetIAdd(patch->subspaces_to_exclude, ifields[k]);
2386e4c66b91SPatrick Farrell     }
2387e4c66b91SPatrick Farrell   }
238859b66c28SPatrick Farrell   ierr = PetscFree(ifields);CHKERRQ(ierr);
23895f824522SMatthew G. Knepley 
239010534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_patches_view", patch->classname);
239110534d48SPatrick Farrell   ierr = PetscOptionsBool(option, "Print out information during patch construction", "PCPATCH", patch->viewPatches, &patch->viewPatches, &flg);CHKERRQ(ierr);
239210534d48SPatrick Farrell 
239310534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_cells_view", patch->classname);
239410534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options,prefix, option,   &patch->viewerCells,   &patch->formatCells,   &patch->viewCells);CHKERRQ(ierr);
239510534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_points_view", patch->classname);
239610534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options,prefix, option,  &patch->viewerPoints,  &patch->formatPoints,  &patch->viewPoints);CHKERRQ(ierr);
239710534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_section_view", patch->classname);
239810534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options,prefix, option, &patch->viewerSection, &patch->formatSection, &patch->viewSection);CHKERRQ(ierr);
239910534d48SPatrick Farrell   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_mat_view", patch->classname);
240010534d48SPatrick Farrell   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options,prefix, option,     &patch->viewerMatrix,  &patch->formatMatrix,  &patch->viewMatrix);CHKERRQ(ierr);
24014bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsTail();CHKERRQ(ierr);
24025f824522SMatthew G. Knepley   patch->optionsSet = PETSC_TRUE;
24034bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
24044bbf5ea8SMatthew G. Knepley }
24054bbf5ea8SMatthew G. Knepley 
24064bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUpOnBlocks_PATCH(PC pc)
24074bbf5ea8SMatthew G. Knepley {
24084bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch = (PC_PATCH*) pc->data;
24094bbf5ea8SMatthew G. Knepley   KSPConvergedReason reason;
24104bbf5ea8SMatthew G. Knepley   PetscInt           i;
24114bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
24124bbf5ea8SMatthew G. Knepley 
24134bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2414a1eac568SLawrence Mitchell   if (!patch->save_operators) {
2415a1eac568SLawrence Mitchell     /* Can't do this here because the sub KSPs don't have an operator attached yet. */
2416a1eac568SLawrence Mitchell     PetscFunctionReturn(0);
2417a1eac568SLawrence Mitchell   }
24184bbf5ea8SMatthew G. Knepley   for (i = 0; i < patch->npatch; ++i) {
2419dadc69c5SMatthew G. Knepley     if (!((KSP) patch->solver[i])->setfromoptionscalled) {
2420dadc69c5SMatthew G. Knepley       ierr = KSPSetFromOptions((KSP) patch->solver[i]);CHKERRQ(ierr);
2421a1eac568SLawrence Mitchell     }
2422dadc69c5SMatthew G. Knepley     ierr = KSPSetUp((KSP) patch->solver[i]);CHKERRQ(ierr);
2423dadc69c5SMatthew G. Knepley     ierr = KSPGetConvergedReason((KSP) patch->solver[i], &reason);CHKERRQ(ierr);
24244bbf5ea8SMatthew G. Knepley     if (reason == KSP_DIVERGED_PCSETUP_FAILED) pc->failedreason = PC_SUBPC_ERROR;
24254bbf5ea8SMatthew G. Knepley   }
24264bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
24274bbf5ea8SMatthew G. Knepley }
24284bbf5ea8SMatthew G. Knepley 
24294bbf5ea8SMatthew G. Knepley static PetscErrorCode PCView_PATCH(PC pc, PetscViewer viewer)
24304bbf5ea8SMatthew G. Knepley {
24314bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
24324bbf5ea8SMatthew G. Knepley   PetscViewer    sviewer;
24334bbf5ea8SMatthew G. Knepley   PetscBool      isascii;
24344bbf5ea8SMatthew G. Knepley   PetscMPIInt    rank;
24354bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
24364bbf5ea8SMatthew G. Knepley 
24374bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
24384bbf5ea8SMatthew G. Knepley   /* TODO Redo tabbing with set tbas in new style */
24394bbf5ea8SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii);CHKERRQ(ierr);
24404bbf5ea8SMatthew G. Knepley   if (!isascii) PetscFunctionReturn(0);
24414bbf5ea8SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) pc), &rank);CHKERRQ(ierr);
24424bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
24434bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "Subspace Correction preconditioner with %d patches\n", patch->npatch);CHKERRQ(ierr);
244461c4b389SFlorian Wechsung   if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
2445c2e6f3c0SFlorian Wechsung     ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: multiplicative\n");CHKERRQ(ierr);
2446e047a90bSFlorian Wechsung   } else {
244773ec7555SLawrence Mitchell     ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: additive\n");CHKERRQ(ierr);
2448c2e6f3c0SFlorian Wechsung   }
24494bbf5ea8SMatthew G. Knepley   if (patch->partition_of_unity) {ierr = PetscViewerASCIIPrintf(viewer, "Weighting by partition of unity\n");CHKERRQ(ierr);}
24504bbf5ea8SMatthew G. Knepley   else                           {ierr = PetscViewerASCIIPrintf(viewer, "Not weighting by partition of unity\n");CHKERRQ(ierr);}
24514bbf5ea8SMatthew G. Knepley   if (patch->symmetrise_sweep) {ierr = PetscViewerASCIIPrintf(viewer, "Symmetrising sweep (start->end, then end->start)\n");CHKERRQ(ierr);}
24524bbf5ea8SMatthew G. Knepley   else                         {ierr = PetscViewerASCIIPrintf(viewer, "Not symmetrising sweep\n");CHKERRQ(ierr);}
24534bbf5ea8SMatthew G. Knepley   if (!patch->save_operators) {ierr = PetscViewerASCIIPrintf(viewer, "Not saving patch operators (rebuilt every PCApply)\n");CHKERRQ(ierr);}
24544bbf5ea8SMatthew G. Knepley   else                        {ierr = PetscViewerASCIIPrintf(viewer, "Saving patch operators (rebuilt every PCSetUp)\n");CHKERRQ(ierr);}
24554bbf5ea8SMatthew G. Knepley   if (patch->patchconstructop == PCPatchConstruct_Star)       {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: star\n");CHKERRQ(ierr);}
24564bbf5ea8SMatthew G. Knepley   else if (patch->patchconstructop == PCPatchConstruct_Vanka) {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: Vanka\n");CHKERRQ(ierr);}
24574bbf5ea8SMatthew G. Knepley   else if (patch->patchconstructop == PCPatchConstruct_User)  {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: user-specified\n");CHKERRQ(ierr);}
24584bbf5ea8SMatthew G. Knepley   else                                                        {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: unknown\n");CHKERRQ(ierr);}
2459dadc69c5SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "Solver on patches (all same):\n");CHKERRQ(ierr);
2460dadc69c5SMatthew G. Knepley   if (patch->solver) {
24614bbf5ea8SMatthew G. Knepley     ierr = PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr);
24624bbf5ea8SMatthew G. Knepley     if (!rank) {
24634bbf5ea8SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(sviewer);CHKERRQ(ierr);
2464dadc69c5SMatthew G. Knepley       ierr = PetscObjectView(patch->solver[0], sviewer);CHKERRQ(ierr);
24654bbf5ea8SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(sviewer);CHKERRQ(ierr);
24664bbf5ea8SMatthew G. Knepley     }
24674bbf5ea8SMatthew G. Knepley     ierr = PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr);
24684bbf5ea8SMatthew G. Knepley   } else {
24694bbf5ea8SMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2470dadc69c5SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Solver not yet set.\n");CHKERRQ(ierr);
24714bbf5ea8SMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
24724bbf5ea8SMatthew G. Knepley   }
24734bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
24744bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
24754bbf5ea8SMatthew G. Knepley }
24764bbf5ea8SMatthew G. Knepley 
2477e5893cccSMatthew G. Knepley /*MC
247898ed095eSMatthew G. Knepley   PCPATCH - A PC object that encapsulates flexible definition of blocks for overlapping and non-overlapping
247998ed095eSMatthew G. Knepley             small block additive preconditioners. Block definition is based on topology from
2480e5893cccSMatthew G. Knepley             a DM and equation numbering from a PetscSection.
2481e5893cccSMatthew G. Knepley 
2482e5893cccSMatthew G. Knepley   Options Database Keys:
2483e5893cccSMatthew G. Knepley + -pc_patch_cells_view   - Views the process local cell numbers for each patch
2484e5893cccSMatthew G. Knepley . -pc_patch_points_view  - Views the process local mesh point numbers for each patch
2485e5893cccSMatthew G. Knepley . -pc_patch_g2l_view     - Views the map between global dofs and patch local dofs for each patch
2486e5893cccSMatthew G. Knepley . -pc_patch_patches_view - Views the global dofs associated with each patch and its boundary
2487e5893cccSMatthew G. Knepley - -pc_patch_sub_mat_view - Views the matrix associated with each patch
2488e5893cccSMatthew G. Knepley 
2489e5893cccSMatthew G. Knepley   Level: intermediate
2490e5893cccSMatthew G. Knepley 
2491e5893cccSMatthew G. Knepley .seealso: PCType, PCCreate(), PCSetType()
2492e5893cccSMatthew G. Knepley M*/
2493642283e9SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PCCreate_Patch(PC pc)
24944bbf5ea8SMatthew G. Knepley {
24954bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch;
24964bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
24974bbf5ea8SMatthew G. Knepley 
24984bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
24994bbf5ea8SMatthew G. Knepley   ierr = PetscNewLog(pc, &patch);CHKERRQ(ierr);
25004bbf5ea8SMatthew G. Knepley 
2501e4c66b91SPatrick Farrell   if (patch->subspaces_to_exclude) {
2502e4c66b91SPatrick Farrell     PetscHSetIDestroy(&patch->subspaces_to_exclude);
2503e4c66b91SPatrick Farrell   }
2504e4c66b91SPatrick Farrell   PetscHSetICreate(&patch->subspaces_to_exclude);
2505e4c66b91SPatrick Farrell 
250610534d48SPatrick Farrell   patch->classname = "pc";
2507debbdec3SPatrick Farrell   patch->isNonlinear = PETSC_FALSE;
250810534d48SPatrick Farrell 
25094bbf5ea8SMatthew G. Knepley   /* Set some defaults */
25105f824522SMatthew G. Knepley   patch->combined           = PETSC_FALSE;
25114bbf5ea8SMatthew G. Knepley   patch->save_operators     = PETSC_TRUE;
251261c4b389SFlorian Wechsung   patch->local_composition_type = PC_COMPOSITE_ADDITIVE;
25134bbf5ea8SMatthew G. Knepley   patch->partition_of_unity = PETSC_FALSE;
25144bbf5ea8SMatthew G. Knepley   patch->codim              = -1;
25154bbf5ea8SMatthew G. Knepley   patch->dim                = -1;
25164bbf5ea8SMatthew G. Knepley   patch->vankadim           = -1;
25175f824522SMatthew G. Knepley   patch->ignoredim          = -1;
25184bbf5ea8SMatthew G. Knepley   patch->patchconstructop   = PCPatchConstruct_Star;
25194bbf5ea8SMatthew G. Knepley   patch->symmetrise_sweep   = PETSC_FALSE;
25205f824522SMatthew G. Knepley   patch->npatch             = 0;
25214bbf5ea8SMatthew G. Knepley   patch->userIS             = NULL;
25225f824522SMatthew G. Knepley   patch->optionsSet         = PETSC_FALSE;
25234bbf5ea8SMatthew G. Knepley   patch->iterationSet       = NULL;
25244bbf5ea8SMatthew G. Knepley   patch->user_patches       = PETSC_FALSE;
25255f824522SMatthew G. Knepley   ierr = PetscStrallocpy(MATDENSE, (char **) &patch->sub_mat_type);CHKERRQ(ierr);
25265f824522SMatthew G. Knepley   patch->viewPatches        = PETSC_FALSE;
25275f824522SMatthew G. Knepley   patch->viewCells          = PETSC_FALSE;
25285f824522SMatthew G. Knepley   patch->viewPoints         = PETSC_FALSE;
25295f824522SMatthew G. Knepley   patch->viewSection        = PETSC_FALSE;
25305f824522SMatthew G. Knepley   patch->viewMatrix         = PETSC_FALSE;
2531dadc69c5SMatthew G. Knepley   patch->setupsolver        = PCSetUp_PATCH_Linear;
2532dadc69c5SMatthew G. Knepley   patch->applysolver        = PCApply_PATCH_Linear;
2533dadc69c5SMatthew G. Knepley   patch->resetsolver        = PCReset_PATCH_Linear;
2534dadc69c5SMatthew G. Knepley   patch->destroysolver      = PCDestroy_PATCH_Linear;
25356c9c532dSPatrick Farrell   patch->updatemultiplicative = PCUpdateMultiplicative_PATCH_Linear;
25364bbf5ea8SMatthew G. Knepley 
25374bbf5ea8SMatthew G. Knepley   pc->data                 = (void *) patch;
25384bbf5ea8SMatthew G. Knepley   pc->ops->apply           = PCApply_PATCH;
25394bbf5ea8SMatthew G. Knepley   pc->ops->applytranspose  = 0; /* PCApplyTranspose_PATCH; */
25404bbf5ea8SMatthew G. Knepley   pc->ops->setup           = PCSetUp_PATCH;
25414bbf5ea8SMatthew G. Knepley   pc->ops->reset           = PCReset_PATCH;
25424bbf5ea8SMatthew G. Knepley   pc->ops->destroy         = PCDestroy_PATCH;
25434bbf5ea8SMatthew G. Knepley   pc->ops->setfromoptions  = PCSetFromOptions_PATCH;
25444bbf5ea8SMatthew G. Knepley   pc->ops->setuponblocks   = PCSetUpOnBlocks_PATCH;
25454bbf5ea8SMatthew G. Knepley   pc->ops->view            = PCView_PATCH;
25464bbf5ea8SMatthew G. Knepley   pc->ops->applyrichardson = 0;
25474bbf5ea8SMatthew G. Knepley 
25484bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
25494bbf5ea8SMatthew G. Knepley }
2550