VPython basics
Claimed by Liubov Nikolenko
This is a beginner guide to programming in VPython. If you have never written code before, this article is for you.
Installation
To install VPython simply follow the instructions in the links given below.
Windows
Install VPython here
OSX
Install VPython here
Linux
Install VPython here
Getting started with Python
Introduction to basic Python use
The first two lines of your program should be:
- from __future__ import division
- from visual import *
The first line enables float division, so 3 / 2 = 1.5 (not 1) and the second line enables graphics module.
Variables
One of the most powerful features of a programming language is the ability to manipulate variables. A variable is a name that refers to a value. In order to assign some value to a variable, you should use the assignment token = . For example,
x = 5
This line of code assigns the variable x the value 5.
y = 0
x = y
This line of code assigns the variable x, the value associated with y, so x is equal to 0.
Note: unlike equality operator you cannot interchange the variables on different sides of the equality sign. Say, our initial conditions are:
x = 5
y = 0
The statement x = y assigns the variable x value 0, while the statement y = x assigns the variable y value 5.
Creating VPython Objects
- Sphere
ball = sphere(pos=(x_coordinate,y_coordinate,z_coordinate), radius=radius_of_the_sphere, color = color.color_of_the_sphere)
- Arrow
myArrow = arrow(pos=(x0_coordinate,y0_coordinate,z0_coordinate), axis=(x1_coordinate,y1_coordinate,z1_coordinate), color = color.color_of_the_arrow)
- Vector
myVector = vector(x_coordinate,y_coordinate,z_coordinate)
Manipulating VPython values
- Accessing attributes of the variable
To access the attribute of a given variable just use the syntax object.attribute (e.g. to access the position of the ball variable, you should do ball.pos)
- Updating variable
To update a variable (such as time, speed, force or the position of the object) you should do variable = variable + delta
Running VPython code
To run VPython code simply press F5. While running your code you will see the visual module and a shell (a place where all print out information is displayed)
Loops
There are two types of loops that can be use in Python: for loop and while loop. Basically, loops are used to tell the computer to execute the same task multiple times. While loops are more common for modeling physics concepts.
- While loops
The body of a while loop is executed, while a certain condition is true. Make sure to indent the statements that should be done in the while loop. While loop is equivalent to integration, because it manually breaks down the physical process into small parts and adds them up. While loop has the following structure:
- while (condition)
- the body of the while loop
- updating the loop
For example:
- while (t < 60)
- distance = distance + velocity * deltat
- t = t + deltat
Note: to see the process clearly it is common to slow down the frame rate by using rate(200) as a first line of a body of the while loop
Comments
Comments in the code have no impact on the code execution, because they are just ignored by the computer. Comments in VPython start with a pound sign (#) and end when a line ends. It is a good practice to place some useful information (like an explanation to a certain step) in the comments, so other people will find it easier to understand your code. For example:
x = x + 1 # incrementing x variable
Debugging techniques
Unfortunately, sometimes programs do not work in a way we intended them to work. Here are some tips to fixing your code.
- If you get an error try to understand the error message and fix your code accordingly. The most common errors are syntax errors, like parenthesis mismatch or wrong arguments passed into the function.
- Make sure you updating your condition for a while loop, so you don't have an infinite loop.
- Check if your are dividing by zero anywhere.
- Make sure you have correct indentation everywhere.
- Put plenty of print() statements, so you can know what is going on in every single stage of your code.
- Check your mathematical expressions for the order of precedence (e.g. x / y*z ! = x / (y * z))
- Try commenting out some steps to see which parts of your code do not work.
Useful built-in functions
Printing out information
- print(value)
Prints out the given value in the programming shell.
Math
- x**y
Raises x to the y-th power.
Vectors
- cross(vectorA, vectorB)
Calculates the cross product of two vectors
- mag(vector)
Calculates the magnitude of the vector
- norm(vector)
Calculates the unit vector of the vector
Connectedness
VPython will be used heavily in lab activities in modern sections of both Physics I and Physics II. One of the reasons it is used is because it is very easy to learn, especially when compared to languages like Java and MATLAB, and if provided with a shell, the process of changing values in order to manipulate the program is very straight forward.
Another aspect of VPython that is very applicable to this class is the ease at which loops can be created. A loop in a computer program is when a set of commands or lines of code are processed several times until some condition is satisfied. For example, a very simple loop can print out the numbers 1 to 10 by using a counter. Each time the code within the loop is processed, the value of the counter is printed out and the counter increases by one. Once the counter reaches 10, the loop can end and the commands within the loop will no longer be evaluated. This is applicable to many topics in this class, because VPython can loop to update velocity, position, momentum, and many other values in order to evaluate Electric fields, Magnetic fields, Gravitational forces, etc.
The basics learned from VPython can be extended beyond the scope of the class to topics from other class, or maybe even personal projects. For example, if you wanted to analyze the magnetic force between two objects, one moving and one stationary, VPython can be used to compute the magnetic field on the moving object at several different points in it's path of motion. This information can be used to determine what type of magnets to use in that specific application. This application of VPython is applicable to most majors, but especially engineering majors dealing with moving parts, electric currents, or magnetic components.
VPython also has industrial uses, which include modeling the movement of components. However, an advantage over other modeling techniques is that VPython allows for a visual representation of the moving components, as well as dynamic arrows or other symbols that can represent a changing velocity, magnetic field, or various other properties that will change with time or position.
How is this topic connected to something that you are interested in?
I like coding and I would love more people to share my passion, so I tried my best to make the coding part of physics more approachable
How is it connected to your major?
I am CS major and Python was the first language I have learned at Tech.
Is there an interesting industrial application?
Every simulation starting from interaction of molecules can be modeled in VPython to get the general idea of the process.
See also
GlowScript
GlowScript is an easy-to-use, powerful environment for creating 3D animations and publishing them on the web. You can write and run GlowScript programs right in your browser, store them in the cloud for free, and easily share them with others. GlowScript uses VPython as a programming language.
External links
References
This section contains the the references you used while writing this page
1. The cT Programming Language
3. GlowScript