Actual source code: plex.c

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

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

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

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

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

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

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

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

1520:   Not collective

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

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

1528:   Level: developer

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

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

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

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

1555:   Not collective

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

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

1564:   Level: beginner

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

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

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

1582:   Not collective

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

1589:   Output Parameters:

1591:   Level: beginner

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

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

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

1610:   Not collective

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

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

1619:   Level: beginner

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

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

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

1638:   Not collective

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

1645:   Output Parameter:

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

1650:   Level: beginner

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

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

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

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

1670:   Not collective

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

1677:   Output Parameter:

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

1682:   Level: beginner

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

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

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

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

1704:   Not collective

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

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

1713:   Level: beginner

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

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

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

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

1740:   Not collective

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

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

1750:   Level: intermediate

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

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

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

1777:   Not collective

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

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

1786:   Level: advanced

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

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

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

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

1814:   Not collective

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

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

1825:   Level: advanced

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

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

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

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

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

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

1907:   Not collective

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

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

1918:   Level: advanced

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

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

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

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

1948:   Not collective

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

1955:   Output Parameter:

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

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

1962:   Level: beginner

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

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

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

1990:   Not collective

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

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

2002:   Level: beginner

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

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

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

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

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

2034:   Not collective

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

2044:   Output Parameter:

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

2049:   Level: beginner

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

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

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

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

2080:   Not collective

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

2088:   Level: beginner

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

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

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

2114:   Not collective

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

2122:   Level: beginner

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

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

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

2147:   Not collective

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

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

2156:   Level: beginner

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

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

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

2175:   Not collective

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

2182:   Output Parameter:

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

2187:   Level: beginner

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

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

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

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

2207:   Not collective

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

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

2216:   Level: beginner

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

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

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

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

2243:   Not collective

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

2250:   Output Parameter:

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

2255:   Level: beginner

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

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

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

2283:   Not collective

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

2291:   Level: beginner

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

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

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

2317:   Not collective

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

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

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

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

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

2338:   Level: beginner

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

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

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

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

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

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

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

2460:   Not collective

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

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

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

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

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

2482:   Level: beginner

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

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

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

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

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

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

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

2612:   Not collective

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

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

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

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

2630:   Level: beginner

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

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

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

2650:   Not collective

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

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

2659:   Level: beginner

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

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

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

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

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

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

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

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

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

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

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

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

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

2758:   Not collective

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

2763:   Output Parameter:

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

2768:   Level: beginner

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

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

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

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

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

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

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

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

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

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

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

2856:   Collective on dm

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

2861:   Output Parameter:

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

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

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

2883:   Level: beginner

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3068:   Collective on dm

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

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

3075:   Level: developer

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

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

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

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

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

3113:   Not Collective

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

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

3124:   Level: intermediate

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

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

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

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

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

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

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

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

3185:   Not Collective

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

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

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

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

3202:   Level: intermediate

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

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

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

3223:   Not Collective

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

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

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

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

3240:   Level: intermediate

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


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

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

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

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

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

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

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

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

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

3329:   Not Collective

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

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

3340:   Level: intermediate

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

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

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

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

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

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

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

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

3401:   Not Collective

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

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

3412:   Level: intermediate

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

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

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

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

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

3439:   Not Collective

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

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

3450:   Level: intermediate

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

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

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


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

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

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

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

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

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

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

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

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

3545:   Not Collective

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

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

3554:   Level: intermediate

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

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


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

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

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

3609:   Not Collective

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

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

3619:   Level: developer

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

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

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

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

3698:   Not Collective

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

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

3706:   Level: developer

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

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

3722:   Not Collective

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

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

3730:   Level: developer

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

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

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

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

3757:   Not Collective

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

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

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

3772:   Level: developer

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

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

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

3802:   Not Collective

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

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

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

3817:   Level: developer

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

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

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

3848:   Not Collective

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

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

3857:   Level: intermediate

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

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

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

3875:   Not Collective

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

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

3884:   Level: intermediate

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

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

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

3905:   Not Collective

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

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

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

3916:   Level: developer

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

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

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

3935:   Not Collective

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

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

3944:   Level: intermediate

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

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

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

3966:   Not Collective

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

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

3978:   Level: advanced

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

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

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

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

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

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

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

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

4038:   Not Collective

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

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

4046:   Level: developer

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

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

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

4063:   Not Collective

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

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

4071:   Level: developer

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

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

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

4088:   Not Collective

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

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

4096:   Level: developer

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

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

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

4113:   Not Collective

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

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

4121:   Level: developer

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

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

4135: /******************************** FEM Support **********************************/

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

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

4160: /*@

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

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

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

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

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

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

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

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

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

4213:   Level: developer

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

4773:   Not collective

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

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

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

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

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

4818:   Level: intermediate

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

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

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

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

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

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

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

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

4953:   Not collective

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

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

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

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

4971:   Level: intermediate

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

5297:   Not collective

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

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

5311:   Level: intermediate

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

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

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

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

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

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

5492: /* Unlike DMPlexVecSetClosure(), this uses plex-native closure permutation, not a user-specified permutation such as DMPlexSetClosurePermutationTensor(). */
5493: 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)
5494: {
5495:   PetscSection      clSection;
5496:   IS                clPoints;
5497:   PetscScalar       *array;
5498:   PetscInt          *points = NULL;
5499:   const PetscInt    *clp;
5500:   PetscInt          numFields, numPoints, p;
5501:   PetscInt          offset = 0, f;
5502:   PetscErrorCode    ierr;

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

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

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

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

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

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

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

5624:   Level: developer

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

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

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

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

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

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

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

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

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

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

5697:  Example:
5698:  Suppose a point contains one field with three components, and for which the unconstrained indices are {10, 11, 12}.
5699:  When the middle component is constrained, we get the array {10, -12, 12} for (islocal=TRUE, setBC=FALSE).
5700:  Note that -12 is the involution of 11, so the user can involute negative indices to recover local indices.
5701:  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.

5703:  Level: developer
5704: */
5705: 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[])
5706: {
5707:   PetscInt       numFields, foff, f;

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

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

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

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

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

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

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

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

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

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

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

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

5821:   PetscSectionGetNumFields(section, &numFields);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

6326:   Not collective

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

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

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

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

6350:   Level: advanced

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

6523:   Not collective

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

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

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

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

6547:   Level: advanced

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

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

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

6566:   Not collective

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

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

6580:   Level: intermediate

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

6594:   if (!section) {DMGetLocalSection(dm, &section);}
6596:   if (!globalSection) {DMGetGlobalSection(dm, &globalSection);}

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

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

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

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

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

6630:   Not collective

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

6644:   Level: intermediate

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

6658:   if (!sectionRow) {DMGetLocalSection(dmRow, &sectionRow);}
6660:   if (!globalSectionRow) {DMGetGlobalSection(dmRow, &globalSectionRow);}
6663:   if (!sectionCol) {DMGetLocalSection(dmCol, &sectionCol);}
6665:   if (!globalSectionCol) {DMGetGlobalSection(dmCol, &globalSectionCol);}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

7003:   Level: developer

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

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

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

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

7025:   Level: developer

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

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

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

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

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

7049:   Level: advanced

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

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

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

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

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

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

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

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

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

7120:   Level: developer

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

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

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

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

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

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

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

7157:   Level: developer

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

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

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

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

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

7182:   Level: developer

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

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

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

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


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

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

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

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

7235:   Level: intermediate

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

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

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

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

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

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

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

7291:   Level: intermediate

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

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

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

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

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

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

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

7342:   Level: developer

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

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

7426: /*
7427:   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.
7428: */
7429: static PetscErrorCode DMPlexCellUnsplitVertices_Private(DM dm, PetscInt c, DMPolytopeType ct, PetscInt *unsplit)
7430: {
7431:   DMPolytopeType  cct;
7432:   PetscInt        ptpoints[4];
7433:   const PetscInt *cone, *ccone, *ptcone;
7434:   PetscInt        coneSize, cp, cconeSize, ccp, npt = 0, pt;
7435:   PetscErrorCode  ierr;

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

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

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

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

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

7487:   Level: developer

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

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

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

7524:       DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);
7525:       if (Nv + unsplit == DMPolytopeTypeGetNumVertices(ct)) continue;
7526:     }
7527:     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));
7528:   }
7529:   return(0);
7530: }

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

7535:   Not Collective

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

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

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

7549:   Level: developer

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

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

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

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

7584:       DMPlexGetCellType(dm, c, &ct);
7585:       DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);
7586:       if (unsplit) continue;
7587:       DMPlexGetConeSize(dm, c, &coneSize);
7588:       DMPlexGetCone(dm, c, &cone);
7589:       DMPlexGetConeOrientation(dm, c, &ornt);
7590:       DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
7591:       for (cl = 0; cl < closureSize*2; cl += 2) {
7592:         const PetscInt p = closure[cl];
7593:         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
7594:       }
7595:       DMPlexGetRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);
7596:       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);
7597:       for (f = 0; f < numFaces; ++f) {
7598:         DMPolytopeType fct;
7599:         PetscInt       *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;

7601:         DMPlexGetCellType(dm, cone[f], &fct);
7602:         DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);
7603:         for (cl = 0; cl < fclosureSize*2; cl += 2) {
7604:           const PetscInt p = fclosure[cl];
7605:           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
7606:         }
7607:         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]);
7608:         for (v = 0; v < fnumCorners; ++v) {
7609:           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]);
7610:         }
7611:         DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);
7612:         fOff += faceSizes[f];
7613:       }
7614:       DMPlexRestoreRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);
7615:       DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
7616:     }
7617:   }
7618:   return(0);
7619: }

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

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

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

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

7632:   Level: developer

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

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

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

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

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

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

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

7698:   Level: developer

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

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

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

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

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

7741:     DMPlexGetConeSize(dm, point, &coneSize);
7742:     DMPlexGetCone(dm, point, &cone);
7743:     for (c = 0; c < coneSize; ++c) {
7744:       if (!rootdegree[cone[c]]) {
7745:         PetscFindInt(cone[c], nleaves, locals, &idx);
7746:         if (idx < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point SF contains %D but not %D from its cone", point, cone[c]);
7747:       }
7748:     }
7749:   }
7750:   return(0);
7751: }

7753: typedef struct cell_stats
7754: {
7755:   PetscReal min, max, sum, squaresum;
7756:   PetscInt  count;
7757: } cell_stats_t;

7759: static void MPIAPI cell_stats_reduce(void *a, void *b, int * len, MPI_Datatype *datatype)
7760: {
7761:   PetscInt i, N = *len;

7763:   for (i = 0; i < N; i++) {
7764:     cell_stats_t *A = (cell_stats_t *) a;
7765:     cell_stats_t *B = (cell_stats_t *) b;

7767:     B->min = PetscMin(A->min,B->min);
7768:     B->max = PetscMax(A->max,B->max);
7769:     B->sum += A->sum;
7770:     B->squaresum += A->squaresum;
7771:     B->count += A->count;
7772:   }
7773: }

7775: /*@
7776:   DMPlexCheckCellShape - Checks the Jacobian of the mapping from reference to real cells and computes some minimal statistics.

7778:   Collective on dm

7780:   Input Parameters:
7781: + dm        - The DMPlex object
7782: . output    - If true, statistics will be displayed on stdout
7783: - condLimit - Display all cells above this condition number, or PETSC_DETERMINE for no cell output

7785:   Notes:
7786:   This is mainly intended for debugging/testing purposes.

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

7790:   Level: developer

7792: .seealso: DMSetFromOptions(), DMPlexComputeOrthogonalQuality()
7793: @*/
7794: PetscErrorCode DMPlexCheckCellShape(DM dm, PetscBool output, PetscReal condLimit)
7795: {
7796:   DM             dmCoarse;
7797:   cell_stats_t   stats, globalStats;
7798:   MPI_Comm       comm = PetscObjectComm((PetscObject)dm);
7799:   PetscReal      *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
7800:   PetscReal      limit = condLimit > 0 ? condLimit : PETSC_MAX_REAL;
7801:   PetscInt       cdim, cStart, cEnd, c, eStart, eEnd, count = 0;
7802:   PetscMPIInt    rank,size;

7807:   stats.min   = PETSC_MAX_REAL;
7808:   stats.max   = PETSC_MIN_REAL;
7809:   stats.sum   = stats.squaresum = 0.;
7810:   stats.count = 0;

7812:   MPI_Comm_size(comm, &size);
7813:   MPI_Comm_rank(comm, &rank);
7814:   DMGetCoordinateDim(dm,&cdim);
7815:   PetscMalloc2(PetscSqr(cdim), &J, PetscSqr(cdim), &invJ);
7816:   DMPlexGetSimplexOrBoxCells(dm,0,&cStart,&cEnd);
7817:   DMPlexGetDepthStratum(dm,1,&eStart,&eEnd);
7818:   for (c = cStart; c < cEnd; c++) {
7819:     PetscInt  i;
7820:     PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;

7822:     DMPlexComputeCellGeometryAffineFEM(dm,c,NULL,J,invJ,&detJ);
7823:     if (detJ < 0.0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted", c);
7824:     for (i = 0; i < PetscSqr(cdim); ++i) {
7825:       frobJ    += J[i] * J[i];
7826:       frobInvJ += invJ[i] * invJ[i];
7827:     }
7828:     cond2 = frobJ * frobInvJ;
7829:     cond  = PetscSqrtReal(cond2);

7831:     stats.min        = PetscMin(stats.min,cond);
7832:     stats.max        = PetscMax(stats.max,cond);
7833:     stats.sum       += cond;
7834:     stats.squaresum += cond2;
7835:     stats.count++;
7836:     if (output && cond > limit) {
7837:       PetscSection coordSection;
7838:       Vec          coordsLocal;
7839:       PetscScalar *coords = NULL;
7840:       PetscInt     Nv, d, clSize, cl, *closure = NULL;

7842:       DMGetCoordinatesLocal(dm, &coordsLocal);
7843:       DMGetCoordinateSection(dm, &coordSection);
7844:       DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);
7845:       PetscSynchronizedPrintf(comm, "[%d] Cell %D cond %g\n", rank, c, (double) cond);
7846:       for (i = 0; i < Nv/cdim; ++i) {
7847:         PetscSynchronizedPrintf(comm, "  Vertex %D: (", i);
7848:         for (d = 0; d < cdim; ++d) {
7849:           if (d > 0) {PetscSynchronizedPrintf(comm, ", ");}
7850:           PetscSynchronizedPrintf(comm, "%g", (double) PetscRealPart(coords[i*cdim+d]));
7851:         }
7852:         PetscSynchronizedPrintf(comm, ")\n");
7853:       }
7854:       DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);
7855:       for (cl = 0; cl < clSize*2; cl += 2) {
7856:         const PetscInt edge = closure[cl];

7858:         if ((edge >= eStart) && (edge < eEnd)) {
7859:           PetscReal len;

7861:           DMPlexComputeCellGeometryFVM(dm, edge, &len, NULL, NULL);
7862:           PetscSynchronizedPrintf(comm, "  Edge %D: length %g\n", edge, (double) len);
7863:         }
7864:       }
7865:       DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);
7866:       DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);
7867:     }
7868:   }
7869:   if (output) {PetscSynchronizedFlush(comm, NULL);}

7871:   if (size > 1) {
7872:     PetscMPIInt   blockLengths[2] = {4,1};
7873:     MPI_Aint      blockOffsets[2] = {offsetof(cell_stats_t,min),offsetof(cell_stats_t,count)};
7874:     MPI_Datatype  blockTypes[2]   = {MPIU_REAL,MPIU_INT}, statType;
7875:     MPI_Op        statReduce;

7877:     MPI_Type_create_struct(2,blockLengths,blockOffsets,blockTypes,&statType);
7878:     MPI_Type_commit(&statType);
7879:     MPI_Op_create(cell_stats_reduce, PETSC_TRUE, &statReduce);
7880:     MPI_Reduce(&stats,&globalStats,1,statType,statReduce,0,comm);
7881:     MPI_Op_free(&statReduce);
7882:     MPI_Type_free(&statType);
7883:   } else {
7884:     PetscArraycpy(&globalStats,&stats,1);
7885:   }
7886:   if (!rank) {
7887:     count = globalStats.count;
7888:     min   = globalStats.min;
7889:     max   = globalStats.max;
7890:     mean  = globalStats.sum / globalStats.count;
7891:     stdev = globalStats.count > 1 ? PetscSqrtReal(PetscMax((globalStats.squaresum - globalStats.count * mean * mean) / (globalStats.count - 1),0)) : 0.0;
7892:   }

7894:   if (output) {
7895:     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);
7896:   }
7897:   PetscFree2(J,invJ);

7899:   DMGetCoarseDM(dm,&dmCoarse);
7900:   if (dmCoarse) {
7901:     PetscBool isplex;

7903:     PetscObjectTypeCompare((PetscObject)dmCoarse,DMPLEX,&isplex);
7904:     if (isplex) {
7905:       DMPlexCheckCellShape(dmCoarse,output,condLimit);
7906:     }
7907:   }
7908:   return(0);
7909: }

7911: /*@
7912:   DMPlexComputeOrthogonalQuality - Compute cell-wise orthogonal quality mesh statistic. Optionally tags all cells with
7913:   orthogonal quality below given tolerance.

7915:   Collective

7917:   Input Parameters:
7918: + dm   - The DMPlex object
7919: . fv   - Optional PetscFV object for pre-computed cell/face centroid information
7920: - atol - [0, 1] Absolute tolerance for tagging cells.

7922:   Output Parameters:
7923: + OrthQual      - Vec containing orthogonal quality per cell
7924: - OrthQualLabel - DMLabel tagging cells below atol with DM_ADAPT_REFINE

7926:   Options Database Keys:
7927: + -dm_plex_orthogonal_quality_label_view - view OrthQualLabel if label is requested. Currently only PETSCVIEWERASCII is
7928: supported.
7929: - -dm_plex_orthogonal_quality_vec_view - view OrthQual vector.

7931:   Notes:
7932:   Orthogonal quality is given by the following formula:

7934:   \min \left[ \frac{A_i \cdot f_i}{\|A_i\| \|f_i\|} , \frac{A_i \cdot c_i}{\|A_i\| \|c_i\|} \right]

7936:   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
7937:   is the vector from the current cells centroid to the centroid of its i'th neighbor (which shares a face with the
7938:   current cell). This computes the vector similarity between each cell face and its corresponding neighbor centroid by
7939:   calculating the cosine of the angle between these vectors.

7941:   Orthogonal quality ranges from 1 (best) to 0 (worst).

7943:   This routine is mainly useful for FVM, however is not restricted to only FVM. The PetscFV object is optionally used to check for
7944:   pre-computed FVM cell data, but if it is not passed in then this data will be computed.

7946:   Cells are tagged if they have an orthogonal quality less than or equal to the absolute tolerance.

7948:   Level: intermediate

7950: .seealso: DMPlexCheckCellShape(), DMCreateLabel()
7951: @*/
7952: PetscErrorCode DMPlexComputeOrthogonalQuality(DM dm, PetscFV fv, PetscReal atol, Vec *OrthQual, DMLabel *OrthQualLabel)
7953: {
7954:   PetscInt                nc, cellHeight, cStart, cEnd, cell;
7955:   const PetscScalar       *cellGeomArr, *faceGeomArr;
7956:   MPI_Comm                comm;
7957:   Vec                     cellgeom, facegeom;
7958:   DM                      dmFace, dmCell;
7959:   IS                      glob;
7960:   DMPlexInterpolatedFlag  interpFlag;
7961:   ISLocalToGlobalMapping  ltog;
7962:   PetscViewer             vwr;
7963:   PetscErrorCode          ierr;

7968:   PetscObjectGetComm((PetscObject) dm, &comm);
7969:   DMGetDimension(dm, &nc);
7970:   if (nc < 2) {
7971:     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must have dimension >= 2 (current %D)", nc);
7972:   }
7973:   DMPlexIsInterpolated(dm, &interpFlag);
7974:   if (interpFlag != DMPLEX_INTERPOLATED_FULL) {
7975:     PetscMPIInt  rank;

7977:     MPI_Comm_rank(comm, &rank);
7978:     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must be fully interpolated, DM on rank %d is not fully interpolated", rank);
7979:   }
7980:   if (OrthQualLabel) {
7982:     DMCreateLabel(dm, "Orthogonal_Quality");
7983:     DMGetLabel(dm, "Orthogonal_Quality", OrthQualLabel);
7984:   } else {
7985:     *OrthQualLabel = NULL;
7986:   }

7988:   DMPlexGetVTKCellHeight(dm, &cellHeight);
7989:   DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
7990:   DMPlexCreateCellNumbering_Internal(dm, PETSC_TRUE, &glob);
7991:   ISLocalToGlobalMappingCreateIS(glob, &ltog);
7992:   ISLocalToGlobalMappingSetType(ltog, ISLOCALTOGLOBALMAPPINGHASH);
7993:   VecCreate(comm, OrthQual);
7994:   VecSetType(*OrthQual, VECSTANDARD);
7995:   VecSetSizes(*OrthQual, cEnd-cStart, PETSC_DETERMINE);
7996:   VecSetLocalToGlobalMapping(*OrthQual, ltog);
7997:   VecSetUp(*OrthQual);
7998:   ISDestroy(&glob);
7999:   ISLocalToGlobalMappingDestroy(&ltog);
8000:   DMPlexGetDataFVM(dm, fv, &cellgeom, &facegeom, NULL);
8001:   VecGetArrayRead(cellgeom, &cellGeomArr);
8002:   VecGetArrayRead(facegeom, &faceGeomArr);
8003:   VecGetDM(cellgeom, &dmCell);
8004:   VecGetDM(facegeom, &dmFace);
8005:   for (cell = cStart; cell < cEnd; cell++) {
8006:     PetscInt           cellneigh, cellneighiter = 0, nf, adjSize = PETSC_DETERMINE, ix = cell-cStart;
8007:     const PetscInt     *cone;
8008:     PetscInt           cellarr[2], *adj = NULL;
8009:     PetscScalar        *cArr, *fArr;
8010:     PetscReal          minvalc = 1.0, minvalf = 1.0;
8011:     PetscScalar        OQ;
8012:     PetscFVCellGeom    *cg;

8014:     cellarr[0] = cell;
8015:     /* Make indexing into cellGeom easier */
8016:     DMPlexPointLocalRead(dmCell, cell, cellGeomArr, &cg);
8017:     DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &adjSize, &adj);
8018:     DMPlexGetConeSize(dm, cell, &nf);
8019:     DMPlexGetCone(dm, cell, &cone);
8020:     /* Technically 1 too big, but easier than fiddling with empty adjacency array */
8021:     PetscCalloc2(adjSize, &cArr, adjSize, &fArr);
8022:     for (cellneigh = 0; cellneigh < adjSize; cellneigh++) {
8023:       PetscInt         numcovpts, i, neigh = adj[cellneigh];
8024:       const PetscInt   *covpts;
8025:       PetscReal        normci = 0, normfi = 0, normai = 0;
8026:       PetscReal        *ci, *fi, *Ai;
8027:       PetscFVCellGeom  *cgneigh;
8028:       PetscFVFaceGeom  *fg;

8030:       /* Don't count ourselves in the neighbor list */
8031:       if (neigh == cell) continue;
8032:       PetscMalloc3(nc, &ci, nc, &fi, nc, &Ai);
8033:       DMPlexPointLocalRead(dmCell, neigh, cellGeomArr, &cgneigh);
8034:       cellarr[1] = neigh;
8035:       DMPlexGetMeet(dm, 2, cellarr, &numcovpts, &covpts);
8036:       DMPlexPointLocalRead(dmFace, covpts[0], faceGeomArr, &fg);
8037:       DMPlexRestoreMeet(dm, 2, cellarr, &numcovpts, &covpts);

8039:       /* Compute c_i, f_i and their norms */
8040:       for (i = 0; i < nc; i++) {
8041:         ci[i] = cgneigh->centroid[i] - cg->centroid[i];
8042:         fi[i] = fg->centroid[i] - cg->centroid[i];
8043:         Ai[i] = fg->normal[i];
8044:         normci += PetscPowScalar(ci[i], 2);
8045:         normfi += PetscPowScalar(fi[i], 2);
8046:         normai += PetscPowScalar(Ai[i], 2);
8047:       }
8048:       normci = PetscSqrtScalar(normci);
8049:       normfi = PetscSqrtScalar(normfi);
8050:       normai = PetscSqrtScalar(normai);

8052:       /* Normalize and compute for each face-cell-normal pair */
8053:       for (i = 0; i < nc; i++) {
8054:         ci[i] = ci[i]/normci;
8055:         fi[i] = fi[i]/normfi;
8056:         Ai[i] = Ai[i]/normai;
8057:         /* PetscAbs because I don't know if normals are guaranteed to point out */
8058:         cArr[cellneighiter] += PetscAbs(Ai[i]*ci[i]);
8059:         fArr[cellneighiter] += PetscAbs(Ai[i]*fi[i]);
8060:       }
8061:       if (PetscRealPart(cArr[cellneighiter]) < minvalc) {
8062:         minvalc = PetscRealPart(cArr[cellneighiter]);
8063:       }
8064:       if (PetscRealPart(fArr[cellneighiter]) < minvalf) {
8065:         minvalf = PetscRealPart(fArr[cellneighiter]);
8066:       }
8067:       cellneighiter++;
8068:       PetscFree3(ci, fi, Ai);
8069:     }
8070:     PetscFree(adj);
8071:     PetscFree2(cArr, fArr);
8072:     /* Defer to cell if they're equal */
8073:     OQ = PetscMin(minvalf, minvalc);
8074:     if (OrthQualLabel) {
8075:       if (PetscRealPart(OQ) <= atol) {
8076:         DMLabelSetValue(*OrthQualLabel, cell, DM_ADAPT_REFINE);
8077:       }
8078:     }
8079:     VecSetValuesLocal(*OrthQual, 1, &ix, &OQ, INSERT_VALUES);
8080:   }
8081:   VecAssemblyBegin(*OrthQual);
8082:   VecAssemblyEnd(*OrthQual);
8083:   VecRestoreArrayRead(cellgeom, &cellGeomArr);
8084:   VecRestoreArrayRead(facegeom, &faceGeomArr);
8085:   PetscOptionsGetViewer(comm, NULL, NULL, "-dm_plex_orthogonal_quality_label_view", &vwr, NULL, NULL);
8086:   if (OrthQualLabel) {
8087:     if (vwr) {
8088:       DMLabelView(*OrthQualLabel, vwr);
8089:     }
8090:   }
8091:   PetscViewerDestroy(&vwr);
8092:   VecViewFromOptions(*OrthQual, NULL, "-dm_plex_orthogonal_quality_vec_view");
8093:   return(0);
8094: }

8096: /* this is here insead of DMGetOutputDM because output DM still has constraints in the local indices that affect
8097:  * interpolator construction */
8098: static PetscErrorCode DMGetFullDM(DM dm, DM *odm)
8099: {
8100:   PetscSection   section, newSection, gsection;
8101:   PetscSF        sf;
8102:   PetscBool      hasConstraints, ghasConstraints;

8108:   DMGetLocalSection(dm, &section);
8109:   PetscSectionHasConstraints(section, &hasConstraints);
8110:   MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));
8111:   if (!ghasConstraints) {
8112:     PetscObjectReference((PetscObject)dm);
8113:     *odm = dm;
8114:     return(0);
8115:   }
8116:   DMClone(dm, odm);
8117:   DMCopyFields(dm, *odm);
8118:   DMGetLocalSection(*odm, &newSection);
8119:   DMGetPointSF(*odm, &sf);
8120:   PetscSectionCreateGlobalSection(newSection, sf, PETSC_TRUE, PETSC_FALSE, &gsection);
8121:   DMSetGlobalSection(*odm, gsection);
8122:   PetscSectionDestroy(&gsection);
8123:   return(0);
8124: }

8126: static PetscErrorCode DMCreateAffineInterpolationCorrection_Plex(DM dmc, DM dmf, Vec *shift)
8127: {
8128:   DM             dmco, dmfo;
8129:   Mat            interpo;
8130:   Vec            rscale;
8131:   Vec            cglobalo, clocal;
8132:   Vec            fglobal, fglobalo, flocal;
8133:   PetscBool      regular;

8137:   DMGetFullDM(dmc, &dmco);
8138:   DMGetFullDM(dmf, &dmfo);
8139:   DMSetCoarseDM(dmfo, dmco);
8140:   DMPlexGetRegularRefinement(dmf, &regular);
8141:   DMPlexSetRegularRefinement(dmfo, regular);
8142:   DMCreateInterpolation(dmco, dmfo, &interpo, &rscale);
8143:   DMCreateGlobalVector(dmco, &cglobalo);
8144:   DMCreateLocalVector(dmc, &clocal);
8145:   VecSet(cglobalo, 0.);
8146:   VecSet(clocal, 0.);
8147:   DMCreateGlobalVector(dmf, &fglobal);
8148:   DMCreateGlobalVector(dmfo, &fglobalo);
8149:   DMCreateLocalVector(dmf, &flocal);
8150:   VecSet(fglobal, 0.);
8151:   VecSet(fglobalo, 0.);
8152:   VecSet(flocal, 0.);
8153:   DMPlexInsertBoundaryValues(dmc, PETSC_TRUE, clocal, 0., NULL, NULL, NULL);
8154:   DMLocalToGlobalBegin(dmco, clocal, INSERT_VALUES, cglobalo);
8155:   DMLocalToGlobalEnd(dmco, clocal, INSERT_VALUES, cglobalo);
8156:   MatMult(interpo, cglobalo, fglobalo);
8157:   DMGlobalToLocalBegin(dmfo, fglobalo, INSERT_VALUES, flocal);
8158:   DMGlobalToLocalEnd(dmfo, fglobalo, INSERT_VALUES, flocal);
8159:   DMLocalToGlobalBegin(dmf, flocal, INSERT_VALUES, fglobal);
8160:   DMLocalToGlobalEnd(dmf, flocal, INSERT_VALUES, fglobal);
8161:   *shift = fglobal;
8162:   VecDestroy(&flocal);
8163:   VecDestroy(&fglobalo);
8164:   VecDestroy(&clocal);
8165:   VecDestroy(&cglobalo);
8166:   VecDestroy(&rscale);
8167:   MatDestroy(&interpo);
8168:   DMDestroy(&dmfo);
8169:   DMDestroy(&dmco);
8170:   return(0);
8171: }

8173: PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
8174: {
8175:   PetscObject    shifto;
8176:   Vec            shift;


8181:   if (!interp) {
8182:     Vec rscale;

8184:     DMCreateInterpolation(coarse, fine, &interp, &rscale);
8185:     VecDestroy(&rscale);
8186:   } else {
8187:     PetscObjectReference((PetscObject)interp);
8188:   }
8189:   PetscObjectQuery((PetscObject)interp, "_DMInterpolateSolution_Plex_Vec", &shifto);
8190:   if (!shifto) {
8191:     DMCreateAffineInterpolationCorrection_Plex(coarse, fine, &shift);
8192:     PetscObjectCompose((PetscObject)interp, "_DMInterpolateSolution_Plex_Vec", (PetscObject) shift);
8193:     shifto = (PetscObject) shift;
8194:     VecDestroy(&shift);
8195:   }
8196:   shift = (Vec) shifto;
8197:   MatInterpolate(interp, coarseSol, fineSol);
8198:   VecAXPY(fineSol, 1.0, shift);
8199:   MatDestroy(&interp);
8200:   return(0);
8201: }

8203: /* Pointwise interpolation
8204:      Just code FEM for now
8205:      u^f = I u^c
8206:      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
8207:      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
8208:      I_{ij} = psi^f_i phi^c_j
8209: */
8210: PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
8211: {
8212:   PetscSection   gsc, gsf;
8213:   PetscInt       m, n;
8214:   void          *ctx;
8215:   DM             cdm;
8216:   PetscBool      regular, ismatis, isRefined = dmCoarse->data == dmFine->data ? PETSC_FALSE : PETSC_TRUE;

8220:   DMGetGlobalSection(dmFine, &gsf);
8221:   PetscSectionGetConstrainedStorageSize(gsf, &m);
8222:   DMGetGlobalSection(dmCoarse, &gsc);
8223:   PetscSectionGetConstrainedStorageSize(gsc, &n);

8225:   PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis);
8226:   MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);
8227:   MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);
8228:   MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype);
8229:   DMGetApplicationContext(dmFine, &ctx);

8231:   DMGetCoarseDM(dmFine, &cdm);
8232:   DMPlexGetRegularRefinement(dmFine, &regular);
8233:   if (!isRefined || (regular && cdm == dmCoarse)) {DMPlexComputeInterpolatorNested(dmCoarse, dmFine, isRefined, *interpolation, ctx);}
8234:   else                                            {DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);}
8235:   MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");
8236:   if (scaling) {
8237:     /* Use naive scaling */
8238:     DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);
8239:   }
8240:   return(0);
8241: }

8243: PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
8244: {
8246:   VecScatter     ctx;

8249:   DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);
8250:   MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);
8251:   VecScatterDestroy(&ctx);
8252:   return(0);
8253: }

8255: static void g0_identity_private(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8256:                                 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8257:                                 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8258:                                 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
8259: {
8260:   g0[0] = 1.0;
8261: }

8263: PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
8264: {
8265:   PetscSection   gsc, gsf;
8266:   PetscInt       m, n;
8267:   void          *ctx;
8268:   DM             cdm;
8269:   PetscBool      regular;

8273:   if (dmFine == dmCoarse) {
8274:     DM       dmc;
8275:     PetscDS  ds;
8276:     Vec      u;
8277:     IS       cellIS;
8278:     PetscHashFormKey key;
8279:     PetscInt depth;

8281:     DMClone(dmFine, &dmc);
8282:     DMCopyDisc(dmFine, dmc);
8283:     DMGetDS(dmc, &ds);
8284:     PetscDSSetJacobian(ds, 0, 0, g0_identity_private, NULL, NULL, NULL);
8285:     DMCreateMatrix(dmc, mass);
8286:     DMGetGlobalVector(dmc, &u);
8287:     DMPlexGetDepth(dmc, &depth);
8288:     DMGetStratumIS(dmc, "depth", depth, &cellIS);
8289:     MatZeroEntries(*mass);
8290:     key.label = NULL;
8291:     key.value = 0;
8292:     key.field = 0;
8293:     DMPlexComputeJacobian_Internal(dmc, key, cellIS, 0.0, 0.0, u, NULL, *mass, *mass, NULL);
8294:     ISDestroy(&cellIS);
8295:     DMRestoreGlobalVector(dmc, &u);
8296:     DMDestroy(&dmc);
8297:   } else {
8298:     DMGetGlobalSection(dmFine, &gsf);
8299:     PetscSectionGetConstrainedStorageSize(gsf, &m);
8300:     DMGetGlobalSection(dmCoarse, &gsc);
8301:     PetscSectionGetConstrainedStorageSize(gsc, &n);

8303:     MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);
8304:     MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);
8305:     MatSetType(*mass, dmCoarse->mattype);
8306:     DMGetApplicationContext(dmFine, &ctx);

8308:     DMGetCoarseDM(dmFine, &cdm);
8309:     DMPlexGetRegularRefinement(dmFine, &regular);
8310:     if (regular && cdm == dmCoarse) {DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);}
8311:     else                            {DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);}
8312:   }
8313:   MatViewFromOptions(*mass, NULL, "-mass_mat_view");
8314:   return(0);
8315: }

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

8320:   Input Parameter:
8321: . dm - The DMPlex object

8323:   Output Parameter:
8324: . regular - The flag

8326:   Level: intermediate

8328: .seealso: DMPlexSetRegularRefinement()
8329: @*/
8330: PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
8331: {
8335:   *regular = ((DM_Plex *) dm->data)->regularRefinement;
8336:   return(0);
8337: }

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

8342:   Input Parameters:
8343: + dm - The DMPlex object
8344: - regular - The flag

8346:   Level: intermediate

8348: .seealso: DMPlexGetRegularRefinement()
8349: @*/
8350: PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
8351: {
8354:   ((DM_Plex *) dm->data)->regularRefinement = regular;
8355:   return(0);
8356: }

8358: /*@
8359:   DMPlexGetCellRefinerType - Get the strategy for refining a cell

8361:   Input Parameter:
8362: . dm - The DMPlex object

8364:   Output Parameter:
8365: . cr - The strategy number

8367:   Level: intermediate

8369: .seealso: DMPlexSetCellRefinerType(), DMPlexSetRegularRefinement()
8370: @*/
8371: PetscErrorCode DMPlexGetCellRefinerType(DM dm, DMPlexCellRefinerType *cr)
8372: {
8376:   *cr = ((DM_Plex *) dm->data)->cellRefiner;
8377:   return(0);
8378: }

8380: /*@
8381:   DMPlexSetCellRefinerType - Set the strategy for refining a cell

8383:   Input Parameters:
8384: + dm - The DMPlex object
8385: - cr - The strategy number

8387:   Level: intermediate

8389: .seealso: DMPlexGetCellRefinerType(), DMPlexGetRegularRefinement()
8390: @*/
8391: PetscErrorCode DMPlexSetCellRefinerType(DM dm, DMPlexCellRefinerType cr)
8392: {
8395:   ((DM_Plex *) dm->data)->cellRefiner = cr;
8396:   return(0);
8397: }

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

8404:   not collective

8406:   Input Parameters:
8407: . dm - The DMPlex object

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


8414:   Level: intermediate

8416: .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
8417: @*/
8418: PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
8419: {
8420:   DM_Plex *plex = (DM_Plex *)dm->data;

8425:   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {(*plex->createanchors)(dm);}
8426:   if (anchorSection) *anchorSection = plex->anchorSection;
8427:   if (anchorIS) *anchorIS = plex->anchorIS;
8428:   return(0);
8429: }

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

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

8439:   collective on dm

8441:   Input Parameters:
8442: + dm - The DMPlex object
8443: . 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).
8444: - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).

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

8448:   Level: intermediate

8450: .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
8451: @*/
8452: PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
8453: {
8454:   DM_Plex        *plex = (DM_Plex *)dm->data;
8455:   PetscMPIInt    result;

8460:   if (anchorSection) {
8462:     MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);
8463:     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
8464:   }
8465:   if (anchorIS) {
8467:     MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);
8468:     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
8469:   }

8471:   PetscObjectReference((PetscObject)anchorSection);
8472:   PetscSectionDestroy(&plex->anchorSection);
8473:   plex->anchorSection = anchorSection;

8475:   PetscObjectReference((PetscObject)anchorIS);
8476:   ISDestroy(&plex->anchorIS);
8477:   plex->anchorIS = anchorIS;

8479:   if (PetscUnlikelyDebug(anchorIS && anchorSection)) {
8480:     PetscInt size, a, pStart, pEnd;
8481:     const PetscInt *anchors;

8483:     PetscSectionGetChart(anchorSection,&pStart,&pEnd);
8484:     ISGetLocalSize(anchorIS,&size);
8485:     ISGetIndices(anchorIS,&anchors);
8486:     for (a = 0; a < size; a++) {
8487:       PetscInt p;

8489:       p = anchors[a];
8490:       if (p >= pStart && p < pEnd) {
8491:         PetscInt dof;

8493:         PetscSectionGetDof(anchorSection,p,&dof);
8494:         if (dof) {
8495:           PetscErrorCode ierr2;

8497:           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
8498:           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
8499:         }
8500:       }
8501:     }
8502:     ISRestoreIndices(anchorIS,&anchors);
8503:   }
8504:   /* reset the generic constraints */
8505:   DMSetDefaultConstraints(dm,NULL,NULL);
8506:   return(0);
8507: }

8509: static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
8510: {
8511:   PetscSection anchorSection;
8512:   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;

8517:   DMPlexGetAnchors(dm,&anchorSection,NULL);
8518:   PetscSectionCreate(PETSC_COMM_SELF,cSec);
8519:   PetscSectionGetNumFields(section,&numFields);
8520:   if (numFields) {
8521:     PetscInt f;
8522:     PetscSectionSetNumFields(*cSec,numFields);

8524:     for (f = 0; f < numFields; f++) {
8525:       PetscInt numComp;

8527:       PetscSectionGetFieldComponents(section,f,&numComp);
8528:       PetscSectionSetFieldComponents(*cSec,f,numComp);
8529:     }
8530:   }
8531:   PetscSectionGetChart(anchorSection,&pStart,&pEnd);
8532:   PetscSectionGetChart(section,&sStart,&sEnd);
8533:   pStart = PetscMax(pStart,sStart);
8534:   pEnd   = PetscMin(pEnd,sEnd);
8535:   pEnd   = PetscMax(pStart,pEnd);
8536:   PetscSectionSetChart(*cSec,pStart,pEnd);
8537:   for (p = pStart; p < pEnd; p++) {
8538:     PetscSectionGetDof(anchorSection,p,&dof);
8539:     if (dof) {
8540:       PetscSectionGetDof(section,p,&dof);
8541:       PetscSectionSetDof(*cSec,p,dof);
8542:       for (f = 0; f < numFields; f++) {
8543:         PetscSectionGetFieldDof(section,p,f,&dof);
8544:         PetscSectionSetFieldDof(*cSec,p,f,dof);
8545:       }
8546:     }
8547:   }
8548:   PetscSectionSetUp(*cSec);
8549:   PetscObjectSetName((PetscObject) *cSec, "Constraint Section");
8550:   return(0);
8551: }

8553: static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
8554: {
8555:   PetscSection   aSec;
8556:   PetscInt       pStart, pEnd, p, sStart, sEnd, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
8557:   const PetscInt *anchors;
8558:   PetscInt       numFields, f;
8559:   IS             aIS;
8561:   MatType        mtype;
8562:   PetscBool      iscuda,iskokkos;

8566:   PetscSectionGetStorageSize(cSec, &m);
8567:   PetscSectionGetStorageSize(section, &n);
8568:   MatCreate(PETSC_COMM_SELF,cMat);
8569:   MatSetSizes(*cMat,m,n,m,n);
8570:   PetscStrcmp(dm->mattype,MATSEQAIJCUSPARSE,&iscuda);
8571:   if (!iscuda) { PetscStrcmp(dm->mattype,MATMPIAIJCUSPARSE,&iscuda); }
8572:   PetscStrcmp(dm->mattype,MATSEQAIJKOKKOS,&iskokkos);
8573:   if (!iskokkos) { PetscStrcmp(dm->mattype,MATMPIAIJKOKKOS,&iskokkos); }
8574:   if (iscuda) mtype = MATSEQAIJCUSPARSE;
8575:   else if (iskokkos) mtype = MATSEQAIJKOKKOS;
8576:   else mtype = MATSEQAIJ;
8577:   MatSetType(*cMat,mtype);
8578:   DMPlexGetAnchors(dm,&aSec,&aIS);
8579:   ISGetIndices(aIS,&anchors);
8580:   /* cSec will be a subset of aSec and section */
8581:   PetscSectionGetChart(cSec,&pStart,&pEnd);
8582:   PetscSectionGetChart(section,&sStart,&sEnd);
8583:   PetscMalloc1(m+1,&i);
8584:   i[0] = 0;
8585:   PetscSectionGetNumFields(section,&numFields);
8586:   for (p = pStart; p < pEnd; p++) {
8587:     PetscInt rDof, rOff, r;

8589:     PetscSectionGetDof(aSec,p,&rDof);
8590:     if (!rDof) continue;
8591:     PetscSectionGetOffset(aSec,p,&rOff);
8592:     if (numFields) {
8593:       for (f = 0; f < numFields; f++) {
8594:         annz = 0;
8595:         for (r = 0; r < rDof; r++) {
8596:           a = anchors[rOff + r];
8597:           if (a < sStart || a >= sEnd) continue;
8598:           PetscSectionGetFieldDof(section,a,f,&aDof);
8599:           annz += aDof;
8600:         }
8601:         PetscSectionGetFieldDof(cSec,p,f,&dof);
8602:         PetscSectionGetFieldOffset(cSec,p,f,&off);
8603:         for (q = 0; q < dof; q++) {
8604:           i[off + q + 1] = i[off + q] + annz;
8605:         }
8606:       }
8607:     }
8608:     else {
8609:       annz = 0;
8610:       PetscSectionGetDof(cSec,p,&dof);
8611:       for (q = 0; q < dof; q++) {
8612:         a = anchors[rOff + q];
8613:         if (a < sStart || a >= sEnd) continue;
8614:         PetscSectionGetDof(section,a,&aDof);
8615:         annz += aDof;
8616:       }
8617:       PetscSectionGetDof(cSec,p,&dof);
8618:       PetscSectionGetOffset(cSec,p,&off);
8619:       for (q = 0; q < dof; q++) {
8620:         i[off + q + 1] = i[off + q] + annz;
8621:       }
8622:     }
8623:   }
8624:   nnz = i[m];
8625:   PetscMalloc1(nnz,&j);
8626:   offset = 0;
8627:   for (p = pStart; p < pEnd; p++) {
8628:     if (numFields) {
8629:       for (f = 0; f < numFields; f++) {
8630:         PetscSectionGetFieldDof(cSec,p,f,&dof);
8631:         for (q = 0; q < dof; q++) {
8632:           PetscInt rDof, rOff, r;
8633:           PetscSectionGetDof(aSec,p,&rDof);
8634:           PetscSectionGetOffset(aSec,p,&rOff);
8635:           for (r = 0; r < rDof; r++) {
8636:             PetscInt s;

8638:             a = anchors[rOff + r];
8639:             if (a < sStart || a >= sEnd) continue;
8640:             PetscSectionGetFieldDof(section,a,f,&aDof);
8641:             PetscSectionGetFieldOffset(section,a,f,&aOff);
8642:             for (s = 0; s < aDof; s++) {
8643:               j[offset++] = aOff + s;
8644:             }
8645:           }
8646:         }
8647:       }
8648:     }
8649:     else {
8650:       PetscSectionGetDof(cSec,p,&dof);
8651:       for (q = 0; q < dof; q++) {
8652:         PetscInt rDof, rOff, r;
8653:         PetscSectionGetDof(aSec,p,&rDof);
8654:         PetscSectionGetOffset(aSec,p,&rOff);
8655:         for (r = 0; r < rDof; r++) {
8656:           PetscInt s;

8658:           a = anchors[rOff + r];
8659:           if (a < sStart || a >= sEnd) continue;
8660:           PetscSectionGetDof(section,a,&aDof);
8661:           PetscSectionGetOffset(section,a,&aOff);
8662:           for (s = 0; s < aDof; s++) {
8663:             j[offset++] = aOff + s;
8664:           }
8665:         }
8666:       }
8667:     }
8668:   }
8669:   MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);
8670:   PetscFree(i);
8671:   PetscFree(j);
8672:   ISRestoreIndices(aIS,&anchors);
8673:   return(0);
8674: }

8676: PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
8677: {
8678:   DM_Plex        *plex = (DM_Plex *)dm->data;
8679:   PetscSection   anchorSection, section, cSec;
8680:   Mat            cMat;

8685:   DMPlexGetAnchors(dm,&anchorSection,NULL);
8686:   if (anchorSection) {
8687:     PetscInt Nf;

8689:     DMGetLocalSection(dm,&section);
8690:     DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);
8691:     DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);
8692:     DMGetNumFields(dm,&Nf);
8693:     if (Nf && plex->computeanchormatrix) {(*plex->computeanchormatrix)(dm,section,cSec,cMat);}
8694:     DMSetDefaultConstraints(dm,cSec,cMat);
8695:     PetscSectionDestroy(&cSec);
8696:     MatDestroy(&cMat);
8697:   }
8698:   return(0);
8699: }

8701: PetscErrorCode DMCreateSubDomainDM_Plex(DM dm, DMLabel label, PetscInt value, IS *is, DM *subdm)
8702: {
8703:   IS             subis;
8704:   PetscSection   section, subsection;

8708:   DMGetLocalSection(dm, &section);
8709:   if (!section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting subdomain");
8710:   if (!subdm)   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set output subDM for splitting subdomain");
8711:   /* Create subdomain */
8712:   DMPlexFilter(dm, label, value, subdm);
8713:   /* Create submodel */
8714:   DMPlexGetSubpointIS(*subdm, &subis);
8715:   PetscSectionCreateSubmeshSection(section, subis, &subsection);
8716:   DMSetLocalSection(*subdm, subsection);
8717:   PetscSectionDestroy(&subsection);
8718:   DMCopyDisc(dm, *subdm);
8719:   /* Create map from submodel to global model */
8720:   if (is) {
8721:     PetscSection    sectionGlobal, subsectionGlobal;
8722:     IS              spIS;
8723:     const PetscInt *spmap;
8724:     PetscInt       *subIndices;
8725:     PetscInt        subSize = 0, subOff = 0, pStart, pEnd, p;
8726:     PetscInt        Nf, f, bs = -1, bsLocal[2], bsMinMax[2];

8728:     DMPlexGetSubpointIS(*subdm, &spIS);
8729:     ISGetIndices(spIS, &spmap);
8730:     PetscSectionGetNumFields(section, &Nf);
8731:     DMGetGlobalSection(dm, &sectionGlobal);
8732:     DMGetGlobalSection(*subdm, &subsectionGlobal);
8733:     PetscSectionGetChart(subsection, &pStart, &pEnd);
8734:     for (p = pStart; p < pEnd; ++p) {
8735:       PetscInt gdof, pSubSize  = 0;

8737:       PetscSectionGetDof(sectionGlobal, p, &gdof);
8738:       if (gdof > 0) {
8739:         for (f = 0; f < Nf; ++f) {
8740:           PetscInt fdof, fcdof;

8742:           PetscSectionGetFieldDof(subsection, p, f, &fdof);
8743:           PetscSectionGetFieldConstraintDof(subsection, p, f, &fcdof);
8744:           pSubSize += fdof-fcdof;
8745:         }
8746:         subSize += pSubSize;
8747:         if (pSubSize) {
8748:           if (bs < 0) {
8749:             bs = pSubSize;
8750:           } else if (bs != pSubSize) {
8751:             /* Layout does not admit a pointwise block size */
8752:             bs = 1;
8753:           }
8754:         }
8755:       }
8756:     }
8757:     /* Must have same blocksize on all procs (some might have no points) */
8758:     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
8759:     PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);
8760:     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
8761:     else                            {bs = bsMinMax[0];}
8762:     PetscMalloc1(subSize, &subIndices);
8763:     for (p = pStart; p < pEnd; ++p) {
8764:       PetscInt gdof, goff;

8766:       PetscSectionGetDof(subsectionGlobal, p, &gdof);
8767:       if (gdof > 0) {
8768:         const PetscInt point = spmap[p];

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

8774:           /* Can get rid of this loop by storing field information in the global section */
8775:           for (f2 = 0; f2 < f; ++f2) {
8776:             PetscSectionGetFieldDof(section, p, f2, &fdof);
8777:             PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);
8778:             poff += fdof-fcdof;
8779:           }
8780:           PetscSectionGetFieldDof(section, p, f, &fdof);
8781:           PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);
8782:           for (fc = 0; fc < fdof-fcdof; ++fc, ++subOff) {
8783:             subIndices[subOff] = goff+poff+fc;
8784:           }
8785:         }
8786:       }
8787:     }
8788:     ISRestoreIndices(spIS, &spmap);
8789:     ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);
8790:     if (bs > 1) {
8791:       /* We need to check that the block size does not come from non-contiguous fields */
8792:       PetscInt i, j, set = 1;
8793:       for (i = 0; i < subSize; i += bs) {
8794:         for (j = 0; j < bs; ++j) {
8795:           if (subIndices[i+j] != subIndices[i]+j) {set = 0; break;}
8796:         }
8797:       }
8798:       if (set) {ISSetBlockSize(*is, bs);}
8799:     }
8800:     /* Attach nullspace */
8801:     for (f = 0; f < Nf; ++f) {
8802:       (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[f];
8803:       if ((*subdm)->nullspaceConstructors[f]) break;
8804:     }
8805:     if (f < Nf) {
8806:       MatNullSpace nullSpace;

8808:       (*(*subdm)->nullspaceConstructors[f])(*subdm, f, f, &nullSpace);
8809:       PetscObjectCompose((PetscObject) *is, "nullspace", (PetscObject) nullSpace);
8810:       MatNullSpaceDestroy(&nullSpace);
8811:     }
8812:   }
8813:   return(0);
8814: }

8816: /*@
8817:   DMPlexMonitorThroughput - Report the cell throughput of FE integration

8819:   Input Parameter:
8820: - dm - The DM

8822:   Level: developer

8824:   Options Database Keys:
8825: . -dm_plex_monitor_throughput - Activate the monitor

8827: .seealso: DMSetFromOptions(), DMPlexCreate()
8828: @*/
8829: PetscErrorCode DMPlexMonitorThroughput(DM dm, void *dummy)
8830: {
8831: #if defined(PETSC_USE_LOG)
8832:   PetscStageLog      stageLog;
8833:   PetscLogEvent      event;
8834:   PetscLogStage      stage;
8835:   PetscEventPerfInfo eventInfo;
8836:   PetscReal          cellRate, flopRate;
8837:   PetscInt           cStart, cEnd, Nf, N;
8838:   const char        *name;
8839:   PetscErrorCode     ierr;
8840: #endif

8844: #if defined(PETSC_USE_LOG)
8845:   PetscObjectGetName((PetscObject) dm, &name);
8846:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
8847:   DMGetNumFields(dm, &Nf);
8848:   PetscLogGetStageLog(&stageLog);
8849:   PetscStageLogGetCurrent(stageLog, &stage);
8850:   PetscLogEventGetId("DMPlexResidualFE", &event);
8851:   PetscLogEventGetPerfInfo(stage, event, &eventInfo);
8852:   N        = (cEnd - cStart)*Nf*eventInfo.count;
8853:   flopRate = eventInfo.flops/eventInfo.time;
8854:   cellRate = N/eventInfo.time;
8855:   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));
8856: #else
8857:   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Plex Throughput Monitor is not supported if logging is turned off. Reconfigure using --with-log.");
8858: #endif
8859:   return(0);
8860: }