22
4-Oct-2007 Computational Physics 2007 1 Rachael Padman – [email protected] Computational Physics 2007

Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

  • Upload
    hadung

  • View
    224

  • Download
    5

Embed Size (px)

Citation preview

Page 1: Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

4-Oct-2007 Computational Physics 2007 1

Rachael Padman – [email protected]

Computational Physics 2007

Page 2: Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

4-Oct-2007 Computational Physics 2007 2

Structure of the course

• General advice about the course, introduction to programming

• Introduction to Fortran (~1 lecture)• Numerical methods – 5 topics, relating to the five

problems (~6 lectures)• Advanced topics (only if time)

PLUS• Self study (in PWF or connect to server)

Page 3: Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

4-Oct-2007 Computational Physics 2007 3

Why…

bother?– Part III projects; almost all jobs for physicists

Fortran?– Established– Designed for the problem– Easy to run on parallel computers– Easy to optimise

linux?– Common in scientific environments (e.g. CERN)!

Page 4: Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

4-Oct-2007 Computational Physics 2007 4

General advice about the courseComputational Physics Project- a complete piece of work that you alone are responsible

for.

NOT just writing a computer programme

Deadline – Thursday 17th January 2008 – 17:00Assessment:• 24% - analysis of computational physics of problem• 28% - implementation of algorithm• 24% - results, analysis of errors• 24% - presentation

Page 5: Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

4-Oct-2007 Computational Physics 2007 5

Doing the project

These lectures are not examinable. There are five problems. The relevant techniques

will be discussed in lectures:• Working with data: Fourier Transforms and Signal

Processing (“Big Ben”)• Ordinary DEs: Trojan asteroids• N-body simulations: Tidal tails of interacting galaxies• Eigenvalue problems: Localization in a linear chain• Abstract simulation: Percolation on a 2-D lattice (“Forest

Fire”)

Page 6: Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

4-Oct-2007 Computational Physics 2007 6

Get started!

If you are not already familiar with linux and Fortran, you are encouraged to start learning now– Demonstrator and HoC in PWF every afternoon in

weeks 3-8 (will have to share with Part IB for two weeks)

– Sign up for an afternoon at the PWF– Use the self-study guides to familiarize yourself with

the basic techniques: logging in, editing, compiling …

Page 7: Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

4-Oct-2007 Computational Physics 2007 7

About software

What is it?

A set of instructions that tell the CPU what to do.

At bottom, a set of ones and zeros!

How can we make this tractable?

Page 8: Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

4-Oct-2007 Computational Physics 2007 8

About software…

Assembler

Page 9: Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

4-Oct-2007 Computational Physics 2007 9

About software…

Higher-level language (Fortran)

Also:• C / C++• Java• Cobol• Pascal• Algol• …

program tableimplicit none! define variables used to hold position and velocityreal :: x, y, vx, vy, dt! set the initial values for the position and velocityx = 0.0; y = 0.0vx = 1.5; vy = 2.0! and the time stepdt = 0.1x = x + vx * dty = y + vy * dtwrite(*,*) x, y

end program table

Page 10: Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

4-Oct-2007 Computational Physics 2007 10

Fortran history

• Invented by John Backus (IBM) in 1962• Contraction of Formula translation• Designed to be simple to understand, but almost

as efficient in execution as Assembly language• Various standards:

– Fortran IV (= Fortran 66)– Fortran 77: introduced structured programming– Fortran 90: modularity, use of virtual memory– Fortran 95: improvements for parallel processing

Page 11: Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

4-Oct-2007 Computational Physics 2007 11

Programming Fortran (1)

Simplest possible program:

To create:1. Edit (emacs)2. Compile (f95)3. (Link)4. Run (type name)note:• Begin/end structure• Comments

program ex1

!! My first program!write(*,*) ’Hello there’

end program ex1

>> ex1 [return]Hello there

Page 12: Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

4-Oct-2007 Computational Physics 2007 12

Programming Fortran

Add another statementNotes:1. Statements executed

sequentially2. White space

meaningless

program ex1a!! My first program!write(*,*) ’Hello there’write(*,*) ‘Ok?’

end program ex1a

>> ex1a [return]Hello thereOk?

Page 13: Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

4-Oct-2007 Computational Physics 2007 13

Programming Fortran

Let’s do some sums.Notes:1. Meaning of “=“2. Forcing type to real3. Mixing characters

and values in write statement.

4. Use brackets to force evaluation order

program convert

! Convert temperatures

tc = 20.0tf = (tc*1.8) + 32.0write(*,*) ‘tf=‘, tf

end program convert

>> convert [return]tf= 68.0000

Page 14: Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

4-Oct-2007 Computational Physics 2007 14

Fortran types

Data can be stored in many different forms:• Integer (e.g. -5, 0, +27)

• Real (10.0, 27.2, 1.38e-23)

• Complex ( 10.0 – 3.1i ≡ (10.0, -3.1) )

• Character ( ‘This is a string’ )

0 0 0 0 10 0 1

0 0 1 0 11 1 1

Mantissa Exponent

Page 15: Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

4-Oct-2007 Computational Physics 2007 15

Programming Fortran – type management

A better way to do it

note:• Fortran has implicit

typing integer :: (i-n)real :: (a-h),(o-z)

• Automatic type conversion

program convert

! Convert temperatures

implicit nonereal :: tc, tf

tc = 20tf = (tc*1.8) +32.0write(*,*) ‘tf=‘, tf

end program convert

Page 16: Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

4-Oct-2007 Computational Physics 2007 16

Programming Fortran – control structures

Inviting an error…

program square_root::write(*,*) ‘type a number’read (*,*) xrootx = sqrt(x)write(*,*) x, rootx

end program square_root

Page 17: Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

4-Oct-2007 Computational Physics 2007 17

Programming Fortran – If statement

Better…1. If statement to

test for bad data2. Stop to halt

program execution

program square_root::write(*,*) ‘type a number’read (*,*) xif (x<=0.0) stop ‘x must be positive!’

rootx = sqrt(x)write(*,*) x, rootx

end program square_root

Page 18: Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

4-Oct-2007 Computational Physics 2007 18

Programming Fortran – Control structures

notes:• if.. endif control

structure• Go to <label>• Continue statement• meaning of i=i+1

i.e. Calculate i+1 and assign it to i

• use of indentation to make structure clear

program convert2::i=010 continueif (i<=10) then

tc = 10.0 * itf = (tc*1.8) +32.0write(*,*) tc, tfi = i + 1go to 10

end ifend program convert2

Page 19: Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

4-Oct-2007 Computational Physics 2007 19

Programming Fortran – control structures

Better…• use of do / enddo

– Test loop variable in range

– If not, exit loop– Otherwise, execute

loop and increment loop variable

• use of indentation to make structure clear

program convert3! Improved version:do i = 1, 10

tc = 10.0 * itf = (tc*1.8) +32.0write(*,*) tc, tf

end doend program convert3

Page 20: Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

4-Oct-2007 Computational Physics 2007 20

Programming Fortran – repeated code

Integer function fact(k)

! Computes factorials

fact = 1 do i = 1, kfact = fact*i

end do

end function fact

Function • Returns a value• Accepts one or more

dummy arguments• Arguments are passed by

address (not value)• Can also use subroutines

– more general

Page 21: Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

4-Oct-2007 Computational Physics 2007 21

Programming Fortran – repeated codeprogram demoimplicit noneinteger factwrite(*,*) fact(5)end program demo

integer function fact(k)! Computes factorialsfact = 1 do i = 1, kfact = fact*i

end do end function fact

Using functions • Pass a value (may be

variable or constant)• Computed value returned

to function name• See intrinsic functions –

trig, sqrt, etc etc

Page 22: Computational Physics 2007 - Cavendish Astrophysicsrachael/compphys/Lecture 1.pdf · ! and the time step dt = 0.1 ... introduced structured programming – Fortran 90: ... 4-Oct-2007

4-Oct-2007 Computational Physics 2007 22

Other topics…

• Much more on arrays …• Formatting output (the FORMAT statement)• Handling files (OPEN, CLOSE)• Combining programs in multiple file (Modules)• Memory handling (ALLOCATE)• Libraries (such as NAG)

See the self-study guide.

You will only learn programming by doing!