Fortran Why am I getting a Segmentation fault: 11 when running my Fortran code?

  • Thread starter Thread starter RissaR
  • Start date Start date
  • Tags Tags
    Fault Fortran
AI Thread Summary
The discussion centers on a "Segmentation fault: 11" error occurring in a Fortran program, specifically at the line checking if `h_temp` equals `blayer`. The user reports that a preceding print statement works without issue, leading to confusion about the source of the error. Suggestions include commenting out the print statement to isolate the problem, as well as implementing debugging techniques such as adding stop statements to trace execution flow. There's a caution against using the equality operator `==` for comparing real numbers due to potential precision issues, recommending the use of a tolerance instead. Ultimately, the user identifies that the segmentation fault was caused by not allocating space for the head and tail vectors after the relevant conditional block, resolving the issue.
RissaR
Messages
5
Reaction score
0
I'm getting a "Segmentation fault: 11" when running at line 72:
Code:
  IF (h_temp == blayer) THEN
However, the line above it:
Code:
  PRINT *,h_temp==blayer
runs just fine. Needless to say, I'm clueless. Here's my entire code.
Code:
PROGRAM bunkers
      IMPLICIT NONE

      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

      STOP
END PROGRAM bunkers


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

      !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
                  u=(ulist(i+1)-ulist(i))/spacing*(targetlvl-hlist(i))+ulist(i)
                  v=(vlist(i+1)-vlist(i))/spacing*(targetlvl-hlist(i))+vlist(i)
            END IF
      END DO
      RETURN
END SUBROUTINE lininterp

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

      !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
                  ALLOCATE(tailvect(2))
                  tailvect = (/ u_temp,v_temp /)
            ELSE IF (h_temp == tlayer) THEN
                  ALLOCATE(headvect(2))
                  headvect = (/ u_temp, v_temp /)
            ELSE IF (h_temp >= tlayer) THEN
                  EXIT
            ELSE
                  EXIT
            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
                  EXIT
            ELSE
                  EXIT
            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/))
      !WIND MEASUREMENTS MUST BE EVENLY HEIGHT-DISTRIBUTED IN THE VERTICAL FOR THESE TO WORK
      !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)/)
      
      RETURN 
END SUBROUTINE meanwind

Any clue what's happening here?
 
Technology news on Phys.org


Just a guess, but I think that your PRINT statement is causing problems. To test this, comment out this line
Code:
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?
 


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 into check what the problem could be.
 


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:


gsal said:
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.
Good advice.
gsal said:
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 :-) )

I see it, too.
 


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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
3
Views
12K
Replies
54
Views
5K
Back
Top