簡體   English   中英

Python - meshio:如何為僅點數據定義頂點單元?

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

問題

我有一個 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'

我嘗試了將一些 arrays 轉換為 NumPy 陣列的不同組合,但我找不到原因。 我請求你的幫助。

更新:

在將每個使用的數字數組更改為 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

(我還注意到文檔在示例中沒有使用 NumPy arrays。)

問題是:

  1. 我應該在任何地方都使用 NumPy arrays :即使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}) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM