xref: /petsc/src/snes/utils/dmplexsnes.c (revision 20cf1dd8cd656e27510885a71ea4be028bf48813)
1 #include <petsc/private/dmpleximpl.h>   /*I "petscdmplex.h" I*/
2 #include <petsc/private/snesimpl.h>     /*I "petscsnes.h"   I*/
3 #include <petscds.h>
4 #include <petscblaslapack.h>
5 #include <petsc/private/petscimpl.h>
6 #include <petsc/private/petscfeimpl.h>
7 
8 static PetscErrorCode ISGetPointRange(IS pointIS, PetscInt *pStart, PetscInt *pEnd, const PetscInt **points)
9 {
10   PetscInt       numCells, step = 1;
11   PetscBool      isStride;
12   PetscErrorCode ierr;
13 
14   PetscFunctionBeginHot;
15   *pStart = 0;
16   *points = NULL;
17   ierr = ISGetLocalSize(pointIS, &numCells);CHKERRQ(ierr);
18   ierr = PetscObjectTypeCompare((PetscObject) pointIS, ISSTRIDE, &isStride);CHKERRQ(ierr);
19   if (isStride) {ierr = ISStrideGetInfo(pointIS, pStart, &step);CHKERRQ(ierr);}
20   *pEnd   = *pStart + numCells;
21   if (!isStride || step != 1) {ierr = ISGetIndices(pointIS, points);CHKERRQ(ierr);}
22   PetscFunctionReturn(0);
23 }
24 
25 static PetscErrorCode ISRestorePointRange(IS pointIS, PetscInt *pStart, PetscInt *pEnd, const PetscInt **points)
26 {
27   PetscInt       step = 1;
28   PetscBool      isStride;
29   PetscErrorCode ierr;
30 
31   PetscFunctionBeginHot;
32   ierr = PetscObjectTypeCompare((PetscObject) pointIS, ISSTRIDE, &isStride);CHKERRQ(ierr);
33   if (isStride) {ierr = ISStrideGetInfo(pointIS, pStart, &step);CHKERRQ(ierr);}
34   if (!isStride || step != 1) {ierr = ISGetIndices(pointIS, points);CHKERRQ(ierr);}
35   PetscFunctionReturn(0);
36 }
37 
38 static PetscErrorCode ISGetPointSubrange(IS subpointIS, PetscInt pStart, PetscInt pEnd, const PetscInt *points)
39 {
40   PetscErrorCode ierr;
41 
42   PetscFunctionBeginHot;
43   if (points) {
44     ierr = ISSetType(subpointIS, ISGENERAL);CHKERRQ(ierr);
45     ierr = ISGeneralSetIndices(subpointIS, pEnd-pStart, &points[pStart], PETSC_USE_POINTER);CHKERRQ(ierr);
46   } else {
47     ierr = ISSetType(subpointIS, ISSTRIDE);CHKERRQ(ierr);
48     ierr = ISStrideSetStride(subpointIS, pEnd-pStart, pStart, 1);CHKERRQ(ierr);
49   }
50   PetscFunctionReturn(0);
51 }
52 
53 /************************** Interpolation *******************************/
54 
55 static PetscErrorCode DMSNESConvertPlex(DM dm, DM *plex, PetscBool copy)
56 {
57   PetscBool      isPlex;
58   PetscErrorCode ierr;
59 
60   PetscFunctionBegin;
61   ierr = PetscObjectTypeCompare((PetscObject) dm, DMPLEX, &isPlex);CHKERRQ(ierr);
62   if (isPlex) {
63     *plex = dm;
64     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
65   } else {
66     ierr = PetscObjectQuery((PetscObject) dm, "dm_plex", (PetscObject *) plex);CHKERRQ(ierr);
67     if (!*plex) {
68       ierr = DMConvert(dm,DMPLEX,plex);CHKERRQ(ierr);
69       ierr = PetscObjectCompose((PetscObject) dm, "dm_plex", (PetscObject) *plex);CHKERRQ(ierr);
70       if (copy) {
71         PetscInt    i;
72         PetscObject obj;
73         const char *comps[3] = {"A","dmAux","dmCh"};
74 
75         ierr = DMCopyDMSNES(dm, *plex);CHKERRQ(ierr);
76         for (i = 0; i < 3; i++) {
77           ierr = PetscObjectQuery((PetscObject) dm, comps[i], &obj);CHKERRQ(ierr);
78           ierr = PetscObjectCompose((PetscObject) *plex, comps[i], obj);CHKERRQ(ierr);
79         }
80       }
81     } else {
82       ierr = PetscObjectReference((PetscObject) *plex);CHKERRQ(ierr);
83     }
84   }
85   PetscFunctionReturn(0);
86 }
87 
88 PetscErrorCode DMInterpolationCreate(MPI_Comm comm, DMInterpolationInfo *ctx)
89 {
90   PetscErrorCode ierr;
91 
92   PetscFunctionBegin;
93   PetscValidPointer(ctx, 2);
94   ierr = PetscNew(ctx);CHKERRQ(ierr);
95 
96   (*ctx)->comm   = comm;
97   (*ctx)->dim    = -1;
98   (*ctx)->nInput = 0;
99   (*ctx)->points = NULL;
100   (*ctx)->cells  = NULL;
101   (*ctx)->n      = -1;
102   (*ctx)->coords = NULL;
103   PetscFunctionReturn(0);
104 }
105 
106 PetscErrorCode DMInterpolationSetDim(DMInterpolationInfo ctx, PetscInt dim)
107 {
108   PetscFunctionBegin;
109   if ((dim < 1) || (dim > 3)) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension for points: %d", dim);
110   ctx->dim = dim;
111   PetscFunctionReturn(0);
112 }
113 
114 PetscErrorCode DMInterpolationGetDim(DMInterpolationInfo ctx, PetscInt *dim)
115 {
116   PetscFunctionBegin;
117   PetscValidIntPointer(dim, 2);
118   *dim = ctx->dim;
119   PetscFunctionReturn(0);
120 }
121 
122 PetscErrorCode DMInterpolationSetDof(DMInterpolationInfo ctx, PetscInt dof)
123 {
124   PetscFunctionBegin;
125   if (dof < 1) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of components: %d", dof);
126   ctx->dof = dof;
127   PetscFunctionReturn(0);
128 }
129 
130 PetscErrorCode DMInterpolationGetDof(DMInterpolationInfo ctx, PetscInt *dof)
131 {
132   PetscFunctionBegin;
133   PetscValidIntPointer(dof, 2);
134   *dof = ctx->dof;
135   PetscFunctionReturn(0);
136 }
137 
138 PetscErrorCode DMInterpolationAddPoints(DMInterpolationInfo ctx, PetscInt n, PetscReal points[])
139 {
140   PetscErrorCode ierr;
141 
142   PetscFunctionBegin;
143   if (ctx->dim < 0) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set");
144   if (ctx->points)  SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "Cannot add points multiple times yet");
145   ctx->nInput = n;
146 
147   ierr = PetscMalloc1(n*ctx->dim, &ctx->points);CHKERRQ(ierr);
148   ierr = PetscMemcpy(ctx->points, points, n*ctx->dim * sizeof(PetscReal));CHKERRQ(ierr);
149   PetscFunctionReturn(0);
150 }
151 
152 PetscErrorCode DMInterpolationSetUp(DMInterpolationInfo ctx, DM dm, PetscBool redundantPoints)
153 {
154   MPI_Comm          comm = ctx->comm;
155   PetscScalar       *a;
156   PetscInt          p, q, i;
157   PetscMPIInt       rank, size;
158   PetscErrorCode    ierr;
159   Vec               pointVec;
160   PetscSF           cellSF;
161   PetscLayout       layout;
162   PetscReal         *globalPoints;
163   PetscScalar       *globalPointsScalar;
164   const PetscInt    *ranges;
165   PetscMPIInt       *counts, *displs;
166   const PetscSFNode *foundCells;
167   const PetscInt    *foundPoints;
168   PetscMPIInt       *foundProcs, *globalProcs;
169   PetscInt          n, N, numFound;
170 
171   PetscFunctionBegin;
172   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
173   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
174   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
175   if (ctx->dim < 0) SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set");
176   /* Locate points */
177   n = ctx->nInput;
178   if (!redundantPoints) {
179     ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
180     ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
181     ierr = PetscLayoutSetLocalSize(layout, n);CHKERRQ(ierr);
182     ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
183     ierr = PetscLayoutGetSize(layout, &N);CHKERRQ(ierr);
184     /* Communicate all points to all processes */
185     ierr = PetscMalloc3(N*ctx->dim,&globalPoints,size,&counts,size,&displs);CHKERRQ(ierr);
186     ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
187     for (p = 0; p < size; ++p) {
188       counts[p] = (ranges[p+1] - ranges[p])*ctx->dim;
189       displs[p] = ranges[p]*ctx->dim;
190     }
191     ierr = MPI_Allgatherv(ctx->points, n*ctx->dim, MPIU_REAL, globalPoints, counts, displs, MPIU_REAL, comm);CHKERRQ(ierr);
192   } else {
193     N = n;
194     globalPoints = ctx->points;
195     counts = displs = NULL;
196     layout = NULL;
197   }
198 #if 0
199   ierr = PetscMalloc3(N,&foundCells,N,&foundProcs,N,&globalProcs);CHKERRQ(ierr);
200   /* foundCells[p] = m->locatePoint(&globalPoints[p*ctx->dim]); */
201 #else
202 #if defined(PETSC_USE_COMPLEX)
203   ierr = PetscMalloc1(N*ctx->dim,&globalPointsScalar);CHKERRQ(ierr);
204   for (i=0; i<N*ctx->dim; i++) globalPointsScalar[i] = globalPoints[i];
205 #else
206   globalPointsScalar = globalPoints;
207 #endif
208   ierr = VecCreateSeqWithArray(PETSC_COMM_SELF, ctx->dim, N*ctx->dim, globalPointsScalar, &pointVec);CHKERRQ(ierr);
209   ierr = PetscMalloc2(N,&foundProcs,N,&globalProcs);CHKERRQ(ierr);
210   for (p = 0; p < N; ++p) {foundProcs[p] = size;}
211   cellSF = NULL;
212   ierr = DMLocatePoints(dm, pointVec, DM_POINTLOCATION_REMOVE, &cellSF);CHKERRQ(ierr);
213   ierr = PetscSFGetGraph(cellSF,NULL,&numFound,&foundPoints,&foundCells);CHKERRQ(ierr);
214 #endif
215   for (p = 0; p < numFound; ++p) {
216     if (foundCells[p].index >= 0) foundProcs[foundPoints ? foundPoints[p] : p] = rank;
217   }
218   /* Let the lowest rank process own each point */
219   ierr   = MPIU_Allreduce(foundProcs, globalProcs, N, MPI_INT, MPI_MIN, comm);CHKERRQ(ierr);
220   ctx->n = 0;
221   for (p = 0; p < N; ++p) {
222     if (globalProcs[p] == size) SETERRQ4(comm, PETSC_ERR_PLIB, "Point %d: %g %g %g not located in mesh", p, (double)globalPoints[p*ctx->dim+0], (double)(ctx->dim > 1 ? globalPoints[p*ctx->dim+1] : 0.0), (double)(ctx->dim > 2 ? globalPoints[p*ctx->dim+2] : 0.0));
223     else if (globalProcs[p] == rank) ctx->n++;
224   }
225   /* Create coordinates vector and array of owned cells */
226   ierr = PetscMalloc1(ctx->n, &ctx->cells);CHKERRQ(ierr);
227   ierr = VecCreate(comm, &ctx->coords);CHKERRQ(ierr);
228   ierr = VecSetSizes(ctx->coords, ctx->n*ctx->dim, PETSC_DECIDE);CHKERRQ(ierr);
229   ierr = VecSetBlockSize(ctx->coords, ctx->dim);CHKERRQ(ierr);
230   ierr = VecSetType(ctx->coords,VECSTANDARD);CHKERRQ(ierr);
231   ierr = VecGetArray(ctx->coords, &a);CHKERRQ(ierr);
232   for (p = 0, q = 0, i = 0; p < N; ++p) {
233     if (globalProcs[p] == rank) {
234       PetscInt d;
235 
236       for (d = 0; d < ctx->dim; ++d, ++i) a[i] = globalPoints[p*ctx->dim+d];
237       ctx->cells[q] = foundCells[q].index;
238       ++q;
239     }
240   }
241   ierr = VecRestoreArray(ctx->coords, &a);CHKERRQ(ierr);
242 #if 0
243   ierr = PetscFree3(foundCells,foundProcs,globalProcs);CHKERRQ(ierr);
244 #else
245   ierr = PetscFree2(foundProcs,globalProcs);CHKERRQ(ierr);
246   ierr = PetscSFDestroy(&cellSF);CHKERRQ(ierr);
247   ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
248 #endif
249   if ((void*)globalPointsScalar != (void*)globalPoints) {ierr = PetscFree(globalPointsScalar);CHKERRQ(ierr);}
250   if (!redundantPoints) {ierr = PetscFree3(globalPoints,counts,displs);CHKERRQ(ierr);}
251   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
252   PetscFunctionReturn(0);
253 }
254 
255 PetscErrorCode DMInterpolationGetCoordinates(DMInterpolationInfo ctx, Vec *coordinates)
256 {
257   PetscFunctionBegin;
258   PetscValidPointer(coordinates, 2);
259   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
260   *coordinates = ctx->coords;
261   PetscFunctionReturn(0);
262 }
263 
264 PetscErrorCode DMInterpolationGetVector(DMInterpolationInfo ctx, Vec *v)
265 {
266   PetscErrorCode ierr;
267 
268   PetscFunctionBegin;
269   PetscValidPointer(v, 2);
270   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
271   ierr = VecCreate(ctx->comm, v);CHKERRQ(ierr);
272   ierr = VecSetSizes(*v, ctx->n*ctx->dof, PETSC_DECIDE);CHKERRQ(ierr);
273   ierr = VecSetBlockSize(*v, ctx->dof);CHKERRQ(ierr);
274   ierr = VecSetType(*v,VECSTANDARD);CHKERRQ(ierr);
275   PetscFunctionReturn(0);
276 }
277 
278 PetscErrorCode DMInterpolationRestoreVector(DMInterpolationInfo ctx, Vec *v)
279 {
280   PetscErrorCode ierr;
281 
282   PetscFunctionBegin;
283   PetscValidPointer(v, 2);
284   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
285   ierr = VecDestroy(v);CHKERRQ(ierr);
286   PetscFunctionReturn(0);
287 }
288 
289 PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Triangle_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
290 {
291   PetscReal      *v0, *J, *invJ, detJ;
292   const PetscScalar *coords;
293   PetscScalar    *a;
294   PetscInt       p;
295   PetscErrorCode ierr;
296 
297   PetscFunctionBegin;
298   ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr);
299   ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
300   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
301   for (p = 0; p < ctx->n; ++p) {
302     PetscInt     c = ctx->cells[p];
303     PetscScalar *x = NULL;
304     PetscReal    xi[4];
305     PetscInt     d, f, comp;
306 
307     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
308     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", (double)detJ, c);
309     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
310     for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp];
311 
312     for (d = 0; d < ctx->dim; ++d) {
313       xi[d] = 0.0;
314       for (f = 0; f < ctx->dim; ++f) xi[d] += invJ[d*ctx->dim+f]*0.5*PetscRealPart(coords[p*ctx->dim+f] - v0[f]);
315       for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] += PetscRealPart(x[(d+1)*ctx->dof+comp] - x[0*ctx->dof+comp])*xi[d];
316     }
317     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
318   }
319   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
320   ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
321   ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr);
322   PetscFunctionReturn(0);
323 }
324 
325 PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Tetrahedron_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
326 {
327   PetscReal      *v0, *J, *invJ, detJ;
328   const PetscScalar *coords;
329   PetscScalar    *a;
330   PetscInt       p;
331   PetscErrorCode ierr;
332 
333   PetscFunctionBegin;
334   ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr);
335   ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
336   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
337   for (p = 0; p < ctx->n; ++p) {
338     PetscInt       c = ctx->cells[p];
339     const PetscInt order[3] = {2, 1, 3};
340     PetscScalar   *x = NULL;
341     PetscReal      xi[4];
342     PetscInt       d, f, comp;
343 
344     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
345     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", (double)detJ, c);
346     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
347     for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp];
348 
349     for (d = 0; d < ctx->dim; ++d) {
350       xi[d] = 0.0;
351       for (f = 0; f < ctx->dim; ++f) xi[d] += invJ[d*ctx->dim+f]*0.5*PetscRealPart(coords[p*ctx->dim+f] - v0[f]);
352       for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] += PetscRealPart(x[order[d]*ctx->dof+comp] - x[0*ctx->dof+comp])*xi[d];
353     }
354     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
355   }
356   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
357   ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
358   ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr);
359   PetscFunctionReturn(0);
360 }
361 
362 PETSC_STATIC_INLINE PetscErrorCode QuadMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx)
363 {
364   const PetscScalar *vertices = (const PetscScalar*) ctx;
365   const PetscScalar x0        = vertices[0];
366   const PetscScalar y0        = vertices[1];
367   const PetscScalar x1        = vertices[2];
368   const PetscScalar y1        = vertices[3];
369   const PetscScalar x2        = vertices[4];
370   const PetscScalar y2        = vertices[5];
371   const PetscScalar x3        = vertices[6];
372   const PetscScalar y3        = vertices[7];
373   const PetscScalar f_1       = x1 - x0;
374   const PetscScalar g_1       = y1 - y0;
375   const PetscScalar f_3       = x3 - x0;
376   const PetscScalar g_3       = y3 - y0;
377   const PetscScalar f_01      = x2 - x1 - x3 + x0;
378   const PetscScalar g_01      = y2 - y1 - y3 + y0;
379   const PetscScalar *ref;
380   PetscScalar       *real;
381   PetscErrorCode    ierr;
382 
383   PetscFunctionBegin;
384   ierr = VecGetArrayRead(Xref,  &ref);CHKERRQ(ierr);
385   ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr);
386   {
387     const PetscScalar p0 = ref[0];
388     const PetscScalar p1 = ref[1];
389 
390     real[0] = x0 + f_1 * p0 + f_3 * p1 + f_01 * p0 * p1;
391     real[1] = y0 + g_1 * p0 + g_3 * p1 + g_01 * p0 * p1;
392   }
393   ierr = PetscLogFlops(28);CHKERRQ(ierr);
394   ierr = VecRestoreArrayRead(Xref,  &ref);CHKERRQ(ierr);
395   ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr);
396   PetscFunctionReturn(0);
397 }
398 
399 #include <petsc/private/dmimpl.h>
400 PETSC_STATIC_INLINE PetscErrorCode QuadJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx)
401 {
402   const PetscScalar *vertices = (const PetscScalar*) ctx;
403   const PetscScalar x0        = vertices[0];
404   const PetscScalar y0        = vertices[1];
405   const PetscScalar x1        = vertices[2];
406   const PetscScalar y1        = vertices[3];
407   const PetscScalar x2        = vertices[4];
408   const PetscScalar y2        = vertices[5];
409   const PetscScalar x3        = vertices[6];
410   const PetscScalar y3        = vertices[7];
411   const PetscScalar f_01      = x2 - x1 - x3 + x0;
412   const PetscScalar g_01      = y2 - y1 - y3 + y0;
413   const PetscScalar *ref;
414   PetscErrorCode    ierr;
415 
416   PetscFunctionBegin;
417   ierr = VecGetArrayRead(Xref,  &ref);CHKERRQ(ierr);
418   {
419     const PetscScalar x       = ref[0];
420     const PetscScalar y       = ref[1];
421     const PetscInt    rows[2] = {0, 1};
422     PetscScalar       values[4];
423 
424     values[0] = (x1 - x0 + f_01*y) * 0.5; values[1] = (x3 - x0 + f_01*x) * 0.5;
425     values[2] = (y1 - y0 + g_01*y) * 0.5; values[3] = (y3 - y0 + g_01*x) * 0.5;
426     ierr      = MatSetValues(J, 2, rows, 2, rows, values, INSERT_VALUES);CHKERRQ(ierr);
427   }
428   ierr = PetscLogFlops(30);CHKERRQ(ierr);
429   ierr = VecRestoreArrayRead(Xref,  &ref);CHKERRQ(ierr);
430   ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
431   ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
432   PetscFunctionReturn(0);
433 }
434 
435 PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Quad_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
436 {
437   DM             dmCoord;
438   PetscFE        fem = NULL;
439   SNES           snes;
440   KSP            ksp;
441   PC             pc;
442   Vec            coordsLocal, r, ref, real;
443   Mat            J;
444   const PetscScalar *coords;
445   PetscScalar    *a;
446   PetscInt       Nf, p;
447   const PetscInt dof = ctx->dof;
448   PetscErrorCode ierr;
449 
450   PetscFunctionBegin;
451   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
452   if (Nf) {ierr = DMGetField(dm, 0, (PetscObject *) &fem);CHKERRQ(ierr);}
453   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
454   ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr);
455   ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr);
456   ierr = SNESSetOptionsPrefix(snes, "quad_interp_");CHKERRQ(ierr);
457   ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr);
458   ierr = VecSetSizes(r, 2, 2);CHKERRQ(ierr);
459   ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr);
460   ierr = VecDuplicate(r, &ref);CHKERRQ(ierr);
461   ierr = VecDuplicate(r, &real);CHKERRQ(ierr);
462   ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr);
463   ierr = MatSetSizes(J, 2, 2, 2, 2);CHKERRQ(ierr);
464   ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr);
465   ierr = MatSetUp(J);CHKERRQ(ierr);
466   ierr = SNESSetFunction(snes, r, QuadMap_Private, NULL);CHKERRQ(ierr);
467   ierr = SNESSetJacobian(snes, J, J, QuadJacobian_Private, NULL);CHKERRQ(ierr);
468   ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr);
469   ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
470   ierr = PCSetType(pc, PCLU);CHKERRQ(ierr);
471   ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
472 
473   ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
474   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
475   for (p = 0; p < ctx->n; ++p) {
476     PetscScalar *x = NULL, *vertices = NULL;
477     PetscScalar *xi;
478     PetscReal    xir[2];
479     PetscInt     c = ctx->cells[p], comp, coordSize, xSize;
480 
481     /* Can make this do all points at once */
482     ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
483     if (4*2 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 4*2);
484     ierr   = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
485     ierr   = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
486     ierr   = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
487     ierr   = VecGetArray(real, &xi);CHKERRQ(ierr);
488     xi[0]  = coords[p*ctx->dim+0];
489     xi[1]  = coords[p*ctx->dim+1];
490     ierr   = VecRestoreArray(real, &xi);CHKERRQ(ierr);
491     ierr   = SNESSolve(snes, real, ref);CHKERRQ(ierr);
492     ierr   = VecGetArray(ref, &xi);CHKERRQ(ierr);
493     xir[0] = PetscRealPart(xi[0]);
494     xir[1] = PetscRealPart(xi[1]);
495     if (4*dof != xSize) {
496       PetscReal *B;
497       PetscInt   d;
498 
499       xir[0] = 2.0*xir[0] - 1.0; xir[1] = 2.0*xir[1] - 1.0;
500       ierr = PetscFEGetTabulation(fem, 1, xir, &B, NULL, NULL);CHKERRQ(ierr);
501       for (comp = 0; comp < dof; ++comp) {
502         a[p*dof+comp] = 0.0;
503         for (d = 0; d < xSize/dof; ++d) {
504           a[p*dof+comp] += x[d*dof+comp]*B[d*dof+comp];
505         }
506       }
507       ierr = PetscFERestoreTabulation(fem, 1, xir, &B, NULL, NULL);CHKERRQ(ierr);
508     } else {
509       for (comp = 0; comp < dof; ++comp)
510         a[p*dof+comp] = x[0*dof+comp]*(1 - xir[0])*(1 - xir[1]) + x[1*dof+comp]*xir[0]*(1 - xir[1]) + x[2*dof+comp]*xir[0]*xir[1] + x[3*dof+comp]*(1 - xir[0])*xir[1];
511     }
512     ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr);
513     ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
514     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
515   }
516   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
517   ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
518 
519   ierr = SNESDestroy(&snes);CHKERRQ(ierr);
520   ierr = VecDestroy(&r);CHKERRQ(ierr);
521   ierr = VecDestroy(&ref);CHKERRQ(ierr);
522   ierr = VecDestroy(&real);CHKERRQ(ierr);
523   ierr = MatDestroy(&J);CHKERRQ(ierr);
524   PetscFunctionReturn(0);
525 }
526 
527 PETSC_STATIC_INLINE PetscErrorCode HexMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx)
528 {
529   const PetscScalar *vertices = (const PetscScalar*) ctx;
530   const PetscScalar x0        = vertices[0];
531   const PetscScalar y0        = vertices[1];
532   const PetscScalar z0        = vertices[2];
533   const PetscScalar x1        = vertices[9];
534   const PetscScalar y1        = vertices[10];
535   const PetscScalar z1        = vertices[11];
536   const PetscScalar x2        = vertices[6];
537   const PetscScalar y2        = vertices[7];
538   const PetscScalar z2        = vertices[8];
539   const PetscScalar x3        = vertices[3];
540   const PetscScalar y3        = vertices[4];
541   const PetscScalar z3        = vertices[5];
542   const PetscScalar x4        = vertices[12];
543   const PetscScalar y4        = vertices[13];
544   const PetscScalar z4        = vertices[14];
545   const PetscScalar x5        = vertices[15];
546   const PetscScalar y5        = vertices[16];
547   const PetscScalar z5        = vertices[17];
548   const PetscScalar x6        = vertices[18];
549   const PetscScalar y6        = vertices[19];
550   const PetscScalar z6        = vertices[20];
551   const PetscScalar x7        = vertices[21];
552   const PetscScalar y7        = vertices[22];
553   const PetscScalar z7        = vertices[23];
554   const PetscScalar f_1       = x1 - x0;
555   const PetscScalar g_1       = y1 - y0;
556   const PetscScalar h_1       = z1 - z0;
557   const PetscScalar f_3       = x3 - x0;
558   const PetscScalar g_3       = y3 - y0;
559   const PetscScalar h_3       = z3 - z0;
560   const PetscScalar f_4       = x4 - x0;
561   const PetscScalar g_4       = y4 - y0;
562   const PetscScalar h_4       = z4 - z0;
563   const PetscScalar f_01      = x2 - x1 - x3 + x0;
564   const PetscScalar g_01      = y2 - y1 - y3 + y0;
565   const PetscScalar h_01      = z2 - z1 - z3 + z0;
566   const PetscScalar f_12      = x7 - x3 - x4 + x0;
567   const PetscScalar g_12      = y7 - y3 - y4 + y0;
568   const PetscScalar h_12      = z7 - z3 - z4 + z0;
569   const PetscScalar f_02      = x5 - x1 - x4 + x0;
570   const PetscScalar g_02      = y5 - y1 - y4 + y0;
571   const PetscScalar h_02      = z5 - z1 - z4 + z0;
572   const PetscScalar f_012     = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7;
573   const PetscScalar g_012     = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7;
574   const PetscScalar h_012     = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7;
575   const PetscScalar *ref;
576   PetscScalar       *real;
577   PetscErrorCode    ierr;
578 
579   PetscFunctionBegin;
580   ierr = VecGetArrayRead(Xref,  &ref);CHKERRQ(ierr);
581   ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr);
582   {
583     const PetscScalar p0 = ref[0];
584     const PetscScalar p1 = ref[1];
585     const PetscScalar p2 = ref[2];
586 
587     real[0] = x0 + f_1*p0 + f_3*p1 + f_4*p2 + f_01*p0*p1 + f_12*p1*p2 + f_02*p0*p2 + f_012*p0*p1*p2;
588     real[1] = y0 + g_1*p0 + g_3*p1 + g_4*p2 + g_01*p0*p1 + g_01*p0*p1 + g_12*p1*p2 + g_02*p0*p2 + g_012*p0*p1*p2;
589     real[2] = z0 + h_1*p0 + h_3*p1 + h_4*p2 + h_01*p0*p1 + h_01*p0*p1 + h_12*p1*p2 + h_02*p0*p2 + h_012*p0*p1*p2;
590   }
591   ierr = PetscLogFlops(114);CHKERRQ(ierr);
592   ierr = VecRestoreArrayRead(Xref,  &ref);CHKERRQ(ierr);
593   ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr);
594   PetscFunctionReturn(0);
595 }
596 
597 PETSC_STATIC_INLINE PetscErrorCode HexJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx)
598 {
599   const PetscScalar *vertices = (const PetscScalar*) ctx;
600   const PetscScalar x0        = vertices[0];
601   const PetscScalar y0        = vertices[1];
602   const PetscScalar z0        = vertices[2];
603   const PetscScalar x1        = vertices[9];
604   const PetscScalar y1        = vertices[10];
605   const PetscScalar z1        = vertices[11];
606   const PetscScalar x2        = vertices[6];
607   const PetscScalar y2        = vertices[7];
608   const PetscScalar z2        = vertices[8];
609   const PetscScalar x3        = vertices[3];
610   const PetscScalar y3        = vertices[4];
611   const PetscScalar z3        = vertices[5];
612   const PetscScalar x4        = vertices[12];
613   const PetscScalar y4        = vertices[13];
614   const PetscScalar z4        = vertices[14];
615   const PetscScalar x5        = vertices[15];
616   const PetscScalar y5        = vertices[16];
617   const PetscScalar z5        = vertices[17];
618   const PetscScalar x6        = vertices[18];
619   const PetscScalar y6        = vertices[19];
620   const PetscScalar z6        = vertices[20];
621   const PetscScalar x7        = vertices[21];
622   const PetscScalar y7        = vertices[22];
623   const PetscScalar z7        = vertices[23];
624   const PetscScalar f_xy      = x2 - x1 - x3 + x0;
625   const PetscScalar g_xy      = y2 - y1 - y3 + y0;
626   const PetscScalar h_xy      = z2 - z1 - z3 + z0;
627   const PetscScalar f_yz      = x7 - x3 - x4 + x0;
628   const PetscScalar g_yz      = y7 - y3 - y4 + y0;
629   const PetscScalar h_yz      = z7 - z3 - z4 + z0;
630   const PetscScalar f_xz      = x5 - x1 - x4 + x0;
631   const PetscScalar g_xz      = y5 - y1 - y4 + y0;
632   const PetscScalar h_xz      = z5 - z1 - z4 + z0;
633   const PetscScalar f_xyz     = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7;
634   const PetscScalar g_xyz     = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7;
635   const PetscScalar h_xyz     = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7;
636   const PetscScalar *ref;
637   PetscErrorCode    ierr;
638 
639   PetscFunctionBegin;
640   ierr = VecGetArrayRead(Xref,  &ref);CHKERRQ(ierr);
641   {
642     const PetscScalar x       = ref[0];
643     const PetscScalar y       = ref[1];
644     const PetscScalar z       = ref[2];
645     const PetscInt    rows[3] = {0, 1, 2};
646     PetscScalar       values[9];
647 
648     values[0] = (x1 - x0 + f_xy*y + f_xz*z + f_xyz*y*z) / 2.0;
649     values[1] = (x3 - x0 + f_xy*x + f_yz*z + f_xyz*x*z) / 2.0;
650     values[2] = (x4 - x0 + f_yz*y + f_xz*x + f_xyz*x*y) / 2.0;
651     values[3] = (y1 - y0 + g_xy*y + g_xz*z + g_xyz*y*z) / 2.0;
652     values[4] = (y3 - y0 + g_xy*x + g_yz*z + g_xyz*x*z) / 2.0;
653     values[5] = (y4 - y0 + g_yz*y + g_xz*x + g_xyz*x*y) / 2.0;
654     values[6] = (z1 - z0 + h_xy*y + h_xz*z + h_xyz*y*z) / 2.0;
655     values[7] = (z3 - z0 + h_xy*x + h_yz*z + h_xyz*x*z) / 2.0;
656     values[8] = (z4 - z0 + h_yz*y + h_xz*x + h_xyz*x*y) / 2.0;
657 
658     ierr = MatSetValues(J, 3, rows, 3, rows, values, INSERT_VALUES);CHKERRQ(ierr);
659   }
660   ierr = PetscLogFlops(152);CHKERRQ(ierr);
661   ierr = VecRestoreArrayRead(Xref,  &ref);CHKERRQ(ierr);
662   ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
663   ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
664   PetscFunctionReturn(0);
665 }
666 
667 PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Hex_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
668 {
669   DM             dmCoord;
670   SNES           snes;
671   KSP            ksp;
672   PC             pc;
673   Vec            coordsLocal, r, ref, real;
674   Mat            J;
675   const PetscScalar *coords;
676   PetscScalar    *a;
677   PetscInt       p;
678   PetscErrorCode ierr;
679 
680   PetscFunctionBegin;
681   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
682   ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr);
683   ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr);
684   ierr = SNESSetOptionsPrefix(snes, "hex_interp_");CHKERRQ(ierr);
685   ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr);
686   ierr = VecSetSizes(r, 3, 3);CHKERRQ(ierr);
687   ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr);
688   ierr = VecDuplicate(r, &ref);CHKERRQ(ierr);
689   ierr = VecDuplicate(r, &real);CHKERRQ(ierr);
690   ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr);
691   ierr = MatSetSizes(J, 3, 3, 3, 3);CHKERRQ(ierr);
692   ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr);
693   ierr = MatSetUp(J);CHKERRQ(ierr);
694   ierr = SNESSetFunction(snes, r, HexMap_Private, NULL);CHKERRQ(ierr);
695   ierr = SNESSetJacobian(snes, J, J, HexJacobian_Private, NULL);CHKERRQ(ierr);
696   ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr);
697   ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
698   ierr = PCSetType(pc, PCLU);CHKERRQ(ierr);
699   ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
700 
701   ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
702   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
703   for (p = 0; p < ctx->n; ++p) {
704     PetscScalar *x = NULL, *vertices = NULL;
705     PetscScalar *xi;
706     PetscReal    xir[3];
707     PetscInt     c = ctx->cells[p], comp, coordSize, xSize;
708 
709     /* Can make this do all points at once */
710     ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
711     if (8*3 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 8*3);
712     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
713     if (8*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 8*ctx->dof);
714     ierr   = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
715     ierr   = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
716     ierr   = VecGetArray(real, &xi);CHKERRQ(ierr);
717     xi[0]  = coords[p*ctx->dim+0];
718     xi[1]  = coords[p*ctx->dim+1];
719     xi[2]  = coords[p*ctx->dim+2];
720     ierr   = VecRestoreArray(real, &xi);CHKERRQ(ierr);
721     ierr   = SNESSolve(snes, real, ref);CHKERRQ(ierr);
722     ierr   = VecGetArray(ref, &xi);CHKERRQ(ierr);
723     xir[0] = PetscRealPart(xi[0]);
724     xir[1] = PetscRealPart(xi[1]);
725     xir[2] = PetscRealPart(xi[2]);
726     for (comp = 0; comp < ctx->dof; ++comp) {
727       a[p*ctx->dof+comp] =
728         x[0*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*(1-xir[2]) +
729         x[3*ctx->dof+comp]*    xir[0]*(1-xir[1])*(1-xir[2]) +
730         x[2*ctx->dof+comp]*    xir[0]*    xir[1]*(1-xir[2]) +
731         x[1*ctx->dof+comp]*(1-xir[0])*    xir[1]*(1-xir[2]) +
732         x[4*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*   xir[2] +
733         x[5*ctx->dof+comp]*    xir[0]*(1-xir[1])*   xir[2] +
734         x[6*ctx->dof+comp]*    xir[0]*    xir[1]*   xir[2] +
735         x[7*ctx->dof+comp]*(1-xir[0])*    xir[1]*   xir[2];
736     }
737     ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr);
738     ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
739     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
740   }
741   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
742   ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
743 
744   ierr = SNESDestroy(&snes);CHKERRQ(ierr);
745   ierr = VecDestroy(&r);CHKERRQ(ierr);
746   ierr = VecDestroy(&ref);CHKERRQ(ierr);
747   ierr = VecDestroy(&real);CHKERRQ(ierr);
748   ierr = MatDestroy(&J);CHKERRQ(ierr);
749   PetscFunctionReturn(0);
750 }
751 
752 /*
753   Input Parameters:
754 + ctx - The DMInterpolationInfo context
755 . dm  - The DM
756 - x   - The local vector containing the field to be interpolated
757 
758   Output Parameters:
759 . v   - The vector containing the interpolated values
760 */
761 PetscErrorCode DMInterpolationEvaluate(DMInterpolationInfo ctx, DM dm, Vec x, Vec v)
762 {
763   PetscInt       dim, coneSize, n;
764   PetscErrorCode ierr;
765 
766   PetscFunctionBegin;
767   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
768   PetscValidHeaderSpecific(x, VEC_CLASSID, 3);
769   PetscValidHeaderSpecific(v, VEC_CLASSID, 4);
770   ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr);
771   if (n != ctx->n*ctx->dof) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid input vector size %d should be %d", n, ctx->n*ctx->dof);
772   if (n) {
773     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
774     ierr = DMPlexGetConeSize(dm, ctx->cells[0], &coneSize);CHKERRQ(ierr);
775     if (dim == 2) {
776       if (coneSize == 3) {
777         ierr = DMInterpolate_Triangle_Private(ctx, dm, x, v);CHKERRQ(ierr);
778       } else if (coneSize == 4) {
779         ierr = DMInterpolate_Quad_Private(ctx, dm, x, v);CHKERRQ(ierr);
780       } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim);
781     } else if (dim == 3) {
782       if (coneSize == 4) {
783         ierr = DMInterpolate_Tetrahedron_Private(ctx, dm, x, v);CHKERRQ(ierr);
784       } else {
785         ierr = DMInterpolate_Hex_Private(ctx, dm, x, v);CHKERRQ(ierr);
786       }
787     } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim);
788   }
789   PetscFunctionReturn(0);
790 }
791 
792 PetscErrorCode DMInterpolationDestroy(DMInterpolationInfo *ctx)
793 {
794   PetscErrorCode ierr;
795 
796   PetscFunctionBegin;
797   PetscValidPointer(ctx, 2);
798   ierr = VecDestroy(&(*ctx)->coords);CHKERRQ(ierr);
799   ierr = PetscFree((*ctx)->points);CHKERRQ(ierr);
800   ierr = PetscFree((*ctx)->cells);CHKERRQ(ierr);
801   ierr = PetscFree(*ctx);CHKERRQ(ierr);
802   *ctx = NULL;
803   PetscFunctionReturn(0);
804 }
805 
806 /*@C
807   SNESMonitorFields - Monitors the residual for each field separately
808 
809   Collective on SNES
810 
811   Input Parameters:
812 + snes   - the SNES context
813 . its    - iteration number
814 . fgnorm - 2-norm of residual
815 - vf  - PetscViewerAndFormat of type ASCII
816 
817   Notes:
818   This routine prints the residual norm at each iteration.
819 
820   Level: intermediate
821 
822 .keywords: SNES, nonlinear, default, monitor, norm
823 .seealso: SNESMonitorSet(), SNESMonitorDefault()
824 @*/
825 PetscErrorCode SNESMonitorFields(SNES snes, PetscInt its, PetscReal fgnorm, PetscViewerAndFormat *vf)
826 {
827   PetscViewer        viewer = vf->viewer;
828   Vec                res;
829   DM                 dm;
830   PetscSection       s;
831   const PetscScalar *r;
832   PetscReal         *lnorms, *norms;
833   PetscInt           numFields, f, pStart, pEnd, p;
834   PetscErrorCode     ierr;
835 
836   PetscFunctionBegin;
837   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
838   ierr = SNESGetFunction(snes, &res, 0, 0);CHKERRQ(ierr);
839   ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr);
840   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
841   ierr = PetscSectionGetNumFields(s, &numFields);CHKERRQ(ierr);
842   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
843   ierr = PetscCalloc2(numFields, &lnorms, numFields, &norms);CHKERRQ(ierr);
844   ierr = VecGetArrayRead(res, &r);CHKERRQ(ierr);
845   for (p = pStart; p < pEnd; ++p) {
846     for (f = 0; f < numFields; ++f) {
847       PetscInt fdof, foff, d;
848 
849       ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr);
850       ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr);
851       for (d = 0; d < fdof; ++d) lnorms[f] += PetscRealPart(PetscSqr(r[foff+d]));
852     }
853   }
854   ierr = VecRestoreArrayRead(res, &r);CHKERRQ(ierr);
855   ierr = MPIU_Allreduce(lnorms, norms, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
856   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
857   ierr = PetscViewerASCIIAddTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr);
858   ierr = PetscViewerASCIIPrintf(viewer, "%3D SNES Function norm %14.12e [", its, (double) fgnorm);CHKERRQ(ierr);
859   for (f = 0; f < numFields; ++f) {
860     if (f > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
861     ierr = PetscViewerASCIIPrintf(viewer, "%14.12e", (double) PetscSqrtReal(norms[f]));CHKERRQ(ierr);
862   }
863   ierr = PetscViewerASCIIPrintf(viewer, "]\n");CHKERRQ(ierr);
864   ierr = PetscViewerASCIISubtractTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr);
865   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
866   ierr = PetscFree2(lnorms, norms);CHKERRQ(ierr);
867   PetscFunctionReturn(0);
868 }
869 
870 /********************* Residual Computation **************************/
871 
872 static PetscErrorCode PetscContainerUserDestroy_PetscFEGeom (void *ctx)
873 {
874   PetscFEGeom *geom = (PetscFEGeom *) ctx;
875   PetscErrorCode ierr;
876 
877   PetscFunctionBegin;
878   ierr = PetscFEGeomDestroy(&geom);CHKERRQ(ierr);
879   PetscFunctionReturn(0);
880 }
881 
882 static PetscErrorCode DMSNESGetFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
883 {
884   char            composeStr[33] = {0};
885   PetscObjectId   id;
886   PetscContainer  container;
887   PetscErrorCode  ierr;
888 
889   PetscFunctionBegin;
890   ierr = PetscObjectGetId((PetscObject)quad,&id);CHKERRQ(ierr);
891   ierr = PetscSNPrintf(composeStr, 32, "DMSNESGetFEGeom_%x\n", id);CHKERRQ(ierr);
892   ierr = PetscObjectQuery((PetscObject) pointIS, composeStr, (PetscObject *) &container);CHKERRQ(ierr);
893   if (container) {
894     ierr = PetscContainerGetPointer(container, (void **) geom);CHKERRQ(ierr);
895   } else {
896     ierr = DMFieldCreateFEGeom(coordField, pointIS, quad, faceData, geom);CHKERRQ(ierr);
897     ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr);
898     ierr = PetscContainerSetPointer(container, (void *) *geom);CHKERRQ(ierr);
899     ierr = PetscContainerSetUserDestroy(container, PetscContainerUserDestroy_PetscFEGeom);CHKERRQ(ierr);
900     ierr = PetscObjectCompose((PetscObject) pointIS, composeStr, (PetscObject) container);CHKERRQ(ierr);
901     ierr = PetscContainerDestroy(&container);CHKERRQ(ierr);
902   }
903   PetscFunctionReturn(0);
904 }
905 
906 static PetscErrorCode DMSNESRestoreFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
907 {
908   PetscFunctionBegin;
909   *geom = NULL;
910   PetscFunctionReturn(0);
911 }
912 
913 /*@
914   DMPlexSNESGetGeometryFVM - Return precomputed geometric data
915 
916   Input Parameter:
917 . dm - The DM
918 
919   Output Parameters:
920 + facegeom - The values precomputed from face geometry
921 . cellgeom - The values precomputed from cell geometry
922 - minRadius - The minimum radius over the mesh of an inscribed sphere in a cell
923 
924   Level: developer
925 
926 .seealso: DMPlexTSSetRHSFunctionLocal()
927 @*/
928 PetscErrorCode DMPlexSNESGetGeometryFVM(DM dm, Vec *facegeom, Vec *cellgeom, PetscReal *minRadius)
929 {
930   DM             plex;
931   PetscErrorCode ierr;
932 
933   PetscFunctionBegin;
934   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
935   ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
936   ierr = DMPlexGetDataFVM(plex, NULL, cellgeom, facegeom, NULL);CHKERRQ(ierr);
937   if (minRadius) {ierr = DMPlexGetMinRadius(plex, minRadius);CHKERRQ(ierr);}
938   ierr = DMDestroy(&plex);CHKERRQ(ierr);
939   PetscFunctionReturn(0);
940 }
941 
942 /*@
943   DMPlexSNESGetGradientDM - Return gradient data layout
944 
945   Input Parameters:
946 + dm - The DM
947 - fv - The PetscFV
948 
949   Output Parameter:
950 . dmGrad - The layout for gradient values
951 
952   Level: developer
953 
954 .seealso: DMPlexSNESGetGeometryFVM()
955 @*/
956 PetscErrorCode DMPlexSNESGetGradientDM(DM dm, PetscFV fv, DM *dmGrad)
957 {
958   DM             plex;
959   PetscBool      computeGradients;
960   PetscErrorCode ierr;
961 
962   PetscFunctionBegin;
963   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
964   PetscValidHeaderSpecific(fv,PETSCFV_CLASSID,2);
965   PetscValidPointer(dmGrad,3);
966   ierr = PetscFVGetComputeGradients(fv, &computeGradients);CHKERRQ(ierr);
967   if (!computeGradients) {*dmGrad = NULL; PetscFunctionReturn(0);}
968   ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
969   ierr = DMPlexGetDataFVM(plex, fv, NULL, NULL, dmGrad);CHKERRQ(ierr);
970   ierr = DMDestroy(&plex);CHKERRQ(ierr);
971   PetscFunctionReturn(0);
972 }
973 
974 /*@C
975   DMPlexGetCellFields - Retrieve the field values values for a chunk of cells
976 
977   Input Parameters:
978 + dm     - The DM
979 . cellIS - The cells to include
980 . locX   - A local vector with the solution fields
981 . locX_t - A local vector with solution field time derivatives, or NULL
982 - locA   - A local vector with auxiliary fields, or NULL
983 
984   Output Parameters:
985 + u   - The field coefficients
986 . u_t - The fields derivative coefficients
987 - a   - The auxiliary field coefficients
988 
989   Level: developer
990 
991 .seealso: DMPlexGetFaceFields()
992 @*/
993 PetscErrorCode DMPlexGetCellFields(DM dm, IS cellIS, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
994 {
995   DM              plex, plexA = NULL;
996   PetscSection    section, sectionAux;
997   PetscDS         prob;
998   const PetscInt *cells;
999   PetscInt        cStart, cEnd, numCells, totDim, totDimAux, c;
1000   PetscErrorCode  ierr;
1001 
1002   PetscFunctionBegin;
1003   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1004   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
1005   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
1006   if (locA)   {PetscValidHeaderSpecific(locA, VEC_CLASSID, 6);}
1007   PetscValidPointer(u, 7);
1008   PetscValidPointer(u_t, 8);
1009   PetscValidPointer(a, 9);
1010   ierr = DMSNESConvertPlex(dm, &plex, PETSC_FALSE);CHKERRQ(ierr);
1011   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
1012   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1013   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1014   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
1015   if (locA) {
1016     DM      dmAux;
1017     PetscDS probAux;
1018 
1019     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
1020     ierr = DMSNESConvertPlex(dmAux, &plexA, PETSC_FALSE);CHKERRQ(ierr);
1021     ierr = DMGetSection(dmAux, &sectionAux);CHKERRQ(ierr);
1022     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
1023     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
1024   }
1025   numCells = cEnd - cStart;
1026   ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, u);CHKERRQ(ierr);
1027   if (locX_t) {ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, u_t);CHKERRQ(ierr);} else {*u_t = NULL;}
1028   if (locA)   {ierr = DMGetWorkArray(dm, numCells*totDimAux, MPIU_SCALAR, a);CHKERRQ(ierr);} else {*a = NULL;}
1029   for (c = cStart; c < cEnd; ++c) {
1030     const PetscInt cell = cells ? cells[c] : c;
1031     const PetscInt cind = c - cStart;
1032     PetscScalar   *x = NULL, *x_t = NULL, *ul = *u, *ul_t = *u_t, *al = *a;
1033     PetscInt       i;
1034 
1035     ierr = DMPlexVecGetClosure(plex, section, locX, cell, NULL, &x);CHKERRQ(ierr);
1036     for (i = 0; i < totDim; ++i) ul[cind*totDim+i] = x[i];
1037     ierr = DMPlexVecRestoreClosure(plex, section, locX, cell, NULL, &x);CHKERRQ(ierr);
1038     if (locX_t) {
1039       ierr = DMPlexVecGetClosure(plex, section, locX_t, cell, NULL, &x_t);CHKERRQ(ierr);
1040       for (i = 0; i < totDim; ++i) ul_t[cind*totDim+i] = x_t[i];
1041       ierr = DMPlexVecRestoreClosure(plex, section, locX_t, cell, NULL, &x_t);CHKERRQ(ierr);
1042     }
1043     if (locA) {
1044       ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, cell, NULL, &x);CHKERRQ(ierr);
1045       for (i = 0; i < totDimAux; ++i) al[cind*totDimAux+i] = x[i];
1046       ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, cell, NULL, &x);CHKERRQ(ierr);
1047     }
1048   }
1049   ierr = DMDestroy(&plex);CHKERRQ(ierr);
1050   if (locA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);}
1051   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
1052   PetscFunctionReturn(0);
1053 }
1054 
1055 /*@C
1056   DMPlexRestoreCellFields - Restore the field values values for a chunk of cells
1057 
1058   Input Parameters:
1059 + dm     - The DM
1060 . cellIS - The cells to include
1061 . locX   - A local vector with the solution fields
1062 . locX_t - A local vector with solution field time derivatives, or NULL
1063 - locA   - A local vector with auxiliary fields, or NULL
1064 
1065   Output Parameters:
1066 + u   - The field coefficients
1067 . u_t - The fields derivative coefficients
1068 - a   - The auxiliary field coefficients
1069 
1070   Level: developer
1071 
1072 .seealso: DMPlexGetFaceFields()
1073 @*/
1074 PetscErrorCode DMPlexRestoreCellFields(DM dm, IS cellIS, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
1075 {
1076   PetscErrorCode ierr;
1077 
1078   PetscFunctionBegin;
1079   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, u);CHKERRQ(ierr);
1080   if (locX_t) {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, u_t);CHKERRQ(ierr);}
1081   if (locA)   {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, a);CHKERRQ(ierr);}
1082   PetscFunctionReturn(0);
1083 }
1084 
1085 /*@C
1086   DMPlexGetFaceFields - Retrieve the field values values for a chunk of faces
1087 
1088   Input Parameters:
1089 + dm     - The DM
1090 . fStart - The first face to include
1091 . fEnd   - The first face to exclude
1092 . locX   - A local vector with the solution fields
1093 . locX_t - A local vector with solution field time derivatives, or NULL
1094 . faceGeometry - A local vector with face geometry
1095 . cellGeometry - A local vector with cell geometry
1096 - locaGrad - A local vector with field gradients, or NULL
1097 
1098   Output Parameters:
1099 + Nface - The number of faces with field values
1100 . uL - The field values at the left side of the face
1101 - uR - The field values at the right side of the face
1102 
1103   Level: developer
1104 
1105 .seealso: DMPlexGetCellFields()
1106 @*/
1107 PetscErrorCode DMPlexGetFaceFields(DM dm, PetscInt fStart, PetscInt fEnd, Vec locX, Vec locX_t, Vec faceGeometry, Vec cellGeometry, Vec locGrad, PetscInt *Nface, PetscScalar **uL, PetscScalar **uR)
1108 {
1109   DM                 dmFace, dmCell, dmGrad = NULL;
1110   PetscSection       section;
1111   PetscDS            prob;
1112   DMLabel            ghostLabel;
1113   const PetscScalar *facegeom, *cellgeom, *x, *lgrad;
1114   PetscBool         *isFE;
1115   PetscInt           dim, Nf, f, Nc, numFaces = fEnd - fStart, iface, face;
1116   PetscErrorCode     ierr;
1117 
1118   PetscFunctionBegin;
1119   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1120   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
1121   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
1122   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 6);
1123   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 7);
1124   if (locGrad) {PetscValidHeaderSpecific(locGrad, VEC_CLASSID, 8);}
1125   PetscValidPointer(uL, 9);
1126   PetscValidPointer(uR, 10);
1127   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1128   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1129   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1130   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
1131   ierr = PetscDSGetTotalComponents(prob, &Nc);CHKERRQ(ierr);
1132   ierr = PetscMalloc1(Nf, &isFE);CHKERRQ(ierr);
1133   for (f = 0; f < Nf; ++f) {
1134     PetscObject  obj;
1135     PetscClassId id;
1136 
1137     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1138     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1139     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
1140     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
1141     else                            {isFE[f] = PETSC_FALSE;}
1142   }
1143   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1144   ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr);
1145   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
1146   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
1147   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
1148   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
1149   if (locGrad) {
1150     ierr = VecGetDM(locGrad, &dmGrad);CHKERRQ(ierr);
1151     ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
1152   }
1153   ierr = DMGetWorkArray(dm, numFaces*Nc, MPIU_SCALAR, uL);CHKERRQ(ierr);
1154   ierr = DMGetWorkArray(dm, numFaces*Nc, MPIU_SCALAR, uR);CHKERRQ(ierr);
1155   /* Right now just eat the extra work for FE (could make a cell loop) */
1156   for (face = fStart, iface = 0; face < fEnd; ++face) {
1157     const PetscInt        *cells;
1158     PetscFVFaceGeom       *fg;
1159     PetscFVCellGeom       *cgL, *cgR;
1160     PetscScalar           *xL, *xR, *gL, *gR;
1161     PetscScalar           *uLl = *uL, *uRl = *uR;
1162     PetscInt               ghost, nsupp, nchild;
1163 
1164     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
1165     ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
1166     ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
1167     if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
1168     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
1169     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
1170     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
1171     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
1172     for (f = 0; f < Nf; ++f) {
1173       PetscInt off;
1174 
1175       ierr = PetscDSGetComponentOffset(prob, f, &off);CHKERRQ(ierr);
1176       if (isFE[f]) {
1177         const PetscInt *cone;
1178         PetscInt        comp, coneSizeL, coneSizeR, faceLocL, faceLocR, ldof, rdof, d;
1179 
1180         xL = xR = NULL;
1181         ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr);
1182         ierr = DMPlexVecGetClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr);
1183         ierr = DMPlexVecGetClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr);
1184         ierr = DMPlexGetCone(dm, cells[0], &cone);CHKERRQ(ierr);
1185         ierr = DMPlexGetConeSize(dm, cells[0], &coneSizeL);CHKERRQ(ierr);
1186         for (faceLocL = 0; faceLocL < coneSizeL; ++faceLocL) if (cone[faceLocL] == face) break;
1187         ierr = DMPlexGetCone(dm, cells[1], &cone);CHKERRQ(ierr);
1188         ierr = DMPlexGetConeSize(dm, cells[1], &coneSizeR);CHKERRQ(ierr);
1189         for (faceLocR = 0; faceLocR < coneSizeR; ++faceLocR) if (cone[faceLocR] == face) break;
1190         if (faceLocL == coneSizeL && faceLocR == coneSizeR) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %d in cone of cell %d or cell %d", face, cells[0], cells[1]);
1191         /* Check that FEM field has values in the right cell (sometimes its an FV ghost cell) */
1192         /* TODO: this is a hack that might not be right for nonconforming */
1193         if (faceLocL < coneSizeL) {
1194           ierr = EvaluateFaceFields(prob, f, faceLocL, xL, &uLl[iface*Nc+off]);CHKERRQ(ierr);
1195           if (rdof == ldof && faceLocR < coneSizeR) {ierr = EvaluateFaceFields(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);}
1196           else              {for(d = 0; d < comp; ++d) uRl[iface*Nc+off+d] = uLl[iface*Nc+off+d];}
1197         }
1198         else {
1199           ierr = EvaluateFaceFields(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);
1200           ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr);
1201           for(d = 0; d < comp; ++d) uLl[iface*Nc+off+d] = uRl[iface*Nc+off+d];
1202         }
1203         ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr);
1204         ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr);
1205       } else {
1206         PetscFV  fv;
1207         PetscInt numComp, c;
1208 
1209         ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fv);CHKERRQ(ierr);
1210         ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr);
1211         ierr = DMPlexPointLocalFieldRead(dm, cells[0], f, x, &xL);CHKERRQ(ierr);
1212         ierr = DMPlexPointLocalFieldRead(dm, cells[1], f, x, &xR);CHKERRQ(ierr);
1213         if (dmGrad) {
1214           PetscReal dxL[3], dxR[3];
1215 
1216           ierr = DMPlexPointLocalRead(dmGrad, cells[0], lgrad, &gL);CHKERRQ(ierr);
1217           ierr = DMPlexPointLocalRead(dmGrad, cells[1], lgrad, &gR);CHKERRQ(ierr);
1218           DMPlex_WaxpyD_Internal(dim, -1, cgL->centroid, fg->centroid, dxL);
1219           DMPlex_WaxpyD_Internal(dim, -1, cgR->centroid, fg->centroid, dxR);
1220           for (c = 0; c < numComp; ++c) {
1221             uLl[iface*Nc+off+c] = xL[c] + DMPlex_DotD_Internal(dim, &gL[c*dim], dxL);
1222             uRl[iface*Nc+off+c] = xR[c] + DMPlex_DotD_Internal(dim, &gR[c*dim], dxR);
1223           }
1224         } else {
1225           for (c = 0; c < numComp; ++c) {
1226             uLl[iface*Nc+off+c] = xL[c];
1227             uRl[iface*Nc+off+c] = xR[c];
1228           }
1229         }
1230       }
1231     }
1232     ++iface;
1233   }
1234   *Nface = iface;
1235   ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr);
1236   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
1237   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
1238   if (locGrad) {
1239     ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
1240   }
1241   ierr = PetscFree(isFE);CHKERRQ(ierr);
1242   PetscFunctionReturn(0);
1243 }
1244 
1245 /*@C
1246   DMPlexRestoreFaceFields - Restore the field values values for a chunk of faces
1247 
1248   Input Parameters:
1249 + dm     - The DM
1250 . fStart - The first face to include
1251 . fEnd   - The first face to exclude
1252 . locX   - A local vector with the solution fields
1253 . locX_t - A local vector with solution field time derivatives, or NULL
1254 . faceGeometry - A local vector with face geometry
1255 . cellGeometry - A local vector with cell geometry
1256 - locaGrad - A local vector with field gradients, or NULL
1257 
1258   Output Parameters:
1259 + Nface - The number of faces with field values
1260 . uL - The field values at the left side of the face
1261 - uR - The field values at the right side of the face
1262 
1263   Level: developer
1264 
1265 .seealso: DMPlexGetFaceFields()
1266 @*/
1267 PetscErrorCode DMPlexRestoreFaceFields(DM dm, PetscInt fStart, PetscInt fEnd, Vec locX, Vec locX_t, Vec faceGeometry, Vec cellGeometry, Vec locGrad, PetscInt *Nface, PetscScalar **uL, PetscScalar **uR)
1268 {
1269   PetscErrorCode ierr;
1270 
1271   PetscFunctionBegin;
1272   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, uL);CHKERRQ(ierr);
1273   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, uR);CHKERRQ(ierr);
1274   PetscFunctionReturn(0);
1275 }
1276 
1277 /*@C
1278   DMPlexGetFaceGeometry - Retrieve the geometric values for a chunk of faces
1279 
1280   Input Parameters:
1281 + dm     - The DM
1282 . fStart - The first face to include
1283 . fEnd   - The first face to exclude
1284 . faceGeometry - A local vector with face geometry
1285 - cellGeometry - A local vector with cell geometry
1286 
1287   Output Parameters:
1288 + Nface - The number of faces with field values
1289 . fgeom - The extract the face centroid and normal
1290 - vol   - The cell volume
1291 
1292   Level: developer
1293 
1294 .seealso: DMPlexGetCellFields()
1295 @*/
1296 PetscErrorCode DMPlexGetFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol)
1297 {
1298   DM                 dmFace, dmCell;
1299   DMLabel            ghostLabel;
1300   const PetscScalar *facegeom, *cellgeom;
1301   PetscInt           dim, numFaces = fEnd - fStart, iface, face;
1302   PetscErrorCode     ierr;
1303 
1304   PetscFunctionBegin;
1305   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1306   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 4);
1307   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 5);
1308   PetscValidPointer(fgeom, 6);
1309   PetscValidPointer(vol, 7);
1310   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1311   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1312   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
1313   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
1314   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
1315   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
1316   ierr = PetscMalloc1(numFaces, fgeom);CHKERRQ(ierr);
1317   ierr = DMGetWorkArray(dm, numFaces*2, MPIU_SCALAR, vol);CHKERRQ(ierr);
1318   for (face = fStart, iface = 0; face < fEnd; ++face) {
1319     const PetscInt        *cells;
1320     PetscFVFaceGeom       *fg;
1321     PetscFVCellGeom       *cgL, *cgR;
1322     PetscFVFaceGeom       *fgeoml = *fgeom;
1323     PetscReal             *voll   = *vol;
1324     PetscInt               ghost, d, nchild, nsupp;
1325 
1326     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
1327     ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
1328     ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
1329     if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
1330     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
1331     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
1332     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
1333     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
1334     for (d = 0; d < dim; ++d) {
1335       fgeoml[iface].centroid[d] = fg->centroid[d];
1336       fgeoml[iface].normal[d]   = fg->normal[d];
1337     }
1338     voll[iface*2+0] = cgL->volume;
1339     voll[iface*2+1] = cgR->volume;
1340     ++iface;
1341   }
1342   *Nface = iface;
1343   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
1344   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
1345   PetscFunctionReturn(0);
1346 }
1347 
1348 /*@C
1349   DMPlexRestoreFaceGeometry - Restore the field values values for a chunk of faces
1350 
1351   Input Parameters:
1352 + dm     - The DM
1353 . fStart - The first face to include
1354 . fEnd   - The first face to exclude
1355 . faceGeometry - A local vector with face geometry
1356 - cellGeometry - A local vector with cell geometry
1357 
1358   Output Parameters:
1359 + Nface - The number of faces with field values
1360 . fgeom - The extract the face centroid and normal
1361 - vol   - The cell volume
1362 
1363   Level: developer
1364 
1365 .seealso: DMPlexGetFaceFields()
1366 @*/
1367 PetscErrorCode DMPlexRestoreFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol)
1368 {
1369   PetscErrorCode ierr;
1370 
1371   PetscFunctionBegin;
1372   ierr = PetscFree(*fgeom);CHKERRQ(ierr);
1373   ierr = DMRestoreWorkArray(dm, 0, MPIU_REAL, vol);CHKERRQ(ierr);
1374   PetscFunctionReturn(0);
1375 }
1376 
1377 static PetscErrorCode DMPlexComputeBdResidual_Single_Internal(DM dm, PetscReal t, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt field, Vec locX, Vec locX_t, Vec locF, DMField coordField, IS facetIS)
1378 {
1379   DM_Plex         *mesh = (DM_Plex *) dm->data;
1380   DM               plex = NULL, plexA = NULL;
1381   PetscDS          prob, probAux = NULL;
1382   PetscSection     section, sectionAux = NULL;
1383   Vec              locA = NULL;
1384   PetscScalar     *u = NULL, *u_t = NULL, *a = NULL, *elemVec = NULL;
1385   PetscInt         v;
1386   PetscInt         totDim, totDimAux = 0;
1387   PetscErrorCode   ierr;
1388 
1389   PetscFunctionBegin;
1390   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
1391   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1392   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1393   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
1394   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
1395   if (locA) {
1396     DM dmAux;
1397 
1398     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
1399     ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr);
1400     ierr = DMGetDS(plexA, &probAux);CHKERRQ(ierr);
1401     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
1402     ierr = DMGetSection(plexA, &sectionAux);CHKERRQ(ierr);
1403   }
1404   for (v = 0; v < numValues; ++v) {
1405     PetscFEGeom    *fgeom;
1406     PetscInt        maxDegree;
1407     PetscQuadrature qGeom = NULL;
1408     IS              pointIS;
1409     const PetscInt *points;
1410     PetscInt        numFaces, face, Nq;
1411 
1412     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
1413     if (!pointIS) continue; /* No points with that id on this process */
1414     {
1415       IS isectIS;
1416 
1417       /* TODO: Special cases of ISIntersect where it is quick to check a priori if one is a superset of the other */
1418       ierr = ISIntersect_Caching_Internal(facetIS,pointIS,&isectIS);CHKERRQ(ierr);
1419       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
1420       pointIS = isectIS;
1421     }
1422     ierr = ISGetLocalSize(pointIS,&numFaces);CHKERRQ(ierr);
1423     ierr = ISGetIndices(pointIS,&points);CHKERRQ(ierr);
1424     ierr = PetscMalloc4(numFaces*totDim, &u, locX_t ? numFaces*totDim : 0, &u_t, numFaces*totDim, &elemVec, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr);
1425     ierr = DMFieldGetDegree(coordField,pointIS,NULL,&maxDegree);CHKERRQ(ierr);
1426     if (maxDegree <= 1) {
1427       ierr = DMFieldCreateDefaultQuadrature(coordField,pointIS,&qGeom);CHKERRQ(ierr);
1428     }
1429     if (!qGeom) {
1430       PetscFE fe;
1431 
1432       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr);
1433       ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr);
1434       ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr);
1435     }
1436     ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
1437     ierr = DMSNESGetFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr);
1438     for (face = 0; face < numFaces; ++face) {
1439       const PetscInt point = points[face], *support, *cone;
1440       PetscScalar   *x     = NULL;
1441       PetscInt       i, coneSize, faceLoc;
1442 
1443       ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
1444       ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr);
1445       ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr);
1446       for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break;
1447       if (faceLoc == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %D in cone of support[0] %D", point, support[0]);
1448       fgeom->face[face][0] = faceLoc;
1449       ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
1450       for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i];
1451       ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
1452       if (locX_t) {
1453         ierr = DMPlexVecGetClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
1454         for (i = 0; i < totDim; ++i) u_t[face*totDim+i] = x[i];
1455         ierr = DMPlexVecRestoreClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
1456       }
1457       if (locA) {
1458         DMLabel  spmap;
1459         PetscInt subp = support[0];
1460 
1461         /* If dm is a submesh, do not get subpoint */
1462         ierr = DMPlexGetSubpointMap(dm, &spmap);CHKERRQ(ierr);
1463         if (!spmap) {ierr = DMPlexGetSubpoint(plexA, support[0], &subp);CHKERRQ(ierr);}
1464         ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
1465         for (i = 0; i < totDimAux; ++i) a[face*totDimAux+i] = x[i];
1466         ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
1467       }
1468     }
1469     ierr = PetscMemzero(elemVec, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
1470     {
1471       PetscFE         fe;
1472       PetscInt        Nb;
1473       PetscFEGeom     *chunkGeom = NULL;
1474       /* Conforming batches */
1475       PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
1476       /* Remainder */
1477       PetscInt        Nr, offset;
1478 
1479       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr);
1480       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
1481       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
1482       /* TODO: documentation is unclear about what is going on with these numbers: how should Nb / Nq factor in ? */
1483       blockSize = Nb;
1484       batchSize = numBlocks * blockSize;
1485       ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
1486       numChunks = numFaces / (numBatches*batchSize);
1487       Ne        = numChunks*numBatches*batchSize;
1488       Nr        = numFaces % (numBatches*batchSize);
1489       offset    = numFaces - Nr;
1490       ierr = PetscFEGeomGetChunk(fgeom,0,offset,&chunkGeom);CHKERRQ(ierr);
1491       ierr = PetscFEIntegrateBdResidual(fe, prob, field, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr);
1492       ierr = PetscFEGeomRestoreChunk(fgeom, 0, offset, &chunkGeom);CHKERRQ(ierr);
1493       ierr = PetscFEGeomGetChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr);
1494       ierr = PetscFEIntegrateBdResidual(fe, prob, field, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, a ? &a[offset*totDimAux] : NULL, t, &elemVec[offset*totDim]);CHKERRQ(ierr);
1495       ierr = PetscFEGeomRestoreChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr);
1496     }
1497     for (face = 0; face < numFaces; ++face) {
1498       const PetscInt point = points[face], *support;
1499 
1500       if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDim, &elemVec[face*totDim]);CHKERRQ(ierr);}
1501       ierr = DMPlexGetSupport(plex, point, &support);CHKERRQ(ierr);
1502       ierr = DMPlexVecSetClosure(plex, NULL, locF, support[0], &elemVec[face*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
1503     }
1504     ierr = DMSNESRestoreFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr);
1505     ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
1506     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
1507     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
1508     ierr = PetscFree4(u, u_t, elemVec, a);CHKERRQ(ierr);
1509   }
1510   if (plex)  {ierr = DMDestroy(&plex);CHKERRQ(ierr);}
1511   if (plexA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);}
1512   PetscFunctionReturn(0);
1513 }
1514 
1515 PetscErrorCode DMPlexComputeBdResidualSingle(DM dm, PetscReal t, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt field, Vec locX, Vec locX_t, Vec locF)
1516 {
1517   DMField        coordField;
1518   DMLabel        depthLabel;
1519   IS             facetIS;
1520   PetscInt       dim;
1521   PetscErrorCode ierr;
1522 
1523   PetscFunctionBegin;
1524   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1525   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1526   ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr);
1527   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
1528   ierr = DMPlexComputeBdResidual_Single_Internal(dm, t, label, numValues, values, field, locX, locX_t, locF, coordField, facetIS);CHKERRQ(ierr);
1529   PetscFunctionReturn(0);
1530 }
1531 
1532 PetscErrorCode DMPlexComputeBdResidual_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user)
1533 {
1534   PetscDS        prob;
1535   PetscInt       dim, numBd, bd;
1536   DMLabel        depthLabel;
1537   DMField        coordField = NULL;
1538   IS             facetIS;
1539   PetscErrorCode ierr;
1540 
1541   PetscFunctionBegin;
1542   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1543   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1544   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1545   ierr = DMLabelGetStratumIS(depthLabel,dim - 1,&facetIS);CHKERRQ(ierr);
1546   ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr);
1547   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
1548   for (bd = 0; bd < numBd; ++bd) {
1549     DMBoundaryConditionType type;
1550     const char             *bdLabel;
1551     DMLabel                 label;
1552     const PetscInt         *values;
1553     PetscInt                field, numValues;
1554     PetscObject             obj;
1555     PetscClassId            id;
1556 
1557     ierr = PetscDSGetBoundary(prob, bd, &type, NULL, &bdLabel, &field, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
1558     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
1559     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1560     if ((id != PETSCFE_CLASSID) || (type & DM_BC_ESSENTIAL)) continue;
1561     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
1562     ierr = DMPlexComputeBdResidual_Single_Internal(dm, t, label, numValues, values, field, locX, locX_t, locF, coordField, facetIS);CHKERRQ(ierr);
1563   }
1564   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
1565   PetscFunctionReturn(0);
1566 }
1567 
1568 PetscErrorCode DMPlexComputeResidual_Internal(DM dm, IS cellIS, PetscReal time, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user)
1569 {
1570   DM_Plex         *mesh       = (DM_Plex *) dm->data;
1571   const char      *name       = "Residual";
1572   DM               dmAux      = NULL;
1573   DM               dmGrad     = NULL;
1574   DMLabel          ghostLabel = NULL;
1575   PetscDS          prob       = NULL;
1576   PetscDS          probAux    = NULL;
1577   PetscSection     section    = NULL;
1578   PetscBool        useFEM     = PETSC_FALSE;
1579   PetscBool        useFVM     = PETSC_FALSE;
1580   PetscBool        isImplicit = (locX_t || time == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE;
1581   PetscFV          fvm        = NULL;
1582   PetscFVCellGeom *cgeomFVM   = NULL;
1583   PetscFVFaceGeom *fgeomFVM   = NULL;
1584   DMField          coordField = NULL;
1585   Vec              locA, cellGeometryFVM = NULL, faceGeometryFVM = NULL, grad, locGrad = NULL;
1586   PetscScalar     *u = NULL, *u_t, *a, *uL, *uR;
1587   IS               chunkIS;
1588   const PetscInt  *cells;
1589   PetscInt         cStart, cEnd, numCells;
1590   PetscInt         Nf, f, totDim, totDimAux, numChunks, cellChunkSize, faceChunkSize, chunk, fStart, fEnd;
1591   PetscInt         maxDegree = PETSC_MAX_INT;
1592   PetscQuadrature  affineQuad = NULL, *quads = NULL;
1593   PetscFEGeom     *affineGeom = NULL, **geoms = NULL;
1594   PetscErrorCode   ierr;
1595 
1596   PetscFunctionBegin;
1597   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
1598   /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */
1599   /* TODO The FVM geometry is over-manipulated. Make the precalc functions return exactly what we need */
1600   /* FEM+FVM */
1601   /* 1: Get sizes from dm and dmAux */
1602   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1603   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1604   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1605   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
1606   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
1607   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
1608   if (locA) {
1609     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
1610     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
1611     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
1612   }
1613   /* 2: Get geometric data */
1614   for (f = 0; f < Nf; ++f) {
1615     PetscObject  obj;
1616     PetscClassId id;
1617     PetscBool    fimp;
1618 
1619     ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
1620     if (isImplicit != fimp) continue;
1621     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1622     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1623     if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;}
1624     if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;}
1625   }
1626   if (useFEM) {
1627     ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
1628     ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
1629     if (maxDegree <= 1) {
1630       ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr);
1631       if (affineQuad) {
1632         ierr = DMSNESGetFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
1633       }
1634     } else {
1635       ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr);
1636       for (f = 0; f < Nf; ++f) {
1637         PetscObject  obj;
1638         PetscClassId id;
1639         PetscBool    fimp;
1640 
1641         ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
1642         if (isImplicit != fimp) continue;
1643         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1644         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1645         if (id == PETSCFE_CLASSID) {
1646           PetscFE fe = (PetscFE) obj;
1647 
1648           ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr);
1649           ierr = PetscObjectReference((PetscObject)quads[f]);CHKERRQ(ierr);
1650           ierr = DMSNESGetFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
1651         }
1652       }
1653     }
1654   }
1655   if (useFVM) {
1656     ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr);
1657     ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr);
1658     ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
1659     /* Reconstruct and limit cell gradients */
1660     ierr = DMPlexSNESGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr);
1661     if (dmGrad) {
1662       ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1663       ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
1664       ierr = DMPlexReconstructGradients_Internal(dm, fvm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr);
1665       /* Communicate gradient values */
1666       ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
1667       ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
1668       ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
1669       ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
1670     }
1671     /* Handle non-essential (e.g. outflow) boundary values */
1672     ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, time, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr);
1673   }
1674   /* Loop over chunks */
1675   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
1676   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1677   if (useFEM) {ierr = ISCreate(PETSC_COMM_SELF, &chunkIS);CHKERRQ(ierr);}
1678   numCells      = cEnd - cStart;
1679   numChunks     = 1;
1680   cellChunkSize = numCells/numChunks;
1681   faceChunkSize = (fEnd - fStart)/numChunks;
1682   numChunks     = PetscMin(1,numCells);
1683   for (chunk = 0; chunk < numChunks; ++chunk) {
1684     PetscScalar     *elemVec, *fluxL, *fluxR;
1685     PetscReal       *vol;
1686     PetscFVFaceGeom *fgeom;
1687     PetscInt         cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c;
1688     PetscInt         fS = fStart+chunk*faceChunkSize, fE = PetscMin(fS+faceChunkSize, fEnd), numFaces = 0, face;
1689 
1690     /* Extract field coefficients */
1691     if (useFEM) {
1692       ierr = ISGetPointSubrange(chunkIS, cS, cE, cells);CHKERRQ(ierr);
1693       ierr = DMPlexGetCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
1694       ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
1695       ierr = PetscMemzero(elemVec, numCells*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
1696     }
1697     if (useFVM) {
1698       ierr = DMPlexGetFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);CHKERRQ(ierr);
1699       ierr = DMPlexGetFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);CHKERRQ(ierr);
1700       ierr = DMGetWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxL);CHKERRQ(ierr);
1701       ierr = DMGetWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxR);CHKERRQ(ierr);
1702       ierr = PetscMemzero(fluxL, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
1703       ierr = PetscMemzero(fluxR, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
1704     }
1705     /* TODO We will interlace both our field coefficients (u, u_t, uL, uR, etc.) and our output (elemVec, fL, fR). I think this works */
1706     /* Loop over fields */
1707     for (f = 0; f < Nf; ++f) {
1708       PetscObject  obj;
1709       PetscClassId id;
1710       PetscBool    fimp;
1711       PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
1712 
1713       ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
1714       if (isImplicit != fimp) continue;
1715       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1716       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1717       if (id == PETSCFE_CLASSID) {
1718         PetscFE         fe = (PetscFE) obj;
1719         PetscFEGeom    *geom = affineGeom ? affineGeom : geoms[f];
1720         PetscFEGeom    *chunkGeom = NULL;
1721         PetscQuadrature quad = affineQuad ? affineQuad : quads[f];
1722         PetscInt        Nq, Nb;
1723 
1724         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
1725         ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
1726         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
1727         blockSize = Nb;
1728         batchSize = numBlocks * blockSize;
1729         ierr      = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
1730         numChunks = numCells / (numBatches*batchSize);
1731         Ne        = numChunks*numBatches*batchSize;
1732         Nr        = numCells % (numBatches*batchSize);
1733         offset    = numCells - Nr;
1734         /* Integrate FE residual to get elemVec (need fields at quadrature points) */
1735         /*   For FV, I think we use a P0 basis and the cell coefficients (for subdivided cells, we can tweak the basis tabulation to be the indicator function) */
1736         ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
1737         ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr);
1738         ierr = PetscFEGeomGetChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
1739         ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr);
1740         ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
1741       } else if (id == PETSCFV_CLASSID) {
1742         PetscFV fv = (PetscFV) obj;
1743 
1744         Ne = numFaces;
1745         /* Riemann solve over faces (need fields at face centroids) */
1746         /*   We need to evaluate FE fields at those coordinates */
1747         ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr);
1748       } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
1749     }
1750     /* Loop over domain */
1751     if (useFEM) {
1752       /* Add elemVec to locX */
1753       for (c = cS; c < cE; ++c) {
1754         const PetscInt cell = cells ? cells[c] : c;
1755         const PetscInt cind = c - cStart;
1756 
1757         if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);CHKERRQ(ierr);}
1758         if (ghostLabel) {
1759           PetscInt ghostVal;
1760 
1761           ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr);
1762           if (ghostVal > 0) continue;
1763         }
1764         ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
1765       }
1766     }
1767     if (useFVM) {
1768       PetscScalar *fa;
1769       PetscInt     iface;
1770 
1771       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
1772       for (f = 0; f < Nf; ++f) {
1773         PetscFV      fv;
1774         PetscObject  obj;
1775         PetscClassId id;
1776         PetscInt     foff, pdim;
1777 
1778         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1779         ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr);
1780         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1781         if (id != PETSCFV_CLASSID) continue;
1782         fv   = (PetscFV) obj;
1783         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
1784         /* Accumulate fluxes to cells */
1785         for (face = fS, iface = 0; face < fE; ++face) {
1786           const PetscInt *scells;
1787           PetscScalar    *fL = NULL, *fR = NULL;
1788           PetscInt        ghost, d, nsupp, nchild;
1789 
1790           ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
1791           ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
1792           ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
1793           if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
1794           ierr = DMPlexGetSupport(dm, face, &scells);CHKERRQ(ierr);
1795           ierr = DMLabelGetValue(ghostLabel,scells[0],&ghost);CHKERRQ(ierr);
1796           if (ghost <= 0) {ierr = DMPlexPointLocalFieldRef(dm, scells[0], f, fa, &fL);CHKERRQ(ierr);}
1797           ierr = DMLabelGetValue(ghostLabel,scells[1],&ghost);CHKERRQ(ierr);
1798           if (ghost <= 0) {ierr = DMPlexPointLocalFieldRef(dm, scells[1], f, fa, &fR);CHKERRQ(ierr);}
1799           for (d = 0; d < pdim; ++d) {
1800             if (fL) fL[d] -= fluxL[iface*totDim+foff+d];
1801             if (fR) fR[d] += fluxR[iface*totDim+foff+d];
1802           }
1803           ++iface;
1804         }
1805       }
1806       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
1807     }
1808     /* Handle time derivative */
1809     if (locX_t) {
1810       PetscScalar *x_t, *fa;
1811 
1812       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
1813       ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr);
1814       for (f = 0; f < Nf; ++f) {
1815         PetscFV      fv;
1816         PetscObject  obj;
1817         PetscClassId id;
1818         PetscInt     pdim, d;
1819 
1820         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1821         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1822         if (id != PETSCFV_CLASSID) continue;
1823         fv   = (PetscFV) obj;
1824         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
1825         for (c = cS; c < cE; ++c) {
1826           const PetscInt cell = cells ? cells[c] : c;
1827           PetscScalar   *u_t, *r;
1828 
1829           if (ghostLabel) {
1830             PetscInt ghostVal;
1831 
1832             ierr = DMLabelGetValue(ghostLabel, cell, &ghostVal);CHKERRQ(ierr);
1833             if (ghostVal > 0) continue;
1834           }
1835           ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr);
1836           ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr);
1837           for (d = 0; d < pdim; ++d) r[d] += u_t[d];
1838         }
1839       }
1840       ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr);
1841       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
1842     }
1843     if (useFEM) {
1844       ierr = DMPlexRestoreCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
1845       ierr = DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
1846     }
1847     if (useFVM) {
1848       ierr = DMPlexRestoreFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);CHKERRQ(ierr);
1849       ierr = DMPlexRestoreFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);CHKERRQ(ierr);
1850       ierr = DMRestoreWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxL);CHKERRQ(ierr);
1851       ierr = DMRestoreWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxR);CHKERRQ(ierr);
1852       if (dmGrad) {ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);}
1853     }
1854   }
1855   if (useFEM) {ierr = ISDestroy(&chunkIS);CHKERRQ(ierr);}
1856   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
1857 
1858   if (useFEM) {
1859     ierr = DMPlexComputeBdResidual_Internal(dm, locX, locX_t, t, locF, user);CHKERRQ(ierr);
1860 
1861     if (maxDegree <= 1) {
1862       ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
1863       ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
1864     } else {
1865       for (f = 0; f < Nf; ++f) {
1866         ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
1867         ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr);
1868       }
1869       ierr = PetscFree2(quads,geoms);CHKERRQ(ierr);
1870     }
1871   }
1872 
1873   /* FEM */
1874   /* 1: Get sizes from dm and dmAux */
1875   /* 2: Get geometric data */
1876   /* 3: Handle boundary values */
1877   /* 4: Loop over domain */
1878   /*   Extract coefficients */
1879   /* Loop over fields */
1880   /*   Set tiling for FE*/
1881   /*   Integrate FE residual to get elemVec */
1882   /*     Loop over subdomain */
1883   /*       Loop over quad points */
1884   /*         Transform coords to real space */
1885   /*         Evaluate field and aux fields at point */
1886   /*         Evaluate residual at point */
1887   /*         Transform residual to real space */
1888   /*       Add residual to elemVec */
1889   /* Loop over domain */
1890   /*   Add elemVec to locX */
1891 
1892   /* FVM */
1893   /* Get geometric data */
1894   /* If using gradients */
1895   /*   Compute gradient data */
1896   /*   Loop over domain faces */
1897   /*     Count computational faces */
1898   /*     Reconstruct cell gradient */
1899   /*   Loop over domain cells */
1900   /*     Limit cell gradients */
1901   /* Handle boundary values */
1902   /* Loop over domain faces */
1903   /*   Read out field, centroid, normal, volume for each side of face */
1904   /* Riemann solve over faces */
1905   /* Loop over domain faces */
1906   /*   Accumulate fluxes to cells */
1907   /* TODO Change printFEM to printDisc here */
1908   if (mesh->printFEM) {
1909     Vec         locFbc;
1910     PetscInt    pStart, pEnd, p, maxDof;
1911     PetscScalar *zeroes;
1912 
1913     ierr = VecDuplicate(locF,&locFbc);CHKERRQ(ierr);
1914     ierr = VecCopy(locF,locFbc);CHKERRQ(ierr);
1915     ierr = PetscSectionGetChart(section,&pStart,&pEnd);CHKERRQ(ierr);
1916     ierr = PetscSectionGetMaxDof(section,&maxDof);CHKERRQ(ierr);
1917     ierr = PetscCalloc1(maxDof,&zeroes);CHKERRQ(ierr);
1918     for (p = pStart; p < pEnd; p++) {
1919       ierr = VecSetValuesSection(locFbc,section,p,zeroes,INSERT_BC_VALUES);CHKERRQ(ierr);
1920     }
1921     ierr = PetscFree(zeroes);CHKERRQ(ierr);
1922     ierr = DMPrintLocalVec(dm, name, mesh->printTol, locFbc);CHKERRQ(ierr);
1923     ierr = VecDestroy(&locFbc);CHKERRQ(ierr);
1924   }
1925   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
1926   PetscFunctionReturn(0);
1927 }
1928 
1929 static PetscErrorCode DMPlexComputeResidualFEM_Check_Internal(DM dm, Vec X, Vec X_t, PetscReal t, Vec F, void *user)
1930 {
1931   DM                dmCh, dmAux;
1932   Vec               A;
1933   DMField           coordField = NULL;
1934   PetscDS           prob, probCh, probAux = NULL;
1935   PetscSection      section, sectionAux;
1936   PetscScalar      *elemVec, *elemVecCh, *u, *u_t, *a = NULL;
1937   PetscInt          Nf, f, numCells, cStart, cEnd, c;
1938   PetscInt          totDim, totDimAux = 0, diffCell = 0;
1939   PetscInt          depth;
1940   PetscInt          maxDegree;
1941   IS                cellIS;
1942   DMLabel           depthLabel;
1943   PetscErrorCode    ierr;
1944 
1945   PetscFunctionBegin;
1946   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1947   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1948   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
1949   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
1950   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1951   numCells = cEnd - cStart;
1952   ierr = PetscObjectQuery((PetscObject) dm, "dmCh", (PetscObject *) &dmCh);CHKERRQ(ierr);
1953   ierr = DMGetDS(dmCh, &probCh);CHKERRQ(ierr);
1954   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
1955   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
1956   if (dmAux) {
1957     ierr = DMGetSection(dmAux, &sectionAux);CHKERRQ(ierr);
1958     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
1959     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
1960   }
1961   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
1962   ierr = PetscMalloc3(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim,&elemVec);CHKERRQ(ierr);
1963   ierr = PetscMalloc1(numCells*totDim,&elemVecCh);CHKERRQ(ierr);
1964   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1965   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1966   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
1967   ierr = DMLabelGetStratumIS(depthLabel,depth,&cellIS);CHKERRQ(ierr);
1968   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
1969   for (c = cStart; c < cEnd; ++c) {
1970     PetscScalar *x = NULL, *x_t = NULL;
1971     PetscInt     i;
1972 
1973     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
1974     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
1975     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
1976     if (X_t) {
1977       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
1978       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
1979       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
1980     }
1981     if (dmAux) {
1982       DM dmAuxPlex;
1983 
1984       ierr = DMSNESConvertPlex(dmAux,&dmAuxPlex, PETSC_FALSE);CHKERRQ(ierr);
1985       ierr = DMPlexVecGetClosure(dmAuxPlex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
1986       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
1987       ierr = DMPlexVecRestoreClosure(dmAuxPlex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
1988       ierr = DMDestroy(&dmAuxPlex);CHKERRQ(ierr);
1989     }
1990   }
1991   for (f = 0; f < Nf; ++f) {
1992     PetscFE  fe, feCh;
1993     PetscInt Nq, Nb;
1994     /* Conforming batches */
1995     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
1996     /* Remainder */
1997     PetscInt Nr, offset;
1998     PetscQuadrature qGeom = NULL;
1999     PetscFEGeom *cgeomFEM, *chunkGeom = NULL;
2000 
2001     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
2002     ierr = PetscDSGetDiscretization(probCh, f, (PetscObject *) &feCh);CHKERRQ(ierr);
2003     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
2004     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
2005     ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
2006     if (maxDegree <= 1) {
2007       ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&qGeom);CHKERRQ(ierr);
2008     }
2009     if (!qGeom) {
2010       ierr = PetscFEGetQuadrature(fe, &qGeom);CHKERRQ(ierr);
2011       ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr);
2012     }
2013     ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
2014     ierr = DMSNESGetFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
2015     blockSize = Nb;
2016     batchSize = numBlocks * blockSize;
2017     ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
2018     numChunks = numCells / (numBatches*batchSize);
2019     Ne        = numChunks*numBatches*batchSize;
2020     Nr        = numCells % (numBatches*batchSize);
2021     offset    = numCells - Nr;
2022     ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
2023     ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr);
2024     ierr = PetscFEIntegrateResidual(feCh, prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVecCh);CHKERRQ(ierr);
2025     ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr);
2026     ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr);
2027     ierr = PetscFEIntegrateResidual(feCh, prob, f, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVecCh[offset*totDim]);CHKERRQ(ierr);
2028     ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr);
2029     ierr = DMSNESRestoreFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
2030     ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
2031   }
2032   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
2033   for (c = cStart; c < cEnd; ++c) {
2034     PetscBool diff = PETSC_FALSE;
2035     PetscInt  d;
2036 
2037     for (d = 0; d < totDim; ++d) if (PetscAbsScalar(elemVec[c*totDim+d] - elemVecCh[c*totDim+d]) > 1.0e-7) {diff = PETSC_TRUE;break;}
2038     if (diff) {
2039       ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Different cell %d\n", c);CHKERRQ(ierr);
2040       ierr = DMPrintCellVector(c, "Residual", totDim, &elemVec[c*totDim]);CHKERRQ(ierr);
2041       ierr = DMPrintCellVector(c, "Check Residual", totDim, &elemVecCh[c*totDim]);CHKERRQ(ierr);
2042       ++diffCell;
2043     }
2044     if (diffCell > 9) break;
2045     ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
2046   }
2047   ierr = PetscFree3(u,u_t,elemVec);CHKERRQ(ierr);
2048   ierr = PetscFree(elemVecCh);CHKERRQ(ierr);
2049   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
2050   PetscFunctionReturn(0);
2051 }
2052 
2053 /*@
2054   DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user
2055 
2056   Input Parameters:
2057 + dm - The mesh
2058 . X  - Local solution
2059 - user - The user context
2060 
2061   Output Parameter:
2062 . F  - Local output vector
2063 
2064   Level: developer
2065 
2066 .seealso: DMPlexComputeJacobianAction()
2067 @*/
2068 PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user)
2069 {
2070   PetscObject    check;
2071   DM             plex;
2072   IS             cellIS;
2073   PetscInt       depth;
2074   PetscErrorCode ierr;
2075 
2076   PetscFunctionBegin;
2077   ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
2078   ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
2079   ierr = DMGetStratumIS(plex, "dim", depth, &cellIS);CHKERRQ(ierr);
2080   if (!cellIS) {
2081     ierr = DMGetStratumIS(plex, "depth", depth, &cellIS);CHKERRQ(ierr);
2082   }
2083   /* The dmCh is used to check two mathematically equivalent discretizations for computational equivalence */
2084   ierr = PetscObjectQuery((PetscObject) plex, "dmCh", &check);CHKERRQ(ierr);
2085   if (check) {ierr = DMPlexComputeResidualFEM_Check_Internal(plex, X, NULL, 0.0, F, user);CHKERRQ(ierr);}
2086   else       {ierr = DMPlexComputeResidual_Internal(plex, cellIS, PETSC_MIN_REAL, X, NULL, 0.0, F, user);CHKERRQ(ierr);}
2087   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
2088   ierr = DMDestroy(&plex);CHKERRQ(ierr);
2089   PetscFunctionReturn(0);
2090 }
2091 
2092 /*@
2093   DMPlexSNESComputeBoundaryFEM - Form the boundary values for the local input X
2094 
2095   Input Parameters:
2096 + dm - The mesh
2097 - user - The user context
2098 
2099   Output Parameter:
2100 . X  - Local solution
2101 
2102   Level: developer
2103 
2104 .seealso: DMPlexComputeJacobianAction()
2105 @*/
2106 PetscErrorCode DMPlexSNESComputeBoundaryFEM(DM dm, Vec X, void *user)
2107 {
2108   DM             plex;
2109   PetscErrorCode ierr;
2110 
2111   PetscFunctionBegin;
2112   ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
2113   ierr = DMPlexInsertBoundaryValues(plex, PETSC_TRUE, X, PETSC_MIN_REAL, NULL, NULL, NULL);CHKERRQ(ierr);
2114   ierr = DMDestroy(&plex);CHKERRQ(ierr);
2115   PetscFunctionReturn(0);
2116 }
2117 
2118 PetscErrorCode DMPlexComputeBdJacobian_Single_Internal(DM dm, PetscReal t, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt fieldI, Vec locX, Vec locX_t, PetscReal X_tShift, Mat Jac, Mat JacP, DMField coordField, IS facetIS)
2119 {
2120   DM_Plex       *mesh = (DM_Plex *) dm->data;
2121   DM             plex = NULL, plexA = NULL;
2122   PetscDS        prob, probAux = NULL;
2123   PetscSection   section, sectionAux = NULL;
2124   PetscSection   globalSection, subSection = NULL;
2125   Vec            locA = NULL;
2126   PetscScalar   *u = NULL, *u_t = NULL, *a = NULL, *elemMat = NULL;
2127   PetscInt       v;
2128   PetscInt       Nf, totDim, totDimAux = 0;
2129   PetscBool      isMatISP;
2130   PetscErrorCode ierr;
2131 
2132   PetscFunctionBegin;
2133   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
2134   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
2135   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
2136   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
2137   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
2138   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
2139   if (locA) {
2140     DM dmAux;
2141 
2142     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
2143     ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr);
2144     ierr = DMGetDS(plexA, &probAux);CHKERRQ(ierr);
2145     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
2146     ierr = DMGetSection(plexA, &sectionAux);CHKERRQ(ierr);
2147   }
2148 
2149   ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr);
2150   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
2151   if (isMatISP) {ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);}
2152   for (v = 0; v < numValues; ++v) {
2153     PetscFEGeom    *fgeom;
2154     PetscInt        maxDegree;
2155     PetscQuadrature qGeom = NULL;
2156     IS              pointIS;
2157     const PetscInt *points;
2158     PetscInt        numFaces, face, Nq;
2159 
2160     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
2161     if (!pointIS) continue; /* No points with that id on this process */
2162     {
2163       IS isectIS;
2164 
2165       /* TODO: Special cases of ISIntersect where it is quick to check a prior if one is a superset of the other */
2166       ierr = ISIntersect_Caching_Internal(facetIS,pointIS,&isectIS);CHKERRQ(ierr);
2167       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2168       pointIS = isectIS;
2169     }
2170     ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr);
2171     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
2172     ierr = PetscMalloc4(numFaces*totDim, &u, locX_t ? numFaces*totDim : 0, &u_t, numFaces*totDim*totDim, &elemMat, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr);
2173     ierr = DMFieldGetDegree(coordField,pointIS,NULL,&maxDegree);CHKERRQ(ierr);
2174     if (maxDegree <= 1) {
2175       ierr = DMFieldCreateDefaultQuadrature(coordField,pointIS,&qGeom);CHKERRQ(ierr);
2176     }
2177     if (!qGeom) {
2178       PetscFE fe;
2179 
2180       ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
2181       ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr);
2182       ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr);
2183     }
2184     ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
2185     ierr = DMSNESGetFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr);
2186     for (face = 0; face < numFaces; ++face) {
2187       const PetscInt point = points[face], *support, *cone;
2188       PetscScalar   *x     = NULL;
2189       PetscInt       i, coneSize, faceLoc;
2190 
2191       ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
2192       ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr);
2193       ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr);
2194       for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break;
2195       if (faceLoc == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %d in cone of support[0] %d", point, support[0]);
2196       fgeom->face[face][0] = faceLoc;
2197       ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
2198       for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i];
2199       ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
2200       if (locX_t) {
2201         ierr = DMPlexVecGetClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
2202         for (i = 0; i < totDim; ++i) u_t[face*totDim+i] = x[i];
2203         ierr = DMPlexVecRestoreClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
2204       }
2205       if (locA) {
2206         PetscInt subp;
2207         ierr = DMPlexGetSubpoint(plexA, support[0], &subp);CHKERRQ(ierr);
2208         ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
2209         for (i = 0; i < totDimAux; ++i) a[face*totDimAux+i] = x[i];
2210         ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
2211       }
2212     }
2213     ierr = PetscMemzero(elemMat, numFaces*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
2214     {
2215       PetscFE         fe;
2216       PetscInt        Nb;
2217       /* Conforming batches */
2218       PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
2219       /* Remainder */
2220       PetscFEGeom    *chunkGeom = NULL;
2221       PetscInt        fieldJ, Nr, offset;
2222 
2223       ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
2224       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
2225       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
2226       blockSize = Nb;
2227       batchSize = numBlocks * blockSize;
2228       ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
2229       numChunks = numFaces / (numBatches*batchSize);
2230       Ne        = numChunks*numBatches*batchSize;
2231       Nr        = numFaces % (numBatches*batchSize);
2232       offset    = numFaces - Nr;
2233       ierr = PetscFEGeomGetChunk(fgeom,0,offset,&chunkGeom);CHKERRQ(ierr);
2234       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
2235         ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);
2236       }
2237       ierr = PetscFEGeomGetChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr);
2238       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
2239         ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, a ? &a[offset*totDimAux] : NULL, t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
2240       }
2241       ierr = PetscFEGeomRestoreChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr);
2242     }
2243     for (face = 0; face < numFaces; ++face) {
2244       const PetscInt point = points[face], *support;
2245 
2246       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDim, totDim, &elemMat[face*totDim*totDim]);CHKERRQ(ierr);}
2247       ierr = DMPlexGetSupport(plex, point, &support);CHKERRQ(ierr);
2248       if (!isMatISP) {
2249         ierr = DMPlexMatSetClosure(plex, section, globalSection, JacP, support[0], &elemMat[face*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
2250       } else {
2251         Mat lJ;
2252 
2253         ierr = MatISGetLocalMat(JacP, &lJ);CHKERRQ(ierr);
2254         ierr = DMPlexMatSetClosure(plex, section, subSection, lJ, support[0], &elemMat[face*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
2255       }
2256     }
2257     ierr = DMSNESRestoreFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr);
2258     ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
2259     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
2260     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2261     ierr = PetscFree4(u, u_t, elemMat, a);CHKERRQ(ierr);
2262   }
2263   if (plex)  {ierr = DMDestroy(&plex);CHKERRQ(ierr);}
2264   if (plexA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);}
2265   PetscFunctionReturn(0);
2266 }
2267 
2268 PetscErrorCode DMPlexComputeBdJacobianSingle(DM dm, PetscReal t, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt field, Vec locX, Vec locX_t, PetscReal X_tShift, Mat Jac, Mat JacP)
2269 {
2270   DMField        coordField;
2271   DMLabel        depthLabel;
2272   IS             facetIS;
2273   PetscInt       dim;
2274   PetscErrorCode ierr;
2275 
2276   PetscFunctionBegin;
2277   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2278   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
2279   ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr);
2280   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
2281   ierr = DMPlexComputeBdJacobian_Single_Internal(dm, t, label, numValues, values, field, locX, locX_t, X_tShift, Jac, JacP, coordField, facetIS);CHKERRQ(ierr);
2282   PetscFunctionReturn(0);
2283 }
2284 
2285 PetscErrorCode DMPlexComputeBdJacobian_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, PetscReal X_tShift, Mat Jac, Mat JacP, void *user)
2286 {
2287   PetscDS          prob;
2288   PetscInt         dim, numBd, bd;
2289   DMLabel          depthLabel;
2290   DMField          coordField = NULL;
2291   IS               facetIS;
2292   PetscErrorCode   ierr;
2293 
2294   PetscFunctionBegin;
2295   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
2296   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
2297   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2298   ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr);
2299   ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr);
2300   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
2301   for (bd = 0; bd < numBd; ++bd) {
2302     DMBoundaryConditionType type;
2303     const char             *bdLabel;
2304     DMLabel                 label;
2305     const PetscInt         *values;
2306     PetscInt                fieldI, numValues;
2307     PetscObject             obj;
2308     PetscClassId            id;
2309 
2310     ierr = PetscDSGetBoundary(prob, bd, &type, NULL, &bdLabel, &fieldI, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
2311     ierr = PetscDSGetDiscretization(prob, fieldI, &obj);CHKERRQ(ierr);
2312     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2313     if ((id != PETSCFE_CLASSID) || (type & DM_BC_ESSENTIAL)) continue;
2314     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
2315     ierr = DMPlexComputeBdJacobian_Single_Internal(dm, t, label, numValues, values, fieldI, locX, locX_t, X_tShift, Jac, JacP, coordField, facetIS);CHKERRQ(ierr);
2316   }
2317   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
2318   PetscFunctionReturn(0);
2319 }
2320 
2321 PetscErrorCode DMPlexComputeJacobian_Internal(DM dm, IS cellIS, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user)
2322 {
2323   DM_Plex        *mesh  = (DM_Plex *) dm->data;
2324   const char     *name  = "Jacobian";
2325   DM              dmAux, plex;
2326   Vec             A;
2327   DMField         coordField;
2328   PetscDS         prob, probAux = NULL;
2329   PetscSection    section, globalSection, subSection, sectionAux;
2330   PetscScalar    *elemMat, *elemMatP, *elemMatD, *u, *u_t, *a = NULL;
2331   const PetscInt *cells;
2332   PetscInt        Nf, fieldI, fieldJ;
2333   PetscInt        totDim, totDimAux, cStart, cEnd, numCells, c;
2334   PetscBool       isMatIS, isMatISP, isShell, hasJac, hasPrec, hasDyn, hasFV = PETSC_FALSE;
2335   PetscErrorCode  ierr;
2336 
2337   PetscFunctionBegin;
2338   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
2339   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
2340   ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr);
2341   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
2342   if (isMatISP) {
2343     ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
2344   }
2345   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
2346   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
2347   ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr);
2348   ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr);
2349   ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr);
2350   hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE;
2351   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
2352   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
2353   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
2354   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
2355   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
2356   if (dmAux) {
2357     ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr);
2358     ierr = DMGetSection(plex, &sectionAux);CHKERRQ(ierr);
2359     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
2360     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
2361   }
2362   ierr = PetscMalloc5(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,hasJac ? numCells*totDim*totDim : 0,&elemMat,hasPrec ? numCells*totDim*totDim : 0, &elemMatP,hasDyn ? numCells*totDim*totDim : 0, &elemMatD);CHKERRQ(ierr);
2363   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
2364   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
2365   for (c = cStart; c < cEnd; ++c) {
2366     const PetscInt cell = cells ? cells[c] : c;
2367     const PetscInt cind = c - cStart;
2368     PetscScalar   *x = NULL,  *x_t = NULL;
2369     PetscInt       i;
2370 
2371     ierr = DMPlexVecGetClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
2372     for (i = 0; i < totDim; ++i) u[cind*totDim+i] = x[i];
2373     ierr = DMPlexVecRestoreClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
2374     if (X_t) {
2375       ierr = DMPlexVecGetClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
2376       for (i = 0; i < totDim; ++i) u_t[cind*totDim+i] = x_t[i];
2377       ierr = DMPlexVecRestoreClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
2378     }
2379     if (dmAux) {
2380       ierr = DMPlexVecGetClosure(plex, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr);
2381       for (i = 0; i < totDimAux; ++i) a[cind*totDimAux+i] = x[i];
2382       ierr = DMPlexVecRestoreClosure(plex, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr);
2383     }
2384   }
2385   if (hasJac)  {ierr = PetscMemzero(elemMat,  numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);}
2386   if (hasPrec) {ierr = PetscMemzero(elemMatP, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);}
2387   if (hasDyn)  {ierr = PetscMemzero(elemMatD, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);}
2388   for (fieldI = 0; fieldI < Nf; ++fieldI) {
2389     PetscClassId    id;
2390     PetscFE         fe;
2391     PetscQuadrature qGeom = NULL;
2392     PetscInt        Nb;
2393     /* Conforming batches */
2394     PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
2395     /* Remainder */
2396     PetscInt        Nr, offset, Nq;
2397     PetscInt        maxDegree;
2398     PetscFEGeom     *cgeomFEM, *chunkGeom = NULL, *remGeom = NULL;
2399 
2400     ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
2401     ierr = PetscObjectGetClassId((PetscObject) fe, &id);CHKERRQ(ierr);
2402     if (id == PETSCFV_CLASSID) {hasFV = PETSC_TRUE; continue;}
2403     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
2404     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
2405     ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
2406     if (maxDegree <= 1) {
2407       ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&qGeom);CHKERRQ(ierr);
2408     }
2409     if (!qGeom) {
2410       ierr = PetscFEGetQuadrature(fe,&qGeom);CHKERRQ(ierr);
2411       ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr);
2412     }
2413     ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
2414     ierr = DMSNESGetFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
2415     blockSize = Nb;
2416     batchSize = numBlocks * blockSize;
2417     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
2418     numChunks = numCells / (numBatches*batchSize);
2419     Ne        = numChunks*numBatches*batchSize;
2420     Nr        = numCells % (numBatches*batchSize);
2421     offset    = numCells - Nr;
2422     ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
2423     ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr);
2424     for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
2425       if (hasJac) {
2426         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);
2427         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
2428       }
2429       if (hasPrec) {
2430         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatP);CHKERRQ(ierr);
2431         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatP[offset*totDim*totDim]);CHKERRQ(ierr);
2432       }
2433       if (hasDyn) {
2434         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr);
2435         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatD[offset*totDim*totDim]);CHKERRQ(ierr);
2436       }
2437     }
2438     ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr);
2439     ierr = PetscFEGeomRestoreChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
2440     ierr = DMSNESRestoreFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
2441     ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
2442   }
2443   if (hasDyn) {for (c = 0; c < numCells*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];}
2444   if (hasFV) {
2445     PetscClassId id;
2446     PetscFV      fv;
2447     PetscInt     offsetI, NcI, NbI = 1, fc, f;
2448 
2449     for (fieldI = 0; fieldI < Nf; ++fieldI) {
2450       ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fv);CHKERRQ(ierr);
2451       ierr = PetscDSGetFieldOffset(prob, fieldI, &offsetI);CHKERRQ(ierr);
2452       ierr = PetscObjectGetClassId((PetscObject) fv, &id);CHKERRQ(ierr);
2453       if (id != PETSCFV_CLASSID) continue;
2454       /* Put in the identity */
2455       ierr = PetscFVGetNumComponents(fv, &NcI);CHKERRQ(ierr);
2456       for (c = cStart; c < cEnd; ++c) {
2457         const PetscInt cind    = c - cStart;
2458         const PetscInt eOffset = cind*totDim*totDim;
2459         for (fc = 0; fc < NcI; ++fc) {
2460           for (f = 0; f < NbI; ++f) {
2461             const PetscInt i = offsetI + f*NcI+fc;
2462             if (hasPrec) {
2463               if (hasJac) {elemMat[eOffset+i*totDim+i] = 1.0;}
2464               elemMatP[eOffset+i*totDim+i] = 1.0;
2465             } else {elemMat[eOffset+i*totDim+i] = 1.0;}
2466           }
2467         }
2468       }
2469     }
2470     /* No allocated space for FV stuff, so ignore the zero entries */
2471     ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);CHKERRQ(ierr);
2472   }
2473   isMatIS = PETSC_FALSE;
2474   if (hasPrec && hasJac) {
2475     ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatIS);CHKERRQ(ierr);
2476   }
2477   if (isMatIS && !subSection) {
2478     ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
2479   }
2480   for (c = cStart; c < cEnd; ++c) {
2481     const PetscInt cell = cells ? cells[c] : c;
2482     const PetscInt cind = c - cStart;
2483 
2484     if (hasPrec) {
2485       if (hasJac) {
2486         if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);}
2487         if (!isMatIS) {
2488           ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
2489         } else {
2490           Mat lJ;
2491 
2492           ierr = MatISGetLocalMat(Jac,&lJ);CHKERRQ(ierr);
2493           ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
2494         }
2495       }
2496       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMatP[cind*totDim*totDim]);CHKERRQ(ierr);}
2497       if (!isMatISP) {
2498         ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
2499       } else {
2500         Mat lJ;
2501 
2502         ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr);
2503         ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
2504       }
2505     } else {
2506       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);}
2507       if (!isMatISP) {
2508         ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
2509       } else {
2510         Mat lJ;
2511 
2512         ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr);
2513         ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
2514       }
2515     }
2516   }
2517   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
2518   if (hasFV) {ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);}
2519   ierr = PetscFree5(u,u_t,elemMat,elemMatP,elemMatD);CHKERRQ(ierr);
2520   if (dmAux) {
2521     ierr = PetscFree(a);CHKERRQ(ierr);
2522     ierr = DMDestroy(&plex);CHKERRQ(ierr);
2523   }
2524   ierr = DMPlexComputeBdJacobian_Internal(dm, X, X_t, t, X_tShift, Jac, JacP, user);CHKERRQ(ierr);
2525   if (hasJac && hasPrec) {
2526     ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2527     ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2528   }
2529   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2530   ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2531   if (mesh->printFEM) {
2532     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
2533     ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr);
2534     ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
2535   }
2536   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
2537   ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr);
2538   if (isShell) {
2539     JacActionCtx *jctx;
2540 
2541     ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr);
2542     ierr = VecCopy(X, jctx->u);CHKERRQ(ierr);
2543   }
2544   PetscFunctionReturn(0);
2545 }
2546 
2547 /*@
2548   DMPlexComputeJacobianAction - Form the local portion of the Jacobian action Z = J(X) Y at the local solution X using pointwise functions specified by the user.
2549 
2550   Input Parameters:
2551 + dm - The mesh
2552 . cellIS -
2553 . t  - The time
2554 . X_tShift - The multiplier for the Jacobian with repsect to X_t
2555 . X  - Local solution vector
2556 . X_t  - Time-derivative of the local solution vector
2557 . Y  - Local input vector
2558 - user - The user context
2559 
2560   Output Parameter:
2561 . Z - Local output vector
2562 
2563   Note:
2564   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
2565   like a GPU, or vectorize on a multicore machine.
2566 
2567   Level: developer
2568 
2569 .seealso: FormFunctionLocal()
2570 @*/
2571 PetscErrorCode DMPlexComputeJacobianAction(DM dm, IS cellIS, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Vec Y, Vec Z, void *user)
2572 {
2573   DM_Plex          *mesh  = (DM_Plex *) dm->data;
2574   const char       *name  = "Jacobian";
2575   DM                dmAux, plex, plexAux = NULL;
2576   Vec               A;
2577   PetscDS           prob, probAux = NULL;
2578   PetscQuadrature   quad;
2579   PetscSection      section, globalSection, sectionAux;
2580   PetscScalar      *elemMat, *elemMatD, *u, *u_t, *a = NULL, *y, *z;
2581   PetscInt          Nf, fieldI, fieldJ;
2582   PetscInt          totDim, totDimAux = 0;
2583   const PetscInt   *cells;
2584   PetscInt          cStart, cEnd, numCells, c;
2585   PetscBool         hasDyn;
2586   DMField           coordField;
2587   PetscErrorCode    ierr;
2588 
2589   PetscFunctionBegin;
2590   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
2591   ierr = DMSNESConvertPlex(dm, &plex, PETSC_TRUE);CHKERRQ(ierr);
2592   if (!cellIS) {
2593     PetscInt depth;
2594 
2595     ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
2596     ierr = DMGetStratumIS(plex, "dim", depth, &cellIS);CHKERRQ(ierr);
2597     if (!cellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &cellIS);CHKERRQ(ierr);}
2598   } else {
2599     ierr = PetscObjectReference((PetscObject) cellIS);CHKERRQ(ierr);
2600   }
2601   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
2602   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
2603   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
2604   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
2605   ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr);
2606   hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE;
2607   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
2608   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
2609   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
2610   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
2611   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
2612   if (dmAux) {
2613     ierr = DMConvert(dmAux, DMPLEX, &plexAux);CHKERRQ(ierr);
2614     ierr = DMGetSection(plexAux, &sectionAux);CHKERRQ(ierr);
2615     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
2616     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
2617   }
2618   ierr = VecSet(Z, 0.0);CHKERRQ(ierr);
2619   ierr = PetscMalloc6(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim*totDim,&elemMat,hasDyn ? numCells*totDim*totDim : 0, &elemMatD,numCells*totDim,&y,totDim,&z);CHKERRQ(ierr);
2620   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
2621   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
2622   for (c = cStart; c < cEnd; ++c) {
2623     const PetscInt cell = cells ? cells[c] : c;
2624     const PetscInt cind = c - cStart;
2625     PetscScalar   *x = NULL,  *x_t = NULL;
2626     PetscInt       i;
2627 
2628     ierr = DMPlexVecGetClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
2629     for (i = 0; i < totDim; ++i) u[cind*totDim+i] = x[i];
2630     ierr = DMPlexVecRestoreClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
2631     if (X_t) {
2632       ierr = DMPlexVecGetClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
2633       for (i = 0; i < totDim; ++i) u_t[cind*totDim+i] = x_t[i];
2634       ierr = DMPlexVecRestoreClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
2635     }
2636     if (dmAux) {
2637       ierr = DMPlexVecGetClosure(plexAux, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr);
2638       for (i = 0; i < totDimAux; ++i) a[cind*totDimAux+i] = x[i];
2639       ierr = DMPlexVecRestoreClosure(plexAux, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr);
2640     }
2641     ierr = DMPlexVecGetClosure(dm, section, Y, cell, NULL, &x);CHKERRQ(ierr);
2642     for (i = 0; i < totDim; ++i) y[cind*totDim+i] = x[i];
2643     ierr = DMPlexVecRestoreClosure(dm, section, Y, cell, NULL, &x);CHKERRQ(ierr);
2644   }
2645   ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
2646   if (hasDyn)  {ierr = PetscMemzero(elemMatD, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);}
2647   for (fieldI = 0; fieldI < Nf; ++fieldI) {
2648     PetscFE  fe;
2649     PetscInt Nb;
2650     /* Conforming batches */
2651     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
2652     /* Remainder */
2653     PetscInt Nr, offset, Nq;
2654     PetscQuadrature qGeom = NULL;
2655     PetscInt    maxDegree;
2656     PetscFEGeom *cgeomFEM, *chunkGeom = NULL, *remGeom = NULL;
2657 
2658     ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
2659     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
2660     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
2661     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
2662     ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
2663     if (maxDegree <= 1) {ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&qGeom);CHKERRQ(ierr);}
2664     if (!qGeom) {
2665       ierr = PetscFEGetQuadrature(fe,&qGeom);CHKERRQ(ierr);
2666       ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr);
2667     }
2668     ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
2669     ierr = DMSNESGetFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
2670     blockSize = Nb;
2671     batchSize = numBlocks * blockSize;
2672     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
2673     numChunks = numCells / (numBatches*batchSize);
2674     Ne        = numChunks*numBatches*batchSize;
2675     Nr        = numCells % (numBatches*batchSize);
2676     offset    = numCells - Nr;
2677     ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
2678     ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr);
2679     for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
2680       ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);
2681       ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
2682       if (hasDyn) {
2683         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr);
2684         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatD[offset*totDim*totDim]);CHKERRQ(ierr);
2685       }
2686     }
2687     ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr);
2688     ierr = PetscFEGeomRestoreChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
2689     ierr = DMSNESRestoreFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
2690     ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
2691   }
2692   if (hasDyn) {
2693     for (c = 0; c < numCells*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];
2694   }
2695   for (c = cStart; c < cEnd; ++c) {
2696     const PetscInt     cell = cells ? cells[c] : c;
2697     const PetscInt     cind = c - cStart;
2698     const PetscBLASInt M = totDim, one = 1;
2699     const PetscScalar  a = 1.0, b = 0.0;
2700 
2701     PetscStackCallBLAS("BLASgemv", BLASgemv_("N", &M, &M, &a, &elemMat[cind*totDim*totDim], &M, &y[cind*totDim], &one, &b, z, &one));
2702     if (mesh->printFEM > 1) {
2703       ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);
2704       ierr = DMPrintCellVector(c, "Y",  totDim, &y[cind*totDim]);CHKERRQ(ierr);
2705       ierr = DMPrintCellVector(c, "Z",  totDim, z);CHKERRQ(ierr);
2706     }
2707     ierr = DMPlexVecSetClosure(dm, section, Z, cell, z, ADD_VALUES);CHKERRQ(ierr);
2708   }
2709   ierr = PetscFree6(u,u_t,elemMat,elemMatD,y,z);CHKERRQ(ierr);
2710   if (mesh->printFEM) {
2711     ierr = PetscPrintf(PETSC_COMM_WORLD, "Z:\n");CHKERRQ(ierr);
2712     ierr = VecView(Z, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
2713   }
2714   ierr = PetscFree(a);CHKERRQ(ierr);
2715   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
2716   ierr = DMDestroy(&plexAux);CHKERRQ(ierr);
2717   ierr = DMDestroy(&plex);CHKERRQ(ierr);
2718   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
2719   PetscFunctionReturn(0);
2720 }
2721 
2722 /*@
2723   DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user.
2724 
2725   Input Parameters:
2726 + dm - The mesh
2727 . X  - Local input vector
2728 - user - The user context
2729 
2730   Output Parameter:
2731 . Jac  - Jacobian matrix
2732 
2733   Note:
2734   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
2735   like a GPU, or vectorize on a multicore machine.
2736 
2737   Level: developer
2738 
2739 .seealso: FormFunctionLocal()
2740 @*/
2741 PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user)
2742 {
2743   DM             plex;
2744   PetscDS        prob;
2745   IS             cellIS;
2746   PetscBool      hasJac, hasPrec;
2747   PetscInt       depth;
2748   PetscErrorCode ierr;
2749 
2750   PetscFunctionBegin;
2751   ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
2752   ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
2753   ierr = DMGetStratumIS(plex, "dim", depth, &cellIS);CHKERRQ(ierr);
2754   if (!cellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &cellIS);CHKERRQ(ierr);}
2755   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
2756   ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr);
2757   ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr);
2758   if (hasJac && hasPrec) {ierr = MatZeroEntries(Jac);CHKERRQ(ierr);}
2759   ierr = MatZeroEntries(JacP);CHKERRQ(ierr);
2760   ierr = DMPlexComputeJacobian_Internal(plex, cellIS, 0.0, 0.0, X, NULL, Jac, JacP, user);CHKERRQ(ierr);
2761   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
2762   ierr = DMDestroy(&plex);CHKERRQ(ierr);
2763   PetscFunctionReturn(0);
2764 }
2765 
2766 /*@
2767   DMPlexSetSNESLocalFEM - Use DMPlex's internal FEM routines to compute SNES boundary values, residual, and Jacobian.
2768 
2769   Input Parameters:
2770 + dm - The DM object
2771 . boundaryctx - the user context that will be passed to pointwise evaluation of boundary values (see PetscDSAddBoundary())
2772 . residualctx - the user context that will be passed to pointwise evaluation of finite element residual computations (see PetscDSSetResidual())
2773 - jacobianctx - the user context that will be passed to pointwise evaluation of finite element Jacobian construction (see PetscDSSetJacobian())
2774 
2775   Level: developer
2776 @*/
2777 PetscErrorCode DMPlexSetSNESLocalFEM(DM dm, void *boundaryctx, void *residualctx, void *jacobianctx)
2778 {
2779   PetscErrorCode ierr;
2780 
2781   PetscFunctionBegin;
2782   ierr = DMSNESSetBoundaryLocal(dm,DMPlexSNESComputeBoundaryFEM,boundaryctx);CHKERRQ(ierr);
2783   ierr = DMSNESSetFunctionLocal(dm,DMPlexSNESComputeResidualFEM,residualctx);CHKERRQ(ierr);
2784   ierr = DMSNESSetJacobianLocal(dm,DMPlexSNESComputeJacobianFEM,jacobianctx);CHKERRQ(ierr);
2785   PetscFunctionReturn(0);
2786 }
2787 
2788 PetscErrorCode DMSNESCheckFromOptions_Internal(SNES snes, DM dm, Vec u, PetscErrorCode (**exactFuncs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx), void **ctxs)
2789 {
2790   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
2791   PetscDS        prob;
2792   Mat            J, M;
2793   Vec            r, b;
2794   MatNullSpace   nullSpace;
2795   PetscReal     *error, res = 0.0;
2796   PetscInt       numFields;
2797   PetscBool      hasJac, hasPrec;
2798   PetscInt       Nf, f;
2799   PetscErrorCode ierr;
2800 
2801   PetscFunctionBegin;
2802   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
2803   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
2804   ierr = PetscMalloc1(Nf, &exacts);CHKERRQ(ierr);
2805   for (f = 0; f < Nf; ++f) {ierr = PetscDSGetExactSolution(prob, f, &exacts[f]);CHKERRQ(ierr);}
2806   ierr = VecDuplicate(u, &r);CHKERRQ(ierr);
2807   ierr = DMCreateMatrix(dm, &J);CHKERRQ(ierr);
2808   /* TODO Null space for J */
2809   /* Check discretization error */
2810   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
2811   ierr = PetscMalloc1(PetscMax(1, numFields), &error);CHKERRQ(ierr);
2812   ierr = DMProjectFunction(dm, 0.0, exactFuncs ? exactFuncs : exacts, ctxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
2813   if (numFields > 1) {
2814     PetscInt f;
2815 
2816     ierr = DMComputeL2FieldDiff(dm, 0.0, exactFuncs ? exactFuncs : exacts, ctxs, u, error);CHKERRQ(ierr);
2817     ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: [");CHKERRQ(ierr);
2818     for (f = 0; f < numFields; ++f) {
2819       if (f) {ierr = PetscPrintf(PETSC_COMM_WORLD, ", ");CHKERRQ(ierr);}
2820       if (error[f] >= 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "%g", (double)error[f]);CHKERRQ(ierr);}
2821       else                     {ierr = PetscPrintf(PETSC_COMM_WORLD, "< 1.0e-11");CHKERRQ(ierr);}
2822     }
2823     ierr = PetscPrintf(PETSC_COMM_WORLD, "]\n");CHKERRQ(ierr);
2824   } else {
2825     ierr = DMComputeL2Diff(dm, 0.0, exactFuncs ? exactFuncs : exacts, ctxs, u, &error[0]);CHKERRQ(ierr);
2826     if (error[0] >= 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: %g\n", (double)error[0]);CHKERRQ(ierr);}
2827     else                     {ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: < 1.0e-11\n");CHKERRQ(ierr);}
2828   }
2829   ierr = PetscFree(error);CHKERRQ(ierr);
2830   /* Check residual */
2831   ierr = SNESComputeFunction(snes, u, r);CHKERRQ(ierr);
2832   ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr);
2833   ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Residual: %g\n", (double)res);CHKERRQ(ierr);
2834   ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr);
2835   ierr = PetscObjectSetName((PetscObject) r, "Initial Residual");CHKERRQ(ierr);
2836   ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"res_");CHKERRQ(ierr);
2837   ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr);
2838   /* Check Jacobian */
2839   ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr);
2840   ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr);
2841   if (hasJac && hasPrec) {
2842     ierr = DMCreateMatrix(dm, &M);CHKERRQ(ierr);
2843     ierr = SNESComputeJacobian(snes, u, J, M);CHKERRQ(ierr);
2844     ierr = PetscObjectSetOptionsPrefix((PetscObject) M, "jacpre_");CHKERRQ(ierr);
2845     ierr = MatViewFromOptions(M, NULL, "-mat_view");CHKERRQ(ierr);
2846     ierr = MatDestroy(&M);CHKERRQ(ierr);
2847   } else {
2848     ierr = SNESComputeJacobian(snes, u, J, J);CHKERRQ(ierr);
2849   }
2850   ierr = PetscObjectSetOptionsPrefix((PetscObject) J, "jac_");CHKERRQ(ierr);
2851   ierr = MatViewFromOptions(J, NULL, "-mat_view");CHKERRQ(ierr);
2852   ierr = MatGetNullSpace(J, &nullSpace);CHKERRQ(ierr);
2853   if (nullSpace) {
2854     PetscBool isNull;
2855     ierr = MatNullSpaceTest(nullSpace, J, &isNull);CHKERRQ(ierr);
2856     if (!isNull) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "The null space calculated for the system operator is invalid.");
2857   }
2858   ierr = VecDuplicate(u, &b);CHKERRQ(ierr);
2859   ierr = VecSet(r, 0.0);CHKERRQ(ierr);
2860   ierr = SNESComputeFunction(snes, r, b);CHKERRQ(ierr);
2861   ierr = MatMult(J, u, r);CHKERRQ(ierr);
2862   ierr = VecAXPY(r, 1.0, b);CHKERRQ(ierr);
2863   ierr = VecDestroy(&b);CHKERRQ(ierr);
2864   ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr);
2865   ierr = PetscPrintf(PETSC_COMM_WORLD, "Linear L_2 Residual: %g\n", (double)res);CHKERRQ(ierr);
2866   ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr);
2867   ierr = PetscObjectSetName((PetscObject) r, "Au - b = Au + F(0)");CHKERRQ(ierr);
2868   ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"linear_res_");CHKERRQ(ierr);
2869   ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr);
2870   ierr = VecDestroy(&r);CHKERRQ(ierr);
2871   ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
2872   ierr = MatDestroy(&J);CHKERRQ(ierr);
2873   ierr = PetscFree(exacts);CHKERRQ(ierr);
2874   PetscFunctionReturn(0);
2875 }
2876 
2877 /*@C
2878   DMSNESCheckFromOptions - Check the residual and Jacobian functions using the exact solution by outputting some diagnostic information
2879 
2880   Input Parameters:
2881 + snes - the SNES object
2882 . u    - representative SNES vector
2883 . exactFuncs - pointwise functions of the exact solution for each field
2884 - ctxs - contexts for the functions
2885 
2886   Level: developer
2887 @*/
2888 PetscErrorCode DMSNESCheckFromOptions(SNES snes, Vec u, PetscErrorCode (**exactFuncs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar *u, void *ctx), void **ctxs)
2889 {
2890   PetscErrorCode (**exact)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *) = NULL;
2891   DM             dm;
2892   PetscDS        prob;
2893   Vec            sol;
2894   PetscBool      check;
2895   PetscInt       Nf, f;
2896   PetscErrorCode ierr;
2897 
2898   PetscFunctionBegin;
2899   ierr = PetscOptionsHasName(((PetscObject)snes)->options,((PetscObject)snes)->prefix, "-dmsnes_check", &check);CHKERRQ(ierr);
2900   if (!check) PetscFunctionReturn(0);
2901   ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr);
2902   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
2903   if (!exactFuncs) {
2904     ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
2905     ierr = PetscMalloc1(Nf, &exact);CHKERRQ(ierr);
2906     for (f = 0; f < Nf; ++f) {ierr = PetscDSGetExactSolution(prob, f, &exact[f]);CHKERRQ(ierr);}
2907   }
2908   ierr = VecDuplicate(u, &sol);CHKERRQ(ierr);
2909   ierr = SNESSetSolution(snes, sol);CHKERRQ(ierr);
2910   ierr = DMSNESCheckFromOptions_Internal(snes, dm, sol, exactFuncs ? exactFuncs : exact, ctxs);CHKERRQ(ierr);
2911   ierr = VecDestroy(&sol);CHKERRQ(ierr);
2912   ierr = PetscFree(exact);CHKERRQ(ierr);
2913   PetscFunctionReturn(0);
2914 }
2915