简体   繁体   English

使用 Python 从自适应网格细化写入 VTK 文件

[英]Write a VTK file from adaptive mesh refinement with Python

My concern is the following one: I have a 3D magnetic field, B, from an adaptive mesh refinement, saved and organized under a HDF format.我关心的是以下问题:我有一个 3D 磁场 B,来自自适应网格细化,保存并以 HDF 格式组织。 Now I would like to create a VTK file using a Python routine.现在我想使用 Python 例程创建一个 VTK 文件。 My goal is to represent it with Paraview.我的目标是用 Paraview 来代表它。

Can you help me to create this routine?你能帮我创建这个例程吗?

Using:使用:

gridToVTK("./B", np.array(x), np.array(y), np.array(z), pointData = {"B" : B})

I succeed in turning HDF-5 non-uniform data to VTK but adaptive mesh refinement is a bit different in its organisation.我成功地将 HDF-5非均匀数据转换为 VTK,但自适应网格细化在其组织上有点不同。

Thanks,谢谢,

If you only want to visualize your mesh you could use a function similar to the following to create a vtk unstructured grid, in order to load the file in paraview:如果您只想可视化您的网格,您可以使用类似于以下内容的 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()

This will create a unstructured grid (mesh) consisting out of quads, which are often used for adaptive mesh refinement.这将创建一个由四边形组成的非结构化网格(网格),通常用于自适应网格细化。 All you need to provide is the list of points:您需要提供的只是点列表:

points = np.array([x, y, z])

and the mesh connections as index list cells .和网格连接作为索引列表cells Your point data B should be a scalar value.您的点数据B应该是一个标量值。 If this has more than one component per point you have to change the number of components for the vtkDoubleArray inside the function.如果每个点有多个组件,则必须更改vtkDoubleArray内 vtkDoubleArray 的组件数量。

Please note, that if you input your refined mesh containing hanging nodes, this might affect the connectivity of the mesh and lead to wrong results for algorithms depending on connectivity information.请注意,如果您输入包含悬挂节点的细化网格,这可能会影响网格的连通性,并导致算法根据连通性信息得出错误的结果。 As far as I know vtk does not support hanging node connectivity.据我所知,vtk 不支持挂节点连接。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM