Python meshio:如何为纯点数据定义顶点单元?

2024-05-16 15:45:30 发布

您现在位置:Python中文网/ 问答频道 /正文

问题

我有一个2D时间序列数据,我想使用meshio将其保存为XMDF。问题是,我的网格只是一个点阵列,带有关联的点数据,我没有定义任何单元。因此,我尝试使用"vertex"单元类型,这是一个单点单元,但它不起作用。Meshio的文档有点缺乏,所以我被卡住了

代码

根据两个例子on their Github page,我做了以下工作。我不知道如何正确定义单元格,因为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之后直接插入points = np.array(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数组。)


Tags: inpyhomedatatimelibnpline
1条回答
网友
1楼 · 发布于 2024-05-16 15:45:30

问题是:

  1. 我应该到处使用NumPy数组:尽管meshio的示例使用Python列表,但xmdf模块显然无法处理这些列表;及
  2. 我也应该把数据展平(显然)。此外,应该以更好的方式定义单元格,尽管最初的概念也适用

我的代码的工作版本是:

# 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,))]
points = np.array(points)  # add this

# won't use cells, so define vertex cell (1 point per cell)
cells = [("vertex", np.array([[i,] for i in range(len(points)])))]
# instead of 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):
        # here data[i] also should be flattened
        writer.write_data(t, point_data={"sin_city": data[i].flatten}) 

相关问题 更多 >