问题
我有一个 2D 时间序列数据,我想使用 meshio 将其保存为 XMDF。问题是,我的网格只是一个带有关联点数据的点数组,我没有定义任何单元格。因此,我尝试使用"vertex"单元格类型,这是一个单点单元格,但它不起作用。Meshio 的文档有点缺乏,所以我被卡住了。
代码
在他们的 Github 页面上的两个示例之后,我做了以下事情。我不确定如何正确定义单元格,因为 meshio 没有正确记录这一点。
# generate some data on a 10x10 mesh with 20 time steps (tested, works)
ts = np.arange(20)
x, y = np.meshgrid(np.arange(10), np.arange(10))
data = np.empty((20, 10, 10))
for i, t in enumerate(ts):
data[i] = np.sin((x + y) * t)
# data is a 3D NumPy array now with dimensions (20,10,10)
# generate list of points (tested, works)
points = [list(p) for p in zip(*(x.flat, y.flat,))]
# won't use cells, so define vertex cell (1 point per cell) <-- ???
cells = [("vertex", [i,]) for i in range(len(points))]
# as seen in meshio's documentation, write time series data
filename = "test.xdmf"
with meshio.xdmf.TimeSeriesWriter(filename) as writer:
writer.write_points_cells(points, cells)
for i, t in enumerate(ts):
writer.write_data(t, point_data={"sin_city": data[i]})
错误
上面的脚本产生以下错误:
Traceback (most recent call last):
File "/home/ezio/Codes/gfield/_temp.py", line 103, in <module>
writer.write_points_cells(points, cells)
File "/home/ezio/anaconda3/envs/radpolpy/lib/python3.8/site-packages/meshio/xdmf/time_series.py", line 284, in write_points_cells
self.points(grid, points)
File "/home/ezio/anaconda3/envs/radpolpy/lib/python3.8/site-packages/meshio/xdmf/time_series.py", line 340, in points
if points.shape[1] == 2:
AttributeError: 'list' object has no attribute 'shape'
我尝试了将一些数组转换为 NumPy 数组的不同组合,但我找不到原因。我请求你的帮助。
更新:
在将每个使用的数字数组更改为 NumPy 数组(归功于注释)points = np.array(points)之后 - 即在定义之后直接插入points,并将单元格生成器行更改为cells = [("vertex", np.array([i,])) for i in range(len(points))]- 我仍然有一个不同的错误:
Traceback (most recent call last):
File "/home/ezio/Codes/gfield/_temp.py", line 105, in <module>
writer.write_points_cells(points, cells)
File "/home/ezio/anaconda3/envs/radpolpy/lib/python3.8/site-packages/meshio/xdmf/time_series.py", line 285, in write_points_cells
self.cells(cells, grid)
File "/home/ezio/anaconda3/envs/radpolpy/lib/python3.8/site-packages/meshio/xdmf/time_series.py", line 409, in cells
[
File "/home/ezio/anaconda3/envs/radpolpy/lib/python3.8/site-packages/meshio/xdmf/time_series.py", line 411, in <listcomp>
np.insert(
File "<__array_function__ internals>", line 5, in insert
File "/home/ezio/anaconda3/envs/radpolpy/lib/python3.8/site-packages/numpy/lib/function_base.py", line 4527, in insert
axis = normalize_axis_index(axis, ndim)
numpy.AxisError: axis 1 is out of bounds for array of dimension 1
(我还注意到文档在示例中没有使用 NumPy 数组。)