Presentation is loading. Please wait.

Presentation is loading. Please wait.

FORTRAN PROGRAMMING BASICS MET 50. Programming Basics The basic layout of all programs is as follows (p.33) PROGRAM name = heading (i.e., title) Specifications.

Similar presentations


Presentation on theme: "FORTRAN PROGRAMMING BASICS MET 50. Programming Basics The basic layout of all programs is as follows (p.33) PROGRAM name = heading (i.e., title) Specifications."— Presentation transcript:

1 FORTRAN PROGRAMMING BASICS MET 50

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 2 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 3 MET 50, FALL 2011, CHAPTER 2 PART 1

4 Programming Basics Examples… Areaofcircle - OK Temp_conversion (for deg F to deg C) - OK 2Version_speed – NOT OK (“2”) Mybro’scode – NOT OK (“ ’ ”) 9/1/2011 4 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/s 2 )  things like data that you might input. Also includes unknowns – the things you are trying to calculate. 9/1/2011 5 MET 50, FALL 2011, CHAPTER 2 PART 1

6 Programming Basics Example … Speed of sound (c) is given by: c = √(  R d T). To compute “c” using Fortran, we need to “name” the quantities: c … suppose we call it SPEED R d … suppose we call it RD T … suppose we call it TEMP 9/1/2011 6 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 7 MET 50, FALL 2011, CHAPTER 2 PART 1

8 Programming Basics Specifications… Always choose meaningful names. Gravity → GRAV Value of  → PI Coriolis parameter → CORIOLIS Kinetic energy → KE (but this starts with a “K” which can be a problem because…) 9/1/2011 8 MET 50, FALL 2011, CHAPTER 2 PART 1

9 Programming Basics Data types… Fortran allows 5 data types: INTEGER REAL COMPLEX CHARACTER LOGICAL 9/1/2011 9 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 10 MET 50, FALL 2011, CHAPTER 2 PART 1 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.

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 11 MET 50, FALL 2011, CHAPTER 2 PART 1

12 Programming Basics INTEGER data… A whole number Positive or negative  Must include minus sign if negative Cannot contain a period See examples on p.16 @ top 9/1/2011 12 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: 6371 x 10 3  6.371E6 1.48 x 10 -17  1.48E-17 9/1/2011 13 MET 50, FALL 2011, CHAPTER 2 PART 1

14 Programming Basics COMPLEX data… Let’s deal with this later 9/1/2011 14 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 15 MET 50, FALL 2011, CHAPTER 2 PART 1

16 Programming Basics LOGICAL data… Let’s deal with this later (or never) 9/1/2011 16 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 17 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 18 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 number 2) “timeofday” will be an INTEGER number 9/1/2011 19 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 20 MET 50, FALL 2011, CHAPTER 2 PART 1

21 Programming Basics So your program structure so far is: PROGRAM myfirstcode IMPLICIT NONE REAL : : RD, GAMMA, TEMP, SPEED Execution statements (not done yet) Subprograms (not done yet) END PROGRAM 9/1/2011 21 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 22 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 23 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 24 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 25 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 = √(  R d T) But not: √(  R d T) = c Write the unknown on the left. 9/1/2011 26 MET 50, FALL 2011, CHAPTER 2 PART 1

27 Programming Basics Arithmetic operations… There are five allowed: addition A+B subtractionA-B multiplicationA*Bnot AB divisionA/B exponentiationA**3 means A 3 9/1/2011 27 MET 50, FALL 2011, CHAPTER 2 PART 1

28 Programming Basics Putting it all together… To evaluate:x = b 2 – 4ac with 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 28 MET 50, FALL 2011, CHAPTER 2 PART 1

29 Programming Basics Another example… To evaluate: c = √(  R d T) 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 29 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 30 MET 50, FALL 2011, CHAPTER 2 PART 1

31 Programming Basics We/you now know enough to: a) Try writing tiny bits of code b) Debug bad code – anything that even slightly violates one of the rules above! 9/1/2011 31 MET 50, FALL 2011, CHAPTER 2 PART 1


Download ppt "FORTRAN PROGRAMMING BASICS MET 50. Programming Basics The basic layout of all programs is as follows (p.33) PROGRAM name = heading (i.e., title) Specifications."

Similar presentations


Ads by Google