Worksheet 1: Integrators

Similar documents
These will be no tutorials for Math on Tuesday April 26.

These will be no tutorials for Math on Tuesday April 26.

Astro Lecture 12. Energy and Gravity (Cont d) 13/02/09 Habbal Astro Lecture 12 1

Leap Frog Solar System

Ph 22.1 Return of the ODEs: higher-order methods

Lesson 36: Satellites

Gravitation & Kepler s Laws

PTYS/ASTR 206 Section 2 Spring 2007 Homework #1 (Page 1/4)

Algorithms Exam TIN093 /DIT602

26. Introduction to the Solar System page 1

Gravitation. Makes the World Go Round

Rockets and Range Teacher Notes

Section 2: Friction, Gravity, and Elastic Forces

Chapter 5 Lecture Notes

Particle Systems. CSE169: Computer Animation Instructor: Steve Rotenberg UCSD, Winter 2017

UNIT 1 - FORCE GRAVITATIONAL FORCE ACTIVITY LESSON DESCRIPTION SCORE/POINTS 1. NTS GRAVITATIONAL NOTE GUIDE /10 2. NTS EXAMPLES OF GRAVITY FORMULA /10

Worksheet #04 Newton s Laws of Motion & Free-Body-Diagram.

Gravity Investigation

Updated 2013 (Mathematica Version) M1.1. Lab M1: The Simple Pendulum


MITOCW watch?v=dztkqqy2jn4

F = ma W = mg v = D t

How to work out really complicated motion. Iteration and Problem Solving Strategies. Let s go. Vertical spring-mass.

Modeling and Simulation I

Name. Satellite Motion Lab

Name: Earth 110 Exploration of the Solar System Assignment 1: Celestial Motions and Forces Due on Tuesday, Jan. 19, 2016

Problem Solving. Undergraduate Physics

You Might Also Like. I look forward helping you focus your instruction while saving tons of time. Kesler Science Station Lab Activities 40%+ Savings!

Motion and Forces Lab

Exercise 1. In the lecture you have used logistic regression as a binary classifier to assign a label y i { 1, 1} for a sample X i R D by

A Question about free-body diagrams

Unit 5 Gravitation. Newton s Law of Universal Gravitation Kepler s Laws of Planetary Motion

VISUAL PHYSICS ONLINE

/////// ///////////// Module ONE /////////////// ///////// Space

Yanbu University College. General Studies Department. Phsc001 Course (111) Chapter2 (forces) Worksheet Solutions

Optimal Control - Homework Exercise 3

Gravitation. Program Support Notes. Grades 10 - College. 32mins. Physics. Video Education America Bringing Learning to Life.

Illustrate It! You will need to set out colored pencil and markers at this station.

Dynamics. What You Will Learn. Introduction. Relating Position, Velocity, Acceleration and Time. Examples

Lab 6: The Planets and Kepler

Wiley Plus Reminder! Assignment 1

AP Physics C 2015 Summer Assignment

1 Newton s 2nd and 3rd Laws

Your Second Physics Simulation: A Mass on a Spring

Chapter 4: Dynamics. Newton s Laws

Momentum Balances & Quadratic Equations

AP Unit 8: Uniform Circular Motion and Gravity HW


10-2: Friction, Gravity, and Elastic Forces. 8 th Grade Physical Sciences

More examples: Summary of previous lecture

Physics 115/242 The leapfrog method and other symplectic algorithms for integrating Newton s laws of motion

Episode 403: Orbital motion

Lecture 5. September 4, 2018 Math/CS 471: Introduction to Scientific Computing University of New Mexico

Forces and Motion in One Dimension

PHYSICS. Chapter 13 Lecture FOR SCIENTISTS AND ENGINEERS A STRATEGIC APPROACH 4/E RANDALL D. KNIGHT Pearson Education, Inc.

Quest Chapter 12. What things did Newton bring together and what did he toss? Read the text or check your notes. How does the moon move?

Flipping Physics Lecture Notes: Demonstrating Rotational Inertia (or Moment of Inertia)

Planetary Orbits: Kepler s Laws 1/18/07

ASTR 310 Tutorial 3: A Human Orrery

Testing Newton s 2nd Law

NUMERICAL ANALYSIS WEEKLY OVERVIEW

MA 102 Mathematics II Lecture Feb, 2015

Lecture Tutorial: Angular Momentum and Kepler s Second Law

Physics 20 Homework 2 SIMS 2016

Learning Outcomes in Focus

Error, Accuracy and Convergence

Ordinary differential equations. Phys 420/580 Lecture 8

LAB: FORCE AND MOTION

Practical Information

Newton s Law of Universal Gravitation (attraction between. Gravitation field strength surrounding the Earth g E

Lecture 3 - Pull! A Puzzle... m g. m g. = d Sin[θ] F μ N 1 (2)

CHAPTER 7 GRAVITATION

Astronomy 104: Stellar Astronomy

Supporting Video:

APS 1030 Astronomy Lab 79 Kepler's Laws KEPLER'S LAWS

Activity 95, Universal Gravitation! Issues & Earth Science: Student Book!!

Lab 5 Forces Part 1. Physics 211 Lab. You will be using Newton s 2 nd Law to help you examine the nature of these forces.

Prof. Dr. I. Nasser T171 Chapter5_I 12/10/2017

The Mass of Jupiter Student Guide

Chapter 7: Circular Motion

The net force on a moving object is suddenly reduced to zero. As a consequence, the object

Please turn on your clickers

CHAPTER 2. FORCE and Motion. CHAPTER s Objectives

ASTRONAUT PUSHES SPACECRAFT

FORCES & MOTION STUDY GUIDE. 1. What does it mean when forces are balanced on an object? (Exploration F)

Gravity. Newton s Law of Gravitation Kepler s Laws of Planetary Motion Gravitational Fields

End-of-Chapter Exercises

Motion on a linear air track

EDEXCEL NATIONAL CERTIFICATE/DIPLOMA SCIENCE FOR TECHNICIANS OUTCOME 3 - ENERGY TUTORIAL 1 MECHANICAL WORK, ENERGY AND POWER: WORK

Introduction to Differential Equations

8 Estimating the Earth s Density

YEAR 9 PHYSICS TIME: 2 hours

D. 2πmv 2 (Total 1 mark)

4.3 Conservation Laws in Astronomy

Topic 6 Circular Motion and Gravitation

PHYA2. General Certificate of Education Advanced Subsidiary Examination January Mechanics, Materials and Waves

= v = 2πr. = mv2 r. = v2 r. F g. a c. F c. Text: Chapter 12 Chapter 13. Chapter 13. Think and Explain: Think and Solve:

Concepts in Physics. Friday, October 16th

Homework Assignment 4 Root Finding Algorithms The Maximum Velocity of a Car Due: Friday, February 19, 2010 at 12noon

AM 205 Final Project The N-Body Problem

Transcription:

Simulation Methods in Physics I WS 2014/2015 Worksheet 1: Integrators Olaf Lenz Alexander Schlaich Stefan Kesselheim Florian Fahrenberger Peter Košovan October 22, 2014 Institute for Computational Physics, University of Stuttgart Contents 1 General Remarks 1 2 Cannonball 2 2.1 Simulating a cannonball............................ 2 2.2 Influence of friction and wind......................... 4 3 Solar system 6 3.1 Simulating the solar system with the Euler scheme............. 6 3.2 Integrators................................... 8 3.3 Long-term stability............................... 10 1 General Remarks Deadline for the report is Monday, 3rd November 2014, 10:00 a.m.. On this worksheet, you can achieve a maximum of 20 points. The report should be written as though it would be read by a fellow student who listens to the lecture, but does not do the tutorials. To hand in your report, send it to your tutor via email Florian (fweik@icp.uni-stuttgart.de) Johannes (zeman@icp.uni-stuttgart.de) Please attach the report to the email. For the report itself, please use the PDF format (we will not accept MS Word doc/docx files!). Include graphs and images into the report. 1

If the task is to write a program, please attach the source code of the program, so that we can test it ourselves. The report should be 5 10 pages long. We recommend to use L A TEX. A good template for a report is available on the home page of the lecture at http://www. icp.uni-stuttgart.de/~icp/simulation_methods_in_physics_i_ws_2014. The worksheets are to be solved in groups of two or three people. We will not accept hand-in exercises that only have a single name on it. 2 Cannonball 2.1 Simulating a cannonball In this exercise, you will simulate the trajectory of a cannonball in 2d until it hits the ground. At time t = 0, the ( cannonball ) (mass m = 2.0 kg) has a position of x(0) = 0 and a 50 m velocity of v(0) = 50 s. To simulate the cannonball, you will use the simple Euler scheme to propagate the position x(t) and velocity v(t) at time t to the time t + t ( t = 0.1 s): x(t + t) = x(t) + v(t) t (1) ( ) F(t) v(t + t) = v(t) + t (2) m ( ) 0 The force acting on the cannonball is gravity F(t) =, where g = 9.81 m is the mg s 2 acceleration due to gravity. The Euler scheme can be derived from a first order Taylor expansion of the position and velocity in time: x(t + t) = x(t) + x(t) t + O( t 2 ) t (3) v(t + t) = v(t) + v(t) t + O( t 2 ) t (4) (3 points) Write a Python program that simulates the cannonball until it hits the ground ({x} 1 0) and plot the trajectory. 2

Hints on preparing the report Whenever you are asked to write a program, hand in the program source code together with the report. In the report, you can put excerpts of the central parts of the code. When a program should plot something, you should include the plot into the report. Explain what you see in the plot! Hints for this task For those of you that do not know Python (yet), you can ask your tutor to give you a template for the program, where only the central parts are missing. Note that we will not give you a program file, but an image or a print-out, so that you will have to type the program into a text editor yourself. This is not meant to annoy you, but will help you to learn Python more quickly. The program you start writing in this task will be successively extended in the course of this worksheet. Therefore it pays off to invest some time to write this program cleanly! Throughout the program, you will use NumPy for numerics and Matplotlib for plotting, therefore import them at the beginning: from numpy import * from matplotlib. pyplot import * Model the position and velocity of the cannonball as 2d NumPy arrays: x=array ([0.0, 0.0 ]) Implement a function compute_forces(x) that returns the force (as a 2d NumPy array) acting on the cannonball at position x. Implement a function step_euler(x, v, dt) that performs a single time step dt of the Euler scheme for position x and velocity v. The function returns the new position x and velocity v. Beware that when you implement the euler step, you should first update the position and then the velocity. If you do it the other way round, you have implemented the so-called symplectic Euler algorithm, which will be discussed later. Remember that NumPy can do elementwise vector operations, so that in many cases there is no need to loop over array elements. Furthermore, these elementwise operations are significantly faster than the loops. For example, assuming that a and b are NumPy arrays of the same shape, the following expressions are equivalent: 3

for i in range (N): a[i] += b[i] # i s t h e same as a += b In the main program, implement a loop that calls the function step_euler() and stores the new position in the trajectory until the cannonball hits the ground. Store the positions at different times in the trajectory by appending them to a list of values # i n i t t h e t r a j e c t o r y traj = [] # append a new v a l u e o f x t o t h e t r a j e c t o r y traj.append ( x.copy ()) Note that when x is a NumPy array, it is necessary to use x.copy() so that the list stores the values, not a reference to the array. If x is a basic type (int, float, string), the call to copy() does not work. When the loop ends, make the trajectory a NumPy array and then plot the trajectory. # t r a n s f o r m t h e l i s t i n t o a NumPy a r r a y, which makes i t e a s i e r # t o p l o t traj = array ( traj ) # F i n a l l y, p l o t t h e t r a j e c t o r y plot ( traj [ :,0 ], traj [ :,1 ], - ) # and show t h e graph show () 2.2 Influence of friction and wind Now we will add the effect of aerodynamic friction and wind on the cannonball. Friction is a non-conservative force of the form F fric (v) = γ(v v 0 ). In our case, we assume that the friction coefficient is γ = 0.1 and that the wind blows parallel to the ground with a wind speed v w (v 0 = ( vw 0 ) m s ). 4

(3 points) Extend the program from the previous task to include the effects of aerodynamic friction. Create a plot that compares the following three trajectories: trajectory without friction trajectory with friction but without wind (v w = 0) trajectory with friction and with strong wind (v w = 50) Create a plot with trajectories at various wind speeds v w. In one of the trajectories, the ball shall hit the ground close to the initial position. Roughly what wind speed v w is needed for this to happen? Hints Extend the function compute_forces(x) so that it also takes the velocity v as an argument and add the friction force. Wrap the main loop into a function so that you can create several trajectories at different values of γ and v w in a single program. The constants γ and v w that are needed in the functions compute_forces() can either be modeled by global variables (use the keyword global), or by extending the functions compute_forces() and step_euler() by corresponding arguments. You can add legends to the plots like this: # make a p l o t w i t h l a b e l " f ( x ) " plot (x, y, label= "f(x)") # make t h e l a b e l s v i s i b l e legend () # show t h e graph show () 5

3 Solar system The goal of this exercise is to simulate a part of the solar system (Sun, Venus, Earth, the Moon, Mars, and Jupiter) in 2d, and to test the behavior of different integrators. In contrast to the previous task, you will now have to simulate several particles (in this case planets and the sun, in the previous case a cannonball) that interact while there is no constant or frictional force. In the following, x i denotes the position of the ith particle (likewise, the velocity v i and acceleration a i ). The behavior of the solar system is governed by the gravitational force between any two particles : r ij F ij = Gm i m j r ij 3 (5) where r ij = x i x j is the distance between particle i and j, G is the gravitational constant, and m i is the mass of particle i. The total force on any single particle is: N F i = F ij (6) j=0 i j 3.1 Simulating the solar system with the Euler scheme The file solar_system.pkl.gz, which can be downloaded from the lecture home page, contains the names of the particles, the initial positions, velocities, masses and the gravitational constant of a part of the solar system. The lengths are given in astronomical units AU (i.e. the distance between earth and sun), the time in years, and the mass in units of the earth s mass. (4 points) Make a copy of the program from the cannonball exercise and modify it to yield a program that simulates the solar system. Simulate the solar system for one year with a time step of t = 0.0001. Create a plot that shows the trajectories of the different particles. Perform the simulation for different time steps t {0.0001, 0.001} and plot the trajectory of the moon (particle number 2) in the rest frame of the earth (particle number 1). Are the trajectories satisfactory? Modern simulations handle up to a few billion particles. Assume that you would have to do a simulation with a large number of particles. What part of the code would use the most computing time? 6

Hints The file solar_system.pkl.gz can be read as follows: import pickle, gzip # l o a d i n i t i a l p o s i t i o n s and masses from f i l e datafile = gzip. open ( solar_ system. pkl. gz ) name, x_ init, v_ init, m, g = pickle. load ( datafile ) datafile. close () This code snippet uses the Python module pickle that can be used to write almost any Python object to a file and read it from there, as well as the module gzip that can be used to open and write a compressed file the same way as an ordinary Python file. Afterwards, name is a list of names of the planets that can be used in a plot to generate labels, x_init and v_init are the initial positions and velocities, m are the masses and g is the gravitational constant. As there are 6 particles now, the position vector x and the velocity vector v are now (2 6)-arrays, and the mass m is an array with 6 elements. The function you need to modify the most is the function compute_forces(x), which is now required to compute the gravitational forces according to equation (6). Here, you might actually have to write a loop over the elements of the array. To be able to do that, it is helpful to know how to access the shape of the array: # c r e a t e a 2 x2 array a = array ([[1,2],[3,4 ]]) # d e t e r m i n e t h e shape o f t h e a r r a y N, M = a. shape # sum a l l e l e m e n t s o f t h e a r r a y sum = 0.0 for i in range (N): for j in range (M): sum += a[ i,j ] When computing the forces, keep in mind Newton s third law, i.e. when particle j acts on particle i with the force F ij, particle i acts on particle j with the force F ij. If you wrote step_euler() using NumPy vector operations, it should not be necessary to modify the function. 7

3.2 Integrators In the previous exercises, you have used the Euler scheme (i.e. a simple mathematical method to solve a initial value problem) to solve Newton s equations of motion. It is the simplest integrator one could think of. However, the errors of the scheme are pretty large, and also the algorithm is not symplectic. Symplectic Euler algorithm The simplest symplectic integrator is the symplectic Euler algorithm: v(t + t) = v(t) + a(t) t (7) x(t + t) = x(t) + v(t + t) t (8) ( ) where x(t) are the positions and a(t) = F(t) m is the acceleration at time t. Compare the algorithm to the simple Euler scheme of equations (1) and (2). Verlet algorithm Another symplectic integrator is the Verlet algorithm, which has been derived in the lecture: ( x(t + t) = 2 x(t) x(t t) + a(t) t 2 + O t 4) (9) Velocity Verlet algorithm An alternative to the Verlet algorithm is the Velocity Verlet algorithm: x(t + t) = x(t) + v(t) t + a(t) ( 2 t2 + O t 4) (10) a(t) + a(t + t) ( v(t + t) = v(t) + t + O t 4). (11) 2 (3 points) Derive the Velocity Verlet algorithm. To derive the position update, use a Taylor expansion of x(t + t) truncated after second order. To derive the velocity update, Taylor-expand v(t+ t) up to the second order. To obtain an expression for 2 v(t)/ t 2, use a Taylor expansion for v(t + t)/ t truncated after the first order. Rearranging the equations of the Velocity Verlet algorithm, show that it is equivalent to the standard Verlet algorithm. First express x(t + t) using x, v and a at (t + t). in Equation (10). Then rearrange Equation (10) to express x(t). Add the two equations and then group velocity terms together. Put all velocity terms on one side of equation (11) and use them to plug them into your previous equation. 8

Implementation Even if you know the equations of the algorithms, this does not mean that it is immediately obvious how to implement them correctly and efficiently and how to use them in practice. For example, in the case of the Euler scheme (equations (1) and (2)), it is very simple to accidentally implement the symplectic Euler scheme instead. The following is pseudocode for a step of the Euler scheme: 1. x x + v t 2. v v + a t If you simply exchange the order of operations, this becomes the symplectic Euler algorithm. Another example for an algorithm that is tricky to use in a simulation is the Verlet algorithm. (1 point) Study equation (9). Why is it difficult to implement a simulation based on this equation in practice? What is missing? Therefore, the Velocity Verlet algorithm is more commonly used in simulations. Unfortunately, implementing equations (10) and (11) has its pitfalls, too. Note that the algorithm requires both a(t) and a(t + t) in equation (11). As computing a(t) requires to compute the forces F(t), this would make it necessary to compute the forces twice. To avoid this, one can store not only the positions x and velocities v in a variable, but also the accelerations a and implement a time step of the algorithm as follows: 1. Update positions as per equation (10), using the value of a stored in the previous time step. 2. Perform the first half of the velocity update of equation (11): v v + a 2 t 3. Compute the new forces and update the acceleration: a F m. 4. Perform the second half of the velocity update with the new acceleration a. (3 points) Implement the symplectic Euler algorithm and the Velocity Verlet algorithm in your simulation of the solar system. Run the simulation with a time step of t = 0.01 for 1 year for the different integrators and plot the trajectory of the moon in the rest frame of the earth. 9

Hint If you have written the rest of the program cleanly, it should be enough to implement new functions step_eulersym(x,v,dt) and step_vv(x,v,a,dt) and to modify the main loop accordingly to call these functions to use a different integrator. 3.3 Long-term stability An important property for Molecular Dynamics simulations is the long-term stability. (3 points) During the simulation, measure the distance between earth and moon in every timestep. Run the simulation with a time step of t = 0.01 for 10 years for the different integrators and plot the distance between earth and moon over time. Compare the results obtained with the different integrators! 10