xref: /petsc/src/dm/impls/plex/plexfem.c (revision 2bd8c2f5ae14a5a1b71eb0c5ceb4fa411520e18d)
1 #include <petsc-private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2 
3 #include <petscfe.h>
4 
5 #undef __FUNCT__
6 #define __FUNCT__ "DMPlexGetScale"
7 PetscErrorCode DMPlexGetScale(DM dm, PetscUnit unit, PetscReal *scale)
8 {
9   DM_Plex *mesh = (DM_Plex*) dm->data;
10 
11   PetscFunctionBegin;
12   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13   PetscValidPointer(scale, 3);
14   *scale = mesh->scale[unit];
15   PetscFunctionReturn(0);
16 }
17 
18 #undef __FUNCT__
19 #define __FUNCT__ "DMPlexSetScale"
20 PetscErrorCode DMPlexSetScale(DM dm, PetscUnit unit, PetscReal scale)
21 {
22   DM_Plex *mesh = (DM_Plex*) dm->data;
23 
24   PetscFunctionBegin;
25   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26   mesh->scale[unit] = scale;
27   PetscFunctionReturn(0);
28 }
29 
30 PETSC_STATIC_INLINE PetscInt epsilon(PetscInt i, PetscInt j, PetscInt k)
31 {
32   switch (i) {
33   case 0:
34     switch (j) {
35     case 0: return 0;
36     case 1:
37       switch (k) {
38       case 0: return 0;
39       case 1: return 0;
40       case 2: return 1;
41       }
42     case 2:
43       switch (k) {
44       case 0: return 0;
45       case 1: return -1;
46       case 2: return 0;
47       }
48     }
49   case 1:
50     switch (j) {
51     case 0:
52       switch (k) {
53       case 0: return 0;
54       case 1: return 0;
55       case 2: return -1;
56       }
57     case 1: return 0;
58     case 2:
59       switch (k) {
60       case 0: return 1;
61       case 1: return 0;
62       case 2: return 0;
63       }
64     }
65   case 2:
66     switch (j) {
67     case 0:
68       switch (k) {
69       case 0: return 0;
70       case 1: return 1;
71       case 2: return 0;
72       }
73     case 1:
74       switch (k) {
75       case 0: return -1;
76       case 1: return 0;
77       case 2: return 0;
78       }
79     case 2: return 0;
80     }
81   }
82   return 0;
83 }
84 
85 #undef __FUNCT__
86 #define __FUNCT__ "DMPlexCreateRigidBody"
87 /*@C
88   DMPlexCreateRigidBody - create rigid body modes from coordinates
89 
90   Collective on DM
91 
92   Input Arguments:
93 + dm - the DM
94 . section - the local section associated with the rigid field, or NULL for the default section
95 - globalSection - the global section associated with the rigid field, or NULL for the default section
96 
97   Output Argument:
98 . sp - the null space
99 
100   Note: This is necessary to take account of Dirichlet conditions on the displacements
101 
102   Level: advanced
103 
104 .seealso: MatNullSpaceCreate()
105 @*/
106 PetscErrorCode DMPlexCreateRigidBody(DM dm, PetscSection section, PetscSection globalSection, MatNullSpace *sp)
107 {
108   MPI_Comm       comm;
109   Vec            coordinates, localMode, mode[6];
110   PetscSection   coordSection;
111   PetscScalar   *coords;
112   PetscInt       dim, vStart, vEnd, v, n, m, d, i, j;
113   PetscErrorCode ierr;
114 
115   PetscFunctionBegin;
116   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
117   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
118   if (dim == 1) {
119     ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr);
120     PetscFunctionReturn(0);
121   }
122   if (!section)       {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
123   if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
124   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr);
125   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
126   ierr = DMPlexGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
127   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
128   m    = (dim*(dim+1))/2;
129   ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr);
130   ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr);
131   ierr = VecSetUp(mode[0]);CHKERRQ(ierr);
132   for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);}
133   /* Assume P1 */
134   ierr = DMGetLocalVector(dm, &localMode);CHKERRQ(ierr);
135   for (d = 0; d < dim; ++d) {
136     PetscScalar values[3] = {0.0, 0.0, 0.0};
137 
138     values[d] = 1.0;
139     ierr      = VecSet(localMode, 0.0);CHKERRQ(ierr);
140     for (v = vStart; v < vEnd; ++v) {
141       ierr = DMPlexVecSetClosure(dm, section, localMode, v, values, INSERT_VALUES);CHKERRQ(ierr);
142     }
143     ierr = DMLocalToGlobalBegin(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
144     ierr = DMLocalToGlobalEnd(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
145   }
146   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
147   for (d = dim; d < dim*(dim+1)/2; ++d) {
148     PetscInt i, j, k = dim > 2 ? d - dim : d;
149 
150     ierr = VecSet(localMode, 0.0);CHKERRQ(ierr);
151     for (v = vStart; v < vEnd; ++v) {
152       PetscScalar values[3] = {0.0, 0.0, 0.0};
153       PetscInt    off;
154 
155       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
156       for (i = 0; i < dim; ++i) {
157         for (j = 0; j < dim; ++j) {
158           values[j] += epsilon(i, j, k)*PetscRealPart(coords[off+i]);
159         }
160       }
161       ierr = DMPlexVecSetClosure(dm, section, localMode, v, values, INSERT_VALUES);CHKERRQ(ierr);
162     }
163     ierr = DMLocalToGlobalBegin(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
164     ierr = DMLocalToGlobalEnd(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
165   }
166   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
167   ierr = DMRestoreLocalVector(dm, &localMode);CHKERRQ(ierr);
168   for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);}
169   /* Orthonormalize system */
170   for (i = dim; i < m; ++i) {
171     PetscScalar dots[6];
172 
173     ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr);
174     for (j = 0; j < i; ++j) dots[j] *= -1.0;
175     ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr);
176     ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);
177   }
178   ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr);
179   for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);}
180   PetscFunctionReturn(0);
181 }
182 /*******************************************************************************
183 This should be in a separate Discretization object, but I am not sure how to lay
184 it out yet, so I am stuffing things here while I experiment.
185 *******************************************************************************/
186 #undef __FUNCT__
187 #define __FUNCT__ "DMPlexSetFEMIntegration"
188 PetscErrorCode DMPlexSetFEMIntegration(DM dm,
189                                        PetscErrorCode (*integrateResidualFEM)(PetscInt, PetscInt, PetscFE[], PetscInt, PetscCellGeometry, const PetscScalar[],
190                                                                               void (*)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]),
191                                                                               void (*)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]),
192                                                                               PetscScalar[]),
193                                        PetscErrorCode (*integrateBdResidualFEM)(PetscInt, PetscInt, PetscInt, PetscQuadrature[], const PetscScalar[],
194                                                                                 const PetscReal[], const PetscReal[], const PetscReal[], const PetscReal[], const PetscReal[],
195                                                                                 void (*)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], const PetscReal[], PetscScalar[]),
196                                                                                 void (*)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], const PetscReal[], PetscScalar[]), PetscScalar[]),
197                                        PetscErrorCode (*integrateJacobianActionFEM)(PetscInt, PetscInt, PetscInt, PetscQuadrature[], const PetscScalar[], const PetscScalar[],
198                                                                                     const PetscReal[], const PetscReal[], const PetscReal[], const PetscReal[],
199                                                                                     void (**)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]),
200                                                                                     void (**)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]),
201                                                                                     void (**)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]),
202                                                                                     void (**)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]), PetscScalar[]),
203                                        PetscErrorCode (*integrateJacobianFEM)(PetscInt, PetscInt, PetscFE[], PetscInt, PetscInt, PetscCellGeometry, const PetscScalar[],
204                                                                               void (*)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]),
205                                                                               void (*)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]),
206                                                                               void (*)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]),
207                                                                               void (*)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]),
208                                                                               PetscScalar[]))
209 {
210   DM_Plex *mesh = (DM_Plex*) dm->data;
211 
212   PetscFunctionBegin;
213   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
214   mesh->integrateResidualFEM       = integrateResidualFEM;
215   mesh->integrateBdResidualFEM     = integrateBdResidualFEM;
216   mesh->integrateJacobianActionFEM = integrateJacobianActionFEM;
217   mesh->integrateJacobianFEM       = integrateJacobianFEM;
218   PetscFunctionReturn(0);
219 }
220 
221 #undef __FUNCT__
222 #define __FUNCT__ "DMPlexProjectFunctionLocal"
223 PetscErrorCode DMPlexProjectFunctionLocal(DM dm, PetscInt numComp, void (**funcs)(const PetscReal [], PetscScalar *), InsertMode mode, Vec localX)
224 {
225   Vec            coordinates;
226   PetscSection   section, cSection;
227   PetscInt       dim, vStart, vEnd, v, c, d;
228   PetscScalar   *values, *cArray;
229   PetscReal     *coords;
230   PetscErrorCode ierr;
231 
232   PetscFunctionBegin;
233   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
234   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
235   ierr = DMPlexGetCoordinateSection(dm, &cSection);CHKERRQ(ierr);
236   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
237   ierr = PetscMalloc(numComp * sizeof(PetscScalar), &values);CHKERRQ(ierr);
238   ierr = VecGetArray(coordinates, &cArray);CHKERRQ(ierr);
239   ierr = PetscSectionGetDof(cSection, vStart, &dim);CHKERRQ(ierr);
240   ierr = PetscMalloc(dim * sizeof(PetscReal),&coords);CHKERRQ(ierr);
241   for (v = vStart; v < vEnd; ++v) {
242     PetscInt dof, off;
243 
244     ierr = PetscSectionGetDof(cSection, v, &dof);CHKERRQ(ierr);
245     ierr = PetscSectionGetOffset(cSection, v, &off);CHKERRQ(ierr);
246     if (dof > dim) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Cannot have more coordinates %d then dimensions %d", dof, dim);
247     for (d = 0; d < dof; ++d) coords[d] = PetscRealPart(cArray[off+d]);
248     for (c = 0; c < numComp; ++c) (*funcs[c])(coords, &values[c]);
249     ierr = VecSetValuesSection(localX, section, v, values, mode);CHKERRQ(ierr);
250   }
251   ierr = VecRestoreArray(coordinates, &cArray);CHKERRQ(ierr);
252   /* Temporary, must be replaced by a projection on the finite element basis */
253   {
254     PetscInt eStart = 0, eEnd = 0, e, depth;
255 
256     ierr = DMPlexGetLabelSize(dm, "depth", &depth);CHKERRQ(ierr);
257     --depth;
258     if (depth > 1) {ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);}
259     for (e = eStart; e < eEnd; ++e) {
260       const PetscInt *cone = NULL;
261       PetscInt        coneSize, d;
262       PetscScalar    *coordsA, *coordsB;
263 
264       ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
265       ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
266       if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "Cone size %d for point %d should be 2", coneSize, e);
267       ierr = VecGetValuesSection(coordinates, cSection, cone[0], &coordsA);CHKERRQ(ierr);
268       ierr = VecGetValuesSection(coordinates, cSection, cone[1], &coordsB);CHKERRQ(ierr);
269       for (d = 0; d < dim; ++d) {
270         coords[d] = 0.5*(PetscRealPart(coordsA[d]) + PetscRealPart(coordsB[d]));
271       }
272       for (c = 0; c < numComp; ++c) (*funcs[c])(coords, &values[c]);
273       ierr = VecSetValuesSection(localX, section, e, values, mode);CHKERRQ(ierr);
274     }
275   }
276 
277   ierr = PetscFree(coords);CHKERRQ(ierr);
278   ierr = PetscFree(values);CHKERRQ(ierr);
279 #if 0
280   const PetscInt localDof = this->_mesh->sizeWithBC(s, *cells->begin());
281   PetscReal      detJ;
282 
283   ierr = PetscMalloc(localDof * sizeof(PetscScalar), &values);CHKERRQ(ierr);
284   ierr = PetscMalloc2(dim,PetscReal,&v0,dim*dim,PetscReal,&J);CHKERRQ(ierr);
285   ALE::ISieveVisitor::PointRetriever<PETSC_MESH_TYPE::sieve_type> pV(PetscPowInt(this->_mesh->getSieve()->getMaxConeSize(),dim+1), true);
286 
287   for (PetscInt c = cStart; c < cEnd; ++c) {
288     ALE::ISieveTraversal<PETSC_MESH_TYPE::sieve_type>::orientedClosure(*this->_mesh->getSieve(), c, pV);
289     const PETSC_MESH_TYPE::point_type *oPoints = pV.getPoints();
290     const int                          oSize   = pV.getSize();
291     int                                v       = 0;
292 
293     ierr = DMPlexComputeCellGeometry(dm, c, v0, J, NULL, &detJ);CHKERRQ(ierr);
294     for (PetscInt cl = 0; cl < oSize; ++cl) {
295       const PetscInt fDim;
296 
297       ierr = PetscSectionGetDof(oPoints[cl], &fDim);CHKERRQ(ierr);
298       if (pointDim) {
299         for (PetscInt d = 0; d < fDim; ++d, ++v) {
300           values[v] = (*this->_options.integrate)(v0, J, v, initFunc);
301         }
302       }
303     }
304     ierr = DMPlexVecSetClosure(dm, NULL, localX, c, values);CHKERRQ(ierr);
305     pV.clear();
306   }
307   ierr = PetscFree2(v0,J);CHKERRQ(ierr);
308   ierr = PetscFree(values);CHKERRQ(ierr);
309 #endif
310   PetscFunctionReturn(0);
311 }
312 
313 #undef __FUNCT__
314 #define __FUNCT__ "DMPlexProjectFunction"
315 /*@C
316   DMPlexProjectFunction - This projects the given function into the function space provided.
317 
318   Input Parameters:
319 + dm      - The DM
320 . numComp - The number of components (functions)
321 . funcs   - The coordinate functions to evaluate
322 - mode    - The insertion mode for values
323 
324   Output Parameter:
325 . X - vector
326 
327   Level: developer
328 
329   Note:
330   This currently just calls the function with the coordinates of each vertex and edge midpoint, and stores the result in a vector.
331   We will eventually fix it.
332 
333 .seealso: DMPlexComputeL2Diff()
334 @*/
335 PetscErrorCode DMPlexProjectFunction(DM dm, PetscInt numComp, void (**funcs)(const PetscReal [], PetscScalar *), InsertMode mode, Vec X)
336 {
337   Vec            localX;
338   PetscErrorCode ierr;
339 
340   PetscFunctionBegin;
341   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
342   ierr = DMPlexProjectFunctionLocal(dm, numComp, funcs, mode, localX);CHKERRQ(ierr);
343   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
344   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
345   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
346   PetscFunctionReturn(0);
347 }
348 
349 #undef __FUNCT__
350 #define __FUNCT__ "DMPlexComputeL2Diff"
351 /*@C
352   DMPlexComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
353 
354   Input Parameters:
355 + dm    - The DM
356 . quad  - The PetscQuadrature object for each field
357 . funcs - The functions to evaluate for each field component
358 - X     - The coefficient vector u_h
359 
360   Output Parameter:
361 . diff - The diff ||u - u_h||_2
362 
363   Level: developer
364 
365 .seealso: DMPlexProjectFunction()
366 @*/
367 PetscErrorCode DMPlexComputeL2Diff(DM dm, PetscQuadrature quad[], void (**funcs)(const PetscReal [], PetscScalar *), Vec X, PetscReal *diff)
368 {
369   const PetscInt debug = 0;
370   PetscSection   section;
371   Vec            localX;
372   PetscReal     *coords, *v0, *J, *invJ, detJ;
373   PetscReal      localDiff = 0.0;
374   PetscInt       dim, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset, comp;
375   PetscErrorCode ierr;
376 
377   PetscFunctionBegin;
378   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
379   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
380   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
381   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
382   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
383   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
384   for (field = 0; field < numFields; ++field) {
385     numComponents += quad[field].numComponents;
386   }
387   ierr = DMPlexProjectFunctionLocal(dm, numComponents, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
388   ierr = PetscMalloc4(dim,PetscReal,&coords,dim,PetscReal,&v0,dim*dim,PetscReal,&J,dim*dim,PetscReal,&invJ);CHKERRQ(ierr);
389   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
390   for (c = cStart; c < cEnd; ++c) {
391     PetscScalar *x;
392     PetscReal    elemDiff = 0.0;
393 
394     ierr = DMPlexComputeCellGeometry(dm, c, v0, J, invJ, &detJ);CHKERRQ(ierr);
395     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
396     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
397 
398     for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) {
399       const PetscInt   numQuadPoints = quad[field].numQuadPoints;
400       const PetscReal *quadPoints    = quad[field].quadPoints;
401       const PetscReal *quadWeights   = quad[field].quadWeights;
402       const PetscInt   numBasisFuncs = quad[field].numBasisFuncs;
403       const PetscInt   numBasisComps = quad[field].numComponents;
404       const PetscReal *basis         = quad[field].basis;
405       PetscInt         q, d, e, fc, f;
406 
407       if (debug) {
408         char title[1024];
409         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
410         ierr = DMPrintCellVector(c, title, numBasisFuncs*numBasisComps, &x[fieldOffset]);CHKERRQ(ierr);
411       }
412       for (q = 0; q < numQuadPoints; ++q) {
413         for (d = 0; d < dim; d++) {
414           coords[d] = v0[d];
415           for (e = 0; e < dim; e++) {
416             coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0);
417           }
418         }
419         for (fc = 0; fc < numBasisComps; ++fc) {
420           PetscScalar funcVal;
421           PetscScalar interpolant = 0.0;
422 
423           (*funcs[comp+fc])(coords, &funcVal);
424           for (f = 0; f < numBasisFuncs; ++f) {
425             const PetscInt fidx = f*numBasisComps+fc;
426             interpolant += x[fieldOffset+fidx]*basis[q*numBasisFuncs*numBasisComps+fidx];
427           }
428           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %d field %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant - funcVal))*quadWeights[q]*detJ);CHKERRQ(ierr);}
429           elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal))*quadWeights[q]*detJ;
430         }
431       }
432       comp        += numBasisComps;
433       fieldOffset += numBasisFuncs*numBasisComps;
434     }
435     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
436     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
437     localDiff += elemDiff;
438   }
439   ierr  = PetscFree4(coords,v0,J,invJ);CHKERRQ(ierr);
440   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
441   ierr  = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
442   *diff = PetscSqrtReal(*diff);
443   PetscFunctionReturn(0);
444 }
445 
446 #if 0
447 
448 #undef __FUNCT__
449 #define __FUNCT__ "DMPlexComputeResidualFEM"
450 PetscErrorCode DMPlexComputeResidualFEM(DM dm, Vec X, Vec F, void *user)
451 {
452   DM_Plex         *mesh   = (DM_Plex*) dm->data;
453   PetscFEM        *fem    = (PetscFEM*) &((DM*) user)[1];
454   PetscQuadrature *quad   = fem->quad;
455   PetscQuadrature *quadBd = fem->quadBd;
456   PetscSection     section;
457   PetscReal       *v0, *n, *J, *invJ, *detJ;
458   PetscScalar     *elemVec, *u;
459   PetscInt         dim, numFields, field, numBatchesTmp = 1, numCells, cStart, cEnd, c;
460   PetscInt         cellDof, numComponents;
461   PetscBool        has;
462   PetscErrorCode   ierr;
463 
464   PetscFunctionBegin;
465   if (has && quadBd) {
466     DMLabel         label;
467     IS              pointIS;
468     const PetscInt *points;
469     PetscInt        numPoints, p;
470 
471     ierr = DMPlexGetLabel(dm, "boundary", &label);CHKERRQ(ierr);
472     ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr);
473     ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
474     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
475     for (field = 0, cellDof = 0, numComponents = 0; field < numFields; ++field) {
476       cellDof       += quadBd[field].numBasisFuncs*quadBd[field].numComponents;
477       numComponents += quadBd[field].numComponents;
478     }
479     ierr = PetscMalloc7(numPoints*cellDof,PetscScalar,&u,numPoints*dim,PetscReal,&v0,numPoints*dim,PetscReal,&n,numPoints*dim*dim,PetscReal,&J,numPoints*dim*dim,PetscReal,&invJ,numPoints,PetscReal,&detJ,numPoints*cellDof,PetscScalar,&elemVec);CHKERRQ(ierr);
480     for (p = 0; p < numPoints; ++p) {
481       const PetscInt point = points[p];
482       PetscScalar   *x;
483       PetscInt       i;
484 
485       /* TODO: Add normal determination here */
486       ierr = DMPlexComputeCellGeometry(dm, point, &v0[p*dim], &J[p*dim*dim], &invJ[p*dim*dim], &detJ[p]);CHKERRQ(ierr);
487       if (detJ[p] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", detJ[p], point);
488       ierr = DMPlexVecGetClosure(dm, NULL, X, point, NULL, &x);CHKERRQ(ierr);
489 
490       for (i = 0; i < cellDof; ++i) u[p*cellDof+i] = x[i];
491       ierr = DMPlexVecRestoreClosure(dm, NULL, X, point, NULL, &x);CHKERRQ(ierr);
492     }
493     for (field = 0; field < numFields; ++field) {
494       const PetscInt numQuadPoints = quadBd[field].numQuadPoints;
495       const PetscInt numBasisFuncs = quadBd[field].numBasisFuncs;
496       void           (*f0)(const PetscScalar[], const PetscScalar[], const PetscReal[], const PetscReal[], PetscScalar[]) = fem->f0BdFuncs[field];
497       void           (*f1)(const PetscScalar[], const PetscScalar[], const PetscReal[], const PetscReal[], PetscScalar[]) = fem->f1BdFuncs[field];
498       /* Conforming batches */
499       PetscInt blockSize  = numBasisFuncs*numQuadPoints;
500       PetscInt numBlocks  = 1;
501       PetscInt batchSize  = numBlocks * blockSize;
502       PetscInt numBatches = numBatchesTmp;
503       PetscInt numChunks  = numPoints / (numBatches*batchSize);
504       /* Remainder */
505       PetscInt numRemainder = numPoints % (numBatches * batchSize);
506       PetscInt offset       = numPoints - numRemainder;
507 
508       ierr = (*mesh->integrateBdResidualFEM)(numChunks*numBatches*batchSize, numFields, field, quadBd, u, v0, n, J, invJ, detJ, f0, f1, elemVec);CHKERRQ(ierr);
509       ierr = (*mesh->integrateBdResidualFEM)(numRemainder, numFields, field, quadBd, &u[offset*cellDof], &v0[offset*dim], &n[offset*dim], &J[offset*dim*dim], &invJ[offset*dim*dim], &detJ[offset],
510                                              f0, f1, &elemVec[offset*cellDof]);CHKERRQ(ierr);
511     }
512     for (p = 0; p < numPoints; ++p) {
513       const PetscInt point = points[p];
514 
515       if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "Residual", cellDof, &elemVec[p*cellDof]);CHKERRQ(ierr);}
516       ierr = DMPlexVecSetClosure(dm, NULL, F, point, &elemVec[p*cellDof], ADD_VALUES);CHKERRQ(ierr);
517     }
518     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
519     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
520     ierr = PetscFree7(u,v0,n,J,invJ,detJ,elemVec);CHKERRQ(ierr);
521   }
522   PetscFunctionReturn(0);
523 }
524 
525 #else
526 
527 #undef __FUNCT__
528 #define __FUNCT__ "DMPlexComputeResidualFEM"
529 /*@
530   DMPlexComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user
531 
532   Input Parameters:
533 + dm - The mesh
534 . X  - Local input vector
535 - user - The user context
536 
537   Output Parameter:
538 . F  - Local output vector
539 
540   Note:
541   The second member of the user context must be an FEMContext.
542 
543   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
544   like a GPU, or vectorize on a multicore machine.
545 
546   Level: developer
547 
548 .seealso: DMPlexComputeJacobianActionFEM()
549 @*/
550 PetscErrorCode DMPlexComputeResidualFEM(DM dm, Vec X, Vec F, void *user)
551 {
552   DM_Plex          *mesh = (DM_Plex*) dm->data;
553   PetscFEM         *fem  = (PetscFEM*) &((DM*) user)[1];
554   PetscFE          *fe   = fem->fe;
555   const char       *name = "Residual";
556   PetscQuadrature   q;
557   PetscCellGeometry geom;
558   PetscSection      section;
559   PetscReal        *v0, *J, *invJ, *detJ;
560   PetscScalar      *elemVec, *u;
561   PetscInt          dim, numFields, f, numCells, cStart, cEnd, c;
562   PetscInt          cellDof = 0, numComponents = 0;
563   PetscErrorCode    ierr;
564 
565   PetscFunctionBegin;
566   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
567   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
568   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
569   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
570   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
571   numCells = cEnd - cStart;
572   for (f = 0; f < numFields; ++f) {
573     PetscInt Nb, Nc;
574 
575     ierr = PetscFEGetDimension(fe[f], &Nb);CHKERRQ(ierr);
576     ierr = PetscFEGetNumComponents(fe[f], &Nc);CHKERRQ(ierr);
577     cellDof       += Nb*Nc;
578     numComponents += Nc;
579   }
580   ierr = DMPlexProjectFunctionLocal(dm, numComponents, fem->bcFuncs, INSERT_BC_VALUES, X);CHKERRQ(ierr);
581   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
582   ierr = PetscMalloc6(numCells*cellDof,PetscScalar,&u,numCells*dim,PetscReal,&v0,numCells*dim*dim,PetscReal,&J,numCells*dim*dim,PetscReal,&invJ,numCells,PetscReal,&detJ,numCells*cellDof,PetscScalar,&elemVec);CHKERRQ(ierr);
583   for (c = cStart; c < cEnd; ++c) {
584     PetscScalar *x = NULL;
585     PetscInt     i;
586 
587     ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
588     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
589     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
590     for (i = 0; i < cellDof; ++i) u[c*cellDof+i] = x[i];
591     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
592   }
593   for (f = 0; f < numFields; ++f) {
594     void   (*f0)(const PetscScalar[], const PetscScalar[], const PetscReal[], const PetscScalar[], const PetscScalar[], PetscScalar[]) = fem->f0Funcs[f];
595     void   (*f1)(const PetscScalar[], const PetscScalar[], const PetscReal[], const PetscScalar[], const PetscScalar[], PetscScalar[]) = fem->f1Funcs[f];
596     PetscInt Nb;
597     /* Conforming batches */
598     PetscInt numBlocks  = 1;
599     PetscInt numBatches = 1;
600     PetscInt numChunks, Ne, blockSize, batchSize;
601     /* Remainder */
602     PetscInt Nr, offset;
603 
604     ierr = PetscFEGetQuadrature(fe[f], &q);CHKERRQ(ierr);
605     ierr = PetscFEGetDimension(fe[f], &Nb);CHKERRQ(ierr);
606     blockSize = Nb*q.numQuadPoints;
607     batchSize = numBlocks * blockSize;
608     numChunks = numCells / (numBatches*batchSize);
609     Ne        = numChunks*numBatches*batchSize;
610     Nr        = numCells % (numBatches*batchSize);
611     offset    = numCells - Nr;
612     geom.v0   = v0;
613     geom.J    = J;
614     geom.invJ = invJ;
615     geom.detJ = detJ;
616     ierr = (*mesh->integrateResidualFEM)(Ne, numFields, fe, f, geom, u, f0, f1, elemVec);CHKERRQ(ierr);
617     geom.v0   = &v0[offset*dim];
618     geom.J    = &J[offset*dim*dim];
619     geom.invJ = &invJ[offset*dim*dim];
620     geom.detJ = &detJ[offset];
621     ierr = (*mesh->integrateResidualFEM)(Nr, numFields, fe, f, geom, &u[offset*cellDof], f0, f1, &elemVec[offset*cellDof]);CHKERRQ(ierr);
622   }
623   for (c = cStart; c < cEnd; ++c) {
624     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(c, name, cellDof, &elemVec[c*cellDof]);CHKERRQ(ierr);}
625     ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*cellDof], ADD_VALUES);CHKERRQ(ierr);
626   }
627   ierr = PetscFree6(u,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr);
628   if (mesh->printFEM) {ierr = DMPrintLocalVec(dm, name, F);CHKERRQ(ierr);}
629   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
630   PetscFunctionReturn(0);
631 }
632 
633 #endif
634 
635 #undef __FUNCT__
636 #define __FUNCT__ "DMPlexComputeJacobianActionFEM"
637 /*@C
638   DMPlexComputeJacobianActionFEM - Form the local action of Jacobian J(u) on the local input X using pointwise functions specified by the user
639 
640   Input Parameters:
641 + dm - The mesh
642 . J  - The Jacobian shell matrix
643 . X  - Local input vector
644 - user - The user context
645 
646   Output Parameter:
647 . F  - Local output vector
648 
649   Note:
650   The second member of the user context must be an FEMContext.
651 
652   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
653   like a GPU, or vectorize on a multicore machine.
654 
655   Level: developer
656 
657 .seealso: DMPlexComputeResidualFEM()
658 @*/
659 PetscErrorCode DMPlexComputeJacobianActionFEM(DM dm, Mat Jac, Vec X, Vec F, void *user)
660 {
661   DM_Plex         *mesh = (DM_Plex*) dm->data;
662   PetscFEM        *fem  = (PetscFEM*) &((DM*) user)[1];
663   PetscQuadrature *quad = fem->quad;
664   PetscSection     section;
665   JacActionCtx    *jctx;
666   PetscReal       *v0, *J, *invJ, *detJ;
667   PetscScalar     *elemVec, *u, *a;
668   PetscInt         dim, numFields, field, numBatchesTmp = 1, numCells, cStart, cEnd, c;
669   PetscInt         cellDof = 0;
670   PetscErrorCode   ierr;
671 
672   PetscFunctionBegin;
673   /* ierr = PetscLogEventBegin(JacobianActionFEMEvent,0,0,0,0);CHKERRQ(ierr); */
674   ierr     = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr);
675   ierr     = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
676   ierr     = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
677   ierr     = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
678   ierr     = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
679   numCells = cEnd - cStart;
680   for (field = 0; field < numFields; ++field) {
681     cellDof += quad[field].numBasisFuncs*quad[field].numComponents;
682   }
683   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
684   ierr = PetscMalloc7(numCells*cellDof,PetscScalar,&u,numCells*cellDof,PetscScalar,&a,numCells*dim,PetscReal,&v0,numCells*dim*dim,PetscReal,&J,numCells*dim*dim,PetscReal,&invJ,numCells,PetscReal,&detJ,numCells*cellDof,PetscScalar,&elemVec);CHKERRQ(ierr);
685   for (c = cStart; c < cEnd; ++c) {
686     PetscScalar *x;
687     PetscInt     i;
688 
689     ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
690     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
691     ierr = DMPlexVecGetClosure(dm, NULL, jctx->u, c, NULL, &x);CHKERRQ(ierr);
692     for (i = 0; i < cellDof; ++i) u[c*cellDof+i] = x[i];
693     ierr = DMPlexVecRestoreClosure(dm, NULL, jctx->u, c, NULL, &x);CHKERRQ(ierr);
694     ierr = DMPlexVecGetClosure(dm, NULL, X, c, NULL, &x);CHKERRQ(ierr);
695     for (i = 0; i < cellDof; ++i) a[c*cellDof+i] = x[i];
696     ierr = DMPlexVecRestoreClosure(dm, NULL, X, c, NULL, &x);CHKERRQ(ierr);
697   }
698   for (field = 0; field < numFields; ++field) {
699     const PetscInt numQuadPoints = quad[field].numQuadPoints;
700     const PetscInt numBasisFuncs = quad[field].numBasisFuncs;
701     /* Conforming batches */
702     PetscInt blockSize  = numBasisFuncs*numQuadPoints;
703     PetscInt numBlocks  = 1;
704     PetscInt batchSize  = numBlocks * blockSize;
705     PetscInt numBatches = numBatchesTmp;
706     PetscInt numChunks  = numCells / (numBatches*batchSize);
707     /* Remainder */
708     PetscInt numRemainder = numCells % (numBatches * batchSize);
709     PetscInt offset       = numCells - numRemainder;
710 
711     ierr = (*mesh->integrateJacobianActionFEM)(numChunks*numBatches*batchSize, numFields, field, quad, u, a, v0, J, invJ, detJ, fem->g0Funcs, fem->g1Funcs, fem->g2Funcs, fem->g3Funcs, elemVec);CHKERRQ(ierr);
712     ierr = (*mesh->integrateJacobianActionFEM)(numRemainder, numFields, field, quad, &u[offset*cellDof], &a[offset*cellDof], &v0[offset*dim], &J[offset*dim*dim], &invJ[offset*dim*dim], &detJ[offset],
713                                                fem->g0Funcs, fem->g1Funcs, fem->g2Funcs, fem->g3Funcs, &elemVec[offset*cellDof]);CHKERRQ(ierr);
714   }
715   for (c = cStart; c < cEnd; ++c) {
716     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(c, "Jacobian Action", cellDof, &elemVec[c*cellDof]);CHKERRQ(ierr);}
717     ierr = DMPlexVecSetClosure(dm, NULL, F, c, &elemVec[c*cellDof], ADD_VALUES);CHKERRQ(ierr);
718   }
719   ierr = PetscFree7(u,a,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr);
720   if (mesh->printFEM) {
721     PetscMPIInt rank, numProcs;
722     PetscInt    p;
723 
724     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
725     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &numProcs);CHKERRQ(ierr);
726     ierr = PetscPrintf(PetscObjectComm((PetscObject)dm), "Jacobian Action:\n");CHKERRQ(ierr);
727     for (p = 0; p < numProcs; ++p) {
728       if (p == rank) {ierr = VecView(F, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);}
729       ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr);
730     }
731   }
732   /* ierr = PetscLogEventEnd(JacobianActionFEMEvent,0,0,0,0);CHKERRQ(ierr); */
733   PetscFunctionReturn(0);
734 }
735 
736 #undef __FUNCT__
737 #define __FUNCT__ "DMPlexComputeJacobianFEM"
738 /*@
739   DMPlexComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user.
740 
741   Input Parameters:
742 + dm - The mesh
743 . X  - Local input vector
744 - user - The user context
745 
746   Output Parameter:
747 . Jac  - Jacobian matrix
748 
749   Note:
750   The second member of the user context must be an FEMContext.
751 
752   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
753   like a GPU, or vectorize on a multicore machine.
754 
755   Level: developer
756 
757 .seealso: FormFunctionLocal()
758 @*/
759 PetscErrorCode DMPlexComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP, MatStructure *str,void *user)
760 {
761   DM_Plex          *mesh = (DM_Plex*) dm->data;
762   PetscFEM         *fem  = (PetscFEM*) &((DM*) user)[1];
763   PetscFE          *fe   = fem->fe;
764   const char       *name = "Jacobian";
765   PetscQuadrature   quad;
766   PetscCellGeometry geom;
767   PetscSection      section, globalSection;
768   PetscReal        *v0, *J, *invJ, *detJ;
769   PetscScalar      *elemMat, *u;
770   PetscInt          dim, numFields, f, fieldI, fieldJ, numCells, cStart, cEnd, c;
771   PetscInt          cellDof = 0, numComponents = 0;
772   PetscBool         isShell;
773   PetscErrorCode    ierr;
774 
775   PetscFunctionBegin;
776   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
777   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
778   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
779   ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);
780   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
781   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
782   numCells = cEnd - cStart;
783   for (f = 0; f < numFields; ++f) {
784     PetscInt Nb, Nc;
785 
786     ierr = PetscFEGetDimension(fe[f], &Nb);CHKERRQ(ierr);
787     ierr = PetscFEGetNumComponents(fe[f], &Nc);CHKERRQ(ierr);
788     cellDof       += Nb*Nc;
789     numComponents += Nc;
790   }
791   ierr = DMPlexProjectFunctionLocal(dm, numComponents, fem->bcFuncs, INSERT_BC_VALUES, X);CHKERRQ(ierr);
792   ierr = MatZeroEntries(JacP);CHKERRQ(ierr);
793   ierr = PetscMalloc6(numCells*cellDof,PetscScalar,&u,numCells*dim,PetscReal,&v0,numCells*dim*dim,PetscReal,&J,numCells*dim*dim,PetscReal,&invJ,numCells,PetscReal,&detJ,numCells*cellDof*cellDof,PetscScalar,&elemMat);CHKERRQ(ierr);
794   for (c = cStart; c < cEnd; ++c) {
795     PetscScalar *x;
796     PetscInt     i;
797 
798     ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
799     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
800     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
801     for (i = 0; i < cellDof; ++i) u[c*cellDof+i] = x[i];
802     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
803   }
804   ierr = PetscMemzero(elemMat, numCells*cellDof*cellDof * sizeof(PetscScalar));CHKERRQ(ierr);
805   for (fieldI = 0; fieldI < numFields; ++fieldI) {
806     PetscInt Nb;
807     ierr = PetscFEGetQuadrature(fe[fieldI], &quad);CHKERRQ(ierr);
808     ierr = PetscFEGetDimension(fe[fieldI], &Nb);CHKERRQ(ierr);
809     for (fieldJ = 0; fieldJ < numFields; ++fieldJ) {
810       void   (*g0)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]) = fem->g0Funcs[fieldI*numFields+fieldJ];
811       void   (*g1)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]) = fem->g1Funcs[fieldI*numFields+fieldJ];
812       void   (*g2)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]) = fem->g2Funcs[fieldI*numFields+fieldJ];
813       void   (*g3)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]) = fem->g3Funcs[fieldI*numFields+fieldJ];
814       /* Conforming batches */
815       PetscInt numBlocks  = 1;
816       PetscInt numBatches = 1;
817       PetscInt numChunks, Ne, blockSize, batchSize;
818       /* Remainder */
819       PetscInt Nr, offset;
820 
821       blockSize = Nb*quad.numQuadPoints;
822       batchSize = numBlocks * blockSize;
823       numChunks = numCells / (numBatches*batchSize);
824       Ne        = numChunks*numBatches*batchSize;
825       Nr        = numCells % (numBatches*batchSize);
826       offset    = numCells - Nr;
827       geom.v0   = v0;
828       geom.J    = J;
829       geom.invJ = invJ;
830       geom.detJ = detJ;
831       ierr = (*mesh->integrateJacobianFEM)(Ne, numFields, fe, fieldI, fieldJ, geom, u, g0, g1, g2, g3, elemMat);CHKERRQ(ierr);
832       geom.v0   = &v0[offset*dim];
833       geom.J    = &J[offset*dim*dim];
834       geom.invJ = &invJ[offset*dim*dim];
835       geom.detJ = &detJ[offset];
836       ierr = (*mesh->integrateJacobianFEM)(Nr, numFields, fe, fieldI, fieldJ, geom, &u[offset*cellDof], g0, g1, g2, g3, &elemMat[offset*cellDof*cellDof]);CHKERRQ(ierr);
837     }
838   }
839   for (c = cStart; c < cEnd; ++c) {
840     if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, cellDof, cellDof, &elemMat[c*cellDof*cellDof]);CHKERRQ(ierr);}
841     ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMat[c*cellDof*cellDof], ADD_VALUES);CHKERRQ(ierr);
842   }
843   ierr = PetscFree6(u,v0,J,invJ,detJ,elemMat);CHKERRQ(ierr);
844   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
845   ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
846   if (mesh->printFEM) {
847     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
848     ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr);
849     ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
850   }
851   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
852   ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr);
853   if (isShell) {
854     JacActionCtx *jctx;
855 
856     ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr);
857     ierr = VecCopy(X, jctx->u);CHKERRQ(ierr);
858   }
859   *str = SAME_NONZERO_PATTERN;
860   PetscFunctionReturn(0);
861 }
862