Review More Arrays Modules Final Review

Similar documents
Review Functions Subroutines Flow Control Summary

AMath 483/583 Lecture 8

Introduction to Fortran Programming. -Internal subprograms (1)-

PACKAGE SPECIFICATION HSL 2013

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

Chapter 4. Fortran Arrays

Subroutines and Functions

FORTRAN 90: Functions, Modules, and Subroutines. Meteorology 227 Fall 2017

Introduction to Fortran Programming. -External subprograms-

Table 2 1. F90/95 Data Types and Pointer Attributes. Data Option. (Default Precision) Selected-Int-Kind

An Introduction to Fortran

Reusing this material

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

SUBPROGRAMS AND MODULES

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

A Brief Introduction to Fortran of 15

Chapter 6. A Brief Introduction to Fortran David A. Padua

Programming Tips for Plugins

Subroutines, Functions and Modules

Goals for This Lecture:

Fortran. (FORmula TRANslator) History

Goals for This Lecture:

6.1 Expression Evaluation. 1 of 21

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

Module 7.2: nag sym fft Symmetric Discrete Fourier Transforms. Contents

ParaFEM Coding Standard for Fortran 90. Contents. 1.0 Introduction. 2.0 Documentation. 2.1 External Documentation

7. Procedures and Structured Programming

INTRODUCTION TO FORTRAN PART II

Our Strategy for Learning Fortran 90

Lecture V: Introduction to parallel programming with Fortran coarrays

Review Arrays Advanced Input/Output New Data Types Debugging Your Programs

Introduction to Fortran95 Programming Part II. By Deniz Savas, CiCS, Shef. Univ., 2018

Allocating Storage for 1-Dimensional Arrays

Computational Techniques I

More Coarray Features. SC10 Tutorial, November 15 th 2010 Parallel Programming with Coarray Fortran

1. User-Defined Functions & Subroutines Part 1 Outline

ISO/IEC : TECHNICAL CORRIGENDUM 2

Module 28.3: nag mv rotation Rotations. Contents


Fortran Coding Standards and Style

Quick Guide to Fortran

Computational Astrophysics AS 3013

Programmation in Fortran

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

ARRAYS COMPUTER PROGRAMMING. By Zerihun Alemayehu

NAGWare f95 Recent and Future Developments

AN INTRODUCTION TO FORTRAN 90 LECTURE 2. Consider the following system of linear equations: a x + a x + a x = b

Introduction to Modern Fortran

NO CALCULATOR ALLOWED!!

1 Introduction to MATLAB

Introduction to Programming with Fortran 90

Information technology Programming languages Fortran Part 1: Base language

FORTRAN Block data subprograms 2. Common blocks 3. Entry points 4. Function subprograms 5. Main program 6. Subroutines

Types II. Hwansoo Han

Fortran 95/2003 Course

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Page 1 of 7. Date: 1998/05/31 To: WG5 From: J3/interop Subject: Interoperability syntax (Part 1) References: J3/98-132r1, J3/98-139

1 Introduction to MATLAB

Fortran 90 - A thumbnail sketch

Introduction to Modern Fortran

Module 25.2: nag correl Correlation Analysis. Contents

Chapter 3. Fortran Statements

Module 19.1: nag ip Integer Programming. Contents

Appendix D. Fortran quick reference

Co-arrays to be included in the Fortran 2008 Standard

CUDA Fortran COMPILERS &TOOLS. Porting Guide

Fortran classes and data visibility

Introduction to Modern Fortran

Modern Fortran OO Features

Module 5.5: nag sym bnd lin sys Symmetric Banded Systems of Linear Equations. Contents

Introduction to Fortran Programming. - Module -

Chapter 9 Subprograms

Module 29.1: nag tsa identify Time Series Analysis Identification. Contents

Old Questions Name: a. if b. open c. output d. write e. do f. exit

Fortran Coarrays John Reid, ISO Fortran Convener, JKR Associates and Rutherford Appleton Laboratory

2 Recommended books and a Web page

Practical Exercise 1 Question 1: The Hello World Program Write a Fortran 95 program to write out Hello World on the screen.

In context with optimizing Fortran 90 code it would be very helpful to have a selection of

Declaration and Initialization

Goals for This Lecture:

PGI Accelerator Programming Model for Fortran & C

Computational Methods of Scientific Programming. Lecturers Thomas A Herring Chris Hill

Goals for This Lecture:

Arrays. 1. Derived Data Types Outline

Lecture 5: Methods CS2301

multiple variables having the same value multiple variables having the same identifier multiple uses of the same variable

Computers in Engineering. Moving From Fortran to C Michael A. Hawker

The new features of Fortran 2003

A SHORT COURSE ON C++

Technical Specification on further interoperability with C

Chapter 8 :: Composite Types

FORTRAN - ARRAYS. For example, to declare a one-dimensional array named number, of real numbers containing 5 elements, you write,

Parallel Programming in Fortran with Coarrays

HPF commands specify which processor gets which part of the data. Concurrency is defined by HPF commands based on Fortran90

NAGWare f95 and reliable, portable programming.

PROBLEM SOLVING WITH FORTRAN 90

1. User-Defined Functions & Subroutines Part 2 Outline

1. General Computer Questions

Porting Guide. CUDA Fortran COMPILERS &TOOLS

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Transcription:

OUTLINE 1 REVIEW 2 MORE ARRAYS Using Arrays Why do we need dynamic arrays? Using Dynamic Arrays 3 MODULES Global Variables Interface Blocks Modular Programming 4 FINAL REVIEW

THE STORY SO FAR... Create arrays and new types of data Advanced input/output How to structure the flow of your program Debug your programs

MULTIDIMENSIONAL ARRAYS We can easily declare multidimensional arrays using the command REAL :: a(10,5),b(5,10) Again we access elements of the array using subscripts, now separated by a comma a(1,2) = b(1,1) + 10. a(10,:) = (/ 1,2,3,4,5 /) a(1:5,1) = (/ 1,2,3,4,5 /) We can use : to indicate all elements in a dimension or a subsection of them

THE SHAPE OF THE ARRAY Fortran has some intrinsic functions to use with arrays In order for the functions to work the arrays they must be conformal REAL :: a(3,2),b(2,3),c(2,2)! assign values to a and b a(1,1) =...! use function MATMUL c = MATMUL(a,b) The rank is the number of dimensions The shape of an array is a vector of the extent of each dimension If arrays have the same shape we can add and multiply them together

PASSING AN ARRAY TO A SUBPROGRAM We can pass an array to a subroutine or a function as an argument PROGRAM test REAL :: a(5),x,y x = my_func(a) CALL my_sub(a,x,y) The subroutine and function must know the size of the array SUBROUTINE my_sub(a,x,y) REAL, INTENT(INOUT) :: a(5),x,y a(1) =... We cannot at least in this simple way return an array from a function

As your programs become more and more complex, the time needed to compile them will increase We therefore want to specify as much as we can at run time So far we can only change the size of an array in the code Dynamic array allocation allows the size of arrays to be set at runtime

We declare a array without size in the following way REAL, ALLOCATABLE :: a(:),two_dim_a(:,:) Then to specify the size of the array at runtime we write PRINT *, Input size of a and two_dim_a READ *,n,m,p ALLOCATE( a(n),two_dim_a(m,p),stat=error ) and deallocate or delete the memory space with the statement DEALLOCATE(a,two_dim_a)

If we pass a dynamic array to a subprogram we must tell it about the size of the array CALL my_sub(a,n) and inside the subroutine SUBROUTINE my_sub(a,n) INTEGER, INTENT(IN) ::! declare n before a REAL, INTENT(INOUT) :: DO i = 1,n a(i) =... END DO n a(n)

DATA FOR ALL TO SEE We often wish to allow lots of our subprograms to see the same piece of data This could be: The number of points used in a finite difference scheme The precision of the data variables A mathematical constant such as π To include a module (and let the program have access to the variables) - we simple write the following statement at the top of each program USE module_name

EXAMPLE MODULE MODULE global_data! double precision real number parameter INTEGER, PARAMETER :: DP = KIND(1.0D0)! mathematical constant pi REAL(DP), PARAMETER :: & PI_D=3.14159265358979323846264338327_dp! number of points in finite difference INTEGER :: no_of_points END MODULE global_data We can then set real numbers to double precision with REAL(dp) :: x,y,z

CONNECTING PROGRAMS We use interface blocks to tell the compiler how to connect together functions An interface can allow us to return an array from a function or assume the shape of an array on entry to a function We can also use it to overload functions See Fortran 90 Programming by Ellis, Philips and Lahey for more information on overloading

EXAMPLE - FUNCTION USING ASSUMED ARRAY SHAPE MAIN PROGRAM PROGRAM test! interface at the top INTERFACE FUNCTION add_all_elements(a) IMPLICIT NONE REAL, INTENT(IN) :: a(:) REAL :: add_all_elements END FUNCTION END INTERFACE! create allocatable array with n elements REAL :: a(:)... ALLOCATE(a(n)) PRINT *,add_all_elements(a) END PROGRAM test

EXAMPLE - FUNCTION USING ASSUMED ARRAY SHAPE FUNCTION FUNCTION add_all_elements(a) IMPLICIT NONE REAL, INTENT(IN) :: a(:) REAL :: add_all_elements INTEGER :: n,i n=size(a) add_all_elements = 0. DO i=1,n add_all_elements = add_all_elements + a(i) END DO END FUNCTION

EASIER CONNECTING If we put the function in previous example inside a module, the module will create the interface for us We use the keyword CONTAINS to put functions or subroutines into a module MODULE MODULE array_ops CONTAINS FUNCTION add_all_elements(a) IMPLICIT NONE REAL, INTENT(IN) :: a(:)... END FUNCTION END MODULE

MORE THAN JUST DATA Sometimes we may wish to connect functions or subroutines to a data type For example there are many functions we could associate with the data type point... such as the distance function so we can put the function into the module along with the data type when each data type is called it will have functions associated with it

WHAT YOU CAN DO NOW... Able to produce, compile and run a fortran program Use subroutines and functions to structure your program Control the flow of your program Read and write from files and control the way data is formatted Use simple modules to declare global variables