18
$\begingroup$

Fortran language is used in many first-principles matter modeling codes, such as VASP and QE. These codes usually include many files and thousand of lines and are not readable for a newbie. Are there good strategies and resources to master the Fortran language with minimal effort if I have some experience with the PYTHON language? The final purpose is to read and modify the existing Fortran codes such as QE.

$\endgroup$
8
  • 6
    $\begingroup$ Is there anything in particular with which you're struggling? The easiest way to learn a programming language (in my experience) is to practice programming in it. $\endgroup$ – tmph Jun 27 at 1:47
  • 6
    $\begingroup$ When I was learning Fortran, I first tried to write a few simple, standalone programs, such as a program that computes all prime numbers in a certain range, or simulates the three-body problem, or calculates the rotational constants of a molecule. This helped a lot, and when I finished quite a few such small programs I felt sufficiently confident about my Fortran skills that I could move to the actual codebase that I have to familiarize myself with. $\endgroup$ – wzkchem5 Jun 27 at 8:15
  • 7
    $\begingroup$ The first thing you need to learn is that the quality of almost all Fortran code written by "academics" is atrocious. That cuts both ways: if you are in academia the bar is set so low you can learn as much as your colleagues know about software development in an afternoon, but on the other hand if you want to learn Fortran (or any other language) properly, all the code you are likely to see is worthless as an example of good practice. $\endgroup$ – alephzero Jun 28 at 3:49
  • 2
    $\begingroup$ Just because it has come up quite a few times note Fortran is officially spelt lower case, and that has been the case for over 30 years now. $\endgroup$ – Ian Bush Jun 28 at 10:34
  • 5
    $\begingroup$ You can't master anything with minimal effort. Even if you're a genius. Get used to it. Gaining skills requires work. It's really as simple as that. $\endgroup$ – J... Jun 28 at 15:08
25
$\begingroup$

"How to master Fortran programing with minimal effort?"

The concept of "mastering" a programming language is a bit subjective, and people typically don't "master" a language with "minimal effort".

However as a Fortran programmer myself, there's two strategies I can recommend:

(1) Tutorials: As with any programming language, there's plenty of books and tutorials for Fortran, and some of these come with exercises you can do to help you learn the language. Here's some of them:

(2) Practicing: As the comment by user tmph says the best way is often just to practice. When I began Fortran as an undergrad summer student, I was told ahead of time that I'd be coding in Fortran, and I dreaded it. I went to the library and picked up a bunch of books (online tutorials and youtube videos were unheard of at the time), thinking that I'd learn based on strategy #1 above. It turns out though that I learned much faster "by doing" rather than by reading.

I recommend playing around with existing Fortran code, and adding simple features with gradually increasing complexity. Changing the number of digits for a numerical output in QE is an easy thing that I think you can try doing right now! Next you can add more output (more words, more numbers, etc.) You could also try to add new input flags for the feature you want to implement, and at every step of the way, see if it compiles, and follow the error messages at compile time: I learned so much Fortran that way! Very soon you'll find yourself feeling quite comfortable and you'll gain your confidence this way!

I find Fortran to actually be in many respects, simpler and easier to master than Python, especially since most Fortran code is not object-oriented, and you'll see that it's really designed for scientific computing (the way you can output numerical values for example, will probably convince you of this). Python was not originally designed for number crunching, and neither was C or C++ or Java: these languages were designed to be more general, and are used for writing everything from operating systems, to GUIs, to web applications, to computer games, etc., whereas Fortran was literally designed for number crunching (a bit like MATLAB and Julia, the latter which was discussed here at MMSE) so once you spend some time familiarizing yourself with it, I'm confident that you'll find it much more natural for scientific computing than other languages you've used in the past.

$\endgroup$
5
  • 1
    $\begingroup$ Nice video tutorial! $\endgroup$ – Jack Jun 27 at 23:33
  • 1
    $\begingroup$ OOP is added in Fortran2003. $\endgroup$ – Jack Jun 27 at 23:34
  • 2
    $\begingroup$ Correct. That's why I said "most Fortran code is not object-oriented" with emphasis on "most". I'm glad the video tutorial helped! $\endgroup$ – Nike Dattani Jun 27 at 23:44
  • 2
    $\begingroup$ Also for mastering usually one should also read the compiler output in assembler to get a decent mental mapping function. $\endgroup$ – lalala Jun 28 at 7:07
  • 5
    $\begingroup$ I will just note that I totally disagree with having to read the assembler. That might give you information on how one implementation of Fortran works, it doesn't help you learn the language. $\endgroup$ – Ian Bush Jun 28 at 10:31
18
$\begingroup$

Fortran is a language that evolved strongly and a programmer just maintain very old legacy codes will have very different needs from a programmer maintaining codes written in Fortran 95 and different needs will have a programmer who wants to employ modern techniques and develop new program that use Fortran 2003, 2008 and 2018 (and future revisions).

The amount of features that appear in the successive revisions increases strongly, the biggest steps being Fortran 90 and Fortran 2003.

Fortran 90 was completely superseded by Fortran 95 which fixed several defects or inconsistencies.

Fortran 77 and earlier are very old and the programs often use techniques that are strongly discouraged today (and even deleted from the standard or marked obsolescent).

Fortran 2003 brought object orientation, portable interfacing to C and much more. Fortran 2008 added smaller enhancement and parallel programming using coarrays.

Fortran can be learnt from book and there is a choice different styles of introductory books. I always preferred the Fortran Explained (Fortran 90 explained, Fortran 2003 explained, Modern Fortran Explained) by Metcalf, Reid and Cohen. However, it is rather terse and can also be used as a reference book. It is written in a university textbook style and I prefer to use it as the recommended reference for my course.

Some other books are oriented more practically, like the Fortran for Scientists and Engineers series by Chapman or Modern Fortran in Practice by Markus.

There are several other books that I did not have in my hand. Modern Fortran: Style and Usage by Norman and Spector is for more advanced programmers who want to use good style but know the basic language.

You can get a lot of help at Stack Overflow in the Fortran tag https://stackoverflow.com/questions/tagged/fortran .

When it comes to online tutorials, be aware that there are many obsolete ones. https://www.fortran90.org/ is not that old, even if missing more advanced modern stuff, and the author Ondřej Čertík is active in the standard committee developing future Fortran standards.

The community dealing with modern Fortran, developing a community software ecosystem, libraries and so on meets at https://fortran-lang.discourse.group/ and will be happy to discuss any interesting Fortran topic. The usenet channel comp.lang.fortran can also be used for discussion with experts and contains a lot of useful information in its history.

An interesting option to try Fortran is the https://lfortran.org/. A compiler that is still in development, but enables you to try Fortran interactively in notebooks you might know from Python or Julia (Jupyter).

$\endgroup$
1
  • 1
    $\begingroup$ Having programmed Fortran for more than 40 years, in pretty much every version from the IBM 1130 up to Fortran 2018, IMO Metcalf, Reid and Cohen would be a terrible book to learn the language from. In fact it isn't a good reference book either, because the organization is chaotic (far too many forwards-and-backwards cross references) making it nearly impossible to know if you have found all the information about any particular topic. Trying to cover every version from Fortran 90/95 to the latest one, in historical order, is a very good way to confuse the reader. $\endgroup$ – alephzero Jun 28 at 3:42
12
$\begingroup$

The answers provided by others are excellent and rather comprehensive. I would only introduce a few other resources that could help you down the road to find answers to problems more efficiently. Several Fortran communities can be extremely helpful in learning optimization and performance tips or simply to seek help from experts:

  1. The Intel Fortran forum has over 130,000 Fortran-related questions and posts, as far I as remember from a few years ago that I checked. There are also many industries and academia experts, as well as Fortran Standard Committee members there ready to take challenging questions and to guide and educate people (I have no connection to the Intel HPC team, just a user).
  2. The NVIDIA Fortran forum is another place to seek help, although NVIDIA is more focused on CUDA and GPU-enabled Fortran.
  3. The Reddit Fortran community has close to 6,000 Fortran programmers and enthusiasts already who can help with a diverse range of Fortran-related questions or direct you to relevant people or communities for further help.

There are likely many more resources that I am simply missing in the above list. There are also online Fortran compilers like the following that can aid rapid limited testing of ideas:

  1. tutorialspoint
  2. rextester
  3. onlinegdb
  4. jdoodle
  5. godbolt (for inspecting the generated assembly code)

and Fortran Jupyter bindings for serial and parallel Fortran programming like OpenCoarrays Jupyter Binder.

$\endgroup$
3
  • 2
    $\begingroup$ There is also a new Fortran discourse which is very welcoming to new users and is fairly active. $\endgroup$ – Rashid Rafeek Jun 28 at 12:17
  • 1
    $\begingroup$ @Rashid Rafeek, thanks for the link, VladimirF had this already mentioned in his response, so that is why I did not repeat it. $\endgroup$ – King Jun 28 at 15:00
  • $\begingroup$ Oh thanks. Hadn't noticed that. $\endgroup$ – Rashid Rafeek Jun 28 at 15:27
12
$\begingroup$

I've done that professionally.

In the early '90's, I worked at IBM and spent some time porting FORTRAN "codes" to work on the new RISC SYSTEM / 6000.

I found that FORTRAN was such a simple language that it could be worked with without "knowing" it, per se. I obtained a FORTRAN 77 text book, which was rather small and slim, about the same volume as a paperback novel though the pages were larger and the the number of pages smaller. With the text book, I easily looked up the meaning of any non-obvious construct and precise details like the exact order of precedence, when needed.

The real problem with these "codes" isn't the nuances of the FORTRAN language, but the code itself. They are typically written by people who are not software engineers, and cobbled together over time. Worse, are complex programs that go to great lengths to exceed the natural bounds of the language: I recall MODFLOW implementing its own memory management in order to do dynamic data structures as subscripts in static arrays; and more generally the lack of structures, so related data is spread out over separate named variables or parallel arrays. Of course, we only had current source code, zero design documents or anything explaining the code organization.

Point is, your "thousands of lines" might not be readable even when you are fluent in exactly what the FORTRAN statements mean. Now, with the World Wide Web, you can easily find some reference material for the language itself, and since you already know how to program it will be no problem understanding it all.


"Modern" Fortran is a more complex language, but I expect it's just a matter of learning the specific syntax as the concepts are no different than what is found in other languages; e.g. structures (use % instead of .), modules (like Modula-2), generics (like Ada), block-structured statements (any modern procedural language), etc.

$\endgroup$
2
  • 4
    $\begingroup$ I registered just to upvote. People often focus too much on mastering the language and forget that it is just a tool to express an idea. $\endgroup$ – Hristo Iliev Jun 29 at 5:09
  • 1
    $\begingroup$ @HristoIliev fitting, since I had registered just to post the answer. $\endgroup$ – JDługosz Jun 29 at 14:33
10
$\begingroup$

Extending the answer by Nike Dattani separately rather than an addition in the text:

(2) Practicing, bis: After gaining some familiarity with the language, join a "drill site" like codewars.com where you may train yourself (dojo-like) according to difficulty, or subject. In addition to "you passed the test", there is a community-based rating for solutions by others to the same problem (and if you know an other language than Fortran, you may tackle the problem again there).

Open an account on a platform like GitHub. Aside from learning version control, you may draw inspiration and spur collaboration from projects you identified by a search with Fortran as a keyword.

(3) user groups / conferences: Get in touch with others already using the language. In the best case, this will be an exchange of mutual benefit. You might like the idea to have a sparring partner, especially if you are not sure if a concept was understood by you. It may show you applications of Fortran you did not anticipate yet. An example was the eventually virtual FortanCon2020, with video recordings available, yet the monthly video calls on fortran-lang.org (e.g., April 2021, code-curation of rocket programs) equally may contain such insights.

$\endgroup$
9
$\begingroup$

If you already know Python, perhaps you could try to convert Python code to Fortran code. That is probably the fastest way to get to know the language. Unless you go from one paradigm to another programming principals are mostly the same and it is syntax that differentiates one language from another. If you go through books and tutorials then you will probably be "reinventing the wheel" to some extent by going over familiar ground.

$\endgroup$
3
  • 1
    $\begingroup$ This is one of the reasons I like the concept of codewars.com mentioned. You train on a problem (often set agnostic of the language you train), with the integrated tests you check if it is working well enough. When done with language A, you train again, now in language B. While the coverage for Fortran still is less than the for Python, you may adjust the kata accordingly. You may run it locally, too, as you find much / all (incl. test frameworks) on GitHub. Plus the guiding stars by others' solutions. $\endgroup$ – Buttonwood Jun 29 at 9:28
  • $\begingroup$ After your comment I tried out codewars again and thanks, it is a good suggestion. I like that after you finish a task you can see how other people have solved it. I then find out there are much more succinct ways of doing it. $\endgroup$ – Tman Jun 30 at 10:52
  • $\begingroup$ I share the experience to be surprised again and again how some basic 101-textbook approaches are replaced in solutions by others in the field. Speaking for Python, as an example, the initial presentation of list comprehension seen in a book was still something strange for me, at best. This perception passed for the better; after multiple kata eventually showcasing a "more Pythonic / idiomatic" approach, I started to appreciate it. $\endgroup$ – Buttonwood Jun 30 at 17:49
8
$\begingroup$

I have not had the pleasure of programming Fortran, but have learned and used many other languages over decades, both privately and commercially.

My preferred way of diving into a new language may be a bit old-fashioned. I normally stay very much clear of "tutorials" and the like. First thing I do is I grab the official reference documentation - or, if you are lucky, a more informal long-form text (in the old days, a book; but today, plenty of languages have this kind of description as well, next to a more dry reference) - but only if it comes from the same source as the language itself, whatever that may be.

For example, to learn Ruby, I first read a seminal book on it (Programming Ruby), then I actually read quite a few of the dry reference pages from top to bottom (e.g. Array).

For C, I did (back in the day) indeed buy The C Programming Language and read it completely (obviously speed-reading/glossing over some bits).

For Fortran, start at fortran-lang.org and pick those documentations they suggest for you; maybe do one of the quick tutorials they link, but take everything written in a tutorial with a grain of salt. Not because it's necessarily wrong, but because they are by nature very much condensed, and by taking them as gospel might hurt you very much in the long term.

As soon as you have a general feel of the language (which you, for your current background, probably already have), get the most technical, most complete documentation you can find and stomach, and go through it front to back. You do not need to read, much less understand every single word, but you should know all the major concepts - at least know that they exist, and where you have to look when you need actual hard facts during development.

$\endgroup$
4
$\begingroup$

Mastery and minimal effort create a large conflict. It takes significant effort to master a computer language, beyond just learning the syntax. So, I'd first set my expectations that I need to work hard.

Next, yes, the previous authors have very sound suggestions and I'll respectfully not repeat them here.

One technique I've used in my many decade career (and yes, I did Fortran programming back in 1971), is to take a task for which I already have a sound algorithm and implementation in another language, and implement in the new language. It helps understand syntax and semantics of the new language, in more detail, as it is applied to fundamental constructs - data types, memory management, logic, looping, arrays, structures, functions, I/O, string formatting (and reading and writing and parsing), etc. Running the same program in different languages and then comparing (CPU time, memory usage, I/O, the actual results) between the two, which should be very similar in almost all cases, will illuminate important items for deeper learning when results are vastly different. My favorite piece of code is a raytracer, which I've implemented in Fortran, C, C++, .NET, JAVA, and most recently in GPU coding.

My final thought is that much more effort "mastering" the various libraries supplied or available in the langauge (ie, GUI, numerical algorithms, I/O, tasking, memory management, communicating with other running tasks, etc.) takes much more effort than learning the language syntax, and is a worthwhile expenditure of your time and energy. After all, then you are building on the shoulders of those who built the libraries for you.

$\endgroup$
1
$\begingroup$

The best start for learning Fortran it to try to ignore the newer languages you have learned. Fortran is an early language that was very powerful at the time. It is not "Object Oriented" and is not geared for dealing with bits, bytes, addresses. I have taught Assembler, Visual Basic, RPG, PL1, Pascal, Fortran, C, & C++ in colleges. But I will admit that Fortran (IV, 77) was one of my favorites. Many of these were on the on mainframes.

$\endgroup$
1

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.