xref: /petsc/src/dm/impls/plex/plexfem.c (revision 9a5590873ee58c373eea72386f137c00bcad11be)
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 #undef __FUNCT__
184 #define __FUNCT__ "DMPlexProjectFunctionLocal"
185 PetscErrorCode DMPlexProjectFunctionLocal(DM dm, PetscInt numComp, void (**funcs)(const PetscReal [], PetscScalar *), InsertMode mode, Vec localX)
186 {
187   Vec            coordinates;
188   PetscSection   section, cSection;
189   PetscInt       dim, vStart, vEnd, v, c, d;
190   PetscScalar   *values, *cArray;
191   PetscReal     *coords;
192   PetscErrorCode ierr;
193 
194   PetscFunctionBegin;
195   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
196   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
197   ierr = DMPlexGetCoordinateSection(dm, &cSection);CHKERRQ(ierr);
198   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
199   ierr = PetscMalloc(numComp * sizeof(PetscScalar), &values);CHKERRQ(ierr);
200   ierr = VecGetArray(coordinates, &cArray);CHKERRQ(ierr);
201   ierr = PetscSectionGetDof(cSection, vStart, &dim);CHKERRQ(ierr);
202   ierr = PetscMalloc(dim * sizeof(PetscReal),&coords);CHKERRQ(ierr);
203   for (v = vStart; v < vEnd; ++v) {
204     PetscInt dof, off;
205 
206     ierr = PetscSectionGetDof(cSection, v, &dof);CHKERRQ(ierr);
207     ierr = PetscSectionGetOffset(cSection, v, &off);CHKERRQ(ierr);
208     if (dof > dim) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Cannot have more coordinates %d then dimensions %d", dof, dim);
209     for (d = 0; d < dof; ++d) coords[d] = PetscRealPart(cArray[off+d]);
210     for (c = 0; c < numComp; ++c) (*funcs[c])(coords, &values[c]);
211     ierr = VecSetValuesSection(localX, section, v, values, mode);CHKERRQ(ierr);
212   }
213   ierr = VecRestoreArray(coordinates, &cArray);CHKERRQ(ierr);
214   /* Temporary, must be replaced by a projection on the finite element basis */
215   {
216     PetscInt eStart = 0, eEnd = 0, e, depth;
217 
218     ierr = DMPlexGetLabelSize(dm, "depth", &depth);CHKERRQ(ierr);
219     --depth;
220     if (depth > 1) {ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);}
221     for (e = eStart; e < eEnd; ++e) {
222       const PetscInt *cone = NULL;
223       PetscInt        coneSize, d;
224       PetscScalar    *coordsA, *coordsB;
225 
226       ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
227       ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
228       if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "Cone size %d for point %d should be 2", coneSize, e);
229       ierr = VecGetValuesSection(coordinates, cSection, cone[0], &coordsA);CHKERRQ(ierr);
230       ierr = VecGetValuesSection(coordinates, cSection, cone[1], &coordsB);CHKERRQ(ierr);
231       for (d = 0; d < dim; ++d) {
232         coords[d] = 0.5*(PetscRealPart(coordsA[d]) + PetscRealPart(coordsB[d]));
233       }
234       for (c = 0; c < numComp; ++c) (*funcs[c])(coords, &values[c]);
235       ierr = VecSetValuesSection(localX, section, e, values, mode);CHKERRQ(ierr);
236     }
237   }
238 
239   ierr = PetscFree(coords);CHKERRQ(ierr);
240   ierr = PetscFree(values);CHKERRQ(ierr);
241 #if 0
242   const PetscInt localDof = this->_mesh->sizeWithBC(s, *cells->begin());
243   PetscReal      detJ;
244 
245   ierr = PetscMalloc(localDof * sizeof(PetscScalar), &values);CHKERRQ(ierr);
246   ierr = PetscMalloc2(dim,PetscReal,&v0,dim*dim,PetscReal,&J);CHKERRQ(ierr);
247   ALE::ISieveVisitor::PointRetriever<PETSC_MESH_TYPE::sieve_type> pV(PetscPowInt(this->_mesh->getSieve()->getMaxConeSize(),dim+1), true);
248 
249   for (PetscInt c = cStart; c < cEnd; ++c) {
250     ALE::ISieveTraversal<PETSC_MESH_TYPE::sieve_type>::orientedClosure(*this->_mesh->getSieve(), c, pV);
251     const PETSC_MESH_TYPE::point_type *oPoints = pV.getPoints();
252     const int                          oSize   = pV.getSize();
253     int                                v       = 0;
254 
255     ierr = DMPlexComputeCellGeometry(dm, c, v0, J, NULL, &detJ);CHKERRQ(ierr);
256     for (PetscInt cl = 0; cl < oSize; ++cl) {
257       const PetscInt fDim;
258 
259       ierr = PetscSectionGetDof(oPoints[cl], &fDim);CHKERRQ(ierr);
260       if (pointDim) {
261         for (PetscInt d = 0; d < fDim; ++d, ++v) {
262           values[v] = (*this->_options.integrate)(v0, J, v, initFunc);
263         }
264       }
265     }
266     ierr = DMPlexVecSetClosure(dm, NULL, localX, c, values);CHKERRQ(ierr);
267     pV.clear();
268   }
269   ierr = PetscFree2(v0,J);CHKERRQ(ierr);
270   ierr = PetscFree(values);CHKERRQ(ierr);
271 #endif
272   PetscFunctionReturn(0);
273 }
274 
275 #undef __FUNCT__
276 #define __FUNCT__ "DMPlexProjectFunction"
277 /*@C
278   DMPlexProjectFunction - This projects the given function into the function space provided.
279 
280   Input Parameters:
281 + dm      - The DM
282 . numComp - The number of components (functions)
283 . funcs   - The coordinate functions to evaluate
284 - mode    - The insertion mode for values
285 
286   Output Parameter:
287 . X - vector
288 
289   Level: developer
290 
291   Note:
292   This currently just calls the function with the coordinates of each vertex and edge midpoint, and stores the result in a vector.
293   We will eventually fix it.
294 
295 .seealso: DMPlexComputeL2Diff()
296 @*/
297 PetscErrorCode DMPlexProjectFunction(DM dm, PetscInt numComp, void (**funcs)(const PetscReal [], PetscScalar *), InsertMode mode, Vec X)
298 {
299   Vec            localX;
300   PetscErrorCode ierr;
301 
302   PetscFunctionBegin;
303   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
304   ierr = DMPlexProjectFunctionLocal(dm, numComp, funcs, mode, localX);CHKERRQ(ierr);
305   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
306   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
307   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
308   PetscFunctionReturn(0);
309 }
310 
311 #undef __FUNCT__
312 #define __FUNCT__ "DMPlexComputeL2Diff"
313 /*@C
314   DMPlexComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
315 
316   Input Parameters:
317 + dm    - The DM
318 . fe    - The PetscFE object for each field
319 . funcs - The functions to evaluate for each field component
320 - X     - The coefficient vector u_h
321 
322   Output Parameter:
323 . diff - The diff ||u - u_h||_2
324 
325   Level: developer
326 
327 .seealso: DMPlexProjectFunction()
328 @*/
329 PetscErrorCode DMPlexComputeL2Diff(DM dm, PetscFE fe[], void (**funcs)(const PetscReal [], PetscScalar *), Vec X, PetscReal *diff)
330 {
331   const PetscInt  debug = 0;
332   PetscSection    section;
333   PetscQuadrature quad;
334   Vec             localX;
335   PetscReal      *coords, *v0, *J, *invJ, detJ;
336   PetscReal       localDiff = 0.0;
337   PetscInt        dim, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset, comp;
338   PetscErrorCode  ierr;
339 
340   PetscFunctionBegin;
341   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
342   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
343   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
344   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
345   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
346   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
347   for (field = 0; field < numFields; ++field) {
348     PetscInt Nc;
349 
350     ierr = PetscFEGetNumComponents(fe[field], &Nc);CHKERRQ(ierr);
351     numComponents += Nc;
352   }
353   ierr = DMPlexProjectFunctionLocal(dm, numComponents, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
354   ierr = PetscMalloc4(dim,PetscReal,&coords,dim,PetscReal,&v0,dim*dim,PetscReal,&J,dim*dim,PetscReal,&invJ);CHKERRQ(ierr);
355   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
356   ierr = PetscFEGetQuadrature(fe[0], &quad);CHKERRQ(ierr);
357   for (c = cStart; c < cEnd; ++c) {
358     PetscScalar *x;
359     PetscReal    elemDiff = 0.0;
360 
361     ierr = DMPlexComputeCellGeometry(dm, c, v0, J, invJ, &detJ);CHKERRQ(ierr);
362     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
363     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
364 
365     for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) {
366       const PetscInt   numQuadPoints = quad.numQuadPoints;
367       const PetscReal *quadPoints    = quad.quadPoints;
368       const PetscReal *quadWeights   = quad.quadWeights;
369       PetscReal       *basis;
370       PetscInt         numBasisFuncs, numBasisComps, q, d, e, fc, f;
371 
372       ierr = PetscFEGetDimension(fe[field], &numBasisFuncs);CHKERRQ(ierr);
373       ierr = PetscFEGetNumComponents(fe[field], &numBasisComps);CHKERRQ(ierr);
374       ierr = PetscFEGetDefaultTabulation(fe[field], &basis, NULL, NULL);CHKERRQ(ierr);
375       if (debug) {
376         char title[1024];
377         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
378         ierr = DMPrintCellVector(c, title, numBasisFuncs*numBasisComps, &x[fieldOffset]);CHKERRQ(ierr);
379       }
380       for (q = 0; q < numQuadPoints; ++q) {
381         for (d = 0; d < dim; d++) {
382           coords[d] = v0[d];
383           for (e = 0; e < dim; e++) {
384             coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0);
385           }
386         }
387         for (fc = 0; fc < numBasisComps; ++fc) {
388           PetscScalar funcVal;
389           PetscScalar interpolant = 0.0;
390 
391           (*funcs[comp+fc])(coords, &funcVal);
392           for (f = 0; f < numBasisFuncs; ++f) {
393             const PetscInt fidx = f*numBasisComps+fc;
394             interpolant += x[fieldOffset+fidx]*basis[q*numBasisFuncs*numBasisComps+fidx];
395           }
396           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);}
397           elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal))*quadWeights[q]*detJ;
398         }
399       }
400       comp        += numBasisComps;
401       fieldOffset += numBasisFuncs*numBasisComps;
402     }
403     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
404     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
405     localDiff += elemDiff;
406   }
407   ierr  = PetscFree4(coords,v0,J,invJ);CHKERRQ(ierr);
408   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
409   ierr  = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
410   *diff = PetscSqrtReal(*diff);
411   PetscFunctionReturn(0);
412 }
413 
414 #if 0
415 
416 #undef __FUNCT__
417 #define __FUNCT__ "DMPlexComputeResidualFEM"
418 PetscErrorCode DMPlexComputeResidualFEM(DM dm, Vec X, Vec F, void *user)
419 {
420   DM_Plex         *mesh   = (DM_Plex *) dm->data;
421   PetscFEM        *fem    = (PetscFEM *) user;
422   PetscQuadrature *quad   = fem->quad;
423   PetscQuadrature *quadBd = fem->quadBd;
424   PetscSection     section;
425   PetscReal       *v0, *n, *J, *invJ, *detJ;
426   PetscScalar     *elemVec, *u;
427   PetscInt         dim, numFields, field, numBatchesTmp = 1, numCells, cStart, cEnd, c;
428   PetscInt         cellDof, numComponents;
429   PetscBool        has;
430   PetscErrorCode   ierr;
431 
432   PetscFunctionBegin;
433   if (has && quadBd) {
434     DMLabel         label;
435     IS              pointIS;
436     const PetscInt *points;
437     PetscInt        numPoints, p;
438 
439     ierr = DMPlexGetLabel(dm, "boundary", &label);CHKERRQ(ierr);
440     ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr);
441     ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
442     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
443     for (field = 0, cellDof = 0, numComponents = 0; field < numFields; ++field) {
444       cellDof       += quadBd[field].numBasisFuncs*quadBd[field].numComponents;
445       numComponents += quadBd[field].numComponents;
446     }
447     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);
448     for (p = 0; p < numPoints; ++p) {
449       const PetscInt point = points[p];
450       PetscScalar   *x;
451       PetscInt       i;
452 
453       /* TODO: Add normal determination here */
454       ierr = DMPlexComputeCellGeometry(dm, point, &v0[p*dim], &J[p*dim*dim], &invJ[p*dim*dim], &detJ[p]);CHKERRQ(ierr);
455       if (detJ[p] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", detJ[p], point);
456       ierr = DMPlexVecGetClosure(dm, NULL, X, point, NULL, &x);CHKERRQ(ierr);
457 
458       for (i = 0; i < cellDof; ++i) u[p*cellDof+i] = x[i];
459       ierr = DMPlexVecRestoreClosure(dm, NULL, X, point, NULL, &x);CHKERRQ(ierr);
460     }
461     for (field = 0; field < numFields; ++field) {
462       const PetscInt numQuadPoints = quadBd[field].numQuadPoints;
463       const PetscInt numBasisFuncs = quadBd[field].numBasisFuncs;
464       void           (*f0)(const PetscScalar[], const PetscScalar[], const PetscReal[], const PetscReal[], PetscScalar[]) = fem->f0BdFuncs[field];
465       void           (*f1)(const PetscScalar[], const PetscScalar[], const PetscReal[], const PetscReal[], PetscScalar[]) = fem->f1BdFuncs[field];
466       /* Conforming batches */
467       PetscInt blockSize  = numBasisFuncs*numQuadPoints;
468       PetscInt numBlocks  = 1;
469       PetscInt batchSize  = numBlocks * blockSize;
470       PetscInt numBatches = numBatchesTmp;
471       PetscInt numChunks  = numPoints / (numBatches*batchSize);
472       /* Remainder */
473       PetscInt numRemainder = numPoints % (numBatches * batchSize);
474       PetscInt offset       = numPoints - numRemainder;
475 
476       ierr = (*mesh->integrateBdResidualFEM)(numChunks*numBatches*batchSize, numFields, field, quadBd, u, v0, n, J, invJ, detJ, f0, f1, elemVec);CHKERRQ(ierr);
477       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],
478                                              f0, f1, &elemVec[offset*cellDof]);CHKERRQ(ierr);
479     }
480     for (p = 0; p < numPoints; ++p) {
481       const PetscInt point = points[p];
482 
483       if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "Residual", cellDof, &elemVec[p*cellDof]);CHKERRQ(ierr);}
484       ierr = DMPlexVecSetClosure(dm, NULL, F, point, &elemVec[p*cellDof], ADD_VALUES);CHKERRQ(ierr);
485     }
486     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
487     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
488     ierr = PetscFree7(u,v0,n,J,invJ,detJ,elemVec);CHKERRQ(ierr);
489   }
490   PetscFunctionReturn(0);
491 }
492 
493 #else
494 
495 #undef __FUNCT__
496 #define __FUNCT__ "DMPlexComputeResidualFEM"
497 /*@
498   DMPlexComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user
499 
500   Input Parameters:
501 + dm - The mesh
502 . X  - Local input vector
503 - user - The user context
504 
505   Output Parameter:
506 . F  - Local output vector
507 
508   Note:
509   The second member of the user context must be an FEMContext.
510 
511   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
512   like a GPU, or vectorize on a multicore machine.
513 
514   Level: developer
515 
516 .seealso: DMPlexComputeJacobianActionFEM()
517 @*/
518 PetscErrorCode DMPlexComputeResidualFEM(DM dm, Vec X, Vec F, void *user)
519 {
520   DM_Plex          *mesh  = (DM_Plex *) dm->data;
521   PetscFEM         *fem   = (PetscFEM *) user;
522   PetscFE          *fe    = fem->fe;
523   PetscFE          *feAux = fem->feAux;
524   const char       *name  = "Residual";
525   DM                dmAux;
526   Vec               A;
527   PetscQuadrature   q;
528   PetscCellGeometry geom;
529   PetscSection      section, sectionAux;
530   PetscReal        *v0, *J, *invJ, *detJ;
531   PetscScalar      *elemVec, *u, *a;
532   PetscInt          dim, Nf, NfAux = 0, f, numCells, cStart, cEnd, c;
533   PetscInt          cellDof = 0, numComponents = 0;
534   PetscInt          cellDofAux = 0, numComponentsAux = 0;
535   PetscErrorCode    ierr;
536 
537   PetscFunctionBegin;
538   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
539   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
540   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
541   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
542   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
543   numCells = cEnd - cStart;
544   for (f = 0; f < Nf; ++f) {
545     PetscInt Nb, Nc;
546 
547     ierr = PetscFEGetDimension(fe[f], &Nb);CHKERRQ(ierr);
548     ierr = PetscFEGetNumComponents(fe[f], &Nc);CHKERRQ(ierr);
549     cellDof       += Nb*Nc;
550     numComponents += Nc;
551   }
552   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
553   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
554   if (dmAux) {
555     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
556     ierr = PetscSectionGetNumFields(sectionAux, &NfAux);CHKERRQ(ierr);
557   }
558   for (f = 0; f < NfAux; ++f) {
559     PetscInt Nb, Nc;
560 
561     ierr = PetscFEGetDimension(feAux[f], &Nb);CHKERRQ(ierr);
562     ierr = PetscFEGetNumComponents(feAux[f], &Nc);CHKERRQ(ierr);
563     cellDofAux       += Nb*Nc;
564     numComponentsAux += Nc;
565   }
566   ierr = DMPlexProjectFunctionLocal(dm, numComponents, fem->bcFuncs, INSERT_BC_VALUES, X);CHKERRQ(ierr);
567   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
568   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);
569   if (dmAux) {ierr = PetscMalloc(numCells*cellDofAux * sizeof(PetscScalar), &a);CHKERRQ(ierr);}
570   for (c = cStart; c < cEnd; ++c) {
571     PetscScalar *x = NULL;
572     PetscInt     i;
573 
574     ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
575     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
576     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
577     for (i = 0; i < cellDof; ++i) u[c*cellDof+i] = x[i];
578     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
579     if (dmAux) {
580       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
581       for (i = 0; i < cellDofAux; ++i) a[c*cellDofAux+i] = x[i];
582       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
583     }
584   }
585   for (f = 0; f < Nf; ++f) {
586     void   (*f0)(const PetscScalar[], const PetscScalar[], const PetscReal[], const PetscScalar[], const PetscScalar[], PetscScalar[]) = fem->f0Funcs[f];
587     void   (*f1)(const PetscScalar[], const PetscScalar[], const PetscReal[], const PetscScalar[], const PetscScalar[], PetscScalar[]) = fem->f1Funcs[f];
588     PetscInt Nb;
589     /* Conforming batches */
590     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
591     /* Remainder */
592     PetscInt Nr, offset;
593 
594     ierr = PetscFEGetQuadrature(fe[f], &q);CHKERRQ(ierr);
595     ierr = PetscFEGetDimension(fe[f], &Nb);CHKERRQ(ierr);
596     ierr = PetscFEGetTileSizes(fe[f], NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
597     blockSize = Nb*q.numQuadPoints;
598     batchSize = numBlocks * blockSize;
599     ierr =  PetscFESetTileSizes(fe[f], blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
600     numChunks = numCells / (numBatches*batchSize);
601     Ne        = numChunks*numBatches*batchSize;
602     Nr        = numCells % (numBatches*batchSize);
603     offset    = numCells - Nr;
604     geom.v0   = v0;
605     geom.J    = J;
606     geom.invJ = invJ;
607     geom.detJ = detJ;
608     ierr = PetscFEIntegrateResidual(fe[f], Ne, Nf, fe, f, geom, u, NfAux, feAux, a, f0, f1, elemVec);CHKERRQ(ierr);
609     geom.v0   = &v0[offset*dim];
610     geom.J    = &J[offset*dim*dim];
611     geom.invJ = &invJ[offset*dim*dim];
612     geom.detJ = &detJ[offset];
613     ierr = PetscFEIntegrateResidual(fe[f], Nr, Nf, fe, f, geom, &u[offset*cellDof], NfAux, feAux, &a[offset*cellDofAux], f0, f1, &elemVec[offset*cellDof]);CHKERRQ(ierr);
614   }
615   for (c = cStart; c < cEnd; ++c) {
616     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(c, name, cellDof, &elemVec[c*cellDof]);CHKERRQ(ierr);}
617     ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*cellDof], ADD_VALUES);CHKERRQ(ierr);
618   }
619   ierr = PetscFree6(u,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr);
620   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
621   if (mesh->printFEM) {ierr = DMPrintLocalVec(dm, name, mesh->printTol, F);CHKERRQ(ierr);}
622   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
623   PetscFunctionReturn(0);
624 }
625 
626 #endif
627 
628 #undef __FUNCT__
629 #define __FUNCT__ "DMPlexComputeJacobianActionFEM"
630 /*@C
631   DMPlexComputeJacobianActionFEM - Form the local action of Jacobian J(u) on the local input X using pointwise functions specified by the user
632 
633   Input Parameters:
634 + dm - The mesh
635 . J  - The Jacobian shell matrix
636 . X  - Local input vector
637 - user - The user context
638 
639   Output Parameter:
640 . F  - Local output vector
641 
642   Note:
643   The second member of the user context must be an FEMContext.
644 
645   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
646   like a GPU, or vectorize on a multicore machine.
647 
648   Level: developer
649 
650 .seealso: DMPlexComputeResidualFEM()
651 @*/
652 PetscErrorCode DMPlexComputeJacobianActionFEM(DM dm, Mat Jac, Vec X, Vec F, void *user)
653 {
654   DM_Plex          *mesh = (DM_Plex *) dm->data;
655   PetscFEM         *fem  = (PetscFEM *) user;
656   PetscFE          *fe   = fem->fe;
657   PetscQuadrature   quad;
658   PetscCellGeometry geom;
659   PetscSection      section;
660   JacActionCtx     *jctx;
661   PetscReal        *v0, *J, *invJ, *detJ;
662   PetscScalar      *elemVec, *u, *a;
663   PetscInt          dim, numFields, field, numCells, cStart, cEnd, c;
664   PetscInt          cellDof = 0;
665   PetscErrorCode    ierr;
666 
667   PetscFunctionBegin;
668   /* ierr = PetscLogEventBegin(DMPLEX_JacobianActionFEM,dm,0,0,0);CHKERRQ(ierr); */
669   ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr);
670   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
671   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
672   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
673   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
674   numCells = cEnd - cStart;
675   for (field = 0; field < numFields; ++field) {
676     PetscInt Nb, Nc;
677 
678     ierr = PetscFEGetDimension(fe[field], &Nb);CHKERRQ(ierr);
679     ierr = PetscFEGetNumComponents(fe[field], &Nc);CHKERRQ(ierr);
680     cellDof += Nb*Nc;
681   }
682   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
683   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);
684   for (c = cStart; c < cEnd; ++c) {
685     PetscScalar *x;
686     PetscInt     i;
687 
688     ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
689     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
690     ierr = DMPlexVecGetClosure(dm, NULL, jctx->u, c, NULL, &x);CHKERRQ(ierr);
691     for (i = 0; i < cellDof; ++i) u[c*cellDof+i] = x[i];
692     ierr = DMPlexVecRestoreClosure(dm, NULL, jctx->u, c, NULL, &x);CHKERRQ(ierr);
693     ierr = DMPlexVecGetClosure(dm, NULL, X, c, NULL, &x);CHKERRQ(ierr);
694     for (i = 0; i < cellDof; ++i) a[c*cellDof+i] = x[i];
695     ierr = DMPlexVecRestoreClosure(dm, NULL, X, c, NULL, &x);CHKERRQ(ierr);
696   }
697   for (field = 0; field < numFields; ++field) {
698     PetscInt Nb;
699     /* Conforming batches */
700     PetscInt numBlocks  = 1;
701     PetscInt numBatches = 1;
702     PetscInt numChunks, Ne, blockSize, batchSize;
703     /* Remainder */
704     PetscInt Nr, offset;
705 
706     ierr = PetscFEGetQuadrature(fe[field], &quad);CHKERRQ(ierr);
707     ierr = PetscFEGetDimension(fe[field], &Nb);CHKERRQ(ierr);
708     blockSize = Nb*quad.numQuadPoints;
709     batchSize = numBlocks * blockSize;
710     numChunks = numCells / (numBatches*batchSize);
711     Ne        = numChunks*numBatches*batchSize;
712     Nr        = numCells % (numBatches*batchSize);
713     offset    = numCells - Nr;
714     geom.v0   = v0;
715     geom.J    = J;
716     geom.invJ = invJ;
717     geom.detJ = detJ;
718     ierr = PetscFEIntegrateJacobianAction(fe[field], Ne, numFields, fe, field, geom, u, a, fem->g0Funcs, fem->g1Funcs, fem->g2Funcs, fem->g3Funcs, elemVec);CHKERRQ(ierr);
719     geom.v0   = &v0[offset*dim];
720     geom.J    = &J[offset*dim*dim];
721     geom.invJ = &invJ[offset*dim*dim];
722     geom.detJ = &detJ[offset];
723     ierr = PetscFEIntegrateJacobianAction(fe[field], Nr, numFields, fe, field, geom, &u[offset*cellDof], &a[offset*cellDof],
724                                           fem->g0Funcs, fem->g1Funcs, fem->g2Funcs, fem->g3Funcs, &elemVec[offset*cellDof]);CHKERRQ(ierr);
725   }
726   for (c = cStart; c < cEnd; ++c) {
727     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(c, "Jacobian Action", cellDof, &elemVec[c*cellDof]);CHKERRQ(ierr);}
728     ierr = DMPlexVecSetClosure(dm, NULL, F, c, &elemVec[c*cellDof], ADD_VALUES);CHKERRQ(ierr);
729   }
730   ierr = PetscFree7(u,a,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr);
731   if (mesh->printFEM) {
732     PetscMPIInt rank, numProcs;
733     PetscInt    p;
734 
735     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
736     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &numProcs);CHKERRQ(ierr);
737     ierr = PetscPrintf(PetscObjectComm((PetscObject)dm), "Jacobian Action:\n");CHKERRQ(ierr);
738     for (p = 0; p < numProcs; ++p) {
739       if (p == rank) {ierr = VecView(F, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);}
740       ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr);
741     }
742   }
743   /* ierr = PetscLogEventEnd(DMPLEX_JacobianActionFEM,dm,0,0,0);CHKERRQ(ierr); */
744   PetscFunctionReturn(0);
745 }
746 
747 #undef __FUNCT__
748 #define __FUNCT__ "DMPlexComputeJacobianFEM"
749 /*@
750   DMPlexComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user.
751 
752   Input Parameters:
753 + dm - The mesh
754 . X  - Local input vector
755 - user - The user context
756 
757   Output Parameter:
758 . Jac  - Jacobian matrix
759 
760   Note:
761   The second member of the user context must be an FEMContext.
762 
763   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
764   like a GPU, or vectorize on a multicore machine.
765 
766   Level: developer
767 
768 .seealso: FormFunctionLocal()
769 @*/
770 PetscErrorCode DMPlexComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP, MatStructure *str,void *user)
771 {
772   DM_Plex          *mesh = (DM_Plex *) dm->data;
773   PetscFEM         *fem  = (PetscFEM *) user;
774   PetscFE          *fe   = fem->fe;
775   const char       *name = "Jacobian";
776   PetscQuadrature   quad;
777   PetscCellGeometry geom;
778   PetscSection      section, globalSection;
779   PetscReal        *v0, *J, *invJ, *detJ;
780   PetscScalar      *elemMat, *u;
781   PetscInt          dim, numFields, f, fieldI, fieldJ, numCells, cStart, cEnd, c;
782   PetscInt          cellDof = 0, numComponents = 0;
783   PetscBool         isShell;
784   PetscErrorCode    ierr;
785 
786   PetscFunctionBegin;
787   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
788   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
789   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
790   ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);
791   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
792   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
793   numCells = cEnd - cStart;
794   for (f = 0; f < numFields; ++f) {
795     PetscInt Nb, Nc;
796 
797     ierr = PetscFEGetDimension(fe[f], &Nb);CHKERRQ(ierr);
798     ierr = PetscFEGetNumComponents(fe[f], &Nc);CHKERRQ(ierr);
799     cellDof       += Nb*Nc;
800     numComponents += Nc;
801   }
802   ierr = DMPlexProjectFunctionLocal(dm, numComponents, fem->bcFuncs, INSERT_BC_VALUES, X);CHKERRQ(ierr);
803   ierr = MatZeroEntries(JacP);CHKERRQ(ierr);
804   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);
805   for (c = cStart; c < cEnd; ++c) {
806     PetscScalar *x;
807     PetscInt     i;
808 
809     ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
810     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
811     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
812     for (i = 0; i < cellDof; ++i) u[c*cellDof+i] = x[i];
813     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
814   }
815   ierr = PetscMemzero(elemMat, numCells*cellDof*cellDof * sizeof(PetscScalar));CHKERRQ(ierr);
816   for (fieldI = 0; fieldI < numFields; ++fieldI) {
817     PetscInt Nb;
818     ierr = PetscFEGetQuadrature(fe[fieldI], &quad);CHKERRQ(ierr);
819     ierr = PetscFEGetDimension(fe[fieldI], &Nb);CHKERRQ(ierr);
820     for (fieldJ = 0; fieldJ < numFields; ++fieldJ) {
821       void   (*g0)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]) = fem->g0Funcs[fieldI*numFields+fieldJ];
822       void   (*g1)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]) = fem->g1Funcs[fieldI*numFields+fieldJ];
823       void   (*g2)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]) = fem->g2Funcs[fieldI*numFields+fieldJ];
824       void   (*g3)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]) = fem->g3Funcs[fieldI*numFields+fieldJ];
825       /* Conforming batches */
826       PetscInt numBlocks  = 1;
827       PetscInt numBatches = 1;
828       PetscInt numChunks, Ne, blockSize, batchSize;
829       /* Remainder */
830       PetscInt Nr, offset;
831 
832       blockSize = Nb*quad.numQuadPoints;
833       batchSize = numBlocks * blockSize;
834       numChunks = numCells / (numBatches*batchSize);
835       Ne        = numChunks*numBatches*batchSize;
836       Nr        = numCells % (numBatches*batchSize);
837       offset    = numCells - Nr;
838       geom.v0   = v0;
839       geom.J    = J;
840       geom.invJ = invJ;
841       geom.detJ = detJ;
842       ierr = PetscFEIntegrateJacobian(fe[fieldI], Ne, numFields, fe, fieldI, fieldJ, geom, u, g0, g1, g2, g3, elemMat);CHKERRQ(ierr);
843       geom.v0   = &v0[offset*dim];
844       geom.J    = &J[offset*dim*dim];
845       geom.invJ = &invJ[offset*dim*dim];
846       geom.detJ = &detJ[offset];
847       ierr = PetscFEIntegrateJacobian(fe[fieldI], Nr, numFields, fe, fieldI, fieldJ, geom, &u[offset*cellDof], g0, g1, g2, g3, &elemMat[offset*cellDof*cellDof]);CHKERRQ(ierr);
848     }
849   }
850   for (c = cStart; c < cEnd; ++c) {
851     if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, cellDof, cellDof, &elemMat[c*cellDof*cellDof]);CHKERRQ(ierr);}
852     ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMat[c*cellDof*cellDof], ADD_VALUES);CHKERRQ(ierr);
853   }
854   ierr = PetscFree6(u,v0,J,invJ,detJ,elemMat);CHKERRQ(ierr);
855   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
856   ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
857   if (mesh->printFEM) {
858     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
859     ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr);
860     ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
861   }
862   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
863   ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr);
864   if (isShell) {
865     JacActionCtx *jctx;
866 
867     ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr);
868     ierr = VecCopy(X, jctx->u);CHKERRQ(ierr);
869   }
870   *str = SAME_NONZERO_PATTERN;
871   PetscFunctionReturn(0);
872 }
873