Fortran中的“%”是什么意思/做什么?

问题描述 投票:16回答:3

我试图阅读一些Fortran代码,但无法确定%(百分号)的作用。

这是一个像:

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

它有什么作用?

fortran fortran90
3个回答
28
投票

在Fortran 90中,它们允许您创建类似于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

3
投票

它是派生类型的部件标识符。看一下这个。 http://www.lahey.com/lookat90.htm


2
投票

%作为代币具有许多密切相关的用途。随着Fortran的发展,这些用途的数量也在增加。

回到Fortran 90,以及在问题中看到的用法,%用于访问派生类型的组件。考虑使用该类型的对象a_t的派生类型a

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

可以使用rhosigma访问aa%rhoa%sigma组件。从问题中可以看出,这些组件可以用在表达式中(例如a%rho * g),或者它们可能是赋值的左侧(a%rho=1.)。

派生类型的组件本身可以是派生类型的对象:

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

所以在一个参考文献中可能会有多次出现%

b%a%rho = ...

这里,派生类型对象rho的组件a,它本身是b的一个组成部分,是赋值的目标。人们可以在一个参考文献中看到相当可怕的%s计数,但部分参考文献总是从左到右解决。

来到Fortran 2003,然后用其他几种方式看到与派生类型相关的%

  • 引用对象的绑定;
  • 查询参数化类型的参数。

考虑派生类型

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

n的参数a可以引用为

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
© www.soinside.com 2019 - 2024. All rights reserved.