from visual import * #adapted from Chabay and Sherwood scene.width = 600 scene.height = 600 # set up params for the coil Radius = 1.6 L = 2*pi*Radius Ncoils = 50 #current and stuff I = 1.0 k = 1E-7 # mu-zero/4pi Bscale = (2.)/(4*pi*1E-7*Ncoils*I/L) #just combining terms dphi = 2.*pi/Ncoils #use this to adjust step size - effects accuracy of simulation phi = arange(0,Ncoils*pi+dphi,dphi) #Create the coil using a curve object a = 1 #a,b set scaling for circular or elliptical coils b = 1 helix=curve(x = a*Radius*cos(phi/2), y = b*Radius*sin(phi/2), z = phi/100) helix.color = (1,0.7,0.2) helix.radius = 0.02 delta = helix.pos[1:] - helix.pos[:-1] center = (helix.pos[:-1] + helix.pos[1:])/2. scene.range = 5*(Radius) vwidth = L/100 #Physics happens here! This is where the Biot-Savart Law is used def BField(obs): r = obs-center rmag = mag(r) rmag.shape = (-1,1) return sum(k*I*cross(delta, r)/rmag**3) #notice the different way of writing the Biot-Savart Law Bvector = arrow(axis=(0,0,0), shaftwidth=vwidth, color=(0,1,1)) drag = 0 while 1: if scene.mouse.clicked: newobs = scene.mouse.pos obs = newobs Bvector.axis = Bscale*BField(obs) Bvector.pos = obs