Fortran

Guide To Learn

2. Working with abstract data using derived types

Derived type implementation of the tsunami solver

Finally, we’re getting close to the home stretch. The following listing provides the (almost) complete code of the derived type implementation of the tsunami simulator. I’ve omitted the declaration section for brevity. Listing 8.13 Derived type implementation of the tsunami solver ❶ Initializes Fields ❷ Sets initial water height perturbation and sync ❸ Sets a constant mean water […]

Passing a class instance to diffx and diffy functions

To use finite difference functions directly with our new Field instances, we’ll make simple wrappers around those functions. First, we’ll import the functions and make them available in the mod_field module: ❶ Renames on import so we can use the original names We want to keep the original names diffx and diffy, so I’ve renamed them on import to avoid a name conflict. In […]

Finite differences in x and y

In addition to an extra equation and a finite difference term for each of the x and y axes, we need specific implementations of diff for each of them. diffx will return a difference along array rows, and diffy along array columns. The function in the following listing served us well in the one-dimensional solver. Listing 8.9 One-dimensional version of the finite difference function ❶ Input array […]

Going from 1-D to 2-D arrays

Extending the solver from one to two dimensions carries two major implications: In other words, whereas so far we’ve been solving for a single velocity u(:) and water height h(:), now we’ll be solving for velocities in x and y axes (u(:,:) and v(:,:), respectively), and for water height h(:,:). This is illustrated in figure 8.6. Figure 8.6 Comparison of data arrays for water height […]

Extending tsunami to two dimensions

In sections 8.2 and 8.3, we learned the basic syntax of defining a derived type and its components, and binding a method to it. We also began to apply these techniques toward building the Field derived type, which we’ll use to model the physical quantities that the tsunami simulator predicts, namely water height and velocity. What we haven’t […]

Bringing it all together

So far in this chapter, we’ve covered the essentials of Fortran derived types: Fortran offers much more in this realm, such as extending derived types (known as inheritance in object-oriented programming), and abstract types whose methods can do different things depending on the concrete type of the instance. See the “Further reading” section at the end of this […]

Controlling access to type components and methods

So far, we’ve been able to access any type components or methods from the main program without issues. This is also the default behavior: all type components and methods are visible (public), unless otherwise specified. Recall from section 4.2.4 that this is the same behavior as with module variables and procedures, where we used public and private attributes to explicitly […]

Type-bound methods for the Field type

It’s now a good time to start planning for the type-bound methods for our Field type, as shown in the following listing. Listing 8.7 Type-bound components to be defined for the Field type ❶ Gathers data on one image ❷ Initializes a bell-shaped blob ❸ Synchronizes data between tiles ❹ Writes data to file Each of the methods in listing 8.7 has a […]

Your first type-bound method

Looking back at our derived-type hello world program from listing 8.2, the subroutine we’ll bind to the type is the greeting subroutine, as shown here: ❶ The input argument will be the type instance itself. ❷ We can access type components from here. Here we have another new syntax element–class. Declaring class(Person) instead of type(Person) allows any type that’s extended (derived) […]

Scroll to top