fortran programming basics met 50. programming basics the basic layout of all programs is as follows...
Post on 22-Dec-2015
220 views
TRANSCRIPT
FORTRAN PROGRAMMING BASICS
MET 50
MET 50, FALL 2011, CHAPTER 2 PART 1
2
Programming Basics
The basic layout of all programs is as follows (p.33)
PROGRAM name = heading (i.e., title)Specifications (e.g., define constants)Executions (the main part)Subprograms (see much later in the semester)END PROGRAM statement
Let’s look at each…
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
3
Programming Basics
Program name…
You get to choose a name!Make it meaningful!Name MUST start with a letter.Can be followed by letters, numbers, and the
underscore.
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
4
Programming Basics
Examples…
Areaofcircle - OKTemp_conversion (for deg F to deg C) - OK
2Version_speed – NOT OK (“2”)Mybro’scode – NOT OK (“ ’ ”)
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
5
Programming Basics
Specifications…
Any quantity in your program MUST be given a name – by you.
Includes known things e.g., “g” – acceleration of gravity (9.8 m/s2) things like data that you might input.
Also includes unknowns – the things you are trying to calculate.
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
6
Programming Basics
Example …
Speed of sound (c) is given by: c = √(RdT).
To compute “c” using Fortran, we need to “name” the quantities:
c … suppose we call it SPEEDRd … suppose we call it RDT … suppose we call it TEMP
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
7
Programming Basics
Memory …
Once we do this, the compiler allocates a memory location to each variable.
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
8
Programming Basics
Specifications…
Always choose meaningful names.Gravity → GRAVValue of → PICoriolis parameter → CORIOLIS Kinetic energy → KE (but this starts with a “K” which can be a
problem because…)
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
9
Programming Basics
Data types…
Fortran allows 5 data types:
INTEGERREALCOMPLEXCHARACTERLOGICAL
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
10
Programming Basics
IN OTHER WORDS, A Fortran program will assume a quantity is one of these (and nothing else).
9/1/2011
Rule:When you choose a name for a quantity starting with the letters I,J,K,L,M,N, Fortran will make that quantity an INTEGER.
MET 50, FALL 2011, CHAPTER 2 PART 1
11
Programming Basics
Example:
If we decide to name “gravity” as “KGRAV”, it will be treated as an integer!
Its value (entered as 9.8) would be recorded in memory as either “9” if truncated – or “10” if rounded up.
Will give an error in calculations
If we name kinetic energy as “KE”, it will be treated as an integer!
We must be careful to avoid these problems…see below
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
12
Programming Basics
INTEGER data…
A whole numberPositive or negative
Must include minus sign if negative
Cannot contain a periodSee examples on p.16 @ top
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
13
Programming Basics
REAL data…
Not a whole number!Positive or negative
MUST include minus sign if negative
MUST include a decimal (e.g., 9.81 or 17.0)
Often write in exponent form:
6371x103 6.371E61.48x10-17 1.48E-17
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
14
Programming Basics
COMPLEX data…
Let’s deal with this later
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
15
Programming Basics
CHARACTER data…
Things like letters!Often used to label some data (e.g., on printed
tables)Character data in a program is enclosed between
apostrophes.
Example:“ALISON” is character data of length 6 … use “-”‘Allison is gone’ is character data of length 15 …
use ‘-’9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
16
Programming Basics
LOGICAL data…
Let’s deal with this later (or never)
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
17
Programming Basics
To repeat…when you choose a name for a quantity starting with the letters I,J,K,L,M,N, Fortran will make that quantity an INTEGER.
Otherwise it is a REAL data type.
We often choose names to make use of this…but we also make mistakes.
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
18
Programming Basics
Having REAL and INTEGER quantities together can lead to problems (examples below).
Thus it is VITAL to explicitly TELL THE COMPILER which is which.
This is called type declaration.Type declaration statements go at the start of
the program.
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
19
Programming Basics
Examples of format:
REAL : : grav INTEGER : : timeofday
Says the following:1) “grav” will be a REAL number2) “timeofday” will be an INTEGER number
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
20
Programming Basics
Statements like these MUST go @ start of code (“specifications”).
As a safeguard, your first statement should be this:
IMPLICIT NONE
Which says to the compiler … “do not assume anything … I will declare everything”!
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
21
Programming Basics
So your program structure so far is:
PROGRAM myfirstcodeIMPLICIT NONEREAL : : RD, GAMMA, TEMP, SPEEDExecution statements (not done yet)Subprograms (not done yet)END PROGRAM
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
22
Programming Basics
More stuff:
REAL : : grav = 9.81
tells the computer the value of “grav”!
This gets put into the memory location for “grav”.
ALL VARIABLES MUST HAVE VALUES DEFINED before the main computations happen!
Otherwise – they may be assumed to be zero
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
23
Programming Basics
For CHARACTER data, we type:
CHARACTER (5) : : month
This says :
“month” will be CHARACTER data with 5 characters
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
24
Programming Basics
Other ways to write:
CHARACTER (5) : : month, day
This says :
“month” and “day” will both be CHARACTER data with 5 characters
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
25
Programming Basics
Assigning values
In Fortran, the statement:
A = B
means: the value stored in memory for “B” will be assigned to (transferred into) the memory space for “A”
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
26
Programming Basics
So – the computer gets the value of a variable on the LHS of the “=” by first computing the value on the RHS.
A = B
So it’s OK to write: c = √(RdT)
But not: √(RdT) = c
Write the unknown on the left.9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
27
Programming Basics
Arithmetic operations…
There are five allowed:
addition A+B subtraction A-B multiplication A*B not AB division A/B exponentiation A**3 means A3
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
28
Programming Basics
Putting it all together…
To evaluate: x = b2 – 4acwith a=1, b=7, and c=-2, we might have lines of code like:
IMPLICIT NONE REAL : : X, B, A, C A=1.0 ! A IS REAL, SO A=1.0 – NOT A=1 B=7.0 C=-2.0 X = B**2 -4.0*A*C
Everything after the “!” is ignored!!!
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
29
Programming Basics
Another example…
To evaluate: c = √(RdT)
with =1.4, b=7, and c=-2:
IMPLICIT NONE REAL : : GAMMA, RD, TEMP, SPEED GAMMA = 1.0 RD = 287.0 TEMP = 273.0 SPEED = SQRT(GAMMA*RD*TEMP)
Here, we have used the intrinsic function: SQRT = √
9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
30
Programming Basics
An intrinsic function is something built into Fortran.
COS(X) SIN(X) EXP(X) SQRT(X) MAX(X) ABS(X)
And more – see Table 2-2 and Appendix A.9/1/2011
MET 50, FALL 2011, CHAPTER 2 PART 1
31
Programming Basics
We/you now know enough to:
a) Try writing tiny bits of codeb) Debug bad code – anything that even
slightly violates one of the rules above!
9/1/2011