GlowScript 2.7 VPython

#Developed by A.Titus

launchday=125 #day to launch

rinitial=5.6*6.4e6 #initial distance of rocket from center of earth

marsrocketdist=500*6.8e6 #distance from mars considered a success

vinitial=5.4e3 #initial speed of rocket

# v = vinitial*norm(vec(-1.7,-4.6, 0)) strangely EJSS values does not coincident with glowscript, need to use -1.4 instead of -1.7

v = vinitial*norm(vec(-1.4,-4.6, 0)) #initial velocity of rocket; change the direction only

#pos unit is m

#time unit is s

#pos and vel generated by http://ssd.jpl.nasa.gov/horizons.cgi

#Aug 29, 2015

#

#no z components

marspos=1000*vec(-1.176389005286295E+08,2.135936464902488E+08,0)

marsvel=1000*vec(-2.027713823922661E+01,-9.661960006152405E+00,0)

earthpos=1000*vec(1.376368513677354E+08,-6.342242292720988E+07,0)

earthvel=1000*vec(1.203162796791987E+01,2.691852057718956E+01,0)

#constants

AU=1000*149597870.7 #AU in m

G=6.67384e-11

#diameters used for drawing sun, mars, and earth; diameters are not to scale

Dsun=0.2*AU

Dearth=0.1*AU

Dmars=0.1*AU

#mass

Msun=1.989e30

Mearth=5.97219e24

Mmars=6.4185e23

Mrocket=1e4

#time

day=24*3600

dt=2*3600

t=0

#set up 3D scene

scene = display(width=430, height=400, userspin=False, userzoom=True)

scene.append_to_title("""<br>Click the simulation to begin.""")

scene.append_to_title("""<br><br>Assumptions include:""")

scene.append_to_title("""<br>1. The rocket's speed near Earth's surface is 12 km/s, relative to Earth.""")

scene.append_to_title("""<br>2. The velocity of the rocket at an altitude of 2 earth radii is 5.4 km/s in the direction shown by the arrow.""")

scene.append_to_title("""<br>3. The thrusters are off during the entire flight. The net force on the rocket is the gravitational force by the Sun, Earth, and Mars.<br><br>""")

#scales for arrows

scale1=Dsun

# set up 3D objects

sun=sphere(dispay=scene, pos=vec(0,0,0), radius=Dsun/2, color=color.yellow)

mars=sphere(dispay=scene, pos=marspos, radius=Dmars/2, color=color.red)

earth=sphere(dispay=scene, pos=earthpos, radius=Dearth/2, color=color.blue)

rocket=sphere( pos=earth.pos+rinitial*norm(v), radius=earth.radius/2, color=color.orange)

rocketarrow1=arrow(dispay=scene, pos=earth.pos, axis=scale1*norm(v), color=color.white)

# create trails

marstrail=attach_trail(mars, radius=0.2*Dmars, trail_type="points", interval=2, retain=1000)

earthtrail=attach_trail(earth, radius=0.2*Dearth, trail_type="points", interval=2, retain=1000)

rockettrail=attach_trail(rocket, radius=0.2*rocket.radius, trail_type="points", interval=2, retain=1000)

rockettrail.stop

#create strings and labels

tstr="Time: {:.0f} days".format(0)

tlabel=label(pos=vector(0,1.2*mag(marspos),0), text=tstr)

launchstr="Starting Date: Aug. 29, 2015. \n"+launchday+" days until launch. \n Click to Run."

launchlabel=label(pos=vector(0,-1.2*mag(marspos),0), text=launchstr)

#set the range

scene.range=1.5*mag(marspos)

#this function is called when the rocket is launched

# it sets booleans and sets the initial velocitiy and momentum of the rocket

def launchRocket():

global vrocket, procket, rocketLaunched, justNowLaunched

rocketLaunched=True

justNowLaunched=True

vrocket=vearth+v

procket=Mrocket*vrocket

# initial positions, velocities, and momenta of all objects

earth.pos=earthpos

rocket.pos=earthpos

mars.pos=marspos

vearth=earthvel

vrocket=earthvel+v

vmars=marsvel

pearth=Mearth*vearth

pmars=Mmars*vmars

procket=Mrocket*vrocket

#booleans

rocketLaunched=False

run = False

justNowLaunched=False

# pause and then change the message

scene.waitfor('click')

launchstr="Launch Day "+launchday

launchlabel.text=launchstr

while True:

rate(200)

#earth

r=earth.pos-sun.pos

rmag=mag(r)

runit=norm(r)

Fearth=-G*Msun*Mearth/rmag**2*runit

pearth=pearth+Fearth*dt

vearth=pearth/Mearth

earth.pos=earth.pos+pearth/Mearth*dt

#mars

r=mars.pos-sun.pos

rmag=mag(r)

runit=norm(r)

Fmars=-G*Msun*Mmars/rmag**2*runit

pmars=pmars+Fmars*dt

mars.pos=mars.pos+pmars/Mmars*dt

#rocket

# launched, then compute Fnet, procket, and rocket.pos

if(rocketLaunched):

rocketarrow1.visible=False

#F by earth

r=rocket.pos-earth.pos

rmag=mag(r)

runit=norm(r)

Frocket_earth=-G*Mrocket*Mearth/rmag**2*runit

#F by sun

r=rocket.pos-sun.pos

rmag=mag(r)

runit=norm(r)

Frocket_sun=-G*Mrocket*Msun/rmag**2*runit

#F by mars

r=rocket.pos-mars.pos

rmag=mag(r)

runit=norm(r)

Frocket_mars=-G*Mrocket*Mmars/rmag**2*runit

#Fnet

Fnet=Frocket_sun+Frocket_earth+Frocket_mars

procket=procket+Fnet*dt

rocket.pos=rocket.pos+procket/Mrocket*dt

if(justNowLaunched):

justNowLaunched=False

#if not launched, just make the rocket at earth's position

else:

rocket.pos=earth.pos

#rocketarrow

rocketarrow1.pos=rocket.pos

#update time and label

t=t+dt

tstr="Time: {:.0f} days".format(t/day)

tlabel.text=tstr

#launch rocket on the launch day

if(t/day>launchday and rocketLaunched==False):

launchRocket()

#arrival at Mars

if(mag(rocket.pos-mars.pos)<marsrocketdist):

launchstr="Arrival at Mars. \nTravel time = {:.0f} days".format(t/day-launchday)

launchlabel.text=launchstr

scene.waitfor('click')