Scientific visualisation is the representation of data graphically so that you can gain an understanding and insight into the data. It is useful in virtually every domain of science. Among the first courses where 3D visualisation helps is in gaining an insight into bonds in chemistry.
The simplest molecule would be a hydrogen molecule ion. You can approximate its orbital functions as a linear combination of atomic orbitals. The intention in this article is to take a very simple item and explore the various ways of visualisation.
Let the two atoms be at +/-a on the x-axis. The simplest two orbitals will be the bonding and anti-bonding 1s-orbitals. Ignoring the normalisation factors, you can plot the electron density on the x axis as follows.
You have used mgrid instead of arange to create an array. The syntax has a hack that if the 3rd parameter is complex, it is interpreted as the number of points rather than the step size.
You have used the Matplotlib and it gives a static 2-d image as you can see in Figure 1. If you wanted to explore the function in other directions, you would have to create a separate plot for each them.
What if you wish to explore the electron density in a plane? Mayavi comes to your aid. It is a project initiated by Prabhu Ramachandran and currently enhanced and supported by http://www.enthought.com/. Mayavi offers very easy to use Python and Scipy bindings to the vistualisation toolkit, VTK, http://www.vtk.org/. You will need to install Mayavi package. It is available in the Fedora and Ubuntu repositories.
You can plot the function values of the bonding orbital in the xy plane as follows:
The mlab module in mayavi interfaces to the scipy arrays. You create a two dimensional array using mgrid. The surf method plots the bonding function values against the x,y grid. The outline and axes are shown. Finally, the show method ensures that the image remains visible till you close the window.
You can interact with the scene as illustrated by two images in Figure 2.
A molecule is a 3 dimensional object. So, what if you wanted to explore the orbital function in x, y and z axes as well? One solution is to observe the contours.
You have now created a 3-dimensional grid. The values of the electron density at each point is given in values. The method contour3d plots the contours and you can interact with the image and view it from various perspectives. The opacity parameter is very important as it allows you to view the contours surrounded by other contours. One perspective is shown in Figure 3.
While contours are useful, you may wish to examine the values at each location. This is possible in Mayavi as in the following example.
You create a source for a scalar field and the volume method maps the source values to colours. The range of values to be mapped is given by the vmin and vmax parameters. A result can be seen in Figure 4.
The power of Mayavi/VTK is that with just a few lines of code, you can create beautiful imagery which can help get an insight into the scientific data. Scipy/Mayavi combination allows you to focus on the data and the observations.
You can explore more examples of what is possible at http://code.enthought.com/projects/mayavi/docs/development/html/mayavi/auto/examples.html.
Python for Research >