Enter the following code in a new cell in the worksheet to define the spheres using NumPy:

import numpy dimension = 20 num_particles = 500 radius = 1.0 rng = numpy.random.mtrand.RandomState(seed=[1]) x_np = rng.uniform(0, dimension, num_particles) y_np = rng.uniform(0, dimension, num_particles) z_np = rng.uniform(0, dimension, num_particles)

Now, enter the following code in another cell to detect collisions:

%time collisions_4 = numpy.zeros(num_particles, dtype=numpy.bool) r_min = numpy.float64(4*radius**2) for i in range(num_particles): for j in range(0,i): r_squared = (x_np[i] - x_np[j])*(x_np[i] - x_np[j]) \ + (y_np[i] - y_np[j])*(y_np[i] - y_np[j]) \ + (z_np[i] - z_np[j])*(z_np[i] - z_np[j]) if r_squared < r_min: ...

Start Free Trial

No credit card required