Welcome to AE Resources
Converted document Converted document

FORTRAN 90+: DERIVED TYPES

Derived types are a way of declaring variables. They allow the user to give multiple attributes to a single variable. For example, a single variable may be a three-dimensional coordinate. Derived types allow the programmer to hold all three values for the one point in a single variable location. The format for setting up a derived type is
TYPE derived_name !In this instant the TYPE is a function name
          type :: variable_names !’type’ here indicates one
                  !of the main types ’CHARACTER’ ’REAL’ etc
     END TYPE name
To help clarify this process, consider this example. The example assign height, width and depth to a variable representing a cube.
TYPE cube
     REAL :: height, width, depth
END TYPE cube
The assignment of cube would then be used to declare the type of a variable
TYPE (cube) :: box1
The ability to do this for variables can save a lot of confusion when assigning similar characteristics to multiple variables, in this case if you need to describe many boxes.
SUPERTYPES
Supertypes are when one derived type is used to describe a second derived type. This occurs when a certain set of characteristics are already defined but more are needed for a specific use. The format would be
TYPE block_tower
          TYPE(cube) :: block_size
          INTEGER    :: number_of_blocks
     END TYPE block_tower
The difference between supertypes and regular derived types is in how the values are assigned.
VALUE ASSIGNMENT
Values can be assigned to derived types in one of two ways. The first is to assign each component one at a time. The other way is to assign them all at once as objects. The general format is
type_name%component_name = value
This is slightly changed for supertypes. The only difference is that more percent [%] signs are needed. If there is only one level of recursion in the supertype assigned to the characteristics supplied by the intern derived type, then it will look like
type_name_outer%type_name_inner%component_name =value
It is important to keep track of the name used to describe each component when the derived type was defined as these are the names used to assign values. Examples include
cube%width= 2.9
     block_tower%number_of_blocks=6
     block_tower%block_size%hieght= 5.3 !Note how to reference
          !cube variables using its name in the block_tower
          !Derived type
ARRAYS AND DEFINED TYPES
Derived types can be arrays. Although they cannot be allocatable arrays (of variable size), they can have the pointer attribute. The format is the same except that the size of the array must be declared in the statement:
TYPE name
     type, DIMENSION(#,#) :: variable_names
END TYPE name
To assign values use the same method as before to call up a specific position.
name%variable_name(pos#,pos#)=val
Using this method the programmer can also make every position in an array another array. An example of (a) defining an array derived type and then (b) using it to create an embedded array is
TYPE imbed
     INTEGER, DIMENSION(10,10) :: name1
END TYPE imbed
TYPE (imbed), DIMENSION(5,5) :: name2
TYPE (imbed)                 :: name3
Assigning values to each of these variables:
name2(3,2)%name1(:,:)= 4
name2(3,2)%name1(:,::4)=9
name2(:,::2)%name1(2,4)=1
name3%name1(:,:)=99
An important to note to make is that when an embedded array is used, the programmer can only section one of the arrays. This means that in the first of the above list only name1 and a specific location of name2 could be sectioned. The following is not allowed because it sections both arrays when assigning a value.
name2(2,:)%name1(::3,:)=56