216x Filetype PDF File size 0.37 MB Source: www.wiwi.uni-wuerzburg.de
2 AnIntroductionto Fortran 90 This chapter aims at giving a short introduction to Fortran 90. As describing all features of the Fortran language would probably fill some hundred pages, we will concentrate onthebasicfeatures that will be needed to follow the rest of this textbook. Nevertheless, therearevariousFortrantutorialsoutintheinternetthatcouldbeusedascomplementary literature. 2.1 AboutFortraningeneral 2.1.1 Thehistory of Fortran Fortran is pretty old. Actually it is considered the first realized higher programming lan- guage. It goes back to a proposal by John W. Backus, an IBM programmer, in 1953. The termFortranisderivedfromTheIBMFormulaTranslationSystem. Beforethereleaseofthe first Fortran compiler in April 1957, people used to use assembly languages. The intro- duction of a higher programming language compiler reduced the number of code lines needed to write a program tremendously and therefore FORTRAN, the first release of the Fortran programming language, grew pretty fast in popularity. From 1957 on several versions followed the initial FORTRAN version, namely FORTRAN II and FORTRAN III in 1958 and FORTRAN IV in 1961. In 1966, the American Standards Association (now knownasthe ANSI) approved a standardized American Standard Fortran.Theprogram- minglanguagedefinedonthisstandardwascalledFORTRAN66. Approvinganupdated standardin1977,theANSIpavedthewayforanewFortranversionknownasFORTRAN 77. This version became pretty popular in computational economics during the 80ies and beginning 90ies. More than 13 years later, the Fortran 90 standard was released by both ISO and ANSI in consecutive years. With FORTRAN 90, the fixed format standard was exchangedbyafreeformatstandard. Inaddition, manynewfeatures like modules, recur- sive procedures, derived data types and dynamic memory allocation made the language muchmoreflexible. From Fortran 90 on, there has only been one major revision which basically introduced object-oriented programming features into the Fortran language in 2003. However, as object-oriented programming will not be needed and Fortran 90 is by far the more popular language, we will focus on the 1990 version in this book. 4 Chapter 2 – AnIntroduction to Fortran 90 2.1.2 WhytouseFortran Fortran is a general purpose, procedural, imperative, high-level programming language. Indetailthis meansthat(a)Fortranwasnotcreatedforaspecialpurposelikee.g. Matlab, MathematicaorGausswereforefficientlydealingwithmatricesorsolvingmathematical problems, (b) Fortran code that will be used severaltimescanbestoredinfunctionsand subroutines that can becalled from other program parts, (c) a Fortran program consists of statements that will be executed one after the other (i.e. a Fortran program starts with the first line and ends with the last), and (d) Fortran abstracts from the details of a computer, i.e. having the right compiler, Fortran code could be run on every computer independent of its hardware configuration and operating system. At this point our students usually ask "So, why to use Fortran? Why not a special pur- pose language or something more "fashionable" like Java?". Let us come up with some justifying points. First, special purpose languages might be relatively efficient in the spe- cific purpose they were created for, however, slow in other areas. Matlab, for example, is very good in working with matrices of regularorsparsenature.However,ifyouhave ever tried to run some do-loops in Matlab, you will know what we mean. Second, when it comes to the more "fashionable" languages like Java or C++, one has to always keep in mind that those languages are usually object-oriented and abundant. They are very goodforcreating software with Graphical User Interfaces, but not for running numerical maths code quickly and efficiently. As computational economics usually consists of at least 80% numerical methods, we need a programming language that is efficient in exe- cuting do-loops and if-statements, calling functions and subroutines and allows to per- manently store general code that is frequently used. Fortran is a language that matches all those features and not much more. The only real alternative we know would be C. ThereisnorealreasonwhynottouseCinsteadofFortran,it’sjustamatterofpreference. Last but not least, Fortran has been used a lot by engineers, numerical mathematicians and computational economists during the 80ies and 90ies where a lot of optimizers and interpolation techniques were written. Fortunately, Fortran 90 is backward compatible with FORTRAN77,i.e. all thecode that is out there can be easily included in our Fortran programs. All in all, it seems reasonable for us to have chosen a language that matches all the fea- tures we need but does not come with any unnecessary extras that just make execution less efficient. 2.1.3 Thewayhigh-level programminglanguages work Low-level assembly languages that are basically used to program computers, micropro- cessors, etc. do not abstract from the computer’s instruction set architecture. Therefore, the syntax of assembly languages depends on the specific physical or virtual computer used. Inaddition, assemblylanguagesarequitecomplicatedandevenasmallprogramm 2.1. About Fortran in general 5 requires many lines of computer code. Inoppositetothat, high-levelprogramminglanguagesabstractfromthesystemarchitec- ture and are therefore portable between different systems. As in all other languages, one has to learn the vocabulary, grammar, and punctuation called syntax of that language. However,comparedtoreallanguages,thesyntaxofahigh-levellanguagecanbelearned within some hours. In addition, the semantics, i.e. the meaning or interpretation of the different symbols you use, usually follows principlesthat can be comprehendedby using purelogic. As high-level languages abstract from the computer’s instruction set architecture, one needsacompiler, sometimes also called interpreter, that interprets the statements given in the programandtranslatesthemintoacomputer’snativelanguage,i.e. binarycode. The wayhigh-level programming works can be seen from Figure 2.1. First, the programmer SourceCode Programmer Compilation Other Libraries Object Code Compiler Linkage Operating Executable (binary) System/ Hardware Figure2.1: Thewayhigh-levelcompilers work hasto write his source code in the respective high-level language and hand it over to the compiler. The compiler now proceeds in two steps. In the compilation step he interprets the source code and creates a so called object code file which consists of binary code. Binary code files usually have the suffix .obj. During this step, the compiler usually performs a syntax validation scan. If there are any syntactical errors, it produces error messages and stops the compilation process. In order to create an executable file, which hasthesuffix.exeinMicrosoft®Windows,thecompilernowlinkstheobjectcodewithall other libraries that are needed to run the program, e.g. external numerical routines. The resulting executable can nowbesenttotheoperatingsystemwhichadvisesthehardware to do what exactly the programmer wants. 6 Chapter 2 – AnIntroduction to Fortran 90 2.1.4 Fortran Compilers for Windows® and Linux For beginners, we suggest to use the SilverFrost FTN95 compiler. You can download a free Personal Edition from http://www.silverfrost.com andinstallitonyoucom- puterbydouble-clickingonthedownloadedfile. Thesetupagentwillguideyouthrough the installation process. SilverFrost’s FTN95 compiler comes with a graphical user inter- face called Plato. You can open this program bydouble-clicking "Plato 3IDE"inthe"Start -> Programs -> Salford Software" menu. The user interface can be seen in Figure 2.2. Figure 2.2: The Plato user interface for the FTN95 compiler Belowthemainmenutherearetwolinesofshortcutsymbols,thefirstofwhichisnearly self-explanatory. The only thing that needs some explanation is the drop-down menu with the three possible execution modes for FTN95. The standard entry thereby is Check- Mate Win 32. CheckMate is a graphical debugger, the Debug mode is a simple console debuggingmode. UsingCheckMateorDebugandrunningintoanerror,thecompilerwill
no reviews yet
Please Login to review.