Skip to content

Instantly share code, notes, and snippets.

@jdpage
Created February 17, 2012 00:44
Show Gist options
  • Save jdpage/1849256 to your computer and use it in GitHub Desktop.
Save jdpage/1849256 to your computer and use it in GitHub Desktop.
Magnetic field sim
from __future__ import division
from visual import *
from math import *
PROTON_CHARGE = 100
MU_ZERO = 1.256e-6
VEL = vector(.01, 0, 0) # m/s
SCALE = 20
observation_points = [vector(x * .01 - .1, .01 * cos(pi/4 * t), .01 * sin(pi/4 * t)) for x in range(20) for t in range(8)]
arrows = [arrow(pos=p, axis=vector(0, 0, 0), color = color.green) for p in observation_points]
particle = sphere(pos=vector(-.1, 0, 0), radius=.005, color = color.red)
def update_arrows(pos):
"""
updates arrows to reflect the particle being at position pos.
"""
for a in arrows:
r = a.pos - pos
b = MU_ZERO/(4*pi)*PROTON_CHARGE*VEL.cross(r/r.mag)/r.mag**2
a.axis = SCALE * b
while True:
rate(100)
update_arrows(particle.pos)
particle.pos += VEL * .01
if particle.pos.x >= .1:
particle.pos.x = -.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment