VPython running slow on simulation of objects collision

488 views
Skip to first unread message

Gabriel Luan

unread,
May 28, 2015, 1:09:25 AM5/28/15
to vpytho...@googlegroups.com
Hello guys,

I'm developing a algorithm to make a simulation of a large number of cylinders (About 900 cylinders) interacting to each other, using rotation and translation only on the X-Y axis.The program calculates each collision and updates the rotations and the positions of the objects on real-time.
But the animation is running very slow. Currently, the animation only works well when I reduce the number of cylinders to 100 or 200, but I need, at least, 900 objects. When I use 300 objects the program almost crashes.
I've tried to use points instead of cylinders, but  points do not have the same properties as the cylinders have. I also tried boxes, but no success, I've got the same problem.
Do you guys have any idea about what I could do to solve this problem?

Thank you,

Gabriel Luan

Bruce Sherwood

unread,
May 28, 2015, 2:18:22 AM5/28/15
to vpytho...@googlegroups.com
There are two different issues, computation and "rendering" (displaying the objects on the screen).

First, rendering. If you just try to display all those cylinders, can you rotate and/or zoom the scene? If this is very sluggish, the limitation is that it's just not possible for VPython to display so many objects. However, here is a program that displays 20*20*20 = 8000 cylinders, and rotating and zooming is smooth:

N = 20
L = 6
scene.range = L
length = 0.6*L/N
height = 0.4*L/N
for x in range(N):
  for y in range(N):
    for z in range(N):
      cylinder(color=vector(x/N,y/N,z/N), radius=height/2, length=length,
            pos=vector(L*(x/(N-1)-.5),L*(y/(N-1)-.5),L*(z/(N-1)-.5)))

So the issue is computation. Basic Python is not an extremely fast, because it is not fully compiled, largely as a result of not being "strongly typed". That is, x can be a simple number, or a vector, or a cylinder object, and it takes time to determine the meaning of x in a statement. Almost certainly your problem is that you have just plain run out of computing power.

There is however a possible approach, which is to use the numpy module (which is automatically imported by VPython). Two of the example programs installed with VPython use numpy features, stars.py and gas.py. Very large speedup of computations can be achieved by using numpy, especially in situations like yours (and stars.py and gas.py), where many objects receive essentially the same treatment and the calculations can be "vectorized", with a single statement updating many objects. Unfortunately, numpy is not easy to learn to use, though there is plenty of documentation of numpy.

There are also even more esoteric ways to speed up Python, including a tool called Cython, or writing the key part of the computation in a fast language like C or C++ and linking to those computations from your VPython program. These approaches are also not easy to learn to use.

What is the animation speed (iterations per second) that you've specified in our rate statement?

Reply all
Reply to author
Forward
0 new messages