Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran Segmentation Fault

  1. Apr 27, 2012 #1
    I'm getting a "Segmentation fault: 11" when running at line 72:
    Code (Text):
      IF (h_temp == blayer) THEN
    However, the line above it:
    Code (Text):
      PRINT *,h_temp==blayer
    runs just fine. Needless to say, I'm clueless. Here's my entire code.
    Code (Text):
    PROGRAM bunkers

          REAL, DIMENSION(5) :: ulist,vlist,hlist
          REAL, DIMENSION(2) :: wind
          INTEGER :: numvars

          ulist = (/ 1,2,3,4,5 /)
          vlist = (/ 4,5,6,6,7 /)
          hlist = (/ 100,200,300,400,500 /)
          numvars = SIZE(ulist)
          CALL meanwind(ulist,vlist,hlist,numvars,150.0,450.0,wind)

          PRINT *,'The mean wind is', wind

    END PROGRAM bunkers

    SUBROUTINE lininterp(ulist,vlist,hlist,numvars,targetlvl,u,v)

          !Dummy argument declarations
          INTEGER, INTENT(IN) :: numvars
          REAL, DIMENSION(numvars), INTENT(IN) :: ulist,vlist,hlist
          REAL, INTENT(IN) :: targetlvl
          REAL, INTENT(OUT) :: u,v

          !Local variable declaration
          REAL :: spacing, currenth, nexth
          INTEGER :: i
          spacing = hlist(2) - hlist(1)
          DO i=1,SIZE(hlist)
                currenth = hlist(i)
                nexth = hlist(i+1)
                IF (targetlvl>currenth .AND. targetlvl<nexth) THEN
                END IF
          END DO
    END SUBROUTINE lininterp

    SUBROUTINE meanwind(ulist,vlist,hlist,numvars,blayer,tlayer,wind)

          !Dummy argument declarations
          INTEGER, INTENT(IN) :: numvars
          REAL, INTENT(IN) :: blayer,tlayer
          REAL, DIMENSION(numvars), INTENT(IN) :: ulist,vlist,hlist
          REAL, DIMENSION(2), INTENT(OUT) :: wind
          !Local variable declaration
          REAL :: umodmean, vmodmean, u, v,angle, headnewx_1, headnewx_2, h_temp,u_temp,v_temp
          INTEGER :: i,j,vmod
          REAL, ALLOCATABLE, DIMENSION(:) :: tailvect,headvect
          REAL, DIMENSION(2,1) :: a, headnewx, meanwindmat
          REAL, DIMENSION(2) ::   vector,modheadvect,vect,modmean
          REAL, DIMENSION (2,2) :: rotation,counter
          !Find head and tail wind vectors
          DO i=1,numvars
                h_temp = hlist(i)
                u_temp = ulist(i)
                v_temp = vlist(i)
                PRINT *,h_temp==blayer
                IF (h_temp == blayer) THEN
                      tailvect = (/ u_temp,v_temp /)
                ELSE IF (h_temp == tlayer) THEN
                      headvect = (/ u_temp, v_temp /)
                ELSE IF (h_temp >= tlayer) THEN
                END IF
          END DO

          IF (.not. allocated(tailvect)) THEN
                CALL lininterp(ulist,vlist,hlist,numvars,blayer,u,v)
                tailvect = (/ u,v /)
          END IF

          IF (.not. allocated(headvect)) THEN
                CALL lininterp(ulist,vlist,hlist,numvars,tlayer,u,v)
                headvect = (/ u,v /)
          END IF
          !Move coord sys so tail vect is origin
          modheadvect = (/ headvect(1)-tailvect(1),headvect(2)-tailvect(2) /)
          angle = acos(modheadvect(1) / sqrt(modheadvect(1)**2.0 + modheadvect(2)**2.0))
          IF (modheadvect(2) .lt. 0) THEN
                angle = -1*angle
          END IF
          rotation = RESHAPE((/ cos(angle),sin(angle),-1*sin(angle),cos(angle) /),(/2,2 /))
          counter = RESHAPE( (/cos(angle),-1*sin(angle),sin(angle),cos(angle) /),(/2,2/))
          !Rotate hodograph so x-axis along mean shear vector
          vmod = 0
          j = 0
          DO i = 1,SIZE(hlist)
                IF (hlist(i) .ge. blayer .and. hlist(i) .le. tlayer) THEN
                      vect = (/ ulist(i) - tailvect(1),vlist(i)-tailvect(2) /)
                      a = RESHAPE((/ rotation(1,1)*vect(1)+rotation(2,1)*vect(1),rotation(1,2)*vect(2),rotation(2,2)*vect(2) /),(/2,1/))
                      vmod = vmod + a(2,1)
                      j = j + 1
                ELSE IF (hlist(i) .gt. tlayer) THEN
                END IF
          END DO
          !Rotate head vector to make it new x-axis
          headnewx_1 = rotation(1,1)*modheadvect(1)+rotation(2,1)*modheadvect(1)
          headnewx_2 = rotation(1,2)*modheadvect(2)+rotation(2,2)*modheadvect(2)
          headnewx = RESHAPE((/headnewx_1,headnewx_2/),(/2,1/))
          !Mean u wind is half of diff b/w bottom and top u
          umodmean = headnewx(1,1) / 2.0
          !Mean v wind is average of v obs in coord sys
          vmodmean = vmod/j
          !Rotate coord system back
          meanwindmat = RESHAPE((/counter(1,1)* umodmean,counter(2,1)*vmodmean/),(/2,1/))
          !Add mean wind to tail vector for true mean wind vector
          wind = (/meanwindmat(1,1)+tailvect(1),meanwindmat(2,1)+tailvect(2)/)
    END SUBROUTINE meanwind
    Any clue what's happening here?
  2. jcsd
  3. Apr 27, 2012 #2


    Staff: Mentor

    Re: Fortran Segmentation Faul

    Just a guess, but I think that your PRINT statement is causing problems. To test this, comment out this line
    Code (Text):
    PRINT *,h_temp==blayer
    If that eliminates the seg fault, then you know which is the offending code.

    Regarding that line, what is it supposed to do? h_temp == blayer will evaluate to true or false, depending on whether the two quantities are equal or not. Is you intent to print TRUE or FALSE?
  4. Apr 27, 2012 #3
    Re: Fortran Segmentation Faul

    I put in the PRINT statement just to test whether the statement inside the IF block was evaluating correctly. It was *not* there when the seg fault first occurred and commenting it out (or removing it all together) does nothing. The PRINT statement was only put in to check what the problem could be.
  5. Apr 27, 2012 #4
    Re: Fortran Segmentation Faul

    you sure?

    get a "stop" statement and start placing at various lines to really figure out if the program is failing where you think is failing....compile and re-run every time. Try having a write statement right before the stop...just to make you got there, etc. You know, some good old fashion debugging...it has never failed me.

    Is the print statement working? What does it print ? True or False?

    I wouldn't trust '==' when comparing two REAL variables...you really need to use some tolerance, there...maybe you think you are going into the "IF () THEN" clause, but you are not...maybe you are going to one of the ELSE clause and maybe even getting out of the DO-loop altogether and the segmentation fault is happening somewhere else.

    Please confirm.
    (I actually know the answer, but I won't tell you :-) )
    Last edited: Apr 27, 2012
  6. Apr 27, 2012 #5


    Staff: Mentor

    Re: Fortran Segmentation Faul

    Good advice.
    I see it, too.
  7. Apr 27, 2012 #6
    Re: Fortran Segmentation Faul

    I had already put STOP in several places over and over and had identified that as a break point in the past. I thought that it was still breaking in the same spot, but wasn't (brain fart perhaps). All is fixed now, forgot to allocate space for the head and tail vectors after that specific IF block.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook