Fortran 子例程的输入参数可以在子例程主体中释放和分配吗? [英] Can a input argument of a Fortran subroutine be deallocated and allocated in the body of the subroutine?

查看:43
本文介绍了Fortran 子例程的输入参数可以在子例程主体中释放和分配吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我修改后的代码如下所示:

UPDATE: My modified code looks like this:

program run_module_test
    use module_test
    implicit none   

    TYPE(newXYZ), allocatable, intent(inout) :: xyzArray(:)

    call update(xyzArray)
    write(6,*)'xyzArray',xyzArray

end program run_module_test

module module_test
    implicit none

    TYPE :: newXYZ
        real(4) :: x, u
        real(4) :: y, v
        real(4) :: z, w
        real(4),dimension(3) :: uvw     
    END TYPE

    integer(4) :: shape = 3

contains

    subroutine update(xyzArray)

    integer(4) :: i
    TYPE(newXYZ), allocatable, intent(inout) :: xyzArray(:)
    allocate( xyzArray(shape) ) 

    do i = 1, shape
        xyzArray(i)%x = 0
        xyzArray(i)%y = 0
        xyzArray(i)%z = 0
        xyzArray(i)%u = 0
        xyzArray(i)%v = 0
        xyzArray(i)%w = 0
        xyzArray(i)%uvw = (/0,0,0/)
    end do
    return
    end subroutine update

end module module_test

当它们被编译时,它们会产生一个类似的错误:

When they are compiled, they generate a similar error:

 TYPE(newXYZ), allocatable, intent(inout) :: xyzArray(:)
                                                    1
Error: ALLOCATABLE attribute conflicts with DUMMY attribute at (1)

当我消除 update() 子程序中的参数时,我收到一个矛盾的错误:

When I eliminate the argument in update() subroutine, I receive a contradictory error:

TYPE(newXYZ), allocatable, intent(inout) :: xyzArray(:)
                                                       1
Error: Symbol at (1) is not a DUMMY variable

我是否消除了有用建议中指出的错误来源?这可能是与编译器相关的错误(使用 mpi90)吗?

Have I eliminated the sources of error pointed out in the helpful suggestions? Could this be a compiler related error (using mpi90)?

~~~第一次编辑~~~我有一个子程序,它的输入参数是一个用户定义类型 XYZ 的数组.我希望解除分配 xyzArray 并将其分配/修改为子例程主体中的不同大小.我尝试了 在 fortran 中更改数组维度 建议的方法,但是当我执行以下操作时:

~~~First Edit~~~ I have a subroutine whose input argument is an array of user defined type XYZ. I wish to deallocate xyzArray and allocate/modify it to a different size in the body of the subroutine. I attempted the method suggested by changing array dimensions in fortran, but when I do the following:

subroutine update(xyzArray, ...)
...
TYPE (XYZ), allocatable :: xyzArray(:)

我收到一条错误消息:

Error: ALLOCATABLE attribute conflicts with DUMMY attribute at (1)

当我尝试时:

subroutine update(xyzArray, ...)
...
deallocate( xyzArray(myshape) )
allocate( xyzArray(newshape) )

我收到错误消息:

Error: Expression in DEALLOCATE statement at (1) must be ALLOCATABLE or a POINTER
Error: Expression in ALLOCATE statement at (1) must be ALLOCATABLE or a POINTER

我需要做什么来改变子程序中数组的大小?

What do I need to do to change the size of the array in the subroutine?

推荐答案

这样做:

  • 虚拟参数必须是可分配的.可分配的虚拟参数需要一个实现 Fortran 2003 标准相关部分的编译器(或一个实现所谓的可分配的"TR 的 Fortran 95 编译器).

  • The dummy argument must be allocatable. Allocatable dummy arguments require a compiler that implements the relevant part of the Fortran 2003 standard (or a Fortran 95 compiler that implements the so called "allocatable" TR).

需要过程的显式接口(过程必须是模块过程、内部过程或在调用范围内具有接口块).

An explicit interface to the procedure is required (the procedure must be a module procedure, an internal procedure or have an interface block in the calling scope).

虚拟参数不能是intent(in).如果您在子例程中根本没有使用虚拟参数值的分配状态或其他方面,那么 intent(out) 可能是合适的(如果预先分配虚拟参数将在调用过程时自动释放),否则意图(输入输出)或无意图.

The dummy argument must not be intent(in). If you are not using the allocation status or other aspects of the value of the dummy argument at all in the subroutine then intent(out) may be appropriate (if allocated beforehand the dummy argument will be automatically deallocated when the procedure is called), otherwise intent(inout) or no intent.

(您的第二个示例代码块在 deallocate 语句中存在语法错误 - 您应该简单地指定 xyzArray 变量,去掉 (myshape) 形状规范))

(Your second block of example code has a syntax error with the deallocate statement - you should simply specify the xyzArray variable, leave off the (myshape) shape specification))

例如,在一个模块中:

subroutine update(xyzArray)
  type(xyz), allocatable, intent(inout) :: xyzArray(:)
  ...
  if (allocated(xyzArray)) deallocate(xyzArray)
  allocate(xyzArray(newshape))
  ...

这篇关于Fortran 子例程的输入参数可以在子例程主体中释放和分配吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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