如何分配数组传递到FORTRAN子程序 [英] How to pass allocatable arrays to subroutines in Fortran

查看:919
本文介绍了如何分配数组传递到FORTRAN子程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code将返回分段错误,因为分配数组我试图通过没有被正确识别(大小,则返回1,当它应该是3)。在这个页面(http://www.eng-tips.com/viewthread.cfm?qid=170599)类似的例子似乎表明,它应F95做工精细;我的code文件具有.F90扩展,但我试图将其更改为F95,而我使用gfortran编译。

我的猜测是,这个问题应该是我传递分配数组的子程序的方式;我在做什么错了?

 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%!
 程序测试
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%!
 隐式NONE
 DOUBLE preCISION,可分配::阵列(:, :)
 INTEGER ::三,JJJ ALLOCATE(阵列(3,3))
 DO III = 1,3
 DO JJJ = 1,3
    阵列(III,JJJ)= III + JJJ
    PRINT *,阵列(三,JJJ)
 ENDDO
 ENDDO
 CALL子测试(阵列) END PROGRAM
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%!
 SUBROUTINE子测试(阵列)
 DOUBLE preCISION,可分配,意向(IN)::阵列(:, :)
 INTEGER ::三,JJJ PRINT *,SIZE(ARRAY,1),SIZE(数组,2)
 DO III = 1,SIZE(ARRAY,1)
 DO JJJ = 1,SIZE(数组,2)
    PRINT *,阵列(三,JJJ)
 ENDDO
 ENDDO END SUBROUTINE
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%!


解决方案

如果一个程序有一个伪参数是分配的,则显式接口中的任何范围内调用所需。

(有需要显式接口许多东西,可分配的假,只有一个。)

您可以通过将接口块为您的子程序主程序里面自己提供了显式接口。另一种与远很远,远更好的选择是把一个子程序模块内,然后用在主程序中该模块 - 将自动创建的显式接口。还有就是这对您提供的链接CHI-提示网站的例子 - 看到XWB后

请注意,它才有意义,对伪参数有可分配的属性,如果你打算做有关其分配状态的东西 - 查询其状态,重新分配它,释放它,等

The following code is returning a Segmentation Fault because the allocatable array I am trying to pass is not being properly recognized (size returns 1, when it should be 3). In this page (http://www.eng-tips.com/viewthread.cfm?qid=170599) a similar example seems to indicate that it should work fine in F95; my code file has a .F90 extension, but I tried changing it to F95, and I am using gfortran to compile.

My guess is that the problem should be in the way I am passing the allocatable array to the subroutine; What am I doing wrong?

!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%!
 PROGRAM test
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%!
 IMPLICIT NONE
 DOUBLE PRECISION,ALLOCATABLE :: Array(:,:)
 INTEGER                      :: iii,jjj

 ALLOCATE(Array(3,3))
 DO iii=1,3
 DO jjj=1,3
    Array(iii,jjj)=iii+jjj
    PRINT*,Array(iii,jjj)
 ENDDO
 ENDDO
 CALL Subtest(Array)

 END PROGRAM
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%!
 SUBROUTINE Subtest(Array)
 DOUBLE PRECISION,ALLOCATABLE,INTENT(IN) :: Array(:,:)
 INTEGER                                 :: iii,jjj

 PRINT*,SIZE(Array,1),SIZE(Array,2)
 DO iii=1,SIZE(Array,1)
 DO jjj=1,SIZE(Array,2)
    PRINT*,Array(iii,jjj)
 ENDDO
 ENDDO

 END SUBROUTINE
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%!

解决方案

If a procedure has a dummy argument that is an allocatable, then an explicit interface is required in any calling scope.

(There are numerous things that require an explicit interface, an allocatable dummy is but one.)

You can provide that explicit interface yourself by putting an interface block for your subroutine inside the main program. An alternative and far, far, far better option is to put the subroutine inside a module and then USE that module in the main program - the explicit interface is then automatically created. There is an example of this on the eng-tips site that you provided a link to - see the post by xwb.

Note that it only makes sense for a dummy argument to have the allocatable attribute if you are going to do something related to its allocation status - query its status, reallocate it, deallocate it, etc.

这篇关于如何分配数组传递到FORTRAN子程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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