[Numpy-discussion] numpy/numexpr performance (particle simulation)

Prashant Saxena animator333 at yahoo.com
Mon Jun 29 09:20:06 EDT 2009

Hi,

I am doing a little test using numpy and numexpr to do a particle simulation. I never used either of them much and this is the first time I have to go deeper. Here is the code:

import numpy as np
import numexpr as nexpr

class Particle( object ):

def __init__( self, id ):
self.position = [0.0, 0.0, 0.0]
self.color = [0.5, 0.5, 0.5]
self.direction = [0.0, 0.0, 0.0]
self..id = id
self.lifeSpan = 0
self.type = 0

class Emitter( object ):

def __init__( self ):
self.particles = np.empty([], dtype=Particle())
self.currentParticleId = 0
self.numParticles = 1000
self.emissionRate = 10
self.position = [0.0, 0.0, 0.0]
self.rotation = [0.0, 0.0, 0.0]

"""
Add a single particle in the emitter.
"""
if self.currentParticleId < self.numParticles:
self.particles = np.append( self.particles, Particle( self..currentParticleId ) )
self.currentParticleId+=1

#######################################################
Problem 1:
self.particles = np.empty([], dtype=Particle())
In "Emitter" class, how do I initialize a numpy array of "Particle" type?

Problem 2:
If problem 1 can be solved, is it possible to use numexpr to alter the position of each particle's position
in emitter.particles array. For example, multiply x,y and z of each particle by using three random values. In other words

self.particles.position*= random(1.0)
self.particles.position*= random(2.0)
self.particles.position*= random(3.0)

If problem 1 cannot be solved then may be I could use something like this using simple list:

a = [Particle(0), Particle(1)]

How do I modify the position of each particle in array"a" using numexpr?

I would be using 1-5 million particles where each particle's attribute postion, rotation and color will be modified using some complex
equations. The above code is just the begining hence any tips for performance would be greatly apperciated.

Regards

Prashant