- #1
RissaR
- 5
- 0
I'm getting a "Segmentation fault: 11" when running at line 72:
However, the line above it:
runs just fine. Needless to say, I'm clueless. Here's my entire code.
Any clue what's happening here?
Code:
IF (h_temp == blayer) THEN
Code:
PRINT *,h_temp==blayer
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?