Fortran

Guide To Learn

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

Accessing derived type components

Once we have a class instance declared and initialized, in most cases we’ll want to access its components, often to read their values, sometimes to modify them. After all, if we use type components to store data that’s specific to the instance, there’s no use for it unless we can access it in some way. […]

Instantiating a derived type

Defining a derived type was the first necessary step, but to actually use it, we need to instantiate one. Instantiating means nothing more than creating a new instance. It’s kind of an unwieldy word, but the more you use it, the more natural it becomes. In listing 8.2, I used a shortcut to declare and initialize a […]

Defining a derived type

Now that you see where we’re going, let’s take a few steps back and start simple and slow. The following snippet demonstrates the syntax to define a new derived type: ❶ Specifies derived type name ❷ Declares a component variable ❸ We can have as many of these as we want. ❹ Closes the type definition block The type […]

Defining, declaring, and initializing derived types

Perhaps the greatest strength of an object-oriented approach to development is that it offers a mechanism that captures a model of the real world.   –Grady Booch (1986) Let’s start with a small program that makes use of a derived type. We’ll define a Person type and assign to it a greeting subroutine, which will do nothing more […]

Recasting the tsunami simulator with derived types

The main goal of this chapter is to apply derived types toward refactoring our parallel tsunami simulator. Specifically, we’ll use them to abstract away the low-level code to allocate and manipulate data arrays, as well as to black-box much of the tedious code used for setting up the data structures. Derived types will help us […]

Scroll to top