fortranfortran90fortran95

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

I'm trying to do an Ah=b decomposition for u and up2 in my program. Nevertheless, up2 is depend on u for it's first value. In order to not repeat the decomposition resolution, i made it into a subroutine but whenever i'm trying to call it in a same loop for both u and up2, i keep getting sigmentation fault which i can't identify.


Solution

  • Please learn about compiler warnings and running time error checking. Using gfortran with the appropriate flags on your program I get:

    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 $ 
    

    Note two things

    1. You are using h uninitialized. The compiler tells you this in the second to last warning. I don't know how to fix this, you will need to do that.
    2. Your are accessing the array ap out of bounds. This occurs at the line

      Ap(i) = a

    and the problem is that i is zero. Looking at the code i goes from n1 to n2, so the ultimate problem is that at the line

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

    you are passing the value 0 to n1, but you allocate the array ap as

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

    which is inconsistent with the above usage of Ap. My complete guess is that the do loop should read

    DO i = n1+1,n2
    

    but that is a guess - the problem as presented is due to out of bounds errors in this loop.