Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Module Fortran子例程调用在模块中的while循环中不起作用_Module_Fortran_Subroutine - Fatal编程技术网

Module Fortran子例程调用在模块中的while循环中不起作用

Module Fortran子例程调用在模块中的while循环中不起作用,module,fortran,subroutine,Module,Fortran,Subroutine,我正在写一个Fortran 95程序来生成随机相关曲面。代码在一个“程序”文件中工作,但我需要把它放在一个模块中。出于某种原因,如果我将排序算法放入模块后在while循环中调用它,那么我使用的排序算法将超出范围。如果我在while循环之外调用它,或者如果我在相同大小的向量(代码中的xo)上调用它,它将保持在边界内。我试着在我想要排序的向量的副本上调用它,但失败了。我真的不知道为什么,有人知道吗 我使用gfortran 4.7.3编译器在Ubuntu13.10中工作 使用变量和while循环编码:

我正在写一个Fortran 95程序来生成随机相关曲面。代码在一个“程序”文件中工作,但我需要把它放在一个模块中。出于某种原因,如果我将排序算法放入模块后在while循环中调用它,那么我使用的排序算法将超出范围。如果我在while循环之外调用它,或者如果我在相同大小的向量(代码中的xo)上调用它,它将保持在边界内。我试着在我想要排序的向量的副本上调用它,但失败了。我真的不知道为什么,有人知道吗

我使用gfortran 4.7.3编译器在Ubuntu13.10中工作

使用变量和while循环编码:

subroutine generate_skewed_surface(x, system_length, corr_length, alpha)
real(wp), intent(inout), allocatable :: x(:)
real(wp), intent(in) :: system_length, corr_length, alpha

real(wp) :: deltat, deltao
real(wp) :: prefactor, factor
integer :: dim, i
integer, allocatable :: indx(:), indxp(:)
real(wp), allocatable :: omega_k(:), Sxx(:), xo(:), Rp(:)
complex(wp), allocatable :: Xf(:), Rk(:)
logical :: converged

if(.not. allocated(x)) then
   !main should be alerted
   return
end if
[跳过初始化和中间步骤。唯一发生在x上的事情是它被随机数填充]

!store a copy
xo = x

!sort original signal
call QsortC(xo) !<- this call works

!create surface by reordering original signal
converged = .false.
do while (.not. converged)
   Rk = cmplx(x, 0._wp, wp)
   call fft(Rk)
   Rp = atan2(real(-imu * Rk, wp), real(Rk, wp))
   Rk = exp(imu*Rp)*abs(Xf)
   call ifft(Rk)
   x = real(Rk, wp)
   indx = (/(i, i=1, dim)/)
   call QsortC(x, indx) !<- this call produces the error during the first call
   do i=1, dim
      x(indx(i)) = xo(i)
   end do
   converged = .true.
   do i=1, dim
      if (indx(i) .ne. indxp(i)) then
         converged = .false.
      end if
   end do
   indxp = indx
end do
正如我提到的,代码在一个“程序”文件中工作,但在我创建了一个单独的模块文件之后就不工作了


干杯

事实证明,我在初始化参数变量时使用了错误的语法,因此它们被设置为0,这反过来意味着我在排序一个NaN向量。很抱歉用一个新手错误来打扰你们:)

如果您包含一个可编译和可运行的最小自包含示例,我们可以尝试使用调试器或编译器选项的一些技巧。我在尝试制作最小示例时发现了这一点,感谢您的回复!投票结束,理由是该问题是由无法再复制的问题或简单的印刷错误引起的。虽然类似的问题可能在这里的主题,这是一个解决的方式不太可能帮助未来的读者。既然OP也承认了这一点,这个问题就具备了未来垃圾的所有特征。
module module_qsort
use SFL_precision, only : DP, SP
implicit none
public :: QsortC
private :: Partition

integer, parameter, private :: wp = DP

contains

recursive subroutine QsortC(A, indices)
  real(wp), intent(in out), dimension(:) :: A
  integer, optional, intent(inout), dimension(:) :: indices
  integer :: iq

  if(size(A) > 1) then
     if (present(indices)) then
        call Partition(A, iq, indices)
        call QsortC(A(:iq-1), indices(:iq-1))
        call QsortC(A(iq:), indices(iq:))
     else
        call Partition(A, iq)
        call QsortC(A(:iq-1))
        call QsortC(A(iq:))
     end if
  endif
end subroutine QsortC

subroutine Partition(A, marker, indices)
  real(wp), intent(in out), dimension(:) :: A
  integer, intent(out) :: marker
  integer, optional, intent(inout), dimension(:) :: indices
  integer :: i, j, tempint
  real(wp) :: temp
  real(wp) :: x      ! pivot point
  x = A(1)
  i= 0
  j= size(A) + 1

  do
     j = j-1
     do
        if (A(j) <= x) exit !<- j goes to zero, giving A(0) below lower bound
        j = j-1
     end do
     i = i+1
     do
        if (A(i) >= x) exit
        i = i+1
     end do
     if (i < j) then
        ! exchange A(i) and A(j)
        temp = A(i)
        A(i) = A(j)
        A(j) = temp
        if (present(indices)) then
           tempint = indices(i)
           indices(i) = indices(j)
           indices(j) = tempint
        end if
     elseif (i == j) then
        marker = i+1
        return
     else
        marker = i
        return
     endif
  end do

end subroutine Partition

end module module_qsort
At line 51 of file module_quicksort.f90 Fortran runtime error: Index '0' of dimension 1 of array 'a' below lower bound of 1