introduction to fortran 90 programming andré paul

23
Introduction to Fortran 90 Programming André Paul

Post on 20-Dec-2015

248 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Fortran 90 Programming André Paul

Introduction to Fortran 90 Programming

André Paul

Page 2: Introduction to Fortran 90 Programming André Paul

Reading

• The following notes are based on

– Ellis, T.M.R., Phillips, Ivor R., and Lahey,

Thomas M.: Fortran 90 Programming,

Addison-Wesley Publishing Company.

Page 3: Introduction to Fortran 90 Programming André Paul

Telling a Computer What To Do

• To get a computer to perform a specific task it must be given a sequence of unambiguous instructions or a program.

• Generating a program:

1. Text file with instructions (Source Code; Fortran 90)

2. Translation into computer-compatible form (Compiler)

3. Executable Program

Page 4: Introduction to Fortran 90 Programming André Paul

The Linux look and feel

• Cygwin is a Linux-like environment for Windows that consists of two parts:

– A DLL (“Dynamic Link Library”, cygwin1.dll)

– A collection of tools, which provide Linux look and feel

• Link: http://www.cygwin.com

Page 5: Introduction to Fortran 90 Programming André Paul

A few useful Linux commands

• pwd – print name of current/working directory

• ls – list directory contents

• cd – change directory

• mkdir – make directories

• rm – remove files or diretories

• man – format and display the on-line manual pages (e.g. man pwd)

Page 6: Introduction to Fortran 90 Programming André Paul

How to start cygwin

• Open cygwin window by double-clicking on the start_cygwin DOS batchfile provided with this exercise

• If this does not work ...

Page 7: Introduction to Fortran 90 Programming André Paul

How to start cygwin II

• Double-click on the cygwin icon on the desktop

• In the computer room, try typing cd "$ceida"

• If this does not work ...

Page 8: Introduction to Fortran 90 Programming André Paul

How to get to your directory

• Copy the file /dozent/public/Austausch/anma/.profile to your cygwin home directory:

• Then type cd "$ceida"

• If this does not work ...

cp /dozent/public/Austausch/anma/.profile .

Page 9: Introduction to Fortran 90 Programming André Paul

How to get to your directory II

• Try it the long way: – cd c:

– cd Dokumente und Einstellungen

– cd [put your username here]

– cd Eigene Dateien

– cd [put your working directory here]

Page 10: Introduction to Fortran 90 Programming André Paul

Creating and Running a Program

• Invoke the editor from the Start menu, or type

notepad hello.f90 &

into the cygwin window to create a file called hello.f90.

Page 11: Introduction to Fortran 90 Programming André Paul

Creating and Running a Program

• Use the editor to create a file called hello.f90, which contains the following source code:

• Compile by typing g95 hello.f90 (in the cygwin window)

• Run the program by typing ./a.exe (g95 by default creates an executable called a.exe)

PROGRAM firsttry

PRINT *,“hello“

END PROGRAM firsttry

Page 12: Introduction to Fortran 90 Programming André Paul

Basic Fortran 90 concepts

• All words which have a special meaning in Fortran are known as keywords.

• Every main program unit must start with a PROGRAM statement which consists of the word PROGRAM followed by the name of the program as given by the programmer.

Page 13: Introduction to Fortran 90 Programming André Paul

Avoid implicit declaration

• The special IMPLICIT NONE statement should always be placed immediately after the PROGRAM statement. It instructs the compiler that all variables must be declared before use.

Page 14: Introduction to Fortran 90 Programming André Paul

Basic building blocks

• A main program unit:

PROGRAM hydrostatic_balance IMPLICIT NONE

! Parameter declarations

! Variable declarations

! Executable statements

END PROGRAM hydrostatic_balance

Page 15: Introduction to Fortran 90 Programming André Paul

REAL and INTEGER variables

• Use the INTEGER :: name statement to declare a whole-number variable

• Use the REAL :: name statement to declare a floating-point variable

! Variable declarations! k = loop variable! p = pressure (Pa)! dp = pressure change (Pa)INTEGER :: kREAL :: p,dp

Page 16: Introduction to Fortran 90 Programming André Paul

Repeating parts of your program

! Loop over vertical levelsDO k=1,101 ! Block of statements p = p + dpEND DO

Page 17: Introduction to Fortran 90 Programming André Paul

Using files to preserve data

• Connecting external files to your program

– Connect a file to a unit by

OPEN(UNIT=unit_number,FILE=file_na

me), where unit_number is an integer

number, variable or parameter and

file_name is a character expression.

Page 18: Introduction to Fortran 90 Programming André Paul

– Write a record to a file by WRITE

(UNIT=unit_number,FMT=*)

– Disconnect a file from a unit by means of a

CLOSE (UNIT=unit_number) statement

Page 19: Introduction to Fortran 90 Programming André Paul

Introduction to arraysPROGRAM hydrostatic_balance IMPLICIT NONE

! Parameter declarations INTEGER, PARAMETER :: km=101

! Variable declarations INTEGER :: k REAL :: dp REAL, DIMENSION(1:km) :: p

! Executable statements dp = 0.1 p(1) = 0.0 DO k=1,km-1 p(k+1) = p(k) + dp END DO

END PROGRAM hydrostatic_balance

Page 20: Introduction to Fortran 90 Programming André Paul

Parameterized REAL variables

• REAL variables (floating point numbers) are parameterized

• The kind type parameter specifies minimum precision and exponent range requirements.

Page 21: Introduction to Fortran 90 Programming André Paul

! Parameter declarations

! Symbolic name for a real kind type with at least ! 15 decimal digits of precision and an exponent range ! from 10**300 to 10**(-300) (“double precision”)INTEGER, PARAMETER :: dp=SELECTED_REAL_KIND(P=15,R=300)

! Symbolic name for a real kind type with at least ! 6 decimal digits of precision and an exponent range! from 10**30 to 10**(-30) (“single precision”)INTEGER, PARAMETER :: sp=SELECTED_REAL_KIND(P=6,R=30)

! Symbolic name for a default real kind typeINTEGER, PARAMETER :: q=dp

! Variable declarationsREAL(KIND=q) :: dpressREAL(KIND=q), DIMENSION(1:km) :: p

Page 22: Introduction to Fortran 90 Programming André Paul

Functions and subroutines REAL(KIND=q) FUNCTION feuler(y,s,dx) IMPLICIT NONE !----------------------------------------------------------------- ! This function integrates one time step using the forward Euler ! method for the ODE: dy/dx = f(x,y(x)) ! ! Input arguments: ! y = temperature (K) ! s = solar radiation (W m^(-2)) ! dx = time step (s) ! ! Result variable: ! feuler = dy ! ! Uses external function: rhs ! ! Based on code by M. Yoshimori !-----------------------------------------------------------------

REAL(KIND=q), INTENT(IN) :: y,s,dx

feuler = rhs(y,s)*dx

END FUNCTION feuler

Page 23: Introduction to Fortran 90 Programming André Paul

The block IF construct

! Variable declarations! tol = criteria of convergence (K)REAL(KIND=q) :: tol[..]! Initializations tol = 1.0E-03_q[..]! Time loopDO itt=1,ittmax

[..]

! Test for convergence IF (ABS(tf – ti) < tol) THEN EXIT END IFEND DO