Actual source code: plexhdf5.c

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

  7: PETSC_EXTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);

  9: #if defined(PETSC_HAVE_HDF5)
 10: static PetscErrorCode DMSequenceView_HDF5(DM dm, const char *seqname, PetscInt seqnum, PetscScalar value, PetscViewer viewer)
 11: {
 12:   Vec            stamp;
 13:   PetscMPIInt    rank;

 17:   if (seqnum < 0) return(0);
 18:   MPI_Comm_rank(PetscObjectComm((PetscObject) viewer), &rank);
 19:   VecCreateMPI(PetscObjectComm((PetscObject) viewer), rank ? 0 : 1, 1, &stamp);
 20:   VecSetBlockSize(stamp, 1);
 21:   PetscObjectSetName((PetscObject) stamp, seqname);
 22:   if (!rank) {
 23:     PetscReal timeScale;
 24:     PetscBool istime;

 26:     PetscStrncmp(seqname, "time", 5, &istime);
 27:     if (istime) {DMPlexGetScale(dm, PETSC_UNIT_TIME, &timeScale); value *= timeScale;}
 28:     VecSetValue(stamp, 0, value, INSERT_VALUES);
 29:   }
 30:   VecAssemblyBegin(stamp);
 31:   VecAssemblyEnd(stamp);
 32:   PetscViewerHDF5PushGroup(viewer, "/");
 33:   PetscViewerHDF5SetTimestep(viewer, seqnum);
 34:   VecView(stamp, viewer);
 35:   PetscViewerHDF5PopGroup(viewer);
 36:   VecDestroy(&stamp);
 37:   return(0);
 38: }

 40: PetscErrorCode DMSequenceLoad_HDF5_Internal(DM dm, const char *seqname, PetscInt seqnum, PetscScalar *value, PetscViewer viewer)
 41: {
 42:   Vec            stamp;
 43:   PetscMPIInt    rank;

 47:   if (seqnum < 0) return(0);
 48:   MPI_Comm_rank(PetscObjectComm((PetscObject) viewer), &rank);
 49:   VecCreateMPI(PetscObjectComm((PetscObject) viewer), rank ? 0 : 1, 1, &stamp);
 50:   VecSetBlockSize(stamp, 1);
 51:   PetscObjectSetName((PetscObject) stamp, seqname);
 52:   PetscViewerHDF5PushGroup(viewer, "/");
 53:   PetscViewerHDF5SetTimestep(viewer, seqnum);
 54:   VecLoad(stamp, viewer);
 55:   PetscViewerHDF5PopGroup(viewer);
 56:   if (!rank) {
 57:     const PetscScalar *a;
 58:     PetscReal timeScale;
 59:     PetscBool istime;

 61:     VecGetArrayRead(stamp, &a);
 62:     *value = a[0];
 63:     VecRestoreArrayRead(stamp, &a);
 64:     PetscStrncmp(seqname, "time", 5, &istime);
 65:     if (istime) {DMPlexGetScale(dm, PETSC_UNIT_TIME, &timeScale); *value /= timeScale;}
 66:   }
 67:   VecDestroy(&stamp);
 68:   return(0);
 69: }

 71: static PetscErrorCode DMPlexCreateCutVertexLabel_Private(DM dm, DMLabel cutLabel, DMLabel *cutVertexLabel)
 72: {
 73:   IS              cutcells = NULL;
 74:   const PetscInt *cutc;
 75:   PetscInt        cellHeight, vStart, vEnd, cStart, cEnd, c;
 76:   PetscErrorCode  ierr;

 79:   if (!cutLabel) return(0);
 80:   DMPlexGetVTKCellHeight(dm, &cellHeight);
 81:   DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
 82:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
 83:   /* Label vertices that should be duplicated */
 84:   DMLabelCreate(PETSC_COMM_SELF, "Cut Vertices", cutVertexLabel);
 85:   DMLabelGetStratumIS(cutLabel, 2, &cutcells);
 86:   if (cutcells) {
 87:     PetscInt n;

 89:     ISGetIndices(cutcells, &cutc);
 90:     ISGetLocalSize(cutcells, &n);
 91:     for (c = 0; c < n; ++c) {
 92:       if ((cutc[c] >= cStart) && (cutc[c] < cEnd)) {
 93:         PetscInt *closure = NULL;
 94:         PetscInt  closureSize, cl, value;

 96:         DMPlexGetTransitiveClosure(dm, cutc[c], PETSC_TRUE, &closureSize, &closure);
 97:         for (cl = 0; cl < closureSize*2; cl += 2) {
 98:           if ((closure[cl] >= vStart) && (closure[cl] < vEnd)) {
 99:             DMLabelGetValue(cutLabel, closure[cl], &value);
100:             if (value == 1) {
101:               DMLabelSetValue(*cutVertexLabel, closure[cl], 1);
102:             }
103:           }
104:         }
105:         DMPlexRestoreTransitiveClosure(dm, cutc[c], PETSC_TRUE, &closureSize, &closure);
106:       }
107:     }
108:     ISRestoreIndices(cutcells, &cutc);
109:   }
110:   ISDestroy(&cutcells);
111:   return(0);
112: }

114: PetscErrorCode VecView_Plex_Local_HDF5_Internal(Vec v, PetscViewer viewer)
115: {
116:   DM                      dm;
117:   DM                      dmBC;
118:   PetscSection            section, sectionGlobal;
119:   Vec                     gv;
120:   const char             *name;
121:   PetscViewerVTKFieldType ft;
122:   PetscViewerFormat       format;
123:   PetscInt                seqnum;
124:   PetscReal               seqval;
125:   PetscBool               isseq;
126:   PetscErrorCode          ierr;

129:   PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);
130:   VecGetDM(v, &dm);
131:   DMGetLocalSection(dm, &section);
132:   DMGetOutputSequenceNumber(dm, &seqnum, &seqval);
133:   PetscViewerHDF5SetTimestep(viewer, seqnum);
134:   DMSequenceView_HDF5(dm, "time", seqnum, (PetscScalar) seqval, viewer);
135:   PetscViewerGetFormat(viewer, &format);
136:   DMGetOutputDM(dm, &dmBC);
137:   DMGetGlobalSection(dmBC, &sectionGlobal);
138:   DMGetGlobalVector(dmBC, &gv);
139:   PetscObjectGetName((PetscObject) v, &name);
140:   PetscObjectSetName((PetscObject) gv, name);
141:   DMLocalToGlobalBegin(dmBC, v, INSERT_VALUES, gv);
142:   DMLocalToGlobalEnd(dmBC, v, INSERT_VALUES, gv);
143:   PetscObjectTypeCompare((PetscObject) gv, VECSEQ, &isseq);
144:   if (isseq) {VecView_Seq(gv, viewer);}
145:   else       {VecView_MPI(gv, viewer);}
146:   if (format == PETSC_VIEWER_HDF5_VIZ) {
147:     /* Output visualization representation */
148:     PetscInt numFields, f;
149:     DMLabel  cutLabel, cutVertexLabel = NULL;

151:     PetscSectionGetNumFields(section, &numFields);
152:     DMGetLabel(dm, "periodic_cut", &cutLabel);
153:     for (f = 0; f < numFields; ++f) {
154:       Vec         subv;
155:       IS          is;
156:       const char *fname, *fgroup;
157:       char        subname[PETSC_MAX_PATH_LEN];
158:       PetscInt    pStart, pEnd;

160:       DMPlexGetFieldType_Internal(dm, section, f, &pStart, &pEnd, &ft);
161:       fgroup = (ft == PETSC_VTK_POINT_VECTOR_FIELD) || (ft == PETSC_VTK_POINT_FIELD) ? "/vertex_fields" : "/cell_fields";
162:       PetscSectionGetFieldName(section, f, &fname);
163:       if (!fname) continue;
164:       PetscViewerHDF5PushGroup(viewer, fgroup);
165:       if (cutLabel) {
166:         const PetscScalar *ga;
167:         PetscScalar       *suba;
168:         PetscInt           Nc, gstart, subSize = 0, extSize = 0, subOff = 0, newOff = 0, p;

170:         DMPlexCreateCutVertexLabel_Private(dm, cutLabel, &cutVertexLabel);
171:         PetscSectionGetFieldComponents(section, f, &Nc);
172:         for (p = pStart; p < pEnd; ++p) {
173:           PetscInt gdof, fdof = 0, val;

175:           PetscSectionGetDof(sectionGlobal, p, &gdof);
176:           if (gdof > 0) {PetscSectionGetFieldDof(section, p, f, &fdof);}
177:           subSize += fdof;
178:           DMLabelGetValue(cutVertexLabel, p, &val);
179:           if (val == 1) extSize += fdof;
180:         }
181:         VecCreate(PetscObjectComm((PetscObject) gv), &subv);
182:         VecSetSizes(subv, subSize+extSize, PETSC_DETERMINE);
183:         VecSetBlockSize(subv, Nc);
184:         VecSetType(subv, VECSTANDARD);
185:         VecGetOwnershipRange(gv, &gstart, NULL);
186:         VecGetArrayRead(gv, &ga);
187:         VecGetArray(subv, &suba);
188:         for (p = pStart; p < pEnd; ++p) {
189:           PetscInt gdof, goff, val;

191:           PetscSectionGetDof(sectionGlobal, p, &gdof);
192:           if (gdof > 0) {
193:             PetscInt fdof, fc, f2, poff = 0;

195:             PetscSectionGetOffset(sectionGlobal, p, &goff);
196:             /* Can get rid of this loop by storing field information in the global section */
197:             for (f2 = 0; f2 < f; ++f2) {
198:               PetscSectionGetFieldDof(section, p, f2, &fdof);
199:               poff += fdof;
200:             }
201:             PetscSectionGetFieldDof(section, p, f, &fdof);
202:             for (fc = 0; fc < fdof; ++fc, ++subOff) suba[subOff] = ga[goff+poff+fc - gstart];
203:             DMLabelGetValue(cutVertexLabel, p, &val);
204:             if (val == 1) {
205:               for (fc = 0; fc < fdof; ++fc, ++newOff) suba[subSize+newOff] = ga[goff+poff+fc - gstart];
206:             }
207:           }
208:         }
209:         VecRestoreArrayRead(gv, &ga);
210:         VecRestoreArray(subv, &suba);
211:         DMLabelDestroy(&cutVertexLabel);
212:       } else {
213:         PetscSectionGetField_Internal(section, sectionGlobal, gv, f, pStart, pEnd, &is, &subv);
214:       }
215:       PetscStrncpy(subname, name,sizeof(subname));
216:       PetscStrlcat(subname, "_",sizeof(subname));
217:       PetscStrlcat(subname, fname,sizeof(subname));
218:       PetscObjectSetName((PetscObject) subv, subname);
219:       if (isseq) {VecView_Seq(subv, viewer);}
220:       else       {VecView_MPI(subv, viewer);}
221:       if ((ft == PETSC_VTK_POINT_VECTOR_FIELD) || (ft == PETSC_VTK_CELL_VECTOR_FIELD)) {
222:         PetscViewerHDF5WriteObjectAttribute(viewer, (PetscObject) subv, "vector_field_type", PETSC_STRING, "vector");
223:       } else {
224:         PetscViewerHDF5WriteObjectAttribute(viewer, (PetscObject) subv, "vector_field_type", PETSC_STRING, "scalar");
225:       }
226:       if (cutLabel) {VecDestroy(&subv);}
227:       else          {PetscSectionRestoreField_Internal(section, sectionGlobal, gv, f, pStart, pEnd, &is, &subv);}
228:       PetscViewerHDF5PopGroup(viewer);
229:     }
230:   }
231:   DMRestoreGlobalVector(dmBC, &gv);
232:   return(0);
233: }

235: PetscErrorCode VecView_Plex_HDF5_Internal(Vec v, PetscViewer viewer)
236: {
237:   DM             dm;
238:   Vec            locv;
239:   const char    *name;
240:   PetscReal      time;

244:   VecGetDM(v, &dm);
245:   DMGetLocalVector(dm, &locv);
246:   PetscObjectGetName((PetscObject) v, &name);
247:   PetscObjectSetName((PetscObject) locv, name);
248:   DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);
249:   DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);
250:   DMGetOutputSequenceNumber(dm, NULL, &time);
251:   DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);
252:   PetscViewerHDF5PushGroup(viewer, "/fields");
253:   PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_VIZ);
254:   VecView_Plex_Local_HDF5_Internal(locv, viewer);
255:   PetscViewerPopFormat(viewer);
256:   PetscViewerHDF5PopGroup(viewer);
257:   DMRestoreLocalVector(dm, &locv);
258:   return(0);
259: }

261: PetscErrorCode VecView_Plex_HDF5_Native_Internal(Vec v, PetscViewer viewer)
262: {
263:   PetscBool      isseq;

267:   PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);
268:   PetscViewerHDF5PushGroup(viewer, "/fields");
269:   if (isseq) {VecView_Seq(v, viewer);}
270:   else       {VecView_MPI(v, viewer);}
271:   PetscViewerHDF5PopGroup(viewer);
272:   return(0);
273: }

275: PetscErrorCode VecLoad_Plex_HDF5_Internal(Vec v, PetscViewer viewer)
276: {
277:   DM             dm;
278:   Vec            locv;
279:   const char    *name;
280:   PetscInt       seqnum;

284:   VecGetDM(v, &dm);
285:   DMGetLocalVector(dm, &locv);
286:   PetscObjectGetName((PetscObject) v, &name);
287:   PetscObjectSetName((PetscObject) locv, name);
288:   DMGetOutputSequenceNumber(dm, &seqnum, NULL);
289:   PetscViewerHDF5SetTimestep(viewer, seqnum);
290:   PetscViewerHDF5PushGroup(viewer, "/fields");
291:   VecLoad_Plex_Local(locv, viewer);
292:   PetscViewerHDF5PopGroup(viewer);
293:   DMLocalToGlobalBegin(dm, locv, INSERT_VALUES, v);
294:   DMLocalToGlobalEnd(dm, locv, INSERT_VALUES, v);
295:   DMRestoreLocalVector(dm, &locv);
296:   return(0);
297: }

299: PetscErrorCode VecLoad_Plex_HDF5_Native_Internal(Vec v, PetscViewer viewer)
300: {
301:   DM             dm;
302:   PetscInt       seqnum;

306:   VecGetDM(v, &dm);
307:   DMGetOutputSequenceNumber(dm, &seqnum, NULL);
308:   PetscViewerHDF5SetTimestep(viewer, seqnum);
309:   PetscViewerHDF5PushGroup(viewer, "/fields");
310:   VecLoad_Default(v, viewer);
311:   PetscViewerHDF5PopGroup(viewer);
312:   return(0);
313: }

315: static PetscErrorCode DMPlexWriteTopology_HDF5_Static(DM dm, IS globalPointNumbers, PetscViewer viewer)
316: {
317:   IS              orderIS, conesIS, cellsIS, orntsIS;
318:   const PetscInt *gpoint;
319:   PetscInt       *order, *sizes, *cones, *ornts;
320:   PetscInt        dim, pStart, pEnd, p, conesSize = 0, cellsSize = 0, c = 0, s = 0;
321:   PetscErrorCode  ierr;

324:   ISGetIndices(globalPointNumbers, &gpoint);
325:   DMGetDimension(dm, &dim);
326:   DMPlexGetChart(dm, &pStart, &pEnd);
327:   for (p = pStart; p < pEnd; ++p) {
328:     if (gpoint[p] >= 0) {
329:       PetscInt coneSize;

331:       DMPlexGetConeSize(dm, p, &coneSize);
332:       conesSize += 1;
333:       cellsSize += coneSize;
334:     }
335:   }
336:   PetscMalloc1(conesSize, &order);
337:   PetscMalloc1(conesSize, &sizes);
338:   PetscMalloc1(cellsSize, &cones);
339:   PetscMalloc1(cellsSize, &ornts);
340:   for (p = pStart; p < pEnd; ++p) {
341:     if (gpoint[p] >= 0) {
342:       const PetscInt *cone, *ornt;
343:       PetscInt        coneSize, cp;

345:       DMPlexGetConeSize(dm, p, &coneSize);
346:       DMPlexGetCone(dm, p, &cone);
347:       DMPlexGetConeOrientation(dm, p, &ornt);
348:       order[s]   = gpoint[p];
349:       sizes[s++] = coneSize;
350:       for (cp = 0; cp < coneSize; ++cp, ++c) {cones[c] = gpoint[cone[cp]] < 0 ? -(gpoint[cone[cp]]+1) : gpoint[cone[cp]]; ornts[c] = ornt[cp];}
351:     }
352:   }
353:   if (s != conesSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Total number of points %d != %d", s, conesSize);
354:   if (c != cellsSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Total number of cone points %d != %d", c, cellsSize);
355:   ISCreateGeneral(PetscObjectComm((PetscObject) dm), conesSize, order, PETSC_OWN_POINTER, &orderIS);
356:   PetscObjectSetName((PetscObject) orderIS, "order");
357:   ISCreateGeneral(PetscObjectComm((PetscObject) dm), conesSize, sizes, PETSC_OWN_POINTER, &conesIS);
358:   PetscObjectSetName((PetscObject) conesIS, "cones");
359:   ISCreateGeneral(PetscObjectComm((PetscObject) dm), cellsSize, cones, PETSC_OWN_POINTER, &cellsIS);
360:   PetscObjectSetName((PetscObject) cellsIS, "cells");
361:   ISCreateGeneral(PetscObjectComm((PetscObject) dm), cellsSize, ornts, PETSC_OWN_POINTER, &orntsIS);
362:   PetscObjectSetName((PetscObject) orntsIS, "orientation");
363:   PetscViewerHDF5PushGroup(viewer, "/topology");
364:   ISView(orderIS, viewer);
365:   ISView(conesIS, viewer);
366:   ISView(cellsIS, viewer);
367:   PetscViewerHDF5WriteObjectAttribute(viewer, (PetscObject) cellsIS, "cell_dim", PETSC_INT, (void *) &dim);
368:   ISView(orntsIS, viewer);
369:   PetscViewerHDF5PopGroup(viewer);
370:   ISDestroy(&orderIS);
371:   ISDestroy(&conesIS);
372:   ISDestroy(&cellsIS);
373:   ISDestroy(&orntsIS);
374:   ISRestoreIndices(globalPointNumbers, &gpoint);
375:   return(0);
376: }

378: static PetscErrorCode CreateConesIS_Private(DM dm, PetscInt cStart, PetscInt cEnd, PetscInt *numCorners, IS *cellIS)
379: {
380:   PetscSF         sfPoint;
381:   DMLabel         cutLabel, cutVertexLabel = NULL;
382:   IS              globalVertexNumbers, cutvertices = NULL;
383:   const PetscInt *gvertex, *cutverts = NULL;
384:   PetscInt       *vertices;
385:   PetscInt        conesSize = 0;
386:   PetscInt        dim, numCornersLocal = 0, cell, vStart, vEnd, vExtra = 0, v;
387:   PetscErrorCode  ierr;

390:   *numCorners = 0;
391:   DMGetDimension(dm, &dim);
392:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);

394:   for (cell = cStart; cell < cEnd; ++cell) {
395:     PetscInt *closure = NULL;
396:     PetscInt  closureSize, v, Nc = 0;

398:     DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);
399:     for (v = 0; v < closureSize*2; v += 2) {
400:       if ((closure[v] >= vStart) && (closure[v] < vEnd)) ++Nc;
401:     }
402:     DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);
403:     conesSize += Nc;
404:     if (!numCornersLocal)           numCornersLocal = Nc;
405:     else if (numCornersLocal != Nc) numCornersLocal = 1;
406:   }
407:   MPIU_Allreduce(&numCornersLocal, numCorners, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));
408:   if (numCornersLocal && (numCornersLocal != *numCorners || *numCorners == 1)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Visualization topology currently only supports identical cell shapes");
409:   /* Handle periodic cuts by identifying vertices which should be duplicated */
410:   DMGetLabel(dm, "periodic_cut", &cutLabel);
411:   DMPlexCreateCutVertexLabel_Private(dm, cutLabel, &cutVertexLabel);
412:   if (cutVertexLabel) {DMLabelGetStratumIS(cutVertexLabel, 1, &cutvertices);}
413:   if (cutvertices) {
414:     ISGetIndices(cutvertices, &cutverts);
415:     ISGetLocalSize(cutvertices, &vExtra);
416:   }
417:   DMGetPointSF(dm, &sfPoint);
418:   if (cutLabel) {
419:     const PetscInt    *ilocal;
420:     const PetscSFNode *iremote;
421:     PetscInt           nroots, nleaves;

423:     PetscSFGetGraph(sfPoint, &nroots, &nleaves, &ilocal, &iremote);
424:     if (nleaves < 0) {
425:       PetscObjectReference((PetscObject) sfPoint);
426:     } else {
427:       PetscSFCreate(PetscObjectComm((PetscObject) sfPoint), &sfPoint);
428:       PetscSFSetGraph(sfPoint, nroots+vExtra, nleaves, ilocal, PETSC_USE_POINTER, iremote, PETSC_USE_POINTER);
429:     }
430:   } else {
431:     PetscObjectReference((PetscObject) sfPoint);
432:   }
433:   /* Number all vertices */
434:   DMPlexCreateNumbering_Plex(dm, vStart, vEnd+vExtra, 0, NULL, sfPoint, &globalVertexNumbers);
435:   PetscSFDestroy(&sfPoint);
436:   /* Create cones */
437:   ISGetIndices(globalVertexNumbers, &gvertex);
438:   PetscMalloc1(conesSize, &vertices);
439:   for (cell = cStart, v = 0; cell < cEnd; ++cell) {
440:     PetscInt *closure = NULL;
441:     PetscInt  closureSize, Nc = 0, p, value = -1;
442:     PetscBool replace;

444:     if (cutLabel) {DMLabelGetValue(cutLabel, cell, &value);}
445:     replace = (value == 2) ? PETSC_TRUE : PETSC_FALSE;
446:     DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);
447:     for (p = 0; p < closureSize*2; p += 2) {
448:       if ((closure[p] >= vStart) && (closure[p] < vEnd)) {
449:         closure[Nc++] = closure[p];
450:       }
451:     }
452:     DMPlexReorderCell(dm, cell, closure);
453:     for (p = 0; p < Nc; ++p) {
454:       PetscInt nv, gv = gvertex[closure[p] - vStart];

456:       if (replace) {
457:         PetscFindInt(closure[p], vExtra, cutverts, &nv);
458:         if (nv >= 0) gv = gvertex[vEnd - vStart + nv];
459:       }
460:       vertices[v++] = gv < 0 ? -(gv+1) : gv;
461:     }
462:     DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);
463:   }
464:   ISRestoreIndices(globalVertexNumbers, &gvertex);
465:   ISDestroy(&globalVertexNumbers);
466:   if (cutvertices) {ISRestoreIndices(cutvertices, &cutverts);}
467:   ISDestroy(&cutvertices);
468:   DMLabelDestroy(&cutVertexLabel);
469:   if (v != conesSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Total number of cell vertices %d != %d", v, conesSize);
470:   ISCreateGeneral(PetscObjectComm((PetscObject) dm), conesSize, vertices, PETSC_OWN_POINTER, cellIS);
471:   PetscLayoutSetBlockSize((*cellIS)->map, *numCorners);
472:   PetscObjectSetName((PetscObject) *cellIS, "cells");
473:   return(0);
474: }

476: static PetscErrorCode DMPlexWriteTopology_Vertices_HDF5_Static(DM dm, PetscViewer viewer)
477: {
478:   DM              cdm;
479:   DMLabel         depthLabel, ctLabel;
480:   IS              cellIS;
481:   PetscInt        dim, depth, cellHeight, c;
482:   hid_t           fileId, groupId;
483:   PetscErrorCode  ierr;

486:   PetscViewerHDF5PushGroup(viewer, "/viz");
487:   PetscViewerHDF5OpenGroup(viewer, &fileId, &groupId);
488:   PetscStackCallHDF5(H5Gclose,(groupId));

490:   PetscViewerHDF5PopGroup(viewer);
491:   DMGetDimension(dm, &dim);
492:   DMPlexGetDepth(dm, &depth);
493:   DMGetCoordinateDM(dm, &cdm);
494:   DMPlexGetVTKCellHeight(dm, &cellHeight);
495:   DMPlexGetDepthLabel(dm, &depthLabel);
496:   DMPlexGetCellTypeLabel(dm, &ctLabel);
497:   for (c = 0; c < DM_NUM_POLYTOPES; ++c) {
498:     const DMPolytopeType ict = (DMPolytopeType) c;
499:     PetscInt             pStart, pEnd, dep, numCorners, n = 0;
500:     PetscBool            output = PETSC_FALSE, doOutput;

502:     DMLabelGetStratumBounds(ctLabel, ict, &pStart, &pEnd);
503:     if (pStart >= 0) {
504:       DMLabelGetValue(depthLabel, pStart, &dep);
505:       if (dep == depth - cellHeight) output = PETSC_TRUE;
506:     }
507:     MPI_Allreduce(&output, &doOutput, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));
508:     if (!doOutput) continue;
509:     CreateConesIS_Private(dm, pStart, pEnd, &numCorners,  &cellIS);
510:     if (!n) {
511:       PetscViewerHDF5PushGroup(viewer, "/viz/topology");
512:     } else {
513:       char group[PETSC_MAX_PATH_LEN];

515:       PetscSNPrintf(group, PETSC_MAX_PATH_LEN, "/viz/topology_%D", n);
516:       PetscViewerHDF5PushGroup(viewer, group);
517:     }
518:     ISView(cellIS, viewer);
519:     PetscViewerHDF5WriteObjectAttribute(viewer, (PetscObject) cellIS, "cell_corners", PETSC_INT, (void *) &numCorners);
520:     PetscViewerHDF5WriteObjectAttribute(viewer, (PetscObject) cellIS, "cell_dim",     PETSC_INT, (void *) &dim);
521:     ISDestroy(&cellIS);
522:     PetscViewerHDF5PopGroup(viewer);
523:     ++n;
524:   }
525:   return(0);
526: }

528: static PetscErrorCode DMPlexWriteCoordinates_HDF5_Static(DM dm, PetscViewer viewer)
529: {
530:   DM             cdm;
531:   Vec            coordinates, newcoords;
532:   PetscReal      lengthScale;
533:   PetscInt       m, M, bs;

537:   DMPlexGetScale(dm, PETSC_UNIT_LENGTH, &lengthScale);
538:   DMGetCoordinateDM(dm, &cdm);
539:   DMGetCoordinates(dm, &coordinates);
540:   VecCreate(PetscObjectComm((PetscObject) coordinates), &newcoords);
541:   PetscObjectSetName((PetscObject) newcoords, "vertices");
542:   VecGetSize(coordinates, &M);
543:   VecGetLocalSize(coordinates, &m);
544:   VecSetSizes(newcoords, m, M);
545:   VecGetBlockSize(coordinates, &bs);
546:   VecSetBlockSize(newcoords, bs);
547:   VecSetType(newcoords,VECSTANDARD);
548:   VecCopy(coordinates, newcoords);
549:   VecScale(newcoords, lengthScale);
550:   /* Did not use DMGetGlobalVector() in order to bypass default group assignment */
551:   PetscViewerHDF5PushGroup(viewer, "/geometry");
552:   PetscViewerPushFormat(viewer, PETSC_VIEWER_NATIVE);
553:   VecView(newcoords, viewer);
554:   PetscViewerPopFormat(viewer);
555:   PetscViewerHDF5PopGroup(viewer);
556:   VecDestroy(&newcoords);
557:   return(0);
558: }

560: static PetscErrorCode DMPlexWriteCoordinates_Vertices_HDF5_Static(DM dm, PetscViewer viewer)
561: {
562:   DM               cdm;
563:   Vec              coordinatesLocal, newcoords;
564:   PetscSection     cSection, cGlobalSection;
565:   PetscScalar     *coords, *ncoords;
566:   DMLabel          cutLabel, cutVertexLabel = NULL;
567:   const PetscReal *L;
568:   const DMBoundaryType *bd;
569:   PetscReal        lengthScale;
570:   PetscInt         vStart, vEnd, v, bs, N, coordSize, dof, off, d;
571:   PetscBool        localized, embedded;
572:   hid_t            fileId, groupId;
573:   PetscErrorCode   ierr;

576:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
577:   DMPlexGetScale(dm, PETSC_UNIT_LENGTH, &lengthScale);
578:   DMGetCoordinatesLocal(dm, &coordinatesLocal);
579:   VecGetBlockSize(coordinatesLocal, &bs);
580:   DMGetCoordinatesLocalized(dm, &localized);
581:   if (localized == PETSC_FALSE) return(0);
582:   DMGetPeriodicity(dm, NULL, NULL, &L, &bd);
583:   DMGetCoordinateDM(dm, &cdm);
584:   DMGetLocalSection(cdm, &cSection);
585:   DMGetGlobalSection(cdm, &cGlobalSection);
586:   DMGetLabel(dm, "periodic_cut", &cutLabel);
587:   N    = 0;

589:   DMPlexCreateCutVertexLabel_Private(dm, cutLabel, &cutVertexLabel);
590:   VecCreate(PetscObjectComm((PetscObject) dm), &newcoords);
591:   PetscSectionGetDof(cSection, vStart, &dof);
592:   PetscPrintf(PETSC_COMM_SELF, "DOF: %D\n", dof);
593:   embedded  = (PetscBool) (L && dof == 2 && !cutLabel);
594:   if (cutVertexLabel) {
595:     DMLabelGetStratumSize(cutVertexLabel, 1, &v);
596:     N   += dof*v;
597:   }
598:   for (v = vStart; v < vEnd; ++v) {
599:     PetscSectionGetDof(cGlobalSection, v, &dof);
600:     if (dof < 0) continue;
601:     if (embedded) N += dof+1;
602:     else          N += dof;
603:   }
604:   if (embedded) {VecSetBlockSize(newcoords, bs+1);}
605:   else          {VecSetBlockSize(newcoords, bs);}
606:   VecSetSizes(newcoords, N, PETSC_DETERMINE);
607:   VecSetType(newcoords, VECSTANDARD);
608:   VecGetArray(coordinatesLocal, &coords);
609:   VecGetArray(newcoords,        &ncoords);
610:   coordSize = 0;
611:   for (v = vStart; v < vEnd; ++v) {
612:     PetscSectionGetDof(cGlobalSection, v, &dof);
613:     PetscSectionGetOffset(cSection, v, &off);
614:     if (dof < 0) continue;
615:     if (embedded) {
616:       if ((bd[0] == DM_BOUNDARY_PERIODIC) && (bd[1] == DM_BOUNDARY_PERIODIC)) {
617:         PetscReal theta, phi, r, R;
618:         /* XY-periodic */
619:         /* Suppose its an y-z circle, then
620:              \hat r = (0, cos(th), sin(th)) \hat x = (1, 0, 0)
621:            and the circle in that plane is
622:              \hat r cos(phi) + \hat x sin(phi) */
623:         theta = 2.0*PETSC_PI*PetscRealPart(coords[off+1])/L[1];
624:         phi   = 2.0*PETSC_PI*PetscRealPart(coords[off+0])/L[0];
625:         r     = L[0]/(2.0*PETSC_PI * 2.0*L[1]);
626:         R     = L[1]/(2.0*PETSC_PI);
627:         ncoords[coordSize++] =  PetscSinReal(phi) * r;
628:         ncoords[coordSize++] = -PetscCosReal(theta) * (R + r * PetscCosReal(phi));
629:         ncoords[coordSize++] =  PetscSinReal(theta) * (R + r * PetscCosReal(phi));
630:       } else if ((bd[0] == DM_BOUNDARY_PERIODIC)) {
631:         /* X-periodic */
632:         ncoords[coordSize++] = -PetscCosReal(2.0*PETSC_PI*PetscRealPart(coords[off+0])/L[0])*(L[0]/(2.0*PETSC_PI));
633:         ncoords[coordSize++] = coords[off+1];
634:         ncoords[coordSize++] = PetscSinReal(2.0*PETSC_PI*PetscRealPart(coords[off+0])/L[0])*(L[0]/(2.0*PETSC_PI));
635:       } else if ((bd[1] == DM_BOUNDARY_PERIODIC)) {
636:         /* Y-periodic */
637:         ncoords[coordSize++] = coords[off+0];
638:         ncoords[coordSize++] = PetscSinReal(2.0*PETSC_PI*PetscRealPart(coords[off+1])/L[1])*(L[1]/(2.0*PETSC_PI));
639:         ncoords[coordSize++] = -PetscCosReal(2.0*PETSC_PI*PetscRealPart(coords[off+1])/L[1])*(L[1]/(2.0*PETSC_PI));
640:       } else if ((bd[0] == DM_BOUNDARY_TWIST)) {
641:         PetscReal phi, r, R;
642:         /* Mobius strip */
643:         /* Suppose its an x-z circle, then
644:              \hat r = (-cos(phi), 0, sin(phi)) \hat y = (0, 1, 0)
645:            and in that plane we rotate by pi as we go around the circle
646:              \hat r cos(phi/2) + \hat y sin(phi/2) */
647:         phi   = 2.0*PETSC_PI*PetscRealPart(coords[off+0])/L[0];
648:         R     = L[0];
649:         r     = PetscRealPart(coords[off+1]) - L[1]/2.0;
650:         ncoords[coordSize++] = -PetscCosReal(phi) * (R + r * PetscCosReal(phi/2.0));
651:         ncoords[coordSize++] =  PetscSinReal(phi/2.0) * r;
652:         ncoords[coordSize++] =  PetscSinReal(phi) * (R + r * PetscCosReal(phi/2.0));
653:       } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot handle periodicity in this domain");
654:     } else {
655:       for (d = 0; d < dof; ++d, ++coordSize) ncoords[coordSize] = coords[off+d];
656:     }
657:   }
658:   if (cutVertexLabel) {
659:     IS              vertices;
660:     const PetscInt *verts;
661:     PetscInt        n;

663:     DMLabelGetStratumIS(cutVertexLabel, 1, &vertices);
664:     if (vertices) {
665:       ISGetIndices(vertices, &verts);
666:       ISGetLocalSize(vertices, &n);
667:       for (v = 0; v < n; ++v) {
668:         PetscSectionGetDof(cSection, verts[v], &dof);
669:         PetscSectionGetOffset(cSection, verts[v], &off);
670:         for (d = 0; d < dof; ++d) ncoords[coordSize++] = coords[off+d] + ((bd[d] == DM_BOUNDARY_PERIODIC) ? L[d] : 0.0);
671:       }
672:       ISRestoreIndices(vertices, &verts);
673:       ISDestroy(&vertices);
674:     }
675:   }
676:   if (coordSize != N) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatched sizes: %D != %D", coordSize, N);
677:   DMLabelDestroy(&cutVertexLabel);
678:   VecRestoreArray(coordinatesLocal, &coords);
679:   VecRestoreArray(newcoords,        &ncoords);
680:   PetscObjectSetName((PetscObject) newcoords, "vertices");
681:   VecScale(newcoords, lengthScale);
682:   PetscViewerHDF5PushGroup(viewer, "/viz");
683:   PetscViewerHDF5OpenGroup(viewer, &fileId, &groupId);
684:   PetscStackCallHDF5(H5Gclose,(groupId));
685:   PetscViewerHDF5PopGroup(viewer);
686:   PetscViewerHDF5PushGroup(viewer, "/viz/geometry");
687:   VecView(newcoords, viewer);
688:   PetscViewerHDF5PopGroup(viewer);
689:   VecDestroy(&newcoords);
690:   return(0);
691: }


694: static PetscErrorCode DMPlexWriteLabels_HDF5_Static(DM dm, IS globalPointNumbers, PetscViewer viewer)
695: {
696:   const PetscInt   *gpoint;
697:   PetscInt          numLabels, l;
698:   hid_t             fileId, groupId;
699:   PetscErrorCode    ierr;

702:   ISGetIndices(globalPointNumbers, &gpoint);
703:   PetscViewerHDF5PushGroup(viewer, "/labels");
704:   PetscViewerHDF5OpenGroup(viewer, &fileId, &groupId);
705:   if (groupId != fileId) PetscStackCallHDF5(H5Gclose,(groupId));
706:   PetscViewerHDF5PopGroup(viewer);
707:   DMGetNumLabels(dm, &numLabels);
708:   for (l = 0; l < numLabels; ++l) {
709:     DMLabel         label;
710:     const char     *name;
711:     IS              valueIS, pvalueIS, globalValueIS;
712:     const PetscInt *values;
713:     PetscInt        numValues, v;
714:     PetscBool       isDepth, output;
715:     char            group[PETSC_MAX_PATH_LEN];

717:     DMGetLabelName(dm, l, &name);
718:     DMGetLabelOutput(dm, name, &output);
719:     PetscStrncmp(name, "depth", 10, &isDepth);
720:     if (isDepth || !output) continue;
721:     DMGetLabel(dm, name, &label);
722:     PetscSNPrintf(group, PETSC_MAX_PATH_LEN, "/labels/%s", name);
723:     PetscViewerHDF5PushGroup(viewer, group);
724:     PetscViewerHDF5OpenGroup(viewer, &fileId, &groupId);
725:     if (groupId != fileId) PetscStackCallHDF5(H5Gclose,(groupId));
726:     PetscViewerHDF5PopGroup(viewer);
727:     DMLabelGetValueIS(label, &valueIS);
728:     /* Must copy to a new IS on the global comm */
729:     ISGetLocalSize(valueIS, &numValues);
730:     ISGetIndices(valueIS, &values);
731:     ISCreateGeneral(PetscObjectComm((PetscObject) dm), numValues, values, PETSC_COPY_VALUES, &pvalueIS);
732:     ISRestoreIndices(valueIS, &values);
733:     ISAllGather(pvalueIS, &globalValueIS);
734:     ISDestroy(&pvalueIS);
735:     ISSortRemoveDups(globalValueIS);
736:     ISGetLocalSize(globalValueIS, &numValues);
737:     ISGetIndices(globalValueIS, &values);
738:     for (v = 0; v < numValues; ++v) {
739:       IS              stratumIS, globalStratumIS;
740:       const PetscInt *spoints = NULL;
741:       PetscInt       *gspoints, n = 0, gn, p;
742:       const char     *iname = "indices";

744:       PetscSNPrintf(group, PETSC_MAX_PATH_LEN, "/labels/%s/%d", name, values[v]);
745:       DMLabelGetStratumIS(label, values[v], &stratumIS);

747:       if (stratumIS) {ISGetLocalSize(stratumIS, &n);}
748:       if (stratumIS) {ISGetIndices(stratumIS, &spoints);}
749:       for (gn = 0, p = 0; p < n; ++p) if (gpoint[spoints[p]] >= 0) ++gn;
750:       PetscMalloc1(gn,&gspoints);
751:       for (gn = 0, p = 0; p < n; ++p) if (gpoint[spoints[p]] >= 0) gspoints[gn++] = gpoint[spoints[p]];
752:       if (stratumIS) {ISRestoreIndices(stratumIS, &spoints);}
753:       ISCreateGeneral(PetscObjectComm((PetscObject) dm), gn, gspoints, PETSC_OWN_POINTER, &globalStratumIS);
754:       if (stratumIS) {PetscObjectGetName((PetscObject) stratumIS, &iname);}
755:       PetscObjectSetName((PetscObject) globalStratumIS, iname);

757:       PetscViewerHDF5PushGroup(viewer, group);
758:       ISView(globalStratumIS, viewer);
759:       PetscViewerHDF5PopGroup(viewer);
760:       ISDestroy(&globalStratumIS);
761:       ISDestroy(&stratumIS);
762:     }
763:     ISRestoreIndices(globalValueIS, &values);
764:     ISDestroy(&globalValueIS);
765:     ISDestroy(&valueIS);
766:   }
767:   ISRestoreIndices(globalPointNumbers, &gpoint);
768:   return(0);
769: }

771: /* We only write cells and vertices. Does this screw up parallel reading? */
772: PetscErrorCode DMPlexView_HDF5_Internal(DM dm, PetscViewer viewer)
773: {
774:   IS                globalPointNumbers;
775:   PetscViewerFormat format;
776:   PetscBool         viz_geom=PETSC_FALSE, xdmf_topo=PETSC_FALSE, petsc_topo=PETSC_FALSE;
777:   PetscErrorCode    ierr;

780:   DMPlexCreatePointNumbering(dm, &globalPointNumbers);
781:   DMPlexWriteCoordinates_HDF5_Static(dm, viewer);
782:   DMPlexWriteLabels_HDF5_Static(dm, globalPointNumbers, viewer);

784:   PetscViewerGetFormat(viewer, &format);
785:   switch (format) {
786:     case PETSC_VIEWER_HDF5_VIZ:
787:       viz_geom    = PETSC_TRUE;
788:       xdmf_topo   = PETSC_TRUE;
789:       break;
790:     case PETSC_VIEWER_HDF5_XDMF:
791:       xdmf_topo   = PETSC_TRUE;
792:       break;
793:     case PETSC_VIEWER_HDF5_PETSC:
794:       petsc_topo  = PETSC_TRUE;
795:       break;
796:     case PETSC_VIEWER_DEFAULT:
797:     case PETSC_VIEWER_NATIVE:
798:       viz_geom    = PETSC_TRUE;
799:       xdmf_topo   = PETSC_TRUE;
800:       petsc_topo  = PETSC_TRUE;
801:       break;
802:     default:
803:       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 output.", PetscViewerFormats[format]);
804:   }

806:   if (viz_geom)   {DMPlexWriteCoordinates_Vertices_HDF5_Static(dm, viewer);}
807:   if (xdmf_topo)  {DMPlexWriteTopology_Vertices_HDF5_Static(dm, viewer);}
808:   if (petsc_topo) {DMPlexWriteTopology_HDF5_Static(dm, globalPointNumbers, viewer);}

810:   ISDestroy(&globalPointNumbers);
811:   return(0);
812: }

814: typedef struct {
815:   PetscMPIInt rank;
816:   DM          dm;
817:   PetscViewer viewer;
818:   DMLabel     label;
819: } LabelCtx;

821: static herr_t ReadLabelStratumHDF5_Static(hid_t g_id, const char *name, const H5L_info_t *info, void *op_data)
822: {
823:   PetscViewer     viewer = ((LabelCtx *) op_data)->viewer;
824:   DMLabel         label  = ((LabelCtx *) op_data)->label;
825:   IS              stratumIS;
826:   const PetscInt *ind;
827:   PetscInt        value, N, i;
828:   const char     *lname;
829:   char            group[PETSC_MAX_PATH_LEN];
830:   PetscErrorCode  ierr;

832:   PetscOptionsStringToInt(name, &value);
833:   ISCreate(PetscObjectComm((PetscObject) viewer), &stratumIS);
834:   PetscObjectSetName((PetscObject) stratumIS, "indices");
835:   PetscObjectGetName((PetscObject) label, &lname);
836:   PetscSNPrintf(group, PETSC_MAX_PATH_LEN, "/labels/%s/%s", lname, name);
837:   PetscViewerHDF5PushGroup(viewer, group);
838:   {
839:     /* Force serial load */
840:     PetscViewerHDF5ReadSizes(viewer, "indices", NULL, &N);
841:     PetscLayoutSetLocalSize(stratumIS->map, !((LabelCtx *) op_data)->rank ? N : 0);
842:     PetscLayoutSetSize(stratumIS->map, N);
843:   }
844:   ISLoad(stratumIS, viewer);
845:   PetscViewerHDF5PopGroup(viewer);
846:   ISGetLocalSize(stratumIS, &N);
847:   ISGetIndices(stratumIS, &ind);
848:   for (i = 0; i < N; ++i) {DMLabelSetValue(label, ind[i], value);}
849:   ISRestoreIndices(stratumIS, &ind);
850:   ISDestroy(&stratumIS);
851:   return 0;
852: }

854: static herr_t ReadLabelHDF5_Static(hid_t g_id, const char *name, const H5L_info_t *info, void *op_data)
855: {
856:   DM             dm  = ((LabelCtx *) op_data)->dm;
857:   hsize_t        idx = 0;
859:   herr_t         err;

861:   DMCreateLabel(dm, name); if (ierr) return (herr_t) ierr;
862:   DMGetLabel(dm, name, &((LabelCtx *) op_data)->label); if (ierr) return (herr_t) ierr;
863:   PetscStackCall("H5Literate_by_name",err = H5Literate_by_name(g_id, name, H5_INDEX_NAME, H5_ITER_NATIVE, &idx, ReadLabelStratumHDF5_Static, op_data, 0));
864:   return err;
865: }

867: PetscErrorCode DMPlexLoadLabels_HDF5_Internal(DM dm, PetscViewer viewer)
868: {
869:   LabelCtx        ctx;
870:   hid_t           fileId, groupId;
871:   hsize_t         idx = 0;
872:   PetscErrorCode  ierr;

875:   MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &ctx.rank);
876:   ctx.dm     = dm;
877:   ctx.viewer = viewer;
878:   PetscViewerHDF5PushGroup(viewer, "/labels");
879:   PetscViewerHDF5OpenGroup(viewer, &fileId, &groupId);
880:   PetscStackCallHDF5(H5Literate,(groupId, H5_INDEX_NAME, H5_ITER_NATIVE, &idx, ReadLabelHDF5_Static, &ctx));
881:   PetscStackCallHDF5(H5Gclose,(groupId));
882:   PetscViewerHDF5PopGroup(viewer);
883:   return(0);
884: }

886: /* The first version will read everything onto proc 0, letting the user distribute
887:    The next will create a naive partition, and then rebalance after reading
888: */
889: PetscErrorCode DMPlexLoad_HDF5_Internal(DM dm, PetscViewer viewer)
890: {
891:   PetscSection    coordSection;
892:   Vec             coordinates;
893:   IS              orderIS, conesIS, cellsIS, orntsIS;
894:   const PetscInt *order, *cones, *cells, *ornts;
895:   PetscReal       lengthScale;
896:   PetscInt       *cone, *ornt;
897:   PetscInt        dim, spatialDim, N, numVertices, vStart, vEnd, v, pEnd, p, q, maxConeSize = 0, c;
898:   PetscMPIInt     rank;
899:   PetscErrorCode  ierr;

902:   MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
903:   /* Read toplogy */
904:   PetscViewerHDF5PushGroup(viewer, "/topology");
905:   ISCreate(PetscObjectComm((PetscObject) dm), &orderIS);
906:   PetscObjectSetName((PetscObject) orderIS, "order");
907:   ISCreate(PetscObjectComm((PetscObject) dm), &conesIS);
908:   PetscObjectSetName((PetscObject) conesIS, "cones");
909:   ISCreate(PetscObjectComm((PetscObject) dm), &cellsIS);
910:   PetscObjectSetName((PetscObject) cellsIS, "cells");
911:   ISCreate(PetscObjectComm((PetscObject) dm), &orntsIS);
912:   PetscObjectSetName((PetscObject) orntsIS, "orientation");
913:   PetscViewerHDF5ReadObjectAttribute(viewer, (PetscObject) cellsIS, "cell_dim", PETSC_INT, (void *) &dim);
914:   DMSetDimension(dm, dim);
915:   {
916:     /* Force serial load */
917:     PetscViewerHDF5ReadSizes(viewer, "order", NULL, &pEnd);
918:     PetscLayoutSetLocalSize(orderIS->map, !rank ? pEnd : 0);
919:     PetscLayoutSetSize(orderIS->map, pEnd);
920:     PetscViewerHDF5ReadSizes(viewer, "cones", NULL, &pEnd);
921:     PetscLayoutSetLocalSize(conesIS->map, !rank ? pEnd : 0);
922:     PetscLayoutSetSize(conesIS->map, pEnd);
923:     pEnd = !rank ? pEnd : 0;
924:     PetscViewerHDF5ReadSizes(viewer, "cells", NULL, &N);
925:     PetscLayoutSetLocalSize(cellsIS->map, !rank ? N : 0);
926:     PetscLayoutSetSize(cellsIS->map, N);
927:     PetscViewerHDF5ReadSizes(viewer, "orientation", NULL, &N);
928:     PetscLayoutSetLocalSize(orntsIS->map, !rank ? N : 0);
929:     PetscLayoutSetSize(orntsIS->map, N);
930:   }
931:   ISLoad(orderIS, viewer);
932:   ISLoad(conesIS, viewer);
933:   ISLoad(cellsIS, viewer);
934:   ISLoad(orntsIS, viewer);
935:   PetscViewerHDF5PopGroup(viewer);
936:   /* Read geometry */
937:   PetscViewerHDF5PushGroup(viewer, "/geometry");
938:   VecCreate(PetscObjectComm((PetscObject) dm), &coordinates);
939:   PetscObjectSetName((PetscObject) coordinates, "vertices");
940:   {
941:     /* Force serial load */
942:     PetscViewerHDF5ReadSizes(viewer, "vertices", &spatialDim, &N);
943:     VecSetSizes(coordinates, !rank ? N : 0, N);
944:     VecSetBlockSize(coordinates, spatialDim);
945:   }
946:   VecLoad(coordinates, viewer);
947:   PetscViewerHDF5PopGroup(viewer);
948:   DMPlexGetScale(dm, PETSC_UNIT_LENGTH, &lengthScale);
949:   VecScale(coordinates, 1.0/lengthScale);
950:   VecGetLocalSize(coordinates, &numVertices);
951:   VecGetBlockSize(coordinates, &spatialDim);
952:   numVertices /= spatialDim;
953:   /* Create Plex */
954:   DMPlexSetChart(dm, 0, pEnd);
955:   ISGetIndices(orderIS, &order);
956:   ISGetIndices(conesIS, &cones);
957:   for (p = 0; p < pEnd; ++p) {
958:     DMPlexSetConeSize(dm, order[p], cones[p]);
959:     maxConeSize = PetscMax(maxConeSize, cones[p]);
960:   }
961:   DMSetUp(dm);
962:   ISGetIndices(cellsIS, &cells);
963:   ISGetIndices(orntsIS, &ornts);
964:   PetscMalloc2(maxConeSize,&cone,maxConeSize,&ornt);
965:   for (p = 0, q = 0; p < pEnd; ++p) {
966:     for (c = 0; c < cones[p]; ++c, ++q) {cone[c] = cells[q]; ornt[c] = ornts[q];}
967:     DMPlexSetCone(dm, order[p], cone);
968:     DMPlexSetConeOrientation(dm, order[p], ornt);
969:   }
970:   PetscFree2(cone,ornt);
971:   ISRestoreIndices(orderIS, &order);
972:   ISRestoreIndices(conesIS, &cones);
973:   ISRestoreIndices(cellsIS, &cells);
974:   ISRestoreIndices(orntsIS, &ornts);
975:   ISDestroy(&orderIS);
976:   ISDestroy(&conesIS);
977:   ISDestroy(&cellsIS);
978:   ISDestroy(&orntsIS);
979:   DMPlexSymmetrize(dm);
980:   DMPlexStratify(dm);
981:   /* Create coordinates */
982:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
983:   if (numVertices != vEnd - vStart) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of coordinates loaded %d does not match number of vertices %d", numVertices, vEnd - vStart);
984:   DMGetCoordinateSection(dm, &coordSection);
985:   PetscSectionSetNumFields(coordSection, 1);
986:   PetscSectionSetFieldComponents(coordSection, 0, spatialDim);
987:   PetscSectionSetChart(coordSection, vStart, vEnd);
988:   for (v = vStart; v < vEnd; ++v) {
989:     PetscSectionSetDof(coordSection, v, spatialDim);
990:     PetscSectionSetFieldDof(coordSection, v, 0, spatialDim);
991:   }
992:   PetscSectionSetUp(coordSection);
993:   DMSetCoordinates(dm, coordinates);
994:   VecDestroy(&coordinates);
995:   /* Read Labels */
996:   DMPlexLoadLabels_HDF5_Internal(dm, viewer);
997:   return(0);
998: }
999: #endif