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

Prashant Saxena animator333@yahoo....
Mon Jun 29 08:20:06 CDT 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
    def addParticle( self ):
        """
        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[0].position*= random(1.0)
self.particles[1].position*= random(2.0)
self.particles[2].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


      Yahoo! recommends that you upgrade to the new and safer Internet Explorer 8. http://downloads.yahoo.com/in/internetexplorer/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090629/a284b6a4/attachment.html 


More information about the Numpy-discussion mailing list