8
$\begingroup$

I was wondering what is the best way to learn how to program in Fortran ?

I've been looking for books, the examples that seemed OK to me were "Fortran 95/2003 Explained" and "Fortran For Scientists And Engineers".

I don't know what you guys think of these?

Another thing that I am wondering about is which compiler I should use? I work on a Windows 7 machine (64-bit).

I hope you guys can help me on this one!

So for my experience with programming:

  1. Matlab and maple were kind off my first introductions into the programming world. Matlab is a program in which I'm actually pretty good, I got the hang of working with loops and how to combine these to make more effeciënt programs. Maple is more in the basic phase (I know how to search the solution to simple integrals etc.).
  2. A year after that we got an introduction to C++ in visual basic. The professor actually went to quick on this one, after 2 months we were already discussing pointers and inheritance. I am able to write simple programs in this language, only the working with classes is something that remains a difficulty, but I believe that's just a matter of practice.
  3. And a year later I needed to learn Mathematica 8 which I'm also really getting good at nowadays.

So I have a little bit of programming-expierince, but I won't say it's an incredible amount!

The reason I want to learn Fortran is mainly because you can't do theoretical physics on it's own, you need to test your theories and for that you'll need to be able to write programs which might be able to test those theories numerically. I heard that Fortran is kindoff the language to use when it comes to numerical mathematics, so I think it would come in handy to know this language.

ps: what's the difference between Fortran95 and Fortran 2003 ?

$\endgroup$
  • 3
    $\begingroup$ Can you be a bit more specific as to where you're coming from and where you want to go? Do you already know how to program, i.e. what other programming languages do you know? And what do you want to be able to do in Fortran? $\endgroup$ – Pedro Aug 25 '13 at 20:56
  • $\begingroup$ Yeah maybe I should have mentioned it in my question, allow me to edit :) @Pedro $\endgroup$ – Nick Aug 26 '13 at 18:44
  • 1
    $\begingroup$ I know it doesn't answer the question, but I have to strongly recommend you use Python over Fortran if you don't have specific performance needs as a beginner. You'll have less headaches dealing with the actual math and theory since Python will scream less at you. It also has incredible packages that make a lot of numerical math very simple (search SciPy, Numpy, Matplotlib). Those tools make Python very similar to MATLAB in its use, but you also have an entire language to build a good piece of software around as ewll. Not to mention it is open source. $\endgroup$ – hadsed Aug 27 '13 at 21:11
  • 1
    $\begingroup$ Fortran 95 is also Matlab like and an ISO standard. A lot of people using Python write computationally heavy kernels in C/C++ (even Fortran) which means that now you have to learn not one but two languages. One for performance and the other one for convenience/ease of use. $\endgroup$ – stali Aug 27 '13 at 21:54
4
$\begingroup$

I'm going to second stali's recommendation of Chapman's book. I learned from Chapman's Fortran 90 book, and it was very helpful. It will teach you the Fortran language. It will not teach you about good workflows for programming, and for that, you should look elsewhere after you've learned some Fortran and programmed for a little bit.

Most online resources to help you with Fortran programming are going to be Linux-based. Similarly, in computational science, most of the written materials are going to be geared towards Linux users. For that reason, I strongly recommend that you install VirtualBox (or some sort of software that enables you to run virtual operating systems) and then install some Linux distribution on there. If you've never used Linux before, a user-friendly distribution like Ubuntu, Mint, or Fedora is probably going to be your best bet. These distributions require a minimum amount of configuration if run in a virtual environment, and have a good GUI to help you navigate the operating system. Each of these distributions has a package manager that you can use to install some additional software. Using the package manager, you can install gfortran.

You're going to want to develop software in a text editor (or an IDE), so it's probably worth learning the basics of the command line. Software Carpentry has good resources for this task. (Disclaimer: I've taught part of a workshop for Software Carpentry.) Then you should be able to move forward with learning to write simple programs and compile them on the command line, and then learn how to use Makefile.

The problem with doing anything Windows-based is that most of the online materials you'll see when doing computational science are going to assume you're using Linux, and the advice (if it was any good to begin with) is going to break down, and cost you time. Cygwin is an API layer that adds more Linux-like functionality, but the correspondence with Linux isn't perfect, and software compiled for Linux is not going to work on your machine; you'll still need to get Windows-native versions or recompile the software for Windows. A Windows-native Fortran compiler such as Visual Fortran is going to cost money, and you're still going to have the problem that most Fortran users and computational scientists use Linux for their software development, meaning you'll have to forge ahead on your own. However, if you really need to distribute Windows-based software for some reason, then it makes sense to go through all the effort to figure out how to use something like Visual Fortran.

$\endgroup$
  • $\begingroup$ Thanks for the advice! But I am wondering, in stead of virtual machine, couldn't I create some kind of dual-boot mechanism in which I get to choose wether I start windows7 or Linux Ubuntu first? I believe it might indeed save me a lot of time! And if I needed to go to windows, then I'll have the programming experience of the Linux-version which allows me to go trough the steps on windows much quicker! $\endgroup$ – Nick Aug 26 '13 at 19:05
  • $\begingroup$ You could, if you so desire. I think the dual-boot setup is less flexible, and requires more technical knowledge and time (you may have to resize partitions, set up a boot loader the right way, etc.). I also think virtual machines are easier to migrate to a new machine, which can be helpful if you want to share your work with a colleague, or if you acquire a new computer and want to migrate all your work there. $\endgroup$ – Geoff Oxberry Aug 26 '13 at 22:08
  • $\begingroup$ I finally found some time to go trough all of the necesarry steps. I've got VirtualBox and I've got Linux (Ubuntu) installed. Now I was wondering which might be the best compiler? I can't seem to find much online about that part :s. I heard someone say gfortran ? $\endgroup$ – Nick Sep 12 '13 at 20:10
  • 1
    $\begingroup$ gfortran is free, and probably the path of least resistance. Most other compilers require buying a license, with possible exceptions for personal or academic use. (I know Intel's compiler can be downloaded and used for personal use only on a free basis, for instance; you'll have to read licenses for details.) HighPerformanceMark linked to a web site that has performance comparisons for compilers. $\endgroup$ – Geoff Oxberry Sep 12 '13 at 20:25
6
$\begingroup$

I think that the Metcalfe book is a good annotated guide to the modern features of Fortran, very useful if you already know an earlier version of the language but not a good introduction to programming in the language.

For my money the Chiver's and Sleightholme book and the Brainerd book are both good enough, though I don't think either is as good as the book by Ellis et al which, unfortunately, has not been updated for Fortran 2003 or later.

I have a strong aversion to the Chapman book. I can't remember why I disliked the 90/95 edition so much so can't justify this aversion any longer, you can discount it as a prejudice.

But the one book I recommend without hesitation is Arjen Markus' book. It's probably not an ideal introduction for someone still trying to figure out the basic features of the language, but once you've mastered those it's worth it's weight in some valuable substance.

As for which compiler to use, I use the Intel compiler and am generally happy with it. It integrates with Visual Studio, which might be important to you. The NAG compiler has better error and warning messages but produces slower (generally and your experience may differ) executables. The gfortran compiler is free and gets good enough reviews but it's not one I use. If you want to know more Polyhedron will tell you.

$\endgroup$
  • $\begingroup$ The Markus book has a great TOC. $\endgroup$ – AlexE Aug 27 '13 at 9:22
  • $\begingroup$ Yes the Markus book indeed seems a great book, so that's one I'll definetely consider! For the compiler I just need something with fast executables (no integration with visual studio needed). $\endgroup$ – Nick Aug 27 '13 at 20:28
  • $\begingroup$ I think the Intel compiler is generally considered fast. $\endgroup$ – AlexE Aug 28 '13 at 9:40
3
$\begingroup$

For books, I recommend "Modern Fortran Explained" by Metcalf/Reid/Cohen. It's more a reference than a textbook, but it'll serve you well if you already had some exposure to programming in general (with say C).

Besides that book, I strongly recommend Ondřej Čertík's website http://fortran90.org/. It won't teach you Fortran in the first place, but it'll provide you a set of best practices. Fortran allows various ways for programming - it's downward compatible after all. The best practices given there will help you develop a consistent, modern Fortran style. Also, the FAQ found there will be of interest to you.

$\endgroup$
  • 2
    $\begingroup$ Upvoted for the F90 best practices page, that was really helpful to me too. $\endgroup$ – Daniel Shapero Aug 26 '13 at 19:57
2
$\begingroup$

Chapmans book I think is more newbie friendly and a good reference. The authors on the second one are all experts but I think the book will be a good addition to your library at a later stage.

For start I would suggest that you stick to Fortran 95 and then later (if needed) move to Fortran 2003. Fortran 95 is easy to learn (almost Matlab like) and you'll be productive very quickly.

On Linux you have a number of free Fortran compilers (GNU, Solaris Studio, Open64 etc.) in addition to a richer set of tools e.g., valgrind, gdb, gnuplot, (for quick viz), svn/hg etc. Additionally installation of pre-compiled packages including numerical libs (blas/lapack/netcdf/fftw etc.) is a command away (at least on Debian with apt-get).

On Windows you might be better of with cygwin as it gives you easy and quick access to some of the above tools/libs in addition to the GNU Fortran compiler. So yes go to cygwin.com and install the core package along with gfortran and an editor of choice (vim is good). That should be a good start. Later if you anything else then I would suggest using the apt-cyg package for installing stuff.

$\endgroup$
  • 1
    $\begingroup$ I do not recommend vim (or, for that matter, emacs) as a first text editor. There's too many essential features (cut, copy, paste, find, find/replace) that involve nonintuitive key combinations to use it as a first text editor, and all of those extra, unfamiliar steps a new learner has to take to do anything simple is both discouraging and distracts from whatever they're trying to do (which, if you're recommending a text editor, is probably some form of software development). $\endgroup$ – Geoff Oxberry Aug 26 '13 at 7:24
  • 1
    $\begingroup$ As a first editor I recommend a tool which has the capability to be the only editor you ever need, not one which you will outgrow in 3 weeks, 3 months or even 3 years. For that reason both Emacs and Vim are suitable first editors. Sure they might have an initially-steeper learning curve than others but an awful lot of programmers have paid that price and found the rewards worth while. $\endgroup$ – High Performance Mark Aug 27 '13 at 9:20
  • $\begingroup$ Your comment sets up a false choice: learn vim/emacs and have an editor you can use for life, despite the steep learning curve, or use some other easy-to-learn inferior tool you'll outgrow. Plenty of professional programmers use other tools with less steep learning curves, like gedit, Sublime Text, TextMate, TextWrangler, and others, and get along just fine. Vim and emacs are great editors -- I use both -- but they're not the only, clearly superior choices out there for programming (WolfgangBangerth makes a good case for IDEs, too). $\endgroup$ – Geoff Oxberry Aug 28 '13 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.