CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM Post-Processing (http://www.cfd-online.com/Forums/openfoam-post-processing/)
-   -   Visualizing OpenFOAM VTK-files using mayavi/mlab in Python (http://www.cfd-online.com/Forums/openfoam-post-processing/127637-visualizing-openfoam-vtk-files-using-mayavi-mlab-python.html)

bjnieuwboer December 16, 2013 09:39

Visualizing OpenFOAM VTK-files using mayavi/mlab in Python
 
Hi All,

I am trying to visualize my model results from OpenFOAM in python using mayavi/mlab. I am able to plot the surfaces of my model. However, I would like to make a quiverplot of my velocity field and a volume plot of my pressures. I found some documentation this. However, this documentation did not state anything on the combination with loaded VTK-files. The code beneath is the one I use for plotting the surfaces. Could anyone help me extend this to plotting the velocities and pressures using python?

Code:

import os
from mayavi.core.api import Engine
from mayavi.sources.vtk_file_reader import VTKFileReader
from mayavi.modules.surface import Surface
from mayavi import mlab
vtkFile_tot = os.path.normpath('D:/Bas/python/VTK/131023cutterBlended08RelVel_0.vtk')
engine = Engine()
engine.start()
scene = engine.new_scene()
reader = VTKFileReader()
reader.initialize(vtkFile_tot)
engine.add_source(reader)
surface = Surface()
engine.add_module(surface)


bjnieuwboer July 30, 2014 10:52

Working principle
 
After a while I found a way to visualise my VTK files generated by OpenFOAM using mayavi.

Code:

import os

 # engine imports.
 from mayavi.scripts import mayavi2
 from mayavi.sources.api import VTKFileReader
 from mayavi.modules.glyph import Glyph
 from mayavi.filters.mask_points import MaskPoints
 from mayavi.core.api import Engine
 from mayavi import mlab
 

 # Create a new VTK scene if none is present yet
 def initializeVTK(filename):
    try:
        engine
    except NameError:
        global engine
        print 'warning is due to the check if the global variable named engine already exists'
        engine = Engine()
        engine.start()
    if len(engine.scenes) == 0:
        engine.new_scene()
    r = VTKFileReader()
    r.initialize(filename)
    engine.add_source(r)
 

 VTK=os.path.normpath('/myFolder/myVTK-file.vtk')
 initializeVTK(VTK)
 

 # Make a mask for the points
 m = MaskPoints()
 m.filter.set(on_ratio=300, random_mode=False)
 engine.add_filter(m) # this adds the module 'm' the engine
 g = Glyph()
 engine.add_module(g) # this adds the module 'g' the engine
 

 g.glyph.color_mode = 'color_by_vector'
 g.glyph.scale_mode = 'scale_by_vector'
 # Use arrows to view the scalars.
 gs = g.glyph.glyph_source
 gs.glyph_source = gs.glyph_dict['arrow_source']
 

 f = mlab.gcf()
 mlab.view(distance=3.5)
 f.scene.background = (1.0, 1.0, 1.0) # set background color 
 f.scene.render()
 mlab.show()



All times are GMT -4. The time now is 08:07.