[vtkusers] HELP Wanted: Visualizing data from an OpenFOAM simulation

Andre Zimmer andre.zimmer at rwth-aachen.de
Tue Nov 15 06:30:20 EST 2016


Ok, after a lot of research I figured it out, as it might help other as 
well here you go:

import vtk

file_name = "U_nearWalls.vtk"

reader = vtk.vtkPolyDataReader()
reader.SetFileName(file_name)
reader.ReadAllScalarsOn()
reader.ReadAllVectorsOn()
reader.Update()
reader.GetOutput().GetPointData().SetActiveScalars("U")
#reader.SetNumberOfComponents(3)
#reader.GetOutput().GetPointData().GetArray("U")
#reader.UsePointFieldData()


calculator=vtk.vtkArrayCalculator()
calculator.SetInputConnection(reader.GetOutputPort())
calculator.AddVectorArrayName("U")
calculator.SetFunction("mag(U)")
calculator.SetResultArrayName("magU")
calculator.Update()



mapper=vtk.vtkPolyDataMapper()
mapper.SetInputArrayToProcess(1,0,0,0,'magU')
#mapper.SetInterpolateScalarsBeforeMapping(1)
#mapper.SetNumberOfComponents(3)
#mapper.SetInputConnection(reader.GetOutputPort())
mapper.SetInputConnection(calculator.GetOutputPort())
mapper.SetScalarRange(calculator.GetOutput().GetPointData().GetScalars().GetRange())
#mapper.SetScalarRange(0,75)
#mapper.ScalarVisibilityOn()
#mapper.UsePointFieldData()
print('range: 
',(calculator.GetOutput().GetPointData().GetScalars().GetRange()))
actor=vtk.vtkActor()
actor.SetMapper(mapper)

renderer = vtk.vtkRenderer()
renderer.AddActor(actor)
renderer.SetBackground(0.1, 0.2, 0.4)

render_window = vtk.vtkRenderWindow()
render_window.AddRenderer(renderer)
render_window.SetSize(600, 600)

interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(render_window)

interactor.Initialize()
render_window.Render()
interactor.Start()



On 11/14/2016 07:29 PM, Andre Zimmer wrote:
> Hi,
>
> I noticed that this approach only shows one component of the array in 
> this case the X-component of the velocity.
>
> I have figured out that I can set my color map to 
> cmap.SetVectorModeToMagnitude() but this does not seem to do much.
>
> Is there a simple setting I am not aware of or do I have to manually 
> calculate the vector magnitude?
>
>
> Thanks,
> André
>
>
> On 11/08/2016 08:54 PM, Bill Lorensen wrote:
>> Andre,
>>
>> BTW: Thanks for providing a runnable script and data. Makes it easier 
>> to help.
>>
>> Enjoy VTK,
>>
>> Bill
>>
>>
>> On Tue, Nov 8, 2016 at 2:52 PM, Bill Lorensen 
>> <bill.lorensen at gmail.com> wrote:
>>> You need to set the active scalar for the point data and specify the
>>> range of the scalars for the mapper.
>>>
>>> See:
>>> #!
>>> import vtk
>>> file_name = "U_nearWalls.vtk"
>>>
>>> reader = vtk.vtkPolyDataReader()
>>> reader.SetFileName(file_name)
>>> reader.ReadAllScalarsOn()
>>> reader.ReadAllVectorsOn()
>>> reader.Update()
>>> reader.GetOutput().GetPointData().SetActiveScalars("U")
>>> mapper=vtk.vtkPolyDataMapper()
>>> mapper.SetInputConnection(reader.GetOutputPort())
>>> mapper.SetScalarRange(reader.GetOutput().GetPointData().GetScalars().GetRange()) 
>>>
>>> actor=vtk.vtkActor()
>>> actor.SetMapper(mapper)
>>>
>>> renderer = vtk.vtkRenderer()
>>> renderer.AddActor(actor)
>>> renderer.SetBackground(0.1, 0.2, 0.4)
>>> render_window = vtk.vtkRenderWindow()
>>> render_window.AddRenderer(renderer)
>>> render_window.SetSize(300, 300)
>>> interactor = vtk.vtkRenderWindowInteractor()
>>> interactor.SetRenderWindow(render_window)
>>>
>>> interactor.Initialize()
>>> render_window.Render()
>>> interactor.Start()
>>>
>>> On Tue, Nov 8, 2016 at 1:08 PM, Andre Zimmer
>>> <andre.zimmer at rwth-aachen.de> wrote:
>>>> Hi,
>>>> by now I have spend days trying to visualize data from the OpenFOAM 
>>>> sample
>>>> function object.
>>>> Here is a small test case:
>>>> https://www.dropbox.com/s/5bhic1ms0anzrno/U_nearWalls.vtk?dl=0
>>>>
>>>> No matter what I do I always get a gray car. I have tried specifically
>>>> setting the scalar, vector, array, transforming and all kinds of stuff
>>>> without success.
>>>> In the simplest form I think the Python code should look like this:
>>>>
>>>> import vtk
>>>>   file_name = "U_nearWalls.vtk"
>>>>
>>>> reader = vtk.vtkPolyDataReader()
>>>> reader.SetFileName(file_name)
>>>> reader.ReadAllScalarsOn()
>>>> reader.ReadAllVectorsOn()
>>>> reader.Update()
>>>>
>>>> mapper=vtk.vtkPolyDataMapper()
>>>> mapper.SetInputConnection(reader.GetOutputPort())
>>>>
>>>> actor=vtk.vtkActor()
>>>> actor.SetMapper(mapper)
>>>>
>>>> renderer = vtk.vtkRenderer()
>>>> renderer.AddActor(actor)
>>>> renderer.SetBackground(0.1, 0.2, 0.4)
>>>>   render_window = vtk.vtkRenderWindow()
>>>> render_window.AddRenderer(renderer)
>>>> render_window.SetSize(300, 300)
>>>>   interactor = vtk.vtkRenderWindowInteractor()
>>>> interactor.SetRenderWindow(render_window)
>>>>
>>>> interactor.Initialize()
>>>> render_window.Render()
>>>> interactor.Start()
>>>>
>>>>
>>>> I am using Fedora 24, Python 2.7, VTK 6.3.
>>>> I assume it is only a small mistake I am making so hopefully 
>>>> someone can
>>>> help.
>>>>
>>>> Thank you in advance,
>>>> André
>>>> _______________________________________________
>>>> Powered by www.kitware.com
>>>>
>>>> Visit other Kitware open-source projects at
>>>> http://www.kitware.com/opensource/opensource.html
>>>>
>>>> Please keep messages on-topic and check the VTK FAQ at:
>>>> http://www.vtk.org/Wiki/VTK_FAQ
>>>>
>>>> Search the list archives at: http://markmail.org/search/?q=vtkusers
>>>>
>>>> Follow this link to subscribe/unsubscribe:
>>>> http://public.kitware.com/mailman/listinfo/vtkusers
>>>
>>>
>>> -- 
>>> Unpaid intern in BillsBasement at noware dot com
>>
>>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at 
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at: 
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Search the list archives at: http://markmail.org/search/?q=vtkusers
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers



More information about the vtkusers mailing list