4 Organizing your Fortran code using modules

 

This chapter covers

  • Accessing variables and procedures in modules
  • Writing your own custom module
  • Refactoring the tsunami simulator with modules

So far, we’ve covered the essential building blocks of Fortran: built-in types and declaration, arithmetic, control flow, and procedures. In theory, this is all you need to write correct and powerful Fortran programs. In practice, however, as your app or library grows in size, organization of your source code becomes ever more important. This is where modules come to the rescue.

Modules allow you to organize variable and procedure definitions in a meaningful way, and make them accessible for use in programs, procedures, or other modules. Modern Fortran libraries are typically organized in one or more modules. Large applications define most functionality in modules, with only the top-level code being defined in the main program. In this chapter, we’ll write a few modules to define variables and functions. We’ll then access the modules from the main program of our tsunami simulator.

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 The structure of a custom module

4.2.2 Defining a module

4.2.3 Compiling Fortran modules

4.2.4 Controlling 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 Updating 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: Defining the set_gaussian subroutine in a module

sitemap