Preface
There is a language you have almost certainly used without knowing it. When you checked the weather this morning, a Fortran program made the forecast. If you have flown, Fortran helped shape the wing. The climate projections in the news, the simulations that certify a nuclear reactor, the quantum- chemistry codes that predict whether a drug molecule will bind — these run on Fortran, today, on the largest computers humanity has ever built. Fortran is not a museum piece. It is infrastructure.
And yet almost everyone believes it is dead. Students are taught that it belongs to the era of punch
cards and GOTO. Working scientists inherit a Fortran code, assume it is a relic, and try to rewrite
it in something "modern" — only to discover that the "old" code is faster, that nobody fully
understands the physics encoded in it, and that a summer of rewriting has reproduced perhaps a tenth of
its capability with new bugs. The gap between Fortran's reputation and Fortran's reality is enormous,
and it costs the scientific world real time and money.
This book exists to close that gap.
Why this book
If you go looking for a book to learn Fortran, you will find a small shelf. There is Chapman, excellent and widely taught, but expensive and light on the features added since 2003. There is Modern Fortran Explained by Metcalf, Reid, and Cohen — the definitive reference, but a reference, not a book you learn from cold. There is Curcic's fine Modern Fortran, written for people who already program well. And there is a scatter of online tutorials, many of them still teaching the language as if it were 1985.
What has not existed is a single, free, comprehensive textbook that (1) teaches Modern Fortran from scratch, assuming only that you have programmed something before; (2) covers the language as it actually is in 2018-and-later — modules, derived types, object orientation, coarrays, C interoperability; (3) connects the language to the scientific computing it was built for, with real numerical methods on real problems; and (4) takes parallel programming seriously, because the whole point of Fortran is to go fast, and going fast now means going parallel. This book is our attempt at that missing text, and it is free because the science that depends on Fortran belongs to everyone.
Who it is for
You should read this book if you are a graduate student in physics, chemistry, engineering, or an earth science who has inherited a research code and needs to read, run, and extend it. If you are a computational scientist whose Python or MATLAB has hit a wall and you need the ten-to-a-hundred-fold speedup that a compiled, array-oriented language can give you. If you are an engineer maintaining a CFD, finite-element, or reactor code and you want to modernize it without breaking the validated science inside. Or if you are a computer-science student who wants to understand why, decades after its obituary was first written, this language refuses to die.
You need to have programmed before — in any language. You need to know what a variable, a loop, and a function are. You need enough mathematics to be comfortable with a matrix and a derivative. You do not need to know any Fortran, any high-performance computing, or anything about compilers or parallel hardware. All of that we will build from the ground up.
How it is written
The voice of this book is a practitioner's. Imagine a computational scientist who has written Fortran for twenty years, who maintains a simulation with more lines than a small city has people, and who can tell you — precisely, with a benchmark — why that simulation is in Fortran and not in C++. That is the register: direct, practical, honest about the language's sharp edges, and never apologetic about its age. We will celebrate what Fortran does better than anything else, we will be candid about what it does badly, and we will teach the modern style that makes it a genuine pleasure to write.
Every example in the book is real, compilable code — not pseudocode — written in modern style and shown with the command that compiles it and the output it produces. Because we care about your trust, we will be strict about one thing: no program in this book was run while it was written. Every "expected output" was computed by hand and can be checked by you when you compile it yourself. A wrong worked example is worse than none at all, and we would rather you catch us than mislead you.
What you will build
Reading about a language teaches you the vocabulary; building something teaches you the language. So running through all forty chapters is one continuous project: a two-dimensional heat-equation solver. It begins in Chapter 2 as a program that prints its own name. Chapter by chapter it acquires arrays, procedures, modules, a proper data structure, file input and visualization output, a real finite-difference numerical core, tests, and finally parallelism — first across the cores of one machine, then across the nodes of a cluster. By the capstone you will have written, and completely understand, a modular, validated, optimized, parallel scientific simulation that produces publication-quality figures. It is a small thing next to a climate model. It is built exactly the same way.
Welcome. The language that runs the world's supercomputers is more approachable than its reputation suggests, and it is waiting for you to compile your first program. Let us begin.