0
我有一個調用子函數的問題,該子函數返回大小未知的向量和數組。我不知道如何去掉我稱之爲函數的變量。該程序不會編譯,但隨着錯誤消息崩潰:Fortran:返回未知大小的向量和數組的調用子程序
Minnesegmentsfeil(核心轉儲)
這有點像在英語記憶段錯誤。
這是我的主要程序。
program transfer
! use konstanter
implicit none
! real :: f3eksitasjon
! real :: amp, w, F3a, eta3a
real, allocatable, dimension(:) :: Aw, Vp, Vtot, Ym, Zm, Zt, Ds
real, allocatable, dimension(:,:) :: Spos
integer, allocatable, dimension(:) :: Ns, Dir
integer :: i, N
call geometry(N, Aw, Vp, Vtot, Ym, Zm, Zt, Ds, Ns, Spos, DIR)
print *, N, Aw(1), Vp(1), Vtot(1), Ym(1), Zm(1), Ds(1), Ns(1), Spos(1,1), Spos(1,2), Dir(1)
end program transfer
子程序幾何從文件讀取。向量的長度和數組的大小取決於它讀取的文件。
subroutine geometry(N, Aw, Vp, Vtot, Ym, Zm, Zt, Ds, Ns, Spos, DIR)
implicit none
integer, intent(out) :: N
real, dimension(:), allocatable, intent(out):: Aw, Vp, Vtot, Ym, Zm, Zt, Ds
real, dimension(:,:), allocatable, intent(out) :: Spos
integer, dimension(:),allocatable, intent(out) :: Ns, DIR
real, dimension(:), allocatable :: Bp, Hp, Lp, Vs
real, parameter :: pi = 4.0*atan(1.0)
integer :: i
open(unit = 11, file = "semisub.dat")
read(11,*) N
allocate(Aw(N))
allocate(Vp(N))
allocate(Vtot(N))
allocate(Ym(N))
allocate(Zm(N))
allocate(Zt(N))
allocate(Ds(N))
allocate(Spos(N,4))
allocate(Ns(N))
allocate(DIR(N))
allocate(Hp(N))
do i = 1,N
read(11,*) Bp(i), Hp(i), Lp(i), Ym(i), Zm(i), DIR(i)
read(11,*) Ns(i), Ds(i)
If (Ns(i) > 8) STOP "Feil i semisub.dat. For mange sOyler"
read(11,*) Spos(i,1:Ns(i))
end do
Zt(:) = Zm(:) + Hp(:)/2
Aw(:) = 2 * Ds(:) * Ns(:)
Vp(:) = 2 * Lp(:) * Bp(:) * Hp(:)
Vs(:) = 2 * Ns(:) * pi * (Ds/2)**2 * (-Zt(:))
Vtot(:) = Vp(:) + Vs(:)
end subroutine geometry
這是一個如何安排代碼的示例,以便主程序將「知道」如何調用子程序,如IanH所述:http://stackoverflow.com/questions/6511711/computing-the-cross -fortran-90 –
在main程序中使用dimension(:)進行分配仍然是正確的,因爲主程序不知道子例程返回的維度? – SM411
是的。如果在編譯時數組維數未知,則用'allocatable'屬性聲明。分配可以在子程序中完成,但主例程必須知道該維度是「可變的」。 –