VPython basics

From Physics Book
Jump to navigation Jump to search

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.

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 Windows, Machintosh and Linux.

A Computational Model

How do we visualize or predict using this topic. Consider embedding some vpython code here Teach hands-on with GlowScript

Basics

Befor writing your VPython program be sure to include those lines

from __future__ import division

from visual import *

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.

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)

Arrow: someArrow = arrow(pos=(x1,y1,z1), axis=(x2,y2,z2), color = color.your_color)

Middling

Difficult

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.

  1. How is this topic connected to something that you are interested in?
  2. How is it connected to your major?
  3. Is there an interesting industrial application?

History

Put this idea in historical context. Give the reader the Who, What, When, Where, and Why.

See also

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

Further reading

Books, Articles or other print media on this topic

External links

Internet resources on this topic

References

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