Introduction to Fortran

Similar documents
2. Basic Elements of Fortran

1 Week 1: Basics of scientific programming I

Welcome. Modern Fortran (F77 to F90 and beyond) Virtual tutorial starts at BST

Our Strategy for Learning Fortran 90

UNIT- 3 Introduction to C++

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Data Types. 9. Types. a collection of values and the definition of one or more operations that can be performed on those values

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

Data Types and Basic Calculation

2 3. Syllabus Time Event 9:00{10:00 morning lecture 10:00{10:30 morning break 10:30{12:30 morning practical session 12:30{1:30 lunch break 1:30{2:00 a

Goals for This Lecture:

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Visual C# Instructor s Manual Table of Contents

PROBLEM SOLVING WITH FORTRAN 90

Scientific Programming in C X. More features & Fortran interface

Definition: Data Type A data type is a collection of values and the definition of one or more operations on those values.

CSE 341 Section Handout #6 Cheat Sheet

C++ Programming: From Problem Analysis to Program Design, Third Edition

ANSI C Programming Simple Programs

Summary of basic C++-commands

Types, Operators and Expressions

Chapter 2. Outline. Simple C++ Programs

Types, Operators and Expressions

Chapter 4: Basic C Operators

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2. MATLAB Basis

Message-Passing Programming with MPI. Message-Passing Concepts

Fortran Coding Standards and Style

Reusing this material

Syntax and Variables

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Type Conversion. and. Statements

Introduction to Modern Fortran

Outline. Data and Operations. Data Types. Integral Types

Digital Image Analysis and Processing CPE

Introduction to Engineering gii

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments

Lab 1 - Worksheet Spring 2013

Contract-based Programming: a Route to Finding Bugs Earlier

CS313D: ADVANCED PROGRAMMING LANGUAGE

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Java Notes. 10th ICSE. Saravanan Ganesh

Getting started with C++ (Part 2)

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CS112 Lecture: Primitive Types, Operators, Strings

Fortran. (FORmula TRANslator) History

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

Learning Language. Reference Manual. George Liao (gkl2104) Joseanibal Colon Ramos (jc2373) Stephen Robinson (sar2120) Huabiao Xu(hx2104)

File Handling in C. EECS 2031 Fall October 27, 2014

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

EEE145 Computer Programming

Decaf Language Reference Manual

CSI33 Data Structures

Introduction to Programming Using Java (98-388)

DEPARTMENT OF MATHS, MJ COLLEGE

History. used in early Mac development notable systems in Pascal Skype TeX embedded systems

Excel Functions & Tables

Evolution of Fortran. Presented by: Tauqeer Ahmad. Seminar on Languages for Scientific Computing

Introduction to Fortran 90

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY

Lecture 2 FORTRAN Basics. Lubna Ahmed

BASIC ELEMENTS OF A COMPUTER PROGRAM

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

MATLAB QUICK START TUTORIAL

High Performance Computing

1007 Imperative Programming Part II

2 Making Decisions. Store the value 3 in memory location y

CITS2401 Computer Analysis & Visualisation

RTL Reference 1. JVM. 2. Lexical Conventions

Expr Language Reference

Introduction to Programming

Engineering Problem Solving with C++, Etter/Ingber

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Program Structure and Format

Starting with a great calculator... Variables. Comments. Topic 5: Introduction to Programming in Matlab CSSE, UWA

C++ Programming Lecture 11 Functions Part I

Introduction. What is function? Multiple functions form a larger program Modular programming

Source: Fortran 90 3 Day Course (Univ. of Liverpool) URL: Ngo Van Thanh, NIMS Oct. 4, 2010

The SPL Programming Language Reference Manual

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Introduction to C++ Dr Alex Martin Room 6.10

ME1107 Computing Y Yan.

Methods CSC 121 Spring 2017 Howard Rosenthal

1 Lexical Considerations

PROGRAMMING IN C AND C++:

Review More Arrays Modules Final Review

Introduce C# as Object Oriented programming language. Explain, tokens,

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Introduction to C++ Dr M.S. Colclough, research fellows, pgtas

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Numerical Analysis First Term Dr. Selcuk CANKURT

Intrinsic Numeric Operations

INTRODUCTION 1 AND REVIEW

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

Methods CSC 121 Fall 2016 Howard Rosenthal

Transcription:

Introduction to Fortran

Reusing this material This work is licensed under a Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International License. http://creativecommons.org/licenses/by-nc-sa/4.0/deed.en_us This means you are free to copy and redistribute the material and adapt and build on the material under the following terms: You must give appropriate credit, provide a link to the license and indicate if changes were made. If you adapt or build on the material you must distribute your work under the same license as the original. Note that this presentation may contain images owned by others. Please seek their permission before reusing these images.

Language evolution Ancient History Name comes from FORmula TRANslation Fortran 66 was the first language to have a standard (1967) Fortran 77 New standard to overcome divergence in different implementations (1978) Fortran 90 Major revision added modules, derived data types, dynamic memory allocation, intrinsics Retained backward compatibility Fortran 95 Minor revision but added several HPC related features; forall, where, pure, elemental, pointers Fortran 2003 Major revision with many new features including; OO capabilities, procedure pointers, IEEE arithmetic, C interoperability Fortran 2008 Minor change: added co-arrays and sub modules

Primarily a procedural language program hello variable declarations program text function calls function definitions end program hello

Software engineering Fortran 90 introduced new features Structured, sane, safe programming! Modules Provide excellent possibilities for encapsulation Provide interfaces for subroutines (argument type-checking) Provide structure Portability Concept of type for data objects Opens the way to obtaining portable behaviour, particularly for floating point arithmetic Subsequent incarnations (95, 2003, 2008) have built on this Result is a modern language that is very good for HPC applications

Hello World The canonical introductory program program hello! Display a message to standard output (usually the screen) implicit none write (unit = *, fmt = *) Hello World! end program hello Basic syntax is based on lines Statements occupy lines of up to 132 characters Case insensitive (c.f. C, C++, Java) Comments are introduced with an exclamation mark! You will see many variations in style

Main program and syntax Formally main program [program program-name] [specification-statements] [executable-statements] end [program [program-name]] Text inside square brackets [] is optional Long lines can be split using continuation & write (unit = *, fmt = *) & Long and somewhat convoluted Hello World line! Multiple statements on a single line Can be split using a semi-colon ; Not recommended for readability use one statement per line

Variables Intrinsic data types are declared implicit none! Enforce strong typing integer :: i! 10 real :: a! 3.14159 character :: letter! a character (len = 12) :: month! January logical :: switch!.false. complex :: z0, z1! (1.0, 1.0) Variables Must be declared before any executable statements Have an acceptable name made up of alphanumeric characters (or underscores _) of which the first character must be a letter Acceptable: a1, a_letter, a123b Not acceptable: 1abc, quid$in

Implicit None Undeclared variables always have an implicit type If the first letter begins with an i, j, k, l, n, m type is integer If the first letter begins with any other letter type is real Implicit typing is very dangerous and should always be turned off using implicit none Consider the following real :: l1 = 1.2345 write(*,*) The value of l1 =, ll The variable ll is implicitly assumed to be of integer type The compiler will not complain Using implicit none would catch this typographical error Can be very difficult to debug

Variable initialisation Variables can be initialised either at point of declaration program initial_declare implicit none integer :: i = 10 real :: pi = 3.14159 character (len = 12) :: month = January end program initial_declare Or within the main program complex :: ci logical :: iostatus ci = (0.0, 1.0) iostatus =.true. Beware: initialising arrays at declaration can result in very large executable sizes (intialised at compile time)

Arrays Arrays hold a collection of values at the same time Elements are accessed by subscripting the array A 10 element 1D array can be visualised as: 1 2 3 4 5 6 7 8 9 10 A 4x2, 2D array can be visualised as: Dimension 2 1,1 1,2 Dimension 1 2,1 2,2 3,1 3,2 4,1 4,2 In Fortran arrays are stored in memory by columns known as column major (C, C++, Java all store by row)

Arrays Arrays are declared with dimension attribute implicit none integer, dimension(4) :: n4 Provides 4 elements Elements: n4(1), n4(2), n4(3), n4(4) First element is, by default, 1 Can set the lower and upper bounds real, dimension(-5:4) :: r Elements: r(-5), r(-4), r(0), r(4) Total number of elements in the array is the size Here n4 has size = 4 and r has size = 10

Multidimensional arrays Arrays can have more than one dimension complex, dimension(1:10, 1:20) :: z Terminology Number of dimensions is the rank (here 2) Number of elements in given dimension is the extent Sequence of the extents is the shape, here (10, 20) Up to 7 dimensions are allowed real, dimension(2, 3, 4, 5, 6, 1) :: vast Has six dimensions (i.e., rank 6) Extent in the fourth dimension is 5 Shape is (2, 3, 4, 5, 6, 1) Size is 2x3x4x5x6x1 = 720 elements

More on character variables Declared in similar way to numeric types Character variables can Refer to a single character Refer to a string (achieved by adding a length specifier) The following are all valid declarations character :: sex character (len = 20) :: name character (len = 10), dimension(10,10) :: carray Assigned using either double or single quotes sex = f name = Joe Bloggs

Parameter attribute Named constants may be defined and used integer, parameter :: n = 100 real, dimension(2*n) :: r real, parameter :: pi = 3.14 Values set at compile time must not change Constant expressions involving parameters are evaluated at compile time Attempt to assign a new value will give a compiler error Any intrinsic type may have the parameter attribute, including arrays The general declaration is type [, attributes] :: variable

Types Floating point variables Variables declared real are of default precision Standard does not specify what this is (but usually 4 bytes) Mechanism for ensuring get desired type E.g., by specifying the range or decimal precision required Uses the kind type parameter (processor dependent) integer, parameter :: sp = kind(1.0) real (kind = sp), dimension(10) :: variable Extended precision (double) integer, parameter :: dp = kind(1.0d0) real (kind = dp) :: variable

Numerical expressions Arithmetic operators are **! exponentiation *! multiplication /! division +! addition -! subtraction decreasing order of precedence Otherwise expressions evaluated left-to-right e.g., a*b*c evaluated as (a*b)*c Except a**b**c evaluated as a**(b**c) Care! Integer division rounded toward zero e.g., (2*4)/5 gives 1 but 2*(4/5) gives 0 Type promotion during arithmetic Promotes to higher type, e.g. integer * real = real

Mixed assignments Promotion during arithmetic (+ - * /) Expression a operator b is evaluated as type of a type of b type of result integer integer integer integer real real integer complex complex real real real real complex complex complex complex complex Explicit conversions are also possible Intrinsic functions int(), real(), cmplx() e.g., z = cmplx(r1,r2), where r1 and r2 are variables of type real containing the real and imaginary parts of the complex number respectively

Intrinsic functions Over 100 intrinsic functions in Fortran 2008 array operations, bit manipulations, character strings check whether there s an intrinsic available (List of intrinsic functions in Metcalf and Reid or the Standard) Conversion int() real() cmplx() abs() nint() aint() aimag() ceiling() floor() Mathematical sqrt(x) exp(x) log(x) log10(x) sin(x) cos(x) tan(x) asin(x) acos(x) atan(x) sinh(x) cosh(x) tanh(x) Others min(x1, x2,...) max(x1, x2,...) mod(a, p) conjg() tiny(x) huge(x)

Relational operators These are <! less than <=! less than or equal >! greater than >=! greater than or equal ==! equal /=! not equal Logical expressions are then, e.g., a < b char1 == a a+b >= c+d For integer and real numeric types Not complex

Logical operators Logical variables take on one of two values.true..false. Relational operators are.not.! unary not.and.! logical and.or.! logical or.eqv.! equivalent.neqv.! not equivalent Decreasing order of precedence e.g., i.or. j.and..not. k evaluated as i.or. (j.and. (.not. k))

Conditionals Very similar to other languages if (logical-expression) then block [else if (logical-expression) then block]... [else block] end if May be nested but not interleaved Also a select case statement (cf switch in Java)

Select case Select case provides an alternative to a series of repeated if...then...else if statements The general form of the case construct is [name:] select case (expression) [case selector [name] block]... [case default block] end select [name] Where expression can be any of A single integer, character, or logical depending on type min: any value from a minimum value upwards :max any value from a maximum value downwards min : : max any value between the two limits

Loops Bounded iteration do n = 1, 100! do something end do Formally do [variable = expr1, expr2[, expr3]] block end do where expr1, expr2, and expr3 are integers number of iterations will be max(0, (expr2-expr1+expr3)/expr3) Arbitrary stride is allowed (including negative stride) do n = 10, 1, -2! do something end do

Controlling loops Unbounded loop do! go around for ever end do Can be terminated with exit do! do some computation if (condition) exit! do something else end do! exits from current loop Can also go to next iteration using cycle

Simple I/O The print statement is the simplest form of directing unformatted data to the standard output print*, The temperature is,temperature, Each print statement begins on a new line degrees Print statement can transfer any object of intrinsic type to standard output Strings are delimited by either double or single quotes Two occurrences of string delimiter produce one occurrence in the output, e.g. print*, Fred says Hello! print only allows access to standard output screen write() is much more useful as it can also handle files

Simple I/O write statement Use write() statement write ([unit =] unit, & [fmt =] format_string...) [list] can take default write (*,*) i.e., standard output and free format To write to an external file open (unit = 20, file = file.dat, & form = formatted, action = write ) write (unit = 20, fmt = *) [list] close (unit = 20, status = keep ) Input is via read() e.g. read(*,*)temperature to read the value of temperature from the keyboard

Summary Fortran is an evolving language Now has many powerful features Natural language for scientific / engineering problems Hence commonly found in HPC applications Vast amount of legacy code Generally a procedural language

Exercise Basic Fortran exercises Logging on to ARCHER Course material at: http://tinyurl.com/archer270218 Password: 5bI8LtOIVKtU CFD Practical Get the source: wget http://tinyurl.com/archer270218/exercises/cfd.tar.gz Writing some basic Fortran programs Starting the percolate practical

Conditionals (example) For example if (t < 0) then! It s cold ice =.true. else if (t > 100) then else! It s hot steam =.true. water =.true. wet =.true. washout =.true. end if

Select case (example) General form of selector is a list of non-overlapping values/ ranges of the same type as expression Values of expression not included in selector can be caught by case default, e.g. seasons: select case (month) case (1:2,12) write(*,*) It is winter case(3:5) write(*,*) It is spring case(6:8) write(*,*) It is summer case(9:11)! month is of type integer! Winter, Dec, Jan, Feb! Spring, Mar, Apr, May! Summer, Jun, Jul, Aug! Autumn, Sep, Oct, Nov write(*,*) It is autumn case default! if month outside 1-12 write(*,*)"must enter 1-12" end select seasons

Controlling iteration (example) mainloop: do write(*,*) Input student id read(*,*)stid if (stid == 0) exit mainloop average = 0 innerloop: do i = 1, 5 write(*,*) Please enter mark read(*,*)mark if (mark < 0) then write(*,*) Mark < 0, start again cycle mainloop end if average = average + mark end do innerloop average = average/5.0 write(*,*) Average of student,stid, end do mainloop is =,average

Simple I/O write statement Can use write and read statements to access standard input (i.e. screen and keyboard) write(*,*) This text will appear on the screen write(*,*) Input temperature (C) read(*,*)temperature! Reads value input via! the keyboard and assigns! to variable temperature Multiple values can be read in from a single line write(*,*) Input 3 results read(*,*)result1,result2,result3

Simple I/O unknown file length To continue reading values from an external file until the end of the file is reached integer :: i, icount = 0 integer, parameter :: maxln=500 real, dimension(maxln) :: a open(unit=10, file= temps.dat, status= old, action= read ) do i = 1, maxln read(10,*,end=100)a(i) icount = icount + 1 end do 100 continue! 100 is a label close(10) write(*,*) No. of lines read in from file =,icount...