Chapter 7: Additional Topics



Similar documents
Simple File Input & Output

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Glossary of Object Oriented Terms

Data Structures. Algorithm Performance and Big O Analysis

WESTMORELAND COUNTY PUBLIC SCHOOLS Integrated Instructional Pacing Guide and Checklist Computer Math

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Section IV.1: Recursive Algorithms and Recursion Trees

New Tools Using the Hardware Pertbrmaiihe... Monitor to Help Users Tune Programs on the Cray X-MP*

One Dimension Array: Declaring a fixed-array, if array-name is the name of an array

Information technology Programming languages Fortran Enhanced data type facilities

MATLAB Tutorial. Chapter 6. Writing and calling functions

Base Conversion written by Cathy Saxton

For another way to generate recursive sequences, see Calculator Note 1D.

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

ML for the Working Programmer

Positional Numbering System

Sources: On the Web: Slides will be available on:

AMATH 352 Lecture 3 MATLAB Tutorial Starting MATLAB Entering Variables

Decimal form. Fw.d Exponential form Ew.d Ew.dEe Scientific form ESw.d ESw.dEe Engineering form ENw.d ENw.dEe Lw. Horizontal

Programming Languages

Programming Languages & Tools

I PUC - Computer Science. Practical s Syllabus. Contents

Moving from CS 61A Scheme to CS 61B Java

Binary Division. Decimal Division. Hardware for Binary Division. Simple 16-bit Divider Circuit

Storing Measurement Data

This unit will lay the groundwork for later units where the students will extend this knowledge to quadratic and exponential functions.

AP Computer Science Java Mr. Clausen Program 9A, 9B

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

Number Conversions Dr. Sarita Agarwal (Acharya Narendra Dev College,University of Delhi)

COMPUTER SCIENCE. Paper 1 (THEORY)

but never to show their absence Edsger W. Dijkstra

Database Programming with PL/SQL: Learning Objectives

Algebra 2 Notes AII.7 Functions: Review, Domain/Range. Function: Domain: Range:

Athena Knowledge Base

Excel & Visual Basic for Applications (VBA)

Symbol Tables. Introduction

Integers are positive and negative whole numbers, that is they are; {... 3, 2, 1,0,1,2,3...}. The dots mean they continue in that pattern.

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Figure 1: Graphical example of a mergesort 1.

HSL and its out-of-core solver

1 Description of The Simpletron

Sybex CCENT Chapter 4: Easy Subnetting. Instructor & Todd Lammle

A Fortran 2003 introduction by examples

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

Informatica e Sistemi in Tempo Reale

Excel Level Two. Introduction. Contents. Exploring Formulas. Entering Formulas

9.4. The Scalar Product. Introduction. Prerequisites. Learning Style. Learning Outcomes

Section V.3: Dot Product

Package png. February 20, 2015

Numbering Systems. InThisAppendix...

Programming in C# with Microsoft Visual Studio 2010

C++ INTERVIEW QUESTIONS

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

Secrets of printf. 1 Background. 2 Simple Printing. Professor Don Colton. Brigham Young University Hawaii. 2.1 Naturally Special Characters

Figure 1. A typical Laboratory Thermometer graduated in C.

THE NAS KERNEL BENCHMARK PROGRAM

Application Unit, MDRC AB/S 1.1, GH Q R0111

MACHINE INSTRUCTIONS AND PROGRAMS

PGR Computing Programming Skills

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Using Casio Graphics Calculators

CSC4510 AUTOMATA 2.1 Finite Automata: Examples and D efinitions Definitions

INDUSTRIAL AUTOMATION Interactive Graphical SCADA System INSIGHT AND OVERVIEW. IGSS Online Training. Exercise 8: Creating Templates

The C Programming Language course syllabus associate level

Multiplying and Dividing Signed Numbers. Finding the Product of Two Signed Numbers. (a) (3)( 4) ( 4) ( 4) ( 4) 12 (b) (4)( 5) ( 5) ( 5) ( 5) ( 5) 20

2) Write in detail the issues in the design of code generator.

Chapter 07: Instruction Level Parallelism VLIW, Vector, Array and Multithreaded Processors. Lesson 05: Array Processors

CSE Case Study: Optimising the CFD code DG-DES

(!' ) "' # "*# "!(!' +,

5.1 Radical Notation and Rational Exponents

Fortran 95, Exercises Introductory course, 8-9 Oct 2009

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

6.1. The Exponential Function. Introduction. Prerequisites. Learning Outcomes. Learning Style

The continuous and discrete Fourier transforms

grep, awk and sed three VERY useful command-line utilities Matt Probert, Uni of York grep = global regular expression print

ASSEMBLY PROGRAMMING ON A VIRTUAL COMPUTER

Module 2 Stacks and Queues: Abstract Data Types

VB.NET Programming Fundamentals

Excel & Visual Basic for Applications (VBA)

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Pseudo code Tutorial and Exercises Teacher s Version

Array Abstract Data Type

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Sequential Program Execution

Rational Exponents. Squaring both sides of the equation yields. and to be consistent, we must have

Section 1.5 Exponents, Square Roots, and the Order of Operations

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

Assignment 2: Option Pricing and the Black-Scholes formula The University of British Columbia Science One CS Instructor: Michael Gelbart

Chapter 6 Quantum Computing Based Software Testing Strategy (QCSTS)

Linear functions Increasing Linear Functions. Decreasing Linear Functions

Python Programming: An Introduction to Computer Science

Computational Mathematics with Python

HTML Code Generator V 1.0 For Simatic IT Modules CP IT, IT, IT

BCS2B02: OOP Concepts and Data Structures Using C++

Transcription:

Chapter 7: Additional Topics In this chapter we ll briefly cover selected advanced topics in fortran programming. All the topics come in handy to add extra functionality to programs, but the feature you ll use most often is dynamic arrays, i.e., an array the size of which can be redefined at execution time. 7.1 Overloading Operators We ve seen a few examples of overloading intrinsic assignment operators, like the = operator. However we can also define our own operators using interfaces. For example, we can create a dot product operator: INTERFACE OPERATOR(.DOT.) Operator syntax: u.dot.v MODULE PROCEDURE dotproduct END INTERFACE Dot Product Operator REAL(KIND=RP) FUNCTION dotproduct(vec1,vec2) RESULT(dot) REAL(KIND=RP),INTENT(IN) :: vec1(:),vec2(:) IF (SIZE(vec1).EQ.SIZE(vec2)) THEN dot = DOT PRODUCT(vec1,vec2) dot = 0.0 RP PRINT*, ERROR: Vector size mismatch END FUNCTION dotproduct 7.2 Dynamic Arrays When we dealt with arrays in Chap. 5 we made the implicit assumption that we already knew how large we wanted to make the array. We can remove such an assumption if we add the ALLOCATABLE attribute to a variable declaration. This attribute will tell the compiler to reserve a chunk of memory for possible use during execution. It also gives us the freedom to resize arrays without recompiling. For example, if we want a three dimensional ALLOCATABLE array we use a : as a placeholder for the dimension size, e.g., REAL(KIND=RP),ALLOCATABLE,DIMENSION(:,:,:) :: array1 To make the reserved memory available for use we use the ALLOCATE command. Always remember that if a PROGRAM, FUNCTION, or SUBROUTINE contains an ALLOCATE command it should have a corresponding DEALLOCATE command to release the memory. This will help prevent memory leaks, which occur when a program mismanages memory. Memory leaks can slow down performance and can even exhaust available system memory (which leads to segmentation faults). Fortran does offer some built-in error checking when allocating memory: ALLOCATE(var name(lowerbound:upperbound),stat=ierr) where the STAT option returns an type. If ierr 0, then there was an error allocating the memory for var name. We also show a quick example of reallocating memory during a program s execution PROGRAM AllocateExample,ALLOCATABLE,DIMENSION(:) AllocateExample.f90 :: array1 1

,ALLOCATABLE,DIMENSION(:,:) :: array2 ALLOCATE(array1(-2:8),array2(-1:2,0:10)) PRINT*,LBOUND(array1),UBOUND(array1) PRINT*,SHAPE(array2) DEALLOCATE(array1,array2) ALLOCATE(array1(0:100),array2(1:5,-5:5)) PRINT*,LBOUND(array1),UBOUND(array1) PRINT*,SHAPE(array2) DEALLOCATE(array1,array2) END PROGRAM AllocateExample 7.3 Optional Arguments When we write a FUNCTION or SUBROUTINE sometimes an argument may not need to be present at all times. For example, in a function that prints a matrix we can write to a file, but default to the terminal if no file name is provided. In this way we can make the file name an OPTIONAL argument. The procedure can then check if the argument is PRESENT (returns a LOGICAL) and operate accordingly. OptionalPrint.f90 SUBROUTINE PrintMatrix(mat,fileName,N),INTENT(IN) :: N REAL(KIND=RP),DIMENSION(N,N),INTENT(IN) :: mat CHARACTER(LEN=*),OPTIONAL,INTENT(IN) :: filename Local Variables :: i,fileunit OPEN(UNIT=fileUnit,FILE=fileName) fileunit = 6 6 is the terminal screen DO i = 1,N WRITE(fileUint,*)mat(i,:) CLOSE(fileUnit) END SUBROUTINE PrintMatrix NOTE: If we put the above subroutine in its own file, it MUST be contained in a module. 7.4 Advanced Input/Output Options We know how to read in or display data to the terminal or to a file, but there are some useful I/O options we glossed over. Next we ll explore some of the advanced options available to us when inputting and outputting information. 7.4.1 Non-Advancing I/O First is the ADVANCE option in READ or WRITE statements. Effectively, this will tell the READ/WRITE statement whether or not to advance to the next line. By default ADVANCE is set to YES. For example, Advance NO READ(fileUnit,ADVANCE= NO )var1,var2,var3,var4 2

will read in four variables from the file pointed to by fileunit where all variables are on the same line. However, if we read in using READ(fileUnit,*)var1,var2,var3,var4 Advance YES reads in four variables from the file pointed to by fileunit where each variables is on its own line, i.e., separated by a carriage return. 7.4.2 Formatted File I/O Sometimes unformatted file reading and writing is insufficient. For example, fortran can create and read binary files, which require special arguments. To format data for reading/writing fortran uses a notation like A4, which means a character of length 4. Or in general, a variable type and the length of the data to be written. The most common variable outputs are A characters/strings I integer F real number, decimal form E real number, exponential form ES real number, scientific notation EN real number, engineering notation For real number outputs we specify the width of the number with a # symbol as well as the number of digits to appear after a decimal points, e.g., F10.6 is a real number with 10 digits, 6 of which appear after the decimal. We can combine several different formatting parameters by replacing the * option with (parameters) as we do in the next example FormatPrint.f90 SUBROUTINE PrintMatrix Formatted(mat,fileName,N),INTENT(IN) :: N REAL(KIND=RP),DIMENSION(N,N),INTENT(IN) :: mat CHARACTER(LEN=*),OPTIONAL,INTENT(IN) :: filename Local Variables :: i,j,funit OPEN(UNIT=fUnit,FILE=fileName) funit = 6 6 is the terminal screen DO i = 1,N DO j = 1,N WRITE(fUint, (A2,I3,A2,I3,A2,F10.6,A1),ADVANCE= NO ) A[,i, ][,j, ]= mat(i,j), WRITE(fUint, (A2,I3,A2,I3,A2,F10.6) ) A[,i, ][,j, ]= mat(i,j) CLOSE(fUnit) END SUBROUTINE PrintMatrix Formatted 3

7.5 Recursive Procedures in Fortran Many mathematical formulae lend themselves to a recursive formulation, like the Fast Fourier Transform (FFT). But, as we mentioned in Chap. 4, normally a FUNCTION or SUBROUTINE cannot reference itself, directly or indirectly. However, if we invoke that the procedure is RECURSIVE self-reference is possible. 7.5.1 Recursive Functions We start with a canonical example of a recursive function to compute the factorial, n, for some integer n. This example also introduces a SELECT CASE, which is a common alternative to IF statements. Recursive Function RECURSIVE FUNCTION factorial(n) RESULT(factorial n),intent(in) :: n Determine if recursion is required SELECT CASE(n) CASE (0) Recursion reached the end factorial n = 1.0 RP CASE (1:) any integer above 0 Recursion call(s) required factorial n = n*factorial(n-1) CASE DEFAULT If n is negative, return error PRINT*, ERROR: n is negative factorial n = 0.0 RP END SELECT END FUNCTION factorial 7.5.2 Recursive Subroutines We can also create recursive subroutines. Another example arises in the bisection method, where one recursively halves an interval based on the function f(x) to locate the root of a function. We also enable the termination of the subroutine once we reach a certain number of iterations (interval halvings) halveinterval.f90 RECURSIVE SUBROUTINE halveinterval(f,xl,xr,tol,iter count,zero,delta,err) REAL(KIND=RP),INTENT(IN) :: tol REAL(KIND=RP),INTENT(INOUT) :: xl,xr,intent(inout) :: iter count REAL(KIND=RP),INTENT(OUT) :: zero,delta,intent(out) :: err REAL(KIND=RP),EXTERNAL Local Variables REAL(KIND=RP) :: f :: xm delta = 0.5 RP*(xR-xL) Check to see if you ve reached the tolerance for the root IF (delta.lt.tol) THEN Yes - Return result err = 0 zero = xl + delta No root yet - check iterations and halve again iter count = iter count - 1 IF (iter count.lt.0) THEN Max iterations w/o solution - return error 4

err = -2 zero = xl + delta Keep iterating xm = xl + delta IF (f(xl)*f(xm).lt.0.0 RP) THEN CALL halveinterval(f,xl,xm,tol,iter count,zero,delta,err) CALL halveinterval(f,xm,xr,tol,iter count,zero,delta,err) END SUBROUTINE halveinterval 5