# Fortran programming by ntz11397

VIEWS: 44 PAGES: 20

• pg 1
```									Fortran programming
Martin Dove
University of Cambridge
Background 1
‣ Fortran is a program of choice for
scientiﬁc computation
‣ It does a good job of translating
mathematical formulae into computing
operations
‣ It is not so good for other tasks (eg text,
system calls)
‣ Reference: http://en.wikipedia.org/wiki/
Fortran
‣ Program reference is on camtools
2
Background 2
‣ There are three ﬂavours of Fortran in
use: F77, F90 and F95
‣ F90 and F95 are practically very
similar, and are a signiﬁcant
modernisation of F77
‣ Fortran is a language for translating
instructions into operations, but only in
a non-dynamic sense; the program
needs to be converted to binary format
before it can be used
3
Sample code
program skills1

! This program will find the roots of a quadratic equation
! with coefficients supplied by the user

! The idea is to deduce the two solutions of the equation
! ax^2 + bx + c = 0, where x = (-b ± sqrt(b^2 - 4ac))/2a

! We need to check that solutions exist, or else we will end
! up with a root of a minus number

double precision a,b,c,x1,x2,sroot

sroot = b**2 - 4.0d0*a*c

if (sroot > 0.0d0) then   ! If we have real solutions ...
x1 = (b + dsqrt(sroot))/(2.0d0*a)
x2 = (b - dsqrt(sroot))/(2.0d0*a)
write(6,'(a,2f10.5)') 'Solutions are :',x1,x2
else                      ! Abort if not
write(6,'(a)') 'No real solutions exist, sorry'
end if

end program
4
Program statement
program skills1
Begin with a program statement,
! This program will find the  and of quadratic equation
roots endawith an end statement
! with coefficients supplied by the user

! The idea is to deduce the two solutions of the equation
! ax^2 + bx + c = 0, where x = (-b ± sqrt(b^2 - 4ac))/2a

! We need to check that solutions exist, or else we will end
! up with a root of a minus number

double precision a,b,c,x1,x2,sroot

sroot = b**2 - 4.0d0*a*c

if (sroot > 0.0d0) then   ! If we have real solutions ...
x1 = (b + dsqrt(sroot))/(2.0d0*a)
x2 = (b - dsqrt(sroot))/(2.0d0*a)
write(6,'(a,2f10.5)') 'Solutions are :',x1,x2
else                      ! Abort if not
write(6,'(a)') 'No real solutions exist, sorry'
end if

end program
5
Comment statements
program skills1

! This program will find the roots of a quadratic equation
! with coefficients supplied by the user

! The idea is to deduce the two solutions of the equation
! ax^2 + bx + c = 0, where x = (-b ± sqrt(b^2 - 4ac))/2a

exist, or statements, end
! We need to check that solutions Comment else we will to tell   you
! up with a root of a minus numberwhat you are doing
double precision a,b,c,x1,x2,sroot

sroot = b**2 - 4.0d0*a*c

if (sroot > 0.0d0) then   ! If we have real solutions ...
x1 = (b + dsqrt(sroot))/(2.0d0*a)
x2 = (b - dsqrt(sroot))/(2.0d0*a)
write(6,'(a,2f10.5)') 'Solutions are :',x1,x2
else                      ! Abort if not
write(6,'(a)') 'No real solutions exist, sorry'
end if

end program
6
Assign variable types
program skills1

! This program will find the roots of a quadratic equation
! with coefficients supplied by the user

! The idea is to deduce the two solutions of the equation
! ax^2 + bx + c = 0, where x = (-b ± sqrt(b^2 - 4ac))/2a

exist, variable types; end
! We need to check that solutions Deﬁne or else we will here    we
! up with a root of a minus numberhave 64-bit real numbers
double precision a,b,c,x1,x2,sroot

sroot = b**2 - 4.0d0*a*c

if (sroot > 0.0d0) then   ! If we have real solutions ...
x1 = (b + dsqrt(sroot))/(2.0d0*a)
x2 = (b - dsqrt(sroot))/(2.0d0*a)
write(6,'(a,2f10.5)') 'Solutions are :',x1,x2
else                      ! Abort if not
write(6,'(a)') 'No real solutions exist, sorry'
end if

end program
7
Write statements
program skills1

! This program will find the roots of a quadratic equation
! with coefficients supplied by the user

! The idea is to deduce the two solutions of the equation
! ax^2 + bx + c = 0, where x = (-b ± sqrt(b^2 - 4ac))/2a

! We need to check that solutions exist, or else we will end
! up with a root of a minus number
Write to standard output
double precision             (channel 6), default = the screen
a,b,c,x1,x2,sroot

sroot = b**2 - 4.0d0*a*c

if (sroot > 0.0d0) then   ! If we have real solutions ...
x1 = (b + dsqrt(sroot))/(2.0d0*a)
x2 = (b - dsqrt(sroot))/(2.0d0*a)
write(6,'(a,2f10.5)') 'Solutions are :',x1,x2
else                      ! Abort if not
write(6,'(a)') 'No real solutions exist, sorry'
end if

end program
8
Ouput format 1
program skills1

! This program will find the roots of a quadratic equation
! with coefficients supplied by the user

! The idea is to deduce the two solutions of the equation
! ax^2 + bx + c = 0, where x = (-b ± sqrt(b^2 - 4ac))/2a

! We need to check that solutions exist, or else we will end
! up with a root of a minus number
(a,\$) means text without a line
double precision             return
a,b,c,x1,x2,sroot

sroot = b**2 - 4.0d0*a*c

if (sroot > 0.0d0) then   ! If we have real solutions ...
x1 = (b + dsqrt(sroot))/(2.0d0*a)
x2 = (b - dsqrt(sroot))/(2.0d0*a)
write(6,'(a,2f10.5)') 'Solutions are :',x1,x2
else                      ! Abort if not
write(6,'(a)') 'No real solutions exist, sorry'
end if

end program
9
Ouput format 2
program skills1

! This program will find the roots of a quadratic equation
! with coefficients supplied by the user

! The idea is to deduce the two solutions of the equation
! ax^2 + bx + c = 0, where x = (-b ± sqrt(b^2 - 4ac))/2a

! We need to check that solutions exist, or else we will end
! up with a root of a minus number

double precision a,b,c,x1,x2,sroot

sroot = b**2 - 4.0d0*a*c      (a,2f10.5) means text and 2
if (sroot >   0.0d0) then      If we have 10 solutions and
! numbers ofrealspace size ... 5
x1 = (b +                   decimal
dsqrt(sroot))/(2.0d0*a) places
x2 = (b - dsqrt(sroot))/(2.0d0*a)
write(6,'(a,2f10.5)') 'Solutions are :',x1,x2
else                      ! Abort if not
write(6,'(a)') 'No real solutions exist, sorry'
end if

end program
10
program skills1

! This program will find the roots of a quadratic equation
! with coefficients supplied by the user

! The idea is to deduce the two solutions of the equation
! ax^2 + bx + c = 0, where x = (-b ± sqrt(b^2 - 4ac))/2a

! We need to check that solutions exist, or else we will end
! up with a root of a minus number
input (channel
double precision a,b,c,x1,x2,sroot          5); default is the
terminal

sroot = b**2 - 4.0d0*a*c

if (sroot > 0.0d0) then   ! If we have real solutions ...
x1 = (b + dsqrt(sroot))/(2.0d0*a)
x2 = (b - dsqrt(sroot))/(2.0d0*a)
write(6,'(a,2f10.5)') 'Solutions are :',x1,x2
else                      ! Abort if not
write(6,'(a)') 'No real solutions exist, sorry'
end if

end program
11
Formatting
program skills1

! This program will find the roots of a quadratic equation
! with coefficients supplied by the user

! The idea is to deduce the two solutions of the equation
! ax^2 + bx + c = 0, where x = (-b ± sqrt(b^2 - 4ac))/2a

! We need to check that solutions exist, or else we will end
*
! up with a root of a minus number means that you are not
requiring
double precision a,b,c,x1,x2,sroot     formatting of input
data (useful for terminal input)

sroot = b**2 - 4.0d0*a*c

if (sroot > 0.0d0) then   ! If we have real solutions ...
x1 = (b + dsqrt(sroot))/(2.0d0*a)
x2 = (b - dsqrt(sroot))/(2.0d0*a)
write(6,'(a,2f10.5)') 'Solutions are :',x1,x2
else                      ! Abort if not
write(6,'(a)') 'No real solutions exist, sorry'
end if

end program
12
Some operations
program skills1

! This program will find the roots of a quadratic equation
! with coefficients supplied by the user

! The idea is to deduce the two solutions of the equation
! ax^2 + bx + c = 0, where x = (-b ± sqrt(b^2 - 4ac))/2a

! We need to check that solutions exist, or else we will end
b**2 means b raised to the
! up with a root of a minus number
power
double precision a,b,c,x1,x2,sroot   of 2 (an integer here)
4.0d0 means that we
write(6,'(a,\$)') 'Please give a,b,c: '          have a
double precision value
sroot = b**2 - 4.0d0*a*c
–,* mean subtract and multiply
if (sroot > 0.0d0) then   ! If we have real solutions ...
x1 = (b + dsqrt(sroot))/(2.0d0*a)
x2 = (b - dsqrt(sroot))/(2.0d0*a)
write(6,'(a,2f10.5)') 'Solutions are :',x1,x2
else                      ! Abort if not
write(6,'(a)') 'No real solutions exist, sorry'
end if

end program
13
Conditional statements
program skills1

! This program will find the roots of a quadratic equation
! with coefficients supplied by the user

! The idea is to deduce the two solutions of the equation
! ax^2 + bx + c = 0, where x = (-b ± sqrt(b^2 - 4ac))/2a

! We need to check that solutions exist, or else we will end
! up with a root of a minus number

double precision a,b,c,x1,x2,sroot

sroot = b**2 - 4.0d0*a*c

if (sroot > 0.0d0) then    if .. we have real solutions ...
! If else construct
x1 = (b + dsqrt(sroot))/(2.0d0*a)
Here we
x2 = (b - dsqrt(sroot))/(2.0d0*a) have a “greater than”
write(6,'(a,2f10.5)') 'Solutions are :',x1,x2
else                       condition; the
! Abort if not condition is that
the quantity in sorry'
write(6,'(a)') 'No real solutions exist,the brackets
end if
should be “true”
end program
14
Some more operations
program skills1

! This program will find the roots of a quadratic equation
! with coefficients supplied by the user

! The idea is to deduce the two solutions of the equation
! ax^2 + bx + c = 0, where x = (-b ± sqrt(b^2 - 4ac))/2a

dsqrt = or else we of double
! We need to check that solutions exist, square root will end
! up with a root of a minus number
precision number
double precision a,b,c,x1,x2,sroot
/ means divide
read(5,*) a,b,c            Note in of brackets
sroot = b**2 - 4.0d0*a*c   that terms are evaluated in the
intended order
if (sroot > 0.0d0) then   ! If we have real solutions ...
x1 = (b + dsqrt(sroot))/(2.0d0*a)
x2 = (b - dsqrt(sroot))/(2.0d0*a)
write(6,'(a,2f10.5)') 'Solutions are :',x1,x2
else                      ! Abort if not
write(6,'(a)') 'No real solutions exist, sorry'
end if

end program
15
Sample code
program skills1

! This program will find the roots of a quadratic equation
! with coefficients supplied by the user

! The idea is to deduce the two solutions of the equation
! ax^2 + bx + c = 0, where x = (-b ± sqrt(b^2 - 4ac))/2a

! We need to check that solutions exist, or else we will end
! up with a root of a minus number

double precision a,b,c,x1,x2,sroot

sroot = b**2 - 4.0d0*a*c

if (sroot > 0.0d0) then   ! If we have real solutions ...
x1 = (b + dsqrt(sroot))/(2.0d0*a)
x2 = (b - dsqrt(sroot))/(2.0d0*a)
write(6,'(a,2f10.5)') 'Solutions are :',x1,x2
else                      ! Abort if not
write(6,'(a)') 'No real solutions exist, sorry'
end if

end program
16
How to use this program
‣ We need to turn this text ﬁle into a
binary “executable” – a process
called “compiling”
‣ A computer may have a number of
different Fortran compilers; you have
to choose!
‣ Then simply treat the binary as a
command

17
Compiling this program
f95 -o skills1 skills1.f95
‣ The -o skills1 parameter gives
the name of the binary
‣ The last parameter is the name of the
text ﬁle containing the program
‣ There are other parameters we can
use, particularly those that control
optimisation (will depend on the
compiler)
18
What’s next?
‣   Looping over parameter values
‣   Data types (eg integer, character)
‣   Arrays
‣   Program structures (eg subroutines)
‣   Input/output (including ﬁle handling)
‣   Program statements
‣   Available functions (eg trigonometric)
‣   Expressions (eg more conditions)

19
Context
Unix! We need to know about
‣ File and directory structures
‣ Some basic commands
‣ Editors
‣ Working environment
‣ Dangers!

20

```
To top