Fortran

Guide To Learn

Exercise 1: Using portable type kinds in the tsunami simulator

The task is to import the portable type kind parameters from the iso_fortran_env module and use them in the tsunami simulator. To do this, we’ll rewrite only the declaration section, as shown in the following listing.

Listing 4.11 Using portable type kinds in the tsunami simulator

program tsunami                                       ❶
 
  use iso_fortran_env, only: int32, real32            ❷
  implicit none                                       ❸
 
  integer(int32) :: n                                 ❹
 
  integer(int32), parameter :: grid_size = 100        ❺
  integer(int32), parameter :: num_time_steps = 100   ❺
  real(real32), parameter :: dt = 1, dx = 1, c = 1    ❻
 
  real(real32) :: u(grid_size)                        ❼
 
  ...
 
end program tsunami                                   ❽

❶ Assigns a name to the program

❷ Accesses only these two constants from the built-in module

❸ Enforces explicit declaration

❹ Declares integer variables for loop counters

❺ Declares integer constants for grid size and number of time steps

❻ Declares real constants for time step, grid spacing, and phase speed

❼ Declares an array

❽ Marks the end of the program

We first import the standard type kinds int32 and real32 from the iso_fortran_env module. We then specify these type kinds in each integer and real declaration statement, respectively. That’s it, we’re done! As long as we declare all our variables like this, we ensure that they’re always of the same size in memory across different machines and compilers. This is one of many important steps toward reproducibility of results. As always, with any change or addition to the code, make sure it works as expected by recompiling and rerunning it.

Exercise 1: Using portable type kinds in the tsunami simulator

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top