VPython: Difference between revisions

From Physics Book
Jump to navigation Jump to search
No edit summary
Tag: Manual revert
 
(50 intermediate revisions by 8 users not shown)
Line 1: Line 1:
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.
 
[[File:VPythonMagneticForceAnimated.gif|thumb|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===
[[File:VPythonWindows.png|thumb|VPython running in Windows 7]]
 
1. Install [http://python.org/ftp/python/2.7.9/python-2.7.9.amd64.msi Python-2.7].
 
2. Download and install VPython from [http://sourceforge.net/projects/vpythonwx/files/6.11-release/VPython-Win-64-Py2.7-6.11.exe/download Sourceforge].
 
 
To troubleshoot any installation issues, look at the extensive [http://vpython.org/contents/download_windows.html VPython Windows installation guide].
 
===OSX===
[[File:VPythonOSX.png|thumb|VPython running in OSX 10.10]]
 
1. Install [https://www.python.org/ftp/python/2.7.9/python-2.7.9-macosx10.6.pkg 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 [http://sourceforge.net/projects/vpythonwx/files/6.11-release/VPython-Mac-Py2.7-6.11.dmg/download Sourceforge].
 
 
To troubleshoot installation issues, see the extensive [http://vpython.org/contents/download_mac.html VPython Mac installation guide].
 
===GNU/Linux===
[[File:VPythonUbuntu.png|thumb|VPython running in Ubuntu 15.04+]]


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


VPython is a Python graphics module used for modeling objects in 3-dimensional spaceIn the field of physics, this is especially useful for calculating and modeling complex relationships between objects and their properties.
<code>sudo apt-get install python-visual</code>
 
If your Linux distribution's repository does not have the latest version of VPython, you can run the Windows version using [https://www.winehq.org/ Wine].  The [http://vpython.org/contents/download_linux.html 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:
 
<code>from __future__ import division
 
from visual import *</code>
 
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 so Python can locate the <code>visual</code> module.
 
===Glowscript===
 
Another useful method of using Vpython can be found at http://www.glowscript.org/. At this website, a user can make an account and have a script up and running within minutes on a web browser without having to deal with downloading any software or worrying about compatibility. Only downside is that it can be tricky to save the code for submitting to Webassign.
 
==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'''
 
<code>particle = sphere(pos=(0,0,0), radius=4, color=color.blue)</code>
 
This creates a blue sphere centered at the origin <0,0,0> with radius 4.
 
'''Arrow'''
 
<code>arrowToOrigin = arrow(pos=(0,2,0), axis=(0,8,0), color=color.green)</code>
 
This creates a green arrow pointing from <0,2,0> to <0,10,0>Note that <code>axis</code> is always relative to the start of the arrow, not the origin <0,0,0>.
 
'''Cylinder'''
 
<code>wire = cylinder(pos=(0,0,0), axis=(20,0,0), radius=1, color=color.yellow)</code>
 
This creates a yellow cylinder starting at the origin <0,0,0> and extending to <20,0,0> with radius 1.
 
'''Helix'''
 
<code>spring = helix(pos=(0,0,0), axis=(10,0,0), radius=0.75, color=color.cyan)</code>
 
Creates a helix of color cyan at the origin and goes to <10,0,0> with a radius of 0.75.
 
'''Pyramid'''
 
<code>shape = pyramid(pos=(8,4,0), size=(20,4,10), color=color.red)</code>
 
Creates a pyramid of size <20,4,10> starting at <8,4,0>
 
'''Object Positioning'''
 
It is very important to note that the position values for arrow, cylinder, helix, and pyramid corresponds to one end of the object, however for a box, sphere, or ring it corresponds to the center of the object.
 
'''Example'''
[[File:VPythonObjects.png|thumb|Result of example code]]
<nowiki>
from __future__ import division
from visual import *
 
particle = sphere(pos=(0,0,0), radius=4, color=color.blue)
arrowToOrigin = arrow(pos=(0,2,0), axis=(0,8,0), color=color.green)
wire = cylinder(pos=(0,0,0), axis=(20,0,0), radius=1, color=color.yellow)</nowiki>
 
'''Updating objects'''
If you assign an object to a variable (such as <code>particle</code> or <code>arrowToOrigin</code> above) you can adjust its parameters such as its location.  For the above sphere, you can change its location to <-5,-5,0> with:
 
<code>particle.pos=(-5,-5,0)</code>
 
To change the axis of the above arrow to point to <-10,5,0>, use:
 
<code>arrowToOrigin.axis=(-10,5,0)</code>
 
To change the radius of the above cylinder to 3, use:
 
<code>wire.radius=3</code>


'''Example'''
[[File:VPythonObjectsChanged.png|thumb|Result of example code]]
<nowiki>
from __future__ import division
from visual import *


particle = sphere(pos=(0,0,0), radius=4, color=color.blue)
arrowToOrigin = arrow(pos=(0,2,0), axis=(0,8,0), color=color.green)
wire = cylinder(pos=(0,0,0), axis=(20,0,0), radius=1, color=color.yellow)


particle.pos=(-5,-5,0)
arrowToOrigin.axis=(-10,5,0)
wire.radius=3</nowiki>


==Useful built-in functions==


[[File:EMWave-Maxwell v275.gif]]
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.
==Installation==


To install VPython simply follow the instructions in the links given below.
'''Cross-multiply two vectors'''


===Windows===
<code>cross(vectorA, vectorB)</code>
Install VPython [http://vpython.org/contents/download_windows.html here]


===OSX===
'''Magnitude of a vector'''


Install VPython [http://vpython.org/contents/download_mac.html here]
<code>mag(vector)</code>


===Linux===
'''Square of the Magnitude of a vector'''


Install VPython [http://vpython.org/contents/download_linux.html here]
<code>mag2(vector)</code>


==Getting started with Python==
'''Unit vector of a vector'''
Introduction to basic Python use


The first two lines of your program should be:
<code>norm(vector)</code>
::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: [https://wiki.python.org/moin/ForLoop for loop] and [https://wiki.python.org/moin/WhileLoop 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
'''Square Root'''


===Comments===
<code>sqrt(value)</code>
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
'''Absolute Value'''
===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===
<code>abs(value)</code>
====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 Operators==


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.
===Mathematical Operators===
1) #  comment out a line


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.
2) + - * /  addition, subtraction, multiplication, division


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.
3) ** exponential


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.
4) % modulus or remainder


===Logical Operators===
1) ==  outputs true if the compared values are the same


How is this topic connected to something that you are interested in?
2) != outputs true if the compared values are not the same


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
3) > or < outputs true if the compared values are greater or less than the other


4) >= or <= Same as above but outputs true if greater or less than or equal to the compared value


How is it connected to your major?
==Connectedness==


I am CS major and Python was the first language I have learned at Tech.
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.


   
VPython is a great tool to bridge the worlds of Computer Science and Physics. Many of the calculations required to model a physical object would be tedious if done by hand.  Yet with often a few lines of code, this work can be reduced to almost nothing.
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.
NASA has published [http://gcmd.gsfc.nasa.gov/KeywordSearch/Metadata.do?Portal=GCMD&MetadataType=1&MetadataView=Full&KeywordPath=&EntryId=3-D_ES-Models VPython models].  However, VPython is still primarily used in an educational context.


==History==
==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.
 
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 ==
== See also ==
===GlowScript===
 
[http://www.glowscript.org/ 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.  
If you have never used Python, the [[VPython_basics| VPython Basics]] wiki page has information for getting started with Python programming.
 
===Further reading===
 
Aiken, John M (2013). ''Transforming High School Physics With Modeling And Computation''. Georgia State University.
 
===External links===
===External links===
[http://vpython.org/contents/doc.html Documentation]


[https://groups.google.com/forum/?fromgroups&hl=en#!forum/vpython-users VPython User forum]
[http://www.wired.com/2015/08/coding-physics-course/ You Should Be Coding in Your Physics Course]
 
[http://vpython.wikidot.com/ VPython Wiki Site]
 
[https://groups.google.com/forum/#!forum/vpython-users VPython user group]
 
==References==
==References==


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


1. [http://vpython.org/contents/cTsource/cToverview.html The cT Programming Language]
[http://vpython.org/contents/cTsource/cToverview.html The cT Programming Language]
 
2. [http://vpython.wikidot.com/ VPython Wiki Site]


3. [http://www.glowscript.org/ GlowScript]
[https://wiki.python.org/moin/VPython/ VPython - Python Wiki]


4. [http://www.visualrelativity.com/vpython/ The image source]
[http://www.nsf.gov/awardsearch/showAward?AWD_ID=0237132 VPython NSF Award]


[[Category:Which Category did you place this in?]]
[[Category:VPython]]

Latest revision as of 21:49, 24 April 2022

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.

Glowscript

Another useful method of using Vpython can be found at http://www.glowscript.org/. At this website, a user can make an account and have a script up and running within minutes on a web browser without having to deal with downloading any software or worrying about compatibility. Only downside is that it can be tricky to save the code for submitting to Webassign.

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=4, color=color.blue)

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

Arrow

arrowToOrigin = arrow(pos=(0,2,0), axis=(0,8,0), color=color.green)

This creates a green arrow pointing from <0,2,0> to <0,10,0>. Note that axis is always relative to the start of the arrow, not the origin <0,0,0>.

Cylinder

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

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

Helix

spring = helix(pos=(0,0,0), axis=(10,0,0), radius=0.75, color=color.cyan)

Creates a helix of color cyan at the origin and goes to <10,0,0> with a radius of 0.75.

Pyramid

shape = pyramid(pos=(8,4,0), size=(20,4,10), color=color.red)

Creates a pyramid of size <20,4,10> starting at <8,4,0>

Object Positioning

It is very important to note that the position values for arrow, cylinder, helix, and pyramid corresponds to one end of the object, however for a box, sphere, or ring it corresponds to the center of the object.

Example

Error creating thumbnail: sh: /usr/bin/convert: No such file or directory Error code: 127
Result of example code
from __future__ import division
from visual import *

particle = sphere(pos=(0,0,0), radius=4, color=color.blue)
arrowToOrigin = arrow(pos=(0,2,0), axis=(0,8,0), color=color.green)
wire = cylinder(pos=(0,0,0), axis=(20,0,0), radius=1, color=color.yellow)

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 <-5,-5,0> with:

particle.pos=(-5,-5,0)

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

arrowToOrigin.axis=(-10,5,0)

To change the radius of the above cylinder to 3, use:

wire.radius=3

Example

Error creating thumbnail: sh: /usr/bin/convert: No such file or directory Error code: 127
Result of example code
from __future__ import division
from visual import *

particle = sphere(pos=(0,0,0), radius=4, color=color.blue)
arrowToOrigin = arrow(pos=(0,2,0), axis=(0,8,0), color=color.green)
wire = cylinder(pos=(0,0,0), axis=(20,0,0), radius=1, color=color.yellow)

particle.pos=(-5,-5,0)
arrowToOrigin.axis=(-10,5,0)
wire.radius=3

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)

Square of the Magnitude of a vector

mag2(vector)

Unit vector of a vector

norm(vector)

Square Root

sqrt(value)

Absolute Value

abs(value)

VPython Operators

Mathematical Operators

1) # comment out a line

2) + - * / addition, subtraction, multiplication, division

3) ** exponential

4) % modulus or remainder

Logical Operators

1) == outputs true if the compared values are the same

2) != outputs true if the compared values are not the same

3) > or < outputs true if the compared values are greater or less than the other

4) >= or <= Same as above but outputs true if greater or less than or equal to the compared value

Connectedness

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.

VPython is a great tool to bridge the worlds of Computer Science and Physics. Many of the calculations required to model a physical object would be tedious if done by hand. Yet with often a few lines of code, this work can be reduced to almost nothing.

NASA has published VPython models. However, VPython is still primarily used in an educational context.

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

Aiken, John M (2013). Transforming High School Physics With Modeling And Computation. Georgia State University.

External links

You Should Be Coding in Your Physics Course

VPython Wiki Site

VPython user group

References

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

The cT Programming Language

VPython - Python Wiki

VPython NSF Award