Skip to content

Commit be91a29

Browse files
committed
Merge branch 'tufte' -- preparing for ICCP organization
2 parents bd8381f + 585037c commit be91a29

20 files changed

Lines changed: 258 additions & 92 deletions

cag.sty

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
\RequirePackage{latexsym,amsmath,amssymb,makeidx}
2-
\RequirePackage[dvipsnames,usenames]{color}
2+
%\RequirePackage[dvipsnames,usenames]{color}
33
%\RequirePackage[colorlinks=true,urlcolor=Blue,citecolor=Green,linkcolor=BrickRed]{hyperref}
44

55
\hyphenation{co-or-din-ate co-or-din-ates half-plane half-space stereo-iso-mers

chapters/Coding_style.tex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@
1818
\item\textbf{Line width}---Limit the line width of your source to 80 characters.
1919
This has been standard practice for decades, and vastly improves both readability and the ability to cleanly print your code.
2020
If you do need more than 80 characters on a line, use the Fortran continuation character `\texttt{\&}'.
21-
\item\textbf{Indentation}---Indent every block, be it a \keyword{program}, \keyword{module}, \keyword{subroutine}, \keyword{function}, \keyword{do} or \keyword{if} block by 2 or 4 spaces.
21+
\item\textbf{Indentation}---Indent every block, be it a \keyword{program}, \keyword{module}, \linebreak \keyword{subroutine}, \keyword{function}, \keyword{do} or \keyword{if} block by 2 or 4 spaces.
2222
\emph{Don't use tabs!} Code that looks properly aligned on your machine may look horrible on another because of a different tab setting.
2323
Proper indentation makes it clear where control structures begin and end, and is a major boon to readability.
2424
If the indentation puts you over the 80 character limit, that's an indication that you may need to refactor your code intro smaller subroutines.
2525
Four levels of indentation should generally be the maximum.
26-
\item\textbf{Spaces}---Put spaces around binary operators, including \texttt{::}, and after keywords and commas, not after unary operators, function names or around parentheses. For example\newpage
27-
\begin{lstlisting}[caption=,nolol,float=t!]
26+
\item\textbf{Spaces}---Put spaces around binary operators, including \texttt{::}, and after keywords and commas, not after unary operators, function names or around parentheses. For example
27+
\begin{lstlisting}[caption=,nolol,float=h!]
2828
real(8) :: x, y
2929
x = func1()
3030
if ((x < 0) .or. (x > 1)) then
3131
y = func2(-2, 2*x + 3)
3232
end if
3333
\end{lstlisting}
3434
\item\textbf{Naming}---Identifiers in Fortran can't contain spaces.
35-
Use \texttt{CamelCase} to separate words in program and module names, and \texttt{lower\_case\_with\_underscores} for function and variable names.
35+
Use \linebreak \texttt{CamelCase} to separate words in program and module names, and \linebreak \texttt{lower\_case\_with\_underscores} for function and variable names.
3636
Names should be descriptive, but they don't have to be overly long.
3737
You can use \texttt{calc\_deriv} instead of \texttt{calculate\_derivative}, but naming your function \texttt{cd} is a shooting offense.
3838
Unfortunately, until the appearance of Fortran 90, only the first six characters of an identifier were significant, leading to such beautifully transparent function names as \texttt{DSYTRF}\footnote{DSYTRF computes the factorization of a real symmetric matrix using the Bunch-Kaufman diagonal pivoting method, in case you wondered.} in legacy codes like \idx{LAPACK}.
@@ -46,16 +46,16 @@
4646
Don't worry about the overhead of calling a function.
4747
If you use optimization flags like \texttt{-O3}, the compiler will try to inline them anyway.
4848
\item\textbf{Modules}---Separate your program into logical units.
49-
Combine related functions and subroutines into modules.
50-
For example, a single module with function integrators, or ODE solvers.
49+
Combine related functions and subroutines into modules\footnote{It's also convenient to put all physical parameters (number of particles, $\pi$, $c$, $k_B$, etc.) into their own module, even if they're just constant numbers. You can then ``load'' the parameters into your subroutines with the \keyword{use} statement instead of having absurdly long lists of arguments, or worse, global variables.}.
50+
For example, a single module with function integrators, or ODE solvers.
5151
If you use the object-oriented features of Fortran, it is also good practice to create separate modules for classes, \eg, a sparse-matrix object.
5252
\item\textbf{Comments}---Comments are kind of a double-edged sword.
5353
If used wrongly, they can actually hurt readability.
5454
The golden rule is: never explain \emph{how} it works, just \emph{what} it does.
5555
The how should be evident from the code itself.
5656
\emph{``Good code is its own best documentation.''} If you follow the other guidelines regarding naming and keeping functions small, you can generally suffice with a small comment at the beginning of every function or subroutine.
5757
\end{itemize}
58-
One final point, not of style, but important nonetheless: start every program and module with \keyword{implicit none}\index{Fortran!implicit none@\keyword{implicit none}}.
58+
One final point, not of style, but important nonetheless: start every program and module with \keyword{implicit none}\marginnote{The \texttt{-fimplicit-none}\index{compiler flags!-fimplicit-none} compiler flag will also turn off implicit typing globally.} \index{Fortran!implicit none@\keyword{implicit none}}.
5959
This is an unfortunate holdover from older versions of Fortran; it forces you to explicitly declare all variables, instead of allowing Fortran to infer the type from the name.
6060
Forgetting \keyword{implicit none} and making a typo in one of your variable names is a major source of bugs.
6161
\emph{Never ever omit this!}

chapters/Debugging.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
\begin{quote}\small
55
\emph{``Everyone knows that debugging is twice as hard as writing a program in the first place.
6-
So if you're as clever as you can be when you write it, how will you ever debug it?'' \\ \\
6+
So if you're as clever as you can be when you write it, how will you ever debug it?'' \\
77
``The most effective debugging tool is still careful thought, coupled with judiciously placed print statements.''} \\\hspace*{\fill}---Brian Kernighan
88
\end{quote}
99
The \texttt{MyProg} program does not contain any bugs (fingers crossed) and works as expected.

chapters/Getting_your_toes_wet.tex

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
After booting Ubuntu, you need to install a Fortran compiler.
1414
Open a terminal and at the prompt (the \$) type
1515
\begin{lstlisting}[style=prompt, nolol]
16-
$ sudo aptitude install gfortran gfortran-doc
16+
$ sudo apt-get install gfortran gfortran-doc
1717
\end{lstlisting}
1818
\emph{Note that anything you type in the console is case-sensitive!}
1919
This command grants \texttt{aptitude} privileges to search for and then install the \texttt{gfortran} package and its documentation.
2020
You may also find it useful to install the \LaTeX\ typesetting system for writing reports.
2121
You can install it with another call to \texttt{aptitude}:
2222
\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
2424
\end{lstlisting}
2525

2626
Once you have \texttt{gfortran} installed, you can start writing programs.
@@ -40,6 +40,8 @@
4040
\end{lstlisting}
4141

4242
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.}
4345
\begin{lstlisting}[style=prompt, nolol]
4446
$ mkdir iccp # create a new directory called 'iccp'
4547
$ cd iccp # move to the new directory
@@ -50,7 +52,7 @@
5052
\begin{itemize}
5153
\item The program starts with a declaration of variables.\index{Fortran!types}
5254
\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.}
5456
Not specifying a size generally defaults to 4-byte precision.
5557
\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!}
5658
\item The attribute \keyword{parameter}\index{Fortran!parameter@\keyword{parameter}} specifies that we are declaring a constant.
@@ -61,14 +63,14 @@
6163
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.
6264
This also works for integers.
6365
\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.
6567
\item `\texttt{*}' is multiplication, `\texttt{**}' is exponentiation.
6668
\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.
6971
\item Comments in Fortran start with `\texttt{!}' and last until the end of the line.
7072
\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.
7274
\end{itemize}
7375
Now we compile and run the program.
7476
Compiling means translating the Fortran source code into machine code (processor instructions).
@@ -101,7 +103,7 @@
101103
The downside is that it can result in executables that will not run on a different machine.
102104
\item \texttt{-O3}\index{compiler flags!-O3} turns on all optimizations.
103105
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.
105107
The only reason not to use this flag is that it might interfere with the debugger (see below).
106108
\item A possible additional optimization flag is \texttt{-ffast-math}\index{compiler flags!-ffast-math}.
107109
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

Comments
 (0)