xref: /petsc/src/ksp/pc/impls/patch/pcpatch.c (revision 73ec7555a8e7d683bfc1dc0dd6cde24bd2d23ffe)
14bbf5ea8SMatthew G. Knepley #include <petsc/private/pcpatchimpl.h>     /*I "petscpc.h" I*/
25f824522SMatthew G. Knepley #include <petsc/private/dmpleximpl.h> /* For DMPlexComputeJacobian_Patch_Internal() */
34bbf5ea8SMatthew G. Knepley #include <petscsf.h>
44bbf5ea8SMatthew G. Knepley #include <petscbt.h>
55f824522SMatthew G. Knepley #include <petscds.h>
64bbf5ea8SMatthew G. Knepley 
74bbf5ea8SMatthew G. Knepley PetscLogEvent PC_Patch_CreatePatches, PC_Patch_ComputeOp, PC_Patch_Solve, PC_Patch_Scatter, PC_Patch_Apply, PC_Patch_Prealloc;
84bbf5ea8SMatthew G. Knepley 
95f824522SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode ObjectView(PetscObject obj, PetscViewer viewer, PetscViewerFormat format)
105f824522SMatthew G. Knepley {
115f824522SMatthew G. Knepley   PetscErrorCode ierr;
125f824522SMatthew G. Knepley 
135f824522SMatthew G. Knepley   ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr);
145f824522SMatthew G. Knepley   ierr = PetscObjectView(obj, viewer);CHKERRQ(ierr);
155f824522SMatthew G. Knepley   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
167974b488SMatthew G. Knepley   return(0);
175f824522SMatthew G. Knepley }
185f824522SMatthew G. Knepley 
194bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_Star(void *vpatch, DM dm, PetscInt point, PetscHashI ht)
204bbf5ea8SMatthew G. Knepley {
214bbf5ea8SMatthew G. Knepley   PetscInt       starSize;
224bbf5ea8SMatthew G. Knepley   PetscInt      *star = NULL, si;
234bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
244bbf5ea8SMatthew G. Knepley 
254bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
264bbf5ea8SMatthew G. Knepley   PetscHashIClear(ht);
274bbf5ea8SMatthew G. Knepley   /* To start with, add the point we care about */
284bbf5ea8SMatthew G. Knepley   PetscHashIAdd(ht, point, 0);
294bbf5ea8SMatthew G. Knepley   /* Loop over all the points that this point connects to */
304bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
315f824522SMatthew G. Knepley   for (si = 0; si < starSize*2; si += 2) {PetscHashIAdd(ht, star[si], 0);}
324bbf5ea8SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
334bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
344bbf5ea8SMatthew G. Knepley }
354bbf5ea8SMatthew G. Knepley 
364bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_Vanka(void *vpatch, DM dm, PetscInt point, PetscHashI ht)
374bbf5ea8SMatthew G. Knepley {
384bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) vpatch;
394bbf5ea8SMatthew G. Knepley   PetscInt       starSize;
404bbf5ea8SMatthew G. Knepley   PetscInt      *star = NULL;
414bbf5ea8SMatthew G. Knepley   PetscBool      shouldIgnore = PETSC_FALSE;
424bbf5ea8SMatthew G. Knepley   PetscInt       cStart, cEnd, iStart, iEnd, si;
434bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
444bbf5ea8SMatthew G. Knepley 
454bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
464bbf5ea8SMatthew G. Knepley   PetscHashIClear(ht);
474bbf5ea8SMatthew G. Knepley   /* To start with, add the point we care about */
484bbf5ea8SMatthew G. Knepley   PetscHashIAdd(ht, point, 0);
494bbf5ea8SMatthew G. Knepley   /* Should we ignore any points of a certain dimension? */
504bbf5ea8SMatthew G. Knepley   if (patch->vankadim >= 0) {
514bbf5ea8SMatthew G. Knepley     shouldIgnore = PETSC_TRUE;
524bbf5ea8SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, patch->vankadim, &iStart, &iEnd);CHKERRQ(ierr);
534bbf5ea8SMatthew G. Knepley   }
544bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
554bbf5ea8SMatthew G. Knepley   /* Loop over all the cells that this point connects to */
564bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
575f824522SMatthew G. Knepley   for (si = 0; si < starSize*2; si += 2) {
584bbf5ea8SMatthew G. Knepley     const PetscInt cell = star[si];
594bbf5ea8SMatthew G. Knepley     PetscInt       closureSize;
604bbf5ea8SMatthew G. Knepley     PetscInt      *closure = NULL, ci;
614bbf5ea8SMatthew G. Knepley 
624bbf5ea8SMatthew G. Knepley     if (cell < cStart || cell >= cEnd) continue;
634bbf5ea8SMatthew G. Knepley     /* now loop over all entities in the closure of that cell */
644bbf5ea8SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
655f824522SMatthew G. Knepley     for (ci = 0; ci < closureSize*2; ci += 2) {
664bbf5ea8SMatthew G. Knepley       const PetscInt newpoint = closure[ci];
674bbf5ea8SMatthew G. Knepley 
684bbf5ea8SMatthew G. Knepley       /* We've been told to ignore entities of this type.*/
694bbf5ea8SMatthew G. Knepley       if (shouldIgnore && newpoint >= iStart && newpoint < iEnd) continue;
704bbf5ea8SMatthew G. Knepley       PetscHashIAdd(ht, newpoint, 0);
714bbf5ea8SMatthew G. Knepley     }
724bbf5ea8SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
734bbf5ea8SMatthew G. Knepley   }
744bbf5ea8SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
754bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
764bbf5ea8SMatthew G. Knepley }
774bbf5ea8SMatthew G. Knepley 
784bbf5ea8SMatthew G. Knepley /* The user's already set the patches in patch->userIS. Build the hash tables */
794bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_User(void *vpatch, DM dm, PetscInt point, PetscHashI ht)
804bbf5ea8SMatthew G. Knepley {
814bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch   = (PC_PATCH *) vpatch;
824bbf5ea8SMatthew G. Knepley   IS              patchis = patch->userIS[point];
834bbf5ea8SMatthew G. Knepley   PetscInt        n;
844bbf5ea8SMatthew G. Knepley   const PetscInt *patchdata;
854bbf5ea8SMatthew G. Knepley   PetscInt        pStart, pEnd, i;
864bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
874bbf5ea8SMatthew G. Knepley 
884bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
894bbf5ea8SMatthew G. Knepley   PetscHashIClear(ht);
904bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);
914bbf5ea8SMatthew G. Knepley   ierr = ISGetLocalSize(patchis, &n);CHKERRQ(ierr);
924bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patchis, &patchdata);CHKERRQ(ierr);
934bbf5ea8SMatthew G. Knepley   for (i = 0; i < n; ++i) {
944bbf5ea8SMatthew G. Knepley     const PetscInt ownedpoint = patchdata[i];
954bbf5ea8SMatthew G. Knepley 
964bbf5ea8SMatthew G. Knepley     if (ownedpoint < pStart || ownedpoint >= pEnd) {
974bbf5ea8SMatthew G. Knepley       SETERRQ3(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D was not in [%D, %D)", ownedpoint, pStart, pEnd);
984bbf5ea8SMatthew G. Knepley     }
994bbf5ea8SMatthew G. Knepley     PetscHashIAdd(ht, ownedpoint, 0);
1004bbf5ea8SMatthew G. Knepley   }
1014bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patchis, &patchdata);CHKERRQ(ierr);
1024bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
1034bbf5ea8SMatthew G. Knepley }
1044bbf5ea8SMatthew G. Knepley 
1054bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateDefaultSF_Private(PC pc, PetscInt n, const PetscSF *sf, const PetscInt *bs)
1064bbf5ea8SMatthew G. Knepley {
1074bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
1084bbf5ea8SMatthew G. Knepley   PetscInt       i;
1094bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
1104bbf5ea8SMatthew G. Knepley 
1114bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1124bbf5ea8SMatthew G. Knepley   if (n == 1 && bs[0] == 1) {
1134bbf5ea8SMatthew G. Knepley     patch->defaultSF = sf[0];
1144bbf5ea8SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) patch->defaultSF);CHKERRQ(ierr);
1154bbf5ea8SMatthew G. Knepley   } else {
1164bbf5ea8SMatthew G. Knepley     PetscInt     allRoots = 0, allLeaves = 0;
1174bbf5ea8SMatthew G. Knepley     PetscInt     leafOffset = 0;
1184bbf5ea8SMatthew G. Knepley     PetscInt    *ilocal = NULL;
1194bbf5ea8SMatthew G. Knepley     PetscSFNode *iremote = NULL;
1204bbf5ea8SMatthew G. Knepley     PetscInt    *remoteOffsets = NULL;
1214bbf5ea8SMatthew G. Knepley     PetscInt     index = 0;
1224bbf5ea8SMatthew G. Knepley     PetscHashI   rankToIndex;
1234bbf5ea8SMatthew G. Knepley     PetscInt     numRanks = 0;
1244bbf5ea8SMatthew G. Knepley     PetscSFNode *remote = NULL;
1254bbf5ea8SMatthew G. Knepley     PetscSF      rankSF;
1264bbf5ea8SMatthew G. Knepley     PetscInt    *ranks = NULL;
1274bbf5ea8SMatthew G. Knepley     PetscInt    *offsets = NULL;
1284bbf5ea8SMatthew G. Knepley     MPI_Datatype contig;
1294bbf5ea8SMatthew G. Knepley     PetscHashI   ranksUniq;
1304bbf5ea8SMatthew G. Knepley 
1314bbf5ea8SMatthew G. Knepley     /* First figure out how many dofs there are in the concatenated numbering.
1324bbf5ea8SMatthew G. Knepley      * allRoots: number of owned global dofs;
1334bbf5ea8SMatthew G. Knepley      * allLeaves: number of visible dofs (global + ghosted).
1344bbf5ea8SMatthew G. Knepley      */
1354bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
1364bbf5ea8SMatthew G. Knepley       PetscInt nroots, nleaves;
1374bbf5ea8SMatthew G. Knepley 
1384bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetGraph(sf[i], &nroots, &nleaves, NULL, NULL);CHKERRQ(ierr);
1394bbf5ea8SMatthew G. Knepley       allRoots  += nroots * bs[i];
1404bbf5ea8SMatthew G. Knepley       allLeaves += nleaves * bs[i];
1414bbf5ea8SMatthew G. Knepley     }
1424bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(allLeaves, &ilocal);CHKERRQ(ierr);
1434bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(allLeaves, &iremote);CHKERRQ(ierr);
1444bbf5ea8SMatthew G. Knepley     /* Now build an SF that just contains process connectivity. */
1454bbf5ea8SMatthew G. Knepley     PetscHashICreate(ranksUniq);
1464bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
1474bbf5ea8SMatthew G. Knepley       const PetscMPIInt *ranks = NULL;
1484bbf5ea8SMatthew G. Knepley       PetscInt           nranks, j;
1494bbf5ea8SMatthew G. Knepley 
1504bbf5ea8SMatthew G. Knepley       ierr = PetscSFSetUp(sf[i]);CHKERRQ(ierr);
1514bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetRanks(sf[i], &nranks, &ranks, NULL, NULL, NULL);CHKERRQ(ierr);
1524bbf5ea8SMatthew G. Knepley       /* These are all the ranks who communicate with me. */
1534bbf5ea8SMatthew G. Knepley       for (j = 0; j < nranks; ++j) {
1544bbf5ea8SMatthew G. Knepley         PetscHashIAdd(ranksUniq, (PetscInt) ranks[j], 0);
1554bbf5ea8SMatthew G. Knepley       }
1564bbf5ea8SMatthew G. Knepley     }
1574bbf5ea8SMatthew G. Knepley     PetscHashISize(ranksUniq, numRanks);
1584bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(numRanks, &remote);CHKERRQ(ierr);
1594bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(numRanks, &ranks);CHKERRQ(ierr);
1604bbf5ea8SMatthew G. Knepley     PetscHashIGetKeys(ranksUniq, &index, ranks);
1614bbf5ea8SMatthew G. Knepley 
1624bbf5ea8SMatthew G. Knepley     PetscHashICreate(rankToIndex);
1634bbf5ea8SMatthew G. Knepley     for (i = 0; i < numRanks; ++i) {
1644bbf5ea8SMatthew G. Knepley       remote[i].rank  = ranks[i];
1654bbf5ea8SMatthew G. Knepley       remote[i].index = 0;
1664bbf5ea8SMatthew G. Knepley       PetscHashIAdd(rankToIndex, ranks[i], i);
1674bbf5ea8SMatthew G. Knepley     }
1684bbf5ea8SMatthew G. Knepley     ierr = PetscFree(ranks);CHKERRQ(ierr);
1694bbf5ea8SMatthew G. Knepley     PetscHashIDestroy(ranksUniq);
1704bbf5ea8SMatthew G. Knepley     ierr = PetscSFCreate(PetscObjectComm((PetscObject) pc), &rankSF);CHKERRQ(ierr);
1714bbf5ea8SMatthew G. Knepley     ierr = PetscSFSetGraph(rankSF, 1, numRanks, NULL, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
1724bbf5ea8SMatthew G. Knepley     ierr = PetscSFSetUp(rankSF);CHKERRQ(ierr);
1734bbf5ea8SMatthew G. Knepley     /* OK, use it to communicate the root offset on the remote
1744bbf5ea8SMatthew G. Knepley      * processes for each subspace. */
1754bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(n, &offsets);CHKERRQ(ierr);
1764bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(n*numRanks, &remoteOffsets);CHKERRQ(ierr);
1774bbf5ea8SMatthew G. Knepley 
1784bbf5ea8SMatthew G. Knepley     offsets[0] = 0;
1794bbf5ea8SMatthew G. Knepley     for (i = 1; i < n; ++i) {
1804bbf5ea8SMatthew G. Knepley       PetscInt nroots;
1814bbf5ea8SMatthew G. Knepley 
1824bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetGraph(sf[i-1], &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
1834bbf5ea8SMatthew G. Knepley       offsets[i] = offsets[i-1] + nroots*bs[i-1];
1844bbf5ea8SMatthew G. Knepley     }
1854bbf5ea8SMatthew G. Knepley     /* Offsets are the offsets on the current process of the
1864bbf5ea8SMatthew G. Knepley      * global dof numbering for the subspaces. */
1874bbf5ea8SMatthew G. Knepley     ierr = MPI_Type_contiguous(n, MPIU_INT, &contig);CHKERRQ(ierr);
1884bbf5ea8SMatthew G. Knepley     ierr = MPI_Type_commit(&contig);CHKERRQ(ierr);
1894bbf5ea8SMatthew G. Knepley 
1904bbf5ea8SMatthew G. Knepley     ierr = PetscSFBcastBegin(rankSF, contig, offsets, remoteOffsets);CHKERRQ(ierr);
1914bbf5ea8SMatthew G. Knepley     ierr = PetscSFBcastEnd(rankSF, contig, offsets, remoteOffsets);CHKERRQ(ierr);
1924bbf5ea8SMatthew G. Knepley     ierr = MPI_Type_free(&contig);CHKERRQ(ierr);
1934bbf5ea8SMatthew G. Knepley     ierr = PetscFree(offsets);CHKERRQ(ierr);
1944bbf5ea8SMatthew G. Knepley     ierr = PetscSFDestroy(&rankSF);CHKERRQ(ierr);
1954bbf5ea8SMatthew G. Knepley     /* Now remoteOffsets contains the offsets on the remote
1964bbf5ea8SMatthew G. Knepley      * processes who communicate with me.  So now we can
1974bbf5ea8SMatthew G. Knepley      * concatenate the list of SFs into a single one. */
1984bbf5ea8SMatthew G. Knepley     index = 0;
1994bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2004bbf5ea8SMatthew G. Knepley       const PetscSFNode *remote = NULL;
2014bbf5ea8SMatthew G. Knepley       const PetscInt    *local  = NULL;
2024bbf5ea8SMatthew G. Knepley       PetscInt           nroots, nleaves, j;
2034bbf5ea8SMatthew G. Knepley 
2044bbf5ea8SMatthew G. Knepley       ierr = PetscSFGetGraph(sf[i], &nroots, &nleaves, &local, &remote);CHKERRQ(ierr);
2054bbf5ea8SMatthew G. Knepley       for (j = 0; j < nleaves; ++j) {
2064bbf5ea8SMatthew G. Knepley         PetscInt rank = remote[j].rank;
2074bbf5ea8SMatthew G. Knepley         PetscInt idx, rootOffset, k;
2084bbf5ea8SMatthew G. Knepley 
2094bbf5ea8SMatthew G. Knepley         PetscHashIMap(rankToIndex, rank, idx);
2104bbf5ea8SMatthew G. Knepley         if (idx == -1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Didn't find rank, huh?");
2114bbf5ea8SMatthew G. Knepley         /* Offset on given rank for ith subspace */
2124bbf5ea8SMatthew G. Knepley         rootOffset = remoteOffsets[n*idx + i];
2134bbf5ea8SMatthew G. Knepley         for (k = 0; k < bs[i]; ++k) {
214*73ec7555SLawrence Mitchell           ilocal[index]        = (local ? local[j] : j)*bs[i] + k + leafOffset;
2154bbf5ea8SMatthew G. Knepley           iremote[index].rank  = remote[j].rank;
2164bbf5ea8SMatthew G. Knepley           iremote[index].index = remote[j].index*bs[i] + k + rootOffset;
2174bbf5ea8SMatthew G. Knepley           ++index;
2184bbf5ea8SMatthew G. Knepley         }
2194bbf5ea8SMatthew G. Knepley       }
2204bbf5ea8SMatthew G. Knepley       leafOffset += nleaves * bs[i];
2214bbf5ea8SMatthew G. Knepley     }
2224bbf5ea8SMatthew G. Knepley     PetscHashIDestroy(rankToIndex);
2234bbf5ea8SMatthew G. Knepley     ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
2244bbf5ea8SMatthew G. Knepley     ierr = PetscSFCreate(PetscObjectComm((PetscObject)pc), &patch->defaultSF);CHKERRQ(ierr);
2254bbf5ea8SMatthew G. Knepley     ierr = PetscSFSetGraph(patch->defaultSF, allRoots, allLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER);CHKERRQ(ierr);
2264bbf5ea8SMatthew G. Knepley   }
2274bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
2284bbf5ea8SMatthew G. Knepley }
2294bbf5ea8SMatthew G. Knepley 
2304bbf5ea8SMatthew G. Knepley /* TODO: Docs */
2315f824522SMatthew G. Knepley PetscErrorCode PCPatchSetIgnoreDim(PC pc, PetscInt dim)
2325f824522SMatthew G. Knepley {
2335f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
2345f824522SMatthew G. Knepley   PetscFunctionBegin;
2355f824522SMatthew G. Knepley   patch->ignoredim = dim;
2365f824522SMatthew G. Knepley   PetscFunctionReturn(0);
2375f824522SMatthew G. Knepley }
2385f824522SMatthew G. Knepley 
2395f824522SMatthew G. Knepley /* TODO: Docs */
2405f824522SMatthew G. Knepley PetscErrorCode PCPatchGetIgnoreDim(PC pc, PetscInt *dim)
2415f824522SMatthew G. Knepley {
2425f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
2435f824522SMatthew G. Knepley   PetscFunctionBegin;
2445f824522SMatthew G. Knepley   *dim = patch->ignoredim;
2455f824522SMatthew G. Knepley   PetscFunctionReturn(0);
2465f824522SMatthew G. Knepley }
2475f824522SMatthew G. Knepley 
2485f824522SMatthew G. Knepley /* TODO: Docs */
2494bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetSaveOperators(PC pc, PetscBool flg)
2504bbf5ea8SMatthew G. Knepley {
2514bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
2524bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2534bbf5ea8SMatthew G. Knepley   patch->save_operators = flg;
2544bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
2554bbf5ea8SMatthew G. Knepley }
2564bbf5ea8SMatthew G. Knepley 
2574bbf5ea8SMatthew G. Knepley /* TODO: Docs */
2584bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetSaveOperators(PC pc, PetscBool *flg)
2594bbf5ea8SMatthew G. Knepley {
2604bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
2614bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2624bbf5ea8SMatthew G. Knepley   *flg = patch->save_operators;
2634bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
2644bbf5ea8SMatthew G. Knepley }
2654bbf5ea8SMatthew G. Knepley 
2664bbf5ea8SMatthew G. Knepley /* TODO: Docs */
2674bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetPartitionOfUnity(PC pc, PetscBool flg)
2684bbf5ea8SMatthew G. Knepley {
2694bbf5ea8SMatthew G. Knepley     PC_PATCH *patch = (PC_PATCH *) pc->data;
2704bbf5ea8SMatthew G. Knepley     PetscFunctionBegin;
2714bbf5ea8SMatthew G. Knepley     patch->partition_of_unity = flg;
2724bbf5ea8SMatthew G. Knepley     PetscFunctionReturn(0);
2734bbf5ea8SMatthew G. Knepley }
2744bbf5ea8SMatthew G. Knepley 
2754bbf5ea8SMatthew G. Knepley /* TODO: Docs */
2764bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetPartitionOfUnity(PC pc, PetscBool *flg)
2774bbf5ea8SMatthew G. Knepley {
2784bbf5ea8SMatthew G. Knepley     PC_PATCH *patch = (PC_PATCH *) pc->data;
2794bbf5ea8SMatthew G. Knepley     PetscFunctionBegin;
2804bbf5ea8SMatthew G. Knepley     *flg = patch->partition_of_unity;
2814bbf5ea8SMatthew G. Knepley     PetscFunctionReturn(0);
2824bbf5ea8SMatthew G. Knepley }
2834bbf5ea8SMatthew G. Knepley 
2844bbf5ea8SMatthew G. Knepley /* TODO: Docs */
2854bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetSubMatType(PC pc, MatType sub_mat_type)
2864bbf5ea8SMatthew G. Knepley {
2874bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2884bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
2894bbf5ea8SMatthew G. Knepley 
2904bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2914bbf5ea8SMatthew G. Knepley   if (patch->sub_mat_type) {ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr);}
2924bbf5ea8SMatthew G. Knepley   ierr = PetscStrallocpy(sub_mat_type, (char **) &patch->sub_mat_type);CHKERRQ(ierr);
2934bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
2944bbf5ea8SMatthew G. Knepley }
2954bbf5ea8SMatthew G. Knepley 
2964bbf5ea8SMatthew G. Knepley /* TODO: Docs */
2974bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetSubMatType(PC pc, MatType *sub_mat_type)
2984bbf5ea8SMatthew G. Knepley {
2994bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3004bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3014bbf5ea8SMatthew G. Knepley   *sub_mat_type = patch->sub_mat_type;
3024bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3034bbf5ea8SMatthew G. Knepley }
3044bbf5ea8SMatthew G. Knepley 
3054bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3064bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetCellNumbering(PC pc, PetscSection cellNumbering)
3074bbf5ea8SMatthew G. Knepley {
3084bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
3094bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
3104bbf5ea8SMatthew G. Knepley 
3114bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3124bbf5ea8SMatthew G. Knepley   patch->cellNumbering = cellNumbering;
3134bbf5ea8SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) cellNumbering);CHKERRQ(ierr);
3144bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3154bbf5ea8SMatthew G. Knepley }
3164bbf5ea8SMatthew G. Knepley 
3174bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3184bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetCellNumbering(PC pc, PetscSection *cellNumbering)
3194bbf5ea8SMatthew G. Knepley {
3204bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3214bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3224bbf5ea8SMatthew G. Knepley   *cellNumbering = patch->cellNumbering;
3234bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3244bbf5ea8SMatthew G. Knepley }
3254bbf5ea8SMatthew G. Knepley 
3264bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3274bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetConstructType(PC pc, PCPatchConstructType ctype, PetscErrorCode (*func)(PC, PetscInt *, IS **, IS *, void *), void *ctx)
3284bbf5ea8SMatthew G. Knepley {
3294bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3304bbf5ea8SMatthew G. Knepley 
3314bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3324bbf5ea8SMatthew G. Knepley   patch->ctype = ctype;
3334bbf5ea8SMatthew G. Knepley   switch (ctype) {
3344bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
3354bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Star;
3364bbf5ea8SMatthew G. Knepley     break;
3374bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
3384bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Vanka;
3394bbf5ea8SMatthew G. Knepley     break;
3404bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
3414bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
3424bbf5ea8SMatthew G. Knepley     patch->user_patches            = PETSC_TRUE;
3434bbf5ea8SMatthew G. Knepley     patch->patchconstructop        = PCPatchConstruct_User;
3444bbf5ea8SMatthew G. Knepley     patch->userpatchconstructionop = func;
3454bbf5ea8SMatthew G. Knepley     patch->userpatchconstructctx   = ctx;
3464bbf5ea8SMatthew G. Knepley     break;
3474bbf5ea8SMatthew G. Knepley   default:
3484bbf5ea8SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype);
3494bbf5ea8SMatthew G. Knepley   }
3504bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3514bbf5ea8SMatthew G. Knepley }
3524bbf5ea8SMatthew G. Knepley 
3534bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3544bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetConstructType(PC pc, PCPatchConstructType *ctype, PetscErrorCode (**func)(PC, PetscInt *, IS **, IS *, void *), void **ctx)
3554bbf5ea8SMatthew G. Knepley {
3564bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3574bbf5ea8SMatthew G. Knepley 
3584bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3594bbf5ea8SMatthew G. Knepley   *ctype = patch->ctype;
3604bbf5ea8SMatthew G. Knepley   switch (patch->ctype) {
3614bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
3624bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
3634bbf5ea8SMatthew G. Knepley     break;
3644bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
3654bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
3664bbf5ea8SMatthew G. Knepley     *func = patch->userpatchconstructionop;
3674bbf5ea8SMatthew G. Knepley     *ctx  = patch->userpatchconstructctx;
3684bbf5ea8SMatthew G. Knepley     break;
3694bbf5ea8SMatthew G. Knepley   default:
3704bbf5ea8SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype);
3714bbf5ea8SMatthew G. Knepley   }
3724bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3734bbf5ea8SMatthew G. Knepley }
3744bbf5ea8SMatthew G. Knepley 
3754bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3764bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfo(PC pc, PetscInt nsubspaces, DM *dms, PetscInt *bs, PetscInt *nodesPerCell, const PetscInt **cellNodeMap,
3774bbf5ea8SMatthew G. Knepley                                             const PetscInt *subspaceOffsets, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
3784bbf5ea8SMatthew G. Knepley {
3794bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
3805f824522SMatthew G. Knepley   DM             dm;
3814bbf5ea8SMatthew G. Knepley   PetscSF       *sfs;
3825f824522SMatthew G. Knepley   PetscInt       cStart, cEnd, i, j;
3834bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
3844bbf5ea8SMatthew G. Knepley 
3854bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3865f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
3875f824522SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
3884bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &sfs);CHKERRQ(ierr);
3894bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->dofSection);CHKERRQ(ierr);
3904bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->bs);CHKERRQ(ierr);
3914bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->nodesPerCell);CHKERRQ(ierr);
3924bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces, &patch->cellNodeMap);CHKERRQ(ierr);
3934bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(nsubspaces+1, &patch->subspaceOffsets);CHKERRQ(ierr);
3944bbf5ea8SMatthew G. Knepley 
3954bbf5ea8SMatthew G. Knepley   patch->nsubspaces       = nsubspaces;
3964bbf5ea8SMatthew G. Knepley   patch->totalDofsPerCell = 0;
3974bbf5ea8SMatthew G. Knepley   for (i = 0; i < nsubspaces; ++i) {
3984bbf5ea8SMatthew G. Knepley     ierr = DMGetDefaultSection(dms[i], &patch->dofSection[i]);CHKERRQ(ierr);
3994bbf5ea8SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) patch->dofSection[i]);CHKERRQ(ierr);
4004bbf5ea8SMatthew G. Knepley     ierr = DMGetDefaultSF(dms[i], &sfs[i]);CHKERRQ(ierr);
4014bbf5ea8SMatthew G. Knepley     patch->bs[i]              = bs[i];
4024bbf5ea8SMatthew G. Knepley     patch->nodesPerCell[i]    = nodesPerCell[i];
4034bbf5ea8SMatthew G. Knepley     patch->totalDofsPerCell  += nodesPerCell[i]*bs[i];
4045f824522SMatthew G. Knepley     ierr = PetscMalloc1((cEnd-cStart)*nodesPerCell[i]*bs[i], &patch->cellNodeMap[i]);CHKERRQ(ierr);
4055f824522SMatthew G. Knepley     for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]*bs[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
4064bbf5ea8SMatthew G. Knepley     patch->subspaceOffsets[i] = subspaceOffsets[i];
4074bbf5ea8SMatthew G. Knepley   }
4084bbf5ea8SMatthew G. Knepley   ierr = PCPatchCreateDefaultSF_Private(pc, nsubspaces, sfs, patch->bs);CHKERRQ(ierr);
4094bbf5ea8SMatthew G. Knepley   ierr = PetscFree(sfs);CHKERRQ(ierr);
4104bbf5ea8SMatthew G. Knepley 
4114bbf5ea8SMatthew G. Knepley   patch->subspaceOffsets[nsubspaces] = subspaceOffsets[nsubspaces];
4124bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes);CHKERRQ(ierr);
4134bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes);CHKERRQ(ierr);
4144bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4154bbf5ea8SMatthew G. Knepley }
4164bbf5ea8SMatthew G. Knepley 
4174bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4185f824522SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfoCombined(PC pc, DM dm, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
4195f824522SMatthew G. Knepley {
4205f824522SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
4215f824522SMatthew G. Knepley   PetscInt       cStart, cEnd, i, j;
4225f824522SMatthew G. Knepley   PetscErrorCode ierr;
4235f824522SMatthew G. Knepley 
4245f824522SMatthew G. Knepley   PetscFunctionBegin;
4255f824522SMatthew G. Knepley   patch->combined = PETSC_TRUE;
4265f824522SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
4275f824522SMatthew G. Knepley   ierr = DMGetNumFields(dm, &patch->nsubspaces);CHKERRQ(ierr);
4285f824522SMatthew G. Knepley   ierr = PetscCalloc1(patch->nsubspaces, &patch->dofSection);CHKERRQ(ierr);
4295f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->bs);CHKERRQ(ierr);
4305f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->nodesPerCell);CHKERRQ(ierr);
4315f824522SMatthew G. Knepley   ierr = PetscMalloc1(patch->nsubspaces, &patch->cellNodeMap);CHKERRQ(ierr);
4325f824522SMatthew G. Knepley   ierr = PetscCalloc1(patch->nsubspaces+1, &patch->subspaceOffsets);CHKERRQ(ierr);
4335f824522SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &patch->dofSection[0]);CHKERRQ(ierr);
4345f824522SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) patch->dofSection[0]);CHKERRQ(ierr);
4355f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(patch->dofSection[0], &patch->subspaceOffsets[patch->nsubspaces]);CHKERRQ(ierr);
4365f824522SMatthew G. Knepley   patch->totalDofsPerCell = 0;
4375f824522SMatthew G. Knepley   for (i = 0; i < patch->nsubspaces; ++i) {
4385f824522SMatthew G. Knepley     patch->bs[i]             = 1;
4395f824522SMatthew G. Knepley     patch->nodesPerCell[i]   = nodesPerCell[i];
4405f824522SMatthew G. Knepley     patch->totalDofsPerCell += nodesPerCell[i];
4415f824522SMatthew G. Knepley     ierr = PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i]);CHKERRQ(ierr);
4425f824522SMatthew G. Knepley     for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
4435f824522SMatthew G. Knepley   }
4445f824522SMatthew G. Knepley   ierr = DMGetDefaultSF(dm, &patch->defaultSF);CHKERRQ(ierr);
4455f824522SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) patch->defaultSF);CHKERRQ(ierr);
4465f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes);CHKERRQ(ierr);
4475f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes);CHKERRQ(ierr);
4485f824522SMatthew G. Knepley   PetscFunctionReturn(0);
4495f824522SMatthew G. Knepley }
4505f824522SMatthew G. Knepley 
4515f824522SMatthew G. Knepley /*@C
4525f824522SMatthew G. Knepley 
4535f824522SMatthew G. Knepley   PCPatchSetComputeOperator - Set the callback used to compute patch matrices
4545f824522SMatthew G. Knepley 
4555f824522SMatthew G. Knepley   Input Parameters:
4565f824522SMatthew G. Knepley + pc   - The PC
4575f824522SMatthew G. Knepley . func - The callback
4585f824522SMatthew G. Knepley - ctx  - The user context
4595f824522SMatthew G. Knepley 
4605f824522SMatthew G. Knepley   Level: advanced
4615f824522SMatthew G. Knepley 
4625f824522SMatthew G. Knepley   Note:
4635f824522SMatthew G. Knepley   The callback has signature:
4645f824522SMatthew G. Knepley +  usercomputeop(pc, mat, ncell, cells, n, u, ctx)
4655f824522SMatthew G. Knepley +  pc    - The PC
4665f824522SMatthew G. Knepley +  mat   - The patch matrix
4675f824522SMatthew G. Knepley +  ncell - The number of cells to integrate over
4685f824522SMatthew G. Knepley +  cells - An array of the cell numbers
4695f824522SMatthew G. Knepley +  n     - The size of g2l
4705f824522SMatthew G. Knepley +  g2l   - The global to local dof translation table
4715f824522SMatthew G. Knepley +  ctx   - The user context
4725f824522SMatthew G. Knepley   and can assume that the matrix entries have been set to zero before the call.
4735f824522SMatthew G. Knepley 
4745f824522SMatthew G. Knepley .seealso: PCPatchGetComputeOperator(), PCPatchSetDiscretisationInfo()
4755f824522SMatthew G. Knepley @*/
4765f824522SMatthew G. Knepley PetscErrorCode PCPatchSetComputeOperator(PC pc, PetscErrorCode (*func)(PC, PetscInt, Mat, PetscInt, const PetscInt [], PetscInt, const PetscInt *, void *), void *ctx)
4774bbf5ea8SMatthew G. Knepley {
4784bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4794bbf5ea8SMatthew G. Knepley 
4804bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4814bbf5ea8SMatthew G. Knepley   patch->usercomputeop  = func;
4824bbf5ea8SMatthew G. Knepley   patch->usercomputectx = ctx;
4834bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4844bbf5ea8SMatthew G. Knepley }
4854bbf5ea8SMatthew G. Knepley 
4864bbf5ea8SMatthew G. Knepley /* On entry, ht contains the topological entities whose dofs we are responsible for solving for;
4874bbf5ea8SMatthew G. Knepley    on exit, cht contains all the topological entities we need to compute their residuals.
4884bbf5ea8SMatthew G. Knepley    In full generality this should incorporate knowledge of the sparsity pattern of the matrix;
4894bbf5ea8SMatthew G. Knepley    here we assume a standard FE sparsity pattern.*/
4904bbf5ea8SMatthew G. Knepley /* TODO: Use DMPlexGetAdjacency() */
4914bbf5ea8SMatthew G. Knepley /* TODO: Look at temp buffer management for GetClosure() */
4925f824522SMatthew G. Knepley static PetscErrorCode PCPatchCompleteCellPatch(PC pc, PetscHashI ht, PetscHashI cht)
4934bbf5ea8SMatthew G. Knepley {
4945f824522SMatthew G. Knepley   DM             dm;
4954bbf5ea8SMatthew G. Knepley   PetscHashIIter hi;
4964bbf5ea8SMatthew G. Knepley   PetscInt       point;
4974bbf5ea8SMatthew G. Knepley   PetscInt      *star = NULL, *closure = NULL;
4984c954380SMatthew G. Knepley   PetscInt       ignoredim, iStart = 0, iEnd = -1, starSize, closureSize, si, ci;
4994bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
5004bbf5ea8SMatthew G. Knepley 
5014bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
5025f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
5035f824522SMatthew G. Knepley   ierr = PCPatchGetIgnoreDim(pc, &ignoredim);CHKERRQ(ierr);
5045f824522SMatthew G. Knepley   if (ignoredim >= 0) {ierr = DMPlexGetDepthStratum(dm, ignoredim, &iStart, &iEnd);CHKERRQ(ierr);}
5054bbf5ea8SMatthew G. Knepley   PetscHashIClear(cht);
5064bbf5ea8SMatthew G. Knepley   PetscHashIIterBegin(ht, hi);
5074bbf5ea8SMatthew G. Knepley   while (!PetscHashIIterAtEnd(ht, hi)) {
5084c954380SMatthew G. Knepley 
5094bbf5ea8SMatthew G. Knepley     PetscHashIIterGetKey(ht, hi, point);
5104bbf5ea8SMatthew G. Knepley     PetscHashIIterNext(ht, hi);
5114bbf5ea8SMatthew G. Knepley 
5124bbf5ea8SMatthew G. Knepley     /* Loop over all the cells that this point connects to */
5134bbf5ea8SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
5145f824522SMatthew G. Knepley     for (si = 0; si < starSize*2; si += 2) {
5154c954380SMatthew G. Knepley       const PetscInt ownedpoint = star[si];
5165f824522SMatthew G. Knepley       /* TODO Check for point in cht before running through closure again */
5174bbf5ea8SMatthew G. Knepley       /* now loop over all entities in the closure of that cell */
5184bbf5ea8SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, ownedpoint, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
5195f824522SMatthew G. Knepley       for (ci = 0; ci < closureSize*2; ci += 2) {
5204c954380SMatthew G. Knepley         const PetscInt seenpoint = closure[ci];
5215f824522SMatthew G. Knepley         if (ignoredim >= 0 && seenpoint >= iStart && seenpoint < iEnd) continue;
5224bbf5ea8SMatthew G. Knepley         PetscHashIAdd(cht, seenpoint, 0);
5234bbf5ea8SMatthew G. Knepley       }
5244bbf5ea8SMatthew G. Knepley     }
5254bbf5ea8SMatthew G. Knepley   }
5264c954380SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, 0, PETSC_TRUE, NULL, &closure);CHKERRQ(ierr);
5275f824522SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dm, 0, PETSC_FALSE, NULL, &star);CHKERRQ(ierr);
5285f824522SMatthew G. Knepley   PetscFunctionReturn(0);
5295f824522SMatthew G. Knepley }
5305f824522SMatthew G. Knepley 
5315f824522SMatthew G. Knepley static PetscErrorCode PCPatchGetGlobalDofs(PC pc, PetscSection dofSection[], PetscInt f, PetscBool combined, PetscInt p, PetscInt *dof, PetscInt *off)
5325f824522SMatthew G. Knepley {
5335f824522SMatthew G. Knepley   PetscErrorCode ierr;
5345f824522SMatthew G. Knepley 
5355f824522SMatthew G. Knepley   PetscFunctionBegin;
5365f824522SMatthew G. Knepley   if (combined) {
5375f824522SMatthew G. Knepley     if (f < 0) {
5385f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetDof(dofSection[0], p, dof);CHKERRQ(ierr);}
5395f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetOffset(dofSection[0], p, off);CHKERRQ(ierr);}
5405f824522SMatthew G. Knepley     } else {
5415f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetFieldDof(dofSection[0], p, f, dof);CHKERRQ(ierr);}
5425f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetFieldOffset(dofSection[0], p, f, off);CHKERRQ(ierr);}
5435f824522SMatthew G. Knepley     }
5445f824522SMatthew G. Knepley   } else {
5455f824522SMatthew G. Knepley     if (f < 0) {
5465f824522SMatthew G. Knepley       PC_PATCH *patch = (PC_PATCH *) pc->data;
5475f824522SMatthew G. Knepley       PetscInt  fdof, g;
5485f824522SMatthew G. Knepley 
5495f824522SMatthew G. Knepley       if (dof) {
5505f824522SMatthew G. Knepley         *dof = 0;
5515f824522SMatthew G. Knepley         for (g = 0; g < patch->nsubspaces; ++g) {
5525f824522SMatthew G. Knepley           ierr = PetscSectionGetDof(dofSection[g], p, &fdof);CHKERRQ(ierr);
5535f824522SMatthew G. Knepley           *dof += fdof;
5545f824522SMatthew G. Knepley         }
5555f824522SMatthew G. Knepley       }
5565f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetOffset(dofSection[0], p, off);CHKERRQ(ierr);}
5575f824522SMatthew G. Knepley     } else {
5585f824522SMatthew G. Knepley       if (dof) {ierr = PetscSectionGetDof(dofSection[f], p, dof);CHKERRQ(ierr);}
5595f824522SMatthew G. Knepley       if (off) {ierr = PetscSectionGetOffset(dofSection[f], p, off);CHKERRQ(ierr);}
5605f824522SMatthew G. Knepley     }
5615f824522SMatthew G. Knepley   }
5624bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5634bbf5ea8SMatthew G. Knepley }
5644bbf5ea8SMatthew G. Knepley 
5654bbf5ea8SMatthew G. Knepley /* Given a hash table with a set of topological entities (pts), compute the degrees of
5664bbf5ea8SMatthew G. Knepley    freedom in global concatenated numbering on those entities.
5674bbf5ea8SMatthew G. Knepley    For Vanka smoothing, this needs to do something special: ignore dofs of the
5684bbf5ea8SMatthew G. Knepley    constraint subspace on entities that aren't the base entity we're building the patch
5694bbf5ea8SMatthew G. Knepley    around. */
5705f824522SMatthew G. Knepley static PetscErrorCode PCPatchGetPointDofs(PC pc, PetscHashI pts, PetscHashI dofs, PetscInt base, PetscInt exclude_subspace)
5714bbf5ea8SMatthew G. Knepley {
5725f824522SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
5734bbf5ea8SMatthew G. Knepley   PetscHashIIter hi;
5744bbf5ea8SMatthew G. Knepley   PetscInt       ldof, loff;
5754bbf5ea8SMatthew G. Knepley   PetscInt       k, p;
5764bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
5774bbf5ea8SMatthew G. Knepley 
5784bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
5794bbf5ea8SMatthew G. Knepley   PetscHashIClear(dofs);
5804bbf5ea8SMatthew G. Knepley   for (k = 0; k < patch->nsubspaces; ++k) {
5814bbf5ea8SMatthew G. Knepley     PetscInt subspaceOffset = patch->subspaceOffsets[k];
5824bbf5ea8SMatthew G. Knepley     PetscInt bs             = patch->bs[k];
5834bbf5ea8SMatthew G. Knepley     PetscInt j, l;
5844bbf5ea8SMatthew G. Knepley 
5854bbf5ea8SMatthew G. Knepley     if (k == exclude_subspace) {
5864bbf5ea8SMatthew G. Knepley       /* only get this subspace dofs at the base entity, not any others */
5875f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, base, &ldof, &loff);CHKERRQ(ierr);
5884bbf5ea8SMatthew G. Knepley       if (0 == ldof) continue;
5894bbf5ea8SMatthew G. Knepley       for (j = loff; j < ldof + loff; ++j) {
5904bbf5ea8SMatthew G. Knepley         for (l = 0; l < bs; ++l) {
5914bbf5ea8SMatthew G. Knepley           PetscInt dof = bs*j + l + subspaceOffset;
5924bbf5ea8SMatthew G. Knepley           PetscHashIAdd(dofs, dof, 0);
5934bbf5ea8SMatthew G. Knepley         }
5944bbf5ea8SMatthew G. Knepley       }
5954bbf5ea8SMatthew G. Knepley       continue; /* skip the other dofs of this subspace */
5964bbf5ea8SMatthew G. Knepley     }
5974bbf5ea8SMatthew G. Knepley 
5984bbf5ea8SMatthew G. Knepley     PetscHashIIterBegin(pts, hi);
5994bbf5ea8SMatthew G. Knepley     while (!PetscHashIIterAtEnd(pts, hi)) {
6004bbf5ea8SMatthew G. Knepley       PetscHashIIterGetKey(pts, hi, p);
6014bbf5ea8SMatthew G. Knepley       PetscHashIIterNext(pts, hi);
6025f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, p, &ldof, &loff);CHKERRQ(ierr);
6034bbf5ea8SMatthew G. Knepley       if (0 == ldof) continue;
6044bbf5ea8SMatthew G. Knepley       for (j = loff; j < ldof + loff; ++j) {
6054bbf5ea8SMatthew G. Knepley         for (l = 0; l < bs; ++l) {
6064bbf5ea8SMatthew G. Knepley           PetscInt dof = bs*j + l + subspaceOffset;
6074bbf5ea8SMatthew G. Knepley           PetscHashIAdd(dofs, dof, 0);
6084bbf5ea8SMatthew G. Knepley         }
6094bbf5ea8SMatthew G. Knepley       }
6104bbf5ea8SMatthew G. Knepley     }
6114bbf5ea8SMatthew G. Knepley   }
6124bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
6134bbf5ea8SMatthew G. Knepley }
6144bbf5ea8SMatthew G. Knepley 
6154bbf5ea8SMatthew G. Knepley /* Given two hash tables A and B, compute the keys in B that are not in A, and put them in C */
6164bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchComputeSetDifference_Private(PetscHashI A, PetscHashI B, PetscHashI C)
6174bbf5ea8SMatthew G. Knepley {
6184bbf5ea8SMatthew G. Knepley   PetscHashIIter hi;
6194bbf5ea8SMatthew G. Knepley   PetscInt       key, val;
6204bbf5ea8SMatthew G. Knepley   PetscBool      flg;
6214bbf5ea8SMatthew G. Knepley 
6224bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
6234bbf5ea8SMatthew G. Knepley   PetscHashIClear(C);
6244bbf5ea8SMatthew G. Knepley   PetscHashIIterBegin(B, hi);
6254bbf5ea8SMatthew G. Knepley   while (!PetscHashIIterAtEnd(B, hi)) {
6264bbf5ea8SMatthew G. Knepley     PetscHashIIterGetKeyVal(B, hi, key, val);
6274bbf5ea8SMatthew G. Knepley     PetscHashIIterNext(B, hi);
6284bbf5ea8SMatthew G. Knepley     PetscHashIHasKey(A, key, flg);
6294bbf5ea8SMatthew G. Knepley     if (!flg) {PetscHashIAdd(C, key, val);}
6304bbf5ea8SMatthew G. Knepley   }
6314bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
6324bbf5ea8SMatthew G. Knepley }
6334bbf5ea8SMatthew G. Knepley 
6344bbf5ea8SMatthew G. Knepley /*
6354bbf5ea8SMatthew G. Knepley  * PCPatchCreateCellPatches - create patches.
6364bbf5ea8SMatthew G. Knepley  *
6374bbf5ea8SMatthew G. Knepley  * Input Parameters:
6384bbf5ea8SMatthew G. Knepley  * + dm - The DMPlex object defining the mesh
6394bbf5ea8SMatthew G. Knepley  *
6404bbf5ea8SMatthew G. Knepley  * Output Parameters:
6414bbf5ea8SMatthew G. Knepley  * + cellCounts  - Section with counts of cells around each vertex
6425f824522SMatthew G. Knepley  * . cells       - IS of the cell point indices of cells in each patch
6435f824522SMatthew G. Knepley  * . pointCounts - Section with counts of cells around each vertex
6445f824522SMatthew G. Knepley  * - point       - IS of the cell point indices of cells in each patch
6454bbf5ea8SMatthew G. Knepley  */
6464bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatches(PC pc)
6474bbf5ea8SMatthew G. Knepley {
6484bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
6495f824522SMatthew G. Knepley   DMLabel         ghost = NULL;
6504bbf5ea8SMatthew G. Knepley   DM              dm, plex;
6514bbf5ea8SMatthew G. Knepley   PetscHashI      ht, cht;
6525f824522SMatthew G. Knepley   PetscSection    cellCounts,  pointCounts;
6535f824522SMatthew G. Knepley   PetscInt       *cellsArray, *pointsArray;
6545f824522SMatthew G. Knepley   PetscInt        numCells,    numPoints;
6555f824522SMatthew G. Knepley   const PetscInt *leaves;
6565f824522SMatthew G. Knepley   PetscInt        nleaves, pStart, pEnd, cStart, cEnd, vStart, vEnd, v;
6575f824522SMatthew G. Knepley   PetscBool       isFiredrake;
6584bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
6594bbf5ea8SMatthew G. Knepley 
6604bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
6614bbf5ea8SMatthew G. Knepley   /* Used to keep track of the cells in the patch. */
6624bbf5ea8SMatthew G. Knepley   PetscHashICreate(ht);
6634bbf5ea8SMatthew G. Knepley   PetscHashICreate(cht);
6644bbf5ea8SMatthew G. Knepley 
6654bbf5ea8SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
6664bbf5ea8SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONGSTATE, "DM not yet set on patch PC\n");
6674bbf5ea8SMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
6684bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetChart(plex, &pStart, &pEnd);CHKERRQ(ierr);
6694bbf5ea8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr);
6704bbf5ea8SMatthew G. Knepley 
6714bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
6725f824522SMatthew G. Knepley     ierr = patch->userpatchconstructionop(pc, &patch->npatch, &patch->userIS, &patch->iterationSet, patch->userpatchconstructctx);CHKERRQ(ierr);
6735f824522SMatthew G. Knepley     vStart = 0; vEnd = patch->npatch;
6745f824522SMatthew G. Knepley   } else if (patch->codim < 0) {
6755f824522SMatthew G. Knepley     if (patch->dim < 0) {ierr = DMPlexGetDepthStratum(plex,  0,            &vStart, &vEnd);CHKERRQ(ierr);}
6765f824522SMatthew G. Knepley     else                {ierr = DMPlexGetDepthStratum(plex,  patch->dim,   &vStart, &vEnd);CHKERRQ(ierr);}
6775f824522SMatthew G. Knepley   } else                {ierr = DMPlexGetHeightStratum(plex, patch->codim, &vStart, &vEnd);CHKERRQ(ierr);}
6785f824522SMatthew G. Knepley   patch->npatch = vEnd - vStart;
6794bbf5ea8SMatthew G. Knepley 
6804bbf5ea8SMatthew G. Knepley   /* These labels mark the owned points.  We only create patches around points that this process owns. */
6815f824522SMatthew G. Knepley   ierr = DMHasLabel(dm, "pyop2_ghost", &isFiredrake);CHKERRQ(ierr);
6825f824522SMatthew G. Knepley   if (isFiredrake) {
6834bbf5ea8SMatthew G. Knepley     ierr = DMGetLabel(dm, "pyop2_ghost", &ghost);CHKERRQ(ierr);
6844bbf5ea8SMatthew G. Knepley     ierr = DMLabelCreateIndex(ghost, pStart, pEnd);CHKERRQ(ierr);
6855f824522SMatthew G. Knepley   } else {
6865f824522SMatthew G. Knepley     PetscSF sf;
6875f824522SMatthew G. Knepley 
6885f824522SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
6895f824522SMatthew G. Knepley     ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
6905f824522SMatthew G. Knepley     nleaves = PetscMax(nleaves, 0);
6915f824522SMatthew G. Knepley   }
6924bbf5ea8SMatthew G. Knepley 
6934bbf5ea8SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->cellCounts);CHKERRQ(ierr);
6945f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->cellCounts, "Patch Cell Layout");CHKERRQ(ierr);
6954bbf5ea8SMatthew G. Knepley   cellCounts = patch->cellCounts;
6964bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetChart(cellCounts, vStart, vEnd);CHKERRQ(ierr);
6975f824522SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->pointCounts);CHKERRQ(ierr);
6985f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->pointCounts, "Patch Point Layout");CHKERRQ(ierr);
6995f824522SMatthew G. Knepley   pointCounts = patch->pointCounts;
7005f824522SMatthew G. Knepley   ierr = PetscSectionSetChart(pointCounts, vStart, vEnd);CHKERRQ(ierr);
7015f824522SMatthew G. Knepley   /* Count cells and points in the patch surrounding each entity */
7024bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
7034bbf5ea8SMatthew G. Knepley     PetscHashIIter hi;
7045f824522SMatthew G. Knepley     PetscInt       chtSize, loc = -1;
7055f824522SMatthew G. Knepley     PetscBool      flg;
7064bbf5ea8SMatthew G. Knepley 
7074bbf5ea8SMatthew G. Knepley     if (!patch->user_patches) {
7085f824522SMatthew G. Knepley       if (ghost) {ierr = DMLabelHasPoint(ghost, v, &flg);CHKERRQ(ierr);}
7095f824522SMatthew G. Knepley       else       {ierr = PetscFindInt(v, nleaves, leaves, &loc); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;}
7104bbf5ea8SMatthew G. Knepley       /* Not an owned entity, don't make a cell patch. */
7114bbf5ea8SMatthew G. Knepley       if (flg) continue;
7124bbf5ea8SMatthew G. Knepley     }
7134bbf5ea8SMatthew G. Knepley 
7144bbf5ea8SMatthew G. Knepley     ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr);
7155f824522SMatthew G. Knepley     ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr);
7164bbf5ea8SMatthew G. Knepley     PetscHashISize(cht, chtSize);
7174bbf5ea8SMatthew G. Knepley     /* empty patch, continue */
7184bbf5ea8SMatthew G. Knepley     if (chtSize == 0) continue;
7194bbf5ea8SMatthew G. Knepley 
7204bbf5ea8SMatthew G. Knepley     /* safe because size(cht) > 0 from above */
7214bbf5ea8SMatthew G. Knepley     PetscHashIIterBegin(cht, hi);
7224bbf5ea8SMatthew G. Knepley     while (!PetscHashIIterAtEnd(cht, hi)) {
7235f824522SMatthew G. Knepley       PetscInt point, pdof;
7244bbf5ea8SMatthew G. Knepley 
7254bbf5ea8SMatthew G. Knepley       PetscHashIIterGetKey(cht, hi, point);
7265f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr);
7275f824522SMatthew G. Knepley       if (pdof)                            {ierr = PetscSectionAddDof(pointCounts, v, 1);CHKERRQ(ierr);}
7285f824522SMatthew G. Knepley       if (point >= cStart && point < cEnd) {ierr = PetscSectionAddDof(cellCounts, v, 1);CHKERRQ(ierr);}
7294bbf5ea8SMatthew G. Knepley       PetscHashIIterNext(cht, hi);
7304bbf5ea8SMatthew G. Knepley     }
7314bbf5ea8SMatthew G. Knepley   }
7325f824522SMatthew G. Knepley   if (isFiredrake) {ierr = DMLabelDestroyIndex(ghost);CHKERRQ(ierr);}
7334bbf5ea8SMatthew G. Knepley 
7344bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetUp(cellCounts);CHKERRQ(ierr);
7354bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr);
7364bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numCells, &cellsArray);CHKERRQ(ierr);
7375f824522SMatthew G. Knepley   ierr = PetscSectionSetUp(pointCounts);CHKERRQ(ierr);
7385f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(pointCounts, &numPoints);CHKERRQ(ierr);
7395f824522SMatthew G. Knepley   ierr = PetscMalloc1(numPoints, &pointsArray);CHKERRQ(ierr);
7404bbf5ea8SMatthew G. Knepley 
7414bbf5ea8SMatthew G. Knepley   /* Now that we know how much space we need, run through again and actually remember the cells. */
7424bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; v++ ) {
7434bbf5ea8SMatthew G. Knepley     PetscHashIIter hi;
7445f824522SMatthew G. Knepley     PetscInt       dof, off, cdof, coff, pdof, n = 0, cn = 0;
7454bbf5ea8SMatthew G. Knepley 
7465f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(pointCounts, v, &dof);CHKERRQ(ierr);
7475f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(pointCounts, v, &off);CHKERRQ(ierr);
7485f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &cdof);CHKERRQ(ierr);
7495f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &coff);CHKERRQ(ierr);
7505f824522SMatthew G. Knepley     if (dof <= 0) continue;
7514bbf5ea8SMatthew G. Knepley     ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr);
7525f824522SMatthew G. Knepley     ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr);
7534bbf5ea8SMatthew G. Knepley     PetscHashIIterBegin(cht, hi);
7544bbf5ea8SMatthew G. Knepley     while (!PetscHashIIterAtEnd(cht, hi)) {
7554bbf5ea8SMatthew G. Knepley       PetscInt point;
7564bbf5ea8SMatthew G. Knepley 
7574bbf5ea8SMatthew G. Knepley       PetscHashIIterGetKey(cht, hi, point);
7585f824522SMatthew G. Knepley       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr);
7595f824522SMatthew G. Knepley       if (pdof)                            {pointsArray[off + n++] = point;}
7605f824522SMatthew G. Knepley       if (point >= cStart && point < cEnd) {cellsArray[coff + cn++] = point;}
7614bbf5ea8SMatthew G. Knepley       PetscHashIIterNext(cht, hi);
7624bbf5ea8SMatthew G. Knepley     }
7635f824522SMatthew 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);
7645f824522SMatthew 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);
7654bbf5ea8SMatthew G. Knepley   }
7664bbf5ea8SMatthew G. Knepley   PetscHashIDestroy(ht);
7674bbf5ea8SMatthew G. Knepley   PetscHashIDestroy(cht);
7684bbf5ea8SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
7695f824522SMatthew G. Knepley 
7705f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numCells,  cellsArray,  PETSC_OWN_POINTER, &patch->cells);CHKERRQ(ierr);
7715f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->cells,  "Patch Cells");CHKERRQ(ierr);
7725f824522SMatthew G. Knepley   if (patch->viewCells) {
7735f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->cellCounts, patch->viewerCells, patch->formatCells);CHKERRQ(ierr);
7745f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->cells,      patch->viewerCells, patch->formatCells);CHKERRQ(ierr);
7755f824522SMatthew G. Knepley   }
7765f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints, pointsArray, PETSC_OWN_POINTER, &patch->points);CHKERRQ(ierr);
7775f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->points, "Patch Points");CHKERRQ(ierr);
7785f824522SMatthew G. Knepley   if (patch->viewPoints) {
7795f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->pointCounts, patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr);
7805f824522SMatthew G. Knepley     ierr = ObjectView((PetscObject) patch->points,      patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr);
7815f824522SMatthew G. Knepley   }
7824bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
7834bbf5ea8SMatthew G. Knepley }
7844bbf5ea8SMatthew G. Knepley 
7854bbf5ea8SMatthew G. Knepley /*
7864bbf5ea8SMatthew G. Knepley  * PCPatchCreateCellPatchDiscretisationInfo - Build the dof maps for cell patches
7874bbf5ea8SMatthew G. Knepley  *
7884bbf5ea8SMatthew G. Knepley  * Input Parameters:
7894bbf5ea8SMatthew G. Knepley  * + dm - The DMPlex object defining the mesh
7904bbf5ea8SMatthew G. Knepley  * . cellCounts - Section with counts of cells around each vertex
7914bbf5ea8SMatthew G. Knepley  * . cells - IS of the cell point indices of cells in each patch
7924bbf5ea8SMatthew G. Knepley  * . cellNumbering - Section mapping plex cell points to Firedrake cell indices.
7934bbf5ea8SMatthew G. Knepley  * . nodesPerCell - number of nodes per cell.
7944bbf5ea8SMatthew G. Knepley  * - cellNodeMap - map from cells to node indices (nodesPerCell * numCells)
7954bbf5ea8SMatthew G. Knepley  *
7964bbf5ea8SMatthew G. Knepley  * Output Parameters:
7975f824522SMatthew G. Knepley  * + dofs - IS of local dof numbers of each cell in the patch, where local is a patch local numbering
7984bbf5ea8SMatthew G. Knepley  * . gtolCounts - Section with counts of dofs per cell patch
7994bbf5ea8SMatthew G. Knepley  * - gtol - IS mapping from global dofs to local dofs for each patch.
8004bbf5ea8SMatthew G. Knepley  */
8014bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatchDiscretisationInfo(PC pc)
8024bbf5ea8SMatthew G. Knepley {
8034bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch           = (PC_PATCH *) pc->data;
8044bbf5ea8SMatthew G. Knepley   PetscSection    cellCounts      = patch->cellCounts;
8055f824522SMatthew G. Knepley   PetscSection    pointCounts     = patch->pointCounts;
8064bbf5ea8SMatthew G. Knepley   PetscSection    gtolCounts;
8074bbf5ea8SMatthew G. Knepley   IS              cells           = patch->cells;
8085f824522SMatthew G. Knepley   IS              points          = patch->points;
8094bbf5ea8SMatthew G. Knepley   PetscSection    cellNumbering   = patch->cellNumbering;
8105f824522SMatthew G. Knepley   PetscInt        Nf              = patch->nsubspaces;
8115f824522SMatthew G. Knepley   PetscInt        numCells, numPoints;
8124bbf5ea8SMatthew G. Knepley   PetscInt        numDofs;
8134bbf5ea8SMatthew G. Knepley   PetscInt        numGlobalDofs;
8144bbf5ea8SMatthew G. Knepley   PetscInt        totalDofsPerCell = patch->totalDofsPerCell;
8154bbf5ea8SMatthew G. Knepley   PetscInt        vStart, vEnd, v;
8165f824522SMatthew G. Knepley   const PetscInt *cellsArray, *pointsArray;
8174bbf5ea8SMatthew G. Knepley   PetscInt       *newCellsArray   = NULL;
8184bbf5ea8SMatthew G. Knepley   PetscInt       *dofsArray       = NULL;
8195f824522SMatthew G. Knepley   PetscInt       *offsArray       = NULL;
8204bbf5ea8SMatthew G. Knepley   PetscInt       *asmArray        = NULL;
8214bbf5ea8SMatthew G. Knepley   PetscInt       *globalDofsArray = NULL;
8224bbf5ea8SMatthew G. Knepley   PetscInt        globalIndex     = 0;
8234bbf5ea8SMatthew G. Knepley   PetscInt        key             = 0;
8244bbf5ea8SMatthew G. Knepley   PetscInt        asmKey          = 0;
8254bbf5ea8SMatthew G. Knepley   PetscHashI      ht;
8265f824522SMatthew G. Knepley   PetscInt        pStart, pEnd, p;
8274bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
8284bbf5ea8SMatthew G. Knepley 
8294bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
8304bbf5ea8SMatthew G. Knepley   /* dofcounts section is cellcounts section * dofPerCell */
8314bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr);
8325f824522SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(patch->pointCounts, &numPoints);CHKERRQ(ierr);
8334bbf5ea8SMatthew G. Knepley   numDofs = numCells * totalDofsPerCell;
8344bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numDofs, &dofsArray);CHKERRQ(ierr);
8355f824522SMatthew G. Knepley   ierr = PetscMalloc1(numPoints*Nf, &offsArray);CHKERRQ(ierr);
8364bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numDofs, &asmArray);CHKERRQ(ierr);
8374bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numCells, &newCellsArray);CHKERRQ(ierr);
8384bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(cellCounts, &vStart, &vEnd);CHKERRQ(ierr);
8394bbf5ea8SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCounts);CHKERRQ(ierr);
8404bbf5ea8SMatthew G. Knepley   gtolCounts = patch->gtolCounts;
8414bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetChart(gtolCounts, vStart, vEnd);CHKERRQ(ierr);
8425f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->gtolCounts, "Patch Global Index Section");CHKERRQ(ierr);
8434bbf5ea8SMatthew G. Knepley 
8444bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(cells, &cellsArray);CHKERRQ(ierr);
8455f824522SMatthew G. Knepley   ierr = ISGetIndices(points, &pointsArray);CHKERRQ(ierr);
8464bbf5ea8SMatthew G. Knepley   PetscHashICreate(ht);
8474bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
8484bbf5ea8SMatthew G. Knepley     PetscInt localIndex = 0;
8494bbf5ea8SMatthew G. Knepley     PetscInt dof, off, i, j, k, l;
8504bbf5ea8SMatthew G. Knepley 
8514bbf5ea8SMatthew G. Knepley     PetscHashIClear(ht);
8524bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr);
8534bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr);
8544bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
8554bbf5ea8SMatthew G. Knepley 
8564bbf5ea8SMatthew G. Knepley     for (k = 0; k < patch->nsubspaces; ++k) {
8574bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
8584bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
8594bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
8604bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
8614bbf5ea8SMatthew G. Knepley 
8624bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
8634bbf5ea8SMatthew G. Knepley         /* Walk over the cells in this patch. */
8644bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
8655f824522SMatthew G. Knepley         PetscInt       cell = c;
8664bbf5ea8SMatthew G. Knepley 
8675f824522SMatthew G. Knepley         /* TODO Change this to an IS */
8685f824522SMatthew G. Knepley         if (cellNumbering) {
8694bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetDof(cellNumbering, c, &cell);CHKERRQ(ierr);
8704bbf5ea8SMatthew G. Knepley           if (cell <= 0) SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_OUTOFRANGE, "Cell %D doesn't appear in cell numbering map", c);
8714bbf5ea8SMatthew G. Knepley           ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);
8725f824522SMatthew G. Knepley         }
8734bbf5ea8SMatthew G. Knepley         newCellsArray[i] = cell;
8744bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
8754bbf5ea8SMatthew G. Knepley           /* For each global dof, map it into contiguous local storage. */
8764bbf5ea8SMatthew G. Knepley           const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + subspaceOffset;
8774bbf5ea8SMatthew G. Knepley           /* finally, loop over block size */
8784bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
8794bbf5ea8SMatthew G. Knepley             PetscInt localDof;
8804bbf5ea8SMatthew G. Knepley 
8814bbf5ea8SMatthew G. Knepley             PetscHashIMap(ht, globalDof + l, localDof);
8824bbf5ea8SMatthew G. Knepley             if (localDof == -1) {
8834bbf5ea8SMatthew G. Knepley               localDof = localIndex++;
8844bbf5ea8SMatthew G. Knepley               PetscHashIAdd(ht, globalDof + l, localDof);
8854bbf5ea8SMatthew G. Knepley             }
8864bbf5ea8SMatthew G. Knepley             if (globalIndex >= numDofs) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
8874bbf5ea8SMatthew G. Knepley             /* And store. */
8884bbf5ea8SMatthew G. Knepley             dofsArray[globalIndex++] = localDof;
8894bbf5ea8SMatthew G. Knepley           }
8904bbf5ea8SMatthew G. Knepley         }
8914bbf5ea8SMatthew G. Knepley       }
8924bbf5ea8SMatthew G. Knepley     }
8934bbf5ea8SMatthew G. Knepley     /* How many local dofs in this patch? */
8944bbf5ea8SMatthew G. Knepley     PetscHashISize(ht, dof);
8954bbf5ea8SMatthew G. Knepley     ierr = PetscSectionSetDof(gtolCounts, v, dof);CHKERRQ(ierr);
8964bbf5ea8SMatthew G. Knepley   }
8974bbf5ea8SMatthew 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);
8984bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetUp(gtolCounts);CHKERRQ(ierr);
8994bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(gtolCounts, &numGlobalDofs);CHKERRQ(ierr);
9004bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(numGlobalDofs, &globalDofsArray);CHKERRQ(ierr);
9014bbf5ea8SMatthew G. Knepley 
9024bbf5ea8SMatthew 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. */
9034bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
9044bbf5ea8SMatthew G. Knepley     PetscHashIIter hi;
9055f824522SMatthew G. Knepley     PetscInt       dof, off, Np, ooff, i, j, k, l;
9064bbf5ea8SMatthew G. Knepley 
9074bbf5ea8SMatthew G. Knepley     PetscHashIClear(ht);
9084bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr);
9094bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr);
9105f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(pointCounts, v, &Np);CHKERRQ(ierr);
9115f824522SMatthew G. Knepley     ierr = PetscSectionGetOffset(pointCounts, v, &ooff);CHKERRQ(ierr);
9124bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
9134bbf5ea8SMatthew G. Knepley 
9144bbf5ea8SMatthew G. Knepley     for (k = 0; k < patch->nsubspaces; ++k) {
9154bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
9164bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
9174bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
9184bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
9194bbf5ea8SMatthew G. Knepley 
9204bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
9214bbf5ea8SMatthew G. Knepley         /* Reconstruct mapping of global-to-local on this patch. */
9224bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
9235f824522SMatthew G. Knepley         PetscInt       cell = c;
9244bbf5ea8SMatthew G. Knepley 
9255f824522SMatthew G. Knepley         if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);}
9264bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
9274bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
9285f824522SMatthew G. Knepley             const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset;
9294bbf5ea8SMatthew G. Knepley             const PetscInt localDof  = dofsArray[key];
9304bbf5ea8SMatthew G. Knepley 
9314bbf5ea8SMatthew G. Knepley             key += 1;
9324bbf5ea8SMatthew G. Knepley             PetscHashIAdd(ht, globalDof, localDof);
9334bbf5ea8SMatthew G. Knepley           }
9344bbf5ea8SMatthew G. Knepley         }
9354bbf5ea8SMatthew G. Knepley       }
9364bbf5ea8SMatthew G. Knepley       if (dof > 0) {
9374bbf5ea8SMatthew G. Knepley         /* Shove it in the output data structure. */
9384bbf5ea8SMatthew G. Knepley         PetscInt goff;
9394bbf5ea8SMatthew G. Knepley 
9404bbf5ea8SMatthew G. Knepley         ierr = PetscSectionGetOffset(gtolCounts, v, &goff);CHKERRQ(ierr);
9414bbf5ea8SMatthew G. Knepley         PetscHashIIterBegin(ht, hi);
9424bbf5ea8SMatthew G. Knepley         while (!PetscHashIIterAtEnd(ht, hi)) {
9434bbf5ea8SMatthew G. Knepley           PetscInt globalDof, localDof;
9444bbf5ea8SMatthew G. Knepley 
9454bbf5ea8SMatthew G. Knepley           PetscHashIIterGetKeyVal(ht, hi, globalDof, localDof);
9464bbf5ea8SMatthew G. Knepley           if (globalDof >= 0) globalDofsArray[goff + localDof] = globalDof;
9474bbf5ea8SMatthew G. Knepley           PetscHashIIterNext(ht, hi);
9484bbf5ea8SMatthew G. Knepley         }
9494bbf5ea8SMatthew G. Knepley       }
9505f824522SMatthew G. Knepley 
9515f824522SMatthew G. Knepley       for (p = 0; p < Np; ++p) {
9525f824522SMatthew G. Knepley         const PetscInt point = pointsArray[ooff + p];
9535f824522SMatthew G. Knepley         PetscInt       globalDof, localDof;
9545f824522SMatthew G. Knepley 
9555f824522SMatthew G. Knepley         ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, point, NULL, &globalDof);CHKERRQ(ierr);
9565f824522SMatthew G. Knepley         PetscHashIMap(ht, globalDof, localDof);
9575f824522SMatthew G. Knepley         offsArray[(ooff + p)*Nf + k] = localDof;
9585f824522SMatthew G. Knepley       }
9594bbf5ea8SMatthew G. Knepley     }
9604bbf5ea8SMatthew G. Knepley 
9614bbf5ea8SMatthew G. Knepley     /* At this point, we have a hash table ht built that maps globalDof -> localDof.
9624bbf5ea8SMatthew G. Knepley      We need to create the dof table laid out cellwise first, then by subspace,
9634bbf5ea8SMatthew G. Knepley      as the assembler assembles cell-wise and we need to stuff the different
9644bbf5ea8SMatthew G. Knepley      contributions of the different function spaces to the right places. So we loop
9654bbf5ea8SMatthew G. Knepley      over cells, then over subspaces. */
9664bbf5ea8SMatthew G. Knepley     if (patch->nsubspaces > 1) { /* for nsubspaces = 1, data we need is already in dofsArray */
9674bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
9684bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
9695f824522SMatthew G. Knepley         PetscInt       cell = c;
9704bbf5ea8SMatthew G. Knepley 
9715f824522SMatthew G. Knepley         if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);}
9724bbf5ea8SMatthew G. Knepley         for (k = 0; k < patch->nsubspaces; ++k) {
9734bbf5ea8SMatthew G. Knepley           const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
9744bbf5ea8SMatthew G. Knepley           PetscInt        nodesPerCell   = patch->nodesPerCell[k];
9754bbf5ea8SMatthew G. Knepley           PetscInt        subspaceOffset = patch->subspaceOffsets[k];
9764bbf5ea8SMatthew G. Knepley           PetscInt        bs             = patch->bs[k];
9774bbf5ea8SMatthew G. Knepley 
9784bbf5ea8SMatthew G. Knepley           for (j = 0; j < nodesPerCell; ++j) {
9794bbf5ea8SMatthew G. Knepley             for (l = 0; l < bs; ++l) {
9805f824522SMatthew G. Knepley               const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset;
9814bbf5ea8SMatthew G. Knepley               PetscInt       localDof;
9824bbf5ea8SMatthew G. Knepley 
9834bbf5ea8SMatthew G. Knepley               PetscHashIMap(ht, globalDof, localDof);
9844bbf5ea8SMatthew G. Knepley               asmArray[asmKey++] = localDof;
9854bbf5ea8SMatthew G. Knepley             }
9864bbf5ea8SMatthew G. Knepley           }
9874bbf5ea8SMatthew G. Knepley         }
9884bbf5ea8SMatthew G. Knepley       }
9894bbf5ea8SMatthew G. Knepley     }
9904bbf5ea8SMatthew G. Knepley   }
9914bbf5ea8SMatthew G. Knepley   if (1 == patch->nsubspaces) {ierr = PetscMemcpy(asmArray, dofsArray, numDofs * sizeof(PetscInt));CHKERRQ(ierr);}
9924bbf5ea8SMatthew G. Knepley 
9934bbf5ea8SMatthew G. Knepley   PetscHashIDestroy(ht);
9944bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(cells, &cellsArray);CHKERRQ(ierr);
9955f824522SMatthew G. Knepley   ierr = ISRestoreIndices(points, &pointsArray);CHKERRQ(ierr);
9964bbf5ea8SMatthew G. Knepley   ierr = PetscFree(dofsArray);CHKERRQ(ierr);
9975f824522SMatthew G. Knepley   /* Create placeholder section for map from points to patch dofs */
9985f824522SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->patchSection);CHKERRQ(ierr);
9995f824522SMatthew G. Knepley   ierr = PetscSectionSetNumFields(patch->patchSection, patch->nsubspaces);CHKERRQ(ierr);
10005f824522SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->dofSection[0], &pStart, &pEnd);CHKERRQ(ierr);
10015f824522SMatthew G. Knepley   ierr = PetscSectionSetChart(patch->patchSection, pStart, pEnd);CHKERRQ(ierr);
10025f824522SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
10035f824522SMatthew G. Knepley     PetscInt dof, fdof, f;
10045f824522SMatthew G. Knepley 
10055f824522SMatthew G. Knepley     ierr = PetscSectionGetDof(patch->dofSection[0], p, &dof);CHKERRQ(ierr);
10065f824522SMatthew G. Knepley     ierr = PetscSectionSetDof(patch->patchSection, p, dof);CHKERRQ(ierr);
10075f824522SMatthew G. Knepley     for (f = 0; f < patch->nsubspaces; ++f) {
10085f824522SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(patch->dofSection[0], p, f, &fdof);CHKERRQ(ierr);
10095f824522SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(patch->patchSection, p, f, fdof);CHKERRQ(ierr);
10105f824522SMatthew G. Knepley     }
10115f824522SMatthew G. Knepley   }
10125f824522SMatthew G. Knepley   ierr = PetscSectionSetUp(patch->patchSection);CHKERRQ(ierr);
10135f824522SMatthew G. Knepley   ierr = PetscSectionSetUseFieldOffsets(patch->patchSection, PETSC_TRUE);CHKERRQ(ierr);
10144bbf5ea8SMatthew G. Knepley   /* Replace cell indices with firedrake-numbered ones. */
10154bbf5ea8SMatthew G. Knepley   ierr = ISGeneralSetIndices(cells, numCells, (const PetscInt *) newCellsArray, PETSC_OWN_POINTER);CHKERRQ(ierr);
10164bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofs, globalDofsArray, PETSC_OWN_POINTER, &patch->gtol);CHKERRQ(ierr);
10175f824522SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) patch->gtol, "Global Indices");CHKERRQ(ierr);
10185f824522SMatthew G. Knepley   ierr = PetscSectionViewFromOptions(patch->gtolCounts, (PetscObject) pc, "-pc_patch_g2l_view");CHKERRQ(ierr);
10195f824522SMatthew G. Knepley   ierr = ISViewFromOptions(patch->gtol, (PetscObject) pc, "-pc_patch_g2l_view");CHKERRQ(ierr);
10204bbf5ea8SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArray, PETSC_OWN_POINTER, &patch->dofs);CHKERRQ(ierr);
10215f824522SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArray, PETSC_OWN_POINTER, &patch->offs);CHKERRQ(ierr);
10224bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
10234bbf5ea8SMatthew G. Knepley }
10244bbf5ea8SMatthew G. Knepley 
10254bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatchBCs(PC pc)
10264bbf5ea8SMatthew G. Knepley {
10274bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch      = (PC_PATCH *) pc->data;
10284bbf5ea8SMatthew G. Knepley   const PetscInt *bcNodes    = NULL;
10294bbf5ea8SMatthew G. Knepley   PetscSection    gtolCounts = patch->gtolCounts;
10304bbf5ea8SMatthew G. Knepley   IS              gtol       = patch->gtol;
10314bbf5ea8SMatthew G. Knepley   DM              dm;
10324bbf5ea8SMatthew G. Knepley   PetscInt        numBcs;
10334bbf5ea8SMatthew G. Knepley   PetscSection    bcCounts;
10344bbf5ea8SMatthew G. Knepley   PetscHashI      globalBcs, localBcs, patchDofs;
10354bbf5ea8SMatthew G. Knepley   PetscHashI      ownedpts, seenpts, owneddofs, seendofs, artificialbcs;
10364bbf5ea8SMatthew G. Knepley   PetscHashIIter  hi;
10374bbf5ea8SMatthew G. Knepley   PetscInt       *bcsArray     = NULL;
10384bbf5ea8SMatthew G. Knepley   const PetscInt *gtolArray;
10394bbf5ea8SMatthew G. Knepley   PetscInt        vStart, vEnd, v, i;
10404bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
10414bbf5ea8SMatthew G. Knepley 
10424bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
10434bbf5ea8SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
10444bbf5ea8SMatthew G. Knepley   PetscHashICreate(globalBcs);
10454bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patch->ghostBcNodes, &bcNodes);CHKERRQ(ierr);
10464bbf5ea8SMatthew G. Knepley   ierr = ISGetSize(patch->ghostBcNodes, &numBcs);CHKERRQ(ierr);
10474bbf5ea8SMatthew G. Knepley   for (i = 0; i < numBcs; ++i) {
10484bbf5ea8SMatthew G. Knepley     /* these are already in concatenated numbering */
10494bbf5ea8SMatthew G. Knepley     PetscHashIAdd(globalBcs, bcNodes[i], 0);
10504bbf5ea8SMatthew G. Knepley   }
10514bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patch->ghostBcNodes, &bcNodes);CHKERRQ(ierr);
10524bbf5ea8SMatthew G. Knepley   PetscHashICreate(patchDofs);
10534bbf5ea8SMatthew G. Knepley   PetscHashICreate(localBcs);
10544bbf5ea8SMatthew G. Knepley   PetscHashICreate(ownedpts);
10554bbf5ea8SMatthew G. Knepley   PetscHashICreate(seenpts);
10564bbf5ea8SMatthew G. Knepley   PetscHashICreate(owneddofs);
10574bbf5ea8SMatthew G. Knepley   PetscHashICreate(seendofs);
10584bbf5ea8SMatthew G. Knepley   PetscHashICreate(artificialbcs);
10594bbf5ea8SMatthew G. Knepley 
10604bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->cellCounts, &vStart, &vEnd);CHKERRQ(ierr);
10614bbf5ea8SMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->bcCounts);CHKERRQ(ierr);
10624bbf5ea8SMatthew G. Knepley   bcCounts = patch->bcCounts;
10634bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetChart(bcCounts, vStart, vEnd);CHKERRQ(ierr);
10644bbf5ea8SMatthew G. Knepley   ierr = PetscMalloc1(vEnd - vStart, &patch->bcs);CHKERRQ(ierr);
10654bbf5ea8SMatthew G. Knepley 
10664bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(gtol, &gtolArray);CHKERRQ(ierr);
10674bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
10684bbf5ea8SMatthew G. Knepley     PetscInt bcIndex     = 0;
10694bbf5ea8SMatthew G. Knepley     PetscInt numBcs, dof, off;
10704bbf5ea8SMatthew G. Knepley 
10714bbf5ea8SMatthew G. Knepley     PetscHashIClear(patchDofs);
10724bbf5ea8SMatthew G. Knepley     PetscHashIClear(localBcs);
10734bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(gtolCounts, v, &dof);CHKERRQ(ierr);
10744bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(gtolCounts, v, &off);CHKERRQ(ierr);
10754bbf5ea8SMatthew G. Knepley 
10764bbf5ea8SMatthew G. Knepley     if (dof <= 0) {
10774bbf5ea8SMatthew G. Knepley       patch->bcs[v-vStart] = NULL;
10784bbf5ea8SMatthew G. Knepley       continue;
10794bbf5ea8SMatthew G. Knepley     }
10804bbf5ea8SMatthew G. Knepley 
10814bbf5ea8SMatthew G. Knepley     for (i = off; i < off + dof; ++i) {
10824bbf5ea8SMatthew G. Knepley       const PetscInt globalDof = gtolArray[i];
10834bbf5ea8SMatthew G. Knepley       const PetscInt localDof  = i-off;
10844bbf5ea8SMatthew G. Knepley       PetscBool      flg;
10854bbf5ea8SMatthew G. Knepley 
10864bbf5ea8SMatthew G. Knepley       PetscHashIAdd(patchDofs, globalDof, localDof);
10874bbf5ea8SMatthew G. Knepley       PetscHashIHasKey(globalBcs, globalDof, flg);
10884bbf5ea8SMatthew G. Knepley       if (flg) {PetscHashIAdd(localBcs, localDof, 0);}
10894bbf5ea8SMatthew G. Knepley     }
10904bbf5ea8SMatthew G. Knepley 
10914bbf5ea8SMatthew G. Knepley     /* Now figure out the artificial BCs: the set difference of {dofs on entities I see on the patch}\{dofs I am responsible for updating} */
10924bbf5ea8SMatthew G. Knepley     ierr = patch->patchconstructop((void*) patch, dm, v, ownedpts);CHKERRQ(ierr);
10935f824522SMatthew G. Knepley     ierr = PCPatchCompleteCellPatch(pc, ownedpts, seenpts);CHKERRQ(ierr);
10945f824522SMatthew G. Knepley     ierr = PCPatchGetPointDofs(pc, ownedpts, owneddofs, v, patch->exclude_subspace);CHKERRQ(ierr);
10955f824522SMatthew G. Knepley     ierr = PCPatchGetPointDofs(pc, seenpts, seendofs, v, -1);CHKERRQ(ierr);
10964bbf5ea8SMatthew G. Knepley     ierr = PCPatchComputeSetDifference_Private(owneddofs, seendofs, artificialbcs);CHKERRQ(ierr);
10974bbf5ea8SMatthew G. Knepley 
10985f824522SMatthew G. Knepley     if (patch->viewPatches) {
10994bbf5ea8SMatthew G. Knepley       PetscHashI globalbcdofs;
11004bbf5ea8SMatthew G. Knepley       MPI_Comm   comm;
11014bbf5ea8SMatthew G. Knepley 
11024bbf5ea8SMatthew G. Knepley       PetscHashICreate(globalbcdofs);
11034bbf5ea8SMatthew G. Knepley 
11044bbf5ea8SMatthew G. Knepley       ierr = PetscObjectGetComm((PetscObject) pc, &comm);CHKERRQ(ierr);
11054bbf5ea8SMatthew G. Knepley       ierr = PetscSynchronizedPrintf(comm, "Patch %d: owned dofs:\n", v);CHKERRQ(ierr);
11064bbf5ea8SMatthew G. Knepley       PetscHashIIterBegin(owneddofs, hi);
11074bbf5ea8SMatthew G. Knepley       while (!PetscHashIIterAtEnd(owneddofs, hi)) {
11084bbf5ea8SMatthew G. Knepley         PetscInt globalDof;
11094bbf5ea8SMatthew G. Knepley 
11104bbf5ea8SMatthew G. Knepley         PetscHashIIterGetKey(owneddofs, hi, globalDof);
11114bbf5ea8SMatthew G. Knepley         PetscHashIIterNext(owneddofs, hi);
11124bbf5ea8SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof);CHKERRQ(ierr);
11134bbf5ea8SMatthew G. Knepley       }
11144bbf5ea8SMatthew G. Knepley       ierr = PetscSynchronizedPrintf(comm, "\n");CHKERRQ(ierr);
11154bbf5ea8SMatthew G. Knepley       ierr = PetscSynchronizedPrintf(comm, "Patch %d: seen dofs:\n", v);CHKERRQ(ierr);
11164bbf5ea8SMatthew G. Knepley       PetscHashIIterBegin(seendofs, hi);
11174bbf5ea8SMatthew G. Knepley       while (!PetscHashIIterAtEnd(seendofs, hi)) {
11184bbf5ea8SMatthew G. Knepley         PetscInt globalDof;
11194bbf5ea8SMatthew G. Knepley         PetscBool flg;
11204bbf5ea8SMatthew G. Knepley 
11214bbf5ea8SMatthew G. Knepley         PetscHashIIterGetKey(seendofs, hi, globalDof);
11224bbf5ea8SMatthew G. Knepley         PetscHashIIterNext(seendofs, hi);
11234bbf5ea8SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof);CHKERRQ(ierr);
11244bbf5ea8SMatthew G. Knepley         PetscHashIHasKey(globalBcs, globalDof, flg);
11254bbf5ea8SMatthew G. Knepley         if (flg) {PetscHashIAdd(globalbcdofs, globalDof, 0);}
11264bbf5ea8SMatthew G. Knepley       }
11274bbf5ea8SMatthew G. Knepley       ierr = PetscSynchronizedPrintf(comm, "\n");CHKERRQ(ierr);
11284bbf5ea8SMatthew G. Knepley       ierr = PetscSynchronizedPrintf(comm, "Patch %d: global BCs:\n", v);CHKERRQ(ierr);
11294bbf5ea8SMatthew G. Knepley       PetscHashISize(globalbcdofs, numBcs);
11304bbf5ea8SMatthew G. Knepley       if (numBcs > 0) {
11314bbf5ea8SMatthew G. Knepley         PetscHashIIterBegin(globalbcdofs, hi);
11324bbf5ea8SMatthew G. Knepley         while (!PetscHashIIterAtEnd(globalbcdofs, hi)) {
11334bbf5ea8SMatthew G. Knepley           PetscInt globalDof;
11344bbf5ea8SMatthew G. Knepley           PetscHashIIterGetKey(globalbcdofs, hi, globalDof);
11354bbf5ea8SMatthew G. Knepley           PetscHashIIterNext(globalbcdofs, hi);
11364bbf5ea8SMatthew G. Knepley           ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof);CHKERRQ(ierr);
11374bbf5ea8SMatthew G. Knepley         }
11384bbf5ea8SMatthew G. Knepley       }
11394bbf5ea8SMatthew G. Knepley       ierr = PetscSynchronizedPrintf(comm, "\n");CHKERRQ(ierr);
11404bbf5ea8SMatthew G. Knepley       ierr = PetscSynchronizedPrintf(comm, "Patch %d: artificial BCs:\n", v);CHKERRQ(ierr);
11414bbf5ea8SMatthew G. Knepley       PetscHashISize(artificialbcs, numBcs);
11424bbf5ea8SMatthew G. Knepley       if (numBcs > 0) {
11434bbf5ea8SMatthew G. Knepley         PetscHashIIterBegin(artificialbcs, hi);
11444bbf5ea8SMatthew G. Knepley         while (!PetscHashIIterAtEnd(artificialbcs, hi)) {
11454bbf5ea8SMatthew G. Knepley           PetscInt globalDof;
11464bbf5ea8SMatthew G. Knepley           PetscHashIIterGetKey(artificialbcs, hi, globalDof);
11474bbf5ea8SMatthew G. Knepley           PetscHashIIterNext(artificialbcs, hi);
11484bbf5ea8SMatthew G. Knepley           ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof);CHKERRQ(ierr);
11494bbf5ea8SMatthew G. Knepley         }
11504bbf5ea8SMatthew G. Knepley       }
11514bbf5ea8SMatthew G. Knepley       ierr = PetscSynchronizedPrintf(comm, "\n\n");CHKERRQ(ierr);
11524bbf5ea8SMatthew G. Knepley       ierr = PetscSynchronizedFlush(comm, PETSC_STDOUT);CHKERRQ(ierr);
11534bbf5ea8SMatthew G. Knepley       PetscHashIDestroy(globalbcdofs);
11544bbf5ea8SMatthew G. Knepley     }
11554bbf5ea8SMatthew G. Knepley 
11564bbf5ea8SMatthew G. Knepley     PetscHashISize(artificialbcs, numBcs);
11574bbf5ea8SMatthew G. Knepley     if (numBcs > 0) {
11584bbf5ea8SMatthew G. Knepley       PetscHashIIterBegin(artificialbcs, hi);
11594bbf5ea8SMatthew G. Knepley       while (!PetscHashIIterAtEnd(artificialbcs, hi)) {
11604bbf5ea8SMatthew G. Knepley         PetscInt globalDof, localDof;
11614bbf5ea8SMatthew G. Knepley         PetscHashIIterGetKey(artificialbcs, hi, globalDof);
11624bbf5ea8SMatthew G. Knepley         PetscHashIIterNext(artificialbcs, hi);
11634bbf5ea8SMatthew G. Knepley         PetscHashIMap(patchDofs, globalDof, localDof);
11645f824522SMatthew G. Knepley         if (localDof == -1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Patch %d Didn't find dof %d in patch\n", v, globalDof);
11654bbf5ea8SMatthew G. Knepley         PetscHashIAdd(localBcs, localDof, 0);
11664bbf5ea8SMatthew G. Knepley       }
11674bbf5ea8SMatthew G. Knepley     }
11684bbf5ea8SMatthew G. Knepley 
11694bbf5ea8SMatthew G. Knepley     /* OK, now we have a hash table with all the bcs indicated by the artificial and global bcs */
11704bbf5ea8SMatthew G. Knepley     PetscHashISize(localBcs, numBcs);
11714bbf5ea8SMatthew G. Knepley     ierr = PetscSectionSetDof(bcCounts, v, numBcs);CHKERRQ(ierr);
11724bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(numBcs, &bcsArray);CHKERRQ(ierr);
11734bbf5ea8SMatthew G. Knepley     PetscHashIGetKeys(localBcs, &bcIndex, bcsArray);
11744bbf5ea8SMatthew G. Knepley     ierr = PetscSortInt(numBcs, bcsArray);CHKERRQ(ierr);
11754bbf5ea8SMatthew G. Knepley     ierr = ISCreateGeneral(PETSC_COMM_SELF, numBcs, bcsArray, PETSC_OWN_POINTER, &(patch->bcs[v - vStart]));CHKERRQ(ierr);
11764bbf5ea8SMatthew G. Knepley   }
11774bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(gtol, &gtolArray);CHKERRQ(ierr);
11784bbf5ea8SMatthew G. Knepley   PetscHashIDestroy(artificialbcs);
11794bbf5ea8SMatthew G. Knepley   PetscHashIDestroy(seendofs);
11804bbf5ea8SMatthew G. Knepley   PetscHashIDestroy(owneddofs);
11814bbf5ea8SMatthew G. Knepley   PetscHashIDestroy(seenpts);
11824bbf5ea8SMatthew G. Knepley   PetscHashIDestroy(ownedpts);
11834bbf5ea8SMatthew G. Knepley   PetscHashIDestroy(localBcs);
11844bbf5ea8SMatthew G. Knepley   PetscHashIDestroy(patchDofs);
11854bbf5ea8SMatthew G. Knepley   PetscHashIDestroy(globalBcs);
11864bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->ghostBcNodes);CHKERRQ(ierr);
11874bbf5ea8SMatthew G. Knepley   ierr = PetscSectionSetUp(bcCounts);CHKERRQ(ierr);
11884bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
11894bbf5ea8SMatthew G. Knepley }
11904bbf5ea8SMatthew G. Knepley 
11914bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchZeroFillMatrix_Private(Mat mat, const PetscInt ncell, const PetscInt ndof, const PetscInt *dof)
11924bbf5ea8SMatthew G. Knepley {
11934bbf5ea8SMatthew G. Knepley   const PetscScalar *values = NULL;
11944bbf5ea8SMatthew G. Knepley   PetscInt           rows, c, i;
11954bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
11964bbf5ea8SMatthew G. Knepley 
11974bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
11984bbf5ea8SMatthew G. Knepley   ierr = PetscCalloc1(ndof*ndof, &values);CHKERRQ(ierr);
11994bbf5ea8SMatthew G. Knepley   for (c = 0; c < ncell; ++c) {
12004bbf5ea8SMatthew G. Knepley     const PetscInt *idx = &dof[ndof*c];
12014bbf5ea8SMatthew G. Knepley     ierr = MatSetValues(mat, ndof, idx, ndof, idx, values, INSERT_VALUES);CHKERRQ(ierr);
12024bbf5ea8SMatthew G. Knepley   }
12034bbf5ea8SMatthew G. Knepley   ierr = MatGetLocalSize(mat, &rows, NULL);CHKERRQ(ierr);
12044bbf5ea8SMatthew G. Knepley   for (i = 0; i < rows; ++i) {
12054bbf5ea8SMatthew G. Knepley     ierr = MatSetValues(mat, 1, &i, 1, &i, values, INSERT_VALUES);CHKERRQ(ierr);
12064bbf5ea8SMatthew G. Knepley   }
12074bbf5ea8SMatthew G. Knepley   ierr = MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
12084bbf5ea8SMatthew G. Knepley   ierr = MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
12094bbf5ea8SMatthew G. Knepley   ierr = PetscFree(values);CHKERRQ(ierr);
12104bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
12114bbf5ea8SMatthew G. Knepley }
12124bbf5ea8SMatthew G. Knepley 
12134bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateMatrix_Private(PC pc, PetscInt point, Mat *mat)
12144bbf5ea8SMatthew G. Knepley {
12154bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
12164bbf5ea8SMatthew G. Knepley   Vec            x, y;
12174bbf5ea8SMatthew G. Knepley   PetscBool      flg;
12184bbf5ea8SMatthew G. Knepley   PetscInt       csize, rsize;
12194bbf5ea8SMatthew G. Knepley   const char    *prefix = NULL;
12204bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
12214bbf5ea8SMatthew G. Knepley 
12224bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
12234bbf5ea8SMatthew G. Knepley   x = patch->patchX[point];
12244bbf5ea8SMatthew G. Knepley   y = patch->patchY[point];
12254bbf5ea8SMatthew G. Knepley   ierr = VecGetSize(x, &csize);CHKERRQ(ierr);
12264bbf5ea8SMatthew G. Knepley   ierr = VecGetSize(y, &rsize);CHKERRQ(ierr);
12274bbf5ea8SMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, mat);CHKERRQ(ierr);
12284bbf5ea8SMatthew G. Knepley   ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr);
12294bbf5ea8SMatthew G. Knepley   ierr = MatSetOptionsPrefix(*mat, prefix);CHKERRQ(ierr);
12305f824522SMatthew G. Knepley   ierr = MatAppendOptionsPrefix(*mat, "pc_patch_sub_");CHKERRQ(ierr);
12314bbf5ea8SMatthew G. Knepley   if (patch->sub_mat_type)       {ierr = MatSetType(*mat, patch->sub_mat_type);CHKERRQ(ierr);}
12327974b488SMatthew G. Knepley   else if (!patch->sub_mat_type) {ierr = MatSetType(*mat, MATDENSE);CHKERRQ(ierr);}
12334bbf5ea8SMatthew G. Knepley   ierr = MatSetSizes(*mat, rsize, csize, rsize, csize);CHKERRQ(ierr);
12344bbf5ea8SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) *mat, MATDENSE, &flg);CHKERRQ(ierr);
12354bbf5ea8SMatthew G. Knepley   if (!flg) {ierr = PetscObjectTypeCompare((PetscObject)*mat, MATSEQDENSE, &flg);CHKERRQ(ierr);}
12364bbf5ea8SMatthew G. Knepley   /* Sparse patch matrices */
12374bbf5ea8SMatthew G. Knepley   if (!flg) {
12384bbf5ea8SMatthew G. Knepley     PetscBT         bt;
12394bbf5ea8SMatthew G. Knepley     PetscInt       *dnnz      = NULL;
12404bbf5ea8SMatthew G. Knepley     const PetscInt *dofsArray = NULL;
12414bbf5ea8SMatthew G. Knepley     PetscInt        pStart, pEnd, ncell, offset, c, i, j;
12424bbf5ea8SMatthew G. Knepley 
12434bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
12444bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
12454bbf5ea8SMatthew G. Knepley     point += pStart;
12464bbf5ea8SMatthew 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);
12474bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
12484bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
12494bbf5ea8SMatthew G. Knepley     ierr = PetscCalloc1(rsize, &dnnz);CHKERRQ(ierr);
12504bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventBegin(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr);
12514bbf5ea8SMatthew G. Knepley     /* XXX: This uses N^2 bits to store the sparsity pattern on a
12524bbf5ea8SMatthew G. Knepley      * patch.  This is probably OK if the patches are not too big,
12534bbf5ea8SMatthew G. Knepley      * but could use quite a bit of memory for planes in 3D.
12544bbf5ea8SMatthew G. Knepley      * Should we switch based on the value of rsize to a
12554bbf5ea8SMatthew G. Knepley      * hash-table (slower, but more memory efficient) approach? */
12564bbf5ea8SMatthew G. Knepley     ierr = PetscBTCreate(rsize*rsize, &bt);CHKERRQ(ierr);
12574bbf5ea8SMatthew G. Knepley     for (c = 0; c < ncell; ++c) {
12584bbf5ea8SMatthew G. Knepley       const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell;
12594bbf5ea8SMatthew G. Knepley       for (i = 0; i < patch->totalDofsPerCell; ++i) {
12604bbf5ea8SMatthew G. Knepley         const PetscInt row = idx[i];
12614bbf5ea8SMatthew G. Knepley         for (j = 0; j < patch->totalDofsPerCell; ++j) {
12624bbf5ea8SMatthew G. Knepley           const PetscInt col = idx[j];
12634bbf5ea8SMatthew G. Knepley           const PetscInt key = row*rsize + col;
12644bbf5ea8SMatthew G. Knepley           if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
12654bbf5ea8SMatthew G. Knepley         }
12664bbf5ea8SMatthew G. Knepley       }
12674bbf5ea8SMatthew G. Knepley     }
12684bbf5ea8SMatthew G. Knepley     ierr = PetscBTDestroy(&bt);CHKERRQ(ierr);
12694bbf5ea8SMatthew G. Knepley     ierr = MatXAIJSetPreallocation(*mat, 1, dnnz, NULL, NULL, NULL);CHKERRQ(ierr);
12704bbf5ea8SMatthew G. Knepley     ierr = PetscFree(dnnz);CHKERRQ(ierr);
12714bbf5ea8SMatthew G. Knepley     ierr = PCPatchZeroFillMatrix_Private(*mat, ncell, patch->totalDofsPerCell, &dofsArray[offset*patch->totalDofsPerCell]);CHKERRQ(ierr);
12724bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr);
12734bbf5ea8SMatthew G. Knepley     ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
12744bbf5ea8SMatthew G. Knepley   }
12754bbf5ea8SMatthew G. Knepley   ierr = MatSetUp(*mat);CHKERRQ(ierr);
12764bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
12774bbf5ea8SMatthew G. Knepley }
12784bbf5ea8SMatthew G. Knepley 
12795f824522SMatthew G. Knepley static PetscErrorCode PCPatchComputeOperator_DMPlex_Private(PC pc, PetscInt patchNum, Mat J, PetscInt ncell, const PetscInt cells[], PetscInt n, const PetscInt *l2p, void *ctx)
12805f824522SMatthew G. Knepley {
12815f824522SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
12825f824522SMatthew G. Knepley   DM              dm;
12835f824522SMatthew G. Knepley   PetscSection    s;
12845f824522SMatthew G. Knepley   const PetscInt *parray, *oarray;
12855f824522SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
12865f824522SMatthew G. Knepley   PetscErrorCode  ierr;
12875f824522SMatthew G. Knepley 
12885f824522SMatthew G. Knepley   PetscFunctionBegin;
12895f824522SMatthew G. Knepley   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
12905f824522SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
12915f824522SMatthew G. Knepley   /* Set offset into patch */
12925f824522SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->pointCounts, patchNum, &Np);CHKERRQ(ierr);
12935f824522SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->pointCounts, patchNum, &poff);CHKERRQ(ierr);
12945f824522SMatthew G. Knepley   ierr = ISGetIndices(patch->points, &parray);CHKERRQ(ierr);
12955f824522SMatthew G. Knepley   ierr = ISGetIndices(patch->offs,   &oarray);CHKERRQ(ierr);
12965f824522SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
12975f824522SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
12985f824522SMatthew G. Knepley       const PetscInt point = parray[poff+p];
12995f824522SMatthew G. Knepley       PetscInt       dof;
13005f824522SMatthew G. Knepley 
13015f824522SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(patch->patchSection, point, f, &dof);CHKERRQ(ierr);
13025f824522SMatthew G. Knepley       ierr = PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);
13035f824522SMatthew G. Knepley       if (patch->nsubspaces == 1) {ierr = PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);}
13045f824522SMatthew G. Knepley       else                        {ierr = PetscSectionSetOffset(patch->patchSection, point, -1);CHKERRQ(ierr);}
13055f824522SMatthew G. Knepley     }
13065f824522SMatthew G. Knepley   }
13075f824522SMatthew G. Knepley   ierr = ISRestoreIndices(patch->points, &parray);CHKERRQ(ierr);
13085f824522SMatthew G. Knepley   ierr = ISRestoreIndices(patch->offs,   &oarray);CHKERRQ(ierr);
13095f824522SMatthew G. Knepley   if (patch->viewSection) {ierr = ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection);CHKERRQ(ierr);}
13105f824522SMatthew G. Knepley   /* TODO Shut off MatViewFromOptions() in MatAssemblyEnd() here */
13115f824522SMatthew G. Knepley   ierr = DMPlexComputeJacobian_Patch_Internal(pc->dm, patch->patchSection, patch->patchSection, 0, ncell, cells, 0.0, 0.0, NULL, NULL, J, J, ctx);CHKERRQ(ierr);
13125f824522SMatthew G. Knepley   PetscFunctionReturn(0);
13135f824522SMatthew G. Knepley }
13145f824522SMatthew G. Knepley 
1315*73ec7555SLawrence Mitchell static PetscErrorCode PCPatchComputeOperator_Private(PC pc, Mat mat, PetscInt point)
13164bbf5ea8SMatthew G. Knepley {
13174bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
13184bbf5ea8SMatthew G. Knepley   const PetscInt *dofsArray;
13194bbf5ea8SMatthew G. Knepley   const PetscInt *cellsArray;
13204bbf5ea8SMatthew G. Knepley   PetscInt        ncell, offset, pStart, pEnd;
13214bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
13224bbf5ea8SMatthew G. Knepley 
13234bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
13244bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
13254bbf5ea8SMatthew G. Knepley   if (!patch->usercomputeop) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback\n");
13264bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
13274bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
13284bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
13294bbf5ea8SMatthew G. Knepley 
13304bbf5ea8SMatthew G. Knepley   point += pStart;
13314bbf5ea8SMatthew 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);
13324bbf5ea8SMatthew G. Knepley 
13334bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
13344bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
13354bbf5ea8SMatthew G. Knepley   if (ncell <= 0) {
13364bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
13374bbf5ea8SMatthew G. Knepley     PetscFunctionReturn(0);
13384bbf5ea8SMatthew G. Knepley   }
13394bbf5ea8SMatthew G. Knepley   PetscStackPush("PCPatch user callback");
13405f824522SMatthew G. Knepley   ierr = patch->usercomputeop(pc, point, mat, ncell, cellsArray + offset, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell, patch->usercomputectx);CHKERRQ(ierr);
13414bbf5ea8SMatthew G. Knepley   PetscStackPop;
13424bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
13434bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
13444bbf5ea8SMatthew G. Knepley   ierr = MatZeroRowsColumnsIS(mat, patch->bcs[point-pStart], (PetscScalar) 1.0, NULL, NULL);CHKERRQ(ierr);
13455f824522SMatthew G. Knepley   if (patch->viewMatrix) {ierr = ObjectView((PetscObject) mat, patch->viewerMatrix, patch->formatMatrix);CHKERRQ(ierr);}
13464bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
13474bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
13484bbf5ea8SMatthew G. Knepley }
13494bbf5ea8SMatthew G. Knepley 
13504bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatch_ScatterLocal_Private(PC pc, PetscInt p, Vec x, Vec y, InsertMode mode, ScatterMode scat)
13514bbf5ea8SMatthew G. Knepley {
13524bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch     = (PC_PATCH *) pc->data;
13534bbf5ea8SMatthew G. Knepley   const PetscScalar *xArray    = NULL;
13544bbf5ea8SMatthew G. Knepley   PetscScalar       *yArray    = NULL;
13554bbf5ea8SMatthew G. Knepley   const PetscInt    *gtolArray = NULL;
13564bbf5ea8SMatthew G. Knepley   PetscInt           dof, offset, lidx;
13574bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
13584bbf5ea8SMatthew G. Knepley 
13594bbf5ea8SMatthew G. Knepley   PetscFunctionBeginHot;
13604bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Scatter, pc, 0, 0, 0);CHKERRQ(ierr);
13614bbf5ea8SMatthew G. Knepley   ierr = VecGetArrayRead(x, &xArray);CHKERRQ(ierr);
13624bbf5ea8SMatthew G. Knepley   ierr = VecGetArray(y, &yArray);CHKERRQ(ierr);
13634bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr);
13644bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
13654bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
13664bbf5ea8SMatthew 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");
13674bbf5ea8SMatthew 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");
13684bbf5ea8SMatthew G. Knepley   for (lidx = 0; lidx < dof; ++lidx) {
13694bbf5ea8SMatthew G. Knepley     const PetscInt gidx = gtolArray[offset+lidx];
13704bbf5ea8SMatthew G. Knepley 
13714bbf5ea8SMatthew G. Knepley     if (mode == INSERT_VALUES) yArray[lidx]  = xArray[gidx]; /* Forward */
13724bbf5ea8SMatthew G. Knepley     else                       yArray[gidx] += xArray[lidx]; /* Reverse */
13734bbf5ea8SMatthew G. Knepley   }
13744bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
13754bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArrayRead(x, &xArray);CHKERRQ(ierr);
13764bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArray(y, &yArray);CHKERRQ(ierr);
13774bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Scatter, pc, 0, 0, 0);CHKERRQ(ierr);
13784bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
13794bbf5ea8SMatthew G. Knepley }
13804bbf5ea8SMatthew G. Knepley 
13814bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUp_PATCH(PC pc)
13824bbf5ea8SMatthew G. Knepley {
13834bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch   = (PC_PATCH *) pc->data;
13844bbf5ea8SMatthew G. Knepley   PetscScalar    *patchX  = NULL;
13854bbf5ea8SMatthew G. Knepley   const PetscInt *bcNodes = NULL;
13864bbf5ea8SMatthew G. Knepley   PetscInt        numBcs, i, j;
13874bbf5ea8SMatthew G. Knepley   const char     *prefix;
13884bbf5ea8SMatthew G. Knepley   PetscErrorCode  ierr;
13894bbf5ea8SMatthew G. Knepley 
13904bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
13914bbf5ea8SMatthew G. Knepley   if (!pc->setupcalled) {
13924bbf5ea8SMatthew G. Knepley     PetscInt pStart, pEnd, p;
13934bbf5ea8SMatthew G. Knepley     PetscInt localSize;
13944bbf5ea8SMatthew G. Knepley 
13954bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventBegin(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr);
13964bbf5ea8SMatthew G. Knepley 
13975f824522SMatthew G. Knepley     if (!patch->nsubspaces) {
13985f824522SMatthew G. Knepley       DM           dm;
13995f824522SMatthew G. Knepley       PetscDS      prob;
14005f824522SMatthew G. Knepley       PetscSection s;
1401e72c1634SMatthew G. Knepley       PetscInt     cStart, cEnd, c, Nf, f, numGlobalBcs = 0, *globalBcs, *Nb, totNb = 0, **cellDofs;
14025f824522SMatthew G. Knepley 
14035f824522SMatthew G. Knepley       ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
14045f824522SMatthew G. Knepley       if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONG, "Must set DM for PCPATCH or call PCPatchSetDiscretisationInfo()");
14055f824522SMatthew G. Knepley       ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
14065f824522SMatthew G. Knepley       ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
14075f824522SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
14085f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
14095f824522SMatthew G. Knepley         PetscInt cdof;
14105f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
14115f824522SMatthew G. Knepley         numGlobalBcs += cdof;
14125f824522SMatthew G. Knepley       }
14135f824522SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
14145f824522SMatthew G. Knepley       ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
14155f824522SMatthew G. Knepley       ierr = PetscMalloc3(Nf, &Nb, Nf, &cellDofs, numGlobalBcs, &globalBcs);CHKERRQ(ierr);
14165f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
14175f824522SMatthew G. Knepley         PetscFE        fe;
14185f824522SMatthew G. Knepley         PetscDualSpace sp;
14195f824522SMatthew G. Knepley         PetscInt       cdoff = 0;
14205f824522SMatthew G. Knepley 
14215f824522SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
14225f824522SMatthew G. Knepley         /* ierr = PetscFEGetNumComponents(fe, &Nc[f]);CHKERRQ(ierr); */
14235f824522SMatthew G. Knepley         ierr = PetscFEGetDualSpace(fe, &sp);CHKERRQ(ierr);
14245f824522SMatthew G. Knepley         ierr = PetscDualSpaceGetDimension(sp, &Nb[f]);CHKERRQ(ierr);
14255f824522SMatthew G. Knepley         totNb += Nb[f];
14265f824522SMatthew G. Knepley 
14275f824522SMatthew G. Knepley         ierr = PetscMalloc1((cEnd-cStart)*Nb[f], &cellDofs[f]);CHKERRQ(ierr);
14285f824522SMatthew G. Knepley         for (c = cStart; c < cEnd; ++c) {
14295f824522SMatthew G. Knepley           PetscInt *closure = NULL;
14305f824522SMatthew G. Knepley           PetscInt  clSize  = 0, cl;
14315f824522SMatthew G. Knepley 
14325f824522SMatthew G. Knepley           ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
14335f824522SMatthew G. Knepley           for (cl = 0; cl < clSize*2; cl += 2) {
14345f824522SMatthew G. Knepley             const PetscInt p = closure[cl];
14355f824522SMatthew G. Knepley             PetscInt       fdof, d, foff;
14365f824522SMatthew G. Knepley 
14375f824522SMatthew G. Knepley             ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr);
14385f824522SMatthew G. Knepley             ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr);
14395f824522SMatthew G. Knepley             for (d = 0; d < fdof; ++d, ++cdoff) cellDofs[f][cdoff] = foff + d;
14405f824522SMatthew G. Knepley           }
14415f824522SMatthew G. Knepley           ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
14425f824522SMatthew G. Knepley         }
14435f824522SMatthew 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]);
14445f824522SMatthew G. Knepley       }
14455f824522SMatthew G. Knepley       numGlobalBcs = 0;
14465f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
14475f824522SMatthew G. Knepley         const PetscInt *ind;
14485f824522SMatthew G. Knepley         PetscInt        off, cdof, d;
14495f824522SMatthew G. Knepley 
14505f824522SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
14515f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
14525f824522SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &ind);CHKERRQ(ierr);
14535f824522SMatthew G. Knepley         for (d = 0; d < cdof; ++d) globalBcs[numGlobalBcs++] = off + ind[d];
14545f824522SMatthew G. Knepley       }
14555f824522SMatthew G. Knepley 
14565f824522SMatthew G. Knepley       ierr = PCPatchSetDiscretisationInfoCombined(pc, dm, Nb, (const PetscInt **) cellDofs, numGlobalBcs, globalBcs, numGlobalBcs, globalBcs);CHKERRQ(ierr);
14575f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
14585f824522SMatthew G. Knepley         ierr = PetscFree(cellDofs[f]);CHKERRQ(ierr);
14595f824522SMatthew G. Knepley       }
14605f824522SMatthew G. Knepley       ierr = PetscFree3(Nb, cellDofs, globalBcs);CHKERRQ(ierr);
14615f824522SMatthew G. Knepley       ierr = PCPatchSetComputeOperator(pc, PCPatchComputeOperator_DMPlex_Private, NULL);CHKERRQ(ierr);
14625f824522SMatthew G. Knepley     }
14635f824522SMatthew G. Knepley 
14644bbf5ea8SMatthew G. Knepley     localSize = patch->subspaceOffsets[patch->nsubspaces];
14654bbf5ea8SMatthew G. Knepley     ierr = VecCreateSeq(PETSC_COMM_SELF, localSize, &patch->localX);CHKERRQ(ierr);
14664bbf5ea8SMatthew G. Knepley     ierr = VecSetUp(patch->localX);CHKERRQ(ierr);
14674bbf5ea8SMatthew G. Knepley     ierr = VecDuplicate(patch->localX, &patch->localY);CHKERRQ(ierr);
14684bbf5ea8SMatthew G. Knepley     ierr = PCPatchCreateCellPatches(pc);CHKERRQ(ierr);
14694bbf5ea8SMatthew G. Knepley     ierr = PCPatchCreateCellPatchDiscretisationInfo(pc);CHKERRQ(ierr);
14704bbf5ea8SMatthew G. Knepley     ierr = PCPatchCreateCellPatchBCs(pc);CHKERRQ(ierr);
14714bbf5ea8SMatthew G. Knepley 
14724bbf5ea8SMatthew G. Knepley     /* OK, now build the work vectors */
14734bbf5ea8SMatthew G. Knepley     ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, &pEnd);CHKERRQ(ierr);
14744bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(patch->npatch, &patch->patchX);CHKERRQ(ierr);
14754bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(patch->npatch, &patch->patchY);CHKERRQ(ierr);
14764bbf5ea8SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
14774bbf5ea8SMatthew G. Knepley       PetscInt dof;
14784bbf5ea8SMatthew G. Knepley 
14794bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr);
14804bbf5ea8SMatthew G. Knepley       ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchX[p-pStart]);CHKERRQ(ierr);
14814bbf5ea8SMatthew G. Knepley       ierr = VecSetUp(patch->patchX[p-pStart]);CHKERRQ(ierr);
14824bbf5ea8SMatthew G. Knepley       ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchY[p-pStart]);CHKERRQ(ierr);
14834bbf5ea8SMatthew G. Knepley       ierr = VecSetUp(patch->patchY[p-pStart]);CHKERRQ(ierr);
14844bbf5ea8SMatthew G. Knepley     }
14854bbf5ea8SMatthew G. Knepley     ierr = PetscMalloc1(patch->npatch, &patch->ksp);CHKERRQ(ierr);
14864bbf5ea8SMatthew G. Knepley     ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr);
14874bbf5ea8SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {
1488484055adSMatthew G. Knepley       PC subpc;
1489484055adSMatthew G. Knepley 
14904bbf5ea8SMatthew G. Knepley       ierr = KSPCreate(PETSC_COMM_SELF, &patch->ksp[i]);CHKERRQ(ierr);
14914bbf5ea8SMatthew G. Knepley       ierr = KSPSetOptionsPrefix(patch->ksp[i], prefix);CHKERRQ(ierr);
14924bbf5ea8SMatthew G. Knepley       ierr = KSPAppendOptionsPrefix(patch->ksp[i], "sub_");CHKERRQ(ierr);
1493484055adSMatthew G. Knepley       ierr = PetscObjectIncrementTabLevel((PetscObject) patch->ksp[i], (PetscObject) pc, 1);CHKERRQ(ierr);
1494484055adSMatthew G. Knepley       ierr = KSPGetPC(patch->ksp[i], &subpc);CHKERRQ(ierr);
1495484055adSMatthew G. Knepley       ierr = PetscObjectIncrementTabLevel((PetscObject) subpc, (PetscObject) pc, 1);CHKERRQ(ierr);
1496484055adSMatthew G. Knepley       ierr = PetscLogObjectParent((PetscObject) pc, (PetscObject) patch->ksp[i]);CHKERRQ(ierr);
14974bbf5ea8SMatthew G. Knepley     }
14984bbf5ea8SMatthew G. Knepley     if (patch->save_operators) {
14994bbf5ea8SMatthew G. Knepley       ierr = PetscMalloc1(patch->npatch, &patch->mat);CHKERRQ(ierr);
15004bbf5ea8SMatthew G. Knepley       for (i = 0; i < patch->npatch; ++i) {
15014bbf5ea8SMatthew G. Knepley         ierr = PCPatchCreateMatrix_Private(pc, i, &patch->mat[i]);CHKERRQ(ierr);
15024bbf5ea8SMatthew G. Knepley       }
15034bbf5ea8SMatthew G. Knepley     }
15044bbf5ea8SMatthew G. Knepley     ierr = PetscLogEventEnd(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr);
15054bbf5ea8SMatthew G. Knepley 
15064bbf5ea8SMatthew G. Knepley     /* If desired, calculate weights for dof multiplicity */
15074bbf5ea8SMatthew G. Knepley     if (patch->partition_of_unity) {
15084bbf5ea8SMatthew G. Knepley       ierr = VecDuplicate(patch->localX, &patch->dof_weights);CHKERRQ(ierr);
15094bbf5ea8SMatthew G. Knepley       for (i = 0; i < patch->npatch; ++i) {
15104bbf5ea8SMatthew G. Knepley         PetscInt dof;
15114bbf5ea8SMatthew G. Knepley 
15124bbf5ea8SMatthew G. Knepley         ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &dof);CHKERRQ(ierr);
15134bbf5ea8SMatthew G. Knepley         if (dof <= 0) continue;
15144bbf5ea8SMatthew G. Knepley         ierr = VecSet(patch->patchX[i], 1.0);CHKERRQ(ierr);
15154bbf5ea8SMatthew G. Knepley         /* TODO: Do we need different scatters for X and Y? */
15164bbf5ea8SMatthew G. Knepley         ierr = VecGetArray(patch->patchX[i], &patchX);CHKERRQ(ierr);
15174bbf5ea8SMatthew G. Knepley         /* Apply bcs to patchX (zero entries) */
15184bbf5ea8SMatthew G. Knepley         ierr = ISGetLocalSize(patch->bcs[i], &numBcs);CHKERRQ(ierr);
15194bbf5ea8SMatthew G. Knepley         ierr = ISGetIndices(patch->bcs[i], &bcNodes);CHKERRQ(ierr);
15204bbf5ea8SMatthew G. Knepley         for (j = 0; j < numBcs; ++j) patchX[bcNodes[j]] = 0;
15214bbf5ea8SMatthew G. Knepley         ierr = ISRestoreIndices(patch->bcs[i], &bcNodes);CHKERRQ(ierr);
15224bbf5ea8SMatthew G. Knepley         ierr = VecRestoreArray(patch->patchX[i], &patchX);CHKERRQ(ierr);
15234bbf5ea8SMatthew G. Knepley 
15244bbf5ea8SMatthew G. Knepley         ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchX[i], patch->dof_weights, ADD_VALUES, SCATTER_REVERSE);CHKERRQ(ierr);
15254bbf5ea8SMatthew G. Knepley       }
15264bbf5ea8SMatthew G. Knepley       ierr = VecReciprocal(patch->dof_weights);CHKERRQ(ierr);
15274bbf5ea8SMatthew G. Knepley     }
15284bbf5ea8SMatthew G. Knepley   }
15294bbf5ea8SMatthew G. Knepley   if (patch->save_operators) {
15304bbf5ea8SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {
15314bbf5ea8SMatthew G. Knepley       ierr = MatZeroEntries(patch->mat[i]);CHKERRQ(ierr);
1532*73ec7555SLawrence Mitchell       ierr = PCPatchComputeOperator_Private(pc, patch->mat[i], i);CHKERRQ(ierr);
15334bbf5ea8SMatthew G. Knepley       ierr = KSPSetOperators(patch->ksp[i], patch->mat[i], patch->mat[i]);CHKERRQ(ierr);
15344bbf5ea8SMatthew G. Knepley     }
15354bbf5ea8SMatthew G. Knepley   }
15365f824522SMatthew G. Knepley   if (!pc->setupcalled && patch->optionsSet) for (i = 0; i < patch->npatch; ++i) {ierr = KSPSetFromOptions(patch->ksp[i]);CHKERRQ(ierr);}
15374bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
15384bbf5ea8SMatthew G. Knepley }
15394bbf5ea8SMatthew G. Knepley 
15404bbf5ea8SMatthew G. Knepley static PetscErrorCode PCApply_PATCH(PC pc, Vec x, Vec y)
15414bbf5ea8SMatthew G. Knepley {
15424bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch    = (PC_PATCH *) pc->data;
15434bbf5ea8SMatthew G. Knepley   const PetscScalar *globalX  = NULL;
15444bbf5ea8SMatthew G. Knepley   PetscScalar       *localX   = NULL;
15454bbf5ea8SMatthew G. Knepley   PetscScalar       *globalY  = NULL;
15464bbf5ea8SMatthew G. Knepley   PetscScalar       *patchX   = NULL;
15474bbf5ea8SMatthew G. Knepley   const PetscInt    *bcNodes  = NULL;
15484bbf5ea8SMatthew G. Knepley   PetscInt           nsweep   = patch->symmetrise_sweep ? 2 : 1;
15494bbf5ea8SMatthew G. Knepley   PetscInt           start[2] = {0, 0};
15504bbf5ea8SMatthew G. Knepley   PetscInt           end[2]   = {-1, -1};
15514bbf5ea8SMatthew G. Knepley   const PetscInt     inc[2]   = {1, -1};
15524bbf5ea8SMatthew G. Knepley   const PetscScalar *localY;
15534bbf5ea8SMatthew G. Knepley   const PetscInt    *iterationSet;
15544bbf5ea8SMatthew G. Knepley   PetscInt           pStart, numBcs, n, sweep, bc, j;
15554bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
15564bbf5ea8SMatthew G. Knepley 
15574bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
15584bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventBegin(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr);
15594bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsPushGetViewerOff(PETSC_TRUE);CHKERRQ(ierr);
15604bbf5ea8SMatthew G. Knepley   end[0]   = patch->npatch;
15614bbf5ea8SMatthew G. Knepley   start[1] = patch->npatch-1;
15624bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
15634bbf5ea8SMatthew G. Knepley     ierr = ISGetLocalSize(patch->iterationSet, &end[0]);CHKERRQ(ierr);
15644bbf5ea8SMatthew G. Knepley     start[1] = end[0] - 1;
15654bbf5ea8SMatthew G. Knepley     ierr = ISGetIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr);
15664bbf5ea8SMatthew G. Knepley   }
15674bbf5ea8SMatthew G. Knepley   /* Scatter from global space into overlapped local spaces */
15684bbf5ea8SMatthew G. Knepley   ierr = VecGetArrayRead(x, &globalX);CHKERRQ(ierr);
15694bbf5ea8SMatthew G. Knepley   ierr = VecGetArray(patch->localX, &localX);CHKERRQ(ierr);
15704bbf5ea8SMatthew G. Knepley   ierr = PetscSFBcastBegin(patch->defaultSF, MPIU_SCALAR, globalX, localX);CHKERRQ(ierr);
15714bbf5ea8SMatthew G. Knepley   ierr = PetscSFBcastEnd(patch->defaultSF, MPIU_SCALAR, globalX, localX);CHKERRQ(ierr);
15724bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArrayRead(x, &globalX);CHKERRQ(ierr);
15734bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArray(patch->localX, &localX);CHKERRQ(ierr);
15744bbf5ea8SMatthew G. Knepley 
15754bbf5ea8SMatthew G. Knepley   ierr = VecSet(patch->localY, 0.0);CHKERRQ(ierr);
15764bbf5ea8SMatthew G. Knepley   ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, NULL);CHKERRQ(ierr);
15774bbf5ea8SMatthew G. Knepley   for (sweep = 0; sweep < nsweep; sweep++) {
15784bbf5ea8SMatthew G. Knepley     for (j = start[sweep]; j*inc[sweep] < end[sweep]*inc[sweep]; j += inc[sweep]) {
15794bbf5ea8SMatthew G. Knepley       PetscInt i       = patch->user_patches ? iterationSet[j] : j;
15804bbf5ea8SMatthew G. Knepley       PetscInt start, len;
15814bbf5ea8SMatthew G. Knepley 
15824bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &len);CHKERRQ(ierr);
15834bbf5ea8SMatthew G. Knepley       ierr = PetscSectionGetOffset(patch->gtolCounts, i+pStart, &start);CHKERRQ(ierr);
15844bbf5ea8SMatthew G. Knepley       /* TODO: Squash out these guys in the setup as well. */
15854bbf5ea8SMatthew G. Knepley       if (len <= 0) continue;
15864bbf5ea8SMatthew G. Knepley       /* TODO: Do we need different scatters for X and Y? */
15874bbf5ea8SMatthew G. Knepley       ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->localX, patch->patchX[i], INSERT_VALUES, SCATTER_FORWARD);CHKERRQ(ierr);
15884bbf5ea8SMatthew G. Knepley       /* Apply bcs to patchX (zero entries) */
15894bbf5ea8SMatthew G. Knepley       ierr = VecGetArray(patch->patchX[i], &patchX);CHKERRQ(ierr);
15904bbf5ea8SMatthew G. Knepley       ierr = ISGetLocalSize(patch->bcs[i], &numBcs);CHKERRQ(ierr);
15914bbf5ea8SMatthew G. Knepley       ierr = ISGetIndices(patch->bcs[i], &bcNodes);CHKERRQ(ierr);
15924bbf5ea8SMatthew G. Knepley       for (bc = 0; bc < numBcs; ++bc) patchX[bcNodes[bc]] = 0;
15934bbf5ea8SMatthew G. Knepley       ierr = ISRestoreIndices(patch->bcs[i], &bcNodes);CHKERRQ(ierr);
15944bbf5ea8SMatthew G. Knepley       ierr = VecRestoreArray(patch->patchX[i], &patchX);CHKERRQ(ierr);
15954bbf5ea8SMatthew G. Knepley       if (!patch->save_operators) {
15964bbf5ea8SMatthew G. Knepley         Mat mat;
15974bbf5ea8SMatthew G. Knepley 
15984bbf5ea8SMatthew G. Knepley         ierr = PCPatchCreateMatrix_Private(pc, i, &mat);CHKERRQ(ierr);
15994bbf5ea8SMatthew G. Knepley         /* Populate operator here. */
1600*73ec7555SLawrence Mitchell         ierr = PCPatchComputeOperator_Private(pc, mat, i);CHKERRQ(ierr);
16014bbf5ea8SMatthew G. Knepley         ierr = KSPSetOperators(patch->ksp[i], mat, mat);
16024bbf5ea8SMatthew G. Knepley         /* Drop reference so the KSPSetOperators below will blow it away. */
16034bbf5ea8SMatthew G. Knepley         ierr = MatDestroy(&mat);CHKERRQ(ierr);
16044bbf5ea8SMatthew G. Knepley       }
16054bbf5ea8SMatthew G. Knepley       ierr = PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr);
16064bbf5ea8SMatthew G. Knepley       ierr = KSPSolve(patch->ksp[i], patch->patchX[i], patch->patchY[i]);CHKERRQ(ierr);
16074bbf5ea8SMatthew G. Knepley       ierr = PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr);
16084bbf5ea8SMatthew G. Knepley 
16094bbf5ea8SMatthew G. Knepley       if (!patch->save_operators) {
16104bbf5ea8SMatthew G. Knepley         PC pc;
16114bbf5ea8SMatthew G. Knepley         ierr = KSPSetOperators(patch->ksp[i], NULL, NULL);CHKERRQ(ierr);
16124bbf5ea8SMatthew G. Knepley         ierr = KSPGetPC(patch->ksp[i], &pc);CHKERRQ(ierr);
16134bbf5ea8SMatthew G. Knepley         /* Destroy PC context too, otherwise the factored matrix hangs around. */
16144bbf5ea8SMatthew G. Knepley         ierr = PCReset(pc);CHKERRQ(ierr);
16154bbf5ea8SMatthew G. Knepley       }
16164bbf5ea8SMatthew G. Knepley 
16174bbf5ea8SMatthew G. Knepley       ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchY[i], patch->localY, ADD_VALUES, SCATTER_REVERSE);CHKERRQ(ierr);
16184bbf5ea8SMatthew G. Knepley     }
16194bbf5ea8SMatthew G. Knepley   }
16204bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {ierr = ISRestoreIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr);}
16214bbf5ea8SMatthew G. Knepley   /* XXX: should we do this on the global vector? */
1622*73ec7555SLawrence Mitchell   if (patch->partition_of_unity) {
16234bbf5ea8SMatthew G. Knepley     ierr = VecPointwiseMult(patch->localY, patch->localY, patch->dof_weights);CHKERRQ(ierr);
16244bbf5ea8SMatthew G. Knepley   }
16254bbf5ea8SMatthew G. Knepley   /* Now patch->localY contains the solution of the patch solves, so we need to combine them all. */
16264bbf5ea8SMatthew G. Knepley   ierr = VecSet(y, 0.0);CHKERRQ(ierr);
16274bbf5ea8SMatthew G. Knepley   ierr = VecGetArray(y, &globalY);CHKERRQ(ierr);
16284bbf5ea8SMatthew G. Knepley   ierr = VecGetArrayRead(patch->localY, &localY);CHKERRQ(ierr);
16294bbf5ea8SMatthew G. Knepley   ierr = PetscSFReduceBegin(patch->defaultSF, MPIU_SCALAR, localY, globalY, MPI_SUM);CHKERRQ(ierr);
16304bbf5ea8SMatthew G. Knepley   ierr = PetscSFReduceEnd(patch->defaultSF, MPIU_SCALAR, localY, globalY, MPI_SUM);CHKERRQ(ierr);
16314bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArrayRead(patch->localY, &localY);CHKERRQ(ierr);
16324bbf5ea8SMatthew G. Knepley 
16334bbf5ea8SMatthew G. Knepley   /* Now we need to send the global BC values through */
16344bbf5ea8SMatthew G. Knepley   ierr = VecGetArrayRead(x, &globalX);CHKERRQ(ierr);
16354bbf5ea8SMatthew G. Knepley   ierr = ISGetSize(patch->globalBcNodes, &numBcs);CHKERRQ(ierr);
16364bbf5ea8SMatthew G. Knepley   ierr = ISGetIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr);
16374bbf5ea8SMatthew G. Knepley   ierr = VecGetLocalSize(x, &n);CHKERRQ(ierr);
16384bbf5ea8SMatthew G. Knepley   for (bc = 0; bc < numBcs; ++bc) {
16394bbf5ea8SMatthew G. Knepley     const PetscInt idx = bcNodes[bc];
16404bbf5ea8SMatthew G. Knepley     if (idx < n) globalY[idx] = globalX[idx];
16414bbf5ea8SMatthew G. Knepley   }
16424bbf5ea8SMatthew G. Knepley 
16434bbf5ea8SMatthew G. Knepley   ierr = ISRestoreIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr);
16444bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArrayRead(x, &globalX);CHKERRQ(ierr);
16454bbf5ea8SMatthew G. Knepley   ierr = VecRestoreArray(y, &globalY);CHKERRQ(ierr);
16464bbf5ea8SMatthew G. Knepley 
16474bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsPopGetViewerOff();CHKERRQ(ierr);
16484bbf5ea8SMatthew G. Knepley   ierr = PetscLogEventEnd(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr);
16494bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
16504bbf5ea8SMatthew G. Knepley }
16514bbf5ea8SMatthew G. Knepley 
16524bbf5ea8SMatthew G. Knepley static PetscErrorCode PCReset_PATCH(PC pc)
16534bbf5ea8SMatthew G. Knepley {
16544bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
16554bbf5ea8SMatthew G. Knepley   PetscInt       i;
16564bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
16574bbf5ea8SMatthew G. Knepley 
16584bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
16594bbf5ea8SMatthew G. Knepley   /* TODO: Get rid of all these ifs */
16604bbf5ea8SMatthew G. Knepley   ierr = PetscSFDestroy(&patch->defaultSF);CHKERRQ(ierr);
16614bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->cellCounts);CHKERRQ(ierr);
16625f824522SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->pointCounts);CHKERRQ(ierr);
16634bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->cellNumbering);CHKERRQ(ierr);
16644bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->gtolCounts);CHKERRQ(ierr);
16654bbf5ea8SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->bcCounts);CHKERRQ(ierr);
16664bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->gtol);CHKERRQ(ierr);
16674bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->cells);CHKERRQ(ierr);
16685f824522SMatthew G. Knepley   ierr = ISDestroy(&patch->points);CHKERRQ(ierr);
16694bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->dofs);CHKERRQ(ierr);
16705f824522SMatthew G. Knepley   ierr = ISDestroy(&patch->offs);CHKERRQ(ierr);
16715f824522SMatthew G. Knepley   ierr = PetscSectionDestroy(&patch->patchSection);CHKERRQ(ierr);
16724bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->ghostBcNodes);CHKERRQ(ierr);
16734bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->globalBcNodes);CHKERRQ(ierr);
16744bbf5ea8SMatthew G. Knepley 
16755f824522SMatthew G. Knepley   if (patch->dofSection) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscSectionDestroy(&patch->dofSection[i]);CHKERRQ(ierr);}
16764bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->dofSection);CHKERRQ(ierr);
16774bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->bs);CHKERRQ(ierr);
16784bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->nodesPerCell);CHKERRQ(ierr);
16795f824522SMatthew G. Knepley   if (patch->cellNodeMap) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscFree(patch->cellNodeMap[i]);CHKERRQ(ierr);}
16804bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->cellNodeMap);CHKERRQ(ierr);
16814bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->subspaceOffsets);CHKERRQ(ierr);
16824bbf5ea8SMatthew G. Knepley 
16834bbf5ea8SMatthew G. Knepley   if (patch->bcs) {
16845f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->bcs[i]);CHKERRQ(ierr);}
16854bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->bcs);CHKERRQ(ierr);
16864bbf5ea8SMatthew G. Knepley   }
16874bbf5ea8SMatthew G. Knepley   if (patch->ksp) {
16885f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = KSPReset(patch->ksp[i]);CHKERRQ(ierr);}
16894bbf5ea8SMatthew G. Knepley   }
16904bbf5ea8SMatthew G. Knepley 
16914bbf5ea8SMatthew G. Knepley   ierr = VecDestroy(&patch->localX);CHKERRQ(ierr);
16924bbf5ea8SMatthew G. Knepley   ierr = VecDestroy(&patch->localY);CHKERRQ(ierr);
16934bbf5ea8SMatthew G. Knepley   if (patch->patchX) {
16945f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchX[i]);CHKERRQ(ierr);}
16954bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->patchX);CHKERRQ(ierr);
16964bbf5ea8SMatthew G. Knepley   }
16974bbf5ea8SMatthew G. Knepley   if (patch->patchY) {
16985f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchY[i]);CHKERRQ(ierr);}
16994bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->patchY);CHKERRQ(ierr);
17004bbf5ea8SMatthew G. Knepley   }
17014bbf5ea8SMatthew G. Knepley   ierr = VecDestroy(&patch->dof_weights);CHKERRQ(ierr);
17024bbf5ea8SMatthew G. Knepley   if (patch->patch_dof_weights) {
17035f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patch_dof_weights[i]);CHKERRQ(ierr);}
17044bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->patch_dof_weights);CHKERRQ(ierr);
17054bbf5ea8SMatthew G. Knepley   }
17064bbf5ea8SMatthew G. Knepley   if (patch->mat) {
17075f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->mat[i]);CHKERRQ(ierr);}
17084bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->mat);CHKERRQ(ierr);
17095f824522SMatthew G. Knepley   }
17104bbf5ea8SMatthew G. Knepley   ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr);
17115f824522SMatthew G. Knepley   if (patch->userIS) {
17125f824522SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->userIS[i]);CHKERRQ(ierr);}
17135f824522SMatthew G. Knepley     ierr = PetscFree(patch->userIS);CHKERRQ(ierr);
17145f824522SMatthew G. Knepley   }
17154bbf5ea8SMatthew G. Knepley   patch->bs          = 0;
17164bbf5ea8SMatthew G. Knepley   patch->cellNodeMap = NULL;
17177974b488SMatthew G. Knepley   patch->nsubspaces  = 0;
17184bbf5ea8SMatthew G. Knepley   ierr = ISDestroy(&patch->iterationSet);CHKERRQ(ierr);
17195f824522SMatthew G. Knepley 
17205f824522SMatthew G. Knepley   ierr = PetscViewerDestroy(&patch->viewerSection);CHKERRQ(ierr);
17214bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
17224bbf5ea8SMatthew G. Knepley }
17234bbf5ea8SMatthew G. Knepley 
17244bbf5ea8SMatthew G. Knepley static PetscErrorCode PCDestroy_PATCH(PC pc)
17254bbf5ea8SMatthew G. Knepley {
17264bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
17274bbf5ea8SMatthew G. Knepley   PetscInt       i;
17284bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
17294bbf5ea8SMatthew G. Knepley 
17304bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
17314bbf5ea8SMatthew G. Knepley   ierr = PCReset_PATCH(pc);CHKERRQ(ierr);
17324bbf5ea8SMatthew G. Knepley   if (patch->ksp) {
17334bbf5ea8SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {ierr = KSPDestroy(&patch->ksp[i]);CHKERRQ(ierr);}
17344bbf5ea8SMatthew G. Knepley     ierr = PetscFree(patch->ksp);CHKERRQ(ierr);
17354bbf5ea8SMatthew G. Knepley   }
17364bbf5ea8SMatthew G. Knepley   ierr = PetscFree(pc->data);CHKERRQ(ierr);
17374bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
17384bbf5ea8SMatthew G. Knepley }
17394bbf5ea8SMatthew G. Knepley 
17404bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetFromOptions_PATCH(PetscOptionItems *PetscOptionsObject, PC pc)
17414bbf5ea8SMatthew G. Knepley {
17424bbf5ea8SMatthew G. Knepley   PC_PATCH            *patch = (PC_PATCH *) pc->data;
17434bbf5ea8SMatthew G. Knepley   PCPatchConstructType patchConstructionType = PC_PATCH_STAR;
17445f824522SMatthew G. Knepley   char                 sub_mat_type[PETSC_MAX_PATH_LEN];
17455f824522SMatthew G. Knepley   const char          *prefix;
17464bbf5ea8SMatthew G. Knepley   PetscBool            flg, dimflg, codimflg;
17475f824522SMatthew G. Knepley   MPI_Comm             comm;
17484bbf5ea8SMatthew G. Knepley   PetscErrorCode       ierr;
17494bbf5ea8SMatthew G. Knepley 
17504bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
17515f824522SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) pc, &comm);CHKERRQ(ierr);
17525f824522SMatthew G. Knepley   ierr = PetscObjectGetOptionsPrefix((PetscObject) pc, &prefix);CHKERRQ(ierr);
17534bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsHead(PetscOptionsObject, "Vertex-patch Additive Schwarz options");CHKERRQ(ierr);
17544bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsBool("-pc_patch_save_operators",  "Store all patch operators for lifetime of PC?", "PCPatchSetSaveOperators", patch->save_operators, &patch->save_operators, &flg);CHKERRQ(ierr);
17554bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsBool("-pc_patch_partition_of_unity", "Weight contributions by dof multiplicity?", "PCPatchSetPartitionOfUnity", patch->partition_of_unity, &patch->partition_of_unity, &flg);CHKERRQ(ierr);
17565f824522SMatthew G. Knepley   ierr = PetscOptionsInt("-pc_patch_construct_dim", "What dimension of mesh point to construct patches by? (0 = vertices)", "PCPATCH", patch->dim, &patch->dim, &dimflg);CHKERRQ(ierr);
17575f824522SMatthew G. Knepley   ierr = PetscOptionsInt("-pc_patch_construct_codim", "What co-dimension of mesh point to construct patches by? (0 = cells)", "PCPATCH", patch->codim, &patch->codim, &codimflg);CHKERRQ(ierr);
17585f824522SMatthew G. Knepley   if (dimflg && codimflg) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Can only set one of dimension or co-dimension");CHKERRQ(ierr);
17594bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsEnum("-pc_patch_construct_type", "How should the patches be constructed?", "PCPatchSetConstructType", PCPatchConstructTypes, (PetscEnum) patchConstructionType, (PetscEnum *) &patchConstructionType, &flg);CHKERRQ(ierr);
17604bbf5ea8SMatthew G. Knepley   if (flg) {ierr = PCPatchSetConstructType(pc, patchConstructionType, NULL, NULL);CHKERRQ(ierr);}
17615f824522SMatthew G. Knepley   ierr = PetscOptionsInt("-pc_patch_vanka_dim", "Topological dimension of entities for Vanka to ignore", "PCPATCH", patch->vankadim, &patch->vankadim, &flg);CHKERRQ(ierr);
17625f824522SMatthew G. Knepley   ierr = PetscOptionsInt("-pc_patch_ignore_dim", "Topological dimension of entities for completion to ignore", "PCPATCH", patch->ignoredim, &patch->ignoredim, &flg);CHKERRQ(ierr);
17635f824522SMatthew G. Knepley   ierr = PetscOptionsFList("-pc_patch_sub_mat_type", "Matrix type for patch solves", "PCPatchSetSubMatType", MatList, NULL, sub_mat_type, PETSC_MAX_PATH_LEN, &flg);CHKERRQ(ierr);
17644bbf5ea8SMatthew G. Knepley   if (flg) {ierr = PCPatchSetSubMatType(pc, sub_mat_type);CHKERRQ(ierr);}
17655f824522SMatthew G. Knepley   ierr = PetscOptionsBool("-pc_patch_symmetrise_sweep", "Go start->end, end->start?", "PCPATCH", patch->symmetrise_sweep, &patch->symmetrise_sweep, &flg);CHKERRQ(ierr);
17665f824522SMatthew G. Knepley   ierr = PetscOptionsInt("-pc_patch_exclude_subspace", "What subspace (if any) to exclude in construction?", "PCPATCH", patch->exclude_subspace, &patch->exclude_subspace, &flg);CHKERRQ(ierr);
17675f824522SMatthew G. Knepley 
17685f824522SMatthew G. Knepley   ierr = PetscOptionsBool("-pc_patch_patches_view", "Print out information during patch construction", "PCPATCH", patch->viewPatches, &patch->viewPatches, &flg);CHKERRQ(ierr);
17695f824522SMatthew G. Knepley   ierr = PetscOptionsGetViewer(comm, prefix, "-pc_patch_cells_view",   &patch->viewerCells,   &patch->formatCells,   &patch->viewCells);CHKERRQ(ierr);
17705f824522SMatthew G. Knepley   ierr = PetscOptionsGetViewer(comm, prefix, "-pc_patch_points_view",  &patch->viewerPoints,  &patch->formatPoints,  &patch->viewPoints);CHKERRQ(ierr);
17715f824522SMatthew G. Knepley   ierr = PetscOptionsGetViewer(comm, prefix, "-pc_patch_section_view", &patch->viewerSection, &patch->formatSection, &patch->viewSection);CHKERRQ(ierr);
17725f824522SMatthew G. Knepley   ierr = PetscOptionsGetViewer(comm, prefix, "-pc_patch_sub_mat_view", &patch->viewerMatrix,  &patch->formatMatrix,  &patch->viewMatrix);CHKERRQ(ierr);
17734bbf5ea8SMatthew G. Knepley   ierr = PetscOptionsTail();CHKERRQ(ierr);
17745f824522SMatthew G. Knepley   patch->optionsSet = PETSC_TRUE;
17754bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
17764bbf5ea8SMatthew G. Knepley }
17774bbf5ea8SMatthew G. Knepley 
17784bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUpOnBlocks_PATCH(PC pc)
17794bbf5ea8SMatthew G. Knepley {
17804bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch = (PC_PATCH*) pc->data;
17814bbf5ea8SMatthew G. Knepley   KSPConvergedReason reason;
17824bbf5ea8SMatthew G. Knepley   PetscInt           i;
17834bbf5ea8SMatthew G. Knepley   PetscErrorCode     ierr;
17844bbf5ea8SMatthew G. Knepley 
17854bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
17864bbf5ea8SMatthew G. Knepley   for (i = 0; i < patch->npatch; ++i) {
17874bbf5ea8SMatthew G. Knepley     ierr = KSPSetUp(patch->ksp[i]);CHKERRQ(ierr);
17884bbf5ea8SMatthew G. Knepley     ierr = KSPGetConvergedReason(patch->ksp[i], &reason);CHKERRQ(ierr);
17894bbf5ea8SMatthew G. Knepley     if (reason == KSP_DIVERGED_PCSETUP_FAILED) pc->failedreason = PC_SUBPC_ERROR;
17904bbf5ea8SMatthew G. Knepley   }
17914bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
17924bbf5ea8SMatthew G. Knepley }
17934bbf5ea8SMatthew G. Knepley 
17944bbf5ea8SMatthew G. Knepley static PetscErrorCode PCView_PATCH(PC pc, PetscViewer viewer)
17954bbf5ea8SMatthew G. Knepley {
17964bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
17974bbf5ea8SMatthew G. Knepley   PetscViewer    sviewer;
17984bbf5ea8SMatthew G. Knepley   PetscBool      isascii;
17994bbf5ea8SMatthew G. Knepley   PetscMPIInt    rank;
18004bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
18014bbf5ea8SMatthew G. Knepley 
18024bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
18034bbf5ea8SMatthew G. Knepley   /* TODO Redo tabbing with set tbas in new style */
18044bbf5ea8SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii);CHKERRQ(ierr);
18054bbf5ea8SMatthew G. Knepley   if (!isascii) PetscFunctionReturn(0);
18064bbf5ea8SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) pc), &rank);CHKERRQ(ierr);
18074bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
18084bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "Subspace Correction preconditioner with %d patches\n", patch->npatch);CHKERRQ(ierr);
1809*73ec7555SLawrence Mitchell   ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: additive\n");CHKERRQ(ierr);
18104bbf5ea8SMatthew G. Knepley   if (patch->partition_of_unity) {ierr = PetscViewerASCIIPrintf(viewer, "Weighting by partition of unity\n");CHKERRQ(ierr);}
18114bbf5ea8SMatthew G. Knepley   else                           {ierr = PetscViewerASCIIPrintf(viewer, "Not weighting by partition of unity\n");CHKERRQ(ierr);}
18124bbf5ea8SMatthew G. Knepley   if (patch->symmetrise_sweep) {ierr = PetscViewerASCIIPrintf(viewer, "Symmetrising sweep (start->end, then end->start)\n");CHKERRQ(ierr);}
18134bbf5ea8SMatthew G. Knepley   else                         {ierr = PetscViewerASCIIPrintf(viewer, "Not symmetrising sweep\n");CHKERRQ(ierr);}
18144bbf5ea8SMatthew G. Knepley   if (!patch->save_operators) {ierr = PetscViewerASCIIPrintf(viewer, "Not saving patch operators (rebuilt every PCApply)\n");CHKERRQ(ierr);}
18154bbf5ea8SMatthew G. Knepley   else                        {ierr = PetscViewerASCIIPrintf(viewer, "Saving patch operators (rebuilt every PCSetUp)\n");CHKERRQ(ierr);}
18164bbf5ea8SMatthew G. Knepley   if (patch->patchconstructop == PCPatchConstruct_Star)       {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: star\n");CHKERRQ(ierr);}
18174bbf5ea8SMatthew G. Knepley   else if (patch->patchconstructop == PCPatchConstruct_Vanka) {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: Vanka\n");CHKERRQ(ierr);}
18184bbf5ea8SMatthew G. Knepley   else if (patch->patchconstructop == PCPatchConstruct_User)  {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: user-specified\n");CHKERRQ(ierr);}
18194bbf5ea8SMatthew G. Knepley   else                                                        {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: unknown\n");CHKERRQ(ierr);}
18204bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "KSP on patches (all same):\n");CHKERRQ(ierr);
18214bbf5ea8SMatthew G. Knepley   if (patch->ksp) {
18224bbf5ea8SMatthew G. Knepley     ierr = PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr);
18234bbf5ea8SMatthew G. Knepley     if (!rank) {
18244bbf5ea8SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(sviewer);CHKERRQ(ierr);
18254bbf5ea8SMatthew G. Knepley       ierr = KSPView(patch->ksp[0], sviewer);CHKERRQ(ierr);
18264bbf5ea8SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(sviewer);CHKERRQ(ierr);
18274bbf5ea8SMatthew G. Knepley     }
18284bbf5ea8SMatthew G. Knepley     ierr = PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr);
18294bbf5ea8SMatthew G. Knepley   } else {
18304bbf5ea8SMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
18314bbf5ea8SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "KSP not yet set.\n");CHKERRQ(ierr);
18324bbf5ea8SMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
18334bbf5ea8SMatthew G. Knepley   }
18344bbf5ea8SMatthew G. Knepley   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
18354bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
18364bbf5ea8SMatthew G. Knepley }
18374bbf5ea8SMatthew G. Knepley 
1838e5893cccSMatthew G. Knepley /*MC
1839e5893cccSMatthew G. Knepley   PCPATCH = "patch" - A PC object that encapsulates flexible definition of blocks for overlapping and non-overlapping
1840e5893cccSMatthew G. Knepley                       small block additive and multiplicative preconditioners. Block definition is based on topology from
1841e5893cccSMatthew G. Knepley                       a DM and equation numbering from a PetscSection.
1842e5893cccSMatthew G. Knepley 
1843e5893cccSMatthew G. Knepley   Options Database Keys:
1844e5893cccSMatthew G. Knepley + -pc_patch_cells_view   - Views the process local cell numbers for each patch
1845e5893cccSMatthew G. Knepley . -pc_patch_points_view  - Views the process local mesh point numbers for each patch
1846e5893cccSMatthew G. Knepley . -pc_patch_g2l_view     - Views the map between global dofs and patch local dofs for each patch
1847e5893cccSMatthew G. Knepley . -pc_patch_patches_view - Views the global dofs associated with each patch and its boundary
1848e5893cccSMatthew G. Knepley - -pc_patch_sub_mat_view - Views the matrix associated with each patch
1849e5893cccSMatthew G. Knepley 
1850e5893cccSMatthew G. Knepley   Level: intermediate
1851e5893cccSMatthew G. Knepley 
1852e5893cccSMatthew G. Knepley .seealso: PCType, PCCreate(), PCSetType()
1853e5893cccSMatthew G. Knepley M*/
1854642283e9SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PCCreate_Patch(PC pc)
18554bbf5ea8SMatthew G. Knepley {
18564bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch;
18574bbf5ea8SMatthew G. Knepley   PetscErrorCode ierr;
18584bbf5ea8SMatthew G. Knepley 
18594bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
18604bbf5ea8SMatthew G. Knepley   ierr = PetscNewLog(pc, &patch);CHKERRQ(ierr);
18614bbf5ea8SMatthew G. Knepley 
18624bbf5ea8SMatthew G. Knepley   /* Set some defaults */
18635f824522SMatthew G. Knepley   patch->combined           = PETSC_FALSE;
18644bbf5ea8SMatthew G. Knepley   patch->save_operators     = PETSC_TRUE;
18654bbf5ea8SMatthew G. Knepley   patch->partition_of_unity = PETSC_FALSE;
18664bbf5ea8SMatthew G. Knepley   patch->codim              = -1;
18674bbf5ea8SMatthew G. Knepley   patch->dim                = -1;
18684bbf5ea8SMatthew G. Knepley   patch->exclude_subspace   = -1;
18694bbf5ea8SMatthew G. Knepley   patch->vankadim           = -1;
18705f824522SMatthew G. Knepley   patch->ignoredim          = -1;
18714bbf5ea8SMatthew G. Knepley   patch->patchconstructop   = PCPatchConstruct_Star;
18724bbf5ea8SMatthew G. Knepley   patch->symmetrise_sweep   = PETSC_FALSE;
18735f824522SMatthew G. Knepley   patch->npatch             = 0;
18744bbf5ea8SMatthew G. Knepley   patch->userIS             = NULL;
18755f824522SMatthew G. Knepley   patch->optionsSet         = PETSC_FALSE;
18764bbf5ea8SMatthew G. Knepley   patch->iterationSet       = NULL;
18774bbf5ea8SMatthew G. Knepley   patch->user_patches       = PETSC_FALSE;
18785f824522SMatthew G. Knepley   ierr = PetscStrallocpy(MATDENSE, (char **) &patch->sub_mat_type);CHKERRQ(ierr);
18795f824522SMatthew G. Knepley   patch->viewPatches        = PETSC_FALSE;
18805f824522SMatthew G. Knepley   patch->viewCells          = PETSC_FALSE;
18815f824522SMatthew G. Knepley   patch->viewPoints         = PETSC_FALSE;
18825f824522SMatthew G. Knepley   patch->viewSection        = PETSC_FALSE;
18835f824522SMatthew G. Knepley   patch->viewMatrix         = PETSC_FALSE;
18844bbf5ea8SMatthew G. Knepley 
18854bbf5ea8SMatthew G. Knepley   pc->data                 = (void *) patch;
18864bbf5ea8SMatthew G. Knepley   pc->ops->apply           = PCApply_PATCH;
18874bbf5ea8SMatthew G. Knepley   pc->ops->applytranspose  = 0; /* PCApplyTranspose_PATCH; */
18884bbf5ea8SMatthew G. Knepley   pc->ops->setup           = PCSetUp_PATCH;
18894bbf5ea8SMatthew G. Knepley   pc->ops->reset           = PCReset_PATCH;
18904bbf5ea8SMatthew G. Knepley   pc->ops->destroy         = PCDestroy_PATCH;
18914bbf5ea8SMatthew G. Knepley   pc->ops->setfromoptions  = PCSetFromOptions_PATCH;
18924bbf5ea8SMatthew G. Knepley   pc->ops->setuponblocks   = PCSetUpOnBlocks_PATCH;
18934bbf5ea8SMatthew G. Knepley   pc->ops->view            = PCView_PATCH;
18944bbf5ea8SMatthew G. Knepley   pc->ops->applyrichardson = 0;
18954bbf5ea8SMatthew G. Knepley 
18964bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
18974bbf5ea8SMatthew G. Knepley }
1898