[英]Write a VTK file from adaptive mesh refinement with Python
我關心的是以下問題:我有一個 3D 磁場 B,來自自適應網格細化,保存並以 HDF 格式組織。 現在我想使用 Python 例程創建一個 VTK 文件。 我的目標是用 Paraview 來代表它。
你能幫我創建這個例程嗎?
使用:
gridToVTK("./B", np.array(x), np.array(y), np.array(z), pointData = {"B" : B})
我成功地將 HDF-5非均勻數據轉換為 VTK,但自適應網格細化在其組織上有點不同。
謝謝,
如果您只想可視化您的網格,您可以使用類似於以下內容的 function 創建 vtk 非結構化網格,以便在 paraview 中加載文件:
def save_vtk_unstructured_grid(path, points, cells, point_data):
"""
Create a vtu grid file containing quads from a list of points,
a list of cells and additional point data.
The list of cells references the points inside the point list via the row index.
N Points: [[x_0, y_0, z_0],
[x_1, y_1, z_1],
...
[x_(n-1), y_(n-1), z_(n-1)]]
M Cells: [[i_00, i_01, i_02, i_03],
[i_10, i_11, i_12, i_13],
...
[i_(m-1)0, i_(m-1)1, i_(m-1)2, i_(m-1)3]]
E.g.:
Cell: p0 x------x p1 => Cell indices inside the cell array:
| | [0, 1, 2, 3]
| |
| |
p2 x------x p3
:param path: Save path as string
:param points: Nx3 numpy array of point coordinates
:param cells: Mx4 numpy array of point indices for a mesh consisting of quads.
:param point_data: Nx1 numpy array of containing data at the point coordinates.
"""
points = vtk.vtkPoints()
cells = vtk.vtkCellArray()
# insert points
for p in points:
points.InsertNextPoint(p[0], p[1], p[2])
# insert the quad cells
for idx in cells:
# create a new quad cell
quad = vtk.vtkQuad()
quad.GetPointIds().SetId(0, idx[0])
quad.GetPointIds().SetId(1, idx[1])
quad.GetPointIds().SetId(2, idx[2])
quad.GetPointIds().SetId(3, idx[3])
cells.InsertNextCell(quad)
# create the point data
data = vtk.vtkDoubleArray()
data.SetNumberOfComponents(1)
data.SetNumberOfValues(len(point_data))
for i, d in enumerate(point_data):
data.SetTuple(i, d)
# create the grid from the points and cells
grid = vtk.vtkUnstructuredGrid()
grid.SetPoints(points)
grid.SetCells(vtk.VTK_QUAD, cells)
grid.GetPointData().SetScalars(data)
# write the grid
writer = vtk.vtkXMLUnstructuredGridWriter()
writer.SetFileName(path)
writer.SetInputData(grid)
writer.Write()
這將創建一個由四邊形組成的非結構化網格(網格),通常用於自適應網格細化。 您需要提供的只是點列表:
points = np.array([x, y, z])
和網格連接作為索引列表cells
。 您的點數據B
應該是一個標量值。 如果每個點有多個組件,則必須更改vtkDoubleArray
內 vtkDoubleArray 的組件數量。
請注意,如果您輸入包含懸掛節點的細化網格,這可能會影響網格的連通性,並導致算法根據連通性信息得出錯誤的結果。 據我所知,vtk 不支持掛節點連接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.