问题
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?
回答1:
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
回答2:
It's a part identifier for a derived type. Check this out. http://www.lahey.com/lookat90.htm
回答3:
%
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
来源:https://stackoverflow.com/questions/8100131/what-does-mean-do-in-fortran