Skip to content

Commit

Permalink
Merge pull request #2436 from luwang00/f/MD_Flexible_WaveKin_File
Browse files Browse the repository at this point in the history
MD: Automatically detect the number of header lines in the WaveKin (wave elevation) file
  • Loading branch information
andrew-platt authored Sep 30, 2024
2 parents 14958ec + b34fb63 commit 56c645d
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions modules/moordyn/src/MoorDyn_Misc.f90
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,7 @@ SUBROUTINE setupWaterKin(WaterKinString, p, Tmax, ErrStat, ErrMsg)
INTEGER(IntKi), INTENT( OUT) :: ErrStat ! Error status of the operation
CHARACTER(*), INTENT( OUT) :: ErrMsg ! Error message if ErrStat /= ErrID_None

INTEGER(IntKi) :: I, iIn, ix, iy, iz
INTEGER(IntKi) :: I, iIn, ix, iy, iz, numHdrLn
INTEGER(IntKi) :: ntIn ! number of time series inputs from file
INTEGER(IntKi) :: UnIn ! unit number for coefficient input file
INTEGER(IntKi) :: UnEcho
Expand All @@ -1302,6 +1302,7 @@ SUBROUTINE setupWaterKin(WaterKinString, p, Tmax, ErrStat, ErrMsg)
CHARACTER(120) :: Line
CHARACTER(4096) :: entries2
INTEGER(IntKi) :: coordtype
LOGICAL :: dataBegin

INTEGER(IntKi) :: NStepWave !
INTEGER(IntKi) :: NStepWave2 !
Expand All @@ -1313,7 +1314,7 @@ SUBROUTINE setupWaterKin(WaterKinString, p, Tmax, ErrStat, ErrMsg)
REAL(SiKi), ALLOCATABLE :: TmpFFTWaveElev(:) ! Data for the FFT calculation
TYPE(FFT_DataType) :: FFT_Data ! the instance of the FFT module we're using


REAL(SiKi) :: tmpReal ! A temporary real number
COMPLEX(SiKi),ALLOCATABLE :: tmpComplex(:) ! A temporary array (0:NStepWave2-1) for FFT use.

REAL(SiKi) :: Omega ! Wave frequency (rad/s)
Expand Down Expand Up @@ -1469,26 +1470,38 @@ SUBROUTINE setupWaterKin(WaterKinString, p, Tmax, ErrStat, ErrMsg)
call WrScr( 'Reading wave elevation data from '//trim(WaveKinFile) )

! Read through length of file to find its length
i = 1 ! start counter
i = 0 ! start line counter
numHdrLn = 0 ! start header-line counter
dataBegin = .FALSE. ! started reading the data section
DO
READ(UnElev,'(A)',IOSTAT=ErrStat2) Line !read into a line
IF (ErrStat2 /= 0) EXIT ! break out of the loop if it couldn't read the line (i.e. if at end of file)
i = i+1
READ(Line,*,IOSTAT=ErrStatTmp) tmpReal
IF (ErrStatTmp/=0) THEN ! Not a number
IF (dataBegin) THEN
CALL SetErrStat( ErrID_Fatal,' Non-data line detected in WaveKinFile past the header lines.',ErrStat, ErrMsg, RoutineName); return
END IF
numHdrLn = numHdrLn + 1
ELSE
dataBegin = .TRUE.
END IF
END DO

! rewind to start of input file to re-read things now that we know how long it is
REWIND(UnElev)

ntIn = i-3 ! save number of lines of file
ntIn = i-numHdrLn ! save number of lines of file


! allocate space for input wave elevation array (including time column)
CALL AllocAry(WaveTimeIn, ntIn, 'WaveTimeIn', ErrStat2, ErrMsg2 ); if(Failed()) return
CALL AllocAry(WaveElevIn, ntIn, 'WaveElevIn', ErrStat2, ErrMsg2 ); if(Failed()) return

! read the data in from the file
READ(UnElev,'(A)',IOSTAT=ErrStat2) Line ! skip the first two lines as headers
READ(UnElev,'(A)',IOSTAT=ErrStat2) Line !
DO i = 1, numHdrLn
READ(UnElev,'(A)',IOSTAT=ErrStat2) Line ! skip header lines
END DO

DO i = 1, ntIn
READ (UnElev, *, IOSTAT=ErrStat2) WaveTimeIn(i), WaveElevIn(i)
Expand All @@ -1502,7 +1515,7 @@ SUBROUTINE setupWaterKin(WaterKinString, p, Tmax, ErrStat, ErrMsg)
CLOSE ( UnElev )

IF (WaveTimeIn(1) .NE. 0.0) THEN
CALL SetErrStat( ErrID_Warn, ' MoorDyn WaveElev time series should start at t = 0 seconds. First two lines are read as headers.',ErrStat, ErrMsg, RoutineName); return
CALL SetErrStat( ErrID_Fatal, ' MoorDyn WaveElev time series should start at t = 0 seconds.',ErrStat, ErrMsg, RoutineName); return
ENDIF

call WrScr( "Read "//trim(num2lstr(ntIn))//" time steps from input file." )
Expand Down

0 comments on commit 56c645d

Please sign in to comment.