VPython basics: Difference between revisions
No edit summary |
No edit summary |
||
Line 6: | Line 6: | ||
[[File:EMWave-Maxwell v275.gif]] | [[File:EMWave-Maxwell v275.gif]] | ||
==Installation== | ==Installation or Online Use== | ||
Python can be rather difficult to install. However, you can use an online version in order to do all code online. | |||
1. Go to [http://www.glowscript.org/] | |||
2. Log into a Google account. | |||
3. Click here in "You are signed in as [account name you select] and your programs are here." | |||
4. Click the blue Create New Program | |||
To install VPython simply follow the instructions in the links given below. After the installation is completed just open the VIDLE for Python (usually there is a shortcut created on your desktop) and start typing your code. | To install VPython simply follow the instructions in the links given below. After the installation is completed just open the VIDLE for Python (usually there is a shortcut created on your desktop) and start typing your code. | ||
===Windows=== | ===Windows=== | ||
Line 28: | Line 36: | ||
::from __future__ import division | ::from __future__ import division | ||
::from visual import *</code> | ::from visual import *</code> | ||
The first line enables float division, so 3 / 2 = 1.5 (not 1) and the second line enables graphics module. | The first line enables float division, so 3 / 2 = 1.5 (not 1) and the second line enables graphics module to plot your 3D simulation. | ||
===Variables=== | ===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. It is a good practice to give your variables meaningful names, so the code will become less confusing for you and other people reading it. In order to assign some value to a variable, you should use the assignment token <code>=</code> . | 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. It is a good practice to give your variables meaningful names, so the code will become less confusing for you and other people reading it. In order to assign some value to a variable, you should use the assignment token <code>=</code> . |
Revision as of 16:52, 13 March 2018
Claimed by Taylor Dingler Spring 2018
This is a beginner guide to programming in VPython. If you have never written code before, this article is for you.
Installation or Online Use
Python can be rather difficult to install. However, you can use an online version in order to do all code online.
1. Go to [1] 2. Log into a Google account. 3. Click here in "You are signed in as [account name you select] and your programs are here." 4. Click the blue Create New Program
To install VPython simply follow the instructions in the links given below. After the installation is completed just open the VIDLE for Python (usually there is a shortcut created on your desktop) and start typing your code.
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 to plot your 3D simulation.
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. It is a good practice to give your variables meaningful names, so the code will become less confusing for you and other people reading it. 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
In VPython you can create an object with certain attributes (e.g. radius, position). You can store the information about that object in a variable. This way you can access a certain object using a variable it is stored in.
ball = sphere(pos=(x_coordinate,y_coordinate,z_coordinate), radius=radius_of_the_sphere, color = color.color_of_the_sphere)
myArrow = arrow(pos=(x0_coordinate,y0_coordinate,z0_coordinate), axis=(x1_coordinate,y1_coordinate,z1_coordinate), color = color.color_of_the_arrow)
myVector = vector(x_coordinate,y_coordinate,z_coordinate)
You can leave a trail behind a moving object simply by specifying make_trail=True
:
ball.make_trail = True
Each time you change ball.pos, this new position is added to a curve, thereby leaving a trail behind the moving sphere. This works with arrow, box, cone, cylinder, ellipsoid, pyramid, ring, and sphere.
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 (see Iterative Prediction as a good example of the physical application of a while loop). 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)
is not equivalent tox / y * z
) - Try commenting out some steps to see which parts of your code do not work.
For further information on debugging see VPython Common Errors and Troubleshooting.
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
dot(vector_1, vector_2)
Calculates the dot product of two vectors
The structure of VPython program
A typical VPython program has the following structure:
- Declare all the constants that will be used in the physical process you are going to model
- Create all the objects that are taking part in the process
- Start a while loop
- Calculate the forces on the given objects (or any other factors that alter the position of the given objects)
- Alter the positions (or other variables you need to alter) of the given objects to observe their trajectory
For example:
This program uses a simple algorithm that produces an approximate motion of Earth around the Sun.
from __future__ import division from visual import * r=5.0 theta=0.0 dtheta=0.01 sun=sphere(color=color.yellow) earth=sphere(pos=(r,0,0),color=color.blue) t = 0 while (t < 60): rate(20) theta=theta+dtheta x=r*cos(theta) y=r*sin(theta) earth.pos=(x,y,0) t = t + 0.1
Connectedness
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. One of the greatest strengths of GlowScript is that is does not require any pre-installed software and can be ran from any computer.
General Overview of VPython
External links
The official VPython website with the links to YouTube tutorials
References
This section contains the the references you used while writing this page
3. GlowScript
5. Code source