Example #1
0
    def __init__(self, laws, initialParticles=[], initialTick=0):
        """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
        self.indexer = SpatialIndexer(laws.maxInteractRadius)

        self.laws = laws
        self.particles = []
        self.tick = initialTick
        self.particleDict = {}
        self.add(*initialParticles)
Example #2
0
    def __init__(self, laws, initialParticles=[], initialTick=0):
        """Initialise the particle system.
           laws             = laws object
           initialParticles = list of particles
           initialTick      = start value for tick counter
        """
        self.indexer = SpatialIndexer(laws.maxInteractRadius)

        self.laws = laws
        self.particles = []
        self.tick = initialTick
        self.particleDict = {}
        self.add(*initialParticles)
Example #3
0
 def __init__(self, laws, initialParticles = [], initialTick = 0):
     """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
     self.indexer = SpatialIndexer(laws.maxInteractRadius)
     
     self.laws         = laws
     self.particles    = []
     self.tick         = initialTick
     self.particleDict = {}
     self.add(*initialParticles)
Example #4
0
 def __init__(self, laws, initialParticles = [], initialTick = 0):
     """Initialise the particle system.
        laws             = laws object
        initialParticles = list of particles
        initialTick      = start value for tick counter
     """
     self.indexer = SpatialIndexer(laws.maxInteractRadius)
     
     self.laws         = laws
     self.particles    = []
     self.tick         = initialTick
     self.particleDict = {}
     self.add(*initialParticles)
Example #5
0
class ParticleSystem(object):
    """System of particles.
    
    Maintains the set of particles and runs the physics simulation over them
    the specified laws.
    """
    def __init__(self, laws, initialParticles=[], initialTick=0):
        """Initialise the particle system.
           laws             = laws object
           initialParticles = list of particles
           initialTick      = start value for tick counter
        """
        self.indexer = SpatialIndexer(laws.maxInteractRadius)

        self.laws = laws
        self.particles = []
        self.tick = initialTick
        self.particleDict = {}
        self.add(*initialParticles)

    def add(self, *newParticles):
        """Add the specified particle(s) into the system"""
        self.particles.extend(newParticles)
        for p in newParticles:
            self.particleDict[p.ID] = p
        self.indexer.updateLoc(*newParticles)

    def remove(self, *oldParticles):
        """Remove the specified particle(s) from the system.
           Note that this method does not destroy bonds from other particles to these ones.
        """
        for particle in oldParticles:
            self.particles.remove(particle)
            del self.particleDict[particle.ID]
        self.indexer.remove(*oldParticles)

    def removeByID(self, *ids):
        """Remove particle(s) as specified by id(s) from the system.
           Note that this method does not destroy bonds from other particles to these ones.
        """
        particles = [self.particleDict[id] for id in ids]
        self.remove(*particles)

    def updateLoc(self, *particles):
        """Notify this physics system that the specified particle(s)
           have changed position.
           
           Must be called if you change a particle's position,
           before calling run().
        """
        self.indexer.updateLoc(*particles)

    def withinRadius(self, centre, radius, filter=(lambda particle: True)):
        """Returns a list of zero or more (particle, distSquared) tuples,
           representing those particles within radius distance of the
           specified centre coords.

           distance-squared from the centre coords is returned too to negate
           any need you may have to calculate it again yourself.

           You can specify a filter function that takes a candidate particle
           as an argument and should return True if it is to be included
           (if it is within the radius, of course). This is to allow efficient
           pre-filtering of the particles before the distance test is done.
        """
        return self.indexer.withinRadius(centre, radius, filter)

    def run(self, cycles=1):
        """Run the simulation for a given number of cycles"""
        _indexer = self.indexer
        _laws = self.laws
        while cycles > 0:
            cycles -= 1
            self.tick += 1
            _tick = self.tick
            for p in self.particles:
                p.doInteractions(_indexer, _laws, _tick)
            for p in self.particles:
                p.update(_laws)
        _indexer.updateAll()
Example #6
0
class ParticleSystem(object):
    """\
    ParticleSystem(laws[,initialParticles][,initialTick]) -> new ParticleSystem object

    Discrete time simulator for a system of particles.

    Keyword arguments:
    
    - initialParticles  -- list of particles (default=[])
    - initialTick       -- start value of the time 'tick' count (default=0)
    """

    def __init__(self, laws, initialParticles = [], initialTick = 0):
        """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
        self.indexer = SpatialIndexer(laws.maxInteractRadius)
        
        self.laws         = laws
        self.particles    = []
        self.tick         = initialTick
        self.particleDict = {}
        self.add(*initialParticles)

        
    def add(self, *newParticles):
        """Add the specified particle(s) into the system"""
        self.particles.extend(newParticles)
        for p in newParticles:
           self.particleDict[p.ID] = p
        self.indexer.updateLoc(*newParticles)

        
    def remove(self, *oldParticles):
        """\
        Remove the specified particle(s) from the system.

        Note that this method does not destroy bonds from other particles to
        these ones.
        """
        for particle in oldParticles:
            self.particles.remove(particle)
            del self.particleDict[particle.ID]
        self.indexer.remove(*oldParticles)

        
    def removeByID(self, *ids):
        """\
        Remove particle(s) as specified by id(s) from the system.
        
        Note that this method does not destroy bonds from other particles to
        these ones.
        """
        particles = [self.particleDict[id] for id in ids]
        self.remove( *particles )
            
        
    def updateLoc(self, *particles):
        """\
        Notify this physics system that the specified particle(s)
        have changed position.
           
        Must be called if you change a particle's position,
        before calling run().
        """
        self.indexer.updateLoc(*particles)

        
    def withinRadius(self, centre, radius, filter=(lambda particle:True)):
        """\
        withinRadius(centre,radius[,filter]) -> list of (particle,distSquared)

        Returns a list of zero or more (particle,distSquared) tuples. The
        particles listed are those within the specified radius of the specified
        centre point, and that passed the (optional) filter function:

        filter(particle) -> True if the particle is to be included in the list
        """
        return self.indexer.withinRadius(centre, radius, filter)

            
    def run(self, cycles = 1):
        """Run the simulation for a given number of cycles (default=1)"""

        # optimisation to speed up access to these functions:
        _indexer = self.indexer
        _laws    = self.laws
        while cycles > 0:
            cycles -= 1
            self.tick += 1
            _tick = self.tick
            for p in self.particles:
                p.doInteractions(_indexer, _laws, _tick)
            for p in self.particles:
                p.update(_laws)
        _indexer.updateAll()
Example #7
0
class ParticleSystem(object):
    """System of particles.
    
    Maintains the set of particles and runs the physics simulation over them
    the specified laws.
    """

    def __init__(self, laws, initialParticles = [], initialTick = 0):
        """Initialise the particle system.
           laws             = laws object
           initialParticles = list of particles
           initialTick      = start value for tick counter
        """
        self.indexer = SpatialIndexer(laws.maxInteractRadius)
        
        self.laws         = laws
        self.particles    = []
        self.tick         = initialTick
        self.particleDict = {}
        self.add(*initialParticles)
    
    def add(self, *newParticles):
        """Add the specified particle(s) into the system"""
        self.particles.extend(newParticles)
        for p in newParticles:
           self.particleDict[p.ID] = p
        self.indexer.updateLoc(*newParticles)

        
    def remove(self, *oldParticles):
        """Remove the specified particle(s) from the system.
           Note that this method does not destroy bonds from other particles to these ones.
        """
        for particle in oldParticles:
            self.particles.remove(particle)
            del self.particleDict[particle.ID]
        self.indexer.remove(*oldParticles)

    def removeByID(self, *ids):
        """Remove particle(s) as specified by id(s) from the system.
           Note that this method does not destroy bonds from other particles to these ones.
        """
        particles = [self.particleDict[id] for id in ids]
        self.remove( *particles )
            
        
    def updateLoc(self, *particles):
        """Notify this physics system that the specified particle(s)
           have changed position.
           
           Must be called if you change a particle's position,
           before calling run().
        """
        self.indexer.updateLoc(*particles)

    def withinRadius(self, centre, radius, filter=(lambda particle:True)):
        """Returns a list of zero or more (particle, distSquared) tuples,
           representing those particles within radius distance of the
           specified centre coords.

           distance-squared from the centre coords is returned too to negate
           any need you may have to calculate it again yourself.

           You can specify a filter function that takes a candidate particle
           as an argument and should return True if it is to be included
           (if it is within the radius, of course). This is to allow efficient
           pre-filtering of the particles before the distance test is done.
        """
        return self.indexer.withinRadius(centre, radius, filter)
        
    def run(self, cycles = 1):
        """Run the simulation for a given number of cycles"""
        _indexer = self.indexer
        _laws    = self.laws
        while cycles > 0:
            cycles -= 1
            self.tick += 1
            _tick = self.tick
            for p in self.particles:
                p.doInteractions(_indexer, _laws, _tick)
            for p in self.particles:
                p.update(_laws)
        _indexer.updateAll()
Example #8
0
class ParticleSystem(object):
    """\
    ParticleSystem(laws[,initialParticles][,initialTick]) -> new ParticleSystem object

    Discrete time simulator for a system of particles.

    Keyword arguments:
    
    - initialParticles  -- list of particles (default=[])
    - initialTick       -- start value of the time 'tick' count (default=0)
    """
    def __init__(self, laws, initialParticles=[], initialTick=0):
        """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
        self.indexer = SpatialIndexer(laws.maxInteractRadius)

        self.laws = laws
        self.particles = []
        self.tick = initialTick
        self.particleDict = {}
        self.add(*initialParticles)

    def add(self, *newParticles):
        """Add the specified particle(s) into the system"""
        self.particles.extend(newParticles)
        for p in newParticles:
            self.particleDict[p.ID] = p
        self.indexer.updateLoc(*newParticles)

    def remove(self, *oldParticles):
        """\
        Remove the specified particle(s) from the system.

        Note that this method does not destroy bonds from other particles to
        these ones.
        """
        for particle in oldParticles:
            self.particles.remove(particle)
            del self.particleDict[particle.ID]
        self.indexer.remove(*oldParticles)

    def removeByID(self, *ids):
        """\
        Remove particle(s) as specified by id(s) from the system.
        
        Note that this method does not destroy bonds from other particles to
        these ones.
        """
        particles = [self.particleDict[id] for id in ids]
        self.remove(*particles)

    def updateLoc(self, *particles):
        """\
        Notify this physics system that the specified particle(s)
        have changed position.
           
        Must be called if you change a particle's position,
        before calling run().
        """
        self.indexer.updateLoc(*particles)

    def withinRadius(self, centre, radius, filter=(lambda particle: True)):
        """\
        withinRadius(centre,radius[,filter]) -> list of (particle,distSquared)

        Returns a list of zero or more (particle,distSquared) tuples. The
        particles listed are those within the specified radius of the specified
        centre point, and that passed the (optional) filter function:

        filter(particle) -> True if the particle is to be included in the list
        """
        return self.indexer.withinRadius(centre, radius, filter)

    def run(self, cycles=1):
        """Run the simulation for a given number of cycles (default=1)"""

        # optimisation to speed up access to these functions:
        _indexer = self.indexer
        _laws = self.laws
        while cycles > 0:
            cycles -= 1
            self.tick += 1
            _tick = self.tick
            for p in self.particles:
                p.doInteractions(_indexer, _laws, _tick)
            for p in self.particles:
                p.update(_laws)
        _indexer.updateAll()