Lab 4B BSYSE 512 Research and Teaching Methods Introduction to Computing and Basic Fortran 1. Miscellaneous Knowledge and Acronyms (primarily compiled from Nyhoff and Leestma [1996]) 1.1. Acronyms • • • • • Fortran: Formula translation ANSI: American National Standard Institute ISO: International Standards Organization ASCII: American Standard Code for Information Interchange IBM: International Business Machines Corporation 1.2. Development of Computing Systems • • • • • • Two important concepts: mechanization of arithmetic and stored program Mechanical devices: abacus used by Chinese 3–4 kyr ago; slide rule invented by English mathematician W. Oughtred in early 1600s; one of the first mechanical adding machines invented by French mathematician B. Pascal in 1642, which was improved to perform multiplication by German mathematician G.W. von Leibniz; similar calculators became important tools in science, business and commerce by the end of the 19th century Stored program: weaving loom designed by Frenchman J.M. Jacquard using punched cards to position threads for weaving process Combining the two concepts: Difference Engine designed by English mathematician C. Babbage in 1822 to compute polynomials; Analytical Engine designed later by C. Babbage and his associate A. Augusta (considered by some to be the first programmer), which had multipurpose components for arithmetic computations, storing data and intermediate results, and inputting and outputting information; a punched-card system designed by H. Hollerith for US census bureau in 1890, who later formed his own tabulating company, which in 1924 became IBM; continued efforts in developing computing devices in the US by pioneers including H. Aiken, J. Atanasoff, J.P. Eckert, J.W. Mauchly, and J. von Newmann led to the first electromechanical Mark I computer in 1944 at IBM Electronic computers: first fully electronic computer developed by J. Atanasoff at Iowa State Univ. between 1939–1942, followed by the well known ENIAC (Electronic Numerical Integrator and Computer) constructed by J.P. Eckert and J.W. Mauchly at Univ. Penn. Computers categorized by the type of hardware (the physical components used in constructing a computer system): first-generation (e.g., ENIAC) with extensive use of vacuum tubes; secondgeneration (IBM 7090, built in 1958) using transistors in place of vacuum tubes; third-generation (IBM System/360, built in 1964) using integrated circuits with better system utilization; microprocessor (4004) introduced by R. Noyce at Intel Corporation; fourth-generation developed in 1980–1990 using very large-scale integrated circuits (VLSI) on silicon chips; development of personal computers made possible owing to the birth of microprocessors, represented by Apple II designed by S. Jobs and S. Wozniak in 1977, and the first of IBM’s PCs in 1981 1.3. Terms Hardware • CPU: Central Processing Unit, the heart of a computing system that controls the entire operation of the system, performs the arithmetic and logic operations, and stores and retrieves instructions and data. A CPU generally consists of a control unit, an arithmetic and logic unit, and a memory unit. 1 Lab 4B BSYSE 512 Research and Teaching Methods • • • • Components of the memory unit: (1) Random Access Memory (RAM, also called internal, main, or primary memory) for storing the instructions and data of the programs being executed, (2) registers, which are a set of special high-speed memory locations, and (3) Read-Only Memory (ROM). Values that are stored in registers can typically be accessed thousands of times faster than can values stored in RAM. Both RAM and registers are volatile memory components in that information stored in these components is lost if the power to the computing system is shut off. ROM is nonvolatile memory used to store important (too important to lose) information, such as start-up instructions External memory: also called auxiliary or secondary memory of which a common form is CDROMs. The time required to access data stored on such media can be much greater than the access time for data stored in RAM Peripheral devices: include external memory and I/O devices (e.g., terminals, scanners, printers) Memory organization: computer memory unit uses binary scheme, i.e., the two binary digits (bits) 0 and 1, to represent information. Memory is commonly measured in bytes (1byte = 8 bits), and a block of 210 = 1,024 bytes is called 1 K of memory. Hence, one megabyte (= 1,024 K) consists of 1,024×1,024 = 1,048,576 bytes, or, equivalently, 1,048,576×8 = 8,388,608 bits. Bytes are usually grouped together into words. The number of bits in a word is equal to the number of bits in a CPU register or to the length of the units of information handled by a computer’s microprocessor (chip). The longer these units, the faster the computer. Typical word sizes are 16 or 32 bits, and microcomputers are often referred to as 8-, 16- or 32-bit machines Software • System software: collections of programs that facilitate and monitor computer use, including operating systems, utilities, compilers, and database management systems; usually provided by either the hardware vendor or a software supplier • Operating system: software that controls the activities of a computer (e.g., job control, scheduling, allocating system resources, I/O, memory management) and acts as an interface between the user and the computer • Typical operating systems: UNIX, developed in 1971 by K. Thompson and D. Ritchie at AT&T’s Bell Laboratories; MS-DOS developed in 1981 by W.H. Gates, founder of the Microsoft Corporation; the recent GUIs (Graphical User Interfaces) such as MIT’s X Window System for UNIX machines, Microsoft’s WindowsTM, and Apple’s Macintosh OSX. • High-level language: computer language that is similar to natural language, e.g., Fortran, BASIC, COBOL, Pascal, Ada, Modula-2, C, C++ • Procedural language: a high-level, machine-independent language that enables the user to describe the steps for the solution of a problem via a set of algorithms or procedures • Source program: a program written in a high-level language • Machine language: the language directly used by a computer • Object program: a program written in a machine language • Compilers: programs that translate source programs into object programs 1.4. Development of Fortran • • • First developed by J. Backus and a team of 13 programmers for the IBM 704 computer during 1954–1957; the fourth revision completed in 1962; the fifth revision appeared in 1977, known as FORTRAN 77; an extensive revision known as Fortran 90 was prepared in 1990 Both FORTRAN 77 and Fortran 90 (note the official spelling with only F in upper case in Fortran 90) are specified as American standards by ANSI, while Fortran 90 is the only international Fortran standard as decided by ISO Organizations in charge of Fortran development: X3J3, an ANSI subcommittee, is dedicated to 2 Lab 4B BSYSE 512 Research and Teaching Methods Fortran development. WG5, the ISO counterpart of X3J3, owns responsibility for Fortran development on an international basis. WG5 has previously tasked X3J3 to develop Fortran, and this arrangement continues. WG5 is composed of Fortran users, vendors, and academics from several ISO supporting nations [The Fortran Company, 2002] 1.5. Comparison of Commonly Used High-Level Languages (compiled from Wanielista et al. [1997]) • • • • • BASIC (Beginner’s All-purpose Symbolic Instruction Code): an interpreted (without the need for compiling), procedural language, DOS-based and not typically available in other operating systems Visual BASIC: mainly for the Microsoft WindowsTM operating system Fortran: a procedural, portable and standardized language allowing programs in Fortran to be compiled for multiple operating systems C and C++: both are procedural, compiled and portable languages Others: other commonly used procedural, high-level languages include Ada, Pascal, COBOL, SmallTalk, and Modula-2 1.6. Fortran Applications in Hydrologic Modeling It may be fair to say that most existing hydrologic models have been developed in Fortran, many of them being shareware, including the industry standard ground-water flow model MODFLOW [Harbaugh, 2000], the watershed hydrologic models of HSPF [Donigian et al., 1995], TOPMODEL [Beven et al., 1995], and PRMS [Leavesley and Stannard, 1995], the widely used HEC (Hydrologic Engineering Center, US ACE) series for solving stream and reservoir flow problems, and the sophisticated vadose-zone hydrologic models of HYDRUS series that are developed at the US Salinity Laboratory, the SHAW model [Flerchinger and Saxton, 1989], and the GLEAMS model [Knisel, 1993]. For more applications, refer to Anderson and Woessner [1992], Singh [1995], and Kramer and Cullen [1995]. 2. Elementary Fortran (compiled from Hahn [1994] and Chapman [1998]) 2.1. Program Layout Program Layout and Structure Programs that have been written can be difficult to follow when read some time later, even for the programmer himself. It is therefore extremely important to develop the art of writing programs (also known as programming style) that are well laid out, with all the logic clearly described. Generally, there should be a comment at the beginning describing what the program does; all the variables should be declared and described on separate lines, in alphabetical order. In Fortran 90, you may like to include initialization with the declaration and description, e.g. REAL :: T = 6 ! time, hr In addition, blanks are used on either side of the equal signs and the operators, and after commas, while blank lines are used to separate distinct parts of a program. You may like to develop your own style; the point is that you must pay attention to readability. The general structure of a simple Fortran program is as follows: PROGRAM program name 3 Lab 4B BSYSE 512 Research and Teaching Methods declaration statements executable statements END The END statement informs the compiler that there are no further Fortran statements to compile. Statements Statements form the basis of any Fortran program, and may contain from 0 to 132 characters (a statement may be blank; the use of blank statements is encouraged to make a program more readable by separating logical sections.) Earlier versions of Fortran insisted that certain parts of a statement start in certain columns; Fortran 90 has no such restriction. All statements, except the assignment statement (e.g., BALANCE = 1000), start with a keyword. Some keywords may be END, PRINT, PROGRAM, and REAL. Generally, there will be one statement per line. However, multiple statements may appear on a line if they are separated by semi-colons. For the sake of clarity, this is recommended only with very short assignments, such as A = 1; B = 1; C = 1 Continuation Long statements may continue over several lines. In Fortran 90, if a statement is too long to fit on a line, it will be continued on the next line if the last non-blank character in it is an ampersand (&): A = 174.6 * & (T ! 1981.2) ** 3 Continuation is normally to the first character in the next non-comment line. However, if the first non-blank character of the continuation line is &, continuation is to the first character after the &. In this way a token (tokens are the units, such as identifier/variable name, assignment or arithmetic operators, or integer constants, that are recognized and analyzed by a compiler to generate an object program) may be split over two lines, although this is not recommended, since it makes the code less easy to read. An & at the end of a comment line will not continue the comment, since the & is construed as part of the comment. In FORTRAN 77, the continuation of a line is indicated by a non-zero, non-blank character in the sixth column. Comments Any characters following an exclamation mark (!) (except in a character string) are commentary, and are ignored by the compiler. An entire line may be a comment, if started with “C” or “*”or “!” in FORTRAN77 and with “!” in Fortran 90. A blank line is also interpreted as comment. Comments should be used liberally to improve readability. 2.2. Data Type The concept of a data type is fundamental in Fortran 90. A data type consists of a set of data values (e.g., the whole numbers), a means of denoting those values (e.g., !2, 0, 999), and a set of operations (e.g., arithmetic, logic) that are allowed on them. The Fortran 90 standard requires five intrinsic or built-in data 4 Lab 4B BSYSE 512 Research and Teaching Methods types, which are divided into two classes. The numeric types are integer, real and complex. The non-numeric types are character and logical. Associated with each data type are various kinds, defined depending on the number of bits available for storage, so that, for example, there might be two kinds of integer: short and long. In addition to the intrinsic data types, you may define your own derived data types, each with their own set of values and operations. 2.3. Names and Vairables In Fortran, names indicate memory locations. In FORTRAN 77, names consist of no more than six (6) alphanumeric characters and must start with a letter. In Fortran 90, names can consist of between 1 and 31 alphanumeric characters and still must start with a letter. The alphanumeric characters are the 26 letters, the 10 digits, and the underscore (_). Names are case insensitive, except in the case of character strings. A name in a program must be unique. A variable is a memory location whose value may be changed during execution of a program. A variable’s name is constructed following the aforementioned rules. A variable has a type that determines the type of number it may hold, as specified in a type declaration, e.g., INTEGER : : X REAL : : INTEREST CHARACTER : : LETTER REAL : : A = 1 2.4. An Example of Numeric Expression Refer to the following numeric expression U * T ! G / 2 * T ** 2 It is a formula combining constants, variables and functions (e.g., exponential) using numeric intrinsic operators. It specifies a rule for computing a value (vertical distance, in this case). Since it only computes a single value it is a scalar numeric expression. There are five numeric intrinsic operators, ** for exponentiation, highest precedence; * for multiplication, / for division, lower precedence; + for addition, and ! for subtraction, lowest precedence. Typing blanks on either side of operators will make expressions more readable. An operator with two operands, as in A + B, is called a binary or dyadic operator. When an operator appears with only one operand, as in !Z, it is called unary or monadic. The order in which operations in an expression are carried out is determined by the precedence of the operators, except that parentheses () always have the highest precedence. Since multiplication has a higher precedence than addition, this means, for example, that 1 + 2 * 3 is evaluated as 7, while (1 + 2) * 3 is evaluated as 9. Note also that !3 ** 2 evaluates to !9, not 9. When operators with the same precedence occur in the same expression, they are with one exception always evaluated from left to right, so 1 / 2 * A is evaluated as (1 / 2) * A and not 1 / (2 * A). The exception to the precedence rules is that in an expression of the form A ** B ** C 5 Lab 4B BSYSE 512 Research and Teaching Methods the right-hand operation B ** C is evaluated first. 2.5. Integer Division Integer division causes so much headache among unsuspecting beginners that it deserves a section of its own. When an integer quantity (constant, variable or expression) is divided by another integer quantity the result is also of integer type, so it is truncated towards zero, i.e. the fractional part is lost. For example, 10 / 3 19 / 4 4/5 !8/3 3 * 10 / 3 10 / 3 * 3 evaluates to 3 evaluates to 4 evaluates to 0 (which could subsequently cause an unwanted division by zero) evaluates to !2 evaluates to 10 evaluates to 9 Fortran 90 allows operands in an expression to be of different type. The general rule is that the weaker or simpler type is converted, or coerced, into the stronger type. Since integer type is the simplest, this means that operations involving real and integer operands will be done in real arithmetic. This applies to each operation separately, not necessarily to the expression as a whole. So, for example, 10 / 3.0 4. / 5 2 ** (!2) 3 / 2 / 3.0 evaluates to 3.33333 evaluates to 0.8 evaluates to 0 (Why?) evaluates to 0.333333 because 3 / 2 is evaluated first, giving integer 1 2.6. Numeric Assignment The purpose of the numeric assignment is to compute the value of a numeric expression and assign it to a variable. Its general form is variable = expression The equal sign does not have the same meaning as the equal sign in mathematics, and should be read as “becomes”. So the assignment X=A+B should be read as “(the content of) X becomes (the content of) A plus (the content of) B”. In this way the assignment N=N+1 is meaningful, and means “increase the value of N by 1”, whereas the mathematical equation n=n+1 is not generally meaningful. If the expression is not of the same type as the variable, it is converted to that type before assignment. 6 Lab 4B BSYSE 512 Research and Teaching Methods This means that there might be loss of precision. For example, assuming N is integer, and X and Y are real: N = 10. / 3 X = 10 / 3 Y = 10 / 3. (value of N is 3) (value of X is 3.0) (value of Y is 3.33333) The danger of performing integer divisions inadvertently cannot be stressed too much. For example, you might want to average two marks which happen to be integers M1 and M2. The most natural statement to write is FINAL = (M1 + M2) / 2 but this operation loses the decimal part of the average. It is always safer to write constants as reals if real arithmetic is what you want: FINAL = (M1 + M2) / 2.0 References Anderson, M.P., and W.W. Woessner, Applied Groundwater Modeling: Simulation of Flow and Advective Transport, Academic Press, San Diego, 1992. Beven, K.J., R. Lamb, P. Quinn, R. Romanowicz, and J. Freer, TOPMODEL, in Computer Models of Watershed Hydrology, edited by V.P. Singh, p. 627–668, 1995. Chapman, S.J., 1998, Fortran 90/95 for Scientists and Engineers, McGraw-Hill, Boston. Donigian, A.S., Jr., B.R. Bicknell, and J.C. Imhoff, Hydrologic Simulation Program - Fortran (HSPF), in Computer Models of Watershed Hydrology, edited by V.P. Singh, p. 395–442, 1995. Flerchinger, G.N., and K.E. Saxton, Simultaneous heat and water model of a freezing snow-residue-soil system I. Theory and development, Trans. ASAE. 32, 565–571, 1989. Hahn, B.D., Fortran 90 for Scientist and Engineers, Butterworth-Heinemann, Woburn, MA, 1994. Harbaugh, A.W., E.R. Banta, M.C. Hill, and M.G. McDonald, MODFLOW-2000, the US Geological Survey modular ground-water model—User guide to modularization concepts and the ground-water flow process: US Geol. Surv. Open-File Rep. 00-92, 121 p., 2000. Knisel, W.G. (ed.), GLEAMS manual, ver. 2.10, UGA-CPES-BAED Pub. 5, Univ. Georgia, Coastal Plain Experiment Station, Tifton, GA, 1993. Kramer, J.H., and S.J. Cullen, Review of vadose zone flow and transport models, Handbook of Vadose Zone Characterization & Monitoring, edited by L.G. Wilson, L.G. Everett and S.J. Cullen, Lewis Publishers, Boca Raton, 1995. Leavesley, G.H., and L.G. Stannard, The Precipitation-Runoff Modeling System - PRMS, in Computer Models of Watershed Hydrology, edited by V.P. Singh, p. 281–310, 1995. Morris, C. (ed.), Academic Press Dictionary of Science and Technology, Academic Press, San Diego, 1992. Nyhoff, L., and S. Leestma, FORTRAN 77 for Engineers and Scientists, 4th ed., Prentice Hall, Upper Saddle River, 1996. Singh, V.P. (ed.), Computer Models of Watershed Hydrology, Water Resources Publications, Highlands Ranch, 1995. The Fortran Company, Information, Source: URL <http://www.fortran.com/fortran/market.html>, accessed in January, 2002. Wanielista, M., R. Kersten, and R. Eaglin, Hydrology: Water Quantity and Quality Control, 2nd ed., John Wiley & Sons, Inc., New York, 1997. 7 Lab 4B BSYSE 512 Research and Teaching Methods Lab 4 Assignment 1. Make connections (3.5 pts) (1) John Backus (2) byte (3) John von Neumann (4) UNIX (5) CPU (6) ROM (7) Fortran (A) a premier operating system (B) central processing unit (C) a high-level language (D) computer memory unit (E) read-only memory (F) designer of Fortran language (G) developer of the scheme to use internally stored commands 2. Are the following names valid or invalid? Why? (4 pts) X X+Y R2D2 SHADOW FAX Pay_day 2A ENDOFTHEMONTH OBI-WAN 3. Decide which of the following constants are not written in standard Fortran. (2.5 pts) (A) (B) (C) (D) (E) 9,87 .0 3.57*E2 1.23E3 3,57E!2 4. Evaluate the following numeric expressions by hand and with a calculator if needed. Then, write a simple Fortran code to verify your result for any chosen case. The given values for the individual variables are A = 2, B = 3, C = 5 (reals); and I = 2, J = 3 (integers) (7.5 pts) A*B+C A * (B + C) B/C*A B / (C * A) A/I/J A * B ** I / A ** J * 2 C + (B / A) ** 3 / B * 2 8 Lab 4B BSYSE 512 Research and Teaching Methods A ** B ** I ! B ** A ** C J / (I / J) Hint: a sample code might look like the following PROGRAM CALC REAL : : A, B, C, X INTEGER : : I, J A = 2; B = 3; C = 5 I = 2; J = 3 X = A * B ** I / A ** J * 2 PRINT *, 'X = ', X END 5. Derive Eq. 11, the continuity equation for stream flow, introduced in the lab discussion (the notes of which are posted on line). (2.5 pts) 6. In detail, describe ONE physical, chemical or biological process involved in the biological system you will investigate in your graduate research, and the materials and methods (mathematical, experimental) to quantify this process. (5 pts) 9