15.02.2015 Views

Numerical Modelling in Fortran: day 2

Numerical Modelling in Fortran: day 2

Numerical Modelling in Fortran: day 2

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Numerical</strong> <strong>Modell<strong>in</strong>g</strong> <strong>in</strong><br />

<strong>Fortran</strong>: <strong>day</strong> 2<br />

Paul Tackley, 2014


Goals for to<strong>day</strong><br />

• Review ma<strong>in</strong> po<strong>in</strong>ts <strong>in</strong> onl<strong>in</strong>e materials you<br />

read for homework<br />

– http://www.cs.mtu.edu/%7eshene/COURSES/cs201/NOTES/<strong>in</strong>tro.html<br />

• More details about loops<br />

• F<strong>in</strong>ite difference approximation<br />

• Introduce and practice<br />

– subrout<strong>in</strong>es & functions<br />

– arrays


Miscellaneous th<strong>in</strong>gs<br />

• Cont<strong>in</strong>u<strong>in</strong>g l<strong>in</strong>es:<br />

– f95 use ‘&’ at the end of the l<strong>in</strong>e<br />

– f77: put any character <strong>in</strong> column 6 on next l<strong>in</strong>e<br />

• Formats of constants:<br />

– Use ‘.’ to dist<strong>in</strong>guish real from <strong>in</strong>teger (avoid 2/3=0 !)<br />

– 1.234x10 -13 is written as 1.234e-13<br />

• logical variables have 2 values: .true. or .false.<br />

• Variable nam<strong>in</strong>g rules:<br />

– start with letter<br />

– mix numbers, letters and _<br />

– no spaces


Character/str<strong>in</strong>g def<strong>in</strong>itions<br />

• character :: a<br />

(s<strong>in</strong>gle character)<br />

• character(len=10) :: a (str<strong>in</strong>g of length 10)<br />

• character :: a*10, b*5<br />

• character*15:: a,b<br />

(fortran77 style)<br />

• character(len=*) :: Name=‘Paul’<br />

– automatic length, otherwise str<strong>in</strong>gs will be<br />

truncated or padded with spaces to fit declared<br />

length


Initialis<strong>in</strong>g variables<br />

• Always <strong>in</strong>itialise variables! (don’t<br />

assume they will automatically be set to<br />

0, for example)<br />

• Either<br />

– when def<strong>in</strong>ed, e.g., real:: a=5.<br />

– <strong>in</strong> the program, e.g., a=5.0<br />

– read from keyboard or file


Arithmetic operators<br />

• e.g., what does a+b*c**3 mean<br />

– ((a+b)*c)**3 a+(b*c)**3 etc.<br />

– No! correct is: a+(b*(c**3))<br />

– priority is **, (* or /) then (+ or -)<br />

• Also LOGICAL operators, <strong>in</strong> this priority:<br />

– .not., .and., .or., (.eqv. or .neqv.)<br />

• Be careful mix<strong>in</strong>g variable types (e.g.<br />

<strong>in</strong>teger & real) <strong>in</strong> the same expression!


Some <strong>in</strong>tr<strong>in</strong>sic mathematical<br />

functions<br />

• abs (absolute value, real or <strong>in</strong>teger)<br />

• sqrt (square root)<br />

• s<strong>in</strong>, cos, tan, as<strong>in</strong>, acos, atan, atan2:<br />

assume angles are <strong>in</strong> radians<br />

• exp and log : log is natural log, use log10<br />

for base 10.<br />

• also cosh, s<strong>in</strong>h, tanh<br />

• for full list see a manual


Some conversion functions<br />

• Int(a): round to smaller # (4.7->4; -4.6->-4)<br />

• N<strong>in</strong>t(a): nearest <strong>in</strong>teger (4.7->5; -4.6->-5)<br />

• Floor(a): (4.7->4; -4.6->-5)<br />

• Ceil<strong>in</strong>g(a): (4.7->5; -4.6->-4)<br />

• Float(i): <strong>in</strong>teger -> real<br />

• Real(c): real part of complex<br />

• mod(a,b) is rema<strong>in</strong>der of x-<strong>in</strong>t(x/y)*y<br />

• max(a,b,c,….), m<strong>in</strong>(a,b,c,…)


ead(*,*) and write(*,*)<br />

• read(*,*) and write(*,*) do the same th<strong>in</strong>g as<br />

read* and pr<strong>in</strong>t* but are more flexible:<br />

– The 1st * can be changed to a file number, to<br />

read or write from a file<br />

– The 2nd * can be used to specify the format<br />

(e.g., number of decimal places)<br />

• More about this later <strong>in</strong> the course!


do<br />

loops:<br />

more<br />

types


functions and subrout<strong>in</strong>es<br />

• Useful for perform<strong>in</strong>g tasks that are<br />

performed more than once <strong>in</strong> a program<br />

and/or<br />

• Modularis<strong>in</strong>g (splitt<strong>in</strong>g up <strong>in</strong>to logical<br />

chunks) the code to make it more<br />

understandable<br />

• A function returns a value, a subrout<strong>in</strong>e<br />

doesn’t (except through chang<strong>in</strong>g its<br />

arguments)


example functions


same th<strong>in</strong>g as subrout<strong>in</strong>es (less elegant)


Internal vs. external functions<br />

• Internal functions (f90-) are conta<strong>in</strong>ed<br />

with<strong>in</strong> the program, and therefore the<br />

compiler can l<strong>in</strong>k them easily<br />

• External functions are def<strong>in</strong>ed outside the<br />

ma<strong>in</strong> program, so the call<strong>in</strong>g rout<strong>in</strong>e must<br />

declare their type (e.g., <strong>in</strong>teger, real).<br />

– In f90 it is also possible to specify the type of<br />

all the arguments, us<strong>in</strong>g an explicit <strong>in</strong>terface<br />

block, which has various advantages.


Example <strong>in</strong>ternal function


…and as an external function


A<br />

r<br />

r<br />

a<br />

y<br />

s


Notes<br />

• Indices start at 1 and go up to the declared<br />

value, e.g., if declare a(5) then it has<br />

components a(1),a(2)…a(5)<br />

• To get a different lower <strong>in</strong>dex, e.g., a(-5:5)<br />

• In subrout<strong>in</strong>es&functions an argument can<br />

be used to dimension arrays<br />

• In allocate statements other variables can be<br />

used<br />

• Use of the (a,$) format <strong>in</strong> write avoids<br />

carriage return at the end<br />

• Note implicit do loop n(j),j=1,3


Homework<br />

• At the ‘<strong>Fortran</strong> 90 Tutorial’ at<br />

http://www.cs.mtu.edu/%7eshene/<br />

COURSES/cs201/NOTES/fortran.html<br />

• Read through the sections<br />

– Selective Execution<br />

– Repetitive Execution<br />

– Functions (not modules - yet)<br />

• Do the exercises on the next slides and<br />

hand <strong>in</strong> by email (.f90 files)


Exercise 1: statements & loops<br />

• Write statements to<br />

– Declare a str<strong>in</strong>g of length 15 characters<br />

– Declare an <strong>in</strong>teger parameter = 5<br />

– Declare a 1-dimensional array with <strong>in</strong>dices runn<strong>in</strong>g from<br />

-1 to +10<br />

– Declare a 4-dimensional allocatable array<br />

– Convert a real number to the nearest <strong>in</strong>teger<br />

– Calculate the rema<strong>in</strong>der after a is divided by b<br />

• Write loops to<br />

– Add up all even numbers between 12 and 124<br />

– Test each element of array a(1:100) start<strong>in</strong>g from 1; if the<br />

element is positive pr<strong>in</strong>t a message to the screen and<br />

leave the loop


Exercise 2: Mean and standard deviation<br />

• Convert your mean & stddev program from<br />

last week <strong>in</strong>to a function or subrout<strong>in</strong>e<br />

that operates on a 1-D array passed <strong>in</strong> as<br />

an argument.<br />

• Write a ma<strong>in</strong> program that<br />

– asks for the number of values,<br />

– allocates the array,<br />

– reads the values <strong>in</strong>to the array,<br />

– calls the function you wrote and<br />

– pr<strong>in</strong>ts the result<br />

• A function can’t return both mean & stddev,<br />

so one of them will have to be an argument


Derivatives us<strong>in</strong>g f<strong>in</strong>ite-differences<br />

• Graphical <strong>in</strong>terpretation: df/dx(x) is<br />

slope of (tangent to) graph of f(x) vs. x<br />

• Calculus def<strong>in</strong>ition:<br />

df<br />

dx ≡ f ′ (x) ≡ lim<br />

dx→ 0<br />

f (x + dx) − f (x)<br />

dx<br />

• Computer version (f<strong>in</strong>ite differences):<br />

f ′(x) = f (x 2 ) − f (x 1 )<br />

x − x 2 1


F<strong>in</strong>ite Difference grid <strong>in</strong> 1-D<br />

• Grid po<strong>in</strong>ts x 0 , x 1 , x 2 …x N<br />

• Here xi=x 0 +i*h<br />

• Function values y 0 , y 1 , y 2 …y N<br />

• Stored <strong>in</strong> array y(i)<br />

• (<strong>Fortran</strong>, by default, starts<br />

arrays at i=1, but you can<br />

change this to i=0)<br />

⎛<br />

⎝<br />

⎜<br />

dy<br />

dx<br />

⎞<br />

⎠<br />

⎟<br />

i<br />

≈ Δy<br />

Δx<br />

=<br />

y(i +1) − y(i)<br />

h


Concept of Discretization<br />

• True solution to equations is cont<strong>in</strong>uous <strong>in</strong><br />

space and time<br />

• In computer, space and time must be<br />

discretized <strong>in</strong>to dist<strong>in</strong>ct units/steps/po<strong>in</strong>ts<br />

• Equations are satisfied for each unit/step/<br />

po<strong>in</strong>t but not necessarily <strong>in</strong>between<br />

• <strong>Numerical</strong> solution approaches true solution<br />

as number of grid or time po<strong>in</strong>ts becomes<br />

larger


Analysis<br />

• Subrout<strong>in</strong>e arguments can have<br />

different names from those <strong>in</strong> call<strong>in</strong>g<br />

rout<strong>in</strong>e: what matters is order<br />

• FD approximation becomes more<br />

accurate as grid spac<strong>in</strong>g dx decreases<br />

• Allocate argument arrays <strong>in</strong> the call<strong>in</strong>g<br />

rout<strong>in</strong>e, not <strong>in</strong> the subrout<strong>in</strong>e/function


Summary: first derivative<br />

dy<br />

dx ≈ Δy<br />

Δx =<br />

y i − y i−1<br />

x i<br />

− x i−1<br />

=<br />

y i − y i−1<br />

h<br />

• Second derivative<br />

⎛<br />

⎜<br />

⎝<br />

∂ 2 y ⎞<br />

∂x 2 ⎟<br />

⎠<br />

i<br />

= y i+1 − 2y i + y i−1<br />

h 2


Exercise 3: Second derivative<br />

• Write a subrout<strong>in</strong>e that calculates the second<br />

derivative of an <strong>in</strong>put 1D array, us<strong>in</strong>g the f<strong>in</strong>ite<br />

difference approximation<br />

• The <strong>in</strong>puts will be the array, number of po<strong>in</strong>ts and grid<br />

spac<strong>in</strong>g.<br />

• The result<strong>in</strong>g 1-D array can be an <strong>in</strong>tent(out) argument.<br />

• Assume the derivative at the end po<strong>in</strong>ts is 0.<br />

• Test this rout<strong>in</strong>e by writ<strong>in</strong>g a ma<strong>in</strong> program that<br />

calls the subrout<strong>in</strong>e with two idealized functions for<br />

which you know the correct answer, e.g., s<strong>in</strong>(x),<br />

x**2.<br />

• Hand <strong>in</strong> your .f90 code and the results of your two<br />

tests

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!