Yes, the title is derived from Stephen Colbert's book - I Am America (And So Can You!). But here is the main point - you too can play with python.
What is python? Python is a programing language that is very easy to pick up. If you are into physics, then there is Vpython. This is just python with the visual module. Really, modules are what makes python so awesome. The visual module allows you to very easily render and animate simple 3D objects.
So, here is the plan. I am going to give you a fun program and you are going to run it and change things in it. It's that simple. The program has two objects orbiting a planet and you can control one of them. The idea is to get the two object to rendezvous in orbit. This program was inspired my study of the iPhone app Space Agency.
Without further delay, here is the code for this program. Yes, you will need to first install the visual module. If you follow the instructions at vpython.org, you should be fine. Here is the code for the orbit program. (I was going to embed the code, but it's just a little too long).
I tried to put way more comments than I normally do - so I hope that helps. Also, remember that I am not a professional programmer. There are likely some better ways to do things, but that is kind of the point. The point that any mere mortal can make a program like this. Wasn't there a famous song from The Carpenters:
Yes, that's how it goes.
If you want, you can stop reading and just go play with the program. However, if you want to understand more details, read on. At the end of this post I will also offer suggestions for things you can try adding to the program or changing.
The Physics
I do want to point out some of the physics in this program - just to be clear. Here is a force diagram for the space craft.
If the rocket thrust is not "on", the only force is the gravitational force. It has a vector value of:
Of course, G is the gravitational constant and M and m are the masses of the two interacting objects (in this case, the planet and the spacecraft). Really, it is r that we need to talk about. The r is the distance from the center of the planet to the center of the spacecraft. In order to make the gravitational force a vector, it is multiplied by r-hat (the r with a pointy hat over it). The hat means that r-hat is a unit vector. A unit vector has the same direction as r but a magnitude of 1 (and no units - which makes it odd being called a unit vector).
I did cheat a little bit in this game. I put the center of the Earth (or planet) at the origin of the coordinate system. This means that the location of the spacecraft is also the vector r from the center of the planet to the spacecraft. If the Earth were not at the center, then I would have to also calculate the r in the gravitational force as:
What about the thrust of the rocket? This would just be another force that pushes the rocket in the direction it is pointing. This force plus the gravitational force is the net force. But what do you do with the net force? You use the momentum principle. It says:
Really, the only other physics idea that is needed is the definition of velocity.
Just to be clear, the vector r is the vector position of the object (so, not exactly the same as the r above).
The Program
Now lets take a look at the program. If you look at the version on GitHub, there are line numbers. I will refer to the line numbers of important parts. Some of the lines have pretty full comments, so I don't think I need to go into that too much. Instead, let's just get to the important parts.
First, I set up all the stuff. In vpython, you can make these objects - like the sphere. There are three objects in this program. There is the Earth, the "other" object and the spacecraft (sc). For objects like the spacecraft, I can give them other properties. In line 50, I have sc.m = 1. This sets the mass of the spacecraft to the value 1. Why 1? Well, since the mass of the planet is assumed to be ginormous compared to the spacecraft. But shouldn't the mass have units? Well, the mass DOES have units. However, the program doesn't really know about the units. It just calculates stuff using the numbers you give it. It is the human's job to make sure the numbers are with the correct units.
Skip to line 81. This is where the bulk of the program runs. In this loop it does the following. First - calculate the force. I already described how to calculate the gravitational force above. The thrust force is just some value. To get the thrust force as a vector, I use the following code:
If you look at the code, ff is the magnitude of the thrust force. The norm(sc.axis) is a unit vector pointing in the direction of the orientation of the spacecraft. The "norm" function is included in the visual module. Oh, the "scence2.kb.keys" thing just gets the key stroke value from the keyboard. If the up key is pressed then the force is "on" then the force is set to some non-zero value. It seems to work.
Once I have the net force, the next step is to update the momentum. This is straight from the momentum principle. In physics, I can write that as:
Really, this is the key to the trick here. As the spacecraft moves, the gravitational force changes. However, if I have a small enough time interval small then this expression above is mostly true. To put this in the program, it would look like this:
I like to point out how this code matches the above momentum equation (called the momentum-update expression). If you think it looks weird, I understand. It looks like the sc.p variables would cancel. Ah ha! There is where you would make your mistake. In python the "=" is not an equal sign. It is an assign sign. The code says take the momentum of the spacecraft and set it to the old momentum plus the net force multiplied by the time interval.
After that, you do the same thing with the position. As a vector equation, I can write this.
This comes from the definition of average velocity. Again, this works because the time interval is small.
Really, that is all you need to know. Sure, there are some small things like updating the thrust arrow (which doesn't always work for me) - but you get the idea.
Things to Try
Now for some play. Here are some things you can try to change or add to the program.
- Add a graph that plots the speed of the space craft and the radius of the orbit. This documentation might be useful. Actually, I tried to add this but it messed up my visual window for some odd reason.
- Add a path that shows how the spacecraft should move to complete the rendezvous. I admit that this might be difficult. You would first have determine how to actually make this rendezvous and then add the path. Yes, it would be tough.
- What about an automatic thruster? What if you make a "smart" thruster that keeps changing direction to make the spacecraft get closer to the other object? That would be interesting.
- What if the gravitational force wasn't a 1/r2 type force? What if the gravitational force was constant or 1/r? Play with stuff.
- Change the magnitude of the thrust force. Again, just play around with it.
There you go. Once you start changing the program, you own it. What's the worst that could happen? If you break it in some way such that it doesn't work anymore, just copy the code from GitHub again.