PP#8~Fortran Tips#3
第三回のテーマは本題の入力データ”10*1234.0″を数値データとして読み込むサブルーチンを作成します。このような事例の使用頻度は低いですが、大規模な行列計算では良く使われます。因みに、”10*1234.0″とは実数1234.0を10回読み込むという指示になります。
メインのサブルーチンReadDblArrayから、コアのサブルーチンstrtoimdを呼び出しています。
subroutine ReadDblArray(Line, LineMax, Inx, Nx, DblArray)
!********************************************************************************
!* Read the Double Array *
!********************************************************************************
implicit none
integer,intent(in):: Nx, LineMax
integer,intent(inout):: Inx
real*8,intent(out):: DblArray(Nx)
character,intent(inout)::Line*(*)
integer:: NumWord, NumValue, IntValue(Nx), i, j
real*8:: DblValue(Nx)
character:: Words(Nx)*LineMax
!================================================================================
! Line : Line string given
! LineMax : Max characters in Line
! Inx : Index of the DblArray
! Nx : Number of array of the words comprised of the line
! DblArray : Array to be inputed
! NumWord : Number of words in the Line
! Words : Array of the words comprised of the line
! NumValue : Number of values
! IntValue(Nx): Integer value extracted from the Line
! DblValue(Nx): Double value extracted from the Line
!================================================================================
! Extract IntValue and DblValue from the line string given
!================================================================================
call strtoimd(Line, Nx, Words, NumValue, IntValue, DblValue)
!================================================================================
! Input DbleArray given
!================================================================================
do i=1,NumValue
do j=1,IntValue(i)
Inx = Inx + 1
DblArray(Inx) = DblValue(i)
enddo
enddo
!================================================================================
end subroutine
目指すイメージはこんな感じです。
呼び出すルーチンcall_ReadDblArrayのコードです。Nxは10に仮設定しています。
program call_ReadDblArray
!********************************************************************************
!* call_ReadDblArray *
!********************************************************************************
implicit none
integer,parameter:: fin = 5
integer,parameter:: Nx = 10, LINEMAX = 130
character(len=LINEMAX)::inkey
integer:: Inx, ip
real*8:: DblArray(Nx)
!================================================================================
do
call getline(fin, inkey)
print '("Key to be input (/ to exit): ", a)', inkey
if (inkey == '/') exit
Inx = 0; call ReadDblArray(inkey, LINEMAX, Inx, Nx, DblArray)
print '("Value to be output:")'
do ip=1,Inx
print '("DblArray (",i2,") = ",f20.7)', ip, DblArray(ip)
enddo
enddo
print '("Reached end of program")'
!================================================================================
stop
!================================================================================
contains
...........
end program
要のサブルーチンstrtoimdの中身は次回までお預け。
皆さんもとくとお考えあれ。
つづく
