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 character(len=pStringLen) :: line
integer :: myStat,fileUnit,s,e integer :: myStat,fileUnit,s,e
integer, allocatable, dimension(:) :: chunkPos
open(newunit=fileUnit, file=getSolverJobName()//INPUTFILEEXTENSION, & open(newunit=fileUnit, file=getSolverJobName()//INPUTFILEEXTENSION, &
status='old', position='rewind', action='read',iostat=myStat) status='old', position='rewind', action='read',iostat=myStat)

View File

@ -36,14 +36,14 @@ module IO
IO_warning IO_warning
#if defined(Marc4DAMASK) || defined(Abaqus) #if defined(Marc4DAMASK) || defined(Abaqus)
public :: & public :: &
IO_open_inputFile, & IO_open_inputFile
IO_continuousIntValues
#endif
#if defined(Abaqus) #if defined(Abaqus)
public :: & public :: &
IO_continuousIntValues, &
IO_extractValue, & IO_extractValue, &
IO_countDataLines IO_countDataLines
#endif #endif
#endif
contains contains
@ -120,7 +120,6 @@ function IO_read_ASCII(fileName) result(fileContent)
fileContent(l) = line fileContent(l) = line
l = l + 1 l = l + 1
enddo enddo
end function IO_read_ASCII end function IO_read_ASCII
@ -398,7 +397,7 @@ end function IO_stringValue
!-------------------------------------------------------------------------------------------------- !--------------------------------------------------------------------------------------------------
!> @brief reads float value at myChunk from string !> @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, 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 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 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 #ifdef Abaqus
!-------------------------------------------------------------------------------------------------- !--------------------------------------------------------------------------------------------------
!> @brief extracts string value from key=value pair and check whether key matches !> @brief extracts string value from key=value pair and check whether key matches
@ -847,7 +828,7 @@ integer function IO_countDataLines(fileUnit)
line = '' line = ''
do while (trim(line) /= IO_EOF) do while (trim(line) /= IO_EOF)
line = IO_read(fileUnit) read(fileUnit,'(A)') line
chunkPos = IO_stringPos(line) chunkPos = IO_stringPos(line)
tmp = IO_lc(IO_stringValue(line,chunkPos,1)) tmp = IO_lc(IO_stringValue(line,chunkPos,1))
if (tmp(1:1) == '*' .and. tmp(2:2) /= '*') then ! found keyword if (tmp(1:1) == '*' .and. tmp(2:2) /= '*') then ! found keyword
@ -859,14 +840,12 @@ integer function IO_countDataLines(fileUnit)
backspace(fileUnit) backspace(fileUnit)
end function IO_countDataLines end function IO_countDataLines
#endif
!-------------------------------------------------------------------------------------------------- !--------------------------------------------------------------------------------------------------
!> @brief return integer list corresponding to items in consecutive lines. !> @brief return integer list corresponding to items in consecutive lines.
!! First integer in array is counter !! First integer in array is counter
!> @details Marc: ints concatenated by "c" as last char, range of a "to" b, or named set !> @details Abaqus: triplet of start,stop,inc or named set
!! Abaqus: triplet of start,stop,inc or named set
!-------------------------------------------------------------------------------------------------- !--------------------------------------------------------------------------------------------------
function IO_continuousIntValues(fileUnit,maxN,lookupName,lookupMap,lookupMaxN) 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 integer, dimension(:,:), intent(in) :: lookupMap
character(len=*), dimension(:), intent(in) :: lookupName character(len=*), dimension(:), intent(in) :: lookupName
integer :: i,first,last integer :: i,first,last
#ifdef Abaqus
integer :: j,l,c integer :: j,l,c
#endif
integer, allocatable, dimension(:) :: chunkPos integer, allocatable, dimension(:) :: chunkPos
character(len=pStringLen) :: line character(len=pStringLen) :: line
logical :: rangeGeneration logical :: rangeGeneration
@ -888,41 +865,6 @@ function IO_continuousIntValues(fileUnit,maxN,lookupName,lookupMap,lookupMaxN)
IO_continuousIntValues = 0 IO_continuousIntValues = 0
rangeGeneration = .false. 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) c = IO_countDataLines(fileUnit)
do l = 1,c do l = 1,c
backspace(fileUnit) backspace(fileUnit)
@ -965,7 +907,6 @@ function IO_continuousIntValues(fileUnit,maxN,lookupName,lookupMap,lookupMaxN)
enddo enddo
endif endif
enddo enddo
#endif
100 end function IO_continuousIntValues 100 end function IO_continuousIntValues
#endif #endif
@ -976,7 +917,7 @@ function IO_continuousIntValues(fileUnit,maxN,lookupName,lookupMap,lookupMaxN)
!-------------------------------------------------------------------------------------------------- !--------------------------------------------------------------------------------------------------
!> @brief returns verified integer value in given string !> @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! character(len=*), intent(in) :: string, & !< string for conversion to int value. Must not contain spaces!
validChars, & !< valid characters in string validChars, & !< valid characters in string
@ -987,12 +928,12 @@ integer function verifyIntValue (string,validChars,myName)
invalidWhere = verify(string,validChars) invalidWhere = verify(string,validChars)
if (invalidWhere == 0) then 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 if (readStatus /= 0) & ! error during string to integer conversion
call IO_warning(203,ext_msg=myName//'"'//string//'"') call IO_warning(203,ext_msg=myName//'"'//string//'"')
else else
call IO_warning(202,ext_msg=myName//'"'//string//'"') ! complain about offending characters 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 if (readStatus /= 0) & ! error during string to integer conversion
call IO_warning(203,ext_msg=myName//'"'//string(1:invalidWhere-1)//'"') call IO_warning(203,ext_msg=myName//'"'//string(1:invalidWhere-1)//'"')
endif endif
@ -1003,7 +944,7 @@ end function verifyIntValue
!-------------------------------------------------------------------------------------------------- !--------------------------------------------------------------------------------------------------
!> @brief returns verified float value in given string !> @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! character(len=*), intent(in) :: string, & !< string for conversion to int value. Must not contain spaces!
validChars, & !< valid characters in string validChars, & !< valid characters in string
@ -1015,12 +956,12 @@ real(pReal) function verifyFloatValue (string,validChars,myName)
invalidWhere = verify(string,validChars) invalidWhere = verify(string,validChars)
if (invalidWhere == 0) then 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 if (readStatus /= 0) & ! error during string to float conversion
call IO_warning(203,ext_msg=myName//'"'//string//'"') call IO_warning(203,ext_msg=myName//'"'//string//'"')
else else
call IO_warning(202,ext_msg=myName//'"'//string//'"') ! complain about offending characters 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 if (readStatus /= 0) & ! error during string to float conversion
call IO_warning(203,ext_msg=myName//'"'//string(1:invalidWhere-1)//'"') call IO_warning(203,ext_msg=myName//'"'//string(1:invalidWhere-1)//'"')
endif endif

View File

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