Python Syntax: Difference between revisions
Line 80: | Line 80: | ||
Sphere: | Sphere: | ||
sphere= sphere(pos=vector(-4,-2,5), radius=.4, color=color.red) | sphere= sphere(pos=vector(-4,-2,5), radius=.4, color=color.red) | ||
Line 87: | Line 88: | ||
Vector: | Vector: | ||
vector=vector(0, 0, 0) | vector=vector(0, 0, 0) | ||
Trail: | Trail: | ||
trail = curve(color=sphere.color) | trail = curve(color=sphere.color) | ||
trail.append(pos=sphere.pos) | trail.append(pos=sphere.pos) | ||
Line 106: | Line 110: | ||
Graphs: | Graphs: | ||
Setup graphing windows: | |||
gdisplay(width=500, height=250, x=600, y=1) | gdisplay(width=500, height=250, x=600, y=1) | ||
ygraph = gcurve(color=color.yellow) | ygraph = gcurve(color=color.yellow) | ||
gdisplay(width=500, height=250, x=600, y=300) | gdisplay(width=500, height=250, x=600, y=300) | ||
Plotting: | |||
pgraph = gcurve(color=color.blue) | pgraph = gcurve(color=color.blue) | ||
ygraph.plot(pos=(time, Fnet.y)) | ygraph.plot(pos=(time, Fnet.y)) | ||
pgraph.plot(pos=(time, sphere.y)) | pgraph.plot(pos=(time, sphere.y)) | ||
Line 122: | Line 132: | ||
CONSTANTS: | |||
G = ? | G = ? | ||
mEarth = ? | mEarth = ? | ||
mmoon = ? | mmoon = ? | ||
mcraft = ? | mcraft = ? | ||
deltat = ? | deltat = ? | ||
t = ? | t = ? | ||
OBJECTS AND INITIAL VALUES: | |||
Earth = sphere(pos=vector(0,0,0), radius=6.4e6, color=color.cyan) | Earth = sphere(pos=vector(0,0,0), radius=6.4e6, color=color.cyan) | ||
scene.range=11*Earth.radius | scene.range=11*Earth.radius | ||
Moon = sphere(pos=(4e8, 0, 0), radius=1.75e6, color=color.white) | Moon = sphere(pos=(4e8, 0, 0), radius=1.75e6, color=color.white) | ||
Add a radius for the spacecraft. It should be BIG, so it can be seen: | |||
craft = sphere(pos=vector(-6.656e7,-3.648e6,0), radius= 10000, color=color.yellow) | craft = sphere(pos=vector(-6.656e7,-3.648e6,0), radius= 10000, color=color.yellow) | ||
vcraft = vector(206, 2645,0) | vcraft = vector(206, 2645,0) | ||
Line 146: | Line 166: | ||
Fnet_perp_arrow= arrow(color=color.magenta) | Fnet_perp_arrow= arrow(color=color.magenta) | ||
trail = curve(color=craft.color) | This creates a trail for the spacecraft: | ||
trail = curve(color=craft.color) | |||
And this prevents zooming in or out: | |||
scene.autoscale = 0 | |||
pscale=Earth.radius/mag(pcraft) | pscale=Earth.radius/mag(pcraft) | ||
fscale=Earth.radius/((G*mEarth*mcraft)*mag(craft.pos-Earth.pos)**2) | fscale=Earth.radius/((G*mEarth*mcraft)*mag(craft.pos-Earth.pos)**2) | ||
Line 153: | Line 178: | ||
print("p=", pcraft) | print("p=", pcraft) | ||
CALCULATIONS: | |||
while t < 165240: | |||
Sets time for loop to run: | |||
while t < 165240: | |||
This slows down the animation (runs faster with bigger number): | |||
rate(10000) | |||
Add statements here for the iterative update of gravitational | |||
force, momentum, and position. | |||
r = craft.pos-Earth.pos | r = craft.pos-Earth.pos | ||
Line 196: | Line 226: | ||
Uncomment these two lines to exit the loop if | |||
the spacecraft crashes onto the Earth. | |||
if rmag < Earth.radius: | if rmag < Earth.radius: | ||
break | break |
Revision as of 10:44, 8 April 2017
The Main Idea
This page discusses basic vPython functions and how they can be used to produce a model. vPython uses the same syntax as regular Python; however, vPython also allows you to produce a 3D model simulating the equations and computations your code is producing.
Mathematical Model
Vpython can be used with any equation. However, you may find some of the following useful:
Momentum Update:
pf = pi + Fnet*deltat
Position Update:
objectf.pos = objecti.pos + (pcart/mcart)*deltat
Gravitational Force:
CONSTANTS
G = 6.7e-11
mEarth = 6e24
mcraft = 15e3
deltat = 60
t = 0
Finds the change in position:
r=craft.pos-Earth.pos m=mcraft
Finds the magnitude of change in position:
rmag= mag(r)
Calculates the new magnitude of gravitational force:
Fmag=(G*mcraft*mEarth)/(rmag**2)
Calculates the direction of tbe change in position:
rhat=r/rmag
Calculates net force:
Fnet=-Fmag*rhat
Spring Force:
L0 = 0.3 Lvec = ball.pos - ceiling.pos Lhat = norm(Lvec) Lmag = mag(Lvec) Fspr = (-ks)*(Lmag - L0)*(Lhat)
Kinetic Energy:
Kinetic = (1/2)*(mball*(vel**2))
Computational Model
VPython is used to create computational models of various real world situations so that we can see how these equations used in the code can manipulate these situations.
Examples
Simple:
Creating Shapes:
Sphere:
sphere= sphere(pos=vector(-4,-2,5), radius=.4, color=color.red)
Arrow:
bt=arrow(pos=sphere.pos, axis=sphere2.pos-sphere.pos, color=color.cyan)
Vector:
vector=vector(0, 0, 0)
Trail:
trail = curve(color=sphere.color)
trail.append(pos=sphere.pos)
Setting Scene Range:
scene.range=11*sphere.radius
Helix:
spring = helix(pos=ceiling.pos, color=color.cyan, thickness=.003, coils=40, radius=0.015)
Intermediate:
Graphs:
Setup graphing windows:
gdisplay(width=500, height=250, x=600, y=1)
ygraph = gcurve(color=color.yellow)
gdisplay(width=500, height=250, x=600, y=300)
Plotting:
pgraph = gcurve(color=color.blue)
ygraph.plot(pos=(time, Fnet.y))
pgraph.plot(pos=(time, sphere.y))
Difficult:
Using Loops to update Equations:
CONSTANTS:
G = ?
mEarth = ?
mmoon = ?
mcraft = ?
deltat = ?
t = ?
OBJECTS AND INITIAL VALUES:
Earth = sphere(pos=vector(0,0,0), radius=6.4e6, color=color.cyan)
scene.range=11*Earth.radius
Moon = sphere(pos=(4e8, 0, 0), radius=1.75e6, color=color.white)
Add a radius for the spacecraft. It should be BIG, so it can be seen:
craft = sphere(pos=vector(-6.656e7,-3.648e6,0), radius= 10000, color=color.yellow) vcraft = vector(206, 2645,0) pcraft = mcraft*vcraft pArrow=arrow(color=color.green) fArrow=arrow(color=color.cyan) dpArrow=arrow(color=color.red) Fnet_tangent_arrow = arrow(color=color.yellow) Fnet_perp_arrow= arrow(color=color.magenta)
This creates a trail for the spacecraft:
trail = curve(color=craft.color)
And this prevents zooming in or out:
scene.autoscale = 0 pscale=Earth.radius/mag(pcraft) fscale=Earth.radius/((G*mEarth*mcraft)*mag(craft.pos-Earth.pos)**2) dpscale=500*Earth.radius/mag(pcraft) print("p=", pcraft)
CALCULATIONS:
Sets time for loop to run:
while t < 165240: This slows down the animation (runs faster with bigger number):
rate(10000)
Add statements here for the iterative update of gravitational force, momentum, and position.
r = craft.pos-Earth.pos rmag = sqrt(r.x**(2)+r.y**(2)+r.z**(2)) Fmag= G*mEarth*mcraft/(rmag**2) rhat= r/rmag rmoon= craft.pos - Moon.pos rmoonmag= mag(rmoon) rmoonhat= norm(rmoon) Fmoonmag= G*mmoon*mcraft/(rmoonmag**2) Fmoon= -Fmoonmag*rmoonhat p_init= mag(pcraft) pcraft_i=pcraft+vector(0,0,0) Fearth= -Fmag*rhat Fnet= Fearth + Fmoon pcraft=Fnet*deltat+pcraft p_final=mag(pcraft)
Fnet_tangent = (p_final-p_init)*norm(pcraft)/deltat Fnet_tangent_arrow.pos=craft.pos Fnet_tangent_arrow.axis=Fnet_tangent*fscale Fnet_perp = Fnet-Fnet_tangent Fnet_perp_arrow.pos=craft.pos Fnet_perp_arrow.axis=Fnet_perp*fscale vcraft=pcraft/mcraft craft.pos=vcraft*deltat+craft.pos pArrow.pos=craft.pos pArrow.axis=pcraft*pscale fArrow.pos=craft.pos fArrow.axis=Fnet*fscale deltap= pcraft-pcraft_i dpArrow.pos=craft.pos dpArrow.axis=deltap*dpscale scene.center=craft.pos scene.range=craft.radius*600
Uncomment these two lines to exit the loop if the spacecraft crashes onto the Earth.
if rmag < Earth.radius: break
trail.append(pos=craft.pos) t = t+deltat
Connectedness
vPython codes are extremely useful for modeling physics situations. However, the coding skills learned in this class can be applied to almost anything. For example, Aerospace Engineers are becoming increasingly dependent on computer simulations to test ideas before prototyping to reduce costs.
History
vPython was released in 2008. It was developed by researchers at Carnegie Mellon University. It is largely used for educational purposes especially producing physics models.