Modern Fortran
Milan Curcic
  • MEAP began March 2018
  • Publication in Spring 2020 (estimated)
  • ISBN 9781617295287
  • 475 pages (estimated)
  • printed in black & white

If I've would had this book when writing my master thesis in astrophysics I would have spared a lot of time in learning how to write the code for my calculations.

Davide Cadamuro
Using Fortran, early and accurate forecasts for hurricanes and other major storms have saved thousands of lives.

Using Fortran, better designs for ships, planes, and automobiles have made travel safer, more efficient, and less expensive than ever before.

Using Fortran, low-level machine learning and deep learning libraries provide incredibly easy, fast, and insightful analysis of massive data for science, business, and industry.

Fortran is an amazingly powerful and flexible programming language that forms the foundation of high performance computing for research, science, and industry. And it's come a long, long way since starting life on IBM mainframes in 1956. Modern Fortran is natively parallel, so it's uniquely suited for efficiently handling problems like complex simulations, long-range predictions, and ultra-precise designs. If you're working on tasks where speed, accuracy, and efficiency matter, it's time to discover—or re-discover—Fortran.
Table of Contents detailed table of contents

Part 1: Getting started with modern Fortran

1 Introducing Fortran

1.1 What is Fortran?

1.2 Fortran features

1.3 Why learn Fortran?

1.4 Advantages and disadvantages

1.4.1 Side-by-side comparison with Python

1.5 The parallel Fortran mental model

1.6 What will you learn in this book?

1.7 Think parallel!

1.7.1 Copying an array from one processor to another

1.8 A Partitioned Global Address Space language

1.9 Running example: A parallel tsunami simulator

1.9.1 Why tsunami simulator?

1.9.2 Shallow water equations

1.9.3 What we want our app to do

1.10 Summary

2 Getting started: Minimal working app

2.1 Compiling and running your first program

2.2 A minimal working app: Moving a blob in space

2.2.1 What should our app do?

2.2.2 What is advection?

2.2.3 Implementation strategy

2.2.4 Defining the main program

2.2.5 Declaring and initializing variables

2.2.6 Numeric data types

2.2.7 Declaring variables

2.2.8 Declaring constants

2.2.9 Declaring arrays

2.2.10 Portable data types with iso_fortran_env

2.2.11 Using a do-loop to iterate

2.2.12 Setting initial conditions for the blob

2.2.13 Predicting the movement of the blob

2.2.14 Writing to screen

2.2.15 Putting it all together

2.3 Going forward

2.4 New Fortran elements, at a glance

2.5 Summary

Part 2 Core elements of Fortran

3 Writing reusable code with procedures and modules

3.1 Toward higher app complexity

3.1.1 Adding terms to the tsunami simulator

3.2 An overview of Fortran program units

3.3 Revisiting the cold front problem

3.4 Don’t repeat yourself, use procedures!

3.4.1 Functions

3.4.2 Subroutines

3.5 Using pure procedures to avoid side effects

3.5.1 What is a pure procedure?

3.5.2 Some restrictions on pure procedures

3.5.3 Why are pure functions important?

3.6 Writing procedures that operate on both scalars and arrays

3.7 Storing globally accessible data and procedures in modules

3.7.1 Defining a module

3.7.2 Accessing procedures and variables in a module

3.7.3 Controlling the visibility and access to variables and procedures

3.8 Incorporating modules and procedures in the Tsunami app

3.8.1 Refactoring the minimal working app with modules and functions

3.8.2 Toward realistic wave simulations

3.9 Answer key

3.9.1 Exercise 1: Calculating the temperature due to cold front passage

3.9.2 Exercise 2: Cold front solution using an external function.

3.9.3 Exercise 3: Modifying state with a subroutine

3.9.4 Exercise 4: Writing an elemental function that operates on both scalars and arrays

3.10 Summary

4 Organizing your Fortran code using modules

4.1 Accessing a module

4.1.1 Getting compiler version and options

4.1.2 Using portable data types

4.2 Creating your first module

4.2.1 A structure of a custom module

4.2.2 Defining a module

4.2.3 Compiling Fortran modules

4.2.4 Controlling the visibility and access to variables and procedures

4.2.5 Putting it all together in the Tsunami simulator

4.3 Toward realistic wave simulations

4.3.1 A brief look at the physics

4.3.2 Update to the finite difference calculation

4.3.3 Renaming imported entities to avoid name conflict

4.3.4 The complete code

4.4 Answer key

4.4.1 Exercise 1: Using portable type kinds in the Tsunami simulator

4.4.2 Exercise 2: Define the set_gaussian subroutine in a module

4.5 New Fortran elements, at a glance

4.6 Summary

5 Analyzing time series data with arrays

5.1 Analyzing stock prices with Fortran arrays

5.1.1 Objectives for this exercise

5.1.2 About the data

5.1.3 Getting the data and code

5.2 Finding the best and worst performing stocks

5.2.1 Declaring arrays

5.2.2 Array constructors

5.2.3 Reading stock data from files

5.2.4 Allocating arrays of certain size or range

5.2.5 Allocating an array from another array

5.2.6 Cleaning up after use

5.2.7 Checking for allocation status

5.2.8 Catching allocation and deallocation errors

5.2.9 Implementing the CSV reader subroutine

5.2.10 Indexing and slicing arrays

5.3 Identifying risky stocks

5.4 Finding good times to buy and sell

5.5 Answer Key

5.5.1 Exercise 1: Convenience (de)allocator subroutines

5.5.2 Exercise 2: Reversing an array

5.5.3 Exercise 3: Computing the moving average over custom time period.

5.6 Summary

6 Reading, writing, and formatting your data

6.1 Your first I/O: Input from keyboard and output to screen

6.1.1 The simplest I/O

6.1.2 Reading and writing multiple variables at once

6.1.3 Standard input, output, and error

6.2 Formatting numbers and text

6.2.1 Designing the aircraft dashboard

6.2.2 Formatting strings, broken down

6.2.3 Format statements in legacy Fortran code

6.3 Writing to files on disk: A minimal note-taking app

6.3.1 Opening a file and writing to it

6.3.2 Opening a file

6.3.3 Writing to a file

6.3.4 Appending to a file

6.3.5 Opening files in read-only or write-only mode

6.3.6 Checking if a file exists

6.3.7 Error handling and closing the file

6.4 Answer Key

6.4.1 Exercise: Redirect stdout and stderr to files

6.5 New Fortran elements, at a glance

6.6 Summary

Part 3: Advanced Fortran use

7 Going parallel with Fortran coarrays

7.1 Why write parallel programs

7.2 Processing real-world weather buoy data

7.2.1 About the data

7.2.2 Getting the data and code

7.2.3 Objectives

7.2.4 Serial implementation of the program

7.3 Parallel processing with images and coarrays

7.3.1 Fortran images

7.3.2 Getting information about the images

7.3.3 Telling images what to do

7.3.4 Gathering all data to a single image

7.4 Coarrays and synchronization, explained

7.4.1 Declaring coarrays

7.4.2 Allocating dynamic coarrays

7.4.3 Sending and receiving data

7.4.4 Controlling the order of image execution

7.5 Toward the parallel Tsunami simulator

7.5.1 Implementation strategy

7.5.2 Finding the indices of neighbor images

7.5.3 Allocating the coarrays

7.5.4 The main time loop

7.6 Answer key

7.6.1 Exercise 1: Finding the array sub-ranges on each image.

7.6.2 Exercise 2: Writing a function that returns the indices of neighbor images

7.7 Summary

8 Working with abstract data using derived types

8.1 Recasting the tsunami simulator with derived types

8.2 Defining, declaring, and initializing derived types

8.2.1 Defining a derived type

8.2.2 Instantiating a derived type

8.2.3 Accessing derived type components

8.2.4 Positional vs. keyword arguments in derived type constructors

8.2.5 Providing default values for derived type components

8.2.6 Writing a custom type constructor

8.2.7 Custom type constructor for the Field type

8.3 Binding procedures to a derived type

8.3.1 Your first type-bound method

8.3.2 Type-bound methods for the Field type

8.3.3 Controlling access to type components and methods

8.3.4 Bringing it all together

8.4 Extending tsunami to two dimensions

8.4.1 Going from 1-d to 2-d arrays

8.4.2 Updating the equation set

8.4.3 Finite differences in x and y

8.4.4 Passing a class instance to diffx() and diffy()

8.4.5 Derived type implementation of the tsunami solver

8.5 Answer Key

8.5.1 Exercise 1: Working with private components

8.5.2 Exercise 2: Invoking a type-bound method from an array of instances

8.5.3 Exercise 3: Computing finite difference in y-direction.

8.6 Summary

9 Generic procedures and operators for any data type

9.1 Analyzing weather data of different types

9.1.1 About the data

9.1.2 Objectives

9.1.3 Strategy for this exercise

9.2 Type systems and generic procedures

9.2.1 Static versus strong typing

9.3 Writing your first generic procedure

9.3.1 The problem with strong typing

9.3.2 Writing the specific functions

9.3.3 Writing the generic interface

9.3.4 Results and complete program

9.4 Getting into Fortran’s operators

9.4.1 What’s an operator?

9.4.2 Things to do with operators

9.4.3 Fortran’s built-in operators

9.4.4 Operator precedence

9.4.5 Writing custom operators

9.4.6 Overloading intrinsic operators

9.5 Generic procedures and operators in the tsunami simulator

9.5.1 Overloading operators for the Field type

9.6 Answer key

9.6.1 Exercise 1: Specific average function for a derived type

9.6.2 Exercise 2: Overloading the string concatenation operator

9.7 Summary

10 Overloading operators for derived types

10.1 Happy Birthday! A countdown app

10.1.1 Some basic specification

10.1.2 Implementation strategy

10.2 Getting user input and current time

10.2.1 Your first datetime class

10.2.2 Reading user input

10.2.3 Getting current date and time

10.3 Calculating the difference between two times

10.3.1 Modeling a time interval

10.3.2 Overloading the subtraction operator

10.3.3 Time difference algorithm

10.3.4 The complete program

10.4 Overloading operators in the Tsunami simulator

10.4.1 A refresher on the Field class

10.4.2 Implementing the arithmetic for the Field class

10.4.3 Syncing parallel images on assignment

10.5 Answer key

10.5.1 Exercise 1: Validating user input

10.5.2 Exercise 2: Leap year in the Gregorian calendar

10.5.3 Exercise 3: Implementing a type-bound method to overload the + operator for two Field instances

10.6 New Fortran elements, at a glance

10.7 Summary

Part 4 The final stretch

11 Advanced parallelism with teams and events

12 Interoperability with C Exposing your app to the web


Appendix A: Setting up the Fortran development environment

A.1 Editing Fortran source files

A.2 Setting up the compiler and libraries

A.2.1 Setting up the Fortran compiler

A.2.2 Setting up the MPI library

A.2.3 Setting up OpenCoarrays

Appendix B: From calculus to code

B.1 The advection equation explained

B.1.1 Discretizing the derivatives

B.1.2 Casting the derivatives into code

Appendix C: Further reading

Appendix D: Glossary

About the Technology

Fortran is a general-purpose programming language that is dominant in engineering and scientific computing applications. It’s the leading language used in high performance (HPC) and parallel programming. While Fortran is the oldest high-level programming language, it is constantly improving. The most recent major revision of the standard, Fortran 2008, added native support for parallel programming on shared-and distributed-memory architectures, and will be one of few languages used for next-generation exascale HPC applications. Fortran 2018 will include additional parallel features and improve interoperability with C.

A few Fortran facts:

  • If you read or listened to the weather forecast this morning, you used Fortran.
  • You’ll find Fortran at the lowest levels of most popular data analysis and machine learning frameworks
  • Fortran is the reference benchmark language used to measure the performance of the fastest supercomputers in the world (
  • Fortran is the only natively-parallel programming language steered by an International Standards Committee with scientists and engineers as its target audience
  • Fortran is the dominant language of the High Performance Computing, spanning industries from applied mathematics, fluid and aerodynamics, to weather, ocean, and climate prediction

Simply put, the future is in parallel, and Fortran is already there.

About the book

Modern Fortran: Building Efficient Parallel Applications teaches you how to develop fast, efficient parallel applications with Fortran. To warm up, you'll start by building a simple application in modern Fortran, with a focus on reusable modules, functions, and subroutines. Next, you'll dive into Fortran's unique approach to parallel programming with coarrays. As you work through this practical guide, you'll systematically explore all the really interesting stuff: how to overload operators and generic functions, work with a variety of different inputs and outputs, and much more. Finally, you'll take on advanced parallelism with teams and events, interoperability with C, and even publishing your finished app.

Packed with practical examples, tasks to practice, cookbook-style recipes, Modern Fortran is guaranteed to make you see this powerful language in a new light.

What's inside

  • Fortran’s place in the modern world
  • Parallel programming from the ground up
  • Working with variables and types
  • Module development in Fortran
  • Fortran with C, C++, and Python
  • Object-oriented and functional programming styles

About the reader

Written for developers, engineers, and computational scientists. No prior experience with Fortran in needed.

About the author

Milan Curcic is a meteorologist and oceanographer. A Fortran programmer since 2006, he has worked with teams from United States Navy and NASA on developing and improving Earth system prediction models. Milan has authored two general-purpose Fortran libraries and is currently working on a startup porting Fortran to the cloud for weather and ocean prediction.

Manning Early Access Program (MEAP) Read chapters as they are written, get the finished eBook as soon as it’s ready, and receive the pBook long before it's in bookstores.
MEAP combo $59.99 pBook + eBook + liveBook
MEAP eBook $47.99 pdf + ePub + kindle + liveBook
Prices displayed in rupees will be charged in USD when you check out.

placing your order...

Don't refresh or navigate away from the page.

FREE domestic shipping on three or more pBooks