xref: /petsc/src/dm/impls/plex/plextree.c (revision bff67a9b4439570f10deaeebe845aeaede4dac2a)
1 #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2 #include <../src/sys/utils/hash.h>
3 #include <petsc/private/isimpl.h>
4 #include <petsc/private/petscfeimpl.h>
5 #include <petscsf.h>
6 #include <petscds.h>
7 
8 /** hierarchy routines */
9 
10 #undef __FUNCT__
11 #define __FUNCT__ "DMPlexSetReferenceTree"
12 /*@
13   DMPlexSetReferenceTree - set the reference tree for hierarchically non-conforming meshes.
14 
15   Not collective
16 
17   Input Parameters:
18 + dm - The DMPlex object
19 - ref - The reference tree DMPlex object
20 
21   Level: intermediate
22 
23 .seealso: DMPlexGetReferenceTree(), DMPlexCreateDefaultReferenceTree()
24 @*/
25 PetscErrorCode DMPlexSetReferenceTree(DM dm, DM ref)
26 {
27   DM_Plex        *mesh = (DM_Plex *)dm->data;
28   PetscErrorCode  ierr;
29 
30   PetscFunctionBegin;
31   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32   if (ref) {PetscValidHeaderSpecific(ref, DM_CLASSID, 2);}
33   ierr = PetscObjectReference((PetscObject)ref);CHKERRQ(ierr);
34   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
35   mesh->referenceTree = ref;
36   PetscFunctionReturn(0);
37 }
38 
39 #undef __FUNCT__
40 #define __FUNCT__ "DMPlexGetReferenceTree"
41 /*@
42   DMPlexGetReferenceTree - get the reference tree for hierarchically non-conforming meshes.
43 
44   Not collective
45 
46   Input Parameters:
47 . dm - The DMPlex object
48 
49   Output Parameters
50 . ref - The reference tree DMPlex object
51 
52   Level: intermediate
53 
54 .seealso: DMPlexSetReferenceTree(), DMPlexCreateDefaultReferenceTree()
55 @*/
56 PetscErrorCode DMPlexGetReferenceTree(DM dm, DM *ref)
57 {
58   DM_Plex        *mesh = (DM_Plex *)dm->data;
59 
60   PetscFunctionBegin;
61   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62   PetscValidPointer(ref,2);
63   *ref = mesh->referenceTree;
64   PetscFunctionReturn(0);
65 }
66 
67 #undef __FUNCT__
68 #define __FUNCT__ "DMPlexReferenceTreeGetChildSymmetry_Default"
69 static PetscErrorCode DMPlexReferenceTreeGetChildSymmetry_Default(DM dm, PetscInt parent, PetscInt parentOrientA, PetscInt childOrientA, PetscInt childA, PetscInt parentOrientB, PetscInt *childOrientB, PetscInt *childB)
70 {
71   PetscInt       coneSize, dStart, dEnd, dim, ABswap, oAvert, oBvert, ABswapVert;
72   PetscErrorCode ierr;
73 
74   PetscFunctionBegin;
75   if (parentOrientA == parentOrientB) {
76     if (childOrientB) *childOrientB = childOrientA;
77     if (childB) *childB = childA;
78     PetscFunctionReturn(0);
79   }
80   for (dim = 0; dim < 3; dim++) {
81     ierr = DMPlexGetDepthStratum(dm,dim,&dStart,&dEnd);CHKERRQ(ierr);
82     if (parent >= dStart && parent <= dEnd) {
83       break;
84     }
85   }
86   if (dim > 2) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot perform child symmetry for %d-cells",dim);
87   if (!dim) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"A vertex has no children");
88   if (childA < dStart || childA >= dEnd) {
89     /* this is a lower-dimensional child: bootstrap */
90     PetscInt size, i, sA = -1, sB, sOrientB, sConeSize;
91     const PetscInt *supp, *coneA, *coneB, *oA, *oB;
92 
93     ierr = DMPlexGetSupportSize(dm,childA,&size);CHKERRQ(ierr);
94     ierr = DMPlexGetSupport(dm,childA,&supp);CHKERRQ(ierr);
95 
96     /* find a point sA in supp(childA) that has the same parent */
97     for (i = 0; i < size; i++) {
98       PetscInt sParent;
99 
100       sA   = supp[i];
101       if (sA == parent) continue;
102       ierr = DMPlexGetTreeParent(dm,sA,&sParent,NULL);CHKERRQ(ierr);
103       if (sParent == parent) {
104         break;
105       }
106     }
107     if (i == size) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"could not find support in children");
108     /* find out which point sB is in an equivalent position to sA under
109      * parentOrientB */
110     ierr = DMPlexReferenceTreeGetChildSymmetry_Default(dm,parent,parentOrientA,0,sA,parentOrientB,&sOrientB,&sB);CHKERRQ(ierr);
111     ierr = DMPlexGetConeSize(dm,sA,&sConeSize);CHKERRQ(ierr);
112     ierr = DMPlexGetCone(dm,sA,&coneA);CHKERRQ(ierr);
113     ierr = DMPlexGetCone(dm,sB,&coneB);CHKERRQ(ierr);
114     ierr = DMPlexGetConeOrientation(dm,sA,&oA);CHKERRQ(ierr);
115     ierr = DMPlexGetConeOrientation(dm,sB,&oB);CHKERRQ(ierr);
116     /* step through the cone of sA in natural order */
117     for (i = 0; i < sConeSize; i++) {
118       if (coneA[i] == childA) {
119         /* if childA is at position i in coneA,
120          * then we want the point that is at sOrientB*i in coneB */
121         PetscInt j = (sOrientB >= 0) ? ((sOrientB + i) % sConeSize) : ((sConeSize -(sOrientB+1) - i) % sConeSize);
122         if (childB) *childB = coneB[j];
123         if (childOrientB) {
124           PetscInt oBtrue;
125 
126           ierr          = DMPlexGetConeSize(dm,childA,&coneSize);CHKERRQ(ierr);
127           /* compose sOrientB and oB[j] */
128           if (coneSize != 0 && coneSize != 2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expected a vertex or an edge");
129           /* we may have to flip an edge */
130           oBtrue        = coneSize ? ((sOrientB >= 0) ? oB[j] : -(oB[j] + 2)) : 0;
131           ABswap        = DihedralSwap(coneSize,oA[i],oBtrue);CHKERRQ(ierr);
132           *childOrientB = DihedralCompose(coneSize,childOrientA,ABswap);
133         }
134         break;
135       }
136     }
137     if (i == sConeSize) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"support cone mismatch");
138     PetscFunctionReturn(0);
139   }
140   /* get the cone size and symmetry swap */
141   ierr   = DMPlexGetConeSize(dm,parent,&coneSize);CHKERRQ(ierr);
142   ABswap = DihedralSwap(coneSize, parentOrientA, parentOrientB);
143   if (dim == 2) {
144     /* orientations refer to cones: we want them to refer to vertices:
145      * if it's a rotation, they are the same, but if the order is reversed, a
146      * permutation that puts side i first does *not* put vertex i first */
147     oAvert     = (parentOrientA >= 0) ? parentOrientA : -((-parentOrientA % coneSize) + 1);
148     oBvert     = (parentOrientB >= 0) ? parentOrientB : -((-parentOrientB % coneSize) + 1);
149     ABswapVert = DihedralSwap(coneSize, oAvert, oBvert);
150   } else {
151     ABswapVert = ABswap;
152   }
153   if (childB) {
154     /* assume that each child corresponds to a vertex, in the same order */
155     PetscInt p, posA = -1, numChildren, i;
156     const PetscInt *children;
157 
158     /* count which position the child is in */
159     ierr = DMPlexGetTreeChildren(dm,parent,&numChildren,&children);CHKERRQ(ierr);
160     for (i = 0; i < numChildren; i++) {
161       p = children[i];
162       if (p == childA) {
163         posA = i;
164         break;
165       }
166     }
167     if (posA >= coneSize) {
168       /* this is the triangle in the middle of a uniformly refined triangle: it
169        * is invariant */
170       if (dim != 2 || posA != 3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Expected a middle triangle, got something else");
171       *childB = childA;
172     }
173     else {
174       /* figure out position B by applying ABswapVert */
175       PetscInt posB;
176 
177       posB = (ABswapVert >= 0) ? ((ABswapVert + posA) % coneSize) : ((coneSize -(ABswapVert + 1) - posA) % coneSize);
178       if (childB) *childB = children[posB];
179     }
180   }
181   if (childOrientB) *childOrientB = DihedralCompose(coneSize,childOrientA,ABswap);
182   PetscFunctionReturn(0);
183 }
184 
185 #undef __FUNCT__
186 #define __FUNCT__ "DMPlexReferenceTreeGetChildSymmetry"
187 /*@
188   DMPlexReferenceTreeGetChildSymmetry - Given a reference tree, transform a childid and orientation from one parent frame to another
189 
190   Input Parameters:
191 + dm - the reference tree DMPlex object
192 . parent - the parent point
193 . parentOrientA - the reference orientation for describing the parent
194 . childOrientA - the reference orientation for describing the child
195 . childA - the reference childID for describing the child
196 - parentOrientB - the new orientation for describing the parent
197 
198   Output Parameters:
199 + childOrientB - if not NULL, set to the new oreintation for describing the child
200 . childB - if not NULL, the new childID for describing the child
201 
202   Level: developer
203 
204 .seealso: DMPlexGetReferenceTree(), DMPlexSetReferenceTree(), DMPlexSetTree()
205 @*/
206 PetscErrorCode DMPlexReferenceTreeGetChildSymmetry(DM dm, PetscInt parent, PetscInt parentOrientA, PetscInt childOrientA, PetscInt childA, PetscInt parentOrientB, PetscInt *childOrientB, PetscInt *childB)
207 {
208   DM_Plex        *mesh = (DM_Plex *)dm->data;
209   PetscErrorCode ierr;
210 
211   PetscFunctionBegin;
212   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
213   if (!mesh->getchildsymmetry) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"DMPlexReferenceTreeGetChildSymmetry not implemented");
214   ierr = mesh->getchildsymmetry(dm,parent,parentOrientA,childOrientA,childA,parentOrientB,childOrientB,childB);CHKERRQ(ierr);
215   PetscFunctionReturn(0);
216 }
217 
218 static PetscErrorCode DMPlexSetTree_Internal(DM,PetscSection,PetscInt*,PetscInt*,PetscBool,PetscBool);
219 
220 #undef __FUNCT__
221 #define __FUNCT__ "DMPlexCreateReferenceTree_Union"
222 PetscErrorCode DMPlexCreateReferenceTree_Union(DM K, DM Kref, const char *labelName, DM *ref)
223 {
224   MPI_Comm       comm;
225   PetscInt       dim, p, pStart, pEnd, pRefStart, pRefEnd, d, offset, parentSize, *parents, *childIDs;
226   PetscInt      *permvals, *unionCones, *coneSizes, *unionOrientations, numUnionPoints, *numDimPoints, numCones, numVerts;
227   DMLabel        identity, identityRef;
228   PetscSection   unionSection, unionConeSection, parentSection;
229   PetscScalar   *unionCoords;
230   IS             perm;
231   PetscErrorCode ierr;
232 
233   PetscFunctionBegin;
234   comm = PetscObjectComm((PetscObject)K);
235   ierr = DMGetDimension(K, &dim);CHKERRQ(ierr);
236   ierr = DMPlexGetChart(K, &pStart, &pEnd);CHKERRQ(ierr);
237   ierr = DMGetLabel(K, labelName, &identity);CHKERRQ(ierr);
238   ierr = DMGetLabel(Kref, labelName, &identityRef);CHKERRQ(ierr);
239   ierr = DMPlexGetChart(Kref, &pRefStart, &pRefEnd);CHKERRQ(ierr);
240   ierr = PetscSectionCreate(comm, &unionSection);CHKERRQ(ierr);
241   ierr = PetscSectionSetChart(unionSection, 0, (pEnd - pStart) + (pRefEnd - pRefStart));CHKERRQ(ierr);
242   /* count points that will go in the union */
243   for (p = pStart; p < pEnd; p++) {
244     ierr = PetscSectionSetDof(unionSection, p - pStart, 1);CHKERRQ(ierr);
245   }
246   for (p = pRefStart; p < pRefEnd; p++) {
247     PetscInt q, qSize;
248     ierr = DMLabelGetValue(identityRef, p, &q);CHKERRQ(ierr);
249     ierr = DMLabelGetStratumSize(identityRef, q, &qSize);CHKERRQ(ierr);
250     if (qSize > 1) {
251       ierr = PetscSectionSetDof(unionSection, p - pRefStart + (pEnd - pStart), 1);CHKERRQ(ierr);
252     }
253   }
254   ierr = PetscMalloc1(pEnd - pStart + pRefEnd - pRefStart,&permvals);CHKERRQ(ierr);
255   offset = 0;
256   /* stratify points in the union by topological dimension */
257   for (d = 0; d <= dim; d++) {
258     PetscInt cStart, cEnd, c;
259 
260     ierr = DMPlexGetHeightStratum(K, d, &cStart, &cEnd);CHKERRQ(ierr);
261     for (c = cStart; c < cEnd; c++) {
262       permvals[offset++] = c;
263     }
264 
265     ierr = DMPlexGetHeightStratum(Kref, d, &cStart, &cEnd);CHKERRQ(ierr);
266     for (c = cStart; c < cEnd; c++) {
267       permvals[offset++] = c + (pEnd - pStart);
268     }
269   }
270   ierr = ISCreateGeneral(comm, (pEnd - pStart) + (pRefEnd - pRefStart), permvals, PETSC_OWN_POINTER, &perm);CHKERRQ(ierr);
271   ierr = PetscSectionSetPermutation(unionSection,perm);CHKERRQ(ierr);
272   ierr = PetscSectionSetUp(unionSection);CHKERRQ(ierr);
273   ierr = PetscSectionGetStorageSize(unionSection,&numUnionPoints);CHKERRQ(ierr);
274   ierr = PetscMalloc2(numUnionPoints,&coneSizes,dim+1,&numDimPoints);CHKERRQ(ierr);
275   /* count dimension points */
276   for (d = 0; d <= dim; d++) {
277     PetscInt cStart, cOff, cOff2;
278     ierr = DMPlexGetHeightStratum(K,d,&cStart,NULL);CHKERRQ(ierr);
279     ierr = PetscSectionGetOffset(unionSection,cStart-pStart,&cOff);CHKERRQ(ierr);
280     if (d < dim) {
281       ierr = DMPlexGetHeightStratum(K,d+1,&cStart,NULL);CHKERRQ(ierr);
282       ierr = PetscSectionGetOffset(unionSection,cStart-pStart,&cOff2);CHKERRQ(ierr);
283     }
284     else {
285       cOff2 = numUnionPoints;
286     }
287     numDimPoints[dim - d] = cOff2 - cOff;
288   }
289   ierr = PetscSectionCreate(comm, &unionConeSection);CHKERRQ(ierr);
290   ierr = PetscSectionSetChart(unionConeSection, 0, numUnionPoints);CHKERRQ(ierr);
291   /* count the cones in the union */
292   for (p = pStart; p < pEnd; p++) {
293     PetscInt dof, uOff;
294 
295     ierr = DMPlexGetConeSize(K, p, &dof);CHKERRQ(ierr);
296     ierr = PetscSectionGetOffset(unionSection, p - pStart,&uOff);CHKERRQ(ierr);
297     ierr = PetscSectionSetDof(unionConeSection, uOff, dof);CHKERRQ(ierr);
298     coneSizes[uOff] = dof;
299   }
300   for (p = pRefStart; p < pRefEnd; p++) {
301     PetscInt dof, uDof, uOff;
302 
303     ierr = DMPlexGetConeSize(Kref, p, &dof);CHKERRQ(ierr);
304     ierr = PetscSectionGetDof(unionSection, p - pRefStart + (pEnd - pStart),&uDof);CHKERRQ(ierr);
305     ierr = PetscSectionGetOffset(unionSection, p - pRefStart + (pEnd - pStart),&uOff);CHKERRQ(ierr);
306     if (uDof) {
307       ierr = PetscSectionSetDof(unionConeSection, uOff, dof);CHKERRQ(ierr);
308       coneSizes[uOff] = dof;
309     }
310   }
311   ierr = PetscSectionSetUp(unionConeSection);CHKERRQ(ierr);
312   ierr = PetscSectionGetStorageSize(unionConeSection,&numCones);CHKERRQ(ierr);
313   ierr = PetscMalloc2(numCones,&unionCones,numCones,&unionOrientations);CHKERRQ(ierr);
314   /* write the cones in the union */
315   for (p = pStart; p < pEnd; p++) {
316     PetscInt dof, uOff, c, cOff;
317     const PetscInt *cone, *orientation;
318 
319     ierr = DMPlexGetConeSize(K, p, &dof);CHKERRQ(ierr);
320     ierr = DMPlexGetCone(K, p, &cone);CHKERRQ(ierr);
321     ierr = DMPlexGetConeOrientation(K, p, &orientation);CHKERRQ(ierr);
322     ierr = PetscSectionGetOffset(unionSection, p - pStart,&uOff);CHKERRQ(ierr);
323     ierr = PetscSectionGetOffset(unionConeSection,uOff,&cOff);CHKERRQ(ierr);
324     for (c = 0; c < dof; c++) {
325       PetscInt e, eOff;
326       e                           = cone[c];
327       ierr                        = PetscSectionGetOffset(unionSection, e - pStart, &eOff);CHKERRQ(ierr);
328       unionCones[cOff + c]        = eOff;
329       unionOrientations[cOff + c] = orientation[c];
330     }
331   }
332   for (p = pRefStart; p < pRefEnd; p++) {
333     PetscInt dof, uDof, uOff, c, cOff;
334     const PetscInt *cone, *orientation;
335 
336     ierr = DMPlexGetConeSize(Kref, p, &dof);CHKERRQ(ierr);
337     ierr = DMPlexGetCone(Kref, p, &cone);CHKERRQ(ierr);
338     ierr = DMPlexGetConeOrientation(Kref, p, &orientation);CHKERRQ(ierr);
339     ierr = PetscSectionGetDof(unionSection, p - pRefStart + (pEnd - pStart),&uDof);CHKERRQ(ierr);
340     ierr = PetscSectionGetOffset(unionSection, p - pRefStart + (pEnd - pStart),&uOff);CHKERRQ(ierr);
341     if (uDof) {
342       ierr = PetscSectionGetOffset(unionConeSection,uOff,&cOff);CHKERRQ(ierr);
343       for (c = 0; c < dof; c++) {
344         PetscInt e, eOff, eDof;
345 
346         e    = cone[c];
347         ierr = PetscSectionGetDof(unionSection, e - pRefStart + (pEnd - pStart),&eDof);CHKERRQ(ierr);
348         if (eDof) {
349           ierr = PetscSectionGetOffset(unionSection, e - pRefStart + (pEnd - pStart), &eOff);CHKERRQ(ierr);
350         }
351         else {
352           ierr = DMLabelGetValue(identityRef, e, &e);CHKERRQ(ierr);
353           ierr = PetscSectionGetOffset(unionSection, e - pStart, &eOff);CHKERRQ(ierr);
354         }
355         unionCones[cOff + c]        = eOff;
356         unionOrientations[cOff + c] = orientation[c];
357       }
358     }
359   }
360   /* get the coordinates */
361   {
362     PetscInt vStart, vEnd, vRefStart, vRefEnd, v, vDof, vOff;
363     PetscSection KcoordsSec, KrefCoordsSec;
364     Vec      KcoordsVec, KrefCoordsVec;
365     PetscScalar *Kcoords;
366 
367     DMGetCoordinateSection(K, &KcoordsSec);CHKERRQ(ierr);
368     DMGetCoordinatesLocal(K, &KcoordsVec);CHKERRQ(ierr);
369     DMGetCoordinateSection(Kref, &KrefCoordsSec);CHKERRQ(ierr);
370     DMGetCoordinatesLocal(Kref, &KrefCoordsVec);CHKERRQ(ierr);
371 
372     numVerts = numDimPoints[0];
373     ierr     = PetscMalloc1(numVerts * dim,&unionCoords);CHKERRQ(ierr);
374     ierr     = DMPlexGetDepthStratum(K,0,&vStart,&vEnd);CHKERRQ(ierr);
375 
376     offset = 0;
377     for (v = vStart; v < vEnd; v++) {
378       ierr = PetscSectionGetOffset(unionSection,v - pStart,&vOff);CHKERRQ(ierr);
379       ierr = VecGetValuesSection(KcoordsVec, KcoordsSec, v, &Kcoords);CHKERRQ(ierr);
380       for (d = 0; d < dim; d++) {
381         unionCoords[offset * dim + d] = Kcoords[d];
382       }
383       offset++;
384     }
385     ierr = DMPlexGetDepthStratum(Kref,0,&vRefStart,&vRefEnd);CHKERRQ(ierr);
386     for (v = vRefStart; v < vRefEnd; v++) {
387       ierr = PetscSectionGetDof(unionSection,v - pRefStart + (pEnd - pStart),&vDof);CHKERRQ(ierr);
388       ierr = PetscSectionGetOffset(unionSection,v - pRefStart + (pEnd - pStart),&vOff);CHKERRQ(ierr);
389       ierr = VecGetValuesSection(KrefCoordsVec, KrefCoordsSec, v, &Kcoords);CHKERRQ(ierr);
390       if (vDof) {
391         for (d = 0; d < dim; d++) {
392           unionCoords[offset * dim + d] = Kcoords[d];
393         }
394         offset++;
395       }
396     }
397   }
398   ierr = DMCreate(comm,ref);CHKERRQ(ierr);
399   ierr = DMSetType(*ref,DMPLEX);CHKERRQ(ierr);
400   ierr = DMSetDimension(*ref,dim);CHKERRQ(ierr);
401   ierr = DMPlexCreateFromDAG(*ref,dim,numDimPoints,coneSizes,unionCones,unionOrientations,unionCoords);CHKERRQ(ierr);
402   /* set the tree */
403   ierr = PetscSectionCreate(comm,&parentSection);CHKERRQ(ierr);
404   ierr = PetscSectionSetChart(parentSection,0,numUnionPoints);CHKERRQ(ierr);
405   for (p = pRefStart; p < pRefEnd; p++) {
406     PetscInt uDof, uOff;
407 
408     ierr = PetscSectionGetDof(unionSection, p - pRefStart + (pEnd - pStart),&uDof);CHKERRQ(ierr);
409     ierr = PetscSectionGetOffset(unionSection, p - pRefStart + (pEnd - pStart),&uOff);CHKERRQ(ierr);
410     if (uDof) {
411       PetscSectionSetDof(parentSection,uOff,1);CHKERRQ(ierr);
412     }
413   }
414   ierr = PetscSectionSetUp(parentSection);CHKERRQ(ierr);
415   ierr = PetscSectionGetStorageSize(parentSection,&parentSize);CHKERRQ(ierr);
416   ierr = PetscMalloc2(parentSize,&parents,parentSize,&childIDs);CHKERRQ(ierr);
417   for (p = pRefStart; p < pRefEnd; p++) {
418     PetscInt uDof, uOff;
419 
420     ierr = PetscSectionGetDof(unionSection, p - pRefStart + (pEnd - pStart),&uDof);CHKERRQ(ierr);
421     ierr = PetscSectionGetOffset(unionSection, p - pRefStart + (pEnd - pStart),&uOff);CHKERRQ(ierr);
422     if (uDof) {
423       PetscInt pOff, parent, parentU;
424       PetscSectionGetOffset(parentSection,uOff,&pOff);CHKERRQ(ierr);
425       DMLabelGetValue(identityRef,p,&parent);CHKERRQ(ierr);
426       ierr = PetscSectionGetOffset(unionSection, parent - pStart,&parentU);CHKERRQ(ierr);
427       parents[pOff] = parentU;
428       childIDs[pOff] = uOff;
429     }
430   }
431   ierr = DMPlexSetTree_Internal(*ref,parentSection,parents,childIDs,PETSC_TRUE,PETSC_FALSE);CHKERRQ(ierr);
432   ierr = PetscSectionDestroy(&parentSection);CHKERRQ(ierr);
433   ierr = PetscFree2(parents,childIDs);CHKERRQ(ierr);
434 
435   /* clean up */
436   ierr = PetscSectionDestroy(&unionSection);CHKERRQ(ierr);
437   ierr = PetscSectionDestroy(&unionConeSection);CHKERRQ(ierr);
438   ierr = ISDestroy(&perm);CHKERRQ(ierr);
439   ierr = PetscFree(unionCoords);CHKERRQ(ierr);
440   ierr = PetscFree2(unionCones,unionOrientations);CHKERRQ(ierr);
441   ierr = PetscFree2(coneSizes,numDimPoints);CHKERRQ(ierr);
442   PetscFunctionReturn(0);
443 }
444 
445 #undef __FUNCT__
446 #define __FUNCT__ "DMPlexCreateDefaultReferenceTree"
447 /*@
448   DMPlexCreateDefaultReferenceTree - create a reference tree for isotropic hierarchical mesh refinement.
449 
450   Collective on comm
451 
452   Input Parameters:
453 + comm    - the MPI communicator
454 . dim     - the spatial dimension
455 - simplex - Flag for simplex, otherwise use a tensor-product cell
456 
457   Output Parameters:
458 . ref     - the reference tree DMPlex object
459 
460   Level: intermediate
461 
462 .keywords: reference cell
463 .seealso: DMPlexSetReferenceTree(), DMPlexGetReferenceTree()
464 @*/
465 PetscErrorCode DMPlexCreateDefaultReferenceTree(MPI_Comm comm, PetscInt dim, PetscBool simplex, DM *ref)
466 {
467   DM_Plex       *mesh;
468   DM             K, Kref;
469   PetscInt       p, pStart, pEnd;
470   DMLabel        identity;
471   PetscErrorCode ierr;
472 
473   PetscFunctionBegin;
474 #if 1
475   comm = PETSC_COMM_SELF;
476 #endif
477   /* create a reference element */
478   ierr = DMPlexCreateReferenceCell(comm, dim, simplex, &K);CHKERRQ(ierr);
479   ierr = DMCreateLabel(K, "identity");CHKERRQ(ierr);
480   ierr = DMGetLabel(K, "identity", &identity);CHKERRQ(ierr);
481   ierr = DMPlexGetChart(K, &pStart, &pEnd);CHKERRQ(ierr);
482   for (p = pStart; p < pEnd; p++) {
483     ierr = DMLabelSetValue(identity, p, p);CHKERRQ(ierr);
484   }
485   /* refine it */
486   ierr = DMRefine(K,comm,&Kref);CHKERRQ(ierr);
487 
488   /* the reference tree is the union of these two, without duplicating
489    * points that appear in both */
490   ierr = DMPlexCreateReferenceTree_Union(K, Kref, "identity", ref);CHKERRQ(ierr);
491   mesh = (DM_Plex *) (*ref)->data;
492   mesh->getchildsymmetry = DMPlexReferenceTreeGetChildSymmetry_Default;
493   ierr = DMDestroy(&K);CHKERRQ(ierr);
494   ierr = DMDestroy(&Kref);CHKERRQ(ierr);
495   PetscFunctionReturn(0);
496 }
497 
498 #undef __FUNCT__
499 #define __FUNCT__ "DMPlexTreeSymmetrize"
500 static PetscErrorCode DMPlexTreeSymmetrize(DM dm)
501 {
502   DM_Plex        *mesh = (DM_Plex *)dm->data;
503   PetscSection   childSec, pSec;
504   PetscInt       p, pSize, cSize, parMax = PETSC_MIN_INT, parMin = PETSC_MAX_INT;
505   PetscInt       *offsets, *children, pStart, pEnd;
506   PetscErrorCode ierr;
507 
508   PetscFunctionBegin;
509   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
510   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
511   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
512   pSec = mesh->parentSection;
513   if (!pSec) PetscFunctionReturn(0);
514   ierr = PetscSectionGetStorageSize(pSec,&pSize);CHKERRQ(ierr);
515   for (p = 0; p < pSize; p++) {
516     PetscInt par = mesh->parents[p];
517 
518     parMax = PetscMax(parMax,par+1);
519     parMin = PetscMin(parMin,par);
520   }
521   if (parMin > parMax) {
522     parMin = -1;
523     parMax = -1;
524   }
525   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)pSec),&childSec);CHKERRQ(ierr);
526   ierr = PetscSectionSetChart(childSec,parMin,parMax);CHKERRQ(ierr);
527   for (p = 0; p < pSize; p++) {
528     PetscInt par = mesh->parents[p];
529 
530     ierr = PetscSectionAddDof(childSec,par,1);CHKERRQ(ierr);
531   }
532   ierr = PetscSectionSetUp(childSec);CHKERRQ(ierr);
533   ierr = PetscSectionGetStorageSize(childSec,&cSize);CHKERRQ(ierr);
534   ierr = PetscMalloc1(cSize,&children);CHKERRQ(ierr);
535   ierr = PetscCalloc1(parMax-parMin,&offsets);CHKERRQ(ierr);
536   ierr = PetscSectionGetChart(pSec,&pStart,&pEnd);CHKERRQ(ierr);
537   for (p = pStart; p < pEnd; p++) {
538     PetscInt dof, off, i;
539 
540     ierr = PetscSectionGetDof(pSec,p,&dof);CHKERRQ(ierr);
541     ierr = PetscSectionGetOffset(pSec,p,&off);CHKERRQ(ierr);
542     for (i = 0; i < dof; i++) {
543       PetscInt par = mesh->parents[off + i], cOff;
544 
545       ierr = PetscSectionGetOffset(childSec,par,&cOff);CHKERRQ(ierr);
546       children[cOff + offsets[par-parMin]++] = p;
547     }
548   }
549   mesh->childSection = childSec;
550   mesh->children = children;
551   ierr = PetscFree(offsets);CHKERRQ(ierr);
552   PetscFunctionReturn(0);
553 }
554 
555 #undef __FUNCT__
556 #define __FUNCT__ "AnchorsFlatten"
557 static PetscErrorCode AnchorsFlatten (PetscSection section, IS is, PetscSection *sectionNew, IS *isNew)
558 {
559   PetscInt       pStart, pEnd, size, sizeNew, i, p, *valsNew = NULL;
560   const PetscInt *vals;
561   PetscSection   secNew;
562   PetscBool      anyNew, globalAnyNew;
563   PetscBool      compress;
564   PetscErrorCode ierr;
565 
566   PetscFunctionBegin;
567   ierr = PetscSectionGetChart(section,&pStart,&pEnd);CHKERRQ(ierr);
568   ierr = ISGetLocalSize(is,&size);CHKERRQ(ierr);
569   ierr = ISGetIndices(is,&vals);CHKERRQ(ierr);
570   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)section),&secNew);CHKERRQ(ierr);
571   ierr = PetscSectionSetChart(secNew,pStart,pEnd);CHKERRQ(ierr);
572   for (i = 0; i < size; i++) {
573     PetscInt dof;
574 
575     p = vals[i];
576     if (p < pStart || p >= pEnd) continue;
577     ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
578     if (dof) break;
579   }
580   if (i == size) {
581     ierr     = PetscSectionSetUp(secNew);CHKERRQ(ierr);
582     anyNew   = PETSC_FALSE;
583     compress = PETSC_FALSE;
584     sizeNew  = 0;
585   }
586   else {
587     anyNew = PETSC_TRUE;
588     for (p = pStart; p < pEnd; p++) {
589       PetscInt dof, off;
590 
591       ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
592       ierr = PetscSectionGetOffset(section, p, &off);CHKERRQ(ierr);
593       for (i = 0; i < dof; i++) {
594         PetscInt q = vals[off + i], qDof = 0;
595 
596         if (q >= pStart && q < pEnd) {
597           ierr = PetscSectionGetDof(section, q, &qDof);CHKERRQ(ierr);
598         }
599         if (qDof) {
600           ierr = PetscSectionAddDof(secNew, p, qDof);CHKERRQ(ierr);
601         }
602         else {
603           ierr = PetscSectionAddDof(secNew, p, 1);CHKERRQ(ierr);
604         }
605       }
606     }
607     ierr = PetscSectionSetUp(secNew);CHKERRQ(ierr);
608     ierr = PetscSectionGetStorageSize(secNew,&sizeNew);CHKERRQ(ierr);
609     ierr = PetscMalloc1(sizeNew,&valsNew);CHKERRQ(ierr);
610     compress = PETSC_FALSE;
611     for (p = pStart; p < pEnd; p++) {
612       PetscInt dof, off, count, offNew, dofNew;
613 
614       ierr  = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
615       ierr  = PetscSectionGetOffset(section, p, &off);CHKERRQ(ierr);
616       ierr  = PetscSectionGetDof(secNew, p, &dofNew);CHKERRQ(ierr);
617       ierr  = PetscSectionGetOffset(secNew, p, &offNew);CHKERRQ(ierr);
618       count = 0;
619       for (i = 0; i < dof; i++) {
620         PetscInt q = vals[off + i], qDof = 0, qOff = 0, j;
621 
622         if (q >= pStart && q < pEnd) {
623           ierr = PetscSectionGetDof(section, q, &qDof);CHKERRQ(ierr);
624           ierr = PetscSectionGetOffset(section, q, &qOff);CHKERRQ(ierr);
625         }
626         if (qDof) {
627           PetscInt oldCount = count;
628 
629           for (j = 0; j < qDof; j++) {
630             PetscInt k, r = vals[qOff + j];
631 
632             for (k = 0; k < oldCount; k++) {
633               if (valsNew[offNew + k] == r) {
634                 break;
635               }
636             }
637             if (k == oldCount) {
638               valsNew[offNew + count++] = r;
639             }
640           }
641         }
642         else {
643           PetscInt k, oldCount = count;
644 
645           for (k = 0; k < oldCount; k++) {
646             if (valsNew[offNew + k] == q) {
647               break;
648             }
649           }
650           if (k == oldCount) {
651             valsNew[offNew + count++] = q;
652           }
653         }
654       }
655       if (count < dofNew) {
656         ierr = PetscSectionSetDof(secNew, p, count);CHKERRQ(ierr);
657         compress = PETSC_TRUE;
658       }
659     }
660   }
661   ierr = ISRestoreIndices(is,&vals);CHKERRQ(ierr);
662   ierr = MPIU_Allreduce(&anyNew,&globalAnyNew,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)secNew));CHKERRQ(ierr);
663   if (!globalAnyNew) {
664     ierr = PetscSectionDestroy(&secNew);CHKERRQ(ierr);
665     *sectionNew = NULL;
666     *isNew = NULL;
667   }
668   else {
669     PetscBool globalCompress;
670 
671     ierr = MPIU_Allreduce(&compress,&globalCompress,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)secNew));CHKERRQ(ierr);
672     if (compress) {
673       PetscSection secComp;
674       PetscInt *valsComp = NULL;
675 
676       ierr = PetscSectionCreate(PetscObjectComm((PetscObject)section),&secComp);CHKERRQ(ierr);
677       ierr = PetscSectionSetChart(secComp,pStart,pEnd);CHKERRQ(ierr);
678       for (p = pStart; p < pEnd; p++) {
679         PetscInt dof;
680 
681         ierr = PetscSectionGetDof(secNew, p, &dof);CHKERRQ(ierr);
682         ierr = PetscSectionSetDof(secComp, p, dof);CHKERRQ(ierr);
683       }
684       ierr = PetscSectionSetUp(secComp);CHKERRQ(ierr);
685       ierr = PetscSectionGetStorageSize(secComp,&sizeNew);CHKERRQ(ierr);
686       ierr = PetscMalloc1(sizeNew,&valsComp);CHKERRQ(ierr);
687       for (p = pStart; p < pEnd; p++) {
688         PetscInt dof, off, offNew, j;
689 
690         ierr = PetscSectionGetDof(secNew, p, &dof);CHKERRQ(ierr);
691         ierr = PetscSectionGetOffset(secNew, p, &off);CHKERRQ(ierr);
692         ierr = PetscSectionGetOffset(secComp, p, &offNew);CHKERRQ(ierr);
693         for (j = 0; j < dof; j++) {
694           valsComp[offNew + j] = valsNew[off + j];
695         }
696       }
697       ierr    = PetscSectionDestroy(&secNew);CHKERRQ(ierr);
698       secNew  = secComp;
699       ierr    = PetscFree(valsNew);CHKERRQ(ierr);
700       valsNew = valsComp;
701     }
702     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)is),sizeNew,valsNew,PETSC_OWN_POINTER,isNew);CHKERRQ(ierr);
703   }
704   PetscFunctionReturn(0);
705 }
706 
707 #undef __FUNCT__
708 #define __FUNCT__ "DMPlexCreateAnchors_Tree"
709 static PetscErrorCode DMPlexCreateAnchors_Tree(DM dm)
710 {
711   PetscInt       p, pStart, pEnd, *anchors, size;
712   PetscInt       aMin = PETSC_MAX_INT, aMax = PETSC_MIN_INT;
713   PetscSection   aSec;
714   DMLabel        canonLabel;
715   IS             aIS;
716   PetscErrorCode ierr;
717 
718   PetscFunctionBegin;
719   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
720   ierr = DMPlexGetChart(dm,&pStart,&pEnd);CHKERRQ(ierr);
721   ierr = DMGetLabel(dm,"canonical",&canonLabel);CHKERRQ(ierr);
722   for (p = pStart; p < pEnd; p++) {
723     PetscInt parent;
724 
725     if (canonLabel) {
726       PetscInt canon;
727 
728       ierr = DMLabelGetValue(canonLabel,p,&canon);CHKERRQ(ierr);
729       if (p != canon) continue;
730     }
731     ierr = DMPlexGetTreeParent(dm,p,&parent,NULL);CHKERRQ(ierr);
732     if (parent != p) {
733       aMin = PetscMin(aMin,p);
734       aMax = PetscMax(aMax,p+1);
735     }
736   }
737   if (aMin > aMax) {
738     aMin = -1;
739     aMax = -1;
740   }
741   ierr = PetscSectionCreate(PETSC_COMM_SELF,&aSec);CHKERRQ(ierr);
742   ierr = PetscSectionSetChart(aSec,aMin,aMax);CHKERRQ(ierr);
743   for (p = aMin; p < aMax; p++) {
744     PetscInt parent, ancestor = p;
745 
746     if (canonLabel) {
747       PetscInt canon;
748 
749       ierr = DMLabelGetValue(canonLabel,p,&canon);CHKERRQ(ierr);
750       if (p != canon) continue;
751     }
752     ierr = DMPlexGetTreeParent(dm,p,&parent,NULL);CHKERRQ(ierr);
753     while (parent != ancestor) {
754       ancestor = parent;
755       ierr     = DMPlexGetTreeParent(dm,ancestor,&parent,NULL);CHKERRQ(ierr);
756     }
757     if (ancestor != p) {
758       PetscInt closureSize, *closure = NULL;
759 
760       ierr = DMPlexGetTransitiveClosure(dm,ancestor,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
761       ierr = PetscSectionSetDof(aSec,p,closureSize);CHKERRQ(ierr);
762       ierr = DMPlexRestoreTransitiveClosure(dm,ancestor,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
763     }
764   }
765   ierr = PetscSectionSetUp(aSec);CHKERRQ(ierr);
766   ierr = PetscSectionGetStorageSize(aSec,&size);CHKERRQ(ierr);
767   ierr = PetscMalloc1(size,&anchors);CHKERRQ(ierr);
768   for (p = aMin; p < aMax; p++) {
769     PetscInt parent, ancestor = p;
770 
771     if (canonLabel) {
772       PetscInt canon;
773 
774       ierr = DMLabelGetValue(canonLabel,p,&canon);CHKERRQ(ierr);
775       if (p != canon) continue;
776     }
777     ierr = DMPlexGetTreeParent(dm,p,&parent,NULL);CHKERRQ(ierr);
778     while (parent != ancestor) {
779       ancestor = parent;
780       ierr     = DMPlexGetTreeParent(dm,ancestor,&parent,NULL);CHKERRQ(ierr);
781     }
782     if (ancestor != p) {
783       PetscInt j, closureSize, *closure = NULL, aOff;
784 
785       ierr = PetscSectionGetOffset(aSec,p,&aOff);CHKERRQ(ierr);
786 
787       ierr = DMPlexGetTransitiveClosure(dm,ancestor,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
788       for (j = 0; j < closureSize; j++) {
789         anchors[aOff + j] = closure[2*j];
790       }
791       ierr = DMPlexRestoreTransitiveClosure(dm,ancestor,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
792     }
793   }
794   ierr = ISCreateGeneral(PETSC_COMM_SELF,size,anchors,PETSC_OWN_POINTER,&aIS);CHKERRQ(ierr);
795   {
796     PetscSection aSecNew = aSec;
797     IS           aISNew  = aIS;
798 
799     ierr = PetscObjectReference((PetscObject)aSec);CHKERRQ(ierr);
800     ierr = PetscObjectReference((PetscObject)aIS);CHKERRQ(ierr);
801     while (aSecNew) {
802       ierr    = PetscSectionDestroy(&aSec);CHKERRQ(ierr);
803       ierr    = ISDestroy(&aIS);CHKERRQ(ierr);
804       aSec    = aSecNew;
805       aIS     = aISNew;
806       aSecNew = NULL;
807       aISNew  = NULL;
808       ierr    = AnchorsFlatten(aSec,aIS,&aSecNew,&aISNew);CHKERRQ(ierr);
809     }
810   }
811   ierr = DMPlexSetAnchors(dm,aSec,aIS);CHKERRQ(ierr);
812   ierr = PetscSectionDestroy(&aSec);CHKERRQ(ierr);
813   ierr = ISDestroy(&aIS);CHKERRQ(ierr);
814   PetscFunctionReturn(0);
815 }
816 
817 #undef __FUNCT__
818 #define __FUNCT__ "DMPlexGetTrueSupportSize"
819 static PetscErrorCode DMPlexGetTrueSupportSize(DM dm,PetscInt p,PetscInt *dof,PetscInt *numTrueSupp)
820 {
821   PetscErrorCode ierr;
822 
823   PetscFunctionBegin;
824   if (numTrueSupp[p] == -1) {
825     PetscInt i, alldof;
826     const PetscInt *supp;
827     PetscInt count = 0;
828 
829     ierr = DMPlexGetSupportSize(dm,p,&alldof);CHKERRQ(ierr);
830     ierr = DMPlexGetSupport(dm,p,&supp);CHKERRQ(ierr);
831     for (i = 0; i < alldof; i++) {
832       PetscInt q = supp[i], numCones, j;
833       const PetscInt *cone;
834 
835       ierr = DMPlexGetConeSize(dm,q,&numCones);CHKERRQ(ierr);
836       ierr = DMPlexGetCone(dm,q,&cone);CHKERRQ(ierr);
837       for (j = 0; j < numCones; j++) {
838         if (cone[j] == p) break;
839       }
840       if (j < numCones) count++;
841     }
842     numTrueSupp[p] = count;
843   }
844   *dof = numTrueSupp[p];
845   PetscFunctionReturn(0);
846 }
847 
848 #undef __FUNCT__
849 #define __FUNCT__ "DMPlexTreeExchangeSupports"
850 static PetscErrorCode DMPlexTreeExchangeSupports(DM dm)
851 {
852   DM_Plex *mesh = (DM_Plex *)dm->data;
853   PetscSection newSupportSection;
854   PetscInt newSize, *newSupports, pStart, pEnd, p, d, depth;
855   PetscInt *numTrueSupp;
856   PetscInt *offsets;
857   PetscErrorCode ierr;
858 
859   PetscFunctionBegin;
860   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
861   /* symmetrize the hierarchy */
862   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
863   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)(mesh->supportSection)),&newSupportSection);CHKERRQ(ierr);
864   ierr = DMPlexGetChart(dm,&pStart,&pEnd);CHKERRQ(ierr);
865   ierr = PetscSectionSetChart(newSupportSection,pStart,pEnd);CHKERRQ(ierr);
866   ierr = PetscCalloc1(pEnd,&offsets);CHKERRQ(ierr);
867   ierr = PetscMalloc1(pEnd,&numTrueSupp);CHKERRQ(ierr);
868   for (p = 0; p < pEnd; p++) numTrueSupp[p] = -1;
869   /* if a point is in the (true) support of q, it should be in the support of
870    * parent(q) */
871   for (d = 0; d <= depth; d++) {
872     ierr = DMPlexGetHeightStratum(dm,d,&pStart,&pEnd);CHKERRQ(ierr);
873     for (p = pStart; p < pEnd; ++p) {
874       PetscInt dof, q, qdof, parent;
875 
876       ierr = DMPlexGetTrueSupportSize(dm,p,&dof,numTrueSupp);CHKERRQ(ierr);
877       ierr = PetscSectionAddDof(newSupportSection, p, dof);CHKERRQ(ierr);
878       q    = p;
879       ierr = DMPlexGetTreeParent(dm,q,&parent,NULL);CHKERRQ(ierr);
880       while (parent != q && parent >= pStart && parent < pEnd) {
881         q = parent;
882 
883         ierr = DMPlexGetTrueSupportSize(dm,q,&qdof,numTrueSupp);CHKERRQ(ierr);
884         ierr = PetscSectionAddDof(newSupportSection,p,qdof);CHKERRQ(ierr);
885         ierr = PetscSectionAddDof(newSupportSection,q,dof);CHKERRQ(ierr);
886         ierr = DMPlexGetTreeParent(dm,q,&parent,NULL);CHKERRQ(ierr);
887       }
888     }
889   }
890   ierr = PetscSectionSetUp(newSupportSection);CHKERRQ(ierr);
891   ierr = PetscSectionGetStorageSize(newSupportSection,&newSize);CHKERRQ(ierr);
892   ierr = PetscMalloc1(newSize,&newSupports);CHKERRQ(ierr);
893   for (d = 0; d <= depth; d++) {
894     ierr = DMPlexGetHeightStratum(dm,d,&pStart,&pEnd);CHKERRQ(ierr);
895     for (p = pStart; p < pEnd; p++) {
896       PetscInt dof, off, q, qdof, qoff, newDof, newOff, newqOff, i, parent;
897 
898       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
899       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
900       ierr = PetscSectionGetDof(newSupportSection, p, &newDof);CHKERRQ(ierr);
901       ierr = PetscSectionGetOffset(newSupportSection, p, &newOff);CHKERRQ(ierr);
902       for (i = 0; i < dof; i++) {
903         PetscInt numCones, j;
904         const PetscInt *cone;
905         PetscInt q = mesh->supports[off + i];
906 
907         ierr = DMPlexGetConeSize(dm,q,&numCones);CHKERRQ(ierr);
908         ierr = DMPlexGetCone(dm,q,&cone);CHKERRQ(ierr);
909         for (j = 0; j < numCones; j++) {
910           if (cone[j] == p) break;
911         }
912         if (j < numCones) newSupports[newOff+offsets[p]++] = q;
913       }
914       mesh->maxSupportSize = PetscMax(mesh->maxSupportSize,newDof);
915 
916       q    = p;
917       ierr = DMPlexGetTreeParent(dm,q,&parent,NULL);CHKERRQ(ierr);
918       while (parent != q && parent >= pStart && parent < pEnd) {
919         q = parent;
920         ierr = PetscSectionGetDof(mesh->supportSection, q, &qdof);CHKERRQ(ierr);
921         ierr = PetscSectionGetOffset(mesh->supportSection, q, &qoff);CHKERRQ(ierr);
922         ierr = PetscSectionGetOffset(newSupportSection, q, &newqOff);CHKERRQ(ierr);
923         for (i = 0; i < qdof; i++) {
924           PetscInt numCones, j;
925           const PetscInt *cone;
926           PetscInt r = mesh->supports[qoff + i];
927 
928           ierr = DMPlexGetConeSize(dm,r,&numCones);CHKERRQ(ierr);
929           ierr = DMPlexGetCone(dm,r,&cone);CHKERRQ(ierr);
930           for (j = 0; j < numCones; j++) {
931             if (cone[j] == q) break;
932           }
933           if (j < numCones) newSupports[newOff+offsets[p]++] = r;
934         }
935         for (i = 0; i < dof; i++) {
936           PetscInt numCones, j;
937           const PetscInt *cone;
938           PetscInt r = mesh->supports[off + i];
939 
940           ierr = DMPlexGetConeSize(dm,r,&numCones);CHKERRQ(ierr);
941           ierr = DMPlexGetCone(dm,r,&cone);CHKERRQ(ierr);
942           for (j = 0; j < numCones; j++) {
943             if (cone[j] == p) break;
944           }
945           if (j < numCones) newSupports[newqOff+offsets[q]++] = r;
946         }
947         ierr = DMPlexGetTreeParent(dm,q,&parent,NULL);CHKERRQ(ierr);
948       }
949     }
950   }
951   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
952   mesh->supportSection = newSupportSection;
953   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
954   mesh->supports = newSupports;
955   ierr = PetscFree(offsets);CHKERRQ(ierr);
956   ierr = PetscFree(numTrueSupp);CHKERRQ(ierr);
957 
958   PetscFunctionReturn(0);
959 }
960 
961 static PetscErrorCode DMPlexComputeAnchorMatrix_Tree_Direct(DM,PetscSection,PetscSection,Mat);
962 static PetscErrorCode DMPlexComputeAnchorMatrix_Tree_FromReference(DM,PetscSection,PetscSection,Mat);
963 
964 #undef __FUNCT__
965 #define __FUNCT__ "DMPlexSetTree_Internal"
966 static PetscErrorCode DMPlexSetTree_Internal(DM dm, PetscSection parentSection, PetscInt *parents, PetscInt *childIDs, PetscBool computeCanonical, PetscBool exchangeSupports)
967 {
968   DM_Plex       *mesh = (DM_Plex *)dm->data;
969   DM             refTree;
970   PetscInt       size;
971   PetscErrorCode ierr;
972 
973   PetscFunctionBegin;
974   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
975   PetscValidHeaderSpecific(parentSection, PETSC_SECTION_CLASSID, 2);
976   ierr = PetscObjectReference((PetscObject)parentSection);CHKERRQ(ierr);
977   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
978   mesh->parentSection = parentSection;
979   ierr = PetscSectionGetStorageSize(parentSection,&size);CHKERRQ(ierr);
980   if (parents != mesh->parents) {
981     ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
982     ierr = PetscMalloc1(size,&mesh->parents);CHKERRQ(ierr);
983     ierr = PetscMemcpy(mesh->parents, parents, size * sizeof(*parents));CHKERRQ(ierr);
984   }
985   if (childIDs != mesh->childIDs) {
986     ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
987     ierr = PetscMalloc1(size,&mesh->childIDs);CHKERRQ(ierr);
988     ierr = PetscMemcpy(mesh->childIDs, childIDs, size * sizeof(*childIDs));CHKERRQ(ierr);
989   }
990   ierr = DMPlexGetReferenceTree(dm,&refTree);CHKERRQ(ierr);
991   if (refTree) {
992     DMLabel canonLabel;
993 
994     ierr = DMGetLabel(refTree,"canonical",&canonLabel);CHKERRQ(ierr);
995     if (canonLabel) {
996       PetscInt i;
997 
998       for (i = 0; i < size; i++) {
999         PetscInt canon;
1000         ierr = DMLabelGetValue(canonLabel, mesh->childIDs[i], &canon);CHKERRQ(ierr);
1001         if (canon >= 0) {
1002           mesh->childIDs[i] = canon;
1003         }
1004       }
1005     }
1006     mesh->computeanchormatrix = DMPlexComputeAnchorMatrix_Tree_FromReference;
1007   }
1008   else {
1009     mesh->computeanchormatrix = DMPlexComputeAnchorMatrix_Tree_Direct;
1010   }
1011   ierr = DMPlexTreeSymmetrize(dm);CHKERRQ(ierr);
1012   if (computeCanonical) {
1013     PetscInt d, dim;
1014 
1015     /* add the canonical label */
1016     ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
1017     ierr = DMCreateLabel(dm,"canonical");CHKERRQ(ierr);
1018     for (d = 0; d <= dim; d++) {
1019       PetscInt p, dStart, dEnd, canon = -1, cNumChildren;
1020       const PetscInt *cChildren;
1021 
1022       ierr = DMPlexGetDepthStratum(dm,d,&dStart,&dEnd);CHKERRQ(ierr);
1023       for (p = dStart; p < dEnd; p++) {
1024         ierr = DMPlexGetTreeChildren(dm,p,&cNumChildren,&cChildren);CHKERRQ(ierr);
1025         if (cNumChildren) {
1026           canon = p;
1027           break;
1028         }
1029       }
1030       if (canon == -1) continue;
1031       for (p = dStart; p < dEnd; p++) {
1032         PetscInt numChildren, i;
1033         const PetscInt *children;
1034 
1035         ierr = DMPlexGetTreeChildren(dm,p,&numChildren,&children);CHKERRQ(ierr);
1036         if (numChildren) {
1037           if (numChildren != cNumChildren) SETERRQ2(PetscObjectComm((PetscObject)dm),PETSC_ERR_PLIB,"All parent points in a stratum should have the same number of children: %d != %d", numChildren, cNumChildren);
1038           ierr = DMSetLabelValue(dm,"canonical",p,canon);CHKERRQ(ierr);
1039           for (i = 0; i < numChildren; i++) {
1040             ierr = DMSetLabelValue(dm,"canonical",children[i],cChildren[i]);CHKERRQ(ierr);
1041           }
1042         }
1043       }
1044     }
1045   }
1046   if (exchangeSupports) {
1047     ierr = DMPlexTreeExchangeSupports(dm);CHKERRQ(ierr);
1048   }
1049   mesh->createanchors = DMPlexCreateAnchors_Tree;
1050   /* reset anchors */
1051   ierr = DMPlexSetAnchors(dm,NULL,NULL);CHKERRQ(ierr);
1052   PetscFunctionReturn(0);
1053 }
1054 
1055 #undef __FUNCT__
1056 #define __FUNCT__ "DMPlexSetTree"
1057 /*@
1058   DMPlexSetTree - set the tree that describes the hierarchy of non-conforming mesh points.  This routine also creates
1059   the point-to-point constraints determined by the tree: a point is constained to the points in the closure of its
1060   tree root.
1061 
1062   Collective on dm
1063 
1064   Input Parameters:
1065 + dm - the DMPlex object
1066 . parentSection - a section describing the tree: a point has a parent if it has 1 dof in the section; the section
1067                   offset indexes the parent and childID list; the reference count of parentSection is incremented
1068 . parents - a list of the point parents; copied, can be destroyed
1069 - childIDs - identifies the relationship of the child point to the parent point; if there is a reference tree, then
1070              the child corresponds to the point in the reference tree with index childIDs; copied, can be destroyed
1071 
1072   Level: intermediate
1073 
1074 .seealso: DMPlexGetTree(), DMPlexSetReferenceTree(), DMPlexSetAnchors(), DMPlexGetTreeParent(), DMPlexGetTreeChildren()
1075 @*/
1076 PetscErrorCode DMPlexSetTree(DM dm, PetscSection parentSection, PetscInt parents[], PetscInt childIDs[])
1077 {
1078   PetscErrorCode ierr;
1079 
1080   PetscFunctionBegin;
1081   ierr = DMPlexSetTree_Internal(dm,parentSection,parents,childIDs,PETSC_FALSE,PETSC_TRUE);CHKERRQ(ierr);
1082   PetscFunctionReturn(0);
1083 }
1084 
1085 #undef __FUNCT__
1086 #define __FUNCT__ "DMPlexGetTree"
1087 /*@
1088   DMPlexGetTree - get the tree that describes the hierarchy of non-conforming mesh points.
1089   Collective on dm
1090 
1091   Input Parameters:
1092 . dm - the DMPlex object
1093 
1094   Output Parameters:
1095 + parentSection - a section describing the tree: a point has a parent if it has 1 dof in the section; the section
1096                   offset indexes the parent and childID list
1097 . parents - a list of the point parents
1098 . childIDs - identifies the relationship of the child point to the parent point; if there is a reference tree, then
1099              the child corresponds to the point in the reference tree with index childID
1100 . childSection - the inverse of the parent section
1101 - children - a list of the point children
1102 
1103   Level: intermediate
1104 
1105 .seealso: DMPlexSetTree(), DMPlexSetReferenceTree(), DMPlexSetAnchors(), DMPlexGetTreeParent(), DMPlexGetTreeChildren()
1106 @*/
1107 PetscErrorCode DMPlexGetTree(DM dm, PetscSection *parentSection, PetscInt *parents[], PetscInt *childIDs[], PetscSection *childSection, PetscInt *children[])
1108 {
1109   DM_Plex        *mesh = (DM_Plex *)dm->data;
1110 
1111   PetscFunctionBegin;
1112   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1113   if (parentSection) *parentSection = mesh->parentSection;
1114   if (parents)       *parents       = mesh->parents;
1115   if (childIDs)      *childIDs      = mesh->childIDs;
1116   if (childSection)  *childSection  = mesh->childSection;
1117   if (children)      *children      = mesh->children;
1118   PetscFunctionReturn(0);
1119 }
1120 
1121 #undef __FUNCT__
1122 #define __FUNCT__ "DMPlexGetTreeParent"
1123 /*@
1124   DMPlexGetTreeParent - get the parent of a point in the tree describing the point hierarchy (not the Sieve DAG)
1125 
1126   Input Parameters:
1127 + dm - the DMPlex object
1128 - point - the query point
1129 
1130   Output Parameters:
1131 + parent - if not NULL, set to the parent of the point, or the point itself if the point does not have a parent
1132 - childID - if not NULL, set to the child ID of the point with respect to its parent, or 0 if the point
1133             does not have a parent
1134 
1135   Level: intermediate
1136 
1137 .seealso: DMPlexSetTree(), DMPlexGetTree(), DMPlexGetTreeChildren()
1138 @*/
1139 PetscErrorCode DMPlexGetTreeParent(DM dm, PetscInt point, PetscInt *parent, PetscInt *childID)
1140 {
1141   DM_Plex       *mesh = (DM_Plex *)dm->data;
1142   PetscSection   pSec;
1143   PetscErrorCode ierr;
1144 
1145   PetscFunctionBegin;
1146   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1147   pSec = mesh->parentSection;
1148   if (pSec && point >= pSec->pStart && point < pSec->pEnd) {
1149     PetscInt dof;
1150 
1151     ierr = PetscSectionGetDof (pSec, point, &dof);CHKERRQ(ierr);
1152     if (dof) {
1153       PetscInt off;
1154 
1155       ierr = PetscSectionGetOffset (pSec, point, &off);CHKERRQ(ierr);
1156       if (parent)  *parent = mesh->parents[off];
1157       if (childID) *childID = mesh->childIDs[off];
1158       PetscFunctionReturn(0);
1159     }
1160   }
1161   if (parent) {
1162     *parent = point;
1163   }
1164   if (childID) {
1165     *childID = 0;
1166   }
1167   PetscFunctionReturn(0);
1168 }
1169 
1170 #undef __FUNCT__
1171 #define __FUNCT__ "DMPlexGetTreeChildren"
1172 /*@C
1173   DMPlexGetTreeChildren - get the children of a point in the tree describing the point hierarchy (not the Sieve DAG)
1174 
1175   Input Parameters:
1176 + dm - the DMPlex object
1177 - point - the query point
1178 
1179   Output Parameters:
1180 + numChildren - if not NULL, set to the number of children
1181 - children - if not NULL, set to a list children, or set to NULL if the point has no children
1182 
1183   Level: intermediate
1184 
1185   Fortran Notes:
1186   Since it returns an array, this routine is only available in Fortran 90, and you must
1187   include petsc.h90 in your code.
1188 
1189 .seealso: DMPlexSetTree(), DMPlexGetTree(), DMPlexGetTreeParent()
1190 @*/
1191 PetscErrorCode DMPlexGetTreeChildren(DM dm, PetscInt point, PetscInt *numChildren, const PetscInt *children[])
1192 {
1193   DM_Plex       *mesh = (DM_Plex *)dm->data;
1194   PetscSection   childSec;
1195   PetscInt       dof = 0;
1196   PetscErrorCode ierr;
1197 
1198   PetscFunctionBegin;
1199   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1200   childSec = mesh->childSection;
1201   if (childSec && point >= childSec->pStart && point < childSec->pEnd) {
1202     ierr = PetscSectionGetDof (childSec, point, &dof);CHKERRQ(ierr);
1203   }
1204   if (numChildren) *numChildren = dof;
1205   if (children) {
1206     if (dof) {
1207       PetscInt off;
1208 
1209       ierr = PetscSectionGetOffset (childSec, point, &off);CHKERRQ(ierr);
1210       *children = &mesh->children[off];
1211     }
1212     else {
1213       *children = NULL;
1214     }
1215   }
1216   PetscFunctionReturn(0);
1217 }
1218 
1219 #undef __FUNCT__
1220 #define __FUNCT__ "DMPlexComputeAnchorMatrix_Tree_Direct"
1221 static PetscErrorCode DMPlexComputeAnchorMatrix_Tree_Direct(DM dm, PetscSection section, PetscSection cSec, Mat cMat)
1222 {
1223   PetscDS        ds;
1224   PetscInt       spdim;
1225   PetscInt       numFields, f, c, cStart, cEnd, pStart, pEnd, conStart, conEnd;
1226   const PetscInt *anchors;
1227   PetscSection   aSec;
1228   PetscReal      *v0, *v0parent, *vtmp, *J, *Jparent, *invJparent, detJ, detJparent;
1229   IS             aIS;
1230   PetscErrorCode ierr;
1231 
1232   PetscFunctionBegin;
1233   ierr = DMPlexGetChart(dm,&pStart,&pEnd);CHKERRQ(ierr);
1234   ierr = DMGetDS(dm,&ds);CHKERRQ(ierr);
1235   ierr = PetscDSGetNumFields(ds,&numFields);CHKERRQ(ierr);
1236   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
1237   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
1238   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
1239   ierr = PetscSectionGetChart(cSec,&conStart,&conEnd);CHKERRQ(ierr);
1240   ierr = DMGetDimension(dm,&spdim);CHKERRQ(ierr);
1241   ierr = PetscMalloc6(spdim,&v0,spdim,&v0parent,spdim,&vtmp,spdim*spdim,&J,spdim*spdim,&Jparent,spdim*spdim,&invJparent);CHKERRQ(ierr);
1242 
1243   for (f = 0; f < numFields; f++) {
1244     PetscObject disc;
1245     PetscFE fe = NULL;
1246     PetscFV fv = NULL;
1247     PetscClassId id;
1248     PetscDualSpace space;
1249     PetscInt i, j, k, nPoints, offset;
1250     PetscInt fSize, fComp;
1251     PetscReal *B = NULL;
1252     PetscReal *weights, *pointsRef, *pointsReal;
1253     Mat Amat, Bmat, Xmat;
1254 
1255     ierr = PetscDSGetDiscretization(ds,f,&disc);CHKERRQ(ierr);
1256     ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
1257     if (id == PETSCFE_CLASSID) {
1258       fe = (PetscFE) disc;
1259       ierr = PetscFEGetDualSpace(fe,&space);CHKERRQ(ierr);
1260       ierr = PetscDualSpaceGetDimension(space,&fSize);CHKERRQ(ierr);
1261       ierr = PetscFEGetNumComponents(fe,&fComp);CHKERRQ(ierr);
1262     }
1263     else if (id == PETSCFV_CLASSID) {
1264       fv = (PetscFV) disc;
1265       ierr = PetscFVGetDualSpace(fv,&space);CHKERRQ(ierr);
1266       ierr = PetscDualSpaceGetDimension(space,&fSize);CHKERRQ(ierr);
1267       ierr = PetscFVGetNumComponents(fv,&fComp);CHKERRQ(ierr);
1268     }
1269     else SETERRQ1(PetscObjectComm(disc),PETSC_ERR_ARG_UNKNOWN_TYPE, "PetscDS discretization id %d not recognized.", id);
1270 
1271     ierr = MatCreate(PETSC_COMM_SELF,&Amat);CHKERRQ(ierr);
1272     ierr = MatSetSizes(Amat,fSize,fSize,fSize,fSize);CHKERRQ(ierr);
1273     ierr = MatSetType(Amat,MATSEQDENSE);CHKERRQ(ierr);
1274     ierr = MatSetUp(Amat);CHKERRQ(ierr);
1275     ierr = MatDuplicate(Amat,MAT_DO_NOT_COPY_VALUES,&Bmat);CHKERRQ(ierr);
1276     ierr = MatDuplicate(Amat,MAT_DO_NOT_COPY_VALUES,&Xmat);CHKERRQ(ierr);
1277     nPoints = 0;
1278     for (i = 0; i < fSize; i++) {
1279       PetscInt        qPoints;
1280       PetscQuadrature quad;
1281 
1282       ierr = PetscDualSpaceGetFunctional(space,i,&quad);CHKERRQ(ierr);
1283       ierr = PetscQuadratureGetData(quad,NULL,&qPoints,NULL,NULL);CHKERRQ(ierr);
1284       nPoints += qPoints;
1285     }
1286     ierr = PetscMalloc3(nPoints,&weights,spdim*nPoints,&pointsRef,spdim*nPoints,&pointsReal);CHKERRQ(ierr);
1287     offset = 0;
1288     for (i = 0; i < fSize; i++) {
1289       PetscInt        qPoints;
1290       const PetscReal    *p, *w;
1291       PetscQuadrature quad;
1292 
1293       ierr = PetscDualSpaceGetFunctional(space,i,&quad);CHKERRQ(ierr);
1294       ierr = PetscQuadratureGetData(quad,NULL,&qPoints,&p,&w);CHKERRQ(ierr);
1295       ierr = PetscMemcpy(weights+offset,w,qPoints*sizeof(*w));CHKERRQ(ierr);
1296       ierr = PetscMemcpy(pointsRef+spdim*offset,p,spdim*qPoints*sizeof(*p));CHKERRQ(ierr);
1297       offset += qPoints;
1298     }
1299     if (id == PETSCFE_CLASSID) {
1300       ierr = PetscFEGetTabulation(fe,nPoints,pointsRef,&B,NULL,NULL);CHKERRQ(ierr);
1301     }
1302     else {
1303       ierr = PetscFVGetTabulation(fv,nPoints,pointsRef,&B,NULL,NULL);CHKERRQ(ierr);
1304     }
1305     offset = 0;
1306     for (i = 0; i < fSize; i++) {
1307       PetscInt        qPoints;
1308       PetscQuadrature quad;
1309 
1310       ierr = PetscDualSpaceGetFunctional(space,i,&quad);CHKERRQ(ierr);
1311       ierr = PetscQuadratureGetData(quad,NULL,&qPoints,NULL,NULL);CHKERRQ(ierr);
1312       for (j = 0; j < fSize; j++) {
1313         PetscScalar val = 0.;
1314 
1315         for (k = 0; k < qPoints; k++) {
1316           val += B[((offset + k) * fSize + j) * fComp] * weights[k];
1317         }
1318         ierr = MatSetValue(Amat,i,j,val,INSERT_VALUES);CHKERRQ(ierr);
1319       }
1320       offset += qPoints;
1321     }
1322     if (id == PETSCFE_CLASSID) {
1323       ierr = PetscFERestoreTabulation(fe,nPoints,pointsRef,&B,NULL,NULL);CHKERRQ(ierr);
1324     }
1325     else {
1326       ierr = PetscFVRestoreTabulation(fv,nPoints,pointsRef,&B,NULL,NULL);CHKERRQ(ierr);
1327     }
1328     ierr = MatAssemblyBegin(Amat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1329     ierr = MatAssemblyEnd(Amat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1330     ierr = MatLUFactor(Amat,NULL,NULL,NULL);CHKERRQ(ierr);
1331     for (c = cStart; c < cEnd; c++) {
1332       PetscInt parent;
1333       PetscInt closureSize, closureSizeP, *closure = NULL, *closureP = NULL;
1334       PetscInt *childOffsets, *parentOffsets;
1335 
1336       ierr = DMPlexGetTreeParent(dm,c,&parent,NULL);CHKERRQ(ierr);
1337       if (parent == c) continue;
1338       ierr = DMPlexGetTransitiveClosure(dm,c,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
1339       for (i = 0; i < closureSize; i++) {
1340         PetscInt p = closure[2*i];
1341         PetscInt conDof;
1342 
1343         if (p < conStart || p >= conEnd) continue;
1344         if (numFields > 1) {
1345           ierr = PetscSectionGetFieldDof(cSec,p,f,&conDof);CHKERRQ(ierr);
1346         }
1347         else {
1348           ierr = PetscSectionGetDof(cSec,p,&conDof);CHKERRQ(ierr);
1349         }
1350         if (conDof) break;
1351       }
1352       if (i == closureSize) {
1353         ierr = DMPlexRestoreTransitiveClosure(dm,c,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
1354         continue;
1355       }
1356 
1357       ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, NULL, &detJ);CHKERRQ(ierr);
1358       ierr = DMPlexComputeCellGeometryFEM(dm, parent, NULL, v0parent, Jparent, invJparent, &detJparent);CHKERRQ(ierr);
1359       for (i = 0; i < nPoints; i++) {
1360         CoordinatesRefToReal(spdim, spdim, v0, J, &pointsRef[i*spdim],vtmp);CHKERRQ(ierr);
1361         CoordinatesRealToRef(spdim, spdim, v0parent, invJparent, vtmp, &pointsReal[i*spdim]);CHKERRQ(ierr);
1362       }
1363       if (id == PETSCFE_CLASSID) {
1364         ierr = PetscFEGetTabulation(fe,nPoints,pointsReal,&B,NULL,NULL);CHKERRQ(ierr);
1365       }
1366       else {
1367         ierr = PetscFVGetTabulation(fv,nPoints,pointsReal,&B,NULL,NULL);CHKERRQ(ierr);
1368       }
1369       offset = 0;
1370       for (i = 0; i < fSize; i++) {
1371         PetscInt        qPoints;
1372         PetscQuadrature quad;
1373 
1374         ierr = PetscDualSpaceGetFunctional(space,i,&quad);CHKERRQ(ierr);
1375         ierr = PetscQuadratureGetData(quad,NULL,&qPoints,NULL,NULL);CHKERRQ(ierr);
1376         for (j = 0; j < fSize; j++) {
1377           PetscScalar val = 0.;
1378 
1379           for (k = 0; k < qPoints; k++) {
1380             val += B[((offset + k) * fSize + j) * fComp] * weights[k];
1381           }
1382           MatSetValue(Bmat,i,j,val,INSERT_VALUES);CHKERRQ(ierr);
1383         }
1384         offset += qPoints;
1385       }
1386       if (id == PETSCFE_CLASSID) {
1387         ierr = PetscFERestoreTabulation(fe,nPoints,pointsReal,&B,NULL,NULL);CHKERRQ(ierr);
1388       }
1389       else {
1390         ierr = PetscFVRestoreTabulation(fv,nPoints,pointsReal,&B,NULL,NULL);CHKERRQ(ierr);
1391       }
1392       ierr = MatAssemblyBegin(Bmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1393       ierr = MatAssemblyEnd(Bmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1394       ierr = MatMatSolve(Amat,Bmat,Xmat);CHKERRQ(ierr);
1395       ierr = DMPlexGetTransitiveClosure(dm,parent,PETSC_TRUE,&closureSizeP,&closureP);CHKERRQ(ierr);
1396       ierr = PetscMalloc2(closureSize+1,&childOffsets,closureSizeP+1,&parentOffsets);CHKERRQ(ierr);
1397       childOffsets[0] = 0;
1398       for (i = 0; i < closureSize; i++) {
1399         PetscInt p = closure[2*i];
1400         PetscInt dof;
1401 
1402         if (numFields > 1) {
1403           ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
1404         }
1405         else {
1406           ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
1407         }
1408         childOffsets[i+1]=childOffsets[i]+dof / fComp;
1409       }
1410       parentOffsets[0] = 0;
1411       for (i = 0; i < closureSizeP; i++) {
1412         PetscInt p = closureP[2*i];
1413         PetscInt dof;
1414 
1415         if (numFields > 1) {
1416           ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
1417         }
1418         else {
1419           ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
1420         }
1421         parentOffsets[i+1]=parentOffsets[i]+dof / fComp;
1422       }
1423       for (i = 0; i < closureSize; i++) {
1424         PetscInt conDof, conOff, aDof, aOff;
1425         PetscInt p = closure[2*i];
1426         PetscInt o = closure[2*i+1];
1427 
1428         if (p < conStart || p >= conEnd) continue;
1429         if (numFields > 1) {
1430           ierr = PetscSectionGetFieldDof(cSec,p,f,&conDof);CHKERRQ(ierr);
1431           ierr = PetscSectionGetFieldOffset(cSec,p,f,&conOff);CHKERRQ(ierr);
1432         }
1433         else {
1434           ierr = PetscSectionGetDof(cSec,p,&conDof);CHKERRQ(ierr);
1435           ierr = PetscSectionGetOffset(cSec,p,&conOff);CHKERRQ(ierr);
1436         }
1437         if (!conDof) continue;
1438         ierr = PetscSectionGetDof(aSec,p,&aDof);CHKERRQ(ierr);
1439         ierr = PetscSectionGetOffset(aSec,p,&aOff);CHKERRQ(ierr);
1440         for (k = 0; k < aDof; k++) {
1441           PetscInt a = anchors[aOff + k];
1442           PetscInt aSecDof, aSecOff;
1443 
1444           if (numFields > 1) {
1445             ierr = PetscSectionGetFieldDof(section,a,f,&aSecDof);CHKERRQ(ierr);
1446             ierr = PetscSectionGetFieldOffset(section,a,f,&aSecOff);CHKERRQ(ierr);
1447           }
1448           else {
1449             ierr = PetscSectionGetDof(section,a,&aSecDof);CHKERRQ(ierr);
1450             ierr = PetscSectionGetOffset(section,a,&aSecOff);CHKERRQ(ierr);
1451           }
1452           if (!aSecDof) continue;
1453 
1454           for (j = 0; j < closureSizeP; j++) {
1455             PetscInt q = closureP[2*j];
1456             PetscInt oq = closureP[2*j+1];
1457 
1458             if (q == a) {
1459               PetscInt r, s, t;
1460 
1461               for (r = childOffsets[i]; r < childOffsets[i+1]; r++) {
1462                 for (s = parentOffsets[j]; s < parentOffsets[j+1]; s++) {
1463                   PetscScalar val;
1464                   PetscInt insertCol, insertRow;
1465 
1466                   ierr = MatGetValue(Xmat,r,s,&val);CHKERRQ(ierr);
1467                   if (o >= 0) {
1468                     insertRow = conOff + fComp * (r - childOffsets[i]);
1469                   }
1470                   else {
1471                     insertRow = conOff + fComp * (childOffsets[i + 1] - 1 - r);
1472                   }
1473                   if (oq >= 0) {
1474                     insertCol = aSecOff + fComp * (s - parentOffsets[j]);
1475                   }
1476                   else {
1477                     insertCol = aSecOff + fComp * (parentOffsets[j + 1] - 1 - s);
1478                   }
1479                   for (t = 0; t < fComp; t++) {
1480                     ierr = MatSetValue(cMat,insertRow + t,insertCol + t,val,INSERT_VALUES);CHKERRQ(ierr);
1481                   }
1482                 }
1483               }
1484             }
1485           }
1486         }
1487       }
1488       ierr = PetscFree2(childOffsets,parentOffsets);CHKERRQ(ierr);
1489       ierr = DMPlexRestoreTransitiveClosure(dm,c,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
1490       ierr = DMPlexRestoreTransitiveClosure(dm,parent,PETSC_TRUE,&closureSizeP,&closureP);CHKERRQ(ierr);
1491     }
1492     ierr = MatDestroy(&Amat);CHKERRQ(ierr);
1493     ierr = MatDestroy(&Bmat);CHKERRQ(ierr);
1494     ierr = MatDestroy(&Xmat);CHKERRQ(ierr);
1495     ierr = PetscFree3(weights,pointsRef,pointsReal);CHKERRQ(ierr);
1496   }
1497   ierr = MatAssemblyBegin(cMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1498   ierr = MatAssemblyEnd(cMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1499   ierr = PetscFree6(v0,v0parent,vtmp,J,Jparent,invJparent);CHKERRQ(ierr);
1500   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
1501 
1502   PetscFunctionReturn(0);
1503 }
1504 
1505 #undef __FUNCT__
1506 #define __FUNCT__ "DMPlexReferenceTreeGetChildrenMatrices"
1507 static PetscErrorCode DMPlexReferenceTreeGetChildrenMatrices(DM refTree, PetscScalar ****childrenMats, PetscInt ***childrenN)
1508 {
1509   Mat            refCmat;
1510   PetscDS        ds;
1511   PetscInt       numFields, f, pRefStart, pRefEnd, p, *rows, *cols, maxDof, maxAnDof, **refPointFieldN;
1512   PetscScalar    ***refPointFieldMats;
1513   PetscSection   refConSec, refAnSec, refSection;
1514   IS             refAnIS;
1515   const PetscInt *refAnchors;
1516   PetscErrorCode ierr;
1517 
1518   PetscFunctionBegin;
1519   ierr = DMGetDS(refTree,&ds);CHKERRQ(ierr);
1520   ierr = PetscDSGetNumFields(ds,&numFields);CHKERRQ(ierr);
1521   ierr = DMGetDefaultConstraints(refTree,&refConSec,&refCmat);CHKERRQ(ierr);
1522   ierr = DMPlexGetAnchors(refTree,&refAnSec,&refAnIS);CHKERRQ(ierr);
1523   ierr = ISGetIndices(refAnIS,&refAnchors);CHKERRQ(ierr);
1524   ierr = DMGetDefaultSection(refTree,&refSection);CHKERRQ(ierr);
1525   ierr = PetscSectionGetChart(refConSec,&pRefStart,&pRefEnd);CHKERRQ(ierr);
1526   ierr = PetscMalloc1(pRefEnd-pRefStart,&refPointFieldMats);CHKERRQ(ierr);
1527   ierr = PetscMalloc1(pRefEnd-pRefStart,&refPointFieldN);CHKERRQ(ierr);
1528   ierr = PetscSectionGetMaxDof(refConSec,&maxDof);CHKERRQ(ierr);
1529   ierr = PetscSectionGetMaxDof(refAnSec,&maxAnDof);CHKERRQ(ierr);
1530   ierr = PetscMalloc1(maxDof,&rows);CHKERRQ(ierr);
1531   ierr = PetscMalloc1(maxDof*maxAnDof,&cols);CHKERRQ(ierr);
1532   for (p = pRefStart; p < pRefEnd; p++) {
1533     PetscInt parent, closureSize, *closure = NULL, pDof;
1534 
1535     ierr = DMPlexGetTreeParent(refTree,p,&parent,NULL);CHKERRQ(ierr);
1536     ierr = PetscSectionGetDof(refConSec,p,&pDof);CHKERRQ(ierr);
1537     if (!pDof || parent == p) continue;
1538 
1539     ierr = PetscMalloc1(numFields,&refPointFieldMats[p-pRefStart]);CHKERRQ(ierr);
1540     ierr = PetscCalloc1(numFields,&refPointFieldN[p-pRefStart]);CHKERRQ(ierr);
1541     ierr = DMPlexGetTransitiveClosure(refTree,parent,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
1542     for (f = 0; f < numFields; f++) {
1543       PetscInt cDof, cOff, numCols, r, i, fComp;
1544       PetscObject disc;
1545       PetscClassId id;
1546       PetscFE fe = NULL;
1547       PetscFV fv = NULL;
1548 
1549       ierr = PetscDSGetDiscretization(ds,f,&disc);CHKERRQ(ierr);
1550       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
1551       if (id == PETSCFE_CLASSID) {
1552         fe = (PetscFE) disc;
1553         ierr = PetscFEGetNumComponents(fe,&fComp);CHKERRQ(ierr);
1554       }
1555       else if (id == PETSCFV_CLASSID) {
1556         fv = (PetscFV) disc;
1557         ierr = PetscFVGetNumComponents(fv,&fComp);CHKERRQ(ierr);
1558       }
1559       else SETERRQ1(PetscObjectComm(disc),PETSC_ERR_ARG_UNKNOWN_TYPE, "PetscDS discretization id %d not recognized.", id);
1560 
1561       if (numFields > 1) {
1562         ierr = PetscSectionGetFieldDof(refConSec,p,f,&cDof);CHKERRQ(ierr);
1563         ierr = PetscSectionGetFieldOffset(refConSec,p,f,&cOff);CHKERRQ(ierr);
1564       }
1565       else {
1566         ierr = PetscSectionGetDof(refConSec,p,&cDof);CHKERRQ(ierr);
1567         ierr = PetscSectionGetOffset(refConSec,p,&cOff);CHKERRQ(ierr);
1568       }
1569 
1570       if (!cDof) continue;
1571       for (r = 0; r < cDof; r++) {
1572         rows[r] = cOff + r;
1573       }
1574       numCols = 0;
1575       for (i = 0; i < closureSize; i++) {
1576         PetscInt q = closure[2*i];
1577         PetscInt o = closure[2*i+1];
1578         PetscInt aDof, aOff, j;
1579 
1580         if (numFields > 1) {
1581           ierr = PetscSectionGetFieldDof(refSection,q,f,&aDof);CHKERRQ(ierr);
1582           ierr = PetscSectionGetFieldOffset(refSection,q,f,&aOff);CHKERRQ(ierr);
1583         }
1584         else {
1585           ierr = PetscSectionGetDof(refSection,q,&aDof);CHKERRQ(ierr);
1586           ierr = PetscSectionGetOffset(refSection,q,&aOff);CHKERRQ(ierr);
1587         }
1588 
1589         for (j = 0; j < aDof; j++) {
1590           PetscInt node = (o >= 0) ? (j / fComp) : ((aDof - 1 - j) / fComp);
1591           PetscInt comp = (j % fComp);
1592 
1593           cols[numCols++] = aOff + node * fComp + comp;
1594         }
1595       }
1596       refPointFieldN[p-pRefStart][f] = numCols;
1597       ierr = PetscMalloc1(cDof*numCols,&refPointFieldMats[p-pRefStart][f]);CHKERRQ(ierr);
1598       ierr = MatGetValues(refCmat,cDof,rows,numCols,cols,refPointFieldMats[p-pRefStart][f]);CHKERRQ(ierr);
1599     }
1600     ierr = DMPlexRestoreTransitiveClosure(refTree,parent,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
1601   }
1602   *childrenMats = refPointFieldMats;
1603   *childrenN = refPointFieldN;
1604   ierr = ISRestoreIndices(refAnIS,&refAnchors);CHKERRQ(ierr);
1605   ierr = PetscFree(rows);CHKERRQ(ierr);
1606   ierr = PetscFree(cols);CHKERRQ(ierr);
1607   PetscFunctionReturn(0);
1608 }
1609 
1610 #undef __FUNCT__
1611 #define __FUNCT__ "DMPlexReferenceTreeRestoreChildrenMatrices"
1612 static PetscErrorCode DMPlexReferenceTreeRestoreChildrenMatrices(DM refTree, PetscScalar ****childrenMats, PetscInt ***childrenN)
1613 {
1614   PetscDS        ds;
1615   PetscInt       **refPointFieldN;
1616   PetscScalar    ***refPointFieldMats;
1617   PetscInt       numFields, pRefStart, pRefEnd, p, f;
1618   PetscSection   refConSec;
1619   PetscErrorCode ierr;
1620 
1621   PetscFunctionBegin;
1622   refPointFieldN = *childrenN;
1623   *childrenN = NULL;
1624   refPointFieldMats = *childrenMats;
1625   *childrenMats = NULL;
1626   ierr = DMGetDS(refTree,&ds);CHKERRQ(ierr);
1627   ierr = PetscDSGetNumFields(ds,&numFields);CHKERRQ(ierr);
1628   ierr = DMGetDefaultConstraints(refTree,&refConSec,NULL);CHKERRQ(ierr);
1629   ierr = PetscSectionGetChart(refConSec,&pRefStart,&pRefEnd);CHKERRQ(ierr);
1630   for (p = pRefStart; p < pRefEnd; p++) {
1631     PetscInt parent, pDof;
1632 
1633     ierr = DMPlexGetTreeParent(refTree,p,&parent,NULL);CHKERRQ(ierr);
1634     ierr = PetscSectionGetDof(refConSec,p,&pDof);CHKERRQ(ierr);
1635     if (!pDof || parent == p) continue;
1636 
1637     for (f = 0; f < numFields; f++) {
1638       PetscInt cDof;
1639 
1640       if (numFields > 1) {
1641         ierr = PetscSectionGetFieldDof(refConSec,p,f,&cDof);CHKERRQ(ierr);
1642       }
1643       else {
1644         ierr = PetscSectionGetDof(refConSec,p,&cDof);CHKERRQ(ierr);
1645       }
1646 
1647       if (!cDof) continue;
1648       ierr = PetscFree(refPointFieldMats[p - pRefStart][f]);CHKERRQ(ierr);
1649     }
1650     ierr = PetscFree(refPointFieldMats[p - pRefStart]);CHKERRQ(ierr);
1651     ierr = PetscFree(refPointFieldN[p - pRefStart]);CHKERRQ(ierr);
1652   }
1653   ierr = PetscFree(refPointFieldMats);CHKERRQ(ierr);
1654   ierr = PetscFree(refPointFieldN);CHKERRQ(ierr);
1655   PetscFunctionReturn(0);
1656 }
1657 
1658 #undef __FUNCT__
1659 #define __FUNCT__ "DMPlexComputeAnchorMatrix_Tree_FromReference"
1660 static PetscErrorCode DMPlexComputeAnchorMatrix_Tree_FromReference(DM dm, PetscSection section, PetscSection conSec, Mat cMat)
1661 {
1662   DM             refTree;
1663   PetscDS        ds;
1664   Mat            refCmat;
1665   PetscInt       numFields, f, pRefStart, pRefEnd, p, maxDof, maxAnDof, *perm, *iperm, pStart, pEnd, conStart, conEnd, **refPointFieldN;
1666   PetscScalar ***refPointFieldMats, *pointWork;
1667   PetscSection   refConSec, refAnSec, anSec;
1668   IS             refAnIS, anIS;
1669   const PetscInt *anchors;
1670   PetscErrorCode ierr;
1671 
1672   PetscFunctionBegin;
1673   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1674   ierr = DMGetDS(dm,&ds);CHKERRQ(ierr);
1675   ierr = PetscDSGetNumFields(ds,&numFields);CHKERRQ(ierr);
1676   ierr = DMPlexGetReferenceTree(dm,&refTree);CHKERRQ(ierr);
1677   ierr = DMSetDS(refTree,ds);CHKERRQ(ierr);
1678   ierr = DMGetDefaultConstraints(refTree,&refConSec,&refCmat);CHKERRQ(ierr);
1679   ierr = DMPlexGetAnchors(refTree,&refAnSec,&refAnIS);CHKERRQ(ierr);
1680   ierr = DMPlexGetAnchors(dm,&anSec,&anIS);CHKERRQ(ierr);
1681   ierr = ISGetIndices(anIS,&anchors);CHKERRQ(ierr);
1682   ierr = PetscSectionGetChart(refConSec,&pRefStart,&pRefEnd);CHKERRQ(ierr);
1683   ierr = PetscSectionGetChart(conSec,&conStart,&conEnd);CHKERRQ(ierr);
1684   ierr = PetscSectionGetMaxDof(refConSec,&maxDof);CHKERRQ(ierr);
1685   ierr = PetscSectionGetMaxDof(refAnSec,&maxAnDof);CHKERRQ(ierr);
1686   ierr = PetscMalloc1(maxDof*maxDof*maxAnDof,&pointWork);CHKERRQ(ierr);
1687 
1688   /* step 1: get submats for every constrained point in the reference tree */
1689   ierr = DMPlexReferenceTreeGetChildrenMatrices(refTree,&refPointFieldMats,&refPointFieldN);CHKERRQ(ierr);
1690 
1691   /* step 2: compute the preorder */
1692   ierr = DMPlexGetChart(dm,&pStart,&pEnd);CHKERRQ(ierr);
1693   ierr = PetscMalloc2(pEnd-pStart,&perm,pEnd-pStart,&iperm);CHKERRQ(ierr);
1694   for (p = pStart; p < pEnd; p++) {
1695     perm[p - pStart] = p;
1696     iperm[p - pStart] = p-pStart;
1697   }
1698   for (p = 0; p < pEnd - pStart;) {
1699     PetscInt point = perm[p];
1700     PetscInt parent;
1701 
1702     ierr = DMPlexGetTreeParent(dm,point,&parent,NULL);CHKERRQ(ierr);
1703     if (parent == point) {
1704       p++;
1705     }
1706     else {
1707       PetscInt size, closureSize, *closure = NULL, i;
1708 
1709       ierr = DMPlexGetTransitiveClosure(dm,parent,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
1710       for (i = 0; i < closureSize; i++) {
1711         PetscInt q = closure[2*i];
1712         if (iperm[q-pStart] > iperm[point-pStart]) {
1713           /* swap */
1714           perm[p]               = q;
1715           perm[iperm[q-pStart]] = point;
1716           iperm[point-pStart]   = iperm[q-pStart];
1717           iperm[q-pStart]       = p;
1718           break;
1719         }
1720       }
1721       size = closureSize;
1722       ierr = DMPlexRestoreTransitiveClosure(dm,parent,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
1723       if (i == size) {
1724         p++;
1725       }
1726     }
1727   }
1728 
1729   /* step 3: fill the constraint matrix */
1730   /* we are going to use a preorder progressive fill strategy.  Mat doesn't
1731    * allow progressive fill without assembly, so we are going to set up the
1732    * values outside of the Mat first.
1733    */
1734   {
1735     PetscInt nRows, row, nnz;
1736     PetscBool done;
1737     const PetscInt *ia, *ja;
1738     PetscScalar *vals;
1739 
1740     ierr = MatGetRowIJ(cMat,0,PETSC_FALSE,PETSC_FALSE,&nRows,&ia,&ja,&done);CHKERRQ(ierr);
1741     if (!done) SETERRQ(PetscObjectComm((PetscObject)cMat),PETSC_ERR_PLIB,"Could not get RowIJ of constraint matrix");
1742     nnz  = ia[nRows];
1743     /* malloc and then zero rows right before we fill them: this way valgrind
1744      * can tell if we are doing progressive fill in the wrong order */
1745     ierr = PetscMalloc1(nnz,&vals);CHKERRQ(ierr);
1746     for (p = 0; p < pEnd - pStart; p++) {
1747       PetscInt parent, childid, closureSize, *closure = NULL;
1748       PetscInt point = perm[p], pointDof;
1749 
1750       ierr = DMPlexGetTreeParent(dm,point,&parent,&childid);CHKERRQ(ierr);
1751       if ((point < conStart) || (point >= conEnd) || (parent == point)) continue;
1752       ierr = PetscSectionGetDof(conSec,point,&pointDof);CHKERRQ(ierr);
1753       if (!pointDof) continue;
1754       ierr = DMPlexGetTransitiveClosure(dm,parent,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
1755       for (f = 0; f < numFields; f++) {
1756         PetscInt cDof, cOff, numCols, numFillCols, i, r, fComp = -1, matOffset, offset;
1757         PetscScalar *pointMat;
1758         PetscObject disc;
1759         PetscClassId id;
1760         PetscFE fe = NULL;
1761         PetscFV fv = NULL;
1762 
1763         ierr = PetscDSGetDiscretization(ds,f,&disc);CHKERRQ(ierr);
1764         ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
1765         if (id == PETSCFE_CLASSID) {
1766           fe = (PetscFE) disc;
1767           ierr = PetscFEGetNumComponents(fe,&fComp);CHKERRQ(ierr);
1768         }
1769         else if (id == PETSCFV_CLASSID) {
1770           fv = (PetscFV) disc;
1771           ierr = PetscFVGetNumComponents(fv,&fComp);CHKERRQ(ierr);
1772         }
1773         else {
1774           SETERRQ(PetscObjectComm((PetscObject)disc),PETSC_ERR_SUP,"Unsupported discretization");
1775         }
1776 
1777         if (numFields > 1) {
1778           ierr = PetscSectionGetFieldDof(conSec,point,f,&cDof);CHKERRQ(ierr);
1779           ierr = PetscSectionGetFieldOffset(conSec,point,f,&cOff);CHKERRQ(ierr);
1780         }
1781         else {
1782           ierr = PetscSectionGetDof(conSec,point,&cDof);CHKERRQ(ierr);
1783           ierr = PetscSectionGetOffset(conSec,point,&cOff);CHKERRQ(ierr);
1784         }
1785         if (!cDof) continue;
1786 
1787         /* make sure that every row for this point is the same size */
1788 #if defined(PETSC_USE_DEBUG)
1789         for (r = 0; r < cDof; r++) {
1790           if (cDof > 1 && r) {
1791             if ((ia[cOff+r+1]-ia[cOff+r]) != (ia[cOff+r]-ia[cOff+r-1])) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Two point rows have different nnz: %D vs. %D", (ia[cOff+r+1]-ia[cOff+r]), (ia[cOff+r]-ia[cOff+r-1]));
1792           }
1793         }
1794 #endif
1795         /* zero rows */
1796         for (i = ia[cOff] ; i< ia[cOff+cDof];i++) {
1797           vals[i] = 0.;
1798         }
1799         matOffset = ia[cOff];
1800         numFillCols = ia[cOff+1] - matOffset;
1801         pointMat = refPointFieldMats[childid-pRefStart][f];
1802         numCols = refPointFieldN[childid-pRefStart][f];
1803         offset = 0;
1804         for (i = 0; i < closureSize; i++) {
1805           PetscInt q = closure[2*i];
1806           PetscInt o = closure[2*i+1];
1807           PetscInt aDof, aOff, j, k, qConDof, qConOff;
1808 
1809           qConDof = qConOff = 0;
1810           if (numFields > 1) {
1811             ierr = PetscSectionGetFieldDof(section,q,f,&aDof);CHKERRQ(ierr);
1812             ierr = PetscSectionGetFieldOffset(section,q,f,&aOff);CHKERRQ(ierr);
1813             if (q >= conStart && q < conEnd) {
1814               ierr = PetscSectionGetFieldDof(conSec,q,f,&qConDof);CHKERRQ(ierr);
1815               ierr = PetscSectionGetFieldOffset(conSec,q,f,&qConOff);CHKERRQ(ierr);
1816             }
1817           }
1818           else {
1819             ierr = PetscSectionGetDof(section,q,&aDof);CHKERRQ(ierr);
1820             ierr = PetscSectionGetOffset(section,q,&aOff);CHKERRQ(ierr);
1821             if (q >= conStart && q < conEnd) {
1822               ierr = PetscSectionGetDof(conSec,q,&qConDof);CHKERRQ(ierr);
1823               ierr = PetscSectionGetOffset(conSec,q,&qConOff);CHKERRQ(ierr);
1824             }
1825           }
1826           if (!aDof) continue;
1827           if (qConDof) {
1828             /* this point has anchors: its rows of the matrix should already
1829              * be filled, thanks to preordering */
1830             /* first multiply into pointWork, then set in matrix */
1831             PetscInt aMatOffset = ia[qConOff];
1832             PetscInt aNumFillCols = ia[qConOff + 1] - aMatOffset;
1833             for (r = 0; r < cDof; r++) {
1834               for (j = 0; j < aNumFillCols; j++) {
1835                 PetscScalar inVal = 0;
1836                 for (k = 0; k < aDof; k++) {
1837                   PetscInt node = (o >= 0) ? (k / fComp) : ((aDof - 1 - k) / fComp);
1838                   PetscInt comp = (k % fComp);
1839                   PetscInt col  = node * fComp + comp;
1840 
1841                   inVal += pointMat[r * numCols + offset + col] * vals[aMatOffset + aNumFillCols * k + j];
1842                 }
1843                 pointWork[r * aNumFillCols + j] = inVal;
1844               }
1845             }
1846             /* assume that the columns are sorted, spend less time searching */
1847             for (j = 0, k = 0; j < aNumFillCols; j++) {
1848               PetscInt col = ja[aMatOffset + j];
1849               for (;k < numFillCols; k++) {
1850                 if (ja[matOffset + k] == col) {
1851                   break;
1852                 }
1853               }
1854               if (k == numFillCols) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"No nonzero space for (%d, %d)", cOff, col);
1855               for (r = 0; r < cDof; r++) {
1856                 vals[matOffset + numFillCols * r + k] = pointWork[r * aNumFillCols + j];
1857               }
1858             }
1859           }
1860           else {
1861             /* find where to put this portion of pointMat into the matrix */
1862             for (k = 0; k < numFillCols; k++) {
1863               if (ja[matOffset + k] == aOff) {
1864                 break;
1865               }
1866             }
1867             if (k == numFillCols) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"No nonzero space for (%d, %d)", cOff, aOff);
1868             for (j = 0; j < aDof; j++) {
1869               PetscInt node = (o >= 0) ? (j / fComp) : ((aDof - 1 - j) / fComp);
1870               PetscInt comp = (j % fComp);
1871               PetscInt col  = node * fComp + comp;
1872               for (r = 0; r < cDof; r++) {
1873                 vals[matOffset + numFillCols * r + k + col] += pointMat[r * numCols + offset + col];
1874               }
1875             }
1876           }
1877           offset += aDof;
1878         }
1879       }
1880       ierr = DMPlexRestoreTransitiveClosure(dm,parent,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
1881     }
1882     for (row = 0; row < nRows; row++) {
1883       ierr = MatSetValues(cMat,1,&row,ia[row+1]-ia[row],&ja[ia[row]],&vals[ia[row]],INSERT_VALUES);CHKERRQ(ierr);
1884     }
1885     ierr = MatRestoreRowIJ(cMat,0,PETSC_FALSE,PETSC_FALSE,&nRows,&ia,&ja,&done);CHKERRQ(ierr);
1886     if (!done) SETERRQ(PetscObjectComm((PetscObject)cMat),PETSC_ERR_PLIB,"Could not restore RowIJ of constraint matrix");
1887     ierr = MatAssemblyBegin(cMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1888     ierr = MatAssemblyEnd(cMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1889     ierr = PetscFree(vals);CHKERRQ(ierr);
1890   }
1891 
1892   /* clean up */
1893   ierr = ISRestoreIndices(anIS,&anchors);CHKERRQ(ierr);
1894   ierr = PetscFree2(perm,iperm);CHKERRQ(ierr);
1895   ierr = PetscFree(pointWork);CHKERRQ(ierr);
1896   ierr = DMPlexReferenceTreeRestoreChildrenMatrices(refTree,&refPointFieldMats,&refPointFieldN);CHKERRQ(ierr);
1897   PetscFunctionReturn(0);
1898 }
1899 
1900 #undef __FUNCT__
1901 #define __FUNCT__ "DMPlexTreeRefineCell"
1902 /* refine a single cell on rank 0: this is not intended to provide good local refinement, only to create an example of
1903  * a non-conforming mesh.  Local refinement comes later */
1904 PetscErrorCode DMPlexTreeRefineCell (DM dm, PetscInt cell, DM *ncdm)
1905 {
1906   DM K;
1907   PetscMPIInt rank;
1908   PetscInt dim, *pNewStart, *pNewEnd, *pNewCount, *pOldStart, *pOldEnd, offset, d, pStart, pEnd;
1909   PetscInt numNewCones, *newConeSizes, *newCones, *newOrientations;
1910   PetscInt *Kembedding;
1911   PetscInt *cellClosure=NULL, nc;
1912   PetscScalar *newVertexCoords;
1913   PetscInt numPointsWithParents, *parents, *childIDs, *perm, *iperm, *preOrient, pOffset;
1914   PetscSection parentSection;
1915   PetscErrorCode ierr;
1916 
1917   PetscFunctionBegin;
1918   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm),&rank);CHKERRQ(ierr);
1919   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
1920   ierr = DMPlexCreate(PetscObjectComm((PetscObject)dm), ncdm);CHKERRQ(ierr);
1921   ierr = DMSetDimension(*ncdm,dim);CHKERRQ(ierr);
1922 
1923   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
1924   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm),&parentSection);CHKERRQ(ierr);
1925   ierr = DMPlexGetReferenceTree(dm,&K);CHKERRQ(ierr);
1926   if (!rank) {
1927     /* compute the new charts */
1928     ierr = PetscMalloc5(dim+1,&pNewCount,dim+1,&pNewStart,dim+1,&pNewEnd,dim+1,&pOldStart,dim+1,&pOldEnd);CHKERRQ(ierr);
1929     offset = 0;
1930     for (d = 0; d <= dim; d++) {
1931       PetscInt pOldCount, kStart, kEnd, k;
1932 
1933       pNewStart[d] = offset;
1934       ierr = DMPlexGetHeightStratum(dm,d,&pOldStart[d],&pOldEnd[d]);CHKERRQ(ierr);
1935       ierr = DMPlexGetHeightStratum(K,d,&kStart,&kEnd);CHKERRQ(ierr);
1936       pOldCount = pOldEnd[d] - pOldStart[d];
1937       /* adding the new points */
1938       pNewCount[d] = pOldCount + kEnd - kStart;
1939       if (!d) {
1940         /* removing the cell */
1941         pNewCount[d]--;
1942       }
1943       for (k = kStart; k < kEnd; k++) {
1944         PetscInt parent;
1945         ierr = DMPlexGetTreeParent(K,k,&parent,NULL);CHKERRQ(ierr);
1946         if (parent == k) {
1947           /* avoid double counting points that won't actually be new */
1948           pNewCount[d]--;
1949         }
1950       }
1951       pNewEnd[d] = pNewStart[d] + pNewCount[d];
1952       offset = pNewEnd[d];
1953 
1954     }
1955     if (cell < pOldStart[0] || cell >= pOldEnd[0]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"%d not in cell range [%d, %d)", cell, pOldStart[0], pOldEnd[0]);
1956     /* get the current closure of the cell that we are removing */
1957     ierr = DMPlexGetTransitiveClosure(dm,cell,PETSC_TRUE,&nc,&cellClosure);CHKERRQ(ierr);
1958 
1959     ierr = PetscMalloc1(pNewEnd[dim],&newConeSizes);CHKERRQ(ierr);
1960     {
1961       PetscInt kStart, kEnd, k, closureSizeK, *closureK = NULL, j;
1962 
1963       ierr = DMPlexGetChart(K,&kStart,&kEnd);CHKERRQ(ierr);
1964       ierr = PetscMalloc4(kEnd-kStart,&Kembedding,kEnd-kStart,&perm,kEnd-kStart,&iperm,kEnd-kStart,&preOrient);CHKERRQ(ierr);
1965 
1966       for (k = kStart; k < kEnd; k++) {
1967         perm[k - kStart] = k;
1968         iperm [k - kStart] = k - kStart;
1969         preOrient[k - kStart] = 0;
1970       }
1971 
1972       ierr = DMPlexGetTransitiveClosure(K,0,PETSC_TRUE,&closureSizeK,&closureK);CHKERRQ(ierr);
1973       for (j = 1; j < closureSizeK; j++) {
1974         PetscInt parentOrientA = closureK[2*j+1];
1975         PetscInt parentOrientB = cellClosure[2*j+1];
1976         PetscInt p, q;
1977 
1978         p = closureK[2*j];
1979         q = cellClosure[2*j];
1980         for (d = 0; d <= dim; d++) {
1981           if (q >= pOldStart[d] && q < pOldEnd[d]) {
1982             Kembedding[p] = (q - pOldStart[d]) + pNewStart[d];
1983           }
1984         }
1985         if (parentOrientA != parentOrientB) {
1986           PetscInt numChildren, i;
1987           const PetscInt *children;
1988 
1989           ierr = DMPlexGetTreeChildren(K,p,&numChildren,&children);CHKERRQ(ierr);
1990           for (i = 0; i < numChildren; i++) {
1991             PetscInt kPerm, oPerm;
1992 
1993             k    = children[i];
1994             ierr = DMPlexReferenceTreeGetChildSymmetry(K,p,parentOrientA,0,k,parentOrientB,&oPerm,&kPerm);CHKERRQ(ierr);
1995             /* perm = what refTree position I'm in */
1996             perm[kPerm-kStart]      = k;
1997             /* iperm = who is at this position */
1998             iperm[k-kStart]         = kPerm-kStart;
1999             preOrient[kPerm-kStart] = oPerm;
2000           }
2001         }
2002       }
2003       ierr = DMPlexRestoreTransitiveClosure(K,0,PETSC_TRUE,&closureSizeK,&closureK);CHKERRQ(ierr);
2004     }
2005     ierr = PetscSectionSetChart(parentSection,0,pNewEnd[dim]);CHKERRQ(ierr);
2006     offset = 0;
2007     numNewCones = 0;
2008     for (d = 0; d <= dim; d++) {
2009       PetscInt kStart, kEnd, k;
2010       PetscInt p;
2011       PetscInt size;
2012 
2013       for (p = pOldStart[d]; p < pOldEnd[d]; p++) {
2014         /* skip cell 0 */
2015         if (p == cell) continue;
2016         /* old cones to new cones */
2017         ierr = DMPlexGetConeSize(dm,p,&size);CHKERRQ(ierr);
2018         newConeSizes[offset++] = size;
2019         numNewCones += size;
2020       }
2021 
2022       ierr = DMPlexGetHeightStratum(K,d,&kStart,&kEnd);CHKERRQ(ierr);
2023       for (k = kStart; k < kEnd; k++) {
2024         PetscInt kParent;
2025 
2026         ierr = DMPlexGetTreeParent(K,k,&kParent,NULL);CHKERRQ(ierr);
2027         if (kParent != k) {
2028           Kembedding[k] = offset;
2029           ierr = DMPlexGetConeSize(K,k,&size);CHKERRQ(ierr);
2030           newConeSizes[offset++] = size;
2031           numNewCones += size;
2032           if (kParent != 0) {
2033             ierr = PetscSectionSetDof(parentSection,Kembedding[k],1);CHKERRQ(ierr);
2034           }
2035         }
2036       }
2037     }
2038 
2039     ierr = PetscSectionSetUp(parentSection);CHKERRQ(ierr);
2040     ierr = PetscSectionGetStorageSize(parentSection,&numPointsWithParents);CHKERRQ(ierr);
2041     ierr = PetscMalloc2(numNewCones,&newCones,numNewCones,&newOrientations);CHKERRQ(ierr);
2042     ierr = PetscMalloc2(numPointsWithParents,&parents,numPointsWithParents,&childIDs);CHKERRQ(ierr);
2043 
2044     /* fill new cones */
2045     offset = 0;
2046     for (d = 0; d <= dim; d++) {
2047       PetscInt kStart, kEnd, k, l;
2048       PetscInt p;
2049       PetscInt size;
2050       const PetscInt *cone, *orientation;
2051 
2052       for (p = pOldStart[d]; p < pOldEnd[d]; p++) {
2053         /* skip cell 0 */
2054         if (p == cell) continue;
2055         /* old cones to new cones */
2056         ierr = DMPlexGetConeSize(dm,p,&size);CHKERRQ(ierr);
2057         ierr = DMPlexGetCone(dm,p,&cone);CHKERRQ(ierr);
2058         ierr = DMPlexGetConeOrientation(dm,p,&orientation);CHKERRQ(ierr);
2059         for (l = 0; l < size; l++) {
2060           newCones[offset]          = (cone[l] - pOldStart[d + 1]) + pNewStart[d + 1];
2061           newOrientations[offset++] = orientation[l];
2062         }
2063       }
2064 
2065       ierr = DMPlexGetHeightStratum(K,d,&kStart,&kEnd);CHKERRQ(ierr);
2066       for (k = kStart; k < kEnd; k++) {
2067         PetscInt kPerm = perm[k], kParent;
2068         PetscInt preO  = preOrient[k];
2069 
2070         ierr = DMPlexGetTreeParent(K,k,&kParent,NULL);CHKERRQ(ierr);
2071         if (kParent != k) {
2072           /* embed new cones */
2073           ierr = DMPlexGetConeSize(K,k,&size);CHKERRQ(ierr);
2074           ierr = DMPlexGetCone(K,kPerm,&cone);CHKERRQ(ierr);
2075           ierr = DMPlexGetConeOrientation(K,kPerm,&orientation);CHKERRQ(ierr);
2076           for (l = 0; l < size; l++) {
2077             PetscInt q, m = (preO >= 0) ? ((preO + l) % size) : ((size -(preO + 1) - l) % size);
2078             PetscInt newO, lSize, oTrue;
2079 
2080             q                         = iperm[cone[m]];
2081             newCones[offset]          = Kembedding[q];
2082             ierr                      = DMPlexGetConeSize(K,q,&lSize);CHKERRQ(ierr);
2083             oTrue                     = orientation[m];
2084             oTrue                     = ((!lSize) || (preOrient[k] >= 0)) ? oTrue : -(oTrue + 2);
2085             newO                      = DihedralCompose(lSize,oTrue,preOrient[q]);
2086             newOrientations[offset++] = newO;
2087           }
2088           if (kParent != 0) {
2089             PetscInt newPoint = Kembedding[kParent];
2090             ierr              = PetscSectionGetOffset(parentSection,Kembedding[k],&pOffset);CHKERRQ(ierr);
2091             parents[pOffset]  = newPoint;
2092             childIDs[pOffset] = k;
2093           }
2094         }
2095       }
2096     }
2097 
2098     ierr = PetscMalloc1(dim*(pNewEnd[dim]-pNewStart[dim]),&newVertexCoords);CHKERRQ(ierr);
2099 
2100     /* fill coordinates */
2101     offset = 0;
2102     {
2103       PetscInt kStart, kEnd, l;
2104       PetscSection vSection;
2105       PetscInt v;
2106       Vec coords;
2107       PetscScalar *coordvals;
2108       PetscInt dof, off;
2109       PetscReal v0[3], J[9], detJ;
2110 
2111 #if defined(PETSC_USE_DEBUG)
2112       {
2113         PetscInt k;
2114         ierr = DMPlexGetHeightStratum(K,0,&kStart,&kEnd);CHKERRQ(ierr);
2115         for (k = kStart; k < kEnd; k++) {
2116           ierr = DMPlexComputeCellGeometryFEM(K, k, NULL, v0, J, NULL, &detJ);CHKERRQ(ierr);
2117           if (detJ <= 0.) SETERRQ1 (PETSC_COMM_SELF,PETSC_ERR_PLIB,"reference tree cell %d has bad determinant",k);
2118         }
2119       }
2120 #endif
2121       ierr = DMPlexComputeCellGeometryFEM(dm, cell, NULL, v0, J, NULL, &detJ);CHKERRQ(ierr);
2122       ierr = DMGetCoordinateSection(dm,&vSection);CHKERRQ(ierr);
2123       ierr = DMGetCoordinatesLocal(dm,&coords);CHKERRQ(ierr);
2124       ierr = VecGetArray(coords,&coordvals);CHKERRQ(ierr);
2125       for (v = pOldStart[dim]; v < pOldEnd[dim]; v++) {
2126 
2127         ierr = PetscSectionGetDof(vSection,v,&dof);CHKERRQ(ierr);
2128         ierr = PetscSectionGetOffset(vSection,v,&off);CHKERRQ(ierr);
2129         for (l = 0; l < dof; l++) {
2130           newVertexCoords[offset++] = coordvals[off + l];
2131         }
2132       }
2133       ierr = VecRestoreArray(coords,&coordvals);CHKERRQ(ierr);
2134 
2135       ierr = DMGetCoordinateSection(K,&vSection);CHKERRQ(ierr);
2136       ierr = DMGetCoordinatesLocal(K,&coords);CHKERRQ(ierr);
2137       ierr = VecGetArray(coords,&coordvals);CHKERRQ(ierr);
2138       ierr = DMPlexGetDepthStratum(K,0,&kStart,&kEnd);CHKERRQ(ierr);
2139       for (v = kStart; v < kEnd; v++) {
2140         PetscReal coord[3], newCoord[3];
2141         PetscInt  vPerm = perm[v];
2142         PetscInt  kParent;
2143 
2144         ierr = DMPlexGetTreeParent(K,v,&kParent,NULL);CHKERRQ(ierr);
2145         if (kParent != v) {
2146           /* this is a new vertex */
2147           ierr = PetscSectionGetOffset(vSection,vPerm,&off);CHKERRQ(ierr);
2148           for (l = 0; l < dim; ++l) coord[l] = PetscRealPart(coordvals[off+l]);
2149           CoordinatesRefToReal(dim, dim, v0, J, coord, newCoord);CHKERRQ(ierr);
2150           for (l = 0; l < dim; ++l) newVertexCoords[offset+l] = newCoord[l];
2151           offset += dim;
2152         }
2153       }
2154       ierr = VecRestoreArray(coords,&coordvals);CHKERRQ(ierr);
2155     }
2156 
2157     /* need to reverse the order of pNewCount: vertices first, cells last */
2158     for (d = 0; d < (dim + 1) / 2; d++) {
2159       PetscInt tmp;
2160 
2161       tmp = pNewCount[d];
2162       pNewCount[d] = pNewCount[dim - d];
2163       pNewCount[dim - d] = tmp;
2164     }
2165 
2166     ierr = DMPlexCreateFromDAG(*ncdm,dim,pNewCount,newConeSizes,newCones,newOrientations,newVertexCoords);CHKERRQ(ierr);
2167     ierr = DMPlexSetReferenceTree(*ncdm,K);CHKERRQ(ierr);
2168     ierr = DMPlexSetTree(*ncdm,parentSection,parents,childIDs);CHKERRQ(ierr);
2169 
2170     /* clean up */
2171     ierr = DMPlexRestoreTransitiveClosure(dm,cell,PETSC_TRUE,&nc,&cellClosure);CHKERRQ(ierr);
2172     ierr = PetscFree5(pNewCount,pNewStart,pNewEnd,pOldStart,pOldEnd);CHKERRQ(ierr);
2173     ierr = PetscFree(newConeSizes);CHKERRQ(ierr);
2174     ierr = PetscFree2(newCones,newOrientations);CHKERRQ(ierr);
2175     ierr = PetscFree(newVertexCoords);CHKERRQ(ierr);
2176     ierr = PetscFree2(parents,childIDs);CHKERRQ(ierr);
2177     ierr = PetscFree4(Kembedding,perm,iperm,preOrient);CHKERRQ(ierr);
2178   }
2179   else {
2180     PetscInt    p, counts[4];
2181     PetscInt    *coneSizes, *cones, *orientations;
2182     Vec         coordVec;
2183     PetscScalar *coords;
2184 
2185     for (d = 0; d <= dim; d++) {
2186       PetscInt dStart, dEnd;
2187 
2188       ierr = DMPlexGetDepthStratum(dm,d,&dStart,&dEnd);CHKERRQ(ierr);
2189       counts[d] = dEnd - dStart;
2190     }
2191     ierr = PetscMalloc1(pEnd-pStart,&coneSizes);CHKERRQ(ierr);
2192     for (p = pStart; p < pEnd; p++) {
2193       ierr = DMPlexGetConeSize(dm,p,&coneSizes[p-pStart]);CHKERRQ(ierr);
2194     }
2195     ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr);
2196     ierr = DMPlexGetConeOrientations(dm, &orientations);CHKERRQ(ierr);
2197     ierr = DMGetCoordinatesLocal(dm,&coordVec);CHKERRQ(ierr);
2198     ierr = VecGetArray(coordVec,&coords);CHKERRQ(ierr);
2199 
2200     ierr = PetscSectionSetChart(parentSection,pStart,pEnd);CHKERRQ(ierr);
2201     ierr = PetscSectionSetUp(parentSection);CHKERRQ(ierr);
2202     ierr = DMPlexCreateFromDAG(*ncdm,dim,counts,coneSizes,cones,orientations,NULL);CHKERRQ(ierr);
2203     ierr = DMPlexSetReferenceTree(*ncdm,K);CHKERRQ(ierr);
2204     ierr = DMPlexSetTree(*ncdm,parentSection,NULL,NULL);CHKERRQ(ierr);
2205     ierr = VecRestoreArray(coordVec,&coords);CHKERRQ(ierr);
2206   }
2207   ierr = PetscSectionDestroy(&parentSection);CHKERRQ(ierr);
2208 
2209   PetscFunctionReturn(0);
2210 }
2211 
2212 #undef __FUNCT__
2213 #define __FUNCT__ "DMPlexComputeInterpolatorTree"
2214 PetscErrorCode DMPlexComputeInterpolatorTree(DM coarse, DM fine, PetscSF coarseToFine, PetscInt *childIds, Mat mat)
2215 {
2216   PetscSF        coarseToFineEmbedded;
2217   PetscSection   globalCoarse, globalFine;
2218   PetscSection   localCoarse, localFine;
2219   PetscSection   aSec, cSec;
2220   PetscSection   rootIndicesSec, rootMatricesSec;
2221   PetscSection   leafIndicesSec, leafMatricesSec;
2222   PetscInt       *rootIndices, *leafIndices;
2223   PetscScalar    *rootMatrices, *leafMatrices;
2224   IS             aIS;
2225   const PetscInt *anchors;
2226   Mat            cMat;
2227   PetscInt       numFields;
2228   PetscInt       pStartC, pEndC, pStartF, pEndF, p;
2229   PetscInt       aStart, aEnd, cStart, cEnd;
2230   PetscInt       *maxChildIds;
2231   PetscInt       *offsets, *newOffsets, *offsetsCopy, *newOffsetsCopy, *rowOffsets, *numD, *numO;
2232   PetscErrorCode ierr;
2233 
2234   PetscFunctionBegin;
2235   ierr = DMPlexGetChart(coarse,&pStartC,&pEndC);CHKERRQ(ierr);
2236   ierr = DMPlexGetChart(fine,&pStartF,&pEndF);CHKERRQ(ierr);
2237   ierr = DMGetDefaultGlobalSection(fine,&globalFine);CHKERRQ(ierr);
2238   { /* winnow fine points that don't have global dofs out of the sf */
2239     PetscInt dof, cdof, numPointsWithDofs, offset, *pointsWithDofs;
2240 
2241     for (p = pStartF, numPointsWithDofs = 0; p < pEndF; p++) {
2242       ierr = PetscSectionGetDof(globalFine,p,&dof);CHKERRQ(ierr);
2243       ierr = PetscSectionGetConstraintDof(globalFine,p,&cdof);CHKERRQ(ierr);
2244       if ((dof - cdof) > 0) {
2245         numPointsWithDofs++;
2246       }
2247     }
2248     ierr = PetscMalloc1(numPointsWithDofs,&pointsWithDofs);CHKERRQ(ierr);
2249     for (p = pStartF, offset = 0; p < pEndF; p++) {
2250       ierr = PetscSectionGetDof(globalFine,p,&dof);CHKERRQ(ierr);
2251       ierr = PetscSectionGetConstraintDof(globalFine,p,&cdof);CHKERRQ(ierr);
2252       if ((dof - cdof) > 0) {
2253         pointsWithDofs[offset++] = p - pStartF;
2254       }
2255     }
2256     ierr = PetscSFCreateEmbeddedLeafSF(coarseToFine, numPointsWithDofs, pointsWithDofs, &coarseToFineEmbedded);CHKERRQ(ierr);
2257     ierr = PetscFree(pointsWithDofs);CHKERRQ(ierr);
2258   }
2259   /* communicate back to the coarse mesh which coarse points have children (that may require interpolation) */
2260   ierr = PetscMalloc1(pEndC-pStartC,&maxChildIds);CHKERRQ(ierr);
2261   for (p = pStartC; p < pEndC; p++) {
2262     maxChildIds[p - pStartC] = -2;
2263   }
2264   ierr = PetscSFReduceBegin(coarseToFineEmbedded,MPIU_INT,childIds,maxChildIds,MPIU_MAX);CHKERRQ(ierr);
2265   ierr = PetscSFReduceEnd(coarseToFineEmbedded,MPIU_INT,childIds,maxChildIds,MPIU_MAX);CHKERRQ(ierr);
2266 
2267   ierr = DMGetDefaultSection(coarse,&localCoarse);CHKERRQ(ierr);
2268   ierr = DMGetDefaultGlobalSection(coarse,&globalCoarse);CHKERRQ(ierr);
2269 
2270   ierr = DMPlexGetAnchors(coarse,&aSec,&aIS);CHKERRQ(ierr);
2271   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
2272   ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
2273 
2274   ierr = DMGetDefaultConstraints(coarse,&cSec,&cMat);CHKERRQ(ierr);
2275   ierr = PetscSectionGetChart(cSec,&cStart,&cEnd);CHKERRQ(ierr);
2276 
2277   /* create sections that will send to children the indices and matrices they will need to construct the interpolator */
2278   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)coarse),&rootIndicesSec);CHKERRQ(ierr);
2279   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)coarse),&rootMatricesSec);CHKERRQ(ierr);
2280   ierr = PetscSectionSetChart(rootIndicesSec,pStartC,pEndC);CHKERRQ(ierr);
2281   ierr = PetscSectionSetChart(rootMatricesSec,pStartC,pEndC);CHKERRQ(ierr);
2282   ierr = PetscSectionGetNumFields(globalCoarse,&numFields);CHKERRQ(ierr);
2283   {
2284     PetscInt maxFields = PetscMax(1,numFields) + 1;
2285     ierr = PetscMalloc7(maxFields,&offsets,maxFields,&offsetsCopy,maxFields,&newOffsets,maxFields,&newOffsetsCopy,maxFields,&rowOffsets,maxFields,&numD,maxFields,&numO);CHKERRQ(ierr);
2286   }
2287 
2288   for (p = pStartC; p < pEndC; p++) { /* count the sizes of the indices and matrices */
2289     PetscInt dof, matSize   = 0;
2290     PetscInt aDof           = 0;
2291     PetscInt cDof           = 0;
2292     PetscInt maxChildId     = maxChildIds[p - pStartC];
2293     PetscInt numRowIndices  = 0;
2294     PetscInt numColIndices  = 0;
2295 
2296     ierr = PetscSectionGetDof(globalCoarse,p,&dof);CHKERRQ(ierr);
2297     if (dof < 0) {
2298       dof = -(dof + 1);
2299     }
2300     if (p >= aStart && p < aEnd) {
2301       ierr = PetscSectionGetDof(aSec,p,&aDof);CHKERRQ(ierr);
2302     }
2303     if (p >= cStart && p < cEnd) {
2304       ierr = PetscSectionGetDof(cSec,p,&cDof);CHKERRQ(ierr);
2305     }
2306     offsets[0]    = 0;
2307     newOffsets[0] = 0;
2308     if (maxChildId >= 0) { /* this point has children (with dofs) that will need to be interpolated from the closure of p */
2309       PetscInt *closure = NULL, closureSize, cl, f;
2310 
2311       ierr = DMPlexGetTransitiveClosure(coarse,p,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
2312       for (cl = 0; cl < closureSize; cl++) { /* get the closure */
2313         PetscInt c = closure[2 * cl], clDof;
2314 
2315         ierr = PetscSectionGetDof(localCoarse,c,&clDof);CHKERRQ(ierr);
2316         numRowIndices += clDof;
2317         for (f = 0; f < numFields; f++) {
2318           ierr = PetscSectionGetFieldDof(localCoarse,c,f,&clDof);CHKERRQ(ierr);
2319           offsets[f + 1] += clDof;
2320         }
2321       }
2322       for (f = 0; f < numFields; f++) {
2323         offsets[f + 1]   += offsets[f];
2324         newOffsets[f + 1] = offsets[f + 1];
2325       }
2326       /* get the number of indices needed and their field offsets */
2327       ierr = DMPlexAnchorsModifyMat(coarse,localCoarse,closureSize,numRowIndices,closure,NULL,NULL,&numColIndices,NULL,NULL,newOffsets,PETSC_FALSE);CHKERRQ(ierr);
2328       ierr = DMPlexRestoreTransitiveClosure(coarse,p,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
2329       if (!numColIndices) { /* there are no hanging constraint modifications, so the matrix is just the identity: do not send it */
2330         numColIndices = numRowIndices;
2331         matSize = 0;
2332       }
2333       else if (numFields) { /* we send one submat for each field: sum their sizes */
2334         matSize = 0;
2335         for (f = 0; f < numFields; f++) {
2336           PetscInt numRow, numCol;
2337 
2338           numRow = offsets[f + 1] - offsets[f];
2339           numCol = newOffsets[f + 1] - newOffsets[f + 1];
2340           matSize += numRow * numCol;
2341         }
2342       }
2343       else {
2344         matSize = numRowIndices * numColIndices;
2345       }
2346     }
2347     else if (maxChildId == -1) {
2348       if (cDof > 0) { /* this point's dofs are interpolated via cMat: get the submatrix of cMat */
2349         PetscInt aOff, a, f;
2350 
2351         ierr = PetscSectionGetOffset(aSec,p,&aOff);CHKERRQ(ierr);
2352         for (f = 0; f < numFields; f++) {
2353           PetscInt fDof;
2354 
2355           ierr = PetscSectionGetFieldDof(localCoarse,p,f,&fDof);CHKERRQ(ierr);
2356           offsets[f+1] = fDof;
2357         }
2358         for (a = 0; a < aDof; a++) {
2359           PetscInt anchor = anchors[a + aOff], aLocalDof;
2360 
2361           ierr = PetscSectionGetDof(localCoarse,anchor,&aLocalDof);CHKERRQ(ierr);
2362           numColIndices += aLocalDof;
2363           for (f = 0; f < numFields; f++) {
2364             PetscInt fDof;
2365 
2366             ierr = PetscSectionGetFieldDof(localCoarse,anchor,f,&fDof);CHKERRQ(ierr);
2367             newOffsets[f+1] += fDof;
2368           }
2369         }
2370         if (numFields) {
2371           matSize = 0;
2372           for (f = 0; f < numFields; f++) {
2373             matSize += offsets[f+1] * newOffsets[f+1];
2374           }
2375         }
2376         else {
2377           matSize = numColIndices * dof;
2378         }
2379       }
2380       else { /* no children, and no constraints on dofs: just get the global indices */
2381         numColIndices = dof;
2382         matSize       = 0;
2383       }
2384     }
2385     /* we will pack the column indices with the field offsets */
2386     ierr = PetscSectionSetDof(rootIndicesSec,p,numColIndices ? numColIndices+2*numFields : 0);CHKERRQ(ierr);
2387     ierr = PetscSectionSetDof(rootMatricesSec,p,matSize);CHKERRQ(ierr);
2388   }
2389   ierr = PetscSectionSetUp(rootIndicesSec);CHKERRQ(ierr);
2390   ierr = PetscSectionSetUp(rootMatricesSec);CHKERRQ(ierr);
2391   {
2392     PetscInt    numRootIndices, numRootMatrices;
2393 
2394     ierr = PetscSectionGetStorageSize(rootIndicesSec,&numRootIndices);CHKERRQ(ierr);
2395     ierr = PetscSectionGetStorageSize(rootMatricesSec,&numRootMatrices);CHKERRQ(ierr);
2396     ierr = PetscMalloc2(numRootIndices,&rootIndices,numRootMatrices,&rootMatrices);CHKERRQ(ierr);
2397     for (p = pStartC; p < pEndC; p++) {
2398       PetscInt    numRowIndices, numColIndices, matSize, dof;
2399       PetscInt    pIndOff, pMatOff;
2400       PetscInt    *pInd;
2401       PetscInt    maxChildId = maxChildIds[p - pStartC];
2402       PetscScalar *pMat = NULL;
2403 
2404       ierr = PetscSectionGetDof(rootIndicesSec,p,&numColIndices);CHKERRQ(ierr);
2405       if (!numColIndices) {
2406         continue;
2407       }
2408       offsets[0]        = 0;
2409       newOffsets[0]     = 0;
2410       offsetsCopy[0]    = 0;
2411       newOffsetsCopy[0] = 0;
2412       numColIndices -= 2 * numFields;
2413       ierr = PetscSectionGetOffset(rootIndicesSec,p,&pIndOff);CHKERRQ(ierr);
2414       pInd = &(rootIndices[pIndOff]);
2415       ierr = PetscSectionGetDof(rootMatricesSec,p,&matSize);CHKERRQ(ierr);
2416       if (matSize) {
2417         ierr = PetscSectionGetOffset(rootMatricesSec,p,&pMatOff);CHKERRQ(ierr);
2418         pMat = &rootMatrices[pMatOff];
2419       }
2420       ierr = PetscSectionGetDof(globalCoarse,p,&dof);CHKERRQ(ierr);
2421       if (dof < 0) {
2422         dof = -(dof + 1);
2423       }
2424       if (maxChildId >= 0) { /* build an identity matrix, apply matrix constraints on the right */
2425         PetscInt i, j;
2426         PetscInt numRowIndices = matSize / numColIndices;
2427 
2428         if (!numRowIndices) { /* don't need to calculate the mat, just the indices */
2429           PetscInt numIndices, *indices;
2430           ierr = DMPlexGetClosureIndices(coarse,localCoarse,globalCoarse,p,&numIndices,&indices,offsets);CHKERRQ(ierr);
2431           if (numIndices != numColIndices) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"mismatching constraint indices calculations");
2432           for (i = 0; i < numColIndices; i++) {
2433             pInd[i] = indices[i];
2434           }
2435           for (i = 0; i < numFields; i++) {
2436             pInd[numColIndices + i]             = offsets[i+1];
2437             pInd[numColIndices + numFields + i] = offsets[i+1];
2438           }
2439           ierr = DMPlexRestoreClosureIndices(coarse,localCoarse,globalCoarse,p,&numIndices,&indices,offsets);CHKERRQ(ierr);
2440         }
2441         else {
2442           PetscInt closureSize, *closure = NULL, cl;
2443           PetscScalar *pMatIn, *pMatModified;
2444           PetscInt numPoints,*points;
2445 
2446           ierr = DMGetWorkArray(coarse,numRowIndices * numRowIndices,PETSC_SCALAR,&pMatIn);CHKERRQ(ierr);
2447           for (i = 0; i < numRowIndices; i++) { /* initialize to the identity */
2448             for (j = 0; j < numRowIndices; j++) {
2449               pMatIn[i * numRowIndices + j] = (i == j) ? 1. : 0.;
2450             }
2451           }
2452           ierr = DMPlexGetTransitiveClosure(coarse, p, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2453           if (numFields) {
2454             PetscInt f;
2455 
2456             for (cl = 0; cl < closureSize; cl++) {
2457               PetscInt c = closure[2 * cl];
2458 
2459               for (f = 0; f < numFields; f++) {
2460                 PetscInt fDof;
2461 
2462                 ierr = PetscSectionGetFieldDof(localCoarse,c,f,&fDof);CHKERRQ(ierr);
2463                 offsets[f + 1] += fDof;
2464               }
2465             }
2466             for (f = 0; f < numFields; f++) {
2467               offsets[f + 1]   += offsets[f];
2468               newOffsets[f + 1] = offsets[f + 1];
2469             }
2470           }
2471           /* apply hanging node constraints on the right, get the new points and the new offsets */
2472           ierr = DMPlexAnchorsModifyMat(coarse,localCoarse,closureSize,numRowIndices,closure,pMatIn,&numPoints,NULL,&points,&pMatModified,newOffsets,PETSC_FALSE);CHKERRQ(ierr);
2473           if (!numFields) {
2474             for (i = 0; i < numRowIndices * numColIndices; i++) {
2475               pMat[i] = pMatModified[i];
2476             }
2477           }
2478           else {
2479             PetscInt f, i, j, count;
2480             for (f = 0, count = 0; f < numFields; f++) {
2481               for (i = offsets[f]; i < offsets[f+1]; i++) {
2482                 for (j = newOffsets[f]; j < newOffsets[f+1]; j++, count++) {
2483                   pMat[count] = pMatModified[i * numColIndices + j];
2484                 }
2485               }
2486             }
2487           }
2488           ierr = DMRestoreWorkArray(coarse,numRowIndices * numColIndices,PETSC_SCALAR,&pMatModified);CHKERRQ(ierr);
2489           ierr = DMPlexRestoreTransitiveClosure(coarse, p, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2490           ierr = DMRestoreWorkArray(coarse,numRowIndices * numColIndices,PETSC_SCALAR,&pMatIn);CHKERRQ(ierr);
2491           if (numFields) {
2492             PetscInt f;
2493             for (f = 0; f < numFields; f++) {
2494               pInd[numColIndices + f]             = offsets[f+1];
2495               pInd[numColIndices + numFields + f] = newOffsets[f+1];
2496             }
2497             for (cl = 0; cl < numPoints*2; cl += 2) {
2498               PetscInt o = points[cl+1], globalOff, c = points[cl];
2499               ierr = PetscSectionGetOffset(globalCoarse, c, &globalOff);CHKERRQ(ierr);
2500               DMPlexGetIndicesPointFields_Internal(localCoarse, c, globalOff < 0 ? -(globalOff+1) : globalOff, newOffsets, PETSC_FALSE, o, pInd);
2501             }
2502           } else {
2503             for (cl = 0; cl < numPoints*2; cl += 2) {
2504               PetscInt o = points[cl+1], c = points[cl], globalOff;
2505               ierr = PetscSectionGetOffset(globalCoarse, c, &globalOff);CHKERRQ(ierr);
2506               DMPlexGetIndicesPoint_Internal(localCoarse, c, globalOff < 0 ? -(globalOff+1) : globalOff, newOffsets, PETSC_FALSE, o, pInd);
2507             }
2508           }
2509           ierr = DMRestoreWorkArray(coarse,numPoints,PETSC_SCALAR,&points);CHKERRQ(ierr);
2510         }
2511       }
2512       else if (matSize) {
2513         PetscInt cOff;
2514         PetscInt *rowIndices, *colIndices, a, aDof, aOff;
2515 
2516         numRowIndices = matSize / numColIndices;
2517         if (numRowIndices != dof) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Miscounted dofs");
2518         ierr = DMGetWorkArray(coarse,numRowIndices,PETSC_INT,&rowIndices);CHKERRQ(ierr);
2519         ierr = DMGetWorkArray(coarse,numColIndices,PETSC_INT,&colIndices);CHKERRQ(ierr);
2520         ierr = PetscSectionGetOffset(cSec,p,&cOff);CHKERRQ(ierr);
2521         ierr = PetscSectionGetDof(aSec,p,&aDof);CHKERRQ(ierr);
2522         ierr = PetscSectionGetOffset(aSec,p,&aOff);CHKERRQ(ierr);
2523         if (numFields) {
2524           PetscInt f;
2525 
2526           for (f = 0; f < numFields; f++) {
2527             PetscInt fDof;
2528             ierr = PetscSectionGetFieldDof(cSec,p,f,&fDof);CHKERRQ(ierr);
2529             offsets[f + 1] = fDof;
2530             for (a = 0; a < aDof; a++) {
2531               PetscInt anchor = anchors[a + aOff];
2532               ierr = PetscSectionGetFieldDof(localCoarse,anchor,f,&fDof);CHKERRQ(ierr);
2533               newOffsets[f + 1] += fDof;
2534             }
2535           }
2536           for (f = 0; f < numFields; f++) {
2537             offsets[f + 1]       += offsets[f];
2538             offsetsCopy[f + 1]    = offsets[f + 1];
2539             newOffsets[f + 1]    += newOffsets[f];
2540             newOffsetsCopy[f + 1] = newOffsets[f + 1];
2541           }
2542           DMPlexGetIndicesPointFields_Internal(cSec,p,cOff,offsetsCopy,PETSC_TRUE,0,rowIndices);CHKERRQ(ierr);
2543           for (a = 0; a < aDof; a++) {
2544             PetscInt anchor = anchors[a + aOff], lOff;
2545             ierr = PetscSectionGetOffset(localCoarse,anchor,&lOff);CHKERRQ(ierr);
2546             DMPlexGetIndicesPointFields_Internal(localCoarse,anchor,lOff,newOffsetsCopy,PETSC_TRUE,0,colIndices);CHKERRQ(ierr);
2547           }
2548         }
2549         else {
2550           DMPlexGetIndicesPoint_Internal(cSec,p,cOff,offsetsCopy,PETSC_TRUE,0,rowIndices);CHKERRQ(ierr);
2551           for (a = 0; a < aDof; a++) {
2552             PetscInt anchor = anchors[a + aOff], lOff;
2553             ierr = PetscSectionGetOffset(localCoarse,anchor,&lOff);CHKERRQ(ierr);
2554             DMPlexGetIndicesPoint_Internal(localCoarse,anchor,lOff,newOffsetsCopy,PETSC_TRUE,0,colIndices);CHKERRQ(ierr);
2555           }
2556         }
2557         if (numFields) {
2558           PetscInt count, f, a;
2559           for (f = 0, count = 0; f < numFields; f++) {
2560             PetscInt iSize = offsets[f + 1] - offsets[f];
2561             PetscInt jSize = newOffsets[f + 1] - newOffsets[f];
2562             ierr = MatGetValues(cMat,iSize,&rowIndices[offsets[f]],jSize,&colIndices[newOffsets[f]],&pMat[count]);CHKERRQ(ierr);
2563             count += iSize * jSize;
2564             pInd[numColIndices + f]             = offsets[f+1];
2565             pInd[numColIndices + numFields + f] = newOffsets[f+1];
2566           }
2567           for (a = 0; a < aDof; a++) {
2568             PetscInt anchor = anchors[a + aOff];
2569             PetscInt gOff;
2570             ierr = PetscSectionGetOffset(globalCoarse,anchor,&gOff);CHKERRQ(ierr);
2571             DMPlexGetIndicesPointFields_Internal(localCoarse,anchor,gOff < 0 ? -(gOff + 1) : gOff,newOffsets,PETSC_FALSE,0,pInd);CHKERRQ(ierr);
2572           }
2573         }
2574         else {
2575           PetscInt a;
2576           ierr = MatGetValues(cMat,numRowIndices,rowIndices,numColIndices,colIndices,pMat);CHKERRQ(ierr);
2577           for (a = 0; a < aDof; a++) {
2578             PetscInt anchor = anchors[a + aOff];
2579             PetscInt gOff;
2580             ierr = PetscSectionGetOffset(globalCoarse,anchor,&gOff);CHKERRQ(ierr);
2581             DMPlexGetIndicesPoint_Internal(localCoarse,anchor,gOff < 0 ? -(gOff + 1) : gOff,newOffsets,PETSC_FALSE,0,pInd);CHKERRQ(ierr);
2582           }
2583         }
2584         ierr = DMRestoreWorkArray(coarse,numColIndices,PETSC_INT,&colIndices);CHKERRQ(ierr);
2585         ierr = DMRestoreWorkArray(coarse,numRowIndices,PETSC_INT,&rowIndices);CHKERRQ(ierr);
2586       }
2587       else {
2588         PetscInt gOff;
2589 
2590         ierr = PetscSectionGetOffset(globalCoarse,p,&gOff);CHKERRQ(ierr);
2591         if (numFields) {
2592           PetscInt f;
2593 
2594           for (f = 0; f < numFields; f++) {
2595             PetscInt fDof;
2596             ierr = PetscSectionGetFieldDof(localCoarse,p,f,&fDof);CHKERRQ(ierr);
2597             offsets[f + 1] = fDof + offsets[f];
2598           }
2599           for (f = 0; f < numFields; f++) {
2600             pInd[numColIndices + f]             = offsets[f+1];
2601             pInd[numColIndices + numFields + f] = offsets[f+1];
2602           }
2603           DMPlexGetIndicesPointFields_Internal(localCoarse,p,gOff < 0 ? -(gOff + 1) : gOff,offsets,PETSC_FALSE,0,pInd);CHKERRQ(ierr);
2604         }
2605         else {
2606           DMPlexGetIndicesPoint_Internal(localCoarse,p,gOff < 0 ? -(gOff + 1) : gOff,offsets,PETSC_FALSE,0,pInd);CHKERRQ(ierr);
2607         }
2608       }
2609     }
2610     ierr = PetscFree(maxChildIds);CHKERRQ(ierr);
2611   }
2612   {
2613     PetscSF  indicesSF, matricesSF;
2614     PetscInt *remoteOffsetsIndices, *remoteOffsetsMatrices, numLeafIndices, numLeafMatrices;
2615 
2616     ierr = PetscSectionCreate(PetscObjectComm((PetscObject)fine),&leafIndicesSec);CHKERRQ(ierr);
2617     ierr = PetscSectionCreate(PetscObjectComm((PetscObject)fine),&leafMatricesSec);CHKERRQ(ierr);
2618     ierr = PetscSFDistributeSection(coarseToFineEmbedded,rootIndicesSec,&remoteOffsetsIndices,leafIndicesSec);CHKERRQ(ierr);
2619     ierr = PetscSFDistributeSection(coarseToFineEmbedded,rootMatricesSec,&remoteOffsetsMatrices,leafMatricesSec);CHKERRQ(ierr);
2620     ierr = PetscSFCreateSectionSF(coarseToFineEmbedded,rootIndicesSec,remoteOffsetsIndices,leafIndicesSec,&indicesSF);CHKERRQ(ierr);
2621     ierr = PetscSFCreateSectionSF(coarseToFineEmbedded,rootMatricesSec,remoteOffsetsMatrices,leafMatricesSec,&matricesSF);CHKERRQ(ierr);
2622     ierr = PetscSFDestroy(&coarseToFineEmbedded);CHKERRQ(ierr);
2623     ierr = PetscFree(remoteOffsetsIndices);CHKERRQ(ierr);
2624     ierr = PetscFree(remoteOffsetsMatrices);CHKERRQ(ierr);
2625     ierr = PetscSectionGetStorageSize(leafIndicesSec,&numLeafIndices);CHKERRQ(ierr);
2626     ierr = PetscSectionGetStorageSize(leafMatricesSec,&numLeafMatrices);CHKERRQ(ierr);
2627     ierr = PetscMalloc2(numLeafIndices,&leafIndices,numLeafMatrices,&leafMatrices);CHKERRQ(ierr);
2628     ierr = PetscSFBcastBegin(indicesSF,MPIU_INT,rootIndices,leafIndices);CHKERRQ(ierr);
2629     ierr = PetscSFBcastBegin(matricesSF,MPIU_SCALAR,rootMatrices,leafMatrices);CHKERRQ(ierr);
2630     ierr = PetscSFBcastEnd(indicesSF,MPIU_INT,rootIndices,leafIndices);CHKERRQ(ierr);
2631     ierr = PetscSFBcastEnd(matricesSF,MPIU_SCALAR,rootMatrices,leafMatrices);CHKERRQ(ierr);
2632     ierr = PetscSFDestroy(&matricesSF);CHKERRQ(ierr);
2633     ierr = PetscSFDestroy(&indicesSF);CHKERRQ(ierr);
2634     ierr = PetscFree2(rootIndices,rootMatrices);CHKERRQ(ierr);
2635     ierr = PetscSectionDestroy(&rootIndicesSec);CHKERRQ(ierr);
2636     ierr = PetscSectionDestroy(&rootMatricesSec);CHKERRQ(ierr);
2637   }
2638   /* count to preallocate */
2639   ierr = DMGetDefaultSection(fine,&localFine);CHKERRQ(ierr);
2640   {
2641     PetscInt    nGlobal;
2642     PetscInt    *dnnz, *onnz;
2643     PetscLayout rowMap, colMap;
2644     PetscInt    rowStart, rowEnd, colStart, colEnd;
2645     PetscInt    maxDof;
2646     PetscInt    *rowIndices;
2647     DM           refTree;
2648     PetscInt     **refPointFieldN;
2649     PetscScalar  ***refPointFieldMats;
2650     PetscSection refConSec, refAnSec;
2651     PetscInt     pRefStart,pRefEnd,maxConDof,maxColumns,leafStart,leafEnd;
2652     PetscScalar  *pointWork;
2653 
2654     ierr = PetscSectionGetConstrainedStorageSize(globalFine,&nGlobal);CHKERRQ(ierr);
2655     ierr = PetscCalloc2(nGlobal,&dnnz,nGlobal,&onnz);CHKERRQ(ierr);
2656     ierr = MatGetLayouts(mat,&rowMap,&colMap);CHKERRQ(ierr);
2657     ierr = PetscLayoutSetUp(rowMap);CHKERRQ(ierr);
2658     ierr = PetscLayoutSetUp(colMap);CHKERRQ(ierr);
2659     ierr = PetscLayoutGetRange(rowMap,&rowStart,&rowEnd);CHKERRQ(ierr);
2660     ierr = PetscLayoutGetRange(colMap,&colStart,&colEnd);CHKERRQ(ierr);
2661     ierr = PetscSectionGetMaxDof(globalFine,&maxDof);CHKERRQ(ierr);
2662     ierr = PetscSectionGetChart(leafIndicesSec,&leafStart,&leafEnd);CHKERRQ(ierr);
2663     ierr = DMGetWorkArray(fine,maxDof,PETSC_INT,&rowIndices);CHKERRQ(ierr);
2664     for (p = leafStart; p < leafEnd; p++) {
2665       PetscInt    gDof, gcDof, gOff;
2666       PetscInt    numColIndices, pIndOff, *pInd;
2667       PetscInt    matSize;
2668       PetscInt    i;
2669 
2670       ierr = PetscSectionGetDof(globalFine,p,&gDof);CHKERRQ(ierr);
2671       ierr = PetscSectionGetConstraintDof(globalFine,p,&gcDof);CHKERRQ(ierr);
2672       if ((gDof - gcDof) <= 0) {
2673         continue;
2674       }
2675       ierr = PetscSectionGetOffset(globalFine,p,&gOff);CHKERRQ(ierr);
2676       if (gOff < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"I though having global dofs meant a non-negative offset");
2677       if ((gOff < rowStart) || ((gOff + gDof - gcDof) > rowEnd)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"I thought the row map would constrain the global dofs");
2678       ierr = PetscSectionGetDof(leafIndicesSec,p,&numColIndices);CHKERRQ(ierr);
2679       ierr = PetscSectionGetOffset(leafIndicesSec,p,&pIndOff);CHKERRQ(ierr);
2680       numColIndices -= 2 * numFields;
2681       if (numColIndices <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"global fine dof with no dofs to interpolate from");
2682       pInd = &leafIndices[pIndOff];
2683       offsets[0]        = 0;
2684       offsetsCopy[0]    = 0;
2685       newOffsets[0]     = 0;
2686       newOffsetsCopy[0] = 0;
2687       if (numFields) {
2688         PetscInt f;
2689         for (f = 0; f < numFields; f++) {
2690           PetscInt rowDof;
2691 
2692           ierr = PetscSectionGetFieldDof(localFine,p,f,&rowDof);CHKERRQ(ierr);
2693           offsets[f + 1]        = offsets[f] + rowDof;
2694           offsetsCopy[f + 1]    = offsets[f + 1];
2695           newOffsets[f + 1]     = pInd[numColIndices + numFields + f];
2696           numD[f] = 0;
2697           numO[f] = 0;
2698         }
2699         ierr = DMPlexGetIndicesPointFields_Internal(localFine,p,gOff,offsetsCopy,PETSC_FALSE,0,rowIndices);CHKERRQ(ierr);
2700         for (f = 0; f < numFields; f++) {
2701           PetscInt colOffset    = newOffsets[f];
2702           PetscInt numFieldCols = newOffsets[f + 1] - newOffsets[f];
2703 
2704           for (i = 0; i < numFieldCols; i++) {
2705             PetscInt gInd = pInd[i + colOffset];
2706 
2707             if (gInd >= colStart && gInd < colEnd) {
2708               numD[f]++;
2709             }
2710             else if (gInd >= 0) { /* negative means non-entry */
2711               numO[f]++;
2712             }
2713           }
2714         }
2715       }
2716       else {
2717         ierr = DMPlexGetIndicesPoint_Internal(localFine,p,gOff,offsetsCopy,PETSC_FALSE,0,rowIndices);CHKERRQ(ierr);
2718         numD[0] = 0;
2719         numO[0] = 0;
2720         for (i = 0; i < numColIndices; i++) {
2721           PetscInt gInd = pInd[i];
2722 
2723           if (gInd >= colStart && gInd < colEnd) {
2724             numD[0]++;
2725           }
2726           else if (gInd >= 0) { /* negative means non-entry */
2727             numO[0]++;
2728           }
2729         }
2730       }
2731       ierr = PetscSectionGetDof(leafMatricesSec,p,&matSize);CHKERRQ(ierr);
2732       if (!matSize) { /* incoming matrix is identity */
2733         PetscInt childId;
2734 
2735         childId = childIds[p-pStartF];
2736         if (childId < 0) { /* no child interpolation: one nnz per */
2737           if (numFields) {
2738             PetscInt f;
2739             for (f = 0; f < numFields; f++) {
2740               PetscInt numRows = offsets[f+1] - offsets[f], row;
2741               for (row = 0; row < numRows; row++) {
2742                 PetscInt gIndCoarse = pInd[newOffsets[f] + row];
2743                 PetscInt gIndFine   = rowIndices[offsets[f] + row];
2744                 if (gIndCoarse >= colStart && gIndCoarse < colEnd) { /* local */
2745                   if (gIndFine < rowStart || gIndFine >= rowEnd) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Mismatched number of constrained dofs");
2746                   dnnz[gIndFine - rowStart] = 1;
2747                 }
2748                 else if (gIndCoarse >= 0) { /* remote */
2749                   if (gIndFine < rowStart || gIndFine >= rowEnd) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Mismatched number of constrained dofs");
2750                   onnz[gIndFine - rowStart] = 1;
2751                 }
2752                 else { /* constrained */
2753                   if (gIndFine >= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Mismatched number of constrained dofs");
2754                 }
2755               }
2756             }
2757           }
2758           else {
2759             PetscInt i;
2760             for (i = 0; i < gDof; i++) {
2761               PetscInt gIndCoarse = pInd[i];
2762               PetscInt gIndFine   = rowIndices[i];
2763               if (gIndCoarse >= colStart && gIndCoarse < colEnd) { /* local */
2764                 if (gIndFine < rowStart || gIndFine >= rowEnd) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Mismatched number of constrained dofs");
2765                 dnnz[gIndFine - rowStart] = 1;
2766               }
2767               else if (gIndCoarse >= 0) { /* remote */
2768                 if (gIndFine < rowStart || gIndFine >= rowEnd) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Mismatched number of constrained dofs");
2769                 onnz[gIndFine - rowStart] = 1;
2770               }
2771               else { /* constrained */
2772                 if (gIndFine >= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Mismatched number of constrained dofs");
2773               }
2774             }
2775           }
2776         }
2777         else { /* interpolate from all */
2778           if (numFields) {
2779             PetscInt f;
2780             for (f = 0; f < numFields; f++) {
2781               PetscInt numRows = offsets[f+1] - offsets[f], row;
2782               for (row = 0; row < numRows; row++) {
2783                 PetscInt gIndFine = rowIndices[offsets[f] + row];
2784                 if (gIndFine >= 0) {
2785                   if (gIndFine < rowStart || gIndFine >= rowEnd) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Mismatched number of constrained dofs");
2786                   dnnz[gIndFine - rowStart] = numD[f];
2787                   onnz[gIndFine - rowStart] = numO[f];
2788                 }
2789               }
2790             }
2791           }
2792           else {
2793             PetscInt i;
2794             for (i = 0; i < gDof; i++) {
2795               PetscInt gIndFine = rowIndices[i];
2796               if (gIndFine >= 0) {
2797                 if (gIndFine < rowStart || gIndFine >= rowEnd) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Mismatched number of constrained dofs");
2798                 dnnz[gIndFine - rowStart] = numD[0];
2799                 onnz[gIndFine - rowStart] = numO[0];
2800               }
2801             }
2802           }
2803         }
2804       }
2805       else { /* interpolate from all */
2806         if (numFields) {
2807           PetscInt f;
2808           for (f = 0; f < numFields; f++) {
2809             PetscInt numRows = offsets[f+1] - offsets[f], row;
2810             for (row = 0; row < numRows; row++) {
2811               PetscInt gIndFine = rowIndices[offsets[f] + row];
2812               if (gIndFine >= 0) {
2813                 if (gIndFine < rowStart || gIndFine >= rowEnd) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Mismatched number of constrained dofs");
2814                 dnnz[gIndFine - rowStart] = numD[f];
2815                 onnz[gIndFine - rowStart] = numO[f];
2816               }
2817             }
2818           }
2819         }
2820         else { /* every dof get a full row */
2821           PetscInt i;
2822           for (i = 0; i < gDof; i++) {
2823             PetscInt gIndFine = rowIndices[i];
2824             if (gIndFine >= 0) {
2825               if (gIndFine < rowStart || gIndFine >= rowEnd) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Mismatched number of constrained dofs");
2826               dnnz[gIndFine - rowStart] = numD[0];
2827               onnz[gIndFine - rowStart] = numO[0];
2828             }
2829           }
2830         }
2831       }
2832     }
2833     ierr = MatXAIJSetPreallocation(mat,1,dnnz,onnz,NULL,NULL);CHKERRQ(ierr);
2834     ierr = PetscFree2(dnnz,onnz);CHKERRQ(ierr);
2835 
2836     ierr = DMPlexGetReferenceTree(fine,&refTree);CHKERRQ(ierr);
2837     ierr = DMPlexReferenceTreeGetChildrenMatrices(refTree,&refPointFieldMats,&refPointFieldN);CHKERRQ(ierr);
2838     ierr = DMGetDefaultConstraints(refTree,&refConSec,NULL);CHKERRQ(ierr);
2839     ierr = DMPlexGetAnchors(refTree,&refAnSec,NULL);CHKERRQ(ierr);
2840     ierr = PetscSectionGetChart(refConSec,&pRefStart,&pRefEnd);CHKERRQ(ierr);
2841     ierr = PetscSectionGetMaxDof(refConSec,&maxConDof);CHKERRQ(ierr);
2842     ierr = PetscSectionGetMaxDof(leafIndicesSec,&maxColumns);CHKERRQ(ierr);
2843     ierr = PetscMalloc1(maxConDof*maxColumns,&pointWork);CHKERRQ(ierr);
2844     for (p = leafStart; p < leafEnd; p++) {
2845       PetscInt    gDof, gcDof, gOff;
2846       PetscInt    numColIndices, pIndOff, *pInd;
2847       PetscInt    matSize;
2848       PetscInt    childId;
2849 
2850 
2851       ierr = PetscSectionGetDof(globalFine,p,&gDof);CHKERRQ(ierr);
2852       ierr = PetscSectionGetConstraintDof(globalFine,p,&gcDof);CHKERRQ(ierr);
2853       if ((gDof - gcDof) <= 0) {
2854         continue;
2855       }
2856       childId = childIds[p-pStartF];
2857       ierr = PetscSectionGetOffset(globalFine,p,&gOff);CHKERRQ(ierr);
2858       ierr = PetscSectionGetDof(leafIndicesSec,p,&numColIndices);CHKERRQ(ierr);
2859       ierr = PetscSectionGetOffset(leafIndicesSec,p,&pIndOff);CHKERRQ(ierr);
2860       numColIndices -= 2 * numFields;
2861       pInd = &leafIndices[pIndOff];
2862       offsets[0]        = 0;
2863       offsetsCopy[0]    = 0;
2864       newOffsets[0]     = 0;
2865       newOffsetsCopy[0] = 0;
2866       rowOffsets[0]     = 0;
2867       if (numFields) {
2868         PetscInt f;
2869         for (f = 0; f < numFields; f++) {
2870           PetscInt rowDof;
2871 
2872           ierr = PetscSectionGetFieldDof(localFine,p,f,&rowDof);CHKERRQ(ierr);
2873           offsets[f + 1]     = offsets[f] + rowDof;
2874           offsetsCopy[f + 1] = offsets[f + 1];
2875           rowOffsets[f + 1]  = pInd[numColIndices + f];
2876           newOffsets[f + 1]  = pInd[numColIndices + numFields + f];
2877         }
2878         ierr = DMPlexGetIndicesPointFields_Internal(localFine,p,gOff,offsetsCopy,PETSC_FALSE,0,rowIndices);CHKERRQ(ierr);
2879       }
2880       else {
2881         ierr = DMPlexGetIndicesPoint_Internal(localFine,p,gOff,offsetsCopy,PETSC_FALSE,0,rowIndices);CHKERRQ(ierr);
2882       }
2883       ierr = PetscSectionGetDof(leafMatricesSec,p,&matSize);CHKERRQ(ierr);
2884       if (!matSize) { /* incoming matrix is identity */
2885         if (childId < 0) { /* no child interpolation: scatter */
2886           if (numFields) {
2887             PetscInt f;
2888             for (f = 0; f < numFields; f++) {
2889               PetscInt numRows = offsets[f+1] - offsets[f], row;
2890               for (row = 0; row < numRows; row++) {
2891                 ierr = MatSetValue(mat,rowIndices[offsets[f]+row],pInd[newOffsets[f]+row],1.,INSERT_VALUES);CHKERRQ(ierr);
2892               }
2893             }
2894           }
2895           else {
2896             PetscInt numRows = gDof, row;
2897             for (row = 0; row < numRows; row++) {
2898               ierr = MatSetValue(mat,rowIndices[row],pInd[row],1.,INSERT_VALUES);CHKERRQ(ierr);
2899             }
2900           }
2901         }
2902         else { /* interpolate from all */
2903           if (numFields) {
2904             PetscInt f;
2905             for (f = 0; f < numFields; f++) {
2906               PetscInt numRows = offsets[f+1] - offsets[f];
2907               PetscInt numCols = newOffsets[f + 1] - newOffsets[f];
2908               ierr = MatSetValues(mat,numRows,&rowIndices[offsets[f]],numCols,&pInd[newOffsets[f]],refPointFieldMats[childId - pRefStart][f],INSERT_VALUES);CHKERRQ(ierr);
2909             }
2910           }
2911           else {
2912             ierr = MatSetValues(mat,gDof,rowIndices,numColIndices,pInd,refPointFieldMats[childId - pRefStart][0],INSERT_VALUES);CHKERRQ(ierr);
2913           }
2914         }
2915       }
2916       else { /* interpolate from all */
2917         PetscInt    pMatOff;
2918         PetscScalar *pMat;
2919 
2920         ierr = PetscSectionGetOffset(leafMatricesSec,p,&pMatOff);CHKERRQ(ierr);
2921         pMat = &leafMatrices[pMatOff];
2922         if (childId < 0) { /* copy the incoming matrix */
2923           if (numFields) {
2924             PetscInt f, count;
2925             for (f = 0, count = 0; f < numFields; f++) {
2926               PetscInt numRows = offsets[f+1]-offsets[f];
2927               PetscInt numCols = newOffsets[f+1]-newOffsets[f];
2928               PetscInt numInRows = rowOffsets[f+1]-rowOffsets[f];
2929               PetscScalar *inMat = &pMat[count];
2930 
2931               ierr = MatSetValues(mat,numRows,&rowIndices[offsets[f]],numCols,&pInd[newOffsets[f]],inMat,INSERT_VALUES);CHKERRQ(ierr);
2932               count += numCols * numInRows;
2933             }
2934           }
2935           else {
2936             ierr = MatSetValues(mat,gDof,rowIndices,numColIndices,pInd,pMat,INSERT_VALUES);CHKERRQ(ierr);
2937           }
2938         }
2939         else { /* multiply the incoming matrix by the child interpolation */
2940           if (numFields) {
2941             PetscInt f, count;
2942             for (f = 0, count = 0; f < numFields; f++) {
2943               PetscInt numRows = offsets[f+1]-offsets[f];
2944               PetscInt numCols = newOffsets[f+1]-newOffsets[f];
2945               PetscInt numInRows = rowOffsets[f+1]-rowOffsets[f];
2946               PetscScalar *inMat = &pMat[count];
2947               PetscInt i, j, k;
2948               if (refPointFieldN[childId - pRefStart][f] != numInRows) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Point constraint matrix multiply dimension mismatch");
2949               for (i = 0; i < numRows; i++) {
2950                 for (j = 0; j < numCols; j++) {
2951                   PetscScalar val = 0.;
2952                   for (k = 0; k < numInRows; k++) {
2953                     val += refPointFieldMats[childId - pRefStart][f][i * numInRows + k] * inMat[k * numCols + j];
2954                   }
2955                   pointWork[i * numCols + j] = val;
2956                 }
2957               }
2958               ierr = MatSetValues(mat,numRows,&rowIndices[offsets[f]],numCols,&pInd[newOffsets[f]],pointWork,INSERT_VALUES);CHKERRQ(ierr);
2959               count += numCols * numInRows;
2960             }
2961           }
2962           else { /* every dof gets a full row */
2963             PetscInt numRows   = gDof;
2964             PetscInt numCols   = numColIndices;
2965             PetscInt numInRows = matSize / numColIndices;
2966             PetscInt i, j, k;
2967             if (refPointFieldN[childId - pRefStart][0] != numInRows) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Point constraint matrix multiply dimension mismatch");
2968             for (i = 0; i < numRows; i++) {
2969               for (j = 0; j < numCols; j++) {
2970                 PetscScalar val = 0.;
2971                 for (k = 0; k < numInRows; k++) {
2972                   val += refPointFieldMats[childId - pRefStart][0][i * numInRows + k] * pMat[k * numCols + j];
2973                 }
2974                 pointWork[i * numCols + j] = val;
2975               }
2976             }
2977             ierr = MatSetValues(mat,numRows,rowIndices,numCols,pInd,pointWork,INSERT_VALUES);CHKERRQ(ierr);
2978           }
2979         }
2980       }
2981     }
2982     ierr = DMPlexReferenceTreeRestoreChildrenMatrices(refTree,&refPointFieldMats,&refPointFieldN);CHKERRQ(ierr);
2983     ierr = DMRestoreWorkArray(fine,maxDof,PETSC_INT,&rowIndices);CHKERRQ(ierr);
2984     ierr = PetscFree(pointWork);CHKERRQ(ierr);
2985   }
2986   ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2987   ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2988   ierr = PetscSectionDestroy(&leafIndicesSec);CHKERRQ(ierr);
2989   ierr = PetscSectionDestroy(&leafMatricesSec);CHKERRQ(ierr);
2990   ierr = PetscFree2(leafIndices,leafMatrices);CHKERRQ(ierr);
2991   ierr = PetscFree7(offsets,offsetsCopy,newOffsets,newOffsetsCopy,rowOffsets,numD,numO);CHKERRQ(ierr);
2992   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
2993   PetscFunctionReturn(0);
2994 }
2995 
2996 #undef __FUNCT__
2997 #define __FUNCT__ "DMPlexComputeInjectorReferenceTree"
2998 /*
2999  * Assuming a nodal basis (w.r.t. the dual basis) basis:
3000  *
3001  * for each coarse dof \phi^c_i:
3002  *   for each quadrature point (w_l,x_l) in the dual basis definition of \phi^c_i:
3003  *     for each fine dof \phi^f_j;
3004  *       a_{i,j} = 0;
3005  *       for each fine dof \phi^f_k:
3006  *         a_{i,j} += interp_{i,k} * \phi^f_k(x_l) * \phi^f_j(x_l) * w_l
3007  *                    [^^^ this is = \phi^c_i ^^^]
3008  */
3009 PetscErrorCode DMPlexComputeInjectorReferenceTree(DM refTree, Mat *inj)
3010 {
3011   PetscDS        ds;
3012   PetscSection   section, cSection;
3013   DMLabel        canonical, depth;
3014   Mat            cMat, mat;
3015   PetscInt       *nnz;
3016   PetscInt       f, dim, numFields, numSecFields, p, pStart, pEnd, cStart, cEnd;
3017   PetscInt       m, n;
3018   PetscScalar    *pointScalar;
3019   PetscReal      *v0, *v0parent, *vtmp, *J, *Jparent, *invJ, *pointRef, detJ, detJparent;
3020   PetscErrorCode ierr;
3021 
3022   PetscFunctionBegin;
3023   ierr = DMGetDefaultSection(refTree,&section);CHKERRQ(ierr);
3024   ierr = DMGetDimension(refTree, &dim);CHKERRQ(ierr);
3025   ierr = PetscMalloc6(dim,&v0,dim,&v0parent,dim,&vtmp,dim*dim,&J,dim*dim,&Jparent,dim*dim,&invJ);CHKERRQ(ierr);
3026   ierr = PetscMalloc2(dim,&pointScalar,dim,&pointRef);CHKERRQ(ierr);
3027   ierr = DMGetDS(refTree,&ds);CHKERRQ(ierr);
3028   ierr = PetscDSGetNumFields(ds,&numFields);CHKERRQ(ierr);
3029   ierr = PetscSectionGetNumFields(section,&numSecFields);CHKERRQ(ierr);
3030   ierr = DMGetLabel(refTree,"canonical",&canonical);CHKERRQ(ierr);
3031   ierr = DMGetLabel(refTree,"depth",&depth);CHKERRQ(ierr);
3032   ierr = DMGetDefaultConstraints(refTree,&cSection,&cMat);CHKERRQ(ierr);
3033   ierr = DMPlexGetChart(refTree, &pStart, &pEnd);CHKERRQ(ierr);
3034   ierr = DMPlexGetHeightStratum(refTree, 0, &cStart, &cEnd);CHKERRQ(ierr);
3035   ierr = MatGetSize(cMat,&n,&m);CHKERRQ(ierr); /* the injector has transpose sizes from the constraint matrix */
3036   /* Step 1: compute non-zero pattern.  A proper subset of constraint matrix non-zero */
3037   ierr = PetscCalloc1(m,&nnz);CHKERRQ(ierr);
3038   for (p = pStart; p < pEnd; p++) { /* a point will have non-zeros if it is canonical, it has dofs, and its children have dofs */
3039     const PetscInt *children;
3040     PetscInt numChildren;
3041     PetscInt i, numChildDof, numSelfDof;
3042 
3043     if (canonical) {
3044       PetscInt pCanonical;
3045       ierr = DMLabelGetValue(canonical,p,&pCanonical);CHKERRQ(ierr);
3046       if (p != pCanonical) continue;
3047     }
3048     ierr = DMPlexGetTreeChildren(refTree,p,&numChildren,&children);CHKERRQ(ierr);
3049     if (!numChildren) continue;
3050     for (i = 0, numChildDof = 0; i < numChildren; i++) {
3051       PetscInt child = children[i];
3052       PetscInt dof;
3053 
3054       ierr = PetscSectionGetDof(section,child,&dof);CHKERRQ(ierr);
3055       numChildDof += dof;
3056     }
3057     ierr = PetscSectionGetDof(section,p,&numSelfDof);CHKERRQ(ierr);
3058     if (!numChildDof || !numSelfDof) continue;
3059     for (f = 0; f < numFields; f++) {
3060       PetscInt selfOff;
3061 
3062       if (numSecFields) { /* count the dofs for just this field */
3063         for (i = 0, numChildDof = 0; i < numChildren; i++) {
3064           PetscInt child = children[i];
3065           PetscInt dof;
3066 
3067           ierr = PetscSectionGetFieldDof(section,child,f,&dof);CHKERRQ(ierr);
3068           numChildDof += dof;
3069         }
3070         ierr = PetscSectionGetFieldDof(section,p,f,&numSelfDof);CHKERRQ(ierr);
3071         ierr = PetscSectionGetFieldOffset(section,p,f,&selfOff);CHKERRQ(ierr);
3072       }
3073       else {
3074         ierr = PetscSectionGetOffset(section,p,&selfOff);CHKERRQ(ierr);
3075       }
3076       for (i = 0; i < numSelfDof; i++) {
3077         nnz[selfOff + i] = numChildDof;
3078       }
3079     }
3080   }
3081   ierr = MatCreateAIJ(PETSC_COMM_SELF,m,n,m,n,-1,nnz,-1,NULL,&mat);CHKERRQ(ierr);
3082   ierr = PetscFree(nnz);CHKERRQ(ierr);
3083   /* Setp 2: compute entries */
3084   for (p = pStart; p < pEnd; p++) {
3085     const PetscInt *children;
3086     PetscInt numChildren;
3087     PetscInt i, numChildDof, numSelfDof;
3088 
3089     /* same conditions about when entries occur */
3090     if (canonical) {
3091       PetscInt pCanonical;
3092       ierr = DMLabelGetValue(canonical,p,&pCanonical);CHKERRQ(ierr);
3093       if (p != pCanonical) continue;
3094     }
3095     ierr = DMPlexGetTreeChildren(refTree,p,&numChildren,&children);CHKERRQ(ierr);
3096     if (!numChildren) continue;
3097     for (i = 0, numChildDof = 0; i < numChildren; i++) {
3098       PetscInt child = children[i];
3099       PetscInt dof;
3100 
3101       ierr = PetscSectionGetDof(section,child,&dof);CHKERRQ(ierr);
3102       numChildDof += dof;
3103     }
3104     ierr = PetscSectionGetDof(section,p,&numSelfDof);CHKERRQ(ierr);
3105     if (!numChildDof || !numSelfDof) continue;
3106 
3107     for (f = 0; f < numFields; f++) {
3108       PetscInt       selfOff, fComp, numSelfShapes, numChildShapes, parentCell;
3109       PetscInt       cellShapeOff;
3110       PetscObject    disc;
3111       PetscDualSpace dsp;
3112       PetscClassId   classId;
3113       PetscScalar    *pointMat;
3114       PetscInt       *matRows, *matCols;
3115       PetscInt       pO = PETSC_MIN_INT;
3116       const PetscInt *depthNumDof;
3117 
3118       if (numSecFields) {
3119         for (i = 0, numChildDof = 0; i < numChildren; i++) {
3120           PetscInt child = children[i];
3121           PetscInt dof;
3122 
3123           ierr = PetscSectionGetFieldDof(section,child,f,&dof);CHKERRQ(ierr);
3124           numChildDof += dof;
3125         }
3126         ierr = PetscSectionGetFieldDof(section,p,f,&numSelfDof);CHKERRQ(ierr);
3127         ierr = PetscSectionGetFieldOffset(section,p,f,&selfOff);CHKERRQ(ierr);
3128         ierr = PetscSectionGetFieldComponents(section,f,&fComp);CHKERRQ(ierr);
3129       }
3130       else {
3131         ierr = PetscSectionGetOffset(section,p,&selfOff);CHKERRQ(ierr);
3132         fComp = 1;
3133       }
3134       numSelfShapes  = numSelfDof  / fComp;
3135       numChildShapes = numChildDof / fComp;
3136 
3137       /* find a cell whose closure contains p */
3138       if (p >= cStart && p < cEnd) {
3139         parentCell = p;
3140       }
3141       else {
3142         PetscInt *star = NULL;
3143         PetscInt numStar;
3144 
3145         parentCell = -1;
3146         ierr = DMPlexGetTransitiveClosure(refTree,p,PETSC_FALSE,&numStar,&star);CHKERRQ(ierr);
3147         for (i = numStar - 1; i >= 0; i--) {
3148           PetscInt c = star[2 * i];
3149 
3150           if (c >= cStart && c < cEnd) {
3151             parentCell = c;
3152             break;
3153           }
3154         }
3155         ierr = DMPlexRestoreTransitiveClosure(refTree,p,PETSC_FALSE,&numStar,&star);CHKERRQ(ierr);
3156       }
3157       /* determine the offset of p's shape functions withing parentCell's shape functions */
3158       ierr = PetscDSGetDiscretization(ds,f,&disc);CHKERRQ(ierr);
3159       ierr = PetscObjectGetClassId(disc,&classId);CHKERRQ(ierr);
3160       if (classId == PETSCFE_CLASSID) {
3161         ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
3162       }
3163       else if (classId == PETSCFV_CLASSID) {
3164         ierr = PetscFVGetDualSpace((PetscFV)disc,&dsp);CHKERRQ(ierr);
3165       }
3166       else {
3167         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported discretization object");CHKERRQ(ierr);
3168       }
3169       ierr = PetscDualSpaceGetNumDof(dsp,&depthNumDof);CHKERRQ(ierr);
3170       {
3171         PetscInt *closure = NULL;
3172         PetscInt numClosure;
3173 
3174         ierr = DMPlexGetTransitiveClosure(refTree,parentCell,PETSC_TRUE,&numClosure,&closure);CHKERRQ(ierr);
3175         for (i = 0, cellShapeOff = 0; i < numClosure; i++) {
3176           PetscInt point = closure[2 * i], pointDepth;
3177 
3178           pO = closure[2 * i + 1];
3179           if (point == p) break;
3180           ierr = DMLabelGetValue(depth,point,&pointDepth);CHKERRQ(ierr);
3181           cellShapeOff += depthNumDof[pointDepth];
3182         }
3183         ierr = DMPlexRestoreTransitiveClosure(refTree,parentCell,PETSC_TRUE,&numClosure,&closure);CHKERRQ(ierr);
3184       }
3185 
3186       ierr = DMGetWorkArray(refTree, numSelfShapes * numChildShapes, PETSC_SCALAR,&pointMat);CHKERRQ(ierr);
3187       ierr = DMGetWorkArray(refTree, numSelfShapes + numChildShapes, PETSC_INT,&matRows);CHKERRQ(ierr);
3188       matCols = matRows + numSelfShapes;
3189       for (i = 0; i < numSelfShapes; i++) {
3190         matRows[i] = selfOff + i * fComp;
3191       }
3192       {
3193         PetscInt colOff = 0;
3194 
3195         for (i = 0; i < numChildren; i++) {
3196           PetscInt child = children[i];
3197           PetscInt dof, off, j;
3198 
3199           if (numSecFields) {
3200             ierr = PetscSectionGetFieldDof(cSection,child,f,&dof);CHKERRQ(ierr);
3201             ierr = PetscSectionGetFieldOffset(cSection,child,f,&off);CHKERRQ(ierr);
3202           }
3203           else {
3204             ierr = PetscSectionGetDof(cSection,child,&dof);CHKERRQ(ierr);
3205             ierr = PetscSectionGetOffset(cSection,child,&off);CHKERRQ(ierr);
3206           }
3207 
3208           for (j = 0; j < dof / fComp; j++) {
3209             matCols[colOff++] = off + j * fComp;
3210           }
3211         }
3212       }
3213       if (classId == PETSCFE_CLASSID) {
3214         PetscFE        fe = (PetscFE) disc;
3215         PetscInt       fSize;
3216 
3217         ierr = PetscFEGetDualSpace(fe,&dsp);CHKERRQ(ierr);
3218         ierr = PetscDualSpaceGetDimension(dsp,&fSize);CHKERRQ(ierr);
3219         for (i = 0; i < numSelfShapes; i++) { /* for every shape function */
3220           PetscQuadrature q;
3221           PetscInt        dim, numPoints, j, k;
3222           const PetscReal *points;
3223           const PetscReal *weights;
3224           PetscInt        *closure = NULL;
3225           PetscInt        numClosure;
3226           PetscInt        parentCellShapeDof = cellShapeOff + (pO < 0 ? (numSelfShapes - 1 - i) : i);
3227           PetscReal       *Bparent;
3228 
3229           ierr = PetscDualSpaceGetFunctional(dsp,parentCellShapeDof,&q);CHKERRQ(ierr);
3230           ierr = PetscQuadratureGetData(q,&dim,&numPoints,&points,&weights);CHKERRQ(ierr);
3231           ierr = PetscFEGetTabulation(fe,numPoints,points,&Bparent,NULL,NULL);CHKERRQ(ierr); /* I'm expecting a nodal basis: weights[:]' * Bparent[:,cellShapeDof] = 1. */
3232           for (k = 0; k < numChildShapes; k++) {
3233             pointMat[numChildShapes * i + k] = 0.;
3234           }
3235           for (j = 0; j < numPoints; j++) {
3236             PetscInt          childCell = -1;
3237             PetscReal         parentValAtPoint;
3238             const PetscReal   *pointReal = &points[dim * j];
3239             const PetscScalar *point;
3240             PetscReal         *Bchild;
3241             PetscInt          childCellShapeOff, pointMatOff;
3242 #if defined(PETSC_USE_COMPLEX)
3243             PetscInt          d;
3244 
3245             for (d = 0; d < dim; d++) {
3246               pointScalar[d] = points[dim * j + d];
3247             }
3248             point = pointScalar;
3249 #else
3250             point = pointReal;
3251 #endif
3252 
3253             parentValAtPoint = Bparent[(fSize * j + parentCellShapeDof) * fComp];
3254 
3255             for (k = 0; k < numChildren; k++) { /* locate the point in a child's star cell*/
3256               PetscInt child = children[k];
3257               PetscInt *star = NULL;
3258               PetscInt numStar, s;
3259 
3260               ierr = DMPlexGetTransitiveClosure(refTree,child,PETSC_FALSE,&numStar,&star);CHKERRQ(ierr);
3261               for (s = numStar - 1; s >= 0; s--) {
3262                 PetscInt c = star[2 * s];
3263 
3264                 if (c < cStart || c >= cEnd) continue;
3265                 ierr = DMPlexLocatePoint_Internal(refTree,dim,point,c,&childCell);CHKERRQ(ierr);
3266                 if (childCell >= 0) break;
3267               }
3268               ierr = DMPlexRestoreTransitiveClosure(refTree,child,PETSC_FALSE,&numStar,&star);CHKERRQ(ierr);
3269               if (childCell >= 0) break;
3270             }
3271             if (childCell < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Could not locate quadrature point");CHKERRQ(ierr);
3272             ierr = DMPlexComputeCellGeometryFEM(refTree, childCell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
3273             ierr = DMPlexComputeCellGeometryFEM(refTree, parentCell, NULL, v0parent, Jparent, NULL, &detJparent);CHKERRQ(ierr);
3274             CoordinatesRefToReal(dim, dim, v0parent, Jparent, pointReal, vtmp);
3275             CoordinatesRealToRef(dim, dim, v0, invJ, vtmp, pointRef);
3276 
3277             ierr = PetscFEGetTabulation(fe,1,pointRef,&Bchild,NULL,NULL);CHKERRQ(ierr);
3278             ierr = DMPlexGetTransitiveClosure(refTree,childCell,PETSC_TRUE,&numClosure,&closure);CHKERRQ(ierr);
3279             for (k = 0, pointMatOff = 0; k < numChildren; k++) { /* point is located in cell => child dofs support at point are in closure of cell */
3280               PetscInt child = children[k], childDepth, childDof, childO = PETSC_MIN_INT;
3281               PetscInt l;
3282 
3283               ierr = DMLabelGetValue(depth,child,&childDepth);CHKERRQ(ierr);
3284               childDof = depthNumDof[childDepth];
3285               for (l = 0, childCellShapeOff = 0; l < numClosure; l++) {
3286                 PetscInt point = closure[2 * l];
3287                 PetscInt pointDepth;
3288 
3289                 childO = closure[2 * l + 1];
3290                 if (point == child) break;
3291                 ierr = DMLabelGetValue(depth,point,&pointDepth);CHKERRQ(ierr);
3292                 childCellShapeOff += depthNumDof[pointDepth];
3293               }
3294               if (l == numClosure) {
3295                 pointMatOff += childDof;
3296                 continue; /* child is not in the closure of the cell: has nothing to contribute to this point */
3297               }
3298               for (l = 0; l < childDof; l++) {
3299                 PetscInt    childCellDof    = childCellShapeOff + (childO ? (childDof - 1 - l) : l);
3300                 PetscReal   childValAtPoint = Bchild[childCellDof * fComp];
3301 
3302                 pointMat[i * numChildShapes + pointMatOff + l] += weights[j] * parentValAtPoint * childValAtPoint;
3303               }
3304               pointMatOff += childDof;
3305             }
3306             ierr = DMPlexRestoreTransitiveClosure(refTree,childCell,PETSC_TRUE,&numClosure,&closure);CHKERRQ(ierr);
3307             ierr = PetscFERestoreTabulation(fe,1,pointRef,&Bchild,NULL,NULL);CHKERRQ(ierr);
3308           }
3309           ierr = PetscFERestoreTabulation(fe,numPoints,points,&Bparent,NULL,NULL);CHKERRQ(ierr);
3310         }
3311       }
3312       else { /* just the volume-weighted averages of the children */
3313         PetscInt  childShapeOff;
3314         PetscReal parentVol;
3315 
3316         ierr = DMPlexComputeCellGeometryFVM(refTree, p, &parentVol, NULL, NULL);CHKERRQ(ierr);
3317         for (i = 0, childShapeOff = 0; i < numChildren; i++) {
3318           PetscInt  child = children[i];
3319           PetscReal childVol;
3320 
3321           if (child < cStart || child >= cEnd) continue;
3322           ierr = DMPlexComputeCellGeometryFVM(refTree, child, &childVol, NULL, NULL);CHKERRQ(ierr);
3323           pointMat[childShapeOff] = childVol / parentVol;
3324           childShapeOff++;
3325         }
3326       }
3327       /* Insert pointMat into mat */
3328       for (i = 0; i < fComp; i++) {
3329         PetscInt j;
3330         ierr = MatSetValues(mat,numSelfShapes,matRows,numChildShapes,matCols,pointMat,INSERT_VALUES);CHKERRQ(ierr);
3331 
3332         for (j = 0; j < numSelfShapes; j++) {
3333           matRows[j]++;
3334         }
3335         for (j = 0; j < numChildShapes; j++) {
3336           matCols[j]++;
3337         }
3338       }
3339       ierr = DMRestoreWorkArray(refTree, numSelfShapes + numChildShapes, PETSC_INT,&matRows);CHKERRQ(ierr);
3340       ierr = DMRestoreWorkArray(refTree, numSelfShapes * numChildShapes, PETSC_SCALAR,&pointMat);CHKERRQ(ierr);
3341     }
3342   }
3343   ierr = PetscFree6(v0,v0parent,vtmp,J,Jparent,invJ);CHKERRQ(ierr);
3344   ierr = PetscFree2(pointScalar,pointRef);CHKERRQ(ierr);
3345   ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3346   ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3347   *inj = mat;
3348   PetscFunctionReturn(0);
3349 }
3350 
3351 #undef __FUNCT__
3352 #define __FUNCT__ "DMPlexReferenceTreeGetChildrenMatrices_Injection"
3353 static PetscErrorCode DMPlexReferenceTreeGetChildrenMatrices_Injection(DM refTree, Mat inj, PetscScalar ****childrenMats)
3354 {
3355   PetscDS        ds;
3356   PetscInt       numFields, f, pRefStart, pRefEnd, p, *rows, *cols, maxDof;
3357   PetscScalar    ***refPointFieldMats;
3358   PetscSection   refConSec, refSection;
3359   PetscErrorCode ierr;
3360 
3361   PetscFunctionBegin;
3362   ierr = DMGetDS(refTree,&ds);CHKERRQ(ierr);
3363   ierr = PetscDSGetNumFields(ds,&numFields);CHKERRQ(ierr);
3364   ierr = DMGetDefaultConstraints(refTree,&refConSec,NULL);CHKERRQ(ierr);
3365   ierr = DMGetDefaultSection(refTree,&refSection);CHKERRQ(ierr);
3366   ierr = PetscSectionGetChart(refConSec,&pRefStart,&pRefEnd);CHKERRQ(ierr);
3367   ierr = PetscMalloc1(pRefEnd-pRefStart,&refPointFieldMats);CHKERRQ(ierr);
3368   ierr = PetscSectionGetMaxDof(refConSec,&maxDof);CHKERRQ(ierr);
3369   ierr = PetscMalloc1(maxDof,&rows);CHKERRQ(ierr);
3370   ierr = PetscMalloc1(maxDof*maxDof,&cols);CHKERRQ(ierr);
3371   for (p = pRefStart; p < pRefEnd; p++) {
3372     PetscInt parent, pDof, parentDof;
3373 
3374     ierr = DMPlexGetTreeParent(refTree,p,&parent,NULL);CHKERRQ(ierr);
3375     ierr = PetscSectionGetDof(refConSec,p,&pDof);CHKERRQ(ierr);
3376     ierr = PetscSectionGetDof(refSection,parent,&parentDof);CHKERRQ(ierr);
3377     if (!pDof || !parentDof || parent == p) continue;
3378 
3379     ierr = PetscMalloc1(numFields,&refPointFieldMats[p-pRefStart]);CHKERRQ(ierr);
3380     for (f = 0; f < numFields; f++) {
3381       PetscInt cDof, cOff, numCols, r, fComp;
3382       PetscObject disc;
3383       PetscClassId id;
3384       PetscFE fe = NULL;
3385       PetscFV fv = NULL;
3386 
3387       ierr = PetscDSGetDiscretization(ds,f,&disc);CHKERRQ(ierr);
3388       ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
3389       if (id == PETSCFE_CLASSID) {
3390         fe = (PetscFE) disc;
3391         ierr = PetscFEGetNumComponents(fe,&fComp);CHKERRQ(ierr);
3392       }
3393       else if (id == PETSCFV_CLASSID) {
3394         fv = (PetscFV) disc;
3395         ierr = PetscFVGetNumComponents(fv,&fComp);CHKERRQ(ierr);
3396       }
3397       else SETERRQ1(PetscObjectComm(disc),PETSC_ERR_ARG_UNKNOWN_TYPE, "PetscDS discretization id %d not recognized.", id);
3398 
3399       if (numFields > 1) {
3400         ierr = PetscSectionGetFieldDof(refConSec,p,f,&cDof);CHKERRQ(ierr);
3401         ierr = PetscSectionGetFieldOffset(refConSec,p,f,&cOff);CHKERRQ(ierr);
3402       }
3403       else {
3404         ierr = PetscSectionGetDof(refConSec,p,&cDof);CHKERRQ(ierr);
3405         ierr = PetscSectionGetOffset(refConSec,p,&cOff);CHKERRQ(ierr);
3406       }
3407 
3408       if (!cDof) continue;
3409       for (r = 0; r < cDof; r++) {
3410         rows[r] = cOff + r;
3411       }
3412       numCols = 0;
3413       {
3414         PetscInt aDof, aOff, j;
3415 
3416         if (numFields > 1) {
3417           ierr = PetscSectionGetFieldDof(refSection,parent,f,&aDof);CHKERRQ(ierr);
3418           ierr = PetscSectionGetFieldOffset(refSection,parent,f,&aOff);CHKERRQ(ierr);
3419         }
3420         else {
3421           ierr = PetscSectionGetDof(refSection,parent,&aDof);CHKERRQ(ierr);
3422           ierr = PetscSectionGetOffset(refSection,parent,&aOff);CHKERRQ(ierr);
3423         }
3424 
3425         for (j = 0; j < aDof; j++) {
3426           cols[numCols++] = aOff + j;
3427         }
3428       }
3429       ierr = PetscMalloc1(cDof*numCols,&refPointFieldMats[p-pRefStart][f]);CHKERRQ(ierr);
3430       /* transpose of constraint matrix */
3431       ierr = MatGetValues(inj,numCols,cols,cDof,rows,refPointFieldMats[p-pRefStart][f]);CHKERRQ(ierr);
3432     }
3433   }
3434   *childrenMats = refPointFieldMats;
3435   ierr = PetscFree(rows);CHKERRQ(ierr);
3436   ierr = PetscFree(cols);CHKERRQ(ierr);
3437   PetscFunctionReturn(0);
3438 }
3439 
3440 #undef __FUNCT__
3441 #define __FUNCT__ "DMPlexReferenceTreeRestoreChildrenMatrices_Injection"
3442 static PetscErrorCode DMPlexReferenceTreeRestoreChildrenMatrices_Injection(DM refTree, Mat inj, PetscScalar ****childrenMats)
3443 {
3444   PetscDS        ds;
3445   PetscScalar    ***refPointFieldMats;
3446   PetscInt       numFields, pRefStart, pRefEnd, p, f;
3447   PetscSection   refConSec, refSection;
3448   PetscErrorCode ierr;
3449 
3450   PetscFunctionBegin;
3451   refPointFieldMats = *childrenMats;
3452   *childrenMats = NULL;
3453   ierr = DMGetDS(refTree,&ds);CHKERRQ(ierr);
3454   ierr = DMGetDefaultSection(refTree,&refSection);CHKERRQ(ierr);
3455   ierr = PetscDSGetNumFields(ds,&numFields);CHKERRQ(ierr);
3456   ierr = DMGetDefaultConstraints(refTree,&refConSec,NULL);CHKERRQ(ierr);
3457   ierr = PetscSectionGetChart(refConSec,&pRefStart,&pRefEnd);CHKERRQ(ierr);
3458   for (p = pRefStart; p < pRefEnd; p++) {
3459     PetscInt parent, pDof, parentDof;
3460 
3461     ierr = DMPlexGetTreeParent(refTree,p,&parent,NULL);CHKERRQ(ierr);
3462     ierr = PetscSectionGetDof(refConSec,p,&pDof);CHKERRQ(ierr);
3463     ierr = PetscSectionGetDof(refSection,parent,&parentDof);CHKERRQ(ierr);
3464     if (!pDof || !parentDof || parent == p) continue;
3465 
3466     for (f = 0; f < numFields; f++) {
3467       PetscInt cDof;
3468 
3469       if (numFields > 1) {
3470         ierr = PetscSectionGetFieldDof(refConSec,p,f,&cDof);CHKERRQ(ierr);
3471       }
3472       else {
3473         ierr = PetscSectionGetDof(refConSec,p,&cDof);CHKERRQ(ierr);
3474       }
3475 
3476       if (!cDof) continue;
3477       ierr = PetscFree(refPointFieldMats[p - pRefStart][f]);CHKERRQ(ierr);
3478     }
3479     ierr = PetscFree(refPointFieldMats[p - pRefStart]);CHKERRQ(ierr);
3480   }
3481   ierr = PetscFree(refPointFieldMats);CHKERRQ(ierr);
3482   PetscFunctionReturn(0);
3483 }
3484 
3485 #undef __FUNCT__
3486 #define __FUNCT__ "DMPlexReferenceTreeGetInjector"
3487 static PetscErrorCode DMPlexReferenceTreeGetInjector(DM refTree,Mat *injRef)
3488 {
3489   Mat            cMatRef;
3490   PetscObject    injRefObj;
3491   PetscErrorCode ierr;
3492 
3493   PetscFunctionBegin;
3494   ierr = DMGetDefaultConstraints(refTree,NULL,&cMatRef);CHKERRQ(ierr);
3495   ierr = PetscObjectQuery((PetscObject)cMatRef,"DMPlexComputeInjectorTree_refTree",&injRefObj);CHKERRQ(ierr);
3496   *injRef = (Mat) injRefObj;
3497   if (!*injRef) {
3498     ierr = DMPlexComputeInjectorReferenceTree(refTree,injRef);CHKERRQ(ierr);
3499     ierr = PetscObjectCompose((PetscObject)cMatRef,"DMPlexComputeInjectorTree_refTree",(PetscObject)*injRef);CHKERRQ(ierr);
3500     /* there is now a reference in cMatRef, which should be the only one for symmetry with the above case */
3501     ierr = PetscObjectDereference((PetscObject)*injRef);CHKERRQ(ierr);
3502   }
3503   PetscFunctionReturn(0);
3504 }
3505 
3506 #undef __FUNCT__
3507 #define __FUNCT__ "DMPlexTransferInjectorTree"
3508 static PetscErrorCode DMPlexTransferInjectorTree(DM coarse, DM fine, PetscSF coarseToFine, const PetscInt *childIds, Vec fineVec, PetscInt numFields, PetscInt *offsets, PetscSection *rootMultiSec, PetscSection *multiLeafSec, PetscInt **gatheredIndices, PetscScalar **gatheredValues)
3509 {
3510   PetscInt       pStartF, pEndF, pStartC, pEndC, p, maxDof, numMulti;
3511   PetscSection   globalCoarse, globalFine;
3512   PetscSection   localCoarse, localFine, leafIndicesSec;
3513   PetscSection   multiRootSec, rootIndicesSec;
3514   PetscInt       *leafInds, *rootInds = NULL;
3515   const PetscInt *rootDegrees;
3516   PetscScalar    *leafVals = NULL, *rootVals = NULL;
3517   PetscSF        coarseToFineEmbedded;
3518   PetscErrorCode ierr;
3519 
3520   PetscFunctionBegin;
3521   ierr = DMPlexGetChart(coarse,&pStartC,&pEndC);CHKERRQ(ierr);
3522   ierr = DMPlexGetChart(fine,&pStartF,&pEndF);CHKERRQ(ierr);
3523   ierr = DMGetDefaultSection(fine,&localFine);CHKERRQ(ierr);
3524   ierr = DMGetDefaultGlobalSection(fine,&globalFine);CHKERRQ(ierr);
3525   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)fine),&leafIndicesSec);CHKERRQ(ierr);
3526   ierr = PetscSectionSetChart(leafIndicesSec,pStartF, pEndF);CHKERRQ(ierr);
3527   ierr = PetscSectionGetMaxDof(localFine,&maxDof);CHKERRQ(ierr);
3528   { /* winnow fine points that don't have global dofs out of the sf */
3529     PetscInt l, nleaves, dof, cdof, numPointsWithDofs, offset, *pointsWithDofs, numIndices;
3530     const PetscInt *leaves;
3531 
3532     ierr = PetscSFGetGraph(coarseToFine,NULL,&nleaves,&leaves,NULL);CHKERRQ(ierr);
3533     for (l = 0, numPointsWithDofs = 0; l < nleaves; l++) {
3534       p    = leaves ? leaves[l] : l;
3535       ierr = PetscSectionGetDof(globalFine,p,&dof);CHKERRQ(ierr);
3536       ierr = PetscSectionGetConstraintDof(globalFine,p,&cdof);CHKERRQ(ierr);
3537       if ((dof - cdof) > 0) {
3538         numPointsWithDofs++;
3539 
3540         ierr = PetscSectionGetDof(localFine,p,&dof);CHKERRQ(ierr);
3541         ierr = PetscSectionSetDof(leafIndicesSec,p,dof + 1);CHKERRQ(ierr);
3542       }
3543     }
3544     ierr = PetscMalloc1(numPointsWithDofs,&pointsWithDofs);CHKERRQ(ierr);
3545     ierr = PetscSectionSetUp(leafIndicesSec);CHKERRQ(ierr);
3546     ierr = PetscSectionGetStorageSize(leafIndicesSec,&numIndices);CHKERRQ(ierr);
3547     ierr = PetscMalloc1(gatheredIndices ? numIndices : (maxDof + 1),&leafInds);CHKERRQ(ierr);
3548     if (gatheredValues)  {ierr = PetscMalloc1(numIndices,&leafVals);CHKERRQ(ierr);}
3549     for (l = 0, offset = 0; l < nleaves; l++) {
3550       p    = leaves ? leaves[l] : l;
3551       ierr = PetscSectionGetDof(globalFine,p,&dof);CHKERRQ(ierr);
3552       ierr = PetscSectionGetConstraintDof(globalFine,p,&cdof);CHKERRQ(ierr);
3553       if ((dof - cdof) > 0) {
3554         PetscInt    off, gOff;
3555         PetscInt    *pInd;
3556         PetscScalar *pVal = NULL;
3557 
3558         pointsWithDofs[offset++] = l;
3559 
3560         ierr = PetscSectionGetOffset(leafIndicesSec,p,&off);CHKERRQ(ierr);
3561 
3562         pInd = gatheredIndices ? (&leafInds[off + 1]) : leafInds;
3563         if (gatheredValues) {
3564           PetscInt i;
3565 
3566           pVal = &leafVals[off + 1];
3567           for (i = 0; i < dof; i++) pVal[i] = 0.;
3568         }
3569         ierr = PetscSectionGetOffset(globalFine,p,&gOff);CHKERRQ(ierr);
3570 
3571         offsets[0] = 0;
3572         if (numFields) {
3573           PetscInt f;
3574 
3575           for (f = 0; f < numFields; f++) {
3576             PetscInt fDof;
3577             ierr = PetscSectionGetFieldDof(localFine,p,f,&fDof);CHKERRQ(ierr);
3578             offsets[f + 1] = fDof + offsets[f];
3579           }
3580           DMPlexGetIndicesPointFields_Internal(localFine,p,gOff < 0 ? -(gOff + 1) : gOff,offsets,PETSC_FALSE,0,pInd);CHKERRQ(ierr);
3581         }
3582         else {
3583           DMPlexGetIndicesPoint_Internal(localFine,p,gOff < 0 ? -(gOff + 1) : gOff,offsets,PETSC_FALSE,0,pInd);CHKERRQ(ierr);
3584         }
3585         if (gatheredValues) {ierr = VecGetValues(fineVec,dof,pInd,pVal);CHKERRQ(ierr);}
3586       }
3587     }
3588     ierr = PetscSFCreateEmbeddedLeafSF(coarseToFine, numPointsWithDofs, pointsWithDofs, &coarseToFineEmbedded);CHKERRQ(ierr);
3589     ierr = PetscFree(pointsWithDofs);CHKERRQ(ierr);
3590   }
3591 
3592   ierr = DMPlexGetChart(coarse,&pStartC,&pEndC);CHKERRQ(ierr);
3593   ierr = DMGetDefaultSection(coarse,&localCoarse);CHKERRQ(ierr);
3594   ierr = DMGetDefaultGlobalSection(coarse,&globalCoarse);CHKERRQ(ierr);
3595 
3596   { /* there may be the case where an sf root has a parent: broadcast parents back to children */
3597     MPI_Datatype threeInt;
3598     PetscMPIInt  rank;
3599     PetscInt     (*parentNodeAndIdCoarse)[3];
3600     PetscInt     (*parentNodeAndIdFine)[3];
3601     PetscInt     p, nleaves, nleavesToParents;
3602     PetscSF      pointSF, sfToParents;
3603     const PetscInt *ilocal;
3604     const PetscSFNode *iremote;
3605     PetscSFNode  *iremoteToParents;
3606     PetscInt     *ilocalToParents;
3607 
3608     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)coarse),&rank);CHKERRQ(ierr);
3609     ierr = MPI_Type_contiguous(3,MPIU_INT,&threeInt);CHKERRQ(ierr);
3610     ierr = MPI_Type_commit(&threeInt);CHKERRQ(ierr);
3611     ierr = PetscMalloc2(pEndC-pStartC,&parentNodeAndIdCoarse,pEndF-pStartF,&parentNodeAndIdFine);CHKERRQ(ierr);
3612     ierr = DMGetPointSF(coarse,&pointSF);CHKERRQ(ierr);
3613     ierr = PetscSFGetGraph(pointSF,NULL,&nleaves,&ilocal,&iremote);CHKERRQ(ierr);
3614     for (p = pStartC; p < pEndC; p++) {
3615       PetscInt parent, childId;
3616       ierr = DMPlexGetTreeParent(coarse,p,&parent,&childId);CHKERRQ(ierr);
3617       parentNodeAndIdCoarse[p - pStartC][0] = rank;
3618       parentNodeAndIdCoarse[p - pStartC][1] = parent - pStartC;
3619       parentNodeAndIdCoarse[p - pStartC][2] = (p == parent) ? -1 : childId;
3620       if (nleaves > 0) {
3621         PetscInt leaf = -1;
3622 
3623         if (ilocal) {
3624           ierr  = PetscFindInt(parent,nleaves,ilocal,&leaf);CHKERRQ(ierr);
3625         }
3626         else {
3627           leaf = p - pStartC;
3628         }
3629         if (leaf >= 0) {
3630           parentNodeAndIdCoarse[p - pStartC][0] = iremote[leaf].rank;
3631           parentNodeAndIdCoarse[p - pStartC][1] = iremote[leaf].index;
3632         }
3633       }
3634     }
3635     for (p = pStartF; p < pEndF; p++) {
3636       parentNodeAndIdFine[p - pStartF][0] = -1;
3637       parentNodeAndIdFine[p - pStartF][1] = -1;
3638       parentNodeAndIdFine[p - pStartF][2] = -1;
3639     }
3640     ierr = PetscSFBcastBegin(coarseToFineEmbedded,threeInt,parentNodeAndIdCoarse,parentNodeAndIdFine);CHKERRQ(ierr);
3641     ierr = PetscSFBcastEnd(coarseToFineEmbedded,threeInt,parentNodeAndIdCoarse,parentNodeAndIdFine);CHKERRQ(ierr);
3642     for (p = pStartF, nleavesToParents = 0; p < pEndF; p++) {
3643       PetscInt dof;
3644 
3645       ierr = PetscSectionGetDof(leafIndicesSec,p,&dof);CHKERRQ(ierr);
3646       if (dof) {
3647         PetscInt off;
3648 
3649         ierr = PetscSectionGetOffset(leafIndicesSec,p,&off);CHKERRQ(ierr);
3650         if (gatheredIndices) {
3651           leafInds[off] = PetscMax(childIds[p-pStartF],parentNodeAndIdFine[p-pStartF][2]);
3652         } else if (gatheredValues) {
3653           leafVals[off] = (PetscScalar) PetscMax(childIds[p-pStartF],parentNodeAndIdFine[p-pStartF][2]);
3654         }
3655       }
3656       if (parentNodeAndIdFine[p-pStartF][0] >= 0) {
3657         nleavesToParents++;
3658       }
3659     }
3660     ierr = PetscMalloc1(nleavesToParents,&ilocalToParents);CHKERRQ(ierr);
3661     ierr = PetscMalloc1(nleavesToParents,&iremoteToParents);CHKERRQ(ierr);
3662     for (p = pStartF, nleavesToParents = 0; p < pEndF; p++) {
3663       if (parentNodeAndIdFine[p-pStartF][0] >= 0) {
3664         ilocalToParents[nleavesToParents] = p - pStartF;
3665         iremoteToParents[nleavesToParents].rank  = parentNodeAndIdFine[p-pStartF][0];
3666         iremoteToParents[nleavesToParents].index = parentNodeAndIdFine[p-pStartF][1];
3667         nleavesToParents++;
3668       }
3669     }
3670     ierr = PetscSFCreate(PetscObjectComm((PetscObject)coarse),&sfToParents);CHKERRQ(ierr);
3671     ierr = PetscSFSetGraph(sfToParents,pEndC-pStartC,nleavesToParents,ilocalToParents,PETSC_OWN_POINTER,iremoteToParents,PETSC_OWN_POINTER);CHKERRQ(ierr);
3672     ierr = PetscSFDestroy(&coarseToFineEmbedded);CHKERRQ(ierr);
3673 
3674     coarseToFineEmbedded = sfToParents;
3675 
3676     ierr = PetscFree2(parentNodeAndIdCoarse,parentNodeAndIdFine);CHKERRQ(ierr);
3677     ierr = MPI_Type_free(&threeInt);CHKERRQ(ierr);
3678   }
3679 
3680   { /* winnow out coarse points that don't have dofs */
3681     PetscInt dof, cdof, numPointsWithDofs, offset, *pointsWithDofs;
3682     PetscSF  sfDofsOnly;
3683 
3684     for (p = pStartC, numPointsWithDofs = 0; p < pEndC; p++) {
3685       ierr = PetscSectionGetDof(globalCoarse,p,&dof);CHKERRQ(ierr);
3686       ierr = PetscSectionGetConstraintDof(globalCoarse,p,&cdof);CHKERRQ(ierr);
3687       if ((dof - cdof) > 0) {
3688         numPointsWithDofs++;
3689       }
3690     }
3691     ierr = PetscMalloc1(numPointsWithDofs,&pointsWithDofs);CHKERRQ(ierr);
3692     for (p = pStartC, offset = 0; p < pEndC; p++) {
3693       ierr = PetscSectionGetDof(globalCoarse,p,&dof);CHKERRQ(ierr);
3694       ierr = PetscSectionGetConstraintDof(globalCoarse,p,&cdof);CHKERRQ(ierr);
3695       if ((dof - cdof) > 0) {
3696         pointsWithDofs[offset++] = p - pStartC;
3697       }
3698     }
3699     ierr = PetscSFCreateEmbeddedSF(coarseToFineEmbedded, numPointsWithDofs, pointsWithDofs, &sfDofsOnly);CHKERRQ(ierr);
3700     ierr = PetscSFDestroy(&coarseToFineEmbedded);CHKERRQ(ierr);
3701     ierr = PetscFree(pointsWithDofs);CHKERRQ(ierr);
3702     coarseToFineEmbedded = sfDofsOnly;
3703   }
3704 
3705   /* communicate back to the coarse mesh which coarse points have children (that may require injection) */
3706   ierr = PetscSFComputeDegreeBegin(coarseToFineEmbedded,&rootDegrees);CHKERRQ(ierr);
3707   ierr = PetscSFComputeDegreeEnd(coarseToFineEmbedded,&rootDegrees);CHKERRQ(ierr);
3708   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)coarse),&multiRootSec);CHKERRQ(ierr);
3709   ierr = PetscSectionSetChart(multiRootSec,pStartC,pEndC);CHKERRQ(ierr);
3710   for (p = pStartC; p < pEndC; p++) {
3711     ierr = PetscSectionSetDof(multiRootSec,p,rootDegrees[p-pStartC]);CHKERRQ(ierr);
3712   }
3713   ierr = PetscSectionSetUp(multiRootSec);CHKERRQ(ierr);
3714   ierr = PetscSectionGetStorageSize(multiRootSec,&numMulti);CHKERRQ(ierr);
3715   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)coarse),&rootIndicesSec);CHKERRQ(ierr);
3716   { /* distribute the leaf section */
3717     PetscSF multi, multiInv, indicesSF;
3718     PetscInt *remoteOffsets, numRootIndices;
3719 
3720     ierr = PetscSFGetMultiSF(coarseToFineEmbedded,&multi);CHKERRQ(ierr);
3721     ierr = PetscSFCreateInverseSF(multi,&multiInv);CHKERRQ(ierr);
3722     ierr = PetscSFDistributeSection(multiInv,leafIndicesSec,&remoteOffsets,rootIndicesSec);CHKERRQ(ierr);
3723     ierr = PetscSFCreateSectionSF(multiInv,leafIndicesSec,remoteOffsets,rootIndicesSec,&indicesSF);CHKERRQ(ierr);
3724     ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
3725     ierr = PetscSFDestroy(&multiInv);CHKERRQ(ierr);
3726     ierr = PetscSectionGetStorageSize(rootIndicesSec,&numRootIndices);CHKERRQ(ierr);
3727     if (gatheredIndices) {
3728       ierr = PetscMalloc1(numRootIndices,&rootInds);CHKERRQ(ierr);
3729       ierr = PetscSFBcastBegin(indicesSF,MPIU_INT,leafInds,rootInds);CHKERRQ(ierr);
3730       ierr = PetscSFBcastEnd(indicesSF,MPIU_INT,leafInds,rootInds);CHKERRQ(ierr);
3731     }
3732     if (gatheredValues) {
3733       ierr = PetscMalloc1(numRootIndices,&rootVals);CHKERRQ(ierr);
3734       ierr = PetscSFBcastBegin(indicesSF,MPIU_SCALAR,leafVals,rootVals);CHKERRQ(ierr);
3735       ierr = PetscSFBcastEnd(indicesSF,MPIU_SCALAR,leafVals,rootVals);CHKERRQ(ierr);
3736     }
3737     ierr = PetscSFDestroy(&indicesSF);CHKERRQ(ierr);
3738   }
3739   ierr = PetscSectionDestroy(&leafIndicesSec);CHKERRQ(ierr);
3740   ierr = PetscFree(leafInds);CHKERRQ(ierr);
3741   ierr = PetscFree(leafVals);CHKERRQ(ierr);
3742   ierr = PetscSFDestroy(&coarseToFineEmbedded);CHKERRQ(ierr);
3743   *rootMultiSec = multiRootSec;
3744   *multiLeafSec = rootIndicesSec;
3745   if (gatheredIndices) *gatheredIndices = rootInds;
3746   if (gatheredValues)  *gatheredValues  = rootVals;
3747   PetscFunctionReturn(0);
3748 }
3749 
3750 #undef __FUNCT__
3751 #define __FUNCT__ "DMPlexComputeInjectorTree"
3752 PetscErrorCode DMPlexComputeInjectorTree(DM coarse, DM fine, PetscSF coarseToFine, PetscInt *childIds, Mat mat)
3753 {
3754   DM             refTree;
3755   PetscSection   multiRootSec, rootIndicesSec;
3756   PetscSection   globalCoarse, globalFine;
3757   PetscSection   localCoarse, localFine;
3758   PetscSection   cSecRef;
3759   PetscInt       *rootIndices, *parentIndices, pRefStart, pRefEnd;
3760   Mat            injRef;
3761   PetscInt       numFields, maxDof;
3762   PetscInt       pStartC, pEndC, pStartF, pEndF, p;
3763   PetscInt       *offsets, *offsetsCopy, *rowOffsets;
3764   PetscLayout    rowMap, colMap;
3765   PetscInt       rowStart, rowEnd, colStart, colEnd, *nnzD, *nnzO;
3766   PetscScalar    ***childrenMats=NULL ; /* gcc -O gives 'may be used uninitialized' warning'. Initializing to suppress this warning */
3767   PetscErrorCode ierr;
3768 
3769   PetscFunctionBegin;
3770 
3771   /* get the templates for the fine-to-coarse injection from the reference tree */
3772   ierr = DMPlexGetReferenceTree(coarse,&refTree);CHKERRQ(ierr);
3773   ierr = DMGetDefaultConstraints(refTree,&cSecRef,NULL);CHKERRQ(ierr);
3774   ierr = PetscSectionGetChart(cSecRef,&pRefStart,&pRefEnd);CHKERRQ(ierr);
3775   ierr = DMPlexReferenceTreeGetInjector(refTree,&injRef);CHKERRQ(ierr);
3776 
3777   ierr = DMPlexGetChart(fine,&pStartF,&pEndF);CHKERRQ(ierr);
3778   ierr = DMGetDefaultSection(fine,&localFine);CHKERRQ(ierr);
3779   ierr = DMGetDefaultGlobalSection(fine,&globalFine);CHKERRQ(ierr);
3780   ierr = PetscSectionGetNumFields(localFine,&numFields);CHKERRQ(ierr);
3781   ierr = DMPlexGetChart(coarse,&pStartC,&pEndC);CHKERRQ(ierr);
3782   ierr = DMGetDefaultSection(coarse,&localCoarse);CHKERRQ(ierr);
3783   ierr = DMGetDefaultGlobalSection(coarse,&globalCoarse);CHKERRQ(ierr);
3784   ierr = PetscSectionGetMaxDof(localCoarse,&maxDof);CHKERRQ(ierr);
3785   {
3786     PetscInt maxFields = PetscMax(1,numFields) + 1;
3787     ierr = PetscMalloc3(maxFields,&offsets,maxFields,&offsetsCopy,maxFields,&rowOffsets);CHKERRQ(ierr);
3788   }
3789 
3790   ierr = DMPlexTransferInjectorTree(coarse,fine,coarseToFine,childIds,NULL,numFields,offsets,&multiRootSec,&rootIndicesSec,&rootIndices,NULL);CHKERRQ(ierr);
3791 
3792   ierr = PetscMalloc1(maxDof,&parentIndices);CHKERRQ(ierr);
3793 
3794   /* count indices */
3795   ierr = MatGetLayouts(mat,&rowMap,&colMap);CHKERRQ(ierr);
3796   ierr = PetscLayoutSetUp(rowMap);CHKERRQ(ierr);
3797   ierr = PetscLayoutSetUp(colMap);CHKERRQ(ierr);
3798   ierr = PetscLayoutGetRange(rowMap,&rowStart,&rowEnd);CHKERRQ(ierr);
3799   ierr = PetscLayoutGetRange(colMap,&colStart,&colEnd);CHKERRQ(ierr);
3800   ierr = PetscCalloc2(rowEnd-rowStart,&nnzD,rowEnd-rowStart,&nnzO);CHKERRQ(ierr);
3801   for (p = pStartC; p < pEndC; p++) {
3802     PetscInt numLeaves, leafStart, leafEnd, l, dof, cdof, gOff;
3803 
3804     ierr = PetscSectionGetDof(globalCoarse,p,&dof);CHKERRQ(ierr);
3805     ierr = PetscSectionGetConstraintDof(globalCoarse,p,&cdof);CHKERRQ(ierr);
3806     if ((dof - cdof) <= 0) continue;
3807     ierr = PetscSectionGetOffset(globalCoarse,p,&gOff);CHKERRQ(ierr);
3808 
3809     rowOffsets[0] = 0;
3810     offsetsCopy[0] = 0;
3811     if (numFields) {
3812       PetscInt f;
3813 
3814       for (f = 0; f < numFields; f++) {
3815         PetscInt fDof;
3816         ierr = PetscSectionGetFieldDof(localCoarse,p,f,&fDof);CHKERRQ(ierr);
3817         rowOffsets[f + 1] = offsetsCopy[f + 1] = fDof + rowOffsets[f];
3818       }
3819       DMPlexGetIndicesPointFields_Internal(localCoarse,p,gOff < 0 ? -(gOff + 1) : gOff,offsetsCopy,PETSC_FALSE,0,parentIndices);CHKERRQ(ierr);
3820     }
3821     else {
3822       DMPlexGetIndicesPoint_Internal(localCoarse,p,gOff < 0 ? -(gOff + 1) : gOff,offsetsCopy,PETSC_FALSE,0,parentIndices);CHKERRQ(ierr);
3823       rowOffsets[1] = offsetsCopy[0];
3824     }
3825 
3826     ierr = PetscSectionGetDof(multiRootSec,p,&numLeaves);CHKERRQ(ierr);
3827     ierr = PetscSectionGetOffset(multiRootSec,p,&leafStart);CHKERRQ(ierr);
3828     leafEnd = leafStart + numLeaves;
3829     for (l = leafStart; l < leafEnd; l++) {
3830       PetscInt numIndices, childId, offset;
3831       const PetscInt *childIndices;
3832 
3833       ierr = PetscSectionGetDof(rootIndicesSec,l,&numIndices);CHKERRQ(ierr);
3834       ierr = PetscSectionGetOffset(rootIndicesSec,l,&offset);CHKERRQ(ierr);
3835       childId = rootIndices[offset++];
3836       childIndices = &rootIndices[offset];
3837       numIndices--;
3838 
3839       if (childId == -1) { /* equivalent points: scatter */
3840         PetscInt i;
3841 
3842         for (i = 0; i < numIndices; i++) {
3843           PetscInt colIndex = childIndices[i];
3844           PetscInt rowIndex = parentIndices[i];
3845           if (rowIndex < 0) continue;
3846           if (colIndex < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unconstrained fine and constrained coarse");
3847           if (colIndex >= colStart && colIndex < colEnd) {
3848             nnzD[rowIndex - rowStart] = 1;
3849           }
3850           else {
3851             nnzO[rowIndex - rowStart] = 1;
3852           }
3853         }
3854       }
3855       else {
3856         PetscInt parentId, f, lim;
3857 
3858         ierr = DMPlexGetTreeParent(refTree,childId,&parentId,NULL);CHKERRQ(ierr);
3859 
3860         lim = PetscMax(1,numFields);
3861         offsets[0] = 0;
3862         if (numFields) {
3863           PetscInt f;
3864 
3865           for (f = 0; f < numFields; f++) {
3866             PetscInt fDof;
3867             ierr = PetscSectionGetFieldDof(cSecRef,childId,f,&fDof);CHKERRQ(ierr);
3868 
3869             offsets[f + 1] = fDof + offsets[f];
3870           }
3871         }
3872         else {
3873           PetscInt cDof;
3874 
3875           ierr = PetscSectionGetDof(cSecRef,childId,&cDof);CHKERRQ(ierr);
3876           offsets[1] = cDof;
3877         }
3878         for (f = 0; f < lim; f++) {
3879           PetscInt parentStart = rowOffsets[f], parentEnd = rowOffsets[f + 1];
3880           PetscInt childStart = offsets[f], childEnd = offsets[f + 1];
3881           PetscInt i, numD = 0, numO = 0;
3882 
3883           for (i = childStart; i < childEnd; i++) {
3884             PetscInt colIndex = childIndices[i];
3885 
3886             if (colIndex < 0) continue;
3887             if (colIndex >= colStart && colIndex < colEnd) {
3888               numD++;
3889             }
3890             else {
3891               numO++;
3892             }
3893           }
3894           for (i = parentStart; i < parentEnd; i++) {
3895             PetscInt rowIndex = parentIndices[i];
3896 
3897             if (rowIndex < 0) continue;
3898             nnzD[rowIndex - rowStart] += numD;
3899             nnzO[rowIndex - rowStart] += numO;
3900           }
3901         }
3902       }
3903     }
3904   }
3905   /* preallocate */
3906   ierr = MatXAIJSetPreallocation(mat,1,nnzD,nnzO,NULL,NULL);CHKERRQ(ierr);
3907   ierr = PetscFree2(nnzD,nnzO);CHKERRQ(ierr);
3908   /* insert values */
3909   ierr = DMPlexReferenceTreeGetChildrenMatrices_Injection(refTree,injRef,&childrenMats);CHKERRQ(ierr);
3910   for (p = pStartC; p < pEndC; p++) {
3911     PetscInt numLeaves, leafStart, leafEnd, l, dof, cdof, gOff;
3912 
3913     ierr = PetscSectionGetDof(globalCoarse,p,&dof);CHKERRQ(ierr);
3914     ierr = PetscSectionGetConstraintDof(globalCoarse,p,&cdof);CHKERRQ(ierr);
3915     if ((dof - cdof) <= 0) continue;
3916     ierr = PetscSectionGetOffset(globalCoarse,p,&gOff);CHKERRQ(ierr);
3917 
3918     rowOffsets[0] = 0;
3919     offsetsCopy[0] = 0;
3920     if (numFields) {
3921       PetscInt f;
3922 
3923       for (f = 0; f < numFields; f++) {
3924         PetscInt fDof;
3925         ierr = PetscSectionGetFieldDof(localCoarse,p,f,&fDof);CHKERRQ(ierr);
3926         rowOffsets[f + 1] = offsetsCopy[f + 1] = fDof + rowOffsets[f];
3927       }
3928       DMPlexGetIndicesPointFields_Internal(localCoarse,p,gOff < 0 ? -(gOff + 1) : gOff,offsetsCopy,PETSC_FALSE,0,parentIndices);CHKERRQ(ierr);
3929     }
3930     else {
3931       DMPlexGetIndicesPoint_Internal(localCoarse,p,gOff < 0 ? -(gOff + 1) : gOff,offsetsCopy,PETSC_FALSE,0,parentIndices);CHKERRQ(ierr);
3932       rowOffsets[1] = offsetsCopy[0];
3933     }
3934 
3935     ierr = PetscSectionGetDof(multiRootSec,p,&numLeaves);CHKERRQ(ierr);
3936     ierr = PetscSectionGetOffset(multiRootSec,p,&leafStart);CHKERRQ(ierr);
3937     leafEnd = leafStart + numLeaves;
3938     for (l = leafStart; l < leafEnd; l++) {
3939       PetscInt numIndices, childId, offset;
3940       const PetscInt *childIndices;
3941 
3942       ierr = PetscSectionGetDof(rootIndicesSec,l,&numIndices);CHKERRQ(ierr);
3943       ierr = PetscSectionGetOffset(rootIndicesSec,l,&offset);CHKERRQ(ierr);
3944       childId = rootIndices[offset++];
3945       childIndices = &rootIndices[offset];
3946       numIndices--;
3947 
3948       if (childId == -1) { /* equivalent points: scatter */
3949         PetscInt i;
3950 
3951         for (i = 0; i < numIndices; i++) {
3952           ierr = MatSetValue(mat,parentIndices[i],childIndices[i],1.,INSERT_VALUES);CHKERRQ(ierr);
3953         }
3954       }
3955       else {
3956         PetscInt parentId, f, lim;
3957 
3958         ierr = DMPlexGetTreeParent(refTree,childId,&parentId,NULL);CHKERRQ(ierr);
3959 
3960         lim = PetscMax(1,numFields);
3961         offsets[0] = 0;
3962         if (numFields) {
3963           PetscInt f;
3964 
3965           for (f = 0; f < numFields; f++) {
3966             PetscInt fDof;
3967             ierr = PetscSectionGetFieldDof(cSecRef,childId,f,&fDof);CHKERRQ(ierr);
3968 
3969             offsets[f + 1] = fDof + offsets[f];
3970           }
3971         }
3972         else {
3973           PetscInt cDof;
3974 
3975           ierr = PetscSectionGetDof(cSecRef,childId,&cDof);CHKERRQ(ierr);
3976           offsets[1] = cDof;
3977         }
3978         for (f = 0; f < lim; f++) {
3979           PetscScalar    *childMat   = &childrenMats[childId - pRefStart][f][0];
3980           PetscInt       *rowIndices = &parentIndices[rowOffsets[f]];
3981           const PetscInt *colIndices = &childIndices[offsets[f]];
3982 
3983           ierr = MatSetValues(mat,rowOffsets[f+1]-rowOffsets[f],rowIndices,offsets[f+1]-offsets[f],colIndices,childMat,INSERT_VALUES);CHKERRQ(ierr);
3984         }
3985       }
3986     }
3987   }
3988   ierr = PetscSectionDestroy(&multiRootSec);CHKERRQ(ierr);
3989   ierr = PetscSectionDestroy(&rootIndicesSec);CHKERRQ(ierr);
3990   ierr = PetscFree(parentIndices);CHKERRQ(ierr);
3991   ierr = DMPlexReferenceTreeRestoreChildrenMatrices_Injection(refTree,injRef,&childrenMats);CHKERRQ(ierr);
3992   ierr = PetscFree(rootIndices);CHKERRQ(ierr);
3993   ierr = PetscFree3(offsets,offsetsCopy,rowOffsets);CHKERRQ(ierr);
3994 
3995   ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3996   ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3997   PetscFunctionReturn(0);
3998 }
3999 
4000 #undef __FUNCT__
4001 #define __FUNCT__ "DMPlexTransferVecTree_Interpolate"
4002 static PetscErrorCode DMPlexTransferVecTree_Interpolate(DM coarse, Vec vecCoarseLocal, DM fine, Vec vecFine, PetscSF coarseToFine, PetscInt *cids, Vec grad, Vec cellGeom)
4003 {
4004   PetscDS           ds;
4005   PetscSF           coarseToFineEmbedded;
4006   PetscSection      globalCoarse, globalFine;
4007   PetscSection      localCoarse, localFine;
4008   PetscSection      aSec, cSec;
4009   PetscSection      rootValuesSec;
4010   PetscSection      leafValuesSec;
4011   PetscScalar       *rootValues, *leafValues;
4012   IS                aIS;
4013   const PetscInt    *anchors;
4014   Mat               cMat;
4015   PetscInt          numFields;
4016   PetscInt          pStartC, pEndC, pStartF, pEndF, p, cellStart, cellEnd;
4017   PetscInt          aStart, aEnd, cStart, cEnd;
4018   PetscInt          *maxChildIds;
4019   PetscInt          *offsets, *newOffsets, *offsetsCopy, *newOffsetsCopy, *rowOffsets, *numD, *numO;
4020   PetscFV           fv = NULL;
4021   PetscInt          dim, numFVcomps = -1, fvField = -1;
4022   DM                cellDM = NULL, gradDM = NULL;
4023   const PetscScalar *cellGeomArray = NULL;
4024   const PetscScalar *gradArray = NULL;
4025   PetscErrorCode    ierr;
4026 
4027   PetscFunctionBegin;
4028   ierr = DMPlexGetChart(coarse,&pStartC,&pEndC);CHKERRQ(ierr);
4029   ierr = DMPlexGetHeightStratum(coarse,0,&cellStart,&cellEnd);CHKERRQ(ierr);
4030   ierr = DMPlexGetChart(fine,&pStartF,&pEndF);CHKERRQ(ierr);
4031   ierr = DMGetDefaultGlobalSection(fine,&globalFine);CHKERRQ(ierr);
4032   ierr = DMGetCoordinateDim(coarse,&dim);CHKERRQ(ierr);
4033   { /* winnow fine points that don't have global dofs out of the sf */
4034     PetscInt       nleaves, l;
4035     const PetscInt *leaves;
4036     PetscInt       dof, cdof, numPointsWithDofs, offset, *pointsWithDofs;
4037 
4038     ierr = PetscSFGetGraph(coarseToFine,NULL,&nleaves,&leaves,NULL);CHKERRQ(ierr);
4039 
4040     for (l = 0, numPointsWithDofs = 0; l < nleaves; l++) {
4041       PetscInt p = leaves ? leaves[l] : l;
4042 
4043       ierr = PetscSectionGetDof(globalFine,p,&dof);CHKERRQ(ierr);
4044       ierr = PetscSectionGetConstraintDof(globalFine,p,&cdof);CHKERRQ(ierr);
4045       if ((dof - cdof) > 0) {
4046         numPointsWithDofs++;
4047       }
4048     }
4049     ierr = PetscMalloc1(numPointsWithDofs,&pointsWithDofs);CHKERRQ(ierr);
4050     for (l = 0, offset = 0; l < nleaves; l++) {
4051       PetscInt p = leaves ? leaves[l] : l;
4052 
4053       ierr = PetscSectionGetDof(globalFine,p,&dof);CHKERRQ(ierr);
4054       ierr = PetscSectionGetConstraintDof(globalFine,p,&cdof);CHKERRQ(ierr);
4055       if ((dof - cdof) > 0) {
4056         pointsWithDofs[offset++] = l;
4057       }
4058     }
4059     ierr = PetscSFCreateEmbeddedLeafSF(coarseToFine, numPointsWithDofs, pointsWithDofs, &coarseToFineEmbedded);CHKERRQ(ierr);
4060     ierr = PetscFree(pointsWithDofs);CHKERRQ(ierr);
4061   }
4062   /* communicate back to the coarse mesh which coarse points have children (that may require interpolation) */
4063   ierr = PetscMalloc1(pEndC-pStartC,&maxChildIds);CHKERRQ(ierr);
4064   for (p = pStartC; p < pEndC; p++) {
4065     maxChildIds[p - pStartC] = -2;
4066   }
4067   ierr = PetscSFReduceBegin(coarseToFineEmbedded,MPIU_INT,cids,maxChildIds,MPIU_MAX);CHKERRQ(ierr);
4068   ierr = PetscSFReduceEnd(coarseToFineEmbedded,MPIU_INT,cids,maxChildIds,MPIU_MAX);CHKERRQ(ierr);
4069 
4070   ierr = DMGetDefaultSection(coarse,&localCoarse);CHKERRQ(ierr);
4071   ierr = DMGetDefaultGlobalSection(coarse,&globalCoarse);CHKERRQ(ierr);
4072 
4073   ierr = DMPlexGetAnchors(coarse,&aSec,&aIS);CHKERRQ(ierr);
4074   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
4075   ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
4076 
4077   ierr = DMGetDefaultConstraints(coarse,&cSec,&cMat);CHKERRQ(ierr);
4078   ierr = PetscSectionGetChart(cSec,&cStart,&cEnd);CHKERRQ(ierr);
4079 
4080   /* create sections that will send to children the indices and matrices they will need to construct the interpolator */
4081   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)coarse),&rootValuesSec);CHKERRQ(ierr);
4082   ierr = PetscSectionSetChart(rootValuesSec,pStartC,pEndC);CHKERRQ(ierr);
4083   ierr = PetscSectionGetNumFields(globalCoarse,&numFields);CHKERRQ(ierr);
4084   {
4085     PetscInt maxFields = PetscMax(1,numFields) + 1;
4086     ierr = PetscMalloc7(maxFields,&offsets,maxFields,&offsetsCopy,maxFields,&newOffsets,maxFields,&newOffsetsCopy,maxFields,&rowOffsets,maxFields,&numD,maxFields,&numO);CHKERRQ(ierr);
4087   }
4088   if (grad) {
4089     PetscInt i;
4090 
4091     ierr = VecGetDM(cellGeom,&cellDM);CHKERRQ(ierr);
4092     ierr = VecGetArrayRead(cellGeom,&cellGeomArray);CHKERRQ(ierr);
4093     ierr = VecGetDM(grad,&gradDM);CHKERRQ(ierr);
4094     ierr = VecGetArrayRead(grad,&gradArray);CHKERRQ(ierr);
4095     for (i = 0; i < PetscMax(1,numFields); i++) {
4096       PetscObject  obj;
4097       PetscClassId id;
4098 
4099       ierr = DMGetField(coarse, i, &obj);CHKERRQ(ierr);
4100       ierr = PetscObjectGetClassId(obj,&id);CHKERRQ(ierr);
4101       if (id == PETSCFV_CLASSID) {
4102         fv      = (PetscFV) obj;
4103         ierr    = PetscFVGetNumComponents(fv,&numFVcomps);CHKERRQ(ierr);
4104         fvField = i;
4105         break;
4106       }
4107     }
4108   }
4109 
4110   for (p = pStartC; p < pEndC; p++) { /* count the sizes of the indices and matrices */
4111     PetscInt dof;
4112     PetscInt maxChildId     = maxChildIds[p - pStartC];
4113     PetscInt numValues      = 0;
4114 
4115     ierr = PetscSectionGetDof(globalCoarse,p,&dof);CHKERRQ(ierr);
4116     if (dof < 0) {
4117       dof = -(dof + 1);
4118     }
4119     offsets[0]    = 0;
4120     newOffsets[0] = 0;
4121     if (maxChildId >= 0) { /* this point has children (with dofs) that will need to be interpolated from the closure of p */
4122       PetscInt *closure = NULL, closureSize, cl;
4123 
4124       ierr = DMPlexGetTransitiveClosure(coarse,p,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
4125       for (cl = 0; cl < closureSize; cl++) { /* get the closure */
4126         PetscInt c = closure[2 * cl], clDof;
4127 
4128         ierr = PetscSectionGetDof(localCoarse,c,&clDof);CHKERRQ(ierr);
4129         numValues += clDof;
4130       }
4131       ierr = DMPlexRestoreTransitiveClosure(coarse,p,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr);
4132     }
4133     else if (maxChildId == -1) {
4134       ierr = PetscSectionGetDof(localCoarse,p,&numValues);CHKERRQ(ierr);
4135     }
4136     /* we will pack the column indices with the field offsets */
4137     if (maxChildId >= -1 && grad && p >= cellStart && p < cellEnd) {
4138       /* also send the centroid, and the gradient */
4139       numValues += dim * (1 + numFVcomps);
4140     }
4141     ierr = PetscSectionSetDof(rootValuesSec,p,numValues);CHKERRQ(ierr);
4142   }
4143   ierr = PetscSectionSetUp(rootValuesSec);CHKERRQ(ierr);
4144   {
4145     PetscInt          numRootValues;
4146     const PetscScalar *coarseArray;
4147 
4148     ierr = PetscSectionGetStorageSize(rootValuesSec,&numRootValues);CHKERRQ(ierr);
4149     ierr = PetscMalloc1(numRootValues,&rootValues);CHKERRQ(ierr);
4150     ierr = VecGetArrayRead(vecCoarseLocal,&coarseArray);CHKERRQ(ierr);
4151     for (p = pStartC; p < pEndC; p++) {
4152       PetscInt    numValues;
4153       PetscInt    pValOff;
4154       PetscScalar *pVal;
4155       PetscInt    maxChildId = maxChildIds[p - pStartC];
4156 
4157       ierr = PetscSectionGetDof(rootValuesSec,p,&numValues);CHKERRQ(ierr);
4158       if (!numValues) {
4159         continue;
4160       }
4161       ierr = PetscSectionGetOffset(rootValuesSec,p,&pValOff);CHKERRQ(ierr);
4162       pVal = &(rootValues[pValOff]);
4163       if (maxChildId >= 0) { /* build an identity matrix, apply matrix constraints on the right */
4164         PetscInt closureSize = numValues;
4165         ierr = DMPlexVecGetClosure(coarse,NULL,vecCoarseLocal,p,&closureSize,&pVal);CHKERRQ(ierr);
4166       }
4167       else if (maxChildId == -1) {
4168         PetscInt lDof, lOff, i;
4169 
4170         ierr = PetscSectionGetDof(localCoarse,p,&lDof);CHKERRQ(ierr);
4171         ierr = PetscSectionGetOffset(localCoarse,p,&lOff);CHKERRQ(ierr);
4172         for (i = 0; i < lDof; i++) pVal[i] = coarseArray[lOff + i];
4173       }
4174       if (grad && p >= cellStart && p < cellEnd) {
4175         const PetscFVCellGeom *cg;
4176         PetscScalar *gradVals;
4177         PetscInt i;
4178 
4179         pVal += (numValues - dim * (1 + numFVcomps));
4180 
4181         ierr = DMPlexPointLocalRead(cellDM,p,cellGeomArray,&cg);CHKERRQ(ierr);
4182         for (i = 0; i < dim; i++) pVal[i] = cg->centroid[i];
4183         pVal += dim;
4184         ierr = DMPlexPointGlobalRead(gradDM,p,gradArray,&gradVals);CHKERRQ(ierr);
4185         for (i = 0; i < dim * numFVcomps; i++) pVal[i] = gradVals[i];
4186       }
4187     }
4188     ierr = VecRestoreArrayRead(vecCoarseLocal,&coarseArray);CHKERRQ(ierr);
4189     ierr = PetscFree(maxChildIds);CHKERRQ(ierr);
4190   }
4191   {
4192     PetscSF  valuesSF;
4193     PetscInt *remoteOffsetsValues, numLeafValues;
4194 
4195     ierr = PetscSectionCreate(PetscObjectComm((PetscObject)fine),&leafValuesSec);CHKERRQ(ierr);
4196     ierr = PetscSFDistributeSection(coarseToFineEmbedded,rootValuesSec,&remoteOffsetsValues,leafValuesSec);CHKERRQ(ierr);
4197     ierr = PetscSFCreateSectionSF(coarseToFineEmbedded,rootValuesSec,remoteOffsetsValues,leafValuesSec,&valuesSF);CHKERRQ(ierr);
4198     ierr = PetscSFDestroy(&coarseToFineEmbedded);CHKERRQ(ierr);
4199     ierr = PetscFree(remoteOffsetsValues);CHKERRQ(ierr);
4200     ierr = PetscSectionGetStorageSize(leafValuesSec,&numLeafValues);CHKERRQ(ierr);
4201     ierr = PetscMalloc1(numLeafValues,&leafValues);CHKERRQ(ierr);
4202     ierr = PetscSFBcastBegin(valuesSF,MPIU_SCALAR,rootValues,leafValues);CHKERRQ(ierr);
4203     ierr = PetscSFBcastEnd(valuesSF,MPIU_SCALAR,rootValues,leafValues);CHKERRQ(ierr);
4204     ierr = PetscSFDestroy(&valuesSF);CHKERRQ(ierr);
4205     ierr = PetscFree(rootValues);CHKERRQ(ierr);
4206     ierr = PetscSectionDestroy(&rootValuesSec);CHKERRQ(ierr);
4207   }
4208   ierr = DMGetDefaultSection(fine,&localFine);CHKERRQ(ierr);
4209   {
4210     PetscInt    maxDof;
4211     PetscInt    *rowIndices;
4212     DM           refTree;
4213     PetscInt     **refPointFieldN;
4214     PetscScalar  ***refPointFieldMats;
4215     PetscSection refConSec, refAnSec;
4216     PetscInt     pRefStart,pRefEnd,leafStart,leafEnd;
4217     PetscScalar  *pointWork;
4218 
4219     ierr = PetscSectionGetMaxDof(globalFine,&maxDof);CHKERRQ(ierr);
4220     ierr = DMGetWorkArray(fine,maxDof,PETSC_INT,&rowIndices);CHKERRQ(ierr);
4221     ierr = DMGetWorkArray(fine,maxDof,PETSC_SCALAR,&pointWork);CHKERRQ(ierr);
4222     ierr = DMPlexGetReferenceTree(fine,&refTree);CHKERRQ(ierr);
4223     ierr = DMGetDS(fine,&ds);CHKERRQ(ierr);
4224     ierr = DMSetDS(refTree,ds);CHKERRQ(ierr);
4225     ierr = DMPlexReferenceTreeGetChildrenMatrices(refTree,&refPointFieldMats,&refPointFieldN);CHKERRQ(ierr);
4226     ierr = DMGetDefaultConstraints(refTree,&refConSec,NULL);CHKERRQ(ierr);
4227     ierr = DMPlexGetAnchors(refTree,&refAnSec,NULL);CHKERRQ(ierr);
4228     ierr = PetscSectionGetChart(refConSec,&pRefStart,&pRefEnd);CHKERRQ(ierr);
4229     ierr = PetscSectionGetChart(leafValuesSec,&leafStart,&leafEnd);CHKERRQ(ierr);
4230     ierr = DMPlexGetHeightStratum(fine,0,&cellStart,&cellEnd);CHKERRQ(ierr);
4231     for (p = leafStart; p < leafEnd; p++) {
4232       PetscInt          gDof, gcDof, gOff, lDof;
4233       PetscInt          numValues, pValOff;
4234       PetscInt          childId;
4235       const PetscScalar *pVal;
4236       const PetscScalar *fvGradData = NULL;
4237 
4238       ierr = PetscSectionGetDof(globalFine,p,&gDof);CHKERRQ(ierr);
4239       ierr = PetscSectionGetDof(localFine,p,&lDof);CHKERRQ(ierr);
4240       ierr = PetscSectionGetConstraintDof(globalFine,p,&gcDof);CHKERRQ(ierr);
4241       if ((gDof - gcDof) <= 0) {
4242         continue;
4243       }
4244       ierr = PetscSectionGetOffset(globalFine,p,&gOff);CHKERRQ(ierr);
4245       ierr = PetscSectionGetDof(leafValuesSec,p,&numValues);CHKERRQ(ierr);
4246       if (!numValues) continue;
4247       ierr = PetscSectionGetOffset(leafValuesSec,p,&pValOff);CHKERRQ(ierr);
4248       pVal = &leafValues[pValOff];
4249       offsets[0]        = 0;
4250       offsetsCopy[0]    = 0;
4251       newOffsets[0]     = 0;
4252       newOffsetsCopy[0] = 0;
4253       childId           = cids[p - pStartF];
4254       if (grad && p >= cellStart && p < cellEnd) {
4255         numValues -= (dim * (1 + numFVcomps));
4256         fvGradData = &pVal[numValues];
4257       }
4258       if (numFields) {
4259         PetscInt f;
4260         for (f = 0; f < numFields; f++) {
4261           PetscInt rowDof;
4262 
4263           ierr = PetscSectionGetFieldDof(localFine,p,f,&rowDof);CHKERRQ(ierr);
4264           offsets[f + 1]        = offsets[f] + rowDof;
4265           offsetsCopy[f + 1]    = offsets[f + 1];
4266           /* TODO: closure indices */
4267           newOffsets[f + 1]     = newOffsets[f] + ((childId == -1) ? rowDof : refPointFieldN[childId - pRefStart][f]);
4268         }
4269         ierr = DMPlexGetIndicesPointFields_Internal(localFine,p,gOff,offsetsCopy,PETSC_FALSE,0,rowIndices);CHKERRQ(ierr);
4270       }
4271       else {
4272         offsets[0]    = 0;
4273         offsets[1]    = lDof;
4274         newOffsets[0] = 0;
4275         newOffsets[1] = (childId == -1) ? lDof : refPointFieldN[childId - pRefStart][0];
4276         ierr = DMPlexGetIndicesPoint_Internal(localFine,p,gOff,offsetsCopy,PETSC_FALSE,0,rowIndices);CHKERRQ(ierr);
4277       }
4278       if (childId == -1) { /* no child interpolation: one nnz per */
4279         ierr = VecSetValues(vecFine,numValues,rowIndices,pVal,ADD_VALUES);CHKERRQ(ierr);
4280       } else {
4281         PetscInt f;
4282 
4283         for (f = 0; f < PetscMax(1,numFields); f++) {
4284           const PetscScalar *childMat = refPointFieldMats[childId - pRefStart][f];
4285           PetscInt numRows = offsets[f+1] - offsets[f];
4286           PetscInt numCols = newOffsets[f + 1] - newOffsets[f];
4287           const PetscScalar *cVal = &pVal[newOffsets[f]];
4288           PetscScalar *rVal = &pointWork[offsets[f]];
4289           PetscInt i, j;
4290 
4291           for (i = 0; i < numRows; i++) {
4292             PetscScalar val = 0.;
4293             for (j = 0; j < numCols; j++) {
4294               val += childMat[i * numCols + j] * cVal[j];
4295             }
4296             rVal[i] = val;
4297           }
4298           if (f == fvField && p >= cellStart && p < cellEnd) {
4299             PetscReal   centroid[3];
4300             PetscScalar diff[3];
4301             const PetscScalar *parentCentroid = &fvGradData[0];
4302             const PetscScalar *gradient       = &fvGradData[dim];
4303 
4304             ierr = DMPlexComputeCellGeometryFVM(fine,p,NULL,centroid,NULL);CHKERRQ(ierr);
4305             for (i = 0; i < dim; i++) {
4306               diff[i] = centroid[i] - parentCentroid[i];
4307             }
4308             for (i = 0; i < numFVcomps; i++) {
4309               PetscScalar val = 0.;
4310 
4311               for (j = 0; j < 3; j++) {
4312                 val += gradient[dim * i + j] * diff[j];
4313               }
4314               rVal[i] += val;
4315             }
4316           }
4317           ierr = VecSetValues(vecFine,numRows,&rowIndices[offsets[f]],rVal,ADD_VALUES);CHKERRQ(ierr);
4318         }
4319       }
4320     }
4321     ierr = DMPlexReferenceTreeRestoreChildrenMatrices(refTree,&refPointFieldMats,&refPointFieldN);CHKERRQ(ierr);
4322     ierr = DMRestoreWorkArray(fine,maxDof,PETSC_INT,&rowIndices);CHKERRQ(ierr);
4323     ierr = DMRestoreWorkArray(fine,maxDof,PETSC_SCALAR,&pointWork);CHKERRQ(ierr);
4324   }
4325   ierr = PetscFree(leafValues);CHKERRQ(ierr);
4326   ierr = PetscSectionDestroy(&leafValuesSec);CHKERRQ(ierr);
4327   ierr = PetscFree7(offsets,offsetsCopy,newOffsets,newOffsetsCopy,rowOffsets,numD,numO);CHKERRQ(ierr);
4328   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
4329   PetscFunctionReturn(0);
4330 }
4331 
4332 #undef __FUNCT__
4333 #define __FUNCT__ "DMPlexTransferVecTree_Inject"
4334 static PetscErrorCode DMPlexTransferVecTree_Inject(DM fine, Vec vecFine, DM coarse, Vec vecCoarse, PetscSF coarseToFine, PetscInt *cids)
4335 {
4336   PetscDS        ds;
4337   DM             refTree;
4338   PetscSection   multiRootSec, rootIndicesSec;
4339   PetscSection   globalCoarse, globalFine;
4340   PetscSection   localCoarse, localFine;
4341   PetscSection   cSecRef;
4342   PetscInt       *parentIndices, pRefStart, pRefEnd;
4343   PetscScalar    *rootValues, *parentValues;
4344   Mat            injRef;
4345   PetscInt       numFields, maxDof;
4346   PetscInt       pStartC, pEndC, pStartF, pEndF, p;
4347   PetscInt       *offsets, *offsetsCopy, *rowOffsets;
4348   PetscLayout    rowMap, colMap;
4349   PetscInt       rowStart, rowEnd, colStart, colEnd;
4350   PetscScalar    ***childrenMats=NULL ; /* gcc -O gives 'may be used uninitialized' warning'. Initializing to suppress this warning */
4351   PetscErrorCode ierr;
4352 
4353   PetscFunctionBegin;
4354 
4355   /* get the templates for the fine-to-coarse injection from the reference tree */
4356   ierr = DMPlexGetReferenceTree(coarse,&refTree);CHKERRQ(ierr);
4357   ierr = DMGetDS(coarse,&ds);CHKERRQ(ierr);
4358   ierr = DMSetDS(refTree,ds);CHKERRQ(ierr);
4359   ierr = DMGetDefaultConstraints(refTree,&cSecRef,NULL);CHKERRQ(ierr);
4360   ierr = PetscSectionGetChart(cSecRef,&pRefStart,&pRefEnd);CHKERRQ(ierr);
4361   ierr = DMPlexReferenceTreeGetInjector(refTree,&injRef);CHKERRQ(ierr);
4362 
4363   ierr = DMPlexGetChart(fine,&pStartF,&pEndF);CHKERRQ(ierr);
4364   ierr = DMGetDefaultSection(fine,&localFine);CHKERRQ(ierr);
4365   ierr = DMGetDefaultGlobalSection(fine,&globalFine);CHKERRQ(ierr);
4366   ierr = PetscSectionGetNumFields(localFine,&numFields);CHKERRQ(ierr);
4367   ierr = DMPlexGetChart(coarse,&pStartC,&pEndC);CHKERRQ(ierr);
4368   ierr = DMGetDefaultSection(coarse,&localCoarse);CHKERRQ(ierr);
4369   ierr = DMGetDefaultGlobalSection(coarse,&globalCoarse);CHKERRQ(ierr);
4370   ierr = PetscSectionGetMaxDof(localCoarse,&maxDof);CHKERRQ(ierr);
4371   {
4372     PetscInt maxFields = PetscMax(1,numFields) + 1;
4373     ierr = PetscMalloc3(maxFields,&offsets,maxFields,&offsetsCopy,maxFields,&rowOffsets);CHKERRQ(ierr);
4374   }
4375 
4376   ierr = DMPlexTransferInjectorTree(coarse,fine,coarseToFine,cids,vecFine,numFields,offsets,&multiRootSec,&rootIndicesSec,NULL,&rootValues);CHKERRQ(ierr);
4377 
4378   ierr = PetscMalloc2(maxDof,&parentIndices,maxDof,&parentValues);CHKERRQ(ierr);
4379 
4380   /* count indices */
4381   ierr = VecGetLayout(vecFine,&colMap);CHKERRQ(ierr);
4382   ierr = VecGetLayout(vecCoarse,&rowMap);CHKERRQ(ierr);
4383   ierr = PetscLayoutSetUp(rowMap);CHKERRQ(ierr);
4384   ierr = PetscLayoutSetUp(colMap);CHKERRQ(ierr);
4385   ierr = PetscLayoutGetRange(rowMap,&rowStart,&rowEnd);CHKERRQ(ierr);
4386   ierr = PetscLayoutGetRange(colMap,&colStart,&colEnd);CHKERRQ(ierr);
4387   /* insert values */
4388   ierr = DMPlexReferenceTreeGetChildrenMatrices_Injection(refTree,injRef,&childrenMats);CHKERRQ(ierr);
4389   for (p = pStartC; p < pEndC; p++) {
4390     PetscInt numLeaves, leafStart, leafEnd, l, dof, cdof, gOff;
4391 
4392     ierr = PetscSectionGetDof(globalCoarse,p,&dof);CHKERRQ(ierr);
4393     ierr = PetscSectionGetConstraintDof(globalCoarse,p,&cdof);CHKERRQ(ierr);
4394     if ((dof - cdof) <= 0) continue;
4395     ierr = PetscSectionGetOffset(globalCoarse,p,&gOff);CHKERRQ(ierr);
4396 
4397     rowOffsets[0] = 0;
4398     offsetsCopy[0] = 0;
4399     if (numFields) {
4400       PetscInt f;
4401 
4402       for (f = 0; f < numFields; f++) {
4403         PetscInt fDof;
4404         ierr = PetscSectionGetFieldDof(localCoarse,p,f,&fDof);CHKERRQ(ierr);
4405         rowOffsets[f + 1] = offsetsCopy[f + 1] = fDof + rowOffsets[f];
4406       }
4407       DMPlexGetIndicesPointFields_Internal(localCoarse,p,gOff < 0 ? -(gOff + 1) : gOff,offsetsCopy,PETSC_FALSE,0,parentIndices);CHKERRQ(ierr);
4408     }
4409     else {
4410       DMPlexGetIndicesPoint_Internal(localCoarse,p,gOff < 0 ? -(gOff + 1) : gOff,offsetsCopy,PETSC_FALSE,0,parentIndices);CHKERRQ(ierr);
4411       rowOffsets[1] = offsetsCopy[0];
4412     }
4413 
4414     ierr = PetscSectionGetDof(multiRootSec,p,&numLeaves);CHKERRQ(ierr);
4415     ierr = PetscSectionGetOffset(multiRootSec,p,&leafStart);CHKERRQ(ierr);
4416     leafEnd = leafStart + numLeaves;
4417     for (l = leafStart; l < leafEnd; l++) {
4418       PetscInt numIndices, childId, offset;
4419       const PetscScalar *childValues;
4420 
4421       ierr = PetscSectionGetDof(rootIndicesSec,l,&numIndices);CHKERRQ(ierr);
4422       ierr = PetscSectionGetOffset(rootIndicesSec,l,&offset);CHKERRQ(ierr);
4423       childId = (PetscInt) PetscRealPart(rootValues[offset++]);
4424       childValues = &rootValues[offset];
4425       numIndices--;
4426 
4427       if (childId == -2) { /* skip */
4428         continue;
4429       } else if (childId == -1) { /* equivalent points: scatter */
4430         ierr = VecSetValues(vecCoarse,numIndices,parentIndices,childValues,ADD_VALUES);CHKERRQ(ierr);
4431       } else {
4432         PetscInt parentId, f, lim;
4433 
4434         ierr = DMPlexGetTreeParent(refTree,childId,&parentId,NULL);CHKERRQ(ierr);
4435 
4436         lim = PetscMax(1,numFields);
4437         offsets[0] = 0;
4438         if (numFields) {
4439           PetscInt f;
4440 
4441           for (f = 0; f < numFields; f++) {
4442             PetscInt fDof;
4443             ierr = PetscSectionGetFieldDof(cSecRef,childId,f,&fDof);CHKERRQ(ierr);
4444 
4445             offsets[f + 1] = fDof + offsets[f];
4446           }
4447         }
4448         else {
4449           PetscInt cDof;
4450 
4451           ierr = PetscSectionGetDof(cSecRef,childId,&cDof);CHKERRQ(ierr);
4452           offsets[1] = cDof;
4453         }
4454         for (f = 0; f < lim; f++) {
4455           PetscScalar       *childMat   = &childrenMats[childId - pRefStart][f][0];
4456           PetscInt          *rowIndices = &parentIndices[rowOffsets[f]];
4457           PetscInt          m           = rowOffsets[f+1]-rowOffsets[f];
4458           PetscInt          n           = offsets[f+1]-offsets[f];
4459           PetscInt          i, j;
4460           const PetscScalar *colValues  = &childValues[offsets[f]];
4461 
4462           for (i = 0; i < m; i++) {
4463             PetscScalar val = 0.;
4464             for (j = 0; j < n; j++) {
4465               val += childMat[n * i + j] * colValues[j];
4466             }
4467             parentValues[i] = val;
4468           }
4469           ierr = VecSetValues(vecCoarse,m,rowIndices,parentValues,ADD_VALUES);CHKERRQ(ierr);
4470         }
4471       }
4472     }
4473   }
4474   ierr = PetscSectionDestroy(&multiRootSec);CHKERRQ(ierr);
4475   ierr = PetscSectionDestroy(&rootIndicesSec);CHKERRQ(ierr);
4476   ierr = PetscFree2(parentIndices,parentValues);CHKERRQ(ierr);
4477   ierr = DMPlexReferenceTreeRestoreChildrenMatrices_Injection(refTree,injRef,&childrenMats);CHKERRQ(ierr);
4478   ierr = PetscFree(rootValues);CHKERRQ(ierr);
4479   ierr = PetscFree3(offsets,offsetsCopy,rowOffsets);CHKERRQ(ierr);
4480   PetscFunctionReturn(0);
4481 }
4482 
4483 #undef __FUNCT__
4484 #define __FUNCT__ "DMPlexTransferVecTree"
4485 PetscErrorCode DMPlexTransferVecTree(DM dmIn, Vec vecIn, DM dmOut, Vec vecOut, PetscSF sfIn, PetscSF sfOut, PetscInt *cidsIn, PetscInt *cidsOut, PetscBool useBCs, PetscReal time)
4486 {
4487   PetscErrorCode ierr;
4488 
4489   PetscFunctionBegin;
4490   if (sfIn) {
4491     Vec vecInLocal;
4492     DM  dmGrad = NULL;
4493     Vec faceGeom = NULL, cellGeom = NULL, grad = NULL;
4494 
4495     ierr = DMGetLocalVector(dmIn,&vecInLocal);CHKERRQ(ierr);
4496     ierr = VecSet(vecInLocal,0.0);CHKERRQ(ierr);
4497     {
4498       PetscInt  numFields, i;
4499 
4500       ierr = DMGetNumFields(dmIn, &numFields);CHKERRQ(ierr);
4501       for (i = 0; i < numFields; i++) {
4502         PetscObject  obj;
4503         PetscClassId classid;
4504 
4505         ierr = DMGetField(dmIn, i, &obj);CHKERRQ(ierr);
4506         ierr = PetscObjectGetClassId(obj, &classid);CHKERRQ(ierr);
4507         if (classid == PETSCFV_CLASSID) {
4508           ierr = DMPlexGetDataFVM(dmIn,(PetscFV)obj,&cellGeom,&faceGeom,&dmGrad);CHKERRQ(ierr);
4509           break;
4510         }
4511       }
4512     }
4513     if (useBCs) {
4514       ierr = DMPlexInsertBoundaryValues(dmIn,PETSC_TRUE,vecInLocal,time,faceGeom,cellGeom,NULL);CHKERRQ(ierr);
4515     }
4516     ierr = DMGlobalToLocalBegin(dmIn,vecIn,INSERT_VALUES,vecInLocal);CHKERRQ(ierr);
4517     ierr = DMGlobalToLocalEnd(dmIn,vecIn,INSERT_VALUES,vecInLocal);CHKERRQ(ierr);
4518     if (dmGrad) {
4519       ierr = DMGetGlobalVector(dmGrad,&grad);CHKERRQ(ierr);
4520       ierr = DMPlexReconstructGradientsFVM(dmIn,vecInLocal,grad);CHKERRQ(ierr);
4521     }
4522     ierr = DMPlexTransferVecTree_Interpolate(dmIn,vecInLocal,dmOut,vecOut,sfIn,cidsIn,grad,cellGeom);CHKERRQ(ierr);
4523     ierr = DMRestoreLocalVector(dmIn,&vecInLocal);CHKERRQ(ierr);
4524     if (dmGrad) {
4525       ierr = DMRestoreGlobalVector(dmGrad,&grad);CHKERRQ(ierr);
4526     }
4527   }
4528   if (sfOut) {
4529     ierr = DMPlexTransferVecTree_Inject(dmIn,vecIn,dmOut,vecOut,sfOut,cidsOut);CHKERRQ(ierr);
4530   }
4531   PetscFunctionReturn(0);
4532 }
4533