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