.. _ch02-fortran-basic: ======================================================== Basics of Fortran ======================================================== **Note** This chapter of the lecture note has been partially extracted and modified from **Prof. LeVeque's** (Univ. of Washington) `online note on Fortran `_. Some parts of the chapter also has been adopted and modified from **Prof. Garaud's** (AMS, UCSC) short notes on Fortran `Part 1 `_, `Part 2 `_, and `Part 3 `_. .. _ch02-fortran-history: History ------- FORTRAN stands for *FORmula TRANslator* and was the first major *high level language* to catch on. The first compiler was written in 1954-57. Before this, programmers generally had to write programs in a low-level programming language called `assembly `_. Many version followed: Fortran II, III, IV. Fortran 66 followed a set of standards formulated in 1966. See also * ``_ * ``_ for brief histories. .. _ch02-fortran-77: Fortran 77 ---------- The standards established in 1977 lead to Fortran 77, or f77, and many codes are still in use that follow this standard. Fortran 77 does not have all the features of newer versions and many things are done quite differently. One feature of f77 is that lines of code have a very rigid structure. This was required in early versions of Fortran due to the fact that computer programs were written on `punched cards `_. All statements must start in column 7 or beyond and no statement may extend beyond column 72. The first 6 columns are used for things like labels (numbers associated with particular statements). In f77 any line that starts with a 'c' in column 1 is a comment. We will not use f77 in this class but if you need to work with Fortran in the future you may need to learn more about it because of all the *legacy codes* that still use it (see also `f77-wikibooks `_). .. _ch02-fortran-90: Fortran 90/95 ------------- Dramatically new standards were introduced with Fortran 90, and these were improved in mostly minor ways in Fortran 95. There are newer Fortran 2003 and 2008 standards but few compilers implement these fully yet. See `Wikipedia page on Fortran standards `_ for more information. For this class we will use the Fortran 90/95 standards, which we will refer to as Fortran 90 for brevity. See also online documents * `on F90 `_ * `on F2003 `_ for more online tutorials on Fortran 90 and Fortran 2003. Why Fortran? -------------------------------------------- Frequently, people ask what is the advantage of using Fortran as opposed to using other modern scientific languages, such as C/C++. One good explanation can be found here: `FAQ `_. .. _ch02-fortran-compilers: Compilers --------- Unlike Python code (which is an *interpreted language*; we will study this later), a Fortran program (which is a *compiled language*) must pass through several stages before being executed (i.e., *compilation*). There are several different compilers that can turn Fortran code into an *executable (or binary)*, as described more below. In this class we will use *gfortran*, which is an open source compiler, part of the `GNU Project `_. See ``_ for more about gfortran. There is an older compiler in this suite called *g77* which compiles Fortran 77 code, but *gfortran* can also be used for Fortran 77 code and has replaced g77. There are several commercial compilers which are better in some ways, in particular they sometimes do better optimization and produce faster running executables. They also may have better debugging tools built in. Some popular ones are the Intel and Portland Group compilers. See a list of `Fortran compilers-wiki `_. .. _ch02-fortran-extensions: File extensions --------------- For the gfortran compiler the typical convention is to use the lower-case file extensions. In this case `fixed format `_ code should use ``.f``, while free format code should have the extension ``.f90`` or ``.f95``. We will use ``.f90`` in this course. Another convention is to use the upper-case extensions such as ``.F`` for fixed format code, and ``.F90`` or ``.F95`` for free format code. The difference between the lower-case and the upper-case extensions is that the latter has an extended feature that can be *preprocessed* in a C-like style. This can be particularly very useful if one develops a code project where Fortran and C/C++ routines are both used and Fortran routines are to be preprocessed. See a `FLASH `_ code example: .. literalinclude:: ./codes/hy_uhd_avgState.F90 :language: fortran :linenos: You see in the above example the lines with macros starting with ``#`` in the first column. They are the C-like preprocessor macros that are found in lines: * 32, 33, 37-39, 44, 45, and 56-58 .. _ch02-fortran-compiling: Compiling, linking, and running a Fortran code ---------------------------------------------- Suppose for example we have a Fortran file named `demo1.f90`. We can not run this directly the way we would do in MATLAB or Python where one does not require to compile the script for execution. Instead, it must be converted into *object code*, a version of the code that is in a machine language specific to the type of computer. This is done by the *compiler*. Then a *linker* must be used to convert the object code into an *executable (or binary)* that can actually be executed. This is broken into two steps because often large programs are split into many different *.f90* files. Each one can be compiled into a separate *object file*, which by default has the same name but with a ``.o`` extension (for example, from `demo1.f90` the compiler would produce `demo1.o`). One may also want to call on *library routines* that have already been compiled and reside in some library. The linker combines all of these into a single executable. For more details on the process, see for example: * ``_ * ``_ For the simplest case of a self-contained program in one file, we can combine both stages in a single `gfortran` command, e.g. :: $ gfortran demo1.f90 By default this will produce an *executable* named `a.out` for obscure historical reasons (it stands for *assembler output*, see `wikipedia `_). To run the code you would then type:: $ ./a.out Note we type `./a.out` to indicate that we are executing `a.out` from the current directory (see :ref:`ch01-unix-commands`). There is an environment variable `PATH` that contains your *search path*, the set of directories that are searched whenever you type a command name at the Unix prompt. Often this is set so that the current directory is the first place searched, in which case you could just type `a.out` instead of `./a.out`. However, it is generally considered bad practice to include the current directory in your search path because bad things can happen if you accidentally execute a file. If you don't like the name `a.out` you can specify an output name using the `-o` flag with the `gfortran` command. For example, if you like the Windows convention of using the extension `.exe` for executable files:: $ gfortran demo1.f90 -o demo1.exe $ ./demo1.exe will also run the code. Note that if you try one of the above commands, there will be no file `demo1.o` created. By default `gfortran` removes this file once the executable (or binary, e.g., `demo1.exe`, or `a.out`) is created. Later, we will see that it is often useful to split up the **compile** and **link** steps, particularly if there are several files that need to be compiled and linked. We can do this using the `-c` flag to compile without linking:: $ gfortran -c demo1.f90 # produces demo1.o $ gfortran demo1.o -o demo1.exe # produces demo1.exe There are many other compiler flags that can be used, see `linux man page for gfortran `_ for a list. .. _ch02-fortran-ex1: Sample codes ------------ The first example simply assigns some numbers to variables and then prints them out. The comments below the code explain some features. .. literalinclude:: ./codes/demo1.f90 :language: fortran :linenos: *Comments:* * Exclamation points are used for comments * The `implicit none` statement in line 7 means that any variable to be used must be explicitly declared. * Lines 8-10 declare four variables `x, y, z, n`. Note that `x` is declared to have type `real` which is a floating point number stored in 4 bytes, also known as *single precision*. This could have equivalently been written as:: real (kind=4) :: x `y` and `z` are floating point numbers stored in 8 bytes (corresponding to *double precision* in older versions of Fortran). This is generally what you want to use. * Fortran is not case-sensitive, so `M` and `m` refer to the same variable!! * `1.23456789e-10` specifies a 4-byte real number. The 8-byte equivalent is `1.23456789d-10`, with a `d` instead of `e`. This is apparent from the output below. Compiling and running this program produces:: $ gfortran demo1.f90 -o demo1.exe $ ./demo1.exe M = 3 x is real (kind=4) x = 1.00000119 y is real (kind=8) but 1.e0 is real (kind=4): y = 1.0000011920928955 z is real (kind=8) z = 1.0000012345678899 For most of what we'll do in this class, we will use real numbers with `(kind=8)`. Be careful to specify constants using the `d` rather than `e` notation if you need to use scientific notation. (But see :ref:`ch02-fortran-default8` below for another approach.) .. _ch02-fortran-intrinsic: Intrinsic functions ------------------- There are a number of built-in functions that you can use in Fortran, for example the trig functions: .. literalinclude:: ./codes/builtinfcns.f90 :language: fortran :linenos: This produces:: $ gfortran builtinfcns.f90 $ ./a.out pi = 3.1415926535897931 x = -1.0000000000000000 y = 3.1415926535897927 See ``_ for a good list of other intrinsic functions. .. _ch02-fortran-default8: Default 8-byte real numbers --------------------------- Note that you can declare variables to be real without appending `(kind=8)` if you compile programs with the gfortran flag `-fdefault-real-8`, e.g. if we modify the program above to: .. literalinclude:: ./codes/builtinfcns2.f90 :language: fortran :linenos: Then:: $ gfortran builtinfcns2.f90 $ ./a.out pi = 3.141593 x = -1.000000 y = 3.141593 gives single precision results, but we can obtain double precisions with:: $ gfortran -fdefault-real-8 builtinfcns2.f90 $ ./a.out pi = 3.1415926535897931 x = -1.0000000000000000 y = 3.1415926535897927 Note that if you plan to do this you might want to define a Unix alias, e.g. :: $ alias gfort8="gfortran -fdefault-real-8" so you can just type:: $ gfort8 builtinfcns2.f90 $ ./a.out pi = 3.1415926535897931 x = -1.0000000000000000 y = 3.1415926535897927 Such an alias could be put in your ``.bashrc`` or ``.bash_profile`` (see :ref:`ch01-unix-commands`). We'll also see how to specify compiler flags easily in a :ref:`ch02-fortran-makefiles`. .. _ch02-fortran-arrays: Fortran Arrays -------------- Note that arrays are indexed starting at 1 by default, rather than 0 as in Python or C. Also note that components of an array are accessed using parentheses, not square brackets! Arrays can be dimensioned and used as in the following example: .. literalinclude:: ./codes/array1.f90 :language: fortran :linenos: Compiling and running this code gives the output:: A = 2.0000000000000000 3.0000000000000000 3.0000000000000000 4.0000000000000000 4.0000000000000000 5.0000000000000000 0.200000D+01 0.300000D+01 0.300000D+01 0.400000D+01 0.400000D+01 0.500000D+01 x = 0.1000D+01 0.1000D+01 b = 0.500000D+01 0.700000D+01 0.900000D+01 *Comments:* * In printing `A` we have used a *slice* operation: `A(i,:)` refers to the i'th row of `A`. In Fortran 90 there are many other array operations that can be done more easily than we have done in the loops above. We will investigate this further later. * Here we set the values of `m,n` as integer parameters before declaring the arrays `A,x,b`. Being parameters means we can not change their values later in the program. * It is possible to declare arrays and determine their size later, using `allocatable` arrays, which we will also see later. There are many array operations you can do, for example: .. literalinclude:: ./codes/vectorops.f90 :language: fortran :linenos: produces:: x = 10.000000000000000 20.000000000000000 30.000000000000000 x**2 + y = 200.00000000000000 800.00000000000000 1800.0000000000000 x*y = 1000.0000000000000 8000.0000000000000 27000.000000000000 sqrt(y) = 10.000000000000000 20.000000000000000 30.000000000000000 dot_product(x,y) = 36000.000000000000 Note that addition, multiplication, exponentiation, and intrinsic functions such as `sqrt` all apply component-wise. Multidimensional arrays can be manipulated in similar manner. The product of two arrays `x` and `y` using `*` (i.e., `x*y`) is always component-wise. For matrix multiplication, use `matmul`. There is also a `transpose` function: .. literalinclude:: ./codes/arrayops.f90 :language: fortran :linenos: produces:: a = 1.0000000000000000 4.0000000000000000 2.0000000000000000 5.0000000000000000 3.0000000000000000 6.0000000000000000 b = 1.0000000000000000 2.0000000000000000 3.0000000000000000 4.0000000000000000 5.0000000000000000 6.0000000000000000 c = 17.000000000000000 22.000000000000000 27.000000000000000 22.000000000000000 29.000000000000000 36.000000000000000 27.000000000000000 36.000000000000000 45.000000000000000 x = 5.0000000000000000 6.0000000000000000 y = 29.000000000000000 40.000000000000000 51.000000000000000 .. _ch02-fortran-loops: Loops ----- Consider a code with do-loops: .. literalinclude:: ./codes/loops1.f90 :language: fortran :linenos: The `while` statement used in the last example is considered obsolete. It is better to use a `do` loop with an `exit` statement if a condition is satisfied. The last loop could be rewritten as:: i = 0 do ! prints 0,1,2,3,4 if (i>=5) exit print *, i i = i+1 end do This form of the `do` is valid but is generally not a good idea. Like the while loop, this has the danger that a bug in the code may cause it to loop forever (e.g. if you typed `i = i-1` instead of `i = i+1`). A better approach for loops of this form is to limit the number of iterations to some maximum value (chosen to be ample for your application), and then print a warning message, or take more drastic action, if this is exceeded, e.g.: .. literalinclude:: ./codes/loops2.f90 :language: fortran :linenos: Note: `j` is incremented *before* comparing to `jmax`. .. _ch02-fortran-if: if-then-else ------------ .. literalinclude:: ./codes/ifelse1.f90 :language: fortran :linenos: Comments: * The `else` clause is optional * You can have optional `else if` clauses There is also a one-line form of an `if` statement that was seen in a previous example on this page:: if (i>=5) exit This is equivalent to:: if (i>=5) then exit end if .. _ch02-fortran-booleans: Booleans -------- * Compare with `<, >, <=, >=, ==, /=`. You can also use the older Fortran 77 style: `.lt., .gt., .le., .ge., .eq., .neq.`. * Combine with `.and.` and `.or.` For example:: ((x>=1.0) .and. (x<=2.0)) .or. (x>5) A boolean variable is declared with type `logical` in Fortran, as for example in the following code: .. literalinclude:: ./codes/boolean1.f90 :language: fortran :linenos: Line Continuation ----------------------------------------------- In case you wish to write a long line so that you want to continue your implementation in the next line, it can be continued using `&`:: if (i>=5) & print *, 'This line is too long and I am using & to continue. This is handy!!!' There is also a case you wish to end the line with `&` and begin a new line with `&`. For instance, if you have a very long variable name and want to split it off and continue in the next line (yes, this is a very weird coding practice though), you can do something like this:: real :: myTrulyTrulyVeryLongVariableNameToStoreRealVariable real :: a, b a = 1.0 myTrulyTrulyVeryLongVariableNameToStoreRealVariable = 2.0 b = a + myTrulyTrulyVeryLongVariableNameToStoreRealVariable or, the last can be put into two lines using `&`:: b = a + myTrulyTrulyVeryLong& &VariableNameToStoreRealVariable But, in this case, make sure that there is no space between the last chracter `Long` and the `&` that follows it:: b = a + myTrulyTrulyVeryLong & &VariableNameToStoreRealVariable This will be then equivalent to:: b = a + myTrulyTrulyVeryLong VariableNameToStoreRealVariable which is not what you wanted to do. The situation is the same with the second line that follows the first line:: b = a + myTrulyTrulyVeryLong& & VariableNameToStoreRealVariable