VPython tutorial 2 - University of Cape Town
VPython tutorial 2 - University of Cape Town
VPython tutorial 2 - University of Cape Town
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Name:<br />
Student Number:<br />
<strong>University</strong> <strong>of</strong> <strong>Cape</strong> <strong>Town</strong><br />
Department <strong>of</strong> Physics<br />
PHY1004W<br />
<strong>VPython</strong> Tutorial<br />
Voyage to the Moon<br />
6 April 2010<br />
Work in groups <strong>of</strong> 2. Carefully read and complete the following worksheet.<br />
Show your worksheet to a demonstrator before you leave this afternoon. Satisfactory completion <strong>of</strong> this<br />
task will result in 5 marks (!) being allocated to your laboratory year mark.<br />
The Ranger 7 spacecraft was launched by NASA on 28 July 1964 with the purpose <strong>of</strong> transmitting high<br />
resolution photographs <strong>of</strong> the surface <strong>of</strong> the moon while on a trajectory directly toward it. The<br />
spacecraft transmitted more than 4 000 photographs <strong>of</strong> excellent quality during its last 17 minutes <strong>of</strong><br />
flight, before impacting the moon, on 31 July 1964.<br />
In this <strong>tutorial</strong>, we will consider a very rough model <strong>of</strong> the Ranger 7 moon mission.<br />
Part A. Sit at a lab bench … you will move to a PC later.<br />
1. Say that the positions <strong>of</strong> the Earth and Moon are fixed in space, and there is a spacecraft in a stable<br />
circular orbit around the Earth as illustrated below. The spacecraft is launched on a trajectory to the<br />
moon by accelerating it to a velocity which allows it to escape its orbit around the Earth.<br />
(a) The path <strong>of</strong> the spacecraft to the moon depends on its launch velocity.<br />
Sketch on the diagram above a possible path that the spacecraft could follow, from its launch to its<br />
impact on the surface on the moon. Label your sketch clearly.<br />
(b) Describe the path in (a) in terms <strong>of</strong> the underlying physics concepts.<br />
1
2.<br />
(a) The spaceship is shown at two positions on the diagram below. Illustrate the forces acting on the<br />
spacecraft at positions A and B, using appropriate vector representations and labels.<br />
A<br />
B<br />
(b) Clearly explain how the forces acting on the spacecraft change as it moves from position A to<br />
position B.<br />
(c) Write down the mathematical expressions <strong>of</strong> these forces.<br />
3. Write down the mathematical expressions which describe the change in the position and momentum<br />
<strong>of</strong> the spacecraft as it moves from the earth to the moon.<br />
4. How long do you think the voyage will take? Explain your reasoning.<br />
STOP !<br />
… call a demonstrator who must check your answers and sign here ___________<br />
before you move to a PC.<br />
2
Part B … now move to a PC<br />
We will now write a <strong>VPython</strong> program to solve the equations <strong>of</strong> motion for a spacecraft<br />
on a voyage from the Earth to the Moon.<br />
I. Program organization<br />
The program will have the same basic structure as in previous <strong>VPython</strong> projects:<br />
1. An initialisation section in which: (a) numerical constants are assigned values; (b) 3D simulation<br />
objects are created; and (c) the initial conditions — positions and momenta — are specified.<br />
2. A loop — usually a while loop — in which: (a) the force on the objects is calculated; (b) the<br />
momenta and positions are updated; and (c) the simulation and time is updated.<br />
II. Getting started<br />
II.1. Creating a new program file<br />
You have already written a program for the motion <strong>of</strong> the Earth around the Sun; perhaps you<br />
have extended it to the case <strong>of</strong> a planet and two suns. In either case it is a good starting point for<br />
this program: read it in using IDLE and save it with a suitable name. You can then modify it rather<br />
that rewriting from scratch.<br />
Note: some <strong>of</strong> you are saving your files with strange names in odd places: save files under “My<br />
Documents” or in a folder located in this folder. You must remember to include the “.py” extension<br />
in the name, otherwise your code will not appear in colour. Also, choose a name that makes some<br />
sense to you, and is meaningful, e.g. “moonvoyage.py”.<br />
II.2. Creating the earth, moon and spacecraft<br />
Some data: the mass <strong>of</strong> the Earth is M E = 6 × 10 24 kg, mass <strong>of</strong> the Moon is M M = 7 × 10 22 kg,<br />
radius <strong>of</strong> the Earth is R E = 6.4 × 10 6 m and radius <strong>of</strong> the moon is R M = 1.75× 10 6 m. The distance<br />
from the Earth to the Moon is 4.0 × 10 8 m. The gravitation constant is G = 6.67 × 10 −11 N m 2 kg -2 .<br />
Take the mass <strong>of</strong> the spacecraft as 175 kg.<br />
Modify your previous program to use the new data: it should start something like<br />
from visual import *<br />
## objects<br />
earth = sphere( pos=(0, 0, 0), radius=6.4e6, color=color.blue )<br />
moon = sphere( pos=(4.0e8, 0, 0), radius=1.75e6, color=color.white )<br />
craft = sphere( pos=(...), radius=1.0e6, color=color.red )<br />
##constants<br />
G = 6.67e−11<br />
earth.M = 6.0e24<br />
moon.M = 7.0e22<br />
craft.M = 175.0<br />
3
Create a “trail”, using the curve object, to represent the path <strong>of</strong> the spacecraft.<br />
craft.trail = curve( pos=craft.pos, color=craft.color )<br />
II.3 Initial values <strong>of</strong> variables<br />
We will take the positions <strong>of</strong> the Earth and Moon to be fixed: the voyage will not take long — a<br />
few days, and the motion <strong>of</strong> the system around the Sun can be neglected in a first calculation. We<br />
can complicate things later. Take the Earth to be at the origin, and the Moon to be at a fixed<br />
position along the x-axis.<br />
To get to the Moon, the craft is launched from a circular orbit 50 km above the surface <strong>of</strong> the<br />
Earth. Replace the … in the following line <strong>of</strong> code by the position from which the spacecraft is<br />
launched. (Hint: it lies along either the x- or y-axis)<br />
craft = sphere( pos = (...), radius = 1.0e6, color = color.red )<br />
The craft is launched by accelerating it over a very short time to a speed <strong>of</strong> about 1.3 × 10 4 m s -1 .<br />
5.<br />
(a) What is the direction <strong>of</strong> this launch velocity? (Hint: it’s tangential to the initial orbit around the<br />
Earth.)<br />
craft.v = vector(...)<br />
(b) Why is the launch velocity not directed straight towards the moon?<br />
6. What is the initial momentum <strong>of</strong> the spacecraft?<br />
craft.p = …<br />
4
III Calculations<br />
Once launched, the craft travels under the influence <strong>of</strong> the gravitational forces <strong>of</strong> the Earth and<br />
the Moon.<br />
7.<br />
(a) Write down the lines <strong>of</strong> code which calculate the gravitational forces <strong>of</strong> the Earth and the Moon on<br />
the spacecraft.<br />
(b) Clearly explain at least 3 differences and/or similarities between this computational model and your<br />
mathematical model in (2c) in terms <strong>of</strong> both the code and the underlying physics.<br />
Your lines <strong>of</strong> code should look something like this:<br />
R = earth.pos−craft.pos<br />
magR = mag(R)<br />
force = −G*craft.M*earth.M*R/magR**3<br />
Your program should solve the equations <strong>of</strong> motion <strong>of</strong> the craft in the same fashion as the “earth<br />
and sun” program; only the craft’s motion needs to be calculated.<br />
8.<br />
(a) Write down the lines <strong>of</strong> code which update the position and momentum <strong>of</strong> the spacecraft.<br />
(b) Clearly explain at least 3 differences and/or similarities between this computational model and your<br />
mathematical model in (3) in terms <strong>of</strong> both the code and the underlying physics.<br />
5
We also need to specify a time interval between "snapshots." We’ll call this very short time<br />
interval deltat (∆t).<br />
To keep track <strong>of</strong> the total time elapsed, let’s also set a "stopwatch" time, t, to start from zero.<br />
Start with a time interval <strong>of</strong> 10 s.<br />
9. Estimate roughly how far the spacecraft travels in one time interval. Explain clearly how you arrived<br />
at your answer.<br />
Update the trail so that you can see the craft’s path.<br />
You can add a rate(50) statement in the loop to limit the update rate to 50 frames per second.<br />
You might like to put a statement scene.autoscale=0 before the start <strong>of</strong> the loop, to limit the<br />
disturbing rescaling <strong>of</strong> the view window.<br />
STOP ! … call a demonstrator who must check your code and sign here ___________ .<br />
10.<br />
(a) Sketch, with appropriate labels, what you observe in the visual output window when you run your<br />
program.<br />
(b) Describe your observation.<br />
(c) Clearly explain your observation in relation to your code.<br />
You may have to adjust the initial momentum and position <strong>of</strong> the spacecraft until its trajectory<br />
takes it to the moon. This should require only a few adjustments.<br />
6
11. Write down the initial conditions required to get the spacecraft to the surface <strong>of</strong> the moon.<br />
It is probably useful to check at each time step (i.e. in the loop!) whether or not your spacecraft<br />
has reached its intended destination. The following code fragment is helpful.<br />
Note that we use a conditional (if) statement to make the decision. We also use a print statement<br />
to print information in the output window. Finally, we use a break statement to “break out <strong>of</strong>” the<br />
loop and exit the program. The code assumes you have calculated the elapsed time and called it t.<br />
rmag = mag(earth.pos−craft.pos)<br />
# distance craft to centre <strong>of</strong> earth<br />
if rmag
(c) Compare the visual output you observe to your prediction in (1). Clearly explain at least 3<br />
differences and/or similarities between the visual output <strong>of</strong> your program and your prediction, in terms<br />
<strong>of</strong> :<br />
(i) the code, and<br />
(ii) the underlying physics.<br />
13. Use your program to determine the length <strong>of</strong> the voyage. Does the time calculated by your program<br />
agree with your prediction in question 4? Clearly explain why or why not.<br />
Part C … thinking about the energy <strong>of</strong> the system<br />
IV More Calculations<br />
IV.1 Work<br />
14. Is the work done on the spacecraft during its flight positive or negative? Explain clearly.<br />
15. Write down a mathematical expression describing the work done on the spacecraft during its flight.<br />
8
Given two vectors a and b the dot product is given by a function dot(a,b).<br />
The force and the displacement are calculated in the loop, at each time step. The position update<br />
statement can be broken down as follows:<br />
deltar = (craft.p/craft.M)*dt<br />
craft.pos = craft.pos+deltar<br />
and deltar can be used to calculate the work done in each time interval. The total work done is<br />
then just the sum <strong>of</strong> work done in each time interval.<br />
This is known as numerical integration.<br />
work = 0.0<br />
while ...<br />
...<br />
w = dot(...)<br />
work = work+w<br />
# i.e. replace work with old value + w<br />
Print out the value <strong>of</strong> work (together with its units!) when the voyage is over.<br />
16. What is the calculated value <strong>of</strong> the work done? Is it positive or negative? Does this agree with your<br />
prediction in question 14? Explain why or why not.<br />
17. What forces do the work?<br />
18. In your model, what is the system? (And what are the surroundings?)<br />
IV.2 Energy<br />
19. Write down the lines <strong>of</strong> code which will allow you to determine the change in kinetic energy <strong>of</strong> the<br />
spacecraft during its flight.<br />
9
Make the changes to your code and run your program.<br />
20. What is the change in kinetic energy <strong>of</strong> the spacecraft during its flight? How does it relate to the<br />
work done on the spacecraft?<br />
21. If you took the “whole problem” as a single system, what should be considered as part <strong>of</strong> this<br />
system?<br />
22. The energy <strong>of</strong> this new system should also be conserved. Is this the case here? Explain.<br />
23. What are the three interactions contributing to the potential energy <strong>of</strong> the new system?<br />
Part D … optional part<br />
Now include the Sun in your program, (you have all the data you need in your original ‘Earth-Sun’<br />
program), and let everything move. . .<br />
24.<br />
(a) Sketch what you observe in the visual output window when the Sun is included in the model, and<br />
the Earth, Moon and Sun are allowed to move.<br />
(b) Describe and explain your observation.<br />
Get a demonstrator to check your answers and sign here ___________ before you leave.<br />
10