Docsity

Lec2: Exploring Mechanics with Computational Science and Python, Assignments of Physics

Partial preview of the text

Download Lec2: Exploring Mechanics with Computational Science and Python and more Assignments Physics in PDF only on Docsity!

Lec2 - More mechanics

  • Summary of lec
  • D > 1. Drawing tracks
  • Python functions
  • Lissajou figures

Summary

  • What is Computational Science?
    • Discipline concerned with solving math- ematical models of physical systems us- ing computers
    • Small systems – “exact” solutions.
    • Large systems – simulations. Model- ing. New types of phenomena not visi- ble with few d.o.f
  • Earliest origins in trying to predict motions in solar system - mechanics + gravity
  • Mechanics: simplest method or algorithm to solve Newton’s laws on computer: Euler method

Python code

Create a code euler.py. Inside

from visual import *

no automatic scaling of window

scene.autoscale=

size of window

scene.range=10.

standard Python 3D object

ball=sphere(radius=0.5,pos=(0,8.0,0)) ball.vel=vector(0,0,0) dt=0. t=

loop over time (for ever!)

while(1): rate(100) t=t+dt ball.pos=ball.pos+ball.veldt ball.vel=ball.vel-ball.posdt

Comments

  • import statement allows us to use the 3D graphics module called VPython.
  • No automatic scaling of window to simula- tion. Fix size of window with range explic- itly.
  • rate is a Python function which controls the speed at which graphics is drawn to the screen. A large argument means a faster simulation in real time.
  • Simple assignment and arithmetic. The statment t=t+dt should be read as “ add the variable dt to the variable t and put the result back in the variable t”.

Control and Indentation

while(arg): command tells the computer to do something until arg is true (1 in computer speak). Structures like while typically act on a group of Python commands Notice the colon You can tell which ones by the level of inden- tation. Eg. in the last code the lines

rate(100) t=t+dt ball.pos=... ball.vel=...

are all carried out in the while loop. IDLE (Python editor) automatically idents codes that follows such a command.

2D Motion

Identical equations for all components eg (x, y) and (vx, vy) eg. new equations for y-motion

dy dt

= vy dvy dt

= Fy/m

Same Python code will automatically integrate them! Just need to change initial conditions to have vy(t = 0) 6 = 0 and add y-component to force What about 3D ??

Force function

2d oscillator

def force(pos,vel): result = vector(0,0,0) result.x=-1.0pos.x result.y=-1.0pos.y result.z= return result

Comments

  • def force() is followed by code defining what the function does.
  • Note indentation showing the lines of code inside the definition.
  • force function takes vector arguments and returns object result of type vector
  • result.x gives x-component of vector.
  • return is keyword (like import, def, while etc)

curve()

  • curve() is really a list of coordinate points which can be dynamically added to.
  • Also knows how to plot itself to the screen
  • Can be given attributes like radius, color etc
  • Like other standard Vpython objects can be added as an attribute to sphere()

Lissajou figures

  • Imagine using different harmonic springs in each direction. What is resulting motion?
  • Does motion close on finite length trajec- tory? When? What condition is needed ...
  • Finite orbits termed Lissajou figures