xref: /petsc/src/ksp/pc/impls/patch/pcpatch.c (revision 28b400f66ebc7ae0049166a2294dfcd3df27e64b)
14bbf5ea8SMatthew G. Knepley #include <petsc/private/pcpatchimpl.h>     /*I "petscpc.h" I*/
254ab768cSLawrence Mitchell #include <petsc/private/kspimpl.h>         /* For ksp->setfromoptionscalled */
39d4fc724SLawrence Mitchell #include <petsc/private/vecimpl.h>         /* For vec->map */
45f824522SMatthew G. Knepley #include <petsc/private/dmpleximpl.h> /* For DMPlexComputeJacobian_Patch_Internal() */
54bbf5ea8SMatthew G. Knepley #include <petscsf.h>
64bbf5ea8SMatthew G. Knepley #include <petscbt.h>
75f824522SMatthew G. Knepley #include <petscds.h>
8c73d2cf6SLawrence Mitchell #include <../src/mat/impls/dense/seq/dense.h> /*I "petscmat.h" I*/
94bbf5ea8SMatthew G. Knepley 
109d4fc724SLawrence Mitchell PetscLogEvent PC_Patch_CreatePatches, PC_Patch_ComputeOp, PC_Patch_Solve, PC_Patch_Apply, PC_Patch_Prealloc;
114bbf5ea8SMatthew G. Knepley 
129fbee547SJacob Faibussowitsch static inline PetscErrorCode ObjectView(PetscObject obj, PetscViewer viewer, PetscViewerFormat format)
135f824522SMatthew G. Knepley {
145f824522SMatthew G. Knepley 
155f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerPushFormat(viewer, format));
165f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectView(obj, viewer));
175f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerPopFormat(viewer));
187974b488SMatthew G. Knepley   return(0);
195f824522SMatthew G. Knepley }
205f824522SMatthew G. Knepley 
211b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_Star(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
224bbf5ea8SMatthew G. Knepley {
234bbf5ea8SMatthew G. Knepley   PetscInt       starSize;
244bbf5ea8SMatthew G. Knepley   PetscInt      *star = NULL, si;
254bbf5ea8SMatthew G. Knepley 
264bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
275f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHSetIClear(ht));
284bbf5ea8SMatthew G. Knepley   /* To start with, add the point we care about */
295f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHSetIAdd(ht, point));
304bbf5ea8SMatthew G. Knepley   /* Loop over all the points that this point connects to */
315f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star));
325f80ce2aSJacob Faibussowitsch   for (si = 0; si < starSize*2; si += 2) CHKERRQ(PetscHSetIAdd(ht, star[si]));
335f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star));
344bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
354bbf5ea8SMatthew G. Knepley }
364bbf5ea8SMatthew G. Knepley 
371b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_Vanka(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
384bbf5ea8SMatthew G. Knepley {
394bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) vpatch;
404bbf5ea8SMatthew G. Knepley   PetscInt       starSize;
414bbf5ea8SMatthew G. Knepley   PetscInt      *star = NULL;
424bbf5ea8SMatthew G. Knepley   PetscBool      shouldIgnore = PETSC_FALSE;
434bbf5ea8SMatthew G. Knepley   PetscInt       cStart, cEnd, iStart, iEnd, si;
444bbf5ea8SMatthew G. Knepley 
454bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
465f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHSetIClear(ht));
474bbf5ea8SMatthew G. Knepley   /* To start with, add the point we care about */
485f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHSetIAdd(ht, point));
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;
525f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetDepthStratum(dm, patch->vankadim, &iStart, &iEnd));
534bbf5ea8SMatthew G. Knepley   }
545f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
554bbf5ea8SMatthew G. Knepley   /* Loop over all the cells that this point connects to */
565f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star));
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 */
645f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
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;
705f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscHSetIAdd(ht, newpoint));
714bbf5ea8SMatthew G. Knepley     }
725f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
734bbf5ea8SMatthew G. Knepley   }
745f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star));
754bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
764bbf5ea8SMatthew G. Knepley }
774bbf5ea8SMatthew G. Knepley 
78e5b9877fSPatrick Farrell static PetscErrorCode PCPatchConstruct_Pardecomp(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
790a390943SPatrick Farrell {
80b525f888SPatrick Farrell   PC_PATCH       *patch = (PC_PATCH *) vpatch;
810a390943SPatrick Farrell   DMLabel         ghost = NULL;
820a390943SPatrick Farrell   const PetscInt *leaves;
830a390943SPatrick Farrell   PetscInt        nleaves, pStart, pEnd, loc;
840a390943SPatrick Farrell   PetscBool       isFiredrake;
850a390943SPatrick Farrell   PetscBool       flg;
86b525f888SPatrick Farrell   PetscInt        starSize;
87b525f888SPatrick Farrell   PetscInt       *star = NULL;
8825fd193aSPatrick Farrell   PetscInt        opoint, overlapi;
890a390943SPatrick Farrell 
900a390943SPatrick Farrell   PetscFunctionBegin;
915f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHSetIClear(ht));
920a390943SPatrick Farrell 
935f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetChart(dm, &pStart, &pEnd));
940a390943SPatrick Farrell 
955f80ce2aSJacob Faibussowitsch   CHKERRQ(DMHasLabel(dm, "pyop2_ghost", &isFiredrake));
960a390943SPatrick Farrell   if (isFiredrake) {
975f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLabel(dm, "pyop2_ghost", &ghost));
985f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelCreateIndex(ghost, pStart, pEnd));
990a390943SPatrick Farrell   } else {
1000a390943SPatrick Farrell     PetscSF sf;
1015f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetPointSF(dm, &sf));
1025f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL));
1030a390943SPatrick Farrell     nleaves = PetscMax(nleaves, 0);
1040a390943SPatrick Farrell   }
1050a390943SPatrick Farrell 
10625fd193aSPatrick Farrell   for (opoint = pStart; opoint < pEnd; ++opoint) {
1075f80ce2aSJacob Faibussowitsch     if (ghost) CHKERRQ(DMLabelHasPoint(ghost, opoint, &flg));
1085f80ce2aSJacob Faibussowitsch     else       {CHKERRQ(PetscFindInt(opoint, nleaves, leaves, &loc)); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;}
1090a390943SPatrick Farrell     /* Not an owned entity, don't make a cell patch. */
1100a390943SPatrick Farrell     if (flg) continue;
1115f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetIAdd(ht, opoint));
1120a390943SPatrick Farrell   }
1130a390943SPatrick Farrell 
114b525f888SPatrick Farrell   /* Now build the overlap for the patch */
11525fd193aSPatrick Farrell   for (overlapi = 0; overlapi < patch->pardecomp_overlap; ++overlapi) {
116b525f888SPatrick Farrell     PetscInt index = 0;
117b525f888SPatrick Farrell     PetscInt *htpoints = NULL;
118b525f888SPatrick Farrell     PetscInt htsize;
11925fd193aSPatrick Farrell     PetscInt i;
120b525f888SPatrick Farrell 
1215f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetIGetSize(ht, &htsize));
1225f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(htsize, &htpoints));
1235f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetIGetElems(ht, &index, htpoints));
124b525f888SPatrick Farrell 
12525fd193aSPatrick Farrell     for (i = 0; i < htsize; ++i) {
12625fd193aSPatrick Farrell       PetscInt hpoint = htpoints[i];
12725fd193aSPatrick Farrell       PetscInt si;
128b525f888SPatrick Farrell 
1295f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star));
13025fd193aSPatrick Farrell       for (si = 0; si < starSize*2; si += 2) {
131b525f888SPatrick Farrell         const PetscInt starp = star[si];
132b525f888SPatrick Farrell         PetscInt       closureSize;
133b525f888SPatrick Farrell         PetscInt      *closure = NULL, ci;
134b525f888SPatrick Farrell 
135b525f888SPatrick Farrell         /* now loop over all entities in the closure of starp */
1365f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure));
13725fd193aSPatrick Farrell         for (ci = 0; ci < closureSize*2; ci += 2) {
138b525f888SPatrick Farrell           const PetscInt closstarp = closure[ci];
1395f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscHSetIAdd(ht, closstarp));
140b525f888SPatrick Farrell         }
1415f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexRestoreTransitiveClosure(dm, starp, PETSC_TRUE, &closureSize, &closure));
142b525f888SPatrick Farrell       }
1435f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexRestoreTransitiveClosure(dm, hpoint, PETSC_FALSE, &starSize, &star));
144b525f888SPatrick Farrell     }
1455f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(htpoints));
146b525f888SPatrick Farrell   }
147b525f888SPatrick Farrell 
1480a390943SPatrick Farrell   PetscFunctionReturn(0);
1490a390943SPatrick Farrell }
1500a390943SPatrick Farrell 
1514bbf5ea8SMatthew G. Knepley /* The user's already set the patches in patch->userIS. Build the hash tables */
1521b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchConstruct_User(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
1534bbf5ea8SMatthew G. Knepley {
1544bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch   = (PC_PATCH *) vpatch;
1554bbf5ea8SMatthew G. Knepley   IS              patchis = patch->userIS[point];
1564bbf5ea8SMatthew G. Knepley   PetscInt        n;
1574bbf5ea8SMatthew G. Knepley   const PetscInt *patchdata;
1584bbf5ea8SMatthew G. Knepley   PetscInt        pStart, pEnd, i;
1594bbf5ea8SMatthew G. Knepley 
1604bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1615f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHSetIClear(ht));
1625f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetChart(dm, &pStart, &pEnd));
1635f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetLocalSize(patchis, &n));
1645f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetIndices(patchis, &patchdata));
1654bbf5ea8SMatthew G. Knepley   for (i = 0; i < n; ++i) {
1664bbf5ea8SMatthew G. Knepley     const PetscInt ownedpoint = patchdata[i];
1674bbf5ea8SMatthew G. Knepley 
1684bbf5ea8SMatthew G. Knepley     if (ownedpoint < pStart || ownedpoint >= pEnd) {
16998921bdaSJacob Faibussowitsch       SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D was not in [%D, %D)", ownedpoint, pStart, pEnd);
1704bbf5ea8SMatthew G. Knepley     }
1715f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetIAdd(ht, ownedpoint));
1724bbf5ea8SMatthew G. Knepley   }
1735f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(patchis, &patchdata));
1744bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
1754bbf5ea8SMatthew G. Knepley }
1764bbf5ea8SMatthew G. Knepley 
1774bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateDefaultSF_Private(PC pc, PetscInt n, const PetscSF *sf, const PetscInt *bs)
1784bbf5ea8SMatthew G. Knepley {
1794bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
1804bbf5ea8SMatthew G. Knepley   PetscInt       i;
1814bbf5ea8SMatthew G. Knepley 
1824bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1834bbf5ea8SMatthew G. Knepley   if (n == 1 && bs[0] == 1) {
1841bb6d2a8SBarry Smith     patch->sectionSF = sf[0];
1855f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectReference((PetscObject) patch->sectionSF));
1864bbf5ea8SMatthew G. Knepley   } else {
1874bbf5ea8SMatthew G. Knepley     PetscInt     allRoots = 0, allLeaves = 0;
1884bbf5ea8SMatthew G. Knepley     PetscInt     leafOffset = 0;
1894bbf5ea8SMatthew G. Knepley     PetscInt    *ilocal = NULL;
1904bbf5ea8SMatthew G. Knepley     PetscSFNode *iremote = NULL;
1914bbf5ea8SMatthew G. Knepley     PetscInt    *remoteOffsets = NULL;
1924bbf5ea8SMatthew G. Knepley     PetscInt     index = 0;
1931b68eb51SMatthew G. Knepley     PetscHMapI   rankToIndex;
1944bbf5ea8SMatthew G. Knepley     PetscInt     numRanks = 0;
1954bbf5ea8SMatthew G. Knepley     PetscSFNode *remote = NULL;
1964bbf5ea8SMatthew G. Knepley     PetscSF      rankSF;
1974bbf5ea8SMatthew G. Knepley     PetscInt    *ranks = NULL;
1984bbf5ea8SMatthew G. Knepley     PetscInt    *offsets = NULL;
1994bbf5ea8SMatthew G. Knepley     MPI_Datatype contig;
2001b68eb51SMatthew G. Knepley     PetscHSetI   ranksUniq;
2014bbf5ea8SMatthew G. Knepley 
2024bbf5ea8SMatthew G. Knepley     /* First figure out how many dofs there are in the concatenated numbering.
2034bbf5ea8SMatthew G. Knepley      * allRoots: number of owned global dofs;
2044bbf5ea8SMatthew G. Knepley      * allLeaves: number of visible dofs (global + ghosted).
2054bbf5ea8SMatthew G. Knepley      */
2064bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2074bbf5ea8SMatthew G. Knepley       PetscInt nroots, nleaves;
2084bbf5ea8SMatthew G. Knepley 
2095f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSFGetGraph(sf[i], &nroots, &nleaves, NULL, NULL));
2104bbf5ea8SMatthew G. Knepley       allRoots  += nroots * bs[i];
2114bbf5ea8SMatthew G. Knepley       allLeaves += nleaves * bs[i];
2124bbf5ea8SMatthew G. Knepley     }
2135f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(allLeaves, &ilocal));
2145f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(allLeaves, &iremote));
2154bbf5ea8SMatthew G. Knepley     /* Now build an SF that just contains process connectivity. */
2165f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetICreate(&ranksUniq));
2174bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2184bbf5ea8SMatthew G. Knepley       const PetscMPIInt *ranks = NULL;
2194bbf5ea8SMatthew G. Knepley       PetscInt           nranks, j;
2204bbf5ea8SMatthew G. Knepley 
2215f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSFSetUp(sf[i]));
2225f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSFGetRootRanks(sf[i], &nranks, &ranks, NULL, NULL, NULL));
2234bbf5ea8SMatthew G. Knepley       /* These are all the ranks who communicate with me. */
2244bbf5ea8SMatthew G. Knepley       for (j = 0; j < nranks; ++j) {
2255f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscHSetIAdd(ranksUniq, (PetscInt) ranks[j]));
2264bbf5ea8SMatthew G. Knepley       }
2274bbf5ea8SMatthew G. Knepley     }
2285f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetIGetSize(ranksUniq, &numRanks));
2295f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(numRanks, &remote));
2305f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(numRanks, &ranks));
2315f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetIGetElems(ranksUniq, &index, ranks));
2324bbf5ea8SMatthew G. Knepley 
2335f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHMapICreate(&rankToIndex));
2344bbf5ea8SMatthew G. Knepley     for (i = 0; i < numRanks; ++i) {
2354bbf5ea8SMatthew G. Knepley       remote[i].rank  = ranks[i];
2364bbf5ea8SMatthew G. Knepley       remote[i].index = 0;
2375f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscHMapISet(rankToIndex, ranks[i], i));
2384bbf5ea8SMatthew G. Knepley     }
2395f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(ranks));
2405f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetIDestroy(&ranksUniq));
2415f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFCreate(PetscObjectComm((PetscObject) pc), &rankSF));
2425f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFSetGraph(rankSF, 1, numRanks, NULL, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER));
2435f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFSetUp(rankSF));
2444bbf5ea8SMatthew G. Knepley     /* OK, use it to communicate the root offset on the remote
2454bbf5ea8SMatthew G. Knepley      * processes for each subspace. */
2465f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(n, &offsets));
2475f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(n*numRanks, &remoteOffsets));
2484bbf5ea8SMatthew G. Knepley 
2494bbf5ea8SMatthew G. Knepley     offsets[0] = 0;
2504bbf5ea8SMatthew G. Knepley     for (i = 1; i < n; ++i) {
2514bbf5ea8SMatthew G. Knepley       PetscInt nroots;
2524bbf5ea8SMatthew G. Knepley 
2535f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSFGetGraph(sf[i-1], &nroots, NULL, NULL, NULL));
2544bbf5ea8SMatthew G. Knepley       offsets[i] = offsets[i-1] + nroots*bs[i-1];
2554bbf5ea8SMatthew G. Knepley     }
2564bbf5ea8SMatthew G. Knepley     /* Offsets are the offsets on the current process of the
2574bbf5ea8SMatthew G. Knepley      * global dof numbering for the subspaces. */
2585f80ce2aSJacob Faibussowitsch     CHKERRMPI(MPI_Type_contiguous(n, MPIU_INT, &contig));
2595f80ce2aSJacob Faibussowitsch     CHKERRMPI(MPI_Type_commit(&contig));
2604bbf5ea8SMatthew G. Knepley 
2615f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFBcastBegin(rankSF, contig, offsets, remoteOffsets,MPI_REPLACE));
2625f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFBcastEnd(rankSF, contig, offsets, remoteOffsets,MPI_REPLACE));
2635f80ce2aSJacob Faibussowitsch     CHKERRMPI(MPI_Type_free(&contig));
2645f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(offsets));
2655f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFDestroy(&rankSF));
2664bbf5ea8SMatthew G. Knepley     /* Now remoteOffsets contains the offsets on the remote
2674bbf5ea8SMatthew G. Knepley      * processes who communicate with me.  So now we can
2684bbf5ea8SMatthew G. Knepley      * concatenate the list of SFs into a single one. */
2694bbf5ea8SMatthew G. Knepley     index = 0;
2704bbf5ea8SMatthew G. Knepley     for (i = 0; i < n; ++i) {
2714bbf5ea8SMatthew G. Knepley       const PetscSFNode *remote = NULL;
2724bbf5ea8SMatthew G. Knepley       const PetscInt    *local  = NULL;
2734bbf5ea8SMatthew G. Knepley       PetscInt           nroots, nleaves, j;
2744bbf5ea8SMatthew G. Knepley 
2755f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSFGetGraph(sf[i], &nroots, &nleaves, &local, &remote));
2764bbf5ea8SMatthew G. Knepley       for (j = 0; j < nleaves; ++j) {
2774bbf5ea8SMatthew G. Knepley         PetscInt rank = remote[j].rank;
2784bbf5ea8SMatthew G. Knepley         PetscInt idx, rootOffset, k;
2794bbf5ea8SMatthew G. Knepley 
2805f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscHMapIGet(rankToIndex, rank, &idx));
2812c71b3e2SJacob Faibussowitsch         PetscCheckFalse(idx == -1,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Didn't find rank, huh?");
2824bbf5ea8SMatthew G. Knepley         /* Offset on given rank for ith subspace */
2834bbf5ea8SMatthew G. Knepley         rootOffset = remoteOffsets[n*idx + i];
2844bbf5ea8SMatthew G. Knepley         for (k = 0; k < bs[i]; ++k) {
28573ec7555SLawrence Mitchell           ilocal[index]        = (local ? local[j] : j)*bs[i] + k + leafOffset;
2864bbf5ea8SMatthew G. Knepley           iremote[index].rank  = remote[j].rank;
2874bbf5ea8SMatthew G. Knepley           iremote[index].index = remote[j].index*bs[i] + k + rootOffset;
2884bbf5ea8SMatthew G. Knepley           ++index;
2894bbf5ea8SMatthew G. Knepley         }
2904bbf5ea8SMatthew G. Knepley       }
2914bbf5ea8SMatthew G. Knepley       leafOffset += nleaves * bs[i];
2924bbf5ea8SMatthew G. Knepley     }
2935f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHMapIDestroy(&rankToIndex));
2945f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(remoteOffsets));
2955f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFCreate(PetscObjectComm((PetscObject)pc), &patch->sectionSF));
2965f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFSetGraph(patch->sectionSF, allRoots, allLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER));
2974bbf5ea8SMatthew G. Knepley   }
2984bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
2994bbf5ea8SMatthew G. Knepley }
3004bbf5ea8SMatthew G. Knepley 
301c73d2cf6SLawrence Mitchell PetscErrorCode PCPatchSetDenseInverse(PC pc, PetscBool flg)
302c73d2cf6SLawrence Mitchell {
303c73d2cf6SLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *) pc->data;
304c73d2cf6SLawrence Mitchell   PetscFunctionBegin;
305c73d2cf6SLawrence Mitchell   patch->denseinverse = flg;
306c73d2cf6SLawrence Mitchell   PetscFunctionReturn(0);
307c73d2cf6SLawrence Mitchell }
308c73d2cf6SLawrence Mitchell 
309c73d2cf6SLawrence Mitchell PetscErrorCode PCPatchGetDenseInverse(PC pc, PetscBool *flg)
310c73d2cf6SLawrence Mitchell {
311c73d2cf6SLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *) pc->data;
312c73d2cf6SLawrence Mitchell   PetscFunctionBegin;
313c73d2cf6SLawrence Mitchell   *flg = patch->denseinverse;
314c73d2cf6SLawrence Mitchell   PetscFunctionReturn(0);
315c73d2cf6SLawrence Mitchell }
316c73d2cf6SLawrence Mitchell 
3174bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3185f824522SMatthew G. Knepley PetscErrorCode PCPatchSetIgnoreDim(PC pc, PetscInt dim)
3195f824522SMatthew G. Knepley {
3205f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3215f824522SMatthew G. Knepley   PetscFunctionBegin;
3225f824522SMatthew G. Knepley   patch->ignoredim = dim;
3235f824522SMatthew G. Knepley   PetscFunctionReturn(0);
3245f824522SMatthew G. Knepley }
3255f824522SMatthew G. Knepley 
3265f824522SMatthew G. Knepley /* TODO: Docs */
3275f824522SMatthew G. Knepley PetscErrorCode PCPatchGetIgnoreDim(PC pc, PetscInt *dim)
3285f824522SMatthew G. Knepley {
3295f824522SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3305f824522SMatthew G. Knepley   PetscFunctionBegin;
3315f824522SMatthew G. Knepley   *dim = patch->ignoredim;
3325f824522SMatthew G. Knepley   PetscFunctionReturn(0);
3335f824522SMatthew G. Knepley }
3345f824522SMatthew G. Knepley 
3355f824522SMatthew G. Knepley /* TODO: Docs */
3364bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetSaveOperators(PC pc, PetscBool flg)
3374bbf5ea8SMatthew G. Knepley {
3384bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3394bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3404bbf5ea8SMatthew G. Knepley   patch->save_operators = flg;
3414bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3424bbf5ea8SMatthew G. Knepley }
3434bbf5ea8SMatthew G. Knepley 
3444bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3454bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetSaveOperators(PC pc, PetscBool *flg)
3464bbf5ea8SMatthew G. Knepley {
3474bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3484bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3494bbf5ea8SMatthew G. Knepley   *flg = patch->save_operators;
3504bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3514bbf5ea8SMatthew G. Knepley }
3524bbf5ea8SMatthew G. Knepley 
3534bbf5ea8SMatthew G. Knepley /* TODO: Docs */
354fa84ea4cSLawrence Mitchell PetscErrorCode PCPatchSetPrecomputeElementTensors(PC pc, PetscBool flg)
355fa84ea4cSLawrence Mitchell {
356fa84ea4cSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *) pc->data;
357fa84ea4cSLawrence Mitchell   PetscFunctionBegin;
358fa84ea4cSLawrence Mitchell   patch->precomputeElementTensors = flg;
359fa84ea4cSLawrence Mitchell   PetscFunctionReturn(0);
360fa84ea4cSLawrence Mitchell }
361fa84ea4cSLawrence Mitchell 
362fa84ea4cSLawrence Mitchell /* TODO: Docs */
363fa84ea4cSLawrence Mitchell PetscErrorCode PCPatchGetPrecomputeElementTensors(PC pc, PetscBool *flg)
364fa84ea4cSLawrence Mitchell {
365fa84ea4cSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *) pc->data;
366fa84ea4cSLawrence Mitchell   PetscFunctionBegin;
367fa84ea4cSLawrence Mitchell   *flg = patch->precomputeElementTensors;
368fa84ea4cSLawrence Mitchell   PetscFunctionReturn(0);
369fa84ea4cSLawrence Mitchell }
370fa84ea4cSLawrence Mitchell 
371fa84ea4cSLawrence Mitchell /* TODO: Docs */
3724bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetPartitionOfUnity(PC pc, PetscBool flg)
3734bbf5ea8SMatthew G. Knepley {
3744bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3754bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3764bbf5ea8SMatthew G. Knepley   patch->partition_of_unity = flg;
3774bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3784bbf5ea8SMatthew G. Knepley }
3794bbf5ea8SMatthew G. Knepley 
3804bbf5ea8SMatthew G. Knepley /* TODO: Docs */
3814bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetPartitionOfUnity(PC pc, PetscBool *flg)
3824bbf5ea8SMatthew G. Knepley {
3834bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
3844bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3854bbf5ea8SMatthew G. Knepley   *flg = patch->partition_of_unity;
3864bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3874bbf5ea8SMatthew G. Knepley }
3884bbf5ea8SMatthew G. Knepley 
3894bbf5ea8SMatthew G. Knepley /* TODO: Docs */
39061c4b389SFlorian Wechsung PetscErrorCode PCPatchSetLocalComposition(PC pc, PCCompositeType type)
391c2e6f3c0SFlorian Wechsung {
392c2e6f3c0SFlorian Wechsung   PC_PATCH *patch = (PC_PATCH *) pc->data;
393c2e6f3c0SFlorian Wechsung   PetscFunctionBegin;
3942c71b3e2SJacob Faibussowitsch   PetscCheckFalse(type != PC_COMPOSITE_ADDITIVE && type != PC_COMPOSITE_MULTIPLICATIVE,PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Only supports additive or multiplicative as the local type");
39561c4b389SFlorian Wechsung   patch->local_composition_type = type;
396c2e6f3c0SFlorian Wechsung   PetscFunctionReturn(0);
397c2e6f3c0SFlorian Wechsung }
398c2e6f3c0SFlorian Wechsung 
399c2e6f3c0SFlorian Wechsung /* TODO: Docs */
40061c4b389SFlorian Wechsung PetscErrorCode PCPatchGetLocalComposition(PC pc, PCCompositeType *type)
401c2e6f3c0SFlorian Wechsung {
402c2e6f3c0SFlorian Wechsung   PC_PATCH *patch = (PC_PATCH *) pc->data;
403c2e6f3c0SFlorian Wechsung   PetscFunctionBegin;
40461c4b389SFlorian Wechsung   *type = patch->local_composition_type;
405c2e6f3c0SFlorian Wechsung   PetscFunctionReturn(0);
406c2e6f3c0SFlorian Wechsung }
407c2e6f3c0SFlorian Wechsung 
408c2e6f3c0SFlorian Wechsung /* TODO: Docs */
4094bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetSubMatType(PC pc, MatType sub_mat_type)
4104bbf5ea8SMatthew G. Knepley {
4114bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
4124bbf5ea8SMatthew G. Knepley 
4134bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4145f80ce2aSJacob Faibussowitsch   if (patch->sub_mat_type) CHKERRQ(PetscFree(patch->sub_mat_type));
4155f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrallocpy(sub_mat_type, (char **) &patch->sub_mat_type));
4164bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4174bbf5ea8SMatthew G. Knepley }
4184bbf5ea8SMatthew G. Knepley 
4194bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4204bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetSubMatType(PC pc, MatType *sub_mat_type)
4214bbf5ea8SMatthew G. Knepley {
4224bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4234bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4244bbf5ea8SMatthew G. Knepley   *sub_mat_type = patch->sub_mat_type;
4254bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4264bbf5ea8SMatthew G. Knepley }
4274bbf5ea8SMatthew G. Knepley 
4284bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4294bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetCellNumbering(PC pc, PetscSection cellNumbering)
4304bbf5ea8SMatthew G. Knepley {
4314bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
4324bbf5ea8SMatthew G. Knepley 
4334bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4344bbf5ea8SMatthew G. Knepley   patch->cellNumbering = cellNumbering;
4355f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject) cellNumbering));
4364bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4374bbf5ea8SMatthew G. Knepley }
4384bbf5ea8SMatthew G. Knepley 
4394bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4404bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetCellNumbering(PC pc, PetscSection *cellNumbering)
4414bbf5ea8SMatthew G. Knepley {
4424bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4434bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4444bbf5ea8SMatthew G. Knepley   *cellNumbering = patch->cellNumbering;
4454bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4464bbf5ea8SMatthew G. Knepley }
4474bbf5ea8SMatthew G. Knepley 
4484bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4494bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetConstructType(PC pc, PCPatchConstructType ctype, PetscErrorCode (*func)(PC, PetscInt *, IS **, IS *, void *), void *ctx)
4504bbf5ea8SMatthew G. Knepley {
4514bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4524bbf5ea8SMatthew G. Knepley 
4534bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4544bbf5ea8SMatthew G. Knepley   patch->ctype = ctype;
4554bbf5ea8SMatthew G. Knepley   switch (ctype) {
4564bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
45740c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
4584bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Star;
4594bbf5ea8SMatthew G. Knepley     break;
4604bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
46140c17a03SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
4624bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_Vanka;
4634bbf5ea8SMatthew G. Knepley     break;
464e5b9877fSPatrick Farrell   case PC_PATCH_PARDECOMP:
4650a390943SPatrick Farrell     patch->user_patches     = PETSC_FALSE;
466e5b9877fSPatrick Farrell     patch->patchconstructop = PCPatchConstruct_Pardecomp;
4670a390943SPatrick Farrell     break;
4684bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
4694bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
4704bbf5ea8SMatthew G. Knepley     patch->user_patches     = PETSC_TRUE;
4714bbf5ea8SMatthew G. Knepley     patch->patchconstructop = PCPatchConstruct_User;
472bdd9e0cdSPatrick Farrell     if (func) {
4734bbf5ea8SMatthew G. Knepley       patch->userpatchconstructionop = func;
4744bbf5ea8SMatthew G. Knepley       patch->userpatchconstructctx   = ctx;
475bdd9e0cdSPatrick Farrell     }
4764bbf5ea8SMatthew G. Knepley     break;
4774bbf5ea8SMatthew G. Knepley   default:
47898921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype);
4794bbf5ea8SMatthew G. Knepley   }
4804bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4814bbf5ea8SMatthew G. Knepley }
4824bbf5ea8SMatthew G. Knepley 
4834bbf5ea8SMatthew G. Knepley /* TODO: Docs */
4844bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchGetConstructType(PC pc, PCPatchConstructType *ctype, PetscErrorCode (**func)(PC, PetscInt *, IS **, IS *, void *), void **ctx)
4854bbf5ea8SMatthew G. Knepley {
4864bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
4874bbf5ea8SMatthew G. Knepley 
4884bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
4894bbf5ea8SMatthew G. Knepley   *ctype = patch->ctype;
4904bbf5ea8SMatthew G. Knepley   switch (patch->ctype) {
4914bbf5ea8SMatthew G. Knepley   case PC_PATCH_STAR:
4924bbf5ea8SMatthew G. Knepley   case PC_PATCH_VANKA:
493e5b9877fSPatrick Farrell   case PC_PATCH_PARDECOMP:
4944bbf5ea8SMatthew G. Knepley     break;
4954bbf5ea8SMatthew G. Knepley   case PC_PATCH_USER:
4964bbf5ea8SMatthew G. Knepley   case PC_PATCH_PYTHON:
4974bbf5ea8SMatthew G. Knepley     *func = patch->userpatchconstructionop;
4984bbf5ea8SMatthew G. Knepley     *ctx  = patch->userpatchconstructctx;
4994bbf5ea8SMatthew G. Knepley     break;
5004bbf5ea8SMatthew G. Knepley   default:
50198921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype);
5024bbf5ea8SMatthew G. Knepley   }
5034bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5044bbf5ea8SMatthew G. Knepley }
5054bbf5ea8SMatthew G. Knepley 
5064bbf5ea8SMatthew G. Knepley /* TODO: Docs */
5074bbf5ea8SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfo(PC pc, PetscInt nsubspaces, DM *dms, PetscInt *bs, PetscInt *nodesPerCell, const PetscInt **cellNodeMap,
5084bbf5ea8SMatthew G. Knepley                                             const PetscInt *subspaceOffsets, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
5094bbf5ea8SMatthew G. Knepley {
5104bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
511b6bb21d1SLawrence Mitchell   DM             dm, plex;
5124bbf5ea8SMatthew G. Knepley   PetscSF       *sfs;
5135f824522SMatthew G. Knepley   PetscInt       cStart, cEnd, i, j;
5144bbf5ea8SMatthew G. Knepley 
5154bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
5165f80ce2aSJacob Faibussowitsch   CHKERRQ(PCGetDM(pc, &dm));
5175f80ce2aSJacob Faibussowitsch   CHKERRQ(DMConvert(dm, DMPLEX, &plex));
518b6bb21d1SLawrence Mitchell   dm = plex;
5195f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
5205f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(nsubspaces, &sfs));
5215f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(nsubspaces, &patch->dofSection));
5225f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(nsubspaces, &patch->bs));
5235f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(nsubspaces, &patch->nodesPerCell));
5245f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(nsubspaces, &patch->cellNodeMap));
5255f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(nsubspaces+1, &patch->subspaceOffsets));
5264bbf5ea8SMatthew G. Knepley 
5274bbf5ea8SMatthew G. Knepley   patch->nsubspaces       = nsubspaces;
5284bbf5ea8SMatthew G. Knepley   patch->totalDofsPerCell = 0;
5294bbf5ea8SMatthew G. Knepley   for (i = 0; i < nsubspaces; ++i) {
5305f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLocalSection(dms[i], &patch->dofSection[i]));
5315f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectReference((PetscObject) patch->dofSection[i]));
5325f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetSectionSF(dms[i], &sfs[i]));
5334bbf5ea8SMatthew G. Knepley     patch->bs[i]              = bs[i];
5344bbf5ea8SMatthew G. Knepley     patch->nodesPerCell[i]    = nodesPerCell[i];
5354bbf5ea8SMatthew G. Knepley     patch->totalDofsPerCell  += nodesPerCell[i]*bs[i];
5365f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i]));
53780e8a965SFlorian Wechsung     for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
5384bbf5ea8SMatthew G. Knepley     patch->subspaceOffsets[i] = subspaceOffsets[i];
5394bbf5ea8SMatthew G. Knepley   }
5405f80ce2aSJacob Faibussowitsch   CHKERRQ(PCPatchCreateDefaultSF_Private(pc, nsubspaces, sfs, patch->bs));
5415f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(sfs));
5424bbf5ea8SMatthew G. Knepley 
5434bbf5ea8SMatthew G. Knepley   patch->subspaceOffsets[nsubspaces] = subspaceOffsets[nsubspaces];
5445f80ce2aSJacob Faibussowitsch   CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes));
5455f80ce2aSJacob Faibussowitsch   CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes));
5465f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&dm));
5474bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5484bbf5ea8SMatthew G. Knepley }
5494bbf5ea8SMatthew G. Knepley 
5504bbf5ea8SMatthew G. Knepley /* TODO: Docs */
5515f824522SMatthew G. Knepley PetscErrorCode PCPatchSetDiscretisationInfoCombined(PC pc, DM dm, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
5525f824522SMatthew G. Knepley {
5535f824522SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
5545f824522SMatthew G. Knepley   PetscInt       cStart, cEnd, i, j;
5555f824522SMatthew G. Knepley 
5565f824522SMatthew G. Knepley   PetscFunctionBegin;
5575f824522SMatthew G. Knepley   patch->combined = PETSC_TRUE;
5585f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
5595f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetNumFields(dm, &patch->nsubspaces));
5605f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscCalloc1(patch->nsubspaces, &patch->dofSection));
5615f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(patch->nsubspaces, &patch->bs));
5625f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(patch->nsubspaces, &patch->nodesPerCell));
5635f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(patch->nsubspaces, &patch->cellNodeMap));
5645f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscCalloc1(patch->nsubspaces+1, &patch->subspaceOffsets));
5655f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLocalSection(dm, &patch->dofSection[0]));
5665f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject) patch->dofSection[0]));
5675f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetStorageSize(patch->dofSection[0], &patch->subspaceOffsets[patch->nsubspaces]));
5685f824522SMatthew G. Knepley   patch->totalDofsPerCell = 0;
5695f824522SMatthew G. Knepley   for (i = 0; i < patch->nsubspaces; ++i) {
5705f824522SMatthew G. Knepley     patch->bs[i]             = 1;
5715f824522SMatthew G. Knepley     patch->nodesPerCell[i]   = nodesPerCell[i];
5725f824522SMatthew G. Knepley     patch->totalDofsPerCell += nodesPerCell[i];
5735f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i]));
5745f824522SMatthew G. Knepley     for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
5755f824522SMatthew G. Knepley   }
5765f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetSectionSF(dm, &patch->sectionSF));
5775f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject) patch->sectionSF));
5785f80ce2aSJacob Faibussowitsch   CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes));
5795f80ce2aSJacob Faibussowitsch   CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes));
5805f824522SMatthew G. Knepley   PetscFunctionReturn(0);
5815f824522SMatthew G. Knepley }
5825f824522SMatthew G. Knepley 
5835f824522SMatthew G. Knepley /*@C
5845f824522SMatthew G. Knepley 
58592d50984SMatthew G. Knepley   PCPatchSetComputeFunction - Set the callback used to compute patch residuals
58692d50984SMatthew G. Knepley 
58799b7e5c6SPatrick Farrell   Logically collective on PC
58899b7e5c6SPatrick Farrell 
58992d50984SMatthew G. Knepley   Input Parameters:
59092d50984SMatthew G. Knepley + pc   - The PC
59192d50984SMatthew G. Knepley . func - The callback
59292d50984SMatthew G. Knepley - ctx  - The user context
59392d50984SMatthew G. Knepley 
5947a50e09dSPatrick Farrell   Calling sequence of func:
5957a50e09dSPatrick Farrell $   func (PC pc,PetscInt point,Vec x,Vec f,IS cellIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx)
5967a50e09dSPatrick Farrell 
5977a50e09dSPatrick Farrell +  pc               - The PC
5987a50e09dSPatrick Farrell .  point            - The point
5997a50e09dSPatrick Farrell .  x                - The input solution (not used in linear problems)
6007a50e09dSPatrick Farrell .  f                - The patch residual vector
6017a50e09dSPatrick Farrell .  cellIS           - An array of the cell numbers
6027a50e09dSPatrick Farrell .  n                - The size of dofsArray
6037a50e09dSPatrick Farrell .  dofsArray        - The dofmap for the dofs to be solved for
6047a50e09dSPatrick Farrell .  dofsArrayWithAll - The dofmap for all dofs on the patch
6057a50e09dSPatrick Farrell -  ctx              - The user context
6067a50e09dSPatrick Farrell 
60792d50984SMatthew G. Knepley   Level: advanced
60892d50984SMatthew G. Knepley 
6097a50e09dSPatrick Farrell   Notes:
61026dc5b63SLawrence Mitchell   The entries of F (the output residual vector) have been set to zero before the call.
61192d50984SMatthew G. Knepley 
61226dc5b63SLawrence Mitchell .seealso: PCPatchSetComputeOperator(), PCPatchGetComputeOperator(), PCPatchSetDiscretisationInfo(), PCPatchSetComputeFunctionInteriorFacets()
61392d50984SMatthew G. Knepley @*/
61439fd2e8aSPatrick Farrell PetscErrorCode PCPatchSetComputeFunction(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Vec, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
61592d50984SMatthew G. Knepley {
61692d50984SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
61792d50984SMatthew G. Knepley 
61892d50984SMatthew G. Knepley   PetscFunctionBegin;
61992d50984SMatthew G. Knepley   patch->usercomputef    = func;
62092d50984SMatthew G. Knepley   patch->usercomputefctx = ctx;
62192d50984SMatthew G. Knepley   PetscFunctionReturn(0);
62292d50984SMatthew G. Knepley }
62392d50984SMatthew G. Knepley 
62492d50984SMatthew G. Knepley /*@C
62592d50984SMatthew G. Knepley 
62659109abcSLawrence Mitchell   PCPatchSetComputeFunctionInteriorFacets - Set the callback used to compute facet integrals for patch residuals
62759109abcSLawrence Mitchell 
6287a50e09dSPatrick Farrell   Logically collective on PC
6297a50e09dSPatrick Farrell 
63059109abcSLawrence Mitchell   Input Parameters:
63159109abcSLawrence Mitchell + pc   - The PC
63259109abcSLawrence Mitchell . func - The callback
63359109abcSLawrence Mitchell - ctx  - The user context
63459109abcSLawrence Mitchell 
6357a50e09dSPatrick Farrell   Calling sequence of func:
6367a50e09dSPatrick Farrell $   func (PC pc,PetscInt point,Vec x,Vec f,IS facetIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx)
6377a50e09dSPatrick Farrell 
6387a50e09dSPatrick Farrell +  pc               - The PC
6397a50e09dSPatrick Farrell .  point            - The point
6407a50e09dSPatrick Farrell .  x                - The input solution (not used in linear problems)
6417a50e09dSPatrick Farrell .  f                - The patch residual vector
6427a50e09dSPatrick Farrell .  facetIS          - An array of the facet numbers
6437a50e09dSPatrick Farrell .  n                - The size of dofsArray
6447a50e09dSPatrick Farrell .  dofsArray        - The dofmap for the dofs to be solved for
6457a50e09dSPatrick Farrell .  dofsArrayWithAll - The dofmap for all dofs on the patch
6467a50e09dSPatrick Farrell -  ctx              - The user context
6477a50e09dSPatrick Farrell 
64859109abcSLawrence Mitchell   Level: advanced
64959109abcSLawrence Mitchell 
6507a50e09dSPatrick Farrell   Notes:
65126dc5b63SLawrence Mitchell   The entries of F (the output residual vector) have been set to zero before the call.
65259109abcSLawrence Mitchell 
65326dc5b63SLawrence Mitchell .seealso: PCPatchSetComputeOperator(), PCPatchGetComputeOperator(), PCPatchSetDiscretisationInfo(), PCPatchSetComputeFunction()
65459109abcSLawrence Mitchell @*/
65559109abcSLawrence Mitchell PetscErrorCode PCPatchSetComputeFunctionInteriorFacets(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Vec, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
65659109abcSLawrence Mitchell {
65759109abcSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *) pc->data;
65859109abcSLawrence Mitchell 
65959109abcSLawrence Mitchell   PetscFunctionBegin;
66059109abcSLawrence Mitchell   patch->usercomputefintfacet    = func;
66159109abcSLawrence Mitchell   patch->usercomputefintfacetctx = ctx;
66259109abcSLawrence Mitchell   PetscFunctionReturn(0);
66359109abcSLawrence Mitchell }
66459109abcSLawrence Mitchell 
66559109abcSLawrence Mitchell /*@C
66659109abcSLawrence Mitchell 
6675f824522SMatthew G. Knepley   PCPatchSetComputeOperator - Set the callback used to compute patch matrices
6685f824522SMatthew G. Knepley 
6697a50e09dSPatrick Farrell   Logically collective on PC
6707a50e09dSPatrick Farrell 
6715f824522SMatthew G. Knepley   Input Parameters:
6725f824522SMatthew G. Knepley + pc   - The PC
6735f824522SMatthew G. Knepley . func - The callback
6745f824522SMatthew G. Knepley - ctx  - The user context
6755f824522SMatthew G. Knepley 
6767a50e09dSPatrick Farrell   Calling sequence of func:
6777a50e09dSPatrick Farrell $   func (PC pc,PetscInt point,Vec x,Mat mat,IS facetIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx)
6787a50e09dSPatrick Farrell 
6797a50e09dSPatrick Farrell +  pc               - The PC
6807a50e09dSPatrick Farrell .  point            - The point
6817a50e09dSPatrick Farrell .  x                - The input solution (not used in linear problems)
6827a50e09dSPatrick Farrell .  mat              - The patch matrix
6837a50e09dSPatrick Farrell .  cellIS           - An array of the cell numbers
6847a50e09dSPatrick Farrell .  n                - The size of dofsArray
6857a50e09dSPatrick Farrell .  dofsArray        - The dofmap for the dofs to be solved for
6867a50e09dSPatrick Farrell .  dofsArrayWithAll - The dofmap for all dofs on the patch
6877a50e09dSPatrick Farrell -  ctx              - The user context
6887a50e09dSPatrick Farrell 
6895f824522SMatthew G. Knepley   Level: advanced
6905f824522SMatthew G. Knepley 
6917a50e09dSPatrick Farrell   Notes:
6927a50e09dSPatrick Farrell   The matrix entries have been set to zero before the call.
6935f824522SMatthew G. Knepley 
694723f9013SMatthew G. Knepley .seealso: PCPatchGetComputeOperator(), PCPatchSetComputeFunction(), PCPatchSetDiscretisationInfo()
6955f824522SMatthew G. Knepley @*/
6964d04e9f1SPatrick Farrell PetscErrorCode PCPatchSetComputeOperator(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Mat, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
6974bbf5ea8SMatthew G. Knepley {
6984bbf5ea8SMatthew G. Knepley   PC_PATCH *patch = (PC_PATCH *) pc->data;
6994bbf5ea8SMatthew G. Knepley 
7004bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
7014bbf5ea8SMatthew G. Knepley   patch->usercomputeop    = func;
702723f9013SMatthew G. Knepley   patch->usercomputeopctx = ctx;
7034bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
7044bbf5ea8SMatthew G. Knepley }
7054bbf5ea8SMatthew G. Knepley 
70659109abcSLawrence Mitchell /*@C
70759109abcSLawrence Mitchell 
7087a50e09dSPatrick Farrell   PCPatchSetComputeOperatorInteriorFacets - Set the callback used to compute facet integrals for patch matrices
70959109abcSLawrence Mitchell 
71099b7e5c6SPatrick Farrell   Logically collective on PC
71199b7e5c6SPatrick Farrell 
71259109abcSLawrence Mitchell   Input Parameters:
71359109abcSLawrence Mitchell + pc   - The PC
71459109abcSLawrence Mitchell . func - The callback
71559109abcSLawrence Mitchell - ctx  - The user context
71659109abcSLawrence Mitchell 
7177a50e09dSPatrick Farrell   Calling sequence of func:
7187a50e09dSPatrick Farrell $   func (PC pc,PetscInt point,Vec x,Mat mat,IS facetIS,PetscInt n,const PetscInt* dofsArray,const PetscInt* dofsArrayWithAll,void* ctx)
7197a50e09dSPatrick Farrell 
7207a50e09dSPatrick Farrell +  pc               - The PC
7217a50e09dSPatrick Farrell .  point            - The point
7227a50e09dSPatrick Farrell .  x                - The input solution (not used in linear problems)
7237a50e09dSPatrick Farrell .  mat              - The patch matrix
7247a50e09dSPatrick Farrell .  facetIS          - An array of the facet numbers
7257a50e09dSPatrick Farrell .  n                - The size of dofsArray
7267a50e09dSPatrick Farrell .  dofsArray        - The dofmap for the dofs to be solved for
7277a50e09dSPatrick Farrell .  dofsArrayWithAll - The dofmap for all dofs on the patch
7287a50e09dSPatrick Farrell -  ctx              - The user context
7297a50e09dSPatrick Farrell 
73059109abcSLawrence Mitchell   Level: advanced
73159109abcSLawrence Mitchell 
7327a50e09dSPatrick Farrell   Notes:
7337a50e09dSPatrick Farrell   The matrix entries have been set to zero before the call.
73459109abcSLawrence Mitchell 
73559109abcSLawrence Mitchell .seealso: PCPatchGetComputeOperator(), PCPatchSetComputeFunction(), PCPatchSetDiscretisationInfo()
73659109abcSLawrence Mitchell @*/
73759109abcSLawrence Mitchell PetscErrorCode PCPatchSetComputeOperatorInteriorFacets(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Mat, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
73859109abcSLawrence Mitchell {
73959109abcSLawrence Mitchell   PC_PATCH *patch = (PC_PATCH *) pc->data;
74059109abcSLawrence Mitchell 
74159109abcSLawrence Mitchell   PetscFunctionBegin;
74259109abcSLawrence Mitchell   patch->usercomputeopintfacet    = func;
74359109abcSLawrence Mitchell   patch->usercomputeopintfacetctx = ctx;
74459109abcSLawrence Mitchell   PetscFunctionReturn(0);
74559109abcSLawrence Mitchell }
74659109abcSLawrence Mitchell 
7474bbf5ea8SMatthew G. Knepley /* On entry, ht contains the topological entities whose dofs we are responsible for solving for;
7484bbf5ea8SMatthew G. Knepley    on exit, cht contains all the topological entities we need to compute their residuals.
7494bbf5ea8SMatthew G. Knepley    In full generality this should incorporate knowledge of the sparsity pattern of the matrix;
7504bbf5ea8SMatthew G. Knepley    here we assume a standard FE sparsity pattern.*/
7514bbf5ea8SMatthew G. Knepley /* TODO: Use DMPlexGetAdjacency() */
7521b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchCompleteCellPatch(PC pc, PetscHSetI ht, PetscHSetI cht)
7534bbf5ea8SMatthew G. Knepley {
754b6bb21d1SLawrence Mitchell   DM             dm, plex;
755bc7fa33aSFlorian Wechsung   PC_PATCH      *patch = (PC_PATCH *) pc->data;
7561b68eb51SMatthew G. Knepley   PetscHashIter  hi;
7574bbf5ea8SMatthew G. Knepley   PetscInt       point;
7584bbf5ea8SMatthew G. Knepley   PetscInt      *star = NULL, *closure = NULL;
7594c954380SMatthew G. Knepley   PetscInt       ignoredim, iStart = 0, iEnd = -1, starSize, closureSize, si, ci;
760bc7fa33aSFlorian Wechsung   PetscInt      *fStar = NULL, *fClosure = NULL;
761bc7fa33aSFlorian Wechsung   PetscInt       fBegin, fEnd, fsi, fci, fStarSize, fClosureSize;
7624bbf5ea8SMatthew G. Knepley 
7634bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
7645f80ce2aSJacob Faibussowitsch   CHKERRQ(PCGetDM(pc, &dm));
7655f80ce2aSJacob Faibussowitsch   CHKERRQ(DMConvert(dm, DMPLEX, &plex));
766b6bb21d1SLawrence Mitchell   dm = plex;
7675f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetHeightStratum(dm, 1, &fBegin, &fEnd));
7685f80ce2aSJacob Faibussowitsch   CHKERRQ(PCPatchGetIgnoreDim(pc, &ignoredim));
7695f80ce2aSJacob Faibussowitsch   if (ignoredim >= 0) CHKERRQ(DMPlexGetDepthStratum(dm, ignoredim, &iStart, &iEnd));
7705f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHSetIClear(cht));
7711b68eb51SMatthew G. Knepley   PetscHashIterBegin(ht, hi);
7721b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(ht, hi)) {
7734c954380SMatthew G. Knepley 
7741b68eb51SMatthew G. Knepley     PetscHashIterGetKey(ht, hi, point);
7751b68eb51SMatthew G. Knepley     PetscHashIterNext(ht, hi);
7764bbf5ea8SMatthew G. Knepley 
7774bbf5ea8SMatthew G. Knepley     /* Loop over all the cells that this point connects to */
7785f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star));
7795f824522SMatthew G. Knepley     for (si = 0; si < starSize*2; si += 2) {
7804c954380SMatthew G. Knepley       const PetscInt ownedpoint = star[si];
7815f824522SMatthew G. Knepley       /* TODO Check for point in cht before running through closure again */
7824bbf5ea8SMatthew G. Knepley       /* now loop over all entities in the closure of that cell */
7835f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetTransitiveClosure(dm, ownedpoint, PETSC_TRUE, &closureSize, &closure));
7845f824522SMatthew G. Knepley       for (ci = 0; ci < closureSize*2; ci += 2) {
7854c954380SMatthew G. Knepley         const PetscInt seenpoint = closure[ci];
7865f824522SMatthew G. Knepley         if (ignoredim >= 0 && seenpoint >= iStart && seenpoint < iEnd) continue;
7875f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscHSetIAdd(cht, seenpoint));
788bc7fa33aSFlorian Wechsung         /* Facet integrals couple dofs across facets, so in that case for each of
789bc7fa33aSFlorian Wechsung          * the facets we need to add all dofs on the other side of the facet to
790bc7fa33aSFlorian Wechsung          * the seen dofs. */
791bc7fa33aSFlorian Wechsung         if (patch->usercomputeopintfacet) {
792bc7fa33aSFlorian Wechsung           if (fBegin <= seenpoint && seenpoint < fEnd) {
7935f80ce2aSJacob Faibussowitsch             CHKERRQ(DMPlexGetTransitiveClosure(dm, seenpoint, PETSC_FALSE, &fStarSize, &fStar));
794bc7fa33aSFlorian Wechsung             for (fsi = 0; fsi < fStarSize*2; fsi += 2) {
7955f80ce2aSJacob Faibussowitsch               CHKERRQ(DMPlexGetTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, &fClosureSize, &fClosure));
796bc7fa33aSFlorian Wechsung               for (fci = 0; fci < fClosureSize*2; fci += 2) {
7975f80ce2aSJacob Faibussowitsch                 CHKERRQ(PetscHSetIAdd(cht, fClosure[fci]));
798bc7fa33aSFlorian Wechsung               }
7995f80ce2aSJacob Faibussowitsch               CHKERRQ(DMPlexRestoreTransitiveClosure(dm, fStar[fsi], PETSC_TRUE, NULL, &fClosure));
800bc7fa33aSFlorian Wechsung             }
8015f80ce2aSJacob Faibussowitsch             CHKERRQ(DMPlexRestoreTransitiveClosure(dm, seenpoint, PETSC_FALSE, NULL, &fStar));
802bc7fa33aSFlorian Wechsung           }
803bc7fa33aSFlorian Wechsung         }
8044bbf5ea8SMatthew G. Knepley       }
8055f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexRestoreTransitiveClosure(dm, ownedpoint, PETSC_TRUE, NULL, &closure));
8064bbf5ea8SMatthew G. Knepley     }
8075f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, NULL, &star));
8084bbf5ea8SMatthew G. Knepley   }
8095f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&dm));
8105f824522SMatthew G. Knepley   PetscFunctionReturn(0);
8115f824522SMatthew G. Knepley }
8125f824522SMatthew G. Knepley 
8135f824522SMatthew G. Knepley static PetscErrorCode PCPatchGetGlobalDofs(PC pc, PetscSection dofSection[], PetscInt f, PetscBool combined, PetscInt p, PetscInt *dof, PetscInt *off)
8145f824522SMatthew G. Knepley {
8155f824522SMatthew G. Knepley   PetscFunctionBegin;
8165f824522SMatthew G. Knepley   if (combined) {
8175f824522SMatthew G. Knepley     if (f < 0) {
8185f80ce2aSJacob Faibussowitsch       if (dof) CHKERRQ(PetscSectionGetDof(dofSection[0], p, dof));
8195f80ce2aSJacob Faibussowitsch       if (off) CHKERRQ(PetscSectionGetOffset(dofSection[0], p, off));
8205f824522SMatthew G. Knepley     } else {
8215f80ce2aSJacob Faibussowitsch       if (dof) CHKERRQ(PetscSectionGetFieldDof(dofSection[0], p, f, dof));
8225f80ce2aSJacob Faibussowitsch       if (off) CHKERRQ(PetscSectionGetFieldOffset(dofSection[0], p, f, off));
8235f824522SMatthew G. Knepley     }
8245f824522SMatthew G. Knepley   } else {
8255f824522SMatthew G. Knepley     if (f < 0) {
8265f824522SMatthew G. Knepley       PC_PATCH *patch = (PC_PATCH *) pc->data;
8275f824522SMatthew G. Knepley       PetscInt  fdof, g;
8285f824522SMatthew G. Knepley 
8295f824522SMatthew G. Knepley       if (dof) {
8305f824522SMatthew G. Knepley         *dof = 0;
8315f824522SMatthew G. Knepley         for (g = 0; g < patch->nsubspaces; ++g) {
8325f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionGetDof(dofSection[g], p, &fdof));
8335f824522SMatthew G. Knepley           *dof += fdof;
8345f824522SMatthew G. Knepley         }
8355f824522SMatthew G. Knepley       }
836624e31c3SLawrence Mitchell       if (off) {
837624e31c3SLawrence Mitchell         *off = 0;
838624e31c3SLawrence Mitchell         for (g = 0; g < patch->nsubspaces; ++g) {
8395f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionGetOffset(dofSection[g], p, &fdof));
840624e31c3SLawrence Mitchell           *off += fdof;
841624e31c3SLawrence Mitchell         }
842624e31c3SLawrence Mitchell       }
8435f824522SMatthew G. Knepley     } else {
8445f80ce2aSJacob Faibussowitsch       if (dof) CHKERRQ(PetscSectionGetDof(dofSection[f], p, dof));
8455f80ce2aSJacob Faibussowitsch       if (off) CHKERRQ(PetscSectionGetOffset(dofSection[f], p, off));
8465f824522SMatthew G. Knepley     }
8475f824522SMatthew G. Knepley   }
8484bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
8494bbf5ea8SMatthew G. Knepley }
8504bbf5ea8SMatthew G. Knepley 
8514bbf5ea8SMatthew G. Knepley /* Given a hash table with a set of topological entities (pts), compute the degrees of
8524bbf5ea8SMatthew G. Knepley    freedom in global concatenated numbering on those entities.
8534bbf5ea8SMatthew G. Knepley    For Vanka smoothing, this needs to do something special: ignore dofs of the
8544bbf5ea8SMatthew G. Knepley    constraint subspace on entities that aren't the base entity we're building the patch
8554bbf5ea8SMatthew G. Knepley    around. */
856e4c66b91SPatrick Farrell static PetscErrorCode PCPatchGetPointDofs(PC pc, PetscHSetI pts, PetscHSetI dofs, PetscInt base, PetscHSetI* subspaces_to_exclude)
8574bbf5ea8SMatthew G. Knepley {
8585f824522SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
8591b68eb51SMatthew G. Knepley   PetscHashIter  hi;
8604bbf5ea8SMatthew G. Knepley   PetscInt       ldof, loff;
8614bbf5ea8SMatthew G. Knepley   PetscInt       k, p;
8624bbf5ea8SMatthew G. Knepley 
8634bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
8645f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHSetIClear(dofs));
8654bbf5ea8SMatthew G. Knepley   for (k = 0; k < patch->nsubspaces; ++k) {
8664bbf5ea8SMatthew G. Knepley     PetscInt subspaceOffset = patch->subspaceOffsets[k];
8674bbf5ea8SMatthew G. Knepley     PetscInt bs             = patch->bs[k];
8684bbf5ea8SMatthew G. Knepley     PetscInt j, l;
8694bbf5ea8SMatthew G. Knepley 
870e4c66b91SPatrick Farrell     if (subspaces_to_exclude != NULL) {
871e4c66b91SPatrick Farrell       PetscBool should_exclude_k = PETSC_FALSE;
8725f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscHSetIHas(*subspaces_to_exclude, k, &should_exclude_k));
873e4c66b91SPatrick Farrell       if (should_exclude_k) {
8744bbf5ea8SMatthew G. Knepley         /* only get this subspace dofs at the base entity, not any others */
8755f80ce2aSJacob Faibussowitsch         CHKERRQ(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, base, &ldof, &loff));
8764bbf5ea8SMatthew G. Knepley         if (0 == ldof) continue;
8774bbf5ea8SMatthew G. Knepley         for (j = loff; j < ldof + loff; ++j) {
8784bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
8794bbf5ea8SMatthew G. Knepley             PetscInt dof = bs*j + l + subspaceOffset;
8805f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscHSetIAdd(dofs, dof));
8814bbf5ea8SMatthew G. Knepley           }
8824bbf5ea8SMatthew G. Knepley         }
8834bbf5ea8SMatthew G. Knepley         continue; /* skip the other dofs of this subspace */
8844bbf5ea8SMatthew G. Knepley       }
885e4c66b91SPatrick Farrell     }
8864bbf5ea8SMatthew G. Knepley 
8871b68eb51SMatthew G. Knepley     PetscHashIterBegin(pts, hi);
8881b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(pts, hi)) {
8891b68eb51SMatthew G. Knepley       PetscHashIterGetKey(pts, hi, p);
8901b68eb51SMatthew G. Knepley       PetscHashIterNext(pts, hi);
8915f80ce2aSJacob Faibussowitsch       CHKERRQ(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, p, &ldof, &loff));
8924bbf5ea8SMatthew G. Knepley       if (0 == ldof) continue;
8934bbf5ea8SMatthew G. Knepley       for (j = loff; j < ldof + loff; ++j) {
8944bbf5ea8SMatthew G. Knepley         for (l = 0; l < bs; ++l) {
8954bbf5ea8SMatthew G. Knepley           PetscInt dof = bs*j + l + subspaceOffset;
8965f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscHSetIAdd(dofs, dof));
8974bbf5ea8SMatthew G. Knepley         }
8984bbf5ea8SMatthew G. Knepley       }
8994bbf5ea8SMatthew G. Knepley     }
9004bbf5ea8SMatthew G. Knepley   }
9014bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
9024bbf5ea8SMatthew G. Knepley }
9034bbf5ea8SMatthew G. Knepley 
9044bbf5ea8SMatthew G. Knepley /* Given two hash tables A and B, compute the keys in B that are not in A, and put them in C */
9051b68eb51SMatthew G. Knepley static PetscErrorCode PCPatchComputeSetDifference_Private(PetscHSetI A, PetscHSetI B, PetscHSetI C)
9064bbf5ea8SMatthew G. Knepley {
9071b68eb51SMatthew G. Knepley   PetscHashIter  hi;
9081b68eb51SMatthew G. Knepley   PetscInt       key;
9094bbf5ea8SMatthew G. Knepley   PetscBool      flg;
9104bbf5ea8SMatthew G. Knepley 
9114bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
9125f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHSetIClear(C));
9131b68eb51SMatthew G. Knepley   PetscHashIterBegin(B, hi);
9141b68eb51SMatthew G. Knepley   while (!PetscHashIterAtEnd(B, hi)) {
9151b68eb51SMatthew G. Knepley     PetscHashIterGetKey(B, hi, key);
9161b68eb51SMatthew G. Knepley     PetscHashIterNext(B, hi);
9175f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetIHas(A, key, &flg));
9185f80ce2aSJacob Faibussowitsch     if (!flg) CHKERRQ(PetscHSetIAdd(C, key));
9194bbf5ea8SMatthew G. Knepley   }
9204bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
9214bbf5ea8SMatthew G. Knepley }
9224bbf5ea8SMatthew G. Knepley 
9234bbf5ea8SMatthew G. Knepley /*
9244bbf5ea8SMatthew G. Knepley  * PCPatchCreateCellPatches - create patches.
9254bbf5ea8SMatthew G. Knepley  *
9264bbf5ea8SMatthew G. Knepley  * Input Parameters:
9274bbf5ea8SMatthew G. Knepley  * + dm - The DMPlex object defining the mesh
9284bbf5ea8SMatthew G. Knepley  *
9294bbf5ea8SMatthew G. Knepley  * Output Parameters:
9304bbf5ea8SMatthew G. Knepley  * + cellCounts  - Section with counts of cells around each vertex
9315f824522SMatthew G. Knepley  * . cells       - IS of the cell point indices of cells in each patch
9325f824522SMatthew G. Knepley  * . pointCounts - Section with counts of cells around each vertex
9335f824522SMatthew G. Knepley  * - point       - IS of the cell point indices of cells in each patch
9344bbf5ea8SMatthew G. Knepley  */
9354bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatches(PC pc)
9364bbf5ea8SMatthew G. Knepley {
9374bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
9385f824522SMatthew G. Knepley   DMLabel         ghost = NULL;
9394bbf5ea8SMatthew G. Knepley   DM              dm, plex;
9401b68eb51SMatthew G. Knepley   PetscHSetI      ht, cht;
9410e126c0bSLawrence Mitchell   PetscSection    cellCounts,  pointCounts, intFacetCounts, extFacetCounts;
942eb62eeaaSLawrence Mitchell   PetscInt       *cellsArray, *pointsArray, *intFacetsArray, *extFacetsArray, *intFacetsToPatchCell;
9430e126c0bSLawrence Mitchell   PetscInt        numCells, numPoints, numIntFacets, numExtFacets;
9445f824522SMatthew G. Knepley   const PetscInt *leaves;
94533cbca70SPatrick Farrell   PetscInt        nleaves, pStart, pEnd, cStart, cEnd, vStart, vEnd, fStart, fEnd, v;
9465f824522SMatthew G. Knepley   PetscBool       isFiredrake;
9474bbf5ea8SMatthew G. Knepley 
9484bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
9494bbf5ea8SMatthew G. Knepley   /* Used to keep track of the cells in the patch. */
9505f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHSetICreate(&ht));
9515f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHSetICreate(&cht));
9524bbf5ea8SMatthew G. Knepley 
9535f80ce2aSJacob Faibussowitsch   CHKERRQ(PCGetDM(pc, &dm));
954*28b400f6SJacob Faibussowitsch   PetscCheck(dm,PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONGSTATE, "DM not yet set on patch PC");
9555f80ce2aSJacob Faibussowitsch   CHKERRQ(DMConvert(dm, DMPLEX, &plex));
956b6bb21d1SLawrence Mitchell   dm = plex;
9575f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetChart(dm, &pStart, &pEnd));
9585f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
9594bbf5ea8SMatthew G. Knepley 
9604bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
9615f80ce2aSJacob Faibussowitsch     CHKERRQ(patch->userpatchconstructionop(pc, &patch->npatch, &patch->userIS, &patch->iterationSet, patch->userpatchconstructctx));
9625f824522SMatthew G. Knepley     vStart = 0; vEnd = patch->npatch;
963e5b9877fSPatrick Farrell   } else if (patch->ctype == PC_PATCH_PARDECOMP) {
9640a390943SPatrick Farrell     vStart = 0; vEnd = 1;
9655f824522SMatthew G. Knepley   } else if (patch->codim < 0) {
9665f80ce2aSJacob Faibussowitsch     if (patch->dim < 0) CHKERRQ(DMPlexGetDepthStratum(dm,  0,            &vStart, &vEnd));
9675f80ce2aSJacob Faibussowitsch     else                CHKERRQ(DMPlexGetDepthStratum(dm,  patch->dim,   &vStart, &vEnd));
9685f80ce2aSJacob Faibussowitsch   } else                CHKERRQ(DMPlexGetHeightStratum(dm, patch->codim, &vStart, &vEnd));
9695f824522SMatthew G. Knepley   patch->npatch = vEnd - vStart;
9704bbf5ea8SMatthew G. Knepley 
9714bbf5ea8SMatthew G. Knepley   /* These labels mark the owned points.  We only create patches around points that this process owns. */
9725f80ce2aSJacob Faibussowitsch   CHKERRQ(DMHasLabel(dm, "pyop2_ghost", &isFiredrake));
9735f824522SMatthew G. Knepley   if (isFiredrake) {
9745f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLabel(dm, "pyop2_ghost", &ghost));
9755f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelCreateIndex(ghost, pStart, pEnd));
9765f824522SMatthew G. Knepley   } else {
9775f824522SMatthew G. Knepley     PetscSF sf;
9785f824522SMatthew G. Knepley 
9795f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetPointSF(dm, &sf));
9805f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL));
9815f824522SMatthew G. Knepley     nleaves = PetscMax(nleaves, 0);
9825f824522SMatthew G. Knepley   }
9834bbf5ea8SMatthew G. Knepley 
9845f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionCreate(PETSC_COMM_SELF, &patch->cellCounts));
9855f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetName((PetscObject) patch->cellCounts, "Patch Cell Layout"));
9864bbf5ea8SMatthew G. Knepley   cellCounts = patch->cellCounts;
9875f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetChart(cellCounts, vStart, vEnd));
9885f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionCreate(PETSC_COMM_SELF, &patch->pointCounts));
9895f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetName((PetscObject) patch->pointCounts, "Patch Point Layout"));
9905f824522SMatthew G. Knepley   pointCounts = patch->pointCounts;
9915f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetChart(pointCounts, vStart, vEnd));
9925f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionCreate(PETSC_COMM_SELF, &patch->extFacetCounts));
9935f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetName((PetscObject) patch->extFacetCounts, "Patch Exterior Facet Layout"));
9940e126c0bSLawrence Mitchell   extFacetCounts = patch->extFacetCounts;
9955f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetChart(extFacetCounts, vStart, vEnd));
9965f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionCreate(PETSC_COMM_SELF, &patch->intFacetCounts));
9975f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetName((PetscObject) patch->intFacetCounts, "Patch Interior Facet Layout"));
9980e126c0bSLawrence Mitchell   intFacetCounts = patch->intFacetCounts;
9995f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetChart(intFacetCounts, vStart, vEnd));
10005f824522SMatthew G. Knepley   /* Count cells and points in the patch surrounding each entity */
10015f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
10024bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
10031b68eb51SMatthew G. Knepley     PetscHashIter hi;
10045f824522SMatthew G. Knepley     PetscInt       chtSize, loc = -1;
10055f824522SMatthew G. Knepley     PetscBool      flg;
10064bbf5ea8SMatthew G. Knepley 
1007b525f888SPatrick Farrell     if (!patch->user_patches && patch->ctype != PC_PATCH_PARDECOMP) {
10085f80ce2aSJacob Faibussowitsch       if (ghost) CHKERRQ(DMLabelHasPoint(ghost, v, &flg));
10095f80ce2aSJacob Faibussowitsch       else       {CHKERRQ(PetscFindInt(v, nleaves, leaves, &loc)); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;}
10104bbf5ea8SMatthew G. Knepley       /* Not an owned entity, don't make a cell patch. */
10114bbf5ea8SMatthew G. Knepley       if (flg) continue;
10124bbf5ea8SMatthew G. Knepley     }
10134bbf5ea8SMatthew G. Knepley 
10145f80ce2aSJacob Faibussowitsch     CHKERRQ(patch->patchconstructop((void *) patch, dm, v, ht));
10155f80ce2aSJacob Faibussowitsch     CHKERRQ(PCPatchCompleteCellPatch(pc, ht, cht));
10165f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetIGetSize(cht, &chtSize));
10174bbf5ea8SMatthew G. Knepley     /* empty patch, continue */
10184bbf5ea8SMatthew G. Knepley     if (chtSize == 0) continue;
10194bbf5ea8SMatthew G. Knepley 
10204bbf5ea8SMatthew G. Knepley     /* safe because size(cht) > 0 from above */
10211b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
10221b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
10235f824522SMatthew G. Knepley       PetscInt point, pdof;
10244bbf5ea8SMatthew G. Knepley 
10251b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
10260e126c0bSLawrence Mitchell       if (fStart <= point && point < fEnd) {
10270e126c0bSLawrence Mitchell         const PetscInt *support;
10280e126c0bSLawrence Mitchell         PetscInt supportSize, p;
10290e126c0bSLawrence Mitchell         PetscBool interior = PETSC_TRUE;
10305f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetSupport(dm, point, &support));
10315f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetSupportSize(dm, point, &supportSize));
10320e126c0bSLawrence Mitchell         if (supportSize == 1) {
10330e126c0bSLawrence Mitchell           interior = PETSC_FALSE;
10340e126c0bSLawrence Mitchell         } else {
10350e126c0bSLawrence Mitchell           for (p = 0; p < supportSize; p++) {
10360e126c0bSLawrence Mitchell             PetscBool found;
10370e126c0bSLawrence Mitchell             /* FIXME: can I do this while iterating over cht? */
10385f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscHSetIHas(cht, support[p], &found));
10390e126c0bSLawrence Mitchell             if (!found) {
10400e126c0bSLawrence Mitchell               interior = PETSC_FALSE;
10410e126c0bSLawrence Mitchell               break;
10420e126c0bSLawrence Mitchell             }
10430e126c0bSLawrence Mitchell           }
10440e126c0bSLawrence Mitchell         }
10450e126c0bSLawrence Mitchell         if (interior) {
10465f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionAddDof(intFacetCounts, v, 1));
10470e126c0bSLawrence Mitchell         } else {
10485f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionAddDof(extFacetCounts, v, 1));
10490e126c0bSLawrence Mitchell         }
10500e126c0bSLawrence Mitchell       }
10515f80ce2aSJacob Faibussowitsch       CHKERRQ(PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL));
10525f80ce2aSJacob Faibussowitsch       if (pdof)                            CHKERRQ(PetscSectionAddDof(pointCounts, v, 1));
10535f80ce2aSJacob Faibussowitsch       if (point >= cStart && point < cEnd) CHKERRQ(PetscSectionAddDof(cellCounts, v, 1));
10541b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
10554bbf5ea8SMatthew G. Knepley     }
10564bbf5ea8SMatthew G. Knepley   }
10575f80ce2aSJacob Faibussowitsch   if (isFiredrake) CHKERRQ(DMLabelDestroyIndex(ghost));
10584bbf5ea8SMatthew G. Knepley 
10595f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetUp(cellCounts));
10605f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetStorageSize(cellCounts, &numCells));
10615f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(numCells, &cellsArray));
10625f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetUp(pointCounts));
10635f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetStorageSize(pointCounts, &numPoints));
10645f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(numPoints, &pointsArray));
10654bbf5ea8SMatthew G. Knepley 
10665f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetUp(intFacetCounts));
10675f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetUp(extFacetCounts));
10685f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetStorageSize(intFacetCounts, &numIntFacets));
10695f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetStorageSize(extFacetCounts, &numExtFacets));
10705f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(numIntFacets, &intFacetsArray));
10715f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(numIntFacets*2, &intFacetsToPatchCell));
10725f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(numExtFacets, &extFacetsArray));
10730e126c0bSLawrence Mitchell 
10744bbf5ea8SMatthew G. Knepley   /* Now that we know how much space we need, run through again and actually remember the cells. */
10754bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; v++) {
10761b68eb51SMatthew G. Knepley     PetscHashIter hi;
10770e126c0bSLawrence Mitchell     PetscInt       dof, off, cdof, coff, efdof, efoff, ifdof, ifoff, pdof, n = 0, cn = 0, ifn = 0, efn = 0;
10784bbf5ea8SMatthew G. Knepley 
10795f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(pointCounts, v, &dof));
10805f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(pointCounts, v, &off));
10815f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(cellCounts, v, &cdof));
10825f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(cellCounts, v, &coff));
10835f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(intFacetCounts, v, &ifdof));
10845f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(intFacetCounts, v, &ifoff));
10855f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(extFacetCounts, v, &efdof));
10865f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(extFacetCounts, v, &efoff));
10875f824522SMatthew G. Knepley     if (dof <= 0) continue;
10885f80ce2aSJacob Faibussowitsch     CHKERRQ(patch->patchconstructop((void *) patch, dm, v, ht));
10895f80ce2aSJacob Faibussowitsch     CHKERRQ(PCPatchCompleteCellPatch(pc, ht, cht));
10901b68eb51SMatthew G. Knepley     PetscHashIterBegin(cht, hi);
10911b68eb51SMatthew G. Knepley     while (!PetscHashIterAtEnd(cht, hi)) {
10924bbf5ea8SMatthew G. Knepley       PetscInt point;
10934bbf5ea8SMatthew G. Knepley 
10941b68eb51SMatthew G. Knepley       PetscHashIterGetKey(cht, hi, point);
10950e126c0bSLawrence Mitchell       if (fStart <= point && point < fEnd) {
10960e126c0bSLawrence Mitchell         const PetscInt *support;
10970e126c0bSLawrence Mitchell         PetscInt       supportSize, p;
10980e126c0bSLawrence Mitchell         PetscBool      interior = PETSC_TRUE;
10995f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetSupport(dm, point, &support));
11005f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetSupportSize(dm, point, &supportSize));
11010e126c0bSLawrence Mitchell         if (supportSize == 1) {
11020e126c0bSLawrence Mitchell           interior = PETSC_FALSE;
11030e126c0bSLawrence Mitchell         } else {
11040e126c0bSLawrence Mitchell           for (p = 0; p < supportSize; p++) {
11050e126c0bSLawrence Mitchell             PetscBool found;
11060e126c0bSLawrence Mitchell             /* FIXME: can I do this while iterating over cht? */
11075f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscHSetIHas(cht, support[p], &found));
11080e126c0bSLawrence Mitchell             if (!found) {
11090e126c0bSLawrence Mitchell               interior = PETSC_FALSE;
11100e126c0bSLawrence Mitchell               break;
11110e126c0bSLawrence Mitchell             }
11120e126c0bSLawrence Mitchell           }
11130e126c0bSLawrence Mitchell         }
11140e126c0bSLawrence Mitchell         if (interior) {
111544b625f7SLawrence Mitchell           intFacetsToPatchCell[2*(ifoff + ifn)] = support[0];
111644b625f7SLawrence Mitchell           intFacetsToPatchCell[2*(ifoff + ifn) + 1] = support[1];
11170e126c0bSLawrence Mitchell           intFacetsArray[ifoff + ifn++] = point;
11180e126c0bSLawrence Mitchell         } else {
11190e126c0bSLawrence Mitchell           extFacetsArray[efoff + efn++] = point;
11200e126c0bSLawrence Mitchell         }
11210e126c0bSLawrence Mitchell       }
11225f80ce2aSJacob Faibussowitsch       CHKERRQ(PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL));
11235f824522SMatthew G. Knepley       if (pdof)                            {pointsArray[off + n++] = point;}
11245f824522SMatthew G. Knepley       if (point >= cStart && point < cEnd) {cellsArray[coff + cn++] = point;}
11251b68eb51SMatthew G. Knepley       PetscHashIterNext(cht, hi);
11264bbf5ea8SMatthew G. Knepley     }
11272c71b3e2SJacob Faibussowitsch     PetscCheckFalse(ifn != ifdof,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of interior facets in patch %D is %D, but should be %D", v, ifn, ifdof);
11282c71b3e2SJacob Faibussowitsch     PetscCheckFalse(efn != efdof,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of exterior facets in patch %D is %D, but should be %D", v, efn, efdof);
11292c71b3e2SJacob Faibussowitsch     PetscCheckFalse(cn != cdof,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of cells in patch %D is %D, but should be %D", v, cn, cdof);
11302c71b3e2SJacob Faibussowitsch     PetscCheckFalse(n  != dof,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of points in patch %D is %D, but should be %D", v, n, dof);
1131eb62eeaaSLawrence Mitchell 
1132eb62eeaaSLawrence Mitchell     for (ifn = 0; ifn < ifdof; ifn++) {
113344b625f7SLawrence Mitchell       PetscInt  cell0 = intFacetsToPatchCell[2*(ifoff + ifn)];
113444b625f7SLawrence Mitchell       PetscInt  cell1 = intFacetsToPatchCell[2*(ifoff + ifn) + 1];
1135eb62eeaaSLawrence Mitchell       PetscBool found0 = PETSC_FALSE, found1 = PETSC_FALSE;
1136eb62eeaaSLawrence Mitchell       for (n = 0; n < cdof; n++) {
11377c54fef0SLawrence Mitchell         if (!found0 && cell0 == cellsArray[coff + n]) {
1138c3faab33SLawrence Mitchell           intFacetsToPatchCell[2*(ifoff + ifn)] = n;
1139eb62eeaaSLawrence Mitchell           found0 = PETSC_TRUE;
1140eb62eeaaSLawrence Mitchell         }
11417c54fef0SLawrence Mitchell         if (!found1 && cell1 == cellsArray[coff + n]) {
1142c3faab33SLawrence Mitchell           intFacetsToPatchCell[2*(ifoff + ifn) + 1] = n;
114380fc4459SLawrence Mitchell           found1 = PETSC_TRUE;
1144eb62eeaaSLawrence Mitchell         }
1145eb62eeaaSLawrence Mitchell         if (found0 && found1) break;
1146eb62eeaaSLawrence Mitchell       }
11472c71b3e2SJacob Faibussowitsch       PetscCheckFalse(!(found0 && found1),PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Didn't manage to find local point numbers for facet support");
1148eb62eeaaSLawrence Mitchell     }
11494bbf5ea8SMatthew G. Knepley   }
11505f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHSetIDestroy(&ht));
11515f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHSetIDestroy(&cht));
11525f824522SMatthew G. Knepley 
11535f80ce2aSJacob Faibussowitsch   CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numCells,  cellsArray,  PETSC_OWN_POINTER, &patch->cells));
11545f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetName((PetscObject) patch->cells,  "Patch Cells"));
11555f824522SMatthew G. Knepley   if (patch->viewCells) {
11565f80ce2aSJacob Faibussowitsch     CHKERRQ(ObjectView((PetscObject) patch->cellCounts, patch->viewerCells, patch->formatCells));
11575f80ce2aSJacob Faibussowitsch     CHKERRQ(ObjectView((PetscObject) patch->cells,      patch->viewerCells, patch->formatCells));
11585f824522SMatthew G. Knepley   }
11595f80ce2aSJacob Faibussowitsch   CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numIntFacets,  intFacetsArray,  PETSC_OWN_POINTER, &patch->intFacets));
11605f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetName((PetscObject) patch->intFacets,  "Patch Interior Facets"));
11615f80ce2aSJacob Faibussowitsch   CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, 2*numIntFacets, intFacetsToPatchCell, PETSC_OWN_POINTER, &patch->intFacetsToPatchCell));
11625f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetName((PetscObject) patch->intFacetsToPatchCell,  "Patch Interior Facets local support"));
11630e126c0bSLawrence Mitchell   if (patch->viewIntFacets) {
11645f80ce2aSJacob Faibussowitsch     CHKERRQ(ObjectView((PetscObject) patch->intFacetCounts,       patch->viewerIntFacets, patch->formatIntFacets));
11655f80ce2aSJacob Faibussowitsch     CHKERRQ(ObjectView((PetscObject) patch->intFacets,            patch->viewerIntFacets, patch->formatIntFacets));
11665f80ce2aSJacob Faibussowitsch     CHKERRQ(ObjectView((PetscObject) patch->intFacetsToPatchCell, patch->viewerIntFacets, patch->formatIntFacets));
11670e126c0bSLawrence Mitchell   }
11685f80ce2aSJacob Faibussowitsch   CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numExtFacets,  extFacetsArray,  PETSC_OWN_POINTER, &patch->extFacets));
11695f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetName((PetscObject) patch->extFacets,  "Patch Exterior Facets"));
11700e126c0bSLawrence Mitchell   if (patch->viewExtFacets) {
11715f80ce2aSJacob Faibussowitsch     CHKERRQ(ObjectView((PetscObject) patch->extFacetCounts, patch->viewerExtFacets, patch->formatExtFacets));
11725f80ce2aSJacob Faibussowitsch     CHKERRQ(ObjectView((PetscObject) patch->extFacets,      patch->viewerExtFacets, patch->formatExtFacets));
11730e126c0bSLawrence Mitchell   }
11745f80ce2aSJacob Faibussowitsch   CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numPoints, pointsArray, PETSC_OWN_POINTER, &patch->points));
11755f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetName((PetscObject) patch->points, "Patch Points"));
11765f824522SMatthew G. Knepley   if (patch->viewPoints) {
11775f80ce2aSJacob Faibussowitsch     CHKERRQ(ObjectView((PetscObject) patch->pointCounts, patch->viewerPoints, patch->formatPoints));
11785f80ce2aSJacob Faibussowitsch     CHKERRQ(ObjectView((PetscObject) patch->points,      patch->viewerPoints, patch->formatPoints));
11795f824522SMatthew G. Knepley   }
11805f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&dm));
11814bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
11824bbf5ea8SMatthew G. Knepley }
11834bbf5ea8SMatthew G. Knepley 
11844bbf5ea8SMatthew G. Knepley /*
11854bbf5ea8SMatthew G. Knepley  * PCPatchCreateCellPatchDiscretisationInfo - Build the dof maps for cell patches
11864bbf5ea8SMatthew G. Knepley  *
11874bbf5ea8SMatthew G. Knepley  * Input Parameters:
11884bbf5ea8SMatthew G. Knepley  * + dm - The DMPlex object defining the mesh
11894bbf5ea8SMatthew G. Knepley  * . cellCounts - Section with counts of cells around each vertex
11904bbf5ea8SMatthew G. Knepley  * . cells - IS of the cell point indices of cells in each patch
11914bbf5ea8SMatthew G. Knepley  * . cellNumbering - Section mapping plex cell points to Firedrake cell indices.
11924bbf5ea8SMatthew G. Knepley  * . nodesPerCell - number of nodes per cell.
11934bbf5ea8SMatthew G. Knepley  * - cellNodeMap - map from cells to node indices (nodesPerCell * numCells)
11944bbf5ea8SMatthew G. Knepley  *
11954bbf5ea8SMatthew G. Knepley  * Output Parameters:
11965f824522SMatthew G. Knepley  * + dofs - IS of local dof numbers of each cell in the patch, where local is a patch local numbering
11974bbf5ea8SMatthew G. Knepley  * . gtolCounts - Section with counts of dofs per cell patch
11984bbf5ea8SMatthew G. Knepley  * - gtol - IS mapping from global dofs to local dofs for each patch.
11994bbf5ea8SMatthew G. Knepley  */
12004bbf5ea8SMatthew G. Knepley static PetscErrorCode PCPatchCreateCellPatchDiscretisationInfo(PC pc)
12014bbf5ea8SMatthew G. Knepley {
12024bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch           = (PC_PATCH *) pc->data;
12034bbf5ea8SMatthew G. Knepley   PetscSection    cellCounts      = patch->cellCounts;
12045f824522SMatthew G. Knepley   PetscSection    pointCounts     = patch->pointCounts;
12050904074fSPatrick Farrell   PetscSection    gtolCounts, gtolCountsWithArtificial = NULL, gtolCountsWithAll = NULL;
12064bbf5ea8SMatthew G. Knepley   IS              cells           = patch->cells;
12075f824522SMatthew G. Knepley   IS              points          = patch->points;
12084bbf5ea8SMatthew G. Knepley   PetscSection    cellNumbering   = patch->cellNumbering;
12095f824522SMatthew G. Knepley   PetscInt        Nf              = patch->nsubspaces;
12105f824522SMatthew G. Knepley   PetscInt        numCells, numPoints;
12114bbf5ea8SMatthew G. Knepley   PetscInt        numDofs;
12120904074fSPatrick Farrell   PetscInt        numGlobalDofs, numGlobalDofsWithArtificial, numGlobalDofsWithAll;
12134bbf5ea8SMatthew G. Knepley   PetscInt        totalDofsPerCell = patch->totalDofsPerCell;
12144bbf5ea8SMatthew G. Knepley   PetscInt        vStart, vEnd, v;
12155f824522SMatthew G. Knepley   const PetscInt *cellsArray, *pointsArray;
12164bbf5ea8SMatthew G. Knepley   PetscInt       *newCellsArray   = NULL;
12174bbf5ea8SMatthew G. Knepley   PetscInt       *dofsArray       = NULL;
1218c2e6f3c0SFlorian Wechsung   PetscInt       *dofsArrayWithArtificial = NULL;
12190904074fSPatrick Farrell   PetscInt       *dofsArrayWithAll = NULL;
12205f824522SMatthew G. Knepley   PetscInt       *offsArray       = NULL;
1221c2e6f3c0SFlorian Wechsung   PetscInt       *offsArrayWithArtificial = NULL;
12220904074fSPatrick Farrell   PetscInt       *offsArrayWithAll = NULL;
12234bbf5ea8SMatthew G. Knepley   PetscInt       *asmArray        = NULL;
1224c2e6f3c0SFlorian Wechsung   PetscInt       *asmArrayWithArtificial = NULL;
12250904074fSPatrick Farrell   PetscInt       *asmArrayWithAll = NULL;
12264bbf5ea8SMatthew G. Knepley   PetscInt       *globalDofsArray = NULL;
1227c2e6f3c0SFlorian Wechsung   PetscInt       *globalDofsArrayWithArtificial = NULL;
12280904074fSPatrick Farrell   PetscInt       *globalDofsArrayWithAll = NULL;
12294bbf5ea8SMatthew G. Knepley   PetscInt        globalIndex     = 0;
12304bbf5ea8SMatthew G. Knepley   PetscInt        key             = 0;
12314bbf5ea8SMatthew G. Knepley   PetscInt        asmKey          = 0;
1232b6bb21d1SLawrence Mitchell   DM              dm              = NULL, plex;
1233557beb66SLawrence Mitchell   const PetscInt *bcNodes         = NULL;
12341b68eb51SMatthew G. Knepley   PetscHMapI      ht;
1235c2e6f3c0SFlorian Wechsung   PetscHMapI      htWithArtificial;
12360904074fSPatrick Farrell   PetscHMapI      htWithAll;
12371b68eb51SMatthew G. Knepley   PetscHSetI      globalBcs;
1238557beb66SLawrence Mitchell   PetscInt        numBcs;
12391b68eb51SMatthew G. Knepley   PetscHSetI      ownedpts, seenpts, owneddofs, seendofs, artificialbcs;
1240cda239d9SMatthew G. Knepley   PetscInt        pStart, pEnd, p, i;
124110534d48SPatrick Farrell   char            option[PETSC_MAX_PATH_LEN];
124239fd2e8aSPatrick Farrell   PetscBool       isNonlinear;
12434bbf5ea8SMatthew G. Knepley 
12444bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1245557beb66SLawrence Mitchell 
12465f80ce2aSJacob Faibussowitsch   CHKERRQ(PCGetDM(pc, &dm));
12475f80ce2aSJacob Faibussowitsch   CHKERRQ(DMConvert(dm, DMPLEX, &plex));
1248b6bb21d1SLawrence Mitchell   dm = plex;
12494bbf5ea8SMatthew G. Knepley   /* dofcounts section is cellcounts section * dofPerCell */
12505f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetStorageSize(cellCounts, &numCells));
12515f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetStorageSize(patch->pointCounts, &numPoints));
12524bbf5ea8SMatthew G. Knepley   numDofs = numCells * totalDofsPerCell;
12535f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(numDofs, &dofsArray));
12545f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(numPoints*Nf, &offsArray));
12555f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(numDofs, &asmArray));
12565f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(numCells, &newCellsArray));
12575f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetChart(cellCounts, &vStart, &vEnd));
12585f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCounts));
12594bbf5ea8SMatthew G. Knepley   gtolCounts = patch->gtolCounts;
12605f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetChart(gtolCounts, vStart, vEnd));
12615f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetName((PetscObject) patch->gtolCounts, "Patch Global Index Section"));
12624bbf5ea8SMatthew G. Knepley 
1263b6bb21d1SLawrence Mitchell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
12645f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(numPoints*Nf, &offsArrayWithArtificial));
12655f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(numDofs, &asmArrayWithArtificial));
12665f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(numDofs, &dofsArrayWithArtificial));
12675f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithArtificial));
1268c2e6f3c0SFlorian Wechsung     gtolCountsWithArtificial = patch->gtolCountsWithArtificial;
12695f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetChart(gtolCountsWithArtificial, vStart, vEnd));
12705f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetName((PetscObject) patch->gtolCountsWithArtificial, "Patch Global Index Section Including Artificial BCs"));
1271c2e6f3c0SFlorian Wechsung   }
1272c2e6f3c0SFlorian Wechsung 
12730904074fSPatrick Farrell   isNonlinear = patch->isNonlinear;
1274b6bb21d1SLawrence Mitchell   if (isNonlinear) {
12755f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(numPoints*Nf, &offsArrayWithAll));
12765f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(numDofs, &asmArrayWithAll));
12775f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(numDofs, &dofsArrayWithAll));
12785f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithAll));
12790904074fSPatrick Farrell     gtolCountsWithAll = patch->gtolCountsWithAll;
12805f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetChart(gtolCountsWithAll, vStart, vEnd));
12815f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetName((PetscObject) patch->gtolCountsWithAll, "Patch Global Index Section Including All BCs"));
12820904074fSPatrick Farrell   }
12830904074fSPatrick Farrell 
1284557beb66SLawrence Mitchell   /* Outside the patch loop, get the dofs that are globally-enforced Dirichlet
1285557beb66SLawrence Mitchell    conditions */
12865f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHSetICreate(&globalBcs));
12875f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetIndices(patch->ghostBcNodes, &bcNodes));
12885f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetSize(patch->ghostBcNodes, &numBcs));
1289cda239d9SMatthew G. Knepley   for (i = 0; i < numBcs; ++i) {
12905f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetIAdd(globalBcs, bcNodes[i])); /* these are already in concatenated numbering */
1291557beb66SLawrence Mitchell   }
12925f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(patch->ghostBcNodes, &bcNodes));
12935f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&patch->ghostBcNodes)); /* memory optimisation */
1294557beb66SLawrence Mitchell 
1295557beb66SLawrence Mitchell   /* Hash tables for artificial BC construction */
12965f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHSetICreate(&ownedpts));
12975f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHSetICreate(&seenpts));
12985f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHSetICreate(&owneddofs));
12995f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHSetICreate(&seendofs));
13005f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHSetICreate(&artificialbcs));
1301557beb66SLawrence Mitchell 
13025f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetIndices(cells, &cellsArray));
13035f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetIndices(points, &pointsArray));
13045f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHMapICreate(&ht));
13055f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHMapICreate(&htWithArtificial));
13065f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHMapICreate(&htWithAll));
13074bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
13084bbf5ea8SMatthew G. Knepley     PetscInt localIndex = 0;
1309c2e6f3c0SFlorian Wechsung     PetscInt localIndexWithArtificial = 0;
13100904074fSPatrick Farrell     PetscInt localIndexWithAll = 0;
13114bbf5ea8SMatthew G. Knepley     PetscInt dof, off, i, j, k, l;
13124bbf5ea8SMatthew G. Knepley 
13135f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHMapIClear(ht));
13145f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHMapIClear(htWithArtificial));
13155f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHMapIClear(htWithAll));
13165f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(cellCounts, v, &dof));
13175f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(cellCounts, v, &off));
13184bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
13194bbf5ea8SMatthew G. Knepley 
1320557beb66SLawrence Mitchell     /* Calculate the global numbers of the artificial BC dofs here first */
13215f80ce2aSJacob Faibussowitsch     CHKERRQ(patch->patchconstructop((void*)patch, dm, v, ownedpts));
13225f80ce2aSJacob Faibussowitsch     CHKERRQ(PCPatchCompleteCellPatch(pc, ownedpts, seenpts));
13235f80ce2aSJacob Faibussowitsch     CHKERRQ(PCPatchGetPointDofs(pc, ownedpts, owneddofs, v, &patch->subspaces_to_exclude));
13245f80ce2aSJacob Faibussowitsch     CHKERRQ(PCPatchGetPointDofs(pc, seenpts, seendofs, v, NULL));
13255f80ce2aSJacob Faibussowitsch     CHKERRQ(PCPatchComputeSetDifference_Private(owneddofs, seendofs, artificialbcs));
13268135ed82SLawrence Mitchell     if (patch->viewPatches) {
13271b68eb51SMatthew G. Knepley       PetscHSetI    globalbcdofs;
13281b68eb51SMatthew G. Knepley       PetscHashIter hi;
13298135ed82SLawrence Mitchell       MPI_Comm      comm = PetscObjectComm((PetscObject)pc);
13301b68eb51SMatthew G. Knepley 
13315f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscHSetICreate(&globalbcdofs));
13325f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSynchronizedPrintf(comm, "Patch %d: owned dofs:\n", v));
13331b68eb51SMatthew G. Knepley       PetscHashIterBegin(owneddofs, hi);
13341b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(owneddofs, hi)) {
13358135ed82SLawrence Mitchell         PetscInt globalDof;
13368135ed82SLawrence Mitchell 
13371b68eb51SMatthew G. Knepley         PetscHashIterGetKey(owneddofs, hi, globalDof);
13381b68eb51SMatthew G. Knepley         PetscHashIterNext(owneddofs, hi);
13395f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSynchronizedPrintf(comm, "%d ", globalDof));
13408135ed82SLawrence Mitchell       }
13415f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSynchronizedPrintf(comm, "\n"));
13425f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSynchronizedPrintf(comm, "Patch %d: seen dofs:\n", v));
13431b68eb51SMatthew G. Knepley       PetscHashIterBegin(seendofs, hi);
13441b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(seendofs, hi)) {
13458135ed82SLawrence Mitchell         PetscInt globalDof;
13468135ed82SLawrence Mitchell         PetscBool flg;
13478135ed82SLawrence Mitchell 
13481b68eb51SMatthew G. Knepley         PetscHashIterGetKey(seendofs, hi, globalDof);
13491b68eb51SMatthew G. Knepley         PetscHashIterNext(seendofs, hi);
13505f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSynchronizedPrintf(comm, "%d ", globalDof));
13518135ed82SLawrence Mitchell 
13525f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscHSetIHas(globalBcs, globalDof, &flg));
13535f80ce2aSJacob Faibussowitsch         if (flg) CHKERRQ(PetscHSetIAdd(globalbcdofs, globalDof));
13548135ed82SLawrence Mitchell       }
13555f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSynchronizedPrintf(comm, "\n"));
13565f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSynchronizedPrintf(comm, "Patch %d: global BCs:\n", v));
13575f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscHSetIGetSize(globalbcdofs, &numBcs));
13588135ed82SLawrence Mitchell       if (numBcs > 0) {
13591b68eb51SMatthew G. Knepley         PetscHashIterBegin(globalbcdofs, hi);
13601b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(globalbcdofs, hi)) {
13618135ed82SLawrence Mitchell           PetscInt globalDof;
13621b68eb51SMatthew G. Knepley           PetscHashIterGetKey(globalbcdofs, hi, globalDof);
13631b68eb51SMatthew G. Knepley           PetscHashIterNext(globalbcdofs, hi);
13645f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSynchronizedPrintf(comm, "%d ", globalDof));
13658135ed82SLawrence Mitchell         }
13668135ed82SLawrence Mitchell       }
13675f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSynchronizedPrintf(comm, "\n"));
13685f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSynchronizedPrintf(comm, "Patch %d: artificial BCs:\n", v));
13695f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscHSetIGetSize(artificialbcs, &numBcs));
13708135ed82SLawrence Mitchell       if (numBcs > 0) {
13711b68eb51SMatthew G. Knepley         PetscHashIterBegin(artificialbcs, hi);
13721b68eb51SMatthew G. Knepley         while (!PetscHashIterAtEnd(artificialbcs, hi)) {
13738135ed82SLawrence Mitchell           PetscInt globalDof;
13741b68eb51SMatthew G. Knepley           PetscHashIterGetKey(artificialbcs, hi, globalDof);
13751b68eb51SMatthew G. Knepley           PetscHashIterNext(artificialbcs, hi);
13765f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSynchronizedPrintf(comm, "%d ", globalDof));
13778135ed82SLawrence Mitchell         }
13788135ed82SLawrence Mitchell       }
13795f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSynchronizedPrintf(comm, "\n\n"));
13805f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscHSetIDestroy(&globalbcdofs));
13818135ed82SLawrence Mitchell     }
13824bbf5ea8SMatthew G. Knepley    for (k = 0; k < patch->nsubspaces; ++k) {
13834bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
13844bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
13854bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
13864bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
13874bbf5ea8SMatthew G. Knepley 
13884bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
13894bbf5ea8SMatthew G. Knepley         /* Walk over the cells in this patch. */
13904bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
13915f824522SMatthew G. Knepley         PetscInt       cell = c;
13924bbf5ea8SMatthew G. Knepley 
13935f824522SMatthew G. Knepley         /* TODO Change this to an IS */
13945f824522SMatthew G. Knepley         if (cellNumbering) {
13955f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionGetDof(cellNumbering, c, &cell));
13962c71b3e2SJacob Faibussowitsch           PetscCheckFalse(cell <= 0,PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_OUTOFRANGE, "Cell %D doesn't appear in cell numbering map", c);
13975f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionGetOffset(cellNumbering, c, &cell));
13985f824522SMatthew G. Knepley         }
13994bbf5ea8SMatthew G. Knepley         newCellsArray[i] = cell;
14004bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
14014bbf5ea8SMatthew G. Knepley           /* For each global dof, map it into contiguous local storage. */
14024bbf5ea8SMatthew G. Knepley           const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + subspaceOffset;
14034bbf5ea8SMatthew G. Knepley           /* finally, loop over block size */
14044bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
14051b68eb51SMatthew G. Knepley             PetscInt  localDof;
14061b68eb51SMatthew G. Knepley             PetscBool isGlobalBcDof, isArtificialBcDof;
14074bbf5ea8SMatthew G. Knepley 
1408557beb66SLawrence Mitchell             /* first, check if this is either a globally enforced or locally enforced BC dof */
14095f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscHSetIHas(globalBcs, globalDof + l, &isGlobalBcDof));
14105f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscHSetIHas(artificialbcs, globalDof + l, &isArtificialBcDof));
1411557beb66SLawrence Mitchell 
1412557beb66SLawrence Mitchell             /* if it's either, don't ever give it a local dof number */
14131b68eb51SMatthew G. Knepley             if (isGlobalBcDof || isArtificialBcDof) {
1414c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = -1; /* don't use this in assembly in this patch */
1415557beb66SLawrence Mitchell             } else {
14165f80ce2aSJacob Faibussowitsch               CHKERRQ(PetscHMapIGet(ht, globalDof + l, &localDof));
14174bbf5ea8SMatthew G. Knepley               if (localDof == -1) {
14184bbf5ea8SMatthew G. Knepley                 localDof = localIndex++;
14195f80ce2aSJacob Faibussowitsch                 CHKERRQ(PetscHMapISet(ht, globalDof + l, localDof));
14204bbf5ea8SMatthew G. Knepley               }
14212c71b3e2SJacob Faibussowitsch               PetscCheckFalse(globalIndex >= numDofs,PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
14224bbf5ea8SMatthew G. Knepley               /* And store. */
1423c2e6f3c0SFlorian Wechsung               dofsArray[globalIndex] = localDof;
14244bbf5ea8SMatthew G. Knepley             }
1425c2e6f3c0SFlorian Wechsung 
14260904074fSPatrick Farrell             if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1427c2e6f3c0SFlorian Wechsung               if (isGlobalBcDof) {
1428e047a90bSFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = -1; /* don't use this in assembly in this patch */
1429c2e6f3c0SFlorian Wechsung               } else {
14305f80ce2aSJacob Faibussowitsch                 CHKERRQ(PetscHMapIGet(htWithArtificial, globalDof + l, &localDof));
1431c2e6f3c0SFlorian Wechsung                 if (localDof == -1) {
1432c2e6f3c0SFlorian Wechsung                   localDof = localIndexWithArtificial++;
14335f80ce2aSJacob Faibussowitsch                   CHKERRQ(PetscHMapISet(htWithArtificial, globalDof + l, localDof));
1434c2e6f3c0SFlorian Wechsung                 }
14352c71b3e2SJacob Faibussowitsch                 PetscCheckFalse(globalIndex >= numDofs,PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
1436c2e6f3c0SFlorian Wechsung                 /* And store.*/
1437c2e6f3c0SFlorian Wechsung                 dofsArrayWithArtificial[globalIndex] = localDof;
1438c2e6f3c0SFlorian Wechsung               }
1439c2e6f3c0SFlorian Wechsung             }
14400904074fSPatrick Farrell 
14410904074fSPatrick Farrell             if (isNonlinear) {
14420904074fSPatrick Farrell               /* Build the dofmap for the function space with _all_ dofs,
14430904074fSPatrick Farrell                  including those in any kind of boundary condition */
14445f80ce2aSJacob Faibussowitsch               CHKERRQ(PetscHMapIGet(htWithAll, globalDof + l, &localDof));
14450904074fSPatrick Farrell               if (localDof == -1) {
14460904074fSPatrick Farrell                 localDof = localIndexWithAll++;
14475f80ce2aSJacob Faibussowitsch                 CHKERRQ(PetscHMapISet(htWithAll, globalDof + l, localDof));
14480904074fSPatrick Farrell               }
14492c71b3e2SJacob Faibussowitsch               PetscCheckFalse(globalIndex >= numDofs,PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
14500904074fSPatrick Farrell               /* And store.*/
14510904074fSPatrick Farrell               dofsArrayWithAll[globalIndex] = localDof;
14520904074fSPatrick Farrell             }
1453c2e6f3c0SFlorian Wechsung             globalIndex++;
14544bbf5ea8SMatthew G. Knepley           }
14554bbf5ea8SMatthew G. Knepley         }
14564bbf5ea8SMatthew G. Knepley       }
1457557beb66SLawrence Mitchell     }
14584bbf5ea8SMatthew G. Knepley      /*How many local dofs in this patch? */
14590904074fSPatrick Farrell    if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
14605f80ce2aSJacob Faibussowitsch      CHKERRQ(PetscHMapIGetSize(htWithArtificial, &dof));
14615f80ce2aSJacob Faibussowitsch      CHKERRQ(PetscSectionSetDof(gtolCountsWithArtificial, v, dof));
1462c2e6f3c0SFlorian Wechsung    }
14630904074fSPatrick Farrell    if (isNonlinear) {
14645f80ce2aSJacob Faibussowitsch      CHKERRQ(PetscHMapIGetSize(htWithAll, &dof));
14655f80ce2aSJacob Faibussowitsch      CHKERRQ(PetscSectionSetDof(gtolCountsWithAll, v, dof));
14660904074fSPatrick Farrell    }
14675f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHMapIGetSize(ht, &dof));
14685f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetDof(gtolCounts, v, dof));
14694bbf5ea8SMatthew G. Knepley   }
1470b6bb21d1SLawrence Mitchell 
14715f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&dm));
14722c71b3e2SJacob Faibussowitsch   PetscCheckFalse(globalIndex != numDofs,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Expected number of dofs (%d) doesn't match found number (%d)", numDofs, globalIndex);
14735f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetUp(gtolCounts));
14745f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetStorageSize(gtolCounts, &numGlobalDofs));
14755f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(numGlobalDofs, &globalDofsArray));
14764bbf5ea8SMatthew G. Knepley 
14770904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
14785f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetUp(gtolCountsWithArtificial));
14795f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetStorageSize(gtolCountsWithArtificial, &numGlobalDofsWithArtificial));
14805f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(numGlobalDofsWithArtificial, &globalDofsArrayWithArtificial));
1481c2e6f3c0SFlorian Wechsung   }
14820904074fSPatrick Farrell   if (isNonlinear) {
14835f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetUp(gtolCountsWithAll));
14845f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetStorageSize(gtolCountsWithAll, &numGlobalDofsWithAll));
14855f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(numGlobalDofsWithAll, &globalDofsArrayWithAll));
14860904074fSPatrick Farrell   }
14874bbf5ea8SMatthew 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. */
14884bbf5ea8SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
14891b68eb51SMatthew G. Knepley     PetscHashIter hi;
14905f824522SMatthew G. Knepley     PetscInt      dof, off, Np, ooff, i, j, k, l;
14914bbf5ea8SMatthew G. Knepley 
14925f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHMapIClear(ht));
14935f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHMapIClear(htWithArtificial));
14945f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHMapIClear(htWithAll));
14955f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(cellCounts, v, &dof));
14965f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(cellCounts, v, &off));
14975f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(pointCounts, v, &Np));
14985f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(pointCounts, v, &ooff));
14994bbf5ea8SMatthew G. Knepley     if (dof <= 0) continue;
15004bbf5ea8SMatthew G. Knepley 
15014bbf5ea8SMatthew G. Knepley     for (k = 0; k < patch->nsubspaces; ++k) {
15024bbf5ea8SMatthew G. Knepley       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
15034bbf5ea8SMatthew G. Knepley       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
15044bbf5ea8SMatthew G. Knepley       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
15054bbf5ea8SMatthew G. Knepley       PetscInt        bs             = patch->bs[k];
1506d490bb3dSLawrence Mitchell       PetscInt        goff;
15074bbf5ea8SMatthew G. Knepley 
15084bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
15094bbf5ea8SMatthew G. Knepley         /* Reconstruct mapping of global-to-local on this patch. */
15104bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
15115f824522SMatthew G. Knepley         PetscInt       cell = c;
15124bbf5ea8SMatthew G. Knepley 
15135f80ce2aSJacob Faibussowitsch         if (cellNumbering) CHKERRQ(PetscSectionGetOffset(cellNumbering, c, &cell));
15144bbf5ea8SMatthew G. Knepley         for (j = 0; j < nodesPerCell; ++j) {
15154bbf5ea8SMatthew G. Knepley           for (l = 0; l < bs; ++l) {
15165f824522SMatthew G. Knepley             const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset;
1517c2e6f3c0SFlorian Wechsung             const PetscInt localDof  = dofsArray[key];
15185f80ce2aSJacob Faibussowitsch             if (localDof >= 0) CHKERRQ(PetscHMapISet(ht, globalDof, localDof));
15190904074fSPatrick Farrell             if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1520c2e6f3c0SFlorian Wechsung               const PetscInt localDofWithArtificial = dofsArrayWithArtificial[key];
1521c2e6f3c0SFlorian Wechsung               if (localDofWithArtificial >= 0) {
15225f80ce2aSJacob Faibussowitsch                 CHKERRQ(PetscHMapISet(htWithArtificial, globalDof, localDofWithArtificial));
1523c2e6f3c0SFlorian Wechsung               }
1524c2e6f3c0SFlorian Wechsung             }
15250904074fSPatrick Farrell             if (isNonlinear) {
15260904074fSPatrick Farrell               const PetscInt localDofWithAll = dofsArrayWithAll[key];
15270904074fSPatrick Farrell               if (localDofWithAll >= 0) {
15285f80ce2aSJacob Faibussowitsch                 CHKERRQ(PetscHMapISet(htWithAll, globalDof, localDofWithAll));
15290904074fSPatrick Farrell               }
15300904074fSPatrick Farrell             }
1531c2e6f3c0SFlorian Wechsung             key++;
15324bbf5ea8SMatthew G. Knepley           }
15334bbf5ea8SMatthew G. Knepley         }
15344bbf5ea8SMatthew G. Knepley       }
1535557beb66SLawrence Mitchell 
15364bbf5ea8SMatthew G. Knepley       /* Shove it in the output data structure. */
15375f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetOffset(gtolCounts, v, &goff));
15381b68eb51SMatthew G. Knepley       PetscHashIterBegin(ht, hi);
15391b68eb51SMatthew G. Knepley       while (!PetscHashIterAtEnd(ht, hi)) {
15404bbf5ea8SMatthew G. Knepley         PetscInt globalDof, localDof;
15414bbf5ea8SMatthew G. Knepley 
15421b68eb51SMatthew G. Knepley         PetscHashIterGetKey(ht, hi, globalDof);
15431b68eb51SMatthew G. Knepley         PetscHashIterGetVal(ht, hi, localDof);
15444bbf5ea8SMatthew G. Knepley         if (globalDof >= 0) globalDofsArray[goff + localDof] = globalDof;
15451b68eb51SMatthew G. Knepley         PetscHashIterNext(ht, hi);
15464bbf5ea8SMatthew G. Knepley       }
15475f824522SMatthew G. Knepley 
15480904074fSPatrick Farrell       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
15495f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetOffset(gtolCountsWithArtificial, v, &goff));
1550c2e6f3c0SFlorian Wechsung         PetscHashIterBegin(htWithArtificial, hi);
1551c2e6f3c0SFlorian Wechsung         while (!PetscHashIterAtEnd(htWithArtificial, hi)) {
1552c2e6f3c0SFlorian Wechsung           PetscInt globalDof, localDof;
1553c2e6f3c0SFlorian Wechsung           PetscHashIterGetKey(htWithArtificial, hi, globalDof);
1554c2e6f3c0SFlorian Wechsung           PetscHashIterGetVal(htWithArtificial, hi, localDof);
1555c2e6f3c0SFlorian Wechsung           if (globalDof >= 0) globalDofsArrayWithArtificial[goff + localDof] = globalDof;
1556c2e6f3c0SFlorian Wechsung           PetscHashIterNext(htWithArtificial, hi);
1557c2e6f3c0SFlorian Wechsung         }
1558c2e6f3c0SFlorian Wechsung       }
15590904074fSPatrick Farrell       if (isNonlinear) {
15605f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetOffset(gtolCountsWithAll, v, &goff));
15610904074fSPatrick Farrell         PetscHashIterBegin(htWithAll, hi);
15620904074fSPatrick Farrell         while (!PetscHashIterAtEnd(htWithAll, hi)) {
15630904074fSPatrick Farrell           PetscInt globalDof, localDof;
15640904074fSPatrick Farrell           PetscHashIterGetKey(htWithAll, hi, globalDof);
15650904074fSPatrick Farrell           PetscHashIterGetVal(htWithAll, hi, localDof);
15660904074fSPatrick Farrell           if (globalDof >= 0) globalDofsArrayWithAll[goff + localDof] = globalDof;
15670904074fSPatrick Farrell           PetscHashIterNext(htWithAll, hi);
15680904074fSPatrick Farrell         }
15690904074fSPatrick Farrell       }
1570c2e6f3c0SFlorian Wechsung 
15715f824522SMatthew G. Knepley       for (p = 0; p < Np; ++p) {
15725f824522SMatthew G. Knepley         const PetscInt point = pointsArray[ooff + p];
15735f824522SMatthew G. Knepley         PetscInt       globalDof, localDof;
15745f824522SMatthew G. Knepley 
15755f80ce2aSJacob Faibussowitsch         CHKERRQ(PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, point, NULL, &globalDof));
15765f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscHMapIGet(ht, globalDof, &localDof));
15775f824522SMatthew G. Knepley         offsArray[(ooff + p)*Nf + k] = localDof;
15780904074fSPatrick Farrell         if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
15795f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscHMapIGet(htWithArtificial, globalDof, &localDof));
1580c2e6f3c0SFlorian Wechsung           offsArrayWithArtificial[(ooff + p)*Nf + k] = localDof;
1581c2e6f3c0SFlorian Wechsung         }
15820904074fSPatrick Farrell         if (isNonlinear) {
15835f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscHMapIGet(htWithAll, globalDof, &localDof));
15840904074fSPatrick Farrell           offsArrayWithAll[(ooff + p)*Nf + k] = localDof;
15850904074fSPatrick Farrell         }
15865f824522SMatthew G. Knepley       }
15874bbf5ea8SMatthew G. Knepley     }
15884bbf5ea8SMatthew G. Knepley 
15895f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetIDestroy(&globalBcs));
15905f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetIDestroy(&ownedpts));
15915f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetIDestroy(&seenpts));
15925f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetIDestroy(&owneddofs));
15935f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetIDestroy(&seendofs));
15945f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetIDestroy(&artificialbcs));
1595557beb66SLawrence Mitchell 
15964bbf5ea8SMatthew G. Knepley       /* At this point, we have a hash table ht built that maps globalDof -> localDof.
15974bbf5ea8SMatthew G. Knepley      We need to create the dof table laid out cellwise first, then by subspace,
15984bbf5ea8SMatthew G. Knepley      as the assembler assembles cell-wise and we need to stuff the different
15994bbf5ea8SMatthew G. Knepley      contributions of the different function spaces to the right places. So we loop
16004bbf5ea8SMatthew G. Knepley      over cells, then over subspaces. */
16014bbf5ea8SMatthew G. Knepley     if (patch->nsubspaces > 1) { /* for nsubspaces = 1, data we need is already in dofsArray */
16024bbf5ea8SMatthew G. Knepley       for (i = off; i < off + dof; ++i) {
16034bbf5ea8SMatthew G. Knepley         const PetscInt c    = cellsArray[i];
16045f824522SMatthew G. Knepley         PetscInt       cell = c;
16054bbf5ea8SMatthew G. Knepley 
16065f80ce2aSJacob Faibussowitsch         if (cellNumbering) CHKERRQ(PetscSectionGetOffset(cellNumbering, c, &cell));
16074bbf5ea8SMatthew G. Knepley         for (k = 0; k < patch->nsubspaces; ++k) {
16084bbf5ea8SMatthew G. Knepley           const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
16094bbf5ea8SMatthew G. Knepley           PetscInt        nodesPerCell   = patch->nodesPerCell[k];
16104bbf5ea8SMatthew G. Knepley           PetscInt        subspaceOffset = patch->subspaceOffsets[k];
16114bbf5ea8SMatthew G. Knepley           PetscInt        bs             = patch->bs[k];
16124bbf5ea8SMatthew G. Knepley 
16134bbf5ea8SMatthew G. Knepley           for (j = 0; j < nodesPerCell; ++j) {
16144bbf5ea8SMatthew G. Knepley             for (l = 0; l < bs; ++l) {
16155f824522SMatthew G. Knepley               const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset;
16164bbf5ea8SMatthew G. Knepley               PetscInt       localDof;
16174bbf5ea8SMatthew G. Knepley 
16185f80ce2aSJacob Faibussowitsch               CHKERRQ(PetscHMapIGet(ht, globalDof, &localDof));
1619557beb66SLawrence Mitchell               /* If it's not in the hash table, i.e. is a BC dof,
16201b68eb51SMatthew G. Knepley                then the PetscHSetIMap above gives -1, which matches
1621557beb66SLawrence Mitchell                exactly the convention for PETSc's matrix assembly to
1622557beb66SLawrence Mitchell                ignore the dof. So we don't need to do anything here */
1623c2e6f3c0SFlorian Wechsung               asmArray[asmKey] = localDof;
16240904074fSPatrick Farrell               if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
16255f80ce2aSJacob Faibussowitsch                 CHKERRQ(PetscHMapIGet(htWithArtificial, globalDof, &localDof));
1626c2e6f3c0SFlorian Wechsung                 asmArrayWithArtificial[asmKey] = localDof;
1627c2e6f3c0SFlorian Wechsung               }
16280904074fSPatrick Farrell               if (isNonlinear) {
16295f80ce2aSJacob Faibussowitsch                 CHKERRQ(PetscHMapIGet(htWithAll, globalDof, &localDof));
16300904074fSPatrick Farrell                 asmArrayWithAll[asmKey] = localDof;
16310904074fSPatrick Farrell               }
1632c2e6f3c0SFlorian Wechsung               asmKey++;
16334bbf5ea8SMatthew G. Knepley             }
16344bbf5ea8SMatthew G. Knepley           }
16354bbf5ea8SMatthew G. Knepley         }
16364bbf5ea8SMatthew G. Knepley       }
16374bbf5ea8SMatthew G. Knepley     }
16384bbf5ea8SMatthew G. Knepley   }
1639c2e6f3c0SFlorian Wechsung   if (1 == patch->nsubspaces) {
16405f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscArraycpy(asmArray, dofsArray, numDofs));
16410904074fSPatrick Farrell     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
16425f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscArraycpy(asmArrayWithArtificial, dofsArrayWithArtificial, numDofs));
1643c2e6f3c0SFlorian Wechsung     }
16440904074fSPatrick Farrell     if (isNonlinear) {
16455f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscArraycpy(asmArrayWithAll, dofsArrayWithAll, numDofs));
16460904074fSPatrick Farrell     }
1647c2e6f3c0SFlorian Wechsung   }
16484bbf5ea8SMatthew G. Knepley 
16495f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHMapIDestroy(&ht));
16505f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHMapIDestroy(&htWithArtificial));
16515f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHMapIDestroy(&htWithAll));
16525f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(cells, &cellsArray));
16535f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(points, &pointsArray));
16545f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(dofsArray));
16550904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
16565f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(dofsArrayWithArtificial));
1657c2e6f3c0SFlorian Wechsung   }
16580904074fSPatrick Farrell   if (isNonlinear) {
16595f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(dofsArrayWithAll));
16600904074fSPatrick Farrell   }
16615f824522SMatthew G. Knepley   /* Create placeholder section for map from points to patch dofs */
16625f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionCreate(PETSC_COMM_SELF, &patch->patchSection));
16635f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetNumFields(patch->patchSection, patch->nsubspaces));
16641e5fa6bbSLawrence Mitchell   if (patch->combined) {
16651e5fa6bbSLawrence Mitchell     PetscInt numFields;
16665f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetNumFields(patch->dofSection[0], &numFields));
16672c71b3e2SJacob Faibussowitsch     PetscCheckFalse(numFields != patch->nsubspaces,PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Mismatch between number of section fields %D and number of subspaces %D", numFields, patch->nsubspaces);
16685f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetChart(patch->dofSection[0], &pStart, &pEnd));
16695f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetChart(patch->patchSection, pStart, pEnd));
16705f824522SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
16715f824522SMatthew G. Knepley       PetscInt dof, fdof, f;
16725f824522SMatthew G. Knepley 
16735f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetDof(patch->dofSection[0], p, &dof));
16745f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetDof(patch->patchSection, p, dof));
16755f824522SMatthew G. Knepley       for (f = 0; f < patch->nsubspaces; ++f) {
16765f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetFieldDof(patch->dofSection[0], p, f, &fdof));
16775f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionSetFieldDof(patch->patchSection, p, f, fdof));
16785f824522SMatthew G. Knepley       }
16791e5fa6bbSLawrence Mitchell     }
16801e5fa6bbSLawrence Mitchell   } else {
16811e5fa6bbSLawrence Mitchell     PetscInt pStartf, pEndf, f;
16821e5fa6bbSLawrence Mitchell     pStart = PETSC_MAX_INT;
16831e5fa6bbSLawrence Mitchell     pEnd = PETSC_MIN_INT;
16841e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
16855f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf));
16861e5fa6bbSLawrence Mitchell       pStart = PetscMin(pStart, pStartf);
16871e5fa6bbSLawrence Mitchell       pEnd = PetscMax(pEnd, pEndf);
16881e5fa6bbSLawrence Mitchell     }
16895f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetChart(patch->patchSection, pStart, pEnd));
16901e5fa6bbSLawrence Mitchell     for (f = 0; f < patch->nsubspaces; ++f) {
16915f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf));
16921e5fa6bbSLawrence Mitchell       for (p = pStartf; p < pEndf; ++p) {
16931e5fa6bbSLawrence Mitchell         PetscInt fdof;
16945f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetDof(patch->dofSection[f], p, &fdof));
16955f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionAddDof(patch->patchSection, p, fdof));
16965f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionSetFieldDof(patch->patchSection, p, f, fdof));
1697bdd9e0cdSPatrick Farrell       }
1698bdd9e0cdSPatrick Farrell     }
16995f824522SMatthew G. Knepley   }
17005f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetUp(patch->patchSection));
17015f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetUseFieldOffsets(patch->patchSection, PETSC_TRUE));
17024bbf5ea8SMatthew G. Knepley   /* Replace cell indices with firedrake-numbered ones. */
17035f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGeneralSetIndices(cells, numCells, (const PetscInt *) newCellsArray, PETSC_OWN_POINTER));
17045f80ce2aSJacob Faibussowitsch   CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofs, globalDofsArray, PETSC_OWN_POINTER, &patch->gtol));
17055f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetName((PetscObject) patch->gtol, "Global Indices"));
17065f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_g2l_view", patch->classname));
17075f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionViewFromOptions(patch->gtolCounts, (PetscObject) pc, option));
17085f80ce2aSJacob Faibussowitsch   CHKERRQ(ISViewFromOptions(patch->gtol, (PetscObject) pc, option));
17095f80ce2aSJacob Faibussowitsch   CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArray, PETSC_OWN_POINTER, &patch->dofs));
17105f80ce2aSJacob Faibussowitsch   CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArray, PETSC_OWN_POINTER, &patch->offs));
17110904074fSPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
17125f80ce2aSJacob Faibussowitsch     CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithArtificial, globalDofsArrayWithArtificial, PETSC_OWN_POINTER, &patch->gtolWithArtificial));
17135f80ce2aSJacob Faibussowitsch     CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithArtificial, PETSC_OWN_POINTER, &patch->dofsWithArtificial));
17145f80ce2aSJacob Faibussowitsch     CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithArtificial, PETSC_OWN_POINTER, &patch->offsWithArtificial));
1715c2e6f3c0SFlorian Wechsung   }
17160904074fSPatrick Farrell   if (isNonlinear) {
17175f80ce2aSJacob Faibussowitsch     CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithAll, globalDofsArrayWithAll, PETSC_OWN_POINTER, &patch->gtolWithAll));
17185f80ce2aSJacob Faibussowitsch     CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithAll, PETSC_OWN_POINTER, &patch->dofsWithAll));
17195f80ce2aSJacob Faibussowitsch     CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithAll, PETSC_OWN_POINTER, &patch->offsWithAll));
17200904074fSPatrick Farrell   }
17214bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
17224bbf5ea8SMatthew G. Knepley }
17234bbf5ea8SMatthew G. Knepley 
1724c2e6f3c0SFlorian Wechsung static PetscErrorCode PCPatchCreateMatrix_Private(PC pc, PetscInt point, Mat *mat, PetscBool withArtificial)
17254bbf5ea8SMatthew G. Knepley {
17264bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
17274bbf5ea8SMatthew G. Knepley   PetscBool      flg;
17284bbf5ea8SMatthew G. Knepley   PetscInt       csize, rsize;
17294bbf5ea8SMatthew G. Knepley   const char    *prefix = NULL;
17304bbf5ea8SMatthew G. Knepley 
17314bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
1732c2e6f3c0SFlorian Wechsung   if (withArtificial) {
1733e047a90bSFlorian Wechsung     /* would be nice if we could create a rectangular matrix of size numDofsWithArtificial x numDofs here */
17349d4fc724SLawrence Mitchell     PetscInt pStart;
17355f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetChart(patch->gtolCountsWithArtificial, &pStart, NULL));
17365f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(patch->gtolCountsWithArtificial, point + pStart, &rsize));
17379d4fc724SLawrence Mitchell     csize = rsize;
1738ff201f6aSFlorian Wechsung   } else {
17399d4fc724SLawrence Mitchell     PetscInt pStart;
17405f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetChart(patch->gtolCounts, &pStart, NULL));
17415f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(patch->gtolCounts, point + pStart, &rsize));
17429d4fc724SLawrence Mitchell     csize = rsize;
1743c2e6f3c0SFlorian Wechsung   }
1744c2e6f3c0SFlorian Wechsung 
17455f80ce2aSJacob Faibussowitsch   CHKERRQ(MatCreate(PETSC_COMM_SELF, mat));
17465f80ce2aSJacob Faibussowitsch   CHKERRQ(PCGetOptionsPrefix(pc, &prefix));
17475f80ce2aSJacob Faibussowitsch   CHKERRQ(MatSetOptionsPrefix(*mat, prefix));
17485f80ce2aSJacob Faibussowitsch   CHKERRQ(MatAppendOptionsPrefix(*mat, "pc_patch_sub_"));
17495f80ce2aSJacob Faibussowitsch   if (patch->sub_mat_type)       CHKERRQ(MatSetType(*mat, patch->sub_mat_type));
17505f80ce2aSJacob Faibussowitsch   else if (!patch->sub_mat_type) CHKERRQ(MatSetType(*mat, MATDENSE));
17515f80ce2aSJacob Faibussowitsch   CHKERRQ(MatSetSizes(*mat, rsize, csize, rsize, csize));
17525f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectTypeCompare((PetscObject) *mat, MATDENSE, &flg));
17535f80ce2aSJacob Faibussowitsch   if (!flg) CHKERRQ(PetscObjectTypeCompare((PetscObject)*mat, MATSEQDENSE, &flg));
17544bbf5ea8SMatthew G. Knepley   /* Sparse patch matrices */
17554bbf5ea8SMatthew G. Knepley   if (!flg) {
17564bbf5ea8SMatthew G. Knepley     PetscBT         bt;
17574bbf5ea8SMatthew G. Knepley     PetscInt       *dnnz      = NULL;
17584bbf5ea8SMatthew G. Knepley     const PetscInt *dofsArray = NULL;
17594bbf5ea8SMatthew G. Knepley     PetscInt        pStart, pEnd, ncell, offset, c, i, j;
17604bbf5ea8SMatthew G. Knepley 
1761c2e6f3c0SFlorian Wechsung     if (withArtificial) {
17625f80ce2aSJacob Faibussowitsch       CHKERRQ(ISGetIndices(patch->dofsWithArtificial, &dofsArray));
1763ff201f6aSFlorian Wechsung     } else {
17645f80ce2aSJacob Faibussowitsch       CHKERRQ(ISGetIndices(patch->dofs, &dofsArray));
1765c2e6f3c0SFlorian Wechsung     }
17665f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd));
17674bbf5ea8SMatthew G. Knepley     point += pStart;
17682c71b3e2SJacob Faibussowitsch     PetscCheckFalse(point >= pEnd,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %D not in [%D, %D)", point, pStart, pEnd);
17695f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(patch->cellCounts, point, &ncell));
17705f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(patch->cellCounts, point, &offset));
17715f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscLogEventBegin(PC_Patch_Prealloc, pc, 0, 0, 0));
1772b2866507SPatrick Farrell     /* A PetscBT uses N^2 bits to store the sparsity pattern on a
17734bbf5ea8SMatthew G. Knepley      * patch. This is probably OK if the patches are not too big,
1774b2866507SPatrick Farrell      * but uses too much memory. We therefore switch based on rsize. */
1775b2866507SPatrick Farrell     if (rsize < 3000) { /* FIXME: I picked this switch value out of my hat */
1776d63cebbaSPatrick Farrell       PetscScalar *zeroes;
1777d63cebbaSPatrick Farrell       PetscInt rows;
1778d63cebbaSPatrick Farrell 
17795f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscCalloc1(rsize, &dnnz));
17805f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscBTCreate(rsize*rsize, &bt));
17814bbf5ea8SMatthew G. Knepley       for (c = 0; c < ncell; ++c) {
17824bbf5ea8SMatthew G. Knepley         const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell;
17834bbf5ea8SMatthew G. Knepley         for (i = 0; i < patch->totalDofsPerCell; ++i) {
17844bbf5ea8SMatthew G. Knepley           const PetscInt row = idx[i];
1785557beb66SLawrence Mitchell           if (row < 0) continue;
17864bbf5ea8SMatthew G. Knepley           for (j = 0; j < patch->totalDofsPerCell; ++j) {
17874bbf5ea8SMatthew G. Knepley             const PetscInt col = idx[j];
17884bbf5ea8SMatthew G. Knepley             const PetscInt key = row*rsize + col;
1789557beb66SLawrence Mitchell             if (col < 0) continue;
17904bbf5ea8SMatthew G. Knepley             if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
17914bbf5ea8SMatthew G. Knepley           }
17924bbf5ea8SMatthew G. Knepley         }
17934bbf5ea8SMatthew G. Knepley       }
1794d63cebbaSPatrick Farrell 
1795d63cebbaSPatrick Farrell       if (patch->usercomputeopintfacet) {
1796d63cebbaSPatrick Farrell         const PetscInt *intFacetsArray = NULL;
1797d63cebbaSPatrick Farrell         PetscInt        i, numIntFacets, intFacetOffset;
1798d63cebbaSPatrick Farrell         const PetscInt *facetCells = NULL;
1799d63cebbaSPatrick Farrell 
18005f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets));
18015f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset));
18025f80ce2aSJacob Faibussowitsch         CHKERRQ(ISGetIndices(patch->intFacetsToPatchCell, &facetCells));
18035f80ce2aSJacob Faibussowitsch         CHKERRQ(ISGetIndices(patch->intFacets, &intFacetsArray));
1804d63cebbaSPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
1805d63cebbaSPatrick Farrell           const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0];
1806d63cebbaSPatrick Farrell           const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1];
1807d63cebbaSPatrick Farrell           PetscInt       celli, cellj;
1808d63cebbaSPatrick Farrell 
1809d63cebbaSPatrick Farrell           for (celli = 0; celli < patch->totalDofsPerCell; celli++) {
1810d63cebbaSPatrick Farrell             const PetscInt row = dofsArray[(offset + cell0)*patch->totalDofsPerCell + celli];
1811b5c64f08SPatrick Farrell             if (row < 0) continue;
1812d63cebbaSPatrick Farrell             for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) {
1813d63cebbaSPatrick Farrell               const PetscInt col = dofsArray[(offset + cell1)*patch->totalDofsPerCell + cellj];
1814d63cebbaSPatrick Farrell               const PetscInt key = row*rsize + col;
1815d63cebbaSPatrick Farrell               if (col < 0) continue;
1816d63cebbaSPatrick Farrell               if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
1817d63cebbaSPatrick Farrell             }
1818d63cebbaSPatrick Farrell           }
1819d63cebbaSPatrick Farrell 
1820d63cebbaSPatrick Farrell           for (celli = 0; celli < patch->totalDofsPerCell; celli++) {
1821d63cebbaSPatrick Farrell             const PetscInt row = dofsArray[(offset + cell1)*patch->totalDofsPerCell + celli];
1822b5c64f08SPatrick Farrell             if (row < 0) continue;
1823d63cebbaSPatrick Farrell             for (cellj = 0; cellj < patch->totalDofsPerCell; cellj++) {
1824d63cebbaSPatrick Farrell               const PetscInt col = dofsArray[(offset + cell0)*patch->totalDofsPerCell + cellj];
1825d63cebbaSPatrick Farrell               const PetscInt key = row*rsize + col;
1826d63cebbaSPatrick Farrell               if (col < 0) continue;
1827d63cebbaSPatrick Farrell               if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
1828d63cebbaSPatrick Farrell             }
1829d63cebbaSPatrick Farrell           }
1830d63cebbaSPatrick Farrell         }
1831d63cebbaSPatrick Farrell       }
18325f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscBTDestroy(&bt));
18335f80ce2aSJacob Faibussowitsch       CHKERRQ(MatXAIJSetPreallocation(*mat, 1, dnnz, NULL, NULL, NULL));
18345f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(dnnz));
1835d63cebbaSPatrick Farrell 
18365f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscCalloc1(patch->totalDofsPerCell*patch->totalDofsPerCell, &zeroes));
1837d63cebbaSPatrick Farrell       for (c = 0; c < ncell; ++c) {
1838d63cebbaSPatrick Farrell         const PetscInt *idx = &dofsArray[(offset + c)*patch->totalDofsPerCell];
18395f80ce2aSJacob Faibussowitsch         CHKERRQ(MatSetValues(*mat, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, zeroes, INSERT_VALUES));
1840d63cebbaSPatrick Farrell       }
18415f80ce2aSJacob Faibussowitsch       CHKERRQ(MatGetLocalSize(*mat, &rows, NULL));
1842d63cebbaSPatrick Farrell       for (i = 0; i < rows; ++i) {
18435f80ce2aSJacob Faibussowitsch         CHKERRQ(MatSetValues(*mat, 1, &i, 1, &i, zeroes, INSERT_VALUES));
1844d63cebbaSPatrick Farrell       }
1845d63cebbaSPatrick Farrell 
1846d63cebbaSPatrick Farrell       if (patch->usercomputeopintfacet) {
1847d63cebbaSPatrick Farrell         const PetscInt *intFacetsArray = NULL;
1848d63cebbaSPatrick Farrell         PetscInt i, numIntFacets, intFacetOffset;
1849d63cebbaSPatrick Farrell         const PetscInt *facetCells = NULL;
1850d63cebbaSPatrick Farrell 
18515f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets));
18525f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset));
18535f80ce2aSJacob Faibussowitsch         CHKERRQ(ISGetIndices(patch->intFacetsToPatchCell, &facetCells));
18545f80ce2aSJacob Faibussowitsch         CHKERRQ(ISGetIndices(patch->intFacets, &intFacetsArray));
1855d63cebbaSPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
1856d63cebbaSPatrick Farrell           const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0];
1857d63cebbaSPatrick Farrell           const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1];
1858d63cebbaSPatrick Farrell           const PetscInt *cell0idx = &dofsArray[(offset + cell0)*patch->totalDofsPerCell];
1859d63cebbaSPatrick Farrell           const PetscInt *cell1idx = &dofsArray[(offset + cell1)*patch->totalDofsPerCell];
18605f80ce2aSJacob Faibussowitsch           CHKERRQ(MatSetValues(*mat, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, zeroes, INSERT_VALUES));
18615f80ce2aSJacob Faibussowitsch           CHKERRQ(MatSetValues(*mat, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, zeroes, INSERT_VALUES));
1862d63cebbaSPatrick Farrell         }
1863d63cebbaSPatrick Farrell       }
1864d63cebbaSPatrick Farrell 
18655f80ce2aSJacob Faibussowitsch       CHKERRQ(MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY));
18665f80ce2aSJacob Faibussowitsch       CHKERRQ(MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY));
1867d63cebbaSPatrick Farrell 
18685f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(zeroes));
1869d63cebbaSPatrick Farrell 
1870b2866507SPatrick Farrell     } else { /* rsize too big, use MATPREALLOCATOR */
1871b2866507SPatrick Farrell       Mat preallocator;
1872b2866507SPatrick Farrell       PetscScalar* vals;
1873b2866507SPatrick Farrell 
18745f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscCalloc1(patch->totalDofsPerCell*patch->totalDofsPerCell, &vals));
18755f80ce2aSJacob Faibussowitsch       CHKERRQ(MatCreate(PETSC_COMM_SELF, &preallocator));
18765f80ce2aSJacob Faibussowitsch       CHKERRQ(MatSetType(preallocator, MATPREALLOCATOR));
18775f80ce2aSJacob Faibussowitsch       CHKERRQ(MatSetSizes(preallocator, rsize, rsize, rsize, rsize));
18785f80ce2aSJacob Faibussowitsch       CHKERRQ(MatSetUp(preallocator));
187911bcd083SPatrick Farrell 
1880b2866507SPatrick Farrell       for (c = 0; c < ncell; ++c) {
1881b2866507SPatrick Farrell         const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell;
18825f80ce2aSJacob Faibussowitsch         CHKERRQ(MatSetValues(preallocator, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, vals, INSERT_VALUES));
1883b2866507SPatrick Farrell       }
188411bcd083SPatrick Farrell 
188511bcd083SPatrick Farrell       if (patch->usercomputeopintfacet) {
188611bcd083SPatrick Farrell         const PetscInt *intFacetsArray = NULL;
188711bcd083SPatrick Farrell         PetscInt        i, numIntFacets, intFacetOffset;
188811bcd083SPatrick Farrell         const PetscInt *facetCells = NULL;
188911bcd083SPatrick Farrell 
18905f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets));
18915f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset));
18925f80ce2aSJacob Faibussowitsch         CHKERRQ(ISGetIndices(patch->intFacetsToPatchCell, &facetCells));
18935f80ce2aSJacob Faibussowitsch         CHKERRQ(ISGetIndices(patch->intFacets, &intFacetsArray));
189411bcd083SPatrick Farrell         for (i = 0; i < numIntFacets; i++) {
189511bcd083SPatrick Farrell           const PetscInt cell0 = facetCells[2*(intFacetOffset + i) + 0];
189611bcd083SPatrick Farrell           const PetscInt cell1 = facetCells[2*(intFacetOffset + i) + 1];
189711bcd083SPatrick Farrell           const PetscInt *cell0idx = &dofsArray[(offset + cell0)*patch->totalDofsPerCell];
189811bcd083SPatrick Farrell           const PetscInt *cell1idx = &dofsArray[(offset + cell1)*patch->totalDofsPerCell];
18995f80ce2aSJacob Faibussowitsch           CHKERRQ(MatSetValues(preallocator, patch->totalDofsPerCell, cell0idx, patch->totalDofsPerCell, cell1idx, vals, INSERT_VALUES));
19005f80ce2aSJacob Faibussowitsch           CHKERRQ(MatSetValues(preallocator, patch->totalDofsPerCell, cell1idx, patch->totalDofsPerCell, cell0idx, vals, INSERT_VALUES));
190111bcd083SPatrick Farrell         }
190211bcd083SPatrick Farrell       }
190311bcd083SPatrick Farrell 
19045f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(vals));
19055f80ce2aSJacob Faibussowitsch       CHKERRQ(MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY));
19065f80ce2aSJacob Faibussowitsch       CHKERRQ(MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY));
19075f80ce2aSJacob Faibussowitsch       CHKERRQ(MatPreallocatorPreallocate(preallocator, PETSC_TRUE, *mat));
19085f80ce2aSJacob Faibussowitsch       CHKERRQ(MatDestroy(&preallocator));
1909b2866507SPatrick Farrell     }
19105f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscLogEventEnd(PC_Patch_Prealloc, pc, 0, 0, 0));
1911fe117d09SFlorian Wechsung     if (withArtificial) {
19125f80ce2aSJacob Faibussowitsch       CHKERRQ(ISRestoreIndices(patch->dofsWithArtificial, &dofsArray));
1913fe117d09SFlorian Wechsung     } else {
19145f80ce2aSJacob Faibussowitsch       CHKERRQ(ISRestoreIndices(patch->dofs, &dofsArray));
19154bbf5ea8SMatthew G. Knepley     }
1916fe117d09SFlorian Wechsung   }
19175f80ce2aSJacob Faibussowitsch   CHKERRQ(MatSetUp(*mat));
19184bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
19194bbf5ea8SMatthew G. Knepley }
19204bbf5ea8SMatthew G. Knepley 
19210904074fSPatrick Farrell static PetscErrorCode PCPatchComputeFunction_DMPlex_Private(PC pc, PetscInt patchNum, Vec x, Vec F, IS cellIS, PetscInt n, const PetscInt *l2p, const PetscInt *l2pWithAll, void *ctx)
192292d50984SMatthew G. Knepley {
192392d50984SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
1924b6bb21d1SLawrence Mitchell   DM              dm, plex;
192592d50984SMatthew G. Knepley   PetscSection    s;
192692d50984SMatthew G. Knepley   const PetscInt *parray, *oarray;
192792d50984SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
192892d50984SMatthew G. Knepley 
192992d50984SMatthew G. Knepley   PetscFunctionBegin;
1930*28b400f6SJacob Faibussowitsch   PetscCheck(!patch->precomputeElementTensors,PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Precomputing element tensors not implemented with DMPlex compute operator");
19315f80ce2aSJacob Faibussowitsch   CHKERRQ(PCGetDM(pc, &dm));
19325f80ce2aSJacob Faibussowitsch   CHKERRQ(DMConvert(dm, DMPLEX, &plex));
1933b6bb21d1SLawrence Mitchell   dm = plex;
19345f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLocalSection(dm, &s));
193592d50984SMatthew G. Knepley   /* Set offset into patch */
19365f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetDof(patch->pointCounts, patchNum, &Np));
19375f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetOffset(patch->pointCounts, patchNum, &poff));
19385f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetIndices(patch->points, &parray));
19395f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetIndices(patch->offs,   &oarray));
194092d50984SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
194192d50984SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
194292d50984SMatthew G. Knepley       const PetscInt point = parray[poff+p];
194392d50984SMatthew G. Knepley       PetscInt       dof;
194492d50984SMatthew G. Knepley 
19455f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldDof(patch->patchSection, point, f, &dof));
19465f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]));
19475f80ce2aSJacob Faibussowitsch       if (patch->nsubspaces == 1) CHKERRQ(PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]));
19485f80ce2aSJacob Faibussowitsch       else                        CHKERRQ(PetscSectionSetOffset(patch->patchSection, point, -1));
194992d50984SMatthew G. Knepley     }
195092d50984SMatthew G. Knepley   }
19515f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(patch->points, &parray));
19525f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(patch->offs,   &oarray));
19535f80ce2aSJacob Faibussowitsch   if (patch->viewSection) CHKERRQ(ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection));
19545f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexComputeResidual_Patch_Internal(dm, patch->patchSection, cellIS, 0.0, x, NULL, F, ctx));
19555f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&dm));
195692d50984SMatthew G. Knepley   PetscFunctionReturn(0);
195792d50984SMatthew G. Knepley }
195892d50984SMatthew G. Knepley 
195992d50984SMatthew G. Knepley PetscErrorCode PCPatchComputeFunction_Internal(PC pc, Vec x, Vec F, PetscInt point)
196092d50984SMatthew G. Knepley {
196192d50984SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
196292d50984SMatthew G. Knepley   const PetscInt *dofsArray;
19630904074fSPatrick Farrell   const PetscInt *dofsArrayWithAll;
196492d50984SMatthew G. Knepley   const PetscInt *cellsArray;
196592d50984SMatthew G. Knepley   PetscInt        ncell, offset, pStart, pEnd;
196692d50984SMatthew G. Knepley   PetscErrorCode  ierr;
196792d50984SMatthew G. Knepley 
196892d50984SMatthew G. Knepley   PetscFunctionBegin;
19695f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0));
1970*28b400f6SJacob Faibussowitsch   PetscCheck(patch->usercomputeop,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback");
19715f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetIndices(patch->dofs, &dofsArray));
19725f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll));
19735f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetIndices(patch->cells, &cellsArray));
19745f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd));
197592d50984SMatthew G. Knepley 
197692d50984SMatthew G. Knepley   point += pStart;
19772c71b3e2SJacob Faibussowitsch   PetscCheckFalse(point >= pEnd,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %D not in [%D, %D)", point, pStart, pEnd);
197892d50984SMatthew G. Knepley 
19795f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetDof(patch->cellCounts, point, &ncell));
19805f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetOffset(patch->cellCounts, point, &offset));
198192d50984SMatthew G. Knepley   if (ncell <= 0) {
19825f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
198392d50984SMatthew G. Knepley     PetscFunctionReturn(0);
198492d50984SMatthew G. Knepley   }
19855f80ce2aSJacob Faibussowitsch   CHKERRQ(VecSet(F, 0.0));
198692d50984SMatthew G. Knepley   PetscStackPush("PCPatch user callback");
198792d50984SMatthew G. Knepley   /* Cannot reuse the same IS because the geometry info is being cached in it */
19885f80ce2aSJacob Faibussowitsch   CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS));
198939fd2e8aSPatrick Farrell   ierr = patch->usercomputef(pc, point, x, F, patch->cellIS, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell,
19900904074fSPatrick Farrell                                                                                             dofsArrayWithAll + offset*patch->totalDofsPerCell,
199139fd2e8aSPatrick Farrell                                                                                             patch->usercomputefctx);CHKERRQ(ierr);
199292d50984SMatthew G. Knepley   PetscStackPop;
19935f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&patch->cellIS));
19945f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(patch->dofs, &dofsArray));
19955f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll));
19965f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(patch->cells, &cellsArray));
199792d50984SMatthew G. Knepley   if (patch->viewMatrix) {
199892d50984SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
199992d50984SMatthew G. Knepley 
20005f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch vector for Point %D", point));
20015f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetName((PetscObject) F, name));
20025f80ce2aSJacob Faibussowitsch     CHKERRQ(ObjectView((PetscObject) F, patch->viewerMatrix, patch->formatMatrix));
200392d50984SMatthew G. Knepley   }
20045f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
200592d50984SMatthew G. Knepley   PetscFunctionReturn(0);
200692d50984SMatthew G. Knepley }
200792d50984SMatthew G. Knepley 
20080904074fSPatrick Farrell static PetscErrorCode PCPatchComputeOperator_DMPlex_Private(PC pc, PetscInt patchNum, Vec x, Mat J, IS cellIS, PetscInt n, const PetscInt *l2p, const PetscInt *l2pWithAll, void *ctx)
20095f824522SMatthew G. Knepley {
20105f824522SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
2011b6bb21d1SLawrence Mitchell   DM              dm, plex;
20125f824522SMatthew G. Knepley   PetscSection    s;
20135f824522SMatthew G. Knepley   const PetscInt *parray, *oarray;
20145f824522SMatthew G. Knepley   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
20155f824522SMatthew G. Knepley 
20165f824522SMatthew G. Knepley   PetscFunctionBegin;
20175f80ce2aSJacob Faibussowitsch   CHKERRQ(PCGetDM(pc, &dm));
20185f80ce2aSJacob Faibussowitsch   CHKERRQ(DMConvert(dm, DMPLEX, &plex));
2019b6bb21d1SLawrence Mitchell   dm = plex;
20205f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLocalSection(dm, &s));
20215f824522SMatthew G. Knepley   /* Set offset into patch */
20225f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetDof(patch->pointCounts, patchNum, &Np));
20235f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetOffset(patch->pointCounts, patchNum, &poff));
20245f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetIndices(patch->points, &parray));
20255f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetIndices(patch->offs,   &oarray));
20265f824522SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
20275f824522SMatthew G. Knepley     for (p = 0; p < Np; ++p) {
20285f824522SMatthew G. Knepley       const PetscInt point = parray[poff+p];
20295f824522SMatthew G. Knepley       PetscInt       dof;
20305f824522SMatthew G. Knepley 
20315f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldDof(patch->patchSection, point, f, &dof));
20325f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]));
20335f80ce2aSJacob Faibussowitsch       if (patch->nsubspaces == 1) CHKERRQ(PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]));
20345f80ce2aSJacob Faibussowitsch       else                        CHKERRQ(PetscSectionSetOffset(patch->patchSection, point, -1));
20355f824522SMatthew G. Knepley     }
20365f824522SMatthew G. Knepley   }
20375f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(patch->points, &parray));
20385f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(patch->offs,   &oarray));
20395f80ce2aSJacob Faibussowitsch   if (patch->viewSection) CHKERRQ(ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection));
20405f824522SMatthew G. Knepley   /* TODO Shut off MatViewFromOptions() in MatAssemblyEnd() here */
20415f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexComputeJacobian_Patch_Internal(dm, patch->patchSection, patch->patchSection, cellIS, 0.0, 0.0, x, NULL, J, J, ctx));
20425f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&dm));
20435f824522SMatthew G. Knepley   PetscFunctionReturn(0);
20445f824522SMatthew G. Knepley }
20455f824522SMatthew G. Knepley 
2046a685ae26SLawrence Mitchell /* This function zeros mat on entry */
204734d8b122SPatrick Farrell PetscErrorCode PCPatchComputeOperator_Internal(PC pc, Vec x, Mat mat, PetscInt point, PetscBool withArtificial)
20484bbf5ea8SMatthew G. Knepley {
20494bbf5ea8SMatthew G. Knepley   PC_PATCH       *patch = (PC_PATCH *) pc->data;
20504bbf5ea8SMatthew G. Knepley   const PetscInt *dofsArray;
20510904074fSPatrick Farrell   const PetscInt *dofsArrayWithAll = NULL;
20524bbf5ea8SMatthew G. Knepley   const PetscInt *cellsArray;
2053eb62eeaaSLawrence Mitchell   PetscInt        ncell, offset, pStart, pEnd, numIntFacets, intFacetOffset;
20544d04e9f1SPatrick Farrell   PetscBool       isNonlinear;
20554bbf5ea8SMatthew G. Knepley 
20564bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
20575f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0));
2058debbdec3SPatrick Farrell   isNonlinear = patch->isNonlinear;
2059*28b400f6SJacob Faibussowitsch   PetscCheck(patch->usercomputeop,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback");
2060c2e6f3c0SFlorian Wechsung   if (withArtificial) {
20615f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(patch->dofsWithArtificial, &dofsArray));
2062c2e6f3c0SFlorian Wechsung   } else {
20635f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(patch->dofs, &dofsArray));
2064c2e6f3c0SFlorian Wechsung   }
20654d04e9f1SPatrick Farrell   if (isNonlinear) {
20665f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll));
20674d04e9f1SPatrick Farrell   }
20685f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetIndices(patch->cells, &cellsArray));
20695f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd));
20704bbf5ea8SMatthew G. Knepley 
20714bbf5ea8SMatthew G. Knepley   point += pStart;
20722c71b3e2SJacob Faibussowitsch   PetscCheckFalse(point >= pEnd,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %D not in [%D, %D)", point, pStart, pEnd);
20734bbf5ea8SMatthew G. Knepley 
20745f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetDof(patch->cellCounts, point, &ncell));
20755f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetOffset(patch->cellCounts, point, &offset));
20764bbf5ea8SMatthew G. Knepley   if (ncell <= 0) {
20775f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
20784bbf5ea8SMatthew G. Knepley     PetscFunctionReturn(0);
20794bbf5ea8SMatthew G. Knepley   }
20805f80ce2aSJacob Faibussowitsch   CHKERRQ(MatZeroEntries(mat));
2081fa84ea4cSLawrence Mitchell   if (patch->precomputeElementTensors) {
2082fa84ea4cSLawrence Mitchell     PetscInt           i;
2083fa84ea4cSLawrence Mitchell     PetscInt           ndof = patch->totalDofsPerCell;
2084fa84ea4cSLawrence Mitchell     const PetscScalar *elementTensors;
2085fa84ea4cSLawrence Mitchell 
20865f80ce2aSJacob Faibussowitsch     CHKERRQ(VecGetArrayRead(patch->cellMats, &elementTensors));
2087fa84ea4cSLawrence Mitchell     for (i = 0; i < ncell; i++) {
2088fa84ea4cSLawrence Mitchell       const PetscInt     cell = cellsArray[i + offset];
2089fa84ea4cSLawrence Mitchell       const PetscInt    *idx  = dofsArray + (offset + i)*ndof;
2090fe988be2SFlorian Wechsung       const PetscScalar *v    = elementTensors + patch->precomputedTensorLocations[cell]*ndof*ndof;
20915f80ce2aSJacob Faibussowitsch       CHKERRQ(MatSetValues(mat, ndof, idx, ndof, idx, v, ADD_VALUES));
2092fa84ea4cSLawrence Mitchell     }
20935f80ce2aSJacob Faibussowitsch     CHKERRQ(VecRestoreArrayRead(patch->cellMats, &elementTensors));
20945f80ce2aSJacob Faibussowitsch     CHKERRQ(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
20955f80ce2aSJacob Faibussowitsch     CHKERRQ(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
2096fa84ea4cSLawrence Mitchell   } else {
20974bbf5ea8SMatthew G. Knepley     PetscStackPush("PCPatch user callback");
20982aa6f319SMatthew G. Knepley     /* Cannot reuse the same IS because the geometry info is being cached in it */
20995f80ce2aSJacob Faibussowitsch     CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS));
21005f80ce2aSJacob Faibussowitsch     CHKERRQ(patch->usercomputeop(pc, point, x, mat, patch->cellIS, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell, dofsArrayWithAll ? dofsArrayWithAll + offset*patch->totalDofsPerCell : NULL, patch->usercomputeopctx));
2101fa84ea4cSLawrence Mitchell   }
210259109abcSLawrence Mitchell   if (patch->usercomputeopintfacet) {
21035f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(patch->intFacetCounts, point, &numIntFacets));
21045f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(patch->intFacetCounts, point, &intFacetOffset));
2105eb62eeaaSLawrence Mitchell     if (numIntFacets > 0) {
2106eb62eeaaSLawrence Mitchell       /* For each interior facet, grab the two cells (in local numbering, and concatenate dof numberings for those cells) */
2107eb62eeaaSLawrence Mitchell       PetscInt       *facetDofs = NULL, *facetDofsWithAll = NULL;
2108eb62eeaaSLawrence Mitchell       const PetscInt *intFacetsArray = NULL;
2109eb62eeaaSLawrence Mitchell       PetscInt        idx = 0;
2110eb62eeaaSLawrence Mitchell       PetscInt        i, c, d;
2111de2d1767SPatrick Farrell       PetscInt        fStart;
2112b6bb21d1SLawrence Mitchell       DM              dm, plex;
2113eb62eeaaSLawrence Mitchell       IS              facetIS = NULL;
2114eb62eeaaSLawrence Mitchell       const PetscInt *facetCells = NULL;
21157a50e09dSPatrick Farrell 
21165f80ce2aSJacob Faibussowitsch       CHKERRQ(ISGetIndices(patch->intFacetsToPatchCell, &facetCells));
21175f80ce2aSJacob Faibussowitsch       CHKERRQ(ISGetIndices(patch->intFacets, &intFacetsArray));
21185f80ce2aSJacob Faibussowitsch       CHKERRQ(PCGetDM(pc, &dm));
21195f80ce2aSJacob Faibussowitsch       CHKERRQ(DMConvert(dm, DMPLEX, &plex));
2120b6bb21d1SLawrence Mitchell       dm = plex;
21215f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetHeightStratum(dm, 1, &fStart, NULL));
2122eb62eeaaSLawrence Mitchell       /* FIXME: Pull this malloc out. */
21235f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofs));
2124eb62eeaaSLawrence Mitchell       if (dofsArrayWithAll) {
21255f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscMalloc1(2 * patch->totalDofsPerCell * numIntFacets, &facetDofsWithAll));
2126eb62eeaaSLawrence Mitchell       }
2127f98464cbSLawrence Mitchell       if (patch->precomputeElementTensors) {
2128f98464cbSLawrence Mitchell         PetscInt           nFacetDof = 2*patch->totalDofsPerCell;
2129f98464cbSLawrence Mitchell         const PetscScalar *elementTensors;
2130f98464cbSLawrence Mitchell 
21315f80ce2aSJacob Faibussowitsch         CHKERRQ(VecGetArrayRead(patch->intFacetMats, &elementTensors));
2132f98464cbSLawrence Mitchell 
2133f98464cbSLawrence Mitchell         for (i = 0; i < numIntFacets; i++) {
2134f98464cbSLawrence Mitchell           const PetscInt     facet = intFacetsArray[i + intFacetOffset];
2135de2d1767SPatrick Farrell           const PetscScalar *v     = elementTensors + patch->precomputedIntFacetTensorLocations[facet - fStart]*nFacetDof*nFacetDof;
2136f98464cbSLawrence Mitchell           idx = 0;
2137f98464cbSLawrence Mitchell           /*
2138f98464cbSLawrence Mitchell            * 0--1
2139f98464cbSLawrence Mitchell            * |\-|
2140f98464cbSLawrence Mitchell            * |+\|
2141f98464cbSLawrence Mitchell            * 2--3
2142f98464cbSLawrence Mitchell            * [0, 2, 3, 0, 1, 3]
2143f98464cbSLawrence Mitchell            */
2144f98464cbSLawrence Mitchell           for (c = 0; c < 2; c++) {
2145f98464cbSLawrence Mitchell             const PetscInt cell = facetCells[2*(intFacetOffset + i) + c];
2146f98464cbSLawrence Mitchell             for (d = 0; d < patch->totalDofsPerCell; d++) {
2147f98464cbSLawrence Mitchell               facetDofs[idx] = dofsArray[(offset + cell)*patch->totalDofsPerCell + d];
2148f98464cbSLawrence Mitchell               idx++;
2149f98464cbSLawrence Mitchell             }
2150f98464cbSLawrence Mitchell           }
21515f80ce2aSJacob Faibussowitsch           CHKERRQ(MatSetValues(mat, nFacetDof, facetDofs, nFacetDof, facetDofs, v, ADD_VALUES));
2152f98464cbSLawrence Mitchell         }
21535f80ce2aSJacob Faibussowitsch         CHKERRQ(VecRestoreArrayRead(patch->intFacetMats, &elementTensors));
2154f98464cbSLawrence Mitchell       } else {
2155eb62eeaaSLawrence Mitchell         /*
2156eb62eeaaSLawrence Mitchell          * 0--1
2157eb62eeaaSLawrence Mitchell          * |\-|
2158eb62eeaaSLawrence Mitchell          * |+\|
2159eb62eeaaSLawrence Mitchell          * 2--3
2160eb62eeaaSLawrence Mitchell          * [0, 2, 3, 0, 1, 3]
2161eb62eeaaSLawrence Mitchell          */
2162eb62eeaaSLawrence Mitchell         for (i = 0; i < numIntFacets; i++) {
2163eb62eeaaSLawrence Mitchell           for (c = 0; c < 2; c++) {
2164eb62eeaaSLawrence Mitchell             const PetscInt cell = facetCells[2*(intFacetOffset + i) + c];
2165eb62eeaaSLawrence Mitchell             for (d = 0; d < patch->totalDofsPerCell; d++) {
2166eb62eeaaSLawrence Mitchell               facetDofs[idx] = dofsArray[(offset + cell)*patch->totalDofsPerCell + d];
2167eb62eeaaSLawrence Mitchell               if (dofsArrayWithAll) {
2168eb62eeaaSLawrence Mitchell                 facetDofsWithAll[idx] = dofsArrayWithAll[(offset + cell)*patch->totalDofsPerCell + d];
2169eb62eeaaSLawrence Mitchell               }
2170eb62eeaaSLawrence Mitchell               idx++;
2171eb62eeaaSLawrence Mitchell             }
2172eb62eeaaSLawrence Mitchell           }
2173eb62eeaaSLawrence Mitchell         }
21745f80ce2aSJacob Faibussowitsch         CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numIntFacets, intFacetsArray + intFacetOffset, PETSC_USE_POINTER, &facetIS));
21755f80ce2aSJacob Faibussowitsch         CHKERRQ(patch->usercomputeopintfacet(pc, point, x, mat, facetIS, 2*numIntFacets*patch->totalDofsPerCell, facetDofs, facetDofsWithAll, patch->usercomputeopintfacetctx));
21765f80ce2aSJacob Faibussowitsch         CHKERRQ(ISDestroy(&facetIS));
2177f98464cbSLawrence Mitchell       }
21785f80ce2aSJacob Faibussowitsch       CHKERRQ(ISRestoreIndices(patch->intFacetsToPatchCell, &facetCells));
21795f80ce2aSJacob Faibussowitsch       CHKERRQ(ISRestoreIndices(patch->intFacets, &intFacetsArray));
21805f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(facetDofs));
21815f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(facetDofsWithAll));
21825f80ce2aSJacob Faibussowitsch       CHKERRQ(DMDestroy(&dm));
2183eb62eeaaSLawrence Mitchell     }
218459109abcSLawrence Mitchell   }
21856710cc29SPatrick Farrell 
21865f80ce2aSJacob Faibussowitsch   CHKERRQ(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
21875f80ce2aSJacob Faibussowitsch   CHKERRQ(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
21886710cc29SPatrick Farrell 
2189c73d2cf6SLawrence Mitchell   if (!(withArtificial || isNonlinear) && patch->denseinverse) {
2190c73d2cf6SLawrence Mitchell     MatFactorInfo info;
2191c73d2cf6SLawrence Mitchell     PetscBool     flg;
21925f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectTypeCompare((PetscObject)mat, MATSEQDENSE, &flg));
2193*28b400f6SJacob Faibussowitsch     PetscCheck(flg,PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Invalid Mat type for dense inverse");
21945f80ce2aSJacob Faibussowitsch     CHKERRQ(MatFactorInfoInitialize(&info));
21955f80ce2aSJacob Faibussowitsch     CHKERRQ(MatLUFactor(mat, NULL, NULL, &info));
21965f80ce2aSJacob Faibussowitsch     CHKERRQ(MatSeqDenseInvertFactors_Private(mat));
2197c73d2cf6SLawrence Mitchell   }
21984bbf5ea8SMatthew G. Knepley   PetscStackPop;
21995f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&patch->cellIS));
22004d04e9f1SPatrick Farrell   if (withArtificial) {
22015f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(patch->dofsWithArtificial, &dofsArray));
2202c2e6f3c0SFlorian Wechsung   } else {
22035f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(patch->dofs, &dofsArray));
2204c2e6f3c0SFlorian Wechsung   }
22054d04e9f1SPatrick Farrell   if (isNonlinear) {
22065f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll));
22074d04e9f1SPatrick Farrell   }
22085f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(patch->cells, &cellsArray));
22092aa6f319SMatthew G. Knepley   if (patch->viewMatrix) {
22102aa6f319SMatthew G. Knepley     char name[PETSC_MAX_PATH_LEN];
22112aa6f319SMatthew G. Knepley 
22125f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch matrix for Point %D", point));
22135f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetName((PetscObject) mat, name));
22145f80ce2aSJacob Faibussowitsch     CHKERRQ(ObjectView((PetscObject) mat, patch->viewerMatrix, patch->formatMatrix));
22152aa6f319SMatthew G. Knepley   }
22165f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
22174bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
22184bbf5ea8SMatthew G. Knepley }
22194bbf5ea8SMatthew G. Knepley 
2220fa84ea4cSLawrence Mitchell static PetscErrorCode MatSetValues_PCPatch_Private(Mat mat, PetscInt m, const PetscInt idxm[],
2221fa84ea4cSLawrence Mitchell                                                    PetscInt n, const PetscInt idxn[], const PetscScalar *v, InsertMode addv)
2222fa84ea4cSLawrence Mitchell {
2223fa84ea4cSLawrence Mitchell   Vec            data;
2224fa84ea4cSLawrence Mitchell   PetscScalar   *array;
2225fe988be2SFlorian Wechsung   PetscInt       bs, nz, i, j, cell;
2226fa84ea4cSLawrence Mitchell 
22275f80ce2aSJacob Faibussowitsch   CHKERRQ(MatShellGetContext(mat, &data));
22285f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetBlockSize(data, &bs));
22295f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetSize(data, &nz));
22305f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetArray(data, &array));
22312c71b3e2SJacob Faibussowitsch   PetscCheckFalse(m != n,PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Only for square insertion");
223233cbca70SPatrick Farrell   cell = (PetscInt)(idxm[0]/bs); /* use the fact that this is called once per cell */
2233fa84ea4cSLawrence Mitchell   for (i = 0; i < m; i++) {
22342c71b3e2SJacob Faibussowitsch     PetscCheckFalse(idxm[i] != idxn[i],PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONG, "Row and column indices must match!");
2235fa84ea4cSLawrence Mitchell     for (j = 0; j < n; j++) {
2236fa84ea4cSLawrence Mitchell       const PetscScalar v_ = v[i*bs + j];
2237fa84ea4cSLawrence Mitchell       /* Indexing is special to the data structure we have! */
2238fa84ea4cSLawrence Mitchell       if (addv == INSERT_VALUES) {
2239fe988be2SFlorian Wechsung         array[cell*bs*bs + i*bs + j] = v_;
2240fa84ea4cSLawrence Mitchell       } else {
2241fe988be2SFlorian Wechsung         array[cell*bs*bs + i*bs + j] += v_;
2242fa84ea4cSLawrence Mitchell       }
2243fa84ea4cSLawrence Mitchell     }
2244fa84ea4cSLawrence Mitchell   }
22455f80ce2aSJacob Faibussowitsch   CHKERRQ(VecRestoreArray(data, &array));
2246fa84ea4cSLawrence Mitchell   PetscFunctionReturn(0);
2247fa84ea4cSLawrence Mitchell }
2248fa84ea4cSLawrence Mitchell 
2249fa84ea4cSLawrence Mitchell static PetscErrorCode PCPatchPrecomputePatchTensors_Private(PC pc)
2250fa84ea4cSLawrence Mitchell {
2251fa84ea4cSLawrence Mitchell   PC_PATCH       *patch = (PC_PATCH *)pc->data;
2252fa84ea4cSLawrence Mitchell   const PetscInt *cellsArray;
2253fa84ea4cSLawrence Mitchell   PetscInt        ncell, offset;
2254fa84ea4cSLawrence Mitchell   const PetscInt *dofMapArray;
2255fa84ea4cSLawrence Mitchell   PetscInt        i, j;
2256fa84ea4cSLawrence Mitchell   IS              dofMap;
2257fa84ea4cSLawrence Mitchell   IS              cellIS;
2258fa84ea4cSLawrence Mitchell   const PetscInt  ndof  = patch->totalDofsPerCell;
2259fa84ea4cSLawrence Mitchell   PetscErrorCode  ierr;
2260fa84ea4cSLawrence Mitchell   Mat             vecMat;
2261fe988be2SFlorian Wechsung   PetscInt        cStart, cEnd;
2262fe988be2SFlorian Wechsung   DM              dm, plex;
2263fe988be2SFlorian Wechsung 
22645f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetSize(patch->cells, &ncell));
2265e9c2c94bSFlorian Wechsung   if (!ncell) { /* No cells to assemble over -> skip */
2266e9c2c94bSFlorian Wechsung     PetscFunctionReturn(0);
2267e9c2c94bSFlorian Wechsung   }
2268e9c2c94bSFlorian Wechsung 
22695f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0));
2270fa84ea4cSLawrence Mitchell 
22715f80ce2aSJacob Faibussowitsch   CHKERRQ(PCGetDM(pc, &dm));
22725f80ce2aSJacob Faibussowitsch   CHKERRQ(DMConvert(dm, DMPLEX, &plex));
2273b6bb21d1SLawrence Mitchell   dm = plex;
2274fa84ea4cSLawrence Mitchell   if (!patch->allCells) {
2275fa84ea4cSLawrence Mitchell     PetscHSetI      cells;
2276fa84ea4cSLawrence Mitchell     PetscHashIter   hi;
2277fa84ea4cSLawrence Mitchell     PetscInt        pStart, pEnd;
2278fa84ea4cSLawrence Mitchell     PetscInt        *allCells = NULL;
22795f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetICreate(&cells));
22805f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(patch->cells, &cellsArray));
22815f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd));
2282fa84ea4cSLawrence Mitchell     for (i = pStart; i < pEnd; i++) {
22835f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetDof(patch->cellCounts, i, &ncell));
22845f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetOffset(patch->cellCounts, i, &offset));
2285fa84ea4cSLawrence Mitchell       if (ncell <= 0) continue;
2286fa84ea4cSLawrence Mitchell       for (j = 0; j < ncell; j++) {
22875f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscHSetIAdd(cells, cellsArray[offset + j]));
2288fa84ea4cSLawrence Mitchell       }
2289fa84ea4cSLawrence Mitchell     }
22905f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(patch->cells, &cellsArray));
22915f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetIGetSize(cells, &ncell));
22925f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(ncell, &allCells));
22935f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
22945f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(cEnd-cStart, &patch->precomputedTensorLocations));
2295fa84ea4cSLawrence Mitchell     i = 0;
2296fa84ea4cSLawrence Mitchell     PetscHashIterBegin(cells, hi);
2297fa84ea4cSLawrence Mitchell     while (!PetscHashIterAtEnd(cells, hi)) {
2298fe988be2SFlorian Wechsung       PetscHashIterGetKey(cells, hi, allCells[i]);
2299fe988be2SFlorian Wechsung       patch->precomputedTensorLocations[allCells[i]] = i;
2300fa84ea4cSLawrence Mitchell       PetscHashIterNext(cells, hi);
2301fe988be2SFlorian Wechsung       i++;
2302fa84ea4cSLawrence Mitchell     }
23035f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetIDestroy(&cells));
23045f80ce2aSJacob Faibussowitsch     CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, ncell, allCells, PETSC_OWN_POINTER, &patch->allCells));
2305fa84ea4cSLawrence Mitchell   }
23065f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetSize(patch->allCells, &ncell));
2307fa84ea4cSLawrence Mitchell   if (!patch->cellMats) {
23085f80ce2aSJacob Faibussowitsch     CHKERRQ(VecCreateSeq(PETSC_COMM_SELF, ncell*ndof*ndof, &patch->cellMats));
23095f80ce2aSJacob Faibussowitsch     CHKERRQ(VecSetBlockSize(patch->cellMats, ndof));
2310fa84ea4cSLawrence Mitchell   }
23115f80ce2aSJacob Faibussowitsch   CHKERRQ(VecSet(patch->cellMats, 0));
2312fa84ea4cSLawrence Mitchell 
2313fa84ea4cSLawrence Mitchell   ierr = MatCreateShell(PETSC_COMM_SELF, ncell*ndof, ncell*ndof, ncell*ndof, ncell*ndof,
231489d5b078SSatish Balay                         (void*)patch->cellMats, &vecMat);CHKERRQ(ierr);
23155f80ce2aSJacob Faibussowitsch   CHKERRQ(MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void(*)(void))&MatSetValues_PCPatch_Private));
23165f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetSize(patch->allCells, &ncell));
23175f80ce2aSJacob Faibussowitsch   CHKERRQ(ISCreateStride(PETSC_COMM_SELF, ndof*ncell, 0, 1, &dofMap));
23185f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetIndices(dofMap, &dofMapArray));
23195f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetIndices(patch->allCells, &cellsArray));
23205f80ce2aSJacob Faibussowitsch   CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray, PETSC_USE_POINTER, &cellIS));
2321fa84ea4cSLawrence Mitchell   PetscStackPush("PCPatch user callback");
2322fa84ea4cSLawrence Mitchell   /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */
23235f80ce2aSJacob Faibussowitsch   CHKERRQ(patch->usercomputeop(pc, -1, NULL, vecMat, cellIS, ndof*ncell, dofMapArray, NULL, patch->usercomputeopctx));
2324f98464cbSLawrence Mitchell   PetscStackPop;
23255f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&cellIS));
23265f80ce2aSJacob Faibussowitsch   CHKERRQ(MatDestroy(&vecMat));
23275f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(patch->allCells, &cellsArray));
23285f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(dofMap, &dofMapArray));
23295f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&dofMap));
2330f98464cbSLawrence Mitchell 
2331f98464cbSLawrence Mitchell   if (patch->usercomputeopintfacet) {
2332f98464cbSLawrence Mitchell     PetscInt nIntFacets;
2333f98464cbSLawrence Mitchell     IS       intFacetsIS;
2334f98464cbSLawrence Mitchell     const PetscInt *intFacetsArray = NULL;
2335f98464cbSLawrence Mitchell     if (!patch->allIntFacets) {
2336f98464cbSLawrence Mitchell       PetscHSetI      facets;
2337f98464cbSLawrence Mitchell       PetscHashIter   hi;
2338f98464cbSLawrence Mitchell       PetscInt        pStart, pEnd, fStart, fEnd;
2339f98464cbSLawrence Mitchell       PetscInt        *allIntFacets = NULL;
23405f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscHSetICreate(&facets));
23415f80ce2aSJacob Faibussowitsch       CHKERRQ(ISGetIndices(patch->intFacets, &intFacetsArray));
23425f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetChart(patch->intFacetCounts, &pStart, &pEnd));
23435f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
2344f98464cbSLawrence Mitchell       for (i = pStart; i < pEnd; i++) {
23455f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetDof(patch->intFacetCounts, i, &nIntFacets));
23465f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetOffset(patch->intFacetCounts, i, &offset));
2347f98464cbSLawrence Mitchell         if (nIntFacets <= 0) continue;
2348f98464cbSLawrence Mitchell         for (j = 0; j < nIntFacets; j++) {
23495f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscHSetIAdd(facets, intFacetsArray[offset + j]));
2350f98464cbSLawrence Mitchell         }
2351f98464cbSLawrence Mitchell       }
23525f80ce2aSJacob Faibussowitsch       CHKERRQ(ISRestoreIndices(patch->intFacets, &intFacetsArray));
23535f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscHSetIGetSize(facets, &nIntFacets));
23545f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscMalloc1(nIntFacets, &allIntFacets));
23555f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscMalloc1(fEnd-fStart, &patch->precomputedIntFacetTensorLocations));
2356f98464cbSLawrence Mitchell       i = 0;
2357f98464cbSLawrence Mitchell       PetscHashIterBegin(facets, hi);
2358f98464cbSLawrence Mitchell       while (!PetscHashIterAtEnd(facets, hi)) {
2359f98464cbSLawrence Mitchell         PetscHashIterGetKey(facets, hi, allIntFacets[i]);
2360de2d1767SPatrick Farrell         patch->precomputedIntFacetTensorLocations[allIntFacets[i] - fStart] = i;
2361f98464cbSLawrence Mitchell         PetscHashIterNext(facets, hi);
2362f98464cbSLawrence Mitchell         i++;
2363f98464cbSLawrence Mitchell       }
23645f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscHSetIDestroy(&facets));
23655f80ce2aSJacob Faibussowitsch       CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, allIntFacets, PETSC_OWN_POINTER, &patch->allIntFacets));
2366f98464cbSLawrence Mitchell     }
23675f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetSize(patch->allIntFacets, &nIntFacets));
2368f98464cbSLawrence Mitchell     if (!patch->intFacetMats) {
23695f80ce2aSJacob Faibussowitsch       CHKERRQ(VecCreateSeq(PETSC_COMM_SELF, nIntFacets*ndof*ndof*4, &patch->intFacetMats));
23705f80ce2aSJacob Faibussowitsch       CHKERRQ(VecSetBlockSize(patch->intFacetMats, ndof*2));
2371f98464cbSLawrence Mitchell     }
23725f80ce2aSJacob Faibussowitsch     CHKERRQ(VecSet(patch->intFacetMats, 0));
2373f98464cbSLawrence Mitchell 
2374f98464cbSLawrence Mitchell     ierr = MatCreateShell(PETSC_COMM_SELF, nIntFacets*ndof*2, nIntFacets*ndof*2, nIntFacets*ndof*2, nIntFacets*ndof*2,
237589d5b078SSatish Balay                           (void*)patch->intFacetMats, &vecMat);CHKERRQ(ierr);
23765f80ce2aSJacob Faibussowitsch     CHKERRQ(MatShellSetOperation(vecMat, MATOP_SET_VALUES, (void(*)(void))&MatSetValues_PCPatch_Private));
23775f80ce2aSJacob Faibussowitsch     CHKERRQ(ISCreateStride(PETSC_COMM_SELF, 2*ndof*nIntFacets, 0, 1, &dofMap));
23785f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(dofMap, &dofMapArray));
23795f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(patch->allIntFacets, &intFacetsArray));
23805f80ce2aSJacob Faibussowitsch     CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, nIntFacets, intFacetsArray, PETSC_USE_POINTER, &intFacetsIS));
2381f98464cbSLawrence Mitchell     PetscStackPush("PCPatch user callback (interior facets)");
2382f98464cbSLawrence Mitchell     /* TODO: Fix for DMPlex compute op, this bypasses a lot of the machinery and just assembles every element tensor. */
23835f80ce2aSJacob Faibussowitsch     CHKERRQ(patch->usercomputeopintfacet(pc, -1, NULL, vecMat, intFacetsIS, 2*ndof*nIntFacets, dofMapArray, NULL, patch->usercomputeopintfacetctx));
2384f98464cbSLawrence Mitchell     PetscStackPop;
23855f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&intFacetsIS));
23865f80ce2aSJacob Faibussowitsch     CHKERRQ(MatDestroy(&vecMat));
23875f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(patch->allIntFacets, &intFacetsArray));
23885f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(dofMap, &dofMapArray));
23895f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&dofMap));
2390f98464cbSLawrence Mitchell   }
23915f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&dm));
23925f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0));
2393fa84ea4cSLawrence Mitchell 
2394fa84ea4cSLawrence Mitchell   PetscFunctionReturn(0);
2395fa84ea4cSLawrence Mitchell }
2396fa84ea4cSLawrence Mitchell 
23970904074fSPatrick Farrell PetscErrorCode PCPatch_ScatterLocal_Private(PC pc, PetscInt p, Vec x, Vec y, InsertMode mode, ScatterMode scat, PatchScatterType scattertype)
23984bbf5ea8SMatthew G. Knepley {
23994bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch     = (PC_PATCH *) pc->data;
24004bbf5ea8SMatthew G. Knepley   const PetscScalar *xArray    = NULL;
24014bbf5ea8SMatthew G. Knepley   PetscScalar       *yArray    = NULL;
24024bbf5ea8SMatthew G. Knepley   const PetscInt    *gtolArray = NULL;
24034bbf5ea8SMatthew G. Knepley   PetscInt           dof, offset, lidx;
24044bbf5ea8SMatthew G. Knepley 
24054bbf5ea8SMatthew G. Knepley   PetscFunctionBeginHot;
24065f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetArrayRead(x, &xArray));
24075f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetArray(y, &yArray));
24080904074fSPatrick Farrell   if (scattertype == SCATTER_WITHARTIFICIAL) {
24095f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof));
24105f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offset));
24115f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(patch->gtolWithArtificial, &gtolArray));
24120904074fSPatrick Farrell   } else if (scattertype == SCATTER_WITHALL) {
24135f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(patch->gtolCountsWithAll, p, &dof));
24145f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offset));
24155f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(patch->gtolWithAll, &gtolArray));
2416c2e6f3c0SFlorian Wechsung   } else {
24175f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(patch->gtolCounts, p, &dof));
24185f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(patch->gtolCounts, p, &offset));
24195f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(patch->gtol, &gtolArray));
2420c2e6f3c0SFlorian Wechsung   }
24212c71b3e2SJacob Faibussowitsch   PetscCheckFalse(mode == INSERT_VALUES && scat != SCATTER_FORWARD,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't insert if not scattering forward");
24222c71b3e2SJacob Faibussowitsch   PetscCheckFalse(mode == ADD_VALUES    && scat != SCATTER_REVERSE,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't add if not scattering reverse");
24234bbf5ea8SMatthew G. Knepley   for (lidx = 0; lidx < dof; ++lidx) {
24244bbf5ea8SMatthew G. Knepley     const PetscInt gidx = gtolArray[offset+lidx];
24254bbf5ea8SMatthew G. Knepley 
24264bbf5ea8SMatthew G. Knepley     if (mode == INSERT_VALUES) yArray[lidx]  = xArray[gidx]; /* Forward */
24274bbf5ea8SMatthew G. Knepley     else                       yArray[gidx] += xArray[lidx]; /* Reverse */
24284bbf5ea8SMatthew G. Knepley   }
24290904074fSPatrick Farrell   if (scattertype == SCATTER_WITHARTIFICIAL) {
24305f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(patch->gtolWithArtificial, &gtolArray));
24310904074fSPatrick Farrell   } else if (scattertype == SCATTER_WITHALL) {
24325f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(patch->gtolWithAll, &gtolArray));
2433c2e6f3c0SFlorian Wechsung   } else {
24345f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(patch->gtol, &gtolArray));
2435c2e6f3c0SFlorian Wechsung   }
24365f80ce2aSJacob Faibussowitsch   CHKERRQ(VecRestoreArrayRead(x, &xArray));
24375f80ce2aSJacob Faibussowitsch   CHKERRQ(VecRestoreArray(y, &yArray));
24384bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
24394bbf5ea8SMatthew G. Knepley }
24404bbf5ea8SMatthew G. Knepley 
2441dadc69c5SMatthew G. Knepley static PetscErrorCode PCSetUp_PATCH_Linear(PC pc)
2442dadc69c5SMatthew G. Knepley {
2443dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2444dadc69c5SMatthew G. Knepley   const char    *prefix;
2445dadc69c5SMatthew G. Knepley   PetscInt       i;
2446dadc69c5SMatthew G. Knepley 
2447dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2448dadc69c5SMatthew G. Knepley   if (!pc->setupcalled) {
24492c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!patch->save_operators && patch->denseinverse,PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Can't have dense inverse without save operators");
2450c73d2cf6SLawrence Mitchell     if (!patch->denseinverse) {
24515f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscMalloc1(patch->npatch, &patch->solver));
24525f80ce2aSJacob Faibussowitsch       CHKERRQ(PCGetOptionsPrefix(pc, &prefix));
2453dadc69c5SMatthew G. Knepley       for (i = 0; i < patch->npatch; ++i) {
2454dadc69c5SMatthew G. Knepley         KSP ksp;
2455dadc69c5SMatthew G. Knepley         PC  subpc;
2456dadc69c5SMatthew G. Knepley 
24575f80ce2aSJacob Faibussowitsch         CHKERRQ(KSPCreate(PETSC_COMM_SELF, &ksp));
24585f80ce2aSJacob Faibussowitsch         CHKERRQ(KSPSetErrorIfNotConverged(ksp, pc->erroriffailure));
24595f80ce2aSJacob Faibussowitsch         CHKERRQ(KSPSetOptionsPrefix(ksp, prefix));
24605f80ce2aSJacob Faibussowitsch         CHKERRQ(KSPAppendOptionsPrefix(ksp, "sub_"));
24615f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscObjectIncrementTabLevel((PetscObject) ksp, (PetscObject) pc, 1));
24625f80ce2aSJacob Faibussowitsch         CHKERRQ(KSPGetPC(ksp, &subpc));
24635f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscObjectIncrementTabLevel((PetscObject) subpc, (PetscObject) pc, 1));
24645f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscLogObjectParent((PetscObject) pc, (PetscObject) ksp));
2465dadc69c5SMatthew G. Knepley         patch->solver[i] = (PetscObject) ksp;
2466dadc69c5SMatthew G. Knepley       }
2467dadc69c5SMatthew G. Knepley     }
2468c73d2cf6SLawrence Mitchell   }
2469dadc69c5SMatthew G. Knepley   if (patch->save_operators) {
2470e9c2c94bSFlorian Wechsung     if (patch->precomputeElementTensors) {
24715f80ce2aSJacob Faibussowitsch       CHKERRQ(PCPatchPrecomputePatchTensors_Private(pc));
2472fe988be2SFlorian Wechsung     }
2473dadc69c5SMatthew G. Knepley     for (i = 0; i < patch->npatch; ++i) {
24745f80ce2aSJacob Faibussowitsch       CHKERRQ(PCPatchComputeOperator_Internal(pc, NULL, patch->mat[i], i, PETSC_FALSE));
2475c73d2cf6SLawrence Mitchell       if (!patch->denseinverse) {
24765f80ce2aSJacob Faibussowitsch         CHKERRQ(KSPSetOperators((KSP) patch->solver[i], patch->mat[i], patch->mat[i]));
24779d4fc724SLawrence Mitchell       } else if (patch->mat[i] && !patch->densesolve) {
24789d4fc724SLawrence Mitchell         /* Setup matmult callback */
24795f80ce2aSJacob Faibussowitsch         CHKERRQ(MatGetOperation(patch->mat[i], MATOP_MULT, (void (**)(void))&patch->densesolve));
2480dadc69c5SMatthew G. Knepley       }
2481dadc69c5SMatthew G. Knepley     }
2482c73d2cf6SLawrence Mitchell   }
248334d8b122SPatrick Farrell   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
248434d8b122SPatrick Farrell     for (i = 0; i < patch->npatch; ++i) {
24851202d238SPatrick Farrell       /* Instead of padding patch->patchUpdate with zeros to get */
24861202d238SPatrick Farrell       /* patch->patchUpdateWithArtificial and then multiplying with the matrix, */
248734d8b122SPatrick Farrell       /* just get rid of the columns that correspond to the dofs with */
248834d8b122SPatrick Farrell       /* artificial bcs. That's of course fairly inefficient, hopefully we */
248934d8b122SPatrick Farrell       /* can just assemble the rectangular matrix in the first place. */
249034d8b122SPatrick Farrell       Mat matSquare;
249134d8b122SPatrick Farrell       IS rowis;
249234d8b122SPatrick Farrell       PetscInt dof;
249334d8b122SPatrick Farrell 
24945f80ce2aSJacob Faibussowitsch       CHKERRQ(MatGetSize(patch->mat[i], &dof, NULL));
249534d8b122SPatrick Farrell       if (dof == 0) {
249634d8b122SPatrick Farrell         patch->matWithArtificial[i] = NULL;
249734d8b122SPatrick Farrell         continue;
249834d8b122SPatrick Farrell       }
249934d8b122SPatrick Farrell 
25005f80ce2aSJacob Faibussowitsch       CHKERRQ(PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE));
25015f80ce2aSJacob Faibussowitsch       CHKERRQ(PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE));
250234d8b122SPatrick Farrell 
25035f80ce2aSJacob Faibussowitsch       CHKERRQ(MatGetSize(matSquare, &dof, NULL));
25045f80ce2aSJacob Faibussowitsch       CHKERRQ(ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis));
250534d8b122SPatrick Farrell       if (pc->setupcalled) {
25065f80ce2aSJacob Faibussowitsch         CHKERRQ(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_REUSE_MATRIX, &patch->matWithArtificial[i]));
250734d8b122SPatrick Farrell       } else {
25085f80ce2aSJacob Faibussowitsch         CHKERRQ(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &patch->matWithArtificial[i]));
250934d8b122SPatrick Farrell       }
25105f80ce2aSJacob Faibussowitsch       CHKERRQ(ISDestroy(&rowis));
25115f80ce2aSJacob Faibussowitsch       CHKERRQ(MatDestroy(&matSquare));
251234d8b122SPatrick Farrell     }
251334d8b122SPatrick Farrell   }
2514dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
2515dadc69c5SMatthew G. Knepley }
2516dadc69c5SMatthew G. Knepley 
25174bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUp_PATCH(PC pc)
25184bbf5ea8SMatthew G. Knepley {
25194bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2520557beb66SLawrence Mitchell   PetscInt       i;
252139fd2e8aSPatrick Farrell   PetscBool      isNonlinear;
25229d4fc724SLawrence Mitchell   PetscInt       maxDof = -1, maxDofWithArtificial = -1;
25234bbf5ea8SMatthew G. Knepley 
25244bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
25254bbf5ea8SMatthew G. Knepley   if (!pc->setupcalled) {
25264bbf5ea8SMatthew G. Knepley     PetscInt pStart, pEnd, p;
25274bbf5ea8SMatthew G. Knepley     PetscInt localSize;
25284bbf5ea8SMatthew G. Knepley 
25295f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscLogEventBegin(PC_Patch_CreatePatches, pc, 0, 0, 0));
25304bbf5ea8SMatthew G. Knepley 
2531debbdec3SPatrick Farrell     isNonlinear = patch->isNonlinear;
25325f824522SMatthew G. Knepley     if (!patch->nsubspaces) {
2533b6bb21d1SLawrence Mitchell       DM           dm, plex;
25345f824522SMatthew G. Knepley       PetscSection s;
2535bd026e97SJed Brown       PetscInt     cStart, cEnd, c, Nf, f, numGlobalBcs = 0, *globalBcs, *Nb, **cellDofs;
25365f824522SMatthew G. Knepley 
25375f80ce2aSJacob Faibussowitsch       CHKERRQ(PCGetDM(pc, &dm));
2538*28b400f6SJacob Faibussowitsch       PetscCheck(dm,PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONG, "Must set DM for PCPATCH or call PCPatchSetDiscretisationInfo()");
25395f80ce2aSJacob Faibussowitsch       CHKERRQ(DMConvert(dm, DMPLEX, &plex));
2540b6bb21d1SLawrence Mitchell       dm = plex;
25415f80ce2aSJacob Faibussowitsch       CHKERRQ(DMGetLocalSection(dm, &s));
25425f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetNumFields(s, &Nf));
25435f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetChart(s, &pStart, &pEnd));
25445f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
25455f824522SMatthew G. Knepley         PetscInt cdof;
25465f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetConstraintDof(s, p, &cdof));
25475f824522SMatthew G. Knepley         numGlobalBcs += cdof;
25485f824522SMatthew G. Knepley       }
25495f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
25505f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscMalloc3(Nf, &Nb, Nf, &cellDofs, numGlobalBcs, &globalBcs));
25515f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
25525f824522SMatthew G. Knepley         PetscFE        fe;
25535f824522SMatthew G. Knepley         PetscDualSpace sp;
25545f824522SMatthew G. Knepley         PetscInt       cdoff = 0;
25555f824522SMatthew G. Knepley 
25565f80ce2aSJacob Faibussowitsch         CHKERRQ(DMGetField(dm, f, NULL, (PetscObject *) &fe));
25575f80ce2aSJacob Faibussowitsch         /* CHKERRQ(PetscFEGetNumComponents(fe, &Nc[f])); */
25585f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscFEGetDualSpace(fe, &sp));
25595f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscDualSpaceGetDimension(sp, &Nb[f]));
25605f824522SMatthew G. Knepley 
25615f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscMalloc1((cEnd-cStart)*Nb[f], &cellDofs[f]));
25625f824522SMatthew G. Knepley         for (c = cStart; c < cEnd; ++c) {
25635f824522SMatthew G. Knepley           PetscInt *closure = NULL;
25645f824522SMatthew G. Knepley           PetscInt  clSize  = 0, cl;
25655f824522SMatthew G. Knepley 
25665f80ce2aSJacob Faibussowitsch           CHKERRQ(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure));
25675f824522SMatthew G. Knepley           for (cl = 0; cl < clSize*2; cl += 2) {
25685f824522SMatthew G. Knepley             const PetscInt p = closure[cl];
25695f824522SMatthew G. Knepley             PetscInt       fdof, d, foff;
25705f824522SMatthew G. Knepley 
25715f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscSectionGetFieldDof(s, p, f, &fdof));
25725f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscSectionGetFieldOffset(s, p, f, &foff));
25735f824522SMatthew G. Knepley             for (d = 0; d < fdof; ++d, ++cdoff) cellDofs[f][cdoff] = foff + d;
25745f824522SMatthew G. Knepley           }
25755f80ce2aSJacob Faibussowitsch           CHKERRQ(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure));
25765f824522SMatthew G. Knepley         }
25772c71b3e2SJacob Faibussowitsch         PetscCheckFalse(cdoff != (cEnd-cStart)*Nb[f],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]);
25785f824522SMatthew G. Knepley       }
25795f824522SMatthew G. Knepley       numGlobalBcs = 0;
25805f824522SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
25815f824522SMatthew G. Knepley         const PetscInt *ind;
25825f824522SMatthew G. Knepley         PetscInt        off, cdof, d;
25835f824522SMatthew G. Knepley 
25845f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetOffset(s, p, &off));
25855f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetConstraintDof(s, p, &cdof));
25865f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetConstraintIndices(s, p, &ind));
25875f824522SMatthew G. Knepley         for (d = 0; d < cdof; ++d) globalBcs[numGlobalBcs++] = off + ind[d];
25885f824522SMatthew G. Knepley       }
25895f824522SMatthew G. Knepley 
25905f80ce2aSJacob Faibussowitsch       CHKERRQ(PCPatchSetDiscretisationInfoCombined(pc, dm, Nb, (const PetscInt **) cellDofs, numGlobalBcs, globalBcs, numGlobalBcs, globalBcs));
25915f824522SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
25925f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscFree(cellDofs[f]));
25935f824522SMatthew G. Knepley       }
25945f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree3(Nb, cellDofs, globalBcs));
25955f80ce2aSJacob Faibussowitsch       CHKERRQ(PCPatchSetComputeFunction(pc, PCPatchComputeFunction_DMPlex_Private, NULL));
25965f80ce2aSJacob Faibussowitsch       CHKERRQ(PCPatchSetComputeOperator(pc, PCPatchComputeOperator_DMPlex_Private, NULL));
25975f80ce2aSJacob Faibussowitsch       CHKERRQ(DMDestroy(&dm));
25985f824522SMatthew G. Knepley     }
25995f824522SMatthew G. Knepley 
26004bbf5ea8SMatthew G. Knepley     localSize = patch->subspaceOffsets[patch->nsubspaces];
26015f80ce2aSJacob Faibussowitsch     CHKERRQ(VecCreateSeq(PETSC_COMM_SELF, localSize, &patch->localRHS));
26025f80ce2aSJacob Faibussowitsch     CHKERRQ(VecSetUp(patch->localRHS));
26035f80ce2aSJacob Faibussowitsch     CHKERRQ(VecDuplicate(patch->localRHS, &patch->localUpdate));
26045f80ce2aSJacob Faibussowitsch     CHKERRQ(PCPatchCreateCellPatches(pc));
26055f80ce2aSJacob Faibussowitsch     CHKERRQ(PCPatchCreateCellPatchDiscretisationInfo(pc));
26064bbf5ea8SMatthew G. Knepley 
26074bbf5ea8SMatthew G. Knepley     /* OK, now build the work vectors */
26085f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetChart(patch->gtolCounts, &pStart, &pEnd));
2609c2e6f3c0SFlorian Wechsung 
261061c4b389SFlorian Wechsung     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
26115f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithArtificial));
2612c2e6f3c0SFlorian Wechsung     }
26130904074fSPatrick Farrell     if (isNonlinear) {
26145f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithAll));
26150904074fSPatrick Farrell     }
26164bbf5ea8SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26174bbf5ea8SMatthew G. Knepley       PetscInt dof;
26184bbf5ea8SMatthew G. Knepley 
26195f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetDof(patch->gtolCounts, p, &dof));
26202f613bf5SBarry Smith       maxDof = PetscMax(maxDof, dof);
26210904074fSPatrick Farrell       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
26223bb0e8f7SKarl Rupp         const PetscInt    *gtolArray, *gtolArrayWithArtificial = NULL;
26233bb0e8f7SKarl Rupp         PetscInt           numPatchDofs, offset;
26243bb0e8f7SKarl Rupp         PetscInt           numPatchDofsWithArtificial, offsetWithArtificial;
26253bb0e8f7SKarl Rupp         PetscInt           dofWithoutArtificialCounter = 0;
26263bb0e8f7SKarl Rupp         PetscInt          *patchWithoutArtificialToWithArtificialArray;
26273bb0e8f7SKarl Rupp 
26285f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof));
26299d4fc724SLawrence Mitchell         maxDofWithArtificial = PetscMax(maxDofWithArtificial, dof);
2630c2e6f3c0SFlorian Wechsung 
2631e047a90bSFlorian Wechsung         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
2632e047a90bSFlorian Wechsung         /* the index in the patch with all dofs */
26335f80ce2aSJacob Faibussowitsch         CHKERRQ(ISGetIndices(patch->gtol, &gtolArray));
263463deea8eSPatrick Farrell 
26355f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs));
263647aca4a6SPatrick Farrell         if (numPatchDofs == 0) {
263747aca4a6SPatrick Farrell           patch->dofMappingWithoutToWithArtificial[p-pStart] = NULL;
263847aca4a6SPatrick Farrell           continue;
263947aca4a6SPatrick Farrell         }
264063deea8eSPatrick Farrell 
26415f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetOffset(patch->gtolCounts, p, &offset));
26425f80ce2aSJacob Faibussowitsch         CHKERRQ(ISGetIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial));
26435f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &numPatchDofsWithArtificial));
26445f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offsetWithArtificial));
2645c2e6f3c0SFlorian Wechsung 
26465f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscMalloc1(numPatchDofs, &patchWithoutArtificialToWithArtificialArray));
2647b0c21b6aSKarl Rupp         for (i=0; i<numPatchDofsWithArtificial; i++) {
2648e047a90bSFlorian Wechsung           if (gtolArrayWithArtificial[i+offsetWithArtificial] == gtolArray[offset+dofWithoutArtificialCounter]) {
2649c2e6f3c0SFlorian Wechsung             patchWithoutArtificialToWithArtificialArray[dofWithoutArtificialCounter] = i;
2650c2e6f3c0SFlorian Wechsung             dofWithoutArtificialCounter++;
2651c2e6f3c0SFlorian Wechsung             if (dofWithoutArtificialCounter == numPatchDofs)
2652c2e6f3c0SFlorian Wechsung               break;
2653c2e6f3c0SFlorian Wechsung           }
2654c2e6f3c0SFlorian Wechsung         }
26555f80ce2aSJacob Faibussowitsch         CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutArtificialToWithArtificialArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithArtificial[p-pStart]));
26565f80ce2aSJacob Faibussowitsch         CHKERRQ(ISRestoreIndices(patch->gtol, &gtolArray));
26575f80ce2aSJacob Faibussowitsch         CHKERRQ(ISRestoreIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial));
2658c2e6f3c0SFlorian Wechsung       }
26590904074fSPatrick Farrell       if (isNonlinear) {
26600904074fSPatrick Farrell         const PetscInt    *gtolArray, *gtolArrayWithAll = NULL;
26610904074fSPatrick Farrell         PetscInt           numPatchDofs, offset;
26620904074fSPatrick Farrell         PetscInt           numPatchDofsWithAll, offsetWithAll;
26630904074fSPatrick Farrell         PetscInt           dofWithoutAllCounter = 0;
26640904074fSPatrick Farrell         PetscInt          *patchWithoutAllToWithAllArray;
26650904074fSPatrick Farrell 
26660904074fSPatrick Farrell         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
26670904074fSPatrick Farrell         /* the index in the patch with all dofs */
26685f80ce2aSJacob Faibussowitsch         CHKERRQ(ISGetIndices(patch->gtol, &gtolArray));
26690904074fSPatrick Farrell 
26705f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs));
267147aca4a6SPatrick Farrell         if (numPatchDofs == 0) {
2672b88cb22dSPatrick Farrell           patch->dofMappingWithoutToWithAll[p-pStart] = NULL;
267347aca4a6SPatrick Farrell           continue;
267447aca4a6SPatrick Farrell         }
26750904074fSPatrick Farrell 
26765f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetOffset(patch->gtolCounts, p, &offset));
26775f80ce2aSJacob Faibussowitsch         CHKERRQ(ISGetIndices(patch->gtolWithAll, &gtolArrayWithAll));
26785f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetDof(patch->gtolCountsWithAll, p, &numPatchDofsWithAll));
26795f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offsetWithAll));
26800904074fSPatrick Farrell 
26815f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscMalloc1(numPatchDofs, &patchWithoutAllToWithAllArray));
26820904074fSPatrick Farrell 
26830904074fSPatrick Farrell         for (i=0; i<numPatchDofsWithAll; i++) {
26840904074fSPatrick Farrell           if (gtolArrayWithAll[i+offsetWithAll] == gtolArray[offset+dofWithoutAllCounter]) {
26850904074fSPatrick Farrell             patchWithoutAllToWithAllArray[dofWithoutAllCounter] = i;
26860904074fSPatrick Farrell             dofWithoutAllCounter++;
26870904074fSPatrick Farrell             if (dofWithoutAllCounter == numPatchDofs)
26880904074fSPatrick Farrell               break;
26890904074fSPatrick Farrell           }
26900904074fSPatrick Farrell         }
26915f80ce2aSJacob Faibussowitsch         CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutAllToWithAllArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithAll[p-pStart]));
26925f80ce2aSJacob Faibussowitsch         CHKERRQ(ISRestoreIndices(patch->gtol, &gtolArray));
26935f80ce2aSJacob Faibussowitsch         CHKERRQ(ISRestoreIndices(patch->gtolWithAll, &gtolArrayWithAll));
26940904074fSPatrick Farrell       }
26954bbf5ea8SMatthew G. Knepley     }
269660dd46caSLawrence Mitchell     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
26975f80ce2aSJacob Faibussowitsch       CHKERRQ(VecCreateSeq(PETSC_COMM_SELF, maxDofWithArtificial, &patch->patchRHSWithArtificial));
26985f80ce2aSJacob Faibussowitsch       CHKERRQ(VecSetUp(patch->patchRHSWithArtificial));
269960dd46caSLawrence Mitchell     }
27005f80ce2aSJacob Faibussowitsch     CHKERRQ(VecCreateSeq(PETSC_COMM_SELF, maxDof, &patch->patchRHS));
27015f80ce2aSJacob Faibussowitsch     CHKERRQ(VecSetUp(patch->patchRHS));
27025f80ce2aSJacob Faibussowitsch     CHKERRQ(VecCreateSeq(PETSC_COMM_SELF, maxDof, &patch->patchUpdate));
27035f80ce2aSJacob Faibussowitsch     CHKERRQ(VecSetUp(patch->patchUpdate));
27044bbf5ea8SMatthew G. Knepley     if (patch->save_operators) {
27055f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscMalloc1(patch->npatch, &patch->mat));
27064bbf5ea8SMatthew G. Knepley       for (i = 0; i < patch->npatch; ++i) {
27075f80ce2aSJacob Faibussowitsch         CHKERRQ(PCPatchCreateMatrix_Private(pc, i, &patch->mat[i], PETSC_FALSE));
27084bbf5ea8SMatthew G. Knepley       }
27094bbf5ea8SMatthew G. Knepley     }
27105f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscLogEventEnd(PC_Patch_CreatePatches, pc, 0, 0, 0));
27114bbf5ea8SMatthew G. Knepley 
27124bbf5ea8SMatthew G. Knepley     /* If desired, calculate weights for dof multiplicity */
27134bbf5ea8SMatthew G. Knepley     if (patch->partition_of_unity) {
27143bb0e8f7SKarl Rupp       PetscScalar *input = NULL;
27153bb0e8f7SKarl Rupp       PetscScalar *output = NULL;
27163bb0e8f7SKarl Rupp       Vec         global;
27173bb0e8f7SKarl Rupp 
27185f80ce2aSJacob Faibussowitsch       CHKERRQ(VecDuplicate(patch->localRHS, &patch->dof_weights));
271961c4b389SFlorian Wechsung       if (patch->local_composition_type == PC_COMPOSITE_ADDITIVE) {
27204bbf5ea8SMatthew G. Knepley         for (i = 0; i < patch->npatch; ++i) {
27214bbf5ea8SMatthew G. Knepley           PetscInt dof;
27224bbf5ea8SMatthew G. Knepley 
27235f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionGetDof(patch->gtolCounts, i+pStart, &dof));
27244bbf5ea8SMatthew G. Knepley           if (dof <= 0) continue;
27255f80ce2aSJacob Faibussowitsch           CHKERRQ(VecSet(patch->patchRHS, 1.0));
27265f80ce2aSJacob Faibussowitsch           CHKERRQ(PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchRHS, patch->dof_weights, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR));
27274bbf5ea8SMatthew G. Knepley         }
2728c2e6f3c0SFlorian Wechsung       } else {
2729e047a90bSFlorian Wechsung         /* multiplicative is actually only locally multiplicative and globally additive. need the pou where the mesh decomposition overlaps */
27305f80ce2aSJacob Faibussowitsch         CHKERRQ(VecSet(patch->dof_weights, 1.0));
27314bbf5ea8SMatthew G. Knepley       }
2732d132cafaSFlorian Wechsung 
2733d132cafaSFlorian Wechsung       VecDuplicate(patch->dof_weights, &global);
2734d132cafaSFlorian Wechsung       VecSet(global, 0.);
2735d132cafaSFlorian Wechsung 
27365f80ce2aSJacob Faibussowitsch       CHKERRQ(VecGetArray(patch->dof_weights, &input));
27375f80ce2aSJacob Faibussowitsch       CHKERRQ(VecGetArray(global, &output));
27385f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSFReduceBegin(patch->sectionSF, MPIU_SCALAR, input, output, MPI_SUM));
27395f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSFReduceEnd(patch->sectionSF, MPIU_SCALAR, input, output, MPI_SUM));
27405f80ce2aSJacob Faibussowitsch       CHKERRQ(VecRestoreArray(patch->dof_weights, &input));
27415f80ce2aSJacob Faibussowitsch       CHKERRQ(VecRestoreArray(global, &output));
2742d132cafaSFlorian Wechsung 
27435f80ce2aSJacob Faibussowitsch       CHKERRQ(VecReciprocal(global));
2744d132cafaSFlorian Wechsung 
27455f80ce2aSJacob Faibussowitsch       CHKERRQ(VecGetArray(patch->dof_weights, &output));
27465f80ce2aSJacob Faibussowitsch       CHKERRQ(VecGetArray(global, &input));
27475f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSFBcastBegin(patch->sectionSF, MPIU_SCALAR, input, output,MPI_REPLACE));
27485f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSFBcastEnd(patch->sectionSF, MPIU_SCALAR, input, output,MPI_REPLACE));
27495f80ce2aSJacob Faibussowitsch       CHKERRQ(VecRestoreArray(patch->dof_weights, &output));
27505f80ce2aSJacob Faibussowitsch       CHKERRQ(VecRestoreArray(global, &input));
27515f80ce2aSJacob Faibussowitsch       CHKERRQ(VecDestroy(&global));
27524bbf5ea8SMatthew G. Knepley     }
275361c4b389SFlorian Wechsung     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE && patch->save_operators) {
27545f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscMalloc1(patch->npatch, &patch->matWithArtificial));
27554bbf5ea8SMatthew G. Knepley     }
27564bbf5ea8SMatthew G. Knepley   }
27575f80ce2aSJacob Faibussowitsch   CHKERRQ((*patch->setupsolver)(pc));
2758dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
27594bbf5ea8SMatthew G. Knepley }
2760dadc69c5SMatthew G. Knepley 
2761dadc69c5SMatthew G. Knepley static PetscErrorCode PCApply_PATCH_Linear(PC pc, PetscInt i, Vec x, Vec y)
2762dadc69c5SMatthew G. Knepley {
2763dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2764c73d2cf6SLawrence Mitchell   KSP            ksp;
27659d4fc724SLawrence Mitchell   Mat            op;
27669d4fc724SLawrence Mitchell   PetscInt       m, n;
2767dadc69c5SMatthew G. Knepley 
2768dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2769c73d2cf6SLawrence Mitchell   if (patch->denseinverse) {
27705f80ce2aSJacob Faibussowitsch     CHKERRQ((*patch->densesolve)(patch->mat[i], x, y));
2771c73d2cf6SLawrence Mitchell     PetscFunctionReturn(0);
2772c73d2cf6SLawrence Mitchell   }
2773c73d2cf6SLawrence Mitchell   ksp = (KSP) patch->solver[i];
2774dadc69c5SMatthew G. Knepley   if (!patch->save_operators) {
2775dadc69c5SMatthew G. Knepley     Mat mat;
2776dadc69c5SMatthew G. Knepley 
27775f80ce2aSJacob Faibussowitsch     CHKERRQ(PCPatchCreateMatrix_Private(pc, i, &mat, PETSC_FALSE));
2778dadc69c5SMatthew G. Knepley     /* Populate operator here. */
27795f80ce2aSJacob Faibussowitsch     CHKERRQ(PCPatchComputeOperator_Internal(pc, NULL, mat, i, PETSC_FALSE));
27805f80ce2aSJacob Faibussowitsch     CHKERRQ(KSPSetOperators(ksp, mat, mat));
2781dadc69c5SMatthew G. Knepley     /* Drop reference so the KSPSetOperators below will blow it away. */
27825f80ce2aSJacob Faibussowitsch     CHKERRQ(MatDestroy(&mat));
2783dadc69c5SMatthew G. Knepley   }
27845f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0));
2785dadc69c5SMatthew G. Knepley   if (!ksp->setfromoptionscalled) {
27865f80ce2aSJacob Faibussowitsch     CHKERRQ(KSPSetFromOptions(ksp));
2787dadc69c5SMatthew G. Knepley   }
27889d4fc724SLawrence Mitchell   /* Disgusting trick to reuse work vectors */
27895f80ce2aSJacob Faibussowitsch   CHKERRQ(KSPGetOperators(ksp, &op, NULL));
27905f80ce2aSJacob Faibussowitsch   CHKERRQ(MatGetLocalSize(op, &m, &n));
27919d4fc724SLawrence Mitchell   x->map->n = m;
27929d4fc724SLawrence Mitchell   y->map->n = n;
27939d4fc724SLawrence Mitchell   x->map->N = m;
27949d4fc724SLawrence Mitchell   y->map->N = n;
27955f80ce2aSJacob Faibussowitsch   CHKERRQ(KSPSolve(ksp, x, y));
27965f80ce2aSJacob Faibussowitsch   CHKERRQ(KSPCheckSolve(ksp, pc, y));
27975f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0));
2798dadc69c5SMatthew G. Knepley   if (!patch->save_operators) {
2799dadc69c5SMatthew G. Knepley     PC pc;
28005f80ce2aSJacob Faibussowitsch     CHKERRQ(KSPSetOperators(ksp, NULL, NULL));
28015f80ce2aSJacob Faibussowitsch     CHKERRQ(KSPGetPC(ksp, &pc));
2802dadc69c5SMatthew G. Knepley     /* Destroy PC context too, otherwise the factored matrix hangs around. */
28035f80ce2aSJacob Faibussowitsch     CHKERRQ(PCReset(pc));
28044bbf5ea8SMatthew G. Knepley   }
28054bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
28064bbf5ea8SMatthew G. Knepley }
28074bbf5ea8SMatthew G. Knepley 
28086c9c532dSPatrick Farrell static PetscErrorCode PCUpdateMultiplicative_PATCH_Linear(PC pc, PetscInt i, PetscInt pStart)
28096c9c532dSPatrick Farrell {
28106c9c532dSPatrick Farrell   PC_PATCH      *patch = (PC_PATCH *) pc->data;
28116c9c532dSPatrick Farrell   Mat            multMat;
28129d4fc724SLawrence Mitchell   PetscInt       n, m;
28136c9c532dSPatrick Farrell 
28144d04e9f1SPatrick Farrell   PetscFunctionBegin;
28154d04e9f1SPatrick Farrell 
28166c9c532dSPatrick Farrell   if (patch->save_operators) {
28176c9c532dSPatrick Farrell     multMat = patch->matWithArtificial[i];
28186c9c532dSPatrick Farrell   } else {
28196c9c532dSPatrick Farrell     /*Very inefficient, hopefully we can just assemble the rectangular matrix in the first place.*/
28206c9c532dSPatrick Farrell     Mat      matSquare;
28216c9c532dSPatrick Farrell     PetscInt dof;
28226c9c532dSPatrick Farrell     IS       rowis;
28235f80ce2aSJacob Faibussowitsch     CHKERRQ(PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE));
28245f80ce2aSJacob Faibussowitsch     CHKERRQ(PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE));
28255f80ce2aSJacob Faibussowitsch     CHKERRQ(MatGetSize(matSquare, &dof, NULL));
28265f80ce2aSJacob Faibussowitsch     CHKERRQ(ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis));
28275f80ce2aSJacob Faibussowitsch     CHKERRQ(MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &multMat));
28285f80ce2aSJacob Faibussowitsch     CHKERRQ(MatDestroy(&matSquare));
28295f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&rowis));
28306c9c532dSPatrick Farrell   }
28319d4fc724SLawrence Mitchell   /* Disgusting trick to reuse work vectors */
28325f80ce2aSJacob Faibussowitsch   CHKERRQ(MatGetLocalSize(multMat, &m, &n));
28339d4fc724SLawrence Mitchell   patch->patchUpdate->map->n = n;
28349d4fc724SLawrence Mitchell   patch->patchRHSWithArtificial->map->n = m;
28359d4fc724SLawrence Mitchell   patch->patchUpdate->map->N = n;
28369d4fc724SLawrence Mitchell   patch->patchRHSWithArtificial->map->N = m;
28375f80ce2aSJacob Faibussowitsch   CHKERRQ(MatMult(multMat, patch->patchUpdate, patch->patchRHSWithArtificial));
28385f80ce2aSJacob Faibussowitsch   CHKERRQ(VecScale(patch->patchRHSWithArtificial, -1.0));
28395f80ce2aSJacob Faibussowitsch   CHKERRQ(PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHSWithArtificial, patch->localRHS, ADD_VALUES, SCATTER_REVERSE, SCATTER_WITHARTIFICIAL));
28406c9c532dSPatrick Farrell   if (!patch->save_operators) {
28415f80ce2aSJacob Faibussowitsch     CHKERRQ(MatDestroy(&multMat));
28426c9c532dSPatrick Farrell   }
28434d04e9f1SPatrick Farrell   PetscFunctionReturn(0);
28446c9c532dSPatrick Farrell }
28456c9c532dSPatrick Farrell 
28464bbf5ea8SMatthew G. Knepley static PetscErrorCode PCApply_PATCH(PC pc, Vec x, Vec y)
28474bbf5ea8SMatthew G. Knepley {
28484bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch    = (PC_PATCH *) pc->data;
28491202d238SPatrick Farrell   const PetscScalar *globalRHS  = NULL;
28501202d238SPatrick Farrell   PetscScalar       *localRHS   = NULL;
28511202d238SPatrick Farrell   PetscScalar       *globalUpdate  = NULL;
28524bbf5ea8SMatthew G. Knepley   const PetscInt    *bcNodes  = NULL;
28534bbf5ea8SMatthew G. Knepley   PetscInt           nsweep   = patch->symmetrise_sweep ? 2 : 1;
28544bbf5ea8SMatthew G. Knepley   PetscInt           start[2] = {0, 0};
28554bbf5ea8SMatthew G. Knepley   PetscInt           end[2]   = {-1, -1};
28564bbf5ea8SMatthew G. Knepley   const PetscInt     inc[2]   = {1, -1};
28571202d238SPatrick Farrell   const PetscScalar *localUpdate;
28584bbf5ea8SMatthew G. Knepley   const PetscInt    *iterationSet;
28594bbf5ea8SMatthew G. Knepley   PetscInt           pStart, numBcs, n, sweep, bc, j;
28604bbf5ea8SMatthew G. Knepley 
28614bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
28625f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventBegin(PC_Patch_Apply, pc, 0, 0, 0));
28635f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsPushGetViewerOff(PETSC_TRUE));
286492d50984SMatthew G. Knepley   /* start, end, inc have 2 entries to manage a second backward sweep if we symmetrize */
28654bbf5ea8SMatthew G. Knepley   end[0]   = patch->npatch;
28664bbf5ea8SMatthew G. Knepley   start[1] = patch->npatch-1;
28674bbf5ea8SMatthew G. Knepley   if (patch->user_patches) {
28685f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetLocalSize(patch->iterationSet, &end[0]));
28694bbf5ea8SMatthew G. Knepley     start[1] = end[0] - 1;
28705f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(patch->iterationSet, &iterationSet));
28714bbf5ea8SMatthew G. Knepley   }
28724bbf5ea8SMatthew G. Knepley   /* Scatter from global space into overlapped local spaces */
28735f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetArrayRead(x, &globalRHS));
28745f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetArray(patch->localRHS, &localRHS));
28755f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSFBcastBegin(patch->sectionSF, MPIU_SCALAR, globalRHS, localRHS,MPI_REPLACE));
28765f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSFBcastEnd(patch->sectionSF, MPIU_SCALAR, globalRHS, localRHS,MPI_REPLACE));
28775f80ce2aSJacob Faibussowitsch   CHKERRQ(VecRestoreArrayRead(x, &globalRHS));
28785f80ce2aSJacob Faibussowitsch   CHKERRQ(VecRestoreArray(patch->localRHS, &localRHS));
28794bbf5ea8SMatthew G. Knepley 
28805f80ce2aSJacob Faibussowitsch   CHKERRQ(VecSet(patch->localUpdate, 0.0));
28815f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetChart(patch->gtolCounts, &pStart, NULL));
28825f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0));
28834bbf5ea8SMatthew G. Knepley   for (sweep = 0; sweep < nsweep; sweep++) {
28844bbf5ea8SMatthew G. Knepley     for (j = start[sweep]; j*inc[sweep] < end[sweep]*inc[sweep]; j += inc[sweep]) {
28854bbf5ea8SMatthew G. Knepley       PetscInt i = patch->user_patches ? iterationSet[j] : j;
28864bbf5ea8SMatthew G. Knepley       PetscInt start, len;
28874bbf5ea8SMatthew G. Knepley 
28885f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetDof(patch->gtolCounts, i+pStart, &len));
28895f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetOffset(patch->gtolCounts, i+pStart, &start));
28904bbf5ea8SMatthew G. Knepley       /* TODO: Squash out these guys in the setup as well. */
28914bbf5ea8SMatthew G. Knepley       if (len <= 0) continue;
28924bbf5ea8SMatthew G. Knepley       /* TODO: Do we need different scatters for X and Y? */
28935f80ce2aSJacob Faibussowitsch       CHKERRQ(PCPatch_ScatterLocal_Private(pc, i+pStart, patch->localRHS, patch->patchRHS, INSERT_VALUES, SCATTER_FORWARD, SCATTER_INTERIOR));
28945f80ce2aSJacob Faibussowitsch       CHKERRQ((*patch->applysolver)(pc, i, patch->patchRHS, patch->patchUpdate));
28955f80ce2aSJacob Faibussowitsch       CHKERRQ(PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchUpdate, patch->localUpdate, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR));
289661c4b389SFlorian Wechsung       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
28975f80ce2aSJacob Faibussowitsch         CHKERRQ((*patch->updatemultiplicative)(pc, i, pStart));
2898c2e6f3c0SFlorian Wechsung       }
28994bbf5ea8SMatthew G. Knepley     }
29004bbf5ea8SMatthew G. Knepley   }
29015f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0));
29025f80ce2aSJacob Faibussowitsch   if (patch->user_patches) CHKERRQ(ISRestoreIndices(patch->iterationSet, &iterationSet));
29034bbf5ea8SMatthew G. Knepley   /* XXX: should we do this on the global vector? */
290473ec7555SLawrence Mitchell   if (patch->partition_of_unity) {
29055f80ce2aSJacob Faibussowitsch     CHKERRQ(VecPointwiseMult(patch->localUpdate, patch->localUpdate, patch->dof_weights));
29064bbf5ea8SMatthew G. Knepley   }
29071202d238SPatrick Farrell   /* Now patch->localUpdate contains the solution of the patch solves, so we need to combine them all. */
29085f80ce2aSJacob Faibussowitsch   CHKERRQ(VecSet(y, 0.0));
29095f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetArray(y, &globalUpdate));
29105f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetArrayRead(patch->localUpdate, &localUpdate));
29115f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSFReduceBegin(patch->sectionSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM));
29125f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSFReduceEnd(patch->sectionSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM));
29135f80ce2aSJacob Faibussowitsch   CHKERRQ(VecRestoreArrayRead(patch->localUpdate, &localUpdate));
29144bbf5ea8SMatthew G. Knepley 
29154bbf5ea8SMatthew G. Knepley   /* Now we need to send the global BC values through */
29165f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetArrayRead(x, &globalRHS));
29175f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetSize(patch->globalBcNodes, &numBcs));
29185f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetIndices(patch->globalBcNodes, &bcNodes));
29195f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetLocalSize(x, &n));
29204bbf5ea8SMatthew G. Knepley   for (bc = 0; bc < numBcs; ++bc) {
29214bbf5ea8SMatthew G. Knepley     const PetscInt idx = bcNodes[bc];
29221202d238SPatrick Farrell     if (idx < n) globalUpdate[idx] = globalRHS[idx];
29234bbf5ea8SMatthew G. Knepley   }
29244bbf5ea8SMatthew G. Knepley 
29255f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(patch->globalBcNodes, &bcNodes));
29265f80ce2aSJacob Faibussowitsch   CHKERRQ(VecRestoreArrayRead(x, &globalRHS));
29275f80ce2aSJacob Faibussowitsch   CHKERRQ(VecRestoreArray(y, &globalUpdate));
29284bbf5ea8SMatthew G. Knepley 
29295f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsPopGetViewerOff());
29305f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventEnd(PC_Patch_Apply, pc, 0, 0, 0));
29314bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
29324bbf5ea8SMatthew G. Knepley }
29334bbf5ea8SMatthew G. Knepley 
2934dadc69c5SMatthew G. Knepley static PetscErrorCode PCReset_PATCH_Linear(PC pc)
2935dadc69c5SMatthew G. Knepley {
2936dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2937dadc69c5SMatthew G. Knepley   PetscInt       i;
2938dadc69c5SMatthew G. Knepley 
2939dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
2940dadc69c5SMatthew G. Knepley   if (patch->solver) {
29415f80ce2aSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) CHKERRQ(KSPReset((KSP) patch->solver[i]));
2942dadc69c5SMatthew G. Knepley   }
2943dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
2944dadc69c5SMatthew G. Knepley }
2945dadc69c5SMatthew G. Knepley 
29464bbf5ea8SMatthew G. Knepley static PetscErrorCode PCReset_PATCH(PC pc)
29474bbf5ea8SMatthew G. Knepley {
29484bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
29494bbf5ea8SMatthew G. Knepley   PetscInt       i;
29504bbf5ea8SMatthew G. Knepley 
29514bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
2952fa84ea4cSLawrence Mitchell 
29535f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSFDestroy(&patch->sectionSF));
29545f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionDestroy(&patch->cellCounts));
29555f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionDestroy(&patch->pointCounts));
29565f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionDestroy(&patch->cellNumbering));
29575f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionDestroy(&patch->gtolCounts));
29585f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&patch->gtol));
29595f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&patch->cells));
29605f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&patch->points));
29615f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&patch->dofs));
29625f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&patch->offs));
29635f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionDestroy(&patch->patchSection));
29645f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&patch->ghostBcNodes));
29655f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&patch->globalBcNodes));
29665f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionDestroy(&patch->gtolCountsWithArtificial));
29675f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&patch->gtolWithArtificial));
29685f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&patch->dofsWithArtificial));
29695f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&patch->offsWithArtificial));
29705f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionDestroy(&patch->gtolCountsWithAll));
29715f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&patch->gtolWithAll));
29725f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&patch->dofsWithAll));
29735f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&patch->offsWithAll));
29745f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&patch->cellMats));
29755f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&patch->intFacetMats));
29765f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&patch->allCells));
29775f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&patch->intFacets));
29785f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&patch->extFacets));
29795f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&patch->intFacetsToPatchCell));
29805f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionDestroy(&patch->intFacetCounts));
29815f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionDestroy(&patch->extFacetCounts));
29824bbf5ea8SMatthew G. Knepley 
29835f80ce2aSJacob Faibussowitsch   if (patch->dofSection) for (i = 0; i < patch->nsubspaces; i++) CHKERRQ(PetscSectionDestroy(&patch->dofSection[i]));
29845f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(patch->dofSection));
29855f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(patch->bs));
29865f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(patch->nodesPerCell));
29875f80ce2aSJacob Faibussowitsch   if (patch->cellNodeMap) for (i = 0; i < patch->nsubspaces; i++) CHKERRQ(PetscFree(patch->cellNodeMap[i]));
29885f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(patch->cellNodeMap));
29895f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(patch->subspaceOffsets));
29904bbf5ea8SMatthew G. Knepley 
29915f80ce2aSJacob Faibussowitsch   CHKERRQ((*patch->resetsolver)(pc));
29924bbf5ea8SMatthew G. Knepley 
2993e4c66b91SPatrick Farrell   if (patch->subspaces_to_exclude) {
29945f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetIDestroy(&patch->subspaces_to_exclude));
2995e4c66b91SPatrick Farrell   }
2996e4c66b91SPatrick Farrell 
29975f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&patch->localRHS));
29985f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&patch->localUpdate));
29995f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&patch->patchRHS));
30005f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&patch->patchUpdate));
30015f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&patch->dof_weights));
30024bbf5ea8SMatthew G. Knepley   if (patch->patch_dof_weights) {
30035f80ce2aSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) CHKERRQ(VecDestroy(&patch->patch_dof_weights[i]));
30045f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(patch->patch_dof_weights));
30054bbf5ea8SMatthew G. Knepley   }
30064bbf5ea8SMatthew G. Knepley   if (patch->mat) {
30075f80ce2aSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) CHKERRQ(MatDestroy(&patch->mat[i]));
30085f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(patch->mat));
30095f824522SMatthew G. Knepley   }
301011ac8bb0SFlorian Wechsung   if (patch->matWithArtificial) {
30115f80ce2aSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) CHKERRQ(MatDestroy(&patch->matWithArtificial[i]));
30125f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(patch->matWithArtificial));
301311ac8bb0SFlorian Wechsung   }
30145f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&patch->patchRHSWithArtificial));
301596b79ebeSFlorian Wechsung   if (patch->dofMappingWithoutToWithArtificial) {
30165f80ce2aSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) CHKERRQ(ISDestroy(&patch->dofMappingWithoutToWithArtificial[i]));
30175f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(patch->dofMappingWithoutToWithArtificial));
301896b79ebeSFlorian Wechsung 
301996b79ebeSFlorian Wechsung   }
30200904074fSPatrick Farrell   if (patch->dofMappingWithoutToWithAll) {
30215f80ce2aSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) CHKERRQ(ISDestroy(&patch->dofMappingWithoutToWithAll[i]));
30225f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(patch->dofMappingWithoutToWithAll));
30230904074fSPatrick Farrell 
30240904074fSPatrick Farrell   }
30255f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(patch->sub_mat_type));
30265f824522SMatthew G. Knepley   if (patch->userIS) {
30275f80ce2aSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) CHKERRQ(ISDestroy(&patch->userIS[i]));
30285f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(patch->userIS));
30295f824522SMatthew G. Knepley   }
30305f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(patch->precomputedTensorLocations));
30315f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(patch->precomputedIntFacetTensorLocations));
3032f98464cbSLawrence Mitchell 
30330a545947SLisandro Dalcin   patch->bs          = NULL;
30344bbf5ea8SMatthew G. Knepley   patch->cellNodeMap = NULL;
30357974b488SMatthew G. Knepley   patch->nsubspaces  = 0;
30365f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&patch->iterationSet));
30375f824522SMatthew G. Knepley 
30385f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerDestroy(&patch->viewerSection));
30394bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
30404bbf5ea8SMatthew G. Knepley }
30414bbf5ea8SMatthew G. Knepley 
3042dadc69c5SMatthew G. Knepley static PetscErrorCode PCDestroy_PATCH_Linear(PC pc)
30434bbf5ea8SMatthew G. Knepley {
30444bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
30454bbf5ea8SMatthew G. Knepley   PetscInt       i;
30464bbf5ea8SMatthew G. Knepley 
30474bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3048dadc69c5SMatthew G. Knepley   if (patch->solver) {
30495f80ce2aSJacob Faibussowitsch     for (i = 0; i < patch->npatch; ++i) CHKERRQ(KSPDestroy((KSP *) &patch->solver[i]));
30505f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(patch->solver));
30514bbf5ea8SMatthew G. Knepley   }
3052dadc69c5SMatthew G. Knepley   PetscFunctionReturn(0);
3053dadc69c5SMatthew G. Knepley }
3054dadc69c5SMatthew G. Knepley 
3055dadc69c5SMatthew G. Knepley static PetscErrorCode PCDestroy_PATCH(PC pc)
3056dadc69c5SMatthew G. Knepley {
3057dadc69c5SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
3058dadc69c5SMatthew G. Knepley 
3059dadc69c5SMatthew G. Knepley   PetscFunctionBegin;
30605f80ce2aSJacob Faibussowitsch   CHKERRQ(PCReset_PATCH(pc));
30615f80ce2aSJacob Faibussowitsch   CHKERRQ((*patch->destroysolver)(pc));
30625f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(pc->data));
30634bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
30644bbf5ea8SMatthew G. Knepley }
30654bbf5ea8SMatthew G. Knepley 
30664bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetFromOptions_PATCH(PetscOptionItems *PetscOptionsObject, PC pc)
30674bbf5ea8SMatthew G. Knepley {
30684bbf5ea8SMatthew G. Knepley   PC_PATCH            *patch = (PC_PATCH *) pc->data;
30694bbf5ea8SMatthew G. Knepley   PCPatchConstructType patchConstructionType = PC_PATCH_STAR;
30705f824522SMatthew G. Knepley   char                 sub_mat_type[PETSC_MAX_PATH_LEN];
307110534d48SPatrick Farrell   char                 option[PETSC_MAX_PATH_LEN];
30725f824522SMatthew G. Knepley   const char          *prefix;
30734bbf5ea8SMatthew G. Knepley   PetscBool            flg, dimflg, codimflg;
30745f824522SMatthew G. Knepley   MPI_Comm             comm;
3075a48c39c8SPatrick Farrell   PetscInt            *ifields, nfields, k;
307661c4b389SFlorian Wechsung   PCCompositeType      loctype = PC_COMPOSITE_ADDITIVE;
30774bbf5ea8SMatthew G. Knepley 
30784bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
30795f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectGetComm((PetscObject) pc, &comm));
30805f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectGetOptionsPrefix((PetscObject) pc, &prefix));
30815f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsHead(PetscOptionsObject, "Patch solver options"));
308210534d48SPatrick Farrell 
30835f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_save_operators", patch->classname));
30845f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsBool(option,  "Store all patch operators for lifetime of object?", "PCPatchSetSaveOperators", patch->save_operators, &patch->save_operators, &flg));
308510534d48SPatrick Farrell 
30865f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_precompute_element_tensors", patch->classname));
30875f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsBool(option,  "Compute each element tensor only once?", "PCPatchSetPrecomputeElementTensors", patch->precomputeElementTensors, &patch->precomputeElementTensors, &flg));
30885f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_partition_of_unity", patch->classname));
30895f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsBool(option, "Weight contributions by dof multiplicity?", "PCPatchSetPartitionOfUnity", patch->partition_of_unity, &patch->partition_of_unity, &flg));
309010534d48SPatrick Farrell 
30915f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_local_type", patch->classname));
30925f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsEnum(option,"Type of local solver composition (additive or multiplicative)","PCPatchSetLocalComposition",PCCompositeTypes,(PetscEnum)loctype,(PetscEnum*)&loctype,&flg));
30935f80ce2aSJacob Faibussowitsch   if (flg) CHKERRQ(PCPatchSetLocalComposition(pc, loctype));
30945f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_dense_inverse", patch->classname));
30955f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsBool(option, "Compute inverses of patch matrices and apply directly? Ignores KSP/PC settings on patch.", "PCPatchSetDenseInverse", patch->denseinverse, &patch->denseinverse, &flg));
30965f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_dim", patch->classname));
30975f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsInt(option, "What dimension of mesh point to construct patches by? (0 = vertices)", "PCPATCH", patch->dim, &patch->dim, &dimflg));
30985f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_codim", patch->classname));
30995f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsInt(option, "What co-dimension of mesh point to construct patches by? (0 = cells)", "PCPATCH", patch->codim, &patch->codim, &codimflg));
31002c71b3e2SJacob Faibussowitsch   PetscCheckFalse(dimflg && codimflg,comm, PETSC_ERR_ARG_WRONG, "Can only set one of dimension or co-dimension");
310110534d48SPatrick Farrell 
31025f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_type", patch->classname));
31035f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsEnum(option, "How should the patches be constructed?", "PCPatchSetConstructType", PCPatchConstructTypes, (PetscEnum) patchConstructionType, (PetscEnum *) &patchConstructionType, &flg));
31045f80ce2aSJacob Faibussowitsch   if (flg) CHKERRQ(PCPatchSetConstructType(pc, patchConstructionType, NULL, NULL));
310510534d48SPatrick Farrell 
31065f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_vanka_dim", patch->classname));
31075f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsInt(option, "Topological dimension of entities for Vanka to ignore", "PCPATCH", patch->vankadim, &patch->vankadim, &flg));
310810534d48SPatrick Farrell 
31095f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_ignore_dim", patch->classname));
31105f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsInt(option, "Topological dimension of entities for completion to ignore", "PCPATCH", patch->ignoredim, &patch->ignoredim, &flg));
311110534d48SPatrick Farrell 
31125f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_pardecomp_overlap", patch->classname));
31135f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsInt(option, "What overlap should we use in construct type pardecomp?", "PCPATCH", patch->pardecomp_overlap, &patch->pardecomp_overlap, &flg));
3114b525f888SPatrick Farrell 
31155f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_sub_mat_type", patch->classname));
31165f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsFList(option, "Matrix type for patch solves", "PCPatchSetSubMatType", MatList, NULL, sub_mat_type, PETSC_MAX_PATH_LEN, &flg));
31175f80ce2aSJacob Faibussowitsch   if (flg) CHKERRQ(PCPatchSetSubMatType(pc, sub_mat_type));
311810534d48SPatrick Farrell 
31195f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_symmetrise_sweep", patch->classname));
31205f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsBool(option, "Go start->end, end->start?", "PCPATCH", patch->symmetrise_sweep, &patch->symmetrise_sweep, &flg));
3121e4c66b91SPatrick Farrell 
3122a48c39c8SPatrick Farrell   /* If the user has set the number of subspaces, use that for the buffer size,
3123a48c39c8SPatrick Farrell      otherwise use a large number */
3124a48c39c8SPatrick Farrell   if (patch->nsubspaces <= 0) {
3125a48c39c8SPatrick Farrell     nfields = 128;
3126a48c39c8SPatrick Farrell   } else {
3127a48c39c8SPatrick Farrell     nfields = patch->nsubspaces;
3128a48c39c8SPatrick Farrell   }
31295f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(nfields, &ifields));
31305f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exclude_subspaces", patch->classname));
31315f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsGetIntArray(((PetscObject)pc)->options,((PetscObject)pc)->prefix,option,ifields,&nfields,&flg));
31322c71b3e2SJacob Faibussowitsch   PetscCheckFalse(flg && (patchConstructionType == PC_PATCH_USER),comm, PETSC_ERR_ARG_INCOMP, "We cannot support excluding a subspace with user patches because we do not index patches with a mesh point");
3133e4c66b91SPatrick Farrell   if (flg) {
31345f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetIClear(patch->subspaces_to_exclude));
313559b66c28SPatrick Farrell     for (k = 0; k < nfields; k++) {
31365f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscHSetIAdd(patch->subspaces_to_exclude, ifields[k]));
3137e4c66b91SPatrick Farrell     }
3138e4c66b91SPatrick Farrell   }
31395f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(ifields));
31405f824522SMatthew G. Knepley 
31415f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_patches_view", patch->classname));
31425f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsBool(option, "Print out information during patch construction", "PCPATCH", patch->viewPatches, &patch->viewPatches, &flg));
31435f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_cells_view", patch->classname));
31445f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerCells, &patch->formatCells, &patch->viewCells));
31455f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_interior_facets_view", patch->classname));
31465f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerIntFacets, &patch->formatIntFacets, &patch->viewIntFacets));
31475f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exterior_facets_view", patch->classname));
31485f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerExtFacets, &patch->formatExtFacets, &patch->viewExtFacets));
31495f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_points_view", patch->classname));
31505f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerPoints, &patch->formatPoints, &patch->viewPoints));
31515f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_section_view", patch->classname));
31525f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerSection, &patch->formatSection, &patch->viewSection));
31535f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_mat_view", patch->classname));
31545f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsGetViewer(comm,((PetscObject) pc)->options, prefix, option, &patch->viewerMatrix, &patch->formatMatrix, &patch->viewMatrix));
31555f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsTail());
31565f824522SMatthew G. Knepley   patch->optionsSet = PETSC_TRUE;
31574bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
31584bbf5ea8SMatthew G. Knepley }
31594bbf5ea8SMatthew G. Knepley 
31604bbf5ea8SMatthew G. Knepley static PetscErrorCode PCSetUpOnBlocks_PATCH(PC pc)
31614bbf5ea8SMatthew G. Knepley {
31624bbf5ea8SMatthew G. Knepley   PC_PATCH          *patch = (PC_PATCH*) pc->data;
31634bbf5ea8SMatthew G. Knepley   KSPConvergedReason reason;
31644bbf5ea8SMatthew G. Knepley   PetscInt           i;
31654bbf5ea8SMatthew G. Knepley 
31664bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
3167a1eac568SLawrence Mitchell   if (!patch->save_operators) {
3168a1eac568SLawrence Mitchell     /* Can't do this here because the sub KSPs don't have an operator attached yet. */
3169a1eac568SLawrence Mitchell     PetscFunctionReturn(0);
3170a1eac568SLawrence Mitchell   }
3171c73d2cf6SLawrence Mitchell   if (patch->denseinverse) {
3172c73d2cf6SLawrence Mitchell     /* No solvers */
3173c73d2cf6SLawrence Mitchell     PetscFunctionReturn(0);
3174c73d2cf6SLawrence Mitchell   }
31754bbf5ea8SMatthew G. Knepley   for (i = 0; i < patch->npatch; ++i) {
3176dadc69c5SMatthew G. Knepley     if (!((KSP) patch->solver[i])->setfromoptionscalled) {
31775f80ce2aSJacob Faibussowitsch       CHKERRQ(KSPSetFromOptions((KSP) patch->solver[i]));
3178a1eac568SLawrence Mitchell     }
31795f80ce2aSJacob Faibussowitsch     CHKERRQ(KSPSetUp((KSP) patch->solver[i]));
31805f80ce2aSJacob Faibussowitsch     CHKERRQ(KSPGetConvergedReason((KSP) patch->solver[i], &reason));
3181c0decd05SBarry Smith     if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR;
31824bbf5ea8SMatthew G. Knepley   }
31834bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
31844bbf5ea8SMatthew G. Knepley }
31854bbf5ea8SMatthew G. Knepley 
31864bbf5ea8SMatthew G. Knepley static PetscErrorCode PCView_PATCH(PC pc, PetscViewer viewer)
31874bbf5ea8SMatthew G. Knepley {
31884bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch = (PC_PATCH *) pc->data;
31894bbf5ea8SMatthew G. Knepley   PetscViewer    sviewer;
31904bbf5ea8SMatthew G. Knepley   PetscBool      isascii;
31914bbf5ea8SMatthew G. Knepley   PetscMPIInt    rank;
31924bbf5ea8SMatthew G. Knepley 
31934bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
31944bbf5ea8SMatthew G. Knepley   /* TODO Redo tabbing with set tbas in new style */
31955f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii));
31964bbf5ea8SMatthew G. Knepley   if (!isascii) PetscFunctionReturn(0);
31975f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) pc), &rank));
31985f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerASCIIPushTab(viewer));
31995f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerASCIIPrintf(viewer, "Subspace Correction preconditioner with %d patches\n", patch->npatch));
320061c4b389SFlorian Wechsung   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
32015f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscViewerASCIIPrintf(viewer, "Schwarz type: multiplicative\n"));
3202e047a90bSFlorian Wechsung   } else {
32035f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscViewerASCIIPrintf(viewer, "Schwarz type: additive\n"));
3204c2e6f3c0SFlorian Wechsung   }
32055f80ce2aSJacob Faibussowitsch   if (patch->partition_of_unity) CHKERRQ(PetscViewerASCIIPrintf(viewer, "Weighting by partition of unity\n"));
32065f80ce2aSJacob Faibussowitsch   else                           CHKERRQ(PetscViewerASCIIPrintf(viewer, "Not weighting by partition of unity\n"));
32075f80ce2aSJacob Faibussowitsch   if (patch->symmetrise_sweep) CHKERRQ(PetscViewerASCIIPrintf(viewer, "Symmetrising sweep (start->end, then end->start)\n"));
32085f80ce2aSJacob Faibussowitsch   else                         CHKERRQ(PetscViewerASCIIPrintf(viewer, "Not symmetrising sweep\n"));
32095f80ce2aSJacob Faibussowitsch   if (!patch->precomputeElementTensors) CHKERRQ(PetscViewerASCIIPrintf(viewer, "Not precomputing element tensors (overlapping cells rebuilt in every patch assembly)\n"));
32105f80ce2aSJacob Faibussowitsch   else                            CHKERRQ(PetscViewerASCIIPrintf(viewer, "Precomputing element tensors (each cell assembled only once)\n"));
32115f80ce2aSJacob Faibussowitsch   if (!patch->save_operators) CHKERRQ(PetscViewerASCIIPrintf(viewer, "Not saving patch operators (rebuilt every PCApply)\n"));
32125f80ce2aSJacob Faibussowitsch   else                        CHKERRQ(PetscViewerASCIIPrintf(viewer, "Saving patch operators (rebuilt every PCSetUp)\n"));
32135f80ce2aSJacob Faibussowitsch   if (patch->patchconstructop == PCPatchConstruct_Star)       CHKERRQ(PetscViewerASCIIPrintf(viewer, "Patch construction operator: star\n"));
32145f80ce2aSJacob Faibussowitsch   else if (patch->patchconstructop == PCPatchConstruct_Vanka) CHKERRQ(PetscViewerASCIIPrintf(viewer, "Patch construction operator: Vanka\n"));
32155f80ce2aSJacob Faibussowitsch   else if (patch->patchconstructop == PCPatchConstruct_User)  CHKERRQ(PetscViewerASCIIPrintf(viewer, "Patch construction operator: user-specified\n"));
32165f80ce2aSJacob Faibussowitsch   else                                                        CHKERRQ(PetscViewerASCIIPrintf(viewer, "Patch construction operator: unknown\n"));
32175d30859aSPatrick Farrell 
3218c73d2cf6SLawrence Mitchell   if (patch->denseinverse) {
32195f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscViewerASCIIPrintf(viewer, "Explicitly forming dense inverse and applying patch solver via MatMult.\n"));
3220c73d2cf6SLawrence Mitchell   } else {
32215d30859aSPatrick Farrell     if (patch->isNonlinear) {
32225f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscViewerASCIIPrintf(viewer, "SNES on patches (all same):\n"));
32235d30859aSPatrick Farrell     } else {
32245f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscViewerASCIIPrintf(viewer, "KSP on patches (all same):\n"));
32255d30859aSPatrick Farrell     }
3226dadc69c5SMatthew G. Knepley     if (patch->solver) {
32275f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
3228dd400576SPatrick Sanan       if (rank == 0) {
32295f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscViewerASCIIPushTab(sviewer));
32305f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscObjectView(patch->solver[0], sviewer));
32315f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscViewerASCIIPopTab(sviewer));
32324bbf5ea8SMatthew G. Knepley       }
32335f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
32344bbf5ea8SMatthew G. Knepley     } else {
32355f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscViewerASCIIPushTab(viewer));
32365f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscViewerASCIIPrintf(viewer, "Solver not yet set.\n"));
32375f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscViewerASCIIPopTab(viewer));
32384bbf5ea8SMatthew G. Knepley     }
3239c73d2cf6SLawrence Mitchell   }
32405f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerASCIIPopTab(viewer));
32414bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
32424bbf5ea8SMatthew G. Knepley }
32434bbf5ea8SMatthew G. Knepley 
3244e5893cccSMatthew G. Knepley /*MC
324598ed095eSMatthew G. Knepley   PCPATCH - A PC object that encapsulates flexible definition of blocks for overlapping and non-overlapping
324698ed095eSMatthew G. Knepley             small block additive preconditioners. Block definition is based on topology from
3247e5893cccSMatthew G. Knepley             a DM and equation numbering from a PetscSection.
3248e5893cccSMatthew G. Knepley 
3249e5893cccSMatthew G. Knepley   Options Database Keys:
3250e5893cccSMatthew G. Knepley + -pc_patch_cells_view   - Views the process local cell numbers for each patch
3251e5893cccSMatthew G. Knepley . -pc_patch_points_view  - Views the process local mesh point numbers for each patch
3252e5893cccSMatthew G. Knepley . -pc_patch_g2l_view     - Views the map between global dofs and patch local dofs for each patch
3253e5893cccSMatthew G. Knepley . -pc_patch_patches_view - Views the global dofs associated with each patch and its boundary
3254e5893cccSMatthew G. Knepley - -pc_patch_sub_mat_view - Views the matrix associated with each patch
3255e5893cccSMatthew G. Knepley 
3256e5893cccSMatthew G. Knepley   Level: intermediate
3257e5893cccSMatthew G. Knepley 
3258e5893cccSMatthew G. Knepley .seealso: PCType, PCCreate(), PCSetType()
3259e5893cccSMatthew G. Knepley M*/
3260642283e9SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PCCreate_Patch(PC pc)
32614bbf5ea8SMatthew G. Knepley {
32624bbf5ea8SMatthew G. Knepley   PC_PATCH      *patch;
32634bbf5ea8SMatthew G. Knepley 
32644bbf5ea8SMatthew G. Knepley   PetscFunctionBegin;
32655f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscNewLog(pc, &patch));
32664bbf5ea8SMatthew G. Knepley 
3267e4c66b91SPatrick Farrell   if (patch->subspaces_to_exclude) {
32685f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHSetIDestroy(&patch->subspaces_to_exclude));
3269e4c66b91SPatrick Farrell   }
32705f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHSetICreate(&patch->subspaces_to_exclude));
3271e4c66b91SPatrick Farrell 
327210534d48SPatrick Farrell   patch->classname = "pc";
3273debbdec3SPatrick Farrell   patch->isNonlinear = PETSC_FALSE;
327410534d48SPatrick Farrell 
32754bbf5ea8SMatthew G. Knepley   /* Set some defaults */
32765f824522SMatthew G. Knepley   patch->combined           = PETSC_FALSE;
32774bbf5ea8SMatthew G. Knepley   patch->save_operators     = PETSC_TRUE;
327861c4b389SFlorian Wechsung   patch->local_composition_type = PC_COMPOSITE_ADDITIVE;
3279fa84ea4cSLawrence Mitchell   patch->precomputeElementTensors = PETSC_FALSE;
32804bbf5ea8SMatthew G. Knepley   patch->partition_of_unity = PETSC_FALSE;
32814bbf5ea8SMatthew G. Knepley   patch->codim              = -1;
32824bbf5ea8SMatthew G. Knepley   patch->dim                = -1;
32834bbf5ea8SMatthew G. Knepley   patch->vankadim           = -1;
32845f824522SMatthew G. Knepley   patch->ignoredim          = -1;
3285b525f888SPatrick Farrell   patch->pardecomp_overlap  = 0;
32864bbf5ea8SMatthew G. Knepley   patch->patchconstructop   = PCPatchConstruct_Star;
32874bbf5ea8SMatthew G. Knepley   patch->symmetrise_sweep   = PETSC_FALSE;
32885f824522SMatthew G. Knepley   patch->npatch             = 0;
32894bbf5ea8SMatthew G. Knepley   patch->userIS             = NULL;
32905f824522SMatthew G. Knepley   patch->optionsSet         = PETSC_FALSE;
32914bbf5ea8SMatthew G. Knepley   patch->iterationSet       = NULL;
32924bbf5ea8SMatthew G. Knepley   patch->user_patches       = PETSC_FALSE;
32935f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrallocpy(MATDENSE, (char **) &patch->sub_mat_type));
32945f824522SMatthew G. Knepley   patch->viewPatches        = PETSC_FALSE;
32955f824522SMatthew G. Knepley   patch->viewCells          = PETSC_FALSE;
32965f824522SMatthew G. Knepley   patch->viewPoints         = PETSC_FALSE;
32975f824522SMatthew G. Knepley   patch->viewSection        = PETSC_FALSE;
32985f824522SMatthew G. Knepley   patch->viewMatrix         = PETSC_FALSE;
32999d4fc724SLawrence Mitchell   patch->densesolve         = NULL;
3300dadc69c5SMatthew G. Knepley   patch->setupsolver        = PCSetUp_PATCH_Linear;
3301dadc69c5SMatthew G. Knepley   patch->applysolver        = PCApply_PATCH_Linear;
3302dadc69c5SMatthew G. Knepley   patch->resetsolver        = PCReset_PATCH_Linear;
3303dadc69c5SMatthew G. Knepley   patch->destroysolver      = PCDestroy_PATCH_Linear;
33046c9c532dSPatrick Farrell   patch->updatemultiplicative = PCUpdateMultiplicative_PATCH_Linear;
330547aca4a6SPatrick Farrell   patch->dofMappingWithoutToWithArtificial = NULL;
330647aca4a6SPatrick Farrell   patch->dofMappingWithoutToWithAll = NULL;
33074bbf5ea8SMatthew G. Knepley 
33084bbf5ea8SMatthew G. Knepley   pc->data                 = (void *) patch;
33094bbf5ea8SMatthew G. Knepley   pc->ops->apply           = PCApply_PATCH;
33100a545947SLisandro Dalcin   pc->ops->applytranspose  = NULL; /* PCApplyTranspose_PATCH; */
33114bbf5ea8SMatthew G. Knepley   pc->ops->setup           = PCSetUp_PATCH;
33124bbf5ea8SMatthew G. Knepley   pc->ops->reset           = PCReset_PATCH;
33134bbf5ea8SMatthew G. Knepley   pc->ops->destroy         = PCDestroy_PATCH;
33144bbf5ea8SMatthew G. Knepley   pc->ops->setfromoptions  = PCSetFromOptions_PATCH;
33154bbf5ea8SMatthew G. Knepley   pc->ops->setuponblocks   = PCSetUpOnBlocks_PATCH;
33164bbf5ea8SMatthew G. Knepley   pc->ops->view            = PCView_PATCH;
33170a545947SLisandro Dalcin   pc->ops->applyrichardson = NULL;
33184bbf5ea8SMatthew G. Knepley 
33194bbf5ea8SMatthew G. Knepley   PetscFunctionReturn(0);
33204bbf5ea8SMatthew G. Knepley }
3321