Thursday, April 23, 2015

VPython, Anaconda, and iVisual

Since 2003, we have taught physics students in our introductory, calculus-based physics course to use VPython to model physical phenomena and to solve problems numerically (that cannot be solved analytically at the introductory level). It is the single best change that we made to our introductory physics course to make it creative, relevant, interesting, and contemporary. Besides teaching students the utility of numerical modeling, one of our goals is to teach them to solve problems by applying fundamental principles, such as the Momentum Principle (Newton's second law). Applying the Momentum Principle in small, successive time intervals with VPython is both illustrative and productive.

VPython is ideal for teaching numerical modeling in introductory physics because its language (such as adding and subtracting vector objects) maps wonderfully onto the mathematical and verbal description we use in physics. VPython in general refers to two necessary parts:  (1) the Python language with (2) the Visual package.  VPython has evolved since we first began using it in 2003. In 2003, you had to install Python and Visual. The Visual package did not work well with other python packages so we rarely installed additional packages. An expert could get Visual and other packages like Matplotlib to work together, but it was not easy.

There are now at least five methods you can use to install and run VPython or code similar to VPython code. Because we can now run VPython code (or code similar to it) in an iPython Notebook or in JavaScript, we sometimes refer to "VPython" as the set of commands used to create 3D objects, refer to the properties of those objects, and add and subtract vectors.

Here are the five methods to install VPython or something similar to VPython.

  1. Install Python and Visual using the installers and instructions at www.vpython.org.
  2. Write programs at www.glowscript.org. GlowScript is a JavaScript library that has a similar syntax as Visual and allows you to write code similar to VPython.
  3. Install Anaconda and Visual.
  4. Install Anaconda, iPython Notebook, and iVisual and run VPython code within a notebook in a browser.
  5. Write GlowScript code in a trinket.



My Recommendation

If you are teaching VPython to students in introductory physics, whether in high school or in college, I recommend Method 1 (vpython.org) or Method 2 (GlowScript). The installation is simple, and introductory physics students can pick up the process of: (1) write, (2) run, (3) edit, and (4) re-run with very little effort. Students with zero background in programming can learn how to do this very quickly.

But if your students are advanced or if they have experience with VPython and wish to combine Python with other packages such as Matplotlib or Sympy, I recommend Method 3 (Anaconda and Visual) and Method 4 (iPython Notebook and iVisual). Anaconda and iPython Notebook are frequently used by professional scientists. Experience with Anaconda, iPython Notebook, and other Python packages will help prepare students for future jobs in industry or research, for example.

If you plan to embed VPython code into a blog or other web site, I recommend using Trinket.


VPython


This installation procedure given at www.vpython.org is very simple and has not changed since before 2003, although Python and Visual have been upgraded. You can choose to install the version of Visual for Python 2.7 or the version for Python 3. We prefer Python 2.7 because other packages like Serial that you might use with Visual have not been upgraded to Python 3. Many professionals in the Python world still use version 2.7. It is still widely supported with many packages.

This is EASY. There are two installers. First, install Python. Second, install Visual. It's that simple. Thousands of students have installed VPython with no trouble. It is simple, and it works reliably.

Download sample programs from Matter and Interactions.


GlowScript

Go to the GlowScript site, view sample programs, and create an account.

Original VPython programs written for the Matter and Interactions textbook can be run in a browser using GlowScript.

There are some introductory GlowScript tutorials by Rhett Allain. Tutorials 1-7 will get you started.


Installing Anaconda, Visual, and iPython Notebook with iVisual


I. Anaconda and Visual

You can install the Visual package with Anaconda and run VPython programs from the command line.  The Anaconda recipe to install visual was created by Matt Craig. Here are the steps to install and run a VPython program using anaconda.

  1. Install Anaconda. (http://continuum.io/downloads)
  2. From a command line, type:        conda install -c mwcraig vpython
  3. To run a program called "filename.py", from a command line type:   pythonw filename.py
Anaconda has many other scientific useful packages pre-installed, such as Matplotlib for graphing data. THIS is the number one advantage of using Anaconda to install Visual.


II. Anaconda, iPython Notebook, and iVisual

If you installed Anaconda, then it already has iPython and Notebook pre-installed. The only additional package you need for running VPython programs in a notebook is iVisual. Here are the steps to install iVisual (more detailed instructions with screen captures are available):
  1. From a command line, type:  pip install ivisual  
  2. From a command line, type: ipython notebook
  3. The notebook home screen will open in your browser. Create a new notebook. 
  4. Type the following program into a cell.

from __future__ import division, print_function
from ivisual import *
#create scene
scene = canvas(title='3D scene')

sphere()
My Classical Mechanics class used iPython Notebook and iVisual for an entire semester, and it worked wonderfully. Here's a presentation I gave to Guilford College with a notebook.

III. Using Anaconda's Virtual Environments

As you install more and more packages in Python, there is the chance that an installation fails and wrecks your Anaconda library, requiring you to install Anaconda and all of your favorite packages all over again. Therefore, if you use Python for different projects that use different packages, I highly recommend creating virtual environments for each project. A virtual environment is a sandbox where you only have the toys that you want to play with in that particular sandbox.

For example, I starting playing with VPython and Arduinos to create 3D games that can be controlled with sensors and an Arduino. In case I messed something up, I created a virtual environment called pyarduino.  So when I'm working on a project with VPython and an Arduino, I go into that environment and install just the packages that I need.

Here are most of the commands that I use to create and use virtual environments:

1. To create a new environment named myenv for example (with a barebones python installation with few packages) , type: 
conda create -n myenv python

2. To activate the environment myenv so that installing new packages or running a python program only occurs within myenv, type:  
 source activate myenv

3. To get out of the environment myenv type:   
source deactivate.

4. Occasionally I forget what environments I created. To list the environments, use:
conda info -e

Using Trinket to Write and Run GlowScript

Trinket is a web-based programming environment designed for teaching and learning. A "trinket" is program that can be viewed, edited, and run within the browser. You can use Trinket to give example code to students. Then they can modify the code, run it, and view the result of their changes.

You can also embed a trinket in a blog or other web site. For example, here is the familiar bouncing ball from the glowscript.org site embedded as a trinket.



As an aside, you can also use Trinket to create entire course web sites. For more information about Trinket, see my blog post or my sample PHY 3110 Classical Mechanics course.


Summary

To summarize, use vpython.org or GlowScript with introductory physics students and/or new programmers. For advanced students doing research or gaining experience toward a career in science, use Anaconda with Visual or use Anaconda with iVisual and iPython Notebook. If you wish to develop instructional resources or teach programming, then use Trinket.

8 comments:

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. I'm sorry for not replying sooner. I trust that you figured it out.

      Delete
  2. it's a shame that using your method "conda install -c mwcraig vpython" to install vpython on top of anaconda breaks spyder and god knows what else. luck i created a restore point, being the mistrusting mensch i is

    ReplyDelete
    Replies
    1. I don't use spyder, and I don't know if the author of the installer uses spyder. You probably should report it to him. I always install vpython classic in a conda environment. I suggest installing it in an environment so it does not affect other programs that you rely on.

      Delete
  3. Very Impressive Python tutorial. The content seems to be pretty exhaustive and excellent and will definitely help in learning Python course. I'm also a learner taken up Python training and I think your content has cleared some concepts of mine. While browsing for Python tutorials on YouTube i found this fantastic video on Python . Do check it out if you are interested to know more.https://www.youtube.com/watch?v=qgOXopu4n7c

    ReplyDelete
  4. [The future in 2019] Trending Technologies to learn in 2019: https://www.youtube.com/watch?v=-y5Z2fmnp-o

    ReplyDelete
  5. If you read their book review then you might know how good they are reviewing work. Here http://www.articlerewriteservice.net/online-article-rewriting-service-you-can-trust/ you will get our paragraph writing services on demand.

    ReplyDelete
  6. if the image or clasp craftsmanship that you choose to utilize end up being under another person's copyright, at that point be prepared to bring your logo down or face the results as indicated by the law. Then again, in the event that the image or clipart is as of now generally being used, at that point your logo won't be sufficiently extraordinary to catch the consideration of the purchaser. logo design service

    ReplyDelete