Sci Prog Atmos Sci
Sci Prog Atmos Sci MSC 321
Popular in Course
Popular in Marine Science
This 10 page Class Notes was uploaded by Rowan Ward on Thursday September 17, 2015. The Class Notes belongs to MSC 321 at University of Miami taught by Staff in Fall. Since its upload, it has received 14 views. For similar materials see /class/205715/msc-321-university-of-miami in Marine Science at University of Miami.
Reviews for Sci Prog Atmos Sci
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 09/17/15
Chapter 2 The Baby Steps Computers can do arithmetic logical operation storing and retrieving data and communicate with users via its inputoutput ports A program is needed to coordi nate these elementary operations so that useful tasks can be performed A program is then a list of directions that tells the machine what operation to perform next In procedural languages such as FORTRAN the directions are executed one at a time in the order they are encountered 21 The notorious Hello World A very simple program to get us started is listed below the textbook lists in the F77 version and here it is in its F90 glory Anything after an exclamation mark is a comment This is a simple code program helloworld start and name of program optional in F77 print Hello World print command stop program terminates end program helloworld end of program F77 accepts quotendquot only The programs simply prints the sentence 77Hello WorldV7 and stops Here are some remarks about the code 0 The program is just text entered with an editor such as emacs The text is referred to as source code 0 Any text after an exclamation mark is a comment and is not processed by the compiler It is there for documentation purposes The xed 0 The other lines of codes are FORTRAN statements Some of them such as print and stop are executable statements in that they direct the computer to do something such as print or stop the program and return control to the 11 12 CHAPTER 2 THE BABY STEPS operating system Other statements7 such as program and end program are non executable and are needed by the compiler to process the source code Each line not beginning with a comment sign is a statement The end of a line denotes the end of a statement unless the line is continued with a continuation sign in F90 that is a amp sign at the end of the line Comment lines cannot be continued and must start with a comment sign The text shown has been entered in free form as opposed to the xed form required by F77 The old dialect required that the rst 6 spaces be reserved for statement labels the rst 5 columns7 for a continuation character the 6th column The line also had to end on column 72 The F77 standard required that all characters be capitalized Many compiler permitted lower case characters7 however7 the requirement has been dropped since the F90 revision to the standard The Stop statement causes the program to terminate and control is handed back to the operating system The print statement causes the string constant Hello WorldV7 to be printed The instructs the compiler to use default formatting to print the string Syntax rules have to be followed for the compiler to recognize the elds within a statement The main focus here is on the semantics and meaning of the statements and on how best to use it to structure your calculation 22 Compiling the code The next step is to translate the FORTRAN source code into a machine language native to the computers processor The machine language calls on the processors instruction set to do the low level operations needed to carry out the execution of the program7 like adding add two numbers A compiler translates the FORTRAN source code into object les containing the machine language instructions In general programs must interact with the operating system to carry out the tasks like taking input from the keyboard and printing output on the screen This interaction takes place via system libraries7 essentially low level machine in structions7 that must be inserted before the program is ready for execution This insertion is called the linkage editing and is done with a program called the loader7 Linux command 1d On most unix machine the compiler takes care of calling the loader automatically to link the object les and the system libraries At this point an executable le is produced its name default defaults to aout on Linux ma chines Notice that you cannot read77 the object les nor the executable as they are store machine language instructions 23 RUNNING THE CODE 13 If there are syntax errors in the source code7 the compiler will trip and usually print a helpfull message indicating where the error occured These errors are often referred as bugs Errors that are not syntactic errors and pass the compiler test are often revealed at run time by producing unexpected answers7 most often a crash 23 Running the code On a linux machine the code is executed as follows 519 aout Hello World where the refers to the Linux prompt The aout is needed to indicate that the executable we want to run is located in the current directory indicated by the syntax When the command is issued the Operating System OS copies the le into memory and starts executing the instructions listed in it The program is now in charge of the computer until the stop statement is executed at which point control is returned to the OS 24 Wrap up The present sections presented a 77bird7s eye view77 of the process of creating7 com piling and running a code The details depend largely on the computer system and software used An essential premise of high level languages is that they free us from writing the same code in machine language for every kind of processors that we need to use Instead we decompose the work into two pieces the applica tion written in a high level language in this case FORTRAN7 and the compiler that takes any FORTRAN code and turns the speci c application into machine language instructions Now we need to expand on the language syntax and learn a few more tricks The rst lab is meant to ll in the details of using a Linux computer CHAPTER 2 THE BABY STEPS Chapter 3 Expressions and Assignment Statements The Hello World77 program was used to illustrate the layout of a FORTRAN source code Here is another example that is more numerical in character to illustrate additional concepts found in FORTRAN find the hypotenuse of a right triangle program hypotenuse implicit none Variable declaration real a length of one side real b length of perpendicular side real hyp length of hypotenus a 40 assignment of input data b 30 assignment of input data hyp sqrta2b2 calculation print hyp output stop end program hypotenuse Notice the layout of the program First is the declaration of variables and the input7 then comes the calculations and nally the output Comments are used to document the meaning of the different variables and the task of the code hy potenuse Finally indenting the code is a good practice to delineate Visually the 15 16 CHAPTER 3 EXPRESSIONS AND ASSIGNMENT STATEMENTS scope of variable and procedures Blocks of code are likewise spatially bundled to make the code compact and to indicate close linkage 31 Some intrinsic data types The code statements involve the numbers 775077 and 777077 including the decimal points and the number 2 without a decimal point These are examples of two FORTRAN intrinsic data types7 namely real and integers One has to also distinguish between constants and variables The number 40 is a real constant whereas the a is a real variable that stores a value of 40 311 Integers An integer constant is a number having no fractional part Examples include 3 a number with no fractional part 0 an integer zero 32154 a large negative integer lntegers are stored exactly and their computer representation does not involve any approximation The range of numbers that can be represented is however limited 312 Reals A number with a fractional part is a real Some examples of real constants are 10 a number with a fractional part of zero 3 141593 an approximation to 7 602E23 a large number representing 602 gtlt 1023 00 a real zero The variables 40 and 30 in the program are real constants We use them since we expect our calculation to yield results with fractional parts Real numbers are stored using a fraction exponent form that will be described in more details later on Their values are thus not stored exactly but only approximately Round off occurs when real numbers cannot be represented exactly 32 Variables and typing rules The source code in hypotenuse include several variables a7 b7 and hyp7 which are reminiscent of those used in algebra to represent arbitrary quantities ln FOR TRAN7 a variable is the name of an address in memory Variable names in fortran start with a letter7 can be up to 31 characters long7 and can include numerals as part of the name7 such as a1 and b1 33 NAMED CONSTANTS 17 Traditional fortran has relied on implicit typing rules whereby variables whose name starts with the letter7 i7 j7 k7 17 1117 or 117 otherwise they are real variables This practice of implicit typing rules is actually discouraged as countless bugs are introduced by simply mistyping the name of a variable lnstead programmers are urged to use implicit none to override the default typing behavior and force the programmer to declare every single variable used in the code Undeclared variables will be caught at compile time and flagged7 instead of the user tripping on these bugs at run time Programmers are urged to pick meaningful names for their variables to help in documenting their program Finally notice that every new variables must be assigned a value before using it in a calculation The hypotenuse code declares 3 real variables at the beginning two of these are input data and one is an output The a and b variables are assigned constant real values while the hyp is assigned its value after a calculation A traditional variable declaration takes the form intrinsicdatatype variablename For example integer e real pi Fortran 90 allows additional properties to be assigned to a variable7 these are attributes7 and the declaration takes the form intrinsicdatatype attributelist variablename 33 Named Constants A simple example of an attribute is to declare named constants using the parameter keyword Parameters are variables whose value does not change throughtout the calculations For example real parameter gravity9 810 gravitational acceleration in ms 2 One reason you may want to use a named constant is that 0 Avoid mistakes by not having to type long numbers multiple times7 eg like the constant 7139 0 Inform the compiler that the address cannot be written to and to crash if such an attempt is made Also some bene cial optimization can be performed o If we need to change the value of the constant we need retype once only and minimizing the chances of introducing bugs 18 CHAPTER 3 EXPRESSIONS AND ASSIGNMENT STATEMENTS Hence if for some reasons the units of the code need to be changed the gravity declaration would become real parameter gravity322 gravitational acceleration in fts 2 34 Arithmetic Operators The example7 aside from constants and variables7 uses arithmetic operators like addition and exponentiation Here is a complete list of numerical operators Addition Subtraction or negation Multiplication Division Exponentiation The minus sign can be a unary operator when it is used to indicate a negative quantity only one input is required as in 3 1415 All others are binary operators and require two inputs to carry out the calculation The quantities involved in a binary operation should be ofthe same type in general except that a whole number exponent should always be an integer Here are some side effects that we need to watch for when coding o lnteger division When one integer is divided by another the result is the integer part of the quotient7 obtained by chopping off and throwing away the fractional part not by rounding 0 Real exponents are more costly to calculate then integer exponent For ex ample X20 is more expensive then X2 o Occasionally the compiler will upgrade77 the variable when it sees mixed mode operations So 312 will be calculated as 30 12 o The order in which the operations are written makes a difference The order of precedence will be visited soon 35 Function References In addition to constants7 variables and operators7 the program contains a function reference to sqrt7 which returns the positive square root of its argument Many mathematical functions are built into FORTRAN and programmers can build their own The syntax of the function is meant to recall the standard mathematical 3 6 EXPRESSIONS 19 notation in which f means a function of x Some of the built in functions are sqrt X abs X eXpX em logx lnz modnm remainder of the division 36 Expressions A FORTRAN expression is made up of constants variables operators function references and parentheses supplemented with rules for computing the value of the expression The order of precedence determines the order in which expressions are evaluated The precedence table is listed below expressions within parentheses are evaluated rst and function references from right to left from left to right from left to right and unary negation Quantities in parentheses are found rst and have the same precedence as func tion evaluations Then comes exponentiation followed by multiplication and di vision on equal footing Finally addition and substractions are evaluated Where operators of equal precendence are present the expression is evaluated from left to right except for exponentiation which is evaluated from right to left Here are some FORTRAN examples paired with their mathematical intent 223 223 28 223 223 26 Xyzw Xyzw 1 3127 E Z Try to write expressions that are not ambiguous When in doubt use parenthesis 37 Assignment statements The sign in FORTRAN does not indicate an equality but is an assignment statement It directs the compiler to store the value on the right hand of the operator into the memory location indicated by the left hand of the operator So the FORTRAN code X3 O directs the compiler to store the value 3 into the address pointed to by the variable X The statement XX40 although mathematically nonsensical is perfectly valid FORTRAN code since the right hand side expression is evaluated before assignment the old value in X is overwritten by the new value 20 CHAPTER 3 EXPRESSIONS AND ASSIGNMENT STATEMENTS 38 InputOutput The print statement here prints a real variable hyp Several constants and vari ables of different types can be printed with a single print statement Usually it is a good idea to con rm the input so the code could have been printed as print a 7 a b b The unformatted input statement read serves to read input from the keyboard Actually in linuX it is taking input from standard input or stdin lt defaults to the keyboard unless the input is redirected from a le So for example the values of a and b can be obtained at run time and do not have to be hardcoded77 so no recompiling is needed in case different values for a and b need to be put in Here is the code program hypotenuse implicit none real abhyp read ab print a a b b hyp sqrta2b2 calculation print hyp output stop end program hypotenuse 39 Summary Fortran statement consists of operation acting on constants variables and func tions Syntax rules govern the relationship between the different elds within a statement A number of issues have not been mentioned yet including other FORTRAN data type the different representation of integer and reals and the attributes that can be assigned to variables
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'