46
MET 50 Fortran - INTRODUCTION

MET 50

  • Upload
    erma

  • View
    59

  • Download
    0

Embed Size (px)

DESCRIPTION

MET 50. Fortran - INTRODUCTION. Introduction. Fortran = FORmula TRANslation Words in blue…look up on wikipedia (homework!). Introduction. Q: Why study Fortran??? A: legacy codes! - PowerPoint PPT Presentation

Citation preview

Page 1: MET 50

MET 50

Fortran - INTRODUCTION

Page 2: MET 50

Introduction

• Fortran = FORmula TRANslation

• Words in blue…look up on wikipedia (homework!)

Page 3: MET 50

Introduction

Q: Why study Fortran???A: legacy codes!

Fortran has been in use for more than fifty years, and there is a vast body of Fortran in daily use throughout the scientific and engineering communities.

It is the primary language for some of the most intensive supercomputing tasks, such as weather and climate modeling, computational fluid dynamics, computational chemistry, computational economics, plant breeding and computational physics.

Page 4: MET 50

Introduction

History of Fortran…1. 1st version: 1950’s…IBM2. Fortran 66…19663. Fortran 77…19774. Fortran 90…fairly big changes but…

Fortran90

Fortran77

Page 5: MET 50

Introduction

• Scientists & engineers develop & use lots of formulae

• We often need to evaluate them – find number values

• Sometimes easy– example: converting deg F to deg C

Page 6: MET 50

Introduction

• Usually much harder

– example: weather forecasting (numerical weather prediction, NWP)

– Involves simultaneous solution of multiple equations at multiple locations in the atmosphere at multiple times to get a forecast

Page 7: MET 50

Introduction

• Solving a scientific/engineering problem ALWAYS involves many steps.

• Many of these steps do not involve Fortran!!!

• So we will learn much more than just Fortran in this class!

Page 8: MET 50

Introduction

• Suppose you have a problem you want to solve.

– In meteorology and climate science (MCS), this will typically be either:

• Data analysis, or• Computation of a quantity, or• Solution of an equation (or set of eqs.)

Page 9: MET 50

Introduction

• The first step is to do as much work as possible on paper

– make sure you have defined the problem properly

– make sure you have as much information as possible

Page 10: MET 50

Introduction

• Example: Suppose we wish to calculate how much kinetic energy is in hurricane Irene.

– Think first about how you would do this with pen and pencil.

– Recall that kinetic energy is defined as:

(speed)2/2

Page 11: MET 50

Introduction

– So for air, we have:

KE = (wind speed) 2/2

– This is the amount for a single kilogram of air. For an entire hurricane, we have to add over all the mass of the air in the hurricane.

K = ∑ { KE }

Summation (over ALL air) (“sigma”)

Page 12: MET 50

Introduction

– Now we can take real wind data (e.g., from research aircraft, forecast models etc.) and compute “K” using our formula.

– Computers are VERY good at these tasks.

• Note that wind speed could be both horizontal winds (“large” numbers) and vertical winds (“small” numbers)

Page 13: MET 50

Introduction

• The next step is to break the calculation down into small bite-sized pieces.

• Typically we do this on paper (or in your head when the problem is easy AND you have lots of experience)

• In our KE example…

Page 14: MET 50

Introduction

• Get a single piece of wind data (e.g., directly over Nassau at 1 km above the surface) horizontal and vertical winds (U and W, say)

• Compute KE for this one location: (U2 + W2)/2• Store the answer in memory• Get the next piece of wind data (e.g., directly over

Nassau at 2 km above the surface) horizontal and vertical winds (U and W, say)

• Compute KE for this next location: (U2 + W2)/2• Add this value to the one you just stored in memory• Store the new sum in memory• Keep going in order to cover the entire hurricane• Print out the final number! Hey presto!

Page 15: MET 50

Introduction

• And now - translate each step in the list above into Fortran!

– This is like translating into Spanish.– You need to know two things:

• Vocabulary– Words like: IF, STOP, GO TO, PRINT

» They all look pretty obvious!• Rules

– Things like: the commands “STOP” and “END” must appear in your program.

Page 16: MET 50

Introduction

• Computers are very smart!

– They can do many things

• they can remember lots of stuff (via memory)

• they are VERY fast

Page 17: MET 50

Introduction

• Computers are very dumb!

– They can ONLY do what YOU tell them

• So if you tell them to do something wrong, they will do it

– So YOU MUST be 100% correct in your coding (and logic…the stuff you did on paper)!

Page 18: MET 50

Introduction

• Once you have your Fortran program written, the next step is to present the program to a computer!

• If you are on a laptop with Fortran software

installed, this will be straightforward.

• If you are on a department machine (including laptop), there are extra steps…see later.

Page 19: MET 50

Introduction

• Computers are very dumb!

– They don’t know what Fortran is!

– They have to translate your Fortran program into a low-level machine language.

• Fortran is machine-independent.

• Machine language is machine-dependent.

Page 20: MET 50

Introduction

• We use something called a compiler to do the translation.

– A compiler is part of Fortran software (which you buy, or use if it’s installed somewhere).

– A compiler ingests your Fortran code.

• Your code is in a file with a name like “program.for” or “program.f”

• YOU get to name it .

Page 21: MET 50

Introduction

– The compiler digests this and produces an executable file – a file called program.exe

– This is what then “runs” on the computer to solve your problem (e.g., make a forecast)

Page 22: MET 50

Introduction

Putting it all together…

• With a computer that has Fortran software installed, you would:

– Open up the Fortran software (just like opening up Word)

– Type in your Fortran code using a text editor

• MS Word is a very fancy text editor; we use simpler ones (see later)

Page 23: MET 50

Introduction

– Submit your code to the compiler.

• See if it finds any mistakes.

• Fix mistakes and re-submit

• repeat these steps as necessary!

– Once you have your *.exe executable file, you run it!

Page 24: MET 50

Introduction

– After the code has run, check see if your answer looks “funny”

• If “no”, stop.

• If “yes”, you made either a coding error (e.g., a “+” instead of a “-“ somewhere), or a logic error

• Find the error, fix it, re-compile, re-run and see if the results look better (repeat these steps as necessary!)

Page 25: MET 50

Introduction

– A problem is called a bug (look up software bug).

• It is assumed hardware bugs are gone when the vendor sells you a computer!

– Read this: http://en.wikipedia.org/wiki/Grace_Hopper

• Go down to “anecdotes” and read the 2nd one

– If the results look OK, you are done!

Page 26: MET 50

Introduction

Putting it all together (2)…

• With a computer that DOES NOT have Fortran software installed…desktops & laptops in DH 614…

– You first need to “go to” a computer that does have Fortran software installed.

– For us, this will be “blizzard” – a unix machine.

Page 27: MET 50

Introduction

– Every computer needs an operating system (OS) to work.

– There are 3 major OS’s in operation for PCs:

1. Windows (now windows 7) for PC-type machines2. Mac OS (OS-X, which is UNIX-based) – latest is “lion”3. Linux, which is used to run many servers (such as

blizzard) & is open-source. There are many “flavours” (e.g., Red Hat).

– Linux – unix are pretty much interchangeable

Page 28: MET 50

Introduction

– In our department, we mainly use windows and linux (some machines are/were dual-boot).

– Our fortran software is installed on blizzard, so we need to learn

• How to get to blizzard• some basic unix

Page 29: MET 50

Introduction

How to log into the “unix side”…

STEP 1: log into a windows machine

– Login ID: dilbert– Password: Dogbert7

{Reset password and remember it!!!???}

Page 30: MET 50

Introduction

STEP 2: log into “blizzard”

– On the desktop or from “start”, find the SSH application.

• SSH (secure shell) is software that allows you to log into remote computers and exchange files “securely”.

– Open SSH.– Click the “quick connect” button and enter:

blizzard.met.sjsu.edu = host nameYour assigned login ID for blizzard (“abridger”)Your assigned password (“ab1234%”)

“last4 of SJSU ID”

Page 31: MET 50

Introduction

STEP 2: get accustomed to UNIX

– To move around “inside blizzard”, you will need to use Unix commands. A sheet of commands will be handed out.

– Practice each command! Type the command and hit <enter>.

Page 32: MET 50

Introduction

Examples:

– To see where you are, type “pwd” (for print working directory).

– To go back up to a higher level directory, type “cd” (for change directory).

– To make a new directory (e.g., for this class), type “mkdir” (for make directory).

– Practice each command! Type the command and hit <enter>.

Page 33: MET 50

Introduction

How to enter a fortran program

– Get into the directory in which you will run your codes and keep your results.

• Example: cd /data3/bridger/classes/met50

– You will have written out your code on paper ahead of time (unless you are very good!)

Page 34: MET 50

Introduction

– You need to type your code into a file.

• Example:

met50ex01.f

• Note: filename has rules, and must have file extension .for or .f (depends on fortran software)

– Q: How do we type code into the file? – A: Using a text editor.

Page 35: MET 50

Introduction

Text editors…

• Software that allows you to enter text (duh!)

• Don’t use MS Word – it has too much fancy formatting stuff.

– Actually, you can if you save the file as a text file (*.txt) but we don’t do this.

Page 36: MET 50

Introduction

Three choices of text editor:

1. vi

• Simply type: vi filename.extension

– As in: “vi met50ex01.f”

• Gives a full screen view of your code– Some editors show you only one line

• Allows you to go in and add/delete/modify code

• In widespread use.

Page 37: MET 50

Introduction

• A bit tricky to navigate and edit code!

• See handout and practice session

2. pico

• Type: pico filename.extension

– As in: “pico met50ex01.f”

• Gives a full screen view of your code

• See handout and practice session

Page 38: MET 50

Introduction

3. emacs

• Can be used two ways:

(a)EITHER: emacs filename.extension → display on unix screen

(b)OR: (a) First open “X-win32” software on the PC (via “start”).(b) Then within blizzard type “methostnn” where “nn” is the

number of the machine you’re at(c) And then type emacs filename.extension (d) → display on your windows machine in living color

• You will probably like this best!

Page 39: MET 50

Introduction

Phew!

A lot so far:

– The UNIX operating system – need to familiarize– Text editors – need to familiarize– Thinking before programming – need to familiarize– And Fortran itself!!!

Page 40: MET 50

Introduction

How to run a Fortran program

– Again in the directory where your codes are, enter the command:

“gfortran –o exename met50ex01.f”

– “gfortran” invokes the Fortran compiler– “met50ex01.f” is the name of the input fortran code– “exename” is the name of the output executable file

Page 41: MET 50

Introduction

“gfortran –o exename met50ex01.f”

– The “-o ” option says “use exename as the name of the executable program”.

• Lower-case “oh” – not zero!

– This is about the simplest example. Many, many more options are available!

Page 42: MET 50

Introduction

– NEXT: study the screen closely: did you get an error message?

– If there is a fortran error in your code, the compile step will generate (an) error statement(s).

Example– if you forget the END statement @ end of code, you

get this error message:

PGF90-S-0022-Unexpected end of file - missing END statement (bu01.f: 145)

This tells you the problem and line number to look at…

Page 43: MET 50

Introduction

– If there is a fortran error message, go back into the editor and fix the problem.

– Then re-compile and read the screen again.

– If there are no fortran errors, your code will compile and you will get the executable file generated.

– In unix, the command “ls –l” will give you a listing of files…look for “exename”.

– You then simply type “exename” and your code runs!!!

Page 44: MET 50

Introduction

How to write a fortran program

– Remember – you have to learn a new vocabulay and some rules.

– Fortran itself is about translating “math statements” into machine language.

Page 45: MET 50

Introduction

Example:

1. E=mc2 might be written as :

E=m*(c**2)

2. The speed of sound is: c=√(RdT)

which might be written as:

c=SQRT(gamma*Rd*T)

Page 46: MET 50

Introduction

– You will have some flexibility in choosing some things.

example:– In calling the symbol “” “GAMMA”– In calling the quantity “Rd” “RD”

– The rules always apply unless you say otherwise.