子程序段错误

Subroutine Segmentation Fault

PROGRAM olaf
IMPLICIT NONE
INTEGER                                  :: i, j, nc, nd,ok,iter
REAL                                     :: alph, bet, chi, ninf1, C1, ninf2, C2
REAL, DIMENSION(:), ALLOCATABLE          :: u,up2
REAL                                     :: E, k, Lc, hc, eps, h, Ld, Cai
INTEGER, DIMENSION(7)                    :: valnc = (/ 10, 50, 100, 500, 1000, 5000, 10000/)



Do iter=1,7
 nc = valnc(iter)
 Ld=0.2*Lc ; nd=((Lc+Ld)/h)-nc;

 E=25. ; k=125. ; hc=0.01 ; eps=0.01 ; Lc=1 ;

 h = Lc/nc ; chi=sqrt((E*hc)/k) ; alph= -(1/h**2) ; bet=(2/h**2)+(k/(E*hc)) ;


 CALL resolution(0,nc,bet,2*alph,alph,2*alph,-2*eps/h,2*eps/h,u)
 Cai=u(nc)
 CALL resolution(nc+1,(nc+nd)+1,-2.0,2.0,1.0,2.0,2.0*eps*h,-2*eps*h,up2,Cai)


 DEALLOCATE(u,up2)
END DO

CONTAINS

SUBROUTINE resolution (n1,n2,a,b1,b,b2,c1,c2,u1,u2)
 INTEGER, INTENT(IN) :: n1,n2
 REAL, INTENT(IN):: a,b1,b,b2,c1,c2
 REAL, INTENT(IN),OPTIONAL :: u2
 REAL, DIMENSION (:),ALLOCATABLE, INTENT(OUT) :: u1
 REAL, DIMENSION(:), ALLOCATABLE          :: Ap, Ae, Aw, bh, Lw, Lp, Ue, y
 INTEGER :: i
 Logical :: Exist

 ALLOCATE(Ap(n1+1:n2), Ae(n1:n2), Aw(n1:n2),bh(n1:n2))
 ALLOCATE(Lw(n1:n2), Lp(n1:n2), Ue(n1:n2), y(n1:n2),u1(n1:n2))

 Aw=0; Ap=0; Ae=0; bh= 0 ; Lw = 0 ; Lp = 0 ; Ue = 0 ; y=0; Lc=0;

 Exist=Present(u2)
 IF(Exist .eqv. .true.)THEN
  u1(n1)=u2
 END IF

DO i = n1,n2
  Ae(i) = b
  Ap(i) = a
  Aw(i) = b
END DO
Ae(n1)=b1
Aw(n2)=b2

bh(n1)=c1
bh(n2)=c2

Lp(n1) = Ap(n1+2)
Ue(n1) = Ae(n1)/Lp(n1)

DO i = n1+1, n2-1
 Lw(i) = Aw(i)
 Lp(i) = Ap(i) - Lw(i)*Ue(i-1)
 Ue(i) = Ae(i)/Lp(i)
END DO

Lw(n2) = Aw(n2)
Lp(n2) = Ap(n2) - Lw(n2)*Ue(n2-1)
y(n1) = bh(n1)/Lp(n1)

DO i = n1, n2
  y(i) = (bh(i) - Lw(i)*y(i-1)) / Lp(i)
END DO

u1(n2) = y(n2)
DO i = n2-1, n1, -1
 u1(i) = y(i) - Ue(i)*u1(i+1)
END DO

DEALLOCATE(Ap, Ae, Aw,bh,Lw, Lp, Ue, y)

END SUBROUTINE

END PROGRAM olaf

我正在尝试在我的程序中对 u 和 up2 进行 Ah=b 分解。然而,up2 取决于 u 的第一个值。为了不重复分解解析,我把它变成了一个子程序,但每当我试图在同一个循环中为你和 up2 调用它时,我总是遇到我无法识别的信号错误。

请了解编译器警告和运行 时间错误检查。在你的程序中使用带有适当标志的 gfortran 我得到:

ijb@ianbushdesktop ~/work/stack $ gfortran -std=f2003 -Wall -Wextra -fcheck=all -g -O olaf.f90 
olaf.f90:13:16:

  Ld=0.2*Lc ; nd=((Lc+Ld)/h)-nc;
                1
Warning: Possible change of value in conversion from REAL(4) to INTEGER(4) at (1) [-Wconversion]
olaf.f90:4:69:

 REAL                                     :: alph, bet, chi, ninf1, C1, ninf2, C2
                                                                     1
Warning: Unused variable ‘c1’ declared at (1) [-Wunused-variable]
olaf.f90:4:80:

 REAL                                     :: alph, bet, chi, ninf1, C1, ninf2, C2
                                                                                1
Warning: Unused variable ‘c2’ declared at (1) [-Wunused-variable]
olaf.f90:3:45:

 INTEGER                                  :: i, j, nc, nd,ok,iter
                                             1
Warning: Unused variable ‘i’ declared at (1) [-Wunused-variable]
olaf.f90:3:48:

 INTEGER                                  :: i, j, nc, nd,ok,iter
                                                1
Warning: Unused variable ‘j’ declared at (1) [-Wunused-variable]
olaf.f90:4:65:

 REAL                                     :: alph, bet, chi, ninf1, C1, ninf2, C2
                                                                 1
Warning: Unused variable ‘ninf1’ declared at (1) [-Wunused-variable]
olaf.f90:4:76:

 REAL                                     :: alph, bet, chi, ninf1, C1, ninf2, C2
                                                                            1
Warning: Unused variable ‘ninf2’ declared at (1) [-Wunused-variable]
olaf.f90:3:59:

 INTEGER                                  :: i, j, nc, nd,ok,iter
                                                           1
Warning: Unused variable ‘ok’ declared at (1) [-Wunused-variable]
olaf.f90:13:0:

  Ld=0.2*Lc ; nd=((Lc+Ld)/h)-nc;

Warning: ‘h’ may be used uninitialized in this function [-Wmaybe-uninitialized]
olaf.f90:6:0:

 REAL                                     :: E, k, Lc, hc, eps, h, Ld, Cai

note: ‘h’ was declared here
ijb@ianbushdesktop ~/work/stack $ ./a.out
At line 51 of file olaf.f90
Fortran runtime error: Index '0' of dimension 1 of array 'ap' below lower bound of 1

Error termination. Backtrace:
#0  0x400f8d in resolution
    at /home/ijb/work/stack/olaf.f90:51
#1  0x401991 in olaf
    at /home/ijb/work/stack/olaf.f90:20
#2  0x401991 in main
    at /home/ijb/work/stack/olaf.f90:11
ijb@ianbushdesktop ~/work/stack $ 

注意两点

  1. 您正在使用未初始化的 h。编译器在倒数第二个警告中告诉您这一点。我不知道如何解决这个问题,你需要这样做。
  2. 您正在越界访问数组ap。这发生在

    Ap(i) = a

问题是 i 为零。查看代码 in1n2,所以最终的问题是在行

  CALL resolution(0,nc,bet,2*alph,alph,2*alph,-2*eps/h,2*eps/h,u)

您将值 0 传递给 n1,但您将数组 ap 分配为

 ALLOCATE(Ap(n1+1:n2), Ae(n1:n2), Aw(n1:n2),bh(n1:n2))

与上面的Ap用法不一致。我的完整猜测是 do 循环应该读取

DO i = n1+1,n2

但这是一个猜测 - 出现的问题是由于此循环中的越界错误造成的。