Paraview is an industrial-strength, open-source 3D visualization program designed to visualize large data sets created by computational fluid dynamics (CFD) simulations. Although its native data file format (.vtu) is designed for reading in point- and cell-based data from CFD simulations, I have successfully used it to read in and visualized discrete particle data from a Brownian Dynamics (BD) simulation. Below, you can download a Python class that is useful for visualizing particles in motion. It should be easily adaptable to any type of discrete data set that involves discrete objects, vectors, and/or scalars. In addition, this class should be useful with any application based on the VTK toolkit because they also read .vtu files.
The Python .vtu Generator
This is the first release of a Python class that writes serial, unstructured VTK files (.vtu) in XML format. 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. Download the class definition at GitHub.
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.
Reference: VTK file format documentation
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