Before Starting (Editing, Compiling &
|
![]() Tutorials on Fortran by www.giuseppeforte.me is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. |
Before starting with Fortran, some terminology has to be introduced. In particular must be clear what is the difference between the source code and the compiler. A source code is nothing but a text file with the extension of the programming language we are using to code our algorithms. For example, if we use Fortran to code our algorithms, the source code is nothing but a text file with the extension *.f, *.for, *.f90, *.f95 (I always use *.f). The asterisk before the dot is just a way to specify a generic name for the program. If we write a Fortran program whose goal is to evaluate the area of a circle, we could name our source code as Circle.f. The file Circle.f just contains all the Fortran instructions to make such an evaluation. Thus, the source code is nothing but a file whose rows are statements in Fortran, an ensemble of lines, each one representing an instruction written using the Fortran syntax. If we are working with C++, we may call our source code Circle.cpp (*.cpp is one of the possible extensions for a program written in C++). The source code is thus characterized by a series of instructions, but it is not an executable program (the source code is just text!!!!). In order to get an executable program from the source code we need to use the compiler. The compiler is thus an executable program itself, which takes as input the source code (for example Circle.f if we are using a Fortran compiler) and returns an executable file whose default name used by the Fortran compiler is a.out (such name may be obviously changed, see below). If we execute a.out, all the instructions present in the source code will be executed by the machine sequentially. For example, if we are using a Fortran compiler to compile the file Circle.f, the associated executable file a.out will evaluate the area of a circle. It is of course supposed that a.out takes in input the value of the circle radius, for example, and returns the area associated to such circle. This means that we have previously written in the source code all the instructions to make the executable file a.out able to do that job. Not all the softwares work with the same compiler. Currently the fortran compiler is named gfortran and it is freely distributed under the GNU General Public license . Another compiler, ifort has been developed by Intel, however it is not free. If we wanted to work with C++, we would need another compiler, gcc (which is the one freely distributed by GNU; also in this case there exists a non free version developed by Intel). The compiler gfortran is not preinstalled on commercial machines (gcc is already installed on GNU/Linux environments and also on OS X). In any case on the GCC Wiki page you may find all the packages you need to install gfortran on your machine, regardless the OS you are using (Windows, Mac, GNU/Linux).
Any source code should be written using a simple text editor. On-line you might find a variety of different editors, which are able to highlight the software syntax, depending on the extension of the file. The most famous (free) editors are Vim, Emacs (supported on the most common operative systems. i.e. Windows, Mac, GNU/Linux) & Notepad++ (Windows only)
Any source code should be written using a simple text editor. On-line you might find a variety of different editors, which are able to highlight the software syntax, depending on the extension of the file. The most famous (free) editors are Vim, Emacs (supported on the most common operative systems. i.e. Windows, Mac, GNU/Linux) & Notepad++ (Windows only)
- Vim download for Windows (Typically already installed on GNU/Linux and OS X)
- Emacs download for Windows (Typically already installed on GNU/Linux and OS X)
- Notepad++ download (Windows only)
EDITING
Let us assume we have got our source code to evaluate the area of a circle given the radius. Such source code could be something like in Fig. 1 with the relative output in Fig. 2 . I do not discuss the meaning of the Fortran instructions here (see, for example Fundamentals) as the main goal of this section is how to compile the source code in Fig.1 and run it in order to get the result in Fig. 2.
My tutorials, from now on, will be always based on using Fortran on a OS X and/or GNU/Linux operative system. In order to compile the source code and execute the executable file obtained from the source code by means of a compiling process, we need to use the terminal. When using a GNU/Linux like operative systems (for example the most common one: Ubuntu) the terminal may be started using a keyboard shortcut: Ctrl + Alt + T (such shortcut does not work on OS X). Alternatively, you may search the word Terminal using the "search" function on the Dash. The search function returns you an icon which looks like Fig. 3; Just (double) click on it and your terminal will start working. Once you click on the terminal icon you will get something like in Fig. 4. The symbol "~Name" (in my case "~Arya" or frequently "~Home" or simply "~" without any name) means that you are in the Home directory. To check the content of the home directory just type the command "ls" and you will get something like Fig. 5 (bottom).
Fig. 4: a typical view of the terminal when we invoke it. Here "~Arya" means that we ere in a specific folder which is frequently referred to as home directory (the home directory is always identified by the symbol "~"). Orientating in the terminal is really easy. For example, if wanted to know the content of the folder returned by terminal when we launch it, we just need to type the command "ls" (Fig.5, bottom)
|
Fig. 5: (top) Finder Icon on a Mac OS. (Bottom) If we type the command "ls" on the terminal we get a complete list of the files and folders included in our current locations
|
Looking for the terminal on a OS X is really easy. We may indeed open the finder, which is identified by the icon shawn in (Fig.5, top) and navigate to Applications/Utilities. Once in the folder Utilities we double click on the icon of the terminal (cfr. Fig.3) and here we go, the terminal is running. Again we will get something similar to Fig.4 and, in order to list the content of the current folder we again use the instruction "ls" . In general, unless explicitly specified, the instructions used to work with the terminal are the same for both GNU/Linux like systems and OS X. The only difference may be in the location of the terminal application and the way to find it the first time. Now that we know how to run the terminal on both the operative systems, all the following contents (included everything about the Fortran examples in the other sections) will be discussed without referring to a specific operative system. Everything will be valid for both OS X and GNU/Linux like systems ( essentially because both OS X and GNU/Linux terminals are based on the so called Bash shell ). This is one of the advantage to work with a terminal on OS X and/or on GNU/Linux . All the work is highly portable and all the procedures are the same. For example, I work on a OS X and I always transfer my files on external Clusters in order to run massive simulations. Typically, external clusters are equipped with a GNU/Linux like system, however this is not a problem because all the procedures I use to work with Fortran on my machine are the same on the typical external cluster. In windows the terminal is based on the DOS (rather than Bash shell) and thus the portability is not at all granted. However it should be mentioned that Microsoft has finally introduced (2016!) the possibility to work with the Bash shell on any Windows operative system (version 10 and following releases), thus definitively erasing any barrier in terms of portability of high-level languages (like Fortran, C, C++, Java ecc..) between the different operative systems. What follows should thus apply to all the common operative systems (Windows, Mac, GNU/Linux). Refer to the following posts on the the Microsoft official blog to find out more information:The binaries for the fortran compiler working on Windows may be downloaded from the following web site:
Now let us move on the editing part of our coding experience. With reference to Fig. 5 (bottom) we may possibly decide to move on the Desktop by typing on the terminal the instruction "cd Desktop" . Once on the Desktop we may think to create a new folder in which we will put all our Fortran source codes. We may think to name such folder "SOURCES". In order to create such folder, we just type on the terminal the instruction "mkdir SOURCES", as shawn in Fig. 6.
Now let us move on the editing part of our coding experience. With reference to Fig. 5 (bottom) we may possibly decide to move on the Desktop by typing on the terminal the instruction "cd Desktop" . Once on the Desktop we may think to create a new folder in which we will put all our Fortran source codes. We may think to name such folder "SOURCES". In order to create such folder, we just type on the terminal the instruction "mkdir SOURCES", as shawn in Fig. 6.
Fig. 6: by typing "cd Desktop" we move from the home folder ("~ Arya" in my case) to the Desktop folder. After that we may create a folder where we will put all the Fortran source codes. The instruction to create the folder is "mkdir FolderName" (in our case "FolderName = SOURCES")
|
Fig. 7: Once in the folder SOURCES we need to create the empty file which will host our source code. The instruction to create such an empty file is "touch FileName" (in our case "FileName = Circle.f")
|
From the Desktop folder we can easily move in the folder SOURCES (just type "cd SOURCES" on the terminal) where we create an empty file using the instruction "touch Circle.f" (cfr. Fig. 7). In order to edit such file and fill it with the content in Fig. 1 we may use one of the editors quickly cited above (vim, emacs and Notepad++). Such editors are not intuitive and it would be better if you refer to their reference manuals to learn how to edit a file All the manuals are really huge, thus do not try to learn all the possible things about them. Just learn how to
- start a text editor;
- insert text characters (to write your code);
- highlight the syntax (if not already done automatically when you start editing your source code with your chosen editor)
- set the row number on the left;
- save without quit;
- cut and past lines;
- save and quit.
Fig. 8: task 1 of the above list (top) Once in the folder SOURCES we can start editing the previously created file Circle.f with vim, simply type "vim Circle.f" and press return; (bottom) Of course we get an empty file with the cursor on the top-left corner. Be ready to introduce your text starting from there.
|
Fig. 9: tasks 2 & 3 of the above list (top) In order to introduce our text in the file we need to tell vim to enter in the "insert mode". To this end, we push on the keyboard the letter "i" just once. From now on we can write whatever we want using the keyboard characters. Here are shawn the first two lines of the code in Fig.1. However they are not emphasized as in Fig.1; (bottom) highlighting the syntax is really easy. We just need to type on the keyboard the combination "esc:syntax on"
1) push button "esc" on the keyboard 2) push the button ":" on the key board 3) type the sentence "syntax on" ----> esc:syntax on |
Fig. 10: tasks 4 & 5 of the above list (top) In order to introduce the row numbering on the left we push in succession the button "esc" on the keyboard and immediately after ":set number"; (bottom) in order to save the file without quitting the vim environment, we type on the keyboard the combination "esc:w"
|
Fig. 11: tasks 6 & 7 of the above list (top) to copy the lines 1 and 2 we put the cursor at the beginning of line 1 and we type the combination "esc:2yy". This instruction will copy 2 lines starting from the line where is currently located the cursor. If we wrote "esc:nyy", we would have copied n lines starting from line 1 (if the cursor was on line 1 before using the combination "esc:nyy"). To paste such lines we put the cursor on a given line (for example line 2) and we use the combination "esc:p", which will attach the previous copied lines (for example lines 1 & 2) starting from the line immediately after the position of the cursor (thus if we put the cursor on line 2, the two lines will be copied starting from line 3); (bottom) in order to save and quit the vim environment, we type on the keyboard the combination "esc:wq"
|
Compiling & Running
We are finally in the position to compile the source code and run the executable file obtained from it. Be sure that you are in the folder SOURCES (the folder where you have created and typed your file Circle.f). Once the compiler gfortran is installed on your machine, to compile the file "Circle.f" just type the following instruction on the terminal and press return
gfortran -O3 Circle.f -o out.x
The above instruction takes the file "Circle.f" and returns the executable file "out.x" (out.x is your own choice, you might call
the name of the executable file as "Circle.out" if you prefer). If the the name of the executable file is skipped (in this example "out.x") the compiler will call your executable file "a.out" (the default name). In general if we have a source code "SourceName.f", the compilation instruction is
the name of the executable file as "Circle.out" if you prefer). If the the name of the executable file is skipped (in this example "out.x") the compiler will call your executable file "a.out" (the default name). In general if we have a source code "SourceName.f", the compilation instruction is
CompilerName -O3 SourceName.f -o ExecutableFileName.x
where CompilerName = gfortran (if we are working with gfortran) or CompilerName = ifort if we are working with the Intel compiler. If there are no compilation errors in your code, you get your executable file in the same folder of the source code ("SOURCES" in the current example). The executable file could be executed in the same folder of the source code (SOURCES) or (better!!) in another folder. I suggest to keep all the source codes in a single SOURCES folder and to run them in separated folders. For example we could run our program "Circle.f" in another folder, called "CircleOutcomes". At the same way, if we write a source code to simulate a partial differential equation (the heat equation for example ) we could put the source in the SOURCES folder an the outcome (the result of the execution of the compiled heat equation source code) in another folder (different from "CircleOutcomes"). For example we could name the folder with the result of our computations as "PDEheatOutcome" and so on. In this way we spread all our different projects in different folders, however all our source codes are in the same place. This technique allows us to backup all our work in few seconds (just copy the folder SOURCES on another device). Indeed loosing a source code implies that we must rewrite everything from scratch, while if we have all our SOURCES updated and correctly saved on different machines, also if we loose the results relative to a given simulation, we might always run it by compiling the source code again e running again the simulation on another machine. For this reason, I strongly suggest to have all your source codes (BUT NOT ALL THE DATA RELATIVE TO THE OUTPUTS) in at least two synchronized folders (i.e. Dropbox and Drive ). You don't even need to save your SOURCES folder on your personal machine (Just run your source codes by connecting to your Dropbox and/or Drive folders synchronized on your machine(s)). This suggestion is not at all needless according to me, indeed it is a bad practice of every PhD student and young postdoc to create a nearly infinite number of folders and sub-sub-sub-sub-folders, each one maybe with the same copy of the source code, however with a different settings in the simulation parameters or with different source codes and a nearly infinite amount of data coming from the outputs. Of course anyone is free to work in this way. I strongly suggest to have ONE folder ( copied on two synchronized folders hosted by one of these big companies around the WWW) with ALL the source codes and DIFFERENT folders for the data outputs. Running the executable file is pretty easy. We move in the folder SOURCES and we type on the terminal the instruction "pwd" and press return. Such instruction returns a string like "FolderOne/FolderTwo/.../SOURCES/", just copy such string and than move in the folder where you prefer to run the simulation (for example "CircleOutcomes") and type in the terminal the instruction
FolderOne/FolderTwo/.../SOURCES/./ExecutableFileName.x
press return and have-fun :-) !!!!....now you can come back to the Fundamentals and start studying the tutorials I uploaded for you on this web page .... and remember that it's free, enjoy !!! Cheers