如何将可分配数组传递给 Fortran 中的子例程 [英] How to pass allocatable arrays to subroutines in Fortran

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

问题描述

以下代码返回一个分段错误,因为我试图传递的可分配数组没有被正确识别(大小返回 1,而它应该是 3).在这个页面 (http://www.eng-tips.com/viewthread.cfm?qid=170599) 中,一个类似的例子似乎表明它在 F95 中应该可以正常工作;我的代码文件的扩展名为 .F90,但我尝试将其更改为 F95,并且我正在使用 gfortran 进行编译.

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.)

您可以通过在主程序中为您的子程序放置一个接口块来自己提供该显式接口.另一种更好的选择是将子例程放在模块中,然后在主程序中使用该模块 - 然后自动创建显式接口.在 eng-tips 网站上有一个例子,你提供了一个链接 - 请参阅 xwb 的帖子.

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.

请注意,只有当您要执行与其分配状态相关的操作时,虚拟参数才具有 allocatable 属性才有意义 - 查询其状态、重新分配、解除分配等.

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天全站免登陆