Fortran

Guide To Learn

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:

  • A function, diff, that computes the finite difference of an input array. The result of this function tells us at what rate the values in an array change from one element to the next.
  • A subroutine, set_gaussian, that initializes an input array to a Gaussian shape.

We defined both of these procedures in the main program. This worked well because our simulator was still rather simple. However, now we have the perfect opportunity to define some modules and define these procedures there. We’ll do that in this section by defining the diff finite difference function in its own module and source file.

How will this shift concretely affect our tsunami simulator? It’ll allow us to break up and organize the code in a few small building blocks that we’ll then reuse in different places. Figure 4.2, which you first saw in chapter 3, illustrates the new code organization.

Figure 4.2 Using a module and a function to reuse and simplify code

We’ll access the finite difference function, diff, in the main program by importing it from the mod_diff module with the use statement. This reorganization of the code won’t semantically change the program. And if we implement our new module correctly, the output will be exactly the same as before. However, splitting the code into the main program and modules will allow us to more easily expand the simulator and make it more realistic.

Creating your first module

Leave a Reply

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

Scroll to top