Fortran language is used in many first-principles matter modeling codes, such as
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
Fortran language is used in many first-principles matter modeling codes, such as
"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:
- Our coding club Fortran intro
- Fortran-lang.org (there's a multitude of links to other tutorials and books there!)
- FORTRANtutorial.com (a 7 lesson course)
- Fortran tutorial youtube video (for those that like to listen rather than read)
(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.
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.
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).
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.
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:
- 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).
- The NVIDIA Fortran forum is another place to seek help, although NVIDIA is more focused on CUDA and GPU-enabled Fortran.
- 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:
and Fortran Jupyter bindings for serial and parallel Fortran programming like OpenCoarrays Jupyter Binder.
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.
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.
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 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.
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.
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.