Fortran

Guide To Learn

Advanced Fortran use

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

Binding procedures to a derived type

Besides storing arbitrary data in type components, we can also bind functions and subroutines to the type, making them type-bound methods. Similar to the custom constructor procedures, there are two steps to defining a type-bound method. The first is to define the function or subroutine itself, and the second is to specify the binding in the type definition block.

Custom type constructor for the Field type

Recall that in the previous version of the tsunami simulator, we did quite a lot of prep work before allocating the data arrays. Now that we know how to write a custom constructor, let’s apply this technique to initializing the Field type. For the time being, let’s focus on just finding the start and end indices, allocating […]

Writing a custom type constructor

Fortran provides a simple mechanism to override the default type constructor with a user-defined function or subroutine. This gives you the power to do any prep work with type components such as allocation, initialization, input validation, and others. Let’s first define the function that returns an instance of the type whose constructor we’re overriding. For […]

Providing default values for derived type components

Fortran allows you to set a default (initial) value of any component in the derived type definition itself. The following listing provides an example. ❶ Sets the default value With the Person type defined like this, only the name and age components are required to be passed to the constructor. If the occupation argument is given, it will override the default value: […]

Scroll to top