|
13 | 13 | After booting Ubuntu, you need to install a Fortran compiler. |
14 | 14 | Open a terminal and at the prompt (the \$) type |
15 | 15 | \begin{lstlisting}[style=prompt, nolol] |
16 | | - $ sudo aptitude install gfortran gfortran-doc |
| 16 | + $ sudo apt-get install gfortran gfortran-doc |
17 | 17 | \end{lstlisting} |
18 | 18 | \emph{Note that anything you type in the console is case-sensitive!} |
19 | 19 | This command grants \texttt{aptitude} privileges to search for and then install the \texttt{gfortran} package and its documentation. |
20 | 20 | You may also find it useful to install the \LaTeX\ typesetting system for writing reports. |
21 | 21 | You can install it with another call to \texttt{aptitude}: |
22 | 22 | \begin{lstlisting}[style=prompt, nolol] |
23 | | - $ sudo aptitude install texlive # or texlive-full for a complete installation |
| 23 | + $ sudo apt-get install texlive # or texlive-full for a complete installation |
24 | 24 | \end{lstlisting} |
25 | 25 |
|
26 | 26 | Once you have \texttt{gfortran} installed, you can start writing programs. |
|
40 | 40 | \end{lstlisting} |
41 | 41 |
|
42 | 42 | For your first program, open a terminal, create a directory for ICCP files and open your first Fortran file by typing |
| 43 | +\marginnote[-0.5cm]{Modern terminal emulators all have a feature called |
| 44 | + \emph{tab completion}\index{Tab completion}; enter part of a command \& hit the \texttt{TAB} key and the terminal will attempt to fill in the rest of the command for you.} |
43 | 45 | \begin{lstlisting}[style=prompt, nolol] |
44 | 46 | $ mkdir iccp # create a new directory called 'iccp' |
45 | 47 | $ cd iccp # move to the new directory |
|
50 | 52 | \begin{itemize} |
51 | 53 | \item The program starts with a declaration of variables.\index{Fortran!types} |
52 | 54 | \texttt{\keyword{real}(8)} denotes a floating-point variable with double (8 byte) precision. |
53 | | - Similarly, \keyword{integer} denotes an integer number.\footnote{Fortran intrinsic types include \keyword{logical}, \keyword{integer}, \keyword{real}, \keyword{complex}, and \keyword{character} data.} |
| 55 | + Similarly, \keyword{integer} denotes an integer number.\footnote{Fortran intrinsic types include \keyword{logical}, \keyword{integer}, \keyword{real}, \keyword{complex}, and \keyword{character} data. Modern Fortran also gives you the ability to define your own datatypes.} |
54 | 56 | Not specifying a size generally defaults to 4-byte precision. |
55 | 57 | \keyword{implicit none}\index{Fortran!implicit none@\keyword{implicit none}} prevents Fortran from trying to infer the type from the variable name, which is a major source of bugs---\emph{always include this!} |
56 | 58 | \item The attribute \keyword{parameter}\index{Fortran!parameter@\keyword{parameter}} specifies that we are declaring a constant. |
|
61 | 63 | It is also possible to specify the precision as a suffix: \texttt{1.0\_4} for single and \texttt{1.0\_8} for double precision. |
62 | 64 | This also works for integers. |
63 | 65 | \item \texttt{\keyword{integer} :: fibonacci(N)} allocates an array of 20 |
64 | | - integers, with the array\index{Fortran!arrays} index\footnote{Array indices can start at any integer by replacing \texttt{N} with a lower and upper bound separated with a colon.} running from 1 to 20. |
| 66 | + integers, with the array\index{Fortran!arrays} index\footnote{Array indices can start at any integer by replacing \texttt{N} with a lower and upper bound separated with a colon. Thus, \lstinline$real :: myArray(-312:74)$ means \texttt{myArray} starts at index -312 and runs through index 74.} running from 1 to 20. |
65 | 67 | \item `\texttt{*}' is multiplication, `\texttt{**}' is exponentiation. |
66 | 68 | \item The \keyword{print} statement on line 24 contains the format code\index{Fortran!format codes} \str{"(4I6)"}. |
67 | | - This tells Fortran to print 4 records of integer type per line, each taking up 6 characters. |
68 | | - Format strings for other datatypes include E$w.d$ (real -- decimal form), ES$w.d$ (real -- scientific form), EN$w.d$ (real -- engineering form), L$w$ (logical), and A (characters). Here, $w$ gives the width of a record and $d$ gives the number of places right of the decimal. |
| 69 | + This tells Fortran to print 4 records of integer type, each having a width of 6 characters (including spaces), per line. |
| 70 | + Format strings for other datatypes include \texttt{Ew.d} (real -- decimal form), \texttt{ESw.d} (real -- scientific form), \texttt{ENw.d} (real -- engineering form), \texttt{Lw} (logical), and \texttt{A} (characters). Here, \texttt{w} gives the width of a record and \texttt{d} gives the number of places right of the decimal. |
69 | 71 | \item Comments in Fortran start with `\texttt{!}' and last until the end of the line. |
70 | 72 | \item Dividing integers \textcolor{red}{\textbf{results in an integer}}\index{Fortran!integer division}, so \texttt{3 / 2 == 1} instead of 1.5 as you might expect. |
71 | | - Multiplying by \texttt{1d0} on line 27 forces Fortran to do a double-precision floating-point calculation. |
| 73 | + Multiplying by \texttt{1d0} on line 28 forces Fortran to do a double-precision floating-point calculation. |
72 | 74 | \end{itemize} |
73 | 75 | Now we compile and run the program. |
74 | 76 | Compiling means translating the Fortran source code into machine code (processor instructions). |
|
101 | 103 | The downside is that it can result in executables that will not run on a different machine. |
102 | 104 | \item \texttt{-O3}\index{compiler flags!-O3} turns on all optimizations. |
103 | 105 | This increases the compilation time significantly, although it should still be fast enough for the programs you'll write in ICCP. |
104 | | - The runtime of your program, on the other hand, will dramatically decrease. |
| 106 | + The run time of your program, on the other hand, will dramatically decrease. |
105 | 107 | The only reason not to use this flag is that it might interfere with the debugger (see below). |
106 | 108 | \item A possible additional optimization flag is \texttt{-ffast-math}\index{compiler flags!-ffast-math}. |
107 | 109 | This flag enables floating-point optimizations which might reduce accuracy or result in incorrect behavior, especially in situations such as divide-by-zero. |
|
0 commit comments