.. _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** (AM, UCSC) short notes on Fortran: * :download:`Part 1 <./garaudFortran_1.pdf>` * :download:`Part 2 <./garaudFortran_2.pdf>` * :download:`Part 3 <./garaudFortran_3.pdf>` .. _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 some 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. GCC, Intel, and PGI, (almost) fully support the Fortran 2003 and 2008 standards. An even newer standard is Fortran 2018, though there are still a few items in the standard that are up for debate. See the GFortran `manual `_ for more information on the support of the various standards in ``gfortran``. For this class we will focus on features of the language following the the Fortran 90 standard. Everything that we write will be automatically be applicable to later versions of the language. However, some of the nice features from the newer standards will be mentioned as we go along. You are welcome to use these in your assignments and projects. A few helpful resources for Fortran are: * `Fortran quickstart `_ * `GFortran manual `_ 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 `_. The exceedingly short answer is the same as any language preference discussion: use the language that suits the problem at hand. .. _ch02-fortran-compilers: Compilers --------- Unlike Python (which is an *interpreted language*), Fortran (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 below. In this class we will use *gfortran*, which is an open source compiler, part of the `GNU Project `_, and ships as part of the *GNU Compiler Collection* (GCC). See ``_ for more about gfortran. 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. Getting gfortran ------------------ The process for getting ``gfortran`` on your machine depends on whether you are using MacOS or Linux, and on which distribution of Linux you have. Before going any further you should check if you already have it installed. Try running the following commands: .. code-block:: console $ which gfortran $ gfortran --version If those succeed then you already have ``gfortran`` and you can skip this section. If those fail and you are on Linux, then you can install ``gfortran`` using your package manager. For Ubuntu you would run: .. code-block:: console $ sudo apt update && sudo apt install gfortran For Arch Linux and its derivatives you would run: .. code-block:: console $ sudo pacman -Sy && sudo pacman -S gcc-fortran For other Linux distributions please reference their documentation. For MacOS you can install ``gfortran`` using the `Homebrew package manager `_ by running: .. code-block:: console $ xcode-select --install $ brew install gcc .. _ch02-fortran-extensions: File extensions --------------- Fortran code can be compiled in a few different modes as a result of some of the constraints present in earlier versions, or to provide extended functionality. The ``gfortran`` compiler will choose some default compilation settings based on the file extension of the source file. The most common file extensions are ``*.f``, ``*.ftn``, and ``*.f`` where ```` is a number indicating the standard used (e.g. ``*.f90``). There are also uppercase versions of these file extensions. .. note:: In short, all of your source files in this class should use either the ``*.f90`` or the ``*.F90`` extension. These file extensions influence two important settings during compilation. The first is whether a source file is processed in *fixed form* or *free form*. Files that aren't labelled with a standard after Fortran 90 will default to fixed form, which is the highly rigid and slightly arcane form described above in :ref:`ch02-fortran-77`. Free format code is much easier to read and to write, so we will prefer file extensions of the form ``*.f``. To make matters even more confusing, most people use the ``*.f90`` and ``*.F90`` extensions for **all** Fortran from Fortran 90 onwards. In fact, some compilers don't even recognize extensions like ``*.f03`` by default. The other important setting is chosen based on the case of the file extension, say ``*.f90`` versus ``*.F90``. Uppercase file extensions will activate an extra feature called the *preprocessor*. When using ``gfortran`` this will actually be the same preprocessor used for C code, and we'll talk about it in more depth in a later chapter. To get a flavor of it, consider this sample file from the `FLASH `_ astrophysics code: .. literalinclude:: ./codes/hy_uhd_avgState.F90 :language: fortran :emphasize-lines: 32-33,37,39,44-45,56,58 :linenos: :download:`Download this code <./codes/hy_uhd_avgState.F90>` In the above example you can see lines starting with a ``#``. These lines don't make any sense in Fortran itself, and would generate many errors if processed as is. Instead, these lines are handled by the *preprocessor* which modifies the source file before passing it on to the actual Fortran compiler. Take a look at these lines: * 33-34, 38-40, 45-46, and 57-59 .. _ch02-fortran-compiling: Compiling, linking, and running a Fortran code ---------------------------------------------- In the following we will use the sample code :download:`demo1.f90 <./codes/demo1.f90>`, presented below. We can not run this directly the way we would for a MATLAB or Python script. 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 we are presently using. 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 into a single call to ``gfortran``, e.g. .. code-block:: console $ gfortran demo1.f90 By default this will produce an *executable* named ``a.out`` for historical reasons (it stands for *assembler output*, see `wikipedia `_). To run the code you would then type .. code-block:: console $ ./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. This could feasibly be 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 on your path. Instead we need to specify the full path to the executable, which is conveniently done through the ``.`` alias for the current directory. 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: .. code-block:: console $ gfortran -o demo1.ex demo1.f90 $ ./demo1.ex 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 (e.g., ``demo1.ex``, 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: .. code-block:: console $ gfortran -c demo1.f90 # produces demo1.o $ gfortran -o demo1.ex demo1.o # produces demo1.ex There are many other compiler flags that can be used, see the `gfortran man page `_ for a list. Alternatively you can run ``man gfortran`` or ``info gfortran`` to get the same information directly. .. note:: Linux doesn't care very much what file extensions are used for different file types. You'll notice that most text files have no extension, and that as above executables could be ``*.ex``, ``*.out``, blank, or really whatever you want. I suggest using a consistent extension for executables so that they easier to put into your ``.gitignore`` file. I often use ``*.ex`` to indicate that a file is executable, but is **not** a Windows compatible binary. .. _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: :download:`Download this code <./codes/demo1.f90>` *Comments:* * Exclamation marks are used for comments * The ``implicit none`` statement in line 8 means that any variable to be used must be explicitly declared. .. note:: Fortran has a very *weird* feature called `implicit variables`. With this feature, you don't need to declare any variables! For instance, you can freely use any variables if you want, and variables name starts with ``i, j, k, l, m, n`` will be considered as an ``integer``, and any other variables assumed to be a ``real`` number. This is an *ancient* feature for saving space on `punched cards`. But we are living in the 21st century, so we don't need to use this **troublesome** feature anymore! Thus, I highly recommend to you to use ``implicit none`` statement on every Fortran file and routine to turn **off** the implicit feature, and make all variables you are using as *explicit*. Or, you can use compiler flag like ``-fimplicit-none`` (in gfortran) to force the compiler use ``implicit none`` statement on every single routine. * Lines 10 and 11 declare two integer constants ``sp`` and ``dp``, which are used to enforce the precision of the floating point numbers used throughout. We will discuss floating point precision more soon. * Lines 12-15 declare five variables ``x, y, z, m, i``. 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 .. code-block:: fortran real (kind=4) :: x ! Or real (kind=sp) :: 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!! * ``i`` declared on line 15 is also a double precision floating point real variable. Note that ``kind`` is implied and it suffices to type ``real (dp) :: i`` * ``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. * Precision of floating point constants can be specified with a suffix of the form ``_p`` where ``p`` is an integer. This specification overrides the choice of ``e`` or ``d`` in scientific notation, and makes it more obvious what precision is desired. (See line 42 and 43) Compiling and running this program produces .. code-block:: console $ gfortran demo1.f90 -o demo1.ex $ ./demo1.ex 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 this class we will be using double precision real numbers and literals. We will discuss variable types in more detail later. Overall, you should specify ``real`` variables using a ``kind`` parameter set through a call to ``selected_real_kind(15)`` to guarantee the correct precision. For literals I prefer the suffix notation, but you can also use scientific notation with ``d`` instead of ``e``. (However, 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 trigonometric functions: .. literalinclude:: ./codes/builtinfcns.f90 :language: fortran :linenos: :download:`Download this code <./codes/builtinfcns.f90>` This produces .. code-block:: console $ 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: :download:`Download this code <./codes/builtinfcns2.f90>` Then .. code-block:: console $ gfortran builtinfcns2.f90 $ ./a.out pi = 3.141593 x = -1.000000 y = 3.141593 gives single precision results, but we can obtain double precision with .. code-block:: console $ gfortran -fdefault-real-8 builtinfcns2.f90 $ ./a.out pi = 3.1415926535897931 x = -1.0000000000000000 y = 3.1415926535897927 .. _ch02-fortran-arrays: Fortran Arrays -------------- Another good reference on Fortran arrays: `Fortran Arrays and Strings `_ 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: :download:`Download this code <./codes/array1.f90>` Compiling and running this code gives the output .. code-block:: console 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. * See `this article `_ for more information about array slicing. * 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: :download:`Download this code <./codes/vectorops.f90>` produces .. code-block:: console 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: :download:`Download this code <./codes/arrayops.f90>` produces .. code-block:: console 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 *Comments:* * Take note of the order of elements in ``a`` (line 14) * The ``matmul`` function works for both matrix-matrix multiplication and matrix-vector multiplication (lines 21 and 28) * Writing a literal array starts with ``(/`` and finishes with ``/)`` (lines 14 and 34) .. _ch02-fortran-loops: Loops ----- Consider a code with do-loops: .. literalinclude:: ./codes/loops1.f90 :language: fortran :linenos: :download:`Download this code <./codes/loops1.f90>` If you need a loop that will be executed an unknown number of times, it may better to use a ``do`` loop with a cap on the maximum number of iterations and an ``exit`` statement. The last loop could be rewritten as: .. literalinclude:: ./codes/loops2.f90 :language: fortran :linenos: :download:`Download this code <./codes/loops2.f90>` Note: ``j`` is incremented *before* comparing to ``jmax``. .. _ch02-fortran-if: if-then-else ------------ .. literalinclude:: ./codes/ifelse1.f90 :language: fortran :linenos: :download:`Download this code <./codes/ifelse1.f90>` Comments: * The ``else`` clause is optional * You can have multiple optional ``else if`` clauses There is also a one-line form of an ``if`` statement that may be useful. Note that you can not follow the statement with additional ``else if`` or ``else`` statements. .. code-block:: fortran if (x<2) exit This is equivalent to .. code-block:: fortran if (x<2) 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 .. code-block:: fortran ((x>=1.0) .and. (x<=2.0)) .or. (x>5) A boolean variable is declared with type ``logical`` in Fortran, as in the following code: .. literalinclude:: ./codes/boolean1.f90 :language: fortran :linenos: :download:`Download this code <./codes/boolean1.f90>` Line Continuation ----------------------------------------------- You can split very long lines into multiple lines using the ``&`` operator. This is potentially useful for single line conditional statements like: .. code-block:: fortran if (i>=5) & print *, 'This line is too long and I am using & to continue. This is handy!!!' If you really wish you were writing enterprise grade Java code and want to use an absurdly long variable name, you could split it off and continue it on another line (please don't actually do this): .. code-block:: fortran real :: myTrulyTrulyVeryLongVariableNameToStoreRealVariable real :: a, b a = 1.0 myTrulyTrulyVeryLongVariableNameToStoreRealVariable = 2.0 b = a + myTrulyTrulyVeryLongVariableNameToStoreRealVariable or, the last can be put into two lines using ``&`` .. code-block:: fortran b = a + myTrulyTrulyVeryLong& &VariableNameToStoreRealVariable But, in this case, make sure that there is no space between the last character ``Long`` and the ``&`` that follows it .. code-block:: fortran b = a + myTrulyTrulyVeryLong & &VariableNameToStoreRealVariable This will be then equivalent to .. code-block:: fortran 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 .. code-block:: fortran b = a + myTrulyTrulyVeryLong& & VariableNameToStoreRealVariable .. note:: Line continuation is mostly a hold over from the Fortran 77 fixed formatting days. For the most part, I would advise that you write code in a way that doesn't need continuation.