我是Fortran的先驱,用它来解决经济学中的数学模型。 我写了一个程序(下面),在运行时崩溃了。 Windows显示错误消息似乎没有发生。我还没有找到可能出错的线索,我已经调试过它并且在构建日志中没有显示任何错误消息。 我正在使用gfortran。
!Value function iteration program with matrices, based on the matlab similar code
program valuefuncmat
implicit none
integer,parameter::dp=selected_real_kind(15) !double precision
integer,parameter::lower=0,upper=50 !lower and upper bounds on capital grid (lower = b in the model)
real(dp),parameter::hex1=.01,hex2=.1 !steps on capital grid, finer on low levels of capital
integer::i,j !loop counters
integer,parameter::sze1=((15-lower)/hex1)+1,sze2=( ((upper-15)/hex2)+1)
integer,parameter::sze=sze1+sze2
real(dp)::alinha1(sze1),alinha2(sze2),agrid(sze)
11 format(i5)
12 format(f35.10)
interface !interface of the functions used
function kron(A,B)
implicit none
integer,parameter::dp=selected_real_kind(15) !double precision
real(dp),dimension(:,:), intent(in)::A,B !dummy arguments
real(dp),dimension(size(A,1)*size(B,1),size(A,2)*size(B,2))::kron
end function kron
function repvec(A,n)
implicit none
integer,parameter::dp=selected_real_kind(15) !double precision, not essencial in the code
real(dp),dimension(:), intent(in)::A
integer::n
real(dp),dimension(n,size(A))::repvec
end function repvec
function repmat(A,n)
implicit none
integer,parameter::dp=selected_real_kind(15) !double precision, not essencial in the code
real(dp),dimension(:,:), intent(in)::A
integer::n
real(dp),dimension(size(A)*n,size(A))::repmat
end function repmat
function kronvec(A,V)
implicit none
integer,parameter::dp=selected_real_kind(15) !double precision, not essencial in the code
real(dp),dimension(:,:),intent(in)::A
integer,dimension(:),intent(in)::V
real(dp),dimension(size(A,1)*size(V),size(A,2))::kronvec
end function kronvec
function kronvec2(A,V)
implicit none
integer,parameter::dp=selected_real_kind(15) !double precision, not essencial in the code
integer,dimension(:,:),intent(in)::A
real(dp),dimension(:),intent(in)::V
real(dp),dimension(size(A,1)*size(V),size(A,2))::kronvec2
end function kronvec2
end interface
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!Markov Matrix and Productivity Grid!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
real(dp), dimension(3,3), parameter::P=reshape((/ .91,.08,.01,.13,.82,.05,.005,.07,.925 /),(/3,3/),(/.0/),(/2,1/)) !markov matrix
real(dp), dimension(3), parameter::theta_grid=(/ .1,.5,1.0 /) !the grid of productivity
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
real(dp),parameter::A=1.0,alpha=.25,beta=.98 !Parameters of Functional Forms
real(dp),parameter::r=0.05,w=1.0,T=1.0 !Parameters for testing the value function iteration
integer,parameter::y=1
real(dp),dimension(size(theta_grid),size(agrid))::vIN=.0,vOUT=1.0,apol
real(dp)::transf(size(theta_grid)*size(agrid),size(agrid)),amatrix(size(agrid),size(agrid))
real(dp)::C(size(agrid)*size(theta_grid),size(agrid))
real(dp)::Rzao(size(agrid)*size(theta_grid),size(agrid)),tvOUT(size(theta_grid)*size(agrid))
integer::indexa(size(theta_grid)*size(agrid)),um(size(agrid))=1,onesm(size(theta_grid))=1,umzao(size(agrid),size(agrid))=1
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!initializing the capital grid!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
do i=1,sze1
alinha1(i)=lower+(i-1)*hex1
end do
do i=1,sze2
alinha2(i)=alinha1((sze1))+hex2 +(i-1)*hex2
end do
agrid(:sze1)=alinha1
agrid(sze1+1:sze)=alinha2
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!Beginning the actual value function iteration algorithm!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
transf(1:y*size(agrid),:)=T !defining the types that will receive the transfer
Amatrix=transpose(repvec((1+r)*agrid,size(agrid)))-repvec(agrid,size(agrid)) !every combination of a and a' on square matrix of rank equal to the size of agrid
C=repmat(Amatrix,size(theta_grid))+w*kronvec2(umzao,theta_grid)+transf
where (C<=0) C=0.00000000000000001
Rzao=log(C)
do while (maxval(abs(vIN-vOUT))>0.001)
vIN=vOUT
tvOUT=maxval(Rzao+beta*matmul(kronvec(P,um),vIN),dim=2)
indexa=maxloc(Rzao+beta*matmul(kronvec(P,um),vIN),dim=2)
vOUT=reshape(tvOUT,(/size(theta_grid),size(agrid)/),order=(/2,1/))
end do
do i=1,size(vOUT,1)
write(*,'(20G12.4)'),vOUT(i,:)
end do
end program
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!Kronecker Product Routine!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
function kron(A,B)
implicit none
integer,parameter::dp=selected_real_kind(15) !double precision, not essencial in the code
real(dp),dimension(:,:), intent(in)::A,B !dummy arguments
real(dp),dimension(size(A,1)*size(B,1),size(A,2)*size(B,2))::kron !output matrix of the kronecker product
integer::i,j !loop counters
do i=1,size(A,1)
do j=1,size(A,2)
kron(1+size(B,1)*(i-1):size(B,1)+size(B,1)*(i-1),1+size(B,2)*(j-1):size(B,2)+size(B,2)*(j-1))=A(i,j)*B
end do
end do
end function kron
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!Function that calculates a kronecker product between a matrix and a vector!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Vector must be integer!!!!!!!!!!!!!!!!!!!!!!!!!!!
function kronvec(A,V)
implicit none
integer,parameter::dp=selected_real_kind(15) !double precision, not essencial in the code
real(dp),dimension(:,:),intent(in)::A
integer,dimension(:),intent(in)::V
real(dp),dimension(size(A,1)*size(V),size(A,2))::kronvec
integer::i
do i=1,size(V)
kronvec(1+size(A,1)*(i-1):size(A,1)+size(A,1)*(i-1),:)=A*V(i)
end do
end function kronvec
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!Function that calculates a kronecker product between a matrix and a vector!!!!
!!!!!!!!!!!!!!!Matrix must be integer and vector real!!!!!!!!!!!!!!!!!!!!!!!!!!!
function kronvec2(A,V)
implicit none
integer,parameter::dp=selected_real_kind(15) !double precision, not essencial in the code
integer,dimension(:,:),intent(in)::A
real(dp),dimension(:),intent(in)::V
real(dp),dimension(size(A,1)*size(V),size(A,2))::kronvec2
integer::i
do i=1,size(V)
kronvec2(1+size(A,1)*(i-1):size(A,1)+size(A,1)*(i-1),:)=A*V(i)
end do
end function kronvec2
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!Function that repeats a vector n times verticaly!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
function repvec(A,n)
implicit none
integer,parameter::dp=selected_real_kind(15) !double precision, not essencial in the code
real(dp),dimension(:), intent(in)::A
integer::n
real(dp),dimension(n,size(A))::repvec
integer::i
do i=1,n
repvec(i,:)=A
end do
end function repvec
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!Function that repeats a matrix n times verticaly!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
function repmat(A,n)
implicit none
integer,parameter::dp=selected_real_kind(15) !double precision, not essencial in the code
real(dp),dimension(:,:), intent(in)::A
integer::n
real(dp),dimension(size(A)*n,size(A))::repmat
integer::i
do i=1,size(A)
repmat(1+size(A)*(i-1):size(A)+size(A)*(i-1),:)=A
end do
end function repmat
答案 0 :(得分:2)
我通过NAG Fortran编译器(在Linux上)运行代码。您发布的版本不会为我编译:
> nagfor -mtrace=all -u -C=all -C=undefined valuefuncmat.f90
...
Error: valuefuncmat.f90, line 100: Syntax error
detected at )@,
...
当然这很容易纠正(即将第100行改为write(*,'(20G12.4)') vOUT(i,:)
)。
完成后,重新编译并运行显示
> nagfor -mtrace=all -u -C=all -C=undefined valuefuncmat.f90 && ./a.out
NAG Fortran Compiler Release 6.0(Hibiya) Build 1050
Extension: valuefuncmat.f90, line 88: Line longer than 132 characters
Warning: valuefuncmat.f90, line 104: Unused local variable APOL
Questionable: valuefuncmat.f90, line 104: Variable INDEXA set but never referenced
Warning: valuefuncmat.f90, line 104: Unused local variable J
Warning: valuefuncmat.f90, line 104: Unused interface for procedure KRON
Warning: valuefuncmat.f90, line 104: Local variable ONESM is initialised but never used
Warning: valuefuncmat.f90, line 62: Unused PARAMETER A
Warning: valuefuncmat.f90, line 62: Unused PARAMETER ALPHA
[NAG Fortran Compiler normal termination, 8 warnings]
[Allocated item 1 (size 65537) = Z'2AE913113010']
[Allocated item 2 (size 65537) = Z'2AE913123030']
[Allocated item 3 (size 65537) = Z'2AE913133050']
[Allocated item 4 (size 14808) at line 88 of valuefuncmat.f90 = Z'2AE913143070']
[Allocated item 5 (size 27409608) at line 154 of valuefuncmat.f90 = Z'2AE913413010']
[Allocated item 6 (size 27409608) at line 154 of valuefuncmat.f90 = Z'2AE914E37010']
[Deallocated item 6 (size 27409608, at Z'2AE914E37010') at line 166 of valuefuncmat.f90]
[Allocated item 7 (size 27409608) at line 154 of valuefuncmat.f90 = Z'2AE914E37010']
[Allocated item 8 (size 27409608) at line 154 of valuefuncmat.f90 = Z'2AE91685B010']
[Deallocated item 8 (size 27409608, at Z'2AE91685B010') at line 166 of valuefuncmat.f90]
[Deallocated item 4 (size 14808, at Z'2AE913143070') at line 88 of valuefuncmat.f90]
[Deallocated item 5 (size 27409608, at Z'2AE913413010') at line 88 of valuefuncmat.f90]
[Deallocated item 7 (size 27409608, at Z'2AE914E37010') at line 88 of valuefuncmat.f90]
[Allocation (size 281732479017624) at line 170 of valuefuncmat.f90 failed]
Runtime Error: valuefuncmat.f90, line 170: Cannot allocate array temporary - out of memory
Program terminated by fatal error
[Deallocated item 2 (size 65537, at Z'2AE913123030')]
[Deallocated item 3 (size 65537, at Z'2AE913133050')]
[Deallocated item 1 (size 65537, at Z'2AE913113010')]
Abort (core dumped)
让我重点介绍一下内存跟踪输出:
[Allocation (size 281732479017624) at line 170 of valuefuncmat.f90 failed]
正如roygvib在评论中指出的那样,这似乎是您需要解决的问题。