Search code examples
visual-studiofortranintel-fortran

Understanding different types in Fortran


I was reading a Fortran code, came across the following code, couldn't understand what it does.

m%AllOuts( BAzimuth(k) ) = m%BEMT_u(indx)%psi(k)*R2D

I know that % here works like a pipe indicator to access values in a way similar to a dictionary in Python. I have a dictionary m let's say and the first key is AllOuts, but what does anything inside parentheses mean? Is it like another dictionary?


Solution

  • The percent sign is not denoting a dictionary. There are no native dictionaries in Fortran.

    The percent sign denotes the component of a type. For example:

    ! Declare a type
    type :: rectangle
        integer :: x, y
        character(len=8) :: color
    end type rectangle
    
    ! Declare a variable of this type
    type(rectangle) :: my_rect
    
    ! Use the type
    
    my_rect % x = 4
    my_rect % y = 3
    my_rect % color = 'red'
    
    print *, "Area: ", my_rect % x * my_rect % y
    

    The parentheses could either indicate the index of an array, or the arguments of a call.

    So, for example:

    integer, dimension(10) :: a
    
    a(8) = 16     ! write the number 16 to the 8th element of array a
    

    Or, as a prodedure:

    print *, my_pow(2, 3)
    
    ...
    
    contains
    
    function my_pow(a, b)
        integer, intent(in) :: a, b
        my_pow = a ** b
    end function my_pow
    

    In order to figure out what m is, you'd need to look at the declaration of m, which would be something like

    type(sometype) :: m
    

    or

    class(sometype) :: m
    

    Then you'd need to find out the type declaration, which would be something like

    type :: sometype
        ! component declarations in here
    end type
    

    Now one of the components, BEMT_u, is almost certainly an array of a different type, which you'd also need to look up.