Computational Methods of Scientific Programming

Slides:



Advertisements
Similar presentations
Input and Output READ WRITE OPEN. FORMAT statement Format statements allow you to control how data are read or written. Some simple examples: Int=2; real=
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.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Python quick start guide
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Scientific Computing Division A tutorial Introduction to Fortran Siddhartha Ghosh Consulting Services Group.
Class Review. Basic Unix Commands list files in a directory: ls list files in a directory: ls remove files: rm remove files: rm rename files: mv rename.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
Some Fortran programming tips ATM 562 Fall 2015 Fovell (see also PDF file on class page) 1.
I Power Higher Computing Software Development High Level Language Constructs.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Beginning Fortran Introduction 13 October 2009 *Black text on white background provided for easy printing.
PHP Form Processing * referenced from
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Introduction to python programming
Chapter 10 Programming Fundamentals with JavaScript
Chapter Topics The Basics of a C++ Program Data Types
The Machine Model Memory
VBA - Excel VBA is Visual Basic for Applications
EEE 161 Applied Electromagnetics
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
A bit of C programming Lecture 3 Uli Raich.
Computing and Statistical Data Analysis Lecture 2
Java Course Review.
COMP 170 – Introduction to Object Oriented Programming
Matlab Training Session 4: Control, Flow and Functions
Programming For Nuclear Engineers Lecture 6 Arrays
C Programming Tutorial – Part I
Basic Elements of C++.
Functions and Procedures
Character (String) Data
Programming Mehdi Bukhari.
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
Numeric Arrays Numeric Arrays Chapter 4.
JavaScript: Functions
A First Book of ANSI C Fourth Edition
JavaScript: Functions.
PHP Introduction.
Java Review: Reference Types
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Intro to PHP & Variables
Engineering Innovation Center
User-Defined Functions
Basic Elements of C++ Chapter 2.
CS190/295 Programming in Python for Life Sciences: Lecture 1
Chapter 5 - Functions Outline 5.1 Introduction
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter 10 Programming Fundamentals with JavaScript
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Use of Mathematics using Technology (Maltlab)
PHP.
Variables Title slide variables.
Chapter 6: User-Defined Functions I
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Functions continued.
Primitive Types and Expressions
PHP an introduction.
REPETITION Why Repetition?
Presentation transcript:

12.010 Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room 54-820A, tah@mit.edu Chris Hill, Room 54-1511, cnh@gulf.mit.edu Web page http://www-gpsg.mit.edu/~tah/12.010

Review of Lecture 4 Looked at Fortran commands in more detail Looked at variables and constants IO commands: Open/Close Read/Write Format Started looking at character strings 09/23/2010 12.010 Lec 05

Today’s Class More Fortran Details Character strings Control statements IF statements DO statements Other command types Include Common Parameters Data statement 09/23/2010 12.010 Lec 05

Character strings Character strings in Fortran are the only variable types where you can easily use pieces of it. (Fortran90 allows character type manipulations with other types of variables) To address part of string use : separator ie., name(n:m) means characters n through m of name A single character in a string is referred to as name(n:n) Note: name(n) means the n’th string of an array of character strings. For all other array types in fortran there is no easy way to refer to a part of an array. You can pass the whole array to a subroutine, or part of an array by passing starting a some location in the array, or a single element with array(i,j). Character strings can be concatenated with the // operator Fortran “knows” about the lengths of strings so larger strings can be equated to smaller strings (copy is truncated) or smaller strings copied to larger strings (copy is padded with blanks). When strings passed to subroutines or functions, best to declare as character*(*) in subroutine or function. 09/23/2010 12.010 Lec 05

Control statements IF If statements: Generic form is If ( logical expression ) then ……. Else if ( logical expression ) then End if Logical expressions are of form. A.eq.B — A equals B .ne. not equal, .gt. Greater than, .ge. Greater than or equal, .lt. Less than, .le. Less than or equal. Combined expressions can be constructed with .not., .and. and .or. e.g., if ( a.eq.b .and. c.eq.d ) then Fortran is (generally) case insensitive and so the auto capitalization of commands here is not a problem. In the other languages it can be problem. 09/23/2010 12.010 Lec 05

If statements 02 A single executable statement can be included after the if (no then used) e.g. if( a.eq.b ) c = d When logical variables are used then only the logical need appear logical done …. if ( done ) then Print *, ‘ Program is finished’ end if Code within an if statements should be indented with the indentation increasing with each layer of if statements. 09/23/2010 12.010 Lec 05

Control statements DO The basic do construction in Fortran comes in several forms. The two main forms are: do j = start, end, inc …. end do do while ( logical expression) In the first form, j starts at value start and continues incrementing by inc until j is greater than end. If inc = 1 then it does not need to appear. Often a numeric label is put after the do; this label is then used to end the loop (label in columns 1-5, statement at end should be continue which is no-operation statement. If end < start and inc is positive, then the code inside the loop is never executed (-onetrip option is available for fortran 66 compatibility) The conventional do loop in fotran looks like (note spacing) do 100 I = 1, 1, 20 Statements 100 continue 09/23/2010 12.010 Lec 05

Control DO 02 Optionally a numeric label can appear after the do in the first form and the loop will end at the (non-format) statement with that label. This form is not recommended, but if used the labeled statement should be a continue. j can be real or integer, but for machine independent results, integer variables are recommended. The code inside the do loop should be indented. Loops may be nested, but may not overlap in range (latter is not possible with recommended form). The index variable in the loop should never be modified nor its value used outside of the loop. 09/23/2010 12.010 Lec 05

Other command types include — allows a block of code to be included in the source file. Usually this code is declarations of variables for a common block (see below) or a set of parameter statements. include ‘file name’ Common — allows the declaration of variables that are available in all modules without them being explicitly passed into the module. This declaration should be used with the include statement. Example: Real*8 a, b, c, d(10) common / reals / a, b, c, d If placed in its own file, all modules that need any of the variables should have the file included. 09/23/2010 12.010 Lec 05

Common blocks The label between the //s names the common and is an arbitrary label. Strict Fortran: Only one type of variable should be placed in a common block (not strongly enforced). Different labeled commons can be used for each variable type. Many computer scientists do not like commons because any module with the common included can change the value of the variable. However, by use of include, it is easy with grep to find all uses of the variables in a common. Variables in commons can be passed into modules but if the common is included in the module, the name needs to be changed. 09/23/2010 12.010 Lec 05

Parameters The parameter statement is a way of naming constants. Again very useful when the include statement is used. Example: Real*8 pi, rad_to_deg Parameter ( pi = 3.1415926535897932d0 ) Parameter ( rad_to_deg = 180.d0/pi ) Notice that the parameters themselves can be included in other parameter statements. Parameters are only available in modules in which they have been declared (thus the use of include statements) Parameters can be used to set the dimensions of variable arrays. 09/23/2010 12.010 Lec 05

Data Statement Data statements are used to initialize variables. Strictly, variables initialized in data statements should not be changed by any module (however they can be changed). Format of a data statement is: Integer*4 days_in_month(12) ! Day of year number at ! start of each month ! (Valid in non-leap year) Data days_in_month / 0, 31, 59, 90, 120, 151, . 181, 212, 243, 273, 304, 334 / Variables in common can only be in data statements in a module type called block data. Exact number and type of values must appear in data statement. 09/23/2010 12.010 Lec 05

Save statement You should assume that variables local to a module will have arbitrary values each time the module is called. If you want variables to retain their values, then either use a data statement (implying their value will not change) or use a save statement. Example Real*8 last_count Save last_count Each time the module called, last_count will retain the value that it had at the end of the last call. The value the first time the module is called is not known, so it should be initialized in the first call. 09/23/2010 12.010 Lec 05

Exercises using FORTRAN Remainder of class we develop and debug fortran programs. Programs will be written in class to Print “Hello World” Compute root-mean-square scatter (RMS) of random numbers generated with the intrinsic rand function Students with laptops might want to bring them along so that can work on their own system or on athena. There is wireless internet in the room. 09/23/2010 12.010 Lec 05

Summary of Today’s class Fortran Details Covered other commands in Fortran: Control statements IF statements DO statements Other command types Include Common Parameters Data statement For the remainder of the class; examine, compile and run the poly_area.f and test programs: loops.f, ifs.f, inout.f and subs.f vars.f is a special examples program. Try modifications of these programs and see what happens. poly_area.f and vars.f should be stored in text files. If they are displayed in a web browser window and the the page saved, they should appear as text files on your system. The reason that the files sometimes do not display correctly is the way that the “end-of-line” is denoted on different OS types. In Unix, a new-line character (ASCII Octal 12 or ^J) is used, on Windows a carriage return/new-line and on MacOSX, a simple carriage return is used (ASCII octal 15, ^M). 09/23/2010 12.010 Lec 05

Exercises using FORTRAN In this exercise session we will write some simple FORTRAN programs: Write a simple program that writes your name to the screen Compile and load the poly_area.f program from the web page. Test the program to see how it works Compile and run the other programs from the web page. Compile and load the vars.f routine from the web page. Test the following modifications to the program: In the first call to var_sub_01, replace j with an integer constant and see what happens To run fortran: gfortran <options> <source files> -o <program name> e.g. gfortran poly_area.f -o poly_area We will be able to try these programs with g77, and fort77 on Mac OSX plus using gfortran on linerva,mit,edu 09/23/2010 12.010 Lec 05