Äîêóìåíò âçÿò èç êýøà ïîèñêîâîé ìàøèíû. Àäðåñ îðèãèíàëüíîãî äîêóìåíòà : http://www.mrao.cam.ac.uk/~rachael/compphys/Lecture%201.pdf
Äàòà èçìåíåíèÿ: Tue Oct 30 16:35:12 2007
Äàòà èíäåêñèðîâàíèÿ: Tue Oct 2 07:12:52 2012
Êîäèðîâêà:

Ïîèñêîâûå ñëîâà: ï ï ï ï ï ï ï ï ï ï ï ï ï ï ï ï ï ï ï ð ï ð ï ð ï ð ï ð ï ð ï ð ï
Computational Physics 2007

Rachael Padman ­ rp10001@cam.ac.uk

4-Oct-2007

Computational Physics 2007

1


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)
4-Oct-2007 Computational Physics 2007 2


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)!
4-Oct-2007 Computational Physics 2007 3


General advice about the course
Computational 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:00 Assessment: · 24% - analysis of computational physics of problem · 28% - implementation of algorithm · 24% - results, analysis of errors · 24% - presentation
4-Oct-2007 Computational Physics 2007 4


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")
Computational Physics 2007 5

4-Oct-2007


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

4-Oct-2007

Computational Physics 2007

6


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?
4-Oct-2007 Computational Physics 2007 7


About software...
Assembler

4-Oct-2007

Computational Physics 2007

8


About software...
Higher-level language (Fortran) Also: · C / C++ · Java · Cobol · Pascal · Algol ·...
4-Oct-2007

program table implicit none ! define variables used to hold position and velocity real :: x, y, vx, vy, dt ! set the initial values for the position and velocity x = 0.0; y = 0.0 vx = 1.5; vy = 2.0 ! and the time step dt = 0.1 x = x + vx * dt y = y + vy * dt write(*,*) x, y end program table

Computational Physics 2007

9


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 Fortran Fortran Fortran IV (= Fortran 66) 77: introduced structured programming 90: modularity, use of virtual memory 95: improvements for parallel processing

4-Oct-2007

Computational Physics 2007

10


Programming Fortran (1)
Simplest possible program: To create:
1. 2. 3. 4. · · Edit (emacs) Compile (f95) (Link) Run (type name) program ex1 ! ! My first program ! write(*,*) 'Hello there' end program ex1

note:

>> ex1 [return] Begin/end structure Hello there Comments
Computational Physics 2007 11

4-Oct-2007


Programming Fortran
program ex1a ! ! My first program ! write(*,*) 'Hello there' write(*,*) `Ok?'

Add another statement Notes: 1. Statements executed end program ex1a sequentially 2. White space meaningless >> ex1a [return] Hello there Ok?
4-Oct-2007 Computational Physics 2007

12


Programming Fortran
program convert

Let's do some sums. Notes:
1. Meaning of "=" 2. Forcing type to real 3. Mixing characters and values in write statement. 4. Use brackets to force evaluation order
4-Oct-2007

! Convert temperatures tc = 20.0 tf = (tc*1.8) + 32.0 write(*,*) `tf=`, tf end program convert >> convert [return] tf= 68.0000
13

Computational Physics 2007


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

· Real (10.0, 27.2, 1.38e-23)
0 0 1 0 1 1 1 1

Mantissa

Exponent

· Complex ( 10.0 ­ 3.1i (10.0, -3.1) ) · Character ( `This is a string' )
4-Oct-2007 Computational Physics 2007 14


Programming Fortran ­ type management
A better way to do it note:
· Fortran has implicit typing
integer :: (i-n) real :: (a-h),(o-z)

program convert ! Convert temperatures implicit none real :: tc, tf tc = 20 tf = (tc*1.8) +32.0 write(*,*) `tf=`, tf end program convert

·

Automatic type conversion

4-Oct-2007

Computational Physics 2007

15


Programming Fortran ­ control structures

Inviting an error...

program square_root : : write(*,*) `type a number' read (*,*) x rootx = sqrt(x) write(*,*) x, rootx end program square_root

4-Oct-2007

Computational Physics 2007

16


Programming Fortran ­ If statement
program square_root : : write(*,*) `type a number' read (*,*) x if (x<=0.0) stop `x must be positive!' rootx = sqrt(x) write(*,*) x, rootx end program square_root

Better...
1. If statement to test for bad data 2. Stop to halt program execution

4-Oct-2007

Computational Physics 2007

17


Programming Fortran ­ Control structures
program convert2 : notes: : · if.. endif control i=0 10 continue structure if (i<=10) then · Go to