xref: /petsc/src/dm/impls/plex/plexfem.c (revision d374af2bfc209185da88830a464b0da3eb10f580)
1cb1e1211SMatthew G Knepley #include <petsc-private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2cb1e1211SMatthew G Knepley 
30f2d7e86SMatthew G. Knepley #include <petsc-private/petscfeimpl.h>
4f62f30faSMatthew G. Knepley #include <petscfv.h>
5a0845e3aSMatthew G. Knepley 
6cb1e1211SMatthew G Knepley #undef __FUNCT__
7cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexGetScale"
8cb1e1211SMatthew G Knepley PetscErrorCode DMPlexGetScale(DM dm, PetscUnit unit, PetscReal *scale)
9cb1e1211SMatthew G Knepley {
10cb1e1211SMatthew G Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
11cb1e1211SMatthew G Knepley 
12cb1e1211SMatthew G Knepley   PetscFunctionBegin;
13cb1e1211SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14cb1e1211SMatthew G Knepley   PetscValidPointer(scale, 3);
15cb1e1211SMatthew G Knepley   *scale = mesh->scale[unit];
16cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
17cb1e1211SMatthew G Knepley }
18cb1e1211SMatthew G Knepley 
19cb1e1211SMatthew G Knepley #undef __FUNCT__
20cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexSetScale"
21cb1e1211SMatthew G Knepley PetscErrorCode DMPlexSetScale(DM dm, PetscUnit unit, PetscReal scale)
22cb1e1211SMatthew G Knepley {
23cb1e1211SMatthew G Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
24cb1e1211SMatthew G Knepley 
25cb1e1211SMatthew G Knepley   PetscFunctionBegin;
26cb1e1211SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27cb1e1211SMatthew G Knepley   mesh->scale[unit] = scale;
28cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
29cb1e1211SMatthew G Knepley }
30cb1e1211SMatthew G Knepley 
31cb1e1211SMatthew G Knepley PETSC_STATIC_INLINE PetscInt epsilon(PetscInt i, PetscInt j, PetscInt k)
32cb1e1211SMatthew G Knepley {
33cb1e1211SMatthew G Knepley   switch (i) {
34cb1e1211SMatthew G Knepley   case 0:
35cb1e1211SMatthew G Knepley     switch (j) {
36cb1e1211SMatthew G Knepley     case 0: return 0;
37cb1e1211SMatthew G Knepley     case 1:
38cb1e1211SMatthew G Knepley       switch (k) {
39cb1e1211SMatthew G Knepley       case 0: return 0;
40cb1e1211SMatthew G Knepley       case 1: return 0;
41cb1e1211SMatthew G Knepley       case 2: return 1;
42cb1e1211SMatthew G Knepley       }
43cb1e1211SMatthew G Knepley     case 2:
44cb1e1211SMatthew G Knepley       switch (k) {
45cb1e1211SMatthew G Knepley       case 0: return 0;
46cb1e1211SMatthew G Knepley       case 1: return -1;
47cb1e1211SMatthew G Knepley       case 2: return 0;
48cb1e1211SMatthew G Knepley       }
49cb1e1211SMatthew G Knepley     }
50cb1e1211SMatthew G Knepley   case 1:
51cb1e1211SMatthew G Knepley     switch (j) {
52cb1e1211SMatthew G Knepley     case 0:
53cb1e1211SMatthew G Knepley       switch (k) {
54cb1e1211SMatthew G Knepley       case 0: return 0;
55cb1e1211SMatthew G Knepley       case 1: return 0;
56cb1e1211SMatthew G Knepley       case 2: return -1;
57cb1e1211SMatthew G Knepley       }
58cb1e1211SMatthew G Knepley     case 1: return 0;
59cb1e1211SMatthew G Knepley     case 2:
60cb1e1211SMatthew G Knepley       switch (k) {
61cb1e1211SMatthew G Knepley       case 0: return 1;
62cb1e1211SMatthew G Knepley       case 1: return 0;
63cb1e1211SMatthew G Knepley       case 2: return 0;
64cb1e1211SMatthew G Knepley       }
65cb1e1211SMatthew G Knepley     }
66cb1e1211SMatthew G Knepley   case 2:
67cb1e1211SMatthew G Knepley     switch (j) {
68cb1e1211SMatthew G Knepley     case 0:
69cb1e1211SMatthew G Knepley       switch (k) {
70cb1e1211SMatthew G Knepley       case 0: return 0;
71cb1e1211SMatthew G Knepley       case 1: return 1;
72cb1e1211SMatthew G Knepley       case 2: return 0;
73cb1e1211SMatthew G Knepley       }
74cb1e1211SMatthew G Knepley     case 1:
75cb1e1211SMatthew G Knepley       switch (k) {
76cb1e1211SMatthew G Knepley       case 0: return -1;
77cb1e1211SMatthew G Knepley       case 1: return 0;
78cb1e1211SMatthew G Knepley       case 2: return 0;
79cb1e1211SMatthew G Knepley       }
80cb1e1211SMatthew G Knepley     case 2: return 0;
81cb1e1211SMatthew G Knepley     }
82cb1e1211SMatthew G Knepley   }
83cb1e1211SMatthew G Knepley   return 0;
84cb1e1211SMatthew G Knepley }
85cb1e1211SMatthew G Knepley 
86cb1e1211SMatthew G Knepley #undef __FUNCT__
87cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexCreateRigidBody"
88cb1e1211SMatthew G Knepley /*@C
89cb1e1211SMatthew G Knepley   DMPlexCreateRigidBody - create rigid body modes from coordinates
90cb1e1211SMatthew G Knepley 
91cb1e1211SMatthew G Knepley   Collective on DM
92cb1e1211SMatthew G Knepley 
93cb1e1211SMatthew G Knepley   Input Arguments:
94cb1e1211SMatthew G Knepley + dm - the DM
95cb1e1211SMatthew G Knepley . section - the local section associated with the rigid field, or NULL for the default section
96cb1e1211SMatthew G Knepley - globalSection - the global section associated with the rigid field, or NULL for the default section
97cb1e1211SMatthew G Knepley 
98cb1e1211SMatthew G Knepley   Output Argument:
99cb1e1211SMatthew G Knepley . sp - the null space
100cb1e1211SMatthew G Knepley 
101cb1e1211SMatthew G Knepley   Note: This is necessary to take account of Dirichlet conditions on the displacements
102cb1e1211SMatthew G Knepley 
103cb1e1211SMatthew G Knepley   Level: advanced
104cb1e1211SMatthew G Knepley 
105cb1e1211SMatthew G Knepley .seealso: MatNullSpaceCreate()
106cb1e1211SMatthew G Knepley @*/
107cb1e1211SMatthew G Knepley PetscErrorCode DMPlexCreateRigidBody(DM dm, PetscSection section, PetscSection globalSection, MatNullSpace *sp)
108cb1e1211SMatthew G Knepley {
109cb1e1211SMatthew G Knepley   MPI_Comm       comm;
110cb1e1211SMatthew G Knepley   Vec            coordinates, localMode, mode[6];
111cb1e1211SMatthew G Knepley   PetscSection   coordSection;
112cb1e1211SMatthew G Knepley   PetscScalar   *coords;
113cb1e1211SMatthew G Knepley   PetscInt       dim, vStart, vEnd, v, n, m, d, i, j;
114cb1e1211SMatthew G Knepley   PetscErrorCode ierr;
115cb1e1211SMatthew G Knepley 
116cb1e1211SMatthew G Knepley   PetscFunctionBegin;
117cb1e1211SMatthew G Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
118c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
119cb1e1211SMatthew G Knepley   if (dim == 1) {
120cb1e1211SMatthew G Knepley     ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr);
121cb1e1211SMatthew G Knepley     PetscFunctionReturn(0);
122cb1e1211SMatthew G Knepley   }
123cb1e1211SMatthew G Knepley   if (!section)       {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
124cb1e1211SMatthew G Knepley   if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
125cb1e1211SMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr);
126cb1e1211SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
12769d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
128cb1e1211SMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
129cb1e1211SMatthew G Knepley   m    = (dim*(dim+1))/2;
130cb1e1211SMatthew G Knepley   ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr);
131cb1e1211SMatthew G Knepley   ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr);
132cb1e1211SMatthew G Knepley   ierr = VecSetUp(mode[0]);CHKERRQ(ierr);
133cb1e1211SMatthew G Knepley   for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);}
134cb1e1211SMatthew G Knepley   /* Assume P1 */
135cb1e1211SMatthew G Knepley   ierr = DMGetLocalVector(dm, &localMode);CHKERRQ(ierr);
136cb1e1211SMatthew G Knepley   for (d = 0; d < dim; ++d) {
137cb1e1211SMatthew G Knepley     PetscScalar values[3] = {0.0, 0.0, 0.0};
138cb1e1211SMatthew G Knepley 
139cb1e1211SMatthew G Knepley     values[d] = 1.0;
140cb1e1211SMatthew G Knepley     ierr      = VecSet(localMode, 0.0);CHKERRQ(ierr);
141cb1e1211SMatthew G Knepley     for (v = vStart; v < vEnd; ++v) {
142cb1e1211SMatthew G Knepley       ierr = DMPlexVecSetClosure(dm, section, localMode, v, values, INSERT_VALUES);CHKERRQ(ierr);
143cb1e1211SMatthew G Knepley     }
144cb1e1211SMatthew G Knepley     ierr = DMLocalToGlobalBegin(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
145cb1e1211SMatthew G Knepley     ierr = DMLocalToGlobalEnd(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
146cb1e1211SMatthew G Knepley   }
147cb1e1211SMatthew G Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
148cb1e1211SMatthew G Knepley   for (d = dim; d < dim*(dim+1)/2; ++d) {
149cb1e1211SMatthew G Knepley     PetscInt i, j, k = dim > 2 ? d - dim : d;
150cb1e1211SMatthew G Knepley 
151cb1e1211SMatthew G Knepley     ierr = VecSet(localMode, 0.0);CHKERRQ(ierr);
152cb1e1211SMatthew G Knepley     for (v = vStart; v < vEnd; ++v) {
153cb1e1211SMatthew G Knepley       PetscScalar values[3] = {0.0, 0.0, 0.0};
154cb1e1211SMatthew G Knepley       PetscInt    off;
155cb1e1211SMatthew G Knepley 
156cb1e1211SMatthew G Knepley       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
157cb1e1211SMatthew G Knepley       for (i = 0; i < dim; ++i) {
158cb1e1211SMatthew G Knepley         for (j = 0; j < dim; ++j) {
159cb1e1211SMatthew G Knepley           values[j] += epsilon(i, j, k)*PetscRealPart(coords[off+i]);
160cb1e1211SMatthew G Knepley         }
161cb1e1211SMatthew G Knepley       }
162cb1e1211SMatthew G Knepley       ierr = DMPlexVecSetClosure(dm, section, localMode, v, values, INSERT_VALUES);CHKERRQ(ierr);
163cb1e1211SMatthew G Knepley     }
164cb1e1211SMatthew G Knepley     ierr = DMLocalToGlobalBegin(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
165cb1e1211SMatthew G Knepley     ierr = DMLocalToGlobalEnd(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
166cb1e1211SMatthew G Knepley   }
167cb1e1211SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
168cb1e1211SMatthew G Knepley   ierr = DMRestoreLocalVector(dm, &localMode);CHKERRQ(ierr);
169cb1e1211SMatthew G Knepley   for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);}
170cb1e1211SMatthew G Knepley   /* Orthonormalize system */
171cb1e1211SMatthew G Knepley   for (i = dim; i < m; ++i) {
172cb1e1211SMatthew G Knepley     PetscScalar dots[6];
173cb1e1211SMatthew G Knepley 
174cb1e1211SMatthew G Knepley     ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr);
175cb1e1211SMatthew G Knepley     for (j = 0; j < i; ++j) dots[j] *= -1.0;
176cb1e1211SMatthew G Knepley     ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr);
177cb1e1211SMatthew G Knepley     ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);
178cb1e1211SMatthew G Knepley   }
179cb1e1211SMatthew G Knepley   ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr);
180cb1e1211SMatthew G Knepley   for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);}
181cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
182cb1e1211SMatthew G Knepley }
183cb1e1211SMatthew G Knepley 
184cb1e1211SMatthew G Knepley #undef __FUNCT__
185b29cfa1cSToby Isaac #define __FUNCT__ "DMPlexSetMaxProjectionHeight"
186b29cfa1cSToby Isaac /*@
187b29cfa1cSToby Isaac   DMPlexSetMaxProjectionHeight - In DMPlexProjectXXXLocal() functions, the projected values of a basis function's dofs
188b29cfa1cSToby Isaac   are computed by associating the basis function with one of the mesh points in its transitively-closed support, and
189b29cfa1cSToby Isaac   evaluating the dual space basis of that point.  A basis function is associated with the point in its
190b29cfa1cSToby Isaac   transitively-closed support whose mesh height is highest (w.r.t. DAG height), but not greater than the maximum
191b29cfa1cSToby Isaac   projection height, which is set with this function.  By default, the maximum projection height is zero, which means
192b29cfa1cSToby Isaac   that only mesh cells are used to project basis functions.  A height of one, for example, evaluates a cell-interior
193b29cfa1cSToby Isaac   basis functions using its cells dual space basis, but all other basis functions with the dual space basis of a face.
194b29cfa1cSToby Isaac 
195b29cfa1cSToby Isaac   Input Parameters:
196b29cfa1cSToby Isaac + dm - the DMPlex object
197b29cfa1cSToby Isaac - height - the maximum projection height >= 0
198b29cfa1cSToby Isaac 
199b29cfa1cSToby Isaac   Level: advanced
200b29cfa1cSToby Isaac 
201048b7b1eSToby Isaac .seealso: DMPlexGetMaxProjectionHeight(), DMPlexProjectFunctionLocal(), DMPlexProjectFunctionLabelLocal()
202b29cfa1cSToby Isaac @*/
203b29cfa1cSToby Isaac PetscErrorCode DMPlexSetMaxProjectionHeight(DM dm, PetscInt height)
204b29cfa1cSToby Isaac {
205b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
206b29cfa1cSToby Isaac 
207b29cfa1cSToby Isaac   PetscFunctionBegin;
208b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
209b29cfa1cSToby Isaac   plex->maxProjectionHeight = height;
210b29cfa1cSToby Isaac   PetscFunctionReturn(0);
211b29cfa1cSToby Isaac }
212b29cfa1cSToby Isaac 
213b29cfa1cSToby Isaac #undef __FUNCT__
214b29cfa1cSToby Isaac #define __FUNCT__ "DMPlexGetMaxProjectionHeight"
215b29cfa1cSToby Isaac /*@
216b29cfa1cSToby Isaac   DMPlexGetMaxProjectionHeight - Get the maximum height (w.r.t. DAG) of mesh points used to evaluate dual bases in
217b29cfa1cSToby Isaac   DMPlexProjectXXXLocal() functions.
218b29cfa1cSToby Isaac 
219b29cfa1cSToby Isaac   Input Parameters:
220b29cfa1cSToby Isaac . dm - the DMPlex object
221b29cfa1cSToby Isaac 
222b29cfa1cSToby Isaac   Output Parameters:
223b29cfa1cSToby Isaac . height - the maximum projection height
224b29cfa1cSToby Isaac 
225b29cfa1cSToby Isaac   Level: intermediate
226b29cfa1cSToby Isaac 
227048b7b1eSToby Isaac .seealso: DMPlexSetMaxProjectionHeight(), DMPlexProjectFunctionLocal(), DMPlexProjectFunctionLabelLocal()
228b29cfa1cSToby Isaac @*/
229b29cfa1cSToby Isaac PetscErrorCode DMPlexGetMaxProjectionHeight(DM dm, PetscInt *height)
230b29cfa1cSToby Isaac {
231b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
232b29cfa1cSToby Isaac 
233b29cfa1cSToby Isaac   PetscFunctionBegin;
234b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
235b29cfa1cSToby Isaac   *height = plex->maxProjectionHeight;
236b29cfa1cSToby Isaac   PetscFunctionReturn(0);
237b29cfa1cSToby Isaac }
238b29cfa1cSToby Isaac 
239b29cfa1cSToby Isaac #undef __FUNCT__
240a18a7fb9SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectFunctionLabelLocal"
241a18a7fb9SMatthew G. Knepley PetscErrorCode DMPlexProjectFunctionLabelLocal(DM dm, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscFE fe[], void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
242a18a7fb9SMatthew G. Knepley {
2437d1dd11eSToby Isaac   PetscDualSpace *sp, *cellsp;
244a18a7fb9SMatthew G. Knepley   PetscSection    section;
245a18a7fb9SMatthew G. Knepley   PetscScalar    *values;
246a18a7fb9SMatthew G. Knepley   PetscReal      *v0, *J, detJ;
247ad96f515SMatthew G. Knepley   PetscBool      *fieldActive;
2487d1dd11eSToby Isaac   PetscInt        numFields, numComp, dim, spDim, totDim, numValues, pStart, pEnd, f, d, v, i, comp, maxHeight, h;
249a18a7fb9SMatthew G. Knepley   PetscErrorCode  ierr;
250a18a7fb9SMatthew G. Knepley 
251a18a7fb9SMatthew G. Knepley   PetscFunctionBegin;
252c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
253a18a7fb9SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
254a18a7fb9SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
255a18a7fb9SMatthew G. Knepley   ierr = PetscMalloc3(numFields,&sp,dim,&v0,dim*dim,&J);CHKERRQ(ierr);
2567d1dd11eSToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr);
2577d1dd11eSToby Isaac   if (maxHeight < 0 || maxHeight > dim) {SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"maximum projection height %d not in [0, %d)\n", maxHeight,dim);}
2587d1dd11eSToby Isaac   if (maxHeight > 0) {
2597d1dd11eSToby Isaac     ierr = PetscMalloc1(numFields,&cellsp);CHKERRQ(ierr);
2607d1dd11eSToby Isaac   }
261048b7b1eSToby Isaac   else {
262048b7b1eSToby Isaac     cellsp = sp;
263048b7b1eSToby Isaac   }
2647d1dd11eSToby Isaac   for (h = 0; h <= maxHeight; h++) {
2657d1dd11eSToby Isaac     ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
2667d1dd11eSToby Isaac     if (pEnd <= pStart) continue;
2677d1dd11eSToby Isaac     totDim = 0;
268a18a7fb9SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
2697d1dd11eSToby Isaac       if (!h) {
2707d1dd11eSToby Isaac         ierr = PetscFEGetDualSpace(fe[f], &cellsp[f]);CHKERRQ(ierr);
2717d1dd11eSToby Isaac         sp[f] = cellsp[f];
2727d1dd11eSToby Isaac       }
2737d1dd11eSToby Isaac       else {
2747d1dd11eSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(cellsp[f], h, &sp[f]);CHKERRQ(ierr);
2757d1dd11eSToby Isaac         if (!sp[f]) continue;
2767d1dd11eSToby Isaac       }
277a18a7fb9SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe[f], &numComp);CHKERRQ(ierr);
278a18a7fb9SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
279a18a7fb9SMatthew G. Knepley       totDim += spDim*numComp;
280a18a7fb9SMatthew G. Knepley     }
2817d1dd11eSToby Isaac     ierr = DMPlexVecGetClosure(dm, section, localX, pStart, &numValues, NULL);CHKERRQ(ierr);
2827d1dd11eSToby Isaac     if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section point closure size %d != dual space dimension %d", numValues, totDim);
283*d374af2bSToby Isaac     if (!totDim) continue;
284a18a7fb9SMatthew G. Knepley     ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
285ad96f515SMatthew G. Knepley     ierr = DMGetWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr);
286ad96f515SMatthew G. Knepley     for (f = 0; f < numFields; ++f) fieldActive[f] = funcs[f] ? PETSC_TRUE : PETSC_FALSE;
287a18a7fb9SMatthew G. Knepley     for (i = 0; i < numIds; ++i) {
288a18a7fb9SMatthew G. Knepley       IS              pointIS;
289a18a7fb9SMatthew G. Knepley       const PetscInt *points;
290a18a7fb9SMatthew G. Knepley       PetscInt        n, p;
291a18a7fb9SMatthew G. Knepley 
292a18a7fb9SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, ids[i], &pointIS);CHKERRQ(ierr);
293a18a7fb9SMatthew G. Knepley       ierr = ISGetLocalSize(pointIS, &n);CHKERRQ(ierr);
294a18a7fb9SMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
295a18a7fb9SMatthew G. Knepley       for (p = 0; p < n; ++p) {
296a18a7fb9SMatthew G. Knepley         const PetscInt    point = points[p];
297a18a7fb9SMatthew G. Knepley         PetscCellGeometry geom;
298a18a7fb9SMatthew G. Knepley 
2997d1dd11eSToby Isaac         if ((point < pStart) || (point >= pEnd)) continue;
3008e0841e0SMatthew G. Knepley         ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, v0, J, NULL, &detJ);CHKERRQ(ierr);
301a18a7fb9SMatthew G. Knepley         geom.v0   = v0;
302a18a7fb9SMatthew G. Knepley         geom.J    = J;
303a18a7fb9SMatthew G. Knepley         geom.detJ = &detJ;
304a18a7fb9SMatthew G. Knepley         for (f = 0, v = 0; f < numFields; ++f) {
305a18a7fb9SMatthew G. Knepley           void * const ctx = ctxs ? ctxs[f] : NULL;
3067d1dd11eSToby Isaac           if (!sp[f]) continue;
307a18a7fb9SMatthew G. Knepley           ierr = PetscFEGetNumComponents(fe[f], &numComp);CHKERRQ(ierr);
308a18a7fb9SMatthew G. Knepley           ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
309a18a7fb9SMatthew G. Knepley           for (d = 0; d < spDim; ++d) {
310a18a7fb9SMatthew G. Knepley             if (funcs[f]) {
311a18a7fb9SMatthew G. Knepley               ierr = PetscDualSpaceApply(sp[f], d, geom, numComp, funcs[f], ctx, &values[v]);CHKERRQ(ierr);
312a18a7fb9SMatthew G. Knepley             } else {
313a18a7fb9SMatthew G. Knepley               for (comp = 0; comp < numComp; ++comp) values[v+comp] = 0.0;
314a18a7fb9SMatthew G. Knepley             }
315a18a7fb9SMatthew G. Knepley             v += numComp;
316a18a7fb9SMatthew G. Knepley           }
317a18a7fb9SMatthew G. Knepley         }
318ad96f515SMatthew G. Knepley         ierr = DMPlexVecSetFieldClosure_Internal(dm, section, localX, fieldActive, point, values, mode);CHKERRQ(ierr);
319a18a7fb9SMatthew G. Knepley       }
320a18a7fb9SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
321a18a7fb9SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
322a18a7fb9SMatthew G. Knepley     }
3237d1dd11eSToby Isaac     if (h) {
3247d1dd11eSToby Isaac       for (f = 0; f < numFields; ++f) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);}
3257d1dd11eSToby Isaac     }
3267d1dd11eSToby Isaac   }
327a18a7fb9SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
328ad96f515SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr);
329a18a7fb9SMatthew G. Knepley   ierr = PetscFree3(sp,v0,J);CHKERRQ(ierr);
3307d1dd11eSToby Isaac   if (maxHeight > 0) {
3317d1dd11eSToby Isaac     ierr = PetscFree(cellsp);CHKERRQ(ierr);
3327d1dd11eSToby Isaac   }
333a18a7fb9SMatthew G. Knepley   PetscFunctionReturn(0);
334a18a7fb9SMatthew G. Knepley }
335a18a7fb9SMatthew G. Knepley 
336a18a7fb9SMatthew G. Knepley #undef __FUNCT__
337cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexProjectFunctionLocal"
3380f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexProjectFunctionLocal(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
339cb1e1211SMatthew G Knepley {
3407d1dd11eSToby Isaac   PetscDualSpace *sp, *cellsp;
34172f94c41SMatthew G. Knepley   PetscSection    section;
34272f94c41SMatthew G. Knepley   PetscScalar    *values;
34372f94c41SMatthew G. Knepley   PetscReal      *v0, *J, detJ;
3447d1dd11eSToby Isaac   PetscInt        numFields, numComp, dim, spDim, totDim, numValues, pStart, pEnd, p, f, d, v, comp, h, maxHeight;
345cb1e1211SMatthew G Knepley   PetscErrorCode  ierr;
346cb1e1211SMatthew G Knepley 
347cb1e1211SMatthew G Knepley   PetscFunctionBegin;
348cb1e1211SMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
34972f94c41SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
350785e854fSJed Brown   ierr = PetscMalloc1(numFields, &sp);CHKERRQ(ierr);
3517d1dd11eSToby Isaac   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3527d1dd11eSToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr);
3537d1dd11eSToby Isaac   if (maxHeight < 0 || maxHeight > dim) {SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"maximum projection height %d not in [0, %d)\n", maxHeight,dim);}
3547d1dd11eSToby Isaac   if (maxHeight > 0) {
3557d1dd11eSToby Isaac     ierr = PetscMalloc1(numFields,&cellsp);CHKERRQ(ierr);
3567d1dd11eSToby Isaac   }
357048b7b1eSToby Isaac   else {
358048b7b1eSToby Isaac     cellsp = sp;
359048b7b1eSToby Isaac   }
3607d1dd11eSToby Isaac   ierr = PetscMalloc2(dim,&v0,dim*dim,&J);CHKERRQ(ierr);
3617d1dd11eSToby Isaac   for (h = 0; h <= maxHeight; h++) {
3627d1dd11eSToby Isaac     ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
363048b7b1eSToby Isaac     if (pEnd <= pStart) continue;
3647d1dd11eSToby Isaac     totDim = 0;
36572f94c41SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
3660f2d7e86SMatthew G. Knepley       PetscFE fe;
3670f2d7e86SMatthew G. Knepley 
3680f2d7e86SMatthew G. Knepley       ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
3697d1dd11eSToby Isaac       if (!h) {
3707d1dd11eSToby Isaac         ierr = PetscFEGetDualSpace(fe, &cellsp[f]);CHKERRQ(ierr);
3717d1dd11eSToby Isaac         sp[f] = cellsp[f];
3727d1dd11eSToby Isaac       }
3737d1dd11eSToby Isaac       else {
3747d1dd11eSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(cellsp[f], h, &sp[f]);CHKERRQ(ierr);
3757d1dd11eSToby Isaac         if (!sp[f]) {
3767d1dd11eSToby Isaac           continue;
3777d1dd11eSToby Isaac         }
3787d1dd11eSToby Isaac       }
3790f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
38072f94c41SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
38172f94c41SMatthew G. Knepley       totDim += spDim*numComp;
382cb1e1211SMatthew G Knepley     }
3837d1dd11eSToby Isaac     ierr = DMPlexVecGetClosure(dm, section, localX, pStart, &numValues, NULL);CHKERRQ(ierr);
3847d1dd11eSToby Isaac     if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section point closure size %d != dual space dimension %d", numValues, totDim);
385*d374af2bSToby Isaac     if (!totDim) continue;
38672f94c41SMatthew G. Knepley     ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
3877d1dd11eSToby Isaac     for (p = pStart; p < pEnd; ++p) {
38872f94c41SMatthew G. Knepley       PetscCellGeometry geom;
389cb1e1211SMatthew G Knepley 
3907d1dd11eSToby Isaac       ierr = DMPlexComputeCellGeometryFEM(dm, p, NULL, v0, J, NULL, &detJ);CHKERRQ(ierr);
39172f94c41SMatthew G. Knepley       geom.v0   = v0;
39272f94c41SMatthew G. Knepley       geom.J    = J;
39372f94c41SMatthew G. Knepley       geom.detJ = &detJ;
39472f94c41SMatthew G. Knepley       for (f = 0, v = 0; f < numFields; ++f) {
3950f2d7e86SMatthew G. Knepley         PetscFE      fe;
396c110b1eeSGeoffrey Irving         void * const ctx = ctxs ? ctxs[f] : NULL;
3970f2d7e86SMatthew G. Knepley 
3987d1dd11eSToby Isaac         if (!sp[f]) continue;
3990f2d7e86SMatthew G. Knepley         ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
4000f2d7e86SMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
40172f94c41SMatthew G. Knepley         ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
40272f94c41SMatthew G. Knepley         for (d = 0; d < spDim; ++d) {
403120386c5SMatthew G. Knepley           if (funcs[f]) {
404c110b1eeSGeoffrey Irving             ierr = PetscDualSpaceApply(sp[f], d, geom, numComp, funcs[f], ctx, &values[v]);CHKERRQ(ierr);
405120386c5SMatthew G. Knepley           } else {
406120386c5SMatthew G. Knepley             for (comp = 0; comp < numComp; ++comp) values[v+comp] = 0.0;
407120386c5SMatthew G. Knepley           }
40872f94c41SMatthew G. Knepley           v += numComp;
409cb1e1211SMatthew G Knepley         }
410cb1e1211SMatthew G Knepley       }
4117d1dd11eSToby Isaac       ierr = DMPlexVecSetClosure(dm, section, localX, p, values, mode);CHKERRQ(ierr);
412cb1e1211SMatthew G Knepley     }
41372f94c41SMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
4147d1dd11eSToby Isaac     if (h) {
4157d1dd11eSToby Isaac       for (f = 0; f < numFields; f++) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);}
4167d1dd11eSToby Isaac     }
4177d1dd11eSToby Isaac   }
4181f2da991SMatthew G. Knepley   ierr = PetscFree2(v0,J);CHKERRQ(ierr);
41972f94c41SMatthew G. Knepley   ierr = PetscFree(sp);CHKERRQ(ierr);
4207d1dd11eSToby Isaac   if (maxHeight > 0) {
4217d1dd11eSToby Isaac     ierr = PetscFree(cellsp);CHKERRQ(ierr);
4227d1dd11eSToby Isaac   }
423cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
424cb1e1211SMatthew G Knepley }
425cb1e1211SMatthew G Knepley 
426cb1e1211SMatthew G Knepley #undef __FUNCT__
427cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexProjectFunction"
428cb1e1211SMatthew G Knepley /*@C
429cb1e1211SMatthew G Knepley   DMPlexProjectFunction - This projects the given function into the function space provided.
430cb1e1211SMatthew G Knepley 
431cb1e1211SMatthew G Knepley   Input Parameters:
432cb1e1211SMatthew G Knepley + dm      - The DM
43372f94c41SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
434c110b1eeSGeoffrey Irving . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
435cb1e1211SMatthew G Knepley - mode    - The insertion mode for values
436cb1e1211SMatthew G Knepley 
437cb1e1211SMatthew G Knepley   Output Parameter:
438cb1e1211SMatthew G Knepley . X - vector
439cb1e1211SMatthew G Knepley 
440cb1e1211SMatthew G Knepley   Level: developer
441cb1e1211SMatthew G Knepley 
442878cb397SSatish Balay .seealso: DMPlexComputeL2Diff()
443878cb397SSatish Balay @*/
4440f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexProjectFunction(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
445cb1e1211SMatthew G Knepley {
446cb1e1211SMatthew G Knepley   Vec            localX;
447cb1e1211SMatthew G Knepley   PetscErrorCode ierr;
448cb1e1211SMatthew G Knepley 
449cb1e1211SMatthew G Knepley   PetscFunctionBegin;
4509a800dd8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
451cb1e1211SMatthew G Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
4520f2d7e86SMatthew G. Knepley   ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, mode, localX);CHKERRQ(ierr);
453cb1e1211SMatthew G Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
454cb1e1211SMatthew G Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
455cb1e1211SMatthew G Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
456cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
457cb1e1211SMatthew G Knepley }
458cb1e1211SMatthew G Knepley 
45955f2e967SMatthew G. Knepley #undef __FUNCT__
4600f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectFieldLocal"
4613bc3b0a0SMatthew G. Knepley PetscErrorCode DMPlexProjectFieldLocal(DM dm, Vec localU, void (**funcs)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal [], PetscScalar []), InsertMode mode, Vec localX)
4620f2d7e86SMatthew G. Knepley {
4630f2d7e86SMatthew G. Knepley   DM                dmAux;
4642764a2aaSMatthew G. Knepley   PetscDS           prob, probAux;
4650f2d7e86SMatthew G. Knepley   Vec               A;
466326413afSMatthew G. Knepley   PetscSection      section, sectionAux;
467326413afSMatthew G. Knepley   PetscScalar      *values, *u, *u_x, *a, *a_x;
4680f2d7e86SMatthew G. Knepley   PetscReal        *x, *v0, *J, *invJ, detJ, **basisField, **basisFieldDer, **basisFieldAux, **basisFieldDerAux;
469048b7b1eSToby Isaac   PetscInt          Nf, dim, spDim, totDim, numValues, cStart, cEnd, c, f, d, v, comp, maxHeight;
4700f2d7e86SMatthew G. Knepley   PetscErrorCode    ierr;
4710f2d7e86SMatthew G. Knepley 
4720f2d7e86SMatthew G. Knepley   PetscFunctionBegin;
473048b7b1eSToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr);
474048b7b1eSToby Isaac   if (maxHeight > 0) {SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Field projection for height > 0 not supported yet");}
4752764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
476c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
4770f2d7e86SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4780f2d7e86SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
4790f2d7e86SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
4802764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
4812764a2aaSMatthew G. Knepley   ierr = PetscDSGetTabulation(prob, &basisField, &basisFieldDer);CHKERRQ(ierr);
4822764a2aaSMatthew G. Knepley   ierr = PetscDSGetEvaluationArrays(prob, &u, NULL, &u_x);CHKERRQ(ierr);
4832764a2aaSMatthew G. Knepley   ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr);
4840f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
4850f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
4860f2d7e86SMatthew G. Knepley   if (dmAux) {
4872764a2aaSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
488326413afSMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
4892764a2aaSMatthew G. Knepley     ierr = PetscDSGetTabulation(prob, &basisFieldAux, &basisFieldDerAux);CHKERRQ(ierr);
4902764a2aaSMatthew G. Knepley     ierr = PetscDSGetEvaluationArrays(probAux, &a, NULL, &a_x);CHKERRQ(ierr);
4910f2d7e86SMatthew G. Knepley   }
4920f2d7e86SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValuesFEM(dm, localU);CHKERRQ(ierr);
4930f2d7e86SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr);
4940f2d7e86SMatthew G. Knepley   if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section cell closure size %d != dual space dimension %d", numValues, totDim);
4950f2d7e86SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
4960f2d7e86SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
4970f2d7e86SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
498326413afSMatthew G. Knepley     PetscScalar *coefficients = NULL, *coefficientsAux = NULL;
499326413afSMatthew G. Knepley 
5008e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
501326413afSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr);
502326413afSMatthew G. Knepley     if (dmAux) {ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);}
5030f2d7e86SMatthew G. Knepley     for (f = 0, v = 0; f < Nf; ++f) {
5043113607cSMatthew G. Knepley       PetscFE        fe;
5053113607cSMatthew G. Knepley       PetscDualSpace sp;
5063113607cSMatthew G. Knepley       PetscInt       Ncf;
5073113607cSMatthew G. Knepley 
5082764a2aaSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
5093113607cSMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &sp);CHKERRQ(ierr);
5103113607cSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Ncf);CHKERRQ(ierr);
5113113607cSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp, &spDim);CHKERRQ(ierr);
5120f2d7e86SMatthew G. Knepley       for (d = 0; d < spDim; ++d) {
5130f2d7e86SMatthew G. Knepley         PetscQuadrature  quad;
5140f2d7e86SMatthew G. Knepley         const PetscReal *points, *weights;
5150f2d7e86SMatthew G. Knepley         PetscInt         numPoints, q;
5160f2d7e86SMatthew G. Knepley 
5170f2d7e86SMatthew G. Knepley         if (funcs[f]) {
5183113607cSMatthew G. Knepley           ierr = PetscDualSpaceGetFunctional(sp, d, &quad);CHKERRQ(ierr);
5190f2d7e86SMatthew G. Knepley           ierr = PetscQuadratureGetData(quad, NULL, &numPoints, &points, &weights);CHKERRQ(ierr);
5203113607cSMatthew G. Knepley           ierr = PetscFEGetTabulation(fe, numPoints, points, &basisField[f], &basisFieldDer[f], NULL);CHKERRQ(ierr);
5210f2d7e86SMatthew G. Knepley           for (q = 0; q < numPoints; ++q) {
5220f2d7e86SMatthew G. Knepley             CoordinatesRefToReal(dim, dim, v0, J, &points[q*dim], x);
523326413afSMatthew G. Knepley             ierr = EvaluateFieldJets(prob,    PETSC_FALSE, q, invJ, coefficients,    NULL, u, u_x, NULL);CHKERRQ(ierr);
524326413afSMatthew G. Knepley             ierr = EvaluateFieldJets(probAux, PETSC_FALSE, q, invJ, coefficientsAux, NULL, a, a_x, NULL);CHKERRQ(ierr);
5253bc3b0a0SMatthew G. Knepley             (*funcs[f])(u, NULL, u_x, a, NULL, a_x, x, &values[v]);
5260f2d7e86SMatthew G. Knepley           }
5273113607cSMatthew G. Knepley           ierr = PetscFERestoreTabulation(fe, numPoints, points, &basisField[f], &basisFieldDer[f], NULL);CHKERRQ(ierr);
5280f2d7e86SMatthew G. Knepley         } else {
5290f2d7e86SMatthew G. Knepley           for (comp = 0; comp < Ncf; ++comp) values[v+comp] = 0.0;
5300f2d7e86SMatthew G. Knepley         }
5310f2d7e86SMatthew G. Knepley         v += Ncf;
5320f2d7e86SMatthew G. Knepley       }
5330f2d7e86SMatthew G. Knepley     }
534326413afSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr);
535326413afSMatthew G. Knepley     if (dmAux) {ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);}
5360f2d7e86SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, localX, c, values, mode);CHKERRQ(ierr);
5370f2d7e86SMatthew G. Knepley   }
5380f2d7e86SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
5390f2d7e86SMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
5400f2d7e86SMatthew G. Knepley   PetscFunctionReturn(0);
5410f2d7e86SMatthew G. Knepley }
5420f2d7e86SMatthew G. Knepley 
5430f2d7e86SMatthew G. Knepley #undef __FUNCT__
5440f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectField"
5450f2d7e86SMatthew G. Knepley /*@C
5460f2d7e86SMatthew G. Knepley   DMPlexProjectField - This projects the given function of the fields into the function space provided.
5470f2d7e86SMatthew G. Knepley 
5480f2d7e86SMatthew G. Knepley   Input Parameters:
5490f2d7e86SMatthew G. Knepley + dm      - The DM
5500f2d7e86SMatthew G. Knepley . U       - The input field vector
5510f2d7e86SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
5520f2d7e86SMatthew G. Knepley - mode    - The insertion mode for values
5530f2d7e86SMatthew G. Knepley 
5540f2d7e86SMatthew G. Knepley   Output Parameter:
5550f2d7e86SMatthew G. Knepley . X       - The output vector
5560f2d7e86SMatthew G. Knepley 
5570f2d7e86SMatthew G. Knepley   Level: developer
5580f2d7e86SMatthew G. Knepley 
5590f2d7e86SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff()
5600f2d7e86SMatthew G. Knepley @*/
5613bc3b0a0SMatthew G. Knepley PetscErrorCode DMPlexProjectField(DM dm, Vec U, void (**funcs)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal [], PetscScalar []), InsertMode mode, Vec X)
5620f2d7e86SMatthew G. Knepley {
5630f2d7e86SMatthew G. Knepley   Vec            localX, localU;
5640f2d7e86SMatthew G. Knepley   PetscErrorCode ierr;
5650f2d7e86SMatthew G. Knepley 
5660f2d7e86SMatthew G. Knepley   PetscFunctionBegin;
5670f2d7e86SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5680f2d7e86SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
5690f2d7e86SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localU);CHKERRQ(ierr);
5700f2d7e86SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, U, INSERT_VALUES, localU);CHKERRQ(ierr);
5710f2d7e86SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, U, INSERT_VALUES, localU);CHKERRQ(ierr);
5723113607cSMatthew G. Knepley   ierr = DMPlexProjectFieldLocal(dm, localU, funcs, mode, localX);CHKERRQ(ierr);
5730f2d7e86SMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
5740f2d7e86SMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
5750f2d7e86SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
5760f2d7e86SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localU);CHKERRQ(ierr);
5770f2d7e86SMatthew G. Knepley   PetscFunctionReturn(0);
5780f2d7e86SMatthew G. Knepley }
5790f2d7e86SMatthew G. Knepley 
5800f2d7e86SMatthew G. Knepley #undef __FUNCT__
5813351dd3dSMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValuesFEM"
5823351dd3dSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesFEM(DM dm, Vec localX)
58355f2e967SMatthew G. Knepley {
58455f2e967SMatthew G. Knepley   void        (**funcs)(const PetscReal x[], PetscScalar *u, void *ctx);
58555f2e967SMatthew G. Knepley   void         **ctxs;
58655f2e967SMatthew G. Knepley   PetscFE       *fe;
58755f2e967SMatthew G. Knepley   PetscInt       numFields, f, numBd, b;
58855f2e967SMatthew G. Knepley   PetscErrorCode ierr;
58955f2e967SMatthew G. Knepley 
59055f2e967SMatthew G. Knepley   PetscFunctionBegin;
59155f2e967SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
59255f2e967SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 2);
59355f2e967SMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
59455f2e967SMatthew G. Knepley   ierr = PetscMalloc3(numFields,&fe,numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
59555f2e967SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {ierr = DMGetField(dm, f, (PetscObject *) &fe[f]);CHKERRQ(ierr);}
59655f2e967SMatthew G. Knepley   /* OPT: Could attempt to do multiple BCs at once */
59755f2e967SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
59855f2e967SMatthew G. Knepley   for (b = 0; b < numBd; ++b) {
599a18a7fb9SMatthew G. Knepley     DMLabel         label;
60055f2e967SMatthew G. Knepley     const PetscInt *ids;
60163d5297fSMatthew G. Knepley     const char     *labelname;
60255f2e967SMatthew G. Knepley     PetscInt        numids, field;
60355f2e967SMatthew G. Knepley     PetscBool       isEssential;
60455f2e967SMatthew G. Knepley     void          (*func)();
60555f2e967SMatthew G. Knepley     void           *ctx;
60655f2e967SMatthew G. Knepley 
60763d5297fSMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, b, &isEssential, NULL, &labelname, &field, &func, &numids, &ids, &ctx);CHKERRQ(ierr);
60863d5297fSMatthew G. Knepley     ierr = DMPlexGetLabel(dm, labelname, &label);CHKERRQ(ierr);
60955f2e967SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
61055f2e967SMatthew G. Knepley       funcs[f] = field == f ? (void (*)(const PetscReal[], PetscScalar *, void *)) func : NULL;
61155f2e967SMatthew G. Knepley       ctxs[f]  = field == f ? ctx : NULL;
61255f2e967SMatthew G. Knepley     }
613a18a7fb9SMatthew G. Knepley     ierr = DMPlexProjectFunctionLabelLocal(dm, label, numids, ids, fe, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
61455f2e967SMatthew G. Knepley   }
61555f2e967SMatthew G. Knepley   ierr = PetscFree3(fe,funcs,ctxs);CHKERRQ(ierr);
61655f2e967SMatthew G. Knepley   PetscFunctionReturn(0);
61755f2e967SMatthew G. Knepley }
61855f2e967SMatthew G. Knepley 
619cb1e1211SMatthew G Knepley #undef __FUNCT__
620cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexComputeL2Diff"
621cb1e1211SMatthew G Knepley /*@C
622cb1e1211SMatthew G Knepley   DMPlexComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
623cb1e1211SMatthew G Knepley 
624cb1e1211SMatthew G Knepley   Input Parameters:
625cb1e1211SMatthew G Knepley + dm    - The DM
626cb1e1211SMatthew G Knepley . funcs - The functions to evaluate for each field component
62751259fa3SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
628cb1e1211SMatthew G Knepley - X     - The coefficient vector u_h
629cb1e1211SMatthew G Knepley 
630cb1e1211SMatthew G Knepley   Output Parameter:
631cb1e1211SMatthew G Knepley . diff - The diff ||u - u_h||_2
632cb1e1211SMatthew G Knepley 
633cb1e1211SMatthew G Knepley   Level: developer
634cb1e1211SMatthew G Knepley 
63523d86601SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2GradientDiff()
636878cb397SSatish Balay @*/
6370f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2Diff(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
638cb1e1211SMatthew G Knepley {
639cb1e1211SMatthew G Knepley   const PetscInt  debug = 0;
640cb1e1211SMatthew G Knepley   PetscSection    section;
641c5bbbd5bSMatthew G. Knepley   PetscQuadrature quad;
642cb1e1211SMatthew G Knepley   Vec             localX;
64372f94c41SMatthew G. Knepley   PetscScalar    *funcVal;
644cb1e1211SMatthew G Knepley   PetscReal      *coords, *v0, *J, *invJ, detJ;
645cb1e1211SMatthew G Knepley   PetscReal       localDiff = 0.0;
646cb1e1211SMatthew G Knepley   PetscInt        dim, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset, comp;
647cb1e1211SMatthew G Knepley   PetscErrorCode  ierr;
648cb1e1211SMatthew G Knepley 
649cb1e1211SMatthew G Knepley   PetscFunctionBegin;
650c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
651cb1e1211SMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
652cb1e1211SMatthew G Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
653cb1e1211SMatthew G Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
654cb1e1211SMatthew G Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
655cb1e1211SMatthew G Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
656cb1e1211SMatthew G Knepley   for (field = 0; field < numFields; ++field) {
6570f2d7e86SMatthew G. Knepley     PetscFE  fe;
658c5bbbd5bSMatthew G. Knepley     PetscInt Nc;
659c5bbbd5bSMatthew G. Knepley 
6600f2d7e86SMatthew G. Knepley     ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
6610f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
6620f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
663c5bbbd5bSMatthew G. Knepley     numComponents += Nc;
664cb1e1211SMatthew G Knepley   }
6650f2d7e86SMatthew G. Knepley   ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
666dcca6d9dSJed Brown   ierr = PetscMalloc5(numComponents,&funcVal,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
667cb1e1211SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
668cb1e1211SMatthew G Knepley   for (c = cStart; c < cEnd; ++c) {
669a1e44745SMatthew G. Knepley     PetscScalar *x = NULL;
670cb1e1211SMatthew G Knepley     PetscReal    elemDiff = 0.0;
671cb1e1211SMatthew G Knepley 
6728e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
673cb1e1211SMatthew G Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
674cb1e1211SMatthew G Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
675cb1e1211SMatthew G Knepley 
676cb1e1211SMatthew G Knepley     for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) {
6770f2d7e86SMatthew G. Knepley       PetscFE          fe;
678c110b1eeSGeoffrey Irving       void * const     ctx = ctxs ? ctxs[field] : NULL;
67921454ff5SMatthew G. Knepley       const PetscReal *quadPoints, *quadWeights;
680c5bbbd5bSMatthew G. Knepley       PetscReal       *basis;
68121454ff5SMatthew G. Knepley       PetscInt         numQuadPoints, numBasisFuncs, numBasisComps, q, d, e, fc, f;
682cb1e1211SMatthew G Knepley 
6830f2d7e86SMatthew G. Knepley       ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
68421454ff5SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
6850f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &numBasisFuncs);CHKERRQ(ierr);
6860f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numBasisComps);CHKERRQ(ierr);
6870f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDefaultTabulation(fe, &basis, NULL, NULL);CHKERRQ(ierr);
688cb1e1211SMatthew G Knepley       if (debug) {
689cb1e1211SMatthew G Knepley         char title[1024];
690cb1e1211SMatthew G Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
691cb1e1211SMatthew G Knepley         ierr = DMPrintCellVector(c, title, numBasisFuncs*numBasisComps, &x[fieldOffset]);CHKERRQ(ierr);
692cb1e1211SMatthew G Knepley       }
693cb1e1211SMatthew G Knepley       for (q = 0; q < numQuadPoints; ++q) {
694cb1e1211SMatthew G Knepley         for (d = 0; d < dim; d++) {
695cb1e1211SMatthew G Knepley           coords[d] = v0[d];
696cb1e1211SMatthew G Knepley           for (e = 0; e < dim; e++) {
697cb1e1211SMatthew G Knepley             coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0);
698cb1e1211SMatthew G Knepley           }
699cb1e1211SMatthew G Knepley         }
700c110b1eeSGeoffrey Irving         (*funcs[field])(coords, funcVal, ctx);
701cb1e1211SMatthew G Knepley         for (fc = 0; fc < numBasisComps; ++fc) {
702a1d24da5SMatthew G. Knepley           PetscScalar interpolant = 0.0;
703a1d24da5SMatthew G. Knepley 
704cb1e1211SMatthew G Knepley           for (f = 0; f < numBasisFuncs; ++f) {
705cb1e1211SMatthew G Knepley             const PetscInt fidx = f*numBasisComps+fc;
706a1d24da5SMatthew G. Knepley             interpolant += x[fieldOffset+fidx]*basis[q*numBasisFuncs*numBasisComps+fidx];
707cb1e1211SMatthew G Knepley           }
70872f94c41SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %d field %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ);CHKERRQ(ierr);}
70972f94c41SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ;
710cb1e1211SMatthew G Knepley         }
711cb1e1211SMatthew G Knepley       }
712cb1e1211SMatthew G Knepley       comp        += numBasisComps;
713cb1e1211SMatthew G Knepley       fieldOffset += numBasisFuncs*numBasisComps;
714cb1e1211SMatthew G Knepley     }
715cb1e1211SMatthew G Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
716cb1e1211SMatthew G Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
717cb1e1211SMatthew G Knepley     localDiff += elemDiff;
718cb1e1211SMatthew G Knepley   }
71972f94c41SMatthew G. Knepley   ierr  = PetscFree5(funcVal,coords,v0,J,invJ);CHKERRQ(ierr);
720cb1e1211SMatthew G Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
72186a74ee0SMatthew G. Knepley   ierr  = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
722cb1e1211SMatthew G Knepley   *diff = PetscSqrtReal(*diff);
723cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
724cb1e1211SMatthew G Knepley }
725cb1e1211SMatthew G Knepley 
726cb1e1211SMatthew G Knepley #undef __FUNCT__
72740e14135SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2GradientDiff"
72840e14135SMatthew G. Knepley /*@C
72940e14135SMatthew G. Knepley   DMPlexComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
73040e14135SMatthew G. Knepley 
73140e14135SMatthew G. Knepley   Input Parameters:
73240e14135SMatthew G. Knepley + dm    - The DM
73340e14135SMatthew G. Knepley . funcs - The gradient functions to evaluate for each field component
73451259fa3SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
73540e14135SMatthew G. Knepley . X     - The coefficient vector u_h
73640e14135SMatthew G. Knepley - n     - The vector to project along
73740e14135SMatthew G. Knepley 
73840e14135SMatthew G. Knepley   Output Parameter:
73940e14135SMatthew G. Knepley . diff - The diff ||(grad u - grad u_h) . n||_2
74040e14135SMatthew G. Knepley 
74140e14135SMatthew G. Knepley   Level: developer
74240e14135SMatthew G. Knepley 
74340e14135SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff()
74440e14135SMatthew G. Knepley @*/
7450f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2GradientDiff(DM dm, void (**funcs)(const PetscReal [], const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, const PetscReal n[], PetscReal *diff)
746cb1e1211SMatthew G Knepley {
74740e14135SMatthew G. Knepley   const PetscInt  debug = 0;
748cb1e1211SMatthew G Knepley   PetscSection    section;
74940e14135SMatthew G. Knepley   PetscQuadrature quad;
75040e14135SMatthew G. Knepley   Vec             localX;
75140e14135SMatthew G. Knepley   PetscScalar    *funcVal, *interpolantVec;
75240e14135SMatthew G. Knepley   PetscReal      *coords, *realSpaceDer, *v0, *J, *invJ, detJ;
75340e14135SMatthew G. Knepley   PetscReal       localDiff = 0.0;
75440e14135SMatthew G. Knepley   PetscInt        dim, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset, comp;
755cb1e1211SMatthew G Knepley   PetscErrorCode  ierr;
756cb1e1211SMatthew G Knepley 
757cb1e1211SMatthew G Knepley   PetscFunctionBegin;
758c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
75940e14135SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
76040e14135SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
76140e14135SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
76240e14135SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
76340e14135SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
764652b88e8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
7650f2d7e86SMatthew G. Knepley     PetscFE  fe;
76640e14135SMatthew G. Knepley     PetscInt Nc;
767652b88e8SMatthew G. Knepley 
7680f2d7e86SMatthew G. Knepley     ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
7690f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
7700f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
77140e14135SMatthew G. Knepley     numComponents += Nc;
772652b88e8SMatthew G. Knepley   }
77340e14135SMatthew G. Knepley   /* ierr = DMPlexProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */
77440e14135SMatthew G. Knepley   ierr = PetscMalloc7(numComponents,&funcVal,dim,&coords,dim,&realSpaceDer,dim,&v0,dim*dim,&J,dim*dim,&invJ,dim,&interpolantVec);CHKERRQ(ierr);
77540e14135SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
77640e14135SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
77740e14135SMatthew G. Knepley     PetscScalar *x = NULL;
77840e14135SMatthew G. Knepley     PetscReal    elemDiff = 0.0;
779652b88e8SMatthew G. Knepley 
7808e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
78140e14135SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
78240e14135SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
78340e14135SMatthew G. Knepley 
78440e14135SMatthew G. Knepley     for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) {
7850f2d7e86SMatthew G. Knepley       PetscFE          fe;
78651259fa3SMatthew G. Knepley       void * const     ctx = ctxs ? ctxs[field] : NULL;
78721454ff5SMatthew G. Knepley       const PetscReal *quadPoints, *quadWeights;
78840e14135SMatthew G. Knepley       PetscReal       *basisDer;
78921454ff5SMatthew G. Knepley       PetscInt         numQuadPoints, Nb, Ncomp, q, d, e, fc, f, g;
79040e14135SMatthew G. Knepley 
7910f2d7e86SMatthew G. Knepley       ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
79221454ff5SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
7930f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
7940f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Ncomp);CHKERRQ(ierr);
7950f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr);
79640e14135SMatthew G. Knepley       if (debug) {
79740e14135SMatthew G. Knepley         char title[1024];
79840e14135SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
79940e14135SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb*Ncomp, &x[fieldOffset]);CHKERRQ(ierr);
800652b88e8SMatthew G. Knepley       }
80140e14135SMatthew G. Knepley       for (q = 0; q < numQuadPoints; ++q) {
80240e14135SMatthew G. Knepley         for (d = 0; d < dim; d++) {
80340e14135SMatthew G. Knepley           coords[d] = v0[d];
80440e14135SMatthew G. Knepley           for (e = 0; e < dim; e++) {
80540e14135SMatthew G. Knepley             coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0);
806652b88e8SMatthew G. Knepley           }
80740e14135SMatthew G. Knepley         }
80851259fa3SMatthew G. Knepley         (*funcs[field])(coords, n, funcVal, ctx);
80940e14135SMatthew G. Knepley         for (fc = 0; fc < Ncomp; ++fc) {
81040e14135SMatthew G. Knepley           PetscScalar interpolant = 0.0;
81140e14135SMatthew G. Knepley 
81240e14135SMatthew G. Knepley           for (d = 0; d < dim; ++d) interpolantVec[d] = 0.0;
81340e14135SMatthew G. Knepley           for (f = 0; f < Nb; ++f) {
81440e14135SMatthew G. Knepley             const PetscInt fidx = f*Ncomp+fc;
81540e14135SMatthew G. Knepley 
81640e14135SMatthew G. Knepley             for (d = 0; d < dim; ++d) {
81740e14135SMatthew G. Knepley               realSpaceDer[d] = 0.0;
81840e14135SMatthew G. Knepley               for (g = 0; g < dim; ++g) {
81940e14135SMatthew G. Knepley                 realSpaceDer[d] += invJ[g*dim+d]*basisDer[(q*Nb*Ncomp+fidx)*dim+g];
82040e14135SMatthew G. Knepley               }
82140e14135SMatthew G. Knepley               interpolantVec[d] += x[fieldOffset+fidx]*realSpaceDer[d];
82240e14135SMatthew G. Knepley             }
82340e14135SMatthew G. Knepley           }
82440e14135SMatthew G. Knepley           for (d = 0; d < dim; ++d) interpolant += interpolantVec[d]*n[d];
82540e14135SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %d fieldDer %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ);CHKERRQ(ierr);}
82640e14135SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ;
82740e14135SMatthew G. Knepley         }
82840e14135SMatthew G. Knepley       }
82940e14135SMatthew G. Knepley       comp        += Ncomp;
83040e14135SMatthew G. Knepley       fieldOffset += Nb*Ncomp;
83140e14135SMatthew G. Knepley     }
83240e14135SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
83340e14135SMatthew G. Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
83440e14135SMatthew G. Knepley     localDiff += elemDiff;
83540e14135SMatthew G. Knepley   }
83640e14135SMatthew G. Knepley   ierr  = PetscFree7(funcVal,coords,realSpaceDer,v0,J,invJ,interpolantVec);CHKERRQ(ierr);
83740e14135SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
83840e14135SMatthew G. Knepley   ierr  = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
83940e14135SMatthew G. Knepley   *diff = PetscSqrtReal(*diff);
840cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
841cb1e1211SMatthew G Knepley }
842cb1e1211SMatthew G Knepley 
843a0845e3aSMatthew G. Knepley #undef __FUNCT__
84473d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2FieldDiff"
8450f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2FieldDiff(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
84673d901b8SMatthew G. Knepley {
84773d901b8SMatthew G. Knepley   const PetscInt  debug = 0;
84873d901b8SMatthew G. Knepley   PetscSection    section;
84973d901b8SMatthew G. Knepley   PetscQuadrature quad;
85073d901b8SMatthew G. Knepley   Vec             localX;
85173d901b8SMatthew G. Knepley   PetscScalar    *funcVal;
85273d901b8SMatthew G. Knepley   PetscReal      *coords, *v0, *J, *invJ, detJ;
85373d901b8SMatthew G. Knepley   PetscReal      *localDiff;
85473d901b8SMatthew G. Knepley   PetscInt        dim, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset, comp;
85573d901b8SMatthew G. Knepley   PetscErrorCode  ierr;
85673d901b8SMatthew G. Knepley 
85773d901b8SMatthew G. Knepley   PetscFunctionBegin;
858c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
85973d901b8SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
86073d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
86173d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
86273d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
86373d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
86473d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
8650f2d7e86SMatthew G. Knepley     PetscFE  fe;
86673d901b8SMatthew G. Knepley     PetscInt Nc;
86773d901b8SMatthew G. Knepley 
8680f2d7e86SMatthew G. Knepley     ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
8690f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
8700f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
87173d901b8SMatthew G. Knepley     numComponents += Nc;
87273d901b8SMatthew G. Knepley   }
8730f2d7e86SMatthew G. Knepley   ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
87473d901b8SMatthew G. Knepley   ierr = PetscCalloc6(numFields,&localDiff,numComponents,&funcVal,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
87573d901b8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
87673d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
87773d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
87873d901b8SMatthew G. Knepley 
8798e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
88073d901b8SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
88173d901b8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
88273d901b8SMatthew G. Knepley 
88373d901b8SMatthew G. Knepley     for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) {
8840f2d7e86SMatthew G. Knepley       PetscFE          fe;
88573d901b8SMatthew G. Knepley       void * const     ctx = ctxs ? ctxs[field] : NULL;
88673d901b8SMatthew G. Knepley       const PetscReal *quadPoints, *quadWeights;
88773d901b8SMatthew G. Knepley       PetscReal       *basis, elemDiff = 0.0;
88873d901b8SMatthew G. Knepley       PetscInt         numQuadPoints, numBasisFuncs, numBasisComps, q, d, e, fc, f;
88973d901b8SMatthew G. Knepley 
8900f2d7e86SMatthew G. Knepley       ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
89173d901b8SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
8920f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &numBasisFuncs);CHKERRQ(ierr);
8930f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numBasisComps);CHKERRQ(ierr);
8940f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDefaultTabulation(fe, &basis, NULL, NULL);CHKERRQ(ierr);
89573d901b8SMatthew G. Knepley       if (debug) {
89673d901b8SMatthew G. Knepley         char title[1024];
89773d901b8SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
89873d901b8SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, numBasisFuncs*numBasisComps, &x[fieldOffset]);CHKERRQ(ierr);
89973d901b8SMatthew G. Knepley       }
90073d901b8SMatthew G. Knepley       for (q = 0; q < numQuadPoints; ++q) {
90173d901b8SMatthew G. Knepley         for (d = 0; d < dim; d++) {
90273d901b8SMatthew G. Knepley           coords[d] = v0[d];
90373d901b8SMatthew G. Knepley           for (e = 0; e < dim; e++) {
90473d901b8SMatthew G. Knepley             coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0);
90573d901b8SMatthew G. Knepley           }
90673d901b8SMatthew G. Knepley         }
90773d901b8SMatthew G. Knepley         (*funcs[field])(coords, funcVal, ctx);
90873d901b8SMatthew G. Knepley         for (fc = 0; fc < numBasisComps; ++fc) {
90973d901b8SMatthew G. Knepley           PetscScalar interpolant = 0.0;
91073d901b8SMatthew G. Knepley 
91173d901b8SMatthew G. Knepley           for (f = 0; f < numBasisFuncs; ++f) {
91273d901b8SMatthew G. Knepley             const PetscInt fidx = f*numBasisComps+fc;
91373d901b8SMatthew G. Knepley             interpolant += x[fieldOffset+fidx]*basis[q*numBasisFuncs*numBasisComps+fidx];
91473d901b8SMatthew G. Knepley           }
91573d901b8SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %d field %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ);CHKERRQ(ierr);}
91673d901b8SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ;
91773d901b8SMatthew G. Knepley         }
91873d901b8SMatthew G. Knepley       }
91973d901b8SMatthew G. Knepley       comp        += numBasisComps;
92073d901b8SMatthew G. Knepley       fieldOffset += numBasisFuncs*numBasisComps;
92173d901b8SMatthew G. Knepley       localDiff[field] += elemDiff;
92273d901b8SMatthew G. Knepley     }
92373d901b8SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
92473d901b8SMatthew G. Knepley   }
92573d901b8SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
92673d901b8SMatthew G. Knepley   ierr  = MPI_Allreduce(localDiff, diff, numFields, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
92773d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) diff[field] = PetscSqrtReal(diff[field]);
92873d901b8SMatthew G. Knepley   ierr  = PetscFree6(localDiff,funcVal,coords,v0,J,invJ);CHKERRQ(ierr);
92973d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
93073d901b8SMatthew G. Knepley }
93173d901b8SMatthew G. Knepley 
93273d901b8SMatthew G. Knepley #undef __FUNCT__
93373d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIntegralFEM"
93473d901b8SMatthew G. Knepley /*@
93573d901b8SMatthew G. Knepley   DMPlexComputeIntegralFEM - Form the local integral F from the local input X using pointwise functions specified by the user
93673d901b8SMatthew G. Knepley 
93773d901b8SMatthew G. Knepley   Input Parameters:
93873d901b8SMatthew G. Knepley + dm - The mesh
93973d901b8SMatthew G. Knepley . X  - Local input vector
94073d901b8SMatthew G. Knepley - user - The user context
94173d901b8SMatthew G. Knepley 
94273d901b8SMatthew G. Knepley   Output Parameter:
94373d901b8SMatthew G. Knepley . integral - Local integral for each field
94473d901b8SMatthew G. Knepley 
94573d901b8SMatthew G. Knepley   Level: developer
94673d901b8SMatthew G. Knepley 
94773d901b8SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM()
94873d901b8SMatthew G. Knepley @*/
9490f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscReal *integral, void *user)
95073d901b8SMatthew G. Knepley {
95173d901b8SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
95273d901b8SMatthew G. Knepley   DM                dmAux;
95373d901b8SMatthew G. Knepley   Vec               localX, A;
9542764a2aaSMatthew G. Knepley   PetscDS           prob, probAux = NULL;
95573d901b8SMatthew G. Knepley   PetscQuadrature   q;
95673d901b8SMatthew G. Knepley   PetscCellGeometry geom;
95773d901b8SMatthew G. Knepley   PetscSection      section, sectionAux;
95873d901b8SMatthew G. Knepley   PetscReal        *v0, *J, *invJ, *detJ;
95973d901b8SMatthew G. Knepley   PetscScalar      *u, *a = NULL;
9600f2d7e86SMatthew G. Knepley   PetscInt          dim, Nf, f, numCells, cStart, cEnd, c;
9610f2d7e86SMatthew G. Knepley   PetscInt          totDim, totDimAux;
96273d901b8SMatthew G. Knepley   PetscErrorCode    ierr;
96373d901b8SMatthew G. Knepley 
96473d901b8SMatthew G. Knepley   PetscFunctionBegin;
96573d901b8SMatthew G. Knepley   /*ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);*/
96673d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
96773d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
96873d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
969c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
97073d901b8SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
9712764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
9722764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
97373d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
97473d901b8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
97573d901b8SMatthew G. Knepley   numCells = cEnd - cStart;
9760f2d7e86SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {integral[f]    = 0.0;}
97773d901b8SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
97873d901b8SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
97973d901b8SMatthew G. Knepley   if (dmAux) {
98073d901b8SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
9812764a2aaSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
9822764a2aaSMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
98373d901b8SMatthew G. Knepley   }
98473d901b8SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValuesFEM(dm, localX);CHKERRQ(ierr);
9850f2d7e86SMatthew G. Knepley   ierr = PetscMalloc5(numCells*totDim,&u,numCells*dim,&v0,numCells*dim*dim,&J,numCells*dim*dim,&invJ,numCells,&detJ);CHKERRQ(ierr);
9860f2d7e86SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
98773d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
98873d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
98973d901b8SMatthew G. Knepley     PetscInt     i;
99073d901b8SMatthew G. Knepley 
9918e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
99273d901b8SMatthew G. Knepley     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
99373d901b8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr);
9940f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
99573d901b8SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr);
99673d901b8SMatthew G. Knepley     if (dmAux) {
99773d901b8SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
9980f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
99973d901b8SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
100073d901b8SMatthew G. Knepley     }
100173d901b8SMatthew G. Knepley   }
100273d901b8SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
10030f2d7e86SMatthew G. Knepley     PetscFE  fe;
100473d901b8SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
100573d901b8SMatthew G. Knepley     /* Conforming batches */
100673d901b8SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
100773d901b8SMatthew G. Knepley     /* Remainder */
100873d901b8SMatthew G. Knepley     PetscInt Nr, offset;
100973d901b8SMatthew G. Knepley 
10102764a2aaSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
10110f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
10120f2d7e86SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
10130f2d7e86SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
101473d901b8SMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
101573d901b8SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
101673d901b8SMatthew G. Knepley     batchSize = numBlocks * blockSize;
10170f2d7e86SMatthew G. Knepley     ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
101873d901b8SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
101973d901b8SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
102073d901b8SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
102173d901b8SMatthew G. Knepley     offset    = numCells - Nr;
102273d901b8SMatthew G. Knepley     geom.v0   = v0;
102373d901b8SMatthew G. Knepley     geom.J    = J;
102473d901b8SMatthew G. Knepley     geom.invJ = invJ;
102573d901b8SMatthew G. Knepley     geom.detJ = detJ;
10260f2d7e86SMatthew G. Knepley     ierr = PetscFEIntegrate(fe, prob, f, Ne, geom, u, probAux, a, integral);CHKERRQ(ierr);
102773d901b8SMatthew G. Knepley     geom.v0   = &v0[offset*dim];
102873d901b8SMatthew G. Knepley     geom.J    = &J[offset*dim*dim];
102973d901b8SMatthew G. Knepley     geom.invJ = &invJ[offset*dim*dim];
103073d901b8SMatthew G. Knepley     geom.detJ = &detJ[offset];
10310f2d7e86SMatthew G. Knepley     ierr = PetscFEIntegrate(fe, prob, f, Nr, geom, &u[offset*totDim], probAux, &a[offset*totDimAux], integral);CHKERRQ(ierr);
103273d901b8SMatthew G. Knepley   }
103373d901b8SMatthew G. Knepley   ierr = PetscFree5(u,v0,J,invJ,detJ);CHKERRQ(ierr);
103473d901b8SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
103573d901b8SMatthew G. Knepley   if (mesh->printFEM) {
103673d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Local integral:");CHKERRQ(ierr);
103773d901b8SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", integral[f]);CHKERRQ(ierr);}
103873d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr);
103973d901b8SMatthew G. Knepley   }
104073d901b8SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
104173d901b8SMatthew G. Knepley   /* TODO: Allreduce for integral */
104273d901b8SMatthew G. Knepley   /*ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);*/
104373d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
104473d901b8SMatthew G. Knepley }
104573d901b8SMatthew G. Knepley 
104673d901b8SMatthew G. Knepley #undef __FUNCT__
10470f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidualFEM_Internal"
10480f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeResidualFEM_Internal(DM dm, Vec X, Vec X_t, Vec F, void *user)
10490f2d7e86SMatthew G. Knepley {
10500f2d7e86SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
10510f2d7e86SMatthew G. Knepley   const char       *name  = "Residual";
10520f2d7e86SMatthew G. Knepley   DM                dmAux;
10530f2d7e86SMatthew G. Knepley   DMLabel           depth;
10540f2d7e86SMatthew G. Knepley   Vec               A;
10552764a2aaSMatthew G. Knepley   PetscDS           prob, probAux = NULL;
10560f2d7e86SMatthew G. Knepley   PetscQuadrature   q;
10570f2d7e86SMatthew G. Knepley   PetscCellGeometry geom;
10580f2d7e86SMatthew G. Knepley   PetscSection      section, sectionAux;
10590f2d7e86SMatthew G. Knepley   PetscReal        *v0, *J, *invJ, *detJ;
10600f2d7e86SMatthew G. Knepley   PetscScalar      *elemVec, *u, *u_t, *a = NULL;
10610f2d7e86SMatthew G. Knepley   PetscInt          dim, Nf, f, numCells, cStart, cEnd, c, numBd, bd;
10620f2d7e86SMatthew G. Knepley   PetscInt          totDim, totDimBd, totDimAux;
10630f2d7e86SMatthew G. Knepley   PetscErrorCode    ierr;
10640f2d7e86SMatthew G. Knepley 
10650f2d7e86SMatthew G. Knepley   PetscFunctionBegin;
10660f2d7e86SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
1067c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
10680f2d7e86SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
10692764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
10702764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
10712764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr);
10720f2d7e86SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
10730f2d7e86SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
10740f2d7e86SMatthew G. Knepley   numCells = cEnd - cStart;
10750f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
10760f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
10770f2d7e86SMatthew G. Knepley   if (dmAux) {
10780f2d7e86SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
10792764a2aaSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
10802764a2aaSMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
10810f2d7e86SMatthew G. Knepley   }
10820f2d7e86SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr);
10830f2d7e86SMatthew G. Knepley   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
10840f2d7e86SMatthew G. Knepley   ierr = PetscMalloc7(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*dim,&v0,numCells*dim*dim,&J,numCells*dim*dim,&invJ,numCells,&detJ,numCells*totDim,&elemVec);CHKERRQ(ierr);
10850f2d7e86SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
10860f2d7e86SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
10870f2d7e86SMatthew G. Knepley     PetscScalar *x = NULL, *x_t = NULL;
10880f2d7e86SMatthew G. Knepley     PetscInt     i;
10890f2d7e86SMatthew G. Knepley 
10908e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
10910f2d7e86SMatthew G. Knepley     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
10920f2d7e86SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
10930f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
10940f2d7e86SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
10950f2d7e86SMatthew G. Knepley     if (X_t) {
10960f2d7e86SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
10970f2d7e86SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
10980f2d7e86SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
10990f2d7e86SMatthew G. Knepley     }
11000f2d7e86SMatthew G. Knepley     if (dmAux) {
11010f2d7e86SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
11020f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
11030f2d7e86SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
11040f2d7e86SMatthew G. Knepley     }
11050f2d7e86SMatthew G. Knepley   }
11060f2d7e86SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
11070f2d7e86SMatthew G. Knepley     PetscFE  fe;
11080f2d7e86SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
11090f2d7e86SMatthew G. Knepley     /* Conforming batches */
11100f2d7e86SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
11110f2d7e86SMatthew G. Knepley     /* Remainder */
11120f2d7e86SMatthew G. Knepley     PetscInt Nr, offset;
11130f2d7e86SMatthew G. Knepley 
11142764a2aaSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
11150f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
11160f2d7e86SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
11170f2d7e86SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
11180f2d7e86SMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
11190f2d7e86SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
11200f2d7e86SMatthew G. Knepley     batchSize = numBlocks * blockSize;
11210f2d7e86SMatthew G. Knepley     ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
11220f2d7e86SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
11230f2d7e86SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
11240f2d7e86SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
11250f2d7e86SMatthew G. Knepley     offset    = numCells - Nr;
11260f2d7e86SMatthew G. Knepley     geom.v0   = v0;
11270f2d7e86SMatthew G. Knepley     geom.J    = J;
11280f2d7e86SMatthew G. Knepley     geom.invJ = invJ;
11290f2d7e86SMatthew G. Knepley     geom.detJ = detJ;
11300f2d7e86SMatthew G. Knepley     ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, geom, u, u_t, probAux, a, elemVec);CHKERRQ(ierr);
11310f2d7e86SMatthew G. Knepley     geom.v0   = &v0[offset*dim];
11320f2d7e86SMatthew G. Knepley     geom.J    = &J[offset*dim*dim];
11330f2d7e86SMatthew G. Knepley     geom.invJ = &invJ[offset*dim*dim];
11340f2d7e86SMatthew G. Knepley     geom.detJ = &detJ[offset];
11350f2d7e86SMatthew G. Knepley     ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, geom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVec[offset*totDim]);CHKERRQ(ierr);
11360f2d7e86SMatthew G. Knepley   }
11370f2d7e86SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
11380f2d7e86SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(c, name, totDim, &elemVec[c*totDim]);CHKERRQ(ierr);}
11390f2d7e86SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_VALUES);CHKERRQ(ierr);
11400f2d7e86SMatthew G. Knepley   }
11410f2d7e86SMatthew G. Knepley   ierr = PetscFree7(u,u_t,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr);
11420f2d7e86SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
11430f2d7e86SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
11440f2d7e86SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
11450f2d7e86SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
11460f2d7e86SMatthew G. Knepley     const char     *bdLabel;
11470f2d7e86SMatthew G. Knepley     DMLabel         label;
11480f2d7e86SMatthew G. Knepley     IS              pointIS;
11490f2d7e86SMatthew G. Knepley     const PetscInt *points;
11500f2d7e86SMatthew G. Knepley     const PetscInt *values;
11510f2d7e86SMatthew G. Knepley     PetscReal      *n;
11520f2d7e86SMatthew G. Knepley     PetscInt        field, numValues, numPoints, p, dep, numFaces;
11530f2d7e86SMatthew G. Knepley     PetscBool       isEssential;
11540f2d7e86SMatthew G. Knepley 
11550f2d7e86SMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
11560f2d7e86SMatthew G. Knepley     if (isEssential) continue;
11570f2d7e86SMatthew G. Knepley     if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this");
11580f2d7e86SMatthew G. Knepley     ierr = DMPlexGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
11590f2d7e86SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr);
11600f2d7e86SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
11610f2d7e86SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
11620f2d7e86SMatthew G. Knepley     for (p = 0, numFaces = 0; p < numPoints; ++p) {
11630f2d7e86SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
11640f2d7e86SMatthew G. Knepley       if (dep == dim-1) ++numFaces;
11650f2d7e86SMatthew G. Knepley     }
11660f2d7e86SMatthew G. Knepley     ierr = PetscMalloc7(numFaces*totDimBd,&u,numFaces*dim,&v0,numFaces*dim,&n,numFaces*dim*dim,&J,numFaces*dim*dim,&invJ,numFaces,&detJ,numFaces*totDimBd,&elemVec);CHKERRQ(ierr);
11670f2d7e86SMatthew G. Knepley     if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);}
11680f2d7e86SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
11690f2d7e86SMatthew G. Knepley       const PetscInt point = points[p];
11700f2d7e86SMatthew G. Knepley       PetscScalar   *x     = NULL;
11710f2d7e86SMatthew G. Knepley       PetscInt       i;
11720f2d7e86SMatthew G. Knepley 
11730f2d7e86SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
11740f2d7e86SMatthew G. Knepley       if (dep != dim-1) continue;
11758e0841e0SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, &v0[f*dim], &J[f*dim*dim], &invJ[f*dim*dim], &detJ[f]);CHKERRQ(ierr);
11760f2d7e86SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, &n[f*dim]);
11770f2d7e86SMatthew G. Knepley       if (detJ[f] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", detJ[f], point);
11780f2d7e86SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
11790f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i];
11800f2d7e86SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
11810f2d7e86SMatthew G. Knepley       if (X_t) {
11820f2d7e86SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
11830f2d7e86SMatthew G. Knepley         for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i];
11840f2d7e86SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
11850f2d7e86SMatthew G. Knepley       }
11860f2d7e86SMatthew G. Knepley       ++f;
11870f2d7e86SMatthew G. Knepley     }
11880f2d7e86SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
11890f2d7e86SMatthew G. Knepley       PetscFE  fe;
11900f2d7e86SMatthew G. Knepley       PetscInt numQuadPoints, Nb;
11910f2d7e86SMatthew G. Knepley       /* Conforming batches */
11920f2d7e86SMatthew G. Knepley       PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
11930f2d7e86SMatthew G. Knepley       /* Remainder */
11940f2d7e86SMatthew G. Knepley       PetscInt Nr, offset;
11950f2d7e86SMatthew G. Knepley 
11962764a2aaSMatthew G. Knepley       ierr = PetscDSGetBdDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
11970f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
11980f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
11990f2d7e86SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
12000f2d7e86SMatthew G. Knepley       ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
12010f2d7e86SMatthew G. Knepley       blockSize = Nb*numQuadPoints;
12020f2d7e86SMatthew G. Knepley       batchSize = numBlocks * blockSize;
12030f2d7e86SMatthew G. Knepley       ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
12040f2d7e86SMatthew G. Knepley       numChunks = numFaces / (numBatches*batchSize);
12050f2d7e86SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
12060f2d7e86SMatthew G. Knepley       Nr        = numFaces % (numBatches*batchSize);
12070f2d7e86SMatthew G. Knepley       offset    = numFaces - Nr;
12080f2d7e86SMatthew G. Knepley       geom.v0   = v0;
12090f2d7e86SMatthew G. Knepley       geom.n    = n;
12100f2d7e86SMatthew G. Knepley       geom.J    = J;
12110f2d7e86SMatthew G. Knepley       geom.invJ = invJ;
12120f2d7e86SMatthew G. Knepley       geom.detJ = detJ;
12130f2d7e86SMatthew G. Knepley       ierr = PetscFEIntegrateBdResidual(fe, prob, f, Ne, geom, u, u_t, NULL, NULL, elemVec);CHKERRQ(ierr);
12140f2d7e86SMatthew G. Knepley       geom.v0   = &v0[offset*dim];
12150f2d7e86SMatthew G. Knepley       geom.n    = &n[offset*dim];
12160f2d7e86SMatthew G. Knepley       geom.J    = &J[offset*dim*dim];
12170f2d7e86SMatthew G. Knepley       geom.invJ = &invJ[offset*dim*dim];
12180f2d7e86SMatthew G. Knepley       geom.detJ = &detJ[offset];
12190f2d7e86SMatthew G. Knepley       ierr = PetscFEIntegrateBdResidual(fe, prob, f, Nr, geom, &u[offset*totDimBd], u_t ? &u_t[offset*totDimBd] : NULL, NULL, NULL, &elemVec[offset*totDimBd]);CHKERRQ(ierr);
12200f2d7e86SMatthew G. Knepley     }
12210f2d7e86SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
12220f2d7e86SMatthew G. Knepley       const PetscInt point = points[p];
12230f2d7e86SMatthew G. Knepley 
12240f2d7e86SMatthew G. Knepley       ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr);
12250f2d7e86SMatthew G. Knepley       if (dep != dim-1) continue;
12260f2d7e86SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDimBd, &elemVec[f*totDimBd]);CHKERRQ(ierr);}
12270f2d7e86SMatthew G. Knepley       ierr = DMPlexVecSetClosure(dm, NULL, F, point, &elemVec[f*totDimBd], ADD_VALUES);CHKERRQ(ierr);
12280f2d7e86SMatthew G. Knepley       ++f;
12290f2d7e86SMatthew G. Knepley     }
12300f2d7e86SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
12310f2d7e86SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
12320f2d7e86SMatthew G. Knepley     ierr = PetscFree7(u,v0,n,J,invJ,detJ,elemVec);CHKERRQ(ierr);
12330f2d7e86SMatthew G. Knepley     if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);}
12340f2d7e86SMatthew G. Knepley   }
12350f2d7e86SMatthew G. Knepley   if (mesh->printFEM) {ierr = DMPrintLocalVec(dm, name, mesh->printTol, F);CHKERRQ(ierr);}
12360f2d7e86SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
12370f2d7e86SMatthew G. Knepley   PetscFunctionReturn(0);
12380f2d7e86SMatthew G. Knepley }
12390f2d7e86SMatthew G. Knepley 
12400f2d7e86SMatthew G. Knepley #undef __FUNCT__
124108f11eefSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidualFEM_Check"
124208f11eefSMatthew G. Knepley static PetscErrorCode DMPlexComputeResidualFEM_Check(DM dm, Vec X, Vec X_t, Vec F, void *user)
124308f11eefSMatthew G. Knepley {
124408f11eefSMatthew G. Knepley   DM                dmCh, dmAux;
124508f11eefSMatthew G. Knepley   Vec               A;
12466f4eac71SMatthew G. Knepley   PetscDS           prob, probCh, probAux = NULL;
124708f11eefSMatthew G. Knepley   PetscQuadrature   q;
124808f11eefSMatthew G. Knepley   PetscCellGeometry geom;
124908f11eefSMatthew G. Knepley   PetscSection      section, sectionAux;
125008f11eefSMatthew G. Knepley   PetscReal        *v0, *J, *invJ, *detJ;
125108f11eefSMatthew G. Knepley   PetscScalar      *elemVec, *elemVecCh, *u, *u_t, *a = NULL;
125208f11eefSMatthew G. Knepley   PetscInt          dim, Nf, f, numCells, cStart, cEnd, c;
125308f11eefSMatthew G. Knepley   PetscInt          totDim, totDimAux, diffCell = 0;
125408f11eefSMatthew G. Knepley   PetscErrorCode    ierr;
125508f11eefSMatthew G. Knepley 
125608f11eefSMatthew G. Knepley   PetscFunctionBegin;
1257c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
125808f11eefSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
12596f4eac71SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
12606f4eac71SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
126108f11eefSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
126208f11eefSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
126308f11eefSMatthew G. Knepley   numCells = cEnd - cStart;
126408f11eefSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmCh", (PetscObject *) &dmCh);CHKERRQ(ierr);
12656f4eac71SMatthew G. Knepley   ierr = DMGetDS(dmCh, &probCh);CHKERRQ(ierr);
126608f11eefSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
126708f11eefSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
126808f11eefSMatthew G. Knepley   if (dmAux) {
126908f11eefSMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
12706f4eac71SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
12716f4eac71SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
127208f11eefSMatthew G. Knepley   }
127308f11eefSMatthew G. Knepley   ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr);
127408f11eefSMatthew G. Knepley   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
127508f11eefSMatthew G. Knepley   ierr = PetscMalloc7(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*dim,&v0,numCells*dim*dim,&J,numCells*dim*dim,&invJ,numCells,&detJ,numCells*totDim,&elemVec);CHKERRQ(ierr);
127608f11eefSMatthew G. Knepley   ierr = PetscMalloc1(numCells*totDim,&elemVecCh);CHKERRQ(ierr);
127708f11eefSMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
127808f11eefSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
127908f11eefSMatthew G. Knepley     PetscScalar *x = NULL, *x_t = NULL;
128008f11eefSMatthew G. Knepley     PetscInt     i;
128108f11eefSMatthew G. Knepley 
12828e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
128308f11eefSMatthew G. Knepley     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
128408f11eefSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
128508f11eefSMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
128608f11eefSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
128708f11eefSMatthew G. Knepley     if (X_t) {
128808f11eefSMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
128908f11eefSMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
129008f11eefSMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
129108f11eefSMatthew G. Knepley     }
129208f11eefSMatthew G. Knepley     if (dmAux) {
129308f11eefSMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
129408f11eefSMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
129508f11eefSMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
129608f11eefSMatthew G. Knepley     }
129708f11eefSMatthew G. Knepley   }
129808f11eefSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
129908f11eefSMatthew G. Knepley     PetscFE  fe, feCh;
130008f11eefSMatthew G. Knepley     PetscInt numQuadPoints, Nb;
130108f11eefSMatthew G. Knepley     /* Conforming batches */
130208f11eefSMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
130308f11eefSMatthew G. Knepley     /* Remainder */
130408f11eefSMatthew G. Knepley     PetscInt Nr, offset;
130508f11eefSMatthew G. Knepley 
13066f4eac71SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
13076f4eac71SMatthew G. Knepley     ierr = PetscDSGetDiscretization(probCh, f, (PetscObject *) &feCh);CHKERRQ(ierr);
130808f11eefSMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
130908f11eefSMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
131008f11eefSMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
131108f11eefSMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
131208f11eefSMatthew G. Knepley     blockSize = Nb*numQuadPoints;
131308f11eefSMatthew G. Knepley     batchSize = numBlocks * blockSize;
131408f11eefSMatthew G. Knepley     ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
131508f11eefSMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
131608f11eefSMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
131708f11eefSMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
131808f11eefSMatthew G. Knepley     offset    = numCells - Nr;
131908f11eefSMatthew G. Knepley     geom.v0   = v0;
132008f11eefSMatthew G. Knepley     geom.J    = J;
132108f11eefSMatthew G. Knepley     geom.invJ = invJ;
132208f11eefSMatthew G. Knepley     geom.detJ = detJ;
132308f11eefSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, geom, u, u_t, probAux, a, elemVec);CHKERRQ(ierr);
132408f11eefSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(feCh, prob, f, Ne, geom, u, u_t, probAux, a, elemVecCh);CHKERRQ(ierr);
132508f11eefSMatthew G. Knepley     geom.v0   = &v0[offset*dim];
132608f11eefSMatthew G. Knepley     geom.J    = &J[offset*dim*dim];
132708f11eefSMatthew G. Knepley     geom.invJ = &invJ[offset*dim*dim];
132808f11eefSMatthew G. Knepley     geom.detJ = &detJ[offset];
132908f11eefSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, geom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVec[offset*totDim]);CHKERRQ(ierr);
133008f11eefSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(feCh, prob, f, Nr, geom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVecCh[offset*totDim]);CHKERRQ(ierr);
133108f11eefSMatthew G. Knepley   }
133208f11eefSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
133308f11eefSMatthew G. Knepley     PetscBool diff = PETSC_FALSE;
133408f11eefSMatthew G. Knepley     PetscInt  d;
133508f11eefSMatthew G. Knepley 
133608f11eefSMatthew G. Knepley     for (d = 0; d < totDim; ++d) if (PetscAbsScalar(elemVec[c*totDim+d] - elemVecCh[c*totDim+d]) > 1.0e-7) {diff = PETSC_TRUE;break;}
133708f11eefSMatthew G. Knepley     if (diff) {
133808f11eefSMatthew G. Knepley       ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Different cell %d\n", c);CHKERRQ(ierr);
133908f11eefSMatthew G. Knepley       ierr = DMPrintCellVector(c, "Residual", totDim, &elemVec[c*totDim]);CHKERRQ(ierr);
134008f11eefSMatthew G. Knepley       ierr = DMPrintCellVector(c, "Check Residual", totDim, &elemVecCh[c*totDim]);CHKERRQ(ierr);
134108f11eefSMatthew G. Knepley       ++diffCell;
134208f11eefSMatthew G. Knepley     }
134308f11eefSMatthew G. Knepley     if (diffCell > 9) break;
134408f11eefSMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_VALUES);CHKERRQ(ierr);
134508f11eefSMatthew G. Knepley   }
134608f11eefSMatthew G. Knepley   ierr = PetscFree7(u,u_t,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr);
134708f11eefSMatthew G. Knepley   ierr = PetscFree(elemVecCh);CHKERRQ(ierr);
134808f11eefSMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
134908f11eefSMatthew G. Knepley   PetscFunctionReturn(0);
135008f11eefSMatthew G. Knepley }
135108f11eefSMatthew G. Knepley 
135208f11eefSMatthew G. Knepley #undef __FUNCT__
13530f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeResidualFEM"
1354a0845e3aSMatthew G. Knepley /*@
13550f2d7e86SMatthew G. Knepley   DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user
1356a0845e3aSMatthew G. Knepley 
1357a0845e3aSMatthew G. Knepley   Input Parameters:
1358a0845e3aSMatthew G. Knepley + dm - The mesh
13590f2d7e86SMatthew G. Knepley . X  - Local solution
1360a0845e3aSMatthew G. Knepley - user - The user context
1361a0845e3aSMatthew G. Knepley 
1362a0845e3aSMatthew G. Knepley   Output Parameter:
1363a0845e3aSMatthew G. Knepley . F  - Local output vector
1364a0845e3aSMatthew G. Knepley 
1365a0845e3aSMatthew G. Knepley   Level: developer
1366a0845e3aSMatthew G. Knepley 
1367a0845e3aSMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
1368a0845e3aSMatthew G. Knepley @*/
13690f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user)
1370a0845e3aSMatthew G. Knepley {
137108f11eefSMatthew G. Knepley   PetscObject    check;
1372a0845e3aSMatthew G. Knepley   PetscErrorCode ierr;
1373a0845e3aSMatthew G. Knepley 
1374a0845e3aSMatthew G. Knepley   PetscFunctionBegin;
137508f11eefSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmCh", &check);CHKERRQ(ierr);
137608f11eefSMatthew G. Knepley   if (check) {ierr = DMPlexComputeResidualFEM_Check(dm, X, NULL, F, user);CHKERRQ(ierr);}
137708f11eefSMatthew G. Knepley   else       {ierr = DMPlexComputeResidualFEM_Internal(dm, X, NULL, F, user);CHKERRQ(ierr);}
13780f2d7e86SMatthew G. Knepley   PetscFunctionReturn(0);
13790f2d7e86SMatthew G. Knepley }
13800f2d7e86SMatthew G. Knepley 
13810f2d7e86SMatthew G. Knepley #undef __FUNCT__
1382adbe6fbbSMatthew G. Knepley #define __FUNCT__ "DMPlexTSComputeIFunctionFEM"
1383adbe6fbbSMatthew G. Knepley /*@
1384adbe6fbbSMatthew G. Knepley   DMPlexTSComputeIFunctionFEM - Form the local residual F from the local input X using pointwise functions specified by the user
1385adbe6fbbSMatthew G. Knepley 
1386adbe6fbbSMatthew G. Knepley   Input Parameters:
1387adbe6fbbSMatthew G. Knepley + dm - The mesh
1388adbe6fbbSMatthew G. Knepley . t - The time
1389adbe6fbbSMatthew G. Knepley . X  - Local solution
1390adbe6fbbSMatthew G. Knepley . X_t - Local solution time derivative, or NULL
1391adbe6fbbSMatthew G. Knepley - user - The user context
1392adbe6fbbSMatthew G. Knepley 
1393adbe6fbbSMatthew G. Knepley   Output Parameter:
1394adbe6fbbSMatthew G. Knepley . F  - Local output vector
1395adbe6fbbSMatthew G. Knepley 
1396adbe6fbbSMatthew G. Knepley   Level: developer
1397adbe6fbbSMatthew G. Knepley 
1398adbe6fbbSMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
1399adbe6fbbSMatthew G. Knepley @*/
1400adbe6fbbSMatthew G. Knepley PetscErrorCode DMPlexTSComputeIFunctionFEM(DM dm, PetscReal time, Vec X, Vec X_t, Vec F, void *user)
1401adbe6fbbSMatthew G. Knepley {
1402adbe6fbbSMatthew G. Knepley   PetscErrorCode ierr;
1403adbe6fbbSMatthew G. Knepley 
1404adbe6fbbSMatthew G. Knepley   PetscFunctionBegin;
1405adbe6fbbSMatthew G. Knepley   ierr = DMPlexComputeResidualFEM_Internal(dm, X, X_t, F, user);CHKERRQ(ierr);
1406adbe6fbbSMatthew G. Knepley   PetscFunctionReturn(0);
1407adbe6fbbSMatthew G. Knepley }
1408adbe6fbbSMatthew G. Knepley 
1409adbe6fbbSMatthew G. Knepley #undef __FUNCT__
14100f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeJacobianFEM_Internal"
14110f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobianFEM_Internal(DM dm, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user)
14120f2d7e86SMatthew G. Knepley {
14130f2d7e86SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
14140f2d7e86SMatthew G. Knepley   const char       *name  = "Jacobian";
14150f2d7e86SMatthew G. Knepley   DM                dmAux;
14160f2d7e86SMatthew G. Knepley   DMLabel           depth;
14170f2d7e86SMatthew G. Knepley   Vec               A;
14182764a2aaSMatthew G. Knepley   PetscDS           prob, probAux = NULL;
14190f2d7e86SMatthew G. Knepley   PetscQuadrature   quad;
14200f2d7e86SMatthew G. Knepley   PetscCellGeometry geom;
14210f2d7e86SMatthew G. Knepley   PetscSection      section, globalSection, sectionAux;
14220f2d7e86SMatthew G. Knepley   PetscReal        *v0, *J, *invJ, *detJ;
14230f2d7e86SMatthew G. Knepley   PetscScalar      *elemMat, *u, *u_t, *a = NULL;
14240f2d7e86SMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, numCells, cStart, cEnd, c;
14250f2d7e86SMatthew G. Knepley   PetscInt          totDim, totDimBd, totDimAux, numBd, bd;
14260f2d7e86SMatthew G. Knepley   PetscBool         isShell;
14270f2d7e86SMatthew G. Knepley   PetscErrorCode    ierr;
14280f2d7e86SMatthew G. Knepley 
14290f2d7e86SMatthew G. Knepley   PetscFunctionBegin;
14300f2d7e86SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
1431c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1432a0845e3aSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
14330f2d7e86SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);
14342764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
14352764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
14362764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr);
14379a559087SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
1438a0845e3aSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1439a0845e3aSMatthew G. Knepley   numCells = cEnd - cStart;
14409a559087SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
14419a559087SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
14429a559087SMatthew G. Knepley   if (dmAux) {
14439a559087SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
14442764a2aaSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
14452764a2aaSMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
14469a559087SMatthew G. Knepley   }
14473351dd3dSMatthew G. Knepley   ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr);
14480f2d7e86SMatthew G. Knepley   ierr = MatZeroEntries(JacP);CHKERRQ(ierr);
14490f2d7e86SMatthew G. Knepley   ierr = PetscMalloc7(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*dim,&v0,numCells*dim*dim,&J,numCells*dim*dim,&invJ,numCells,&detJ,numCells*totDim*totDim,&elemMat);CHKERRQ(ierr);
14500f2d7e86SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1451a0845e3aSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
14520f2d7e86SMatthew G. Knepley     PetscScalar *x = NULL,  *x_t = NULL;
1453a0845e3aSMatthew G. Knepley     PetscInt     i;
1454a0845e3aSMatthew G. Knepley 
14558e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
1456a0845e3aSMatthew G. Knepley     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
1457a0845e3aSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
14580f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
1459a0845e3aSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
14600f2d7e86SMatthew G. Knepley     if (X_t) {
14610f2d7e86SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
14620f2d7e86SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
14630f2d7e86SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
14640f2d7e86SMatthew G. Knepley     }
14659a559087SMatthew G. Knepley     if (dmAux) {
14669a559087SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
14670f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
14689a559087SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
1469a0845e3aSMatthew G. Knepley     }
14709a559087SMatthew G. Knepley   }
14710f2d7e86SMatthew G. Knepley   ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
14720f2d7e86SMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
14730f2d7e86SMatthew G. Knepley     PetscFE  fe;
147421454ff5SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
1475a0845e3aSMatthew G. Knepley     /* Conforming batches */
1476f30c5766SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
1477a0845e3aSMatthew G. Knepley     /* Remainder */
1478a0845e3aSMatthew G. Knepley     PetscInt Nr, offset;
1479a0845e3aSMatthew G. Knepley 
14802764a2aaSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
14810f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
14820f2d7e86SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
14830f2d7e86SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
14840f2d7e86SMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
148521454ff5SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
1486a0845e3aSMatthew G. Knepley     batchSize = numBlocks * blockSize;
14870f2d7e86SMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
1488a0845e3aSMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
1489a0845e3aSMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
1490a0845e3aSMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
1491a0845e3aSMatthew G. Knepley     offset    = numCells - Nr;
14920f2d7e86SMatthew G. Knepley     for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
1493a0845e3aSMatthew G. Knepley       geom.v0   = v0;
1494a0845e3aSMatthew G. Knepley       geom.J    = J;
1495a0845e3aSMatthew G. Knepley       geom.invJ = invJ;
1496a0845e3aSMatthew G. Knepley       geom.detJ = detJ;
14970f2d7e86SMatthew G. Knepley       ierr = PetscFEIntegrateJacobian(fe, prob, fieldI, fieldJ, Ne, geom, u, u_t, probAux, a, elemMat);CHKERRQ(ierr);
1498a0845e3aSMatthew G. Knepley       geom.v0   = &v0[offset*dim];
1499a0845e3aSMatthew G. Knepley       geom.J    = &J[offset*dim*dim];
1500a0845e3aSMatthew G. Knepley       geom.invJ = &invJ[offset*dim*dim];
1501a0845e3aSMatthew G. Knepley       geom.detJ = &detJ[offset];
15020f2d7e86SMatthew G. Knepley       ierr = PetscFEIntegrateJacobian(fe, prob, fieldI, fieldJ, Nr, geom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
15030f2d7e86SMatthew G. Knepley     }
1504a0845e3aSMatthew G. Knepley   }
1505a0845e3aSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
15060f2d7e86SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[c*totDim*totDim]);CHKERRQ(ierr);}
15070f2d7e86SMatthew G. Knepley     ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMat[c*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
1508a0845e3aSMatthew G. Knepley   }
15090f2d7e86SMatthew G. Knepley   ierr = PetscFree7(u,u_t,v0,J,invJ,detJ,elemMat);CHKERRQ(ierr);
15109a559087SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
15110f2d7e86SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
15120f2d7e86SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
15131c093863SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
15141c093863SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
15151c093863SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
15161c093863SMatthew G. Knepley     const char     *bdLabel;
15171c093863SMatthew G. Knepley     DMLabel         label;
15181c093863SMatthew G. Knepley     IS              pointIS;
15191c093863SMatthew G. Knepley     const PetscInt *points;
15201c093863SMatthew G. Knepley     const PetscInt *values;
15211c093863SMatthew G. Knepley     PetscReal      *n;
15221c093863SMatthew G. Knepley     PetscInt        field, numValues, numPoints, p, dep, numFaces;
15231c093863SMatthew G. Knepley     PetscBool       isEssential;
15241c093863SMatthew G. Knepley 
152563d5297fSMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
15260f2d7e86SMatthew G. Knepley     if (isEssential) continue;
15271c093863SMatthew G. Knepley     if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this");
15281c093863SMatthew G. Knepley     ierr = DMPlexGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
15291c093863SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr);
15301c093863SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
15311c093863SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
1532075da914SMatthew G. Knepley     for (p = 0, numFaces = 0; p < numPoints; ++p) {
1533075da914SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
1534075da914SMatthew G. Knepley       if (dep == dim-1) ++numFaces;
1535075da914SMatthew G. Knepley     }
15360f2d7e86SMatthew G. Knepley     ierr = PetscMalloc7(numFaces*totDimBd,&u,numFaces*dim,&v0,numFaces*dim,&n,numFaces*dim*dim,&J,numFaces*dim*dim,&invJ,numFaces,&detJ,numFaces*totDimBd*totDimBd,&elemMat);CHKERRQ(ierr);
15370f2d7e86SMatthew G. Knepley     if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);}
1538075da914SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
1539f1ea0e2fSMatthew G. Knepley       const PetscInt point = points[p];
1540f1ea0e2fSMatthew G. Knepley       PetscScalar   *x     = NULL;
1541f1ea0e2fSMatthew G. Knepley       PetscInt       i;
1542f1ea0e2fSMatthew G. Knepley 
1543075da914SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
1544075da914SMatthew G. Knepley       if (dep != dim-1) continue;
15458e0841e0SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, &v0[f*dim], &J[f*dim*dim], &invJ[f*dim*dim], &detJ[f]);CHKERRQ(ierr);
1546a8007bbfSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, &n[f*dim]);
1547075da914SMatthew G. Knepley       if (detJ[f] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", detJ[f], point);
1548f1ea0e2fSMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
15490f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i];
1550f1ea0e2fSMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
15510f2d7e86SMatthew G. Knepley       if (X_t) {
1552af1eca97SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
15530f2d7e86SMatthew G. Knepley         for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i];
1554af1eca97SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
15550f2d7e86SMatthew G. Knepley       }
1556af1eca97SMatthew G. Knepley       ++f;
1557af1eca97SMatthew G. Knepley     }
15580f2d7e86SMatthew G. Knepley     ierr = PetscMemzero(elemMat, numFaces*totDimBd*totDimBd * sizeof(PetscScalar));CHKERRQ(ierr);
15590f2d7e86SMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
15600f2d7e86SMatthew G. Knepley       PetscFE  fe;
1561af1eca97SMatthew G. Knepley       PetscInt numQuadPoints, Nb;
1562af1eca97SMatthew G. Knepley       /* Conforming batches */
1563af1eca97SMatthew G. Knepley       PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
1564af1eca97SMatthew G. Knepley       /* Remainder */
1565af1eca97SMatthew G. Knepley       PetscInt Nr, offset;
1566af1eca97SMatthew G. Knepley 
15672764a2aaSMatthew G. Knepley       ierr = PetscDSGetBdDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
15680f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
15690f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
15700f2d7e86SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
157121454ff5SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
157221454ff5SMatthew G. Knepley       blockSize = Nb*numQuadPoints;
15730483ade4SMatthew G. Knepley       batchSize = numBlocks * blockSize;
15740f2d7e86SMatthew G. Knepley       ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
15750f2d7e86SMatthew G. Knepley       numChunks = numFaces / (numBatches*batchSize);
15760483ade4SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
15770f2d7e86SMatthew G. Knepley       Nr        = numFaces % (numBatches*batchSize);
15780f2d7e86SMatthew G. Knepley       offset    = numFaces - Nr;
15790f2d7e86SMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
15800483ade4SMatthew G. Knepley         geom.v0   = v0;
15810f2d7e86SMatthew G. Knepley         geom.n    = n;
15820483ade4SMatthew G. Knepley         geom.J    = J;
15830483ade4SMatthew G. Knepley         geom.invJ = invJ;
15840483ade4SMatthew G. Knepley         geom.detJ = detJ;
15850f2d7e86SMatthew G. Knepley         ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Ne, geom, u, u_t, NULL, NULL, elemMat);CHKERRQ(ierr);
15860483ade4SMatthew G. Knepley         geom.v0   = &v0[offset*dim];
15870f2d7e86SMatthew G. Knepley         geom.n    = &n[offset*dim];
15880483ade4SMatthew G. Knepley         geom.J    = &J[offset*dim*dim];
15890483ade4SMatthew G. Knepley         geom.invJ = &invJ[offset*dim*dim];
15900483ade4SMatthew G. Knepley         geom.detJ = &detJ[offset];
15910f2d7e86SMatthew G. Knepley         ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Nr, geom, &u[offset*totDimBd], u_t ? &u_t[offset*totDimBd] : NULL, NULL, NULL, &elemMat[offset*totDimBd*totDimBd]);CHKERRQ(ierr);
1592cb1e1211SMatthew G Knepley       }
1593cb1e1211SMatthew G Knepley     }
15940f2d7e86SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
15950f2d7e86SMatthew G. Knepley       const PetscInt point = points[p];
1596cb1e1211SMatthew G Knepley 
15970f2d7e86SMatthew G. Knepley       ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr);
15980f2d7e86SMatthew G. Knepley       if (dep != dim-1) continue;
15990f2d7e86SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDimBd, totDimBd, &elemMat[f*totDimBd*totDimBd]);CHKERRQ(ierr);}
16000f2d7e86SMatthew G. Knepley       ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, point, &elemMat[f*totDimBd*totDimBd], ADD_VALUES);CHKERRQ(ierr);
16010f2d7e86SMatthew G. Knepley       ++f;
1602cb1e1211SMatthew G Knepley     }
16030f2d7e86SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
16040f2d7e86SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
16050f2d7e86SMatthew G. Knepley     ierr = PetscFree7(u,v0,n,J,invJ,detJ,elemMat);CHKERRQ(ierr);
16060f2d7e86SMatthew G. Knepley     if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);}
1607cb1e1211SMatthew G Knepley   }
16080f2d7e86SMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16090f2d7e86SMatthew G. Knepley   ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16100f2d7e86SMatthew G. Knepley   if (mesh->printFEM) {
16110f2d7e86SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
16120f2d7e86SMatthew G. Knepley     ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr);
16130f2d7e86SMatthew G. Knepley     ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
16140f2d7e86SMatthew G. Knepley   }
16150f2d7e86SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
16160f2d7e86SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr);
16170f2d7e86SMatthew G. Knepley   if (isShell) {
16180f2d7e86SMatthew G. Knepley     JacActionCtx *jctx;
16190f2d7e86SMatthew G. Knepley 
16200f2d7e86SMatthew G. Knepley     ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr);
16210f2d7e86SMatthew G. Knepley     ierr = VecCopy(X, jctx->u);CHKERRQ(ierr);
16220f2d7e86SMatthew G. Knepley   }
1623cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1624cb1e1211SMatthew G Knepley }
1625cb1e1211SMatthew G Knepley 
1626cb1e1211SMatthew G Knepley #undef __FUNCT__
16270f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeJacobianFEM"
1628cb1e1211SMatthew G Knepley /*@
16290f2d7e86SMatthew G. Knepley   DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user.
1630cb1e1211SMatthew G Knepley 
1631cb1e1211SMatthew G Knepley   Input Parameters:
1632cb1e1211SMatthew G Knepley + dm - The mesh
1633cb1e1211SMatthew G Knepley . X  - Local input vector
1634cb1e1211SMatthew G Knepley - user - The user context
1635cb1e1211SMatthew G Knepley 
1636cb1e1211SMatthew G Knepley   Output Parameter:
1637cb1e1211SMatthew G Knepley . Jac  - Jacobian matrix
1638cb1e1211SMatthew G Knepley 
1639cb1e1211SMatthew G Knepley   Note:
16408026896cSMatthew G. Knepley   The first member of the user context must be an FEMContext.
1641cb1e1211SMatthew G Knepley 
1642cb1e1211SMatthew G Knepley   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
1643cb1e1211SMatthew G Knepley   like a GPU, or vectorize on a multicore machine.
1644cb1e1211SMatthew G Knepley 
16450059ad2aSSatish Balay   Level: developer
16460059ad2aSSatish Balay 
1647cb1e1211SMatthew G Knepley .seealso: FormFunctionLocal()
1648878cb397SSatish Balay @*/
16490f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user)
1650cb1e1211SMatthew G Knepley {
1651cb1e1211SMatthew G Knepley   PetscErrorCode ierr;
1652cb1e1211SMatthew G Knepley 
1653cb1e1211SMatthew G Knepley   PetscFunctionBegin;
16540f2d7e86SMatthew G. Knepley   ierr = DMPlexComputeJacobianFEM_Internal(dm, X, NULL, Jac, JacP, user);CHKERRQ(ierr);
1655cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1656cb1e1211SMatthew G Knepley }
1657bceba477SMatthew G. Knepley 
1658d69c5d34SMatthew G. Knepley #undef __FUNCT__
1659d69c5d34SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInterpolatorFEM"
1660d69c5d34SMatthew G. Knepley /*@
1661d69c5d34SMatthew G. Knepley   DMPlexComputeInterpolatorFEM - Form the local portion of the interpolation matrix I from the coarse DM to the uniformly refined DM.
1662d69c5d34SMatthew G. Knepley 
1663d69c5d34SMatthew G. Knepley   Input Parameters:
1664d69c5d34SMatthew G. Knepley + dmf  - The fine mesh
1665d69c5d34SMatthew G. Knepley . dmc  - The coarse mesh
1666d69c5d34SMatthew G. Knepley - user - The user context
1667d69c5d34SMatthew G. Knepley 
1668d69c5d34SMatthew G. Knepley   Output Parameter:
1669934789fcSMatthew G. Knepley . In  - The interpolation matrix
1670d69c5d34SMatthew G. Knepley 
1671d69c5d34SMatthew G. Knepley   Note:
1672d69c5d34SMatthew G. Knepley   The first member of the user context must be an FEMContext.
1673d69c5d34SMatthew G. Knepley 
1674d69c5d34SMatthew G. Knepley   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
1675d69c5d34SMatthew G. Knepley   like a GPU, or vectorize on a multicore machine.
1676d69c5d34SMatthew G. Knepley 
1677d69c5d34SMatthew G. Knepley   Level: developer
1678d69c5d34SMatthew G. Knepley 
1679d69c5d34SMatthew G. Knepley .seealso: DMPlexComputeJacobianFEM()
1680d69c5d34SMatthew G. Knepley @*/
1681934789fcSMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorFEM(DM dmc, DM dmf, Mat In, void *user)
1682d69c5d34SMatthew G. Knepley {
1683d69c5d34SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dmc->data;
1684d69c5d34SMatthew G. Knepley   const char       *name  = "Interpolator";
16852764a2aaSMatthew G. Knepley   PetscDS           prob;
1686d69c5d34SMatthew G. Knepley   PetscFE          *feRef;
1687d69c5d34SMatthew G. Knepley   PetscSection      fsection, fglobalSection;
1688d69c5d34SMatthew G. Knepley   PetscSection      csection, cglobalSection;
1689d69c5d34SMatthew G. Knepley   PetscScalar      *elemMat;
1690942a7a06SMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, c;
16910f2d7e86SMatthew G. Knepley   PetscInt          cTotDim, rTotDim = 0;
1692d69c5d34SMatthew G. Knepley   PetscErrorCode    ierr;
1693d69c5d34SMatthew G. Knepley 
1694d69c5d34SMatthew G. Knepley   PetscFunctionBegin;
1695d69c5d34SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1696c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
1697d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);
1698d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
1699d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);
1700d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
1701d69c5d34SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
1702d69c5d34SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
17032764a2aaSMatthew G. Knepley   ierr = DMGetDS(dmf, &prob);CHKERRQ(ierr);
1704d69c5d34SMatthew G. Knepley   ierr = PetscMalloc1(Nf,&feRef);CHKERRQ(ierr);
1705d69c5d34SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
17060f2d7e86SMatthew G. Knepley     PetscFE  fe;
17070f2d7e86SMatthew G. Knepley     PetscInt rNb, Nc;
1708d69c5d34SMatthew G. Knepley 
17092764a2aaSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
17100f2d7e86SMatthew G. Knepley     ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
1711d69c5d34SMatthew G. Knepley     ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr);
17120f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
17130f2d7e86SMatthew G. Knepley     rTotDim += rNb*Nc;
1714d69c5d34SMatthew G. Knepley   }
17152764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
17160f2d7e86SMatthew G. Knepley   ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr);
17170f2d7e86SMatthew G. Knepley   ierr = PetscMemzero(elemMat, rTotDim*cTotDim * sizeof(PetscScalar));CHKERRQ(ierr);
1718d69c5d34SMatthew G. Knepley   for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) {
1719d69c5d34SMatthew G. Knepley     PetscDualSpace   Qref;
1720d69c5d34SMatthew G. Knepley     PetscQuadrature  f;
1721d69c5d34SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
1722d69c5d34SMatthew G. Knepley     PetscReal       *points;
1723d69c5d34SMatthew G. Knepley     PetscInt         npoints = 0, Nc, Np, fpdim, i, k, p, d;
1724d69c5d34SMatthew G. Knepley 
1725d69c5d34SMatthew G. Knepley     /* Compose points from all dual basis functionals */
1726d69c5d34SMatthew G. Knepley     ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr);
17270f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr);
1728d69c5d34SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr);
1729d69c5d34SMatthew G. Knepley     for (i = 0; i < fpdim; ++i) {
1730d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1731d69c5d34SMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, NULL);CHKERRQ(ierr);
1732d69c5d34SMatthew G. Knepley       npoints += Np;
1733d69c5d34SMatthew G. Knepley     }
1734d69c5d34SMatthew G. Knepley     ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr);
1735d69c5d34SMatthew G. Knepley     for (i = 0, k = 0; i < fpdim; ++i) {
1736d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1737d69c5d34SMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
1738d69c5d34SMatthew G. Knepley       for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d];
1739d69c5d34SMatthew G. Knepley     }
1740d69c5d34SMatthew G. Knepley 
1741d69c5d34SMatthew G. Knepley     for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) {
17420f2d7e86SMatthew G. Knepley       PetscFE    fe;
1743d69c5d34SMatthew G. Knepley       PetscReal *B;
174436a6d9c0SMatthew G. Knepley       PetscInt   NcJ, cpdim, j;
1745d69c5d34SMatthew G. Knepley 
1746d69c5d34SMatthew G. Knepley       /* Evaluate basis at points */
17472764a2aaSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldJ, (PetscObject *) &fe);CHKERRQ(ierr);
17480f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr);
17490f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
1750ffe73a53SMatthew G. Knepley       /* For now, fields only interpolate themselves */
1751ffe73a53SMatthew G. Knepley       if (fieldI == fieldJ) {
17527c927364SMatthew G. Knepley         if (Nc != NcJ) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in fine space field %d does not match coarse field %d", Nc, NcJ);
17530f2d7e86SMatthew G. Knepley         ierr = PetscFEGetTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);
1754d69c5d34SMatthew G. Knepley         for (i = 0, k = 0; i < fpdim; ++i) {
1755d69c5d34SMatthew G. Knepley           ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1756d69c5d34SMatthew G. Knepley           ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, &qweights);CHKERRQ(ierr);
1757d69c5d34SMatthew G. Knepley           for (p = 0; p < Np; ++p, ++k) {
175836a6d9c0SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
17590f2d7e86SMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[(offsetI + i*Nc + c)*cTotDim + offsetJ + j*NcJ + c] += B[k*cpdim*NcJ+j*Nc+c]*qweights[p];
176036a6d9c0SMatthew G. Knepley             }
1761d69c5d34SMatthew G. Knepley           }
1762d69c5d34SMatthew G. Knepley         }
17630f2d7e86SMatthew G. Knepley         ierr = PetscFERestoreTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
1764ffe73a53SMatthew G. Knepley       }
176536a6d9c0SMatthew G. Knepley       offsetJ += cpdim*NcJ;
1766d69c5d34SMatthew G. Knepley     }
1767d69c5d34SMatthew G. Knepley     offsetI += fpdim*Nc;
1768549a8adaSMatthew G. Knepley     ierr = PetscFree(points);CHKERRQ(ierr);
1769d69c5d34SMatthew G. Knepley   }
17700f2d7e86SMatthew G. Knepley   if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);}
17717f5b169aSMatthew G. Knepley   /* Preallocate matrix */
17727f5b169aSMatthew G. Knepley   {
17737f5b169aSMatthew G. Knepley     PetscHashJK ht;
17747f5b169aSMatthew G. Knepley     PetscLayout rLayout;
17757f5b169aSMatthew G. Knepley     PetscInt   *dnz, *onz, *cellCIndices, *cellFIndices;
17767f5b169aSMatthew G. Knepley     PetscInt    locRows, rStart, rEnd, cell, r;
17777f5b169aSMatthew G. Knepley 
17787f5b169aSMatthew G. Knepley     ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr);
17797f5b169aSMatthew G. Knepley     ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr);
17807f5b169aSMatthew G. Knepley     ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
17817f5b169aSMatthew G. Knepley     ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
17827f5b169aSMatthew G. Knepley     ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
17837f5b169aSMatthew G. Knepley     ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
17847f5b169aSMatthew G. Knepley     ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
17857f5b169aSMatthew G. Knepley     ierr = PetscCalloc4(locRows,&dnz,locRows,&onz,cTotDim,&cellCIndices,rTotDim,&cellFIndices);CHKERRQ(ierr);
17867f5b169aSMatthew G. Knepley     ierr = PetscHashJKCreate(&ht);CHKERRQ(ierr);
17877f5b169aSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
17887f5b169aSMatthew G. Knepley       ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, cell, cellCIndices, cellFIndices);CHKERRQ(ierr);
17897f5b169aSMatthew G. Knepley       for (r = 0; r < rTotDim; ++r) {
17907f5b169aSMatthew G. Knepley         PetscHashJKKey  key;
17917f5b169aSMatthew G. Knepley         PetscHashJKIter missing, iter;
17927f5b169aSMatthew G. Knepley 
17937f5b169aSMatthew G. Knepley         key.j = cellFIndices[r];
17947f5b169aSMatthew G. Knepley         if (key.j < 0) continue;
17957f5b169aSMatthew G. Knepley         for (c = 0; c < cTotDim; ++c) {
17967f5b169aSMatthew G. Knepley           key.k = cellCIndices[c];
17977f5b169aSMatthew G. Knepley           if (key.k < 0) continue;
17987f5b169aSMatthew G. Knepley           ierr = PetscHashJKPut(ht, key, &missing, &iter);CHKERRQ(ierr);
17997f5b169aSMatthew G. Knepley           if (missing) {
18007f5b169aSMatthew G. Knepley             ierr = PetscHashJKSet(ht, iter, 1);CHKERRQ(ierr);
18017f5b169aSMatthew G. Knepley             if ((key.k >= rStart) && (key.k < rEnd)) ++dnz[key.j-rStart];
18027f5b169aSMatthew G. Knepley             else                                     ++onz[key.j-rStart];
18037f5b169aSMatthew G. Knepley           }
18047f5b169aSMatthew G. Knepley         }
18057f5b169aSMatthew G. Knepley       }
18067f5b169aSMatthew G. Knepley     }
18077f5b169aSMatthew G. Knepley     ierr = PetscHashJKDestroy(&ht);CHKERRQ(ierr);
18087f5b169aSMatthew G. Knepley     ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
18097f5b169aSMatthew G. Knepley     ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
18107f5b169aSMatthew G. Knepley     ierr = PetscFree4(dnz,onz,cellCIndices,cellFIndices);CHKERRQ(ierr);
18117f5b169aSMatthew G. Knepley   }
18127f5b169aSMatthew G. Knepley   /* Fill matrix */
18137f5b169aSMatthew G. Knepley   ierr = MatZeroEntries(In);CHKERRQ(ierr);
1814d69c5d34SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1815934789fcSMatthew G. Knepley     ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr);
1816d69c5d34SMatthew G. Knepley   }
1817549a8adaSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);}
1818d69c5d34SMatthew G. Knepley   ierr = PetscFree(feRef);CHKERRQ(ierr);
1819549a8adaSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
1820934789fcSMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1821934789fcSMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1822d69c5d34SMatthew G. Knepley   if (mesh->printFEM) {
1823d69c5d34SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
1824934789fcSMatthew G. Knepley     ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr);
1825934789fcSMatthew G. Knepley     ierr = MatView(In, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1826d69c5d34SMatthew G. Knepley   }
1827d69c5d34SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1828d69c5d34SMatthew G. Knepley   PetscFunctionReturn(0);
1829d69c5d34SMatthew G. Knepley }
18306c73c22cSMatthew G. Knepley 
18316c73c22cSMatthew G. Knepley #undef __FUNCT__
18327c927364SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInjectorFEM"
18337c927364SMatthew G. Knepley PetscErrorCode DMPlexComputeInjectorFEM(DM dmc, DM dmf, VecScatter *sc, void *user)
18347c927364SMatthew G. Knepley {
1835e9d4ef1bSMatthew G. Knepley   PetscDS        prob;
18367c927364SMatthew G. Knepley   PetscFE       *feRef;
18377c927364SMatthew G. Knepley   Vec            fv, cv;
18387c927364SMatthew G. Knepley   IS             fis, cis;
18397c927364SMatthew G. Knepley   PetscSection   fsection, fglobalSection, csection, cglobalSection;
18407c927364SMatthew G. Knepley   PetscInt      *cmap, *cellCIndices, *cellFIndices, *cindices, *findices;
18417c927364SMatthew G. Knepley   PetscInt       cTotDim, fTotDim = 0, Nf, f, field, cStart, cEnd, c, dim, d, startC, offsetC, offsetF, m;
18427c927364SMatthew G. Knepley   PetscErrorCode ierr;
18437c927364SMatthew G. Knepley 
18447c927364SMatthew G. Knepley   PetscFunctionBegin;
184575a69067SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1846c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
18477c927364SMatthew G. Knepley   ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);
18487c927364SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
18497c927364SMatthew G. Knepley   ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);
18507c927364SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
18517c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
18527c927364SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
1853e9d4ef1bSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
18547c927364SMatthew G. Knepley   ierr = PetscMalloc1(Nf,&feRef);CHKERRQ(ierr);
18557c927364SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
18567c927364SMatthew G. Knepley     PetscFE  fe;
18577c927364SMatthew G. Knepley     PetscInt fNb, Nc;
18587c927364SMatthew G. Knepley 
1859e9d4ef1bSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
18607c927364SMatthew G. Knepley     ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
18617c927364SMatthew G. Knepley     ierr = PetscFEGetDimension(feRef[f], &fNb);CHKERRQ(ierr);
18627c927364SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
18637c927364SMatthew G. Knepley     fTotDim += fNb*Nc;
18647c927364SMatthew G. Knepley   }
1865e9d4ef1bSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
18667c927364SMatthew G. Knepley   ierr = PetscMalloc1(cTotDim,&cmap);CHKERRQ(ierr);
18677c927364SMatthew G. Knepley   for (field = 0, offsetC = 0, offsetF = 0; field < Nf; ++field) {
18687c927364SMatthew G. Knepley     PetscFE        feC;
18697c927364SMatthew G. Knepley     PetscDualSpace QF, QC;
18707c927364SMatthew G. Knepley     PetscInt       NcF, NcC, fpdim, cpdim;
18717c927364SMatthew G. Knepley 
1872e9d4ef1bSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &feC);CHKERRQ(ierr);
18737c927364SMatthew G. Knepley     ierr = PetscFEGetNumComponents(feC, &NcC);CHKERRQ(ierr);
18747c927364SMatthew G. Knepley     ierr = PetscFEGetNumComponents(feRef[field], &NcF);CHKERRQ(ierr);
18757c927364SMatthew G. Knepley     if (NcF != NcC) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in fine space field %d does not match coarse field %d", NcF, NcC);
18767c927364SMatthew G. Knepley     ierr = PetscFEGetDualSpace(feRef[field], &QF);CHKERRQ(ierr);
18777c927364SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
18787c927364SMatthew G. Knepley     ierr = PetscFEGetDualSpace(feC, &QC);CHKERRQ(ierr);
18797c927364SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
18807c927364SMatthew G. Knepley     for (c = 0; c < cpdim; ++c) {
18817c927364SMatthew G. Knepley       PetscQuadrature  cfunc;
18827c927364SMatthew G. Knepley       const PetscReal *cqpoints;
18837c927364SMatthew G. Knepley       PetscInt         NpC;
18847c927364SMatthew G. Knepley 
18857c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(QC, c, &cfunc);CHKERRQ(ierr);
18867c927364SMatthew G. Knepley       ierr = PetscQuadratureGetData(cfunc, NULL, &NpC, &cqpoints, NULL);CHKERRQ(ierr);
18877c927364SMatthew G. Knepley       if (NpC != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not know how to do injection for moments");
18887c927364SMatthew G. Knepley       for (f = 0; f < fpdim; ++f) {
18897c927364SMatthew G. Knepley         PetscQuadrature  ffunc;
18907c927364SMatthew G. Knepley         const PetscReal *fqpoints;
18917c927364SMatthew G. Knepley         PetscReal        sum = 0.0;
18927c927364SMatthew G. Knepley         PetscInt         NpF, comp;
18937c927364SMatthew G. Knepley 
18947c927364SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(QF, f, &ffunc);CHKERRQ(ierr);
18957c927364SMatthew G. Knepley         ierr = PetscQuadratureGetData(ffunc, NULL, &NpF, &fqpoints, NULL);CHKERRQ(ierr);
18967c927364SMatthew G. Knepley         if (NpC != NpF) continue;
18977c927364SMatthew G. Knepley         for (d = 0; d < dim; ++d) sum += PetscAbsReal(cqpoints[d] - fqpoints[d]);
18987c927364SMatthew G. Knepley         if (sum > 1.0e-9) continue;
18997c927364SMatthew G. Knepley         for (comp = 0; comp < NcC; ++comp) {
19007c927364SMatthew G. Knepley           cmap[(offsetC+c)*NcC+comp] = (offsetF+f)*NcF+comp;
19017c927364SMatthew G. Knepley         }
19027c927364SMatthew G. Knepley         break;
19037c927364SMatthew G. Knepley       }
19047c927364SMatthew G. Knepley     }
19057c927364SMatthew G. Knepley     offsetC += cpdim*NcC;
19067c927364SMatthew G. Knepley     offsetF += fpdim*NcF;
19077c927364SMatthew G. Knepley   }
19087c927364SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);}
19097c927364SMatthew G. Knepley   ierr = PetscFree(feRef);CHKERRQ(ierr);
19107c927364SMatthew G. Knepley 
19117c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmf, &fv);CHKERRQ(ierr);
19127c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmc, &cv);CHKERRQ(ierr);
19137c927364SMatthew G. Knepley   ierr = VecGetOwnershipRange(cv, &startC, NULL);CHKERRQ(ierr);
19147c927364SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(cglobalSection, &m);CHKERRQ(ierr);
19157c927364SMatthew G. Knepley   ierr = PetscMalloc2(cTotDim,&cellCIndices,fTotDim,&cellFIndices);CHKERRQ(ierr);
1916aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&cindices);CHKERRQ(ierr);
1917aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&findices);CHKERRQ(ierr);
19187c927364SMatthew G. Knepley   for (d = 0; d < m; ++d) cindices[d] = findices[d] = -1;
19197c927364SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
19207c927364SMatthew G. Knepley     ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, c, cellCIndices, cellFIndices);CHKERRQ(ierr);
19217c927364SMatthew G. Knepley     for (d = 0; d < cTotDim; ++d) {
19227c927364SMatthew G. Knepley       if (cellCIndices[d] < 0) continue;
19237c927364SMatthew G. Knepley       if ((findices[cellCIndices[d]-startC] >= 0) && (findices[cellCIndices[d]-startC] != cellFIndices[cmap[d]])) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Coarse dof %d maps to both %d and %d", cindices[cellCIndices[d]-startC], findices[cellCIndices[d]-startC], cellFIndices[cmap[d]]);
19247c927364SMatthew G. Knepley       cindices[cellCIndices[d]-startC] = cellCIndices[d];
19257c927364SMatthew G. Knepley       findices[cellCIndices[d]-startC] = cellFIndices[cmap[d]];
19267c927364SMatthew G. Knepley     }
19277c927364SMatthew G. Knepley   }
19287c927364SMatthew G. Knepley   ierr = PetscFree(cmap);CHKERRQ(ierr);
19297c927364SMatthew G. Knepley   ierr = PetscFree2(cellCIndices,cellFIndices);CHKERRQ(ierr);
19307c927364SMatthew G. Knepley 
19317c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, cindices, PETSC_OWN_POINTER, &cis);CHKERRQ(ierr);
19327c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, findices, PETSC_OWN_POINTER, &fis);CHKERRQ(ierr);
19337c927364SMatthew G. Knepley   ierr = VecScatterCreate(cv, cis, fv, fis, sc);CHKERRQ(ierr);
19347c927364SMatthew G. Knepley   ierr = ISDestroy(&cis);CHKERRQ(ierr);
19357c927364SMatthew G. Knepley   ierr = ISDestroy(&fis);CHKERRQ(ierr);
19367c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmf, &fv);CHKERRQ(ierr);
19377c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmc, &cv);CHKERRQ(ierr);
193875a69067SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1939cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1940cb1e1211SMatthew G Knepley }
1941cb1e1211SMatthew G Knepley 
1942cb1e1211SMatthew G Knepley #undef __FUNCT__
19438e136ac0SMatthew G. Knepley #define __FUNCT__ "BoundaryDuplicate"
19448e136ac0SMatthew G. Knepley static PetscErrorCode BoundaryDuplicate(DMBoundary bd, DMBoundary *boundary)
19458e136ac0SMatthew G. Knepley {
19468e136ac0SMatthew G. Knepley   DMBoundary     b = bd, b2, bold = NULL;
19478e136ac0SMatthew G. Knepley   PetscErrorCode ierr;
19488e136ac0SMatthew G. Knepley 
19498e136ac0SMatthew G. Knepley   PetscFunctionBegin;
19508e136ac0SMatthew G. Knepley   *boundary = NULL;
19518e136ac0SMatthew G. Knepley   for (; b; b = b->next, bold = b2) {
19528e136ac0SMatthew G. Knepley     ierr = PetscNew(&b2);CHKERRQ(ierr);
19538e136ac0SMatthew G. Knepley     ierr = PetscStrallocpy(b->name, (char **) &b2->name);CHKERRQ(ierr);
19548e136ac0SMatthew G. Knepley     ierr = PetscStrallocpy(b->labelname, (char **) &b2->labelname);CHKERRQ(ierr);
19558e136ac0SMatthew G. Knepley     ierr = PetscMalloc1(b->numids, &b2->ids);CHKERRQ(ierr);
19568e136ac0SMatthew G. Knepley     ierr = PetscMemcpy(b2->ids, b->ids, b->numids*sizeof(PetscInt));CHKERRQ(ierr);
19578e136ac0SMatthew G. Knepley     b2->label     = NULL;
19588e136ac0SMatthew G. Knepley     b2->essential = b->essential;
19598e136ac0SMatthew G. Knepley     b2->field     = b->field;
19608e136ac0SMatthew G. Knepley     b2->func      = b->func;
19618e136ac0SMatthew G. Knepley     b2->numids    = b->numids;
19628e136ac0SMatthew G. Knepley     b2->ctx       = b->ctx;
19638e136ac0SMatthew G. Knepley     b2->next      = NULL;
19648e136ac0SMatthew G. Knepley     if (!*boundary) *boundary   = b2;
19658e136ac0SMatthew G. Knepley     if (bold)        bold->next = b2;
19668e136ac0SMatthew G. Knepley   }
19678e136ac0SMatthew G. Knepley   PetscFunctionReturn(0);
19688e136ac0SMatthew G. Knepley }
19698e136ac0SMatthew G. Knepley 
19708e136ac0SMatthew G. Knepley #undef __FUNCT__
19718e136ac0SMatthew G. Knepley #define __FUNCT__ "DMPlexCopyBoundary"
19728e136ac0SMatthew G. Knepley PetscErrorCode DMPlexCopyBoundary(DM dm, DM dmNew)
19738e136ac0SMatthew G. Knepley {
19748e136ac0SMatthew G. Knepley   DM_Plex       *mesh    = (DM_Plex *) dm->data;
19758e136ac0SMatthew G. Knepley   DM_Plex       *meshNew = (DM_Plex *) dmNew->data;
19768e136ac0SMatthew G. Knepley   DMBoundary     b;
19778e136ac0SMatthew G. Knepley   PetscErrorCode ierr;
19788e136ac0SMatthew G. Knepley 
19798e136ac0SMatthew G. Knepley   PetscFunctionBegin;
19808e136ac0SMatthew G. Knepley   ierr = BoundaryDuplicate(mesh->boundary, &meshNew->boundary);CHKERRQ(ierr);
19818e136ac0SMatthew G. Knepley   for (b = meshNew->boundary; b; b = b->next) {
19828e136ac0SMatthew G. Knepley     if (b->labelname) {
19838e136ac0SMatthew G. Knepley       ierr = DMPlexGetLabel(dmNew, b->labelname, &b->label);CHKERRQ(ierr);
19848e136ac0SMatthew G. Knepley       if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s does not exist in this DM", b->labelname);
19858e136ac0SMatthew G. Knepley     }
19868e136ac0SMatthew G. Knepley   }
19878e136ac0SMatthew G. Knepley   PetscFunctionReturn(0);
19888e136ac0SMatthew G. Knepley }
19898e136ac0SMatthew G. Knepley 
19908e136ac0SMatthew G. Knepley #undef __FUNCT__
1991cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexAddBoundary"
1992cb1e1211SMatthew G Knepley /* The ids can be overridden by the command line option -bc_<boundary name> */
199363d5297fSMatthew G. Knepley PetscErrorCode DMPlexAddBoundary(DM dm, PetscBool isEssential, const char name[], const char labelname[], PetscInt field, void (*bcFunc)(), PetscInt numids, const PetscInt *ids, void *ctx)
1994cb1e1211SMatthew G Knepley {
1995cb1e1211SMatthew G Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
1996cb1e1211SMatthew G Knepley   DMBoundary     b;
1997cb1e1211SMatthew G Knepley   PetscErrorCode ierr;
1998cb1e1211SMatthew G Knepley 
1999cb1e1211SMatthew G Knepley   PetscFunctionBegin;
200063d5297fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2001cb1e1211SMatthew G Knepley   ierr = PetscNew(&b);CHKERRQ(ierr);
2002cb1e1211SMatthew G Knepley   ierr = PetscStrallocpy(name, (char **) &b->name);CHKERRQ(ierr);
200363d5297fSMatthew G. Knepley   ierr = PetscStrallocpy(labelname, (char **) &b->labelname);CHKERRQ(ierr);
2004cb1e1211SMatthew G Knepley   ierr = PetscMalloc1(numids, &b->ids);CHKERRQ(ierr);
2005cb1e1211SMatthew G Knepley   ierr = PetscMemcpy(b->ids, ids, numids*sizeof(PetscInt));CHKERRQ(ierr);
200663d5297fSMatthew G. Knepley   if (b->labelname) {
200763d5297fSMatthew G. Knepley     ierr = DMPlexGetLabel(dm, b->labelname, &b->label);CHKERRQ(ierr);
200863d5297fSMatthew G. Knepley     if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s does not exist in this DM", b->labelname);
200963d5297fSMatthew G. Knepley   }
2010cb1e1211SMatthew G Knepley   b->essential   = isEssential;
2011cb1e1211SMatthew G Knepley   b->field       = field;
2012cb1e1211SMatthew G Knepley   b->func        = bcFunc;
2013cb1e1211SMatthew G Knepley   b->numids      = numids;
2014cb1e1211SMatthew G Knepley   b->ctx         = ctx;
2015cb1e1211SMatthew G Knepley   b->next        = mesh->boundary;
2016cb1e1211SMatthew G Knepley   mesh->boundary = b;
2017cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
2018cb1e1211SMatthew G Knepley }
2019cb1e1211SMatthew G Knepley 
2020cb1e1211SMatthew G Knepley #undef __FUNCT__
2021cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexGetNumBoundary"
2022cb1e1211SMatthew G Knepley PetscErrorCode DMPlexGetNumBoundary(DM dm, PetscInt *numBd)
2023cb1e1211SMatthew G Knepley {
2024cb1e1211SMatthew G Knepley   DM_Plex   *mesh = (DM_Plex *) dm->data;
2025cb1e1211SMatthew G Knepley   DMBoundary b    = mesh->boundary;
2026cb1e1211SMatthew G Knepley 
2027cb1e1211SMatthew G Knepley   PetscFunctionBegin;
202863d5297fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
202963d5297fSMatthew G. Knepley   PetscValidPointer(numBd, 2);
2030cb1e1211SMatthew G Knepley   *numBd = 0;
2031cb1e1211SMatthew G Knepley   while (b) {++(*numBd); b = b->next;}
2032cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
2033cb1e1211SMatthew G Knepley }
2034cb1e1211SMatthew G Knepley 
2035cb1e1211SMatthew G Knepley #undef __FUNCT__
2036cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexGetBoundary"
203763d5297fSMatthew G. Knepley PetscErrorCode DMPlexGetBoundary(DM dm, PetscInt bd, PetscBool *isEssential, const char **name, const char **labelname, PetscInt *field, void (**func)(), PetscInt *numids, const PetscInt **ids, void **ctx)
2038cb1e1211SMatthew G Knepley {
2039cb1e1211SMatthew G Knepley   DM_Plex   *mesh = (DM_Plex *) dm->data;
2040cb1e1211SMatthew G Knepley   DMBoundary b    = mesh->boundary;
2041cb1e1211SMatthew G Knepley   PetscInt   n    = 0;
2042cb1e1211SMatthew G Knepley 
2043cb1e1211SMatthew G Knepley   PetscFunctionBegin;
204463d5297fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2045cb1e1211SMatthew G Knepley   while (b) {
2046cb1e1211SMatthew G Knepley     if (n == bd) break;
2047cb1e1211SMatthew G Knepley     b = b->next;
2048cb1e1211SMatthew G Knepley     ++n;
2049cb1e1211SMatthew G Knepley   }
2050cb1e1211SMatthew G Knepley   if (n != bd) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Boundary %d is not in [0, %d)", bd, n);
2051cb1e1211SMatthew G Knepley   if (isEssential) {
2052cb1e1211SMatthew G Knepley     PetscValidPointer(isEssential, 3);
2053cb1e1211SMatthew G Knepley     *isEssential = b->essential;
2054cb1e1211SMatthew G Knepley   }
2055cb1e1211SMatthew G Knepley   if (name) {
2056cb1e1211SMatthew G Knepley     PetscValidPointer(name, 4);
2057cb1e1211SMatthew G Knepley     *name = b->name;
2058cb1e1211SMatthew G Knepley   }
205963d5297fSMatthew G. Knepley   if (labelname) {
206063d5297fSMatthew G. Knepley     PetscValidPointer(labelname, 5);
206163d5297fSMatthew G. Knepley     *labelname = b->labelname;
206263d5297fSMatthew G. Knepley   }
2063cb1e1211SMatthew G Knepley   if (field) {
206463d5297fSMatthew G. Knepley     PetscValidPointer(field, 6);
2065cb1e1211SMatthew G Knepley     *field = b->field;
2066cb1e1211SMatthew G Knepley   }
2067cb1e1211SMatthew G Knepley   if (func) {
206863d5297fSMatthew G. Knepley     PetscValidPointer(func, 7);
2069cb1e1211SMatthew G Knepley     *func = b->func;
2070cb1e1211SMatthew G Knepley   }
2071cb1e1211SMatthew G Knepley   if (numids) {
207263d5297fSMatthew G. Knepley     PetscValidPointer(numids, 8);
2073cb1e1211SMatthew G Knepley     *numids = b->numids;
2074cb1e1211SMatthew G Knepley   }
2075cb1e1211SMatthew G Knepley   if (ids) {
207663d5297fSMatthew G. Knepley     PetscValidPointer(ids, 9);
2077cb1e1211SMatthew G Knepley     *ids = b->ids;
2078cb1e1211SMatthew G Knepley   }
2079cb1e1211SMatthew G Knepley   if (ctx) {
208063d5297fSMatthew G. Knepley     PetscValidPointer(ctx, 10);
2081cb1e1211SMatthew G Knepley     *ctx = b->ctx;
2082cb1e1211SMatthew G Knepley   }
2083cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
2084cb1e1211SMatthew G Knepley }
20850225b034SMatthew G. Knepley 
20860225b034SMatthew G. Knepley #undef __FUNCT__
20870225b034SMatthew G. Knepley #define __FUNCT__ "DMPlexIsBoundaryPoint"
20880225b034SMatthew G. Knepley PetscErrorCode DMPlexIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
20890225b034SMatthew G. Knepley {
20900225b034SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
20910225b034SMatthew G. Knepley   DMBoundary     b    = mesh->boundary;
20920225b034SMatthew G. Knepley   PetscErrorCode ierr;
20930225b034SMatthew G. Knepley 
20940225b034SMatthew G. Knepley   PetscFunctionBegin;
20950225b034SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20960225b034SMatthew G. Knepley   PetscValidPointer(isBd, 3);
20970225b034SMatthew G. Knepley   *isBd = PETSC_FALSE;
20980225b034SMatthew G. Knepley   while (b && !(*isBd)) {
20990225b034SMatthew G. Knepley     if (b->label) {
21000225b034SMatthew G. Knepley       PetscInt i;
21010225b034SMatthew G. Knepley 
21020225b034SMatthew G. Knepley       for (i = 0; i < b->numids && !(*isBd); ++i) {
21030225b034SMatthew G. Knepley         ierr = DMLabelStratumHasPoint(b->label, b->ids[i], point, isBd);CHKERRQ(ierr);
21040225b034SMatthew G. Knepley       }
21050225b034SMatthew G. Knepley     }
21060225b034SMatthew G. Knepley     b = b->next;
21070225b034SMatthew G. Knepley   }
21080225b034SMatthew G. Knepley   PetscFunctionReturn(0);
21090225b034SMatthew G. Knepley }
2110