From 31ea4dadf1b5e22b85903dcb2524f459f47c9f89 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Thu, 13 Aug 2015 14:54:34 +0000 Subject: [PATCH] IO_stringPos replacement not needing MAXNCHUNKS but making use of new Fortran features for dynamic allocation --- code/IO.f90 | 30 ++++++++++++++++++++++++++++++ code/mesh.f90 | 28 ++++++++++++++-------------- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/code/IO.f90 b/code/IO.f90 index d0a77f937..c98193eb3 100644 --- a/code/IO.f90 +++ b/code/IO.f90 @@ -56,6 +56,7 @@ module IO IO_spotTagInPart, & IO_globalTagInPart, & IO_stringPos, & + IO_stringPos2, & IO_stringValue, & IO_fixedStringValue ,& IO_floatValue, & @@ -1022,6 +1023,35 @@ pure function IO_stringPos(string,N) end function IO_stringPos +!-------------------------------------------------------------------------------------------------- +!> @brief locates at all space-separated parts in string and returns array containing number of +!! parts in string and the left/right positions to be used by IO_xxxVal +!! Array size is dynamically adjusted to number of chunks found in string +!! IMPORTANT: first element contains number of chunks! +!-------------------------------------------------------------------------------------------------- +pure function IO_stringPos2(string) + + implicit none + integer(pInt), dimension(:), allocatable :: IO_stringPos2 + character(len=*), intent(in) :: string !< string in which parts are searched for + + character(len=*), parameter :: SEP=achar(44)//achar(32)//achar(9)//achar(10)//achar(13) ! comma and whitespaces + integer :: left, right ! no pInt (verify and scan return default integer) + + allocate(IO_stringPos2(1), source=0_pInt) + right = 0 + + do while (verify(string(right+1:),SEP)>0) + left = right + verify(string(right+1:),SEP) + right = left + scan(string(left:),SEP) - 2 + if ( string(left:left) == '#' ) exit + IO_stringPos2 = [IO_stringPos2,int(left, pInt), int(right, pInt)] + IO_stringPos2(1) = IO_stringPos2(1)+1_pInt + enddo + +end function IO_stringPos2 + + !-------------------------------------------------------------------------------------------------- !> @brief reads string value at myPos from string !-------------------------------------------------------------------------------------------------- diff --git a/code/mesh.f90 b/code/mesh.f90 index c3b9f4ec8..135b54d3a 100644 --- a/code/mesh.f90 +++ b/code/mesh.f90 @@ -1042,7 +1042,7 @@ function mesh_spectral_getGrid(fileUnit) use IO, only: & IO_checkAndRewind, & IO_open_file, & - IO_stringPos, & + IO_stringPos2, & IO_lc, & IO_stringValue, & IO_intValue, & @@ -1054,7 +1054,7 @@ function mesh_spectral_getGrid(fileUnit) implicit none integer(pInt), dimension(3) :: mesh_spectral_getGrid integer(pInt), intent(in), optional :: fileUnit - integer(pInt), dimension(1_pInt + 7_pInt*2_pInt) :: positions ! for a,b,c + 3 values + keyword + integer(pInt), dimension(:), allocatable :: positions integer(pInt) :: headerLength = 0_pInt character(len=1024) :: line, & @@ -1073,7 +1073,7 @@ function mesh_spectral_getGrid(fileUnit) call IO_checkAndRewind(myFileUnit) read(myFileUnit,'(a1024)') line - positions = IO_stringPos(line,7_pInt) + positions = IO_stringPos2(line) keyword = IO_lc(IO_StringValue(line,positions,2_pInt,.true.)) if (keyword(1:4) == 'head') then headerLength = IO_intValue(line,positions,1_pInt) + 1_pInt @@ -1083,7 +1083,7 @@ function mesh_spectral_getGrid(fileUnit) rewind(myFileUnit) do i = 1_pInt, headerLength read(myFileUnit,'(a1024)') line - positions = IO_stringPos(line,7_pInt) + positions = IO_stringPos2(line) select case ( IO_lc(IO_StringValue(line,positions,1_pInt,.true.)) ) case ('grid') gotGrid = .true. @@ -1118,7 +1118,7 @@ function mesh_spectral_getSize(fileUnit) use IO, only: & IO_checkAndRewind, & IO_open_file, & - IO_stringPos, & + IO_stringPos2, & IO_lc, & IO_stringValue, & IO_intValue, & @@ -1130,7 +1130,7 @@ function mesh_spectral_getSize(fileUnit) implicit none real(pReal), dimension(3) :: mesh_spectral_getSize integer(pInt), intent(in), optional :: fileUnit - integer(pInt), dimension(1_pInt + 7_pInt*2_pInt) :: positions ! for x,y,z + 3 values + keyword + integer(pInt), dimension(:), allocatable :: positions integer(pInt) :: headerLength = 0_pInt character(len=1024) :: line, & keyword @@ -1148,7 +1148,7 @@ function mesh_spectral_getSize(fileUnit) call IO_checkAndRewind(myFileUnit) read(myFileUnit,'(a1024)') line - positions = IO_stringPos(line,7_pInt) + positions = IO_stringPos2(line) keyword = IO_lc(IO_StringValue(line,positions,2_pInt,.true.)) if (keyword(1:4) == 'head') then headerLength = IO_intValue(line,positions,1_pInt) + 1_pInt @@ -1158,7 +1158,7 @@ function mesh_spectral_getSize(fileUnit) rewind(myFileUnit) do i = 1_pInt, headerLength read(myFileUnit,'(a1024)') line - positions = IO_stringPos(line,7_pInt) + positions = IO_stringPos2(line) select case ( IO_lc(IO_StringValue(line,positions,1,.true.)) ) case ('size') gotSize = .true. @@ -1193,7 +1193,7 @@ integer(pInt) function mesh_spectral_getHomogenization(fileUnit) use IO, only: & IO_checkAndRewind, & IO_open_file, & - IO_stringPos, & + IO_stringPos2, & IO_lc, & IO_stringValue, & IO_intValue, & @@ -1203,7 +1203,7 @@ integer(pInt) function mesh_spectral_getHomogenization(fileUnit) implicit none integer(pInt), intent(in), optional :: fileUnit - integer(pInt), dimension(1_pInt + 7_pInt*2_pInt) :: positions ! for a, b, c + 3 values + keyword + integer(pInt), dimension(:), allocatable :: positions integer(pInt) :: headerLength = 0_pInt character(len=1024) :: line, & keyword @@ -1221,7 +1221,7 @@ integer(pInt) function mesh_spectral_getHomogenization(fileUnit) call IO_checkAndRewind(myFileUnit) read(myFileUnit,'(a1024)') line - positions = IO_stringPos(line,7_pInt) + positions = IO_stringPos2(line) keyword = IO_lc(IO_StringValue(line,positions,2_pInt,.true.)) if (keyword(1:4) == 'head') then headerLength = IO_intValue(line,positions,1_pInt) + 1_pInt @@ -1231,7 +1231,7 @@ integer(pInt) function mesh_spectral_getHomogenization(fileUnit) rewind(myFileUnit) do i = 1_pInt, headerLength read(myFileUnit,'(a1024)') line - positions = IO_stringPos(line,7_pInt) + positions = IO_stringPos2(line) select case ( IO_lc(IO_StringValue(line,positions,1,.true.)) ) case ('homogenization') gotHomogenization = .true. @@ -1346,7 +1346,7 @@ subroutine mesh_spectral_build_elements(fileUnit) IO_checkAndRewind, & IO_lc, & IO_stringValue, & - IO_stringPos, & + IO_stringPos2, & IO_error, & IO_continuousIntValues, & IO_intValue, & @@ -1381,7 +1381,7 @@ subroutine mesh_spectral_build_elements(fileUnit) ! get header length call IO_checkAndRewind(fileUnit) read(fileUnit,'(a65536)') line - myPos = IO_stringPos(line,7_pInt) + myPos = IO_stringPos2(line) keyword = IO_lc(IO_StringValue(line,myPos,2_pInt,.true.)) if (keyword(1:4) == 'head') then headerLength = IO_intValue(line,myPos,1_pInt) + 1_pInt