xref: /petsc/src/dm/impls/plex/plexfem.c (revision c5bbbd5bcc0bc74c33668bbd60b464dee9a9800d)
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*) &((DM*) user)[1];
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*) &((DM*) user)[1];
522   PetscFE          *fe   = fem->fe;
523   const char       *name = "Residual";
524   PetscQuadrature   q;
525   PetscCellGeometry geom;
526   PetscSection      section;
527   PetscReal        *v0, *J, *invJ, *detJ;
528   PetscScalar      *elemVec, *u;
529   PetscInt          dim, numFields, f, numCells, cStart, cEnd, c;
530   PetscInt          cellDof = 0, numComponents = 0;
531   PetscErrorCode    ierr;
532 
533   PetscFunctionBegin;
534   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
535   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
536   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
537   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
538   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
539   numCells = cEnd - cStart;
540   for (f = 0; f < numFields; ++f) {
541     PetscInt Nb, Nc;
542 
543     ierr = PetscFEGetDimension(fe[f], &Nb);CHKERRQ(ierr);
544     ierr = PetscFEGetNumComponents(fe[f], &Nc);CHKERRQ(ierr);
545     cellDof       += Nb*Nc;
546     numComponents += Nc;
547   }
548   ierr = DMPlexProjectFunctionLocal(dm, numComponents, fem->bcFuncs, INSERT_BC_VALUES, X);CHKERRQ(ierr);
549   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
550   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);
551   for (c = cStart; c < cEnd; ++c) {
552     PetscScalar *x = NULL;
553     PetscInt     i;
554 
555     ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
556     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
557     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
558     for (i = 0; i < cellDof; ++i) u[c*cellDof+i] = x[i];
559     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
560   }
561   for (f = 0; f < numFields; ++f) {
562     void   (*f0)(const PetscScalar[], const PetscScalar[], const PetscReal[], const PetscScalar[], const PetscScalar[], PetscScalar[]) = fem->f0Funcs[f];
563     void   (*f1)(const PetscScalar[], const PetscScalar[], const PetscReal[], const PetscScalar[], const PetscScalar[], PetscScalar[]) = fem->f1Funcs[f];
564     PetscInt Nb;
565     /* Conforming batches */
566     PetscInt numBlocks  = 1;
567     PetscInt numBatches = 1;
568     PetscInt numChunks, Ne, blockSize, batchSize;
569     /* Remainder */
570     PetscInt Nr, offset;
571 
572     ierr = PetscFEGetQuadrature(fe[f], &q);CHKERRQ(ierr);
573     ierr = PetscFEGetDimension(fe[f], &Nb);CHKERRQ(ierr);
574     blockSize = Nb*q.numQuadPoints;
575     batchSize = numBlocks * blockSize;
576     numChunks = numCells / (numBatches*batchSize);
577     Ne        = numChunks*numBatches*batchSize;
578     Nr        = numCells % (numBatches*batchSize);
579     offset    = numCells - Nr;
580     geom.v0   = v0;
581     geom.J    = J;
582     geom.invJ = invJ;
583     geom.detJ = detJ;
584     ierr = PetscFEIntegrateResidual(fe[f], Ne, numFields, fe, f, geom, u, f0, f1, elemVec);CHKERRQ(ierr);
585     geom.v0   = &v0[offset*dim];
586     geom.J    = &J[offset*dim*dim];
587     geom.invJ = &invJ[offset*dim*dim];
588     geom.detJ = &detJ[offset];
589     ierr = PetscFEIntegrateResidual(fe[f], Nr, numFields, fe, f, geom, &u[offset*cellDof], f0, f1, &elemVec[offset*cellDof]);CHKERRQ(ierr);
590   }
591   for (c = cStart; c < cEnd; ++c) {
592     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(c, name, cellDof, &elemVec[c*cellDof]);CHKERRQ(ierr);}
593     ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*cellDof], ADD_VALUES);CHKERRQ(ierr);
594   }
595   ierr = PetscFree6(u,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr);
596   if (mesh->printFEM) {ierr = DMPrintLocalVec(dm, name, F);CHKERRQ(ierr);}
597   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
598   PetscFunctionReturn(0);
599 }
600 
601 #endif
602 
603 #undef __FUNCT__
604 #define __FUNCT__ "DMPlexComputeJacobianActionFEM"
605 /*@C
606   DMPlexComputeJacobianActionFEM - Form the local action of Jacobian J(u) on the local input X using pointwise functions specified by the user
607 
608   Input Parameters:
609 + dm - The mesh
610 . J  - The Jacobian shell matrix
611 . X  - Local input vector
612 - user - The user context
613 
614   Output Parameter:
615 . F  - Local output vector
616 
617   Note:
618   The second member of the user context must be an FEMContext.
619 
620   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
621   like a GPU, or vectorize on a multicore machine.
622 
623   Level: developer
624 
625 .seealso: DMPlexComputeResidualFEM()
626 @*/
627 PetscErrorCode DMPlexComputeJacobianActionFEM(DM dm, Mat Jac, Vec X, Vec F, void *user)
628 {
629   DM_Plex          *mesh = (DM_Plex*) dm->data;
630   PetscFEM         *fem  = (PetscFEM*) &((DM*) user)[1];
631   PetscFE          *fe   = fem->fe;
632   PetscQuadrature   quad;
633   PetscCellGeometry geom;
634   PetscSection      section;
635   JacActionCtx     *jctx;
636   PetscReal        *v0, *J, *invJ, *detJ;
637   PetscScalar      *elemVec, *u, *a;
638   PetscInt          dim, numFields, field, numCells, cStart, cEnd, c;
639   PetscInt          cellDof = 0;
640   PetscErrorCode    ierr;
641 
642   PetscFunctionBegin;
643   /* ierr = PetscLogEventBegin(DMPLEX_JacobianActionFEM,dm,0,0,0);CHKERRQ(ierr); */
644   ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr);
645   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
646   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
647   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
648   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
649   numCells = cEnd - cStart;
650   for (field = 0; field < numFields; ++field) {
651     PetscInt Nb, Nc;
652 
653     ierr = PetscFEGetDimension(fe[field], &Nb);CHKERRQ(ierr);
654     ierr = PetscFEGetNumComponents(fe[field], &Nc);CHKERRQ(ierr);
655     cellDof += Nb*Nc;
656   }
657   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
658   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);
659   for (c = cStart; c < cEnd; ++c) {
660     PetscScalar *x;
661     PetscInt     i;
662 
663     ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
664     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
665     ierr = DMPlexVecGetClosure(dm, NULL, jctx->u, c, NULL, &x);CHKERRQ(ierr);
666     for (i = 0; i < cellDof; ++i) u[c*cellDof+i] = x[i];
667     ierr = DMPlexVecRestoreClosure(dm, NULL, jctx->u, c, NULL, &x);CHKERRQ(ierr);
668     ierr = DMPlexVecGetClosure(dm, NULL, X, c, NULL, &x);CHKERRQ(ierr);
669     for (i = 0; i < cellDof; ++i) a[c*cellDof+i] = x[i];
670     ierr = DMPlexVecRestoreClosure(dm, NULL, X, c, NULL, &x);CHKERRQ(ierr);
671   }
672   for (field = 0; field < numFields; ++field) {
673     PetscInt Nb;
674     /* Conforming batches */
675     PetscInt numBlocks  = 1;
676     PetscInt numBatches = 1;
677     PetscInt numChunks, Ne, blockSize, batchSize;
678     /* Remainder */
679     PetscInt Nr, offset;
680 
681     ierr = PetscFEGetQuadrature(fe[field], &quad);CHKERRQ(ierr);
682     ierr = PetscFEGetDimension(fe[field], &Nb);CHKERRQ(ierr);
683     blockSize = Nb*quad.numQuadPoints;
684     batchSize = numBlocks * blockSize;
685     numChunks = numCells / (numBatches*batchSize);
686     Ne        = numChunks*numBatches*batchSize;
687     Nr        = numCells % (numBatches*batchSize);
688     offset    = numCells - Nr;
689     geom.v0   = v0;
690     geom.J    = J;
691     geom.invJ = invJ;
692     geom.detJ = detJ;
693     ierr = PetscFEIntegrateJacobianAction(fe[field], Ne, numFields, fe, field, geom, u, a, fem->g0Funcs, fem->g1Funcs, fem->g2Funcs, fem->g3Funcs, elemVec);CHKERRQ(ierr);
694     geom.v0   = &v0[offset*dim];
695     geom.J    = &J[offset*dim*dim];
696     geom.invJ = &invJ[offset*dim*dim];
697     geom.detJ = &detJ[offset];
698     ierr = PetscFEIntegrateJacobianAction(fe[field], Nr, numFields, fe, field, geom, &u[offset*cellDof], &a[offset*cellDof],
699                                           fem->g0Funcs, fem->g1Funcs, fem->g2Funcs, fem->g3Funcs, &elemVec[offset*cellDof]);CHKERRQ(ierr);
700   }
701   for (c = cStart; c < cEnd; ++c) {
702     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(c, "Jacobian Action", cellDof, &elemVec[c*cellDof]);CHKERRQ(ierr);}
703     ierr = DMPlexVecSetClosure(dm, NULL, F, c, &elemVec[c*cellDof], ADD_VALUES);CHKERRQ(ierr);
704   }
705   ierr = PetscFree7(u,a,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr);
706   if (mesh->printFEM) {
707     PetscMPIInt rank, numProcs;
708     PetscInt    p;
709 
710     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
711     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &numProcs);CHKERRQ(ierr);
712     ierr = PetscPrintf(PetscObjectComm((PetscObject)dm), "Jacobian Action:\n");CHKERRQ(ierr);
713     for (p = 0; p < numProcs; ++p) {
714       if (p == rank) {ierr = VecView(F, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);}
715       ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr);
716     }
717   }
718   /* ierr = PetscLogEventEnd(DMPLEX_JacobianActionFEM,dm,0,0,0);CHKERRQ(ierr); */
719   PetscFunctionReturn(0);
720 }
721 
722 #undef __FUNCT__
723 #define __FUNCT__ "DMPlexComputeJacobianFEM"
724 /*@
725   DMPlexComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user.
726 
727   Input Parameters:
728 + dm - The mesh
729 . X  - Local input vector
730 - user - The user context
731 
732   Output Parameter:
733 . Jac  - Jacobian matrix
734 
735   Note:
736   The second member of the user context must be an FEMContext.
737 
738   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
739   like a GPU, or vectorize on a multicore machine.
740 
741   Level: developer
742 
743 .seealso: FormFunctionLocal()
744 @*/
745 PetscErrorCode DMPlexComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP, MatStructure *str,void *user)
746 {
747   DM_Plex          *mesh = (DM_Plex*) dm->data;
748   PetscFEM         *fem  = (PetscFEM*) &((DM*) user)[1];
749   PetscFE          *fe   = fem->fe;
750   const char       *name = "Jacobian";
751   PetscQuadrature   quad;
752   PetscCellGeometry geom;
753   PetscSection      section, globalSection;
754   PetscReal        *v0, *J, *invJ, *detJ;
755   PetscScalar      *elemMat, *u;
756   PetscInt          dim, numFields, f, fieldI, fieldJ, numCells, cStart, cEnd, c;
757   PetscInt          cellDof = 0, numComponents = 0;
758   PetscBool         isShell;
759   PetscErrorCode    ierr;
760 
761   PetscFunctionBegin;
762   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
763   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
764   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
765   ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);
766   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
767   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
768   numCells = cEnd - cStart;
769   for (f = 0; f < numFields; ++f) {
770     PetscInt Nb, Nc;
771 
772     ierr = PetscFEGetDimension(fe[f], &Nb);CHKERRQ(ierr);
773     ierr = PetscFEGetNumComponents(fe[f], &Nc);CHKERRQ(ierr);
774     cellDof       += Nb*Nc;
775     numComponents += Nc;
776   }
777   ierr = DMPlexProjectFunctionLocal(dm, numComponents, fem->bcFuncs, INSERT_BC_VALUES, X);CHKERRQ(ierr);
778   ierr = MatZeroEntries(JacP);CHKERRQ(ierr);
779   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);
780   for (c = cStart; c < cEnd; ++c) {
781     PetscScalar *x;
782     PetscInt     i;
783 
784     ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
785     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
786     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
787     for (i = 0; i < cellDof; ++i) u[c*cellDof+i] = x[i];
788     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
789   }
790   ierr = PetscMemzero(elemMat, numCells*cellDof*cellDof * sizeof(PetscScalar));CHKERRQ(ierr);
791   for (fieldI = 0; fieldI < numFields; ++fieldI) {
792     PetscInt Nb;
793     ierr = PetscFEGetQuadrature(fe[fieldI], &quad);CHKERRQ(ierr);
794     ierr = PetscFEGetDimension(fe[fieldI], &Nb);CHKERRQ(ierr);
795     for (fieldJ = 0; fieldJ < numFields; ++fieldJ) {
796       void   (*g0)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]) = fem->g0Funcs[fieldI*numFields+fieldJ];
797       void   (*g1)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]) = fem->g1Funcs[fieldI*numFields+fieldJ];
798       void   (*g2)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]) = fem->g2Funcs[fieldI*numFields+fieldJ];
799       void   (*g3)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]) = fem->g3Funcs[fieldI*numFields+fieldJ];
800       /* Conforming batches */
801       PetscInt numBlocks  = 1;
802       PetscInt numBatches = 1;
803       PetscInt numChunks, Ne, blockSize, batchSize;
804       /* Remainder */
805       PetscInt Nr, offset;
806 
807       blockSize = Nb*quad.numQuadPoints;
808       batchSize = numBlocks * blockSize;
809       numChunks = numCells / (numBatches*batchSize);
810       Ne        = numChunks*numBatches*batchSize;
811       Nr        = numCells % (numBatches*batchSize);
812       offset    = numCells - Nr;
813       geom.v0   = v0;
814       geom.J    = J;
815       geom.invJ = invJ;
816       geom.detJ = detJ;
817       ierr = PetscFEIntegrateJacobian(fe[fieldI], Ne, numFields, fe, fieldI, fieldJ, geom, u, g0, g1, g2, g3, elemMat);CHKERRQ(ierr);
818       geom.v0   = &v0[offset*dim];
819       geom.J    = &J[offset*dim*dim];
820       geom.invJ = &invJ[offset*dim*dim];
821       geom.detJ = &detJ[offset];
822       ierr = PetscFEIntegrateJacobian(fe[fieldI], Nr, numFields, fe, fieldI, fieldJ, geom, &u[offset*cellDof], g0, g1, g2, g3, &elemMat[offset*cellDof*cellDof]);CHKERRQ(ierr);
823     }
824   }
825   for (c = cStart; c < cEnd; ++c) {
826     if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, cellDof, cellDof, &elemMat[c*cellDof*cellDof]);CHKERRQ(ierr);}
827     ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMat[c*cellDof*cellDof], ADD_VALUES);CHKERRQ(ierr);
828   }
829   ierr = PetscFree6(u,v0,J,invJ,detJ,elemMat);CHKERRQ(ierr);
830   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
831   ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
832   if (mesh->printFEM) {
833     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
834     ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr);
835     ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
836   }
837   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
838   ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr);
839   if (isShell) {
840     JacActionCtx *jctx;
841 
842     ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr);
843     ierr = VecCopy(X, jctx->u);CHKERRQ(ierr);
844   }
845   *str = SAME_NONZERO_PATTERN;
846   PetscFunctionReturn(0);
847 }
848