3.36k likes | 3.66k Views
Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. Grading system: 1. Attendance & Performance: 30% 2. Midterm: 30% 3. Final: 40%. Office hours: (PH224) 1. Wed. 10:10~12:00 2. Fri. 09:10~10:00 11:10~12:00.
E N D
Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman Grading system: 1. Attendance & Performance: 30% 2. Midterm: 30% 3. Final: 40% Office hours: (PH224) 1. Wed. 10:10~12:00 2. Fri. 09:10~10:00 11:10~12:00
Ch. 1 Introduction to Computers and the Fortran Language Sec. 1.1 The Computer Fig 1-1 Main Memory Secondary Memory Output devices Input devices C P U (Central processing unit)
Sec 1.2 Data Representation in a Computer bit : Each switch represents one binary digit. ON 1 OFF 0 byte : a group of 8 bits e.g., 825 MB hard disk. ( 1MB = 106 byte) Sec 1.2.1 The Binary Number System The base 10 number system: 12310 = 1 x 102 + 2 x 101 + 3 x 100 = 123 102 101 100
The base 2 number system: 1012 = 1 x 22 + 0 x 21 + 1 x 20 = 5 1112 = 1 x 22 + 1 x 21 + 1 x 20 = 7 22 21 20 3 bits can represent 8 possible values : 0 ~ 7 (or 0002 ~ 1112 )
In general, n bits 2n possible values. e.g., 8 bits ( = 1 byte) 28 = 256 (-128 ~ +127) 16 bits ( = 2 byte) 216 = 65,536 (-32,768 ~ +32,767) Sec 1.2.2 Octal (or base 8) and Hexadecimal (or base 16) Representations of Binary Numbers (see Table 1-1) • Sec 1.2.3 Types of Data Stored in Memory • Character data : (western language, < 256, use 1 byte) • A ~ Z (26) • a ~ z (26) • 0 ~ 9 (10) • Miscellaneous symbols: ( ) { } ! … • Special letters or symbols: à ë …
Coding systems: (see App. A, 8-bit codes) • ASCII(American Standard Code for Information Interchange) • EBCDIC (Extended Binary Coded Decimal Interchange Code) • *The unicode coding system uses 2 bytes for each character. • (for any language) • Integer data: (negative, zero, positive) • For an n-bit integer, • Smallest Integer value = - 2n-1 • Largest Integer value = 2n-1 – 1 • e.g., a 4-byte (= 32-bit) integer, • the smallest = -2,147,483,648( = - 232-1) • the largest = 2,147,483,647( = 232-1-1) • *Overflow condition: An integer > the largest or < the smallest.
Real data: (or floating-point data) • The base 10 system: • 299,800,000 = 2.998 x 108 (scientific notation) exponent mantissa The base 2 system: e.g., a 4-byte real number = 24-bit mantissa + 8-bit exponent value = mantissa x 2exponent Precision:The number of significant digits that can be preserved in a number. e.g.,24-bit mantissa ± 223 (~ seven significant digits) Range: The diff. between the largest and the smallest numbers. e.g.,8-bit exponent 2-128 ~ 2127 (range ~ 10-38 to 1038)
Sec 1.3 Computer Languages • Machine language: The actual language that a computer • recognizes and executes. • High-level languages: Basic, C, Fortran, … • Sec 1.4 The History of the Fortran Language • Fortran = Formula translation • Fortran 66 Fortran 77 Fortran 90 Fortran 95 • (1966) (1977) (1991) (1996) Fortran 2003 (2004)
Ch. 2 Basic Element of Fortran Sec. 2.1 Introduction Write simple but practical Fortran programs ! Sec. 2.2 Fortran Character Set (case insensitive) • A ~ Z (26) • a ~ z (26) • 0 ~ 9 (26) • _ (underscore) • + - * / ** (arithmatic symbols) • ( ) . = , ‘ … (miscellaneous symbols)
Sec. 2.3 The Structure of a Fortran Statement a Fortran program = a series of statements • Executable statements: e.g., additions, subtractions, … • Non-executable statements: providing information. Free-source form: Fortran statements may be entered anywhere on a line, up to 132 characters long. e.g., 100 output = input1 + input2 ! Sum the inputs or 100 output = input1 & ! Sum the inputs + input2 (statement label, 1~99999, not a line number)
Sec. 2.4 The Structure of a Fortran Program Fig 2-1 (A simple Fortran program) PROGRAM my_first_program ! Purpose: … ! Declare the variables INTEGER :: i, j, k !All variable are integers ! Get the variables WRITE (*,*) " Enter the numbers to multiply:" READ (*,*) i, j k = i * j ! Write out the result WRITE (*,*) 'Result = ', k STOP END PROGRAM (Declaration Section) (Execution Section) (Termination section)
Sec. 2.4.4 Program Style • Textbook : • Capitalizing Fortran keywords ( e.g., READ, WRITE) • Using lowercase for variables, parameters Sec. 2.4.5 Compiling, Linking, and Executing the Fortran Program Fig 2-2 Fortran program Executable program Object file (Compile) (Link)
Sec. 2.5 Constants and Variables Valid variable names: time distance z123456789 I_want_to_go_home (up to 31 chracters, and the 1st character in a name must always be alphabetic) Invalid variable names: this_is _a_very_long_variable_name 3_days A$ ($ is an illegal character) my-help (“-” is an illegal character)
Five intrinsic types of Fortran constants and variables: • INTEGER • REAL • COMPLEX • LOGICAL • CHARACTER (numeric) (logical, Ch. 3) (character) Sec. 2.5.1 Integer Constant and Variables Integer constants: (no decimal point) e.g., 0 -999 +17 1,000,000(X) -100. (X)
Integer variables: 16-bit integers 32-bit integers (diff. kinds of integers, Ch. 11) Sec. 2.5.2 Real Constants and Variables Real constants: (with a decimal point) e.g., 10. -999.9 1.0E-3 (= 1.0 x 10-3 or 0.001) 123.45E20 0.12E+1 1,000,000. (X) 111E3 (X) -12.0E1.5 (X)
Real variables: 32-bit real numbers 64-bit real numbers (diff. kinds of real numbers, Ch. 11) Sec. 2.5.3 Character Constants and Variables Character constants: [enclosed in single (‘) or double (“) quotes)] e.g., ‘This is a test!’ “This is a test!” ‘ ‘ (a single blank) ‘{^}’ ‘3.141593’ (not a number) This is a test! (X) ‘This is a test!” (X) A character variable contains a value of the character data type.
Sec. 2.5.4 Default and Explicit Variable Typing Default typing: Any variable names beginning with the letters I, J, K, L, M, or N are assumed to be of type INTEGER. e.g., incr (integer data type) big (real data type) (Conti.)
Explicit typing: The type of a variable is explicitly defined in the declaration section. e.g., PROGRAM example INTEGER :: day, month, year REAL :: second LOGICAL :: test1, test2 CHARACTER :: initial (Executable statements) *No default names for the character data type!
Sec. 2.5.5 Keeping Constants Consistent in a Program Using the PARAMETER attribute : type, PARAMETER :: name=value e.g., REAL, PARAMETER :: pi=3.14159 CHARACTER, PARAMETER :: error=‘unknown’ Sec. 2.6 Assignment Statements and Arithmetic Calculations Assignment statement: variable_name = expression e.g., I = I + 1 ( I + 1 I )
Arithmetic operators: • binary operators: • a + b (a + b, addition) • a – b (a – b, subtraction) • a * b (a x b, multiplication) • a / b (a/b, division) • a ** b (ab, exponentiation) • unary operators: • + a • - b • Rules: • 1. No two operators may occur side by side. • e.g., • a*-b(X)a*(-b) • a**-2 (X) a**(-2)
2. Implied multiplication is illegal. e.g., x (y + z)(X)x*(y + z) 3.Parentheses may be used to group terms whenever desired e.g., 2**((8+2) / 5) Sec. 2.6.1 Integer Arithmetic e.g., 3/4 = 0, 6/4 = 1 7/4 = 1, 9/4 = 2 Sec. 2.6.2 Real Arithmetic (or floating-point arithmetic) e.g., 3./4. = 0.75, 6./4. = 1.50 7./4. = 1.75, 9./4. = 2.25
Sec. 2.6.3 Hierarchy (or order) of Operators • e.g., • x = 0.5 * a * t **2 • is equal to • x = 0.5 * a * (t **2) ? • or • x = (0.5 * a * t ) **2 ? • Order: • Parentheses, from inward to outward. • Exponentials, from right to left. • Multiplications and divisions, from left to right. • Additions and subtractions, from left to right.
Example 2-1 a = 3. b = 2. c=5. d=4. e = 10. f = 2. g= 3. (1) output = a * b + c * d + e / f **g (2) output = a * (b + c) * d + (e / f) **g (3) output = a * (b + c) * (d + e) / f **g Solu. : (1) output = 3. * 2. + 5. * 4. + 10. / 2. ** 3. = 6. + 20. + 1.25 = 27.25 (2) output = 3. * (2. + 5.) * 4. + (10. / 2.) ** 3. = 84. + 125. = 209. (3) output = 3. * (2. + 5.) * (4. + 10.) / 2. ** 3. = 3. * 7. * 14. / 8. = 294. / 8. = 36.75
Example 2-2 a = 3. b = 2. c=3. (1) output = a ** (b ** c) (2) output = (a ** b) ** c (3) output = a ** b ** c Solu.: (1) output = 3. ** (2. ** 3.) = 3. ** 8. = 6561. (2) output = (3. ** 2.) ** 3. = 9. ** 3. = 729. (3) output = 3. ** 2. ** 3. = 3. ** 8. = 6561.
Sec. 2.6.4 Mixed-Mode Arithmetic In the case of an operation between a real number and an integer, the integer is converted by the computer into a real number. e.g., 3. / 2 = 1.5 1 + 1/4 = 1 1. + 1/4 = 1. 1 + 1./4 = 1.25 Automatic type conversion: e.g., nres = 1.25 + 9/4 ave = (5 + 2) / 2 = 1.25 + 2 = 7/2 = 3.25 = 3. = 3 (a integer variable) (a real variable)
Logarithm • Base 10: • If 10x= N, then x = ? log N = x • e.g., N = 100 log 100 = log (102) = 2 • N = 3 log 3 = 0.47712… • Base e (=2.71828…): (Natural logarithm) • If ex = N, then x = ? ln N = x • e.g., N = e2 ln (e2) = 2 • N = 3 ln 3 = 1.09861… • * If N < 0 ( log N ) or ( ln N ) is undefined !
Sec. 2.6.5 Mixed-Mode Arithmetic and Exponentiation Ifboth result and y are real, and n is an integer, result = y ** n = y * y * y…*y (real arithmetic, not mixed-mode) But if result, y and x are all real, result = y ** x = ?
use yx = e x ln y(∵ e x ln y = e ln (yx) = yx ) e.g., (4.0) ** 1.5 = 8. (8.0)**(1./3)=2. (-2.0) ** 2 = 4. [∵ (-2.0) * (-2.0) = 4.] (-2.0) ** 2.0 [X, ∵ln (-2.0) is undefined!]
Sec. 2.7 Intrinsic Functions • Intrinsic functions are the most common functions built directly • into the Fortran language. ( see Table 2-4 and App. B) • External functions are supplied by the user. (see Ch. 7) e.g., y = sin(3.141593) INT(2.9995) = 2 (truncates the real number) y = sin(x) y = sin(pi*x) NINT(2.9995) = 3 (rounds the real number) y = sin(SQRT(x))
Generic functions: (can use more than one type of input data) e.g., If x is a real number, ABS(x) is real. If x is an integer, ABS(x) is integer. Specific functions: (can use only one specific type of input data) e.g., IABS(i) (integer only) *See Appendix B for a complete list of all intrinsic functions.
Sec. 2.8 List-directed (or free-format) Input and Output Statements • The list-directed input statement: • READ (*,*) input_list • I/O unitformat • The list-directed output statement: • WRITE (*,*) output_list • I/O unitformat
e.g., PROGRAM input_example INTEGER :: i, j REAL :: a CHARACTER (len=12) :: chars READ(*,*) i, j, a, chars WRITE(*,*) i, j, a, chars END PROGRAM Input: 1, 2, 3., ‘This one.’ (or 1 2 3. ‘This one.’) Output: 1 2 3.00000 This one. (Try it out!)
Sec. 2.9 Initialization of Variables E.g., PROGRAM init INTEGER :: i WRITE(*,*) I END PROGRAM Output: i = ??? (uninitialized variable) Run-timeerror! (depends on machines) (Try it out!)
Three ways to initialize variables: • 1. Assignment statements: • e.g., • PROGRAM init_1 • INTEGER :: i • i = 1 • WRITE(*,*) i • END PROGRAM • READ statements: • e.g., • PROGRAM init_2 • INTEGER :: i • READ(*,*) i • WRITE(*,*) i • END PROGRAM
3. Type declaration Statements: type :: var1 = value1, [var2 = value2, …] e.g., REAL :: time = 0.0, distance = 5128. INTEGER :: loop = 10 LOGICAL :: done = .FALSE. CARACTER (len=12) :: string = ‘characters’ or PROGRAM init_3 INTEGER :: i = 1 WRITE(*,*) i END PROGRAM
Sec. 2.10 The IMPLICIT NONE Statement When the IMPLICIT NONE statement is included in a program, any variable that does not appear in an explicit type declaration statement is considered an error. e.g., PROGRAM test_1 REAL :: time time = 10.0 WRITE(*,*) ‘Time=‘,tmie END PROGRAM Output: Run-timeerror! (depends on machines)
+ IMPLICIT NONE, PROGRAM test_1 IMPLICIT NONE REAL :: time time = 10.0 WRITE(*,*) ‘Time=‘,tmie END PROGRAM Output: Compile-timeerror! (depends on machines)
Sec. 2.10 Program Examples Example 2-3 (Temperature conversion) T (0F) = (9/5) T(0C) + 32 Fig. 2-6 PROGRAM temp IMPLICIT NONE REAL :: temp_c, temp_f WRITE(*,*) ’Enter T in degrees C:’ READ(*,*) temp_c temp_f = (9./5.) * temp_c + 32. WRITE(*,*) temp_c,’ degrees C =‘, temp_f, & ‘degrees F’ END PROGRAM (Try it out!)
Example (extra) Write a program for converting a 4 bits integer into a base 10 number, e.g., 1 0 1 1 = 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20 = 11
Problem: (extra) The geometric mean for three real positive values is defined as xave= (x1x2x3)1/3 . Write a Fortran program to calculated xave. Use the IMPLICIT NONE statement Test: x1=1.0, x2=2.0, x3=4.0, xave= 2.0
Ch. 3 Program Design and Branching Structures Ch. 2: Sequential programs(simple and fixed order) Here: Complex programs (using two control statements) (1) branches (2) loops
Sec. 3.1 Introduction to Top-down Design Techniques Fig. 3-1 (a formal program design process) Start State the problem Design the algorithm Convert algorithm into Fortran statements Test the program Finished !
Sec. 3.2 Use of Pseudocode and Flowcharts (1) Pseudocode : a mixture of Fortran and English (2) Flowcharts : graphical symbolsl e.g., (1) The pseudocode for Example 2-3: Prompt user to enter temp. in degree Farenheit Read temp. in degree Farenheit temp_k in Kelvins (5./9.)*(temp_f-32.)+273.15 Write temp. in Kelvins
(2) The flowcharts for Example 2-3: (an oval for start or stop) Start Tell user to enter temp_f (a parallelogram for I/O) Get temp_f Calculate temp_k Write temp_k Stop (a rectangle for computation)
Sec. 3.3 Logical Constants, Variables, and Operators Sec. 3.3.1 Logical Constants and Variables Logical constants: e.g., .TRUE. .FALSE. TRUE (X) .FALSE (X) A logical variable contains a value of the logical data type. e.g., LOGICAL :: var1 [var2, var3, …]
Sec. 3.3.2 Assignment Statements and Logical Calculations • Assignment statements: • logical variable name = logical expression • Logical operators: • relational operators • combinational operators Sec. 3.3.3 Relational Operators a1 op a2 a1, a2: arithmetic expressions, variables, constants, or character strings. op: the relational logical operators. (see Table 3-1)
Table 3-1 operation meaning = = equal to / = not equal to > greater than > = greater than or equal to < less than < = less than or equal to e.g., operation result 3 < 4 .TRUE. 3 < = 4 .TRUE. 3 = = 4 .FALSE. ‘A’ < ‘B’ .TRUE. (in ASCII, A 65, B 66, p.493) 7+3 < 2+11 .TRUE.
Sec. 3.3.4 Combinational Logic Operators l1 .op. l2 and .NOT. l1(.NOT. is a unary operator) l1, l2: logical expressions, variables, or constants. op: the binary operators. (see Table 2-4) Table 3-2 operation meaning .AND. logical AND .OR. logical OR .EQV. logical equivalence .NEQV. logical non-equivalence .NOT. logical NOT
The order of operations: • Arithmetic operators. • All relational operators, from left to right. • All .NOT. operators. • All .AND. operators, from left to right. • All .OR. operators, from left to right. • All .EQV. And .NEQV. operators, from left to right.
Example 3-1 L1 = .TRUE., L2 = .TRUE., L3 = .FALSE. (a) .NOT. L1 .FALSE. (b) L1 .OR. L3 .TRUE. (c) L1 .AND. L3 .FALSE. (d) L2 .NEQV. L3 .TRUE. (e) L1 .AND. L2 .OR. L3 .TRUE. (f) L1 .OR. L2 .AND. L3 .TRUE. (g) .NOT. (L1 .EQV. L2) .FALSE.