Actual source code: plexhdf5.c
petsc-3.9.4 2018-09-11
1: #include <petsc/private/dmpleximpl.h>
2: #include <petsc/private/isimpl.h>
3: #include <petsc/private/vecimpl.h>
4: #include <petscviewerhdf5.h>
6: PETSC_EXTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);
8: #if defined(PETSC_HAVE_HDF5)
9: static PetscErrorCode DMSequenceView_HDF5(DM dm, const char *seqname, PetscInt seqnum, PetscScalar value, PetscViewer viewer)
10: {
11: Vec stamp;
12: PetscMPIInt rank;
16: if (seqnum < 0) return(0);
17: MPI_Comm_rank(PetscObjectComm((PetscObject) viewer), &rank);
18: VecCreateMPI(PetscObjectComm((PetscObject) viewer), rank ? 0 : 1, 1, &stamp);
19: VecSetBlockSize(stamp, 1);
20: PetscObjectSetName((PetscObject) stamp, seqname);
21: if (!rank) {
22: PetscReal timeScale;
23: PetscBool istime;
25: PetscStrncmp(seqname, "time", 5, &istime);
26: if (istime) {DMPlexGetScale(dm, PETSC_UNIT_TIME, &timeScale); value *= timeScale;}
27: VecSetValue(stamp, 0, value, INSERT_VALUES);
28: }
29: VecAssemblyBegin(stamp);
30: VecAssemblyEnd(stamp);
31: PetscViewerHDF5PushGroup(viewer, "/");
32: PetscViewerHDF5SetTimestep(viewer, seqnum);
33: VecView(stamp, viewer);
34: PetscViewerHDF5PopGroup(viewer);
35: VecDestroy(&stamp);
36: return(0);
37: }
39: PetscErrorCode DMSequenceLoad_HDF5_Internal(DM dm, const char *seqname, PetscInt seqnum, PetscScalar *value, PetscViewer viewer)
40: {
41: Vec stamp;
42: PetscMPIInt rank;
46: if (seqnum < 0) return(0);
47: MPI_Comm_rank(PetscObjectComm((PetscObject) viewer), &rank);
48: VecCreateMPI(PetscObjectComm((PetscObject) viewer), rank ? 0 : 1, 1, &stamp);
49: VecSetBlockSize(stamp, 1);
50: PetscObjectSetName((PetscObject) stamp, seqname);
51: PetscViewerHDF5PushGroup(viewer, "/");
52: PetscViewerHDF5SetTimestep(viewer, seqnum);
53: VecLoad(stamp, viewer);
54: PetscViewerHDF5PopGroup(viewer);
55: if (!rank) {
56: const PetscScalar *a;
57: PetscReal timeScale;
58: PetscBool istime;
60: VecGetArrayRead(stamp, &a);
61: *value = a[0];
62: VecRestoreArrayRead(stamp, &a);
63: PetscStrncmp(seqname, "time", 5, &istime);
64: if (istime) {DMPlexGetScale(dm, PETSC_UNIT_TIME, &timeScale); *value /= timeScale;}
65: }
66: VecDestroy(&stamp);
67: return(0);
68: }
70: PetscErrorCode VecView_Plex_Local_HDF5_Internal(Vec v, PetscViewer viewer)
71: {
72: DM dm;
73: DM dmBC;
74: PetscSection section, sectionGlobal;
75: Vec gv;
76: const char *name;
77: PetscViewerVTKFieldType ft;
78: PetscViewerFormat format;
79: PetscInt seqnum;
80: PetscReal seqval;
81: PetscBool isseq;
82: PetscErrorCode ierr;
85: PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);
86: VecGetDM(v, &dm);
87: DMGetDefaultSection(dm, §ion);
88: DMGetOutputSequenceNumber(dm, &seqnum, &seqval);
89: PetscViewerHDF5SetTimestep(viewer, seqnum);
90: DMSequenceView_HDF5(dm, "time", seqnum, (PetscScalar) seqval, viewer);
91: PetscViewerGetFormat(viewer, &format);
92: DMGetOutputDM(dm, &dmBC);
93: DMGetDefaultGlobalSection(dmBC, §ionGlobal);
94: DMGetGlobalVector(dmBC, &gv);
95: PetscObjectGetName((PetscObject) v, &name);
96: PetscObjectSetName((PetscObject) gv, name);
97: DMLocalToGlobalBegin(dmBC, v, INSERT_VALUES, gv);
98: DMLocalToGlobalEnd(dmBC, v, INSERT_VALUES, gv);
99: PetscObjectTypeCompare((PetscObject) gv, VECSEQ, &isseq);
100: if (isseq) {VecView_Seq(gv, viewer);}
101: else {VecView_MPI(gv, viewer);}
102: if (format == PETSC_VIEWER_HDF5_VIZ) {
103: /* Output visualization representation */
104: PetscInt numFields, f;
105: DMLabel cutLabel;
107: PetscSectionGetNumFields(section, &numFields);
108: DMGetLabel(dm, "periodic_cut", &cutLabel);
109: for (f = 0; f < numFields; ++f) {
110: Vec subv;
111: IS is;
112: const char *fname, *fgroup;
113: char subname[PETSC_MAX_PATH_LEN];
114: char group[PETSC_MAX_PATH_LEN];
115: PetscInt pStart, pEnd;
116: PetscBool flag;
118: DMPlexGetFieldType_Internal(dm, section, f, &pStart, &pEnd, &ft);
119: fgroup = (ft == PETSC_VTK_POINT_VECTOR_FIELD) || (ft == PETSC_VTK_POINT_FIELD) ? "/vertex_fields" : "/cell_fields";
120: PetscSectionGetFieldName(section, f, &fname);
121: if (!fname) continue;
122: PetscViewerHDF5PushGroup(viewer, fgroup);
123: if (cutLabel) {
124: const PetscScalar *ga;
125: PetscScalar *suba;
126: PetscInt Nc, gstart, subSize = 0, extSize = 0, subOff = 0, newOff = 0, p;
128: PetscSectionGetFieldComponents(section, f, &Nc);
129: for (p = pStart; p < pEnd; ++p) {
130: PetscInt gdof, fdof = 0, val;
132: PetscSectionGetDof(sectionGlobal, p, &gdof);
133: if (gdof > 0) {PetscSectionGetFieldDof(section, p, f, &fdof);}
134: subSize += fdof;
135: DMLabelGetValue(cutLabel, p, &val);
136: if (val == 1) extSize += fdof;
137: }
138: VecCreate(PetscObjectComm((PetscObject) gv), &subv);
139: VecSetSizes(subv, subSize+extSize, PETSC_DETERMINE);
140: VecSetBlockSize(subv, Nc);
141: VecSetType(subv, VECSTANDARD);
142: VecGetOwnershipRange(gv, &gstart, NULL);
143: VecGetArrayRead(gv, &ga);
144: VecGetArray(subv, &suba);
145: for (p = pStart; p < pEnd; ++p) {
146: PetscInt gdof, goff, val;
148: PetscSectionGetDof(sectionGlobal, p, &gdof);
149: if (gdof > 0) {
150: PetscInt fdof, fc, f2, poff = 0;
152: PetscSectionGetOffset(sectionGlobal, p, &goff);
153: /* Can get rid of this loop by storing field information in the global section */
154: for (f2 = 0; f2 < f; ++f2) {
155: PetscSectionGetFieldDof(section, p, f2, &fdof);
156: poff += fdof;
157: }
158: PetscSectionGetFieldDof(section, p, f, &fdof);
159: for (fc = 0; fc < fdof; ++fc, ++subOff) suba[subOff] = ga[goff+poff+fc - gstart];
160: DMLabelGetValue(cutLabel, p, &val);
161: if (val == 1) {
162: for (fc = 0; fc < fdof; ++fc, ++newOff) suba[subSize+newOff] = ga[goff+poff+fc - gstart];
163: }
164: }
165: }
166: VecRestoreArrayRead(gv, &ga);
167: VecRestoreArray(subv, &suba);
168: } else {
169: PetscSectionGetField_Internal(section, sectionGlobal, gv, f, pStart, pEnd, &is, &subv);
170: }
171: PetscStrncpy(subname, name,sizeof(subname));
172: PetscStrlcat(subname, "_",sizeof(subname));
173: PetscStrlcat(subname, fname,sizeof(subname));
174: PetscObjectSetName((PetscObject) subv, subname);
175: if (isseq) {VecView_Seq(subv, viewer);}
176: else {VecView_MPI(subv, viewer);}
177: if (cutLabel) {
178: VecDestroy(&subv);
179: } else {
180: PetscSectionRestoreField_Internal(section, sectionGlobal, gv, f, pStart, pEnd, &is, &subv);
181: }
182: PetscViewerHDF5PopGroup(viewer);
183: PetscSNPrintf(group, PETSC_MAX_PATH_LEN, "%s/%s", fgroup, subname);
184: PetscViewerHDF5HasAttribute(viewer, group, "vector_field_type", &flag);
185: if (!flag) {
186: if ((ft == PETSC_VTK_POINT_VECTOR_FIELD) || (ft == PETSC_VTK_CELL_VECTOR_FIELD)) {
187: PetscViewerHDF5WriteAttribute(viewer, group, "vector_field_type", PETSC_STRING, "vector");
188: } else {
189: PetscViewerHDF5WriteAttribute(viewer, group, "vector_field_type", PETSC_STRING, "scalar");
190: }
191: }
192: }
193: }
194: DMRestoreGlobalVector(dmBC, &gv);
195: return(0);
196: }
198: PetscErrorCode VecView_Plex_HDF5_Internal(Vec v, PetscViewer viewer)
199: {
200: DM dm;
201: Vec locv;
202: const char *name;
203: PetscReal time;
207: VecGetDM(v, &dm);
208: DMGetLocalVector(dm, &locv);
209: PetscObjectGetName((PetscObject) v, &name);
210: PetscObjectSetName((PetscObject) locv, name);
211: DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);
212: DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);
213: DMGetOutputSequenceNumber(dm, NULL, &time);
214: DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);
215: PetscViewerHDF5PushGroup(viewer, "/fields");
216: PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_VIZ);
217: VecView_Plex_Local_HDF5_Internal(locv, viewer);
218: PetscViewerPopFormat(viewer);
219: PetscViewerHDF5PopGroup(viewer);
220: DMRestoreLocalVector(dm, &locv);
221: return(0);
222: }
224: PetscErrorCode VecView_Plex_HDF5_Native_Internal(Vec v, PetscViewer viewer)
225: {
226: PetscBool isseq;
230: PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);
231: PetscViewerHDF5PushGroup(viewer, "/fields");
232: if (isseq) {VecView_Seq(v, viewer);}
233: else {VecView_MPI(v, viewer);}
234: PetscViewerHDF5PopGroup(viewer);
235: return(0);
236: }
238: PetscErrorCode VecLoad_Plex_HDF5_Internal(Vec v, PetscViewer viewer)
239: {
240: DM dm;
241: Vec locv;
242: const char *name;
243: PetscInt seqnum;
247: VecGetDM(v, &dm);
248: DMGetLocalVector(dm, &locv);
249: PetscObjectGetName((PetscObject) v, &name);
250: PetscObjectSetName((PetscObject) locv, name);
251: DMGetOutputSequenceNumber(dm, &seqnum, NULL);
252: PetscViewerHDF5SetTimestep(viewer, seqnum);
253: PetscViewerHDF5PushGroup(viewer, "/fields");
254: VecLoad_Plex_Local(locv, viewer);
255: PetscViewerHDF5PopGroup(viewer);
256: DMLocalToGlobalBegin(dm, locv, INSERT_VALUES, v);
257: DMLocalToGlobalEnd(dm, locv, INSERT_VALUES, v);
258: DMRestoreLocalVector(dm, &locv);
259: return(0);
260: }
262: PetscErrorCode VecLoad_Plex_HDF5_Native_Internal(Vec v, PetscViewer viewer)
263: {
264: DM dm;
265: PetscInt seqnum;
269: VecGetDM(v, &dm);
270: DMGetOutputSequenceNumber(dm, &seqnum, NULL);
271: PetscViewerHDF5SetTimestep(viewer, seqnum);
272: PetscViewerHDF5PushGroup(viewer, "/fields");
273: VecLoad_Default(v, viewer);
274: PetscViewerHDF5PopGroup(viewer);
275: return(0);
276: }
278: static PetscErrorCode DMPlexWriteTopology_HDF5_Static(DM dm, IS globalPointNumbers, PetscViewer viewer)
279: {
280: IS orderIS, conesIS, cellsIS, orntsIS;
281: const PetscInt *gpoint;
282: PetscInt *order, *sizes, *cones, *ornts;
283: PetscInt dim, pStart, pEnd, p, conesSize = 0, cellsSize = 0, c = 0, s = 0;
284: PetscErrorCode ierr;
287: ISGetIndices(globalPointNumbers, &gpoint);
288: DMGetDimension(dm, &dim);
289: DMPlexGetChart(dm, &pStart, &pEnd);
290: for (p = pStart; p < pEnd; ++p) {
291: if (gpoint[p] >= 0) {
292: PetscInt coneSize;
294: DMPlexGetConeSize(dm, p, &coneSize);
295: conesSize += 1;
296: cellsSize += coneSize;
297: }
298: }
299: PetscMalloc1(conesSize, &order);
300: PetscMalloc1(conesSize, &sizes);
301: PetscMalloc1(cellsSize, &cones);
302: PetscMalloc1(cellsSize, &ornts);
303: for (p = pStart; p < pEnd; ++p) {
304: if (gpoint[p] >= 0) {
305: const PetscInt *cone, *ornt;
306: PetscInt coneSize, cp;
308: DMPlexGetConeSize(dm, p, &coneSize);
309: DMPlexGetCone(dm, p, &cone);
310: DMPlexGetConeOrientation(dm, p, &ornt);
311: order[s] = gpoint[p];
312: sizes[s++] = coneSize;
313: for (cp = 0; cp < coneSize; ++cp, ++c) {cones[c] = gpoint[cone[cp]] < 0 ? -(gpoint[cone[cp]]+1) : gpoint[cone[cp]]; ornts[c] = ornt[cp];}
314: }
315: }
316: if (s != conesSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Total number of points %d != %d", s, conesSize);
317: if (c != cellsSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Total number of cone points %d != %d", c, cellsSize);
318: ISCreateGeneral(PetscObjectComm((PetscObject) dm), conesSize, order, PETSC_OWN_POINTER, &orderIS);
319: PetscObjectSetName((PetscObject) orderIS, "order");
320: ISCreateGeneral(PetscObjectComm((PetscObject) dm), conesSize, sizes, PETSC_OWN_POINTER, &conesIS);
321: PetscObjectSetName((PetscObject) conesIS, "cones");
322: ISCreateGeneral(PetscObjectComm((PetscObject) dm), cellsSize, cones, PETSC_OWN_POINTER, &cellsIS);
323: PetscObjectSetName((PetscObject) cellsIS, "cells");
324: ISCreateGeneral(PetscObjectComm((PetscObject) dm), cellsSize, ornts, PETSC_OWN_POINTER, &orntsIS);
325: PetscObjectSetName((PetscObject) orntsIS, "orientation");
326: PetscViewerHDF5PushGroup(viewer, "/topology");
327: ISView(orderIS, viewer);
328: ISView(conesIS, viewer);
329: ISView(cellsIS, viewer);
330: ISView(orntsIS, viewer);
331: PetscViewerHDF5PopGroup(viewer);
332: ISDestroy(&orderIS);
333: ISDestroy(&conesIS);
334: ISDestroy(&cellsIS);
335: ISDestroy(&orntsIS);
336: ISRestoreIndices(globalPointNumbers, &gpoint);
338: PetscViewerHDF5WriteAttribute(viewer, "/topology/cells", "cell_dim", PETSC_INT, (void *) &dim);
339: return(0);
340: }
342: static PetscErrorCode DMPlexWriteTopology_Vertices_HDF5_Static(DM dm, DMLabel label, PetscInt labelId, PetscViewer viewer)
343: {
344: DMLabel cutLabel;
345: PetscSection cSection;
346: IS cellIS, globalVertexNumbers;
347: const PetscInt *gvertex;
348: PetscInt *vertices;
349: IS cutvertices;
350: const PetscInt *cutverts;
351: PetscInt dim, depth, vStart, vEnd, vExtra = 0, v, cellHeight, cStart, cMax, cEnd, cell, conesSize = 0, numCornersLocal = 0, numCorners;
352: hid_t fileId, groupId;
353: PetscErrorCode ierr;
356: DMGetDimension(dm, &dim);
357: DMPlexGetDepth(dm, &depth);
358: DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
359: DMGetCoordinateSection(dm, &cSection);
360: DMPlexGetVTKCellHeight(dm, &cellHeight);
361: DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
362: DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);
363: if (cMax >= 0) cEnd = PetscMin(cEnd, cMax);
364: for (cell = cStart; cell < cEnd; ++cell) {
365: PetscInt *closure = NULL;
366: PetscInt closureSize, v, Nc = 0;
368: if (label) {
369: PetscInt value;
370: DMLabelGetValue(label, cell, &value);
371: if (value == labelId) continue;
372: }
373: DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);
374: for (v = 0; v < closureSize*2; v += 2) {
375: if ((closure[v] >= vStart) && (closure[v] < vEnd)) ++Nc;
376: }
377: DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);
378: conesSize += Nc;
379: if (!numCornersLocal) numCornersLocal = Nc;
380: else if (numCornersLocal != Nc) numCornersLocal = 1;
381: }
382: MPIU_Allreduce(&numCornersLocal, &numCorners, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));
383: if (numCornersLocal && (numCornersLocal != numCorners || numCorners == 1)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Visualization topology currently only supports identical cell shapes");
385: PetscViewerHDF5PushGroup(viewer, "/viz");
386: PetscViewerHDF5OpenGroup(viewer, &fileId, &groupId);
387: PetscStackCallHDF5(H5Gclose,(groupId));
388: PetscViewerHDF5PopGroup(viewer);
390: DMGetLabel(dm, "periodic_cut", &cutLabel);
391: if (cutLabel) {
392: DMLabelGetStratumIS(cutLabel, 1, &cutvertices);
393: ISGetIndices(cutvertices, &cutverts);
394: ISGetLocalSize(cutvertices, &vExtra);
395: }
396: DMPlexGetVertexNumbering(dm, &globalVertexNumbers);
397: ISGetIndices(globalVertexNumbers, &gvertex);
398: PetscMalloc1(conesSize, &vertices);
399: for (cell = cStart, v = 0; cell < cEnd; ++cell) {
400: PetscInt *closure = NULL;
401: PetscInt closureSize, Nc = 0, p;
402: PetscBool replace = PETSC_FALSE;
404: if (label) {
405: PetscInt value;
406: DMLabelGetValue(label, cell, &value);
407: if (value == labelId) continue;
408: }
409: if (cutLabel) {
410: PetscInt value;
411: DMLabelGetValue(cutLabel, cell, &value);
412: if (value == 2) replace = PETSC_TRUE;
413: }
414: DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);
415: for (p = 0; p < closureSize*2; p += 2) {
416: if ((closure[p] >= vStart) && (closure[p] < vEnd)) {
417: closure[Nc++] = closure[p];
418: }
419: }
420: DMPlexInvertCell_Internal(dim, Nc, closure);
421: for (p = 0; p < Nc; ++p) {
422: const PetscInt gv = gvertex[closure[p] - vStart];
423: vertices[v++] = gv < 0 ? -(gv+1) : gv;
424: if (replace) {
425: PetscInt newv;
426: PetscFindInt(closure[p], vExtra, cutverts, &newv);
427: if (newv >= 0) vertices[v-1] = vEnd - vStart + newv;
428: }
429: }
430: DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);
431: }
432: if (cutLabel) {
433: ISRestoreIndices(cutvertices, &cutverts);
434: ISDestroy(&cutvertices);
435: }
436: if (v != conesSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Total number of cell vertices %d != %d", v, conesSize);
437: ISCreateGeneral(PetscObjectComm((PetscObject) dm), conesSize, vertices, PETSC_OWN_POINTER, &cellIS);
438: PetscLayoutSetBlockSize(cellIS->map, numCorners);
439: PetscObjectSetName((PetscObject) cellIS, "cells");
440: PetscViewerHDF5PushGroup(viewer, "/viz/topology");
441: ISView(cellIS, viewer);
442: PetscViewerHDF5WriteAttribute(viewer, "/viz/topology/cells", "cell_corners", PETSC_INT, (void *) &numCorners);
443: ISDestroy(&cellIS);
445: PetscViewerHDF5WriteAttribute(viewer, "/viz/topology/cells", "cell_dim", PETSC_INT, (void *) &dim);
446: return(0);
447: }
449: static PetscErrorCode DMPlexWriteCoordinates_HDF5_Static(DM dm, PetscViewer viewer)
450: {
451: DM cdm;
452: Vec coordinates, newcoords;
453: PetscReal lengthScale;
454: PetscInt m, M, bs;
458: DMPlexGetScale(dm, PETSC_UNIT_LENGTH, &lengthScale);
459: DMGetCoordinateDM(dm, &cdm);
460: DMGetCoordinates(dm, &coordinates);
461: VecCreate(PetscObjectComm((PetscObject) coordinates), &newcoords);
462: PetscObjectSetName((PetscObject) newcoords, "vertices");
463: VecGetSize(coordinates, &M);
464: VecGetLocalSize(coordinates, &m);
465: VecSetSizes(newcoords, m, M);
466: VecGetBlockSize(coordinates, &bs);
467: VecSetBlockSize(newcoords, bs);
468: VecSetType(newcoords,VECSTANDARD);
469: VecCopy(coordinates, newcoords);
470: VecScale(newcoords, lengthScale);
471: /* Did not use DMGetGlobalVector() in order to bypass default group assignment */
472: PetscViewerHDF5PushGroup(viewer, "/geometry");
473: PetscViewerPushFormat(viewer, PETSC_VIEWER_NATIVE);
474: VecView(newcoords, viewer);
475: PetscViewerPopFormat(viewer);
476: PetscViewerHDF5PopGroup(viewer);
477: VecDestroy(&newcoords);
478: return(0);
479: }
481: static PetscErrorCode DMPlexWriteCoordinates_Vertices_HDF5_Static(DM dm, PetscViewer viewer)
482: {
483: Vec coordinates, newcoords;
484: PetscSection cSection;
485: PetscScalar *coords, *ncoords;
486: DMLabel cutLabel;
487: const PetscReal *L;
488: const DMBoundaryType *bd;
489: PetscReal lengthScale;
490: PetscInt vStart, vEnd, vExtra = 0, v, bs, coordSize, dof, off, d;
491: PetscBool localized, embedded;
492: hid_t fileId, groupId;
493: PetscErrorCode ierr;
496: DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
497: DMPlexGetScale(dm, PETSC_UNIT_LENGTH, &lengthScale);
498: DMGetCoordinatesLocal(dm, &coordinates);
499: VecGetBlockSize(coordinates, &bs);
500: VecGetLocalSize(coordinates, &coordSize);
501: DMGetCoordinatesLocalized(dm,&localized);
502: if (localized == PETSC_FALSE) return(0);
503: DMGetLabel(dm, "periodic_cut", &cutLabel);
504: if (cutLabel) {
505: IS vertices;
506: const PetscInt *verts;
507: PetscInt n;
509: DMLabelGetStratumIS(cutLabel, 1, &vertices);
510: ISGetIndices(vertices, &verts);
511: ISGetLocalSize(vertices, &n);
512: for (v = 0; v < n; ++v) {
513: if ((verts[v] >= vStart) && (verts[v] < vEnd)) ++vExtra;
514: }
515: ISRestoreIndices(vertices, &verts);
516: ISDestroy(&vertices);
517: }
518: DMGetPeriodicity(dm, NULL, NULL, &L, &bd);
519: DMGetCoordinateSection(dm, &cSection);
520: VecCreate(PetscObjectComm((PetscObject) coordinates), &newcoords);
521: PetscSectionGetDof(cSection, vStart, &dof);
522: embedded = (PetscBool) (L && dof == 2 && !cutLabel);
523: coordSize = 0;
524: coordSize += dof*vExtra;
525: for (v = vStart; v < vEnd; ++v) {
526: PetscSectionGetDof(cSection, v, &dof);
527: if (embedded) coordSize += dof+1;
528: else coordSize += dof;
529: }
530: if (embedded) {VecSetBlockSize(newcoords, bs+1);}
531: else {VecSetBlockSize(newcoords, bs);}
532: VecSetSizes(newcoords, coordSize, PETSC_DETERMINE);
533: VecSetType(newcoords, VECSTANDARD);
534: VecGetArray(coordinates, &coords);
535: VecGetArray(newcoords, &ncoords);
536: coordSize = 0;
537: for (v = vStart; v < vEnd; ++v) {
538: PetscSectionGetDof(cSection, v, &dof);
539: PetscSectionGetOffset(cSection, v, &off);
540: if (embedded) {
541: if ((bd[0] == DM_BOUNDARY_PERIODIC) && (bd[1] == DM_BOUNDARY_PERIODIC)) {
542: PetscReal theta, phi, r, R;
543: /* XY-periodic */
544: /* Suppose its an y-z circle, then
545: \hat r = (0, cos(th), sin(th)) \hat x = (1, 0, 0)
546: and the circle in that plane is
547: \hat r cos(phi) + \hat x sin(phi) */
548: theta = 2.0*PETSC_PI*PetscRealPart(coords[off+1])/L[1];
549: phi = 2.0*PETSC_PI*PetscRealPart(coords[off+0])/L[0];
550: r = L[0]/(2.0*PETSC_PI * 2.0*L[1]);
551: R = L[1]/(2.0*PETSC_PI);
552: ncoords[coordSize++] = PetscSinReal(phi) * r;
553: ncoords[coordSize++] = -PetscCosReal(theta) * (R + r * PetscCosReal(phi));
554: ncoords[coordSize++] = PetscSinReal(theta) * (R + r * PetscCosReal(phi));
555: } else if ((bd[0] == DM_BOUNDARY_PERIODIC)) {
556: /* X-periodic */
557: ncoords[coordSize++] = -PetscCosReal(2.0*PETSC_PI*PetscRealPart(coords[off+0])/L[0])*(L[0]/(2.0*PETSC_PI));
558: ncoords[coordSize++] = coords[off+1];
559: ncoords[coordSize++] = PetscSinReal(2.0*PETSC_PI*PetscRealPart(coords[off+0])/L[0])*(L[0]/(2.0*PETSC_PI));
560: } else if ((bd[1] == DM_BOUNDARY_PERIODIC)) {
561: /* Y-periodic */
562: ncoords[coordSize++] = coords[off+0];
563: ncoords[coordSize++] = PetscSinReal(2.0*PETSC_PI*PetscRealPart(coords[off+1])/L[1])*(L[1]/(2.0*PETSC_PI));
564: ncoords[coordSize++] = -PetscCosReal(2.0*PETSC_PI*PetscRealPart(coords[off+1])/L[1])*(L[1]/(2.0*PETSC_PI));
565: } else if ((bd[0] == DM_BOUNDARY_TWIST)) {
566: PetscReal phi, r, R;
567: /* Mobius strip */
568: /* Suppose its an x-z circle, then
569: \hat r = (-cos(phi), 0, sin(phi)) \hat y = (0, 1, 0)
570: and in that plane we rotate by pi as we go around the circle
571: \hat r cos(phi/2) + \hat y sin(phi/2) */
572: phi = 2.0*PETSC_PI*PetscRealPart(coords[off+0])/L[0];
573: R = L[0];
574: r = PetscRealPart(coords[off+1]) - L[1]/2.0;
575: ncoords[coordSize++] = -PetscCosReal(phi) * (R + r * PetscCosReal(phi/2.0));
576: ncoords[coordSize++] = PetscSinReal(phi/2.0) * r;
577: ncoords[coordSize++] = PetscSinReal(phi) * (R + r * PetscCosReal(phi/2.0));
578: } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot handle periodicity in this domain");
579: } else {
580: if (cutLabel) {
581: DMLocalizeCoordinate(dm, &coords[off], PETSC_TRUE, &ncoords[coordSize]);
582: coordSize += dof;
583: } else {
584: for (d = 0; d < dof; ++d, ++coordSize) ncoords[coordSize] = coords[off+d];
585: }
586: }
587: }
588: if (cutLabel) {
589: IS vertices;
590: const PetscInt *verts;
591: PetscInt n;
593: DMLabelGetStratumIS(cutLabel, 1, &vertices);
594: ISGetIndices(vertices, &verts);
595: ISGetLocalSize(vertices, &n);
596: for (v = 0; v < n; ++v) {
597: if ((verts[v] < vStart) || (verts[v] >= vEnd)) continue;
598: PetscSectionGetDof(cSection, verts[v], &dof);
599: PetscSectionGetOffset(cSection, verts[v], &off);
600: for (d = 0; d < dof; ++d, ++coordSize) ncoords[coordSize] = coords[off+d] + ((bd[d] == DM_BOUNDARY_PERIODIC) ? L[d] : 0.0);
601: }
602: ISRestoreIndices(vertices, &verts);
603: ISDestroy(&vertices);
604: }
605: VecRestoreArray(coordinates, &coords);
606: VecRestoreArray(newcoords, &ncoords);
607: PetscObjectSetName((PetscObject) newcoords, "vertices");
608: VecScale(newcoords, lengthScale);
609: PetscViewerHDF5PushGroup(viewer, "/viz");
610: PetscViewerHDF5OpenGroup(viewer, &fileId, &groupId);
611: PetscStackCallHDF5(H5Gclose,(groupId));
612: PetscViewerHDF5PopGroup(viewer);
613: PetscViewerHDF5PushGroup(viewer, "/viz/geometry");
614: VecView(newcoords, viewer);
615: PetscViewerHDF5PopGroup(viewer);
616: VecDestroy(&newcoords);
617: return(0);
618: }
620: /* We only write cells and vertices. Does this screw up parallel reading? */
621: PetscErrorCode DMPlexView_HDF5_Internal(DM dm, PetscViewer viewer)
622: {
623: DMLabel label = NULL;
624: PetscInt labelId = 0;
625: IS globalPointNumbers;
626: const PetscInt *gpoint;
627: PetscInt numLabels, l;
628: hid_t fileId, groupId;
629: PetscViewerFormat format;
630: PetscErrorCode ierr;
633: DMPlexCreatePointNumbering(dm, &globalPointNumbers);
634: PetscViewerGetFormat(viewer, &format);
635: DMPlexWriteCoordinates_HDF5_Static(dm, viewer);
636: if (format == PETSC_VIEWER_HDF5_VIZ) {DMPlexWriteCoordinates_Vertices_HDF5_Static(dm, viewer);}
637: DMPlexWriteTopology_HDF5_Static(dm, globalPointNumbers, viewer);
638: if (format == PETSC_VIEWER_HDF5_VIZ) {DMPlexWriteTopology_Vertices_HDF5_Static(dm, label, labelId, viewer);}
639: /* Write Labels*/
640: ISGetIndices(globalPointNumbers, &gpoint);
641: PetscViewerHDF5PushGroup(viewer, "/labels");
642: PetscViewerHDF5OpenGroup(viewer, &fileId, &groupId);
643: if (groupId != fileId) PetscStackCallHDF5(H5Gclose,(groupId));
644: PetscViewerHDF5PopGroup(viewer);
645: DMGetNumLabels(dm, &numLabels);
646: for (l = 0; l < numLabels; ++l) {
647: DMLabel label;
648: const char *name;
649: IS valueIS, pvalueIS, globalValueIS;
650: const PetscInt *values;
651: PetscInt numValues, v;
652: PetscBool isDepth, output;
653: char group[PETSC_MAX_PATH_LEN];
655: DMGetLabelName(dm, l, &name);
656: DMGetLabelOutput(dm, name, &output);
657: PetscStrncmp(name, "depth", 10, &isDepth);
658: if (isDepth || !output) continue;
659: DMGetLabel(dm, name, &label);
660: PetscSNPrintf(group, PETSC_MAX_PATH_LEN, "/labels/%s", name);
661: PetscViewerHDF5PushGroup(viewer, group);
662: PetscViewerHDF5OpenGroup(viewer, &fileId, &groupId);
663: if (groupId != fileId) PetscStackCallHDF5(H5Gclose,(groupId));
664: PetscViewerHDF5PopGroup(viewer);
665: DMLabelGetValueIS(label, &valueIS);
666: /* Must copy to a new IS on the global comm */
667: ISGetLocalSize(valueIS, &numValues);
668: ISGetIndices(valueIS, &values);
669: ISCreateGeneral(PetscObjectComm((PetscObject) dm), numValues, values, PETSC_COPY_VALUES, &pvalueIS);
670: ISRestoreIndices(valueIS, &values);
671: ISAllGather(pvalueIS, &globalValueIS);
672: ISDestroy(&pvalueIS);
673: ISSortRemoveDups(globalValueIS);
674: ISGetLocalSize(globalValueIS, &numValues);
675: ISGetIndices(globalValueIS, &values);
676: for (v = 0; v < numValues; ++v) {
677: IS stratumIS, globalStratumIS;
678: const PetscInt *spoints = NULL;
679: PetscInt *gspoints, n = 0, gn, p;
680: const char *iname = "indices";
682: PetscSNPrintf(group, PETSC_MAX_PATH_LEN, "/labels/%s/%d", name, values[v]);
683: DMLabelGetStratumIS(label, values[v], &stratumIS);
685: if (stratumIS) {ISGetLocalSize(stratumIS, &n);}
686: if (stratumIS) {ISGetIndices(stratumIS, &spoints);}
687: for (gn = 0, p = 0; p < n; ++p) if (gpoint[spoints[p]] >= 0) ++gn;
688: PetscMalloc1(gn,&gspoints);
689: for (gn = 0, p = 0; p < n; ++p) if (gpoint[spoints[p]] >= 0) gspoints[gn++] = gpoint[spoints[p]];
690: if (stratumIS) {ISRestoreIndices(stratumIS, &spoints);}
691: ISCreateGeneral(PetscObjectComm((PetscObject) dm), gn, gspoints, PETSC_OWN_POINTER, &globalStratumIS);
692: if (stratumIS) {PetscObjectGetName((PetscObject) stratumIS, &iname);}
693: PetscObjectSetName((PetscObject) globalStratumIS, iname);
695: PetscViewerHDF5PushGroup(viewer, group);
696: ISView(globalStratumIS, viewer);
697: PetscViewerHDF5PopGroup(viewer);
698: ISDestroy(&globalStratumIS);
699: ISDestroy(&stratumIS);
700: }
701: ISRestoreIndices(globalValueIS, &values);
702: ISDestroy(&globalValueIS);
703: ISDestroy(&valueIS);
704: }
705: ISRestoreIndices(globalPointNumbers, &gpoint);
706: ISDestroy(&globalPointNumbers);
707: return(0);
708: }
710: typedef struct {
711: PetscMPIInt rank;
712: DM dm;
713: PetscViewer viewer;
714: DMLabel label;
715: } LabelCtx;
717: static herr_t ReadLabelStratumHDF5_Static(hid_t g_id, const char *name, const H5L_info_t *info, void *op_data)
718: {
719: PetscViewer viewer = ((LabelCtx *) op_data)->viewer;
720: DMLabel label = ((LabelCtx *) op_data)->label;
721: IS stratumIS;
722: const PetscInt *ind;
723: PetscInt value, N, i;
724: const char *lname;
725: char group[PETSC_MAX_PATH_LEN];
726: PetscErrorCode ierr;
728: PetscOptionsStringToInt(name, &value);
729: ISCreate(PetscObjectComm((PetscObject) viewer), &stratumIS);
730: PetscObjectSetName((PetscObject) stratumIS, "indices");
731: DMLabelGetName(label, &lname);
732: PetscSNPrintf(group, PETSC_MAX_PATH_LEN, "/labels/%s/%s", lname, name);
733: PetscViewerHDF5PushGroup(viewer, group);
734: {
735: /* Force serial load */
736: PetscViewerHDF5ReadSizes(viewer, "indices", NULL, &N);
737: PetscLayoutSetLocalSize(stratumIS->map, !((LabelCtx *) op_data)->rank ? N : 0);
738: PetscLayoutSetSize(stratumIS->map, N);
739: }
740: ISLoad(stratumIS, viewer);
741: PetscViewerHDF5PopGroup(viewer);
742: ISGetLocalSize(stratumIS, &N);
743: ISGetIndices(stratumIS, &ind);
744: for (i = 0; i < N; ++i) {DMLabelSetValue(label, ind[i], value);}
745: ISRestoreIndices(stratumIS, &ind);
746: ISDestroy(&stratumIS);
747: return 0;
748: }
750: static herr_t ReadLabelHDF5_Static(hid_t g_id, const char *name, const H5L_info_t *info, void *op_data)
751: {
752: DM dm = ((LabelCtx *) op_data)->dm;
753: hsize_t idx = 0;
755: herr_t err;
757: DMCreateLabel(dm, name); if (ierr) return (herr_t) ierr;
758: DMGetLabel(dm, name, &((LabelCtx *) op_data)->label); if (ierr) return (herr_t) ierr;
759: PetscStackCall("H5Literate_by_name",err = H5Literate_by_name(g_id, name, H5_INDEX_NAME, H5_ITER_NATIVE, &idx, ReadLabelStratumHDF5_Static, op_data, 0));
760: return err;
761: }
763: /* The first version will read everything onto proc 0, letting the user distribute
764: The next will create a naive partition, and then rebalance after reading
765: */
766: PetscErrorCode DMPlexLoad_HDF5_Internal(DM dm, PetscViewer viewer)
767: {
768: LabelCtx ctx;
769: PetscSection coordSection;
770: Vec coordinates;
771: IS orderIS, conesIS, cellsIS, orntsIS;
772: const PetscInt *order, *cones, *cells, *ornts;
773: PetscReal lengthScale;
774: PetscInt *cone, *ornt;
775: PetscInt dim, spatialDim, N, numVertices, vStart, vEnd, v, pEnd, p, q, maxConeSize = 0, c;
776: hid_t fileId, groupId;
777: hsize_t idx = 0;
778: PetscMPIInt rank;
779: PetscErrorCode ierr;
782: MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
783: /* Read toplogy */
784: PetscViewerHDF5ReadAttribute(viewer, "/topology/cells", "cell_dim", PETSC_INT, (void *) &dim);
785: DMSetDimension(dm, dim);
786: PetscViewerHDF5PushGroup(viewer, "/topology");
788: ISCreate(PetscObjectComm((PetscObject) dm), &orderIS);
789: PetscObjectSetName((PetscObject) orderIS, "order");
790: ISCreate(PetscObjectComm((PetscObject) dm), &conesIS);
791: PetscObjectSetName((PetscObject) conesIS, "cones");
792: ISCreate(PetscObjectComm((PetscObject) dm), &cellsIS);
793: PetscObjectSetName((PetscObject) cellsIS, "cells");
794: ISCreate(PetscObjectComm((PetscObject) dm), &orntsIS);
795: PetscObjectSetName((PetscObject) orntsIS, "orientation");
796: {
797: /* Force serial load */
798: PetscViewerHDF5ReadSizes(viewer, "order", NULL, &pEnd);
799: PetscLayoutSetLocalSize(orderIS->map, !rank ? pEnd : 0);
800: PetscLayoutSetSize(orderIS->map, pEnd);
801: PetscViewerHDF5ReadSizes(viewer, "cones", NULL, &pEnd);
802: PetscLayoutSetLocalSize(conesIS->map, !rank ? pEnd : 0);
803: PetscLayoutSetSize(conesIS->map, pEnd);
804: pEnd = !rank ? pEnd : 0;
805: PetscViewerHDF5ReadSizes(viewer, "cells", NULL, &N);
806: PetscLayoutSetLocalSize(cellsIS->map, !rank ? N : 0);
807: PetscLayoutSetSize(cellsIS->map, N);
808: PetscViewerHDF5ReadSizes(viewer, "orientation", NULL, &N);
809: PetscLayoutSetLocalSize(orntsIS->map, !rank ? N : 0);
810: PetscLayoutSetSize(orntsIS->map, N);
811: }
812: ISLoad(orderIS, viewer);
813: ISLoad(conesIS, viewer);
814: ISLoad(cellsIS, viewer);
815: ISLoad(orntsIS, viewer);
816: PetscViewerHDF5PopGroup(viewer);
817: /* Read geometry */
818: PetscViewerHDF5PushGroup(viewer, "/geometry");
819: VecCreate(PetscObjectComm((PetscObject) dm), &coordinates);
820: PetscObjectSetName((PetscObject) coordinates, "vertices");
821: {
822: /* Force serial load */
823: PetscViewerHDF5ReadSizes(viewer, "vertices", &spatialDim, &N);
824: VecSetSizes(coordinates, !rank ? N : 0, N);
825: VecSetBlockSize(coordinates, spatialDim);
826: }
827: VecLoad(coordinates, viewer);
828: PetscViewerHDF5PopGroup(viewer);
829: DMPlexGetScale(dm, PETSC_UNIT_LENGTH, &lengthScale);
830: VecScale(coordinates, 1.0/lengthScale);
831: VecGetLocalSize(coordinates, &numVertices);
832: VecGetBlockSize(coordinates, &spatialDim);
833: numVertices /= spatialDim;
834: /* Create Plex */
835: DMPlexSetChart(dm, 0, pEnd);
836: ISGetIndices(orderIS, &order);
837: ISGetIndices(conesIS, &cones);
838: for (p = 0; p < pEnd; ++p) {
839: DMPlexSetConeSize(dm, order[p], cones[p]);
840: maxConeSize = PetscMax(maxConeSize, cones[p]);
841: }
842: DMSetUp(dm);
843: ISGetIndices(cellsIS, &cells);
844: ISGetIndices(orntsIS, &ornts);
845: PetscMalloc2(maxConeSize,&cone,maxConeSize,&ornt);
846: for (p = 0, q = 0; p < pEnd; ++p) {
847: for (c = 0; c < cones[p]; ++c, ++q) {cone[c] = cells[q]; ornt[c] = ornts[q];}
848: DMPlexSetCone(dm, order[p], cone);
849: DMPlexSetConeOrientation(dm, order[p], ornt);
850: }
851: PetscFree2(cone,ornt);
852: ISRestoreIndices(orderIS, &order);
853: ISRestoreIndices(conesIS, &cones);
854: ISRestoreIndices(cellsIS, &cells);
855: ISRestoreIndices(orntsIS, &ornts);
856: ISDestroy(&orderIS);
857: ISDestroy(&conesIS);
858: ISDestroy(&cellsIS);
859: ISDestroy(&orntsIS);
860: DMPlexSymmetrize(dm);
861: DMPlexStratify(dm);
862: /* Create coordinates */
863: DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
864: 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);
865: DMGetCoordinateSection(dm, &coordSection);
866: PetscSectionSetNumFields(coordSection, 1);
867: PetscSectionSetFieldComponents(coordSection, 0, spatialDim);
868: PetscSectionSetChart(coordSection, vStart, vEnd);
869: for (v = vStart; v < vEnd; ++v) {
870: PetscSectionSetDof(coordSection, v, spatialDim);
871: PetscSectionSetFieldDof(coordSection, v, 0, spatialDim);
872: }
873: PetscSectionSetUp(coordSection);
874: DMSetCoordinates(dm, coordinates);
875: VecDestroy(&coordinates);
876: /* Read Labels*/
877: ctx.rank = rank;
878: ctx.dm = dm;
879: ctx.viewer = viewer;
880: PetscViewerHDF5PushGroup(viewer, "/labels");
881: PetscViewerHDF5OpenGroup(viewer, &fileId, &groupId);
882: PetscStackCallHDF5(H5Literate,(groupId, H5_INDEX_NAME, H5_ITER_NATIVE, &idx, ReadLabelHDF5_Static, &ctx));
883: PetscStackCallHDF5(H5Gclose,(groupId));
884: PetscViewerHDF5PopGroup(viewer);
885: return(0);
886: }
887: #endif