Fortran

Guide To Learn

 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 […]

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

The solution is to add the pure elemental attributes to our previous version of the cold front program, as shown in the following listing. Listing 3.22 Cold front temperature function that works with scalars and arrays ❶ The elemental attribute makes the function compatible with both scalars and arrays. You can now invoke this function with one or more […]

Scroll to top