-1

我在文件 4 中写入了一些值,我需要它们再次调用以进行新计算,但我在读取行“读取 (4,*) NNrow(I),Niz(I),NNbin(I),Nfi( I),NfiStdDev(I),NfiAvr(I),NMagbin(I),Nzup(I)” 当我想运行代码时收到错误“发生分段错误”如何再次使用此文件?

                                 do j=1,nmax
                                    if (zb(iz,im,j).ne.0) then
              call Romberg (dix,dDistCa,zb(iz,im,j),zup(iz))                    !COMOVING DISTANCE
              Vmax=dix*S                                    !COMOVINF VOLUME
              fi=fi+1/Vmax                                    !LUMINOSITY FUNCTION
 write(2,'(i5,2x,f9.4,2x,f8.5,2x,3f14.10)')j,magbin,zbin,S,Vmax,dix
            endif

        enddo

            if (Nbin.ge.n_thresh) then
    Nrow=Nrow+1
write(4,'(3i7,2x,f25.8,2x,2f20.8,2x,f9.4,2x,f8.5)')Nrow,iz,Nbin,fi,fiStdDev,fiAvr,magbin,zup(iz)
            endif
              enddo loopmag

rewind(4)
close(4)


write(*,*)Nrow 
 open(4,file='luminosity_func_I.asc')

 allocate (fiStdDev2(Nrow),stat=ok)
 allocate (fi_expected(Nrow),stat=ok)
 allocate (DFI(Nrow),stat=ok)
 allocate (CHISQ(Nrow),stat=ok)
 ! Ln10=2.3025
 A=0.4*2.3025
            do I=1,Nrow        ! NDATA=NMAX
            write(*,*)I
                read (4,*) NNrow(I),Niz(I),NNbin(I),Nfi(I),NfiStdDev(I),NfiAvr(I),NMagbin(I),Nzup(I)

                  fiStdDev2(I)=1/NfiStdDev(I)*NfiStdDev(I)
            write(*,*)fiStdDev2(I)
                  fi_expected(I)=A*fi_star*10**(0.4*(alpha+1)*(M_star-NMagbin(I)))*exp(-10**(0.4*(M_star-NMagbin(I))))
                  DFI(I)=fi_expected(I)-NFI(I)
                  CHISQ(I)=DFI(I)*DFI(I)*fiStdDev2(I)
            END DO
4

1 回答 1

1

据我所知,该声明中可能有两件事出错read

  1. 您正在尝试将信息存储在数组之外,即i > size(<one of the arrays>). 您可以使用-fbounds-checkforgfortran-check boundsfor 进行检查ifort

  2. 从文件中读取时出现问题:

    • 该单位非常低,您可以访问保留的单位 - 尝试类似1234. 另请参阅这篇文章:ansys 的 linux 中的分段错误
    • 您阅读超出文件末尾的内容
    • 没有足够的列可以从文件中读取

您可以iostat=ierrorread语句中检查是否在读取时发生错误。ierror<0这意味着您正在尝试读取文件末尾之外的内容,而ierror>0在读取文件时对应于错误。

于 2013-10-16T15:53:24.113 回答