xref: /petsc/src/dm/dt/dualspace/impls/lagrange/dspacelagrange.c (revision 1f440fbe05c1c9541782cc07bb8c30cd44409942)
1 #include <petsc/private/petscfeimpl.h> /*I "petscfe.h" I*/
2 #include <petscdmplex.h>
3 #include <petscblaslapack.h>
4 
5 PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM, PetscInt, PetscInt, PetscBool, PetscInt *, PetscInt *[]);
6 
7 struct _n_Petsc1DNodeFamily
8 {
9   PetscInt         refct;
10   PetscDTNodeType  nodeFamily;
11   PetscReal        gaussJacobiExp;
12   PetscInt         nComputed;
13   PetscReal      **nodesets;
14   PetscBool        endpoints;
15 };
16 
17 /* users set node families for PETSCDUALSPACELAGRANGE with just the inputs to this function, but internally we create
18  * an object that can cache the computations across multiple dual spaces */
19 static PetscErrorCode Petsc1DNodeFamilyCreate(PetscDTNodeType family, PetscReal gaussJacobiExp, PetscBool endpoints, Petsc1DNodeFamily *nf)
20 {
21   Petsc1DNodeFamily f;
22   PetscErrorCode ierr;
23 
24   PetscFunctionBegin;
25   ierr = PetscNew(&f);CHKERRQ(ierr);
26   switch (family) {
27   case PETSCDTNODES_GAUSSJACOBI:
28   case PETSCDTNODES_EQUISPACED:
29     f->nodeFamily = family;
30     break;
31   default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unknown 1D node family");
32   }
33   f->endpoints = endpoints;
34   f->gaussJacobiExp = 0.;
35   if (family == PETSCDTNODES_GAUSSJACOBI) {
36     if (gaussJacobiExp <= -1.) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Gauss-Jacobi exponent must be > -1.\n");
37     f->gaussJacobiExp = gaussJacobiExp;
38   }
39   f->refct = 1;
40   *nf = f;
41   PetscFunctionReturn(0);
42 }
43 
44 static PetscErrorCode Petsc1DNodeFamilyReference(Petsc1DNodeFamily nf)
45 {
46   PetscFunctionBegin;
47   if (nf) nf->refct++;
48   PetscFunctionReturn(0);
49 }
50 
51 static PetscErrorCode Petsc1DNodeFamilyDestroy(Petsc1DNodeFamily *nf) {
52   PetscInt       i, nc;
53   PetscErrorCode ierr;
54 
55   PetscFunctionBegin;
56   if (!(*nf)) PetscFunctionReturn(0);
57   if (--(*nf)->refct > 0) {
58     *nf = NULL;
59     PetscFunctionReturn(0);
60   }
61   nc = (*nf)->nComputed;
62   for (i = 0; i < nc; i++) {
63     ierr = PetscFree((*nf)->nodesets[i]);CHKERRQ(ierr);
64   }
65   ierr = PetscFree((*nf)->nodesets);CHKERRQ(ierr);
66   ierr = PetscFree(*nf);CHKERRQ(ierr);
67   *nf = NULL;
68   PetscFunctionReturn(0);
69 }
70 
71 static PetscErrorCode Petsc1DNodeFamilyGetNodeSets(Petsc1DNodeFamily f, PetscInt degree, PetscReal ***nodesets)
72 {
73   PetscInt       nc;
74   PetscErrorCode ierr;
75 
76   PetscFunctionBegin;
77   nc = f->nComputed;
78   if (degree >= nc) {
79     PetscInt    i, j;
80     PetscReal **new_nodesets;
81     PetscReal  *w;
82 
83     ierr = PetscMalloc1(degree + 1, &new_nodesets);CHKERRQ(ierr);
84     ierr = PetscArraycpy(new_nodesets, f->nodesets, nc);CHKERRQ(ierr);
85     ierr = PetscFree(f->nodesets);CHKERRQ(ierr);
86     f->nodesets = new_nodesets;
87     ierr = PetscMalloc1(degree + 1, &w);CHKERRQ(ierr);
88     for (i = nc; i < degree + 1; i++) {
89       ierr = PetscMalloc1(i + 1, &(f->nodesets[i]));CHKERRQ(ierr);
90       if (!i) {
91         f->nodesets[i][0] = 0.5;
92       } else {
93         switch (f->nodeFamily) {
94         case PETSCDTNODES_EQUISPACED:
95           if (f->endpoints) {
96             for (j = 0; j <= i; j++) f->nodesets[i][j] = (PetscReal) j / (PetscReal) i;
97           } else {
98             /* these nodes are at the centroids of the small simplices created by the equispaced nodes that include
99              * the endpoints */
100             for (j = 0; j <= i; j++) f->nodesets[i][j] = ((PetscReal) j + 0.5) / ((PetscReal) i + 1.);
101           }
102           break;
103         case PETSCDTNODES_GAUSSJACOBI:
104           if (f->endpoints) {
105             ierr = PetscDTGaussLobattoJacobiQuadrature(i + 1, 0., 1., f->gaussJacobiExp, f->gaussJacobiExp, f->nodesets[i], w);CHKERRQ(ierr);
106           } else {
107             ierr = PetscDTGaussJacobiQuadrature(i + 1, 0., 1., f->gaussJacobiExp, f->gaussJacobiExp, f->nodesets[i], w);CHKERRQ(ierr);
108           }
109           break;
110         default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unknown 1D node family");
111         }
112       }
113     }
114     ierr = PetscFree(w);CHKERRQ(ierr);
115     f->nComputed = degree + 1;
116   }
117   *nodesets = f->nodesets;
118   PetscFunctionReturn(0);
119 }
120 
121 /* http://arxiv.org/abs/2002.09421 for details */
122 static PetscErrorCode PetscNodeRecursive_Internal(PetscInt dim, PetscInt degree, PetscReal **nodesets, PetscInt tup[], PetscReal node[])
123 {
124   PetscReal w;
125   PetscInt i, j;
126   PetscErrorCode ierr;
127 
128   PetscFunctionBeginHot;
129   w = 0.;
130   if (dim == 1) {
131     node[0] = nodesets[degree][tup[0]];
132     node[1] = nodesets[degree][tup[1]];
133   } else {
134     for (i = 0; i < dim + 1; i++) node[i] = 0.;
135     for (i = 0; i < dim + 1; i++) {
136       PetscReal wi = nodesets[degree][degree-tup[i]];
137 
138       for (j = 0; j < dim+1; j++) tup[dim+1+j] = tup[j+(j>=i)];
139       ierr = PetscNodeRecursive_Internal(dim-1,degree-tup[i],nodesets,&tup[dim+1],&node[dim+1]);CHKERRQ(ierr);
140       for (j = 0; j < dim+1; j++) node[j+(j>=i)] += wi * node[dim+1+j];
141       w += wi;
142     }
143     for (i = 0; i < dim+1; i++) node[i] /= w;
144   }
145   PetscFunctionReturn(0);
146 }
147 
148 /* compute simplex nodes for the biunit simplex from the 1D node family */
149 static PetscErrorCode Petsc1DNodeFamilyComputeSimplexNodes(Petsc1DNodeFamily f, PetscInt dim, PetscInt degree, PetscReal points[])
150 {
151   PetscInt      *tup;
152   PetscInt       k;
153   PetscInt       npoints;
154   PetscReal    **nodesets = NULL;
155   PetscInt       worksize;
156   PetscReal     *nodework;
157   PetscInt      *tupwork;
158   PetscErrorCode ierr;
159 
160   PetscFunctionBegin;
161   if (dim < 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must have non-negative dimension\n");
162   if (degree < 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must have non-negative degree\n");
163   if (!dim) PetscFunctionReturn(0);
164   ierr = PetscCalloc1(dim+2, &tup);CHKERRQ(ierr);
165   k = 0;
166   ierr = PetscDTBinomialInt(degree + dim, dim, &npoints);CHKERRQ(ierr);
167   ierr = Petsc1DNodeFamilyGetNodeSets(f, degree, &nodesets);CHKERRQ(ierr);
168   worksize = ((dim + 2) * (dim + 3)) / 2;
169   ierr = PetscMalloc2(worksize, &nodework, worksize, &tupwork);CHKERRQ(ierr);
170   /* loop over the tuples of length dim with sum at most degree */
171   for (k = 0; k < npoints; k++) {
172     PetscInt i;
173 
174     /* turn thm into tuples of length dim + 1 with sum equal to degree (barycentric indice) */
175     tup[0] = degree;
176     for (i = 0; i < dim; i++) {
177       tup[0] -= tup[i+1];
178     }
179     switch(f->nodeFamily) {
180     case PETSCDTNODES_EQUISPACED:
181       /* compute equispaces nodes on the unit reference triangle */
182       if (f->endpoints) {
183         for (i = 0; i < dim; i++) {
184           points[dim*k + i] = (PetscReal) tup[i+1] / (PetscReal) degree;
185         }
186       } else {
187         for (i = 0; i < dim; i++) {
188           /* these nodes are at the centroids of the small simplices created by the equispaced nodes that include
189            * the endpoints */
190           points[dim*k + i] = ((PetscReal) tup[i+1] + 1./(dim+1.)) / (PetscReal) (degree + 1.);
191         }
192       }
193       break;
194     default:
195       /* compute equispaces nodes on the barycentric reference triangle (the trace on the first dim dimensions are the
196        * unit reference triangle nodes */
197       for (i = 0; i < dim + 1; i++) tupwork[i] = tup[i];
198       ierr = PetscNodeRecursive_Internal(dim, degree, nodesets, tupwork, nodework);CHKERRQ(ierr);
199       for (i = 0; i < dim; i++) points[dim*k + i] = nodework[i + 1];
200       break;
201     }
202     ierr = PetscDualSpaceLatticePointLexicographic_Internal(dim, degree, &tup[1]);CHKERRQ(ierr);
203   }
204   /* map from unit simplex to biunit simplex */
205   for (k = 0; k < npoints * dim; k++) points[k] = points[k] * 2. - 1.;
206   ierr = PetscFree2(nodework, tupwork);CHKERRQ(ierr);
207   ierr = PetscFree(tup);
208   PetscFunctionReturn(0);
209 }
210 
211 /* If we need to get the dofs from a mesh point, or add values into dofs at a mesh point, and there is more than one dof
212  * on that mesh point, we have to be careful about getting/adding everything in the right place.
213  *
214  * With nodal dofs like PETSCDUALSPACELAGRANGE makes, the general approach to calculate the value of dofs associate
215  * with a node A is
216  * - transform the node locations x(A) by the map that takes the mesh point to its reorientation, x' = phi(x(A))
217  * - figure out which node was originally at the location of the transformed point, A' = idx(x')
218  * - if the dofs are not scalars, figure out how to represent the transformed dofs in terms of the basis
219  *   of dofs at A' (using pushforward/pullback rules)
220  *
221  * The one sticky point with this approach is the "A' = idx(x')" step: trying to go from real valued coordinates
222  * back to indices.  I don't want to rely on floating point tolerances.  Additionally, PETSCDUALSPACELAGRANGE may
223  * eventually support quasi-Lagrangian dofs, which could involve quadrature at multiple points, so the location "x(A)"
224  * would be ambiguous.
225  *
226  * So each dof gets an integer value coordinate (nodeIdx in the structure below).  The choice of integer coordinates
227  * is somewhat arbitrary, as long as all of the relevant symmetries of the mesh point correspond to *permutations* of
228  * the integer coordinates, which do not depend on numerical precision.
229  *
230  * So
231  *
232  * - DMPlexGetTransitiveClosure_Internal() tells me how an orientation turns into a permutation of the vertices of a
233  *   mesh point
234  * - The permutation of the vertices, and the nodeIdx values assigned to them, tells what permutation in index space
235  *   is associated with the orientation
236  * - I uses that permutation to get xi' = phi(xi(A)), the integer coordinate of the transformed dof
237  * - I can without numerical issues compute A' = idx(xi')
238  *
239  * Here are some examples of how the process works
240  *
241  * - With a triangle:
242  *
243  *   The triangle has the following integer coordinates for vertices, taken from the barycentric triangle
244  *
245  *     closure order 2
246  *     nodeIdx (0,0,1)
247  *      \
248  *       +
249  *       |\
250  *       | \
251  *       |  \
252  *       |   \    closure order 1
253  *       |    \ / nodeIdx (0,1,0)
254  *       +-----+
255  *        \
256  *      closure order 0
257  *      nodeIdx (1,0,0)
258  *
259  *   If I do DMPlexGetTransitiveClosure_Internal() with orientation 1, the vertices would appear
260  *   in the order (1, 2, 0)
261  *
262  *   If I list the nodeIdx of each vertex in closure order for orientation 0 (0, 1, 2) and orientation 1 (1, 2, 0), I
263  *   see
264  *
265  *   orientation 0  | orientation 1
266  *
267  *   [0] (1,0,0)      [1] (0,1,0)
268  *   [1] (0,1,0)      [2] (0,0,1)
269  *   [2] (0,0,1)      [0] (1,0,0)
270  *          A                B
271  *
272  *   In other words, B is the result of a row permutation of A.  But, there is also
273  *   a column permutation that accomplishes the same result, (2,0,1).
274  *
275  *   So if a dof has nodeIdx coordinate (a,b,c), after the transformation its nodeIdx coordinate
276  *   is (c,a,b), and the transformed degree of freedom will be a linear combination of dofs
277  *   that originally had coordinate (c,a,b).
278  *
279  * - With a quadrilateral:
280  *
281  *   The quadrilateral has the following integer coordinates for vertices, taken from concatenating barycentric
282  *   coordinates for two segments:
283  *
284  *     closure order 3      closure order 2
285  *     nodeIdx (1,0,0,1)    nodeIdx (0,1,0,1)
286  *                   \      /
287  *                    +----+
288  *                    |    |
289  *                    |    |
290  *                    +----+
291  *                   /      \
292  *     closure order 0      closure order 1
293  *     nodeIdx (1,0,1,0)    nodeIdx (0,1,1,0)
294  *
295  *   If I do DMPlexGetTransitiveClosure_Internal() with orientation 1, the vertices would appear
296  *   in the order (1, 2, 3, 0)
297  *
298  *   If I list the nodeIdx of each vertex in closure order for orientation 0 (0, 1, 2, 3) and
299  *   orientation 1 (1, 2, 3, 0), I see
300  *
301  *   orientation 0  | orientation 1
302  *
303  *   [0] (1,0,1,0)    [1] (0,1,1,0)
304  *   [1] (0,1,1,0)    [2] (0,1,0,1)
305  *   [2] (0,1,0,1)    [3] (1,0,0,1)
306  *   [3] (1,0,0,1)    [0] (1,0,1,0)
307  *          A                B
308  *
309  *   The column permutation that accomplishes the same result is (3,2,0,1).
310  *
311  *   So if a dof has nodeIdx coordinate (a,b,c,d), after the transformation its nodeIdx coordinate
312  *   is (d,c,a,b), and the transformed degree of freedom will be a linear combination of dofs
313  *   that originally had coordinate (d,c,a,b).
314  *
315  * Previously PETSCDUALSPACELAGRANGE had hardcoded symmetries for the triangle and quadrilateral,
316  * but this approach will work for any polytope, such as the wedge (triangular prism).
317  */
318 struct _n_PetscLagNodeIndices
319 {
320   PetscInt   refct;
321   PetscInt   nodeIdxDim;
322   PetscInt   nodeVecDim;
323   PetscInt   nNodes;
324   PetscInt  *nodeIdx;      /* for each node an index of size nodeIdxDim */
325   PetscReal *nodeVec;      /* for each node a vector of size nodeVecDim */
326   PetscInt  *perm;         /* if these are vertices, perm takes DMPlex point index to closure order;
327                               if these are nodes, perm lists nodes in index revlex order */
328 };
329 
330 /* this is just here so I can access the values in tests/ex1.c outside the library */
331 PetscErrorCode PetscLagNodeIndicesGetData_Internal(PetscLagNodeIndices ni, PetscInt *nodeIdxDim, PetscInt *nodeVecDim, PetscInt *nNodes, const PetscInt *nodeIdx[], const PetscReal *nodeVec[])
332 {
333   PetscFunctionBegin;
334   *nodeIdxDim = ni->nodeIdxDim;
335   *nodeVecDim = ni->nodeVecDim;
336   *nNodes = ni->nNodes;
337   *nodeIdx = ni->nodeIdx;
338   *nodeVec = ni->nodeVec;
339   PetscFunctionReturn(0);
340 }
341 
342 static PetscErrorCode PetscLagNodeIndicesReference(PetscLagNodeIndices ni)
343 {
344   PetscFunctionBegin;
345   if (ni) ni->refct++;
346   PetscFunctionReturn(0);
347 }
348 
349 static PetscErrorCode PetscLagNodeIndicesDuplicate(PetscLagNodeIndices ni, PetscLagNodeIndices *niNew)
350 {
351   PetscErrorCode ierr;
352 
353   PetscFunctionBegin;
354   ierr = PetscNew(niNew);CHKERRQ(ierr);
355   (*niNew)->refct = 1;
356   (*niNew)->nodeIdxDim = ni->nodeIdxDim;
357   (*niNew)->nodeVecDim = ni->nodeVecDim;
358   (*niNew)->nNodes = ni->nNodes;
359   ierr = PetscMalloc1(ni->nNodes * ni->nodeIdxDim, &((*niNew)->nodeIdx));CHKERRQ(ierr);
360   ierr = PetscArraycpy((*niNew)->nodeIdx, ni->nodeIdx, ni->nNodes * ni->nodeIdxDim);CHKERRQ(ierr);
361   ierr = PetscMalloc1(ni->nNodes * ni->nodeVecDim, &((*niNew)->nodeVec));CHKERRQ(ierr);
362   ierr = PetscArraycpy((*niNew)->nodeVec, ni->nodeVec, ni->nNodes * ni->nodeVecDim);CHKERRQ(ierr);
363   (*niNew)->perm = NULL;
364   PetscFunctionReturn(0);
365 }
366 
367 static PetscErrorCode PetscLagNodeIndicesDestroy(PetscLagNodeIndices *ni) {
368   PetscErrorCode ierr;
369 
370   PetscFunctionBegin;
371   if (!(*ni)) PetscFunctionReturn(0);
372   if (--(*ni)->refct > 0) {
373     *ni = NULL;
374     PetscFunctionReturn(0);
375   }
376   ierr = PetscFree((*ni)->nodeIdx);CHKERRQ(ierr);
377   ierr = PetscFree((*ni)->nodeVec);CHKERRQ(ierr);
378   ierr = PetscFree((*ni)->perm);CHKERRQ(ierr);
379   ierr = PetscFree(*ni);CHKERRQ(ierr);
380   *ni = NULL;
381   PetscFunctionReturn(0);
382 }
383 
384 /* The vertices are given nodeIdx coordinates (e.g. the corners of the barycentric triangle).  Those coordinates are
385  * in some other order, and to understand the effect of different symmetries, we need them to be in closure order.
386  *
387  * If sortIdx is PETSC_FALSE, the coordinates are already in revlex order, otherwise we must sort them
388  * to that order before we do the real work of this function, which is
389  *
390  * - mark the vertices in closure order
391  * - sort them in revlex order
392  * - use the resulting permutation to list the vertex coordinates in closure order
393  */
394 static PetscErrorCode PetscLagNodeIndicesComputeVertexOrder(DM dm, PetscLagNodeIndices ni, PetscBool sortIdx)
395 {
396   PetscInt        v, w, vStart, vEnd, c, d;
397   PetscInt        nVerts;
398   PetscInt        closureSize = 0;
399   PetscInt       *closure = NULL;
400   PetscInt       *closureOrder;
401   PetscInt       *invClosureOrder;
402   PetscInt       *revlexOrder;
403   PetscInt       *newNodeIdx;
404   PetscInt        dim;
405   Vec             coordVec;
406   const PetscScalar *coords;
407   PetscErrorCode  ierr;
408 
409   PetscFunctionBegin;
410   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
411   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
412   nVerts = vEnd - vStart;
413   ierr = PetscMalloc1(nVerts, &closureOrder);CHKERRQ(ierr);
414   ierr = PetscMalloc1(nVerts, &invClosureOrder);CHKERRQ(ierr);
415   ierr = PetscMalloc1(nVerts, &revlexOrder);CHKERRQ(ierr);
416   if (sortIdx) { /* bubble sort nodeIdx into revlex order */
417     PetscInt nodeIdxDim = ni->nodeIdxDim;
418     PetscInt *idxOrder;
419 
420     ierr = PetscMalloc1(nVerts * nodeIdxDim, &newNodeIdx);CHKERRQ(ierr);
421     ierr = PetscMalloc1(nVerts, &idxOrder);CHKERRQ(ierr);
422     for (v = 0; v < nVerts; v++) idxOrder[v] = v;
423     for (v = 0; v < nVerts; v++) {
424       for (w = v + 1; w < nVerts; w++) {
425         const PetscInt *iv = &(ni->nodeIdx[idxOrder[v] * nodeIdxDim]);
426         const PetscInt *iw = &(ni->nodeIdx[idxOrder[w] * nodeIdxDim]);
427         PetscInt diff = 0;
428 
429         for (d = nodeIdxDim - 1; d >= 0; d--) if ((diff = (iv[d] - iw[d]))) break;
430         if (diff > 0) {
431           PetscInt swap = idxOrder[v];
432 
433           idxOrder[v] = idxOrder[w];
434           idxOrder[w] = swap;
435         }
436       }
437     }
438     for (v = 0; v < nVerts; v++) {
439       for (d = 0; d < nodeIdxDim; d++) {
440         newNodeIdx[v * ni->nodeIdxDim + d] = ni->nodeIdx[idxOrder[v] * nodeIdxDim + d];
441       }
442     }
443     ierr = PetscFree(ni->nodeIdx);CHKERRQ(ierr);
444     ni->nodeIdx = newNodeIdx;
445     newNodeIdx = NULL;
446     ierr = PetscFree(idxOrder);CHKERRQ(ierr);
447   }
448   ierr = DMPlexGetTransitiveClosure(dm, 0, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
449   c = closureSize - nVerts;
450   for (v = 0; v < nVerts; v++) closureOrder[v] = closure[2 * (c + v)] - vStart;
451   for (v = 0; v < nVerts; v++) invClosureOrder[closureOrder[v]] = v;
452   ierr = DMPlexRestoreTransitiveClosure(dm, 0, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
453   ierr = DMGetCoordinatesLocal(dm, &coordVec);CHKERRQ(ierr);
454   ierr = VecGetArrayRead(coordVec, &coords);CHKERRQ(ierr);
455   /* bubble sort closure vertices by coordinates in revlex order */
456   for (v = 0; v < nVerts; v++) revlexOrder[v] = v;
457   for (v = 0; v < nVerts; v++) {
458     for (w = v + 1; w < nVerts; w++) {
459       const PetscScalar *cv = &coords[closureOrder[revlexOrder[v]] * dim];
460       const PetscScalar *cw = &coords[closureOrder[revlexOrder[w]] * dim];
461       PetscReal diff = 0;
462 
463       for (d = dim - 1; d >= 0; d--) if ((diff = PetscRealPart(cv[d] - cw[d])) != 0.) break;
464       if (diff > 0.) {
465         PetscInt swap = revlexOrder[v];
466 
467         revlexOrder[v] = revlexOrder[w];
468         revlexOrder[w] = swap;
469       }
470     }
471   }
472   ierr = VecRestoreArrayRead(coordVec, &coords);CHKERRQ(ierr);
473   ierr = PetscMalloc1(ni->nodeIdxDim * nVerts, &newNodeIdx);CHKERRQ(ierr);
474   /* reorder nodeIdx to be in closure order */
475   for (v = 0; v < nVerts; v++) {
476     for (d = 0; d < ni->nodeIdxDim; d++) {
477       newNodeIdx[revlexOrder[v] * ni->nodeIdxDim + d] = ni->nodeIdx[v * ni->nodeIdxDim + d];
478     }
479   }
480   ierr = PetscFree(ni->nodeIdx);CHKERRQ(ierr);
481   ni->nodeIdx = newNodeIdx;
482   ni->perm = invClosureOrder;
483   ierr = PetscFree(revlexOrder);CHKERRQ(ierr);
484   ierr = PetscFree(closureOrder);CHKERRQ(ierr);
485   PetscFunctionReturn(0);
486 }
487 
488 /* the coordinates of the simplex vertices are the corners of the barycentric simplex.
489  * When we stack them on top of each other in revlex order, they look like the identity matrix */
490 static PetscErrorCode PetscLagNodeIndicesCreateSimplexVertices(DM dm, PetscLagNodeIndices *nodeIndices)
491 {
492   PetscLagNodeIndices ni;
493   PetscInt       dim, d;
494 
495   PetscErrorCode ierr;
496 
497   PetscFunctionBegin;
498   ierr = PetscNew(&ni);CHKERRQ(ierr);
499   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
500   ni->nodeIdxDim = dim + 1;
501   ni->nodeVecDim = 0;
502   ni->nNodes = dim + 1;
503   ni->refct = 1;
504   ierr = PetscCalloc1((dim + 1)*(dim + 1), &(ni->nodeIdx));CHKERRQ(ierr);
505   for (d = 0; d < dim + 1; d++) ni->nodeIdx[d*(dim + 2)] = 1;
506   ierr = PetscLagNodeIndicesComputeVertexOrder(dm, ni, PETSC_FALSE);CHKERRQ(ierr);
507   *nodeIndices = ni;
508   PetscFunctionReturn(0);
509 }
510 
511 /* A polytope that is a tensor product of a facet and a segment.
512  * We take whatever coordinate system was being used for the facet
513  * and we concatenate the barycentric coordinates for the vertices
514  * at the end of the segment, (1,0) and (0,1), to get a coordinate
515  * system for the tensor product element */
516 static PetscErrorCode PetscLagNodeIndicesCreateTensorVertices(DM dm, PetscLagNodeIndices facetni, PetscLagNodeIndices *nodeIndices)
517 {
518   PetscLagNodeIndices ni;
519   PetscInt       nodeIdxDim, subNodeIdxDim = facetni->nodeIdxDim;
520   PetscInt       nVerts, nSubVerts = facetni->nNodes;
521   PetscInt       dim, d, e, f, g;
522 
523   PetscErrorCode ierr;
524 
525   PetscFunctionBegin;
526   ierr = PetscNew(&ni);CHKERRQ(ierr);
527   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
528   ni->nodeIdxDim = nodeIdxDim = subNodeIdxDim + 2;
529   ni->nodeVecDim = 0;
530   ni->nNodes = nVerts = 2 * nSubVerts;
531   ni->refct = 1;
532   ierr = PetscCalloc1(nodeIdxDim * nVerts, &(ni->nodeIdx));CHKERRQ(ierr);
533   for (f = 0, d = 0; d < 2; d++) {
534     for (e = 0; e < nSubVerts; e++, f++) {
535       for (g = 0; g < subNodeIdxDim; g++) {
536         ni->nodeIdx[f * nodeIdxDim + g] = facetni->nodeIdx[e * subNodeIdxDim + g];
537       }
538       ni->nodeIdx[f * nodeIdxDim + subNodeIdxDim] = (1 - d);
539       ni->nodeIdx[f * nodeIdxDim + subNodeIdxDim + 1] = d;
540     }
541   }
542   ierr = PetscLagNodeIndicesComputeVertexOrder(dm, ni, PETSC_TRUE);CHKERRQ(ierr);
543   *nodeIndices = ni;
544   PetscFunctionReturn(0);
545 }
546 
547 /* This helps us compute symmetries, and it also helps us compute coordinates for dofs that are being pushed
548  * forward from a boundary mesh point.
549  *
550  * Input:
551  *
552  * dm - the target reference cell where we want new coordinates and dof directions to be valid
553  * vert - the vertex coordinate system for the target reference cell
554  * p - the point in the target reference cell that the dofs are coming from
555  * vertp - the vertex coordinate system for p's reference cell
556  * ornt - the resulting coordinates and dof vectors will be for p under this orientation
557  * nodep - the node coordinates and dof vectors in p's reference cell
558  * formDegree - the form degree that the dofs transform as
559  *
560  * Output:
561  *
562  * pfNodeIdx - the node coordinates for p's dofs, in the dm reference cell, from the ornt perspective
563  * pfNodeVec - the node dof vectors for p's dofs, in the dm reference cell, from the ornt perspective
564  */
565 static PetscErrorCode PetscLagNodeIndicesPushForward(DM dm, PetscLagNodeIndices vert, PetscInt p, PetscLagNodeIndices vertp, PetscLagNodeIndices nodep, PetscInt ornt, PetscInt formDegree, PetscInt pfNodeIdx[], PetscReal pfNodeVec[])
566 {
567   PetscInt       *closureVerts;
568   PetscInt        closureSize = 0;
569   PetscInt       *closure = NULL;
570   PetscInt        dim, pdim, c, i, j, k, n, v, vStart, vEnd;
571   PetscInt        nSubVert = vertp->nNodes;
572   PetscInt        nodeIdxDim = vert->nodeIdxDim;
573   PetscInt        subNodeIdxDim = vertp->nodeIdxDim;
574   PetscInt        nNodes = nodep->nNodes;
575   const PetscInt  *vertIdx = vert->nodeIdx;
576   const PetscInt  *subVertIdx = vertp->nodeIdx;
577   const PetscInt  *nodeIdx = nodep->nodeIdx;
578   const PetscReal *nodeVec = nodep->nodeVec;
579   PetscReal       *J, *Jstar;
580   PetscReal       detJ;
581   PetscInt        depth, pdepth, Nk, pNk;
582   Vec             coordVec;
583   PetscScalar      *newCoords = NULL;
584   const PetscScalar *oldCoords = NULL;
585   PetscErrorCode  ierr;
586 
587   PetscFunctionBegin;
588   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
589   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
590   ierr = DMGetCoordinatesLocal(dm, &coordVec);CHKERRQ(ierr);
591   ierr = DMPlexGetPointDepth(dm, p, &pdepth);CHKERRQ(ierr);
592   pdim = pdepth != depth ? pdepth != 0 ? pdepth : 0 : dim;
593   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
594   ierr = DMGetWorkArray(dm, nSubVert, MPIU_INT, &closureVerts);CHKERRQ(ierr);
595   ierr = DMPlexGetTransitiveClosure_Internal(dm, p, ornt, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
596   c = closureSize - nSubVert;
597   /* we want which cell closure indices the closure of this point corresponds to */
598   for (v = 0; v < nSubVert; v++) closureVerts[v] = vert->perm[closure[2 * (c + v)] - vStart];
599   ierr = DMPlexRestoreTransitiveClosure(dm, p, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
600   /* push forward indices */
601   for (i = 0; i < nodeIdxDim; i++) { /* for every component of the target index space */
602     /* check if this is a component that all vertices around this point have in common */
603     for (j = 1; j < nSubVert; j++) {
604       if (vertIdx[closureVerts[j] * nodeIdxDim + i] != vertIdx[closureVerts[0] * nodeIdxDim + i]) break;
605     }
606     if (j == nSubVert) { /* all vertices have this component in common, directly copy to output */
607       PetscInt val = vertIdx[closureVerts[0] * nodeIdxDim + i];
608       for (n = 0; n < nNodes; n++) pfNodeIdx[n * nodeIdxDim + i] = val;
609     } else {
610       PetscInt subi = -1;
611       /* there must be a component in vertp that looks the same */
612       for (k = 0; k < subNodeIdxDim; k++) {
613         for (j = 0; j < nSubVert; j++) {
614           if (vertIdx[closureVerts[j] * nodeIdxDim + i] != subVertIdx[j * subNodeIdxDim + k]) break;
615         }
616         if (j == nSubVert) {
617           subi = k;
618           break;
619         }
620       }
621       if (subi < 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Did not find matching coordinate\n");
622       /* that component in the vertp system becomes component i in the vert system for each dof */
623       for (n = 0; n < nNodes; n++) pfNodeIdx[n * nodeIdxDim + i] = nodeIdx[n * subNodeIdxDim + subi];
624     }
625   }
626   /* push forward vectors */
627   ierr = DMGetWorkArray(dm, dim * dim, MPIU_REAL, &J);CHKERRQ(ierr);
628   if (ornt != 0) { /* temporarily change the coordinate vector so
629                       DMPlexComputeCellGeometryAffineFEM gives us the Jacobian we want */
630     PetscInt        closureSize2 = 0;
631     PetscInt       *closure2 = NULL;
632 
633     ierr = DMPlexGetTransitiveClosure_Internal(dm, p, 0, PETSC_TRUE, &closureSize2, &closure2);CHKERRQ(ierr);
634     ierr = PetscMalloc1(dim * nSubVert, &newCoords);CHKERRQ(ierr);
635     ierr = VecGetArrayRead(coordVec, &oldCoords);CHKERRQ(ierr);
636     for (v = 0; v < nSubVert; v++) {
637       PetscInt d;
638       for (d = 0; d < dim; d++) {
639         newCoords[(closure2[2 * (c + v)] - vStart) * dim + d] = oldCoords[closureVerts[v] * dim + d];
640       }
641     }
642     ierr = VecRestoreArrayRead(coordVec, &oldCoords);CHKERRQ(ierr);
643     ierr = DMPlexRestoreTransitiveClosure(dm, p, PETSC_TRUE, &closureSize2, &closure2);CHKERRQ(ierr);
644     ierr = VecPlaceArray(coordVec, newCoords);CHKERRQ(ierr);
645   }
646   ierr = DMPlexComputeCellGeometryAffineFEM(dm, p, NULL, J, NULL, &detJ);CHKERRQ(ierr);
647   if (ornt != 0) {
648     ierr = VecResetArray(coordVec);CHKERRQ(ierr);
649     ierr = PetscFree(newCoords);CHKERRQ(ierr);
650   }
651   ierr = DMRestoreWorkArray(dm, nSubVert, MPIU_INT, &closureVerts);CHKERRQ(ierr);
652   /* compactify */
653   for (i = 0; i < dim; i++) for (j = 0; j < pdim; j++) J[i * pdim + j] = J[i * dim + j];
654   /* We have the Jacobian mapping the point's reference cell to this reference cell:
655    * pulling back a function to the point and applying the dof is what we want,
656    * so we get the pullback matrix and multiply the dof by that matrix on the right */
657   ierr = PetscDTBinomialInt(dim, PetscAbsInt(formDegree), &Nk);CHKERRQ(ierr);
658   ierr = PetscDTBinomialInt(pdim, PetscAbsInt(formDegree), &pNk);CHKERRQ(ierr);
659   ierr = DMGetWorkArray(dm, pNk * Nk, MPIU_REAL, &Jstar);CHKERRQ(ierr);
660   ierr = PetscDTAltVPullbackMatrix(pdim, dim, J, formDegree, Jstar);CHKERRQ(ierr);
661   for (n = 0; n < nNodes; n++) {
662     for (i = 0; i < Nk; i++) {
663       PetscReal val = 0.;
664       for (j = 0; j < pNk; j++) val += nodeVec[n * pNk + j] * Jstar[j * Nk + i];
665       pfNodeVec[n * Nk + i] = val;
666     }
667   }
668   ierr = DMRestoreWorkArray(dm, pNk * Nk, MPIU_REAL, &Jstar);CHKERRQ(ierr);
669   ierr = DMRestoreWorkArray(dm, dim * dim, MPIU_REAL, &J);CHKERRQ(ierr);
670   PetscFunctionReturn(0);
671 }
672 
673 /* given to sets of nodes, take the tensor product, where the product of the dof indices is concatenation and the
674  * product of the dof vectors is the wedge product */
675 static PetscErrorCode PetscLagNodeIndicesTensor(PetscLagNodeIndices tracei, PetscInt dimT, PetscInt kT, PetscLagNodeIndices fiberi, PetscInt dimF, PetscInt kF, PetscLagNodeIndices *nodeIndices)
676 {
677   PetscInt       dim = dimT + dimF;
678   PetscInt       nodeIdxDim, nNodes;
679   PetscInt       formDegree = kT + kF;
680   PetscInt       Nk, NkT, NkF;
681   PetscInt       MkT, MkF;
682   PetscLagNodeIndices ni;
683   PetscInt       i, j, l;
684   PetscReal      *projF, *projT;
685   PetscReal      *projFstar, *projTstar;
686   PetscReal      *workF, *workF2, *workT, *workT2, *work, *work2;
687   PetscReal      *wedgeMat;
688   PetscReal      sign;
689   PetscErrorCode ierr;
690 
691   PetscFunctionBegin;
692   ierr = PetscDTBinomialInt(dim, PetscAbsInt(formDegree), &Nk);CHKERRQ(ierr);
693   ierr = PetscDTBinomialInt(dimT, PetscAbsInt(kT), &NkT);CHKERRQ(ierr);
694   ierr = PetscDTBinomialInt(dimF, PetscAbsInt(kF), &NkF);CHKERRQ(ierr);
695   ierr = PetscDTBinomialInt(dim, PetscAbsInt(kT), &MkT);CHKERRQ(ierr);
696   ierr = PetscDTBinomialInt(dim, PetscAbsInt(kF), &MkF);CHKERRQ(ierr);
697   ierr = PetscNew(&ni);CHKERRQ(ierr);
698   ni->nodeIdxDim = nodeIdxDim = tracei->nodeIdxDim + fiberi->nodeIdxDim;
699   ni->nodeVecDim = Nk;
700   ni->nNodes = nNodes = tracei->nNodes * fiberi->nNodes;
701   ni->refct = 1;
702   ierr = PetscMalloc1(nNodes * nodeIdxDim, &(ni->nodeIdx));CHKERRQ(ierr);
703   /* first concatenate the indices */
704   for (l = 0, j = 0; j < fiberi->nNodes; j++) {
705     for (i = 0; i < tracei->nNodes; i++, l++) {
706       PetscInt m, n = 0;
707 
708       for (m = 0; m < tracei->nodeIdxDim; m++) ni->nodeIdx[l * nodeIdxDim + n++] = tracei->nodeIdx[i * tracei->nodeIdxDim + m];
709       for (m = 0; m < fiberi->nodeIdxDim; m++) ni->nodeIdx[l * nodeIdxDim + n++] = fiberi->nodeIdx[j * fiberi->nodeIdxDim + m];
710     }
711   }
712 
713   /* now wedge together the push-forward vectors */
714   ierr = PetscMalloc1(nNodes * Nk, &(ni->nodeVec));CHKERRQ(ierr);
715   ierr = PetscCalloc2(dimT*dim, &projT, dimF*dim, &projF);CHKERRQ(ierr);
716   for (i = 0; i < dimT; i++) projT[i * (dim + 1)] = 1.;
717   for (i = 0; i < dimF; i++) projF[i * (dim + dimT + 1) + dimT] = 1.;
718   ierr = PetscMalloc2(MkT*NkT, &projTstar, MkF*NkF, &projFstar);CHKERRQ(ierr);
719   ierr = PetscDTAltVPullbackMatrix(dim, dimT, projT, kT, projTstar);CHKERRQ(ierr);
720   ierr = PetscDTAltVPullbackMatrix(dim, dimF, projF, kF, projFstar);CHKERRQ(ierr);
721   ierr = PetscMalloc6(MkT, &workT, MkT, &workT2, MkF, &workF, MkF, &workF2, Nk, &work, Nk, &work2);CHKERRQ(ierr);
722   ierr = PetscMalloc1(Nk * MkT, &wedgeMat);CHKERRQ(ierr);
723   sign = (PetscAbsInt(kT * kF) & 1) ? -1. : 1.;
724   for (l = 0, j = 0; j < fiberi->nNodes; j++) {
725     PetscInt d, e;
726 
727     /* push forward fiber k-form */
728     for (d = 0; d < MkF; d++) {
729       PetscReal val = 0.;
730       for (e = 0; e < NkF; e++) val += projFstar[d * NkF + e] * fiberi->nodeVec[j * NkF + e];
731       workF[d] = val;
732     }
733     /* Hodge star to proper form if necessary */
734     if (kF < 0) {
735       for (d = 0; d < MkF; d++) workF2[d] = workF[d];
736       ierr = PetscDTAltVStar(dim, PetscAbsInt(kF), 1, workF2, workF);CHKERRQ(ierr);
737     }
738     /* Compute the matrix that wedges this form with one of the trace k-form */
739     ierr = PetscDTAltVWedgeMatrix(dim, PetscAbsInt(kF), PetscAbsInt(kT), workF, wedgeMat);CHKERRQ(ierr);
740     for (i = 0; i < tracei->nNodes; i++, l++) {
741       /* push forward trace k-form */
742       for (d = 0; d < MkT; d++) {
743         PetscReal val = 0.;
744         for (e = 0; e < NkT; e++) val += projTstar[d * NkT + e] * tracei->nodeVec[i * NkT + e];
745         workT[d] = val;
746       }
747       /* Hodge star to proper form if necessary */
748       if (kT < 0) {
749         for (d = 0; d < MkT; d++) workT2[d] = workT[d];
750         ierr = PetscDTAltVStar(dim, PetscAbsInt(kT), 1, workT2, workT);CHKERRQ(ierr);
751       }
752       /* compute the wedge product of the push-forward trace form and firer forms */
753       for (d = 0; d < Nk; d++) {
754         PetscReal val = 0.;
755         for (e = 0; e < MkT; e++) val += wedgeMat[d * MkT + e] * workT[e];
756         work[d] = val;
757       }
758       /* inverse Hodge star from proper form if necessary */
759       if (formDegree < 0) {
760         for (d = 0; d < Nk; d++) work2[d] = work[d];
761         ierr = PetscDTAltVStar(dim, PetscAbsInt(formDegree), -1, work2, work);CHKERRQ(ierr);
762       }
763       /* insert into the array (adjusting for sign) */
764       for (d = 0; d < Nk; d++) ni->nodeVec[l * Nk + d] = sign * work[d];
765     }
766   }
767   ierr = PetscFree(wedgeMat);CHKERRQ(ierr);
768   ierr = PetscFree6(workT, workT2, workF, workF2, work, work2);CHKERRQ(ierr);
769   ierr = PetscFree2(projTstar, projFstar);CHKERRQ(ierr);
770   ierr = PetscFree2(projT, projF);CHKERRQ(ierr);
771   *nodeIndices = ni;
772   PetscFunctionReturn(0);
773 }
774 
775 /* simple union of two sets of nodes */
776 static PetscErrorCode PetscLagNodeIndicesMerge(PetscLagNodeIndices niA, PetscLagNodeIndices niB, PetscLagNodeIndices *nodeIndices)
777 {
778   PetscLagNodeIndices ni;
779   PetscInt            nodeIdxDim, nodeVecDim, nNodes;
780   PetscErrorCode      ierr;
781 
782   PetscFunctionBegin;
783   ierr = PetscNew(&ni);CHKERRQ(ierr);
784   ni->nodeIdxDim = nodeIdxDim = niA->nodeIdxDim;
785   if (niB->nodeIdxDim != nodeIdxDim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Cannot merge PetscLagNodeIndices with different nodeIdxDim");
786   ni->nodeVecDim = nodeVecDim = niA->nodeVecDim;
787   if (niB->nodeVecDim != nodeVecDim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Cannot merge PetscLagNodeIndices with different nodeVecDim");
788   ni->nNodes = nNodes = niA->nNodes + niB->nNodes;
789   ni->refct = 1;
790   ierr = PetscMalloc1(nNodes * nodeIdxDim, &(ni->nodeIdx));CHKERRQ(ierr);
791   ierr = PetscMalloc1(nNodes * nodeVecDim, &(ni->nodeVec));CHKERRQ(ierr);
792   ierr = PetscArraycpy(ni->nodeIdx, niA->nodeIdx, niA->nNodes * nodeIdxDim);CHKERRQ(ierr);
793   ierr = PetscArraycpy(ni->nodeVec, niA->nodeVec, niA->nNodes * nodeVecDim);CHKERRQ(ierr);
794   ierr = PetscArraycpy(&(ni->nodeIdx[niA->nNodes * nodeIdxDim]), niB->nodeIdx, niB->nNodes * nodeIdxDim);CHKERRQ(ierr);
795   ierr = PetscArraycpy(&(ni->nodeVec[niA->nNodes * nodeVecDim]), niB->nodeVec, niB->nNodes * nodeVecDim);CHKERRQ(ierr);
796   *nodeIndices = ni;
797   PetscFunctionReturn(0);
798 }
799 
800 #define PETSCTUPINTCOMPREVLEX(N)                                   \
801 static int PetscTupIntCompRevlex_##N(const void *a, const void *b) \
802 {                                                                  \
803   const PetscInt *A = (const PetscInt *) a;                        \
804   const PetscInt *B = (const PetscInt *) b;                        \
805   int i;                                                           \
806   PetscInt diff = 0;                                               \
807   for (i = 0; i < N; i++) {                                        \
808     diff = A[N - i] - B[N - i];                                    \
809     if (diff) break;                                               \
810   }                                                                \
811   return (diff <= 0) ? (diff < 0) ? -1 : 0 : 1;                    \
812 }
813 
814 PETSCTUPINTCOMPREVLEX(3)
815 PETSCTUPINTCOMPREVLEX(4)
816 PETSCTUPINTCOMPREVLEX(5)
817 PETSCTUPINTCOMPREVLEX(6)
818 PETSCTUPINTCOMPREVLEX(7)
819 
820 static int PetscTupIntCompRevlex_N(const void *a, const void *b)
821 {
822   const PetscInt *A = (const PetscInt *) a;
823   const PetscInt *B = (const PetscInt *) b;
824   int i;
825   int N = A[0];
826   PetscInt diff = 0;
827   for (i = 0; i < N; i++) {
828     diff = A[N - i] - B[N - i];
829     if (diff) break;
830   }
831   return (diff <= 0) ? (diff < 0) ? -1 : 0 : 1;
832 }
833 
834 /* The nodes are not necessarily in revlex order wrt nodeIdx: get the permutation
835  * that puts them in that order */
836 static PetscErrorCode PetscLagNodeIndicesGetPermutation(PetscLagNodeIndices ni, PetscInt *perm[])
837 {
838   PetscErrorCode ierr;
839 
840   PetscFunctionBegin;
841   if (!(ni->perm)) {
842     PetscInt *sorter;
843     PetscInt m = ni->nNodes;
844     PetscInt nodeIdxDim = ni->nodeIdxDim;
845     PetscInt i, j, k, l;
846     PetscInt *prm;
847     int (*comp) (const void *, const void *);
848 
849     ierr = PetscMalloc1((nodeIdxDim + 2) * m, &sorter);CHKERRQ(ierr);
850     for (k = 0, l = 0, i = 0; i < m; i++) {
851       sorter[k++] = nodeIdxDim + 1;
852       sorter[k++] = i;
853       for (j = 0; j < nodeIdxDim; j++) sorter[k++] = ni->nodeIdx[l++];
854     }
855     switch (nodeIdxDim) {
856     case 2:
857       comp = PetscTupIntCompRevlex_3;
858       break;
859     case 3:
860       comp = PetscTupIntCompRevlex_4;
861       break;
862     case 4:
863       comp = PetscTupIntCompRevlex_5;
864       break;
865     case 5:
866       comp = PetscTupIntCompRevlex_6;
867       break;
868     case 6:
869       comp = PetscTupIntCompRevlex_7;
870       break;
871     default:
872       comp = PetscTupIntCompRevlex_N;
873       break;
874     }
875     qsort(sorter, m, (nodeIdxDim + 2) * sizeof(PetscInt), comp);
876     ierr = PetscMalloc1(m, &prm);CHKERRQ(ierr);
877     for (i = 0; i < m; i++) prm[i] = sorter[(nodeIdxDim + 2) * i + 1];
878     ni->perm = prm;
879     ierr = PetscFree(sorter);
880   }
881   *perm = ni->perm;
882   PetscFunctionReturn(0);
883 }
884 
885 static PetscErrorCode PetscDualSpaceDestroy_Lagrange(PetscDualSpace sp)
886 {
887   PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *) sp->data;
888   PetscErrorCode      ierr;
889 
890   PetscFunctionBegin;
891   if (lag->symperms) {
892     PetscInt **selfSyms = lag->symperms[0];
893 
894     if (selfSyms) {
895       PetscInt i, **allocated = &selfSyms[-lag->selfSymOff];
896 
897       for (i = 0; i < lag->numSelfSym; i++) {
898         ierr = PetscFree(allocated[i]);CHKERRQ(ierr);
899       }
900       ierr = PetscFree(allocated);CHKERRQ(ierr);
901     }
902     ierr = PetscFree(lag->symperms);CHKERRQ(ierr);
903   }
904   if (lag->symflips) {
905     PetscScalar **selfSyms = lag->symflips[0];
906 
907     if (selfSyms) {
908       PetscInt i;
909       PetscScalar **allocated = &selfSyms[-lag->selfSymOff];
910 
911       for (i = 0; i < lag->numSelfSym; i++) {
912         ierr = PetscFree(allocated[i]);CHKERRQ(ierr);
913       }
914       ierr = PetscFree(allocated);CHKERRQ(ierr);
915     }
916     ierr = PetscFree(lag->symflips);CHKERRQ(ierr);
917   }
918   ierr = Petsc1DNodeFamilyDestroy(&(lag->nodeFamily));CHKERRQ(ierr);
919   ierr = PetscLagNodeIndicesDestroy(&(lag->vertIndices));CHKERRQ(ierr);
920   ierr = PetscLagNodeIndicesDestroy(&(lag->intNodeIndices));CHKERRQ(ierr);
921   ierr = PetscLagNodeIndicesDestroy(&(lag->allNodeIndices));CHKERRQ(ierr);
922   ierr = PetscFree(lag);CHKERRQ(ierr);
923   ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeGetContinuity_C", NULL);CHKERRQ(ierr);
924   ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeSetContinuity_C", NULL);CHKERRQ(ierr);
925   ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeGetTensor_C", NULL);CHKERRQ(ierr);
926   ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeSetTensor_C", NULL);CHKERRQ(ierr);
927   ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeGetTrimmed_C", NULL);CHKERRQ(ierr);
928   ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeSetTrimmed_C", NULL);CHKERRQ(ierr);
929   ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeGetNodeType_C", NULL);CHKERRQ(ierr);
930   ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeSetNodeType_C", NULL);CHKERRQ(ierr);
931   PetscFunctionReturn(0);
932 }
933 
934 static PetscErrorCode PetscDualSpaceLagrangeView_Ascii(PetscDualSpace sp, PetscViewer viewer)
935 {
936   PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *) sp->data;
937   PetscErrorCode      ierr;
938 
939   PetscFunctionBegin;
940   ierr = PetscViewerASCIIPrintf(viewer, "%s %s%sLagrange dual space\n", lag->continuous ? "Continuous" : "Discontinuous", lag->tensorSpace ? "tensor " : "", lag->trimmed ? "trimmed " : "");CHKERRQ(ierr);
941   PetscFunctionReturn(0);
942 }
943 
944 static PetscErrorCode PetscDualSpaceView_Lagrange(PetscDualSpace sp, PetscViewer viewer)
945 {
946   PetscBool      iascii;
947   PetscErrorCode ierr;
948 
949   PetscFunctionBegin;
950   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
951   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
952   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
953   if (iascii) {ierr = PetscDualSpaceLagrangeView_Ascii(sp, viewer);CHKERRQ(ierr);}
954   PetscFunctionReturn(0);
955 }
956 
957 static PetscErrorCode PetscDualSpaceSetFromOptions_Lagrange(PetscOptionItems *PetscOptionsObject,PetscDualSpace sp)
958 {
959   PetscBool      continuous, tensor, trimmed, flg, flg2, flg3;
960   PetscDTNodeType nodeType;
961   PetscReal      nodeExponent;
962   PetscBool      nodeEndpoints;
963   PetscErrorCode ierr;
964 
965   PetscFunctionBegin;
966   ierr = PetscDualSpaceLagrangeGetContinuity(sp, &continuous);CHKERRQ(ierr);
967   ierr = PetscDualSpaceLagrangeGetTensor(sp, &tensor);CHKERRQ(ierr);
968   ierr = PetscDualSpaceLagrangeGetTrimmed(sp, &trimmed);CHKERRQ(ierr);
969   ierr = PetscDualSpaceLagrangeGetNodeType(sp, &nodeType, &nodeEndpoints, &nodeExponent);CHKERRQ(ierr);
970   if (nodeType == PETSCDTNODES_DEFAULT) nodeType = PETSCDTNODES_GAUSSJACOBI;
971   ierr = PetscOptionsHead(PetscOptionsObject,"PetscDualSpace Lagrange Options");CHKERRQ(ierr);
972   ierr = PetscOptionsBool("-petscdualspace_lagrange_continuity", "Flag for continuous element", "PetscDualSpaceLagrangeSetContinuity", continuous, &continuous, &flg);CHKERRQ(ierr);
973   if (flg) {ierr = PetscDualSpaceLagrangeSetContinuity(sp, continuous);CHKERRQ(ierr);}
974   ierr = PetscOptionsBool("-petscdualspace_lagrange_tensor", "Flag for tensor dual space", "PetscDualSpaceLagrangeSetTensor", tensor, &tensor, &flg);CHKERRQ(ierr);
975   if (flg) {ierr = PetscDualSpaceLagrangeSetTensor(sp, tensor);CHKERRQ(ierr);}
976   ierr = PetscOptionsBool("-petscdualspace_lagrange_trimmed", "Flag for trimmed dual space", "PetscDualSpaceLagrangeSetTrimmed", trimmed, &trimmed, &flg);CHKERRQ(ierr);
977   if (flg) {ierr = PetscDualSpaceLagrangeSetTrimmed(sp, trimmed);CHKERRQ(ierr);}
978   ierr = PetscOptionsEnum("-petscdualspace_lagrange_node_type", "Lagrange node location type", "PetscDualSpaceLagrangeSetNodeType", PetscDTNodeTypes, (PetscEnum)nodeType, (PetscEnum *)&nodeType, &flg);CHKERRQ(ierr);
979   ierr = PetscOptionsBool("-petscdualspace_lagrange_node_endpoints", "Flag for nodes that include endpoints", "PetscDualSpaceLagrangeSetNodeType", nodeEndpoints, &nodeEndpoints, &flg2);CHKERRQ(ierr);
980   flg3 = PETSC_FALSE;
981   if (nodeType == PETSCDTNODES_GAUSSJACOBI) {
982     ierr = PetscOptionsReal("-petscdualspace_lagrange_node_exponent", "Gauss-Jacobi weight function exponent", "PetscDualSpaceLagrangeSetNodeType", nodeExponent, &nodeExponent, &flg3);CHKERRQ(ierr);
983   }
984   if (flg || flg2 || flg3) {ierr = PetscDualSpaceLagrangeSetNodeType(sp, nodeType, nodeEndpoints, nodeExponent);CHKERRQ(ierr);}
985   ierr = PetscOptionsTail();CHKERRQ(ierr);
986   PetscFunctionReturn(0);
987 }
988 
989 static PetscErrorCode PetscDualSpaceDuplicate_Lagrange(PetscDualSpace sp, PetscDualSpace spNew)
990 {
991   PetscBool           cont, tensor, trimmed, boundary;
992   PetscDTNodeType     nodeType;
993   PetscReal           exponent;
994   PetscDualSpace_Lag *lag    = (PetscDualSpace_Lag *) sp->data;
995   PetscErrorCode      ierr;
996 
997   PetscFunctionBegin;
998   ierr = PetscDualSpaceLagrangeGetContinuity(sp, &cont);CHKERRQ(ierr);
999   ierr = PetscDualSpaceLagrangeSetContinuity(spNew, cont);CHKERRQ(ierr);
1000   ierr = PetscDualSpaceLagrangeGetTensor(sp, &tensor);CHKERRQ(ierr);
1001   ierr = PetscDualSpaceLagrangeSetTensor(spNew, tensor);CHKERRQ(ierr);
1002   ierr = PetscDualSpaceLagrangeGetTrimmed(sp, &trimmed);CHKERRQ(ierr);
1003   ierr = PetscDualSpaceLagrangeSetTrimmed(spNew, trimmed);CHKERRQ(ierr);
1004   ierr = PetscDualSpaceLagrangeGetNodeType(sp, &nodeType, &boundary, &exponent);CHKERRQ(ierr);
1005   ierr = PetscDualSpaceLagrangeSetNodeType(spNew, nodeType, boundary, exponent);CHKERRQ(ierr);
1006   if (lag->nodeFamily) {
1007     PetscDualSpace_Lag *lagnew = (PetscDualSpace_Lag *) spNew->data;
1008 
1009     ierr = Petsc1DNodeFamilyReference(lag->nodeFamily);CHKERRQ(ierr);
1010     lagnew->nodeFamily = lag->nodeFamily;
1011   }
1012   PetscFunctionReturn(0);
1013 }
1014 
1015 /* for making tensor product spaces: take a dual space and product a segment space that has all the same
1016  * specifications (trimmed, continuous, order, node set), except for the form degree */
1017 static PetscErrorCode PetscDualSpaceCreateEdgeSubspace_Lagrange(PetscDualSpace sp, PetscInt order, PetscInt k, PetscInt Nc, PetscBool interiorOnly, PetscDualSpace *bdsp)
1018 {
1019   DM                 K;
1020   PetscDualSpace_Lag *newlag;
1021   PetscErrorCode     ierr;
1022 
1023   PetscFunctionBegin;
1024   ierr = PetscDualSpaceDuplicate(sp,bdsp);CHKERRQ(ierr);
1025   ierr = PetscDualSpaceSetFormDegree(*bdsp, k);CHKERRQ(ierr);
1026   ierr = PetscDualSpaceCreateReferenceCell(*bdsp, 1, PETSC_TRUE, &K);CHKERRQ(ierr);
1027   ierr = PetscDualSpaceSetDM(*bdsp, K);CHKERRQ(ierr);
1028   ierr = DMDestroy(&K);CHKERRQ(ierr);
1029   ierr = PetscDualSpaceSetOrder(*bdsp, order);CHKERRQ(ierr);
1030   ierr = PetscDualSpaceSetNumComponents(*bdsp, Nc);CHKERRQ(ierr);
1031   newlag = (PetscDualSpace_Lag *) (*bdsp)->data;
1032   newlag->interiorOnly = interiorOnly;
1033   ierr = PetscDualSpaceSetUp(*bdsp);CHKERRQ(ierr);
1034   PetscFunctionReturn(0);
1035 }
1036 
1037 /* just the points, weights aren't handled */
1038 static PetscErrorCode PetscQuadratureCreateTensor(PetscQuadrature trace, PetscQuadrature fiber, PetscQuadrature *product)
1039 {
1040   PetscInt         dimTrace, dimFiber;
1041   PetscInt         numPointsTrace, numPointsFiber;
1042   PetscInt         dim, numPoints;
1043   const PetscReal *pointsTrace;
1044   const PetscReal *pointsFiber;
1045   PetscReal       *points;
1046   PetscInt         i, j, k, p;
1047   PetscErrorCode   ierr;
1048 
1049   PetscFunctionBegin;
1050   ierr = PetscQuadratureGetData(trace, &dimTrace, NULL, &numPointsTrace, &pointsTrace, NULL);CHKERRQ(ierr);
1051   ierr = PetscQuadratureGetData(fiber, &dimFiber, NULL, &numPointsFiber, &pointsFiber, NULL);CHKERRQ(ierr);
1052   dim = dimTrace + dimFiber;
1053   numPoints = numPointsFiber * numPointsTrace;
1054   ierr = PetscMalloc1(numPoints * dim, &points);CHKERRQ(ierr);
1055   for (p = 0, j = 0; j < numPointsFiber; j++) {
1056     for (i = 0; i < numPointsTrace; i++, p++) {
1057       for (k = 0; k < dimTrace; k++) points[p * dim +            k] = pointsTrace[i * dimTrace + k];
1058       for (k = 0; k < dimFiber; k++) points[p * dim + dimTrace + k] = pointsFiber[j * dimFiber + k];
1059     }
1060   }
1061   ierr = PetscQuadratureCreate(PETSC_COMM_SELF, product);CHKERRQ(ierr);
1062   ierr = PetscQuadratureSetData(*product, dim, 0, numPoints, points, NULL);CHKERRQ(ierr);
1063   PetscFunctionReturn(0);
1064 }
1065 
1066 /* Kronecker tensor product where matrix is considered a matrix of k-forms, so that
1067  * the entries in the product matrix are wedge products of the entries in the original matrices */
1068 static PetscErrorCode MatTensorAltV(Mat trace, Mat fiber, PetscInt dimTrace, PetscInt kTrace, PetscInt dimFiber, PetscInt kFiber, Mat *product)
1069 {
1070   PetscInt mTrace, nTrace, mFiber, nFiber, m, n, k, i, j, l;
1071   PetscInt dim, NkTrace, NkFiber, Nk;
1072   PetscInt dT, dF;
1073   PetscInt *nnzTrace, *nnzFiber, *nnz;
1074   PetscInt iT, iF, jT, jF, il, jl;
1075   PetscReal *workT, *workT2, *workF, *workF2, *work, *workstar;
1076   PetscReal *projT, *projF;
1077   PetscReal *projTstar, *projFstar;
1078   PetscReal *wedgeMat;
1079   PetscReal sign;
1080   PetscScalar *workS;
1081   Mat prod;
1082   /* this produces dof groups that look like the identity */
1083   PetscErrorCode ierr;
1084 
1085   PetscFunctionBegin;
1086   ierr = MatGetSize(trace, &mTrace, &nTrace);CHKERRQ(ierr);
1087   ierr = PetscDTBinomialInt(dimTrace, PetscAbsInt(kTrace), &NkTrace);CHKERRQ(ierr);
1088   if (nTrace % NkTrace) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "point value space of trace matrix is not a multiple of k-form size");
1089   ierr = MatGetSize(fiber, &mFiber, &nFiber);CHKERRQ(ierr);
1090   ierr = PetscDTBinomialInt(dimFiber, PetscAbsInt(kFiber), &NkFiber);CHKERRQ(ierr);
1091   if (nFiber % NkFiber) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "point value space of fiber matrix is not a multiple of k-form size");
1092   ierr = PetscMalloc2(mTrace, &nnzTrace, mFiber, &nnzFiber);CHKERRQ(ierr);
1093   for (i = 0; i < mTrace; i++) {
1094     ierr = MatGetRow(trace, i, &(nnzTrace[i]), NULL, NULL);CHKERRQ(ierr);
1095     if (nnzTrace[i] % NkTrace) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "nonzeros in trace matrix are not in k-form size blocks");
1096   }
1097   for (i = 0; i < mFiber; i++) {
1098     ierr = MatGetRow(fiber, i, &(nnzFiber[i]), NULL, NULL);CHKERRQ(ierr);
1099     if (nnzFiber[i] % NkFiber) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "nonzeros in fiber matrix are not in k-form size blocks");
1100   }
1101   dim = dimTrace + dimFiber;
1102   k = kFiber + kTrace;
1103   ierr = PetscDTBinomialInt(dim, PetscAbsInt(k), &Nk);CHKERRQ(ierr);
1104   m = mTrace * mFiber;
1105   ierr = PetscMalloc1(m, &nnz);CHKERRQ(ierr);
1106   for (l = 0, j = 0; j < mFiber; j++) for (i = 0; i < mTrace; i++, l++) nnz[l] = (nnzTrace[i] / NkTrace) * (nnzFiber[j] / NkFiber) * Nk;
1107   n = (nTrace / NkTrace) * (nFiber / NkFiber) * Nk;
1108   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF, m, n, 0, nnz, &prod);CHKERRQ(ierr);
1109   ierr = PetscFree(nnz);CHKERRQ(ierr);
1110   ierr = PetscFree2(nnzTrace,nnzFiber);CHKERRQ(ierr);
1111   /* reasoning about which points each dof needs depends on having zeros computed at points preserved */
1112   ierr = MatSetOption(prod, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);
1113   /* compute pullbacks */
1114   ierr = PetscDTBinomialInt(dim, PetscAbsInt(kTrace), &dT);CHKERRQ(ierr);
1115   ierr = PetscDTBinomialInt(dim, PetscAbsInt(kFiber), &dF);CHKERRQ(ierr);
1116   ierr = PetscMalloc4(dimTrace * dim, &projT, dimFiber * dim, &projF, dT * NkTrace, &projTstar, dF * NkFiber, &projFstar);CHKERRQ(ierr);
1117   ierr = PetscArrayzero(projT, dimTrace * dim);CHKERRQ(ierr);
1118   for (i = 0; i < dimTrace; i++) projT[i * (dim + 1)] = 1.;
1119   ierr = PetscArrayzero(projF, dimFiber * dim);CHKERRQ(ierr);
1120   for (i = 0; i < dimFiber; i++) projF[i * (dim + 1) + dimTrace] = 1.;
1121   ierr = PetscDTAltVPullbackMatrix(dim, dimTrace, projT, kTrace, projTstar);CHKERRQ(ierr);
1122   ierr = PetscDTAltVPullbackMatrix(dim, dimFiber, projF, kFiber, projFstar);CHKERRQ(ierr);
1123   ierr = PetscMalloc5(dT, &workT, dF, &workF, Nk, &work, Nk, &workstar, Nk, &workS);CHKERRQ(ierr);
1124   ierr = PetscMalloc2(dT, &workT2, dF, &workF2);CHKERRQ(ierr);
1125   ierr = PetscMalloc1(Nk * dT, &wedgeMat);CHKERRQ(ierr);
1126   sign = (PetscAbsInt(kTrace * kFiber) & 1) ? -1. : 1.;
1127   for (i = 0, iF = 0; iF < mFiber; iF++) {
1128     PetscInt           ncolsF, nformsF;
1129     const PetscInt    *colsF;
1130     const PetscScalar *valsF;
1131 
1132     ierr = MatGetRow(fiber, iF, &ncolsF, &colsF, &valsF);CHKERRQ(ierr);
1133     nformsF = ncolsF / NkFiber;
1134     for (iT = 0; iT < mTrace; iT++, i++) {
1135       PetscInt           ncolsT, nformsT;
1136       const PetscInt    *colsT;
1137       const PetscScalar *valsT;
1138 
1139       ierr = MatGetRow(trace, iT, &ncolsT, &colsT, &valsT);CHKERRQ(ierr);
1140       nformsT = ncolsT / NkTrace;
1141       for (j = 0, jF = 0; jF < nformsF; jF++) {
1142         PetscInt colF = colsF[jF * NkFiber] / NkFiber;
1143 
1144         for (il = 0; il < dF; il++) {
1145           PetscReal val = 0.;
1146           for (jl = 0; jl < NkFiber; jl++) val += projFstar[il * NkFiber + jl] * PetscRealPart(valsF[jF * NkFiber + jl]);
1147           workF[il] = val;
1148         }
1149         if (kFiber < 0) {
1150           for (il = 0; il < dF; il++) workF2[il] = workF[il];
1151           ierr = PetscDTAltVStar(dim, PetscAbsInt(kFiber), 1, workF2, workF);CHKERRQ(ierr);
1152         }
1153         ierr = PetscDTAltVWedgeMatrix(dim, PetscAbsInt(kFiber), PetscAbsInt(kTrace), workF, wedgeMat);CHKERRQ(ierr);
1154         for (jT = 0; jT < nformsT; jT++, j++) {
1155           PetscInt colT = colsT[jT * NkTrace] / NkTrace;
1156           PetscInt col = colF * (nTrace / NkTrace) + colT;
1157           const PetscScalar *vals;
1158 
1159           for (il = 0; il < dT; il++) {
1160             PetscReal val = 0.;
1161             for (jl = 0; jl < NkTrace; jl++) val += projTstar[il * NkTrace + jl] * PetscRealPart(valsT[jT * NkTrace + jl]);
1162             workT[il] = val;
1163           }
1164           if (kTrace < 0) {
1165             for (il = 0; il < dT; il++) workT2[il] = workT[il];
1166             ierr = PetscDTAltVStar(dim, PetscAbsInt(kTrace), 1, workT2, workT);CHKERRQ(ierr);
1167           }
1168 
1169           for (il = 0; il < Nk; il++) {
1170             PetscReal val = 0.;
1171             for (jl = 0; jl < dT; jl++) val += sign * wedgeMat[il * dT + jl] * workT[jl];
1172             work[il] = val;
1173           }
1174           if (k < 0) {
1175             ierr = PetscDTAltVStar(dim, PetscAbsInt(k), -1, work, workstar);CHKERRQ(ierr);
1176 #if defined(PETSC_USE_COMPLEX)
1177             for (l = 0; l < Nk; l++) workS[l] = workstar[l];
1178             vals = &workS[0];
1179 #else
1180             vals = &workstar[0];
1181 #endif
1182           } else {
1183 #if defined(PETSC_USE_COMPLEX)
1184             for (l = 0; l < Nk; l++) workS[l] = work[l];
1185             vals = &workS[0];
1186 #else
1187             vals = &work[0];
1188 #endif
1189           }
1190           for (l = 0; l < Nk; l++) {
1191             ierr = MatSetValue(prod, i, col * Nk + l, vals[l], INSERT_VALUES);CHKERRQ(ierr);
1192           } /* Nk */
1193         } /* jT */
1194       } /* jF */
1195       ierr = MatRestoreRow(trace, iT, &ncolsT, &colsT, &valsT);CHKERRQ(ierr);
1196     } /* iT */
1197     ierr = MatRestoreRow(fiber, iF, &ncolsF, &colsF, &valsF);CHKERRQ(ierr);
1198   } /* iF */
1199   ierr = PetscFree(wedgeMat);CHKERRQ(ierr);
1200   ierr = PetscFree4(projT, projF, projTstar, projFstar);CHKERRQ(ierr);
1201   ierr = PetscFree2(workT2, workF2);CHKERRQ(ierr);
1202   ierr = PetscFree5(workT, workF, work, workstar, workS);CHKERRQ(ierr);
1203   ierr = MatAssemblyBegin(prod, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1204   ierr = MatAssemblyEnd(prod, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1205   *product = prod;
1206   PetscFunctionReturn(0);
1207 }
1208 
1209 /* Union of quadrature points, with an attempt to identify commont points in the two sets */
1210 static PetscErrorCode PetscQuadraturePointsMerge(PetscQuadrature quadA, PetscQuadrature quadB, PetscQuadrature *quadJoint, PetscInt *aToJoint[], PetscInt *bToJoint[])
1211 {
1212   PetscInt         dimA, dimB;
1213   PetscInt         nA, nB, nJoint, i, j, d;
1214   const PetscReal *pointsA;
1215   const PetscReal *pointsB;
1216   PetscReal       *pointsJoint;
1217   PetscInt        *aToJ, *bToJ;
1218   PetscQuadrature  qJ;
1219   PetscErrorCode   ierr;
1220 
1221   PetscFunctionBegin;
1222   ierr = PetscQuadratureGetData(quadA, &dimA, NULL, &nA, &pointsA, NULL);CHKERRQ(ierr);
1223   ierr = PetscQuadratureGetData(quadB, &dimB, NULL, &nB, &pointsB, NULL);CHKERRQ(ierr);
1224   if (dimA != dimB) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Quadrature points must be in the same dimension");
1225   nJoint = nA;
1226   ierr = PetscMalloc1(nA, &aToJ);CHKERRQ(ierr);
1227   for (i = 0; i < nA; i++) aToJ[i] = i;
1228   ierr = PetscMalloc1(nB, &bToJ);CHKERRQ(ierr);
1229   for (i = 0; i < nB; i++) {
1230     for (j = 0; j < nA; j++) {
1231       bToJ[i] = -1;
1232       for (d = 0; d < dimA; d++) if (PetscAbsReal(pointsB[i * dimA + d] - pointsA[j * dimA + d]) > PETSC_SMALL) break;
1233       if (d == dimA) {
1234         bToJ[i] = j;
1235         break;
1236       }
1237     }
1238     if (bToJ[i] == -1) {
1239       bToJ[i] = nJoint++;
1240     }
1241   }
1242   *aToJoint = aToJ;
1243   *bToJoint = bToJ;
1244   ierr = PetscMalloc1(nJoint * dimA, &pointsJoint);CHKERRQ(ierr);
1245   ierr = PetscArraycpy(pointsJoint, pointsA, nA * dimA);CHKERRQ(ierr);
1246   for (i = 0; i < nB; i++) {
1247     if (bToJ[i] >= nA) {
1248       for (d = 0; d < dimA; d++) pointsJoint[bToJ[i] * dimA + d] = pointsB[i * dimA + d];
1249     }
1250   }
1251   ierr = PetscQuadratureCreate(PETSC_COMM_SELF, &qJ);CHKERRQ(ierr);
1252   ierr = PetscQuadratureSetData(qJ, dimA, 0, nJoint, pointsJoint, NULL);CHKERRQ(ierr);
1253   *quadJoint = qJ;
1254   PetscFunctionReturn(0);
1255 }
1256 
1257 /* Matrices matA and matB are both quadrature -> dof matrices: produce a matrix that is joint quadrature -> union of
1258  * dofs, where the joint quadrature was produced by PetscQuadraturePointsMerge */
1259 static PetscErrorCode MatricesMerge(Mat matA, Mat matB, PetscInt dim, PetscInt k, PetscInt numMerged, const PetscInt aToMerged[], const PetscInt bToMerged[], Mat *matMerged)
1260 {
1261   PetscInt m, n, mA, nA, mB, nB, Nk, i, j, l;
1262   Mat      M;
1263   PetscInt *nnz;
1264   PetscInt maxnnz;
1265   PetscInt *work;
1266   PetscErrorCode ierr;
1267 
1268   PetscFunctionBegin;
1269   ierr = PetscDTBinomialInt(dim, PetscAbsInt(k), &Nk);CHKERRQ(ierr);
1270   ierr = MatGetSize(matA, &mA, &nA);CHKERRQ(ierr);
1271   if (nA % Nk) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "matA column space not a multiple of k-form size");
1272   ierr = MatGetSize(matB, &mB, &nB);CHKERRQ(ierr);
1273   if (nB % Nk) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "matB column space not a multiple of k-form size");
1274   m = mA + mB;
1275   n = numMerged * Nk;
1276   ierr = PetscMalloc1(m, &nnz);CHKERRQ(ierr);
1277   maxnnz = 0;
1278   for (i = 0; i < mA; i++) {
1279     ierr = MatGetRow(matA, i, &(nnz[i]), NULL, NULL);CHKERRQ(ierr);
1280     if (nnz[i] % Nk) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "nonzeros in matA are not in k-form size blocks");
1281     maxnnz = PetscMax(maxnnz, nnz[i]);
1282   }
1283   for (i = 0; i < mB; i++) {
1284     ierr = MatGetRow(matB, i, &(nnz[i+mA]), NULL, NULL);CHKERRQ(ierr);
1285     if (nnz[i+mA] % Nk) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "nonzeros in matB are not in k-form size blocks");
1286     maxnnz = PetscMax(maxnnz, nnz[i+mA]);
1287   }
1288   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF, m, n, 0, nnz, &M);CHKERRQ(ierr);
1289   ierr = PetscFree(nnz);CHKERRQ(ierr);
1290   /* reasoning about which points each dof needs depends on having zeros computed at points preserved */
1291   ierr = MatSetOption(M, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);
1292   ierr = PetscMalloc1(maxnnz, &work);CHKERRQ(ierr);
1293   for (i = 0; i < mA; i++) {
1294     const PetscInt *cols;
1295     const PetscScalar *vals;
1296     PetscInt nCols;
1297     ierr = MatGetRow(matA, i, &nCols, &cols, &vals);CHKERRQ(ierr);
1298     for (j = 0; j < nCols / Nk; j++) {
1299       PetscInt newCol = aToMerged[cols[j * Nk] / Nk];
1300       for (l = 0; l < Nk; l++) work[j * Nk + l] = newCol * Nk + l;
1301     }
1302     ierr = MatSetValuesBlocked(M, 1, &i, nCols, work, vals, INSERT_VALUES);CHKERRQ(ierr);
1303     ierr = MatRestoreRow(matA, i, &nCols, &cols, &vals);CHKERRQ(ierr);
1304   }
1305   for (i = 0; i < mB; i++) {
1306     const PetscInt *cols;
1307     const PetscScalar *vals;
1308 
1309     PetscInt row = i + mA;
1310     PetscInt nCols;
1311     ierr = MatGetRow(matB, i, &nCols, &cols, &vals);CHKERRQ(ierr);
1312     for (j = 0; j < nCols / Nk; j++) {
1313       PetscInt newCol = bToMerged[cols[j * Nk] / Nk];
1314       for (l = 0; l < Nk; l++) work[j * Nk + l] = newCol * Nk + l;
1315     }
1316     ierr = MatSetValuesBlocked(M, 1, &row, nCols, work, vals, INSERT_VALUES);CHKERRQ(ierr);
1317     ierr = MatRestoreRow(matB, i, &nCols, &cols, &vals);CHKERRQ(ierr);
1318   }
1319   ierr = PetscFree(work);CHKERRQ(ierr);
1320   ierr = MatAssemblyBegin(M, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1321   ierr = MatAssemblyEnd(M, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1322   *matMerged = M;
1323   PetscFunctionReturn(0);
1324 }
1325 
1326 /* Take a dual space and product a segment space that has all the same specifications (trimmed, continuous, order,
1327  * node set), except for the form degree.  For computing boundary dofs and for making tensor product spaces */
1328 static PetscErrorCode PetscDualSpaceCreateFacetSubspace_Lagrange(PetscDualSpace sp, DM K, PetscInt f, PetscInt k, PetscInt Ncopies, PetscBool interiorOnly, PetscDualSpace *bdsp)
1329 {
1330   PetscInt           Nknew, Ncnew;
1331   PetscInt           dim, pointDim = -1;
1332   PetscInt           depth;
1333   DM                 dm;
1334   PetscDualSpace_Lag *newlag;
1335   PetscErrorCode     ierr;
1336 
1337   PetscFunctionBegin;
1338   ierr = PetscDualSpaceGetDM(sp,&dm);CHKERRQ(ierr);
1339   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
1340   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
1341   ierr = PetscDualSpaceDuplicate(sp,bdsp);CHKERRQ(ierr);
1342   ierr = PetscDualSpaceSetFormDegree(*bdsp,k);CHKERRQ(ierr);
1343   if (!K) {
1344     PetscBool isSimplex;
1345 
1346 
1347     if (depth == dim) {
1348       PetscInt coneSize;
1349 
1350       pointDim = dim - 1;
1351       ierr = DMPlexGetConeSize(dm,f,&coneSize);CHKERRQ(ierr);
1352       isSimplex = (PetscBool) (coneSize == dim);
1353       ierr = PetscDualSpaceCreateReferenceCell(*bdsp, dim-1, isSimplex, &K);CHKERRQ(ierr);
1354     } else if (depth == 1) {
1355       pointDim = 0;
1356       ierr = PetscDualSpaceCreateReferenceCell(*bdsp, 0, PETSC_TRUE, &K);CHKERRQ(ierr);
1357     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unsupported interpolation state of reference element");
1358   } else {
1359     ierr = PetscObjectReference((PetscObject)K);CHKERRQ(ierr);
1360     ierr = DMGetDimension(K, &pointDim);CHKERRQ(ierr);
1361   }
1362   ierr = PetscDualSpaceSetDM(*bdsp, K);CHKERRQ(ierr);
1363   ierr = DMDestroy(&K);CHKERRQ(ierr);
1364   ierr = PetscDTBinomialInt(pointDim, PetscAbsInt(k), &Nknew);CHKERRQ(ierr);
1365   Ncnew = Nknew * Ncopies;
1366   ierr = PetscDualSpaceSetNumComponents(*bdsp, Ncnew);CHKERRQ(ierr);
1367   newlag = (PetscDualSpace_Lag *) (*bdsp)->data;
1368   newlag->interiorOnly = interiorOnly;
1369   ierr = PetscDualSpaceSetUp(*bdsp);CHKERRQ(ierr);
1370   PetscFunctionReturn(0);
1371 }
1372 
1373 /* Construct simplex nodes from a nodefamily, add Nk dof vectors of length Nk at each node.
1374  * Return the (quadrature, matrix) form of the dofs and the nodeIndices form as well.
1375  *
1376  * Sometimes we want a set of nodes to be contained in the interior of the element,
1377  * even when the node scheme puts nodes on the boundaries.  numNodeSkip tells
1378  * the routine how many "layers" of nodes need to be skipped.
1379  * */
1380 static PetscErrorCode PetscDualSpaceLagrangeCreateSimplexNodeMat(Petsc1DNodeFamily nodeFamily, PetscInt dim, PetscInt sum, PetscInt Nk, PetscInt numNodeSkip, PetscQuadrature *iNodes, Mat *iMat, PetscLagNodeIndices *nodeIndices)
1381 {
1382   PetscReal *extraNodeCoords, *nodeCoords;
1383   PetscInt nNodes, nExtraNodes;
1384   PetscInt i, j, k, extraSum = sum + numNodeSkip * (1 + dim);
1385   PetscQuadrature intNodes;
1386   Mat intMat;
1387   PetscLagNodeIndices ni;
1388   PetscErrorCode ierr;
1389 
1390   PetscFunctionBegin;
1391   ierr = PetscDTBinomialInt(dim + sum, dim, &nNodes);CHKERRQ(ierr);
1392   ierr = PetscDTBinomialInt(dim + extraSum, dim, &nExtraNodes);CHKERRQ(ierr);
1393 
1394   ierr = PetscMalloc1(dim * nExtraNodes, &extraNodeCoords);CHKERRQ(ierr);
1395   ierr = PetscNew(&ni);CHKERRQ(ierr);
1396   ni->nodeIdxDim = dim + 1;
1397   ni->nodeVecDim = Nk;
1398   ni->nNodes = nNodes * Nk;
1399   ni->refct = 1;
1400   ierr = PetscMalloc1(nNodes * Nk * (dim + 1), &(ni->nodeIdx));CHKERRQ(ierr);
1401   ierr = PetscMalloc1(nNodes * Nk * Nk, &(ni->nodeVec));CHKERRQ(ierr);
1402   for (i = 0; i < nNodes; i++) for (j = 0; j < Nk; j++) for (k = 0; k < Nk; k++) ni->nodeVec[(i * Nk + j) * Nk + k] = (j == k) ? 1. : 0.;
1403   ierr = Petsc1DNodeFamilyComputeSimplexNodes(nodeFamily, dim, extraSum, extraNodeCoords);CHKERRQ(ierr);
1404   if (numNodeSkip) {
1405     PetscInt k;
1406     PetscInt *tup;
1407 
1408     ierr = PetscMalloc1(dim * nNodes, &nodeCoords);CHKERRQ(ierr);
1409     ierr = PetscMalloc1(dim + 1, &tup);CHKERRQ(ierr);
1410     for (k = 0; k < nNodes; k++) {
1411       PetscInt j, c;
1412       PetscInt index;
1413 
1414       ierr = PetscDTIndexToBary(dim + 1, sum, k, tup);CHKERRQ(ierr);
1415       for (j = 0; j < dim + 1; j++) tup[j] += numNodeSkip;
1416       for (c = 0; c < Nk; c++) {
1417         for (j = 0; j < dim + 1; j++) {
1418           ni->nodeIdx[(k * Nk + c) * (dim + 1) + j] = tup[j] + 1;
1419         }
1420       }
1421       ierr = PetscDTBaryToIndex(dim + 1, extraSum, tup, &index);CHKERRQ(ierr);
1422       for (j = 0; j < dim; j++) nodeCoords[k * dim + j] = extraNodeCoords[index * dim + j];
1423     }
1424     ierr = PetscFree(tup);CHKERRQ(ierr);
1425     ierr = PetscFree(extraNodeCoords);CHKERRQ(ierr);
1426   } else {
1427     PetscInt k;
1428     PetscInt *tup;
1429 
1430     nodeCoords = extraNodeCoords;
1431     ierr = PetscMalloc1(dim + 1, &tup);CHKERRQ(ierr);
1432     for (k = 0; k < nNodes; k++) {
1433       PetscInt j, c;
1434 
1435       ierr = PetscDTIndexToBary(dim + 1, sum, k, tup);CHKERRQ(ierr);
1436       for (c = 0; c < Nk; c++) {
1437         for (j = 0; j < dim + 1; j++) {
1438           /* barycentric indices can have zeros, but we don't want to push forward zeros because it makes it harder to
1439            * determine which nodes correspond to which under symmetries, so we increase by 1.  This is fine
1440            * because the nodeIdx coordinates don't have any meaning other than helping to identify symmetries */
1441           ni->nodeIdx[(k * Nk + c) * (dim + 1) + j] = tup[j] + 1;
1442         }
1443       }
1444     }
1445     ierr = PetscFree(tup);CHKERRQ(ierr);
1446   }
1447   ierr = PetscQuadratureCreate(PETSC_COMM_SELF, &intNodes);CHKERRQ(ierr);
1448   ierr = PetscQuadratureSetData(intNodes, dim, 0, nNodes, nodeCoords, NULL);CHKERRQ(ierr);
1449   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF, nNodes * Nk, nNodes * Nk, Nk, NULL, &intMat);CHKERRQ(ierr);
1450   ierr = MatSetOption(intMat,MAT_IGNORE_ZERO_ENTRIES,PETSC_FALSE);CHKERRQ(ierr);
1451   for (j = 0; j < nNodes * Nk; j++) {
1452     PetscInt rem = j % Nk;
1453     PetscInt a, aprev = j - rem;
1454     PetscInt anext = aprev + Nk;
1455 
1456     for (a = aprev; a < anext; a++) {
1457       ierr = MatSetValue(intMat, j, a, (a == j) ? 1. : 0., INSERT_VALUES);CHKERRQ(ierr);
1458     }
1459   }
1460   ierr = MatAssemblyBegin(intMat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1461   ierr = MatAssemblyEnd(intMat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1462   *iNodes = intNodes;
1463   *iMat = intMat;
1464   *nodeIndices = ni;
1465   PetscFunctionReturn(0);
1466 }
1467 
1468 /* once the nodeIndices have been created for the interior of the reference cell, and for all of the boundary cells,
1469  * push forward the boudary dofs and concatenate them into the full node indices for the dual space */
1470 static PetscErrorCode PetscDualSpaceLagrangeCreateAllNodeIdx(PetscDualSpace sp)
1471 {
1472   DM             dm;
1473   PetscInt       dim, nDofs;
1474   PetscSection   section;
1475   PetscInt       pStart, pEnd, p;
1476   PetscInt       formDegree, Nk;
1477   PetscInt       nodeIdxDim, spintdim;
1478   PetscDualSpace_Lag *lag;
1479   PetscLagNodeIndices ni, verti;
1480   PetscErrorCode ierr;
1481 
1482   PetscFunctionBegin;
1483   lag = (PetscDualSpace_Lag *) sp->data;
1484   verti = lag->vertIndices;
1485   ierr = PetscDualSpaceGetDM(sp, &dm);CHKERRQ(ierr);
1486   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1487   ierr = PetscDualSpaceGetFormDegree(sp, &formDegree);CHKERRQ(ierr);
1488   ierr = PetscDTBinomialInt(dim, PetscAbsInt(formDegree), &Nk);CHKERRQ(ierr);
1489   ierr = PetscDualSpaceGetSection(sp, &section);CHKERRQ(ierr);
1490   ierr = PetscSectionGetStorageSize(section, &nDofs);CHKERRQ(ierr);
1491   ierr = PetscNew(&ni);CHKERRQ(ierr);
1492   ni->nodeIdxDim = nodeIdxDim = verti->nodeIdxDim;
1493   ni->nodeVecDim = Nk;
1494   ni->nNodes = nDofs;
1495   ni->refct = 1;
1496   ierr = PetscMalloc1(nodeIdxDim * nDofs, &(ni->nodeIdx));CHKERRQ(ierr);
1497   ierr = PetscMalloc1(Nk * nDofs, &(ni->nodeVec));CHKERRQ(ierr);
1498   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
1499   ierr = PetscSectionGetDof(section, 0, &spintdim);CHKERRQ(ierr);
1500   if (spintdim) {
1501     ierr = PetscArraycpy(ni->nodeIdx, lag->intNodeIndices->nodeIdx, spintdim * nodeIdxDim);CHKERRQ(ierr);
1502     ierr = PetscArraycpy(ni->nodeVec, lag->intNodeIndices->nodeVec, spintdim * Nk);CHKERRQ(ierr);
1503   }
1504   for (p = pStart + 1; p < pEnd; p++) {
1505     PetscDualSpace psp = sp->pointSpaces[p];
1506     PetscDualSpace_Lag *plag;
1507     PetscInt dof, off;
1508 
1509     ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1510     if (!dof) continue;
1511     plag = (PetscDualSpace_Lag *) psp->data;
1512     ierr = PetscSectionGetOffset(section, p, &off);CHKERRQ(ierr);
1513     ierr = PetscLagNodeIndicesPushForward(dm, verti, p, plag->vertIndices, plag->intNodeIndices, 0, formDegree, &(ni->nodeIdx[off * nodeIdxDim]), &(ni->nodeVec[off * Nk]));CHKERRQ(ierr);
1514   }
1515   lag->allNodeIndices = ni;
1516   PetscFunctionReturn(0);
1517 }
1518 
1519 /* once the (quadrature, Matrix) forms of the dofs have been created for the interior of the
1520  * reference cell and for the boundary cells, jk
1521  * push forward the boundary data and concatenate them into the full (quadrature, matrix) data
1522  * for the dual space */
1523 static PetscErrorCode PetscDualSpaceCreateAllDataFromInteriorData(PetscDualSpace sp)
1524 {
1525   DM               dm;
1526   PetscSection     section;
1527   PetscInt         pStart, pEnd, p, k, Nk, dim, Nc;
1528   PetscInt         nNodes;
1529   PetscInt         countNodes;
1530   Mat              allMat;
1531   PetscQuadrature  allNodes;
1532   PetscInt         nDofs;
1533   PetscInt         maxNzforms, j;
1534   PetscScalar      *work;
1535   PetscReal        *L, *J, *Jinv, *v0, *pv0;
1536   PetscInt         *iwork;
1537   PetscReal        *nodes;
1538   PetscErrorCode   ierr;
1539 
1540   PetscFunctionBegin;
1541   ierr = PetscDualSpaceGetDM(sp, &dm);CHKERRQ(ierr);
1542   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1543   ierr = PetscDualSpaceGetSection(sp, &section);CHKERRQ(ierr);
1544   ierr = PetscSectionGetStorageSize(section, &nDofs);CHKERRQ(ierr);
1545   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
1546   ierr = PetscDualSpaceGetFormDegree(sp, &k);CHKERRQ(ierr);
1547   ierr = PetscDualSpaceGetNumComponents(sp, &Nc);CHKERRQ(ierr);
1548   ierr = PetscDTBinomialInt(dim, PetscAbsInt(k), &Nk);CHKERRQ(ierr);
1549   for (p = pStart, nNodes = 0, maxNzforms = 0; p < pEnd; p++) {
1550     PetscDualSpace  psp;
1551     DM              pdm;
1552     PetscInt        pdim, pNk;
1553     PetscQuadrature intNodes;
1554     Mat intMat;
1555 
1556     ierr = PetscDualSpaceGetPointSubspace(sp, p, &psp);CHKERRQ(ierr);
1557     if (!psp) continue;
1558     ierr = PetscDualSpaceGetDM(psp, &pdm);CHKERRQ(ierr);
1559     ierr = DMGetDimension(pdm, &pdim);CHKERRQ(ierr);
1560     if (pdim < PetscAbsInt(k)) continue;
1561     ierr = PetscDTBinomialInt(pdim, PetscAbsInt(k), &pNk);CHKERRQ(ierr);
1562     ierr = PetscDualSpaceGetInteriorData(psp, &intNodes, &intMat);CHKERRQ(ierr);
1563     if (intNodes) {
1564       PetscInt nNodesp;
1565 
1566       ierr = PetscQuadratureGetData(intNodes, NULL, NULL, &nNodesp, NULL, NULL);CHKERRQ(ierr);
1567       nNodes += nNodesp;
1568     }
1569     if (intMat) {
1570       PetscInt maxNzsp;
1571       PetscInt maxNzformsp;
1572 
1573       ierr = MatSeqAIJGetMaxRowNonzeros(intMat, &maxNzsp);CHKERRQ(ierr);
1574       if (maxNzsp % pNk) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "interior matrix is not laid out as blocks of k-forms");
1575       maxNzformsp = maxNzsp / pNk;
1576       maxNzforms = PetscMax(maxNzforms, maxNzformsp);
1577     }
1578   }
1579   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF, nDofs, nNodes * Nc, maxNzforms * Nk, NULL, &allMat);CHKERRQ(ierr);
1580   ierr = MatSetOption(allMat,MAT_IGNORE_ZERO_ENTRIES,PETSC_FALSE);CHKERRQ(ierr);
1581   ierr = PetscMalloc7(dim, &v0, dim, &pv0, dim * dim, &J, dim * dim, &Jinv, Nk * Nk, &L, maxNzforms * Nk, &work, maxNzforms * Nk, &iwork);CHKERRQ(ierr);
1582   for (j = 0; j < dim; j++) pv0[j] = -1.;
1583   ierr = PetscMalloc1(dim * nNodes, &nodes);CHKERRQ(ierr);
1584   for (p = pStart, countNodes = 0; p < pEnd; p++) {
1585     PetscDualSpace  psp;
1586     PetscQuadrature intNodes;
1587     DM pdm;
1588     PetscInt pdim, pNk;
1589     PetscInt countNodesIn = countNodes;
1590     PetscReal detJ;
1591     Mat intMat;
1592 
1593     ierr = PetscDualSpaceGetPointSubspace(sp, p, &psp);CHKERRQ(ierr);
1594     if (!psp) continue;
1595     ierr = PetscDualSpaceGetDM(psp, &pdm);CHKERRQ(ierr);
1596     ierr = DMGetDimension(pdm, &pdim);CHKERRQ(ierr);
1597     if (pdim < PetscAbsInt(k)) continue;
1598     ierr = PetscDualSpaceGetInteriorData(psp, &intNodes, &intMat);CHKERRQ(ierr);
1599     if (intNodes == NULL && intMat == NULL) continue;
1600     ierr = PetscDTBinomialInt(pdim, PetscAbsInt(k), &pNk);CHKERRQ(ierr);
1601     if (p) {
1602       ierr = DMPlexComputeCellGeometryAffineFEM(dm, p, v0, J, Jinv, &detJ);CHKERRQ(ierr);
1603     } else { /* identity */
1604       PetscInt i,j;
1605 
1606       for (i = 0; i < dim; i++) for (j = 0; j < dim; j++) J[i * dim + j] = Jinv[i * dim + j] = 0.;
1607       for (i = 0; i < dim; i++) J[i * dim + i] = Jinv[i * dim + i] = 1.;
1608       for (i = 0; i < dim; i++) v0[i] = -1.;
1609     }
1610     if (pdim != dim) { /* compactify Jacobian */
1611       PetscInt i, j;
1612 
1613       for (i = 0; i < dim; i++) for (j = 0; j < pdim; j++) J[i * pdim + j] = J[i * dim + j];
1614     }
1615     ierr = PetscDTAltVPullbackMatrix(pdim, dim, J, k, L);CHKERRQ(ierr);
1616     if (intNodes) { /* push forward quadrature locations by the affine transformation */
1617       PetscInt nNodesp;
1618       const PetscReal *nodesp;
1619       PetscInt j;
1620 
1621       ierr = PetscQuadratureGetData(intNodes, NULL, NULL, &nNodesp, &nodesp, NULL);CHKERRQ(ierr);
1622       for (j = 0; j < nNodesp; j++, countNodes++) {
1623         PetscInt d, e;
1624 
1625         for (d = 0; d < dim; d++) {
1626           nodes[countNodes * dim + d] = v0[d];
1627           for (e = 0; e < pdim; e++) {
1628             nodes[countNodes * dim + d] += J[d * pdim + e] * (nodesp[j * pdim + e] - pv0[e]);
1629           }
1630         }
1631       }
1632     }
1633     if (intMat) {
1634       PetscInt nrows;
1635       PetscInt off;
1636 
1637       ierr = PetscSectionGetDof(section, p, &nrows);CHKERRQ(ierr);
1638       ierr = PetscSectionGetOffset(section, p, &off);CHKERRQ(ierr);
1639       for (j = 0; j < nrows; j++) {
1640         PetscInt ncols;
1641         const PetscInt *cols;
1642         const PetscScalar *vals;
1643         PetscInt l, d, e;
1644         PetscInt row = j + off;
1645 
1646         ierr = MatGetRow(intMat, j, &ncols, &cols, &vals);CHKERRQ(ierr);
1647         if (ncols % pNk) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "interior matrix is not laid out as blocks of k-forms");
1648         for (l = 0; l < ncols / pNk; l++) {
1649           PetscInt blockcol;
1650 
1651           for (d = 0; d < pNk; d++) {
1652             if ((cols[l * pNk + d] % pNk) != d) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "interior matrix is not laid out as blocks of k-forms");
1653           }
1654           blockcol = cols[l * pNk] / pNk;
1655           for (d = 0; d < Nk; d++) {
1656             iwork[l * Nk + d] = (blockcol + countNodesIn) * Nk + d;
1657           }
1658           for (d = 0; d < Nk; d++) work[l * Nk + d] = 0.;
1659           for (d = 0; d < Nk; d++) {
1660             for (e = 0; e < pNk; e++) {
1661               /* "push forward" dof by pulling back a k-form to be evaluated on the point: multiply on the right by L */
1662               work[l * Nk + d] += vals[l * pNk + e] * L[e * Nk + d];
1663             }
1664           }
1665         }
1666         ierr = MatSetValues(allMat, 1, &row, (ncols / pNk) * Nk, iwork, work, INSERT_VALUES);CHKERRQ(ierr);
1667         ierr = MatRestoreRow(intMat, j, &ncols, &cols, &vals);CHKERRQ(ierr);
1668       }
1669     }
1670   }
1671   ierr = MatAssemblyBegin(allMat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1672   ierr = MatAssemblyEnd(allMat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1673   ierr = PetscQuadratureCreate(PETSC_COMM_SELF, &allNodes);CHKERRQ(ierr);
1674   ierr = PetscQuadratureSetData(allNodes, dim, 0, nNodes, nodes, NULL);CHKERRQ(ierr);
1675   ierr = PetscFree7(v0, pv0, J, Jinv, L, work, iwork);CHKERRQ(ierr);
1676   ierr = MatDestroy(&(sp->allMat));CHKERRQ(ierr);
1677   sp->allMat = allMat;
1678   ierr = PetscQuadratureDestroy(&(sp->allNodes));CHKERRQ(ierr);
1679   sp->allNodes = allNodes;
1680   PetscFunctionReturn(0);
1681 }
1682 
1683 /* rather than trying to get all data from the functionals, we create
1684  * the functionals from rows of the quadrature -> dof matrix.
1685  *
1686  * Ideally most of the uses of PetscDualSpace in PetscFE will switch
1687  * to using intMat and allMat, so that the individual functionals
1688  * don't need to be constructed at all */
1689 static PetscErrorCode PetscDualSpaceComputeFunctionalsFromAllData(PetscDualSpace sp)
1690 {
1691   PetscQuadrature allNodes;
1692   Mat             allMat;
1693   PetscInt        nDofs;
1694   PetscInt        dim, k, Nk, Nc, f;
1695   DM              dm;
1696   PetscInt        nNodes, spdim;
1697   const PetscReal *nodes = NULL;
1698   PetscSection    section;
1699   PetscErrorCode  ierr;
1700 
1701   PetscFunctionBegin;
1702   ierr = PetscDualSpaceGetDM(sp, &dm);CHKERRQ(ierr);
1703   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1704   ierr = PetscDualSpaceGetNumComponents(sp, &Nc);CHKERRQ(ierr);
1705   ierr = PetscDualSpaceGetFormDegree(sp, &k);CHKERRQ(ierr);
1706   ierr = PetscDTBinomialInt(dim, PetscAbsInt(k), &Nk);CHKERRQ(ierr);
1707   ierr = PetscDualSpaceGetAllData(sp, &allNodes, &allMat);CHKERRQ(ierr);
1708   nNodes = 0;
1709   if (allNodes) {
1710     ierr = PetscQuadratureGetData(allNodes, NULL, NULL, &nNodes, &nodes, NULL);CHKERRQ(ierr);
1711   }
1712   ierr = MatGetSize(allMat, &nDofs, NULL);CHKERRQ(ierr);
1713   ierr = PetscDualSpaceGetSection(sp, &section);CHKERRQ(ierr);
1714   ierr = PetscSectionGetStorageSize(section, &spdim);CHKERRQ(ierr);
1715   if (spdim != nDofs) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "incompatible all matrix size");
1716   ierr = PetscMalloc1(nDofs, &(sp->functional));CHKERRQ(ierr);
1717   for (f = 0; f < nDofs; f++) {
1718     PetscInt ncols, c;
1719     const PetscInt *cols;
1720     const PetscScalar *vals;
1721     PetscReal *nodesf;
1722     PetscReal *weightsf;
1723     PetscInt nNodesf;
1724     PetscInt countNodes;
1725 
1726     ierr = MatGetRow(allMat, f, &ncols, &cols, &vals);CHKERRQ(ierr);
1727     if (ncols % Nk) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "all matrix is not laid out as blocks of k-forms");
1728     for (c = 1, nNodesf = 1; c < ncols; c++) {
1729       if ((cols[c] / Nc) != (cols[c-1] / Nc)) nNodesf++;
1730     }
1731     ierr = PetscMalloc1(dim * nNodesf, &nodesf);CHKERRQ(ierr);
1732     ierr = PetscMalloc1(Nc * nNodesf, &weightsf);CHKERRQ(ierr);
1733     for (c = 0, countNodes = 0; c < ncols; c++) {
1734       if (!c || ((cols[c] / Nc) != (cols[c-1] / Nc))) {
1735         PetscInt d;
1736 
1737         for (d = 0; d < Nc; d++) {
1738           weightsf[countNodes * Nc + d] = 0.;
1739         }
1740         for (d = 0; d < dim; d++) {
1741           nodesf[countNodes * dim + d] = nodes[(cols[c] / Nc) * dim + d];
1742         }
1743         countNodes++;
1744       }
1745       weightsf[(countNodes - 1) * Nc + (cols[c] % Nc)] = PetscRealPart(vals[c]);
1746     }
1747     ierr = PetscQuadratureCreate(PETSC_COMM_SELF, &(sp->functional[f]));CHKERRQ(ierr);
1748     ierr = PetscQuadratureSetData(sp->functional[f], dim, Nc, nNodesf, nodesf, weightsf);CHKERRQ(ierr);
1749     ierr = MatRestoreRow(allMat, f, &ncols, &cols, &vals);CHKERRQ(ierr);
1750   }
1751   PetscFunctionReturn(0);
1752 }
1753 
1754 /* take a matrix meant for k-forms and expand it to one for Ncopies */
1755 static PetscErrorCode PetscDualSpaceLagrangeMatrixCreateCopies(Mat A, PetscInt Nk, PetscInt Ncopies, Mat *Abs)
1756 {
1757   PetscInt       m, n, i, j, k;
1758   PetscInt       maxnnz, *nnz, *iwork;
1759   Mat            Ac;
1760   PetscErrorCode ierr;
1761 
1762   PetscFunctionBegin;
1763   ierr = MatGetSize(A, &m, &n);CHKERRQ(ierr);
1764   if (n % Nk) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of columns in A %D is not a multiple of Nk %D", n, Nk);
1765   ierr = PetscMalloc1(m * Ncopies, &nnz);CHKERRQ(ierr);
1766   for (i = 0, maxnnz = 0; i < m; i++) {
1767     PetscInt innz;
1768     ierr = MatGetRow(A, i, &innz, NULL, NULL);CHKERRQ(ierr);
1769     if (innz % Nk) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "A row %D nnzs is not a multiple of Nk %D", innz, Nk);
1770     for (j = 0; j < Ncopies; j++) nnz[i * Ncopies + j] = innz;
1771     maxnnz = PetscMax(maxnnz, innz);
1772   }
1773   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF, m * Ncopies, n * Ncopies, 0, nnz, &Ac);CHKERRQ(ierr);
1774   ierr = MatSetOption(Ac, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);
1775   ierr = PetscFree(nnz);CHKERRQ(ierr);
1776   ierr = PetscMalloc1(maxnnz, &iwork);CHKERRQ(ierr);
1777   for (i = 0; i < m; i++) {
1778     PetscInt innz;
1779     const PetscInt    *cols;
1780     const PetscScalar *vals;
1781 
1782     ierr = MatGetRow(A, i, &innz, &cols, &vals);CHKERRQ(ierr);
1783     for (j = 0; j < innz; j++) iwork[j] = (cols[j] / Nk) * (Nk * Ncopies) + (cols[j] % Nk);
1784     for (j = 0; j < Ncopies; j++) {
1785       PetscInt row = i * Ncopies + j;
1786 
1787       ierr = MatSetValues(Ac, 1, &row, innz, iwork, vals, INSERT_VALUES);CHKERRQ(ierr);
1788       for (k = 0; k < innz; k++) iwork[k] += Nk;
1789     }
1790     ierr = MatRestoreRow(A, i, &innz, &cols, &vals);CHKERRQ(ierr);
1791   }
1792   ierr = PetscFree(iwork);CHKERRQ(ierr);
1793   ierr = MatAssemblyBegin(Ac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1794   ierr = MatAssemblyEnd(Ac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1795   *Abs = Ac;
1796   PetscFunctionReturn(0);
1797 }
1798 
1799 /* check if a cell is a tensor product of the segment with a facet,
1800  * specifically checking if f and f2 can be the "endpoints" (like the triangles
1801  * at either end of a wedge) */
1802 static PetscErrorCode DMPlexPointIsTensor_Internal_Given(DM dm, PetscInt p, PetscInt f, PetscInt f2, PetscBool *isTensor)
1803 {
1804   PetscInt        coneSize, c;
1805   const PetscInt *cone;
1806   const PetscInt *fCone;
1807   const PetscInt *f2Cone;
1808   PetscInt        fs[2];
1809   PetscInt        meetSize, nmeet;
1810   const PetscInt *meet;
1811   PetscErrorCode  ierr;
1812 
1813   PetscFunctionBegin;
1814   fs[0] = f;
1815   fs[1] = f2;
1816   ierr = DMPlexGetMeet(dm, 2, fs, &meetSize, &meet);CHKERRQ(ierr);
1817   nmeet = meetSize;
1818   ierr = DMPlexRestoreMeet(dm, 2, fs, &meetSize, &meet);CHKERRQ(ierr);
1819   /* two points that have a non-empty meet cannot be at opposite ends of a cell */
1820   if (nmeet) {
1821     *isTensor = PETSC_FALSE;
1822     PetscFunctionReturn(0);
1823   }
1824   ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
1825   ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
1826   ierr = DMPlexGetCone(dm, f, &fCone);CHKERRQ(ierr);
1827   ierr = DMPlexGetCone(dm, f2, &f2Cone);CHKERRQ(ierr);
1828   for (c = 0; c < coneSize; c++) {
1829     PetscInt e, ef;
1830     PetscInt d = -1, d2 = -1;
1831     PetscInt dcount, d2count;
1832     PetscInt t = cone[c];
1833     PetscInt tConeSize;
1834     PetscBool tIsTensor;
1835     const PetscInt *tCone;
1836 
1837     if (t == f || t == f2) continue;
1838     /* for every other facet in the cone, check that is has
1839      * one ridge in common with each end */
1840     ierr = DMPlexGetConeSize(dm, t, &tConeSize);CHKERRQ(ierr);
1841     ierr = DMPlexGetCone(dm, t, &tCone);CHKERRQ(ierr);
1842 
1843     dcount = 0;
1844     d2count = 0;
1845     for (e = 0; e < tConeSize; e++) {
1846       PetscInt q = tCone[e];
1847       for (ef = 0; ef < coneSize - 2; ef++) {
1848         if (fCone[ef] == q) {
1849           if (dcount) {
1850             *isTensor = PETSC_FALSE;
1851             PetscFunctionReturn(0);
1852           }
1853           d = q;
1854           dcount++;
1855         } else if (f2Cone[ef] == q) {
1856           if (d2count) {
1857             *isTensor = PETSC_FALSE;
1858             PetscFunctionReturn(0);
1859           }
1860           d2 = q;
1861           d2count++;
1862         }
1863       }
1864     }
1865     /* if the whole cell is a tensor with the segment, then this
1866      * facet should be a tensor with the segment */
1867     ierr = DMPlexPointIsTensor_Internal_Given(dm, t, d, d2, &tIsTensor);CHKERRQ(ierr);
1868     if (!tIsTensor) {
1869       *isTensor = PETSC_FALSE;
1870       PetscFunctionReturn(0);
1871     }
1872   }
1873   *isTensor = PETSC_TRUE;
1874   PetscFunctionReturn(0);
1875 }
1876 
1877 /* determine if a cell is a tensor with a segment by looping over pairs of facets to find a pair
1878  * that could be the opposite ends */
1879 static PetscErrorCode DMPlexPointIsTensor_Internal(DM dm, PetscInt p, PetscBool *isTensor, PetscInt *endA, PetscInt *endB)
1880 {
1881   PetscInt        coneSize, c, c2;
1882   const PetscInt *cone;
1883   PetscErrorCode  ierr;
1884 
1885   PetscFunctionBegin;
1886   ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
1887   if (!coneSize) {
1888     if (isTensor) *isTensor = PETSC_FALSE;
1889     if (endA) *endA = -1;
1890     if (endB) *endB = -1;
1891   }
1892   ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
1893   for (c = 0; c < coneSize; c++) {
1894     PetscInt f = cone[c];
1895     PetscInt fConeSize;
1896 
1897     ierr = DMPlexGetConeSize(dm, f, &fConeSize);CHKERRQ(ierr);
1898     if (fConeSize != coneSize - 2) continue;
1899 
1900     for (c2 = c + 1; c2 < coneSize; c2++) {
1901       PetscInt  f2 = cone[c2];
1902       PetscBool isTensorff2;
1903       PetscInt f2ConeSize;
1904 
1905       ierr = DMPlexGetConeSize(dm, f2, &f2ConeSize);CHKERRQ(ierr);
1906       if (f2ConeSize != coneSize - 2) continue;
1907 
1908       ierr = DMPlexPointIsTensor_Internal_Given(dm, p, f, f2, &isTensorff2);CHKERRQ(ierr);
1909       if (isTensorff2) {
1910         if (isTensor) *isTensor = PETSC_TRUE;
1911         if (endA) *endA = f;
1912         if (endB) *endB = f2;
1913         PetscFunctionReturn(0);
1914       }
1915     }
1916   }
1917   if (isTensor) *isTensor = PETSC_FALSE;
1918   if (endA) *endA = -1;
1919   if (endB) *endB = -1;
1920   PetscFunctionReturn(0);
1921 }
1922 
1923 /* determine if a cell is a tensor with a segment by looping over pairs of facets to find a pair
1924  * that could be the opposite ends */
1925 static PetscErrorCode DMPlexPointIsTensor(DM dm, PetscInt p, PetscBool *isTensor, PetscInt *endA, PetscInt *endB)
1926 {
1927   DMPlexInterpolatedFlag interpolated;
1928   PetscErrorCode ierr;
1929 
1930   PetscFunctionBegin;
1931   ierr = DMPlexIsInterpolated(dm, &interpolated);CHKERRQ(ierr);
1932   if (interpolated != DMPLEX_INTERPOLATED_FULL) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Only for interpolated DMPlex's");
1933   ierr = DMPlexPointIsTensor_Internal(dm, p, isTensor, endA, endB);CHKERRQ(ierr);
1934   PetscFunctionReturn(0);
1935 }
1936 
1937 /* Let k = formDegree and k' = -sign(k) * dim + k.  Transform a symmetric
1938  * frame for k'-forms on the biunit simplex into a symmetric frame for k-forms
1939  * on the biunit simplex.
1940  *
1941  * A frame is "symmetric" if the pullback every symmetry of the biunit simplex
1942  * is a permutation of the frame.
1943  *
1944  * forms in the symmetric frame are used to as dofs in the
1945  * untrimmed simplex spaces.  This way, symmetries of the reference cell
1946  * result in permutations of dofs grouped by node.
1947  *
1948  * Use T to transform dof matrices as a block diagonal transformation on the right.
1949  */
1950 static PetscErrorCode BiunitSimplexSymmetricFormTransformation(PetscInt dim, PetscInt formDegree, PetscReal T[])
1951 {
1952   PetscInt       k = formDegree;
1953   PetscInt       kd = k < 0 ? dim + k : k - dim;
1954   PetscInt       Nk;
1955   PetscReal      *biToEq, *eqToBi, *biToEqStar, *eqToBiStar;
1956   PetscInt       fact;
1957   PetscErrorCode ierr;
1958 
1959   PetscFunctionBegin;
1960   ierr = PetscDTBinomialInt(dim, PetscAbsInt(k), &Nk);CHKERRQ(ierr);
1961   ierr = PetscCalloc4(dim * dim, &biToEq, dim * dim, &eqToBi, Nk * Nk, &biToEqStar, Nk * Nk, &eqToBiStar);CHKERRQ(ierr);
1962   /* fill in biToEq: Jacobian of the transformation from the biunit simplex to the equilateral simplex */
1963   fact = 0;
1964   for (PetscInt i = 0; i < dim; i++) {
1965     biToEq[i * dim + i] = PetscSqrtReal(((PetscReal)i + 2.) / (2.*((PetscReal)i+1.)));
1966     fact += 4*(i+1);
1967     for (PetscInt j = i+1; j < dim; j++) {
1968       biToEq[i * dim + j] = PetscSqrtReal(1./(PetscReal)fact);
1969     }
1970   }
1971   /* fill in eqToBi: Jacobian of the transformation from the equilateral simplex to the bilateral simplex */
1972   fact = 0;
1973   for (PetscInt j = 0; j < dim; j++) {
1974     eqToBi[j * dim + j] = PetscSqrtReal(2.*((PetscReal)j+1.)/((PetscReal)j+2));
1975     fact += j+1;
1976     for (PetscInt i = 0; i < j; i++) {
1977       eqToBi[i * dim + j] = -PetscSqrtReal(1./(PetscReal)fact);
1978     }
1979   }
1980   ierr = PetscDTAltVPullbackMatrix(dim, dim, biToEq, kd, biToEqStar);CHKERRQ(ierr);
1981   ierr = PetscDTAltVPullbackMatrix(dim, dim, eqToBi, k, eqToBiStar);CHKERRQ(ierr);
1982   for (PetscInt i = 0; i < Nk; i++) {
1983     for (PetscInt j = 0; j < Nk; j++) {
1984       PetscReal val = 0.;
1985       for (PetscInt k = 0; k < Nk; k++) val += biToEqStar[i * Nk + k] * eqToBiStar[k * Nk + j];
1986       T[i * Nk + j] = val;
1987     }
1988   }
1989   ierr = PetscFree4(biToEq, eqToBi, biToEqStar, eqToBiStar);CHKERRQ(ierr);
1990   PetscFunctionReturn(0);
1991 }
1992 
1993 /* permute a quadrature -> dof matrix so that its rows are in revlex order by nodeIdx */
1994 static PetscErrorCode MatPermuteByNodeIdx(Mat A, PetscLagNodeIndices ni, Mat *Aperm)
1995 {
1996   PetscInt       m, n, i, j;
1997   PetscInt       nodeIdxDim = ni->nodeIdxDim;
1998   PetscInt       nodeVecDim = ni->nodeVecDim;
1999   PetscInt       *perm;
2000   IS             permIS;
2001   IS             id;
2002   PetscInt       *nIdxPerm;
2003   PetscReal      *nVecPerm;
2004   PetscErrorCode ierr;
2005 
2006   PetscFunctionBegin;
2007   ierr = PetscLagNodeIndicesGetPermutation(ni, &perm);CHKERRQ(ierr);
2008   ierr = MatGetSize(A, &m, &n);CHKERRQ(ierr);
2009   ierr = PetscMalloc1(nodeIdxDim * m, &nIdxPerm);CHKERRQ(ierr);
2010   ierr = PetscMalloc1(nodeVecDim * m, &nVecPerm);CHKERRQ(ierr);
2011   for (i = 0; i < m; i++) for (j = 0; j < nodeIdxDim; j++) nIdxPerm[i * nodeIdxDim + j] = ni->nodeIdx[perm[i] * nodeIdxDim + j];
2012   for (i = 0; i < m; i++) for (j = 0; j < nodeVecDim; j++) nVecPerm[i * nodeVecDim + j] = ni->nodeVec[perm[i] * nodeVecDim + j];
2013   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, perm, PETSC_USE_POINTER, &permIS);CHKERRQ(ierr);
2014   ierr = ISSetPermutation(permIS);CHKERRQ(ierr);
2015   ierr = ISCreateStride(PETSC_COMM_SELF, n, 0, 1, &id);CHKERRQ(ierr);
2016   ierr = ISSetPermutation(id);CHKERRQ(ierr);
2017   ierr = MatPermute(A, permIS, id, Aperm);CHKERRQ(ierr);
2018   ierr = ISDestroy(&permIS);CHKERRQ(ierr);
2019   ierr = ISDestroy(&id);CHKERRQ(ierr);
2020   for (i = 0; i < m; i++) perm[i] = i;
2021   ierr = PetscFree(ni->nodeIdx);CHKERRQ(ierr);
2022   ierr = PetscFree(ni->nodeVec);CHKERRQ(ierr);
2023   ni->nodeIdx = nIdxPerm;
2024   ni->nodeVec = nVecPerm;
2025   PetscFunctionReturn(0);
2026 }
2027 
2028 static PetscErrorCode PetscDualSpaceSetUp_Lagrange(PetscDualSpace sp)
2029 {
2030   PetscDualSpace_Lag *lag   = (PetscDualSpace_Lag *) sp->data;
2031   DM                  dm    = sp->dm;
2032   DM                  dmint = NULL;
2033   PetscInt            order;
2034   PetscInt            Nc    = sp->Nc;
2035   MPI_Comm            comm;
2036   PetscBool           continuous;
2037   PetscSection        section;
2038   PetscInt            depth, dim, pStart, pEnd, cStart, cEnd, p, *pStratStart, *pStratEnd, d;
2039   PetscInt            formDegree, Nk, Ncopies;
2040   PetscInt            tensorf = -1, tensorf2 = -1;
2041   PetscBool           tensorCell, tensorSpace;
2042   PetscBool           uniform, trimmed;
2043   Petsc1DNodeFamily   nodeFamily;
2044   PetscInt            numNodeSkip;
2045   DMPlexInterpolatedFlag interpolated;
2046   PetscBool           isbdm;
2047   PetscErrorCode      ierr;
2048 
2049   PetscFunctionBegin;
2050   /* step 1: sanitize input */
2051   ierr = PetscObjectGetComm((PetscObject) sp, &comm);CHKERRQ(ierr);
2052   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2053   ierr = PetscObjectTypeCompare((PetscObject)sp, PETSCDUALSPACEBDM, &isbdm);CHKERRQ(ierr);
2054   if (isbdm) {
2055     sp->k = -(dim-1); /* form degree of H-div */
2056     ierr = PetscObjectChangeTypeName((PetscObject)sp, PETSCDUALSPACELAGRANGE);CHKERRQ(ierr);
2057   }
2058   ierr = PetscDualSpaceGetFormDegree(sp, &formDegree);CHKERRQ(ierr);
2059   if (PetscAbsInt(formDegree) > dim) SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Form degree must be bounded by dimension");
2060   ierr = PetscDTBinomialInt(dim,PetscAbsInt(formDegree),&Nk);CHKERRQ(ierr);
2061   if (sp->Nc <= 0 && lag->numCopies > 0) sp->Nc = Nk * lag->numCopies;
2062   Nc = sp->Nc;
2063   if (Nc % Nk) SETERRQ(comm, PETSC_ERR_ARG_INCOMP, "Number of components is not a multiple of form degree size");
2064   if (lag->numCopies <= 0) lag->numCopies = Nc / Nk;
2065   Ncopies = lag->numCopies;
2066   if (Nc / Nk != Ncopies) SETERRQ(comm, PETSC_ERR_ARG_INCOMP, "Number of copies * (dim choose k) != Nc");
2067   if (!dim) sp->order = 0;
2068   order = sp->order;
2069   uniform = sp->uniform;
2070   if (!uniform) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Variable order not supported yet");
2071   if (lag->trimmed && !formDegree) lag->trimmed = PETSC_FALSE; /* trimmed spaces are the same as full spaces for 0-forms */
2072   if (lag->nodeType == PETSCDTNODES_DEFAULT) {
2073     lag->nodeType = PETSCDTNODES_GAUSSJACOBI;
2074     lag->nodeExponent = 0.;
2075     /* trimmed spaces don't include corner vertices, so don't use end nodes by default */
2076     lag->endNodes = lag->trimmed ? PETSC_FALSE : PETSC_TRUE;
2077   }
2078   /* If a trimmed space and the user did choose nodes with endpoints, skip them by default */
2079   if (lag->numNodeSkip < 0) lag->numNodeSkip = (lag->trimmed && lag->endNodes) ? 1 : 0;
2080   numNodeSkip = lag->numNodeSkip;
2081   if (lag->trimmed && !order) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot have zeroth order trimmed elements");
2082   if (lag->trimmed && PetscAbsInt(formDegree) == dim) { /* convert trimmed n-forms to untrimmed of one polynomial order less */
2083     sp->order--;
2084     order--;
2085     lag->trimmed = PETSC_FALSE;
2086   }
2087   trimmed = lag->trimmed;
2088   if (!order || PetscAbsInt(formDegree) == dim) lag->continuous = PETSC_FALSE;
2089   continuous = lag->continuous;
2090   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2091   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2092   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2093   if (pStart != 0 || cStart != 0) SETERRQ(PetscObjectComm((PetscObject)sp), PETSC_ERR_ARG_WRONGSTATE, "Expect DM with chart starting at zero and cells first");
2094   if (cEnd != 1) SETERRQ(PetscObjectComm((PetscObject)sp), PETSC_ERR_ARG_WRONGSTATE, "Use PETSCDUALSPACEREFINED for multi-cell reference meshes");
2095   ierr = DMPlexIsInterpolated(dm, &interpolated);CHKERRQ(ierr);
2096   if (interpolated != DMPLEX_INTERPOLATED_FULL) {
2097     ierr = DMPlexInterpolate(dm, &dmint);CHKERRQ(ierr);
2098   } else {
2099     ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
2100     dmint = dm;
2101   }
2102   tensorCell = PETSC_FALSE;
2103   if (dim > 1) {
2104     ierr = DMPlexPointIsTensor(dmint, 0, &tensorCell, &tensorf, &tensorf2);CHKERRQ(ierr);
2105   }
2106   lag->tensorCell = tensorCell;
2107   if (dim < 2 || !lag->tensorCell) lag->tensorSpace = PETSC_FALSE;
2108   tensorSpace = lag->tensorSpace;
2109   if (!lag->nodeFamily) {
2110     ierr = Petsc1DNodeFamilyCreate(lag->nodeType, lag->nodeExponent, lag->endNodes, &lag->nodeFamily);CHKERRQ(ierr);
2111   }
2112   nodeFamily = lag->nodeFamily;
2113   if (interpolated != DMPLEX_INTERPOLATED_FULL && continuous && (PetscAbsInt(formDegree) > 0 || order > 1)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Reference element won't support all boundary nodes");
2114 
2115   /* step 2: construct the boundary spaces */
2116   ierr = PetscMalloc2(depth+1,&pStratStart,depth+1,&pStratEnd);CHKERRQ(ierr);
2117   ierr = PetscCalloc1(pEnd,&(sp->pointSpaces));CHKERRQ(ierr);
2118   for (d = 0; d <= depth; ++d) {ierr = DMPlexGetDepthStratum(dm, d, &pStratStart[d], &pStratEnd[d]);CHKERRQ(ierr);}
2119   ierr = PetscDualSpaceSectionCreate_Internal(sp, &section);CHKERRQ(ierr);
2120   sp->pointSection = section;
2121   if (continuous && !(lag->interiorOnly)) {
2122     PetscInt h;
2123 
2124     for (p = pStratStart[depth - 1]; p < pStratEnd[depth - 1]; p++) { /* calculate the facet dual spaces */
2125       PetscReal v0[3];
2126       DMPolytopeType ptype;
2127       PetscReal J[9], detJ;
2128       PetscInt  q;
2129 
2130       ierr = DMPlexComputeCellGeometryAffineFEM(dm, p, v0, J, NULL, &detJ);CHKERRQ(ierr);
2131       ierr = DMPlexGetCellType(dm, p, &ptype);CHKERRQ(ierr);
2132 
2133       /* compare to previous facets: if computed, reference that dualspace */
2134       for (q = pStratStart[depth - 1]; q < p; q++) {
2135         DMPolytopeType qtype;
2136 
2137         ierr = DMPlexGetCellType(dm, q, &qtype);CHKERRQ(ierr);
2138         if (qtype == ptype) break;
2139       }
2140       if (q < p) { /* this facet has the same dual space as that one */
2141         ierr = PetscObjectReference((PetscObject)sp->pointSpaces[q]);CHKERRQ(ierr);
2142         sp->pointSpaces[p] = sp->pointSpaces[q];
2143         continue;
2144       }
2145       /* if not, recursively compute this dual space */
2146       ierr = PetscDualSpaceCreateFacetSubspace_Lagrange(sp,NULL,p,formDegree,Ncopies,PETSC_FALSE,&sp->pointSpaces[p]);CHKERRQ(ierr);
2147     }
2148     for (h = 2; h <= depth; h++) { /* get the higher subspaces from the facet subspaces */
2149       PetscInt hd = depth - h;
2150       PetscInt hdim = dim - h;
2151 
2152       if (hdim < PetscAbsInt(formDegree)) break;
2153       for (p = pStratStart[hd]; p < pStratEnd[hd]; p++) {
2154         PetscInt suppSize, s;
2155         const PetscInt *supp;
2156 
2157         ierr = DMPlexGetSupportSize(dm, p, &suppSize);CHKERRQ(ierr);
2158         ierr = DMPlexGetSupport(dm, p, &supp);CHKERRQ(ierr);
2159         for (s = 0; s < suppSize; s++) {
2160           DM             qdm;
2161           PetscDualSpace qsp, psp;
2162           PetscInt c, coneSize, q;
2163           const PetscInt *cone;
2164           const PetscInt *refCone;
2165 
2166           q = supp[0];
2167           qsp = sp->pointSpaces[q];
2168           ierr = DMPlexGetConeSize(dm, q, &coneSize);CHKERRQ(ierr);
2169           ierr = DMPlexGetCone(dm, q, &cone);CHKERRQ(ierr);
2170           for (c = 0; c < coneSize; c++) if (cone[c] == p) break;
2171           if (c == coneSize) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "cone/support mismatch");
2172           ierr = PetscDualSpaceGetDM(qsp, &qdm);CHKERRQ(ierr);
2173           ierr = DMPlexGetCone(qdm, 0, &refCone);CHKERRQ(ierr);
2174           /* get the equivalent dual space from the support dual space */
2175           ierr = PetscDualSpaceGetPointSubspace(qsp, refCone[c], &psp);CHKERRQ(ierr);
2176           if (!s) {
2177             ierr = PetscObjectReference((PetscObject)psp);CHKERRQ(ierr);
2178             sp->pointSpaces[p] = psp;
2179           }
2180         }
2181       }
2182     }
2183     for (p = 1; p < pEnd; p++) {
2184       PetscInt pspdim;
2185       if (!sp->pointSpaces[p]) continue;
2186       ierr = PetscDualSpaceGetInteriorDimension(sp->pointSpaces[p], &pspdim);CHKERRQ(ierr);
2187       ierr = PetscSectionSetDof(section, p, pspdim);CHKERRQ(ierr);
2188     }
2189   }
2190 
2191   if (Ncopies > 1) {
2192     Mat intMatScalar, allMatScalar;
2193     PetscDualSpace scalarsp;
2194     PetscDualSpace_Lag *scalarlag;
2195 
2196     ierr = PetscDualSpaceDuplicate(sp, &scalarsp);CHKERRQ(ierr);
2197     /* Setting the number of components to Nk is a space with 1 copy of each k-form */
2198     ierr = PetscDualSpaceSetNumComponents(scalarsp, Nk);CHKERRQ(ierr);
2199     ierr = PetscDualSpaceSetUp(scalarsp);CHKERRQ(ierr);
2200     ierr = PetscDualSpaceGetInteriorData(scalarsp, &(sp->intNodes), &intMatScalar);CHKERRQ(ierr);
2201     ierr = PetscObjectReference((PetscObject)(sp->intNodes));CHKERRQ(ierr);
2202     if (intMatScalar) {ierr = PetscDualSpaceLagrangeMatrixCreateCopies(intMatScalar, Nk, Ncopies, &(sp->intMat));CHKERRQ(ierr);}
2203     ierr = PetscDualSpaceGetAllData(scalarsp, &(sp->allNodes), &allMatScalar);CHKERRQ(ierr);
2204     ierr = PetscObjectReference((PetscObject)(sp->allNodes));CHKERRQ(ierr);
2205     ierr = PetscDualSpaceLagrangeMatrixCreateCopies(allMatScalar, Nk, Ncopies, &(sp->allMat));CHKERRQ(ierr);
2206     sp->spdim = scalarsp->spdim * Ncopies;
2207     sp->spintdim = scalarsp->spintdim * Ncopies;
2208     scalarlag = (PetscDualSpace_Lag *) scalarsp->data;
2209     ierr = PetscLagNodeIndicesReference(scalarlag->vertIndices);CHKERRQ(ierr);
2210     lag->vertIndices = scalarlag->vertIndices;
2211     ierr = PetscLagNodeIndicesReference(scalarlag->intNodeIndices);CHKERRQ(ierr);
2212     lag->intNodeIndices = scalarlag->intNodeIndices;
2213     ierr = PetscLagNodeIndicesReference(scalarlag->allNodeIndices);CHKERRQ(ierr);
2214     lag->allNodeIndices = scalarlag->allNodeIndices;
2215     ierr = PetscDualSpaceDestroy(&scalarsp);CHKERRQ(ierr);
2216     ierr = PetscSectionSetDof(section, 0, sp->spintdim);CHKERRQ(ierr);
2217     ierr = PetscDualSpaceSectionSetUp_Internal(sp, section);CHKERRQ(ierr);
2218     ierr = PetscDualSpaceComputeFunctionalsFromAllData(sp);CHKERRQ(ierr);
2219     ierr = PetscFree2(pStratStart, pStratEnd);CHKERRQ(ierr);
2220     ierr = DMDestroy(&dmint);CHKERRQ(ierr);
2221     PetscFunctionReturn(0);
2222   }
2223 
2224   if (trimmed && !continuous) {
2225     /* the dofs of a trimmed space don't have a nice tensor/lattice structure:
2226      * just construct the continuous dual space and copy all of the data over,
2227      * allocating it all to the cell instead of splitting it up between the boundaries */
2228     PetscDualSpace  spcont;
2229     PetscInt        spdim, f;
2230     PetscQuadrature allNodes;
2231     PetscDualSpace_Lag *lagc;
2232     Mat             allMat;
2233 
2234     ierr = PetscDualSpaceDuplicate(sp, &spcont);CHKERRQ(ierr);
2235     ierr = PetscDualSpaceLagrangeSetContinuity(spcont, PETSC_TRUE);CHKERRQ(ierr);
2236     ierr = PetscDualSpaceSetUp(spcont);CHKERRQ(ierr);
2237     ierr = PetscDualSpaceGetDimension(spcont, &spdim);CHKERRQ(ierr);
2238     sp->spdim = sp->spintdim = spdim;
2239     ierr = PetscSectionSetDof(section, 0, spdim);CHKERRQ(ierr);
2240     ierr = PetscDualSpaceSectionSetUp_Internal(sp, section);CHKERRQ(ierr);
2241     ierr = PetscMalloc1(spdim, &(sp->functional));CHKERRQ(ierr);
2242     for (f = 0; f < spdim; f++) {
2243       PetscQuadrature fn;
2244 
2245       ierr = PetscDualSpaceGetFunctional(spcont, f, &fn);CHKERRQ(ierr);
2246       ierr = PetscObjectReference((PetscObject)fn);CHKERRQ(ierr);
2247       sp->functional[f] = fn;
2248     }
2249     ierr = PetscDualSpaceGetAllData(spcont, &allNodes, &allMat);CHKERRQ(ierr);
2250     ierr = PetscObjectReference((PetscObject) allNodes);CHKERRQ(ierr);
2251     ierr = PetscObjectReference((PetscObject) allNodes);CHKERRQ(ierr);
2252     sp->allNodes = sp->intNodes = allNodes;
2253     ierr = PetscObjectReference((PetscObject) allMat);CHKERRQ(ierr);
2254     ierr = PetscObjectReference((PetscObject) allMat);CHKERRQ(ierr);
2255     sp->allMat = sp->intMat = allMat;
2256     lagc = (PetscDualSpace_Lag *) spcont->data;
2257     ierr = PetscLagNodeIndicesReference(lagc->vertIndices);CHKERRQ(ierr);
2258     lag->vertIndices = lagc->vertIndices;
2259     ierr = PetscLagNodeIndicesReference(lagc->allNodeIndices);CHKERRQ(ierr);
2260     ierr = PetscLagNodeIndicesReference(lagc->allNodeIndices);CHKERRQ(ierr);
2261     lag->intNodeIndices = lagc->allNodeIndices;
2262     lag->allNodeIndices = lagc->allNodeIndices;
2263     ierr = PetscDualSpaceDestroy(&spcont);CHKERRQ(ierr);
2264     ierr = PetscFree2(pStratStart, pStratEnd);CHKERRQ(ierr);
2265     ierr = DMDestroy(&dmint);CHKERRQ(ierr);
2266     PetscFunctionReturn(0);
2267   }
2268 
2269   /* step 3: construct intNodes, and intMat, and combine it with boundray data to make allNodes and allMat */
2270   if (!tensorSpace) {
2271     if (!tensorCell) {ierr = PetscLagNodeIndicesCreateSimplexVertices(dm, &(lag->vertIndices));CHKERRQ(ierr);}
2272 
2273     if (trimmed) {
2274       /* there is one dof in the interior of the a trimmed element for each full polynomial of with degree at most
2275        * order + k - dim - 1 */
2276       if (order + PetscAbsInt(formDegree) > dim) {
2277         PetscInt sum = order + PetscAbsInt(formDegree) - dim - 1;
2278         PetscInt nDofs;
2279 
2280         ierr = PetscDualSpaceLagrangeCreateSimplexNodeMat(nodeFamily, dim, sum, Nk, numNodeSkip, &sp->intNodes, &sp->intMat, &(lag->intNodeIndices));CHKERRQ(ierr);
2281         ierr = MatGetSize(sp->intMat, &nDofs, NULL);CHKERRQ(ierr);
2282         ierr = PetscSectionSetDof(section, 0, nDofs);CHKERRQ(ierr);
2283       }
2284       ierr = PetscDualSpaceSectionSetUp_Internal(sp, section);CHKERRQ(ierr);
2285       ierr = PetscDualSpaceCreateAllDataFromInteriorData(sp);CHKERRQ(ierr);
2286       ierr = PetscDualSpaceLagrangeCreateAllNodeIdx(sp);CHKERRQ(ierr);
2287     } else {
2288       if (!continuous) {
2289         /* if discontinuous just construct one node for each set of dofs (a set of dofs is a basis for the k-form
2290          * space) */
2291         PetscInt sum = order;
2292         PetscInt nDofs;
2293 
2294         ierr = PetscDualSpaceLagrangeCreateSimplexNodeMat(nodeFamily, dim, sum, Nk, numNodeSkip, &sp->intNodes, &sp->intMat, &(lag->intNodeIndices));CHKERRQ(ierr);
2295         ierr = MatGetSize(sp->intMat, &nDofs, NULL);CHKERRQ(ierr);
2296         ierr = PetscSectionSetDof(section, 0, nDofs);CHKERRQ(ierr);
2297         ierr = PetscDualSpaceSectionSetUp_Internal(sp, section);CHKERRQ(ierr);
2298         ierr = PetscObjectReference((PetscObject)(sp->intNodes));CHKERRQ(ierr);
2299         sp->allNodes = sp->intNodes;
2300         ierr = PetscObjectReference((PetscObject)(sp->intMat));CHKERRQ(ierr);
2301         sp->allMat = sp->intMat;
2302         ierr = PetscLagNodeIndicesReference(lag->intNodeIndices);CHKERRQ(ierr);
2303         lag->allNodeIndices = lag->intNodeIndices;
2304       } else {
2305         /* there is one dof in the interior of the a full element for each trimmed polynomial of with degree at most
2306          * order + k - dim, but with complementary form degree */
2307         if (order + PetscAbsInt(formDegree) > dim) {
2308           PetscDualSpace trimmedsp;
2309           PetscDualSpace_Lag *trimmedlag;
2310           PetscQuadrature intNodes;
2311           PetscInt trFormDegree = formDegree >= 0 ? formDegree - dim : dim - PetscAbsInt(formDegree);
2312           PetscInt nDofs;
2313           Mat intMat;
2314 
2315           ierr = PetscDualSpaceDuplicate(sp, &trimmedsp);CHKERRQ(ierr);
2316           ierr = PetscDualSpaceLagrangeSetTrimmed(trimmedsp, PETSC_TRUE);CHKERRQ(ierr);
2317           ierr = PetscDualSpaceSetOrder(trimmedsp, order + PetscAbsInt(formDegree) - dim);CHKERRQ(ierr);
2318           ierr = PetscDualSpaceSetFormDegree(trimmedsp, trFormDegree);CHKERRQ(ierr);
2319           trimmedlag = (PetscDualSpace_Lag *) trimmedsp->data;
2320           trimmedlag->numNodeSkip = numNodeSkip + 1;
2321           ierr = PetscDualSpaceSetUp(trimmedsp);CHKERRQ(ierr);
2322           ierr = PetscDualSpaceGetAllData(trimmedsp, &intNodes, &intMat);CHKERRQ(ierr);
2323           ierr = PetscObjectReference((PetscObject)intNodes);CHKERRQ(ierr);
2324           sp->intNodes = intNodes;
2325           ierr = PetscLagNodeIndicesReference(trimmedlag->allNodeIndices);CHKERRQ(ierr);
2326           lag->intNodeIndices = trimmedlag->allNodeIndices;
2327           ierr = PetscObjectReference((PetscObject)intMat);CHKERRQ(ierr);
2328           if (PetscAbsInt(formDegree) > 0 && PetscAbsInt(formDegree) < dim) {
2329             PetscReal *T;
2330             PetscScalar *work;
2331             PetscInt nCols, nRows;
2332             Mat intMatT;
2333 
2334             ierr = MatDuplicate(intMat, MAT_COPY_VALUES, &intMatT);CHKERRQ(ierr);
2335             ierr = MatGetSize(intMat, &nRows, &nCols);CHKERRQ(ierr);
2336             ierr = PetscMalloc2(Nk * Nk, &T, nCols, &work);CHKERRQ(ierr);
2337             ierr = BiunitSimplexSymmetricFormTransformation(dim, formDegree, T);CHKERRQ(ierr);
2338             for (PetscInt row = 0; row < nRows; row++) {
2339               PetscInt nrCols;
2340               const PetscInt *rCols;
2341               const PetscScalar *rVals;
2342 
2343               ierr = MatGetRow(intMat, row, &nrCols, &rCols, &rVals);CHKERRQ(ierr);
2344               if (nrCols % Nk) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "nonzeros in intMat matrix are not in k-form size blocks");
2345               for (PetscInt b = 0; b < nrCols; b += Nk) {
2346                 const PetscScalar *v = &rVals[b];
2347                 PetscScalar *w = &work[b];
2348                 for (PetscInt j = 0; j < Nk; j++) {
2349                   w[j] = 0.;
2350                   for (PetscInt i = 0; i < Nk; i++) {
2351                     w[j] += v[i] * T[i * Nk + j];
2352                   }
2353                 }
2354               }
2355               ierr = MatSetValuesBlocked(intMatT, 1, &row, nrCols, rCols, work, INSERT_VALUES);CHKERRQ(ierr);
2356               ierr = MatRestoreRow(intMat, row, &nrCols, &rCols, &rVals);CHKERRQ(ierr);
2357             }
2358             ierr = MatAssemblyBegin(intMatT, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2359             ierr = MatAssemblyEnd(intMatT, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2360             ierr = MatDestroy(&intMat);CHKERRQ(ierr);
2361             intMat = intMatT;
2362             ierr = PetscLagNodeIndicesDestroy(&(lag->intNodeIndices));CHKERRQ(ierr);
2363             ierr = PetscLagNodeIndicesDuplicate(trimmedlag->allNodeIndices, &(lag->intNodeIndices));CHKERRQ(ierr);
2364             {
2365               PetscInt nNodes = lag->intNodeIndices->nNodes;
2366               PetscReal *newNodeVec = lag->intNodeIndices->nodeVec;
2367               const PetscReal *oldNodeVec = trimmedlag->allNodeIndices->nodeVec;
2368 
2369               for (PetscInt n = 0; n < nNodes; n++) {
2370                 PetscReal *w = &newNodeVec[n * Nk];
2371                 const PetscReal *v = &oldNodeVec[n * Nk];
2372 
2373                 for (PetscInt j = 0; j < Nk; j++) {
2374                   w[j] = 0.;
2375                   for (PetscInt i = 0; i < Nk; i++) {
2376                     w[j] += v[i] * T[i * Nk + j];
2377                   }
2378                 }
2379               }
2380             }
2381             ierr = PetscFree2(T, work);CHKERRQ(ierr);
2382           }
2383           sp->intMat = intMat;
2384           ierr = MatGetSize(sp->intMat, &nDofs, NULL);CHKERRQ(ierr);
2385           ierr = PetscDualSpaceDestroy(&trimmedsp);CHKERRQ(ierr);
2386           ierr = PetscSectionSetDof(section, 0, nDofs);CHKERRQ(ierr);
2387         }
2388         ierr = PetscDualSpaceSectionSetUp_Internal(sp, section);CHKERRQ(ierr);
2389         ierr = PetscDualSpaceCreateAllDataFromInteriorData(sp);CHKERRQ(ierr);
2390         ierr = PetscDualSpaceLagrangeCreateAllNodeIdx(sp);CHKERRQ(ierr);
2391       }
2392     }
2393   } else {
2394     PetscQuadrature intNodesTrace = NULL;
2395     PetscQuadrature intNodesFiber = NULL;
2396     PetscQuadrature intNodes = NULL;
2397     PetscLagNodeIndices intNodeIndices = NULL;
2398     Mat             intMat = NULL;
2399 
2400     if (PetscAbsInt(formDegree) < dim) { /* get the trace k-forms on the first facet, and the 0-forms on the edge,
2401                                             and wedge them together to create some of the k-form dofs */
2402       PetscDualSpace  trace, fiber;
2403       PetscDualSpace_Lag *tracel, *fiberl;
2404       Mat             intMatTrace, intMatFiber;
2405 
2406       if (sp->pointSpaces[tensorf]) {
2407         ierr = PetscObjectReference((PetscObject)(sp->pointSpaces[tensorf]));CHKERRQ(ierr);
2408         trace = sp->pointSpaces[tensorf];
2409       } else {
2410         ierr = PetscDualSpaceCreateFacetSubspace_Lagrange(sp,NULL,tensorf,formDegree,Ncopies,PETSC_TRUE,&trace);CHKERRQ(ierr);
2411       }
2412       ierr = PetscDualSpaceCreateEdgeSubspace_Lagrange(sp,order,0,1,PETSC_TRUE,&fiber);CHKERRQ(ierr);
2413       tracel = (PetscDualSpace_Lag *) trace->data;
2414       fiberl = (PetscDualSpace_Lag *) fiber->data;
2415       ierr = PetscLagNodeIndicesCreateTensorVertices(dm, tracel->vertIndices, &(lag->vertIndices));CHKERRQ(ierr);
2416       ierr = PetscDualSpaceGetInteriorData(trace, &intNodesTrace, &intMatTrace);CHKERRQ(ierr);
2417       ierr = PetscDualSpaceGetInteriorData(fiber, &intNodesFiber, &intMatFiber);CHKERRQ(ierr);
2418       if (intNodesTrace && intNodesFiber) {
2419         ierr = PetscQuadratureCreateTensor(intNodesTrace, intNodesFiber, &intNodes);CHKERRQ(ierr);
2420         ierr = MatTensorAltV(intMatTrace, intMatFiber, dim-1, formDegree, 1, 0, &intMat);CHKERRQ(ierr);
2421         ierr = PetscLagNodeIndicesTensor(tracel->intNodeIndices, dim - 1, formDegree, fiberl->intNodeIndices, 1, 0, &intNodeIndices);CHKERRQ(ierr);
2422       }
2423       ierr = PetscObjectReference((PetscObject) intNodesTrace);CHKERRQ(ierr);
2424       ierr = PetscObjectReference((PetscObject) intNodesFiber);CHKERRQ(ierr);
2425       ierr = PetscDualSpaceDestroy(&fiber);CHKERRQ(ierr);
2426       ierr = PetscDualSpaceDestroy(&trace);CHKERRQ(ierr);
2427     }
2428     if (PetscAbsInt(formDegree) > 0) { /* get the trace (k-1)-forms on the first facet, and the 1-forms on the edge,
2429                                           and wedge them together to create the remaining k-form dofs */
2430       PetscDualSpace  trace, fiber;
2431       PetscDualSpace_Lag *tracel, *fiberl;
2432       PetscQuadrature intNodesTrace2, intNodesFiber2, intNodes2;
2433       PetscLagNodeIndices intNodeIndices2;
2434       Mat             intMatTrace, intMatFiber, intMat2;
2435       PetscInt        traceDegree = formDegree > 0 ? formDegree - 1 : formDegree + 1;
2436       PetscInt        fiberDegree = formDegree > 0 ? 1 : -1;
2437 
2438       ierr = PetscDualSpaceCreateFacetSubspace_Lagrange(sp,NULL,tensorf,traceDegree,Ncopies,PETSC_TRUE,&trace);CHKERRQ(ierr);
2439       ierr = PetscDualSpaceCreateEdgeSubspace_Lagrange(sp,order,fiberDegree,1,PETSC_TRUE,&fiber);CHKERRQ(ierr);
2440       tracel = (PetscDualSpace_Lag *) trace->data;
2441       fiberl = (PetscDualSpace_Lag *) fiber->data;
2442       if (!lag->vertIndices) {
2443         ierr = PetscLagNodeIndicesCreateTensorVertices(dm, tracel->vertIndices, &(lag->vertIndices));CHKERRQ(ierr);
2444       }
2445       ierr = PetscDualSpaceGetInteriorData(trace, &intNodesTrace2, &intMatTrace);CHKERRQ(ierr);
2446       ierr = PetscDualSpaceGetInteriorData(fiber, &intNodesFiber2, &intMatFiber);CHKERRQ(ierr);
2447       if (intNodesTrace2 && intNodesFiber2) {
2448         ierr = PetscQuadratureCreateTensor(intNodesTrace2, intNodesFiber2, &intNodes2);CHKERRQ(ierr);
2449         ierr = MatTensorAltV(intMatTrace, intMatFiber, dim-1, traceDegree, 1, fiberDegree, &intMat2);CHKERRQ(ierr);
2450         ierr = PetscLagNodeIndicesTensor(tracel->intNodeIndices, dim - 1, traceDegree, fiberl->intNodeIndices, 1, fiberDegree, &intNodeIndices2);CHKERRQ(ierr);
2451         if (!intMat) {
2452           intMat = intMat2;
2453           intNodes = intNodes2;
2454           intNodeIndices = intNodeIndices2;
2455         } else {
2456           /* merge the matrices, quadrature points, and nodes */
2457           PetscInt         nM;
2458           PetscInt         nDof, nDof2;
2459           PetscInt        *toMerged = NULL, *toMerged2 = NULL;
2460           PetscQuadrature  merged = NULL;
2461           PetscLagNodeIndices intNodeIndicesMerged = NULL;
2462           Mat              matMerged = NULL;
2463 
2464           ierr = MatGetSize(intMat, &nDof, NULL);CHKERRQ(ierr);
2465           ierr = MatGetSize(intMat2, &nDof2, NULL);CHKERRQ(ierr);
2466           ierr = PetscQuadraturePointsMerge(intNodes, intNodes2, &merged, &toMerged, &toMerged2);CHKERRQ(ierr);
2467           ierr = PetscQuadratureGetData(merged, NULL, NULL, &nM, NULL, NULL);CHKERRQ(ierr);
2468           ierr = MatricesMerge(intMat, intMat2, dim, formDegree, nM, toMerged, toMerged2, &matMerged);CHKERRQ(ierr);
2469           ierr = PetscLagNodeIndicesMerge(intNodeIndices, intNodeIndices2, &intNodeIndicesMerged);CHKERRQ(ierr);
2470           ierr = PetscFree(toMerged);CHKERRQ(ierr);
2471           ierr = PetscFree(toMerged2);CHKERRQ(ierr);
2472           ierr = MatDestroy(&intMat);CHKERRQ(ierr);
2473           ierr = MatDestroy(&intMat2);CHKERRQ(ierr);
2474           ierr = PetscQuadratureDestroy(&intNodes);CHKERRQ(ierr);
2475           ierr = PetscQuadratureDestroy(&intNodes2);CHKERRQ(ierr);
2476           ierr = PetscLagNodeIndicesDestroy(&intNodeIndices);CHKERRQ(ierr);
2477           ierr = PetscLagNodeIndicesDestroy(&intNodeIndices2);CHKERRQ(ierr);
2478           intNodes = merged;
2479           intMat = matMerged;
2480           intNodeIndices = intNodeIndicesMerged;
2481           if (!trimmed) {
2482             /* I think users expect that, when a node has a full basis for the k-forms,
2483              * they should be consecutive dofs.  That isn't the case for trimmed spaces,
2484              * but is for some of the nodes in untrimmed spaces, so in that case we
2485              * sort them to group them by node */
2486             Mat intMatPerm;
2487 
2488             ierr = MatPermuteByNodeIdx(intMat, intNodeIndices, &intMatPerm);CHKERRQ(ierr);
2489             ierr = MatDestroy(&intMat);CHKERRQ(ierr);
2490             intMat = intMatPerm;
2491           }
2492         }
2493       }
2494       ierr = PetscDualSpaceDestroy(&fiber);CHKERRQ(ierr);
2495       ierr = PetscDualSpaceDestroy(&trace);CHKERRQ(ierr);
2496     }
2497     ierr = PetscQuadratureDestroy(&intNodesTrace);CHKERRQ(ierr);
2498     ierr = PetscQuadratureDestroy(&intNodesFiber);CHKERRQ(ierr);
2499     sp->intNodes = intNodes;
2500     sp->intMat = intMat;
2501     lag->intNodeIndices = intNodeIndices;
2502     {
2503       PetscInt nDofs = 0;
2504 
2505       if (intMat) {
2506         ierr = MatGetSize(intMat, &nDofs, NULL);CHKERRQ(ierr);
2507       }
2508       ierr = PetscSectionSetDof(section, 0, nDofs);CHKERRQ(ierr);
2509     }
2510     ierr = PetscDualSpaceSectionSetUp_Internal(sp, section);CHKERRQ(ierr);
2511     if (continuous) {
2512       ierr = PetscDualSpaceCreateAllDataFromInteriorData(sp);CHKERRQ(ierr);
2513       ierr = PetscDualSpaceLagrangeCreateAllNodeIdx(sp);CHKERRQ(ierr);
2514     } else {
2515       ierr = PetscObjectReference((PetscObject) intNodes);CHKERRQ(ierr);
2516       sp->allNodes = intNodes;
2517       ierr = PetscObjectReference((PetscObject) intMat);CHKERRQ(ierr);
2518       sp->allMat = intMat;
2519       ierr = PetscLagNodeIndicesReference(intNodeIndices);CHKERRQ(ierr);
2520       lag->allNodeIndices = intNodeIndices;
2521     }
2522   }
2523   ierr = PetscSectionGetStorageSize(section, &sp->spdim);CHKERRQ(ierr);
2524   ierr = PetscSectionGetConstrainedStorageSize(section, &sp->spintdim);CHKERRQ(ierr);
2525   ierr = PetscDualSpaceComputeFunctionalsFromAllData(sp);CHKERRQ(ierr);
2526   ierr = PetscFree2(pStratStart, pStratEnd);CHKERRQ(ierr);
2527   ierr = DMDestroy(&dmint);CHKERRQ(ierr);
2528   PetscFunctionReturn(0);
2529 }
2530 
2531 /* Create a matrix that represents the transformation that DMPlexVecGetClosure() would need
2532  * to get the representation of the dofs for a mesh point if the mesh point had this orientation
2533  * relative to the cell */
2534 PetscErrorCode PetscDualSpaceCreateInteriorSymmetryMatrix_Lagrange(PetscDualSpace sp, PetscInt ornt, Mat *symMat)
2535 {
2536   PetscDualSpace_Lag *lag;
2537   DM dm;
2538   PetscLagNodeIndices vertIndices, intNodeIndices;
2539   PetscLagNodeIndices ni;
2540   PetscInt nodeIdxDim, nodeVecDim, nNodes;
2541   PetscInt formDegree;
2542   PetscInt *perm, *permOrnt;
2543   PetscInt *nnz;
2544   PetscInt n;
2545   PetscInt maxGroupSize;
2546   PetscScalar *V, *W, *work;
2547   Mat A;
2548   PetscErrorCode ierr;
2549 
2550   PetscFunctionBegin;
2551   if (!sp->spintdim) {
2552     *symMat = NULL;
2553     PetscFunctionReturn(0);
2554   }
2555   lag = (PetscDualSpace_Lag *) sp->data;
2556   vertIndices = lag->vertIndices;
2557   intNodeIndices = lag->intNodeIndices;
2558   ierr = PetscDualSpaceGetDM(sp, &dm);CHKERRQ(ierr);
2559   ierr = PetscDualSpaceGetFormDegree(sp, &formDegree);CHKERRQ(ierr);
2560   ierr = PetscNew(&ni);CHKERRQ(ierr);
2561   ni->refct = 1;
2562   ni->nodeIdxDim = nodeIdxDim = intNodeIndices->nodeIdxDim;
2563   ni->nodeVecDim = nodeVecDim = intNodeIndices->nodeVecDim;
2564   ni->nNodes = nNodes = intNodeIndices->nNodes;
2565   ierr = PetscMalloc1(nNodes * nodeIdxDim, &(ni->nodeIdx));CHKERRQ(ierr);
2566   ierr = PetscMalloc1(nNodes * nodeVecDim, &(ni->nodeVec));CHKERRQ(ierr);
2567   /* push forward the dofs by the symmetry of the reference element induced by ornt */
2568   ierr = PetscLagNodeIndicesPushForward(dm, vertIndices, 0, vertIndices, intNodeIndices, ornt, formDegree, ni->nodeIdx, ni->nodeVec);CHKERRQ(ierr);
2569   /* get the revlex order for both the original and transformed dofs */
2570   ierr = PetscLagNodeIndicesGetPermutation(intNodeIndices, &perm);CHKERRQ(ierr);
2571   ierr = PetscLagNodeIndicesGetPermutation(ni, &permOrnt);CHKERRQ(ierr);
2572   ierr = PetscMalloc1(nNodes, &nnz);CHKERRQ(ierr);
2573   for (n = 0, maxGroupSize = 0; n < nNodes;) { /* incremented in the loop */
2574     PetscInt *nind = &(ni->nodeIdx[permOrnt[n] * nodeIdxDim]);
2575     PetscInt m, nEnd;
2576     PetscInt groupSize;
2577     /* for each group of dofs that have the same nodeIdx coordinate */
2578     for (nEnd = n + 1; nEnd < nNodes; nEnd++) {
2579       PetscInt *mind = &(ni->nodeIdx[permOrnt[nEnd] * nodeIdxDim]);
2580       PetscInt d;
2581 
2582       /* compare the oriented permutation indices */
2583       for (d = 0; d < nodeIdxDim; d++) if (mind[d] != nind[d]) break;
2584       if (d < nodeIdxDim) break;
2585     }
2586     /* permOrnt[[n, nEnd)] is a group of dofs that, under the symmetry are at the same location */
2587 
2588     /* the symmetry had better map the group of dofs with the same permuted nodeIdx
2589      * to a group of dofs with the same size, otherwise we messed up */
2590     if (PetscDefined(USE_DEBUG)) {
2591       PetscInt m;
2592       PetscInt *nind = &(intNodeIndices->nodeIdx[perm[n] * nodeIdxDim]);
2593 
2594       for (m = n + 1; m < nEnd; m++) {
2595         PetscInt *mind = &(intNodeIndices->nodeIdx[perm[m] * nodeIdxDim]);
2596         PetscInt d;
2597 
2598         /* compare the oriented permutation indices */
2599         for (d = 0; d < nodeIdxDim; d++) if (mind[d] != nind[d]) break;
2600         if (d < nodeIdxDim) break;
2601       }
2602       if (m < nEnd) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Dofs with same index after symmetry not same block size");
2603     }
2604     groupSize = nEnd - n;
2605     /* each pushforward dof vector will be expressed in a basis of the unpermuted dofs */
2606     for (m = n; m < nEnd; m++) nnz[permOrnt[m]] = groupSize;
2607 
2608     maxGroupSize = PetscMax(maxGroupSize, nEnd - n);
2609     n = nEnd;
2610   }
2611   if (maxGroupSize > nodeVecDim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Dofs are not in blocks that can be solved");
2612   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF, nNodes, nNodes, 0, nnz, &A);CHKERRQ(ierr);
2613   ierr = PetscFree(nnz);CHKERRQ(ierr);
2614   ierr = PetscMalloc3(maxGroupSize * nodeVecDim, &V, maxGroupSize * nodeVecDim, &W, nodeVecDim * 2, &work);CHKERRQ(ierr);
2615   for (n = 0; n < nNodes;) { /* incremented in the loop */
2616     PetscInt *nind = &(ni->nodeIdx[permOrnt[n] * nodeIdxDim]);
2617     PetscInt nEnd;
2618     PetscInt m;
2619     PetscInt groupSize;
2620     for (nEnd = n + 1; nEnd < nNodes; nEnd++) {
2621       PetscInt *mind = &(ni->nodeIdx[permOrnt[nEnd] * nodeIdxDim]);
2622       PetscInt d;
2623 
2624       /* compare the oriented permutation indices */
2625       for (d = 0; d < nodeIdxDim; d++) if (mind[d] != nind[d]) break;
2626       if (d < nodeIdxDim) break;
2627     }
2628     groupSize = nEnd - n;
2629     /* get all of the vectors from the original and all of the pushforward vectors */
2630     for (m = n; m < nEnd; m++) {
2631       PetscInt d;
2632 
2633       for (d = 0; d < nodeVecDim; d++) {
2634         V[(m - n) * nodeVecDim + d] = intNodeIndices->nodeVec[perm[m] * nodeVecDim + d];
2635         W[(m - n) * nodeVecDim + d] = ni->nodeVec[permOrnt[m] * nodeVecDim + d];
2636       }
2637     }
2638     /* now we have to solve for W in terms of V: the systems isn't always square, but the span
2639      * of V and W should always be the same, so the solution of the normal equations works */
2640     {
2641       char transpose = 'N';
2642       PetscBLASInt bm = nodeVecDim;
2643       PetscBLASInt bn = groupSize;
2644       PetscBLASInt bnrhs = groupSize;
2645       PetscBLASInt blda = bm;
2646       PetscBLASInt bldb = bm;
2647       PetscBLASInt blwork = 2 * nodeVecDim;
2648       PetscBLASInt info;
2649 
2650       PetscStackCallBLAS("LAPACKgels",LAPACKgels_(&transpose,&bm,&bn,&bnrhs,V,&blda,W,&bldb,work,&blwork, &info));
2651       if (info != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad argument to GELS");
2652       /* repack */
2653       {
2654         PetscInt i, j;
2655 
2656         for (i = 0; i < groupSize; i++) {
2657           for (j = 0; j < groupSize; j++) {
2658             /* notice the different leading dimension */
2659             V[i * groupSize + j] = W[i * nodeVecDim + j];
2660           }
2661         }
2662       }
2663       if (PetscDefined(USE_DEBUG)) {
2664         PetscReal res;
2665 
2666         /* check that the normal error is 0 */
2667         for (m = n; m < nEnd; m++) {
2668           PetscInt d;
2669 
2670           for (d = 0; d < nodeVecDim; d++) {
2671             W[(m - n) * nodeVecDim + d] = ni->nodeVec[permOrnt[m] * nodeVecDim + d];
2672           }
2673         }
2674         res = 0.;
2675         for (PetscInt i = 0; i < groupSize; i++) {
2676           for (PetscInt j = 0; j < nodeVecDim; j++) {
2677             for (PetscInt k = 0; k < groupSize; k++) {
2678               W[i * nodeVecDim + j] -= V[i * groupSize + k] * intNodeIndices->nodeVec[perm[n+k] * nodeVecDim + j];
2679             }
2680             res += PetscAbsScalar(W[i * nodeVecDim + j]);
2681           }
2682         }
2683         if (res > PETSC_SMALL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Dof block did not solve");
2684       }
2685     }
2686     ierr = MatSetValues(A, groupSize, &permOrnt[n], groupSize, &perm[n], V, INSERT_VALUES);CHKERRQ(ierr);
2687     n = nEnd;
2688   }
2689   ierr = MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2690   ierr = MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2691   *symMat = A;
2692   ierr = PetscFree3(V,W,work);CHKERRQ(ierr);
2693   ierr = PetscLagNodeIndicesDestroy(&ni);CHKERRQ(ierr);
2694   PetscFunctionReturn(0);
2695 }
2696 
2697 #define BaryIndex(perEdge,a,b,c) (((b)*(2*perEdge+1-(b)))/2)+(c)
2698 
2699 #define CartIndex(perEdge,a,b) (perEdge*(a)+b)
2700 
2701 /* the existing interface for symmetries is insufficient for all cases:
2702  * - it should be sufficient for form degrees that are scalar (0 and n)
2703  * - it should be sufficient for hypercube dofs
2704  * - it isn't sufficient for simplex cells with non-scalar form degrees if
2705  *   there are any dofs in the interior
2706  *
2707  * We compute the general transformation matrices, and if they fit, we return them,
2708  * otherwise we error (but we should probably change the interface to allow for
2709  * these symmetries)
2710  */
2711 static PetscErrorCode PetscDualSpaceGetSymmetries_Lagrange(PetscDualSpace sp, const PetscInt ****perms, const PetscScalar ****flips)
2712 {
2713   PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *) sp->data;
2714   PetscInt           dim, order, Nc;
2715   PetscErrorCode     ierr;
2716 
2717   PetscFunctionBegin;
2718   ierr = PetscDualSpaceGetOrder(sp,&order);CHKERRQ(ierr);
2719   ierr = PetscDualSpaceGetNumComponents(sp,&Nc);CHKERRQ(ierr);
2720   ierr = DMGetDimension(sp->dm,&dim);CHKERRQ(ierr);
2721   if (!lag->symComputed) { /* store symmetries */
2722     PetscInt       pStart, pEnd, p;
2723     PetscInt       numPoints;
2724     PetscInt       numFaces;
2725     PetscInt       spintdim;
2726     PetscInt       ***symperms;
2727     PetscScalar    ***symflips;
2728 
2729     ierr = DMPlexGetChart(sp->dm, &pStart, &pEnd);CHKERRQ(ierr);
2730     numPoints = pEnd - pStart;
2731     ierr = DMPlexGetConeSize(sp->dm, 0, &numFaces);CHKERRQ(ierr);
2732     ierr = PetscCalloc1(numPoints,&symperms);CHKERRQ(ierr);
2733     ierr = PetscCalloc1(numPoints,&symflips);CHKERRQ(ierr);
2734     spintdim = sp->spintdim;
2735     /* The nodal symmetry behavior is not present when tensorSpace != tensorCell: someone might want this for the "S"
2736      * family of FEEC spaces.  Most used in particular are discontinuous polynomial L2 spaces in tensor cells, where
2737      * the symmetries are not necessary for FE assembly.  So for now we assume this is the case and don't return
2738      * symmetries if tensorSpace != tensorCell */
2739     if (spintdim && 0 < dim && dim < 3 && (lag->tensorSpace == lag->tensorCell)) { /* compute self symmetries */
2740       PetscInt **cellSymperms;
2741       PetscScalar **cellSymflips;
2742       PetscInt ornt;
2743       PetscInt nCopies = Nc / lag->intNodeIndices->nodeVecDim;
2744       PetscInt nNodes = lag->intNodeIndices->nNodes;
2745 
2746       lag->numSelfSym = 2 * numFaces;
2747       lag->selfSymOff = numFaces;
2748       ierr = PetscCalloc1(2*numFaces,&cellSymperms);CHKERRQ(ierr);
2749       ierr = PetscCalloc1(2*numFaces,&cellSymflips);CHKERRQ(ierr);
2750       /* we want to be able to index symmetries directly with the orientations, which range from [-numFaces,numFaces) */
2751       symperms[0] = &cellSymperms[numFaces];
2752       symflips[0] = &cellSymflips[numFaces];
2753       if (lag->intNodeIndices->nodeVecDim * nCopies != Nc) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Node indices incompatible with dofs");
2754       if (nNodes * nCopies != spintdim) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Node indices incompatible with dofs");
2755       for (ornt = -numFaces; ornt < numFaces; ornt++) { /* for every symmetry, compute the symmetry matrix, and extract rows to see if it fits in the perm + flip framework */
2756         Mat symMat;
2757         PetscInt *perm;
2758         PetscScalar *flips;
2759         PetscInt i;
2760 
2761         if (!ornt) continue;
2762         ierr = PetscMalloc1(spintdim, &perm);CHKERRQ(ierr);
2763         ierr = PetscCalloc1(spintdim, &flips);CHKERRQ(ierr);
2764         for (i = 0; i < spintdim; i++) perm[i] = -1;
2765         ierr = PetscDualSpaceCreateInteriorSymmetryMatrix_Lagrange(sp, ornt, &symMat);CHKERRQ(ierr);
2766         for (i = 0; i < nNodes; i++) {
2767           PetscInt ncols;
2768           PetscInt j, k;
2769           const PetscInt *cols;
2770           const PetscScalar *vals;
2771           PetscBool nz_seen = PETSC_FALSE;
2772 
2773           ierr = MatGetRow(symMat, i, &ncols, &cols, &vals);CHKERRQ(ierr);
2774           for (j = 0; j < ncols; j++) {
2775             if (PetscAbsScalar(vals[j]) > PETSC_SMALL) {
2776               if (nz_seen) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "This dual space has symmetries that can't be described as a permutation + sign flips");
2777               nz_seen = PETSC_TRUE;
2778               if (PetscAbsReal(PetscAbsScalar(vals[j]) - PetscRealConstant(1.)) > PETSC_SMALL) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "This dual space has symmetries that can't be described as a permutation + sign flips");
2779               if (PetscAbsReal(PetscImaginaryPart(vals[j])) > PETSC_SMALL) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "This dual space has symmetries that can't be described as a permutation + sign flips");
2780               if (perm[cols[j] * nCopies] >= 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "This dual space has symmetries that can't be described as a permutation + sign flips");
2781               for (k = 0; k < nCopies; k++) {
2782                 perm[cols[j] * nCopies + k] = i * nCopies + k;
2783               }
2784               if (PetscRealPart(vals[j]) < 0.) {
2785                 for (k = 0; k < nCopies; k++) {
2786                   flips[i * nCopies + k] = -1.;
2787                 }
2788               } else {
2789                 for (k = 0; k < nCopies; k++) {
2790                   flips[i * nCopies + k] = 1.;
2791                 }
2792               }
2793             }
2794           }
2795           ierr = MatRestoreRow(symMat, i, &ncols, &cols, &vals);CHKERRQ(ierr);
2796         }
2797         ierr = MatDestroy(&symMat);CHKERRQ(ierr);
2798         /* if there were no sign flips, keep NULL */
2799         for (i = 0; i < spintdim; i++) if (flips[i] != 1.) break;
2800         if (i == spintdim) {
2801           ierr = PetscFree(flips);CHKERRQ(ierr);
2802           flips = NULL;
2803         }
2804         /* if the permutation is identity, keep NULL */
2805         for (i = 0; i < spintdim; i++) if (perm[i] != i) break;
2806         if (i == spintdim) {
2807           ierr = PetscFree(perm);CHKERRQ(ierr);
2808           perm = NULL;
2809         }
2810         symperms[0][ornt] = perm;
2811         symflips[0][ornt] = flips;
2812       }
2813       /* if no orientations produced non-identity permutations, keep NULL */
2814       for (ornt = -numFaces; ornt < numFaces; ornt++) if (symperms[0][ornt]) break;
2815       if (ornt == numFaces) {
2816         ierr = PetscFree(cellSymperms);CHKERRQ(ierr);
2817         symperms[0] = NULL;
2818       }
2819       /* if no orientations produced sign flips, keep NULL */
2820       for (ornt = -numFaces; ornt < numFaces; ornt++) if (symflips[0][ornt]) break;
2821       if (ornt == numFaces) {
2822         ierr = PetscFree(cellSymflips);CHKERRQ(ierr);
2823         symflips[0] = NULL;
2824       }
2825     }
2826     { /* get the symmetries of closure points */
2827       PetscInt closureSize = 0;
2828       PetscInt *closure = NULL;
2829       PetscInt r;
2830 
2831       ierr = DMPlexGetTransitiveClosure(sp->dm,0,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
2832       for (r = 0; r < closureSize; r++) {
2833         PetscDualSpace psp;
2834         PetscInt point = closure[2 * r];
2835         PetscInt pspintdim;
2836         const PetscInt ***psymperms = NULL;
2837         const PetscScalar ***psymflips = NULL;
2838 
2839         if (!point) continue;
2840         ierr = PetscDualSpaceGetPointSubspace(sp, point, &psp);CHKERRQ(ierr);
2841         if (!psp) continue;
2842         ierr = PetscDualSpaceGetInteriorDimension(psp, &pspintdim);CHKERRQ(ierr);
2843         if (!pspintdim) continue;
2844         ierr = PetscDualSpaceGetSymmetries(psp,&psymperms,&psymflips);CHKERRQ(ierr);
2845         symperms[r] = (PetscInt **) (psymperms ? psymperms[0] : NULL);
2846         symflips[r] = (PetscScalar **) (psymflips ? psymflips[0] : NULL);
2847       }
2848       ierr = DMPlexRestoreTransitiveClosure(sp->dm,0,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
2849     }
2850     for (p = 0; p < pEnd; p++) if (symperms[p]) break;
2851     if (p == pEnd) {
2852       ierr = PetscFree(symperms);CHKERRQ(ierr);
2853       symperms = NULL;
2854     }
2855     for (p = 0; p < pEnd; p++) if (symflips[p]) break;
2856     if (p == pEnd) {
2857       ierr = PetscFree(symflips);CHKERRQ(ierr);
2858       symflips = NULL;
2859     }
2860     lag->symperms = symperms;
2861     lag->symflips = symflips;
2862     lag->symComputed = PETSC_TRUE;
2863   }
2864   if (perms) *perms = (const PetscInt ***) lag->symperms;
2865   if (flips) *flips = (const PetscScalar ***) lag->symflips;
2866   PetscFunctionReturn(0);
2867 }
2868 
2869 static PetscErrorCode PetscDualSpaceLagrangeGetContinuity_Lagrange(PetscDualSpace sp, PetscBool *continuous)
2870 {
2871   PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *) sp->data;
2872 
2873   PetscFunctionBegin;
2874   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
2875   PetscValidPointer(continuous, 2);
2876   *continuous = lag->continuous;
2877   PetscFunctionReturn(0);
2878 }
2879 
2880 static PetscErrorCode PetscDualSpaceLagrangeSetContinuity_Lagrange(PetscDualSpace sp, PetscBool continuous)
2881 {
2882   PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *) sp->data;
2883 
2884   PetscFunctionBegin;
2885   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
2886   lag->continuous = continuous;
2887   PetscFunctionReturn(0);
2888 }
2889 
2890 /*@
2891   PetscDualSpaceLagrangeGetContinuity - Retrieves the flag for element continuity
2892 
2893   Not Collective
2894 
2895   Input Parameter:
2896 . sp         - the PetscDualSpace
2897 
2898   Output Parameter:
2899 . continuous - flag for element continuity
2900 
2901   Level: intermediate
2902 
2903 .seealso: PetscDualSpaceLagrangeSetContinuity()
2904 @*/
2905 PetscErrorCode PetscDualSpaceLagrangeGetContinuity(PetscDualSpace sp, PetscBool *continuous)
2906 {
2907   PetscErrorCode ierr;
2908 
2909   PetscFunctionBegin;
2910   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
2911   PetscValidPointer(continuous, 2);
2912   ierr = PetscTryMethod(sp, "PetscDualSpaceLagrangeGetContinuity_C", (PetscDualSpace,PetscBool*),(sp,continuous));CHKERRQ(ierr);
2913   PetscFunctionReturn(0);
2914 }
2915 
2916 /*@
2917   PetscDualSpaceLagrangeSetContinuity - Indicate whether the element is continuous
2918 
2919   Logically Collective on sp
2920 
2921   Input Parameters:
2922 + sp         - the PetscDualSpace
2923 - continuous - flag for element continuity
2924 
2925   Options Database:
2926 . -petscdualspace_lagrange_continuity <bool>
2927 
2928   Level: intermediate
2929 
2930 .seealso: PetscDualSpaceLagrangeGetContinuity()
2931 @*/
2932 PetscErrorCode PetscDualSpaceLagrangeSetContinuity(PetscDualSpace sp, PetscBool continuous)
2933 {
2934   PetscErrorCode ierr;
2935 
2936   PetscFunctionBegin;
2937   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
2938   PetscValidLogicalCollectiveBool(sp, continuous, 2);
2939   ierr = PetscTryMethod(sp, "PetscDualSpaceLagrangeSetContinuity_C", (PetscDualSpace,PetscBool),(sp,continuous));CHKERRQ(ierr);
2940   PetscFunctionReturn(0);
2941 }
2942 
2943 static PetscErrorCode PetscDualSpaceLagrangeGetTensor_Lagrange(PetscDualSpace sp, PetscBool *tensor)
2944 {
2945   PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *)sp->data;
2946 
2947   PetscFunctionBegin;
2948   *tensor = lag->tensorSpace;
2949   PetscFunctionReturn(0);
2950 }
2951 
2952 static PetscErrorCode PetscDualSpaceLagrangeSetTensor_Lagrange(PetscDualSpace sp, PetscBool tensor)
2953 {
2954   PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *)sp->data;
2955 
2956   PetscFunctionBegin;
2957   lag->tensorSpace = tensor;
2958   PetscFunctionReturn(0);
2959 }
2960 
2961 static PetscErrorCode PetscDualSpaceLagrangeGetTrimmed_Lagrange(PetscDualSpace sp, PetscBool *trimmed)
2962 {
2963   PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *)sp->data;
2964 
2965   PetscFunctionBegin;
2966   *trimmed = lag->trimmed;
2967   PetscFunctionReturn(0);
2968 }
2969 
2970 static PetscErrorCode PetscDualSpaceLagrangeSetTrimmed_Lagrange(PetscDualSpace sp, PetscBool trimmed)
2971 {
2972   PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *)sp->data;
2973 
2974   PetscFunctionBegin;
2975   lag->trimmed = trimmed;
2976   PetscFunctionReturn(0);
2977 }
2978 
2979 static PetscErrorCode PetscDualSpaceLagrangeGetNodeType_Lagrange(PetscDualSpace sp, PetscDTNodeType *nodeType, PetscBool *boundary, PetscReal *exponent)
2980 {
2981   PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *)sp->data;
2982 
2983   PetscFunctionBegin;
2984   if (nodeType) *nodeType = lag->nodeType;
2985   if (boundary) *boundary = lag->endNodes;
2986   if (exponent) *exponent = lag->nodeExponent;
2987   PetscFunctionReturn(0);
2988 }
2989 
2990 static PetscErrorCode PetscDualSpaceLagrangeSetNodeType_Lagrange(PetscDualSpace sp, PetscDTNodeType nodeType, PetscBool boundary, PetscReal exponent)
2991 {
2992   PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *)sp->data;
2993 
2994   PetscFunctionBegin;
2995   if (nodeType == PETSCDTNODES_GAUSSJACOBI && exponent <= -1.) SETERRQ(PetscObjectComm((PetscObject) sp), PETSC_ERR_ARG_OUTOFRANGE, "Exponent must be > -1");
2996   lag->nodeType = nodeType;
2997   lag->endNodes = boundary;
2998   lag->nodeExponent = exponent;
2999   PetscFunctionReturn(0);
3000 }
3001 
3002 /*@
3003   PetscDualSpaceLagrangeGetTensor - Get the tensor nature of the dual space
3004 
3005   Not collective
3006 
3007   Input Parameter:
3008 . sp - The PetscDualSpace
3009 
3010   Output Parameter:
3011 . tensor - Whether the dual space has tensor layout (vs. simplicial)
3012 
3013   Level: intermediate
3014 
3015 .seealso: PetscDualSpaceLagrangeSetTensor(), PetscDualSpaceCreate()
3016 @*/
3017 PetscErrorCode PetscDualSpaceLagrangeGetTensor(PetscDualSpace sp, PetscBool *tensor)
3018 {
3019   PetscErrorCode ierr;
3020 
3021   PetscFunctionBegin;
3022   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
3023   PetscValidPointer(tensor, 2);
3024   ierr = PetscTryMethod(sp,"PetscDualSpaceLagrangeGetTensor_C",(PetscDualSpace,PetscBool *),(sp,tensor));CHKERRQ(ierr);
3025   PetscFunctionReturn(0);
3026 }
3027 
3028 /*@
3029   PetscDualSpaceLagrangeSetTensor - Set the tensor nature of the dual space
3030 
3031   Not collective
3032 
3033   Input Parameters:
3034 + sp - The PetscDualSpace
3035 - tensor - Whether the dual space has tensor layout (vs. simplicial)
3036 
3037   Level: intermediate
3038 
3039 .seealso: PetscDualSpaceLagrangeGetTensor(), PetscDualSpaceCreate()
3040 @*/
3041 PetscErrorCode PetscDualSpaceLagrangeSetTensor(PetscDualSpace sp, PetscBool tensor)
3042 {
3043   PetscErrorCode ierr;
3044 
3045   PetscFunctionBegin;
3046   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
3047   ierr = PetscTryMethod(sp,"PetscDualSpaceLagrangeSetTensor_C",(PetscDualSpace,PetscBool),(sp,tensor));CHKERRQ(ierr);
3048   PetscFunctionReturn(0);
3049 }
3050 
3051 /*@
3052   PetscDualSpaceLagrangeGetTrimmed - Get the trimmed nature of the dual space
3053 
3054   Not collective
3055 
3056   Input Parameter:
3057 . sp - The PetscDualSpace
3058 
3059   Output Parameter:
3060 . trimmed - Whether the dual space represents to dual basis of a trimmed polynomial space (e.g. Raviart-Thomas and higher order / other form degree variants)
3061 
3062   Level: intermediate
3063 
3064 .seealso: PetscDualSpaceLagrangeSetTrimmed(), PetscDualSpaceCreate()
3065 @*/
3066 PetscErrorCode PetscDualSpaceLagrangeGetTrimmed(PetscDualSpace sp, PetscBool *trimmed)
3067 {
3068   PetscErrorCode ierr;
3069 
3070   PetscFunctionBegin;
3071   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
3072   PetscValidPointer(trimmed, 2);
3073   ierr = PetscTryMethod(sp,"PetscDualSpaceLagrangeGetTrimmed_C",(PetscDualSpace,PetscBool *),(sp,trimmed));CHKERRQ(ierr);
3074   PetscFunctionReturn(0);
3075 }
3076 
3077 /*@
3078   PetscDualSpaceLagrangeSetTrimmed - Set the trimmed nature of the dual space
3079 
3080   Not collective
3081 
3082   Input Parameters:
3083 + sp - The PetscDualSpace
3084 - trimmed - Whether the dual space represents to dual basis of a trimmed polynomial space (e.g. Raviart-Thomas and higher order / other form degree variants)
3085 
3086   Level: intermediate
3087 
3088 .seealso: PetscDualSpaceLagrangeGetTrimmed(), PetscDualSpaceCreate()
3089 @*/
3090 PetscErrorCode PetscDualSpaceLagrangeSetTrimmed(PetscDualSpace sp, PetscBool trimmed)
3091 {
3092   PetscErrorCode ierr;
3093 
3094   PetscFunctionBegin;
3095   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
3096   ierr = PetscTryMethod(sp,"PetscDualSpaceLagrangeSetTrimmed_C",(PetscDualSpace,PetscBool),(sp,trimmed));CHKERRQ(ierr);
3097   PetscFunctionReturn(0);
3098 }
3099 
3100 /*@
3101   PetscDualSpaceLagrangeGetNodeType - Get a description of how nodes are laid out for Lagrange polynomials in this
3102   dual space
3103 
3104   Not collective
3105 
3106   Input Parameter:
3107 . sp - The PetscDualSpace
3108 
3109   Output Parameters:
3110 + nodeType - The type of nodes
3111 . boundary - Whether the node type is one that includes endpoints (if nodeType is PETSCDTNODES_GAUSSJACOBI, nodes that
3112              include the boundary are Gauss-Lobatto-Jacobi nodes)
3113 - exponent - If nodeType is PETSCDTNODES_GAUSJACOBI, indicates the exponent used for both ends of the 1D Jacobi weight function
3114              '0' is Gauss-Legendre, '-0.5' is Gauss-Chebyshev of the first type, '0.5' is Gauss-Chebyshev of the second type
3115 
3116   Level: advanced
3117 
3118 .seealso: PetscDTNodeType, PetscDualSpaceLagrangeSetNodeType()
3119 @*/
3120 PetscErrorCode PetscDualSpaceLagrangeGetNodeType(PetscDualSpace sp, PetscDTNodeType *nodeType, PetscBool *boundary, PetscReal *exponent)
3121 {
3122   PetscErrorCode ierr;
3123 
3124   PetscFunctionBegin;
3125   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
3126   if (nodeType) PetscValidPointer(nodeType, 2);
3127   if (boundary) PetscValidPointer(boundary, 3);
3128   if (exponent) PetscValidPointer(exponent, 4);
3129   ierr = PetscTryMethod(sp,"PetscDualSpaceLagrangeGetNodeType_C",(PetscDualSpace,PetscDTNodeType *,PetscBool *,PetscReal *),(sp,nodeType,boundary,exponent));CHKERRQ(ierr);
3130   PetscFunctionReturn(0);
3131 }
3132 
3133 /*@
3134   PetscDualSpaceLagrangeSetNodeType - Set a description of how nodes are laid out for Lagrange polynomials in this
3135   dual space
3136 
3137   Logically collective
3138 
3139   Input Parameters:
3140 + sp - The PetscDualSpace
3141 . nodeType - The type of nodes
3142 . boundary - Whether the node type is one that includes endpoints (if nodeType is PETSCDTNODES_GAUSSJACOBI, nodes that
3143              include the boundary are Gauss-Lobatto-Jacobi nodes)
3144 - exponent - If nodeType is PETSCDTNODES_GAUSJACOBI, indicates the exponent used for both ends of the 1D Jacobi weight function
3145              '0' is Gauss-Legendre, '-0.5' is Gauss-Chebyshev of the first type, '0.5' is Gauss-Chebyshev of the second type
3146 
3147   Level: advanced
3148 
3149 .seealso: PetscDTNodeType, PetscDualSpaceLagrangeGetNodeType()
3150 @*/
3151 PetscErrorCode PetscDualSpaceLagrangeSetNodeType(PetscDualSpace sp, PetscDTNodeType nodeType, PetscBool boundary, PetscReal exponent)
3152 {
3153   PetscErrorCode ierr;
3154 
3155   PetscFunctionBegin;
3156   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
3157   ierr = PetscTryMethod(sp,"PetscDualSpaceLagrangeSetNodeType_C",(PetscDualSpace,PetscDTNodeType,PetscBool,PetscReal),(sp,nodeType,boundary,exponent));CHKERRQ(ierr);
3158   PetscFunctionReturn(0);
3159 }
3160 
3161 
3162 static PetscErrorCode PetscDualSpaceInitialize_Lagrange(PetscDualSpace sp)
3163 {
3164   PetscFunctionBegin;
3165   sp->ops->destroy              = PetscDualSpaceDestroy_Lagrange;
3166   sp->ops->view                 = PetscDualSpaceView_Lagrange;
3167   sp->ops->setfromoptions       = PetscDualSpaceSetFromOptions_Lagrange;
3168   sp->ops->duplicate            = PetscDualSpaceDuplicate_Lagrange;
3169   sp->ops->setup                = PetscDualSpaceSetUp_Lagrange;
3170   sp->ops->createheightsubspace = NULL;
3171   sp->ops->createpointsubspace  = NULL;
3172   sp->ops->getsymmetries        = PetscDualSpaceGetSymmetries_Lagrange;
3173   sp->ops->apply                = PetscDualSpaceApplyDefault;
3174   sp->ops->applyall             = PetscDualSpaceApplyAllDefault;
3175   sp->ops->applyint             = PetscDualSpaceApplyInteriorDefault;
3176   sp->ops->createalldata        = PetscDualSpaceCreateAllDataDefault;
3177   sp->ops->createintdata        = PetscDualSpaceCreateInteriorDataDefault;
3178   PetscFunctionReturn(0);
3179 }
3180 
3181 /*MC
3182   PETSCDUALSPACELAGRANGE = "lagrange" - A PetscDualSpace object that encapsulates a dual space of pointwise evaluation functionals
3183 
3184   Level: intermediate
3185 
3186 .seealso: PetscDualSpaceType, PetscDualSpaceCreate(), PetscDualSpaceSetType()
3187 M*/
3188 PETSC_EXTERN PetscErrorCode PetscDualSpaceCreate_Lagrange(PetscDualSpace sp)
3189 {
3190   PetscDualSpace_Lag *lag;
3191   PetscErrorCode      ierr;
3192 
3193   PetscFunctionBegin;
3194   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
3195   ierr     = PetscNewLog(sp,&lag);CHKERRQ(ierr);
3196   sp->data = lag;
3197 
3198   lag->tensorCell  = PETSC_FALSE;
3199   lag->tensorSpace = PETSC_FALSE;
3200   lag->continuous  = PETSC_TRUE;
3201   lag->numCopies   = PETSC_DEFAULT;
3202   lag->numNodeSkip = PETSC_DEFAULT;
3203   lag->nodeType    = PETSCDTNODES_DEFAULT;
3204 
3205   ierr = PetscDualSpaceInitialize_Lagrange(sp);CHKERRQ(ierr);
3206   ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeGetContinuity_C", PetscDualSpaceLagrangeGetContinuity_Lagrange);CHKERRQ(ierr);
3207   ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeSetContinuity_C", PetscDualSpaceLagrangeSetContinuity_Lagrange);CHKERRQ(ierr);
3208   ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeGetTensor_C", PetscDualSpaceLagrangeGetTensor_Lagrange);CHKERRQ(ierr);
3209   ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeSetTensor_C", PetscDualSpaceLagrangeSetTensor_Lagrange);CHKERRQ(ierr);
3210   ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeGetTrimmed_C", PetscDualSpaceLagrangeGetTrimmed_Lagrange);CHKERRQ(ierr);
3211   ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeSetTrimmed_C", PetscDualSpaceLagrangeSetTrimmed_Lagrange);CHKERRQ(ierr);
3212   ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeGetNodeType_C", PetscDualSpaceLagrangeGetNodeType_Lagrange);CHKERRQ(ierr);
3213   ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeSetNodeType_C", PetscDualSpaceLagrangeSetNodeType_Lagrange);CHKERRQ(ierr);
3214   PetscFunctionReturn(0);
3215 }
3216