Skip Navigation | ANU Home | Search ANU
The Australian
National University
Computational Science Education Outreach and Training (EOT)
Printer Friendly Version of this Document
VPython Tutorial on Apollo Flight
Vakul Talwar and Stephen Roberts
This tutorial introduces gravitational motion and explains with the help of examples, motion under the gravitational force. If you lack knowledge of programming, gravitation and Newton's laws of motion then we advice you to start with the projectile motion tutorial, which introduces VPython as a computational language and uses it to simulate a projectile motion with and without air resistance.
A clean version of the projectile motion program can be obtained from the link below. Example projectile motion program
Start IDLE by clicking on the snake icon on the panel at the bottom of your desktop. A window labelled 'Untitled' should appear. This is the window in which you will type your program. Open your projectile motion program, or cut and paste the clean version into IDLE. Check to see if it is working.

1  Newton's Law of Gravitation

Before we start the simulation we need to understand the concept of gravitation force given by Sir Isaac Newton in 1665.
Newton was the first to show that the force that holds the Moon in its orbit is the same force that makes an apple fall. Newton concluded that not only does the Earth attract an apple and the Moon but every body in universe attracts every other body, this phenomena of bodies to move towards each other is called gravitation. Quantitatively, Newton proposed a law, which is famously called as Newton's Law of Gravitation. This law states that every particle attracts any other particle with a gravitational force whose magnitude is given by
F= Gm1m2

r2
r12
(1)
here m1 and m2 are the mass of the particles, r = r2−r1 is the distance between them, r12 is a vector of unit length pointing from r1 to r2 and G is the gravitational constant, whose value is known to be
G = 6.67 ×10−11 N m2/kg2
The force F acting on the two particles is opposite in direction and equal in magnitude.

2  Simulating Flight Path of Apollo 13

To help us visualise and make this tutorial more interesting we will simulate the flight path of Apollo 13 when subjected to the Earth's gravitational force. We will need to calculate the parking orbit (orbit of the space craft to revolve around the Earth) of Apollo 13.
For successfully implementing the simulation we will need to know the acceleration, position and velocity of Apollo 13, which can be respectively found by equation (1) and the equations of motion learnt in the projectile tutorial.

2.1  Initial attributes and display window

In every computer we need to specify some basic variables without which a computer program fails to run. In this and the next section we will fill in these attributes,
from visual import *

G=6.67e-11
dt=10

Here G is the gravitational constant and dt is the time interval.
In VPython we can display various windows, but initially there is one Visual display window named scene. Any objects we begin creating will by default go into the scene. In this program we embellish the display window by modifying the attributes with the help of the display function.
scene = display(title = 'Earth and Apollo 13',
                width = 800, height = 600, background=(0,1,1))

Here we create a visual display window 800 by 600, with 'Earth and Apollo 13' in the title bar and with a background color of cyan filling the window. Various other attributes can also be specified. For more information visit the documentation of VPython in www.vpython.org

2.2  Attributes of Apollo 13 and Earth

Now we specify the attributes concerning the earth and apollo 13 needed for the simulation. This is done as:
###################################################
#   Declaration of Variables                      #
###################################################

#Declaring variables for earth
earth= sphere(pos=(0,0,0), color=color.green)
earth.velocity = vector(0,0,0)
earth.mass=5.97e24
earth.radius = 6378.5e3

#Declaring variables for apollo
apollo=sphere(pos=(6551e3,0,0),color=color.red)
apollo.velocity=vector(0,7801,0)
apollo.radius= 100e3

#Creating the trails for graphing the orbital path of Apollo
apollo.trail=curve(pos=[apollo.pos],color=apollo.color)

2.3  Labelling the Objects

VPython allows us to label objects in the display window with the label() command. In our program we label Earth and Apollo in the following manner.
#Labelling Earth and Apollo
earthlabel = label(pos = earth.pos,text='Earth',xoffset = 10, yoffset = 10,
                   space = earth.radius, height = 10, border = 6)
apollolabel = label(pos = apollo.pos,text='Apollo13',xoffset = 10, yoffset = 10,
                    space = apollo.radius, height = 10, border = 6)

A unique feature of the label object is that several attributes are given in terms of screen pixels instead of the usual "world-space" coordinates. For example the height of the text is given in pixels, with the result the text remains readable even when the object is moved far away.
For a brief explanation on the description of the attributes click here http://vpython.org/webdoc/visual/label.html

2.4  Acceleration of Apollo 13

To calculate the position and velocity of Apollo 13 as it moves around the earth we will need to know the acceleration of the space ship. We can then apply this acceleration to the equations of motion learnt from the projectile tutorial.
The acceleration of an object is determined by the forces on it, which in turn depends on its position compared to other objects. For example the force of gravity between two objects in space is given by equation (1). The acceleration of the two bodies can be calculated by
mapollo aapollo
=
GmapolloMearth

r2
⇒ aapollo
=
GMearth

r2
In Vpython we can calculate the acceleration of the two planets can be calculated as:
####################################################
#   Loop for moving the Apollo 13 around earth     #
####################################################

while (1==1):
    rate(1000)

    # Calculating the acceleration of the Apollo
    R = mag(earth.pos-apollo.pos)
    direction=norm(earth.pos-apollo.pos)
    apollo.acceleration=(G*earth.mass/R**2)*direction

Here mag gives the magnitude of the distance between the two planets and norm gives the normalised unit vector.
In our program we will need to insert this in the while loop part of the program because the acceleration of Apollo 13 will constantly change w.r.t time. The condition while (1==1): specifies the simulation to continue forever, as the condition 1==1 can never become false. We can exit the program by just closing down the display window.

2.5  Velocity and Position calculations

We will use the equations of motion used in the projectile tutorial to determine the new velocity and position of Apollo 13 at some later point in time. This is done in the form of
    #Calculating the updated position of the Apollo
    apollo.velocity=apollo.velocity+apollo.acceleration*dt
    apollo.pos=apollo.pos+apollo.velocity*dt
    apollo.trail.append(pos=apollo.pos)

Now we have managed to complete our simulation of Apollo 13, around the earth, due to earth's gravitational force.
The finished program seems like.
NOTE: We have ignored the force exerted by Apollo 13 on the earth because of it's small magnitude.

3  Gravitational Interaction between Three or more Bodies

The situation for with gravitational interactions between more than two objects/planets is more complicated than that for two planets. We need to use the principle of superposition of gravitational forces
F1 = F12+F13+F14+…+ F1n
(2)
Here F1 is the net force on planet 1 and F1n is the force exerted on particle 1 by particle n, which is calculated by the gravitational force formula given in equation (1).

4  The Earth, Apollo 13 and the Moon

Now we will take our simulation a step further and will now try to simulate how Apollo 13 escaped from it's parking orbit and went to Moon.
After Apollo 13 was launched, it orbited the earth 1.5 times, which is roughly 2.5hrs. after being launched from earth. At this time the translunar injection(TLI) took place. The translunar injection (ignition of engines designed to escape earth) accelerates the spacecraft such that its velocity changes from 7801 m/s to 10920 m/s. The direction of velocity will be determined by the current orientation of the spacecraft. It is therefore important to initiate the TLI in precisely the correct moment to put Apollo 13 on the correct path to the moon. In the following simulation we aim to determine this moment for the TLI.
In this exercise we will simulate this TLI and Apollo 13 path to the moon and make it hit the moon.
The circular motion of the spacecraft in the parking orbit, can be parametrised as follows
r(Θ) = rpark ( cos(Θ), sin(Θ),0) and v(Θ) = vpark (cos(Θ), sin(Θ), 0)
(3)
where rpark = 6551km and vpark = 7801m/s. The angle Θ for circular motion is given by Θ(t) = [2πt/T] where T is the period of one orbit.
Question: What is the time taken by Apollo 13 to complete one orbit around the earth?
To compute the path for the passage from the earth's parking orbit to the moon's orbit, we use the initial conditions for the position r as given in equation (3). However, for the velocity use the same direction as in equation (3) but with a higher magnitude vtrans = 10920m/s that the spacecraft has after burning the TLI engine.
r(Θ) = rpark (cos(Θ), sin(Θ),0) and v0(Θ) = vtrans (−sin(Θ), cos(Θ),0)
(4)

5  Logical Reasoning of the Simulation

Now we will briefly touch the logical reasoning steps to the simulation:
  1. We will need to know the parameters of the moon, time taken complete one orbit and angle at which the TLI takes place.
  2. We will need to check Apollo 13 acceleration due to gravitational force from the earth and moon.
  3. We will need to keep a check on angle for the TLI, as at that specified angle the velocity and radius of the spacecraft take the form of equation (3). But please note this method of increasing the velocity of the spacecraft from 7801m/s to 10920m/s is used to make the simulation easy to understand. In reality the spacecraft gradually increases it's velocity to the specified velocity.
  4. We simulate the TLI with initial conditions given by equation (3) and check when to end simulation.

6  Begin with the Simulation

Let's try incorporate step 1 from the previous section i.e. to include the parameters of the moon, time taken to complete one orbit and angle at which the TLI takes place. In VPython we define these variables in the Declaration of Variables section,
###################################################
#   Declaration of Variables                      #
###################################################

#Time(in secs) to complete 1 revolution around earth
T = 5040
#Time(in secs) for calculating simulation time
t = 0
#Angle at which Translunar Injection occurs
TLI_theta = pi
#Angle to check for Translunar Injection
theta = 0

6.1  Including the Moon

Now we include the coordinates of the moon
#Declaring variables for earth
earth= sphere(pos=(0,0,0), color=color.green)
earth.velocity = vector(0,0,0)
earth.mass=5.97e24
earth.radius = 6378.5e3

#Declaring variables for Apollo
apollo=sphere(pos=(6551e3,0,0),color=color.red)
apollo.velocity=vector(0,7801,0)
apollo.radius= 100e3

#Declaring variables for Moon
moon=sphere(pos=(384e6,0,0),color=color.black)
moon.velocity=vector(0,0,0)
moon.mass=7.3480e22
moon.radius=2.238e6

6.2  Creating trails and labelling

Now we create the trail for Apollo 13 and label the earth and moon.
#Creating the trails for graphing the orbital path of Apollo
apollo.trail=curve(pos=[apollo.pos],color=apollo.color)

#Labelling Earth and Moon
earthlabel = label(pos = earth.pos,text='Earth',xoffset = 10, yoffset = 10,
                   space = earth.radius, height = 10, border = 6)
moonlabel = label(pos = moon.pos,text='Moon',xoffset = 10, yoffset = 10,
                   space = earth.radius, height = 10, border = 6)

6.3  Apollo Acceleration

With the introduction of the moon into the simulation, the Apollo 13 experiences gravitational from from earth and moon. So the net force on Apollo 13 is,
Fapollo = Fmoon+Fearth
(5)
This makes the acceleration of Apollo 13,
mapollo aapollo
=
GmapolloMearth

r122
+ GmapolloMmoon

r132
(6)
⇒ aapollo
=
GMearth

r122
+ GMmoon

r132
(7)
where r12 is distance between the Earth and Apollo 13 and r13 is distance between the Moon and Apollo 13.

6.4  Applying the TLI

This part of the simulation is similar to the previous simulation where we only had the Earth and Apollo 13. The only difference is we keep a check on the angle Apollo 13 is making and when this angle equals the angle for TLI, we switch to the TLI part of the simulation. We perform this check by using an if statement of VPython and checking when the angle of Apollo becomes equal to angle of TLI.
TLI = true
while (apollo.pos.x<1.2*moon.pos.x):
    rate(1000)
    #Updating time
    t = t+dt
    #Checking angle for TLI
    theta = (2*pi*t)/T

    # At point TLI_theta increase velocity of craft
    if( (theta > TLI_theta) & TLI):
        TLI = false
        TLI_velocity = 10920
        R_parking = 6551e3

        apollo.velocity = TLI_velocity*vector(-sin(TLI_theta),cos(TLI_theta),0)
        apollo.pos = (6551e3*cos(TLI_theta), 6551e3*sin(TLI_theta), 0)

    # Calculating the acceleration of Apollo before TLI
    R12 = mag(earth.pos-apollo.pos)
    direction12=norm(earth.pos-apollo.pos)
    a12 = (G*earth.mass/R12**2)*direction12
    R13 = mag(moon.pos-apollo.pos)
    direction13 = norm(moon.pos - apollo.pos)
    a13 = (G*moon.mass/R13**2)*direction13
    apollo.acceleration = a12 + a13

    #Calulating the updated position of the Apollo
    apollo.velocity=apollo.velocity+apollo.acceleration*dt
    apollo.pos=apollo.pos+apollo.velocity*dt
    apollo.trail.append(pos=apollo.pos)

The program will enter the if loop, only if the statement if((theta > TLI_theta) & TLI comes true. If this statement comes true then all the instructions under it are executed. Here we are using a logical variable TLI to flag whether the TLI has been performed. We only want to jump into the statements in the if statement once.
The simulation is stopped (in the while statement) if the x position of the craft ever gets much larger that the x position of the moon. We intend to make the Apollo 13 loop around the moon and return to earth.
Final version of the this simulation can be found here. Try to calculate the perfect angle to enable the spacecraft to travel towards the moon and return, the so called free return trajectory.

7  Conclusion

We have simulated the motion of a Apollo 13 around the Earth and performing a Translunar Injection to escape the Earth's force and travel towards the Moon. The equations of motion have been used to calculate the updated velocity and position of Apollo 13.

8  Research questions for simulations involving gravitational interactions

More sophisticated programs solving gravitational interactions are used in recent research to:
  • test the long term stability of the solar system
  • study the evolution of the solar system and
  • find what range of planetary orbits were possible
  • explain the distribution of orbits within the asteroid belt
  • study the evolution of galaxies (Python example stars.py)
  • study motion near black holes (visualisation BlackHoles.java)
For more information on the history of research into solar system motion try Newton's Clock: Chaos in the Solar System, by Ivars Peterson