Table of Contents

Common Commands and Tips for Python

In this class, we are often going to use VPython to create computational models, which will serve as a powerful tool to help us create visualizations and apply the ideas in this course to more real-world contexts. Below are some of the common Python commands that we will use in this course and some coding tips compiled by previous EMP-Cubed students. (Note: we do not expect you to have any coding experience prior to this course, and we will not expect you to write a program from scratch. We will primarily be asking you to interpret chunks of code with your group members and adjust/modify pieces of code that you will be given.)

Tips for Coding

A=4+4/2
B=(4+4)/2
## Set the radius (this line would be ignored by the program)
R=20 #cm (Everything in this line before the # would run in the program, everything after the # is ignored)

Common Commands

A=5.42*10**4
cloud = sphere(pos=vec(0,3500,0), color=color.white, radius=100)
object1=box(pos=vec(0,0,0), length=2, width=2,color=color.green)
object1.pos=vec(3,4,5)
position1=vec(3,4,5)
position2=vec(2,1,0)
separation=position2-position1  #This will give separation=vec(-1,-3,-5)
testing=separation-4 #This will give an error
vector1=vec(1,2,3)
mag1=mag(vector1) #This will calculate sqrt(1^2+2^2+3^2)
A=7*9/3
print(A) #This will then print out the number 21
A=vec(1,2,3)
B=vec(4,5,6)
AB=dot(A,B)  #This will calculate (1*4)+(2*5)+(3*6)
print(AB)  #This will print out 32
C=vec(5,6,7)
D=vec(3,2,1)
CD=cross(C,D)  #This will calculate (6*1-2*7)i-hat - (5*1-7*2)j-hat + (5*2-6*3)k-hat
print(CD)  #This will print out the vector (-8, 9, -8)
get_library('https://cdn.rawgit.com/PERLMSU/physutil/master/js/physutil.js')  #Imports physutil

MassOfBall = 20
SpeedOfBall = 10

t=0
dt=0.1
tf=10

ExampleGraph2 = PhysGraph(numPlots = 1)   #Defines graph window

while t < tf:
    rate(50)
    
    KineticEnergy = 0.5*MassOfBall*SpeedOfBall**2
    ExampleGraph2.plot(t, KineticEnergy)   #Plot function is identical
    SpeedOfBall = SpeedOfBall - 0.5
    
    t = t+ dt
get_library('https://cdn.rawgit.com/PERLMSU/physutil/master/js/physutil.js')  #Imports physutil

Moving_Sphere = sphere(pos=vector(0,0,0), radius = 0.5, color = color.cyan)  #The object we want to place arrows for, a cyan sphere.
Sphere_velocity = vector(5,5,0)  #Let's say we want a MotionMap for the velocity of this sphere.

Setting up time parameters for the motion
t=0
dt=0.1
tf=10

#Creating the MotionMap via MotionMap(object, stop time, number of arrows, arrow size, arrow color, number labels on arrows or not)
Moving_Sphere_MotionMap = MotionMap(Moving_Sphere, tf, 5, markerScale=1, markerColor=color.orange, labelMarkerOrder=False)

while t < tf:
    rate(100)
    
    Moving_Sphere.pos = Moving_Sphere.pos + Sphere_velocity * dt   #Sphere is moving according to its velocity
    Moving_Sphere_MotionMap.update(t,Sphere_velocity)   #MotionMap update function plots the vector as time goes by
    
    t = t + dt