代码之家  ›  专栏  ›  技术社区  ›  Tom de Geus

如何读取有限元网格并将其视为非结构化网格

  •  0
  • Tom de Geus  · 技术社区  · 8 年前

    我知道如何做一些简单的事情:

    from paraview.simple import *
    
    Sphere()
    Show()
    Render()
    

    但是,如何制作网格?让我们跳过HDF5部分,重点关注这个非常简单的二维网格,包括二维四边形:

    from paraview.simple import *
    import numpy as np
    
    coor = np.array([
        [ 0.0 , 0.0 ],
        [ 1.0 , 0.0 ],
        [ 2.0 , 0.0 ],
        [ 0.0 , 1.0 ],
        [ 1.0 , 1.0 ],
        [ 2.0 , 1.0 ],
    ])
    
    conn = np.array([
        [ 0 , 1 , 4 , 3 ],
        [ 1 , 2 , 5 , 4 ],
    ])
    

    一种可能的方法是建立一个 vtkUnstructuredGrid .但问题是我不知道该怎么办。一、 e.我如何告诉ParaView使用它?

    from paraview import vtk
    
    points = vtk.vtkPoints()
    
    for i,(x,y) in enumerate(coor):
        points.InsertNextPoint(x,y,0.0)
    
    grid = vtk.vtkUnstructuredGrid()
    
    for el in conn:
        cell = vtk.vtkQuad()
        for i,ver in enumerate(el): 
            cell.GetPointIds().SetId(i,ver)
        grid.InsertNextCell(cell.GetCellType(),cell.GetPointIds())
    
    grid.SetPoints(points)
    

    理解如何包含单元数据和点数据将非常有帮助,但我可能可以从这个问题的解决方案开始解决这个问题。

    2 回复  |  直到 8 年前
        1
  •  2
  •   Cory Quammen    8 年前

    在较低的级别上,可以使用Python定义VTK的操作。 Programmable Filter Programmable Source 可编程源 paraview.simple paraview.simple

    可编程源 ,设置其 Output Data Set Type vtkUnstructuredGrid

    import numpy as np
    
    coor = np.array([
        [ 0.0 , 0.0 ],
        [ 1.0 , 0.0 ],
        [ 2.0 , 0.0 ],
        [ 0.0 , 1.0 ],
        [ 1.0 , 1.0 ],
        [ 2.0 , 1.0 ],
    ])
    
    conn = np.array([
        [ 0 , 1 , 4 , 3 ],
        [ 1 , 2 , 5 , 4 ],
    ])
    
    import vtk
    
    grid = self.GetOutput()
    
    points = vtk.vtkPoints()
    for i,(x,y) in enumerate(coor):
        points.InsertNextPoint(x,y,0.0)
    grid.SetPoints(points)
    
    grid.Allocate()
    for el in conn:
        cell = vtk.vtkQuad()
        for i,ver in enumerate(el): 
              cell.GetPointIds().InsertId(i,ver)
        grid.InsertNextCell(cell.GetCellType(),cell.GetPointIds())
    
        2
  •  0
  •   Tom de Geus    8 年前

    请相信@CoryQuammen,因为这确实是他的解决方案

    完整脚本,包括一些单元格和点数据,供将来参考

    from paraview.simple import *
    
    paraview.simple._DisableFirstRenderCameraReset()
    
    # create a new 'Programmable Source'
    mesh = ProgrammableSource()
    mesh.OutputDataSetType = 'vtkUnstructuredGrid'
    mesh.ScriptRequestInformation = ''
    mesh.PythonPath = ''
    mesh.Script = '''
    import numpy as np
    
    coor = np.array([
        [ 0.0 , 0.0 ],
        [ 1.0 , 0.0 ],
        [ 2.0 , 0.0 ],
        [ 0.0 , 1.0 ],
        [ 1.0 , 1.0 ],
        [ 2.0 , 1.0 ],
    ])
    
    conn = np.array([
        [ 0 , 1 , 4 , 3 ],
        [ 1 , 2 , 5 , 4 ],
    ])
    
    celldata = [
      1.0 ,
      2.0
    ]
    
    normals = np.array([
      [ -1.0 , -1.0 ],
      [  0.0 , -1.0 ],
      [ +1.0 , -1.0 ],
      [ -1.0 , +1.0 ],
      [  0.0 , +1.0 ],
      [ +1.0 , +1.0 ],
    ])
    
    normals /= np.tile(np.sqrt(np.sum(normals**2.,axis=1)).reshape(-1,1),(1,2))
    
    import vtk
    
    grid = self.GetOutput()
    
    points = vtk.vtkPoints()
    for i,(x,y) in enumerate(coor):
        points.InsertNextPoint(x,y,0.0)
    grid.SetPoints(points)
    
    grid.Allocate()
    for el in conn:
        cell = vtk.vtkQuad()
        for i,ver in enumerate(el):
              cell.GetPointIds().InsertId(i,ver)
        grid.InsertNextCell(cell.GetCellType(),cell.GetPointIds())
    
    data = vtk.vtkDoubleArray()
    data.SetName("Example data")
    for i in celldata:
      data.InsertNextValue(i)
    
    grid.GetCellData().AddArray(data)
    
    data = vtk.vtkDoubleArray()
    data.SetNumberOfComponents(3)
    data.SetName("Normals")
    for i in normals:
      data.InsertNextTuple([i[0],i[1],0.0])
    
    grid.GetPointData().AddArray(data)
    '''
    
    # show data from mesh
    Mesh = Show(mesh)
    
    Render()
    
    推荐文章