Introduction to Fortran 90 Programming André Paul.
-
date post
20-Dec-2015 -
Category
Documents
-
view
239 -
download
0
Transcript of Introduction to Fortran 90 Programming André Paul.
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.
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
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
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)
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 ...
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 ...
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 .
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]
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.
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
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.
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.
Basic building blocks
• A main program unit:
PROGRAM hydrostatic_balance IMPLICIT NONE
! Parameter declarations
! Variable declarations
! Executable statements
END PROGRAM hydrostatic_balance
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
Repeating parts of your program
! Loop over vertical levelsDO k=1,101 ! Block of statements p = p + dpEND DO
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.
– 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
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
Parameterized REAL variables
• REAL variables (floating point numbers) are parameterized
• The kind type parameter specifies minimum precision and exponent range requirements.
! 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
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
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