avoid direct file operations

This commit is contained in:
Martin Diehl 2020-01-25 13:30:42 +01:00
parent a2e4160e61
commit 8a2a9d3861
3 changed files with 35 additions and 114 deletions

View File

@ -119,7 +119,6 @@ logical function solverIsSymmetric()
character(len=pStringLen) :: line
integer :: myStat,fileUnit,s,e
integer, allocatable, dimension(:) :: chunkPos
open(newunit=fileUnit, file=getSolverJobName()//INPUTFILEEXTENSION, &
status='old', position='rewind', action='read',iostat=myStat)

View File

@ -36,14 +36,14 @@ module IO
IO_warning
#if defined(Marc4DAMASK) || defined(Abaqus)
public :: &
IO_open_inputFile, &
IO_continuousIntValues
#endif
IO_open_inputFile
#if defined(Abaqus)
public :: &
IO_continuousIntValues, &
IO_extractValue, &
IO_countDataLines
#endif
#endif
contains
@ -120,7 +120,6 @@ function IO_read_ASCII(fileName) result(fileContent)
fileContent(l) = line
l = l + 1
enddo
end function IO_read_ASCII
@ -398,7 +397,7 @@ end function IO_stringValue
!--------------------------------------------------------------------------------------------------
!> @brief reads float value at myChunk from string
!--------------------------------------------------------------------------------------------------
real(pReal) function IO_floatValue (string,chunkPos,myChunk)
real(pReal) function IO_floatValue(string,chunkPos,myChunk)
integer, dimension(:), intent(in) :: chunkPos !< positions of start and end of each tag/chunk in given string
integer, intent(in) :: myChunk !< position number of desired chunk
@ -792,24 +791,6 @@ subroutine IO_warning(warning_ID,el,ip,g,ext_msg)
end subroutine IO_warning
#if defined(Abaqus) || defined(Marc4DAMASK)
!--------------------------------------------------------------------------------------------------
!> @brief reads a line from a text file.
!--------------------------------------------------------------------------------------------------
function IO_read(fileUnit) result(line)
integer, intent(in) :: fileUnit !< file unit
character(len=pStringLen) :: line
read(fileUnit,'(A)',END=100) line
100 end function IO_read
#ifdef Abaqus
!--------------------------------------------------------------------------------------------------
!> @brief extracts string value from key=value pair and check whether key matches
@ -847,7 +828,7 @@ integer function IO_countDataLines(fileUnit)
line = ''
do while (trim(line) /= IO_EOF)
line = IO_read(fileUnit)
read(fileUnit,'(A)') line
chunkPos = IO_stringPos(line)
tmp = IO_lc(IO_stringValue(line,chunkPos,1))
if (tmp(1:1) == '*' .and. tmp(2:2) /= '*') then ! found keyword
@ -859,14 +840,12 @@ integer function IO_countDataLines(fileUnit)
backspace(fileUnit)
end function IO_countDataLines
#endif
!--------------------------------------------------------------------------------------------------
!> @brief return integer list corresponding to items in consecutive lines.
!! First integer in array is counter
!> @details Marc: ints concatenated by "c" as last char, range of a "to" b, or named set
!! Abaqus: triplet of start,stop,inc or named set
!> @details Abaqus: triplet of start,stop,inc or named set
!--------------------------------------------------------------------------------------------------
function IO_continuousIntValues(fileUnit,maxN,lookupName,lookupMap,lookupMaxN)
@ -878,9 +857,7 @@ function IO_continuousIntValues(fileUnit,maxN,lookupName,lookupMap,lookupMaxN)
integer, dimension(:,:), intent(in) :: lookupMap
character(len=*), dimension(:), intent(in) :: lookupName
integer :: i,first,last
#ifdef Abaqus
integer :: j,l,c
#endif
integer, allocatable, dimension(:) :: chunkPos
character(len=pStringLen) :: line
logical :: rangeGeneration
@ -888,41 +865,6 @@ function IO_continuousIntValues(fileUnit,maxN,lookupName,lookupMap,lookupMaxN)
IO_continuousIntValues = 0
rangeGeneration = .false.
#if defined(Marc4DAMASK)
do
read(fileUnit,'(A)',end=100) line
chunkPos = IO_stringPos(line)
if (chunkPos(1) < 1) then ! empty line
exit
elseif (verify(IO_stringValue(line,chunkPos,1),'0123456789') > 0) then ! a non-int, i.e. set name
do i = 1, lookupMaxN ! loop over known set names
if (IO_stringValue(line,chunkPos,1) == lookupName(i)) then ! found matching name
IO_continuousIntValues = lookupMap(:,i) ! return resp. entity list
exit
endif
enddo
exit
else if (chunkPos(1) > 2 .and. IO_lc(IO_stringValue(line,chunkPos,2)) == 'to' ) then ! found range indicator
first = IO_intValue(line,chunkPos,1)
last = IO_intValue(line,chunkPos,3)
do i = first, last, sign(1,last-first)
IO_continuousIntValues(1) = IO_continuousIntValues(1) + 1
IO_continuousIntValues(1+IO_continuousIntValues(1)) = i
enddo
exit
else
do i = 1,chunkPos(1)-1 ! interpret up to second to last value
IO_continuousIntValues(1) = IO_continuousIntValues(1) + 1
IO_continuousIntValues(1+IO_continuousIntValues(1)) = IO_intValue(line,chunkPos,i)
enddo
if ( IO_lc(IO_stringValue(line,chunkPos,chunkPos(1))) /= 'c' ) then ! line finished, read last value
IO_continuousIntValues(1) = IO_continuousIntValues(1) + 1
IO_continuousIntValues(1+IO_continuousIntValues(1)) = IO_intValue(line,chunkPos,chunkPos(1))
exit
endif
endif
enddo
#elif defined(Abaqus)
c = IO_countDataLines(fileUnit)
do l = 1,c
backspace(fileUnit)
@ -965,7 +907,6 @@ function IO_continuousIntValues(fileUnit,maxN,lookupName,lookupMap,lookupMaxN)
enddo
endif
enddo
#endif
100 end function IO_continuousIntValues
#endif
@ -976,7 +917,7 @@ function IO_continuousIntValues(fileUnit,maxN,lookupName,lookupMap,lookupMaxN)
!--------------------------------------------------------------------------------------------------
!> @brief returns verified integer value in given string
!--------------------------------------------------------------------------------------------------
integer function verifyIntValue (string,validChars,myName)
integer function verifyIntValue(string,validChars,myName)
character(len=*), intent(in) :: string, & !< string for conversion to int value. Must not contain spaces!
validChars, & !< valid characters in string
@ -987,12 +928,12 @@ integer function verifyIntValue (string,validChars,myName)
invalidWhere = verify(string,validChars)
if (invalidWhere == 0) then
read(UNIT=string,iostat=readStatus,FMT=*) verifyIntValue ! no offending chars found
read(string,*,iostat=readStatus) verifyIntValue ! no offending chars found
if (readStatus /= 0) & ! error during string to integer conversion
call IO_warning(203,ext_msg=myName//'"'//string//'"')
else
call IO_warning(202,ext_msg=myName//'"'//string//'"') ! complain about offending characters
read(UNIT=string(1:invalidWhere-1),iostat=readStatus,FMT=*) verifyIntValue ! interpret remaining string
read(string(1:invalidWhere-1),*,iostat=readStatus) verifyIntValue ! interpret remaining string
if (readStatus /= 0) & ! error during string to integer conversion
call IO_warning(203,ext_msg=myName//'"'//string(1:invalidWhere-1)//'"')
endif
@ -1003,7 +944,7 @@ end function verifyIntValue
!--------------------------------------------------------------------------------------------------
!> @brief returns verified float value in given string
!--------------------------------------------------------------------------------------------------
real(pReal) function verifyFloatValue (string,validChars,myName)
real(pReal) function verifyFloatValue(string,validChars,myName)
character(len=*), intent(in) :: string, & !< string for conversion to int value. Must not contain spaces!
validChars, & !< valid characters in string
@ -1015,12 +956,12 @@ real(pReal) function verifyFloatValue (string,validChars,myName)
invalidWhere = verify(string,validChars)
if (invalidWhere == 0) then
read(UNIT=string,iostat=readStatus,FMT=*) verifyFloatValue ! no offending chars found
read(string,*,iostat=readStatus) verifyFloatValue ! no offending chars found
if (readStatus /= 0) & ! error during string to float conversion
call IO_warning(203,ext_msg=myName//'"'//string//'"')
else
call IO_warning(202,ext_msg=myName//'"'//string//'"') ! complain about offending characters
read(UNIT=string(1:invalidWhere-1),iostat=readStatus,FMT=*) verifyFloatValue ! interpret remaining string
read(string(1:invalidWhere-1),*,iostat=readStatus) verifyFloatValue ! interpret remaining string
if (readStatus /= 0) & ! error during string to float conversion
call IO_warning(203,ext_msg=myName//'"'//string(1:invalidWhere-1)//'"')
endif

View File

@ -184,8 +184,6 @@ subroutine inputRead(elem,node0_elem,connectivity_elem,microstructureAt,homogeni
initialcondTableStyle, &
nNodes, &
nElems
integer, parameter :: &
FILEUNIT = 222
integer, dimension(:), allocatable :: &
matNumber !< material numbers for hypoelastic material
character(len=pStringLen), dimension(:), allocatable :: inputFile !< file content, separated per lines
@ -225,12 +223,9 @@ subroutine inputRead(elem,node0_elem,connectivity_elem,microstructureAt,homogeni
connectivity_elem = inputRead_connectivityElem(nElems,elem%nNodes,inputFile)
call IO_open_inputFile(FILEUNIT) ! ToDo: It would be better to use fileContent
call inputRead_microstructureAndHomogenization(microstructureAt,homogenizationAt, &
nElems,elem%nNodes,nameElemSet,mapElemSet,&
initialcondTableStyle,FILEUNIT)
close(FILEUNIT)
initialcondTableStyle,inputFile)
end subroutine inputRead
@ -656,7 +651,7 @@ end function inputRead_connectivityElem
!> @brief Stores homogenization and microstructure ID
!--------------------------------------------------------------------------------------------------
subroutine inputRead_microstructureAndHomogenization(microstructureAt,homogenizationAt, &
nElem,nNodes,nameElemSet,mapElemSet,initialcondTableStyle,fileUnit)
nElem,nNodes,nameElemSet,mapElemSet,initialcondTableStyle,fileContent)
integer, dimension(:), allocatable, intent(out) :: &
microstructureAt, &
@ -664,60 +659,46 @@ subroutine inputRead_microstructureAndHomogenization(microstructureAt,homogeniza
integer, intent(in) :: &
nElem, &
nNodes, & !< number of nodes per element
initialcondTableStyle, &
fileUnit
character(len=64), dimension(:), intent(in) :: &
nameElemSet
integer, dimension(:,:), intent(in) :: &
mapElemSet !< list of elements in elementSet
initialcondTableStyle
character(len=*), dimension(:), intent(in) :: nameElemSet
integer, dimension(:,:), intent(in) :: mapElemSet !< list of elements in elementSet
character(len=*), dimension(:), intent(in) :: fileContent !< file content, separated per lines
integer, allocatable, dimension(:) :: chunkPos
character(len=300) line
integer, dimension(1+nElem) :: contInts
integer :: i,j,t,sv,myVal,e,nNodesAlreadyRead
integer :: i,j,t,sv,myVal,e,nNodesAlreadyRead,l,k,m
allocate(microstructureAt(nElem),source=0)
allocate(homogenizationAt(nElem),source=0)
rewind(fileUnit)
read (fileUnit,'(A)',END=630) line
do
chunkPos = IO_stringPos(line)
if( (IO_lc(IO_stringValue(line,chunkPos,1)) == 'initial') .and. &
(IO_lc(IO_stringValue(line,chunkPos,2)) == 'state') ) then
if (initialcondTableStyle == 2) read (fileUnit,'(A)',END=630) line ! read extra line for new style
read (fileUnit,'(A)',END=630) line ! read line with index of state var
chunkPos = IO_stringPos(line)
sv = IO_IntValue(line,chunkPos,1) ! figure state variable index
if( (sv == 2).or.(sv == 3) ) then ! only state vars 2 and 3 of interest
read (fileUnit,'(A)',END=630) line ! read line with value of state var
chunkPos = IO_stringPos(line)
do while (scan(IO_stringValue(line,chunkPos,1),'+-',back=.true.)>1) ! is noEfloat value?
myVal = nint(IO_floatValue(line,chunkPos,1))
if (initialcondTableStyle == 2) then
read (fileUnit,'(A)',END=630) line ! read extra line
read (fileUnit,'(A)',END=630) line ! read extra line
endif
contInts = IO_continuousIntValues& ! get affected elements
(fileUnit,nElem,nameElemSet,mapElemSet,size(nameElemSet))
do l = 1, size(fileContent)
chunkPos = IO_stringPos(fileContent(l))
if( (IO_lc(IO_stringValue(fileContent(l),chunkPos,1)) == 'initial') .and. &
(IO_lc(IO_stringValue(fileContent(l),chunkPos,2)) == 'state') ) then
k = merge(2,1,initialcondTableStyle == 2)
chunkPos = IO_stringPos(fileContent(l+k))
sv = IO_IntValue(fileContent(l+k),chunkPos,1) ! figure state variable index
if( (sv == 2) .or. (sv == 3) ) then ! only state vars 2 and 3 of interest
m = 1
chunkPos = IO_stringPos(fileContent(l+k+m))
do while (scan(IO_stringValue(fileContent(l+k+m),chunkPos,1),'+-',back=.true.)>1) ! is noEfloat value?
myVal = nint(IO_floatValue(fileContent(l+k+m),chunkPos,1))
if (initialcondTableStyle == 2) m = m + 2
contInts = continuousIntValues(fileContent(l+k+m+1:),nElem,nameElemSet,mapElemSet,size(nameElemSet)) ! get affected elements
do i = 1,contInts(1)
e = mesh_FEasCP('elem',contInts(1+i))
if (sv == 2) microstructureAt(e) = myVal
if (sv == 3) homogenizationAt(e) = myVal
enddo
if (initialcondTableStyle == 0) read (fileUnit,'(A)',END=630) line ! ignore IP range for old table style
read (fileUnit,'(A)',END=630) line
chunkPos = IO_stringPos(line)
if (initialcondTableStyle == 0) m = m + 1
enddo
endif
else
read (fileUnit,'(A)',END=630) line
endif
enddo
630 end subroutine inputRead_microstructureAndHomogenization
end subroutine inputRead_microstructureAndHomogenization
!--------------------------------------------------------------------------------------------------