|
| 1 | +import warnings |
| 2 | + |
| 3 | +import meshio |
| 4 | + |
| 5 | +from .._common import cell_data_from_raw, raw_from_cell_data |
| 6 | +from .._helpers import register |
| 7 | +from ..xdmf.common import meshio_to_xdmf_type, xdmf_to_meshio_type |
| 8 | + |
| 9 | + |
| 10 | +def read(filename): |
| 11 | + import h5py |
| 12 | + |
| 13 | + with h5py.File(filename, "r") as f: |
| 14 | + assert f.attrs["type"] == "hmf" |
| 15 | + assert f.attrs["version"] == "0.1-alpha" |
| 16 | + |
| 17 | + assert len(f) == 1, "only one domain supported for now" |
| 18 | + domain = f["domain"] |
| 19 | + |
| 20 | + assert len(domain) == 1, "only one grid supported for now" |
| 21 | + grid = domain["grid"] |
| 22 | + |
| 23 | + points = None |
| 24 | + cells = {} |
| 25 | + point_data = {} |
| 26 | + cell_data_raw = {} |
| 27 | + |
| 28 | + for key, value in grid.items(): |
| 29 | + if key[:8] == "Topology": |
| 30 | + cell_type = value.attrs["TopologyType"] |
| 31 | + cells[xdmf_to_meshio_type[cell_type]] = value[()] |
| 32 | + |
| 33 | + elif key == "Geometry": |
| 34 | + # TODO is GeometryType really needed? |
| 35 | + assert value.attrs["GeometryType"] in ["X", "XY", "XYZ"] |
| 36 | + points = value[()] |
| 37 | + |
| 38 | + elif key == "CellAttributes": |
| 39 | + for name, ca in value.items(): |
| 40 | + cell_data_raw[name] = ca[()] |
| 41 | + |
| 42 | + else: |
| 43 | + assert key == "NodeAttributes" |
| 44 | + for name, na in value.items(): |
| 45 | + point_data[name] = na[()] |
| 46 | + |
| 47 | + cell_data = cell_data_from_raw(cells, cell_data_raw) |
| 48 | + |
| 49 | + return meshio.Mesh( |
| 50 | + points, |
| 51 | + cells, |
| 52 | + point_data=point_data, |
| 53 | + cell_data=cell_data, |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +def write_points_cells(filename, points, cells, **kwargs): |
| 58 | + write(filename, meshio.Mesh(points, cells), **kwargs) |
| 59 | + |
| 60 | + |
| 61 | +def write(filename, mesh, compression="gzip", compression_opts=4): |
| 62 | + import h5py |
| 63 | + |
| 64 | + warnings.warn("Experimental file format. Format can change at any time.") |
| 65 | + with h5py.File(filename, "w") as h5_file: |
| 66 | + h5_file.attrs["type"] = "hmf" |
| 67 | + h5_file.attrs["version"] = "0.1-alpha" |
| 68 | + domain = h5_file.create_group("domain") |
| 69 | + grid = domain.create_group("grid") |
| 70 | + |
| 71 | + _write_points(grid, mesh.points, compression, compression_opts) |
| 72 | + _write_cells(mesh.cells, grid, compression, compression_opts) |
| 73 | + _write_point_data(mesh.point_data, grid, compression, compression_opts) |
| 74 | + _write_cell_data(mesh.cell_data, grid, compression, compression_opts) |
| 75 | + |
| 76 | + |
| 77 | +def _write_points(grid, points, compression, compression_opts): |
| 78 | + geo = grid.create_dataset( |
| 79 | + "Geometry", |
| 80 | + data=points, |
| 81 | + compression=compression, |
| 82 | + compression_opts=compression_opts, |
| 83 | + ) |
| 84 | + geo.attrs["GeometryType"] = "XYZ"[: points.shape[1]] |
| 85 | + |
| 86 | + |
| 87 | +def _write_cells(cell_blocks, grid, compression, compression_opts): |
| 88 | + for k, cell_block in enumerate(cell_blocks): |
| 89 | + xdmf_type = meshio_to_xdmf_type[cell_block.type][0] |
| 90 | + topo = grid.create_dataset( |
| 91 | + f"Topology{k}", |
| 92 | + data=cell_block.data, |
| 93 | + compression=compression, |
| 94 | + compression_opts=compression_opts, |
| 95 | + ) |
| 96 | + topo.attrs["TopologyType"] = xdmf_type |
| 97 | + |
| 98 | + |
| 99 | +# In XDMF, the point/cell data are stored as |
| 100 | +# |
| 101 | +# <Attribute Name="phi" AttributeType="Scalar" Center="Node"> |
| 102 | +# <DataItem DataType="Float" Dimensions="4" Format="HDF" Precision="8"> |
| 103 | +# out.h5:/data2 |
| 104 | +# </DataItem> |
| 105 | +# </Attribute> |
| 106 | +# |
| 107 | +# We cannot register multiple entries with the same name in HDF, so instead of |
| 108 | +# "Attribute", use |
| 109 | +# ``` |
| 110 | +# NodeAttributes |
| 111 | +# -> name0 + data0 |
| 112 | +# -> name1 + data0 |
| 113 | +# -> ... |
| 114 | +# CellAttributes |
| 115 | +# -> ... |
| 116 | +# ``` |
| 117 | +# Alternative: |
| 118 | +# ``` |
| 119 | +# NodeAttribute0 |
| 120 | +# -> name |
| 121 | +# -> data |
| 122 | +# NodeAttribute1 |
| 123 | +# -> name |
| 124 | +# -> data |
| 125 | +# ... |
| 126 | +# ``` |
| 127 | +# It's done similarly for Topologies (cells). |
| 128 | +# |
| 129 | +def _write_point_data(point_data, grid, compression, compression_opts): |
| 130 | + na = grid.create_group("NodeAttributes") |
| 131 | + for name, data in point_data.items(): |
| 132 | + na.create_dataset( |
| 133 | + name, |
| 134 | + data=data, |
| 135 | + compression=compression, |
| 136 | + compression_opts=compression_opts, |
| 137 | + ) |
| 138 | + |
| 139 | + |
| 140 | +def _write_cell_data(cell_data, grid, compression, compression_opts): |
| 141 | + raw = raw_from_cell_data(cell_data) |
| 142 | + ca = grid.create_group("CellAttributes") |
| 143 | + for name, data in raw.items(): |
| 144 | + ca.create_dataset( |
| 145 | + name, |
| 146 | + data=data, |
| 147 | + compression=compression, |
| 148 | + compression_opts=compression_opts, |
| 149 | + ) |
| 150 | + |
| 151 | + |
| 152 | +# TODO register all xdmf except hdf outside this try block |
| 153 | +register( |
| 154 | + "hmf", |
| 155 | + [".hmf"], |
| 156 | + read, |
| 157 | + {"hmf": write}, |
| 158 | +) |
0 commit comments