VPython basics: Difference between revisions

From Physics Book
Jump to navigation Jump to search
No edit summary
Line 1: Line 1:
claimed by Liubov Nikolenko
Claimed by Liubov Nikolenko




This is a beginner guide to VPython for people who have no or little coding experience and are concerned about the coding part of the course.
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.


==The Main Idea==


State, in your own words, the main idea for this topic
VPython is a programming language with graphics module that is useful for simulation and animation of physical processes. In other words, it allows to see what would happen to the motion of the Earth if it crashed with the giant asteroid just by writing several lines of code insted of creating a doomsday!


===Installation===
VPython is available for [http://vpython.org/contents/download_windows.html Windows], [http://vpython.org/contents/download_mac.html Machintosh] and [http://vpython.org/contents/download_linux.html Linux].


===A Computational Model===


How do we visualize or predict using this topic. Consider embedding some vpython code here [https://trinket.io/glowscript/31d0f9ad9e Teach hands-on with GlowScript]
[[File:EMWave-Maxwell v275.gif]]
==Installation==


==Basics==
To install VPython simply follow the instructions in the links given below.
Befor writing your VPython program be sure to include those lines


from __future__ import division
===Windows===
Install VPython [http://vpython.org/contents/download_windows.html here]


from visual import *
===OSX===


These lines are needed to invoke the visual module and to make sure that when you calculate 3/4 you get 0.75 and not 0.
Install VPython [http://vpython.org/contents/download_mac.html here]
===Creating vectors===
You can create a vector v in Vpython by doing v = vector(x, y, z)
===Creating objects===
You will be creating several 3D shapes to represent objects and particles in your simulations. You will usually name your objects in order to access or modify their information (e.g. update the position). You can access the attributes of the object you created just by doing object.attribute (e.g. electron.pos).  Here is the code sample for the most common ones:


Sphere: ball = sphere(pos=(x,y,z,), radius=r, color = color.your_color)
===Linux===


Arrow: someArrow = arrow(pos=(x1,y1,z1), axis=(x2,y2,z2), color = color.your_color)
Install VPython [http://vpython.org/contents/download_linux.html here]
===Middling===
 
===Difficult===
==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: [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
 
===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==
==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.  
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.  
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.  
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.  
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?
#How is it connected to your major?
#Is there an interesting industrial application?


==History==
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
 


Put this idea in historical context. Give the reader the Who, What, When, Where, and Why.
How is it connected to your major?


== See also ==
I am CS major and Python was the first language I have learned at Tech.


Are there related topics or categories in this wiki resource for the curious reader to explore?  How does this topic fit into that context?
Is there an interesting industrial application?


===Further reading===
Every simulation starting from interaction of molecules can be modeled in VPython to get the general idea of the process.


Books, Articles or other print media on this topic
==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===
[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.
===External links===
===External links===
[http://vpython.org/contents/doc.html Documentation]


Internet resources on this topic
[https://groups.google.com/forum/?fromgroups&hl=en#!forum/vpython-users VPython User forum]
 
==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]
2. [http://vpython.wikidot.com/ VPython Wiki Site]
3. [http://www.glowscript.org/ GlowScript]
4. [http://www.visualrelativity.com/vpython/ The image source]


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

Revision as of 17:08, 2 December 2015

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

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.

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