Fortran错误:变量的大小太大

时间:2016-01-19 17:21:35

标签: fortran size

我有一个很长的程序,目标是解决矩阵系统ax = b。当我运行它时,它会显示"错误:变量的大小太大"。

program ddm

  integer :: i,j,k
  integer, parameter :: FN=1,FML=80,FMH=80
  integer, parameter :: NBE=1*80*80 !NBE=FN*FML*FMH

  double precision, dimension(1:3*NBE,1:3*NBE) :: AA
  double precision, dimension(1:3*NBE) :: BB
  double precision :: XX(3*NBE)
  double precision, dimension(1:NBE) :: DSL,DSH,DNN
  double precision, dimension(1:FML,1:FMH) :: DSL1,DSH1,DNN1

  ! Construct a block matrix
  AA(1:NBE,1:NBE) = SLSL
  AA(1:NBE,NBE+1:2*NBE) = SLSH
  AA(1:NBE,2*NBE+1:3*NBE) = SLNN

  AA(NBE+1:2*NBE,1:NBE) = SHSL
  AA(NBE+1:2*NBE,NBE+1:2*NBE) = SHSH
  AA(NBE+1:2*NBE,2*NBE+1:3*NBE) = SHNN

  AA(2*NBE+1:3*NBE,1:NBE) = NNSL
  AA(2*NBE+1:3*NBE,NBE+1:2*NBE) = NNSH
  AA(2*NBE+1:3*NBE,2*NBE+1:3*NBE) = NNNN

  ! Construct a block matrix for boundary condition
  BB(1:NBE) = SLBC
  BB(NBE+1:2*NBE) = SHBC
  BB(2*NBE+1:3*NBE) = NNBC

  call GE(AA,BB,XX,3*NBE)

  DSL = XX(1:NBE)
  DSH = XX(NBE+1:2*NBE)
  DNN = XX(2*NBE+1:3*NBE)

  DSL1 = reshape(DSL,(/FML,FMH/))
  DSH1 = reshape(DSH,(/FML,FMH/))
  DNN1 = reshape(DNN,(/FML,FMH/))

  open(unit=2, file='DNN2.txt', ACTION="write", STATUS="replace")
  do i=1,80
      write(2,'(*(F14.7))') real(DNN1(i,:))
  end do

end program ddm

注意:GE(AA,BB,XX,3*NBE)是解决矩阵系统的函数。以下是GE的功能。

subroutine GE(a,b,x,n)
!===========================================================
! Solutions to a system of linear equations A*x=b
! Method: Gauss elimination (with scaling and pivoting)
!-----------------------------------------------------------
! input ...
! a(n,n) - array of coefficients for matrix A
! b(n) - array of the right hand coefficients b
! n - number of equations (size of matrix A)
! output ...
! x(n) - solutions
! coments ...
! the original arrays a(n,n) and b(n) will be destroyed
! during the calculation
!===========================================================

implicit none
integer n
double precision a(n,n),b(n),x(n)
double precision s(n)
double precision c, pivot, store
integer i, j, k, l

! step 1: begin forward elimination
do k=1, n-1

! step 2: "scaling"
! s(i) will have the largest element from row i
do i=k,n ! loop over rows
s(i) = 0.0
do j=k,n ! loop over elements of row i
s(i) = max(s(i),abs(a(i,j)))
end do
end do

! step 3: "pivoting 1"
! find a row with the largest pivoting element
pivot = abs(a(k,k)/s(k))
l = k
do j=k+1,n
if(abs(a(j,k)/s(j)) > pivot) then
pivot = abs(a(j,k)/s(j))
l = j
end if
end do
! Check if the system has a sigular matrix
if(pivot == 0.0) then
write(*,*) "The matrix is singular"
return
end if

! step 4: "pivoting 2" interchange rows k and l (if needed)
if (l /= k) then
do j=k,n
store = a(k,j)
a(k,j) = a(l,j)
a(l,j) = store
end do
store = b(k)
b(k) = b(l)
b(l) = store
end if

! step 5: the elimination (after scaling and pivoting)
do i=k+1,n
c=a(i,k)/a(k,k)
a(i,k) = 0.0
b(i)=b(i)- c*b(k)
do j=k+1,n
a(i,j) = a(i,j)-c*a(k,j)
end do
end do
end do

! step 6: back substiturion
x(n) = b(n)/a(n,n)
do i=n-1,1,-1
c=0.0
do j=i+1,n
c= c + a(i,j)*x(j)
end do
x(i) = (b(i)- c)/a(i,i)
end do

end subroutine GE

1 个答案:

答案 0 :(得分:3)

将数组(至少AABBXX)转换为可分配的数组,并在代码中自行分配。您正在达到静态分配的数组的内存限制。如果我记得很清楚,某些系统的限制为2GB(专家会确认或给出正确的数字)。