Straight import of existing python script using "% import file.py
"
Broken up into cells, but not much else done.
from vpython import * # NOTE: This has changed from "from visual import *"
####################################################################
# 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')
######################################################################
# 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
######################################################################
# 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)
while True:
rate(100)
stepER(charge1)
charge1.trail.append(pos=charge1.pos)
t += dt
# print(t,charge1.pos)
%reload_ext version_information
%version_information vpython, scipy