fortran,将可分配数组传递给有正确边界的子例程 [英] fortran, passing allocatable arrays to a subroutine with right bounds
问题描述
我需要在一个程序中传递一些可分配的数组到子例程中,并且我需要知道我是这样做的方式是否在标准中。
如果你知道我可以在哪里搜索fortran的标准,请告诉我。
以下是一些可以更好解释的代码
程序测试
使用modt99
隐式无
真实(pr) ,dimension(:),allocatable :: vx
allocate(vx(-1:6))
vx =(/ 666,214,558,332,-521,-999,120,55 /)
调用test3(vx,vx,vx)
解除分配(vx)
结束程序测试
与模块modt99
模块modt99
包含
子程序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子程序test3
结束模块modt99
屏幕上,I得到
========================= =======
666.000000000000 214.000000000000 558.000000000000
================================
214.000000000000 558.000000000000 332.000000000000
================================
558.000000000000 332.000000000000 - 521.000000000000
================================
那么子程序test3中的虚拟参数的三种方法是否合法(在fortran,90,95,2003的哪个版本中?),并且它们的行为是否正常? p>
第一个版本将数组切片传递给子版本utine。请注意,边界信息不以这种方式传递,数组假定从 1
开始并转到 size(array)
。 第二种方法与第一种方法类似,但是您手动将下限设置为 0
,这就是为什么打印 v3(1:3)
会为您提供偏移量 1
的值。第三种方法将所有的数组信息传递给子程序(包括边界),因此是正确的索引。在Fortran 2003中引入了传递 allocatable
数组。
除了你有一个别名问题相同的变量到三个不同的虚拟参数),所有三个版本都是合法的。
您可以在这里找到。
特别是,看看Fortran 2003 Standard, Ch。 5.1.2.5 DIMENSION属性
来查看伪参数中假定形状和延迟形状数组之间的差异。
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.
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
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
on screen, I get
================================
666.000000000000 214.000000000000 558.000000000000
================================
214.000000000000 558.000000000000 332.000000000000
================================
558.000000000000 332.000000000000 -521.000000000000
================================
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?
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)
.
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
.
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.
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屋!