xref: /petsc/src/ksp/pc/impls/patch/pcpatch.c (revision e5b9877fd3e1272f23659a4ca92cd8e8fe28a052)
1 #include <petsc/private/pcpatchimpl.h>     /*I "petscpc.h" I*/
2 #include <petsc/private/kspimpl.h>         /* For ksp->setfromoptionscalled */
3 #include <petsc/private/dmpleximpl.h> /* For DMPlexComputeJacobian_Patch_Internal() */
4 #include <petscsf.h>
5 #include <petscbt.h>
6 #include <petscds.h>
7 
8 PetscLogEvent PC_Patch_CreatePatches, PC_Patch_ComputeOp, PC_Patch_Solve, PC_Patch_Scatter, PC_Patch_Apply, PC_Patch_Prealloc;
9 
10 PETSC_STATIC_INLINE PetscErrorCode ObjectView(PetscObject obj, PetscViewer viewer, PetscViewerFormat format)
11 {
12   PetscErrorCode ierr;
13 
14   ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr);
15   ierr = PetscObjectView(obj, viewer);CHKERRQ(ierr);
16   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
17   return(0);
18 }
19 
20 static PetscErrorCode PCPatchConstruct_Star(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
21 {
22   PetscInt       starSize;
23   PetscInt      *star = NULL, si;
24   PetscErrorCode ierr;
25 
26   PetscFunctionBegin;
27   PetscHSetIClear(ht);
28   /* To start with, add the point we care about */
29   ierr = PetscHSetIAdd(ht, point);CHKERRQ(ierr);
30   /* Loop over all the points that this point connects to */
31   ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
32   for (si = 0; si < starSize*2; si += 2) {ierr = PetscHSetIAdd(ht, star[si]);CHKERRQ(ierr);}
33   ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
34   PetscFunctionReturn(0);
35 }
36 
37 static PetscErrorCode PCPatchConstruct_Vanka(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
38 {
39   PC_PATCH      *patch = (PC_PATCH *) vpatch;
40   PetscInt       starSize;
41   PetscInt      *star = NULL;
42   PetscBool      shouldIgnore = PETSC_FALSE;
43   PetscInt       cStart, cEnd, iStart, iEnd, si;
44   PetscErrorCode ierr;
45 
46   PetscFunctionBegin;
47   ierr = PetscHSetIClear(ht);CHKERRQ(ierr);
48   /* To start with, add the point we care about */
49   ierr = PetscHSetIAdd(ht, point);CHKERRQ(ierr);
50   /* Should we ignore any points of a certain dimension? */
51   if (patch->vankadim >= 0) {
52     shouldIgnore = PETSC_TRUE;
53     ierr = DMPlexGetDepthStratum(dm, patch->vankadim, &iStart, &iEnd);CHKERRQ(ierr);
54   }
55   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
56   /* Loop over all the cells that this point connects to */
57   ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
58   for (si = 0; si < starSize*2; si += 2) {
59     const PetscInt cell = star[si];
60     PetscInt       closureSize;
61     PetscInt      *closure = NULL, ci;
62 
63     if (cell < cStart || cell >= cEnd) continue;
64     /* now loop over all entities in the closure of that cell */
65     ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
66     for (ci = 0; ci < closureSize*2; ci += 2) {
67       const PetscInt newpoint = closure[ci];
68 
69       /* We've been told to ignore entities of this type.*/
70       if (shouldIgnore && newpoint >= iStart && newpoint < iEnd) continue;
71       ierr = PetscHSetIAdd(ht, newpoint);CHKERRQ(ierr);
72     }
73     ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
74   }
75   ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
76   PetscFunctionReturn(0);
77 }
78 
79 static PetscErrorCode PCPatchConstruct_Pardecomp(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
80 {
81   DMLabel         ghost = NULL;
82   const PetscInt *leaves;
83   PetscInt        nleaves, pStart, pEnd, loc;
84   PetscBool       isFiredrake;
85   DM              plex;
86   PetscBool       flg;
87   PetscErrorCode ierr;
88 
89   PetscFunctionBegin;
90   PetscHSetIClear(ht);
91 
92   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
93   ierr = DMPlexGetChart(plex, &pStart, &pEnd);CHKERRQ(ierr);
94 
95   ierr = DMHasLabel(dm, "pyop2_ghost", &isFiredrake);CHKERRQ(ierr);
96   if (isFiredrake) {
97     ierr = DMGetLabel(dm, "pyop2_ghost", &ghost);CHKERRQ(ierr);
98     ierr = DMLabelCreateIndex(ghost, pStart, pEnd);CHKERRQ(ierr);
99   } else {
100     PetscSF sf;
101     ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
102     ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
103     nleaves = PetscMax(nleaves, 0);
104   }
105 
106   for (PetscInt point = pStart; point < pEnd; ++point) {
107     if (ghost) {ierr = DMLabelHasPoint(ghost, point, &flg);CHKERRQ(ierr);}
108     else       {ierr = PetscFindInt(point, nleaves, leaves, &loc);CHKERRQ(ierr); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;}
109     /* Not an owned entity, don't make a cell patch. */
110     if (flg) continue;
111     ierr = PetscHSetIAdd(ht, point);CHKERRQ(ierr);
112   }
113 
114   PetscFunctionReturn(0);
115 }
116 
117 /* The user's already set the patches in patch->userIS. Build the hash tables */
118 static PetscErrorCode PCPatchConstruct_User(void *vpatch, DM dm, PetscInt point, PetscHSetI ht)
119 {
120   PC_PATCH       *patch   = (PC_PATCH *) vpatch;
121   IS              patchis = patch->userIS[point];
122   PetscInt        n;
123   const PetscInt *patchdata;
124   PetscInt        pStart, pEnd, i;
125   PetscErrorCode  ierr;
126 
127   PetscFunctionBegin;
128   ierr = PetscHSetIClear(ht);CHKERRQ(ierr);
129   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
130   ierr = ISGetLocalSize(patchis, &n);CHKERRQ(ierr);
131   ierr = ISGetIndices(patchis, &patchdata);CHKERRQ(ierr);
132   for (i = 0; i < n; ++i) {
133     const PetscInt ownedpoint = patchdata[i];
134 
135     if (ownedpoint < pStart || ownedpoint >= pEnd) {
136       SETERRQ3(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D was not in [%D, %D)", ownedpoint, pStart, pEnd);
137     }
138     ierr = PetscHSetIAdd(ht, ownedpoint);CHKERRQ(ierr);
139   }
140   ierr = ISRestoreIndices(patchis, &patchdata);CHKERRQ(ierr);
141   PetscFunctionReturn(0);
142 }
143 
144 static PetscErrorCode PCPatchCreateDefaultSF_Private(PC pc, PetscInt n, const PetscSF *sf, const PetscInt *bs)
145 {
146   PC_PATCH      *patch = (PC_PATCH *) pc->data;
147   PetscInt       i;
148   PetscErrorCode ierr;
149 
150   PetscFunctionBegin;
151   if (n == 1 && bs[0] == 1) {
152     patch->defaultSF = sf[0];
153     ierr = PetscObjectReference((PetscObject) patch->defaultSF);CHKERRQ(ierr);
154   } else {
155     PetscInt     allRoots = 0, allLeaves = 0;
156     PetscInt     leafOffset = 0;
157     PetscInt    *ilocal = NULL;
158     PetscSFNode *iremote = NULL;
159     PetscInt    *remoteOffsets = NULL;
160     PetscInt     index = 0;
161     PetscHMapI   rankToIndex;
162     PetscInt     numRanks = 0;
163     PetscSFNode *remote = NULL;
164     PetscSF      rankSF;
165     PetscInt    *ranks = NULL;
166     PetscInt    *offsets = NULL;
167     MPI_Datatype contig;
168     PetscHSetI   ranksUniq;
169 
170     /* First figure out how many dofs there are in the concatenated numbering.
171      * allRoots: number of owned global dofs;
172      * allLeaves: number of visible dofs (global + ghosted).
173      */
174     for (i = 0; i < n; ++i) {
175       PetscInt nroots, nleaves;
176 
177       ierr = PetscSFGetGraph(sf[i], &nroots, &nleaves, NULL, NULL);CHKERRQ(ierr);
178       allRoots  += nroots * bs[i];
179       allLeaves += nleaves * bs[i];
180     }
181     ierr = PetscMalloc1(allLeaves, &ilocal);CHKERRQ(ierr);
182     ierr = PetscMalloc1(allLeaves, &iremote);CHKERRQ(ierr);
183     /* Now build an SF that just contains process connectivity. */
184     ierr = PetscHSetICreate(&ranksUniq);CHKERRQ(ierr);
185     for (i = 0; i < n; ++i) {
186       const PetscMPIInt *ranks = NULL;
187       PetscInt           nranks, j;
188 
189       ierr = PetscSFSetUp(sf[i]);CHKERRQ(ierr);
190       ierr = PetscSFGetRanks(sf[i], &nranks, &ranks, NULL, NULL, NULL);CHKERRQ(ierr);
191       /* These are all the ranks who communicate with me. */
192       for (j = 0; j < nranks; ++j) {
193         ierr = PetscHSetIAdd(ranksUniq, (PetscInt) ranks[j]);CHKERRQ(ierr);
194       }
195     }
196     ierr = PetscHSetIGetSize(ranksUniq, &numRanks);CHKERRQ(ierr);
197     ierr = PetscMalloc1(numRanks, &remote);CHKERRQ(ierr);
198     ierr = PetscMalloc1(numRanks, &ranks);CHKERRQ(ierr);
199     ierr = PetscHSetIGetElems(ranksUniq, &index, ranks);CHKERRQ(ierr);
200 
201     ierr = PetscHMapICreate(&rankToIndex);CHKERRQ(ierr);
202     for (i = 0; i < numRanks; ++i) {
203       remote[i].rank  = ranks[i];
204       remote[i].index = 0;
205       ierr = PetscHMapISet(rankToIndex, ranks[i], i);CHKERRQ(ierr);
206     }
207     ierr = PetscFree(ranks);CHKERRQ(ierr);
208     ierr = PetscHSetIDestroy(&ranksUniq);CHKERRQ(ierr);
209     ierr = PetscSFCreate(PetscObjectComm((PetscObject) pc), &rankSF);CHKERRQ(ierr);
210     ierr = PetscSFSetGraph(rankSF, 1, numRanks, NULL, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
211     ierr = PetscSFSetUp(rankSF);CHKERRQ(ierr);
212     /* OK, use it to communicate the root offset on the remote
213      * processes for each subspace. */
214     ierr = PetscMalloc1(n, &offsets);CHKERRQ(ierr);
215     ierr = PetscMalloc1(n*numRanks, &remoteOffsets);CHKERRQ(ierr);
216 
217     offsets[0] = 0;
218     for (i = 1; i < n; ++i) {
219       PetscInt nroots;
220 
221       ierr = PetscSFGetGraph(sf[i-1], &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
222       offsets[i] = offsets[i-1] + nroots*bs[i-1];
223     }
224     /* Offsets are the offsets on the current process of the
225      * global dof numbering for the subspaces. */
226     ierr = MPI_Type_contiguous(n, MPIU_INT, &contig);CHKERRQ(ierr);
227     ierr = MPI_Type_commit(&contig);CHKERRQ(ierr);
228 
229     ierr = PetscSFBcastBegin(rankSF, contig, offsets, remoteOffsets);CHKERRQ(ierr);
230     ierr = PetscSFBcastEnd(rankSF, contig, offsets, remoteOffsets);CHKERRQ(ierr);
231     ierr = MPI_Type_free(&contig);CHKERRQ(ierr);
232     ierr = PetscFree(offsets);CHKERRQ(ierr);
233     ierr = PetscSFDestroy(&rankSF);CHKERRQ(ierr);
234     /* Now remoteOffsets contains the offsets on the remote
235      * processes who communicate with me.  So now we can
236      * concatenate the list of SFs into a single one. */
237     index = 0;
238     for (i = 0; i < n; ++i) {
239       const PetscSFNode *remote = NULL;
240       const PetscInt    *local  = NULL;
241       PetscInt           nroots, nleaves, j;
242 
243       ierr = PetscSFGetGraph(sf[i], &nroots, &nleaves, &local, &remote);CHKERRQ(ierr);
244       for (j = 0; j < nleaves; ++j) {
245         PetscInt rank = remote[j].rank;
246         PetscInt idx, rootOffset, k;
247 
248         ierr = PetscHMapIGet(rankToIndex, rank, &idx);CHKERRQ(ierr);
249         if (idx == -1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Didn't find rank, huh?");
250         /* Offset on given rank for ith subspace */
251         rootOffset = remoteOffsets[n*idx + i];
252         for (k = 0; k < bs[i]; ++k) {
253           ilocal[index]        = (local ? local[j] : j)*bs[i] + k + leafOffset;
254           iremote[index].rank  = remote[j].rank;
255           iremote[index].index = remote[j].index*bs[i] + k + rootOffset;
256           ++index;
257         }
258       }
259       leafOffset += nleaves * bs[i];
260     }
261     ierr = PetscHMapIDestroy(&rankToIndex);CHKERRQ(ierr);
262     ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
263     ierr = PetscSFCreate(PetscObjectComm((PetscObject)pc), &patch->defaultSF);CHKERRQ(ierr);
264     ierr = PetscSFSetGraph(patch->defaultSF, allRoots, allLeaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER);CHKERRQ(ierr);
265   }
266   PetscFunctionReturn(0);
267 }
268 
269 /* TODO: Docs */
270 PetscErrorCode PCPatchSetIgnoreDim(PC pc, PetscInt dim)
271 {
272   PC_PATCH *patch = (PC_PATCH *) pc->data;
273   PetscFunctionBegin;
274   patch->ignoredim = dim;
275   PetscFunctionReturn(0);
276 }
277 
278 /* TODO: Docs */
279 PetscErrorCode PCPatchGetIgnoreDim(PC pc, PetscInt *dim)
280 {
281   PC_PATCH *patch = (PC_PATCH *) pc->data;
282   PetscFunctionBegin;
283   *dim = patch->ignoredim;
284   PetscFunctionReturn(0);
285 }
286 
287 /* TODO: Docs */
288 PetscErrorCode PCPatchSetSaveOperators(PC pc, PetscBool flg)
289 {
290   PC_PATCH *patch = (PC_PATCH *) pc->data;
291   PetscFunctionBegin;
292   patch->save_operators = flg;
293   PetscFunctionReturn(0);
294 }
295 
296 /* TODO: Docs */
297 PetscErrorCode PCPatchGetSaveOperators(PC pc, PetscBool *flg)
298 {
299   PC_PATCH *patch = (PC_PATCH *) pc->data;
300   PetscFunctionBegin;
301   *flg = patch->save_operators;
302   PetscFunctionReturn(0);
303 }
304 
305 /* TODO: Docs */
306 PetscErrorCode PCPatchSetPartitionOfUnity(PC pc, PetscBool flg)
307 {
308   PC_PATCH *patch = (PC_PATCH *) pc->data;
309   PetscFunctionBegin;
310   patch->partition_of_unity = flg;
311   PetscFunctionReturn(0);
312 }
313 
314 /* TODO: Docs */
315 PetscErrorCode PCPatchGetPartitionOfUnity(PC pc, PetscBool *flg)
316 {
317   PC_PATCH *patch = (PC_PATCH *) pc->data;
318   PetscFunctionBegin;
319   *flg = patch->partition_of_unity;
320   PetscFunctionReturn(0);
321 }
322 
323 /* TODO: Docs */
324 PetscErrorCode PCPatchSetLocalComposition(PC pc, PCCompositeType type)
325 {
326   PC_PATCH *patch = (PC_PATCH *) pc->data;
327   PetscFunctionBegin;
328   if (type != PC_COMPOSITE_ADDITIVE && type != PC_COMPOSITE_MULTIPLICATIVE) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Only supports additive or multiplicative as the local type");
329   patch->local_composition_type = type;
330   PetscFunctionReturn(0);
331 }
332 
333 /* TODO: Docs */
334 PetscErrorCode PCPatchGetLocalComposition(PC pc, PCCompositeType *type)
335 {
336   PC_PATCH *patch = (PC_PATCH *) pc->data;
337   PetscFunctionBegin;
338   *type = patch->local_composition_type;
339   PetscFunctionReturn(0);
340 }
341 
342 /* TODO: Docs */
343 PetscErrorCode PCPatchSetSubMatType(PC pc, MatType sub_mat_type)
344 {
345   PC_PATCH      *patch = (PC_PATCH *) pc->data;
346   PetscErrorCode ierr;
347 
348   PetscFunctionBegin;
349   if (patch->sub_mat_type) {ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr);}
350   ierr = PetscStrallocpy(sub_mat_type, (char **) &patch->sub_mat_type);CHKERRQ(ierr);
351   PetscFunctionReturn(0);
352 }
353 
354 /* TODO: Docs */
355 PetscErrorCode PCPatchGetSubMatType(PC pc, MatType *sub_mat_type)
356 {
357   PC_PATCH *patch = (PC_PATCH *) pc->data;
358   PetscFunctionBegin;
359   *sub_mat_type = patch->sub_mat_type;
360   PetscFunctionReturn(0);
361 }
362 
363 /* TODO: Docs */
364 PetscErrorCode PCPatchSetCellNumbering(PC pc, PetscSection cellNumbering)
365 {
366   PC_PATCH      *patch = (PC_PATCH *) pc->data;
367   PetscErrorCode ierr;
368 
369   PetscFunctionBegin;
370   patch->cellNumbering = cellNumbering;
371   ierr = PetscObjectReference((PetscObject) cellNumbering);CHKERRQ(ierr);
372   PetscFunctionReturn(0);
373 }
374 
375 /* TODO: Docs */
376 PetscErrorCode PCPatchGetCellNumbering(PC pc, PetscSection *cellNumbering)
377 {
378   PC_PATCH *patch = (PC_PATCH *) pc->data;
379   PetscFunctionBegin;
380   *cellNumbering = patch->cellNumbering;
381   PetscFunctionReturn(0);
382 }
383 
384 /* TODO: Docs */
385 PetscErrorCode PCPatchSetConstructType(PC pc, PCPatchConstructType ctype, PetscErrorCode (*func)(PC, PetscInt *, IS **, IS *, void *), void *ctx)
386 {
387   PC_PATCH *patch = (PC_PATCH *) pc->data;
388 
389   PetscFunctionBegin;
390   patch->ctype = ctype;
391   switch (ctype) {
392   case PC_PATCH_STAR:
393     patch->user_patches     = PETSC_FALSE;
394     patch->patchconstructop = PCPatchConstruct_Star;
395     break;
396   case PC_PATCH_VANKA:
397     patch->user_patches     = PETSC_FALSE;
398     patch->patchconstructop = PCPatchConstruct_Vanka;
399     break;
400   case PC_PATCH_PARDECOMP:
401     patch->user_patches     = PETSC_FALSE;
402     patch->patchconstructop = PCPatchConstruct_Pardecomp;
403     break;
404   case PC_PATCH_USER:
405   case PC_PATCH_PYTHON:
406     patch->user_patches     = PETSC_TRUE;
407     patch->patchconstructop = PCPatchConstruct_User;
408     if (func) {
409       patch->userpatchconstructionop = func;
410       patch->userpatchconstructctx   = ctx;
411     }
412     break;
413   default:
414     SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype);
415   }
416   PetscFunctionReturn(0);
417 }
418 
419 /* TODO: Docs */
420 PetscErrorCode PCPatchGetConstructType(PC pc, PCPatchConstructType *ctype, PetscErrorCode (**func)(PC, PetscInt *, IS **, IS *, void *), void **ctx)
421 {
422   PC_PATCH *patch = (PC_PATCH *) pc->data;
423 
424   PetscFunctionBegin;
425   *ctype = patch->ctype;
426   switch (patch->ctype) {
427   case PC_PATCH_STAR:
428   case PC_PATCH_VANKA:
429   case PC_PATCH_PARDECOMP:
430     break;
431   case PC_PATCH_USER:
432   case PC_PATCH_PYTHON:
433     *func = patch->userpatchconstructionop;
434     *ctx  = patch->userpatchconstructctx;
435     break;
436   default:
437     SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_USER, "Unknown patch construction type %D", (PetscInt) patch->ctype);
438   }
439   PetscFunctionReturn(0);
440 }
441 
442 /* TODO: Docs */
443 PetscErrorCode PCPatchSetDiscretisationInfo(PC pc, PetscInt nsubspaces, DM *dms, PetscInt *bs, PetscInt *nodesPerCell, const PetscInt **cellNodeMap,
444                                             const PetscInt *subspaceOffsets, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
445 {
446   PC_PATCH      *patch = (PC_PATCH *) pc->data;
447   DM             dm;
448   PetscSF       *sfs;
449   PetscInt       cStart, cEnd, i, j;
450   PetscErrorCode ierr;
451 
452   PetscFunctionBegin;
453   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
454   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
455   ierr = PetscMalloc1(nsubspaces, &sfs);CHKERRQ(ierr);
456   ierr = PetscMalloc1(nsubspaces, &patch->dofSection);CHKERRQ(ierr);
457   ierr = PetscMalloc1(nsubspaces, &patch->bs);CHKERRQ(ierr);
458   ierr = PetscMalloc1(nsubspaces, &patch->nodesPerCell);CHKERRQ(ierr);
459   ierr = PetscMalloc1(nsubspaces, &patch->cellNodeMap);CHKERRQ(ierr);
460   ierr = PetscMalloc1(nsubspaces+1, &patch->subspaceOffsets);CHKERRQ(ierr);
461 
462   patch->nsubspaces       = nsubspaces;
463   patch->totalDofsPerCell = 0;
464   for (i = 0; i < nsubspaces; ++i) {
465     ierr = DMGetDefaultSection(dms[i], &patch->dofSection[i]);CHKERRQ(ierr);
466     ierr = PetscObjectReference((PetscObject) patch->dofSection[i]);CHKERRQ(ierr);
467     ierr = DMGetDefaultSF(dms[i], &sfs[i]);CHKERRQ(ierr);
468     patch->bs[i]              = bs[i];
469     patch->nodesPerCell[i]    = nodesPerCell[i];
470     patch->totalDofsPerCell  += nodesPerCell[i]*bs[i];
471     ierr = PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i]);CHKERRQ(ierr);
472     for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
473     patch->subspaceOffsets[i] = subspaceOffsets[i];
474   }
475   ierr = PCPatchCreateDefaultSF_Private(pc, nsubspaces, sfs, patch->bs);CHKERRQ(ierr);
476   ierr = PetscFree(sfs);CHKERRQ(ierr);
477 
478   patch->subspaceOffsets[nsubspaces] = subspaceOffsets[nsubspaces];
479   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes);CHKERRQ(ierr);
480   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes);CHKERRQ(ierr);
481   PetscFunctionReturn(0);
482 }
483 
484 /* TODO: Docs */
485 PetscErrorCode PCPatchSetDiscretisationInfoCombined(PC pc, DM dm, PetscInt *nodesPerCell, const PetscInt **cellNodeMap, PetscInt numGhostBcs, const PetscInt *ghostBcNodes, PetscInt numGlobalBcs, const PetscInt *globalBcNodes)
486 {
487   PC_PATCH      *patch = (PC_PATCH *) pc->data;
488   PetscInt       cStart, cEnd, i, j;
489   PetscErrorCode ierr;
490 
491   PetscFunctionBegin;
492   patch->combined = PETSC_TRUE;
493   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
494   ierr = DMGetNumFields(dm, &patch->nsubspaces);CHKERRQ(ierr);
495   ierr = PetscCalloc1(patch->nsubspaces, &patch->dofSection);CHKERRQ(ierr);
496   ierr = PetscMalloc1(patch->nsubspaces, &patch->bs);CHKERRQ(ierr);
497   ierr = PetscMalloc1(patch->nsubspaces, &patch->nodesPerCell);CHKERRQ(ierr);
498   ierr = PetscMalloc1(patch->nsubspaces, &patch->cellNodeMap);CHKERRQ(ierr);
499   ierr = PetscCalloc1(patch->nsubspaces+1, &patch->subspaceOffsets);CHKERRQ(ierr);
500   ierr = DMGetDefaultSection(dm, &patch->dofSection[0]);CHKERRQ(ierr);
501   ierr = PetscObjectReference((PetscObject) patch->dofSection[0]);CHKERRQ(ierr);
502   ierr = PetscSectionGetStorageSize(patch->dofSection[0], &patch->subspaceOffsets[patch->nsubspaces]);CHKERRQ(ierr);
503   patch->totalDofsPerCell = 0;
504   for (i = 0; i < patch->nsubspaces; ++i) {
505     patch->bs[i]             = 1;
506     patch->nodesPerCell[i]   = nodesPerCell[i];
507     patch->totalDofsPerCell += nodesPerCell[i];
508     ierr = PetscMalloc1((cEnd-cStart)*nodesPerCell[i], &patch->cellNodeMap[i]);CHKERRQ(ierr);
509     for (j = 0; j < (cEnd-cStart)*nodesPerCell[i]; ++j) patch->cellNodeMap[i][j] = cellNodeMap[i][j];
510   }
511   ierr = DMGetDefaultSF(dm, &patch->defaultSF);CHKERRQ(ierr);
512   ierr = PetscObjectReference((PetscObject) patch->defaultSF);CHKERRQ(ierr);
513   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGhostBcs, ghostBcNodes, PETSC_COPY_VALUES, &patch->ghostBcNodes);CHKERRQ(ierr);
514   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalBcs, globalBcNodes, PETSC_COPY_VALUES, &patch->globalBcNodes);CHKERRQ(ierr);
515   PetscFunctionReturn(0);
516 }
517 
518 /*@C
519 
520   PCPatchSetComputeFunction - Set the callback used to compute patch residuals
521 
522   Input Parameters:
523 + pc   - The PC
524 . func - The callback
525 - ctx  - The user context
526 
527   Level: advanced
528 
529   Note:
530   The callback has signature:
531 +  usercomputef(pc, point, x, f, cellIS, n, u, ctx)
532 +  pc     - The PC
533 +  point  - The point
534 +  x      - The input solution (not used in linear problems)
535 +  f      - The patch residual vector
536 +  cellIS - An array of the cell numbers
537 +  n      - The size of g2l
538 +  g2l    - The global to local dof translation table
539 +  ctx    - The user context
540   and can assume that the matrix entries have been set to zero before the call.
541 
542 .seealso: PCPatchSetComputeOperator(), PCPatchGetComputeOperator(), PCPatchSetDiscretisationInfo()
543 @*/
544 PetscErrorCode PCPatchSetComputeFunction(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Vec, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
545 {
546   PC_PATCH *patch = (PC_PATCH *) pc->data;
547 
548   PetscFunctionBegin;
549   patch->usercomputef    = func;
550   patch->usercomputefctx = ctx;
551   PetscFunctionReturn(0);
552 }
553 
554 /*@C
555 
556   PCPatchSetComputeOperator - Set the callback used to compute patch matrices
557 
558   Input Parameters:
559 + pc   - The PC
560 . func - The callback
561 - ctx  - The user context
562 
563   Level: advanced
564 
565   Note:
566   The callback has signature:
567 +  usercomputeop(pc, point, x, mat, cellIS, n, u, ctx)
568 +  pc     - The PC
569 +  point  - The point
570 +  x      - The input solution (not used in linear problems)
571 +  mat    - The patch matrix
572 +  cellIS - An array of the cell numbers
573 +  n      - The size of g2l
574 +  g2l    - The global to local dof translation table
575 +  ctx    - The user context
576   and can assume that the matrix entries have been set to zero before the call.
577 
578 .seealso: PCPatchGetComputeOperator(), PCPatchSetComputeFunction(), PCPatchSetDiscretisationInfo()
579 @*/
580 PetscErrorCode PCPatchSetComputeOperator(PC pc, PetscErrorCode (*func)(PC, PetscInt, Vec, Mat, IS, PetscInt, const PetscInt *, const PetscInt *, void *), void *ctx)
581 {
582   PC_PATCH *patch = (PC_PATCH *) pc->data;
583 
584   PetscFunctionBegin;
585   patch->usercomputeop    = func;
586   patch->usercomputeopctx = ctx;
587   PetscFunctionReturn(0);
588 }
589 
590 /* On entry, ht contains the topological entities whose dofs we are responsible for solving for;
591    on exit, cht contains all the topological entities we need to compute their residuals.
592    In full generality this should incorporate knowledge of the sparsity pattern of the matrix;
593    here we assume a standard FE sparsity pattern.*/
594 /* TODO: Use DMPlexGetAdjacency() */
595 static PetscErrorCode PCPatchCompleteCellPatch(PC pc, PetscHSetI ht, PetscHSetI cht)
596 {
597   DM             dm;
598   PetscHashIter  hi;
599   PetscInt       point;
600   PetscInt      *star = NULL, *closure = NULL;
601   PetscInt       ignoredim, iStart = 0, iEnd = -1, starSize, closureSize, si, ci;
602   PetscErrorCode ierr;
603 
604   PetscFunctionBegin;
605   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
606   ierr = PCPatchGetIgnoreDim(pc, &ignoredim);CHKERRQ(ierr);
607   if (ignoredim >= 0) {ierr = DMPlexGetDepthStratum(dm, ignoredim, &iStart, &iEnd);CHKERRQ(ierr);}
608   ierr = PetscHSetIClear(cht);CHKERRQ(ierr);
609   PetscHashIterBegin(ht, hi);
610   while (!PetscHashIterAtEnd(ht, hi)) {
611 
612     PetscHashIterGetKey(ht, hi, point);
613     PetscHashIterNext(ht, hi);
614 
615     /* Loop over all the cells that this point connects to */
616     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
617     for (si = 0; si < starSize*2; si += 2) {
618       const PetscInt ownedpoint = star[si];
619       /* TODO Check for point in cht before running through closure again */
620       /* now loop over all entities in the closure of that cell */
621       ierr = DMPlexGetTransitiveClosure(dm, ownedpoint, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
622       for (ci = 0; ci < closureSize*2; ci += 2) {
623         const PetscInt seenpoint = closure[ci];
624         if (ignoredim >= 0 && seenpoint >= iStart && seenpoint < iEnd) continue;
625         ierr = PetscHSetIAdd(cht, seenpoint);CHKERRQ(ierr);
626       }
627     }
628   }
629   ierr = DMPlexRestoreTransitiveClosure(dm, 0, PETSC_TRUE, NULL, &closure);CHKERRQ(ierr);
630   ierr = DMPlexRestoreTransitiveClosure(dm, 0, PETSC_FALSE, NULL, &star);CHKERRQ(ierr);
631   PetscFunctionReturn(0);
632 }
633 
634 static PetscErrorCode PCPatchGetGlobalDofs(PC pc, PetscSection dofSection[], PetscInt f, PetscBool combined, PetscInt p, PetscInt *dof, PetscInt *off)
635 {
636   PetscErrorCode ierr;
637 
638   PetscFunctionBegin;
639   if (combined) {
640     if (f < 0) {
641       if (dof) {ierr = PetscSectionGetDof(dofSection[0], p, dof);CHKERRQ(ierr);}
642       if (off) {ierr = PetscSectionGetOffset(dofSection[0], p, off);CHKERRQ(ierr);}
643     } else {
644       if (dof) {ierr = PetscSectionGetFieldDof(dofSection[0], p, f, dof);CHKERRQ(ierr);}
645       if (off) {ierr = PetscSectionGetFieldOffset(dofSection[0], p, f, off);CHKERRQ(ierr);}
646     }
647   } else {
648     if (f < 0) {
649       PC_PATCH *patch = (PC_PATCH *) pc->data;
650       PetscInt  fdof, g;
651 
652       if (dof) {
653         *dof = 0;
654         for (g = 0; g < patch->nsubspaces; ++g) {
655           ierr = PetscSectionGetDof(dofSection[g], p, &fdof);CHKERRQ(ierr);
656           *dof += fdof;
657         }
658       }
659       if (off) {
660         *off = 0;
661         for (g = 0; g < patch->nsubspaces; ++g) {
662           ierr = PetscSectionGetOffset(dofSection[g], p, &fdof);CHKERRQ(ierr);
663           *off += fdof;
664         }
665       }
666     } else {
667       if (dof) {ierr = PetscSectionGetDof(dofSection[f], p, dof);CHKERRQ(ierr);}
668       if (off) {ierr = PetscSectionGetOffset(dofSection[f], p, off);CHKERRQ(ierr);}
669     }
670   }
671   PetscFunctionReturn(0);
672 }
673 
674 /* Given a hash table with a set of topological entities (pts), compute the degrees of
675    freedom in global concatenated numbering on those entities.
676    For Vanka smoothing, this needs to do something special: ignore dofs of the
677    constraint subspace on entities that aren't the base entity we're building the patch
678    around. */
679 static PetscErrorCode PCPatchGetPointDofs(PC pc, PetscHSetI pts, PetscHSetI dofs, PetscInt base, PetscHSetI* subspaces_to_exclude)
680 {
681   PC_PATCH      *patch = (PC_PATCH *) pc->data;
682   PetscHashIter  hi;
683   PetscInt       ldof, loff;
684   PetscInt       k, p;
685   PetscErrorCode ierr;
686 
687   PetscFunctionBegin;
688   ierr = PetscHSetIClear(dofs);CHKERRQ(ierr);
689   for (k = 0; k < patch->nsubspaces; ++k) {
690     PetscInt subspaceOffset = patch->subspaceOffsets[k];
691     PetscInt bs             = patch->bs[k];
692     PetscInt j, l;
693 
694     if (subspaces_to_exclude != NULL) {
695       PetscBool should_exclude_k = PETSC_FALSE;
696       PetscHSetIHas(*subspaces_to_exclude, k, &should_exclude_k);
697       if (should_exclude_k) {
698         /* only get this subspace dofs at the base entity, not any others */
699         ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, base, &ldof, &loff);CHKERRQ(ierr);
700         if (0 == ldof) continue;
701         for (j = loff; j < ldof + loff; ++j) {
702           for (l = 0; l < bs; ++l) {
703             PetscInt dof = bs*j + l + subspaceOffset;
704             ierr = PetscHSetIAdd(dofs, dof);CHKERRQ(ierr);
705           }
706         }
707         continue; /* skip the other dofs of this subspace */
708       }
709     }
710 
711     PetscHashIterBegin(pts, hi);
712     while (!PetscHashIterAtEnd(pts, hi)) {
713       PetscHashIterGetKey(pts, hi, p);
714       PetscHashIterNext(pts, hi);
715       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, p, &ldof, &loff);CHKERRQ(ierr);
716       if (0 == ldof) continue;
717       for (j = loff; j < ldof + loff; ++j) {
718         for (l = 0; l < bs; ++l) {
719           PetscInt dof = bs*j + l + subspaceOffset;
720           ierr = PetscHSetIAdd(dofs, dof);CHKERRQ(ierr);
721         }
722       }
723     }
724   }
725   PetscFunctionReturn(0);
726 }
727 
728 /* Given two hash tables A and B, compute the keys in B that are not in A, and put them in C */
729 static PetscErrorCode PCPatchComputeSetDifference_Private(PetscHSetI A, PetscHSetI B, PetscHSetI C)
730 {
731   PetscHashIter  hi;
732   PetscInt       key;
733   PetscBool      flg;
734   PetscErrorCode ierr;
735 
736   PetscFunctionBegin;
737   ierr = PetscHSetIClear(C);CHKERRQ(ierr);
738   PetscHashIterBegin(B, hi);
739   while (!PetscHashIterAtEnd(B, hi)) {
740     PetscHashIterGetKey(B, hi, key);
741     PetscHashIterNext(B, hi);
742     ierr = PetscHSetIHas(A, key, &flg);CHKERRQ(ierr);
743     if (!flg) {ierr = PetscHSetIAdd(C, key);CHKERRQ(ierr);}
744   }
745   PetscFunctionReturn(0);
746 }
747 
748 /*
749  * PCPatchCreateCellPatches - create patches.
750  *
751  * Input Parameters:
752  * + dm - The DMPlex object defining the mesh
753  *
754  * Output Parameters:
755  * + cellCounts  - Section with counts of cells around each vertex
756  * . cells       - IS of the cell point indices of cells in each patch
757  * . pointCounts - Section with counts of cells around each vertex
758  * - point       - IS of the cell point indices of cells in each patch
759  */
760 static PetscErrorCode PCPatchCreateCellPatches(PC pc)
761 {
762   PC_PATCH       *patch = (PC_PATCH *) pc->data;
763   DMLabel         ghost = NULL;
764   DM              dm, plex;
765   PetscHSetI      ht, cht;
766   PetscSection    cellCounts,  pointCounts;
767   PetscInt       *cellsArray, *pointsArray;
768   PetscInt        numCells,    numPoints;
769   const PetscInt *leaves;
770   PetscInt        nleaves, pStart, pEnd, cStart, cEnd, vStart, vEnd, v;
771   PetscBool       isFiredrake;
772   PetscErrorCode  ierr;
773 
774   PetscFunctionBegin;
775   /* Used to keep track of the cells in the patch. */
776   ierr = PetscHSetICreate(&ht);CHKERRQ(ierr);
777   ierr = PetscHSetICreate(&cht);CHKERRQ(ierr);
778 
779   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
780   if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONGSTATE, "DM not yet set on patch PC\n");
781   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
782   ierr = DMPlexGetChart(plex, &pStart, &pEnd);CHKERRQ(ierr);
783   ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr);
784 
785   if (patch->user_patches) {
786     ierr = patch->userpatchconstructionop(pc, &patch->npatch, &patch->userIS, &patch->iterationSet, patch->userpatchconstructctx);CHKERRQ(ierr);
787     vStart = 0; vEnd = patch->npatch;
788   } else if (patch->ctype == PC_PATCH_PARDECOMP) {
789     vStart = 0; vEnd = 1;
790   } else if (patch->codim < 0) {
791     if (patch->dim < 0) {ierr = DMPlexGetDepthStratum(plex,  0,            &vStart, &vEnd);CHKERRQ(ierr);}
792     else                {ierr = DMPlexGetDepthStratum(plex,  patch->dim,   &vStart, &vEnd);CHKERRQ(ierr);}
793   } else                {ierr = DMPlexGetHeightStratum(plex, patch->codim, &vStart, &vEnd);CHKERRQ(ierr);}
794   patch->npatch = vEnd - vStart;
795 
796   /* These labels mark the owned points.  We only create patches around points that this process owns. */
797   ierr = DMHasLabel(dm, "pyop2_ghost", &isFiredrake);CHKERRQ(ierr);
798   if (isFiredrake) {
799     ierr = DMGetLabel(dm, "pyop2_ghost", &ghost);CHKERRQ(ierr);
800     ierr = DMLabelCreateIndex(ghost, pStart, pEnd);CHKERRQ(ierr);
801   } else {
802     PetscSF sf;
803 
804     ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
805     ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
806     nleaves = PetscMax(nleaves, 0);
807   }
808 
809   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->cellCounts);CHKERRQ(ierr);
810   ierr = PetscObjectSetName((PetscObject) patch->cellCounts, "Patch Cell Layout");CHKERRQ(ierr);
811   cellCounts = patch->cellCounts;
812   ierr = PetscSectionSetChart(cellCounts, vStart, vEnd);CHKERRQ(ierr);
813   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->pointCounts);CHKERRQ(ierr);
814   ierr = PetscObjectSetName((PetscObject) patch->pointCounts, "Patch Point Layout");CHKERRQ(ierr);
815   pointCounts = patch->pointCounts;
816   ierr = PetscSectionSetChart(pointCounts, vStart, vEnd);CHKERRQ(ierr);
817   /* Count cells and points in the patch surrounding each entity */
818   for (v = vStart; v < vEnd; ++v) {
819     PetscHashIter hi;
820     PetscInt       chtSize, loc = -1;
821     PetscBool      flg;
822 
823     if (!patch->user_patches) {
824       if (ghost) {ierr = DMLabelHasPoint(ghost, v, &flg);CHKERRQ(ierr);}
825       else       {ierr = PetscFindInt(v, nleaves, leaves, &loc);CHKERRQ(ierr); flg = loc >=0 ? PETSC_TRUE : PETSC_FALSE;}
826       /* Not an owned entity, don't make a cell patch. */
827       if (flg) continue;
828     }
829 
830     ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr);
831     ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr);
832     ierr = PetscHSetIGetSize(cht, &chtSize);CHKERRQ(ierr);
833     /* empty patch, continue */
834     if (chtSize == 0) continue;
835 
836     /* safe because size(cht) > 0 from above */
837     PetscHashIterBegin(cht, hi);
838     while (!PetscHashIterAtEnd(cht, hi)) {
839       PetscInt point, pdof;
840 
841       PetscHashIterGetKey(cht, hi, point);
842       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr);
843       if (pdof)                            {ierr = PetscSectionAddDof(pointCounts, v, 1);CHKERRQ(ierr);}
844       if (point >= cStart && point < cEnd) {ierr = PetscSectionAddDof(cellCounts, v, 1);CHKERRQ(ierr);}
845       PetscHashIterNext(cht, hi);
846     }
847   }
848   if (isFiredrake) {ierr = DMLabelDestroyIndex(ghost);CHKERRQ(ierr);}
849 
850   ierr = PetscSectionSetUp(cellCounts);CHKERRQ(ierr);
851   ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr);
852   ierr = PetscMalloc1(numCells, &cellsArray);CHKERRQ(ierr);
853   ierr = PetscSectionSetUp(pointCounts);CHKERRQ(ierr);
854   ierr = PetscSectionGetStorageSize(pointCounts, &numPoints);CHKERRQ(ierr);
855   ierr = PetscMalloc1(numPoints, &pointsArray);CHKERRQ(ierr);
856 
857   /* Now that we know how much space we need, run through again and actually remember the cells. */
858   for (v = vStart; v < vEnd; v++ ) {
859     PetscHashIter hi;
860     PetscInt       dof, off, cdof, coff, pdof, n = 0, cn = 0;
861 
862     ierr = PetscSectionGetDof(pointCounts, v, &dof);CHKERRQ(ierr);
863     ierr = PetscSectionGetOffset(pointCounts, v, &off);CHKERRQ(ierr);
864     ierr = PetscSectionGetDof(cellCounts, v, &cdof);CHKERRQ(ierr);
865     ierr = PetscSectionGetOffset(cellCounts, v, &coff);CHKERRQ(ierr);
866     if (dof <= 0) continue;
867     ierr = patch->patchconstructop((void *) patch, dm, v, ht);CHKERRQ(ierr);
868     ierr = PCPatchCompleteCellPatch(pc, ht, cht);CHKERRQ(ierr);
869     PetscHashIterBegin(cht, hi);
870     while (!PetscHashIterAtEnd(cht, hi)) {
871       PetscInt point;
872 
873       PetscHashIterGetKey(cht, hi, point);
874       ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, -1, patch->combined, point, &pdof, NULL);CHKERRQ(ierr);
875       if (pdof)                            {pointsArray[off + n++] = point;}
876       if (point >= cStart && point < cEnd) {cellsArray[coff + cn++] = point;}
877       PetscHashIterNext(cht, hi);
878     }
879     if (cn != cdof) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of cells in patch %D is %D, but should be %D", v, cn, cdof);
880     if (n  != dof)  SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of points in patch %D is %D, but should be %D", v, n, dof);
881   }
882   ierr = PetscHSetIDestroy(&ht);CHKERRQ(ierr);
883   ierr = PetscHSetIDestroy(&cht);CHKERRQ(ierr);
884   ierr = DMDestroy(&plex);CHKERRQ(ierr);
885 
886   ierr = ISCreateGeneral(PETSC_COMM_SELF, numCells,  cellsArray,  PETSC_OWN_POINTER, &patch->cells);CHKERRQ(ierr);
887   ierr = PetscObjectSetName((PetscObject) patch->cells,  "Patch Cells");CHKERRQ(ierr);
888   if (patch->viewCells) {
889     ierr = ObjectView((PetscObject) patch->cellCounts, patch->viewerCells, patch->formatCells);CHKERRQ(ierr);
890     ierr = ObjectView((PetscObject) patch->cells,      patch->viewerCells, patch->formatCells);CHKERRQ(ierr);
891   }
892   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints, pointsArray, PETSC_OWN_POINTER, &patch->points);CHKERRQ(ierr);
893   ierr = PetscObjectSetName((PetscObject) patch->points, "Patch Points");CHKERRQ(ierr);
894   if (patch->viewPoints) {
895     ierr = ObjectView((PetscObject) patch->pointCounts, patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr);
896     ierr = ObjectView((PetscObject) patch->points,      patch->viewerPoints, patch->formatPoints);CHKERRQ(ierr);
897   }
898   PetscFunctionReturn(0);
899 }
900 
901 /*
902  * PCPatchCreateCellPatchDiscretisationInfo - Build the dof maps for cell patches
903  *
904  * Input Parameters:
905  * + dm - The DMPlex object defining the mesh
906  * . cellCounts - Section with counts of cells around each vertex
907  * . cells - IS of the cell point indices of cells in each patch
908  * . cellNumbering - Section mapping plex cell points to Firedrake cell indices.
909  * . nodesPerCell - number of nodes per cell.
910  * - cellNodeMap - map from cells to node indices (nodesPerCell * numCells)
911  *
912  * Output Parameters:
913  * + dofs - IS of local dof numbers of each cell in the patch, where local is a patch local numbering
914  * . gtolCounts - Section with counts of dofs per cell patch
915  * - gtol - IS mapping from global dofs to local dofs for each patch.
916  */
917 static PetscErrorCode PCPatchCreateCellPatchDiscretisationInfo(PC pc)
918 {
919   PC_PATCH       *patch           = (PC_PATCH *) pc->data;
920   PetscSection    cellCounts      = patch->cellCounts;
921   PetscSection    pointCounts     = patch->pointCounts;
922   PetscSection    gtolCounts, gtolCountsWithArtificial = NULL, gtolCountsWithAll = NULL;
923   IS              cells           = patch->cells;
924   IS              points          = patch->points;
925   PetscSection    cellNumbering   = patch->cellNumbering;
926   PetscInt        Nf              = patch->nsubspaces;
927   PetscInt        numCells, numPoints;
928   PetscInt        numDofs;
929   PetscInt        numGlobalDofs, numGlobalDofsWithArtificial, numGlobalDofsWithAll;
930   PetscInt        totalDofsPerCell = patch->totalDofsPerCell;
931   PetscInt        vStart, vEnd, v;
932   const PetscInt *cellsArray, *pointsArray;
933   PetscInt       *newCellsArray   = NULL;
934   PetscInt       *dofsArray       = NULL;
935   PetscInt       *dofsArrayWithArtificial = NULL;
936   PetscInt       *dofsArrayWithAll = NULL;
937   PetscInt       *offsArray       = NULL;
938   PetscInt       *offsArrayWithArtificial = NULL;
939   PetscInt       *offsArrayWithAll = NULL;
940   PetscInt       *asmArray        = NULL;
941   PetscInt       *asmArrayWithArtificial = NULL;
942   PetscInt       *asmArrayWithAll = NULL;
943   PetscInt       *globalDofsArray = NULL;
944   PetscInt       *globalDofsArrayWithArtificial = NULL;
945   PetscInt       *globalDofsArrayWithAll = NULL;
946   PetscInt        globalIndex     = 0;
947   PetscInt        key             = 0;
948   PetscInt        asmKey          = 0;
949   DM              dm              = NULL;
950   const PetscInt *bcNodes         = NULL;
951   PetscHMapI      ht;
952   PetscHMapI      htWithArtificial;
953   PetscHMapI      htWithAll;
954   PetscHSetI      globalBcs;
955   PetscInt        numBcs;
956   PetscHSetI      ownedpts, seenpts, owneddofs, seendofs, artificialbcs;
957   PetscInt        pStart, pEnd, p, i;
958   char            option[PETSC_MAX_PATH_LEN];
959   PetscBool       isNonlinear;
960   PetscErrorCode  ierr;
961 
962   PetscFunctionBegin;
963 
964   ierr = PCGetDM(pc, &dm); CHKERRQ(ierr);
965   /* dofcounts section is cellcounts section * dofPerCell */
966   ierr = PetscSectionGetStorageSize(cellCounts, &numCells);CHKERRQ(ierr);
967   ierr = PetscSectionGetStorageSize(patch->pointCounts, &numPoints);CHKERRQ(ierr);
968   numDofs = numCells * totalDofsPerCell;
969   ierr = PetscMalloc1(numDofs, &dofsArray);CHKERRQ(ierr);
970   ierr = PetscMalloc1(numPoints*Nf, &offsArray);CHKERRQ(ierr);
971   ierr = PetscMalloc1(numDofs, &asmArray);CHKERRQ(ierr);
972   ierr = PetscMalloc1(numCells, &newCellsArray);CHKERRQ(ierr);
973   ierr = PetscSectionGetChart(cellCounts, &vStart, &vEnd);CHKERRQ(ierr);
974   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCounts);CHKERRQ(ierr);
975   gtolCounts = patch->gtolCounts;
976   ierr = PetscSectionSetChart(gtolCounts, vStart, vEnd);CHKERRQ(ierr);
977   ierr = PetscObjectSetName((PetscObject) patch->gtolCounts, "Patch Global Index Section");CHKERRQ(ierr);
978 
979   if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE)
980   {
981     ierr = PetscMalloc1(numPoints*Nf, &offsArrayWithArtificial);CHKERRQ(ierr);
982     ierr = PetscMalloc1(numDofs, &asmArrayWithArtificial);CHKERRQ(ierr);
983     ierr = PetscMalloc1(numDofs, &dofsArrayWithArtificial);CHKERRQ(ierr);
984     ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithArtificial);CHKERRQ(ierr);
985     gtolCountsWithArtificial = patch->gtolCountsWithArtificial;
986     ierr = PetscSectionSetChart(gtolCountsWithArtificial, vStart, vEnd);CHKERRQ(ierr);
987     ierr = PetscObjectSetName((PetscObject) patch->gtolCountsWithArtificial, "Patch Global Index Section Including Artificial BCs");CHKERRQ(ierr);
988   }
989 
990   isNonlinear = patch->isNonlinear;
991   if(isNonlinear)
992   {
993     ierr = PetscMalloc1(numPoints*Nf, &offsArrayWithAll);CHKERRQ(ierr);
994     ierr = PetscMalloc1(numDofs, &asmArrayWithAll);CHKERRQ(ierr);
995     ierr = PetscMalloc1(numDofs, &dofsArrayWithAll);CHKERRQ(ierr);
996     ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->gtolCountsWithAll);CHKERRQ(ierr);
997     gtolCountsWithAll = patch->gtolCountsWithAll;
998     ierr = PetscSectionSetChart(gtolCountsWithAll, vStart, vEnd);CHKERRQ(ierr);
999     ierr = PetscObjectSetName((PetscObject) patch->gtolCountsWithAll, "Patch Global Index Section Including All BCs");CHKERRQ(ierr);
1000   }
1001 
1002   /* Outside the patch loop, get the dofs that are globally-enforced Dirichlet
1003    conditions */
1004   ierr = PetscHSetICreate(&globalBcs);CHKERRQ(ierr);
1005   ierr = ISGetIndices(patch->ghostBcNodes, &bcNodes); CHKERRQ(ierr);
1006   ierr = ISGetSize(patch->ghostBcNodes, &numBcs); CHKERRQ(ierr);
1007   for (i = 0; i < numBcs; ++i) {
1008     ierr = PetscHSetIAdd(globalBcs, bcNodes[i]);CHKERRQ(ierr); /* these are already in concatenated numbering */
1009   }
1010   ierr = ISRestoreIndices(patch->ghostBcNodes, &bcNodes); CHKERRQ(ierr);
1011   ierr = ISDestroy(&patch->ghostBcNodes); CHKERRQ(ierr); /* memory optimisation */
1012 
1013   /* Hash tables for artificial BC construction */
1014   ierr = PetscHSetICreate(&ownedpts);CHKERRQ(ierr);
1015   ierr = PetscHSetICreate(&seenpts);CHKERRQ(ierr);
1016   ierr = PetscHSetICreate(&owneddofs);CHKERRQ(ierr);
1017   ierr = PetscHSetICreate(&seendofs);CHKERRQ(ierr);
1018   ierr = PetscHSetICreate(&artificialbcs);CHKERRQ(ierr);
1019 
1020   ierr = ISGetIndices(cells, &cellsArray);CHKERRQ(ierr);
1021   ierr = ISGetIndices(points, &pointsArray);CHKERRQ(ierr);
1022   ierr = PetscHMapICreate(&ht);CHKERRQ(ierr);
1023   ierr = PetscHMapICreate(&htWithArtificial);CHKERRQ(ierr);
1024   ierr = PetscHMapICreate(&htWithAll);CHKERRQ(ierr);
1025   for (v = vStart; v < vEnd; ++v) {
1026     PetscInt localIndex = 0;
1027     PetscInt localIndexWithArtificial = 0;
1028     PetscInt localIndexWithAll = 0;
1029     PetscInt dof, off, i, j, k, l;
1030 
1031     ierr = PetscHMapIClear(ht);CHKERRQ(ierr);
1032     ierr = PetscHMapIClear(htWithArtificial);CHKERRQ(ierr);
1033     ierr = PetscHMapIClear(htWithAll);CHKERRQ(ierr);
1034     ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr);
1035     ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr);
1036     if (dof <= 0) continue;
1037 
1038     /* Calculate the global numbers of the artificial BC dofs here first */
1039     ierr = patch->patchconstructop((void*)patch, dm, v, ownedpts); CHKERRQ(ierr);
1040     ierr = PCPatchCompleteCellPatch(pc, ownedpts, seenpts); CHKERRQ(ierr);
1041     ierr = PCPatchGetPointDofs(pc, ownedpts, owneddofs, v, &patch->subspaces_to_exclude); CHKERRQ(ierr);
1042     ierr = PCPatchGetPointDofs(pc, seenpts, seendofs, v, NULL); CHKERRQ(ierr);
1043     ierr = PCPatchComputeSetDifference_Private(owneddofs, seendofs, artificialbcs); CHKERRQ(ierr);
1044     if (patch->viewPatches) {
1045       PetscHSetI globalbcdofs;
1046       PetscHashIter hi;
1047       MPI_Comm comm = PetscObjectComm((PetscObject)pc);
1048 
1049       ierr = PetscHSetICreate(&globalbcdofs);CHKERRQ(ierr);
1050       ierr = PetscSynchronizedPrintf(comm, "Patch %d: owned dofs:\n", v); CHKERRQ(ierr);
1051       PetscHashIterBegin(owneddofs, hi);
1052       while (!PetscHashIterAtEnd(owneddofs, hi)) {
1053         PetscInt globalDof;
1054 
1055         PetscHashIterGetKey(owneddofs, hi, globalDof);
1056         PetscHashIterNext(owneddofs, hi);
1057         ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
1058       }
1059       ierr = PetscSynchronizedPrintf(comm, "\n"); CHKERRQ(ierr);
1060       ierr = PetscSynchronizedPrintf(comm, "Patch %d: seen dofs:\n", v); CHKERRQ(ierr);
1061       PetscHashIterBegin(seendofs, hi);
1062       while (!PetscHashIterAtEnd(seendofs, hi)) {
1063         PetscInt globalDof;
1064         PetscBool flg;
1065 
1066         PetscHashIterGetKey(seendofs, hi, globalDof);
1067         PetscHashIterNext(seendofs, hi);
1068         ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
1069 
1070         ierr = PetscHSetIHas(globalBcs, globalDof, &flg);CHKERRQ(ierr);
1071         if (flg) {ierr = PetscHSetIAdd(globalbcdofs, globalDof);CHKERRQ(ierr);}
1072       }
1073       ierr = PetscSynchronizedPrintf(comm, "\n"); CHKERRQ(ierr);
1074       ierr = PetscSynchronizedPrintf(comm, "Patch %d: global BCs:\n", v); CHKERRQ(ierr);
1075       ierr = PetscHSetIGetSize(globalbcdofs, &numBcs);CHKERRQ(ierr);
1076       if (numBcs > 0) {
1077         PetscHashIterBegin(globalbcdofs, hi);
1078         while (!PetscHashIterAtEnd(globalbcdofs, hi)) {
1079           PetscInt globalDof;
1080           PetscHashIterGetKey(globalbcdofs, hi, globalDof);
1081           PetscHashIterNext(globalbcdofs, hi);
1082           ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof);CHKERRQ(ierr);
1083         }
1084       }
1085       ierr = PetscSynchronizedPrintf(comm, "\n");CHKERRQ(ierr);
1086       ierr = PetscSynchronizedPrintf(comm, "Patch %d: artificial BCs:\n", v);CHKERRQ(ierr);
1087       ierr = PetscHSetIGetSize(artificialbcs, &numBcs);CHKERRQ(ierr);
1088       if (numBcs > 0) {
1089         PetscHashIterBegin(artificialbcs, hi);
1090         while (!PetscHashIterAtEnd(artificialbcs, hi)) {
1091           PetscInt globalDof;
1092           PetscHashIterGetKey(artificialbcs, hi, globalDof);
1093           PetscHashIterNext(artificialbcs, hi);
1094           ierr = PetscSynchronizedPrintf(comm, "%d ", globalDof); CHKERRQ(ierr);
1095         }
1096       }
1097       ierr = PetscSynchronizedPrintf(comm, "\n\n"); CHKERRQ(ierr);
1098       ierr = PetscHSetIDestroy(&globalbcdofs);CHKERRQ(ierr);
1099     }
1100    for (k = 0; k < patch->nsubspaces; ++k) {
1101       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
1102       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
1103       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
1104       PetscInt        bs             = patch->bs[k];
1105 
1106       for (i = off; i < off + dof; ++i) {
1107         /* Walk over the cells in this patch. */
1108         const PetscInt c    = cellsArray[i];
1109         PetscInt       cell = c;
1110 
1111         /* TODO Change this to an IS */
1112         if (cellNumbering) {
1113           ierr = PetscSectionGetDof(cellNumbering, c, &cell);CHKERRQ(ierr);
1114           if (cell <= 0) SETERRQ1(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_OUTOFRANGE, "Cell %D doesn't appear in cell numbering map", c);
1115           ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);
1116         }
1117         newCellsArray[i] = cell;
1118         for (j = 0; j < nodesPerCell; ++j) {
1119           /* For each global dof, map it into contiguous local storage. */
1120           const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + subspaceOffset;
1121           /* finally, loop over block size */
1122           for (l = 0; l < bs; ++l) {
1123             PetscInt  localDof;
1124             PetscBool isGlobalBcDof, isArtificialBcDof;
1125 
1126             /* first, check if this is either a globally enforced or locally enforced BC dof */
1127             ierr = PetscHSetIHas(globalBcs, globalDof + l, &isGlobalBcDof);CHKERRQ(ierr);
1128             ierr = PetscHSetIHas(artificialbcs, globalDof + l, &isArtificialBcDof);CHKERRQ(ierr);
1129 
1130             /* if it's either, don't ever give it a local dof number */
1131             if (isGlobalBcDof || isArtificialBcDof) {
1132               dofsArray[globalIndex] = -1; /* don't use this in assembly in this patch */
1133             } else {
1134               ierr = PetscHMapIGet(ht, globalDof + l, &localDof);CHKERRQ(ierr);
1135               if (localDof == -1) {
1136                 localDof = localIndex++;
1137                 ierr = PetscHMapISet(ht, globalDof + l, localDof);CHKERRQ(ierr);
1138               }
1139               if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
1140               /* And store. */
1141               dofsArray[globalIndex] = localDof;
1142             }
1143 
1144             if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1145               if (isGlobalBcDof) {
1146                 dofsArrayWithArtificial[globalIndex] = -1; /* don't use this in assembly in this patch */
1147               } else {
1148                 ierr = PetscHMapIGet(htWithArtificial, globalDof + l, &localDof);CHKERRQ(ierr);
1149                 if (localDof == -1) {
1150                   localDof = localIndexWithArtificial++;
1151                   ierr = PetscHMapISet(htWithArtificial, globalDof + l, localDof);CHKERRQ(ierr);
1152                 }
1153                 if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
1154                 /* And store.*/
1155                 dofsArrayWithArtificial[globalIndex] = localDof;
1156               }
1157             }
1158 
1159             if(isNonlinear) {
1160               /* Build the dofmap for the function space with _all_ dofs,
1161                  including those in any kind of boundary condition */
1162               ierr = PetscHMapIGet(htWithAll, globalDof + l, &localDof);CHKERRQ(ierr);
1163               if (localDof == -1) {
1164                 localDof = localIndexWithAll++;
1165                 ierr = PetscHMapISet(htWithAll, globalDof + l, localDof);CHKERRQ(ierr);
1166               }
1167               if ( globalIndex >= numDofs ) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Found more dofs %D than expected %D", globalIndex+1, numDofs);
1168               /* And store.*/
1169               dofsArrayWithAll[globalIndex] = localDof;
1170             }
1171             globalIndex++;
1172           }
1173         }
1174       }
1175     }
1176      /*How many local dofs in this patch? */
1177    if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1178      ierr = PetscHMapIGetSize(htWithArtificial, &dof);CHKERRQ(ierr);
1179      ierr = PetscSectionSetDof(gtolCountsWithArtificial, v, dof);CHKERRQ(ierr);
1180    }
1181    if (isNonlinear) {
1182      ierr = PetscHMapIGetSize(htWithAll, &dof);CHKERRQ(ierr);
1183      ierr = PetscSectionSetDof(gtolCountsWithAll, v, dof);CHKERRQ(ierr);
1184    }
1185     ierr = PetscHMapIGetSize(ht, &dof);CHKERRQ(ierr);
1186     ierr = PetscSectionSetDof(gtolCounts, v, dof);CHKERRQ(ierr);
1187   }
1188   if (globalIndex != numDofs) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Expected number of dofs (%d) doesn't match found number (%d)", numDofs, globalIndex);
1189   ierr = PetscSectionSetUp(gtolCounts);CHKERRQ(ierr);
1190   ierr = PetscSectionGetStorageSize(gtolCounts, &numGlobalDofs);CHKERRQ(ierr);
1191   ierr = PetscMalloc1(numGlobalDofs, &globalDofsArray);CHKERRQ(ierr);
1192 
1193   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1194     ierr = PetscSectionSetUp(gtolCountsWithArtificial);CHKERRQ(ierr);
1195     ierr = PetscSectionGetStorageSize(gtolCountsWithArtificial, &numGlobalDofsWithArtificial);CHKERRQ(ierr);
1196     ierr = PetscMalloc1(numGlobalDofsWithArtificial, &globalDofsArrayWithArtificial);CHKERRQ(ierr);
1197   }
1198   if (isNonlinear) {
1199     ierr = PetscSectionSetUp(gtolCountsWithAll);CHKERRQ(ierr);
1200     ierr = PetscSectionGetStorageSize(gtolCountsWithAll, &numGlobalDofsWithAll);CHKERRQ(ierr);
1201     ierr = PetscMalloc1(numGlobalDofsWithAll, &globalDofsArrayWithAll);CHKERRQ(ierr);
1202   }
1203   /* Now populate the global to local map.  This could be merged into the above loop if we were willing to deal with reallocs. */
1204   for (v = vStart; v < vEnd; ++v) {
1205     PetscHashIter hi;
1206     PetscInt      dof, off, Np, ooff, i, j, k, l;
1207 
1208     ierr = PetscHMapIClear(ht);CHKERRQ(ierr);
1209     ierr = PetscHMapIClear(htWithArtificial);CHKERRQ(ierr);
1210     ierr = PetscHMapIClear(htWithAll);CHKERRQ(ierr);
1211     ierr = PetscSectionGetDof(cellCounts, v, &dof);CHKERRQ(ierr);
1212     ierr = PetscSectionGetOffset(cellCounts, v, &off);CHKERRQ(ierr);
1213     ierr = PetscSectionGetDof(pointCounts, v, &Np);CHKERRQ(ierr);
1214     ierr = PetscSectionGetOffset(pointCounts, v, &ooff);CHKERRQ(ierr);
1215     if (dof <= 0) continue;
1216 
1217     for (k = 0; k < patch->nsubspaces; ++k) {
1218       const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
1219       PetscInt        nodesPerCell   = patch->nodesPerCell[k];
1220       PetscInt        subspaceOffset = patch->subspaceOffsets[k];
1221       PetscInt        bs             = patch->bs[k];
1222       PetscInt        goff;
1223 
1224       for (i = off; i < off + dof; ++i) {
1225         /* Reconstruct mapping of global-to-local on this patch. */
1226         const PetscInt c    = cellsArray[i];
1227         PetscInt       cell = c;
1228 
1229         if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);}
1230         for (j = 0; j < nodesPerCell; ++j) {
1231           for (l = 0; l < bs; ++l) {
1232             const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset;
1233             const PetscInt localDof  = dofsArray[key];
1234             if (localDof >= 0) {ierr = PetscHMapISet(ht, globalDof, localDof);CHKERRQ(ierr);}
1235             if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1236               const PetscInt localDofWithArtificial = dofsArrayWithArtificial[key];
1237               if (localDofWithArtificial >= 0) {
1238                 ierr = PetscHMapISet(htWithArtificial, globalDof, localDofWithArtificial);CHKERRQ(ierr);
1239               }
1240             }
1241             if (isNonlinear) {
1242               const PetscInt localDofWithAll = dofsArrayWithAll[key];
1243               if (localDofWithAll >= 0) {
1244                 ierr = PetscHMapISet(htWithAll, globalDof, localDofWithAll);CHKERRQ(ierr);
1245               }
1246             }
1247             key++;
1248           }
1249         }
1250       }
1251 
1252       /* Shove it in the output data structure. */
1253       ierr = PetscSectionGetOffset(gtolCounts, v, &goff);CHKERRQ(ierr);
1254       PetscHashIterBegin(ht, hi);
1255       while (!PetscHashIterAtEnd(ht, hi)) {
1256         PetscInt globalDof, localDof;
1257 
1258         PetscHashIterGetKey(ht, hi, globalDof);
1259         PetscHashIterGetVal(ht, hi, localDof);
1260         if (globalDof >= 0) globalDofsArray[goff + localDof] = globalDof;
1261         PetscHashIterNext(ht, hi);
1262       }
1263 
1264       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1265         ierr = PetscSectionGetOffset(gtolCountsWithArtificial, v, &goff);CHKERRQ(ierr);
1266         PetscHashIterBegin(htWithArtificial, hi);
1267         while (!PetscHashIterAtEnd(htWithArtificial, hi)) {
1268           PetscInt globalDof, localDof;
1269           PetscHashIterGetKey(htWithArtificial, hi, globalDof);
1270           PetscHashIterGetVal(htWithArtificial, hi, localDof);
1271           if (globalDof >= 0) globalDofsArrayWithArtificial[goff + localDof] = globalDof;
1272           PetscHashIterNext(htWithArtificial, hi);
1273         }
1274       }
1275       if (isNonlinear) {
1276         ierr = PetscSectionGetOffset(gtolCountsWithAll, v, &goff);CHKERRQ(ierr);
1277         PetscHashIterBegin(htWithAll, hi);
1278         while (!PetscHashIterAtEnd(htWithAll, hi)) {
1279           PetscInt globalDof, localDof;
1280           PetscHashIterGetKey(htWithAll, hi, globalDof);
1281           PetscHashIterGetVal(htWithAll, hi, localDof);
1282           if (globalDof >= 0) globalDofsArrayWithAll[goff + localDof] = globalDof;
1283           PetscHashIterNext(htWithAll, hi);
1284         }
1285       }
1286 
1287       for (p = 0; p < Np; ++p) {
1288         const PetscInt point = pointsArray[ooff + p];
1289         PetscInt       globalDof, localDof;
1290 
1291         ierr = PCPatchGetGlobalDofs(pc, patch->dofSection, k, patch->combined, point, NULL, &globalDof);CHKERRQ(ierr);
1292         ierr = PetscHMapIGet(ht, globalDof, &localDof);CHKERRQ(ierr);
1293         offsArray[(ooff + p)*Nf + k] = localDof;
1294         if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1295           ierr = PetscHMapIGet(htWithArtificial, globalDof, &localDof);CHKERRQ(ierr);
1296           offsArrayWithArtificial[(ooff + p)*Nf + k] = localDof;
1297         }
1298         if (isNonlinear) {
1299           ierr = PetscHMapIGet(htWithAll, globalDof, &localDof);CHKERRQ(ierr);
1300           offsArrayWithAll[(ooff + p)*Nf + k] = localDof;
1301         }
1302       }
1303     }
1304 
1305     ierr = PetscHSetIDestroy(&globalBcs);CHKERRQ(ierr);
1306     ierr = PetscHSetIDestroy(&ownedpts);CHKERRQ(ierr);
1307     ierr = PetscHSetIDestroy(&seenpts);CHKERRQ(ierr);
1308     ierr = PetscHSetIDestroy(&owneddofs);CHKERRQ(ierr);
1309     ierr = PetscHSetIDestroy(&seendofs);CHKERRQ(ierr);
1310     ierr = PetscHSetIDestroy(&artificialbcs);CHKERRQ(ierr);
1311 
1312       /* At this point, we have a hash table ht built that maps globalDof -> localDof.
1313      We need to create the dof table laid out cellwise first, then by subspace,
1314      as the assembler assembles cell-wise and we need to stuff the different
1315      contributions of the different function spaces to the right places. So we loop
1316      over cells, then over subspaces. */
1317     if (patch->nsubspaces > 1) { /* for nsubspaces = 1, data we need is already in dofsArray */
1318       for (i = off; i < off + dof; ++i) {
1319         const PetscInt c    = cellsArray[i];
1320         PetscInt       cell = c;
1321 
1322         if (cellNumbering) {ierr = PetscSectionGetOffset(cellNumbering, c, &cell);CHKERRQ(ierr);}
1323         for (k = 0; k < patch->nsubspaces; ++k) {
1324           const PetscInt *cellNodeMap    = patch->cellNodeMap[k];
1325           PetscInt        nodesPerCell   = patch->nodesPerCell[k];
1326           PetscInt        subspaceOffset = patch->subspaceOffsets[k];
1327           PetscInt        bs             = patch->bs[k];
1328 
1329           for (j = 0; j < nodesPerCell; ++j) {
1330             for (l = 0; l < bs; ++l) {
1331               const PetscInt globalDof = cellNodeMap[cell*nodesPerCell + j]*bs + l + subspaceOffset;
1332               PetscInt       localDof;
1333 
1334               ierr = PetscHMapIGet(ht, globalDof, &localDof);CHKERRQ(ierr);
1335               /* If it's not in the hash table, i.e. is a BC dof,
1336                then the PetscHSetIMap above gives -1, which matches
1337                exactly the convention for PETSc's matrix assembly to
1338                ignore the dof. So we don't need to do anything here */
1339               asmArray[asmKey] = localDof;
1340               if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1341                 ierr = PetscHMapIGet(htWithArtificial, globalDof, &localDof);CHKERRQ(ierr);
1342                 asmArrayWithArtificial[asmKey] = localDof;
1343               }
1344               if (isNonlinear) {
1345                 ierr = PetscHMapIGet(htWithAll, globalDof, &localDof);CHKERRQ(ierr);
1346                 asmArrayWithAll[asmKey] = localDof;
1347               }
1348               asmKey++;
1349             }
1350           }
1351         }
1352       }
1353     }
1354   }
1355   if (1 == patch->nsubspaces) {
1356     ierr = PetscMemcpy(asmArray, dofsArray, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
1357     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1358       ierr = PetscMemcpy(asmArrayWithArtificial, dofsArrayWithArtificial, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
1359     }
1360     if (isNonlinear) {
1361       ierr = PetscMemcpy(asmArrayWithAll, dofsArrayWithAll, numDofs * sizeof(PetscInt));CHKERRQ(ierr);
1362     }
1363   }
1364 
1365   ierr = PetscHMapIDestroy(&ht);CHKERRQ(ierr);
1366   ierr = PetscHMapIDestroy(&htWithArtificial);CHKERRQ(ierr);
1367   ierr = PetscHMapIDestroy(&htWithAll);CHKERRQ(ierr);
1368   ierr = ISRestoreIndices(cells, &cellsArray);CHKERRQ(ierr);
1369   ierr = ISRestoreIndices(points, &pointsArray);CHKERRQ(ierr);
1370   ierr = PetscFree(dofsArray);CHKERRQ(ierr);
1371   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1372     ierr = PetscFree(dofsArrayWithArtificial);CHKERRQ(ierr);
1373   }
1374   if (isNonlinear) {
1375     ierr = PetscFree(dofsArrayWithAll);CHKERRQ(ierr);
1376   }
1377   /* Create placeholder section for map from points to patch dofs */
1378   ierr = PetscSectionCreate(PETSC_COMM_SELF, &patch->patchSection);CHKERRQ(ierr);
1379   ierr = PetscSectionSetNumFields(patch->patchSection, patch->nsubspaces);CHKERRQ(ierr);
1380   if (patch->combined) {
1381     PetscInt numFields;
1382     ierr = PetscSectionGetNumFields(patch->dofSection[0], &numFields);CHKERRQ(ierr);
1383     if (numFields != patch->nsubspaces) SETERRQ2(PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Mismatch between number of section fields %D and number of subspaces %D", numFields, patch->nsubspaces);
1384     ierr = PetscSectionGetChart(patch->dofSection[0], &pStart, &pEnd);CHKERRQ(ierr);
1385     ierr = PetscSectionSetChart(patch->patchSection, pStart, pEnd);CHKERRQ(ierr);
1386     for (p = pStart; p < pEnd; ++p) {
1387       PetscInt dof, fdof, f;
1388 
1389       ierr = PetscSectionGetDof(patch->dofSection[0], p, &dof);CHKERRQ(ierr);
1390       ierr = PetscSectionSetDof(patch->patchSection, p, dof);CHKERRQ(ierr);
1391       for (f = 0; f < patch->nsubspaces; ++f) {
1392         ierr = PetscSectionGetFieldDof(patch->dofSection[0], p, f, &fdof);CHKERRQ(ierr);
1393         ierr = PetscSectionSetFieldDof(patch->patchSection, p, f, fdof);CHKERRQ(ierr);
1394       }
1395     }
1396   } else {
1397     PetscInt pStartf, pEndf, f;
1398     pStart = PETSC_MAX_INT;
1399     pEnd = PETSC_MIN_INT;
1400     for (f = 0; f < patch->nsubspaces; ++f) {
1401       ierr = PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf);CHKERRQ(ierr);
1402       pStart = PetscMin(pStart, pStartf);
1403       pEnd = PetscMax(pEnd, pEndf);
1404     }
1405     ierr = PetscSectionSetChart(patch->patchSection, pStart, pEnd);CHKERRQ(ierr);
1406     for (f = 0; f < patch->nsubspaces; ++f) {
1407       ierr = PetscSectionGetChart(patch->dofSection[f], &pStartf, &pEndf);CHKERRQ(ierr);
1408       for (p = pStartf; p < pEndf; ++p) {
1409         PetscInt fdof;
1410         ierr = PetscSectionGetDof(patch->dofSection[f], p, &fdof);CHKERRQ(ierr);
1411         ierr = PetscSectionAddDof(patch->patchSection, p, fdof);CHKERRQ(ierr);
1412         ierr = PetscSectionSetFieldDof(patch->patchSection, p, f, fdof);CHKERRQ(ierr);
1413       }
1414     }
1415   }
1416   ierr = PetscSectionSetUp(patch->patchSection);CHKERRQ(ierr);
1417   ierr = PetscSectionSetUseFieldOffsets(patch->patchSection, PETSC_TRUE);CHKERRQ(ierr);
1418   /* Replace cell indices with firedrake-numbered ones. */
1419   ierr = ISGeneralSetIndices(cells, numCells, (const PetscInt *) newCellsArray, PETSC_OWN_POINTER);CHKERRQ(ierr);
1420   ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofs, globalDofsArray, PETSC_OWN_POINTER, &patch->gtol);CHKERRQ(ierr);
1421   ierr = PetscObjectSetName((PetscObject) patch->gtol, "Global Indices");CHKERRQ(ierr);
1422   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_g2l_view", patch->classname);CHKERRQ(ierr);
1423   ierr = PetscSectionViewFromOptions(patch->gtolCounts, (PetscObject) pc, option);CHKERRQ(ierr);
1424   ierr = ISViewFromOptions(patch->gtol, (PetscObject) pc, option);CHKERRQ(ierr);
1425   ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArray, PETSC_OWN_POINTER, &patch->dofs);CHKERRQ(ierr);
1426   ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArray, PETSC_OWN_POINTER, &patch->offs);CHKERRQ(ierr);
1427   if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1428     ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithArtificial, globalDofsArrayWithArtificial, PETSC_OWN_POINTER, &patch->gtolWithArtificial);CHKERRQ(ierr);
1429     ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithArtificial, PETSC_OWN_POINTER, &patch->dofsWithArtificial);CHKERRQ(ierr);
1430     ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithArtificial, PETSC_OWN_POINTER, &patch->offsWithArtificial);CHKERRQ(ierr);
1431   }
1432   if (isNonlinear) {
1433     ierr = ISCreateGeneral(PETSC_COMM_SELF, numGlobalDofsWithAll, globalDofsArrayWithAll, PETSC_OWN_POINTER, &patch->gtolWithAll);CHKERRQ(ierr);
1434     ierr = ISCreateGeneral(PETSC_COMM_SELF, numDofs, asmArrayWithAll, PETSC_OWN_POINTER, &patch->dofsWithAll);CHKERRQ(ierr);
1435     ierr = ISCreateGeneral(PETSC_COMM_SELF, numPoints*Nf, offsArrayWithAll, PETSC_OWN_POINTER, &patch->offsWithAll);CHKERRQ(ierr);
1436   }
1437   PetscFunctionReturn(0);
1438 }
1439 
1440 static PetscErrorCode PCPatchZeroFillMatrix_Private(Mat mat, const PetscInt ncell, const PetscInt ndof, const PetscInt *dof)
1441 {
1442   PetscScalar    *values = NULL;
1443   PetscInt        rows, c, i;
1444   PetscErrorCode  ierr;
1445 
1446   PetscFunctionBegin;
1447   ierr = PetscCalloc1(ndof*ndof, &values);CHKERRQ(ierr);
1448   for (c = 0; c < ncell; ++c) {
1449     const PetscInt *idx = &dof[ndof*c];
1450     ierr = MatSetValues(mat, ndof, idx, ndof, idx, values, INSERT_VALUES);CHKERRQ(ierr);
1451   }
1452   ierr = MatGetLocalSize(mat, &rows, NULL);CHKERRQ(ierr);
1453   for (i = 0; i < rows; ++i) {
1454     ierr = MatSetValues(mat, 1, &i, 1, &i, values, INSERT_VALUES);CHKERRQ(ierr);
1455   }
1456   ierr = MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1457   ierr = MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1458   ierr = PetscFree(values);CHKERRQ(ierr);
1459   PetscFunctionReturn(0);
1460 }
1461 
1462 static PetscErrorCode PCPatchCreateMatrix_Private(PC pc, PetscInt point, Mat *mat, PetscBool withArtificial)
1463 {
1464   PC_PATCH      *patch = (PC_PATCH *) pc->data;
1465   Vec            x, y;
1466   PetscBool      flg;
1467   PetscInt       csize, rsize;
1468   const char    *prefix = NULL;
1469   PetscErrorCode ierr;
1470 
1471   PetscFunctionBegin;
1472   if(withArtificial) {
1473     /* would be nice if we could create a rectangular matrix of size numDofsWithArtificial x numDofs here */
1474     x = patch->patchRHSWithArtificial[point];
1475     y = patch->patchRHSWithArtificial[point];
1476   } else {
1477     x = patch->patchRHS[point];
1478     y = patch->patchUpdate[point];
1479   }
1480 
1481   ierr = VecGetSize(x, &csize);CHKERRQ(ierr);
1482   ierr = VecGetSize(y, &rsize);CHKERRQ(ierr);
1483   ierr = MatCreate(PETSC_COMM_SELF, mat);CHKERRQ(ierr);
1484   ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr);
1485   ierr = MatSetOptionsPrefix(*mat, prefix);CHKERRQ(ierr);
1486   ierr = MatAppendOptionsPrefix(*mat, "pc_patch_sub_");CHKERRQ(ierr);
1487   if (patch->sub_mat_type)       {ierr = MatSetType(*mat, patch->sub_mat_type);CHKERRQ(ierr);}
1488   else if (!patch->sub_mat_type) {ierr = MatSetType(*mat, MATDENSE);CHKERRQ(ierr);}
1489   ierr = MatSetSizes(*mat, rsize, csize, rsize, csize);CHKERRQ(ierr);
1490   ierr = PetscObjectTypeCompare((PetscObject) *mat, MATDENSE, &flg);CHKERRQ(ierr);
1491   if (!flg) {ierr = PetscObjectTypeCompare((PetscObject)*mat, MATSEQDENSE, &flg);CHKERRQ(ierr);}
1492   /* Sparse patch matrices */
1493   if (!flg) {
1494     PetscBT         bt;
1495     PetscInt       *dnnz      = NULL;
1496     const PetscInt *dofsArray = NULL;
1497     PetscInt        pStart, pEnd, ncell, offset, c, i, j;
1498 
1499     if(withArtificial) {
1500       ierr = ISGetIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1501     } else {
1502       ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1503     }
1504     ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
1505     point += pStart;
1506     if (point >= pEnd) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %D not in [%D, %D)\n", point, pStart, pEnd);CHKERRQ(ierr);
1507     ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
1508     ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
1509     ierr = PetscLogEventBegin(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr);
1510     /* A PetscBT uses N^2 bits to store the sparsity pattern on a
1511      * patch. This is probably OK if the patches are not too big,
1512      * but uses too much memory. We therefore switch based on rsize. */
1513     if (rsize < 3000) { /* FIXME: I picked this switch value out of my hat */
1514       ierr = PetscCalloc1(rsize, &dnnz);CHKERRQ(ierr);
1515       ierr = PetscBTCreate(rsize*rsize, &bt);CHKERRQ(ierr);
1516       for (c = 0; c < ncell; ++c) {
1517         const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell;
1518         for (i = 0; i < patch->totalDofsPerCell; ++i) {
1519           const PetscInt row = idx[i];
1520           if (row < 0) continue;
1521           for (j = 0; j < patch->totalDofsPerCell; ++j) {
1522             const PetscInt col = idx[j];
1523             const PetscInt key = row*rsize + col;
1524             if (col < 0) continue;
1525             if (!PetscBTLookupSet(bt, key)) ++dnnz[row];
1526           }
1527         }
1528       }
1529       ierr = PetscBTDestroy(&bt);CHKERRQ(ierr);
1530       ierr = MatXAIJSetPreallocation(*mat, 1, dnnz, NULL, NULL, NULL);CHKERRQ(ierr);
1531       ierr = PetscFree(dnnz);CHKERRQ(ierr);
1532       ierr = PCPatchZeroFillMatrix_Private(*mat, ncell, patch->totalDofsPerCell, &dofsArray[offset*patch->totalDofsPerCell]);CHKERRQ(ierr);
1533     } else { /* rsize too big, use MATPREALLOCATOR */
1534       Mat preallocator;
1535       PetscScalar* vals;
1536 
1537       ierr = PetscCalloc1(patch->totalDofsPerCell*patch->totalDofsPerCell, &vals);CHKERRQ(ierr);
1538       ierr = MatCreate(PETSC_COMM_SELF, &preallocator);CHKERRQ(ierr);
1539       ierr = MatSetType(preallocator, MATPREALLOCATOR);CHKERRQ(ierr);
1540       ierr = MatSetSizes(preallocator, rsize, rsize, rsize, rsize);CHKERRQ(ierr);
1541       ierr = MatSetUp(preallocator);CHKERRQ(ierr);
1542       for (c = 0; c < ncell; ++c) {
1543         const PetscInt *idx = dofsArray + (offset + c)*patch->totalDofsPerCell;
1544         ierr = MatSetValues(preallocator, patch->totalDofsPerCell, idx, patch->totalDofsPerCell, idx, vals, INSERT_VALUES);CHKERRQ(ierr);
1545       }
1546       ierr = PetscFree(vals);CHKERRQ(ierr);
1547       ierr = MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1548       ierr = MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1549       ierr = MatPreallocatorPreallocate(preallocator, PETSC_TRUE, *mat);CHKERRQ(ierr);
1550       ierr = MatDestroy(&preallocator);CHKERRQ(ierr);
1551     }
1552     ierr = PetscLogEventEnd(PC_Patch_Prealloc, pc, 0, 0, 0);CHKERRQ(ierr);
1553     if(withArtificial) {
1554       ierr = ISRestoreIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1555     } else {
1556       ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1557     }
1558   }
1559   ierr = MatSetUp(*mat);CHKERRQ(ierr);
1560   PetscFunctionReturn(0);
1561 }
1562 
1563 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)
1564 {
1565   PC_PATCH       *patch = (PC_PATCH *) pc->data;
1566   DM              dm;
1567   PetscSection    s;
1568   const PetscInt *parray, *oarray;
1569   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
1570   PetscErrorCode  ierr;
1571 
1572   PetscFunctionBegin;
1573   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
1574   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
1575   /* Set offset into patch */
1576   ierr = PetscSectionGetDof(patch->pointCounts, patchNum, &Np);CHKERRQ(ierr);
1577   ierr = PetscSectionGetOffset(patch->pointCounts, patchNum, &poff);CHKERRQ(ierr);
1578   ierr = ISGetIndices(patch->points, &parray);CHKERRQ(ierr);
1579   ierr = ISGetIndices(patch->offs,   &oarray);CHKERRQ(ierr);
1580   for (f = 0; f < Nf; ++f) {
1581     for (p = 0; p < Np; ++p) {
1582       const PetscInt point = parray[poff+p];
1583       PetscInt       dof;
1584 
1585       ierr = PetscSectionGetFieldDof(patch->patchSection, point, f, &dof);CHKERRQ(ierr);
1586       ierr = PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);
1587       if (patch->nsubspaces == 1) {ierr = PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);}
1588       else                        {ierr = PetscSectionSetOffset(patch->patchSection, point, -1);CHKERRQ(ierr);}
1589     }
1590   }
1591   ierr = ISRestoreIndices(patch->points, &parray);CHKERRQ(ierr);
1592   ierr = ISRestoreIndices(patch->offs,   &oarray);CHKERRQ(ierr);
1593   if (patch->viewSection) {ierr = ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection);CHKERRQ(ierr);}
1594   ierr = DMPlexComputeResidual_Patch_Internal(pc->dm, patch->patchSection, cellIS, 0.0, x, NULL, F, ctx);CHKERRQ(ierr);
1595   PetscFunctionReturn(0);
1596 }
1597 
1598 PetscErrorCode PCPatchComputeFunction_Internal(PC pc, Vec x, Vec F, PetscInt point)
1599 {
1600   PC_PATCH       *patch = (PC_PATCH *) pc->data;
1601   const PetscInt *dofsArray;
1602   const PetscInt *dofsArrayWithAll;
1603   const PetscInt *cellsArray;
1604   PetscInt        ncell, offset, pStart, pEnd;
1605   PetscErrorCode  ierr;
1606 
1607   PetscFunctionBegin;
1608   ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
1609   if (!patch->usercomputeop) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback\n");
1610   ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1611   ierr = ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
1612   ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
1613   ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
1614 
1615   point += pStart;
1616   if (point >= pEnd) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %D not in [%D, %D)\n", point, pStart, pEnd);CHKERRQ(ierr);
1617 
1618   ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
1619   ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
1620   if (ncell <= 0) {
1621     ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
1622     PetscFunctionReturn(0);
1623   }
1624   PetscStackPush("PCPatch user callback");
1625   /* Cannot reuse the same IS because the geometry info is being cached in it */
1626   ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS);CHKERRQ(ierr);
1627   ierr = patch->usercomputef(pc, point, x, F, patch->cellIS, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell,
1628                                                                                             dofsArrayWithAll + offset*patch->totalDofsPerCell,
1629                                                                                             patch->usercomputefctx);CHKERRQ(ierr);
1630   PetscStackPop;
1631   ierr = ISDestroy(&patch->cellIS);CHKERRQ(ierr);
1632   ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1633   ierr = ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
1634   ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
1635   if (patch->viewMatrix) {
1636     char name[PETSC_MAX_PATH_LEN];
1637 
1638     ierr = PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch vector for Point %D", point);CHKERRQ(ierr);
1639     ierr = PetscObjectSetName((PetscObject) F, name);CHKERRQ(ierr);
1640     ierr = ObjectView((PetscObject) F, patch->viewerMatrix, patch->formatMatrix);CHKERRQ(ierr);
1641   }
1642   ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
1643   PetscFunctionReturn(0);
1644 }
1645 
1646 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)
1647 {
1648   PC_PATCH       *patch = (PC_PATCH *) pc->data;
1649   DM              dm;
1650   PetscSection    s;
1651   const PetscInt *parray, *oarray;
1652   PetscInt        Nf = patch->nsubspaces, Np, poff, p, f;
1653   PetscErrorCode  ierr;
1654 
1655   PetscFunctionBegin;
1656   ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
1657   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
1658   /* Set offset into patch */
1659   ierr = PetscSectionGetDof(patch->pointCounts, patchNum, &Np);CHKERRQ(ierr);
1660   ierr = PetscSectionGetOffset(patch->pointCounts, patchNum, &poff);CHKERRQ(ierr);
1661   ierr = ISGetIndices(patch->points, &parray);CHKERRQ(ierr);
1662   ierr = ISGetIndices(patch->offs,   &oarray);CHKERRQ(ierr);
1663   for (f = 0; f < Nf; ++f) {
1664     for (p = 0; p < Np; ++p) {
1665       const PetscInt point = parray[poff+p];
1666       PetscInt       dof;
1667 
1668       ierr = PetscSectionGetFieldDof(patch->patchSection, point, f, &dof);CHKERRQ(ierr);
1669       ierr = PetscSectionSetFieldOffset(patch->patchSection, point, f, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);
1670       if (patch->nsubspaces == 1) {ierr = PetscSectionSetOffset(patch->patchSection, point, oarray[(poff+p)*Nf+f]);CHKERRQ(ierr);}
1671       else                        {ierr = PetscSectionSetOffset(patch->patchSection, point, -1);CHKERRQ(ierr);}
1672     }
1673   }
1674   ierr = ISRestoreIndices(patch->points, &parray);CHKERRQ(ierr);
1675   ierr = ISRestoreIndices(patch->offs,   &oarray);CHKERRQ(ierr);
1676   if (patch->viewSection) {ierr = ObjectView((PetscObject) patch->patchSection, patch->viewerSection, patch->formatSection);CHKERRQ(ierr);}
1677   /* TODO Shut off MatViewFromOptions() in MatAssemblyEnd() here */
1678   ierr = DMPlexComputeJacobian_Patch_Internal(pc->dm, patch->patchSection, patch->patchSection, cellIS, 0.0, 0.0, x, NULL, J, J, ctx);CHKERRQ(ierr);
1679   PetscFunctionReturn(0);
1680 }
1681 
1682 PetscErrorCode PCPatchComputeOperator_Internal(PC pc, Vec x, Mat mat, PetscInt point, PetscBool withArtificial)
1683 {
1684   PC_PATCH       *patch = (PC_PATCH *) pc->data;
1685   const PetscInt *dofsArray;
1686   const PetscInt *dofsArrayWithArtificial = NULL;
1687   const PetscInt *dofsArrayWithAll = NULL;
1688   const PetscInt *cellsArray;
1689   PetscInt        ncell, offset, pStart, pEnd;
1690   PetscBool       isNonlinear;
1691   PetscErrorCode  ierr;
1692 
1693   PetscFunctionBegin;
1694   ierr = PetscLogEventBegin(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
1695   isNonlinear = patch->isNonlinear;
1696   if (!patch->usercomputeop) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PCPatchSetComputeOperator() to set user callback\n");
1697   if(withArtificial) {
1698     ierr = ISGetIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1699   } else {
1700     ierr = ISGetIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1701   }
1702   if (isNonlinear) {
1703     ierr = ISGetIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
1704   }
1705   ierr = ISGetIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
1706   ierr = PetscSectionGetChart(patch->cellCounts, &pStart, &pEnd);CHKERRQ(ierr);
1707 
1708   point += pStart;
1709   if (point >= pEnd) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Operator point %D not in [%D, %D)\n", point, pStart, pEnd);CHKERRQ(ierr);
1710 
1711   ierr = PetscSectionGetDof(patch->cellCounts, point, &ncell);CHKERRQ(ierr);
1712   ierr = PetscSectionGetOffset(patch->cellCounts, point, &offset);CHKERRQ(ierr);
1713   if (ncell <= 0) {
1714     ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
1715     PetscFunctionReturn(0);
1716   }
1717   PetscStackPush("PCPatch user callback");
1718   /* Cannot reuse the same IS because the geometry info is being cached in it */
1719   ierr = ISCreateGeneral(PETSC_COMM_SELF, ncell, cellsArray + offset, PETSC_USE_POINTER, &patch->cellIS);CHKERRQ(ierr);
1720   ierr = patch->usercomputeop(pc, point, x, mat, patch->cellIS, ncell*patch->totalDofsPerCell, dofsArray + offset*patch->totalDofsPerCell, dofsArrayWithAll ? dofsArrayWithAll + offset*patch->totalDofsPerCell : NULL, patch->usercomputeopctx);CHKERRQ(ierr);
1721   PetscStackPop;
1722   ierr = ISDestroy(&patch->cellIS);CHKERRQ(ierr);
1723   if(withArtificial) {
1724     ierr = ISRestoreIndices(patch->dofsWithArtificial, &dofsArray);CHKERRQ(ierr);
1725   } else {
1726     ierr = ISRestoreIndices(patch->dofs, &dofsArray);CHKERRQ(ierr);
1727   }
1728   if (isNonlinear) {
1729     ierr = ISRestoreIndices(patch->dofsWithAll, &dofsArrayWithAll);CHKERRQ(ierr);
1730   }
1731   ierr = ISRestoreIndices(patch->cells, &cellsArray);CHKERRQ(ierr);
1732   if (patch->viewMatrix) {
1733     char name[PETSC_MAX_PATH_LEN];
1734 
1735     ierr = PetscSNPrintf(name, PETSC_MAX_PATH_LEN-1, "Patch matrix for Point %D", point);CHKERRQ(ierr);
1736     ierr = PetscObjectSetName((PetscObject) mat, name);CHKERRQ(ierr);
1737     ierr = ObjectView((PetscObject) mat, patch->viewerMatrix, patch->formatMatrix);CHKERRQ(ierr);
1738   }
1739   ierr = PetscLogEventEnd(PC_Patch_ComputeOp, pc, 0, 0, 0);CHKERRQ(ierr);
1740   PetscFunctionReturn(0);
1741 }
1742 
1743 PetscErrorCode PCPatch_ScatterLocal_Private(PC pc, PetscInt p, Vec x, Vec y, InsertMode mode, ScatterMode scat, PatchScatterType scattertype)
1744 {
1745   PC_PATCH          *patch     = (PC_PATCH *) pc->data;
1746   const PetscScalar *xArray    = NULL;
1747   PetscScalar       *yArray    = NULL;
1748   const PetscInt    *gtolArray = NULL;
1749   PetscInt           dof, offset, lidx;
1750   PetscErrorCode     ierr;
1751 
1752   PetscFunctionBeginHot;
1753   ierr = PetscLogEventBegin(PC_Patch_Scatter, pc, 0, 0, 0);CHKERRQ(ierr);
1754   ierr = VecGetArrayRead(x, &xArray);CHKERRQ(ierr);
1755   ierr = VecGetArray(y, &yArray);CHKERRQ(ierr);
1756   if (scattertype == SCATTER_WITHARTIFICIAL) {
1757     ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof);CHKERRQ(ierr);
1758     ierr = PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offset);CHKERRQ(ierr);
1759     ierr = ISGetIndices(patch->gtolWithArtificial, &gtolArray);CHKERRQ(ierr);
1760   } else if (scattertype == SCATTER_WITHALL) {
1761     ierr = PetscSectionGetDof(patch->gtolCountsWithAll, p, &dof);CHKERRQ(ierr);
1762     ierr = PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offset);CHKERRQ(ierr);
1763     ierr = ISGetIndices(patch->gtolWithAll, &gtolArray);CHKERRQ(ierr);
1764   } else {
1765     ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr);
1766     ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
1767     ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
1768   }
1769   if (mode == INSERT_VALUES && scat != SCATTER_FORWARD) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't insert if not scattering forward\n");
1770   if (mode == ADD_VALUES    && scat != SCATTER_REVERSE) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Can't add if not scattering reverse\n");
1771   for (lidx = 0; lidx < dof; ++lidx) {
1772     const PetscInt gidx = gtolArray[offset+lidx];
1773 
1774     if (mode == INSERT_VALUES) yArray[lidx]  = xArray[gidx]; /* Forward */
1775     else                       yArray[gidx] += xArray[lidx]; /* Reverse */
1776   }
1777   if (scattertype == SCATTER_WITHARTIFICIAL) {
1778     ierr = ISRestoreIndices(patch->gtolWithArtificial, &gtolArray);CHKERRQ(ierr);
1779   } else if (scattertype == SCATTER_WITHALL) {
1780     ierr = ISRestoreIndices(patch->gtolWithAll, &gtolArray);CHKERRQ(ierr);
1781   } else {
1782     ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
1783   }
1784   ierr = VecRestoreArrayRead(x, &xArray);CHKERRQ(ierr);
1785   ierr = VecRestoreArray(y, &yArray);CHKERRQ(ierr);
1786   ierr = PetscLogEventEnd(PC_Patch_Scatter, pc, 0, 0, 0);CHKERRQ(ierr);
1787   PetscFunctionReturn(0);
1788 }
1789 
1790 static PetscErrorCode PCSetUp_PATCH_Linear(PC pc)
1791 {
1792   PC_PATCH      *patch = (PC_PATCH *) pc->data;
1793   const char    *prefix;
1794   PetscInt       i;
1795   PetscErrorCode ierr;
1796 
1797   PetscFunctionBegin;
1798   if (!pc->setupcalled) {
1799     ierr = PetscMalloc1(patch->npatch, &patch->solver);CHKERRQ(ierr);
1800     ierr = PCGetOptionsPrefix(pc, &prefix);CHKERRQ(ierr);
1801     for (i = 0; i < patch->npatch; ++i) {
1802       KSP ksp;
1803       PC  subpc;
1804 
1805       ierr = KSPCreate(PETSC_COMM_SELF, &ksp);CHKERRQ(ierr);
1806       ierr = KSPSetOptionsPrefix(ksp, prefix);CHKERRQ(ierr);
1807       ierr = KSPAppendOptionsPrefix(ksp, "sub_");CHKERRQ(ierr);
1808       ierr = PetscObjectIncrementTabLevel((PetscObject) ksp, (PetscObject) pc, 1);CHKERRQ(ierr);
1809       ierr = KSPGetPC(ksp, &subpc);CHKERRQ(ierr);
1810       ierr = PetscObjectIncrementTabLevel((PetscObject) subpc, (PetscObject) pc, 1);CHKERRQ(ierr);
1811       ierr = PetscLogObjectParent((PetscObject) pc, (PetscObject) ksp);CHKERRQ(ierr);
1812       patch->solver[i] = (PetscObject) ksp;
1813     }
1814   }
1815   if (patch->save_operators) {
1816     for (i = 0; i < patch->npatch; ++i) {
1817       ierr = MatZeroEntries(patch->mat[i]);CHKERRQ(ierr);
1818       ierr = PCPatchComputeOperator_Internal(pc, NULL, patch->mat[i], i, PETSC_FALSE);CHKERRQ(ierr);
1819       ierr = KSPSetOperators((KSP) patch->solver[i], patch->mat[i], patch->mat[i]);CHKERRQ(ierr);
1820     }
1821   }
1822   if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1823     for (i = 0; i < patch->npatch; ++i) {
1824       /* Instead of padding patch->patchUpdate with zeros to get */
1825       /* patch->patchUpdateWithArtificial and then multiplying with the matrix, */
1826       /* just get rid of the columns that correspond to the dofs with */
1827       /* artificial bcs. That's of course fairly inefficient, hopefully we */
1828       /* can just assemble the rectangular matrix in the first place. */
1829       Mat matSquare;
1830       IS rowis;
1831       PetscInt dof;
1832 
1833       ierr = MatGetSize(patch->mat[i], &dof, NULL);CHKERRQ(ierr);
1834       if (dof == 0) {
1835         patch->matWithArtificial[i] = NULL;
1836         continue;
1837       }
1838 
1839       ierr = PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE);CHKERRQ(ierr);
1840       ierr = MatZeroEntries(matSquare);CHKERRQ(ierr);
1841       ierr = PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE);CHKERRQ(ierr);
1842 
1843       ierr = MatGetSize(matSquare, &dof, NULL);CHKERRQ(ierr);
1844       ierr = ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis); CHKERRQ(ierr);
1845       if(pc->setupcalled) {
1846         ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_REUSE_MATRIX, &patch->matWithArtificial[i]); CHKERRQ(ierr);
1847       } else {
1848         ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &patch->matWithArtificial[i]); CHKERRQ(ierr);
1849       }
1850       ierr = ISDestroy(&rowis); CHKERRQ(ierr);
1851       ierr = MatDestroy(&matSquare);CHKERRQ(ierr);
1852     }
1853   }
1854   PetscFunctionReturn(0);
1855 }
1856 
1857 static PetscErrorCode PCSetUp_PATCH(PC pc)
1858 {
1859   PC_PATCH      *patch = (PC_PATCH *) pc->data;
1860   PetscInt       i;
1861   PetscBool       isNonlinear;
1862   PetscErrorCode ierr;
1863 
1864   PetscFunctionBegin;
1865   if (!pc->setupcalled) {
1866     PetscInt pStart, pEnd, p;
1867     PetscInt localSize;
1868 
1869     ierr = PetscLogEventBegin(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr);
1870 
1871     isNonlinear = patch->isNonlinear;
1872     if (!patch->nsubspaces) {
1873       DM           dm;
1874       PetscDS      prob;
1875       PetscSection s;
1876       PetscInt     cStart, cEnd, c, Nf, f, numGlobalBcs = 0, *globalBcs, *Nb, totNb = 0, **cellDofs;
1877 
1878       ierr = PCGetDM(pc, &dm);CHKERRQ(ierr);
1879       if (!dm) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_WRONG, "Must set DM for PCPATCH or call PCPatchSetDiscretisationInfo()");
1880       ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
1881       ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
1882       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
1883       for (p = pStart; p < pEnd; ++p) {
1884         PetscInt cdof;
1885         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
1886         numGlobalBcs += cdof;
1887       }
1888       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1889       ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1890       ierr = PetscMalloc3(Nf, &Nb, Nf, &cellDofs, numGlobalBcs, &globalBcs);CHKERRQ(ierr);
1891       for (f = 0; f < Nf; ++f) {
1892         PetscFE        fe;
1893         PetscDualSpace sp;
1894         PetscInt       cdoff = 0;
1895 
1896         ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
1897         /* ierr = PetscFEGetNumComponents(fe, &Nc[f]);CHKERRQ(ierr); */
1898         ierr = PetscFEGetDualSpace(fe, &sp);CHKERRQ(ierr);
1899         ierr = PetscDualSpaceGetDimension(sp, &Nb[f]);CHKERRQ(ierr);
1900         totNb += Nb[f];
1901 
1902         ierr = PetscMalloc1((cEnd-cStart)*Nb[f], &cellDofs[f]);CHKERRQ(ierr);
1903         for (c = cStart; c < cEnd; ++c) {
1904           PetscInt *closure = NULL;
1905           PetscInt  clSize  = 0, cl;
1906 
1907           ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
1908           for (cl = 0; cl < clSize*2; cl += 2) {
1909             const PetscInt p = closure[cl];
1910             PetscInt       fdof, d, foff;
1911 
1912             ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr);
1913             ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr);
1914             for (d = 0; d < fdof; ++d, ++cdoff) cellDofs[f][cdoff] = foff + d;
1915           }
1916           ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
1917         }
1918         if (cdoff != (cEnd-cStart)*Nb[f]) SETERRQ4(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_SIZ, "Total number of cellDofs %D for field %D should be Nc (%D) * cellDof (%D)", cdoff, f, cEnd-cStart, Nb[f]);
1919       }
1920       numGlobalBcs = 0;
1921       for (p = pStart; p < pEnd; ++p) {
1922         const PetscInt *ind;
1923         PetscInt        off, cdof, d;
1924 
1925         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
1926         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
1927         ierr = PetscSectionGetConstraintIndices(s, p, &ind);CHKERRQ(ierr);
1928         for (d = 0; d < cdof; ++d) globalBcs[numGlobalBcs++] = off + ind[d];
1929       }
1930 
1931       ierr = PCPatchSetDiscretisationInfoCombined(pc, dm, Nb, (const PetscInt **) cellDofs, numGlobalBcs, globalBcs, numGlobalBcs, globalBcs);CHKERRQ(ierr);
1932       for (f = 0; f < Nf; ++f) {
1933         ierr = PetscFree(cellDofs[f]);CHKERRQ(ierr);
1934       }
1935       ierr = PetscFree3(Nb, cellDofs, globalBcs);CHKERRQ(ierr);
1936       ierr = PCPatchSetComputeFunction(pc, PCPatchComputeFunction_DMPlex_Private, NULL);CHKERRQ(ierr);
1937       ierr = PCPatchSetComputeOperator(pc, PCPatchComputeOperator_DMPlex_Private, NULL);CHKERRQ(ierr);
1938     }
1939 
1940     localSize = patch->subspaceOffsets[patch->nsubspaces];
1941     ierr = VecCreateSeq(PETSC_COMM_SELF, localSize, &patch->localRHS);CHKERRQ(ierr);
1942     ierr = VecSetUp(patch->localRHS);CHKERRQ(ierr);
1943     ierr = VecDuplicate(patch->localRHS, &patch->localUpdate);CHKERRQ(ierr);
1944     ierr = PCPatchCreateCellPatches(pc);CHKERRQ(ierr);
1945     ierr = PCPatchCreateCellPatchDiscretisationInfo(pc);CHKERRQ(ierr);
1946 
1947     /* OK, now build the work vectors */
1948     ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, &pEnd);CHKERRQ(ierr);
1949     ierr = PetscMalloc1(patch->npatch, &patch->patchRHS);CHKERRQ(ierr);
1950     ierr = PetscMalloc1(patch->npatch, &patch->patchUpdate);CHKERRQ(ierr);
1951 
1952     if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1953       ierr = PetscMalloc1(patch->npatch, &patch->patchRHSWithArtificial);CHKERRQ(ierr);
1954       ierr = PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithArtificial);CHKERRQ(ierr);
1955     }
1956     if (isNonlinear) {
1957       ierr = PetscMalloc1(patch->npatch, &patch->dofMappingWithoutToWithAll);CHKERRQ(ierr);
1958     }
1959     for (p = pStart; p < pEnd; ++p) {
1960       PetscInt dof;
1961 
1962       ierr = PetscSectionGetDof(patch->gtolCounts, p, &dof);CHKERRQ(ierr);
1963       ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchRHS[p-pStart]);CHKERRQ(ierr);
1964       ierr = VecSetUp(patch->patchRHS[p-pStart]);CHKERRQ(ierr);
1965       ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchUpdate[p-pStart]);CHKERRQ(ierr);
1966       ierr = VecSetUp(patch->patchUpdate[p-pStart]);CHKERRQ(ierr);
1967       if (patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
1968         const PetscInt    *gtolArray, *gtolArrayWithArtificial = NULL;
1969         PetscInt           numPatchDofs, offset;
1970         PetscInt           numPatchDofsWithArtificial, offsetWithArtificial;
1971         PetscInt           dofWithoutArtificialCounter = 0;
1972         PetscInt          *patchWithoutArtificialToWithArtificialArray;
1973 
1974         ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &dof);CHKERRQ(ierr);
1975         ierr = VecCreateSeq(PETSC_COMM_SELF, dof, &patch->patchRHSWithArtificial[p-pStart]);CHKERRQ(ierr);
1976         ierr = VecSetUp(patch->patchRHSWithArtificial[p-pStart]);CHKERRQ(ierr);
1977 
1978         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
1979         /* the index in the patch with all dofs */
1980         ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
1981 
1982         ierr = PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs);CHKERRQ(ierr);
1983 
1984         ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
1985         ierr = ISGetIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial);CHKERRQ(ierr);
1986         ierr = PetscSectionGetDof(patch->gtolCountsWithArtificial, p, &numPatchDofsWithArtificial);CHKERRQ(ierr);
1987         ierr = PetscSectionGetOffset(patch->gtolCountsWithArtificial, p, &offsetWithArtificial);CHKERRQ(ierr);
1988 
1989         ierr = PetscMalloc1(numPatchDofs, &patchWithoutArtificialToWithArtificialArray);CHKERRQ(ierr);
1990 
1991         ierr = ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutArtificialToWithArtificialArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithArtificial[p-pStart]);CHKERRQ(ierr);
1992         if (numPatchDofs == 0) continue;
1993         for (i=0; i<numPatchDofsWithArtificial; i++) {
1994           if (gtolArrayWithArtificial[i+offsetWithArtificial] == gtolArray[offset+dofWithoutArtificialCounter]) {
1995             patchWithoutArtificialToWithArtificialArray[dofWithoutArtificialCounter] = i;
1996             dofWithoutArtificialCounter++;
1997             if (dofWithoutArtificialCounter == numPatchDofs)
1998               break;
1999           }
2000         }
2001         ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
2002         ierr = ISRestoreIndices(patch->gtolWithArtificial, &gtolArrayWithArtificial);CHKERRQ(ierr);
2003       }
2004       if (isNonlinear) {
2005         const PetscInt    *gtolArray, *gtolArrayWithAll = NULL;
2006         PetscInt           numPatchDofs, offset;
2007         PetscInt           numPatchDofsWithAll, offsetWithAll;
2008         PetscInt           dofWithoutAllCounter = 0;
2009         PetscInt          *patchWithoutAllToWithAllArray;
2010 
2011         /* Now build the mapping that for a dof in a patch WITHOUT dofs that have artificial bcs gives the */
2012         /* the index in the patch with all dofs */
2013         ierr = ISGetIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
2014 
2015         ierr = PetscSectionGetDof(patch->gtolCounts, p, &numPatchDofs);CHKERRQ(ierr);
2016 
2017         ierr = PetscSectionGetOffset(patch->gtolCounts, p, &offset);CHKERRQ(ierr);
2018         ierr = ISGetIndices(patch->gtolWithAll, &gtolArrayWithAll);CHKERRQ(ierr);
2019         ierr = PetscSectionGetDof(patch->gtolCountsWithAll, p, &numPatchDofsWithAll);CHKERRQ(ierr);
2020         ierr = PetscSectionGetOffset(patch->gtolCountsWithAll, p, &offsetWithAll);CHKERRQ(ierr);
2021 
2022         ierr = PetscMalloc1(numPatchDofs, &patchWithoutAllToWithAllArray);CHKERRQ(ierr);
2023 
2024         ierr = ISCreateGeneral(PETSC_COMM_SELF, numPatchDofs, patchWithoutAllToWithAllArray, PETSC_OWN_POINTER, &patch->dofMappingWithoutToWithAll[p-pStart]);CHKERRQ(ierr);
2025         if (numPatchDofs == 0) continue;
2026         for (i=0; i<numPatchDofsWithAll; i++) {
2027           if (gtolArrayWithAll[i+offsetWithAll] == gtolArray[offset+dofWithoutAllCounter]) {
2028             patchWithoutAllToWithAllArray[dofWithoutAllCounter] = i;
2029             dofWithoutAllCounter++;
2030             if (dofWithoutAllCounter == numPatchDofs)
2031               break;
2032           }
2033         }
2034         ierr = ISRestoreIndices(patch->gtol, &gtolArray);CHKERRQ(ierr);
2035         ierr = ISRestoreIndices(patch->gtolWithAll, &gtolArrayWithAll);CHKERRQ(ierr);
2036       }
2037     }
2038     if (patch->save_operators) {
2039       ierr = PetscMalloc1(patch->npatch, &patch->mat);CHKERRQ(ierr);
2040       for (i = 0; i < patch->npatch; ++i) {
2041         ierr = PCPatchCreateMatrix_Private(pc, i, &patch->mat[i], PETSC_FALSE);CHKERRQ(ierr);
2042       }
2043     }
2044     ierr = PetscLogEventEnd(PC_Patch_CreatePatches, pc, 0, 0, 0);CHKERRQ(ierr);
2045 
2046     /* If desired, calculate weights for dof multiplicity */
2047     if (patch->partition_of_unity) {
2048       PetscScalar *input = NULL;
2049       PetscScalar *output = NULL;
2050       Vec global;
2051 
2052       ierr = VecDuplicate(patch->localRHS, &patch->dof_weights);CHKERRQ(ierr);
2053       if(patch->local_composition_type == PC_COMPOSITE_ADDITIVE) {
2054         for (i = 0; i < patch->npatch; ++i) {
2055           PetscInt dof;
2056 
2057           ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &dof);CHKERRQ(ierr);
2058           if (dof <= 0) continue;
2059           ierr = VecSet(patch->patchRHS[i], 1.0);CHKERRQ(ierr);
2060           ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchRHS[i], patch->dof_weights, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR);CHKERRQ(ierr);
2061         }
2062       } else {
2063         /* multiplicative is actually only locally multiplicative and globally additive. need the pou where the mesh decomposition overlaps */
2064         ierr = VecSet(patch->dof_weights, 1.0);CHKERRQ(ierr);
2065       }
2066 
2067       VecDuplicate(patch->dof_weights, &global);
2068       VecSet(global, 0.);
2069 
2070       ierr = VecGetArray(patch->dof_weights, &input);CHKERRQ(ierr);
2071       ierr = VecGetArray(global, &output);CHKERRQ(ierr);
2072       ierr = PetscSFReduceBegin(patch->defaultSF, MPIU_SCALAR, input, output, MPI_SUM);CHKERRQ(ierr);
2073       ierr = PetscSFReduceEnd(patch->defaultSF, MPIU_SCALAR, input, output, MPI_SUM);CHKERRQ(ierr);
2074       ierr = VecRestoreArray(patch->dof_weights, &input);CHKERRQ(ierr);
2075       ierr = VecRestoreArray(global, &output);CHKERRQ(ierr);
2076 
2077       ierr = VecReciprocal(global);CHKERRQ(ierr);
2078 
2079       ierr = VecGetArray(patch->dof_weights, &output);CHKERRQ(ierr);
2080       ierr = VecGetArray(global, &input);CHKERRQ(ierr);
2081       ierr = PetscSFBcastBegin(patch->defaultSF, MPIU_SCALAR, input, output);CHKERRQ(ierr);
2082       ierr = PetscSFBcastEnd(patch->defaultSF, MPIU_SCALAR, input, output);CHKERRQ(ierr);
2083       ierr = VecRestoreArray(patch->dof_weights, &output);CHKERRQ(ierr);
2084       ierr = VecRestoreArray(global, &input);CHKERRQ(ierr);
2085       ierr = VecDestroy(&global);CHKERRQ(ierr);
2086     }
2087     if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE && patch->save_operators) {
2088       ierr = PetscMalloc1(patch->npatch, &patch->matWithArtificial);CHKERRQ(ierr);
2089     }
2090   }
2091   ierr = (*patch->setupsolver)(pc);CHKERRQ(ierr);
2092   PetscFunctionReturn(0);
2093 }
2094 
2095 static PetscErrorCode PCApply_PATCH_Linear(PC pc, PetscInt i, Vec x, Vec y)
2096 {
2097   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2098   KSP            ksp   = (KSP) patch->solver[i];
2099   PetscErrorCode ierr;
2100 
2101   PetscFunctionBegin;
2102   if (!patch->save_operators) {
2103     Mat mat;
2104 
2105     ierr = PCPatchCreateMatrix_Private(pc, i, &mat, PETSC_FALSE);CHKERRQ(ierr);
2106     /* Populate operator here. */
2107     ierr = PCPatchComputeOperator_Internal(pc, NULL, mat, i, PETSC_FALSE);CHKERRQ(ierr);
2108     ierr = KSPSetOperators(ksp, mat, mat);CHKERRQ(ierr);
2109     /* Drop reference so the KSPSetOperators below will blow it away. */
2110     ierr = MatDestroy(&mat);CHKERRQ(ierr);
2111   }
2112   ierr = PetscLogEventBegin(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr);
2113   if (!ksp->setfromoptionscalled) {
2114     ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
2115   }
2116   ierr = KSPSolve(ksp, x, y);CHKERRQ(ierr);
2117   ierr = PetscLogEventEnd(PC_Patch_Solve, pc, 0, 0, 0);CHKERRQ(ierr);
2118   if (!patch->save_operators) {
2119     PC pc;
2120     ierr = KSPSetOperators(ksp, NULL, NULL);CHKERRQ(ierr);
2121     ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
2122     /* Destroy PC context too, otherwise the factored matrix hangs around. */
2123     ierr = PCReset(pc);CHKERRQ(ierr);
2124   }
2125   PetscFunctionReturn(0);
2126 }
2127 
2128 static PetscErrorCode PCUpdateMultiplicative_PATCH_Linear(PC pc, PetscInt i, PetscInt pStart)
2129 {
2130   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2131   Mat multMat;
2132   PetscErrorCode ierr;
2133 
2134   PetscFunctionBegin;
2135 
2136   if (patch->save_operators) {
2137     multMat = patch->matWithArtificial[i];
2138   } else {
2139     /*Very inefficient, hopefully we can just assemble the rectangular matrix in the first place.*/
2140     Mat matSquare;
2141     PetscInt dof;
2142     IS rowis;
2143     ierr = PCPatchCreateMatrix_Private(pc, i, &matSquare, PETSC_TRUE);CHKERRQ(ierr);
2144     ierr = MatZeroEntries(matSquare);CHKERRQ(ierr);
2145     ierr = PCPatchComputeOperator_Internal(pc, NULL, matSquare, i, PETSC_TRUE);CHKERRQ(ierr);
2146     ierr = MatGetSize(matSquare, &dof, NULL);CHKERRQ(ierr);
2147     ierr = ISCreateStride(PETSC_COMM_SELF, dof, 0, 1, &rowis); CHKERRQ(ierr);
2148     ierr = MatCreateSubMatrix(matSquare, rowis, patch->dofMappingWithoutToWithArtificial[i], MAT_INITIAL_MATRIX, &multMat); CHKERRQ(ierr);
2149     ierr = MatDestroy(&matSquare);CHKERRQ(ierr);
2150     ierr = ISDestroy(&rowis); CHKERRQ(ierr);
2151   }
2152   ierr = MatMult(multMat, patch->patchUpdate[i], patch->patchRHSWithArtificial[i]); CHKERRQ(ierr);
2153   ierr = VecScale(patch->patchRHSWithArtificial[i], -1.0); CHKERRQ(ierr);
2154   ierr = PCPatch_ScatterLocal_Private(pc, i + pStart, patch->patchRHSWithArtificial[i], patch->localRHS, ADD_VALUES, SCATTER_REVERSE, SCATTER_WITHARTIFICIAL); CHKERRQ(ierr);
2155   if (!patch->save_operators) {
2156     ierr = MatDestroy(&multMat); CHKERRQ(ierr);
2157   }
2158   PetscFunctionReturn(0);
2159 }
2160 
2161 static PetscErrorCode PCApply_PATCH(PC pc, Vec x, Vec y)
2162 {
2163   PC_PATCH          *patch    = (PC_PATCH *) pc->data;
2164   const PetscScalar *globalRHS  = NULL;
2165   PetscScalar       *localRHS   = NULL;
2166   PetscScalar       *globalUpdate  = NULL;
2167   const PetscInt    *bcNodes  = NULL;
2168   PetscInt           nsweep   = patch->symmetrise_sweep ? 2 : 1;
2169   PetscInt           start[2] = {0, 0};
2170   PetscInt           end[2]   = {-1, -1};
2171   const PetscInt     inc[2]   = {1, -1};
2172   const PetscScalar *localUpdate;
2173   const PetscInt    *iterationSet;
2174   PetscInt           pStart, numBcs, n, sweep, bc, j;
2175   PetscErrorCode     ierr;
2176 
2177   PetscFunctionBegin;
2178   ierr = PetscLogEventBegin(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr);
2179   ierr = PetscOptionsPushGetViewerOff(PETSC_TRUE);CHKERRQ(ierr);
2180   /* start, end, inc have 2 entries to manage a second backward sweep if we symmetrize */
2181   end[0]   = patch->npatch;
2182   start[1] = patch->npatch-1;
2183   if (patch->user_patches) {
2184     ierr = ISGetLocalSize(patch->iterationSet, &end[0]);CHKERRQ(ierr);
2185     start[1] = end[0] - 1;
2186     ierr = ISGetIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr);
2187   }
2188   /* Scatter from global space into overlapped local spaces */
2189   ierr = VecGetArrayRead(x, &globalRHS);CHKERRQ(ierr);
2190   ierr = VecGetArray(patch->localRHS, &localRHS);CHKERRQ(ierr);
2191   ierr = PetscSFBcastBegin(patch->defaultSF, MPIU_SCALAR, globalRHS, localRHS);CHKERRQ(ierr);
2192   ierr = PetscSFBcastEnd(patch->defaultSF, MPIU_SCALAR, globalRHS, localRHS);CHKERRQ(ierr);
2193   ierr = VecRestoreArrayRead(x, &globalRHS);CHKERRQ(ierr);
2194   ierr = VecRestoreArray(patch->localRHS, &localRHS);CHKERRQ(ierr);
2195 
2196   ierr = VecSet(patch->localUpdate, 0.0);CHKERRQ(ierr);
2197   ierr = PetscSectionGetChart(patch->gtolCounts, &pStart, NULL);CHKERRQ(ierr);
2198   for (sweep = 0; sweep < nsweep; sweep++) {
2199     for (j = start[sweep]; j*inc[sweep] < end[sweep]*inc[sweep]; j += inc[sweep]) {
2200       PetscInt i       = patch->user_patches ? iterationSet[j] : j;
2201       PetscInt start, len;
2202 
2203       ierr = PetscSectionGetDof(patch->gtolCounts, i+pStart, &len);CHKERRQ(ierr);
2204       ierr = PetscSectionGetOffset(patch->gtolCounts, i+pStart, &start);CHKERRQ(ierr);
2205       /* TODO: Squash out these guys in the setup as well. */
2206       if (len <= 0) continue;
2207       /* TODO: Do we need different scatters for X and Y? */
2208       ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->localRHS, patch->patchRHS[i], INSERT_VALUES, SCATTER_FORWARD, SCATTER_INTERIOR);CHKERRQ(ierr);
2209       ierr = (*patch->applysolver)(pc, i, patch->patchRHS[i], patch->patchUpdate[i]);CHKERRQ(ierr);
2210       ierr = PCPatch_ScatterLocal_Private(pc, i+pStart, patch->patchUpdate[i], patch->localUpdate, ADD_VALUES, SCATTER_REVERSE, SCATTER_INTERIOR);CHKERRQ(ierr);
2211       if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
2212         ierr = (*patch->updatemultiplicative)(pc, i, pStart);CHKERRQ(ierr);
2213       }
2214     }
2215   }
2216   if (patch->user_patches) {ierr = ISRestoreIndices(patch->iterationSet, &iterationSet);CHKERRQ(ierr);}
2217   /* XXX: should we do this on the global vector? */
2218   if (patch->partition_of_unity) {
2219     ierr = VecPointwiseMult(patch->localUpdate, patch->localUpdate, patch->dof_weights);CHKERRQ(ierr);
2220   }
2221   /* Now patch->localUpdate contains the solution of the patch solves, so we need to combine them all. */
2222   ierr = VecSet(y, 0.0);CHKERRQ(ierr);
2223   ierr = VecGetArray(y, &globalUpdate);CHKERRQ(ierr);
2224   ierr = VecGetArrayRead(patch->localUpdate, &localUpdate);CHKERRQ(ierr);
2225   ierr = PetscSFReduceBegin(patch->defaultSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM);CHKERRQ(ierr);
2226   ierr = PetscSFReduceEnd(patch->defaultSF, MPIU_SCALAR, localUpdate, globalUpdate, MPI_SUM);CHKERRQ(ierr);
2227   ierr = VecRestoreArrayRead(patch->localUpdate, &localUpdate);CHKERRQ(ierr);
2228 
2229   /* Now we need to send the global BC values through */
2230   ierr = VecGetArrayRead(x, &globalRHS);CHKERRQ(ierr);
2231   ierr = ISGetSize(patch->globalBcNodes, &numBcs);CHKERRQ(ierr);
2232   ierr = ISGetIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr);
2233   ierr = VecGetLocalSize(x, &n);CHKERRQ(ierr);
2234   for (bc = 0; bc < numBcs; ++bc) {
2235     const PetscInt idx = bcNodes[bc];
2236     if (idx < n) globalUpdate[idx] = globalRHS[idx];
2237   }
2238 
2239   ierr = ISRestoreIndices(patch->globalBcNodes, &bcNodes);CHKERRQ(ierr);
2240   ierr = VecRestoreArrayRead(x, &globalRHS);CHKERRQ(ierr);
2241   ierr = VecRestoreArray(y, &globalUpdate);CHKERRQ(ierr);
2242 
2243   ierr = PetscOptionsPopGetViewerOff();CHKERRQ(ierr);
2244   ierr = PetscLogEventEnd(PC_Patch_Apply, pc, 0, 0, 0);CHKERRQ(ierr);
2245   PetscFunctionReturn(0);
2246 }
2247 
2248 static PetscErrorCode PCReset_PATCH_Linear(PC pc)
2249 {
2250   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2251   PetscInt       i;
2252   PetscErrorCode ierr;
2253 
2254   PetscFunctionBegin;
2255   if (patch->solver) {
2256     for (i = 0; i < patch->npatch; ++i) {ierr = KSPReset((KSP) patch->solver[i]);CHKERRQ(ierr);}
2257   }
2258   PetscFunctionReturn(0);
2259 }
2260 
2261 static PetscErrorCode PCReset_PATCH(PC pc)
2262 {
2263   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2264   PetscInt       i;
2265   PetscErrorCode ierr;
2266 
2267   PetscFunctionBegin;
2268   /* TODO: Get rid of all these ifs */
2269   ierr = PetscSFDestroy(&patch->defaultSF);CHKERRQ(ierr);
2270   ierr = PetscSectionDestroy(&patch->cellCounts);CHKERRQ(ierr);
2271   ierr = PetscSectionDestroy(&patch->pointCounts);CHKERRQ(ierr);
2272   ierr = PetscSectionDestroy(&patch->cellNumbering);CHKERRQ(ierr);
2273   ierr = PetscSectionDestroy(&patch->gtolCounts);CHKERRQ(ierr);
2274   ierr = ISDestroy(&patch->gtol);CHKERRQ(ierr);
2275   ierr = ISDestroy(&patch->cells);CHKERRQ(ierr);
2276   ierr = ISDestroy(&patch->points);CHKERRQ(ierr);
2277   ierr = ISDestroy(&patch->dofs);CHKERRQ(ierr);
2278   ierr = ISDestroy(&patch->offs);CHKERRQ(ierr);
2279   ierr = PetscSectionDestroy(&patch->patchSection);CHKERRQ(ierr);
2280   ierr = ISDestroy(&patch->ghostBcNodes);CHKERRQ(ierr);
2281   ierr = ISDestroy(&patch->globalBcNodes);CHKERRQ(ierr);
2282   ierr = PetscSectionDestroy(&patch->gtolCountsWithArtificial);CHKERRQ(ierr);
2283   ierr = ISDestroy(&patch->gtolWithArtificial);CHKERRQ(ierr);
2284   ierr = ISDestroy(&patch->dofsWithArtificial);CHKERRQ(ierr);
2285   ierr = ISDestroy(&patch->offsWithArtificial);CHKERRQ(ierr);
2286   ierr = PetscSectionDestroy(&patch->gtolCountsWithAll);CHKERRQ(ierr);
2287   ierr = ISDestroy(&patch->gtolWithAll);CHKERRQ(ierr);
2288   ierr = ISDestroy(&patch->dofsWithAll);CHKERRQ(ierr);
2289   ierr = ISDestroy(&patch->offsWithAll);CHKERRQ(ierr);
2290 
2291 
2292   if (patch->dofSection) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscSectionDestroy(&patch->dofSection[i]);CHKERRQ(ierr);}
2293   ierr = PetscFree(patch->dofSection);CHKERRQ(ierr);
2294   ierr = PetscFree(patch->bs);CHKERRQ(ierr);
2295   ierr = PetscFree(patch->nodesPerCell);CHKERRQ(ierr);
2296   if (patch->cellNodeMap) for (i = 0; i < patch->nsubspaces; i++) {ierr = PetscFree(patch->cellNodeMap[i]);CHKERRQ(ierr);}
2297   ierr = PetscFree(patch->cellNodeMap);CHKERRQ(ierr);
2298   ierr = PetscFree(patch->subspaceOffsets);CHKERRQ(ierr);
2299 
2300   ierr = (*patch->resetsolver)(pc);CHKERRQ(ierr);
2301 
2302   if (patch->subspaces_to_exclude) {
2303     PetscHSetIDestroy(&patch->subspaces_to_exclude);
2304   }
2305 
2306   ierr = VecDestroy(&patch->localRHS);CHKERRQ(ierr);
2307   ierr = VecDestroy(&patch->localUpdate);CHKERRQ(ierr);
2308   if (patch->patchRHS) {
2309     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchRHS[i]);CHKERRQ(ierr);}
2310     ierr = PetscFree(patch->patchRHS);CHKERRQ(ierr);
2311   }
2312   if (patch->patchUpdate) {
2313     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchUpdate[i]);CHKERRQ(ierr);}
2314     ierr = PetscFree(patch->patchUpdate);CHKERRQ(ierr);
2315   }
2316   ierr = VecDestroy(&patch->dof_weights);CHKERRQ(ierr);
2317   if (patch->patch_dof_weights) {
2318     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patch_dof_weights[i]);CHKERRQ(ierr);}
2319     ierr = PetscFree(patch->patch_dof_weights);CHKERRQ(ierr);
2320   }
2321   if (patch->mat) {
2322     for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->mat[i]);CHKERRQ(ierr);}
2323     ierr = PetscFree(patch->mat);CHKERRQ(ierr);
2324   }
2325   if (patch->matWithArtificial) {
2326     for (i = 0; i < patch->npatch; ++i) {ierr = MatDestroy(&patch->matWithArtificial[i]);CHKERRQ(ierr);}
2327     ierr = PetscFree(patch->matWithArtificial);CHKERRQ(ierr);
2328   }
2329   if (patch->patchRHSWithArtificial) {
2330     for (i = 0; i < patch->npatch; ++i) {ierr = VecDestroy(&patch->patchRHSWithArtificial[i]);CHKERRQ(ierr);}
2331     ierr = PetscFree(patch->patchRHSWithArtificial);CHKERRQ(ierr);
2332   }
2333   if(patch->dofMappingWithoutToWithArtificial) {
2334     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->dofMappingWithoutToWithArtificial[i]);CHKERRQ(ierr);}
2335     ierr = PetscFree(patch->dofMappingWithoutToWithArtificial);CHKERRQ(ierr);
2336 
2337   }
2338   if(patch->dofMappingWithoutToWithAll) {
2339     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->dofMappingWithoutToWithAll[i]);CHKERRQ(ierr);}
2340     ierr = PetscFree(patch->dofMappingWithoutToWithAll);CHKERRQ(ierr);
2341 
2342   }
2343   ierr = PetscFree(patch->sub_mat_type);CHKERRQ(ierr);
2344   if (patch->userIS) {
2345     for (i = 0; i < patch->npatch; ++i) {ierr = ISDestroy(&patch->userIS[i]);CHKERRQ(ierr);}
2346     ierr = PetscFree(patch->userIS);CHKERRQ(ierr);
2347   }
2348   patch->bs          = 0;
2349   patch->cellNodeMap = NULL;
2350   patch->nsubspaces  = 0;
2351   ierr = ISDestroy(&patch->iterationSet);CHKERRQ(ierr);
2352 
2353   ierr = PetscViewerDestroy(&patch->viewerSection);CHKERRQ(ierr);
2354   PetscFunctionReturn(0);
2355 }
2356 
2357 static PetscErrorCode PCDestroy_PATCH_Linear(PC pc)
2358 {
2359   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2360   PetscInt       i;
2361   PetscErrorCode ierr;
2362 
2363   PetscFunctionBegin;
2364   if (patch->solver) {
2365     for (i = 0; i < patch->npatch; ++i) {ierr = KSPDestroy((KSP *) &patch->solver[i]);CHKERRQ(ierr);}
2366     ierr = PetscFree(patch->solver);CHKERRQ(ierr);
2367   }
2368   PetscFunctionReturn(0);
2369 }
2370 
2371 static PetscErrorCode PCDestroy_PATCH(PC pc)
2372 {
2373   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2374   PetscErrorCode ierr;
2375 
2376   PetscFunctionBegin;
2377   ierr = PCReset_PATCH(pc);CHKERRQ(ierr);
2378   ierr = (*patch->destroysolver)(pc);CHKERRQ(ierr);
2379   ierr = PetscFree(pc->data);CHKERRQ(ierr);
2380   PetscFunctionReturn(0);
2381 }
2382 
2383 static PetscErrorCode PCSetFromOptions_PATCH(PetscOptionItems *PetscOptionsObject, PC pc)
2384 {
2385   PC_PATCH            *patch = (PC_PATCH *) pc->data;
2386   PCPatchConstructType patchConstructionType = PC_PATCH_STAR;
2387   char                 sub_mat_type[PETSC_MAX_PATH_LEN];
2388   char                 option[PETSC_MAX_PATH_LEN];
2389   const char          *prefix;
2390   PetscBool            flg, dimflg, codimflg;
2391   MPI_Comm             comm;
2392   PetscInt            *ifields, nfields, k;
2393   PetscErrorCode       ierr;
2394   PCCompositeType loctype = PC_COMPOSITE_ADDITIVE;
2395 
2396   PetscFunctionBegin;
2397   ierr = PetscObjectGetComm((PetscObject) pc, &comm);CHKERRQ(ierr);
2398   ierr = PetscObjectGetOptionsPrefix((PetscObject) pc, &prefix);CHKERRQ(ierr);
2399   ierr = PetscOptionsHead(PetscOptionsObject, "Patch solver options");CHKERRQ(ierr);
2400 
2401   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_save_operators", patch->classname);
2402   ierr = PetscOptionsBool(option,  "Store all patch operators for lifetime of object?", "PCPatchSetSaveOperators", patch->save_operators, &patch->save_operators, &flg);CHKERRQ(ierr);
2403 
2404   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_partition_of_unity", patch->classname);
2405   ierr = PetscOptionsBool(option, "Weight contributions by dof multiplicity?", "PCPatchSetPartitionOfUnity", patch->partition_of_unity, &patch->partition_of_unity, &flg);CHKERRQ(ierr);
2406 
2407   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_local_type", patch->classname);
2408   ierr = PetscOptionsEnum(option,"Type of local solver composition (additive or multiplicative)","PCPatchSetLocalComposition",PCCompositeTypes,(PetscEnum)loctype,(PetscEnum*)&loctype,&flg);CHKERRQ(ierr);
2409   if(flg) { ierr = PCPatchSetLocalComposition(pc, loctype);CHKERRQ(ierr);}
2410 
2411   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_dim", patch->classname);
2412   ierr = PetscOptionsInt(option, "What dimension of mesh point to construct patches by? (0 = vertices)", "PCPATCH", patch->dim, &patch->dim, &dimflg);CHKERRQ(ierr);
2413   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_codim", patch->classname);
2414   ierr = PetscOptionsInt(option, "What co-dimension of mesh point to construct patches by? (0 = cells)", "PCPATCH", patch->codim, &patch->codim, &codimflg);CHKERRQ(ierr);
2415   if (dimflg && codimflg) {SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Can only set one of dimension or co-dimension");CHKERRQ(ierr);}
2416 
2417   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_construct_type", patch->classname);
2418   ierr = PetscOptionsEnum(option, "How should the patches be constructed?", "PCPatchSetConstructType", PCPatchConstructTypes, (PetscEnum) patchConstructionType, (PetscEnum *) &patchConstructionType, &flg);CHKERRQ(ierr);
2419   if (flg) {ierr = PCPatchSetConstructType(pc, patchConstructionType, NULL, NULL);CHKERRQ(ierr);}
2420 
2421   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_vanka_dim", patch->classname);
2422   ierr = PetscOptionsInt(option, "Topological dimension of entities for Vanka to ignore", "PCPATCH", patch->vankadim, &patch->vankadim, &flg);CHKERRQ(ierr);
2423 
2424   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_ignore_dim", patch->classname);
2425   ierr = PetscOptionsInt(option, "Topological dimension of entities for completion to ignore", "PCPATCH", patch->ignoredim, &patch->ignoredim, &flg);CHKERRQ(ierr);
2426 
2427   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_sub_mat_type", patch->classname);
2428   ierr = PetscOptionsFList(option, "Matrix type for patch solves", "PCPatchSetSubMatType", MatList, NULL, sub_mat_type, PETSC_MAX_PATH_LEN, &flg);CHKERRQ(ierr);
2429   if (flg) {ierr = PCPatchSetSubMatType(pc, sub_mat_type);CHKERRQ(ierr);}
2430 
2431   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_symmetrise_sweep", patch->classname);
2432   ierr = PetscOptionsBool(option, "Go start->end, end->start?", "PCPATCH", patch->symmetrise_sweep, &patch->symmetrise_sweep, &flg);CHKERRQ(ierr);
2433 
2434   /* If the user has set the number of subspaces, use that for the buffer size,
2435      otherwise use a large number */
2436   if (patch->nsubspaces <= 0) {
2437     nfields = 128;
2438   } else {
2439     nfields = patch->nsubspaces;
2440   }
2441   ierr = PetscMalloc1(nfields, &ifields);CHKERRQ(ierr);
2442   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_exclude_subspaces", patch->classname);
2443   ierr = PetscOptionsGetIntArray(((PetscObject)pc)->options,((PetscObject)pc)->prefix,option,ifields,&nfields,&flg);CHKERRQ(ierr);
2444   if (flg && (patchConstructionType == PC_PATCH_USER)) SETERRQ(comm, PETSC_ERR_ARG_INCOMP, "We cannot support excluding a subspace with user patches because we do not index patches with a mesh point");
2445   if (flg) {
2446     PetscHSetIClear(patch->subspaces_to_exclude);
2447     for (k = 0; k < nfields; k++) {
2448       PetscHSetIAdd(patch->subspaces_to_exclude, ifields[k]);
2449     }
2450   }
2451   ierr = PetscFree(ifields);CHKERRQ(ierr);
2452 
2453   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_patches_view", patch->classname);
2454   ierr = PetscOptionsBool(option, "Print out information during patch construction", "PCPATCH", patch->viewPatches, &patch->viewPatches, &flg);CHKERRQ(ierr);
2455 
2456   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_cells_view", patch->classname);
2457   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options,prefix, option,   &patch->viewerCells,   &patch->formatCells,   &patch->viewCells);CHKERRQ(ierr);
2458   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_points_view", patch->classname);
2459   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options,prefix, option,  &patch->viewerPoints,  &patch->formatPoints,  &patch->viewPoints);CHKERRQ(ierr);
2460   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_section_view", patch->classname);
2461   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options,prefix, option, &patch->viewerSection, &patch->formatSection, &patch->viewSection);CHKERRQ(ierr);
2462   ierr = PetscSNPrintf(option, PETSC_MAX_PATH_LEN, "-%s_patch_mat_view", patch->classname);
2463   ierr = PetscOptionsGetViewer(comm,((PetscObject) pc)->options,prefix, option,     &patch->viewerMatrix,  &patch->formatMatrix,  &patch->viewMatrix);CHKERRQ(ierr);
2464   ierr = PetscOptionsTail();CHKERRQ(ierr);
2465   patch->optionsSet = PETSC_TRUE;
2466   PetscFunctionReturn(0);
2467 }
2468 
2469 static PetscErrorCode PCSetUpOnBlocks_PATCH(PC pc)
2470 {
2471   PC_PATCH          *patch = (PC_PATCH*) pc->data;
2472   KSPConvergedReason reason;
2473   PetscInt           i;
2474   PetscErrorCode     ierr;
2475 
2476   PetscFunctionBegin;
2477   if (!patch->save_operators) {
2478     /* Can't do this here because the sub KSPs don't have an operator attached yet. */
2479     PetscFunctionReturn(0);
2480   }
2481   for (i = 0; i < patch->npatch; ++i) {
2482     if (!((KSP) patch->solver[i])->setfromoptionscalled) {
2483       ierr = KSPSetFromOptions((KSP) patch->solver[i]);CHKERRQ(ierr);
2484     }
2485     ierr = KSPSetUp((KSP) patch->solver[i]);CHKERRQ(ierr);
2486     ierr = KSPGetConvergedReason((KSP) patch->solver[i], &reason);CHKERRQ(ierr);
2487     if (reason == KSP_DIVERGED_PCSETUP_FAILED) pc->failedreason = PC_SUBPC_ERROR;
2488   }
2489   PetscFunctionReturn(0);
2490 }
2491 
2492 static PetscErrorCode PCView_PATCH(PC pc, PetscViewer viewer)
2493 {
2494   PC_PATCH      *patch = (PC_PATCH *) pc->data;
2495   PetscViewer    sviewer;
2496   PetscBool      isascii;
2497   PetscMPIInt    rank;
2498   PetscErrorCode ierr;
2499 
2500   PetscFunctionBegin;
2501   /* TODO Redo tabbing with set tbas in new style */
2502   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii);CHKERRQ(ierr);
2503   if (!isascii) PetscFunctionReturn(0);
2504   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) pc), &rank);CHKERRQ(ierr);
2505   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2506   ierr = PetscViewerASCIIPrintf(viewer, "Subspace Correction preconditioner with %d patches\n", patch->npatch);CHKERRQ(ierr);
2507   if(patch->local_composition_type == PC_COMPOSITE_MULTIPLICATIVE) {
2508     ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: multiplicative\n");CHKERRQ(ierr);
2509   } else {
2510     ierr = PetscViewerASCIIPrintf(viewer, "Schwarz type: additive\n");CHKERRQ(ierr);
2511   }
2512   if (patch->partition_of_unity) {ierr = PetscViewerASCIIPrintf(viewer, "Weighting by partition of unity\n");CHKERRQ(ierr);}
2513   else                           {ierr = PetscViewerASCIIPrintf(viewer, "Not weighting by partition of unity\n");CHKERRQ(ierr);}
2514   if (patch->symmetrise_sweep) {ierr = PetscViewerASCIIPrintf(viewer, "Symmetrising sweep (start->end, then end->start)\n");CHKERRQ(ierr);}
2515   else                         {ierr = PetscViewerASCIIPrintf(viewer, "Not symmetrising sweep\n");CHKERRQ(ierr);}
2516   if (!patch->save_operators) {ierr = PetscViewerASCIIPrintf(viewer, "Not saving patch operators (rebuilt every PCApply)\n");CHKERRQ(ierr);}
2517   else                        {ierr = PetscViewerASCIIPrintf(viewer, "Saving patch operators (rebuilt every PCSetUp)\n");CHKERRQ(ierr);}
2518   if (patch->patchconstructop == PCPatchConstruct_Star)       {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: star\n");CHKERRQ(ierr);}
2519   else if (patch->patchconstructop == PCPatchConstruct_Vanka) {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: Vanka\n");CHKERRQ(ierr);}
2520   else if (patch->patchconstructop == PCPatchConstruct_User)  {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: user-specified\n");CHKERRQ(ierr);}
2521   else                                                        {ierr = PetscViewerASCIIPrintf(viewer, "Patch construction operator: unknown\n");CHKERRQ(ierr);}
2522   ierr = PetscViewerASCIIPrintf(viewer, "Solver on patches (all same):\n");CHKERRQ(ierr);
2523   if (patch->solver) {
2524     ierr = PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr);
2525     if (!rank) {
2526       ierr = PetscViewerASCIIPushTab(sviewer);CHKERRQ(ierr);
2527       ierr = PetscObjectView(patch->solver[0], sviewer);CHKERRQ(ierr);
2528       ierr = PetscViewerASCIIPopTab(sviewer);CHKERRQ(ierr);
2529     }
2530     ierr = PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer);CHKERRQ(ierr);
2531   } else {
2532     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2533     ierr = PetscViewerASCIIPrintf(viewer, "Solver not yet set.\n");CHKERRQ(ierr);
2534     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2535   }
2536   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2537   PetscFunctionReturn(0);
2538 }
2539 
2540 /*MC
2541   PCPATCH - A PC object that encapsulates flexible definition of blocks for overlapping and non-overlapping
2542             small block additive preconditioners. Block definition is based on topology from
2543             a DM and equation numbering from a PetscSection.
2544 
2545   Options Database Keys:
2546 + -pc_patch_cells_view   - Views the process local cell numbers for each patch
2547 . -pc_patch_points_view  - Views the process local mesh point numbers for each patch
2548 . -pc_patch_g2l_view     - Views the map between global dofs and patch local dofs for each patch
2549 . -pc_patch_patches_view - Views the global dofs associated with each patch and its boundary
2550 - -pc_patch_sub_mat_view - Views the matrix associated with each patch
2551 
2552   Level: intermediate
2553 
2554 .seealso: PCType, PCCreate(), PCSetType()
2555 M*/
2556 PETSC_EXTERN PetscErrorCode PCCreate_Patch(PC pc)
2557 {
2558   PC_PATCH      *patch;
2559   PetscErrorCode ierr;
2560 
2561   PetscFunctionBegin;
2562   ierr = PetscNewLog(pc, &patch);CHKERRQ(ierr);
2563 
2564   if (patch->subspaces_to_exclude) {
2565     PetscHSetIDestroy(&patch->subspaces_to_exclude);
2566   }
2567   PetscHSetICreate(&patch->subspaces_to_exclude);
2568 
2569   patch->classname = "pc";
2570   patch->isNonlinear = PETSC_FALSE;
2571 
2572   /* Set some defaults */
2573   patch->combined           = PETSC_FALSE;
2574   patch->save_operators     = PETSC_TRUE;
2575   patch->local_composition_type = PC_COMPOSITE_ADDITIVE;
2576   patch->partition_of_unity = PETSC_FALSE;
2577   patch->codim              = -1;
2578   patch->dim                = -1;
2579   patch->vankadim           = -1;
2580   patch->ignoredim          = -1;
2581   patch->patchconstructop   = PCPatchConstruct_Star;
2582   patch->symmetrise_sweep   = PETSC_FALSE;
2583   patch->npatch             = 0;
2584   patch->userIS             = NULL;
2585   patch->optionsSet         = PETSC_FALSE;
2586   patch->iterationSet       = NULL;
2587   patch->user_patches       = PETSC_FALSE;
2588   ierr = PetscStrallocpy(MATDENSE, (char **) &patch->sub_mat_type);CHKERRQ(ierr);
2589   patch->viewPatches        = PETSC_FALSE;
2590   patch->viewCells          = PETSC_FALSE;
2591   patch->viewPoints         = PETSC_FALSE;
2592   patch->viewSection        = PETSC_FALSE;
2593   patch->viewMatrix         = PETSC_FALSE;
2594   patch->setupsolver        = PCSetUp_PATCH_Linear;
2595   patch->applysolver        = PCApply_PATCH_Linear;
2596   patch->resetsolver        = PCReset_PATCH_Linear;
2597   patch->destroysolver      = PCDestroy_PATCH_Linear;
2598   patch->updatemultiplicative = PCUpdateMultiplicative_PATCH_Linear;
2599 
2600   pc->data                 = (void *) patch;
2601   pc->ops->apply           = PCApply_PATCH;
2602   pc->ops->applytranspose  = 0; /* PCApplyTranspose_PATCH; */
2603   pc->ops->setup           = PCSetUp_PATCH;
2604   pc->ops->reset           = PCReset_PATCH;
2605   pc->ops->destroy         = PCDestroy_PATCH;
2606   pc->ops->setfromoptions  = PCSetFromOptions_PATCH;
2607   pc->ops->setuponblocks   = PCSetUpOnBlocks_PATCH;
2608   pc->ops->view            = PCView_PATCH;
2609   pc->ops->applyrichardson = 0;
2610 
2611   PetscFunctionReturn(0);
2612 }
2613