Cyclotron motion with Euler-Richardson Integration

Straight import of existing python script using "% import file.py" Broken up into cells, but not much else done.

In [1]:
from vpython import *   # NOTE: This has changed from "from visual import *"
In [2]:
####################################################################
# Set the scene with transparent x-y, x-z, and y-z planes.
# Also sets vewpoint so that z is up, and initially looking from (1,1,1)
# to the origin.
####################################################################
scale = 10.                                   # Size of transparent planes
canvas(forward=vec(-1,-1,-1),up=vec(0,0,1))  # Gives standard physics view
xyplane = box(pos=vec(0,0,0), length=scale,width=scale,height=0.1, color = color.yellow, opacity=0.1)
yzplane = box(pos=vec(0,0,0), length=0.1,width=scale,height=scale, color = color.yellow, opacity=0.1)
xzplane = box(pos=vec(0,0,0), length=scale,width=0.1,height=scale, color = color.yellow, opacity=0.1)

label(pos=vec(6,0,0), text='x')
label(pos=vec(0,6,0), text='y')
label(pos=vec(0,0,6), text='z')
In [3]:
######################################################################
# Functions
######################################################################
def e(particle):
    e0 = 0.
    return vector(e0,0.,0.)

def b(particle):
    b0 = 1.
    return vector(0,0,b0)

def force(particle):
    return particle.charge*(e(particle.pos)+ cross(particle.vel,b(particle.pos)))

def stepEuler(particle):
    a = force(particle)/particle.mass             # Calculate accel.
    particle.pos += particle.vel*dt               # Update position
    particle.vel += a*dt			      # Update velocity
    
def stepER(particle):
# Note that in this function, xOld and vOld defined as vector().
# This is necessary, because if it's not done that way, xOld and vOld
# are tied to the current value of particle.pos and particle.vel, i.e., 
# updating particle.pos or particle.vel changes xOld and vOld -- they're
# not "Old" anymore.
    a = force(particle)/particle.mass
    xOld = vector(particle.pos)
    vOld = vector(particle.vel)
    vMid = vOld + a*dt/2            # Extra lines in following for clarity 
    particle.vel = vMid             # about xOld, vOld, xMid, vMid, aMid
    xMid = xOld + vOld*dt/2 
    particle.pos = xMid
    aMid = force(particle)/particle.mass
    particle.vel = vOld + aMid*dt
    particle.pos = xOld + vMid*dt
    
In [4]:
######################################################################
# Definitions and initial conditions 
######################################################################

pos1Init = vec(0,3.,0)
vel1Init = vec(3,0,0)
t = 0.
dt = 0.01

charge1 = sphere(pos=pos1Init,vel=vel1Init, radius=0.2, color=color.red, mass=1., charge=1.)

charge1.trail = curve(color=charge1.color,radius=0.02)
In [ ]:
while True: 
    rate(100)
    stepER(charge1)
    charge1.trail.append(pos=charge1.pos)
    t += dt
    # print(t,charge1.pos)
In [5]:
%reload_ext version_information
%version_information vpython, scipy
ERROR! Session/line number was not unique in database. History logging moved to new session 239
Out[5]:
SoftwareVersion
Python3.5.1 64bit [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
IPython4.2.0
OSLinux 3.10.0 327.36.3.el7.x86_64 x86_64 with redhat 7.2 Maipo
vpython0.3.11
scipy0.17.1
Fri Dec 09 11:35:48 2016 EST
In [ ]: