0

I have a class that defines how water droplets move around in 3-dimensional space.

I need to compare the positions of every droplet with each other in order to determine if they collided with one another after a set amount of time. The way I define their positions is with three values, x,y,z. Each value is an attribute of the water droplet.

I have a large number of water droplets that I place into an array. How can my code compare all of them without getting repeat comparisons because they can form bigger droplets if they collide?

2
  • 1
    I would investigate numpy if they are numerics ... Commented Jul 13, 2012 at 22:45
  • Check out "pairwise testing". Commented Jul 13, 2012 at 22:47

2 Answers 2

3

You may wish to investigate the usage of octrees. By keeping your points in an octree, you can vastly reduce the number of comparisons you have to make (since you already know some points definitely don't collide with others).

Sign up to request clarification or add additional context in comments.

Comments

1

Can you sort based on position? Then you only need to compare adjacent elements ... If the step size you take is small, you could only sort once every few steps and compare the adjacent few droplets, merging as necessary.

import numpy as np

def merge(p1,p2):    
    #somehow merge particles.  With real particles, mass might matter too,
    #but my particles only have position :-)
    return (p1+p2)/2 #average location of particles.

def merge_particles(particles,merge_dist_squared):
    #Merge particles in 1 pass through the list
    #This will always work if the particles are sorted in order
    #It will fail if the particles aren't sorted well enough.
    i=1
    output=[]
    prev=particles[0] #location of previous (first) particle
    while i<(len(particles)):
        dist_vec=(particles[i]-prev)
        dist_squared=np.dot(dist_vec,dist_vec)
        if dist_squared > merge_dist_squared: #particle isn't close enough to merge
            output.append(prev)
            prev=particles[i]
        else:                       #particle is close enough to merge
            prev=merge(prev,particles[i])
        i+=1

    output.append(prev) #Have to make sure we add the last particle.

    return output

#create N particles:
N=1000
particles=[np.random.random(3) for x in xrange(N)]
particles.sort(key=tuple) #sort particles based on position.
particles=merge_particles(particles,0.1**2)
print len(particles)

6 Comments

Not quite sure what you mean...I have a maximum distance that two droplets can be from each other and still collide after a given time. So I was thinking if the distance between droplets after the time step is within that maximum distance I would then run my code that determines if they collided at some point
@user1524753 -- correct. But, in order to know if your droplet is close enough to merge, you need to compare its location with all the other droplets' locations. When you do this with all of the droplets you get an Order NN/2 algorithm (assuming if you compare droplet A with droplet B and not droplet B with droplet A). However, if you sort the particles first ( Order Nlog(N)), you only need to compare with a few particles nearby giving an Order N*log(N) algorithm during steps where you sort and Order N on steps without a sort. Does that make sense?
I understand the basic idea. However, Im not sure what you mean by the Order N*log(N) algorithm
I guess Im just not that familiar with sorting algorithms
@user1524753 -- I've posted some sample code. python uses a very good sorting algorithm that has an order of N*log(N) operations. In other words, if your list has 100 elements, the algorithm takes on the order of 100*log(100) operations. Compared to the 100*100/2 operations it takes to compare each particle with all the other particles.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.