Friday, February 27, 2009

Real-time Plotting from Numerical Simulations

Complex numerical simulations usually take a long time to run while using full CPU(s). If something goes wrong during such a run, we generally only find out in the end of the run when the traces of what went wrong are no longer available, debugging such codes is also not an easy task, because the code gets even slower running through the debugger, and when we don't know where the code is going to break down, it can become a painstaking process.

So any way to monitor such a running code without slowing it down is always welcome. As the title of this article points out, if your code is a numerical simulation, real-time plots of its progress is an extremely useful thing to have. However, traditional Python plotting tools such as Matplotlib, which I have been using for many years, is not a viable solution since it is not very fast and don't support very well real-time plotting.

Recently, while going crazy debugging one such simulation I decide to come up with a solution for real-time plotting in Python. I examine many candidates which I won't mention here, in order to keep this story simple. I finally settled down on a old but still very good solution: Gnuplot!

Since Gnuplot is a stand-alone program, implemented in C, it is very fast at drawing plots and with the help of python-gnuplot, I was able write a class with methods which would send data to Gnuplot and returned immediately without slowing down my running Python code! Gnuplot, on it's side plotted whatever data I throwed at it very fastly, giving me my much needed real-time scope into my simulations. Gnuplot is a real example of the Unix philosophy: Do one thing, and do it well!!

6 comments:

Anonymous said...

xmgrace together with GracePlot.py and grace_np.py is also a good solution for real-time plotting

aonlazio said...

So..would u share us some example code?

Unknown said...

Yeah that would be great to see how it works!

gragus said...

I wonder why you have not considered LiveGraph. It sounds like it is exactly what you need. It also has some features for quick-and-dirty data exploration - stuff that you want in a real-time plotter for simulation data.

GNUPlot is great, but I find it a bit clumsy to use. It's much better suited for more detailed analysis and plotting, once you roughly know what you are after.

I will not link to LiveGraph here in order not to create an impression that I am link-farming, but you can easily Google it, and if you think that I am making a good point, you can add a link yourself.

Cheers!

Unknown said...

hello, i want to know how can i do if i want to do a simulation with vpython, i need mor information baout that

Unknown said...

I want to know How can I close a graphics after that show, because i have many graphics and i want to put one over other for see a effect of a movie or a simulation, i am using visual python

ccp

Amazon