You are on page 1of 7

6/29/2021 vasp - How to master Fortran with minimal effort?

- Matter Modeling Stack Exchange

Matter Modeling Stack Exchange is a


question and answer site for materials
modelers and data scientists. It only takes
a minute to sign up.

Join this community

Anybody can ask a question

Anybody can answer

The best answers are voted


up and rise to the top

Matter Modeling Beta

How to master Fortran with minimal effort?


Asked
2 days ago Active
today Viewed
3k times

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.
13 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 .

5 vasp quantum-espresso programming fortran

Share Cite Improve this question edited yesterday asked 2 days ago
Follow Ian Bush Jack
111 3 11.6k 1 23 62

4 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.
– tmph
2 days ago

5 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.
– wzkchem5
yesterday

3 The first thing you need to learn is that the quality of almost all Fortran code written by "academics" is
https://mattermodeling.stackexchange.com/questions/6252/how-to-master-fortran-with-minimal-effort 1/7
6/29/2021 vasp - How to master Fortran with minimal effort? - Matter Modeling Stack Exchange
3 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.
– alephzero
yesterday

1 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.
– Ian Bush
21 hours ago

3 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.
– J...
16 hours ago

6 Answers Active Oldest Votes

"How to master Fortran programing with minimal effort?"

15
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
https://mattermodeling.stackexchange.com/questions/6252/how-to-master-fortran-with-minimal-effort 2/7
6/29/2021 vasp - How to master Fortran with minimal effort? - Matter Modeling Stack Exchange

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.

Share Cite Improve this answer edited yesterday answered 2 days ago
Follow Buttonwood Nike Dattani
347 1 5 20.5k 3 62 156

Nice video tutorial!


–  Jack
yesterday

1 OOP is added in Fortran2003.


–  Jack
yesterday

Correct. That's why I said "most Fortran code is not object-oriented" with emphasis on "most". I'm glad the
video tutorial helped!
– Nike Dattani
yesterday

1 Also for mastering usually one should also read the compiler output in assembler to get a decent mental
mapping function.
– lalala
yesterday

3 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.
– Ian Bush
21 hours ago

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
13 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
f b k It i itt i i it t tb k t l dI
https://mattermodeling.stackexchange.com/questions/6252/how-to-master-fortran-with-minimal-effort
f t it th 3/7
6/29/2021 vasp - How to master Fortran with minimal effort? - Matter Modeling Stack Exchange
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).

Share Cite Improve this answer edited yesterday answered yesterday


Follow Tyberius ♦ Vladimir F
9,939 3 18 90 311 1 5

1 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.
– alephzero
yesterday

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
9 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
https://mattermodeling.stackexchange.com/questions/6252/how-to-master-fortran-with-minimal-effort 4/7
6/29/2021 vasp - How to master Fortran with minimal effort? - Matter Modeling Stack Exchange

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.

Share Cite Improve this answer edited yesterday answered yesterday


Follow King
191 3

New contributor

2 There is also a new Fortran discourse which is very welcoming to new users and is fairly active.
– 
Rashid Rafeek
19 hours ago

1 @Rashid Rafeek, thanks for the link, VladimirF had this already mentioned in his response, so that is why I
did not repeat it.
– King
16 hours ago

Oh thanks. Hadn't noticed that.


– Rashid Rafeek
16 hours ago

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

7 (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
k d
https://mattermodeling.stackexchange.com/questions/6252/how-to-master-fortran-with-minimal-effort 5/7
6/29/2021 vasp - How to master Fortran with minimal effort? - Matter Modeling Stack Exchange
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.

Share Cite Improve this answer Follow answered yesterday


Buttonwood
347 1 5

I've done that professionally.

5 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.

Share Cite Improve this answer edited 15 hours ago answered 17 hours ago
Follow JDługosz
https://mattermodeling.stackexchange.com/questions/6252/how-to-master-fortran-with-minimal-effort 6/7
6/29/2021 vasp - How to master Fortran with minimal effort? - Matter Modeling Stack Exchange
Follow JDługosz
151 2

New contributor

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.
– Hristo Iliev
2 hours ago

I have not had the pleasure of programming Fortran, but have learned and used many other
languages over decades, both privately and commercially.
4
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.

Share Cite Improve this answer edited 19 hours ago answered 23 hours ago
Follow Ian Bush AnoE
111 3 141 2

New contributor

https://mattermodeling.stackexchange.com/questions/6252/how-to-master-fortran-with-minimal-effort 7/7

You might also like