def run_simulation(mesh,e_field):
	reaper=[]
	current = 0
	for particle in mesh.particles:
		ds.randomElectronMovement(particle,e_field,
						None,mesh,reaper)
	for index in xrange(len(mesh.particles)):
		p = mesh.particles[index]
		if p.dead == False: #if we didn't kill it.
			if(du.out_of_bounds(mesh,p.pos)): 
				#need to figure out exit
				reaper.append(index)
				p.dead = True
				current += mc.current_exit(p,mesh)
			else:
				p.id = kdtree_c.find_point_id(mesh.kdt,p.pos)
	reap_list(mesh.particles,reaper)
	return current
	def __init__(self,pos,momentum,dx,lifetime,charge,mesh):
		self.uid = p_count.next()
		self.pos = array(pos)
		#mesh.trajectories[self.uid] = [tuple(self.pos)]
		#print mesh.trajectories[0]
		self.momentum = array(momentum)*mesh.scale
#		self.dx = array(dx)
#		self.lifetime = 2*rd.random()#int(lifetime)
		self.charge = charge
		self.dead = False
		self.mass = mesh.material[tuple(pos)].electron_mass #mesh.mass[charge]

		#mesh_id data
		#self.id = du.vert_index(mesh,self.pos)
		self.id = kdtree_c.find_point_id(mesh.kdt,self.pos)
		#self.meshpos = mesh.coordinates()[self.id]
		#particles_point must be update on move
#		mesh.particles_point[mesh.point_index[tuple(self.meshpos)]].append(self)
		self.part_id = len(mesh.particles)
		mesh.particles.append(self)
import numpy.random as random


def dist(point1, point2):
    vec1, vec2 = np.array(point1), np.array(point2)
    return np.linalg.norm(vec1 - vec2)


n = 100000
no_fail = True
points = map(list, zip(random.rand(n), random.rand(n)))
tree = kd.new_kdtree(points)

for id, point in zip(range(len(points)), points):
    offset = list((point[0] + 0.01, point[1] + 0.01))
    res = kd.find_point_id(tree, offset)
    if id != res:
        better = dist(offset, points[res])
        expected = dist(offset, point)
        if better > expected:
            no_fail = False
            print "point:", point, expected
            print "offset:", offset
            print "believed:", points[res], better
            break
if no_fail == True:
    print "Success for 2d!", id
else:
    print "Failure for 2d", id

print "Longest chain 2D", kd.longest_chain(tree, 0, 0)