Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Fortran 90 Programming André Paul.

Similar presentations


Presentation on theme: "Introduction to Fortran 90 Programming André Paul."— Presentation transcript:

1 Introduction to Fortran 90 Programming André Paul

2 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.

3 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

4 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

5 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 )

6 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...

7 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...

8 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.

9 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]

10 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.

11 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

12 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.

13 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.

14 Basic building blocks A main program unit: PROGRAM hydrostatic_balance IMPLICIT NONE ! Parameter declarations ! Variable declarations ! Executable statements END PROGRAM hydrostatic_balance

15 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 :: k REAL :: p,dp

16 Repeating parts of your program ! Loop over vertical levels DO k=1,101 ! Block of statements p = p + dp END DO

17 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.

18 –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

19 Introduction to arrays PROGRAM 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

20 Parameterized REAL variables REAL variables (floating point numbers) are parameterized The kind type parameter specifies minimum precision and exponent range requirements.

21 ! 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 type INTEGER, PARAMETER :: q=dp ! Variable declarations REAL(KIND=q) :: dpress REAL(KIND=q), DIMENSION(1:km) :: p

22 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

23 The block IF construct ! Variable declarations ! tol = criteria of convergence (K) REAL(KIND=q) :: tol [..] ! Initializations tol = 1.0E-03_q [..] ! Time loop DO itt=1,ittmax [..] ! Test for convergence IF (ABS(tf – ti) < tol) THEN EXIT END IF END DO


Download ppt "Introduction to Fortran 90 Programming André Paul."

Similar presentations


Ads by Google