FORTRAN Short Course Week 2 Kate Thayer-Calder February 23, 2009.

Slides:



Advertisements
Similar presentations
Introduction to arrays
Advertisements

Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
FORTRAN Short Course Week 1 Kate T-C February 17, 2008.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
IDL Tutorials: Day 4 Goal: Learn some programming techniques: Relational operators, conditional statements, boolean operators, loops Angela Des Jardins.
Program Design and Development
Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 2 - Java Programming Fundamentals1 Chapter 2 Java Programming Fundamentals.
An Object-Oriented Approach to Programming Logic and Design Chapter 7 Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
VB .NET Programming Fundamentals
The switch Statement, DecimalFormat, and Introduction to Looping
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Functions. Program complexity the more complicated our programs get, the more difficult they are to develop and debug. It is easier to write short algorithms.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
1 Week 12 Arrays, vectors, matrices and cubes. Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an.
FORTRAN Short Course Week 4 Kate Thayer-Calder March 10, 2009.
Multi-Dimensional Arrays
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Scientific Computing Division A tutorial Introduction to Fortran Siddhartha Ghosh Consulting Services Group.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
PHP Logic. Review: Variables Variables: a symbol or name that stands for a value – Data types ( Similar to C++ or Java): Int, Float, Boolean, String,
CRE Programming Club - Class 4 Robert Eckstein and Robert Heard.
CPS120: Introduction to Computer Science Decision Making in Programs.
Some Fortran programming tips ATM 562 Fall 2015 Fovell (see also PDF file on class page) 1.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
IDL Tutorials: Day 4 Goal: Learn some programming techniques: Relational operators, conditional statements, boolean operators, loops Maria Kazachenko
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
I Power Higher Computing Software Development High Level Language Constructs.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Beginning Fortran Introduction 13 October 2009 *Black text on white background provided for easy printing.
Decision Statements, Short- Circuit Evaluation, Errors.
CRE Programming Club - Class 4 Robert Eckstein and Robert Heard.
Controlling Program Flow with Decision Structures.
Fortran: Control Structures Session Three ICoCSIS.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
JavaScript, Sixth Edition
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 4 – C Program Control
Programming For Nuclear Engineers Lecture 6 Arrays
MATLAB: Structures and File I/O
Arrays, For loop While loop Do while loop
Midterm Review Programming in Fortran
Structured Program
Chap 7. Advanced Control Statements in Java
Controlling Program Flow
REPETITION Why Repetition?
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation transcript:

FORTRAN Short Course Week 2 Kate Thayer-Calder February 23, 2009

Any Questions?

Topics for this week The Clock Logical Operators Conditional Statements DO loops Whole Array Operators Multi-Dimensional Arrays Subroutines and Functions

The Machine Clock There is a little electronic heartbeat in the processor called the clock, that switches on and off Each time the clock goes on, the processor gets another chunk of 1’s and 0’s from memory and sends them down the pipeline Effectively, the computer can do one operation per cycle. If you are pulling from a slower part of memory, the machine might sit idle for several cycles waiting. The speed of your processor is the speed of your clock - 2 GHz = On/Off 2x10 9 (2 billion) s -1

FLOPS A FLOP is a FLoating point OPeration Because Floating point numbers have two parts and extreme precision requirements, multiplication and division with floats can take several cycles to do one operation. Supercomputers mostly do floating point math, so their performance is measured in FLOPs instead of cycles. Recently, computers were created that produce PETA- FLOP speed (10 15 flops/s - 1 quadrillian, or 1,000 trillian operations each second)

Execution Control Another type of operation that we can do is decision making The IF statement evaluates a logical statement and determines whether or not the body statements will be evaluated. The Logical Type IF (logical expression) THEN.... (body) ENDIF See Example: IfExample.f90 Each If statement requires at least 2 cycles

Relational and logical operators.TRUE.,.FALSE. 0 is false, all other values are true == Equal (.eq.), /= Not equal (.neq.) >= Greater than or equal (.ge.) <= Less than or equal (.le.) Greater than (.gt.).AND.,.OR. and.NOT..eqv.,.neqv.

New Order of Operations (), **, */, +-.EQ.,.NE.,.LT.,.LE.,.GT.,.GE..NOT..AND..OR..EQV. and.NEQV.

Try some of these out... 2**a*b == 2**(a*b) T.and. T.or. F a > b.eqv. a >= b.not. T.or. 5 < ne and. 2.gt. 1

IF/THEN/ELSE An efficient way to branch control between two sets of operations Very similar to an IF statement, but with a second option, only one body or the other will be executed. IF (logical expression) THEN.... (body1) ELSE.... (body2) ENDIF

IF/Then/Else IF/Else A less efficient and more complex method to branch between multiple bodies of operations IF (logical expression) THEN.... (body1) ELSE IF (logical expression) THEN.... (body2) ELSE.... (body2) ENDIF This structure can be dangerously overused

CASE Statements Could be more efficient, because the initial expression is only evaluated once. Less likely to accidently evaluate code due to poorly formed secondary tests. Much easier to read, understand and debug. UNFORTUNATELY, the value of the case selector must be a single integer, character (of any length), or logical value, which limits use.

CASE Statements SELECT CASE (expression) CASE (case selector).... CASE (case selector).... (body2) CASE DEFAULT.... (body2) END SELECT See Examples

Computers DO Operations Repeatedly The DO loop lets us do the same series of operations over and over again DO Counter = Start, End, Increment.... (Operations) ENDDO See Example OscarCount.f90

DO Loops, A Few Rules The statements in the body of the loop are repeated until the counter no longer equals the “End” value Note that the loop IS executed at the start and end values - unlike IF statements. The “Increment” is optional, if omitted, the compiler assumes it is “+1” You must be able to reach “End” from “Start” (so an Increment of 0 is not allowed), if this is not possible, the loop is not executed. Need to be careful, plenty of infinite loops are not caught by the compiler

Nesting DO Loops This can be done as many times as necessary, but be careful, because your statements will be evaluated exponentially more times for each loop! DO Counter = Start, End, Increment DO Counter2 = Start, End, Increment.... (Operations) ENDDO To keep it understandable, Counter and Counter2 should be different variables Two nested loops are order n 2, three is n 3, etc

More on Arrays Rank, bounds, extent, size, conformable Subsections: xarr(start:end) Fortran Triplets: xarr( start:end:stride ) Whole Array arithmetic - the arrays must be conformable C=A+B E=C*D foo=bar**0.5 results=0 adds or multiplies each element of each array to the other

Multi Dimensional Arrays type, dimension(dim1,dim2,...) :: name REAL, dimension(lon,lat,height,time) :: temp Array element ordering - indices vary slower as the dimension gets larger {(1,1)(2,1)(3,1)(1,2)(2,2)(3,2)(1,3)(2,3)(3,3)} Higher dimensional arrays are usually stored contiguously in memory, in ROW MAJOR order (lon, lat)

Multi-Dimensional Dimension 1 Dimension 2 For more dimensions: (1,1,1)(2,1,1)(3,1,1) (1,2,1)(2,2,1)(3,2,1) (1,3,1)(2,3,1)(3,3,1) (1,1,2)(2,1,2)(3,1,2) (1,2,2)(2,2,2)(3,2,2)

Array Transformation Reshape function is pretty cool Matrix = RESHAPE( Source, Shape ) A = RESHAPE( B, (/3,2/) ) Another way to index your array elements uses ‘mod’ and integer division lon = array(MOD(i,num_lons)) lat = array(i/num_lats) Can store data as (lat, lon) as well, but it will be counter-intuitive and less efficient to transform

WHERE statements An easy way to initialize or set sections of arrays WHERE (array expression) array assignment block ELSEWHERE array assignment block 2 END WHERE This is called “masking”

FORALL Construct This statement indicates to the compiler that the operations can be performed in parallel (no operations depend on the value of the operation on other elements in the array) FORALL (triplet) variable = expression

Subroutines In our constant effort to avoid re-inventing the wheel, we can abstract our code by packaging certain parts to be reusable. A subroutine is just a sub-section of code that can be reused in the main program. Subroutines can be included in the same file as the main program or in other files (but we’ll talk about that next week) In Fortran, arguments are passed by reference, so subroutines can change the value of variables back in the main program.

Subroutines SUBROUTINE name ([arguments]) Implicit None argument declaration with INTENT.... (body) END SUBROUTINE name The arguments are generally optional, but are the safest way to pass information. In your main program, call the subroutine as: call name ([arguments]) See example OscarCount2.f90

Subroutines Intent lets the compiler know what you are doing with the variables It is not required, but will help your compiler help you INTENT(IN) - the parameter has a value when the function is called, and that value is not changed INTENT(OUT) - the parameter does not have a given value, but is assigned to when the subroutine or function is run INTENT(INOUT) - the parameter is given a value which could be changed by the subroutine.

Functions Exactly the same as subroutines, except Functions can return a value. Like all of the built-in functions we’ve been using, a call to a function returns the value which can be stored in a variable (but the types must match). result = funct(arg1,arg2,...) Example OscarCount3.f90

Functions You can declare a function in the same file as your main program (after it) TYPE FUNCTION name ([arguments]) Implicit None argument declaration with INTENT.... (body) END FUNCTION name

What did we cover today? The clock Logical execution control Relational and logical operators DO loops Whole array manipulators Multi-Dimensional Arrays Where and ForAll Subroutines and Functions