Commits

trikitrok  committed 7820382

randomly created particles do not overlap now

  • Participants
  • Parent commits 1a45bab

Comments (0)

Files changed (2)

 
     def move(self, delta_t):
         self.x += delta_t * self.v
-
+        
+    def overlaps_with(self, other_particle, radius):
+        squared_distance = np.sum((other_particle.x - self.x)**2)
+        return squared_distance < 4.0 * radius**2
 
 class Box(object):
     def __init__(self, size=10):
         if x_ij_dot_v_ij > 0:
             return float('inf')
 
-        squared_distance = np.sum((x_ij) ** 2.0)
+        squared_distance = np.sum(x_ij ** 2.0)
 
         # all particles with same radius
         if squared_distance >= 4.0 * (self.radius ** 2):
     coords_upper_bound = box.size - radius
     coords_lower_bound = -box.size + radius
     for i in xrange(0, num_particles):
-        x = np.random.uniform(coords_lower_bound, coords_upper_bound, 2)
-        v = np.random.uniform(minimium_velocity, maximum_velocity, 2)
-        particles.append(Particle(x, v))
+        overlap = True
+        while(overlap):   
+            x = np.random.uniform(coords_lower_bound, coords_upper_bound, 2)
+            v = np.random.uniform(minimium_velocity, maximum_velocity, 2)
+            new_particle = Particle(x, v)
+            overlap = is_there_overlap(new_particle, particles, radius)    
+        particles.append(new_particle)
     return particles
 
+def is_there_overlap(new_particle, particles, radius):
+    for particle in particles:
+        if new_particle.overlaps_with(particle, radius):
+            return True
+    return False
+
 def create_simulation_from_files(coords_filename, vels_filename, **kwargs):
     particles = load_initial_particles(coords_filename, vels_filename)
     rules = CollisionRules(box=Box(kwargs['box_size']), eps=0.0)

File tests/test_edmd.py

     minimium_velocity = -1.0;
     seed = 10
     particles = edmd.create_random_particles(seed, box, 20, radius, minimium_velocity, maximum_velocity)
-    for particle in particles:
+    
+    for i, particle in enumerate(particles):
+        particle = particles[i]
         assert(box.contains(particle.x, radius))
         assert(minimium_velocity <= particle.v[0] and maximum_velocity > particle.v[0] and
-                minimium_velocity <= particle.v[1] and maximum_velocity > particle.v[1])
+                minimium_velocity <= particle.v[1] and maximum_velocity > particle.v[1])                
+        for other_particle in particles[i+1:]:
+            assert(not particle.overlaps_with(other_particle, radius))
 
-# def test_particle_overlap():
-#     part1 = Particle([0, 0], [0, 0])
+def test_particles_overlap():
+     part1 = Particle([0, 0], [0, 0])
+     part2 = Particle([0, 1], [0, 0])
+     assert(part1.overlaps_with(part2, 1))
+     
+def test_particle_do_not_overlap():
+     part1 = Particle([0, 0], [0, 0])
+     part2 = Particle([0, 2], [0, 0])
+     assert(not part1.overlaps_with(part2, 1))
+