Lab 2: Introduction to simulations

Goals this week

By the end of this lab you should be able to:

  1. Rename the notebook and edit the text in a notebook.
  2. Run your first simulation of a physical object using ivisual.

Rename the notebook, and an editing reminder

Rename this notebook so that it includes your name. Quick refresher (last one on this topic!): Click the name of the lab at the top to rename it.

How to edit text or computer code: double click on it to edit, push Shift-Enter when you are done.

Your first simulation

Your simulations in this class are a tool to model the physical world. You will do that using a package called "ivisual".

The code in the cell below will create an animation of a box. Until you make modifications later in this lab it will be a pretty boring animation...nothing will move!

However, you can interact with the animation:

  • Right-click and drag to rotate around the square
  • Scroll to zoom in and out

Execute the code cell below and trying rotation and zooming.


In [ ]:
from ivisual import *

In [ ]:
scene = canvas()
scene.autoscale = False

initial_box_pos = vector(0, 0, 0) # meters
my_box = box(size=(1, 2, 3))
my_box.pos = initial_box_pos

finished = False

velocity = vector(0, 0, 0)  # m/s
dt = 0.1  # sec

steps = 0

while not finished:
    rate(30)  # no more than 50 frames per second
    
    my_box.pos = my_box.pos + velocity * dt
    
    steps = steps + 1

    if my_box.pos.x > 10 or steps > 100:
        finished = True

For you to do

  • Modify the code cell above so that the box moves to the right at a velocity of 0.25 m/s (remember to re-run the code after modifying it).
  • Answer all of the questions below; put your answers in the notebook itself (remember, double-click to edit the text).
  1. Explain how you figured out whether direction (x, y or z) corresponded to "right" on the screen.
  2. How far, in meters, did the box move during the time the simulation ran? Explain your reasoning.