简体   繁体   English

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

[英]Python - meshio: How to define vertex cells for point-only data?

Problem问题

I have a 2D time-series data, and I want to save it as XMDF using meshio.我有一个 2D 时间序列数据,我想使用 meshio 将其保存为 XMDF。 The problem is, my mesh is just an array of points with associated point data, and I don't have any cell defined.问题是,我的网格只是一个带有关联点数据的点数组,我没有定义任何单元格。 As such, I tried to use the "vertex" cell type, which is a single-point cell, but it doesn't work.因此,我尝试使用"vertex"单元格类型,它是一个单点单元格,但它不起作用。 Meshio's documentation is kind of lacking, so I'm stuck. Meshio 的文档有点缺乏,所以我被卡住了。

Code代码

Following the two examples on their Github page , I did the following.他们的 Github 页面上的两个示例之后,我执行了以下操作。 I'm not sure how to define the cells correctly, as meshio doesn't document this properly.我不确定如何正确定义单元格,因为 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]})

Error错误

The above script produces the following error:上面的脚本产生以下错误:

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'

I tried different combinations of converting some of the arrays used to NumPy array, but I couldn't find out the cause.我尝试了将一些 arrays 转换为 NumPy 阵列的不同组合,但我找不到原因。 I ask for your help.我请求你的帮助。

Update:更新:

After changing every used number array to NumPy arrays (credit to comments) - that is, inserting points = np.array(points) directly after points is defined, and changing the cell generator line to cells = [("vertex", np.array([i,])) for i in range(len(points))] - I still have a different error:在将每个使用的数字数组更改为 NumPy arrays (注释)之后 - 即在定义点之后直接插入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

(I also note that the documentation does not use NumPy arrays in the examples.) (我还注意到文档在示例中没有使用 NumPy arrays。)

The problem was that:问题是:

  1. I should've use NumPy arrays everywhere: even though meshio's example use Python lists, the xmdf module apparently can't handle those;我应该在任何地方都使用 NumPy arrays :即使meshio的示例使用 Python 列表,xmdf 模块显然无法处理这些; and
  2. I should've flatten the data as well (obviously).我也应该展平数据(显然)。 Also the cells should be be defined in a better way, though the original concept works too.此外,应该以更好的方式定义单元格,尽管原始概念也有效。

A working version of my code is:我的代码的工作版本是:

# 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}) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python,OpenGL:如何在GPU上存储顶点数据 - Python, OpenGL: how to store vertex data on GPU 如何在 python 中执行数学函数(mod)以仅包含数字数据的单元格并跳过空单元格 - how to perform math function(mod) in python to only cells with numeric data and skip empty cells 通过x,y坐标在python iGraph中定义顶点 - define vertex in python iGraph by x,y coordinates Python OpenGL VAO - 如何为顶点和颜色数据使用单独的数组 - Python OpenGL VAO - How to use separate arrays for vertex and color data 如何使用python获取orient db中顶点的边缘对象数据 - How to get the edge object data of a vertex in orient db using python 给定 4 个点,如何在 Python 中定义超平面? 那么我如何定义 4 个超平面的交集(这应该是一个点)? - How do I define a hyperplane in Python given 4 points? How do I then define the intersection of 4 hyperplanes (this should be a point)? 如何将点云拆分为单元格 - How to split point cloud into cells 如何在 python 中实现这一点:检查顶点是否存在,如果不存在,则创建一个新顶点 - How to implement this in python : Check if a vertex exist, if not, create a new vertex 如何在Python中为实体定义良好的数据结构? - How to define a good data structure for an entity in Python? (Python)如何只保留 dataframe 中的特定部分单元格 - (Python) How to only keep specific part of cells in a dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM