VPython: Difference between revisions

From Physics Book
Jump to navigation Jump to search
m (Fix code description)
Line 46: Line 46:
from visual import *</code>
from visual import *</code>


This first line tells Python to not use integer division, which would result in <code>1/3</code> returning the floor <code>0</code> instead of <code>0.3333333333333333</code>.  The second line tells Python to import everything from the visual (VPython) library for you to use.
The first line tells Python to not use integer division, which would result in <code>1/3</code> returning the floor <code>0</code> instead of <code>0.3333333333333333</code>.  The second line tells Python to import everything from the visual (VPython) library for you to use.


After these two lines, you can create objects, assign variables, do calculations, and much more.  You can also run VPython from the command line, although it may require additional setup.
After these two lines, you can create objects, assign variables, do calculations, and much more.  You can also run VPython from the command line, although it may require additional setup so Python can locate the <code>visual</code> module.


==Creating VPython Objects==
==Creating VPython Objects==

Revision as of 14:52, 3 December 2015

Claimed by Nathan Cheek

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.

Error creating thumbnail: sh: /usr/bin/convert: No such file or directory Error code: 127
VPython modeling the magnetic force on a moving particle

Installation

VPython is compatible with Windows, OSX, and Linux. While there is an older VPython version available that supports Python 3, the Georgia Tech physics courses use Python 2 syntax so it is recommended to install the latest version of VPython as outlined below.

Windows

Error creating thumbnail: sh: /usr/bin/convert: No such file or directory Error code: 127
VPython running in Windows 7

1. Install Python-2.7.

2. Download and install VPython from Sourceforge.


To troubleshoot any installation issues, look at the extensive VPython Windows installation guide.

OSX

Error creating thumbnail: sh: /usr/bin/convert: No such file or directory Error code: 127
VPython running in OSX 10.10

1. Install Python-2.7. This is required as the version of Python that Apple provides is not compatible.

2. Download and install the VPython package from Sourceforge.


To troubleshoot installation issues, see the extensive VPython Mac installation guide.

GNU/Linux

Error creating thumbnail: sh: /usr/bin/convert: No such file or directory Error code: 127
VPython running in Ubuntu 15.04

Some Linux distributions include VPython in their repositories. For example, to install VPython in Ubuntu 15.04:

sudo apt-get install python-visual


If your Linux distribution's repository does not have the latest version of VPython, you can run the Windows version using Wine. The VPython Linux installation guide contains detailed instructions.

Getting started with VPython

The easiest way to use VPython is with VIDLE. VIDLE is a development environment and is the most useful for editing and running code. To start, open the VIDLE application (it may be called something like VIDLE-Py2.7). A blank file should open. Type the following two lines in:

from __future__ import division

from visual import *

The first line tells Python to not use integer division, which would result in 1/3 returning the floor 0 instead of 0.3333333333333333. The second line tells Python to import everything from the visual (VPython) library for you to use.

After these two lines, you can create objects, assign variables, do calculations, and much more. You can also run VPython from the command line, although it may require additional setup so Python can locate the visual module.

Creating VPython Objects

When you create objects, it is often useful to assign them to variables so you can reference them later to either get or set values describing for example the position and size of the object.

Sphere

particle = sphere(pos=(0,0,0), radius=8, color=color.blue)

This creates a blue sphere at the origin <0,0,0> with radius 8.

Arrow

arrowToOrigin = arrow(pos=(5,0,0), axis=(-5,0,0), color=color.green)

This creates a green arrow pointing from <5,0,0> to the origin <0,0,0>.

Cylinder

wire = cylinder(pos=(0,0,0), axis=(100,0,0), radius=1, color=color.yellow)

This creates a yellow cylinder starting at the origin <0,0,0> and extending to <100,0,0> with radius 1.

Updating objects

If you assign an object to a variable (such as particle or arrowToOrigin above) you can adjust its parameters such as its location. For the above sphere, you can change its location to <1,2,3> with:

particle.pos=(1,2,3)

To change the axis of the above arrow to point to <5,0,10>, use:

arrowToOrigin.axis=(0,0,10)

Useful built-in functions

VPython includes various functions that can make your calculations much easier. The following functions simplify mathematical operations on vectors and will come in handy very often.

Cross-multiply two vectors

cross(vectorA, vectorB)

Magnitude of a vector

mag(vector)

Unit vector of a vector

norm(vector)

Connectedness

  1. How is this topic connected to something that you are interested in?

I am very interested in programming. I have used Python for years, so translating Physics problems into VPython code is a great way to cement the fundamental ideas in my mind.

  1. How is it connected to your major?

VPython helps to bridge the worlds of Computer Science and Physics.

  1. Is there an interesting industrial application?

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. Beginning in 2002, the National Science Foundation had awarded $292,286.00 as of December 2015 for the further development of this tool. VPython is released under an Open Source license, and development continues today.

See also

If you have never used Python, the VPython Basics wiki page has information for getting started with Python programming.

Further reading

Books, Articles or other print media on this topic

External links

You Should Be Coding in Your Physics Course

VPython Wiki Site

References

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

The cT Programming Language

VPython - Python Wiki

VPython NSF Award