Generating VTK files with Python
The Python .vtu Generator
This is the first release of a Python class that writes serial, unstructured VTK files (.vtu) in XML format. I use this to visualize what’s happening in a Brownian Dynamics simulation. Even if your application is very different, I think this class will be useful as an example of how to write valid VTK files in the new XML format. Here is the class definition for you to download:
The class is used as follows. Brownian Dynamics is a method of simulating the motion of colloidal particles in a liquid. I need to visualize the particles–where they are, where they’ve been, and what forces are acting upon them. First, I create an object (let’s call it vtk_writer). Every time I need to see what’s going on in the simulation (perhaps every 5 time steps), I call the method vtk_writer.snapshot(filename, x, y, z, …) to write out a .vtu file. The object keeps track of every .vtu file that has been written. At the end of my simulation, I call vtk_writer.writePVD(filename) which writes out a .pvd file that contains all the filenames of the .vtu files. Using Paraview, open the .pvd file, and all the .vtu files will automatically be loaded.
Reading XML Data into Paraview
Even if you have completed all the steps correctly so far, you will see nothing in Paraview! In order to visualize something, you use a “filter.” Select your .pvd file in the Pipeline Browser, and choose Glyph from the Filter menu, or the toolbar. In the Object Inspector, choose Sphere for the Glyph Type. Set Radius to 1, Scale Mode to Scalar, and set the Scale Factor to 1. Assuming you put radii into the .vtu files, you should now have scale spheres. However, you may not be able to see them, so click on the Display tab and click the “Zoom to Data” button. Now you should see your spheres! If you have saved forces in the .vtu file, add an Arrow glyph and choose “Forces” from the “Vectors” drop-down. Now you will see the force being exerted on each sphere. Finally, create a box from the “Sources” menu and set the appropriate dimensions for your simulation area.
Creating Movies from Paraview
From the File menu, choose “Save Animation”, choose a directory, and enter a filename. Paraview will generate a .jpg image for every frame of your animation. You then need to use a third-party tool to turn the images into a movie. You can do it with ImageMagick, but I don’t recommend it if you have more than a few hundred frames. Imagemagick loads every image into RAM before creating the movie. A better way is to install mplayer (use the “encode” USE flag on the Gentoo ebuild). Then you can use the command line tool “mencoder” to create the movie much more efficiently. I had some trouble getting Windows Media Player 11 to play the movies created with mencoder. I used the following command line to create Windows-compatible mpeg movies:
mencoder "mf://*.jpg" -of rawvideo -mpegopts format=mpeg1:tsaf:muxrate=2000 -o output.mpg -oac lavc -lavcopts acodec=mp2:abitrate=224 -ovc lavc -lavcopts vcodec=mpeg2video:vbitrate=1152:keyint=15:mbd=2:aspect=4/3
Reference: creating movies with Imagemagick

