fortran,将可分配数组传递给具有右边界的子例程 [英] fortran, passing allocatable arrays to a subroutine with right bounds

查看:30
本文介绍了fortran,将可分配数组传递给具有右边界的子例程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在程序中将一些可分配的数组传递给子例程,我需要知道我这样做的方式是否符合标准.

I need in a program to pass some allocatable arrays to subroutines, and i need to know if the way I do it are in the standard or not.

如果你知道我在哪里可以搜索到fortran的标准,请告诉我.

If you know where I can search for the standard of fortran, Tell me please.

这是一段比文字更能说明问题的小代码

Here is a little code that will better explain than words

program test

use modt99

implicit none

real(pr), dimension(:), allocatable :: vx

allocate(vx(-1:6))
vx=(/666,214,558,332,-521,-999,120,55/)
call test3(vx,vx,vx)
deallocate(vx)

end program test

使用模块 modt99

with the module modt99

module modt99

contains
subroutine test3(v1,v2,v3)
  real(pr), dimension(:), intent(in) :: v1
  real(pr), dimension(0:), intent(in) :: v2
  real(pr), dimension(:), allocatable, intent(in) :: v3

  print*,'================================'
  print*,v1(1:3)
  print*,'================================'
  print*,v2(1:3)
  print*,'================================'
  print*,v3(1:3)
  print*,'================================'

end subroutine test3


end module modt99

在屏幕上,我得到

 ================================
   666.000000000000        214.000000000000        558.000000000000     
 ================================
   214.000000000000        558.000000000000        332.000000000000     
 ================================
   558.000000000000        332.000000000000       -521.000000000000     
 ================================

那么子程序 test3 中的三种伪参数方式是否合法(在什么版本的 fortran、90、95、2003 中?)并且它们的行为是否正常?

So are the three ways of dummy arguments in subroutine test3 legal (in what version of fortran, 90, 95, 2003?) and are their behavior normal?

推荐答案

第一个版本将数组切片传递给子程序.请注意,边界信息不会以这种方式传递,假设数组从 1 开始并转到 size(array).

The first version passes the array slice to the subroutine. Note that boundary information are not passed along in this way, arrays are assumed to start at 1 and go to size(array).

第二种方式和第一种方式一样,但是你手动设置下边界为0,这就是为什么打印v3(1:3)会给你值偏移量为 1.

The second way is just like the first one, but you manually set the lower boundary to 0, that's why printing v3(1:3) gives you the values with an offset of 1.

第三种方式将所有数组信息传递给子程序(包括边界),因此是正确"的索引.Fortran 2003 引入了传递 allocable 数组.

The third way passes all array information to the subroutine (including boundaries), hence the "correct" indexing. Passing allocatable arrays was introduced with Fortran 2003.

除了您有别名问题(将相同的变量传递给三个不同的虚拟参数)这一事实之外,所有三个版本都是合法的.

Apart from the fact that you have an aliasing issue (passing the same variable to three different dummy arguments), all three versions are legal.

您可以在此处找到标准的所有文档.

You can find all documents of the standards here.

特别是,看看 Fortran 2003 标准,Ch.5.1.2.5 DIMENSION 属性,查看虚拟参数中假定形状延迟形状数组之间的差异.

Especially, take a look at the Fortran 2003 Standard, Ch. 5.1.2.5 DIMENSION attribute to see the differences between assumed shape and deferred shape arrays in dummy arguments.

这篇关于fortran,将可分配数组传递给具有右边界的子例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆