首页 > 解决方案 > 将可分配数组传递给具有固定维度虚拟变量的子程序

问题描述

我正在编写一些代码,其中我需要使用我的指南给我的一些模块。现在,在这些模块中,有一些子程序采用具有固定维度的虚拟数组。但是,我需要将一些可分配的数组传递给这些函数。

我想知道的是是否有一种方法可以将可分配数组作为参数传递给子例程或虚拟参数具有固定维度的函数。

这是要使用的实际子程序:

function crosscorr(x, y, n, k)
! Cross-correlation of array x and y for a given time shift k.
implicit none
integer :: n, k
real(8), dimension(0:n-1) :: x, y
real(8) crosscorr
if (k.ge.0) then
    crosscorr = dot_product(x(0:n-1-k), y(k:n-1))
else
    crosscorr = dot_product(x(-k:n-1), y(0:n-1+k))
endif
end function crosscorr
!!!---------------------------------------------------------------------!!!


!!!---------------------------------------------------------------------!!!
subroutine xcorr_full(x, y, n, shift, delay, ccmax, ccpol)
! Cross-correlation of array x and y.
! Return time shift, correlation coefficient, and polarity at maximum correlation.
implicit none
integer :: n, shift, delay, ccpol
real(8), dimension(0:n-1) :: x, y
real(8) :: cc, ccmax, ccmin
integer :: k, kmin
!f2py intent(in)  :: x, y, n, shift
!f2py intent(out) :: delay, ccmax, ccpol
!f2py intent(hide):: c, k, kmin, ccmin

shift = 1
ccmax = 0
ccmin = 0
kmin = 0
do k = -n+1,n-1,shift
    cc = crosscorr(x,y,n,k)   
    if (cc.gt.ccmax) then
        ccmax = cc
        delay = k
    endif
    if (cc.lt.ccmin) then
        ccmin = cc
        kmin = k
    endif
enddo
if (ccmax.gt.-ccmin) then
    ccpol = 1
else
    ccmax = -ccmin
    delay = kmin
    ccpol = -1
endif
ccmax = ccmax/sqrt( dot_product(x,x) * dot_product(y,y) )

end subroutine xcorr_full

这是程序中的代码:

real(8), allocatable :: v(:,:),vref_win(:),v_win(:)

allocate(vref_win(0:ending-starting))
allocate(v_win(0:ending-starting))
vref_win(0:ending-starting)=v(starting-1:ending-1,ref_t)


do i=0,n_stn-1
    v_win(0:ending-starting)=v(starting-1:ending-1,i)
    call xcorr_full(vref_win,v_win,ending-starting+1,1,t_corr,r,sign)
enddo

整个程序有点长,这里就不贴了,但是所有变量都定义正确,数组v有数据,索引正确

如果我只是尝试正常传递数组,这是我得到的错误:

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

我在-fcheck=all使用 gfortran(v7.3.0) 编译时使用该标志

然而,我的问题并不特别依赖于这个程序。对此的任何解释以及任何阅读资源都会非常有帮助,因为我找不到任何可以帮助回答这个确切问题的适当资源。

标签: fortrangfortran

解决方案


推荐阅读