Introduction to fortran
This presentation is the property of its rightful owner.
Sponsored Links
1 / 44

Introduction to FORTRAN PowerPoint PPT Presentation


  • 77 Views
  • Uploaded on
  • Presentation posted in: General

Introduction to FORTRAN. Suzanne Parete-Koon OLCF. “ Fortran changed the terms of communication between humans and computers. ” ~ New York Times. FOR mula TRAN slation Language – developed by IBM in the 1950s.

Download Presentation

Introduction to FORTRAN

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Introduction to fortran

Introduction to FORTRAN

Suzanne Parete-Koon

OLCF


Fortran changed the terms of communication between humans and computers new york times

“Fortran changed the terms of communication between humans and computers.” ~ New York Times

  • FORmula TRANslation Language – developed by IBM in the 1950s.

  • Still widely used today. ~50% of OLCF and NICS production simulation codes use it.

  • Has a very user friendly syntax


Fortran basics

Fortran basics

  • Variables

  • Selection

  • Loops

  • Arrays

  • First hour, we will get used to FORTRAN –Hello World,Variables, loops, selection, dog years.

  • 2nd hour we will use what what we learned with arrays and dot products.

  • When this session is over you should be able to write a “hello world” in Fortran


Setup 4 steps

Setup 4 steps

Step 1 Login and setup pin

Log in to the machine:

ssh [email protected]

Follow the prompts to set up your PIN.

Step 2 Start an interactive job

qsub -I -A UT-NTNLEDU –l size=16,walltime=02:00:00

Lowercase L

Upper case i


Setup

Setup

Step 3 Copy the code files to your lustre directory

cp –r /lustre/medusa/std0001/fortran /lustre/medusa/std00##/

Change to the fortran folder your lustre dir.

cd /lustre/medusa/std00##/fortran

Step 4 Switch to the gnu compiler

Module swap PrgEnv-cray PrgEnv-gnu


Step 0 vi tutorial

Step 0 : vi Tutorial

  • You will be writing this code with a text editor.

  • To create an open a file with vi:

  • vi filename.f90

  • To write in vi, type i, to stop writing hit esc.

  • To delete, when in write mode, delete key

  • To save hit esc :w

  • To quit hit esc :q


Program structure

Program Structure

Program program name

Variable declarations

Execution statements

Subprograms

End program name


Hello world fortran

Hello World Fortran

Type this in:

vi hello.f90

Type ito start writing.

Use the delete key to delete

Hit the esc key to stop writing.

Type :w to save

Type :qto exit

Vi Cheat sheet

To start vi hello.f90

To write i

Delete

if in write mode delete

if not in write mode x

To stop writing esc

Save :w

Exit :q

program hello

write(*,*)“Hello World!”

end program hello


Hello world fortran1

Hello World Fortran

hello.f90

write(*,*) – means write in the

default format, to the screen.

To compile:

gfortan hello.f90

To run:

./a.out

Vi Cheat sheet

To start vi hello.f90

To write i

Delete

if in write mode delete

if not in write mode x

To stop writing esc

Save :w

Exit :q

program hello

write(*,*)“Hello World”

end program hello


Hello world fortran2

Hello World Fortran


Variables fortran

Variables FORTRAN

  • FORTRAN supports six different data types:

  • Integer !32 bits

  • Real !32, 64 bits

  • Double precision !64 bits

  • Character

  • Complex

  • Logical


Variable declaration syntax

Variable Declaration Syntax

Type :: variable name

  • Integer :: x

  • Real :: fraction

  • Character (len= 3) :: three_letter_word


Hello world fortran3

Hello+ World Fortran

cp hello.f90 hello+.f90

vi hello+.f90

Compilegfortan hello+.f90

To run./a.out

Vi Cheat sheet

To start vi hello+.f90

To write i

Delete

if in write mode delete

if not in write mode x

To stop writing esc

Save :w

Exit :q

program hello

implicit none

integer:: x

character (len=12):: phrase

x=10

phrase="hello world!"

write(*,*) phrase, x

end program hello


Implicit none

Implicit None?

  • In the 1950s computers only had a few KB of memory

  • Programs needed to be as short as possible to fit

  • Fortran Variables were implicit- you did not have to declare them.

  • All variables starting with i, j, k, l, m and n, if not declared, are of the INTEGER type by default.

  • Typos were not caught by the compiler

    numberyears=nubmeryear+1

    Use “implicit none” unless you like unnecessary head aches.


Comments

Comments

  • Everything following a !is a comment and will be ignored by the compiler

!This program demonstrates the basics

program hello

implicit none ! No implicit variables

integer:: x ! Number of iterations

character (len=12):: phrase

x=10

phrase="hello world!"

write(*,*) phrase, x ! Write to screen

end program hello !End program


Arithmetic operations

Arithmetic Operations

  • + Addition z= y+x

  • - Subtraction y= z-x

  • * multiplication z=y*x

  • / Division y=z/x

  • ** Exponentiation three_squared= 3**2

    Operator Priority.

  • ** is the highest; * and / are the next, followed by + and –

  • Use () to ensure the wanted priority

    Age=20+7*(h-2)


Loops fortran

Loops Fortran

  • The Do loop Syntax

    integer :: index

    . . .

    do index=min,max

    operation(index)

    enddo

Integer:: I

Real :: a

a=1.01

do i=1,10

a=a+i

enddo

write(*,*) a


Hello world fortran4

Hello++ World Fortran

cp hello+.f90 hello++.f90

vi hello++.f90

To compile gfortan hello+.f90

To run:./a.out

Vi Cheat sheet

To start vi hello++.f90

To write i

Delete

if in write mode delete

if not in write mode x

To stop writing esc

Save :w

Exit :q

program hello

implicit none

integer:: x, i

character (len=12):: phrase

x=10

phrase="hello world!”

do i=1,x

write(*,*) phrase, i

enddo

end program hello


Hello world fortran5

Hello World++ Fortran


Fortran your turn dog years

Fortran your turn: Dog years

  • If a dog ages 7 dog years for each human year, write a program that coverts human years to dog years and writes the result to the screen. Do this for dogs between the ages of 1 and 10.

  • Start from scratch if you like

  • OR

  • vi fortran/dogyears.f90

  • For hints.

Vi Cheat sheet

To start vi dogyears.f90

To write i

Delete

if in write mode delete

if not in write mode x

To stop writing esc

Save :w

Exit :q


Fortran your turn dog years1

Fortran your turn: Dog years

! If a dog ages 7 dog years for each

! human year, write a program that coverts

!human years to dog years

! and writes the result.

program dogyears

implicit none

! declare two integers ,h and d,

! to hold human years and dog years

! Do a loop with h as the loop index.

! convert human years to dog years

!write the result

! Close the loop

end program dogyears


Fortran your turn dog years2

Fortran your turn: Dog years

program dogyears

implicit none

! declare two integers ,h and d,

!to hold human and dog years

integer :: d,h

! Do a loop with h as the loop index.

do h=1,10

! convert human years to dog years

d=7*h

!write the result

write(*,*) "In dog years, fido is ",d,".”

! Close the loop

enddo

end program dogyears

Compile gfortran dogyears.f90 run ./a.out


Dog years

Dog Years


Selection fortran

Selection FORTRAN

Syntax for if statements

IF (logical-expression) THEN

statements-1

ELSE

statements-2

END IF


Selection fortran1

Selection FORTRAN

if (x < 10)then

write(*,*) “low”

else

write(*,*) “high”

Endif

The conditions (a partial list)

< less than

> greater than

= equal too

<= less than or equal to


Fortran your turn dog years3

Fortran your turn: Dog years.

  • Use a selection statement to modify dogyears so that the dog ages 10 years per human year for the first two years and 7 years per human year for the rest of its life.

  • cp dogyears.f90 dogyears+.f90

  • vi dogyears+.f90

    Hints

    If dog is 2 or less

    d= h*10

    Else

    d=20+7*(h-2)

Vi Cheat sheet

To start vi dogyears+.f90

To write i

Delete

if in write mode delete

if not in write mode x

To stop writing esc

Save :w

Exit :q


Fortran your turn dog years4

Fortran your turn: Dog years

program dogyears

implicit none

! declare two integers,h,d, to hold human/dog years

integer :: d,

do h=1,10

if(h<3)then

d=10*h

write(*,*) "In dog years, fido is ",d,"."

else

d=20+7*(h-2)

write(*,*) "In dog years, fido is ",d,"."

endif

enddo

end program dogyears


Dog years1

Dog years+


Arrays fundamentals

Arrays Fundamentals

  • An array is a collection of data of the sametype.

  • Used for vector and matrix operations

  • We will calculate a vector dot product.

    a = (a1,a2,a3) b= (b1,b2,b3)

    ab =a*bCos(θ)

    Used to find the angle between vectors

    Use to find projection of a vector etc.


Arrays fundamentals1

Arrays Fundamentals

  • An array is a collection of data of the same type.

  • Syntax:

    type, DIMENSION(shape, shape ) :: name1,name2,name3

    The rank shown above is 2. A 3 dimensional array would have (shape,shape,shape). Fortran 90 can handle up to rank 7.

    The shape is the number of elements in that dimension.

    There is one more attribute, extent, that allows you to control where the indices start. The default is to start at 1.


Arrays fundamentals2

Arrays Fundamentals

  • One dimensional

    • Real, dimension(3) :: A ! A 1D floating point array with thee elements

    • integer, dimension (5) :: B ! A 1D integer array with 5 elements

  • Two dimensional

    • Real, dimension(2,2):: A ! A 2D array, (2 by 2)

    • Integer, dimension(2,3):: B ! A 2D array (2 by 3)

    • Not covered here, but arrays in Fortran can be allocated, after they are declared.

    • Integer, dimension(x,y):: B ! A 2D array x and y can be set later in the program.


Arrays fundamentals3

Arrays fundamentals

A quick way to set up an array in f90:

integer, dimension(3):: A =(/ 1, 2, 3/)

This gets tricky for multi-D arrays because Fortran is column major.


Arrays fundamentals4

Arrays fundamentals

  • This program is in

    /lustre/medusa/std00##/fortran/ array.f90

To Compile gfortran array.f90

To run ./a.out

program array

implicit none

integer, dimension(3):: A =(/ 1, 2, 3/)

integer, dimension(3):: C

integer :: i

DO i = 1, 3

C(i)=A(i)**2 !square A and store in C

write(*,*) i, A(i),C(i)

END DO

end program array


Arrays fundamentals5

Arrays fundamentals


Arrays fundamentals6

Arrays fundamentals

Modify array.f90 to give you the dot product of A and B.

Step1. Enter Vector B and multiply the elements of A and B

cp array.f90 array+.f90

Enter a vector B =(/3, 2, 4/).

Multiply A*B and store it in C.

Write A, B, and C to the screen.

Vi Cheat sheet

To start vi array+.f90

To write i

Delete

if in write mode delete

if not in write mode x

To stop writing esc

Save :w

Exit :q


Arrays fundamentals7

Arrays fundamentals

program array

implicit none

integer, dimension(3):: A = (/ 1, 2, 3/)

integer, dimension(3):: B = (/ 3, 2, 4/)

integer, dimension(3):: C

integer :: i

DO i = 1, 3

C(i)=A(i)*B(i) !square A and store in C

write(*,*) i, A(i),B(i),C(i)

END DO

end program array

To Compile: gfortran array.f90

To run ./a.out


Arrays fundamentals8

Arrays fundamentals


Arrays fundamentals9

Arrays fundamentals

Step2 Sum the element products.

cp array+.f90 array++.f90

Many ways your could do this, but using just what we’ve covered. .

Create an integer variable called dot.

Initialize dot to 0

Then put dot = dot+c(i) in the loop

Out side the loop

Write(*,*) dot


Arrays fundamentals10

Arrays fundamentals

program array

implicit none

integer, dimension(3):: A = (/ 1, 2, 3/)

integer, dimension(3):: B = (/ 3, 2, 4/)

integer, dimension(3):: C

integer :: dot

integer :: i

dot=0.0

DO i = 1, 3

C(i)=A(i)*B(i) !square A and store in C

dot= dot+C(i)

write(*,*) i, A(i),B(i),C(i)

END DO

write(*,*) "dot",dot

end program array

To Compile: gfortran array.f90

To run ./a.out


Arrays fundamentals11

Arrays fundamentals


Questions

Questions?

http://www.olcf.ornl.gov


Subroutines fortran

Subroutines Fortran

  • Recurring code

    Subroutine(parameters)

    body

    end subroutine

    Program mainpr

    call subroutine(par1)

    do something with par1

    End mainpr

subroutine square (i,isquare)

integer, intent(in) :: i

integer, intent(out) :: isquare

isquare = i**2

end subroutine square

program sq

implicit none

integer :: i,isq,icub

i = 4

call square(i,isq)

print*,"i,i^2=",i,isq,icub

end program sq


Row major c colum major fortran

Row major C Colum major FORTRAN

C

fortran


  • Login