Some Fortran programming tips ATM 562 Fall 2015 Fovell (see also PDF file on class page) 1.

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

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=
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.
FORTRAN Short Course Week 1 Kate T-C February 17, 2008.
Introduction to Programming Lesson 1. Objectives Skills/ConceptsMTA Exam Objectives Understanding Computer Programming Understand computer storage and.
Programming in Visual Basic
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Chapter 8 Introduction to Arrays Part II Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
FORTRAN Short Course Week 2 Kate Thayer-Calder February 23, 2009.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Simplest program and Data Output Sen Zhang. The simplest program looks like a single person company! void main() { // this starts a comment line // here.
FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Chapter 8 Arrays and Strings
Basic Elements of C++ Chapter 2.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Python  By: Ben Blake, Andrew Dzambo, Paul Flanagan.
Introduction to FORTRAN
Beginning Fortran Fortran (77) Basics 22 October 2009 *Black text on white background provided for easy printing.
Guidelines for the CMM coding project 5 October 2006 (or, “How to make your life easier in the long run”)
Fortran 1- Basics Chapters 1-2 in your Fortran book.
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
Computer Science 101 Introduction to Programming.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 8 Arrays and Strings
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.
FORTRAN FORmula TRANslator -Anand Trivedi. HISTORY  Designed and written from scratch in by an IBM team lead by John W. Backus as the first.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
Module and Data Sharing. Programming in the Large Software, in general, is large having multiple units Multiple units designed and developed independently.
Model Task 0A: Programming the 1D upstream scheme ATM 562 Fall 2015 Fovell 1.
1 Serial Run-time Error Detection and the Fortran Standard Glenn Luecke Professor of Mathematics, and Director, High Performance Computing Group Iowa State.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
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)
Beginning Fortran Fortran (77) Advanced 29 October 2009 *Black text on white background provided for easy printing.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
CSCI 171 Presentation 6 Functions and Variable Scope.
A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Python Let’s get started!.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Beginning Fortran Introduction 13 October 2009 *Black text on white background provided for easy printing.
More on F90 Outline: I.Parameter statements II.Comments III.Program layout-- execution part IV.if/then/else V.Case statements VI.do loops VII.goto VIII.Intrinsic.
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Fundamentals of Programming I Overview of Programming
Chapter Topics The Basics of a C++ Program Data Types
CMPT 201.
Basic Elements of C++.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Data types and variables
Pre-processor Directives
Basic Elements of C++ Chapter 2.
11/10/2018.
Chapter 5 - Functions Outline 5.1 Introduction
Topics Introduction to File Input and Output
Midterm Review Programming in Fortran
mdul-cntns-sub-cmmnts2.f90
Topics Introduction to File Input and Output
Presentation transcript:

Some Fortran programming tips ATM 562 Fall 2015 Fovell (see also PDF file on class page) 1

Background “Fortran” derived from “formula translation”, and emphasizes letting “math look like math”. Fortran is NOT case sensitive. Variables assumed real, unless the first letter of the variable name starts with the letters I-N, inclusive. Then they're integers by default. – (Mnemonic device: I-N are INtegers). Fortran 90+ permits free format. Fortran 77 (F77) statements start in column 7, with column 6 for continuation markers, and cols 1-5 for labels. Comment lines start with “C”, “c”, or “!”. Anything after “!”, wherever it appears, is not read.

Arrays Arrays are indexed from 1, unless otherwise specified. Array indices must be integers. parameter(nx=50,ny=40,nz=25) dimension pressure(nx,ny,nz),height(0:nz) do k=1,nz do j=1,ny do i=1,nx pressure(i,j,k) = [do something here] enddo ! i enddo ! j enddo ! k

More on arrays If arrays are the same shape, can manipulate entire arrays outside of do loops parameter(nx=50,nz=40) dimension pi(nx,nz),temp(nx,nz),theta(nx,nz) ! This… do k=1,nz do i=1,nx temp(i,k)=theta(i,k)*pi(i,k) enddo ! …is equivalent to this… temp=theta*pi

A sample Fortran program (F77 syntax)

program example ! this is optional, actually ! non-executable statements go first: this includes parameter, common, ! dimension, data statements and statement functions ! parameter statements make array bookkeeping easy parameter(nx=100,nz=40) ! common blocks hold arrays, constants you want to use in ! multiple program modules common/base/ub(nz),tb(nz),qb(nz),pib(nz),rhou(nz),rhow(nz) common/uwind/up(nx,nz),u(nx,nz),um(nx,nz) ! wind at 3 times common/ptemp/thp(nx,nz),th(nx,nz),thm(nx,nz) ! temp at 3 times common/grids/dx,dz,dt,d2t,time ! grid setup, etc common/grid2/zw(nz),zu(nz) ! heights common/consts/rd,g,cp,psl ! physical constants ! dimension statements for local arrays - used only in this program unit dimension crap(nx,nz) ! initialize constant values you will NOT change during execution data g/9.8/,dx/400./,dz/400./,dt/2.0/,cp/1004./,rd/287./

! ! executable statements go here ! ! a neat way of getting trigonometric ``PI'' to machine precision trigpi=4.0*atan(1.0) ! frequent divisions should be converted to multiplications where possible ! ex: I need to divide rd by cp a lot, and also (dx**2) and (dz**2) xk=rd/cp rdx2=1./(dx*dx) rdz2=1./(dz**2) ! block DOs are easy to read when indented. ! every DO has an ENDDO ! float turns an integer into a real before multiplication do k=2,nz zw=(float(k-1)*dz) enddo

! block IFs may be simple or compound iflag=0 do k=2,nz-1 ztp = (float(k)-1.5)*dz if(k.eq.2) iflag=1 if(ztp.gt.ztr)then tb(k)=thetatr*exp(9.8*(ztp-ztr)/(cp*ttr) else tb(k)= *(ztp/ztr)**1.25 endif if(k.le.20)then [do something here] else if(k.lt.nz-5)then [do something here] else [do something here] endif enddo

! F77 continuation marker is most any symbol placed in column 6 ! for multiline statements ! **line up** your statements for easy debugging and reading do k=2,nz-1 do i=2,nx-1 thp(i,k)=thm(i,k) *dtx*(u(i+1,k)*(th(i+1,k)+th(i,k)) 2 -u( i,k)*(th(i-1,k)+th(i,k))) enddo ! subroutine calls make program neater, more modular ! try to avoid passing unnecessary arguments -- use common statements call setup call runmodel call cleanup

! Example of a formatted write statement write(6,1000) real,integer,real 1000 format(1x,f5.2,i6,e15.6) ! you can avoid format statements when you don't need them write(6,*) ' the real number is ',real ! write to the screen with print, which is the same as write(6,*) print *,' the real number is ',real print 1000, real, integer, real ! open a file before you write to it ! this opens a NEW formatted file called output at unit number 12 ! if this file exists, it will cause an ERROR ! AVOID unit numbers 5 and 6; those are standard input and output open(12,file='output',status='new',form='formatted') ! OR open the file with status UNKNOWN which will overwrite any old ! file of same name without warning open(13,file='output2',status='unknown',form='formatted') ! write to the file using the designated unit number write(12,*) rd,g,cp

! program ends with ``stop'' and ``end'' statements stop end ! then your subroutines go here. they terminate with ``return'' and ! ``end'' statements subroutine setup [your parameter and common blocks repeated here] return end

Continuation markers: F77 vs. F90+ ! Fortran 77 style (file name using.f suffix usually defaults to F77) trigpi2 = 4.0*atan(1.0) 1 /2 ! Fortran 90+ style (file name using.f90 suffix indicates F90) trigpi2 = 4.0*atan(1.0) & /2 !...which is the same as trigpi2 = 4.0*atan(1.0) & & /2

Global declarations program example2 include 'global' [etc.] stop end subroutine setup include 'global' [etc.] return end The file global contains your parameter and array declarations, named common blocks, defined constants, etc.. If you modify this file, you need to recompile your code

Some common problems Using values before they have been initialized – Many Fortran compilers initialize all variables and array elements to zero… but not all Overrunning array boundaries parameter(nx=100,nz=25) dimension u(nx), h(nz) do i=1,nx h(i)=… enddo Combining integers and reals in computations – IFIX converts real to integer; FLOAT converts integer to real