Skip to content

Commit

Permalink
FAST.Farm: fix some memory leaks (#860)
Browse files Browse the repository at this point in the history
1. AWAE_End was never called, so some memory wasn't getting deallocated from InflowWind

2. Many of the "farm" variables were not deallocated

3. I made the OutList(:) array in FAST.Farm a fixed size instead of allocatable. It was getting allocated to a fixed size (set by parameter) anyway.
  • Loading branch information
bjonkman committed Oct 7, 2021
1 parent 7a59bec commit 9cdd272
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions glue-codes/fast-farm/src/FAST_Farm_Subs.f90
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ SUBROUTINE Farm_Initialize( farm, InputFile, ErrStat, ErrMsg )
TYPE(SC_InitInputType) :: SC_InitInp ! input-file data for SC module
TYPE(SC_InitOutputType) :: SC_InitOut ! Init output for SC module
CHARACTER(*), PARAMETER :: RoutineName = 'Farm_Initialize'
CHARACTER(ChanLen),ALLOCATABLE :: OutList(:) ! list of user-requested output channels
CHARACTER(ChanLen) :: OutList(Farm_MaxOutPts) ! list of user-requested output channels
INTEGER(IntKi) :: i
!..........
ErrStat = ErrID_None
Expand Down Expand Up @@ -253,7 +253,9 @@ SUBROUTINE Farm_Initialize( farm, InputFile, ErrStat, ErrMsg )
IF (ErrStat >= AbortErrLev) THEN
CALL Cleanup()
RETURN
END IF
END IF

farm%AWAE%IsInitialized = .true.

farm%p%X0_Low = AWAE_InitOutput%X0_Low
farm%p%Y0_low = AWAE_InitOutput%Y0_low
Expand All @@ -278,6 +280,7 @@ SUBROUTINE Farm_Initialize( farm, InputFile, ErrStat, ErrMsg )
return
end if
farm%p%Module_Ver( ModuleFF_SC ) = SC_InitOut%Ver
farm%SC%IsInitialized = .true.
else
farm%SC%p%nInpGlobal = 0
farm%SC%p%NumParamGlobal = 0
Expand Down Expand Up @@ -364,7 +367,7 @@ SUBROUTINE Farm_ReadPrimaryFile( InputFile, p, WD_InitInp, AWAE_InitInp, SC_Init
TYPE(WD_InputFileType), INTENT( OUT) :: WD_InitInp !< input-file data for WakeDynamics module
TYPE(AWAE_InputFileType), INTENT( OUT) :: AWAE_InitInp !< input-file data for AWAE module
TYPE(SC_InitInputType), INTENT( OUT) :: SC_InitInp !< input-file data for SC module
CHARACTER(ChanLen),ALLOCATABLE, INTENT( OUT) :: OutList(:) !< list of user-requested output channels
CHARACTER(ChanLen), INTENT( OUT) :: OutList(:) !< list of user-requested output channels
INTEGER(IntKi), INTENT( OUT) :: ErrStat !< Error status
CHARACTER(*), INTENT( OUT) :: ErrMsg !< Error message

Expand Down Expand Up @@ -410,13 +413,6 @@ SUBROUTINE Farm_ReadPrimaryFile( InputFile, p, WD_InitInp, AWAE_InitInp, SC_Init
RETURN
end if

CALL AllocAry( OutList, Farm_MaxOutPts, "FAST.Farm's Input File's Outlist", ErrStat2, ErrMsg2 )
CALL SetErrStat( ErrStat2, ErrMsg2, ErrStat, ErrMsg, RoutineName )
IF ( ErrStat >= AbortErrLev ) THEN
CALL Cleanup()
RETURN
END IF

! Read the lines up/including to the "Echo" simulation control variable
! If echo is FALSE, don't write these lines to the echo file.
! If Echo is TRUE, rewind and write on the second try.
Expand Down Expand Up @@ -2282,8 +2278,9 @@ subroutine FARM_End(farm, ErrStat, ErrMsg)
! 1. end AWAE
if (farm%AWAE%IsInitialized) then
call AWAE_End( farm%AWAE%u, farm%AWAE%p, farm%AWAE%x, farm%AWAE%xd, farm%AWAE%z, &
farm%AWAE%OtherSt, farm%AWAE%y, farm%AWAE%m, ErrStat2, ErrMsg2 )
farm%AWAE%OtherSt, farm%AWAE%y, farm%AWAE%m, ErrStat2, ErrMsg2 )
call SetErrStat(ErrStat2, ErrMsg2, ErrStat, ErrMsg, RoutineName)
farm%AWAE%IsInitialized = .false.
end if


Expand All @@ -2294,8 +2291,9 @@ subroutine FARM_End(farm, ErrStat, ErrMsg)
DO nt = 1,farm%p%NumTurbines
if (farm%WD(nt)%IsInitialized) then
call WD_End( farm%WD(nt)%u, farm%WD(nt)%p, farm%WD(nt)%x, farm%WD(nt)%xd, farm%WD(nt)%z, &
farm%WD(nt)%OtherSt, farm%WD(nt)%y, farm%WD(nt)%m, ErrStat2, ErrMsg2 )
farm%WD(nt)%OtherSt, farm%WD(nt)%y, farm%WD(nt)%m, ErrStat2, ErrMsg2 )
call SetErrStat(ErrStat2, ErrMsg2, ErrStat, ErrMsg, 'T'//trim(num2lstr(nt))//':'//RoutineName)
farm%WD(nt)%IsInitialized = .false.
end if
END DO

Expand All @@ -2307,6 +2305,7 @@ subroutine FARM_End(farm, ErrStat, ErrMsg)
if ( farm%p%useSC ) then
CALL SC_End(farm%SC%uInputs, farm%SC%p, farm%SC%x, farm%SC%xd, farm%SC%z, farm%SC%OtherState, &
farm%SC%y, farm%SC%m, ErrStat2, ErrMsg2)
farm%SC%IsInitialized = .false.
end if

!--------------
Expand All @@ -2318,6 +2317,7 @@ subroutine FARM_End(farm, ErrStat, ErrMsg)
CALL FWrap_End( farm%FWrap(nt)%u, farm%FWrap(nt)%p, farm%FWrap(nt)%x, farm%FWrap(nt)%xd, farm%FWrap(nt)%z, &
farm%FWrap(nt)%OtherSt, farm%FWrap(nt)%y, farm%FWrap(nt)%m, ErrStat2, ErrMsg2 )
call SetErrStat(ErrStat2, ErrMsg2, ErrStat, ErrMsg, 'T'//trim(num2lstr(nt))//':'//RoutineName)
farm%FWrap(nt)%IsInitialized = .false.
end if
END DO

Expand All @@ -2327,7 +2327,15 @@ subroutine FARM_End(farm, ErrStat, ErrMsg)
! close output file
!.......................................................................................
call Farm_EndOutput( farm, ErrStat2, ErrMsg2 )
call SetErrStat(ErrStat2, ErrMsg2, ErrStat, ErrMsg, 'T'//trim(num2lstr(nt))//':'//RoutineName)
call SetErrStat(ErrStat2, ErrMsg2, ErrStat, ErrMsg,RoutineName)


!.......................................................................................
! clear all data from 'farm' structure
!.......................................................................................
call Farm_DestroyAll_FastFarm_Data( farm, ErrStat2, ErrMsg2 )
call SetErrStat(ErrStat2, ErrMsg2, ErrStat, ErrMsg, RoutineName)

end subroutine FARM_End
!----------------------------------------------------------------------------------------------------------------------------------
SUBROUTINE Transfer_FAST_to_WD(farm)
Expand Down

0 comments on commit 9cdd272

Please sign in to comment.