Actual source code: plex.c

petsc-master 2021-02-25
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,DMPLEX_LocatePoints;

 13: PETSC_EXTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);

 15: /*@
 16:   DMPlexGetSimplexOrBoxCells - Get the range of cells which are neither prisms nor ghost FV cells

 18:   Input Parameter:
 19: + dm     - The DMPlex object
 20: - height - The cell height in the Plex, 0 is the default

 22:   Output Parameters:
 23: + cStart - The first "normal" cell
 24: - cEnd   - The upper bound on "normal"" cells

 26:   Note: This just gives the first range of cells found. If the mesh has several cell types, it will only give the first.

 28:   Level: developer

 30: .seealso DMPlexConstructGhostCells(), DMPlexGetGhostCellStratum()
 31: @*/
 32: PetscErrorCode DMPlexGetSimplexOrBoxCells(DM dm, PetscInt height, PetscInt *cStart, PetscInt *cEnd)
 33: {
 34:   DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
 35:   PetscInt       cS, cE, c;

 39:   DMPlexGetHeightStratum(dm, PetscMax(height, 0), &cS, &cE);
 40:   for (c = cS; c < cE; ++c) {
 41:     DMPolytopeType cct;

 43:     DMPlexGetCellType(dm, c, &cct);
 44:     if ((PetscInt) cct < 0) break;
 45:     switch (cct) {
 46:       case DM_POLYTOPE_POINT:
 47:       case DM_POLYTOPE_SEGMENT:
 48:       case DM_POLYTOPE_TRIANGLE:
 49:       case DM_POLYTOPE_QUADRILATERAL:
 50:       case DM_POLYTOPE_TETRAHEDRON:
 51:       case DM_POLYTOPE_HEXAHEDRON:
 52:         ct = cct;
 53:         break;
 54:       default: break;
 55:     }
 56:     if (ct != DM_POLYTOPE_UNKNOWN) break;
 57:   }
 58:   if (ct != DM_POLYTOPE_UNKNOWN) {
 59:     DMLabel ctLabel;

 61:     DMPlexGetCellTypeLabel(dm, &ctLabel);
 62:     DMLabelGetStratumBounds(ctLabel, ct, &cS, &cE);
 63:   }
 64:   if (cStart) *cStart = cS;
 65:   if (cEnd)   *cEnd   = cE;
 66:   return(0);
 67: }

 69: PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
 70: {
 71:   PetscInt       cdim, pStart, pEnd, vStart, vEnd, cStart, cEnd;
 72:   PetscInt       vcdof[2] = {0,0}, globalvcdof[2];

 76:   *ft  = PETSC_VTK_INVALID;
 77:   DMGetCoordinateDim(dm, &cdim);
 78:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
 79:   DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);
 80:   PetscSectionGetChart(section, &pStart, &pEnd);
 81:   if (field >= 0) {
 82:     if ((vStart >= pStart) && (vStart < pEnd)) {PetscSectionGetFieldDof(section, vStart, field, &vcdof[0]);}
 83:     if ((cStart >= pStart) && (cStart < pEnd)) {PetscSectionGetFieldDof(section, cStart, field, &vcdof[1]);}
 84:   } else {
 85:     if ((vStart >= pStart) && (vStart < pEnd)) {PetscSectionGetDof(section, vStart, &vcdof[0]);}
 86:     if ((cStart >= pStart) && (cStart < pEnd)) {PetscSectionGetDof(section, cStart, &vcdof[1]);}
 87:   }
 88:   MPI_Allreduce(vcdof, globalvcdof, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));
 89:   if (globalvcdof[0]) {
 90:     *sStart = vStart;
 91:     *sEnd   = vEnd;
 92:     if (globalvcdof[0] == cdim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
 93:     else                        *ft = PETSC_VTK_POINT_FIELD;
 94:   } else if (globalvcdof[1]) {
 95:     *sStart = cStart;
 96:     *sEnd   = cEnd;
 97:     if (globalvcdof[1] == cdim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
 98:     else                        *ft = PETSC_VTK_CELL_FIELD;
 99:   } else {
100:     if (field >= 0) {
101:       const char *fieldname;

103:       PetscSectionGetFieldName(section, field, &fieldname);
104:       PetscInfo2((PetscObject) dm, "Could not classify VTK output type of section field %D \"%s\"\n", field, fieldname);
105:     } else {
106:       PetscInfo((PetscObject) dm, "Could not classify VTK output typp of section\"%s\"\n");
107:     }
108:   }
109:   return(0);
110: }

112: static PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
113: {
114:   DM                 dm;
115:   PetscSection       s;
116:   PetscDraw          draw, popup;
117:   DM                 cdm;
118:   PetscSection       coordSection;
119:   Vec                coordinates;
120:   const PetscScalar *coords, *array;
121:   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
122:   PetscReal          vbound[2], time;
123:   PetscBool          isnull, flg;
124:   PetscInt           dim, Nf, f, Nc, comp, vStart, vEnd, cStart, cEnd, c, N, level, step, w = 0;
125:   const char        *name;
126:   char               title[PETSC_MAX_PATH_LEN];
127:   PetscErrorCode     ierr;

130:   PetscViewerDrawGetDraw(viewer, 0, &draw);
131:   PetscDrawIsNull(draw, &isnull);
132:   if (isnull) return(0);

134:   VecGetDM(v, &dm);
135:   DMGetCoordinateDim(dm, &dim);
136:   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D. Use PETSCVIEWERGLVIS", dim);
137:   DMGetLocalSection(dm, &s);
138:   PetscSectionGetNumFields(s, &Nf);
139:   DMGetCoarsenLevel(dm, &level);
140:   DMGetCoordinateDM(dm, &cdm);
141:   DMGetLocalSection(cdm, &coordSection);
142:   DMGetCoordinatesLocal(dm, &coordinates);
143:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
144:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);

146:   PetscObjectGetName((PetscObject) v, &name);
147:   DMGetOutputSequenceNumber(dm, &step, &time);

149:   VecGetLocalSize(coordinates, &N);
150:   VecGetArrayRead(coordinates, &coords);
151:   for (c = 0; c < N; c += dim) {
152:     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
153:     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
154:   }
155:   VecRestoreArrayRead(coordinates, &coords);
156:   PetscDrawClear(draw);

158:   /* Could implement something like DMDASelectFields() */
159:   for (f = 0; f < Nf; ++f) {
160:     DM   fdm = dm;
161:     Vec  fv  = v;
162:     IS   fis;
163:     char prefix[PETSC_MAX_PATH_LEN];
164:     const char *fname;

166:     PetscSectionGetFieldComponents(s, f, &Nc);
167:     PetscSectionGetFieldName(s, f, &fname);

169:     if (v->hdr.prefix) {PetscStrncpy(prefix, v->hdr.prefix,sizeof(prefix));}
170:     else               {prefix[0] = '\0';}
171:     if (Nf > 1) {
172:       DMCreateSubDM(dm, 1, &f, &fis, &fdm);
173:       VecGetSubVector(v, fis, &fv);
174:       PetscStrlcat(prefix, fname,sizeof(prefix));
175:       PetscStrlcat(prefix, "_",sizeof(prefix));
176:     }
177:     for (comp = 0; comp < Nc; ++comp, ++w) {
178:       PetscInt nmax = 2;

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

185:       /* TODO Get max and min only for this component */
186:       PetscOptionsGetRealArray(NULL, prefix, "-vec_view_bounds", vbound, &nmax, &flg);
187:       if (!flg) {
188:         VecMin(fv, NULL, &vbound[0]);
189:         VecMax(fv, NULL, &vbound[1]);
190:         if (vbound[1] <= vbound[0]) vbound[1] = vbound[0] + 1.0;
191:       }
192:       PetscDrawGetPopup(draw, &popup);
193:       PetscDrawScalePopup(popup, vbound[0], vbound[1]);
194:       PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);

196:       VecGetArrayRead(fv, &array);
197:       for (c = cStart; c < cEnd; ++c) {
198:         PetscScalar *coords = NULL, *a = NULL;
199:         PetscInt     numCoords, color[4] = {-1,-1,-1,-1};

201:         DMPlexPointLocalRead(fdm, c, array, &a);
202:         if (a) {
203:           color[0] = PetscDrawRealToColor(PetscRealPart(a[comp]), vbound[0], vbound[1]);
204:           color[1] = color[2] = color[3] = color[0];
205:         } else {
206:           PetscScalar *vals = NULL;
207:           PetscInt     numVals, va;

209:           DMPlexVecGetClosure(fdm, NULL, fv, c, &numVals, &vals);
210:           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);
211:           switch (numVals/Nc) {
212:           case 3: /* P1 Triangle */
213:           case 4: /* P1 Quadrangle */
214:             for (va = 0; va < numVals/Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp]), vbound[0], vbound[1]);
215:             break;
216:           case 6: /* P2 Triangle */
217:           case 8: /* P2 Quadrangle */
218:             for (va = 0; va < numVals/(Nc*2); ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp + numVals/(Nc*2)]), vbound[0], vbound[1]);
219:             break;
220:           default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of values for cell closure %D cannot be handled", numVals/Nc);
221:           }
222:           DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals);
223:         }
224:         DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);
225:         switch (numCoords) {
226:         case 6:
227:           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]);
228:           break;
229:         case 8:
230:           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]);
231:           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]);
232:           break;
233:         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
234:         }
235:         DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);
236:       }
237:       VecRestoreArrayRead(fv, &array);
238:       PetscDrawFlush(draw);
239:       PetscDrawPause(draw);
240:       PetscDrawSave(draw);
241:     }
242:     if (Nf > 1) {
243:       VecRestoreSubVector(v, fis, &fv);
244:       ISDestroy(&fis);
245:       DMDestroy(&fdm);
246:     }
247:   }
248:   return(0);
249: }

251: static PetscErrorCode VecView_Plex_Local_VTK(Vec v, PetscViewer viewer)
252: {
253:   DM                      dm;
254:   Vec                     locv;
255:   const char              *name;
256:   PetscSection            section;
257:   PetscInt                pStart, pEnd;
258:   PetscInt                numFields;
259:   PetscViewerVTKFieldType ft;
260:   PetscErrorCode          ierr;

263:   VecGetDM(v, &dm);
264:   DMCreateLocalVector(dm, &locv); /* VTK viewer requires exclusive ownership of the vector */
265:   PetscObjectGetName((PetscObject) v, &name);
266:   PetscObjectSetName((PetscObject) locv, name);
267:   VecCopy(v, locv);
268:   DMGetLocalSection(dm, &section);
269:   PetscSectionGetNumFields(section, &numFields);
270:   if (!numFields) {
271:     DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);
272:     PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, PETSC_DEFAULT, ft, PETSC_TRUE,(PetscObject) locv);
273:   } else {
274:     PetscInt f;

276:     for (f = 0; f < numFields; f++) {
277:       DMPlexGetFieldType_Internal(dm, section, f, &pStart, &pEnd, &ft);
278:       if (ft == PETSC_VTK_INVALID) continue;
279:       PetscObjectReference((PetscObject)locv);
280:       PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, f, ft, PETSC_TRUE,(PetscObject) locv);
281:     }
282:     VecDestroy(&locv);
283:   }
284:   return(0);
285: }

287: PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
288: {
289:   DM             dm;
290:   PetscBool      isvtk, ishdf5, isdraw, isglvis;

294:   VecGetDM(v, &dm);
295:   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
296:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);
297:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);
298:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);
299:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);
300:   if (isvtk || ishdf5 || isdraw || isglvis) {
301:     PetscInt    i,numFields;
302:     PetscObject fe;
303:     PetscBool   fem = PETSC_FALSE;
304:     Vec         locv = v;
305:     const char  *name;
306:     PetscInt    step;
307:     PetscReal   time;

309:     DMGetNumFields(dm, &numFields);
310:     for (i=0; i<numFields; i++) {
311:       DMGetField(dm, i, NULL, &fe);
312:       if (fe->classid == PETSCFE_CLASSID) { fem = PETSC_TRUE; break; }
313:     }
314:     if (fem) {
315:       PetscObject isZero;

317:       DMGetLocalVector(dm, &locv);
318:       PetscObjectGetName((PetscObject) v, &name);
319:       PetscObjectSetName((PetscObject) locv, name);
320:       PetscObjectQuery((PetscObject) v, "__Vec_bc_zero__", &isZero);
321:       PetscObjectCompose((PetscObject) locv, "__Vec_bc_zero__", isZero);
322:       VecCopy(v, locv);
323:       DMGetOutputSequenceNumber(dm, NULL, &time);
324:       DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);
325:     }
326:     if (isvtk) {
327:       VecView_Plex_Local_VTK(locv, viewer);
328:     } else if (ishdf5) {
329: #if defined(PETSC_HAVE_HDF5)
330:       VecView_Plex_Local_HDF5_Internal(locv, viewer);
331: #else
332:       SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
333: #endif
334:     } else if (isdraw) {
335:       VecView_Plex_Local_Draw(locv, viewer);
336:     } else if (isglvis) {
337:       DMGetOutputSequenceNumber(dm, &step, NULL);
338:       PetscViewerGLVisSetSnapId(viewer, step);
339:       VecView_GLVis(locv, viewer);
340:     }
341:     if (fem) {
342:       PetscObjectCompose((PetscObject) locv, "__Vec_bc_zero__", NULL);
343:       DMRestoreLocalVector(dm, &locv);
344:     }
345:   } else {
346:     PetscBool isseq;

348:     PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);
349:     if (isseq) {VecView_Seq(v, viewer);}
350:     else       {VecView_MPI(v, viewer);}
351:   }
352:   return(0);
353: }

355: PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
356: {
357:   DM             dm;
358:   PetscBool      isvtk, ishdf5, isdraw, isglvis;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

556:   DMGetDimension(dm, &dim);
557:   DMGetCoordinatesLocal(dm, &coordinates);
558:   DMGetCoordinateSection(dm, &coordSection);
559:   DMPlexGetDepthLabel(dm, &depthLabel);
560:   DMPlexGetCellTypeLabel(dm, &celltypeLabel);
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, ct;

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

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

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

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

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

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

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

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

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

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

723:       DMGetLabel(dm, lname, &lbl);
724:       if (lbl) {
725:         PetscInt val, defval;

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

733:           DMLabelGetValue(lbl, c, &val);
734:           if (val == defval) continue;

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

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

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

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

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

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

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

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

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

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

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

965:     /* Get connectivity */
966:     DMPlexGetVTKCellHeight(dm,&cellHeight);
967:     DMPlexCreatePartitionerGraph(dm,cellHeight,&numVertices,&start,&adjacency,&gid);

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

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

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

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

1046:     PetscObjectGetComm((PetscObject) dm, &comm);
1047:     MPI_Comm_size(comm, &size);
1048:     MPI_Comm_rank(comm, &rank);
1049:     DMGetDimension(dm, &dim);
1050:     DMPlexGetVTKCellHeight(dm, &cellHeight);
1051:     PetscObjectGetName((PetscObject) dm, &name);
1052:     if (name) {PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");}
1053:     else      {PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");}
1054:     if (cellHeight) {PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);}
1055:     DMPlexGetDepth(dm, &locDepth);
1056:     MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);
1057:     DMPlexGetGhostCellStratum(dm, &gcStart, &gcEnd);
1058:     gcNum = gcEnd - gcStart;
1059:     PetscCalloc3(size,&sizes,size,&hybsizes,size,&ghostsizes);
1060:     for (d = 0; d <= depth; d++) {
1061:       PetscInt Nc[2] = {0, 0}, ict;

1063:       DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);
1064:       DMPlexGetCellType(dm, pStart, &ct0);
1065:       ict  = ct0;
1066:       MPI_Bcast(&ict, 1, MPIU_INT, 0, comm);
1067:       ct0  = (DMPolytopeType) ict;
1068:       for (p = pStart; p < pEnd; ++p) {
1069:         DMPolytopeType ct;

1071:         DMPlexGetCellType(dm, p, &ct);
1072:         if (ct == ct0) ++Nc[0];
1073:         else           ++Nc[1];
1074:       }
1075:       MPI_Gather(&Nc[0], 1, MPIU_INT, sizes,    1, MPIU_INT, 0, comm);
1076:       MPI_Gather(&Nc[1], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);
1077:       if (d == depth) {MPI_Gather(&gcNum, 1, MPIU_INT, ghostsizes, 1, MPIU_INT, 0, comm);}
1078:       PetscViewerASCIIPrintf(viewer, "  %D-cells:", (depth == 1) && d ? dim : d);
1079:       for (p = 0; p < size; ++p) {
1080:         if (!rank) {
1081:           PetscViewerASCIIPrintf(viewer, " %D", sizes[p]+hybsizes[p]);
1082:           if (hybsizes[p]   > 0) {PetscViewerASCIIPrintf(viewer, " (%D)", hybsizes[p]);}
1083:           if (ghostsizes[p] > 0) {PetscViewerASCIIPrintf(viewer, " [%D]", ghostsizes[p]);}
1084:         }
1085:       }
1086:       PetscViewerASCIIPrintf(viewer, "\n");
1087:     }
1088:     PetscFree3(sizes,hybsizes,ghostsizes);
1089:     DMGetNumLabels(dm, &numLabels);
1090:     if (numLabels) {PetscViewerASCIIPrintf(viewer, "Labels:\n");}
1091:     for (l = 0; l < numLabels; ++l) {
1092:       DMLabel         label;
1093:       const char     *name;
1094:       IS              valueIS;
1095:       const PetscInt *values;
1096:       PetscInt        numValues, v;

1098:       DMGetLabelName(dm, l, &name);
1099:       DMGetLabel(dm, name, &label);
1100:       DMLabelGetNumValues(label, &numValues);
1101:       PetscViewerASCIIPrintf(viewer, "  %s: %D strata with value/size (", name, numValues);
1102:       DMLabelGetValueIS(label, &valueIS);
1103:       ISGetIndices(valueIS, &values);
1104:       PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);
1105:       for (v = 0; v < numValues; ++v) {
1106:         PetscInt size;

1108:         DMLabelGetStratumSize(label, values[v], &size);
1109:         if (v > 0) {PetscViewerASCIIPrintf(viewer, ", ");}
1110:         PetscViewerASCIIPrintf(viewer, "%D (%D)", values[v], size);
1111:       }
1112:       PetscViewerASCIIPrintf(viewer, ")\n");
1113:       PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);
1114:       ISRestoreIndices(valueIS, &values);
1115:       ISDestroy(&valueIS);
1116:     }
1117:     /* If no fields are specified, people do not want to see adjacency */
1118:     if (dm->Nf) {
1119:       PetscInt f;

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

1124:         PetscObjectGetName(dm->fields[f].disc, &name);
1125:         if (numLabels) {PetscViewerASCIIPrintf(viewer, "Field %s:\n", name);}
1126:         PetscViewerASCIIPushTab(viewer);
1127:         if (dm->fields[f].label) {DMLabelView(dm->fields[f].label, viewer);}
1128:         if (dm->fields[f].adjacency[0]) {
1129:           if (dm->fields[f].adjacency[1]) {PetscViewerASCIIPrintf(viewer, "adjacency FVM++\n");}
1130:           else                            {PetscViewerASCIIPrintf(viewer, "adjacency FVM\n");}
1131:         } else {
1132:           if (dm->fields[f].adjacency[1]) {PetscViewerASCIIPrintf(viewer, "adjacency FEM\n");}
1133:           else                            {PetscViewerASCIIPrintf(viewer, "adjacency FUNKY\n");}
1134:         }
1135:         PetscViewerASCIIPopTab(viewer);
1136:       }
1137:     }
1138:     DMGetCoarseDM(dm, &cdm);
1139:     if (cdm) {
1140:       PetscViewerASCIIPushTab(viewer);
1141:       DMPlexView_Ascii(cdm, viewer);
1142:       PetscViewerASCIIPopTab(viewer);
1143:     }
1144:   }
1145:   return(0);
1146: }

1148: static PetscErrorCode DMPlexDrawCell(DM dm, PetscDraw draw, PetscInt cell, const PetscScalar coords[])
1149: {
1150:   DMPolytopeType ct;
1151:   PetscMPIInt    rank;

1155:   MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
1156:   DMPlexGetCellType(dm, cell, &ct);
1157:   switch (ct) {
1158:   case DM_POLYTOPE_TRIANGLE:
1159:     PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1160:                              PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1161:                              PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1162:                              PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);
1163:     PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);
1164:     PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);
1165:     PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);
1166:     break;
1167:   case DM_POLYTOPE_QUADRILATERAL:
1168:     PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1169:                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1170:                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1171:                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);
1172:     PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]),
1173:                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1174:                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1175:                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);
1176:     PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);
1177:     PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);
1178:     PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);
1179:     PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);
1180:     break;
1181:   default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
1182:   }
1183:   return(0);
1184: }

1186: static PetscErrorCode DMPlexDrawCellHighOrder(DM dm, PetscDraw draw, PetscInt cell, const PetscScalar coords[], PetscInt edgeDiv, PetscReal refCoords[], PetscReal edgeCoords[])
1187: {
1188:   DMPolytopeType ct;
1189:   PetscReal      centroid[2] = {0., 0.};
1190:   PetscMPIInt    rank;
1191:   PetscInt       fillColor, v, e, d;

1195:   MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
1196:   DMPlexGetCellType(dm, cell, &ct);
1197:   fillColor = PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2;
1198:   switch (ct) {
1199:   case DM_POLYTOPE_TRIANGLE:
1200:     {
1201:       PetscReal refVertices[6] = {-1., -1., 1., -1., -1., 1.};

1203:       for (v = 0; v < 3; ++v) {centroid[0] += PetscRealPart(coords[v*2+0])/3.;centroid[1] += PetscRealPart(coords[v*2+1])/3.;}
1204:       for (e = 0; e < 3; ++e) {
1205:         refCoords[0] = refVertices[e*2+0];
1206:         refCoords[1] = refVertices[e*2+1];
1207:         for (d = 1; d <= edgeDiv; ++d) {
1208:           refCoords[d*2+0] = refCoords[0] + (refVertices[(e+1)%3 * 2 + 0] - refCoords[0])*d/edgeDiv;
1209:           refCoords[d*2+1] = refCoords[1] + (refVertices[(e+1)%3 * 2 + 1] - refCoords[1])*d/edgeDiv;
1210:         }
1211:         DMPlexReferenceToCoordinates(dm, cell, edgeDiv+1, refCoords, edgeCoords);
1212:         for (d = 0; d < edgeDiv; ++d) {
1213:           PetscDrawTriangle(draw, centroid[0], centroid[1], edgeCoords[d*2+0], edgeCoords[d*2+1], edgeCoords[(d+1)*2+0], edgeCoords[(d+1)*2+1], fillColor, fillColor, fillColor);
1214:           PetscDrawLine(draw, edgeCoords[d*2+0], edgeCoords[d*2+1], edgeCoords[(d+1)*2+0], edgeCoords[(d+1)*2+1], PETSC_DRAW_BLACK);
1215:         }
1216:       }
1217:     }
1218:     break;
1219:   default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
1220:   }
1221:   return(0);
1222: }

1224: static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
1225: {
1226:   PetscDraw          draw;
1227:   DM                 cdm;
1228:   PetscSection       coordSection;
1229:   Vec                coordinates;
1230:   const PetscScalar *coords;
1231:   PetscReal          xyl[2],xyr[2],bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
1232:   PetscReal         *refCoords, *edgeCoords;
1233:   PetscBool          isnull, drawAffine = PETSC_TRUE;
1234:   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N, edgeDiv = 4;
1235:   PetscErrorCode     ierr;

1238:   DMGetCoordinateDim(dm, &dim);
1239:   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
1240:   PetscOptionsGetBool(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_view_draw_affine", &drawAffine, NULL);
1241:   if (!drawAffine) {PetscMalloc2((edgeDiv+1)*dim, &refCoords, (edgeDiv+1)*dim, &edgeCoords);}
1242:   DMGetCoordinateDM(dm, &cdm);
1243:   DMGetLocalSection(cdm, &coordSection);
1244:   DMGetCoordinatesLocal(dm, &coordinates);
1245:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
1246:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);

1248:   PetscViewerDrawGetDraw(viewer, 0, &draw);
1249:   PetscDrawIsNull(draw, &isnull);
1250:   if (isnull) return(0);
1251:   PetscDrawSetTitle(draw, "Mesh");

1253:   VecGetLocalSize(coordinates, &N);
1254:   VecGetArrayRead(coordinates, &coords);
1255:   for (c = 0; c < N; c += dim) {
1256:     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
1257:     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
1258:   }
1259:   VecRestoreArrayRead(coordinates, &coords);
1260:   MPIU_Allreduce(&bound[0],xyl,2,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)dm));
1261:   MPIU_Allreduce(&bound[2],xyr,2,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)dm));
1262:   PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);
1263:   PetscDrawClear(draw);

1265:   for (c = cStart; c < cEnd; ++c) {
1266:     PetscScalar *coords = NULL;
1267:     PetscInt     numCoords;

1269:     DMPlexVecGetClosureAtDepth_Internal(dm, coordSection, coordinates, c, 0, &numCoords, &coords);
1270:     if (drawAffine) {
1271:       DMPlexDrawCell(dm, draw, c, coords);
1272:     } else {
1273:       DMPlexDrawCellHighOrder(dm, draw, c, coords, edgeDiv, refCoords, edgeCoords);
1274:     }
1275:     DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);
1276:   }
1277:   if (!drawAffine) {PetscFree2(refCoords, edgeCoords);}
1278:   PetscDrawFlush(draw);
1279:   PetscDrawPause(draw);
1280:   PetscDrawSave(draw);
1281:   return(0);
1282: }

1284: #if defined(PETSC_HAVE_EXODUSII)
1285: #include <exodusII.h>
1286: #endif

1288: PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
1289: {
1290:   PetscBool      iascii, ishdf5, isvtk, isdraw, flg, isglvis, isexodus;
1291:   char           name[PETSC_MAX_PATH_LEN];

1297:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII,    &iascii);
1298:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,      &isvtk);
1299:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,     &ishdf5);
1300:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,     &isdraw);
1301:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS,    &isglvis);
1302:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWEREXODUSII, &isexodus);
1303:   if (iascii) {
1304:     PetscViewerFormat format;
1305:     PetscViewerGetFormat(viewer, &format);
1306:     if (format == PETSC_VIEWER_ASCII_GLVIS) {
1307:       DMPlexView_GLVis(dm, viewer);
1308:     } else {
1309:       DMPlexView_Ascii(dm, viewer);
1310:     }
1311:   } else if (ishdf5) {
1312: #if defined(PETSC_HAVE_HDF5)
1313:     DMPlexView_HDF5_Internal(dm, viewer);
1314: #else
1315:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1316: #endif
1317:   } else if (isvtk) {
1318:     DMPlexVTKWriteAll((PetscObject) dm,viewer);
1319:   } else if (isdraw) {
1320:     DMPlexView_Draw(dm, viewer);
1321:   } else if (isglvis) {
1322:     DMPlexView_GLVis(dm, viewer);
1323: #if defined(PETSC_HAVE_EXODUSII)
1324:   } else if (isexodus) {
1325:     int exoid;
1326:     PetscInt cStart, cEnd, c;

1328:     DMCreateLabel(dm, "Cell Sets");
1329:     DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
1330:     for (c = cStart; c < cEnd; ++c) {DMSetLabelValue(dm, "Cell Sets", c, 1);}
1331:     PetscViewerExodusIIGetId(viewer, &exoid);
1332:     DMPlexView_ExodusII_Internal(dm, exoid, 1);
1333: #endif
1334:   } else {
1335:     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex writing", ((PetscObject)viewer)->type_name);
1336:   }
1337:   /* Optionally view the partition */
1338:   PetscOptionsHasName(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_partition_view", &flg);
1339:   if (flg) {
1340:     Vec ranks;
1341:     DMPlexCreateRankField(dm, &ranks);
1342:     VecView(ranks, viewer);
1343:     VecDestroy(&ranks);
1344:   }
1345:   /* Optionally view a label */
1346:   PetscOptionsGetString(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_label_view", name, sizeof(name), &flg);
1347:   if (flg) {
1348:     DMLabel label;
1349:     Vec     val;

1351:     DMGetLabel(dm, name, &label);
1352:     if (!label) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Label %s provided to -dm_label_view does not exist in this DM", name);
1353:     DMPlexCreateLabelField(dm, label, &val);
1354:     VecView(val, viewer);
1355:     VecDestroy(&val);
1356:   }
1357:   return(0);
1358: }

1360: PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
1361: {
1362:   PetscBool      ishdf5;

1368:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);
1369:   if (ishdf5) {
1370: #if defined(PETSC_HAVE_HDF5)
1371:     PetscViewerFormat format;
1372:     PetscViewerGetFormat(viewer, &format);
1373:     if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
1374:       DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer);
1375:     } else if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
1376:       DMPlexLoad_HDF5_Internal(dm, viewer);
1377:     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
1378:     return(0);
1379: #else
1380:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1381: #endif
1382:   } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
1383: }

1385: PetscErrorCode DMDestroy_Plex(DM dm)
1386: {
1387:   DM_Plex       *mesh = (DM_Plex*) dm->data;

1391:   PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);
1392:   PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);
1393:   PetscObjectComposeFunction((PetscObject)dm,"DMCreateNeumannOverlap_C", NULL);
1394:   if (--mesh->refct > 0) return(0);
1395:   PetscSectionDestroy(&mesh->coneSection);
1396:   PetscFree(mesh->cones);
1397:   PetscFree(mesh->coneOrientations);
1398:   PetscSectionDestroy(&mesh->supportSection);
1399:   PetscSectionDestroy(&mesh->subdomainSection);
1400:   PetscFree(mesh->supports);
1401:   PetscFree(mesh->facesTmp);
1402:   PetscFree(mesh->tetgenOpts);
1403:   PetscFree(mesh->triangleOpts);
1404:   PetscPartitionerDestroy(&mesh->partitioner);
1405:   DMLabelDestroy(&mesh->subpointMap);
1406:   ISDestroy(&mesh->subpointIS);
1407:   ISDestroy(&mesh->globalVertexNumbers);
1408:   ISDestroy(&mesh->globalCellNumbers);
1409:   PetscSectionDestroy(&mesh->anchorSection);
1410:   ISDestroy(&mesh->anchorIS);
1411:   PetscSectionDestroy(&mesh->parentSection);
1412:   PetscFree(mesh->parents);
1413:   PetscFree(mesh->childIDs);
1414:   PetscSectionDestroy(&mesh->childSection);
1415:   PetscFree(mesh->children);
1416:   DMDestroy(&mesh->referenceTree);
1417:   PetscGridHashDestroy(&mesh->lbox);
1418:   PetscFree(mesh->neighbors);
1419:   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1420:   PetscFree(mesh);
1421:   return(0);
1422: }

1424: PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
1425: {
1426:   PetscSection           sectionGlobal;
1427:   PetscInt               bs = -1, mbs;
1428:   PetscInt               localSize;
1429:   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
1430:   PetscErrorCode         ierr;
1431:   MatType                mtype;
1432:   ISLocalToGlobalMapping ltog;

1435:   MatInitializePackage();
1436:   mtype = dm->mattype;
1437:   DMGetGlobalSection(dm, &sectionGlobal);
1438:   /* PetscSectionGetStorageSize(sectionGlobal, &localSize); */
1439:   PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);
1440:   MatCreate(PetscObjectComm((PetscObject)dm), J);
1441:   MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);
1442:   MatSetType(*J, mtype);
1443:   MatSetFromOptions(*J);
1444:   MatGetBlockSize(*J, &mbs);
1445:   if (mbs > 1) bs = mbs;
1446:   PetscStrcmp(mtype, MATSHELL, &isShell);
1447:   PetscStrcmp(mtype, MATBAIJ, &isBlock);
1448:   PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);
1449:   PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);
1450:   PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);
1451:   PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);
1452:   PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);
1453:   PetscStrcmp(mtype, MATIS, &isMatIS);
1454:   if (!isShell) {
1455:     PetscSection subSection;
1456:     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
1457:     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *ltogidx, lsize;
1458:     PetscInt     pStart, pEnd, p, dof, cdof;

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

1465:       DMGetLocalSection(dm, &section);
1466:       PetscSectionGetStorageSize(section, &size);
1467:       PetscMalloc1(size,&ltogidx);
1468:       DMPlexGetSubdomainSection(dm, &subSection);
1469:     } else {
1470:       DMGetLocalToGlobalMapping(dm,&ltog);
1471:     }
1472:     PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);
1473:     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1474:       PetscInt bdof;

1476:       PetscSectionGetDof(sectionGlobal, p, &dof);
1477:       PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);
1478:       dof  = dof < 0 ? -(dof+1) : dof;
1479:       bdof = cdof && (dof-cdof) ? 1 : dof;
1480:       if (dof) {
1481:         if (bs < 0)          {bs = bdof;}
1482:         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1483:       }
1484:       if (isMatIS) {
1485:         PetscInt loff,c,off;
1486:         PetscSectionGetOffset(subSection, p, &loff);
1487:         PetscSectionGetOffset(sectionGlobal, p, &off);
1488:         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1489:       }
1490:     }
1491:     /* Must have same blocksize on all procs (some might have no points) */
1492:     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
1493:     PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);
1494:     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
1495:     else                            {bs = bsMinMax[0];}
1496:     bs = PetscMax(1,bs);
1497:     if (isMatIS) { /* Must reduce indices by blocksize */
1498:       PetscInt l;

1500:       lsize = lsize/bs;
1501:       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] = ltogidx[l*bs]/bs;
1502:       ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);
1503:     }
1504:     MatSetLocalToGlobalMapping(*J,ltog,ltog);
1505:     if (isMatIS) {
1506:       ISLocalToGlobalMappingDestroy(&ltog);
1507:     }
1508:     PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);
1509:     DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);
1510:     PetscFree4(dnz, onz, dnzu, onzu);
1511:   }
1512:   MatSetDM(*J, dm);
1513:   return(0);
1514: }

1516: /*@
1517:   DMPlexGetSubdomainSection - Returns the section associated with the subdomain

1519:   Not collective

1521:   Input Parameter:
1522: . mesh - The DMPlex

1524:   Output Parameters:
1525: . subsection - The subdomain section

1527:   Level: developer

1529: .seealso:
1530: @*/
1531: PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1532: {
1533:   DM_Plex       *mesh = (DM_Plex*) dm->data;

1538:   if (!mesh->subdomainSection) {
1539:     PetscSection section;
1540:     PetscSF      sf;

1542:     PetscSFCreate(PETSC_COMM_SELF,&sf);
1543:     DMGetLocalSection(dm,&section);
1544:     PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);
1545:     PetscSFDestroy(&sf);
1546:   }
1547:   *subsection = mesh->subdomainSection;
1548:   return(0);
1549: }

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

1554:   Not collective

1556:   Input Parameter:
1557: . mesh - The DMPlex

1559:   Output Parameters:
1560: + pStart - The first mesh point
1561: - pEnd   - The upper bound for mesh points

1563:   Level: beginner

1565: .seealso: DMPlexCreate(), DMPlexSetChart()
1566: @*/
1567: PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1568: {
1569:   DM_Plex       *mesh = (DM_Plex*) dm->data;

1574:   PetscSectionGetChart(mesh->coneSection, pStart, pEnd);
1575:   return(0);
1576: }

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

1581:   Not collective

1583:   Input Parameters:
1584: + mesh - The DMPlex
1585: . pStart - The first mesh point
1586: - pEnd   - The upper bound for mesh points

1588:   Output Parameters:

1590:   Level: beginner

1592: .seealso: DMPlexCreate(), DMPlexGetChart()
1593: @*/
1594: PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1595: {
1596:   DM_Plex       *mesh = (DM_Plex*) dm->data;

1601:   PetscSectionSetChart(mesh->coneSection, pStart, pEnd);
1602:   PetscSectionSetChart(mesh->supportSection, pStart, pEnd);
1603:   return(0);
1604: }

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

1609:   Not collective

1611:   Input Parameters:
1612: + mesh - The DMPlex
1613: - p - The point, which must lie in the chart set with DMPlexSetChart()

1615:   Output Parameter:
1616: . size - The cone size for point p

1618:   Level: beginner

1620: .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1621: @*/
1622: PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1623: {
1624:   DM_Plex       *mesh = (DM_Plex*) dm->data;

1630:   PetscSectionGetDof(mesh->coneSection, p, size);
1631:   return(0);
1632: }

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

1637:   Not collective

1639:   Input Parameters:
1640: + mesh - The DMPlex
1641: . p - The point, which must lie in the chart set with DMPlexSetChart()
1642: - size - The cone size for point p

1644:   Output Parameter:

1646:   Note:
1647:   This should be called after DMPlexSetChart().

1649:   Level: beginner

1651: .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1652: @*/
1653: PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1654: {
1655:   DM_Plex       *mesh = (DM_Plex*) dm->data;

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

1662:   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1663:   return(0);
1664: }

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

1669:   Not collective

1671:   Input Parameters:
1672: + mesh - The DMPlex
1673: . p - The point, which must lie in the chart set with DMPlexSetChart()
1674: - size - The additional cone size for point p

1676:   Output Parameter:

1678:   Note:
1679:   This should be called after DMPlexSetChart().

1681:   Level: beginner

1683: .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1684: @*/
1685: PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1686: {
1687:   DM_Plex       *mesh = (DM_Plex*) dm->data;
1688:   PetscInt       csize;

1693:   PetscSectionAddDof(mesh->coneSection, p, size);
1694:   PetscSectionGetDof(mesh->coneSection, p, &csize);

1696:   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1697:   return(0);
1698: }

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

1703:   Not collective

1705:   Input Parameters:
1706: + dm - The DMPlex
1707: - p - The point, which must lie in the chart set with DMPlexSetChart()

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

1712:   Level: beginner

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

1720: .seealso: DMPlexGetConeSize(), DMPlexSetCone(), DMPlexGetConeTuple(), DMPlexSetChart()
1721: @*/
1722: PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1723: {
1724:   DM_Plex       *mesh = (DM_Plex*) dm->data;
1725:   PetscInt       off;

1731:   PetscSectionGetOffset(mesh->coneSection, p, &off);
1732:   *cone = &mesh->cones[off];
1733:   return(0);
1734: }

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

1739:   Not collective

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

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

1749:   Level: intermediate

1751: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeRecursive(), DMPlexSetChart()
1752: @*/
1753: PetscErrorCode DMPlexGetConeTuple(DM dm, IS p, PetscSection *pConesSection, IS *pCones)
1754: {
1755:   PetscSection        cs, newcs;
1756:   PetscInt            *cones;
1757:   PetscInt            *newarr=NULL;
1758:   PetscInt            n;
1759:   PetscErrorCode      ierr;

1762:   DMPlexGetCones(dm, &cones);
1763:   DMPlexGetConeSection(dm, &cs);
1764:   PetscSectionExtractDofsFromArray(cs, MPIU_INT, cones, p, &newcs, pCones ? ((void**)&newarr) : NULL);
1765:   if (pConesSection) *pConesSection = newcs;
1766:   if (pCones) {
1767:     PetscSectionGetStorageSize(newcs, &n);
1768:     ISCreateGeneral(PetscObjectComm((PetscObject)p), n, newarr, PETSC_OWN_POINTER, pCones);
1769:   }
1770:   return(0);
1771: }

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

1776:   Not collective

1778:   Input Parameters:
1779: + dm - The DMPlex
1780: - points - The IS of points, which must lie in the chart set with DMPlexSetChart()

1782:   Output Parameter:
1783: . expandedPoints - An array of vertices recursively expanded from input points

1785:   Level: advanced

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

1791: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexRestoreConeRecursive(), DMPlexGetDepth()
1792: @*/
1793: PetscErrorCode DMPlexGetConeRecursiveVertices(DM dm, IS points, IS *expandedPoints)
1794: {
1795:   IS                  *expandedPointsAll;
1796:   PetscInt            depth;
1797:   PetscErrorCode      ierr;

1803:   DMPlexGetConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);
1804:   *expandedPoints = expandedPointsAll[0];
1805:   PetscObjectReference((PetscObject)expandedPointsAll[0]);
1806:   DMPlexRestoreConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);
1807:   return(0);
1808: }

1810: /*@
1811:   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).

1813:   Not collective

1815:   Input Parameters:
1816: + dm - The DMPlex
1817: - points - The IS of points, which must lie in the chart set with DMPlexSetChart()

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

1824:   Level: advanced

1826:   Notes:
1827:   Like DMPlexGetConeTuple() but recursive.

1829:   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.
1830:   For example, for d=0 it contains only vertices, for d=1 it can contain vertices and edges, etc.

1832:   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:
1833:   (1) DAG points in expandedPoints[d+1] with depth d+1 to their cone points in expandedPoints[d];
1834:   (2) DAG points in expandedPoints[d+1] with depth in [0,d] to the same points in expandedPoints[d].

1836: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexRestoreConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
1837: @*/
1838: PetscErrorCode DMPlexGetConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
1839: {
1840:   const PetscInt      *arr0=NULL, *cone=NULL;
1841:   PetscInt            *arr=NULL, *newarr=NULL;
1842:   PetscInt            d, depth_, i, n, newn, cn, co, start, end;
1843:   IS                  *expandedPoints_;
1844:   PetscSection        *sections_;
1845:   PetscErrorCode      ierr;

1853:   ISGetLocalSize(points, &n);
1854:   ISGetIndices(points, &arr0);
1855:   DMPlexGetDepth(dm, &depth_);
1856:   PetscCalloc1(depth_, &expandedPoints_);
1857:   PetscCalloc1(depth_, &sections_);
1858:   arr = (PetscInt*) arr0; /* this is ok because first generation of arr is not modified */
1859:   for (d=depth_-1; d>=0; d--) {
1860:     PetscSectionCreate(PETSC_COMM_SELF, &sections_[d]);
1861:     PetscSectionSetChart(sections_[d], 0, n);
1862:     for (i=0; i<n; i++) {
1863:       DMPlexGetDepthStratum(dm, d+1, &start, &end);
1864:       if (arr[i] >= start && arr[i] < end) {
1865:         DMPlexGetConeSize(dm, arr[i], &cn);
1866:         PetscSectionSetDof(sections_[d], i, cn);
1867:       } else {
1868:         PetscSectionSetDof(sections_[d], i, 1);
1869:       }
1870:     }
1871:     PetscSectionSetUp(sections_[d]);
1872:     PetscSectionGetStorageSize(sections_[d], &newn);
1873:     PetscMalloc1(newn, &newarr);
1874:     for (i=0; i<n; i++) {
1875:       PetscSectionGetDof(sections_[d], i, &cn);
1876:       PetscSectionGetOffset(sections_[d], i, &co);
1877:       if (cn > 1) {
1878:         DMPlexGetCone(dm, arr[i], &cone);
1879:         PetscMemcpy(&newarr[co], cone, cn*sizeof(PetscInt));
1880:       } else {
1881:         newarr[co] = arr[i];
1882:       }
1883:     }
1884:     ISCreateGeneral(PETSC_COMM_SELF, newn, newarr, PETSC_OWN_POINTER, &expandedPoints_[d]);
1885:     arr = newarr;
1886:     n = newn;
1887:   }
1888:   ISRestoreIndices(points, &arr0);
1889:   *depth = depth_;
1890:   if (expandedPoints) *expandedPoints = expandedPoints_;
1891:   else {
1892:     for (d=0; d<depth_; d++) {ISDestroy(&expandedPoints_[d]);}
1893:     PetscFree(expandedPoints_);
1894:   }
1895:   if (sections) *sections = sections_;
1896:   else {
1897:     for (d=0; d<depth_; d++) {PetscSectionDestroy(&sections_[d]);}
1898:     PetscFree(sections_);
1899:   }
1900:   return(0);
1901: }

1903: /*@
1904:   DMPlexRestoreConeRecursive - Deallocates arrays created by DMPlexGetConeRecursive

1906:   Not collective

1908:   Input Parameters:
1909: + dm - The DMPlex
1910: - points - The IS of points, which must lie in the chart set with DMPlexSetChart()

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

1917:   Level: advanced

1919:   Notes:
1920:   See DMPlexGetConeRecursive() for details.

1922: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
1923: @*/
1924: PetscErrorCode DMPlexRestoreConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
1925: {
1926:   PetscInt            d, depth_;
1927:   PetscErrorCode      ierr;

1930:   DMPlexGetDepth(dm, &depth_);
1931:   if (depth && *depth != depth_) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "depth changed since last call to DMPlexGetConeRecursive");
1932:   if (depth) *depth = 0;
1933:   if (expandedPoints) {
1934:     for (d=0; d<depth_; d++) {ISDestroy(&((*expandedPoints)[d]));}
1935:     PetscFree(*expandedPoints);
1936:   }
1937:   if (sections)  {
1938:     for (d=0; d<depth_; d++) {PetscSectionDestroy(&((*sections)[d]));}
1939:     PetscFree(*sections);
1940:   }
1941:   return(0);
1942: }

1944: /*@
1945:   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

1947:   Not collective

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

1954:   Output Parameter:

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

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

1961:   Level: beginner

1963: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp(), DMPlexSetSupport(), DMPlexSetSupportSize()
1964: @*/
1965: PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1966: {
1967:   DM_Plex       *mesh = (DM_Plex*) dm->data;
1968:   PetscInt       pStart, pEnd;
1969:   PetscInt       dof, off, c;

1974:   PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
1975:   PetscSectionGetDof(mesh->coneSection, p, &dof);
1977:   PetscSectionGetOffset(mesh->coneSection, p, &off);
1978:   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);
1979:   for (c = 0; c < dof; ++c) {
1980:     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);
1981:     mesh->cones[off+c] = cone[c];
1982:   }
1983:   return(0);
1984: }

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

1989:   Not collective

1991:   Input Parameters:
1992: + mesh - The DMPlex
1993: - p - The point, which must lie in the chart set with DMPlexSetChart()

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

2001:   Level: beginner

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

2009: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
2010: @*/
2011: PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
2012: {
2013:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2014:   PetscInt       off;

2019:   if (PetscDefined(USE_DEBUG)) {
2020:     PetscInt dof;
2021:     PetscSectionGetDof(mesh->coneSection, p, &dof);
2023:   }
2024:   PetscSectionGetOffset(mesh->coneSection, p, &off);

2026:   *coneOrientation = &mesh->coneOrientations[off];
2027:   return(0);
2028: }

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

2033:   Not collective

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

2043:   Output Parameter:

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

2048:   Level: beginner

2050: .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
2051: @*/
2052: PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
2053: {
2054:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2055:   PetscInt       pStart, pEnd;
2056:   PetscInt       dof, off, c;

2061:   PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
2062:   PetscSectionGetDof(mesh->coneSection, p, &dof);
2064:   PetscSectionGetOffset(mesh->coneSection, p, &off);
2065:   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);
2066:   for (c = 0; c < dof; ++c) {
2067:     PetscInt cdof, o = coneOrientation[c];

2069:     PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);
2070:     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);
2071:     mesh->coneOrientations[off+c] = o;
2072:   }
2073:   return(0);
2074: }

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

2079:   Not collective

2081:   Input Parameters:
2082: + mesh - The DMPlex
2083: . p - The point, which must lie in the chart set with DMPlexSetChart()
2084: . conePos - The local index in the cone where the point should be put
2085: - conePoint - The mesh point to insert

2087:   Level: beginner

2089: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
2090: @*/
2091: PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
2092: {
2093:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2094:   PetscInt       pStart, pEnd;
2095:   PetscInt       dof, off;

2100:   PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
2101:   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);
2102:   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);
2103:   PetscSectionGetDof(mesh->coneSection, p, &dof);
2104:   PetscSectionGetOffset(mesh->coneSection, p, &off);
2105:   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);
2106:   mesh->cones[off+conePos] = conePoint;
2107:   return(0);
2108: }

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

2113:   Not collective

2115:   Input Parameters:
2116: + mesh - The DMPlex
2117: . p - The point, which must lie in the chart set with DMPlexSetChart()
2118: . conePos - The local index in the cone where the point should be put
2119: - coneOrientation - The point orientation to insert

2121:   Level: beginner

2123: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
2124: @*/
2125: PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
2126: {
2127:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2128:   PetscInt       pStart, pEnd;
2129:   PetscInt       dof, off;

2134:   PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
2135:   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);
2136:   PetscSectionGetDof(mesh->coneSection, p, &dof);
2137:   PetscSectionGetOffset(mesh->coneSection, p, &off);
2138:   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);
2139:   mesh->coneOrientations[off+conePos] = coneOrientation;
2140:   return(0);
2141: }

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

2146:   Not collective

2148:   Input Parameters:
2149: + mesh - The DMPlex
2150: - p - The point, which must lie in the chart set with DMPlexSetChart()

2152:   Output Parameter:
2153: . size - The support size for point p

2155:   Level: beginner

2157: .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
2158: @*/
2159: PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
2160: {
2161:   DM_Plex       *mesh = (DM_Plex*) dm->data;

2167:   PetscSectionGetDof(mesh->supportSection, p, size);
2168:   return(0);
2169: }

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

2174:   Not collective

2176:   Input Parameters:
2177: + mesh - The DMPlex
2178: . p - The point, which must lie in the chart set with DMPlexSetChart()
2179: - size - The support size for point p

2181:   Output Parameter:

2183:   Note:
2184:   This should be called after DMPlexSetChart().

2186:   Level: beginner

2188: .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
2189: @*/
2190: PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
2191: {
2192:   DM_Plex       *mesh = (DM_Plex*) dm->data;

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

2199:   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
2200:   return(0);
2201: }

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

2206:   Not collective

2208:   Input Parameters:
2209: + mesh - The DMPlex
2210: - p - The point, which must lie in the chart set with DMPlexSetChart()

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

2215:   Level: beginner

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

2223: .seealso: DMPlexGetSupportSize(), DMPlexSetSupport(), DMPlexGetCone(), DMPlexSetChart()
2224: @*/
2225: PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
2226: {
2227:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2228:   PetscInt       off;

2234:   PetscSectionGetOffset(mesh->supportSection, p, &off);
2235:   *support = &mesh->supports[off];
2236:   return(0);
2237: }

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

2242:   Not collective

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

2249:   Output Parameter:

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

2254:   Level: beginner

2256: .seealso: DMPlexSetCone(), DMPlexSetConeSize(), DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
2257: @*/
2258: PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
2259: {
2260:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2261:   PetscInt       pStart, pEnd;
2262:   PetscInt       dof, off, c;

2267:   PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);
2268:   PetscSectionGetDof(mesh->supportSection, p, &dof);
2270:   PetscSectionGetOffset(mesh->supportSection, p, &off);
2271:   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);
2272:   for (c = 0; c < dof; ++c) {
2273:     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);
2274:     mesh->supports[off+c] = support[c];
2275:   }
2276:   return(0);
2277: }

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

2282:   Not collective

2284:   Input Parameters:
2285: + mesh - The DMPlex
2286: . p - The point, which must lie in the chart set with DMPlexSetChart()
2287: . supportPos - The local index in the cone where the point should be put
2288: - supportPoint - The mesh point to insert

2290:   Level: beginner

2292: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
2293: @*/
2294: PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
2295: {
2296:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2297:   PetscInt       pStart, pEnd;
2298:   PetscInt       dof, off;

2303:   PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);
2304:   PetscSectionGetDof(mesh->supportSection, p, &dof);
2305:   PetscSectionGetOffset(mesh->supportSection, p, &off);
2306:   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);
2307:   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);
2308:   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);
2309:   mesh->supports[off+supportPos] = supportPoint;
2310:   return(0);
2311: }

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

2316:   Not collective

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

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

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

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

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

2337:   Level: beginner

2339: .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2340: @*/
2341: PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2342: {
2343:   DM_Plex        *mesh = (DM_Plex*) dm->data;
2344:   PetscInt       *closure, *fifo;
2345:   const PetscInt *tmp = NULL, *tmpO = NULL;
2346:   PetscInt        tmpSize, t;
2347:   PetscInt        depth       = 0, maxSize;
2348:   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
2349:   PetscErrorCode  ierr;

2353:   DMPlexGetDepth(dm, &depth);
2354:   /* This is only 1-level */
2355:   if (useCone) {
2356:     DMPlexGetConeSize(dm, p, &tmpSize);
2357:     DMPlexGetCone(dm, p, &tmp);
2358:     DMPlexGetConeOrientation(dm, p, &tmpO);
2359:   } else {
2360:     DMPlexGetSupportSize(dm, p, &tmpSize);
2361:     DMPlexGetSupport(dm, p, &tmp);
2362:   }
2363:   if (depth == 1) {
2364:     if (*points) {
2365:       closure = *points;
2366:     } else {
2367:       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
2368:       DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);
2369:     }
2370:     closure[0] = p; closure[1] = 0;
2371:     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
2372:       closure[closureSize]   = tmp[t];
2373:       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
2374:     }
2375:     if (numPoints) *numPoints = closureSize/2;
2376:     if (points)    *points    = closure;
2377:     return(0);
2378:   }
2379:   {
2380:     PetscInt c, coneSeries, s,supportSeries;

2382:     c = mesh->maxConeSize;
2383:     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
2384:     s = mesh->maxSupportSize;
2385:     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
2386:     maxSize = 2*PetscMax(coneSeries,supportSeries);
2387:   }
2388:   DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);
2389:   if (*points) {
2390:     closure = *points;
2391:   } else {
2392:     DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);
2393:   }
2394:   closure[0] = p; closure[1] = 0;
2395:   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
2396:     const PetscInt cp = tmp[t];
2397:     const PetscInt co = tmpO ? tmpO[t] : 0;

2399:     closure[closureSize]   = cp;
2400:     closure[closureSize+1] = co;
2401:     fifo[fifoSize]         = cp;
2402:     fifo[fifoSize+1]       = co;
2403:   }
2404:   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
2405:   while (fifoSize - fifoStart) {
2406:     const PetscInt q   = fifo[fifoStart];
2407:     const PetscInt o   = fifo[fifoStart+1];
2408:     const PetscInt rev = o >= 0 ? 0 : 1;
2409:     const PetscInt off = rev ? -(o+1) : o;

2411:     if (useCone) {
2412:       DMPlexGetConeSize(dm, q, &tmpSize);
2413:       DMPlexGetCone(dm, q, &tmp);
2414:       DMPlexGetConeOrientation(dm, q, &tmpO);
2415:     } else {
2416:       DMPlexGetSupportSize(dm, q, &tmpSize);
2417:       DMPlexGetSupport(dm, q, &tmp);
2418:       tmpO = NULL;
2419:     }
2420:     for (t = 0; t < tmpSize; ++t) {
2421:       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
2422:       const PetscInt cp = tmp[i];
2423:       /* Must propogate orientation: When we reverse orientation, we both reverse the direction of iteration and start at the other end of the chain. */
2424:       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
2425:        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
2426:       PetscInt       co = tmpO ? tmpO[i] : 0;
2427:       PetscInt       c;

2429:       if (rev) {
2430:         PetscInt childSize, coff;
2431:         DMPlexGetConeSize(dm, cp, &childSize);
2432:         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
2433:         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
2434:       }
2435:       /* Check for duplicate */
2436:       for (c = 0; c < closureSize; c += 2) {
2437:         if (closure[c] == cp) break;
2438:       }
2439:       if (c == closureSize) {
2440:         closure[closureSize]   = cp;
2441:         closure[closureSize+1] = co;
2442:         fifo[fifoSize]         = cp;
2443:         fifo[fifoSize+1]       = co;
2444:         closureSize           += 2;
2445:         fifoSize              += 2;
2446:       }
2447:     }
2448:     fifoStart += 2;
2449:   }
2450:   if (numPoints) *numPoints = closureSize/2;
2451:   if (points)    *points    = closure;
2452:   DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);
2453:   return(0);
2454: }

2456: /*@C
2457:   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

2459:   Not collective

2461:   Input Parameters:
2462: + mesh - The DMPlex
2463: . p - The point, which must lie in the chart set with DMPlexSetChart()
2464: . orientation - The orientation of the point
2465: . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2466: - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used

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

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

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

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

2481:   Level: beginner

2483: .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2484: @*/
2485: PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2486: {
2487:   DM_Plex        *mesh = (DM_Plex*) dm->data;
2488:   PetscInt       *closure, *fifo;
2489:   const PetscInt *tmp = NULL, *tmpO = NULL;
2490:   PetscInt        tmpSize, t;
2491:   PetscInt        depth       = 0, maxSize;
2492:   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
2493:   PetscErrorCode  ierr;

2497:   DMPlexGetDepth(dm, &depth);
2498:   /* This is only 1-level */
2499:   if (useCone) {
2500:     DMPlexGetConeSize(dm, p, &tmpSize);
2501:     DMPlexGetCone(dm, p, &tmp);
2502:     DMPlexGetConeOrientation(dm, p, &tmpO);
2503:   } else {
2504:     DMPlexGetSupportSize(dm, p, &tmpSize);
2505:     DMPlexGetSupport(dm, p, &tmp);
2506:   }
2507:   if (depth == 1) {
2508:     if (*points) {
2509:       closure = *points;
2510:     } else {
2511:       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
2512:       DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);
2513:     }
2514:     closure[0] = p; closure[1] = ornt;
2515:     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
2516:       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
2517:       closure[closureSize]   = tmp[i];
2518:       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
2519:     }
2520:     if (numPoints) *numPoints = closureSize/2;
2521:     if (points)    *points    = closure;
2522:     return(0);
2523:   }
2524:   {
2525:     PetscInt c, coneSeries, s,supportSeries;

2527:     c = mesh->maxConeSize;
2528:     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
2529:     s = mesh->maxSupportSize;
2530:     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
2531:     maxSize = 2*PetscMax(coneSeries,supportSeries);
2532:   }
2533:   DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);
2534:   if (*points) {
2535:     closure = *points;
2536:   } else {
2537:     DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);
2538:   }
2539:   closure[0] = p; closure[1] = ornt;
2540:   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
2541:     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
2542:     const PetscInt cp = tmp[i];
2543:     PetscInt       co = tmpO ? tmpO[i] : 0;

2545:     if (ornt < 0) {
2546:       PetscInt childSize, coff;
2547:       DMPlexGetConeSize(dm, cp, &childSize);
2548:       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
2549:       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
2550:     }
2551:     closure[closureSize]   = cp;
2552:     closure[closureSize+1] = co;
2553:     fifo[fifoSize]         = cp;
2554:     fifo[fifoSize+1]       = co;
2555:   }
2556:   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
2557:   while (fifoSize - fifoStart) {
2558:     const PetscInt q   = fifo[fifoStart];
2559:     const PetscInt o   = fifo[fifoStart+1];
2560:     const PetscInt rev = o >= 0 ? 0 : 1;
2561:     const PetscInt off = rev ? -(o+1) : o;

2563:     if (useCone) {
2564:       DMPlexGetConeSize(dm, q, &tmpSize);
2565:       DMPlexGetCone(dm, q, &tmp);
2566:       DMPlexGetConeOrientation(dm, q, &tmpO);
2567:     } else {
2568:       DMPlexGetSupportSize(dm, q, &tmpSize);
2569:       DMPlexGetSupport(dm, q, &tmp);
2570:       tmpO = NULL;
2571:     }
2572:     for (t = 0; t < tmpSize; ++t) {
2573:       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
2574:       const PetscInt cp = tmp[i];
2575:       /* Must propogate orientation: When we reverse orientation, we both reverse the direction of iteration and start at the other end of the chain. */
2576:       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
2577:        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
2578:       PetscInt       co = tmpO ? tmpO[i] : 0;
2579:       PetscInt       c;

2581:       if (rev) {
2582:         PetscInt childSize, coff;
2583:         DMPlexGetConeSize(dm, cp, &childSize);
2584:         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
2585:         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
2586:       }
2587:       /* Check for duplicate */
2588:       for (c = 0; c < closureSize; c += 2) {
2589:         if (closure[c] == cp) break;
2590:       }
2591:       if (c == closureSize) {
2592:         closure[closureSize]   = cp;
2593:         closure[closureSize+1] = co;
2594:         fifo[fifoSize]         = cp;
2595:         fifo[fifoSize+1]       = co;
2596:         closureSize           += 2;
2597:         fifoSize              += 2;
2598:       }
2599:     }
2600:     fifoStart += 2;
2601:   }
2602:   if (numPoints) *numPoints = closureSize/2;
2603:   if (points)    *points    = closure;
2604:   DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);
2605:   return(0);
2606: }

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

2611:   Not collective

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

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

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

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

2629:   Level: beginner

2631: .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2632: @*/
2633: PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2634: {

2641:   DMRestoreWorkArray(dm, 0, MPIU_INT, points);
2642:   if (numPoints) *numPoints = 0;
2643:   return(0);
2644: }

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

2649:   Not collective

2651:   Input Parameter:
2652: . mesh - The DMPlex

2654:   Output Parameters:
2655: + maxConeSize - The maximum number of in-edges
2656: - maxSupportSize - The maximum number of out-edges

2658:   Level: beginner

2660: .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2661: @*/
2662: PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
2663: {
2664:   DM_Plex *mesh = (DM_Plex*) dm->data;

2668:   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
2669:   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
2670:   return(0);
2671: }

2673: PetscErrorCode DMSetUp_Plex(DM dm)
2674: {
2675:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2676:   PetscInt       size;

2681:   PetscSectionSetUp(mesh->coneSection);
2682:   PetscSectionGetStorageSize(mesh->coneSection, &size);
2683:   PetscMalloc1(size, &mesh->cones);
2684:   PetscCalloc1(size, &mesh->coneOrientations);
2685:   PetscLogObjectMemory((PetscObject) dm, size*2*sizeof(PetscInt));
2686:   if (mesh->maxSupportSize) {
2687:     PetscSectionSetUp(mesh->supportSection);
2688:     PetscSectionGetStorageSize(mesh->supportSection, &size);
2689:     PetscMalloc1(size, &mesh->supports);
2690:     PetscLogObjectMemory((PetscObject) dm, size*sizeof(PetscInt));
2691:   }
2692:   return(0);
2693: }

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

2700:   if (subdm) {DMClone(dm, subdm);}
2701:   DMCreateSectionSubDM(dm, numFields, fields, is, subdm);
2702:   if (subdm) {(*subdm)->useNatural = dm->useNatural;}
2703:   if (dm->useNatural && dm->sfMigration) {
2704:     PetscSF        sfMigrationInv,sfNatural;
2705:     PetscSection   section, sectionSeq;

2707:     (*subdm)->sfMigration = dm->sfMigration;
2708:     PetscObjectReference((PetscObject) dm->sfMigration);
2709:     DMGetLocalSection((*subdm), &section);
2710:     PetscSFCreateInverseSF((*subdm)->sfMigration, &sfMigrationInv);
2711:     PetscSectionCreate(PetscObjectComm((PetscObject) (*subdm)), &sectionSeq);
2712:     PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);

2714:     DMPlexCreateGlobalToNaturalSF(*subdm, sectionSeq, (*subdm)->sfMigration, &sfNatural);
2715:     (*subdm)->sfNatural = sfNatural;
2716:     PetscSectionDestroy(&sectionSeq);
2717:     PetscSFDestroy(&sfMigrationInv);
2718:   }
2719:   return(0);
2720: }

2722: PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
2723: {
2725:   PetscInt       i = 0;

2728:   DMClone(dms[0], superdm);
2729:   DMCreateSectionSuperDM(dms, len, is, superdm);
2730:   (*superdm)->useNatural = PETSC_FALSE;
2731:   for (i = 0; i < len; i++){
2732:     if (dms[i]->useNatural && dms[i]->sfMigration) {
2733:       PetscSF        sfMigrationInv,sfNatural;
2734:       PetscSection   section, sectionSeq;

2736:       (*superdm)->sfMigration = dms[i]->sfMigration;
2737:       PetscObjectReference((PetscObject) dms[i]->sfMigration);
2738:       (*superdm)->useNatural = PETSC_TRUE;
2739:       DMGetLocalSection((*superdm), &section);
2740:       PetscSFCreateInverseSF((*superdm)->sfMigration, &sfMigrationInv);
2741:       PetscSectionCreate(PetscObjectComm((PetscObject) (*superdm)), &sectionSeq);
2742:       PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);

2744:       DMPlexCreateGlobalToNaturalSF(*superdm, sectionSeq, (*superdm)->sfMigration, &sfNatural);
2745:       (*superdm)->sfNatural = sfNatural;
2746:       PetscSectionDestroy(&sectionSeq);
2747:       PetscSFDestroy(&sfMigrationInv);
2748:       break;
2749:     }
2750:   }
2751:   return(0);
2752: }

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

2757:   Not collective

2759:   Input Parameter:
2760: . mesh - The DMPlex

2762:   Output Parameter:

2764:   Note:
2765:   This should be called after all calls to DMPlexSetCone()

2767:   Level: beginner

2769: .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2770: @*/
2771: PetscErrorCode DMPlexSymmetrize(DM dm)
2772: {
2773:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2774:   PetscInt      *offsets;
2775:   PetscInt       supportSize;
2776:   PetscInt       pStart, pEnd, p;

2781:   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
2782:   PetscLogEventBegin(DMPLEX_Symmetrize,dm,0,0,0);
2783:   /* Calculate support sizes */
2784:   DMPlexGetChart(dm, &pStart, &pEnd);
2785:   for (p = pStart; p < pEnd; ++p) {
2786:     PetscInt dof, off, c;

2788:     PetscSectionGetDof(mesh->coneSection, p, &dof);
2789:     PetscSectionGetOffset(mesh->coneSection, p, &off);
2790:     for (c = off; c < off+dof; ++c) {
2791:       PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);
2792:     }
2793:   }
2794:   for (p = pStart; p < pEnd; ++p) {
2795:     PetscInt dof;

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

2799:     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2800:   }
2801:   PetscSectionSetUp(mesh->supportSection);
2802:   /* Calculate supports */
2803:   PetscSectionGetStorageSize(mesh->supportSection, &supportSize);
2804:   PetscMalloc1(supportSize, &mesh->supports);
2805:   PetscCalloc1(pEnd - pStart, &offsets);
2806:   for (p = pStart; p < pEnd; ++p) {
2807:     PetscInt dof, off, c;

2809:     PetscSectionGetDof(mesh->coneSection, p, &dof);
2810:     PetscSectionGetOffset(mesh->coneSection, p, &off);
2811:     for (c = off; c < off+dof; ++c) {
2812:       const PetscInt q = mesh->cones[c];
2813:       PetscInt       offS;

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

2817:       mesh->supports[offS+offsets[q]] = p;
2818:       ++offsets[q];
2819:     }
2820:   }
2821:   PetscFree(offsets);
2822:   PetscLogEventEnd(DMPLEX_Symmetrize,dm,0,0,0);
2823:   return(0);
2824: }

2826: static PetscErrorCode DMPlexCreateDepthStratum(DM dm, DMLabel label, PetscInt depth, PetscInt pStart, PetscInt pEnd)
2827: {
2828:   IS             stratumIS;

2832:   if (pStart >= pEnd) return(0);
2833:   if (PetscDefined(USE_DEBUG)) {
2834:     PetscInt  qStart, qEnd, numLevels, level;
2835:     PetscBool overlap = PETSC_FALSE;
2836:     DMLabelGetNumValues(label, &numLevels);
2837:     for (level = 0; level < numLevels; level++) {
2838:       DMLabelGetStratumBounds(label, level, &qStart, &qEnd);
2839:       if ((pStart >= qStart && pStart < qEnd) || (pEnd > qStart && pEnd <= qEnd)) {overlap = PETSC_TRUE; break;}
2840:     }
2841:     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);
2842:   }
2843:   ISCreateStride(PETSC_COMM_SELF, pEnd-pStart, pStart, 1, &stratumIS);
2844:   DMLabelSetStratumIS(label, depth, stratumIS);
2845:   ISDestroy(&stratumIS);
2846:   return(0);
2847: }

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

2855:   Collective on dm

2857:   Input Parameter:
2858: . mesh - The DMPlex

2860:   Output Parameter:

2862:   Notes:
2863:   Concretely, DMPlexStratify() creates a new label named "depth" containing the depth in the DAG of each point. For cell-vertex
2864:   meshes, vertices are depth 0 and cells are depth 1. For fully interpolated meshes, depth 0 for vertices, 1 for edges, and so on
2865:   until cells have depth equal to the dimension of the mesh. The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2866:   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2867:   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.

2869:   The depth of a point is calculated by executing a breadth-first search (BFS) on the DAG. This could produce surprising results
2870:   if run on a partially interpolated mesh, meaning one that had some edges and faces, but not others. For example, suppose that
2871:   we had a mesh consisting of one triangle (c0) and three vertices (v0, v1, v2), and only one edge is on the boundary so we choose
2872:   to interpolate only that one (e0), so that
2873: $  cone(c0) = {e0, v2}
2874: $  cone(e0) = {v0, v1}
2875:   If DMPlexStratify() is run on this mesh, it will give depths
2876: $  depth 0 = {v0, v1, v2}
2877: $  depth 1 = {e0, c0}
2878:   where the triangle has been given depth 1, instead of 2, because it is reachable from vertex v2.

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

2882:   Level: beginner

2884: .seealso: DMPlexCreate(), DMPlexSymmetrize(), DMPlexComputeCellTypes()
2885: @*/
2886: PetscErrorCode DMPlexStratify(DM dm)
2887: {
2888:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2889:   DMLabel        label;
2890:   PetscInt       pStart, pEnd, p;
2891:   PetscInt       numRoots = 0, numLeaves = 0;

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

2898:   /* Create depth label */
2899:   DMPlexGetChart(dm, &pStart, &pEnd);
2900:   DMCreateLabel(dm, "depth");
2901:   DMPlexGetDepthLabel(dm, &label);

2903:   {
2904:     /* Initialize roots and count leaves */
2905:     PetscInt sMin = PETSC_MAX_INT;
2906:     PetscInt sMax = PETSC_MIN_INT;
2907:     PetscInt coneSize, supportSize;

2909:     for (p = pStart; p < pEnd; ++p) {
2910:       DMPlexGetConeSize(dm, p, &coneSize);
2911:       DMPlexGetSupportSize(dm, p, &supportSize);
2912:       if (!coneSize && supportSize) {
2913:         sMin = PetscMin(p, sMin);
2914:         sMax = PetscMax(p, sMax);
2915:         ++numRoots;
2916:       } else if (!supportSize && coneSize) {
2917:         ++numLeaves;
2918:       } else if (!supportSize && !coneSize) {
2919:         /* Isolated points */
2920:         sMin = PetscMin(p, sMin);
2921:         sMax = PetscMax(p, sMax);
2922:       }
2923:     }
2924:     DMPlexCreateDepthStratum(dm, label, 0, sMin, sMax+1);
2925:   }

2927:   if (numRoots + numLeaves == (pEnd - pStart)) {
2928:     PetscInt sMin = PETSC_MAX_INT;
2929:     PetscInt sMax = PETSC_MIN_INT;
2930:     PetscInt coneSize, supportSize;

2932:     for (p = pStart; p < pEnd; ++p) {
2933:       DMPlexGetConeSize(dm, p, &coneSize);
2934:       DMPlexGetSupportSize(dm, p, &supportSize);
2935:       if (!supportSize && coneSize) {
2936:         sMin = PetscMin(p, sMin);
2937:         sMax = PetscMax(p, sMax);
2938:       }
2939:     }
2940:     DMPlexCreateDepthStratum(dm, label, 1, sMin, sMax+1);
2941:   } else {
2942:     PetscInt level = 0;
2943:     PetscInt qStart, qEnd, q;

2945:     DMLabelGetStratumBounds(label, level, &qStart, &qEnd);
2946:     while (qEnd > qStart) {
2947:       PetscInt sMin = PETSC_MAX_INT;
2948:       PetscInt sMax = PETSC_MIN_INT;

2950:       for (q = qStart; q < qEnd; ++q) {
2951:         const PetscInt *support;
2952:         PetscInt        supportSize, s;

2954:         DMPlexGetSupportSize(dm, q, &supportSize);
2955:         DMPlexGetSupport(dm, q, &support);
2956:         for (s = 0; s < supportSize; ++s) {
2957:           sMin = PetscMin(support[s], sMin);
2958:           sMax = PetscMax(support[s], sMax);
2959:         }
2960:       }
2961:       DMLabelGetNumValues(label, &level);
2962:       DMPlexCreateDepthStratum(dm, label, level, sMin, sMax+1);
2963:       DMLabelGetStratumBounds(label, level, &qStart, &qEnd);
2964:     }
2965:   }
2966:   { /* just in case there is an empty process */
2967:     PetscInt numValues, maxValues = 0, v;

2969:     DMLabelGetNumValues(label, &numValues);
2970:     MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));
2971:     for (v = numValues; v < maxValues; v++) {
2972:       DMLabelAddStratum(label, v);
2973:     }
2974:   }
2975:   PetscObjectStateGet((PetscObject) label, &mesh->depthState);
2976:   PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);
2977:   return(0);
2978: }

2980: PetscErrorCode DMPlexComputeCellType_Internal(DM dm, PetscInt p, PetscInt pdepth, DMPolytopeType *pt)
2981: {
2982:   DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
2983:   PetscInt       dim, depth, pheight, coneSize;

2987:   DMGetDimension(dm, &dim);
2988:   DMPlexGetDepth(dm, &depth);
2989:   DMPlexGetConeSize(dm, p, &coneSize);
2990:   pheight = depth - pdepth;
2991:   if (depth <= 1) {
2992:     switch (pdepth) {
2993:       case 0: ct = DM_POLYTOPE_POINT;break;
2994:       case 1:
2995:         switch (coneSize) {
2996:           case 2: ct = DM_POLYTOPE_SEGMENT;break;
2997:           case 3: ct = DM_POLYTOPE_TRIANGLE;break;
2998:           case 4:
2999:           switch (dim) {
3000:             case 2: ct = DM_POLYTOPE_QUADRILATERAL;break;
3001:             case 3: ct = DM_POLYTOPE_TETRAHEDRON;break;
3002:             default: break;
3003:           }
3004:           break;
3005:         case 5: ct = DM_POLYTOPE_PYRAMID;break;
3006:         case 6: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
3007:         case 8: ct = DM_POLYTOPE_HEXAHEDRON;break;
3008:         default: break;
3009:       }
3010:     }
3011:   } else {
3012:     if (pdepth == 0) {
3013:       ct = DM_POLYTOPE_POINT;
3014:     } else if (pheight == 0) {
3015:       switch (dim) {
3016:         case 1:
3017:           switch (coneSize) {
3018:             case 2: ct = DM_POLYTOPE_SEGMENT;break;
3019:             default: break;
3020:           }
3021:           break;
3022:         case 2:
3023:           switch (coneSize) {
3024:             case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3025:             case 4: ct = DM_POLYTOPE_QUADRILATERAL;break;
3026:             default: break;
3027:           }
3028:           break;
3029:         case 3:
3030:           switch (coneSize) {
3031:             case 4: ct = DM_POLYTOPE_TETRAHEDRON;break;
3032:             case 5:
3033:             {
3034:               const PetscInt *cone;
3035:               PetscInt        faceConeSize;

3037:               DMPlexGetCone(dm, p, &cone);
3038:               DMPlexGetConeSize(dm, cone[0], &faceConeSize);
3039:               switch (faceConeSize) {
3040:                 case 3: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
3041:                 case 4: ct = DM_POLYTOPE_PYRAMID;break;
3042:               }
3043:             }
3044:             break;
3045:             case 6: ct = DM_POLYTOPE_HEXAHEDRON;break;
3046:             default: break;
3047:           }
3048:           break;
3049:         default: break;
3050:       }
3051:     } else if (pheight > 0) {
3052:       switch (coneSize) {
3053:         case 2: ct = DM_POLYTOPE_SEGMENT;break;
3054:         case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3055:         case 4: ct = DM_POLYTOPE_QUADRILATERAL;break;
3056:         default: break;
3057:       }
3058:     }
3059:   }
3060:   *pt = ct;
3061:   return(0);
3062: }

3064: /*@
3065:   DMPlexComputeCellTypes - Infer the polytope type of every cell using its dimension and cone size.

3067:   Collective on dm

3069:   Input Parameter:
3070: . mesh - The DMPlex

3072:   DMPlexComputeCellTypes() should be called after all calls to DMPlexSymmetrize() and DMPlexStratify()

3074:   Level: developer

3076:   Note: This function is normally called automatically by Plex when a cell type is requested. It creates an
3077:   internal DMLabel named "celltype" which can be directly accessed using DMGetLabel(). A user may disable
3078:   automatic creation by creating the label manually, using DMCreateLabel(dm, "celltype").

3080: .seealso: DMPlexCreate(), DMPlexSymmetrize(), DMPlexStratify(), DMGetLabel(), DMCreateLabel()
3081: @*/
3082: PetscErrorCode DMPlexComputeCellTypes(DM dm)
3083: {
3084:   DM_Plex       *mesh;
3085:   DMLabel        ctLabel;
3086:   PetscInt       pStart, pEnd, p;

3091:   mesh = (DM_Plex *) dm->data;
3092:   DMCreateLabel(dm, "celltype");
3093:   DMPlexGetCellTypeLabel(dm, &ctLabel);
3094:   DMPlexGetChart(dm, &pStart, &pEnd);
3095:   for (p = pStart; p < pEnd; ++p) {
3096:     DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
3097:     PetscInt       pdepth;

3099:     DMPlexGetPointDepth(dm, p, &pdepth);
3100:     DMPlexComputeCellType_Internal(dm, p, pdepth, &ct);
3101:     if (ct == DM_POLYTOPE_UNKNOWN) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Point %D is screwed up", p);
3102:     DMLabelSetValue(ctLabel, p, ct);
3103:   }
3104:   PetscObjectStateGet((PetscObject) ctLabel, &mesh->celltypeState);
3105:   PetscObjectViewFromOptions((PetscObject) ctLabel, NULL, "-dm_plex_celltypes_view");
3106:   return(0);
3107: }

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

3112:   Not Collective

3114:   Input Parameters:
3115: + dm - The DMPlex object
3116: . numPoints - The number of input points for the join
3117: - points - The input points

3119:   Output Parameters:
3120: + numCoveredPoints - The number of points in the join
3121: - coveredPoints - The points in the join

3123:   Level: intermediate

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

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

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

3133: .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
3134: @*/
3135: PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3136: {
3137:   DM_Plex       *mesh = (DM_Plex*) dm->data;
3138:   PetscInt      *join[2];
3139:   PetscInt       joinSize, i = 0;
3140:   PetscInt       dof, off, p, c, m;

3148:   DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[0]);
3149:   DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1]);
3150:   /* Copy in support of first point */
3151:   PetscSectionGetDof(mesh->supportSection, points[0], &dof);
3152:   PetscSectionGetOffset(mesh->supportSection, points[0], &off);
3153:   for (joinSize = 0; joinSize < dof; ++joinSize) {
3154:     join[i][joinSize] = mesh->supports[off+joinSize];
3155:   }
3156:   /* Check each successive support */
3157:   for (p = 1; p < numPoints; ++p) {
3158:     PetscInt newJoinSize = 0;

3160:     PetscSectionGetDof(mesh->supportSection, points[p], &dof);
3161:     PetscSectionGetOffset(mesh->supportSection, points[p], &off);
3162:     for (c = 0; c < dof; ++c) {
3163:       const PetscInt point = mesh->supports[off+c];

3165:       for (m = 0; m < joinSize; ++m) {
3166:         if (point == join[i][m]) {
3167:           join[1-i][newJoinSize++] = point;
3168:           break;
3169:         }
3170:       }
3171:     }
3172:     joinSize = newJoinSize;
3173:     i        = 1-i;
3174:   }
3175:   *numCoveredPoints = joinSize;
3176:   *coveredPoints    = join[i];
3177:   DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);
3178:   return(0);
3179: }

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

3184:   Not Collective

3186:   Input Parameters:
3187: + dm - The DMPlex object
3188: . numPoints - The number of input points for the join
3189: - points - The input points

3191:   Output Parameters:
3192: + numCoveredPoints - The number of points in the join
3193: - coveredPoints - The points in the join

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

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

3201:   Level: intermediate

3203: .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
3204: @*/
3205: PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3206: {

3214:   DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);
3215:   if (numCoveredPoints) *numCoveredPoints = 0;
3216:   return(0);
3217: }

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

3222:   Not Collective

3224:   Input Parameters:
3225: + dm - The DMPlex object
3226: . numPoints - The number of input points for the join
3227: - points - The input points

3229:   Output Parameters:
3230: + numCoveredPoints - The number of points in the join
3231: - coveredPoints - The points in the join

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

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

3239:   Level: intermediate

3241: .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
3242: @*/
3243: PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3244: {
3245:   DM_Plex       *mesh = (DM_Plex*) dm->data;
3246:   PetscInt      *offsets, **closures;
3247:   PetscInt      *join[2];
3248:   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
3249:   PetscInt       p, d, c, m, ms;


3258:   DMPlexGetDepth(dm, &depth);
3259:   PetscCalloc1(numPoints, &closures);
3260:   DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);
3261:   ms      = mesh->maxSupportSize;
3262:   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
3263:   DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);
3264:   DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);

3266:   for (p = 0; p < numPoints; ++p) {
3267:     PetscInt closureSize;

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

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

3275:       DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);
3276:       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
3277:         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3278:           offsets[p*(depth+2)+d+1] = i;
3279:           break;
3280:         }
3281:       }
3282:       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
3283:     }
3284:     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);
3285:   }
3286:   for (d = 0; d < depth+1; ++d) {
3287:     PetscInt dof;

3289:     /* Copy in support of first point */
3290:     dof = offsets[d+1] - offsets[d];
3291:     for (joinSize = 0; joinSize < dof; ++joinSize) {
3292:       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
3293:     }
3294:     /* Check each successive cone */
3295:     for (p = 1; p < numPoints && joinSize; ++p) {
3296:       PetscInt newJoinSize = 0;

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

3302:         for (m = 0; m < joinSize; ++m) {
3303:           if (point == join[i][m]) {
3304:             join[1-i][newJoinSize++] = point;
3305:             break;
3306:           }
3307:         }
3308:       }
3309:       joinSize = newJoinSize;
3310:       i        = 1-i;
3311:     }
3312:     if (joinSize) break;
3313:   }
3314:   *numCoveredPoints = joinSize;
3315:   *coveredPoints    = join[i];
3316:   for (p = 0; p < numPoints; ++p) {
3317:     DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);
3318:   }
3319:   PetscFree(closures);
3320:   DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);
3321:   DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);
3322:   return(0);
3323: }

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

3328:   Not Collective

3330:   Input Parameters:
3331: + dm - The DMPlex object
3332: . numPoints - The number of input points for the meet
3333: - points - The input points

3335:   Output Parameters:
3336: + numCoveredPoints - The number of points in the meet
3337: - coveredPoints - The points in the meet

3339:   Level: intermediate

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

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

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

3349: .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
3350: @*/
3351: PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
3352: {
3353:   DM_Plex       *mesh = (DM_Plex*) dm->data;
3354:   PetscInt      *meet[2];
3355:   PetscInt       meetSize, i = 0;
3356:   PetscInt       dof, off, p, c, m;

3364:   DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[0]);
3365:   DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1]);
3366:   /* Copy in cone of first point */
3367:   PetscSectionGetDof(mesh->coneSection, points[0], &dof);
3368:   PetscSectionGetOffset(mesh->coneSection, points[0], &off);
3369:   for (meetSize = 0; meetSize < dof; ++meetSize) {
3370:     meet[i][meetSize] = mesh->cones[off+meetSize];
3371:   }
3372:   /* Check each successive cone */
3373:   for (p = 1; p < numPoints; ++p) {
3374:     PetscInt newMeetSize = 0;

3376:     PetscSectionGetDof(mesh->coneSection, points[p], &dof);
3377:     PetscSectionGetOffset(mesh->coneSection, points[p], &off);
3378:     for (c = 0; c < dof; ++c) {
3379:       const PetscInt point = mesh->cones[off+c];

3381:       for (m = 0; m < meetSize; ++m) {
3382:         if (point == meet[i][m]) {
3383:           meet[1-i][newMeetSize++] = point;
3384:           break;
3385:         }
3386:       }
3387:     }
3388:     meetSize = newMeetSize;
3389:     i        = 1-i;
3390:   }
3391:   *numCoveringPoints = meetSize;
3392:   *coveringPoints    = meet[i];
3393:   DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);
3394:   return(0);
3395: }

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

3400:   Not Collective

3402:   Input Parameters:
3403: + dm - The DMPlex object
3404: . numPoints - The number of input points for the meet
3405: - points - The input points

3407:   Output Parameters:
3408: + numCoveredPoints - The number of points in the meet
3409: - coveredPoints - The points in the meet

3411:   Level: intermediate

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

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

3419: .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
3420: @*/
3421: PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3422: {

3430:   DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);
3431:   if (numCoveredPoints) *numCoveredPoints = 0;
3432:   return(0);
3433: }

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

3438:   Not Collective

3440:   Input Parameters:
3441: + dm - The DMPlex object
3442: . numPoints - The number of input points for the meet
3443: - points - The input points

3445:   Output Parameters:
3446: + numCoveredPoints - The number of points in the meet
3447: - coveredPoints - The points in the meet

3449:   Level: intermediate

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

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

3457: .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
3458: @*/
3459: PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3460: {
3461:   DM_Plex       *mesh = (DM_Plex*) dm->data;
3462:   PetscInt      *offsets, **closures;
3463:   PetscInt      *meet[2];
3464:   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
3465:   PetscInt       p, h, c, m, mc;


3474:   DMPlexGetDepth(dm, &height);
3475:   PetscMalloc1(numPoints, &closures);
3476:   DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);
3477:   mc      = mesh->maxConeSize;
3478:   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
3479:   DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);
3480:   DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);

3482:   for (p = 0; p < numPoints; ++p) {
3483:     PetscInt closureSize;

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

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

3491:       DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);
3492:       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
3493:         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3494:           offsets[p*(height+2)+h+1] = i;
3495:           break;
3496:         }
3497:       }
3498:       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
3499:     }
3500:     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);
3501:   }
3502:   for (h = 0; h < height+1; ++h) {
3503:     PetscInt dof;

3505:     /* Copy in cone of first point */
3506:     dof = offsets[h+1] - offsets[h];
3507:     for (meetSize = 0; meetSize < dof; ++meetSize) {
3508:       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
3509:     }
3510:     /* Check each successive cone */
3511:     for (p = 1; p < numPoints && meetSize; ++p) {
3512:       PetscInt newMeetSize = 0;

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

3518:         for (m = 0; m < meetSize; ++m) {
3519:           if (point == meet[i][m]) {
3520:             meet[1-i][newMeetSize++] = point;
3521:             break;
3522:           }
3523:         }
3524:       }
3525:       meetSize = newMeetSize;
3526:       i        = 1-i;
3527:     }
3528:     if (meetSize) break;
3529:   }
3530:   *numCoveredPoints = meetSize;
3531:   *coveredPoints    = meet[i];
3532:   for (p = 0; p < numPoints; ++p) {
3533:     DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);
3534:   }
3535:   PetscFree(closures);
3536:   DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);
3537:   DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);
3538:   return(0);
3539: }

3541: /*@C
3542:   DMPlexEqual - Determine if two DMs have the same topology

3544:   Not Collective

3546:   Input Parameters:
3547: + dmA - A DMPlex object
3548: - dmB - A DMPlex object

3550:   Output Parameters:
3551: . equal - PETSC_TRUE if the topologies are identical

3553:   Level: intermediate

3555:   Notes:
3556:   We are not solving graph isomorphism, so we do not permutation.

3558: .seealso: DMPlexGetCone()
3559: @*/
3560: PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
3561: {
3562:   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;


3570:   *equal = PETSC_FALSE;
3571:   DMPlexGetDepth(dmA, &depth);
3572:   DMPlexGetDepth(dmB, &depthB);
3573:   if (depth != depthB) return(0);
3574:   DMPlexGetChart(dmA, &pStart,  &pEnd);
3575:   DMPlexGetChart(dmB, &pStartB, &pEndB);
3576:   if ((pStart != pStartB) || (pEnd != pEndB)) return(0);
3577:   for (p = pStart; p < pEnd; ++p) {
3578:     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
3579:     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;

3581:     DMPlexGetConeSize(dmA, p, &coneSize);
3582:     DMPlexGetCone(dmA, p, &cone);
3583:     DMPlexGetConeOrientation(dmA, p, &ornt);
3584:     DMPlexGetConeSize(dmB, p, &coneSizeB);
3585:     DMPlexGetCone(dmB, p, &coneB);
3586:     DMPlexGetConeOrientation(dmB, p, &orntB);
3587:     if (coneSize != coneSizeB) return(0);
3588:     for (c = 0; c < coneSize; ++c) {
3589:       if (cone[c] != coneB[c]) return(0);
3590:       if (ornt[c] != orntB[c]) return(0);
3591:     }
3592:     DMPlexGetSupportSize(dmA, p, &supportSize);
3593:     DMPlexGetSupport(dmA, p, &support);
3594:     DMPlexGetSupportSize(dmB, p, &supportSizeB);
3595:     DMPlexGetSupport(dmB, p, &supportB);
3596:     if (supportSize != supportSizeB) return(0);
3597:     for (s = 0; s < supportSize; ++s) {
3598:       if (support[s] != supportB[s]) return(0);
3599:     }
3600:   }
3601:   *equal = PETSC_TRUE;
3602:   return(0);
3603: }

3605: /*@C
3606:   DMPlexGetNumFaceVertices - Returns the number of vertices on a face

3608:   Not Collective

3610:   Input Parameters:
3611: + dm         - The DMPlex
3612: . cellDim    - The cell dimension
3613: - numCorners - The number of vertices on a cell

3615:   Output Parameters:
3616: . numFaceVertices - The number of vertices on a face

3618:   Level: developer

3620:   Notes:
3621:   Of course this can only work for a restricted set of symmetric shapes

3623: .seealso: DMPlexGetCone()
3624: @*/
3625: PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
3626: {
3627:   MPI_Comm       comm;

3631:   PetscObjectGetComm((PetscObject)dm,&comm);
3633:   switch (cellDim) {
3634:   case 0:
3635:     *numFaceVertices = 0;
3636:     break;
3637:   case 1:
3638:     *numFaceVertices = 1;
3639:     break;
3640:   case 2:
3641:     switch (numCorners) {
3642:     case 3: /* triangle */
3643:       *numFaceVertices = 2; /* Edge has 2 vertices */
3644:       break;
3645:     case 4: /* quadrilateral */
3646:       *numFaceVertices = 2; /* Edge has 2 vertices */
3647:       break;
3648:     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
3649:       *numFaceVertices = 3; /* Edge has 3 vertices */
3650:       break;
3651:     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
3652:       *numFaceVertices = 3; /* Edge has 3 vertices */
3653:       break;
3654:     default:
3655:       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3656:     }
3657:     break;
3658:   case 3:
3659:     switch (numCorners) {
3660:     case 4: /* tetradehdron */
3661:       *numFaceVertices = 3; /* Face has 3 vertices */
3662:       break;
3663:     case 6: /* tet cohesive cells */
3664:       *numFaceVertices = 4; /* Face has 4 vertices */
3665:       break;
3666:     case 8: /* hexahedron */
3667:       *numFaceVertices = 4; /* Face has 4 vertices */
3668:       break;
3669:     case 9: /* tet cohesive Lagrange cells */
3670:       *numFaceVertices = 6; /* Face has 6 vertices */
3671:       break;
3672:     case 10: /* quadratic tetrahedron */
3673:       *numFaceVertices = 6; /* Face has 6 vertices */
3674:       break;
3675:     case 12: /* hex cohesive Lagrange cells */
3676:       *numFaceVertices = 6; /* Face has 6 vertices */
3677:       break;
3678:     case 18: /* quadratic tet cohesive Lagrange cells */
3679:       *numFaceVertices = 6; /* Face has 6 vertices */
3680:       break;
3681:     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
3682:       *numFaceVertices = 9; /* Face has 9 vertices */
3683:       break;
3684:     default:
3685:       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3686:     }
3687:     break;
3688:   default:
3689:     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
3690:   }
3691:   return(0);
3692: }

3694: /*@
3695:   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point

3697:   Not Collective

3699:   Input Parameter:
3700: . dm    - The DMPlex object

3702:   Output Parameter:
3703: . depthLabel - The DMLabel recording point depth

3705:   Level: developer

3707: .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum(), DMPlexGetPointDepth(),
3708: @*/
3709: PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
3710: {
3714:   *depthLabel = dm->depthLabel;
3715:   return(0);
3716: }

3718: /*@
3719:   DMPlexGetDepth - Get the depth of the DAG representing this mesh

3721:   Not Collective

3723:   Input Parameter:
3724: . dm    - The DMPlex object

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

3729:   Level: developer

3731:   Notes:
3732:   This returns maximum of point depths over all points, i.e. maximum value of the label returned by DMPlexGetDepthLabel().
3733:   The point depth is described more in detail in DMPlexGetDepthStratum().
3734:   An empty mesh gives -1.

3736: .seealso: DMPlexGetDepthLabel(), DMPlexGetDepthStratum(), DMPlexGetPointDepth(), DMPlexSymmetrize()
3737: @*/
3738: PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
3739: {
3740:   DMLabel        label;
3741:   PetscInt       d = 0;

3747:   DMPlexGetDepthLabel(dm, &label);
3748:   if (label) {DMLabelGetNumValues(label, &d);}
3749:   *depth = d-1;
3750:   return(0);
3751: }

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

3756:   Not Collective

3758:   Input Parameters:
3759: + dm           - The DMPlex object
3760: - stratumValue - The requested depth

3762:   Output Parameters:
3763: + start - The first point at this depth
3764: - end   - One beyond the last point at this depth

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

3771:   Level: developer

3773: .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth(), DMPlexGetDepthLabel(), DMPlexGetPointDepth(), DMPlexSymmetrize(), DMPlexInterpolate()
3774: @*/
3775: PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
3776: {
3777:   DMLabel        label;
3778:   PetscInt       pStart, pEnd;

3785:   DMPlexGetChart(dm, &pStart, &pEnd);
3786:   if (pStart == pEnd) return(0);
3787:   if (stratumValue < 0) {
3788:     if (start) *start = pStart;
3789:     if (end)   *end   = pEnd;
3790:     return(0);
3791:   }
3792:   DMPlexGetDepthLabel(dm, &label);
3793:   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
3794:   DMLabelGetStratumBounds(label, stratumValue, start, end);
3795:   return(0);
3796: }

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

3801:   Not Collective

3803:   Input Parameters:
3804: + dm           - The DMPlex object
3805: - stratumValue - The requested height

3807:   Output Parameters:
3808: + start - The first point at this height
3809: - end   - One beyond the last point at this height

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

3816:   Level: developer

3818: .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth(), DMPlexGetPointHeight()
3819: @*/
3820: PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
3821: {
3822:   DMLabel        label;
3823:   PetscInt       depth, pStart, pEnd;

3830:   DMPlexGetChart(dm, &pStart, &pEnd);
3831:   if (pStart == pEnd) return(0);
3832:   if (stratumValue < 0) {
3833:     if (start) *start = pStart;
3834:     if (end)   *end   = pEnd;
3835:     return(0);
3836:   }
3837:   DMPlexGetDepthLabel(dm, &label);
3838:   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
3839:   DMLabelGetNumValues(label, &depth);
3840:   DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);
3841:   return(0);
3842: }

3844: /*@
3845:   DMPlexGetPointDepth - Get the depth of a given point

3847:   Not Collective

3849:   Input Parameter:
3850: + dm    - The DMPlex object
3851: - point - The point

3853:   Output Parameter:
3854: . depth - The depth of the point

3856:   Level: intermediate

3858: .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexGetPointHeight()
3859: @*/
3860: PetscErrorCode DMPlexGetPointDepth(DM dm, PetscInt point, PetscInt *depth)
3861: {

3867:   DMLabelGetValue(dm->depthLabel, point, depth);
3868:   return(0);
3869: }

3871: /*@
3872:   DMPlexGetPointHeight - Get the height of a given point

3874:   Not Collective

3876:   Input Parameter:
3877: + dm    - The DMPlex object
3878: - point - The point

3880:   Output Parameter:
3881: . height - The height of the point

3883:   Level: intermediate

3885: .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexGetPointDepth()
3886: @*/
3887: PetscErrorCode DMPlexGetPointHeight(DM dm, PetscInt point, PetscInt *height)
3888: {
3889:   PetscInt       n, pDepth;

3895:   DMLabelGetNumValues(dm->depthLabel, &n);
3896:   DMLabelGetValue(dm->depthLabel, point, &pDepth);
3897:   *height = n - 1 - pDepth;  /* DAG depth is n-1 */
3898:   return(0);
3899: }

3901: /*@
3902:   DMPlexGetCellTypeLabel - Get the DMLabel recording the polytope type of each cell

3904:   Not Collective

3906:   Input Parameter:
3907: . dm - The DMPlex object

3909:   Output Parameter:
3910: . celltypeLabel - The DMLabel recording cell polytope type

3912:   Note: This function will trigger automatica computation of cell types. This can be disabled by calling
3913:   DMCreateLabel(dm, "celltype") beforehand.

3915:   Level: developer

3917: .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMCreateLabel()
3918: @*/
3919: PetscErrorCode DMPlexGetCellTypeLabel(DM dm, DMLabel *celltypeLabel)
3920: {

3926:   if (!dm->celltypeLabel) {DMPlexComputeCellTypes(dm);}
3927:   *celltypeLabel = dm->celltypeLabel;
3928:   return(0);
3929: }

3931: /*@
3932:   DMPlexGetCellType - Get the polytope type of a given cell

3934:   Not Collective

3936:   Input Parameter:
3937: + dm   - The DMPlex object
3938: - cell - The cell

3940:   Output Parameter:
3941: . celltype - The polytope type of the cell

3943:   Level: intermediate

3945: .seealso: DMPlexGetCellTypeLabel(), DMPlexGetDepthLabel(), DMPlexGetDepth()
3946: @*/
3947: PetscErrorCode DMPlexGetCellType(DM dm, PetscInt cell, DMPolytopeType *celltype)
3948: {
3949:   DMLabel        label;
3950:   PetscInt       ct;

3956:   DMPlexGetCellTypeLabel(dm, &label);
3957:   DMLabelGetValue(label, cell, &ct);
3958:   *celltype = (DMPolytopeType) ct;
3959:   return(0);
3960: }

3962: /*@
3963:   DMPlexSetCellType - Set the polytope type of a given cell

3965:   Not Collective

3967:   Input Parameters:
3968: + dm   - The DMPlex object
3969: . cell - The cell
3970: - celltype - The polytope type of the cell

3972:   Note: By default, cell types will be automatically computed using DMPlexComputeCellTypes() before this function
3973:   is executed. This function will override the computed type. However, if automatic classification will not succeed
3974:   and a user wants to manually specify all types, the classification must be disabled by calling
3975:   DMCreaateLabel(dm, "celltype") before getting or setting any cell types.

3977:   Level: advanced

3979: .seealso: DMPlexGetCellTypeLabel(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexComputeCellTypes(), DMCreateLabel()
3980: @*/
3981: PetscErrorCode DMPlexSetCellType(DM dm, PetscInt cell, DMPolytopeType celltype)
3982: {
3983:   DMLabel        label;

3988:   DMPlexGetCellTypeLabel(dm, &label);
3989:   DMLabelSetValue(label, cell, celltype);
3990:   return(0);
3991: }

3993: PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
3994: {
3995:   PetscSection   section, s;
3996:   Mat            m;
3997:   PetscInt       maxHeight;

4001:   DMClone(dm, cdm);
4002:   DMPlexGetMaxProjectionHeight(dm, &maxHeight);
4003:   DMPlexSetMaxProjectionHeight(*cdm, maxHeight);
4004:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);
4005:   DMSetLocalSection(*cdm, section);
4006:   PetscSectionDestroy(&section);
4007:   PetscSectionCreate(PETSC_COMM_SELF, &s);
4008:   MatCreate(PETSC_COMM_SELF, &m);
4009:   DMSetDefaultConstraints(*cdm, s, m);
4010:   PetscSectionDestroy(&s);
4011:   MatDestroy(&m);

4013:   DMSetNumFields(*cdm, 1);
4014:   DMCreateDS(*cdm);
4015:   return(0);
4016: }

4018: PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
4019: {
4020:   Vec            coordsLocal;
4021:   DM             coordsDM;

4025:   *field = NULL;
4026:   DMGetCoordinatesLocal(dm,&coordsLocal);
4027:   DMGetCoordinateDM(dm,&coordsDM);
4028:   if (coordsLocal && coordsDM) {
4029:     DMFieldCreateDS(coordsDM, 0, coordsLocal, field);
4030:   }
4031:   return(0);
4032: }

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

4037:   Not Collective

4039:   Input Parameters:
4040: . dm        - The DMPlex object

4042:   Output Parameter:
4043: . section - The PetscSection object

4045:   Level: developer

4047: .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
4048: @*/
4049: PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
4050: {
4051:   DM_Plex *mesh = (DM_Plex*) dm->data;

4055:   if (section) *section = mesh->coneSection;
4056:   return(0);
4057: }

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

4062:   Not Collective

4064:   Input Parameters:
4065: . dm        - The DMPlex object

4067:   Output Parameter:
4068: . section - The PetscSection object

4070:   Level: developer

4072: .seealso: DMPlexGetConeSection()
4073: @*/
4074: PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
4075: {
4076:   DM_Plex *mesh = (DM_Plex*) dm->data;

4080:   if (section) *section = mesh->supportSection;
4081:   return(0);
4082: }

4084: /*@C
4085:   DMPlexGetCones - Return cone data

4087:   Not Collective

4089:   Input Parameters:
4090: . dm        - The DMPlex object

4092:   Output Parameter:
4093: . cones - The cone for each point

4095:   Level: developer

4097: .seealso: DMPlexGetConeSection()
4098: @*/
4099: PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
4100: {
4101:   DM_Plex *mesh = (DM_Plex*) dm->data;

4105:   if (cones) *cones = mesh->cones;
4106:   return(0);
4107: }

4109: /*@C
4110:   DMPlexGetConeOrientations - Return cone orientation data

4112:   Not Collective

4114:   Input Parameters:
4115: . dm        - The DMPlex object

4117:   Output Parameter:
4118: . coneOrientations - The cone orientation for each point

4120:   Level: developer

4122: .seealso: DMPlexGetConeSection()
4123: @*/
4124: PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
4125: {
4126:   DM_Plex *mesh = (DM_Plex*) dm->data;

4130:   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
4131:   return(0);
4132: }

4134: /******************************** FEM Support **********************************/

4136: /*
4137:  Returns number of components and tensor degree for the field.  For interpolated meshes, line should be a point
4138:  representing a line in the section.
4139: */
4140: static PetscErrorCode PetscSectionFieldGetTensorDegree_Private(PetscSection section,PetscInt field,PetscInt line,PetscBool vertexchart,PetscInt *Nc,PetscInt *k)
4141: {

4145:   PetscSectionGetFieldComponents(section, field, Nc);
4146:   if (line < 0) {
4147:     *k = 0;
4148:     *Nc = 0;
4149:   } else if (vertexchart) {            /* If we only have a vertex chart, we must have degree k=1 */
4150:     *k = 1;
4151:   } else {                      /* Assume the full interpolated mesh is in the chart; lines in particular */
4152:     /* An order k SEM disc has k-1 dofs on an edge */
4153:     PetscSectionGetFieldDof(section, line, field, k);
4154:     *k = *k / *Nc + 1;
4155:   }
4156:   return(0);
4157: }

4159: /*@

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

4165:   Input Parameters:
4166: + dm      - The DM
4167: . point   - Either a cell (highest dim point) or an edge (dim 1 point), or PETSC_DETERMINE
4168: - section - The PetscSection to reorder, or NULL for the default section

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

4173:   Example:
4174:   A typical interpolated single-quad mesh might order points as
4175: .vb
4176:   [c0, v1, v2, v3, v4, e5, e6, e7, e8]

4178:   v4 -- e6 -- v3
4179:   |           |
4180:   e7    c0    e8
4181:   |           |
4182:   v1 -- e5 -- v2
4183: .ve

4185:   (There is no significance to the ordering described here.)  The default section for a Q3 quad might typically assign
4186:   dofs in the order of points, e.g.,
4187: .vb
4188:     c0 -> [0,1,2,3]
4189:     v1 -> [4]
4190:     ...
4191:     e5 -> [8, 9]
4192: .ve

4194:   which corresponds to the dofs
4195: .vb
4196:     6   10  11  7
4197:     13  2   3   15
4198:     12  0   1   14
4199:     4   8   9   5
4200: .ve

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

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

4212:   Level: developer

4214: .seealso: DMGetLocalSection(), PetscSectionSetClosurePermutation(), DMSetGlobalSection()
4215: @*/
4216: PetscErrorCode DMPlexSetClosurePermutationTensor(DM dm, PetscInt point, PetscSection section)
4217: {
4218:   DMLabel        label;
4219:   PetscInt       dim, depth = -1, eStart = -1, Nf;
4220:   PetscBool      vertexchart;

4224:   DMGetDimension(dm, &dim);
4225:   if (dim < 1) return(0);
4226:   if (point < 0) {
4227:     PetscInt sStart,sEnd;

4229:     DMPlexGetDepthStratum(dm, 1, &sStart, &sEnd);
4230:     point = sEnd-sStart ? sStart : point;
4231:   }
4232:   DMPlexGetDepthLabel(dm, &label);
4233:   if (point >= 0) { DMLabelGetValue(label, point, &depth); }
4234:   if (!section) {DMGetLocalSection(dm, &section);}
4235:   if (depth == 1) {eStart = point;}
4236:   else if  (depth == dim) {
4237:     const PetscInt *cone;

4239:     DMPlexGetCone(dm, point, &cone);
4240:     if (dim == 2) eStart = cone[0];
4241:     else if (dim == 3) {
4242:       const PetscInt *cone2;
4243:       DMPlexGetCone(dm, cone[0], &cone2);
4244:       eStart = cone2[0];
4245:     } 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);
4246:   } else if (depth >= 0) 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);
4247:   {                             /* Determine whether the chart covers all points or just vertices. */
4248:     PetscInt pStart,pEnd,cStart,cEnd;
4249:     DMPlexGetDepthStratum(dm,0,&pStart,&pEnd);
4250:     PetscSectionGetChart(section,&cStart,&cEnd);
4251:     if (pStart == cStart && pEnd == cEnd) vertexchart = PETSC_TRUE; /* Just vertices */
4252:     else vertexchart = PETSC_FALSE;                                 /* Assume all interpolated points are in chart */
4253:   }
4254:   PetscSectionGetNumFields(section, &Nf);
4255:   for (PetscInt d=1; d<=dim; d++) {
4256:     PetscInt k, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
4257:     PetscInt *perm;

4259:     for (f = 0; f < Nf; ++f) {
4260:       PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);
4261:       size += PetscPowInt(k+1, d)*Nc;
4262:     }
4263:     PetscMalloc1(size, &perm);
4264:     for (f = 0; f < Nf; ++f) {
4265:       switch (d) {
4266:       case 1:
4267:         PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);
4268:         /*
4269:          Original ordering is [ edge of length k-1; vtx0; vtx1 ]
4270:          We want              [ vtx0; edge of length k-1; vtx1 ]
4271:          */
4272:         for (c=0; c<Nc; c++,offset++) perm[offset] = (k-1)*Nc + c + foffset;
4273:         for (i=0; i<k-1; i++) for (c=0; c<Nc; c++,offset++) perm[offset] = i*Nc + c + foffset;
4274:         for (c=0; c<Nc; c++,offset++) perm[offset] = k*Nc + c + foffset;
4275:         foffset = offset;
4276:         break;
4277:       case 2:
4278:         /* The original quad closure is oriented clockwise, {f, e_b, e_r, e_t, e_l, v_lb, v_rb, v_tr, v_tl} */
4279:         PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);
4280:         /* The SEM order is

4282:          v_lb, {e_b}, v_rb,
4283:          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
4284:          v_lt, reverse {e_t}, v_rt
4285:          */
4286:         {
4287:           const PetscInt of   = 0;
4288:           const PetscInt oeb  = of   + PetscSqr(k-1);
4289:           const PetscInt oer  = oeb  + (k-1);
4290:           const PetscInt oet  = oer  + (k-1);
4291:           const PetscInt oel  = oet  + (k-1);
4292:           const PetscInt ovlb = oel  + (k-1);
4293:           const PetscInt ovrb = ovlb + 1;
4294:           const PetscInt ovrt = ovrb + 1;
4295:           const PetscInt ovlt = ovrt + 1;
4296:           PetscInt       o;

4298:           /* bottom */
4299:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
4300:           for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
4301:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
4302:           /* middle */
4303:           for (i = 0; i < k-1; ++i) {
4304:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
4305:             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;
4306:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
4307:           }
4308:           /* top */
4309:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
4310:           for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
4311:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
4312:           foffset = offset;
4313:         }
4314:         break;
4315:       case 3:
4316:         /* The original hex closure is

4318:          {c,
4319:          f_b, f_t, f_f, f_b, f_r, f_l,
4320:          e_bl, e_bb, e_br, e_bf,  e_tf, e_tr, e_tb, e_tl,  e_rf, e_lf, e_lb, e_rb,
4321:          v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
4322:          */
4323:         PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);
4324:         /* The SEM order is
4325:          Bottom Slice
4326:          v_blf, {e^{(k-1)-n}_bf}, v_brf,
4327:          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
4328:          v_blb, {e_bb}, v_brb,

4330:          Middle Slice (j)
4331:          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
4332:          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
4333:          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,

4335:          Top Slice
4336:          v_tlf, {e_tf}, v_trf,
4337:          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
4338:          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
4339:          */
4340:         {
4341:           const PetscInt oc    = 0;
4342:           const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
4343:           const PetscInt oft   = ofb   + PetscSqr(k-1);
4344:           const PetscInt off   = oft   + PetscSqr(k-1);
4345:           const PetscInt ofk   = off   + PetscSqr(k-1);
4346:           const PetscInt ofr   = ofk   + PetscSqr(k-1);
4347:           const PetscInt ofl   = ofr   + PetscSqr(k-1);
4348:           const PetscInt oebl  = ofl   + PetscSqr(k-1);
4349:           const PetscInt oebb  = oebl  + (k-1);
4350:           const PetscInt oebr  = oebb  + (k-1);
4351:           const PetscInt oebf  = oebr  + (k-1);
4352:           const PetscInt oetf  = oebf  + (k-1);
4353:           const PetscInt oetr  = oetf  + (k-1);
4354:           const PetscInt oetb  = oetr  + (k-1);
4355:           const PetscInt oetl  = oetb  + (k-1);
4356:           const PetscInt oerf  = oetl  + (k-1);
4357:           const PetscInt oelf  = oerf  + (k-1);
4358:           const PetscInt oelb  = oelf  + (k-1);
4359:           const PetscInt oerb  = oelb  + (k-1);
4360:           const PetscInt ovblf = oerb  + (k-1);
4361:           const PetscInt ovblb = ovblf + 1;
4362:           const PetscInt ovbrb = ovblb + 1;
4363:           const PetscInt ovbrf = ovbrb + 1;
4364:           const PetscInt ovtlf = ovbrf + 1;
4365:           const PetscInt ovtrf = ovtlf + 1;
4366:           const PetscInt ovtrb = ovtrf + 1;
4367:           const PetscInt ovtlb = ovtrb + 1;
4368:           PetscInt       o, n;

4370:           /* Bottom Slice */
4371:           /*   bottom */
4372:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
4373:           for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
4374:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
4375:           /*   middle */
4376:           for (i = 0; i < k-1; ++i) {
4377:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
4378:             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;}
4379:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
4380:           }
4381:           /*   top */
4382:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
4383:           for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
4384:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;

4386:           /* Middle Slice */
4387:           for (j = 0; j < k-1; ++j) {
4388:             /*   bottom */
4389:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
4390:             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;
4391:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
4392:             /*   middle */
4393:             for (i = 0; i < k-1; ++i) {
4394:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
4395:               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;
4396:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
4397:             }
4398:             /*   top */
4399:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
4400:             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;
4401:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
4402:           }

4404:           /* Top Slice */
4405:           /*   bottom */
4406:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
4407:           for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
4408:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
4409:           /*   middle */
4410:           for (i = 0; i < k-1; ++i) {
4411:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
4412:             for (n = 0; n < k-1; ++n) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oft+i*(k-1)+n)*Nc + c + foffset;
4413:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
4414:           }
4415:           /*   top */
4416:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
4417:           for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
4418:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;

4420:           foffset = offset;
4421:         }
4422:         break;
4423:       default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", d);
4424:       }
4425:     }
4426:     if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
4427:     /* Check permutation */
4428:     {
4429:       PetscInt *check;

4431:       PetscMalloc1(size, &check);
4432:       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]);}
4433:       for (i = 0; i < size; ++i) check[perm[i]] = i;
4434:       for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
4435:       PetscFree(check);
4436:     }
4437:     PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, d, size, PETSC_OWN_POINTER, perm);
4438:   }
4439:   return(0);
4440: }

4442: PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
4443: {
4444:   PetscDS        prob;
4445:   PetscInt       depth, Nf, h;
4446:   DMLabel        label;

4450:   DMGetDS(dm, &prob);
4451:   Nf      = prob->Nf;
4452:   label   = dm->depthLabel;
4453:   *dspace = NULL;
4454:   if (field < Nf) {
4455:     PetscObject disc = prob->disc[field];

4457:     if (disc->classid == PETSCFE_CLASSID) {
4458:       PetscDualSpace dsp;

4460:       PetscFEGetDualSpace((PetscFE)disc,&dsp);
4461:       DMLabelGetNumValues(label,&depth);
4462:       DMLabelGetValue(label,point,&h);
4463:       h    = depth - 1 - h;
4464:       if (h) {
4465:         PetscDualSpaceGetHeightSubspace(dsp,h,dspace);
4466:       } else {
4467:         *dspace = dsp;
4468:       }
4469:     }
4470:   }
4471:   return(0);
4472: }


4475: PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4476: {
4477:   PetscScalar    *array, *vArray;
4478:   const PetscInt *cone, *coneO;
4479:   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
4480:   PetscErrorCode  ierr;

4483:   PetscSectionGetChart(section, &pStart, &pEnd);
4484:   DMPlexGetConeSize(dm, point, &numPoints);
4485:   DMPlexGetCone(dm, point, &cone);
4486:   DMPlexGetConeOrientation(dm, point, &coneO);
4487:   if (!values || !*values) {
4488:     if ((point >= pStart) && (point < pEnd)) {
4489:       PetscInt dof;

4491:       PetscSectionGetDof(section, point, &dof);
4492:       size += dof;
4493:     }
4494:     for (p = 0; p < numPoints; ++p) {
4495:       const PetscInt cp = cone[p];
4496:       PetscInt       dof;

4498:       if ((cp < pStart) || (cp >= pEnd)) continue;
4499:       PetscSectionGetDof(section, cp, &dof);
4500:       size += dof;
4501:     }
4502:     if (!values) {
4503:       if (csize) *csize = size;
4504:       return(0);
4505:     }
4506:     DMGetWorkArray(dm, size, MPIU_SCALAR, &array);
4507:   } else {
4508:     array = *values;
4509:   }
4510:   size = 0;
4511:   VecGetArray(v, &vArray);
4512:   if ((point >= pStart) && (point < pEnd)) {
4513:     PetscInt     dof, off, d;
4514:     PetscScalar *varr;

4516:     PetscSectionGetDof(section, point, &dof);
4517:     PetscSectionGetOffset(section, point, &off);
4518:     varr = &vArray[off];
4519:     for (d = 0; d < dof; ++d, ++offset) {
4520:       array[offset] = varr[d];
4521:     }
4522:     size += dof;
4523:   }
4524:   for (p = 0; p < numPoints; ++p) {
4525:     const PetscInt cp = cone[p];
4526:     PetscInt       o  = coneO[p];
4527:     PetscInt       dof, off, d;
4528:     PetscScalar   *varr;

4530:     if ((cp < pStart) || (cp >= pEnd)) continue;
4531:     PetscSectionGetDof(section, cp, &dof);
4532:     PetscSectionGetOffset(section, cp, &off);
4533:     varr = &vArray[off];
4534:     if (o >= 0) {
4535:       for (d = 0; d < dof; ++d, ++offset) {
4536:         array[offset] = varr[d];
4537:       }
4538:     } else {
4539:       for (d = dof-1; d >= 0; --d, ++offset) {
4540:         array[offset] = varr[d];
4541:       }
4542:     }
4543:     size += dof;
4544:   }
4545:   VecRestoreArray(v, &vArray);
4546:   if (!*values) {
4547:     if (csize) *csize = size;
4548:     *values = array;
4549:   } else {
4550:     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
4551:     *csize = size;
4552:   }
4553:   return(0);
4554: }

4556: /* Compress out points not in the section */
4557: PETSC_STATIC_INLINE PetscErrorCode CompressPoints_Private(PetscSection section, PetscInt *numPoints, PetscInt points[])
4558: {
4559:   const PetscInt np = *numPoints;
4560:   PetscInt       pStart, pEnd, p, q;

4563:   PetscSectionGetChart(section, &pStart, &pEnd);
4564:   for (p = 0, q = 0; p < np; ++p) {
4565:     const PetscInt r = points[p*2];
4566:     if ((r >= pStart) && (r < pEnd)) {
4567:       points[q*2]   = r;
4568:       points[q*2+1] = points[p*2+1];
4569:       ++q;
4570:     }
4571:   }
4572:   *numPoints = q;
4573:   return 0;
4574: }

4576: static PetscErrorCode DMPlexTransitiveClosure_Hybrid_Internal(DM dm, PetscInt point, PetscInt np, PetscInt *numPoints, PetscInt **points)
4577: {
4578:   const PetscInt *cone, *ornt;
4579:   PetscInt       *pts,  *closure = NULL;
4580:   PetscInt        dim, coneSize, c, d, clSize, cl;
4581:   PetscErrorCode  ierr;

4584:   DMGetDimension(dm, &dim);
4585:   DMPlexGetConeSize(dm, point, &coneSize);
4586:   DMPlexGetCone(dm, point, &cone);
4587:   DMPlexGetConeOrientation(dm, point, &ornt);
4588:   DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &clSize, &closure);
4589:   DMGetWorkArray(dm, np*2, MPIU_INT, &pts);
4590:   c    = 0;
4591:   pts[c*2+0] = point;
4592:   pts[c*2+1] = 0;
4593:   ++c;
4594:   for (cl = 0; cl < clSize*2; cl += 2, ++c) {pts[c*2+0] = closure[cl]; pts[c*2+1] = closure[cl+1];}
4595:   DMPlexGetTransitiveClosure(dm, cone[1], PETSC_TRUE, &clSize, &closure);
4596:   for (cl = 0; cl < clSize*2; cl += 2, ++c) {pts[c*2+0] = closure[cl]; pts[c*2+1] = closure[cl+1];}
4597:   DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &clSize, &closure);
4598:   if (dim >= 2) {
4599:     for (d = 2; d < coneSize; ++d, ++c) {pts[c*2+0] = cone[d]; pts[c*2+1] = ornt[d];}
4600:   }
4601:   if (dim >= 3) {
4602:     for (d = 2; d < coneSize; ++d) {
4603:       const PetscInt  fpoint = cone[d];
4604:       const PetscInt *fcone;
4605:       PetscInt        fconeSize, fc, i;

4607:       DMPlexGetConeSize(dm, fpoint, &fconeSize);
4608:       DMPlexGetCone(dm, fpoint, &fcone);
4609:       for (fc = 0; fc < fconeSize; ++fc) {
4610:         for (i = 0; i < c; ++i) if (pts[i*2] == fcone[fc]) break;
4611:         if (i == c) {pts[c*2+0] = fcone[fc]; pts[c*2+1] = 0; ++c;}
4612:       }
4613:     }
4614:   }
4615:   if (c != np) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid closure for hybrid point %D, size %D != %D", point, c, np);
4616:   *numPoints = np;
4617:   *points    = pts;
4618:   return(0);
4619: }

4621: /* Compressed closure does not apply closure permutation */
4622: PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4623: {
4624:   const PetscInt *cla = NULL;
4625:   PetscInt       np, *pts = NULL;

4629:   PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);
4630:   if (*clPoints) {
4631:     PetscInt dof, off;

4633:     PetscSectionGetDof(*clSec, point, &dof);
4634:     PetscSectionGetOffset(*clSec, point, &off);
4635:     ISGetIndices(*clPoints, &cla);
4636:     np   = dof/2;
4637:     pts  = (PetscInt *) &cla[off];
4638:   } else {
4639:     DMPolytopeType ct;

4641:     /* Do not make the label if it does not exist */
4642:     if (!dm->celltypeLabel) {ct = DM_POLYTOPE_POINT;}
4643:     else                    {DMPlexGetCellType(dm, point, &ct);}
4644:     switch (ct) {
4645:       case DM_POLYTOPE_SEG_PRISM_TENSOR:
4646:         DMPlexTransitiveClosure_Hybrid_Internal(dm, point, 9, &np, &pts);
4647:         break;
4648:       case DM_POLYTOPE_TRI_PRISM_TENSOR:
4649:         DMPlexTransitiveClosure_Hybrid_Internal(dm, point, 21, &np, &pts);
4650:         break;
4651:       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
4652:         DMPlexTransitiveClosure_Hybrid_Internal(dm, point, 27, &np, &pts);
4653:         break;
4654:       default:
4655:         DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);
4656:     }
4657:     CompressPoints_Private(section, &np, pts);
4658:   }
4659:   *numPoints = np;
4660:   *points    = pts;
4661:   *clp       = cla;
4662:   return(0);
4663: }

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

4670:   if (!*clPoints) {
4671:     DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);
4672:   } else {
4673:     ISRestoreIndices(*clPoints, clp);
4674:   }
4675:   *numPoints = 0;
4676:   *points    = NULL;
4677:   *clSec     = NULL;
4678:   *clPoints  = NULL;
4679:   *clp       = NULL;
4680:   return(0);
4681: }

4683: 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[])
4684: {
4685:   PetscInt          offset = 0, p;
4686:   const PetscInt    **perms = NULL;
4687:   const PetscScalar **flips = NULL;
4688:   PetscErrorCode    ierr;

4691:   *size = 0;
4692:   PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);
4693:   for (p = 0; p < numPoints; p++) {
4694:     const PetscInt    point = points[2*p];
4695:     const PetscInt    *perm = perms ? perms[p] : NULL;
4696:     const PetscScalar *flip = flips ? flips[p] : NULL;
4697:     PetscInt          dof, off, d;
4698:     const PetscScalar *varr;

4700:     PetscSectionGetDof(section, point, &dof);
4701:     PetscSectionGetOffset(section, point, &off);
4702:     varr = &vArray[off];
4703:     if (clperm) {
4704:       if (perm) {
4705:         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
4706:       } else {
4707:         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
4708:       }
4709:       if (flip) {
4710:         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
4711:       }
4712:     } else {
4713:       if (perm) {
4714:         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
4715:       } else {
4716:         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
4717:       }
4718:       if (flip) {
4719:         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
4720:       }
4721:     }
4722:     offset += dof;
4723:   }
4724:   PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);
4725:   *size = offset;
4726:   return(0);
4727: }

4729: 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[])
4730: {
4731:   PetscInt          offset = 0, f;
4732:   PetscErrorCode    ierr;

4735:   *size = 0;
4736:   for (f = 0; f < numFields; ++f) {
4737:     PetscInt          p;
4738:     const PetscInt    **perms = NULL;
4739:     const PetscScalar **flips = NULL;

4741:     PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);
4742:     for (p = 0; p < numPoints; p++) {
4743:       const PetscInt    point = points[2*p];
4744:       PetscInt          fdof, foff, b;
4745:       const PetscScalar *varr;
4746:       const PetscInt    *perm = perms ? perms[p] : NULL;
4747:       const PetscScalar *flip = flips ? flips[p] : NULL;

4749:       PetscSectionGetFieldDof(section, point, f, &fdof);
4750:       PetscSectionGetFieldOffset(section, point, f, &foff);
4751:       varr = &vArray[foff];
4752:       if (clperm) {
4753:         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
4754:         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
4755:         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
4756:       } else {
4757:         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
4758:         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
4759:         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
4760:       }
4761:       offset += fdof;
4762:     }
4763:     PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);
4764:   }
4765:   *size = offset;
4766:   return(0);
4767: }

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

4772:   Not collective

4774:   Input Parameters:
4775: + dm - The DM
4776: . section - The section describing the layout in v, or NULL to use the default section
4777: . v - The local vector
4778: . point - The point in the DM
4779: . csize - The size of the input values array, or NULL
4780: - values - An array to use for the values, or NULL to have it allocated automatically

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

4786: $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
4787: $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
4788: $ assembly function, and a user may already have allocated storage for this operation.
4789: $
4790: $ A typical use could be
4791: $
4792: $  values = NULL;
4793: $  DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);
4794: $  for (cl = 0; cl < clSize; ++cl) {
4795: $    <Compute on closure>
4796: $  }
4797: $  DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);
4798: $
4799: $ or
4800: $
4801: $  PetscMalloc1(clMaxSize, &values);
4802: $  for (p = pStart; p < pEnd; ++p) {
4803: $    clSize = clMaxSize;
4804: $    DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);
4805: $    for (cl = 0; cl < clSize; ++cl) {
4806: $      <Compute on closure>
4807: $    }
4808: $  }
4809: $  PetscFree(values);

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

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

4817:   Level: intermediate

4819: .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4820: @*/
4821: PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4822: {
4823:   PetscSection       clSection;
4824:   IS                 clPoints;
4825:   PetscInt          *points = NULL;
4826:   const PetscInt    *clp, *perm;
4827:   PetscInt           depth, numFields, numPoints, asize;
4828:   PetscErrorCode     ierr;

4832:   if (!section) {DMGetLocalSection(dm, &section);}
4835:   DMPlexGetDepth(dm, &depth);
4836:   PetscSectionGetNumFields(section, &numFields);
4837:   if (depth == 1 && numFields < 2) {
4838:     DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);
4839:     return(0);
4840:   }
4841:   /* Get points */
4842:   DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
4843:   /* Get sizes */
4844:   asize = 0;
4845:   for (PetscInt p = 0; p < numPoints*2; p += 2) {
4846:     PetscInt dof;
4847:     PetscSectionGetDof(section, points[p], &dof);
4848:     asize += dof;
4849:   }
4850:   if (values) {
4851:     const PetscScalar *vArray;
4852:     PetscInt          size;

4854:     if (*values) {
4855:       if (PetscUnlikely(*csize < asize)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Provided array size %D not sufficient to hold closure size %D", *csize, asize);
4856:     } else {DMGetWorkArray(dm, asize, MPIU_SCALAR, values);}
4857:     PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, asize, &perm);
4858:     VecGetArrayRead(v, &vArray);
4859:     /* Get values */
4860:     if (numFields > 0) {DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, *values);}
4861:     else               {DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, *values);}
4862:     if (PetscUnlikely(asize != size)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Section size %D does not match Vec closure size %D", asize, size);
4863:     /* Cleanup array */
4864:     VecRestoreArrayRead(v, &vArray);
4865:   }
4866:   if (csize) *csize = asize;
4867:   /* Cleanup points */
4868:   DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
4869:   return(0);
4870: }

4872: PetscErrorCode DMPlexVecGetClosureAtDepth_Internal(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt depth, PetscInt *csize, PetscScalar *values[])
4873: {
4874:   DMLabel            depthLabel;
4875:   PetscSection       clSection;
4876:   IS                 clPoints;
4877:   PetscScalar       *array;
4878:   const PetscScalar *vArray;
4879:   PetscInt          *points = NULL;
4880:   const PetscInt    *clp, *perm = NULL;
4881:   PetscInt           mdepth, numFields, numPoints, Np = 0, p, clsize, size;
4882:   PetscErrorCode     ierr;

4886:   if (!section) {DMGetLocalSection(dm, &section);}
4889:   DMPlexGetDepth(dm, &mdepth);
4890:   DMPlexGetDepthLabel(dm, &depthLabel);
4891:   PetscSectionGetNumFields(section, &numFields);
4892:   if (mdepth == 1 && numFields < 2) {
4893:     DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);
4894:     return(0);
4895:   }
4896:   /* Get points */
4897:   DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
4898:   for (clsize=0,p=0; p<Np; p++) {
4899:     PetscInt dof;
4900:     PetscSectionGetDof(section, points[2*p], &dof);
4901:     clsize += dof;
4902:   }
4903:   PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &perm);
4904:   /* Filter points */
4905:   for (p = 0; p < numPoints*2; p += 2) {
4906:     PetscInt dep;

4908:     DMLabelGetValue(depthLabel, points[p], &dep);
4909:     if (dep != depth) continue;
4910:     points[Np*2+0] = points[p];
4911:     points[Np*2+1] = points[p+1];
4912:     ++Np;
4913:   }
4914:   /* Get array */
4915:   if (!values || !*values) {
4916:     PetscInt asize = 0, dof;

4918:     for (p = 0; p < Np*2; p += 2) {
4919:       PetscSectionGetDof(section, points[p], &dof);
4920:       asize += dof;
4921:     }
4922:     if (!values) {
4923:       DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
4924:       if (csize) *csize = asize;
4925:       return(0);
4926:     }
4927:     DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);
4928:   } else {
4929:     array = *values;
4930:   }
4931:   VecGetArrayRead(v, &vArray);
4932:   /* Get values */
4933:   if (numFields > 0) {DMPlexVecGetClosure_Fields_Static(dm, section, Np, points, numFields, perm, vArray, &size, array);}
4934:   else               {DMPlexVecGetClosure_Static(dm, section, Np, points, perm, vArray, &size, array);}
4935:   /* Cleanup points */
4936:   DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
4937:   /* Cleanup array */
4938:   VecRestoreArrayRead(v, &vArray);
4939:   if (!*values) {
4940:     if (csize) *csize = size;
4941:     *values = array;
4942:   } else {
4943:     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
4944:     *csize = size;
4945:   }
4946:   return(0);
4947: }

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

4952:   Not collective

4954:   Input Parameters:
4955: + dm - The DM
4956: . section - The section describing the layout in v, or NULL to use the default section
4957: . v - The local vector
4958: . point - The point in the DM
4959: . csize - The number of values in the closure, or NULL
4960: - values - The array of values, which is a borrowed array and should not be freed

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

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

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

4970:   Level: intermediate

4972: .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4973: @*/
4974: PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4975: {
4976:   PetscInt       size = 0;

4980:   /* Should work without recalculating size */
4981:   DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);
4982:   *values = NULL;
4983:   return(0);
4984: }

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

4989: 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[])
4990: {
4991:   PetscInt        cdof;   /* The number of constraints on this point */
4992:   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4993:   PetscScalar    *a;
4994:   PetscInt        off, cind = 0, k;
4995:   PetscErrorCode  ierr;

4998:   PetscSectionGetConstraintDof(section, point, &cdof);
4999:   PetscSectionGetOffset(section, point, &off);
5000:   a    = &array[off];
5001:   if (!cdof || setBC) {
5002:     if (clperm) {
5003:       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
5004:       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
5005:     } else {
5006:       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
5007:       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
5008:     }
5009:   } else {
5010:     PetscSectionGetConstraintIndices(section, point, &cdofs);
5011:     if (clperm) {
5012:       if (perm) {for (k = 0; k < dof; ++k) {
5013:           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5014:           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
5015:         }
5016:       } else {
5017:         for (k = 0; k < dof; ++k) {
5018:           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5019:           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
5020:         }
5021:       }
5022:     } else {
5023:       if (perm) {
5024:         for (k = 0; k < dof; ++k) {
5025:           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5026:           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
5027:         }
5028:       } else {
5029:         for (k = 0; k < dof; ++k) {
5030:           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5031:           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
5032:         }
5033:       }
5034:     }
5035:   }
5036:   return(0);
5037: }

5039: 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[])
5040: {
5041:   PetscInt        cdof;   /* The number of constraints on this point */
5042:   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5043:   PetscScalar    *a;
5044:   PetscInt        off, cind = 0, k;
5045:   PetscErrorCode  ierr;

5048:   PetscSectionGetConstraintDof(section, point, &cdof);
5049:   PetscSectionGetOffset(section, point, &off);
5050:   a    = &array[off];
5051:   if (cdof) {
5052:     PetscSectionGetConstraintIndices(section, point, &cdofs);
5053:     if (clperm) {
5054:       if (perm) {
5055:         for (k = 0; k < dof; ++k) {
5056:           if ((cind < cdof) && (k == cdofs[cind])) {
5057:             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
5058:             cind++;
5059:           }
5060:         }
5061:       } else {
5062:         for (k = 0; k < dof; ++k) {
5063:           if ((cind < cdof) && (k == cdofs[cind])) {
5064:             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
5065:             cind++;
5066:           }
5067:         }
5068:       }
5069:     } else {
5070:       if (perm) {
5071:         for (k = 0; k < dof; ++k) {
5072:           if ((cind < cdof) && (k == cdofs[cind])) {
5073:             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
5074:             cind++;
5075:           }
5076:         }
5077:       } else {
5078:         for (k = 0; k < dof; ++k) {
5079:           if ((cind < cdof) && (k == cdofs[cind])) {
5080:             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
5081:             cind++;
5082:           }
5083:         }
5084:       }
5085:     }
5086:   }
5087:   return(0);
5088: }

5090: 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[])
5091: {
5092:   PetscScalar    *a;
5093:   PetscInt        fdof, foff, fcdof, foffset = *offset;
5094:   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
5095:   PetscInt        cind = 0, b;
5096:   PetscErrorCode  ierr;

5099:   PetscSectionGetFieldDof(section, point, f, &fdof);
5100:   PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);
5101:   PetscSectionGetFieldOffset(section, point, f, &foff);
5102:   a    = &array[foff];
5103:   if (!fcdof || setBC) {
5104:     if (clperm) {
5105:       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
5106:       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
5107:     } else {
5108:       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
5109:       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
5110:     }
5111:   } else {
5112:     PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
5113:     if (clperm) {
5114:       if (perm) {
5115:         for (b = 0; b < fdof; b++) {
5116:           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
5117:           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
5118:         }
5119:       } else {
5120:         for (b = 0; b < fdof; b++) {
5121:           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
5122:           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
5123:         }
5124:       }
5125:     } else {
5126:       if (perm) {
5127:         for (b = 0; b < fdof; b++) {
5128:           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
5129:           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
5130:         }
5131:       } else {
5132:         for (b = 0; b < fdof; b++) {
5133:           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
5134:           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
5135:         }
5136:       }
5137:     }
5138:   }
5139:   *offset += fdof;
5140:   return(0);
5141: }

5143: 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[])
5144: {
5145:   PetscScalar    *a;
5146:   PetscInt        fdof, foff, fcdof, foffset = *offset;
5147:   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
5148:   PetscInt        Nc, cind = 0, ncind = 0, b;
5149:   PetscBool       ncSet, fcSet;
5150:   PetscErrorCode  ierr;

5153:   PetscSectionGetFieldComponents(section, f, &Nc);
5154:   PetscSectionGetFieldDof(section, point, f, &fdof);
5155:   PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);
5156:   PetscSectionGetFieldOffset(section, point, f, &foff);
5157:   a    = &array[foff];
5158:   if (fcdof) {
5159:     /* We just override fcdof and fcdofs with Ncc and comps */
5160:     PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
5161:     if (clperm) {
5162:       if (perm) {
5163:         if (comps) {
5164:           for (b = 0; b < fdof; b++) {
5165:             ncSet = fcSet = PETSC_FALSE;
5166:             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5167:             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5168:             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
5169:           }
5170:         } else {
5171:           for (b = 0; b < fdof; b++) {
5172:             if ((cind < fcdof) && (b == fcdofs[cind])) {
5173:               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
5174:               ++cind;
5175:             }
5176:           }
5177:         }
5178:       } else {
5179:         if (comps) {
5180:           for (b = 0; b < fdof; b++) {
5181:             ncSet = fcSet = PETSC_FALSE;
5182:             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5183:             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5184:             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
5185:           }
5186:         } else {
5187:           for (b = 0; b < fdof; b++) {
5188:             if ((cind < fcdof) && (b == fcdofs[cind])) {
5189:               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
5190:               ++cind;
5191:             }
5192:           }
5193:         }
5194:       }
5195:     } else {
5196:       if (perm) {
5197:         if (comps) {
5198:           for (b = 0; b < fdof; b++) {
5199:             ncSet = fcSet = PETSC_FALSE;
5200:             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5201:             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5202:             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
5203:           }
5204:         } else {
5205:           for (b = 0; b < fdof; b++) {
5206:             if ((cind < fcdof) && (b == fcdofs[cind])) {
5207:               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
5208:               ++cind;
5209:             }
5210:           }
5211:         }
5212:       } else {
5213:         if (comps) {
5214:           for (b = 0; b < fdof; b++) {
5215:             ncSet = fcSet = PETSC_FALSE;
5216:             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
5217:             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
5218:             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
5219:           }
5220:         } else {
5221:           for (b = 0; b < fdof; b++) {
5222:             if ((cind < fcdof) && (b == fcdofs[cind])) {
5223:               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
5224:               ++cind;
5225:             }
5226:           }
5227:         }
5228:       }
5229:     }
5230:   }
5231:   *offset += fdof;
5232:   return(0);
5233: }

5235: PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
5236: {
5237:   PetscScalar    *array;
5238:   const PetscInt *cone, *coneO;
5239:   PetscInt        pStart, pEnd, p, numPoints, off, dof;
5240:   PetscErrorCode  ierr;

5243:   PetscSectionGetChart(section, &pStart, &pEnd);
5244:   DMPlexGetConeSize(dm, point, &numPoints);
5245:   DMPlexGetCone(dm, point, &cone);
5246:   DMPlexGetConeOrientation(dm, point, &coneO);
5247:   VecGetArray(v, &array);
5248:   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
5249:     const PetscInt cp = !p ? point : cone[p-1];
5250:     const PetscInt o  = !p ? 0     : coneO[p-1];

5252:     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
5253:     PetscSectionGetDof(section, cp, &dof);
5254:     /* ADD_VALUES */
5255:     {
5256:       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5257:       PetscScalar    *a;
5258:       PetscInt        cdof, coff, cind = 0, k;

5260:       PetscSectionGetConstraintDof(section, cp, &cdof);
5261:       PetscSectionGetOffset(section, cp, &coff);
5262:       a    = &array[coff];
5263:       if (!cdof) {
5264:         if (o >= 0) {
5265:           for (k = 0; k < dof; ++k) {
5266:             a[k] += values[off+k];
5267:           }
5268:         } else {
5269:           for (k = 0; k < dof; ++k) {
5270:             a[k] += values[off+dof-k-1];
5271:           }
5272:         }
5273:       } else {
5274:         PetscSectionGetConstraintIndices(section, cp, &cdofs);
5275:         if (o >= 0) {
5276:           for (k = 0; k < dof; ++k) {
5277:             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5278:             a[k] += values[off+k];
5279:           }
5280:         } else {
5281:           for (k = 0; k < dof; ++k) {
5282:             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5283:             a[k] += values[off+dof-k-1];
5284:           }
5285:         }
5286:       }
5287:     }
5288:   }
5289:   VecRestoreArray(v, &array);
5290:   return(0);
5291: }

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

5296:   Not collective

5298:   Input Parameters:
5299: + dm - The DM
5300: . section - The section describing the layout in v, or NULL to use the default section
5301: . v - The local vector
5302: . point - The point in the DM
5303: . values - The array of values
5304: - mode - The insert mode. One of INSERT_ALL_VALUES, ADD_ALL_VALUES, INSERT_VALUES, ADD_VALUES, INSERT_BC_VALUES, and ADD_BC_VALUES,
5305:          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.

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

5310:   Level: intermediate

5312: .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
5313: @*/
5314: PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
5315: {
5316:   PetscSection    clSection;
5317:   IS              clPoints;
5318:   PetscScalar    *array;
5319:   PetscInt       *points = NULL;
5320:   const PetscInt *clp, *clperm = NULL;
5321:   PetscInt        depth, numFields, numPoints, p, clsize;
5322:   PetscErrorCode  ierr;

5326:   if (!section) {DMGetLocalSection(dm, &section);}
5329:   DMPlexGetDepth(dm, &depth);
5330:   PetscSectionGetNumFields(section, &numFields);
5331:   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
5332:     DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);
5333:     return(0);
5334:   }
5335:   /* Get points */
5336:   DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
5337:   for (clsize=0,p=0; p<numPoints; p++) {
5338:     PetscInt dof;
5339:     PetscSectionGetDof(section, points[2*p], &dof);
5340:     clsize += dof;
5341:   }
5342:   PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &clperm);
5343:   /* Get array */
5344:   VecGetArray(v, &array);
5345:   /* Get values */
5346:   if (numFields > 0) {
5347:     PetscInt offset = 0, f;
5348:     for (f = 0; f < numFields; ++f) {
5349:       const PetscInt    **perms = NULL;
5350:       const PetscScalar **flips = NULL;

5352:       PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);
5353:       switch (mode) {
5354:       case INSERT_VALUES:
5355:         for (p = 0; p < numPoints; p++) {
5356:           const PetscInt    point = points[2*p];
5357:           const PetscInt    *perm = perms ? perms[p] : NULL;
5358:           const PetscScalar *flip = flips ? flips[p] : NULL;
5359:           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
5360:         } break;
5361:       case INSERT_ALL_VALUES:
5362:         for (p = 0; p < numPoints; p++) {
5363:           const PetscInt    point = points[2*p];
5364:           const PetscInt    *perm = perms ? perms[p] : NULL;
5365:           const PetscScalar *flip = flips ? flips[p] : NULL;
5366:           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
5367:         } break;
5368:       case INSERT_BC_VALUES:
5369:         for (p = 0; p < numPoints; p++) {
5370:           const PetscInt    point = points[2*p];
5371:           const PetscInt    *perm = perms ? perms[p] : NULL;
5372:           const PetscScalar *flip = flips ? flips[p] : NULL;
5373:           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
5374:         } break;
5375:       case ADD_VALUES:
5376:         for (p = 0; p < numPoints; p++) {
5377:           const PetscInt    point = points[2*p];
5378:           const PetscInt    *perm = perms ? perms[p] : NULL;
5379:           const PetscScalar *flip = flips ? flips[p] : NULL;
5380:           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
5381:         } break;
5382:       case ADD_ALL_VALUES:
5383:         for (p = 0; p < numPoints; p++) {
5384:           const PetscInt    point = points[2*p];
5385:           const PetscInt    *perm = perms ? perms[p] : NULL;
5386:           const PetscScalar *flip = flips ? flips[p] : NULL;
5387:           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
5388:         } break;
5389:       case ADD_BC_VALUES:
5390:         for (p = 0; p < numPoints; p++) {
5391:           const PetscInt    point = points[2*p];
5392:           const PetscInt    *perm = perms ? perms[p] : NULL;
5393:           const PetscScalar *flip = flips ? flips[p] : NULL;
5394:           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
5395:         } break;
5396:       default:
5397:         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5398:       }
5399:       PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);
5400:     }
5401:   } else {
5402:     PetscInt dof, off;
5403:     const PetscInt    **perms = NULL;
5404:     const PetscScalar **flips = NULL;

5406:     PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);
5407:     switch (mode) {
5408:     case INSERT_VALUES:
5409:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
5410:         const PetscInt    point = points[2*p];
5411:         const PetscInt    *perm = perms ? perms[p] : NULL;
5412:         const PetscScalar *flip = flips ? flips[p] : NULL;
5413:         PetscSectionGetDof(section, point, &dof);
5414:         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
5415:       } break;
5416:     case INSERT_ALL_VALUES:
5417:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
5418:         const PetscInt    point = points[2*p];
5419:         const PetscInt    *perm = perms ? perms[p] : NULL;
5420:         const PetscScalar *flip = flips ? flips[p] : NULL;
5421:         PetscSectionGetDof(section, point, &dof);
5422:         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
5423:       } break;
5424:     case INSERT_BC_VALUES:
5425:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
5426:         const PetscInt    point = points[2*p];
5427:         const PetscInt    *perm = perms ? perms[p] : NULL;
5428:         const PetscScalar *flip = flips ? flips[p] : NULL;
5429:         PetscSectionGetDof(section, point, &dof);
5430:         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
5431:       } break;
5432:     case ADD_VALUES:
5433:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
5434:         const PetscInt    point = points[2*p];
5435:         const PetscInt    *perm = perms ? perms[p] : NULL;
5436:         const PetscScalar *flip = flips ? flips[p] : NULL;
5437:         PetscSectionGetDof(section, point, &dof);
5438:         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
5439:       } break;
5440:     case ADD_ALL_VALUES:
5441:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
5442:         const PetscInt    point = points[2*p];
5443:         const PetscInt    *perm = perms ? perms[p] : NULL;
5444:         const PetscScalar *flip = flips ? flips[p] : NULL;
5445:         PetscSectionGetDof(section, point, &dof);
5446:         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
5447:       } break;
5448:     case ADD_BC_VALUES:
5449:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
5450:         const PetscInt    point = points[2*p];
5451:         const PetscInt    *perm = perms ? perms[p] : NULL;
5452:         const PetscScalar *flip = flips ? flips[p] : NULL;
5453:         PetscSectionGetDof(section, point, &dof);
5454:         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
5455:       } break;
5456:     default:
5457:       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5458:     }
5459:     PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);
5460:   }
5461:   /* Cleanup points */
5462:   DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
5463:   /* Cleanup array */
5464:   VecRestoreArray(v, &array);
5465:   return(0);
5466: }

5468: /* Check whether the given point is in the label. If not, update the offset to skip this point */
5469: PETSC_STATIC_INLINE PetscErrorCode CheckPoint_Private(DMLabel label, PetscInt labelId, PetscSection section, PetscInt point, PetscInt f, PetscInt *offset)
5470: {
5472:   if (label) {
5473:     PetscInt       val, fdof;

5476:     /* There is a problem with this:
5477:          Suppose we have two label values, defining surfaces, interecting along a line in 3D. When we add cells to the label, the cells that
5478:        touch both surfaces must pick a label value. Thus we miss setting values for the surface with that other value intersecting that cell.
5479:        Thus I am only going to check val != -1, not val != labelId
5480:     */
5481:     DMLabelGetValue(label, point, &val);
5482:     if (val < 0) {
5483:       PetscSectionGetFieldDof(section, point, f, &fdof);
5484:       *offset += fdof;
5485:       PetscFunctionReturn(1);
5486:     }
5487:   }
5488:   return(0);
5489: }

5491: /* Unlike DMPlexVecSetClosure(), this uses plex-native closure permutation, not a user-specified permutation such as DMPlexSetClosurePermutationTensor(). */
5492: PetscErrorCode DMPlexVecSetFieldClosure_Internal(DM dm, PetscSection section, Vec v, PetscBool fieldActive[], PetscInt point, PetscInt Ncc, const PetscInt comps[], DMLabel label, PetscInt labelId, const PetscScalar values[], InsertMode mode)
5493: {
5494:   PetscSection      clSection;
5495:   IS                clPoints;
5496:   PetscScalar       *array;
5497:   PetscInt          *points = NULL;
5498:   const PetscInt    *clp;
5499:   PetscInt          numFields, numPoints, p;
5500:   PetscInt          offset = 0, f;
5501:   PetscErrorCode    ierr;

5505:   if (!section) {DMGetLocalSection(dm, &section);}
5508:   PetscSectionGetNumFields(section, &numFields);
5509:   /* Get points */
5510:   DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
5511:   /* Get array */
5512:   VecGetArray(v, &array);
5513:   /* Get values */
5514:   for (f = 0; f < numFields; ++f) {
5515:     const PetscInt    **perms = NULL;
5516:     const PetscScalar **flips = NULL;

5518:     if (!fieldActive[f]) {
5519:       for (p = 0; p < numPoints*2; p += 2) {
5520:         PetscInt fdof;
5521:         PetscSectionGetFieldDof(section, points[p], f, &fdof);
5522:         offset += fdof;
5523:       }
5524:       continue;
5525:     }
5526:     PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);
5527:     switch (mode) {
5528:     case INSERT_VALUES:
5529:       for (p = 0; p < numPoints; p++) {
5530:         const PetscInt    point = points[2*p];
5531:         const PetscInt    *perm = perms ? perms[p] : NULL;
5532:         const PetscScalar *flip = flips ? flips[p] : NULL;
5533:         CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
5534:         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, NULL, values, &offset, array);
5535:       } break;
5536:     case INSERT_ALL_VALUES:
5537:       for (p = 0; p < numPoints; p++) {
5538:         const PetscInt    point = points[2*p];
5539:         const PetscInt    *perm = perms ? perms[p] : NULL;
5540:         const PetscScalar *flip = flips ? flips[p] : NULL;
5541:         CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
5542:         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, NULL, values, &offset, array);
5543:       } break;
5544:     case INSERT_BC_VALUES:
5545:       for (p = 0; p < numPoints; p++) {
5546:         const PetscInt    point = points[2*p];
5547:         const PetscInt    *perm = perms ? perms[p] : NULL;
5548:         const PetscScalar *flip = flips ? flips[p] : NULL;
5549:         CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
5550:         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, NULL, values, &offset, array);
5551:       } break;
5552:     case ADD_VALUES:
5553:       for (p = 0; p < numPoints; p++) {
5554:         const PetscInt    point = points[2*p];
5555:         const PetscInt    *perm = perms ? perms[p] : NULL;
5556:         const PetscScalar *flip = flips ? flips[p] : NULL;
5557:         CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
5558:         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, NULL, values, &offset, array);
5559:       } break;
5560:     case ADD_ALL_VALUES:
5561:       for (p = 0; p < numPoints; p++) {
5562:         const PetscInt    point = points[2*p];
5563:         const PetscInt    *perm = perms ? perms[p] : NULL;
5564:         const PetscScalar *flip = flips ? flips[p] : NULL;
5565:         CheckPoint_Private(label, labelId, section, point, f, &offset); if (ierr) continue;
5566:         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, NULL, values, &offset, array);
5567:       } break;
5568:     default:
5569:       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5570:     }
5571:     PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);
5572:   }
5573:   /* Cleanup points */
5574:   DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
5575:   /* Cleanup array */
5576:   VecRestoreArray(v, &array);
5577:   return(0);
5578: }

5580: static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
5581: {
5582:   PetscMPIInt    rank;
5583:   PetscInt       i, j;

5587:   MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);
5588:   PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);
5589:   for (i = 0; i < numRIndices; i++) {PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);}
5590:   for (i = 0; i < numCIndices; i++) {PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);}
5591:   numCIndices = numCIndices ? numCIndices : numRIndices;
5592:   if (!values) return(0);
5593:   for (i = 0; i < numRIndices; i++) {
5594:     PetscViewerASCIIPrintf(viewer, "[%d]", rank);
5595:     for (j = 0; j < numCIndices; j++) {
5596: #if defined(PETSC_USE_COMPLEX)
5597:       PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));
5598: #else
5599:       PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);
5600: #endif
5601:     }
5602:     PetscViewerASCIIPrintf(viewer, "\n");
5603:   }
5604:   return(0);
5605: }

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

5610:   Input Parameters:
5611: + section - The section for this data layout
5612: . islocal - Is the section (and thus indices being requested) local or global?
5613: . point   - The point contributing dofs with these indices
5614: . off     - The global offset of this point
5615: . loff    - The local offset of each field
5616: . setBC   - The flag determining whether to include indices of bounsary values
5617: . perm    - A permutation of the dofs on this point, or NULL
5618: - indperm - A permutation of the entire indices array, or NULL

5620:   Output Parameter:
5621: . indices - Indices for dofs on this point

5623:   Level: developer

5625:   Note: The indices could be local or global, depending on the value of 'off'.
5626: */
5627: PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscBool islocal,PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], const PetscInt indperm[], PetscInt indices[])
5628: {
5629:   PetscInt        dof;   /* The number of unknowns on this point */
5630:   PetscInt        cdof;  /* The number of constraints on this point */
5631:   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5632:   PetscInt        cind = 0, k;
5633:   PetscErrorCode  ierr;

5636:   if (!islocal && setBC) SETERRQ(PetscObjectComm((PetscObject)section),PETSC_ERR_ARG_INCOMP,"setBC incompatible with global indices; use a local section or disable setBC");
5637:   PetscSectionGetDof(section, point, &dof);
5638:   PetscSectionGetConstraintDof(section, point, &cdof);
5639:   if (!cdof || setBC) {
5640:     for (k = 0; k < dof; ++k) {
5641:       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
5642:       const PetscInt ind    = indperm ? indperm[preind] : preind;

5644:       indices[ind] = off + k;
5645:     }
5646:   } else {
5647:     PetscSectionGetConstraintIndices(section, point, &cdofs);
5648:     for (k = 0; k < dof; ++k) {
5649:       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
5650:       const PetscInt ind    = indperm ? indperm[preind] : preind;

5652:       if ((cind < cdof) && (k == cdofs[cind])) {
5653:         /* Insert check for returning constrained indices */
5654:         indices[ind] = -(off+k+1);
5655:         ++cind;
5656:       } else {
5657:         indices[ind] = off + k - (islocal ? 0 : cind);
5658:       }
5659:     }
5660:   }
5661:   *loff += dof;
5662:   return(0);
5663: }

5665: /*
5666:  DMPlexGetIndicesPointFields_Internal - gets section indices for a point in its canonical ordering.

5668:  Input Parameters:
5669: + section - a section (global or local)
5670: - islocal - PETSC_TRUE if requesting local indices (i.e., section is local); PETSC_FALSE for global
5671: . point - point within section
5672: . off - The offset of this point in the (local or global) indexed space - should match islocal and (usually) the section
5673: . foffs - array of length numFields containing the offset in canonical point ordering (the location in indices) of each field
5674: . setBC - identify constrained (boundary condition) points via involution.
5675: . perms - perms[f][permsoff][:] is a permutation of dofs within each field
5676: . permsoff - offset
5677: - indperm - index permutation

5679:  Output Parameter:
5680: . foffs - each entry is incremented by the number of (unconstrained if setBC=FALSE) dofs in that field
5681: . indices - array to hold indices (as defined by section) of each dof associated with point

5683:  Notes:
5684:  If section is local and setBC=true, there is no distinction between constrained and unconstrained dofs.
5685:  If section is local and setBC=false, the indices for constrained points are the involution -(i+1) of their position
5686:  in the local vector.

5688:  If section is global and setBC=false, the indices for constrained points are negative (and their value is not
5689:  significant).  It is invalid to call with a global section and setBC=true.

5691:  Developer Note:
5692:  The section is only used for field layout, so islocal is technically a statement about the offset (off).  At some point
5693:  in the future, global sections may have fields set, in which case we could pass the global section and obtain the
5694:  offset could be obtained from the section instead of passing it explicitly as we do now.

5696:  Example:
5697:  Suppose a point contains one field with three components, and for which the unconstrained indices are {10, 11, 12}.
5698:  When the middle component is constrained, we get the array {10, -12, 12} for (islocal=TRUE, setBC=FALSE).
5699:  Note that -12 is the involution of 11, so the user can involute negative indices to recover local indices.
5700:  The global vector does not store constrained dofs, so when this function returns global indices, say {110, -112, 111}, the value of -112 is an arbitrary flag that should not be interpreted beyond its sign.

5702:  Level: developer
5703: */
5704: PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscBool islocal, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
5705: {
5706:   PetscInt       numFields, foff, f;

5710:   if (!islocal && setBC) SETERRQ(PetscObjectComm((PetscObject)section),PETSC_ERR_ARG_INCOMP,"setBC incompatible with global indices; use a local section or disable setBC");
5711:   PetscSectionGetNumFields(section, &numFields);
5712:   for (f = 0, foff = 0; f < numFields; ++f) {
5713:     PetscInt        fdof, cfdof;
5714:     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
5715:     PetscInt        cind = 0, b;
5716:     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;

5718:     PetscSectionGetFieldDof(section, point, f, &fdof);
5719:     PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);
5720:     if (!cfdof || setBC) {
5721:       for (b = 0; b < fdof; ++b) {
5722:         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
5723:         const PetscInt ind    = indperm ? indperm[preind] : preind;

5725:         indices[ind] = off+foff+b;
5726:       }
5727:     } else {
5728:       PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
5729:       for (b = 0; b < fdof; ++b) {
5730:         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
5731:         const PetscInt ind    = indperm ? indperm[preind] : preind;

5733:         if ((cind < cfdof) && (b == fcdofs[cind])) {
5734:           indices[ind] = -(off+foff+b+1);
5735:           ++cind;
5736:         } else {
5737:           indices[ind] = off + foff + b - (islocal ? 0 : cind);
5738:         }
5739:       }
5740:     }
5741:     foff     += (setBC || islocal ? fdof : (fdof - cfdof));
5742:     foffs[f] += fdof;
5743:   }
5744:   return(0);
5745: }

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

5750:  . foffs - The offset into 'indices' for each field, since it is segregated by field

5752:  Notes:
5753:  The semantics of this function relate to that of setBC=FALSE in DMPlexGetIndicesPointFields_Internal.
5754:  Since this function uses global indices, setBC=TRUE would be invalid, so no such argument exists.
5755: */
5756: static PetscErrorCode DMPlexGetIndicesPointFieldsSplit_Internal(PetscSection section, PetscSection globalSection, PetscInt point, PetscInt foffs[], const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
5757: {
5758:   PetscInt       numFields, foff, f;

5762:   PetscSectionGetNumFields(section, &numFields);
5763:   for (f = 0; f < numFields; ++f) {
5764:     PetscInt        fdof, cfdof;
5765:     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
5766:     PetscInt        cind = 0, b;
5767:     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;

5769:     PetscSectionGetFieldDof(section, point, f, &fdof);
5770:     PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);
5771:     PetscSectionGetFieldOffset(globalSection, point, f, &foff);
5772:     if (!cfdof) {
5773:       for (b = 0; b < fdof; ++b) {
5774:         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
5775:         const PetscInt ind    = indperm ? indperm[preind] : preind;

5777:         indices[ind] = foff+b;
5778:       }
5779:     } else {
5780:       PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
5781:       for (b = 0; b < fdof; ++b) {
5782:         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
5783:         const PetscInt ind    = indperm ? indperm[preind] : preind;

5785:         if ((cind < cfdof) && (b == fcdofs[cind])) {
5786:           indices[ind] = -(foff+b+1);
5787:           ++cind;
5788:         } else {
5789:           indices[ind] = foff+b-cind;
5790:         }
5791:       }
5792:     }
5793:     foffs[f] += fdof;
5794:   }
5795:   return(0);
5796: }

5798: 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)
5799: {
5800:   Mat             cMat;
5801:   PetscSection    aSec, cSec;
5802:   IS              aIS;
5803:   PetscInt        aStart = -1, aEnd = -1;
5804:   const PetscInt  *anchors;
5805:   PetscInt        numFields, f, p, q, newP = 0;
5806:   PetscInt        newNumPoints = 0, newNumIndices = 0;
5807:   PetscInt        *newPoints, *indices, *newIndices;
5808:   PetscInt        maxAnchor, maxDof;
5809:   PetscInt        newOffsets[32];
5810:   PetscInt        *pointMatOffsets[32];
5811:   PetscInt        *newPointOffsets[32];
5812:   PetscScalar     *pointMat[32];
5813:   PetscScalar     *newValues=NULL,*tmpValues;
5814:   PetscBool       anyConstrained = PETSC_FALSE;
5815:   PetscErrorCode  ierr;

5820:   PetscSectionGetNumFields(section, &numFields);

5822:   DMPlexGetAnchors(dm,&aSec,&aIS);
5823:   /* if there are point-to-point constraints */
5824:   if (aSec) {
5825:     PetscArrayzero(newOffsets, 32);
5826:     ISGetIndices(aIS,&anchors);
5827:     PetscSectionGetChart(aSec,&aStart,&aEnd);
5828:     /* figure out how many points are going to be in the new element matrix
5829:      * (we allow double counting, because it's all just going to be summed
5830:      * into the global matrix anyway) */
5831:     for (p = 0; p < 2*numPoints; p+=2) {
5832:       PetscInt b    = points[p];
5833:       PetscInt bDof = 0, bSecDof;

5835:       PetscSectionGetDof(section,b,&bSecDof);
5836:       if (!bSecDof) {
5837:         continue;
5838:       }
5839:       if (b >= aStart && b < aEnd) {
5840:         PetscSectionGetDof(aSec,b,&bDof);
5841:       }
5842:       if (bDof) {
5843:         /* this point is constrained */
5844:         /* it is going to be replaced by its anchors */
5845:         PetscInt bOff, q;

5847:         anyConstrained = PETSC_TRUE;
5848:         newNumPoints  += bDof;
5849:         PetscSectionGetOffset(aSec,b,&bOff);
5850:         for (q = 0; q < bDof; q++) {
5851:           PetscInt a = anchors[bOff + q];
5852:           PetscInt aDof;

5854:           PetscSectionGetDof(section,a,&aDof);
5855:           newNumIndices += aDof;
5856:           for (f = 0; f < numFields; ++f) {
5857:             PetscInt fDof;

5859:             PetscSectionGetFieldDof(section, a, f, &fDof);
5860:             newOffsets[f+1] += fDof;
5861:           }
5862:         }
5863:       }
5864:       else {
5865:         /* this point is not constrained */
5866:         newNumPoints++;
5867:         newNumIndices += bSecDof;
5868:         for (f = 0; f < numFields; ++f) {
5869:           PetscInt fDof;

5871:           PetscSectionGetFieldDof(section, b, f, &fDof);
5872:           newOffsets[f+1] += fDof;
5873:         }
5874:       }
5875:     }
5876:   }
5877:   if (!anyConstrained) {
5878:     if (outNumPoints)  *outNumPoints  = 0;
5879:     if (outNumIndices) *outNumIndices = 0;
5880:     if (outPoints)     *outPoints     = NULL;
5881:     if (outValues)     *outValues     = NULL;
5882:     if (aSec) {ISRestoreIndices(aIS,&anchors);}
5883:     return(0);
5884:   }

5886:   if (outNumPoints)  *outNumPoints  = newNumPoints;
5887:   if (outNumIndices) *outNumIndices = newNumIndices;

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

5891:   if (!outPoints && !outValues) {
5892:     if (offsets) {
5893:       for (f = 0; f <= numFields; f++) {
5894:         offsets[f] = newOffsets[f];
5895:       }
5896:     }
5897:     if (aSec) {ISRestoreIndices(aIS,&anchors);}
5898:     return(0);
5899:   }

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

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

5905:   /* workspaces */
5906:   if (numFields) {
5907:     for (f = 0; f < numFields; f++) {
5908:       DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);
5909:       DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);
5910:     }
5911:   }
5912:   else {
5913:     DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);
5914:     DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);
5915:   }

5917:   /* get workspaces for the point-to-point matrices */
5918:   if (numFields) {
5919:     PetscInt totalOffset, totalMatOffset;

5921:     for (p = 0; p < numPoints; p++) {
5922:       PetscInt b    = points[2*p];
5923:       PetscInt bDof = 0, bSecDof;

5925:       PetscSectionGetDof(section,b,&bSecDof);
5926:       if (!bSecDof) {
5927:         for (f = 0; f < numFields; f++) {
5928:           newPointOffsets[f][p + 1] = 0;
5929:           pointMatOffsets[f][p + 1] = 0;
5930:         }
5931:         continue;
5932:       }
5933:       if (b >= aStart && b < aEnd) {
5934:         PetscSectionGetDof(aSec, b, &bDof);
5935:       }
5936:       if (bDof) {
5937:         for (f = 0; f < numFields; f++) {
5938:           PetscInt fDof, q, bOff, allFDof = 0;

5940:           PetscSectionGetFieldDof(section, b, f, &fDof);
5941:           PetscSectionGetOffset(aSec, b, &bOff);
5942:           for (q = 0; q < bDof; q++) {
5943:             PetscInt a = anchors[bOff + q];
5944:             PetscInt aFDof;

5946:             PetscSectionGetFieldDof(section, a, f, &aFDof);
5947:             allFDof += aFDof;
5948:           }
5949:           newPointOffsets[f][p+1] = allFDof;
5950:           pointMatOffsets[f][p+1] = fDof * allFDof;
5951:         }
5952:       }
5953:       else {
5954:         for (f = 0; f < numFields; f++) {
5955:           PetscInt fDof;

5957:           PetscSectionGetFieldDof(section, b, f, &fDof);
5958:           newPointOffsets[f][p+1] = fDof;
5959:           pointMatOffsets[f][p+1] = 0;
5960:         }
5961:       }
5962:     }
5963:     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
5964:       newPointOffsets[f][0] = totalOffset;
5965:       pointMatOffsets[f][0] = totalMatOffset;
5966:       for (p = 0; p < numPoints; p++) {
5967:         newPointOffsets[f][p+1] += newPointOffsets[f][p];
5968:         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
5969:       }
5970:       totalOffset    = newPointOffsets[f][numPoints];
5971:       totalMatOffset = pointMatOffsets[f][numPoints];
5972:       DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);
5973:     }
5974:   }
5975:   else {
5976:     for (p = 0; p < numPoints; p++) {
5977:       PetscInt b    = points[2*p];
5978:       PetscInt bDof = 0, bSecDof;

5980:       PetscSectionGetDof(section,b,&bSecDof);
5981:       if (!bSecDof) {
5982:         newPointOffsets[0][p + 1] = 0;
5983:         pointMatOffsets[0][p + 1] = 0;
5984:         continue;
5985:       }
5986:       if (b >= aStart && b < aEnd) {
5987:         PetscSectionGetDof(aSec, b, &bDof);
5988:       }
5989:       if (bDof) {
5990:         PetscInt bOff, q, allDof = 0;

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

5996:           PetscSectionGetDof(section, a, &aDof);
5997:           allDof += aDof;
5998:         }
5999:         newPointOffsets[0][p+1] = allDof;
6000:         pointMatOffsets[0][p+1] = bSecDof * allDof;
6001:       }
6002:       else {
6003:         newPointOffsets[0][p+1] = bSecDof;
6004:         pointMatOffsets[0][p+1] = 0;
6005:       }
6006:     }
6007:     newPointOffsets[0][0] = 0;
6008:     pointMatOffsets[0][0] = 0;
6009:     for (p = 0; p < numPoints; p++) {
6010:       newPointOffsets[0][p+1] += newPointOffsets[0][p];
6011:       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
6012:     }
6013:     DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);
6014:   }

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

6019:   /* get the point-to-point matrices; construct newPoints */
6020:   PetscSectionGetMaxDof(aSec, &maxAnchor);
6021:   PetscSectionGetMaxDof(section, &maxDof);
6022:   DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);
6023:   DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);
6024:   if (numFields) {
6025:     for (p = 0, newP = 0; p < numPoints; p++) {
6026:       PetscInt b    = points[2*p];
6027:       PetscInt o    = points[2*p+1];
6028:       PetscInt bDof = 0, bSecDof;

6030:       PetscSectionGetDof(section, b, &bSecDof);
6031:       if (!bSecDof) {
6032:         continue;
6033:       }
6034:       if (b >= aStart && b < aEnd) {
6035:         PetscSectionGetDof(aSec, b, &bDof);
6036:       }
6037:       if (bDof) {
6038:         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;

6040:         fStart[0] = 0;
6041:         fEnd[0]   = 0;
6042:         for (f = 0; f < numFields; f++) {
6043:           PetscInt fDof;

6045:           PetscSectionGetFieldDof(cSec, b, f, &fDof);
6046:           fStart[f+1] = fStart[f] + fDof;
6047:           fEnd[f+1]   = fStart[f+1];
6048:         }
6049:         PetscSectionGetOffset(cSec, b, &bOff);
6050:         DMPlexGetIndicesPointFields_Internal(cSec, PETSC_TRUE, b, bOff, fEnd, PETSC_TRUE, perms, p, NULL, indices);

6052:         fAnchorStart[0] = 0;
6053:         fAnchorEnd[0]   = 0;
6054:         for (f = 0; f < numFields; f++) {
6055:           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];

6057:           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
6058:           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
6059:         }
6060:         PetscSectionGetOffset(aSec, b, &bOff);
6061:         for (q = 0; q < bDof; q++) {
6062:           PetscInt a = anchors[bOff + q], aOff;

6064:           /* we take the orientation of ap into account in the order that we constructed the indices above: the newly added points have no orientation */
6065:           newPoints[2*(newP + q)]     = a;
6066:           newPoints[2*(newP + q) + 1] = 0;
6067:           PetscSectionGetOffset(section, a, &aOff);
6068:           DMPlexGetIndicesPointFields_Internal(section, PETSC_TRUE, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, NULL, newIndices);
6069:         }
6070:         newP += bDof;

6072:         if (outValues) {
6073:           /* get the point-to-point submatrix */
6074:           for (f = 0; f < numFields; f++) {
6075:             MatGetValues(cMat,fEnd[f]-fStart[f],indices + fStart[f],fAnchorEnd[f] - fAnchorStart[f],newIndices + fAnchorStart[f],pointMat[f] + pointMatOffsets[f][p]);
6076:           }
6077:         }
6078:       }
6079:       else {
6080:         newPoints[2 * newP]     = b;
6081:         newPoints[2 * newP + 1] = o;
6082:         newP++;
6083:       }
6084:     }
6085:   } else {
6086:     for (p = 0; p < numPoints; p++) {
6087:       PetscInt b    = points[2*p];
6088:       PetscInt o    = points[2*p+1];
6089:       PetscInt bDof = 0, bSecDof;

6091:       PetscSectionGetDof(section, b, &bSecDof);
6092:       if (!bSecDof) {
6093:         continue;
6094:       }
6095:       if (b >= aStart && b < aEnd) {
6096:         PetscSectionGetDof(aSec, b, &bDof);
6097:       }
6098:       if (bDof) {
6099:         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;

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

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

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

6110:           newPoints[2*(newP + q)]     = a;
6111:           newPoints[2*(newP + q) + 1] = 0;
6112:           PetscSectionGetOffset(section, a, &aOff);
6113:           DMPlexGetIndicesPoint_Internal(section, PETSC_TRUE, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, NULL, newIndices);
6114:         }
6115:         newP += bDof;

6117:         /* get the point-to-point submatrix */
6118:         if (outValues) {
6119:           MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);
6120:         }
6121:       }
6122:       else {
6123:         newPoints[2 * newP]     = b;
6124:         newPoints[2 * newP + 1] = o;
6125:         newP++;
6126:       }
6127:     }
6128:   }

6130:   if (outValues) {
6131:     DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);
6132:     PetscArrayzero(tmpValues,newNumIndices*numIndices);
6133:     /* multiply constraints on the right */
6134:     if (numFields) {
6135:       for (f = 0; f < numFields; f++) {
6136:         PetscInt oldOff = offsets[f];

6138:         for (p = 0; p < numPoints; p++) {
6139:           PetscInt cStart = newPointOffsets[f][p];
6140:           PetscInt b      = points[2 * p];
6141:           PetscInt c, r, k;
6142:           PetscInt dof;

6144:           PetscSectionGetFieldDof(section,b,f,&dof);
6145:           if (!dof) {
6146:             continue;
6147:           }
6148:           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
6149:             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
6150:             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];

6152:             for (r = 0; r < numIndices; r++) {
6153:               for (c = 0; c < nCols; c++) {
6154:                 for (k = 0; k < dof; k++) {
6155:                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
6156:                 }
6157:               }
6158:             }
6159:           }
6160:           else {
6161:             /* copy this column as is */
6162:             for (r = 0; r < numIndices; r++) {
6163:               for (c = 0; c < dof; c++) {
6164:                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
6165:               }
6166:             }
6167:           }
6168:           oldOff += dof;
6169:         }
6170:       }
6171:     }
6172:     else {
6173:       PetscInt oldOff = 0;
6174:       for (p = 0; p < numPoints; p++) {
6175:         PetscInt cStart = newPointOffsets[0][p];
6176:         PetscInt b      = points[2 * p];
6177:         PetscInt c, r, k;
6178:         PetscInt dof;

6180:         PetscSectionGetDof(section,b,&dof);
6181:         if (!dof) {
6182:           continue;
6183:         }
6184:         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
6185:           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
6186:           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];

6188:           for (r = 0; r < numIndices; r++) {
6189:             for (c = 0; c < nCols; c++) {
6190:               for (k = 0; k < dof; k++) {
6191:                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
6192:               }
6193:             }
6194:           }
6195:         }
6196:         else {
6197:           /* copy this column as is */
6198:           for (r = 0; r < numIndices; r++) {
6199:             for (c = 0; c < dof; c++) {
6200:               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
6201:             }
6202:           }
6203:         }
6204:         oldOff += dof;
6205:       }
6206:     }

6208:     if (multiplyLeft) {
6209:       DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);
6210:       PetscArrayzero(newValues,newNumIndices*newNumIndices);
6211:       /* multiply constraints transpose on the left */
6212:       if (numFields) {
6213:         for (f = 0; f < numFields; f++) {
6214:           PetscInt oldOff = offsets[f];

6216:           for (p = 0; p < numPoints; p++) {
6217:             PetscInt rStart = newPointOffsets[f][p];
6218:             PetscInt b      = points[2 * p];
6219:             PetscInt c, r, k;
6220:             PetscInt dof;

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

6227:               for (r = 0; r < nRows; r++) {
6228:                 for (c = 0; c < newNumIndices; c++) {
6229:                   for (k = 0; k < dof; k++) {
6230:                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
6231:                   }
6232:                 }
6233:               }
6234:             }
6235:             else {
6236:               /* copy this row as is */
6237:               for (r = 0; r < dof; r++) {
6238:                 for (c = 0; c < newNumIndices; c++) {
6239:                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
6240:                 }
6241:               }
6242:             }
6243:             oldOff += dof;
6244:           }
6245:         }
6246:       }
6247:       else {
6248:         PetscInt oldOff = 0;

6250:         for (p = 0; p < numPoints; p++) {
6251:           PetscInt rStart = newPointOffsets[0][p];
6252:           PetscInt b      = points[2 * p];
6253:           PetscInt c, r, k;
6254:           PetscInt dof;

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

6261:             for (r = 0; r < nRows; r++) {
6262:               for (c = 0; c < newNumIndices; c++) {
6263:                 for (k = 0; k < dof; k++) {
6264:                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
6265:                 }
6266:               }
6267:             }
6268:           }
6269:           else {
6270:             /* copy this row as is */
6271:             for (r = 0; r < dof; r++) {
6272:               for (c = 0; c < newNumIndices; c++) {
6273:                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
6274:               }
6275:             }
6276:           }
6277:           oldOff += dof;
6278:         }
6279:       }

6281:       DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);
6282:     }
6283:     else {
6284:       newValues = tmpValues;
6285:     }
6286:   }

6288:   /* clean up */
6289:   DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);
6290:   DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);

6292:   if (numFields) {
6293:     for (f = 0; f < numFields; f++) {
6294:       DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);
6295:       DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);
6296:       DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);
6297:     }
6298:   }
6299:   else {
6300:     DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);
6301:     DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);
6302:     DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);
6303:   }
6304:   ISRestoreIndices(aIS,&anchors);

6306:   /* output */
6307:   if (outPoints) {
6308:     *outPoints = newPoints;
6309:   }
6310:   else {
6311:     DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);
6312:   }
6313:   if (outValues) {
6314:     *outValues = newValues;
6315:   }
6316:   for (f = 0; f <= numFields; f++) {
6317:     offsets[f] = newOffsets[f];
6318:   }
6319:   return(0);
6320: }

6322: /*@C
6323:   DMPlexGetClosureIndices - Gets the global dof indices associated with the closure of the given point within the provided sections.

6325:   Not collective

6327:   Input Parameters:
6328: + dm         - The DM
6329: . section    - The PetscSection describing the points (a local section)
6330: . idxSection - The PetscSection from which to obtain indices (may be local or global)
6331: . point      - The point defining the closure
6332: - useClPerm  - Use the closure point permutation if available

6334:   Output Parameters:
6335: + numIndices - The number of dof indices in the closure of point with the input sections
6336: . indices    - The dof indices
6337: . outOffsets - Array to write the field offsets into, or NULL
6338: - values     - The input values, which may be modified if sign flips are induced by the point symmetries, or NULL

6340:   Notes:
6341:   Must call DMPlexRestoreClosureIndices() to free allocated memory

6343:   If idxSection is global, any constrained dofs (see DMAddBoundary(), for example) will get negative indices.  The value
6344:   of those indices is not significant.  If idxSection is local, the constrained dofs will yield the involution -(idx+1)
6345:   of their index in a local vector.  A caller who does not wish to distinguish those points may recover the nonnegative
6346:   indices via involution, -(-(idx+1)+1)==idx.  Local indices are provided when idxSection == section, otherwise global
6347:   indices (with the above semantics) are implied.

6349:   Level: advanced

6351: .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure(), DMGetLocalSection(), DMGetGlobalSection()
6352: @*/
6353: PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm,
6354:                                        PetscInt *numIndices, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[])
6355: {
6356:   /* Closure ordering */
6357:   PetscSection        clSection;
6358:   IS                  clPoints;
6359:   const PetscInt     *clp;
6360:   PetscInt           *points;
6361:   const PetscInt     *clperm = NULL;
6362:   /* Dof permutation and sign flips */
6363:   const PetscInt    **perms[32] = {NULL};
6364:   const PetscScalar **flips[32] = {NULL};
6365:   PetscScalar        *valCopy   = NULL;
6366:   /* Hanging node constraints */
6367:   PetscInt           *pointsC = NULL;
6368:   PetscScalar        *valuesC = NULL;
6369:   PetscInt            NclC, NiC;

6371:   PetscInt           *idx;
6372:   PetscInt            Nf, Ncl, Ni = 0, offsets[32], p, f;
6373:   PetscBool           isLocal = (section == idxSection) ? PETSC_TRUE : PETSC_FALSE;
6374:   PetscErrorCode      ierr;

6384:   PetscSectionGetNumFields(section, &Nf);
6385:   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
6386:   PetscArrayzero(offsets, 32);
6387:   /* 1) Get points in closure */
6388:   DMPlexGetCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);
6389:   if (useClPerm) {
6390:     PetscInt depth, clsize;
6391:     DMPlexGetPointDepth(dm, point, &depth);
6392:     for (clsize=0,p=0; p<Ncl; p++) {
6393:       PetscInt dof;
6394:       PetscSectionGetDof(section, points[2*p], &dof);
6395:       clsize += dof;
6396:     }
6397:     PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &clperm);
6398:   }
6399:   /* 2) Get number of indices on these points and field offsets from section */
6400:   for (p = 0; p < Ncl*2; p += 2) {
6401:     PetscInt dof, fdof;

6403:     PetscSectionGetDof(section, points[p], &dof);
6404:     for (f = 0; f < Nf; ++f) {
6405:       PetscSectionGetFieldDof(section, points[p], f, &fdof);
6406:       offsets[f+1] += fdof;
6407:     }
6408:     Ni += dof;
6409:   }
6410:   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
6411:   if (Nf && offsets[Nf] != Ni) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Ni);
6412:   /* 3) Get symmetries and sign flips. Apply sign flips to values if passed in (only works for square values matrix) */
6413:   for (f = 0; f < PetscMax(1, Nf); ++f) {
6414:     if (Nf) {PetscSectionGetFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);}
6415:     else    {PetscSectionGetPointSyms(section, Ncl, points, &perms[f], &flips[f]);}
6416:     /* may need to apply sign changes to the element matrix */
6417:     if (values && flips[f]) {
6418:       PetscInt foffset = offsets[f];

6420:       for (p = 0; p < Ncl; ++p) {
6421:         PetscInt           pnt  = points[2*p], fdof;
6422:         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;

6424:         if (!Nf) {PetscSectionGetDof(section, pnt, &fdof);}
6425:         else     {PetscSectionGetFieldDof(section, pnt, f, &fdof);}
6426:         if (flip) {
6427:           PetscInt i, j, k;

6429:           if (!valCopy) {
6430:             DMGetWorkArray(dm, Ni*Ni, MPIU_SCALAR, &valCopy);
6431:             for (j = 0; j < Ni * Ni; ++j) valCopy[j] = (*values)[j];
6432:             *values = valCopy;
6433:           }
6434:           for (i = 0; i < fdof; ++i) {
6435:             PetscScalar fval = flip[i];

6437:             for (k = 0; k < Ni; ++k) {
6438:               valCopy[Ni * (foffset + i) + k] *= fval;
6439:               valCopy[Ni * k + (foffset + i)] *= fval;
6440:             }
6441:           }
6442:         }
6443:         foffset += fdof;
6444:       }
6445:     }
6446:   }
6447:   /* 4) Apply hanging node constraints. Get new symmetries and replace all storage with constrained storage */
6448:   DMPlexAnchorsModifyMat(dm, section, Ncl, Ni, points, perms, values ? *values : NULL, &NclC, &NiC, &pointsC, values ? &valuesC : NULL, offsets, PETSC_TRUE);
6449:   if (NclC) {
6450:     if (valCopy) {DMRestoreWorkArray(dm, Ni*Ni, MPIU_SCALAR, &valCopy);}
6451:     for (f = 0; f < PetscMax(1, Nf); ++f) {
6452:       if (Nf) {PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);}
6453:       else    {PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]);}
6454:     }
6455:     for (f = 0; f < PetscMax(1, Nf); ++f) {
6456:       if (Nf) {PetscSectionGetFieldPointSyms(section, f, NclC, pointsC, &perms[f], &flips[f]);}
6457:       else    {PetscSectionGetPointSyms(section, NclC, pointsC, &perms[f], &flips[f]);}
6458:     }
6459:     DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);
6460:     Ncl     = NclC;
6461:     Ni      = NiC;
6462:     points  = pointsC;
6463:     if (values) *values = valuesC;
6464:   }
6465:   /* 5) Calculate indices */
6466:   DMGetWorkArray(dm, Ni, MPIU_INT, &idx);
6467:   if (Nf) {
6468:     PetscInt  idxOff;
6469:     PetscBool useFieldOffsets;

6471:     if (outOffsets) {for (f = 0; f <= Nf; f++) outOffsets[f] = offsets[f];}
6472:     PetscSectionGetUseFieldOffsets(idxSection, &useFieldOffsets);
6473:     if (useFieldOffsets) {
6474:       for (p = 0; p < Ncl; ++p) {
6475:         const PetscInt pnt = points[p*2];

6477:         DMPlexGetIndicesPointFieldsSplit_Internal(section, idxSection, pnt, offsets, perms, p, clperm, idx);
6478:       }
6479:     } else {
6480:       for (p = 0; p < Ncl; ++p) {
6481:         const PetscInt pnt = points[p*2];

6483:         PetscSectionGetOffset(idxSection, pnt, &idxOff);
6484:         /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
6485:          * not (at the time of this writing) have fields set. They probably should, in which case we would pass the
6486:          * global section. */
6487:         DMPlexGetIndicesPointFields_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff+1) : idxOff, offsets, PETSC_FALSE, perms, p, clperm, idx);
6488:       }
6489:     }
6490:   } else {
6491:     PetscInt off = 0, idxOff;

6493:     for (p = 0; p < Ncl; ++p) {
6494:       const PetscInt  pnt  = points[p*2];
6495:       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;

6497:       PetscSectionGetOffset(idxSection, pnt, &idxOff);
6498:       /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
6499:        * not (at the time of this writing) have fields set. They probably should, in which case we would pass the global section. */
6500:       DMPlexGetIndicesPoint_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff+1) : idxOff, &off, PETSC_FALSE, perm, clperm, idx);
6501:     }
6502:   }
6503:   /* 6) Cleanup */
6504:   for (f = 0; f < PetscMax(1, Nf); ++f) {
6505:     if (Nf) {PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);}
6506:     else    {PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]);}
6507:   }
6508:   if (NclC) {
6509:     DMRestoreWorkArray(dm, NclC*2, MPIU_INT, &pointsC);
6510:   } else {
6511:     DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);
6512:   }

6514:   if (numIndices) *numIndices = Ni;
6515:   if (indices)    *indices    = idx;
6516:   return(0);
6517: }

6519: /*@C
6520:   DMPlexRestoreClosureIndices - Restores the global dof indices associated with the closure of the given point within the provided sections.

6522:   Not collective

6524:   Input Parameters:
6525: + dm         - The DM
6526: . section    - The PetscSection describing the points (a local section)
6527: . idxSection - The PetscSection from which to obtain indices (may be local or global)
6528: . point      - The point defining the closure
6529: - useClPerm  - Use the closure point permutation if available

6531:   Output Parameters:
6532: + numIndices - The number of dof indices in the closure of point with the input sections
6533: . indices    - The dof indices
6534: . outOffsets - Array to write the field offsets into, or NULL
6535: - values     - The input values, which may be modified if sign flips are induced by the point symmetries, or NULL

6537:   Notes:
6538:   If values were modified, the user is responsible for calling DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values).

6540:   If idxSection is global, any constrained dofs (see DMAddBoundary(), for example) will get negative indices.  The value
6541:   of those indices is not significant.  If idxSection is local, the constrained dofs will yield the involution -(idx+1)
6542:   of their index in a local vector.  A caller who does not wish to distinguish those points may recover the nonnegative
6543:   indices via involution, -(-(idx+1)+1)==idx.  Local indices are provided when idxSection == section, otherwise global
6544:   indices (with the above semantics) are implied.

6546:   Level: advanced

6548: .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure(), DMGetLocalSection(), DMGetGlobalSection()
6549: @*/
6550: PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm,
6551:                                            PetscInt *numIndices, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[])
6552: {

6558:   DMRestoreWorkArray(dm, 0, MPIU_INT, indices);
6559:   return(0);
6560: }

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

6565:   Not collective

6567:   Input Parameters:
6568: + dm - The DM
6569: . section - The section describing the layout in v, or NULL to use the default section
6570: . globalSection - The section describing the layout in v, or NULL to use the default global section
6571: . A - The matrix
6572: . point - The point in the DM
6573: . values - The array of values
6574: - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions

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

6579:   Level: intermediate

6581: .seealso DMPlexMatSetClosureGeneral(), DMPlexVecGetClosure(), DMPlexVecSetClosure()
6582: @*/
6583: PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
6584: {
6585:   DM_Plex           *mesh = (DM_Plex*) dm->data;
6586:   PetscInt          *indices;
6587:   PetscInt           numIndices;
6588:   const PetscScalar *valuesOrig = values;
6589:   PetscErrorCode     ierr;

6593:   if (!section) {DMGetLocalSection(dm, &section);}
6595:   if (!globalSection) {DMGetGlobalSection(dm, &globalSection);}

6599:   DMPlexGetClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);

6601:   if (mesh->printSetValues) {DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);}
6602:   MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
6603:   if (ierr) {
6604:     PetscMPIInt    rank;
6605:     PetscErrorCode ierr2;

6607:     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr2);
6608:     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6609:     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
6610:     ierr2 = DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
6611:     if (values != valuesOrig) {ierr2 = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values);CHKERRQ(ierr2);}
6612:     
6613:   }
6614:   if (mesh->printFEM > 1) {
6615:     PetscInt i;
6616:     PetscPrintf(PETSC_COMM_SELF, "  Indices:");
6617:     for (i = 0; i < numIndices; ++i) {PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);}
6618:     PetscPrintf(PETSC_COMM_SELF, "\n");
6619:   }

6621:   DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);
6622:   if (values != valuesOrig) {DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values);}
6623:   return(0);
6624: }

6626: /*@C
6627:   DMPlexMatSetClosure - Set an array of the values on the closure of 'point' using a different row and column section

6629:   Not collective

6631:   Input Parameters:
6632: + dmRow - The DM for the row fields
6633: . sectionRow - The section describing the layout, or NULL to use the default section in dmRow
6634: . globalSectionRow - The section describing the layout, or NULL to use the default global section in dmRow
6635: . dmCol - The DM for the column fields
6636: . sectionCol - The section describing the layout, or NULL to use the default section in dmCol
6637: . globalSectionCol - The section describing the layout, or NULL to use the default global section in dmCol
6638: . A - The matrix
6639: . point - The point in the DMs
6640: . values - The array of values
6641: - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions

6643:   Level: intermediate

6645: .seealso DMPlexMatSetClosure(), DMPlexVecGetClosure(), DMPlexVecSetClosure()
6646: @*/
6647: PetscErrorCode DMPlexMatSetClosureGeneral(DM dmRow, PetscSection sectionRow, PetscSection globalSectionRow, DM dmCol, PetscSection sectionCol, PetscSection globalSectionCol, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
6648: {
6649:   DM_Plex           *mesh = (DM_Plex*) dmRow->data;
6650:   PetscInt          *indicesRow, *indicesCol;
6651:   PetscInt           numIndicesRow, numIndicesCol;
6652:   const PetscScalar *valuesOrig = values;
6653:   PetscErrorCode     ierr;

6657:   if (!sectionRow) {DMGetLocalSection(dmRow, &sectionRow);}
6659:   if (!globalSectionRow) {DMGetGlobalSection(dmRow, &globalSectionRow);}
6662:   if (!sectionCol) {DMGetLocalSection(dmCol, &sectionCol);}
6664:   if (!globalSectionCol) {DMGetGlobalSection(dmCol, &globalSectionCol);}

6668:   DMPlexGetClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);
6669:   DMPlexGetClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesCol, NULL, (PetscScalar **) &values);

6671:   if (mesh->printSetValues) {DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values);}
6672:   MatSetValues(A, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values, mode);
6673:   if (ierr) {
6674:     PetscMPIInt    rank;
6675:     PetscErrorCode ierr2;

6677:     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr2);
6678:     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6679:     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values);CHKERRQ(ierr2);
6680:     ierr2 = DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
6681:     ierr2 = DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesRow, NULL, (PetscScalar **) &values);CHKERRQ(ierr2);
6682:     if (values != valuesOrig) {ierr2 = DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &values);CHKERRQ(ierr2);}
6683:     
6684:   }

6686:   DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);
6687:   DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesCol, NULL, (PetscScalar **) &values);
6688:   if (values != valuesOrig) {DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &values);}
6689:   return(0);
6690: }

6692: PetscErrorCode DMPlexMatSetClosureRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
6693: {
6694:   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
6695:   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
6696:   PetscInt       *cpoints = NULL;
6697:   PetscInt       *findices, *cindices;
6698:   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
6699:   PetscInt        foffsets[32], coffsets[32];
6700:   DMPolytopeType  ct;
6701:   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
6702:   PetscErrorCode  ierr;

6707:   if (!fsection) {DMGetLocalSection(dmf, &fsection);}
6709:   if (!csection) {DMGetLocalSection(dmc, &csection);}
6711:   if (!globalFSection) {DMGetGlobalSection(dmf, &globalFSection);}
6713:   if (!globalCSection) {DMGetGlobalSection(dmc, &globalCSection);}
6716:   PetscSectionGetNumFields(fsection, &numFields);
6717:   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
6718:   PetscArrayzero(foffsets, 32);
6719:   PetscArrayzero(coffsets, 32);
6720:   /* Column indices */
6721:   DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
6722:   maxFPoints = numCPoints;
6723:   /* Compress out points not in the section */
6724:   /*   TODO: Squeeze out points with 0 dof as well */
6725:   PetscSectionGetChart(csection, &pStart, &pEnd);
6726:   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
6727:     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
6728:       cpoints[q*2]   = cpoints[p];
6729:       cpoints[q*2+1] = cpoints[p+1];
6730:       ++q;
6731:     }
6732:   }
6733:   numCPoints = q;
6734:   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
6735:     PetscInt fdof;

6737:     PetscSectionGetDof(csection, cpoints[p], &dof);
6738:     if (!dof) continue;
6739:     for (f = 0; f < numFields; ++f) {
6740:       PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);
6741:       coffsets[f+1] += fdof;
6742:     }
6743:     numCIndices += dof;
6744:   }
6745:   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
6746:   /* Row indices */
6747:   DMPlexGetCellType(dmc, point, &ct);
6748:   {
6749:     DMPlexCellRefiner cr;
6750:     DMPlexCellRefinerCreate(dmc, &cr);
6751:     DMPlexCellRefinerGetAffineTransforms(cr, ct, &numSubcells, NULL, NULL, NULL);
6752:     DMPlexCellRefinerDestroy(&cr);
6753:   }
6754:   DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);
6755:   for (r = 0, q = 0; r < numSubcells; ++r) {
6756:     /* TODO Map from coarse to fine cells */
6757:     DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);
6758:     /* Compress out points not in the section */
6759:     PetscSectionGetChart(fsection, &pStart, &pEnd);
6760:     for (p = 0; p < numFPoints*2; p += 2) {
6761:       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
6762:         PetscSectionGetDof(fsection, fpoints[p], &dof);
6763:         if (!dof) continue;
6764:         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
6765:         if (s < q) continue;
6766:         ftotpoints[q*2]   = fpoints[p];
6767:         ftotpoints[q*2+1] = fpoints[p+1];
6768:         ++q;
6769:       }
6770:     }
6771:     DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);
6772:   }
6773:   numFPoints = q;
6774:   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
6775:     PetscInt fdof;

6777:     PetscSectionGetDof(fsection, ftotpoints[p], &dof);
6778:     if (!dof) continue;
6779:     for (f = 0; f < numFields; ++f) {
6780:       PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);
6781:       foffsets[f+1] += fdof;
6782:     }
6783:     numFIndices += dof;
6784:   }
6785:   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];

6787:   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
6788:   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
6789:   DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);
6790:   DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);
6791:   if (numFields) {
6792:     const PetscInt **permsF[32] = {NULL};
6793:     const PetscInt **permsC[32] = {NULL};

6795:     for (f = 0; f < numFields; f++) {
6796:       PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);
6797:       PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);
6798:     }
6799:     for (p = 0; p < numFPoints; p++) {
6800:       PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);
6801:       DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);
6802:     }
6803:     for (p = 0; p < numCPoints; p++) {
6804:       PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);
6805:       DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);
6806:     }
6807:     for (f = 0; f < numFields; f++) {
6808:       PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);
6809:       PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);
6810:     }
6811:   } else {
6812:     const PetscInt **permsF = NULL;
6813:     const PetscInt **permsC = NULL;

6815:     PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);
6816:     PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);
6817:     for (p = 0, off = 0; p < numFPoints; p++) {
6818:       const PetscInt *perm = permsF ? permsF[p] : NULL;

6820:       PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);
6821:       DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);
6822:     }
6823:     for (p = 0, off = 0; p < numCPoints; p++) {
6824:       const PetscInt *perm = permsC ? permsC[p] : NULL;

6826:       PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);
6827:       DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);
6828:     }
6829:     PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);
6830:     PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);
6831:   }
6832:   if (mesh->printSetValues) {DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);}
6833:   /* TODO: flips */
6834:   MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
6835:   if (ierr) {
6836:     PetscMPIInt    rank;
6837:     PetscErrorCode ierr2;

6839:     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRMPI(ierr2);
6840:     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6841:     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
6842:     ierr2 = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr2);
6843:     ierr2 = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr2);
6844:     
6845:   }
6846:   DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);
6847:   DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
6848:   DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);
6849:   DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);
6850:   return(0);
6851: }

6853: PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
6854: {
6855:   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
6856:   PetscInt      *cpoints = NULL;
6857:   PetscInt       foffsets[32], coffsets[32];
6858:   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
6859:   DMPolytopeType ct;
6860:   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;

6866:   if (!fsection) {DMGetLocalSection(dmf, &fsection);}
6868:   if (!csection) {DMGetLocalSection(dmc, &csection);}
6870:   if (!globalFSection) {DMGetGlobalSection(dmf, &globalFSection);}
6872:   if (!globalCSection) {DMGetGlobalSection(dmc, &globalCSection);}
6874:   PetscSectionGetNumFields(fsection, &numFields);
6875:   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
6876:   PetscArrayzero(foffsets, 32);
6877:   PetscArrayzero(coffsets, 32);
6878:   /* Column indices */
6879:   DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
6880:   maxFPoints = numCPoints;
6881:   /* Compress out points not in the section */
6882:   /*   TODO: Squeeze out points with 0 dof as well */
6883:   PetscSectionGetChart(csection, &pStart, &pEnd);
6884:   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
6885:     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
6886:       cpoints[q*2]   = cpoints[p];
6887:       cpoints[q*2+1] = cpoints[p+1];
6888:       ++q;
6889:     }
6890:   }
6891:   numCPoints = q;
6892:   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
6893:     PetscInt fdof;

6895:     PetscSectionGetDof(csection, cpoints[p], &dof);
6896:     if (!dof) continue;
6897:     for (f = 0; f < numFields; ++f) {
6898:       PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);
6899:       coffsets[f+1] += fdof;
6900:     }
6901:     numCIndices += dof;
6902:   }
6903:   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
6904:   /* Row indices */
6905:   DMPlexGetCellType(dmc, point, &ct);
6906:   {
6907:     DMPlexCellRefiner cr;
6908:     DMPlexCellRefinerCreate(dmc, &cr);
6909:     DMPlexCellRefinerGetAffineTransforms(cr, ct, &numSubcells, NULL, NULL, NULL);
6910:     DMPlexCellRefinerDestroy(&cr);
6911:   }
6912:   DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);
6913:   for (r = 0, q = 0; r < numSubcells; ++r) {
6914:     /* TODO Map from coarse to fine cells */
6915:     DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);
6916:     /* Compress out points not in the section */
6917:     PetscSectionGetChart(fsection, &pStart, &pEnd);
6918:     for (p = 0; p < numFPoints*2; p += 2) {
6919:       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
6920:         PetscSectionGetDof(fsection, fpoints[p], &dof);
6921:         if (!dof) continue;
6922:         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
6923:         if (s < q) continue;
6924:         ftotpoints[q*2]   = fpoints[p];
6925:         ftotpoints[q*2+1] = fpoints[p+1];
6926:         ++q;
6927:       }
6928:     }
6929:     DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);
6930:   }
6931:   numFPoints = q;
6932:   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
6933:     PetscInt fdof;

6935:     PetscSectionGetDof(fsection, ftotpoints[p], &dof);
6936:     if (!dof) continue;
6937:     for (f = 0; f < numFields; ++f) {
6938:       PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);
6939:       foffsets[f+1] += fdof;
6940:     }
6941:     numFIndices += dof;
6942:   }
6943:   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];

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

6951:     for (f = 0; f < numFields; f++) {
6952:       PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);
6953:       PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);
6954:     }
6955:     for (p = 0; p < numFPoints; p++) {
6956:       PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);
6957:       DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);
6958:     }
6959:     for (p = 0; p < numCPoints; p++) {
6960:       PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);
6961:       DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);
6962:     }
6963:     for (f = 0; f < numFields; f++) {
6964:       PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);
6965:       PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);
6966:     }
6967:   } else {
6968:     const PetscInt **permsF = NULL;
6969:     const PetscInt **permsC = NULL;

6971:     PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);
6972:     PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);
6973:     for (p = 0, off = 0; p < numFPoints; p++) {
6974:       const PetscInt *perm = permsF ? permsF[p] : NULL;

6976:       PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);
6977:       DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);
6978:     }
6979:     for (p = 0, off = 0; p < numCPoints; p++) {
6980:       const PetscInt *perm = permsC ? permsC[p] : NULL;

6982:       PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);
6983:       DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);
6984:     }
6985:     PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);
6986:     PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);
6987:   }
6988:   DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);
6989:   DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
6990:   return(0);
6991: }

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

6996:   Input Parameter:
6997: . dm   - The DMPlex object

6999:   Output Parameter:
7000: . cellHeight - The height of a cell

7002:   Level: developer

7004: .seealso DMPlexSetVTKCellHeight()
7005: @*/
7006: PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
7007: {
7008:   DM_Plex *mesh = (DM_Plex*) dm->data;

7013:   *cellHeight = mesh->vtkCellHeight;
7014:   return(0);
7015: }

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

7020:   Input Parameters:
7021: + dm   - The DMPlex object
7022: - cellHeight - The height of a cell

7024:   Level: developer

7026: .seealso DMPlexGetVTKCellHeight()
7027: @*/
7028: PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
7029: {
7030:   DM_Plex *mesh = (DM_Plex*) dm->data;

7034:   mesh->vtkCellHeight = cellHeight;
7035:   return(0);
7036: }

7038: /*@
7039:   DMPlexGetGhostCellStratum - Get the range of cells which are used to enforce FV boundary conditions

7041:   Input Parameter:
7042: . dm - The DMPlex object

7044:   Output Parameters:
7045: + gcStart - The first ghost cell, or NULL
7046: - gcEnd   - The upper bound on ghost cells, or NULL

7048:   Level: advanced

7050: .seealso DMPlexConstructGhostCells(), DMPlexGetGhostCellStratum()
7051: @*/
7052: PetscErrorCode DMPlexGetGhostCellStratum(DM dm, PetscInt *gcStart, PetscInt *gcEnd)
7053: {
7054:   DMLabel        ctLabel;

7059:   DMPlexGetCellTypeLabel(dm, &ctLabel);
7060:   DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_FV_GHOST, gcStart, gcEnd);
7061:   return(0);
7062: }

7064: /* We can easily have a form that takes an IS instead */
7065: PetscErrorCode DMPlexCreateNumbering_Plex(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
7066: {
7067:   PetscSection   section, globalSection;
7068:   PetscInt      *numbers, p;

7072:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);
7073:   PetscSectionSetChart(section, pStart, pEnd);
7074:   for (p = pStart; p < pEnd; ++p) {
7075:     PetscSectionSetDof(section, p, 1);
7076:   }
7077:   PetscSectionSetUp(section);
7078:   PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);
7079:   PetscMalloc1(pEnd - pStart, &numbers);
7080:   for (p = pStart; p < pEnd; ++p) {
7081:     PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);
7082:     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
7083:     else                       numbers[p-pStart] += shift;
7084:   }
7085:   ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);
7086:   if (globalSize) {
7087:     PetscLayout layout;
7088:     PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);
7089:     PetscLayoutGetSize(layout, globalSize);
7090:     PetscLayoutDestroy(&layout);
7091:   }
7092:   PetscSectionDestroy(&section);
7093:   PetscSectionDestroy(&globalSection);
7094:   return(0);
7095: }

7097: PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
7098: {
7099:   PetscInt       cellHeight, cStart, cEnd;

7103:   DMPlexGetVTKCellHeight(dm, &cellHeight);
7104:   if (includeHybrid) {DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);}
7105:   else               {DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);}
7106:   DMPlexCreateNumbering_Plex(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);
7107:   return(0);
7108: }

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

7113:   Input Parameter:
7114: . dm   - The DMPlex object

7116:   Output Parameter:
7117: . globalCellNumbers - Global cell numbers for all cells on this process

7119:   Level: developer

7121: .seealso DMPlexGetVertexNumbering()
7122: @*/
7123: PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
7124: {
7125:   DM_Plex       *mesh = (DM_Plex*) dm->data;

7130:   if (!mesh->globalCellNumbers) {DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);}
7131:   *globalCellNumbers = mesh->globalCellNumbers;
7132:   return(0);
7133: }

7135: PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
7136: {
7137:   PetscInt       vStart, vEnd;

7142:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
7143:   DMPlexCreateNumbering_Plex(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);
7144:   return(0);
7145: }

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

7150:   Input Parameter:
7151: . dm   - The DMPlex object

7153:   Output Parameter:
7154: . globalVertexNumbers - Global vertex numbers for all vertices on this process

7156:   Level: developer

7158: .seealso DMPlexGetCellNumbering()
7159: @*/
7160: PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
7161: {
7162:   DM_Plex       *mesh = (DM_Plex*) dm->data;

7167:   if (!mesh->globalVertexNumbers) {DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);}
7168:   *globalVertexNumbers = mesh->globalVertexNumbers;
7169:   return(0);
7170: }

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

7175:   Input Parameter:
7176: . dm   - The DMPlex object

7178:   Output Parameter:
7179: . globalPointNumbers - Global numbers for all points on this process

7181:   Level: developer

7183: .seealso DMPlexGetCellNumbering()
7184: @*/
7185: PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
7186: {
7187:   IS             nums[4];
7188:   PetscInt       depths[4], gdepths[4], starts[4];
7189:   PetscInt       depth, d, shift = 0;

7194:   DMPlexGetDepth(dm, &depth);
7195:   /* For unstratified meshes use dim instead of depth */
7196:   if (depth < 0) {DMGetDimension(dm, &depth);}
7197:   for (d = 0; d <= depth; ++d) {
7198:     PetscInt end;

7200:     depths[d] = depth-d;
7201:     DMPlexGetDepthStratum(dm, depths[d], &starts[d], &end);
7202:     if (!(starts[d]-end)) { starts[d] = depths[d] = -1; }
7203:   }
7204:   PetscSortIntWithArray(depth+1, starts, depths);
7205:   MPIU_Allreduce(depths, gdepths, depth+1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));
7206:   for (d = 0; d <= depth; ++d) {
7207:     if (starts[d] >= 0 && depths[d] != gdepths[d]) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expected depth %D, found %D",depths[d],gdepths[d]);
7208:   }
7209:   for (d = 0; d <= depth; ++d) {
7210:     PetscInt pStart, pEnd, gsize;

7212:     DMPlexGetDepthStratum(dm, gdepths[d], &pStart, &pEnd);
7213:     DMPlexCreateNumbering_Plex(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);
7214:     shift += gsize;
7215:   }
7216:   ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);
7217:   for (d = 0; d <= depth; ++d) {ISDestroy(&nums[d]);}
7218:   return(0);
7219: }


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

7225:   Input Parameter:
7226: . dm - The DMPlex object

7228:   Output Parameter:
7229: . ranks - The rank field

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

7234:   Level: intermediate

7236: .seealso: DMView()
7237: @*/
7238: PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
7239: {
7240:   DM             rdm;
7241:   PetscFE        fe;
7242:   PetscScalar   *r;
7243:   PetscMPIInt    rank;
7244:   DMPolytopeType ct;
7245:   PetscInt       dim, cStart, cEnd, c;
7246:   PetscBool      simplex;

7252:   MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
7253:   DMClone(dm, &rdm);
7254:   DMGetDimension(rdm, &dim);
7255:   DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);
7256:   DMPlexGetCellType(dm, cStart, &ct);
7257:   simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
7258:   PetscFECreateDefault(PETSC_COMM_SELF, dim, 1, simplex, "PETSc___rank_", -1, &fe);
7259:   PetscObjectSetName((PetscObject) fe, "rank");
7260:   DMSetField(rdm, 0, NULL, (PetscObject) fe);
7261:   PetscFEDestroy(&fe);
7262:   DMCreateDS(rdm);
7263:   DMCreateGlobalVector(rdm, ranks);
7264:   PetscObjectSetName((PetscObject) *ranks, "partition");
7265:   VecGetArray(*ranks, &r);
7266:   for (c = cStart; c < cEnd; ++c) {
7267:     PetscScalar *lr;

7269:     DMPlexPointGlobalRef(rdm, c, r, &lr);
7270:     if (lr) *lr = rank;
7271:   }
7272:   VecRestoreArray(*ranks, &r);
7273:   DMDestroy(&rdm);
7274:   return(0);
7275: }

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

7280:   Input Parameters:
7281: + dm    - The DMPlex
7282: - label - The DMLabel

7284:   Output Parameter:
7285: . val - The label value field

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

7290:   Level: intermediate

7292: .seealso: DMView()
7293: @*/
7294: PetscErrorCode DMPlexCreateLabelField(DM dm, DMLabel label, Vec *val)
7295: {
7296:   DM             rdm;
7297:   PetscFE        fe;
7298:   PetscScalar   *v;
7299:   PetscInt       dim, cStart, cEnd, c;

7306:   DMClone(dm, &rdm);
7307:   DMGetDimension(rdm, &dim);
7308:   PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___label_value_", -1, &fe);
7309:   PetscObjectSetName((PetscObject) fe, "label_value");
7310:   DMSetField(rdm, 0, NULL, (PetscObject) fe);
7311:   PetscFEDestroy(&fe);
7312:   DMCreateDS(rdm);
7313:   DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);
7314:   DMCreateGlobalVector(rdm, val);
7315:   PetscObjectSetName((PetscObject) *val, "label_value");
7316:   VecGetArray(*val, &v);
7317:   for (c = cStart; c < cEnd; ++c) {
7318:     PetscScalar *lv;
7319:     PetscInt     cval;

7321:     DMPlexPointGlobalRef(rdm, c, v, &lv);
7322:     DMLabelGetValue(label, c, &cval);
7323:     *lv = cval;
7324:   }
7325:   VecRestoreArray(*val, &v);
7326:   DMDestroy(&rdm);
7327:   return(0);
7328: }

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

7333:   Input Parameter:
7334: . dm - The DMPlex object

7336:   Notes:
7337:   This is a useful diagnostic when creating meshes programmatically.

7339:   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().

7341:   Level: developer

7343: .seealso: DMCreate(), DMSetFromOptions()
7344: @*/
7345: PetscErrorCode DMPlexCheckSymmetry(DM dm)
7346: {
7347:   PetscSection    coneSection, supportSection;
7348:   const PetscInt *cone, *support;
7349:   PetscInt        coneSize, c, supportSize, s;
7350:   PetscInt        pStart, pEnd, p, pp, csize, ssize;
7351:   PetscBool       storagecheck = PETSC_TRUE;
7352:   PetscErrorCode  ierr;

7356:   DMViewFromOptions(dm, NULL, "-sym_dm_view");
7357:   DMPlexGetConeSection(dm, &coneSection);
7358:   DMPlexGetSupportSection(dm, &supportSection);
7359:   /* Check that point p is found in the support of its cone points, and vice versa */
7360:   DMPlexGetChart(dm, &pStart, &pEnd);
7361:   for (p = pStart; p < pEnd; ++p) {
7362:     DMPlexGetConeSize(dm, p, &coneSize);
7363:     DMPlexGetCone(dm, p, &cone);
7364:     for (c = 0; c < coneSize; ++c) {
7365:       PetscBool dup = PETSC_FALSE;
7366:       PetscInt  d;
7367:       for (d = c-1; d >= 0; --d) {
7368:         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
7369:       }
7370:       DMPlexGetSupportSize(dm, cone[c], &supportSize);
7371:       DMPlexGetSupport(dm, cone[c], &support);
7372:       for (s = 0; s < supportSize; ++s) {
7373:         if (support[s] == p) break;
7374:       }
7375:       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
7376:         PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);
7377:         for (s = 0; s < coneSize; ++s) {
7378:           PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);
7379:         }
7380:         PetscPrintf(PETSC_COMM_SELF, "\n");
7381:         PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);
7382:         for (s = 0; s < supportSize; ++s) {
7383:           PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);
7384:         }
7385:         PetscPrintf(PETSC_COMM_SELF, "\n");
7386:         if (dup) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not repeatedly found in support of repeated cone point %D", p, cone[c]);
7387:         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
7388:       }
7389:     }
7390:     DMPlexGetTreeParent(dm, p, &pp, NULL);
7391:     if (p != pp) { storagecheck = PETSC_FALSE; continue; }
7392:     DMPlexGetSupportSize(dm, p, &supportSize);
7393:     DMPlexGetSupport(dm, p, &support);
7394:     for (s = 0; s < supportSize; ++s) {
7395:       DMPlexGetConeSize(dm, support[s], &coneSize);
7396:       DMPlexGetCone(dm, support[s], &cone);
7397:       for (c = 0; c < coneSize; ++c) {
7398:         DMPlexGetTreeParent(dm, cone[c], &pp, NULL);
7399:         if (cone[c] != pp) { c = 0; break; }
7400:         if (cone[c] == p) break;
7401:       }
7402:       if (c >= coneSize) {
7403:         PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);
7404:         for (c = 0; c < supportSize; ++c) {
7405:           PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);
7406:         }
7407:         PetscPrintf(PETSC_COMM_SELF, "\n");
7408:         PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);
7409:         for (c = 0; c < coneSize; ++c) {
7410:           PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);
7411:         }
7412:         PetscPrintf(PETSC_COMM_SELF, "\n");
7413:         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
7414:       }
7415:     }
7416:   }
7417:   if (storagecheck) {
7418:     PetscSectionGetStorageSize(coneSection, &csize);
7419:     PetscSectionGetStorageSize(supportSection, &ssize);
7420:     if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
7421:   }
7422:   return(0);
7423: }

7425: /*
7426:   For submeshes with cohesive cells (see DMPlexConstructCohesiveCells()), we allow a special case where some of the boundary of a face (edges and vertices) are not duplicated. We call these special boundary points "unsplit", since the same edge or vertex appears in both copies of the face. These unsplit points throw off our counting, so we have to explicitly account for them here.
7427: */
7428: static PetscErrorCode DMPlexCellUnsplitVertices_Private(DM dm, PetscInt c, DMPolytopeType ct, PetscInt *unsplit)
7429: {
7430:   DMPolytopeType  cct;
7431:   PetscInt        ptpoints[4];
7432:   const PetscInt *cone, *ccone, *ptcone;
7433:   PetscInt        coneSize, cp, cconeSize, ccp, npt = 0, pt;
7434:   PetscErrorCode  ierr;

7437:   *unsplit = 0;
7438:   switch (ct) {
7439:     case DM_POLYTOPE_SEG_PRISM_TENSOR:
7440:       DMPlexGetCone(dm, c, &cone);
7441:       DMPlexGetConeSize(dm, c, &coneSize);
7442:       for (cp = 0; cp < coneSize; ++cp) {
7443:         DMPlexGetCellType(dm, cone[cp], &cct);
7444:         if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) ptpoints[npt++] = cone[cp];
7445:       }
7446:       break;
7447:     case DM_POLYTOPE_TRI_PRISM_TENSOR:
7448:     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
7449:       DMPlexGetCone(dm, c, &cone);
7450:       DMPlexGetConeSize(dm, c, &coneSize);
7451:       for (cp = 0; cp < coneSize; ++cp) {
7452:         DMPlexGetCone(dm, cone[cp], &ccone);
7453:         DMPlexGetConeSize(dm, cone[cp], &cconeSize);
7454:         for (ccp = 0; ccp < cconeSize; ++ccp) {
7455:           DMPlexGetCellType(dm, ccone[ccp], &cct);
7456:           if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) {
7457:             PetscInt p;
7458:             for (p = 0; p < npt; ++p) if (ptpoints[p] == ccone[ccp]) break;
7459:             if (p == npt) ptpoints[npt++] = ccone[ccp];
7460:           }
7461:         }
7462:       }
7463:       break;
7464:     default: break;
7465:   }
7466:   for (pt = 0; pt < npt; ++pt) {
7467:     DMPlexGetCone(dm, ptpoints[pt], &ptcone);
7468:     if (ptcone[0] == ptcone[1]) ++(*unsplit);
7469:   }
7470:   return(0);
7471: }

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

7476:   Input Parameters:
7477: + dm - The DMPlex object
7478: - cellHeight - Normally 0

7480:   Notes:
7481:   This is a useful diagnostic when creating meshes programmatically.
7482:   Currently applicable only to homogeneous simplex or tensor meshes.

7484:   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().

7486:   Level: developer

7488: .seealso: DMCreate(), DMSetFromOptions()
7489: @*/
7490: PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscInt cellHeight)
7491: {
7492:   DMPlexInterpolatedFlag interp;
7493:   DMPolytopeType         ct;
7494:   PetscInt               vStart, vEnd, cStart, cEnd, c;
7495:   PetscErrorCode         ierr;

7499:   DMPlexIsInterpolated(dm, &interp);
7500:   DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
7501:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
7502:   for (c = cStart; c < cEnd; ++c) {
7503:     PetscInt *closure = NULL;
7504:     PetscInt  coneSize, closureSize, cl, Nv = 0;

7506:     DMPlexGetCellType(dm, c, &ct);
7507:     if ((PetscInt) ct < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has no cell type", c);
7508:     if (ct == DM_POLYTOPE_UNKNOWN) continue;
7509:     if (interp == DMPLEX_INTERPOLATED_FULL) {
7510:       DMPlexGetConeSize(dm, c, &coneSize);
7511:       if (coneSize != DMPolytopeTypeGetConeSize(ct)) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D of type %s has cone size %D != %D", c, DMPolytopeTypes[ct], coneSize, DMPolytopeTypeGetConeSize(ct));
7512:     }
7513:     DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
7514:     for (cl = 0; cl < closureSize*2; cl += 2) {
7515:       const PetscInt p = closure[cl];
7516:       if ((p >= vStart) && (p < vEnd)) ++Nv;
7517:     }
7518:     DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
7519:     /* Special Case: Tensor faces with identified vertices */
7520:     if (Nv < DMPolytopeTypeGetNumVertices(ct)) {
7521:       PetscInt unsplit;

7523:       DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);
7524:       if (Nv + unsplit == DMPolytopeTypeGetNumVertices(ct)) continue;
7525:     }
7526:     if (Nv != DMPolytopeTypeGetNumVertices(ct)) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D of type %s has %D vertices != %D", c, DMPolytopeTypes[ct], Nv, DMPolytopeTypeGetNumVertices(ct));
7527:   }
7528:   return(0);
7529: }

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

7534:   Not Collective

7536:   Input Parameters:
7537: + dm - The DMPlex object
7538: - cellHeight - Normally 0

7540:   Notes:
7541:   This is a useful diagnostic when creating meshes programmatically.
7542:   This routine is only relevant for meshes that are fully interpolated across all ranks.
7543:   It will error out if a partially interpolated mesh is given on some rank.
7544:   It will do nothing for locally uninterpolated mesh (as there is nothing to check).

7546:   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().

7548:   Level: developer

7550: .seealso: DMCreate(), DMPlexGetVTKCellHeight(), DMSetFromOptions()
7551: @*/
7552: PetscErrorCode DMPlexCheckFaces(DM dm, PetscInt cellHeight)
7553: {
7554:   PetscInt       dim, depth, vStart, vEnd, cStart, cEnd, c, h;
7556:   DMPlexInterpolatedFlag interpEnum;

7560:   DMPlexIsInterpolated(dm, &interpEnum);
7561:   if (interpEnum == DMPLEX_INTERPOLATED_NONE) return(0);
7562:   if (interpEnum == DMPLEX_INTERPOLATED_PARTIAL) {
7563:     PetscMPIInt rank;
7564:     MPI_Comm    comm;

7566:     PetscObjectGetComm((PetscObject) dm, &comm);
7567:     MPI_Comm_rank(comm, &rank);
7568:     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Mesh is only partially interpolated on rank %d, this is currently not supported", rank);
7569:   }

7571:   DMGetDimension(dm, &dim);
7572:   DMPlexGetDepth(dm, &depth);
7573:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
7574:   for (h = cellHeight; h < PetscMin(depth, dim); ++h) {
7575:     DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);
7576:     for (c = cStart; c < cEnd; ++c) {
7577:       const PetscInt      *cone, *ornt, *faceSizes, *faces;
7578:       const DMPolytopeType *faceTypes;
7579:       DMPolytopeType        ct;
7580:       PetscInt              numFaces, coneSize, f;
7581:       PetscInt             *closure = NULL, closureSize, cl, numCorners = 0, fOff = 0, unsplit;

7583:       DMPlexGetCellType(dm, c, &ct);
7584:       DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);
7585:       if (unsplit) continue;
7586:       DMPlexGetConeSize(dm, c, &coneSize);
7587:       DMPlexGetCone(dm, c, &cone);
7588:       DMPlexGetConeOrientation(dm, c, &ornt);
7589:       DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
7590:       for (cl = 0; cl < closureSize*2; cl += 2) {
7591:         const PetscInt p = closure[cl];
7592:         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
7593:       }
7594:       DMPlexGetRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);
7595:       if (coneSize != numFaces) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D of type %s has %D faces but should have %D", c, DMPolytopeTypes[ct], coneSize, numFaces);
7596:       for (f = 0; f < numFaces; ++f) {
7597:         DMPolytopeType fct;
7598:         PetscInt       *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;

7600:         DMPlexGetCellType(dm, cone[f], &fct);
7601:         DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);
7602:         for (cl = 0; cl < fclosureSize*2; cl += 2) {
7603:           const PetscInt p = fclosure[cl];
7604:           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
7605:         }
7606:         if (fnumCorners != faceSizes[f]) SETERRQ7(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D of type %s (cone idx %D) of cell %D of type %s has %D vertices but should have %D", cone[f], DMPolytopeTypes[fct], f, c, DMPolytopeTypes[ct], fnumCorners, faceSizes[f]);
7607:         for (v = 0; v < fnumCorners; ++v) {
7608:           if (fclosure[v] != faces[fOff+v]) SETERRQ8(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D of type %s (cone idx %d) of cell %D of type %s vertex %D, %D != %D", cone[f], DMPolytopeTypes[fct], f, c, DMPolytopeTypes[ct], v, fclosure[v], faces[fOff+v]);
7609:         }
7610:         DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);
7611:         fOff += faceSizes[f];
7612:       }
7613:       DMPlexRestoreRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);
7614:       DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
7615:     }
7616:   }
7617:   return(0);
7618: }

7620: /*@
7621:   DMPlexCheckGeometry - Check the geometry of mesh cells

7623:   Input Parameter:
7624: . dm - The DMPlex object

7626:   Notes:
7627:   This is a useful diagnostic when creating meshes programmatically.

7629:   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().

7631:   Level: developer

7633: .seealso: DMCreate(), DMSetFromOptions()
7634: @*/
7635: PetscErrorCode DMPlexCheckGeometry(DM dm)
7636: {
7637:   PetscReal      detJ, J[9], refVol = 1.0;
7638:   PetscReal      vol;
7639:   PetscBool      periodic;
7640:   PetscInt       dim, depth, dE, d, cStart, cEnd, c;

7644:   DMGetDimension(dm, &dim);
7645:   DMGetCoordinateDim(dm, &dE);
7646:   if (dim != dE) return(0);
7647:   DMPlexGetDepth(dm, &depth);
7648:   DMGetPeriodicity(dm, &periodic, NULL, NULL, NULL);
7649:   for (d = 0; d < dim; ++d) refVol *= 2.0;
7650:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
7651:   for (c = cStart; c < cEnd; ++c) {
7652:     DMPolytopeType ct;
7653:     PetscInt       unsplit;
7654:     PetscBool      ignoreZeroVol = PETSC_FALSE;

7656:     DMPlexGetCellType(dm, c, &ct);
7657:     switch (ct) {
7658:       case DM_POLYTOPE_SEG_PRISM_TENSOR:
7659:       case DM_POLYTOPE_TRI_PRISM_TENSOR:
7660:       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
7661:         ignoreZeroVol = PETSC_TRUE; break;
7662:       default: break;
7663:     }
7664:     switch (ct) {
7665:       case DM_POLYTOPE_TRI_PRISM:
7666:       case DM_POLYTOPE_TRI_PRISM_TENSOR:
7667:       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
7668:         continue;
7669:       default: break;
7670:     }
7671:     DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);
7672:     if (unsplit) continue;
7673:     DMPlexComputeCellGeometryFEM(dm, c, NULL, NULL, J, NULL, &detJ);
7674:     if (detJ < -PETSC_SMALL || (detJ <= 0.0 && !ignoreZeroVol)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D of type %s is inverted, |J| = %g", c, DMPolytopeTypes[ct], (double) detJ);
7675:     PetscInfo2(dm, "Cell %D FEM Volume %g\n", c, (double) detJ*refVol);
7676:     if (depth > 1 && !periodic) {
7677:       DMPlexComputeCellGeometryFVM(dm, c, &vol, NULL, NULL);
7678:       if (vol < -PETSC_SMALL || (vol <= 0.0 && !ignoreZeroVol)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D of type %s is inverted, vol = %g", c, DMPolytopeTypes[ct], (double) vol);
7679:       PetscInfo2(dm, "Cell %D FVM Volume %g\n", c, (double) vol);
7680:     }
7681:   }
7682:   return(0);
7683: }

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

7688:   Input Parameters:
7689: . dm - The DMPlex object

7691:   Notes:
7692:   This is mainly intended for debugging/testing purposes.
7693:   It currently checks only meshes with no partition overlapping.

7695:   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().

7697:   Level: developer

7699: .seealso: DMGetPointSF(), DMSetFromOptions()
7700: @*/
7701: PetscErrorCode DMPlexCheckPointSF(DM dm)
7702: {
7703:   PetscSF         pointSF;
7704:   PetscInt        cellHeight, cStart, cEnd, l, nleaves, nroots, overlap;
7705:   const PetscInt *locals, *rootdegree;
7706:   PetscBool       distributed;
7707:   PetscErrorCode  ierr;

7711:   DMGetPointSF(dm, &pointSF);
7712:   DMPlexIsDistributed(dm, &distributed);
7713:   if (!distributed) return(0);
7714:   DMPlexGetOverlap(dm, &overlap);
7715:   if (overlap) {
7716:     PetscPrintf(PetscObjectComm((PetscObject)dm), "Warning: DMPlexCheckPointSF() is currently not implemented for meshes with partition overlapping");
7717:     return(0);
7718:   }
7719:   if (!pointSF) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but does not have PointSF attached");
7720:   PetscSFGetGraph(pointSF, &nroots, &nleaves, &locals, NULL);
7721:   if (nroots < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but its PointSF has no graph set");
7722:   PetscSFComputeDegreeBegin(pointSF, &rootdegree);
7723:   PetscSFComputeDegreeEnd(pointSF, &rootdegree);

7725:   /* 1) check there are no faces in 2D, cells in 3D, in interface */
7726:   DMPlexGetVTKCellHeight(dm, &cellHeight);
7727:   DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
7728:   for (l = 0; l < nleaves; ++l) {
7729:     const PetscInt point = locals[l];

7731:     if (point >= cStart && point < cEnd) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point SF contains %D which is a cell", point);
7732:   }

7734:   /* 2) if some point is in interface, then all its cone points must be also in interface (either as leaves or roots) */
7735:   for (l = 0; l < nleaves; ++l) {
7736:     const PetscInt  point = locals[l];
7737:     const PetscInt *cone;
7738:     PetscInt        coneSize, c, idx;

7740:     DMPlexGetConeSize(dm, point, &coneSize);
7741:     DMPlexGetCone(dm, point, &cone);
7742:     for (c = 0; c < coneSize; ++c) {
7743:       if (!rootdegree[cone[c]]) {
7744:         PetscFindInt(cone[c], nleaves, locals, &idx);
7745:         if (idx < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point SF contains %D but not %D from its cone", point, cone[c]);
7746:       }
7747:     }
7748:   }
7749:   return(0);
7750: }

7752: typedef struct cell_stats
7753: {
7754:   PetscReal min, max, sum, squaresum;
7755:   PetscInt  count;
7756: } cell_stats_t;

7758: static void MPIAPI cell_stats_reduce(void *a, void *b, int * len, MPI_Datatype *datatype)
7759: {
7760:   PetscInt i, N = *len;

7762:   for (i = 0; i < N; i++) {
7763:     cell_stats_t *A = (cell_stats_t *) a;
7764:     cell_stats_t *B = (cell_stats_t *) b;

7766:     B->min = PetscMin(A->min,B->min);
7767:     B->max = PetscMax(A->max,B->max);
7768:     B->sum += A->sum;
7769:     B->squaresum += A->squaresum;
7770:     B->count += A->count;
7771:   }
7772: }

7774: /*@
7775:   DMPlexCheckCellShape - Checks the Jacobian of the mapping from reference to real cells and computes some minimal statistics.

7777:   Collective on dm

7779:   Input Parameters:
7780: + dm        - The DMPlex object
7781: . output    - If true, statistics will be displayed on stdout
7782: - condLimit - Display all cells above this condition number, or PETSC_DETERMINE for no cell output

7784:   Notes:
7785:   This is mainly intended for debugging/testing purposes.

7787:   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().

7789:   Level: developer

7791: .seealso: DMSetFromOptions(), DMPlexComputeOrthogonalQuality()
7792: @*/
7793: PetscErrorCode DMPlexCheckCellShape(DM dm, PetscBool output, PetscReal condLimit)
7794: {
7795:   DM             dmCoarse;
7796:   cell_stats_t   stats, globalStats;
7797:   MPI_Comm       comm = PetscObjectComm((PetscObject)dm);
7798:   PetscReal      *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
7799:   PetscReal      limit = condLimit > 0 ? condLimit : PETSC_MAX_REAL;
7800:   PetscInt       cdim, cStart, cEnd, c, eStart, eEnd, count = 0;
7801:   PetscMPIInt    rank,size;

7806:   stats.min   = PETSC_MAX_REAL;
7807:   stats.max   = PETSC_MIN_REAL;
7808:   stats.sum   = stats.squaresum = 0.;
7809:   stats.count = 0;

7811:   MPI_Comm_size(comm, &size);
7812:   MPI_Comm_rank(comm, &rank);
7813:   DMGetCoordinateDim(dm,&cdim);
7814:   PetscMalloc2(PetscSqr(cdim), &J, PetscSqr(cdim), &invJ);
7815:   DMPlexGetSimplexOrBoxCells(dm,0,&cStart,&cEnd);
7816:   DMPlexGetDepthStratum(dm,1,&eStart,&eEnd);
7817:   for (c = cStart; c < cEnd; c++) {
7818:     PetscInt  i;
7819:     PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;

7821:     DMPlexComputeCellGeometryAffineFEM(dm,c,NULL,J,invJ,&detJ);
7822:     if (detJ < 0.0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted", c);
7823:     for (i = 0; i < PetscSqr(cdim); ++i) {
7824:       frobJ    += J[i] * J[i];
7825:       frobInvJ += invJ[i] * invJ[i];
7826:     }
7827:     cond2 = frobJ * frobInvJ;
7828:     cond  = PetscSqrtReal(cond2);

7830:     stats.min        = PetscMin(stats.min,cond);
7831:     stats.max        = PetscMax(stats.max,cond);
7832:     stats.sum       += cond;
7833:     stats.squaresum += cond2;
7834:     stats.count++;
7835:     if (output && cond > limit) {
7836:       PetscSection coordSection;
7837:       Vec          coordsLocal;
7838:       PetscScalar *coords = NULL;
7839:       PetscInt     Nv, d, clSize, cl, *closure = NULL;

7841:       DMGetCoordinatesLocal(dm, &coordsLocal);
7842:       DMGetCoordinateSection(dm, &coordSection);
7843:       DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);
7844:       PetscSynchronizedPrintf(comm, "[%d] Cell %D cond %g\n", rank, c, (double) cond);
7845:       for (i = 0; i < Nv/cdim; ++i) {
7846:         PetscSynchronizedPrintf(comm, "  Vertex %D: (", i);
7847:         for (d = 0; d < cdim; ++d) {
7848:           if (d > 0) {PetscSynchronizedPrintf(comm, ", ");}
7849:           PetscSynchronizedPrintf(comm, "%g", (double) PetscRealPart(coords[i*cdim+d]));
7850:         }
7851:         PetscSynchronizedPrintf(comm, ")\n");
7852:       }
7853:       DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);
7854:       for (cl = 0; cl < clSize*2; cl += 2) {
7855:         const PetscInt edge = closure[cl];

7857:         if ((edge >= eStart) && (edge < eEnd)) {
7858:           PetscReal len;

7860:           DMPlexComputeCellGeometryFVM(dm, edge, &len, NULL, NULL);
7861:           PetscSynchronizedPrintf(comm, "  Edge %D: length %g\n", edge, (double) len);
7862:         }
7863:       }
7864:       DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);
7865:       DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);
7866:     }
7867:   }
7868:   if (output) {PetscSynchronizedFlush(comm, NULL);}

7870:   if (size > 1) {
7871:     PetscMPIInt   blockLengths[2] = {4,1};
7872:     MPI_Aint      blockOffsets[2] = {offsetof(cell_stats_t,min),offsetof(cell_stats_t,count)};
7873:     MPI_Datatype  blockTypes[2]   = {MPIU_REAL,MPIU_INT}, statType;
7874:     MPI_Op        statReduce;

7876:     MPI_Type_create_struct(2,blockLengths,blockOffsets,blockTypes,&statType);
7877:     MPI_Type_commit(&statType);
7878:     MPI_Op_create(cell_stats_reduce, PETSC_TRUE, &statReduce);
7879:     MPI_Reduce(&stats,&globalStats,1,statType,statReduce,0,comm);
7880:     MPI_Op_free(&statReduce);
7881:     MPI_Type_free(&statType);
7882:   } else {
7883:     PetscArraycpy(&globalStats,&stats,1);
7884:   }
7885:   if (!rank) {
7886:     count = globalStats.count;
7887:     min   = globalStats.min;
7888:     max   = globalStats.max;
7889:     mean  = globalStats.sum / globalStats.count;
7890:     stdev = globalStats.count > 1 ? PetscSqrtReal(PetscMax((globalStats.squaresum - globalStats.count * mean * mean) / (globalStats.count - 1),0)) : 0.0;
7891:   }

7893:   if (output) {
7894:     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);
7895:   }
7896:   PetscFree2(J,invJ);

7898:   DMGetCoarseDM(dm,&dmCoarse);
7899:   if (dmCoarse) {
7900:     PetscBool isplex;

7902:     PetscObjectTypeCompare((PetscObject)dmCoarse,DMPLEX,&isplex);
7903:     if (isplex) {
7904:       DMPlexCheckCellShape(dmCoarse,output,condLimit);
7905:     }
7906:   }
7907:   return(0);
7908: }

7910: /*@
7911:   DMPlexComputeOrthogonalQuality - Compute cell-wise orthogonal quality mesh statistic. Optionally tags all cells with
7912:   orthogonal quality below given tolerance.

7914:   Collective

7916:   Input Parameters:
7917: + dm   - The DMPlex object
7918: . fv   - Optional PetscFV object for pre-computed cell/face centroid information
7919: - atol - [0, 1] Absolute tolerance for tagging cells.

7921:   Output Parameters:
7922: + OrthQual      - Vec containing orthogonal quality per cell
7923: - OrthQualLabel - DMLabel tagging cells below atol with DM_ADAPT_REFINE

7925:   Options Database Keys:
7926: + -dm_plex_orthogonal_quality_label_view - view OrthQualLabel if label is requested. Currently only PETSCVIEWERASCII is
7927: supported.
7928: - -dm_plex_orthogonal_quality_vec_view - view OrthQual vector.

7930:   Notes:
7931:   Orthogonal quality is given by the following formula:

7933:   \min \left[ \frac{A_i \cdot f_i}{\|A_i\| \|f_i\|} , \frac{A_i \cdot c_i}{\|A_i\| \|c_i\|} \right]

7935:   Where A_i is the i'th face-normal vector, f_i is the vector from the cell centroid to the i'th face centroid, and c_i
7936:   is the vector from the current cells centroid to the centroid of its i'th neighbor (which shares a face with the
7937:   current cell). This computes the vector similarity between each cell face and its corresponding neighbor centroid by
7938:   calculating the cosine of the angle between these vectors.

7940:   Orthogonal quality ranges from 1 (best) to 0 (worst).

7942:   This routine is mainly useful for FVM, however is not restricted to only FVM. The PetscFV object is optionally used to check for
7943:   pre-computed FVM cell data, but if it is not passed in then this data will be computed.

7945:   Cells are tagged if they have an orthogonal quality less than or equal to the absolute tolerance.

7947:   Level: intermediate

7949: .seealso: DMPlexCheckCellShape(), DMCreateLabel()
7950: @*/
7951: PetscErrorCode DMPlexComputeOrthogonalQuality(DM dm, PetscFV fv, PetscReal atol, Vec *OrthQual, DMLabel *OrthQualLabel)
7952: {
7953:   PetscInt                nc, cellHeight, cStart, cEnd, cell;
7954:   const PetscScalar       *cellGeomArr, *faceGeomArr;
7955:   MPI_Comm                comm;
7956:   Vec                     cellgeom, facegeom;
7957:   DM                      dmFace, dmCell;
7958:   IS                      glob;
7959:   DMPlexInterpolatedFlag  interpFlag;
7960:   ISLocalToGlobalMapping  ltog;
7961:   PetscViewer             vwr;
7962:   PetscErrorCode          ierr;

7967:   PetscObjectGetComm((PetscObject) dm, &comm);
7968:   DMGetDimension(dm, &nc);
7969:   if (nc < 2) {
7970:     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must have dimension >= 2 (current %D)", nc);
7971:   }
7972:   DMPlexIsInterpolated(dm, &interpFlag);
7973:   if (interpFlag != DMPLEX_INTERPOLATED_FULL) {
7974:     PetscMPIInt  rank;

7976:     MPI_Comm_rank(comm, &rank);
7977:     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must be fully interpolated, DM on rank %d is not fully interpolated", rank);
7978:   }
7979:   if (OrthQualLabel) {
7981:     DMCreateLabel(dm, "Orthogonal_Quality");
7982:     DMGetLabel(dm, "Orthogonal_Quality", OrthQualLabel);
7983:   } else {
7984:     *OrthQualLabel = NULL;
7985:   }

7987:   DMPlexGetVTKCellHeight(dm, &cellHeight);
7988:   DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
7989:   DMPlexCreateCellNumbering_Internal(dm, PETSC_TRUE, &glob);
7990:   ISLocalToGlobalMappingCreateIS(glob, &ltog);
7991:   ISLocalToGlobalMappingSetType(ltog, ISLOCALTOGLOBALMAPPINGHASH);
7992:   VecCreate(comm, OrthQual);
7993:   VecSetType(*OrthQual, VECSTANDARD);
7994:   VecSetSizes(*OrthQual, cEnd-cStart, PETSC_DETERMINE);
7995:   VecSetLocalToGlobalMapping(*OrthQual, ltog);
7996:   VecSetUp(*OrthQual);
7997:   ISDestroy(&glob);
7998:   ISLocalToGlobalMappingDestroy(&ltog);
7999:   DMPlexGetDataFVM(dm, fv, &cellgeom, &facegeom, NULL);
8000:   VecGetArrayRead(cellgeom, &cellGeomArr);
8001:   VecGetArrayRead(facegeom, &faceGeomArr);
8002:   VecGetDM(cellgeom, &dmCell);
8003:   VecGetDM(facegeom, &dmFace);
8004:   for (cell = cStart; cell < cEnd; cell++) {
8005:     PetscInt           cellneigh, cellneighiter = 0, nf, adjSize = PETSC_DETERMINE, ix = cell-cStart;
8006:     const PetscInt     *cone;
8007:     PetscInt           cellarr[2], *adj = NULL;
8008:     PetscScalar        *cArr, *fArr;
8009:     PetscReal          minvalc = 1.0, minvalf = 1.0, OQ;
8010:     PetscFVCellGeom    *cg;

8012:     cellarr[0] = cell;
8013:     /* Make indexing into cellGeom easier */
8014:     DMPlexPointLocalRead(dmCell, cell, cellGeomArr, &cg);
8015:     DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &adjSize, &adj);
8016:     DMPlexGetConeSize(dm, cell, &nf);
8017:     DMPlexGetCone(dm, cell, &cone);
8018:     /* Technically 1 too big, but easier than fiddling with empty adjacency array */
8019:     PetscCalloc2(adjSize, &cArr, adjSize, &fArr);
8020:     for (cellneigh = 0; cellneigh < adjSize; cellneigh++) {
8021:       PetscInt         numcovpts, i, neigh = adj[cellneigh];
8022:       const PetscInt   *covpts;
8023:       PetscReal        normci = 0, normfi = 0, normai = 0;
8024:       PetscReal        *ci, *fi, *Ai;
8025:       PetscFVCellGeom  *cgneigh;
8026:       PetscFVFaceGeom  *fg;

8028:       /* Don't count ourselves in the neighbor list */
8029:       if (neigh == cell) continue;
8030:       PetscMalloc3(nc, &ci, nc, &fi, nc, &Ai);
8031:       DMPlexPointLocalRead(dmCell, neigh, cellGeomArr, &cgneigh);
8032:       cellarr[1] = neigh;
8033:       DMPlexGetMeet(dm, 2, cellarr, &numcovpts, &covpts);
8034:       DMPlexPointLocalRead(dmFace, covpts[0], faceGeomArr, &fg);
8035:       DMPlexRestoreMeet(dm, 2, cellarr, &numcovpts, &covpts);

8037:       /* Compute c_i, f_i and their norms */
8038:       for (i = 0; i < nc; i++) {
8039:         ci[i] = cgneigh->centroid[i] - cg->centroid[i];
8040:         fi[i] = fg->centroid[i] - cg->centroid[i];
8041:         Ai[i] = fg->normal[i];
8042:         normci += PetscPowScalar(ci[i], 2);
8043:         normfi += PetscPowScalar(fi[i], 2);
8044:         normai += PetscPowScalar(Ai[i], 2);
8045:       }
8046:       normci = PetscSqrtScalar(normci);
8047:       normfi = PetscSqrtScalar(normfi);
8048:       normai = PetscSqrtScalar(normai);

8050:       /* Normalize and compute for each face-cell-normal pair */
8051:       for (i = 0; i < nc; i++) {
8052:         ci[i] = ci[i]/normci;
8053:         fi[i] = fi[i]/normfi;
8054:         Ai[i] = Ai[i]/normai;
8055:         /* PetscAbs because I don't know if normals are guaranteed to point out */
8056:         cArr[cellneighiter] += PetscAbs(Ai[i]*ci[i]);
8057:         fArr[cellneighiter] += PetscAbs(Ai[i]*fi[i]);
8058:       }
8059:       if (PetscRealPart(cArr[cellneighiter]) < minvalc) {
8060:         minvalc = PetscRealPart(cArr[cellneighiter]);
8061:       }
8062:       if (PetscRealPart(fArr[cellneighiter]) < minvalf) {
8063:         minvalf = PetscRealPart(fArr[cellneighiter]);
8064:       }
8065:       cellneighiter++;
8066:       PetscFree3(ci, fi, Ai);
8067:     }
8068:     PetscFree(adj);
8069:     PetscFree2(cArr, fArr);
8070:     /* Defer to cell if they're equal */
8071:     OQ = PetscMin(minvalf, minvalc);
8072:     if (OrthQualLabel) {
8073:       if (OQ <= atol) {
8074:         DMLabelSetValue(*OrthQualLabel, cell, DM_ADAPT_REFINE);
8075:       }
8076:     }
8077:     VecSetValuesLocal(*OrthQual, 1, (const PetscInt *) &ix, (const PetscScalar *) &OQ, INSERT_VALUES);
8078:   }
8079:   VecAssemblyBegin(*OrthQual);
8080:   VecAssemblyEnd(*OrthQual);
8081:   VecRestoreArrayRead(cellgeom, &cellGeomArr);
8082:   VecRestoreArrayRead(facegeom, &faceGeomArr);
8083:   PetscOptionsGetViewer(comm, NULL, NULL, "-dm_plex_orthogonal_quality_label_view", &vwr, NULL, NULL);
8084:   if (OrthQualLabel) {
8085:     if (vwr) {
8086:       DMLabelView(*OrthQualLabel, vwr);
8087:     }
8088:   }
8089:   PetscViewerDestroy(&vwr);
8090:   VecViewFromOptions(*OrthQual, NULL, "-dm_plex_orthogonal_quality_vec_view");
8091:   return(0);
8092: }

8094: /* Pointwise interpolation
8095:      Just code FEM for now
8096:      u^f = I u^c
8097:      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
8098:      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
8099:      I_{ij} = psi^f_i phi^c_j
8100: */
8101: PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
8102: {
8103:   PetscSection   gsc, gsf;
8104:   PetscInt       m, n;
8105:   void          *ctx;
8106:   DM             cdm;
8107:   PetscBool      regular, ismatis, isRefined = dmCoarse->data == dmFine->data ? PETSC_FALSE : PETSC_TRUE;

8111:   DMGetGlobalSection(dmFine, &gsf);
8112:   PetscSectionGetConstrainedStorageSize(gsf, &m);
8113:   DMGetGlobalSection(dmCoarse, &gsc);
8114:   PetscSectionGetConstrainedStorageSize(gsc, &n);

8116:   PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis);
8117:   MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);
8118:   MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);
8119:   MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype);
8120:   DMGetApplicationContext(dmFine, &ctx);

8122:   DMGetCoarseDM(dmFine, &cdm);
8123:   DMPlexGetRegularRefinement(dmFine, &regular);
8124:   if (!isRefined || (regular && cdm == dmCoarse)) {DMPlexComputeInterpolatorNested(dmCoarse, dmFine, isRefined, *interpolation, ctx);}
8125:   else                                            {DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);}
8126:   MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");
8127:   if (scaling) {
8128:     /* Use naive scaling */
8129:     DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);
8130:   }
8131:   return(0);
8132: }

8134: PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
8135: {
8137:   VecScatter     ctx;

8140:   DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);
8141:   MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);
8142:   VecScatterDestroy(&ctx);
8143:   return(0);
8144: }

8146: static void g0_identity_private(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8147:                                 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8148:                                 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8149:                                 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
8150: {
8151:   g0[0] = 1.0;
8152: }

8154: PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
8155: {
8156:   PetscSection   gsc, gsf;
8157:   PetscInt       m, n;
8158:   void          *ctx;
8159:   DM             cdm;
8160:   PetscBool      regular;

8164:   if (dmFine == dmCoarse) {
8165:     DM       dmc;
8166:     PetscDS  ds;
8167:     Vec      u;
8168:     IS       cellIS;
8169:     PetscInt depth;

8171:     DMClone(dmFine, &dmc);
8172:     DMCopyDisc(dmFine, dmc);
8173:     DMGetDS(dmc, &ds);
8174:     PetscDSSetJacobian(ds, 0, 0, g0_identity_private, NULL, NULL, NULL);
8175:     DMCreateMatrix(dmc, mass);
8176:     DMGetGlobalVector(dmc, &u);
8177:     DMPlexGetDepth(dmc, &depth);
8178:     DMGetStratumIS(dmc, "depth", depth, &cellIS);
8179:     MatZeroEntries(*mass);
8180:     DMPlexComputeJacobian_Internal(dmc, cellIS, 0.0, 0.0, u, NULL, *mass, *mass, NULL);
8181:     ISDestroy(&cellIS);
8182:     DMRestoreGlobalVector(dmc, &u);
8183:     DMDestroy(&dmc);
8184:   } else {
8185:     DMGetGlobalSection(dmFine, &gsf);
8186:     PetscSectionGetConstrainedStorageSize(gsf, &m);
8187:     DMGetGlobalSection(dmCoarse, &gsc);
8188:     PetscSectionGetConstrainedStorageSize(gsc, &n);

8190:     MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);
8191:     MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);
8192:     MatSetType(*mass, dmCoarse->mattype);
8193:     DMGetApplicationContext(dmFine, &ctx);

8195:     DMGetCoarseDM(dmFine, &cdm);
8196:     DMPlexGetRegularRefinement(dmFine, &regular);
8197:     if (regular && cdm == dmCoarse) {DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);}
8198:     else                            {DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);}
8199:   }
8200:   MatViewFromOptions(*mass, NULL, "-mass_mat_view");
8201:   return(0);
8202: }

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

8207:   Input Parameter:
8208: . dm - The DMPlex object

8210:   Output Parameter:
8211: . regular - The flag

8213:   Level: intermediate

8215: .seealso: DMPlexSetRegularRefinement()
8216: @*/
8217: PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
8218: {
8222:   *regular = ((DM_Plex *) dm->data)->regularRefinement;
8223:   return(0);
8224: }

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

8229:   Input Parameters:
8230: + dm - The DMPlex object
8231: - regular - The flag

8233:   Level: intermediate

8235: .seealso: DMPlexGetRegularRefinement()
8236: @*/
8237: PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
8238: {
8241:   ((DM_Plex *) dm->data)->regularRefinement = regular;
8242:   return(0);
8243: }

8245: /*@
8246:   DMPlexGetCellRefinerType - Get the strategy for refining a cell

8248:   Input Parameter:
8249: . dm - The DMPlex object

8251:   Output Parameter:
8252: . cr - The strategy number

8254:   Level: intermediate

8256: .seealso: DMPlexSetCellRefinerType(), DMPlexSetRegularRefinement()
8257: @*/
8258: PetscErrorCode DMPlexGetCellRefinerType(DM dm, DMPlexCellRefinerType *cr)
8259: {
8263:   *cr = ((DM_Plex *) dm->data)->cellRefiner;
8264:   return(0);
8265: }

8267: /*@
8268:   DMPlexSetCellRefinerType - Set the strategy for refining a cell

8270:   Input Parameters:
8271: + dm - The DMPlex object
8272: - cr - The strategy number

8274:   Level: intermediate

8276: .seealso: DMPlexGetCellRefinerType(), DMPlexGetRegularRefinement()
8277: @*/
8278: PetscErrorCode DMPlexSetCellRefinerType(DM dm, DMPlexCellRefinerType cr)
8279: {
8282:   ((DM_Plex *) dm->data)->cellRefiner = cr;
8283:   return(0);
8284: }

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

8291:   not collective

8293:   Input Parameters:
8294: . dm - The DMPlex object

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


8301:   Level: intermediate

8303: .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
8304: @*/
8305: PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
8306: {
8307:   DM_Plex *plex = (DM_Plex *)dm->data;

8312:   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {(*plex->createanchors)(dm);}
8313:   if (anchorSection) *anchorSection = plex->anchorSection;
8314:   if (anchorIS) *anchorIS = plex->anchorIS;
8315:   return(0);
8316: }

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

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

8326:   collective on dm

8328:   Input Parameters:
8329: + dm - The DMPlex object
8330: . 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).
8331: - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).

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

8335:   Level: intermediate

8337: .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
8338: @*/
8339: PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
8340: {
8341:   DM_Plex        *plex = (DM_Plex *)dm->data;
8342:   PetscMPIInt    result;

8347:   if (anchorSection) {
8349:     MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);
8350:     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
8351:   }
8352:   if (anchorIS) {
8354:     MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);
8355:     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
8356:   }

8358:   PetscObjectReference((PetscObject)anchorSection);
8359:   PetscSectionDestroy(&plex->anchorSection);
8360:   plex->anchorSection = anchorSection;

8362:   PetscObjectReference((PetscObject)anchorIS);
8363:   ISDestroy(&plex->anchorIS);
8364:   plex->anchorIS = anchorIS;

8366:   if (PetscUnlikelyDebug(anchorIS && anchorSection)) {
8367:     PetscInt size, a, pStart, pEnd;
8368:     const PetscInt *anchors;

8370:     PetscSectionGetChart(anchorSection,&pStart,&pEnd);
8371:     ISGetLocalSize(anchorIS,&size);
8372:     ISGetIndices(anchorIS,&anchors);
8373:     for (a = 0; a < size; a++) {
8374:       PetscInt p;

8376:       p = anchors[a];
8377:       if (p >= pStart && p < pEnd) {
8378:         PetscInt dof;

8380:         PetscSectionGetDof(anchorSection,p,&dof);
8381:         if (dof) {
8382:           PetscErrorCode ierr2;

8384:           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
8385:           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
8386:         }
8387:       }
8388:     }
8389:     ISRestoreIndices(anchorIS,&anchors);
8390:   }
8391:   /* reset the generic constraints */
8392:   DMSetDefaultConstraints(dm,NULL,NULL);
8393:   return(0);
8394: }

8396: static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
8397: {
8398:   PetscSection anchorSection;
8399:   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;

8404:   DMPlexGetAnchors(dm,&anchorSection,NULL);
8405:   PetscSectionCreate(PETSC_COMM_SELF,cSec);
8406:   PetscSectionGetNumFields(section,&numFields);
8407:   if (numFields) {
8408:     PetscInt f;
8409:     PetscSectionSetNumFields(*cSec,numFields);

8411:     for (f = 0; f < numFields; f++) {
8412:       PetscInt numComp;

8414:       PetscSectionGetFieldComponents(section,f,&numComp);
8415:       PetscSectionSetFieldComponents(*cSec,f,numComp);
8416:     }
8417:   }
8418:   PetscSectionGetChart(anchorSection,&pStart,&pEnd);
8419:   PetscSectionGetChart(section,&sStart,&sEnd);
8420:   pStart = PetscMax(pStart,sStart);
8421:   pEnd   = PetscMin(pEnd,sEnd);
8422:   pEnd   = PetscMax(pStart,pEnd);
8423:   PetscSectionSetChart(*cSec,pStart,pEnd);
8424:   for (p = pStart; p < pEnd; p++) {
8425:     PetscSectionGetDof(anchorSection,p,&dof);
8426:     if (dof) {
8427:       PetscSectionGetDof(section,p,&dof);
8428:       PetscSectionSetDof(*cSec,p,dof);
8429:       for (f = 0; f < numFields; f++) {
8430:         PetscSectionGetFieldDof(section,p,f,&dof);
8431:         PetscSectionSetFieldDof(*cSec,p,f,dof);
8432:       }
8433:     }
8434:   }
8435:   PetscSectionSetUp(*cSec);
8436:   return(0);
8437: }

8439: static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
8440: {
8441:   PetscSection   aSec;
8442:   PetscInt       pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
8443:   const PetscInt *anchors;
8444:   PetscInt       numFields, f;
8445:   IS             aIS;
8447:   MatType        mtype;
8448:   PetscBool      iscuda,iskokkos;

8452:   PetscSectionGetStorageSize(cSec, &m);
8453:   PetscSectionGetStorageSize(section, &n);
8454:   MatCreate(PETSC_COMM_SELF,cMat);
8455:   MatSetSizes(*cMat,m,n,m,n);
8456:   PetscStrcmp(dm->mattype,MATSEQAIJCUSPARSE,&iscuda);
8457:   if (!iscuda) { PetscStrcmp(dm->mattype,MATMPIAIJCUSPARSE,&iscuda); }
8458:   PetscStrcmp(dm->mattype,MATSEQAIJKOKKOS,&iskokkos);
8459:   if (!iskokkos) { PetscStrcmp(dm->mattype,MATMPIAIJKOKKOS,&iskokkos); }
8460:   if (iscuda) mtype = MATSEQAIJCUSPARSE;
8461:   else if (iskokkos) mtype = MATSEQAIJKOKKOS;
8462:   else mtype = MATSEQAIJ;
8463:   MatSetType(*cMat,mtype);
8464:   DMPlexGetAnchors(dm,&aSec,&aIS);
8465:   ISGetIndices(aIS,&anchors);
8466:   /* cSec will be a subset of aSec and section */
8467:   PetscSectionGetChart(cSec,&pStart,&pEnd);
8468:   PetscMalloc1(m+1,&i);
8469:   i[0] = 0;
8470:   PetscSectionGetNumFields(section,&numFields);
8471:   for (p = pStart; p < pEnd; p++) {
8472:     PetscInt rDof, rOff, r;

8474:     PetscSectionGetDof(aSec,p,&rDof);
8475:     if (!rDof) continue;
8476:     PetscSectionGetOffset(aSec,p,&rOff);
8477:     if (numFields) {
8478:       for (f = 0; f < numFields; f++) {
8479:         annz = 0;
8480:         for (r = 0; r < rDof; r++) {
8481:           a = anchors[rOff + r];
8482:           PetscSectionGetFieldDof(section,a,f,&aDof);
8483:           annz += aDof;
8484:         }
8485:         PetscSectionGetFieldDof(cSec,p,f,&dof);
8486:         PetscSectionGetFieldOffset(cSec,p,f,&off);
8487:         for (q = 0; q < dof; q++) {
8488:           i[off + q + 1] = i[off + q] + annz;
8489:         }
8490:       }
8491:     }
8492:     else {
8493:       annz = 0;
8494:       for (q = 0; q < dof; q++) {
8495:         a = anchors[off + q];
8496:         PetscSectionGetDof(section,a,&aDof);
8497:         annz += aDof;
8498:       }
8499:       PetscSectionGetDof(cSec,p,&dof);
8500:       PetscSectionGetOffset(cSec,p,&off);
8501:       for (q = 0; q < dof; q++) {
8502:         i[off + q + 1] = i[off + q] + annz;
8503:       }
8504:     }
8505:   }
8506:   nnz = i[m];
8507:   PetscMalloc1(nnz,&j);
8508:   offset = 0;
8509:   for (p = pStart; p < pEnd; p++) {
8510:     if (numFields) {
8511:       for (f = 0; f < numFields; f++) {
8512:         PetscSectionGetFieldDof(cSec,p,f,&dof);
8513:         for (q = 0; q < dof; q++) {
8514:           PetscInt rDof, rOff, r;
8515:           PetscSectionGetDof(aSec,p,&rDof);
8516:           PetscSectionGetOffset(aSec,p,&rOff);
8517:           for (r = 0; r < rDof; r++) {
8518:             PetscInt s;

8520:             a = anchors[rOff + r];
8521:             PetscSectionGetFieldDof(section,a,f,&aDof);
8522:             PetscSectionGetFieldOffset(section,a,f,&aOff);
8523:             for (s = 0; s < aDof; s++) {
8524:               j[offset++] = aOff + s;
8525:             }
8526:           }
8527:         }
8528:       }
8529:     }
8530:     else {
8531:       PetscSectionGetDof(cSec,p,&dof);
8532:       for (q = 0; q < dof; q++) {
8533:         PetscInt rDof, rOff, r;
8534:         PetscSectionGetDof(aSec,p,&rDof);
8535:         PetscSectionGetOffset(aSec,p,&rOff);
8536:         for (r = 0; r < rDof; r++) {
8537:           PetscInt s;

8539:           a = anchors[rOff + r];
8540:           PetscSectionGetDof(section,a,&aDof);
8541:           PetscSectionGetOffset(section,a,&aOff);
8542:           for (s = 0; s < aDof; s++) {
8543:             j[offset++] = aOff + s;
8544:           }
8545:         }
8546:       }
8547:     }
8548:   }
8549:   MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);
8550:   PetscFree(i);
8551:   PetscFree(j);
8552:   ISRestoreIndices(aIS,&anchors);
8553:   return(0);
8554: }

8556: PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
8557: {
8558:   DM_Plex        *plex = (DM_Plex *)dm->data;
8559:   PetscSection   anchorSection, section, cSec;
8560:   Mat            cMat;

8565:   DMPlexGetAnchors(dm,&anchorSection,NULL);
8566:   if (anchorSection) {
8567:     PetscInt Nf;

8569:     DMGetLocalSection(dm,&section);
8570:     DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);
8571:     DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);
8572:     DMGetNumFields(dm,&Nf);
8573:     if (Nf && plex->computeanchormatrix) {(*plex->computeanchormatrix)(dm,section,cSec,cMat);}
8574:     DMSetDefaultConstraints(dm,cSec,cMat);
8575:     PetscSectionDestroy(&cSec);
8576:     MatDestroy(&cMat);
8577:   }
8578:   return(0);
8579: }

8581: PetscErrorCode DMCreateSubDomainDM_Plex(DM dm, DMLabel label, PetscInt value, IS *is, DM *subdm)
8582: {
8583:   IS             subis;
8584:   PetscSection   section, subsection;

8588:   DMGetLocalSection(dm, &section);
8589:   if (!section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting subdomain");
8590:   if (!subdm)   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set output subDM for splitting subdomain");
8591:   /* Create subdomain */
8592:   DMPlexFilter(dm, label, value, subdm);
8593:   /* Create submodel */
8594:   DMPlexGetSubpointIS(*subdm, &subis);
8595:   PetscSectionCreateSubmeshSection(section, subis, &subsection);
8596:   DMSetLocalSection(*subdm, subsection);
8597:   PetscSectionDestroy(&subsection);
8598:   DMCopyDisc(dm, *subdm);
8599:   /* Create map from submodel to global model */
8600:   if (is) {
8601:     PetscSection    sectionGlobal, subsectionGlobal;
8602:     IS              spIS;
8603:     const PetscInt *spmap;
8604:     PetscInt       *subIndices;
8605:     PetscInt        subSize = 0, subOff = 0, pStart, pEnd, p;
8606:     PetscInt        Nf, f, bs = -1, bsLocal[2], bsMinMax[2];

8608:     DMPlexGetSubpointIS(*subdm, &spIS);
8609:     ISGetIndices(spIS, &spmap);
8610:     PetscSectionGetNumFields(section, &Nf);
8611:     DMGetGlobalSection(dm, &sectionGlobal);
8612:     DMGetGlobalSection(*subdm, &subsectionGlobal);
8613:     PetscSectionGetChart(subsection, &pStart, &pEnd);
8614:     for (p = pStart; p < pEnd; ++p) {
8615:       PetscInt gdof, pSubSize  = 0;

8617:       PetscSectionGetDof(sectionGlobal, p, &gdof);
8618:       if (gdof > 0) {
8619:         for (f = 0; f < Nf; ++f) {
8620:           PetscInt fdof, fcdof;

8622:           PetscSectionGetFieldDof(subsection, p, f, &fdof);
8623:           PetscSectionGetFieldConstraintDof(subsection, p, f, &fcdof);
8624:           pSubSize += fdof-fcdof;
8625:         }
8626:         subSize += pSubSize;
8627:         if (pSubSize) {
8628:           if (bs < 0) {
8629:             bs = pSubSize;
8630:           } else if (bs != pSubSize) {
8631:             /* Layout does not admit a pointwise block size */
8632:             bs = 1;
8633:           }
8634:         }
8635:       }
8636:     }
8637:     /* Must have same blocksize on all procs (some might have no points) */
8638:     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
8639:     PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);
8640:     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
8641:     else                            {bs = bsMinMax[0];}
8642:     PetscMalloc1(subSize, &subIndices);
8643:     for (p = pStart; p < pEnd; ++p) {
8644:       PetscInt gdof, goff;

8646:       PetscSectionGetDof(subsectionGlobal, p, &gdof);
8647:       if (gdof > 0) {
8648:         const PetscInt point = spmap[p];

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

8654:           /* Can get rid of this loop by storing field information in the global section */
8655:           for (f2 = 0; f2 < f; ++f2) {
8656:             PetscSectionGetFieldDof(section, p, f2, &fdof);
8657:             PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);
8658:             poff += fdof-fcdof;
8659:           }
8660:           PetscSectionGetFieldDof(section, p, f, &fdof);
8661:           PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);
8662:           for (fc = 0; fc < fdof-fcdof; ++fc, ++subOff) {
8663:             subIndices[subOff] = goff+poff+fc;
8664:           }
8665:         }
8666:       }
8667:     }
8668:     ISRestoreIndices(spIS, &spmap);
8669:     ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);
8670:     if (bs > 1) {
8671:       /* We need to check that the block size does not come from non-contiguous fields */
8672:       PetscInt i, j, set = 1;
8673:       for (i = 0; i < subSize; i += bs) {
8674:         for (j = 0; j < bs; ++j) {
8675:           if (subIndices[i+j] != subIndices[i]+j) {set = 0; break;}
8676:         }
8677:       }
8678:       if (set) {ISSetBlockSize(*is, bs);}
8679:     }
8680:     /* Attach nullspace */
8681:     for (f = 0; f < Nf; ++f) {
8682:       (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[f];
8683:       if ((*subdm)->nullspaceConstructors[f]) break;
8684:     }
8685:     if (f < Nf) {
8686:       MatNullSpace nullSpace;

8688:       (*(*subdm)->nullspaceConstructors[f])(*subdm, f, f, &nullSpace);
8689:       PetscObjectCompose((PetscObject) *is, "nullspace", (PetscObject) nullSpace);
8690:       MatNullSpaceDestroy(&nullSpace);
8691:     }
8692:   }
8693:   return(0);
8694: }

8696: /*@
8697:   DMPlexMonitorThroughput - Report the cell throughput of FE integration

8699:   Input Parameter:
8700: - dm - The DM

8702:   Level: developer

8704:   Options Database Keys:
8705: . -dm_plex_monitor_throughput - Activate the monitor

8707: .seealso: DMSetFromOptions(), DMPlexCreate()
8708: @*/
8709: PetscErrorCode DMPlexMonitorThroughput(DM dm, void *dummy)
8710: {
8711: #if defined(PETSC_USE_LOG)
8712:   PetscStageLog      stageLog;
8713:   PetscLogEvent      event;
8714:   PetscLogStage      stage;
8715:   PetscEventPerfInfo eventInfo;
8716:   PetscReal          cellRate, flopRate;
8717:   PetscInt           cStart, cEnd, Nf, N;
8718:   const char        *name;
8719:   PetscErrorCode     ierr;
8720: #endif

8724: #if defined(PETSC_USE_LOG)
8725:   PetscObjectGetName((PetscObject) dm, &name);
8726:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
8727:   DMGetNumFields(dm, &Nf);
8728:   PetscLogGetStageLog(&stageLog);
8729:   PetscStageLogGetCurrent(stageLog, &stage);
8730:   PetscLogEventGetId("DMPlexResidualFE", &event);
8731:   PetscLogEventGetPerfInfo(stage, event, &eventInfo);
8732:   N        = (cEnd - cStart)*Nf*eventInfo.count;
8733:   flopRate = eventInfo.flops/eventInfo.time;
8734:   cellRate = N/eventInfo.time;
8735:   PetscPrintf(PetscObjectComm((PetscObject) dm), "DM (%s) FE Residual Integration: %D integrals %D reps\n  Cell rate: %.2g/s flop rate: %.2g MF/s\n", name ? name : "unknown", N, eventInfo.count, (double) cellRate, (double) (flopRate/1.e6));
8736: #else
8737:   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Plex Throughput Monitor is not supported if logging is turned off. Reconfigure using --with-log.");
8738: #endif
8739:   return(0);
8740: }