我关心的是:我有一个三维磁场,B,从一个自适应网格细化,保存和组织的HDF格式。现在,我想使用Python例程创建一个VTK文件。我的目标是用Paraview来代表它。
你能帮我创建这个例行公事吗?
使用:
gridToVTK("./B", np.array(x), np.array(y), np.array(z), pointData = {"B" : B})
我成功地将HDF-5 非均匀数据转换为VTK,但是自适应网格细化在组织上有一点不同。
谢谢,
发布于 2021-01-13 15:55:15
如果只想可视化网格,可以使用类似于下面的函数来创建vtk非结构化网格,以便在截图中加载文件:
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
的组件数。
请注意,如果您输入包含挂节点的精化网格,这可能会影响网格的连通性,并根据连接性信息导致算法的错误结果。据我所知,vtk不支持挂节点连接。
https://stackoverflow.com/questions/65683933
复制相似问题