【问题标题】:passing unknown sized array through subroutines in fortran (array allocated in subroutine)通过 fortran 中的子例程传递未知大小的数组(在子例程中分配的数组)
【发布时间】:2012-12-09 07:12:54
【问题描述】:

我有一个程序,它在第一个子程序 (CALCONE) 中创建一个数组,然后我想将它传递给下一个子程序 (CALCTWO),然后将其输出到主程序中的文件。我不知道将 INTENT(IN)、INTENT(OUT) 语句放在哪里,因为需要在子例程 CALCONE 中分配数组(因为在我的程序中,长度是在 CALCONE 中确定的) - 我已经尝试避免困惑。有人可以帮助将它们放在正确的位置。一旦我有了一个模板,我就会明白它在未来是如何工作的。谢谢。

我已经简化了程序,变量代表了其他不能移动的东西。我只需要一种方法在子程序之间移动一个数组,然后在主程序的末尾写出来。

 program TEST
 implicit none

 integer a,b,c,d,reclen

 a = 1
 b = 2
 c = 3
 d = 4

 call CALCONE(a,b,c,d,array)

 call CALCTWO(e,f,g,h,array)

 inquire(iolength=reclen)array 
 open(unit=8,file='array_output.dat', &
   form="unformatted",access="stream")
 write(unit=8)array       
 close(unit=8)

 END PROGRAM TEST

 SUBROUTINE CALCONE(adummy,bdummy,cdummy,ddummy,arraydummy)
 IMPLICIT NONE

 integer i,j,k
 integer e,f,g,h,N
 !ALLOCATE ARRAY HERE OR IN MAIN PROGRAM?
 real*8,   allocatable  :: arraydummy(:,:)

 e = a + 1
 f = b + 1
 g = c + 1
 h = d + 1

 ! N can only be is calculated here

 allocate(arraydummy(1:N,1:3))

 !POPULATE 'arraydummy'
 !PASS ARRAY BACK OUT TO NEXT SUBROUTINE FOR FURTHER PROCESSING IN CALCTWO
 END SUBROUTINE CALCTWO

 SUBROUTINE CALCTWO(edummy,fdummy,gdummy,hdummy,arraydummy)
 IMPLICIT NONE

 integer e,f,g,h
 !DEFINE HERE ALSO? i.e.
 !real*8,   allocatable  :: arraydummy(:,:)

 e = a + 1
 f = b + 1
 g = c + 1
 h = d + 1

 arraydummy = arraydummy*e*f*g*h

 END SUBROUTINE CALCTWO

【问题讨论】:

    标签: arrays fortran allocation subroutine


    【解决方案1】:

    您不必在主程序中分配数组,但您必须在主程序中声明它并且您必须将它作为参数传递(您这样做)。注意,符号array 没有在主程序中定义。

    请务必为 subs 提供显式接口。因为您使用高级功能(可分配的虚拟参数),所以这是必要的。最好是将它们放在一个模块中。

    数组必须在两者中定义为虚拟参数。我在第一个中将其声明为 intent(out),因为它是在开头分配的。这不是绝对必要的。

    另一种选择是在模块中声明数组并让模块过程共享它。

    免责声明:我没有尝试编译它。

     module subs
       integer,parameter :: rp = kind(1d0)
    
    
       contains
    
    
    
    
       SUBROUTINE CALCONE(adummy,bdummy,cdummy,ddummy,arraydummy)
       IMPLICIT NONE
    
       integer i,j,k
       integer e,f,g,h,N
       !ALLOCATE ARRAY HERE OR IN MAIN PROGRAM?
       real(rp),   allocatable, intent(out) :: arraydummy(:,:)
    
       e = a + 1
       f = b + 1
       g = c + 1
       h = d + 1
    
       !N can only be is calculated here
    
        allocate(arraydummy(1:N,1:3))
    
       !POPULATE 'arraydummy'
       !PASS ARRAY BACK OUT TO NEXT SUBROUTINE FOR FURTHER PROCESSING IN CALCTWO
       END SUBROUTINE CALCTWO
    
       SUBROUTINE CALCTWO(edummy,fdummy,gdummy,hdummy,arraydummy)
       IMPLICIT NONE
    
       integer e,f,g,h
       !DEFINE HERE ALSO? i.e.
       real(rp),   allocatable, intent(inout)  :: arraydummy(:,:)
    
       e = a + 1
       f = b + 1
       g = c + 1
       h = d + 1
    
       arraydummy = arraydummy*e*f*g*h
    
       END SUBROUTINE CALCTWO
    
    
     end module subs
    
     program TEST
    
     use subs
    
     implicit none
    
     integer a,b,c,d,reclen
    
     real(rp),allocatable :: array
    
     a = 1
     b = 2
     c = 3
     d = 4
    
     call CALCONE(a,b,c,d,array)
    
     call CALCTWO(e,f,g,h,array)
    
     inquire(iolength=reclen)array 
     open(unit=8,file='array_output.dat', &
       form="unformatted",access="stream")
     write(unit=8)array       
     close(unit=8)
    
     END PROGRAM TEST
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 2013-11-01
      • 2023-02-06
      • 1970-01-01
      相关资源
      最近更新 更多