Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "%" mean / do in Fortran?

Tags:

I am trying to read some Fortran code, but can not determine what the % (percentage sign) does.

It is in a line like:

   x = a%rho * g * (-g*a%sigma + m%gb * m%ca * (1.6 * a%rho+g))

What does it do?

like image 236
Abe Avatar asked Nov 11 '11 21:11

Abe


People also ask

What does d0 mean in Fortran?

Note that a processor may assign an in itial value of zero (0.) to all double precision variables. In FORTRAN, the double-precision zero is written as 0.0D0, in which D stands for "times ten to the power...", i.e., 0.0 X 100.

What does == mean in Fortran?

== . eq. Checks if the values of two operands are equal or not, if yes then condition becomes true. (A == B) is not true.

What does kind mean in Fortran?

The KIND of a variable is an integer label which tells the compiler which of its supported kinds it should use. Beware that although it is common for the KIND parameter to be the same as the number of bytes stored in a variable of that KIND, it is not required by the Fortran standard.


3 Answers

In Fortran 90 they allow you to create structures like in C++. It basically acts as the dot (.) operator.

From http://www.lahey.com/lookat90.htm :

Structures (Derived Types)

You can group your data using derived types. This enables users to combine intrinsic types (including arrays and pointers) into new types whose individual components can be accessed using the percent sign as a delimiter. (Derived types are known as records in VAX Fortran.) ! Example using derived types and modules.

module pipedef
   type pipe                          ! Define new type 'pipe', which
     real diameter                    ! is made up of two reals, an
     real flowrate                    ! integer, and a character.
     integer length
     character(len=10) :: flowtype
   end type pipe
end module pipedef

program main
   use pipedef                ! Associate module pipedef with main.
   type(pipe) water1, gas1    ! Declare two variables of type 'pipe'.
   water1 = pipe(4.5,44.8,1200,"turbulent") ! Assign value to water1.
   gas1%diameter = 14.9                     ! Assign value to parts
   gas1%flowrate = 91.284                   ! of gas1.
   gas1%length = 2550
   gas1%flowtype = 'laminar'
   .
   .
   .
end program
like image 53
NickLH Avatar answered Nov 19 '22 17:11

NickLH


% as a token has a number of closely related uses. As Fortran has developed those uses have increased in count.

Going back to Fortran 90, and the use seen in the question, % is used to access the components of a derived type. Consider the derived type a_t with object a of that type:

type a_t
  real rho, sigma
end type
type(a_t) a

The components rho and sigma of a may be accessed with a%rho and a%sigma. As can be seen in the question, these components may be used in expressions (such as a%rho * g) or they may be the left-hand side of an assignment (a%rho=1.).

A component of a derived type may itself be an object of derived type:

type b_t
  type(a_t) a
end type
type(b_t) b

and so there may be multiple appearances of % in a single reference:

b%a%rho = ...

Here, the component rho of the derived type object a, which is itself a component of b, is the target of assignment. One can see a quite horrifying count of %s in one reference, but the part references are always resolved from left to right.

Coming to Fortran 2003, one then sees % relating to derived types in a couple of other ways:

  • referencing a binding of an object;
  • inquiring of a parameterized type's parameters.

Consider the derived type

type a_t(n)
  integer, len :: n=1
  real x(n)
 contains
  procedure f
end type
type(a_t(2)) a

The object a has a single length-type parameter and a type-bound procedure. In an expression like

x = a%f()

the binding f of the derived type object is referenced.

The parameter n of a may be referenced as

print *, a%n, SIZE(a%x)

much as the component x may be referenced.

Finally, from Fortran 2008, % may be used to access the real and imaginary parts of a complex object:

complex x, y(3)
x%im = 1.
x%re = 0.
y = (2., 1.)
print *, y(2)%im+y(3)%re
like image 28
francescalus Avatar answered Nov 19 '22 15:11

francescalus


It's a part identifier for a derived type. Check this out. http://www.lahey.com/lookat90.htm

like image 36
Carth Avatar answered Nov 19 '22 17:11

Carth