This guide will be extended during the semester. First out are some instructions on how to play around with Python and MayaVi. We will add more to this guide as we move along to new topics and different methods to perform things.
Before you get started, you need to follow the instructions on how to install Python and MayaVi.
Drawing a simple vector field
The first thing we’ll do is to draw a simple vector field from a point charge. Create a file named vectorfield.py and save it somewhere on your computer.
Add the following to the file, see the comments for details:
from numpy import * try: from mayavi.mlab import * except ImportError: from enthought.mayavi.mlab import * # this creates a 3D grid which is ranges from -100 to 100 in all # coordinates, with intersections at every 20th value # (-100, -80, -60, ...). See 'help mgrid' for details. x,y,z = mgrid[-100.:101.:20., -100.:101.:20., -100.:101.:20.] # We will now store the coordinates of our charge as a # vector. All vectors in Python should be stored as arrays. # Note that we are placing this charge in between the grid points. # This is to get the best possible symmetry in this specific field # If you place the charge _on_ the grid, you get a division of zero # when calculating the E-field. qpos = array([10.,10.,10.]) # the magnitude of our charge. This could be any number # at the moment. qcharge = 1.0 # Create a grid for the electric field. This has the size of # x,y and z, but all the values are now set to zero. Ex, Ey, Ez = x*0, y*0, z*0 # Calculate the x, y and z distance to the charge at every point in the grid: rx = x - qpos ry = y - qpos rz = z - qpos # Calculate the distance at every point in the grid: r = sqrt(rx**2 + ry**2 + rz**2) # Calculate the field for each component at every point in the grid: Ex = (qcharge / r**2) * (rx / r) Ey = (qcharge / r**2) * (ry / r) Ez = (qcharge / r**2) * (rz / r) # Draw the vector field in 3D quiver3d(x,y,z,Ex,Ey,Ez)
Open up a terminal and change directory to the same place as your saved file. If you are running Windows, open up Start > Enthought > PyLab and use cd to change directory from there. (Do not use IDLE if you’re on Windows. It does not work well with Mayavi.)
We recommend using IPython in this course. Using IPython gives you the possibility to run Python scripts and continue working on the data from the script without editing the files. In addition you get a lot of other nice features that you can read more about on this page.
If you are using Windows you already have PyLab open (this is IPython), otherwise type the following to open up an IPython session:
(If you are running Ubuntu, use ipython -wthread instead.)
Run the file by typing:
Wow! That should create a cool looking figure like the one below:
- Left click to rotate.
- Right click, scroll or Ctrl + left click to zoom.
- Middle click or Shift + left click to pan.
As most things in 3D, this looks really cool, but might not be as when you want to read out specific properties of a field. Then it could be much more useful to see the field in 2D with axes and labels.
Edit the file above to have only two dimensions by changing the x,y,z-line to
x,y,z = mgrid[-100.:101.:20., -100.:101.:20., 0.:1.:1.]
And place the charge in the xy-plane:
qpos = array([10.,10.,0])
This will leave us with a 2D plot. Try running the file again by typing:
You should end up with something like this:
That should rid us of the 3D view:
Select the color you prefer (black and white works out best) under the Scene properties:
Now, we need to move on with the axes. You can do the following steps either directly in IPython by typing the commands as we go, or save them in the vectorfield.py file and run the file from IPython.
To add axes, just use the following command:
myaxes = axes()
This should give you something like this:
myaxes.axes.y_axis_visibility = False
Note that in both cases, the z-axis has been named y. It is beyond me why Mayavi does it this way, but it is easy to figure out what axis to disable by looking at the effect.
Under Title text and Label text you may also change the font used on the labels. Personally I prefer the Times font without bold or italic for the labels and Times with italic only for the titles.
In addition, we’d probably like to have a title for our plot. This may be added by typing:
mytitle = title("My vector field")
You can also add this by right-clicking VectorScatter in the Mayavi Pipeline and selecting Add Module > Text. Afterwards you’ll have to adjust the size and positioning to your needs.
My final render looks like this:
Don’t be sorry if your render is not exactly the same. There are many ways to do this and you should play with the settings until your are happy. If you are having problems with anything, don’t hesitate to contact us or leave a comment on this page.
To save the render as an image, click the save icon in the Mayavi window.
How to learn more
If you want to know more about any of the functions, you can open up ipython, import the necessary packages and type
to get more info about the function “functionname”. If you for example want to know more about mgrid, you can open up IPython and type the following:
from numpy import * help(mgrid)
Type Q to exit help mode.