Actual source code: plextree.c

petsc-3.13.6 2020-09-29
Report Typos and Errors
  1:  #include <petsc/private/dmpleximpl.h>
  2:  #include <petsc/private/isimpl.h>
  3:  #include <petsc/private/petscfeimpl.h>
  4:  #include <petscsf.h>
  5:  #include <petscds.h>

  7: /** hierarchy routines */

  9: /*@
 10:   DMPlexSetReferenceTree - set the reference tree for hierarchically non-conforming meshes.

 12:   Not collective

 14:   Input Parameters:
 15: + dm - The DMPlex object
 16: - ref - The reference tree DMPlex object

 18:   Level: intermediate

 20: .seealso: DMPlexGetReferenceTree(), DMPlexCreateDefaultReferenceTree()
 21: @*/
 22: PetscErrorCode DMPlexSetReferenceTree(DM dm, DM ref)
 23: {
 24:   DM_Plex        *mesh = (DM_Plex *)dm->data;
 25:   PetscErrorCode  ierr;

 30:   PetscObjectReference((PetscObject)ref);
 31:   DMDestroy(&mesh->referenceTree);
 32:   mesh->referenceTree = ref;
 33:   return(0);
 34: }

 36: /*@
 37:   DMPlexGetReferenceTree - get the reference tree for hierarchically non-conforming meshes.

 39:   Not collective

 41:   Input Parameters:
 42: . dm - The DMPlex object

 44:   Output Parameters:
 45: . ref - The reference tree DMPlex object

 47:   Level: intermediate

 49: .seealso: DMPlexSetReferenceTree(), DMPlexCreateDefaultReferenceTree()
 50: @*/
 51: PetscErrorCode DMPlexGetReferenceTree(DM dm, DM *ref)
 52: {
 53:   DM_Plex        *mesh = (DM_Plex *)dm->data;

 58:   *ref = mesh->referenceTree;
 59:   return(0);
 60: }

 62: static PetscErrorCode DMPlexReferenceTreeGetChildSymmetry_Default(DM dm, PetscInt parent, PetscInt parentOrientA, PetscInt childOrientA, PetscInt childA, PetscInt parentOrientB, PetscInt *childOrientB, PetscInt *childB)
 63: {
 64:   PetscInt       coneSize, dStart, dEnd, dim, ABswap, oAvert, oBvert, ABswapVert;

 68:   if (parentOrientA == parentOrientB) {
 69:     if (childOrientB) *childOrientB = childOrientA;
 70:     if (childB) *childB = childA;
 71:     return(0);
 72:   }
 73:   for (dim = 0; dim < 3; dim++) {
 74:     DMPlexGetDepthStratum(dm,dim,&dStart,&dEnd);
 75:     if (parent >= dStart && parent <= dEnd) {
 76:       break;
 77:     }
 78:   }
 79:   if (dim > 2) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot perform child symmetry for %d-cells",dim);
 80:   if (!dim) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"A vertex has no children");
 81:   if (childA < dStart || childA >= dEnd) {
 82:     /* this is a lower-dimensional child: bootstrap */
 83:     PetscInt size, i, sA = -1, sB, sOrientB, sConeSize;
 84:     const PetscInt *supp, *coneA, *coneB, *oA, *oB;

 86:     DMPlexGetSupportSize(dm,childA,&size);
 87:     DMPlexGetSupport(dm,childA,&supp);

 89:     /* find a point sA in supp(childA) that has the same parent */
 90:     for (i = 0; i < size; i++) {
 91:       PetscInt sParent;

 93:       sA   = supp[i];
 94:       if (sA == parent) continue;
 95:       DMPlexGetTreeParent(dm,sA,&sParent,NULL);
 96:       if (sParent == parent) {
 97:         break;
 98:       }
 99:     }
100:     if (i == size) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"could not find support in children");
101:     /* find out which point sB is in an equivalent position to sA under
102:      * parentOrientB */
103:     DMPlexReferenceTreeGetChildSymmetry_Default(dm,parent,parentOrientA,0,sA,parentOrientB,&sOrientB,&sB);
104:     DMPlexGetConeSize(dm,sA,&sConeSize);
105:     DMPlexGetCone(dm,sA,&coneA);
106:     DMPlexGetCone(dm,sB,&coneB);
107:     DMPlexGetConeOrientation(dm,sA,&oA);
108:     DMPlexGetConeOrientation(dm,sB,&oB);
109:     /* step through the cone of sA in natural order */
110:     for (i = 0; i < sConeSize; i++) {
111:       if (coneA[i] == childA) {
112:         /* if childA is at position i in coneA,
113:          * then we want the point that is at sOrientB*i in coneB */
114:         PetscInt j = (sOrientB >= 0) ? ((sOrientB + i) % sConeSize) : ((sConeSize -(sOrientB+1) - i) % sConeSize);
115:         if (childB) *childB = coneB[j];
116:         if (childOrientB) {
117:           PetscInt oBtrue;

119:           DMPlexGetConeSize(dm,childA,&coneSize);
120:           /* compose sOrientB and oB[j] */
121:           if (coneSize != 0 && coneSize != 2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expected a vertex or an edge");
122:           /* we may have to flip an edge */
123:           oBtrue        = coneSize ? ((sOrientB >= 0) ? oB[j] : -(oB[j] + 2)) : 0;
124:           ABswap        = DihedralSwap(coneSize,oA[i],oBtrue);
125:           *childOrientB = DihedralCompose(coneSize,childOrientA,ABswap);
126:         }
127:         break;
128:       }
129:     }
130:     if (i == sConeSize) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"support cone mismatch");
131:     return(0);
132:   }
133:   /* get the cone size and symmetry swap */
134:   DMPlexGetConeSize(dm,parent,&coneSize);
135:   ABswap = DihedralSwap(coneSize, parentOrientA, parentOrientB);
136:   if (dim == 2) {
137:     /* orientations refer to cones: we want them to refer to vertices:
138:      * if it's a rotation, they are the same, but if the order is reversed, a
139:      * permutation that puts side i first does *not* put vertex i first */
140:     oAvert     = (parentOrientA >= 0) ? parentOrientA : -((-parentOrientA % coneSize) + 1);
141:     oBvert     = (parentOrientB >= 0) ? parentOrientB : -((-parentOrientB % coneSize) + 1);
142:     ABswapVert = DihedralSwap(coneSize, oAvert, oBvert);
143:   } else {
144:     ABswapVert = ABswap;
145:   }
146:   if (childB) {
147:     /* assume that each child corresponds to a vertex, in the same order */
148:     PetscInt p, posA = -1, numChildren, i;
149:     const PetscInt *children;

151:     /* count which position the child is in */
152:     DMPlexGetTreeChildren(dm,parent,&numChildren,&children);
153:     for (i = 0; i < numChildren; i++) {
154:       p = children[i];
155:       if (p == childA) {
156:         posA = i;
157:         break;
158:       }
159:     }
160:     if (posA >= coneSize) {
161:       /* this is the triangle in the middle of a uniformly refined triangle: it
162:        * is invariant */
163:       if (dim != 2 || posA != 3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Expected a middle triangle, got something else");
164:       *childB = childA;
165:     }
166:     else {
167:       /* figure out position B by applying ABswapVert */
168:       PetscInt posB;

170:       posB = (ABswapVert >= 0) ? ((ABswapVert + posA) % coneSize) : ((coneSize -(ABswapVert + 1) - posA) % coneSize);
171:       if (childB) *childB = children[posB];
172:     }
173:   }
174:   if (childOrientB) *childOrientB = DihedralCompose(coneSize,childOrientA,ABswap);
175:   return(0);
176: }

178: /*@
179:   DMPlexReferenceTreeGetChildSymmetry - Given a reference tree, transform a childid and orientation from one parent frame to another

181:   Input Parameters:
182: + dm - the reference tree DMPlex object
183: . parent - the parent point
184: . parentOrientA - the reference orientation for describing the parent
185: . childOrientA - the reference orientation for describing the child
186: . childA - the reference childID for describing the child
187: - parentOrientB - the new orientation for describing the parent

189:   Output Parameters:
190: + childOrientB - if not NULL, set to the new oreintation for describing the child
191: - childB - if not NULL, the new childID for describing the child

193:   Level: developer

195: .seealso: DMPlexGetReferenceTree(), DMPlexSetReferenceTree(), DMPlexSetTree()
196: @*/
197: PetscErrorCode DMPlexReferenceTreeGetChildSymmetry(DM dm, PetscInt parent, PetscInt parentOrientA, PetscInt childOrientA, PetscInt childA, PetscInt parentOrientB, PetscInt *childOrientB, PetscInt *childB)
198: {
199:   DM_Plex        *mesh = (DM_Plex *)dm->data;

204:   if (!mesh->getchildsymmetry) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"DMPlexReferenceTreeGetChildSymmetry not implemented");
205:   mesh->getchildsymmetry(dm,parent,parentOrientA,childOrientA,childA,parentOrientB,childOrientB,childB);
206:   return(0);
207: }

209: static PetscErrorCode DMPlexSetTree_Internal(DM,PetscSection,PetscInt*,PetscInt*,PetscBool,PetscBool);

211: PetscErrorCode DMPlexCreateReferenceTree_SetTree(DM dm, PetscSection parentSection, PetscInt parents[], PetscInt childIDs[])
212: {

216:   DMPlexSetTree_Internal(dm,parentSection,parents,childIDs,PETSC_TRUE,PETSC_FALSE);
217:   return(0);
218: }

220: PetscErrorCode DMPlexCreateReferenceTree_Union(DM K, DM Kref, const char *labelName, DM *ref)
221: {
222:   MPI_Comm       comm;
223:   PetscInt       dim, p, pStart, pEnd, pRefStart, pRefEnd, d, offset, parentSize, *parents, *childIDs;
224:   PetscInt      *permvals, *unionCones, *coneSizes, *unionOrientations, numUnionPoints, *numDimPoints, numCones, numVerts;
225:   DMLabel        identity, identityRef;
226:   PetscSection   unionSection, unionConeSection, parentSection;
227:   PetscScalar   *unionCoords;
228:   IS             perm;

232:   comm = PetscObjectComm((PetscObject)K);
233:   DMGetDimension(K, &dim);
234:   DMPlexGetChart(K, &pStart, &pEnd);
235:   DMGetLabel(K, labelName, &identity);
236:   DMGetLabel(Kref, labelName, &identityRef);
237:   DMPlexGetChart(Kref, &pRefStart, &pRefEnd);
238:   PetscSectionCreate(comm, &unionSection);
239:   PetscSectionSetChart(unionSection, 0, (pEnd - pStart) + (pRefEnd - pRefStart));
240:   /* count points that will go in the union */
241:   for (p = pStart; p < pEnd; p++) {
242:     PetscSectionSetDof(unionSection, p - pStart, 1);
243:   }
244:   for (p = pRefStart; p < pRefEnd; p++) {
245:     PetscInt q, qSize;
246:     DMLabelGetValue(identityRef, p, &q);
247:     DMLabelGetStratumSize(identityRef, q, &qSize);
248:     if (qSize > 1) {
249:       PetscSectionSetDof(unionSection, p - pRefStart + (pEnd - pStart), 1);
250:     }
251:   }
252:   PetscMalloc1(pEnd - pStart + pRefEnd - pRefStart,&permvals);
253:   offset = 0;
254:   /* stratify points in the union by topological dimension */
255:   for (d = 0; d <= dim; d++) {
256:     PetscInt cStart, cEnd, c;

258:     DMPlexGetHeightStratum(K, d, &cStart, &cEnd);
259:     for (c = cStart; c < cEnd; c++) {
260:       permvals[offset++] = c;
261:     }

263:     DMPlexGetHeightStratum(Kref, d, &cStart, &cEnd);
264:     for (c = cStart; c < cEnd; c++) {
265:       permvals[offset++] = c + (pEnd - pStart);
266:     }
267:   }
268:   ISCreateGeneral(comm, (pEnd - pStart) + (pRefEnd - pRefStart), permvals, PETSC_OWN_POINTER, &perm);
269:   PetscSectionSetPermutation(unionSection,perm);
270:   PetscSectionSetUp(unionSection);
271:   PetscSectionGetStorageSize(unionSection,&numUnionPoints);
272:   PetscMalloc2(numUnionPoints,&coneSizes,dim+1,&numDimPoints);
273:   /* count dimension points */
274:   for (d = 0; d <= dim; d++) {
275:     PetscInt cStart, cOff, cOff2;
276:     DMPlexGetHeightStratum(K,d,&cStart,NULL);
277:     PetscSectionGetOffset(unionSection,cStart-pStart,&cOff);
278:     if (d < dim) {
279:       DMPlexGetHeightStratum(K,d+1,&cStart,NULL);
280:       PetscSectionGetOffset(unionSection,cStart-pStart,&cOff2);
281:     }
282:     else {
283:       cOff2 = numUnionPoints;
284:     }
285:     numDimPoints[dim - d] = cOff2 - cOff;
286:   }
287:   PetscSectionCreate(comm, &unionConeSection);
288:   PetscSectionSetChart(unionConeSection, 0, numUnionPoints);
289:   /* count the cones in the union */
290:   for (p = pStart; p < pEnd; p++) {
291:     PetscInt dof, uOff;

293:     DMPlexGetConeSize(K, p, &dof);
294:     PetscSectionGetOffset(unionSection, p - pStart,&uOff);
295:     PetscSectionSetDof(unionConeSection, uOff, dof);
296:     coneSizes[uOff] = dof;
297:   }
298:   for (p = pRefStart; p < pRefEnd; p++) {
299:     PetscInt dof, uDof, uOff;

301:     DMPlexGetConeSize(Kref, p, &dof);
302:     PetscSectionGetDof(unionSection, p - pRefStart + (pEnd - pStart),&uDof);
303:     PetscSectionGetOffset(unionSection, p - pRefStart + (pEnd - pStart),&uOff);
304:     if (uDof) {
305:       PetscSectionSetDof(unionConeSection, uOff, dof);
306:       coneSizes[uOff] = dof;
307:     }
308:   }
309:   PetscSectionSetUp(unionConeSection);
310:   PetscSectionGetStorageSize(unionConeSection,&numCones);
311:   PetscMalloc2(numCones,&unionCones,numCones,&unionOrientations);
312:   /* write the cones in the union */
313:   for (p = pStart; p < pEnd; p++) {
314:     PetscInt dof, uOff, c, cOff;
315:     const PetscInt *cone, *orientation;

317:     DMPlexGetConeSize(K, p, &dof);
318:     DMPlexGetCone(K, p, &cone);
319:     DMPlexGetConeOrientation(K, p, &orientation);
320:     PetscSectionGetOffset(unionSection, p - pStart,&uOff);
321:     PetscSectionGetOffset(unionConeSection,uOff,&cOff);
322:     for (c = 0; c < dof; c++) {
323:       PetscInt e, eOff;
324:       e                           = cone[c];
325:       PetscSectionGetOffset(unionSection, e - pStart, &eOff);
326:       unionCones[cOff + c]        = eOff;
327:       unionOrientations[cOff + c] = orientation[c];
328:     }
329:   }
330:   for (p = pRefStart; p < pRefEnd; p++) {
331:     PetscInt dof, uDof, uOff, c, cOff;
332:     const PetscInt *cone, *orientation;

334:     DMPlexGetConeSize(Kref, p, &dof);
335:     DMPlexGetCone(Kref, p, &cone);
336:     DMPlexGetConeOrientation(Kref, p, &orientation);
337:     PetscSectionGetDof(unionSection, p - pRefStart + (pEnd - pStart),&uDof);
338:     PetscSectionGetOffset(unionSection, p - pRefStart + (pEnd - pStart),&uOff);
339:     if (uDof) {
340:       PetscSectionGetOffset(unionConeSection,uOff,&cOff);
341:       for (c = 0; c < dof; c++) {
342:         PetscInt e, eOff, eDof;

344:         e    = cone[c];
345:         PetscSectionGetDof(unionSection, e - pRefStart + (pEnd - pStart),&eDof);
346:         if (eDof) {
347:           PetscSectionGetOffset(unionSection, e - pRefStart + (pEnd - pStart), &eOff);
348:         }
349:         else {
350:           DMLabelGetValue(identityRef, e, &e);
351:           PetscSectionGetOffset(unionSection, e - pStart, &eOff);
352:         }
353:         unionCones[cOff + c]        = eOff;
354:         unionOrientations[cOff + c] = orientation[c];
355:       }
356:     }
357:   }
358:   /* get the coordinates */
359:   {
360:     PetscInt     vStart, vEnd, vRefStart, vRefEnd, v, vDof, vOff;
361:     PetscSection KcoordsSec, KrefCoordsSec;
362:     Vec          KcoordsVec, KrefCoordsVec;
363:     PetscScalar *Kcoords;

365:     DMGetCoordinateSection(K, &KcoordsSec);
366:     DMGetCoordinatesLocal(K, &KcoordsVec);
367:     DMGetCoordinateSection(Kref, &KrefCoordsSec);
368:     DMGetCoordinatesLocal(Kref, &KrefCoordsVec);

370:     numVerts = numDimPoints[0];
371:     PetscMalloc1(numVerts * dim,&unionCoords);
372:     DMPlexGetDepthStratum(K,0,&vStart,&vEnd);

374:     offset = 0;
375:     for (v = vStart; v < vEnd; v++) {
376:       PetscSectionGetOffset(unionSection,v - pStart,&vOff);
377:       VecGetValuesSection(KcoordsVec, KcoordsSec, v, &Kcoords);
378:       for (d = 0; d < dim; d++) {
379:         unionCoords[offset * dim + d] = Kcoords[d];
380:       }
381:       offset++;
382:     }
383:     DMPlexGetDepthStratum(Kref,0,&vRefStart,&vRefEnd);
384:     for (v = vRefStart; v < vRefEnd; v++) {
385:       PetscSectionGetDof(unionSection,v - pRefStart + (pEnd - pStart),&vDof);
386:       PetscSectionGetOffset(unionSection,v - pRefStart + (pEnd - pStart),&vOff);
387:       VecGetValuesSection(KrefCoordsVec, KrefCoordsSec, v, &Kcoords);
388:       if (vDof) {
389:         for (d = 0; d < dim; d++) {
390:           unionCoords[offset * dim + d] = Kcoords[d];
391:         }
392:         offset++;
393:       }
394:     }
395:   }
396:   DMCreate(comm,ref);
397:   DMSetType(*ref,DMPLEX);
398:   DMSetDimension(*ref,dim);
399:   DMPlexCreateFromDAG(*ref,dim,numDimPoints,coneSizes,unionCones,unionOrientations,unionCoords);
400:   /* set the tree */
401:   PetscSectionCreate(comm,&parentSection);
402:   PetscSectionSetChart(parentSection,0,numUnionPoints);
403:   for (p = pRefStart; p < pRefEnd; p++) {
404:     PetscInt uDof, uOff;

406:     PetscSectionGetDof(unionSection, p - pRefStart + (pEnd - pStart),&uDof);
407:     PetscSectionGetOffset(unionSection, p - pRefStart + (pEnd - pStart),&uOff);
408:     if (uDof) {
409:       PetscSectionSetDof(parentSection,uOff,1);
410:     }
411:   }
412:   PetscSectionSetUp(parentSection);
413:   PetscSectionGetStorageSize(parentSection,&parentSize);
414:   PetscMalloc2(parentSize,&parents,parentSize,&childIDs);
415:   for (p = pRefStart; p < pRefEnd; p++) {
416:     PetscInt uDof, uOff;

418:     PetscSectionGetDof(unionSection, p - pRefStart + (pEnd - pStart),&uDof);
419:     PetscSectionGetOffset(unionSection, p - pRefStart + (pEnd - pStart),&uOff);
420:     if (uDof) {
421:       PetscInt pOff, parent, parentU;
422:       PetscSectionGetOffset(parentSection,uOff,&pOff);
423:       DMLabelGetValue(identityRef,p,&parent);
424:       PetscSectionGetOffset(unionSection, parent - pStart,&parentU);
425:       parents[pOff] = parentU;
426:       childIDs[pOff] = uOff;
427:     }
428:   }
429:   DMPlexCreateReferenceTree_SetTree(*ref,parentSection,parents,childIDs);
430:   PetscSectionDestroy(&parentSection);
431:   PetscFree2(parents,childIDs);

433:   /* clean up */
434:   PetscSectionDestroy(&unionSection);
435:   PetscSectionDestroy(&unionConeSection);
436:   ISDestroy(&perm);
437:   PetscFree(unionCoords);
438:   PetscFree2(unionCones,unionOrientations);
439:   PetscFree2(coneSizes,numDimPoints);
440:   return(0);
441: }

443: /*@
444:   DMPlexCreateDefaultReferenceTree - create a reference tree for isotropic hierarchical mesh refinement.

446:   Collective

448:   Input Parameters:
449: + comm    - the MPI communicator
450: . dim     - the spatial dimension
451: - simplex - Flag for simplex, otherwise use a tensor-product cell

453:   Output Parameters:
454: . ref     - the reference tree DMPlex object

456:   Level: intermediate

458: .seealso: DMPlexSetReferenceTree(), DMPlexGetReferenceTree()
459: @*/
460: PetscErrorCode DMPlexCreateDefaultReferenceTree(MPI_Comm comm, PetscInt dim, PetscBool simplex, DM *ref)
461: {
462:   DM_Plex       *mesh;
463:   DM             K, Kref;
464:   PetscInt       p, pStart, pEnd;
465:   DMLabel        identity;

469: #if 1
470:   comm = PETSC_COMM_SELF;
471: #endif
472:   /* create a reference element */
473:   DMPlexCreateReferenceCell(comm, dim, simplex, &K);
474:   DMCreateLabel(K, "identity");
475:   DMGetLabel(K, "identity", &identity);
476:   DMPlexGetChart(K, &pStart, &pEnd);
477:   for (p = pStart; p < pEnd; p++) {
478:     DMLabelSetValue(identity, p, p);
479:   }
480:   /* refine it */
481:   DMRefine(K,comm,&Kref);

483:   /* the reference tree is the union of these two, without duplicating
484:    * points that appear in both */
485:   DMPlexCreateReferenceTree_Union(K, Kref, "identity", ref);
486:   mesh = (DM_Plex *) (*ref)->data;
487:   mesh->getchildsymmetry = DMPlexReferenceTreeGetChildSymmetry_Default;
488:   DMDestroy(&K);
489:   DMDestroy(&Kref);
490:   return(0);
491: }

493: static PetscErrorCode DMPlexTreeSymmetrize(DM dm)
494: {
495:   DM_Plex        *mesh = (DM_Plex *)dm->data;
496:   PetscSection   childSec, pSec;
497:   PetscInt       p, pSize, cSize, parMax = PETSC_MIN_INT, parMin = PETSC_MAX_INT;
498:   PetscInt       *offsets, *children, pStart, pEnd;

503:   PetscSectionDestroy(&mesh->childSection);
504:   PetscFree(mesh->children);
505:   pSec = mesh->parentSection;
506:   if (!pSec) return(0);
507:   PetscSectionGetStorageSize(pSec,&pSize);
508:   for (p = 0; p < pSize; p++) {
509:     PetscInt par = mesh->parents[p];

511:     parMax = PetscMax(parMax,par+1);
512:     parMin = PetscMin(parMin,par);
513:   }
514:   if (parMin > parMax) {
515:     parMin = -1;
516:     parMax = -1;
517:   }
518:   PetscSectionCreate(PetscObjectComm((PetscObject)pSec),&childSec);
519:   PetscSectionSetChart(childSec,parMin,parMax);
520:   for (p = 0; p < pSize; p++) {
521:     PetscInt par = mesh->parents[p];

523:     PetscSectionAddDof(childSec,par,1);
524:   }
525:   PetscSectionSetUp(childSec);
526:   PetscSectionGetStorageSize(childSec,&cSize);
527:   PetscMalloc1(cSize,&children);
528:   PetscCalloc1(parMax-parMin,&offsets);
529:   PetscSectionGetChart(pSec,&pStart,&pEnd);
530:   for (p = pStart; p < pEnd; p++) {
531:     PetscInt dof, off, i;

533:     PetscSectionGetDof(pSec,p,&dof);
534:     PetscSectionGetOffset(pSec,p,&off);
535:     for (i = 0; i < dof; i++) {
536:       PetscInt par = mesh->parents[off + i], cOff;

538:       PetscSectionGetOffset(childSec,par,&cOff);
539:       children[cOff + offsets[par-parMin]++] = p;
540:     }
541:   }
542:   mesh->childSection = childSec;
543:   mesh->children = children;
544:   PetscFree(offsets);
545:   return(0);
546: }

548: static PetscErrorCode AnchorsFlatten (PetscSection section, IS is, PetscSection *sectionNew, IS *isNew)
549: {
550:   PetscInt       pStart, pEnd, size, sizeNew, i, p, *valsNew = NULL;
551:   const PetscInt *vals;
552:   PetscSection   secNew;
553:   PetscBool      anyNew, globalAnyNew;
554:   PetscBool      compress;

558:   PetscSectionGetChart(section,&pStart,&pEnd);
559:   ISGetLocalSize(is,&size);
560:   ISGetIndices(is,&vals);
561:   PetscSectionCreate(PetscObjectComm((PetscObject)section),&secNew);
562:   PetscSectionSetChart(secNew,pStart,pEnd);
563:   for (i = 0; i < size; i++) {
564:     PetscInt dof;

566:     p = vals[i];
567:     if (p < pStart || p >= pEnd) continue;
568:     PetscSectionGetDof(section, p, &dof);
569:     if (dof) break;
570:   }
571:   if (i == size) {
572:     PetscSectionSetUp(secNew);
573:     anyNew   = PETSC_FALSE;
574:     compress = PETSC_FALSE;
575:     sizeNew  = 0;
576:   }
577:   else {
578:     anyNew = PETSC_TRUE;
579:     for (p = pStart; p < pEnd; p++) {
580:       PetscInt dof, off;

582:       PetscSectionGetDof(section, p, &dof);
583:       PetscSectionGetOffset(section, p, &off);
584:       for (i = 0; i < dof; i++) {
585:         PetscInt q = vals[off + i], qDof = 0;

587:         if (q >= pStart && q < pEnd) {
588:           PetscSectionGetDof(section, q, &qDof);
589:         }
590:         if (qDof) {
591:           PetscSectionAddDof(secNew, p, qDof);
592:         }
593:         else {
594:           PetscSectionAddDof(secNew, p, 1);
595:         }
596:       }
597:     }
598:     PetscSectionSetUp(secNew);
599:     PetscSectionGetStorageSize(secNew,&sizeNew);
600:     PetscMalloc1(sizeNew,&valsNew);
601:     compress = PETSC_FALSE;
602:     for (p = pStart; p < pEnd; p++) {
603:       PetscInt dof, off, count, offNew, dofNew;

605:       PetscSectionGetDof(section, p, &dof);
606:       PetscSectionGetOffset(section, p, &off);
607:       PetscSectionGetDof(secNew, p, &dofNew);
608:       PetscSectionGetOffset(secNew, p, &offNew);
609:       count = 0;
610:       for (i = 0; i < dof; i++) {
611:         PetscInt q = vals[off + i], qDof = 0, qOff = 0, j;

613:         if (q >= pStart && q < pEnd) {
614:           PetscSectionGetDof(section, q, &qDof);
615:           PetscSectionGetOffset(section, q, &qOff);
616:         }
617:         if (qDof) {
618:           PetscInt oldCount = count;

620:           for (j = 0; j < qDof; j++) {
621:             PetscInt k, r = vals[qOff + j];

623:             for (k = 0; k < oldCount; k++) {
624:               if (valsNew[offNew + k] == r) {
625:                 break;
626:               }
627:             }
628:             if (k == oldCount) {
629:               valsNew[offNew + count++] = r;
630:             }
631:           }
632:         }
633:         else {
634:           PetscInt k, oldCount = count;

636:           for (k = 0; k < oldCount; k++) {
637:             if (valsNew[offNew + k] == q) {
638:               break;
639:             }
640:           }
641:           if (k == oldCount) {
642:             valsNew[offNew + count++] = q;
643:           }
644:         }
645:       }
646:       if (count < dofNew) {
647:         PetscSectionSetDof(secNew, p, count);
648:         compress = PETSC_TRUE;
649:       }
650:     }
651:   }
652:   ISRestoreIndices(is,&vals);
653:   MPIU_Allreduce(&anyNew,&globalAnyNew,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)secNew));
654:   if (!globalAnyNew) {
655:     PetscSectionDestroy(&secNew);
656:     *sectionNew = NULL;
657:     *isNew = NULL;
658:   }
659:   else {
660:     PetscBool globalCompress;

662:     MPIU_Allreduce(&compress,&globalCompress,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)secNew));
663:     if (compress) {
664:       PetscSection secComp;
665:       PetscInt *valsComp = NULL;

667:       PetscSectionCreate(PetscObjectComm((PetscObject)section),&secComp);
668:       PetscSectionSetChart(secComp,pStart,pEnd);
669:       for (p = pStart; p < pEnd; p++) {
670:         PetscInt dof;

672:         PetscSectionGetDof(secNew, p, &dof);
673:         PetscSectionSetDof(secComp, p, dof);
674:       }
675:       PetscSectionSetUp(secComp);
676:       PetscSectionGetStorageSize(secComp,&sizeNew);
677:       PetscMalloc1(sizeNew,&valsComp);
678:       for (p = pStart; p < pEnd; p++) {
679:         PetscInt dof, off, offNew, j;

681:         PetscSectionGetDof(secNew, p, &dof);
682:         PetscSectionGetOffset(secNew, p, &off);
683:         PetscSectionGetOffset(secComp, p, &offNew);
684:         for (j = 0; j < dof; j++) {
685:           valsComp[offNew + j] = valsNew[off + j];
686:         }
687:       }
688:       PetscSectionDestroy(&secNew);
689:       secNew  = secComp;
690:       PetscFree(valsNew);
691:       valsNew = valsComp;
692:     }
693:     ISCreateGeneral(PetscObjectComm((PetscObject)is),sizeNew,valsNew,PETSC_OWN_POINTER,isNew);
694:   }
695:   return(0);
696: }

698: static PetscErrorCode DMPlexCreateAnchors_Tree(DM dm)
699: {
700:   PetscInt       p, pStart, pEnd, *anchors, size;
701:   PetscInt       aMin = PETSC_MAX_INT, aMax = PETSC_MIN_INT;
702:   PetscSection   aSec;
703:   DMLabel        canonLabel;
704:   IS             aIS;

709:   DMPlexGetChart(dm,&pStart,&pEnd);
710:   DMGetLabel(dm,"canonical",&canonLabel);
711:   for (p = pStart; p < pEnd; p++) {
712:     PetscInt parent;

714:     if (canonLabel) {
715:       PetscInt canon;

717:       DMLabelGetValue(canonLabel,p,&canon);
718:       if (p != canon) continue;
719:     }
720:     DMPlexGetTreeParent(dm,p,&parent,NULL);
721:     if (parent != p) {
722:       aMin = PetscMin(aMin,p);
723:       aMax = PetscMax(aMax,p+1);
724:     }
725:   }
726:   if (aMin > aMax) {
727:     aMin = -1;
728:     aMax = -1;
729:   }
730:   PetscSectionCreate(PETSC_COMM_SELF,&aSec);
731:   PetscSectionSetChart(aSec,aMin,aMax);
732:   for (p = aMin; p < aMax; p++) {
733:     PetscInt parent, ancestor = p;

735:     if (canonLabel) {
736:       PetscInt canon;

738:       DMLabelGetValue(canonLabel,p,&canon);
739:       if (p != canon) continue;
740:     }
741:     DMPlexGetTreeParent(dm,p,&parent,NULL);
742:     while (parent != ancestor) {
743:       ancestor = parent;
744:       DMPlexGetTreeParent(dm,ancestor,&parent,NULL);
745:     }
746:     if (ancestor != p) {
747:       PetscInt closureSize, *closure = NULL;

749:       DMPlexGetTransitiveClosure(dm,ancestor,PETSC_TRUE,&closureSize,&closure);
750:       PetscSectionSetDof(aSec,p,closureSize);
751:       DMPlexRestoreTransitiveClosure(dm,ancestor,PETSC_TRUE,&closureSize,&closure);
752:     }
753:   }
754:   PetscSectionSetUp(aSec);
755:   PetscSectionGetStorageSize(aSec,&size);
756:   PetscMalloc1(size,&anchors);
757:   for (p = aMin; p < aMax; p++) {
758:     PetscInt parent, ancestor = p;

760:     if (canonLabel) {
761:       PetscInt canon;

763:       DMLabelGetValue(canonLabel,p,&canon);
764:       if (p != canon) continue;
765:     }
766:     DMPlexGetTreeParent(dm,p,&parent,NULL);
767:     while (parent != ancestor) {
768:       ancestor = parent;
769:       DMPlexGetTreeParent(dm,ancestor,&parent,NULL);
770:     }
771:     if (ancestor != p) {
772:       PetscInt j, closureSize, *closure = NULL, aOff;

774:       PetscSectionGetOffset(aSec,p,&aOff);

776:       DMPlexGetTransitiveClosure(dm,ancestor,PETSC_TRUE,&closureSize,&closure);
777:       for (j = 0; j < closureSize; j++) {
778:         anchors[aOff + j] = closure[2*j];
779:       }
780:       DMPlexRestoreTransitiveClosure(dm,ancestor,PETSC_TRUE,&closureSize,&closure);
781:     }
782:   }
783:   ISCreateGeneral(PETSC_COMM_SELF,size,anchors,PETSC_OWN_POINTER,&aIS);
784:   {
785:     PetscSection aSecNew = aSec;
786:     IS           aISNew  = aIS;

788:     PetscObjectReference((PetscObject)aSec);
789:     PetscObjectReference((PetscObject)aIS);
790:     while (aSecNew) {
791:       PetscSectionDestroy(&aSec);
792:       ISDestroy(&aIS);
793:       aSec    = aSecNew;
794:       aIS     = aISNew;
795:       aSecNew = NULL;
796:       aISNew  = NULL;
797:       AnchorsFlatten(aSec,aIS,&aSecNew,&aISNew);
798:     }
799:   }
800:   DMPlexSetAnchors(dm,aSec,aIS);
801:   PetscSectionDestroy(&aSec);
802:   ISDestroy(&aIS);
803:   return(0);
804: }

806: static PetscErrorCode DMPlexGetTrueSupportSize(DM dm,PetscInt p,PetscInt *dof,PetscInt *numTrueSupp)
807: {

811:   if (numTrueSupp[p] == -1) {
812:     PetscInt i, alldof;
813:     const PetscInt *supp;
814:     PetscInt count = 0;

816:     DMPlexGetSupportSize(dm,p,&alldof);
817:     DMPlexGetSupport(dm,p,&supp);
818:     for (i = 0; i < alldof; i++) {
819:       PetscInt q = supp[i], numCones, j;
820:       const PetscInt *cone;

822:       DMPlexGetConeSize(dm,q,&numCones);
823:       DMPlexGetCone(dm,q,&cone);
824:       for (j = 0; j < numCones; j++) {
825:         if (cone[j] == p) break;
826:       }
827:       if (j < numCones) count++;
828:     }
829:     numTrueSupp[p] = count;
830:   }
831:   *dof = numTrueSupp[p];
832:   return(0);
833: }

835: static PetscErrorCode DMPlexTreeExchangeSupports(DM dm)
836: {
837:   DM_Plex        *mesh = (DM_Plex *)dm->data;
838:   PetscSection   newSupportSection;
839:   PetscInt       newSize, *newSupports, pStart, pEnd, p, d, depth;
840:   PetscInt       *numTrueSupp;
841:   PetscInt       *offsets;

846:   /* symmetrize the hierarchy */
847:   DMPlexGetDepth(dm,&depth);
848:   PetscSectionCreate(PetscObjectComm((PetscObject)(mesh->supportSection)),&newSupportSection);
849:   DMPlexGetChart(dm,&pStart,&pEnd);
850:   PetscSectionSetChart(newSupportSection,pStart,pEnd);
851:   PetscCalloc1(pEnd,&offsets);
852:   PetscMalloc1(pEnd,&numTrueSupp);
853:   for (p = 0; p < pEnd; p++) numTrueSupp[p] = -1;
854:   /* if a point is in the (true) support of q, it should be in the support of
855:    * parent(q) */
856:   for (d = 0; d <= depth; d++) {
857:     DMPlexGetHeightStratum(dm,d,&pStart,&pEnd);
858:     for (p = pStart; p < pEnd; ++p) {
859:       PetscInt dof, q, qdof, parent;

861:       DMPlexGetTrueSupportSize(dm,p,&dof,numTrueSupp);
862:       PetscSectionAddDof(newSupportSection, p, dof);
863:       q    = p;
864:       DMPlexGetTreeParent(dm,q,&parent,NULL);
865:       while (parent != q && parent >= pStart && parent < pEnd) {
866:         q = parent;

868:         DMPlexGetTrueSupportSize(dm,q,&qdof,numTrueSupp);
869:         PetscSectionAddDof(newSupportSection,p,qdof);
870:         PetscSectionAddDof(newSupportSection,q,dof);
871:         DMPlexGetTreeParent(dm,q,&parent,NULL);
872:       }
873:     }
874:   }
875:   PetscSectionSetUp(newSupportSection);
876:   PetscSectionGetStorageSize(newSupportSection,&newSize);
877:   PetscMalloc1(newSize,&newSupports);
878:   for (d = 0; d <= depth; d++) {
879:     DMPlexGetHeightStratum(dm,d,&pStart,&pEnd);
880:     for (p = pStart; p < pEnd; p++) {
881:       PetscInt dof, off, q, qdof, qoff, newDof, newOff, newqOff, i, parent;

883:       PetscSectionGetDof(mesh->supportSection, p, &dof);
884:       PetscSectionGetOffset(mesh->supportSection, p, &off);
885:       PetscSectionGetDof(newSupportSection, p, &newDof);
886:       PetscSectionGetOffset(newSupportSection, p, &newOff);
887:       for (i = 0; i < dof; i++) {
888:         PetscInt numCones, j;
889:         const PetscInt *cone;
890:         PetscInt q = mesh->supports[off + i];

892:         DMPlexGetConeSize(dm,q,&numCones);
893:         DMPlexGetCone(dm,q,&cone);
894:         for (j = 0; j < numCones; j++) {
895:           if (cone[j] == p) break;
896:         }
897:         if (j < numCones) newSupports[newOff+offsets[p]++] = q;
898:       }
899:       mesh->maxSupportSize = PetscMax(mesh->maxSupportSize,newDof);

901:       q    = p;
902:       DMPlexGetTreeParent(dm,q,&parent,NULL);
903:       while (parent != q && parent >= pStart && parent < pEnd) {
904:         q = parent;
905:         PetscSectionGetDof(mesh->supportSection, q, &qdof);
906:         PetscSectionGetOffset(mesh->supportSection, q, &qoff);
907:         PetscSectionGetOffset(newSupportSection, q, &newqOff);
908:         for (i = 0; i < qdof; i++) {
909:           PetscInt numCones, j;
910:           const PetscInt *cone;
911:           PetscInt r = mesh->supports[qoff + i];

913:           DMPlexGetConeSize(dm,r,&numCones);
914:           DMPlexGetCone(dm,r,&cone);
915:           for (j = 0; j < numCones; j++) {
916:             if (cone[j] == q) break;
917:           }
918:           if (j < numCones) newSupports[newOff+offsets[p]++] = r;
919:         }
920:         for (i = 0; i < dof; i++) {
921:           PetscInt numCones, j;
922:           const PetscInt *cone;
923:           PetscInt r = mesh->supports[off + i];

925:           DMPlexGetConeSize(dm,r,&numCones);
926:           DMPlexGetCone(dm,r,&cone);
927:           for (j = 0; j < numCones; j++) {
928:             if (cone[j] == p) break;
929:           }
930:           if (j < numCones) newSupports[newqOff+offsets[q]++] = r;
931:         }
932:         DMPlexGetTreeParent(dm,q,&parent,NULL);
933:       }
934:     }
935:   }
936:   PetscSectionDestroy(&mesh->supportSection);
937:   mesh->supportSection = newSupportSection;
938:   PetscFree(mesh->supports);
939:   mesh->supports = newSupports;
940:   PetscFree(offsets);
941:   PetscFree(numTrueSupp);

943:   return(0);
944: }

946: static PetscErrorCode DMPlexComputeAnchorMatrix_Tree_Direct(DM,PetscSection,PetscSection,Mat);
947: static PetscErrorCode DMPlexComputeAnchorMatrix_Tree_FromReference(DM,PetscSection,PetscSection,Mat);

949: static PetscErrorCode DMPlexSetTree_Internal(DM dm, PetscSection parentSection, PetscInt *parents, PetscInt *childIDs, PetscBool computeCanonical, PetscBool exchangeSupports)
950: {
951:   DM_Plex       *mesh = (DM_Plex *)dm->data;
952:   DM             refTree;
953:   PetscInt       size;

959:   PetscObjectReference((PetscObject)parentSection);
960:   PetscSectionDestroy(&mesh->parentSection);
961:   mesh->parentSection = parentSection;
962:   PetscSectionGetStorageSize(parentSection,&size);
963:   if (parents != mesh->parents) {
964:     PetscFree(mesh->parents);
965:     PetscMalloc1(size,&mesh->parents);
966:     PetscArraycpy(mesh->parents, parents, size);
967:   }
968:   if (childIDs != mesh->childIDs) {
969:     PetscFree(mesh->childIDs);
970:     PetscMalloc1(size,&mesh->childIDs);
971:     PetscArraycpy(mesh->childIDs, childIDs, size);
972:   }
973:   DMPlexGetReferenceTree(dm,&refTree);
974:   if (refTree) {
975:     DMLabel canonLabel;

977:     DMGetLabel(refTree,"canonical",&canonLabel);
978:     if (canonLabel) {
979:       PetscInt i;

981:       for (i = 0; i < size; i++) {
982:         PetscInt canon;
983:         DMLabelGetValue(canonLabel, mesh->childIDs[i], &canon);
984:         if (canon >= 0) {
985:           mesh->childIDs[i] = canon;
986:         }
987:       }
988:     }
989:     mesh->computeanchormatrix = DMPlexComputeAnchorMatrix_Tree_FromReference;
990:   } else {
991:     mesh->computeanchormatrix = DMPlexComputeAnchorMatrix_Tree_Direct;
992:   }
993:   DMPlexTreeSymmetrize(dm);
994:   if (computeCanonical) {
995:     PetscInt d, dim;

997:     /* add the canonical label */
998:     DMGetDimension(dm,&dim);
999:     DMCreateLabel(dm,"canonical");
1000:     for (d = 0; d <= dim; d++) {
1001:       PetscInt p, dStart, dEnd, canon = -1, cNumChildren;
1002:       const PetscInt *cChildren;

1004:       DMPlexGetDepthStratum(dm,d,&dStart,&dEnd);
1005:       for (p = dStart; p < dEnd; p++) {
1006:         DMPlexGetTreeChildren(dm,p,&cNumChildren,&cChildren);
1007:         if (cNumChildren) {
1008:           canon = p;
1009:           break;
1010:         }
1011:       }
1012:       if (canon == -1) continue;
1013:       for (p = dStart; p < dEnd; p++) {
1014:         PetscInt numChildren, i;
1015:         const PetscInt *children;

1017:         DMPlexGetTreeChildren(dm,p,&numChildren,&children);
1018:         if (numChildren) {
1019:           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);
1020:           DMSetLabelValue(dm,"canonical",p,canon);
1021:           for (i = 0; i < numChildren; i++) {
1022:             DMSetLabelValue(dm,"canonical",children[i],cChildren[i]);
1023:           }
1024:         }
1025:       }
1026:     }
1027:   }
1028:   if (exchangeSupports) {
1029:     DMPlexTreeExchangeSupports(dm);
1030:   }
1031:   mesh->createanchors = DMPlexCreateAnchors_Tree;
1032:   /* reset anchors */
1033:   DMPlexSetAnchors(dm,NULL,NULL);
1034:   return(0);
1035: }

1037: /*@
1038:   DMPlexSetTree - set the tree that describes the hierarchy of non-conforming mesh points.  This routine also creates
1039:   the point-to-point constraints determined by the tree: a point is constained to the points in the closure of its
1040:   tree root.

1042:   Collective on dm

1044:   Input Parameters:
1045: + dm - the DMPlex object
1046: . parentSection - a section describing the tree: a point has a parent if it has 1 dof in the section; the section
1047:                   offset indexes the parent and childID list; the reference count of parentSection is incremented
1048: . parents - a list of the point parents; copied, can be destroyed
1049: - childIDs - identifies the relationship of the child point to the parent point; if there is a reference tree, then
1050:              the child corresponds to the point in the reference tree with index childIDs; copied, can be destroyed

1052:   Level: intermediate

1054: .seealso: DMPlexGetTree(), DMPlexSetReferenceTree(), DMPlexSetAnchors(), DMPlexGetTreeParent(), DMPlexGetTreeChildren()
1055: @*/
1056: PetscErrorCode DMPlexSetTree(DM dm, PetscSection parentSection, PetscInt parents[], PetscInt childIDs[])
1057: {

1061:   DMPlexSetTree_Internal(dm,parentSection,parents,childIDs,PETSC_FALSE,PETSC_TRUE);
1062:   return(0);
1063: }

1065: /*@
1066:   DMPlexGetTree - get the tree that describes the hierarchy of non-conforming mesh points.
1067:   Collective on dm

1069:   Input Parameters:
1070: . dm - the DMPlex object

1072:   Output Parameters:
1073: + parentSection - a section describing the tree: a point has a parent if it has 1 dof in the section; the section
1074:                   offset indexes the parent and childID list
1075: . parents - a list of the point parents
1076: . childIDs - identifies the relationship of the child point to the parent point; if there is a reference tree, then
1077:              the child corresponds to the point in the reference tree with index childID
1078: . childSection - the inverse of the parent section
1079: - children - a list of the point children

1081:   Level: intermediate

1083: .seealso: DMPlexSetTree(), DMPlexSetReferenceTree(), DMPlexSetAnchors(), DMPlexGetTreeParent(), DMPlexGetTreeChildren()
1084: @*/
1085: PetscErrorCode DMPlexGetTree(DM dm, PetscSection *parentSection, PetscInt *parents[], PetscInt *childIDs[], PetscSection *childSection, PetscInt *children[])
1086: {
1087:   DM_Plex        *mesh = (DM_Plex *)dm->data;

1091:   if (parentSection) *parentSection = mesh->parentSection;
1092:   if (parents)       *parents       = mesh->parents;
1093:   if (childIDs)      *childIDs      = mesh->childIDs;
1094:   if (childSection)  *childSection  = mesh->childSection;
1095:   if (children)      *children      = mesh->children;
1096:   return(0);
1097: }

1099: /*@
1100:   DMPlexGetTreeParent - get the parent of a point in the tree describing the point hierarchy (not the DAG)

1102:   Input Parameters:
1103: + dm - the DMPlex object
1104: - point - the query point

1106:   Output Parameters:
1107: + parent - if not NULL, set to the parent of the point, or the point itself if the point does not have a parent
1108: - childID - if not NULL, set to the child ID of the point with respect to its parent, or 0 if the point
1109:             does not have a parent

1111:   Level: intermediate

1113: .seealso: DMPlexSetTree(), DMPlexGetTree(), DMPlexGetTreeChildren()
1114: @*/
1115: PetscErrorCode DMPlexGetTreeParent(DM dm, PetscInt point, PetscInt *parent, PetscInt *childID)
1116: {
1117:   DM_Plex       *mesh = (DM_Plex *)dm->data;
1118:   PetscSection   pSec;

1123:   pSec = mesh->parentSection;
1124:   if (pSec && point >= pSec->pStart && point < pSec->pEnd) {
1125:     PetscInt dof;

1127:     PetscSectionGetDof (pSec, point, &dof);
1128:     if (dof) {
1129:       PetscInt off;

1131:       PetscSectionGetOffset (pSec, point, &off);
1132:       if (parent)  *parent = mesh->parents[off];
1133:       if (childID) *childID = mesh->childIDs[off];
1134:       return(0);
1135:     }
1136:   }
1137:   if (parent) {
1138:     *parent = point;
1139:   }
1140:   if (childID) {
1141:     *childID = 0;
1142:   }
1143:   return(0);
1144: }

1146: /*@C
1147:   DMPlexGetTreeChildren - get the children of a point in the tree describing the point hierarchy (not the DAG)

1149:   Input Parameters:
1150: + dm - the DMPlex object
1151: - point - the query point

1153:   Output Parameters:
1154: + numChildren - if not NULL, set to the number of children
1155: - children - if not NULL, set to a list children, or set to NULL if the point has no children

1157:   Level: intermediate

1159:   Fortran Notes:
1160:   Since it returns an array, this routine is only available in Fortran 90, and you must
1161:   include petsc.h90 in your code.

1163: .seealso: DMPlexSetTree(), DMPlexGetTree(), DMPlexGetTreeParent()
1164: @*/
1165: PetscErrorCode DMPlexGetTreeChildren(DM dm, PetscInt point, PetscInt *numChildren, const PetscInt *children[])
1166: {
1167:   DM_Plex       *mesh = (DM_Plex *)dm->data;
1168:   PetscSection   childSec;
1169:   PetscInt       dof = 0;

1174:   childSec = mesh->childSection;
1175:   if (childSec && point >= childSec->pStart && point < childSec->pEnd) {
1176:     PetscSectionGetDof (childSec, point, &dof);
1177:   }
1178:   if (numChildren) *numChildren = dof;
1179:   if (children) {
1180:     if (dof) {
1181:       PetscInt off;

1183:       PetscSectionGetOffset (childSec, point, &off);
1184:       *children = &mesh->children[off];
1185:     }
1186:     else {
1187:       *children = NULL;
1188:     }
1189:   }
1190:   return(0);
1191: }

1193: static PetscErrorCode EvaluateBasis(PetscSpace space, PetscInt nBasis, PetscInt nFunctionals, PetscInt nComps, PetscInt nPoints, const PetscInt *pointsPerFn, const PetscReal *points, const PetscReal *weights, PetscReal *work, Mat basisAtPoints)
1194: {
1195:   PetscInt       f, b, p, c, offset, qPoints;

1199:   PetscSpaceEvaluate(space,nPoints,points,work,NULL,NULL);
1200:   for (f = 0, offset = 0; f < nFunctionals; f++) {
1201:     qPoints = pointsPerFn[f];
1202:     for (b = 0; b < nBasis; b++) {
1203:       PetscScalar val = 0.;

1205:       for (p = 0; p < qPoints; p++) {
1206:         for (c = 0; c < nComps; c++) {
1207:           val += work[((offset + p) * nBasis + b) * nComps + c] * weights[(offset + p) * nComps + c];
1208:         }
1209:       }
1210:       MatSetValue(basisAtPoints,b,f,val,INSERT_VALUES);
1211:     }
1212:     offset += qPoints;
1213:   }
1214:   MatAssemblyBegin(basisAtPoints,MAT_FINAL_ASSEMBLY);
1215:   MatAssemblyEnd(basisAtPoints,MAT_FINAL_ASSEMBLY);
1216:   return(0);
1217: }

1219: static PetscErrorCode DMPlexComputeAnchorMatrix_Tree_Direct(DM dm, PetscSection section, PetscSection cSec, Mat cMat)
1220: {
1221:   PetscDS        ds;
1222:   PetscInt       spdim;
1223:   PetscInt       numFields, f, c, cStart, cEnd, pStart, pEnd, conStart, conEnd;
1224:   const PetscInt *anchors;
1225:   PetscSection   aSec;
1226:   PetscReal      *v0, *v0parent, *vtmp, *J, *Jparent, *invJparent, detJ, detJparent;
1227:   IS             aIS;

1231:   DMPlexGetChart(dm,&pStart,&pEnd);
1232:   DMGetDS(dm,&ds);
1233:   PetscDSGetNumFields(ds,&numFields);
1234:   DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);
1235:   DMPlexGetAnchors(dm,&aSec,&aIS);
1236:   ISGetIndices(aIS,&anchors);
1237:   PetscSectionGetChart(cSec,&conStart,&conEnd);
1238:   DMGetDimension(dm,&spdim);
1239:   PetscMalloc6(spdim,&v0,spdim,&v0parent,spdim,&vtmp,spdim*spdim,&J,spdim*spdim,&Jparent,spdim*spdim,&invJparent);

1241:   for (f = 0; f < numFields; f++) {
1242:     PetscObject       disc;
1243:     PetscClassId      id;
1244:     PetscSpace        bspace;
1245:     PetscDualSpace    dspace;
1246:     PetscInt          i, j, k, nPoints, Nc, offset;
1247:     PetscInt          fSize, maxDof;
1248:     PetscReal         *weights, *pointsRef, *pointsReal, *work;
1249:     PetscScalar       *scwork;
1250:     const PetscScalar *X;
1251:     PetscInt          *sizes, *workIndRow, *workIndCol;
1252:     Mat               Amat, Bmat, Xmat;
1253:     const PetscInt    *numDof  = NULL;
1254:     const PetscInt    ***perms = NULL;
1255:     const PetscScalar ***flips = NULL;

1257:     PetscDSGetDiscretization(ds,f,&disc);
1258:     PetscObjectGetClassId(disc,&id);
1259:     if (id == PETSCFE_CLASSID) {
1260:       PetscFE fe = (PetscFE) disc;

1262:       PetscFEGetBasisSpace(fe,&bspace);
1263:       PetscFEGetDualSpace(fe,&dspace);
1264:       PetscDualSpaceGetDimension(dspace,&fSize);
1265:       PetscFEGetNumComponents(fe,&Nc);
1266:     }
1267:     else if (id == PETSCFV_CLASSID) {
1268:       PetscFV fv = (PetscFV) disc;

1270:       PetscFVGetNumComponents(fv,&Nc);
1271:       PetscSpaceCreate(PetscObjectComm((PetscObject)fv),&bspace);
1272:       PetscSpaceSetType(bspace,PETSCSPACEPOLYNOMIAL);
1273:       PetscSpaceSetDegree(bspace,0,PETSC_DETERMINE);
1274:       PetscSpaceSetNumComponents(bspace,Nc);
1275:       PetscSpaceSetNumVariables(bspace,spdim);
1276:       PetscSpaceSetUp(bspace);
1277:       PetscFVGetDualSpace(fv,&dspace);
1278:       PetscDualSpaceGetDimension(dspace,&fSize);
1279:     }
1280:     else SETERRQ1(PetscObjectComm(disc),PETSC_ERR_ARG_UNKNOWN_TYPE, "PetscDS discretization id %d not recognized.", id);
1281:     PetscDualSpaceGetNumDof(dspace,&numDof);
1282:     for (i = 0, maxDof = 0; i <= spdim; i++) {maxDof = PetscMax(maxDof,numDof[i]);}
1283:     PetscDualSpaceGetSymmetries(dspace,&perms,&flips);

1285:     MatCreate(PETSC_COMM_SELF,&Amat);
1286:     MatSetSizes(Amat,fSize,fSize,fSize,fSize);
1287:     MatSetType(Amat,MATSEQDENSE);
1288:     MatSetUp(Amat);
1289:     MatDuplicate(Amat,MAT_DO_NOT_COPY_VALUES,&Bmat);
1290:     MatDuplicate(Amat,MAT_DO_NOT_COPY_VALUES,&Xmat);
1291:     nPoints = 0;
1292:     for (i = 0; i < fSize; i++) {
1293:       PetscInt        qPoints, thisNc;
1294:       PetscQuadrature quad;

1296:       PetscDualSpaceGetFunctional(dspace,i,&quad);
1297:       PetscQuadratureGetData(quad,NULL,&thisNc,&qPoints,NULL,NULL);
1298:       if (thisNc != Nc) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Functional dim %D does not much basis dim %D\n",thisNc,Nc);
1299:       nPoints += qPoints;
1300:     }
1301:     PetscMalloc7(fSize,&sizes,nPoints*Nc,&weights,spdim*nPoints,&pointsRef,spdim*nPoints,&pointsReal,nPoints*fSize*Nc,&work,maxDof,&workIndRow,maxDof,&workIndCol);
1302:     PetscMalloc1(maxDof * maxDof,&scwork);
1303:     offset = 0;
1304:     for (i = 0; i < fSize; i++) {
1305:       PetscInt        qPoints;
1306:       const PetscReal    *p, *w;
1307:       PetscQuadrature quad;

1309:       PetscDualSpaceGetFunctional(dspace,i,&quad);
1310:       PetscQuadratureGetData(quad,NULL,NULL,&qPoints,&p,&w);
1311:       PetscArraycpy(weights+Nc*offset,w,Nc*qPoints);
1312:       PetscArraycpy(pointsRef+spdim*offset,p,spdim*qPoints);
1313:       sizes[i] = qPoints;
1314:       offset  += qPoints;
1315:     }
1316:     EvaluateBasis(bspace,fSize,fSize,Nc,nPoints,sizes,pointsRef,weights,work,Amat);
1317:     MatLUFactor(Amat,NULL,NULL,NULL);
1318:     for (c = cStart; c < cEnd; c++) {
1319:       PetscInt        parent;
1320:       PetscInt        closureSize, closureSizeP, *closure = NULL, *closureP = NULL;
1321:       PetscInt        *childOffsets, *parentOffsets;

1323:       DMPlexGetTreeParent(dm,c,&parent,NULL);
1324:       if (parent == c) continue;
1325:       DMPlexGetTransitiveClosure(dm,c,PETSC_TRUE,&closureSize,&closure);
1326:       for (i = 0; i < closureSize; i++) {
1327:         PetscInt p = closure[2*i];
1328:         PetscInt conDof;

1330:         if (p < conStart || p >= conEnd) continue;
1331:         if (numFields) {
1332:           PetscSectionGetFieldDof(cSec,p,f,&conDof);
1333:         }
1334:         else {
1335:           PetscSectionGetDof(cSec,p,&conDof);
1336:         }
1337:         if (conDof) break;
1338:       }
1339:       if (i == closureSize) {
1340:         DMPlexRestoreTransitiveClosure(dm,c,PETSC_TRUE,&closureSize,&closure);
1341:         continue;
1342:       }

1344:       DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, NULL, &detJ);
1345:       DMPlexComputeCellGeometryFEM(dm, parent, NULL, v0parent, Jparent, invJparent, &detJparent);
1346:       for (i = 0; i < nPoints; i++) {
1347:         const PetscReal xi0[3] = {-1.,-1.,-1.};

1349:         CoordinatesRefToReal(spdim, spdim, xi0, v0, J, &pointsRef[i*spdim],vtmp);
1350:         CoordinatesRealToRef(spdim, spdim, xi0, v0parent, invJparent, vtmp, &pointsReal[i*spdim]);
1351:       }
1352:       EvaluateBasis(bspace,fSize,fSize,Nc,nPoints,sizes,pointsReal,weights,work,Bmat);
1353:       MatMatSolve(Amat,Bmat,Xmat);
1354:       MatDenseGetArrayRead(Xmat,&X);
1355:       DMPlexGetTransitiveClosure(dm,parent,PETSC_TRUE,&closureSizeP,&closureP);
1356:       PetscMalloc2(closureSize+1,&childOffsets,closureSizeP+1,&parentOffsets);
1357:       childOffsets[0] = 0;
1358:       for (i = 0; i < closureSize; i++) {
1359:         PetscInt p = closure[2*i];
1360:         PetscInt dof;

1362:         if (numFields) {
1363:           PetscSectionGetFieldDof(section,p,f,&dof);
1364:         }
1365:         else {
1366:           PetscSectionGetDof(section,p,&dof);
1367:         }
1368:         childOffsets[i+1]=childOffsets[i]+dof;
1369:       }
1370:       parentOffsets[0] = 0;
1371:       for (i = 0; i < closureSizeP; i++) {
1372:         PetscInt p = closureP[2*i];
1373:         PetscInt dof;

1375:         if (numFields) {
1376:           PetscSectionGetFieldDof(section,p,f,&dof);
1377:         }
1378:         else {
1379:           PetscSectionGetDof(section,p,&dof);
1380:         }
1381:         parentOffsets[i+1]=parentOffsets[i]+dof;
1382:       }
1383:       for (i = 0; i < closureSize; i++) {
1384:         PetscInt conDof, conOff, aDof, aOff, nWork;
1385:         PetscInt p = closure[2*i];
1386:         PetscInt o = closure[2*i+1];
1387:         const PetscInt    *perm;
1388:         const PetscScalar *flip;

1390:         if (p < conStart || p >= conEnd) continue;
1391:         if (numFields) {
1392:           PetscSectionGetFieldDof(cSec,p,f,&conDof);
1393:           PetscSectionGetFieldOffset(cSec,p,f,&conOff);
1394:         }
1395:         else {
1396:           PetscSectionGetDof(cSec,p,&conDof);
1397:           PetscSectionGetOffset(cSec,p,&conOff);
1398:         }
1399:         if (!conDof) continue;
1400:         perm  = (perms && perms[i]) ? perms[i][o] : NULL;
1401:         flip  = (flips && flips[i]) ? flips[i][o] : NULL;
1402:         PetscSectionGetDof(aSec,p,&aDof);
1403:         PetscSectionGetOffset(aSec,p,&aOff);
1404:         nWork = childOffsets[i+1]-childOffsets[i];
1405:         for (k = 0; k < aDof; k++) {
1406:           PetscInt a = anchors[aOff + k];
1407:           PetscInt aSecDof, aSecOff;

1409:           if (numFields) {
1410:             PetscSectionGetFieldDof(section,a,f,&aSecDof);
1411:             PetscSectionGetFieldOffset(section,a,f,&aSecOff);
1412:           }
1413:           else {
1414:             PetscSectionGetDof(section,a,&aSecDof);
1415:             PetscSectionGetOffset(section,a,&aSecOff);
1416:           }
1417:           if (!aSecDof) continue;

1419:           for (j = 0; j < closureSizeP; j++) {
1420:             PetscInt q = closureP[2*j];
1421:             PetscInt oq = closureP[2*j+1];

1423:             if (q == a) {
1424:               PetscInt           r, s, nWorkP;
1425:               const PetscInt    *permP;
1426:               const PetscScalar *flipP;

1428:               permP  = (perms && perms[j]) ? perms[j][oq] : NULL;
1429:               flipP  = (flips && flips[j]) ? flips[j][oq] : NULL;
1430:               nWorkP = parentOffsets[j+1]-parentOffsets[j];
1431:               /* get a copy of the child-to-anchor portion of the matrix, and transpose so that rows correspond to the
1432:                * child and columns correspond to the anchor: BUT the maxrix returned by MatDenseGetArrayRead() is
1433:                * column-major, so transpose-transpose = do nothing */
1434:               for (r = 0; r < nWork; r++) {
1435:                 for (s = 0; s < nWorkP; s++) {
1436:                   scwork[r * nWorkP + s] = X[fSize * (r + childOffsets[i]) + (s + parentOffsets[j])];
1437:                 }
1438:               }
1439:               for (r = 0; r < nWork; r++)  {workIndRow[perm  ? perm[r]  : r] = conOff  + r;}
1440:               for (s = 0; s < nWorkP; s++) {workIndCol[permP ? permP[s] : s] = aSecOff + s;}
1441:               if (flip) {
1442:                 for (r = 0; r < nWork; r++) {
1443:                   for (s = 0; s < nWorkP; s++) {
1444:                     scwork[r * nWorkP + s] *= flip[r];
1445:                   }
1446:                 }
1447:               }
1448:               if (flipP) {
1449:                 for (r = 0; r < nWork; r++) {
1450:                   for (s = 0; s < nWorkP; s++) {
1451:                     scwork[r * nWorkP + s] *= flipP[s];
1452:                   }
1453:                 }
1454:               }
1455:               MatSetValues(cMat,nWork,workIndRow,nWorkP,workIndCol,scwork,INSERT_VALUES);
1456:               break;
1457:             }
1458:           }
1459:         }
1460:       }
1461:       MatDenseRestoreArrayRead(Xmat,&X);
1462:       PetscFree2(childOffsets,parentOffsets);
1463:       DMPlexRestoreTransitiveClosure(dm,c,PETSC_TRUE,&closureSize,&closure);
1464:       DMPlexRestoreTransitiveClosure(dm,parent,PETSC_TRUE,&closureSizeP,&closureP);
1465:     }
1466:     MatDestroy(&Amat);
1467:     MatDestroy(&Bmat);
1468:     MatDestroy(&Xmat);
1469:     PetscFree(scwork);
1470:     PetscFree7(sizes,weights,pointsRef,pointsReal,work,workIndRow,workIndCol);
1471:     if (id == PETSCFV_CLASSID) {
1472:       PetscSpaceDestroy(&bspace);
1473:     }
1474:   }
1475:   MatAssemblyBegin(cMat,MAT_FINAL_ASSEMBLY);
1476:   MatAssemblyEnd(cMat,MAT_FINAL_ASSEMBLY);
1477:   PetscFree6(v0,v0parent,vtmp,J,Jparent,invJparent);
1478:   ISRestoreIndices(aIS,&anchors);

1480:   return(0);
1481: }

1483: static PetscErrorCode DMPlexReferenceTreeGetChildrenMatrices(DM refTree, PetscScalar ****childrenMats, PetscInt ***childrenN)
1484: {
1485:   Mat               refCmat;
1486:   PetscDS           ds;
1487:   PetscInt          numFields, maxFields, f, pRefStart, pRefEnd, p, *rows, *cols, maxDof, maxAnDof, **refPointFieldN;
1488:   PetscScalar       ***refPointFieldMats;
1489:   PetscSection      refConSec, refAnSec, refSection;
1490:   IS                refAnIS;
1491:   const PetscInt    *refAnchors;
1492:   const PetscInt    **perms;
1493:   const PetscScalar **flips;
1494:   PetscErrorCode    ierr;

1497:   DMGetDS(refTree,&ds);
1498:   PetscDSGetNumFields(ds,&numFields);
1499:   maxFields = PetscMax(1,numFields);
1500:   DMGetDefaultConstraints(refTree,&refConSec,&refCmat);
1501:   DMPlexGetAnchors(refTree,&refAnSec,&refAnIS);
1502:   ISGetIndices(refAnIS,&refAnchors);
1503:   DMGetLocalSection(refTree,&refSection);
1504:   PetscSectionGetChart(refConSec,&pRefStart,&pRefEnd);
1505:   PetscMalloc1(pRefEnd-pRefStart,&refPointFieldMats);
1506:   PetscMalloc1(pRefEnd-pRefStart,&refPointFieldN);
1507:   PetscSectionGetMaxDof(refConSec,&maxDof);
1508:   PetscSectionGetMaxDof(refAnSec,&maxAnDof);
1509:   PetscMalloc1(maxDof,&rows);
1510:   PetscMalloc1(maxDof*maxAnDof,&cols);
1511:   for (p = pRefStart; p < pRefEnd; p++) {
1512:     PetscInt parent, closureSize, *closure = NULL, pDof;

1514:     DMPlexGetTreeParent(refTree,p,&parent,NULL);
1515:     PetscSectionGetDof(refConSec,p,&pDof);
1516:     if (!pDof || parent == p) continue;

1518:     PetscMalloc1(maxFields,&refPointFieldMats[p-pRefStart]);
1519:     PetscCalloc1(maxFields,&refPointFieldN[p-pRefStart]);
1520:     DMPlexGetTransitiveClosure(refTree,parent,PETSC_TRUE,&closureSize,&closure);
1521:     for (f = 0; f < maxFields; f++) {
1522:       PetscInt cDof, cOff, numCols, r, i;

1524:       if (f < numFields) {
1525:         PetscSectionGetFieldDof(refConSec,p,f,&cDof);
1526:         PetscSectionGetFieldOffset(refConSec,p,f,&cOff);
1527:         PetscSectionGetFieldPointSyms(refSection,f,closureSize,closure,&perms,&flips);
1528:       } else {
1529:         PetscSectionGetDof(refConSec,p,&cDof);
1530:         PetscSectionGetOffset(refConSec,p,&cOff);
1531:         PetscSectionGetPointSyms(refSection,closureSize,closure,&perms,&flips);
1532:       }

1534:       for (r = 0; r < cDof; r++) {
1535:         rows[r] = cOff + r;
1536:       }
1537:       numCols = 0;
1538:       for (i = 0; i < closureSize; i++) {
1539:         PetscInt          q = closure[2*i];
1540:         PetscInt          aDof, aOff, j;
1541:         const PetscInt    *perm = perms ? perms[i] : NULL;

1543:         if (numFields) {
1544:           PetscSectionGetFieldDof(refSection,q,f,&aDof);
1545:           PetscSectionGetFieldOffset(refSection,q,f,&aOff);
1546:         }
1547:         else {
1548:           PetscSectionGetDof(refSection,q,&aDof);
1549:           PetscSectionGetOffset(refSection,q,&aOff);
1550:         }

1552:         for (j = 0; j < aDof; j++) {
1553:           cols[numCols++] = aOff + (perm ? perm[j] : j);
1554:         }
1555:       }
1556:       refPointFieldN[p-pRefStart][f] = numCols;
1557:       PetscMalloc1(cDof*numCols,&refPointFieldMats[p-pRefStart][f]);
1558:       MatGetValues(refCmat,cDof,rows,numCols,cols,refPointFieldMats[p-pRefStart][f]);
1559:       if (flips) {
1560:         PetscInt colOff = 0;

1562:         for (i = 0; i < closureSize; i++) {
1563:           PetscInt          q = closure[2*i];
1564:           PetscInt          aDof, aOff, j;
1565:           const PetscScalar *flip = flips ? flips[i] : NULL;

1567:           if (numFields) {
1568:             PetscSectionGetFieldDof(refSection,q,f,&aDof);
1569:             PetscSectionGetFieldOffset(refSection,q,f,&aOff);
1570:           }
1571:           else {
1572:             PetscSectionGetDof(refSection,q,&aDof);
1573:             PetscSectionGetOffset(refSection,q,&aOff);
1574:           }
1575:           if (flip) {
1576:             PetscInt k;
1577:             for (k = 0; k < cDof; k++) {
1578:               for (j = 0; j < aDof; j++) {
1579:                 refPointFieldMats[p-pRefStart][f][k * numCols + colOff + j] *= flip[j];
1580:               }
1581:             }
1582:           }
1583:           colOff += aDof;
1584:         }
1585:       }
1586:       if (numFields) {
1587:         PetscSectionRestoreFieldPointSyms(refSection,f,closureSize,closure,&perms,&flips);
1588:       } else {
1589:         PetscSectionRestorePointSyms(refSection,closureSize,closure,&perms,&flips);
1590:       }
1591:     }
1592:     DMPlexRestoreTransitiveClosure(refTree,parent,PETSC_TRUE,&closureSize,&closure);
1593:   }
1594:   *childrenMats = refPointFieldMats;
1595:   *childrenN = refPointFieldN;
1596:   ISRestoreIndices(refAnIS,&refAnchors);
1597:   PetscFree(rows);
1598:   PetscFree(cols);
1599:   return(0);
1600: }

1602: static PetscErrorCode DMPlexReferenceTreeRestoreChildrenMatrices(DM refTree, PetscScalar ****childrenMats, PetscInt ***childrenN)
1603: {
1604:   PetscDS        ds;
1605:   PetscInt       **refPointFieldN;
1606:   PetscScalar    ***refPointFieldMats;
1607:   PetscInt       numFields, maxFields, pRefStart, pRefEnd, p, f;
1608:   PetscSection   refConSec;

1612:   refPointFieldN = *childrenN;
1613:   *childrenN = NULL;
1614:   refPointFieldMats = *childrenMats;
1615:   *childrenMats = NULL;
1616:   DMGetDS(refTree,&ds);
1617:   PetscDSGetNumFields(ds,&numFields);
1618:   maxFields = PetscMax(1,numFields);
1619:   DMGetDefaultConstraints(refTree,&refConSec,NULL);
1620:   PetscSectionGetChart(refConSec,&pRefStart,&pRefEnd);
1621:   for (p = pRefStart; p < pRefEnd; p++) {
1622:     PetscInt parent, pDof;

1624:     DMPlexGetTreeParent(refTree,p,&parent,NULL);
1625:     PetscSectionGetDof(refConSec,p,&pDof);
1626:     if (!pDof || parent == p) continue;

1628:     for (f = 0; f < maxFields; f++) {
1629:       PetscInt cDof;

1631:       if (numFields) {
1632:         PetscSectionGetFieldDof(refConSec,p,f,&cDof);
1633:       }
1634:       else {
1635:         PetscSectionGetDof(refConSec,p,&cDof);
1636:       }

1638:       PetscFree(refPointFieldMats[p - pRefStart][f]);
1639:     }
1640:     PetscFree(refPointFieldMats[p - pRefStart]);
1641:     PetscFree(refPointFieldN[p - pRefStart]);
1642:   }
1643:   PetscFree(refPointFieldMats);
1644:   PetscFree(refPointFieldN);
1645:   return(0);
1646: }

1648: static PetscErrorCode DMPlexComputeAnchorMatrix_Tree_FromReference(DM dm, PetscSection section, PetscSection conSec, Mat cMat)
1649: {
1650:   DM             refTree;
1651:   PetscDS        ds;
1652:   Mat            refCmat;
1653:   PetscInt       numFields, maxFields, f, pRefStart, pRefEnd, p, maxDof, maxAnDof, *perm, *iperm, pStart, pEnd, conStart, conEnd, **refPointFieldN;
1654:   PetscScalar ***refPointFieldMats, *pointWork;
1655:   PetscSection   refConSec, refAnSec, anSec;
1656:   IS             refAnIS, anIS;
1657:   const PetscInt *anchors;

1662:   DMGetDS(dm,&ds);
1663:   PetscDSGetNumFields(ds,&numFields);
1664:   maxFields = PetscMax(1,numFields);
1665:   DMPlexGetReferenceTree(dm,&refTree);
1666:   DMCopyDisc(dm,refTree);
1667:   DMGetDefaultConstraints(refTree,&refConSec,&refCmat);
1668:   DMPlexGetAnchors(refTree,&refAnSec,&refAnIS);
1669:   DMPlexGetAnchors(dm,&anSec,&anIS);
1670:   ISGetIndices(anIS,&anchors);
1671:   PetscSectionGetChart(refConSec,&pRefStart,&pRefEnd);
1672:   PetscSectionGetChart(conSec,&conStart,&conEnd);
1673:   PetscSectionGetMaxDof(refConSec,&maxDof);
1674:   PetscSectionGetMaxDof(refAnSec,&maxAnDof);
1675:   PetscMalloc1(maxDof*maxDof*maxAnDof,&pointWork);

1677:   /* step 1: get submats for every constrained point in the reference tree */
1678:   DMPlexReferenceTreeGetChildrenMatrices(refTree,&refPointFieldMats,&refPointFieldN);

1680:   /* step 2: compute the preorder */
1681:   DMPlexGetChart(dm,&pStart,&pEnd);
1682:   PetscMalloc2(pEnd-pStart,&perm,pEnd-pStart,&iperm);
1683:   for (p = pStart; p < pEnd; p++) {
1684:     perm[p - pStart] = p;
1685:     iperm[p - pStart] = p-pStart;
1686:   }
1687:   for (p = 0; p < pEnd - pStart;) {
1688:     PetscInt point = perm[p];
1689:     PetscInt parent;

1691:     DMPlexGetTreeParent(dm,point,&parent,NULL);
1692:     if (parent == point) {
1693:       p++;
1694:     }
1695:     else {
1696:       PetscInt size, closureSize, *closure = NULL, i;

1698:       DMPlexGetTransitiveClosure(dm,parent,PETSC_TRUE,&closureSize,&closure);
1699:       for (i = 0; i < closureSize; i++) {
1700:         PetscInt q = closure[2*i];
1701:         if (iperm[q-pStart] > iperm[point-pStart]) {
1702:           /* swap */
1703:           perm[p]               = q;
1704:           perm[iperm[q-pStart]] = point;
1705:           iperm[point-pStart]   = iperm[q-pStart];
1706:           iperm[q-pStart]       = p;
1707:           break;
1708:         }
1709:       }
1710:       size = closureSize;
1711:       DMPlexRestoreTransitiveClosure(dm,parent,PETSC_TRUE,&closureSize,&closure);
1712:       if (i == size) {
1713:         p++;
1714:       }
1715:     }
1716:   }

1718:   /* step 3: fill the constraint matrix */
1719:   /* we are going to use a preorder progressive fill strategy.  Mat doesn't
1720:    * allow progressive fill without assembly, so we are going to set up the
1721:    * values outside of the Mat first.
1722:    */
1723:   {
1724:     PetscInt nRows, row, nnz;
1725:     PetscBool done;
1726:     const PetscInt *ia, *ja;
1727:     PetscScalar *vals;

1729:     MatGetRowIJ(cMat,0,PETSC_FALSE,PETSC_FALSE,&nRows,&ia,&ja,&done);
1730:     if (!done) SETERRQ(PetscObjectComm((PetscObject)cMat),PETSC_ERR_PLIB,"Could not get RowIJ of constraint matrix");
1731:     nnz  = ia[nRows];
1732:     /* malloc and then zero rows right before we fill them: this way valgrind
1733:      * can tell if we are doing progressive fill in the wrong order */
1734:     PetscMalloc1(nnz,&vals);
1735:     for (p = 0; p < pEnd - pStart; p++) {
1736:       PetscInt        parent, childid, closureSize, *closure = NULL;
1737:       PetscInt        point = perm[p], pointDof;

1739:       DMPlexGetTreeParent(dm,point,&parent,&childid);
1740:       if ((point < conStart) || (point >= conEnd) || (parent == point)) continue;
1741:       PetscSectionGetDof(conSec,point,&pointDof);
1742:       if (!pointDof) continue;
1743:       DMPlexGetTransitiveClosure(dm,parent,PETSC_TRUE,&closureSize,&closure);
1744:       for (f = 0; f < maxFields; f++) {
1745:         PetscInt cDof, cOff, numCols, numFillCols, i, r, matOffset, offset;
1746:         PetscScalar *pointMat;
1747:         const PetscInt    **perms;
1748:         const PetscScalar **flips;

1750:         if (numFields) {
1751:           PetscSectionGetFieldDof(conSec,point,f,&cDof);
1752:           PetscSectionGetFieldOffset(conSec,point,f,&cOff);
1753:         }
1754:         else {
1755:           PetscSectionGetDof(conSec,point,&cDof);
1756:           PetscSectionGetOffset(conSec,point,&cOff);
1757:         }
1758:         if (!cDof) continue;
1759:         if (numFields) {PetscSectionGetFieldPointSyms(section,f,closureSize,closure,&perms,&flips);}
1760:         else           {PetscSectionGetPointSyms(section,closureSize,closure,&perms,&flips);}

1762:         /* make sure that every row for this point is the same size */
1763: #if defined(PETSC_USE_DEBUG)
1764:         for (r = 0; r < cDof; r++) {
1765:           if (cDof > 1 && r) {
1766:             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]));
1767:           }
1768:         }
1769: #endif
1770:         /* zero rows */
1771:         for (i = ia[cOff] ; i< ia[cOff+cDof];i++) {
1772:           vals[i] = 0.;
1773:         }
1774:         matOffset = ia[cOff];
1775:         numFillCols = ia[cOff+1] - matOffset;
1776:         pointMat = refPointFieldMats[childid-pRefStart][f];
1777:         numCols = refPointFieldN[childid-pRefStart][f];
1778:         offset = 0;
1779:         for (i = 0; i < closureSize; i++) {
1780:           PetscInt q = closure[2*i];
1781:           PetscInt aDof, aOff, j, k, qConDof, qConOff;
1782:           const PetscInt    *perm = perms ? perms[i] : NULL;
1783:           const PetscScalar *flip = flips ? flips[i] : NULL;

1785:           qConDof = qConOff = 0;
1786:           if (numFields) {
1787:             PetscSectionGetFieldDof(section,q,f,&aDof);
1788:             PetscSectionGetFieldOffset(section,q,f,&aOff);
1789:             if (q >= conStart && q < conEnd) {
1790:               PetscSectionGetFieldDof(conSec,q,f,&qConDof);
1791:               PetscSectionGetFieldOffset(conSec,q,f,&qConOff);
1792:             }
1793:           }
1794:           else {
1795:             PetscSectionGetDof(section,q,&aDof);
1796:             PetscSectionGetOffset(section,q,&aOff);
1797:             if (q >= conStart && q < conEnd) {
1798:               PetscSectionGetDof(conSec,q,&qConDof);
1799:               PetscSectionGetOffset(conSec,q,&qConOff);
1800:             }
1801:           }
1802:           if (!aDof) continue;
1803:           if (qConDof) {
1804:             /* this point has anchors: its rows of the matrix should already
1805:              * be filled, thanks to preordering */
1806:             /* first multiply into pointWork, then set in matrix */
1807:             PetscInt aMatOffset = ia[qConOff];
1808:             PetscInt aNumFillCols = ia[qConOff + 1] - aMatOffset;
1809:             for (r = 0; r < cDof; r++) {
1810:               for (j = 0; j < aNumFillCols; j++) {
1811:                 PetscScalar inVal = 0;
1812:                 for (k = 0; k < aDof; k++) {
1813:                   PetscInt col = perm ? perm[k] : k;

1815:                   inVal += pointMat[r * numCols + offset + col] * vals[aMatOffset + aNumFillCols * k + j] * (flip ? flip[col] : 1.);
1816:                 }
1817:                 pointWork[r * aNumFillCols + j] = inVal;
1818:               }
1819:             }
1820:             /* assume that the columns are sorted, spend less time searching */
1821:             for (j = 0, k = 0; j < aNumFillCols; j++) {
1822:               PetscInt col = ja[aMatOffset + j];
1823:               for (;k < numFillCols; k++) {
1824:                 if (ja[matOffset + k] == col) {
1825:                   break;
1826:                 }
1827:               }
1828:               if (k == numFillCols) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"No nonzero space for (%d, %d)", cOff, col);
1829:               for (r = 0; r < cDof; r++) {
1830:                 vals[matOffset + numFillCols * r + k] = pointWork[r * aNumFillCols + j];
1831:               }
1832:             }
1833:           }
1834:           else {
1835:             /* find where to put this portion of pointMat into the matrix */
1836:             for (k = 0; k < numFillCols; k++) {
1837:               if (ja[matOffset + k] == aOff) {
1838:                 break;
1839:               }
1840:             }
1841:             if (k == numFillCols) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"No nonzero space for (%d, %d)", cOff, aOff);
1842:             for (r = 0; r < cDof; r++) {
1843:               for (j = 0; j < aDof; j++) {
1844:                 PetscInt col = perm ? perm[j] : j;

1846:                 vals[matOffset + numFillCols * r + k + col] += pointMat[r * numCols + offset + j] * (flip ? flip[col] : 1.);
1847:               }
1848:             }
1849:           }
1850:           offset += aDof;
1851:         }
1852:         if (numFields) {
1853:           PetscSectionRestoreFieldPointSyms(section,f,closureSize,closure,&perms,&flips);
1854:         } else {
1855:           PetscSectionRestorePointSyms(section,closureSize,closure,&perms,&flips);
1856:         }
1857:       }
1858:       DMPlexRestoreTransitiveClosure(dm,parent,PETSC_TRUE,&closureSize,&closure);
1859:     }
1860:     for (row = 0; row < nRows; row++) {
1861:       MatSetValues(cMat,1,&row,ia[row+1]-ia[row],&ja[ia[row]],&vals[ia[row]],INSERT_VALUES);
1862:     }
1863:     MatRestoreRowIJ(cMat,0,PETSC_FALSE,PETSC_FALSE,&nRows,&ia,&ja,&done);
1864:     if (!done) SETERRQ(PetscObjectComm((PetscObject)cMat),PETSC_ERR_PLIB,"Could not restore RowIJ of constraint matrix");
1865:     MatAssemblyBegin(cMat,MAT_FINAL_ASSEMBLY);
1866:     MatAssemblyEnd(cMat,MAT_FINAL_ASSEMBLY);
1867:     PetscFree(vals);
1868:   }

1870:   /* clean up */
1871:   ISRestoreIndices(anIS,&anchors);
1872:   PetscFree2(perm,iperm);
1873:   PetscFree(pointWork);
1874:   DMPlexReferenceTreeRestoreChildrenMatrices(refTree,&refPointFieldMats,&refPointFieldN);
1875:   return(0);
1876: }

1878: /* refine a single cell on rank 0: this is not intended to provide good local refinement, only to create an example of
1879:  * a non-conforming mesh.  Local refinement comes later */
1880: PetscErrorCode DMPlexTreeRefineCell (DM dm, PetscInt cell, DM *ncdm)
1881: {
1882:   DM K;
1883:   PetscMPIInt rank;
1884:   PetscInt dim, *pNewStart, *pNewEnd, *pNewCount, *pOldStart, *pOldEnd, offset, d, pStart, pEnd;
1885:   PetscInt numNewCones, *newConeSizes, *newCones, *newOrientations;
1886:   PetscInt *Kembedding;
1887:   PetscInt *cellClosure=NULL, nc;
1888:   PetscScalar *newVertexCoords;
1889:   PetscInt numPointsWithParents, *parents, *childIDs, *perm, *iperm, *preOrient, pOffset;
1890:   PetscSection parentSection;

1894:   MPI_Comm_rank(PetscObjectComm((PetscObject)dm),&rank);
1895:   DMGetDimension(dm,&dim);
1896:   DMPlexCreate(PetscObjectComm((PetscObject)dm), ncdm);
1897:   DMSetDimension(*ncdm,dim);

1899:   DMPlexGetChart(dm, &pStart, &pEnd);
1900:   PetscSectionCreate(PetscObjectComm((PetscObject)dm),&parentSection);
1901:   DMPlexGetReferenceTree(dm,&K);
1902:   if (!rank) {
1903:     /* compute the new charts */
1904:     PetscMalloc5(dim+1,&pNewCount,dim+1,&pNewStart,dim+1,&pNewEnd,dim+1,&pOldStart,dim+1,&pOldEnd);
1905:     offset = 0;
1906:     for (d = 0; d <= dim; d++) {
1907:       PetscInt pOldCount, kStart, kEnd, k;

1909:       pNewStart[d] = offset;
1910:       DMPlexGetHeightStratum(dm,d,&pOldStart[d],&pOldEnd[d]);
1911:       DMPlexGetHeightStratum(K,d,&kStart,&kEnd);
1912:       pOldCount = pOldEnd[d] - pOldStart[d];
1913:       /* adding the new points */
1914:       pNewCount[d] = pOldCount + kEnd - kStart;
1915:       if (!d) {
1916:         /* removing the cell */
1917:         pNewCount[d]--;
1918:       }
1919:       for (k = kStart; k < kEnd; k++) {
1920:         PetscInt parent;
1921:         DMPlexGetTreeParent(K,k,&parent,NULL);
1922:         if (parent == k) {
1923:           /* avoid double counting points that won't actually be new */
1924:           pNewCount[d]--;
1925:         }
1926:       }
1927:       pNewEnd[d] = pNewStart[d] + pNewCount[d];
1928:       offset = pNewEnd[d];

1930:     }
1931:     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]);
1932:     /* get the current closure of the cell that we are removing */
1933:     DMPlexGetTransitiveClosure(dm,cell,PETSC_TRUE,&nc,&cellClosure);

1935:     PetscMalloc1(pNewEnd[dim],&newConeSizes);
1936:     {
1937:       PetscInt kStart, kEnd, k, closureSizeK, *closureK = NULL, j;

1939:       DMPlexGetChart(K,&kStart,&kEnd);
1940:       PetscMalloc4(kEnd-kStart,&Kembedding,kEnd-kStart,&perm,kEnd-kStart,&iperm,kEnd-kStart,&preOrient);

1942:       for (k = kStart; k < kEnd; k++) {
1943:         perm[k - kStart] = k;
1944:         iperm [k - kStart] = k - kStart;
1945:         preOrient[k - kStart] = 0;
1946:       }

1948:       DMPlexGetTransitiveClosure(K,0,PETSC_TRUE,&closureSizeK,&closureK);
1949:       for (j = 1; j < closureSizeK; j++) {
1950:         PetscInt parentOrientA = closureK[2*j+1];
1951:         PetscInt parentOrientB = cellClosure[2*j+1];
1952:         PetscInt p, q;

1954:         p = closureK[2*j];
1955:         q = cellClosure[2*j];
1956:         for (d = 0; d <= dim; d++) {
1957:           if (q >= pOldStart[d] && q < pOldEnd[d]) {
1958:             Kembedding[p] = (q - pOldStart[d]) + pNewStart[d];
1959:           }
1960:         }
1961:         if (parentOrientA != parentOrientB) {
1962:           PetscInt numChildren, i;
1963:           const PetscInt *children;

1965:           DMPlexGetTreeChildren(K,p,&numChildren,&children);
1966:           for (i = 0; i < numChildren; i++) {
1967:             PetscInt kPerm, oPerm;

1969:             k    = children[i];
1970:             DMPlexReferenceTreeGetChildSymmetry(K,p,parentOrientA,0,k,parentOrientB,&oPerm,&kPerm);
1971:             /* perm = what refTree position I'm in */
1972:             perm[kPerm-kStart]      = k;
1973:             /* iperm = who is at this position */
1974:             iperm[k-kStart]         = kPerm-kStart;
1975:             preOrient[kPerm-kStart] = oPerm;
1976:           }
1977:         }
1978:       }
1979:       DMPlexRestoreTransitiveClosure(K,0,PETSC_TRUE,&closureSizeK,&closureK);
1980:     }
1981:     PetscSectionSetChart(parentSection,0,pNewEnd[dim]);
1982:     offset = 0;
1983:     numNewCones = 0;
1984:     for (d = 0; d <= dim; d++) {
1985:       PetscInt kStart, kEnd, k;
1986:       PetscInt p;
1987:       PetscInt size;

1989:       for (p = pOldStart[d]; p < pOldEnd[d]; p++) {
1990:         /* skip cell 0 */
1991:         if (p == cell) continue;
1992:         /* old cones to new cones */
1993:         DMPlexGetConeSize(dm,p,&size);
1994:         newConeSizes[offset++] = size;
1995:         numNewCones += size;
1996:       }

1998:       DMPlexGetHeightStratum(K,d,&kStart,&kEnd);
1999:       for (k = kStart; k < kEnd; k++) {
2000:         PetscInt kParent;

2002:         DMPlexGetTreeParent(K,k,&kParent,NULL);
2003:         if (kParent != k) {
2004:           Kembedding[k] = offset;
2005:           DMPlexGetConeSize(K,k,&size);
2006:           newConeSizes[offset++] = size;
2007:           numNewCones += size;
2008:           if (kParent != 0) {
2009:             PetscSectionSetDof(parentSection,Kembedding[k],1);
2010:           }
2011:         }
2012:       }
2013:     }

2015:     PetscSectionSetUp(parentSection);
2016:     PetscSectionGetStorageSize(parentSection,&numPointsWithParents);
2017:     PetscMalloc2(numNewCones,&newCones,numNewCones,&newOrientations);
2018:     PetscMalloc2(numPointsWithParents,&parents,numPointsWithParents,&childIDs);

2020:     /* fill new cones */
2021:     offset = 0;
2022:     for (d = 0; d <= dim; d++) {
2023:       PetscInt kStart, kEnd, k, l;
2024:       PetscInt p;
2025:       PetscInt size;
2026:       const PetscInt *cone, *orientation;

2028:       for (p = pOldStart[d]; p < pOldEnd[d]; p++) {
2029:         /* skip cell 0 */
2030:         if (p == cell) continue;
2031:         /* old cones to new cones */
2032:         DMPlexGetConeSize(dm,p,&size);
2033:         DMPlexGetCone(dm,p,&cone);
2034:         DMPlexGetConeOrientation(dm,p,&orientation);
2035:         for (l = 0; l < size; l++) {
2036:           newCones[offset]          = (cone[l] - pOldStart[d + 1]) + pNewStart[d + 1];
2037:           newOrientations[offset++] = orientation[l];
2038:         }
2039:       }

2041:       DMPlexGetHeightStratum(K,d,&kStart,&kEnd);
2042:       for (k = kStart; k < kEnd; k++) {
2043:         PetscInt kPerm = perm[k], kParent;
2044:         PetscInt preO  = preOrient[k];

2046:         DMPlexGetTreeParent(K,k,&kParent,NULL);
2047:         if (kParent != k) {
2048:           /* embed new cones */
2049:           DMPlexGetConeSize(K,k,&size);
2050:           DMPlexGetCone(K,kPerm,&cone);
2051:           DMPlexGetConeOrientation(K,kPerm,&orientation);
2052:           for (l = 0; l < size; l++) {
2053:             PetscInt q, m = (preO >= 0) ? ((preO + l) % size) : ((size -(preO + 1) - l) % size);
2054:             PetscInt newO, lSize, oTrue;

2056:             q                         = iperm[cone[m]];
2057:             newCones[offset]          = Kembedding[q];
2058:             DMPlexGetConeSize(K,q,&lSize);
2059:             oTrue                     = orientation[m];
2060:             oTrue                     = ((!lSize) || (preOrient[k] >= 0)) ? oTrue : -(oTrue + 2);
2061:             newO                      = DihedralCompose(lSize,oTrue,preOrient[q]);
2062:             newOrientations[offset++] = newO;
2063:           }
2064:           if (kParent != 0) {
2065:             PetscInt newPoint = Kembedding[kParent];
2066:             PetscSectionGetOffset(parentSection,Kembedding[k],&pOffset);
2067:             parents[pOffset]  = newPoint;
2068:             childIDs[pOffset] = k;
2069:           }
2070:         }
2071:       }
2072:     }

2074:     PetscMalloc1(dim*(pNewEnd[dim]-pNewStart[dim]),&newVertexCoords);

2076:     /* fill coordinates */
2077:     offset = 0;
2078:     {
2079:       PetscInt kStart, kEnd, l;
2080:       PetscSection vSection;
2081:       PetscInt v;
2082:       Vec coords;
2083:       PetscScalar *coordvals;
2084:       PetscInt dof, off;
2085:       PetscReal v0[3], J[9], detJ;

2087: #if defined(PETSC_USE_DEBUG)
2088:       {
2089:         PetscInt k;
2090:         DMPlexGetHeightStratum(K,0,&kStart,&kEnd);
2091:         for (k = kStart; k < kEnd; k++) {
2092:           DMPlexComputeCellGeometryFEM(K, k, NULL, v0, J, NULL, &detJ);
2093:           if (detJ <= 0.) SETERRQ1 (PETSC_COMM_SELF,PETSC_ERR_PLIB,"reference tree cell %d has bad determinant",k);
2094:         }
2095:       }
2096: #endif
2097:       DMPlexComputeCellGeometryFEM(dm, cell, NULL, v0, J, NULL, &detJ);
2098:       DMGetCoordinateSection(dm,&vSection);
2099:       DMGetCoordinatesLocal(dm,&coords);
2100:       VecGetArray(coords,&coordvals);
2101:       for (v = pOldStart[dim]; v < pOldEnd[dim]; v++) {

2103:         PetscSectionGetDof(vSection,v,&dof);
2104:         PetscSectionGetOffset(vSection,v,&off);
2105:         for (l = 0; l < dof; l++) {
2106:           newVertexCoords[offset++] = coordvals[off + l];
2107:         }
2108:       }
2109:       VecRestoreArray(coords,&coordvals);

2111:       DMGetCoordinateSection(K,&vSection);
2112:       DMGetCoordinatesLocal(K,&coords);
2113:       VecGetArray(coords,&coordvals);
2114:       DMPlexGetDepthStratum(K,0,&kStart,&kEnd);
2115:       for (v = kStart; v < kEnd; v++) {
2116:         PetscReal coord[3], newCoord[3];
2117:         PetscInt  vPerm = perm[v];
2118:         PetscInt  kParent;
2119:         const PetscReal xi0[3] = {-1.,-1.,-1.};

2121:         DMPlexGetTreeParent(K,v,&kParent,NULL);
2122:         if (kParent != v) {
2123:           /* this is a new vertex */
2124:           PetscSectionGetOffset(vSection,vPerm,&off);
2125:           for (l = 0; l < dim; ++l) coord[l] = PetscRealPart(coordvals[off+l]);
2126:           CoordinatesRefToReal(dim, dim, xi0, v0, J, coord, newCoord);
2127:           for (l = 0; l < dim; ++l) newVertexCoords[offset+l] = newCoord[l];
2128:           offset += dim;
2129:         }
2130:       }
2131:       VecRestoreArray(coords,&coordvals);
2132:     }

2134:     /* need to reverse the order of pNewCount: vertices first, cells last */
2135:     for (d = 0; d < (dim + 1) / 2; d++) {
2136:       PetscInt tmp;

2138:       tmp = pNewCount[d];
2139:       pNewCount[d] = pNewCount[dim - d];
2140:       pNewCount[dim - d] = tmp;
2141:     }

2143:     DMPlexCreateFromDAG(*ncdm,dim,pNewCount,newConeSizes,newCones,newOrientations,newVertexCoords);
2144:     DMPlexSetReferenceTree(*ncdm,K);
2145:     DMPlexSetTree(*ncdm,parentSection,parents,childIDs);

2147:     /* clean up */
2148:     DMPlexRestoreTransitiveClosure(dm,cell,PETSC_TRUE,&nc,&cellClosure);
2149:     PetscFree5(pNewCount,pNewStart,pNewEnd,pOldStart,pOldEnd);
2150:     PetscFree(newConeSizes);
2151:     PetscFree2(newCones,newOrientations);
2152:     PetscFree(newVertexCoords);
2153:     PetscFree2(parents,childIDs);
2154:     PetscFree4(Kembedding,perm,iperm,preOrient);
2155:   }
2156:   else {
2157:     PetscInt    p, counts[4];
2158:     PetscInt    *coneSizes, *cones, *orientations;
2159:     Vec         coordVec;
2160:     PetscScalar *coords;

2162:     for (d = 0; d <= dim; d++) {
2163:       PetscInt dStart, dEnd;

2165:       DMPlexGetDepthStratum(dm,d,&dStart,&dEnd);
2166:       counts[d] = dEnd - dStart;
2167:     }
2168:     PetscMalloc1(pEnd-pStart,&coneSizes);
2169:     for (p = pStart; p < pEnd; p++) {
2170:       DMPlexGetConeSize(dm,p,&coneSizes[p-pStart]);
2171:     }
2172:     DMPlexGetCones(dm, &cones);
2173:     DMPlexGetConeOrientations(dm, &orientations);
2174:     DMGetCoordinatesLocal(dm,&coordVec);
2175:     VecGetArray(coordVec,&coords);

2177:     PetscSectionSetChart(parentSection,pStart,pEnd);
2178:     PetscSectionSetUp(parentSection);
2179:     DMPlexCreateFromDAG(*ncdm,dim,counts,coneSizes,cones,orientations,NULL);
2180:     DMPlexSetReferenceTree(*ncdm,K);
2181:     DMPlexSetTree(*ncdm,parentSection,NULL,NULL);
2182:     VecRestoreArray(coordVec,&coords);
2183:   }
2184:   PetscSectionDestroy(&parentSection);

2186:   return(0);
2187: }

2189: PetscErrorCode DMPlexComputeInterpolatorTree(DM coarse, DM fine, PetscSF coarseToFine, PetscInt *childIds, Mat mat)
2190: {
2191:   PetscSF           coarseToFineEmbedded;
2192:   PetscSection      globalCoarse, globalFine;
2193:   PetscSection      localCoarse, localFine;
2194:   PetscSection      aSec, cSec;
2195:   PetscSection      rootIndicesSec, rootMatricesSec;
2196:   PetscSection      leafIndicesSec, leafMatricesSec;
2197:   PetscInt          *rootIndices, *leafIndices;
2198:   PetscScalar       *rootMatrices, *leafMatrices;
2199:   IS                aIS;
2200:   const PetscInt    *anchors;
2201:   Mat               cMat;
2202:   PetscInt          numFields, maxFields;
2203:   PetscInt          pStartC, pEndC, pStartF, pEndF, p;
2204:   PetscInt          aStart, aEnd, cStart, cEnd;
2205:   PetscInt          *maxChildIds;
2206:   PetscInt          *offsets, *newOffsets, *offsetsCopy, *newOffsetsCopy, *rowOffsets, *numD, *numO;
2207:   const PetscInt    ***perms;
2208:   const PetscScalar ***flips;
2209:   PetscErrorCode    ierr;

2212:   DMPlexGetChart(coarse,&pStartC,&pEndC);
2213:   DMPlexGetChart(fine,&pStartF,&pEndF);
2214:   DMGetGlobalSection(fine,&globalFine);
2215:   { /* winnow fine points that don't have global dofs out of the sf */
2216:     PetscInt dof, cdof, numPointsWithDofs, offset, *pointsWithDofs, nleaves, l;
2217:     const PetscInt *leaves;

2219:     PetscSFGetGraph(coarseToFine,NULL,&nleaves,&leaves,NULL);
2220:     for (l = 0, numPointsWithDofs = 0; l < nleaves; l++) {
2221:       p = leaves ? leaves[l] : l;
2222:       PetscSectionGetDof(globalFine,p,&dof);
2223:       PetscSectionGetConstraintDof(globalFine,p,&cdof);
2224:       if ((dof - cdof) > 0) {
2225:         numPointsWithDofs++;
2226:       }
2227:     }
2228:     PetscMalloc1(numPointsWithDofs,&pointsWithDofs);
2229:     for (l = 0, offset = 0; l < nleaves; l++) {
2230:       p = leaves ? leaves[l] : l;
2231:       PetscSectionGetDof(globalFine,p,&dof);
2232:       PetscSectionGetConstraintDof(globalFine,p,&cdof);
2233:       if ((dof - cdof) > 0) {
2234:         pointsWithDofs[offset++] = l;
2235:       }
2236:     }
2237:     PetscSFCreateEmbeddedLeafSF(coarseToFine, numPointsWithDofs, pointsWithDofs, &coarseToFineEmbedded);
2238:     PetscFree(pointsWithDofs);
2239:   }
2240:   /* communicate back to the coarse mesh which coarse points have children (that may require interpolation) */
2241:   PetscMalloc1(pEndC-pStartC,&maxChildIds);
2242:   for (p = pStartC; p < pEndC; p++) {
2243:     maxChildIds[p - pStartC] = -2;
2244:   }
2245:   PetscSFReduceBegin(coarseToFineEmbedded,MPIU_INT,childIds,maxChildIds,MPIU_MAX);
2246:   PetscSFReduceEnd(coarseToFineEmbedded,MPIU_INT,childIds,maxChildIds,MPIU_MAX);

2248:   DMGetLocalSection(coarse,&localCoarse);
2249:   DMGetGlobalSection(coarse,&globalCoarse);

2251:   DMPlexGetAnchors(coarse,&aSec,&aIS);
2252:   ISGetIndices(aIS,&anchors);
2253:   PetscSectionGetChart(aSec,&aStart,&aEnd);

2255:   DMGetDefaultConstraints(coarse,&cSec,&cMat);
2256:   PetscSectionGetChart(cSec,&cStart,&cEnd);

2258:   /* create sections that will send to children the indices and matrices they will need to construct the interpolator */
2259:   PetscSectionCreate(PetscObjectComm((PetscObject)coarse),&rootIndicesSec);
2260:   PetscSectionCreate(PetscObjectComm((PetscObject)coarse),&rootMatricesSec);
2261:   PetscSectionSetChart(rootIndicesSec,pStartC,pEndC);
2262:   PetscSectionSetChart(rootMatricesSec,pStartC,pEndC);
2263:   PetscSectionGetNumFields(localCoarse,&numFields);
2264:   maxFields = PetscMax(1,numFields);
2265:   PetscMalloc7(maxFields+1,&offsets,maxFields+1,&offsetsCopy,maxFields+1,&newOffsets,maxFields+1,&newOffsetsCopy,maxFields+1,&rowOffsets,maxFields+1,&numD,maxFields+1,&numO);
2266:   PetscMalloc2(maxFields+1,(PetscInt****)&perms,maxFields+1,(PetscScalar****)&flips);
2267:   PetscMemzero((void *) perms, (maxFields+1) * sizeof(const PetscInt **));
2268:   PetscMemzero((void *) flips, (maxFields+1) * sizeof(const PetscScalar **));

2270:   for (p = pStartC; p < pEndC; p++) { /* count the sizes of the indices and matrices */
2271:     PetscInt dof, matSize   = 0;
2272:     PetscInt aDof           = 0;
2273:     PetscInt cDof           = 0;
2274:     PetscInt maxChildId     = maxChildIds[p - pStartC];
2275:     PetscInt numRowIndices  = 0;
2276:     PetscInt numColIndices  = 0;
2277:     PetscInt f;

2279:     PetscSectionGetDof(globalCoarse,p,&dof);
2280:     if (dof < 0) {
2281:       dof = -(dof + 1);
2282:     }
2283:     if (p >= aStart && p < aEnd) {
2284:       PetscSectionGetDof(aSec,p,&aDof);
2285:     }
2286:     if (p >= cStart && p < cEnd) {
2287:       PetscSectionGetDof(cSec,p,&cDof);
2288:     }
2289:     for (f = 0; f <= numFields; f++) offsets[f] = 0;
2290:     for (f = 0; f <= numFields; f++) newOffsets[f] = 0;
2291:     if (maxChildId >= 0) { /* this point has children (with dofs) that will need to be interpolated from the closure of p */
2292:       PetscInt *closure = NULL, closureSize, cl;

2294:       DMPlexGetTransitiveClosure(coarse,p,PETSC_TRUE,&closureSize,&closure);
2295:       for (cl = 0; cl < closureSize; cl++) { /* get the closure */
2296:         PetscInt c = closure[2 * cl], clDof;

2298:         PetscSectionGetDof(localCoarse,c,&clDof);
2299:         numRowIndices += clDof;
2300:         for (f = 0; f < numFields; f++) {
2301:           PetscSectionGetFieldDof(localCoarse,c,f,&clDof);
2302:           offsets[f + 1] += clDof;
2303:         }
2304:       }
2305:       for (f = 0; f < numFields; f++) {
2306:         offsets[f + 1]   += offsets[f];
2307:         newOffsets[f + 1] = offsets[f + 1];
2308:       }
2309:       /* get the number of indices needed and their field offsets */
2310:       DMPlexAnchorsModifyMat(coarse,localCoarse,closureSize,numRowIndices,closure,NULL,NULL,NULL,&numColIndices,NULL,NULL,newOffsets,PETSC_FALSE);
2311:       DMPlexRestoreTransitiveClosure(coarse,p,PETSC_TRUE,&closureSize,&closure);
2312:       if (!numColIndices) { /* there are no hanging constraint modifications, so the matrix is just the identity: do not send it */
2313:         numColIndices = numRowIndices;
2314:         matSize = 0;
2315:       }
2316:       else if (numFields) { /* we send one submat for each field: sum their sizes */
2317:         matSize = 0;
2318:         for (f = 0; f < numFields; f++) {
2319:           PetscInt numRow, numCol;

2321:           numRow = offsets[f + 1] - offsets[f];
2322:           numCol = newOffsets[f + 1] - newOffsets[f];
2323:           matSize += numRow * numCol;
2324:         }
2325:       }
2326:       else {
2327:         matSize = numRowIndices * numColIndices;
2328:       }
2329:     } else if (maxChildId == -1) {
2330:       if (cDof > 0) { /* this point's dofs are interpolated via cMat: get the submatrix of cMat */
2331:         PetscInt aOff, a;

2333:         PetscSectionGetOffset(aSec,p,&aOff);
2334:         for (f = 0; f < numFields; f++) {
2335:           PetscInt fDof;

2337:           PetscSectionGetFieldDof(localCoarse,p,f,&fDof);
2338:           offsets[f+1] = fDof;
2339:         }
2340:         for (a = 0; a < aDof; a++) {
2341:           PetscInt anchor = anchors[a + aOff], aLocalDof;

2343:           PetscSectionGetDof(localCoarse,anchor,&aLocalDof);
2344:           numColIndices += aLocalDof;
2345:           for (f = 0; f < numFields; f++) {
2346:             PetscInt fDof;

2348:             PetscSectionGetFieldDof(localCoarse,anchor,f,&fDof);
2349:             newOffsets[f+1] += fDof;
2350:           }
2351:         }
2352:         if (numFields) {
2353:           matSize = 0;
2354:           for (f = 0; f < numFields; f++) {
2355:             matSize += offsets[f+1] * newOffsets[f+1];
2356:           }
2357:         }
2358:         else {
2359:           matSize = numColIndices * dof;
2360:         }
2361:       }
2362:       else { /* no children, and no constraints on dofs: just get the global indices */
2363:         numColIndices = dof;
2364:         matSize       = 0;
2365:       }
2366:     }
2367:     /* we will pack the column indices with the field offsets */
2368:     PetscSectionSetDof(rootIndicesSec,p,numColIndices ? numColIndices+2*numFields : 0);
2369:     PetscSectionSetDof(rootMatricesSec,p,matSize);
2370:   }
2371:   PetscSectionSetUp(rootIndicesSec);
2372:   PetscSectionSetUp(rootMatricesSec);
2373:   {
2374:     PetscInt numRootIndices, numRootMatrices;

2376:     PetscSectionGetStorageSize(rootIndicesSec,&numRootIndices);
2377:     PetscSectionGetStorageSize(rootMatricesSec,&numRootMatrices);
2378:     PetscMalloc2(numRootIndices,&rootIndices,numRootMatrices,&rootMatrices);
2379:     for (p = pStartC; p < pEndC; p++) {
2380:       PetscInt    numRowIndices, numColIndices, matSize, dof;
2381:       PetscInt    pIndOff, pMatOff, f;
2382:       PetscInt    *pInd;
2383:       PetscInt    maxChildId = maxChildIds[p - pStartC];
2384:       PetscScalar *pMat = NULL;

2386:       PetscSectionGetDof(rootIndicesSec,p,&numColIndices);
2387:       if (!numColIndices) {
2388:         continue;
2389:       }
2390:       for (f = 0; f <= numFields; f++) {
2391:         offsets[f]        = 0;
2392:         newOffsets[f]     = 0;
2393:         offsetsCopy[f]    = 0;
2394:         newOffsetsCopy[f] = 0;
2395:       }
2396:       numColIndices -= 2 * numFields;
2397:       PetscSectionGetOffset(rootIndicesSec,p,&pIndOff);
2398:       pInd = &(rootIndices[pIndOff]);
2399:       PetscSectionGetDof(rootMatricesSec,p,&matSize);
2400:       if (matSize) {
2401:         PetscSectionGetOffset(rootMatricesSec,p,&pMatOff);
2402:         pMat = &rootMatrices[pMatOff];
2403:       }
2404:       PetscSectionGetDof(globalCoarse,p,&dof);
2405:       if (dof < 0) {
2406:         dof = -(dof + 1);
2407:       }
2408:       if (maxChildId >= 0) { /* build an identity matrix, apply matrix constraints on the right */
2409:         PetscInt i, j;
2410:         PetscInt numRowIndices = matSize / numColIndices;

2412:         if (!numRowIndices) { /* don't need to calculate the mat, just the indices */
2413:           PetscInt numIndices, *indices;
2414:           DMPlexGetClosureIndices(coarse,localCoarse,globalCoarse,p,&numIndices,&indices,offsets);
2415:           if (numIndices != numColIndices) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"mismatching constraint indices calculations");
2416:           for (i = 0; i < numColIndices; i++) {
2417:             pInd[i] = indices[i];
2418:           }
2419:           for (i = 0; i < numFields; i++) {
2420:             pInd[numColIndices + i]             = offsets[i+1];
2421:             pInd[numColIndices + numFields + i] = offsets[i+1];
2422:           }
2423:           DMPlexRestoreClosureIndices(coarse,localCoarse,globalCoarse,p,&numIndices,&indices,offsets);
2424:         }
2425:         else {
2426:           PetscInt closureSize, *closure = NULL, cl;
2427:           PetscScalar *pMatIn, *pMatModified;
2428:           PetscInt numPoints,*points;

2430:           DMGetWorkArray(coarse,numRowIndices * numRowIndices,MPIU_SCALAR,&pMatIn);
2431:           for (i = 0; i < numRowIndices; i++) { /* initialize to the identity */
2432:             for (j = 0; j < numRowIndices; j++) {
2433:               pMatIn[i * numRowIndices + j] = (i == j) ? 1. : 0.;
2434:             }
2435:           }
2436:           DMPlexGetTransitiveClosure(coarse, p, PETSC_TRUE, &closureSize, &closure);
2437:           for (f = 0; f < maxFields; f++) {
2438:             if (numFields) {PetscSectionGetFieldPointSyms(localCoarse,f,closureSize,closure,&perms[f],&flips[f]);}
2439:             else           {PetscSectionGetPointSyms(localCoarse,closureSize,closure,&perms[f],&flips[f]);}
2440:           }
2441:           if (numFields) {
2442:             for (cl = 0; cl < closureSize; cl++) {
2443:               PetscInt c = closure[2 * cl];

2445:               for (f = 0; f < numFields; f++) {
2446:                 PetscInt fDof;

2448:                 PetscSectionGetFieldDof(localCoarse,c,f,&fDof);
2449:                 offsets[f + 1] += fDof;
2450:               }
2451:             }
2452:             for (f = 0; f < numFields; f++) {
2453:               offsets[f + 1]   += offsets[f];
2454:               newOffsets[f + 1] = offsets[f + 1];
2455:             }
2456:           }
2457:           /* TODO : flips here ? */
2458:           /* apply hanging node constraints on the right, get the new points and the new offsets */
2459:           DMPlexAnchorsModifyMat(coarse,localCoarse,closureSize,numRowIndices,closure,perms,pMatIn,&numPoints,NULL,&points,&pMatModified,newOffsets,PETSC_FALSE);
2460:           for (f = 0; f < maxFields; f++) {
2461:             if (numFields) {PetscSectionRestoreFieldPointSyms(localCoarse,f,closureSize,closure,&perms[f],&flips[f]);}
2462:             else           {PetscSectionRestorePointSyms(localCoarse,closureSize,closure,&perms[f],&flips[f]);}
2463:           }
2464:           for (f = 0; f < maxFields; f++) {
2465:             if (numFields) {PetscSectionGetFieldPointSyms(localCoarse,f,numPoints,points,&perms[f],&flips[f]);}
2466:             else           {PetscSectionGetPointSyms(localCoarse,numPoints,points,&perms[f],&flips[f]);}
2467:           }
2468:           if (!numFields) {
2469:             for (i = 0; i < numRowIndices * numColIndices; i++) {
2470:               pMat[i] = pMatModified[i];
2471:             }
2472:           }
2473:           else {
2474:             PetscInt i, j, count;
2475:             for (f = 0, count = 0; f < numFields; f++) {
2476:               for (i = offsets[f]; i < offsets[f+1]; i++) {
2477:                 for (j = newOffsets[f]; j < newOffsets[f+1]; j++, count++) {
2478:                   pMat[count] = pMatModified[i * numColIndices + j];
2479:                 }
2480:               }
2481:             }
2482:           }
2483:           DMRestoreWorkArray(coarse,numRowIndices * numColIndices,MPIU_SCALAR,&pMatModified);
2484:           DMPlexRestoreTransitiveClosure(coarse, p, PETSC_TRUE, &closureSize, &closure);
2485:           DMRestoreWorkArray(coarse,numRowIndices * numColIndices,MPIU_SCALAR,&pMatIn);
2486:           if (numFields) {
2487:             for (f = 0; f < numFields; f++) {
2488:               pInd[numColIndices + f]             = offsets[f+1];
2489:               pInd[numColIndices + numFields + f] = newOffsets[f+1];
2490:             }
2491:             for (cl = 0; cl < numPoints; cl++) {
2492:               PetscInt globalOff, c = points[2*cl];
2493:               PetscSectionGetOffset(globalCoarse, c, &globalOff);
2494:               DMPlexGetIndicesPointFields_Internal(localCoarse, PETSC_FALSE, c, globalOff < 0 ? -(globalOff+1) : globalOff, newOffsets, PETSC_FALSE, perms, cl, NULL, pInd);
2495:             }
2496:           } else {
2497:             for (cl = 0; cl < numPoints; cl++) {
2498:               PetscInt c = points[2*cl], globalOff;
2499:               const PetscInt *perm = perms[0] ? perms[0][cl] : NULL;

2501:               PetscSectionGetOffset(globalCoarse, c, &globalOff);
2502:               DMPlexGetIndicesPoint_Internal(localCoarse, PETSC_FALSE, c, globalOff < 0 ? -(globalOff+1) : globalOff, newOffsets, PETSC_FALSE, perm, NULL, pInd);
2503:             }
2504:           }
2505:           for (f = 0; f < maxFields; f++) {
2506:             if (numFields) {PetscSectionRestoreFieldPointSyms(localCoarse,f,numPoints,points,&perms[f],&flips[f]);}
2507:             else           {PetscSectionRestorePointSyms(localCoarse,numPoints,points,&perms[f],&flips[f]);}
2508:           }
2509:           DMRestoreWorkArray(coarse,numPoints,MPIU_SCALAR,&points);
2510:         }
2511:       }
2512:       else if (matSize) {
2513:         PetscInt cOff;
2514:         PetscInt *rowIndices, *colIndices, a, aDof, aOff;

2516:         numRowIndices = matSize / numColIndices;
2517:         if (numRowIndices != dof) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Miscounted dofs");
2518:         DMGetWorkArray(coarse,numRowIndices,MPIU_INT,&rowIndices);
2519:         DMGetWorkArray(coarse,numColIndices,MPIU_INT,&colIndices);
2520:         PetscSectionGetOffset(cSec,p,&cOff);
2521:         PetscSectionGetDof(aSec,p,&aDof);
2522:         PetscSectionGetOffset(aSec,p,&aOff);
2523:         if (numFields) {
2524:           for (f = 0; f < numFields; f++) {
2525:             PetscInt fDof;

2527:             PetscSectionGetFieldDof(cSec,p,f,&fDof);
2528:             offsets[f + 1] = fDof;
2529:             for (a = 0; a < aDof; a++) {
2530:               PetscInt anchor = anchors[a + aOff];
2531:               PetscSectionGetFieldDof(localCoarse,anchor,f,&fDof);
2532:               newOffsets[f + 1] += fDof;
2533:             }
2534:           }
2535:           for (f = 0; f < numFields; f++) {
2536:             offsets[f + 1]       += offsets[f];
2537:             offsetsCopy[f + 1]    = offsets[f + 1];
2538:             newOffsets[f + 1]    += newOffsets[f];
2539:             newOffsetsCopy[f + 1] = newOffsets[f + 1];
2540:           }
2541:           DMPlexGetIndicesPointFields_Internal(cSec,PETSC_TRUE,p,cOff,offsetsCopy,PETSC_TRUE,NULL,-1, NULL,rowIndices);
2542:           for (a = 0; a < aDof; a++) {
2543:             PetscInt anchor = anchors[a + aOff], lOff;
2544:             PetscSectionGetOffset(localCoarse,anchor,&lOff);
2545:             DMPlexGetIndicesPointFields_Internal(localCoarse,PETSC_TRUE,anchor,lOff,newOffsetsCopy,PETSC_TRUE,NULL,-1, NULL,colIndices);
2546:           }
2547:         }
2548:         else {
2549:           DMPlexGetIndicesPoint_Internal(cSec,PETSC_TRUE,p,cOff,offsetsCopy,PETSC_TRUE,NULL, NULL,rowIndices);
2550:           for (a = 0; a < aDof; a++) {
2551:             PetscInt anchor = anchors[a + aOff], lOff;
2552:             PetscSectionGetOffset(localCoarse,anchor,&lOff);
2553:             DMPlexGetIndicesPoint_Internal(localCoarse,PETSC_TRUE,anchor,lOff,newOffsetsCopy,PETSC_TRUE,NULL, NULL,colIndices);
2554:           }
2555:         }
2556:         if (numFields) {
2557:           PetscInt count, 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:             MatGetValues(cMat,iSize,&rowIndices[offsets[f]],jSize,&colIndices[newOffsets[f]],&pMat[count]);
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:             PetscSectionGetOffset(globalCoarse,anchor,&gOff);
2571:             DMPlexGetIndicesPointFields_Internal(localCoarse,PETSC_FALSE,anchor,gOff < 0 ? -(gOff + 1) : gOff,newOffsets,PETSC_FALSE,NULL,-1, NULL,pInd);
2572:           }
2573:         }
2574:         else {
2575:           PetscInt a;
2576:           MatGetValues(cMat,numRowIndices,rowIndices,numColIndices,colIndices,pMat);
2577:           for (a = 0; a < aDof; a++) {
2578:             PetscInt anchor = anchors[a + aOff];
2579:             PetscInt gOff;
2580:             PetscSectionGetOffset(globalCoarse,anchor,&gOff);
2581:             DMPlexGetIndicesPoint_Internal(localCoarse,PETSC_FALSE,anchor,gOff < 0 ? -(gOff + 1) : gOff,newOffsets,PETSC_FALSE,NULL, NULL,pInd);
2582:           }
2583:         }
2584:         DMRestoreWorkArray(coarse,numColIndices,MPIU_INT,&colIndices);
2585:         DMRestoreWorkArray(coarse,numRowIndices,MPIU_INT,&rowIndices);
2586:       }
2587:       else {
2588:         PetscInt gOff;

2590:         PetscSectionGetOffset(globalCoarse,p,&gOff);
2591:         if (numFields) {
2592:           for (f = 0; f < numFields; f++) {
2593:             PetscInt fDof;
2594:             PetscSectionGetFieldDof(localCoarse,p,f,&fDof);
2595:             offsets[f + 1] = fDof + offsets[f];
2596:           }
2597:           for (f = 0; f < numFields; f++) {
2598:             pInd[numColIndices + f]             = offsets[f+1];
2599:             pInd[numColIndices + numFields + f] = offsets[f+1];
2600:           }
2601:           DMPlexGetIndicesPointFields_Internal(localCoarse,PETSC_FALSE,p,gOff < 0 ? -(gOff + 1) : gOff,offsets,PETSC_FALSE,NULL,-1, NULL,pInd);
2602:         } else {
2603:           DMPlexGetIndicesPoint_Internal(localCoarse,PETSC_FALSE,p,gOff < 0 ? -(gOff + 1) : gOff,offsets,PETSC_FALSE,NULL, NULL,pInd);
2604:         }
2605:       }
2606:     }
2607:     PetscFree(maxChildIds);
2608:   }
2609:   {
2610:     PetscSF  indicesSF, matricesSF;
2611:     PetscInt *remoteOffsetsIndices, *remoteOffsetsMatrices, numLeafIndices, numLeafMatrices;

2613:     PetscSectionCreate(PetscObjectComm((PetscObject)fine),&leafIndicesSec);
2614:     PetscSectionCreate(PetscObjectComm((PetscObject)fine),&leafMatricesSec);
2615:     PetscSFDistributeSection(coarseToFineEmbedded,rootIndicesSec,&remoteOffsetsIndices,leafIndicesSec);
2616:     PetscSFDistributeSection(coarseToFineEmbedded,rootMatricesSec,&remoteOffsetsMatrices,leafMatricesSec);
2617:     PetscSFCreateSectionSF(coarseToFineEmbedded,rootIndicesSec,remoteOffsetsIndices,leafIndicesSec,&indicesSF);
2618:     PetscSFCreateSectionSF(coarseToFineEmbedded,rootMatricesSec,remoteOffsetsMatrices,leafMatricesSec,&matricesSF);
2619:     PetscSFDestroy(&coarseToFineEmbedded);
2620:     PetscFree(remoteOffsetsIndices);
2621:     PetscFree(remoteOffsetsMatrices);
2622:     PetscSectionGetStorageSize(leafIndicesSec,&numLeafIndices);
2623:     PetscSectionGetStorageSize(leafMatricesSec,&numLeafMatrices);
2624:     PetscMalloc2(numLeafIndices,&leafIndices,numLeafMatrices,&leafMatrices);
2625:     PetscSFBcastBegin(indicesSF,MPIU_INT,rootIndices,leafIndices);
2626:     PetscSFBcastBegin(matricesSF,MPIU_SCALAR,rootMatrices,leafMatrices);
2627:     PetscSFBcastEnd(indicesSF,MPIU_INT,rootIndices,leafIndices);
2628:     PetscSFBcastEnd(matricesSF,MPIU_SCALAR,rootMatrices,leafMatrices);
2629:     PetscSFDestroy(&matricesSF);
2630:     PetscSFDestroy(&indicesSF);
2631:     PetscFree2(rootIndices,rootMatrices);
2632:     PetscSectionDestroy(&rootIndicesSec);
2633:     PetscSectionDestroy(&rootMatricesSec);
2634:   }
2635:   /* count to preallocate */
2636:   DMGetLocalSection(fine,&localFine);
2637:   {
2638:     PetscInt    nGlobal;
2639:     PetscInt    *dnnz, *onnz;
2640:     PetscLayout rowMap, colMap;
2641:     PetscInt    rowStart, rowEnd, colStart, colEnd;
2642:     PetscInt    maxDof;
2643:     PetscInt    *rowIndices;
2644:     DM           refTree;
2645:     PetscInt     **refPointFieldN;
2646:     PetscScalar  ***refPointFieldMats;
2647:     PetscSection refConSec, refAnSec;
2648:     PetscInt     pRefStart,pRefEnd,maxConDof,maxColumns,leafStart,leafEnd;
2649:     PetscScalar  *pointWork;

2651:     PetscSectionGetConstrainedStorageSize(globalFine,&nGlobal);
2652:     PetscCalloc2(nGlobal,&dnnz,nGlobal,&onnz);
2653:     MatGetLayouts(mat,&rowMap,&colMap);
2654:     PetscLayoutSetUp(rowMap);
2655:     PetscLayoutSetUp(colMap);
2656:     PetscLayoutGetRange(rowMap,&rowStart,&rowEnd);
2657:     PetscLayoutGetRange(colMap,&colStart,&colEnd);
2658:     PetscSectionGetMaxDof(globalFine,&maxDof);
2659:     PetscSectionGetChart(leafIndicesSec,&leafStart,&leafEnd);
2660:     DMGetWorkArray(fine,maxDof,MPIU_INT,&rowIndices);
2661:     for (p = leafStart; p < leafEnd; p++) {
2662:       PetscInt    gDof, gcDof, gOff;
2663:       PetscInt    numColIndices, pIndOff, *pInd;
2664:       PetscInt    matSize;
2665:       PetscInt    i;

2667:       PetscSectionGetDof(globalFine,p,&gDof);
2668:       PetscSectionGetConstraintDof(globalFine,p,&gcDof);
2669:       if ((gDof - gcDof) <= 0) {
2670:         continue;
2671:       }
2672:       PetscSectionGetOffset(globalFine,p,&gOff);
2673:       if (gOff < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"I though having global dofs meant a non-negative offset");
2674:       if ((gOff < rowStart) || ((gOff + gDof - gcDof) > rowEnd)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"I thought the row map would constrain the global dofs");
2675:       PetscSectionGetDof(leafIndicesSec,p,&numColIndices);
2676:       PetscSectionGetOffset(leafIndicesSec,p,&pIndOff);
2677:       numColIndices -= 2 * numFields;
2678:       if (numColIndices <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"global fine dof with no dofs to interpolate from");
2679:       pInd = &leafIndices[pIndOff];
2680:       offsets[0]        = 0;
2681:       offsetsCopy[0]    = 0;
2682:       newOffsets[0]     = 0;
2683:       newOffsetsCopy[0] = 0;
2684:       if (numFields) {
2685:         PetscInt f;
2686:         for (f = 0; f < numFields; f++) {
2687:           PetscInt rowDof;

2689:           PetscSectionGetFieldDof(localFine,p,f,&rowDof);
2690:           offsets[f + 1]        = offsets[f] + rowDof;
2691:           offsetsCopy[f + 1]    = offsets[f + 1];
2692:           newOffsets[f + 1]     = pInd[numColIndices + numFields + f];
2693:           numD[f] = 0;
2694:           numO[f] = 0;
2695:         }
2696:         DMPlexGetIndicesPointFields_Internal(localFine,PETSC_FALSE,p,gOff,offsetsCopy,PETSC_FALSE,NULL,-1, NULL,rowIndices);
2697:         for (f = 0; f < numFields; f++) {
2698:           PetscInt colOffset    = newOffsets[f];
2699:           PetscInt numFieldCols = newOffsets[f + 1] - newOffsets[f];

2701:           for (i = 0; i < numFieldCols; i++) {
2702:             PetscInt gInd = pInd[i + colOffset];

2704:             if (gInd >= colStart && gInd < colEnd) {
2705:               numD[f]++;
2706:             }
2707:             else if (gInd >= 0) { /* negative means non-entry */
2708:               numO[f]++;
2709:             }
2710:           }
2711:         }
2712:       }
2713:       else {
2714:         DMPlexGetIndicesPoint_Internal(localFine,PETSC_FALSE,p,gOff,offsetsCopy,PETSC_FALSE,NULL, NULL,rowIndices);
2715:         numD[0] = 0;
2716:         numO[0] = 0;
2717:         for (i = 0; i < numColIndices; i++) {
2718:           PetscInt gInd = pInd[i];

2720:           if (gInd >= colStart && gInd < colEnd) {
2721:             numD[0]++;
2722:           }
2723:           else if (gInd >= 0) { /* negative means non-entry */
2724:             numO[0]++;
2725:           }
2726:         }
2727:       }
2728:       PetscSectionGetDof(leafMatricesSec,p,&matSize);
2729:       if (!matSize) { /* incoming matrix is identity */
2730:         PetscInt childId;

2732:         childId = childIds[p-pStartF];
2733:         if (childId < 0) { /* no child interpolation: one nnz per */
2734:           if (numFields) {
2735:             PetscInt f;
2736:             for (f = 0; f < numFields; f++) {
2737:               PetscInt numRows = offsets[f+1] - offsets[f], row;
2738:               for (row = 0; row < numRows; row++) {
2739:                 PetscInt gIndCoarse = pInd[newOffsets[f] + row];
2740:                 PetscInt gIndFine   = rowIndices[offsets[f] + row];
2741:                 if (gIndCoarse >= colStart && gIndCoarse < colEnd) { /* local */
2742:                   if (gIndFine < rowStart || gIndFine >= rowEnd) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Mismatched number of constrained dofs");
2743:                   dnnz[gIndFine - rowStart] = 1;
2744:                 }
2745:                 else if (gIndCoarse >= 0) { /* remote */
2746:                   if (gIndFine < rowStart || gIndFine >= rowEnd) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Mismatched number of constrained dofs");
2747:                   onnz[gIndFine - rowStart] = 1;
2748:                 }
2749:                 else { /* constrained */
2750:                   if (gIndFine >= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Mismatched number of constrained dofs");
2751:                 }
2752:               }
2753:             }
2754:           }
2755:           else {
2756:             PetscInt i;
2757:             for (i = 0; i < gDof; i++) {
2758:               PetscInt gIndCoarse = pInd[i];
2759:               PetscInt gIndFine   = rowIndices[i];
2760:               if (gIndCoarse >= colStart && gIndCoarse < colEnd) { /* local */
2761:                 if (gIndFine < rowStart || gIndFine >= rowEnd) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Mismatched number of constrained dofs");
2762:                 dnnz[gIndFine - rowStart] = 1;
2763:               }
2764:               else if (gIndCoarse >= 0) { /* remote */
2765:                 if (gIndFine < rowStart || gIndFine >= rowEnd) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Mismatched number of constrained dofs");
2766:                 onnz[gIndFine - rowStart] = 1;
2767:               }
2768:               else { /* constrained */
2769:                 if (gIndFine >= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Mismatched number of constrained dofs");
2770:               }
2771:             }
2772:           }
2773:         }
2774:         else { /* interpolate from all */
2775:           if (numFields) {
2776:             PetscInt f;
2777:             for (f = 0; f < numFields; f++) {
2778:               PetscInt numRows = offsets[f+1] - offsets[f], row;
2779:               for (row = 0; row < numRows; row++) {
2780:                 PetscInt gIndFine = rowIndices[offsets[f] + row];
2781:                 if (gIndFine >= 0) {
2782:                   if (gIndFine < rowStart || gIndFine >= rowEnd) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Mismatched number of constrained dofs");
2783:                   dnnz[gIndFine - rowStart] = numD[f];
2784:                   onnz[gIndFine - rowStart] = numO[f];
2785:                 }
2786:               }
2787:             }
2788:           }
2789:           else {
2790:             PetscInt i;
2791:             for (i = 0; i < gDof; i++) {
2792:               PetscInt gIndFine = rowIndices[i];
2793:               if (gIndFine >= 0) {
2794:                 if (gIndFine < rowStart || gIndFine >= rowEnd) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Mismatched number of constrained dofs");
2795:                 dnnz[gIndFine - rowStart] = numD[0];
2796:                 onnz[gIndFine - rowStart] = numO[0];
2797:               }
2798:             }
2799:           }
2800:         }
2801:       }
2802:       else { /* interpolate from all */
2803:         if (numFields) {
2804:           PetscInt f;
2805:           for (f = 0; f < numFields; f++) {
2806:             PetscInt numRows = offsets[f+1] - offsets[f], row;
2807:             for (row = 0; row < numRows; row++) {
2808:               PetscInt gIndFine = rowIndices[offsets[f] + row];
2809:               if (gIndFine >= 0) {
2810:                 if (gIndFine < rowStart || gIndFine >= rowEnd) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Mismatched number of constrained dofs");
2811:                 dnnz[gIndFine - rowStart] = numD[f];
2812:                 onnz[gIndFine - rowStart] = numO[f];
2813:               }
2814:             }
2815:           }
2816:         }
2817:         else { /* every dof get a full row */
2818:           PetscInt i;
2819:           for (i = 0; i < gDof; i++) {
2820:             PetscInt gIndFine = rowIndices[i];
2821:             if (gIndFine >= 0) {
2822:               if (gIndFine < rowStart || gIndFine >= rowEnd) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Mismatched number of constrained dofs");
2823:               dnnz[gIndFine - rowStart] = numD[0];
2824:               onnz[gIndFine - rowStart] = numO[0];
2825:             }
2826:           }
2827:         }
2828:       }
2829:     }
2830:     MatXAIJSetPreallocation(mat,1,dnnz,onnz,NULL,NULL);
2831:     PetscFree2(dnnz,onnz);

2833:     DMPlexGetReferenceTree(fine,&refTree);
2834:     DMPlexReferenceTreeGetChildrenMatrices(refTree,&refPointFieldMats,&refPointFieldN);
2835:     DMGetDefaultConstraints(refTree,&refConSec,NULL);
2836:     DMPlexGetAnchors(refTree,&refAnSec,NULL);
2837:     PetscSectionGetChart(refConSec,&pRefStart,&pRefEnd);
2838:     PetscSectionGetMaxDof(refConSec,&maxConDof);
2839:     PetscSectionGetMaxDof(leafIndicesSec,&maxColumns);
2840:     PetscMalloc1(maxConDof*maxColumns,&pointWork);
2841:     for (p = leafStart; p < leafEnd; p++) {
2842:       PetscInt gDof, gcDof, gOff;
2843:       PetscInt numColIndices, pIndOff, *pInd;
2844:       PetscInt matSize;
2845:       PetscInt childId;

2847:       PetscSectionGetDof(globalFine,p,&gDof);
2848:       PetscSectionGetConstraintDof(globalFine,p,&gcDof);
2849:       if ((gDof - gcDof) <= 0) {
2850:         continue;
2851:       }
2852:       childId = childIds[p-pStartF];
2853:       PetscSectionGetOffset(globalFine,p,&gOff);
2854:       PetscSectionGetDof(leafIndicesSec,p,&numColIndices);
2855:       PetscSectionGetOffset(leafIndicesSec,p,&pIndOff);
2856:       numColIndices -= 2 * numFields;
2857:       pInd = &leafIndices[pIndOff];
2858:       offsets[0]        = 0;
2859:       offsetsCopy[0]    = 0;
2860:       newOffsets[0]     = 0;
2861:       newOffsetsCopy[0] = 0;
2862:       rowOffsets[0]     = 0;
2863:       if (numFields) {
2864:         PetscInt f;
2865:         for (f = 0; f < numFields; f++) {
2866:           PetscInt rowDof;

2868:           PetscSectionGetFieldDof(localFine,p,f,&rowDof);
2869:           offsets[f + 1]     = offsets[f] + rowDof;
2870:           offsetsCopy[f + 1] = offsets[f + 1];
2871:           rowOffsets[f + 1]  = pInd[numColIndices + f];
2872:           newOffsets[f + 1]  = pInd[numColIndices + numFields + f];
2873:         }
2874:         DMPlexGetIndicesPointFields_Internal(localFine,PETSC_FALSE,p,gOff,offsetsCopy,PETSC_FALSE,NULL,-1, NULL,rowIndices);
2875:       }
2876:       else {
2877:         DMPlexGetIndicesPoint_Internal(localFine,PETSC_FALSE,p,gOff,offsetsCopy,PETSC_FALSE,NULL, NULL,rowIndices);
2878:       }
2879:       PetscSectionGetDof(leafMatricesSec,p,&matSize);
2880:       if (!matSize) { /* incoming matrix is identity */
2881:         if (childId < 0) { /* no child interpolation: scatter */
2882:           if (numFields) {
2883:             PetscInt f;
2884:             for (f = 0; f < numFields; f++) {
2885:               PetscInt numRows = offsets[f+1] - offsets[f], row;
2886:               for (row = 0; row < numRows; row++) {
2887:                 MatSetValue(mat,rowIndices[offsets[f]+row],pInd[newOffsets[f]+row],1.,INSERT_VALUES);
2888:               }
2889:             }
2890:           }
2891:           else {
2892:             PetscInt numRows = gDof, row;
2893:             for (row = 0; row < numRows; row++) {
2894:               MatSetValue(mat,rowIndices[row],pInd[row],1.,INSERT_VALUES);
2895:             }
2896:           }
2897:         }
2898:         else { /* interpolate from all */
2899:           if (numFields) {
2900:             PetscInt f;
2901:             for (f = 0; f < numFields; f++) {
2902:               PetscInt numRows = offsets[f+1] - offsets[f];
2903:               PetscInt numCols = newOffsets[f + 1] - newOffsets[f];
2904:               MatSetValues(mat,numRows,&rowIndices[offsets[f]],numCols,&pInd[newOffsets[f]],refPointFieldMats[childId - pRefStart][f],INSERT_VALUES);
2905:             }
2906:           }
2907:           else {
2908:             MatSetValues(mat,gDof,rowIndices,numColIndices,pInd,refPointFieldMats[childId - pRefStart][0],INSERT_VALUES);
2909:           }
2910:         }
2911:       }
2912:       else { /* interpolate from all */
2913:         PetscInt    pMatOff;
2914:         PetscScalar *pMat;

2916:         PetscSectionGetOffset(leafMatricesSec,p,&pMatOff);
2917:         pMat = &leafMatrices[pMatOff];
2918:         if (childId < 0) { /* copy the incoming matrix */
2919:           if (numFields) {
2920:             PetscInt f, count;
2921:             for (f = 0, count = 0; f < numFields; f++) {
2922:               PetscInt numRows = offsets[f+1]-offsets[f];
2923:               PetscInt numCols = newOffsets[f+1]-newOffsets[f];
2924:               PetscInt numInRows = rowOffsets[f+1]-rowOffsets[f];
2925:               PetscScalar *inMat = &pMat[count];

2927:               MatSetValues(mat,numRows,&rowIndices[offsets[f]],numCols,&pInd[newOffsets[f]],inMat,INSERT_VALUES);
2928:               count += numCols * numInRows;
2929:             }
2930:           }
2931:           else {
2932:             MatSetValues(mat,gDof,rowIndices,numColIndices,pInd,pMat,INSERT_VALUES);
2933:           }
2934:         }
2935:         else { /* multiply the incoming matrix by the child interpolation */
2936:           if (numFields) {
2937:             PetscInt f, count;
2938:             for (f = 0, count = 0; f < numFields; f++) {
2939:               PetscInt numRows = offsets[f+1]-offsets[f];
2940:               PetscInt numCols = newOffsets[f+1]-newOffsets[f];
2941:               PetscInt numInRows = rowOffsets[f+1]-rowOffsets[f];
2942:               PetscScalar *inMat = &pMat[count];
2943:               PetscInt i, j, k;
2944:               if (refPointFieldN[childId - pRefStart][f] != numInRows) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Point constraint matrix multiply dimension mismatch");
2945:               for (i = 0; i < numRows; i++) {
2946:                 for (j = 0; j < numCols; j++) {
2947:                   PetscScalar val = 0.;
2948:                   for (k = 0; k < numInRows; k++) {
2949:                     val += refPointFieldMats[childId - pRefStart][f][i * numInRows + k] * inMat[k * numCols + j];
2950:                   }
2951:                   pointWork[i * numCols + j] = val;
2952:                 }
2953:               }
2954:               MatSetValues(mat,numRows,&rowIndices[offsets[f]],numCols,&pInd[newOffsets[f]],pointWork,INSERT_VALUES);
2955:               count += numCols * numInRows;
2956:             }
2957:           }
2958:           else { /* every dof gets a full row */
2959:             PetscInt numRows   = gDof;
2960:             PetscInt numCols   = numColIndices;
2961:             PetscInt numInRows = matSize / numColIndices;
2962:             PetscInt i, j, k;
2963:             if (refPointFieldN[childId - pRefStart][0] != numInRows) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Point constraint matrix multiply dimension mismatch");
2964:             for (i = 0; i < numRows; i++) {
2965:               for (j = 0; j < numCols; j++) {
2966:                 PetscScalar val = 0.;
2967:                 for (k = 0; k < numInRows; k++) {
2968:                   val += refPointFieldMats[childId - pRefStart][0][i * numInRows + k] * pMat[k * numCols + j];
2969:                 }
2970:                 pointWork[i * numCols + j] = val;
2971:               }
2972:             }
2973:             MatSetValues(mat,numRows,rowIndices,numCols,pInd,pointWork,INSERT_VALUES);
2974:           }
2975:         }
2976:       }
2977:     }
2978:     DMPlexReferenceTreeRestoreChildrenMatrices(refTree,&refPointFieldMats,&refPointFieldN);
2979:     DMRestoreWorkArray(fine,maxDof,MPIU_INT,&rowIndices);
2980:     PetscFree(pointWork);
2981:   }
2982:   MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
2983:   MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
2984:   PetscSectionDestroy(&leafIndicesSec);
2985:   PetscSectionDestroy(&leafMatricesSec);
2986:   PetscFree2(leafIndices,leafMatrices);
2987:   PetscFree2(*(PetscInt****)&perms,*(PetscScalar****)&flips);
2988:   PetscFree7(offsets,offsetsCopy,newOffsets,newOffsetsCopy,rowOffsets,numD,numO);
2989:   ISRestoreIndices(aIS,&anchors);
2990:   return(0);
2991: }

2993: /*
2994:  * Assuming a nodal basis (w.r.t. the dual basis) basis:
2995:  *
2996:  * for each coarse dof \phi^c_i:
2997:  *   for each quadrature point (w_l,x_l) in the dual basis definition of \phi^c_i:
2998:  *     for each fine dof \phi^f_j;
2999:  *       a_{i,j} = 0;
3000:  *       for each fine dof \phi^f_k:
3001:  *         a_{i,j} += interp_{i,k} * \phi^f_k(x_l) * \phi^f_j(x_l) * w_l
3002:  *                    [^^^ this is = \phi^c_i ^^^]
3003:  */
3004: PetscErrorCode DMPlexComputeInjectorReferenceTree(DM refTree, Mat *inj)
3005: {
3006:   PetscDS        ds;
3007:   PetscSection   section, cSection;
3008:   DMLabel        canonical, depth;
3009:   Mat            cMat, mat;
3010:   PetscInt       *nnz;
3011:   PetscInt       f, dim, numFields, numSecFields, p, pStart, pEnd, cStart, cEnd;
3012:   PetscInt       m, n;
3013:   PetscScalar    *pointScalar;
3014:   PetscReal      *v0, *v0parent, *vtmp, *J, *Jparent, *invJ, *pointRef, detJ, detJparent;

3018:   DMGetLocalSection(refTree,&section);
3019:   DMGetDimension(refTree, &dim);
3020:   PetscMalloc6(dim,&v0,dim,&v0parent,dim,&vtmp,dim*dim,&J,dim*dim,&Jparent,dim*dim,&invJ);
3021:   PetscMalloc2(dim,&pointScalar,dim,&pointRef);
3022:   DMGetDS(refTree,&ds);
3023:   PetscDSGetNumFields(ds,&numFields);
3024:   PetscSectionGetNumFields(section,&numSecFields);
3025:   DMGetLabel(refTree,"canonical",&canonical);
3026:   DMGetLabel(refTree,"depth",&depth);
3027:   DMGetDefaultConstraints(refTree,&cSection,&cMat);
3028:   DMPlexGetChart(refTree, &pStart, &pEnd);
3029:   DMPlexGetHeightStratum(refTree, 0, &cStart, &cEnd);
3030:   MatGetSize(cMat,&n,&m); /* the injector has transpose sizes from the constraint matrix */
3031:   /* Step 1: compute non-zero pattern.  A proper subset of constraint matrix non-zero */
3032:   PetscCalloc1(m,&nnz);
3033:   for (p = pStart; p < pEnd; p++) { /* a point will have non-zeros if it is canonical, it has dofs, and its children have dofs */
3034:     const PetscInt *children;
3035:     PetscInt numChildren;
3036:     PetscInt i, numChildDof, numSelfDof;

3038:     if (canonical) {
3039:       PetscInt pCanonical;
3040:       DMLabelGetValue(canonical,p,&pCanonical);
3041:       if (p != pCanonical) continue;
3042:     }
3043:     DMPlexGetTreeChildren(refTree,p,&numChildren,&children);
3044:     if (!numChildren) continue;
3045:     for (i = 0, numChildDof = 0; i < numChildren; i++) {
3046:       PetscInt child = children[i];
3047:       PetscInt dof;

3049:       PetscSectionGetDof(section,child,&dof);
3050:       numChildDof += dof;
3051:     }
3052:     PetscSectionGetDof(section,p,&numSelfDof);
3053:     if (!numChildDof || !numSelfDof) continue;
3054:     for (f = 0; f < numFields; f++) {
3055:       PetscInt selfOff;

3057:       if (numSecFields) { /* count the dofs for just this field */
3058:         for (i = 0, numChildDof = 0; i < numChildren; i++) {
3059:           PetscInt child = children[i];
3060:           PetscInt dof;

3062:           PetscSectionGetFieldDof(section,child,f,&dof);
3063:           numChildDof += dof;
3064:         }
3065:         PetscSectionGetFieldDof(section,p,f,&numSelfDof);
3066:         PetscSectionGetFieldOffset(section,p,f,&selfOff);
3067:       }
3068:       else {
3069:         PetscSectionGetOffset(section,p,&selfOff);
3070:       }
3071:       for (i = 0; i < numSelfDof; i++) {
3072:         nnz[selfOff + i] = numChildDof;
3073:       }
3074:     }
3075:   }
3076:   MatCreateAIJ(PETSC_COMM_SELF,m,n,m,n,-1,nnz,-1,NULL,&mat);
3077:   PetscFree(nnz);
3078:   /* Setp 2: compute entries */
3079:   for (p = pStart; p < pEnd; p++) {
3080:     const PetscInt *children;
3081:     PetscInt numChildren;
3082:     PetscInt i, numChildDof, numSelfDof;

3084:     /* same conditions about when entries occur */
3085:     if (canonical) {
3086:       PetscInt pCanonical;
3087:       DMLabelGetValue(canonical,p,&pCanonical);
3088:       if (p != pCanonical) continue;
3089:     }
3090:     DMPlexGetTreeChildren(refTree,p,&numChildren,&children);
3091:     if (!numChildren) continue;
3092:     for (i = 0, numChildDof = 0; i < numChildren; i++) {
3093:       PetscInt child = children[i];
3094:       PetscInt dof;

3096:       PetscSectionGetDof(section,child,&dof);
3097:       numChildDof += dof;
3098:     }
3099:     PetscSectionGetDof(section,p,&numSelfDof);
3100:     if (!numChildDof || !numSelfDof) continue;

3102:     for (f = 0; f < numFields; f++) {
3103:       PetscInt       pI = -1, cI = -1;
3104:       PetscInt       selfOff, Nc, parentCell;
3105:       PetscInt       cellShapeOff;
3106:       PetscObject    disc;
3107:       PetscDualSpace dsp;
3108:       PetscClassId   classId;
3109:       PetscScalar    *pointMat;
3110:       PetscInt       *matRows, *matCols;
3111:       PetscInt       pO = PETSC_MIN_INT;
3112:       const PetscInt *depthNumDof;

3114:       if (numSecFields) {
3115:         for (i = 0, numChildDof = 0; i < numChildren; i++) {
3116:           PetscInt child = children[i];
3117:           PetscInt dof;

3119:           PetscSectionGetFieldDof(section,child,f,&dof);
3120:           numChildDof += dof;
3121:         }
3122:         PetscSectionGetFieldDof(section,p,f,&numSelfDof);
3123:         PetscSectionGetFieldOffset(section,p,f,&selfOff);
3124:       }
3125:       else {
3126:         PetscSectionGetOffset(section,p,&selfOff);
3127:       }

3129:       /* find a cell whose closure contains p */
3130:       if (p >= cStart && p < cEnd) {
3131:         parentCell = p;
3132:       }
3133:       else {
3134:         PetscInt *star = NULL;
3135:         PetscInt numStar;

3137:         parentCell = -1;
3138:         DMPlexGetTransitiveClosure(refTree,p,PETSC_FALSE,&numStar,&star);
3139:         for (i = numStar - 1; i >= 0; i--) {
3140:           PetscInt c = star[2 * i];

3142:           if (c >= cStart && c < cEnd) {
3143:             parentCell = c;
3144:             break;
3145:           }
3146:         }
3147:         DMPlexRestoreTransitiveClosure(refTree,p,PETSC_FALSE,&numStar,&star);
3148:       }
3149:       /* determine the offset of p's shape functions withing parentCell's shape functions */
3150:       PetscDSGetDiscretization(ds,f,&disc);
3151:       PetscObjectGetClassId(disc,&classId);
3152:       if (classId == PETSCFE_CLASSID) {
3153:         PetscFEGetDualSpace((PetscFE)disc,&dsp);
3154:       }
3155:       else if (classId == PETSCFV_CLASSID) {
3156:         PetscFVGetDualSpace((PetscFV)disc,&dsp);
3157:       }
3158:       else {
3159:         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported discretization object");
3160:       }
3161:       PetscDualSpaceGetNumDof(dsp,&depthNumDof);
3162:       PetscDualSpaceGetNumComponents(dsp,&Nc);
3163:       {
3164:         PetscInt *closure = NULL;
3165:         PetscInt numClosure;

3167:         DMPlexGetTransitiveClosure(refTree,parentCell,PETSC_TRUE,&numClosure,&closure);
3168:         for (i = 0, pI = -1, cellShapeOff = 0; i < numClosure; i++) {
3169:           PetscInt point = closure[2 * i], pointDepth;

3171:           pO = closure[2 * i + 1];
3172:           if (point == p) {
3173:             pI = i;
3174:             break;
3175:           }
3176:           DMLabelGetValue(depth,point,&pointDepth);
3177:           cellShapeOff += depthNumDof[pointDepth];
3178:         }
3179:         DMPlexRestoreTransitiveClosure(refTree,parentCell,PETSC_TRUE,&numClosure,&closure);
3180:       }

3182:       DMGetWorkArray(refTree, numSelfDof * numChildDof, MPIU_SCALAR,&pointMat);
3183:       DMGetWorkArray(refTree, numSelfDof + numChildDof, MPIU_INT,&matRows);
3184:       matCols = matRows + numSelfDof;
3185:       for (i = 0; i < numSelfDof; i++) {
3186:         matRows[i] = selfOff + i;
3187:       }
3188:       for (i = 0; i < numSelfDof * numChildDof; i++) pointMat[i] = 0.;
3189:       {
3190:         PetscInt colOff = 0;

3192:         for (i = 0; i < numChildren; i++) {
3193:           PetscInt child = children[i];
3194:           PetscInt dof, off, j;

3196:           if (numSecFields) {
3197:             PetscSectionGetFieldDof(cSection,child,f,&dof);
3198:             PetscSectionGetFieldOffset(cSection,child,f,&off);
3199:           }
3200:           else {
3201:             PetscSectionGetDof(cSection,child,&dof);
3202:             PetscSectionGetOffset(cSection,child,&off);
3203:           }

3205:           for (j = 0; j < dof; j++) {
3206:             matCols[colOff++] = off + j;
3207:           }
3208:         }
3209:       }
3210:       if (classId == PETSCFE_CLASSID) {
3211:         PetscFE        fe = (PetscFE) disc;
3212:         PetscInt       fSize;
3213:         const PetscInt ***perms;
3214:         const PetscScalar ***flips;
3215:         const PetscInt *pperms;


3218:         PetscFEGetDualSpace(fe,&dsp);
3219:         PetscDualSpaceGetDimension(dsp,&fSize);
3220:         PetscDualSpaceGetSymmetries(dsp, &perms, &flips);
3221:         pperms = perms ? perms[pI] ? perms[pI][pO] : NULL : NULL;
3222:         for (i = 0; i < numSelfDof; i++) { /* for every shape function */
3223:           PetscQuadrature q;
3224:           PetscInt        dim, thisNc, numPoints, j, k;
3225:           const PetscReal *points;
3226:           const PetscReal *weights;
3227:           PetscInt        *closure = NULL;
3228:           PetscInt        numClosure;
3229:           PetscInt        iCell = pperms ? pperms[i] : i;
3230:           PetscInt        parentCellShapeDof = cellShapeOff + iCell;
3231:           PetscTabulation Tparent;

3233:           PetscDualSpaceGetFunctional(dsp,parentCellShapeDof,&q);
3234:           PetscQuadratureGetData(q,&dim,&thisNc,&numPoints,&points,&weights);
3235:           if (thisNc != Nc) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Functional dim %D does not much basis dim %D\n",thisNc,Nc);
3236:           PetscFECreateTabulation(fe,1,numPoints,points,0,&Tparent); /* I'm expecting a nodal basis: weights[:]' * Bparent[:,cellShapeDof] = 1. */
3237:           for (j = 0; j < numPoints; j++) {
3238:             PetscInt          childCell = -1;
3239:             PetscReal         *parentValAtPoint;
3240:             const PetscReal   xi0[3] = {-1.,-1.,-1.};
3241:             const PetscReal   *pointReal = &points[dim * j];
3242:             const PetscScalar *point;
3243:             PetscTabulation Tchild;
3244:             PetscInt          childCellShapeOff, pointMatOff;
3245: #if defined(PETSC_USE_COMPLEX)
3246:             PetscInt          d;

3248:             for (d = 0; d < dim; d++) {
3249:               pointScalar[d] = points[dim * j + d];
3250:             }
3251:             point = pointScalar;
3252: #else
3253:             point = pointReal;
3254: #endif

3256:             parentValAtPoint = &Tparent->T[0][(fSize * j + parentCellShapeDof) * Nc];

3258:             for (k = 0; k < numChildren; k++) { /* locate the point in a child's star cell*/
3259:               PetscInt child = children[k];
3260:               PetscInt *star = NULL;
3261:               PetscInt numStar, s;

3263:               DMPlexGetTransitiveClosure(refTree,child,PETSC_FALSE,&numStar,&star);
3264:               for (s = numStar - 1; s >= 0; s--) {
3265:                 PetscInt c = star[2 * s];

3267:                 if (c < cStart || c >= cEnd) continue;
3268:                 DMPlexLocatePoint_Internal(refTree,dim,point,c,&childCell);
3269:                 if (childCell >= 0) break;
3270:               }
3271:               DMPlexRestoreTransitiveClosure(refTree,child,PETSC_FALSE,&numStar,&star);
3272:               if (childCell >= 0) break;
3273:             }
3274:             if (childCell < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Could not locate quadrature point");
3275:             DMPlexComputeCellGeometryFEM(refTree, childCell, NULL, v0, J, invJ, &detJ);
3276:             DMPlexComputeCellGeometryFEM(refTree, parentCell, NULL, v0parent, Jparent, NULL, &detJparent);
3277:             CoordinatesRefToReal(dim, dim, xi0, v0parent, Jparent, pointReal, vtmp);
3278:             CoordinatesRealToRef(dim, dim, xi0, v0, invJ, vtmp, pointRef);

3280:             PetscFECreateTabulation(fe,1,1,pointRef,0,&Tchild);
3281:             DMPlexGetTransitiveClosure(refTree,childCell,PETSC_TRUE,&numClosure,&closure);
3282:             for (k = 0, pointMatOff = 0; k < numChildren; k++) { /* point is located in cell => child dofs support at point are in closure of cell */
3283:               PetscInt child = children[k], childDepth, childDof, childO = PETSC_MIN_INT;
3284:               PetscInt l;
3285:               const PetscInt *cperms;

3287:               DMLabelGetValue(depth,child,&childDepth);
3288:               childDof = depthNumDof[childDepth];
3289:               for (l = 0, cI = -1, childCellShapeOff = 0; l < numClosure; l++) {
3290:                 PetscInt point = closure[2 * l];
3291:                 PetscInt pointDepth;

3293:                 childO = closure[2 * l + 1];
3294:                 if (point == child) {
3295:                   cI = l;
3296:                   break;
3297:                 }
3298:                 DMLabelGetValue(depth,point,&pointDepth);
3299:                 childCellShapeOff += depthNumDof[pointDepth];
3300:               }
3301:               if (l == numClosure) {
3302:                 pointMatOff += childDof;
3303:                 continue; /* child is not in the closure of the cell: has nothing to contribute to this point */
3304:               }
3305:               cperms = perms ? perms[cI] ? perms[cI][childO] : NULL : NULL;
3306:               for (l = 0; l < childDof; l++) {
3307:                 PetscInt    lCell = cperms ? cperms[l] : l;
3308:                 PetscInt    childCellDof = childCellShapeOff + lCell;
3309:                 PetscReal   *childValAtPoint;
3310:                 PetscReal   val = 0.;

3312:                 childValAtPoint = &Tchild->T[0][childCellDof * Nc];
3313:                 for (m = 0; m < Nc; m++) {
3314:                   val += weights[j * Nc + m] * parentValAtPoint[m] * childValAtPoint[m];
3315:                 }

3317:                 pointMat[i * numChildDof + pointMatOff + l] += val;
3318:               }
3319:               pointMatOff += childDof;
3320:             }
3321:             DMPlexRestoreTransitiveClosure(refTree,childCell,PETSC_TRUE,&numClosure,&closure);
3322:             PetscTabulationDestroy(&Tchild);
3323:           }
3324:           PetscTabulationDestroy(&Tparent);
3325:         }
3326:       }
3327:       else { /* just the volume-weighted averages of the children */
3328:         PetscReal parentVol;
3329:         PetscInt  childCell;

3331:         DMPlexComputeCellGeometryFVM(refTree, p, &parentVol, NULL, NULL);
3332:         for (i = 0, childCell = 0; i < numChildren; i++) {
3333:           PetscInt  child = children[i], j;
3334:           PetscReal childVol;

3336:           if (child < cStart || child >= cEnd) continue;
3337:           DMPlexComputeCellGeometryFVM(refTree, child, &childVol, NULL, NULL);
3338:           for (j = 0; j < Nc; j++) {
3339:             pointMat[j * numChildDof + Nc * childCell + j] = childVol / parentVol;
3340:           }
3341:           childCell++;
3342:         }
3343:       }
3344:       /* Insert pointMat into mat */
3345:       MatSetValues(mat,numSelfDof,matRows,numChildDof,matCols,pointMat,INSERT_VALUES);
3346:       DMRestoreWorkArray(refTree, numSelfDof + numChildDof, MPIU_INT,&matRows);
3347:       DMRestoreWorkArray(refTree, numSelfDof * numChildDof, MPIU_SCALAR,&pointMat);
3348:     }
3349:   }
3350:   PetscFree6(v0,v0parent,vtmp,J,Jparent,invJ);
3351:   PetscFree2(pointScalar,pointRef);
3352:   MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
3353:   MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
3354:   *inj = mat;
3355:   return(0);
3356: }

3358: static PetscErrorCode DMPlexReferenceTreeGetChildrenMatrices_Injection(DM refTree, Mat inj, PetscScalar ****childrenMats)
3359: {
3360:   PetscDS        ds;
3361:   PetscInt       numFields, f, pRefStart, pRefEnd, p, *rows, *cols, maxDof;
3362:   PetscScalar    ***refPointFieldMats;
3363:   PetscSection   refConSec, refSection;

3367:   DMGetDS(refTree,&ds);
3368:   PetscDSGetNumFields(ds,&numFields);
3369:   DMGetDefaultConstraints(refTree,&refConSec,NULL);
3370:   DMGetLocalSection(refTree,&refSection);
3371:   PetscSectionGetChart(refConSec,&pRefStart,&pRefEnd);
3372:   PetscMalloc1(pRefEnd-pRefStart,&refPointFieldMats);
3373:   PetscSectionGetMaxDof(refConSec,&maxDof);
3374:   PetscMalloc1(maxDof,&rows);
3375:   PetscMalloc1(maxDof*maxDof,&cols);
3376:   for (p = pRefStart; p < pRefEnd; p++) {
3377:     PetscInt parent, pDof, parentDof;

3379:     DMPlexGetTreeParent(refTree,p,&parent,NULL);
3380:     PetscSectionGetDof(refConSec,p,&pDof);
3381:     PetscSectionGetDof(refSection,parent,&parentDof);
3382:     if (!pDof || !parentDof || parent == p) continue;

3384:     PetscMalloc1(numFields,&refPointFieldMats[p-pRefStart]);
3385:     for (f = 0; f < numFields; f++) {
3386:       PetscInt cDof, cOff, numCols, r;

3388:       if (numFields > 1) {
3389:         PetscSectionGetFieldDof(refConSec,p,f,&cDof);
3390:         PetscSectionGetFieldOffset(refConSec,p,f,&cOff);
3391:       }
3392:       else {
3393:         PetscSectionGetDof(refConSec,p,&cDof);
3394:         PetscSectionGetOffset(refConSec,p,&cOff);
3395:       }

3397:       for (r = 0; r < cDof; r++) {
3398:         rows[r] = cOff + r;
3399:       }
3400:       numCols = 0;
3401:       {
3402:         PetscInt aDof, aOff, j;

3404:         if (numFields > 1) {
3405:           PetscSectionGetFieldDof(refSection,parent,f,&aDof);
3406:           PetscSectionGetFieldOffset(refSection,parent,f,&aOff);
3407:         }
3408:         else {
3409:           PetscSectionGetDof(refSection,parent,&aDof);
3410:           PetscSectionGetOffset(refSection,parent,&aOff);
3411:         }

3413:         for (j = 0; j < aDof; j++) {
3414:           cols[numCols++] = aOff + j;
3415:         }
3416:       }
3417:       PetscMalloc1(cDof*numCols,&refPointFieldMats[p-pRefStart][f]);
3418:       /* transpose of constraint matrix */
3419:       MatGetValues(inj,numCols,cols,cDof,rows,refPointFieldMats[p-pRefStart][f]);
3420:     }
3421:   }
3422:   *childrenMats = refPointFieldMats;
3423:   PetscFree(rows);
3424:   PetscFree(cols);
3425:   return(0);
3426: }

3428: static PetscErrorCode DMPlexReferenceTreeRestoreChildrenMatrices_Injection(DM refTree, Mat inj, PetscScalar ****childrenMats)
3429: {
3430:   PetscDS        ds;
3431:   PetscScalar    ***refPointFieldMats;
3432:   PetscInt       numFields, pRefStart, pRefEnd, p, f;
3433:   PetscSection   refConSec, refSection;

3437:   refPointFieldMats = *childrenMats;
3438:   *childrenMats = NULL;
3439:   DMGetDS(refTree,&ds);
3440:   DMGetLocalSection(refTree,&refSection);
3441:   PetscDSGetNumFields(ds,&numFields);
3442:   DMGetDefaultConstraints(refTree,&refConSec,NULL);
3443:   PetscSectionGetChart(refConSec,&pRefStart,&pRefEnd);
3444:   for (p = pRefStart; p < pRefEnd; p++) {
3445:     PetscInt parent, pDof, parentDof;

3447:     DMPlexGetTreeParent(refTree,p,&parent,NULL);
3448:     PetscSectionGetDof(refConSec,p,&pDof);
3449:     PetscSectionGetDof(refSection,parent,&parentDof);
3450:     if (!pDof || !parentDof || parent == p) continue;

3452:     for (f = 0; f < numFields; f++) {
3453:       PetscInt cDof;

3455:       if (numFields > 1) {
3456:         PetscSectionGetFieldDof(refConSec,p,f,&cDof);
3457:       }
3458:       else {
3459:         PetscSectionGetDof(refConSec,p,&cDof);
3460:       }

3462:       PetscFree(refPointFieldMats[p - pRefStart][f]);
3463:     }
3464:     PetscFree(refPointFieldMats[p - pRefStart]);
3465:   }
3466:   PetscFree(refPointFieldMats);
3467:   return(0);
3468: }

3470: static PetscErrorCode DMPlexReferenceTreeGetInjector(DM refTree,Mat *injRef)
3471: {
3472:   Mat            cMatRef;
3473:   PetscObject    injRefObj;

3477:   DMGetDefaultConstraints(refTree,NULL,&cMatRef);
3478:   PetscObjectQuery((PetscObject)cMatRef,"DMPlexComputeInjectorTree_refTree",&injRefObj);
3479:   *injRef = (Mat) injRefObj;
3480:   if (!*injRef) {
3481:     DMPlexComputeInjectorReferenceTree(refTree,injRef);
3482:     PetscObjectCompose((PetscObject)cMatRef,"DMPlexComputeInjectorTree_refTree",(PetscObject)*injRef);
3483:     /* there is now a reference in cMatRef, which should be the only one for symmetry with the above case */
3484:     PetscObjectDereference((PetscObject)*injRef);
3485:   }
3486:   return(0);
3487: }

3489: 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)
3490: {
3491:   PetscInt       pStartF, pEndF, pStartC, pEndC, p, maxDof, numMulti;
3492:   PetscSection   globalCoarse, globalFine;
3493:   PetscSection   localCoarse, localFine, leafIndicesSec;
3494:   PetscSection   multiRootSec, rootIndicesSec;
3495:   PetscInt       *leafInds, *rootInds = NULL;
3496:   const PetscInt *rootDegrees;
3497:   PetscScalar    *leafVals = NULL, *rootVals = NULL;
3498:   PetscSF        coarseToFineEmbedded;

3502:   DMPlexGetChart(coarse,&pStartC,&pEndC);
3503:   DMPlexGetChart(fine,&pStartF,&pEndF);
3504:   DMGetLocalSection(fine,&localFine);
3505:   DMGetGlobalSection(fine,&globalFine);
3506:   PetscSectionCreate(PetscObjectComm((PetscObject)fine),&leafIndicesSec);
3507:   PetscSectionSetChart(leafIndicesSec,pStartF, pEndF);
3508:   PetscSectionGetMaxDof(localFine,&maxDof);
3509:   { /* winnow fine points that don't have global dofs out of the sf */
3510:     PetscInt l, nleaves, dof, cdof, numPointsWithDofs, offset, *pointsWithDofs, numIndices;
3511:     const PetscInt *leaves;

3513:     PetscSFGetGraph(coarseToFine,NULL,&nleaves,&leaves,NULL);
3514:     for (l = 0, numPointsWithDofs = 0; l < nleaves; l++) {
3515:       p    = leaves ? leaves[l] : l;
3516:       PetscSectionGetDof(globalFine,p,&dof);
3517:       PetscSectionGetConstraintDof(globalFine,p,&cdof);
3518:       if ((dof - cdof) > 0) {
3519:         numPointsWithDofs++;

3521:         PetscSectionGetDof(localFine,p,&dof);
3522:         PetscSectionSetDof(leafIndicesSec,p,dof + 1);
3523:       }
3524:     }
3525:     PetscMalloc1(numPointsWithDofs,&pointsWithDofs);
3526:     PetscSectionSetUp(leafIndicesSec);
3527:     PetscSectionGetStorageSize(leafIndicesSec,&numIndices);
3528:     PetscMalloc1(gatheredIndices ? numIndices : (maxDof + 1),&leafInds);
3529:     if (gatheredValues)  {PetscMalloc1(numIndices,&leafVals);}
3530:     for (l = 0, offset = 0; l < nleaves; l++) {
3531:       p    = leaves ? leaves[l] : l;
3532:       PetscSectionGetDof(globalFine,p,&dof);
3533:       PetscSectionGetConstraintDof(globalFine,p,&cdof);
3534:       if ((dof - cdof) > 0) {
3535:         PetscInt    off, gOff;
3536:         PetscInt    *pInd;
3537:         PetscScalar *pVal = NULL;

3539:         pointsWithDofs[offset++] = l;

3541:         PetscSectionGetOffset(leafIndicesSec,p,&off);

3543:         pInd = gatheredIndices ? (&leafInds[off + 1]) : leafInds;
3544:         if (gatheredValues) {
3545:           PetscInt i;

3547:           pVal = &leafVals[off + 1];
3548:           for (i = 0; i < dof; i++) pVal[i] = 0.;
3549:         }
3550:         PetscSectionGetOffset(globalFine,p,&gOff);

3552:         offsets[0] = 0;
3553:         if (numFields) {
3554:           PetscInt f;

3556:           for (f = 0; f < numFields; f++) {
3557:             PetscInt fDof;
3558:             PetscSectionGetFieldDof(localFine,p,f,&fDof);
3559:             offsets[f + 1] = fDof + offsets[f];
3560:           }
3561:           DMPlexGetIndicesPointFields_Internal(localFine,PETSC_FALSE,p,gOff < 0 ? -(gOff + 1) : gOff,offsets,PETSC_FALSE,NULL,-1, NULL,pInd);
3562:         } else {
3563:           DMPlexGetIndicesPoint_Internal(localFine,PETSC_FALSE,p,gOff < 0 ? -(gOff + 1) : gOff,offsets,PETSC_FALSE,NULL, NULL,pInd);
3564:         }
3565:         if (gatheredValues) {VecGetValues(fineVec,dof,pInd,pVal);}
3566:       }
3567:     }
3568:     PetscSFCreateEmbeddedLeafSF(coarseToFine, numPointsWithDofs, pointsWithDofs, &coarseToFineEmbedded);
3569:     PetscFree(pointsWithDofs);
3570:   }

3572:   DMPlexGetChart(coarse,&pStartC,&pEndC);
3573:   DMGetLocalSection(coarse,&localCoarse);
3574:   DMGetGlobalSection(coarse,&globalCoarse);

3576:   { /* there may be the case where an sf root has a parent: broadcast parents back to children */
3577:     MPI_Datatype threeInt;
3578:     PetscMPIInt  rank;
3579:     PetscInt     (*parentNodeAndIdCoarse)[3];
3580:     PetscInt     (*parentNodeAndIdFine)[3];
3581:     PetscInt     p, nleaves, nleavesToParents;
3582:     PetscSF      pointSF, sfToParents;
3583:     const PetscInt *ilocal;
3584:     const PetscSFNode *iremote;
3585:     PetscSFNode  *iremoteToParents;
3586:     PetscInt     *ilocalToParents;

3588:     MPI_Comm_rank(PetscObjectComm((PetscObject)coarse),&rank);
3589:     MPI_Type_contiguous(3,MPIU_INT,&threeInt);
3590:     MPI_Type_commit(&threeInt);
3591:     PetscMalloc2(pEndC-pStartC,&parentNodeAndIdCoarse,pEndF-pStartF,&parentNodeAndIdFine);
3592:     DMGetPointSF(coarse,&pointSF);
3593:     PetscSFGetGraph(pointSF,NULL,&nleaves,&ilocal,&iremote);
3594:     for (p = pStartC; p < pEndC; p++) {
3595:       PetscInt parent, childId;
3596:       DMPlexGetTreeParent(coarse,p,&parent,&childId);
3597:       parentNodeAndIdCoarse[p - pStartC][0] = rank;
3598:       parentNodeAndIdCoarse[p - pStartC][1] = parent - pStartC;
3599:       parentNodeAndIdCoarse[p - pStartC][2] = (p == parent) ? -1 : childId;
3600:       if (nleaves > 0) {
3601:         PetscInt leaf = -1;

3603:         if (ilocal) {
3604:           PetscFindInt(parent,nleaves,ilocal,&leaf);
3605:         }
3606:         else {
3607:           leaf = p - pStartC;
3608:         }
3609:         if (leaf >= 0) {
3610:           parentNodeAndIdCoarse[p - pStartC][0] = iremote[leaf].rank;
3611:           parentNodeAndIdCoarse[p - pStartC][1] = iremote[leaf].index;
3612:         }
3613:       }
3614:     }
3615:     for (p = pStartF; p < pEndF; p++) {
3616:       parentNodeAndIdFine[p - pStartF][0] = -1;
3617:       parentNodeAndIdFine[p - pStartF][1] = -1;
3618:       parentNodeAndIdFine[p - pStartF][2] = -1;
3619:     }
3620:     PetscSFBcastBegin(coarseToFineEmbedded,threeInt,parentNodeAndIdCoarse,parentNodeAndIdFine);
3621:     PetscSFBcastEnd(coarseToFineEmbedded,threeInt,parentNodeAndIdCoarse,parentNodeAndIdFine);
3622:     for (p = pStartF, nleavesToParents = 0; p < pEndF; p++) {
3623:       PetscInt dof;

3625:       PetscSectionGetDof(leafIndicesSec,p,&dof);
3626:       if (dof) {
3627:         PetscInt off;

3629:         PetscSectionGetOffset(leafIndicesSec,p,&off);
3630:         if (gatheredIndices) {
3631:           leafInds[off] = PetscMax(childIds[p-pStartF],parentNodeAndIdFine[p-pStartF][2]);
3632:         } else if (gatheredValues) {
3633:           leafVals[off] = (PetscScalar) PetscMax(childIds[p-pStartF],parentNodeAndIdFine[p-pStartF][2]);
3634:         }
3635:       }
3636:       if (parentNodeAndIdFine[p-pStartF][0] >= 0) {
3637:         nleavesToParents++;
3638:       }
3639:     }
3640:     PetscMalloc1(nleavesToParents,&ilocalToParents);
3641:     PetscMalloc1(nleavesToParents,&iremoteToParents);
3642:     for (p = pStartF, nleavesToParents = 0; p < pEndF; p++) {
3643:       if (parentNodeAndIdFine[p-pStartF][0] >= 0) {
3644:         ilocalToParents[nleavesToParents] = p - pStartF;
3645:         iremoteToParents[nleavesToParents].rank  = parentNodeAndIdFine[p-pStartF][0];
3646:         iremoteToParents[nleavesToParents].index = parentNodeAndIdFine[p-pStartF][1];
3647:         nleavesToParents++;
3648:       }
3649:     }
3650:     PetscSFCreate(PetscObjectComm((PetscObject)coarse),&sfToParents);
3651:     PetscSFSetGraph(sfToParents,pEndC-pStartC,nleavesToParents,ilocalToParents,PETSC_OWN_POINTER,iremoteToParents,PETSC_OWN_POINTER);
3652:     PetscSFDestroy(&coarseToFineEmbedded);

3654:     coarseToFineEmbedded = sfToParents;

3656:     PetscFree2(parentNodeAndIdCoarse,parentNodeAndIdFine);
3657:     MPI_Type_free(&threeInt);
3658:   }

3660:   { /* winnow out coarse points that don't have dofs */
3661:     PetscInt dof, cdof, numPointsWithDofs, offset, *pointsWithDofs;
3662:     PetscSF  sfDofsOnly;

3664:     for (p = pStartC, numPointsWithDofs = 0; p < pEndC; p++) {
3665:       PetscSectionGetDof(globalCoarse,p,&dof);
3666:       PetscSectionGetConstraintDof(globalCoarse,p,&cdof);
3667:       if ((dof - cdof) > 0) {
3668:         numPointsWithDofs++;
3669:       }
3670:     }
3671:     PetscMalloc1(numPointsWithDofs,&pointsWithDofs);
3672:     for (p = pStartC, offset = 0; p < pEndC; p++) {
3673:       PetscSectionGetDof(globalCoarse,p,&dof);
3674:       PetscSectionGetConstraintDof(globalCoarse,p,&cdof);
3675:       if ((dof - cdof) > 0) {
3676:         pointsWithDofs[offset++] = p - pStartC;
3677:       }
3678:     }
3679:     PetscSFCreateEmbeddedSF(coarseToFineEmbedded, numPointsWithDofs, pointsWithDofs, &sfDofsOnly);
3680:     PetscSFDestroy(&coarseToFineEmbedded);
3681:     PetscFree(pointsWithDofs);
3682:     coarseToFineEmbedded = sfDofsOnly;
3683:   }

3685:   /* communicate back to the coarse mesh which coarse points have children (that may require injection) */
3686:   PetscSFComputeDegreeBegin(coarseToFineEmbedded,&rootDegrees);
3687:   PetscSFComputeDegreeEnd(coarseToFineEmbedded,&rootDegrees);
3688:   PetscSectionCreate(PetscObjectComm((PetscObject)coarse),&multiRootSec);
3689:   PetscSectionSetChart(multiRootSec,pStartC,pEndC);
3690:   for (p = pStartC; p < pEndC; p++) {
3691:     PetscSectionSetDof(multiRootSec,p,rootDegrees[p-pStartC]);
3692:   }
3693:   PetscSectionSetUp(multiRootSec);
3694:   PetscSectionGetStorageSize(multiRootSec,&numMulti);
3695:   PetscSectionCreate(PetscObjectComm((PetscObject)coarse),&rootIndicesSec);
3696:   { /* distribute the leaf section */
3697:     PetscSF multi, multiInv, indicesSF;
3698:     PetscInt *remoteOffsets, numRootIndices;

3700:     PetscSFGetMultiSF(coarseToFineEmbedded,&multi);
3701:     PetscSFCreateInverseSF(multi,&multiInv);
3702:     PetscSFDistributeSection(multiInv,leafIndicesSec,&remoteOffsets,rootIndicesSec);
3703:     PetscSFCreateSectionSF(multiInv,leafIndicesSec,remoteOffsets,rootIndicesSec,&indicesSF);
3704:     PetscFree(remoteOffsets);
3705:     PetscSFDestroy(&multiInv);
3706:     PetscSectionGetStorageSize(rootIndicesSec,&numRootIndices);
3707:     if (gatheredIndices) {
3708:       PetscMalloc1(numRootIndices,&rootInds);
3709:       PetscSFBcastBegin(indicesSF,MPIU_INT,leafInds,rootInds);
3710:       PetscSFBcastEnd(indicesSF,MPIU_INT,leafInds,rootInds);
3711:     }
3712:     if (gatheredValues) {
3713:       PetscMalloc1(numRootIndices,&rootVals);
3714:       PetscSFBcastBegin(indicesSF,MPIU_SCALAR,leafVals,rootVals);
3715:       PetscSFBcastEnd(indicesSF,MPIU_SCALAR,leafVals,rootVals);
3716:     }
3717:     PetscSFDestroy(&indicesSF);
3718:   }
3719:   PetscSectionDestroy(&leafIndicesSec);
3720:   PetscFree(leafInds);
3721:   PetscFree(leafVals);
3722:   PetscSFDestroy(&coarseToFineEmbedded);
3723:   *rootMultiSec = multiRootSec;
3724:   *multiLeafSec = rootIndicesSec;
3725:   if (gatheredIndices) *gatheredIndices = rootInds;
3726:   if (gatheredValues)  *gatheredValues  = rootVals;
3727:   return(0);
3728: }

3730: PetscErrorCode DMPlexComputeInjectorTree(DM coarse, DM fine, PetscSF coarseToFine, PetscInt *childIds, Mat mat)
3731: {
3732:   DM             refTree;
3733:   PetscSection   multiRootSec, rootIndicesSec;
3734:   PetscSection   globalCoarse, globalFine;
3735:   PetscSection   localCoarse, localFine;
3736:   PetscSection   cSecRef;
3737:   PetscInt       *rootIndices = NULL, *parentIndices, pRefStart, pRefEnd;
3738:   Mat            injRef;
3739:   PetscInt       numFields, maxDof;
3740:   PetscInt       pStartC, pEndC, pStartF, pEndF, p;
3741:   PetscInt       *offsets, *offsetsCopy, *rowOffsets;
3742:   PetscLayout    rowMap, colMap;
3743:   PetscInt       rowStart, rowEnd, colStart, colEnd, *nnzD, *nnzO;
3744:   PetscScalar    ***childrenMats=NULL ; /* gcc -O gives 'may be used uninitialized' warning'. Initializing to suppress this warning */


3749:   /* get the templates for the fine-to-coarse injection from the reference tree */
3750:   DMPlexGetReferenceTree(coarse,&refTree);
3751:   DMGetDefaultConstraints(refTree,&cSecRef,NULL);
3752:   PetscSectionGetChart(cSecRef,&pRefStart,&pRefEnd);
3753:   DMPlexReferenceTreeGetInjector(refTree,&injRef);

3755:   DMPlexGetChart(fine,&pStartF,&pEndF);
3756:   DMGetLocalSection(fine,&localFine);
3757:   DMGetGlobalSection(fine,&globalFine);
3758:   PetscSectionGetNumFields(localFine,&numFields);
3759:   DMPlexGetChart(coarse,&pStartC,&pEndC);
3760:   DMGetLocalSection(coarse,&localCoarse);
3761:   DMGetGlobalSection(coarse,&globalCoarse);
3762:   PetscSectionGetMaxDof(localCoarse,&maxDof);
3763:   {
3764:     PetscInt maxFields = PetscMax(1,numFields) + 1;
3765:     PetscMalloc3(maxFields,&offsets,maxFields,&offsetsCopy,maxFields,&rowOffsets);
3766:   }

3768:   DMPlexTransferInjectorTree(coarse,fine,coarseToFine,childIds,NULL,numFields,offsets,&multiRootSec,&rootIndicesSec,&rootIndices,NULL);

3770:   PetscMalloc1(maxDof,&parentIndices);

3772:   /* count indices */
3773:   MatGetLayouts(mat,&rowMap,&colMap);
3774:   PetscLayoutSetUp(rowMap);
3775:   PetscLayoutSetUp(colMap);
3776:   PetscLayoutGetRange(rowMap,&rowStart,&rowEnd);
3777:   PetscLayoutGetRange(colMap,&colStart,&colEnd);
3778:   PetscCalloc2(rowEnd-rowStart,&nnzD,rowEnd-rowStart,&nnzO);
3779:   for (p = pStartC; p < pEndC; p++) {
3780:     PetscInt numLeaves, leafStart, leafEnd, l, dof, cdof, gOff;

3782:     PetscSectionGetDof(globalCoarse,p,&dof);
3783:     PetscSectionGetConstraintDof(globalCoarse,p,&cdof);
3784:     if ((dof - cdof) <= 0) continue;
3785:     PetscSectionGetOffset(globalCoarse,p,&gOff);

3787:     rowOffsets[0] = 0;
3788:     offsetsCopy[0] = 0;
3789:     if (numFields) {
3790:       PetscInt f;

3792:       for (f = 0; f < numFields; f++) {
3793:         PetscInt fDof;
3794:         PetscSectionGetFieldDof(localCoarse,p,f,&fDof);
3795:         rowOffsets[f + 1] = offsetsCopy[f + 1] = fDof + rowOffsets[f];
3796:       }
3797:       DMPlexGetIndicesPointFields_Internal(localCoarse,PETSC_FALSE,p,gOff < 0 ? -(gOff + 1) : gOff,offsetsCopy,PETSC_FALSE,NULL,-1, NULL,parentIndices);
3798:     } else {
3799:       DMPlexGetIndicesPoint_Internal(localCoarse,PETSC_FALSE,p,gOff < 0 ? -(gOff + 1) : gOff,offsetsCopy,PETSC_FALSE,NULL, NULL,parentIndices);
3800:       rowOffsets[1] = offsetsCopy[0];
3801:     }

3803:     PetscSectionGetDof(multiRootSec,p,&numLeaves);
3804:     PetscSectionGetOffset(multiRootSec,p,&leafStart);
3805:     leafEnd = leafStart + numLeaves;
3806:     for (l = leafStart; l < leafEnd; l++) {
3807:       PetscInt numIndices, childId, offset;
3808:       const PetscInt *childIndices;

3810:       PetscSectionGetDof(rootIndicesSec,l,&numIndices);
3811:       PetscSectionGetOffset(rootIndicesSec,l,&offset);
3812:       childId = rootIndices[offset++];
3813:       childIndices = &rootIndices[offset];
3814:       numIndices--;

3816:       if (childId == -1) { /* equivalent points: scatter */
3817:         PetscInt i;

3819:         for (i = 0; i < numIndices; i++) {
3820:           PetscInt colIndex = childIndices[i];
3821:           PetscInt rowIndex = parentIndices[i];
3822:           if (rowIndex < 0) continue;
3823:           if (colIndex < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unconstrained fine and constrained coarse");
3824:           if (colIndex >= colStart && colIndex < colEnd) {
3825:             nnzD[rowIndex - rowStart] = 1;
3826:           }
3827:           else {
3828:             nnzO[rowIndex - rowStart] = 1;
3829:           }
3830:         }
3831:       }
3832:       else {
3833:         PetscInt parentId, f, lim;

3835:         DMPlexGetTreeParent(refTree,childId,&parentId,NULL);

3837:         lim = PetscMax(1,numFields);
3838:         offsets[0] = 0;
3839:         if (numFields) {
3840:           PetscInt f;

3842:           for (f = 0; f < numFields; f++) {
3843:             PetscInt fDof;
3844:             PetscSectionGetFieldDof(cSecRef,childId,f,&fDof);

3846:             offsets[f + 1] = fDof + offsets[f];
3847:           }
3848:         }
3849:         else {
3850:           PetscInt cDof;

3852:           PetscSectionGetDof(cSecRef,childId,&cDof);
3853:           offsets[1] = cDof;
3854:         }
3855:         for (f = 0; f < lim; f++) {
3856:           PetscInt parentStart = rowOffsets[f], parentEnd = rowOffsets[f + 1];
3857:           PetscInt childStart = offsets[f], childEnd = offsets[f + 1];
3858:           PetscInt i, numD = 0, numO = 0;

3860:           for (i = childStart; i < childEnd; i++) {
3861:             PetscInt colIndex = childIndices[i];

3863:             if (colIndex < 0) continue;
3864:             if (colIndex >= colStart && colIndex < colEnd) {
3865:               numD++;
3866:             }
3867:             else {
3868:               numO++;
3869:             }
3870:           }
3871:           for (i = parentStart; i < parentEnd; i++) {
3872:             PetscInt rowIndex = parentIndices[i];

3874:             if (rowIndex < 0) continue;
3875:             nnzD[rowIndex - rowStart] += numD;
3876:             nnzO[rowIndex - rowStart] += numO;
3877:           }
3878:         }
3879:       }
3880:     }
3881:   }
3882:   /* preallocate */
3883:   MatXAIJSetPreallocation(mat,1,nnzD,nnzO,NULL,NULL);
3884:   PetscFree2(nnzD,nnzO);
3885:   /* insert values */
3886:   DMPlexReferenceTreeGetChildrenMatrices_Injection(refTree,injRef,&childrenMats);
3887:   for (p = pStartC; p < pEndC; p++) {
3888:     PetscInt numLeaves, leafStart, leafEnd, l, dof, cdof, gOff;

3890:     PetscSectionGetDof(globalCoarse,p,&dof);
3891:     PetscSectionGetConstraintDof(globalCoarse,p,&cdof);
3892:     if ((dof - cdof) <= 0) continue;
3893:     PetscSectionGetOffset(globalCoarse,p,&gOff);

3895:     rowOffsets[0] = 0;
3896:     offsetsCopy[0] = 0;
3897:     if (numFields) {
3898:       PetscInt f;

3900:       for (f = 0; f < numFields; f++) {
3901:         PetscInt fDof;
3902:         PetscSectionGetFieldDof(localCoarse,p,f,&fDof);
3903:         rowOffsets[f + 1] = offsetsCopy[f + 1] = fDof + rowOffsets[f];
3904:       }
3905:       DMPlexGetIndicesPointFields_Internal(localCoarse,PETSC_FALSE,p,gOff < 0 ? -(gOff + 1) : gOff,offsetsCopy,PETSC_FALSE,NULL,-1, NULL,parentIndices);
3906:     } else {
3907:       DMPlexGetIndicesPoint_Internal(localCoarse,PETSC_FALSE,p,gOff < 0 ? -(gOff + 1) : gOff,offsetsCopy,PETSC_FALSE,NULL, NULL,parentIndices);
3908:       rowOffsets[1] = offsetsCopy[0];
3909:     }

3911:     PetscSectionGetDof(multiRootSec,p,&numLeaves);
3912:     PetscSectionGetOffset(multiRootSec,p,&leafStart);
3913:     leafEnd = leafStart + numLeaves;
3914:     for (l = leafStart; l < leafEnd; l++) {
3915:       PetscInt numIndices, childId, offset;
3916:       const PetscInt *childIndices;

3918:       PetscSectionGetDof(rootIndicesSec,l,&numIndices);
3919:       PetscSectionGetOffset(rootIndicesSec,l,&offset);
3920:       childId = rootIndices[offset++];
3921:       childIndices = &rootIndices[offset];
3922:       numIndices--;

3924:       if (childId == -1) { /* equivalent points: scatter */
3925:         PetscInt i;

3927:         for (i = 0; i < numIndices; i++) {
3928:           MatSetValue(mat,parentIndices[i],childIndices[i],1.,INSERT_VALUES);
3929:         }
3930:       }
3931:       else {
3932:         PetscInt parentId, f, lim;

3934:         DMPlexGetTreeParent(refTree,childId,&parentId,NULL);

3936:         lim = PetscMax(1,numFields);
3937:         offsets[0] = 0;
3938:         if (numFields) {
3939:           PetscInt f;

3941:           for (f = 0; f < numFields; f++) {
3942:             PetscInt fDof;
3943:             PetscSectionGetFieldDof(cSecRef,childId,f,&fDof);

3945:             offsets[f + 1] = fDof + offsets[f];
3946:           }
3947:         }
3948:         else {
3949:           PetscInt cDof;

3951:           PetscSectionGetDof(cSecRef,childId,&cDof);
3952:           offsets[1] = cDof;
3953:         }
3954:         for (f = 0; f < lim; f++) {
3955:           PetscScalar    *childMat   = &childrenMats[childId - pRefStart][f][0];
3956:           PetscInt       *rowIndices = &parentIndices[rowOffsets[f]];
3957:           const PetscInt *colIndices = &childIndices[offsets[f]];

3959:           MatSetValues(mat,rowOffsets[f+1]-rowOffsets[f],rowIndices,offsets[f+1]-offsets[f],colIndices,childMat,INSERT_VALUES);
3960:         }
3961:       }
3962:     }
3963:   }
3964:   PetscSectionDestroy(&multiRootSec);
3965:   PetscSectionDestroy(&rootIndicesSec);
3966:   PetscFree(parentIndices);
3967:   DMPlexReferenceTreeRestoreChildrenMatrices_Injection(refTree,injRef,&childrenMats);
3968:   PetscFree(rootIndices);
3969:   PetscFree3(offsets,offsetsCopy,rowOffsets);

3971:   MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
3972:   MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
3973:   return(0);
3974: }

3976: static PetscErrorCode DMPlexTransferVecTree_Interpolate(DM coarse, Vec vecCoarseLocal, DM fine, Vec vecFine, PetscSF coarseToFine, PetscInt *cids, Vec grad, Vec cellGeom)
3977: {
3978:   PetscSF           coarseToFineEmbedded;
3979:   PetscSection      globalCoarse, globalFine;
3980:   PetscSection      localCoarse, localFine;
3981:   PetscSection      aSec, cSec;
3982:   PetscSection      rootValuesSec;
3983:   PetscSection      leafValuesSec;
3984:   PetscScalar       *rootValues, *leafValues;
3985:   IS                aIS;
3986:   const PetscInt    *anchors;
3987:   Mat               cMat;
3988:   PetscInt          numFields;
3989:   PetscInt          pStartC, pEndC, pStartF, pEndF, p, cellStart, cellEnd;
3990:   PetscInt          aStart, aEnd, cStart, cEnd;
3991:   PetscInt          *maxChildIds;
3992:   PetscInt          *offsets, *newOffsets, *offsetsCopy, *newOffsetsCopy, *rowOffsets, *numD, *numO;
3993:   PetscFV           fv = NULL;
3994:   PetscInt          dim, numFVcomps = -1, fvField = -1;
3995:   DM                cellDM = NULL, gradDM = NULL;
3996:   const PetscScalar *cellGeomArray = NULL;
3997:   const PetscScalar *gradArray = NULL;
3998:   PetscErrorCode    ierr;

4001:   VecSetOption(vecFine,VEC_IGNORE_NEGATIVE_INDICES,PETSC_TRUE);
4002:   DMPlexGetChart(coarse,&pStartC,&pEndC);
4003:   DMPlexGetSimplexOrBoxCells(coarse,0,&cellStart,&cellEnd);
4004:   DMPlexGetChart(fine,&pStartF,&pEndF);
4005:   DMGetGlobalSection(fine,&globalFine);
4006:   DMGetCoordinateDim(coarse,&dim);
4007:   { /* winnow fine points that don't have global dofs out of the sf */
4008:     PetscInt       nleaves, l;
4009:     const PetscInt *leaves;
4010:     PetscInt       dof, cdof, numPointsWithDofs, offset, *pointsWithDofs;

4012:     PetscSFGetGraph(coarseToFine,NULL,&nleaves,&leaves,NULL);

4014:     for (l = 0, numPointsWithDofs = 0; l < nleaves; l++) {
4015:       PetscInt p = leaves ? leaves[l] : l;

4017:       PetscSectionGetDof(globalFine,p,&dof);
4018:       PetscSectionGetConstraintDof(globalFine,p,&cdof);
4019:       if ((dof - cdof) > 0) {
4020:         numPointsWithDofs++;
4021:       }
4022:     }
4023:     PetscMalloc1(numPointsWithDofs,&pointsWithDofs);
4024:     for (l = 0, offset = 0; l < nleaves; l++) {
4025:       PetscInt p = leaves ? leaves[l] : l;

4027:       PetscSectionGetDof(globalFine,p,&dof);
4028:       PetscSectionGetConstraintDof(globalFine,p,&cdof);
4029:       if ((dof - cdof) > 0) {
4030:         pointsWithDofs[offset++] = l;
4031:       }
4032:     }
4033:     PetscSFCreateEmbeddedLeafSF(coarseToFine, numPointsWithDofs, pointsWithDofs, &coarseToFineEmbedded);
4034:     PetscFree(pointsWithDofs);
4035:   }
4036:   /* communicate back to the coarse mesh which coarse points have children (that may require interpolation) */
4037:   PetscMalloc1(pEndC-pStartC,&maxChildIds);
4038:   for (p = pStartC; p < pEndC; p++) {
4039:     maxChildIds[p - pStartC] = -2;
4040:   }
4041:   PetscSFReduceBegin(coarseToFineEmbedded,MPIU_INT,cids,maxChildIds,MPIU_MAX);
4042:   PetscSFReduceEnd(coarseToFineEmbedded,MPIU_INT,cids,maxChildIds,MPIU_MAX);

4044:   DMGetLocalSection(coarse,&localCoarse);
4045:   DMGetGlobalSection(coarse,&globalCoarse);

4047:   DMPlexGetAnchors(coarse,&aSec,&aIS);
4048:   ISGetIndices(aIS,&anchors);
4049:   PetscSectionGetChart(aSec,&aStart,&aEnd);

4051:   DMGetDefaultConstraints(coarse,&cSec,&cMat);
4052:   PetscSectionGetChart(cSec,&cStart,&cEnd);

4054:   /* create sections that will send to children the indices and matrices they will need to construct the interpolator */
4055:   PetscSectionCreate(PetscObjectComm((PetscObject)coarse),&rootValuesSec);
4056:   PetscSectionSetChart(rootValuesSec,pStartC,pEndC);
4057:   PetscSectionGetNumFields(localCoarse,&numFields);
4058:   {
4059:     PetscInt maxFields = PetscMax(1,numFields) + 1;
4060:     PetscMalloc7(maxFields,&offsets,maxFields,&offsetsCopy,maxFields,&newOffsets,maxFields,&newOffsetsCopy,maxFields,&rowOffsets,maxFields,&numD,maxFields,&numO);
4061:   }
4062:   if (grad) {
4063:     PetscInt i;

4065:     VecGetDM(cellGeom,&cellDM);
4066:     VecGetArrayRead(cellGeom,&cellGeomArray);
4067:     VecGetDM(grad,&gradDM);
4068:     VecGetArrayRead(grad,&gradArray);
4069:     for (i = 0; i < PetscMax(1,numFields); i++) {
4070:       PetscObject  obj;
4071:       PetscClassId id;

4073:       DMGetField(coarse, i, NULL, &obj);
4074:       PetscObjectGetClassId(obj,&id);
4075:       if (id == PETSCFV_CLASSID) {
4076:         fv      = (PetscFV) obj;
4077:         PetscFVGetNumComponents(fv,&numFVcomps);
4078:         fvField = i;
4079:         break;
4080:       }
4081:     }
4082:   }

4084:   for (p = pStartC; p < pEndC; p++) { /* count the sizes of the indices and matrices */
4085:     PetscInt dof;
4086:     PetscInt maxChildId     = maxChildIds[p - pStartC];
4087:     PetscInt numValues      = 0;

4089:     PetscSectionGetDof(globalCoarse,p,&dof);
4090:     if (dof < 0) {
4091:       dof = -(dof + 1);
4092:     }
4093:     offsets[0]    = 0;
4094:     newOffsets[0] = 0;
4095:     if (maxChildId >= 0) { /* this point has children (with dofs) that will need to be interpolated from the closure of p */
4096:       PetscInt *closure = NULL, closureSize, cl;

4098:       DMPlexGetTransitiveClosure(coarse,p,PETSC_TRUE,&closureSize,&closure);
4099:       for (cl = 0; cl < closureSize; cl++) { /* get the closure */
4100:         PetscInt c = closure[2 * cl], clDof;

4102:         PetscSectionGetDof(localCoarse,c,&clDof);
4103:         numValues += clDof;
4104:       }
4105:       DMPlexRestoreTransitiveClosure(coarse,p,PETSC_TRUE,&closureSize,&closure);
4106:     }
4107:     else if (maxChildId == -1) {
4108:       PetscSectionGetDof(localCoarse,p,&numValues);
4109:     }
4110:     /* we will pack the column indices with the field offsets */
4111:     if (maxChildId >= 0 && grad && p >= cellStart && p < cellEnd) {
4112:       /* also send the centroid, and the gradient */
4113:       numValues += dim * (1 + numFVcomps);
4114:     }
4115:     PetscSectionSetDof(rootValuesSec,p,numValues);
4116:   }
4117:   PetscSectionSetUp(rootValuesSec);
4118:   {
4119:     PetscInt          numRootValues;
4120:     const PetscScalar *coarseArray;

4122:     PetscSectionGetStorageSize(rootValuesSec,&numRootValues);
4123:     PetscMalloc1(numRootValues,&rootValues);
4124:     VecGetArrayRead(vecCoarseLocal,&coarseArray);
4125:     for (p = pStartC; p < pEndC; p++) {
4126:       PetscInt    numValues;
4127:       PetscInt    pValOff;
4128:       PetscScalar *pVal;
4129:       PetscInt    maxChildId = maxChildIds[p - pStartC];

4131:       PetscSectionGetDof(rootValuesSec,p,&numValues);
4132:       if (!numValues) {
4133:         continue;
4134:       }
4135:       PetscSectionGetOffset(rootValuesSec,p,&pValOff);
4136:       pVal = &(rootValues[pValOff]);
4137:       if (maxChildId >= 0) { /* build an identity matrix, apply matrix constraints on the right */
4138:         PetscInt closureSize = numValues;
4139:         DMPlexVecGetClosure(coarse,NULL,vecCoarseLocal,p,&closureSize,&pVal);
4140:         if (grad && p >= cellStart && p < cellEnd) {
4141:           PetscFVCellGeom *cg;
4142:           PetscScalar     *gradVals = NULL;
4143:           PetscInt        i;

4145:           pVal += (numValues - dim * (1 + numFVcomps));

4147:           DMPlexPointLocalRead(cellDM,p,cellGeomArray,(void *) &cg);
4148:           for (i = 0; i < dim; i++) pVal[i] = cg->centroid[i];
4149:           pVal += dim;
4150:           DMPlexPointGlobalRead(gradDM,p,gradArray,(void *) &gradVals);
4151:           for (i = 0; i < dim * numFVcomps; i++) pVal[i] = gradVals[i];
4152:         }
4153:       }
4154:       else if (maxChildId == -1) {
4155:         PetscInt lDof, lOff, i;

4157:         PetscSectionGetDof(localCoarse,p,&lDof);
4158:         PetscSectionGetOffset(localCoarse,p,&lOff);
4159:         for (i = 0; i < lDof; i++) pVal[i] = coarseArray[lOff + i];
4160:       }
4161:     }
4162:     VecRestoreArrayRead(vecCoarseLocal,&coarseArray);
4163:     PetscFree(maxChildIds);
4164:   }
4165:   {
4166:     PetscSF  valuesSF;
4167:     PetscInt *remoteOffsetsValues, numLeafValues;

4169:     PetscSectionCreate(PetscObjectComm((PetscObject)fine),&leafValuesSec);
4170:     PetscSFDistributeSection(coarseToFineEmbedded,rootValuesSec,&remoteOffsetsValues,leafValuesSec);
4171:     PetscSFCreateSectionSF(coarseToFineEmbedded,rootValuesSec,remoteOffsetsValues,leafValuesSec,&valuesSF);
4172:     PetscSFDestroy(&coarseToFineEmbedded);
4173:     PetscFree(remoteOffsetsValues);
4174:     PetscSectionGetStorageSize(leafValuesSec,&numLeafValues);
4175:     PetscMalloc1(numLeafValues,&leafValues);
4176:     PetscSFBcastBegin(valuesSF,MPIU_SCALAR,rootValues,leafValues);
4177:     PetscSFBcastEnd(valuesSF,MPIU_SCALAR,rootValues,leafValues);
4178:     PetscSFDestroy(&valuesSF);
4179:     PetscFree(rootValues);
4180:     PetscSectionDestroy(&rootValuesSec);
4181:   }
4182:   DMGetLocalSection(fine,&localFine);
4183:   {
4184:     PetscInt    maxDof;
4185:     PetscInt    *rowIndices;
4186:     DM           refTree;
4187:     PetscInt     **refPointFieldN;
4188:     PetscScalar  ***refPointFieldMats;
4189:     PetscSection refConSec, refAnSec;
4190:     PetscInt     pRefStart,pRefEnd,leafStart,leafEnd;
4191:     PetscScalar  *pointWork;

4193:     PetscSectionGetMaxDof(localFine,&maxDof);
4194:     DMGetWorkArray(fine,maxDof,MPIU_INT,&rowIndices);
4195:     DMGetWorkArray(fine,maxDof,MPIU_SCALAR,&pointWork);
4196:     DMPlexGetReferenceTree(fine,&refTree);
4197:     DMCopyDisc(fine,refTree);
4198:     DMPlexReferenceTreeGetChildrenMatrices(refTree,&refPointFieldMats,&refPointFieldN);
4199:     DMGetDefaultConstraints(refTree,&refConSec,NULL);
4200:     DMPlexGetAnchors(refTree,&refAnSec,NULL);
4201:     PetscSectionGetChart(refConSec,&pRefStart,&pRefEnd);
4202:     PetscSectionGetChart(leafValuesSec,&leafStart,&leafEnd);
4203:     DMPlexGetSimplexOrBoxCells(fine,0,&cellStart,&cellEnd);
4204:     for (p = leafStart; p < leafEnd; p++) {
4205:       PetscInt          gDof, gcDof, gOff, lDof;
4206:       PetscInt          numValues, pValOff;
4207:       PetscInt          childId;
4208:       const PetscScalar *pVal;
4209:       const PetscScalar *fvGradData = NULL;

4211:       PetscSectionGetDof(globalFine,p,&gDof);
4212:       PetscSectionGetDof(localFine,p,&lDof);
4213:       PetscSectionGetConstraintDof(globalFine,p,&gcDof);
4214:       if ((gDof - gcDof) <= 0) {
4215:         continue;
4216:       }
4217:       PetscSectionGetOffset(globalFine,p,&gOff);
4218:       PetscSectionGetDof(leafValuesSec,p,&numValues);
4219:       if (!numValues) continue;
4220:       PetscSectionGetOffset(leafValuesSec,p,&pValOff);
4221:       pVal = &leafValues[pValOff];
4222:       offsets[0]        = 0;
4223:       offsetsCopy[0]    = 0;
4224:       newOffsets[0]     = 0;
4225:       newOffsetsCopy[0] = 0;
4226:       childId           = cids[p - pStartF];
4227:       if (numFields) {
4228:         PetscInt f;
4229:         for (f = 0; f < numFields; f++) {
4230:           PetscInt rowDof;

4232:           PetscSectionGetFieldDof(localFine,p,f,&rowDof);
4233:           offsets[f + 1]        = offsets[f] + rowDof;
4234:           offsetsCopy[f + 1]    = offsets[f + 1];
4235:           /* TODO: closure indices */
4236:           newOffsets[f + 1]     = newOffsets[f] + ((childId == -1) ? rowDof : refPointFieldN[childId - pRefStart][f]);
4237:         }
4238:         DMPlexGetIndicesPointFields_Internal(localFine,PETSC_FALSE,p,gOff,offsetsCopy,PETSC_FALSE,NULL,-1,NULL,rowIndices);
4239:       }
4240:       else {
4241:         offsets[0]    = 0;
4242:         offsets[1]    = lDof;
4243:         newOffsets[0] = 0;
4244:         newOffsets[1] = (childId == -1) ? lDof : refPointFieldN[childId - pRefStart][0];
4245:         DMPlexGetIndicesPoint_Internal(localFine,PETSC_FALSE,p,gOff,offsetsCopy,PETSC_FALSE,NULL,NULL,rowIndices);
4246:       }
4247:       if (childId == -1) { /* no child interpolation: one nnz per */
4248:         VecSetValues(vecFine,numValues,rowIndices,pVal,INSERT_VALUES);
4249:       } else {
4250:         PetscInt f;

4252:         if (grad && p >= cellStart && p < cellEnd) {
4253:           numValues -= (dim * (1 + numFVcomps));
4254:           fvGradData = &pVal[numValues];
4255:         }
4256:         for (f = 0; f < PetscMax(1,numFields); f++) {
4257:           const PetscScalar *childMat = refPointFieldMats[childId - pRefStart][f];
4258:           PetscInt numRows = offsets[f+1] - offsets[f];
4259:           PetscInt numCols = newOffsets[f + 1] - newOffsets[f];
4260:           const PetscScalar *cVal = &pVal[newOffsets[f]];
4261:           PetscScalar *rVal = &pointWork[offsets[f]];
4262:           PetscInt i, j;

4264: #if 0
4265:           PetscInfo5(coarse,"childId %D, numRows %D, numCols %D, refPointFieldN %D maxDof %D\n",childId,numRows,numCols,refPointFieldN[childId - pRefStart][f], maxDof);
4266: #endif
4267:           for (i = 0; i < numRows; i++) {
4268:             PetscScalar val = 0.;
4269:             for (j = 0; j < numCols; j++) {
4270:               val += childMat[i * numCols + j] * cVal[j];
4271:             }
4272:             rVal[i] = val;
4273:           }
4274:           if (f == fvField && p >= cellStart && p < cellEnd) {
4275:             PetscReal   centroid[3];
4276:             PetscScalar diff[3];
4277:             const PetscScalar *parentCentroid = &fvGradData[0];
4278:             const PetscScalar *gradient       = &fvGradData[dim];

4280:             DMPlexComputeCellGeometryFVM(fine,p,NULL,centroid,NULL);
4281:             for (i = 0; i < dim; i++) {
4282:               diff[i] = centroid[i] - parentCentroid[i];
4283:             }
4284:             for (i = 0; i < numFVcomps; i++) {
4285:               PetscScalar val = 0.;

4287:               for (j = 0; j < dim; j++) {
4288:                 val += gradient[dim * i + j] * diff[j];
4289:               }
4290:               rVal[i] += val;
4291:             }
4292:           }
4293:           VecSetValues(vecFine,numRows,&rowIndices[offsets[f]],rVal,INSERT_VALUES);
4294:         }
4295:       }
4296:     }
4297:     DMPlexReferenceTreeRestoreChildrenMatrices(refTree,&refPointFieldMats,&refPointFieldN);
4298:     DMRestoreWorkArray(fine,maxDof,MPIU_SCALAR,&pointWork);
4299:     DMRestoreWorkArray(fine,maxDof,MPIU_INT,&rowIndices);
4300:   }
4301:   PetscFree(leafValues);
4302:   PetscSectionDestroy(&leafValuesSec);
4303:   PetscFree7(offsets,offsetsCopy,newOffsets,newOffsetsCopy,rowOffsets,numD,numO);
4304:   ISRestoreIndices(aIS,&anchors);
4305:   return(0);
4306: }

4308: static PetscErrorCode DMPlexTransferVecTree_Inject(DM fine, Vec vecFine, DM coarse, Vec vecCoarse, PetscSF coarseToFine, PetscInt *cids)
4309: {
4310:   DM             refTree;
4311:   PetscSection   multiRootSec, rootIndicesSec;
4312:   PetscSection   globalCoarse, globalFine;
4313:   PetscSection   localCoarse, localFine;
4314:   PetscSection   cSecRef;
4315:   PetscInt       *parentIndices, pRefStart, pRefEnd;
4316:   PetscScalar    *rootValues, *parentValues;
4317:   Mat            injRef;
4318:   PetscInt       numFields, maxDof;
4319:   PetscInt       pStartC, pEndC, pStartF, pEndF, p;
4320:   PetscInt       *offsets, *offsetsCopy, *rowOffsets;
4321:   PetscLayout    rowMap, colMap;
4322:   PetscInt       rowStart, rowEnd, colStart, colEnd;
4323:   PetscScalar    ***childrenMats=NULL ; /* gcc -O gives 'may be used uninitialized' warning'. Initializing to suppress this warning */


4328:   /* get the templates for the fine-to-coarse injection from the reference tree */
4329:   VecSetOption(vecFine,VEC_IGNORE_NEGATIVE_INDICES,PETSC_TRUE);
4330:   VecSetOption(vecCoarse,VEC_IGNORE_NEGATIVE_INDICES,PETSC_TRUE);
4331:   DMPlexGetReferenceTree(coarse,&refTree);
4332:   DMCopyDisc(coarse,refTree);
4333:   DMGetDefaultConstraints(refTree,&cSecRef,NULL);
4334:   PetscSectionGetChart(cSecRef,&pRefStart,&pRefEnd);
4335:   DMPlexReferenceTreeGetInjector(refTree,&injRef);

4337:   DMPlexGetChart(fine,&pStartF,&pEndF);
4338:   DMGetLocalSection(fine,&localFine);
4339:   DMGetGlobalSection(fine,&globalFine);
4340:   PetscSectionGetNumFields(localFine,&numFields);
4341:   DMPlexGetChart(coarse,&pStartC,&pEndC);
4342:   DMGetLocalSection(coarse,&localCoarse);
4343:   DMGetGlobalSection(coarse,&globalCoarse);
4344:   PetscSectionGetMaxDof(localCoarse,&maxDof);
4345:   {
4346:     PetscInt maxFields = PetscMax(1,numFields) + 1;
4347:     PetscMalloc3(maxFields,&offsets,maxFields,&offsetsCopy,maxFields,&rowOffsets);
4348:   }

4350:   DMPlexTransferInjectorTree(coarse,fine,coarseToFine,cids,vecFine,numFields,offsets,&multiRootSec,&rootIndicesSec,NULL,&rootValues);

4352:   PetscMalloc2(maxDof,&parentIndices,maxDof,&parentValues);

4354:   /* count indices */
4355:   VecGetLayout(vecFine,&colMap);
4356:   VecGetLayout(vecCoarse,&rowMap);
4357:   PetscLayoutSetUp(rowMap);
4358:   PetscLayoutSetUp(colMap);
4359:   PetscLayoutGetRange(rowMap,&rowStart,&rowEnd);
4360:   PetscLayoutGetRange(colMap,&colStart,&colEnd);
4361:   /* insert values */
4362:   DMPlexReferenceTreeGetChildrenMatrices_Injection(refTree,injRef,&childrenMats);
4363:   for (p = pStartC; p < pEndC; p++) {
4364:     PetscInt  numLeaves, leafStart, leafEnd, l, dof, cdof, gOff;
4365:     PetscBool contribute = PETSC_FALSE;

4367:     PetscSectionGetDof(globalCoarse,p,&dof);
4368:     PetscSectionGetConstraintDof(globalCoarse,p,&cdof);
4369:     if ((dof - cdof) <= 0) continue;
4370:     PetscSectionGetDof(localCoarse,p,&dof);
4371:     PetscSectionGetOffset(globalCoarse,p,&gOff);

4373:     rowOffsets[0] = 0;
4374:     offsetsCopy[0] = 0;
4375:     if (numFields) {
4376:       PetscInt f;

4378:       for (f = 0; f < numFields; f++) {
4379:         PetscInt fDof;
4380:         PetscSectionGetFieldDof(localCoarse,p,f,&fDof);
4381:         rowOffsets[f + 1] = offsetsCopy[f + 1] = fDof + rowOffsets[f];
4382:       }
4383:       DMPlexGetIndicesPointFields_Internal(localCoarse,PETSC_FALSE,p,gOff < 0 ? -(gOff + 1) : gOff,offsetsCopy,PETSC_FALSE,NULL,-1,NULL,parentIndices);
4384:     } else {
4385:       DMPlexGetIndicesPoint_Internal(localCoarse,PETSC_FALSE,p,gOff < 0 ? -(gOff + 1) : gOff,offsetsCopy,PETSC_FALSE,NULL,NULL,parentIndices);
4386:       rowOffsets[1] = offsetsCopy[0];
4387:     }

4389:     PetscSectionGetDof(multiRootSec,p,&numLeaves);
4390:     PetscSectionGetOffset(multiRootSec,p,&leafStart);
4391:     leafEnd = leafStart + numLeaves;
4392:     for (l = 0; l < dof; l++) parentValues[l] = 0.;
4393:     for (l = leafStart; l < leafEnd; l++) {
4394:       PetscInt numIndices, childId, offset;
4395:       const PetscScalar *childValues;

4397:       PetscSectionGetDof(rootIndicesSec,l,&numIndices);
4398:       PetscSectionGetOffset(rootIndicesSec,l,&offset);
4399:       childId = (PetscInt) PetscRealPart(rootValues[offset++]);
4400:       childValues = &rootValues[offset];
4401:       numIndices--;

4403:       if (childId == -2) { /* skip */
4404:         continue;
4405:       } else if (childId == -1) { /* equivalent points: scatter */
4406:         PetscInt m;

4408:         contribute = PETSC_TRUE;
4409:         for (m = 0; m < numIndices; m++) parentValues[m] = childValues[m];
4410:       } else { /* contributions from children: sum with injectors from reference tree */
4411:         PetscInt parentId, f, lim;

4413:         contribute = PETSC_TRUE;
4414:         DMPlexGetTreeParent(refTree,childId,&parentId,NULL);

4416:         lim = PetscMax(1,numFields);
4417:         offsets[0] = 0;
4418:         if (numFields) {
4419:           PetscInt f;

4421:           for (f = 0; f < numFields; f++) {
4422:             PetscInt fDof;
4423:             PetscSectionGetFieldDof(cSecRef,childId,f,&fDof);

4425:             offsets[f + 1] = fDof + offsets[f];
4426:           }
4427:         }
4428:         else {
4429:           PetscInt cDof;

4431:           PetscSectionGetDof(cSecRef,childId,&cDof);
4432:           offsets[1] = cDof;
4433:         }
4434:         for (f = 0; f < lim; f++) {
4435:           PetscScalar       *childMat   = &childrenMats[childId - pRefStart][f][0];
4436:           PetscInt          n           = offsets[f+1]-offsets[f];
4437:           PetscInt          m           = rowOffsets[f+1]-rowOffsets[f];
4438:           PetscInt          i, j;
4439:           const PetscScalar *colValues  = &childValues[offsets[f]];

4441:           for (i = 0; i < m; i++) {
4442:             PetscScalar val = 0.;
4443:             for (j = 0; j < n; j++) {
4444:               val += childMat[n * i + j] * colValues[j];
4445:             }
4446:             parentValues[rowOffsets[f] + i] += val;
4447:           }
4448:         }
4449:       }
4450:     }
4451:     if (contribute) {VecSetValues(vecCoarse,dof,parentIndices,parentValues,INSERT_VALUES);}
4452:   }
4453:   PetscSectionDestroy(&multiRootSec);
4454:   PetscSectionDestroy(&rootIndicesSec);
4455:   PetscFree2(parentIndices,parentValues);
4456:   DMPlexReferenceTreeRestoreChildrenMatrices_Injection(refTree,injRef,&childrenMats);
4457:   PetscFree(rootValues);
4458:   PetscFree3(offsets,offsetsCopy,rowOffsets);
4459:   return(0);
4460: }

4462: /*@
4463:   DMPlexTransferVecTree - transfer a vector between two meshes that differ from each other by refinement/coarsening
4464:   that can be represented by a common reference tree used by both.  This routine can be used for a combination of
4465:   coarsening and refinement at the same time.

4467:   collective

4469:   Input Parameters:
4470: + dmIn        - The DMPlex mesh for the input vector
4471: . vecIn       - The input vector
4472: . sfRefine    - A star forest indicating points in the mesh dmIn (roots in the star forest) that are parents to points in
4473:                 the mesh dmOut (leaves in the star forest), i.e. where dmOut is more refined than dmIn
4474: . sfCoarsen   - A star forest indicating points in the mesh dmOut (roots in the star forest) that are parents to points in
4475:                 the mesh dmIn (leaves in the star forest), i.e. where dmOut is more coarsened than dmIn
4476: . cidsRefine  - The childIds of the points in dmOut.  These childIds relate back to the reference tree: childid[j] = k implies
4477:                 that mesh point j of dmOut was refined from a point in dmIn just as the mesh point k in the reference
4478:                 tree was refined from its parent.  childid[j] = -1 indicates that the point j in dmOut is exactly
4479:                 equivalent to its root in dmIn, so no interpolation is necessary.  childid[j] = -2 indicates that this
4480:                 point j in dmOut is not a leaf of sfRefine.
4481: . cidsCoarsen - The childIds of the points in dmIn.  These childIds relate back to the reference tree: childid[j] = k implies
4482:                 that mesh point j of dmIn coarsens to a point in dmOut just as the mesh point k in the reference
4483:                 tree coarsens to its parent.  childid[j] = -2 indicates that point j in dmOut is not a leaf in sfCoarsen.
4484: . useBCs      - PETSC_TRUE indicates that boundary values should be inserted into vecIn before transfer.
4485: - time        - Used if boundary values are time dependent.

4487:   Output Parameters:
4488: . vecOut      - Using interpolation and injection operators calculated on the reference tree, the transferred
4489:                 projection of vecIn from dmIn to dmOut.  Note that any field discretized with a PetscFV finite volume
4490:                 method that uses gradient reconstruction will use reconstructed gradients when interpolating from
4491:                 coarse points to fine points.

4493:   Level: developer

4495: .seealso: DMPlexSetReferenceTree(), DMPlexGetReferenceTree(), PetscFVGetComputeGradients()
4496: @*/
4497: PetscErrorCode DMPlexTransferVecTree(DM dmIn, Vec vecIn, DM dmOut, Vec vecOut, PetscSF sfRefine, PetscSF sfCoarsen, PetscInt *cidsRefine, PetscInt *cidsCoarsen, PetscBool useBCs, PetscReal time)
4498: {

4502:   VecSet(vecOut,0.0);
4503:   if (sfRefine) {
4504:     Vec vecInLocal;
4505:     DM  dmGrad = NULL;
4506:     Vec faceGeom = NULL, cellGeom = NULL, grad = NULL;

4508:     DMGetLocalVector(dmIn,&vecInLocal);
4509:     VecSet(vecInLocal,0.0);
4510:     {
4511:       PetscInt  numFields, i;

4513:       DMGetNumFields(dmIn, &numFields);
4514:       for (i = 0; i < numFields; i++) {
4515:         PetscObject  obj;
4516:         PetscClassId classid;

4518:         DMGetField(dmIn, i, NULL, &obj);
4519:         PetscObjectGetClassId(obj, &classid);
4520:         if (classid == PETSCFV_CLASSID) {
4521:           DMPlexGetDataFVM(dmIn,(PetscFV)obj,&cellGeom,&faceGeom,&dmGrad);
4522:           break;
4523:         }
4524:       }
4525:     }
4526:     if (useBCs) {
4527:       DMPlexInsertBoundaryValues(dmIn,PETSC_TRUE,vecInLocal,time,faceGeom,cellGeom,NULL);
4528:     }
4529:     DMGlobalToLocalBegin(dmIn,vecIn,INSERT_VALUES,vecInLocal);
4530:     DMGlobalToLocalEnd(dmIn,vecIn,INSERT_VALUES,vecInLocal);
4531:     if (dmGrad) {
4532:       DMGetGlobalVector(dmGrad,&grad);
4533:       DMPlexReconstructGradientsFVM(dmIn,vecInLocal,grad);
4534:     }
4535:     DMPlexTransferVecTree_Interpolate(dmIn,vecInLocal,dmOut,vecOut,sfRefine,cidsRefine,grad,cellGeom);
4536:     DMRestoreLocalVector(dmIn,&vecInLocal);
4537:     if (dmGrad) {
4538:       DMRestoreGlobalVector(dmGrad,&grad);
4539:     }
4540:   }
4541:   if (sfCoarsen) {
4542:     DMPlexTransferVecTree_Inject(dmIn,vecIn,dmOut,vecOut,sfCoarsen,cidsCoarsen);
4543:   }
4544:   VecAssemblyBegin(vecOut);
4545:   VecAssemblyEnd(vecOut);
4546:   return(0);
4547: }