Electric field lines

We have made a small demo that will allow you to play around with electric field lines in your web browser. You can place and move around charged particles to see how the field changes as you make your own charge configuration.

An electric field line simulator straight in your browser!

Try the simulator here

Note that there are some limitations to this simulation, leaving it a bit unphysical when it comes to lines that might end up in open space, wrong field line density and other quirks. All in all it should be correct to a decent approximation, though.

For the programming enthusiasts out there, the simulation is created using Javascript and HTML5. You can check out the source code here.

Enjoy!

Adjusting to the new version of Pylab and Mayavi on Ubuntu 12.04

It seems the IPython and Pylab packages has also been updated in 12.04 and thus removing the old ipython -wthread flag that would ensure Mayavi plots to be run in a separate thread. Running with the flag causes this error to show up:

[TerminalIPythonApp] Unrecognized flag: '-wthread'

Without this flag, the Mayavi plots lock up the UI and hangs. If you want to get the possibility back to rotate and play around with the plots, just start IPython the following way from now on:

ipython --pylab=qt

This will launch IPython with the Qt backend and threading. Using only –pylab does not include threading. For easy and quick access, add the following to a file named .bashrc in your home folder:

alias pylab='ipython --pylab=qt'

From now on you can launch IPython just by typing

pylab

in a terminal.

Using the same script on installs with different EPD versions

In the newest version of Enthought’s Python Distribution (EPD) on Ubuntu, the plotting package has been moved from enthought.mayavi.mlab to the shorter and more general mayavi.mlab. This does however mean that if you, like me, need to work with different versions of EPD on multiple systems, will experience the following error from time to time:

ImportError: No module named enthought.mayavi.mlab

Now, to avoid switching the import statement every time you switch systems, you can make Python check if one of the versions is installed during import. If it is not, we’ll tell it to try the other. This is done in this simple command:

try:
    from enthought.mayavi.mlab import *
except ImportError:
    from mayavi.mlab import *

Just replace any other similar import statements the same way and your code should once again be working across all your installations.

Classical mechanics in HTML5 and Javascript

Ok, so we’re working on the home exam in classical mechanics and I decided to spend some time simulating the solution of the first exercise. Probably not the wisest way to spend time during an home exam, but what the heck. I’m learning some Javascript and HTML5, plus the fact that it’s always fun to verify results by looking at something moving!

So here you go. A pendulum on a cart.

I won’t go into details about deriving and solving the Lagrange equation for this system now, but I might get back to that at a later time. And of course there will be some added controls to this so you could set the initial positions, velocities and stuff. Sometime later, I hope.

Nanoparticles: An after-exam bonus

I guess you are pretty exhausted after the midterm exam this week and a lot of you have probably had other exams to deal with as well. What better time to share a game with you to put your minds at ease for a little while.

The game is called Nanoparticles and was written by me about half a year ago. It is based on Coulomb’s law with a few modifications and Newtonian physics. But instead of having you calculate anything, the game does it all for you in the background. You can download the game for Ubuntu, Linux, Windows and Nokia phones here (Mac and Android versions will be available sometime in the future):

Download Nanoparticles

Your mission is to make sure the positive particles you control does not crash into the negative ones. That would cause it to annihilate. To do this, you need to place out other positive and negative particles to keep your particle away for as long as possible.

The game is also open source, so if you’re interested in programming and C++, you can have a look at the source code too. More information about the game and how to obtain the source code is found here.

Animating plots and waves in Python using matplotlib

matplotlib is an amazing framework to do visual plots in Python. It compares well with GnuPlot and beats Matlab’s plotting abilities by having more features. Although it does lack some 3D support, you may simply choose a different framework for 3D plots thanks to Python’s flexibility . In which case I would recommend Mayavi as a superb 3D plotting engine which I have written about before.

But now, let’s have a look at matplotlib’s animation capabilities. The script below shows a very easy approach to animation in matplotlib. This results in an animation of the standing wave shown here:

The script is as follows:
Continue reading

Using binary data instead of text files in Python

It’s been a while since we’ve posted anything here. Sorry about that. It has just been too much to do lately to find the time to write new posts.

This post is also arriving a bit late to be useful for anyone this semester, but what it shows might be useful for others. Or maybe for someone taking the FYS1120 course at a later time. So I’ll post it here still.

In mandatory exercise 2 we needed to load a huge file containing data from an AM radio signal. This was available a Matlab file and as a text file with all the values on each row. Those of us using Python realized quite quickly that most of the time spent on this exercise went to load the data before performing any computations on the data.

However, in proper hindsight, I thought it might have been a better idea to save the file as binary data instead of as a text file. After all, binary data saves space and usually is a bit quicker to load.

Doing this in NumPy is extremely simple. Just load the text file data and save it back in NumPy’s binary format:

Continue reading

The beauty of Mayavi

Four charges with different magnitude plotted in 3D using Mayavi

In one of my earlier posts about Mayavi, I wrote about how you could visualize 2D field line plots using the flow function. At the end of that post I added that Mayavi is actually best at 3D plotting, and to follow up on that I’ll show you some of these plots with a few example Python scripts you might try out on your own.

First of all, you might want to know how to install Mayavi. For those lucky ones of you who have freed yourself and jumped on the Linux bandwagon, installing Mayavi should be quite easy. If you are using Ubuntu in particular, you may just install the package mayavi2 using either Synaptic or apt-get. If you are on Windows or Mac, you may either install Enthought’s own Python distribution (EPD) or give a shot at compiling on your own. Just note that EPD is quite expensive, even though all its components are open source, but if you are a student or academic user you could go ahead and download the academic version for free. It is basically the same as the commercial one, but with an academic license. (Kudos to Enthought for both making Mayavi open source, building an business model around it and still providing a great solution for students!)

Now, Enter 3D!

The way you do your plots in Mayavi depends on what you want to express. Most likely, you would prefer to show some simple plots giving just the necessary amount of information to tell you how the electric field behaves around your charges. A simple example of this is shown below:

Continue reading

Using Mayavi in Fys1120 oblig 1

3D plot for exercise 2d

There are only two days left before the first mandatory exercise in Fys1120 is due. Last time I posted about Mayavi I wrote in a comment that I would give you some hints on how to use it with the oblig. Actually, there are not so many useful applications for Mayavi in this particular oblig, since most plots are 2D and are better created using scitools.

In fact, the 3D plots in this oblig might also be plotted using scitools, but I decided to use Mayavi to test it out for this particular purpose.

Continue reading

Using Mayavi to visualize electric fields

Mayavi renders great field line plots.

While searching for a good Python module to visualize electric fields, I found Mayavi. Developed by Enthought, Mayavi is a very good module for visualizing a huge range of different scientific data sets. Everything from surfaces, flows and streamlines to bar charts, 3D plots and contour surfs are beautifully drawn on screen and exported to several file formats, such as PDF, PNG, EPS and more.

What I needed it for, however, was to visualize electric field lines in the course FYS1120 at the University of Oslo. We were told to use Matlab with the streamline and quiver functions, but even so, I wanted to use Python and decided to do a search and see if something similar was possible with Python. It took me some time to figure out how to use the scitools package to do streamline plots, but eventually I made it. However, these were a bit tedious to get working correctly and looked only about as good as the Matlab plots.

Continue reading