Skip to content

Commit

Permalink
NWTC: adding tools for string manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
ebranlard committed Aug 21, 2023
1 parent 62c5f46 commit 44b9a9a
Show file tree
Hide file tree
Showing 27 changed files with 169 additions and 131 deletions.
1 change: 0 additions & 1 deletion modules/aerodyn/src/FVW.f90
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@ subroutine FVW_SetParametersFromInputs( InitInp, p, ErrStat, ErrMsg )
integer(IntKi), intent( out) :: ErrStat !< Error status of the operation
character(*), intent( out) :: ErrMsg !< Error message if ErrStat /= ErrID_None
! Local variables
character(1024) :: rootDir, baseName ! Simulation root dir and basename
integer(IntKi) :: iW, nBldMax
integer(IntKi), allocatable :: nBldPerRot(:)
integer(IntKi) :: ErrStat2
Expand Down
48 changes: 4 additions & 44 deletions modules/aerodyn/src/FVW_IO.f90
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ subroutine ReadGridOut(sLine, GridOut)
! Name
GridOut%name =StrArray(1)
! Type
if (.not. is_int (StrArray(2), GridOut%type ) ) then
if (.not. is_integer(StrArray(2), GridOut%type ) ) then
ErrMsg2=trim(ErrMsg2)//NewLine//'GridType needs to be an integer.'
return
endif
Expand Down Expand Up @@ -317,62 +317,22 @@ subroutine ReadGridOut(sLine, GridOut)
ErrMsg2='Error reading OLAF "x" inputs for grid outputs line: '//trim(sLine)
if (.not. is_numeric(StrArray( 6), GridOut%xStart) ) return
if (.not. is_numeric(StrArray( 7), GridOut%xEnd ) ) return
if (.not. is_int (StrArray( 8), GridOut%nx ) ) return
if (.not. is_integer(StrArray( 8), GridOut%nx ) ) return
ErrMsg2='Error reading OLAF "y" inputs for grid outputs line: '//trim(sLine)
if (.not. is_numeric(StrArray( 9), GridOut%yStart) ) return
if (.not. is_numeric(StrArray(10), GridOut%yEnd ) ) return
if (.not. is_int (StrArray(11), GridOut%ny ) ) return
if (.not. is_integer(StrArray(11), GridOut%ny ) ) return
ErrMsg2='Error reading OLAF "z" inputs for grid outputs line: '//trim(sLine)
if (.not. is_numeric(StrArray(12), GridOut%zStart) ) return
if (.not. is_numeric(StrArray(13), GridOut%zEnd ) ) return
if (.not. is_int (StrArray(14), GridOut%nz ) ) return
if (.not. is_integer(StrArray(14), GridOut%nz ) ) return
! Success
ErrStat2=ErrID_None
ErrMsg2=''
end subroutine ReadGridOut

END SUBROUTINE FVW_ReadInputFile

function is_numeric(string, x)
implicit none
character(len=*), intent(in) :: string
real(reki), intent(out) :: x
logical :: is_numeric
integer :: e,n
character(len=12) :: fmt
x = 0.0_reki
n=len_trim(string)

if (n==0) then ! blank lines shouldn't be valid numbers
is_numeric = .false.
return
end if

write(fmt,'("(F",I0,".0)")') n
read(string,fmt,iostat=e) x
is_numeric = e == 0
end function is_numeric

function is_int(string, x)
implicit none
character(len=*), intent(in) :: string
integer(IntKi), intent(out) :: x
logical :: is_int
integer :: e,n
character(len=12) :: fmt
x = 0
n=len_trim(string)

if (n==0) then ! blank lines shouldn't be valid integers
is_int = .false.
return
end if

write(fmt,'("(I",I0,")")') n
read(string,fmt,iostat=e) x
is_int = e == 0
end function is_int


!=================================================
!> Export FVW variables to VTK
Expand Down
1 change: 1 addition & 0 deletions modules/nwtc-library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ set(NWTCLIBS_SOURCES
src/NWTC_IO.f90
src/NWTC_Library.f90
src/NWTC_Num.f90
src/NWTC_Str.f90
src/NWTC_RandomNumber.f90

src/NWTC_Library_Types.f90
Expand Down
1 change: 1 addition & 0 deletions modules/nwtc-library/Old_test/Test_ChkRealFmtStr/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ LIB_SOURCES = \
$(SYS_FILE).f90 \
NWTC_IO.f90 \
NWTC_Num.f90 \
NWTC_Str.f90 \
ModMesh.f90 \
NWTC_Aero.f90 \
NWTC_Library.f90
Expand Down
1 change: 1 addition & 0 deletions modules/nwtc-library/Old_test/Test_FileSize/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ LIB_SOURCES = \
$(SYS_FILE).f90 \
NWTC_IO.f90 \
NWTC_Num.f90 \
NWTC_Str.f90 \
ModMesh_Types.f90 \
ModMesh.f90 \
NWTC_Aero.f90 \
Expand Down
1 change: 1 addition & 0 deletions modules/nwtc-library/Old_test/Test_MeshMapping/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ LIB_SOURCES = \
NWTC_Base.f90 \
$(SYS_FILE).f90 \
NWTC_IO.f90 \
NWTC_Str.f90 \
NWTC_Library_Types.f90 \
ModMesh_Types.f90 \
ModMesh.f90 \
Expand Down
1 change: 1 addition & 0 deletions modules/nwtc-library/Old_test/Test_OpenCon_GnuWin/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ LIB_SOURCES = \
$(SYS_FILE).f90 \
NWTC_IO.f90 \
NWTC_Num.f90 \
NWTC_Str.f90 \
ModMesh.f90 \
NWTC_Aero.f90 \
NWTC_Library.f90
Expand Down
1 change: 1 addition & 0 deletions modules/nwtc-library/Old_test/Test_ReadComFile/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ LIB_SOURCES = \
$(SYS_FILE).f90 \
NWTC_IO.f90 \
NWTC_Num.f90 \
NWTC_Str.f90 \
ModMesh_Types.f90 \
ModMesh.f90 \
NWTC_Library.f90
Expand Down
3 changes: 3 additions & 0 deletions modules/nwtc-library/src/NWTC_Library.f90
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ MODULE NWTC_Library
! NWTC_Library.f90
! NWTC_Library_Types.f90
! NWTC_Num.f90
! NWTC_Str.f90
! ModMesh.f90
! ModMesh_Types.f90
!
Expand All @@ -51,6 +52,7 @@ MODULE NWTC_Library
! NWTC_Library_Types.f90
! NWTC_IO.f90
! NWTC_Num.f90
! NWTC_Str.f90
! ModMesh_Types.f90
! ModMesh.f90
! ModMesh_Mapping.f90 (remove if compiling with -DNO_MESHMAPPING)
Expand All @@ -73,6 +75,7 @@ MODULE NWTC_Library

USE NWTC_Library_Types
USE NWTC_Num ! technically we don't need to specify this if we have ModMesh (because ModMesh USEs NWTC_Num)
USE NWTC_Str ! String utils
USE ModMesh
USE ModReg

Expand Down
141 changes: 141 additions & 0 deletions modules/nwtc-library/src/NWTC_Str.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
!**********************************************************************************************************************************
! LICENSING
! Copyright (C) 2013-2016 National Renewable Energy Laboratory
!
! This file is part of the NWTC Subroutine Library.
!
! Licensed under the Apache License, Version 2.0 (the "License");
! you may not use this file except in compliance with the License.
! You may obtain a copy of the License at
!
! http://www.apache.org/licenses/LICENSE-2.0
!
! Unless required by applicable law or agreed to in writing, software
! distributed under the License is distributed on an "AS IS" BASIS,
! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
! See the License for the specific language governing permissions and
! limitations under the License.
!**********************************************************************************************************************************

!> This module contains string manipulation routines
MODULE NWTC_Str

use Precision ! ProgDesc and other types with copy and other routines for those types

implicit none

interface is_numeric
module procedure is_numericR4
module procedure is_numericR8
end interface

CONTAINS



!> Count number of occurence of a substring in an input string.
function countsubstring(s1, s2) result(c)
character(len=*), intent(in) :: s1 !< Input string
character(len=*), intent(in) :: s2 !< string to be searched
integer :: c !< number of substrings
integer :: p, posn
c = 0
if(len(s2) == 0) return
p = 1
do
posn = index(s1(p:), s2)
if(posn == 0) return
c = c + 1
p = p + posn + len(s2)
end do
end function countsubstring

!> split a string according to a delimiter of size 1
subroutine strsplit(String, StrArray, delimiter)
character(len=*),intent(in) :: String
character(len=1),intent(in) :: delimiter
character(1024), intent(out), allocatable :: StrArray(:) ! Array of strings extracted from line
! Variable
integer :: j, k, l, n, nmax
logical :: EndOfLine
! Find number of occurences
n = countsubstring(String, delimiter)
nmax = n+1
! Allocate substrings
if(allocated(StrArray)) deallocate(StrArray)
allocate(StrArray(nmax))
StrArray(:)=''
! Loop on string and store splits
n = 0
k = 1
l = len_trim(string)
EndOfLine = l-k < 0
do while (.not.EndOfLine)
j = index(string(k:l),delimiter)
if (j == 0) then
j = l + 1
else
j = j + k - 1
end if
n = n + 1
if(n==nmax) then
StrArray(n) = String(k:len(String))
EndOfLine = .true.
else
if (j /= k .and. len_trim(string(k:j-1)) /= 0) StrArray(n) = String(k:j-1)
k = j + 1
EndOfLine = l-k < 0
endif
end do
end subroutine strsplit

!> Return true if string is an integer, and also return the integer
logical function is_integer(string, x)
character(len=*), intent(in ) :: string
integer(IntKi), intent(out) :: x
integer :: e, n
x = 0
n=len_trim(string)
if (n==0) then ! blank lines shouldn't be valid integers
is_integer = .false.
return
end if
read(string,*,iostat=e) x
is_integer = e == 0
end function is_integer

logical function is_numericR4(string, x) result(is_numeric)
character(len=*), intent(in ) :: string
real(SiKi), intent(out) :: x
integer :: e,n
character(len=12) :: fmt
x = 0.0_ReKi
n=len_trim(string)
write(fmt,'("(F",I0,".0)")') n
read(string,fmt,iostat=e) x
is_numeric = e == 0
end function is_numericR4

logical function is_numericR8(string, x) result(is_numeric)
character(len=*), intent(in ) :: string
real(R8Ki), intent(out) :: x
integer :: e,n
character(len=12) :: fmt
x = 0.0_ReKi
n=len_trim(string)
write(fmt,'("(F",I0,".0)")') n
read(string,fmt,iostat=e) x
is_numeric = e == 0
end function is_numericR8

logical function is_logical(string, b)
character(len=*), intent(in ) :: string
logical, intent(out) :: b
integer :: e,n
b = .false.
n=len_trim(string)
read(string,*,iostat=e) b
is_logical = e == 0
end function is_logical

END MODULE NWTC_Str
16 changes: 0 additions & 16 deletions modules/seastate/src/UserWaves.f90
Original file line number Diff line number Diff line change
Expand Up @@ -1236,20 +1236,4 @@ SUBROUTINE ReadRealNumber(UnitNum, FileName, VarName, VarRead, StrRead, IsRealNu
RETURN
END SUBROUTINE ReadRealNumber


FUNCTION is_numeric(string, x)
IMPLICIT NONE
CHARACTER(len=*), INTENT(IN) :: string
REAL(SiKi), INTENT(OUT) :: x
LOGICAL :: is_numeric

INTEGER :: e,n
CHARACTER(len=12) :: fmt
x = 0.0_SiKi
n=LEN_TRIM(string)
WRITE(fmt,'("(F",I0,".0)")') n
READ(string,fmt,IOSTAT=e) x
is_numeric = e == 0
END FUNCTION is_numeric

END MODULE UserWaves
38 changes: 0 additions & 38 deletions modules/subdyn/src/SubDyn.f90
Original file line number Diff line number Diff line change
Expand Up @@ -4170,44 +4170,6 @@ SUBROUTINE SymMatDebug(M,MAT)

END SUBROUTINE SymMatDebug

FUNCTION is_numeric(string, x)
IMPLICIT NONE
CHARACTER(len=*), INTENT(IN) :: string
REAL(ReKi), INTENT(OUT) :: x
LOGICAL :: is_numeric
INTEGER :: e,n
CHARACTER(len=12) :: fmt
x = 0.0_ReKi
n=LEN_TRIM(string)
WRITE(fmt,'("(F",I0,".0)")') n
READ(string,fmt,IOSTAT=e) x
is_numeric = e == 0
END FUNCTION is_numeric

FUNCTION is_integer(string, x)
IMPLICIT NONE
CHARACTER(len=*), INTENT(IN) :: string
INTEGER(IntKi), INTENT(OUT) :: x
LOGICAL :: is_integer
INTEGER :: e, n
x = 0
n=LEN_TRIM(string)
READ(string,*,IOSTAT=e) x
is_integer = e == 0
END FUNCTION is_integer

FUNCTION is_logical(string, b)
IMPLICIT NONE
CHARACTER(len=*), INTENT(IN) :: string
Logical, INTENT(OUT) :: b
LOGICAL :: is_logical
INTEGER :: e,n
b = .false.
n=LEN_TRIM(string)
READ(string,*,IOSTAT=e) b
is_logical = e == 0
END FUNCTION is_logical

!> Parses a file for Kxx,Kxy,..Kxthtx,..Kxtz, Kytx, Kyty,..Kztz
SUBROUTINE ReadSSIfile ( Filename, JointID, SSIK, SSIM, ErrStat, ErrMsg, UnEc )
USE NWTC_IO
Expand Down
Loading

0 comments on commit 44b9a9a

Please sign in to comment.