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 1 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) 2 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. 5 Euler I What is meant by derivatives ? Nothing more than: dx dt = lim dt→0 x(t + dt) − x(t) dt Insert in first of Newton’s laws: dtv(t) ∼ x(t + dt) − x(t) turning this around x(t + dt) = x(t) + dtv(t) If know v(t) and use a small enough time step dt will give approx for x(t + dt) Do same for v(t + dt) 6 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 x0, v0. Generate x1, v1, then x2, v2, keep going .... Accurate to O(dt) Simple example – unit mass harmonic oscillator F = −x 7 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. 10 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. 11 Real oscillators Can add drag force f = −kv (why minus sign ?) Python code line changes: ball.vel=ball.vel-ball.pos-0.1*ball.vel*dt What is k ? - what happens larger k ? Damped oscillations - overdamping Linear damping analytic solution possible but not in general But numerical solution easy – just change force to say f = −kv2! 12 Force function # 2d oscillator def force(pos,vel): result = vector(0,0,0) result.x=-1.0*pos.x result.y=-1.0*pos.y result.z=0 return result 15 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) 16 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.01 t=0 while 1: rate(100) t=t+dt ball.pos=ball.pos+ball.vel*dt ball.vel=ball.vel+force*dt ball.track.append(pos=ball.pos) 17