Python Programming for Computational Science: An Introduction to Euler Method, Assignments of Physics

An introduction to computational science and its two main facets: providing quantitative solutions to known scientific laws and simulating complex models. It focuses on python programming using the vpython library for easy implementation. The basics of newton's laws, derivatives, and the euler method for numerical solutions.

Typology: Assignments

Pre 2010

Uploaded on 08/09/2009

koofers-user-nxr-2
koofers-user-nxr-2 🇺🇸

10 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lec1 - Intro, Python revisited
Mechanics, syllabus
General comments
What is computational science ?
Simple mechanics and intro to Python
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Python Programming for Computational Science: An Introduction to Euler Method and more Assignments Physics in PDF only on Docsity!

Lec1 - Intro, Python revisited

  • Mechanics, syllabus
  • General comments
  • What is computational science?
  • Simple mechanics and intro to Python

General comments

  • Not programming course. Not traditional physics course.
  • Topics drawn from many different areas of physics – mechanics, waves, statistical physics, quantum physics
  • Brief discussion of programming issues - only as much as needed to do the science
  • Python/VPython makes life easy. Don’t need to know much to do some quite com- plicated stuff (graphics built-in)

Examples

Type 1: throwing a ball into the air - time to reach ground? a)Newton’s laws, analytic solution (neglect air resistance, model as point particle) b)Real world: put in air resistance, spin of ball, can integrate the equations numerically with high accuracy.

Type 2: Show that water freezes when cooled. Features:

  • Many d.o.f, complex interactions
  • Qualitative change of state of system.

Errors: finite simulation time. Finite number of particles.

Newton’s laws

One particle, one dimension:

dx dt

= v dv dt

= a = F (x, v, t)/m

First order form important! Generalize to three dimensions and many par- ticles later Definitions of velocity and acceleration.

Euler II

Divide time into discrete steps t = ndt, n = 0 , 1... Full algorithm:

xn+1 = xn + dtvn vn+1 = vn + dtFn/m

Note xn ≡ x(ndt) Start from some x 0 , v 0. Generate x 1 , v 1 , then x 2 , v 2 , keep going .... Accurate to O(dt) Simple example – unit mass harmonic oscillator F = −x

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

VPython Objects

  • sphere is a standard Python object. It may have a number of attributes such as po- sition, color, size etc. You may also add attributes such as velocity (vel) which is defined to be a vector.
  • ball is the name of a specific one of these sphere objects created in this code with the line ball=sphere(...)
  • Attributes such as pos are accessed using the dot operator eg. ball.pos.

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 ??

General forces

Nice to rewrite Python code to make it easier to keep the force definition separate from the Euler update.

from force import * while(1): rate(100) t=t+dt ball.pos=ball.pos+ball.veldt ball.vel=ball.vel+force(ball.pos,ball.vel)dt

Need a new code force.py

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)

Drawing the path

Nice with 2D,3D problems to visualize the tra- jectory. Need the track attribute which adds an object of type curve which is standard in VPython.

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

while 1: rate(100) t=t+dt ball.pos=ball.pos+ball.veldt ball.vel=ball.vel+forcedt ball.track.append(pos=ball.pos)