\documentclass[a4paper,dvips,12pt]{article}
\usepackage[latin1]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{amsmath,amssymb}
\begin{document}

\section*{Introduction to VPython}
The goal of this week's lab exercise is to begin to familiarize ourselves with some of the skills needed to deal with programming, both writing your own programs (eventually) as well as reading, understanding, using, and perhaps even modifying programs written by others. This is a difficult task since there will be some of you who are already rather sophisticated programmers and others of you have no experience at all. Not only that, there is also the fact that we have limited time. Still, we do have time to get started.

Rather than write for you what VPython is, I leave it up to you to find out as much about it as you would like. The most important reference of which I am aware is the VPython page:
\begin{equation*}
\text{\texttt{\small{http://www.vpython.org/}}}
\end{equation*}
Here you will find a wealth of reference material, including a tutorial (in .pdf format):
\begin{equation*}
\text{\texttt{\small{http://www.vpython.org/VPython$\_$Intro.pdf}}}
\end{equation*}
In addition, you will find links here to downloadable versions of VPython for Windows, Linux, Unix, and Mac. You will not need to download VPython for these exercises, but if you find it a useful tool (or even just fun) it is very simple to install on the computing platform of your choice. A large number of other resources can be found using google, but be careful; as with all things google, not every link is terribly useful and some are completely useless.

VPython is a combination of a 3D graphics module amd the Python, a modern object-oriented programming language. While you will not be expected to do any complicated programming in Fysik 1, it is my hope (and belief) that this experience will help prepare you for more complicated tasks, and that other object-oriented languages (like java and c++) will look a little familiar when you meet them.

OK, enough with the blah-blah-blah. Let's get started!
\\ \\
VPython is installed on the computers in FørsteLab. To start VPython, open a terminal window and, at the prompt,type:
\begin{equation*}
\text{\texttt{\small{vpython myFirstProgram.py}}}
\end{equation*}
This will open two windows, one will be titled Python Shell and the other will be an editor window titled myFirstProgram.py.
Note that the .py ending identifies the file as a python program. In the editor, write your first line of program:
\begin{equation*}
\text{\texttt{\small{from visual import *}}}
\end{equation*}
This line tells the python interpreter to include everything (hence the *) from the module visual. You now have a working, but rather uninteresting program. Let us make it a little more interesting, shall we?

VPython has predefined geometrical objects like spheres, boxes, and arrows. We will use these today. Let us start by making a red ball at position (0,2,0) in our three dimensional coordinate system. Since sphere is an object, we need just to make a specific sphere which we can call mass1 (actually, we can call it anything we like, but let us call it mass1 for now). To create mass1, we insert the following into our program:
\begin{equation*}
\text{\texttt{\small{mass1=sphere(pos=(0.0,2.0,0.0),radius=0.5,color=color.red)}}}
\end{equation*}
Now save the program and run it by pressing F5. If you see a small window appear with a red sphere in it, you are now a programmer. If not, try again.
You have created an instance of the sphere object called mass1, with the properties position (x=0,y=2,z=0) and color (red). You can assign other properties as well. For example, we can give our red ball a mass and a velocity:
\begin{align*}
& \text{\texttt{\small{mass1.mass=5}}} \\
& \text{\texttt{\small{mass1.velocity=vector(2,0,0)}}} 
\end{align*}
You see that now we have a red ball of radius 0.5(what units?), with a mass of 5kg (why is it kg?) and a velocity of 2 (what units?) in the x-direction. Run the program and see what happens. Well, yes, not much happened. But, we can make things happen. We need to tell our ball how to move. That is, we need to introduce physics into the program. To do this, we will need to calculate some quantities(in particular, position and velocity) as a function of time. Let us start by having our ball move with constant velocity. To see this happen, we need to fix the scene in which we view our ball. To do this, add the lines
\begin{align*}
& \text{\texttt{\small{autoscale=0 $\#$this turns off auto scaling!}}} \\
& \text{\texttt{\small{scene.range=(5,5,5) $\#$gives the property range to the scene object}}} 
\end{align*}
If you rerun the program, you will see that the ball appears smaller than before. This is because the window is now defined to be larger than it was earlier. Now, let us make the ball move. To do this, we need to include time and calculate the ball's position as a function of time. Let us first do this for the case in which the ball is not subject to any acceleration. First, we must decide on a time step, that is, how long between our calculations. Let's first define this as a tenth of a second (note that for some processes this would be several orders of magnitude too large, while for others it would be several orders of magnitude too small. Discuss why this is and write some of the answers you reach in your log.) So, we insert the line
\begin{equation*}
\text{\texttt{\small{t=0.1 $\#$dt is delta t and is 0.1 second if we are using MKS}}}
\end{equation*}
Next, we would like for something to happen, so we make a loop that will be repeated for some number of calculations. In VPython, we do this as follows:
\begin{align*}
& \text{\texttt{\small{$\#$loop for 5 seconds:}}} \\
& \text{\texttt{\small{t=0 $\#$start at time 0}}} \\
& \text{\texttt{\small{while t<5:}}} \\
& \quad \text{\small{\texttt{rate(5) $\#$slows animation by 1/arg seconds}}} \\
& \quad \text{\small{\texttt{mass1.pos=mass1.pos+mass1.velocity*dt}}} \\
& \quad \text{\small{\texttt{t=t+dt}}} 
\end{align*}
What happens? Why does this happen? Now, make the mass fall subject to gravitational acceleration. What happened? What path did the ball follow?

To find out, you can draw the trajectory of the ball. I won't tell you how to do this, but you can find it in this example program:
\begin{equation*}
\text{\texttt{\scriptsize{http://www.nbi.dk/$\sim$bearden/BeardWeb/Teaching/Fys1L2007/VPython/Uge37/rvacircle.py}}}
\end{equation*}
Here, you can also find out how to draw an arrow. Using this example, draw the momentum vector for mass1 so that it is correct at all times. You should also be able to draw the path which the ball takes.

After you have done that, take a look at the program you downloaded, called rvacircle.py. What is the physical system being modelled by this program? What conditions is the particle subject to? What is the momentum of the particle? What do the various arrows represent? Add to this program the vector angular velocity. If you would like a bit of a challenge, make a program that has a particle moving on an elliptical path with constant speed. Does the vector veclocity behave in the way you would expect after what you did in the tutorial that began this session? If not, why not?

If you still have some time and energy, try to include a screen shot of your graphics window (showing the velocity and trajectory of the ball in your program) into your e-log.

Next week, we will continue with this program and make it more sophisticated so that we can make a reasonable simulation of week 36's spring gun. So, you have something fun waiting next week.
\\ \\
Ian Bearden 

\end{document}