0% found this document useful (0 votes)
29 views4 pages

227 2022 Lab01 Introduction To Computation

Uploaded by

Nicholas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views4 pages

227 2022 Lab01 Introduction To Computation

Uploaded by

Nicholas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Matter & Interactions 4e Univ.

of Calgary PHYS 227 Lab 01 Intro to Computation

Introduction to 3D Computational Modeling in Physics


Equipment
• computer with VPython and access to internet

Objectives
Throughout your undergraduate program and your career, you will use computers to calculate. To help prepare
you, this introductory physics course (and subsequent physics courses in coming terms) includes a number of com-
putational exercises to predict and display the motion of interacting objects in 3D. You will use a programming
environment called VPython, which consists of the widely-used programming language Python plus a 3D graphics
module called “visual” which makes it possible to do vector algebra and to visualize vector quantities in 3D. VPython
(http://vpython.org) is free and runs on Windows, MacOSX, and Linux. It’s possible to run these programs in
a web-browser at a website: glowscript.org. To write and execute programs directly on your computer, you use an
editor called “VIDLE” (pronounced vee-idle) which comes with VPython.

After completing this activity you should be able to:


• Use the glowscript IDE, the web-based interactive editor for VPython
• Write a simple VPython program
• Create 3D objects such as spheres and arrows
• Use vectors in VPython

You should finish this activity in one lab period and turn your work in at the end.

1 What is a Computer Program?


You have seen short computer programs, written in VPython, in Chapter 1 of the Matter & Interactions 4e textbook.
The relevant things to understand about a computer program are:
• A computer program consists of a sequence of instructions that describe a calculation.
• The computer carries out the instructions one by one, in a predetermined order, and stops when it reaches the
end.
• Each instruction must be entered exactly correctly, as if it were an instruction to your calculator.
• If the computer encounters an error in an instruction (such as mis-typing a command), it may stop running and
print an error message.

2 Setting up to write your first program: glowscript


⇒ Go to glowscript.org and make yourself an account.
⇒ Create a new folder called 227programs and make sure that the box marked public is checked (or we won’t be
able to grade your work).
⇒ Create a new program called Lab01ex01YourLastNames (add the last names of all of the people in your group).
⇒ The # mark lets the computer program know to ignore a line. On the second line, put in a # mark and
then list what lab this is lab 01 exercise 01, and the full name of everyone in your group (that way everyone
gets a grade on the lab). You’ll do this every time you have something to turn in for a computer programming lab.

Note that the first line says ”GlowScript 2.1 VPython”, this tells Glowscript to treat your program as a visual
python program. Don’t modify this line. This line will be needed in all VPython programs in glowscript. If
running a program in VIDLE, make sure you start with the following two lines, which must go at the beginning
of every VPython program:

from future import division, print function


from visual import *

These lines are not needed in glowscript.

1
Matter & Interactions 4e Univ. of Calgary PHYS 227 Lab 01 Intro to Computation

3 Your First Program: Creating 3D Objects


⇒ Watch the first VPython instructional video VPython Instructional Videos: 1. 3D Objects
http://vpython.org/video01.html demonstrating how to easily create 3D objects in VPython. Note that this
video explains how to run a program in VIDLE rather than glowscript. Please discuss the differences with your
group and TA.
⇒ Complete the challenge task presented at the end of the video. Use glowscript to do so.
Show your working program to the TA. You will e-mail the link to the TA so that your group can get credit for
lab 01 exercise 01.
Show your work to your TA before continuing.

3.1 Zooming and Rotating


By default the origin h0, 0, 0i is at the center of the scene, and the “camera” (that is, your point of view) is looking
directly at the origin. When you first run the program, the coordinate system has the +x direction to the right, the
+y direction pointing up, and the +z direction coming out of the screen toward you. This is the same convention
used in the Matter & Interactions 4e textbook.
⇒ On a two-button mouse, hold down both mouse buttons and move the mouse up and down to make the camera
move closer or farther away from the center of the scene. (On a one-button mouse, hold down the ALT key and
the mouse button.)
⇒ On a two-button mouse, hold down the right mouse button alone and move the mouse to make the camera
“revolve” around the scene, while always looking at the center. (On a one-button mouse, hold down CTRL and
the mouse button.)

4 Finding and Fixing Errors


Everyone makes errors when programming–you may already have made one or more errors in the process of creating
your first program. During the course of your work with VPython you will need to find and fix two different kinds
of errors:
• Syntax Errors (typing the computre code improperly)
• Math, Physics, or Logic Errors

Both kinds of errors are similar to errors you may make when working pencil and paper problems using a calculator.
Errors made entering expressions into your calculator (pressing the 8 key as opposed to the 5 key) are syntax errors.
Using an inappropriate or incorrect equation, or trying to divide a scalar by a vector, is an error in math, physics or
logic. These errors can give meaningless answers, but the computer won’t warn you!

4.1 Syntax Errors


Common syntax errors include
• Typos and spelling errors
• Missing commas, colons, equal signs, etc.
• Unmatched parentheses

⇒ Create a new VPython program as lab01ex02YourLastNames (lab 01, exercise 02, and change the commented
line to reflect that as well, but keep your names in there!) This new program is to make sure that you won’t
change the program that you are submitting.
⇒ Watch the VPython instructional video VPython Instructional Videos: A. Debugging Syntax Errors
http://vpython.org/videoA.html which illustrates some common errors.
⇒ Try modifying your program (lab01ex02 not the one that you’ve submitted for a grade) to introduce some of the
common errors shown in the video.

2
Matter & Interactions 4e Univ. of Calgary PHYS 227 Lab 01 Intro to Computation

5 Other Things to Notice


5.1 Autoscaling and Units
⇒ Change the position of one of the objects in your program so it is three times farther from the origin (for example,
if a sphere’s position is h6, 0, 0i, move it to h18, 0, 0i). Can you still see all the spheres when you run the program?

VPython automatically “zooms” the camera in or out so that all objects appear in the window. This behavior is
called “autoscaling.” Usually this is helpful, but occasionally we may want to turn off autoscaling.

⇒ What units do you think VPython is using?

Since VPython automatically moves the camera to try to keep all objects in the display window, it can handle any
consistent set of units. We will always use SI units in our physics programs.

5.2 Comments
Comment lines start with a # (pound sign). A comment line can be a note to yourself, such as:

# objects created in the following lines

Or a comment can be used to remove a line of code temporarily, without erasing it.
You can also put a comment at the end of a line: sphere() # it’s round.

⇒ Comment out all but one arrow in your program. For the remaining arrow:
⇒ Change something in the arrow’s code such that the arrow is half as long and points in the opposite direction,
with its tail remaining on the same sphere.

6 Naming Objects and Variables


⇒ Watch the second VPython instructional video VPython Instructional Videos: 2. Variable Assignment
http://vpython.org/video02.html demonstrating how to name objects and varibles so you can refer to them
later in the program.
⇒ Complete the challenge task presented at the end of the video in Lab01ex02YourLastNames you will eventually
submit this program to your TA.

Show your work to your TA before continuing.

7 Print Command
⇒ Start a new line at the end of your program and type:1
print(object.attribute)
⇒ Replace object.attribute with the name of one of your 3D objects and one of the valid attributes associated with
that object. For example, if you want to print the position attribute of a sphere named ball, it would look like
this:
print(ball.pos)
⇒ Run the program, and look below the window. You should see the value printed there.
⇒ You can also print text in VPython.
print("Here is the position of ball:", ball.pos)
⇒ Have your program print the position and radius of the ball (color does something counterintuitive that does
make sense). Supply text to state what each of these attributes are.
1 Another example of change in Python is that before Python 3.0, you could say print ball.pos, but starting with Python 3.0 one

must say print(ball.pos). If you are using an earlier version of Python, it is a good idea to use parentheses anyway, because it doesn’t
hurt, and it works with later versions of Python.

3
Matter & Interactions 4e Univ. of Calgary PHYS 227 Lab 01 Intro to Computation

⇒ Save the challenge task you just completed as Lab01ex02YourLastNames to submit to your TA.
⇒ Optional: If you have the time, come up with a pretty pattern in your VPython program and show it off your
VPython skills. There are cylinders, boxes, helices, rings, pyramids and many more! Explore different colors
and positions then save this as Lab01ex03YourLastNames. If you have more than one that you like save them as
Lab01ex03A, Lab01ex03B, etc.

Show your work to your TA before continuing.

E-mail your TA: Lab01ex01 and Lab01ex02 and if you did them any optional programs.

You must e-mail your programs to the TAs before you leave the lab. Make sure that each program has the names of
every in the group and has a name corresponding to which lab and exercise it is. Also make sure that every member
of your group has access to the code.

8 Optional : Using VPython outside of class


8.1 Downloading and Installing VPython
While you can use glowscript on your computer or even your phone, the computations will be a little slower than they
would be in locally run VPython. For convenience these labs will all use glowscript for easy sharing and consistent
compiling. If you’d like to explore more, you can download Python and VPython from http://vpython.org and install
them on your own computer. Follow the installation instructions carefully. VPython is free and open source, and
runs on Windows, MacOSX, and Linux.

8.2 Reference manual and programming help


There is an on-line reference manual for VPython. In the text editor (VIDLE), on the Help menu, choose “VPython.”
If you are interested you can also choose “Python Docs” to obtain detailed information on the Python programming
language upon which VPython is based. We will use only a small subset of Python’s extensive capabilities; you can
learn what you need from the Matter & Interactions 4e textbook and the VPython videos.
When writing a program that compiles directly on your computer, you would use the VIDLE editor (as is done in
the video). This is not required in this lab.
Make sure you start with the following two lines, which must go at the beginning of every VPython program:

from future import division, print function


from visual import *

The first line is needed because there are different versions of Python, which is continuously improved and upgraded2 .
Note that there are two underscores immediately before and after the word “future.”
The second line tells the program to use the 3D module (called “visual”). The asterisk means, “Add to Python all
of the features available in the visual module”.

8.3 The Python Shell Window


Red error messages appear in the Python Shell window. Since it’s important not to miss error messages, arrange
windows on your screen so the Shell window is always visible, even if it is small. To stop your program from running,
close the graphic display window – don’t close the Shell window.

An error message in the Shell window typically consists of four lines of red text. The information you need is in the
bottom line – read the bottom line first.

2022-07-14
2 The statement (from space underscore underscore future underscore underscore space division, print function) tells the Python

language to treat 1/2 as 0.5, and makes the new form of print statements work on older versions of Python. You don’t need this statement
if you are using Python 3.0 or later, but it doesn’t hurt, because it is simply ignored by later versions of Python.

You might also like