VPython

From Physics Book
Jump to navigation Jump to search

Claimed by Liubov Nikolenko


VPython is a Python graphics module used for modeling objects in 3-dimensional space. In the field of physics, this is especially useful for calculating and modeling complex relationships between objects and their properties.



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.

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 object

To access the attribute of a given object just use the syntax object.attribute (e.g. to access the position of the ball object, you should do ball.pos)

  • Updating values

To update a value (such as time, speed, force or the position of the object) you should do value = value + delta_value

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.

  1. 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.
  2. Make sure you updating your condition for a while loop, so you don't have an infinite loop.
  3. Check if your are dividing by zero anywhere.
  4. Make sure you have correct indentation everywhere.
  5. Put plenty of print() statements, so you can know what is going on in every single stage of your code.
  6. Check your mathematical expressions for the order of precedence (e.g. x / y*z ! = x / (y * z))
  7. 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

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.

History

VPython was originally released in 2000 by David Scherer after he took an introductory physics class at Carnegie Mellon University. At the time, the school used the cT programming language for 2D modeling, and David saw the need for something better. Working with several people including professors Ruth Chabay and Bruce Sherwood, he developed a Python module called Visual. Visual Python or VPython featured 3D modeling, as well as an easier-to-understand object-oriented syntax. VPython is released under an Open Source license, and development continues today.

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

Documentation

VPython User forum

References

This section contains the the references you used while writing this page

1. The cT Programming Language

2. VPython Wiki Site

3. GlowScript

4. The image source