5

我正在尝试使用 paraview-python 脚本以 x3d 格式的 OpenFOAM 结果导出。当我通过 paraview 图形界面执行此操作时,它可以工作并且可以在 Blender 中可视化结果,请参见下图

在此处输入图像描述

但是,当我尝试使用以下脚本执行相同的操作时

from paraview.simple import *
import fnmatch
import os
import shutil

#create alist of all vtk files
vtkFiles = []
for root, dirnames, filenames in os.walk('.'):
    for filename in fnmatch.filter(filenames, '*.vtk'):
        vtkFiles.append(os.path.join(root, filename))


vtkFilesGroups=[
    'U',
]


def ResetSession():
    pxm = servermanager.ProxyManager()
    pxm.UnRegisterProxies()
    del pxm
    Disconnect()
    Connect()


def x3dExport(output,r):
    #export in x3d format

    exporters = servermanager.createModule("exporters")
    Show(r)
    view = GetActiveView()
    render = Render()
    x3dExporter = exporters.X3DExporter(FileName=output)
    x3dExporter.SetView(view)
    x3dExporter.Write()
    ResetSession()

# group VTK files by gruop (fields in openfoam "vtkFilesGroups")
# then loop over all and save it into different formats
groupedVtkFiles=[]
for group in vtkFilesGroups:

    vtkDir = os.path.join('.', group, 'vtk')
    if not os.path.exists(vtkDir):
        os.makedirs(vtkDir)

    vtuDir = os.path.join('.', group, 'vtu')
    if not os.path.exists(vtuDir):
        os.makedirs(vtuDir)

    x3dDir = os.path.join('.', group, 'x3d')
    if not os.path.exists(x3dDir):
        os.makedirs(x3dDir)



    for stepFile in vtkFiles:
        tmp = stepFile.split(os.sep)
        oldFileName = tmp[-1].split('.')[0]
        time = tmp[-2]
        fileNameVtk = '{}_{}.vtk'.format(oldFileName, time)
        fileNameVtp = '{}_{}.vtp'.format(oldFileName, time)
        fileNameX3d = '{}_{}.x3d'.format(oldFileName, time)

        r = LegacyVTKReader(FileNames=[stepFile])
        w = XMLUnstructuredGridWriter()
        w.FileName = os.path.join(vtuDir, fileNameVtp)
        w.UpdatePipeline()

        x3dExport(os.path.join(x3dDir, fileNameX3d), r)

从这张图片中可以看出,场值(速度 U)没有被导出!

在此处输入图像描述

有人可以告诉我我做错了什么吗?谢谢!

4

2 回答 2

3

您的问题是.foam文件不是科学可视化文件,因为 VTK,.foam文件仅用于 ParaView(通过其扩展名,而不是其内容)来识别阅读器OpenFOAMReader,然后将其用于后处理。

我有两个解决方案给你:

  1. 阅读阅读器文档以找到执行此操作的方法。
  2. 使用FoamToVTK将结果转换为 VTK 文件,然后循环遍历结果。

编辑

我很久以前使用此代码来转换做那件事:

from paraview.simple import *
import fnmatch
import os
import shutil

#create alist of all vtk files
vtkFiles = []
for root, dirnames, filenames in os.walk('.'):
    for filename in fnmatch.filter(filenames, '*.vtk'):
        vtkFiles.append(os.path.join(root, filename))


vtkFilesGroups=('p', 'U')


def ResetSession():
    pxm = servermanager.ProxyManager()
    pxm.UnRegisterProxies()
    del pxm
    Disconnect()
    Connect()


def x3dExport(output,r):
    #export in x3d format

    exporters = servermanager.createModule("exporters")
    Show(r)
    view = GetActiveView()
    render = Render()
    x3dExporter = exporters.X3DExporter(FileName=output)
    x3dExporter.SetView(view)
    x3dExporter.Write()
    ResetSession()

# group VTK files by gruop (fields in openfoam "vtkFilesGroups")
# then loop over all and save it into different formats
for group in vtkFilesGroups:
    x3dDir = os.path.join('.', group, 'x3d')
    if not os.path.exists(x3dDir):
        os.makedirs(x3dDir)

    for stepFile in (f for f in vtkFiles if group in f):
        tmp = stepFile.split(os.sep)
        oldFileName = tmp[-1].split('.')[0]
        time = tmp[-2]
        fileNameX3d = '{}_{}.x3d'.format(oldFileName, time)

        x3dExport(os.path.join(x3dDir, fileNameX3d), r)
于 2019-05-02T01:48:20.127 回答
1

您需要在脚本中为数据着色,例如:

ColorBy(yourRep, ('POINTS', ('YourScalar', 'YourComp'))

文档

于 2020-04-14T03:58:50.993 回答