Actual source code: plex.c

petsc-master 2019-09-20
Report Typos and Errors
  1:  #include <petsc/private/dmpleximpl.h>
  2:  #include <petsc/private/isimpl.h>
  3:  #include <petsc/private/vecimpl.h>
  4:  #include <petsc/private/glvisvecimpl.h>
  5:  #include <petscsf.h>
  6:  #include <petscds.h>
  7:  #include <petscdraw.h>
  8:  #include <petscdmfield.h>

 10: /* Logging support */
 11: PetscLogEvent DMPLEX_Interpolate, DMPLEX_Partition, DMPLEX_Distribute, DMPLEX_DistributeCones, DMPLEX_DistributeLabels, DMPLEX_DistributeSF, DMPLEX_DistributeOverlap, DMPLEX_DistributeField, DMPLEX_DistributeData, DMPLEX_Migrate, DMPLEX_InterpolateSF, DMPLEX_GlobalToNaturalBegin, DMPLEX_GlobalToNaturalEnd, DMPLEX_NaturalToGlobalBegin, DMPLEX_NaturalToGlobalEnd, DMPLEX_Stratify, DMPLEX_Symmetrize, DMPLEX_Preallocate, DMPLEX_ResidualFEM, DMPLEX_JacobianFEM, DMPLEX_InterpolatorFEM, DMPLEX_InjectorFEM, DMPLEX_IntegralFEM, DMPLEX_CreateGmsh, DMPLEX_RebalanceSharedPoints, DMPLEX_PartSelf, DMPLEX_PartLabelInvert, DMPLEX_PartLabelCreateSF, DMPLEX_PartStratSF, DMPLEX_CreatePointSF;

 13: PETSC_EXTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);

 15: /*@
 16:   DMPlexRefineSimplexToTensor - Uniformly refines simplicial cells into tensor product cells.
 17:   3 quadrilaterals per triangle in 2D and 4 hexahedra per tetrahedron in 3D.

 19:   Collective

 21:   Input Parameters:
 22: . dm - The DMPlex object

 24:   Output Parameters:
 25: . dmRefined - The refined DMPlex object

 27:   Note: Returns NULL if the mesh is already a tensor product mesh.

 29:   Level: intermediate

 31: .seealso: DMPlexCreate(), DMPlexSetRefinementUniform()
 32: @*/
 33: PetscErrorCode DMPlexRefineSimplexToTensor(DM dm, DM *dmRefined)
 34: {
 35:   PetscInt         dim, cMax, fMax, cStart, cEnd, coneSize;
 36:   CellRefiner      cellRefiner;
 37:   PetscBool        lop, allnoop, localized;
 38:   PetscErrorCode   ierr;

 43:   DMGetDimension(dm, &dim);
 44:   DMPlexGetHybridBounds(dm,&cMax,&fMax,NULL,NULL);
 45:   DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);
 46:   if (!(cEnd - cStart)) cellRefiner = REFINER_NOOP;
 47:   else {
 48:     DMPlexGetConeSize(dm,cStart,&coneSize);
 49:     switch (dim) {
 50:     case 1:
 51:       cellRefiner = REFINER_NOOP;
 52:     break;
 53:     case 2:
 54:       switch (coneSize) {
 55:       case 3:
 56:         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_2D;
 57:         else cellRefiner = REFINER_SIMPLEX_TO_HEX_2D;
 58:       break;
 59:       case 4:
 60:         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_2D;
 61:         else cellRefiner = REFINER_NOOP;
 62:       break;
 63:       default: SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle coneSize %D with dimension %D",coneSize,dim);
 64:       }
 65:     break;
 66:     case 3:
 67:       switch (coneSize) {
 68:       case 4:
 69:         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_3D;
 70:         else cellRefiner = REFINER_SIMPLEX_TO_HEX_3D;
 71:       break;
 72:       case 5:
 73:         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_3D;
 74:         else cellRefiner = REFINER_NOOP;
 75:       break;
 76:       case 6:
 77:         if (cMax >= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Simplex2Tensor in 3D with Hybrid mesh not yet done");
 78:         cellRefiner = REFINER_NOOP;
 79:       break;
 80:       default: SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle coneSize %D with dimension %D",coneSize,dim);
 81:       }
 82:     break;
 83:     default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle dimension %D",dim);
 84:     }
 85:   }
 86:   /* return if we don't need to refine */
 87:   lop = (cellRefiner == REFINER_NOOP) ? PETSC_TRUE : PETSC_FALSE;
 88:   MPIU_Allreduce(&lop,&allnoop,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));
 89:   if (allnoop) {
 90:     *dmRefined = NULL;
 91:     return(0);
 92:   }
 93:   DMPlexRefineUniform_Internal(dm, cellRefiner, dmRefined);
 94:   DMCopyBoundary(dm, *dmRefined);
 95:   DMGetCoordinatesLocalized(dm, &localized);
 96:   if (localized) {
 97:     DMLocalizeCoordinates(*dmRefined);
 98:   }
 99:   return(0);
100: }

102: PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
103: {
104:   PetscInt       dim, pStart, pEnd, vStart, vEnd, cStart, cEnd, cEndInterior;
105:   PetscInt       vcdof[2] = {0,0}, globalvcdof[2];

109:   *ft  = PETSC_VTK_POINT_FIELD;
110:   DMGetDimension(dm, &dim);
111:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
112:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
113:   DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);
114:   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
115:   PetscSectionGetChart(section, &pStart, &pEnd);
116:   if (field >= 0) {
117:     if ((vStart >= pStart) && (vStart < pEnd)) {PetscSectionGetFieldDof(section, vStart, field, &vcdof[0]);}
118:     if ((cStart >= pStart) && (cStart < pEnd)) {PetscSectionGetFieldDof(section, cStart, field, &vcdof[1]);}
119:   } else {
120:     if ((vStart >= pStart) && (vStart < pEnd)) {PetscSectionGetDof(section, vStart, &vcdof[0]);}
121:     if ((cStart >= pStart) && (cStart < pEnd)) {PetscSectionGetDof(section, cStart, &vcdof[1]);}
122:   }
123:   MPI_Allreduce(vcdof, globalvcdof, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));
124:   if (globalvcdof[0]) {
125:     *sStart = vStart;
126:     *sEnd   = vEnd;
127:     if (globalvcdof[0] == dim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
128:     else                       *ft = PETSC_VTK_POINT_FIELD;
129:   } else if (globalvcdof[1]) {
130:     *sStart = cStart;
131:     *sEnd   = cEnd;
132:     if (globalvcdof[1] == dim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
133:     else                       *ft = PETSC_VTK_CELL_FIELD;
134:   } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Could not classify input Vec for VTK");
135:   return(0);
136: }

138: static PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
139: {
140:   DM                 dm;
141:   PetscSection       s;
142:   PetscDraw          draw, popup;
143:   DM                 cdm;
144:   PetscSection       coordSection;
145:   Vec                coordinates;
146:   const PetscScalar *coords, *array;
147:   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
148:   PetscReal          vbound[2], time;
149:   PetscBool          isnull, flg;
150:   PetscInt           dim, Nf, f, Nc, comp, vStart, vEnd, cStart, cEnd, c, N, level, step, w = 0;
151:   const char        *name;
152:   char               title[PETSC_MAX_PATH_LEN];
153:   PetscErrorCode     ierr;

156:   PetscViewerDrawGetDraw(viewer, 0, &draw);
157:   PetscDrawIsNull(draw, &isnull);
158:   if (isnull) return(0);

160:   VecGetDM(v, &dm);
161:   DMGetCoordinateDim(dm, &dim);
162:   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D. Use PETSCVIEWERGLVIS", dim);
163:   DMGetLocalSection(dm, &s);
164:   PetscSectionGetNumFields(s, &Nf);
165:   DMGetCoarsenLevel(dm, &level);
166:   DMGetCoordinateDM(dm, &cdm);
167:   DMGetLocalSection(cdm, &coordSection);
168:   DMGetCoordinatesLocal(dm, &coordinates);
169:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
170:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);

172:   PetscObjectGetName((PetscObject) v, &name);
173:   DMGetOutputSequenceNumber(dm, &step, &time);

175:   VecGetLocalSize(coordinates, &N);
176:   VecGetArrayRead(coordinates, &coords);
177:   for (c = 0; c < N; c += dim) {
178:     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
179:     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
180:   }
181:   VecRestoreArrayRead(coordinates, &coords);
182:   PetscDrawClear(draw);

184:   /* Could implement something like DMDASelectFields() */
185:   for (f = 0; f < Nf; ++f) {
186:     DM   fdm = dm;
187:     Vec  fv  = v;
188:     IS   fis;
189:     char prefix[PETSC_MAX_PATH_LEN];
190:     const char *fname;

192:     PetscSectionGetFieldComponents(s, f, &Nc);
193:     PetscSectionGetFieldName(s, f, &fname);

195:     if (v->hdr.prefix) {PetscStrncpy(prefix, v->hdr.prefix,sizeof(prefix));}
196:     else               {prefix[0] = '\0';}
197:     if (Nf > 1) {
198:       DMCreateSubDM(dm, 1, &f, &fis, &fdm);
199:       VecGetSubVector(v, fis, &fv);
200:       PetscStrlcat(prefix, fname,sizeof(prefix));
201:       PetscStrlcat(prefix, "_",sizeof(prefix));
202:     }
203:     for (comp = 0; comp < Nc; ++comp, ++w) {
204:       PetscInt nmax = 2;

206:       PetscViewerDrawGetDraw(viewer, w, &draw);
207:       if (Nc > 1) {PetscSNPrintf(title, sizeof(title), "%s:%s_%D Step: %D Time: %.4g", name, fname, comp, step, time);}
208:       else        {PetscSNPrintf(title, sizeof(title), "%s:%s Step: %D Time: %.4g", name, fname, step, time);}
209:       PetscDrawSetTitle(draw, title);

211:       /* TODO Get max and min only for this component */
212:       PetscOptionsGetRealArray(NULL, prefix, "-vec_view_bounds", vbound, &nmax, &flg);
213:       if (!flg) {
214:         VecMin(fv, NULL, &vbound[0]);
215:         VecMax(fv, NULL, &vbound[1]);
216:         if (vbound[1] <= vbound[0]) vbound[1] = vbound[0] + 1.0;
217:       }
218:       PetscDrawGetPopup(draw, &popup);
219:       PetscDrawScalePopup(popup, vbound[0], vbound[1]);
220:       PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);

222:       VecGetArrayRead(fv, &array);
223:       for (c = cStart; c < cEnd; ++c) {
224:         PetscScalar *coords = NULL, *a = NULL;
225:         PetscInt     numCoords, color[4] = {-1,-1,-1,-1};

227:         DMPlexPointLocalRead(fdm, c, array, &a);
228:         if (a) {
229:           color[0] = PetscDrawRealToColor(PetscRealPart(a[comp]), vbound[0], vbound[1]);
230:           color[1] = color[2] = color[3] = color[0];
231:         } else {
232:           PetscScalar *vals = NULL;
233:           PetscInt     numVals, va;

235:           DMPlexVecGetClosure(fdm, NULL, fv, c, &numVals, &vals);
236:           if (numVals % Nc) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of components %D does not divide the number of values in the closure %D", Nc, numVals);
237:           switch (numVals/Nc) {
238:           case 3: /* P1 Triangle */
239:           case 4: /* P1 Quadrangle */
240:             for (va = 0; va < numVals/Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp]), vbound[0], vbound[1]);
241:             break;
242:           case 6: /* P2 Triangle */
243:           case 8: /* P2 Quadrangle */
244:             for (va = 0; va < numVals/(Nc*2); ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp + numVals/(Nc*2)]), vbound[0], vbound[1]);
245:             break;
246:           default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of values for cell closure %D cannot be handled", numVals/Nc);
247:           }
248:           DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals);
249:         }
250:         DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);
251:         switch (numCoords) {
252:         case 6:
253:           PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), color[0], color[1], color[2]);
254:           break;
255:         case 8:
256:           PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), color[0], color[1], color[2]);
257:           PetscDrawTriangle(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), color[2], color[3], color[0]);
258:           break;
259:         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
260:         }
261:         DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);
262:       }
263:       VecRestoreArrayRead(fv, &array);
264:       PetscDrawFlush(draw);
265:       PetscDrawPause(draw);
266:       PetscDrawSave(draw);
267:     }
268:     if (Nf > 1) {
269:       VecRestoreSubVector(v, fis, &fv);
270:       ISDestroy(&fis);
271:       DMDestroy(&fdm);
272:     }
273:   }
274:   return(0);
275: }

277: static PetscErrorCode VecView_Plex_Local_VTK(Vec v, PetscViewer viewer)
278: {
279:   DM                      dm;
280:   Vec                     locv;
281:   const char              *name;
282:   PetscSection            section;
283:   PetscInt                pStart, pEnd;
284:   PetscViewerVTKFieldType ft;
285:   PetscErrorCode          ierr;

288:   VecGetDM(v, &dm);
289:   DMCreateLocalVector(dm, &locv); /* VTK viewer requires exclusive ownership of the vector */
290:   PetscObjectGetName((PetscObject) v, &name);
291:   PetscObjectSetName((PetscObject) locv, name);
292:   VecCopy(v, locv);
293:   DMGetLocalSection(dm, &section);
294:   DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);
295:   PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, ft, PETSC_TRUE,(PetscObject) locv);
296:   return(0);
297: }

299: PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
300: {
301:   DM             dm;
302:   PetscBool      isvtk, ishdf5, isdraw, isglvis;

306:   VecGetDM(v, &dm);
307:   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
308:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);
309:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);
310:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);
311:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);
312:   if (isvtk || ishdf5 || isdraw || isglvis) {
313:     PetscInt    i,numFields;
314:     PetscObject fe;
315:     PetscBool   fem = PETSC_FALSE;
316:     Vec         locv = v;
317:     const char  *name;
318:     PetscInt    step;
319:     PetscReal   time;

321:     DMGetNumFields(dm, &numFields);
322:     for (i=0; i<numFields; i++) {
323:       DMGetField(dm, i, NULL, &fe);
324:       if (fe->classid == PETSCFE_CLASSID) { fem = PETSC_TRUE; break; }
325:     }
326:     if (fem) {
327:       DMGetLocalVector(dm, &locv);
328:       PetscObjectGetName((PetscObject) v, &name);
329:       PetscObjectSetName((PetscObject) locv, name);
330:       VecCopy(v, locv);
331:       DMGetOutputSequenceNumber(dm, NULL, &time);
332:       DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);
333:     }
334:     if (isvtk) {
335:       VecView_Plex_Local_VTK(locv, viewer);
336:     } else if (ishdf5) {
337: #if defined(PETSC_HAVE_HDF5)
338:       VecView_Plex_Local_HDF5_Internal(locv, viewer);
339: #else
340:       SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
341: #endif
342:     } else if (isdraw) {
343:       VecView_Plex_Local_Draw(locv, viewer);
344:     } else if (isglvis) {
345:       DMGetOutputSequenceNumber(dm, &step, NULL);
346:       PetscViewerGLVisSetSnapId(viewer, step);
347:       VecView_GLVis(locv, viewer);
348:     }
349:     if (fem) {DMRestoreLocalVector(dm, &locv);}
350:   } else {
351:     PetscBool isseq;

353:     PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);
354:     if (isseq) {VecView_Seq(v, viewer);}
355:     else       {VecView_MPI(v, viewer);}
356:   }
357:   return(0);
358: }

360: PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
361: {
362:   DM             dm;
363:   PetscBool      isvtk, ishdf5, isdraw, isglvis;

367:   VecGetDM(v, &dm);
368:   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
369:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);
370:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);
371:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);
372:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);
373:   if (isvtk || isdraw || isglvis) {
374:     Vec         locv;
375:     const char *name;

377:     DMGetLocalVector(dm, &locv);
378:     PetscObjectGetName((PetscObject) v, &name);
379:     PetscObjectSetName((PetscObject) locv, name);
380:     DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);
381:     DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);
382:     VecView_Plex_Local(locv, viewer);
383:     DMRestoreLocalVector(dm, &locv);
384:   } else if (ishdf5) {
385: #if defined(PETSC_HAVE_HDF5)
386:     VecView_Plex_HDF5_Internal(v, viewer);
387: #else
388:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
389: #endif
390:   } else {
391:     PetscBool isseq;

393:     PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);
394:     if (isseq) {VecView_Seq(v, viewer);}
395:     else       {VecView_MPI(v, viewer);}
396:   }
397:   return(0);
398: }

400: PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
401: {
402:   DM                dm;
403:   MPI_Comm          comm;
404:   PetscViewerFormat format;
405:   Vec               v;
406:   PetscBool         isvtk, ishdf5;
407:   PetscErrorCode    ierr;

410:   VecGetDM(originalv, &dm);
411:   PetscObjectGetComm((PetscObject) originalv, &comm);
412:   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
413:   PetscViewerGetFormat(viewer, &format);
414:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);
415:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);
416:   if (format == PETSC_VIEWER_NATIVE) {
417:     /* Natural ordering is the common case for DMDA, NATIVE means plain vector, for PLEX is the opposite */
418:     /* this need a better fix */
419:     if (dm->useNatural) {
420:       if (dm->sfNatural) {
421:         const char *vecname;
422:         PetscInt    n, nroots;

424:         VecGetLocalSize(originalv, &n);
425:         PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);
426:         if (n == nroots) {
427:           DMGetGlobalVector(dm, &v);
428:           DMPlexGlobalToNaturalBegin(dm, originalv, v);
429:           DMPlexGlobalToNaturalEnd(dm, originalv, v);
430:           PetscObjectGetName((PetscObject) originalv, &vecname);
431:           PetscObjectSetName((PetscObject) v, vecname);
432:         } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
433:       } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
434:     } else v = originalv;
435:   } else v = originalv;

437:   if (ishdf5) {
438: #if defined(PETSC_HAVE_HDF5)
439:     VecView_Plex_HDF5_Native_Internal(v, viewer);
440: #else
441:     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
442: #endif
443:   } else if (isvtk) {
444:     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
445:   } else {
446:     PetscBool isseq;

448:     PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);
449:     if (isseq) {VecView_Seq(v, viewer);}
450:     else       {VecView_MPI(v, viewer);}
451:   }
452:   if (v != originalv) {DMRestoreGlobalVector(dm, &v);}
453:   return(0);
454: }

456: PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
457: {
458:   DM             dm;
459:   PetscBool      ishdf5;

463:   VecGetDM(v, &dm);
464:   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
465:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);
466:   if (ishdf5) {
467:     DM          dmBC;
468:     Vec         gv;
469:     const char *name;

471:     DMGetOutputDM(dm, &dmBC);
472:     DMGetGlobalVector(dmBC, &gv);
473:     PetscObjectGetName((PetscObject) v, &name);
474:     PetscObjectSetName((PetscObject) gv, name);
475:     VecLoad_Default(gv, viewer);
476:     DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);
477:     DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);
478:     DMRestoreGlobalVector(dmBC, &gv);
479:   } else {
480:     VecLoad_Default(v, viewer);
481:   }
482:   return(0);
483: }

485: PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
486: {
487:   DM             dm;
488:   PetscBool      ishdf5;

492:   VecGetDM(v, &dm);
493:   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
494:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);
495:   if (ishdf5) {
496: #if defined(PETSC_HAVE_HDF5)
497:     VecLoad_Plex_HDF5_Internal(v, viewer);
498: #else
499:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
500: #endif
501:   } else {
502:     VecLoad_Default(v, viewer);
503:   }
504:   return(0);
505: }

507: PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
508: {
509:   DM                dm;
510:   PetscViewerFormat format;
511:   PetscBool         ishdf5;
512:   PetscErrorCode    ierr;

515:   VecGetDM(originalv, &dm);
516:   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
517:   PetscViewerGetFormat(viewer, &format);
518:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);
519:   if (format == PETSC_VIEWER_NATIVE) {
520:     if (dm->useNatural) {
521:       if (dm->sfNatural) {
522:         if (ishdf5) {
523: #if defined(PETSC_HAVE_HDF5)
524:           Vec         v;
525:           const char *vecname;

527:           DMGetGlobalVector(dm, &v);
528:           PetscObjectGetName((PetscObject) originalv, &vecname);
529:           PetscObjectSetName((PetscObject) v, vecname);
530:           VecLoad_Plex_HDF5_Native_Internal(v, viewer);
531:           DMPlexNaturalToGlobalBegin(dm, v, originalv);
532:           DMPlexNaturalToGlobalEnd(dm, v, originalv);
533:           DMRestoreGlobalVector(dm, &v);
534: #else
535:           SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
536: #endif
537:         } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
538:       }
539:     } else {
540:       VecLoad_Default(originalv, viewer);
541:     }
542:   }
543:   return(0);
544: }

546: PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
547: {
548:   PetscSection       coordSection;
549:   Vec                coordinates;
550:   DMLabel            depthLabel;
551:   const char        *name[4];
552:   const PetscScalar *a;
553:   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
554:   PetscErrorCode     ierr;

557:   DMGetDimension(dm, &dim);
558:   DMGetCoordinatesLocal(dm, &coordinates);
559:   DMGetCoordinateSection(dm, &coordSection);
560:   DMPlexGetDepthLabel(dm, &depthLabel);
561:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
562:   PetscSectionGetChart(coordSection, &pStart, &pEnd);
563:   VecGetArrayRead(coordinates, &a);
564:   name[0]     = "vertex";
565:   name[1]     = "edge";
566:   name[dim-1] = "face";
567:   name[dim]   = "cell";
568:   for (c = cStart; c < cEnd; ++c) {
569:     PetscInt *closure = NULL;
570:     PetscInt  closureSize, cl;

572:     PetscViewerASCIIPrintf(viewer, "Geometry for cell %D:\n", c);
573:     DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
574:     PetscViewerASCIIPushTab(viewer);
575:     for (cl = 0; cl < closureSize*2; cl += 2) {
576:       PetscInt point = closure[cl], depth, dof, off, d, p;

578:       if ((point < pStart) || (point >= pEnd)) continue;
579:       PetscSectionGetDof(coordSection, point, &dof);
580:       if (!dof) continue;
581:       DMLabelGetValue(depthLabel, point, &depth);
582:       PetscSectionGetOffset(coordSection, point, &off);
583:       PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);
584:       for (p = 0; p < dof/dim; ++p) {
585:         PetscViewerASCIIPrintf(viewer, " (");
586:         for (d = 0; d < dim; ++d) {
587:           if (d > 0) {PetscViewerASCIIPrintf(viewer, ", ");}
588:           PetscViewerASCIIPrintf(viewer, "%g", PetscRealPart(a[off+p*dim+d]));
589:         }
590:         PetscViewerASCIIPrintf(viewer, ")");
591:       }
592:       PetscViewerASCIIPrintf(viewer, "\n");
593:     }
594:     DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
595:     PetscViewerASCIIPopTab(viewer);
596:   }
597:   VecRestoreArrayRead(coordinates, &a);
598:   return(0);
599: }

601: static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
602: {
603:   DM_Plex          *mesh = (DM_Plex*) dm->data;
604:   DM                cdm;
605:   DMLabel           markers;
606:   PetscSection      coordSection;
607:   Vec               coordinates;
608:   PetscViewerFormat format;
609:   PetscErrorCode    ierr;

612:   DMGetCoordinateDM(dm, &cdm);
613:   DMGetLocalSection(cdm, &coordSection);
614:   DMGetCoordinatesLocal(dm, &coordinates);
615:   PetscViewerGetFormat(viewer, &format);
616:   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
617:     const char *name;
618:     PetscInt    dim, cellHeight, maxConeSize, maxSupportSize;
619:     PetscInt    pStart, pEnd, p;
620:     PetscMPIInt rank, size;

622:     MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
623:     MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);
624:     PetscObjectGetName((PetscObject) dm, &name);
625:     DMPlexGetChart(dm, &pStart, &pEnd);
626:     DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);
627:     DMGetDimension(dm, &dim);
628:     DMPlexGetVTKCellHeight(dm, &cellHeight);
629:     if (name) {PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");}
630:     else      {PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");}
631:     if (cellHeight) {PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);}
632:     PetscViewerASCIIPrintf(viewer, "Supports:\n", name);
633:     PetscViewerASCIIPushSynchronized(viewer);
634:     PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max support size: %D\n", rank, maxSupportSize);
635:     for (p = pStart; p < pEnd; ++p) {
636:       PetscInt dof, off, s;

638:       PetscSectionGetDof(mesh->supportSection, p, &dof);
639:       PetscSectionGetOffset(mesh->supportSection, p, &off);
640:       for (s = off; s < off+dof; ++s) {
641:         PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);
642:       }
643:     }
644:     PetscViewerFlush(viewer);
645:     PetscViewerASCIIPrintf(viewer, "Cones:\n", name);
646:     PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max cone size: %D\n", rank, maxConeSize);
647:     for (p = pStart; p < pEnd; ++p) {
648:       PetscInt dof, off, c;

650:       PetscSectionGetDof(mesh->coneSection, p, &dof);
651:       PetscSectionGetOffset(mesh->coneSection, p, &off);
652:       for (c = off; c < off+dof; ++c) {
653:         PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);
654:       }
655:     }
656:     PetscViewerFlush(viewer);
657:     PetscViewerASCIIPopSynchronized(viewer);
658:     if (coordSection && coordinates) {
659:       PetscSectionVecView(coordSection, coordinates, viewer);
660:     }
661:     DMGetLabel(dm, "marker", &markers);
662:     if (markers) {DMLabelView(markers,viewer);}
663:     if (size > 1) {
664:       PetscSF sf;

666:       DMGetPointSF(dm, &sf);
667:       PetscSFView(sf, viewer);
668:     }
669:     PetscViewerFlush(viewer);
670:   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
671:     const char  *name, *color;
672:     const char  *defcolors[3]  = {"gray", "orange", "green"};
673:     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
674:     char         lname[PETSC_MAX_PATH_LEN];
675:     PetscReal    scale         = 2.0;
676:     PetscReal    tikzscale     = 1.0;
677:     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
678:     double       tcoords[3];
679:     PetscScalar *coords;
680:     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
681:     PetscMPIInt  rank, size;
682:     char         **names, **colors, **lcolors;
683:     PetscBool    plotEdges, flg, lflg;
684:     PetscBT      wp = NULL;
685:     PetscInt     pEnd, pStart;

687:     DMGetDimension(dm, &dim);
688:     DMPlexGetDepth(dm, &depth);
689:     DMGetNumLabels(dm, &numLabels);
690:     numLabels  = PetscMax(numLabels, 10);
691:     numColors  = 10;
692:     numLColors = 10;
693:     PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);
694:     PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);
695:     PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_tikzscale", &tikzscale, NULL);
696:     PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);
697:     PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);
698:     if (!useLabels) numLabels = 0;
699:     PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);
700:     if (!useColors) {
701:       numColors = 3;
702:       for (c = 0; c < numColors; ++c) {PetscStrallocpy(defcolors[c], &colors[c]);}
703:     }
704:     PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);
705:     if (!useColors) {
706:       numLColors = 4;
707:       for (c = 0; c < numLColors; ++c) {PetscStrallocpy(deflcolors[c], &lcolors[c]);}
708:     }
709:     PetscOptionsGetString(((PetscObject) viewer)->options, ((PetscObject) viewer)->prefix, "-dm_plex_view_label_filter", lname, PETSC_MAX_PATH_LEN, &lflg);
710:     plotEdges = (PetscBool)(depth > 1 && useNumbers && dim < 3);
711:     PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_edges", &plotEdges, &flg);
712:     if (flg && plotEdges && depth < dim) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Mesh must be interpolated");
713:     if (depth < dim) plotEdges = PETSC_FALSE;

715:     /* filter points with labelvalue != labeldefaultvalue */
716:     DMPlexGetChart(dm, &pStart, &pEnd);
717:     if (lflg) {
718:       DMLabel lbl;

720:       DMGetLabel(dm, lname, &lbl);
721:       if (lbl) {
722:         PetscInt val, defval;

724:         DMLabelGetDefaultValue(lbl, &defval);
725:         PetscBTCreate(pEnd-pStart, &wp);
726:         for (c = pStart;  c < pEnd; c++) {
727:           PetscInt *closure = NULL;
728:           PetscInt  closureSize;

730:           DMLabelGetValue(lbl, c, &val);
731:           if (val == defval) continue;

733:           DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
734:           for (p = 0; p < closureSize*2; p += 2) {
735:             PetscBTSet(wp, closure[p] - pStart);
736:           }
737:           DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
738:         }
739:       }
740:     }

742:     MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
743:     MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);
744:     PetscObjectGetName((PetscObject) dm, &name);
745:     PetscViewerASCIIPrintf(viewer, "\
746: \\documentclass[tikz]{standalone}\n\n\
747: \\usepackage{pgflibraryshapes}\n\
748: \\usetikzlibrary{backgrounds}\n\
749: \\usetikzlibrary{arrows}\n\
750: \\begin{document}\n");
751:     if (size > 1) {
752:       PetscViewerASCIIPrintf(viewer, "%s for process ", name);
753:       for (p = 0; p < size; ++p) {
754:         if (p > 0 && p == size-1) {
755:           PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);
756:         } else if (p > 0) {
757:           PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);
758:         }
759:         PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);
760:       }
761:       PetscViewerASCIIPrintf(viewer, ".\n\n\n");
762:     }
763:     PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", tikzscale);

765:     /* Plot vertices */
766:     DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
767:     VecGetArray(coordinates, &coords);
768:     PetscViewerASCIIPushSynchronized(viewer);
769:     for (v = vStart; v < vEnd; ++v) {
770:       PetscInt  off, dof, d;
771:       PetscBool isLabeled = PETSC_FALSE;

773:       if (wp && !PetscBTLookup(wp,v - pStart)) continue;
774:       PetscSectionGetDof(coordSection, v, &dof);
775:       PetscSectionGetOffset(coordSection, v, &off);
776:       PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");
777:       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
778:       for (d = 0; d < dof; ++d) {
779:         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
780:         tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
781:       }
782:       /* Rotate coordinates since PGF makes z point out of the page instead of up */
783:       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
784:       for (d = 0; d < dof; ++d) {
785:         if (d > 0) {PetscViewerASCIISynchronizedPrintf(viewer, ",");}
786:         PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]);
787:       }
788:       color = colors[rank%numColors];
789:       for (l = 0; l < numLabels; ++l) {
790:         PetscInt val;
791:         DMGetLabelValue(dm, names[l], v, &val);
792:         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
793:       }
794:       if (useNumbers) {
795:         PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);
796:       } else {
797:         PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);
798:       }
799:     }
800:     VecRestoreArray(coordinates, &coords);
801:     PetscViewerFlush(viewer);
802:     /* Plot cells */
803:     DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
804:     DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);
805:     if (dim == 3 || !useNumbers) {
806:       for (e = eStart; e < eEnd; ++e) {
807:         const PetscInt *cone;

809:         if (wp && !PetscBTLookup(wp,e - pStart)) continue;
810:         color = colors[rank%numColors];
811:         for (l = 0; l < numLabels; ++l) {
812:           PetscInt val;
813:           DMGetLabelValue(dm, names[l], e, &val);
814:           if (val >= 0) {color = lcolors[l%numLColors]; break;}
815:         }
816:         DMPlexGetCone(dm, e, &cone);
817:         PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);
818:       }
819:     } else {
820:       for (c = cStart; c < cEnd; ++c) {
821:         PetscInt *closure = NULL;
822:         PetscInt  closureSize, firstPoint = -1;

824:         if (wp && !PetscBTLookup(wp,c - pStart)) continue;
825:         DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
826:         PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);
827:         for (p = 0; p < closureSize*2; p += 2) {
828:           const PetscInt point = closure[p];

830:           if ((point < vStart) || (point >= vEnd)) continue;
831:           if (firstPoint >= 0) {PetscViewerASCIISynchronizedPrintf(viewer, " -- ");}
832:           PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);
833:           if (firstPoint < 0) firstPoint = point;
834:         }
835:         /* Why doesn't this work? PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n"); */
836:         PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);
837:         DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
838:       }
839:     }
840:     VecGetArray(coordinates, &coords);
841:     for (c = cStart; c < cEnd; ++c) {
842:       double    ccoords[3] = {0.0, 0.0, 0.0};
843:       PetscBool isLabeled  = PETSC_FALSE;
844:       PetscInt *closure    = NULL;
845:       PetscInt  closureSize, dof, d, n = 0;

847:       if (wp && !PetscBTLookup(wp,c - pStart)) continue;
848:       DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
849:       PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");
850:       for (p = 0; p < closureSize*2; p += 2) {
851:         const PetscInt point = closure[p];
852:         PetscInt       off;

854:         if ((point < vStart) || (point >= vEnd)) continue;
855:         PetscSectionGetDof(coordSection, point, &dof);
856:         PetscSectionGetOffset(coordSection, point, &off);
857:         for (d = 0; d < dof; ++d) {
858:           tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
859:           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
860:         }
861:         /* Rotate coordinates since PGF makes z point out of the page instead of up */
862:         if (dof == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
863:         for (d = 0; d < dof; ++d) {ccoords[d] += tcoords[d];}
864:         ++n;
865:       }
866:       for (d = 0; d < dof; ++d) {ccoords[d] /= n;}
867:       DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
868:       for (d = 0; d < dof; ++d) {
869:         if (d > 0) {PetscViewerASCIISynchronizedPrintf(viewer, ",");}
870:         PetscViewerASCIISynchronizedPrintf(viewer, "%g", ccoords[d]);
871:       }
872:       color = colors[rank%numColors];
873:       for (l = 0; l < numLabels; ++l) {
874:         PetscInt val;
875:         DMGetLabelValue(dm, names[l], c, &val);
876:         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
877:       }
878:       if (useNumbers) {
879:         PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", c, rank, color, c);
880:       } else {
881:         PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", c, rank, !isLabeled ? 1 : 2, color);
882:       }
883:     }
884:     VecRestoreArray(coordinates, &coords);
885:     /* Plot edges */
886:     if (plotEdges) {
887:       VecGetArray(coordinates, &coords);
888:       PetscViewerASCIIPrintf(viewer, "\\path\n");
889:       for (e = eStart; e < eEnd; ++e) {
890:         const PetscInt *cone;
891:         PetscInt        coneSize, offA, offB, dof, d;

893:         if (wp && !PetscBTLookup(wp,e - pStart)) continue;
894:         DMPlexGetConeSize(dm, e, &coneSize);
895:         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
896:         DMPlexGetCone(dm, e, &cone);
897:         PetscSectionGetDof(coordSection, cone[0], &dof);
898:         PetscSectionGetOffset(coordSection, cone[0], &offA);
899:         PetscSectionGetOffset(coordSection, cone[1], &offB);
900:         PetscViewerASCIISynchronizedPrintf(viewer, "(");
901:         for (d = 0; d < dof; ++d) {
902:           tcoords[d] = (double) (0.5*scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
903:           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
904:         }
905:         /* Rotate coordinates since PGF makes z point out of the page instead of up */
906:         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
907:         for (d = 0; d < dof; ++d) {
908:           if (d > 0) {PetscViewerASCIISynchronizedPrintf(viewer, ",");}
909:           PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);
910:         }
911:         color = colors[rank%numColors];
912:         for (l = 0; l < numLabels; ++l) {
913:           PetscInt val;
914:           DMGetLabelValue(dm, names[l], v, &val);
915:           if (val >= 0) {color = lcolors[l%numLColors]; break;}
916:         }
917:         PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);
918:       }
919:       VecRestoreArray(coordinates, &coords);
920:       PetscViewerFlush(viewer);
921:       PetscViewerASCIIPrintf(viewer, "(0,0);\n");
922:     }
923:     PetscViewerFlush(viewer);
924:     PetscViewerASCIIPopSynchronized(viewer);
925:     PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");
926:     PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);
927:     for (l = 0; l < numLabels;  ++l) {PetscFree(names[l]);}
928:     for (c = 0; c < numColors;  ++c) {PetscFree(colors[c]);}
929:     for (c = 0; c < numLColors; ++c) {PetscFree(lcolors[c]);}
930:     PetscFree3(names, colors, lcolors);
931:     PetscBTDestroy(&wp);
932:   } else if (format == PETSC_VIEWER_LOAD_BALANCE) {
933:     Vec                    cown,acown;
934:     VecScatter             sct;
935:     ISLocalToGlobalMapping g2l;
936:     IS                     gid,acis;
937:     MPI_Comm               comm,ncomm = MPI_COMM_NULL;
938:     MPI_Group              ggroup,ngroup;
939:     PetscScalar            *array,nid;
940:     const PetscInt         *idxs;
941:     PetscInt               *idxs2,*start,*adjacency,*work;
942:     PetscInt64             lm[3],gm[3];
943:     PetscInt               i,c,cStart,cEnd,cum,numVertices,ect,ectn,cellHeight;
944:     PetscMPIInt            d1,d2,rank;

946:     PetscObjectGetComm((PetscObject)dm,&comm);
947:     MPI_Comm_rank(comm,&rank);
948: #if defined(PETSC_HAVE_MPI_SHARED_COMM)
949:     MPI_Comm_split_type(comm,MPI_COMM_TYPE_SHARED,rank,MPI_INFO_NULL,&ncomm);
950: #endif
951:     if (ncomm != MPI_COMM_NULL) {
952:       MPI_Comm_group(comm,&ggroup);
953:       MPI_Comm_group(ncomm,&ngroup);
954:       d1   = 0;
955:       MPI_Group_translate_ranks(ngroup,1,&d1,ggroup,&d2);
956:       nid  = d2;
957:       MPI_Group_free(&ggroup);
958:       MPI_Group_free(&ngroup);
959:       MPI_Comm_free(&ncomm);
960:     } else nid = 0.0;

962:     /* Get connectivity */
963:     DMPlexGetVTKCellHeight(dm,&cellHeight);
964:     DMPlexCreatePartitionerGraph(dm,cellHeight,&numVertices,&start,&adjacency,&gid);

966:     /* filter overlapped local cells */
967:     DMPlexGetHeightStratum(dm,cellHeight,&cStart,&cEnd);
968:     ISGetIndices(gid,&idxs);
969:     ISGetLocalSize(gid,&cum);
970:     PetscMalloc1(cum,&idxs2);
971:     for (c = cStart, cum = 0; c < cEnd; c++) {
972:       if (idxs[c-cStart] < 0) continue;
973:       idxs2[cum++] = idxs[c-cStart];
974:     }
975:     ISRestoreIndices(gid,&idxs);
976:     if (numVertices != cum) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected %D != %D",numVertices,cum);
977:     ISDestroy(&gid);
978:     ISCreateGeneral(comm,numVertices,idxs2,PETSC_OWN_POINTER,&gid);

980:     /* support for node-aware cell locality */
981:     ISCreateGeneral(comm,start[numVertices],adjacency,PETSC_USE_POINTER,&acis);
982:     VecCreateSeq(PETSC_COMM_SELF,start[numVertices],&acown);
983:     VecCreateMPI(comm,numVertices,PETSC_DECIDE,&cown);
984:     VecGetArray(cown,&array);
985:     for (c = 0; c < numVertices; c++) array[c] = nid;
986:     VecRestoreArray(cown,&array);
987:     VecScatterCreate(cown,acis,acown,NULL,&sct);
988:     VecScatterBegin(sct,cown,acown,INSERT_VALUES,SCATTER_FORWARD);
989:     VecScatterEnd(sct,cown,acown,INSERT_VALUES,SCATTER_FORWARD);
990:     ISDestroy(&acis);
991:     VecScatterDestroy(&sct);
992:     VecDestroy(&cown);

994:     /* compute edgeCut */
995:     for (c = 0, cum = 0; c < numVertices; c++) cum = PetscMax(cum,start[c+1]-start[c]);
996:     PetscMalloc1(cum,&work);
997:     ISLocalToGlobalMappingCreateIS(gid,&g2l);
998:     ISLocalToGlobalMappingSetType(g2l,ISLOCALTOGLOBALMAPPINGHASH);
999:     ISDestroy(&gid);
1000:     VecGetArray(acown,&array);
1001:     for (c = 0, ect = 0, ectn = 0; c < numVertices; c++) {
1002:       PetscInt totl;

1004:       totl = start[c+1]-start[c];
1005:       ISGlobalToLocalMappingApply(g2l,IS_GTOLM_MASK,totl,adjacency+start[c],NULL,work);
1006:       for (i = 0; i < totl; i++) {
1007:         if (work[i] < 0) {
1008:           ect  += 1;
1009:           ectn += (array[i + start[c]] != nid) ? 0 : 1;
1010:         }
1011:       }
1012:     }
1013:     PetscFree(work);
1014:     VecRestoreArray(acown,&array);
1015:     lm[0] = numVertices > 0 ?  numVertices : PETSC_MAX_INT;
1016:     lm[1] = -numVertices;
1017:     MPIU_Allreduce(lm,gm,2,MPIU_INT64,MPI_MIN,comm);
1018:     PetscViewerASCIIPrintf(viewer,"  Cell balance: %.2f (max %D, min %D",-((double)gm[1])/((double)gm[0]),-(PetscInt)gm[1],(PetscInt)gm[0]);
1019:     lm[0] = ect; /* edgeCut */
1020:     lm[1] = ectn; /* node-aware edgeCut */
1021:     lm[2] = numVertices > 0 ? 0 : 1; /* empty processes */
1022:     MPIU_Allreduce(lm,gm,3,MPIU_INT64,MPI_SUM,comm);
1023:     PetscViewerASCIIPrintf(viewer,", empty %D)\n",(PetscInt)gm[2]);
1024: #if defined(PETSC_HAVE_MPI_SHARED_COMM)
1025:     PetscViewerASCIIPrintf(viewer,"  Edge Cut: %D (on node %.3f)\n",(PetscInt)(gm[0]/2),gm[0] ? ((double)(gm[1]))/((double)gm[0]) : 1.);
1026: #else
1027:     PetscViewerASCIIPrintf(viewer,"  Edge Cut: %D (on node %.3f)\n",(PetscInt)(gm[0]/2),0.0);
1028: #endif
1029:     ISLocalToGlobalMappingDestroy(&g2l);
1030:     PetscFree(start);
1031:     PetscFree(adjacency);
1032:     VecDestroy(&acown);
1033:   } else {
1034:     MPI_Comm    comm;
1035:     PetscInt   *sizes, *hybsizes;
1036:     PetscInt    locDepth, depth, cellHeight, dim, d, pMax[4];
1037:     PetscInt    pStart, pEnd, p;
1038:     PetscInt    numLabels, l;
1039:     const char *name;
1040:     PetscMPIInt size;

1042:     PetscObjectGetComm((PetscObject)dm,&comm);
1043:     MPI_Comm_size(comm, &size);
1044:     DMGetDimension(dm, &dim);
1045:     DMPlexGetVTKCellHeight(dm, &cellHeight);
1046:     PetscObjectGetName((PetscObject) dm, &name);
1047:     if (name) {PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");}
1048:     else      {PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");}
1049:     if (cellHeight) {PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);}
1050:     DMPlexGetDepth(dm, &locDepth);
1051:     MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);
1052:     DMPlexGetHybridBounds(dm, &pMax[depth], depth > 0 ? &pMax[depth-1] : NULL, depth > 1 ? &pMax[depth - 2] : NULL, &pMax[0]);
1053:     PetscCalloc2(size,&sizes,size,&hybsizes);
1054:     if (depth == 1) {
1055:       DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);
1056:       pEnd = pEnd - pStart;
1057:       pMax[0] -= pStart;
1058:       MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);
1059:       MPI_Gather(&pMax[0], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);
1060:       PetscViewerASCIIPrintf(viewer, "  %d-cells:", 0);
1061:       for (p = 0; p < size; ++p) {
1062:         if (hybsizes[p] >= 0) {PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);}
1063:         else                  {PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);}
1064:       }
1065:       PetscViewerASCIIPrintf(viewer, "\n");
1066:       DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);
1067:       pEnd = pEnd - pStart;
1068:       pMax[depth] -= pStart;
1069:       MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);
1070:       MPI_Gather(&pMax[depth], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);
1071:       PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);
1072:       for (p = 0; p < size; ++p) {
1073:         if (hybsizes[p] >= 0) {PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);}
1074:         else                  {PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);}
1075:       }
1076:       PetscViewerASCIIPrintf(viewer, "\n");
1077:     } else {
1078:       PetscMPIInt rank;
1079:       MPI_Comm_rank(comm, &rank);
1080:       for (d = 0; d <= dim; d++) {
1081:         DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);
1082:         pEnd    -= pStart;
1083:         pMax[d] -= pStart;
1084:         MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);
1085:         MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);
1086:         PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);
1087:         for (p = 0; p < size; ++p) {
1088:           if (!rank) {
1089:             if (hybsizes[p] >= 0) {PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);}
1090:             else                  {PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);}
1091:           }
1092:         }
1093:         PetscViewerASCIIPrintf(viewer, "\n");
1094:       }
1095:     }
1096:     PetscFree2(sizes,hybsizes);
1097:     DMGetNumLabels(dm, &numLabels);
1098:     if (numLabels) {PetscViewerASCIIPrintf(viewer, "Labels:\n");}
1099:     for (l = 0; l < numLabels; ++l) {
1100:       DMLabel         label;
1101:       const char     *name;
1102:       IS              valueIS;
1103:       const PetscInt *values;
1104:       PetscInt        numValues, v;

1106:       DMGetLabelName(dm, l, &name);
1107:       DMGetLabel(dm, name, &label);
1108:       DMLabelGetNumValues(label, &numValues);
1109:       PetscViewerASCIIPrintf(viewer, "  %s: %D strata with value/size (", name, numValues);
1110:       DMLabelGetValueIS(label, &valueIS);
1111:       ISGetIndices(valueIS, &values);
1112:       PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);
1113:       for (v = 0; v < numValues; ++v) {
1114:         PetscInt size;

1116:         DMLabelGetStratumSize(label, values[v], &size);
1117:         if (v > 0) {PetscViewerASCIIPrintf(viewer, ", ");}
1118:         PetscViewerASCIIPrintf(viewer, "%D (%D)", values[v], size);
1119:       }
1120:       PetscViewerASCIIPrintf(viewer, ")\n");
1121:       PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);
1122:       ISRestoreIndices(valueIS, &values);
1123:       ISDestroy(&valueIS);
1124:     }
1125:     /* If no fields are specified, people do not want to see adjacency */
1126:     if (dm->Nf) {
1127:       PetscInt f;

1129:       for (f = 0; f < dm->Nf; ++f) {
1130:         const char *name;

1132:         PetscObjectGetName(dm->fields[f].disc, &name);
1133:         if (numLabels) {PetscViewerASCIIPrintf(viewer, "Field %s:\n", name);}
1134:         PetscViewerASCIIPushTab(viewer);
1135:         if (dm->fields[f].label) {DMLabelView(dm->fields[f].label, viewer);}
1136:         if (dm->fields[f].adjacency[0]) {
1137:           if (dm->fields[f].adjacency[1]) {PetscViewerASCIIPrintf(viewer, "adjacency FVM++\n");}
1138:           else                            {PetscViewerASCIIPrintf(viewer, "adjacency FVM\n");}
1139:         } else {
1140:           if (dm->fields[f].adjacency[1]) {PetscViewerASCIIPrintf(viewer, "adjacency FEM\n");}
1141:           else                            {PetscViewerASCIIPrintf(viewer, "adjacency FUNKY\n");}
1142:         }
1143:         PetscViewerASCIIPopTab(viewer);
1144:       }
1145:     }
1146:     DMGetCoarseDM(dm, &cdm);
1147:     if (cdm) {
1148:       PetscViewerASCIIPushTab(viewer);
1149:       DMPlexView_Ascii(cdm, viewer);
1150:       PetscViewerASCIIPopTab(viewer);
1151:     }
1152:   }
1153:   return(0);
1154: }

1156: static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
1157: {
1158:   PetscDraw          draw;
1159:   DM                 cdm;
1160:   PetscSection       coordSection;
1161:   Vec                coordinates;
1162:   const PetscScalar *coords;
1163:   PetscReal          xyl[2],xyr[2],bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
1164:   PetscBool          isnull;
1165:   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N;
1166:   PetscMPIInt        rank;
1167:   PetscErrorCode     ierr;

1170:   DMGetCoordinateDim(dm, &dim);
1171:   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
1172:   DMGetCoordinateDM(dm, &cdm);
1173:   DMGetLocalSection(cdm, &coordSection);
1174:   DMGetCoordinatesLocal(dm, &coordinates);
1175:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
1176:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);

1178:   PetscViewerDrawGetDraw(viewer, 0, &draw);
1179:   PetscDrawIsNull(draw, &isnull);
1180:   if (isnull) return(0);
1181:   PetscDrawSetTitle(draw, "Mesh");

1183:   VecGetLocalSize(coordinates, &N);
1184:   VecGetArrayRead(coordinates, &coords);
1185:   for (c = 0; c < N; c += dim) {
1186:     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
1187:     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
1188:   }
1189:   VecRestoreArrayRead(coordinates, &coords);
1190:   MPIU_Allreduce(&bound[0],xyl,2,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)dm));
1191:   MPIU_Allreduce(&bound[2],xyr,2,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)dm));
1192:   PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);
1193:   PetscDrawClear(draw);

1195:   MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
1196:   for (c = cStart; c < cEnd; ++c) {
1197:     PetscScalar *coords = NULL;
1198:     PetscInt     numCoords,coneSize;

1200:     DMPlexGetConeSize(dm, c, &coneSize);
1201:     DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);
1202:     switch (coneSize) {
1203:     case 3:
1204:       PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1205:                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1206:                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1207:                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);
1208:       break;
1209:     case 4:
1210:       PetscDrawRectangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1211:                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1212:                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1213:                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1214:                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);
1215:       break;
1216:     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D facets", coneSize);
1217:     }
1218:     DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);
1219:   }
1220:   for (c = cStart; c < cEnd; ++c) {
1221:     PetscScalar *coords = NULL;
1222:     PetscInt     numCoords,coneSize;

1224:     DMPlexGetConeSize(dm, c, &coneSize);
1225:     DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);
1226:     switch (coneSize) {
1227:     case 3:
1228:       PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);
1229:       PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);
1230:       PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);
1231:       break;
1232:     case 4:
1233:       PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);
1234:       PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);
1235:       PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);
1236:       PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);
1237:       break;
1238:     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D facets", coneSize);
1239:     }
1240:     DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);
1241:   }
1242:   PetscDrawFlush(draw);
1243:   PetscDrawPause(draw);
1244:   PetscDrawSave(draw);
1245:   return(0);
1246: }

1248: PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
1249: {
1250:   PetscBool      iascii, ishdf5, isvtk, isdraw, flg, isglvis;
1251:   char           name[PETSC_MAX_PATH_LEN];

1257:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);
1258:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);
1259:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);
1260:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);
1261:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);
1262:   if (iascii) {
1263:     PetscViewerFormat format;
1264:     PetscViewerGetFormat(viewer, &format);
1265:     if (format == PETSC_VIEWER_ASCII_GLVIS) {
1266:       DMPlexView_GLVis(dm, viewer);
1267:     } else {
1268:       DMPlexView_Ascii(dm, viewer);
1269:     }
1270:   } else if (ishdf5) {
1271: #if defined(PETSC_HAVE_HDF5)
1272:     DMPlexView_HDF5_Internal(dm, viewer);
1273: #else
1274:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1275: #endif
1276:   } else if (isvtk) {
1277:     DMPlexVTKWriteAll((PetscObject) dm,viewer);
1278:   } else if (isdraw) {
1279:     DMPlexView_Draw(dm, viewer);
1280:   } else if (isglvis) {
1281:     DMPlexView_GLVis(dm, viewer);
1282:   } else {
1283:     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex writing", ((PetscObject)viewer)->type_name);
1284:   }
1285:   /* Optionally view the partition */
1286:   PetscOptionsHasName(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_partition_view", &flg);
1287:   if (flg) {
1288:     Vec ranks;
1289:     DMPlexCreateRankField(dm, &ranks);
1290:     VecView(ranks, viewer);
1291:     VecDestroy(&ranks);
1292:   }
1293:   /* Optionally view a label */
1294:   PetscOptionsGetString(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_label_view", name, PETSC_MAX_PATH_LEN, &flg);
1295:   if (flg) {
1296:     DMLabel label;
1297:     Vec     val;

1299:     DMGetLabel(dm, name, &label);
1300:     if (!label) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Label %s provided to -dm_label_view does not exist in this DM", name);
1301:     DMPlexCreateLabelField(dm, label, &val);
1302:     VecView(val, viewer);
1303:     VecDestroy(&val);
1304:   }
1305:   return(0);
1306: }

1308: PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
1309: {
1310:   PetscBool      ishdf5;

1316:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);
1317:   if (ishdf5) {
1318: #if defined(PETSC_HAVE_HDF5)
1319:     PetscViewerFormat format;
1320:     PetscViewerGetFormat(viewer, &format);
1321:     if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
1322:       DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer);
1323:     } else if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
1324:       DMPlexLoad_HDF5_Internal(dm, viewer);
1325:     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
1326: #else
1327:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1328: #endif
1329:   } else {
1330:     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
1331:   }
1332:   return(0);
1333: }

1335: PetscErrorCode DMDestroy_Plex(DM dm)
1336: {
1337:   DM_Plex       *mesh = (DM_Plex*) dm->data;

1341:   PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);
1342:   PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);
1343:   if (--mesh->refct > 0) return(0);
1344:   PetscSectionDestroy(&mesh->coneSection);
1345:   PetscFree(mesh->cones);
1346:   PetscFree(mesh->coneOrientations);
1347:   PetscSectionDestroy(&mesh->supportSection);
1348:   PetscSectionDestroy(&mesh->subdomainSection);
1349:   PetscFree(mesh->supports);
1350:   PetscFree(mesh->facesTmp);
1351:   PetscFree(mesh->tetgenOpts);
1352:   PetscFree(mesh->triangleOpts);
1353:   PetscPartitionerDestroy(&mesh->partitioner);
1354:   DMLabelDestroy(&mesh->subpointMap);
1355:   ISDestroy(&mesh->globalVertexNumbers);
1356:   ISDestroy(&mesh->globalCellNumbers);
1357:   PetscSectionDestroy(&mesh->anchorSection);
1358:   ISDestroy(&mesh->anchorIS);
1359:   PetscSectionDestroy(&mesh->parentSection);
1360:   PetscFree(mesh->parents);
1361:   PetscFree(mesh->childIDs);
1362:   PetscSectionDestroy(&mesh->childSection);
1363:   PetscFree(mesh->children);
1364:   DMDestroy(&mesh->referenceTree);
1365:   PetscGridHashDestroy(&mesh->lbox);
1366:   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1367:   PetscFree(mesh);
1368:   return(0);
1369: }

1371: PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
1372: {
1373:   PetscSection           sectionGlobal;
1374:   PetscInt               bs = -1, mbs;
1375:   PetscInt               localSize;
1376:   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
1377:   PetscErrorCode         ierr;
1378:   MatType                mtype;
1379:   ISLocalToGlobalMapping ltog;

1382:   MatInitializePackage();
1383:   mtype = dm->mattype;
1384:   DMGetGlobalSection(dm, &sectionGlobal);
1385:   /* PetscSectionGetStorageSize(sectionGlobal, &localSize); */
1386:   PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);
1387:   MatCreate(PetscObjectComm((PetscObject)dm), J);
1388:   MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);
1389:   MatSetType(*J, mtype);
1390:   MatSetFromOptions(*J);
1391:   MatGetBlockSize(*J, &mbs);
1392:   if (mbs > 1) bs = mbs;
1393:   PetscStrcmp(mtype, MATSHELL, &isShell);
1394:   PetscStrcmp(mtype, MATBAIJ, &isBlock);
1395:   PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);
1396:   PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);
1397:   PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);
1398:   PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);
1399:   PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);
1400:   PetscStrcmp(mtype, MATIS, &isMatIS);
1401:   if (!isShell) {
1402:     PetscSection subSection;
1403:     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
1404:     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *ltogidx, lsize;
1405:     PetscInt     pStart, pEnd, p, dof, cdof;

1407:     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
1408:     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
1409:       PetscSection section;
1410:       PetscInt     size;

1412:       DMGetLocalSection(dm, &section);
1413:       PetscSectionGetStorageSize(section, &size);
1414:       PetscMalloc1(size,&ltogidx);
1415:       DMPlexGetSubdomainSection(dm, &subSection);
1416:     } else {
1417:       DMGetLocalToGlobalMapping(dm,&ltog);
1418:     }
1419:     PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);
1420:     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1421:       PetscInt bdof;

1423:       PetscSectionGetDof(sectionGlobal, p, &dof);
1424:       PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);
1425:       dof  = dof < 0 ? -(dof+1) : dof;
1426:       bdof = cdof && (dof-cdof) ? 1 : dof;
1427:       if (dof) {
1428:         if (bs < 0)          {bs = bdof;}
1429:         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1430:       }
1431:       if (isMatIS) {
1432:         PetscInt loff,c,off;
1433:         PetscSectionGetOffset(subSection, p, &loff);
1434:         PetscSectionGetOffset(sectionGlobal, p, &off);
1435:         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1436:       }
1437:     }
1438:     /* Must have same blocksize on all procs (some might have no points) */
1439:     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
1440:     PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);
1441:     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
1442:     else                            {bs = bsMinMax[0];}
1443:     bs = PetscMax(1,bs);
1444:     if (isMatIS) { /* Must reduce indices by blocksize */
1445:       PetscInt l;

1447:       lsize = lsize/bs;
1448:       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] = ltogidx[l*bs]/bs;
1449:       ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);
1450:     }
1451:     MatSetLocalToGlobalMapping(*J,ltog,ltog);
1452:     if (isMatIS) {
1453:       ISLocalToGlobalMappingDestroy(&ltog);
1454:     }
1455:     PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);
1456:     DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);
1457:     PetscFree4(dnz, onz, dnzu, onzu);
1458:   }
1459:   MatSetDM(*J, dm);
1460:   return(0);
1461: }

1463: /*@
1464:   DMPlexGetSubdomainSection - Returns the section associated with the subdomain

1466:   Not collective

1468:   Input Parameter:
1469: . mesh - The DMPlex

1471:   Output Parameters:
1472: . subsection - The subdomain section

1474:   Level: developer

1476: .seealso:
1477: @*/
1478: PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1479: {
1480:   DM_Plex       *mesh = (DM_Plex*) dm->data;

1485:   if (!mesh->subdomainSection) {
1486:     PetscSection section;
1487:     PetscSF      sf;

1489:     PetscSFCreate(PETSC_COMM_SELF,&sf);
1490:     DMGetLocalSection(dm,&section);
1491:     PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);
1492:     PetscSFDestroy(&sf);
1493:   }
1494:   *subsection = mesh->subdomainSection;
1495:   return(0);
1496: }

1498: /*@
1499:   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)

1501:   Not collective

1503:   Input Parameter:
1504: . mesh - The DMPlex

1506:   Output Parameters:
1507: + pStart - The first mesh point
1508: - pEnd   - The upper bound for mesh points

1510:   Level: beginner

1512: .seealso: DMPlexCreate(), DMPlexSetChart()
1513: @*/
1514: PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1515: {
1516:   DM_Plex       *mesh = (DM_Plex*) dm->data;

1521:   PetscSectionGetChart(mesh->coneSection, pStart, pEnd);
1522:   return(0);
1523: }

1525: /*@
1526:   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)

1528:   Not collective

1530:   Input Parameters:
1531: + mesh - The DMPlex
1532: . pStart - The first mesh point
1533: - pEnd   - The upper bound for mesh points

1535:   Output Parameters:

1537:   Level: beginner

1539: .seealso: DMPlexCreate(), DMPlexGetChart()
1540: @*/
1541: PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1542: {
1543:   DM_Plex       *mesh = (DM_Plex*) dm->data;

1548:   PetscSectionSetChart(mesh->coneSection, pStart, pEnd);
1549:   PetscSectionSetChart(mesh->supportSection, pStart, pEnd);
1550:   return(0);
1551: }

1553: /*@
1554:   DMPlexGetConeSize - Return the number of in-edges for this point in the DAG

1556:   Not collective

1558:   Input Parameters:
1559: + mesh - The DMPlex
1560: - p - The point, which must lie in the chart set with DMPlexSetChart()

1562:   Output Parameter:
1563: . size - The cone size for point p

1565:   Level: beginner

1567: .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1568: @*/
1569: PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1570: {
1571:   DM_Plex       *mesh = (DM_Plex*) dm->data;

1577:   PetscSectionGetDof(mesh->coneSection, p, size);
1578:   return(0);
1579: }

1581: /*@
1582:   DMPlexSetConeSize - Set the number of in-edges for this point in the DAG

1584:   Not collective

1586:   Input Parameters:
1587: + mesh - The DMPlex
1588: . p - The point, which must lie in the chart set with DMPlexSetChart()
1589: - size - The cone size for point p

1591:   Output Parameter:

1593:   Note:
1594:   This should be called after DMPlexSetChart().

1596:   Level: beginner

1598: .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1599: @*/
1600: PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1601: {
1602:   DM_Plex       *mesh = (DM_Plex*) dm->data;

1607:   PetscSectionSetDof(mesh->coneSection, p, size);

1609:   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1610:   return(0);
1611: }

1613: /*@
1614:   DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG

1616:   Not collective

1618:   Input Parameters:
1619: + mesh - The DMPlex
1620: . p - The point, which must lie in the chart set with DMPlexSetChart()
1621: - size - The additional cone size for point p

1623:   Output Parameter:

1625:   Note:
1626:   This should be called after DMPlexSetChart().

1628:   Level: beginner

1630: .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1631: @*/
1632: PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1633: {
1634:   DM_Plex       *mesh = (DM_Plex*) dm->data;
1635:   PetscInt       csize;

1640:   PetscSectionAddDof(mesh->coneSection, p, size);
1641:   PetscSectionGetDof(mesh->coneSection, p, &csize);

1643:   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1644:   return(0);
1645: }

1647: /*@C
1648:   DMPlexGetCone - Return the points on the in-edges for this point in the DAG

1650:   Not collective

1652:   Input Parameters:
1653: + dm - The DMPlex
1654: - p - The point, which must lie in the chart set with DMPlexSetChart()

1656:   Output Parameter:
1657: . cone - An array of points which are on the in-edges for point p

1659:   Level: beginner

1661:   Fortran Notes:
1662:   Since it returns an array, this routine is only available in Fortran 90, and you must
1663:   include petsc.h90 in your code.
1664:   You must also call DMPlexRestoreCone() after you finish using the returned array.
1665:   DMPlexRestoreCone() is not needed/available in C.

1667: .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexGetConeTuple(), DMPlexSetChart()
1668: @*/
1669: PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1670: {
1671:   DM_Plex       *mesh = (DM_Plex*) dm->data;
1672:   PetscInt       off;

1678:   PetscSectionGetOffset(mesh->coneSection, p, &off);
1679:   *cone = &mesh->cones[off];
1680:   return(0);
1681: }

1683: /*@C
1684:   DMPlexGetConeTuple - Return the points on the in-edges of several points in the DAG

1686:   Not collective

1688:   Input Parameters:
1689: + dm - The DMPlex
1690: - p - The IS of points, which must lie in the chart set with DMPlexSetChart()

1692:   Output Parameter:
1693: + pConesSection - PetscSection describing the layout of pCones
1694: - pCones - An array of points which are on the in-edges for the point set p

1696:   Level: intermediate

1698: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeRecursive(), DMPlexSetChart()
1699: @*/
1700: PetscErrorCode DMPlexGetConeTuple(DM dm, IS p, PetscSection *pConesSection, IS *pCones)
1701: {
1702:   PetscSection        cs, newcs;
1703:   PetscInt            *cones;
1704:   PetscInt            *newarr=NULL;
1705:   PetscInt            n;
1706:   PetscErrorCode      ierr;

1709:   DMPlexGetCones(dm, &cones);
1710:   DMPlexGetConeSection(dm, &cs);
1711:   PetscSectionExtractDofsFromArray(cs, MPIU_INT, cones, p, &newcs, pCones ? ((void**)&newarr) : NULL);
1712:   if (pConesSection) *pConesSection = newcs;
1713:   if (pCones) {
1714:     PetscSectionGetStorageSize(newcs, &n);
1715:     ISCreateGeneral(PetscObjectComm((PetscObject)p), n, newarr, PETSC_OWN_POINTER, pCones);
1716:   }
1717:   return(0);
1718: }

1720: /*@
1721:   DMPlexGetConeRecursiveVertices - Expand each given point into its cone points and do that recursively until we end up just with vertices.

1723:   Not collective

1725:   Input Parameters:
1726: + dm - The DMPlex
1727: - points - The IS of points, which must lie in the chart set with DMPlexSetChart()

1729:   Output Parameter:
1730: . expandedPoints - An array of vertices recursively expanded from input points

1732:   Level: advanced

1734:   Notes:
1735:   Like DMPlexGetConeRecursive but returns only the 0-depth IS (i.e. vertices only) and no sections.
1736:   There is no corresponding Restore function, just call ISDestroy() on the returned IS to deallocate.

1738: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexRestoreConeRecursive(), DMPlexGetDepth()
1739: @*/
1740: PetscErrorCode DMPlexGetConeRecursiveVertices(DM dm, IS points, IS *expandedPoints)
1741: {
1742:   IS                  *expandedPointsAll;
1743:   PetscInt            depth;
1744:   PetscErrorCode      ierr;

1750:   DMPlexGetConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);
1751:   *expandedPoints = expandedPointsAll[0];
1752:   PetscObjectReference((PetscObject)expandedPointsAll[0]);
1753:   DMPlexRestoreConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);
1754:   return(0);
1755: }

1757: /*@
1758:   DMPlexGetConeRecursive - Expand each given point into its cone points and do that recursively until we end up just with vertices (DAG points of depth 0, i.e. without cones).

1760:   Not collective

1762:   Input Parameters:
1763: + dm - The DMPlex
1764: - points - The IS of points, which must lie in the chart set with DMPlexSetChart()

1766:   Output Parameter:
1767: + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
1768: . expandedPoints - (optional) An array of index sets with recursively expanded cones
1769: - sections - (optional) An array of sections which describe mappings from points to their cone points

1771:   Level: advanced

1773:   Notes:
1774:   Like DMPlexGetConeTuple() but recursive.

1776:   Array expandedPoints has size equal to depth. Each expandedPoints[d] contains DAG points with maximum depth d, recursively cone-wise expanded from the input points.
1777:   For example, for d=0 it contains only vertices, for d=1 it can contain vertices and edges, etc.

1779:   Array section has size equal to depth.  Each PetscSection sections[d] realizes mapping from expandedPoints[d+1] (section points) to expandedPoints[d] (section dofs) as follows:
1780:   (1) DAG points in expandedPoints[d+1] with depth d+1 to their cone points in expandedPoints[d];
1781:   (2) DAG points in expandedPoints[d+1] with depth in [0,d] to the same points in expandedPoints[d].

1783: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexRestoreConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
1784: @*/
1785: PetscErrorCode DMPlexGetConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
1786: {
1787:   const PetscInt      *arr0=NULL, *cone=NULL;
1788:   PetscInt            *arr=NULL, *newarr=NULL;
1789:   PetscInt            d, depth_, i, n, newn, cn, co, start, end;
1790:   IS                  *expandedPoints_;
1791:   PetscSection        *sections_;
1792:   PetscErrorCode      ierr;

1800:   ISGetLocalSize(points, &n);
1801:   ISGetIndices(points, &arr0);
1802:   DMPlexGetDepth(dm, &depth_);
1803:   PetscCalloc1(depth_, &expandedPoints_);
1804:   PetscCalloc1(depth_, &sections_);
1805:   arr = (PetscInt*) arr0; /* this is ok because first generation of arr is not modified */
1806:   for (d=depth_-1; d>=0; d--) {
1807:     PetscSectionCreate(PETSC_COMM_SELF, &sections_[d]);
1808:     PetscSectionSetChart(sections_[d], 0, n);
1809:     for (i=0; i<n; i++) {
1810:       DMPlexGetDepthStratum(dm, d+1, &start, &end);
1811:       if (arr[i] >= start && arr[i] < end) {
1812:         DMPlexGetConeSize(dm, arr[i], &cn);
1813:         PetscSectionSetDof(sections_[d], i, cn);
1814:       } else {
1815:         PetscSectionSetDof(sections_[d], i, 1);
1816:       }
1817:     }
1818:     PetscSectionSetUp(sections_[d]);
1819:     PetscSectionGetStorageSize(sections_[d], &newn);
1820:     PetscMalloc1(newn, &newarr);
1821:     for (i=0; i<n; i++) {
1822:       PetscSectionGetDof(sections_[d], i, &cn);
1823:       PetscSectionGetOffset(sections_[d], i, &co);
1824:       if (cn > 1) {
1825:         DMPlexGetCone(dm, arr[i], &cone);
1826:         PetscMemcpy(&newarr[co], cone, cn*sizeof(PetscInt));
1827:       } else {
1828:         newarr[co] = arr[i];
1829:       }
1830:     }
1831:     ISCreateGeneral(PETSC_COMM_SELF, newn, newarr, PETSC_OWN_POINTER, &expandedPoints_[d]);
1832:     arr = newarr;
1833:     n = newn;
1834:   }
1835:   *depth = depth_;
1836:   if (expandedPoints) *expandedPoints = expandedPoints_;
1837:   else {
1838:     for (d=0; d<depth_; d++) {ISDestroy(&expandedPoints_[d]);}
1839:     PetscFree(expandedPoints_);
1840:   }
1841:   if (sections) *sections = sections_;
1842:   else {
1843:     for (d=0; d<depth_; d++) {PetscSectionDestroy(&sections_[d]);}
1844:     PetscFree(sections_);
1845:   }
1846:   return(0);
1847: }

1849: /*@
1850:   DMPlexRestoreConeRecursive - Deallocates arrays created by DMPlexGetConeRecursive

1852:   Not collective

1854:   Input Parameters:
1855: + dm - The DMPlex
1856: - points - The IS of points, which must lie in the chart set with DMPlexSetChart()

1858:   Output Parameter:
1859: + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
1860: . expandedPoints - (optional) An array of recursively expanded cones
1861: - sections - (optional) An array of sections which describe mappings from points to their cone points

1863:   Level: advanced

1865:   Notes:
1866:   See DMPlexGetConeRecursive() for details.

1868: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
1869: @*/
1870: PetscErrorCode DMPlexRestoreConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
1871: {
1872:   PetscInt            d, depth_;
1873:   PetscErrorCode      ierr;

1876:   DMPlexGetDepth(dm, &depth_);
1877:   if (depth && *depth != depth_) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "depth changed since last call to DMPlexGetConeRecursive");
1878:   if (depth) *depth = 0;
1879:   if (expandedPoints) {
1880:     for (d=0; d<depth_; d++) {ISDestroy(&((*expandedPoints)[d]));}
1881:     PetscFree(*expandedPoints);
1882:   }
1883:   if (sections)  {
1884:     for (d=0; d<depth_; d++) {PetscSectionDestroy(&((*sections)[d]));}
1885:     PetscFree(*sections);
1886:   }
1887:   return(0);
1888: }

1890: /*@
1891:   DMPlexSetCone - Set the points on the in-edges for this point in the DAG; that is these are the points that cover the specific point

1893:   Not collective

1895:   Input Parameters:
1896: + mesh - The DMPlex
1897: . p - The point, which must lie in the chart set with DMPlexSetChart()
1898: - cone - An array of points which are on the in-edges for point p

1900:   Output Parameter:

1902:   Note:
1903:   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().

1905:   Developer Note: Why not call this DMPlexSetCover()

1907:   Level: beginner

1909: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp(), DMPlexSetSupport(), DMPlexSetSupportSize()
1910: @*/
1911: PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1912: {
1913:   DM_Plex       *mesh = (DM_Plex*) dm->data;
1914:   PetscInt       pStart, pEnd;
1915:   PetscInt       dof, off, c;

1920:   PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
1921:   PetscSectionGetDof(mesh->coneSection, p, &dof);
1923:   PetscSectionGetOffset(mesh->coneSection, p, &off);
1924:   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
1925:   for (c = 0; c < dof; ++c) {
1926:     if ((cone[c] < pStart) || (cone[c] >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone point %D is not in the valid range [%D, %D)", cone[c], pStart, pEnd);
1927:     mesh->cones[off+c] = cone[c];
1928:   }
1929:   return(0);
1930: }

1932: /*@C
1933:   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG

1935:   Not collective

1937:   Input Parameters:
1938: + mesh - The DMPlex
1939: - p - The point, which must lie in the chart set with DMPlexSetChart()

1941:   Output Parameter:
1942: . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1943:                     integer giving the prescription for cone traversal. If it is negative, the cone is
1944:                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1945:                     the index of the cone point on which to start.

1947:   Level: beginner

1949:   Fortran Notes:
1950:   Since it returns an array, this routine is only available in Fortran 90, and you must
1951:   include petsc.h90 in your code.
1952:   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1953:   DMPlexRestoreConeOrientation() is not needed/available in C.

1955: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1956: @*/
1957: PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1958: {
1959:   DM_Plex       *mesh = (DM_Plex*) dm->data;
1960:   PetscInt       off;

1965: #if defined(PETSC_USE_DEBUG)
1966:   {
1967:     PetscInt dof;
1968:     PetscSectionGetDof(mesh->coneSection, p, &dof);
1970:   }
1971: #endif
1972:   PetscSectionGetOffset(mesh->coneSection, p, &off);

1974:   *coneOrientation = &mesh->coneOrientations[off];
1975:   return(0);
1976: }

1978: /*@
1979:   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG

1981:   Not collective

1983:   Input Parameters:
1984: + mesh - The DMPlex
1985: . p - The point, which must lie in the chart set with DMPlexSetChart()
1986: - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1987:                     integer giving the prescription for cone traversal. If it is negative, the cone is
1988:                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1989:                     the index of the cone point on which to start.

1991:   Output Parameter:

1993:   Note:
1994:   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().

1996:   Level: beginner

1998: .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1999: @*/
2000: PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
2001: {
2002:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2003:   PetscInt       pStart, pEnd;
2004:   PetscInt       dof, off, c;

2009:   PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
2010:   PetscSectionGetDof(mesh->coneSection, p, &dof);
2012:   PetscSectionGetOffset(mesh->coneSection, p, &off);
2013:   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
2014:   for (c = 0; c < dof; ++c) {
2015:     PetscInt cdof, o = coneOrientation[c];

2017:     PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);
2018:     if (o && ((o < -(cdof+1)) || (o >= cdof))) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone orientation %D is not in the valid range [%D. %D)", o, -(cdof+1), cdof);
2019:     mesh->coneOrientations[off+c] = o;
2020:   }
2021:   return(0);
2022: }

2024: /*@
2025:   DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG

2027:   Not collective

2029:   Input Parameters:
2030: + mesh - The DMPlex
2031: . p - The point, which must lie in the chart set with DMPlexSetChart()
2032: . conePos - The local index in the cone where the point should be put
2033: - conePoint - The mesh point to insert

2035:   Level: beginner

2037: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
2038: @*/
2039: PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
2040: {
2041:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2042:   PetscInt       pStart, pEnd;
2043:   PetscInt       dof, off;

2048:   PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
2049:   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
2050:   if ((conePoint < pStart) || (conePoint >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone point %D is not in the valid range [%D, %D)", conePoint, pStart, pEnd);
2051:   PetscSectionGetDof(mesh->coneSection, p, &dof);
2052:   PetscSectionGetOffset(mesh->coneSection, p, &off);
2053:   if ((conePos < 0) || (conePos >= dof)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone position %D of point %D is not in the valid range [0, %D)", conePos, p, dof);
2054:   mesh->cones[off+conePos] = conePoint;
2055:   return(0);
2056: }

2058: /*@
2059:   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG

2061:   Not collective

2063:   Input Parameters:
2064: + mesh - The DMPlex
2065: . p - The point, which must lie in the chart set with DMPlexSetChart()
2066: . conePos - The local index in the cone where the point should be put
2067: - coneOrientation - The point orientation to insert

2069:   Level: beginner

2071: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
2072: @*/
2073: PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
2074: {
2075:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2076:   PetscInt       pStart, pEnd;
2077:   PetscInt       dof, off;

2082:   PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
2083:   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
2084:   PetscSectionGetDof(mesh->coneSection, p, &dof);
2085:   PetscSectionGetOffset(mesh->coneSection, p, &off);
2086:   if ((conePos < 0) || (conePos >= dof)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone position %D of point %D is not in the valid range [0, %D)", conePos, p, dof);
2087:   mesh->coneOrientations[off+conePos] = coneOrientation;
2088:   return(0);
2089: }

2091: /*@
2092:   DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG

2094:   Not collective

2096:   Input Parameters:
2097: + mesh - The DMPlex
2098: - p - The point, which must lie in the chart set with DMPlexSetChart()

2100:   Output Parameter:
2101: . size - The support size for point p

2103:   Level: beginner

2105: .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
2106: @*/
2107: PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
2108: {
2109:   DM_Plex       *mesh = (DM_Plex*) dm->data;

2115:   PetscSectionGetDof(mesh->supportSection, p, size);
2116:   return(0);
2117: }

2119: /*@
2120:   DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG

2122:   Not collective

2124:   Input Parameters:
2125: + mesh - The DMPlex
2126: . p - The point, which must lie in the chart set with DMPlexSetChart()
2127: - size - The support size for point p

2129:   Output Parameter:

2131:   Note:
2132:   This should be called after DMPlexSetChart().

2134:   Level: beginner

2136: .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
2137: @*/
2138: PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
2139: {
2140:   DM_Plex       *mesh = (DM_Plex*) dm->data;

2145:   PetscSectionSetDof(mesh->supportSection, p, size);

2147:   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
2148:   return(0);
2149: }

2151: /*@C
2152:   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG

2154:   Not collective

2156:   Input Parameters:
2157: + mesh - The DMPlex
2158: - p - The point, which must lie in the chart set with DMPlexSetChart()

2160:   Output Parameter:
2161: . support - An array of points which are on the out-edges for point p

2163:   Level: beginner

2165:   Fortran Notes:
2166:   Since it returns an array, this routine is only available in Fortran 90, and you must
2167:   include petsc.h90 in your code.
2168:   You must also call DMPlexRestoreSupport() after you finish using the returned array.
2169:   DMPlexRestoreSupport() is not needed/available in C.

2171: .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2172: @*/
2173: PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
2174: {
2175:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2176:   PetscInt       off;

2182:   PetscSectionGetOffset(mesh->supportSection, p, &off);
2183:   *support = &mesh->supports[off];
2184:   return(0);
2185: }

2187: /*@
2188:   DMPlexSetSupport - Set the points on the out-edges for this point in the DAG, that is the list of points that this point covers

2190:   Not collective

2192:   Input Parameters:
2193: + mesh - The DMPlex
2194: . p - The point, which must lie in the chart set with DMPlexSetChart()
2195: - support - An array of points which are on the out-edges for point p

2197:   Output Parameter:

2199:   Note:
2200:   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().

2202:   Level: beginner

2204: .seealso: DMPlexSetCone(), DMPlexSetConeSize(), DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
2205: @*/
2206: PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
2207: {
2208:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2209:   PetscInt       pStart, pEnd;
2210:   PetscInt       dof, off, c;

2215:   PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);
2216:   PetscSectionGetDof(mesh->supportSection, p, &dof);
2218:   PetscSectionGetOffset(mesh->supportSection, p, &off);
2219:   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
2220:   for (c = 0; c < dof; ++c) {
2221:     if ((support[c] < pStart) || (support[c] >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Support point %D is not in the valid range [%D, %D)", support[c], pStart, pEnd);
2222:     mesh->supports[off+c] = support[c];
2223:   }
2224:   return(0);
2225: }

2227: /*@
2228:   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG

2230:   Not collective

2232:   Input Parameters:
2233: + mesh - The DMPlex
2234: . p - The point, which must lie in the chart set with DMPlexSetChart()
2235: . supportPos - The local index in the cone where the point should be put
2236: - supportPoint - The mesh point to insert

2238:   Level: beginner

2240: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
2241: @*/
2242: PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
2243: {
2244:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2245:   PetscInt       pStart, pEnd;
2246:   PetscInt       dof, off;

2251:   PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);
2252:   PetscSectionGetDof(mesh->supportSection, p, &dof);
2253:   PetscSectionGetOffset(mesh->supportSection, p, &off);
2254:   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
2255:   if ((supportPoint < pStart) || (supportPoint >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Support point %D is not in the valid range [%D, %D)", supportPoint, pStart, pEnd);
2256:   if (supportPos >= dof) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Support position %D of point %D is not in the valid range [0, %D)", supportPos, p, dof);
2257:   mesh->supports[off+supportPos] = supportPoint;
2258:   return(0);
2259: }

2261: /*@C
2262:   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG

2264:   Not collective

2266:   Input Parameters:
2267: + mesh - The DMPlex
2268: . p - The point, which must lie in the chart set with DMPlexSetChart()
2269: . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2270: - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used

2272:   Output Parameters:
2273: + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
2274: - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]

2276:   Note:
2277:   If using internal storage (points is NULL on input), each call overwrites the last output.

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

2283:   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.

2285:   Level: beginner

2287: .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2288: @*/
2289: PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2290: {
2291:   DM_Plex        *mesh = (DM_Plex*) dm->data;
2292:   PetscInt       *closure, *fifo;
2293:   const PetscInt *tmp = NULL, *tmpO = NULL;
2294:   PetscInt        tmpSize, t;
2295:   PetscInt        depth       = 0, maxSize;
2296:   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
2297:   PetscErrorCode  ierr;

2301:   DMPlexGetDepth(dm, &depth);
2302:   /* This is only 1-level */
2303:   if (useCone) {
2304:     DMPlexGetConeSize(dm, p, &tmpSize);
2305:     DMPlexGetCone(dm, p, &tmp);
2306:     DMPlexGetConeOrientation(dm, p, &tmpO);
2307:   } else {
2308:     DMPlexGetSupportSize(dm, p, &tmpSize);
2309:     DMPlexGetSupport(dm, p, &tmp);
2310:   }
2311:   if (depth == 1) {
2312:     if (*points) {
2313:       closure = *points;
2314:     } else {
2315:       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
2316:       DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);
2317:     }
2318:     closure[0] = p; closure[1] = 0;
2319:     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
2320:       closure[closureSize]   = tmp[t];
2321:       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
2322:     }
2323:     if (numPoints) *numPoints = closureSize/2;
2324:     if (points)    *points    = closure;
2325:     return(0);
2326:   }
2327:   {
2328:     PetscInt c, coneSeries, s,supportSeries;

2330:     c = mesh->maxConeSize;
2331:     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
2332:     s = mesh->maxSupportSize;
2333:     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
2334:     maxSize = 2*PetscMax(coneSeries,supportSeries);
2335:   }
2336:   DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);
2337:   if (*points) {
2338:     closure = *points;
2339:   } else {
2340:     DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);
2341:   }
2342:   closure[0] = p; closure[1] = 0;
2343:   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
2344:     const PetscInt cp = tmp[t];
2345:     const PetscInt co = tmpO ? tmpO[t] : 0;

2347:     closure[closureSize]   = cp;
2348:     closure[closureSize+1] = co;
2349:     fifo[fifoSize]         = cp;
2350:     fifo[fifoSize+1]       = co;
2351:   }
2352:   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
2353:   while (fifoSize - fifoStart) {
2354:     const PetscInt q   = fifo[fifoStart];
2355:     const PetscInt o   = fifo[fifoStart+1];
2356:     const PetscInt rev = o >= 0 ? 0 : 1;
2357:     const PetscInt off = rev ? -(o+1) : o;

2359:     if (useCone) {
2360:       DMPlexGetConeSize(dm, q, &tmpSize);
2361:       DMPlexGetCone(dm, q, &tmp);
2362:       DMPlexGetConeOrientation(dm, q, &tmpO);
2363:     } else {
2364:       DMPlexGetSupportSize(dm, q, &tmpSize);
2365:       DMPlexGetSupport(dm, q, &tmp);
2366:       tmpO = NULL;
2367:     }
2368:     for (t = 0; t < tmpSize; ++t) {
2369:       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
2370:       const PetscInt cp = tmp[i];
2371:       /* Must propogate orientation: When we reverse orientation, we both reverse the direction of iteration and start at the other end of the chain. */
2372:       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
2373:        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
2374:       PetscInt       co = tmpO ? tmpO[i] : 0;
2375:       PetscInt       c;

2377:       if (rev) {
2378:         PetscInt childSize, coff;
2379:         DMPlexGetConeSize(dm, cp, &childSize);
2380:         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
2381:         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
2382:       }
2383:       /* Check for duplicate */
2384:       for (c = 0; c < closureSize; c += 2) {
2385:         if (closure[c] == cp) break;
2386:       }
2387:       if (c == closureSize) {
2388:         closure[closureSize]   = cp;
2389:         closure[closureSize+1] = co;
2390:         fifo[fifoSize]         = cp;
2391:         fifo[fifoSize+1]       = co;
2392:         closureSize           += 2;
2393:         fifoSize              += 2;
2394:       }
2395:     }
2396:     fifoStart += 2;
2397:   }
2398:   if (numPoints) *numPoints = closureSize/2;
2399:   if (points)    *points    = closure;
2400:   DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);
2401:   return(0);
2402: }

2404: /*@C
2405:   DMPlexGetTransitiveClosure_Internal - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG with a specified initial orientation

2407:   Not collective

2409:   Input Parameters:
2410: + mesh - The DMPlex
2411: . p - The point, which must lie in the chart set with DMPlexSetChart()
2412: . orientation - The orientation of the point
2413: . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2414: - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used

2416:   Output Parameters:
2417: + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
2418: - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]

2420:   Note:
2421:   If using internal storage (points is NULL on input), each call overwrites the last output.

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

2427:   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.

2429:   Level: beginner

2431: .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2432: @*/
2433: PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2434: {
2435:   DM_Plex        *mesh = (DM_Plex*) dm->data;
2436:   PetscInt       *closure, *fifo;
2437:   const PetscInt *tmp = NULL, *tmpO = NULL;
2438:   PetscInt        tmpSize, t;
2439:   PetscInt        depth       = 0, maxSize;
2440:   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
2441:   PetscErrorCode  ierr;

2445:   DMPlexGetDepth(dm, &depth);
2446:   /* This is only 1-level */
2447:   if (useCone) {
2448:     DMPlexGetConeSize(dm, p, &tmpSize);
2449:     DMPlexGetCone(dm, p, &tmp);
2450:     DMPlexGetConeOrientation(dm, p, &tmpO);
2451:   } else {
2452:     DMPlexGetSupportSize(dm, p, &tmpSize);
2453:     DMPlexGetSupport(dm, p, &tmp);
2454:   }
2455:   if (depth == 1) {
2456:     if (*points) {
2457:       closure = *points;
2458:     } else {
2459:       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
2460:       DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);
2461:     }
2462:     closure[0] = p; closure[1] = ornt;
2463:     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
2464:       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
2465:       closure[closureSize]   = tmp[i];
2466:       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
2467:     }
2468:     if (numPoints) *numPoints = closureSize/2;
2469:     if (points)    *points    = closure;
2470:     return(0);
2471:   }
2472:   {
2473:     PetscInt c, coneSeries, s,supportSeries;

2475:     c = mesh->maxConeSize;
2476:     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
2477:     s = mesh->maxSupportSize;
2478:     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
2479:     maxSize = 2*PetscMax(coneSeries,supportSeries);
2480:   }
2481:   DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);
2482:   if (*points) {
2483:     closure = *points;
2484:   } else {
2485:     DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);
2486:   }
2487:   closure[0] = p; closure[1] = ornt;
2488:   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
2489:     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
2490:     const PetscInt cp = tmp[i];
2491:     PetscInt       co = tmpO ? tmpO[i] : 0;

2493:     if (ornt < 0) {
2494:       PetscInt childSize, coff;
2495:       DMPlexGetConeSize(dm, cp, &childSize);
2496:       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
2497:       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
2498:     }
2499:     closure[closureSize]   = cp;
2500:     closure[closureSize+1] = co;
2501:     fifo[fifoSize]         = cp;
2502:     fifo[fifoSize+1]       = co;
2503:   }
2504:   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
2505:   while (fifoSize - fifoStart) {
2506:     const PetscInt q   = fifo[fifoStart];
2507:     const PetscInt o   = fifo[fifoStart+1];
2508:     const PetscInt rev = o >= 0 ? 0 : 1;
2509:     const PetscInt off = rev ? -(o+1) : o;

2511:     if (useCone) {
2512:       DMPlexGetConeSize(dm, q, &tmpSize);
2513:       DMPlexGetCone(dm, q, &tmp);
2514:       DMPlexGetConeOrientation(dm, q, &tmpO);
2515:     } else {
2516:       DMPlexGetSupportSize(dm, q, &tmpSize);
2517:       DMPlexGetSupport(dm, q, &tmp);
2518:       tmpO = NULL;
2519:     }
2520:     for (t = 0; t < tmpSize; ++t) {
2521:       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
2522:       const PetscInt cp = tmp[i];
2523:       /* Must propogate orientation: When we reverse orientation, we both reverse the direction of iteration and start at the other end of the chain. */
2524:       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
2525:        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
2526:       PetscInt       co = tmpO ? tmpO[i] : 0;
2527:       PetscInt       c;

2529:       if (rev) {
2530:         PetscInt childSize, coff;
2531:         DMPlexGetConeSize(dm, cp, &childSize);
2532:         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
2533:         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
2534:       }
2535:       /* Check for duplicate */
2536:       for (c = 0; c < closureSize; c += 2) {
2537:         if (closure[c] == cp) break;
2538:       }
2539:       if (c == closureSize) {
2540:         closure[closureSize]   = cp;
2541:         closure[closureSize+1] = co;
2542:         fifo[fifoSize]         = cp;
2543:         fifo[fifoSize+1]       = co;
2544:         closureSize           += 2;
2545:         fifoSize              += 2;
2546:       }
2547:     }
2548:     fifoStart += 2;
2549:   }
2550:   if (numPoints) *numPoints = closureSize/2;
2551:   if (points)    *points    = closure;
2552:   DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);
2553:   return(0);
2554: }

2556: /*@C
2557:   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG

2559:   Not collective

2561:   Input Parameters:
2562: + mesh - The DMPlex
2563: . p - The point, which must lie in the chart set with DMPlexSetChart()
2564: . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2565: . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
2566: - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit

2568:   Note:
2569:   If not using internal storage (points is not NULL on input), this call is unnecessary

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

2575:   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.

2577:   Level: beginner

2579: .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2580: @*/
2581: PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2582: {

2589:   DMRestoreWorkArray(dm, 0, MPIU_INT, points);
2590:   if (numPoints) *numPoints = 0;
2591:   return(0);
2592: }

2594: /*@
2595:   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG

2597:   Not collective

2599:   Input Parameter:
2600: . mesh - The DMPlex

2602:   Output Parameters:
2603: + maxConeSize - The maximum number of in-edges
2604: - maxSupportSize - The maximum number of out-edges

2606:   Level: beginner

2608: .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2609: @*/
2610: PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
2611: {
2612:   DM_Plex *mesh = (DM_Plex*) dm->data;

2616:   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
2617:   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
2618:   return(0);
2619: }

2621: PetscErrorCode DMSetUp_Plex(DM dm)
2622: {
2623:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2624:   PetscInt       size;

2629:   PetscSectionSetUp(mesh->coneSection);
2630:   PetscSectionGetStorageSize(mesh->coneSection, &size);
2631:   PetscMalloc1(size, &mesh->cones);
2632:   PetscCalloc1(size, &mesh->coneOrientations);
2633:   if (mesh->maxSupportSize) {
2634:     PetscSectionSetUp(mesh->supportSection);
2635:     PetscSectionGetStorageSize(mesh->supportSection, &size);
2636:     PetscMalloc1(size, &mesh->supports);
2637:   }
2638:   return(0);
2639: }

2641: PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2642: {

2646:   if (subdm) {DMClone(dm, subdm);}
2647:   DMCreateSectionSubDM(dm, numFields, fields, is, subdm);
2648:   if (subdm) {(*subdm)->useNatural = dm->useNatural;}
2649:   if (dm->useNatural && dm->sfMigration) {
2650:     PetscSF        sfMigrationInv,sfNatural;
2651:     PetscSection   section, sectionSeq;

2653:     (*subdm)->sfMigration = dm->sfMigration;
2654:     PetscObjectReference((PetscObject) dm->sfMigration);
2655:     DMGetLocalSection((*subdm), &section);
2656:     PetscSFCreateInverseSF((*subdm)->sfMigration, &sfMigrationInv);
2657:     PetscSectionCreate(PetscObjectComm((PetscObject) (*subdm)), &sectionSeq);
2658:     PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);

2660:     DMPlexCreateGlobalToNaturalSF(*subdm, sectionSeq, (*subdm)->sfMigration, &sfNatural);
2661:     (*subdm)->sfNatural = sfNatural;
2662:     PetscSectionDestroy(&sectionSeq);
2663:     PetscSFDestroy(&sfMigrationInv);
2664:   }
2665:   return(0);
2666: }

2668: PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
2669: {
2671:   PetscInt       i = 0;

2674:   DMClone(dms[0], superdm);
2675:   DMCreateSectionSuperDM(dms, len, is, superdm);
2676:   (*superdm)->useNatural = PETSC_FALSE;
2677:   for (i = 0; i < len; i++){
2678:     if (dms[i]->useNatural && dms[i]->sfMigration) {
2679:       PetscSF        sfMigrationInv,sfNatural;
2680:       PetscSection   section, sectionSeq;

2682:       (*superdm)->sfMigration = dms[i]->sfMigration;
2683:       PetscObjectReference((PetscObject) dms[i]->sfMigration);
2684:       (*superdm)->useNatural = PETSC_TRUE;
2685:       DMGetLocalSection((*superdm), &section);
2686:       PetscSFCreateInverseSF((*superdm)->sfMigration, &sfMigrationInv);
2687:       PetscSectionCreate(PetscObjectComm((PetscObject) (*superdm)), &sectionSeq);
2688:       PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);

2690:       DMPlexCreateGlobalToNaturalSF(*superdm, sectionSeq, (*superdm)->sfMigration, &sfNatural);
2691:       (*superdm)->sfNatural = sfNatural;
2692:       PetscSectionDestroy(&sectionSeq);
2693:       PetscSFDestroy(&sfMigrationInv);
2694:       break;
2695:     }
2696:   }
2697:   return(0);
2698: }

2700: /*@
2701:   DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information

2703:   Not collective

2705:   Input Parameter:
2706: . mesh - The DMPlex

2708:   Output Parameter:

2710:   Note:
2711:   This should be called after all calls to DMPlexSetCone()

2713:   Level: beginner

2715: .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2716: @*/
2717: PetscErrorCode DMPlexSymmetrize(DM dm)
2718: {
2719:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2720:   PetscInt      *offsets;
2721:   PetscInt       supportSize;
2722:   PetscInt       pStart, pEnd, p;

2727:   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
2728:   PetscLogEventBegin(DMPLEX_Symmetrize,dm,0,0,0);
2729:   /* Calculate support sizes */
2730:   DMPlexGetChart(dm, &pStart, &pEnd);
2731:   for (p = pStart; p < pEnd; ++p) {
2732:     PetscInt dof, off, c;

2734:     PetscSectionGetDof(mesh->coneSection, p, &dof);
2735:     PetscSectionGetOffset(mesh->coneSection, p, &off);
2736:     for (c = off; c < off+dof; ++c) {
2737:       PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);
2738:     }
2739:   }
2740:   for (p = pStart; p < pEnd; ++p) {
2741:     PetscInt dof;

2743:     PetscSectionGetDof(mesh->supportSection, p, &dof);

2745:     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2746:   }
2747:   PetscSectionSetUp(mesh->supportSection);
2748:   /* Calculate supports */
2749:   PetscSectionGetStorageSize(mesh->supportSection, &supportSize);
2750:   PetscMalloc1(supportSize, &mesh->supports);
2751:   PetscCalloc1(pEnd - pStart, &offsets);
2752:   for (p = pStart; p < pEnd; ++p) {
2753:     PetscInt dof, off, c;

2755:     PetscSectionGetDof(mesh->coneSection, p, &dof);
2756:     PetscSectionGetOffset(mesh->coneSection, p, &off);
2757:     for (c = off; c < off+dof; ++c) {
2758:       const PetscInt q = mesh->cones[c];
2759:       PetscInt       offS;

2761:       PetscSectionGetOffset(mesh->supportSection, q, &offS);

2763:       mesh->supports[offS+offsets[q]] = p;
2764:       ++offsets[q];
2765:     }
2766:   }
2767:   PetscFree(offsets);
2768:   PetscLogEventEnd(DMPLEX_Symmetrize,dm,0,0,0);
2769:   return(0);
2770: }

2772: static PetscErrorCode DMPlexCreateDepthStratum(DM dm, DMLabel label, PetscInt depth, PetscInt pStart, PetscInt pEnd)
2773: {
2774:   IS             stratumIS;

2778:   if (pStart >= pEnd) return(0);
2779: #if defined(PETSC_USE_DEBUG)
2780:   {
2781:     PetscInt  qStart, qEnd, numLevels, level;
2782:     PetscBool overlap = PETSC_FALSE;
2783:     DMLabelGetNumValues(label, &numLevels);
2784:     for (level = 0; level < numLevels; level++) {
2785:       DMLabelGetStratumBounds(label, level, &qStart, &qEnd);
2786:       if ((pStart >= qStart && pStart < qEnd) || (pEnd > qStart && pEnd <= qEnd)) {overlap = PETSC_TRUE; break;}
2787:     }
2788:     if (overlap) SETERRQ6(PETSC_COMM_SELF, PETSC_ERR_PLIB, "New depth %D range [%D,%D) overlaps with depth %D range [%D,%D)", depth, pStart, pEnd, level, qStart, qEnd);
2789:   }
2790: #endif
2791:   ISCreateStride(PETSC_COMM_SELF, pEnd-pStart, pStart, 1, &stratumIS);
2792:   DMLabelSetStratumIS(label, depth, stratumIS);
2793:   ISDestroy(&stratumIS);
2794:   return(0);
2795: }

2797: static PetscErrorCode DMPlexCreateDimStratum(DM,DMLabel,DMLabel,PetscInt,PetscInt);

2799: /*@
2800:   DMPlexStratify - The DAG for most topologies is a graded poset (https://en.wikipedia.org/wiki/Graded_poset), and
2801:   can be illustrated by a Hasse Diagram (https://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2802:   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2803:   the DAG.

2805:   Collective on dm

2807:   Input Parameter:
2808: . mesh - The DMPlex

2810:   Output Parameter:

2812:   Notes:
2813:   Concretely, DMPlexStratify() creates a new label named "depth" containing the dimension of each element: 0 for vertices,
2814:   1 for edges, and so on.  The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2815:   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2816:   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.

2818:   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()

2820:   Level: beginner

2822: .seealso: DMPlexCreate(), DMPlexSymmetrize()
2823: @*/
2824: PetscErrorCode DMPlexStratify(DM dm)
2825: {
2826:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2827:   DMLabel        label;
2828:   PetscInt       pStart, pEnd, p;
2829:   PetscInt       numRoots = 0, numLeaves = 0;
2830:   PetscInt       cMax, fMax, eMax, vMax;

2835:   PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);

2837:   /* Create depth label */
2838:   DMPlexGetChart(dm, &pStart, &pEnd);
2839:   DMCreateLabel(dm, "depth");
2840:   DMPlexGetDepthLabel(dm, &label);

2842:   {
2843:     /* Initialize roots and count leaves */
2844:     PetscInt sMin = PETSC_MAX_INT;
2845:     PetscInt sMax = PETSC_MIN_INT;
2846:     PetscInt coneSize, supportSize;

2848:     for (p = pStart; p < pEnd; ++p) {
2849:       DMPlexGetConeSize(dm, p, &coneSize);
2850:       DMPlexGetSupportSize(dm, p, &supportSize);
2851:       if (!coneSize && supportSize) {
2852:         sMin = PetscMin(p, sMin);
2853:         sMax = PetscMax(p, sMax);
2854:         ++numRoots;
2855:       } else if (!supportSize && coneSize) {
2856:         ++numLeaves;
2857:       } else if (!supportSize && !coneSize) {
2858:         /* Isolated points */
2859:         sMin = PetscMin(p, sMin);
2860:         sMax = PetscMax(p, sMax);
2861:       }
2862:     }
2863:     DMPlexCreateDepthStratum(dm, label, 0, sMin, sMax+1);
2864:   }

2866:   if (numRoots + numLeaves == (pEnd - pStart)) {
2867:     PetscInt sMin = PETSC_MAX_INT;
2868:     PetscInt sMax = PETSC_MIN_INT;
2869:     PetscInt coneSize, supportSize;

2871:     for (p = pStart; p < pEnd; ++p) {
2872:       DMPlexGetConeSize(dm, p, &coneSize);
2873:       DMPlexGetSupportSize(dm, p, &supportSize);
2874:       if (!supportSize && coneSize) {
2875:         sMin = PetscMin(p, sMin);
2876:         sMax = PetscMax(p, sMax);
2877:       }
2878:     }
2879:     DMPlexCreateDepthStratum(dm, label, 1, sMin, sMax+1);
2880:   } else {
2881:     PetscInt level = 0;
2882:     PetscInt qStart, qEnd, q;

2884:     DMLabelGetStratumBounds(label, level, &qStart, &qEnd);
2885:     while (qEnd > qStart) {
2886:       PetscInt sMin = PETSC_MAX_INT;
2887:       PetscInt sMax = PETSC_MIN_INT;

2889:       for (q = qStart; q < qEnd; ++q) {
2890:         const PetscInt *support;
2891:         PetscInt        supportSize, s;

2893:         DMPlexGetSupportSize(dm, q, &supportSize);
2894:         DMPlexGetSupport(dm, q, &support);
2895:         for (s = 0; s < supportSize; ++s) {
2896:           sMin = PetscMin(support[s], sMin);
2897:           sMax = PetscMax(support[s], sMax);
2898:         }
2899:       }
2900:       DMLabelGetNumValues(label, &level);
2901:       DMPlexCreateDepthStratum(dm, label, level, sMin, sMax+1);
2902:       DMLabelGetStratumBounds(label, level, &qStart, &qEnd);
2903:     }
2904:   }
2905:   { /* just in case there is an empty process */
2906:     PetscInt numValues, maxValues = 0, v;

2908:     DMLabelGetNumValues(label, &numValues);
2909:     MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));
2910:     for (v = numValues; v < maxValues; v++) {
2911:       DMLabelAddStratum(label, v);
2912:     }
2913:   }
2914:   PetscObjectStateGet((PetscObject) label, &mesh->depthState);

2916:   DMPlexGetHybridBounds(dm, &cMax, &fMax, &eMax, &vMax);
2917:   if (cMax >= 0 || fMax >= 0 || eMax >= 0 || vMax >= 0) {
2918:     PetscInt dim;
2919:     DMLabel  dimLabel;

2921:     DMGetDimension(dm, &dim);
2922:     DMCreateLabel(dm, "dim");
2923:     DMGetLabel(dm, "dim", &dimLabel);
2924:     if (cMax >= 0) {DMPlexCreateDimStratum(dm, label, dimLabel, dim, cMax);}
2925:     if (fMax >= 0) {DMPlexCreateDimStratum(dm, label, dimLabel, dim - 1, fMax);}
2926:     if (eMax >= 0) {DMPlexCreateDimStratum(dm, label, dimLabel, 1, eMax);}
2927:     if (vMax >= 0) {DMPlexCreateDimStratum(dm, label, dimLabel, 0, vMax);}
2928:   }
2929:   PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);
2930:   return(0);
2931: }

2933: /*@C
2934:   DMPlexGetJoin - Get an array for the join of the set of points

2936:   Not Collective

2938:   Input Parameters:
2939: + dm - The DMPlex object
2940: . numPoints - The number of input points for the join
2941: - points - The input points

2943:   Output Parameters:
2944: + numCoveredPoints - The number of points in the join
2945: - coveredPoints - The points in the join

2947:   Level: intermediate

2949:   Note: Currently, this is restricted to a single level join

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

2955:   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.

2957: .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
2958: @*/
2959: PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2960: {
2961:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2962:   PetscInt      *join[2];
2963:   PetscInt       joinSize, i = 0;
2964:   PetscInt       dof, off, p, c, m;

2972:   DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[0]);
2973:   DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1]);
2974:   /* Copy in support of first point */
2975:   PetscSectionGetDof(mesh->supportSection, points[0], &dof);
2976:   PetscSectionGetOffset(mesh->supportSection, points[0], &off);
2977:   for (joinSize = 0; joinSize < dof; ++joinSize) {
2978:     join[i][joinSize] = mesh->supports[off+joinSize];
2979:   }
2980:   /* Check each successive support */
2981:   for (p = 1; p < numPoints; ++p) {
2982:     PetscInt newJoinSize = 0;

2984:     PetscSectionGetDof(mesh->supportSection, points[p], &dof);
2985:     PetscSectionGetOffset(mesh->supportSection, points[p], &off);
2986:     for (c = 0; c < dof; ++c) {
2987:       const PetscInt point = mesh->supports[off+c];

2989:       for (m = 0; m < joinSize; ++m) {
2990:         if (point == join[i][m]) {
2991:           join[1-i][newJoinSize++] = point;
2992:           break;
2993:         }
2994:       }
2995:     }
2996:     joinSize = newJoinSize;
2997:     i        = 1-i;
2998:   }
2999:   *numCoveredPoints = joinSize;
3000:   *coveredPoints    = join[i];
3001:   DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);
3002:   return(0);
3003: }

3005: /*@C
3006:   DMPlexRestoreJoin - Restore an array for the join of the set of points

3008:   Not Collective

3010:   Input Parameters:
3011: + dm - The DMPlex object
3012: . numPoints - The number of input points for the join
3013: - points - The input points

3015:   Output Parameters:
3016: + numCoveredPoints - The number of points in the join
3017: - coveredPoints - The points in the join

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

3023:   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.

3025:   Level: intermediate

3027: .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
3028: @*/
3029: PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3030: {

3038:   DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);
3039:   if (numCoveredPoints) *numCoveredPoints = 0;
3040:   return(0);
3041: }

3043: /*@C
3044:   DMPlexGetFullJoin - Get an array for the join of the set of points

3046:   Not Collective

3048:   Input Parameters:
3049: + dm - The DMPlex object
3050: . numPoints - The number of input points for the join
3051: - points - The input points

3053:   Output Parameters:
3054: + numCoveredPoints - The number of points in the join
3055: - coveredPoints - The points in the join

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

3061:   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.

3063:   Level: intermediate

3065: .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
3066: @*/
3067: PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3068: {
3069:   DM_Plex       *mesh = (DM_Plex*) dm->data;
3070:   PetscInt      *offsets, **closures;
3071:   PetscInt      *join[2];
3072:   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
3073:   PetscInt       p, d, c, m, ms;


3082:   DMPlexGetDepth(dm, &depth);
3083:   PetscCalloc1(numPoints, &closures);
3084:   DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);
3085:   ms      = mesh->maxSupportSize;
3086:   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
3087:   DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);
3088:   DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);

3090:   for (p = 0; p < numPoints; ++p) {
3091:     PetscInt closureSize;

3093:     DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);

3095:     offsets[p*(depth+2)+0] = 0;
3096:     for (d = 0; d < depth+1; ++d) {
3097:       PetscInt pStart, pEnd, i;

3099:       DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);
3100:       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
3101:         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3102:           offsets[p*(depth+2)+d+1] = i;
3103:           break;
3104:         }
3105:       }
3106:       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
3107:     }
3108:     if (offsets[p*(depth+2)+depth+1] != closureSize) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Total size of closure %D should be %D", offsets[p*(depth+2)+depth+1], closureSize);
3109:   }
3110:   for (d = 0; d < depth+1; ++d) {
3111:     PetscInt dof;

3113:     /* Copy in support of first point */
3114:     dof = offsets[d+1] - offsets[d];
3115:     for (joinSize = 0; joinSize < dof; ++joinSize) {
3116:       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
3117:     }
3118:     /* Check each successive cone */
3119:     for (p = 1; p < numPoints && joinSize; ++p) {
3120:       PetscInt newJoinSize = 0;

3122:       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
3123:       for (c = 0; c < dof; ++c) {
3124:         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];

3126:         for (m = 0; m < joinSize; ++m) {
3127:           if (point == join[i][m]) {
3128:             join[1-i][newJoinSize++] = point;
3129:             break;
3130:           }
3131:         }
3132:       }
3133:       joinSize = newJoinSize;
3134:       i        = 1-i;
3135:     }
3136:     if (joinSize) break;
3137:   }
3138:   *numCoveredPoints = joinSize;
3139:   *coveredPoints    = join[i];
3140:   for (p = 0; p < numPoints; ++p) {
3141:     DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);
3142:   }
3143:   PetscFree(closures);
3144:   DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);
3145:   DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);
3146:   return(0);
3147: }

3149: /*@C
3150:   DMPlexGetMeet - Get an array for the meet of the set of points

3152:   Not Collective

3154:   Input Parameters:
3155: + dm - The DMPlex object
3156: . numPoints - The number of input points for the meet
3157: - points - The input points

3159:   Output Parameters:
3160: + numCoveredPoints - The number of points in the meet
3161: - coveredPoints - The points in the meet

3163:   Level: intermediate

3165:   Note: Currently, this is restricted to a single level meet

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

3171:   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.

3173: .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
3174: @*/
3175: PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
3176: {
3177:   DM_Plex       *mesh = (DM_Plex*) dm->data;
3178:   PetscInt      *meet[2];
3179:   PetscInt       meetSize, i = 0;
3180:   PetscInt       dof, off, p, c, m;

3188:   DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[0]);
3189:   DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1]);
3190:   /* Copy in cone of first point */
3191:   PetscSectionGetDof(mesh->coneSection, points[0], &dof);
3192:   PetscSectionGetOffset(mesh->coneSection, points[0], &off);
3193:   for (meetSize = 0; meetSize < dof; ++meetSize) {
3194:     meet[i][meetSize] = mesh->cones[off+meetSize];
3195:   }
3196:   /* Check each successive cone */
3197:   for (p = 1; p < numPoints; ++p) {
3198:     PetscInt newMeetSize = 0;

3200:     PetscSectionGetDof(mesh->coneSection, points[p], &dof);
3201:     PetscSectionGetOffset(mesh->coneSection, points[p], &off);
3202:     for (c = 0; c < dof; ++c) {
3203:       const PetscInt point = mesh->cones[off+c];

3205:       for (m = 0; m < meetSize; ++m) {
3206:         if (point == meet[i][m]) {
3207:           meet[1-i][newMeetSize++] = point;
3208:           break;
3209:         }
3210:       }
3211:     }
3212:     meetSize = newMeetSize;
3213:     i        = 1-i;
3214:   }
3215:   *numCoveringPoints = meetSize;
3216:   *coveringPoints    = meet[i];
3217:   DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);
3218:   return(0);
3219: }

3221: /*@C
3222:   DMPlexRestoreMeet - Restore an array for the meet of the set of points

3224:   Not Collective

3226:   Input Parameters:
3227: + dm - The DMPlex object
3228: . numPoints - The number of input points for the meet
3229: - points - The input points

3231:   Output Parameters:
3232: + numCoveredPoints - The number of points in the meet
3233: - coveredPoints - The points in the meet

3235:   Level: intermediate

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

3241:   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.

3243: .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
3244: @*/
3245: PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3246: {

3254:   DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);
3255:   if (numCoveredPoints) *numCoveredPoints = 0;
3256:   return(0);
3257: }

3259: /*@C
3260:   DMPlexGetFullMeet - Get an array for the meet of the set of points

3262:   Not Collective

3264:   Input Parameters:
3265: + dm - The DMPlex object
3266: . numPoints - The number of input points for the meet
3267: - points - The input points

3269:   Output Parameters:
3270: + numCoveredPoints - The number of points in the meet
3271: - coveredPoints - The points in the meet

3273:   Level: intermediate

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

3279:   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.

3281: .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
3282: @*/
3283: PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3284: {
3285:   DM_Plex       *mesh = (DM_Plex*) dm->data;
3286:   PetscInt      *offsets, **closures;
3287:   PetscInt      *meet[2];
3288:   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
3289:   PetscInt       p, h, c, m, mc;


3298:   DMPlexGetDepth(dm, &height);
3299:   PetscMalloc1(numPoints, &closures);
3300:   DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);
3301:   mc      = mesh->maxConeSize;
3302:   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
3303:   DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);
3304:   DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);

3306:   for (p = 0; p < numPoints; ++p) {
3307:     PetscInt closureSize;

3309:     DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);

3311:     offsets[p*(height+2)+0] = 0;
3312:     for (h = 0; h < height+1; ++h) {
3313:       PetscInt pStart, pEnd, i;

3315:       DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);
3316:       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
3317:         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3318:           offsets[p*(height+2)+h+1] = i;
3319:           break;
3320:         }
3321:       }
3322:       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
3323:     }
3324:     if (offsets[p*(height+2)+height+1] != closureSize) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Total size of closure %D should be %D", offsets[p*(height+2)+height+1], closureSize);
3325:   }
3326:   for (h = 0; h < height+1; ++h) {
3327:     PetscInt dof;

3329:     /* Copy in cone of first point */
3330:     dof = offsets[h+1] - offsets[h];
3331:     for (meetSize = 0; meetSize < dof; ++meetSize) {
3332:       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
3333:     }
3334:     /* Check each successive cone */
3335:     for (p = 1; p < numPoints && meetSize; ++p) {
3336:       PetscInt newMeetSize = 0;

3338:       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
3339:       for (c = 0; c < dof; ++c) {
3340:         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];

3342:         for (m = 0; m < meetSize; ++m) {
3343:           if (point == meet[i][m]) {
3344:             meet[1-i][newMeetSize++] = point;
3345:             break;
3346:           }
3347:         }
3348:       }
3349:       meetSize = newMeetSize;
3350:       i        = 1-i;
3351:     }
3352:     if (meetSize) break;
3353:   }
3354:   *numCoveredPoints = meetSize;
3355:   *coveredPoints    = meet[i];
3356:   for (p = 0; p < numPoints; ++p) {
3357:     DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);
3358:   }
3359:   PetscFree(closures);
3360:   DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);
3361:   DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);
3362:   return(0);
3363: }

3365: /*@C
3366:   DMPlexEqual - Determine if two DMs have the same topology

3368:   Not Collective

3370:   Input Parameters:
3371: + dmA - A DMPlex object
3372: - dmB - A DMPlex object

3374:   Output Parameters:
3375: . equal - PETSC_TRUE if the topologies are identical

3377:   Level: intermediate

3379:   Notes:
3380:   We are not solving graph isomorphism, so we do not permutation.

3382: .seealso: DMPlexGetCone()
3383: @*/
3384: PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
3385: {
3386:   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;


3394:   *equal = PETSC_FALSE;
3395:   DMPlexGetDepth(dmA, &depth);
3396:   DMPlexGetDepth(dmB, &depthB);
3397:   if (depth != depthB) return(0);
3398:   DMPlexGetChart(dmA, &pStart,  &pEnd);
3399:   DMPlexGetChart(dmB, &pStartB, &pEndB);
3400:   if ((pStart != pStartB) || (pEnd != pEndB)) return(0);
3401:   for (p = pStart; p < pEnd; ++p) {
3402:     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
3403:     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;

3405:     DMPlexGetConeSize(dmA, p, &coneSize);
3406:     DMPlexGetCone(dmA, p, &cone);
3407:     DMPlexGetConeOrientation(dmA, p, &ornt);
3408:     DMPlexGetConeSize(dmB, p, &coneSizeB);
3409:     DMPlexGetCone(dmB, p, &coneB);
3410:     DMPlexGetConeOrientation(dmB, p, &orntB);
3411:     if (coneSize != coneSizeB) return(0);
3412:     for (c = 0; c < coneSize; ++c) {
3413:       if (cone[c] != coneB[c]) return(0);
3414:       if (ornt[c] != orntB[c]) return(0);
3415:     }
3416:     DMPlexGetSupportSize(dmA, p, &supportSize);
3417:     DMPlexGetSupport(dmA, p, &support);
3418:     DMPlexGetSupportSize(dmB, p, &supportSizeB);
3419:     DMPlexGetSupport(dmB, p, &supportB);
3420:     if (supportSize != supportSizeB) return(0);
3421:     for (s = 0; s < supportSize; ++s) {
3422:       if (support[s] != supportB[s]) return(0);
3423:     }
3424:   }
3425:   *equal = PETSC_TRUE;
3426:   return(0);
3427: }

3429: /*@C
3430:   DMPlexGetNumFaceVertices - Returns the number of vertices on a face

3432:   Not Collective

3434:   Input Parameters:
3435: + dm         - The DMPlex
3436: . cellDim    - The cell dimension
3437: - numCorners - The number of vertices on a cell

3439:   Output Parameters:
3440: . numFaceVertices - The number of vertices on a face

3442:   Level: developer

3444:   Notes:
3445:   Of course this can only work for a restricted set of symmetric shapes

3447: .seealso: DMPlexGetCone()
3448: @*/
3449: PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
3450: {
3451:   MPI_Comm       comm;

3455:   PetscObjectGetComm((PetscObject)dm,&comm);
3457:   switch (cellDim) {
3458:   case 0:
3459:     *numFaceVertices = 0;
3460:     break;
3461:   case 1:
3462:     *numFaceVertices = 1;
3463:     break;
3464:   case 2:
3465:     switch (numCorners) {
3466:     case 3: /* triangle */
3467:       *numFaceVertices = 2; /* Edge has 2 vertices */
3468:       break;
3469:     case 4: /* quadrilateral */
3470:       *numFaceVertices = 2; /* Edge has 2 vertices */
3471:       break;
3472:     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
3473:       *numFaceVertices = 3; /* Edge has 3 vertices */
3474:       break;
3475:     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
3476:       *numFaceVertices = 3; /* Edge has 3 vertices */
3477:       break;
3478:     default:
3479:       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3480:     }
3481:     break;
3482:   case 3:
3483:     switch (numCorners) {
3484:     case 4: /* tetradehdron */
3485:       *numFaceVertices = 3; /* Face has 3 vertices */
3486:       break;
3487:     case 6: /* tet cohesive cells */
3488:       *numFaceVertices = 4; /* Face has 4 vertices */
3489:       break;
3490:     case 8: /* hexahedron */
3491:       *numFaceVertices = 4; /* Face has 4 vertices */
3492:       break;
3493:     case 9: /* tet cohesive Lagrange cells */
3494:       *numFaceVertices = 6; /* Face has 6 vertices */
3495:       break;
3496:     case 10: /* quadratic tetrahedron */
3497:       *numFaceVertices = 6; /* Face has 6 vertices */
3498:       break;
3499:     case 12: /* hex cohesive Lagrange cells */
3500:       *numFaceVertices = 6; /* Face has 6 vertices */
3501:       break;
3502:     case 18: /* quadratic tet cohesive Lagrange cells */
3503:       *numFaceVertices = 6; /* Face has 6 vertices */
3504:       break;
3505:     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
3506:       *numFaceVertices = 9; /* Face has 9 vertices */
3507:       break;
3508:     default:
3509:       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3510:     }
3511:     break;
3512:   default:
3513:     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
3514:   }
3515:   return(0);
3516: }

3518: /*@
3519:   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point

3521:   Not Collective

3523:   Input Parameter:
3524: . dm    - The DMPlex object

3526:   Output Parameter:
3527: . depthLabel - The DMLabel recording point depth

3529:   Level: developer

3531: .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
3532: @*/
3533: PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
3534: {

3540:   if (!dm->depthLabel) {DMGetLabel(dm, "depth", &dm->depthLabel);}
3541:   *depthLabel = dm->depthLabel;
3542:   return(0);
3543: }

3545: /*@
3546:   DMPlexGetDepth - Get the depth of the DAG representing this mesh

3548:   Not Collective

3550:   Input Parameter:
3551: . dm    - The DMPlex object

3553:   Output Parameter:
3554: . depth - The number of strata (breadth first levels) in the DAG

3556:   Level: developer

3558: .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
3559: @*/
3560: PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
3561: {
3562:   DMLabel        label;
3563:   PetscInt       d = 0;

3569:   DMPlexGetDepthLabel(dm, &label);
3570:   if (label) {DMLabelGetNumValues(label, &d);}
3571:   *depth = d-1;
3572:   return(0);
3573: }

3575: /*@
3576:   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.

3578:   Not Collective

3580:   Input Parameters:
3581: + dm           - The DMPlex object
3582: - stratumValue - The requested depth

3584:   Output Parameters:
3585: + start - The first point at this depth
3586: - end   - One beyond the last point at this depth

3588:   Notes:
3589:   Depth indexing is related to topological dimension.  Depth stratum 0 contains the lowest topological dimension points,
3590:   often "vertices".  If the mesh is "interpolated" (see DMPlexInterpolate()), then depth stratum 1 contains the next
3591:   higher dimension, e.g., "edges".

3593:   Level: developer

3595: .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
3596: @*/
3597: PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
3598: {
3599:   DMLabel        label;
3600:   PetscInt       pStart, pEnd;

3607:   DMPlexGetChart(dm, &pStart, &pEnd);
3608:   if (pStart == pEnd) return(0);
3609:   if (stratumValue < 0) {
3610:     if (start) *start = pStart;
3611:     if (end)   *end   = pEnd;
3612:     return(0);
3613:   }
3614:   DMPlexGetDepthLabel(dm, &label);
3615:   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
3616:   DMLabelGetStratumBounds(label, stratumValue, start, end);
3617:   return(0);
3618: }

3620: /*@
3621:   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.

3623:   Not Collective

3625:   Input Parameters:
3626: + dm           - The DMPlex object
3627: - stratumValue - The requested height

3629:   Output Parameters:
3630: + start - The first point at this height
3631: - end   - One beyond the last point at this height

3633:   Notes:
3634:   Height indexing is related to topological codimension.  Height stratum 0 contains the highest topological dimension
3635:   points, often called "cells" or "elements".  If the mesh is "interpolated" (see DMPlexInterpolate()), then height
3636:   stratum 1 contains the boundary of these "cells", often called "faces" or "facets".

3638:   Level: developer

3640: .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
3641: @*/
3642: PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
3643: {
3644:   DMLabel        label;
3645:   PetscInt       depth, pStart, pEnd;

3652:   DMPlexGetChart(dm, &pStart, &pEnd);
3653:   if (pStart == pEnd) return(0);
3654:   if (stratumValue < 0) {
3655:     if (start) *start = pStart;
3656:     if (end)   *end   = pEnd;
3657:     return(0);
3658:   }
3659:   DMPlexGetDepthLabel(dm, &label);
3660:   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
3661:   DMLabelGetNumValues(label, &depth);
3662:   DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);
3663:   return(0);
3664: }

3666: PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
3667: {
3668:   PetscSection   section, s;
3669:   Mat            m;
3670:   PetscInt       maxHeight;

3674:   DMClone(dm, cdm);
3675:   DMPlexGetMaxProjectionHeight(dm, &maxHeight);
3676:   DMPlexSetMaxProjectionHeight(*cdm, maxHeight);
3677:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);
3678:   DMSetLocalSection(*cdm, section);
3679:   PetscSectionDestroy(&section);
3680:   PetscSectionCreate(PETSC_COMM_SELF, &s);
3681:   MatCreate(PETSC_COMM_SELF, &m);
3682:   DMSetDefaultConstraints(*cdm, s, m);
3683:   PetscSectionDestroy(&s);
3684:   MatDestroy(&m);

3686:   DMSetNumFields(*cdm, 1);
3687:   DMCreateDS(*cdm);
3688:   return(0);
3689: }

3691: PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
3692: {
3693:   Vec            coordsLocal;
3694:   DM             coordsDM;

3698:   *field = NULL;
3699:   DMGetCoordinatesLocal(dm,&coordsLocal);
3700:   DMGetCoordinateDM(dm,&coordsDM);
3701:   if (coordsLocal && coordsDM) {
3702:     DMFieldCreateDS(coordsDM, 0, coordsLocal, field);
3703:   }
3704:   return(0);
3705: }

3707: /*@C
3708:   DMPlexGetConeSection - Return a section which describes the layout of cone data

3710:   Not Collective

3712:   Input Parameters:
3713: . dm        - The DMPlex object

3715:   Output Parameter:
3716: . section - The PetscSection object

3718:   Level: developer

3720: .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
3721: @*/
3722: PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
3723: {
3724:   DM_Plex *mesh = (DM_Plex*) dm->data;

3728:   if (section) *section = mesh->coneSection;
3729:   return(0);
3730: }

3732: /*@C
3733:   DMPlexGetSupportSection - Return a section which describes the layout of support data

3735:   Not Collective

3737:   Input Parameters:
3738: . dm        - The DMPlex object

3740:   Output Parameter:
3741: . section - The PetscSection object

3743:   Level: developer

3745: .seealso: DMPlexGetConeSection()
3746: @*/
3747: PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
3748: {
3749:   DM_Plex *mesh = (DM_Plex*) dm->data;

3753:   if (section) *section = mesh->supportSection;
3754:   return(0);
3755: }

3757: /*@C
3758:   DMPlexGetCones - Return cone data

3760:   Not Collective

3762:   Input Parameters:
3763: . dm        - The DMPlex object

3765:   Output Parameter:
3766: . cones - The cone for each point

3768:   Level: developer

3770: .seealso: DMPlexGetConeSection()
3771: @*/
3772: PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
3773: {
3774:   DM_Plex *mesh = (DM_Plex*) dm->data;

3778:   if (cones) *cones = mesh->cones;
3779:   return(0);
3780: }

3782: /*@C
3783:   DMPlexGetConeOrientations - Return cone orientation data

3785:   Not Collective

3787:   Input Parameters:
3788: . dm        - The DMPlex object

3790:   Output Parameter:
3791: . coneOrientations - The cone orientation for each point

3793:   Level: developer

3795: .seealso: DMPlexGetConeSection()
3796: @*/
3797: PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
3798: {
3799:   DM_Plex *mesh = (DM_Plex*) dm->data;

3803:   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
3804:   return(0);
3805: }

3807: /******************************** FEM Support **********************************/

3809: /*
3810:  Returns number of components and tensor degree for the field.  For interpolated meshes, line should be a point
3811:  representing a line in the section.
3812: */
3813: static PetscErrorCode PetscSectionFieldGetTensorDegree_Private(PetscSection section,PetscInt field,PetscInt line,PetscBool vertexchart,PetscInt *Nc,PetscInt *k)
3814: {

3818:   PetscSectionGetFieldComponents(section, field, Nc);
3819:   if (vertexchart) {            /* If we only have a vertex chart, we must have degree k=1 */
3820:     *k = 1;
3821:   } else {                      /* Assume the full interpolated mesh is in the chart; lines in particular */
3822:     /* An order k SEM disc has k-1 dofs on an edge */
3823:     PetscSectionGetFieldDof(section, line, field, k);
3824:     *k = *k / *Nc + 1;
3825:   }
3826:   return(0);
3827: }

3829: /*@

3831:   DMPlexSetClosurePermutationTensor - Create a permutation from the default (BFS) point ordering in the closure, to a
3832:   lexicographic ordering over the tensor product cell (i.e., line, quad, hex, etc.), and set this permutation in the
3833:   section provided (or the section of the DM).

3835:   Input Parameters:
3836: + dm      - The DM
3837: . point   - Either a cell (highest dim point) or an edge (dim 1 point), or PETSC_DETERMINE
3838: - section - The PetscSection to reorder, or NULL for the default section

3840:   Note: The point is used to determine the number of dofs/field on an edge. For SEM, this is related to the polynomial
3841:   degree of the basis.

3843:   Example:
3844:   A typical interpolated single-quad mesh might order points as
3845: .vb
3846:   [c0, v1, v2, v3, v4, e5, e6, e7, e8]

3848:   v4 -- e6 -- v3
3849:   |           |
3850:   e7    c0    e8
3851:   |           |
3852:   v1 -- e5 -- v2
3853: .ve

3855:   (There is no significance to the ordering described here.)  The default section for a Q3 quad might typically assign
3856:   dofs in the order of points, e.g.,
3857: .vb
3858:     c0 -> [0,1,2,3]
3859:     v1 -> [4]
3860:     ...
3861:     e5 -> [8, 9]
3862: .ve

3864:   which corresponds to the dofs
3865: .vb
3866:     6   10  11  7
3867:     13  2   3   15
3868:     12  0   1   14
3869:     4   8   9   5
3870: .ve

3872:   The closure in BFS ordering works through height strata (cells, edges, vertices) to produce the ordering
3873: .vb
3874:   0 1 2 3 8 9 14 15 11 10 13 12 4 5 7 6
3875: .ve

3877:   After calling DMPlexSetClosurePermutationTensor(), the closure will be ordered lexicographically,
3878: .vb
3879:    4 8 9 5 12 0 1 14 13 2 3 15 6 10 11 7
3880: .ve

3882:   Level: developer

3884: .seealso: DMGetLocalSection(), PetscSectionSetClosurePermutation(), DMSetGlocalSection()
3885: @*/
3886: PetscErrorCode DMPlexSetClosurePermutationTensor(DM dm, PetscInt point, PetscSection section)
3887: {
3888:   DMLabel        label;
3889:   PetscInt      *perm;
3890:   PetscInt       dim, depth, eStart, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
3891:   PetscBool      vertexchart;

3895:   if (point < 0) {DMPlexGetDepthStratum(dm, 1, &point, NULL);}
3896:   DMGetDimension(dm, &dim);
3897:   DMPlexGetDepthLabel(dm, &label);
3898:   DMLabelGetValue(label, point, &depth);
3899:   if (depth == 1) {eStart = point;}
3900:   else if  (depth == dim) {
3901:     const PetscInt *cone;

3903:     DMPlexGetCone(dm, point, &cone);
3904:     if (dim == 2) eStart = cone[0];
3905:     else if (dim == 3) {
3906:       const PetscInt *cone2;
3907:       DMPlexGetCone(dm, cone[0], &cone2);
3908:       eStart = cone2[0];
3909:     } else SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Point %D of depth %D cannot be used to bootstrap spectral ordering for dim %D", point, depth, dim);
3910:   } else SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Point %D of depth %D cannot be used to bootstrap spectral ordering for dim %D", point, depth, dim);
3911:   if (!section) {DMGetLocalSection(dm, &section);}
3912:   {                             /* Determine whether the chart covers all points or just vertices. */
3913:     PetscInt pStart,pEnd,cStart,cEnd;
3914:     DMPlexGetDepthStratum(dm,0,&pStart,&pEnd);
3915:     PetscSectionGetChart(section,&cStart,&cEnd);
3916:     if (pStart == cStart && pEnd == cEnd) vertexchart = PETSC_TRUE; /* Just vertices */
3917:     else vertexchart = PETSC_FALSE;                                 /* Assume all interpolated points are in chart */
3918:   }
3919:   PetscSectionGetNumFields(section, &Nf);
3920:   if (dim < 1) return(0);
3921:   for (f = 0; f < Nf; ++f) {
3922:     PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);
3923:     size += PetscPowInt(k+1, dim)*Nc;
3924:   }
3925:   PetscMalloc1(size, &perm);
3926:   for (f = 0; f < Nf; ++f) {
3927:     switch (dim) {
3928:     case 1:
3929:       PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);
3930:       /*
3931:         Original ordering is [ edge of length k-1; vtx0; vtx1 ]
3932:         We want              [ vtx0; edge of length k-1; vtx1 ]
3933:       */
3934:       for (c=0; c<Nc; c++,offset++) perm[offset] = (k-1)*Nc + c + foffset;
3935:       for (i=0; i<k-1; i++) for (c=0; c<Nc; c++,offset++) perm[offset] = i*Nc + c + foffset;
3936:       for (c=0; c<Nc; c++,offset++) perm[offset] = k*Nc + c + foffset;
3937:       foffset = offset;
3938:       break;
3939:     case 2:
3940:       /* The original quad closure is oriented clockwise, {f, e_b, e_r, e_t, e_l, v_lb, v_rb, v_tr, v_tl} */
3941:       PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);
3942:       /* The SEM order is

3944:          v_lb, {e_b}, v_rb,
3945:          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
3946:          v_lt, reverse {e_t}, v_rt
3947:       */
3948:       {
3949:         const PetscInt of   = 0;
3950:         const PetscInt oeb  = of   + PetscSqr(k-1);
3951:         const PetscInt oer  = oeb  + (k-1);
3952:         const PetscInt oet  = oer  + (k-1);
3953:         const PetscInt oel  = oet  + (k-1);
3954:         const PetscInt ovlb = oel  + (k-1);
3955:         const PetscInt ovrb = ovlb + 1;
3956:         const PetscInt ovrt = ovrb + 1;
3957:         const PetscInt ovlt = ovrt + 1;
3958:         PetscInt       o;

3960:         /* bottom */
3961:         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
3962:         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
3963:         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
3964:         /* middle */
3965:         for (i = 0; i < k-1; ++i) {
3966:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
3967:           for (o = of+(k-1)*i; o < of+(k-1)*(i+1); ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
3968:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
3969:         }
3970:         /* top */
3971:         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
3972:         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
3973:         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
3974:         foffset = offset;
3975:       }
3976:       break;
3977:     case 3:
3978:       /* The original hex closure is

3980:          {c,
3981:           f_b, f_t, f_f, f_b, f_r, f_l,
3982:           e_bl, e_bb, e_br, e_bf,  e_tf, e_tr, e_tb, e_tl,  e_rf, e_lf, e_lb, e_rb,
3983:           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
3984:       */
3985:       PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);
3986:       /* The SEM order is
3987:          Bottom Slice
3988:          v_blf, {e^{(k-1)-n}_bf}, v_brf,
3989:          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
3990:          v_blb, {e_bb}, v_brb,

3992:          Middle Slice (j)
3993:          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
3994:          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
3995:          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,

3997:          Top Slice
3998:          v_tlf, {e_tf}, v_trf,
3999:          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
4000:          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
4001:       */
4002:       {
4003:         const PetscInt oc    = 0;
4004:         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
4005:         const PetscInt oft   = ofb   + PetscSqr(k-1);
4006:         const PetscInt off   = oft   + PetscSqr(k-1);
4007:         const PetscInt ofk   = off   + PetscSqr(k-1);
4008:         const PetscInt ofr   = ofk   + PetscSqr(k-1);
4009:         const PetscInt ofl   = ofr   + PetscSqr(k-1);
4010:         const PetscInt oebl  = ofl   + PetscSqr(k-1);
4011:         const PetscInt oebb  = oebl  + (k-1);
4012:         const PetscInt oebr  = oebb  + (k-1);
4013:         const PetscInt oebf  = oebr  + (k-1);
4014:         const PetscInt oetf  = oebf  + (k-1);
4015:         const PetscInt oetr  = oetf  + (k-1);
4016:         const PetscInt oetb  = oetr  + (k-1);
4017:         const PetscInt oetl  = oetb  + (k-1);
4018:         const PetscInt oerf  = oetl  + (k-1);
4019:         const PetscInt oelf  = oerf  + (k-1);
4020:         const PetscInt oelb  = oelf  + (k-1);
4021:         const PetscInt oerb  = oelb  + (k-1);
4022:         const PetscInt ovblf = oerb  + (k-1);
4023:         const PetscInt ovblb = ovblf + 1;
4024:         const PetscInt ovbrb = ovblb + 1;
4025:         const PetscInt ovbrf = ovbrb + 1;
4026:         const PetscInt ovtlf = ovbrf + 1;
4027:         const PetscInt ovtrf = ovtlf + 1;
4028:         const PetscInt ovtrb = ovtrf + 1;
4029:         const PetscInt ovtlb = ovtrb + 1;
4030:         PetscInt       o, n;

4032:         /* Bottom Slice */
4033:         /*   bottom */
4034:         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
4035:         for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
4036:         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
4037:         /*   middle */
4038:         for (i = 0; i < k-1; ++i) {
4039:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
4040:           for (n = 0; n < k-1; ++n) {o = ofb+n*(k-1)+i; for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;}
4041:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
4042:         }
4043:         /*   top */
4044:         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
4045:         for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
4046:         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;

4048:         /* Middle Slice */
4049:         for (j = 0; j < k-1; ++j) {
4050:           /*   bottom */
4051:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
4052:           for (o = off+j*(k-1); o < off+(j+1)*(k-1); ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
4053:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
4054:           /*   middle */
4055:           for (i = 0; i < k-1; ++i) {
4056:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
4057:             for (n = 0; n < k-1; ++n) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oc+(j*(k-1)+i)*(k-1)+n)*Nc + c + foffset;
4058:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
4059:           }
4060:           /*   top */
4061:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
4062:           for (o = ofk+j*(k-1)+(k-2); o >= ofk+j*(k-1); --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
4063:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
4064:         }

4066:         /* Top Slice */
4067:         /*   bottom */
4068:         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
4069:         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
4070:         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
4071:         /*   middle */
4072:         for (i = 0; i < k-1; ++i) {
4073:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
4074:           for (n = 0; n < k-1; ++n) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oft+i*(k-1)+n)*Nc + c + foffset;
4075:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
4076:         }
4077:         /*   top */
4078:         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
4079:         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
4080:         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;

4082:         foffset = offset;
4083:       }
4084:       break;
4085:     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
4086:     }
4087:   }
4088:   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
4089:   /* Check permutation */
4090:   {
4091:     PetscInt *check;

4093:     PetscMalloc1(size, &check);
4094:     for (i = 0; i < size; ++i) {check[i] = -1; if (perm[i] < 0 || perm[i] >= size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid permutation index p[%D] = %D", i, perm[i]);}
4095:     for (i = 0; i < size; ++i) check[perm[i]] = i;
4096:     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
4097:     PetscFree(check);
4098:   }
4099:   PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);
4100:   return(0);
4101: }

4103: PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
4104: {
4105:   PetscDS        prob;
4106:   PetscInt       depth, Nf, h;
4107:   DMLabel        label;

4111:   DMGetDS(dm, &prob);
4112:   Nf      = prob->Nf;
4113:   label   = dm->depthLabel;
4114:   *dspace = NULL;
4115:   if (field < Nf) {
4116:     PetscObject disc = prob->disc[field];

4118:     if (disc->classid == PETSCFE_CLASSID) {
4119:       PetscDualSpace dsp;

4121:       PetscFEGetDualSpace((PetscFE)disc,&dsp);
4122:       DMLabelGetNumValues(label,&depth);
4123:       DMLabelGetValue(label,point,&h);
4124:       h    = depth - 1 - h;
4125:       if (h) {
4126:         PetscDualSpaceGetHeightSubspace(dsp,h,dspace);
4127:       } else {
4128:         *dspace = dsp;
4129:       }
4130:     }
4131:   }
4132:   return(0);
4133: }


4136: PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4137: {
4138:   PetscScalar    *array, *vArray;
4139:   const PetscInt *cone, *coneO;
4140:   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
4141:   PetscErrorCode  ierr;

4144:   PetscSectionGetChart(section, &pStart, &pEnd);
4145:   DMPlexGetConeSize(dm, point, &numPoints);
4146:   DMPlexGetCone(dm, point, &cone);
4147:   DMPlexGetConeOrientation(dm, point, &coneO);
4148:   if (!values || !*values) {
4149:     if ((point >= pStart) && (point < pEnd)) {
4150:       PetscInt dof;

4152:       PetscSectionGetDof(section, point, &dof);
4153:       size += dof;
4154:     }
4155:     for (p = 0; p < numPoints; ++p) {
4156:       const PetscInt cp = cone[p];
4157:       PetscInt       dof;

4159:       if ((cp < pStart) || (cp >= pEnd)) continue;
4160:       PetscSectionGetDof(section, cp, &dof);
4161:       size += dof;
4162:     }
4163:     if (!values) {
4164:       if (csize) *csize = size;
4165:       return(0);
4166:     }
4167:     DMGetWorkArray(dm, size, MPIU_SCALAR, &array);
4168:   } else {
4169:     array = *values;
4170:   }
4171:   size = 0;
4172:   VecGetArray(v, &vArray);
4173:   if ((point >= pStart) && (point < pEnd)) {
4174:     PetscInt     dof, off, d;
4175:     PetscScalar *varr;

4177:     PetscSectionGetDof(section, point, &dof);
4178:     PetscSectionGetOffset(section, point, &off);
4179:     varr = &vArray[off];
4180:     for (d = 0; d < dof; ++d, ++offset) {
4181:       array[offset] = varr[d];
4182:     }
4183:     size += dof;
4184:   }
4185:   for (p = 0; p < numPoints; ++p) {
4186:     const PetscInt cp = cone[p];
4187:     PetscInt       o  = coneO[p];
4188:     PetscInt       dof, off, d;
4189:     PetscScalar   *varr;

4191:     if ((cp < pStart) || (cp >= pEnd)) continue;
4192:     PetscSectionGetDof(section, cp, &dof);
4193:     PetscSectionGetOffset(section, cp, &off);
4194:     varr = &vArray[off];
4195:     if (o >= 0) {
4196:       for (d = 0; d < dof; ++d, ++offset) {
4197:         array[offset] = varr[d];
4198:       }
4199:     } else {
4200:       for (d = dof-1; d >= 0; --d, ++offset) {
4201:         array[offset] = varr[d];
4202:       }
4203:     }
4204:     size += dof;
4205:   }
4206:   VecRestoreArray(v, &vArray);
4207:   if (!*values) {
4208:     if (csize) *csize = size;
4209:     *values = array;
4210:   } else {
4211:     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
4212:     *csize = size;
4213:   }
4214:   return(0);
4215: }

4217: PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4218: {
4219:   const PetscInt *cla;
4220:   PetscInt       np, *pts = NULL;

4224:   PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);
4225:   if (!*clPoints) {
4226:     PetscInt pStart, pEnd, p, q;

4228:     PetscSectionGetChart(section, &pStart, &pEnd);
4229:     DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);
4230:     /* Compress out points not in the section */
4231:     for (p = 0, q = 0; p < np; p++) {
4232:       PetscInt r = pts[2*p];
4233:       if ((r >= pStart) && (r < pEnd)) {
4234:         pts[q*2]   = r;
4235:         pts[q*2+1] = pts[2*p+1];
4236:         ++q;
4237:       }
4238:     }
4239:     np = q;
4240:     cla = NULL;
4241:   } else {
4242:     PetscInt dof, off;

4244:     PetscSectionGetDof(*clSec, point, &dof);
4245:     PetscSectionGetOffset(*clSec, point, &off);
4246:     ISGetIndices(*clPoints, &cla);
4247:     np   = dof/2;
4248:     pts  = (PetscInt *) &cla[off];
4249:   }
4250:   *numPoints = np;
4251:   *points    = pts;
4252:   *clp       = cla;

4254:   return(0);
4255: }

4257: PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4258: {

4262:   if (!*clPoints) {
4263:     DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);
4264:   } else {
4265:     ISRestoreIndices(*clPoints, clp);
4266:   }
4267:   *numPoints = 0;
4268:   *points    = NULL;
4269:   *clSec     = NULL;
4270:   *clPoints  = NULL;
4271:   *clp       = NULL;
4272:   return(0);
4273: }

4275: PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Static(DM dm, PetscSection section, PetscInt numPoints, const PetscInt points[], const PetscInt clperm[], const PetscScalar vArray[], PetscInt *size, PetscScalar array[])
4276: {
4277:   PetscInt          offset = 0, p;
4278:   const PetscInt    **perms = NULL;
4279:   const PetscScalar **flips = NULL;
4280:   PetscErrorCode    ierr;

4283:   *size = 0;
4284:   PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);
4285:   for (p = 0; p < numPoints; p++) {
4286:     const PetscInt    point = points[2*p];
4287:     const PetscInt    *perm = perms ? perms[p] : NULL;
4288:     const PetscScalar *flip = flips ? flips[p] : NULL;
4289:     PetscInt          dof, off, d;
4290:     const PetscScalar *varr;

4292:     PetscSectionGetDof(section, point, &dof);
4293:     PetscSectionGetOffset(section, point, &off);
4294:     varr = &vArray[off];
4295:     if (clperm) {
4296:       if (perm) {
4297:         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
4298:       } else {
4299:         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
4300:       }
4301:       if (flip) {
4302:         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
4303:       }
4304:     } else {
4305:       if (perm) {
4306:         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
4307:       } else {
4308:         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
4309:       }
4310:       if (flip) {
4311:         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
4312:       }
4313:     }
4314:     offset += dof;
4315:   }
4316:   PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);
4317:   *size = offset;
4318:   return(0);
4319: }

4321: PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Fields_Static(DM dm, PetscSection section, PetscInt numPoints, const PetscInt points[], PetscInt numFields, const PetscInt clperm[], const PetscScalar vArray[], PetscInt *size, PetscScalar array[])
4322: {
4323:   PetscInt          offset = 0, f;
4324:   PetscErrorCode    ierr;

4327:   *size = 0;
4328:   for (f = 0; f < numFields; ++f) {
4329:     PetscInt          p;
4330:     const PetscInt    **perms = NULL;
4331:     const PetscScalar **flips = NULL;

4333:     PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);
4334:     for (p = 0; p < numPoints; p++) {
4335:       const PetscInt    point = points[2*p];
4336:       PetscInt          fdof, foff, b;
4337:       const PetscScalar *varr;
4338:       const PetscInt    *perm = perms ? perms[p] : NULL;
4339:       const PetscScalar *flip = flips ? flips[p] : NULL;

4341:       PetscSectionGetFieldDof(section, point, f, &fdof);
4342:       PetscSectionGetFieldOffset(section, point, f, &foff);
4343:       varr = &vArray[foff];
4344:       if (clperm) {
4345:         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
4346:         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
4347:         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
4348:       } else {
4349:         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
4350:         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
4351:         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
4352:       }
4353:       offset += fdof;
4354:     }
4355:     PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);
4356:   }
4357:   *size = offset;
4358:   return(0);
4359: }

4361: /*@C
4362:   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'

4364:   Not collective

4366:   Input Parameters:
4367: + dm - The DM
4368: . section - The section describing the layout in v, or NULL to use the default section
4369: . v - The local vector
4370: . point - The point in the DM
4371: . csize - The size of the input values array, or NULL
4372: - values - An array to use for the values, or NULL to have it allocated automatically

4374:   Output Parameters:
4375: + csize - The number of values in the closure
4376: - values - The array of values. If the user provided NULL, it is a borrowed array and should not be freed

4378: $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
4379: $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
4380: $ assembly function, and a user may already have allocated storage for this operation.
4381: $
4382: $ A typical use could be
4383: $
4384: $  values = NULL;
4385: $  DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);
4386: $  for (cl = 0; cl < clSize; ++cl) {
4387: $    <Compute on closure>
4388: $  }
4389: $  DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);
4390: $
4391: $ or
4392: $
4393: $  PetscMalloc1(clMaxSize, &values);
4394: $  for (p = pStart; p < pEnd; ++p) {
4395: $    clSize = clMaxSize;
4396: $    DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);
4397: $    for (cl = 0; cl < clSize; ++cl) {
4398: $      <Compute on closure>
4399: $    }
4400: $  }
4401: $  PetscFree(values);

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

4407:   The csize argument is not present in the Fortran 90 binding since it is internal to the array.

4409:   Level: intermediate

4411: .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4412: @*/
4413: PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4414: {
4415:   PetscSection       clSection;
4416:   IS                 clPoints;
4417:   PetscScalar       *array;
4418:   const PetscScalar *vArray;
4419:   PetscInt          *points = NULL;
4420:   const PetscInt    *clp, *perm;
4421:   PetscInt           depth, numFields, numPoints, size;
4422:   PetscErrorCode     ierr;

4426:   if (!section) {DMGetLocalSection(dm, &section);}
4429:   DMPlexGetDepth(dm, &depth);
4430:   PetscSectionGetNumFields(section, &numFields);
4431:   if (depth == 1 && numFields < 2) {
4432:     DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);
4433:     return(0);
4434:   }
4435:   /* Get points */
4436:   DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
4437:   PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);
4438:   /* Get array */
4439:   if (!values || !*values) {
4440:     PetscInt asize = 0, dof, p;

4442:     for (p = 0; p < numPoints*2; p += 2) {
4443:       PetscSectionGetDof(section, points[p], &dof);
4444:       asize += dof;
4445:     }
4446:     if (!values) {
4447:       DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
4448:       if (csize) *csize = asize;
4449:       return(0);
4450:     }
4451:     DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);
4452:   } else {
4453:     array = *values;
4454:   }
4455:   VecGetArrayRead(v, &vArray);
4456:   /* Get values */
4457:   if (numFields > 0) {DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, array);}
4458:   else               {DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, array);}
4459:   /* Cleanup points */
4460:   DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
4461:   /* Cleanup array */
4462:   VecRestoreArrayRead(v, &vArray);
4463:   if (!*values) {
4464:     if (csize) *csize = size;
4465:     *values = array;
4466:   } else {
4467:     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
4468:     *csize = size;
4469:   }
4470:   return(0);
4471: }

4473: /*@C
4474:   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'

4476:   Not collective

4478:   Input Parameters:
4479: + dm - The DM
4480: . section - The section describing the layout in v, or NULL to use the default section
4481: . v - The local vector
4482: . point - The point in the DM
4483: . csize - The number of values in the closure, or NULL
4484: - values - The array of values, which is a borrowed array and should not be freed

4486:   Note that the array values are discarded and not copied back into v. In order to copy values back to v, use DMPlexVecSetClosure()

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

4492:   The csize argument is not present in the Fortran 90 binding since it is internal to the array.

4494:   Level: intermediate

4496: .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4497: @*/
4498: PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4499: {
4500:   PetscInt       size = 0;

4504:   /* Should work without recalculating size */
4505:   DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);
4506:   *values = NULL;
4507:   return(0);
4508: }

4510: PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
4511: PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}

4513: PETSC_STATIC_INLINE PetscErrorCode updatePoint_private(PetscSection section, PetscInt point, PetscInt dof, void (*fuse)(PetscScalar*, PetscScalar), PetscBool setBC, const PetscInt perm[], const PetscScalar flip[], const PetscInt clperm[], const PetscScalar values[], PetscInt offset, PetscScalar array[])
4514: {
4515:   PetscInt        cdof;   /* The number of constraints on this point */
4516:   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4517:   PetscScalar    *a;
4518:   PetscInt        off, cind = 0, k;
4519:   PetscErrorCode  ierr;

4522:   PetscSectionGetConstraintDof(section, point, &cdof);
4523:   PetscSectionGetOffset(section, point, &off);
4524:   a    = &array[off];
4525:   if (!cdof || setBC) {
4526:     if (clperm) {
4527:       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
4528:       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
4529:     } else {
4530:       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
4531:       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
4532:     }
4533:   } else {
4534:     PetscSectionGetConstraintIndices(section, point, &cdofs);
4535:     if (clperm) {
4536:       if (perm) {for (k = 0; k < dof; ++k) {
4537:           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4538:           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
4539:         }
4540:       } else {
4541:         for (k = 0; k < dof; ++k) {
4542:           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4543:           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
4544:         }
4545:       }
4546:     } else {
4547:       if (perm) {
4548:         for (k = 0; k < dof; ++k) {
4549:           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4550:           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
4551:         }
4552:       } else {
4553:         for (k = 0; k < dof; ++k) {
4554:           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4555:           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
4556:         }
4557:       }
4558:     }
4559:   }
4560:   return(0);
4561: }

4563: PETSC_STATIC_INLINE PetscErrorCode updatePointBC_private(PetscSection section, PetscInt point, PetscInt dof, void (*fuse)(PetscScalar*, PetscScalar), const PetscInt perm[], const PetscScalar flip[], const PetscInt clperm[], const PetscScalar values[], PetscInt offset, PetscScalar array[])
4564: {
4565:   PetscInt        cdof;   /* The number of constraints on this point */
4566:   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4567:   PetscScalar    *a;
4568:   PetscInt        off, cind = 0, k;
4569:   PetscErrorCode  ierr;

4572:   PetscSectionGetConstraintDof(section, point, &cdof);
4573:   PetscSectionGetOffset(section, point, &off);
4574:   a    = &array[off];
4575:   if (cdof) {
4576:     PetscSectionGetConstraintIndices(section, point, &cdofs);
4577:     if (clperm) {
4578:       if (perm) {
4579:         for (k = 0; k < dof; ++k) {
4580:           if ((cind < cdof) && (k == cdofs[cind])) {
4581:             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
4582:             cind++;
4583:           }
4584:         }
4585:       } else {
4586:         for (k = 0; k < dof; ++k) {
4587:           if ((cind < cdof) && (k == cdofs[cind])) {
4588:             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
4589:             cind++;
4590:           }
4591:         }
4592:       }
4593:     } else {
4594:       if (perm) {
4595:         for (k = 0; k < dof; ++k) {
4596:           if ((cind < cdof) && (k == cdofs[cind])) {
4597:             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
4598:             cind++;
4599:           }
4600:         }
4601:       } else {
4602:         for (k = 0; k < dof; ++k) {
4603:           if ((cind < cdof) && (k == cdofs[cind])) {
4604:             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
4605:             cind++;
4606:           }
4607:         }
4608:       }
4609:     }
4610:   }
4611:   return(0);
4612: }

4614: PETSC_STATIC_INLINE PetscErrorCode updatePointFields_private(PetscSection section, PetscInt point, const PetscInt *perm, const PetscScalar *flip, PetscInt f, void (*fuse)(PetscScalar*, PetscScalar), PetscBool setBC, const PetscInt clperm[], const PetscScalar values[], PetscInt *offset, PetscScalar array[])
4615: {
4616:   PetscScalar    *a;
4617:   PetscInt        fdof, foff, fcdof, foffset = *offset;
4618:   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
4619:   PetscInt        cind = 0, b;
4620:   PetscErrorCode  ierr;

4623:   PetscSectionGetFieldDof(section, point, f, &fdof);
4624:   PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);
4625:   PetscSectionGetFieldOffset(section, point, f, &foff);
4626:   a    = &array[foff];
4627:   if (!fcdof || setBC) {
4628:     if (clperm) {
4629:       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
4630:       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
4631:     } else {
4632:       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
4633:       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
4634:     }
4635:   } else {
4636:     PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
4637:     if (clperm) {
4638:       if (perm) {
4639:         for (b = 0; b < fdof; b++) {
4640:           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
4641:           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4642:         }
4643:       } else {
4644:         for (b = 0; b < fdof; b++) {
4645:           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
4646:           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
4647:         }
4648:       }
4649:     } else {
4650:       if (perm) {
4651:         for (b = 0; b < fdof; b++) {
4652:           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
4653:           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
4654:         }
4655:       } else {
4656:         for (b = 0; b < fdof; b++) {
4657:           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
4658:           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4659:         }
4660:       }
4661:     }
4662:   }
4663:   *offset += fdof;
4664:   return(0);
4665: }

4667: PETSC_STATIC_INLINE PetscErrorCode updatePointFieldsBC_private(PetscSection section, PetscInt point, const PetscInt perm[], const PetscScalar flip[], PetscInt f, PetscInt Ncc, const PetscInt comps[], void (*fuse)(PetscScalar*, PetscScalar), const PetscInt clperm[], const PetscScalar values[], PetscInt *offset, PetscScalar array[])
4668: {
4669:   PetscScalar    *a;
4670:   PetscInt        fdof, foff, fcdof, foffset = *offset;
4671:   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
4672:   PetscInt        cind = 0, ncind = 0, b;
4673:   PetscBool       ncSet, fcSet;
4674:   PetscErrorCode  ierr;

4677:   PetscSectionGetFieldDof(section, point, f, &fdof);
4678:   PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);
4679:   PetscSectionGetFieldOffset(section, point, f, &foff);
4680:   a    = &array[foff];
4681:   if (fcdof) {
4682:     /* We just override fcdof and fcdofs with Ncc and comps */
4683:     PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
4684:     if (clperm) {
4685:       if (perm) {
4686:         if (comps) {
4687:           for (b = 0; b < fdof; b++) {
4688:             ncSet = fcSet = PETSC_FALSE;
4689:             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4690:             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4691:             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
4692:           }
4693:         } else {
4694:           for (b = 0; b < fdof; b++) {
4695:             if ((cind < fcdof) && (b == fcdofs[cind])) {
4696:               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4697:               ++cind;
4698:             }
4699:           }
4700:         }
4701:       } else {
4702:         if (comps) {
4703:           for (b = 0; b < fdof; b++) {
4704:             ncSet = fcSet = PETSC_FALSE;
4705:             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4706:             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4707:             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
4708:           }
4709:         } else {
4710:           for (b = 0; b < fdof; b++) {
4711:             if ((cind < fcdof) && (b == fcdofs[cind])) {
4712:               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
4713:               ++cind;
4714:             }
4715:           }
4716:         }
4717:       }
4718:     } else {
4719:       if (perm) {
4720:         if (comps) {
4721:           for (b = 0; b < fdof; b++) {
4722:             ncSet = fcSet = PETSC_FALSE;
4723:             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4724:             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4725:             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
4726:           }
4727:         } else {
4728:           for (b = 0; b < fdof; b++) {
4729:             if ((cind < fcdof) && (b == fcdofs[cind])) {
4730:               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
4731:               ++cind;
4732:             }
4733:           }
4734:         }
4735:       } else {
4736:         if (comps) {
4737:           for (b = 0; b < fdof; b++) {
4738:             ncSet = fcSet = PETSC_FALSE;
4739:             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4740:             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4741:             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
4742:           }
4743:         } else {
4744:           for (b = 0; b < fdof; b++) {
4745:             if ((cind < fcdof) && (b == fcdofs[cind])) {
4746:               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4747:               ++cind;
4748:             }
4749:           }
4750:         }
4751:       }
4752:     }
4753:   }
4754:   *offset += fdof;
4755:   return(0);
4756: }

4758: PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
4759: {
4760:   PetscScalar    *array;
4761:   const PetscInt *cone, *coneO;
4762:   PetscInt        pStart, pEnd, p, numPoints, off, dof;
4763:   PetscErrorCode  ierr;

4766:   PetscSectionGetChart(section, &pStart, &pEnd);
4767:   DMPlexGetConeSize(dm, point, &numPoints);
4768:   DMPlexGetCone(dm, point, &cone);
4769:   DMPlexGetConeOrientation(dm, point, &coneO);
4770:   VecGetArray(v, &array);
4771:   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
4772:     const PetscInt cp = !p ? point : cone[p-1];
4773:     const PetscInt o  = !p ? 0     : coneO[p-1];

4775:     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
4776:     PetscSectionGetDof(section, cp, &dof);
4777:     /* ADD_VALUES */
4778:     {
4779:       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4780:       PetscScalar    *a;
4781:       PetscInt        cdof, coff, cind = 0, k;

4783:       PetscSectionGetConstraintDof(section, cp, &cdof);
4784:       PetscSectionGetOffset(section, cp, &coff);
4785:       a    = &array[coff];
4786:       if (!cdof) {
4787:         if (o >= 0) {
4788:           for (k = 0; k < dof; ++k) {
4789:             a[k] += values[off+k];
4790:           }
4791:         } else {
4792:           for (k = 0; k < dof; ++k) {
4793:             a[k] += values[off+dof-k-1];
4794:           }
4795:         }
4796:       } else {
4797:         PetscSectionGetConstraintIndices(section, cp, &cdofs);
4798:         if (o >= 0) {
4799:           for (k = 0; k < dof; ++k) {
4800:             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4801:             a[k] += values[off+k];
4802:           }
4803:         } else {
4804:           for (k = 0; k < dof; ++k) {
4805:             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4806:             a[k] += values[off+dof-k-1];
4807:           }
4808:         }
4809:       }
4810:     }
4811:   }
4812:   VecRestoreArray(v, &array);
4813:   return(0);
4814: }

4816: /*@C
4817:   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'

4819:   Not collective

4821:   Input Parameters:
4822: + dm - The DM
4823: . section - The section describing the layout in v, or NULL to use the default section
4824: . v - The local vector
4825: . point - The point in the DM
4826: . values - The array of values
4827: - mode - The insert mode. One of INSERT_ALL_VALUES, ADD_ALL_VALUES, INSERT_VALUES, ADD_VALUES, INSERT_BC_VALUES, and ADD_BC_VALUES,
4828:          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.

4830:   Fortran Notes:
4831:   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.

4833:   Level: intermediate

4835: .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
4836: @*/
4837: PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
4838: {
4839:   PetscSection    clSection;
4840:   IS              clPoints;
4841:   PetscScalar    *array;
4842:   PetscInt       *points = NULL;
4843:   const PetscInt *clp, *clperm;
4844:   PetscInt        depth, numFields, numPoints, p;
4845:   PetscErrorCode  ierr;

4849:   if (!section) {DMGetLocalSection(dm, &section);}
4852:   DMPlexGetDepth(dm, &depth);
4853:   PetscSectionGetNumFields(section, &numFields);
4854:   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
4855:     DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);
4856:     return(0);
4857:   }
4858:   /* Get points */
4859:   PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);
4860:   DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
4861:   /* Get array */
4862:   VecGetArray(v, &array);
4863:   /* Get values */
4864:   if (numFields > 0) {
4865:     PetscInt offset = 0, f;
4866:     for (f = 0; f < numFields; ++f) {
4867:       const PetscInt    **perms = NULL;
4868:       const PetscScalar **flips = NULL;

4870:       PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);
4871:       switch (mode) {
4872:       case INSERT_VALUES:
4873:         for (p = 0; p < numPoints; p++) {
4874:           const PetscInt    point = points[2*p];
4875:           const PetscInt    *perm = perms ? perms[p] : NULL;
4876:           const PetscScalar *flip = flips ? flips[p] : NULL;
4877:           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4878:         } break;
4879:       case INSERT_ALL_VALUES:
4880:         for (p = 0; p < numPoints; p++) {
4881:           const PetscInt    point = points[2*p];
4882:           const PetscInt    *perm = perms ? perms[p] : NULL;
4883:           const PetscScalar *flip = flips ? flips[p] : NULL;
4884:           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4885:         } break;
4886:       case INSERT_BC_VALUES:
4887:         for (p = 0; p < numPoints; p++) {
4888:           const PetscInt    point = points[2*p];
4889:           const PetscInt    *perm = perms ? perms[p] : NULL;
4890:           const PetscScalar *flip = flips ? flips[p] : NULL;
4891:           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
4892:         } break;
4893:       case ADD_VALUES:
4894:         for (p = 0; p < numPoints; p++) {
4895:           const PetscInt    point = points[2*p];
4896:           const PetscInt    *perm = perms ? perms[p] : NULL;
4897:           const PetscScalar *flip = flips ? flips[p] : NULL;
4898:           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4899:         } break;
4900:       case ADD_ALL_VALUES:
4901:         for (p = 0; p < numPoints; p++) {
4902:           const PetscInt    point = points[2*p];
4903:           const PetscInt    *perm = perms ? perms[p] : NULL;
4904:           const PetscScalar *flip = flips ? flips[p] : NULL;
4905:           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4906:         } break;
4907:       case ADD_BC_VALUES:
4908:         for (p = 0; p < numPoints; p++) {
4909:           const PetscInt    point = points[2*p];
4910:           const PetscInt    *perm = perms ? perms[p] : NULL;
4911:           const PetscScalar *flip = flips ? flips[p] : NULL;
4912:           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
4913:         } break;
4914:       default:
4915:         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4916:       }
4917:       PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);
4918:     }
4919:   } else {
4920:     PetscInt dof, off;
4921:     const PetscInt    **perms = NULL;
4922:     const PetscScalar **flips = NULL;

4924:     PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);
4925:     switch (mode) {
4926:     case INSERT_VALUES:
4927:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
4928:         const PetscInt    point = points[2*p];
4929:         const PetscInt    *perm = perms ? perms[p] : NULL;
4930:         const PetscScalar *flip = flips ? flips[p] : NULL;
4931:         PetscSectionGetDof(section, point, &dof);
4932:         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
4933:       } break;
4934:     case INSERT_ALL_VALUES:
4935:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
4936:         const PetscInt    point = points[2*p];
4937:         const PetscInt    *perm = perms ? perms[p] : NULL;
4938:         const PetscScalar *flip = flips ? flips[p] : NULL;
4939:         PetscSectionGetDof(section, point, &dof);
4940:         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
4941:       } break;
4942:     case INSERT_BC_VALUES:
4943:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
4944:         const PetscInt    point = points[2*p];
4945:         const PetscInt    *perm = perms ? perms[p] : NULL;
4946:         const PetscScalar *flip = flips ? flips[p] : NULL;
4947:         PetscSectionGetDof(section, point, &dof);
4948:         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
4949:       } break;
4950:     case ADD_VALUES:
4951:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
4952:         const PetscInt    point = points[2*p];
4953:         const PetscInt    *perm = perms ? perms[p] : NULL;
4954:         const PetscScalar *flip = flips ? flips[p] : NULL;
4955:         PetscSectionGetDof(section, point, &dof);
4956:         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
4957:       } break;
4958:     case ADD_ALL_VALUES:
4959:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
4960:         const PetscInt    point = points[2*p];
4961:         const PetscInt    *perm = perms ? perms[p] : NULL;
4962:         const PetscScalar *flip = flips ? flips[p] : NULL;
4963:         PetscSectionGetDof(section, point, &dof);
4964:         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
4965:       } break;
4966:     case ADD_BC_VALUES:
4967:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
4968:         const PetscInt    point = points[2*p];
4969:         const PetscInt    *perm = perms ? perms[p] : NULL;
4970:         const PetscScalar *flip = flips ? flips[p] : NULL;
4971:         PetscSectionGetDof(section, point, &dof);
4972:         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
4973:       } break;
4974:     default:
4975:       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4976:     }
4977:     PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);
4978:   }
4979:   /* Cleanup points */
4980:   DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
4981:   /* Cleanup array */
4982:   VecRestoreArray(v, &array);
4983:   return(0);
4984: }

4986: PetscErrorCode DMPlexVecSetFieldClosure_Internal(DM dm, PetscSection section, Vec v, PetscBool fieldActive[], PetscInt point, PetscInt Ncc, const PetscInt comps[], const PetscScalar values[], InsertMode mode)
4987: {
4988:   PetscSection      clSection;
4989:   IS                clPoints;
4990:   PetscScalar       *array;
4991:   PetscInt          *points = NULL;
4992:   const PetscInt    *clp, *clperm;
4993:   PetscInt          numFields, numPoints, p;
4994:   PetscInt          offset = 0, f;
4995:   PetscErrorCode    ierr;

4999:   if (!section) {DMGetLocalSection(dm, &section);}
5002:   PetscSectionGetNumFields(section, &numFields);
5003:   /* Get points */
5004:   PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);
5005:   DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
5006:   /* Get array */
5007:   VecGetArray(v, &array);
5008:   /* Get values */
5009:   for (f = 0; f < numFields; ++f) {
5010:     const PetscInt    **perms = NULL;
5011:     const PetscScalar **flips = NULL;

5013:     if (!fieldActive[f]) {
5014:       for (p = 0; p < numPoints*2; p += 2) {
5015:         PetscInt fdof;
5016:         PetscSectionGetFieldDof(section, points[p], f, &fdof);
5017:         offset += fdof;
5018:       }
5019:       continue;
5020:     }
5021:     PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);
5022:     switch (mode) {
5023:     case INSERT_VALUES:
5024:       for (p = 0; p < numPoints; p++) {
5025:         const PetscInt    point = points[2*p];
5026:         const PetscInt    *perm = perms ? perms[p] : NULL;
5027:         const PetscScalar *flip = flips ? flips[p] : NULL;
5028:         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
5029:       } break;
5030:     case INSERT_ALL_VALUES:
5031:       for (p = 0; p < numPoints; p++) {
5032:         const PetscInt    point = points[2*p];
5033:         const PetscInt    *perm = perms ? perms[p] : NULL;
5034:         const PetscScalar *flip = flips ? flips[p] : NULL;
5035:         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
5036:         } break;
5037:     case INSERT_BC_VALUES:
5038:       for (p = 0; p < numPoints; p++) {
5039:         const PetscInt    point = points[2*p];
5040:         const PetscInt    *perm = perms ? perms[p] : NULL;
5041:         const PetscScalar *flip = flips ? flips[p] : NULL;
5042:         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, clperm, values, &offset, array);
5043:       } break;
5044:     case ADD_VALUES:
5045:       for (p = 0; p < numPoints; p++) {
5046:         const PetscInt    point = points[2*p];
5047:         const PetscInt    *perm = perms ? perms[p] : NULL;
5048:         const PetscScalar *flip = flips ? flips[p] : NULL;
5049:         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
5050:       } break;
5051:     case ADD_ALL_VALUES:
5052:       for (p = 0; p < numPoints; p++) {
5053:         const PetscInt    point = points[2*p];
5054:         const PetscInt    *perm = perms ? perms[p] : NULL;
5055:         const PetscScalar *flip = flips ? flips[p] : NULL;
5056:         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
5057:       } break;
5058:     default:
5059:       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5060:     }
5061:     PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);
5062:   }
5063:   /* Cleanup points */
5064:   DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
5065:   /* Cleanup array */
5066:   VecRestoreArray(v, &array);
5067:   return(0);
5068: }

5070: static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
5071: {
5072:   PetscMPIInt    rank;
5073:   PetscInt       i, j;

5077:   MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);
5078:   PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);
5079:   for (i = 0; i < numRIndices; i++) {PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);}
5080:   for (i = 0; i < numCIndices; i++) {PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);}
5081:   numCIndices = numCIndices ? numCIndices : numRIndices;
5082:   for (i = 0; i < numRIndices; i++) {
5083:     PetscViewerASCIIPrintf(viewer, "[%d]", rank);
5084:     for (j = 0; j < numCIndices; j++) {
5085: #if defined(PETSC_USE_COMPLEX)
5086:       PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));
5087: #else
5088:       PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);
5089: #endif
5090:     }
5091:     PetscViewerASCIIPrintf(viewer, "\n");
5092:   }
5093:   return(0);
5094: }

5096: /*
5097:   DMPlexGetIndicesPoint_Internal - Add the indices for dofs on a point to an index array

5099:   Input Parameters:
5100: + section - The section for this data layout
5101: . point   - The point contributing dofs with these indices
5102: . off     - The global offset of this point
5103: . loff    - The local offset of each field
5104: . setBC   - The flag determining whether to include indices of bounsary values
5105: . perm    - A permutation of the dofs on this point, or NULL
5106: - indperm - A permutation of the entire indices array, or NULL

5108:   Output Parameter:
5109: . indices - Indices for dofs on this point

5111:   Level: developer

5113:   Note: The indices could be local or global, depending on the value of 'off'.
5114: */
5115: PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], const PetscInt indperm[], PetscInt indices[])
5116: {
5117:   PetscInt        dof;   /* The number of unknowns on this point */
5118:   PetscInt        cdof;  /* The number of constraints on this point */
5119:   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5120:   PetscInt        cind = 0, k;
5121:   PetscErrorCode  ierr;

5124:   PetscSectionGetDof(section, point, &dof);
5125:   PetscSectionGetConstraintDof(section, point, &cdof);
5126:   if (!cdof || setBC) {
5127:     for (k = 0; k < dof; ++k) {
5128:       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
5129:       const PetscInt ind    = indperm ? indperm[preind] : preind;

5131:       indices[ind] = off + k;
5132:     }
5133:   } else {
5134:     PetscSectionGetConstraintIndices(section, point, &cdofs);
5135:     for (k = 0; k < dof; ++k) {
5136:       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
5137:       const PetscInt ind    = indperm ? indperm[preind] : preind;

5139:       if ((cind < cdof) && (k == cdofs[cind])) {
5140:         /* Insert check for returning constrained indices */
5141:         indices[ind] = -(off+k+1);
5142:         ++cind;
5143:       } else {
5144:         indices[ind] = off+k-cind;
5145:       }
5146:     }
5147:   }
5148:   *loff += dof;
5149:   return(0);
5150: }

5152: /*
5153:   This version only believes the point offset from the globalSection

5155:  . off - The global offset of this point
5156: */
5157: PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
5158: {
5159:   PetscInt       numFields, foff, f;

5163:   PetscSectionGetNumFields(section, &numFields);
5164:   for (f = 0, foff = 0; f < numFields; ++f) {
5165:     PetscInt        fdof, cfdof;
5166:     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
5167:     PetscInt        cind = 0, b;
5168:     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;

5170:     PetscSectionGetFieldDof(section, point, f, &fdof);
5171:     PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);
5172:     if (!cfdof || setBC) {
5173:       for (b = 0; b < fdof; ++b) {
5174:         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
5175:         const PetscInt ind    = indperm ? indperm[preind] : preind;

5177:         indices[ind] = off+foff+b;
5178:       }
5179:     } else {
5180:       PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
5181:       for (b = 0; b < fdof; ++b) {
5182:         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
5183:         const PetscInt ind    = indperm ? indperm[preind] : preind;

5185:         if ((cind < cfdof) && (b == fcdofs[cind])) {
5186:           indices[ind] = -(off+foff+b+1);
5187:           ++cind;
5188:         } else {
5189:           indices[ind] = off+foff+b-cind;
5190:         }
5191:       }
5192:     }
5193:     foff     += (setBC ? fdof : (fdof - cfdof));
5194:     foffs[f] += fdof;
5195:   }
5196:   return(0);
5197: }

5199: /*
5200:   This version believes the globalSection offsets for each field, rather than just the point offset

5202:  . foffs - The offset into 'indices' for each field, since it is segregated by field
5203: */
5204: PetscErrorCode DMPlexGetIndicesPointFieldsSplit_Internal(PetscSection section, PetscSection globalSection, PetscInt point, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
5205: {
5206:   PetscInt       numFields, foff, f;

5210:   PetscSectionGetNumFields(section, &numFields);
5211:   for (f = 0; f < numFields; ++f) {
5212:     PetscInt        fdof, cfdof;
5213:     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
5214:     PetscInt        cind = 0, b;
5215:     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;

5217:     PetscSectionGetFieldDof(section, point, f, &fdof);
5218:     PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);
5219:     PetscSectionGetFieldOffset(globalSection, point, f, &foff);
5220:     if (!cfdof || setBC) {
5221:       for (b = 0; b < fdof; ++b) {
5222:         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
5223:         const PetscInt ind    = indperm ? indperm[preind] : preind;

5225:         indices[ind] = foff+b;
5226:       }
5227:     } else {
5228:       PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
5229:       for (b = 0; b < fdof; ++b) {
5230:         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
5231:         const PetscInt ind    = indperm ? indperm[preind] : preind;

5233:         if ((cind < cfdof) && (b == fcdofs[cind])) {
5234:           indices[ind] = -(foff+b+1);
5235:           ++cind;
5236:         } else {
5237:           indices[ind] = foff+b-cind;
5238:         }
5239:       }
5240:     }
5241:     foffs[f] += fdof;
5242:   }
5243:   return(0);
5244: }

5246: PetscErrorCode DMPlexAnchorsModifyMat(DM dm, PetscSection section, PetscInt numPoints, PetscInt numIndices, const PetscInt points[], const PetscInt ***perms, const PetscScalar values[], PetscInt *outNumPoints, PetscInt *outNumIndices, PetscInt *outPoints[], PetscScalar *outValues[], PetscInt offsets[], PetscBool multiplyLeft)
5247: {
5248:   Mat             cMat;
5249:   PetscSection    aSec, cSec;
5250:   IS              aIS;
5251:   PetscInt        aStart = -1, aEnd = -1;
5252:   const PetscInt  *anchors;
5253:   PetscInt        numFields, f, p, q, newP = 0;
5254:   PetscInt        newNumPoints = 0, newNumIndices = 0;
5255:   PetscInt        *newPoints, *indices, *newIndices;
5256:   PetscInt        maxAnchor, maxDof;
5257:   PetscInt        newOffsets[32];
5258:   PetscInt        *pointMatOffsets[32];
5259:   PetscInt        *newPointOffsets[32];
5260:   PetscScalar     *pointMat[32];
5261:   PetscScalar     *newValues=NULL,*tmpValues;
5262:   PetscBool       anyConstrained = PETSC_FALSE;
5263:   PetscErrorCode  ierr;

5268:   PetscSectionGetNumFields(section, &numFields);

5270:   DMPlexGetAnchors(dm,&aSec,&aIS);
5271:   /* if there are point-to-point constraints */
5272:   if (aSec) {
5273:     PetscArrayzero(newOffsets, 32);
5274:     ISGetIndices(aIS,&anchors);
5275:     PetscSectionGetChart(aSec,&aStart,&aEnd);
5276:     /* figure out how many points are going to be in the new element matrix
5277:      * (we allow double counting, because it's all just going to be summed
5278:      * into the global matrix anyway) */
5279:     for (p = 0; p < 2*numPoints; p+=2) {
5280:       PetscInt b    = points[p];
5281:       PetscInt bDof = 0, bSecDof;

5283:       PetscSectionGetDof(section,b,&bSecDof);
5284:       if (!bSecDof) {
5285:         continue;
5286:       }
5287:       if (b >= aStart && b < aEnd) {
5288:         PetscSectionGetDof(aSec,b,&bDof);
5289:       }
5290:       if (bDof) {
5291:         /* this point is constrained */
5292:         /* it is going to be replaced by its anchors */
5293:         PetscInt bOff, q;

5295:         anyConstrained = PETSC_TRUE;
5296:         newNumPoints  += bDof;
5297:         PetscSectionGetOffset(aSec,b,&bOff);
5298:         for (q = 0; q < bDof; q++) {
5299:           PetscInt a = anchors[bOff + q];
5300:           PetscInt aDof;

5302:           PetscSectionGetDof(section,a,&aDof);
5303:           newNumIndices += aDof;
5304:           for (f = 0; f < numFields; ++f) {
5305:             PetscInt fDof;

5307:             PetscSectionGetFieldDof(section, a, f, &fDof);
5308:             newOffsets[f+1] += fDof;
5309:           }
5310:         }
5311:       }
5312:       else {
5313:         /* this point is not constrained */
5314:         newNumPoints++;
5315:         newNumIndices += bSecDof;
5316:         for (f = 0; f < numFields; ++f) {
5317:           PetscInt fDof;

5319:           PetscSectionGetFieldDof(section, b, f, &fDof);
5320:           newOffsets[f+1] += fDof;
5321:         }
5322:       }
5323:     }
5324:   }
5325:   if (!anyConstrained) {
5326:     if (outNumPoints)  *outNumPoints  = 0;
5327:     if (outNumIndices) *outNumIndices = 0;
5328:     if (outPoints)     *outPoints     = NULL;
5329:     if (outValues)     *outValues     = NULL;
5330:     if (aSec) {ISRestoreIndices(aIS,&anchors);}
5331:     return(0);
5332:   }

5334:   if (outNumPoints)  *outNumPoints  = newNumPoints;
5335:   if (outNumIndices) *outNumIndices = newNumIndices;

5337:   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];

5339:   if (!outPoints && !outValues) {
5340:     if (offsets) {
5341:       for (f = 0; f <= numFields; f++) {
5342:         offsets[f] = newOffsets[f];
5343:       }
5344:     }
5345:     if (aSec) {ISRestoreIndices(aIS,&anchors);}
5346:     return(0);
5347:   }

5349:   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);

5351:   DMGetDefaultConstraints(dm, &cSec, &cMat);

5353:   /* workspaces */
5354:   if (numFields) {
5355:     for (f = 0; f < numFields; f++) {
5356:       DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);
5357:       DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);
5358:     }
5359:   }
5360:   else {
5361:     DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);
5362:     DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);
5363:   }

5365:   /* get workspaces for the point-to-point matrices */
5366:   if (numFields) {
5367:     PetscInt totalOffset, totalMatOffset;

5369:     for (p = 0; p < numPoints; p++) {
5370:       PetscInt b    = points[2*p];
5371:       PetscInt bDof = 0, bSecDof;

5373:       PetscSectionGetDof(section,b,&bSecDof);
5374:       if (!bSecDof) {
5375:         for (f = 0; f < numFields; f++) {
5376:           newPointOffsets[f][p + 1] = 0;
5377:           pointMatOffsets[f][p + 1] = 0;
5378:         }
5379:         continue;
5380:       }
5381:       if (b >= aStart && b < aEnd) {
5382:         PetscSectionGetDof(aSec, b, &bDof);
5383:       }
5384:       if (bDof) {
5385:         for (f = 0; f < numFields; f++) {
5386:           PetscInt fDof, q, bOff, allFDof = 0;

5388:           PetscSectionGetFieldDof(section, b, f, &fDof);
5389:           PetscSectionGetOffset(aSec, b, &bOff);
5390:           for (q = 0; q < bDof; q++) {
5391:             PetscInt a = anchors[bOff + q];
5392:             PetscInt aFDof;

5394:             PetscSectionGetFieldDof(section, a, f, &aFDof);
5395:             allFDof += aFDof;
5396:           }
5397:           newPointOffsets[f][p+1] = allFDof;
5398:           pointMatOffsets[f][p+1] = fDof * allFDof;
5399:         }
5400:       }
5401:       else {
5402:         for (f = 0; f < numFields; f++) {
5403:           PetscInt fDof;

5405:           PetscSectionGetFieldDof(section, b, f, &fDof);
5406:           newPointOffsets[f][p+1] = fDof;
5407:           pointMatOffsets[f][p+1] = 0;
5408:         }
5409:       }
5410:     }
5411:     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
5412:       newPointOffsets[f][0] = totalOffset;
5413:       pointMatOffsets[f][0] = totalMatOffset;
5414:       for (p = 0; p < numPoints; p++) {
5415:         newPointOffsets[f][p+1] += newPointOffsets[f][p];
5416:         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
5417:       }
5418:       totalOffset    = newPointOffsets[f][numPoints];
5419:       totalMatOffset = pointMatOffsets[f][numPoints];
5420:       DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);
5421:     }
5422:   }
5423:   else {
5424:     for (p = 0; p < numPoints; p++) {
5425:       PetscInt b    = points[2*p];
5426:       PetscInt bDof = 0, bSecDof;

5428:       PetscSectionGetDof(section,b,&bSecDof);
5429:       if (!bSecDof) {
5430:         newPointOffsets[0][p + 1] = 0;
5431:         pointMatOffsets[0][p + 1] = 0;
5432:         continue;
5433:       }
5434:       if (b >= aStart && b < aEnd) {
5435:         PetscSectionGetDof(aSec, b, &bDof);
5436:       }
5437:       if (bDof) {
5438:         PetscInt bOff, q, allDof = 0;

5440:         PetscSectionGetOffset(aSec, b, &bOff);
5441:         for (q = 0; q < bDof; q++) {
5442:           PetscInt a = anchors[bOff + q], aDof;

5444:           PetscSectionGetDof(section, a, &aDof);
5445:           allDof += aDof;
5446:         }
5447:         newPointOffsets[0][p+1] = allDof;
5448:         pointMatOffsets[0][p+1] = bSecDof * allDof;
5449:       }
5450:       else {
5451:         newPointOffsets[0][p+1] = bSecDof;
5452:         pointMatOffsets[0][p+1] = 0;
5453:       }
5454:     }
5455:     newPointOffsets[0][0] = 0;
5456:     pointMatOffsets[0][0] = 0;
5457:     for (p = 0; p < numPoints; p++) {
5458:       newPointOffsets[0][p+1] += newPointOffsets[0][p];
5459:       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
5460:     }
5461:     DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);
5462:   }

5464:   /* output arrays */
5465:   DMGetWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);

5467:   /* get the point-to-point matrices; construct newPoints */
5468:   PetscSectionGetMaxDof(aSec, &maxAnchor);
5469:   PetscSectionGetMaxDof(section, &maxDof);
5470:   DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);
5471:   DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);
5472:   if (numFields) {
5473:     for (p = 0, newP = 0; p < numPoints; p++) {
5474:       PetscInt b    = points[2*p];
5475:       PetscInt o    = points[2*p+1];
5476:       PetscInt bDof = 0, bSecDof;

5478:       PetscSectionGetDof(section, b, &bSecDof);
5479:       if (!bSecDof) {
5480:         continue;
5481:       }
5482:       if (b >= aStart && b < aEnd) {
5483:         PetscSectionGetDof(aSec, b, &bDof);
5484:       }
5485:       if (bDof) {
5486:         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;

5488:         fStart[0] = 0;
5489:         fEnd[0]   = 0;
5490:         for (f = 0; f < numFields; f++) {
5491:           PetscInt fDof;

5493:           PetscSectionGetFieldDof(cSec, b, f, &fDof);
5494:           fStart[f+1] = fStart[f] + fDof;
5495:           fEnd[f+1]   = fStart[f+1];
5496:         }
5497:         PetscSectionGetOffset(cSec, b, &bOff);
5498:         DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, perms, p, NULL, indices);

5500:         fAnchorStart[0] = 0;
5501:         fAnchorEnd[0]   = 0;
5502:         for (f = 0; f < numFields; f++) {
5503:           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];

5505:           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
5506:           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
5507:         }
5508:         PetscSectionGetOffset(aSec, b, &bOff);
5509:         for (q = 0; q < bDof; q++) {
5510:           PetscInt a = anchors[bOff + q], aOff;

5512:           /* we take the orientation of ap into account in the order that we constructed the indices above: the newly added points have no orientation */
5513:           newPoints[2*(newP + q)]     = a;
5514:           newPoints[2*(newP + q) + 1] = 0;
5515:           PetscSectionGetOffset(section, a, &aOff);
5516:           DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, NULL, newIndices);
5517:         }
5518:         newP += bDof;

5520:         if (outValues) {
5521:           /* get the point-to-point submatrix */
5522:           for (f = 0; f < numFields; f++) {
5523:             MatGetValues(cMat,fEnd[f]-fStart[f],indices + fStart[f],fAnchorEnd[f] - fAnchorStart[f],newIndices + fAnchorStart[f],pointMat[f] + pointMatOffsets[f][p]);
5524:           }
5525:         }
5526:       }
5527:       else {
5528:         newPoints[2 * newP]     = b;
5529:         newPoints[2 * newP + 1] = o;
5530:         newP++;
5531:       }
5532:     }
5533:   } else {
5534:     for (p = 0; p < numPoints; p++) {
5535:       PetscInt b    = points[2*p];
5536:       PetscInt o    = points[2*p+1];
5537:       PetscInt bDof = 0, bSecDof;

5539:       PetscSectionGetDof(section, b, &bSecDof);
5540:       if (!bSecDof) {
5541:         continue;
5542:       }
5543:       if (b >= aStart && b < aEnd) {
5544:         PetscSectionGetDof(aSec, b, &bDof);
5545:       }
5546:       if (bDof) {
5547:         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;

5549:         PetscSectionGetOffset(cSec, b, &bOff);
5550:         DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, NULL, indices);

5552:         PetscSectionGetOffset (aSec, b, &bOff);
5553:         for (q = 0; q < bDof; q++) {
5554:           PetscInt a = anchors[bOff + q], aOff;

5556:           /* we take the orientation of ap into account in the order that we constructed the indices above: the newly added points have no orientation */

5558:           newPoints[2*(newP + q)]     = a;
5559:           newPoints[2*(newP + q) + 1] = 0;
5560:           PetscSectionGetOffset(section, a, &aOff);
5561:           DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, NULL, newIndices);
5562:         }
5563:         newP += bDof;

5565:         /* get the point-to-point submatrix */
5566:         if (outValues) {
5567:           MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);
5568:         }
5569:       }
5570:       else {
5571:         newPoints[2 * newP]     = b;
5572:         newPoints[2 * newP + 1] = o;
5573:         newP++;
5574:       }
5575:     }
5576:   }

5578:   if (outValues) {
5579:     DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);
5580:     PetscArrayzero(tmpValues,newNumIndices*numIndices);
5581:     /* multiply constraints on the right */
5582:     if (numFields) {
5583:       for (f = 0; f < numFields; f++) {
5584:         PetscInt oldOff = offsets[f];

5586:         for (p = 0; p < numPoints; p++) {
5587:           PetscInt cStart = newPointOffsets[f][p];
5588:           PetscInt b      = points[2 * p];
5589:           PetscInt c, r, k;
5590:           PetscInt dof;

5592:           PetscSectionGetFieldDof(section,b,f,&dof);
5593:           if (!dof) {
5594:             continue;
5595:           }
5596:           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5597:             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
5598:             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];

5600:             for (r = 0; r < numIndices; r++) {
5601:               for (c = 0; c < nCols; c++) {
5602:                 for (k = 0; k < dof; k++) {
5603:                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
5604:                 }
5605:               }
5606:             }
5607:           }
5608:           else {
5609:             /* copy this column as is */
5610:             for (r = 0; r < numIndices; r++) {
5611:               for (c = 0; c < dof; c++) {
5612:                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5613:               }
5614:             }
5615:           }
5616:           oldOff += dof;
5617:         }
5618:       }
5619:     }
5620:     else {
5621:       PetscInt oldOff = 0;
5622:       for (p = 0; p < numPoints; p++) {
5623:         PetscInt cStart = newPointOffsets[0][p];
5624:         PetscInt b      = points[2 * p];
5625:         PetscInt c, r, k;
5626:         PetscInt dof;

5628:         PetscSectionGetDof(section,b,&dof);
5629:         if (!dof) {
5630:           continue;
5631:         }
5632:         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5633:           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
5634:           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];

5636:           for (r = 0; r < numIndices; r++) {
5637:             for (c = 0; c < nCols; c++) {
5638:               for (k = 0; k < dof; k++) {
5639:                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
5640:               }
5641:             }
5642:           }
5643:         }
5644:         else {
5645:           /* copy this column as is */
5646:           for (r = 0; r < numIndices; r++) {
5647:             for (c = 0; c < dof; c++) {
5648:               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5649:             }
5650:           }
5651:         }
5652:         oldOff += dof;
5653:       }
5654:     }

5656:     if (multiplyLeft) {
5657:       DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);
5658:       PetscArrayzero(newValues,newNumIndices*newNumIndices);
5659:       /* multiply constraints transpose on the left */
5660:       if (numFields) {
5661:         for (f = 0; f < numFields; f++) {
5662:           PetscInt oldOff = offsets[f];

5664:           for (p = 0; p < numPoints; p++) {
5665:             PetscInt rStart = newPointOffsets[f][p];
5666:             PetscInt b      = points[2 * p];
5667:             PetscInt c, r, k;
5668:             PetscInt dof;

5670:             PetscSectionGetFieldDof(section,b,f,&dof);
5671:             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5672:               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
5673:               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];

5675:               for (r = 0; r < nRows; r++) {
5676:                 for (c = 0; c < newNumIndices; c++) {
5677:                   for (k = 0; k < dof; k++) {
5678:                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5679:                   }
5680:                 }
5681:               }
5682:             }
5683:             else {
5684:               /* copy this row as is */
5685:               for (r = 0; r < dof; r++) {
5686:                 for (c = 0; c < newNumIndices; c++) {
5687:                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5688:                 }
5689:               }
5690:             }
5691:             oldOff += dof;
5692:           }
5693:         }
5694:       }
5695:       else {
5696:         PetscInt oldOff = 0;

5698:         for (p = 0; p < numPoints; p++) {
5699:           PetscInt rStart = newPointOffsets[0][p];
5700:           PetscInt b      = points[2 * p];
5701:           PetscInt c, r, k;
5702:           PetscInt dof;

5704:           PetscSectionGetDof(section,b,&dof);
5705:           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5706:             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
5707:             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];

5709:             for (r = 0; r < nRows; r++) {
5710:               for (c = 0; c < newNumIndices; c++) {
5711:                 for (k = 0; k < dof; k++) {
5712:                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5713:                 }
5714:               }
5715:             }
5716:           }
5717:           else {
5718:             /* copy this row as is */
5719:             for (r = 0; r < dof; r++) {
5720:               for (c = 0; c < newNumIndices; c++) {
5721:                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5722:               }
5723:             }
5724:           }
5725:           oldOff += dof;
5726:         }
5727:       }

5729:       DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);
5730:     }
5731:     else {
5732:       newValues = tmpValues;
5733:     }
5734:   }

5736:   /* clean up */
5737:   DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);
5738:   DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);

5740:   if (numFields) {
5741:     for (f = 0; f < numFields; f++) {
5742:       DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);
5743:       DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);
5744:       DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);
5745:     }
5746:   }
5747:   else {
5748:     DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);
5749:     DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);
5750:     DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);
5751:   }
5752:   ISRestoreIndices(aIS,&anchors);

5754:   /* output */
5755:   if (outPoints) {
5756:     *outPoints = newPoints;
5757:   }
5758:   else {
5759:     DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);
5760:   }
5761:   if (outValues) {
5762:     *outValues = newValues;
5763:   }
5764:   for (f = 0; f <= numFields; f++) {
5765:     offsets[f] = newOffsets[f];
5766:   }
5767:   return(0);
5768: }

5770: /*@C
5771:   DMPlexGetClosureIndices - Get the global indices in a vector v for all points in the closure of the given point

5773:   Not collective

5775:   Input Parameters:
5776: + dm - The DM
5777: . section - The section describing the layout in v, or NULL to use the default section
5778: . globalSection - The section describing the parallel layout in v, or NULL to use the default section
5779: - point - The mesh point

5781:   Output parameters:
5782: + numIndices - The number of indices
5783: . indices - The indices
5784: - outOffsets - Field offset if not NULL

5786:   Note: Must call DMPlexRestoreClosureIndices() to free allocated memory

5788:   Level: advanced

5790: .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
5791: @*/
5792: PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
5793: {
5794:   PetscSection    clSection;
5795:   IS              clPoints;
5796:   const PetscInt *clp, *clperm;
5797:   const PetscInt  **perms[32] = {NULL};
5798:   PetscInt       *points = NULL, *pointsNew;
5799:   PetscInt        numPoints, numPointsNew;
5800:   PetscInt        offsets[32];
5801:   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
5802:   PetscErrorCode  ierr;

5810:   PetscSectionGetNumFields(section, &Nf);
5811:   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
5812:   PetscArrayzero(offsets, 32);
5813:   /* Get points in closure */
5814:   DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
5815:   PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);
5816:   /* Get number of indices and indices per field */
5817:   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
5818:     PetscInt dof, fdof;

5820:     PetscSectionGetDof(section, points[p], &dof);
5821:     for (f = 0; f < Nf; ++f) {
5822:       PetscSectionGetFieldDof(section, points[p], f, &fdof);
5823:       offsets[f+1] += fdof;
5824:     }
5825:     Nind += dof;
5826:   }
5827:   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
5828:   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Nind);
5829:   if (!Nf) offsets[1] = Nind;
5830:   /* Get dual space symmetries */
5831:   for (f = 0; f < PetscMax(1,Nf); f++) {
5832:     if (Nf) {PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);}
5833:     else    {PetscSectionGetPointSyms(section,numPoints,points,&perms[f],NULL);}
5834:   }
5835:   /* Correct for hanging node constraints */
5836:   {
5837:     DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, perms, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);
5838:     if (numPointsNew) {
5839:       for (f = 0; f < PetscMax(1,Nf); f++) {
5840:         if (Nf) {PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);}
5841:         else    {PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);}
5842:       }
5843:       for (f = 0; f < PetscMax(1,Nf); f++) {
5844:         if (Nf) {PetscSectionGetFieldPointSyms(section,f,numPointsNew,pointsNew,&perms[f],NULL);}
5845:         else    {PetscSectionGetPointSyms(section,numPointsNew,pointsNew,&perms[f],NULL);}
5846:       }
5847:       DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
5848:       numPoints = numPointsNew;
5849:       Nind      = NindNew;
5850:       points    = pointsNew;
5851:     }
5852:   }
5853:   /* Calculate indices */
5854:   DMGetWorkArray(dm, Nind, MPIU_INT, indices);
5855:   if (Nf) {
5856:     if (outOffsets) {
5857:       PetscInt f;

5859:       for (f = 0; f <= Nf; f++) {
5860:         outOffsets[f] = offsets[f];
5861:       }
5862:     }
5863:     for (p = 0; p < numPoints; p++) {
5864:       PetscSectionGetOffset(globalSection, points[2*p], &globalOff);
5865:       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, clperm, *indices);
5866:     }
5867:   } else {
5868:     for (p = 0, off = 0; p < numPoints; p++) {
5869:       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;

5871:       PetscSectionGetOffset(globalSection, points[2*p], &globalOff);
5872:       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, clperm, *indices);
5873:     }
5874:   }
5875:   /* Cleanup points */
5876:   for (f = 0; f < PetscMax(1,Nf); f++) {
5877:     if (Nf) {PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);}
5878:     else    {PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);}
5879:   }
5880:   if (numPointsNew) {
5881:     DMRestoreWorkArray(dm, 2*numPointsNew, MPIU_INT, &pointsNew);
5882:   } else {
5883:     DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
5884:   }
5885:   if (numIndices) *numIndices = Nind;
5886:   return(0);
5887: }

5889: /*@C
5890:   DMPlexRestoreClosureIndices - Restore the indices in a vector v for all points in the closure of the given point

5892:   Not collective

5894:   Input Parameters:
5895: + dm - The DM
5896: . section - The section describing the layout in v, or NULL to use the default section
5897: . globalSection - The section describing the parallel layout in v, or NULL to use the default section
5898: . point - The mesh point
5899: . numIndices - The number of indices
5900: . indices - The indices
5901: - outOffsets - Field offset if not NULL

5903:   Level: advanced

5905: .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
5906: @*/
5907: PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
5908: {

5914:   DMRestoreWorkArray(dm, 0, MPIU_INT, indices);
5915:   return(0);
5916: }

5918: /*@C
5919:   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'

5921:   Not collective

5923:   Input Parameters:
5924: + dm - The DM
5925: . section - The section describing the layout in v, or NULL to use the default section
5926: . globalSection - The section describing the layout in v, or NULL to use the default global section
5927: . A - The matrix
5928: . point - The point in the DM
5929: . values - The array of values
5930: - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions

5932:   Fortran Notes:
5933:   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.

5935:   Level: intermediate

5937: .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
5938: @*/
5939: PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5940: {
5941:   DM_Plex            *mesh   = (DM_Plex*) dm->data;
5942:   PetscSection        clSection;
5943:   IS                  clPoints;
5944:   PetscInt           *points = NULL, *newPoints;
5945:   const PetscInt     *clp, *clperm;
5946:   PetscInt           *indices;
5947:   PetscInt            offsets[32];
5948:   const PetscInt    **perms[32] = {NULL};
5949:   const PetscScalar **flips[32] = {NULL};
5950:   PetscInt            numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, p, f;
5951:   PetscScalar        *valCopy = NULL;
5952:   PetscScalar        *newValues;
5953:   PetscErrorCode      ierr;

5957:   if (!section) {DMGetLocalSection(dm, &section);}
5959:   if (!globalSection) {DMGetGlobalSection(dm, &globalSection);}
5962:   PetscSectionGetNumFields(section, &numFields);
5963:   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5964:   PetscArrayzero(offsets, 32);
5965:   PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);
5966:   DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
5967:   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
5968:     PetscInt fdof;

5970:     PetscSectionGetDof(section, points[p], &dof);
5971:     for (f = 0; f < numFields; ++f) {
5972:       PetscSectionGetFieldDof(section, points[p], f, &fdof);
5973:       offsets[f+1] += fdof;
5974:     }
5975:     numIndices += dof;
5976:   }
5977:   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];

5979:   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[numFields], numIndices);
5980:   /* Get symmetries */
5981:   for (f = 0; f < PetscMax(1,numFields); f++) {
5982:     if (numFields) {PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);}
5983:     else           {PetscSectionGetPointSyms(section,numPoints,points,&perms[f],&flips[f]);}
5984:     if (values && flips[f]) { /* may need to apply sign changes to the element matrix */
5985:       PetscInt foffset = offsets[f];

5987:       for (p = 0; p < numPoints; p++) {
5988:         PetscInt point          = points[2*p], fdof;
5989:         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;

5991:         if (!numFields) {
5992:           PetscSectionGetDof(section,point,&fdof);
5993:         } else {
5994:           PetscSectionGetFieldDof(section,point,f,&fdof);
5995:         }
5996:         if (flip) {
5997:           PetscInt i, j, k;

5999:           if (!valCopy) {
6000:             DMGetWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);
6001:             for (j = 0; j < numIndices * numIndices; j++) valCopy[j] = values[j];
6002:             values = valCopy;
6003:           }
6004:           for (i = 0; i < fdof; i++) {
6005:             PetscScalar fval = flip[i];

6007:             for (k = 0; k < numIndices; k++) {
6008:               valCopy[numIndices * (foffset + i) + k] *= fval;
6009:               valCopy[numIndices * k + (foffset + i)] *= fval;
6010:             }
6011:           }
6012:         }
6013:         foffset += fdof;
6014:       }
6015:     }
6016:   }
6017:   DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,perms,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);
6018:   if (newNumPoints) {
6019:     if (valCopy) {
6020:       DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);
6021:     }
6022:     for (f = 0; f < PetscMax(1,numFields); f++) {
6023:       if (numFields) {PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);}
6024:       else           {PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);}
6025:     }
6026:     for (f = 0; f < PetscMax(1,numFields); f++) {
6027:       if (numFields) {PetscSectionGetFieldPointSyms(section,f,newNumPoints,newPoints,&perms[f],&flips[f]);}
6028:       else           {PetscSectionGetPointSyms(section,newNumPoints,newPoints,&perms[f],&flips[f]);}
6029:     }
6030:     DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
6031:     numPoints  = newNumPoints;
6032:     numIndices = newNumIndices;
6033:     points     = newPoints;
6034:     values     = newValues;
6035:   }
6036:   DMGetWorkArray(dm, numIndices, MPIU_INT, &indices);
6037:   if (numFields) {
6038:     PetscBool useFieldOffsets;

6040:     PetscSectionGetUseFieldOffsets(globalSection, &useFieldOffsets);
6041:     if (useFieldOffsets) {
6042:       for (p = 0; p < numPoints; p++) {
6043:         DMPlexGetIndicesPointFieldsSplit_Internal(section, globalSection, points[2*p], offsets, PETSC_FALSE, perms, p, clperm, indices);
6044:       }
6045:     } else {
6046:       for (p = 0; p < numPoints; p++) {
6047:         PetscSectionGetOffset(globalSection, points[2*p], &globalOff);
6048:         DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, clperm, indices);
6049:       }
6050:     }
6051:   } else {
6052:     for (p = 0, off = 0; p < numPoints; p++) {
6053:       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
6054:       PetscSectionGetOffset(globalSection, points[2*p], &globalOff);
6055:       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, clperm, indices);
6056:     }
6057:   }
6058:   if (mesh->printSetValues) {DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);}
6059:   MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
6060:   if (mesh->printFEM > 1) {
6061:     PetscInt i;
6062:     PetscPrintf(PETSC_COMM_SELF, "  Indices:");
6063:     for (i = 0; i < numIndices; ++i) {PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);}
6064:     PetscPrintf(PETSC_COMM_SELF, "\n");
6065:   }
6066:   if (ierr) {
6067:     PetscMPIInt    rank;
6068:     PetscErrorCode ierr2;

6070:     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
6071:     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6072:     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
6073:     ierr2 = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr2);
6074: 
6075:   }
6076:   for (f = 0; f < PetscMax(1,numFields); f++) {
6077:     if (numFields) {PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);}
6078:     else           {PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);}
6079:   }
6080:   if (newNumPoints) {
6081:     DMRestoreWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);
6082:     DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);
6083:   }
6084:   else {
6085:     if (valCopy) {
6086:       DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);
6087:     }
6088:     DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
6089:   }
6090:   DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);
6091:   return(0);
6092: }

6094: PetscErrorCode DMPlexMatSetClosureRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
6095: {
6096:   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
6097:   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
6098:   PetscInt       *cpoints = NULL;
6099:   PetscInt       *findices, *cindices;
6100:   const PetscInt *fclperm, *cclperm;
6101:   PetscInt        foffsets[32], coffsets[32];
6102:   CellRefiner     cellRefiner;
6103:   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
6104:   PetscErrorCode  ierr;

6109:   if (!fsection) {DMGetLocalSection(dmf, &fsection);}
6111:   if (!csection) {DMGetLocalSection(dmc, &csection);}
6113:   if (!globalFSection) {DMGetGlobalSection(dmf, &globalFSection);}
6115:   if (!globalCSection) {DMGetGlobalSection(dmc, &globalCSection);}
6118:   PetscSectionGetNumFields(fsection, &numFields);
6119:   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
6120:   PetscArrayzero(foffsets, 32);
6121:   PetscArrayzero(coffsets, 32);
6122:   PetscSectionGetClosureInversePermutation_Internal(fsection, (PetscObject) dmf, NULL, &fclperm);
6123:   PetscSectionGetClosureInversePermutation_Internal(csection, (PetscObject) dmc, NULL, &cclperm);
6124:   /* Column indices */
6125:   DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
6126:   maxFPoints = numCPoints;
6127:   /* Compress out points not in the section */
6128:   /*   TODO: Squeeze out points with 0 dof as well */
6129:   PetscSectionGetChart(csection, &pStart, &pEnd);
6130:   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
6131:     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
6132:       cpoints[q*2]   = cpoints[p];
6133:       cpoints[q*2+1] = cpoints[p+1];
6134:       ++q;
6135:     }
6136:   }
6137:   numCPoints = q;
6138:   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
6139:     PetscInt fdof;

6141:     PetscSectionGetDof(csection, cpoints[p], &dof);
6142:     if (!dof) continue;
6143:     for (f = 0; f < numFields; ++f) {
6144:       PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);
6145:       coffsets[f+1] += fdof;
6146:     }
6147:     numCIndices += dof;
6148:   }
6149:   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
6150:   /* Row indices */
6151:   DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);
6152:   CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);
6153:   DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);
6154:   for (r = 0, q = 0; r < numSubcells; ++r) {
6155:     /* TODO Map from coarse to fine cells */
6156:     DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);
6157:     /* Compress out points not in the section */
6158:     PetscSectionGetChart(fsection, &pStart, &pEnd);
6159:     for (p = 0; p < numFPoints*2; p += 2) {
6160:       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
6161:         PetscSectionGetDof(fsection, fpoints[p], &dof);
6162:         if (!dof) continue;
6163:         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
6164:         if (s < q) continue;
6165:         ftotpoints[q*2]   = fpoints[p];
6166:         ftotpoints[q*2+1] = fpoints[p+1];
6167:         ++q;
6168:       }
6169:     }
6170:     DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);
6171:   }
6172:   numFPoints = q;
6173:   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
6174:     PetscInt fdof;

6176:     PetscSectionGetDof(fsection, ftotpoints[p], &dof);
6177:     if (!dof) continue;
6178:     for (f = 0; f < numFields; ++f) {
6179:       PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);
6180:       foffsets[f+1] += fdof;
6181:     }
6182:     numFIndices += dof;
6183:   }
6184:   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];

6186:   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
6187:   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
6188:   DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);
6189:   DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);
6190:   if (numFields) {
6191:     const PetscInt **permsF[32] = {NULL};
6192:     const PetscInt **permsC[32] = {NULL};

6194:     for (f = 0; f < numFields; f++) {
6195:       PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);
6196:       PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);
6197:     }
6198:     for (p = 0; p < numFPoints; p++) {
6199:       PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);
6200:       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);
6201:     }
6202:     for (p = 0; p < numCPoints; p++) {
6203:       PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);
6204:       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);
6205:     }
6206:     for (f = 0; f < numFields; f++) {
6207:       PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);
6208:       PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);
6209:     }
6210:   } else {
6211:     const PetscInt **permsF = NULL;
6212:     const PetscInt **permsC = NULL;

6214:     PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);
6215:     PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);
6216:     for (p = 0, off = 0; p < numFPoints; p++) {
6217:       const PetscInt *perm = permsF ? permsF[p] : NULL;

6219:       PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);
6220:       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);
6221:     }
6222:     for (p = 0, off = 0; p < numCPoints; p++) {
6223:       const PetscInt *perm = permsC ? permsC[p] : NULL;

6225:       PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);
6226:       DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);
6227:     }
6228:     PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);
6229:     PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);
6230:   }
6231:   if (mesh->printSetValues) {DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);}
6232:   /* TODO: flips */
6233:   MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
6234:   if (ierr) {
6235:     PetscMPIInt    rank;
6236:     PetscErrorCode ierr2;

6238:     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
6239:     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6240:     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
6241:     ierr2 = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr2);
6242:     ierr2 = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr2);
6243: 
6244:   }
6245:   DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);
6246:   DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
6247:   DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);
6248:   DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);
6249:   return(0);
6250: }

6252: PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
6253: {
6254:   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
6255:   PetscInt      *cpoints = NULL;
6256:   PetscInt       foffsets[32], coffsets[32];
6257:   const PetscInt *fclperm, *cclperm;
6258:   CellRefiner    cellRefiner;
6259:   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;

6265:   if (!fsection) {DMGetLocalSection(dmf, &fsection);}
6267:   if (!csection) {DMGetLocalSection(dmc, &csection);}
6269:   if (!globalFSection) {DMGetGlobalSection(dmf, &globalFSection);}
6271:   if (!globalCSection) {DMGetGlobalSection(dmc, &globalCSection);}
6273:   PetscSectionGetNumFields(fsection, &numFields);
6274:   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
6275:   PetscArrayzero(foffsets, 32);
6276:   PetscArrayzero(coffsets, 32);
6277:   PetscSectionGetClosureInversePermutation_Internal(fsection, (PetscObject) dmf, NULL, &fclperm);
6278:   PetscSectionGetClosureInversePermutation_Internal(csection, (PetscObject) dmc, NULL, &cclperm);
6279:   /* Column indices */
6280:   DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
6281:   maxFPoints = numCPoints;
6282:   /* Compress out points not in the section */
6283:   /*   TODO: Squeeze out points with 0 dof as well */
6284:   PetscSectionGetChart(csection, &pStart, &pEnd);
6285:   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
6286:     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
6287:       cpoints[q*2]   = cpoints[p];
6288:       cpoints[q*2+1] = cpoints[p+1];
6289:       ++q;
6290:     }
6291:   }
6292:   numCPoints = q;
6293:   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
6294:     PetscInt fdof;

6296:     PetscSectionGetDof(csection, cpoints[p], &dof);
6297:     if (!dof) continue;
6298:     for (f = 0; f < numFields; ++f) {
6299:       PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);
6300:       coffsets[f+1] += fdof;
6301:     }
6302:     numCIndices += dof;
6303:   }
6304:   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
6305:   /* Row indices */
6306:   DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);
6307:   CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);
6308:   DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);
6309:   for (r = 0, q = 0; r < numSubcells; ++r) {
6310:     /* TODO Map from coarse to fine cells */
6311:     DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);
6312:     /* Compress out points not in the section */
6313:     PetscSectionGetChart(fsection, &pStart, &pEnd);
6314:     for (p = 0; p < numFPoints*2; p += 2) {
6315:       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
6316:         PetscSectionGetDof(fsection, fpoints[p], &dof);
6317:         if (!dof) continue;
6318:         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
6319:         if (s < q) continue;
6320:         ftotpoints[q*2]   = fpoints[p];
6321:         ftotpoints[q*2+1] = fpoints[p+1];
6322:         ++q;
6323:       }
6324:     }
6325:     DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);
6326:   }
6327:   numFPoints = q;
6328:   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
6329:     PetscInt fdof;

6331:     PetscSectionGetDof(fsection, ftotpoints[p], &dof);
6332:     if (!dof) continue;
6333:     for (f = 0; f < numFields; ++f) {
6334:       PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);
6335:       foffsets[f+1] += fdof;
6336:     }
6337:     numFIndices += dof;
6338:   }
6339:   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];

6341:   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
6342:   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
6343:   if (numFields) {
6344:     const PetscInt **permsF[32] = {NULL};
6345:     const PetscInt **permsC[32] = {NULL};

6347:     for (f = 0; f < numFields; f++) {
6348:       PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);
6349:       PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);
6350:     }
6351:     for (p = 0; p < numFPoints; p++) {
6352:       PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);
6353:       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);
6354:     }
6355:     for (p = 0; p < numCPoints; p++) {
6356:       PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);
6357:       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);
6358:     }
6359:     for (f = 0; f < numFields; f++) {
6360:       PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);
6361:       PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);
6362:     }
6363:   } else {
6364:     const PetscInt **permsF = NULL;
6365:     const PetscInt **permsC = NULL;

6367:     PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);
6368:     PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);
6369:     for (p = 0, off = 0; p < numFPoints; p++) {
6370:       const PetscInt *perm = permsF ? permsF[p] : NULL;

6372:       PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);
6373:       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);
6374:     }
6375:     for (p = 0, off = 0; p < numCPoints; p++) {
6376:       const PetscInt *perm = permsC ? permsC[p] : NULL;

6378:       PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);
6379:       DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);
6380:     }
6381:     PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);
6382:     PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);
6383:   }
6384:   DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);
6385:   DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
6386:   return(0);
6387: }

6389: /*@
6390:   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid

6392:   Input Parameter:
6393: . dm - The DMPlex object

6395:   Output Parameters:
6396: + cMax - The first hybrid cell
6397: . fMax - The first hybrid face
6398: . eMax - The first hybrid edge
6399: - vMax - The first hybrid vertex

6401:   Level: developer

6403: .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
6404: @*/
6405: PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
6406: {
6407:   DM_Plex       *mesh = (DM_Plex*) dm->data;
6408:   PetscInt       dim;

6413:   DMGetDimension(dm, &dim);
6414:   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6415:   if (cMax) *cMax = mesh->hybridPointMax[dim];
6416:   if (fMax) *fMax = mesh->hybridPointMax[PetscMax(dim-1,0)];
6417:   if (eMax) *eMax = mesh->hybridPointMax[1];
6418:   if (vMax) *vMax = mesh->hybridPointMax[0];
6419:   return(0);
6420: }

6422: static PetscErrorCode DMPlexCreateDimStratum(DM dm, DMLabel depthLabel, DMLabel dimLabel, PetscInt d, PetscInt dMax)
6423: {
6424:   IS             is, his;
6425:   PetscInt       first = 0, stride;
6426:   PetscBool      isStride;

6430:   DMLabelGetStratumIS(depthLabel, d, &is);
6431:   PetscObjectTypeCompare((PetscObject) is, ISSTRIDE, &isStride);
6432:   if (isStride) {
6433:     ISStrideGetInfo(is, &first, &stride);
6434:   }
6435:   if (is && (!isStride || stride != 1)) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM is not stratified: depth %D IS is not contiguous", d);
6436:   ISCreateStride(PETSC_COMM_SELF, (dMax - first), first, 1, &his);
6437:   DMLabelSetStratumIS(dimLabel, d, his);
6438:   ISDestroy(&his);
6439:   ISDestroy(&is);
6440:   return(0);
6441: }

6443: /*@
6444:   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid

6446:   Input Parameters:
6447: + dm   - The DMPlex object
6448: . cMax - The first hybrid cell
6449: . fMax - The first hybrid face
6450: . eMax - The first hybrid edge
6451: - vMax - The first hybrid vertex

6453:   Level: developer

6455: .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
6456: @*/
6457: PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
6458: {
6459:   DM_Plex       *mesh = (DM_Plex*) dm->data;
6460:   PetscInt       dim;

6465:   DMGetDimension(dm, &dim);
6466:   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6467:   if (cMax >= 0) mesh->hybridPointMax[dim]               = cMax;
6468:   if (fMax >= 0) mesh->hybridPointMax[PetscMax(dim-1,0)] = fMax;
6469:   if (eMax >= 0) mesh->hybridPointMax[1]                 = eMax;
6470:   if (vMax >= 0) mesh->hybridPointMax[0]                 = vMax;
6471:   return(0);
6472: }

6474: /*@C
6475:   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)

6477:   Input Parameter:
6478: . dm   - The DMPlex object

6480:   Output Parameter:
6481: . cellHeight - The height of a cell

6483:   Level: developer

6485: .seealso DMPlexSetVTKCellHeight()
6486: @*/
6487: PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
6488: {
6489:   DM_Plex *mesh = (DM_Plex*) dm->data;

6494:   *cellHeight = mesh->vtkCellHeight;
6495:   return(0);
6496: }

6498: /*@C
6499:   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)

6501:   Input Parameters:
6502: + dm   - The DMPlex object
6503: - cellHeight - The height of a cell

6505:   Level: developer

6507: .seealso DMPlexGetVTKCellHeight()
6508: @*/
6509: PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
6510: {
6511:   DM_Plex *mesh = (DM_Plex*) dm->data;

6515:   mesh->vtkCellHeight = cellHeight;
6516:   return(0);
6517: }

6519: /* We can easily have a form that takes an IS instead */
6520: PetscErrorCode DMPlexCreateNumbering_Internal(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
6521: {
6522:   PetscSection   section, globalSection;
6523:   PetscInt      *numbers, p;

6527:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);
6528:   PetscSectionSetChart(section, pStart, pEnd);
6529:   for (p = pStart; p < pEnd; ++p) {
6530:     PetscSectionSetDof(section, p, 1);
6531:   }
6532:   PetscSectionSetUp(section);
6533:   PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);
6534:   PetscMalloc1(pEnd - pStart, &numbers);
6535:   for (p = pStart; p < pEnd; ++p) {
6536:     PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);
6537:     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
6538:     else                       numbers[p-pStart] += shift;
6539:   }
6540:   ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);
6541:   if (globalSize) {
6542:     PetscLayout layout;
6543:     PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);
6544:     PetscLayoutGetSize(layout, globalSize);
6545:     PetscLayoutDestroy(&layout);
6546:   }
6547:   PetscSectionDestroy(&section);
6548:   PetscSectionDestroy(&globalSection);
6549:   return(0);
6550: }

6552: PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
6553: {
6554:   PetscInt       cellHeight, cStart, cEnd, cMax;

6558:   DMPlexGetVTKCellHeight(dm, &cellHeight);
6559:   DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
6560:   DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);
6561:   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
6562:   DMPlexCreateNumbering_Internal(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);
6563:   return(0);
6564: }

6566: /*@
6567:   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process

6569:   Input Parameter:
6570: . dm   - The DMPlex object

6572:   Output Parameter:
6573: . globalCellNumbers - Global cell numbers for all cells on this process

6575:   Level: developer

6577: .seealso DMPlexGetVertexNumbering()
6578: @*/
6579: PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
6580: {
6581:   DM_Plex       *mesh = (DM_Plex*) dm->data;

6586:   if (!mesh->globalCellNumbers) {DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);}
6587:   *globalCellNumbers = mesh->globalCellNumbers;
6588:   return(0);
6589: }

6591: PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
6592: {
6593:   PetscInt       vStart, vEnd, vMax;

6598:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
6599:   DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);
6600:   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
6601:   DMPlexCreateNumbering_Internal(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);
6602:   return(0);
6603: }

6605: /*@
6606:   DMPlexGetVertexNumbering - Get a global vertex numbering for all vertices on this process

6608:   Input Parameter:
6609: . dm   - The DMPlex object

6611:   Output Parameter:
6612: . globalVertexNumbers - Global vertex numbers for all vertices on this process

6614:   Level: developer

6616: .seealso DMPlexGetCellNumbering()
6617: @*/
6618: PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
6619: {
6620:   DM_Plex       *mesh = (DM_Plex*) dm->data;

6625:   if (!mesh->globalVertexNumbers) {DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);}
6626:   *globalVertexNumbers = mesh->globalVertexNumbers;
6627:   return(0);
6628: }

6630: /*@
6631:   DMPlexCreatePointNumbering - Create a global numbering for all points on this process

6633:   Input Parameter:
6634: . dm   - The DMPlex object

6636:   Output Parameter:
6637: . globalPointNumbers - Global numbers for all points on this process

6639:   Level: developer

6641: .seealso DMPlexGetCellNumbering()
6642: @*/
6643: PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
6644: {
6645:   IS             nums[4];
6646:   PetscInt       depths[4], gdepths[4], starts[4];
6647:   PetscInt       depth, d, shift = 0;

6652:   DMPlexGetDepth(dm, &depth);
6653:   /* For unstratified meshes use dim instead of depth */
6654:   if (depth < 0) {DMGetDimension(dm, &depth);}
6655:   for (d = 0; d <= depth; ++d) {
6656:     PetscInt end;

6658:     depths[d] = depth-d;
6659:     DMPlexGetDepthStratum(dm, depths[d], &starts[d], &end);
6660:     if (!(starts[d]-end)) { starts[d] = depths[d] = -1; }
6661:   }
6662:   PetscSortIntWithArray(depth+1, starts, depths);
6663:   MPIU_Allreduce(depths, gdepths, depth+1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));
6664:   for (d = 0; d <= depth; ++d) {
6665:     if (starts[d] >= 0 && depths[d] != gdepths[d]) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expected depth %D, found %D",depths[d],gdepths[d]);
6666:   }
6667:   for (d = 0; d <= depth; ++d) {
6668:     PetscInt pStart, pEnd, gsize;

6670:     DMPlexGetDepthStratum(dm, gdepths[d], &pStart, &pEnd);
6671:     DMPlexCreateNumbering_Internal(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);
6672:     shift += gsize;
6673:   }
6674:   ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);
6675:   for (d = 0; d <= depth; ++d) {ISDestroy(&nums[d]);}
6676:   return(0);
6677: }


6680: /*@
6681:   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner

6683:   Input Parameter:
6684: . dm - The DMPlex object

6686:   Output Parameter:
6687: . ranks - The rank field

6689:   Options Database Keys:
6690: . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer

6692:   Level: intermediate

6694: .seealso: DMView()
6695: @*/
6696: PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
6697: {
6698:   DM             rdm;
6699:   PetscFE        fe;
6700:   PetscScalar   *r;
6701:   PetscMPIInt    rank;
6702:   PetscInt       dim, cStart, cEnd, c;

6708:   MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
6709:   DMClone(dm, &rdm);
6710:   DMGetDimension(rdm, &dim);
6711:   PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___rank_", -1, &fe);
6712:   PetscObjectSetName((PetscObject) fe, "rank");
6713:   DMSetField(rdm, 0, NULL, (PetscObject) fe);
6714:   PetscFEDestroy(&fe);
6715:   DMCreateDS(rdm);
6716:   DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);
6717:   DMCreateGlobalVector(rdm, ranks);
6718:   PetscObjectSetName((PetscObject) *ranks, "partition");
6719:   VecGetArray(*ranks, &r);
6720:   for (c = cStart; c < cEnd; ++c) {
6721:     PetscScalar *lr;

6723:     DMPlexPointGlobalRef(rdm, c, r, &lr);
6724:     *lr = rank;
6725:   }
6726:   VecRestoreArray(*ranks, &r);
6727:   DMDestroy(&rdm);
6728:   return(0);
6729: }

6731: /*@
6732:   DMPlexCreateLabelField - Create a cell field whose value is the label value for that cell

6734:   Input Parameters:
6735: + dm    - The DMPlex
6736: - label - The DMLabel

6738:   Output Parameter:
6739: . val - The label value field

6741:   Options Database Keys:
6742: . -dm_label_view - Adds the label value field into the DM output from -dm_view using the same viewer

6744:   Level: intermediate

6746: .seealso: DMView()
6747: @*/
6748: PetscErrorCode DMPlexCreateLabelField(DM dm, DMLabel label, Vec *val)
6749: {
6750:   DM             rdm;
6751:   PetscFE        fe;
6752:   PetscScalar   *v;
6753:   PetscInt       dim, cStart, cEnd, c;

6760:   DMClone(dm, &rdm);
6761:   DMGetDimension(rdm, &dim);
6762:   PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___label_value_", -1, &fe);
6763:   PetscObjectSetName((PetscObject) fe, "label_value");
6764:   DMSetField(rdm, 0, NULL, (PetscObject) fe);
6765:   PetscFEDestroy(&fe);
6766:   DMCreateDS(rdm);
6767:   DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);
6768:   DMCreateGlobalVector(rdm, val);
6769:   PetscObjectSetName((PetscObject) *val, "label_value");
6770:   VecGetArray(*val, &v);
6771:   for (c = cStart; c < cEnd; ++c) {
6772:     PetscScalar *lv;
6773:     PetscInt     cval;

6775:     DMPlexPointGlobalRef(rdm, c, v, &lv);
6776:     DMLabelGetValue(label, c, &cval);
6777:     *lv = cval;
6778:   }
6779:   VecRestoreArray(*val, &v);
6780:   DMDestroy(&rdm);
6781:   return(0);
6782: }

6784: /*@
6785:   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.

6787:   Input Parameter:
6788: . dm - The DMPlex object

6790:   Note: This is a useful diagnostic when creating meshes programmatically.

6792:   Level: developer

6794: .seealso: DMCreate(), DMPlexCheckSkeleton(), DMPlexCheckFaces()
6795: @*/
6796: PetscErrorCode DMPlexCheckSymmetry(DM dm)
6797: {
6798:   PetscSection    coneSection, supportSection;
6799:   const PetscInt *cone, *support;
6800:   PetscInt        coneSize, c, supportSize, s;
6801:   PetscInt        pStart, pEnd, p, pp, csize, ssize;
6802:   PetscBool       storagecheck = PETSC_TRUE;
6803:   PetscErrorCode  ierr;

6807:   DMPlexGetConeSection(dm, &coneSection);
6808:   DMPlexGetSupportSection(dm, &supportSection);
6809:   /* Check that point p is found in the support of its cone points, and vice versa */
6810:   DMPlexGetChart(dm, &pStart, &pEnd);
6811:   for (p = pStart; p < pEnd; ++p) {
6812:     DMPlexGetConeSize(dm, p, &coneSize);
6813:     DMPlexGetCone(dm, p, &cone);
6814:     for (c = 0; c < coneSize; ++c) {
6815:       PetscBool dup = PETSC_FALSE;
6816:       PetscInt  d;
6817:       for (d = c-1; d >= 0; --d) {
6818:         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
6819:       }
6820:       DMPlexGetSupportSize(dm, cone[c], &supportSize);
6821:       DMPlexGetSupport(dm, cone[c], &support);
6822:       for (s = 0; s < supportSize; ++s) {
6823:         if (support[s] == p) break;
6824:       }
6825:       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
6826:         PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);
6827:         for (s = 0; s < coneSize; ++s) {
6828:           PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);
6829:         }
6830:         PetscPrintf(PETSC_COMM_SELF, "\n");
6831:         PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);
6832:         for (s = 0; s < supportSize; ++s) {
6833:           PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);
6834:         }
6835:         PetscPrintf(PETSC_COMM_SELF, "\n");
6836:         if (dup) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not repeatedly found in support of repeated cone point %D", p, cone[c]);
6837:         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
6838:       }
6839:     }
6840:     DMPlexGetTreeParent(dm, p, &pp, NULL);
6841:     if (p != pp) { storagecheck = PETSC_FALSE; continue; }
6842:     DMPlexGetSupportSize(dm, p, &supportSize);
6843:     DMPlexGetSupport(dm, p, &support);
6844:     for (s = 0; s < supportSize; ++s) {
6845:       DMPlexGetConeSize(dm, support[s], &coneSize);
6846:       DMPlexGetCone(dm, support[s], &cone);
6847:       for (c = 0; c < coneSize; ++c) {
6848:         DMPlexGetTreeParent(dm, cone[c], &pp, NULL);
6849:         if (cone[c] != pp) { c = 0; break; }
6850:         if (cone[c] == p) break;
6851:       }
6852:       if (c >= coneSize) {
6853:         PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);
6854:         for (c = 0; c < supportSize; ++c) {
6855:           PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);
6856:         }
6857:         PetscPrintf(PETSC_COMM_SELF, "\n");
6858:         PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);
6859:         for (c = 0; c < coneSize; ++c) {
6860:           PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);
6861:         }
6862:         PetscPrintf(PETSC_COMM_SELF, "\n");
6863:         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
6864:       }
6865:     }
6866:   }
6867:   if (storagecheck) {
6868:     PetscSectionGetStorageSize(coneSection, &csize);
6869:     PetscSectionGetStorageSize(supportSection, &ssize);
6870:     if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
6871:   }
6872:   return(0);
6873: }

6875: /*@
6876:   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices

6878:   Input Parameters:
6879: + dm - The DMPlex object
6880: - cellHeight - Normally 0

6882:   Note: This is a useful diagnostic when creating meshes programmatically.
6883:   Currently applicable only to homogeneous simplex or tensor meshes.

6885:   Level: developer

6887: .seealso: DMCreate(), DMPlexCheckSymmetry(), DMPlexCheckFaces()
6888: @*/
6889: PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscInt cellHeight)
6890: {
6891:   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;
6892:   PetscBool      isSimplex = PETSC_FALSE;

6897:   DMGetDimension(dm, &dim);
6898:   DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
6899:   if (cStart < cEnd) {
6900:     DMPlexGetConeSize(dm, cStart, &c);
6901:     isSimplex = c == dim+1 ? PETSC_TRUE : PETSC_FALSE;
6902:   }
6903:   switch (dim) {
6904:   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
6905:   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
6906:   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
6907:   default:
6908:     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %D", dim);
6909:   }
6910:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
6911:   DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);
6912:   cMax = cMax >= 0 ? cMax : cEnd;
6913:   for (c = cStart; c < cMax; ++c) {
6914:     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;

6916:     DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
6917:     for (cl = 0; cl < closureSize*2; cl += 2) {
6918:       const PetscInt p = closure[cl];
6919:       if ((p >= vStart) && (p < vEnd)) ++coneSize;
6920:     }
6921:     DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
6922:     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has  %D vertices != %D", c, coneSize, numCorners);
6923:   }
6924:   for (c = cMax; c < cEnd; ++c) {
6925:     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;

6927:     DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
6928:     for (cl = 0; cl < closureSize*2; cl += 2) {
6929:       const PetscInt p = closure[cl];
6930:       if ((p >= vStart) && (p < vEnd)) ++coneSize;
6931:     }
6932:     DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
6933:     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %D has  %D vertices > %D", c, coneSize, numHybridCorners);
6934:   }
6935:   return(0);
6936: }

6938: /*@
6939:   DMPlexCheckFaces - Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type

6941:   Input Parameters:
6942: + dm - The DMPlex object
6943: - cellHeight - Normally 0

6945:   Note: This is a useful diagnostic when creating meshes programmatically.

6947:   Level: developer

6949: .seealso: DMCreate(), DMPlexCheckSymmetry(), DMPlexCheckSkeleton()
6950: @*/
6951: PetscErrorCode DMPlexCheckFaces(DM dm, PetscInt cellHeight)
6952: {
6953:   PetscInt       pMax[4];
6954:   PetscInt       dim, depth, vStart, vEnd, cStart, cEnd, c, h;

6959:   DMGetDimension(dm, &dim);
6960:   DMPlexGetDepth(dm, &depth);
6961:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
6962:   DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);
6963:   for (h = cellHeight; h < PetscMin(depth, dim); ++h) {
6964:     DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);
6965:     for (c = cStart; c < cEnd; ++c) {
6966:       const PetscInt *cone, *ornt, *faces;
6967:       PetscInt        numFaces, faceSize, coneSize,f;
6968:       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;

6970:       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
6971:       DMPlexGetConeSize(dm, c, &coneSize);
6972:       DMPlexGetCone(dm, c, &cone);
6973:       DMPlexGetConeOrientation(dm, c, &ornt);
6974:       DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
6975:       for (cl = 0; cl < closureSize*2; cl += 2) {
6976:         const PetscInt p = closure[cl];
6977:         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
6978:       }
6979:       DMPlexGetRawFaces_Internal(dm, dim-h, numCorners, closure, &numFaces, &faceSize, &faces);
6980:       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D faces but should have %D", c, coneSize, numFaces);
6981:       for (f = 0; f < numFaces; ++f) {
6982:         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;

6984:         DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);
6985:         for (cl = 0; cl < fclosureSize*2; cl += 2) {
6986:           const PetscInt p = fclosure[cl];
6987:           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
6988:         }
6989:         if (fnumCorners != faceSize) SETERRQ5(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D (%D) of cell %D has %D vertices but should have %D", cone[f], f, c, fnumCorners, faceSize);
6990:         for (v = 0; v < fnumCorners; ++v) {
6991:           if (fclosure[v] != faces[f*faceSize+v]) SETERRQ6(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D (%d) of cell %D vertex %D, %D != %D", cone[f], f, c, v, fclosure[v], faces[f*faceSize+v]);
6992:         }
6993:         DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);
6994:       }
6995:       DMPlexRestoreFaces_Internal(dm, dim, c, &numFaces, &faceSize, &faces);
6996:       DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
6997:     }
6998:   }
6999:   return(0);
7000: }

7002: /*@
7003:   DMPlexCheckGeometry - Check the geometry of mesh cells

7005:   Input Parameter:
7006: . dm - The DMPlex object

7008:   Note: This is a useful diagnostic when creating meshes programmatically.

7010:   Level: developer

7012: .seealso: DMCreate(), DMCheckSymmetry(), DMCheckSkeleton(), DMCheckFaces()
7013: @*/
7014: PetscErrorCode DMPlexCheckGeometry(DM dm)
7015: {
7016:   PetscReal      detJ, J[9], refVol = 1.0;
7017:   PetscReal      vol;
7018:   PetscInt       dim, depth, d, cStart, cEnd, c, cMax;

7022:   DMGetDimension(dm, &dim);
7023:   DMPlexGetDepth(dm, &depth);
7024:   for (d = 0; d < dim; ++d) refVol *= 2.0;
7025:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
7026:   DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);
7027:   cMax = cMax < 0 ? cEnd : cMax;
7028:   for (c = cStart; c < cMax; ++c) {
7029:     DMPlexComputeCellGeometryFEM(dm, c, NULL, NULL, J, NULL, &detJ);
7030:     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted, |J| = %g", c, (double) detJ);
7031:     PetscInfo2(dm, "Cell %D FEM Volume %g\n", c, (double) detJ*refVol);
7032:     if (depth > 1) {
7033:       DMPlexComputeCellGeometryFVM(dm, c, &vol, NULL, NULL);
7034:       if (vol <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %d is inverted, vol = %g", c, (double) vol);
7035:       PetscInfo2(dm, "Cell %D FVM Volume %g\n", c, (double) vol);
7036:     }
7037:   }
7038:   return(0);
7039: }

7041: static PetscErrorCode DMPlexAreAllConePointsInArray_Private(DM dm, PetscInt p, PetscInt npoints, const PetscInt *points, PetscInt *missingPoint)
7042: {
7043:   PetscInt i,l,n;
7044:   const PetscInt *cone;

7048:   *missingPoint = -1;
7049:   DMPlexGetConeSize(dm, p, &n);
7050:   DMPlexGetCone(dm, p, &cone);
7051:   for (i=0; i<n; i++) {
7052:     PetscFindInt(cone[i], npoints, points, &l);
7053:     if (l < 0) {
7054:       *missingPoint = cone[i];
7055:       break;
7056:     }
7057:   }
7058:   return(0);
7059: }

7061: /*@
7062:   DMPlexCheckPointSF - Check that several necessary conditions are met for the point SF of this plex.

7064:   Input Parameters:
7065: . dm - The DMPlex object

7067:   Notes:
7068:   This is mainly intended for debugging/testing purposes.

7070:   Level: developer

7072: .seealso: DMGetPointSF(), DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces()
7073: @*/
7074: PetscErrorCode DMPlexCheckPointSF(DM dm)
7075: {
7076:   PetscSF         sf;
7077:   PetscInt        d,depth,i,nleaves,p,plo,phi,missingPoint;
7078:   const PetscInt *locals;
7079:   PetscErrorCode  ierr;

7083:   DMPlexGetDepth(dm, &depth);
7084:   DMGetPointSF(dm, &sf);
7085:   PetscSFGetGraph(sf, NULL, &nleaves, &locals, NULL);

7087:   /* 1) check there are no faces in 2D, cells in 3D, in interface */
7088:   DMPlexGetVTKCellHeight(dm, &d);
7089:   DMPlexGetHeightStratum(dm, d, &plo, &phi);
7090:   for (i=0; i<nleaves; i++) {
7091:     p = locals[i];
7092:     if (p >= plo && p < phi) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "point SF contains %d which is a cell",p);
7093:   }

7095:   /* 2) if some point is in interface, then all its cone points must be also in interface  */
7096:   for (i=0; i<nleaves; i++) {
7097:     p = locals[i];
7098:     DMPlexAreAllConePointsInArray_Private(dm, p, nleaves, locals, &missingPoint);
7099:     if (missingPoint >= 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "point SF contains %d but not %d from its cone",p,missingPoint);
7100:   }
7101:   return(0);
7102: }

7104: typedef struct cell_stats
7105: {
7106:   PetscReal min, max, sum, squaresum;
7107:   PetscInt  count;
7108: } cell_stats_t;

7110: static void MPIAPI cell_stats_reduce(void *a, void *b, int * len, MPI_Datatype *datatype)
7111: {
7112:   PetscInt i, N = *len;

7114:   for (i = 0; i < N; i++) {
7115:     cell_stats_t *A = (cell_stats_t *) a;
7116:     cell_stats_t *B = (cell_stats_t *) b;

7118:     B->min = PetscMin(A->min,B->min);
7119:     B->max = PetscMax(A->max,B->max);
7120:     B->sum += A->sum;
7121:     B->squaresum += A->squaresum;
7122:     B->count += A->count;
7123:   }
7124: }

7126: /*@
7127:   DMPlexCheckCellShape - Checks the Jacobian of the mapping and computes some minimal statistics.

7129:   Input Parameters:
7130: + dm - The DMPlex object
7131: - output - If true, statistics will be displayed on stdout

7133:   Note: This is mainly intended for debugging/testing purposes.

7135:   Level: developer

7137: .seealso: DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces()
7138: @*/
7139: PetscErrorCode DMPlexCheckCellShape(DM dm, PetscBool output)
7140: {
7141:   PetscMPIInt    rank,size;
7142:   PetscInt       dim, c, cStart, cEnd, cMax, count = 0;
7143:   cell_stats_t   stats, globalStats;
7144:   PetscReal      *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
7145:   MPI_Comm       comm = PetscObjectComm((PetscObject)dm);
7146:   DM             dmCoarse;

7151:   stats.min   = PETSC_MAX_REAL;
7152:   stats.max   = PETSC_MIN_REAL;
7153:   stats.sum   = stats.squaresum = 0.;
7154:   stats.count = 0;

7156:   DMGetCoordinateDim(dm,&dim);
7157:   PetscMalloc2(dim * dim, &J, dim * dim, &invJ);
7158:   DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);
7159:   DMPlexGetHybridBounds(dm,&cMax,NULL,NULL,NULL);
7160:   cMax = cMax < 0 ? cEnd : cMax;
7161:   for (c = cStart; c < cMax; c++) {
7162:     PetscInt  i;
7163:     PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;

7165:     DMPlexComputeCellGeometryAffineFEM(dm,c,NULL,J,invJ,&detJ);
7166:     if (detJ < 0.0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted", c);
7167:     for (i = 0; i < dim * dim; i++) {
7168:       frobJ    += J[i] * J[i];
7169:       frobInvJ += invJ[i] * invJ[i];
7170:     }
7171:     cond2 = frobJ * frobInvJ;
7172:     cond  = PetscSqrtReal(cond2);

7174:     stats.min        = PetscMin(stats.min,cond);
7175:     stats.max        = PetscMax(stats.max,cond);
7176:     stats.sum       += cond;
7177:     stats.squaresum += cond2;
7178:     stats.count++;
7179:   }

7181:   MPI_Comm_size(comm,&size);
7182:   if (size > 1) {
7183:     PetscMPIInt   blockLengths[2] = {4,1};
7184:     MPI_Aint      blockOffsets[2] = {offsetof(cell_stats_t,min),offsetof(cell_stats_t,count)};
7185:     MPI_Datatype  blockTypes[2]   = {MPIU_REAL,MPIU_INT}, statType;
7186:     MPI_Op        statReduce;

7188:     MPI_Type_create_struct(2,blockLengths,blockOffsets,blockTypes,&statType);
7189:     MPI_Type_commit(&statType);
7190:     MPI_Op_create(cell_stats_reduce, PETSC_TRUE, &statReduce);
7191:     MPI_Reduce(&stats,&globalStats,1,statType,statReduce,0,comm);
7192:     MPI_Op_free(&statReduce);
7193:     MPI_Type_free(&statType);
7194:   } else {
7195:     PetscArraycpy(&globalStats,&stats,1);
7196:   }

7198:   MPI_Comm_rank(comm,&rank);
7199:   if (!rank) {
7200:     count = globalStats.count;
7201:     min   = globalStats.min;
7202:     max   = globalStats.max;
7203:     mean  = globalStats.sum / globalStats.count;
7204:     stdev = globalStats.count > 1 ? PetscSqrtReal(PetscMax((globalStats.squaresum - globalStats.count * mean * mean) / (globalStats.count - 1),0)) : 0.0;
7205:   }

7207:   if (output) {
7208:     PetscPrintf(comm,"Mesh with %D cells, shape condition numbers: min = %g, max = %g, mean = %g, stddev = %g\n", count, (double) min, (double) max, (double) mean, (double) stdev);
7209:   }
7210:   PetscFree2(J,invJ);

7212:   DMGetCoarseDM(dm,&dmCoarse);
7213:   if (dmCoarse) {
7214:     PetscBool isplex;

7216:     PetscObjectTypeCompare((PetscObject)dmCoarse,DMPLEX,&isplex);
7217:     if (isplex) {
7218:       DMPlexCheckCellShape(dmCoarse,output);
7219:     }
7220:   }
7221:   return(0);
7222: }

7224: /* Pointwise interpolation
7225:      Just code FEM for now
7226:      u^f = I u^c
7227:      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
7228:      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
7229:      I_{ij} = psi^f_i phi^c_j
7230: */
7231: PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
7232: {
7233:   PetscSection   gsc, gsf;
7234:   PetscInt       m, n;
7235:   void          *ctx;
7236:   DM             cdm;
7237:   PetscBool      regular, ismatis;

7241:   DMGetGlobalSection(dmFine, &gsf);
7242:   PetscSectionGetConstrainedStorageSize(gsf, &m);
7243:   DMGetGlobalSection(dmCoarse, &gsc);
7244:   PetscSectionGetConstrainedStorageSize(gsc, &n);

7246:   PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis);
7247:   MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);
7248:   MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);
7249:   MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype);
7250:   DMGetApplicationContext(dmFine, &ctx);

7252:   DMGetCoarseDM(dmFine, &cdm);
7253:   DMPlexGetRegularRefinement(dmFine, &regular);
7254:   if (regular && cdm == dmCoarse) {DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);}
7255:   else                            {DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);}
7256:   MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");
7257:   if (scaling) {
7258:     /* Use naive scaling */
7259:     DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);
7260:   }
7261:   return(0);
7262: }

7264: PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
7265: {
7267:   VecScatter     ctx;

7270:   DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);
7271:   MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);
7272:   VecScatterDestroy(&ctx);
7273:   return(0);
7274: }

7276: PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
7277: {
7278:   PetscSection   gsc, gsf;
7279:   PetscInt       m, n;
7280:   void          *ctx;
7281:   DM             cdm;
7282:   PetscBool      regular;

7286:   DMGetGlobalSection(dmFine, &gsf);
7287:   PetscSectionGetConstrainedStorageSize(gsf, &m);
7288:   DMGetGlobalSection(dmCoarse, &gsc);
7289:   PetscSectionGetConstrainedStorageSize(gsc, &n);

7291:   MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);
7292:   MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);
7293:   MatSetType(*mass, dmCoarse->mattype);
7294:   DMGetApplicationContext(dmFine, &ctx);

7296:   DMGetCoarseDM(dmFine, &cdm);
7297:   DMPlexGetRegularRefinement(dmFine, &regular);
7298:   if (regular && cdm == dmCoarse) {DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);}
7299:   else                            {DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);}
7300:   MatViewFromOptions(*mass, NULL, "-mass_mat_view");
7301:   return(0);
7302: }

7304: /*@
7305:   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh

7307:   Input Parameter:
7308: . dm - The DMPlex object

7310:   Output Parameter:
7311: . regular - The flag

7313:   Level: intermediate

7315: .seealso: DMPlexSetRegularRefinement()
7316: @*/
7317: PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
7318: {
7322:   *regular = ((DM_Plex *) dm->data)->regularRefinement;
7323:   return(0);
7324: }

7326: /*@
7327:   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh

7329:   Input Parameters:
7330: + dm - The DMPlex object
7331: - regular - The flag

7333:   Level: intermediate

7335: .seealso: DMPlexGetRegularRefinement()
7336: @*/
7337: PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
7338: {
7341:   ((DM_Plex *) dm->data)->regularRefinement = regular;
7342:   return(0);
7343: }

7345: /* anchors */
7346: /*@
7347:   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
7348:   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().

7350:   not collective

7352:   Input Parameters:
7353: . dm - The DMPlex object

7355:   Output Parameters:
7356: + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
7357: - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection


7360:   Level: intermediate

7362: .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
7363: @*/
7364: PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
7365: {
7366:   DM_Plex *plex = (DM_Plex *)dm->data;

7371:   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {(*plex->createanchors)(dm);}
7372:   if (anchorSection) *anchorSection = plex->anchorSection;
7373:   if (anchorIS) *anchorIS = plex->anchorIS;
7374:   return(0);
7375: }

7377: /*@
7378:   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
7379:   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
7380:   point's degrees of freedom to be a linear combination of other points' degrees of freedom.

7382:   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
7383:   DMGetConstraints() and filling in the entries in the constraint matrix.

7385:   collective on dm

7387:   Input Parameters:
7388: + dm - The DMPlex object
7389: . anchorSection - The section that describes the mapping from constrained points to the anchor points listed in anchorIS.  Must have a local communicator (PETSC_COMM_SELF or derivative).
7390: - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).

7392:   The reference counts of anchorSection and anchorIS are incremented.

7394:   Level: intermediate

7396: .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
7397: @*/
7398: PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
7399: {
7400:   DM_Plex        *plex = (DM_Plex *)dm->data;
7401:   PetscMPIInt    result;

7406:   if (anchorSection) {
7408:     MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);
7409:     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
7410:   }
7411:   if (anchorIS) {
7413:     MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);
7414:     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
7415:   }

7417:   PetscObjectReference((PetscObject)anchorSection);
7418:   PetscSectionDestroy(&plex->anchorSection);
7419:   plex->anchorSection = anchorSection;

7421:   PetscObjectReference((PetscObject)anchorIS);
7422:   ISDestroy(&plex->anchorIS);
7423:   plex->anchorIS = anchorIS;

7425: #if defined(PETSC_USE_DEBUG)
7426:   if (anchorIS && anchorSection) {
7427:     PetscInt size, a, pStart, pEnd;
7428:     const PetscInt *anchors;

7430:     PetscSectionGetChart(anchorSection,&pStart,&pEnd);
7431:     ISGetLocalSize(anchorIS,&size);
7432:     ISGetIndices(anchorIS,&anchors);
7433:     for (a = 0; a < size; a++) {
7434:       PetscInt p;

7436:       p = anchors[a];
7437:       if (p >= pStart && p < pEnd) {
7438:         PetscInt dof;

7440:         PetscSectionGetDof(anchorSection,p,&dof);
7441:         if (dof) {
7442:           PetscErrorCode ierr2;

7444:           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
7445:           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
7446:         }
7447:       }
7448:     }
7449:     ISRestoreIndices(anchorIS,&anchors);
7450:   }
7451: #endif
7452:   /* reset the generic constraints */
7453:   DMSetDefaultConstraints(dm,NULL,NULL);
7454:   return(0);
7455: }

7457: static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
7458: {
7459:   PetscSection anchorSection;
7460:   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;

7465:   DMPlexGetAnchors(dm,&anchorSection,NULL);
7466:   PetscSectionCreate(PETSC_COMM_SELF,cSec);
7467:   PetscSectionGetNumFields(section,&numFields);
7468:   if (numFields) {
7469:     PetscInt f;
7470:     PetscSectionSetNumFields(*cSec,numFields);

7472:     for (f = 0; f < numFields; f++) {
7473:       PetscInt numComp;

7475:       PetscSectionGetFieldComponents(section,f,&numComp);
7476:       PetscSectionSetFieldComponents(*cSec,f,numComp);
7477:     }
7478:   }
7479:   PetscSectionGetChart(anchorSection,&pStart,&pEnd);
7480:   PetscSectionGetChart(section,&sStart,&sEnd);
7481:   pStart = PetscMax(pStart,sStart);
7482:   pEnd   = PetscMin(pEnd,sEnd);
7483:   pEnd   = PetscMax(pStart,pEnd);
7484:   PetscSectionSetChart(*cSec,pStart,pEnd);
7485:   for (p = pStart; p < pEnd; p++) {
7486:     PetscSectionGetDof(anchorSection,p,&dof);
7487:     if (dof) {
7488:       PetscSectionGetDof(section,p,&dof);
7489:       PetscSectionSetDof(*cSec,p,dof);
7490:       for (f = 0; f < numFields; f++) {
7491:         PetscSectionGetFieldDof(section,p,f,&dof);
7492:         PetscSectionSetFieldDof(*cSec,p,f,dof);
7493:       }
7494:     }
7495:   }
7496:   PetscSectionSetUp(*cSec);
7497:   return(0);
7498: }

7500: static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
7501: {
7502:   PetscSection aSec;
7503:   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
7504:   const PetscInt *anchors;
7505:   PetscInt numFields, f;
7506:   IS aIS;

7511:   PetscSectionGetStorageSize(cSec, &m);
7512:   PetscSectionGetStorageSize(section, &n);
7513:   MatCreate(PETSC_COMM_SELF,cMat);
7514:   MatSetSizes(*cMat,m,n,m,n);
7515:   MatSetType(*cMat,MATSEQAIJ);
7516:   DMPlexGetAnchors(dm,&aSec,&aIS);
7517:   ISGetIndices(aIS,&anchors);
7518:   /* cSec will be a subset of aSec and section */
7519:   PetscSectionGetChart(cSec,&pStart,&pEnd);
7520:   PetscMalloc1(m+1,&i);
7521:   i[0] = 0;
7522:   PetscSectionGetNumFields(section,&numFields);
7523:   for (p = pStart; p < pEnd; p++) {
7524:     PetscInt rDof, rOff, r;

7526:     PetscSectionGetDof(aSec,p,&rDof);
7527:     if (!rDof) continue;
7528:     PetscSectionGetOffset(aSec,p,&rOff);
7529:     if (numFields) {
7530:       for (f = 0; f < numFields; f++) {
7531:         annz = 0;
7532:         for (r = 0; r < rDof; r++) {
7533:           a = anchors[rOff + r];
7534:           PetscSectionGetFieldDof(section,a,f,&aDof);
7535:           annz += aDof;
7536:         }
7537:         PetscSectionGetFieldDof(cSec,p,f,&dof);
7538:         PetscSectionGetFieldOffset(cSec,p,f,&off);
7539:         for (q = 0; q < dof; q++) {
7540:           i[off + q + 1] = i[off + q] + annz;
7541:         }
7542:       }
7543:     }
7544:     else {
7545:       annz = 0;
7546:       for (q = 0; q < dof; q++) {
7547:         a = anchors[off + q];
7548:         PetscSectionGetDof(section,a,&aDof);
7549:         annz += aDof;
7550:       }
7551:       PetscSectionGetDof(cSec,p,&dof);
7552:       PetscSectionGetOffset(cSec,p,&off);
7553:       for (q = 0; q < dof; q++) {
7554:         i[off + q + 1] = i[off + q] + annz;
7555:       }
7556:     }
7557:   }
7558:   nnz = i[m];
7559:   PetscMalloc1(nnz,&j);
7560:   offset = 0;
7561:   for (p = pStart; p < pEnd; p++) {
7562:     if (numFields) {
7563:       for (f = 0; f < numFields; f++) {
7564:         PetscSectionGetFieldDof(cSec,p,f,&dof);
7565:         for (q = 0; q < dof; q++) {
7566:           PetscInt rDof, rOff, r;
7567:           PetscSectionGetDof(aSec,p,&rDof);
7568:           PetscSectionGetOffset(aSec,p,&rOff);
7569:           for (r = 0; r < rDof; r++) {
7570:             PetscInt s;

7572:             a = anchors[rOff + r];
7573:             PetscSectionGetFieldDof(section,a,f,&aDof);
7574:             PetscSectionGetFieldOffset(section,a,f,&aOff);
7575:             for (s = 0; s < aDof; s++) {
7576:               j[offset++] = aOff + s;
7577:             }
7578:           }
7579:         }
7580:       }
7581:     }
7582:     else {
7583:       PetscSectionGetDof(cSec,p,&dof);
7584:       for (q = 0; q < dof; q++) {
7585:         PetscInt rDof, rOff, r;
7586:         PetscSectionGetDof(aSec,p,&rDof);
7587:         PetscSectionGetOffset(aSec,p,&rOff);
7588:         for (r = 0; r < rDof; r++) {
7589:           PetscInt s;

7591:           a = anchors[rOff + r];
7592:           PetscSectionGetDof(section,a,&aDof);
7593:           PetscSectionGetOffset(section,a,&aOff);
7594:           for (s = 0; s < aDof; s++) {
7595:             j[offset++] = aOff + s;
7596:           }
7597:         }
7598:       }
7599:     }
7600:   }
7601:   MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);
7602:   PetscFree(i);
7603:   PetscFree(j);
7604:   ISRestoreIndices(aIS,&anchors);
7605:   return(0);
7606: }

7608: PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
7609: {
7610:   DM_Plex        *plex = (DM_Plex *)dm->data;
7611:   PetscSection   anchorSection, section, cSec;
7612:   Mat            cMat;

7617:   DMPlexGetAnchors(dm,&anchorSection,NULL);
7618:   if (anchorSection) {
7619:     PetscInt Nf;

7621:     DMGetLocalSection(dm,&section);
7622:     DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);
7623:     DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);
7624:     DMGetNumFields(dm,&Nf);
7625:     if (Nf && plex->computeanchormatrix) {(*plex->computeanchormatrix)(dm,section,cSec,cMat);}
7626:     DMSetDefaultConstraints(dm,cSec,cMat);
7627:     PetscSectionDestroy(&cSec);
7628:     MatDestroy(&cMat);
7629:   }
7630:   return(0);
7631: }

7633: PetscErrorCode DMCreateSubDomainDM_Plex(DM dm, DMLabel label, PetscInt value, IS *is, DM *subdm)
7634: {
7635:   IS             subis;
7636:   PetscSection   section, subsection;

7640:   DMGetLocalSection(dm, &section);
7641:   if (!section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting subdomain");
7642:   if (!subdm)   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set output subDM for splitting subdomain");
7643:   /* Create subdomain */
7644:   DMPlexFilter(dm, label, value, subdm);
7645:   /* Create submodel */
7646:   DMPlexCreateSubpointIS(*subdm, &subis);
7647:   PetscSectionCreateSubmeshSection(section, subis, &subsection);
7648:   ISDestroy(&subis);
7649:   DMSetLocalSection(*subdm, subsection);
7650:   PetscSectionDestroy(&subsection);
7651:   DMCopyDisc(dm, *subdm);
7652:   /* Create map from submodel to global model */
7653:   if (is) {
7654:     PetscSection    sectionGlobal, subsectionGlobal;
7655:     IS              spIS;
7656:     const PetscInt *spmap;
7657:     PetscInt       *subIndices;
7658:     PetscInt        subSize = 0, subOff = 0, pStart, pEnd, p;
7659:     PetscInt        Nf, f, bs = -1, bsLocal[2], bsMinMax[2];

7661:     DMPlexCreateSubpointIS(*subdm, &spIS);
7662:     ISGetIndices(spIS, &spmap);
7663:     PetscSectionGetNumFields(section, &Nf);
7664:     DMGetGlobalSection(dm, &sectionGlobal);
7665:     DMGetGlobalSection(*subdm, &subsectionGlobal);
7666:     PetscSectionGetChart(subsection, &pStart, &pEnd);
7667:     for (p = pStart; p < pEnd; ++p) {
7668:       PetscInt gdof, pSubSize  = 0;

7670:       PetscSectionGetDof(sectionGlobal, p, &gdof);
7671:       if (gdof > 0) {
7672:         for (f = 0; f < Nf; ++f) {
7673:           PetscInt fdof, fcdof;

7675:           PetscSectionGetFieldDof(subsection, p, f, &fdof);
7676:           PetscSectionGetFieldConstraintDof(subsection, p, f, &fcdof);
7677:           pSubSize += fdof-fcdof;
7678:         }
7679:         subSize += pSubSize;
7680:         if (pSubSize) {
7681:           if (bs < 0) {
7682:             bs = pSubSize;
7683:           } else if (bs != pSubSize) {
7684:             /* Layout does not admit a pointwise block size */
7685:             bs = 1;
7686:           }
7687:         }
7688:       }
7689:     }
7690:     /* Must have same blocksize on all procs (some might have no points) */
7691:     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
7692:     PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);
7693:     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
7694:     else                            {bs = bsMinMax[0];}
7695:     PetscMalloc1(subSize, &subIndices);
7696:     for (p = pStart; p < pEnd; ++p) {
7697:       PetscInt gdof, goff;

7699:       PetscSectionGetDof(subsectionGlobal, p, &gdof);
7700:       if (gdof > 0) {
7701:         const PetscInt point = spmap[p];

7703:         PetscSectionGetOffset(sectionGlobal, point, &goff);
7704:         for (f = 0; f < Nf; ++f) {
7705:           PetscInt fdof, fcdof, fc, f2, poff = 0;

7707:           /* Can get rid of this loop by storing field information in the global section */
7708:           for (f2 = 0; f2 < f; ++f2) {
7709:             PetscSectionGetFieldDof(section, p, f2, &fdof);
7710:             PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);
7711:             poff += fdof-fcdof;
7712:           }
7713:           PetscSectionGetFieldDof(section, p, f, &fdof);
7714:           PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);
7715:           for (fc = 0; fc < fdof-fcdof; ++fc, ++subOff) {
7716:             subIndices[subOff] = goff+poff+fc;
7717:           }
7718:         }
7719:       }
7720:     }
7721:     ISRestoreIndices(spIS, &spmap);
7722:     ISDestroy(&spIS);
7723:     ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);
7724:     if (bs > 1) {
7725:       /* We need to check that the block size does not come from non-contiguous fields */
7726:       PetscInt i, j, set = 1;
7727:       for (i = 0; i < subSize; i += bs) {
7728:         for (j = 0; j < bs; ++j) {
7729:           if (subIndices[i+j] != subIndices[i]+j) {set = 0; break;}
7730:         }
7731:       }
7732:       if (set) {ISSetBlockSize(*is, bs);}
7733:     }
7734:     /* Attach nullspace */
7735:     for (f = 0; f < Nf; ++f) {
7736:       (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[f];
7737:       if ((*subdm)->nullspaceConstructors[f]) break;
7738:     }
7739:     if (f < Nf) {
7740:       MatNullSpace nullSpace;

7742:       (*(*subdm)->nullspaceConstructors[f])(*subdm, f, &nullSpace);
7743:       PetscObjectCompose((PetscObject) *is, "nullspace", (PetscObject) nullSpace);
7744:       MatNullSpaceDestroy(&nullSpace);
7745:     }
7746:   }
7747:   return(0);
7748: }