我在代碼中分配了很多二維數組,我希望每個數組都從一個名爲數組名的文件中讀取。問題是每個陣列都有不同的大小,所以我正在尋找最有效的方法。代碼是這樣的:fortran循環使用指針的二維數組列表
Module Test
USE ...
implicit NONE
private
public:: initializeTest, readFile
real(kind=8),dimension(:,:),allocatable,target:: ar1,ar2,ar3,ar4,ar5,...,ar10
real(kind=8),dimension(:,:),pointer:: pAr
CONTAINS
!
subroutine initializeTest
integer:: k1,k2,k3,k4,k5
integer:: ind1,ind2
allocate(ar1(k1,k1),ar2(k1,k2),ar3(k2,k4),ar4(k5,k5),...) !variable sizes
! here needs automatization - since its repeated
pAr => ar1
ind1 = size(pAr,1)
ind2 = size(pAr,2)
call readFile(par,ind1,ind2)
pAr => ar2
ind1 = size(pAr,1)
ind2 = size(pAr,2)
call readFile(par,ind1,ind2)
!....ar3, ... , ar9
pAr => ar10
ind1 = size(pAr,1)
ind2 = size(pAr,2)
call readFile(par,ind1,ind2)
end subroutine initializeTest
!
!
subroutine readFile(ar,row,col)
real(kind=8),dimension(row,col)
integer:: i,j,row,col
! it should open the file with same name as 'ar'
open(unit=111,file='ar.dat')
do i = 1, row
read(222,*) (ar(i,j),j=1,col)
enddo
end subroutine importFile
!
!
end module Test
我試過你的例子,它的工作原理。答案:指針是我對如何做到這一點的看法,而不是使用指針來完成的。我不知道那種= 8可以產生這些問題。 readFile對於公開並不重要。兩個問題:open(newunit = fd)似乎與f90一起使用,僅僅是f08的一個特性?還有一種方式來循環這個調用readFile(ar1,'ar1'到ar10,'ar10'),但是數組名稱是不同的,所以'ar1'可能'grav','ar2'可能'radius',等等。我可以創建一個數組變量名稱的列表並循環它嗎? – OverStacked
'newunit'是Fortran 2008,但它支持gfortran(自4.5開始,請參閱http://gcc.gnu.org/wiki/Fortran2008Status)。你的意思是「似乎與f90一起工作」?您是否在使用例如'-std = f95'來編譯? – steabert
不,我不使用std = 95的地方。我有一個makefile與intel編譯器編譯,但我的意思是模塊文件命名爲Module1.f90。 – OverStacked