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

In summary: THEN !Free up memory by deleting the temporary vector FREE(tailvect) ELSE !Copy the tail vector into the head vector headvect(1) = tailvect(2) END IF RETURN END SUBROUTINE meanwind In
  • #1
RissaR
5
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
  • #2


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?
 
  • #3


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.
 
  • #4


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:
  • #5


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.
 
  • #6


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.
 

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

1. What is a Fortran Segmentation Fault?

A Fortran Segmentation Fault, also known as a segmentation violation, is a common error that occurs when a program attempts to access a memory location that is not allocated to it. This can happen due to a variety of reasons, such as a programming error or a hardware issue.

2. What causes a Fortran Segmentation Fault?

There are several potential causes for a Fortran Segmentation Fault. One possibility is that the program is trying to access memory that it does not have permission to access. This can happen due to a programming error, such as a buffer overflow, or due to a hardware issue, such as faulty RAM. Another cause could be a missing or corrupted file that the program is trying to access.

3. How do I debug a Fortran Segmentation Fault?

Debugging a Fortran Segmentation Fault can be a challenging task, as it requires identifying the specific line of code that is causing the error. One approach is to use a debugger tool, such as GDB, to step through the code and identify the line where the error occurs. Another approach is to use print statements or logging to track the program's execution and identify the source of the error.

4. Can I prevent a Fortran Segmentation Fault?

While it is not always possible to prevent a Fortran Segmentation Fault, there are steps that can be taken to minimize the chances of it occurring. These include writing code with proper memory management techniques, avoiding buffer overflows, and regularly testing the program for errors. It is also important to keep hardware and software up-to-date to prevent any potential issues.

5. How do I fix a Fortran Segmentation Fault?

The approach to fixing a Fortran Segmentation Fault will depend on the specific cause of the error. If it is a programming error, then going through the code and identifying and fixing the issue should resolve the error. If it is a hardware issue, then addressing the underlying hardware problem, such as replacing faulty RAM, should fix the error. In some cases, it may be necessary to seek help from a more experienced programmer or consult online resources for troubleshooting tips.

Similar threads

  • Programming and Computer Science
Replies
4
Views
12K
  • Programming and Computer Science
2
Replies
54
Views
4K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
5K
Back
Top