Fortran

Guide To Learn

2. Organizing your Fortran code using modules

Putting it all together in the tsunami simulator

You now know how to import variables and procedures from modules, and also how to write your own modules. Finally, we get to put this all together to refactor, improve, and expand our wave simulator. If you’ve followed the lessons in this section, and if you’ve worked through both exercises in this chapter, you now […]

Controlling access to variables and procedures

Modules allow you to specify the visibility of variables and procedures defined in them. All entities in a module can be either public or private: For example, let’s take a look at a module that defines a function to calculate the area of a circle, given an input radius. The following listing shows how that’s […]

 Compiling Fortran modules

The tsunami simulator source code is now made of two source files: the main program (tsunami.f 90), which defines the top-level simulation loop, and the finite difference module (mod_diff.f 90). In all our work so far, we’ve always compiled our Fortran programs from single-source files. How do you compile a program that’s defined across multiple […]

The structure of a custom module

Before we begin, let’s see what a custom module may look like on the inside, and how its building blocks work (figure 4.3). Figure 4.3 Structure of a custom Fortran module Every Fortran module is defined with a pair of module/end module statements. Modules can’t have any executable code on their own. Instead, they’re used to declare data […]

Creating your first module

You’re now ready to tackle the next big item–writing your own custom module. Let’s do so by implementing it directly in the tsunami simulator. Recall that in the previous chapter we created two procedures: We defined both of these procedures in the main program. This worked well because our simulator was still rather simple. However, […]

Using portable data types

In chapter 2, I mentioned that variables of built-in types can be explicitly and portably declared using specific type kinds. Type kind parameters determine the space that numeric variables occupy in memory, which in turn limits the range for integers, and the range and precision for real and complex numbers. Most Fortran compilers by default […]

Getting compiler version and options

Once you have a compiled program executable, it’s not obvious how it was compiled. Specifically, what compiler was used, and were any compiler options used–for example, for debugging or optimization? Fortran’s iso_fortran_env module provides two functions that allow you to get this information at runtime: compiler_version and compiler_options. You get the idea what each of these functions does. Let’s write a program that […]

Accessing a module

Before we venture into writing our own custom modules, let’s first get familiar by accessing a built-in module that comes with Fortran out of the box. Fortran has a few built-in modules. The most commonly used one is iso_fortran_env, which, among other things, provides constants and procedures that allow you to write more portable programs. Another commonly […]

Scroll to top