我正在尝试阅读一些Fortran代码,但无法确定%
(百分号)是做什么的。
它是这样排成一行的:
x = a%rho * g * (-g*a%sigma + m%gb * m%ca * (1.6 * a%rho+g))
是干什么的呢?
发布于 2011-11-12 05:26:29
在Fortran90中,它们允许您创建类似于C++的结构。它基本上充当点(.)操作符。
来自http://www.lahey.com/lookat90.htm:
结构(派生类型)
您可以使用派生类型对数据进行分组。这使用户能够将内部类型(包括数组和指针)组合到新类型中,新类型的各个组件可以使用百分号作为分隔符进行访问。(派生类型在VAX Fortran中称为记录。)!使用派生类型和模块的示例。
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
发布于 2011-11-12 05:26:21
它是派生类型的部件标识符。看看这个。http://www.lahey.com/lookat90.htm
发布于 2017-12-30 18:53:03
%
作为令牌有许多密切相关的用法。随着Fortran的发展,这些用法在数量上有所增加。
回到Fortran90,和问题中看到的用法一样,%
用于访问派生类型的组件。考虑具有该类型的对象a
的派生类型a_t
:
type a_t
real rho, sigma
end type
type(a_t) a
可以通过a%rho
和a%sigma
访问a
的组件rho
和sigma
。从问题中可以看出,这些组件可以用在表达式中(如a%rho * g
),也可以用在赋值的左边(a%rho=1.
)。
派生类型的组件本身可以是派生类型的对象:
type b_t
type(a_t) a
end type
type(b_t) b
因此,在单个引用中可能存在%
的多个外观:
b%a%rho = ...
在这里,派生类型object a
的组件rho
本身就是b
的组件,它是赋值的目标。可以在一个参照中看到相当可怕的%
计数,但零件参照总是从左到右解析的。
谈到Fortran 2003,人们会看到%
以其他几种方式与派生类型相关:
引用参数化类型参数的object;
考虑派生类型
type a_t(n)
integer, len :: n=1
real x(n)
contains
procedure f
end type
type(a_t(2)) a
对象a
有一个长度类型参数和一个类型绑定过程。在类似这样的表达式中
x = a%f()
引用派生类型对象的绑定f
。
a
的参数n
可以引用为
print *, a%n, SIZE(a%x)
就像可以引用组件x
一样。
最后,从Fortran 2008开始,%
可以用来访问复杂对象的实部和虚部:
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
复制相似问题